1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Support PCI/PCIe on PowerNV platforms
4 *
5 * Copyright 2011 Benjamin Herrenschmidt, IBM Corp.
6 */
7
8 #undef DEBUG
9
10 #include <linux/kernel.h>
11 #include <linux/pci.h>
12 #include <linux/crash_dump.h>
13 #include <linux/delay.h>
14 #include <linux/string.h>
15 #include <linux/init.h>
16 #include <linux/memblock.h>
17 #include <linux/irq.h>
18 #include <linux/io.h>
19 #include <linux/msi.h>
20 #include <linux/iommu.h>
21 #include <linux/rculist.h>
22 #include <linux/sizes.h>
23 #include <linux/debugfs.h>
24 #include <linux/of_address.h>
25 #include <linux/of_irq.h>
26
27 #include <asm/sections.h>
28 #include <asm/io.h>
29 #include <asm/pci-bridge.h>
30 #include <asm/machdep.h>
31 #include <asm/msi_bitmap.h>
32 #include <asm/ppc-pci.h>
33 #include <asm/opal.h>
34 #include <asm/iommu.h>
35 #include <asm/tce.h>
36 #include <asm/xics.h>
37 #include <asm/firmware.h>
38 #include <asm/pnv-pci.h>
39 #include <asm/mmzone.h>
40 #include <asm/xive.h>
41
42 #include "powernv.h"
43 #include "pci.h"
44 #include "../../../../drivers/pci/pci.h"
45
46 /* This array is indexed with enum pnv_phb_type */
47 static const char * const pnv_phb_names[] = { "IODA2", "NPU_OCAPI" };
48
49 static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable);
50 static void pnv_pci_configure_bus(struct pci_bus *bus);
51
pe_level_printk(const struct pnv_ioda_pe * pe,const char * level,const char * fmt,...)52 void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,
53 const char *fmt, ...)
54 {
55 struct va_format vaf;
56 va_list args;
57 char pfix[32];
58
59 va_start(args, fmt);
60
61 vaf.fmt = fmt;
62 vaf.va = &args;
63
64 if (pe->flags & PNV_IODA_PE_DEV)
65 strscpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
66 else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
67 sprintf(pfix, "%04x:%02x ",
68 pci_domain_nr(pe->pbus), pe->pbus->number);
69 #ifdef CONFIG_PCI_IOV
70 else if (pe->flags & PNV_IODA_PE_VF)
71 sprintf(pfix, "%04x:%02x:%2x.%d",
72 pci_domain_nr(pe->parent_dev->bus),
73 (pe->rid & 0xff00) >> 8,
74 PCI_SLOT(pe->rid), PCI_FUNC(pe->rid));
75 #endif /* CONFIG_PCI_IOV*/
76
77 printk("%spci %s: [PE# %.2x] %pV",
78 level, pfix, pe->pe_number, &vaf);
79
80 va_end(args);
81 }
82
83 static bool pnv_iommu_bypass_disabled __read_mostly;
84 static bool pci_reset_phbs __read_mostly;
85
iommu_setup(char * str)86 static int __init iommu_setup(char *str)
87 {
88 if (!str)
89 return -EINVAL;
90
91 while (*str) {
92 if (!strncmp(str, "nobypass", 8)) {
93 pnv_iommu_bypass_disabled = true;
94 pr_info("PowerNV: IOMMU bypass window disabled.\n");
95 break;
96 }
97 str += strcspn(str, ",");
98 if (*str == ',')
99 str++;
100 }
101
102 return 0;
103 }
104 early_param("iommu", iommu_setup);
105
pci_reset_phbs_setup(char * str)106 static int __init pci_reset_phbs_setup(char *str)
107 {
108 pci_reset_phbs = true;
109 return 0;
110 }
111
112 early_param("ppc_pci_reset_phbs", pci_reset_phbs_setup);
113
pnv_ioda_init_pe(struct pnv_phb * phb,int pe_no)114 static struct pnv_ioda_pe *pnv_ioda_init_pe(struct pnv_phb *phb, int pe_no)
115 {
116 s64 rc;
117
118 phb->ioda.pe_array[pe_no].phb = phb;
119 phb->ioda.pe_array[pe_no].pe_number = pe_no;
120 phb->ioda.pe_array[pe_no].dma_setup_done = false;
121
122 /*
123 * Clear the PE frozen state as it might be put into frozen state
124 * in the last PCI remove path. It's not harmful to do so when the
125 * PE is already in unfrozen state.
126 */
127 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no,
128 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
129 if (rc != OPAL_SUCCESS && rc != OPAL_UNSUPPORTED)
130 pr_warn("%s: Error %lld unfreezing PHB#%x-PE#%x\n",
131 __func__, rc, phb->hose->global_number, pe_no);
132
133 return &phb->ioda.pe_array[pe_no];
134 }
135
pnv_ioda_reserve_pe(struct pnv_phb * phb,int pe_no)136 static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int pe_no)
137 {
138 if (!(pe_no >= 0 && pe_no < phb->ioda.total_pe_num)) {
139 pr_warn("%s: Invalid PE %x on PHB#%x\n",
140 __func__, pe_no, phb->hose->global_number);
141 return;
142 }
143
144 mutex_lock(&phb->ioda.pe_alloc_mutex);
145 if (test_and_set_bit(pe_no, phb->ioda.pe_alloc))
146 pr_debug("%s: PE %x was reserved on PHB#%x\n",
147 __func__, pe_no, phb->hose->global_number);
148 mutex_unlock(&phb->ioda.pe_alloc_mutex);
149
150 pnv_ioda_init_pe(phb, pe_no);
151 }
152
pnv_ioda_alloc_pe(struct pnv_phb * phb,int count)153 struct pnv_ioda_pe *pnv_ioda_alloc_pe(struct pnv_phb *phb, int count)
154 {
155 struct pnv_ioda_pe *ret = NULL;
156 int run = 0, pe, i;
157
158 mutex_lock(&phb->ioda.pe_alloc_mutex);
159
160 /* scan backwards for a run of @count cleared bits */
161 for (pe = phb->ioda.total_pe_num - 1; pe >= 0; pe--) {
162 if (test_bit(pe, phb->ioda.pe_alloc)) {
163 run = 0;
164 continue;
165 }
166
167 run++;
168 if (run == count)
169 break;
170 }
171 if (run != count)
172 goto out;
173
174 for (i = pe; i < pe + count; i++) {
175 set_bit(i, phb->ioda.pe_alloc);
176 pnv_ioda_init_pe(phb, i);
177 }
178 ret = &phb->ioda.pe_array[pe];
179
180 out:
181 mutex_unlock(&phb->ioda.pe_alloc_mutex);
182 return ret;
183 }
184
pnv_ioda_free_pe(struct pnv_ioda_pe * pe)185 void pnv_ioda_free_pe(struct pnv_ioda_pe *pe)
186 {
187 struct pnv_phb *phb = pe->phb;
188 unsigned int pe_num = pe->pe_number;
189
190 WARN_ON(pe->pdev);
191 memset(pe, 0, sizeof(struct pnv_ioda_pe));
192
193 mutex_lock(&phb->ioda.pe_alloc_mutex);
194 clear_bit(pe_num, phb->ioda.pe_alloc);
195 mutex_unlock(&phb->ioda.pe_alloc_mutex);
196 }
197
198 /* The default M64 BAR is shared by all PEs */
pnv_ioda2_init_m64(struct pnv_phb * phb)199 static int pnv_ioda2_init_m64(struct pnv_phb *phb)
200 {
201 const char *desc;
202 struct resource *r;
203 s64 rc;
204
205 /* Configure the default M64 BAR */
206 rc = opal_pci_set_phb_mem_window(phb->opal_id,
207 OPAL_M64_WINDOW_TYPE,
208 phb->ioda.m64_bar_idx,
209 phb->ioda.m64_base,
210 0, /* unused */
211 phb->ioda.m64_size);
212 if (rc != OPAL_SUCCESS) {
213 desc = "configuring";
214 goto fail;
215 }
216
217 /* Enable the default M64 BAR */
218 rc = opal_pci_phb_mmio_enable(phb->opal_id,
219 OPAL_M64_WINDOW_TYPE,
220 phb->ioda.m64_bar_idx,
221 OPAL_ENABLE_M64_SPLIT);
222 if (rc != OPAL_SUCCESS) {
223 desc = "enabling";
224 goto fail;
225 }
226
227 /*
228 * Exclude the segments for reserved and root bus PE, which
229 * are first or last two PEs.
230 */
231 r = &phb->hose->mem_resources[1];
232 if (phb->ioda.reserved_pe_idx == 0)
233 r->start += (2 * phb->ioda.m64_segsize);
234 else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1))
235 r->end -= (2 * phb->ioda.m64_segsize);
236 else
237 pr_warn(" Cannot strip M64 segment for reserved PE#%x\n",
238 phb->ioda.reserved_pe_idx);
239
240 return 0;
241
242 fail:
243 pr_warn(" Failure %lld %s M64 BAR#%d\n",
244 rc, desc, phb->ioda.m64_bar_idx);
245 opal_pci_phb_mmio_enable(phb->opal_id,
246 OPAL_M64_WINDOW_TYPE,
247 phb->ioda.m64_bar_idx,
248 OPAL_DISABLE_M64);
249 return -EIO;
250 }
251
pnv_ioda_reserve_dev_m64_pe(struct pci_dev * pdev,unsigned long * pe_bitmap)252 static void pnv_ioda_reserve_dev_m64_pe(struct pci_dev *pdev,
253 unsigned long *pe_bitmap)
254 {
255 struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
256 struct resource *r;
257 resource_size_t base, sgsz, start, end;
258 int segno, i;
259
260 base = phb->ioda.m64_base;
261 sgsz = phb->ioda.m64_segsize;
262 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
263 r = &pdev->resource[i];
264 if (!r->parent || !pnv_pci_is_m64(phb, r))
265 continue;
266
267 start = ALIGN_DOWN(r->start - base, sgsz);
268 end = ALIGN(r->end - base, sgsz);
269 for (segno = start / sgsz; segno < end / sgsz; segno++) {
270 if (pe_bitmap)
271 set_bit(segno, pe_bitmap);
272 else
273 pnv_ioda_reserve_pe(phb, segno);
274 }
275 }
276 }
277
pnv_ioda_reserve_m64_pe(struct pci_bus * bus,unsigned long * pe_bitmap,bool all)278 static void pnv_ioda_reserve_m64_pe(struct pci_bus *bus,
279 unsigned long *pe_bitmap,
280 bool all)
281 {
282 struct pci_dev *pdev;
283
284 list_for_each_entry(pdev, &bus->devices, bus_list) {
285 pnv_ioda_reserve_dev_m64_pe(pdev, pe_bitmap);
286
287 if (all && pdev->subordinate)
288 pnv_ioda_reserve_m64_pe(pdev->subordinate,
289 pe_bitmap, all);
290 }
291 }
292
pnv_ioda_pick_m64_pe(struct pci_bus * bus,bool all)293 static struct pnv_ioda_pe *pnv_ioda_pick_m64_pe(struct pci_bus *bus, bool all)
294 {
295 struct pnv_phb *phb = pci_bus_to_pnvhb(bus);
296 struct pnv_ioda_pe *master_pe, *pe;
297 unsigned long size, *pe_alloc;
298 int i;
299
300 /* Root bus shouldn't use M64 */
301 if (pci_is_root_bus(bus))
302 return NULL;
303
304 /* Allocate bitmap */
305 size = ALIGN(phb->ioda.total_pe_num / 8, sizeof(unsigned long));
306 pe_alloc = kzalloc(size, GFP_KERNEL);
307 if (!pe_alloc) {
308 pr_warn("%s: Out of memory !\n",
309 __func__);
310 return NULL;
311 }
312
313 /* Figure out reserved PE numbers by the PE */
314 pnv_ioda_reserve_m64_pe(bus, pe_alloc, all);
315
316 /*
317 * the current bus might not own M64 window and that's all
318 * contributed by its child buses. For the case, we needn't
319 * pick M64 dependent PE#.
320 */
321 if (bitmap_empty(pe_alloc, phb->ioda.total_pe_num)) {
322 kfree(pe_alloc);
323 return NULL;
324 }
325
326 /*
327 * Figure out the master PE and put all slave PEs to master
328 * PE's list to form compound PE.
329 */
330 master_pe = NULL;
331 i = -1;
332 while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe_num, i + 1)) <
333 phb->ioda.total_pe_num) {
334 pe = &phb->ioda.pe_array[i];
335
336 phb->ioda.m64_segmap[pe->pe_number] = pe->pe_number;
337 if (!master_pe) {
338 pe->flags |= PNV_IODA_PE_MASTER;
339 INIT_LIST_HEAD(&pe->slaves);
340 master_pe = pe;
341 } else {
342 pe->flags |= PNV_IODA_PE_SLAVE;
343 pe->master = master_pe;
344 list_add_tail(&pe->list, &master_pe->slaves);
345 }
346 }
347
348 kfree(pe_alloc);
349 return master_pe;
350 }
351
pnv_ioda_parse_m64_window(struct pnv_phb * phb)352 static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
353 {
354 struct pci_controller *hose = phb->hose;
355 struct device_node *dn = hose->dn;
356 struct resource *res;
357 u32 m64_range[2], i;
358 const __be32 *r;
359 u64 pci_addr;
360
361 if (phb->type != PNV_PHB_IODA2) {
362 pr_info(" Not support M64 window\n");
363 return;
364 }
365
366 if (!firmware_has_feature(FW_FEATURE_OPAL)) {
367 pr_info(" Firmware too old to support M64 window\n");
368 return;
369 }
370
371 r = of_get_property(dn, "ibm,opal-m64-window", NULL);
372 if (!r) {
373 pr_info(" No <ibm,opal-m64-window> on %pOF\n",
374 dn);
375 return;
376 }
377
378 /*
379 * Find the available M64 BAR range and pickup the last one for
380 * covering the whole 64-bits space. We support only one range.
381 */
382 if (of_property_read_u32_array(dn, "ibm,opal-available-m64-ranges",
383 m64_range, 2)) {
384 /* In absence of the property, assume 0..15 */
385 m64_range[0] = 0;
386 m64_range[1] = 16;
387 }
388 /* We only support 64 bits in our allocator */
389 if (m64_range[1] > 63) {
390 pr_warn("%s: Limiting M64 range to 63 (from %d) on PHB#%x\n",
391 __func__, m64_range[1], phb->hose->global_number);
392 m64_range[1] = 63;
393 }
394 /* Empty range, no m64 */
395 if (m64_range[1] <= m64_range[0]) {
396 pr_warn("%s: M64 empty, disabling M64 usage on PHB#%x\n",
397 __func__, phb->hose->global_number);
398 return;
399 }
400
401 /* Configure M64 informations */
402 res = &hose->mem_resources[1];
403 res->name = dn->full_name;
404 res->start = of_translate_address(dn, r + 2);
405 res->end = res->start + of_read_number(r + 4, 2) - 1;
406 res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
407 pci_addr = of_read_number(r, 2);
408 hose->mem_offset[1] = res->start - pci_addr;
409
410 phb->ioda.m64_size = resource_size(res);
411 phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe_num;
412 phb->ioda.m64_base = pci_addr;
413
414 /* This lines up nicely with the display from processing OF ranges */
415 pr_info(" MEM 0x%016llx..0x%016llx -> 0x%016llx (M64 #%d..%d)\n",
416 res->start, res->end, pci_addr, m64_range[0],
417 m64_range[0] + m64_range[1] - 1);
418
419 /* Mark all M64 used up by default */
420 phb->ioda.m64_bar_alloc = (unsigned long)-1;
421
422 /* Use last M64 BAR to cover M64 window */
423 m64_range[1]--;
424 phb->ioda.m64_bar_idx = m64_range[0] + m64_range[1];
425
426 pr_info(" Using M64 #%d as default window\n", phb->ioda.m64_bar_idx);
427
428 /* Mark remaining ones free */
429 for (i = m64_range[0]; i < m64_range[1]; i++)
430 clear_bit(i, &phb->ioda.m64_bar_alloc);
431
432 /*
433 * Setup init functions for M64 based on IODA version, IODA3 uses
434 * the IODA2 code.
435 */
436 phb->init_m64 = pnv_ioda2_init_m64;
437 }
438
pnv_ioda_freeze_pe(struct pnv_phb * phb,int pe_no)439 static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
440 {
441 struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
442 struct pnv_ioda_pe *slave;
443 s64 rc;
444
445 /* Fetch master PE */
446 if (pe->flags & PNV_IODA_PE_SLAVE) {
447 pe = pe->master;
448 if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))
449 return;
450
451 pe_no = pe->pe_number;
452 }
453
454 /* Freeze master PE */
455 rc = opal_pci_eeh_freeze_set(phb->opal_id,
456 pe_no,
457 OPAL_EEH_ACTION_SET_FREEZE_ALL);
458 if (rc != OPAL_SUCCESS) {
459 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
460 __func__, rc, phb->hose->global_number, pe_no);
461 return;
462 }
463
464 /* Freeze slave PEs */
465 if (!(pe->flags & PNV_IODA_PE_MASTER))
466 return;
467
468 list_for_each_entry(slave, &pe->slaves, list) {
469 rc = opal_pci_eeh_freeze_set(phb->opal_id,
470 slave->pe_number,
471 OPAL_EEH_ACTION_SET_FREEZE_ALL);
472 if (rc != OPAL_SUCCESS)
473 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
474 __func__, rc, phb->hose->global_number,
475 slave->pe_number);
476 }
477 }
478
pnv_ioda_unfreeze_pe(struct pnv_phb * phb,int pe_no,int opt)479 static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
480 {
481 struct pnv_ioda_pe *pe, *slave;
482 s64 rc;
483
484 /* Find master PE */
485 pe = &phb->ioda.pe_array[pe_no];
486 if (pe->flags & PNV_IODA_PE_SLAVE) {
487 pe = pe->master;
488 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
489 pe_no = pe->pe_number;
490 }
491
492 /* Clear frozen state for master PE */
493 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
494 if (rc != OPAL_SUCCESS) {
495 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
496 __func__, rc, opt, phb->hose->global_number, pe_no);
497 return -EIO;
498 }
499
500 if (!(pe->flags & PNV_IODA_PE_MASTER))
501 return 0;
502
503 /* Clear frozen state for slave PEs */
504 list_for_each_entry(slave, &pe->slaves, list) {
505 rc = opal_pci_eeh_freeze_clear(phb->opal_id,
506 slave->pe_number,
507 opt);
508 if (rc != OPAL_SUCCESS) {
509 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
510 __func__, rc, opt, phb->hose->global_number,
511 slave->pe_number);
512 return -EIO;
513 }
514 }
515
516 return 0;
517 }
518
pnv_ioda_get_pe_state(struct pnv_phb * phb,int pe_no)519 static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
520 {
521 struct pnv_ioda_pe *slave, *pe;
522 u8 fstate = 0, state;
523 __be16 pcierr = 0;
524 s64 rc;
525
526 /* Sanity check on PE number */
527 if (pe_no < 0 || pe_no >= phb->ioda.total_pe_num)
528 return OPAL_EEH_STOPPED_PERM_UNAVAIL;
529
530 /*
531 * Fetch the master PE and the PE instance might be
532 * not initialized yet.
533 */
534 pe = &phb->ioda.pe_array[pe_no];
535 if (pe->flags & PNV_IODA_PE_SLAVE) {
536 pe = pe->master;
537 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
538 pe_no = pe->pe_number;
539 }
540
541 /* Check the master PE */
542 rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
543 &state, &pcierr, NULL);
544 if (rc != OPAL_SUCCESS) {
545 pr_warn("%s: Failure %lld getting "
546 "PHB#%x-PE#%x state\n",
547 __func__, rc,
548 phb->hose->global_number, pe_no);
549 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
550 }
551
552 /* Check the slave PE */
553 if (!(pe->flags & PNV_IODA_PE_MASTER))
554 return state;
555
556 list_for_each_entry(slave, &pe->slaves, list) {
557 rc = opal_pci_eeh_freeze_status(phb->opal_id,
558 slave->pe_number,
559 &fstate,
560 &pcierr,
561 NULL);
562 if (rc != OPAL_SUCCESS) {
563 pr_warn("%s: Failure %lld getting "
564 "PHB#%x-PE#%x state\n",
565 __func__, rc,
566 phb->hose->global_number, slave->pe_number);
567 return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
568 }
569
570 /*
571 * Override the result based on the ascending
572 * priority.
573 */
574 if (fstate > state)
575 state = fstate;
576 }
577
578 return state;
579 }
580
pnv_pci_bdfn_to_pe(struct pnv_phb * phb,u16 bdfn)581 struct pnv_ioda_pe *pnv_pci_bdfn_to_pe(struct pnv_phb *phb, u16 bdfn)
582 {
583 int pe_number = phb->ioda.pe_rmap[bdfn];
584
585 if (pe_number == IODA_INVALID_PE)
586 return NULL;
587
588 return &phb->ioda.pe_array[pe_number];
589 }
590
pnv_ioda_get_pe(struct pci_dev * dev)591 struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
592 {
593 struct pnv_phb *phb = pci_bus_to_pnvhb(dev->bus);
594 struct pci_dn *pdn = pci_get_pdn(dev);
595
596 if (!pdn)
597 return NULL;
598 if (pdn->pe_number == IODA_INVALID_PE)
599 return NULL;
600 return &phb->ioda.pe_array[pdn->pe_number];
601 }
602
pnv_ioda_set_one_peltv(struct pnv_phb * phb,struct pnv_ioda_pe * parent,struct pnv_ioda_pe * child,bool is_add)603 static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
604 struct pnv_ioda_pe *parent,
605 struct pnv_ioda_pe *child,
606 bool is_add)
607 {
608 const char *desc = is_add ? "adding" : "removing";
609 uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
610 OPAL_REMOVE_PE_FROM_DOMAIN;
611 struct pnv_ioda_pe *slave;
612 long rc;
613
614 /* Parent PE affects child PE */
615 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
616 child->pe_number, op);
617 if (rc != OPAL_SUCCESS) {
618 pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
619 rc, desc);
620 return -ENXIO;
621 }
622
623 if (!(child->flags & PNV_IODA_PE_MASTER))
624 return 0;
625
626 /* Compound case: parent PE affects slave PEs */
627 list_for_each_entry(slave, &child->slaves, list) {
628 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
629 slave->pe_number, op);
630 if (rc != OPAL_SUCCESS) {
631 pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
632 rc, desc);
633 return -ENXIO;
634 }
635 }
636
637 return 0;
638 }
639
pnv_ioda_set_peltv(struct pnv_phb * phb,struct pnv_ioda_pe * pe,bool is_add)640 static int pnv_ioda_set_peltv(struct pnv_phb *phb,
641 struct pnv_ioda_pe *pe,
642 bool is_add)
643 {
644 struct pnv_ioda_pe *slave;
645 struct pci_dev *pdev = NULL;
646 int ret;
647
648 /*
649 * Clear PE frozen state. If it's master PE, we need
650 * clear slave PE frozen state as well.
651 */
652 if (is_add) {
653 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
654 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
655 if (pe->flags & PNV_IODA_PE_MASTER) {
656 list_for_each_entry(slave, &pe->slaves, list)
657 opal_pci_eeh_freeze_clear(phb->opal_id,
658 slave->pe_number,
659 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
660 }
661 }
662
663 /*
664 * Associate PE in PELT. We need add the PE into the
665 * corresponding PELT-V as well. Otherwise, the error
666 * originated from the PE might contribute to other
667 * PEs.
668 */
669 ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
670 if (ret)
671 return ret;
672
673 /* For compound PEs, any one affects all of them */
674 if (pe->flags & PNV_IODA_PE_MASTER) {
675 list_for_each_entry(slave, &pe->slaves, list) {
676 ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
677 if (ret)
678 return ret;
679 }
680 }
681
682 if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
683 pdev = pe->pbus->self;
684 else if (pe->flags & PNV_IODA_PE_DEV)
685 pdev = pe->pdev->bus->self;
686 #ifdef CONFIG_PCI_IOV
687 else if (pe->flags & PNV_IODA_PE_VF)
688 pdev = pe->parent_dev;
689 #endif /* CONFIG_PCI_IOV */
690 while (pdev) {
691 struct pci_dn *pdn = pci_get_pdn(pdev);
692 struct pnv_ioda_pe *parent;
693
694 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
695 parent = &phb->ioda.pe_array[pdn->pe_number];
696 ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
697 if (ret)
698 return ret;
699 }
700
701 pdev = pdev->bus->self;
702 }
703
704 return 0;
705 }
706
pnv_ioda_unset_peltv(struct pnv_phb * phb,struct pnv_ioda_pe * pe,struct pci_dev * parent)707 static void pnv_ioda_unset_peltv(struct pnv_phb *phb,
708 struct pnv_ioda_pe *pe,
709 struct pci_dev *parent)
710 {
711 int64_t rc;
712
713 while (parent) {
714 struct pci_dn *pdn = pci_get_pdn(parent);
715
716 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
717 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
718 pe->pe_number,
719 OPAL_REMOVE_PE_FROM_DOMAIN);
720 /* XXX What to do in case of error ? */
721 }
722 parent = parent->bus->self;
723 }
724
725 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
726 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
727
728 /* Disassociate PE in PELT */
729 rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
730 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
731 if (rc)
732 pe_warn(pe, "OPAL error %lld remove self from PELTV\n", rc);
733 }
734
pnv_ioda_deconfigure_pe(struct pnv_phb * phb,struct pnv_ioda_pe * pe)735 int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
736 {
737 struct pci_dev *parent;
738 uint8_t bcomp, dcomp, fcomp;
739 int64_t rc;
740 long rid_end, rid;
741
742 /* Currently, we just deconfigure VF PE. Bus PE will always there.*/
743 if (pe->pbus) {
744 int count;
745
746 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
747 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
748 parent = pe->pbus->self;
749 if (pe->flags & PNV_IODA_PE_BUS_ALL)
750 count = resource_size(&pe->pbus->busn_res);
751 else
752 count = 1;
753
754 switch(count) {
755 case 1: bcomp = OpalPciBusAll; break;
756 case 2: bcomp = OpalPciBus7Bits; break;
757 case 4: bcomp = OpalPciBus6Bits; break;
758 case 8: bcomp = OpalPciBus5Bits; break;
759 case 16: bcomp = OpalPciBus4Bits; break;
760 case 32: bcomp = OpalPciBus3Bits; break;
761 default:
762 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
763 count);
764 /* Do an exact match only */
765 bcomp = OpalPciBusAll;
766 }
767 rid_end = pe->rid + (count << 8);
768 } else {
769 #ifdef CONFIG_PCI_IOV
770 if (pe->flags & PNV_IODA_PE_VF)
771 parent = pe->parent_dev;
772 else
773 #endif
774 parent = pe->pdev->bus->self;
775 bcomp = OpalPciBusAll;
776 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
777 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
778 rid_end = pe->rid + 1;
779 }
780
781 /* Clear the reverse map */
782 for (rid = pe->rid; rid < rid_end; rid++)
783 phb->ioda.pe_rmap[rid] = IODA_INVALID_PE;
784
785 /*
786 * Release from all parents PELT-V. NPUs don't have a PELTV
787 * table
788 */
789 if (phb->type != PNV_PHB_NPU_OCAPI)
790 pnv_ioda_unset_peltv(phb, pe, parent);
791
792 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
793 bcomp, dcomp, fcomp, OPAL_UNMAP_PE);
794 if (rc)
795 pe_err(pe, "OPAL error %lld trying to setup PELT table\n", rc);
796
797 pe->pbus = NULL;
798 pe->pdev = NULL;
799 #ifdef CONFIG_PCI_IOV
800 pe->parent_dev = NULL;
801 #endif
802
803 return 0;
804 }
805
pnv_ioda_configure_pe(struct pnv_phb * phb,struct pnv_ioda_pe * pe)806 int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
807 {
808 uint8_t bcomp, dcomp, fcomp;
809 long rc, rid_end, rid;
810
811 /* Bus validation ? */
812 if (pe->pbus) {
813 int count;
814
815 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
816 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
817 if (pe->flags & PNV_IODA_PE_BUS_ALL)
818 count = resource_size(&pe->pbus->busn_res);
819 else
820 count = 1;
821
822 switch(count) {
823 case 1: bcomp = OpalPciBusAll; break;
824 case 2: bcomp = OpalPciBus7Bits; break;
825 case 4: bcomp = OpalPciBus6Bits; break;
826 case 8: bcomp = OpalPciBus5Bits; break;
827 case 16: bcomp = OpalPciBus4Bits; break;
828 case 32: bcomp = OpalPciBus3Bits; break;
829 default:
830 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
831 count);
832 /* Do an exact match only */
833 bcomp = OpalPciBusAll;
834 }
835 rid_end = pe->rid + (count << 8);
836 } else {
837 bcomp = OpalPciBusAll;
838 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
839 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
840 rid_end = pe->rid + 1;
841 }
842
843 /*
844 * Associate PE in PELT. We need add the PE into the
845 * corresponding PELT-V as well. Otherwise, the error
846 * originated from the PE might contribute to other
847 * PEs.
848 */
849 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
850 bcomp, dcomp, fcomp, OPAL_MAP_PE);
851 if (rc) {
852 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
853 return -ENXIO;
854 }
855
856 /*
857 * Configure PELTV. NPUs don't have a PELTV table so skip
858 * configuration on them.
859 */
860 if (phb->type != PNV_PHB_NPU_OCAPI)
861 pnv_ioda_set_peltv(phb, pe, true);
862
863 /* Setup reverse map */
864 for (rid = pe->rid; rid < rid_end; rid++)
865 phb->ioda.pe_rmap[rid] = pe->pe_number;
866
867 pe->mve_number = 0;
868
869 return 0;
870 }
871
pnv_ioda_setup_dev_PE(struct pci_dev * dev)872 static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
873 {
874 struct pnv_phb *phb = pci_bus_to_pnvhb(dev->bus);
875 struct pci_dn *pdn = pci_get_pdn(dev);
876 struct pnv_ioda_pe *pe;
877
878 if (!pdn) {
879 pr_err("%s: Device tree node not associated properly\n",
880 pci_name(dev));
881 return NULL;
882 }
883 if (pdn->pe_number != IODA_INVALID_PE)
884 return NULL;
885
886 pe = pnv_ioda_alloc_pe(phb, 1);
887 if (!pe) {
888 pr_warn("%s: Not enough PE# available, disabling device\n",
889 pci_name(dev));
890 return NULL;
891 }
892
893 /* NOTE: We don't get a reference for the pointer in the PE
894 * data structure, both the device and PE structures should be
895 * destroyed at the same time.
896 *
897 * At some point we want to remove the PDN completely anyways
898 */
899 pdn->pe_number = pe->pe_number;
900 pe->flags = PNV_IODA_PE_DEV;
901 pe->pdev = dev;
902 pe->pbus = NULL;
903 pe->mve_number = -1;
904 pe->rid = dev->bus->number << 8 | pdn->devfn;
905 pe->device_count++;
906
907 pe_info(pe, "Associated device to PE\n");
908
909 if (pnv_ioda_configure_pe(phb, pe)) {
910 /* XXX What do we do here ? */
911 pnv_ioda_free_pe(pe);
912 pdn->pe_number = IODA_INVALID_PE;
913 pe->pdev = NULL;
914 return NULL;
915 }
916
917 /* Put PE to the list */
918 mutex_lock(&phb->ioda.pe_list_mutex);
919 list_add_tail(&pe->list, &phb->ioda.pe_list);
920 mutex_unlock(&phb->ioda.pe_list_mutex);
921 return pe;
922 }
923
924 /*
925 * There're 2 types of PCI bus sensitive PEs: One that is compromised of
926 * single PCI bus. Another one that contains the primary PCI bus and its
927 * subordinate PCI devices and buses. The second type of PE is normally
928 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
929 */
pnv_ioda_setup_bus_PE(struct pci_bus * bus,bool all)930 static struct pnv_ioda_pe *pnv_ioda_setup_bus_PE(struct pci_bus *bus, bool all)
931 {
932 struct pnv_phb *phb = pci_bus_to_pnvhb(bus);
933 struct pnv_ioda_pe *pe = NULL;
934 unsigned int pe_num;
935
936 /*
937 * In partial hotplug case, the PE instance might be still alive.
938 * We should reuse it instead of allocating a new one.
939 */
940 pe_num = phb->ioda.pe_rmap[bus->number << 8];
941 if (WARN_ON(pe_num != IODA_INVALID_PE)) {
942 pe = &phb->ioda.pe_array[pe_num];
943 return NULL;
944 }
945
946 /* PE number for root bus should have been reserved */
947 if (pci_is_root_bus(bus))
948 pe = &phb->ioda.pe_array[phb->ioda.root_pe_idx];
949
950 /* Check if PE is determined by M64 */
951 if (!pe)
952 pe = pnv_ioda_pick_m64_pe(bus, all);
953
954 /* The PE number isn't pinned by M64 */
955 if (!pe)
956 pe = pnv_ioda_alloc_pe(phb, 1);
957
958 if (!pe) {
959 pr_warn("%s: Not enough PE# available for PCI bus %04x:%02x\n",
960 __func__, pci_domain_nr(bus), bus->number);
961 return NULL;
962 }
963
964 pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
965 pe->pbus = bus;
966 pe->pdev = NULL;
967 pe->mve_number = -1;
968 pe->rid = bus->busn_res.start << 8;
969
970 if (all)
971 pe_info(pe, "Secondary bus %pad..%pad associated with PE#%x\n",
972 &bus->busn_res.start, &bus->busn_res.end,
973 pe->pe_number);
974 else
975 pe_info(pe, "Secondary bus %pad associated with PE#%x\n",
976 &bus->busn_res.start, pe->pe_number);
977
978 if (pnv_ioda_configure_pe(phb, pe)) {
979 /* XXX What do we do here ? */
980 pnv_ioda_free_pe(pe);
981 pe->pbus = NULL;
982 return NULL;
983 }
984
985 /* Put PE to the list */
986 list_add_tail(&pe->list, &phb->ioda.pe_list);
987
988 return pe;
989 }
990
pnv_pci_ioda_dma_dev_setup(struct pci_dev * pdev)991 static void pnv_pci_ioda_dma_dev_setup(struct pci_dev *pdev)
992 {
993 struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
994 struct pci_dn *pdn = pci_get_pdn(pdev);
995 struct pnv_ioda_pe *pe;
996
997 /* Check if the BDFN for this device is associated with a PE yet */
998 pe = pnv_pci_bdfn_to_pe(phb, pci_dev_id(pdev));
999 if (!pe) {
1000 /* VF PEs should be pre-configured in pnv_pci_sriov_enable() */
1001 if (WARN_ON(pdev->is_virtfn))
1002 return;
1003
1004 pnv_pci_configure_bus(pdev->bus);
1005 pe = pnv_pci_bdfn_to_pe(phb, pci_dev_id(pdev));
1006 pci_info(pdev, "Configured PE#%x\n", pe ? pe->pe_number : 0xfffff);
1007
1008
1009 /*
1010 * If we can't setup the IODA PE something has gone horribly
1011 * wrong and we can't enable DMA for the device.
1012 */
1013 if (WARN_ON(!pe))
1014 return;
1015 } else {
1016 pci_info(pdev, "Added to existing PE#%x\n", pe->pe_number);
1017 }
1018
1019 /*
1020 * We assume that bridges *probably* don't need to do any DMA so we can
1021 * skip allocating a TCE table, etc unless we get a non-bridge device.
1022 */
1023 if (!pe->dma_setup_done && !pci_is_bridge(pdev)) {
1024 switch (phb->type) {
1025 case PNV_PHB_IODA2:
1026 pnv_pci_ioda2_setup_dma_pe(phb, pe);
1027 break;
1028 default:
1029 pr_warn("%s: No DMA for PHB#%x (type %d)\n",
1030 __func__, phb->hose->global_number, phb->type);
1031 }
1032 }
1033
1034 if (pdn)
1035 pdn->pe_number = pe->pe_number;
1036 pe->device_count++;
1037
1038 WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
1039 pdev->dev.archdata.dma_offset = pe->tce_bypass_base;
1040 set_iommu_table_base(&pdev->dev, pe->table_group.tables[0]);
1041
1042 /* PEs with a DMA weight of zero won't have a group */
1043 if (pe->table_group.group)
1044 iommu_add_device(&pe->table_group, &pdev->dev);
1045 }
1046
1047 /*
1048 * Reconfigure TVE#0 to be usable as 64-bit DMA space.
1049 *
1050 * The first 4GB of virtual memory for a PE is reserved for 32-bit accesses.
1051 * Devices can only access more than that if bit 59 of the PCI address is set
1052 * by hardware, which indicates TVE#1 should be used instead of TVE#0.
1053 * Many PCI devices are not capable of addressing that many bits, and as a
1054 * result are limited to the 4GB of virtual memory made available to 32-bit
1055 * devices in TVE#0.
1056 *
1057 * In order to work around this, reconfigure TVE#0 to be suitable for 64-bit
1058 * devices by configuring the virtual memory past the first 4GB inaccessible
1059 * by 64-bit DMAs. This should only be used by devices that want more than
1060 * 4GB, and only on PEs that have no 32-bit devices.
1061 *
1062 * Currently this will only work on PHB3 (POWER8).
1063 */
pnv_pci_ioda_dma_64bit_bypass(struct pnv_ioda_pe * pe)1064 static int pnv_pci_ioda_dma_64bit_bypass(struct pnv_ioda_pe *pe)
1065 {
1066 u64 window_size, table_size, tce_count, addr;
1067 struct page *table_pages;
1068 u64 tce_order = 28; /* 256MB TCEs */
1069 __be64 *tces;
1070 s64 rc;
1071
1072 /*
1073 * Window size needs to be a power of two, but needs to account for
1074 * shifting memory by the 4GB offset required to skip 32bit space.
1075 */
1076 window_size = roundup_pow_of_two(memory_hotplug_max() + (1ULL << 32));
1077 tce_count = window_size >> tce_order;
1078 table_size = tce_count << 3;
1079
1080 if (table_size < PAGE_SIZE)
1081 table_size = PAGE_SIZE;
1082
1083 table_pages = alloc_pages_node(pe->phb->hose->node, GFP_KERNEL,
1084 get_order(table_size));
1085 if (!table_pages)
1086 goto err;
1087
1088 tces = page_address(table_pages);
1089 if (!tces)
1090 goto err;
1091
1092 memset(tces, 0, table_size);
1093
1094 for (addr = 0; addr < memory_hotplug_max(); addr += (1 << tce_order)) {
1095 tces[(addr + (1ULL << 32)) >> tce_order] =
1096 cpu_to_be64(addr | TCE_PCI_READ | TCE_PCI_WRITE);
1097 }
1098
1099 rc = opal_pci_map_pe_dma_window(pe->phb->opal_id,
1100 pe->pe_number,
1101 /* reconfigure window 0 */
1102 (pe->pe_number << 1) + 0,
1103 1,
1104 __pa(tces),
1105 table_size,
1106 1 << tce_order);
1107 if (rc == OPAL_SUCCESS) {
1108 pe_info(pe, "Using 64-bit DMA iommu bypass (through TVE#0)\n");
1109 return 0;
1110 }
1111 err:
1112 pe_err(pe, "Error configuring 64-bit DMA bypass\n");
1113 return -EIO;
1114 }
1115
pnv_pci_ioda_iommu_bypass_supported(struct pci_dev * pdev,u64 dma_mask)1116 static bool pnv_pci_ioda_iommu_bypass_supported(struct pci_dev *pdev,
1117 u64 dma_mask)
1118 {
1119 struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
1120 struct pci_dn *pdn = pci_get_pdn(pdev);
1121 struct pnv_ioda_pe *pe;
1122
1123 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
1124 return false;
1125
1126 pe = &phb->ioda.pe_array[pdn->pe_number];
1127 if (pe->tce_bypass_enabled) {
1128 u64 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
1129 if (dma_mask >= top)
1130 return true;
1131 }
1132
1133 /*
1134 * If the device can't set the TCE bypass bit but still wants
1135 * to access 4GB or more, on PHB3 we can reconfigure TVE#0 to
1136 * bypass the 32-bit region and be usable for 64-bit DMAs.
1137 * The device needs to be able to address all of this space.
1138 */
1139 if (dma_mask >> 32 &&
1140 dma_mask > (memory_hotplug_max() + (1ULL << 32)) &&
1141 /* pe->pdev should be set if it's a single device, pe->pbus if not */
1142 (pe->device_count == 1 || !pe->pbus) &&
1143 phb->model == PNV_PHB_MODEL_PHB3) {
1144 /* Configure the bypass mode */
1145 s64 rc = pnv_pci_ioda_dma_64bit_bypass(pe);
1146 if (rc)
1147 return false;
1148 /* 4GB offset bypasses 32-bit space */
1149 pdev->dev.archdata.dma_offset = (1ULL << 32);
1150 return true;
1151 }
1152
1153 return false;
1154 }
1155
pnv_ioda_get_inval_reg(struct pnv_phb * phb)1156 static inline __be64 __iomem *pnv_ioda_get_inval_reg(struct pnv_phb *phb)
1157 {
1158 return phb->regs + 0x210;
1159 }
1160
1161 #ifdef CONFIG_IOMMU_API
1162 /* Common for IODA1 and IODA2 */
pnv_ioda_tce_xchg_no_kill(struct iommu_table * tbl,long index,unsigned long * hpa,enum dma_data_direction * direction)1163 static int pnv_ioda_tce_xchg_no_kill(struct iommu_table *tbl, long index,
1164 unsigned long *hpa, enum dma_data_direction *direction)
1165 {
1166 return pnv_tce_xchg(tbl, index, hpa, direction);
1167 }
1168 #endif
1169
1170 #define PHB3_TCE_KILL_INVAL_ALL PPC_BIT(0)
1171 #define PHB3_TCE_KILL_INVAL_PE PPC_BIT(1)
1172 #define PHB3_TCE_KILL_INVAL_ONE PPC_BIT(2)
1173
pnv_pci_phb3_tce_invalidate_pe(struct pnv_ioda_pe * pe)1174 static inline void pnv_pci_phb3_tce_invalidate_pe(struct pnv_ioda_pe *pe)
1175 {
1176 /* 01xb - invalidate TCEs that match the specified PE# */
1177 __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb);
1178 unsigned long val = PHB3_TCE_KILL_INVAL_PE | (pe->pe_number & 0xFF);
1179
1180 mb(); /* Ensure above stores are visible */
1181 __raw_writeq_be(val, invalidate);
1182 }
1183
pnv_pci_phb3_tce_invalidate(struct pnv_ioda_pe * pe,unsigned shift,unsigned long index,unsigned long npages)1184 static void pnv_pci_phb3_tce_invalidate(struct pnv_ioda_pe *pe,
1185 unsigned shift, unsigned long index,
1186 unsigned long npages)
1187 {
1188 __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb);
1189 unsigned long start, end, inc;
1190
1191 /* We'll invalidate DMA address in PE scope */
1192 start = PHB3_TCE_KILL_INVAL_ONE;
1193 start |= (pe->pe_number & 0xFF);
1194 end = start;
1195
1196 /* Figure out the start, end and step */
1197 start |= (index << shift);
1198 end |= ((index + npages - 1) << shift);
1199 inc = (0x1ull << shift);
1200 mb();
1201
1202 while (start <= end) {
1203 __raw_writeq_be(start, invalidate);
1204 start += inc;
1205 }
1206 }
1207
pnv_pci_ioda2_tce_invalidate_pe(struct pnv_ioda_pe * pe)1208 static inline void pnv_pci_ioda2_tce_invalidate_pe(struct pnv_ioda_pe *pe)
1209 {
1210 struct pnv_phb *phb = pe->phb;
1211
1212 if (phb->model == PNV_PHB_MODEL_PHB3 && phb->regs)
1213 pnv_pci_phb3_tce_invalidate_pe(pe);
1214 else
1215 opal_pci_tce_kill(phb->opal_id, OPAL_PCI_TCE_KILL_PE,
1216 pe->pe_number, 0, 0, 0);
1217 }
1218
pnv_pci_ioda2_tce_invalidate(struct iommu_table * tbl,unsigned long index,unsigned long npages)1219 static void pnv_pci_ioda2_tce_invalidate(struct iommu_table *tbl,
1220 unsigned long index, unsigned long npages)
1221 {
1222 struct iommu_table_group_link *tgl;
1223
1224 list_for_each_entry_lockless(tgl, &tbl->it_group_list, next) {
1225 struct pnv_ioda_pe *pe = container_of(tgl->table_group,
1226 struct pnv_ioda_pe, table_group);
1227 struct pnv_phb *phb = pe->phb;
1228 unsigned int shift = tbl->it_page_shift;
1229
1230 if (phb->model == PNV_PHB_MODEL_PHB3 && phb->regs)
1231 pnv_pci_phb3_tce_invalidate(pe, shift,
1232 index, npages);
1233 else
1234 opal_pci_tce_kill(phb->opal_id,
1235 OPAL_PCI_TCE_KILL_PAGES,
1236 pe->pe_number, 1u << shift,
1237 index << shift, npages);
1238 }
1239 }
1240
pnv_ioda2_tce_build(struct iommu_table * tbl,long index,long npages,unsigned long uaddr,enum dma_data_direction direction,unsigned long attrs)1241 static int pnv_ioda2_tce_build(struct iommu_table *tbl, long index,
1242 long npages, unsigned long uaddr,
1243 enum dma_data_direction direction,
1244 unsigned long attrs)
1245 {
1246 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
1247 attrs);
1248
1249 if (!ret)
1250 pnv_pci_ioda2_tce_invalidate(tbl, index, npages);
1251
1252 return ret;
1253 }
1254
pnv_ioda2_tce_free(struct iommu_table * tbl,long index,long npages)1255 static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index,
1256 long npages)
1257 {
1258 pnv_tce_free(tbl, index, npages);
1259
1260 pnv_pci_ioda2_tce_invalidate(tbl, index, npages);
1261 }
1262
1263 static struct iommu_table_ops pnv_ioda2_iommu_ops = {
1264 .set = pnv_ioda2_tce_build,
1265 #ifdef CONFIG_IOMMU_API
1266 .xchg_no_kill = pnv_ioda_tce_xchg_no_kill,
1267 .tce_kill = pnv_pci_ioda2_tce_invalidate,
1268 .useraddrptr = pnv_tce_useraddrptr,
1269 #endif
1270 .clear = pnv_ioda2_tce_free,
1271 .get = pnv_tce_get,
1272 .free = pnv_pci_ioda2_table_free_pages,
1273 };
1274
pnv_pci_ioda2_set_window(struct iommu_table_group * table_group,int num,struct iommu_table * tbl)1275 static long pnv_pci_ioda2_set_window(struct iommu_table_group *table_group,
1276 int num, struct iommu_table *tbl)
1277 {
1278 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
1279 table_group);
1280 struct pnv_phb *phb = pe->phb;
1281 int64_t rc;
1282 const unsigned long size = tbl->it_indirect_levels ?
1283 tbl->it_level_size : tbl->it_size;
1284 const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
1285 const __u64 win_size = tbl->it_size << tbl->it_page_shift;
1286
1287 pe_info(pe, "Setting up window#%d %llx..%llx pg=%lx\n",
1288 num, start_addr, start_addr + win_size - 1,
1289 IOMMU_PAGE_SIZE(tbl));
1290
1291 /*
1292 * Map TCE table through TVT. The TVE index is the PE number
1293 * shifted by 1 bit for 32-bits DMA space.
1294 */
1295 rc = opal_pci_map_pe_dma_window(phb->opal_id,
1296 pe->pe_number,
1297 (pe->pe_number << 1) + num,
1298 tbl->it_indirect_levels + 1,
1299 __pa(tbl->it_base),
1300 size << 3,
1301 IOMMU_PAGE_SIZE(tbl));
1302 if (rc) {
1303 pe_err(pe, "Failed to configure TCE table, err %lld\n", rc);
1304 return rc;
1305 }
1306
1307 pnv_pci_link_table_and_group(phb->hose->node, num,
1308 tbl, &pe->table_group);
1309 pnv_pci_ioda2_tce_invalidate_pe(pe);
1310
1311 return 0;
1312 }
1313
pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe * pe,bool enable)1314 static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable)
1315 {
1316 uint16_t window_id = (pe->pe_number << 1 ) + 1;
1317 int64_t rc;
1318
1319 pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
1320 if (enable) {
1321 phys_addr_t top = memblock_end_of_DRAM();
1322
1323 top = roundup_pow_of_two(top);
1324 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1325 pe->pe_number,
1326 window_id,
1327 pe->tce_bypass_base,
1328 top);
1329 } else {
1330 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
1331 pe->pe_number,
1332 window_id,
1333 pe->tce_bypass_base,
1334 0);
1335 }
1336 if (rc)
1337 pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
1338 else
1339 pe->tce_bypass_enabled = enable;
1340 }
1341
pnv_pci_ioda2_create_table(struct iommu_table_group * table_group,int num,__u32 page_shift,__u64 window_size,__u32 levels,bool alloc_userspace_copy,struct iommu_table ** ptbl)1342 static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group,
1343 int num, __u32 page_shift, __u64 window_size, __u32 levels,
1344 bool alloc_userspace_copy, struct iommu_table **ptbl)
1345 {
1346 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
1347 table_group);
1348 int nid = pe->phb->hose->node;
1349 __u64 bus_offset = num ? pe->tce_bypass_base : table_group->tce32_start;
1350 long ret;
1351 struct iommu_table *tbl;
1352
1353 tbl = pnv_pci_table_alloc(nid);
1354 if (!tbl)
1355 return -ENOMEM;
1356
1357 tbl->it_ops = &pnv_ioda2_iommu_ops;
1358
1359 ret = pnv_pci_ioda2_table_alloc_pages(nid,
1360 bus_offset, page_shift, window_size,
1361 levels, alloc_userspace_copy, tbl);
1362 if (ret) {
1363 iommu_tce_table_put(tbl);
1364 return ret;
1365 }
1366
1367 *ptbl = tbl;
1368
1369 return 0;
1370 }
1371
pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe * pe)1372 static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe)
1373 {
1374 struct iommu_table *tbl = NULL;
1375 long rc;
1376 unsigned long res_start, res_end;
1377
1378 /*
1379 * crashkernel= specifies the kdump kernel's maximum memory at
1380 * some offset and there is no guaranteed the result is a power
1381 * of 2, which will cause errors later.
1382 */
1383 const u64 max_memory = __rounddown_pow_of_two(memory_hotplug_max());
1384
1385 /*
1386 * In memory constrained environments, e.g. kdump kernel, the
1387 * DMA window can be larger than available memory, which will
1388 * cause errors later.
1389 */
1390 const u64 maxblock = 1UL << (PAGE_SHIFT + MAX_PAGE_ORDER);
1391
1392 /*
1393 * We create the default window as big as we can. The constraint is
1394 * the max order of allocation possible. The TCE table is likely to
1395 * end up being multilevel and with on-demand allocation in place,
1396 * the initial use is not going to be huge as the default window aims
1397 * to support crippled devices (i.e. not fully 64bit DMAble) only.
1398 */
1399 /* iommu_table::it_map uses 1 bit per IOMMU page, hence 8 */
1400 const u64 window_size = min((maxblock * 8) << PAGE_SHIFT, max_memory);
1401 /* Each TCE level cannot exceed maxblock so go multilevel if needed */
1402 unsigned long tces_order = ilog2(window_size >> PAGE_SHIFT);
1403 unsigned long tcelevel_order = ilog2(maxblock >> 3);
1404 unsigned int levels = tces_order / tcelevel_order;
1405
1406 if (tces_order % tcelevel_order)
1407 levels += 1;
1408 /*
1409 * We try to stick to default levels (which is >1 at the moment) in
1410 * order to save memory by relying on on-demain TCE level allocation.
1411 */
1412 levels = max_t(unsigned int, levels, POWERNV_IOMMU_DEFAULT_LEVELS);
1413
1414 rc = pnv_pci_ioda2_create_table(&pe->table_group, 0, PAGE_SHIFT,
1415 window_size, levels, false, &tbl);
1416 if (rc) {
1417 pe_err(pe, "Failed to create 32-bit TCE table, err %ld",
1418 rc);
1419 return rc;
1420 }
1421
1422 /* We use top part of 32bit space for MMIO so exclude it from DMA */
1423 res_start = 0;
1424 res_end = 0;
1425 if (window_size > pe->phb->ioda.m32_pci_base) {
1426 res_start = pe->phb->ioda.m32_pci_base >> tbl->it_page_shift;
1427 res_end = min(window_size, SZ_4G) >> tbl->it_page_shift;
1428 }
1429
1430 tbl->it_index = (pe->phb->hose->global_number << 16) | pe->pe_number;
1431 if (iommu_init_table(tbl, pe->phb->hose->node, res_start, res_end))
1432 rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl);
1433 else
1434 rc = -ENOMEM;
1435 if (rc) {
1436 pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n", rc);
1437 iommu_tce_table_put(tbl);
1438 tbl = NULL; /* This clears iommu_table_base below */
1439 }
1440 if (!pnv_iommu_bypass_disabled)
1441 pnv_pci_ioda2_set_bypass(pe, true);
1442
1443 /*
1444 * Set table base for the case of IOMMU DMA use. Usually this is done
1445 * from dma_dev_setup() which is not called when a device is returned
1446 * from VFIO so do it here.
1447 */
1448 if (pe->pdev)
1449 set_iommu_table_base(&pe->pdev->dev, tbl);
1450
1451 return 0;
1452 }
1453
pnv_pci_ioda2_unset_window(struct iommu_table_group * table_group,int num)1454 static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
1455 int num)
1456 {
1457 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
1458 table_group);
1459 struct pnv_phb *phb = pe->phb;
1460 long ret;
1461
1462 pe_info(pe, "Removing DMA window #%d\n", num);
1463
1464 ret = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
1465 (pe->pe_number << 1) + num,
1466 0/* levels */, 0/* table address */,
1467 0/* table size */, 0/* page size */);
1468 if (ret)
1469 pe_warn(pe, "Unmapping failed, ret = %ld\n", ret);
1470 else
1471 pnv_pci_ioda2_tce_invalidate_pe(pe);
1472
1473 pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);
1474
1475 return ret;
1476 }
1477
1478 #ifdef CONFIG_IOMMU_API
pnv_pci_ioda2_get_table_size(__u32 page_shift,__u64 window_size,__u32 levels)1479 unsigned long pnv_pci_ioda2_get_table_size(__u32 page_shift,
1480 __u64 window_size, __u32 levels)
1481 {
1482 unsigned long bytes = 0;
1483 const unsigned window_shift = ilog2(window_size);
1484 unsigned entries_shift = window_shift - page_shift;
1485 unsigned table_shift = entries_shift + 3;
1486 unsigned long tce_table_size = max(0x1000UL, 1UL << table_shift);
1487 unsigned long direct_table_size;
1488
1489 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS) ||
1490 !is_power_of_2(window_size))
1491 return 0;
1492
1493 /* Calculate a direct table size from window_size and levels */
1494 entries_shift = (entries_shift + levels - 1) / levels;
1495 table_shift = entries_shift + 3;
1496 table_shift = max_t(unsigned, table_shift, PAGE_SHIFT);
1497 direct_table_size = 1UL << table_shift;
1498
1499 for ( ; levels; --levels) {
1500 bytes += ALIGN(tce_table_size, direct_table_size);
1501
1502 tce_table_size /= direct_table_size;
1503 tce_table_size <<= 3;
1504 tce_table_size = max_t(unsigned long,
1505 tce_table_size, direct_table_size);
1506 }
1507
1508 return bytes + bytes; /* one for HW table, one for userspace copy */
1509 }
1510
pnv_pci_ioda2_create_table_userspace(struct iommu_table_group * table_group,int num,__u32 page_shift,__u64 window_size,__u32 levels,struct iommu_table ** ptbl)1511 static long pnv_pci_ioda2_create_table_userspace(
1512 struct iommu_table_group *table_group,
1513 int num, __u32 page_shift, __u64 window_size, __u32 levels,
1514 struct iommu_table **ptbl)
1515 {
1516 long ret = pnv_pci_ioda2_create_table(table_group,
1517 num, page_shift, window_size, levels, true, ptbl);
1518
1519 if (!ret)
1520 (*ptbl)->it_allocated_size = pnv_pci_ioda2_get_table_size(
1521 page_shift, window_size, levels);
1522 return ret;
1523 }
1524
pnv_ioda_setup_bus_dma(struct pnv_ioda_pe * pe,struct pci_bus * bus)1525 static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe, struct pci_bus *bus)
1526 {
1527 struct pci_dev *dev;
1528
1529 list_for_each_entry(dev, &bus->devices, bus_list) {
1530 set_iommu_table_base(&dev->dev, pe->table_group.tables[0]);
1531 dev->dev.archdata.dma_offset = pe->tce_bypass_base;
1532
1533 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
1534 pnv_ioda_setup_bus_dma(pe, dev->subordinate);
1535 }
1536 }
1537
pnv_ioda2_take_ownership(struct iommu_table_group * table_group,struct device * dev __maybe_unused)1538 static long pnv_ioda2_take_ownership(struct iommu_table_group *table_group,
1539 struct device *dev __maybe_unused)
1540 {
1541 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
1542 table_group);
1543 /* Store @tbl as pnv_pci_ioda2_unset_window() resets it */
1544 struct iommu_table *tbl = pe->table_group.tables[0];
1545
1546 /*
1547 * iommu_ops transfers the ownership per a device and we mode
1548 * the group ownership with the first device in the group.
1549 */
1550 if (!tbl)
1551 return 0;
1552
1553 pnv_pci_ioda2_set_bypass(pe, false);
1554 pnv_pci_ioda2_unset_window(&pe->table_group, 0);
1555 if (pe->pbus)
1556 pnv_ioda_setup_bus_dma(pe, pe->pbus);
1557 else if (pe->pdev)
1558 set_iommu_table_base(&pe->pdev->dev, NULL);
1559 iommu_tce_table_put(tbl);
1560
1561 return 0;
1562 }
1563
pnv_ioda2_release_ownership(struct iommu_table_group * table_group,struct device * dev __maybe_unused)1564 static void pnv_ioda2_release_ownership(struct iommu_table_group *table_group,
1565 struct device *dev __maybe_unused)
1566 {
1567 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
1568 table_group);
1569
1570 /* See the comment about iommu_ops above */
1571 if (pe->table_group.tables[0])
1572 return;
1573 pnv_pci_ioda2_setup_default_config(pe);
1574 if (pe->pbus)
1575 pnv_ioda_setup_bus_dma(pe, pe->pbus);
1576 }
1577
1578 static struct iommu_table_group_ops pnv_pci_ioda2_ops = {
1579 .get_table_size = pnv_pci_ioda2_get_table_size,
1580 .create_table = pnv_pci_ioda2_create_table_userspace,
1581 .set_window = pnv_pci_ioda2_set_window,
1582 .unset_window = pnv_pci_ioda2_unset_window,
1583 .take_ownership = pnv_ioda2_take_ownership,
1584 .release_ownership = pnv_ioda2_release_ownership,
1585 };
1586 #endif
1587
pnv_pci_ioda2_setup_dma_pe(struct pnv_phb * phb,struct pnv_ioda_pe * pe)1588 void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
1589 struct pnv_ioda_pe *pe)
1590 {
1591 int64_t rc;
1592
1593 /* TVE #1 is selected by PCI address bit 59 */
1594 pe->tce_bypass_base = 1ull << 59;
1595
1596 /* The PE will reserve all possible 32-bits space */
1597 pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
1598 phb->ioda.m32_pci_base);
1599
1600 /* Setup linux iommu table */
1601 pe->table_group.tce32_start = 0;
1602 pe->table_group.tce32_size = phb->ioda.m32_pci_base;
1603 pe->table_group.max_dynamic_windows_supported =
1604 IOMMU_TABLE_GROUP_MAX_TABLES;
1605 pe->table_group.max_levels = POWERNV_IOMMU_MAX_LEVELS;
1606 pe->table_group.pgsizes = pnv_ioda_parse_tce_sizes(phb);
1607
1608 rc = pnv_pci_ioda2_setup_default_config(pe);
1609 if (rc)
1610 return;
1611
1612 #ifdef CONFIG_IOMMU_API
1613 pe->table_group.ops = &pnv_pci_ioda2_ops;
1614 iommu_register_group(&pe->table_group, phb->hose->global_number,
1615 pe->pe_number);
1616 #endif
1617 pe->dma_setup_done = true;
1618 }
1619
1620 /*
1621 * Called from KVM in real mode to EOI passthru interrupts. The ICP
1622 * EOI is handled directly in KVM in kvmppc_deliver_irq_passthru().
1623 *
1624 * The IRQ data is mapped in the PCI-MSI domain and the EOI OPAL call
1625 * needs an HW IRQ number mapped in the XICS IRQ domain. The HW IRQ
1626 * numbers of the in-the-middle MSI domain are vector numbers and it's
1627 * good enough for OPAL. Use that.
1628 */
pnv_opal_pci_msi_eoi(struct irq_data * d)1629 int64_t pnv_opal_pci_msi_eoi(struct irq_data *d)
1630 {
1631 struct pci_controller *hose = irq_data_get_irq_chip_data(d->parent_data);
1632 struct pnv_phb *phb = hose->private_data;
1633
1634 return opal_pci_msi_eoi(phb->opal_id, d->parent_data->hwirq);
1635 }
1636
1637 static struct irq_chip pnv_pci_msi_irq_chip;
1638
1639 /*
1640 * Returns true iff chip is something that we could call
1641 * pnv_opal_pci_msi_eoi for.
1642 */
is_pnv_opal_msi(struct irq_chip * chip)1643 bool is_pnv_opal_msi(struct irq_chip *chip)
1644 {
1645 return chip == &pnv_pci_msi_irq_chip;
1646 }
1647 EXPORT_SYMBOL_GPL(is_pnv_opal_msi);
1648
__pnv_pci_ioda_msi_setup(struct pnv_phb * phb,struct pci_dev * dev,unsigned int xive_num,unsigned int is_64,struct msi_msg * msg)1649 static int __pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
1650 unsigned int xive_num,
1651 unsigned int is_64, struct msi_msg *msg)
1652 {
1653 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
1654 __be32 data;
1655 int rc;
1656
1657 dev_dbg(&dev->dev, "%s: setup %s-bit MSI for vector #%d\n", __func__,
1658 is_64 ? "64" : "32", xive_num);
1659
1660 /* No PE assigned ? bail out ... no MSI for you ! */
1661 if (pe == NULL)
1662 return -ENXIO;
1663
1664 /* Check if we have an MVE */
1665 if (pe->mve_number < 0)
1666 return -ENXIO;
1667
1668 /* Force 32-bit MSI on some broken devices */
1669 if (dev->no_64bit_msi)
1670 is_64 = 0;
1671
1672 /* Assign XIVE to PE */
1673 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
1674 if (rc) {
1675 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
1676 pci_name(dev), rc, xive_num);
1677 return -EIO;
1678 }
1679
1680 if (is_64) {
1681 __be64 addr64;
1682
1683 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
1684 &addr64, &data);
1685 if (rc) {
1686 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
1687 pci_name(dev), rc);
1688 return -EIO;
1689 }
1690 msg->address_hi = be64_to_cpu(addr64) >> 32;
1691 msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
1692 } else {
1693 __be32 addr32;
1694
1695 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
1696 &addr32, &data);
1697 if (rc) {
1698 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
1699 pci_name(dev), rc);
1700 return -EIO;
1701 }
1702 msg->address_hi = 0;
1703 msg->address_lo = be32_to_cpu(addr32);
1704 }
1705 msg->data = be32_to_cpu(data);
1706
1707 return 0;
1708 }
1709
1710 /*
1711 * The msi_free() op is called before irq_domain_free_irqs_top() when
1712 * the handler data is still available. Use that to clear the XIVE
1713 * controller.
1714 */
pnv_msi_ops_msi_free(struct irq_domain * domain,struct msi_domain_info * info,unsigned int irq)1715 static void pnv_msi_ops_msi_free(struct irq_domain *domain,
1716 struct msi_domain_info *info,
1717 unsigned int irq)
1718 {
1719 if (xive_enabled())
1720 xive_irq_free_data(irq);
1721 }
1722
1723 static struct msi_domain_ops pnv_pci_msi_domain_ops = {
1724 .msi_free = pnv_msi_ops_msi_free,
1725 };
1726
pnv_msi_shutdown(struct irq_data * d)1727 static void pnv_msi_shutdown(struct irq_data *d)
1728 {
1729 d = d->parent_data;
1730 if (d->chip->irq_shutdown)
1731 d->chip->irq_shutdown(d);
1732 }
1733
pnv_msi_mask(struct irq_data * d)1734 static void pnv_msi_mask(struct irq_data *d)
1735 {
1736 pci_msi_mask_irq(d);
1737 irq_chip_mask_parent(d);
1738 }
1739
pnv_msi_unmask(struct irq_data * d)1740 static void pnv_msi_unmask(struct irq_data *d)
1741 {
1742 pci_msi_unmask_irq(d);
1743 irq_chip_unmask_parent(d);
1744 }
1745
1746 static struct irq_chip pnv_pci_msi_irq_chip = {
1747 .name = "PNV-PCI-MSI",
1748 .irq_shutdown = pnv_msi_shutdown,
1749 .irq_mask = pnv_msi_mask,
1750 .irq_unmask = pnv_msi_unmask,
1751 .irq_eoi = irq_chip_eoi_parent,
1752 };
1753
1754 static struct msi_domain_info pnv_msi_domain_info = {
1755 .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
1756 MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX),
1757 .ops = &pnv_pci_msi_domain_ops,
1758 .chip = &pnv_pci_msi_irq_chip,
1759 };
1760
pnv_msi_compose_msg(struct irq_data * d,struct msi_msg * msg)1761 static void pnv_msi_compose_msg(struct irq_data *d, struct msi_msg *msg)
1762 {
1763 struct msi_desc *entry = irq_data_get_msi_desc(d);
1764 struct pci_dev *pdev = msi_desc_to_pci_dev(entry);
1765 struct pci_controller *hose = irq_data_get_irq_chip_data(d);
1766 struct pnv_phb *phb = hose->private_data;
1767 int rc;
1768
1769 rc = __pnv_pci_ioda_msi_setup(phb, pdev, d->hwirq,
1770 entry->pci.msi_attrib.is_64, msg);
1771 if (rc)
1772 dev_err(&pdev->dev, "Failed to setup %s-bit MSI #%ld : %d\n",
1773 entry->pci.msi_attrib.is_64 ? "64" : "32", d->hwirq, rc);
1774 }
1775
1776 /*
1777 * The IRQ data is mapped in the MSI domain in which HW IRQ numbers
1778 * correspond to vector numbers.
1779 */
pnv_msi_eoi(struct irq_data * d)1780 static void pnv_msi_eoi(struct irq_data *d)
1781 {
1782 struct pci_controller *hose = irq_data_get_irq_chip_data(d);
1783 struct pnv_phb *phb = hose->private_data;
1784
1785 if (phb->model == PNV_PHB_MODEL_PHB3) {
1786 /*
1787 * The EOI OPAL call takes an OPAL HW IRQ number but
1788 * since it is translated into a vector number in
1789 * OPAL, use that directly.
1790 */
1791 WARN_ON_ONCE(opal_pci_msi_eoi(phb->opal_id, d->hwirq));
1792 }
1793
1794 irq_chip_eoi_parent(d);
1795 }
1796
1797 static struct irq_chip pnv_msi_irq_chip = {
1798 .name = "PNV-MSI",
1799 .irq_shutdown = pnv_msi_shutdown,
1800 .irq_mask = irq_chip_mask_parent,
1801 .irq_unmask = irq_chip_unmask_parent,
1802 .irq_eoi = pnv_msi_eoi,
1803 .irq_set_affinity = irq_chip_set_affinity_parent,
1804 .irq_compose_msi_msg = pnv_msi_compose_msg,
1805 };
1806
pnv_irq_parent_domain_alloc(struct irq_domain * domain,unsigned int virq,int hwirq)1807 static int pnv_irq_parent_domain_alloc(struct irq_domain *domain,
1808 unsigned int virq, int hwirq)
1809 {
1810 struct irq_fwspec parent_fwspec;
1811 int ret;
1812
1813 parent_fwspec.fwnode = domain->parent->fwnode;
1814 parent_fwspec.param_count = 2;
1815 parent_fwspec.param[0] = hwirq;
1816 parent_fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
1817
1818 ret = irq_domain_alloc_irqs_parent(domain, virq, 1, &parent_fwspec);
1819 if (ret)
1820 return ret;
1821
1822 return 0;
1823 }
1824
pnv_irq_domain_alloc(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs,void * arg)1825 static int pnv_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
1826 unsigned int nr_irqs, void *arg)
1827 {
1828 struct pci_controller *hose = domain->host_data;
1829 struct pnv_phb *phb = hose->private_data;
1830 msi_alloc_info_t *info = arg;
1831 struct pci_dev *pdev = msi_desc_to_pci_dev(info->desc);
1832 int hwirq;
1833 int i, ret;
1834
1835 hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, nr_irqs);
1836 if (hwirq < 0) {
1837 dev_warn(&pdev->dev, "failed to find a free MSI\n");
1838 return -ENOSPC;
1839 }
1840
1841 dev_dbg(&pdev->dev, "%s bridge %pOF %d/%x #%d\n", __func__,
1842 hose->dn, virq, hwirq, nr_irqs);
1843
1844 for (i = 0; i < nr_irqs; i++) {
1845 ret = pnv_irq_parent_domain_alloc(domain, virq + i,
1846 phb->msi_base + hwirq + i);
1847 if (ret)
1848 goto out;
1849
1850 irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
1851 &pnv_msi_irq_chip, hose);
1852 }
1853
1854 return 0;
1855
1856 out:
1857 irq_domain_free_irqs_parent(domain, virq, i - 1);
1858 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, nr_irqs);
1859 return ret;
1860 }
1861
pnv_irq_domain_free(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)1862 static void pnv_irq_domain_free(struct irq_domain *domain, unsigned int virq,
1863 unsigned int nr_irqs)
1864 {
1865 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
1866 struct pci_controller *hose = irq_data_get_irq_chip_data(d);
1867 struct pnv_phb *phb = hose->private_data;
1868
1869 pr_debug("%s bridge %pOF %d/%lx #%d\n", __func__, hose->dn,
1870 virq, d->hwirq, nr_irqs);
1871
1872 msi_bitmap_free_hwirqs(&phb->msi_bmp, d->hwirq, nr_irqs);
1873 /* XIVE domain is cleared through ->msi_free() */
1874 }
1875
1876 static const struct irq_domain_ops pnv_irq_domain_ops = {
1877 .alloc = pnv_irq_domain_alloc,
1878 .free = pnv_irq_domain_free,
1879 };
1880
pnv_msi_allocate_domains(struct pci_controller * hose,unsigned int count)1881 static int __init pnv_msi_allocate_domains(struct pci_controller *hose, unsigned int count)
1882 {
1883 struct pnv_phb *phb = hose->private_data;
1884 struct irq_domain *parent = irq_get_default_domain();
1885
1886 hose->fwnode = irq_domain_alloc_named_id_fwnode("PNV-MSI", phb->opal_id);
1887 if (!hose->fwnode)
1888 return -ENOMEM;
1889
1890 hose->dev_domain = irq_domain_create_hierarchy(parent, 0, count,
1891 hose->fwnode,
1892 &pnv_irq_domain_ops, hose);
1893 if (!hose->dev_domain) {
1894 pr_err("PCI: failed to create IRQ domain bridge %pOF (domain %d)\n",
1895 hose->dn, hose->global_number);
1896 irq_domain_free_fwnode(hose->fwnode);
1897 return -ENOMEM;
1898 }
1899
1900 hose->msi_domain = pci_msi_create_irq_domain(of_fwnode_handle(hose->dn),
1901 &pnv_msi_domain_info,
1902 hose->dev_domain);
1903 if (!hose->msi_domain) {
1904 pr_err("PCI: failed to create MSI IRQ domain bridge %pOF (domain %d)\n",
1905 hose->dn, hose->global_number);
1906 irq_domain_free_fwnode(hose->fwnode);
1907 irq_domain_remove(hose->dev_domain);
1908 return -ENOMEM;
1909 }
1910
1911 return 0;
1912 }
1913
pnv_pci_init_ioda_msis(struct pnv_phb * phb)1914 static void __init pnv_pci_init_ioda_msis(struct pnv_phb *phb)
1915 {
1916 unsigned int count;
1917 const __be32 *prop = of_get_property(phb->hose->dn,
1918 "ibm,opal-msi-ranges", NULL);
1919 if (!prop) {
1920 /* BML Fallback */
1921 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
1922 }
1923 if (!prop)
1924 return;
1925
1926 phb->msi_base = be32_to_cpup(prop);
1927 count = be32_to_cpup(prop + 1);
1928 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
1929 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
1930 phb->hose->global_number);
1931 return;
1932 }
1933
1934 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
1935 count, phb->msi_base);
1936
1937 pnv_msi_allocate_domains(phb->hose, count);
1938 }
1939
pnv_ioda_setup_pe_res(struct pnv_ioda_pe * pe,struct resource * res)1940 static void pnv_ioda_setup_pe_res(struct pnv_ioda_pe *pe,
1941 struct resource *res)
1942 {
1943 struct pnv_phb *phb = pe->phb;
1944 struct pci_bus_region region;
1945 int index;
1946 int64_t rc;
1947
1948 if (!res || !res->flags || res->start > res->end ||
1949 res->flags & IORESOURCE_UNSET)
1950 return;
1951
1952 if (res->flags & IORESOURCE_IO) {
1953 region.start = res->start - phb->ioda.io_pci_base;
1954 region.end = res->end - phb->ioda.io_pci_base;
1955 index = region.start / phb->ioda.io_segsize;
1956
1957 while (index < phb->ioda.total_pe_num &&
1958 region.start <= region.end) {
1959 phb->ioda.io_segmap[index] = pe->pe_number;
1960 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1961 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
1962 if (rc != OPAL_SUCCESS) {
1963 pr_err("%s: Error %lld mapping IO segment#%d to PE#%x\n",
1964 __func__, rc, index, pe->pe_number);
1965 break;
1966 }
1967
1968 region.start += phb->ioda.io_segsize;
1969 index++;
1970 }
1971 } else if ((res->flags & IORESOURCE_MEM) &&
1972 !pnv_pci_is_m64(phb, res)) {
1973 region.start = res->start -
1974 phb->hose->mem_offset[0] -
1975 phb->ioda.m32_pci_base;
1976 region.end = res->end -
1977 phb->hose->mem_offset[0] -
1978 phb->ioda.m32_pci_base;
1979 index = region.start / phb->ioda.m32_segsize;
1980
1981 while (index < phb->ioda.total_pe_num &&
1982 region.start <= region.end) {
1983 phb->ioda.m32_segmap[index] = pe->pe_number;
1984 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
1985 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
1986 if (rc != OPAL_SUCCESS) {
1987 pr_err("%s: Error %lld mapping M32 segment#%d to PE#%x",
1988 __func__, rc, index, pe->pe_number);
1989 break;
1990 }
1991
1992 region.start += phb->ioda.m32_segsize;
1993 index++;
1994 }
1995 }
1996 }
1997
1998 /*
1999 * This function is supposed to be called on basis of PE from top
2000 * to bottom style. So the I/O or MMIO segment assigned to
2001 * parent PE could be overridden by its child PEs if necessary.
2002 */
pnv_ioda_setup_pe_seg(struct pnv_ioda_pe * pe)2003 static void pnv_ioda_setup_pe_seg(struct pnv_ioda_pe *pe)
2004 {
2005 struct pci_dev *pdev;
2006 int i;
2007
2008 /*
2009 * NOTE: We only care PCI bus based PE for now. For PCI
2010 * device based PE, for example SRIOV sensitive VF should
2011 * be figured out later.
2012 */
2013 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
2014
2015 list_for_each_entry(pdev, &pe->pbus->devices, bus_list) {
2016 for (i = 0; i <= PCI_ROM_RESOURCE; i++)
2017 pnv_ioda_setup_pe_res(pe, &pdev->resource[i]);
2018
2019 /*
2020 * If the PE contains all subordinate PCI buses, the
2021 * windows of the child bridges should be mapped to
2022 * the PE as well.
2023 */
2024 if (!(pe->flags & PNV_IODA_PE_BUS_ALL) || !pci_is_bridge(pdev))
2025 continue;
2026 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
2027 pnv_ioda_setup_pe_res(pe,
2028 &pdev->resource[PCI_BRIDGE_RESOURCES + i]);
2029 }
2030 }
2031
2032 #ifdef CONFIG_DEBUG_FS
pnv_pci_diag_data_set(void * data,u64 val)2033 static int pnv_pci_diag_data_set(void *data, u64 val)
2034 {
2035 struct pnv_phb *phb = data;
2036 s64 ret;
2037
2038 /* Retrieve the diag data from firmware */
2039 ret = opal_pci_get_phb_diag_data2(phb->opal_id, phb->diag_data,
2040 phb->diag_data_size);
2041 if (ret != OPAL_SUCCESS)
2042 return -EIO;
2043
2044 /* Print the diag data to the kernel log */
2045 pnv_pci_dump_phb_diag_data(phb->hose, phb->diag_data);
2046 return 0;
2047 }
2048
2049 DEFINE_DEBUGFS_ATTRIBUTE(pnv_pci_diag_data_fops, NULL, pnv_pci_diag_data_set,
2050 "%llu\n");
2051
pnv_pci_ioda_pe_dump(void * data,u64 val)2052 static int pnv_pci_ioda_pe_dump(void *data, u64 val)
2053 {
2054 struct pnv_phb *phb = data;
2055 int pe_num;
2056
2057 for (pe_num = 0; pe_num < phb->ioda.total_pe_num; pe_num++) {
2058 struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_num];
2059
2060 if (!test_bit(pe_num, phb->ioda.pe_alloc))
2061 continue;
2062
2063 pe_warn(pe, "rid: %04x dev count: %2d flags: %s%s%s%s%s%s\n",
2064 pe->rid, pe->device_count,
2065 (pe->flags & PNV_IODA_PE_DEV) ? "dev " : "",
2066 (pe->flags & PNV_IODA_PE_BUS) ? "bus " : "",
2067 (pe->flags & PNV_IODA_PE_BUS_ALL) ? "all " : "",
2068 (pe->flags & PNV_IODA_PE_MASTER) ? "master " : "",
2069 (pe->flags & PNV_IODA_PE_SLAVE) ? "slave " : "",
2070 (pe->flags & PNV_IODA_PE_VF) ? "vf " : "");
2071 }
2072
2073 return 0;
2074 }
2075
2076 DEFINE_DEBUGFS_ATTRIBUTE(pnv_pci_ioda_pe_dump_fops, NULL,
2077 pnv_pci_ioda_pe_dump, "%llu\n");
2078
2079 #endif /* CONFIG_DEBUG_FS */
2080
pnv_pci_ioda_create_dbgfs(void)2081 static void pnv_pci_ioda_create_dbgfs(void)
2082 {
2083 #ifdef CONFIG_DEBUG_FS
2084 struct pci_controller *hose, *tmp;
2085 struct pnv_phb *phb;
2086 char name[16];
2087
2088 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
2089 phb = hose->private_data;
2090
2091 sprintf(name, "PCI%04x", hose->global_number);
2092 phb->dbgfs = debugfs_create_dir(name, arch_debugfs_dir);
2093
2094 debugfs_create_file_unsafe("dump_diag_regs", 0200, phb->dbgfs,
2095 phb, &pnv_pci_diag_data_fops);
2096 debugfs_create_file_unsafe("dump_ioda_pe_state", 0200, phb->dbgfs,
2097 phb, &pnv_pci_ioda_pe_dump_fops);
2098 }
2099 #endif /* CONFIG_DEBUG_FS */
2100 }
2101
pnv_pci_enable_bridge(struct pci_bus * bus)2102 static void pnv_pci_enable_bridge(struct pci_bus *bus)
2103 {
2104 struct pci_dev *dev = bus->self;
2105 struct pci_bus *child;
2106
2107 /* Empty bus ? bail */
2108 if (list_empty(&bus->devices))
2109 return;
2110
2111 /*
2112 * If there's a bridge associated with that bus enable it. This works
2113 * around races in the generic code if the enabling is done during
2114 * parallel probing. This can be removed once those races have been
2115 * fixed.
2116 */
2117 if (dev) {
2118 int rc = pci_enable_device(dev);
2119 if (rc)
2120 pci_err(dev, "Error enabling bridge (%d)\n", rc);
2121 pci_set_master(dev);
2122 }
2123
2124 /* Perform the same to child busses */
2125 list_for_each_entry(child, &bus->children, node)
2126 pnv_pci_enable_bridge(child);
2127 }
2128
pnv_pci_enable_bridges(void)2129 static void pnv_pci_enable_bridges(void)
2130 {
2131 struct pci_controller *hose;
2132
2133 list_for_each_entry(hose, &hose_list, list_node)
2134 pnv_pci_enable_bridge(hose->bus);
2135 }
2136
pnv_pci_ioda_fixup(void)2137 static void pnv_pci_ioda_fixup(void)
2138 {
2139 pnv_pci_ioda_create_dbgfs();
2140
2141 pnv_pci_enable_bridges();
2142
2143 #ifdef CONFIG_EEH
2144 pnv_eeh_post_init();
2145 #endif
2146 }
2147
2148 /*
2149 * Returns the alignment for I/O or memory windows for P2P
2150 * bridges. That actually depends on how PEs are segmented.
2151 * For now, we return I/O or M32 segment size for PE sensitive
2152 * P2P bridges. Otherwise, the default values (4KiB for I/O,
2153 * 1MiB for memory) will be returned.
2154 *
2155 * The current PCI bus might be put into one PE, which was
2156 * create against the parent PCI bridge. For that case, we
2157 * needn't enlarge the alignment so that we can save some
2158 * resources.
2159 */
pnv_pci_window_alignment(struct pci_bus * bus,unsigned long type)2160 static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
2161 unsigned long type)
2162 {
2163 struct pnv_phb *phb = pci_bus_to_pnvhb(bus);
2164 int num_pci_bridges = 0;
2165 struct pci_dev *bridge;
2166
2167 bridge = bus->self;
2168 while (bridge) {
2169 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
2170 num_pci_bridges++;
2171 if (num_pci_bridges >= 2)
2172 return 1;
2173 }
2174
2175 bridge = bridge->bus->self;
2176 }
2177
2178 /*
2179 * We fall back to M32 if M64 isn't supported. We enforce the M64
2180 * alignment for any 64-bit resource, PCIe doesn't care and
2181 * bridges only do 64-bit prefetchable anyway.
2182 */
2183 if (phb->ioda.m64_segsize && pnv_pci_is_m64_flags(type))
2184 return phb->ioda.m64_segsize;
2185 if (type & IORESOURCE_MEM)
2186 return phb->ioda.m32_segsize;
2187
2188 return phb->ioda.io_segsize;
2189 }
2190
2191 /*
2192 * We are updating root port or the upstream port of the
2193 * bridge behind the root port with PHB's windows in order
2194 * to accommodate the changes on required resources during
2195 * PCI (slot) hotplug, which is connected to either root
2196 * port or the downstream ports of PCIe switch behind the
2197 * root port.
2198 */
pnv_pci_fixup_bridge_resources(struct pci_bus * bus,unsigned long type)2199 static void pnv_pci_fixup_bridge_resources(struct pci_bus *bus,
2200 unsigned long type)
2201 {
2202 struct pci_controller *hose = pci_bus_to_host(bus);
2203 struct pnv_phb *phb = hose->private_data;
2204 struct pci_dev *bridge = bus->self;
2205 struct resource *r, *w;
2206 bool msi_region = false;
2207 int i;
2208
2209 /* Check if we need apply fixup to the bridge's windows */
2210 if (!pci_is_root_bus(bridge->bus) &&
2211 !pci_is_root_bus(bridge->bus->self->bus))
2212 return;
2213
2214 /* Fixup the resources */
2215 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
2216 r = &bridge->resource[PCI_BRIDGE_RESOURCES + i];
2217 if (!r->flags || !r->parent)
2218 continue;
2219
2220 w = NULL;
2221 if (r->flags & type & IORESOURCE_IO)
2222 w = &hose->io_resource;
2223 else if (pnv_pci_is_m64(phb, r) &&
2224 (type & IORESOURCE_PREFETCH) &&
2225 phb->ioda.m64_segsize)
2226 w = &hose->mem_resources[1];
2227 else if (r->flags & type & IORESOURCE_MEM) {
2228 w = &hose->mem_resources[0];
2229 msi_region = true;
2230 }
2231
2232 r->start = w->start;
2233 r->end = w->end;
2234
2235 /* The 64KB 32-bits MSI region shouldn't be included in
2236 * the 32-bits bridge window. Otherwise, we can see strange
2237 * issues. One of them is EEH error observed on Garrison.
2238 *
2239 * Exclude top 1MB region which is the minimal alignment of
2240 * 32-bits bridge window.
2241 */
2242 if (msi_region) {
2243 r->end += 0x10000;
2244 r->end -= 0x100000;
2245 }
2246 }
2247 }
2248
pnv_pci_configure_bus(struct pci_bus * bus)2249 static void pnv_pci_configure_bus(struct pci_bus *bus)
2250 {
2251 struct pci_dev *bridge = bus->self;
2252 struct pnv_ioda_pe *pe;
2253 bool all = (bridge && pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE);
2254
2255 dev_info(&bus->dev, "Configuring PE for bus\n");
2256
2257 /* Don't assign PE to PCI bus, which doesn't have subordinate devices */
2258 if (WARN_ON(list_empty(&bus->devices)))
2259 return;
2260
2261 /* Reserve PEs according to used M64 resources */
2262 pnv_ioda_reserve_m64_pe(bus, NULL, all);
2263
2264 /*
2265 * Assign PE. We might run here because of partial hotplug.
2266 * For the case, we just pick up the existing PE and should
2267 * not allocate resources again.
2268 */
2269 pe = pnv_ioda_setup_bus_PE(bus, all);
2270 if (!pe)
2271 return;
2272
2273 pnv_ioda_setup_pe_seg(pe);
2274 }
2275
pnv_pci_default_alignment(void)2276 static resource_size_t pnv_pci_default_alignment(void)
2277 {
2278 return PAGE_SIZE;
2279 }
2280
2281 /* Prevent enabling devices for which we couldn't properly
2282 * assign a PE
2283 */
pnv_pci_enable_device_hook(struct pci_dev * dev)2284 static bool pnv_pci_enable_device_hook(struct pci_dev *dev)
2285 {
2286 struct pci_dn *pdn;
2287
2288 pdn = pci_get_pdn(dev);
2289 if (!pdn || pdn->pe_number == IODA_INVALID_PE) {
2290 pci_err(dev, "pci_enable_device() blocked, no PE assigned.\n");
2291 return false;
2292 }
2293
2294 return true;
2295 }
2296
pnv_ocapi_enable_device_hook(struct pci_dev * dev)2297 static bool pnv_ocapi_enable_device_hook(struct pci_dev *dev)
2298 {
2299 struct pci_dn *pdn;
2300 struct pnv_ioda_pe *pe;
2301
2302 pdn = pci_get_pdn(dev);
2303 if (!pdn)
2304 return false;
2305
2306 if (pdn->pe_number == IODA_INVALID_PE) {
2307 pe = pnv_ioda_setup_dev_PE(dev);
2308 if (!pe)
2309 return false;
2310 }
2311 return true;
2312 }
2313
pnv_pci_ioda2_release_pe_dma(struct pnv_ioda_pe * pe)2314 void pnv_pci_ioda2_release_pe_dma(struct pnv_ioda_pe *pe)
2315 {
2316 struct iommu_table *tbl = pe->table_group.tables[0];
2317 int64_t rc;
2318
2319 if (!pe->dma_setup_done)
2320 return;
2321
2322 rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);
2323 if (rc)
2324 pe_warn(pe, "OPAL error %lld release DMA window\n", rc);
2325
2326 pnv_pci_ioda2_set_bypass(pe, false);
2327 if (pe->table_group.group) {
2328 iommu_group_put(pe->table_group.group);
2329 WARN_ON(pe->table_group.group);
2330 }
2331
2332 iommu_tce_table_put(tbl);
2333 }
2334
pnv_ioda_free_pe_seg(struct pnv_ioda_pe * pe,unsigned short win,unsigned int * map)2335 static void pnv_ioda_free_pe_seg(struct pnv_ioda_pe *pe,
2336 unsigned short win,
2337 unsigned int *map)
2338 {
2339 struct pnv_phb *phb = pe->phb;
2340 int idx;
2341 int64_t rc;
2342
2343 for (idx = 0; idx < phb->ioda.total_pe_num; idx++) {
2344 if (map[idx] != pe->pe_number)
2345 continue;
2346
2347 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
2348 phb->ioda.reserved_pe_idx, win, 0, idx);
2349
2350 if (rc != OPAL_SUCCESS)
2351 pe_warn(pe, "Error %lld unmapping (%d) segment#%d\n",
2352 rc, win, idx);
2353
2354 map[idx] = IODA_INVALID_PE;
2355 }
2356 }
2357
pnv_ioda_release_pe_seg(struct pnv_ioda_pe * pe)2358 static void pnv_ioda_release_pe_seg(struct pnv_ioda_pe *pe)
2359 {
2360 struct pnv_phb *phb = pe->phb;
2361
2362 if (phb->type == PNV_PHB_IODA2) {
2363 pnv_ioda_free_pe_seg(pe, OPAL_M32_WINDOW_TYPE,
2364 phb->ioda.m32_segmap);
2365 }
2366 }
2367
pnv_ioda_release_pe(struct pnv_ioda_pe * pe)2368 static void pnv_ioda_release_pe(struct pnv_ioda_pe *pe)
2369 {
2370 struct pnv_phb *phb = pe->phb;
2371 struct pnv_ioda_pe *slave, *tmp;
2372
2373 pe_info(pe, "Releasing PE\n");
2374
2375 mutex_lock(&phb->ioda.pe_list_mutex);
2376 list_del(&pe->list);
2377 mutex_unlock(&phb->ioda.pe_list_mutex);
2378
2379 switch (phb->type) {
2380 case PNV_PHB_IODA2:
2381 pnv_pci_ioda2_release_pe_dma(pe);
2382 break;
2383 case PNV_PHB_NPU_OCAPI:
2384 break;
2385 default:
2386 WARN_ON(1);
2387 }
2388
2389 pnv_ioda_release_pe_seg(pe);
2390 pnv_ioda_deconfigure_pe(pe->phb, pe);
2391
2392 /* Release slave PEs in the compound PE */
2393 if (pe->flags & PNV_IODA_PE_MASTER) {
2394 list_for_each_entry_safe(slave, tmp, &pe->slaves, list) {
2395 list_del(&slave->list);
2396 pnv_ioda_free_pe(slave);
2397 }
2398 }
2399
2400 /*
2401 * The PE for root bus can be removed because of hotplug in EEH
2402 * recovery for fenced PHB error. We need to mark the PE dead so
2403 * that it can be populated again in PCI hot add path. The PE
2404 * shouldn't be destroyed as it's the global reserved resource.
2405 */
2406 if (phb->ioda.root_pe_idx == pe->pe_number)
2407 return;
2408
2409 pnv_ioda_free_pe(pe);
2410 }
2411
pnv_pci_release_device(struct pci_dev * pdev)2412 static void pnv_pci_release_device(struct pci_dev *pdev)
2413 {
2414 struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
2415 struct pci_dn *pdn = pci_get_pdn(pdev);
2416 struct pnv_ioda_pe *pe;
2417
2418 /* The VF PE state is torn down when sriov_disable() is called */
2419 if (pdev->is_virtfn)
2420 return;
2421
2422 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
2423 return;
2424
2425 #ifdef CONFIG_PCI_IOV
2426 /*
2427 * FIXME: Try move this to sriov_disable(). It's here since we allocate
2428 * the iov state at probe time since we need to fiddle with the IOV
2429 * resources.
2430 */
2431 if (pdev->is_physfn)
2432 kfree(pdev->dev.archdata.iov_data);
2433 #endif
2434
2435 /*
2436 * PCI hotplug can happen as part of EEH error recovery. The @pdn
2437 * isn't removed and added afterwards in this scenario. We should
2438 * set the PE number in @pdn to an invalid one. Otherwise, the PE's
2439 * device count is decreased on removing devices while failing to
2440 * be increased on adding devices. It leads to unbalanced PE's device
2441 * count and eventually make normal PCI hotplug path broken.
2442 */
2443 pe = &phb->ioda.pe_array[pdn->pe_number];
2444 pdn->pe_number = IODA_INVALID_PE;
2445
2446 WARN_ON(--pe->device_count < 0);
2447 if (pe->device_count == 0)
2448 pnv_ioda_release_pe(pe);
2449 }
2450
pnv_pci_ioda_shutdown(struct pci_controller * hose)2451 static void pnv_pci_ioda_shutdown(struct pci_controller *hose)
2452 {
2453 struct pnv_phb *phb = hose->private_data;
2454
2455 opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
2456 OPAL_ASSERT_RESET);
2457 }
2458
pnv_pci_ioda_dma_bus_setup(struct pci_bus * bus)2459 static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus)
2460 {
2461 struct pnv_phb *phb = pci_bus_to_pnvhb(bus);
2462 struct pnv_ioda_pe *pe;
2463
2464 list_for_each_entry(pe, &phb->ioda.pe_list, list) {
2465 if (!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)))
2466 continue;
2467
2468 if (!pe->pbus)
2469 continue;
2470
2471 if (bus->number == ((pe->rid >> 8) & 0xFF)) {
2472 pe->pbus = bus;
2473 break;
2474 }
2475 }
2476 }
2477
2478 #ifdef CONFIG_IOMMU_API
pnv_pci_device_group(struct pci_controller * hose,struct pci_dev * pdev)2479 static struct iommu_group *pnv_pci_device_group(struct pci_controller *hose,
2480 struct pci_dev *pdev)
2481 {
2482 struct pnv_phb *phb = hose->private_data;
2483 struct pnv_ioda_pe *pe;
2484
2485 if (WARN_ON(!phb))
2486 return ERR_PTR(-ENODEV);
2487
2488 pe = pnv_pci_bdfn_to_pe(phb, pci_dev_id(pdev));
2489 if (!pe)
2490 return ERR_PTR(-ENODEV);
2491
2492 if (!pe->table_group.group)
2493 return ERR_PTR(-ENODEV);
2494
2495 return iommu_group_ref_get(pe->table_group.group);
2496 }
2497 #endif
2498
2499 static const struct pci_controller_ops pnv_pci_ioda_controller_ops = {
2500 .dma_dev_setup = pnv_pci_ioda_dma_dev_setup,
2501 .dma_bus_setup = pnv_pci_ioda_dma_bus_setup,
2502 .iommu_bypass_supported = pnv_pci_ioda_iommu_bypass_supported,
2503 .enable_device_hook = pnv_pci_enable_device_hook,
2504 .release_device = pnv_pci_release_device,
2505 .window_alignment = pnv_pci_window_alignment,
2506 .setup_bridge = pnv_pci_fixup_bridge_resources,
2507 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
2508 .shutdown = pnv_pci_ioda_shutdown,
2509 #ifdef CONFIG_IOMMU_API
2510 .device_group = pnv_pci_device_group,
2511 #endif
2512 };
2513
2514 static const struct pci_controller_ops pnv_npu_ocapi_ioda_controller_ops = {
2515 .enable_device_hook = pnv_ocapi_enable_device_hook,
2516 .release_device = pnv_pci_release_device,
2517 .window_alignment = pnv_pci_window_alignment,
2518 .reset_secondary_bus = pnv_pci_reset_secondary_bus,
2519 .shutdown = pnv_pci_ioda_shutdown,
2520 };
2521
pnv_pci_init_ioda_phb(struct device_node * np,u64 hub_id,int ioda_type)2522 static void __init pnv_pci_init_ioda_phb(struct device_node *np,
2523 u64 hub_id, int ioda_type)
2524 {
2525 struct pci_controller *hose;
2526 struct pnv_phb *phb;
2527 unsigned long size, m64map_off, m32map_off, pemap_off;
2528 struct pnv_ioda_pe *root_pe;
2529 struct resource r;
2530 const __be64 *prop64;
2531 const __be32 *prop32;
2532 int len;
2533 unsigned int segno;
2534 u64 phb_id;
2535 void *aux;
2536 long rc;
2537
2538 if (!of_device_is_available(np))
2539 return;
2540
2541 pr_info("Initializing %s PHB (%pOF)\n", pnv_phb_names[ioda_type], np);
2542
2543 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
2544 if (!prop64) {
2545 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
2546 return;
2547 }
2548 phb_id = be64_to_cpup(prop64);
2549 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
2550
2551 phb = kzalloc(sizeof(*phb), GFP_KERNEL);
2552 if (!phb)
2553 panic("%s: Failed to allocate %zu bytes\n", __func__,
2554 sizeof(*phb));
2555
2556 /* Allocate PCI controller */
2557 phb->hose = hose = pcibios_alloc_controller(np);
2558 if (!phb->hose) {
2559 pr_err(" Can't allocate PCI controller for %pOF\n",
2560 np);
2561 memblock_free(phb, sizeof(struct pnv_phb));
2562 return;
2563 }
2564
2565 spin_lock_init(&phb->lock);
2566 prop32 = of_get_property(np, "bus-range", &len);
2567 if (prop32 && len == 8) {
2568 hose->first_busno = be32_to_cpu(prop32[0]);
2569 hose->last_busno = be32_to_cpu(prop32[1]);
2570 } else {
2571 pr_warn(" Broken <bus-range> on %pOF\n", np);
2572 hose->first_busno = 0;
2573 hose->last_busno = 0xff;
2574 }
2575 hose->private_data = phb;
2576 phb->hub_id = hub_id;
2577 phb->opal_id = phb_id;
2578 phb->type = ioda_type;
2579 mutex_init(&phb->ioda.pe_alloc_mutex);
2580
2581 /* Detect specific models for error handling */
2582 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
2583 phb->model = PNV_PHB_MODEL_P7IOC;
2584 else if (of_device_is_compatible(np, "ibm,power8-pciex"))
2585 phb->model = PNV_PHB_MODEL_PHB3;
2586 else
2587 phb->model = PNV_PHB_MODEL_UNKNOWN;
2588
2589 /* Initialize diagnostic data buffer */
2590 prop32 = of_get_property(np, "ibm,phb-diag-data-size", NULL);
2591 if (prop32)
2592 phb->diag_data_size = be32_to_cpup(prop32);
2593 else
2594 phb->diag_data_size = PNV_PCI_DIAG_BUF_SIZE;
2595
2596 phb->diag_data = kzalloc(phb->diag_data_size, GFP_KERNEL);
2597 if (!phb->diag_data)
2598 panic("%s: Failed to allocate %u bytes\n", __func__,
2599 phb->diag_data_size);
2600
2601 /* Parse 32-bit and IO ranges (if any) */
2602 pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
2603
2604 /* Get registers */
2605 if (!of_address_to_resource(np, 0, &r)) {
2606 phb->regs_phys = r.start;
2607 phb->regs = ioremap(r.start, resource_size(&r));
2608 if (phb->regs == NULL)
2609 pr_err(" Failed to map registers !\n");
2610 }
2611
2612 /* Initialize more IODA stuff */
2613 phb->ioda.total_pe_num = 1;
2614 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
2615 if (prop32)
2616 phb->ioda.total_pe_num = be32_to_cpup(prop32);
2617 prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
2618 if (prop32)
2619 phb->ioda.reserved_pe_idx = be32_to_cpup(prop32);
2620
2621 /* Invalidate RID to PE# mapping */
2622 for (segno = 0; segno < ARRAY_SIZE(phb->ioda.pe_rmap); segno++)
2623 phb->ioda.pe_rmap[segno] = IODA_INVALID_PE;
2624
2625 /* Parse 64-bit MMIO range */
2626 pnv_ioda_parse_m64_window(phb);
2627
2628 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
2629 /* FW Has already off top 64k of M32 space (MSI space) */
2630 phb->ioda.m32_size += 0x10000;
2631
2632 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe_num;
2633 phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
2634 phb->ioda.io_size = hose->pci_io_size;
2635 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe_num;
2636 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
2637
2638 /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
2639 size = ALIGN(max_t(unsigned, phb->ioda.total_pe_num, 8) / 8,
2640 sizeof(unsigned long));
2641 m64map_off = size;
2642 size += phb->ioda.total_pe_num * sizeof(phb->ioda.m64_segmap[0]);
2643 m32map_off = size;
2644 size += phb->ioda.total_pe_num * sizeof(phb->ioda.m32_segmap[0]);
2645 pemap_off = size;
2646 size += phb->ioda.total_pe_num * sizeof(struct pnv_ioda_pe);
2647 aux = kzalloc(size, GFP_KERNEL);
2648 if (!aux)
2649 panic("%s: Failed to allocate %lu bytes\n", __func__, size);
2650
2651 phb->ioda.pe_alloc = aux;
2652 phb->ioda.m64_segmap = aux + m64map_off;
2653 phb->ioda.m32_segmap = aux + m32map_off;
2654 for (segno = 0; segno < phb->ioda.total_pe_num; segno++) {
2655 phb->ioda.m64_segmap[segno] = IODA_INVALID_PE;
2656 phb->ioda.m32_segmap[segno] = IODA_INVALID_PE;
2657 }
2658 phb->ioda.pe_array = aux + pemap_off;
2659
2660 /*
2661 * Choose PE number for root bus, which shouldn't have
2662 * M64 resources consumed by its child devices. To pick
2663 * the PE number adjacent to the reserved one if possible.
2664 */
2665 pnv_ioda_reserve_pe(phb, phb->ioda.reserved_pe_idx);
2666 if (phb->ioda.reserved_pe_idx == 0) {
2667 phb->ioda.root_pe_idx = 1;
2668 pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx);
2669 } else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1)) {
2670 phb->ioda.root_pe_idx = phb->ioda.reserved_pe_idx - 1;
2671 pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx);
2672 } else {
2673 /* otherwise just allocate one */
2674 root_pe = pnv_ioda_alloc_pe(phb, 1);
2675 phb->ioda.root_pe_idx = root_pe->pe_number;
2676 }
2677
2678 INIT_LIST_HEAD(&phb->ioda.pe_list);
2679 mutex_init(&phb->ioda.pe_list_mutex);
2680
2681 #if 0 /* We should really do that ... */
2682 rc = opal_pci_set_phb_mem_window(opal->phb_id,
2683 window_type,
2684 window_num,
2685 starting_real_address,
2686 starting_pci_address,
2687 segment_size);
2688 #endif
2689
2690 pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
2691 phb->ioda.total_pe_num, phb->ioda.reserved_pe_idx,
2692 phb->ioda.m32_size, phb->ioda.m32_segsize);
2693 if (phb->ioda.m64_size)
2694 pr_info(" M64: 0x%lx [segment=0x%lx]\n",
2695 phb->ioda.m64_size, phb->ioda.m64_segsize);
2696 if (phb->ioda.io_size)
2697 pr_info(" IO: 0x%x [segment=0x%x]\n",
2698 phb->ioda.io_size, phb->ioda.io_segsize);
2699
2700
2701 phb->hose->ops = &pnv_pci_ops;
2702 phb->get_pe_state = pnv_ioda_get_pe_state;
2703 phb->freeze_pe = pnv_ioda_freeze_pe;
2704 phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
2705
2706 /* Setup MSI support */
2707 pnv_pci_init_ioda_msis(phb);
2708
2709 /*
2710 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
2711 * to let the PCI core do resource assignment. It's supposed
2712 * that the PCI core will do correct I/O and MMIO alignment
2713 * for the P2P bridge bars so that each PCI bus (excluding
2714 * the child P2P bridges) can form individual PE.
2715 */
2716 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
2717
2718 switch (phb->type) {
2719 case PNV_PHB_NPU_OCAPI:
2720 hose->controller_ops = pnv_npu_ocapi_ioda_controller_ops;
2721 break;
2722 default:
2723 hose->controller_ops = pnv_pci_ioda_controller_ops;
2724 }
2725
2726 ppc_md.pcibios_default_alignment = pnv_pci_default_alignment;
2727
2728 #ifdef CONFIG_PCI_IOV
2729 ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov;
2730 ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;
2731 ppc_md.pcibios_sriov_enable = pnv_pcibios_sriov_enable;
2732 ppc_md.pcibios_sriov_disable = pnv_pcibios_sriov_disable;
2733 #endif
2734
2735 pci_add_flags(PCI_REASSIGN_ALL_RSRC);
2736
2737 /* Reset IODA tables to a clean state */
2738 rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
2739 if (rc)
2740 pr_warn(" OPAL Error %ld performing IODA table reset !\n", rc);
2741
2742 /*
2743 * If we're running in kdump kernel, the previous kernel never
2744 * shutdown PCI devices correctly. We already got IODA table
2745 * cleaned out. So we have to issue PHB reset to stop all PCI
2746 * transactions from previous kernel. The ppc_pci_reset_phbs
2747 * kernel parameter will force this reset too. Additionally,
2748 * if the IODA reset above failed then use a bigger hammer.
2749 * This can happen if we get a PHB fatal error in very early
2750 * boot.
2751 */
2752 if (is_kdump_kernel() || pci_reset_phbs || rc) {
2753 pr_info(" Issue PHB reset ...\n");
2754 pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
2755 pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
2756 }
2757
2758 /* Remove M64 resource if we can't configure it successfully */
2759 if (!phb->init_m64 || phb->init_m64(phb))
2760 hose->mem_resources[1].flags = 0;
2761
2762 /* create pci_dn's for DT nodes under this PHB */
2763 pci_devs_phb_init_dynamic(hose);
2764 }
2765
pnv_pci_init_ioda2_phb(struct device_node * np)2766 void __init pnv_pci_init_ioda2_phb(struct device_node *np)
2767 {
2768 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
2769 }
2770
pnv_pci_init_npu2_opencapi_phb(struct device_node * np)2771 void __init pnv_pci_init_npu2_opencapi_phb(struct device_node *np)
2772 {
2773 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_NPU_OCAPI);
2774 }
2775
pnv_npu2_opencapi_cfg_size_fixup(struct pci_dev * dev)2776 static void pnv_npu2_opencapi_cfg_size_fixup(struct pci_dev *dev)
2777 {
2778 struct pnv_phb *phb = pci_bus_to_pnvhb(dev->bus);
2779
2780 if (!machine_is(powernv))
2781 return;
2782
2783 if (phb->type == PNV_PHB_NPU_OCAPI)
2784 dev->cfg_size = PCI_CFG_SPACE_EXP_SIZE;
2785 }
2786 DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, pnv_npu2_opencapi_cfg_size_fixup);
2787