blob: e16b729f46f9c316c2afd344f57ea314bacca790 [file] [log] [blame]
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +00001/*
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>
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000022#include <linux/msi.h>
Alexey Kardashevskiy4e13c1a2013-05-21 13:33:09 +100023#include <linux/iommu.h>
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +000024
25#include <asm/sections.h>
26#include <asm/io.h>
27#include <asm/prom.h>
28#include <asm/pci-bridge.h>
29#include <asm/machdep.h>
Gavin Shanfb1b55d2013-03-05 21:12:37 +000030#include <asm/msi_bitmap.h>
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +000031#include <asm/ppc-pci.h>
32#include <asm/opal.h>
33#include <asm/iommu.h>
34#include <asm/tce.h>
Stephen Rothwellf5339272012-03-15 18:18:00 +000035#include <asm/firmware.h>
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +000036
37#include "powernv.h"
38#include "pci.h"
39
Benjamin Herrenschmidt82ba1292011-09-19 17:45:07 +000040/* Delay in usec */
41#define PCI_RESET_DELAY_US 3000000
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +000042
43#define cfg_dbg(fmt...) do { } while(0)
44//#define cfg_dbg(fmt...) printk(fmt)
45
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000046#ifdef CONFIG_PCI_MSI
47static int pnv_msi_check_device(struct pci_dev* pdev, int nvec, int type)
48{
49 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
50 struct pnv_phb *phb = hose->private_data;
Benjamin Herrenschmidtb72c1f62013-05-21 22:58:21 +000051 struct pci_dn *pdn = pci_get_pdn(pdev);
52
53 if (pdn && pdn->force_32bit_msi && !phb->msi32_support)
54 return -ENODEV;
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000055
Gavin Shanfb1b55d2013-03-05 21:12:37 +000056 return (phb && phb->msi_bmp.bitmap) ? 0 : -ENODEV;
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000057}
58
59static int pnv_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
60{
61 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
62 struct pnv_phb *phb = hose->private_data;
63 struct msi_desc *entry;
64 struct msi_msg msg;
Gavin Shanfb1b55d2013-03-05 21:12:37 +000065 int hwirq;
66 unsigned int virq;
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000067 int rc;
68
69 if (WARN_ON(!phb))
70 return -ENODEV;
71
72 list_for_each_entry(entry, &pdev->msi_list, list) {
73 if (!entry->msi_attrib.is_64 && !phb->msi32_support) {
74 pr_warn("%s: Supports only 64-bit MSIs\n",
75 pci_name(pdev));
76 return -ENXIO;
77 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +000078 hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, 1);
79 if (hwirq < 0) {
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000080 pr_warn("%s: Failed to find a free MSI\n",
81 pci_name(pdev));
82 return -ENOSPC;
83 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +000084 virq = irq_create_mapping(NULL, phb->msi_base + hwirq);
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000085 if (virq == NO_IRQ) {
86 pr_warn("%s: Failed to map MSI to linux irq\n",
87 pci_name(pdev));
Gavin Shanfb1b55d2013-03-05 21:12:37 +000088 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, 1);
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000089 return -ENOMEM;
90 }
Gavin Shanfb1b55d2013-03-05 21:12:37 +000091 rc = phb->msi_setup(phb, pdev, phb->msi_base + hwirq,
Gavin Shan137436c2013-04-25 19:20:59 +000092 virq, entry->msi_attrib.is_64, &msg);
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000093 if (rc) {
94 pr_warn("%s: Failed to setup MSI\n", pci_name(pdev));
95 irq_dispose_mapping(virq);
Gavin Shanfb1b55d2013-03-05 21:12:37 +000096 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, 1);
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000097 return rc;
98 }
99 irq_set_msi_desc(virq, entry);
100 write_msi_msg(virq, &msg);
101 }
102 return 0;
103}
104
105static void pnv_teardown_msi_irqs(struct pci_dev *pdev)
106{
107 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
108 struct pnv_phb *phb = hose->private_data;
109 struct msi_desc *entry;
110
111 if (WARN_ON(!phb))
112 return;
113
114 list_for_each_entry(entry, &pdev->msi_list, list) {
115 if (entry->irq == NO_IRQ)
116 continue;
117 irq_set_msi_desc(entry->irq, NULL);
Gavin Shanfb1b55d2013-03-05 21:12:37 +0000118 msi_bitmap_free_hwirqs(&phb->msi_bmp,
119 virq_to_hw(entry->irq) - phb->msi_base, 1);
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +0000120 irq_dispose_mapping(entry->irq);
121 }
122}
123#endif /* CONFIG_PCI_MSI */
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000124
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000125static void pnv_pci_dump_p7ioc_diag_data(struct pnv_phb *phb)
126{
127 struct OpalIoP7IOCPhbErrorData *data = &phb->diag.p7ioc;
128 int i;
129
130 pr_info("PHB %d diagnostic data:\n", phb->hose->global_number);
131
132 pr_info(" brdgCtl = 0x%08x\n", data->brdgCtl);
133
134 pr_info(" portStatusReg = 0x%08x\n", data->portStatusReg);
135 pr_info(" rootCmplxStatus = 0x%08x\n", data->rootCmplxStatus);
136 pr_info(" busAgentStatus = 0x%08x\n", data->busAgentStatus);
137
138 pr_info(" deviceStatus = 0x%08x\n", data->deviceStatus);
139 pr_info(" slotStatus = 0x%08x\n", data->slotStatus);
140 pr_info(" linkStatus = 0x%08x\n", data->linkStatus);
141 pr_info(" devCmdStatus = 0x%08x\n", data->devCmdStatus);
142 pr_info(" devSecStatus = 0x%08x\n", data->devSecStatus);
143
144 pr_info(" rootErrorStatus = 0x%08x\n", data->rootErrorStatus);
145 pr_info(" uncorrErrorStatus = 0x%08x\n", data->uncorrErrorStatus);
146 pr_info(" corrErrorStatus = 0x%08x\n", data->corrErrorStatus);
147 pr_info(" tlpHdr1 = 0x%08x\n", data->tlpHdr1);
148 pr_info(" tlpHdr2 = 0x%08x\n", data->tlpHdr2);
149 pr_info(" tlpHdr3 = 0x%08x\n", data->tlpHdr3);
150 pr_info(" tlpHdr4 = 0x%08x\n", data->tlpHdr4);
151 pr_info(" sourceId = 0x%08x\n", data->sourceId);
152
153 pr_info(" errorClass = 0x%016llx\n", data->errorClass);
154 pr_info(" correlator = 0x%016llx\n", data->correlator);
155
156 pr_info(" p7iocPlssr = 0x%016llx\n", data->p7iocPlssr);
157 pr_info(" p7iocCsr = 0x%016llx\n", data->p7iocCsr);
158 pr_info(" lemFir = 0x%016llx\n", data->lemFir);
159 pr_info(" lemErrorMask = 0x%016llx\n", data->lemErrorMask);
160 pr_info(" lemWOF = 0x%016llx\n", data->lemWOF);
161 pr_info(" phbErrorStatus = 0x%016llx\n", data->phbErrorStatus);
162 pr_info(" phbFirstErrorStatus = 0x%016llx\n", data->phbFirstErrorStatus);
163 pr_info(" phbErrorLog0 = 0x%016llx\n", data->phbErrorLog0);
164 pr_info(" phbErrorLog1 = 0x%016llx\n", data->phbErrorLog1);
165 pr_info(" mmioErrorStatus = 0x%016llx\n", data->mmioErrorStatus);
166 pr_info(" mmioFirstErrorStatus = 0x%016llx\n", data->mmioFirstErrorStatus);
167 pr_info(" mmioErrorLog0 = 0x%016llx\n", data->mmioErrorLog0);
168 pr_info(" mmioErrorLog1 = 0x%016llx\n", data->mmioErrorLog1);
169 pr_info(" dma0ErrorStatus = 0x%016llx\n", data->dma0ErrorStatus);
170 pr_info(" dma0FirstErrorStatus = 0x%016llx\n", data->dma0FirstErrorStatus);
171 pr_info(" dma0ErrorLog0 = 0x%016llx\n", data->dma0ErrorLog0);
172 pr_info(" dma0ErrorLog1 = 0x%016llx\n", data->dma0ErrorLog1);
173 pr_info(" dma1ErrorStatus = 0x%016llx\n", data->dma1ErrorStatus);
174 pr_info(" dma1FirstErrorStatus = 0x%016llx\n", data->dma1FirstErrorStatus);
175 pr_info(" dma1ErrorLog0 = 0x%016llx\n", data->dma1ErrorLog0);
176 pr_info(" dma1ErrorLog1 = 0x%016llx\n", data->dma1ErrorLog1);
177
178 for (i = 0; i < OPAL_P7IOC_NUM_PEST_REGS; i++) {
179 if ((data->pestA[i] >> 63) == 0 &&
180 (data->pestB[i] >> 63) == 0)
181 continue;
182 pr_info(" PE[%3d] PESTA = 0x%016llx\n", i, data->pestA[i]);
183 pr_info(" PESTB = 0x%016llx\n", data->pestB[i]);
184 }
185}
186
187static void pnv_pci_dump_phb_diag_data(struct pnv_phb *phb)
188{
189 switch(phb->model) {
190 case PNV_PHB_MODEL_P7IOC:
191 pnv_pci_dump_p7ioc_diag_data(phb);
192 break;
193 default:
194 pr_warning("PCI %d: Can't decode this PHB diag data\n",
195 phb->hose->global_number);
196 }
197}
198
199static void pnv_pci_handle_eeh_config(struct pnv_phb *phb, u32 pe_no)
200{
201 unsigned long flags, rc;
202 int has_diag;
203
204 spin_lock_irqsave(&phb->lock, flags);
205
206 rc = opal_pci_get_phb_diag_data(phb->opal_id, phb->diag.blob, PNV_PCI_DIAG_BUF_SIZE);
207 has_diag = (rc == OPAL_SUCCESS);
208
209 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no,
210 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
211 if (rc) {
212 pr_warning("PCI %d: Failed to clear EEH freeze state"
213 " for PE#%d, err %ld\n",
214 phb->hose->global_number, pe_no, rc);
215
216 /* For now, let's only display the diag buffer when we fail to clear
217 * the EEH status. We'll do more sensible things later when we have
218 * proper EEH support. We need to make sure we don't pollute ourselves
219 * with the normal errors generated when probing empty slots
220 */
221 if (has_diag)
222 pnv_pci_dump_phb_diag_data(phb);
223 else
224 pr_warning("PCI %d: No diag data available\n",
225 phb->hose->global_number);
226 }
227
228 spin_unlock_irqrestore(&phb->lock, flags);
229}
230
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000231static void pnv_pci_config_check_eeh(struct pnv_phb *phb, struct pci_bus *bus,
232 u32 bdfn)
233{
234 s64 rc;
235 u8 fstate;
236 u16 pcierr;
237 u32 pe_no;
238
239 /* Get PE# if we support IODA */
240 pe_no = phb->bdfn_to_pe ? phb->bdfn_to_pe(phb, bus, bdfn & 0xff) : 0;
241
242 /* Read freeze status */
243 rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no, &fstate, &pcierr,
244 NULL);
245 if (rc) {
246 pr_warning("PCI %d: Failed to read EEH status for PE#%d,"
247 " err %lld\n", phb->hose->global_number, pe_no, rc);
248 return;
249 }
250 cfg_dbg(" -> EEH check, bdfn=%04x PE%d fstate=%x\n",
251 bdfn, pe_no, fstate);
Benjamin Herrenschmidtcee72d52011-11-29 18:22:53 +0000252 if (fstate != 0)
253 pnv_pci_handle_eeh_config(phb, pe_no);
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000254}
255
256static int pnv_pci_read_config(struct pci_bus *bus,
257 unsigned int devfn,
258 int where, int size, u32 *val)
259{
260 struct pci_controller *hose = pci_bus_to_host(bus);
261 struct pnv_phb *phb = hose->private_data;
262 u32 bdfn = (((uint64_t)bus->number) << 8) | devfn;
263 s64 rc;
264
265 if (hose == NULL)
266 return PCIBIOS_DEVICE_NOT_FOUND;
267
268 switch (size) {
269 case 1: {
270 u8 v8;
271 rc = opal_pci_config_read_byte(phb->opal_id, bdfn, where, &v8);
272 *val = (rc == OPAL_SUCCESS) ? v8 : 0xff;
273 break;
274 }
275 case 2: {
276 u16 v16;
277 rc = opal_pci_config_read_half_word(phb->opal_id, bdfn, where,
278 &v16);
279 *val = (rc == OPAL_SUCCESS) ? v16 : 0xffff;
280 break;
281 }
282 case 4: {
283 u32 v32;
284 rc = opal_pci_config_read_word(phb->opal_id, bdfn, where, &v32);
285 *val = (rc == OPAL_SUCCESS) ? v32 : 0xffffffff;
286 break;
287 }
288 default:
289 return PCIBIOS_FUNC_NOT_SUPPORTED;
290 }
291 cfg_dbg("pnv_pci_read_config bus: %x devfn: %x +%x/%x -> %08x\n",
292 bus->number, devfn, where, size, *val);
293
294 /* Check if the PHB got frozen due to an error (no response) */
295 pnv_pci_config_check_eeh(phb, bus, bdfn);
296
297 return PCIBIOS_SUCCESSFUL;
298}
299
300static int pnv_pci_write_config(struct pci_bus *bus,
301 unsigned int devfn,
302 int where, int size, u32 val)
303{
304 struct pci_controller *hose = pci_bus_to_host(bus);
305 struct pnv_phb *phb = hose->private_data;
306 u32 bdfn = (((uint64_t)bus->number) << 8) | devfn;
307
308 if (hose == NULL)
309 return PCIBIOS_DEVICE_NOT_FOUND;
310
311 cfg_dbg("pnv_pci_write_config bus: %x devfn: %x +%x/%x -> %08x\n",
312 bus->number, devfn, where, size, val);
313 switch (size) {
314 case 1:
315 opal_pci_config_write_byte(phb->opal_id, bdfn, where, val);
316 break;
317 case 2:
318 opal_pci_config_write_half_word(phb->opal_id, bdfn, where, val);
319 break;
320 case 4:
321 opal_pci_config_write_word(phb->opal_id, bdfn, where, val);
322 break;
323 default:
324 return PCIBIOS_FUNC_NOT_SUPPORTED;
325 }
326 /* Check if the PHB got frozen due to an error (no response) */
327 pnv_pci_config_check_eeh(phb, bus, bdfn);
328
329 return PCIBIOS_SUCCESSFUL;
330}
331
332struct pci_ops pnv_pci_ops = {
333 .read = pnv_pci_read_config,
334 .write = pnv_pci_write_config,
335};
336
337static int pnv_tce_build(struct iommu_table *tbl, long index, long npages,
338 unsigned long uaddr, enum dma_data_direction direction,
339 struct dma_attrs *attrs)
340{
341 u64 proto_tce;
Benjamin Herrenschmidt1f1616e2011-11-06 18:55:59 +0000342 u64 *tcep, *tces;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000343 u64 rpn;
344
345 proto_tce = TCE_PCI_READ; // Read allowed
346
347 if (direction != DMA_TO_DEVICE)
348 proto_tce |= TCE_PCI_WRITE;
349
Benjamin Herrenschmidt1f1616e2011-11-06 18:55:59 +0000350 tces = tcep = ((u64 *)tbl->it_base) + index - tbl->it_offset;
351 rpn = __pa(uaddr) >> TCE_SHIFT;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000352
Benjamin Herrenschmidt1f1616e2011-11-06 18:55:59 +0000353 while (npages--)
354 *(tcep++) = proto_tce | (rpn++ << TCE_RPN_SHIFT);
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000355
Benjamin Herrenschmidt1f1616e2011-11-06 18:55:59 +0000356 /* Some implementations won't cache invalid TCEs and thus may not
357 * need that flush. We'll probably turn it_type into a bit mask
358 * of flags if that becomes the case
359 */
360 if (tbl->it_type & TCE_PCI_SWINV_CREATE)
Gavin Shan4cce9552013-04-25 19:21:00 +0000361 pnv_pci_ioda_tce_invalidate(tbl, tces, tcep - 1);
Benjamin Herrenschmidt1f1616e2011-11-06 18:55:59 +0000362
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000363 return 0;
364}
365
366static void pnv_tce_free(struct iommu_table *tbl, long index, long npages)
367{
Benjamin Herrenschmidt1f1616e2011-11-06 18:55:59 +0000368 u64 *tcep, *tces;
369
370 tces = tcep = ((u64 *)tbl->it_base) + index - tbl->it_offset;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000371
372 while (npages--)
373 *(tcep++) = 0;
Benjamin Herrenschmidt1f1616e2011-11-06 18:55:59 +0000374
Benjamin Herrenschmidt605e44d2013-05-20 17:25:15 +0000375 if (tbl->it_type & TCE_PCI_SWINV_FREE)
Gavin Shan4cce9552013-04-25 19:21:00 +0000376 pnv_pci_ioda_tce_invalidate(tbl, tces, tcep - 1);
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000377}
378
Alexey Kardashevskiy11f63d32012-09-04 15:19:35 +0000379static unsigned long pnv_tce_get(struct iommu_table *tbl, long index)
380{
381 return ((u64 *)tbl->it_base)[index - tbl->it_offset];
382}
383
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000384void pnv_pci_setup_iommu_table(struct iommu_table *tbl,
385 void *tce_mem, u64 tce_size,
386 u64 dma_offset)
387{
388 tbl->it_blocksize = 16;
389 tbl->it_base = (unsigned long)tce_mem;
390 tbl->it_offset = dma_offset >> IOMMU_PAGE_SHIFT;
391 tbl->it_index = 0;
392 tbl->it_size = tce_size >> 3;
393 tbl->it_busno = 0;
394 tbl->it_type = TCE_PCI;
395}
396
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800397static struct iommu_table *pnv_pci_setup_bml_iommu(struct pci_controller *hose)
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000398{
399 struct iommu_table *tbl;
Benjamin Herrenschmidt1f1616e2011-11-06 18:55:59 +0000400 const __be64 *basep, *swinvp;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000401 const __be32 *sizep;
402
403 basep = of_get_property(hose->dn, "linux,tce-base", NULL);
404 sizep = of_get_property(hose->dn, "linux,tce-size", NULL);
405 if (basep == NULL || sizep == NULL) {
Benjamin Herrenschmidt1f1616e2011-11-06 18:55:59 +0000406 pr_err("PCI: %s has missing tce entries !\n",
407 hose->dn->full_name);
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000408 return NULL;
409 }
410 tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, hose->node);
411 if (WARN_ON(!tbl))
412 return NULL;
413 pnv_pci_setup_iommu_table(tbl, __va(be64_to_cpup(basep)),
414 be32_to_cpup(sizep), 0);
415 iommu_init_table(tbl, hose->node);
Alexey Kardashevskiy4e13c1a2013-05-21 13:33:09 +1000416 iommu_register_group(tbl, pci_domain_nr(hose->bus), 0);
Benjamin Herrenschmidt1f1616e2011-11-06 18:55:59 +0000417
418 /* Deal with SW invalidated TCEs when needed (BML way) */
419 swinvp = of_get_property(hose->dn, "linux,tce-sw-invalidate-info",
420 NULL);
421 if (swinvp) {
422 tbl->it_busno = swinvp[1];
423 tbl->it_index = (unsigned long)ioremap(swinvp[0], 8);
424 tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE;
425 }
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000426 return tbl;
427}
428
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800429static void pnv_pci_dma_fallback_setup(struct pci_controller *hose,
430 struct pci_dev *pdev)
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000431{
432 struct device_node *np = pci_bus_to_OF_node(hose->bus);
433 struct pci_dn *pdn;
434
435 if (np == NULL)
436 return;
437 pdn = PCI_DN(np);
438 if (!pdn->iommu_table)
439 pdn->iommu_table = pnv_pci_setup_bml_iommu(hose);
440 if (!pdn->iommu_table)
441 return;
442 set_iommu_table_base(&pdev->dev, pdn->iommu_table);
443}
444
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800445static void pnv_pci_dma_dev_setup(struct pci_dev *pdev)
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000446{
447 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
448 struct pnv_phb *phb = hose->private_data;
449
450 /* If we have no phb structure, try to setup a fallback based on
451 * the device-tree (RTAS PCI for example)
452 */
453 if (phb && phb->dma_dev_setup)
454 phb->dma_dev_setup(phb, pdev);
455 else
456 pnv_pci_dma_fallback_setup(hose, pdev);
457}
458
Benjamin Herrenschmidt73ed1482013-05-10 16:59:18 +1000459void pnv_pci_shutdown(void)
460{
461 struct pci_controller *hose;
462
463 list_for_each_entry(hose, &hose_list, list_node) {
464 struct pnv_phb *phb = hose->private_data;
465
466 if (phb && phb->shutdown)
467 phb->shutdown(phb);
468 }
469}
470
Gavin Shanaa0c0332013-04-25 19:20:57 +0000471/* Fixup wrong class code in p7ioc and p8 root complex */
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -0800472static void pnv_p7ioc_rc_quirk(struct pci_dev *dev)
Benjamin Herrenschmidtca45cfe2011-11-06 18:56:00 +0000473{
474 dev->class = PCI_CLASS_BRIDGE_PCI << 8;
475}
476DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_IBM, 0x3b9, pnv_p7ioc_rc_quirk);
477
Benjamin Herrenschmidt82ba1292011-09-19 17:45:07 +0000478static int pnv_pci_probe_mode(struct pci_bus *bus)
479{
480 struct pci_controller *hose = pci_bus_to_host(bus);
481 const __be64 *tstamp;
482 u64 now, target;
483
484
485 /* We hijack this as a way to ensure we have waited long
486 * enough since the reset was lifted on the PCI bus
487 */
488 if (bus != hose->bus)
489 return PCI_PROBE_NORMAL;
490 tstamp = of_get_property(hose->dn, "reset-clear-timestamp", NULL);
491 if (!tstamp || !*tstamp)
492 return PCI_PROBE_NORMAL;
493
494 now = mftb() / tb_ticks_per_usec;
495 target = (be64_to_cpup(tstamp) / tb_ticks_per_usec)
496 + PCI_RESET_DELAY_US;
497
498 pr_devel("pci %04d: Reset target: 0x%llx now: 0x%llx\n",
499 hose->global_number, target, now);
500
501 if (now < target)
502 msleep((target - now + 999) / 1000);
503
504 return PCI_PROBE_NORMAL;
505}
506
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000507void __init pnv_pci_init(void)
508{
509 struct device_node *np;
510
Bjorn Helgaas673c9752012-02-23 20:18:58 -0700511 pci_add_flags(PCI_CAN_SKIP_ISA_ALIGN);
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000512
513 /* OPAL absent, try POPAL first then RTAS detection of PHBs */
514 if (!firmware_has_feature(FW_FEATURE_OPAL)) {
515#ifdef CONFIG_PPC_POWERNV_RTAS
516 init_pci_config_tokens();
517 find_and_init_phbs();
518#endif /* CONFIG_PPC_POWERNV_RTAS */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000519 }
520 /* OPAL is here, do our normal stuff */
521 else {
522 int found_ioda = 0;
523
524 /* Look for IODA IO-Hubs. We don't support mixing IODA
525 * and p5ioc2 due to the need to change some global
526 * probing flags
527 */
528 for_each_compatible_node(np, NULL, "ibm,ioda-hub") {
529 pnv_pci_init_ioda_hub(np);
530 found_ioda = 1;
531 }
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000532
533 /* Look for p5ioc2 IO-Hubs */
Benjamin Herrenschmidt184cd4a2011-11-15 17:29:08 +0000534 if (!found_ioda)
535 for_each_compatible_node(np, NULL, "ibm,p5ioc2")
536 pnv_pci_init_p5ioc2_hub(np);
Gavin Shanaa0c0332013-04-25 19:20:57 +0000537
538 /* Look for ioda2 built-in PHB3's */
539 for_each_compatible_node(np, NULL, "ibm,ioda2-phb")
540 pnv_pci_init_ioda2_phb(np);
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000541 }
542
543 /* Setup the linkage between OF nodes and PHBs */
544 pci_devs_phb_init();
545
546 /* Configure IOMMU DMA hooks */
547 ppc_md.pci_dma_dev_setup = pnv_pci_dma_dev_setup;
548 ppc_md.tce_build = pnv_tce_build;
549 ppc_md.tce_free = pnv_tce_free;
Alexey Kardashevskiy11f63d32012-09-04 15:19:35 +0000550 ppc_md.tce_get = pnv_tce_get;
Benjamin Herrenschmidt82ba1292011-09-19 17:45:07 +0000551 ppc_md.pci_probe_mode = pnv_pci_probe_mode;
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000552 set_pci_dma_ops(&dma_iommu_ops);
553
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +0000554 /* Configure MSIs */
555#ifdef CONFIG_PCI_MSI
556 ppc_md.msi_check_device = pnv_msi_check_device;
557 ppc_md.setup_msi_irqs = pnv_setup_msi_irqs;
558 ppc_md.teardown_msi_irqs = pnv_teardown_msi_irqs;
559#endif
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000560}