blob: 0a8f5a9e87c94d48b040735d156d1e6ff298ee62 [file] [log] [blame]
Michael Ellerman05af7bd2007-05-08 12:58:37 +10001/*
2 * Copyright 2006, Segher Boessenkool, IBM Corporation.
3 * Copyright 2006-2007, Michael Ellerman, IBM Corporation.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; version 2 of the
8 * License.
9 *
10 */
11
12#include <linux/irq.h>
13#include <linux/bootmem.h>
14#include <linux/msi.h>
15#include <asm/mpic.h>
16#include <asm/prom.h>
17#include <asm/hw_irq.h>
18#include <asm/ppc-pci.h>
Michael Ellerman25235f72008-08-06 09:10:03 +100019#include <asm/msi_bitmap.h>
Michael Ellerman05af7bd2007-05-08 12:58:37 +100020
21#include "mpic.h"
22
23/* A bit ugly, can we get this from the pci_dev somehow? */
24static struct mpic *msi_mpic;
25
26static void mpic_u3msi_mask_irq(unsigned int irq)
27{
28 mask_msi_irq(irq);
29 mpic_mask_irq(irq);
30}
31
32static void mpic_u3msi_unmask_irq(unsigned int irq)
33{
34 mpic_unmask_irq(irq);
35 unmask_msi_irq(irq);
36}
37
38static struct irq_chip mpic_u3msi_chip = {
39 .shutdown = mpic_u3msi_mask_irq,
40 .mask = mpic_u3msi_mask_irq,
41 .unmask = mpic_u3msi_unmask_irq,
42 .eoi = mpic_end_irq,
43 .set_type = mpic_set_irq_type,
Olof Johansson17b5ee02007-09-18 06:12:29 +100044 .set_affinity = mpic_set_affinity,
Michael Ellerman05af7bd2007-05-08 12:58:37 +100045 .typename = "MPIC-U3MSI",
46};
47
48static u64 read_ht_magic_addr(struct pci_dev *pdev, unsigned int pos)
49{
50 u8 flags;
51 u32 tmp;
52 u64 addr;
53
54 pci_read_config_byte(pdev, pos + HT_MSI_FLAGS, &flags);
55
56 if (flags & HT_MSI_FLAGS_FIXED)
57 return HT_MSI_FIXED_ADDR;
58
59 pci_read_config_dword(pdev, pos + HT_MSI_ADDR_LO, &tmp);
60 addr = tmp & HT_MSI_ADDR_LO_MASK;
61 pci_read_config_dword(pdev, pos + HT_MSI_ADDR_HI, &tmp);
62 addr = addr | ((u64)tmp << 32);
63
64 return addr;
65}
66
67static u64 find_ht_magic_addr(struct pci_dev *pdev)
68{
69 struct pci_bus *bus;
70 unsigned int pos;
71
72 for (bus = pdev->bus; bus; bus = bus->parent) {
73 pos = pci_find_ht_capability(bus->self, HT_CAPTYPE_MSI_MAPPING);
74 if (pos)
75 return read_ht_magic_addr(bus->self, pos);
76 }
77
78 return 0;
79}
80
81static int u3msi_msi_check_device(struct pci_dev *pdev, int nvec, int type)
82{
83 if (type == PCI_CAP_ID_MSIX)
84 pr_debug("u3msi: MSI-X untested, trying anyway.\n");
85
86 /* If we can't find a magic address then MSI ain't gonna work */
87 if (find_ht_magic_addr(pdev) == 0) {
88 pr_debug("u3msi: no magic address found for %s\n",
89 pci_name(pdev));
90 return -ENXIO;
91 }
92
93 return 0;
94}
95
96static void u3msi_teardown_msi_irqs(struct pci_dev *pdev)
97{
98 struct msi_desc *entry;
99
100 list_for_each_entry(entry, &pdev->msi_list, list) {
101 if (entry->irq == NO_IRQ)
102 continue;
103
104 set_irq_msi(entry->irq, NULL);
Michael Ellerman25235f72008-08-06 09:10:03 +1000105 msi_bitmap_free_hwirqs(&msi_mpic->msi_bitmap,
106 virq_to_hw(entry->irq), 1);
Michael Ellerman05af7bd2007-05-08 12:58:37 +1000107 irq_dispose_mapping(entry->irq);
108 }
109
110 return;
111}
112
Michael Ellerman05af7bd2007-05-08 12:58:37 +1000113static int u3msi_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
114{
Michael Ellerman05af7bd2007-05-08 12:58:37 +1000115 unsigned int virq;
116 struct msi_desc *entry;
117 struct msi_msg msg;
Michael Ellerman21ccdd32007-09-20 16:36:51 +1000118 u64 addr;
Michael Ellerman25235f72008-08-06 09:10:03 +1000119 int hwirq;
Michael Ellerman21ccdd32007-09-20 16:36:51 +1000120
121 addr = find_ht_magic_addr(pdev);
122 msg.address_lo = addr & 0xFFFFFFFF;
123 msg.address_hi = addr >> 32;
Michael Ellerman05af7bd2007-05-08 12:58:37 +1000124
125 list_for_each_entry(entry, &pdev->msi_list, list) {
Michael Ellerman25235f72008-08-06 09:10:03 +1000126 hwirq = msi_bitmap_alloc_hwirqs(&msi_mpic->msi_bitmap, 1);
127 if (hwirq < 0) {
Michael Ellerman05af7bd2007-05-08 12:58:37 +1000128 pr_debug("u3msi: failed allocating hwirq\n");
Michael Ellerman25235f72008-08-06 09:10:03 +1000129 return hwirq;
Michael Ellerman05af7bd2007-05-08 12:58:37 +1000130 }
131
132 virq = irq_create_mapping(msi_mpic->irqhost, hwirq);
133 if (virq == NO_IRQ) {
Michael Ellerman25235f72008-08-06 09:10:03 +1000134 pr_debug("u3msi: failed mapping hwirq 0x%x\n", hwirq);
135 msi_bitmap_free_hwirqs(&msi_mpic->msi_bitmap, hwirq, 1);
Michael Ellermand9303d62007-09-20 16:36:47 +1000136 return -ENOSPC;
Michael Ellerman05af7bd2007-05-08 12:58:37 +1000137 }
138
139 set_irq_msi(virq, entry);
140 set_irq_chip(virq, &mpic_u3msi_chip);
141 set_irq_type(virq, IRQ_TYPE_EDGE_RISING);
142
Michael Ellerman25235f72008-08-06 09:10:03 +1000143 pr_debug("u3msi: allocated virq 0x%x (hw 0x%x) addr 0x%lx\n",
144 virq, hwirq, (unsigned long)addr);
Michael Ellerman21ccdd32007-09-20 16:36:51 +1000145
146 msg.data = hwirq;
Michael Ellerman05af7bd2007-05-08 12:58:37 +1000147 write_msi_msg(virq, &msg);
148
149 hwirq++;
150 }
151
152 return 0;
Michael Ellerman05af7bd2007-05-08 12:58:37 +1000153}
154
155int mpic_u3msi_init(struct mpic *mpic)
156{
157 int rc;
158
159 rc = mpic_msi_init_allocator(mpic);
160 if (rc) {
161 pr_debug("u3msi: Error allocating bitmap!\n");
162 return rc;
163 }
164
165 pr_debug("u3msi: Registering MPIC U3 MSI callbacks.\n");
166
167 BUG_ON(msi_mpic);
168 msi_mpic = mpic;
169
170 WARN_ON(ppc_md.setup_msi_irqs);
171 ppc_md.setup_msi_irqs = u3msi_setup_msi_irqs;
172 ppc_md.teardown_msi_irqs = u3msi_teardown_msi_irqs;
173 ppc_md.msi_check_device = u3msi_msi_check_device;
174
175 return 0;
176}