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