axon_msi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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/reboot.h>
  15. #include <asm/dcr.h>
  16. #include <asm/machdep.h>
  17. #include <asm/prom.h>
  18. /*
  19. * MSIC registers, specified as offsets from dcr_base
  20. */
  21. #define MSIC_CTRL_REG 0x0
  22. /* Base Address registers specify FIFO location in BE memory */
  23. #define MSIC_BASE_ADDR_HI_REG 0x3
  24. #define MSIC_BASE_ADDR_LO_REG 0x4
  25. /* Hold the read/write offsets into the FIFO */
  26. #define MSIC_READ_OFFSET_REG 0x5
  27. #define MSIC_WRITE_OFFSET_REG 0x6
  28. /* MSIC control register flags */
  29. #define MSIC_CTRL_ENABLE 0x0001
  30. #define MSIC_CTRL_FIFO_FULL_ENABLE 0x0002
  31. #define MSIC_CTRL_IRQ_ENABLE 0x0008
  32. #define MSIC_CTRL_FULL_STOP_ENABLE 0x0010
  33. /*
  34. * The MSIC can be configured to use a FIFO of 32KB, 64KB, 128KB or 256KB.
  35. * Currently we're using a 64KB FIFO size.
  36. */
  37. #define MSIC_FIFO_SIZE_SHIFT 16
  38. #define MSIC_FIFO_SIZE_BYTES (1 << MSIC_FIFO_SIZE_SHIFT)
  39. /*
  40. * To configure the FIFO size as (1 << n) bytes, we write (n - 15) into bits
  41. * 8-9 of the MSIC control reg.
  42. */
  43. #define MSIC_CTRL_FIFO_SIZE (((MSIC_FIFO_SIZE_SHIFT - 15) << 8) & 0x300)
  44. /*
  45. * We need to mask the read/write offsets to make sure they stay within
  46. * the bounds of the FIFO. Also they should always be 16-byte aligned.
  47. */
  48. #define MSIC_FIFO_SIZE_MASK ((MSIC_FIFO_SIZE_BYTES - 1) & ~0xFu)
  49. /* Each entry in the FIFO is 16 bytes, the first 4 bytes hold the irq # */
  50. #define MSIC_FIFO_ENTRY_SIZE 0x10
  51. struct axon_msic {
  52. struct device_node *dn;
  53. struct irq_host *irq_host;
  54. __le32 *fifo;
  55. dcr_host_t dcr_host;
  56. struct list_head list;
  57. u32 read_offset;
  58. u32 dcr_base;
  59. };
  60. static LIST_HEAD(axon_msic_list);
  61. static void msic_dcr_write(struct axon_msic *msic, unsigned int dcr_n, u32 val)
  62. {
  63. pr_debug("axon_msi: dcr_write(0x%x, 0x%x)\n", val, dcr_n);
  64. dcr_write(msic->dcr_host, msic->dcr_base + dcr_n, val);
  65. }
  66. static u32 msic_dcr_read(struct axon_msic *msic, unsigned int dcr_n)
  67. {
  68. return dcr_read(msic->dcr_host, msic->dcr_base + dcr_n);
  69. }
  70. static void axon_msi_cascade(unsigned int irq, struct irq_desc *desc)
  71. {
  72. struct axon_msic *msic = get_irq_data(irq);
  73. u32 write_offset, msi;
  74. int idx;
  75. write_offset = msic_dcr_read(msic, MSIC_WRITE_OFFSET_REG);
  76. pr_debug("axon_msi: original write_offset 0x%x\n", write_offset);
  77. /* write_offset doesn't wrap properly, so we have to mask it */
  78. write_offset &= MSIC_FIFO_SIZE_MASK;
  79. while (msic->read_offset != write_offset) {
  80. idx = msic->read_offset / sizeof(__le32);
  81. msi = le32_to_cpu(msic->fifo[idx]);
  82. msi &= 0xFFFF;
  83. pr_debug("axon_msi: woff %x roff %x msi %x\n",
  84. write_offset, msic->read_offset, msi);
  85. msic->read_offset += MSIC_FIFO_ENTRY_SIZE;
  86. msic->read_offset &= MSIC_FIFO_SIZE_MASK;
  87. if (msi < NR_IRQS && irq_map[msi].host == msic->irq_host)
  88. generic_handle_irq(msi);
  89. else
  90. pr_debug("axon_msi: invalid irq 0x%x!\n", msi);
  91. }
  92. desc->chip->eoi(irq);
  93. }
  94. static struct axon_msic *find_msi_translator(struct pci_dev *dev)
  95. {
  96. struct irq_host *irq_host;
  97. struct device_node *dn, *tmp;
  98. const phandle *ph;
  99. struct axon_msic *msic = NULL;
  100. dn = pci_device_to_OF_node(dev);
  101. if (!dn) {
  102. dev_dbg(&dev->dev, "axon_msi: no pci_dn found\n");
  103. return NULL;
  104. }
  105. for (; dn; tmp = of_get_parent(dn), of_node_put(dn), dn = tmp) {
  106. ph = of_get_property(dn, "msi-translator", NULL);
  107. if (ph)
  108. break;
  109. }
  110. if (!ph) {
  111. dev_dbg(&dev->dev,
  112. "axon_msi: no msi-translator property found\n");
  113. goto out_error;
  114. }
  115. tmp = dn;
  116. dn = of_find_node_by_phandle(*ph);
  117. if (!dn) {
  118. dev_dbg(&dev->dev,
  119. "axon_msi: msi-translator doesn't point to a node\n");
  120. goto out_error;
  121. }
  122. irq_host = irq_find_host(dn);
  123. if (!irq_host) {
  124. dev_dbg(&dev->dev, "axon_msi: no irq_host found for node %s\n",
  125. dn->full_name);
  126. goto out_error;
  127. }
  128. msic = irq_host->host_data;
  129. out_error:
  130. of_node_put(dn);
  131. of_node_put(tmp);
  132. return msic;
  133. }
  134. static int axon_msi_check_device(struct pci_dev *dev, int nvec, int type)
  135. {
  136. if (!find_msi_translator(dev))
  137. return -ENODEV;
  138. return 0;
  139. }
  140. static int setup_msi_msg_address(struct pci_dev *dev, struct msi_msg *msg)
  141. {
  142. struct device_node *dn, *tmp;
  143. struct msi_desc *entry;
  144. int len;
  145. const u32 *prop;
  146. dn = pci_device_to_OF_node(dev);
  147. if (!dn) {
  148. dev_dbg(&dev->dev, "axon_msi: no pci_dn found\n");
  149. return -ENODEV;
  150. }
  151. entry = list_first_entry(&dev->msi_list, struct msi_desc, list);
  152. for (; dn; tmp = of_get_parent(dn), of_node_put(dn), dn = tmp) {
  153. if (entry->msi_attrib.is_64) {
  154. prop = of_get_property(dn, "msi-address-64", &len);
  155. if (prop)
  156. break;
  157. }
  158. prop = of_get_property(dn, "msi-address-32", &len);
  159. if (prop)
  160. break;
  161. }
  162. if (!prop) {
  163. dev_dbg(&dev->dev,
  164. "axon_msi: no msi-address-(32|64) properties found\n");
  165. return -ENOENT;
  166. }
  167. switch (len) {
  168. case 8:
  169. msg->address_hi = prop[0];
  170. msg->address_lo = prop[1];
  171. break;
  172. case 4:
  173. msg->address_hi = 0;
  174. msg->address_lo = prop[0];
  175. break;
  176. default:
  177. dev_dbg(&dev->dev,
  178. "axon_msi: malformed msi-address-(32|64) property\n");
  179. of_node_put(dn);
  180. return -EINVAL;
  181. }
  182. of_node_put(dn);
  183. return 0;
  184. }
  185. static int axon_msi_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  186. {
  187. unsigned int virq, rc;
  188. struct msi_desc *entry;
  189. struct msi_msg msg;
  190. struct axon_msic *msic;
  191. msic = find_msi_translator(dev);
  192. if (!msic)
  193. return -ENODEV;
  194. rc = setup_msi_msg_address(dev, &msg);
  195. if (rc)
  196. return rc;
  197. /* We rely on being able to stash a virq in a u16 */
  198. BUILD_BUG_ON(NR_IRQS > 65536);
  199. list_for_each_entry(entry, &dev->msi_list, list) {
  200. virq = irq_create_direct_mapping(msic->irq_host);
  201. if (virq == NO_IRQ) {
  202. dev_warn(&dev->dev,
  203. "axon_msi: virq allocation failed!\n");
  204. return -1;
  205. }
  206. dev_dbg(&dev->dev, "axon_msi: allocated virq 0x%x\n", virq);
  207. set_irq_msi(virq, entry);
  208. msg.data = virq;
  209. write_msi_msg(virq, &msg);
  210. }
  211. return 0;
  212. }
  213. static void axon_msi_teardown_msi_irqs(struct pci_dev *dev)
  214. {
  215. struct msi_desc *entry;
  216. dev_dbg(&dev->dev, "axon_msi: tearing down msi irqs\n");
  217. list_for_each_entry(entry, &dev->msi_list, list) {
  218. if (entry->irq == NO_IRQ)
  219. continue;
  220. set_irq_msi(entry->irq, NULL);
  221. irq_dispose_mapping(entry->irq);
  222. }
  223. }
  224. static struct irq_chip msic_irq_chip = {
  225. .mask = mask_msi_irq,
  226. .unmask = unmask_msi_irq,
  227. .shutdown = unmask_msi_irq,
  228. .typename = "AXON-MSI",
  229. };
  230. static int msic_host_map(struct irq_host *h, unsigned int virq,
  231. irq_hw_number_t hw)
  232. {
  233. set_irq_chip_and_handler(virq, &msic_irq_chip, handle_simple_irq);
  234. return 0;
  235. }
  236. static int msic_host_match(struct irq_host *host, struct device_node *dn)
  237. {
  238. struct axon_msic *msic = host->host_data;
  239. return msic->dn == dn;
  240. }
  241. static struct irq_host_ops msic_host_ops = {
  242. .match = msic_host_match,
  243. .map = msic_host_map,
  244. };
  245. static int axon_msi_notify_reboot(struct notifier_block *nb,
  246. unsigned long code, void *data)
  247. {
  248. struct axon_msic *msic;
  249. u32 tmp;
  250. list_for_each_entry(msic, &axon_msic_list, list) {
  251. pr_debug("axon_msi: disabling %s\n", msic->dn->full_name);
  252. tmp = msic_dcr_read(msic, MSIC_CTRL_REG);
  253. tmp &= ~MSIC_CTRL_ENABLE & ~MSIC_CTRL_IRQ_ENABLE;
  254. msic_dcr_write(msic, MSIC_CTRL_REG, tmp);
  255. }
  256. return 0;
  257. }
  258. static struct notifier_block axon_msi_reboot_notifier = {
  259. .notifier_call = axon_msi_notify_reboot
  260. };
  261. static int axon_msi_setup_one(struct device_node *dn)
  262. {
  263. struct page *page;
  264. struct axon_msic *msic;
  265. unsigned int virq;
  266. int dcr_len;
  267. pr_debug("axon_msi: setting up dn %s\n", dn->full_name);
  268. msic = kzalloc(sizeof(struct axon_msic), GFP_KERNEL);
  269. if (!msic) {
  270. printk(KERN_ERR "axon_msi: couldn't allocate msic for %s\n",
  271. dn->full_name);
  272. goto out;
  273. }
  274. msic->dcr_base = dcr_resource_start(dn, 0);
  275. dcr_len = dcr_resource_len(dn, 0);
  276. if (msic->dcr_base == 0 || dcr_len == 0) {
  277. printk(KERN_ERR
  278. "axon_msi: couldn't parse dcr properties on %s\n",
  279. dn->full_name);
  280. goto out;
  281. }
  282. msic->dcr_host = dcr_map(dn, msic->dcr_base, dcr_len);
  283. if (!DCR_MAP_OK(msic->dcr_host)) {
  284. printk(KERN_ERR "axon_msi: dcr_map failed for %s\n",
  285. dn->full_name);
  286. goto out_free_msic;
  287. }
  288. page = alloc_pages_node(of_node_to_nid(dn), GFP_KERNEL,
  289. get_order(MSIC_FIFO_SIZE_BYTES));
  290. if (!page) {
  291. printk(KERN_ERR "axon_msi: couldn't allocate fifo for %s\n",
  292. dn->full_name);
  293. goto out_free_msic;
  294. }
  295. msic->fifo = page_address(page);
  296. msic->irq_host = irq_alloc_host(IRQ_HOST_MAP_NOMAP, NR_IRQS,
  297. &msic_host_ops, 0);
  298. if (!msic->irq_host) {
  299. printk(KERN_ERR "axon_msi: couldn't allocate irq_host for %s\n",
  300. dn->full_name);
  301. goto out_free_fifo;
  302. }
  303. msic->irq_host->host_data = msic;
  304. virq = irq_of_parse_and_map(dn, 0);
  305. if (virq == NO_IRQ) {
  306. printk(KERN_ERR "axon_msi: irq parse and map failed for %s\n",
  307. dn->full_name);
  308. goto out_free_host;
  309. }
  310. msic->dn = of_node_get(dn);
  311. set_irq_data(virq, msic);
  312. set_irq_chained_handler(virq, axon_msi_cascade);
  313. pr_debug("axon_msi: irq 0x%x setup for axon_msi\n", virq);
  314. /* Enable the MSIC hardware */
  315. msic_dcr_write(msic, MSIC_BASE_ADDR_HI_REG, (u64)msic->fifo >> 32);
  316. msic_dcr_write(msic, MSIC_BASE_ADDR_LO_REG,
  317. (u64)msic->fifo & 0xFFFFFFFF);
  318. msic_dcr_write(msic, MSIC_CTRL_REG,
  319. MSIC_CTRL_IRQ_ENABLE | MSIC_CTRL_ENABLE |
  320. MSIC_CTRL_FIFO_SIZE);
  321. list_add(&msic->list, &axon_msic_list);
  322. printk(KERN_DEBUG "axon_msi: setup MSIC on %s\n", dn->full_name);
  323. return 0;
  324. out_free_host:
  325. kfree(msic->irq_host);
  326. out_free_fifo:
  327. __free_pages(virt_to_page(msic->fifo), get_order(MSIC_FIFO_SIZE_BYTES));
  328. out_free_msic:
  329. kfree(msic);
  330. out:
  331. return -1;
  332. }
  333. static int axon_msi_init(void)
  334. {
  335. struct device_node *dn;
  336. int found = 0;
  337. pr_debug("axon_msi: initialising ...\n");
  338. for_each_compatible_node(dn, NULL, "ibm,axon-msic") {
  339. if (axon_msi_setup_one(dn) == 0)
  340. found++;
  341. }
  342. if (found) {
  343. ppc_md.setup_msi_irqs = axon_msi_setup_msi_irqs;
  344. ppc_md.teardown_msi_irqs = axon_msi_teardown_msi_irqs;
  345. ppc_md.msi_check_device = axon_msi_check_device;
  346. register_reboot_notifier(&axon_msi_reboot_notifier);
  347. pr_debug("axon_msi: registered callbacks!\n");
  348. }
  349. return 0;
  350. }
  351. arch_initcall(axon_msi_init);