psycho_common.c 14 KB

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