powerpc/MSI: Fix race condition in tearing down MSI interrupts
[pandora-kernel.git] / arch / powerpc / platforms / powernv / pci.c
1 /*
2  * Support PCI/PCIe on PowerNV platforms
3  *
4  * Currently supports only P5IOC2
5  *
6  * Copyright 2011 Benjamin Herrenschmidt, IBM Corp.
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
10  * as published by the Free Software Foundation; either version
11  * 2 of the License, or (at your option) any later version.
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/pci.h>
16 #include <linux/delay.h>
17 #include <linux/string.h>
18 #include <linux/init.h>
19 #include <linux/bootmem.h>
20 #include <linux/irq.h>
21 #include <linux/io.h>
22 #include <linux/msi.h>
23
24 #include <asm/sections.h>
25 #include <asm/io.h>
26 #include <asm/prom.h>
27 #include <asm/pci-bridge.h>
28 #include <asm/machdep.h>
29 #include <asm/ppc-pci.h>
30 #include <asm/opal.h>
31 #include <asm/iommu.h>
32 #include <asm/tce.h>
33 #include <asm/abs_addr.h>
34
35 #include "powernv.h"
36 #include "pci.h"
37
38 /* Delay in usec */
39 #define PCI_RESET_DELAY_US      3000000
40
41 #define cfg_dbg(fmt...) do { } while(0)
42 //#define cfg_dbg(fmt...)       printk(fmt)
43
44 #ifdef CONFIG_PCI_MSI
45 static int pnv_msi_check_device(struct pci_dev* pdev, int nvec, int type)
46 {
47         struct pci_controller *hose = pci_bus_to_host(pdev->bus);
48         struct pnv_phb *phb = hose->private_data;
49
50         return (phb && phb->msi_map) ? 0 : -ENODEV;
51 }
52
53 static unsigned int pnv_get_one_msi(struct pnv_phb *phb)
54 {
55         unsigned int id;
56
57         spin_lock(&phb->lock);
58         id = find_next_zero_bit(phb->msi_map, phb->msi_count, phb->msi_next);
59         if (id >= phb->msi_count && phb->msi_next)
60                 id = find_next_zero_bit(phb->msi_map, phb->msi_count, 0);
61         if (id >= phb->msi_count) {
62                 spin_unlock(&phb->lock);
63                 return 0;
64         }
65         __set_bit(id, phb->msi_map);
66         spin_unlock(&phb->lock);
67         return id + phb->msi_base;
68 }
69
70 static void pnv_put_msi(struct pnv_phb *phb, unsigned int hwirq)
71 {
72         unsigned int id;
73
74         if (WARN_ON(hwirq < phb->msi_base ||
75                     hwirq >= (phb->msi_base + phb->msi_count)))
76                 return;
77         id = hwirq - phb->msi_base;
78         spin_lock(&phb->lock);
79         __clear_bit(id, phb->msi_map);
80         spin_unlock(&phb->lock);
81 }
82
83 static int pnv_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
84 {
85         struct pci_controller *hose = pci_bus_to_host(pdev->bus);
86         struct pnv_phb *phb = hose->private_data;
87         struct msi_desc *entry;
88         struct msi_msg msg;
89         unsigned int hwirq, virq;
90         int rc;
91
92         if (WARN_ON(!phb))
93                 return -ENODEV;
94
95         list_for_each_entry(entry, &pdev->msi_list, list) {
96                 if (!entry->msi_attrib.is_64 && !phb->msi32_support) {
97                         pr_warn("%s: Supports only 64-bit MSIs\n",
98                                 pci_name(pdev));
99                         return -ENXIO;
100                 }
101                 hwirq = pnv_get_one_msi(phb);
102                 if (!hwirq) {
103                         pr_warn("%s: Failed to find a free MSI\n",
104                                 pci_name(pdev));
105                         return -ENOSPC;
106                 }
107                 virq = irq_create_mapping(NULL, hwirq);
108                 if (virq == NO_IRQ) {
109                         pr_warn("%s: Failed to map MSI to linux irq\n",
110                                 pci_name(pdev));
111                         pnv_put_msi(phb, hwirq);
112                         return -ENOMEM;
113                 }
114                 rc = phb->msi_setup(phb, pdev, hwirq, entry->msi_attrib.is_64,
115                                     &msg);
116                 if (rc) {
117                         pr_warn("%s: Failed to setup MSI\n", pci_name(pdev));
118                         irq_dispose_mapping(virq);
119                         pnv_put_msi(phb, hwirq);
120                         return rc;
121                 }
122                 irq_set_msi_desc(virq, entry);
123                 write_msi_msg(virq, &msg);
124         }
125         return 0;
126 }
127
128 static void pnv_teardown_msi_irqs(struct pci_dev *pdev)
129 {
130         struct pci_controller *hose = pci_bus_to_host(pdev->bus);
131         struct pnv_phb *phb = hose->private_data;
132         struct msi_desc *entry;
133         irq_hw_number_t hwirq;
134
135         if (WARN_ON(!phb))
136                 return;
137
138         list_for_each_entry(entry, &pdev->msi_list, list) {
139                 if (entry->irq == NO_IRQ)
140                         continue;
141                 hwirq = virq_to_hw(entry->irq);
142                 irq_set_msi_desc(entry->irq, NULL);
143                 irq_dispose_mapping(entry->irq);
144                 pnv_put_msi(phb, hwirq);
145         }
146 }
147 #endif /* CONFIG_PCI_MSI */
148
149 static void pnv_pci_config_check_eeh(struct pnv_phb *phb, struct pci_bus *bus,
150                                      u32 bdfn)
151 {
152         s64     rc;
153         u8      fstate;
154         u16     pcierr;
155         u32     pe_no;
156
157         /* Get PE# if we support IODA */
158         pe_no = phb->bdfn_to_pe ? phb->bdfn_to_pe(phb, bus, bdfn & 0xff) : 0;
159
160         /* Read freeze status */
161         rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no, &fstate, &pcierr,
162                                         NULL);
163         if (rc) {
164                 pr_warning("PCI %d: Failed to read EEH status for PE#%d,"
165                            " err %lld\n", phb->hose->global_number, pe_no, rc);
166                 return;
167         }
168         cfg_dbg(" -> EEH check, bdfn=%04x PE%d fstate=%x\n",
169                 bdfn, pe_no, fstate);
170         if (fstate != 0) {
171                 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no,
172                                               OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
173                 if (rc) {
174                         pr_warning("PCI %d: Failed to clear EEH freeze state"
175                                    " for PE#%d, err %lld\n",
176                                    phb->hose->global_number, pe_no, rc);
177                 }
178         }
179 }
180
181 static int pnv_pci_read_config(struct pci_bus *bus,
182                                unsigned int devfn,
183                                int where, int size, u32 *val)
184 {
185         struct pci_controller *hose = pci_bus_to_host(bus);
186         struct pnv_phb *phb = hose->private_data;
187         u32 bdfn = (((uint64_t)bus->number) << 8) | devfn;
188         s64 rc;
189
190         if (hose == NULL)
191                 return PCIBIOS_DEVICE_NOT_FOUND;
192
193         switch (size) {
194         case 1: {
195                 u8 v8;
196                 rc = opal_pci_config_read_byte(phb->opal_id, bdfn, where, &v8);
197                 *val = (rc == OPAL_SUCCESS) ? v8 : 0xff;
198                 break;
199         }
200         case 2: {
201                 u16 v16;
202                 rc = opal_pci_config_read_half_word(phb->opal_id, bdfn, where,
203                                                    &v16);
204                 *val = (rc == OPAL_SUCCESS) ? v16 : 0xffff;
205                 break;
206         }
207         case 4: {
208                 u32 v32;
209                 rc = opal_pci_config_read_word(phb->opal_id, bdfn, where, &v32);
210                 *val = (rc == OPAL_SUCCESS) ? v32 : 0xffffffff;
211                 break;
212         }
213         default:
214                 return PCIBIOS_FUNC_NOT_SUPPORTED;
215         }
216         cfg_dbg("pnv_pci_read_config bus: %x devfn: %x +%x/%x -> %08x\n",
217                 bus->number, devfn, where, size, *val);
218
219         /* Check if the PHB got frozen due to an error (no response) */
220         pnv_pci_config_check_eeh(phb, bus, bdfn);
221
222         return PCIBIOS_SUCCESSFUL;
223 }
224
225 static int pnv_pci_write_config(struct pci_bus *bus,
226                                 unsigned int devfn,
227                                 int where, int size, u32 val)
228 {
229         struct pci_controller *hose = pci_bus_to_host(bus);
230         struct pnv_phb *phb = hose->private_data;
231         u32 bdfn = (((uint64_t)bus->number) << 8) | devfn;
232
233         if (hose == NULL)
234                 return PCIBIOS_DEVICE_NOT_FOUND;
235
236         cfg_dbg("pnv_pci_write_config bus: %x devfn: %x +%x/%x -> %08x\n",
237                 bus->number, devfn, where, size, val);
238         switch (size) {
239         case 1:
240                 opal_pci_config_write_byte(phb->opal_id, bdfn, where, val);
241                 break;
242         case 2:
243                 opal_pci_config_write_half_word(phb->opal_id, bdfn, where, val);
244                 break;
245         case 4:
246                 opal_pci_config_write_word(phb->opal_id, bdfn, where, val);
247                 break;
248         default:
249                 return PCIBIOS_FUNC_NOT_SUPPORTED;
250         }
251         /* Check if the PHB got frozen due to an error (no response) */
252         pnv_pci_config_check_eeh(phb, bus, bdfn);
253
254         return PCIBIOS_SUCCESSFUL;
255 }
256
257 struct pci_ops pnv_pci_ops = {
258         .read = pnv_pci_read_config,
259         .write = pnv_pci_write_config,
260 };
261
262 static int pnv_tce_build(struct iommu_table *tbl, long index, long npages,
263                          unsigned long uaddr, enum dma_data_direction direction,
264                          struct dma_attrs *attrs)
265 {
266         u64 proto_tce;
267         u64 *tcep;
268         u64 rpn;
269
270         proto_tce = TCE_PCI_READ; // Read allowed
271
272         if (direction != DMA_TO_DEVICE)
273                 proto_tce |= TCE_PCI_WRITE;
274
275         tcep = ((u64 *)tbl->it_base) + index;
276
277         while (npages--) {
278                 /* can't move this out since we might cross LMB boundary */
279                 rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
280                 *tcep = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
281
282                 uaddr += TCE_PAGE_SIZE;
283                 tcep++;
284         }
285         return 0;
286 }
287
288 static void pnv_tce_free(struct iommu_table *tbl, long index, long npages)
289 {
290         u64 *tcep = ((u64 *)tbl->it_base) + index;
291
292         while (npages--)
293                 *(tcep++) = 0;
294 }
295
296 void pnv_pci_setup_iommu_table(struct iommu_table *tbl,
297                                void *tce_mem, u64 tce_size,
298                                u64 dma_offset)
299 {
300         tbl->it_blocksize = 16;
301         tbl->it_base = (unsigned long)tce_mem;
302         tbl->it_offset = dma_offset >> IOMMU_PAGE_SHIFT;
303         tbl->it_index = 0;
304         tbl->it_size = tce_size >> 3;
305         tbl->it_busno = 0;
306         tbl->it_type = TCE_PCI;
307 }
308
309 static struct iommu_table * __devinit
310 pnv_pci_setup_bml_iommu(struct pci_controller *hose)
311 {
312         struct iommu_table *tbl;
313         const __be64 *basep;
314         const __be32 *sizep;
315
316         basep = of_get_property(hose->dn, "linux,tce-base", NULL);
317         sizep = of_get_property(hose->dn, "linux,tce-size", NULL);
318         if (basep == NULL || sizep == NULL) {
319                 pr_err("PCI: %s has missing tce entries !\n", hose->dn->full_name);
320                 return NULL;
321         }
322         tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, hose->node);
323         if (WARN_ON(!tbl))
324                 return NULL;
325         pnv_pci_setup_iommu_table(tbl, __va(be64_to_cpup(basep)),
326                                   be32_to_cpup(sizep), 0);
327         iommu_init_table(tbl, hose->node);
328         return tbl;
329 }
330
331 static void __devinit pnv_pci_dma_fallback_setup(struct pci_controller *hose,
332                                                  struct pci_dev *pdev)
333 {
334         struct device_node *np = pci_bus_to_OF_node(hose->bus);
335         struct pci_dn *pdn;
336
337         if (np == NULL)
338                 return;
339         pdn = PCI_DN(np);
340         if (!pdn->iommu_table)
341                 pdn->iommu_table = pnv_pci_setup_bml_iommu(hose);
342         if (!pdn->iommu_table)
343                 return;
344         set_iommu_table_base(&pdev->dev, pdn->iommu_table);
345 }
346
347 static void __devinit pnv_pci_dma_dev_setup(struct pci_dev *pdev)
348 {
349         struct pci_controller *hose = pci_bus_to_host(pdev->bus);
350         struct pnv_phb *phb = hose->private_data;
351
352         /* If we have no phb structure, try to setup a fallback based on
353          * the device-tree (RTAS PCI for example)
354          */
355         if (phb && phb->dma_dev_setup)
356                 phb->dma_dev_setup(phb, pdev);
357         else
358                 pnv_pci_dma_fallback_setup(hose, pdev);
359 }
360
361 static int pnv_pci_probe_mode(struct pci_bus *bus)
362 {
363         struct pci_controller *hose = pci_bus_to_host(bus);
364         const __be64 *tstamp;
365         u64 now, target;
366
367
368         /* We hijack this as a way to ensure we have waited long
369          * enough since the reset was lifted on the PCI bus
370          */
371         if (bus != hose->bus)
372                 return PCI_PROBE_NORMAL;
373         tstamp = of_get_property(hose->dn, "reset-clear-timestamp", NULL);
374         if (!tstamp || !*tstamp)
375                 return PCI_PROBE_NORMAL;
376
377         now = mftb() / tb_ticks_per_usec;
378         target = (be64_to_cpup(tstamp) / tb_ticks_per_usec)
379                 + PCI_RESET_DELAY_US;
380
381         pr_devel("pci %04d: Reset target: 0x%llx now: 0x%llx\n",
382                  hose->global_number, target, now);
383
384         if (now < target)
385                 msleep((target - now + 999) / 1000);
386
387         return PCI_PROBE_NORMAL;
388 }
389
390 void __init pnv_pci_init(void)
391 {
392         struct device_node *np;
393
394         pci_set_flags(PCI_CAN_SKIP_ISA_ALIGN);
395
396         /* We do not want to just probe */
397         pci_probe_only = 0;
398
399         /* OPAL absent, try POPAL first then RTAS detection of PHBs */
400         if (!firmware_has_feature(FW_FEATURE_OPAL)) {
401 #ifdef CONFIG_PPC_POWERNV_RTAS
402                 init_pci_config_tokens();
403                 find_and_init_phbs();
404 #endif /* CONFIG_PPC_POWERNV_RTAS */
405         } else {
406                 /* OPAL is here, do our normal stuff */
407
408                 /* Look for p5ioc2 IO-Hubs */
409                 for_each_compatible_node(np, NULL, "ibm,p5ioc2")
410                         pnv_pci_init_p5ioc2_hub(np);
411         }
412
413         /* Setup the linkage between OF nodes and PHBs */
414         pci_devs_phb_init();
415
416         /* Configure IOMMU DMA hooks */
417         ppc_md.pci_dma_dev_setup = pnv_pci_dma_dev_setup;
418         ppc_md.tce_build = pnv_tce_build;
419         ppc_md.tce_free = pnv_tce_free;
420         ppc_md.pci_probe_mode = pnv_pci_probe_mode;
421         set_pci_dma_ops(&dma_iommu_ops);
422
423         /* Configure MSIs */
424 #ifdef CONFIG_PCI_MSI
425         ppc_md.msi_check_device = pnv_msi_check_device;
426         ppc_md.setup_msi_irqs = pnv_setup_msi_irqs;
427         ppc_md.teardown_msi_irqs = pnv_teardown_msi_irqs;
428 #endif
429 }