blob: 1ab70391597298134a007ee42b71c4cb596fbbca [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
Li Yang694a7a32010-04-22 16:31:36 +080028LIST_HEAD(msi_head);
29
Jason Jin34e36c12008-05-23 16:32:46 +080030struct fsl_msi_feature {
31 u32 fsl_pic_ip;
32 u32 msiir_offset;
33};
34
Li Yang02adac62010-04-22 16:31:35 +080035struct fsl_msi_cascade_data {
36 struct fsl_msi *msi_data;
37 int index;
38};
Jason Jin34e36c12008-05-23 16:32:46 +080039
40static inline u32 fsl_msi_read(u32 __iomem *base, unsigned int reg)
41{
42 return in_be32(base + (reg >> 2));
43}
44
Jason Jin34e36c12008-05-23 16:32:46 +080045/*
46 * We do not need this actually. The MSIR register has been read once
47 * in the cascade interrupt. So, this MSI interrupt has been acked
48*/
49static void fsl_msi_end_irq(unsigned int virq)
50{
51}
52
53static struct irq_chip fsl_msi_chip = {
54 .mask = mask_msi_irq,
55 .unmask = unmask_msi_irq,
56 .ack = fsl_msi_end_irq,
Anton Blanchardfc380c02010-01-31 20:33:41 +000057 .name = "FSL-MSI",
Jason Jin34e36c12008-05-23 16:32:46 +080058};
59
60static int fsl_msi_host_map(struct irq_host *h, unsigned int virq,
61 irq_hw_number_t hw)
62{
Lan Chunhe-B2580680818812010-03-15 06:38:33 +000063 struct fsl_msi *msi_data = h->host_data;
Jason Jin34e36c12008-05-23 16:32:46 +080064 struct irq_chip *chip = &fsl_msi_chip;
65
Michael Ellerman6cff46f2009-10-13 19:44:51 +000066 irq_to_desc(virq)->status |= IRQ_TYPE_EDGE_FALLING;
Jason Jin34e36c12008-05-23 16:32:46 +080067
Lan Chunhe-B2580680818812010-03-15 06:38:33 +000068 set_irq_chip_data(virq, msi_data);
Anton Vorontsov692d1032008-05-23 17:41:02 +040069 set_irq_chip_and_handler(virq, chip, handle_edge_irq);
Jason Jin34e36c12008-05-23 16:32:46 +080070
71 return 0;
72}
73
74static struct irq_host_ops fsl_msi_host_ops = {
75 .map = fsl_msi_host_map,
76};
77
Jason Jin34e36c12008-05-23 16:32:46 +080078static int fsl_msi_init_allocator(struct fsl_msi *msi_data)
79{
Anton Vorontsov692d1032008-05-23 17:41:02 +040080 int rc;
Jason Jin34e36c12008-05-23 16:32:46 +080081
Michael Ellerman7e7ab362008-08-06 09:10:02 +100082 rc = msi_bitmap_alloc(&msi_data->bitmap, NR_MSI_IRQS,
83 msi_data->irqhost->of_node);
84 if (rc)
85 return rc;
Jason Jin34e36c12008-05-23 16:32:46 +080086
Michael Ellerman7e7ab362008-08-06 09:10:02 +100087 rc = msi_bitmap_reserve_dt_hwirqs(&msi_data->bitmap);
88 if (rc < 0) {
89 msi_bitmap_free(&msi_data->bitmap);
90 return rc;
Jason Jin34e36c12008-05-23 16:32:46 +080091 }
92
Jason Jin34e36c12008-05-23 16:32:46 +080093 return 0;
Jason Jin34e36c12008-05-23 16:32:46 +080094}
95
96static int fsl_msi_check_device(struct pci_dev *pdev, int nvec, int type)
97{
98 if (type == PCI_CAP_ID_MSIX)
99 pr_debug("fslmsi: MSI-X untested, trying anyway.\n");
100
101 return 0;
102}
103
104static void fsl_teardown_msi_irqs(struct pci_dev *pdev)
105{
106 struct msi_desc *entry;
Lan Chunhe-B2580680818812010-03-15 06:38:33 +0000107 struct fsl_msi *msi_data;
Jason Jin34e36c12008-05-23 16:32:46 +0800108
109 list_for_each_entry(entry, &pdev->msi_list, list) {
110 if (entry->irq == NO_IRQ)
111 continue;
Li Yang02adac62010-04-22 16:31:35 +0800112 msi_data = get_irq_data(entry->irq);
Jason Jin34e36c12008-05-23 16:32:46 +0800113 set_irq_msi(entry->irq, NULL);
Michael Ellerman7e7ab362008-08-06 09:10:02 +1000114 msi_bitmap_free_hwirqs(&msi_data->bitmap,
115 virq_to_hw(entry->irq), 1);
Jason Jin34e36c12008-05-23 16:32:46 +0800116 irq_dispose_mapping(entry->irq);
117 }
118
119 return;
120}
121
122static void fsl_compose_msi_msg(struct pci_dev *pdev, int hwirq,
Lan Chunhe-B2580680818812010-03-15 06:38:33 +0000123 struct msi_msg *msg,
124 struct fsl_msi *fsl_msi_data)
Jason Jin34e36c12008-05-23 16:32:46 +0800125{
Lan Chunhe-B2580680818812010-03-15 06:38:33 +0000126 struct fsl_msi *msi_data = fsl_msi_data;
Kumar Gala3da34aa2009-05-12 15:51:56 -0500127 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
128 u32 base = 0;
Jason Jin34e36c12008-05-23 16:32:46 +0800129
Kumar Gala3da34aa2009-05-12 15:51:56 -0500130 pci_bus_read_config_dword(hose->bus,
131 PCI_DEVFN(0, 0), PCI_BASE_ADDRESS_0, &base);
132
133 msg->address_lo = msi_data->msi_addr_lo + base;
Jason Jin34e36c12008-05-23 16:32:46 +0800134 msg->address_hi = msi_data->msi_addr_hi;
135 msg->data = hwirq;
136
137 pr_debug("%s: allocated srs: %d, ibs: %d\n",
138 __func__, hwirq / IRQS_PER_MSI_REG, hwirq % IRQS_PER_MSI_REG);
139}
140
141static int fsl_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
142{
Li Yang694a7a32010-04-22 16:31:36 +0800143 int rc, hwirq = -ENOMEM;
Jason Jin34e36c12008-05-23 16:32:46 +0800144 unsigned int virq;
145 struct msi_desc *entry;
146 struct msi_msg msg;
Lan Chunhe-B2580680818812010-03-15 06:38:33 +0000147 struct fsl_msi *msi_data;
Jason Jin34e36c12008-05-23 16:32:46 +0800148
149 list_for_each_entry(entry, &pdev->msi_list, list) {
Li Yang694a7a32010-04-22 16:31:36 +0800150 list_for_each_entry(msi_data, &msi_head, list) {
151 hwirq = msi_bitmap_alloc_hwirqs(&msi_data->bitmap, 1);
152 if (hwirq >= 0)
153 break;
154 }
Lan Chunhe-B2580680818812010-03-15 06:38:33 +0000155
Jason Jin34e36c12008-05-23 16:32:46 +0800156 if (hwirq < 0) {
157 rc = hwirq;
158 pr_debug("%s: fail allocating msi interrupt\n",
159 __func__);
160 goto out_free;
161 }
162
163 virq = irq_create_mapping(msi_data->irqhost, hwirq);
164
165 if (virq == NO_IRQ) {
Michael Ellerman7e7ab362008-08-06 09:10:02 +1000166 pr_debug("%s: fail mapping hwirq 0x%x\n",
Jason Jin34e36c12008-05-23 16:32:46 +0800167 __func__, hwirq);
Michael Ellerman7e7ab362008-08-06 09:10:02 +1000168 msi_bitmap_free_hwirqs(&msi_data->bitmap, hwirq, 1);
Jason Jin34e36c12008-05-23 16:32:46 +0800169 rc = -ENOSPC;
170 goto out_free;
171 }
Li Yang02adac62010-04-22 16:31:35 +0800172 set_irq_data(virq, msi_data);
Jason Jin34e36c12008-05-23 16:32:46 +0800173 set_irq_msi(virq, entry);
174
Lan Chunhe-B2580680818812010-03-15 06:38:33 +0000175 fsl_compose_msi_msg(pdev, hwirq, &msg, msi_data);
Jason Jin34e36c12008-05-23 16:32:46 +0800176 write_msi_msg(virq, &msg);
177 }
178 return 0;
179
180out_free:
Li Yang694a7a32010-04-22 16:31:36 +0800181 /* free by the caller of this function */
Jason Jin34e36c12008-05-23 16:32:46 +0800182 return rc;
183}
184
Anton Vorontsov692d1032008-05-23 17:41:02 +0400185static void fsl_msi_cascade(unsigned int irq, struct irq_desc *desc)
Jason Jin34e36c12008-05-23 16:32:46 +0800186{
187 unsigned int cascade_irq;
Li Yang02adac62010-04-22 16:31:35 +0800188 struct fsl_msi *msi_data;
Jason Jin34e36c12008-05-23 16:32:46 +0800189 int msir_index = -1;
190 u32 msir_value = 0;
191 u32 intr_index;
192 u32 have_shift = 0;
Li Yang02adac62010-04-22 16:31:35 +0800193 struct fsl_msi_cascade_data *cascade_data;
194
195 cascade_data = (struct fsl_msi_cascade_data *)get_irq_data(irq);
196 msi_data = cascade_data->msi_data;
Jason Jin34e36c12008-05-23 16:32:46 +0800197
Thomas Gleixner239007b2009-11-17 16:46:45 +0100198 raw_spin_lock(&desc->lock);
Jason Jin34e36c12008-05-23 16:32:46 +0800199 if ((msi_data->feature & FSL_PIC_IP_MASK) == FSL_PIC_IP_IPIC) {
200 if (desc->chip->mask_ack)
201 desc->chip->mask_ack(irq);
202 else {
203 desc->chip->mask(irq);
204 desc->chip->ack(irq);
205 }
206 }
207
208 if (unlikely(desc->status & IRQ_INPROGRESS))
209 goto unlock;
210
Li Yang02adac62010-04-22 16:31:35 +0800211 msir_index = cascade_data->index;
Jason Jin34e36c12008-05-23 16:32:46 +0800212
213 if (msir_index >= NR_MSI_REG)
214 cascade_irq = NO_IRQ;
215
216 desc->status |= IRQ_INPROGRESS;
Lan Chunhe-B2580680818812010-03-15 06:38:33 +0000217 switch (msi_data->feature & FSL_PIC_IP_MASK) {
Jason Jin34e36c12008-05-23 16:32:46 +0800218 case FSL_PIC_IP_MPIC:
219 msir_value = fsl_msi_read(msi_data->msi_regs,
220 msir_index * 0x10);
221 break;
222 case FSL_PIC_IP_IPIC:
223 msir_value = fsl_msi_read(msi_data->msi_regs, msir_index * 0x4);
224 break;
225 }
226
227 while (msir_value) {
228 intr_index = ffs(msir_value) - 1;
229
230 cascade_irq = irq_linear_revmap(msi_data->irqhost,
Anton Vorontsov692d1032008-05-23 17:41:02 +0400231 msir_index * IRQS_PER_MSI_REG +
232 intr_index + have_shift);
Jason Jin34e36c12008-05-23 16:32:46 +0800233 if (cascade_irq != NO_IRQ)
234 generic_handle_irq(cascade_irq);
Anton Vorontsov692d1032008-05-23 17:41:02 +0400235 have_shift += intr_index + 1;
236 msir_value = msir_value >> (intr_index + 1);
Jason Jin34e36c12008-05-23 16:32:46 +0800237 }
238 desc->status &= ~IRQ_INPROGRESS;
239
240 switch (msi_data->feature & FSL_PIC_IP_MASK) {
241 case FSL_PIC_IP_MPIC:
242 desc->chip->eoi(irq);
243 break;
244 case FSL_PIC_IP_IPIC:
245 if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
246 desc->chip->unmask(irq);
247 break;
248 }
249unlock:
Thomas Gleixner239007b2009-11-17 16:46:45 +0100250 raw_spin_unlock(&desc->lock);
Jason Jin34e36c12008-05-23 16:32:46 +0800251}
252
253static int __devinit fsl_of_msi_probe(struct of_device *dev,
254 const struct of_device_id *match)
255{
256 struct fsl_msi *msi;
257 struct resource res;
258 int err, i, count;
259 int rc;
260 int virt_msir;
261 const u32 *p;
Anton Vorontsov692d1032008-05-23 17:41:02 +0400262 struct fsl_msi_feature *features = match->data;
Li Yang02adac62010-04-22 16:31:35 +0800263 struct fsl_msi_cascade_data *cascade_data = NULL;
Li Yang061ca4a2010-04-22 16:31:37 +0800264 int len;
265 u32 offset;
Jason Jin34e36c12008-05-23 16:32:46 +0800266
267 printk(KERN_DEBUG "Setting up Freescale MSI support\n");
268
269 msi = kzalloc(sizeof(struct fsl_msi), GFP_KERNEL);
270 if (!msi) {
271 dev_err(&dev->dev, "No memory for MSI structure\n");
272 err = -ENOMEM;
273 goto error_out;
274 }
275
Michael Ellerman611cd902008-08-06 09:10:00 +1000276 msi->irqhost = irq_alloc_host(dev->node, IRQ_HOST_MAP_LINEAR,
277 NR_MSI_IRQS, &fsl_msi_host_ops, 0);
Jason Jin34e36c12008-05-23 16:32:46 +0800278
Jason Jin34e36c12008-05-23 16:32:46 +0800279 if (msi->irqhost == NULL) {
280 dev_err(&dev->dev, "No memory for MSI irqhost\n");
Jason Jin34e36c12008-05-23 16:32:46 +0800281 err = -ENOMEM;
282 goto error_out;
283 }
284
285 /* Get the MSI reg base */
286 err = of_address_to_resource(dev->node, 0, &res);
287 if (err) {
288 dev_err(&dev->dev, "%s resource error!\n",
289 dev->node->full_name);
290 goto error_out;
291 }
292
293 msi->msi_regs = ioremap(res.start, res.end - res.start + 1);
294 if (!msi->msi_regs) {
295 dev_err(&dev->dev, "ioremap problem failed\n");
296 goto error_out;
297 }
298
Anton Vorontsov692d1032008-05-23 17:41:02 +0400299 msi->feature = features->fsl_pic_ip;
Jason Jin34e36c12008-05-23 16:32:46 +0800300
301 msi->irqhost->host_data = msi;
302
303 msi->msi_addr_hi = 0x0;
Kumar Gala3da34aa2009-05-12 15:51:56 -0500304 msi->msi_addr_lo = features->msiir_offset + (res.start & 0xfffff);
Jason Jin34e36c12008-05-23 16:32:46 +0800305
306 rc = fsl_msi_init_allocator(msi);
307 if (rc) {
308 dev_err(&dev->dev, "Error allocating MSI bitmap\n");
309 goto error_out;
310 }
311
312 p = of_get_property(dev->node, "interrupts", &count);
313 if (!p) {
314 dev_err(&dev->dev, "no interrupts property found on %s\n",
315 dev->node->full_name);
316 err = -ENODEV;
317 goto error_out;
318 }
319 if (count % 8 != 0) {
320 dev_err(&dev->dev, "Malformed interrupts property on %s\n",
321 dev->node->full_name);
322 err = -EINVAL;
323 goto error_out;
324 }
Li Yang061ca4a2010-04-22 16:31:37 +0800325 offset = 0;
326 p = of_get_property(dev->node, "msi-available-ranges", &len);
327 if (p)
328 offset = *p / IRQS_PER_MSI_REG;
Jason Jin34e36c12008-05-23 16:32:46 +0800329
330 count /= sizeof(u32);
331 for (i = 0; i < count / 2; i++) {
332 if (i > NR_MSI_REG)
333 break;
334 virt_msir = irq_of_parse_and_map(dev->node, i);
335 if (virt_msir != NO_IRQ) {
Li Yang02adac62010-04-22 16:31:35 +0800336 cascade_data = kzalloc(
337 sizeof(struct fsl_msi_cascade_data),
338 GFP_KERNEL);
339 if (!cascade_data) {
340 dev_err(&dev->dev,
341 "No memory for MSI cascade data\n");
342 err = -ENOMEM;
343 goto error_out;
344 }
Li Yang061ca4a2010-04-22 16:31:37 +0800345 cascade_data->index = i + offset;
Li Yang02adac62010-04-22 16:31:35 +0800346 cascade_data->msi_data = msi;
347 set_irq_data(virt_msir, (void *)cascade_data);
Jason Jin34e36c12008-05-23 16:32:46 +0800348 set_irq_chained_handler(virt_msir, fsl_msi_cascade);
349 }
350 }
351
Li Yang694a7a32010-04-22 16:31:36 +0800352 list_add_tail(&msi->list, &msi_head);
353
Lan Chunhe-B2580680818812010-03-15 06:38:33 +0000354 /* The multiple setting ppc_md.setup_msi_irqs will not harm things */
355 if (!ppc_md.setup_msi_irqs) {
356 ppc_md.setup_msi_irqs = fsl_setup_msi_irqs;
357 ppc_md.teardown_msi_irqs = fsl_teardown_msi_irqs;
358 ppc_md.msi_check_device = fsl_msi_check_device;
359 } else if (ppc_md.setup_msi_irqs != fsl_setup_msi_irqs) {
360 dev_err(&dev->dev, "Different MSI driver already installed!\n");
361 err = -ENODEV;
362 goto error_out;
363 }
Jason Jin34e36c12008-05-23 16:32:46 +0800364 return 0;
365error_out:
366 kfree(msi);
367 return err;
368}
369
370static const struct fsl_msi_feature mpic_msi_feature = {
371 .fsl_pic_ip = FSL_PIC_IP_MPIC,
372 .msiir_offset = 0x140,
373};
374
375static const struct fsl_msi_feature ipic_msi_feature = {
376 .fsl_pic_ip = FSL_PIC_IP_IPIC,
377 .msiir_offset = 0x38,
378};
379
380static const struct of_device_id fsl_of_msi_ids[] = {
381 {
382 .compatible = "fsl,mpic-msi",
383 .data = (void *)&mpic_msi_feature,
384 },
385 {
386 .compatible = "fsl,ipic-msi",
387 .data = (void *)&ipic_msi_feature,
388 },
389 {}
390};
391
392static struct of_platform_driver fsl_of_msi_driver = {
393 .name = "fsl-msi",
394 .match_table = fsl_of_msi_ids,
395 .probe = fsl_of_msi_probe,
396};
397
398static __init int fsl_of_msi_init(void)
399{
400 return of_register_platform_driver(&fsl_of_msi_driver);
401}
402
403subsys_initcall(fsl_of_msi_init);