axon_msi.c 12 KB

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