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