axon_msi.c 11 KB

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