axon_msi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. * Copyright 2007, Michael Ellerman, IBM Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include <linux/interrupt.h>
  10. #include <linux/irq.h>
  11. #include <linux/kernel.h>
  12. #include <linux/pci.h>
  13. #include <linux/msi.h>
  14. #include <linux/export.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/debugfs.h>
  17. #include <linux/slab.h>
  18. #include <asm/dcr.h>
  19. #include <asm/machdep.h>
  20. #include <asm/prom.h>
  21. /*
  22. * MSIC registers, specified as offsets from dcr_base
  23. */
  24. #define MSIC_CTRL_REG 0x0
  25. /* Base Address registers specify FIFO location in BE memory */
  26. #define MSIC_BASE_ADDR_HI_REG 0x3
  27. #define MSIC_BASE_ADDR_LO_REG 0x4
  28. /* Hold the read/write offsets into the FIFO */
  29. #define MSIC_READ_OFFSET_REG 0x5
  30. #define MSIC_WRITE_OFFSET_REG 0x6
  31. /* MSIC control register flags */
  32. #define MSIC_CTRL_ENABLE 0x0001
  33. #define MSIC_CTRL_FIFO_FULL_ENABLE 0x0002
  34. #define MSIC_CTRL_IRQ_ENABLE 0x0008
  35. #define MSIC_CTRL_FULL_STOP_ENABLE 0x0010
  36. /*
  37. * The MSIC can be configured to use a FIFO of 32KB, 64KB, 128KB or 256KB.
  38. * Currently we're using a 64KB FIFO size.
  39. */
  40. #define MSIC_FIFO_SIZE_SHIFT 16
  41. #define MSIC_FIFO_SIZE_BYTES (1 << MSIC_FIFO_SIZE_SHIFT)
  42. /*
  43. * To configure the FIFO size as (1 << n) bytes, we write (n - 15) into bits
  44. * 8-9 of the MSIC control reg.
  45. */
  46. #define MSIC_CTRL_FIFO_SIZE (((MSIC_FIFO_SIZE_SHIFT - 15) << 8) & 0x300)
  47. /*
  48. * We need to mask the read/write offsets to make sure they stay within
  49. * the bounds of the FIFO. Also they should always be 16-byte aligned.
  50. */
  51. #define MSIC_FIFO_SIZE_MASK ((MSIC_FIFO_SIZE_BYTES - 1) & ~0xFu)
  52. /* Each entry in the FIFO is 16 bytes, the first 4 bytes hold the irq # */
  53. #define MSIC_FIFO_ENTRY_SIZE 0x10
  54. struct axon_msic {
  55. struct irq_domain *irq_domain;
  56. __le32 *fifo_virt;
  57. dma_addr_t fifo_phys;
  58. dcr_host_t dcr_host;
  59. u32 read_offset;
  60. #ifdef DEBUG
  61. u32 __iomem *trigger;
  62. #endif
  63. };
  64. #ifdef DEBUG
  65. void axon_msi_debug_setup(struct device_node *dn, struct axon_msic *msic);
  66. #else
  67. static inline void axon_msi_debug_setup(struct device_node *dn,
  68. struct axon_msic *msic) { }
  69. #endif
  70. static void msic_dcr_write(struct axon_msic *msic, unsigned int dcr_n, u32 val)
  71. {
  72. pr_devel("axon_msi: dcr_write(0x%x, 0x%x)\n", val, dcr_n);
  73. dcr_write(msic->dcr_host, dcr_n, val);
  74. }
  75. static void axon_msi_cascade(unsigned int irq, struct irq_desc *desc)
  76. {
  77. struct irq_chip *chip = irq_desc_get_chip(desc);
  78. struct axon_msic *msic = irq_get_handler_data(irq);
  79. u32 write_offset, msi;
  80. int idx;
  81. int retry = 0;
  82. write_offset = dcr_read(msic->dcr_host, MSIC_WRITE_OFFSET_REG);
  83. pr_devel("axon_msi: original write_offset 0x%x\n", write_offset);
  84. /* write_offset doesn't wrap properly, so we have to mask it */
  85. write_offset &= MSIC_FIFO_SIZE_MASK;
  86. while (msic->read_offset != write_offset && retry < 100) {
  87. idx = msic->read_offset / sizeof(__le32);
  88. msi = le32_to_cpu(msic->fifo_virt[idx]);
  89. msi &= 0xFFFF;
  90. pr_devel("axon_msi: woff %x roff %x msi %x\n",
  91. write_offset, msic->read_offset, msi);
  92. if (msi < NR_IRQS && irq_get_chip_data(msi) == msic) {
  93. generic_handle_irq(msi);
  94. msic->fifo_virt[idx] = cpu_to_le32(0xffffffff);
  95. } else {
  96. /*
  97. * Reading the MSIC_WRITE_OFFSET_REG does not
  98. * reliably flush the outstanding DMA to the
  99. * FIFO buffer. Here we were reading stale
  100. * data, so we need to retry.
  101. */
  102. udelay(1);
  103. retry++;
  104. pr_devel("axon_msi: invalid irq 0x%x!\n", msi);
  105. continue;
  106. }
  107. if (retry) {
  108. pr_devel("axon_msi: late irq 0x%x, retry %d\n",
  109. msi, retry);
  110. retry = 0;
  111. }
  112. msic->read_offset += MSIC_FIFO_ENTRY_SIZE;
  113. msic->read_offset &= MSIC_FIFO_SIZE_MASK;
  114. }
  115. if (retry) {
  116. printk(KERN_WARNING "axon_msi: irq timed out\n");
  117. msic->read_offset += MSIC_FIFO_ENTRY_SIZE;
  118. msic->read_offset &= MSIC_FIFO_SIZE_MASK;
  119. }
  120. chip->irq_eoi(&desc->irq_data);
  121. }
  122. static struct axon_msic *find_msi_translator(struct pci_dev *dev)
  123. {
  124. struct irq_domain *irq_domain;
  125. struct device_node *dn, *tmp;
  126. const phandle *ph;
  127. struct axon_msic *msic = NULL;
  128. dn = of_node_get(pci_device_to_OF_node(dev));
  129. if (!dn) {
  130. dev_dbg(&dev->dev, "axon_msi: no pci_dn found\n");
  131. return NULL;
  132. }
  133. for (; dn; dn = of_get_next_parent(dn)) {
  134. ph = of_get_property(dn, "msi-translator", NULL);
  135. if (ph)
  136. break;
  137. }
  138. if (!ph) {
  139. dev_dbg(&dev->dev,
  140. "axon_msi: no msi-translator property found\n");
  141. goto out_error;
  142. }
  143. tmp = dn;
  144. dn = of_find_node_by_phandle(*ph);
  145. of_node_put(tmp);
  146. if (!dn) {
  147. dev_dbg(&dev->dev,
  148. "axon_msi: msi-translator doesn't point to a node\n");
  149. goto out_error;
  150. }
  151. irq_domain = irq_find_host(dn);
  152. if (!irq_domain) {
  153. dev_dbg(&dev->dev, "axon_msi: no irq_domain found for node %s\n",
  154. dn->full_name);
  155. goto out_error;
  156. }
  157. msic = irq_domain->host_data;
  158. out_error:
  159. of_node_put(dn);
  160. return msic;
  161. }
  162. static int axon_msi_check_device(struct pci_dev *dev, int nvec, int type)
  163. {
  164. if (!find_msi_translator(dev))
  165. return -ENODEV;
  166. return 0;
  167. }
  168. static int setup_msi_msg_address(struct pci_dev *dev, struct msi_msg *msg)
  169. {
  170. struct device_node *dn;
  171. struct msi_desc *entry;
  172. int len;
  173. const u32 *prop;
  174. dn = of_node_get(pci_device_to_OF_node(dev));
  175. if (!dn) {
  176. dev_dbg(&dev->dev, "axon_msi: no pci_dn found\n");
  177. return -ENODEV;
  178. }
  179. entry = list_first_entry(&dev->msi_list, struct msi_desc, list);
  180. for (; dn; dn = of_get_next_parent(dn)) {
  181. if (entry->msi_attrib.is_64) {
  182. prop = of_get_property(dn, "msi-address-64", &len);
  183. if (prop)
  184. break;
  185. }
  186. prop = of_get_property(dn, "msi-address-32", &len);
  187. if (prop)
  188. break;
  189. }
  190. if (!prop) {
  191. dev_dbg(&dev->dev,
  192. "axon_msi: no msi-address-(32|64) properties found\n");
  193. return -ENOENT;
  194. }
  195. switch (len) {
  196. case 8:
  197. msg->address_hi = prop[0];
  198. msg->address_lo = prop[1];
  199. break;
  200. case 4:
  201. msg->address_hi = 0;
  202. msg->address_lo = prop[0];
  203. break;
  204. default:
  205. dev_dbg(&dev->dev,
  206. "axon_msi: malformed msi-address-(32|64) property\n");
  207. of_node_put(dn);
  208. return -EINVAL;
  209. }
  210. of_node_put(dn);
  211. return 0;
  212. }
  213. static int axon_msi_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  214. {
  215. unsigned int virq, rc;
  216. struct msi_desc *entry;
  217. struct msi_msg msg;
  218. struct axon_msic *msic;
  219. msic = find_msi_translator(dev);
  220. if (!msic)
  221. return -ENODEV;
  222. rc = setup_msi_msg_address(dev, &msg);
  223. if (rc)
  224. return rc;
  225. /* We rely on being able to stash a virq in a u16 */
  226. BUILD_BUG_ON(NR_IRQS > 65536);
  227. list_for_each_entry(entry, &dev->msi_list, list) {
  228. virq = irq_create_direct_mapping(msic->irq_domain);
  229. if (virq == NO_IRQ) {
  230. dev_warn(&dev->dev,
  231. "axon_msi: virq allocation failed!\n");
  232. return -1;
  233. }
  234. dev_dbg(&dev->dev, "axon_msi: allocated virq 0x%x\n", virq);
  235. irq_set_msi_desc(virq, entry);
  236. msg.data = virq;
  237. write_msi_msg(virq, &msg);
  238. }
  239. return 0;
  240. }
  241. static void axon_msi_teardown_msi_irqs(struct pci_dev *dev)
  242. {
  243. struct msi_desc *entry;
  244. dev_dbg(&dev->dev, "axon_msi: tearing down msi irqs\n");
  245. list_for_each_entry(entry, &dev->msi_list, list) {
  246. if (entry->irq == NO_IRQ)
  247. continue;
  248. irq_set_msi_desc(entry->irq, NULL);
  249. irq_dispose_mapping(entry->irq);
  250. }
  251. }
  252. static struct irq_chip msic_irq_chip = {
  253. .irq_mask = mask_msi_irq,
  254. .irq_unmask = unmask_msi_irq,
  255. .irq_shutdown = mask_msi_irq,
  256. .name = "AXON-MSI",
  257. };
  258. static int msic_host_map(struct irq_domain *h, unsigned int virq,
  259. irq_hw_number_t hw)
  260. {
  261. irq_set_chip_data(virq, h->host_data);
  262. irq_set_chip_and_handler(virq, &msic_irq_chip, handle_simple_irq);
  263. return 0;
  264. }
  265. static const struct irq_domain_ops msic_host_ops = {
  266. .map = msic_host_map,
  267. };
  268. static void axon_msi_shutdown(struct platform_device *device)
  269. {
  270. struct axon_msic *msic = dev_get_drvdata(&device->dev);
  271. u32 tmp;
  272. pr_devel("axon_msi: disabling %s\n",
  273. msic->irq_domain->of_node->full_name);
  274. tmp = dcr_read(msic->dcr_host, MSIC_CTRL_REG);
  275. tmp &= ~MSIC_CTRL_ENABLE & ~MSIC_CTRL_IRQ_ENABLE;
  276. msic_dcr_write(msic, MSIC_CTRL_REG, tmp);
  277. }
  278. static int axon_msi_probe(struct platform_device *device)
  279. {
  280. struct device_node *dn = device->dev.of_node;
  281. struct axon_msic *msic;
  282. unsigned int virq;
  283. int dcr_base, dcr_len;
  284. pr_devel("axon_msi: setting up dn %s\n", dn->full_name);
  285. msic = kzalloc(sizeof(struct axon_msic), GFP_KERNEL);
  286. if (!msic) {
  287. printk(KERN_ERR "axon_msi: couldn't allocate msic for %s\n",
  288. dn->full_name);
  289. goto out;
  290. }
  291. dcr_base = dcr_resource_start(dn, 0);
  292. dcr_len = dcr_resource_len(dn, 0);
  293. if (dcr_base == 0 || dcr_len == 0) {
  294. printk(KERN_ERR
  295. "axon_msi: couldn't parse dcr properties on %s\n",
  296. dn->full_name);
  297. goto out_free_msic;
  298. }
  299. msic->dcr_host = dcr_map(dn, dcr_base, dcr_len);
  300. if (!DCR_MAP_OK(msic->dcr_host)) {
  301. printk(KERN_ERR "axon_msi: dcr_map failed for %s\n",
  302. dn->full_name);
  303. goto out_free_msic;
  304. }
  305. msic->fifo_virt = dma_alloc_coherent(&device->dev, MSIC_FIFO_SIZE_BYTES,
  306. &msic->fifo_phys, GFP_KERNEL);
  307. if (!msic->fifo_virt) {
  308. printk(KERN_ERR "axon_msi: couldn't allocate fifo for %s\n",
  309. dn->full_name);
  310. goto out_free_msic;
  311. }
  312. virq = irq_of_parse_and_map(dn, 0);
  313. if (virq == NO_IRQ) {
  314. printk(KERN_ERR "axon_msi: irq parse and map failed for %s\n",
  315. dn->full_name);
  316. goto out_free_fifo;
  317. }
  318. memset(msic->fifo_virt, 0xff, MSIC_FIFO_SIZE_BYTES);
  319. msic->irq_domain = irq_domain_add_nomap(dn, &msic_host_ops, msic);
  320. if (!msic->irq_domain) {
  321. printk(KERN_ERR "axon_msi: couldn't allocate irq_domain for %s\n",
  322. dn->full_name);
  323. goto out_free_fifo;
  324. }
  325. irq_set_handler_data(virq, msic);
  326. irq_set_chained_handler(virq, axon_msi_cascade);
  327. pr_devel("axon_msi: irq 0x%x setup for axon_msi\n", virq);
  328. /* Enable the MSIC hardware */
  329. msic_dcr_write(msic, MSIC_BASE_ADDR_HI_REG, msic->fifo_phys >> 32);
  330. msic_dcr_write(msic, MSIC_BASE_ADDR_LO_REG,
  331. msic->fifo_phys & 0xFFFFFFFF);
  332. msic_dcr_write(msic, MSIC_CTRL_REG,
  333. MSIC_CTRL_IRQ_ENABLE | MSIC_CTRL_ENABLE |
  334. MSIC_CTRL_FIFO_SIZE);
  335. msic->read_offset = dcr_read(msic->dcr_host, MSIC_WRITE_OFFSET_REG)
  336. & MSIC_FIFO_SIZE_MASK;
  337. dev_set_drvdata(&device->dev, msic);
  338. ppc_md.setup_msi_irqs = axon_msi_setup_msi_irqs;
  339. ppc_md.teardown_msi_irqs = axon_msi_teardown_msi_irqs;
  340. ppc_md.msi_check_device = axon_msi_check_device;
  341. axon_msi_debug_setup(dn, msic);
  342. printk(KERN_DEBUG "axon_msi: setup MSIC on %s\n", dn->full_name);
  343. return 0;
  344. out_free_fifo:
  345. dma_free_coherent(&device->dev, MSIC_FIFO_SIZE_BYTES, msic->fifo_virt,
  346. msic->fifo_phys);
  347. out_free_msic:
  348. kfree(msic);
  349. out:
  350. return -1;
  351. }
  352. static const struct of_device_id axon_msi_device_id[] = {
  353. {
  354. .compatible = "ibm,axon-msic"
  355. },
  356. {}
  357. };
  358. static struct platform_driver axon_msi_driver = {
  359. .probe = axon_msi_probe,
  360. .shutdown = axon_msi_shutdown,
  361. .driver = {
  362. .name = "axon-msi",
  363. .owner = THIS_MODULE,
  364. .of_match_table = axon_msi_device_id,
  365. },
  366. };
  367. static int __init axon_msi_init(void)
  368. {
  369. return platform_driver_register(&axon_msi_driver);
  370. }
  371. subsys_initcall(axon_msi_init);
  372. #ifdef DEBUG
  373. static int msic_set(void *data, u64 val)
  374. {
  375. struct axon_msic *msic = data;
  376. out_le32(msic->trigger, val);
  377. return 0;
  378. }
  379. static int msic_get(void *data, u64 *val)
  380. {
  381. *val = 0;
  382. return 0;
  383. }
  384. DEFINE_SIMPLE_ATTRIBUTE(fops_msic, msic_get, msic_set, "%llu\n");
  385. void axon_msi_debug_setup(struct device_node *dn, struct axon_msic *msic)
  386. {
  387. char name[8];
  388. u64 addr;
  389. addr = of_translate_address(dn, of_get_property(dn, "reg", NULL));
  390. if (addr == OF_BAD_ADDR) {
  391. pr_devel("axon_msi: couldn't translate reg property\n");
  392. return;
  393. }
  394. msic->trigger = ioremap(addr, 0x4);
  395. if (!msic->trigger) {
  396. pr_devel("axon_msi: ioremap failed\n");
  397. return;
  398. }
  399. snprintf(name, sizeof(name), "msic_%d", of_node_to_nid(dn));
  400. if (!debugfs_create_file(name, 0600, powerpc_debugfs_root,
  401. msic, &fops_msic)) {
  402. pr_devel("axon_msi: debugfs_create_file failed!\n");
  403. return;
  404. }
  405. }
  406. #endif /* DEBUG */