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/reboot.h>
19 #include <asm/machdep.h>
24 * MSIC registers, specified as offsets from dcr_base
26 #define MSIC_CTRL_REG 0x0
28 /* Base Address registers specify FIFO location in BE memory */
29 #define MSIC_BASE_ADDR_HI_REG 0x3
30 #define MSIC_BASE_ADDR_LO_REG 0x4
32 /* Hold the read/write offsets into the FIFO */
33 #define MSIC_READ_OFFSET_REG 0x5
34 #define MSIC_WRITE_OFFSET_REG 0x6
37 /* MSIC control register flags */
38 #define MSIC_CTRL_ENABLE 0x0001
39 #define MSIC_CTRL_FIFO_FULL_ENABLE 0x0002
40 #define MSIC_CTRL_IRQ_ENABLE 0x0008
41 #define MSIC_CTRL_FULL_STOP_ENABLE 0x0010
44 * The MSIC can be configured to use a FIFO of 32KB, 64KB, 128KB or 256KB.
45 * Currently we're using a 64KB FIFO size.
47 #define MSIC_FIFO_SIZE_SHIFT 16
48 #define MSIC_FIFO_SIZE_BYTES (1 << MSIC_FIFO_SIZE_SHIFT)
51 * To configure the FIFO size as (1 << n) bytes, we write (n - 15) into bits
52 * 8-9 of the MSIC control reg.
54 #define MSIC_CTRL_FIFO_SIZE (((MSIC_FIFO_SIZE_SHIFT - 15) << 8) & 0x300)
57 * We need to mask the read/write offsets to make sure they stay within
58 * the bounds of the FIFO. Also they should always be 16-byte aligned.
60 #define MSIC_FIFO_SIZE_MASK ((MSIC_FIFO_SIZE_BYTES - 1) & ~0xFu)
62 /* Each entry in the FIFO is 16 bytes, the first 4 bytes hold the irq # */
63 #define MSIC_FIFO_ENTRY_SIZE 0x10
67 struct device_node *dn;
68 struct irq_host *irq_host;
71 struct list_head list;
76 static LIST_HEAD(axon_msic_list);
78 static void msic_dcr_write(struct axon_msic *msic, unsigned int dcr_n, u32 val)
80 pr_debug("axon_msi: dcr_write(0x%x, 0x%x)\n", val, dcr_n);
82 dcr_write(msic->dcr_host, msic->dcr_base + dcr_n, val);
85 static u32 msic_dcr_read(struct axon_msic *msic, unsigned int dcr_n)
87 return dcr_read(msic->dcr_host, msic->dcr_base + dcr_n);
90 static void axon_msi_cascade(unsigned int irq, struct irq_desc *desc)
92 struct axon_msic *msic = get_irq_data(irq);
93 u32 write_offset, msi;
96 write_offset = msic_dcr_read(msic, MSIC_WRITE_OFFSET_REG);
97 pr_debug("axon_msi: original write_offset 0x%x\n", write_offset);
99 /* write_offset doesn't wrap properly, so we have to mask it */
100 write_offset &= MSIC_FIFO_SIZE_MASK;
102 while (msic->read_offset != write_offset) {
103 idx = msic->read_offset / sizeof(__le32);
104 msi = le32_to_cpu(msic->fifo[idx]);
107 pr_debug("axon_msi: woff %x roff %x msi %x\n",
108 write_offset, msic->read_offset, msi);
110 msic->read_offset += MSIC_FIFO_ENTRY_SIZE;
111 msic->read_offset &= MSIC_FIFO_SIZE_MASK;
113 if (msi < NR_IRQS && irq_map[msi].host == msic->irq_host)
114 generic_handle_irq(msi);
116 pr_debug("axon_msi: invalid irq 0x%x!\n", msi);
119 desc->chip->eoi(irq);
122 static struct axon_msic *find_msi_translator(struct pci_dev *dev)
124 struct irq_host *irq_host;
125 struct device_node *dn, *tmp;
127 struct axon_msic *msic = NULL;
129 dn = pci_device_to_OF_node(dev);
131 dev_dbg(&dev->dev, "axon_msi: no pci_dn found\n");
135 for (; dn; tmp = of_get_parent(dn), of_node_put(dn), dn = tmp) {
136 ph = of_get_property(dn, "msi-translator", NULL);
143 "axon_msi: no msi-translator property found\n");
148 dn = of_find_node_by_phandle(*ph);
151 "axon_msi: msi-translator doesn't point to a node\n");
155 irq_host = irq_find_host(dn);
157 dev_dbg(&dev->dev, "axon_msi: no irq_host found for node %s\n",
162 msic = irq_host->host_data;
171 static int axon_msi_check_device(struct pci_dev *dev, int nvec, int type)
173 if (!find_msi_translator(dev))
179 static int setup_msi_msg_address(struct pci_dev *dev, struct msi_msg *msg)
181 struct device_node *dn, *tmp;
182 struct msi_desc *entry;
186 dn = pci_device_to_OF_node(dev);
188 dev_dbg(&dev->dev, "axon_msi: no pci_dn found\n");
192 entry = list_first_entry(&dev->msi_list, struct msi_desc, list);
194 for (; dn; tmp = of_get_parent(dn), of_node_put(dn), dn = tmp) {
195 if (entry->msi_attrib.is_64) {
196 prop = of_get_property(dn, "msi-address-64", &len);
201 prop = of_get_property(dn, "msi-address-32", &len);
208 "axon_msi: no msi-address-(32|64) properties found\n");
214 msg->address_hi = prop[0];
215 msg->address_lo = prop[1];
219 msg->address_lo = prop[0];
223 "axon_msi: malformed msi-address-(32|64) property\n");
233 static int axon_msi_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
235 unsigned int virq, rc;
236 struct msi_desc *entry;
238 struct axon_msic *msic;
240 msic = find_msi_translator(dev);
244 rc = setup_msi_msg_address(dev, &msg);
248 /* We rely on being able to stash a virq in a u16 */
249 BUILD_BUG_ON(NR_IRQS > 65536);
251 list_for_each_entry(entry, &dev->msi_list, list) {
252 virq = irq_create_direct_mapping(msic->irq_host);
253 if (virq == NO_IRQ) {
255 "axon_msi: virq allocation failed!\n");
258 dev_dbg(&dev->dev, "axon_msi: allocated virq 0x%x\n", virq);
260 set_irq_msi(virq, entry);
262 write_msi_msg(virq, &msg);
268 static void axon_msi_teardown_msi_irqs(struct pci_dev *dev)
270 struct msi_desc *entry;
272 dev_dbg(&dev->dev, "axon_msi: tearing down msi irqs\n");
274 list_for_each_entry(entry, &dev->msi_list, list) {
275 if (entry->irq == NO_IRQ)
278 set_irq_msi(entry->irq, NULL);
279 irq_dispose_mapping(entry->irq);
283 static struct irq_chip msic_irq_chip = {
284 .mask = mask_msi_irq,
285 .unmask = unmask_msi_irq,
286 .shutdown = unmask_msi_irq,
287 .typename = "AXON-MSI",
290 static int msic_host_map(struct irq_host *h, unsigned int virq,
293 set_irq_chip_and_handler(virq, &msic_irq_chip, handle_simple_irq);
298 static int msic_host_match(struct irq_host *host, struct device_node *dn)
300 struct axon_msic *msic = host->host_data;
302 return msic->dn == dn;
305 static struct irq_host_ops msic_host_ops = {
306 .match = msic_host_match,
307 .map = msic_host_map,
310 static int axon_msi_notify_reboot(struct notifier_block *nb,
311 unsigned long code, void *data)
313 struct axon_msic *msic;
316 list_for_each_entry(msic, &axon_msic_list, list) {
317 pr_debug("axon_msi: disabling %s\n", msic->dn->full_name);
318 tmp = msic_dcr_read(msic, MSIC_CTRL_REG);
319 tmp &= ~MSIC_CTRL_ENABLE & ~MSIC_CTRL_IRQ_ENABLE;
320 msic_dcr_write(msic, MSIC_CTRL_REG, tmp);
326 static struct notifier_block axon_msi_reboot_notifier = {
327 .notifier_call = axon_msi_notify_reboot
330 static int axon_msi_setup_one(struct device_node *dn)
333 struct axon_msic *msic;
337 pr_debug("axon_msi: setting up dn %s\n", dn->full_name);
339 msic = kzalloc(sizeof(struct axon_msic), GFP_KERNEL);
341 printk(KERN_ERR "axon_msi: couldn't allocate msic for %s\n",
346 msic->dcr_base = dcr_resource_start(dn, 0);
347 dcr_len = dcr_resource_len(dn, 0);
349 if (msic->dcr_base == 0 || dcr_len == 0) {
351 "axon_msi: couldn't parse dcr properties on %s\n",
356 msic->dcr_host = dcr_map(dn, msic->dcr_base, dcr_len);
357 if (!DCR_MAP_OK(msic->dcr_host)) {
358 printk(KERN_ERR "axon_msi: dcr_map failed for %s\n",
363 page = alloc_pages_node(of_node_to_nid(dn), GFP_KERNEL,
364 get_order(MSIC_FIFO_SIZE_BYTES));
366 printk(KERN_ERR "axon_msi: couldn't allocate fifo for %s\n",
371 msic->fifo = page_address(page);
373 msic->irq_host = irq_alloc_host(IRQ_HOST_MAP_NOMAP, NR_IRQS,
375 if (!msic->irq_host) {
376 printk(KERN_ERR "axon_msi: couldn't allocate irq_host for %s\n",
381 msic->irq_host->host_data = msic;
383 virq = irq_of_parse_and_map(dn, 0);
384 if (virq == NO_IRQ) {
385 printk(KERN_ERR "axon_msi: irq parse and map failed for %s\n",
390 msic->dn = of_node_get(dn);
392 set_irq_data(virq, msic);
393 set_irq_chained_handler(virq, axon_msi_cascade);
394 pr_debug("axon_msi: irq 0x%x setup for axon_msi\n", virq);
396 /* Enable the MSIC hardware */
397 msic_dcr_write(msic, MSIC_BASE_ADDR_HI_REG, (u64)msic->fifo >> 32);
398 msic_dcr_write(msic, MSIC_BASE_ADDR_LO_REG,
399 (u64)msic->fifo & 0xFFFFFFFF);
400 msic_dcr_write(msic, MSIC_CTRL_REG,
401 MSIC_CTRL_IRQ_ENABLE | MSIC_CTRL_ENABLE |
402 MSIC_CTRL_FIFO_SIZE);
404 list_add(&msic->list, &axon_msic_list);
406 printk(KERN_DEBUG "axon_msi: setup MSIC on %s\n", dn->full_name);
411 kfree(msic->irq_host);
413 __free_pages(virt_to_page(msic->fifo), get_order(MSIC_FIFO_SIZE_BYTES));
421 static int axon_msi_init(void)
423 struct device_node *dn;
426 pr_debug("axon_msi: initialising ...\n");
428 for_each_compatible_node(dn, NULL, "ibm,axon-msic") {
429 if (axon_msi_setup_one(dn) == 0)
434 ppc_md.setup_msi_irqs = axon_msi_setup_msi_irqs;
435 ppc_md.teardown_msi_irqs = axon_msi_teardown_msi_irqs;
436 ppc_md.msi_check_device = axon_msi_check_device;
438 register_reboot_notifier(&axon_msi_reboot_notifier);
440 pr_debug("axon_msi: registered callbacks!\n");
445 arch_initcall(axon_msi_init);