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