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