blob: bdd97bb2446d283c7a7cb68c16dd73ef40b96ebd [file] [log] [blame]
Michael Ellermance21b3c2007-07-20 21:39:28 +02001/*
2 * Copyright 2007, Michael Ellerman, IBM Corporation.
3 *
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.
8 */
9
10
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>
17
18#include <asm/dcr.h>
19#include <asm/machdep.h>
20#include <asm/prom.h>
21
22
23/*
24 * MSIC registers, specified as offsets from dcr_base
25 */
26#define MSIC_CTRL_REG 0x0
27
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
31
32/* Hold the read/write offsets into the FIFO */
33#define MSIC_READ_OFFSET_REG 0x5
34#define MSIC_WRITE_OFFSET_REG 0x6
35
36
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
42
43/*
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.
46 */
47#define MSIC_FIFO_SIZE_SHIFT 16
48#define MSIC_FIFO_SIZE_BYTES (1 << MSIC_FIFO_SIZE_SHIFT)
49
50/*
51 * To configure the FIFO size as (1 << n) bytes, we write (n - 15) into bits
52 * 8-9 of the MSIC control reg.
53 */
54#define MSIC_CTRL_FIFO_SIZE (((MSIC_FIFO_SIZE_SHIFT - 15) << 8) & 0x300)
55
56/*
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.
59 */
60#define MSIC_FIFO_SIZE_MASK ((MSIC_FIFO_SIZE_BYTES - 1) & ~0xFu)
61
62/* Each entry in the FIFO is 16 bytes, the first 4 bytes hold the irq # */
63#define MSIC_FIFO_ENTRY_SIZE 0x10
64
65
66struct axon_msic {
Michael Ellermance21b3c2007-07-20 21:39:28 +020067 struct irq_host *irq_host;
68 __le32 *fifo;
69 dcr_host_t dcr_host;
70 struct list_head list;
71 u32 read_offset;
72 u32 dcr_base;
73};
74
75static LIST_HEAD(axon_msic_list);
76
77static void msic_dcr_write(struct axon_msic *msic, unsigned int dcr_n, u32 val)
78{
79 pr_debug("axon_msi: dcr_write(0x%x, 0x%x)\n", val, dcr_n);
80
81 dcr_write(msic->dcr_host, msic->dcr_base + dcr_n, val);
82}
83
84static u32 msic_dcr_read(struct axon_msic *msic, unsigned int dcr_n)
85{
86 return dcr_read(msic->dcr_host, msic->dcr_base + dcr_n);
87}
88
89static void axon_msi_cascade(unsigned int irq, struct irq_desc *desc)
90{
91 struct axon_msic *msic = get_irq_data(irq);
92 u32 write_offset, msi;
93 int idx;
94
95 write_offset = msic_dcr_read(msic, MSIC_WRITE_OFFSET_REG);
96 pr_debug("axon_msi: original write_offset 0x%x\n", write_offset);
97
98 /* write_offset doesn't wrap properly, so we have to mask it */
99 write_offset &= MSIC_FIFO_SIZE_MASK;
100
101 while (msic->read_offset != write_offset) {
102 idx = msic->read_offset / sizeof(__le32);
103 msi = le32_to_cpu(msic->fifo[idx]);
104 msi &= 0xFFFF;
105
106 pr_debug("axon_msi: woff %x roff %x msi %x\n",
107 write_offset, msic->read_offset, msi);
108
109 msic->read_offset += MSIC_FIFO_ENTRY_SIZE;
110 msic->read_offset &= MSIC_FIFO_SIZE_MASK;
111
112 if (msi < NR_IRQS && irq_map[msi].host == msic->irq_host)
113 generic_handle_irq(msi);
114 else
115 pr_debug("axon_msi: invalid irq 0x%x!\n", msi);
116 }
117
118 desc->chip->eoi(irq);
119}
120
121static struct axon_msic *find_msi_translator(struct pci_dev *dev)
122{
123 struct irq_host *irq_host;
124 struct device_node *dn, *tmp;
125 const phandle *ph;
126 struct axon_msic *msic = NULL;
127
128 dn = pci_device_to_OF_node(dev);
129 if (!dn) {
130 dev_dbg(&dev->dev, "axon_msi: no pci_dn found\n");
131 return NULL;
132 }
133
134 for (; dn; tmp = of_get_parent(dn), of_node_put(dn), dn = tmp) {
135 ph = of_get_property(dn, "msi-translator", NULL);
136 if (ph)
137 break;
138 }
139
140 if (!ph) {
141 dev_dbg(&dev->dev,
142 "axon_msi: no msi-translator property found\n");
143 goto out_error;
144 }
145
146 tmp = dn;
147 dn = of_find_node_by_phandle(*ph);
148 if (!dn) {
149 dev_dbg(&dev->dev,
150 "axon_msi: msi-translator doesn't point to a node\n");
151 goto out_error;
152 }
153
154 irq_host = irq_find_host(dn);
155 if (!irq_host) {
156 dev_dbg(&dev->dev, "axon_msi: no irq_host found for node %s\n",
157 dn->full_name);
158 goto out_error;
159 }
160
161 msic = irq_host->host_data;
162
163out_error:
164 of_node_put(dn);
165 of_node_put(tmp);
166
167 return msic;
168}
169
170static int axon_msi_check_device(struct pci_dev *dev, int nvec, int type)
171{
172 if (!find_msi_translator(dev))
173 return -ENODEV;
174
175 return 0;
176}
177
178static int setup_msi_msg_address(struct pci_dev *dev, struct msi_msg *msg)
179{
180 struct device_node *dn, *tmp;
181 struct msi_desc *entry;
182 int len;
183 const u32 *prop;
184
185 dn = pci_device_to_OF_node(dev);
186 if (!dn) {
187 dev_dbg(&dev->dev, "axon_msi: no pci_dn found\n");
188 return -ENODEV;
189 }
190
191 entry = list_first_entry(&dev->msi_list, struct msi_desc, list);
192
193 for (; dn; tmp = of_get_parent(dn), of_node_put(dn), dn = tmp) {
194 if (entry->msi_attrib.is_64) {
195 prop = of_get_property(dn, "msi-address-64", &len);
196 if (prop)
197 break;
198 }
199
200 prop = of_get_property(dn, "msi-address-32", &len);
201 if (prop)
202 break;
203 }
204
205 if (!prop) {
206 dev_dbg(&dev->dev,
207 "axon_msi: no msi-address-(32|64) properties found\n");
208 return -ENOENT;
209 }
210
211 switch (len) {
212 case 8:
213 msg->address_hi = prop[0];
214 msg->address_lo = prop[1];
215 break;
216 case 4:
217 msg->address_hi = 0;
218 msg->address_lo = prop[0];
219 break;
220 default:
221 dev_dbg(&dev->dev,
222 "axon_msi: malformed msi-address-(32|64) property\n");
223 of_node_put(dn);
224 return -EINVAL;
225 }
226
227 of_node_put(dn);
228
229 return 0;
230}
231
232static int axon_msi_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
233{
234 unsigned int virq, rc;
235 struct msi_desc *entry;
236 struct msi_msg msg;
237 struct axon_msic *msic;
238
239 msic = find_msi_translator(dev);
240 if (!msic)
241 return -ENODEV;
242
243 rc = setup_msi_msg_address(dev, &msg);
244 if (rc)
245 return rc;
246
247 /* We rely on being able to stash a virq in a u16 */
248 BUILD_BUG_ON(NR_IRQS > 65536);
249
250 list_for_each_entry(entry, &dev->msi_list, list) {
251 virq = irq_create_direct_mapping(msic->irq_host);
252 if (virq == NO_IRQ) {
253 dev_warn(&dev->dev,
254 "axon_msi: virq allocation failed!\n");
255 return -1;
256 }
257 dev_dbg(&dev->dev, "axon_msi: allocated virq 0x%x\n", virq);
258
259 set_irq_msi(virq, entry);
260 msg.data = virq;
261 write_msi_msg(virq, &msg);
262 }
263
264 return 0;
265}
266
267static void axon_msi_teardown_msi_irqs(struct pci_dev *dev)
268{
269 struct msi_desc *entry;
270
271 dev_dbg(&dev->dev, "axon_msi: tearing down msi irqs\n");
272
273 list_for_each_entry(entry, &dev->msi_list, list) {
274 if (entry->irq == NO_IRQ)
275 continue;
276
277 set_irq_msi(entry->irq, NULL);
278 irq_dispose_mapping(entry->irq);
279 }
280}
281
282static struct irq_chip msic_irq_chip = {
283 .mask = mask_msi_irq,
284 .unmask = unmask_msi_irq,
285 .shutdown = unmask_msi_irq,
286 .typename = "AXON-MSI",
287};
288
289static int msic_host_map(struct irq_host *h, unsigned int virq,
290 irq_hw_number_t hw)
291{
292 set_irq_chip_and_handler(virq, &msic_irq_chip, handle_simple_irq);
293
294 return 0;
295}
296
297static int msic_host_match(struct irq_host *host, struct device_node *dn)
298{
Michael Ellerman52964f82007-08-28 18:47:54 +1000299 return host->of_node == dn;
Michael Ellermance21b3c2007-07-20 21:39:28 +0200300}
301
302static struct irq_host_ops msic_host_ops = {
303 .match = msic_host_match,
304 .map = msic_host_map,
305};
306
307static int axon_msi_notify_reboot(struct notifier_block *nb,
308 unsigned long code, void *data)
309{
310 struct axon_msic *msic;
311 u32 tmp;
312
313 list_for_each_entry(msic, &axon_msic_list, list) {
Michael Ellerman52964f82007-08-28 18:47:54 +1000314 pr_debug("axon_msi: disabling %s\n",
315 msic->irq_host->of_node->full_name);
Michael Ellermance21b3c2007-07-20 21:39:28 +0200316 tmp = msic_dcr_read(msic, MSIC_CTRL_REG);
317 tmp &= ~MSIC_CTRL_ENABLE & ~MSIC_CTRL_IRQ_ENABLE;
318 msic_dcr_write(msic, MSIC_CTRL_REG, tmp);
319 }
320
321 return 0;
322}
323
324static struct notifier_block axon_msi_reboot_notifier = {
325 .notifier_call = axon_msi_notify_reboot
326};
327
328static int axon_msi_setup_one(struct device_node *dn)
329{
330 struct page *page;
331 struct axon_msic *msic;
332 unsigned int virq;
333 int dcr_len;
334
335 pr_debug("axon_msi: setting up dn %s\n", dn->full_name);
336
337 msic = kzalloc(sizeof(struct axon_msic), GFP_KERNEL);
338 if (!msic) {
339 printk(KERN_ERR "axon_msi: couldn't allocate msic for %s\n",
340 dn->full_name);
341 goto out;
342 }
343
344 msic->dcr_base = dcr_resource_start(dn, 0);
345 dcr_len = dcr_resource_len(dn, 0);
346
347 if (msic->dcr_base == 0 || dcr_len == 0) {
348 printk(KERN_ERR
349 "axon_msi: couldn't parse dcr properties on %s\n",
350 dn->full_name);
351 goto out;
352 }
353
354 msic->dcr_host = dcr_map(dn, msic->dcr_base, dcr_len);
355 if (!DCR_MAP_OK(msic->dcr_host)) {
356 printk(KERN_ERR "axon_msi: dcr_map failed for %s\n",
357 dn->full_name);
358 goto out_free_msic;
359 }
360
361 page = alloc_pages_node(of_node_to_nid(dn), GFP_KERNEL,
362 get_order(MSIC_FIFO_SIZE_BYTES));
363 if (!page) {
364 printk(KERN_ERR "axon_msi: couldn't allocate fifo for %s\n",
365 dn->full_name);
366 goto out_free_msic;
367 }
368
369 msic->fifo = page_address(page);
370
Michael Ellerman52964f82007-08-28 18:47:54 +1000371 msic->irq_host = irq_alloc_host(of_node_get(dn), IRQ_HOST_MAP_NOMAP,
372 NR_IRQS, &msic_host_ops, 0);
Michael Ellermance21b3c2007-07-20 21:39:28 +0200373 if (!msic->irq_host) {
374 printk(KERN_ERR "axon_msi: couldn't allocate irq_host for %s\n",
375 dn->full_name);
376 goto out_free_fifo;
377 }
378
379 msic->irq_host->host_data = msic;
380
381 virq = irq_of_parse_and_map(dn, 0);
382 if (virq == NO_IRQ) {
383 printk(KERN_ERR "axon_msi: irq parse and map failed for %s\n",
384 dn->full_name);
385 goto out_free_host;
386 }
387
Michael Ellermance21b3c2007-07-20 21:39:28 +0200388 set_irq_data(virq, msic);
389 set_irq_chained_handler(virq, axon_msi_cascade);
390 pr_debug("axon_msi: irq 0x%x setup for axon_msi\n", virq);
391
392 /* Enable the MSIC hardware */
393 msic_dcr_write(msic, MSIC_BASE_ADDR_HI_REG, (u64)msic->fifo >> 32);
394 msic_dcr_write(msic, MSIC_BASE_ADDR_LO_REG,
395 (u64)msic->fifo & 0xFFFFFFFF);
396 msic_dcr_write(msic, MSIC_CTRL_REG,
397 MSIC_CTRL_IRQ_ENABLE | MSIC_CTRL_ENABLE |
398 MSIC_CTRL_FIFO_SIZE);
399
400 list_add(&msic->list, &axon_msic_list);
401
402 printk(KERN_DEBUG "axon_msi: setup MSIC on %s\n", dn->full_name);
403
404 return 0;
405
406out_free_host:
407 kfree(msic->irq_host);
408out_free_fifo:
409 __free_pages(virt_to_page(msic->fifo), get_order(MSIC_FIFO_SIZE_BYTES));
410out_free_msic:
411 kfree(msic);
412out:
413
414 return -1;
415}
416
417static int axon_msi_init(void)
418{
419 struct device_node *dn;
420 int found = 0;
421
422 pr_debug("axon_msi: initialising ...\n");
423
424 for_each_compatible_node(dn, NULL, "ibm,axon-msic") {
425 if (axon_msi_setup_one(dn) == 0)
426 found++;
427 }
428
429 if (found) {
430 ppc_md.setup_msi_irqs = axon_msi_setup_msi_irqs;
431 ppc_md.teardown_msi_irqs = axon_msi_teardown_msi_irqs;
432 ppc_md.msi_check_device = axon_msi_check_device;
433
434 register_reboot_notifier(&axon_msi_reboot_notifier);
435
436 pr_debug("axon_msi: registered callbacks!\n");
437 }
438
439 return 0;
440}
441arch_initcall(axon_msi_init);