]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - arch/powerpc/platforms/4xx/msi.c
Merge tag 'leds-for-5.1-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/j.anasz...
[mirror_ubuntu-hirsute-kernel.git] / arch / powerpc / platforms / 4xx / msi.c
1 /*
2 * Adding PCI-E MSI support for PPC4XX SoCs.
3 *
4 * Copyright (c) 2010, Applied Micro Circuits Corporation
5 * Authors: Tirumala R Marri <tmarri@apm.com>
6 * Feng Kan <fkan@apm.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24 #include <linux/irq.h>
25 #include <linux/pci.h>
26 #include <linux/msi.h>
27 #include <linux/of_platform.h>
28 #include <linux/interrupt.h>
29 #include <linux/export.h>
30 #include <linux/kernel.h>
31 #include <asm/prom.h>
32 #include <asm/hw_irq.h>
33 #include <asm/ppc-pci.h>
34 #include <asm/dcr.h>
35 #include <asm/dcr-regs.h>
36 #include <asm/msi_bitmap.h>
37
38 #define PEIH_TERMADH 0x00
39 #define PEIH_TERMADL 0x08
40 #define PEIH_MSIED 0x10
41 #define PEIH_MSIMK 0x18
42 #define PEIH_MSIASS 0x20
43 #define PEIH_FLUSH0 0x30
44 #define PEIH_FLUSH1 0x38
45 #define PEIH_CNTRST 0x48
46
47 static int msi_irqs;
48
49 struct ppc4xx_msi {
50 u32 msi_addr_lo;
51 u32 msi_addr_hi;
52 void __iomem *msi_regs;
53 int *msi_virqs;
54 struct msi_bitmap bitmap;
55 struct device_node *msi_dev;
56 };
57
58 static struct ppc4xx_msi ppc4xx_msi;
59
60 static int ppc4xx_msi_init_allocator(struct platform_device *dev,
61 struct ppc4xx_msi *msi_data)
62 {
63 int err;
64
65 err = msi_bitmap_alloc(&msi_data->bitmap, msi_irqs,
66 dev->dev.of_node);
67 if (err)
68 return err;
69
70 err = msi_bitmap_reserve_dt_hwirqs(&msi_data->bitmap);
71 if (err < 0) {
72 msi_bitmap_free(&msi_data->bitmap);
73 return err;
74 }
75
76 return 0;
77 }
78
79 static int ppc4xx_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
80 {
81 int int_no = -ENOMEM;
82 unsigned int virq;
83 struct msi_msg msg;
84 struct msi_desc *entry;
85 struct ppc4xx_msi *msi_data = &ppc4xx_msi;
86
87 dev_dbg(&dev->dev, "PCIE-MSI:%s called. vec %x type %d\n",
88 __func__, nvec, type);
89 if (type == PCI_CAP_ID_MSIX)
90 pr_debug("ppc4xx msi: MSI-X untested, trying anyway.\n");
91
92 msi_data->msi_virqs = kmalloc_array(msi_irqs, sizeof(int), GFP_KERNEL);
93 if (!msi_data->msi_virqs)
94 return -ENOMEM;
95
96 for_each_pci_msi_entry(entry, dev) {
97 int_no = msi_bitmap_alloc_hwirqs(&msi_data->bitmap, 1);
98 if (int_no >= 0)
99 break;
100 if (int_no < 0) {
101 pr_debug("%s: fail allocating msi interrupt\n",
102 __func__);
103 }
104 virq = irq_of_parse_and_map(msi_data->msi_dev, int_no);
105 if (!virq) {
106 dev_err(&dev->dev, "%s: fail mapping irq\n", __func__);
107 msi_bitmap_free_hwirqs(&msi_data->bitmap, int_no, 1);
108 return -ENOSPC;
109 }
110 dev_dbg(&dev->dev, "%s: virq = %d\n", __func__, virq);
111
112 /* Setup msi address space */
113 msg.address_hi = msi_data->msi_addr_hi;
114 msg.address_lo = msi_data->msi_addr_lo;
115
116 irq_set_msi_desc(virq, entry);
117 msg.data = int_no;
118 pci_write_msi_msg(virq, &msg);
119 }
120 return 0;
121 }
122
123 void ppc4xx_teardown_msi_irqs(struct pci_dev *dev)
124 {
125 struct msi_desc *entry;
126 struct ppc4xx_msi *msi_data = &ppc4xx_msi;
127 irq_hw_number_t hwirq;
128
129 dev_dbg(&dev->dev, "PCIE-MSI: tearing down msi irqs\n");
130
131 for_each_pci_msi_entry(entry, dev) {
132 if (!entry->irq)
133 continue;
134 hwirq = virq_to_hw(entry->irq);
135 irq_set_msi_desc(entry->irq, NULL);
136 irq_dispose_mapping(entry->irq);
137 msi_bitmap_free_hwirqs(&msi_data->bitmap, hwirq, 1);
138 }
139 }
140
141 static int ppc4xx_setup_pcieh_hw(struct platform_device *dev,
142 struct resource res, struct ppc4xx_msi *msi)
143 {
144 const u32 *msi_data;
145 const u32 *msi_mask;
146 const u32 *sdr_addr;
147 dma_addr_t msi_phys;
148 void *msi_virt;
149 int err;
150
151 sdr_addr = of_get_property(dev->dev.of_node, "sdr-base", NULL);
152 if (!sdr_addr)
153 return -EINVAL;
154
155 msi_data = of_get_property(dev->dev.of_node, "msi-data", NULL);
156 if (!msi_data)
157 return -EINVAL;
158
159 msi_mask = of_get_property(dev->dev.of_node, "msi-mask", NULL);
160 if (!msi_mask)
161 return -EINVAL;
162
163 msi->msi_dev = of_find_node_by_name(NULL, "ppc4xx-msi");
164 if (!msi->msi_dev)
165 return -ENODEV;
166
167 msi->msi_regs = of_iomap(msi->msi_dev, 0);
168 if (!msi->msi_regs) {
169 dev_err(&dev->dev, "of_iomap failed\n");
170 err = -ENOMEM;
171 goto node_put;
172 }
173 dev_dbg(&dev->dev, "PCIE-MSI: msi register mapped 0x%x 0x%x\n",
174 (u32) (msi->msi_regs + PEIH_TERMADH), (u32) (msi->msi_regs));
175
176 msi_virt = dma_alloc_coherent(&dev->dev, 64, &msi_phys, GFP_KERNEL);
177 if (!msi_virt) {
178 err = -ENOMEM;
179 goto iounmap;
180 }
181 msi->msi_addr_hi = upper_32_bits(msi_phys);
182 msi->msi_addr_lo = lower_32_bits(msi_phys & 0xffffffff);
183 dev_dbg(&dev->dev, "PCIE-MSI: msi address high 0x%x, low 0x%x\n",
184 msi->msi_addr_hi, msi->msi_addr_lo);
185
186 mtdcri(SDR0, *sdr_addr, upper_32_bits(res.start)); /*HIGH addr */
187 mtdcri(SDR0, *sdr_addr + 1, lower_32_bits(res.start)); /* Low addr */
188
189 /* Progam the Interrupt handler Termination addr registers */
190 out_be32(msi->msi_regs + PEIH_TERMADH, msi->msi_addr_hi);
191 out_be32(msi->msi_regs + PEIH_TERMADL, msi->msi_addr_lo);
192
193 /* Program MSI Expected data and Mask bits */
194 out_be32(msi->msi_regs + PEIH_MSIED, *msi_data);
195 out_be32(msi->msi_regs + PEIH_MSIMK, *msi_mask);
196
197 dma_free_coherent(&dev->dev, 64, msi_virt, msi_phys);
198
199 return 0;
200
201 iounmap:
202 iounmap(msi->msi_regs);
203 node_put:
204 of_node_put(msi->msi_dev);
205 return err;
206 }
207
208 static int ppc4xx_of_msi_remove(struct platform_device *dev)
209 {
210 struct ppc4xx_msi *msi = dev->dev.platform_data;
211 int i;
212 int virq;
213
214 for (i = 0; i < msi_irqs; i++) {
215 virq = msi->msi_virqs[i];
216 if (virq)
217 irq_dispose_mapping(virq);
218 }
219
220 if (msi->bitmap.bitmap)
221 msi_bitmap_free(&msi->bitmap);
222 iounmap(msi->msi_regs);
223 of_node_put(msi->msi_dev);
224
225 return 0;
226 }
227
228 static int ppc4xx_msi_probe(struct platform_device *dev)
229 {
230 struct ppc4xx_msi *msi;
231 struct resource res;
232 int err = 0;
233 struct pci_controller *phb;
234
235 dev_dbg(&dev->dev, "PCIE-MSI: Setting up MSI support...\n");
236
237 msi = devm_kzalloc(&dev->dev, sizeof(*msi), GFP_KERNEL);
238 if (!msi)
239 return -ENOMEM;
240 dev->dev.platform_data = msi;
241
242 /* Get MSI ranges */
243 err = of_address_to_resource(dev->dev.of_node, 0, &res);
244 if (err) {
245 dev_err(&dev->dev, "%pOF resource error!\n", dev->dev.of_node);
246 return err;
247 }
248
249 msi_irqs = of_irq_count(dev->dev.of_node);
250 if (!msi_irqs)
251 return -ENODEV;
252
253 err = ppc4xx_setup_pcieh_hw(dev, res, msi);
254 if (err)
255 return err;
256
257 err = ppc4xx_msi_init_allocator(dev, msi);
258 if (err) {
259 dev_err(&dev->dev, "Error allocating MSI bitmap\n");
260 goto error_out;
261 }
262 ppc4xx_msi = *msi;
263
264 list_for_each_entry(phb, &hose_list, list_node) {
265 phb->controller_ops.setup_msi_irqs = ppc4xx_setup_msi_irqs;
266 phb->controller_ops.teardown_msi_irqs = ppc4xx_teardown_msi_irqs;
267 }
268 return 0;
269
270 error_out:
271 ppc4xx_of_msi_remove(dev);
272 return err;
273 }
274 static const struct of_device_id ppc4xx_msi_ids[] = {
275 {
276 .compatible = "amcc,ppc4xx-msi",
277 },
278 {}
279 };
280 static struct platform_driver ppc4xx_msi_driver = {
281 .probe = ppc4xx_msi_probe,
282 .remove = ppc4xx_of_msi_remove,
283 .driver = {
284 .name = "ppc4xx-msi",
285 .of_match_table = ppc4xx_msi_ids,
286 },
287
288 };
289
290 static __init int ppc4xx_msi_init(void)
291 {
292 return platform_driver_register(&ppc4xx_msi_driver);
293 }
294
295 subsys_initcall(ppc4xx_msi_init);