2 * Copyright 2007, Michael Ellerman, IBM Corporation.
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.
11 #include <linux/interrupt.h>
12 #include <linux/irq.h>
13 #include <linux/kernel.h>
14 #include <linux/pci.h>
15 #include <linux/msi.h>
16 #include <linux/of_platform.h>
17 #include <linux/debugfs.h>
18 #include <linux/slab.h>
21 #include <asm/machdep.h>
26 * MSIC registers, specified as offsets from dcr_base
28 #define MSIC_CTRL_REG 0x0
30 /* Base Address registers specify FIFO location in BE memory */
31 #define MSIC_BASE_ADDR_HI_REG 0x3
32 #define MSIC_BASE_ADDR_LO_REG 0x4
34 /* Hold the read/write offsets into the FIFO */
35 #define MSIC_READ_OFFSET_REG 0x5
36 #define MSIC_WRITE_OFFSET_REG 0x6
39 /* MSIC control register flags */
40 #define MSIC_CTRL_ENABLE 0x0001
41 #define MSIC_CTRL_FIFO_FULL_ENABLE 0x0002
42 #define MSIC_CTRL_IRQ_ENABLE 0x0008
43 #define MSIC_CTRL_FULL_STOP_ENABLE 0x0010
46 * The MSIC can be configured to use a FIFO of 32KB, 64KB, 128KB or 256KB.
47 * Currently we're using a 64KB FIFO size.
49 #define MSIC_FIFO_SIZE_SHIFT 16
50 #define MSIC_FIFO_SIZE_BYTES (1 << MSIC_FIFO_SIZE_SHIFT)
53 * To configure the FIFO size as (1 << n) bytes, we write (n - 15) into bits
54 * 8-9 of the MSIC control reg.
56 #define MSIC_CTRL_FIFO_SIZE (((MSIC_FIFO_SIZE_SHIFT - 15) << 8) & 0x300)
59 * We need to mask the read/write offsets to make sure they stay within
60 * the bounds of the FIFO. Also they should always be 16-byte aligned.
62 #define MSIC_FIFO_SIZE_MASK ((MSIC_FIFO_SIZE_BYTES - 1) & ~0xFu)
64 /* Each entry in the FIFO is 16 bytes, the first 4 bytes hold the irq # */
65 #define MSIC_FIFO_ENTRY_SIZE 0x10
69 struct irq_host *irq_host;
80 void axon_msi_debug_setup(struct device_node *dn, struct axon_msic *msic);
82 static inline void axon_msi_debug_setup(struct device_node *dn,
83 struct axon_msic *msic) { }
87 static void msic_dcr_write(struct axon_msic *msic, unsigned int dcr_n, u32 val)
89 pr_devel("axon_msi: dcr_write(0x%x, 0x%x)\n", val, dcr_n);
91 dcr_write(msic->dcr_host, dcr_n, val);
94 static void axon_msi_cascade(unsigned int irq, struct irq_desc *desc)
96 struct axon_msic *msic = get_irq_data(irq);
97 u32 write_offset, msi;
101 write_offset = dcr_read(msic->dcr_host, MSIC_WRITE_OFFSET_REG);
102 pr_devel("axon_msi: original write_offset 0x%x\n", write_offset);
104 /* write_offset doesn't wrap properly, so we have to mask it */
105 write_offset &= MSIC_FIFO_SIZE_MASK;
107 while (msic->read_offset != write_offset && retry < 100) {
108 idx = msic->read_offset / sizeof(__le32);
109 msi = le32_to_cpu(msic->fifo_virt[idx]);
112 pr_devel("axon_msi: woff %x roff %x msi %x\n",
113 write_offset, msic->read_offset, msi);
115 if (msi < NR_IRQS && irq_map[msi].host == msic->irq_host) {
116 generic_handle_irq(msi);
117 msic->fifo_virt[idx] = cpu_to_le32(0xffffffff);
120 * Reading the MSIC_WRITE_OFFSET_REG does not
121 * reliably flush the outstanding DMA to the
122 * FIFO buffer. Here we were reading stale
123 * data, so we need to retry.
127 pr_devel("axon_msi: invalid irq 0x%x!\n", msi);
132 pr_devel("axon_msi: late irq 0x%x, retry %d\n",
137 msic->read_offset += MSIC_FIFO_ENTRY_SIZE;
138 msic->read_offset &= MSIC_FIFO_SIZE_MASK;
142 printk(KERN_WARNING "axon_msi: irq timed out\n");
144 msic->read_offset += MSIC_FIFO_ENTRY_SIZE;
145 msic->read_offset &= MSIC_FIFO_SIZE_MASK;
148 desc->chip->eoi(irq);
151 static struct axon_msic *find_msi_translator(struct pci_dev *dev)
153 struct irq_host *irq_host;
154 struct device_node *dn, *tmp;
156 struct axon_msic *msic = NULL;
158 dn = of_node_get(pci_device_to_OF_node(dev));
160 dev_dbg(&dev->dev, "axon_msi: no pci_dn found\n");
164 for (; dn; dn = of_get_next_parent(dn)) {
165 ph = of_get_property(dn, "msi-translator", NULL);
172 "axon_msi: no msi-translator property found\n");
177 dn = of_find_node_by_phandle(*ph);
181 "axon_msi: msi-translator doesn't point to a node\n");
185 irq_host = irq_find_host(dn);
187 dev_dbg(&dev->dev, "axon_msi: no irq_host found for node %s\n",
192 msic = irq_host->host_data;
200 static int axon_msi_check_device(struct pci_dev *dev, int nvec, int type)
202 if (!find_msi_translator(dev))
208 static int setup_msi_msg_address(struct pci_dev *dev, struct msi_msg *msg)
210 struct device_node *dn;
211 struct msi_desc *entry;
215 dn = of_node_get(pci_device_to_OF_node(dev));
217 dev_dbg(&dev->dev, "axon_msi: no pci_dn found\n");
221 entry = list_first_entry(&dev->msi_list, struct msi_desc, list);
223 for (; dn; dn = of_get_next_parent(dn)) {
224 if (entry->msi_attrib.is_64) {
225 prop = of_get_property(dn, "msi-address-64", &len);
230 prop = of_get_property(dn, "msi-address-32", &len);
237 "axon_msi: no msi-address-(32|64) properties found\n");
243 msg->address_hi = prop[0];
244 msg->address_lo = prop[1];
248 msg->address_lo = prop[0];
252 "axon_msi: malformed msi-address-(32|64) property\n");
262 static int axon_msi_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
264 unsigned int virq, rc;
265 struct msi_desc *entry;
267 struct axon_msic *msic;
269 msic = find_msi_translator(dev);
273 rc = setup_msi_msg_address(dev, &msg);
277 /* We rely on being able to stash a virq in a u16 */
278 BUILD_BUG_ON(NR_IRQS > 65536);
280 list_for_each_entry(entry, &dev->msi_list, list) {
281 virq = irq_create_direct_mapping(msic->irq_host);
282 if (virq == NO_IRQ) {
284 "axon_msi: virq allocation failed!\n");
287 dev_dbg(&dev->dev, "axon_msi: allocated virq 0x%x\n", virq);
289 set_irq_msi(virq, entry);
291 write_msi_msg(virq, &msg);
297 static void axon_msi_teardown_msi_irqs(struct pci_dev *dev)
299 struct msi_desc *entry;
301 dev_dbg(&dev->dev, "axon_msi: tearing down msi irqs\n");
303 list_for_each_entry(entry, &dev->msi_list, list) {
304 if (entry->irq == NO_IRQ)
307 set_irq_msi(entry->irq, NULL);
308 irq_dispose_mapping(entry->irq);
312 static struct irq_chip msic_irq_chip = {
313 .mask = mask_msi_irq,
314 .unmask = unmask_msi_irq,
315 .shutdown = unmask_msi_irq,
319 static int msic_host_map(struct irq_host *h, unsigned int virq,
322 set_irq_chip_and_handler(virq, &msic_irq_chip, handle_simple_irq);
327 static struct irq_host_ops msic_host_ops = {
328 .map = msic_host_map,
331 static int axon_msi_shutdown(struct platform_device *device)
333 struct axon_msic *msic = dev_get_drvdata(&device->dev);
336 pr_devel("axon_msi: disabling %s\n",
337 msic->irq_host->of_node->full_name);
338 tmp = dcr_read(msic->dcr_host, MSIC_CTRL_REG);
339 tmp &= ~MSIC_CTRL_ENABLE & ~MSIC_CTRL_IRQ_ENABLE;
340 msic_dcr_write(msic, MSIC_CTRL_REG, tmp);
345 static int axon_msi_probe(struct platform_device *device,
346 const struct of_device_id *device_id)
348 struct device_node *dn = device->dev.of_node;
349 struct axon_msic *msic;
351 int dcr_base, dcr_len;
353 pr_devel("axon_msi: setting up dn %s\n", dn->full_name);
355 msic = kzalloc(sizeof(struct axon_msic), GFP_KERNEL);
357 printk(KERN_ERR "axon_msi: couldn't allocate msic for %s\n",
362 dcr_base = dcr_resource_start(dn, 0);
363 dcr_len = dcr_resource_len(dn, 0);
365 if (dcr_base == 0 || dcr_len == 0) {
367 "axon_msi: couldn't parse dcr properties on %s\n",
372 msic->dcr_host = dcr_map(dn, dcr_base, dcr_len);
373 if (!DCR_MAP_OK(msic->dcr_host)) {
374 printk(KERN_ERR "axon_msi: dcr_map failed for %s\n",
379 msic->fifo_virt = dma_alloc_coherent(&device->dev, MSIC_FIFO_SIZE_BYTES,
380 &msic->fifo_phys, GFP_KERNEL);
381 if (!msic->fifo_virt) {
382 printk(KERN_ERR "axon_msi: couldn't allocate fifo for %s\n",
387 virq = irq_of_parse_and_map(dn, 0);
388 if (virq == NO_IRQ) {
389 printk(KERN_ERR "axon_msi: irq parse and map failed for %s\n",
393 memset(msic->fifo_virt, 0xff, MSIC_FIFO_SIZE_BYTES);
395 msic->irq_host = irq_alloc_host(dn, IRQ_HOST_MAP_NOMAP,
396 NR_IRQS, &msic_host_ops, 0);
397 if (!msic->irq_host) {
398 printk(KERN_ERR "axon_msi: couldn't allocate irq_host for %s\n",
403 msic->irq_host->host_data = msic;
405 set_irq_data(virq, msic);
406 set_irq_chained_handler(virq, axon_msi_cascade);
407 pr_devel("axon_msi: irq 0x%x setup for axon_msi\n", virq);
409 /* Enable the MSIC hardware */
410 msic_dcr_write(msic, MSIC_BASE_ADDR_HI_REG, msic->fifo_phys >> 32);
411 msic_dcr_write(msic, MSIC_BASE_ADDR_LO_REG,
412 msic->fifo_phys & 0xFFFFFFFF);
413 msic_dcr_write(msic, MSIC_CTRL_REG,
414 MSIC_CTRL_IRQ_ENABLE | MSIC_CTRL_ENABLE |
415 MSIC_CTRL_FIFO_SIZE);
417 msic->read_offset = dcr_read(msic->dcr_host, MSIC_WRITE_OFFSET_REG)
418 & MSIC_FIFO_SIZE_MASK;
420 dev_set_drvdata(&device->dev, msic);
422 ppc_md.setup_msi_irqs = axon_msi_setup_msi_irqs;
423 ppc_md.teardown_msi_irqs = axon_msi_teardown_msi_irqs;
424 ppc_md.msi_check_device = axon_msi_check_device;
426 axon_msi_debug_setup(dn, msic);
428 printk(KERN_DEBUG "axon_msi: setup MSIC on %s\n", dn->full_name);
433 dma_free_coherent(&device->dev, MSIC_FIFO_SIZE_BYTES, msic->fifo_virt,
442 static const struct of_device_id axon_msi_device_id[] = {
444 .compatible = "ibm,axon-msic"
449 static struct of_platform_driver axon_msi_driver = {
450 .probe = axon_msi_probe,
451 .shutdown = axon_msi_shutdown,
454 .owner = THIS_MODULE,
455 .of_match_table = axon_msi_device_id,
459 static int __init axon_msi_init(void)
461 return of_register_platform_driver(&axon_msi_driver);
463 subsys_initcall(axon_msi_init);
467 static int msic_set(void *data, u64 val)
469 struct axon_msic *msic = data;
470 out_le32(msic->trigger, val);
474 static int msic_get(void *data, u64 *val)
480 DEFINE_SIMPLE_ATTRIBUTE(fops_msic, msic_get, msic_set, "%llu\n");
482 void axon_msi_debug_setup(struct device_node *dn, struct axon_msic *msic)
487 addr = of_translate_address(dn, of_get_property(dn, "reg", NULL));
488 if (addr == OF_BAD_ADDR) {
489 pr_devel("axon_msi: couldn't translate reg property\n");
493 msic->trigger = ioremap(addr, 0x4);
494 if (!msic->trigger) {
495 pr_devel("axon_msi: ioremap failed\n");
499 snprintf(name, sizeof(name), "msic_%d", of_node_to_nid(dn));
501 if (!debugfs_create_file(name, 0600, powerpc_debugfs_root,
503 pr_devel("axon_msi: debugfs_create_file failed!\n");