psycho_common.c 14 KB

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