blob: b769982612a09cde29443975683ebe276def53f0 [file] [log] [blame]
Jason Jin34e36c12008-05-23 16:32:46 +08001/*
Lan Chunhe-B2580680818812010-03-15 06:38:33 +00002 * Copyright (C) 2007-2010 Freescale Semiconductor, Inc.
Jason Jin34e36c12008-05-23 16:32:46 +08003 *
4 * Author: Tony Li <tony.li@freescale.com>
5 * Jason Jin <Jason.jin@freescale.com>
6 *
7 * The hwirq alloc and free code reuse from sysdev/mpic_msi.c
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; version 2 of the
12 * License.
13 *
14 */
15#include <linux/irq.h>
16#include <linux/bootmem.h>
Jason Jin34e36c12008-05-23 16:32:46 +080017#include <linux/msi.h>
18#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Jason Jin34e36c12008-05-23 16:32:46 +080020#include <linux/of_platform.h>
21#include <sysdev/fsl_soc.h>
22#include <asm/prom.h>
23#include <asm/hw_irq.h>
24#include <asm/ppc-pci.h>
Li Yang02adac62010-04-22 16:31:35 +080025#include <asm/mpic.h>
Jason Jin34e36c12008-05-23 16:32:46 +080026#include "fsl_msi.h"
27
28struct fsl_msi_feature {
29 u32 fsl_pic_ip;
30 u32 msiir_offset;
31};
32
Li Yang02adac62010-04-22 16:31:35 +080033struct fsl_msi_cascade_data {
34 struct fsl_msi *msi_data;
35 int index;
36};
Jason Jin34e36c12008-05-23 16:32:46 +080037
38static inline u32 fsl_msi_read(u32 __iomem *base, unsigned int reg)
39{
40 return in_be32(base + (reg >> 2));
41}
42
Jason Jin34e36c12008-05-23 16:32:46 +080043/*
44 * We do not need this actually. The MSIR register has been read once
45 * in the cascade interrupt. So, this MSI interrupt has been acked
46*/
47static void fsl_msi_end_irq(unsigned int virq)
48{
49}
50
51static struct irq_chip fsl_msi_chip = {
52 .mask = mask_msi_irq,
53 .unmask = unmask_msi_irq,
54 .ack = fsl_msi_end_irq,
Anton Blanchardfc380c02010-01-31 20:33:41 +000055 .name = "FSL-MSI",
Jason Jin34e36c12008-05-23 16:32:46 +080056};
57
58static int fsl_msi_host_map(struct irq_host *h, unsigned int virq,
59 irq_hw_number_t hw)
60{
Lan Chunhe-B2580680818812010-03-15 06:38:33 +000061 struct fsl_msi *msi_data = h->host_data;
Jason Jin34e36c12008-05-23 16:32:46 +080062 struct irq_chip *chip = &fsl_msi_chip;
63
Michael Ellerman6cff46f2009-10-13 19:44:51 +000064 irq_to_desc(virq)->status |= IRQ_TYPE_EDGE_FALLING;
Jason Jin34e36c12008-05-23 16:32:46 +080065
Lan Chunhe-B2580680818812010-03-15 06:38:33 +000066 set_irq_chip_data(virq, msi_data);
Anton Vorontsov692d1032008-05-23 17:41:02 +040067 set_irq_chip_and_handler(virq, chip, handle_edge_irq);
Jason Jin34e36c12008-05-23 16:32:46 +080068
69 return 0;
70}
71
72static struct irq_host_ops fsl_msi_host_ops = {
73 .map = fsl_msi_host_map,
74};
75
Jason Jin34e36c12008-05-23 16:32:46 +080076static int fsl_msi_init_allocator(struct fsl_msi *msi_data)
77{
Anton Vorontsov692d1032008-05-23 17:41:02 +040078 int rc;
Jason Jin34e36c12008-05-23 16:32:46 +080079
Michael Ellerman7e7ab362008-08-06 09:10:02 +100080 rc = msi_bitmap_alloc(&msi_data->bitmap, NR_MSI_IRQS,
81 msi_data->irqhost->of_node);
82 if (rc)
83 return rc;
Jason Jin34e36c12008-05-23 16:32:46 +080084
Michael Ellerman7e7ab362008-08-06 09:10:02 +100085 rc = msi_bitmap_reserve_dt_hwirqs(&msi_data->bitmap);
86 if (rc < 0) {
87 msi_bitmap_free(&msi_data->bitmap);
88 return rc;
Jason Jin34e36c12008-05-23 16:32:46 +080089 }
90
Jason Jin34e36c12008-05-23 16:32:46 +080091 return 0;
Jason Jin34e36c12008-05-23 16:32:46 +080092}
93
94static int fsl_msi_check_device(struct pci_dev *pdev, int nvec, int type)
95{
96 if (type == PCI_CAP_ID_MSIX)
97 pr_debug("fslmsi: MSI-X untested, trying anyway.\n");
98
99 return 0;
100}
101
102static void fsl_teardown_msi_irqs(struct pci_dev *pdev)
103{
104 struct msi_desc *entry;
Lan Chunhe-B2580680818812010-03-15 06:38:33 +0000105 struct fsl_msi *msi_data;
Jason Jin34e36c12008-05-23 16:32:46 +0800106
107 list_for_each_entry(entry, &pdev->msi_list, list) {
108 if (entry->irq == NO_IRQ)
109 continue;
Li Yang02adac62010-04-22 16:31:35 +0800110 msi_data = get_irq_data(entry->irq);
Jason Jin34e36c12008-05-23 16:32:46 +0800111 set_irq_msi(entry->irq, NULL);
Michael Ellerman7e7ab362008-08-06 09:10:02 +1000112 msi_bitmap_free_hwirqs(&msi_data->bitmap,
113 virq_to_hw(entry->irq), 1);
Jason Jin34e36c12008-05-23 16:32:46 +0800114 irq_dispose_mapping(entry->irq);
115 }
116
117 return;
118}
119
120static void fsl_compose_msi_msg(struct pci_dev *pdev, int hwirq,
Lan Chunhe-B2580680818812010-03-15 06:38:33 +0000121 struct msi_msg *msg,
122 struct fsl_msi *fsl_msi_data)
Jason Jin34e36c12008-05-23 16:32:46 +0800123{
Lan Chunhe-B2580680818812010-03-15 06:38:33 +0000124 struct fsl_msi *msi_data = fsl_msi_data;
Kumar Gala3da34aa2009-05-12 15:51:56 -0500125 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
126 u32 base = 0;
Jason Jin34e36c12008-05-23 16:32:46 +0800127
Kumar Gala3da34aa2009-05-12 15:51:56 -0500128 pci_bus_read_config_dword(hose->bus,
129 PCI_DEVFN(0, 0), PCI_BASE_ADDRESS_0, &base);
130
131 msg->address_lo = msi_data->msi_addr_lo + base;
Jason Jin34e36c12008-05-23 16:32:46 +0800132 msg->address_hi = msi_data->msi_addr_hi;
133 msg->data = hwirq;
134
135 pr_debug("%s: allocated srs: %d, ibs: %d\n",
136 __func__, hwirq / IRQS_PER_MSI_REG, hwirq % IRQS_PER_MSI_REG);
137}
138
139static int fsl_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
140{
Li Yang02adac62010-04-22 16:31:35 +0800141 int rc, hwirq = NO_IRQ;
Jason Jin34e36c12008-05-23 16:32:46 +0800142 unsigned int virq;
143 struct msi_desc *entry;
144 struct msi_msg msg;
Lan Chunhe-B2580680818812010-03-15 06:38:33 +0000145 struct fsl_msi *msi_data;
Jason Jin34e36c12008-05-23 16:32:46 +0800146
147 list_for_each_entry(entry, &pdev->msi_list, list) {
Lan Chunhe-B2580680818812010-03-15 06:38:33 +0000148 msi_data = get_irq_chip_data(entry->irq);
149
Michael Ellerman7e7ab362008-08-06 09:10:02 +1000150 hwirq = msi_bitmap_alloc_hwirqs(&msi_data->bitmap, 1);
Jason Jin34e36c12008-05-23 16:32:46 +0800151 if (hwirq < 0) {
152 rc = hwirq;
153 pr_debug("%s: fail allocating msi interrupt\n",
154 __func__);
155 goto out_free;
156 }
157
158 virq = irq_create_mapping(msi_data->irqhost, hwirq);
159
160 if (virq == NO_IRQ) {
Michael Ellerman7e7ab362008-08-06 09:10:02 +1000161 pr_debug("%s: fail mapping hwirq 0x%x\n",
Jason Jin34e36c12008-05-23 16:32:46 +0800162 __func__, hwirq);
Michael Ellerman7e7ab362008-08-06 09:10:02 +1000163 msi_bitmap_free_hwirqs(&msi_data->bitmap, hwirq, 1);
Jason Jin34e36c12008-05-23 16:32:46 +0800164 rc = -ENOSPC;
165 goto out_free;
166 }
Li Yang02adac62010-04-22 16:31:35 +0800167 set_irq_data(virq, msi_data);
Jason Jin34e36c12008-05-23 16:32:46 +0800168 set_irq_msi(virq, entry);
169
Lan Chunhe-B2580680818812010-03-15 06:38:33 +0000170 fsl_compose_msi_msg(pdev, hwirq, &msg, msi_data);
Jason Jin34e36c12008-05-23 16:32:46 +0800171 write_msi_msg(virq, &msg);
172 }
173 return 0;
174
175out_free:
176 return rc;
177}
178
Anton Vorontsov692d1032008-05-23 17:41:02 +0400179static void fsl_msi_cascade(unsigned int irq, struct irq_desc *desc)
Jason Jin34e36c12008-05-23 16:32:46 +0800180{
181 unsigned int cascade_irq;
Li Yang02adac62010-04-22 16:31:35 +0800182 struct fsl_msi *msi_data;
Jason Jin34e36c12008-05-23 16:32:46 +0800183 int msir_index = -1;
184 u32 msir_value = 0;
185 u32 intr_index;
186 u32 have_shift = 0;
Li Yang02adac62010-04-22 16:31:35 +0800187 struct fsl_msi_cascade_data *cascade_data;
188
189 cascade_data = (struct fsl_msi_cascade_data *)get_irq_data(irq);
190 msi_data = cascade_data->msi_data;
Jason Jin34e36c12008-05-23 16:32:46 +0800191
Thomas Gleixner239007b2009-11-17 16:46:45 +0100192 raw_spin_lock(&desc->lock);
Jason Jin34e36c12008-05-23 16:32:46 +0800193 if ((msi_data->feature & FSL_PIC_IP_MASK) == FSL_PIC_IP_IPIC) {
194 if (desc->chip->mask_ack)
195 desc->chip->mask_ack(irq);
196 else {
197 desc->chip->mask(irq);
198 desc->chip->ack(irq);
199 }
200 }
201
202 if (unlikely(desc->status & IRQ_INPROGRESS))
203 goto unlock;
204
Li Yang02adac62010-04-22 16:31:35 +0800205 msir_index = cascade_data->index;
Jason Jin34e36c12008-05-23 16:32:46 +0800206
207 if (msir_index >= NR_MSI_REG)
208 cascade_irq = NO_IRQ;
209
210 desc->status |= IRQ_INPROGRESS;
Lan Chunhe-B2580680818812010-03-15 06:38:33 +0000211 switch (msi_data->feature & FSL_PIC_IP_MASK) {
Jason Jin34e36c12008-05-23 16:32:46 +0800212 case FSL_PIC_IP_MPIC:
213 msir_value = fsl_msi_read(msi_data->msi_regs,
214 msir_index * 0x10);
215 break;
216 case FSL_PIC_IP_IPIC:
217 msir_value = fsl_msi_read(msi_data->msi_regs, msir_index * 0x4);
218 break;
219 }
220
221 while (msir_value) {
222 intr_index = ffs(msir_value) - 1;
223
224 cascade_irq = irq_linear_revmap(msi_data->irqhost,
Anton Vorontsov692d1032008-05-23 17:41:02 +0400225 msir_index * IRQS_PER_MSI_REG +
226 intr_index + have_shift);
Jason Jin34e36c12008-05-23 16:32:46 +0800227 if (cascade_irq != NO_IRQ)
228 generic_handle_irq(cascade_irq);
Anton Vorontsov692d1032008-05-23 17:41:02 +0400229 have_shift += intr_index + 1;
230 msir_value = msir_value >> (intr_index + 1);
Jason Jin34e36c12008-05-23 16:32:46 +0800231 }
232 desc->status &= ~IRQ_INPROGRESS;
233
234 switch (msi_data->feature & FSL_PIC_IP_MASK) {
235 case FSL_PIC_IP_MPIC:
236 desc->chip->eoi(irq);
237 break;
238 case FSL_PIC_IP_IPIC:
239 if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
240 desc->chip->unmask(irq);
241 break;
242 }
243unlock:
Thomas Gleixner239007b2009-11-17 16:46:45 +0100244 raw_spin_unlock(&desc->lock);
Jason Jin34e36c12008-05-23 16:32:46 +0800245}
246
247static int __devinit fsl_of_msi_probe(struct of_device *dev,
248 const struct of_device_id *match)
249{
250 struct fsl_msi *msi;
251 struct resource res;
252 int err, i, count;
253 int rc;
254 int virt_msir;
255 const u32 *p;
Anton Vorontsov692d1032008-05-23 17:41:02 +0400256 struct fsl_msi_feature *features = match->data;
Li Yang02adac62010-04-22 16:31:35 +0800257 struct fsl_msi_cascade_data *cascade_data = NULL;
Jason Jin34e36c12008-05-23 16:32:46 +0800258
259 printk(KERN_DEBUG "Setting up Freescale MSI support\n");
260
261 msi = kzalloc(sizeof(struct fsl_msi), GFP_KERNEL);
262 if (!msi) {
263 dev_err(&dev->dev, "No memory for MSI structure\n");
264 err = -ENOMEM;
265 goto error_out;
266 }
267
Michael Ellerman611cd902008-08-06 09:10:00 +1000268 msi->irqhost = irq_alloc_host(dev->node, IRQ_HOST_MAP_LINEAR,
269 NR_MSI_IRQS, &fsl_msi_host_ops, 0);
Jason Jin34e36c12008-05-23 16:32:46 +0800270
Jason Jin34e36c12008-05-23 16:32:46 +0800271 if (msi->irqhost == NULL) {
272 dev_err(&dev->dev, "No memory for MSI irqhost\n");
Jason Jin34e36c12008-05-23 16:32:46 +0800273 err = -ENOMEM;
274 goto error_out;
275 }
276
277 /* Get the MSI reg base */
278 err = of_address_to_resource(dev->node, 0, &res);
279 if (err) {
280 dev_err(&dev->dev, "%s resource error!\n",
281 dev->node->full_name);
282 goto error_out;
283 }
284
285 msi->msi_regs = ioremap(res.start, res.end - res.start + 1);
286 if (!msi->msi_regs) {
287 dev_err(&dev->dev, "ioremap problem failed\n");
288 goto error_out;
289 }
290
Anton Vorontsov692d1032008-05-23 17:41:02 +0400291 msi->feature = features->fsl_pic_ip;
Jason Jin34e36c12008-05-23 16:32:46 +0800292
293 msi->irqhost->host_data = msi;
294
295 msi->msi_addr_hi = 0x0;
Kumar Gala3da34aa2009-05-12 15:51:56 -0500296 msi->msi_addr_lo = features->msiir_offset + (res.start & 0xfffff);
Jason Jin34e36c12008-05-23 16:32:46 +0800297
298 rc = fsl_msi_init_allocator(msi);
299 if (rc) {
300 dev_err(&dev->dev, "Error allocating MSI bitmap\n");
301 goto error_out;
302 }
303
304 p = of_get_property(dev->node, "interrupts", &count);
305 if (!p) {
306 dev_err(&dev->dev, "no interrupts property found on %s\n",
307 dev->node->full_name);
308 err = -ENODEV;
309 goto error_out;
310 }
311 if (count % 8 != 0) {
312 dev_err(&dev->dev, "Malformed interrupts property on %s\n",
313 dev->node->full_name);
314 err = -EINVAL;
315 goto error_out;
316 }
317
318 count /= sizeof(u32);
319 for (i = 0; i < count / 2; i++) {
320 if (i > NR_MSI_REG)
321 break;
322 virt_msir = irq_of_parse_and_map(dev->node, i);
323 if (virt_msir != NO_IRQ) {
Li Yang02adac62010-04-22 16:31:35 +0800324 cascade_data = kzalloc(
325 sizeof(struct fsl_msi_cascade_data),
326 GFP_KERNEL);
327 if (!cascade_data) {
328 dev_err(&dev->dev,
329 "No memory for MSI cascade data\n");
330 err = -ENOMEM;
331 goto error_out;
332 }
333 cascade_data->index = i;
334 cascade_data->msi_data = msi;
335 set_irq_data(virt_msir, (void *)cascade_data);
Jason Jin34e36c12008-05-23 16:32:46 +0800336 set_irq_chained_handler(virt_msir, fsl_msi_cascade);
337 }
338 }
339
Lan Chunhe-B2580680818812010-03-15 06:38:33 +0000340 /* The multiple setting ppc_md.setup_msi_irqs will not harm things */
341 if (!ppc_md.setup_msi_irqs) {
342 ppc_md.setup_msi_irqs = fsl_setup_msi_irqs;
343 ppc_md.teardown_msi_irqs = fsl_teardown_msi_irqs;
344 ppc_md.msi_check_device = fsl_msi_check_device;
345 } else if (ppc_md.setup_msi_irqs != fsl_setup_msi_irqs) {
346 dev_err(&dev->dev, "Different MSI driver already installed!\n");
347 err = -ENODEV;
348 goto error_out;
349 }
Jason Jin34e36c12008-05-23 16:32:46 +0800350 return 0;
351error_out:
352 kfree(msi);
353 return err;
354}
355
356static const struct fsl_msi_feature mpic_msi_feature = {
357 .fsl_pic_ip = FSL_PIC_IP_MPIC,
358 .msiir_offset = 0x140,
359};
360
361static const struct fsl_msi_feature ipic_msi_feature = {
362 .fsl_pic_ip = FSL_PIC_IP_IPIC,
363 .msiir_offset = 0x38,
364};
365
366static const struct of_device_id fsl_of_msi_ids[] = {
367 {
368 .compatible = "fsl,mpic-msi",
369 .data = (void *)&mpic_msi_feature,
370 },
371 {
372 .compatible = "fsl,ipic-msi",
373 .data = (void *)&ipic_msi_feature,
374 },
375 {}
376};
377
378static struct of_platform_driver fsl_of_msi_driver = {
379 .name = "fsl-msi",
380 .match_table = fsl_of_msi_ids,
381 .probe = fsl_of_msi_probe,
382};
383
384static __init int fsl_of_msi_init(void)
385{
386 return of_register_platform_driver(&fsl_of_msi_driver);
387}
388
389subsys_initcall(fsl_of_msi_init);