1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation 4 * 5 * Rewrite, cleanup: 6 * 7 * Copyright (C) 2004 Olof Johansson <olof@lixom.net>, IBM Corporation 8 * Copyright (C) 2006 Olof Johansson <olof@lixom.net> 9 * 10 * Dynamic DMA mapping support, pSeries-specific parts, both SMP and LPAR. 11 */ 12 13 #include <linux/init.h> 14 #include <linux/types.h> 15 #include <linux/slab.h> 16 #include <linux/mm.h> 17 #include <linux/memblock.h> 18 #include <linux/spinlock.h> 19 #include <linux/string.h> 20 #include <linux/pci.h> 21 #include <linux/dma-mapping.h> 22 #include <linux/crash_dump.h> 23 #include <linux/memory.h> 24 #include <linux/of.h> 25 #include <linux/of_address.h> 26 #include <linux/iommu.h> 27 #include <linux/rculist.h> 28 #include <asm/io.h> 29 #include <asm/prom.h> 30 #include <asm/rtas.h> 31 #include <asm/iommu.h> 32 #include <asm/pci-bridge.h> 33 #include <asm/machdep.h> 34 #include <asm/firmware.h> 35 #include <asm/tce.h> 36 #include <asm/ppc-pci.h> 37 #include <asm/udbg.h> 38 #include <asm/mmzone.h> 39 #include <asm/plpar_wrappers.h> 40 41 #include "pseries.h" 42 43 enum { 44 DDW_QUERY_PE_DMA_WIN = 0, 45 DDW_CREATE_PE_DMA_WIN = 1, 46 DDW_REMOVE_PE_DMA_WIN = 2, 47 48 DDW_APPLICABLE_SIZE 49 }; 50 51 enum { 52 DDW_EXT_SIZE = 0, 53 DDW_EXT_RESET_DMA_WIN = 1, 54 DDW_EXT_QUERY_OUT_SIZE = 2 55 }; 56 57 static struct iommu_table *iommu_pseries_alloc_table(int node) 58 { 59 struct iommu_table *tbl; 60 61 tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, node); 62 if (!tbl) 63 return NULL; 64 65 INIT_LIST_HEAD_RCU(&tbl->it_group_list); 66 kref_init(&tbl->it_kref); 67 return tbl; 68 } 69 70 static struct iommu_table_group *iommu_pseries_alloc_group(int node) 71 { 72 struct iommu_table_group *table_group; 73 74 table_group = kzalloc_node(sizeof(*table_group), GFP_KERNEL, node); 75 if (!table_group) 76 return NULL; 77 78 #ifdef CONFIG_IOMMU_API 79 table_group->ops = &spapr_tce_table_group_ops; 80 table_group->pgsizes = SZ_4K; 81 #endif 82 83 table_group->tables[0] = iommu_pseries_alloc_table(node); 84 if (table_group->tables[0]) 85 return table_group; 86 87 kfree(table_group); 88 return NULL; 89 } 90 91 static void iommu_pseries_free_group(struct iommu_table_group *table_group, 92 const char *node_name) 93 { 94 struct iommu_table *tbl; 95 96 if (!table_group) 97 return; 98 99 tbl = table_group->tables[0]; 100 #ifdef CONFIG_IOMMU_API 101 if (table_group->group) { 102 iommu_group_put(table_group->group); 103 BUG_ON(table_group->group); 104 } 105 #endif 106 iommu_tce_table_put(tbl); 107 108 kfree(table_group); 109 } 110 111 static int tce_build_pSeries(struct iommu_table *tbl, long index, 112 long npages, unsigned long uaddr, 113 enum dma_data_direction direction, 114 unsigned long attrs) 115 { 116 u64 proto_tce; 117 __be64 *tcep; 118 u64 rpn; 119 const unsigned long tceshift = tbl->it_page_shift; 120 const unsigned long pagesize = IOMMU_PAGE_SIZE(tbl); 121 122 proto_tce = TCE_PCI_READ; // Read allowed 123 124 if (direction != DMA_TO_DEVICE) 125 proto_tce |= TCE_PCI_WRITE; 126 127 tcep = ((__be64 *)tbl->it_base) + index; 128 129 while (npages--) { 130 /* can't move this out since we might cross MEMBLOCK boundary */ 131 rpn = __pa(uaddr) >> tceshift; 132 *tcep = cpu_to_be64(proto_tce | rpn << tceshift); 133 134 uaddr += pagesize; 135 tcep++; 136 } 137 return 0; 138 } 139 140 141 static void tce_free_pSeries(struct iommu_table *tbl, long index, long npages) 142 { 143 __be64 *tcep; 144 145 tcep = ((__be64 *)tbl->it_base) + index; 146 147 while (npages--) 148 *(tcep++) = 0; 149 } 150 151 static unsigned long tce_get_pseries(struct iommu_table *tbl, long index) 152 { 153 __be64 *tcep; 154 155 tcep = ((__be64 *)tbl->it_base) + index; 156 157 return be64_to_cpu(*tcep); 158 } 159 160 static void tce_free_pSeriesLP(unsigned long liobn, long, long, long); 161 static void tce_freemulti_pSeriesLP(struct iommu_table*, long, long); 162 163 static int tce_build_pSeriesLP(unsigned long liobn, long tcenum, long tceshift, 164 long npages, unsigned long uaddr, 165 enum dma_data_direction direction, 166 unsigned long attrs) 167 { 168 u64 rc = 0; 169 u64 proto_tce, tce; 170 u64 rpn; 171 int ret = 0; 172 long tcenum_start = tcenum, npages_start = npages; 173 174 rpn = __pa(uaddr) >> tceshift; 175 proto_tce = TCE_PCI_READ; 176 if (direction != DMA_TO_DEVICE) 177 proto_tce |= TCE_PCI_WRITE; 178 179 while (npages--) { 180 tce = proto_tce | rpn << tceshift; 181 rc = plpar_tce_put((u64)liobn, (u64)tcenum << tceshift, tce); 182 183 if (unlikely(rc == H_NOT_ENOUGH_RESOURCES)) { 184 ret = (int)rc; 185 tce_free_pSeriesLP(liobn, tcenum_start, tceshift, 186 (npages_start - (npages + 1))); 187 break; 188 } 189 190 if (rc && printk_ratelimit()) { 191 printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc); 192 printk("\tindex = 0x%llx\n", (u64)liobn); 193 printk("\ttcenum = 0x%llx\n", (u64)tcenum); 194 printk("\ttce val = 0x%llx\n", tce ); 195 dump_stack(); 196 } 197 198 tcenum++; 199 rpn++; 200 } 201 return ret; 202 } 203 204 static DEFINE_PER_CPU(__be64 *, tce_page); 205 206 static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum, 207 long npages, unsigned long uaddr, 208 enum dma_data_direction direction, 209 unsigned long attrs) 210 { 211 u64 rc = 0; 212 u64 proto_tce; 213 __be64 *tcep; 214 u64 rpn; 215 long l, limit; 216 long tcenum_start = tcenum, npages_start = npages; 217 int ret = 0; 218 unsigned long flags; 219 const unsigned long tceshift = tbl->it_page_shift; 220 221 if ((npages == 1) || !firmware_has_feature(FW_FEATURE_PUT_TCE_IND)) { 222 return tce_build_pSeriesLP(tbl->it_index, tcenum, 223 tceshift, npages, uaddr, 224 direction, attrs); 225 } 226 227 local_irq_save(flags); /* to protect tcep and the page behind it */ 228 229 tcep = __this_cpu_read(tce_page); 230 231 /* This is safe to do since interrupts are off when we're called 232 * from iommu_alloc{,_sg}() 233 */ 234 if (!tcep) { 235 tcep = (__be64 *)__get_free_page(GFP_ATOMIC); 236 /* If allocation fails, fall back to the loop implementation */ 237 if (!tcep) { 238 local_irq_restore(flags); 239 return tce_build_pSeriesLP(tbl->it_index, tcenum, 240 tceshift, 241 npages, uaddr, direction, attrs); 242 } 243 __this_cpu_write(tce_page, tcep); 244 } 245 246 rpn = __pa(uaddr) >> tceshift; 247 proto_tce = TCE_PCI_READ; 248 if (direction != DMA_TO_DEVICE) 249 proto_tce |= TCE_PCI_WRITE; 250 251 /* We can map max one pageful of TCEs at a time */ 252 do { 253 /* 254 * Set up the page with TCE data, looping through and setting 255 * the values. 256 */ 257 limit = min_t(long, npages, 4096 / TCE_ENTRY_SIZE); 258 259 for (l = 0; l < limit; l++) { 260 tcep[l] = cpu_to_be64(proto_tce | rpn << tceshift); 261 rpn++; 262 } 263 264 rc = plpar_tce_put_indirect((u64)tbl->it_index, 265 (u64)tcenum << tceshift, 266 (u64)__pa(tcep), 267 limit); 268 269 npages -= limit; 270 tcenum += limit; 271 } while (npages > 0 && !rc); 272 273 local_irq_restore(flags); 274 275 if (unlikely(rc == H_NOT_ENOUGH_RESOURCES)) { 276 ret = (int)rc; 277 tce_freemulti_pSeriesLP(tbl, tcenum_start, 278 (npages_start - (npages + limit))); 279 return ret; 280 } 281 282 if (rc && printk_ratelimit()) { 283 printk("tce_buildmulti_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc); 284 printk("\tindex = 0x%llx\n", (u64)tbl->it_index); 285 printk("\tnpages = 0x%llx\n", (u64)npages); 286 printk("\ttce[0] val = 0x%llx\n", tcep[0]); 287 dump_stack(); 288 } 289 return ret; 290 } 291 292 static void tce_free_pSeriesLP(unsigned long liobn, long tcenum, long tceshift, 293 long npages) 294 { 295 u64 rc; 296 297 while (npages--) { 298 rc = plpar_tce_put((u64)liobn, (u64)tcenum << tceshift, 0); 299 300 if (rc && printk_ratelimit()) { 301 printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc); 302 printk("\tindex = 0x%llx\n", (u64)liobn); 303 printk("\ttcenum = 0x%llx\n", (u64)tcenum); 304 dump_stack(); 305 } 306 307 tcenum++; 308 } 309 } 310 311 312 static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages) 313 { 314 u64 rc; 315 316 if (!firmware_has_feature(FW_FEATURE_STUFF_TCE)) 317 return tce_free_pSeriesLP(tbl->it_index, tcenum, 318 tbl->it_page_shift, npages); 319 320 rc = plpar_tce_stuff((u64)tbl->it_index, 321 (u64)tcenum << tbl->it_page_shift, 0, npages); 322 323 if (rc && printk_ratelimit()) { 324 printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n"); 325 printk("\trc = %lld\n", rc); 326 printk("\tindex = 0x%llx\n", (u64)tbl->it_index); 327 printk("\tnpages = 0x%llx\n", (u64)npages); 328 dump_stack(); 329 } 330 } 331 332 static unsigned long tce_get_pSeriesLP(struct iommu_table *tbl, long tcenum) 333 { 334 u64 rc; 335 unsigned long tce_ret; 336 337 rc = plpar_tce_get((u64)tbl->it_index, 338 (u64)tcenum << tbl->it_page_shift, &tce_ret); 339 340 if (rc && printk_ratelimit()) { 341 printk("tce_get_pSeriesLP: plpar_tce_get failed. rc=%lld\n", rc); 342 printk("\tindex = 0x%llx\n", (u64)tbl->it_index); 343 printk("\ttcenum = 0x%llx\n", (u64)tcenum); 344 dump_stack(); 345 } 346 347 return tce_ret; 348 } 349 350 /* this is compatible with cells for the device tree property */ 351 struct dynamic_dma_window_prop { 352 __be32 liobn; /* tce table number */ 353 __be64 dma_base; /* address hi,lo */ 354 __be32 tce_shift; /* ilog2(tce_page_size) */ 355 __be32 window_shift; /* ilog2(tce_window_size) */ 356 }; 357 358 struct dma_win { 359 struct device_node *device; 360 const struct dynamic_dma_window_prop *prop; 361 struct list_head list; 362 }; 363 364 /* Dynamic DMA Window support */ 365 struct ddw_query_response { 366 u32 windows_available; 367 u64 largest_available_block; 368 u32 page_size; 369 u32 migration_capable; 370 }; 371 372 struct ddw_create_response { 373 u32 liobn; 374 u32 addr_hi; 375 u32 addr_lo; 376 }; 377 378 static LIST_HEAD(dma_win_list); 379 /* prevents races between memory on/offline and window creation */ 380 static DEFINE_SPINLOCK(dma_win_list_lock); 381 /* protects initializing window twice for same device */ 382 static DEFINE_MUTEX(dma_win_init_mutex); 383 #define DIRECT64_PROPNAME "linux,direct64-ddr-window-info" 384 #define DMA64_PROPNAME "linux,dma64-ddr-window-info" 385 386 static int tce_clearrange_multi_pSeriesLP(unsigned long start_pfn, 387 unsigned long num_pfn, const void *arg) 388 { 389 const struct dynamic_dma_window_prop *maprange = arg; 390 int rc; 391 u64 tce_size, num_tce, dma_offset, next; 392 u32 tce_shift; 393 long limit; 394 395 tce_shift = be32_to_cpu(maprange->tce_shift); 396 tce_size = 1ULL << tce_shift; 397 next = start_pfn << PAGE_SHIFT; 398 num_tce = num_pfn << PAGE_SHIFT; 399 400 /* round back to the beginning of the tce page size */ 401 num_tce += next & (tce_size - 1); 402 next &= ~(tce_size - 1); 403 404 /* covert to number of tces */ 405 num_tce |= tce_size - 1; 406 num_tce >>= tce_shift; 407 408 do { 409 /* 410 * Set up the page with TCE data, looping through and setting 411 * the values. 412 */ 413 limit = min_t(long, num_tce, 512); 414 dma_offset = next + be64_to_cpu(maprange->dma_base); 415 416 rc = plpar_tce_stuff((u64)be32_to_cpu(maprange->liobn), 417 dma_offset, 418 0, limit); 419 next += limit * tce_size; 420 num_tce -= limit; 421 } while (num_tce > 0 && !rc); 422 423 return rc; 424 } 425 426 static int tce_setrange_multi_pSeriesLP(unsigned long start_pfn, 427 unsigned long num_pfn, const void *arg) 428 { 429 const struct dynamic_dma_window_prop *maprange = arg; 430 u64 tce_size, num_tce, dma_offset, next, proto_tce, liobn; 431 __be64 *tcep; 432 u32 tce_shift; 433 u64 rc = 0; 434 long l, limit; 435 436 if (!firmware_has_feature(FW_FEATURE_PUT_TCE_IND)) { 437 unsigned long tceshift = be32_to_cpu(maprange->tce_shift); 438 unsigned long dmastart = (start_pfn << PAGE_SHIFT) + 439 be64_to_cpu(maprange->dma_base); 440 unsigned long tcenum = dmastart >> tceshift; 441 unsigned long npages = num_pfn << PAGE_SHIFT >> tceshift; 442 void *uaddr = __va(start_pfn << PAGE_SHIFT); 443 444 return tce_build_pSeriesLP(be32_to_cpu(maprange->liobn), 445 tcenum, tceshift, npages, (unsigned long) uaddr, 446 DMA_BIDIRECTIONAL, 0); 447 } 448 449 local_irq_disable(); /* to protect tcep and the page behind it */ 450 tcep = __this_cpu_read(tce_page); 451 452 if (!tcep) { 453 tcep = (__be64 *)__get_free_page(GFP_ATOMIC); 454 if (!tcep) { 455 local_irq_enable(); 456 return -ENOMEM; 457 } 458 __this_cpu_write(tce_page, tcep); 459 } 460 461 proto_tce = TCE_PCI_READ | TCE_PCI_WRITE; 462 463 liobn = (u64)be32_to_cpu(maprange->liobn); 464 tce_shift = be32_to_cpu(maprange->tce_shift); 465 tce_size = 1ULL << tce_shift; 466 next = start_pfn << PAGE_SHIFT; 467 num_tce = num_pfn << PAGE_SHIFT; 468 469 /* round back to the beginning of the tce page size */ 470 num_tce += next & (tce_size - 1); 471 next &= ~(tce_size - 1); 472 473 /* covert to number of tces */ 474 num_tce |= tce_size - 1; 475 num_tce >>= tce_shift; 476 477 /* We can map max one pageful of TCEs at a time */ 478 do { 479 /* 480 * Set up the page with TCE data, looping through and setting 481 * the values. 482 */ 483 limit = min_t(long, num_tce, 4096 / TCE_ENTRY_SIZE); 484 dma_offset = next + be64_to_cpu(maprange->dma_base); 485 486 for (l = 0; l < limit; l++) { 487 tcep[l] = cpu_to_be64(proto_tce | next); 488 next += tce_size; 489 } 490 491 rc = plpar_tce_put_indirect(liobn, 492 dma_offset, 493 (u64)__pa(tcep), 494 limit); 495 496 num_tce -= limit; 497 } while (num_tce > 0 && !rc); 498 499 /* error cleanup: caller will clear whole range */ 500 501 local_irq_enable(); 502 return rc; 503 } 504 505 static int tce_setrange_multi_pSeriesLP_walk(unsigned long start_pfn, 506 unsigned long num_pfn, void *arg) 507 { 508 return tce_setrange_multi_pSeriesLP(start_pfn, num_pfn, arg); 509 } 510 511 static void iommu_table_setparms_common(struct iommu_table *tbl, unsigned long busno, 512 unsigned long liobn, unsigned long win_addr, 513 unsigned long window_size, unsigned long page_shift, 514 void *base, struct iommu_table_ops *table_ops) 515 { 516 tbl->it_busno = busno; 517 tbl->it_index = liobn; 518 tbl->it_offset = win_addr >> page_shift; 519 tbl->it_size = window_size >> page_shift; 520 tbl->it_page_shift = page_shift; 521 tbl->it_base = (unsigned long)base; 522 tbl->it_blocksize = 16; 523 tbl->it_type = TCE_PCI; 524 tbl->it_ops = table_ops; 525 } 526 527 struct iommu_table_ops iommu_table_pseries_ops; 528 529 static void iommu_table_setparms(struct pci_controller *phb, 530 struct device_node *dn, 531 struct iommu_table *tbl) 532 { 533 struct device_node *node; 534 const unsigned long *basep; 535 const u32 *sizep; 536 537 /* Test if we are going over 2GB of DMA space */ 538 if (phb->dma_window_base_cur + phb->dma_window_size > SZ_2G) { 539 udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n"); 540 panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n"); 541 } 542 543 node = phb->dn; 544 basep = of_get_property(node, "linux,tce-base", NULL); 545 sizep = of_get_property(node, "linux,tce-size", NULL); 546 if (basep == NULL || sizep == NULL) { 547 printk(KERN_ERR "PCI_DMA: iommu_table_setparms: %pOF has " 548 "missing tce entries !\n", dn); 549 return; 550 } 551 552 iommu_table_setparms_common(tbl, phb->bus->number, 0, phb->dma_window_base_cur, 553 phb->dma_window_size, IOMMU_PAGE_SHIFT_4K, 554 __va(*basep), &iommu_table_pseries_ops); 555 556 if (!is_kdump_kernel()) 557 memset((void *)tbl->it_base, 0, *sizep); 558 559 phb->dma_window_base_cur += phb->dma_window_size; 560 } 561 562 struct iommu_table_ops iommu_table_lpar_multi_ops; 563 564 /* 565 * iommu_table_setparms_lpar 566 * 567 * Function: On pSeries LPAR systems, return TCE table info, given a pci bus. 568 */ 569 static void iommu_table_setparms_lpar(struct pci_controller *phb, 570 struct device_node *dn, 571 struct iommu_table *tbl, 572 struct iommu_table_group *table_group, 573 const __be32 *dma_window) 574 { 575 unsigned long offset, size, liobn; 576 577 of_parse_dma_window(dn, dma_window, &liobn, &offset, &size); 578 579 iommu_table_setparms_common(tbl, phb->bus->number, liobn, offset, size, IOMMU_PAGE_SHIFT_4K, NULL, 580 &iommu_table_lpar_multi_ops); 581 582 583 table_group->tce32_start = offset; 584 table_group->tce32_size = size; 585 } 586 587 struct iommu_table_ops iommu_table_pseries_ops = { 588 .set = tce_build_pSeries, 589 .clear = tce_free_pSeries, 590 .get = tce_get_pseries 591 }; 592 593 static void pci_dma_bus_setup_pSeries(struct pci_bus *bus) 594 { 595 struct device_node *dn; 596 struct iommu_table *tbl; 597 struct device_node *isa_dn, *isa_dn_orig; 598 struct device_node *tmp; 599 struct pci_dn *pci; 600 int children; 601 602 dn = pci_bus_to_OF_node(bus); 603 604 pr_debug("pci_dma_bus_setup_pSeries: setting up bus %pOF\n", dn); 605 606 if (bus->self) { 607 /* This is not a root bus, any setup will be done for the 608 * device-side of the bridge in iommu_dev_setup_pSeries(). 609 */ 610 return; 611 } 612 pci = PCI_DN(dn); 613 614 /* Check if the ISA bus on the system is under 615 * this PHB. 616 */ 617 isa_dn = isa_dn_orig = of_find_node_by_type(NULL, "isa"); 618 619 while (isa_dn && isa_dn != dn) 620 isa_dn = isa_dn->parent; 621 622 of_node_put(isa_dn_orig); 623 624 /* Count number of direct PCI children of the PHB. */ 625 for (children = 0, tmp = dn->child; tmp; tmp = tmp->sibling) 626 children++; 627 628 pr_debug("Children: %d\n", children); 629 630 /* Calculate amount of DMA window per slot. Each window must be 631 * a power of two (due to pci_alloc_consistent requirements). 632 * 633 * Keep 256MB aside for PHBs with ISA. 634 */ 635 636 if (!isa_dn) { 637 /* No ISA/IDE - just set window size and return */ 638 pci->phb->dma_window_size = 0x80000000ul; /* To be divided */ 639 640 while (pci->phb->dma_window_size * children > 0x80000000ul) 641 pci->phb->dma_window_size >>= 1; 642 pr_debug("No ISA/IDE, window size is 0x%llx\n", 643 pci->phb->dma_window_size); 644 pci->phb->dma_window_base_cur = 0; 645 646 return; 647 } 648 649 /* If we have ISA, then we probably have an IDE 650 * controller too. Allocate a 128MB table but 651 * skip the first 128MB to avoid stepping on ISA 652 * space. 653 */ 654 pci->phb->dma_window_size = 0x8000000ul; 655 pci->phb->dma_window_base_cur = 0x8000000ul; 656 657 pci->table_group = iommu_pseries_alloc_group(pci->phb->node); 658 tbl = pci->table_group->tables[0]; 659 660 iommu_table_setparms(pci->phb, dn, tbl); 661 662 if (!iommu_init_table(tbl, pci->phb->node, 0, 0)) 663 panic("Failed to initialize iommu table"); 664 665 /* Divide the rest (1.75GB) among the children */ 666 pci->phb->dma_window_size = 0x80000000ul; 667 while (pci->phb->dma_window_size * children > 0x70000000ul) 668 pci->phb->dma_window_size >>= 1; 669 670 pr_debug("ISA/IDE, window size is 0x%llx\n", pci->phb->dma_window_size); 671 } 672 673 #ifdef CONFIG_IOMMU_API 674 static int tce_exchange_pseries(struct iommu_table *tbl, long index, unsigned 675 long *tce, enum dma_data_direction *direction) 676 { 677 long rc; 678 unsigned long ioba = (unsigned long) index << tbl->it_page_shift; 679 unsigned long flags, oldtce = 0; 680 u64 proto_tce = iommu_direction_to_tce_perm(*direction); 681 unsigned long newtce = *tce | proto_tce; 682 683 spin_lock_irqsave(&tbl->large_pool.lock, flags); 684 685 rc = plpar_tce_get((u64)tbl->it_index, ioba, &oldtce); 686 if (!rc) 687 rc = plpar_tce_put((u64)tbl->it_index, ioba, newtce); 688 689 if (!rc) { 690 *direction = iommu_tce_direction(oldtce); 691 *tce = oldtce & ~(TCE_PCI_READ | TCE_PCI_WRITE); 692 } 693 694 spin_unlock_irqrestore(&tbl->large_pool.lock, flags); 695 696 return rc; 697 } 698 #endif 699 700 struct iommu_table_ops iommu_table_lpar_multi_ops = { 701 .set = tce_buildmulti_pSeriesLP, 702 #ifdef CONFIG_IOMMU_API 703 .xchg_no_kill = tce_exchange_pseries, 704 #endif 705 .clear = tce_freemulti_pSeriesLP, 706 .get = tce_get_pSeriesLP 707 }; 708 709 /* 710 * Find nearest ibm,dma-window (default DMA window) or direct DMA window or 711 * dynamic 64bit DMA window, walking up the device tree. 712 */ 713 static struct device_node *pci_dma_find(struct device_node *dn, 714 const __be32 **dma_window) 715 { 716 const __be32 *dw = NULL; 717 718 for ( ; dn && PCI_DN(dn); dn = dn->parent) { 719 dw = of_get_property(dn, "ibm,dma-window", NULL); 720 if (dw) { 721 if (dma_window) 722 *dma_window = dw; 723 return dn; 724 } 725 dw = of_get_property(dn, DIRECT64_PROPNAME, NULL); 726 if (dw) 727 return dn; 728 dw = of_get_property(dn, DMA64_PROPNAME, NULL); 729 if (dw) 730 return dn; 731 } 732 733 return NULL; 734 } 735 736 static void pci_dma_bus_setup_pSeriesLP(struct pci_bus *bus) 737 { 738 struct iommu_table *tbl; 739 struct device_node *dn, *pdn; 740 struct pci_dn *ppci; 741 const __be32 *dma_window = NULL; 742 743 dn = pci_bus_to_OF_node(bus); 744 745 pr_debug("pci_dma_bus_setup_pSeriesLP: setting up bus %pOF\n", 746 dn); 747 748 pdn = pci_dma_find(dn, &dma_window); 749 750 if (dma_window == NULL) 751 pr_debug(" no ibm,dma-window property !\n"); 752 753 ppci = PCI_DN(pdn); 754 755 pr_debug(" parent is %pOF, iommu_table: 0x%p\n", 756 pdn, ppci->table_group); 757 758 if (!ppci->table_group) { 759 ppci->table_group = iommu_pseries_alloc_group(ppci->phb->node); 760 tbl = ppci->table_group->tables[0]; 761 if (dma_window) { 762 iommu_table_setparms_lpar(ppci->phb, pdn, tbl, 763 ppci->table_group, dma_window); 764 765 if (!iommu_init_table(tbl, ppci->phb->node, 0, 0)) 766 panic("Failed to initialize iommu table"); 767 } 768 iommu_register_group(ppci->table_group, 769 pci_domain_nr(bus), 0); 770 pr_debug(" created table: %p\n", ppci->table_group); 771 } 772 } 773 774 775 static void pci_dma_dev_setup_pSeries(struct pci_dev *dev) 776 { 777 struct device_node *dn; 778 struct iommu_table *tbl; 779 780 pr_debug("pci_dma_dev_setup_pSeries: %s\n", pci_name(dev)); 781 782 dn = dev->dev.of_node; 783 784 /* If we're the direct child of a root bus, then we need to allocate 785 * an iommu table ourselves. The bus setup code should have setup 786 * the window sizes already. 787 */ 788 if (!dev->bus->self) { 789 struct pci_controller *phb = PCI_DN(dn)->phb; 790 791 pr_debug(" --> first child, no bridge. Allocating iommu table.\n"); 792 PCI_DN(dn)->table_group = iommu_pseries_alloc_group(phb->node); 793 tbl = PCI_DN(dn)->table_group->tables[0]; 794 iommu_table_setparms(phb, dn, tbl); 795 796 if (!iommu_init_table(tbl, phb->node, 0, 0)) 797 panic("Failed to initialize iommu table"); 798 799 set_iommu_table_base(&dev->dev, tbl); 800 return; 801 } 802 803 /* If this device is further down the bus tree, search upwards until 804 * an already allocated iommu table is found and use that. 805 */ 806 807 while (dn && PCI_DN(dn) && PCI_DN(dn)->table_group == NULL) 808 dn = dn->parent; 809 810 if (dn && PCI_DN(dn)) 811 set_iommu_table_base(&dev->dev, 812 PCI_DN(dn)->table_group->tables[0]); 813 else 814 printk(KERN_WARNING "iommu: Device %s has no iommu table\n", 815 pci_name(dev)); 816 } 817 818 static int __read_mostly disable_ddw; 819 820 static int __init disable_ddw_setup(char *str) 821 { 822 disable_ddw = 1; 823 printk(KERN_INFO "ppc iommu: disabling ddw.\n"); 824 825 return 0; 826 } 827 828 early_param("disable_ddw", disable_ddw_setup); 829 830 static void clean_dma_window(struct device_node *np, struct dynamic_dma_window_prop *dwp) 831 { 832 int ret; 833 834 ret = tce_clearrange_multi_pSeriesLP(0, 835 1ULL << (be32_to_cpu(dwp->window_shift) - PAGE_SHIFT), dwp); 836 if (ret) 837 pr_warn("%pOF failed to clear tces in window.\n", 838 np); 839 else 840 pr_debug("%pOF successfully cleared tces in window.\n", 841 np); 842 } 843 844 /* 845 * Call only if DMA window is clean. 846 */ 847 static void __remove_dma_window(struct device_node *np, u32 *ddw_avail, u64 liobn) 848 { 849 int ret; 850 851 ret = rtas_call(ddw_avail[DDW_REMOVE_PE_DMA_WIN], 1, 1, NULL, liobn); 852 if (ret) 853 pr_warn("%pOF: failed to remove DMA window: rtas returned " 854 "%d to ibm,remove-pe-dma-window(%x) %llx\n", 855 np, ret, ddw_avail[DDW_REMOVE_PE_DMA_WIN], liobn); 856 else 857 pr_debug("%pOF: successfully removed DMA window: rtas returned " 858 "%d to ibm,remove-pe-dma-window(%x) %llx\n", 859 np, ret, ddw_avail[DDW_REMOVE_PE_DMA_WIN], liobn); 860 } 861 862 static void remove_dma_window(struct device_node *np, u32 *ddw_avail, 863 struct property *win) 864 { 865 struct dynamic_dma_window_prop *dwp; 866 u64 liobn; 867 868 dwp = win->value; 869 liobn = (u64)be32_to_cpu(dwp->liobn); 870 871 clean_dma_window(np, dwp); 872 __remove_dma_window(np, ddw_avail, liobn); 873 } 874 875 static int remove_ddw(struct device_node *np, bool remove_prop, const char *win_name) 876 { 877 struct property *win; 878 u32 ddw_avail[DDW_APPLICABLE_SIZE]; 879 int ret = 0; 880 881 win = of_find_property(np, win_name, NULL); 882 if (!win) 883 return -EINVAL; 884 885 ret = of_property_read_u32_array(np, "ibm,ddw-applicable", 886 &ddw_avail[0], DDW_APPLICABLE_SIZE); 887 if (ret) 888 return 0; 889 890 891 if (win->length >= sizeof(struct dynamic_dma_window_prop)) 892 remove_dma_window(np, ddw_avail, win); 893 894 if (!remove_prop) 895 return 0; 896 897 ret = of_remove_property(np, win); 898 if (ret) 899 pr_warn("%pOF: failed to remove DMA window property: %d\n", 900 np, ret); 901 return 0; 902 } 903 904 static bool find_existing_ddw(struct device_node *pdn, u64 *dma_addr, int *window_shift) 905 { 906 struct dma_win *window; 907 const struct dynamic_dma_window_prop *dma64; 908 bool found = false; 909 910 spin_lock(&dma_win_list_lock); 911 /* check if we already created a window and dupe that config if so */ 912 list_for_each_entry(window, &dma_win_list, list) { 913 if (window->device == pdn) { 914 dma64 = window->prop; 915 *dma_addr = be64_to_cpu(dma64->dma_base); 916 *window_shift = be32_to_cpu(dma64->window_shift); 917 found = true; 918 break; 919 } 920 } 921 spin_unlock(&dma_win_list_lock); 922 923 return found; 924 } 925 926 static struct dma_win *ddw_list_new_entry(struct device_node *pdn, 927 const struct dynamic_dma_window_prop *dma64) 928 { 929 struct dma_win *window; 930 931 window = kzalloc(sizeof(*window), GFP_KERNEL); 932 if (!window) 933 return NULL; 934 935 window->device = pdn; 936 window->prop = dma64; 937 938 return window; 939 } 940 941 static void find_existing_ddw_windows_named(const char *name) 942 { 943 int len; 944 struct device_node *pdn; 945 struct dma_win *window; 946 const struct dynamic_dma_window_prop *dma64; 947 948 for_each_node_with_property(pdn, name) { 949 dma64 = of_get_property(pdn, name, &len); 950 if (!dma64 || len < sizeof(*dma64)) { 951 remove_ddw(pdn, true, name); 952 continue; 953 } 954 955 window = ddw_list_new_entry(pdn, dma64); 956 if (!window) { 957 of_node_put(pdn); 958 break; 959 } 960 961 spin_lock(&dma_win_list_lock); 962 list_add(&window->list, &dma_win_list); 963 spin_unlock(&dma_win_list_lock); 964 } 965 } 966 967 static int find_existing_ddw_windows(void) 968 { 969 if (!firmware_has_feature(FW_FEATURE_LPAR)) 970 return 0; 971 972 find_existing_ddw_windows_named(DIRECT64_PROPNAME); 973 find_existing_ddw_windows_named(DMA64_PROPNAME); 974 975 return 0; 976 } 977 machine_arch_initcall(pseries, find_existing_ddw_windows); 978 979 /** 980 * ddw_read_ext - Get the value of an DDW extension 981 * @np: device node from which the extension value is to be read. 982 * @extnum: index number of the extension. 983 * @value: pointer to return value, modified when extension is available. 984 * 985 * Checks if "ibm,ddw-extensions" exists for this node, and get the value 986 * on index 'extnum'. 987 * It can be used only to check if a property exists, passing value == NULL. 988 * 989 * Returns: 990 * 0 if extension successfully read 991 * -EINVAL if the "ibm,ddw-extensions" does not exist, 992 * -ENODATA if "ibm,ddw-extensions" does not have a value, and 993 * -EOVERFLOW if "ibm,ddw-extensions" does not contain this extension. 994 */ 995 static inline int ddw_read_ext(const struct device_node *np, int extnum, 996 u32 *value) 997 { 998 static const char propname[] = "ibm,ddw-extensions"; 999 u32 count; 1000 int ret; 1001 1002 ret = of_property_read_u32_index(np, propname, DDW_EXT_SIZE, &count); 1003 if (ret) 1004 return ret; 1005 1006 if (count < extnum) 1007 return -EOVERFLOW; 1008 1009 if (!value) 1010 value = &count; 1011 1012 return of_property_read_u32_index(np, propname, extnum, value); 1013 } 1014 1015 static int query_ddw(struct pci_dev *dev, const u32 *ddw_avail, 1016 struct ddw_query_response *query, 1017 struct device_node *parent) 1018 { 1019 struct device_node *dn; 1020 struct pci_dn *pdn; 1021 u32 cfg_addr, ext_query, query_out[5]; 1022 u64 buid; 1023 int ret, out_sz; 1024 1025 /* 1026 * From LoPAR level 2.8, "ibm,ddw-extensions" index 3 can rule how many 1027 * output parameters ibm,query-pe-dma-windows will have, ranging from 1028 * 5 to 6. 1029 */ 1030 ret = ddw_read_ext(parent, DDW_EXT_QUERY_OUT_SIZE, &ext_query); 1031 if (!ret && ext_query == 1) 1032 out_sz = 6; 1033 else 1034 out_sz = 5; 1035 1036 /* 1037 * Get the config address and phb buid of the PE window. 1038 * Rely on eeh to retrieve this for us. 1039 * Retrieve them from the pci device, not the node with the 1040 * dma-window property 1041 */ 1042 dn = pci_device_to_OF_node(dev); 1043 pdn = PCI_DN(dn); 1044 buid = pdn->phb->buid; 1045 cfg_addr = ((pdn->busno << 16) | (pdn->devfn << 8)); 1046 1047 ret = rtas_call(ddw_avail[DDW_QUERY_PE_DMA_WIN], 3, out_sz, query_out, 1048 cfg_addr, BUID_HI(buid), BUID_LO(buid)); 1049 1050 switch (out_sz) { 1051 case 5: 1052 query->windows_available = query_out[0]; 1053 query->largest_available_block = query_out[1]; 1054 query->page_size = query_out[2]; 1055 query->migration_capable = query_out[3]; 1056 break; 1057 case 6: 1058 query->windows_available = query_out[0]; 1059 query->largest_available_block = ((u64)query_out[1] << 32) | 1060 query_out[2]; 1061 query->page_size = query_out[3]; 1062 query->migration_capable = query_out[4]; 1063 break; 1064 } 1065 1066 dev_info(&dev->dev, "ibm,query-pe-dma-windows(%x) %x %x %x returned %d, lb=%llx ps=%x wn=%d\n", 1067 ddw_avail[DDW_QUERY_PE_DMA_WIN], cfg_addr, BUID_HI(buid), 1068 BUID_LO(buid), ret, query->largest_available_block, 1069 query->page_size, query->windows_available); 1070 1071 return ret; 1072 } 1073 1074 static int create_ddw(struct pci_dev *dev, const u32 *ddw_avail, 1075 struct ddw_create_response *create, int page_shift, 1076 int window_shift) 1077 { 1078 struct device_node *dn; 1079 struct pci_dn *pdn; 1080 u32 cfg_addr; 1081 u64 buid; 1082 int ret; 1083 1084 /* 1085 * Get the config address and phb buid of the PE window. 1086 * Rely on eeh to retrieve this for us. 1087 * Retrieve them from the pci device, not the node with the 1088 * dma-window property 1089 */ 1090 dn = pci_device_to_OF_node(dev); 1091 pdn = PCI_DN(dn); 1092 buid = pdn->phb->buid; 1093 cfg_addr = ((pdn->busno << 16) | (pdn->devfn << 8)); 1094 1095 do { 1096 /* extra outputs are LIOBN and dma-addr (hi, lo) */ 1097 ret = rtas_call(ddw_avail[DDW_CREATE_PE_DMA_WIN], 5, 4, 1098 (u32 *)create, cfg_addr, BUID_HI(buid), 1099 BUID_LO(buid), page_shift, window_shift); 1100 } while (rtas_busy_delay(ret)); 1101 dev_info(&dev->dev, 1102 "ibm,create-pe-dma-window(%x) %x %x %x %x %x returned %d " 1103 "(liobn = 0x%x starting addr = %x %x)\n", 1104 ddw_avail[DDW_CREATE_PE_DMA_WIN], cfg_addr, BUID_HI(buid), 1105 BUID_LO(buid), page_shift, window_shift, ret, create->liobn, 1106 create->addr_hi, create->addr_lo); 1107 1108 return ret; 1109 } 1110 1111 struct failed_ddw_pdn { 1112 struct device_node *pdn; 1113 struct list_head list; 1114 }; 1115 1116 static LIST_HEAD(failed_ddw_pdn_list); 1117 1118 static phys_addr_t ddw_memory_hotplug_max(void) 1119 { 1120 resource_size_t max_addr = memory_hotplug_max(); 1121 struct device_node *memory; 1122 1123 for_each_node_by_type(memory, "memory") { 1124 struct resource res; 1125 1126 if (of_address_to_resource(memory, 0, &res)) 1127 continue; 1128 1129 max_addr = max_t(resource_size_t, max_addr, res.end + 1); 1130 } 1131 1132 return max_addr; 1133 } 1134 1135 /* 1136 * Platforms supporting the DDW option starting with LoPAR level 2.7 implement 1137 * ibm,ddw-extensions, which carries the rtas token for 1138 * ibm,reset-pe-dma-windows. 1139 * That rtas-call can be used to restore the default DMA window for the device. 1140 */ 1141 static void reset_dma_window(struct pci_dev *dev, struct device_node *par_dn) 1142 { 1143 int ret; 1144 u32 cfg_addr, reset_dma_win; 1145 u64 buid; 1146 struct device_node *dn; 1147 struct pci_dn *pdn; 1148 1149 ret = ddw_read_ext(par_dn, DDW_EXT_RESET_DMA_WIN, &reset_dma_win); 1150 if (ret) 1151 return; 1152 1153 dn = pci_device_to_OF_node(dev); 1154 pdn = PCI_DN(dn); 1155 buid = pdn->phb->buid; 1156 cfg_addr = (pdn->busno << 16) | (pdn->devfn << 8); 1157 1158 ret = rtas_call(reset_dma_win, 3, 1, NULL, cfg_addr, BUID_HI(buid), 1159 BUID_LO(buid)); 1160 if (ret) 1161 dev_info(&dev->dev, 1162 "ibm,reset-pe-dma-windows(%x) %x %x %x returned %d ", 1163 reset_dma_win, cfg_addr, BUID_HI(buid), BUID_LO(buid), 1164 ret); 1165 } 1166 1167 /* Return largest page shift based on "IO Page Sizes" output of ibm,query-pe-dma-window. */ 1168 static int iommu_get_page_shift(u32 query_page_size) 1169 { 1170 /* Supported IO page-sizes according to LoPAR, note that 2M is out of order */ 1171 const int shift[] = { 1172 __builtin_ctzll(SZ_4K), __builtin_ctzll(SZ_64K), __builtin_ctzll(SZ_16M), 1173 __builtin_ctzll(SZ_32M), __builtin_ctzll(SZ_64M), __builtin_ctzll(SZ_128M), 1174 __builtin_ctzll(SZ_256M), __builtin_ctzll(SZ_16G), __builtin_ctzll(SZ_2M) 1175 }; 1176 1177 int i = ARRAY_SIZE(shift) - 1; 1178 int ret = 0; 1179 1180 /* 1181 * On LoPAR, ibm,query-pe-dma-window outputs "IO Page Sizes" using a bit field: 1182 * - bit 31 means 4k pages are supported, 1183 * - bit 30 means 64k pages are supported, and so on. 1184 * Larger pagesizes map more memory with the same amount of TCEs, so start probing them. 1185 */ 1186 for (; i >= 0 ; i--) { 1187 if (query_page_size & (1 << i)) 1188 ret = max(ret, shift[i]); 1189 } 1190 1191 return ret; 1192 } 1193 1194 static struct property *ddw_property_create(const char *propname, u32 liobn, u64 dma_addr, 1195 u32 page_shift, u32 window_shift) 1196 { 1197 struct dynamic_dma_window_prop *ddwprop; 1198 struct property *win64; 1199 1200 win64 = kzalloc(sizeof(*win64), GFP_KERNEL); 1201 if (!win64) 1202 return NULL; 1203 1204 win64->name = kstrdup(propname, GFP_KERNEL); 1205 ddwprop = kzalloc(sizeof(*ddwprop), GFP_KERNEL); 1206 win64->value = ddwprop; 1207 win64->length = sizeof(*ddwprop); 1208 if (!win64->name || !win64->value) { 1209 kfree(win64->name); 1210 kfree(win64->value); 1211 kfree(win64); 1212 return NULL; 1213 } 1214 1215 ddwprop->liobn = cpu_to_be32(liobn); 1216 ddwprop->dma_base = cpu_to_be64(dma_addr); 1217 ddwprop->tce_shift = cpu_to_be32(page_shift); 1218 ddwprop->window_shift = cpu_to_be32(window_shift); 1219 1220 return win64; 1221 } 1222 1223 /* 1224 * If the PE supports dynamic dma windows, and there is space for a table 1225 * that can map all pages in a linear offset, then setup such a table, 1226 * and record the dma-offset in the struct device. 1227 * 1228 * dev: the pci device we are checking 1229 * pdn: the parent pe node with the ibm,dma_window property 1230 * Future: also check if we can remap the base window for our base page size 1231 * 1232 * returns true if can map all pages (direct mapping), false otherwise.. 1233 */ 1234 static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn) 1235 { 1236 int len = 0, ret; 1237 int max_ram_len = order_base_2(ddw_memory_hotplug_max()); 1238 struct ddw_query_response query; 1239 struct ddw_create_response create; 1240 int page_shift; 1241 u64 win_addr; 1242 const char *win_name; 1243 struct device_node *dn; 1244 u32 ddw_avail[DDW_APPLICABLE_SIZE]; 1245 struct dma_win *window; 1246 struct property *win64; 1247 struct failed_ddw_pdn *fpdn; 1248 bool default_win_removed = false, direct_mapping = false; 1249 bool pmem_present; 1250 struct pci_dn *pci = PCI_DN(pdn); 1251 struct property *default_win = NULL; 1252 1253 dn = of_find_node_by_type(NULL, "ibm,pmemory"); 1254 pmem_present = dn != NULL; 1255 of_node_put(dn); 1256 1257 mutex_lock(&dma_win_init_mutex); 1258 1259 if (find_existing_ddw(pdn, &dev->dev.archdata.dma_offset, &len)) { 1260 direct_mapping = (len >= max_ram_len); 1261 goto out_unlock; 1262 } 1263 1264 /* 1265 * If we already went through this for a previous function of 1266 * the same device and failed, we don't want to muck with the 1267 * DMA window again, as it will race with in-flight operations 1268 * and can lead to EEHs. The above mutex protects access to the 1269 * list. 1270 */ 1271 list_for_each_entry(fpdn, &failed_ddw_pdn_list, list) { 1272 if (fpdn->pdn == pdn) 1273 goto out_unlock; 1274 } 1275 1276 /* 1277 * the ibm,ddw-applicable property holds the tokens for: 1278 * ibm,query-pe-dma-window 1279 * ibm,create-pe-dma-window 1280 * ibm,remove-pe-dma-window 1281 * for the given node in that order. 1282 * the property is actually in the parent, not the PE 1283 */ 1284 ret = of_property_read_u32_array(pdn, "ibm,ddw-applicable", 1285 &ddw_avail[0], DDW_APPLICABLE_SIZE); 1286 if (ret) 1287 goto out_failed; 1288 1289 /* 1290 * Query if there is a second window of size to map the 1291 * whole partition. Query returns number of windows, largest 1292 * block assigned to PE (partition endpoint), and two bitmasks 1293 * of page sizes: supported and supported for migrate-dma. 1294 */ 1295 dn = pci_device_to_OF_node(dev); 1296 ret = query_ddw(dev, ddw_avail, &query, pdn); 1297 if (ret != 0) 1298 goto out_failed; 1299 1300 /* 1301 * If there is no window available, remove the default DMA window, 1302 * if it's present. This will make all the resources available to the 1303 * new DDW window. 1304 * If anything fails after this, we need to restore it, so also check 1305 * for extensions presence. 1306 */ 1307 if (query.windows_available == 0) { 1308 int reset_win_ext; 1309 1310 /* DDW + IOMMU on single window may fail if there is any allocation */ 1311 if (iommu_table_in_use(pci->table_group->tables[0])) { 1312 dev_warn(&dev->dev, "current IOMMU table in use, can't be replaced.\n"); 1313 goto out_failed; 1314 } 1315 1316 default_win = of_find_property(pdn, "ibm,dma-window", NULL); 1317 if (!default_win) 1318 goto out_failed; 1319 1320 reset_win_ext = ddw_read_ext(pdn, DDW_EXT_RESET_DMA_WIN, NULL); 1321 if (reset_win_ext) 1322 goto out_failed; 1323 1324 remove_dma_window(pdn, ddw_avail, default_win); 1325 default_win_removed = true; 1326 1327 /* Query again, to check if the window is available */ 1328 ret = query_ddw(dev, ddw_avail, &query, pdn); 1329 if (ret != 0) 1330 goto out_failed; 1331 1332 if (query.windows_available == 0) { 1333 /* no windows are available for this device. */ 1334 dev_dbg(&dev->dev, "no free dynamic windows"); 1335 goto out_failed; 1336 } 1337 } 1338 1339 page_shift = iommu_get_page_shift(query.page_size); 1340 if (!page_shift) { 1341 dev_dbg(&dev->dev, "no supported page size in mask %x", 1342 query.page_size); 1343 goto out_failed; 1344 } 1345 1346 1347 /* 1348 * The "ibm,pmemory" can appear anywhere in the address space. 1349 * Assuming it is still backed by page structs, try MAX_PHYSMEM_BITS 1350 * for the upper limit and fallback to max RAM otherwise but this 1351 * disables device::dma_ops_bypass. 1352 */ 1353 len = max_ram_len; 1354 if (pmem_present) { 1355 if (query.largest_available_block >= 1356 (1ULL << (MAX_PHYSMEM_BITS - page_shift))) 1357 len = MAX_PHYSMEM_BITS; 1358 else 1359 dev_info(&dev->dev, "Skipping ibm,pmemory"); 1360 } 1361 1362 /* check if the available block * number of ptes will map everything */ 1363 if (query.largest_available_block < (1ULL << (len - page_shift))) { 1364 dev_dbg(&dev->dev, 1365 "can't map partition max 0x%llx with %llu %llu-sized pages\n", 1366 1ULL << len, 1367 query.largest_available_block, 1368 1ULL << page_shift); 1369 1370 len = order_base_2(query.largest_available_block << page_shift); 1371 win_name = DMA64_PROPNAME; 1372 } else { 1373 direct_mapping = !default_win_removed || 1374 (len == MAX_PHYSMEM_BITS) || 1375 (!pmem_present && (len == max_ram_len)); 1376 win_name = direct_mapping ? DIRECT64_PROPNAME : DMA64_PROPNAME; 1377 } 1378 1379 ret = create_ddw(dev, ddw_avail, &create, page_shift, len); 1380 if (ret != 0) 1381 goto out_failed; 1382 1383 dev_dbg(&dev->dev, "created tce table LIOBN 0x%x for %pOF\n", 1384 create.liobn, dn); 1385 1386 win_addr = ((u64)create.addr_hi << 32) | create.addr_lo; 1387 win64 = ddw_property_create(win_name, create.liobn, win_addr, page_shift, len); 1388 1389 if (!win64) { 1390 dev_info(&dev->dev, 1391 "couldn't allocate property, property name, or value\n"); 1392 goto out_remove_win; 1393 } 1394 1395 ret = of_add_property(pdn, win64); 1396 if (ret) { 1397 dev_err(&dev->dev, "unable to add DMA window property for %pOF: %d", 1398 pdn, ret); 1399 goto out_free_prop; 1400 } 1401 1402 window = ddw_list_new_entry(pdn, win64->value); 1403 if (!window) 1404 goto out_del_prop; 1405 1406 if (direct_mapping) { 1407 /* DDW maps the whole partition, so enable direct DMA mapping */ 1408 ret = walk_system_ram_range(0, memblock_end_of_DRAM() >> PAGE_SHIFT, 1409 win64->value, tce_setrange_multi_pSeriesLP_walk); 1410 if (ret) { 1411 dev_info(&dev->dev, "failed to map DMA window for %pOF: %d\n", 1412 dn, ret); 1413 1414 /* Make sure to clean DDW if any TCE was set*/ 1415 clean_dma_window(pdn, win64->value); 1416 goto out_del_list; 1417 } 1418 } else { 1419 struct iommu_table *newtbl; 1420 int i; 1421 unsigned long start = 0, end = 0; 1422 1423 for (i = 0; i < ARRAY_SIZE(pci->phb->mem_resources); i++) { 1424 const unsigned long mask = IORESOURCE_MEM_64 | IORESOURCE_MEM; 1425 1426 /* Look for MMIO32 */ 1427 if ((pci->phb->mem_resources[i].flags & mask) == IORESOURCE_MEM) { 1428 start = pci->phb->mem_resources[i].start; 1429 end = pci->phb->mem_resources[i].end; 1430 break; 1431 } 1432 } 1433 1434 /* New table for using DDW instead of the default DMA window */ 1435 newtbl = iommu_pseries_alloc_table(pci->phb->node); 1436 if (!newtbl) { 1437 dev_dbg(&dev->dev, "couldn't create new IOMMU table\n"); 1438 goto out_del_list; 1439 } 1440 1441 iommu_table_setparms_common(newtbl, pci->phb->bus->number, create.liobn, win_addr, 1442 1UL << len, page_shift, NULL, &iommu_table_lpar_multi_ops); 1443 iommu_init_table(newtbl, pci->phb->node, start, end); 1444 1445 pci->table_group->tables[1] = newtbl; 1446 1447 set_iommu_table_base(&dev->dev, newtbl); 1448 } 1449 1450 if (default_win_removed) { 1451 iommu_tce_table_put(pci->table_group->tables[0]); 1452 pci->table_group->tables[0] = NULL; 1453 1454 /* default_win is valid here because default_win_removed == true */ 1455 of_remove_property(pdn, default_win); 1456 dev_info(&dev->dev, "Removed default DMA window for %pOF\n", pdn); 1457 } 1458 1459 spin_lock(&dma_win_list_lock); 1460 list_add(&window->list, &dma_win_list); 1461 spin_unlock(&dma_win_list_lock); 1462 1463 dev->dev.archdata.dma_offset = win_addr; 1464 goto out_unlock; 1465 1466 out_del_list: 1467 kfree(window); 1468 1469 out_del_prop: 1470 of_remove_property(pdn, win64); 1471 1472 out_free_prop: 1473 kfree(win64->name); 1474 kfree(win64->value); 1475 kfree(win64); 1476 1477 out_remove_win: 1478 /* DDW is clean, so it's ok to call this directly. */ 1479 __remove_dma_window(pdn, ddw_avail, create.liobn); 1480 1481 out_failed: 1482 if (default_win_removed) 1483 reset_dma_window(dev, pdn); 1484 1485 fpdn = kzalloc(sizeof(*fpdn), GFP_KERNEL); 1486 if (!fpdn) 1487 goto out_unlock; 1488 fpdn->pdn = pdn; 1489 list_add(&fpdn->list, &failed_ddw_pdn_list); 1490 1491 out_unlock: 1492 mutex_unlock(&dma_win_init_mutex); 1493 1494 /* 1495 * If we have persistent memory and the window size is only as big 1496 * as RAM, then we failed to create a window to cover persistent 1497 * memory and need to set the DMA limit. 1498 */ 1499 if (pmem_present && direct_mapping && len == max_ram_len) 1500 dev->dev.bus_dma_limit = dev->dev.archdata.dma_offset + (1ULL << len); 1501 1502 return direct_mapping; 1503 } 1504 1505 static void pci_dma_dev_setup_pSeriesLP(struct pci_dev *dev) 1506 { 1507 struct device_node *pdn, *dn; 1508 struct iommu_table *tbl; 1509 const __be32 *dma_window = NULL; 1510 struct pci_dn *pci; 1511 1512 pr_debug("pci_dma_dev_setup_pSeriesLP: %s\n", pci_name(dev)); 1513 1514 /* dev setup for LPAR is a little tricky, since the device tree might 1515 * contain the dma-window properties per-device and not necessarily 1516 * for the bus. So we need to search upwards in the tree until we 1517 * either hit a dma-window property, OR find a parent with a table 1518 * already allocated. 1519 */ 1520 dn = pci_device_to_OF_node(dev); 1521 pr_debug(" node is %pOF\n", dn); 1522 1523 pdn = pci_dma_find(dn, &dma_window); 1524 if (!pdn || !PCI_DN(pdn)) { 1525 printk(KERN_WARNING "pci_dma_dev_setup_pSeriesLP: " 1526 "no DMA window found for pci dev=%s dn=%pOF\n", 1527 pci_name(dev), dn); 1528 return; 1529 } 1530 pr_debug(" parent is %pOF\n", pdn); 1531 1532 pci = PCI_DN(pdn); 1533 if (!pci->table_group) { 1534 pci->table_group = iommu_pseries_alloc_group(pci->phb->node); 1535 tbl = pci->table_group->tables[0]; 1536 iommu_table_setparms_lpar(pci->phb, pdn, tbl, 1537 pci->table_group, dma_window); 1538 1539 iommu_init_table(tbl, pci->phb->node, 0, 0); 1540 iommu_register_group(pci->table_group, 1541 pci_domain_nr(pci->phb->bus), 0); 1542 pr_debug(" created table: %p\n", pci->table_group); 1543 } else { 1544 pr_debug(" found DMA window, table: %p\n", pci->table_group); 1545 } 1546 1547 set_iommu_table_base(&dev->dev, pci->table_group->tables[0]); 1548 iommu_add_device(pci->table_group, &dev->dev); 1549 } 1550 1551 static bool iommu_bypass_supported_pSeriesLP(struct pci_dev *pdev, u64 dma_mask) 1552 { 1553 struct device_node *dn = pci_device_to_OF_node(pdev), *pdn; 1554 1555 /* only attempt to use a new window if 64-bit DMA is requested */ 1556 if (dma_mask < DMA_BIT_MASK(64)) 1557 return false; 1558 1559 dev_dbg(&pdev->dev, "node is %pOF\n", dn); 1560 1561 /* 1562 * the device tree might contain the dma-window properties 1563 * per-device and not necessarily for the bus. So we need to 1564 * search upwards in the tree until we either hit a dma-window 1565 * property, OR find a parent with a table already allocated. 1566 */ 1567 pdn = pci_dma_find(dn, NULL); 1568 if (pdn && PCI_DN(pdn)) 1569 return enable_ddw(pdev, pdn); 1570 1571 return false; 1572 } 1573 1574 static int iommu_mem_notifier(struct notifier_block *nb, unsigned long action, 1575 void *data) 1576 { 1577 struct dma_win *window; 1578 struct memory_notify *arg = data; 1579 int ret = 0; 1580 1581 switch (action) { 1582 case MEM_GOING_ONLINE: 1583 spin_lock(&dma_win_list_lock); 1584 list_for_each_entry(window, &dma_win_list, list) { 1585 ret |= tce_setrange_multi_pSeriesLP(arg->start_pfn, 1586 arg->nr_pages, window->prop); 1587 /* XXX log error */ 1588 } 1589 spin_unlock(&dma_win_list_lock); 1590 break; 1591 case MEM_CANCEL_ONLINE: 1592 case MEM_OFFLINE: 1593 spin_lock(&dma_win_list_lock); 1594 list_for_each_entry(window, &dma_win_list, list) { 1595 ret |= tce_clearrange_multi_pSeriesLP(arg->start_pfn, 1596 arg->nr_pages, window->prop); 1597 /* XXX log error */ 1598 } 1599 spin_unlock(&dma_win_list_lock); 1600 break; 1601 default: 1602 break; 1603 } 1604 if (ret && action != MEM_CANCEL_ONLINE) 1605 return NOTIFY_BAD; 1606 1607 return NOTIFY_OK; 1608 } 1609 1610 static struct notifier_block iommu_mem_nb = { 1611 .notifier_call = iommu_mem_notifier, 1612 }; 1613 1614 static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *data) 1615 { 1616 int err = NOTIFY_OK; 1617 struct of_reconfig_data *rd = data; 1618 struct device_node *np = rd->dn; 1619 struct pci_dn *pci = PCI_DN(np); 1620 struct dma_win *window; 1621 1622 switch (action) { 1623 case OF_RECONFIG_DETACH_NODE: 1624 /* 1625 * Removing the property will invoke the reconfig 1626 * notifier again, which causes dead-lock on the 1627 * read-write semaphore of the notifier chain. So 1628 * we have to remove the property when releasing 1629 * the device node. 1630 */ 1631 if (remove_ddw(np, false, DIRECT64_PROPNAME)) 1632 remove_ddw(np, false, DMA64_PROPNAME); 1633 1634 if (pci && pci->table_group) 1635 iommu_pseries_free_group(pci->table_group, 1636 np->full_name); 1637 1638 spin_lock(&dma_win_list_lock); 1639 list_for_each_entry(window, &dma_win_list, list) { 1640 if (window->device == np) { 1641 list_del(&window->list); 1642 kfree(window); 1643 break; 1644 } 1645 } 1646 spin_unlock(&dma_win_list_lock); 1647 break; 1648 default: 1649 err = NOTIFY_DONE; 1650 break; 1651 } 1652 return err; 1653 } 1654 1655 static struct notifier_block iommu_reconfig_nb = { 1656 .notifier_call = iommu_reconfig_notifier, 1657 }; 1658 1659 /* These are called very early. */ 1660 void __init iommu_init_early_pSeries(void) 1661 { 1662 if (of_chosen && of_get_property(of_chosen, "linux,iommu-off", NULL)) 1663 return; 1664 1665 if (firmware_has_feature(FW_FEATURE_LPAR)) { 1666 pseries_pci_controller_ops.dma_bus_setup = pci_dma_bus_setup_pSeriesLP; 1667 pseries_pci_controller_ops.dma_dev_setup = pci_dma_dev_setup_pSeriesLP; 1668 if (!disable_ddw) 1669 pseries_pci_controller_ops.iommu_bypass_supported = 1670 iommu_bypass_supported_pSeriesLP; 1671 } else { 1672 pseries_pci_controller_ops.dma_bus_setup = pci_dma_bus_setup_pSeries; 1673 pseries_pci_controller_ops.dma_dev_setup = pci_dma_dev_setup_pSeries; 1674 } 1675 1676 1677 of_reconfig_notifier_register(&iommu_reconfig_nb); 1678 register_memory_notifier(&iommu_mem_nb); 1679 1680 set_pci_dma_ops(&dma_iommu_ops); 1681 } 1682 1683 static int __init disable_multitce(char *str) 1684 { 1685 if (strcmp(str, "off") == 0 && 1686 firmware_has_feature(FW_FEATURE_LPAR) && 1687 (firmware_has_feature(FW_FEATURE_PUT_TCE_IND) || 1688 firmware_has_feature(FW_FEATURE_STUFF_TCE))) { 1689 printk(KERN_INFO "Disabling MULTITCE firmware feature\n"); 1690 powerpc_firmware_features &= 1691 ~(FW_FEATURE_PUT_TCE_IND | FW_FEATURE_STUFF_TCE); 1692 } 1693 return 1; 1694 } 1695 1696 __setup("multitce=", disable_multitce); 1697 1698 static int tce_iommu_bus_notifier(struct notifier_block *nb, 1699 unsigned long action, void *data) 1700 { 1701 struct device *dev = data; 1702 1703 switch (action) { 1704 case BUS_NOTIFY_DEL_DEVICE: 1705 iommu_del_device(dev); 1706 return 0; 1707 default: 1708 return 0; 1709 } 1710 } 1711 1712 static struct notifier_block tce_iommu_bus_nb = { 1713 .notifier_call = tce_iommu_bus_notifier, 1714 }; 1715 1716 static int __init tce_iommu_bus_notifier_init(void) 1717 { 1718 bus_register_notifier(&pci_bus_type, &tce_iommu_bus_nb); 1719 return 0; 1720 } 1721 machine_subsys_initcall_sync(pseries, tce_iommu_bus_notifier_init); 1722 1723 #ifdef CONFIG_SPAPR_TCE_IOMMU 1724 struct iommu_group *pSeries_pci_device_group(struct pci_controller *hose, 1725 struct pci_dev *pdev) 1726 { 1727 struct device_node *pdn, *dn = pdev->dev.of_node; 1728 struct iommu_group *grp; 1729 struct pci_dn *pci; 1730 1731 pdn = pci_dma_find(dn, NULL); 1732 if (!pdn || !PCI_DN(pdn)) 1733 return ERR_PTR(-ENODEV); 1734 1735 pci = PCI_DN(pdn); 1736 if (!pci->table_group) 1737 return ERR_PTR(-ENODEV); 1738 1739 grp = pci->table_group->group; 1740 if (!grp) 1741 return ERR_PTR(-ENODEV); 1742 1743 return iommu_group_ref_get(grp); 1744 } 1745 #endif 1746