psycho_common.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* psycho_common.c: Code common to PSYCHO and derivative PCI controllers.
  3. *
  4. * Copyright (C) 2008 David S. Miller <davem@davemloft.net>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/numa.h>
  9. #include <linux/platform_device.h>
  10. #include <asm/upa.h>
  11. #include "pci_impl.h"
  12. #include "iommu_common.h"
  13. #include "psycho_common.h"
  14. #define PSYCHO_STRBUF_CTRL_DENAB 0x0000000000000002ULL
  15. #define PSYCHO_STCERR_WRITE 0x0000000000000002ULL
  16. #define PSYCHO_STCERR_READ 0x0000000000000001ULL
  17. #define PSYCHO_STCTAG_PPN 0x0fffffff00000000ULL
  18. #define PSYCHO_STCTAG_VPN 0x00000000ffffe000ULL
  19. #define PSYCHO_STCTAG_VALID 0x0000000000000002ULL
  20. #define PSYCHO_STCTAG_WRITE 0x0000000000000001ULL
  21. #define PSYCHO_STCLINE_LINDX 0x0000000001e00000ULL
  22. #define PSYCHO_STCLINE_SPTR 0x00000000001f8000ULL
  23. #define PSYCHO_STCLINE_LADDR 0x0000000000007f00ULL
  24. #define PSYCHO_STCLINE_EPTR 0x00000000000000fcULL
  25. #define PSYCHO_STCLINE_VALID 0x0000000000000002ULL
  26. #define PSYCHO_STCLINE_FOFN 0x0000000000000001ULL
  27. static DEFINE_SPINLOCK(stc_buf_lock);
  28. static unsigned long stc_error_buf[128];
  29. static unsigned long stc_tag_buf[16];
  30. static unsigned long stc_line_buf[16];
  31. static void psycho_check_stc_error(struct pci_pbm_info *pbm)
  32. {
  33. unsigned long err_base, tag_base, line_base;
  34. struct strbuf *strbuf = &pbm->stc;
  35. u64 control;
  36. int i;
  37. if (!strbuf->strbuf_control)
  38. return;
  39. err_base = strbuf->strbuf_err_stat;
  40. tag_base = strbuf->strbuf_tag_diag;
  41. line_base = strbuf->strbuf_line_diag;
  42. spin_lock(&stc_buf_lock);
  43. /* This is __REALLY__ dangerous. When we put the streaming
  44. * buffer into diagnostic mode to probe its tags and error
  45. * status, we _must_ clear all of the line tag valid bits
  46. * before re-enabling the streaming buffer. If any dirty data
  47. * lives in the STC when we do this, we will end up
  48. * invalidating it before it has a chance to reach main
  49. * memory.
  50. */
  51. control = upa_readq(strbuf->strbuf_control);
  52. upa_writeq(control | PSYCHO_STRBUF_CTRL_DENAB, strbuf->strbuf_control);
  53. for (i = 0; i < 128; i++) {
  54. u64 val;
  55. val = upa_readq(err_base + (i * 8UL));
  56. upa_writeq(0UL, err_base + (i * 8UL));
  57. stc_error_buf[i] = val;
  58. }
  59. for (i = 0; i < 16; i++) {
  60. stc_tag_buf[i] = upa_readq(tag_base + (i * 8UL));
  61. stc_line_buf[i] = upa_readq(line_base + (i * 8UL));
  62. upa_writeq(0UL, tag_base + (i * 8UL));
  63. upa_writeq(0UL, line_base + (i * 8UL));
  64. }
  65. /* OK, state is logged, exit diagnostic mode. */
  66. upa_writeq(control, strbuf->strbuf_control);
  67. for (i = 0; i < 16; i++) {
  68. int j, saw_error, first, last;
  69. saw_error = 0;
  70. first = i * 8;
  71. last = first + 8;
  72. for (j = first; j < last; j++) {
  73. u64 errval = stc_error_buf[j];
  74. if (errval != 0) {
  75. saw_error++;
  76. printk(KERN_ERR "%s: STC_ERR(%d)[wr(%d)"
  77. "rd(%d)]\n",
  78. pbm->name,
  79. j,
  80. (errval & PSYCHO_STCERR_WRITE) ? 1 : 0,
  81. (errval & PSYCHO_STCERR_READ) ? 1 : 0);
  82. }
  83. }
  84. if (saw_error != 0) {
  85. u64 tagval = stc_tag_buf[i];
  86. u64 lineval = stc_line_buf[i];
  87. printk(KERN_ERR "%s: STC_TAG(%d)[PA(%016llx)VA(%08llx)"
  88. "V(%d)W(%d)]\n",
  89. pbm->name,
  90. i,
  91. ((tagval & PSYCHO_STCTAG_PPN) >> 19UL),
  92. (tagval & PSYCHO_STCTAG_VPN),
  93. ((tagval & PSYCHO_STCTAG_VALID) ? 1 : 0),
  94. ((tagval & PSYCHO_STCTAG_WRITE) ? 1 : 0));
  95. printk(KERN_ERR "%s: STC_LINE(%d)[LIDX(%llx)SP(%llx)"
  96. "LADDR(%llx)EP(%llx)V(%d)FOFN(%d)]\n",
  97. pbm->name,
  98. i,
  99. ((lineval & PSYCHO_STCLINE_LINDX) >> 21UL),
  100. ((lineval & PSYCHO_STCLINE_SPTR) >> 15UL),
  101. ((lineval & PSYCHO_STCLINE_LADDR) >> 8UL),
  102. ((lineval & PSYCHO_STCLINE_EPTR) >> 2UL),
  103. ((lineval & PSYCHO_STCLINE_VALID) ? 1 : 0),
  104. ((lineval & PSYCHO_STCLINE_FOFN) ? 1 : 0));
  105. }
  106. }
  107. spin_unlock(&stc_buf_lock);
  108. }
  109. #define PSYCHO_IOMMU_TAG 0xa580UL
  110. #define PSYCHO_IOMMU_DATA 0xa600UL
  111. static void psycho_record_iommu_tags_and_data(struct pci_pbm_info *pbm,
  112. u64 *tag, u64 *data)
  113. {
  114. int i;
  115. for (i = 0; i < 16; i++) {
  116. unsigned long base = pbm->controller_regs;
  117. unsigned long off = i * 8UL;
  118. tag[i] = upa_readq(base + PSYCHO_IOMMU_TAG+off);
  119. data[i] = upa_readq(base + PSYCHO_IOMMU_DATA+off);
  120. /* Now clear out the entry. */
  121. upa_writeq(0, base + PSYCHO_IOMMU_TAG + off);
  122. upa_writeq(0, base + PSYCHO_IOMMU_DATA + off);
  123. }
  124. }
  125. #define PSYCHO_IOMMU_TAG_ERRSTS (0x3UL << 23UL)
  126. #define PSYCHO_IOMMU_TAG_ERR (0x1UL << 22UL)
  127. #define PSYCHO_IOMMU_TAG_WRITE (0x1UL << 21UL)
  128. #define PSYCHO_IOMMU_TAG_STREAM (0x1UL << 20UL)
  129. #define PSYCHO_IOMMU_TAG_SIZE (0x1UL << 19UL)
  130. #define PSYCHO_IOMMU_TAG_VPAGE 0x7ffffULL
  131. #define PSYCHO_IOMMU_DATA_VALID (1UL << 30UL)
  132. #define PSYCHO_IOMMU_DATA_CACHE (1UL << 28UL)
  133. #define PSYCHO_IOMMU_DATA_PPAGE 0xfffffffULL
  134. static void psycho_dump_iommu_tags_and_data(struct pci_pbm_info *pbm,
  135. u64 *tag, u64 *data)
  136. {
  137. int i;
  138. for (i = 0; i < 16; i++) {
  139. u64 tag_val, data_val;
  140. const char *type_str;
  141. tag_val = tag[i];
  142. if (!(tag_val & PSYCHO_IOMMU_TAG_ERR))
  143. continue;
  144. data_val = data[i];
  145. switch((tag_val & PSYCHO_IOMMU_TAG_ERRSTS) >> 23UL) {
  146. case 0:
  147. type_str = "Protection Error";
  148. break;
  149. case 1:
  150. type_str = "Invalid Error";
  151. break;
  152. case 2:
  153. type_str = "TimeOut Error";
  154. break;
  155. case 3:
  156. default:
  157. type_str = "ECC Error";
  158. break;
  159. }
  160. printk(KERN_ERR "%s: IOMMU TAG(%d)[error(%s) wr(%d) "
  161. "str(%d) sz(%dK) vpg(%08llx)]\n",
  162. pbm->name, i, type_str,
  163. ((tag_val & PSYCHO_IOMMU_TAG_WRITE) ? 1 : 0),
  164. ((tag_val & PSYCHO_IOMMU_TAG_STREAM) ? 1 : 0),
  165. ((tag_val & PSYCHO_IOMMU_TAG_SIZE) ? 64 : 8),
  166. (tag_val & PSYCHO_IOMMU_TAG_VPAGE) << IOMMU_PAGE_SHIFT);
  167. printk(KERN_ERR "%s: IOMMU DATA(%d)[valid(%d) cache(%d) "
  168. "ppg(%016llx)]\n",
  169. pbm->name, i,
  170. ((data_val & PSYCHO_IOMMU_DATA_VALID) ? 1 : 0),
  171. ((data_val & PSYCHO_IOMMU_DATA_CACHE) ? 1 : 0),
  172. (data_val & PSYCHO_IOMMU_DATA_PPAGE) << IOMMU_PAGE_SHIFT);
  173. }
  174. }
  175. #define PSYCHO_IOMMU_CTRL_XLTESTAT 0x0000000006000000UL
  176. #define PSYCHO_IOMMU_CTRL_XLTEERR 0x0000000001000000UL
  177. void psycho_check_iommu_error(struct pci_pbm_info *pbm,
  178. unsigned long afsr,
  179. unsigned long afar,
  180. enum psycho_error_type type)
  181. {
  182. u64 control, iommu_tag[16], iommu_data[16];
  183. struct iommu *iommu = pbm->iommu;
  184. unsigned long flags;
  185. spin_lock_irqsave(&iommu->lock, flags);
  186. control = upa_readq(iommu->iommu_control);
  187. if (control & PSYCHO_IOMMU_CTRL_XLTEERR) {
  188. const char *type_str;
  189. control &= ~PSYCHO_IOMMU_CTRL_XLTEERR;
  190. upa_writeq(control, iommu->iommu_control);
  191. switch ((control & PSYCHO_IOMMU_CTRL_XLTESTAT) >> 25UL) {
  192. case 0:
  193. type_str = "Protection Error";
  194. break;
  195. case 1:
  196. type_str = "Invalid Error";
  197. break;
  198. case 2:
  199. type_str = "TimeOut Error";
  200. break;
  201. case 3:
  202. default:
  203. type_str = "ECC Error";
  204. break;
  205. }
  206. printk(KERN_ERR "%s: IOMMU Error, type[%s]\n",
  207. pbm->name, type_str);
  208. /* It is very possible for another DVMA to occur while
  209. * we do this probe, and corrupt the system further.
  210. * But we are so screwed at this point that we are
  211. * likely to crash hard anyways, so get as much
  212. * diagnostic information to the console as we can.
  213. */
  214. psycho_record_iommu_tags_and_data(pbm, iommu_tag, iommu_data);
  215. psycho_dump_iommu_tags_and_data(pbm, iommu_tag, iommu_data);
  216. }
  217. psycho_check_stc_error(pbm);
  218. spin_unlock_irqrestore(&iommu->lock, flags);
  219. }
  220. #define PSYCHO_PCICTRL_SBH_ERR 0x0000000800000000UL
  221. #define PSYCHO_PCICTRL_SERR 0x0000000400000000UL
  222. static irqreturn_t psycho_pcierr_intr_other(struct pci_pbm_info *pbm)
  223. {
  224. irqreturn_t ret = IRQ_NONE;
  225. u64 csr, csr_error_bits;
  226. u16 stat, *addr;
  227. csr = upa_readq(pbm->pci_csr);
  228. csr_error_bits = csr & (PSYCHO_PCICTRL_SBH_ERR | PSYCHO_PCICTRL_SERR);
  229. if (csr_error_bits) {
  230. /* Clear the errors. */
  231. upa_writeq(csr, pbm->pci_csr);
  232. /* Log 'em. */
  233. if (csr_error_bits & PSYCHO_PCICTRL_SBH_ERR)
  234. printk(KERN_ERR "%s: PCI streaming byte hole "
  235. "error asserted.\n", pbm->name);
  236. if (csr_error_bits & PSYCHO_PCICTRL_SERR)
  237. printk(KERN_ERR "%s: PCI SERR signal asserted.\n",
  238. pbm->name);
  239. ret = IRQ_HANDLED;
  240. }
  241. addr = psycho_pci_config_mkaddr(pbm, pbm->pci_first_busno,
  242. 0, PCI_STATUS);
  243. pci_config_read16(addr, &stat);
  244. if (stat & (PCI_STATUS_PARITY |
  245. PCI_STATUS_SIG_TARGET_ABORT |
  246. PCI_STATUS_REC_TARGET_ABORT |
  247. PCI_STATUS_REC_MASTER_ABORT |
  248. PCI_STATUS_SIG_SYSTEM_ERROR)) {
  249. printk(KERN_ERR "%s: PCI bus error, PCI_STATUS[%04x]\n",
  250. pbm->name, stat);
  251. pci_config_write16(addr, 0xffff);
  252. ret = IRQ_HANDLED;
  253. }
  254. return ret;
  255. }
  256. #define PSYCHO_PCIAFSR_PMA 0x8000000000000000ULL
  257. #define PSYCHO_PCIAFSR_PTA 0x4000000000000000ULL
  258. #define PSYCHO_PCIAFSR_PRTRY 0x2000000000000000ULL
  259. #define PSYCHO_PCIAFSR_PPERR 0x1000000000000000ULL
  260. #define PSYCHO_PCIAFSR_SMA 0x0800000000000000ULL
  261. #define PSYCHO_PCIAFSR_STA 0x0400000000000000ULL
  262. #define PSYCHO_PCIAFSR_SRTRY 0x0200000000000000ULL
  263. #define PSYCHO_PCIAFSR_SPERR 0x0100000000000000ULL
  264. #define PSYCHO_PCIAFSR_RESV1 0x00ff000000000000ULL
  265. #define PSYCHO_PCIAFSR_BMSK 0x0000ffff00000000ULL
  266. #define PSYCHO_PCIAFSR_BLK 0x0000000080000000ULL
  267. #define PSYCHO_PCIAFSR_RESV2 0x0000000040000000ULL
  268. #define PSYCHO_PCIAFSR_MID 0x000000003e000000ULL
  269. #define PSYCHO_PCIAFSR_RESV3 0x0000000001ffffffULL
  270. irqreturn_t psycho_pcierr_intr(int irq, void *dev_id)
  271. {
  272. struct pci_pbm_info *pbm = dev_id;
  273. u64 afsr, afar, error_bits;
  274. int reported;
  275. afsr = upa_readq(pbm->pci_afsr);
  276. afar = upa_readq(pbm->pci_afar);
  277. error_bits = afsr &
  278. (PSYCHO_PCIAFSR_PMA | PSYCHO_PCIAFSR_PTA |
  279. PSYCHO_PCIAFSR_PRTRY | PSYCHO_PCIAFSR_PPERR |
  280. PSYCHO_PCIAFSR_SMA | PSYCHO_PCIAFSR_STA |
  281. PSYCHO_PCIAFSR_SRTRY | PSYCHO_PCIAFSR_SPERR);
  282. if (!error_bits)
  283. return psycho_pcierr_intr_other(pbm);
  284. upa_writeq(error_bits, pbm->pci_afsr);
  285. printk(KERN_ERR "%s: PCI Error, primary error type[%s]\n",
  286. pbm->name,
  287. (((error_bits & PSYCHO_PCIAFSR_PMA) ?
  288. "Master Abort" :
  289. ((error_bits & PSYCHO_PCIAFSR_PTA) ?
  290. "Target Abort" :
  291. ((error_bits & PSYCHO_PCIAFSR_PRTRY) ?
  292. "Excessive Retries" :
  293. ((error_bits & PSYCHO_PCIAFSR_PPERR) ?
  294. "Parity Error" : "???"))))));
  295. printk(KERN_ERR "%s: bytemask[%04llx] UPA_MID[%02llx] was_block(%d)\n",
  296. pbm->name,
  297. (afsr & PSYCHO_PCIAFSR_BMSK) >> 32UL,
  298. (afsr & PSYCHO_PCIAFSR_MID) >> 25UL,
  299. (afsr & PSYCHO_PCIAFSR_BLK) ? 1 : 0);
  300. printk(KERN_ERR "%s: PCI AFAR [%016llx]\n", pbm->name, afar);
  301. printk(KERN_ERR "%s: PCI Secondary errors [", pbm->name);
  302. reported = 0;
  303. if (afsr & PSYCHO_PCIAFSR_SMA) {
  304. reported++;
  305. printk("(Master Abort)");
  306. }
  307. if (afsr & PSYCHO_PCIAFSR_STA) {
  308. reported++;
  309. printk("(Target Abort)");
  310. }
  311. if (afsr & PSYCHO_PCIAFSR_SRTRY) {
  312. reported++;
  313. printk("(Excessive Retries)");
  314. }
  315. if (afsr & PSYCHO_PCIAFSR_SPERR) {
  316. reported++;
  317. printk("(Parity Error)");
  318. }
  319. if (!reported)
  320. printk("(none)");
  321. printk("]\n");
  322. if (error_bits & (PSYCHO_PCIAFSR_PTA | PSYCHO_PCIAFSR_STA)) {
  323. psycho_check_iommu_error(pbm, afsr, afar, PCI_ERR);
  324. pci_scan_for_target_abort(pbm, pbm->pci_bus);
  325. }
  326. if (error_bits & (PSYCHO_PCIAFSR_PMA | PSYCHO_PCIAFSR_SMA))
  327. pci_scan_for_master_abort(pbm, pbm->pci_bus);
  328. if (error_bits & (PSYCHO_PCIAFSR_PPERR | PSYCHO_PCIAFSR_SPERR))
  329. pci_scan_for_parity_error(pbm, pbm->pci_bus);
  330. return IRQ_HANDLED;
  331. }
  332. static void psycho_iommu_flush(struct pci_pbm_info *pbm)
  333. {
  334. int i;
  335. for (i = 0; i < 16; i++) {
  336. unsigned long off = i * 8;
  337. upa_writeq(0, pbm->controller_regs + PSYCHO_IOMMU_TAG + off);
  338. upa_writeq(0, pbm->controller_regs + PSYCHO_IOMMU_DATA + off);
  339. }
  340. }
  341. #define PSYCHO_IOMMU_CONTROL 0x0200UL
  342. #define PSYCHO_IOMMU_CTRL_TSBSZ 0x0000000000070000UL
  343. #define PSYCHO_IOMMU_TSBSZ_1K 0x0000000000000000UL
  344. #define PSYCHO_IOMMU_TSBSZ_2K 0x0000000000010000UL
  345. #define PSYCHO_IOMMU_TSBSZ_4K 0x0000000000020000UL
  346. #define PSYCHO_IOMMU_TSBSZ_8K 0x0000000000030000UL
  347. #define PSYCHO_IOMMU_TSBSZ_16K 0x0000000000040000UL
  348. #define PSYCHO_IOMMU_TSBSZ_32K 0x0000000000050000UL
  349. #define PSYCHO_IOMMU_TSBSZ_64K 0x0000000000060000UL
  350. #define PSYCHO_IOMMU_TSBSZ_128K 0x0000000000070000UL
  351. #define PSYCHO_IOMMU_CTRL_TBWSZ 0x0000000000000004UL
  352. #define PSYCHO_IOMMU_CTRL_DENAB 0x0000000000000002UL
  353. #define PSYCHO_IOMMU_CTRL_ENAB 0x0000000000000001UL
  354. #define PSYCHO_IOMMU_FLUSH 0x0210UL
  355. #define PSYCHO_IOMMU_TSBBASE 0x0208UL
  356. int psycho_iommu_init(struct pci_pbm_info *pbm, int tsbsize,
  357. u32 dvma_offset, u32 dma_mask,
  358. unsigned long write_complete_offset)
  359. {
  360. struct iommu *iommu = pbm->iommu;
  361. u64 control;
  362. int err;
  363. iommu->iommu_control = pbm->controller_regs + PSYCHO_IOMMU_CONTROL;
  364. iommu->iommu_tsbbase = pbm->controller_regs + PSYCHO_IOMMU_TSBBASE;
  365. iommu->iommu_flush = pbm->controller_regs + PSYCHO_IOMMU_FLUSH;
  366. iommu->iommu_tags = pbm->controller_regs + PSYCHO_IOMMU_TAG;
  367. iommu->write_complete_reg = (pbm->controller_regs +
  368. write_complete_offset);
  369. iommu->iommu_ctxflush = 0;
  370. control = upa_readq(iommu->iommu_control);
  371. control |= PSYCHO_IOMMU_CTRL_DENAB;
  372. upa_writeq(control, iommu->iommu_control);
  373. psycho_iommu_flush(pbm);
  374. /* Leave diag mode enabled for full-flushing done in pci_iommu.c */
  375. err = iommu_table_init(iommu, tsbsize * 1024 * 8,
  376. dvma_offset, dma_mask, pbm->numa_node);
  377. if (err)
  378. return err;
  379. upa_writeq(__pa(iommu->page_table), iommu->iommu_tsbbase);
  380. control = upa_readq(iommu->iommu_control);
  381. control &= ~(PSYCHO_IOMMU_CTRL_TSBSZ | PSYCHO_IOMMU_CTRL_TBWSZ);
  382. control |= PSYCHO_IOMMU_CTRL_ENAB;
  383. switch (tsbsize) {
  384. case 64:
  385. control |= PSYCHO_IOMMU_TSBSZ_64K;
  386. break;
  387. case 128:
  388. control |= PSYCHO_IOMMU_TSBSZ_128K;
  389. break;
  390. default:
  391. return -EINVAL;
  392. }
  393. upa_writeq(control, iommu->iommu_control);
  394. return 0;
  395. }
  396. void psycho_pbm_init_common(struct pci_pbm_info *pbm, struct platform_device *op,
  397. const char *chip_name, int chip_type)
  398. {
  399. struct device_node *dp = op->dev.of_node;
  400. pbm->name = dp->full_name;
  401. pbm->numa_node = NUMA_NO_NODE;
  402. pbm->chip_type = chip_type;
  403. pbm->chip_version = of_getintprop_default(dp, "version#", 0);
  404. pbm->chip_revision = of_getintprop_default(dp, "module-revision#", 0);
  405. pbm->op = op;
  406. pbm->pci_ops = &sun4u_pci_ops;
  407. pbm->config_space_reg_bits = 8;
  408. pbm->index = pci_num_pbms++;
  409. pci_get_pbm_props(pbm);
  410. pci_determine_mem_io_space(pbm);
  411. printk(KERN_INFO "%s: %s PCI Bus Module ver[%x:%x]\n",
  412. pbm->name, chip_name,
  413. pbm->chip_version, pbm->chip_revision);
  414. }