]>
Commit | Line | Data |
---|---|---|
184cd4a3 BH |
1 | /* |
2 | * Support PCI/PCIe on PowerNV platforms | |
3 | * | |
4 | * Copyright 2011 Benjamin Herrenschmidt, IBM Corp. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version | |
9 | * 2 of the License, or (at your option) any later version. | |
10 | */ | |
11 | ||
cee72d5b | 12 | #undef DEBUG |
184cd4a3 BH |
13 | |
14 | #include <linux/kernel.h> | |
15 | #include <linux/pci.h> | |
361f2a2a | 16 | #include <linux/crash_dump.h> |
37c367f2 | 17 | #include <linux/debugfs.h> |
184cd4a3 BH |
18 | #include <linux/delay.h> |
19 | #include <linux/string.h> | |
20 | #include <linux/init.h> | |
21 | #include <linux/bootmem.h> | |
22 | #include <linux/irq.h> | |
23 | #include <linux/io.h> | |
24 | #include <linux/msi.h> | |
cd15b048 | 25 | #include <linux/memblock.h> |
184cd4a3 BH |
26 | |
27 | #include <asm/sections.h> | |
28 | #include <asm/io.h> | |
29 | #include <asm/prom.h> | |
30 | #include <asm/pci-bridge.h> | |
31 | #include <asm/machdep.h> | |
fb1b55d6 | 32 | #include <asm/msi_bitmap.h> |
184cd4a3 BH |
33 | #include <asm/ppc-pci.h> |
34 | #include <asm/opal.h> | |
35 | #include <asm/iommu.h> | |
36 | #include <asm/tce.h> | |
137436c9 | 37 | #include <asm/xics.h> |
37c367f2 | 38 | #include <asm/debug.h> |
262af557 | 39 | #include <asm/firmware.h> |
184cd4a3 BH |
40 | |
41 | #include "powernv.h" | |
42 | #include "pci.h" | |
43 | ||
184cd4a3 BH |
44 | #define define_pe_printk_level(func, kern_level) \ |
45 | static int func(const struct pnv_ioda_pe *pe, const char *fmt, ...) \ | |
46 | { \ | |
47 | struct va_format vaf; \ | |
48 | va_list args; \ | |
490e078d | 49 | char pfix[32]; \ |
184cd4a3 BH |
50 | int r; \ |
51 | \ | |
52 | va_start(args, fmt); \ | |
53 | \ | |
54 | vaf.fmt = fmt; \ | |
55 | vaf.va = &args; \ | |
56 | \ | |
490e078d GS |
57 | if (pe->pdev) \ |
58 | strlcpy(pfix, dev_name(&pe->pdev->dev), \ | |
59 | sizeof(pfix)); \ | |
60 | else \ | |
61 | sprintf(pfix, "%04x:%02x ", \ | |
62 | pci_domain_nr(pe->pbus), \ | |
63 | pe->pbus->number); \ | |
64 | r = printk(kern_level "pci %s: [PE# %.3d] %pV", \ | |
65 | pfix, pe->pe_number, &vaf); \ | |
66 | \ | |
184cd4a3 BH |
67 | va_end(args); \ |
68 | \ | |
69 | return r; \ | |
70 | } \ | |
71 | ||
72 | define_pe_printk_level(pe_err, KERN_ERR); | |
73 | define_pe_printk_level(pe_warn, KERN_WARNING); | |
74 | define_pe_printk_level(pe_info, KERN_INFO); | |
75 | ||
8e0a1611 AK |
76 | /* |
77 | * stdcix is only supposed to be used in hypervisor real mode as per | |
78 | * the architecture spec | |
79 | */ | |
80 | static inline void __raw_rm_writeq(u64 val, volatile void __iomem *paddr) | |
81 | { | |
82 | __asm__ __volatile__("stdcix %0,0,%1" | |
83 | : : "r" (val), "r" (paddr) : "memory"); | |
84 | } | |
85 | ||
262af557 GC |
86 | static inline bool pnv_pci_is_mem_pref_64(unsigned long flags) |
87 | { | |
88 | return ((flags & (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH)) == | |
89 | (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH)); | |
90 | } | |
91 | ||
cad5cef6 | 92 | static int pnv_ioda_alloc_pe(struct pnv_phb *phb) |
184cd4a3 BH |
93 | { |
94 | unsigned long pe; | |
95 | ||
96 | do { | |
97 | pe = find_next_zero_bit(phb->ioda.pe_alloc, | |
98 | phb->ioda.total_pe, 0); | |
99 | if (pe >= phb->ioda.total_pe) | |
100 | return IODA_INVALID_PE; | |
101 | } while(test_and_set_bit(pe, phb->ioda.pe_alloc)); | |
102 | ||
4cce9550 | 103 | phb->ioda.pe_array[pe].phb = phb; |
184cd4a3 BH |
104 | phb->ioda.pe_array[pe].pe_number = pe; |
105 | return pe; | |
106 | } | |
107 | ||
cad5cef6 | 108 | static void pnv_ioda_free_pe(struct pnv_phb *phb, int pe) |
184cd4a3 BH |
109 | { |
110 | WARN_ON(phb->ioda.pe_array[pe].pdev); | |
111 | ||
112 | memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe)); | |
113 | clear_bit(pe, phb->ioda.pe_alloc); | |
114 | } | |
115 | ||
262af557 GC |
116 | /* The default M64 BAR is shared by all PEs */ |
117 | static int pnv_ioda2_init_m64(struct pnv_phb *phb) | |
118 | { | |
119 | const char *desc; | |
120 | struct resource *r; | |
121 | s64 rc; | |
122 | ||
123 | /* Configure the default M64 BAR */ | |
124 | rc = opal_pci_set_phb_mem_window(phb->opal_id, | |
125 | OPAL_M64_WINDOW_TYPE, | |
126 | phb->ioda.m64_bar_idx, | |
127 | phb->ioda.m64_base, | |
128 | 0, /* unused */ | |
129 | phb->ioda.m64_size); | |
130 | if (rc != OPAL_SUCCESS) { | |
131 | desc = "configuring"; | |
132 | goto fail; | |
133 | } | |
134 | ||
135 | /* Enable the default M64 BAR */ | |
136 | rc = opal_pci_phb_mmio_enable(phb->opal_id, | |
137 | OPAL_M64_WINDOW_TYPE, | |
138 | phb->ioda.m64_bar_idx, | |
139 | OPAL_ENABLE_M64_SPLIT); | |
140 | if (rc != OPAL_SUCCESS) { | |
141 | desc = "enabling"; | |
142 | goto fail; | |
143 | } | |
144 | ||
145 | /* Mark the M64 BAR assigned */ | |
146 | set_bit(phb->ioda.m64_bar_idx, &phb->ioda.m64_bar_alloc); | |
147 | ||
148 | /* | |
149 | * Strip off the segment used by the reserved PE, which is | |
150 | * expected to be 0 or last one of PE capabicity. | |
151 | */ | |
152 | r = &phb->hose->mem_resources[1]; | |
153 | if (phb->ioda.reserved_pe == 0) | |
154 | r->start += phb->ioda.m64_segsize; | |
155 | else if (phb->ioda.reserved_pe == (phb->ioda.total_pe - 1)) | |
156 | r->end -= phb->ioda.m64_segsize; | |
157 | else | |
158 | pr_warn(" Cannot strip M64 segment for reserved PE#%d\n", | |
159 | phb->ioda.reserved_pe); | |
160 | ||
161 | return 0; | |
162 | ||
163 | fail: | |
164 | pr_warn(" Failure %lld %s M64 BAR#%d\n", | |
165 | rc, desc, phb->ioda.m64_bar_idx); | |
166 | opal_pci_phb_mmio_enable(phb->opal_id, | |
167 | OPAL_M64_WINDOW_TYPE, | |
168 | phb->ioda.m64_bar_idx, | |
169 | OPAL_DISABLE_M64); | |
170 | return -EIO; | |
171 | } | |
172 | ||
173 | static void pnv_ioda2_alloc_m64_pe(struct pnv_phb *phb) | |
174 | { | |
175 | resource_size_t sgsz = phb->ioda.m64_segsize; | |
176 | struct pci_dev *pdev; | |
177 | struct resource *r; | |
178 | int base, step, i; | |
179 | ||
180 | /* | |
181 | * Root bus always has full M64 range and root port has | |
182 | * M64 range used in reality. So we're checking root port | |
183 | * instead of root bus. | |
184 | */ | |
185 | list_for_each_entry(pdev, &phb->hose->bus->devices, bus_list) { | |
186 | for (i = PCI_BRIDGE_RESOURCES; | |
187 | i <= PCI_BRIDGE_RESOURCE_END; i++) { | |
188 | r = &pdev->resource[i]; | |
189 | if (!r->parent || | |
190 | !pnv_pci_is_mem_pref_64(r->flags)) | |
191 | continue; | |
192 | ||
193 | base = (r->start - phb->ioda.m64_base) / sgsz; | |
194 | for (step = 0; step < resource_size(r) / sgsz; step++) | |
195 | set_bit(base + step, phb->ioda.pe_alloc); | |
196 | } | |
197 | } | |
198 | } | |
199 | ||
200 | static int pnv_ioda2_pick_m64_pe(struct pnv_phb *phb, | |
201 | struct pci_bus *bus, int all) | |
202 | { | |
203 | resource_size_t segsz = phb->ioda.m64_segsize; | |
204 | struct pci_dev *pdev; | |
205 | struct resource *r; | |
206 | struct pnv_ioda_pe *master_pe, *pe; | |
207 | unsigned long size, *pe_alloc; | |
208 | bool found; | |
209 | int start, i, j; | |
210 | ||
211 | /* Root bus shouldn't use M64 */ | |
212 | if (pci_is_root_bus(bus)) | |
213 | return IODA_INVALID_PE; | |
214 | ||
215 | /* We support only one M64 window on each bus */ | |
216 | found = false; | |
217 | pci_bus_for_each_resource(bus, r, i) { | |
218 | if (r && r->parent && | |
219 | pnv_pci_is_mem_pref_64(r->flags)) { | |
220 | found = true; | |
221 | break; | |
222 | } | |
223 | } | |
224 | ||
225 | /* No M64 window found ? */ | |
226 | if (!found) | |
227 | return IODA_INVALID_PE; | |
228 | ||
229 | /* Allocate bitmap */ | |
230 | size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long)); | |
231 | pe_alloc = kzalloc(size, GFP_KERNEL); | |
232 | if (!pe_alloc) { | |
233 | pr_warn("%s: Out of memory !\n", | |
234 | __func__); | |
235 | return IODA_INVALID_PE; | |
236 | } | |
237 | ||
238 | /* | |
239 | * Figure out reserved PE numbers by the PE | |
240 | * the its child PEs. | |
241 | */ | |
242 | start = (r->start - phb->ioda.m64_base) / segsz; | |
243 | for (i = 0; i < resource_size(r) / segsz; i++) | |
244 | set_bit(start + i, pe_alloc); | |
245 | ||
246 | if (all) | |
247 | goto done; | |
248 | ||
249 | /* | |
250 | * If the PE doesn't cover all subordinate buses, | |
251 | * we need subtract from reserved PEs for children. | |
252 | */ | |
253 | list_for_each_entry(pdev, &bus->devices, bus_list) { | |
254 | if (!pdev->subordinate) | |
255 | continue; | |
256 | ||
257 | pci_bus_for_each_resource(pdev->subordinate, r, i) { | |
258 | if (!r || !r->parent || | |
259 | !pnv_pci_is_mem_pref_64(r->flags)) | |
260 | continue; | |
261 | ||
262 | start = (r->start - phb->ioda.m64_base) / segsz; | |
263 | for (j = 0; j < resource_size(r) / segsz ; j++) | |
264 | clear_bit(start + j, pe_alloc); | |
265 | } | |
266 | } | |
267 | ||
268 | /* | |
269 | * the current bus might not own M64 window and that's all | |
270 | * contributed by its child buses. For the case, we needn't | |
271 | * pick M64 dependent PE#. | |
272 | */ | |
273 | if (bitmap_empty(pe_alloc, phb->ioda.total_pe)) { | |
274 | kfree(pe_alloc); | |
275 | return IODA_INVALID_PE; | |
276 | } | |
277 | ||
278 | /* | |
279 | * Figure out the master PE and put all slave PEs to master | |
280 | * PE's list to form compound PE. | |
281 | */ | |
282 | done: | |
283 | master_pe = NULL; | |
284 | i = -1; | |
285 | while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe, i + 1)) < | |
286 | phb->ioda.total_pe) { | |
287 | pe = &phb->ioda.pe_array[i]; | |
288 | pe->phb = phb; | |
289 | pe->pe_number = i; | |
290 | ||
291 | if (!master_pe) { | |
292 | pe->flags |= PNV_IODA_PE_MASTER; | |
293 | INIT_LIST_HEAD(&pe->slaves); | |
294 | master_pe = pe; | |
295 | } else { | |
296 | pe->flags |= PNV_IODA_PE_SLAVE; | |
297 | pe->master = master_pe; | |
298 | list_add_tail(&pe->list, &master_pe->slaves); | |
299 | } | |
300 | } | |
301 | ||
302 | kfree(pe_alloc); | |
303 | return master_pe->pe_number; | |
304 | } | |
305 | ||
306 | static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb) | |
307 | { | |
308 | struct pci_controller *hose = phb->hose; | |
309 | struct device_node *dn = hose->dn; | |
310 | struct resource *res; | |
311 | const u32 *r; | |
312 | u64 pci_addr; | |
313 | ||
314 | if (!firmware_has_feature(FW_FEATURE_OPALv3)) { | |
315 | pr_info(" Firmware too old to support M64 window\n"); | |
316 | return; | |
317 | } | |
318 | ||
319 | r = of_get_property(dn, "ibm,opal-m64-window", NULL); | |
320 | if (!r) { | |
321 | pr_info(" No <ibm,opal-m64-window> on %s\n", | |
322 | dn->full_name); | |
323 | return; | |
324 | } | |
325 | ||
326 | /* FIXME: Support M64 for P7IOC */ | |
327 | if (phb->type != PNV_PHB_IODA2) { | |
328 | pr_info(" Not support M64 window\n"); | |
329 | return; | |
330 | } | |
331 | ||
332 | res = &hose->mem_resources[1]; | |
333 | res->start = of_translate_address(dn, r + 2); | |
334 | res->end = res->start + of_read_number(r + 4, 2) - 1; | |
335 | res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH); | |
336 | pci_addr = of_read_number(r, 2); | |
337 | hose->mem_offset[1] = res->start - pci_addr; | |
338 | ||
339 | phb->ioda.m64_size = resource_size(res); | |
340 | phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe; | |
341 | phb->ioda.m64_base = pci_addr; | |
342 | ||
343 | /* Use last M64 BAR to cover M64 window */ | |
344 | phb->ioda.m64_bar_idx = 15; | |
345 | phb->init_m64 = pnv_ioda2_init_m64; | |
346 | phb->alloc_m64_pe = pnv_ioda2_alloc_m64_pe; | |
347 | phb->pick_m64_pe = pnv_ioda2_pick_m64_pe; | |
348 | } | |
349 | ||
184cd4a3 BH |
350 | /* Currently those 2 are only used when MSIs are enabled, this will change |
351 | * but in the meantime, we need to protect them to avoid warnings | |
352 | */ | |
353 | #ifdef CONFIG_PCI_MSI | |
cad5cef6 | 354 | static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev) |
184cd4a3 BH |
355 | { |
356 | struct pci_controller *hose = pci_bus_to_host(dev->bus); | |
357 | struct pnv_phb *phb = hose->private_data; | |
b72c1f65 | 358 | struct pci_dn *pdn = pci_get_pdn(dev); |
184cd4a3 BH |
359 | |
360 | if (!pdn) | |
361 | return NULL; | |
362 | if (pdn->pe_number == IODA_INVALID_PE) | |
363 | return NULL; | |
364 | return &phb->ioda.pe_array[pdn->pe_number]; | |
365 | } | |
184cd4a3 BH |
366 | #endif /* CONFIG_PCI_MSI */ |
367 | ||
cad5cef6 | 368 | static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe) |
184cd4a3 BH |
369 | { |
370 | struct pci_dev *parent; | |
371 | uint8_t bcomp, dcomp, fcomp; | |
372 | long rc, rid_end, rid; | |
373 | ||
374 | /* Bus validation ? */ | |
375 | if (pe->pbus) { | |
376 | int count; | |
377 | ||
378 | dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER; | |
379 | fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER; | |
380 | parent = pe->pbus->self; | |
fb446ad0 GS |
381 | if (pe->flags & PNV_IODA_PE_BUS_ALL) |
382 | count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1; | |
383 | else | |
384 | count = 1; | |
385 | ||
184cd4a3 BH |
386 | switch(count) { |
387 | case 1: bcomp = OpalPciBusAll; break; | |
388 | case 2: bcomp = OpalPciBus7Bits; break; | |
389 | case 4: bcomp = OpalPciBus6Bits; break; | |
390 | case 8: bcomp = OpalPciBus5Bits; break; | |
391 | case 16: bcomp = OpalPciBus4Bits; break; | |
392 | case 32: bcomp = OpalPciBus3Bits; break; | |
393 | default: | |
394 | pr_err("%s: Number of subordinate busses %d" | |
395 | " unsupported\n", | |
396 | pci_name(pe->pbus->self), count); | |
397 | /* Do an exact match only */ | |
398 | bcomp = OpalPciBusAll; | |
399 | } | |
400 | rid_end = pe->rid + (count << 8); | |
401 | } else { | |
402 | parent = pe->pdev->bus->self; | |
403 | bcomp = OpalPciBusAll; | |
404 | dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER; | |
405 | fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER; | |
406 | rid_end = pe->rid + 1; | |
407 | } | |
408 | ||
631ad691 GS |
409 | /* |
410 | * Associate PE in PELT. We need add the PE into the | |
411 | * corresponding PELT-V as well. Otherwise, the error | |
412 | * originated from the PE might contribute to other | |
413 | * PEs. | |
414 | */ | |
184cd4a3 BH |
415 | rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid, |
416 | bcomp, dcomp, fcomp, OPAL_MAP_PE); | |
417 | if (rc) { | |
418 | pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc); | |
419 | return -ENXIO; | |
420 | } | |
631ad691 GS |
421 | |
422 | rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number, | |
423 | pe->pe_number, OPAL_ADD_PE_TO_DOMAIN); | |
424 | if (rc) | |
425 | pe_warn(pe, "OPAL error %d adding self to PELTV\n", rc); | |
184cd4a3 BH |
426 | opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number, |
427 | OPAL_EEH_ACTION_CLEAR_FREEZE_ALL); | |
428 | ||
429 | /* Add to all parents PELT-V */ | |
430 | while (parent) { | |
b72c1f65 | 431 | struct pci_dn *pdn = pci_get_pdn(parent); |
184cd4a3 BH |
432 | if (pdn && pdn->pe_number != IODA_INVALID_PE) { |
433 | rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number, | |
cee72d5b | 434 | pe->pe_number, OPAL_ADD_PE_TO_DOMAIN); |
184cd4a3 BH |
435 | /* XXX What to do in case of error ? */ |
436 | } | |
437 | parent = parent->bus->self; | |
438 | } | |
439 | /* Setup reverse map */ | |
440 | for (rid = pe->rid; rid < rid_end; rid++) | |
441 | phb->ioda.pe_rmap[rid] = pe->pe_number; | |
442 | ||
443 | /* Setup one MVTs on IODA1 */ | |
444 | if (phb->type == PNV_PHB_IODA1) { | |
445 | pe->mve_number = pe->pe_number; | |
446 | rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, | |
447 | pe->pe_number); | |
448 | if (rc) { | |
449 | pe_err(pe, "OPAL error %ld setting up MVE %d\n", | |
450 | rc, pe->mve_number); | |
451 | pe->mve_number = -1; | |
452 | } else { | |
453 | rc = opal_pci_set_mve_enable(phb->opal_id, | |
cee72d5b | 454 | pe->mve_number, OPAL_ENABLE_MVE); |
184cd4a3 BH |
455 | if (rc) { |
456 | pe_err(pe, "OPAL error %ld enabling MVE %d\n", | |
457 | rc, pe->mve_number); | |
458 | pe->mve_number = -1; | |
459 | } | |
460 | } | |
461 | } else if (phb->type == PNV_PHB_IODA2) | |
462 | pe->mve_number = 0; | |
463 | ||
464 | return 0; | |
465 | } | |
466 | ||
cad5cef6 GKH |
467 | static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb, |
468 | struct pnv_ioda_pe *pe) | |
184cd4a3 BH |
469 | { |
470 | struct pnv_ioda_pe *lpe; | |
471 | ||
7ebdf956 | 472 | list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) { |
184cd4a3 | 473 | if (lpe->dma_weight < pe->dma_weight) { |
7ebdf956 | 474 | list_add_tail(&pe->dma_link, &lpe->dma_link); |
184cd4a3 BH |
475 | return; |
476 | } | |
477 | } | |
7ebdf956 | 478 | list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list); |
184cd4a3 BH |
479 | } |
480 | ||
481 | static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev) | |
482 | { | |
483 | /* This is quite simplistic. The "base" weight of a device | |
484 | * is 10. 0 means no DMA is to be accounted for it. | |
485 | */ | |
486 | ||
487 | /* If it's a bridge, no DMA */ | |
488 | if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL) | |
489 | return 0; | |
490 | ||
491 | /* Reduce the weight of slow USB controllers */ | |
492 | if (dev->class == PCI_CLASS_SERIAL_USB_UHCI || | |
493 | dev->class == PCI_CLASS_SERIAL_USB_OHCI || | |
494 | dev->class == PCI_CLASS_SERIAL_USB_EHCI) | |
495 | return 3; | |
496 | ||
497 | /* Increase the weight of RAID (includes Obsidian) */ | |
498 | if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID) | |
499 | return 15; | |
500 | ||
501 | /* Default */ | |
502 | return 10; | |
503 | } | |
504 | ||
fb446ad0 | 505 | #if 0 |
cad5cef6 | 506 | static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev) |
184cd4a3 BH |
507 | { |
508 | struct pci_controller *hose = pci_bus_to_host(dev->bus); | |
509 | struct pnv_phb *phb = hose->private_data; | |
b72c1f65 | 510 | struct pci_dn *pdn = pci_get_pdn(dev); |
184cd4a3 BH |
511 | struct pnv_ioda_pe *pe; |
512 | int pe_num; | |
513 | ||
514 | if (!pdn) { | |
515 | pr_err("%s: Device tree node not associated properly\n", | |
516 | pci_name(dev)); | |
517 | return NULL; | |
518 | } | |
519 | if (pdn->pe_number != IODA_INVALID_PE) | |
520 | return NULL; | |
521 | ||
522 | /* PE#0 has been pre-set */ | |
523 | if (dev->bus->number == 0) | |
524 | pe_num = 0; | |
525 | else | |
526 | pe_num = pnv_ioda_alloc_pe(phb); | |
527 | if (pe_num == IODA_INVALID_PE) { | |
528 | pr_warning("%s: Not enough PE# available, disabling device\n", | |
529 | pci_name(dev)); | |
530 | return NULL; | |
531 | } | |
532 | ||
533 | /* NOTE: We get only one ref to the pci_dev for the pdn, not for the | |
534 | * pointer in the PE data structure, both should be destroyed at the | |
535 | * same time. However, this needs to be looked at more closely again | |
536 | * once we actually start removing things (Hotplug, SR-IOV, ...) | |
537 | * | |
538 | * At some point we want to remove the PDN completely anyways | |
539 | */ | |
540 | pe = &phb->ioda.pe_array[pe_num]; | |
541 | pci_dev_get(dev); | |
542 | pdn->pcidev = dev; | |
543 | pdn->pe_number = pe_num; | |
544 | pe->pdev = dev; | |
545 | pe->pbus = NULL; | |
546 | pe->tce32_seg = -1; | |
547 | pe->mve_number = -1; | |
548 | pe->rid = dev->bus->number << 8 | pdn->devfn; | |
549 | ||
550 | pe_info(pe, "Associated device to PE\n"); | |
551 | ||
552 | if (pnv_ioda_configure_pe(phb, pe)) { | |
553 | /* XXX What do we do here ? */ | |
554 | if (pe_num) | |
555 | pnv_ioda_free_pe(phb, pe_num); | |
556 | pdn->pe_number = IODA_INVALID_PE; | |
557 | pe->pdev = NULL; | |
558 | pci_dev_put(dev); | |
559 | return NULL; | |
560 | } | |
561 | ||
562 | /* Assign a DMA weight to the device */ | |
563 | pe->dma_weight = pnv_ioda_dma_weight(dev); | |
564 | if (pe->dma_weight != 0) { | |
565 | phb->ioda.dma_weight += pe->dma_weight; | |
566 | phb->ioda.dma_pe_count++; | |
567 | } | |
568 | ||
569 | /* Link the PE */ | |
570 | pnv_ioda_link_pe_by_weight(phb, pe); | |
571 | ||
572 | return pe; | |
573 | } | |
fb446ad0 | 574 | #endif /* Useful for SRIOV case */ |
184cd4a3 BH |
575 | |
576 | static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe) | |
577 | { | |
578 | struct pci_dev *dev; | |
579 | ||
580 | list_for_each_entry(dev, &bus->devices, bus_list) { | |
b72c1f65 | 581 | struct pci_dn *pdn = pci_get_pdn(dev); |
184cd4a3 BH |
582 | |
583 | if (pdn == NULL) { | |
584 | pr_warn("%s: No device node associated with device !\n", | |
585 | pci_name(dev)); | |
586 | continue; | |
587 | } | |
184cd4a3 BH |
588 | pdn->pcidev = dev; |
589 | pdn->pe_number = pe->pe_number; | |
590 | pe->dma_weight += pnv_ioda_dma_weight(dev); | |
fb446ad0 | 591 | if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate) |
184cd4a3 BH |
592 | pnv_ioda_setup_same_PE(dev->subordinate, pe); |
593 | } | |
594 | } | |
595 | ||
fb446ad0 GS |
596 | /* |
597 | * There're 2 types of PCI bus sensitive PEs: One that is compromised of | |
598 | * single PCI bus. Another one that contains the primary PCI bus and its | |
599 | * subordinate PCI devices and buses. The second type of PE is normally | |
600 | * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports. | |
601 | */ | |
cad5cef6 | 602 | static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, int all) |
184cd4a3 | 603 | { |
fb446ad0 | 604 | struct pci_controller *hose = pci_bus_to_host(bus); |
184cd4a3 | 605 | struct pnv_phb *phb = hose->private_data; |
184cd4a3 | 606 | struct pnv_ioda_pe *pe; |
262af557 GC |
607 | int pe_num = IODA_INVALID_PE; |
608 | ||
609 | /* Check if PE is determined by M64 */ | |
610 | if (phb->pick_m64_pe) | |
611 | pe_num = phb->pick_m64_pe(phb, bus, all); | |
612 | ||
613 | /* The PE number isn't pinned by M64 */ | |
614 | if (pe_num == IODA_INVALID_PE) | |
615 | pe_num = pnv_ioda_alloc_pe(phb); | |
184cd4a3 | 616 | |
184cd4a3 | 617 | if (pe_num == IODA_INVALID_PE) { |
fb446ad0 GS |
618 | pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n", |
619 | __func__, pci_domain_nr(bus), bus->number); | |
184cd4a3 BH |
620 | return; |
621 | } | |
622 | ||
623 | pe = &phb->ioda.pe_array[pe_num]; | |
262af557 | 624 | pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS); |
184cd4a3 BH |
625 | pe->pbus = bus; |
626 | pe->pdev = NULL; | |
627 | pe->tce32_seg = -1; | |
628 | pe->mve_number = -1; | |
b918c62e | 629 | pe->rid = bus->busn_res.start << 8; |
184cd4a3 BH |
630 | pe->dma_weight = 0; |
631 | ||
fb446ad0 GS |
632 | if (all) |
633 | pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n", | |
634 | bus->busn_res.start, bus->busn_res.end, pe_num); | |
635 | else | |
636 | pe_info(pe, "Secondary bus %d associated with PE#%d\n", | |
637 | bus->busn_res.start, pe_num); | |
184cd4a3 BH |
638 | |
639 | if (pnv_ioda_configure_pe(phb, pe)) { | |
640 | /* XXX What do we do here ? */ | |
641 | if (pe_num) | |
642 | pnv_ioda_free_pe(phb, pe_num); | |
643 | pe->pbus = NULL; | |
644 | return; | |
645 | } | |
646 | ||
647 | /* Associate it with all child devices */ | |
648 | pnv_ioda_setup_same_PE(bus, pe); | |
649 | ||
7ebdf956 GS |
650 | /* Put PE to the list */ |
651 | list_add_tail(&pe->list, &phb->ioda.pe_list); | |
652 | ||
184cd4a3 BH |
653 | /* Account for one DMA PE if at least one DMA capable device exist |
654 | * below the bridge | |
655 | */ | |
656 | if (pe->dma_weight != 0) { | |
657 | phb->ioda.dma_weight += pe->dma_weight; | |
658 | phb->ioda.dma_pe_count++; | |
659 | } | |
660 | ||
661 | /* Link the PE */ | |
662 | pnv_ioda_link_pe_by_weight(phb, pe); | |
663 | } | |
664 | ||
cad5cef6 | 665 | static void pnv_ioda_setup_PEs(struct pci_bus *bus) |
184cd4a3 BH |
666 | { |
667 | struct pci_dev *dev; | |
fb446ad0 GS |
668 | |
669 | pnv_ioda_setup_bus_PE(bus, 0); | |
184cd4a3 BH |
670 | |
671 | list_for_each_entry(dev, &bus->devices, bus_list) { | |
fb446ad0 GS |
672 | if (dev->subordinate) { |
673 | if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE) | |
674 | pnv_ioda_setup_bus_PE(dev->subordinate, 1); | |
675 | else | |
676 | pnv_ioda_setup_PEs(dev->subordinate); | |
677 | } | |
678 | } | |
679 | } | |
680 | ||
681 | /* | |
682 | * Configure PEs so that the downstream PCI buses and devices | |
683 | * could have their associated PE#. Unfortunately, we didn't | |
684 | * figure out the way to identify the PLX bridge yet. So we | |
685 | * simply put the PCI bus and the subordinate behind the root | |
686 | * port to PE# here. The game rule here is expected to be changed | |
687 | * as soon as we can detected PLX bridge correctly. | |
688 | */ | |
cad5cef6 | 689 | static void pnv_pci_ioda_setup_PEs(void) |
fb446ad0 GS |
690 | { |
691 | struct pci_controller *hose, *tmp; | |
262af557 | 692 | struct pnv_phb *phb; |
fb446ad0 GS |
693 | |
694 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { | |
262af557 GC |
695 | phb = hose->private_data; |
696 | ||
697 | /* M64 layout might affect PE allocation */ | |
698 | if (phb->alloc_m64_pe) | |
699 | phb->alloc_m64_pe(phb); | |
700 | ||
fb446ad0 | 701 | pnv_ioda_setup_PEs(hose->bus); |
184cd4a3 BH |
702 | } |
703 | } | |
704 | ||
959c9bdd | 705 | static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev) |
184cd4a3 | 706 | { |
b72c1f65 | 707 | struct pci_dn *pdn = pci_get_pdn(pdev); |
959c9bdd | 708 | struct pnv_ioda_pe *pe; |
184cd4a3 | 709 | |
959c9bdd GS |
710 | /* |
711 | * The function can be called while the PE# | |
712 | * hasn't been assigned. Do nothing for the | |
713 | * case. | |
714 | */ | |
715 | if (!pdn || pdn->pe_number == IODA_INVALID_PE) | |
716 | return; | |
184cd4a3 | 717 | |
959c9bdd | 718 | pe = &phb->ioda.pe_array[pdn->pe_number]; |
cd15b048 | 719 | WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops); |
3f28c5af | 720 | set_iommu_table_base(&pdev->dev, &pe->tce32_table); |
184cd4a3 BH |
721 | } |
722 | ||
cd15b048 BH |
723 | static int pnv_pci_ioda_dma_set_mask(struct pnv_phb *phb, |
724 | struct pci_dev *pdev, u64 dma_mask) | |
725 | { | |
726 | struct pci_dn *pdn = pci_get_pdn(pdev); | |
727 | struct pnv_ioda_pe *pe; | |
728 | uint64_t top; | |
729 | bool bypass = false; | |
730 | ||
731 | if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE)) | |
732 | return -ENODEV;; | |
733 | ||
734 | pe = &phb->ioda.pe_array[pdn->pe_number]; | |
735 | if (pe->tce_bypass_enabled) { | |
736 | top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1; | |
737 | bypass = (dma_mask >= top); | |
738 | } | |
739 | ||
740 | if (bypass) { | |
741 | dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n"); | |
742 | set_dma_ops(&pdev->dev, &dma_direct_ops); | |
743 | set_dma_offset(&pdev->dev, pe->tce_bypass_base); | |
744 | } else { | |
745 | dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n"); | |
746 | set_dma_ops(&pdev->dev, &dma_iommu_ops); | |
747 | set_iommu_table_base(&pdev->dev, &pe->tce32_table); | |
748 | } | |
a32305bf | 749 | *pdev->dev.dma_mask = dma_mask; |
cd15b048 BH |
750 | return 0; |
751 | } | |
752 | ||
dff4a39e GS |
753 | static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe, |
754 | struct pci_bus *bus, | |
755 | bool add_to_iommu_group) | |
74251fe2 BH |
756 | { |
757 | struct pci_dev *dev; | |
758 | ||
759 | list_for_each_entry(dev, &bus->devices, bus_list) { | |
dff4a39e GS |
760 | if (add_to_iommu_group) |
761 | set_iommu_table_base_and_group(&dev->dev, | |
762 | &pe->tce32_table); | |
763 | else | |
764 | set_iommu_table_base(&dev->dev, &pe->tce32_table); | |
765 | ||
74251fe2 | 766 | if (dev->subordinate) |
dff4a39e GS |
767 | pnv_ioda_setup_bus_dma(pe, dev->subordinate, |
768 | add_to_iommu_group); | |
74251fe2 BH |
769 | } |
770 | } | |
771 | ||
8e0a1611 AK |
772 | static void pnv_pci_ioda1_tce_invalidate(struct pnv_ioda_pe *pe, |
773 | struct iommu_table *tbl, | |
3ad26e5c | 774 | __be64 *startp, __be64 *endp, bool rm) |
4cce9550 | 775 | { |
3ad26e5c BH |
776 | __be64 __iomem *invalidate = rm ? |
777 | (__be64 __iomem *)pe->tce_inval_reg_phys : | |
778 | (__be64 __iomem *)tbl->it_index; | |
4cce9550 | 779 | unsigned long start, end, inc; |
b0376c9b | 780 | const unsigned shift = tbl->it_page_shift; |
4cce9550 GS |
781 | |
782 | start = __pa(startp); | |
783 | end = __pa(endp); | |
784 | ||
785 | /* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */ | |
786 | if (tbl->it_busno) { | |
b0376c9b AK |
787 | start <<= shift; |
788 | end <<= shift; | |
789 | inc = 128ull << shift; | |
4cce9550 GS |
790 | start |= tbl->it_busno; |
791 | end |= tbl->it_busno; | |
792 | } else if (tbl->it_type & TCE_PCI_SWINV_PAIR) { | |
793 | /* p7ioc-style invalidation, 2 TCEs per write */ | |
794 | start |= (1ull << 63); | |
795 | end |= (1ull << 63); | |
796 | inc = 16; | |
797 | } else { | |
798 | /* Default (older HW) */ | |
799 | inc = 128; | |
800 | } | |
801 | ||
802 | end |= inc - 1; /* round up end to be different than start */ | |
803 | ||
804 | mb(); /* Ensure above stores are visible */ | |
805 | while (start <= end) { | |
8e0a1611 | 806 | if (rm) |
3ad26e5c | 807 | __raw_rm_writeq(cpu_to_be64(start), invalidate); |
8e0a1611 | 808 | else |
3ad26e5c | 809 | __raw_writeq(cpu_to_be64(start), invalidate); |
4cce9550 GS |
810 | start += inc; |
811 | } | |
812 | ||
813 | /* | |
814 | * The iommu layer will do another mb() for us on build() | |
815 | * and we don't care on free() | |
816 | */ | |
817 | } | |
818 | ||
819 | static void pnv_pci_ioda2_tce_invalidate(struct pnv_ioda_pe *pe, | |
820 | struct iommu_table *tbl, | |
3ad26e5c | 821 | __be64 *startp, __be64 *endp, bool rm) |
4cce9550 GS |
822 | { |
823 | unsigned long start, end, inc; | |
3ad26e5c BH |
824 | __be64 __iomem *invalidate = rm ? |
825 | (__be64 __iomem *)pe->tce_inval_reg_phys : | |
826 | (__be64 __iomem *)tbl->it_index; | |
b0376c9b | 827 | const unsigned shift = tbl->it_page_shift; |
4cce9550 GS |
828 | |
829 | /* We'll invalidate DMA address in PE scope */ | |
b0376c9b | 830 | start = 0x2ull << 60; |
4cce9550 GS |
831 | start |= (pe->pe_number & 0xFF); |
832 | end = start; | |
833 | ||
834 | /* Figure out the start, end and step */ | |
835 | inc = tbl->it_offset + (((u64)startp - tbl->it_base) / sizeof(u64)); | |
b0376c9b | 836 | start |= (inc << shift); |
4cce9550 | 837 | inc = tbl->it_offset + (((u64)endp - tbl->it_base) / sizeof(u64)); |
b0376c9b AK |
838 | end |= (inc << shift); |
839 | inc = (0x1ull << shift); | |
4cce9550 GS |
840 | mb(); |
841 | ||
842 | while (start <= end) { | |
8e0a1611 | 843 | if (rm) |
3ad26e5c | 844 | __raw_rm_writeq(cpu_to_be64(start), invalidate); |
8e0a1611 | 845 | else |
3ad26e5c | 846 | __raw_writeq(cpu_to_be64(start), invalidate); |
4cce9550 GS |
847 | start += inc; |
848 | } | |
849 | } | |
850 | ||
851 | void pnv_pci_ioda_tce_invalidate(struct iommu_table *tbl, | |
3ad26e5c | 852 | __be64 *startp, __be64 *endp, bool rm) |
4cce9550 GS |
853 | { |
854 | struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe, | |
855 | tce32_table); | |
856 | struct pnv_phb *phb = pe->phb; | |
857 | ||
858 | if (phb->type == PNV_PHB_IODA1) | |
8e0a1611 | 859 | pnv_pci_ioda1_tce_invalidate(pe, tbl, startp, endp, rm); |
4cce9550 | 860 | else |
8e0a1611 | 861 | pnv_pci_ioda2_tce_invalidate(pe, tbl, startp, endp, rm); |
4cce9550 GS |
862 | } |
863 | ||
cad5cef6 GKH |
864 | static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb, |
865 | struct pnv_ioda_pe *pe, unsigned int base, | |
866 | unsigned int segs) | |
184cd4a3 BH |
867 | { |
868 | ||
869 | struct page *tce_mem = NULL; | |
870 | const __be64 *swinvp; | |
871 | struct iommu_table *tbl; | |
872 | unsigned int i; | |
873 | int64_t rc; | |
874 | void *addr; | |
875 | ||
876 | /* 256M DMA window, 4K TCE pages, 8 bytes TCE */ | |
877 | #define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8) | |
878 | ||
879 | /* XXX FIXME: Handle 64-bit only DMA devices */ | |
880 | /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */ | |
881 | /* XXX FIXME: Allocate multi-level tables on PHB3 */ | |
882 | ||
883 | /* We shouldn't already have a 32-bit DMA associated */ | |
884 | if (WARN_ON(pe->tce32_seg >= 0)) | |
885 | return; | |
886 | ||
887 | /* Grab a 32-bit TCE table */ | |
888 | pe->tce32_seg = base; | |
889 | pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n", | |
890 | (base << 28), ((base + segs) << 28) - 1); | |
891 | ||
892 | /* XXX Currently, we allocate one big contiguous table for the | |
893 | * TCEs. We only really need one chunk per 256M of TCE space | |
894 | * (ie per segment) but that's an optimization for later, it | |
895 | * requires some added smarts with our get/put_tce implementation | |
896 | */ | |
897 | tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL, | |
898 | get_order(TCE32_TABLE_SIZE * segs)); | |
899 | if (!tce_mem) { | |
900 | pe_err(pe, " Failed to allocate a 32-bit TCE memory\n"); | |
901 | goto fail; | |
902 | } | |
903 | addr = page_address(tce_mem); | |
904 | memset(addr, 0, TCE32_TABLE_SIZE * segs); | |
905 | ||
906 | /* Configure HW */ | |
907 | for (i = 0; i < segs; i++) { | |
908 | rc = opal_pci_map_pe_dma_window(phb->opal_id, | |
909 | pe->pe_number, | |
910 | base + i, 1, | |
911 | __pa(addr) + TCE32_TABLE_SIZE * i, | |
912 | TCE32_TABLE_SIZE, 0x1000); | |
913 | if (rc) { | |
914 | pe_err(pe, " Failed to configure 32-bit TCE table," | |
915 | " err %ld\n", rc); | |
916 | goto fail; | |
917 | } | |
918 | } | |
919 | ||
920 | /* Setup linux iommu table */ | |
921 | tbl = &pe->tce32_table; | |
922 | pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs, | |
8fa5d454 | 923 | base << 28, IOMMU_PAGE_SHIFT_4K); |
184cd4a3 BH |
924 | |
925 | /* OPAL variant of P7IOC SW invalidated TCEs */ | |
926 | swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL); | |
927 | if (swinvp) { | |
928 | /* We need a couple more fields -- an address and a data | |
929 | * to or. Since the bus is only printed out on table free | |
930 | * errors, and on the first pass the data will be a relative | |
931 | * bus number, print that out instead. | |
932 | */ | |
8e0a1611 AK |
933 | pe->tce_inval_reg_phys = be64_to_cpup(swinvp); |
934 | tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys, | |
935 | 8); | |
65fd766b GS |
936 | tbl->it_type |= (TCE_PCI_SWINV_CREATE | |
937 | TCE_PCI_SWINV_FREE | | |
938 | TCE_PCI_SWINV_PAIR); | |
184cd4a3 BH |
939 | } |
940 | iommu_init_table(tbl, phb->hose->node); | |
e9bc03fe | 941 | iommu_register_group(tbl, phb->hose->global_number, pe->pe_number); |
184cd4a3 | 942 | |
74251fe2 | 943 | if (pe->pdev) |
d905c5df | 944 | set_iommu_table_base_and_group(&pe->pdev->dev, tbl); |
74251fe2 | 945 | else |
dff4a39e | 946 | pnv_ioda_setup_bus_dma(pe, pe->pbus, true); |
74251fe2 | 947 | |
184cd4a3 BH |
948 | return; |
949 | fail: | |
950 | /* XXX Failure: Try to fallback to 64-bit only ? */ | |
951 | if (pe->tce32_seg >= 0) | |
952 | pe->tce32_seg = -1; | |
953 | if (tce_mem) | |
954 | __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs)); | |
955 | } | |
956 | ||
cd15b048 BH |
957 | static void pnv_pci_ioda2_set_bypass(struct iommu_table *tbl, bool enable) |
958 | { | |
959 | struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe, | |
960 | tce32_table); | |
961 | uint16_t window_id = (pe->pe_number << 1 ) + 1; | |
962 | int64_t rc; | |
963 | ||
964 | pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis"); | |
965 | if (enable) { | |
966 | phys_addr_t top = memblock_end_of_DRAM(); | |
967 | ||
968 | top = roundup_pow_of_two(top); | |
969 | rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id, | |
970 | pe->pe_number, | |
971 | window_id, | |
972 | pe->tce_bypass_base, | |
973 | top); | |
974 | } else { | |
975 | rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id, | |
976 | pe->pe_number, | |
977 | window_id, | |
978 | pe->tce_bypass_base, | |
979 | 0); | |
980 | ||
981 | /* | |
dff4a39e GS |
982 | * EEH needs the mapping between IOMMU table and group |
983 | * of those VFIO/KVM pass-through devices. We can postpone | |
984 | * resetting DMA ops until the DMA mask is configured in | |
985 | * host side. | |
cd15b048 | 986 | */ |
dff4a39e GS |
987 | if (pe->pdev) |
988 | set_iommu_table_base(&pe->pdev->dev, tbl); | |
989 | else | |
990 | pnv_ioda_setup_bus_dma(pe, pe->pbus, false); | |
cd15b048 BH |
991 | } |
992 | if (rc) | |
993 | pe_err(pe, "OPAL error %lld configuring bypass window\n", rc); | |
994 | else | |
995 | pe->tce_bypass_enabled = enable; | |
996 | } | |
997 | ||
998 | static void pnv_pci_ioda2_setup_bypass_pe(struct pnv_phb *phb, | |
999 | struct pnv_ioda_pe *pe) | |
1000 | { | |
1001 | /* TVE #1 is selected by PCI address bit 59 */ | |
1002 | pe->tce_bypass_base = 1ull << 59; | |
1003 | ||
1004 | /* Install set_bypass callback for VFIO */ | |
1005 | pe->tce32_table.set_bypass = pnv_pci_ioda2_set_bypass; | |
1006 | ||
1007 | /* Enable bypass by default */ | |
1008 | pnv_pci_ioda2_set_bypass(&pe->tce32_table, true); | |
1009 | } | |
1010 | ||
373f5657 GS |
1011 | static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb, |
1012 | struct pnv_ioda_pe *pe) | |
1013 | { | |
1014 | struct page *tce_mem = NULL; | |
1015 | void *addr; | |
1016 | const __be64 *swinvp; | |
1017 | struct iommu_table *tbl; | |
1018 | unsigned int tce_table_size, end; | |
1019 | int64_t rc; | |
1020 | ||
1021 | /* We shouldn't already have a 32-bit DMA associated */ | |
1022 | if (WARN_ON(pe->tce32_seg >= 0)) | |
1023 | return; | |
1024 | ||
1025 | /* The PE will reserve all possible 32-bits space */ | |
1026 | pe->tce32_seg = 0; | |
1027 | end = (1 << ilog2(phb->ioda.m32_pci_base)); | |
1028 | tce_table_size = (end / 0x1000) * 8; | |
1029 | pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n", | |
1030 | end); | |
1031 | ||
1032 | /* Allocate TCE table */ | |
1033 | tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL, | |
1034 | get_order(tce_table_size)); | |
1035 | if (!tce_mem) { | |
1036 | pe_err(pe, "Failed to allocate a 32-bit TCE memory\n"); | |
1037 | goto fail; | |
1038 | } | |
1039 | addr = page_address(tce_mem); | |
1040 | memset(addr, 0, tce_table_size); | |
1041 | ||
1042 | /* | |
1043 | * Map TCE table through TVT. The TVE index is the PE number | |
1044 | * shifted by 1 bit for 32-bits DMA space. | |
1045 | */ | |
1046 | rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number, | |
1047 | pe->pe_number << 1, 1, __pa(addr), | |
1048 | tce_table_size, 0x1000); | |
1049 | if (rc) { | |
1050 | pe_err(pe, "Failed to configure 32-bit TCE table," | |
1051 | " err %ld\n", rc); | |
1052 | goto fail; | |
1053 | } | |
1054 | ||
1055 | /* Setup linux iommu table */ | |
1056 | tbl = &pe->tce32_table; | |
8fa5d454 AK |
1057 | pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, 0, |
1058 | IOMMU_PAGE_SHIFT_4K); | |
373f5657 GS |
1059 | |
1060 | /* OPAL variant of PHB3 invalidated TCEs */ | |
1061 | swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL); | |
1062 | if (swinvp) { | |
1063 | /* We need a couple more fields -- an address and a data | |
1064 | * to or. Since the bus is only printed out on table free | |
1065 | * errors, and on the first pass the data will be a relative | |
1066 | * bus number, print that out instead. | |
1067 | */ | |
8e0a1611 AK |
1068 | pe->tce_inval_reg_phys = be64_to_cpup(swinvp); |
1069 | tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys, | |
1070 | 8); | |
65fd766b | 1071 | tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE); |
373f5657 GS |
1072 | } |
1073 | iommu_init_table(tbl, phb->hose->node); | |
e9bc03fe | 1074 | iommu_register_group(tbl, phb->hose->global_number, pe->pe_number); |
373f5657 | 1075 | |
74251fe2 | 1076 | if (pe->pdev) |
d905c5df | 1077 | set_iommu_table_base_and_group(&pe->pdev->dev, tbl); |
74251fe2 | 1078 | else |
dff4a39e | 1079 | pnv_ioda_setup_bus_dma(pe, pe->pbus, true); |
74251fe2 | 1080 | |
cd15b048 BH |
1081 | /* Also create a bypass window */ |
1082 | pnv_pci_ioda2_setup_bypass_pe(phb, pe); | |
373f5657 GS |
1083 | return; |
1084 | fail: | |
1085 | if (pe->tce32_seg >= 0) | |
1086 | pe->tce32_seg = -1; | |
1087 | if (tce_mem) | |
1088 | __free_pages(tce_mem, get_order(tce_table_size)); | |
1089 | } | |
1090 | ||
cad5cef6 | 1091 | static void pnv_ioda_setup_dma(struct pnv_phb *phb) |
184cd4a3 BH |
1092 | { |
1093 | struct pci_controller *hose = phb->hose; | |
1094 | unsigned int residual, remaining, segs, tw, base; | |
1095 | struct pnv_ioda_pe *pe; | |
1096 | ||
1097 | /* If we have more PE# than segments available, hand out one | |
1098 | * per PE until we run out and let the rest fail. If not, | |
1099 | * then we assign at least one segment per PE, plus more based | |
1100 | * on the amount of devices under that PE | |
1101 | */ | |
1102 | if (phb->ioda.dma_pe_count > phb->ioda.tce32_count) | |
1103 | residual = 0; | |
1104 | else | |
1105 | residual = phb->ioda.tce32_count - | |
1106 | phb->ioda.dma_pe_count; | |
1107 | ||
1108 | pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n", | |
1109 | hose->global_number, phb->ioda.tce32_count); | |
1110 | pr_info("PCI: %d PE# for a total weight of %d\n", | |
1111 | phb->ioda.dma_pe_count, phb->ioda.dma_weight); | |
1112 | ||
1113 | /* Walk our PE list and configure their DMA segments, hand them | |
1114 | * out one base segment plus any residual segments based on | |
1115 | * weight | |
1116 | */ | |
1117 | remaining = phb->ioda.tce32_count; | |
1118 | tw = phb->ioda.dma_weight; | |
1119 | base = 0; | |
7ebdf956 | 1120 | list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) { |
184cd4a3 BH |
1121 | if (!pe->dma_weight) |
1122 | continue; | |
1123 | if (!remaining) { | |
1124 | pe_warn(pe, "No DMA32 resources available\n"); | |
1125 | continue; | |
1126 | } | |
1127 | segs = 1; | |
1128 | if (residual) { | |
1129 | segs += ((pe->dma_weight * residual) + (tw / 2)) / tw; | |
1130 | if (segs > remaining) | |
1131 | segs = remaining; | |
1132 | } | |
373f5657 GS |
1133 | |
1134 | /* | |
1135 | * For IODA2 compliant PHB3, we needn't care about the weight. | |
1136 | * The all available 32-bits DMA space will be assigned to | |
1137 | * the specific PE. | |
1138 | */ | |
1139 | if (phb->type == PNV_PHB_IODA1) { | |
1140 | pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n", | |
1141 | pe->dma_weight, segs); | |
1142 | pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs); | |
1143 | } else { | |
1144 | pe_info(pe, "Assign DMA32 space\n"); | |
1145 | segs = 0; | |
1146 | pnv_pci_ioda2_setup_dma_pe(phb, pe); | |
1147 | } | |
1148 | ||
184cd4a3 BH |
1149 | remaining -= segs; |
1150 | base += segs; | |
1151 | } | |
1152 | } | |
1153 | ||
1154 | #ifdef CONFIG_PCI_MSI | |
137436c9 GS |
1155 | static void pnv_ioda2_msi_eoi(struct irq_data *d) |
1156 | { | |
1157 | unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d); | |
1158 | struct irq_chip *chip = irq_data_get_irq_chip(d); | |
1159 | struct pnv_phb *phb = container_of(chip, struct pnv_phb, | |
1160 | ioda.irq_chip); | |
1161 | int64_t rc; | |
1162 | ||
1163 | rc = opal_pci_msi_eoi(phb->opal_id, hw_irq); | |
1164 | WARN_ON_ONCE(rc); | |
1165 | ||
1166 | icp_native_eoi(d); | |
1167 | } | |
1168 | ||
184cd4a3 | 1169 | static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev, |
137436c9 GS |
1170 | unsigned int hwirq, unsigned int virq, |
1171 | unsigned int is_64, struct msi_msg *msg) | |
184cd4a3 BH |
1172 | { |
1173 | struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev); | |
b72c1f65 | 1174 | struct pci_dn *pdn = pci_get_pdn(dev); |
137436c9 GS |
1175 | struct irq_data *idata; |
1176 | struct irq_chip *ichip; | |
184cd4a3 | 1177 | unsigned int xive_num = hwirq - phb->msi_base; |
3a1a4661 | 1178 | __be32 data; |
184cd4a3 BH |
1179 | int rc; |
1180 | ||
1181 | /* No PE assigned ? bail out ... no MSI for you ! */ | |
1182 | if (pe == NULL) | |
1183 | return -ENXIO; | |
1184 | ||
1185 | /* Check if we have an MVE */ | |
1186 | if (pe->mve_number < 0) | |
1187 | return -ENXIO; | |
1188 | ||
b72c1f65 BH |
1189 | /* Force 32-bit MSI on some broken devices */ |
1190 | if (pdn && pdn->force_32bit_msi) | |
1191 | is_64 = 0; | |
1192 | ||
184cd4a3 BH |
1193 | /* Assign XIVE to PE */ |
1194 | rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num); | |
1195 | if (rc) { | |
1196 | pr_warn("%s: OPAL error %d setting XIVE %d PE\n", | |
1197 | pci_name(dev), rc, xive_num); | |
1198 | return -EIO; | |
1199 | } | |
1200 | ||
1201 | if (is_64) { | |
3a1a4661 BH |
1202 | __be64 addr64; |
1203 | ||
184cd4a3 BH |
1204 | rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1, |
1205 | &addr64, &data); | |
1206 | if (rc) { | |
1207 | pr_warn("%s: OPAL error %d getting 64-bit MSI data\n", | |
1208 | pci_name(dev), rc); | |
1209 | return -EIO; | |
1210 | } | |
3a1a4661 BH |
1211 | msg->address_hi = be64_to_cpu(addr64) >> 32; |
1212 | msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful; | |
184cd4a3 | 1213 | } else { |
3a1a4661 BH |
1214 | __be32 addr32; |
1215 | ||
184cd4a3 BH |
1216 | rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1, |
1217 | &addr32, &data); | |
1218 | if (rc) { | |
1219 | pr_warn("%s: OPAL error %d getting 32-bit MSI data\n", | |
1220 | pci_name(dev), rc); | |
1221 | return -EIO; | |
1222 | } | |
1223 | msg->address_hi = 0; | |
3a1a4661 | 1224 | msg->address_lo = be32_to_cpu(addr32); |
184cd4a3 | 1225 | } |
3a1a4661 | 1226 | msg->data = be32_to_cpu(data); |
184cd4a3 | 1227 | |
137436c9 GS |
1228 | /* |
1229 | * Change the IRQ chip for the MSI interrupts on PHB3. | |
1230 | * The corresponding IRQ chip should be populated for | |
1231 | * the first time. | |
1232 | */ | |
1233 | if (phb->type == PNV_PHB_IODA2) { | |
1234 | if (!phb->ioda.irq_chip_init) { | |
1235 | idata = irq_get_irq_data(virq); | |
1236 | ichip = irq_data_get_irq_chip(idata); | |
1237 | phb->ioda.irq_chip_init = 1; | |
1238 | phb->ioda.irq_chip = *ichip; | |
1239 | phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi; | |
1240 | } | |
1241 | ||
1242 | irq_set_chip(virq, &phb->ioda.irq_chip); | |
1243 | } | |
1244 | ||
184cd4a3 BH |
1245 | pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d)," |
1246 | " address=%x_%08x data=%x PE# %d\n", | |
1247 | pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num, | |
1248 | msg->address_hi, msg->address_lo, data, pe->pe_number); | |
1249 | ||
1250 | return 0; | |
1251 | } | |
1252 | ||
1253 | static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) | |
1254 | { | |
fb1b55d6 | 1255 | unsigned int count; |
184cd4a3 BH |
1256 | const __be32 *prop = of_get_property(phb->hose->dn, |
1257 | "ibm,opal-msi-ranges", NULL); | |
1258 | if (!prop) { | |
1259 | /* BML Fallback */ | |
1260 | prop = of_get_property(phb->hose->dn, "msi-ranges", NULL); | |
1261 | } | |
1262 | if (!prop) | |
1263 | return; | |
1264 | ||
1265 | phb->msi_base = be32_to_cpup(prop); | |
fb1b55d6 GS |
1266 | count = be32_to_cpup(prop + 1); |
1267 | if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) { | |
184cd4a3 BH |
1268 | pr_err("PCI %d: Failed to allocate MSI bitmap !\n", |
1269 | phb->hose->global_number); | |
1270 | return; | |
1271 | } | |
fb1b55d6 | 1272 | |
184cd4a3 BH |
1273 | phb->msi_setup = pnv_pci_ioda_msi_setup; |
1274 | phb->msi32_support = 1; | |
1275 | pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n", | |
fb1b55d6 | 1276 | count, phb->msi_base); |
184cd4a3 BH |
1277 | } |
1278 | #else | |
1279 | static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { } | |
1280 | #endif /* CONFIG_PCI_MSI */ | |
1281 | ||
11685bec GS |
1282 | /* |
1283 | * This function is supposed to be called on basis of PE from top | |
1284 | * to bottom style. So the the I/O or MMIO segment assigned to | |
1285 | * parent PE could be overrided by its child PEs if necessary. | |
1286 | */ | |
cad5cef6 GKH |
1287 | static void pnv_ioda_setup_pe_seg(struct pci_controller *hose, |
1288 | struct pnv_ioda_pe *pe) | |
11685bec GS |
1289 | { |
1290 | struct pnv_phb *phb = hose->private_data; | |
1291 | struct pci_bus_region region; | |
1292 | struct resource *res; | |
1293 | int i, index; | |
1294 | int rc; | |
1295 | ||
1296 | /* | |
1297 | * NOTE: We only care PCI bus based PE for now. For PCI | |
1298 | * device based PE, for example SRIOV sensitive VF should | |
1299 | * be figured out later. | |
1300 | */ | |
1301 | BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))); | |
1302 | ||
1303 | pci_bus_for_each_resource(pe->pbus, res, i) { | |
1304 | if (!res || !res->flags || | |
1305 | res->start > res->end) | |
1306 | continue; | |
1307 | ||
1308 | if (res->flags & IORESOURCE_IO) { | |
1309 | region.start = res->start - phb->ioda.io_pci_base; | |
1310 | region.end = res->end - phb->ioda.io_pci_base; | |
1311 | index = region.start / phb->ioda.io_segsize; | |
1312 | ||
1313 | while (index < phb->ioda.total_pe && | |
1314 | region.start <= region.end) { | |
1315 | phb->ioda.io_segmap[index] = pe->pe_number; | |
1316 | rc = opal_pci_map_pe_mmio_window(phb->opal_id, | |
1317 | pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index); | |
1318 | if (rc != OPAL_SUCCESS) { | |
1319 | pr_err("%s: OPAL error %d when mapping IO " | |
1320 | "segment #%d to PE#%d\n", | |
1321 | __func__, rc, index, pe->pe_number); | |
1322 | break; | |
1323 | } | |
1324 | ||
1325 | region.start += phb->ioda.io_segsize; | |
1326 | index++; | |
1327 | } | |
1328 | } else if (res->flags & IORESOURCE_MEM) { | |
1329 | region.start = res->start - | |
3fd47f06 | 1330 | hose->mem_offset[0] - |
11685bec GS |
1331 | phb->ioda.m32_pci_base; |
1332 | region.end = res->end - | |
3fd47f06 | 1333 | hose->mem_offset[0] - |
11685bec GS |
1334 | phb->ioda.m32_pci_base; |
1335 | index = region.start / phb->ioda.m32_segsize; | |
1336 | ||
1337 | while (index < phb->ioda.total_pe && | |
1338 | region.start <= region.end) { | |
1339 | phb->ioda.m32_segmap[index] = pe->pe_number; | |
1340 | rc = opal_pci_map_pe_mmio_window(phb->opal_id, | |
1341 | pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index); | |
1342 | if (rc != OPAL_SUCCESS) { | |
1343 | pr_err("%s: OPAL error %d when mapping M32 " | |
1344 | "segment#%d to PE#%d", | |
1345 | __func__, rc, index, pe->pe_number); | |
1346 | break; | |
1347 | } | |
1348 | ||
1349 | region.start += phb->ioda.m32_segsize; | |
1350 | index++; | |
1351 | } | |
1352 | } | |
1353 | } | |
1354 | } | |
1355 | ||
cad5cef6 | 1356 | static void pnv_pci_ioda_setup_seg(void) |
11685bec GS |
1357 | { |
1358 | struct pci_controller *tmp, *hose; | |
1359 | struct pnv_phb *phb; | |
1360 | struct pnv_ioda_pe *pe; | |
1361 | ||
1362 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { | |
1363 | phb = hose->private_data; | |
1364 | list_for_each_entry(pe, &phb->ioda.pe_list, list) { | |
1365 | pnv_ioda_setup_pe_seg(hose, pe); | |
1366 | } | |
1367 | } | |
1368 | } | |
1369 | ||
cad5cef6 | 1370 | static void pnv_pci_ioda_setup_DMA(void) |
13395c48 GS |
1371 | { |
1372 | struct pci_controller *hose, *tmp; | |
db1266c8 | 1373 | struct pnv_phb *phb; |
13395c48 GS |
1374 | |
1375 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { | |
1376 | pnv_ioda_setup_dma(hose->private_data); | |
db1266c8 GS |
1377 | |
1378 | /* Mark the PHB initialization done */ | |
1379 | phb = hose->private_data; | |
1380 | phb->initialized = 1; | |
13395c48 GS |
1381 | } |
1382 | } | |
1383 | ||
37c367f2 GS |
1384 | static void pnv_pci_ioda_create_dbgfs(void) |
1385 | { | |
1386 | #ifdef CONFIG_DEBUG_FS | |
1387 | struct pci_controller *hose, *tmp; | |
1388 | struct pnv_phb *phb; | |
1389 | char name[16]; | |
1390 | ||
1391 | list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { | |
1392 | phb = hose->private_data; | |
1393 | ||
1394 | sprintf(name, "PCI%04x", hose->global_number); | |
1395 | phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root); | |
1396 | if (!phb->dbgfs) | |
1397 | pr_warning("%s: Error on creating debugfs on PHB#%x\n", | |
1398 | __func__, hose->global_number); | |
1399 | } | |
1400 | #endif /* CONFIG_DEBUG_FS */ | |
1401 | } | |
1402 | ||
cad5cef6 | 1403 | static void pnv_pci_ioda_fixup(void) |
fb446ad0 GS |
1404 | { |
1405 | pnv_pci_ioda_setup_PEs(); | |
11685bec | 1406 | pnv_pci_ioda_setup_seg(); |
13395c48 | 1407 | pnv_pci_ioda_setup_DMA(); |
e9cc17d4 | 1408 | |
37c367f2 GS |
1409 | pnv_pci_ioda_create_dbgfs(); |
1410 | ||
e9cc17d4 | 1411 | #ifdef CONFIG_EEH |
e9cc17d4 | 1412 | eeh_init(); |
dadcd6d6 | 1413 | eeh_addr_cache_build(); |
e9cc17d4 | 1414 | #endif |
fb446ad0 GS |
1415 | } |
1416 | ||
271fd03a GS |
1417 | /* |
1418 | * Returns the alignment for I/O or memory windows for P2P | |
1419 | * bridges. That actually depends on how PEs are segmented. | |
1420 | * For now, we return I/O or M32 segment size for PE sensitive | |
1421 | * P2P bridges. Otherwise, the default values (4KiB for I/O, | |
1422 | * 1MiB for memory) will be returned. | |
1423 | * | |
1424 | * The current PCI bus might be put into one PE, which was | |
1425 | * create against the parent PCI bridge. For that case, we | |
1426 | * needn't enlarge the alignment so that we can save some | |
1427 | * resources. | |
1428 | */ | |
1429 | static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus, | |
1430 | unsigned long type) | |
1431 | { | |
1432 | struct pci_dev *bridge; | |
1433 | struct pci_controller *hose = pci_bus_to_host(bus); | |
1434 | struct pnv_phb *phb = hose->private_data; | |
1435 | int num_pci_bridges = 0; | |
1436 | ||
1437 | bridge = bus->self; | |
1438 | while (bridge) { | |
1439 | if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) { | |
1440 | num_pci_bridges++; | |
1441 | if (num_pci_bridges >= 2) | |
1442 | return 1; | |
1443 | } | |
1444 | ||
1445 | bridge = bridge->bus->self; | |
1446 | } | |
1447 | ||
262af557 GC |
1448 | /* We fail back to M32 if M64 isn't supported */ |
1449 | if (phb->ioda.m64_segsize && | |
1450 | pnv_pci_is_mem_pref_64(type)) | |
1451 | return phb->ioda.m64_segsize; | |
271fd03a GS |
1452 | if (type & IORESOURCE_MEM) |
1453 | return phb->ioda.m32_segsize; | |
1454 | ||
1455 | return phb->ioda.io_segsize; | |
1456 | } | |
1457 | ||
184cd4a3 BH |
1458 | /* Prevent enabling devices for which we couldn't properly |
1459 | * assign a PE | |
1460 | */ | |
cad5cef6 | 1461 | static int pnv_pci_enable_device_hook(struct pci_dev *dev) |
184cd4a3 | 1462 | { |
db1266c8 GS |
1463 | struct pci_controller *hose = pci_bus_to_host(dev->bus); |
1464 | struct pnv_phb *phb = hose->private_data; | |
1465 | struct pci_dn *pdn; | |
184cd4a3 | 1466 | |
db1266c8 GS |
1467 | /* The function is probably called while the PEs have |
1468 | * not be created yet. For example, resource reassignment | |
1469 | * during PCI probe period. We just skip the check if | |
1470 | * PEs isn't ready. | |
1471 | */ | |
1472 | if (!phb->initialized) | |
1473 | return 0; | |
1474 | ||
b72c1f65 | 1475 | pdn = pci_get_pdn(dev); |
184cd4a3 BH |
1476 | if (!pdn || pdn->pe_number == IODA_INVALID_PE) |
1477 | return -EINVAL; | |
db1266c8 | 1478 | |
184cd4a3 BH |
1479 | return 0; |
1480 | } | |
1481 | ||
1482 | static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus, | |
1483 | u32 devfn) | |
1484 | { | |
1485 | return phb->ioda.pe_rmap[(bus->number << 8) | devfn]; | |
1486 | } | |
1487 | ||
73ed148a BH |
1488 | static void pnv_pci_ioda_shutdown(struct pnv_phb *phb) |
1489 | { | |
1490 | opal_pci_reset(phb->opal_id, OPAL_PCI_IODA_TABLE_RESET, | |
1491 | OPAL_ASSERT_RESET); | |
1492 | } | |
1493 | ||
e9cc17d4 GS |
1494 | void __init pnv_pci_init_ioda_phb(struct device_node *np, |
1495 | u64 hub_id, int ioda_type) | |
184cd4a3 BH |
1496 | { |
1497 | struct pci_controller *hose; | |
184cd4a3 | 1498 | struct pnv_phb *phb; |
8184616f | 1499 | unsigned long size, m32map_off, pemap_off, iomap_off = 0; |
c681b93c | 1500 | const __be64 *prop64; |
3a1a4661 | 1501 | const __be32 *prop32; |
f1b7cc3e | 1502 | int len; |
184cd4a3 BH |
1503 | u64 phb_id; |
1504 | void *aux; | |
1505 | long rc; | |
1506 | ||
58d714ec | 1507 | pr_info("Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name); |
184cd4a3 BH |
1508 | |
1509 | prop64 = of_get_property(np, "ibm,opal-phbid", NULL); | |
1510 | if (!prop64) { | |
1511 | pr_err(" Missing \"ibm,opal-phbid\" property !\n"); | |
1512 | return; | |
1513 | } | |
1514 | phb_id = be64_to_cpup(prop64); | |
1515 | pr_debug(" PHB-ID : 0x%016llx\n", phb_id); | |
1516 | ||
1517 | phb = alloc_bootmem(sizeof(struct pnv_phb)); | |
58d714ec GS |
1518 | if (!phb) { |
1519 | pr_err(" Out of memory !\n"); | |
1520 | return; | |
184cd4a3 | 1521 | } |
58d714ec GS |
1522 | |
1523 | /* Allocate PCI controller */ | |
1524 | memset(phb, 0, sizeof(struct pnv_phb)); | |
1525 | phb->hose = hose = pcibios_alloc_controller(np); | |
1526 | if (!phb->hose) { | |
1527 | pr_err(" Can't allocate PCI controller for %s\n", | |
184cd4a3 | 1528 | np->full_name); |
58d714ec | 1529 | free_bootmem((unsigned long)phb, sizeof(struct pnv_phb)); |
184cd4a3 BH |
1530 | return; |
1531 | } | |
1532 | ||
1533 | spin_lock_init(&phb->lock); | |
f1b7cc3e GS |
1534 | prop32 = of_get_property(np, "bus-range", &len); |
1535 | if (prop32 && len == 8) { | |
3a1a4661 BH |
1536 | hose->first_busno = be32_to_cpu(prop32[0]); |
1537 | hose->last_busno = be32_to_cpu(prop32[1]); | |
f1b7cc3e GS |
1538 | } else { |
1539 | pr_warn(" Broken <bus-range> on %s\n", np->full_name); | |
1540 | hose->first_busno = 0; | |
1541 | hose->last_busno = 0xff; | |
1542 | } | |
184cd4a3 | 1543 | hose->private_data = phb; |
e9cc17d4 | 1544 | phb->hub_id = hub_id; |
184cd4a3 | 1545 | phb->opal_id = phb_id; |
aa0c033f | 1546 | phb->type = ioda_type; |
184cd4a3 | 1547 | |
cee72d5b BH |
1548 | /* Detect specific models for error handling */ |
1549 | if (of_device_is_compatible(np, "ibm,p7ioc-pciex")) | |
1550 | phb->model = PNV_PHB_MODEL_P7IOC; | |
f3d40c25 | 1551 | else if (of_device_is_compatible(np, "ibm,power8-pciex")) |
aa0c033f | 1552 | phb->model = PNV_PHB_MODEL_PHB3; |
cee72d5b BH |
1553 | else |
1554 | phb->model = PNV_PHB_MODEL_UNKNOWN; | |
1555 | ||
aa0c033f | 1556 | /* Parse 32-bit and IO ranges (if any) */ |
2f1ec02e | 1557 | pci_process_bridge_OF_ranges(hose, np, !hose->global_number); |
184cd4a3 | 1558 | |
aa0c033f | 1559 | /* Get registers */ |
184cd4a3 BH |
1560 | phb->regs = of_iomap(np, 0); |
1561 | if (phb->regs == NULL) | |
1562 | pr_err(" Failed to map registers !\n"); | |
1563 | ||
184cd4a3 | 1564 | /* Initialize more IODA stuff */ |
36954dc7 | 1565 | phb->ioda.total_pe = 1; |
aa0c033f | 1566 | prop32 = of_get_property(np, "ibm,opal-num-pes", NULL); |
36954dc7 | 1567 | if (prop32) |
3a1a4661 | 1568 | phb->ioda.total_pe = be32_to_cpup(prop32); |
36954dc7 GS |
1569 | prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL); |
1570 | if (prop32) | |
1571 | phb->ioda.reserved_pe = be32_to_cpup(prop32); | |
262af557 GC |
1572 | |
1573 | /* Parse 64-bit MMIO range */ | |
1574 | pnv_ioda_parse_m64_window(phb); | |
1575 | ||
184cd4a3 | 1576 | phb->ioda.m32_size = resource_size(&hose->mem_resources[0]); |
aa0c033f | 1577 | /* FW Has already off top 64k of M32 space (MSI space) */ |
184cd4a3 BH |
1578 | phb->ioda.m32_size += 0x10000; |
1579 | ||
1580 | phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe; | |
3fd47f06 | 1581 | phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0]; |
184cd4a3 BH |
1582 | phb->ioda.io_size = hose->pci_io_size; |
1583 | phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe; | |
1584 | phb->ioda.io_pci_base = 0; /* XXX calculate this ? */ | |
1585 | ||
c35d2a8c | 1586 | /* Allocate aux data & arrays. We don't have IO ports on PHB3 */ |
184cd4a3 BH |
1587 | size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long)); |
1588 | m32map_off = size; | |
e47747f4 | 1589 | size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]); |
c35d2a8c GS |
1590 | if (phb->type == PNV_PHB_IODA1) { |
1591 | iomap_off = size; | |
1592 | size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]); | |
1593 | } | |
184cd4a3 BH |
1594 | pemap_off = size; |
1595 | size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe); | |
1596 | aux = alloc_bootmem(size); | |
1597 | memset(aux, 0, size); | |
1598 | phb->ioda.pe_alloc = aux; | |
1599 | phb->ioda.m32_segmap = aux + m32map_off; | |
c35d2a8c GS |
1600 | if (phb->type == PNV_PHB_IODA1) |
1601 | phb->ioda.io_segmap = aux + iomap_off; | |
184cd4a3 | 1602 | phb->ioda.pe_array = aux + pemap_off; |
36954dc7 | 1603 | set_bit(phb->ioda.reserved_pe, phb->ioda.pe_alloc); |
184cd4a3 | 1604 | |
7ebdf956 | 1605 | INIT_LIST_HEAD(&phb->ioda.pe_dma_list); |
184cd4a3 BH |
1606 | INIT_LIST_HEAD(&phb->ioda.pe_list); |
1607 | ||
1608 | /* Calculate how many 32-bit TCE segments we have */ | |
1609 | phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28; | |
1610 | ||
aa0c033f | 1611 | #if 0 /* We should really do that ... */ |
184cd4a3 BH |
1612 | rc = opal_pci_set_phb_mem_window(opal->phb_id, |
1613 | window_type, | |
1614 | window_num, | |
1615 | starting_real_address, | |
1616 | starting_pci_address, | |
1617 | segment_size); | |
1618 | #endif | |
1619 | ||
262af557 GC |
1620 | pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n", |
1621 | phb->ioda.total_pe, phb->ioda.reserved_pe, | |
1622 | phb->ioda.m32_size, phb->ioda.m32_segsize); | |
1623 | if (phb->ioda.m64_size) | |
1624 | pr_info(" M64: 0x%lx [segment=0x%lx]\n", | |
1625 | phb->ioda.m64_size, phb->ioda.m64_segsize); | |
1626 | if (phb->ioda.io_size) | |
1627 | pr_info(" IO: 0x%x [segment=0x%x]\n", | |
1628 | phb->ioda.io_size, phb->ioda.io_segsize); | |
1629 | ||
184cd4a3 | 1630 | |
184cd4a3 | 1631 | phb->hose->ops = &pnv_pci_ops; |
e9cc17d4 GS |
1632 | #ifdef CONFIG_EEH |
1633 | phb->eeh_ops = &ioda_eeh_ops; | |
1634 | #endif | |
184cd4a3 BH |
1635 | |
1636 | /* Setup RID -> PE mapping function */ | |
1637 | phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe; | |
1638 | ||
1639 | /* Setup TCEs */ | |
1640 | phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup; | |
cd15b048 | 1641 | phb->dma_set_mask = pnv_pci_ioda_dma_set_mask; |
184cd4a3 | 1642 | |
73ed148a BH |
1643 | /* Setup shutdown function for kexec */ |
1644 | phb->shutdown = pnv_pci_ioda_shutdown; | |
1645 | ||
184cd4a3 BH |
1646 | /* Setup MSI support */ |
1647 | pnv_pci_init_ioda_msis(phb); | |
1648 | ||
c40a4210 GS |
1649 | /* |
1650 | * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here | |
1651 | * to let the PCI core do resource assignment. It's supposed | |
1652 | * that the PCI core will do correct I/O and MMIO alignment | |
1653 | * for the P2P bridge bars so that each PCI bus (excluding | |
1654 | * the child P2P bridges) can form individual PE. | |
184cd4a3 | 1655 | */ |
fb446ad0 | 1656 | ppc_md.pcibios_fixup = pnv_pci_ioda_fixup; |
184cd4a3 | 1657 | ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook; |
271fd03a | 1658 | ppc_md.pcibios_window_alignment = pnv_pci_window_alignment; |
d92a208d | 1659 | ppc_md.pcibios_reset_secondary_bus = pnv_pci_reset_secondary_bus; |
c40a4210 | 1660 | pci_add_flags(PCI_REASSIGN_ALL_RSRC); |
184cd4a3 BH |
1661 | |
1662 | /* Reset IODA tables to a clean state */ | |
f11fe552 | 1663 | rc = opal_pci_reset(phb_id, OPAL_PCI_IODA_TABLE_RESET, OPAL_ASSERT_RESET); |
184cd4a3 | 1664 | if (rc) |
f11fe552 | 1665 | pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc); |
361f2a2a GS |
1666 | |
1667 | /* If we're running in kdump kerenl, the previous kerenl never | |
1668 | * shutdown PCI devices correctly. We already got IODA table | |
1669 | * cleaned out. So we have to issue PHB reset to stop all PCI | |
1670 | * transactions from previous kerenl. | |
1671 | */ | |
1672 | if (is_kdump_kernel()) { | |
1673 | pr_info(" Issue PHB reset ...\n"); | |
1674 | ioda_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL); | |
1675 | ioda_eeh_phb_reset(hose, OPAL_DEASSERT_RESET); | |
1676 | } | |
262af557 GC |
1677 | |
1678 | /* Configure M64 window */ | |
1679 | if (phb->init_m64 && phb->init_m64(phb)) | |
1680 | hose->mem_resources[1].flags = 0; | |
aa0c033f GS |
1681 | } |
1682 | ||
67975005 | 1683 | void __init pnv_pci_init_ioda2_phb(struct device_node *np) |
aa0c033f | 1684 | { |
e9cc17d4 | 1685 | pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2); |
184cd4a3 BH |
1686 | } |
1687 | ||
1688 | void __init pnv_pci_init_ioda_hub(struct device_node *np) | |
1689 | { | |
1690 | struct device_node *phbn; | |
c681b93c | 1691 | const __be64 *prop64; |
184cd4a3 BH |
1692 | u64 hub_id; |
1693 | ||
1694 | pr_info("Probing IODA IO-Hub %s\n", np->full_name); | |
1695 | ||
1696 | prop64 = of_get_property(np, "ibm,opal-hubid", NULL); | |
1697 | if (!prop64) { | |
1698 | pr_err(" Missing \"ibm,opal-hubid\" property !\n"); | |
1699 | return; | |
1700 | } | |
1701 | hub_id = be64_to_cpup(prop64); | |
1702 | pr_devel(" HUB-ID : 0x%016llx\n", hub_id); | |
1703 | ||
1704 | /* Count child PHBs */ | |
1705 | for_each_child_of_node(np, phbn) { | |
1706 | /* Look for IODA1 PHBs */ | |
1707 | if (of_device_is_compatible(phbn, "ibm,ioda-phb")) | |
e9cc17d4 | 1708 | pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1); |
184cd4a3 BH |
1709 | } |
1710 | } |