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