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 24 #include <asm/sections.h> 25 #include <asm/io.h> 26 #include <asm/prom.h> 27 #include <asm/pci-bridge.h> 28 #include <asm/machdep.h> 29 #include <asm/msi_bitmap.h> 30 #include <asm/ppc-pci.h> 31 #include <asm/opal.h> 32 #include <asm/iommu.h> 33 #include <asm/tce.h> 34 #include <asm/xics.h> 35 #include <asm/debugfs.h> 36 #include <asm/firmware.h> 37 #include <asm/pnv-pci.h> 38 #include <asm/mmzone.h> 39 40 #include <misc/cxl-base.h> 41 42 #include "powernv.h" 43 #include "pci.h" 44 #include "../../../../drivers/pci/pci.h" 45 46 #define PNV_IODA1_M64_NUM 16 /* Number of M64 BARs */ 47 #define PNV_IODA1_M64_SEGS 8 /* Segments per M64 BAR */ 48 #define PNV_IODA1_DMA32_SEGSIZE 0x10000000 49 50 static const char * const pnv_phb_names[] = { "IODA1", "IODA2", "NPU_NVLINK", 51 "NPU_OCAPI" }; 52 53 static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable); 54 static void pnv_pci_configure_bus(struct pci_bus *bus); 55 56 void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level, 57 const char *fmt, ...) 58 { 59 struct va_format vaf; 60 va_list args; 61 char pfix[32]; 62 63 va_start(args, fmt); 64 65 vaf.fmt = fmt; 66 vaf.va = &args; 67 68 if (pe->flags & PNV_IODA_PE_DEV) 69 strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix)); 70 else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)) 71 sprintf(pfix, "%04x:%02x ", 72 pci_domain_nr(pe->pbus), pe->pbus->number); 73 #ifdef CONFIG_PCI_IOV 74 else if (pe->flags & PNV_IODA_PE_VF) 75 sprintf(pfix, "%04x:%02x:%2x.%d", 76 pci_domain_nr(pe->parent_dev->bus), 77 (pe->rid & 0xff00) >> 8, 78 PCI_SLOT(pe->rid), PCI_FUNC(pe->rid)); 79 #endif /* CONFIG_PCI_IOV*/ 80 81 printk("%spci %s: [PE# %.2x] %pV", 82 level, pfix, pe->pe_number, &vaf); 83 84 va_end(args); 85 } 86 87 static bool pnv_iommu_bypass_disabled __read_mostly; 88 static bool pci_reset_phbs __read_mostly; 89 90 static int __init iommu_setup(char *str) 91 { 92 if (!str) 93 return -EINVAL; 94 95 while (*str) { 96 if (!strncmp(str, "nobypass", 8)) { 97 pnv_iommu_bypass_disabled = true; 98 pr_info("PowerNV: IOMMU bypass window disabled.\n"); 99 break; 100 } 101 str += strcspn(str, ","); 102 if (*str == ',') 103 str++; 104 } 105 106 return 0; 107 } 108 early_param("iommu", iommu_setup); 109 110 static int __init pci_reset_phbs_setup(char *str) 111 { 112 pci_reset_phbs = true; 113 return 0; 114 } 115 116 early_param("ppc_pci_reset_phbs", pci_reset_phbs_setup); 117 118 static inline bool pnv_pci_is_m64(struct pnv_phb *phb, struct resource *r) 119 { 120 /* 121 * WARNING: We cannot rely on the resource flags. The Linux PCI 122 * allocation code sometimes decides to put a 64-bit prefetchable 123 * BAR in the 32-bit window, so we have to compare the addresses. 124 * 125 * For simplicity we only test resource start. 126 */ 127 return (r->start >= phb->ioda.m64_base && 128 r->start < (phb->ioda.m64_base + phb->ioda.m64_size)); 129 } 130 131 static inline bool pnv_pci_is_m64_flags(unsigned long resource_flags) 132 { 133 unsigned long flags = (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH); 134 135 return (resource_flags & flags) == flags; 136 } 137 138 static struct pnv_ioda_pe *pnv_ioda_init_pe(struct pnv_phb *phb, int pe_no) 139 { 140 s64 rc; 141 142 phb->ioda.pe_array[pe_no].phb = phb; 143 phb->ioda.pe_array[pe_no].pe_number = pe_no; 144 145 /* 146 * Clear the PE frozen state as it might be put into frozen state 147 * in the last PCI remove path. It's not harmful to do so when the 148 * PE is already in unfrozen state. 149 */ 150 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, 151 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL); 152 if (rc != OPAL_SUCCESS && rc != OPAL_UNSUPPORTED) 153 pr_warn("%s: Error %lld unfreezing PHB#%x-PE#%x\n", 154 __func__, rc, phb->hose->global_number, pe_no); 155 156 return &phb->ioda.pe_array[pe_no]; 157 } 158 159 static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int pe_no) 160 { 161 if (!(pe_no >= 0 && pe_no < phb->ioda.total_pe_num)) { 162 pr_warn("%s: Invalid PE %x on PHB#%x\n", 163 __func__, pe_no, phb->hose->global_number); 164 return; 165 } 166 167 if (test_and_set_bit(pe_no, phb->ioda.pe_alloc)) 168 pr_debug("%s: PE %x was reserved on PHB#%x\n", 169 __func__, pe_no, phb->hose->global_number); 170 171 pnv_ioda_init_pe(phb, pe_no); 172 } 173 174 static struct pnv_ioda_pe *pnv_ioda_alloc_pe(struct pnv_phb *phb) 175 { 176 long pe; 177 178 for (pe = phb->ioda.total_pe_num - 1; pe >= 0; pe--) { 179 if (!test_and_set_bit(pe, phb->ioda.pe_alloc)) 180 return pnv_ioda_init_pe(phb, pe); 181 } 182 183 return NULL; 184 } 185 186 static void pnv_ioda_free_pe(struct pnv_ioda_pe *pe) 187 { 188 struct pnv_phb *phb = pe->phb; 189 unsigned int pe_num = pe->pe_number; 190 191 WARN_ON(pe->pdev); 192 WARN_ON(pe->npucomp); /* NPUs for nvlink are not supposed to be freed */ 193 kfree(pe->npucomp); 194 memset(pe, 0, sizeof(struct pnv_ioda_pe)); 195 clear_bit(pe_num, phb->ioda.pe_alloc); 196 } 197 198 /* The default M64 BAR is shared by all PEs */ 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 252 static void pnv_ioda_reserve_dev_m64_pe(struct pci_dev *pdev, 253 unsigned long *pe_bitmap) 254 { 255 struct pci_controller *hose = pci_bus_to_host(pdev->bus); 256 struct pnv_phb *phb = hose->private_data; 257 struct resource *r; 258 resource_size_t base, sgsz, start, end; 259 int segno, i; 260 261 base = phb->ioda.m64_base; 262 sgsz = phb->ioda.m64_segsize; 263 for (i = 0; i <= PCI_ROM_RESOURCE; i++) { 264 r = &pdev->resource[i]; 265 if (!r->parent || !pnv_pci_is_m64(phb, r)) 266 continue; 267 268 start = ALIGN_DOWN(r->start - base, sgsz); 269 end = ALIGN(r->end - base, sgsz); 270 for (segno = start / sgsz; segno < end / sgsz; segno++) { 271 if (pe_bitmap) 272 set_bit(segno, pe_bitmap); 273 else 274 pnv_ioda_reserve_pe(phb, segno); 275 } 276 } 277 } 278 279 static int pnv_ioda1_init_m64(struct pnv_phb *phb) 280 { 281 struct resource *r; 282 int index; 283 284 /* 285 * There are 16 M64 BARs, each of which has 8 segments. So 286 * there are as many M64 segments as the maximum number of 287 * PEs, which is 128. 288 */ 289 for (index = 0; index < PNV_IODA1_M64_NUM; index++) { 290 unsigned long base, segsz = phb->ioda.m64_segsize; 291 int64_t rc; 292 293 base = phb->ioda.m64_base + 294 index * PNV_IODA1_M64_SEGS * segsz; 295 rc = opal_pci_set_phb_mem_window(phb->opal_id, 296 OPAL_M64_WINDOW_TYPE, index, base, 0, 297 PNV_IODA1_M64_SEGS * segsz); 298 if (rc != OPAL_SUCCESS) { 299 pr_warn(" Error %lld setting M64 PHB#%x-BAR#%d\n", 300 rc, phb->hose->global_number, index); 301 goto fail; 302 } 303 304 rc = opal_pci_phb_mmio_enable(phb->opal_id, 305 OPAL_M64_WINDOW_TYPE, index, 306 OPAL_ENABLE_M64_SPLIT); 307 if (rc != OPAL_SUCCESS) { 308 pr_warn(" Error %lld enabling M64 PHB#%x-BAR#%d\n", 309 rc, phb->hose->global_number, index); 310 goto fail; 311 } 312 } 313 314 /* 315 * Exclude the segments for reserved and root bus PE, which 316 * are first or last two PEs. 317 */ 318 r = &phb->hose->mem_resources[1]; 319 if (phb->ioda.reserved_pe_idx == 0) 320 r->start += (2 * phb->ioda.m64_segsize); 321 else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1)) 322 r->end -= (2 * phb->ioda.m64_segsize); 323 else 324 WARN(1, "Wrong reserved PE#%x on PHB#%x\n", 325 phb->ioda.reserved_pe_idx, phb->hose->global_number); 326 327 return 0; 328 329 fail: 330 for ( ; index >= 0; index--) 331 opal_pci_phb_mmio_enable(phb->opal_id, 332 OPAL_M64_WINDOW_TYPE, index, OPAL_DISABLE_M64); 333 334 return -EIO; 335 } 336 337 static void pnv_ioda_reserve_m64_pe(struct pci_bus *bus, 338 unsigned long *pe_bitmap, 339 bool all) 340 { 341 struct pci_dev *pdev; 342 343 list_for_each_entry(pdev, &bus->devices, bus_list) { 344 pnv_ioda_reserve_dev_m64_pe(pdev, pe_bitmap); 345 346 if (all && pdev->subordinate) 347 pnv_ioda_reserve_m64_pe(pdev->subordinate, 348 pe_bitmap, all); 349 } 350 } 351 352 static struct pnv_ioda_pe *pnv_ioda_pick_m64_pe(struct pci_bus *bus, bool all) 353 { 354 struct pci_controller *hose = pci_bus_to_host(bus); 355 struct pnv_phb *phb = hose->private_data; 356 struct pnv_ioda_pe *master_pe, *pe; 357 unsigned long size, *pe_alloc; 358 int i; 359 360 /* Root bus shouldn't use M64 */ 361 if (pci_is_root_bus(bus)) 362 return NULL; 363 364 /* Allocate bitmap */ 365 size = ALIGN(phb->ioda.total_pe_num / 8, sizeof(unsigned long)); 366 pe_alloc = kzalloc(size, GFP_KERNEL); 367 if (!pe_alloc) { 368 pr_warn("%s: Out of memory !\n", 369 __func__); 370 return NULL; 371 } 372 373 /* Figure out reserved PE numbers by the PE */ 374 pnv_ioda_reserve_m64_pe(bus, pe_alloc, all); 375 376 /* 377 * the current bus might not own M64 window and that's all 378 * contributed by its child buses. For the case, we needn't 379 * pick M64 dependent PE#. 380 */ 381 if (bitmap_empty(pe_alloc, phb->ioda.total_pe_num)) { 382 kfree(pe_alloc); 383 return NULL; 384 } 385 386 /* 387 * Figure out the master PE and put all slave PEs to master 388 * PE's list to form compound PE. 389 */ 390 master_pe = NULL; 391 i = -1; 392 while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe_num, i + 1)) < 393 phb->ioda.total_pe_num) { 394 pe = &phb->ioda.pe_array[i]; 395 396 phb->ioda.m64_segmap[pe->pe_number] = pe->pe_number; 397 if (!master_pe) { 398 pe->flags |= PNV_IODA_PE_MASTER; 399 INIT_LIST_HEAD(&pe->slaves); 400 master_pe = pe; 401 } else { 402 pe->flags |= PNV_IODA_PE_SLAVE; 403 pe->master = master_pe; 404 list_add_tail(&pe->list, &master_pe->slaves); 405 } 406 407 /* 408 * P7IOC supports M64DT, which helps mapping M64 segment 409 * to one particular PE#. However, PHB3 has fixed mapping 410 * between M64 segment and PE#. In order to have same logic 411 * for P7IOC and PHB3, we enforce fixed mapping between M64 412 * segment and PE# on P7IOC. 413 */ 414 if (phb->type == PNV_PHB_IODA1) { 415 int64_t rc; 416 417 rc = opal_pci_map_pe_mmio_window(phb->opal_id, 418 pe->pe_number, OPAL_M64_WINDOW_TYPE, 419 pe->pe_number / PNV_IODA1_M64_SEGS, 420 pe->pe_number % PNV_IODA1_M64_SEGS); 421 if (rc != OPAL_SUCCESS) 422 pr_warn("%s: Error %lld mapping M64 for PHB#%x-PE#%x\n", 423 __func__, rc, phb->hose->global_number, 424 pe->pe_number); 425 } 426 } 427 428 kfree(pe_alloc); 429 return master_pe; 430 } 431 432 static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb) 433 { 434 struct pci_controller *hose = phb->hose; 435 struct device_node *dn = hose->dn; 436 struct resource *res; 437 u32 m64_range[2], i; 438 const __be32 *r; 439 u64 pci_addr; 440 441 if (phb->type != PNV_PHB_IODA1 && phb->type != PNV_PHB_IODA2) { 442 pr_info(" Not support M64 window\n"); 443 return; 444 } 445 446 if (!firmware_has_feature(FW_FEATURE_OPAL)) { 447 pr_info(" Firmware too old to support M64 window\n"); 448 return; 449 } 450 451 r = of_get_property(dn, "ibm,opal-m64-window", NULL); 452 if (!r) { 453 pr_info(" No <ibm,opal-m64-window> on %pOF\n", 454 dn); 455 return; 456 } 457 458 /* 459 * Find the available M64 BAR range and pickup the last one for 460 * covering the whole 64-bits space. We support only one range. 461 */ 462 if (of_property_read_u32_array(dn, "ibm,opal-available-m64-ranges", 463 m64_range, 2)) { 464 /* In absence of the property, assume 0..15 */ 465 m64_range[0] = 0; 466 m64_range[1] = 16; 467 } 468 /* We only support 64 bits in our allocator */ 469 if (m64_range[1] > 63) { 470 pr_warn("%s: Limiting M64 range to 63 (from %d) on PHB#%x\n", 471 __func__, m64_range[1], phb->hose->global_number); 472 m64_range[1] = 63; 473 } 474 /* Empty range, no m64 */ 475 if (m64_range[1] <= m64_range[0]) { 476 pr_warn("%s: M64 empty, disabling M64 usage on PHB#%x\n", 477 __func__, phb->hose->global_number); 478 return; 479 } 480 481 /* Configure M64 informations */ 482 res = &hose->mem_resources[1]; 483 res->name = dn->full_name; 484 res->start = of_translate_address(dn, r + 2); 485 res->end = res->start + of_read_number(r + 4, 2) - 1; 486 res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH); 487 pci_addr = of_read_number(r, 2); 488 hose->mem_offset[1] = res->start - pci_addr; 489 490 phb->ioda.m64_size = resource_size(res); 491 phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe_num; 492 phb->ioda.m64_base = pci_addr; 493 494 /* This lines up nicely with the display from processing OF ranges */ 495 pr_info(" MEM 0x%016llx..0x%016llx -> 0x%016llx (M64 #%d..%d)\n", 496 res->start, res->end, pci_addr, m64_range[0], 497 m64_range[0] + m64_range[1] - 1); 498 499 /* Mark all M64 used up by default */ 500 phb->ioda.m64_bar_alloc = (unsigned long)-1; 501 502 /* Use last M64 BAR to cover M64 window */ 503 m64_range[1]--; 504 phb->ioda.m64_bar_idx = m64_range[0] + m64_range[1]; 505 506 pr_info(" Using M64 #%d as default window\n", phb->ioda.m64_bar_idx); 507 508 /* Mark remaining ones free */ 509 for (i = m64_range[0]; i < m64_range[1]; i++) 510 clear_bit(i, &phb->ioda.m64_bar_alloc); 511 512 /* 513 * Setup init functions for M64 based on IODA version, IODA3 uses 514 * the IODA2 code. 515 */ 516 if (phb->type == PNV_PHB_IODA1) 517 phb->init_m64 = pnv_ioda1_init_m64; 518 else 519 phb->init_m64 = pnv_ioda2_init_m64; 520 } 521 522 static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no) 523 { 524 struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no]; 525 struct pnv_ioda_pe *slave; 526 s64 rc; 527 528 /* Fetch master PE */ 529 if (pe->flags & PNV_IODA_PE_SLAVE) { 530 pe = pe->master; 531 if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER))) 532 return; 533 534 pe_no = pe->pe_number; 535 } 536 537 /* Freeze master PE */ 538 rc = opal_pci_eeh_freeze_set(phb->opal_id, 539 pe_no, 540 OPAL_EEH_ACTION_SET_FREEZE_ALL); 541 if (rc != OPAL_SUCCESS) { 542 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n", 543 __func__, rc, phb->hose->global_number, pe_no); 544 return; 545 } 546 547 /* Freeze slave PEs */ 548 if (!(pe->flags & PNV_IODA_PE_MASTER)) 549 return; 550 551 list_for_each_entry(slave, &pe->slaves, list) { 552 rc = opal_pci_eeh_freeze_set(phb->opal_id, 553 slave->pe_number, 554 OPAL_EEH_ACTION_SET_FREEZE_ALL); 555 if (rc != OPAL_SUCCESS) 556 pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n", 557 __func__, rc, phb->hose->global_number, 558 slave->pe_number); 559 } 560 } 561 562 static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt) 563 { 564 struct pnv_ioda_pe *pe, *slave; 565 s64 rc; 566 567 /* Find master PE */ 568 pe = &phb->ioda.pe_array[pe_no]; 569 if (pe->flags & PNV_IODA_PE_SLAVE) { 570 pe = pe->master; 571 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)); 572 pe_no = pe->pe_number; 573 } 574 575 /* Clear frozen state for master PE */ 576 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt); 577 if (rc != OPAL_SUCCESS) { 578 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n", 579 __func__, rc, opt, phb->hose->global_number, pe_no); 580 return -EIO; 581 } 582 583 if (!(pe->flags & PNV_IODA_PE_MASTER)) 584 return 0; 585 586 /* Clear frozen state for slave PEs */ 587 list_for_each_entry(slave, &pe->slaves, list) { 588 rc = opal_pci_eeh_freeze_clear(phb->opal_id, 589 slave->pe_number, 590 opt); 591 if (rc != OPAL_SUCCESS) { 592 pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n", 593 __func__, rc, opt, phb->hose->global_number, 594 slave->pe_number); 595 return -EIO; 596 } 597 } 598 599 return 0; 600 } 601 602 static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no) 603 { 604 struct pnv_ioda_pe *slave, *pe; 605 u8 fstate = 0, state; 606 __be16 pcierr = 0; 607 s64 rc; 608 609 /* Sanity check on PE number */ 610 if (pe_no < 0 || pe_no >= phb->ioda.total_pe_num) 611 return OPAL_EEH_STOPPED_PERM_UNAVAIL; 612 613 /* 614 * Fetch the master PE and the PE instance might be 615 * not initialized yet. 616 */ 617 pe = &phb->ioda.pe_array[pe_no]; 618 if (pe->flags & PNV_IODA_PE_SLAVE) { 619 pe = pe->master; 620 WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)); 621 pe_no = pe->pe_number; 622 } 623 624 /* Check the master PE */ 625 rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no, 626 &state, &pcierr, NULL); 627 if (rc != OPAL_SUCCESS) { 628 pr_warn("%s: Failure %lld getting " 629 "PHB#%x-PE#%x state\n", 630 __func__, rc, 631 phb->hose->global_number, pe_no); 632 return OPAL_EEH_STOPPED_TEMP_UNAVAIL; 633 } 634 635 /* Check the slave PE */ 636 if (!(pe->flags & PNV_IODA_PE_MASTER)) 637 return state; 638 639 list_for_each_entry(slave, &pe->slaves, list) { 640 rc = opal_pci_eeh_freeze_status(phb->opal_id, 641 slave->pe_number, 642 &fstate, 643 &pcierr, 644 NULL); 645 if (rc != OPAL_SUCCESS) { 646 pr_warn("%s: Failure %lld getting " 647 "PHB#%x-PE#%x state\n", 648 __func__, rc, 649 phb->hose->global_number, slave->pe_number); 650 return OPAL_EEH_STOPPED_TEMP_UNAVAIL; 651 } 652 653 /* 654 * Override the result based on the ascending 655 * priority. 656 */ 657 if (fstate > state) 658 state = fstate; 659 } 660 661 return state; 662 } 663 664 struct pnv_ioda_pe *pnv_pci_bdfn_to_pe(struct pnv_phb *phb, u16 bdfn) 665 { 666 int pe_number = phb->ioda.pe_rmap[bdfn]; 667 668 if (pe_number == IODA_INVALID_PE) 669 return NULL; 670 671 return &phb->ioda.pe_array[pe_number]; 672 } 673 674 struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev) 675 { 676 struct pci_controller *hose = pci_bus_to_host(dev->bus); 677 struct pnv_phb *phb = hose->private_data; 678 struct pci_dn *pdn = pci_get_pdn(dev); 679 680 if (!pdn) 681 return NULL; 682 if (pdn->pe_number == IODA_INVALID_PE) 683 return NULL; 684 return &phb->ioda.pe_array[pdn->pe_number]; 685 } 686 687 static int pnv_ioda_set_one_peltv(struct pnv_phb *phb, 688 struct pnv_ioda_pe *parent, 689 struct pnv_ioda_pe *child, 690 bool is_add) 691 { 692 const char *desc = is_add ? "adding" : "removing"; 693 uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN : 694 OPAL_REMOVE_PE_FROM_DOMAIN; 695 struct pnv_ioda_pe *slave; 696 long rc; 697 698 /* Parent PE affects child PE */ 699 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number, 700 child->pe_number, op); 701 if (rc != OPAL_SUCCESS) { 702 pe_warn(child, "OPAL error %ld %s to parent PELTV\n", 703 rc, desc); 704 return -ENXIO; 705 } 706 707 if (!(child->flags & PNV_IODA_PE_MASTER)) 708 return 0; 709 710 /* Compound case: parent PE affects slave PEs */ 711 list_for_each_entry(slave, &child->slaves, list) { 712 rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number, 713 slave->pe_number, op); 714 if (rc != OPAL_SUCCESS) { 715 pe_warn(slave, "OPAL error %ld %s to parent PELTV\n", 716 rc, desc); 717 return -ENXIO; 718 } 719 } 720 721 return 0; 722 } 723 724 static int pnv_ioda_set_peltv(struct pnv_phb *phb, 725 struct pnv_ioda_pe *pe, 726 bool is_add) 727 { 728 struct pnv_ioda_pe *slave; 729 struct pci_dev *pdev = NULL; 730 int ret; 731 732 /* 733 * Clear PE frozen state. If it's master PE, we need 734 * clear slave PE frozen state as well. 735 */ 736 if (is_add) { 737 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number, 738 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL); 739 if (pe->flags & PNV_IODA_PE_MASTER) { 740 list_for_each_entry(slave, &pe->slaves, list) 741 opal_pci_eeh_freeze_clear(phb->opal_id, 742 slave->pe_number, 743 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL); 744 } 745 } 746 747 /* 748 * Associate PE in PELT. We need add the PE into the 749 * corresponding PELT-V as well. Otherwise, the error 750 * originated from the PE might contribute to other 751 * PEs. 752 */ 753 ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add); 754 if (ret) 755 return ret; 756 757 /* For compound PEs, any one affects all of them */ 758 if (pe->flags & PNV_IODA_PE_MASTER) { 759 list_for_each_entry(slave, &pe->slaves, list) { 760 ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add); 761 if (ret) 762 return ret; 763 } 764 } 765 766 if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS)) 767 pdev = pe->pbus->self; 768 else if (pe->flags & PNV_IODA_PE_DEV) 769 pdev = pe->pdev->bus->self; 770 #ifdef CONFIG_PCI_IOV 771 else if (pe->flags & PNV_IODA_PE_VF) 772 pdev = pe->parent_dev; 773 #endif /* CONFIG_PCI_IOV */ 774 while (pdev) { 775 struct pci_dn *pdn = pci_get_pdn(pdev); 776 struct pnv_ioda_pe *parent; 777 778 if (pdn && pdn->pe_number != IODA_INVALID_PE) { 779 parent = &phb->ioda.pe_array[pdn->pe_number]; 780 ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add); 781 if (ret) 782 return ret; 783 } 784 785 pdev = pdev->bus->self; 786 } 787 788 return 0; 789 } 790 791 static void pnv_ioda_unset_peltv(struct pnv_phb *phb, 792 struct pnv_ioda_pe *pe, 793 struct pci_dev *parent) 794 { 795 int64_t rc; 796 797 while (parent) { 798 struct pci_dn *pdn = pci_get_pdn(parent); 799 800 if (pdn && pdn->pe_number != IODA_INVALID_PE) { 801 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number, 802 pe->pe_number, 803 OPAL_REMOVE_PE_FROM_DOMAIN); 804 /* XXX What to do in case of error ? */ 805 } 806 parent = parent->bus->self; 807 } 808 809 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number, 810 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL); 811 812 /* Disassociate PE in PELT */ 813 rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number, 814 pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN); 815 if (rc) 816 pe_warn(pe, "OPAL error %lld remove self from PELTV\n", rc); 817 } 818 819 static int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe) 820 { 821 struct pci_dev *parent; 822 uint8_t bcomp, dcomp, fcomp; 823 int64_t rc; 824 long rid_end, rid; 825 826 /* Currently, we just deconfigure VF PE. Bus PE will always there.*/ 827 if (pe->pbus) { 828 int count; 829 830 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER; 831 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER; 832 parent = pe->pbus->self; 833 if (pe->flags & PNV_IODA_PE_BUS_ALL) 834 count = resource_size(&pe->pbus->busn_res); 835 else 836 count = 1; 837 838 switch(count) { 839 case 1: bcomp = OpalPciBusAll; break; 840 case 2: bcomp = OpalPciBus7Bits; break; 841 case 4: bcomp = OpalPciBus6Bits; break; 842 case 8: bcomp = OpalPciBus5Bits; break; 843 case 16: bcomp = OpalPciBus4Bits; break; 844 case 32: bcomp = OpalPciBus3Bits; break; 845 default: 846 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n", 847 count); 848 /* Do an exact match only */ 849 bcomp = OpalPciBusAll; 850 } 851 rid_end = pe->rid + (count << 8); 852 } else { 853 #ifdef CONFIG_PCI_IOV 854 if (pe->flags & PNV_IODA_PE_VF) 855 parent = pe->parent_dev; 856 else 857 #endif 858 parent = pe->pdev->bus->self; 859 bcomp = OpalPciBusAll; 860 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER; 861 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER; 862 rid_end = pe->rid + 1; 863 } 864 865 /* Clear the reverse map */ 866 for (rid = pe->rid; rid < rid_end; rid++) 867 phb->ioda.pe_rmap[rid] = IODA_INVALID_PE; 868 869 /* 870 * Release from all parents PELT-V. NPUs don't have a PELTV 871 * table 872 */ 873 if (phb->type != PNV_PHB_NPU_NVLINK && phb->type != PNV_PHB_NPU_OCAPI) 874 pnv_ioda_unset_peltv(phb, pe, parent); 875 876 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid, 877 bcomp, dcomp, fcomp, OPAL_UNMAP_PE); 878 if (rc) 879 pe_err(pe, "OPAL error %lld trying to setup PELT table\n", rc); 880 881 pe->pbus = NULL; 882 pe->pdev = NULL; 883 #ifdef CONFIG_PCI_IOV 884 pe->parent_dev = NULL; 885 #endif 886 887 return 0; 888 } 889 890 static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe) 891 { 892 struct pci_dev *parent; 893 uint8_t bcomp, dcomp, fcomp; 894 long rc, rid_end, rid; 895 896 /* Bus validation ? */ 897 if (pe->pbus) { 898 int count; 899 900 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER; 901 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER; 902 parent = pe->pbus->self; 903 if (pe->flags & PNV_IODA_PE_BUS_ALL) 904 count = resource_size(&pe->pbus->busn_res); 905 else 906 count = 1; 907 908 switch(count) { 909 case 1: bcomp = OpalPciBusAll; break; 910 case 2: bcomp = OpalPciBus7Bits; break; 911 case 4: bcomp = OpalPciBus6Bits; break; 912 case 8: bcomp = OpalPciBus5Bits; break; 913 case 16: bcomp = OpalPciBus4Bits; break; 914 case 32: bcomp = OpalPciBus3Bits; break; 915 default: 916 dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n", 917 count); 918 /* Do an exact match only */ 919 bcomp = OpalPciBusAll; 920 } 921 rid_end = pe->rid + (count << 8); 922 } else { 923 #ifdef CONFIG_PCI_IOV 924 if (pe->flags & PNV_IODA_PE_VF) 925 parent = pe->parent_dev; 926 else 927 #endif /* CONFIG_PCI_IOV */ 928 parent = pe->pdev->bus->self; 929 bcomp = OpalPciBusAll; 930 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER; 931 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER; 932 rid_end = pe->rid + 1; 933 } 934 935 /* 936 * Associate PE in PELT. We need add the PE into the 937 * corresponding PELT-V as well. Otherwise, the error 938 * originated from the PE might contribute to other 939 * PEs. 940 */ 941 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid, 942 bcomp, dcomp, fcomp, OPAL_MAP_PE); 943 if (rc) { 944 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc); 945 return -ENXIO; 946 } 947 948 /* 949 * Configure PELTV. NPUs don't have a PELTV table so skip 950 * configuration on them. 951 */ 952 if (phb->type != PNV_PHB_NPU_NVLINK && phb->type != PNV_PHB_NPU_OCAPI) 953 pnv_ioda_set_peltv(phb, pe, true); 954 955 /* Setup reverse map */ 956 for (rid = pe->rid; rid < rid_end; rid++) 957 phb->ioda.pe_rmap[rid] = pe->pe_number; 958 959 /* Setup one MVTs on IODA1 */ 960 if (phb->type != PNV_PHB_IODA1) { 961 pe->mve_number = 0; 962 goto out; 963 } 964 965 pe->mve_number = pe->pe_number; 966 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number); 967 if (rc != OPAL_SUCCESS) { 968 pe_err(pe, "OPAL error %ld setting up MVE %x\n", 969 rc, pe->mve_number); 970 pe->mve_number = -1; 971 } else { 972 rc = opal_pci_set_mve_enable(phb->opal_id, 973 pe->mve_number, OPAL_ENABLE_MVE); 974 if (rc) { 975 pe_err(pe, "OPAL error %ld enabling MVE %x\n", 976 rc, pe->mve_number); 977 pe->mve_number = -1; 978 } 979 } 980 981 out: 982 return 0; 983 } 984 985 #ifdef CONFIG_PCI_IOV 986 static int pnv_pci_vf_resource_shift(struct pci_dev *dev, int offset) 987 { 988 struct pci_dn *pdn = pci_get_pdn(dev); 989 int i; 990 struct resource *res, res2; 991 resource_size_t size; 992 u16 num_vfs; 993 994 if (!dev->is_physfn) 995 return -EINVAL; 996 997 /* 998 * "offset" is in VFs. The M64 windows are sized so that when they 999 * are segmented, each segment is the same size as the IOV BAR. 1000 * Each segment is in a separate PE, and the high order bits of the 1001 * address are the PE number. Therefore, each VF's BAR is in a 1002 * separate PE, and changing the IOV BAR start address changes the 1003 * range of PEs the VFs are in. 1004 */ 1005 num_vfs = pdn->num_vfs; 1006 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { 1007 res = &dev->resource[i + PCI_IOV_RESOURCES]; 1008 if (!res->flags || !res->parent) 1009 continue; 1010 1011 /* 1012 * The actual IOV BAR range is determined by the start address 1013 * and the actual size for num_vfs VFs BAR. This check is to 1014 * make sure that after shifting, the range will not overlap 1015 * with another device. 1016 */ 1017 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES); 1018 res2.flags = res->flags; 1019 res2.start = res->start + (size * offset); 1020 res2.end = res2.start + (size * num_vfs) - 1; 1021 1022 if (res2.end > res->end) { 1023 dev_err(&dev->dev, "VF BAR%d: %pR would extend past %pR (trying to enable %d VFs shifted by %d)\n", 1024 i, &res2, res, num_vfs, offset); 1025 return -EBUSY; 1026 } 1027 } 1028 1029 /* 1030 * Since M64 BAR shares segments among all possible 256 PEs, 1031 * we have to shift the beginning of PF IOV BAR to make it start from 1032 * the segment which belongs to the PE number assigned to the first VF. 1033 * This creates a "hole" in the /proc/iomem which could be used for 1034 * allocating other resources so we reserve this area below and 1035 * release when IOV is released. 1036 */ 1037 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { 1038 res = &dev->resource[i + PCI_IOV_RESOURCES]; 1039 if (!res->flags || !res->parent) 1040 continue; 1041 1042 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES); 1043 res2 = *res; 1044 res->start += size * offset; 1045 1046 dev_info(&dev->dev, "VF BAR%d: %pR shifted to %pR (%sabling %d VFs shifted by %d)\n", 1047 i, &res2, res, (offset > 0) ? "En" : "Dis", 1048 num_vfs, offset); 1049 1050 if (offset < 0) { 1051 devm_release_resource(&dev->dev, &pdn->holes[i]); 1052 memset(&pdn->holes[i], 0, sizeof(pdn->holes[i])); 1053 } 1054 1055 pci_update_resource(dev, i + PCI_IOV_RESOURCES); 1056 1057 if (offset > 0) { 1058 pdn->holes[i].start = res2.start; 1059 pdn->holes[i].end = res2.start + size * offset - 1; 1060 pdn->holes[i].flags = IORESOURCE_BUS; 1061 pdn->holes[i].name = "pnv_iov_reserved"; 1062 devm_request_resource(&dev->dev, res->parent, 1063 &pdn->holes[i]); 1064 } 1065 } 1066 return 0; 1067 } 1068 #endif /* CONFIG_PCI_IOV */ 1069 1070 static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev) 1071 { 1072 struct pci_controller *hose = pci_bus_to_host(dev->bus); 1073 struct pnv_phb *phb = hose->private_data; 1074 struct pci_dn *pdn = pci_get_pdn(dev); 1075 struct pnv_ioda_pe *pe; 1076 1077 if (!pdn) { 1078 pr_err("%s: Device tree node not associated properly\n", 1079 pci_name(dev)); 1080 return NULL; 1081 } 1082 if (pdn->pe_number != IODA_INVALID_PE) 1083 return NULL; 1084 1085 pe = pnv_ioda_alloc_pe(phb); 1086 if (!pe) { 1087 pr_warn("%s: Not enough PE# available, disabling device\n", 1088 pci_name(dev)); 1089 return NULL; 1090 } 1091 1092 /* NOTE: We don't get a reference for the pointer in the PE 1093 * data structure, both the device and PE structures should be 1094 * destroyed at the same time. However, removing nvlink 1095 * devices will need some work. 1096 * 1097 * At some point we want to remove the PDN completely anyways 1098 */ 1099 pdn->pe_number = pe->pe_number; 1100 pe->flags = PNV_IODA_PE_DEV; 1101 pe->pdev = dev; 1102 pe->pbus = NULL; 1103 pe->mve_number = -1; 1104 pe->rid = dev->bus->number << 8 | pdn->devfn; 1105 pe->device_count++; 1106 1107 pe_info(pe, "Associated device to PE\n"); 1108 1109 if (pnv_ioda_configure_pe(phb, pe)) { 1110 /* XXX What do we do here ? */ 1111 pnv_ioda_free_pe(pe); 1112 pdn->pe_number = IODA_INVALID_PE; 1113 pe->pdev = NULL; 1114 return NULL; 1115 } 1116 1117 /* Put PE to the list */ 1118 mutex_lock(&phb->ioda.pe_list_mutex); 1119 list_add_tail(&pe->list, &phb->ioda.pe_list); 1120 mutex_unlock(&phb->ioda.pe_list_mutex); 1121 return pe; 1122 } 1123 1124 /* 1125 * There're 2 types of PCI bus sensitive PEs: One that is compromised of 1126 * single PCI bus. Another one that contains the primary PCI bus and its 1127 * subordinate PCI devices and buses. The second type of PE is normally 1128 * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports. 1129 */ 1130 static struct pnv_ioda_pe *pnv_ioda_setup_bus_PE(struct pci_bus *bus, bool all) 1131 { 1132 struct pci_controller *hose = pci_bus_to_host(bus); 1133 struct pnv_phb *phb = hose->private_data; 1134 struct pnv_ioda_pe *pe = NULL; 1135 unsigned int pe_num; 1136 1137 /* 1138 * In partial hotplug case, the PE instance might be still alive. 1139 * We should reuse it instead of allocating a new one. 1140 */ 1141 pe_num = phb->ioda.pe_rmap[bus->number << 8]; 1142 if (WARN_ON(pe_num != IODA_INVALID_PE)) { 1143 pe = &phb->ioda.pe_array[pe_num]; 1144 return NULL; 1145 } 1146 1147 /* PE number for root bus should have been reserved */ 1148 if (pci_is_root_bus(bus)) 1149 pe = &phb->ioda.pe_array[phb->ioda.root_pe_idx]; 1150 1151 /* Check if PE is determined by M64 */ 1152 if (!pe) 1153 pe = pnv_ioda_pick_m64_pe(bus, all); 1154 1155 /* The PE number isn't pinned by M64 */ 1156 if (!pe) 1157 pe = pnv_ioda_alloc_pe(phb); 1158 1159 if (!pe) { 1160 pr_warn("%s: Not enough PE# available for PCI bus %04x:%02x\n", 1161 __func__, pci_domain_nr(bus), bus->number); 1162 return NULL; 1163 } 1164 1165 pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS); 1166 pe->pbus = bus; 1167 pe->pdev = NULL; 1168 pe->mve_number = -1; 1169 pe->rid = bus->busn_res.start << 8; 1170 1171 if (all) 1172 pe_info(pe, "Secondary bus %pad..%pad associated with PE#%x\n", 1173 &bus->busn_res.start, &bus->busn_res.end, 1174 pe->pe_number); 1175 else 1176 pe_info(pe, "Secondary bus %pad associated with PE#%x\n", 1177 &bus->busn_res.start, pe->pe_number); 1178 1179 if (pnv_ioda_configure_pe(phb, pe)) { 1180 /* XXX What do we do here ? */ 1181 pnv_ioda_free_pe(pe); 1182 pe->pbus = NULL; 1183 return NULL; 1184 } 1185 1186 /* Put PE to the list */ 1187 list_add_tail(&pe->list, &phb->ioda.pe_list); 1188 1189 return pe; 1190 } 1191 1192 static struct pnv_ioda_pe *pnv_ioda_setup_npu_PE(struct pci_dev *npu_pdev) 1193 { 1194 int pe_num, found_pe = false, rc; 1195 long rid; 1196 struct pnv_ioda_pe *pe; 1197 struct pci_dev *gpu_pdev; 1198 struct pci_dn *npu_pdn; 1199 struct pci_controller *hose = pci_bus_to_host(npu_pdev->bus); 1200 struct pnv_phb *phb = hose->private_data; 1201 1202 /* 1203 * Intentionally leak a reference on the npu device (for 1204 * nvlink only; this is not an opencapi path) to make sure it 1205 * never goes away, as it's been the case all along and some 1206 * work is needed otherwise. 1207 */ 1208 pci_dev_get(npu_pdev); 1209 1210 /* 1211 * Due to a hardware errata PE#0 on the NPU is reserved for 1212 * error handling. This means we only have three PEs remaining 1213 * which need to be assigned to four links, implying some 1214 * links must share PEs. 1215 * 1216 * To achieve this we assign PEs such that NPUs linking the 1217 * same GPU get assigned the same PE. 1218 */ 1219 gpu_pdev = pnv_pci_get_gpu_dev(npu_pdev); 1220 for (pe_num = 0; pe_num < phb->ioda.total_pe_num; pe_num++) { 1221 pe = &phb->ioda.pe_array[pe_num]; 1222 if (!pe->pdev) 1223 continue; 1224 1225 if (pnv_pci_get_gpu_dev(pe->pdev) == gpu_pdev) { 1226 /* 1227 * This device has the same peer GPU so should 1228 * be assigned the same PE as the existing 1229 * peer NPU. 1230 */ 1231 dev_info(&npu_pdev->dev, 1232 "Associating to existing PE %x\n", pe_num); 1233 npu_pdn = pci_get_pdn(npu_pdev); 1234 rid = npu_pdev->bus->number << 8 | npu_pdn->devfn; 1235 npu_pdn->pe_number = pe_num; 1236 phb->ioda.pe_rmap[rid] = pe->pe_number; 1237 pe->device_count++; 1238 1239 /* Map the PE to this link */ 1240 rc = opal_pci_set_pe(phb->opal_id, pe_num, rid, 1241 OpalPciBusAll, 1242 OPAL_COMPARE_RID_DEVICE_NUMBER, 1243 OPAL_COMPARE_RID_FUNCTION_NUMBER, 1244 OPAL_MAP_PE); 1245 WARN_ON(rc != OPAL_SUCCESS); 1246 found_pe = true; 1247 break; 1248 } 1249 } 1250 1251 if (!found_pe) 1252 /* 1253 * Could not find an existing PE so allocate a new 1254 * one. 1255 */ 1256 return pnv_ioda_setup_dev_PE(npu_pdev); 1257 else 1258 return pe; 1259 } 1260 1261 static void pnv_ioda_setup_npu_PEs(struct pci_bus *bus) 1262 { 1263 struct pci_dev *pdev; 1264 1265 list_for_each_entry(pdev, &bus->devices, bus_list) 1266 pnv_ioda_setup_npu_PE(pdev); 1267 } 1268 1269 static void pnv_pci_ioda_setup_nvlink(void) 1270 { 1271 struct pci_controller *hose; 1272 struct pnv_phb *phb; 1273 struct pnv_ioda_pe *pe; 1274 1275 list_for_each_entry(hose, &hose_list, list_node) { 1276 phb = hose->private_data; 1277 if (phb->type == PNV_PHB_NPU_NVLINK) { 1278 /* PE#0 is needed for error reporting */ 1279 pnv_ioda_reserve_pe(phb, 0); 1280 pnv_ioda_setup_npu_PEs(hose->bus); 1281 if (phb->model == PNV_PHB_MODEL_NPU2) 1282 WARN_ON_ONCE(pnv_npu2_init(hose)); 1283 } 1284 } 1285 list_for_each_entry(hose, &hose_list, list_node) { 1286 phb = hose->private_data; 1287 if (phb->type != PNV_PHB_IODA2) 1288 continue; 1289 1290 list_for_each_entry(pe, &phb->ioda.pe_list, list) 1291 pnv_npu2_map_lpar(pe, MSR_DR | MSR_PR | MSR_HV); 1292 } 1293 1294 #ifdef CONFIG_IOMMU_API 1295 /* setup iommu groups so we can do nvlink pass-thru */ 1296 pnv_pci_npu_setup_iommu_groups(); 1297 #endif 1298 } 1299 1300 #ifdef CONFIG_PCI_IOV 1301 static int pnv_pci_vf_release_m64(struct pci_dev *pdev, u16 num_vfs) 1302 { 1303 struct pci_bus *bus; 1304 struct pci_controller *hose; 1305 struct pnv_phb *phb; 1306 struct pci_dn *pdn; 1307 int i, j; 1308 int m64_bars; 1309 1310 bus = pdev->bus; 1311 hose = pci_bus_to_host(bus); 1312 phb = hose->private_data; 1313 pdn = pci_get_pdn(pdev); 1314 1315 if (pdn->m64_single_mode) 1316 m64_bars = num_vfs; 1317 else 1318 m64_bars = 1; 1319 1320 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) 1321 for (j = 0; j < m64_bars; j++) { 1322 if (pdn->m64_map[j][i] == IODA_INVALID_M64) 1323 continue; 1324 opal_pci_phb_mmio_enable(phb->opal_id, 1325 OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 0); 1326 clear_bit(pdn->m64_map[j][i], &phb->ioda.m64_bar_alloc); 1327 pdn->m64_map[j][i] = IODA_INVALID_M64; 1328 } 1329 1330 kfree(pdn->m64_map); 1331 return 0; 1332 } 1333 1334 static int pnv_pci_vf_assign_m64(struct pci_dev *pdev, u16 num_vfs) 1335 { 1336 struct pci_bus *bus; 1337 struct pci_controller *hose; 1338 struct pnv_phb *phb; 1339 struct pci_dn *pdn; 1340 unsigned int win; 1341 struct resource *res; 1342 int i, j; 1343 int64_t rc; 1344 int total_vfs; 1345 resource_size_t size, start; 1346 int pe_num; 1347 int m64_bars; 1348 1349 bus = pdev->bus; 1350 hose = pci_bus_to_host(bus); 1351 phb = hose->private_data; 1352 pdn = pci_get_pdn(pdev); 1353 total_vfs = pci_sriov_get_totalvfs(pdev); 1354 1355 if (pdn->m64_single_mode) 1356 m64_bars = num_vfs; 1357 else 1358 m64_bars = 1; 1359 1360 pdn->m64_map = kmalloc_array(m64_bars, 1361 sizeof(*pdn->m64_map), 1362 GFP_KERNEL); 1363 if (!pdn->m64_map) 1364 return -ENOMEM; 1365 /* Initialize the m64_map to IODA_INVALID_M64 */ 1366 for (i = 0; i < m64_bars ; i++) 1367 for (j = 0; j < PCI_SRIOV_NUM_BARS; j++) 1368 pdn->m64_map[i][j] = IODA_INVALID_M64; 1369 1370 1371 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { 1372 res = &pdev->resource[i + PCI_IOV_RESOURCES]; 1373 if (!res->flags || !res->parent) 1374 continue; 1375 1376 for (j = 0; j < m64_bars; j++) { 1377 do { 1378 win = find_next_zero_bit(&phb->ioda.m64_bar_alloc, 1379 phb->ioda.m64_bar_idx + 1, 0); 1380 1381 if (win >= phb->ioda.m64_bar_idx + 1) 1382 goto m64_failed; 1383 } while (test_and_set_bit(win, &phb->ioda.m64_bar_alloc)); 1384 1385 pdn->m64_map[j][i] = win; 1386 1387 if (pdn->m64_single_mode) { 1388 size = pci_iov_resource_size(pdev, 1389 PCI_IOV_RESOURCES + i); 1390 start = res->start + size * j; 1391 } else { 1392 size = resource_size(res); 1393 start = res->start; 1394 } 1395 1396 /* Map the M64 here */ 1397 if (pdn->m64_single_mode) { 1398 pe_num = pdn->pe_num_map[j]; 1399 rc = opal_pci_map_pe_mmio_window(phb->opal_id, 1400 pe_num, OPAL_M64_WINDOW_TYPE, 1401 pdn->m64_map[j][i], 0); 1402 } 1403 1404 rc = opal_pci_set_phb_mem_window(phb->opal_id, 1405 OPAL_M64_WINDOW_TYPE, 1406 pdn->m64_map[j][i], 1407 start, 1408 0, /* unused */ 1409 size); 1410 1411 1412 if (rc != OPAL_SUCCESS) { 1413 dev_err(&pdev->dev, "Failed to map M64 window #%d: %lld\n", 1414 win, rc); 1415 goto m64_failed; 1416 } 1417 1418 if (pdn->m64_single_mode) 1419 rc = opal_pci_phb_mmio_enable(phb->opal_id, 1420 OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 2); 1421 else 1422 rc = opal_pci_phb_mmio_enable(phb->opal_id, 1423 OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 1); 1424 1425 if (rc != OPAL_SUCCESS) { 1426 dev_err(&pdev->dev, "Failed to enable M64 window #%d: %llx\n", 1427 win, rc); 1428 goto m64_failed; 1429 } 1430 } 1431 } 1432 return 0; 1433 1434 m64_failed: 1435 pnv_pci_vf_release_m64(pdev, num_vfs); 1436 return -EBUSY; 1437 } 1438 1439 static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group, 1440 int num); 1441 1442 static void pnv_pci_ioda2_release_dma_pe(struct pci_dev *dev, struct pnv_ioda_pe *pe) 1443 { 1444 struct iommu_table *tbl; 1445 int64_t rc; 1446 1447 tbl = pe->table_group.tables[0]; 1448 rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0); 1449 if (rc) 1450 pe_warn(pe, "OPAL error %lld release DMA window\n", rc); 1451 1452 pnv_pci_ioda2_set_bypass(pe, false); 1453 if (pe->table_group.group) { 1454 iommu_group_put(pe->table_group.group); 1455 BUG_ON(pe->table_group.group); 1456 } 1457 iommu_tce_table_put(tbl); 1458 } 1459 1460 static void pnv_ioda_release_vf_PE(struct pci_dev *pdev) 1461 { 1462 struct pci_bus *bus; 1463 struct pci_controller *hose; 1464 struct pnv_phb *phb; 1465 struct pnv_ioda_pe *pe, *pe_n; 1466 struct pci_dn *pdn; 1467 1468 bus = pdev->bus; 1469 hose = pci_bus_to_host(bus); 1470 phb = hose->private_data; 1471 pdn = pci_get_pdn(pdev); 1472 1473 if (!pdev->is_physfn) 1474 return; 1475 1476 list_for_each_entry_safe(pe, pe_n, &phb->ioda.pe_list, list) { 1477 if (pe->parent_dev != pdev) 1478 continue; 1479 1480 pnv_pci_ioda2_release_dma_pe(pdev, pe); 1481 1482 /* Remove from list */ 1483 mutex_lock(&phb->ioda.pe_list_mutex); 1484 list_del(&pe->list); 1485 mutex_unlock(&phb->ioda.pe_list_mutex); 1486 1487 pnv_ioda_deconfigure_pe(phb, pe); 1488 1489 pnv_ioda_free_pe(pe); 1490 } 1491 } 1492 1493 void pnv_pci_sriov_disable(struct pci_dev *pdev) 1494 { 1495 struct pci_bus *bus; 1496 struct pci_controller *hose; 1497 struct pnv_phb *phb; 1498 struct pnv_ioda_pe *pe; 1499 struct pci_dn *pdn; 1500 u16 num_vfs, i; 1501 1502 bus = pdev->bus; 1503 hose = pci_bus_to_host(bus); 1504 phb = hose->private_data; 1505 pdn = pci_get_pdn(pdev); 1506 num_vfs = pdn->num_vfs; 1507 1508 /* Release VF PEs */ 1509 pnv_ioda_release_vf_PE(pdev); 1510 1511 if (phb->type == PNV_PHB_IODA2) { 1512 if (!pdn->m64_single_mode) 1513 pnv_pci_vf_resource_shift(pdev, -*pdn->pe_num_map); 1514 1515 /* Release M64 windows */ 1516 pnv_pci_vf_release_m64(pdev, num_vfs); 1517 1518 /* Release PE numbers */ 1519 if (pdn->m64_single_mode) { 1520 for (i = 0; i < num_vfs; i++) { 1521 if (pdn->pe_num_map[i] == IODA_INVALID_PE) 1522 continue; 1523 1524 pe = &phb->ioda.pe_array[pdn->pe_num_map[i]]; 1525 pnv_ioda_free_pe(pe); 1526 } 1527 } else 1528 bitmap_clear(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs); 1529 /* Releasing pe_num_map */ 1530 kfree(pdn->pe_num_map); 1531 } 1532 } 1533 1534 static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb, 1535 struct pnv_ioda_pe *pe); 1536 static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs) 1537 { 1538 struct pci_bus *bus; 1539 struct pci_controller *hose; 1540 struct pnv_phb *phb; 1541 struct pnv_ioda_pe *pe; 1542 int pe_num; 1543 u16 vf_index; 1544 struct pci_dn *pdn; 1545 1546 bus = pdev->bus; 1547 hose = pci_bus_to_host(bus); 1548 phb = hose->private_data; 1549 pdn = pci_get_pdn(pdev); 1550 1551 if (!pdev->is_physfn) 1552 return; 1553 1554 /* Reserve PE for each VF */ 1555 for (vf_index = 0; vf_index < num_vfs; vf_index++) { 1556 int vf_devfn = pci_iov_virtfn_devfn(pdev, vf_index); 1557 int vf_bus = pci_iov_virtfn_bus(pdev, vf_index); 1558 struct pci_dn *vf_pdn; 1559 1560 if (pdn->m64_single_mode) 1561 pe_num = pdn->pe_num_map[vf_index]; 1562 else 1563 pe_num = *pdn->pe_num_map + vf_index; 1564 1565 pe = &phb->ioda.pe_array[pe_num]; 1566 pe->pe_number = pe_num; 1567 pe->phb = phb; 1568 pe->flags = PNV_IODA_PE_VF; 1569 pe->pbus = NULL; 1570 pe->parent_dev = pdev; 1571 pe->mve_number = -1; 1572 pe->rid = (vf_bus << 8) | vf_devfn; 1573 1574 pe_info(pe, "VF %04d:%02d:%02d.%d associated with PE#%x\n", 1575 hose->global_number, pdev->bus->number, 1576 PCI_SLOT(vf_devfn), PCI_FUNC(vf_devfn), pe_num); 1577 1578 if (pnv_ioda_configure_pe(phb, pe)) { 1579 /* XXX What do we do here ? */ 1580 pnv_ioda_free_pe(pe); 1581 pe->pdev = NULL; 1582 continue; 1583 } 1584 1585 /* Put PE to the list */ 1586 mutex_lock(&phb->ioda.pe_list_mutex); 1587 list_add_tail(&pe->list, &phb->ioda.pe_list); 1588 mutex_unlock(&phb->ioda.pe_list_mutex); 1589 1590 /* associate this pe to it's pdn */ 1591 list_for_each_entry(vf_pdn, &pdn->parent->child_list, list) { 1592 if (vf_pdn->busno == vf_bus && 1593 vf_pdn->devfn == vf_devfn) { 1594 vf_pdn->pe_number = pe_num; 1595 break; 1596 } 1597 } 1598 1599 pnv_pci_ioda2_setup_dma_pe(phb, pe); 1600 } 1601 } 1602 1603 int pnv_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs) 1604 { 1605 struct pci_bus *bus; 1606 struct pci_controller *hose; 1607 struct pnv_phb *phb; 1608 struct pnv_ioda_pe *pe; 1609 struct pci_dn *pdn; 1610 int ret; 1611 u16 i; 1612 1613 bus = pdev->bus; 1614 hose = pci_bus_to_host(bus); 1615 phb = hose->private_data; 1616 pdn = pci_get_pdn(pdev); 1617 1618 if (phb->type == PNV_PHB_IODA2) { 1619 if (!pdn->vfs_expanded) { 1620 dev_info(&pdev->dev, "don't support this SRIOV device" 1621 " with non 64bit-prefetchable IOV BAR\n"); 1622 return -ENOSPC; 1623 } 1624 1625 /* 1626 * When M64 BARs functions in Single PE mode, the number of VFs 1627 * could be enabled must be less than the number of M64 BARs. 1628 */ 1629 if (pdn->m64_single_mode && num_vfs > phb->ioda.m64_bar_idx) { 1630 dev_info(&pdev->dev, "Not enough M64 BAR for VFs\n"); 1631 return -EBUSY; 1632 } 1633 1634 /* Allocating pe_num_map */ 1635 if (pdn->m64_single_mode) 1636 pdn->pe_num_map = kmalloc_array(num_vfs, 1637 sizeof(*pdn->pe_num_map), 1638 GFP_KERNEL); 1639 else 1640 pdn->pe_num_map = kmalloc(sizeof(*pdn->pe_num_map), GFP_KERNEL); 1641 1642 if (!pdn->pe_num_map) 1643 return -ENOMEM; 1644 1645 if (pdn->m64_single_mode) 1646 for (i = 0; i < num_vfs; i++) 1647 pdn->pe_num_map[i] = IODA_INVALID_PE; 1648 1649 /* Calculate available PE for required VFs */ 1650 if (pdn->m64_single_mode) { 1651 for (i = 0; i < num_vfs; i++) { 1652 pe = pnv_ioda_alloc_pe(phb); 1653 if (!pe) { 1654 ret = -EBUSY; 1655 goto m64_failed; 1656 } 1657 1658 pdn->pe_num_map[i] = pe->pe_number; 1659 } 1660 } else { 1661 mutex_lock(&phb->ioda.pe_alloc_mutex); 1662 *pdn->pe_num_map = bitmap_find_next_zero_area( 1663 phb->ioda.pe_alloc, phb->ioda.total_pe_num, 1664 0, num_vfs, 0); 1665 if (*pdn->pe_num_map >= phb->ioda.total_pe_num) { 1666 mutex_unlock(&phb->ioda.pe_alloc_mutex); 1667 dev_info(&pdev->dev, "Failed to enable VF%d\n", num_vfs); 1668 kfree(pdn->pe_num_map); 1669 return -EBUSY; 1670 } 1671 bitmap_set(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs); 1672 mutex_unlock(&phb->ioda.pe_alloc_mutex); 1673 } 1674 pdn->num_vfs = num_vfs; 1675 1676 /* Assign M64 window accordingly */ 1677 ret = pnv_pci_vf_assign_m64(pdev, num_vfs); 1678 if (ret) { 1679 dev_info(&pdev->dev, "Not enough M64 window resources\n"); 1680 goto m64_failed; 1681 } 1682 1683 /* 1684 * When using one M64 BAR to map one IOV BAR, we need to shift 1685 * the IOV BAR according to the PE# allocated to the VFs. 1686 * Otherwise, the PE# for the VF will conflict with others. 1687 */ 1688 if (!pdn->m64_single_mode) { 1689 ret = pnv_pci_vf_resource_shift(pdev, *pdn->pe_num_map); 1690 if (ret) 1691 goto m64_failed; 1692 } 1693 } 1694 1695 /* Setup VF PEs */ 1696 pnv_ioda_setup_vf_PE(pdev, num_vfs); 1697 1698 return 0; 1699 1700 m64_failed: 1701 if (pdn->m64_single_mode) { 1702 for (i = 0; i < num_vfs; i++) { 1703 if (pdn->pe_num_map[i] == IODA_INVALID_PE) 1704 continue; 1705 1706 pe = &phb->ioda.pe_array[pdn->pe_num_map[i]]; 1707 pnv_ioda_free_pe(pe); 1708 } 1709 } else 1710 bitmap_clear(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs); 1711 1712 /* Releasing pe_num_map */ 1713 kfree(pdn->pe_num_map); 1714 1715 return ret; 1716 } 1717 1718 int pnv_pcibios_sriov_disable(struct pci_dev *pdev) 1719 { 1720 pnv_pci_sriov_disable(pdev); 1721 1722 /* Release PCI data */ 1723 remove_sriov_vf_pdns(pdev); 1724 return 0; 1725 } 1726 1727 int pnv_pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs) 1728 { 1729 /* Allocate PCI data */ 1730 add_sriov_vf_pdns(pdev); 1731 1732 return pnv_pci_sriov_enable(pdev, num_vfs); 1733 } 1734 #endif /* CONFIG_PCI_IOV */ 1735 1736 static void pnv_pci_ioda_dma_dev_setup(struct pci_dev *pdev) 1737 { 1738 struct pci_controller *hose = pci_bus_to_host(pdev->bus); 1739 struct pnv_phb *phb = hose->private_data; 1740 struct pci_dn *pdn = pci_get_pdn(pdev); 1741 struct pnv_ioda_pe *pe; 1742 1743 /* Check if the BDFN for this device is associated with a PE yet */ 1744 pe = pnv_pci_bdfn_to_pe(phb, pdev->devfn | (pdev->bus->number << 8)); 1745 if (!pe) { 1746 /* VF PEs should be pre-configured in pnv_pci_sriov_enable() */ 1747 if (WARN_ON(pdev->is_virtfn)) 1748 return; 1749 1750 pnv_pci_configure_bus(pdev->bus); 1751 pe = pnv_pci_bdfn_to_pe(phb, pdev->devfn | (pdev->bus->number << 8)); 1752 pci_info(pdev, "Configured PE#%x\n", pe ? pe->pe_number : 0xfffff); 1753 1754 1755 /* 1756 * If we can't setup the IODA PE something has gone horribly 1757 * wrong and we can't enable DMA for the device. 1758 */ 1759 if (WARN_ON(!pe)) 1760 return; 1761 } else { 1762 pci_info(pdev, "Added to existing PE#%x\n", pe->pe_number); 1763 } 1764 1765 if (pdn) 1766 pdn->pe_number = pe->pe_number; 1767 pe->device_count++; 1768 1769 WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops); 1770 pdev->dev.archdata.dma_offset = pe->tce_bypass_base; 1771 set_iommu_table_base(&pdev->dev, pe->table_group.tables[0]); 1772 1773 /* PEs with a DMA weight of zero won't have a group */ 1774 if (pe->table_group.group) 1775 iommu_add_device(&pe->table_group, &pdev->dev); 1776 } 1777 1778 /* 1779 * Reconfigure TVE#0 to be usable as 64-bit DMA space. 1780 * 1781 * The first 4GB of virtual memory for a PE is reserved for 32-bit accesses. 1782 * Devices can only access more than that if bit 59 of the PCI address is set 1783 * by hardware, which indicates TVE#1 should be used instead of TVE#0. 1784 * Many PCI devices are not capable of addressing that many bits, and as a 1785 * result are limited to the 4GB of virtual memory made available to 32-bit 1786 * devices in TVE#0. 1787 * 1788 * In order to work around this, reconfigure TVE#0 to be suitable for 64-bit 1789 * devices by configuring the virtual memory past the first 4GB inaccessible 1790 * by 64-bit DMAs. This should only be used by devices that want more than 1791 * 4GB, and only on PEs that have no 32-bit devices. 1792 * 1793 * Currently this will only work on PHB3 (POWER8). 1794 */ 1795 static int pnv_pci_ioda_dma_64bit_bypass(struct pnv_ioda_pe *pe) 1796 { 1797 u64 window_size, table_size, tce_count, addr; 1798 struct page *table_pages; 1799 u64 tce_order = 28; /* 256MB TCEs */ 1800 __be64 *tces; 1801 s64 rc; 1802 1803 /* 1804 * Window size needs to be a power of two, but needs to account for 1805 * shifting memory by the 4GB offset required to skip 32bit space. 1806 */ 1807 window_size = roundup_pow_of_two(memory_hotplug_max() + (1ULL << 32)); 1808 tce_count = window_size >> tce_order; 1809 table_size = tce_count << 3; 1810 1811 if (table_size < PAGE_SIZE) 1812 table_size = PAGE_SIZE; 1813 1814 table_pages = alloc_pages_node(pe->phb->hose->node, GFP_KERNEL, 1815 get_order(table_size)); 1816 if (!table_pages) 1817 goto err; 1818 1819 tces = page_address(table_pages); 1820 if (!tces) 1821 goto err; 1822 1823 memset(tces, 0, table_size); 1824 1825 for (addr = 0; addr < memory_hotplug_max(); addr += (1 << tce_order)) { 1826 tces[(addr + (1ULL << 32)) >> tce_order] = 1827 cpu_to_be64(addr | TCE_PCI_READ | TCE_PCI_WRITE); 1828 } 1829 1830 rc = opal_pci_map_pe_dma_window(pe->phb->opal_id, 1831 pe->pe_number, 1832 /* reconfigure window 0 */ 1833 (pe->pe_number << 1) + 0, 1834 1, 1835 __pa(tces), 1836 table_size, 1837 1 << tce_order); 1838 if (rc == OPAL_SUCCESS) { 1839 pe_info(pe, "Using 64-bit DMA iommu bypass (through TVE#0)\n"); 1840 return 0; 1841 } 1842 err: 1843 pe_err(pe, "Error configuring 64-bit DMA bypass\n"); 1844 return -EIO; 1845 } 1846 1847 static bool pnv_pci_ioda_iommu_bypass_supported(struct pci_dev *pdev, 1848 u64 dma_mask) 1849 { 1850 struct pci_controller *hose = pci_bus_to_host(pdev->bus); 1851 struct pnv_phb *phb = hose->private_data; 1852 struct pci_dn *pdn = pci_get_pdn(pdev); 1853 struct pnv_ioda_pe *pe; 1854 1855 if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE)) 1856 return false; 1857 1858 pe = &phb->ioda.pe_array[pdn->pe_number]; 1859 if (pe->tce_bypass_enabled) { 1860 u64 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1; 1861 if (dma_mask >= top) 1862 return true; 1863 } 1864 1865 /* 1866 * If the device can't set the TCE bypass bit but still wants 1867 * to access 4GB or more, on PHB3 we can reconfigure TVE#0 to 1868 * bypass the 32-bit region and be usable for 64-bit DMAs. 1869 * The device needs to be able to address all of this space. 1870 */ 1871 if (dma_mask >> 32 && 1872 dma_mask > (memory_hotplug_max() + (1ULL << 32)) && 1873 /* pe->pdev should be set if it's a single device, pe->pbus if not */ 1874 (pe->device_count == 1 || !pe->pbus) && 1875 phb->model == PNV_PHB_MODEL_PHB3) { 1876 /* Configure the bypass mode */ 1877 s64 rc = pnv_pci_ioda_dma_64bit_bypass(pe); 1878 if (rc) 1879 return false; 1880 /* 4GB offset bypasses 32-bit space */ 1881 pdev->dev.archdata.dma_offset = (1ULL << 32); 1882 return true; 1883 } 1884 1885 return false; 1886 } 1887 1888 static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe, struct pci_bus *bus) 1889 { 1890 struct pci_dev *dev; 1891 1892 list_for_each_entry(dev, &bus->devices, bus_list) { 1893 set_iommu_table_base(&dev->dev, pe->table_group.tables[0]); 1894 dev->dev.archdata.dma_offset = pe->tce_bypass_base; 1895 1896 if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate) 1897 pnv_ioda_setup_bus_dma(pe, dev->subordinate); 1898 } 1899 } 1900 1901 static inline __be64 __iomem *pnv_ioda_get_inval_reg(struct pnv_phb *phb, 1902 bool real_mode) 1903 { 1904 return real_mode ? (__be64 __iomem *)(phb->regs_phys + 0x210) : 1905 (phb->regs + 0x210); 1906 } 1907 1908 static void pnv_pci_p7ioc_tce_invalidate(struct iommu_table *tbl, 1909 unsigned long index, unsigned long npages, bool rm) 1910 { 1911 struct iommu_table_group_link *tgl = list_first_entry_or_null( 1912 &tbl->it_group_list, struct iommu_table_group_link, 1913 next); 1914 struct pnv_ioda_pe *pe = container_of(tgl->table_group, 1915 struct pnv_ioda_pe, table_group); 1916 __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb, rm); 1917 unsigned long start, end, inc; 1918 1919 start = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset); 1920 end = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset + 1921 npages - 1); 1922 1923 /* p7ioc-style invalidation, 2 TCEs per write */ 1924 start |= (1ull << 63); 1925 end |= (1ull << 63); 1926 inc = 16; 1927 end |= inc - 1; /* round up end to be different than start */ 1928 1929 mb(); /* Ensure above stores are visible */ 1930 while (start <= end) { 1931 if (rm) 1932 __raw_rm_writeq_be(start, invalidate); 1933 else 1934 __raw_writeq_be(start, invalidate); 1935 1936 start += inc; 1937 } 1938 1939 /* 1940 * The iommu layer will do another mb() for us on build() 1941 * and we don't care on free() 1942 */ 1943 } 1944 1945 static int pnv_ioda1_tce_build(struct iommu_table *tbl, long index, 1946 long npages, unsigned long uaddr, 1947 enum dma_data_direction direction, 1948 unsigned long attrs) 1949 { 1950 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction, 1951 attrs); 1952 1953 if (!ret) 1954 pnv_pci_p7ioc_tce_invalidate(tbl, index, npages, false); 1955 1956 return ret; 1957 } 1958 1959 #ifdef CONFIG_IOMMU_API 1960 /* Common for IODA1 and IODA2 */ 1961 static int pnv_ioda_tce_xchg_no_kill(struct iommu_table *tbl, long index, 1962 unsigned long *hpa, enum dma_data_direction *direction, 1963 bool realmode) 1964 { 1965 return pnv_tce_xchg(tbl, index, hpa, direction, !realmode); 1966 } 1967 #endif 1968 1969 static void pnv_ioda1_tce_free(struct iommu_table *tbl, long index, 1970 long npages) 1971 { 1972 pnv_tce_free(tbl, index, npages); 1973 1974 pnv_pci_p7ioc_tce_invalidate(tbl, index, npages, false); 1975 } 1976 1977 static struct iommu_table_ops pnv_ioda1_iommu_ops = { 1978 .set = pnv_ioda1_tce_build, 1979 #ifdef CONFIG_IOMMU_API 1980 .xchg_no_kill = pnv_ioda_tce_xchg_no_kill, 1981 .tce_kill = pnv_pci_p7ioc_tce_invalidate, 1982 .useraddrptr = pnv_tce_useraddrptr, 1983 #endif 1984 .clear = pnv_ioda1_tce_free, 1985 .get = pnv_tce_get, 1986 }; 1987 1988 #define PHB3_TCE_KILL_INVAL_ALL PPC_BIT(0) 1989 #define PHB3_TCE_KILL_INVAL_PE PPC_BIT(1) 1990 #define PHB3_TCE_KILL_INVAL_ONE PPC_BIT(2) 1991 1992 static void pnv_pci_phb3_tce_invalidate_entire(struct pnv_phb *phb, bool rm) 1993 { 1994 __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(phb, rm); 1995 const unsigned long val = PHB3_TCE_KILL_INVAL_ALL; 1996 1997 mb(); /* Ensure previous TCE table stores are visible */ 1998 if (rm) 1999 __raw_rm_writeq_be(val, invalidate); 2000 else 2001 __raw_writeq_be(val, invalidate); 2002 } 2003 2004 static inline void pnv_pci_phb3_tce_invalidate_pe(struct pnv_ioda_pe *pe) 2005 { 2006 /* 01xb - invalidate TCEs that match the specified PE# */ 2007 __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb, false); 2008 unsigned long val = PHB3_TCE_KILL_INVAL_PE | (pe->pe_number & 0xFF); 2009 2010 mb(); /* Ensure above stores are visible */ 2011 __raw_writeq_be(val, invalidate); 2012 } 2013 2014 static void pnv_pci_phb3_tce_invalidate(struct pnv_ioda_pe *pe, bool rm, 2015 unsigned shift, unsigned long index, 2016 unsigned long npages) 2017 { 2018 __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb, rm); 2019 unsigned long start, end, inc; 2020 2021 /* We'll invalidate DMA address in PE scope */ 2022 start = PHB3_TCE_KILL_INVAL_ONE; 2023 start |= (pe->pe_number & 0xFF); 2024 end = start; 2025 2026 /* Figure out the start, end and step */ 2027 start |= (index << shift); 2028 end |= ((index + npages - 1) << shift); 2029 inc = (0x1ull << shift); 2030 mb(); 2031 2032 while (start <= end) { 2033 if (rm) 2034 __raw_rm_writeq_be(start, invalidate); 2035 else 2036 __raw_writeq_be(start, invalidate); 2037 start += inc; 2038 } 2039 } 2040 2041 static inline void pnv_pci_ioda2_tce_invalidate_pe(struct pnv_ioda_pe *pe) 2042 { 2043 struct pnv_phb *phb = pe->phb; 2044 2045 if (phb->model == PNV_PHB_MODEL_PHB3 && phb->regs) 2046 pnv_pci_phb3_tce_invalidate_pe(pe); 2047 else 2048 opal_pci_tce_kill(phb->opal_id, OPAL_PCI_TCE_KILL_PE, 2049 pe->pe_number, 0, 0, 0); 2050 } 2051 2052 static void pnv_pci_ioda2_tce_invalidate(struct iommu_table *tbl, 2053 unsigned long index, unsigned long npages, bool rm) 2054 { 2055 struct iommu_table_group_link *tgl; 2056 2057 list_for_each_entry_lockless(tgl, &tbl->it_group_list, next) { 2058 struct pnv_ioda_pe *pe = container_of(tgl->table_group, 2059 struct pnv_ioda_pe, table_group); 2060 struct pnv_phb *phb = pe->phb; 2061 unsigned int shift = tbl->it_page_shift; 2062 2063 /* 2064 * NVLink1 can use the TCE kill register directly as 2065 * it's the same as PHB3. NVLink2 is different and 2066 * should go via the OPAL call. 2067 */ 2068 if (phb->model == PNV_PHB_MODEL_NPU) { 2069 /* 2070 * The NVLink hardware does not support TCE kill 2071 * per TCE entry so we have to invalidate 2072 * the entire cache for it. 2073 */ 2074 pnv_pci_phb3_tce_invalidate_entire(phb, rm); 2075 continue; 2076 } 2077 if (phb->model == PNV_PHB_MODEL_PHB3 && phb->regs) 2078 pnv_pci_phb3_tce_invalidate(pe, rm, shift, 2079 index, npages); 2080 else 2081 opal_pci_tce_kill(phb->opal_id, 2082 OPAL_PCI_TCE_KILL_PAGES, 2083 pe->pe_number, 1u << shift, 2084 index << shift, npages); 2085 } 2086 } 2087 2088 void pnv_pci_ioda2_tce_invalidate_entire(struct pnv_phb *phb, bool rm) 2089 { 2090 if (phb->model == PNV_PHB_MODEL_NPU || phb->model == PNV_PHB_MODEL_PHB3) 2091 pnv_pci_phb3_tce_invalidate_entire(phb, rm); 2092 else 2093 opal_pci_tce_kill(phb->opal_id, OPAL_PCI_TCE_KILL, 0, 0, 0, 0); 2094 } 2095 2096 static int pnv_ioda2_tce_build(struct iommu_table *tbl, long index, 2097 long npages, unsigned long uaddr, 2098 enum dma_data_direction direction, 2099 unsigned long attrs) 2100 { 2101 int ret = pnv_tce_build(tbl, index, npages, uaddr, direction, 2102 attrs); 2103 2104 if (!ret) 2105 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false); 2106 2107 return ret; 2108 } 2109 2110 static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index, 2111 long npages) 2112 { 2113 pnv_tce_free(tbl, index, npages); 2114 2115 pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false); 2116 } 2117 2118 static struct iommu_table_ops pnv_ioda2_iommu_ops = { 2119 .set = pnv_ioda2_tce_build, 2120 #ifdef CONFIG_IOMMU_API 2121 .xchg_no_kill = pnv_ioda_tce_xchg_no_kill, 2122 .tce_kill = pnv_pci_ioda2_tce_invalidate, 2123 .useraddrptr = pnv_tce_useraddrptr, 2124 #endif 2125 .clear = pnv_ioda2_tce_free, 2126 .get = pnv_tce_get, 2127 .free = pnv_pci_ioda2_table_free_pages, 2128 }; 2129 2130 static int pnv_pci_ioda_dev_dma_weight(struct pci_dev *dev, void *data) 2131 { 2132 unsigned int *weight = (unsigned int *)data; 2133 2134 /* This is quite simplistic. The "base" weight of a device 2135 * is 10. 0 means no DMA is to be accounted for it. 2136 */ 2137 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL) 2138 return 0; 2139 2140 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI || 2141 dev->class == PCI_CLASS_SERIAL_USB_OHCI || 2142 dev->class == PCI_CLASS_SERIAL_USB_EHCI) 2143 *weight += 3; 2144 else if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID) 2145 *weight += 15; 2146 else 2147 *weight += 10; 2148 2149 return 0; 2150 } 2151 2152 static unsigned int pnv_pci_ioda_pe_dma_weight(struct pnv_ioda_pe *pe) 2153 { 2154 unsigned int weight = 0; 2155 2156 /* SRIOV VF has same DMA32 weight as its PF */ 2157 #ifdef CONFIG_PCI_IOV 2158 if ((pe->flags & PNV_IODA_PE_VF) && pe->parent_dev) { 2159 pnv_pci_ioda_dev_dma_weight(pe->parent_dev, &weight); 2160 return weight; 2161 } 2162 #endif 2163 2164 if ((pe->flags & PNV_IODA_PE_DEV) && pe->pdev) { 2165 pnv_pci_ioda_dev_dma_weight(pe->pdev, &weight); 2166 } else if ((pe->flags & PNV_IODA_PE_BUS) && pe->pbus) { 2167 struct pci_dev *pdev; 2168 2169 list_for_each_entry(pdev, &pe->pbus->devices, bus_list) 2170 pnv_pci_ioda_dev_dma_weight(pdev, &weight); 2171 } else if ((pe->flags & PNV_IODA_PE_BUS_ALL) && pe->pbus) { 2172 pci_walk_bus(pe->pbus, pnv_pci_ioda_dev_dma_weight, &weight); 2173 } 2174 2175 return weight; 2176 } 2177 2178 static void pnv_pci_ioda1_setup_dma_pe(struct pnv_phb *phb, 2179 struct pnv_ioda_pe *pe) 2180 { 2181 2182 struct page *tce_mem = NULL; 2183 struct iommu_table *tbl; 2184 unsigned int weight, total_weight = 0; 2185 unsigned int tce32_segsz, base, segs, avail, i; 2186 int64_t rc; 2187 void *addr; 2188 2189 /* XXX FIXME: Handle 64-bit only DMA devices */ 2190 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */ 2191 /* XXX FIXME: Allocate multi-level tables on PHB3 */ 2192 weight = pnv_pci_ioda_pe_dma_weight(pe); 2193 if (!weight) 2194 return; 2195 2196 pci_walk_bus(phb->hose->bus, pnv_pci_ioda_dev_dma_weight, 2197 &total_weight); 2198 segs = (weight * phb->ioda.dma32_count) / total_weight; 2199 if (!segs) 2200 segs = 1; 2201 2202 /* 2203 * Allocate contiguous DMA32 segments. We begin with the expected 2204 * number of segments. With one more attempt, the number of DMA32 2205 * segments to be allocated is decreased by one until one segment 2206 * is allocated successfully. 2207 */ 2208 do { 2209 for (base = 0; base <= phb->ioda.dma32_count - segs; base++) { 2210 for (avail = 0, i = base; i < base + segs; i++) { 2211 if (phb->ioda.dma32_segmap[i] == 2212 IODA_INVALID_PE) 2213 avail++; 2214 } 2215 2216 if (avail == segs) 2217 goto found; 2218 } 2219 } while (--segs); 2220 2221 if (!segs) { 2222 pe_warn(pe, "No available DMA32 segments\n"); 2223 return; 2224 } 2225 2226 found: 2227 tbl = pnv_pci_table_alloc(phb->hose->node); 2228 if (WARN_ON(!tbl)) 2229 return; 2230 2231 iommu_register_group(&pe->table_group, phb->hose->global_number, 2232 pe->pe_number); 2233 pnv_pci_link_table_and_group(phb->hose->node, 0, tbl, &pe->table_group); 2234 2235 /* Grab a 32-bit TCE table */ 2236 pe_info(pe, "DMA weight %d (%d), assigned (%d) %d DMA32 segments\n", 2237 weight, total_weight, base, segs); 2238 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n", 2239 base * PNV_IODA1_DMA32_SEGSIZE, 2240 (base + segs) * PNV_IODA1_DMA32_SEGSIZE - 1); 2241 2242 /* XXX Currently, we allocate one big contiguous table for the 2243 * TCEs. We only really need one chunk per 256M of TCE space 2244 * (ie per segment) but that's an optimization for later, it 2245 * requires some added smarts with our get/put_tce implementation 2246 * 2247 * Each TCE page is 4KB in size and each TCE entry occupies 8 2248 * bytes 2249 */ 2250 tce32_segsz = PNV_IODA1_DMA32_SEGSIZE >> (IOMMU_PAGE_SHIFT_4K - 3); 2251 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL, 2252 get_order(tce32_segsz * segs)); 2253 if (!tce_mem) { 2254 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n"); 2255 goto fail; 2256 } 2257 addr = page_address(tce_mem); 2258 memset(addr, 0, tce32_segsz * segs); 2259 2260 /* Configure HW */ 2261 for (i = 0; i < segs; i++) { 2262 rc = opal_pci_map_pe_dma_window(phb->opal_id, 2263 pe->pe_number, 2264 base + i, 1, 2265 __pa(addr) + tce32_segsz * i, 2266 tce32_segsz, IOMMU_PAGE_SIZE_4K); 2267 if (rc) { 2268 pe_err(pe, " Failed to configure 32-bit TCE table, err %lld\n", 2269 rc); 2270 goto fail; 2271 } 2272 } 2273 2274 /* Setup DMA32 segment mapping */ 2275 for (i = base; i < base + segs; i++) 2276 phb->ioda.dma32_segmap[i] = pe->pe_number; 2277 2278 /* Setup linux iommu table */ 2279 pnv_pci_setup_iommu_table(tbl, addr, tce32_segsz * segs, 2280 base * PNV_IODA1_DMA32_SEGSIZE, 2281 IOMMU_PAGE_SHIFT_4K); 2282 2283 tbl->it_ops = &pnv_ioda1_iommu_ops; 2284 pe->table_group.tce32_start = tbl->it_offset << tbl->it_page_shift; 2285 pe->table_group.tce32_size = tbl->it_size << tbl->it_page_shift; 2286 iommu_init_table(tbl, phb->hose->node, 0, 0); 2287 2288 return; 2289 fail: 2290 /* XXX Failure: Try to fallback to 64-bit only ? */ 2291 if (tce_mem) 2292 __free_pages(tce_mem, get_order(tce32_segsz * segs)); 2293 if (tbl) { 2294 pnv_pci_unlink_table_and_group(tbl, &pe->table_group); 2295 iommu_tce_table_put(tbl); 2296 } 2297 } 2298 2299 static long pnv_pci_ioda2_set_window(struct iommu_table_group *table_group, 2300 int num, struct iommu_table *tbl) 2301 { 2302 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe, 2303 table_group); 2304 struct pnv_phb *phb = pe->phb; 2305 int64_t rc; 2306 const unsigned long size = tbl->it_indirect_levels ? 2307 tbl->it_level_size : tbl->it_size; 2308 const __u64 start_addr = tbl->it_offset << tbl->it_page_shift; 2309 const __u64 win_size = tbl->it_size << tbl->it_page_shift; 2310 2311 pe_info(pe, "Setting up window#%d %llx..%llx pg=%lx\n", 2312 num, start_addr, start_addr + win_size - 1, 2313 IOMMU_PAGE_SIZE(tbl)); 2314 2315 /* 2316 * Map TCE table through TVT. The TVE index is the PE number 2317 * shifted by 1 bit for 32-bits DMA space. 2318 */ 2319 rc = opal_pci_map_pe_dma_window(phb->opal_id, 2320 pe->pe_number, 2321 (pe->pe_number << 1) + num, 2322 tbl->it_indirect_levels + 1, 2323 __pa(tbl->it_base), 2324 size << 3, 2325 IOMMU_PAGE_SIZE(tbl)); 2326 if (rc) { 2327 pe_err(pe, "Failed to configure TCE table, err %lld\n", rc); 2328 return rc; 2329 } 2330 2331 pnv_pci_link_table_and_group(phb->hose->node, num, 2332 tbl, &pe->table_group); 2333 pnv_pci_ioda2_tce_invalidate_pe(pe); 2334 2335 return 0; 2336 } 2337 2338 static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable) 2339 { 2340 uint16_t window_id = (pe->pe_number << 1 ) + 1; 2341 int64_t rc; 2342 2343 pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis"); 2344 if (enable) { 2345 phys_addr_t top = memblock_end_of_DRAM(); 2346 2347 top = roundup_pow_of_two(top); 2348 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id, 2349 pe->pe_number, 2350 window_id, 2351 pe->tce_bypass_base, 2352 top); 2353 } else { 2354 rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id, 2355 pe->pe_number, 2356 window_id, 2357 pe->tce_bypass_base, 2358 0); 2359 } 2360 if (rc) 2361 pe_err(pe, "OPAL error %lld configuring bypass window\n", rc); 2362 else 2363 pe->tce_bypass_enabled = enable; 2364 } 2365 2366 static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group, 2367 int num, __u32 page_shift, __u64 window_size, __u32 levels, 2368 bool alloc_userspace_copy, struct iommu_table **ptbl) 2369 { 2370 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe, 2371 table_group); 2372 int nid = pe->phb->hose->node; 2373 __u64 bus_offset = num ? pe->tce_bypass_base : table_group->tce32_start; 2374 long ret; 2375 struct iommu_table *tbl; 2376 2377 tbl = pnv_pci_table_alloc(nid); 2378 if (!tbl) 2379 return -ENOMEM; 2380 2381 tbl->it_ops = &pnv_ioda2_iommu_ops; 2382 2383 ret = pnv_pci_ioda2_table_alloc_pages(nid, 2384 bus_offset, page_shift, window_size, 2385 levels, alloc_userspace_copy, tbl); 2386 if (ret) { 2387 iommu_tce_table_put(tbl); 2388 return ret; 2389 } 2390 2391 *ptbl = tbl; 2392 2393 return 0; 2394 } 2395 2396 static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe) 2397 { 2398 struct iommu_table *tbl = NULL; 2399 long rc; 2400 unsigned long res_start, res_end; 2401 2402 /* 2403 * crashkernel= specifies the kdump kernel's maximum memory at 2404 * some offset and there is no guaranteed the result is a power 2405 * of 2, which will cause errors later. 2406 */ 2407 const u64 max_memory = __rounddown_pow_of_two(memory_hotplug_max()); 2408 2409 /* 2410 * In memory constrained environments, e.g. kdump kernel, the 2411 * DMA window can be larger than available memory, which will 2412 * cause errors later. 2413 */ 2414 const u64 maxblock = 1UL << (PAGE_SHIFT + MAX_ORDER - 1); 2415 2416 /* 2417 * We create the default window as big as we can. The constraint is 2418 * the max order of allocation possible. The TCE table is likely to 2419 * end up being multilevel and with on-demand allocation in place, 2420 * the initial use is not going to be huge as the default window aims 2421 * to support crippled devices (i.e. not fully 64bit DMAble) only. 2422 */ 2423 /* iommu_table::it_map uses 1 bit per IOMMU page, hence 8 */ 2424 const u64 window_size = min((maxblock * 8) << PAGE_SHIFT, max_memory); 2425 /* Each TCE level cannot exceed maxblock so go multilevel if needed */ 2426 unsigned long tces_order = ilog2(window_size >> PAGE_SHIFT); 2427 unsigned long tcelevel_order = ilog2(maxblock >> 3); 2428 unsigned int levels = tces_order / tcelevel_order; 2429 2430 if (tces_order % tcelevel_order) 2431 levels += 1; 2432 /* 2433 * We try to stick to default levels (which is >1 at the moment) in 2434 * order to save memory by relying on on-demain TCE level allocation. 2435 */ 2436 levels = max_t(unsigned int, levels, POWERNV_IOMMU_DEFAULT_LEVELS); 2437 2438 rc = pnv_pci_ioda2_create_table(&pe->table_group, 0, PAGE_SHIFT, 2439 window_size, levels, false, &tbl); 2440 if (rc) { 2441 pe_err(pe, "Failed to create 32-bit TCE table, err %ld", 2442 rc); 2443 return rc; 2444 } 2445 2446 /* We use top part of 32bit space for MMIO so exclude it from DMA */ 2447 res_start = 0; 2448 res_end = 0; 2449 if (window_size > pe->phb->ioda.m32_pci_base) { 2450 res_start = pe->phb->ioda.m32_pci_base >> tbl->it_page_shift; 2451 res_end = min(window_size, SZ_4G) >> tbl->it_page_shift; 2452 } 2453 iommu_init_table(tbl, pe->phb->hose->node, res_start, res_end); 2454 2455 rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl); 2456 if (rc) { 2457 pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n", 2458 rc); 2459 iommu_tce_table_put(tbl); 2460 return rc; 2461 } 2462 2463 if (!pnv_iommu_bypass_disabled) 2464 pnv_pci_ioda2_set_bypass(pe, true); 2465 2466 /* 2467 * Set table base for the case of IOMMU DMA use. Usually this is done 2468 * from dma_dev_setup() which is not called when a device is returned 2469 * from VFIO so do it here. 2470 */ 2471 if (pe->pdev) 2472 set_iommu_table_base(&pe->pdev->dev, tbl); 2473 2474 return 0; 2475 } 2476 2477 #if defined(CONFIG_IOMMU_API) || defined(CONFIG_PCI_IOV) 2478 static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group, 2479 int num) 2480 { 2481 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe, 2482 table_group); 2483 struct pnv_phb *phb = pe->phb; 2484 long ret; 2485 2486 pe_info(pe, "Removing DMA window #%d\n", num); 2487 2488 ret = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number, 2489 (pe->pe_number << 1) + num, 2490 0/* levels */, 0/* table address */, 2491 0/* table size */, 0/* page size */); 2492 if (ret) 2493 pe_warn(pe, "Unmapping failed, ret = %ld\n", ret); 2494 else 2495 pnv_pci_ioda2_tce_invalidate_pe(pe); 2496 2497 pnv_pci_unlink_table_and_group(table_group->tables[num], table_group); 2498 2499 return ret; 2500 } 2501 #endif 2502 2503 #ifdef CONFIG_IOMMU_API 2504 unsigned long pnv_pci_ioda2_get_table_size(__u32 page_shift, 2505 __u64 window_size, __u32 levels) 2506 { 2507 unsigned long bytes = 0; 2508 const unsigned window_shift = ilog2(window_size); 2509 unsigned entries_shift = window_shift - page_shift; 2510 unsigned table_shift = entries_shift + 3; 2511 unsigned long tce_table_size = max(0x1000UL, 1UL << table_shift); 2512 unsigned long direct_table_size; 2513 2514 if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS) || 2515 !is_power_of_2(window_size)) 2516 return 0; 2517 2518 /* Calculate a direct table size from window_size and levels */ 2519 entries_shift = (entries_shift + levels - 1) / levels; 2520 table_shift = entries_shift + 3; 2521 table_shift = max_t(unsigned, table_shift, PAGE_SHIFT); 2522 direct_table_size = 1UL << table_shift; 2523 2524 for ( ; levels; --levels) { 2525 bytes += ALIGN(tce_table_size, direct_table_size); 2526 2527 tce_table_size /= direct_table_size; 2528 tce_table_size <<= 3; 2529 tce_table_size = max_t(unsigned long, 2530 tce_table_size, direct_table_size); 2531 } 2532 2533 return bytes + bytes; /* one for HW table, one for userspace copy */ 2534 } 2535 2536 static long pnv_pci_ioda2_create_table_userspace( 2537 struct iommu_table_group *table_group, 2538 int num, __u32 page_shift, __u64 window_size, __u32 levels, 2539 struct iommu_table **ptbl) 2540 { 2541 long ret = pnv_pci_ioda2_create_table(table_group, 2542 num, page_shift, window_size, levels, true, ptbl); 2543 2544 if (!ret) 2545 (*ptbl)->it_allocated_size = pnv_pci_ioda2_get_table_size( 2546 page_shift, window_size, levels); 2547 return ret; 2548 } 2549 2550 static void pnv_ioda2_take_ownership(struct iommu_table_group *table_group) 2551 { 2552 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe, 2553 table_group); 2554 /* Store @tbl as pnv_pci_ioda2_unset_window() resets it */ 2555 struct iommu_table *tbl = pe->table_group.tables[0]; 2556 2557 pnv_pci_ioda2_set_bypass(pe, false); 2558 pnv_pci_ioda2_unset_window(&pe->table_group, 0); 2559 if (pe->pbus) 2560 pnv_ioda_setup_bus_dma(pe, pe->pbus); 2561 else if (pe->pdev) 2562 set_iommu_table_base(&pe->pdev->dev, NULL); 2563 iommu_tce_table_put(tbl); 2564 } 2565 2566 static void pnv_ioda2_release_ownership(struct iommu_table_group *table_group) 2567 { 2568 struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe, 2569 table_group); 2570 2571 pnv_pci_ioda2_setup_default_config(pe); 2572 if (pe->pbus) 2573 pnv_ioda_setup_bus_dma(pe, pe->pbus); 2574 } 2575 2576 static struct iommu_table_group_ops pnv_pci_ioda2_ops = { 2577 .get_table_size = pnv_pci_ioda2_get_table_size, 2578 .create_table = pnv_pci_ioda2_create_table_userspace, 2579 .set_window = pnv_pci_ioda2_set_window, 2580 .unset_window = pnv_pci_ioda2_unset_window, 2581 .take_ownership = pnv_ioda2_take_ownership, 2582 .release_ownership = pnv_ioda2_release_ownership, 2583 }; 2584 #endif 2585 2586 static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb, 2587 struct pnv_ioda_pe *pe) 2588 { 2589 int64_t rc; 2590 2591 if (!pnv_pci_ioda_pe_dma_weight(pe)) 2592 return; 2593 2594 /* TVE #1 is selected by PCI address bit 59 */ 2595 pe->tce_bypass_base = 1ull << 59; 2596 2597 /* The PE will reserve all possible 32-bits space */ 2598 pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n", 2599 phb->ioda.m32_pci_base); 2600 2601 /* Setup linux iommu table */ 2602 pe->table_group.tce32_start = 0; 2603 pe->table_group.tce32_size = phb->ioda.m32_pci_base; 2604 pe->table_group.max_dynamic_windows_supported = 2605 IOMMU_TABLE_GROUP_MAX_TABLES; 2606 pe->table_group.max_levels = POWERNV_IOMMU_MAX_LEVELS; 2607 pe->table_group.pgsizes = pnv_ioda_parse_tce_sizes(phb); 2608 2609 rc = pnv_pci_ioda2_setup_default_config(pe); 2610 if (rc) 2611 return; 2612 2613 #ifdef CONFIG_IOMMU_API 2614 pe->table_group.ops = &pnv_pci_ioda2_ops; 2615 iommu_register_group(&pe->table_group, phb->hose->global_number, 2616 pe->pe_number); 2617 #endif 2618 } 2619 2620 int64_t pnv_opal_pci_msi_eoi(struct irq_chip *chip, unsigned int hw_irq) 2621 { 2622 struct pnv_phb *phb = container_of(chip, struct pnv_phb, 2623 ioda.irq_chip); 2624 2625 return opal_pci_msi_eoi(phb->opal_id, hw_irq); 2626 } 2627 2628 static void pnv_ioda2_msi_eoi(struct irq_data *d) 2629 { 2630 int64_t rc; 2631 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d); 2632 struct irq_chip *chip = irq_data_get_irq_chip(d); 2633 2634 rc = pnv_opal_pci_msi_eoi(chip, hw_irq); 2635 WARN_ON_ONCE(rc); 2636 2637 icp_native_eoi(d); 2638 } 2639 2640 2641 void pnv_set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq) 2642 { 2643 struct irq_data *idata; 2644 struct irq_chip *ichip; 2645 2646 /* The MSI EOI OPAL call is only needed on PHB3 */ 2647 if (phb->model != PNV_PHB_MODEL_PHB3) 2648 return; 2649 2650 if (!phb->ioda.irq_chip_init) { 2651 /* 2652 * First time we setup an MSI IRQ, we need to setup the 2653 * corresponding IRQ chip to route correctly. 2654 */ 2655 idata = irq_get_irq_data(virq); 2656 ichip = irq_data_get_irq_chip(idata); 2657 phb->ioda.irq_chip_init = 1; 2658 phb->ioda.irq_chip = *ichip; 2659 phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi; 2660 } 2661 irq_set_chip(virq, &phb->ioda.irq_chip); 2662 } 2663 2664 /* 2665 * Returns true iff chip is something that we could call 2666 * pnv_opal_pci_msi_eoi for. 2667 */ 2668 bool is_pnv_opal_msi(struct irq_chip *chip) 2669 { 2670 return chip->irq_eoi == pnv_ioda2_msi_eoi; 2671 } 2672 EXPORT_SYMBOL_GPL(is_pnv_opal_msi); 2673 2674 static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev, 2675 unsigned int hwirq, unsigned int virq, 2676 unsigned int is_64, struct msi_msg *msg) 2677 { 2678 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev); 2679 unsigned int xive_num = hwirq - phb->msi_base; 2680 __be32 data; 2681 int rc; 2682 2683 /* No PE assigned ? bail out ... no MSI for you ! */ 2684 if (pe == NULL) 2685 return -ENXIO; 2686 2687 /* Check if we have an MVE */ 2688 if (pe->mve_number < 0) 2689 return -ENXIO; 2690 2691 /* Force 32-bit MSI on some broken devices */ 2692 if (dev->no_64bit_msi) 2693 is_64 = 0; 2694 2695 /* Assign XIVE to PE */ 2696 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num); 2697 if (rc) { 2698 pr_warn("%s: OPAL error %d setting XIVE %d PE\n", 2699 pci_name(dev), rc, xive_num); 2700 return -EIO; 2701 } 2702 2703 if (is_64) { 2704 __be64 addr64; 2705 2706 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1, 2707 &addr64, &data); 2708 if (rc) { 2709 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n", 2710 pci_name(dev), rc); 2711 return -EIO; 2712 } 2713 msg->address_hi = be64_to_cpu(addr64) >> 32; 2714 msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful; 2715 } else { 2716 __be32 addr32; 2717 2718 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1, 2719 &addr32, &data); 2720 if (rc) { 2721 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n", 2722 pci_name(dev), rc); 2723 return -EIO; 2724 } 2725 msg->address_hi = 0; 2726 msg->address_lo = be32_to_cpu(addr32); 2727 } 2728 msg->data = be32_to_cpu(data); 2729 2730 pnv_set_msi_irq_chip(phb, virq); 2731 2732 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d)," 2733 " address=%x_%08x data=%x PE# %x\n", 2734 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num, 2735 msg->address_hi, msg->address_lo, data, pe->pe_number); 2736 2737 return 0; 2738 } 2739 2740 static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) 2741 { 2742 unsigned int count; 2743 const __be32 *prop = of_get_property(phb->hose->dn, 2744 "ibm,opal-msi-ranges", NULL); 2745 if (!prop) { 2746 /* BML Fallback */ 2747 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL); 2748 } 2749 if (!prop) 2750 return; 2751 2752 phb->msi_base = be32_to_cpup(prop); 2753 count = be32_to_cpup(prop + 1); 2754 if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) { 2755 pr_err("PCI %d: Failed to allocate MSI bitmap !\n", 2756 phb->hose->global_number); 2757 return; 2758 } 2759 2760 phb->msi_setup = pnv_pci_ioda_msi_setup; 2761 phb->msi32_support = 1; 2762 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n", 2763 count, phb->msi_base); 2764 } 2765 2766 #ifdef CONFIG_PCI_IOV 2767 static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev) 2768 { 2769 struct pci_controller *hose = pci_bus_to_host(pdev->bus); 2770 struct pnv_phb *phb = hose->private_data; 2771 const resource_size_t gate = phb->ioda.m64_segsize >> 2; 2772 struct resource *res; 2773 int i; 2774 resource_size_t size, total_vf_bar_sz; 2775 struct pci_dn *pdn; 2776 int mul, total_vfs; 2777 2778 pdn = pci_get_pdn(pdev); 2779 pdn->vfs_expanded = 0; 2780 pdn->m64_single_mode = false; 2781 2782 total_vfs = pci_sriov_get_totalvfs(pdev); 2783 mul = phb->ioda.total_pe_num; 2784 total_vf_bar_sz = 0; 2785 2786 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { 2787 res = &pdev->resource[i + PCI_IOV_RESOURCES]; 2788 if (!res->flags || res->parent) 2789 continue; 2790 if (!pnv_pci_is_m64_flags(res->flags)) { 2791 dev_warn(&pdev->dev, "Don't support SR-IOV with" 2792 " non M64 VF BAR%d: %pR. \n", 2793 i, res); 2794 goto truncate_iov; 2795 } 2796 2797 total_vf_bar_sz += pci_iov_resource_size(pdev, 2798 i + PCI_IOV_RESOURCES); 2799 2800 /* 2801 * If bigger than quarter of M64 segment size, just round up 2802 * power of two. 2803 * 2804 * Generally, one M64 BAR maps one IOV BAR. To avoid conflict 2805 * with other devices, IOV BAR size is expanded to be 2806 * (total_pe * VF_BAR_size). When VF_BAR_size is half of M64 2807 * segment size , the expanded size would equal to half of the 2808 * whole M64 space size, which will exhaust the M64 Space and 2809 * limit the system flexibility. This is a design decision to 2810 * set the boundary to quarter of the M64 segment size. 2811 */ 2812 if (total_vf_bar_sz > gate) { 2813 mul = roundup_pow_of_two(total_vfs); 2814 dev_info(&pdev->dev, 2815 "VF BAR Total IOV size %llx > %llx, roundup to %d VFs\n", 2816 total_vf_bar_sz, gate, mul); 2817 pdn->m64_single_mode = true; 2818 break; 2819 } 2820 } 2821 2822 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { 2823 res = &pdev->resource[i + PCI_IOV_RESOURCES]; 2824 if (!res->flags || res->parent) 2825 continue; 2826 2827 size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES); 2828 /* 2829 * On PHB3, the minimum size alignment of M64 BAR in single 2830 * mode is 32MB. 2831 */ 2832 if (pdn->m64_single_mode && (size < SZ_32M)) 2833 goto truncate_iov; 2834 dev_dbg(&pdev->dev, " Fixing VF BAR%d: %pR to\n", i, res); 2835 res->end = res->start + size * mul - 1; 2836 dev_dbg(&pdev->dev, " %pR\n", res); 2837 dev_info(&pdev->dev, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)", 2838 i, res, mul); 2839 } 2840 pdn->vfs_expanded = mul; 2841 2842 return; 2843 2844 truncate_iov: 2845 /* To save MMIO space, IOV BAR is truncated. */ 2846 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { 2847 res = &pdev->resource[i + PCI_IOV_RESOURCES]; 2848 res->flags = 0; 2849 res->end = res->start - 1; 2850 } 2851 } 2852 2853 static void pnv_pci_ioda_fixup_iov(struct pci_dev *pdev) 2854 { 2855 if (WARN_ON(pci_dev_is_added(pdev))) 2856 return; 2857 2858 if (pdev->is_virtfn) { 2859 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(pdev); 2860 2861 /* 2862 * VF PEs are single-device PEs so their pdev pointer needs to 2863 * be set. The pdev doesn't exist when the PE is allocated (in 2864 * (pcibios_sriov_enable()) so we fix it up here. 2865 */ 2866 pe->pdev = pdev; 2867 WARN_ON(!(pe->flags & PNV_IODA_PE_VF)); 2868 } else if (pdev->is_physfn) { 2869 /* 2870 * For PFs adjust their allocated IOV resources to match what 2871 * the PHB can support using it's M64 BAR table. 2872 */ 2873 pnv_pci_ioda_fixup_iov_resources(pdev); 2874 } 2875 } 2876 #endif /* CONFIG_PCI_IOV */ 2877 2878 static void pnv_ioda_setup_pe_res(struct pnv_ioda_pe *pe, 2879 struct resource *res) 2880 { 2881 struct pnv_phb *phb = pe->phb; 2882 struct pci_bus_region region; 2883 int index; 2884 int64_t rc; 2885 2886 if (!res || !res->flags || res->start > res->end) 2887 return; 2888 2889 if (res->flags & IORESOURCE_IO) { 2890 region.start = res->start - phb->ioda.io_pci_base; 2891 region.end = res->end - phb->ioda.io_pci_base; 2892 index = region.start / phb->ioda.io_segsize; 2893 2894 while (index < phb->ioda.total_pe_num && 2895 region.start <= region.end) { 2896 phb->ioda.io_segmap[index] = pe->pe_number; 2897 rc = opal_pci_map_pe_mmio_window(phb->opal_id, 2898 pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index); 2899 if (rc != OPAL_SUCCESS) { 2900 pr_err("%s: Error %lld mapping IO segment#%d to PE#%x\n", 2901 __func__, rc, index, pe->pe_number); 2902 break; 2903 } 2904 2905 region.start += phb->ioda.io_segsize; 2906 index++; 2907 } 2908 } else if ((res->flags & IORESOURCE_MEM) && 2909 !pnv_pci_is_m64(phb, res)) { 2910 region.start = res->start - 2911 phb->hose->mem_offset[0] - 2912 phb->ioda.m32_pci_base; 2913 region.end = res->end - 2914 phb->hose->mem_offset[0] - 2915 phb->ioda.m32_pci_base; 2916 index = region.start / phb->ioda.m32_segsize; 2917 2918 while (index < phb->ioda.total_pe_num && 2919 region.start <= region.end) { 2920 phb->ioda.m32_segmap[index] = pe->pe_number; 2921 rc = opal_pci_map_pe_mmio_window(phb->opal_id, 2922 pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index); 2923 if (rc != OPAL_SUCCESS) { 2924 pr_err("%s: Error %lld mapping M32 segment#%d to PE#%x", 2925 __func__, rc, index, pe->pe_number); 2926 break; 2927 } 2928 2929 region.start += phb->ioda.m32_segsize; 2930 index++; 2931 } 2932 } 2933 } 2934 2935 /* 2936 * This function is supposed to be called on basis of PE from top 2937 * to bottom style. So the the I/O or MMIO segment assigned to 2938 * parent PE could be overridden by its child PEs if necessary. 2939 */ 2940 static void pnv_ioda_setup_pe_seg(struct pnv_ioda_pe *pe) 2941 { 2942 struct pci_dev *pdev; 2943 int i; 2944 2945 /* 2946 * NOTE: We only care PCI bus based PE for now. For PCI 2947 * device based PE, for example SRIOV sensitive VF should 2948 * be figured out later. 2949 */ 2950 BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))); 2951 2952 list_for_each_entry(pdev, &pe->pbus->devices, bus_list) { 2953 for (i = 0; i <= PCI_ROM_RESOURCE; i++) 2954 pnv_ioda_setup_pe_res(pe, &pdev->resource[i]); 2955 2956 /* 2957 * If the PE contains all subordinate PCI buses, the 2958 * windows of the child bridges should be mapped to 2959 * the PE as well. 2960 */ 2961 if (!(pe->flags & PNV_IODA_PE_BUS_ALL) || !pci_is_bridge(pdev)) 2962 continue; 2963 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) 2964 pnv_ioda_setup_pe_res(pe, 2965 &pdev->resource[PCI_BRIDGE_RESOURCES + i]); 2966 } 2967 } 2968 2969 #ifdef CONFIG_DEBUG_FS 2970 static int pnv_pci_diag_data_set(void *data, u64 val) 2971 { 2972 struct pnv_phb *phb = data; 2973 s64 ret; 2974 2975 /* Retrieve the diag data from firmware */ 2976 ret = opal_pci_get_phb_diag_data2(phb->opal_id, phb->diag_data, 2977 phb->diag_data_size); 2978 if (ret != OPAL_SUCCESS) 2979 return -EIO; 2980 2981 /* Print the diag data to the kernel log */ 2982 pnv_pci_dump_phb_diag_data(phb->hose, phb->diag_data); 2983 return 0; 2984 } 2985 2986 DEFINE_DEBUGFS_ATTRIBUTE(pnv_pci_diag_data_fops, NULL, pnv_pci_diag_data_set, 2987 "%llu\n"); 2988 2989 static int pnv_pci_ioda_pe_dump(void *data, u64 val) 2990 { 2991 struct pnv_phb *phb = data; 2992 int pe_num; 2993 2994 for (pe_num = 0; pe_num < phb->ioda.total_pe_num; pe_num++) { 2995 struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_num]; 2996 2997 if (!test_bit(pe_num, phb->ioda.pe_alloc)) 2998 continue; 2999 3000 pe_warn(pe, "rid: %04x dev count: %2d flags: %s%s%s%s%s%s\n", 3001 pe->rid, pe->device_count, 3002 (pe->flags & PNV_IODA_PE_DEV) ? "dev " : "", 3003 (pe->flags & PNV_IODA_PE_BUS) ? "bus " : "", 3004 (pe->flags & PNV_IODA_PE_BUS_ALL) ? "all " : "", 3005 (pe->flags & PNV_IODA_PE_MASTER) ? "master " : "", 3006 (pe->flags & PNV_IODA_PE_SLAVE) ? "slave " : "", 3007 (pe->flags & PNV_IODA_PE_VF) ? "vf " : ""); 3008 } 3009 3010 return 0; 3011 } 3012 3013 DEFINE_DEBUGFS_ATTRIBUTE(pnv_pci_ioda_pe_dump_fops, NULL, 3014 pnv_pci_ioda_pe_dump, "%llu\n"); 3015 3016 #endif /* CONFIG_DEBUG_FS */ 3017 3018 static void pnv_pci_ioda_create_dbgfs(void) 3019 { 3020 #ifdef CONFIG_DEBUG_FS 3021 struct pci_controller *hose, *tmp; 3022 struct pnv_phb *phb; 3023 char name[16]; 3024 3025 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { 3026 phb = hose->private_data; 3027 3028 /* Notify initialization of PHB done */ 3029 phb->initialized = 1; 3030 3031 sprintf(name, "PCI%04x", hose->global_number); 3032 phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root); 3033 3034 debugfs_create_file_unsafe("dump_diag_regs", 0200, phb->dbgfs, 3035 phb, &pnv_pci_diag_data_fops); 3036 debugfs_create_file_unsafe("dump_ioda_pe_state", 0200, phb->dbgfs, 3037 phb, &pnv_pci_ioda_pe_dump_fops); 3038 } 3039 #endif /* CONFIG_DEBUG_FS */ 3040 } 3041 3042 static void pnv_pci_enable_bridge(struct pci_bus *bus) 3043 { 3044 struct pci_dev *dev = bus->self; 3045 struct pci_bus *child; 3046 3047 /* Empty bus ? bail */ 3048 if (list_empty(&bus->devices)) 3049 return; 3050 3051 /* 3052 * If there's a bridge associated with that bus enable it. This works 3053 * around races in the generic code if the enabling is done during 3054 * parallel probing. This can be removed once those races have been 3055 * fixed. 3056 */ 3057 if (dev) { 3058 int rc = pci_enable_device(dev); 3059 if (rc) 3060 pci_err(dev, "Error enabling bridge (%d)\n", rc); 3061 pci_set_master(dev); 3062 } 3063 3064 /* Perform the same to child busses */ 3065 list_for_each_entry(child, &bus->children, node) 3066 pnv_pci_enable_bridge(child); 3067 } 3068 3069 static void pnv_pci_enable_bridges(void) 3070 { 3071 struct pci_controller *hose; 3072 3073 list_for_each_entry(hose, &hose_list, list_node) 3074 pnv_pci_enable_bridge(hose->bus); 3075 } 3076 3077 static void pnv_pci_ioda_fixup(void) 3078 { 3079 pnv_pci_ioda_setup_nvlink(); 3080 pnv_pci_ioda_create_dbgfs(); 3081 3082 pnv_pci_enable_bridges(); 3083 3084 #ifdef CONFIG_EEH 3085 pnv_eeh_post_init(); 3086 #endif 3087 } 3088 3089 /* 3090 * Returns the alignment for I/O or memory windows for P2P 3091 * bridges. That actually depends on how PEs are segmented. 3092 * For now, we return I/O or M32 segment size for PE sensitive 3093 * P2P bridges. Otherwise, the default values (4KiB for I/O, 3094 * 1MiB for memory) will be returned. 3095 * 3096 * The current PCI bus might be put into one PE, which was 3097 * create against the parent PCI bridge. For that case, we 3098 * needn't enlarge the alignment so that we can save some 3099 * resources. 3100 */ 3101 static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus, 3102 unsigned long type) 3103 { 3104 struct pci_dev *bridge; 3105 struct pci_controller *hose = pci_bus_to_host(bus); 3106 struct pnv_phb *phb = hose->private_data; 3107 int num_pci_bridges = 0; 3108 3109 bridge = bus->self; 3110 while (bridge) { 3111 if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) { 3112 num_pci_bridges++; 3113 if (num_pci_bridges >= 2) 3114 return 1; 3115 } 3116 3117 bridge = bridge->bus->self; 3118 } 3119 3120 /* 3121 * We fall back to M32 if M64 isn't supported. We enforce the M64 3122 * alignment for any 64-bit resource, PCIe doesn't care and 3123 * bridges only do 64-bit prefetchable anyway. 3124 */ 3125 if (phb->ioda.m64_segsize && pnv_pci_is_m64_flags(type)) 3126 return phb->ioda.m64_segsize; 3127 if (type & IORESOURCE_MEM) 3128 return phb->ioda.m32_segsize; 3129 3130 return phb->ioda.io_segsize; 3131 } 3132 3133 /* 3134 * We are updating root port or the upstream port of the 3135 * bridge behind the root port with PHB's windows in order 3136 * to accommodate the changes on required resources during 3137 * PCI (slot) hotplug, which is connected to either root 3138 * port or the downstream ports of PCIe switch behind the 3139 * root port. 3140 */ 3141 static void pnv_pci_fixup_bridge_resources(struct pci_bus *bus, 3142 unsigned long type) 3143 { 3144 struct pci_controller *hose = pci_bus_to_host(bus); 3145 struct pnv_phb *phb = hose->private_data; 3146 struct pci_dev *bridge = bus->self; 3147 struct resource *r, *w; 3148 bool msi_region = false; 3149 int i; 3150 3151 /* Check if we need apply fixup to the bridge's windows */ 3152 if (!pci_is_root_bus(bridge->bus) && 3153 !pci_is_root_bus(bridge->bus->self->bus)) 3154 return; 3155 3156 /* Fixup the resources */ 3157 for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) { 3158 r = &bridge->resource[PCI_BRIDGE_RESOURCES + i]; 3159 if (!r->flags || !r->parent) 3160 continue; 3161 3162 w = NULL; 3163 if (r->flags & type & IORESOURCE_IO) 3164 w = &hose->io_resource; 3165 else if (pnv_pci_is_m64(phb, r) && 3166 (type & IORESOURCE_PREFETCH) && 3167 phb->ioda.m64_segsize) 3168 w = &hose->mem_resources[1]; 3169 else if (r->flags & type & IORESOURCE_MEM) { 3170 w = &hose->mem_resources[0]; 3171 msi_region = true; 3172 } 3173 3174 r->start = w->start; 3175 r->end = w->end; 3176 3177 /* The 64KB 32-bits MSI region shouldn't be included in 3178 * the 32-bits bridge window. Otherwise, we can see strange 3179 * issues. One of them is EEH error observed on Garrison. 3180 * 3181 * Exclude top 1MB region which is the minimal alignment of 3182 * 32-bits bridge window. 3183 */ 3184 if (msi_region) { 3185 r->end += 0x10000; 3186 r->end -= 0x100000; 3187 } 3188 } 3189 } 3190 3191 static void pnv_pci_configure_bus(struct pci_bus *bus) 3192 { 3193 struct pci_controller *hose = pci_bus_to_host(bus); 3194 struct pnv_phb *phb = hose->private_data; 3195 struct pci_dev *bridge = bus->self; 3196 struct pnv_ioda_pe *pe; 3197 bool all = (bridge && pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE); 3198 3199 dev_info(&bus->dev, "Configuring PE for bus\n"); 3200 3201 /* Don't assign PE to PCI bus, which doesn't have subordinate devices */ 3202 if (WARN_ON(list_empty(&bus->devices))) 3203 return; 3204 3205 /* Reserve PEs according to used M64 resources */ 3206 pnv_ioda_reserve_m64_pe(bus, NULL, all); 3207 3208 /* 3209 * Assign PE. We might run here because of partial hotplug. 3210 * For the case, we just pick up the existing PE and should 3211 * not allocate resources again. 3212 */ 3213 pe = pnv_ioda_setup_bus_PE(bus, all); 3214 if (!pe) 3215 return; 3216 3217 pnv_ioda_setup_pe_seg(pe); 3218 switch (phb->type) { 3219 case PNV_PHB_IODA1: 3220 pnv_pci_ioda1_setup_dma_pe(phb, pe); 3221 break; 3222 case PNV_PHB_IODA2: 3223 pnv_pci_ioda2_setup_dma_pe(phb, pe); 3224 break; 3225 default: 3226 pr_warn("%s: No DMA for PHB#%x (type %d)\n", 3227 __func__, phb->hose->global_number, phb->type); 3228 } 3229 } 3230 3231 static resource_size_t pnv_pci_default_alignment(void) 3232 { 3233 return PAGE_SIZE; 3234 } 3235 3236 #ifdef CONFIG_PCI_IOV 3237 static resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev, 3238 int resno) 3239 { 3240 struct pci_controller *hose = pci_bus_to_host(pdev->bus); 3241 struct pnv_phb *phb = hose->private_data; 3242 struct pci_dn *pdn = pci_get_pdn(pdev); 3243 resource_size_t align; 3244 3245 /* 3246 * On PowerNV platform, IOV BAR is mapped by M64 BAR to enable the 3247 * SR-IOV. While from hardware perspective, the range mapped by M64 3248 * BAR should be size aligned. 3249 * 3250 * When IOV BAR is mapped with M64 BAR in Single PE mode, the extra 3251 * powernv-specific hardware restriction is gone. But if just use the 3252 * VF BAR size as the alignment, PF BAR / VF BAR may be allocated with 3253 * in one segment of M64 #15, which introduces the PE conflict between 3254 * PF and VF. Based on this, the minimum alignment of an IOV BAR is 3255 * m64_segsize. 3256 * 3257 * This function returns the total IOV BAR size if M64 BAR is in 3258 * Shared PE mode or just VF BAR size if not. 3259 * If the M64 BAR is in Single PE mode, return the VF BAR size or 3260 * M64 segment size if IOV BAR size is less. 3261 */ 3262 align = pci_iov_resource_size(pdev, resno); 3263 if (!pdn->vfs_expanded) 3264 return align; 3265 if (pdn->m64_single_mode) 3266 return max(align, (resource_size_t)phb->ioda.m64_segsize); 3267 3268 return pdn->vfs_expanded * align; 3269 } 3270 #endif /* CONFIG_PCI_IOV */ 3271 3272 /* Prevent enabling devices for which we couldn't properly 3273 * assign a PE 3274 */ 3275 static bool pnv_pci_enable_device_hook(struct pci_dev *dev) 3276 { 3277 struct pci_controller *hose = pci_bus_to_host(dev->bus); 3278 struct pnv_phb *phb = hose->private_data; 3279 struct pci_dn *pdn; 3280 3281 /* The function is probably called while the PEs have 3282 * not be created yet. For example, resource reassignment 3283 * during PCI probe period. We just skip the check if 3284 * PEs isn't ready. 3285 */ 3286 if (!phb->initialized) 3287 return true; 3288 3289 pdn = pci_get_pdn(dev); 3290 if (!pdn || pdn->pe_number == IODA_INVALID_PE) 3291 return false; 3292 3293 return true; 3294 } 3295 3296 static bool pnv_ocapi_enable_device_hook(struct pci_dev *dev) 3297 { 3298 struct pci_controller *hose = pci_bus_to_host(dev->bus); 3299 struct pnv_phb *phb = hose->private_data; 3300 struct pci_dn *pdn; 3301 struct pnv_ioda_pe *pe; 3302 3303 if (!phb->initialized) 3304 return true; 3305 3306 pdn = pci_get_pdn(dev); 3307 if (!pdn) 3308 return false; 3309 3310 if (pdn->pe_number == IODA_INVALID_PE) { 3311 pe = pnv_ioda_setup_dev_PE(dev); 3312 if (!pe) 3313 return false; 3314 } 3315 return true; 3316 } 3317 3318 static long pnv_pci_ioda1_unset_window(struct iommu_table_group *table_group, 3319 int num) 3320 { 3321 struct pnv_ioda_pe *pe = container_of(table_group, 3322 struct pnv_ioda_pe, table_group); 3323 struct pnv_phb *phb = pe->phb; 3324 unsigned int idx; 3325 long rc; 3326 3327 pe_info(pe, "Removing DMA window #%d\n", num); 3328 for (idx = 0; idx < phb->ioda.dma32_count; idx++) { 3329 if (phb->ioda.dma32_segmap[idx] != pe->pe_number) 3330 continue; 3331 3332 rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number, 3333 idx, 0, 0ul, 0ul, 0ul); 3334 if (rc != OPAL_SUCCESS) { 3335 pe_warn(pe, "Failure %ld unmapping DMA32 segment#%d\n", 3336 rc, idx); 3337 return rc; 3338 } 3339 3340 phb->ioda.dma32_segmap[idx] = IODA_INVALID_PE; 3341 } 3342 3343 pnv_pci_unlink_table_and_group(table_group->tables[num], table_group); 3344 return OPAL_SUCCESS; 3345 } 3346 3347 static void pnv_pci_ioda1_release_pe_dma(struct pnv_ioda_pe *pe) 3348 { 3349 unsigned int weight = pnv_pci_ioda_pe_dma_weight(pe); 3350 struct iommu_table *tbl = pe->table_group.tables[0]; 3351 int64_t rc; 3352 3353 if (!weight) 3354 return; 3355 3356 rc = pnv_pci_ioda1_unset_window(&pe->table_group, 0); 3357 if (rc != OPAL_SUCCESS) 3358 return; 3359 3360 pnv_pci_p7ioc_tce_invalidate(tbl, tbl->it_offset, tbl->it_size, false); 3361 if (pe->table_group.group) { 3362 iommu_group_put(pe->table_group.group); 3363 WARN_ON(pe->table_group.group); 3364 } 3365 3366 free_pages(tbl->it_base, get_order(tbl->it_size << 3)); 3367 iommu_tce_table_put(tbl); 3368 } 3369 3370 static void pnv_pci_ioda2_release_pe_dma(struct pnv_ioda_pe *pe) 3371 { 3372 struct iommu_table *tbl = pe->table_group.tables[0]; 3373 unsigned int weight = pnv_pci_ioda_pe_dma_weight(pe); 3374 #ifdef CONFIG_IOMMU_API 3375 int64_t rc; 3376 #endif 3377 3378 if (!weight) 3379 return; 3380 3381 #ifdef CONFIG_IOMMU_API 3382 rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0); 3383 if (rc) 3384 pe_warn(pe, "OPAL error %lld release DMA window\n", rc); 3385 #endif 3386 3387 pnv_pci_ioda2_set_bypass(pe, false); 3388 if (pe->table_group.group) { 3389 iommu_group_put(pe->table_group.group); 3390 WARN_ON(pe->table_group.group); 3391 } 3392 3393 iommu_tce_table_put(tbl); 3394 } 3395 3396 static void pnv_ioda_free_pe_seg(struct pnv_ioda_pe *pe, 3397 unsigned short win, 3398 unsigned int *map) 3399 { 3400 struct pnv_phb *phb = pe->phb; 3401 int idx; 3402 int64_t rc; 3403 3404 for (idx = 0; idx < phb->ioda.total_pe_num; idx++) { 3405 if (map[idx] != pe->pe_number) 3406 continue; 3407 3408 if (win == OPAL_M64_WINDOW_TYPE) 3409 rc = opal_pci_map_pe_mmio_window(phb->opal_id, 3410 phb->ioda.reserved_pe_idx, win, 3411 idx / PNV_IODA1_M64_SEGS, 3412 idx % PNV_IODA1_M64_SEGS); 3413 else 3414 rc = opal_pci_map_pe_mmio_window(phb->opal_id, 3415 phb->ioda.reserved_pe_idx, win, 0, idx); 3416 3417 if (rc != OPAL_SUCCESS) 3418 pe_warn(pe, "Error %lld unmapping (%d) segment#%d\n", 3419 rc, win, idx); 3420 3421 map[idx] = IODA_INVALID_PE; 3422 } 3423 } 3424 3425 static void pnv_ioda_release_pe_seg(struct pnv_ioda_pe *pe) 3426 { 3427 struct pnv_phb *phb = pe->phb; 3428 3429 if (phb->type == PNV_PHB_IODA1) { 3430 pnv_ioda_free_pe_seg(pe, OPAL_IO_WINDOW_TYPE, 3431 phb->ioda.io_segmap); 3432 pnv_ioda_free_pe_seg(pe, OPAL_M32_WINDOW_TYPE, 3433 phb->ioda.m32_segmap); 3434 pnv_ioda_free_pe_seg(pe, OPAL_M64_WINDOW_TYPE, 3435 phb->ioda.m64_segmap); 3436 } else if (phb->type == PNV_PHB_IODA2) { 3437 pnv_ioda_free_pe_seg(pe, OPAL_M32_WINDOW_TYPE, 3438 phb->ioda.m32_segmap); 3439 } 3440 } 3441 3442 static void pnv_ioda_release_pe(struct pnv_ioda_pe *pe) 3443 { 3444 struct pnv_phb *phb = pe->phb; 3445 struct pnv_ioda_pe *slave, *tmp; 3446 3447 pe_info(pe, "Releasing PE\n"); 3448 3449 mutex_lock(&phb->ioda.pe_list_mutex); 3450 list_del(&pe->list); 3451 mutex_unlock(&phb->ioda.pe_list_mutex); 3452 3453 switch (phb->type) { 3454 case PNV_PHB_IODA1: 3455 pnv_pci_ioda1_release_pe_dma(pe); 3456 break; 3457 case PNV_PHB_IODA2: 3458 pnv_pci_ioda2_release_pe_dma(pe); 3459 break; 3460 case PNV_PHB_NPU_OCAPI: 3461 break; 3462 default: 3463 WARN_ON(1); 3464 } 3465 3466 pnv_ioda_release_pe_seg(pe); 3467 pnv_ioda_deconfigure_pe(pe->phb, pe); 3468 3469 /* Release slave PEs in the compound PE */ 3470 if (pe->flags & PNV_IODA_PE_MASTER) { 3471 list_for_each_entry_safe(slave, tmp, &pe->slaves, list) { 3472 list_del(&slave->list); 3473 pnv_ioda_free_pe(slave); 3474 } 3475 } 3476 3477 /* 3478 * The PE for root bus can be removed because of hotplug in EEH 3479 * recovery for fenced PHB error. We need to mark the PE dead so 3480 * that it can be populated again in PCI hot add path. The PE 3481 * shouldn't be destroyed as it's the global reserved resource. 3482 */ 3483 if (phb->ioda.root_pe_idx == pe->pe_number) 3484 return; 3485 3486 pnv_ioda_free_pe(pe); 3487 } 3488 3489 static void pnv_pci_release_device(struct pci_dev *pdev) 3490 { 3491 struct pci_controller *hose = pci_bus_to_host(pdev->bus); 3492 struct pnv_phb *phb = hose->private_data; 3493 struct pci_dn *pdn = pci_get_pdn(pdev); 3494 struct pnv_ioda_pe *pe; 3495 3496 if (pdev->is_virtfn) 3497 return; 3498 3499 if (!pdn || pdn->pe_number == IODA_INVALID_PE) 3500 return; 3501 3502 /* 3503 * PCI hotplug can happen as part of EEH error recovery. The @pdn 3504 * isn't removed and added afterwards in this scenario. We should 3505 * set the PE number in @pdn to an invalid one. Otherwise, the PE's 3506 * device count is decreased on removing devices while failing to 3507 * be increased on adding devices. It leads to unbalanced PE's device 3508 * count and eventually make normal PCI hotplug path broken. 3509 */ 3510 pe = &phb->ioda.pe_array[pdn->pe_number]; 3511 pdn->pe_number = IODA_INVALID_PE; 3512 3513 WARN_ON(--pe->device_count < 0); 3514 if (pe->device_count == 0) 3515 pnv_ioda_release_pe(pe); 3516 } 3517 3518 static void pnv_npu_disable_device(struct pci_dev *pdev) 3519 { 3520 struct eeh_dev *edev = pci_dev_to_eeh_dev(pdev); 3521 struct eeh_pe *eehpe = edev ? edev->pe : NULL; 3522 3523 if (eehpe && eeh_ops && eeh_ops->reset) 3524 eeh_ops->reset(eehpe, EEH_RESET_HOT); 3525 } 3526 3527 static void pnv_pci_ioda_shutdown(struct pci_controller *hose) 3528 { 3529 struct pnv_phb *phb = hose->private_data; 3530 3531 opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE, 3532 OPAL_ASSERT_RESET); 3533 } 3534 3535 static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus) 3536 { 3537 struct pci_controller *hose = bus->sysdata; 3538 struct pnv_phb *phb = hose->private_data; 3539 struct pnv_ioda_pe *pe; 3540 3541 list_for_each_entry(pe, &phb->ioda.pe_list, list) { 3542 if (!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))) 3543 continue; 3544 3545 if (!pe->pbus) 3546 continue; 3547 3548 if (bus->number == ((pe->rid >> 8) & 0xFF)) { 3549 pe->pbus = bus; 3550 break; 3551 } 3552 } 3553 } 3554 3555 static const struct pci_controller_ops pnv_pci_ioda_controller_ops = { 3556 .dma_dev_setup = pnv_pci_ioda_dma_dev_setup, 3557 .dma_bus_setup = pnv_pci_ioda_dma_bus_setup, 3558 .iommu_bypass_supported = pnv_pci_ioda_iommu_bypass_supported, 3559 .setup_msi_irqs = pnv_setup_msi_irqs, 3560 .teardown_msi_irqs = pnv_teardown_msi_irqs, 3561 .enable_device_hook = pnv_pci_enable_device_hook, 3562 .release_device = pnv_pci_release_device, 3563 .window_alignment = pnv_pci_window_alignment, 3564 .setup_bridge = pnv_pci_fixup_bridge_resources, 3565 .reset_secondary_bus = pnv_pci_reset_secondary_bus, 3566 .shutdown = pnv_pci_ioda_shutdown, 3567 }; 3568 3569 static const struct pci_controller_ops pnv_npu_ioda_controller_ops = { 3570 .setup_msi_irqs = pnv_setup_msi_irqs, 3571 .teardown_msi_irqs = pnv_teardown_msi_irqs, 3572 .enable_device_hook = pnv_pci_enable_device_hook, 3573 .window_alignment = pnv_pci_window_alignment, 3574 .reset_secondary_bus = pnv_pci_reset_secondary_bus, 3575 .shutdown = pnv_pci_ioda_shutdown, 3576 .disable_device = pnv_npu_disable_device, 3577 }; 3578 3579 static const struct pci_controller_ops pnv_npu_ocapi_ioda_controller_ops = { 3580 .enable_device_hook = pnv_ocapi_enable_device_hook, 3581 .release_device = pnv_pci_release_device, 3582 .window_alignment = pnv_pci_window_alignment, 3583 .reset_secondary_bus = pnv_pci_reset_secondary_bus, 3584 .shutdown = pnv_pci_ioda_shutdown, 3585 }; 3586 3587 static void __init pnv_pci_init_ioda_phb(struct device_node *np, 3588 u64 hub_id, int ioda_type) 3589 { 3590 struct pci_controller *hose; 3591 struct pnv_phb *phb; 3592 unsigned long size, m64map_off, m32map_off, pemap_off; 3593 unsigned long iomap_off = 0, dma32map_off = 0; 3594 struct pnv_ioda_pe *root_pe; 3595 struct resource r; 3596 const __be64 *prop64; 3597 const __be32 *prop32; 3598 int len; 3599 unsigned int segno; 3600 u64 phb_id; 3601 void *aux; 3602 long rc; 3603 3604 if (!of_device_is_available(np)) 3605 return; 3606 3607 pr_info("Initializing %s PHB (%pOF)\n", pnv_phb_names[ioda_type], np); 3608 3609 prop64 = of_get_property(np, "ibm,opal-phbid", NULL); 3610 if (!prop64) { 3611 pr_err(" Missing \"ibm,opal-phbid\" property !\n"); 3612 return; 3613 } 3614 phb_id = be64_to_cpup(prop64); 3615 pr_debug(" PHB-ID : 0x%016llx\n", phb_id); 3616 3617 phb = memblock_alloc(sizeof(*phb), SMP_CACHE_BYTES); 3618 if (!phb) 3619 panic("%s: Failed to allocate %zu bytes\n", __func__, 3620 sizeof(*phb)); 3621 3622 /* Allocate PCI controller */ 3623 phb->hose = hose = pcibios_alloc_controller(np); 3624 if (!phb->hose) { 3625 pr_err(" Can't allocate PCI controller for %pOF\n", 3626 np); 3627 memblock_free(__pa(phb), sizeof(struct pnv_phb)); 3628 return; 3629 } 3630 3631 spin_lock_init(&phb->lock); 3632 prop32 = of_get_property(np, "bus-range", &len); 3633 if (prop32 && len == 8) { 3634 hose->first_busno = be32_to_cpu(prop32[0]); 3635 hose->last_busno = be32_to_cpu(prop32[1]); 3636 } else { 3637 pr_warn(" Broken <bus-range> on %pOF\n", np); 3638 hose->first_busno = 0; 3639 hose->last_busno = 0xff; 3640 } 3641 hose->private_data = phb; 3642 phb->hub_id = hub_id; 3643 phb->opal_id = phb_id; 3644 phb->type = ioda_type; 3645 mutex_init(&phb->ioda.pe_alloc_mutex); 3646 3647 /* Detect specific models for error handling */ 3648 if (of_device_is_compatible(np, "ibm,p7ioc-pciex")) 3649 phb->model = PNV_PHB_MODEL_P7IOC; 3650 else if (of_device_is_compatible(np, "ibm,power8-pciex")) 3651 phb->model = PNV_PHB_MODEL_PHB3; 3652 else if (of_device_is_compatible(np, "ibm,power8-npu-pciex")) 3653 phb->model = PNV_PHB_MODEL_NPU; 3654 else if (of_device_is_compatible(np, "ibm,power9-npu-pciex")) 3655 phb->model = PNV_PHB_MODEL_NPU2; 3656 else 3657 phb->model = PNV_PHB_MODEL_UNKNOWN; 3658 3659 /* Initialize diagnostic data buffer */ 3660 prop32 = of_get_property(np, "ibm,phb-diag-data-size", NULL); 3661 if (prop32) 3662 phb->diag_data_size = be32_to_cpup(prop32); 3663 else 3664 phb->diag_data_size = PNV_PCI_DIAG_BUF_SIZE; 3665 3666 phb->diag_data = memblock_alloc(phb->diag_data_size, SMP_CACHE_BYTES); 3667 if (!phb->diag_data) 3668 panic("%s: Failed to allocate %u bytes\n", __func__, 3669 phb->diag_data_size); 3670 3671 /* Parse 32-bit and IO ranges (if any) */ 3672 pci_process_bridge_OF_ranges(hose, np, !hose->global_number); 3673 3674 /* Get registers */ 3675 if (!of_address_to_resource(np, 0, &r)) { 3676 phb->regs_phys = r.start; 3677 phb->regs = ioremap(r.start, resource_size(&r)); 3678 if (phb->regs == NULL) 3679 pr_err(" Failed to map registers !\n"); 3680 } 3681 3682 /* Initialize more IODA stuff */ 3683 phb->ioda.total_pe_num = 1; 3684 prop32 = of_get_property(np, "ibm,opal-num-pes", NULL); 3685 if (prop32) 3686 phb->ioda.total_pe_num = be32_to_cpup(prop32); 3687 prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL); 3688 if (prop32) 3689 phb->ioda.reserved_pe_idx = be32_to_cpup(prop32); 3690 3691 /* Invalidate RID to PE# mapping */ 3692 for (segno = 0; segno < ARRAY_SIZE(phb->ioda.pe_rmap); segno++) 3693 phb->ioda.pe_rmap[segno] = IODA_INVALID_PE; 3694 3695 /* Parse 64-bit MMIO range */ 3696 pnv_ioda_parse_m64_window(phb); 3697 3698 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]); 3699 /* FW Has already off top 64k of M32 space (MSI space) */ 3700 phb->ioda.m32_size += 0x10000; 3701 3702 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe_num; 3703 phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0]; 3704 phb->ioda.io_size = hose->pci_io_size; 3705 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe_num; 3706 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */ 3707 3708 /* Calculate how many 32-bit TCE segments we have */ 3709 phb->ioda.dma32_count = phb->ioda.m32_pci_base / 3710 PNV_IODA1_DMA32_SEGSIZE; 3711 3712 /* Allocate aux data & arrays. We don't have IO ports on PHB3 */ 3713 size = ALIGN(max_t(unsigned, phb->ioda.total_pe_num, 8) / 8, 3714 sizeof(unsigned long)); 3715 m64map_off = size; 3716 size += phb->ioda.total_pe_num * sizeof(phb->ioda.m64_segmap[0]); 3717 m32map_off = size; 3718 size += phb->ioda.total_pe_num * sizeof(phb->ioda.m32_segmap[0]); 3719 if (phb->type == PNV_PHB_IODA1) { 3720 iomap_off = size; 3721 size += phb->ioda.total_pe_num * sizeof(phb->ioda.io_segmap[0]); 3722 dma32map_off = size; 3723 size += phb->ioda.dma32_count * 3724 sizeof(phb->ioda.dma32_segmap[0]); 3725 } 3726 pemap_off = size; 3727 size += phb->ioda.total_pe_num * sizeof(struct pnv_ioda_pe); 3728 aux = memblock_alloc(size, SMP_CACHE_BYTES); 3729 if (!aux) 3730 panic("%s: Failed to allocate %lu bytes\n", __func__, size); 3731 phb->ioda.pe_alloc = aux; 3732 phb->ioda.m64_segmap = aux + m64map_off; 3733 phb->ioda.m32_segmap = aux + m32map_off; 3734 for (segno = 0; segno < phb->ioda.total_pe_num; segno++) { 3735 phb->ioda.m64_segmap[segno] = IODA_INVALID_PE; 3736 phb->ioda.m32_segmap[segno] = IODA_INVALID_PE; 3737 } 3738 if (phb->type == PNV_PHB_IODA1) { 3739 phb->ioda.io_segmap = aux + iomap_off; 3740 for (segno = 0; segno < phb->ioda.total_pe_num; segno++) 3741 phb->ioda.io_segmap[segno] = IODA_INVALID_PE; 3742 3743 phb->ioda.dma32_segmap = aux + dma32map_off; 3744 for (segno = 0; segno < phb->ioda.dma32_count; segno++) 3745 phb->ioda.dma32_segmap[segno] = IODA_INVALID_PE; 3746 } 3747 phb->ioda.pe_array = aux + pemap_off; 3748 3749 /* 3750 * Choose PE number for root bus, which shouldn't have 3751 * M64 resources consumed by its child devices. To pick 3752 * the PE number adjacent to the reserved one if possible. 3753 */ 3754 pnv_ioda_reserve_pe(phb, phb->ioda.reserved_pe_idx); 3755 if (phb->ioda.reserved_pe_idx == 0) { 3756 phb->ioda.root_pe_idx = 1; 3757 pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx); 3758 } else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1)) { 3759 phb->ioda.root_pe_idx = phb->ioda.reserved_pe_idx - 1; 3760 pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx); 3761 } else { 3762 /* otherwise just allocate one */ 3763 root_pe = pnv_ioda_alloc_pe(phb); 3764 phb->ioda.root_pe_idx = root_pe->pe_number; 3765 } 3766 3767 INIT_LIST_HEAD(&phb->ioda.pe_list); 3768 mutex_init(&phb->ioda.pe_list_mutex); 3769 3770 /* Calculate how many 32-bit TCE segments we have */ 3771 phb->ioda.dma32_count = phb->ioda.m32_pci_base / 3772 PNV_IODA1_DMA32_SEGSIZE; 3773 3774 #if 0 /* We should really do that ... */ 3775 rc = opal_pci_set_phb_mem_window(opal->phb_id, 3776 window_type, 3777 window_num, 3778 starting_real_address, 3779 starting_pci_address, 3780 segment_size); 3781 #endif 3782 3783 pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n", 3784 phb->ioda.total_pe_num, phb->ioda.reserved_pe_idx, 3785 phb->ioda.m32_size, phb->ioda.m32_segsize); 3786 if (phb->ioda.m64_size) 3787 pr_info(" M64: 0x%lx [segment=0x%lx]\n", 3788 phb->ioda.m64_size, phb->ioda.m64_segsize); 3789 if (phb->ioda.io_size) 3790 pr_info(" IO: 0x%x [segment=0x%x]\n", 3791 phb->ioda.io_size, phb->ioda.io_segsize); 3792 3793 3794 phb->hose->ops = &pnv_pci_ops; 3795 phb->get_pe_state = pnv_ioda_get_pe_state; 3796 phb->freeze_pe = pnv_ioda_freeze_pe; 3797 phb->unfreeze_pe = pnv_ioda_unfreeze_pe; 3798 3799 /* Setup MSI support */ 3800 pnv_pci_init_ioda_msis(phb); 3801 3802 /* 3803 * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here 3804 * to let the PCI core do resource assignment. It's supposed 3805 * that the PCI core will do correct I/O and MMIO alignment 3806 * for the P2P bridge bars so that each PCI bus (excluding 3807 * the child P2P bridges) can form individual PE. 3808 */ 3809 ppc_md.pcibios_fixup = pnv_pci_ioda_fixup; 3810 3811 switch (phb->type) { 3812 case PNV_PHB_NPU_NVLINK: 3813 hose->controller_ops = pnv_npu_ioda_controller_ops; 3814 break; 3815 case PNV_PHB_NPU_OCAPI: 3816 hose->controller_ops = pnv_npu_ocapi_ioda_controller_ops; 3817 break; 3818 default: 3819 hose->controller_ops = pnv_pci_ioda_controller_ops; 3820 } 3821 3822 ppc_md.pcibios_default_alignment = pnv_pci_default_alignment; 3823 3824 #ifdef CONFIG_PCI_IOV 3825 ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov; 3826 ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment; 3827 ppc_md.pcibios_sriov_enable = pnv_pcibios_sriov_enable; 3828 ppc_md.pcibios_sriov_disable = pnv_pcibios_sriov_disable; 3829 #endif 3830 3831 pci_add_flags(PCI_REASSIGN_ALL_RSRC); 3832 3833 /* Reset IODA tables to a clean state */ 3834 rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET); 3835 if (rc) 3836 pr_warn(" OPAL Error %ld performing IODA table reset !\n", rc); 3837 3838 /* 3839 * If we're running in kdump kernel, the previous kernel never 3840 * shutdown PCI devices correctly. We already got IODA table 3841 * cleaned out. So we have to issue PHB reset to stop all PCI 3842 * transactions from previous kernel. The ppc_pci_reset_phbs 3843 * kernel parameter will force this reset too. Additionally, 3844 * if the IODA reset above failed then use a bigger hammer. 3845 * This can happen if we get a PHB fatal error in very early 3846 * boot. 3847 */ 3848 if (is_kdump_kernel() || pci_reset_phbs || rc) { 3849 pr_info(" Issue PHB reset ...\n"); 3850 pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL); 3851 pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE); 3852 } 3853 3854 /* Remove M64 resource if we can't configure it successfully */ 3855 if (!phb->init_m64 || phb->init_m64(phb)) 3856 hose->mem_resources[1].flags = 0; 3857 } 3858 3859 void __init pnv_pci_init_ioda2_phb(struct device_node *np) 3860 { 3861 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2); 3862 } 3863 3864 void __init pnv_pci_init_npu_phb(struct device_node *np) 3865 { 3866 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_NPU_NVLINK); 3867 } 3868 3869 void __init pnv_pci_init_npu2_opencapi_phb(struct device_node *np) 3870 { 3871 pnv_pci_init_ioda_phb(np, 0, PNV_PHB_NPU_OCAPI); 3872 } 3873 3874 static void pnv_npu2_opencapi_cfg_size_fixup(struct pci_dev *dev) 3875 { 3876 struct pci_controller *hose = pci_bus_to_host(dev->bus); 3877 struct pnv_phb *phb = hose->private_data; 3878 3879 if (!machine_is(powernv)) 3880 return; 3881 3882 if (phb->type == PNV_PHB_NPU_OCAPI) 3883 dev->cfg_size = PCI_CFG_SPACE_EXP_SIZE; 3884 } 3885 DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, pnv_npu2_opencapi_cfg_size_fixup); 3886 3887 void __init pnv_pci_init_ioda_hub(struct device_node *np) 3888 { 3889 struct device_node *phbn; 3890 const __be64 *prop64; 3891 u64 hub_id; 3892 3893 pr_info("Probing IODA IO-Hub %pOF\n", np); 3894 3895 prop64 = of_get_property(np, "ibm,opal-hubid", NULL); 3896 if (!prop64) { 3897 pr_err(" Missing \"ibm,opal-hubid\" property !\n"); 3898 return; 3899 } 3900 hub_id = be64_to_cpup(prop64); 3901 pr_devel(" HUB-ID : 0x%016llx\n", hub_id); 3902 3903 /* Count child PHBs */ 3904 for_each_child_of_node(np, phbn) { 3905 /* Look for IODA1 PHBs */ 3906 if (of_device_is_compatible(phbn, "ibm,ioda-phb")) 3907 pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1); 3908 } 3909 } 3910