1 /* 2 * arch/ppc64/kernel/pSeries_iommu.c 3 * 4 * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation 5 * 6 * Rewrite, cleanup: 7 * 8 * Copyright (C) 2004 Olof Johansson <olof@austin.ibm.com>, IBM Corporation 9 * 10 * Dynamic DMA mapping support, pSeries-specific parts, both SMP and LPAR. 11 * 12 * 13 * This program is free software; you can redistribute it and/or modify 14 * it under the terms of the GNU General Public License as published by 15 * the Free Software Foundation; either version 2 of the License, or 16 * (at your option) any later version. 17 * 18 * This program is distributed in the hope that it will be useful, 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 * GNU General Public License for more details. 22 * 23 * You should have received a copy of the GNU General Public License 24 * along with this program; if not, write to the Free Software 25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 26 */ 27 28 #include <linux/config.h> 29 #include <linux/init.h> 30 #include <linux/types.h> 31 #include <linux/slab.h> 32 #include <linux/mm.h> 33 #include <linux/spinlock.h> 34 #include <linux/string.h> 35 #include <linux/pci.h> 36 #include <linux/dma-mapping.h> 37 #include <asm/io.h> 38 #include <asm/prom.h> 39 #include <asm/rtas.h> 40 #include <asm/ppcdebug.h> 41 #include <asm/iommu.h> 42 #include <asm/pci-bridge.h> 43 #include <asm/machdep.h> 44 #include <asm/abs_addr.h> 45 #include <asm/pSeries_reconfig.h> 46 #include <asm/systemcfg.h> 47 #include <asm/firmware.h> 48 #include <asm/tce.h> 49 #include <asm/ppc-pci.h> 50 51 #include "plpar_wrappers.h" 52 53 #define DBG(fmt...) 54 55 extern int is_python(struct device_node *); 56 57 static void tce_build_pSeries(struct iommu_table *tbl, long index, 58 long npages, unsigned long uaddr, 59 enum dma_data_direction direction) 60 { 61 union tce_entry t; 62 union tce_entry *tp; 63 64 index <<= TCE_PAGE_FACTOR; 65 npages <<= TCE_PAGE_FACTOR; 66 67 t.te_word = 0; 68 t.te_rdwr = 1; // Read allowed 69 70 if (direction != DMA_TO_DEVICE) 71 t.te_pciwr = 1; 72 73 tp = ((union tce_entry *)tbl->it_base) + index; 74 75 while (npages--) { 76 /* can't move this out since we might cross LMB boundary */ 77 t.te_rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT; 78 79 tp->te_word = t.te_word; 80 81 uaddr += TCE_PAGE_SIZE; 82 tp++; 83 } 84 } 85 86 87 static void tce_free_pSeries(struct iommu_table *tbl, long index, long npages) 88 { 89 union tce_entry t; 90 union tce_entry *tp; 91 92 npages <<= TCE_PAGE_FACTOR; 93 index <<= TCE_PAGE_FACTOR; 94 95 t.te_word = 0; 96 tp = ((union tce_entry *)tbl->it_base) + index; 97 98 while (npages--) { 99 tp->te_word = t.te_word; 100 101 tp++; 102 } 103 } 104 105 106 static void tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum, 107 long npages, unsigned long uaddr, 108 enum dma_data_direction direction) 109 { 110 u64 rc; 111 union tce_entry tce; 112 113 tce.te_word = 0; 114 tce.te_rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT; 115 tce.te_rdwr = 1; 116 if (direction != DMA_TO_DEVICE) 117 tce.te_pciwr = 1; 118 119 while (npages--) { 120 rc = plpar_tce_put((u64)tbl->it_index, 121 (u64)tcenum << 12, 122 tce.te_word ); 123 124 if (rc && printk_ratelimit()) { 125 printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc); 126 printk("\tindex = 0x%lx\n", (u64)tbl->it_index); 127 printk("\ttcenum = 0x%lx\n", (u64)tcenum); 128 printk("\ttce val = 0x%lx\n", tce.te_word ); 129 show_stack(current, (unsigned long *)__get_SP()); 130 } 131 132 tcenum++; 133 tce.te_rpn++; 134 } 135 } 136 137 static DEFINE_PER_CPU(void *, tce_page) = NULL; 138 139 static void tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum, 140 long npages, unsigned long uaddr, 141 enum dma_data_direction direction) 142 { 143 u64 rc; 144 union tce_entry tce, *tcep; 145 long l, limit; 146 147 tcenum <<= TCE_PAGE_FACTOR; 148 npages <<= TCE_PAGE_FACTOR; 149 150 if (npages == 1) 151 return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr, 152 direction); 153 154 tcep = __get_cpu_var(tce_page); 155 156 /* This is safe to do since interrupts are off when we're called 157 * from iommu_alloc{,_sg}() 158 */ 159 if (!tcep) { 160 tcep = (void *)__get_free_page(GFP_ATOMIC); 161 /* If allocation fails, fall back to the loop implementation */ 162 if (!tcep) 163 return tce_build_pSeriesLP(tbl, tcenum, npages, 164 uaddr, direction); 165 __get_cpu_var(tce_page) = tcep; 166 } 167 168 tce.te_word = 0; 169 tce.te_rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT; 170 tce.te_rdwr = 1; 171 if (direction != DMA_TO_DEVICE) 172 tce.te_pciwr = 1; 173 174 /* We can map max one pageful of TCEs at a time */ 175 do { 176 /* 177 * Set up the page with TCE data, looping through and setting 178 * the values. 179 */ 180 limit = min_t(long, npages, 4096/sizeof(union tce_entry)); 181 182 for (l = 0; l < limit; l++) { 183 tcep[l] = tce; 184 tce.te_rpn++; 185 } 186 187 rc = plpar_tce_put_indirect((u64)tbl->it_index, 188 (u64)tcenum << 12, 189 (u64)virt_to_abs(tcep), 190 limit); 191 192 npages -= limit; 193 tcenum += limit; 194 } while (npages > 0 && !rc); 195 196 if (rc && printk_ratelimit()) { 197 printk("tce_buildmulti_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc); 198 printk("\tindex = 0x%lx\n", (u64)tbl->it_index); 199 printk("\tnpages = 0x%lx\n", (u64)npages); 200 printk("\ttce[0] val = 0x%lx\n", tcep[0].te_word); 201 show_stack(current, (unsigned long *)__get_SP()); 202 } 203 } 204 205 static void tce_free_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages) 206 { 207 u64 rc; 208 union tce_entry tce; 209 210 tcenum <<= TCE_PAGE_FACTOR; 211 npages <<= TCE_PAGE_FACTOR; 212 213 tce.te_word = 0; 214 215 while (npages--) { 216 rc = plpar_tce_put((u64)tbl->it_index, 217 (u64)tcenum << 12, 218 tce.te_word); 219 220 if (rc && printk_ratelimit()) { 221 printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc); 222 printk("\tindex = 0x%lx\n", (u64)tbl->it_index); 223 printk("\ttcenum = 0x%lx\n", (u64)tcenum); 224 printk("\ttce val = 0x%lx\n", tce.te_word ); 225 show_stack(current, (unsigned long *)__get_SP()); 226 } 227 228 tcenum++; 229 } 230 } 231 232 233 static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages) 234 { 235 u64 rc; 236 union tce_entry tce; 237 238 tcenum <<= TCE_PAGE_FACTOR; 239 npages <<= TCE_PAGE_FACTOR; 240 241 tce.te_word = 0; 242 243 rc = plpar_tce_stuff((u64)tbl->it_index, 244 (u64)tcenum << 12, 245 tce.te_word, 246 npages); 247 248 if (rc && printk_ratelimit()) { 249 printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n"); 250 printk("\trc = %ld\n", rc); 251 printk("\tindex = 0x%lx\n", (u64)tbl->it_index); 252 printk("\tnpages = 0x%lx\n", (u64)npages); 253 printk("\ttce val = 0x%lx\n", tce.te_word ); 254 show_stack(current, (unsigned long *)__get_SP()); 255 } 256 } 257 258 static void iommu_table_setparms(struct pci_controller *phb, 259 struct device_node *dn, 260 struct iommu_table *tbl) 261 { 262 struct device_node *node; 263 unsigned long *basep; 264 unsigned int *sizep; 265 266 node = (struct device_node *)phb->arch_data; 267 268 basep = (unsigned long *)get_property(node, "linux,tce-base", NULL); 269 sizep = (unsigned int *)get_property(node, "linux,tce-size", NULL); 270 if (basep == NULL || sizep == NULL) { 271 printk(KERN_ERR "PCI_DMA: iommu_table_setparms: %s has " 272 "missing tce entries !\n", dn->full_name); 273 return; 274 } 275 276 tbl->it_base = (unsigned long)__va(*basep); 277 memset((void *)tbl->it_base, 0, *sizep); 278 279 tbl->it_busno = phb->bus->number; 280 281 /* Units of tce entries */ 282 tbl->it_offset = phb->dma_window_base_cur >> PAGE_SHIFT; 283 284 /* Test if we are going over 2GB of DMA space */ 285 if (phb->dma_window_base_cur + phb->dma_window_size > 0x80000000ul) { 286 udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n"); 287 panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n"); 288 } 289 290 phb->dma_window_base_cur += phb->dma_window_size; 291 292 /* Set the tce table size - measured in entries */ 293 tbl->it_size = phb->dma_window_size >> PAGE_SHIFT; 294 295 tbl->it_index = 0; 296 tbl->it_blocksize = 16; 297 tbl->it_type = TCE_PCI; 298 } 299 300 /* 301 * iommu_table_setparms_lpar 302 * 303 * Function: On pSeries LPAR systems, return TCE table info, given a pci bus. 304 * 305 * ToDo: properly interpret the ibm,dma-window property. The definition is: 306 * logical-bus-number (1 word) 307 * phys-address (#address-cells words) 308 * size (#cell-size words) 309 * 310 * Currently we hard code these sizes (more or less). 311 */ 312 static void iommu_table_setparms_lpar(struct pci_controller *phb, 313 struct device_node *dn, 314 struct iommu_table *tbl, 315 unsigned int *dma_window) 316 { 317 tbl->it_busno = PCI_DN(dn)->bussubno; 318 319 /* TODO: Parse field size properties properly. */ 320 tbl->it_size = (((unsigned long)dma_window[4] << 32) | 321 (unsigned long)dma_window[5]) >> PAGE_SHIFT; 322 tbl->it_offset = (((unsigned long)dma_window[2] << 32) | 323 (unsigned long)dma_window[3]) >> PAGE_SHIFT; 324 tbl->it_base = 0; 325 tbl->it_index = dma_window[0]; 326 tbl->it_blocksize = 16; 327 tbl->it_type = TCE_PCI; 328 } 329 330 static void iommu_bus_setup_pSeries(struct pci_bus *bus) 331 { 332 struct device_node *dn; 333 struct iommu_table *tbl; 334 struct device_node *isa_dn, *isa_dn_orig; 335 struct device_node *tmp; 336 struct pci_dn *pci; 337 int children; 338 339 DBG("iommu_bus_setup_pSeries, bus %p, bus->self %p\n", bus, bus->self); 340 341 dn = pci_bus_to_OF_node(bus); 342 pci = PCI_DN(dn); 343 344 if (bus->self) { 345 /* This is not a root bus, any setup will be done for the 346 * device-side of the bridge in iommu_dev_setup_pSeries(). 347 */ 348 return; 349 } 350 351 /* Check if the ISA bus on the system is under 352 * this PHB. 353 */ 354 isa_dn = isa_dn_orig = of_find_node_by_type(NULL, "isa"); 355 356 while (isa_dn && isa_dn != dn) 357 isa_dn = isa_dn->parent; 358 359 if (isa_dn_orig) 360 of_node_put(isa_dn_orig); 361 362 /* Count number of direct PCI children of the PHB. 363 * All PCI device nodes have class-code property, so it's 364 * an easy way to find them. 365 */ 366 for (children = 0, tmp = dn->child; tmp; tmp = tmp->sibling) 367 if (get_property(tmp, "class-code", NULL)) 368 children++; 369 370 DBG("Children: %d\n", children); 371 372 /* Calculate amount of DMA window per slot. Each window must be 373 * a power of two (due to pci_alloc_consistent requirements). 374 * 375 * Keep 256MB aside for PHBs with ISA. 376 */ 377 378 if (!isa_dn) { 379 /* No ISA/IDE - just set window size and return */ 380 pci->phb->dma_window_size = 0x80000000ul; /* To be divided */ 381 382 while (pci->phb->dma_window_size * children > 0x80000000ul) 383 pci->phb->dma_window_size >>= 1; 384 DBG("No ISA/IDE, window size is 0x%lx\n", 385 pci->phb->dma_window_size); 386 pci->phb->dma_window_base_cur = 0; 387 388 return; 389 } 390 391 /* If we have ISA, then we probably have an IDE 392 * controller too. Allocate a 128MB table but 393 * skip the first 128MB to avoid stepping on ISA 394 * space. 395 */ 396 pci->phb->dma_window_size = 0x8000000ul; 397 pci->phb->dma_window_base_cur = 0x8000000ul; 398 399 tbl = kmalloc(sizeof(struct iommu_table), GFP_KERNEL); 400 401 iommu_table_setparms(pci->phb, dn, tbl); 402 pci->iommu_table = iommu_init_table(tbl); 403 404 /* Divide the rest (1.75GB) among the children */ 405 pci->phb->dma_window_size = 0x80000000ul; 406 while (pci->phb->dma_window_size * children > 0x70000000ul) 407 pci->phb->dma_window_size >>= 1; 408 409 DBG("ISA/IDE, window size is 0x%lx\n", pci->phb->dma_window_size); 410 411 } 412 413 414 static void iommu_bus_setup_pSeriesLP(struct pci_bus *bus) 415 { 416 struct iommu_table *tbl; 417 struct device_node *dn, *pdn; 418 struct pci_dn *ppci; 419 unsigned int *dma_window = NULL; 420 421 DBG("iommu_bus_setup_pSeriesLP, bus %p, bus->self %p\n", bus, bus->self); 422 423 dn = pci_bus_to_OF_node(bus); 424 425 /* Find nearest ibm,dma-window, walking up the device tree */ 426 for (pdn = dn; pdn != NULL; pdn = pdn->parent) { 427 dma_window = (unsigned int *)get_property(pdn, "ibm,dma-window", NULL); 428 if (dma_window != NULL) 429 break; 430 } 431 432 if (dma_window == NULL) { 433 DBG("iommu_bus_setup_pSeriesLP: bus %s seems to have no ibm,dma-window property\n", dn->full_name); 434 return; 435 } 436 437 ppci = pdn->data; 438 if (!ppci->iommu_table) { 439 /* Bussubno hasn't been copied yet. 440 * Do it now because iommu_table_setparms_lpar needs it. 441 */ 442 443 ppci->bussubno = bus->number; 444 445 tbl = (struct iommu_table *)kmalloc(sizeof(struct iommu_table), 446 GFP_KERNEL); 447 448 iommu_table_setparms_lpar(ppci->phb, pdn, tbl, dma_window); 449 450 ppci->iommu_table = iommu_init_table(tbl); 451 } 452 453 if (pdn != dn) 454 PCI_DN(dn)->iommu_table = ppci->iommu_table; 455 } 456 457 458 static void iommu_dev_setup_pSeries(struct pci_dev *dev) 459 { 460 struct device_node *dn, *mydn; 461 struct iommu_table *tbl; 462 463 DBG("iommu_dev_setup_pSeries, dev %p (%s)\n", dev, pci_name(dev)); 464 465 mydn = dn = pci_device_to_OF_node(dev); 466 467 /* If we're the direct child of a root bus, then we need to allocate 468 * an iommu table ourselves. The bus setup code should have setup 469 * the window sizes already. 470 */ 471 if (!dev->bus->self) { 472 DBG(" --> first child, no bridge. Allocating iommu table.\n"); 473 tbl = kmalloc(sizeof(struct iommu_table), GFP_KERNEL); 474 iommu_table_setparms(PCI_DN(dn)->phb, dn, tbl); 475 PCI_DN(mydn)->iommu_table = iommu_init_table(tbl); 476 477 return; 478 } 479 480 /* If this device is further down the bus tree, search upwards until 481 * an already allocated iommu table is found and use that. 482 */ 483 484 while (dn && dn->data && PCI_DN(dn)->iommu_table == NULL) 485 dn = dn->parent; 486 487 if (dn && dn->data) { 488 PCI_DN(mydn)->iommu_table = PCI_DN(dn)->iommu_table; 489 } else { 490 DBG("iommu_dev_setup_pSeries, dev %p (%s) has no iommu table\n", dev, pci_name(dev)); 491 } 492 } 493 494 static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node) 495 { 496 int err = NOTIFY_OK; 497 struct device_node *np = node; 498 struct pci_dn *pci = np->data; 499 500 switch (action) { 501 case PSERIES_RECONFIG_REMOVE: 502 if (pci && pci->iommu_table && 503 get_property(np, "ibm,dma-window", NULL)) 504 iommu_free_table(np); 505 break; 506 default: 507 err = NOTIFY_DONE; 508 break; 509 } 510 return err; 511 } 512 513 static struct notifier_block iommu_reconfig_nb = { 514 .notifier_call = iommu_reconfig_notifier, 515 }; 516 517 static void iommu_dev_setup_pSeriesLP(struct pci_dev *dev) 518 { 519 struct device_node *pdn, *dn; 520 struct iommu_table *tbl; 521 int *dma_window = NULL; 522 struct pci_dn *pci; 523 524 DBG("iommu_dev_setup_pSeriesLP, dev %p (%s)\n", dev, pci_name(dev)); 525 526 /* dev setup for LPAR is a little tricky, since the device tree might 527 * contain the dma-window properties per-device and not neccesarily 528 * for the bus. So we need to search upwards in the tree until we 529 * either hit a dma-window property, OR find a parent with a table 530 * already allocated. 531 */ 532 dn = pci_device_to_OF_node(dev); 533 534 for (pdn = dn; pdn && pdn->data && !PCI_DN(pdn)->iommu_table; 535 pdn = pdn->parent) { 536 dma_window = (unsigned int *) 537 get_property(pdn, "ibm,dma-window", NULL); 538 if (dma_window) 539 break; 540 } 541 542 /* Check for parent == NULL so we don't try to setup the empty EADS 543 * slots on POWER4 machines. 544 */ 545 if (dma_window == NULL || pdn->parent == NULL) { 546 DBG("No dma window for device, linking to parent\n"); 547 PCI_DN(dn)->iommu_table = PCI_DN(pdn)->iommu_table; 548 return; 549 } else { 550 DBG("Found DMA window, allocating table\n"); 551 } 552 553 pci = pdn->data; 554 if (!pci->iommu_table) { 555 /* iommu_table_setparms_lpar needs bussubno. */ 556 pci->bussubno = pci->phb->bus->number; 557 558 tbl = (struct iommu_table *)kmalloc(sizeof(struct iommu_table), 559 GFP_KERNEL); 560 561 iommu_table_setparms_lpar(pci->phb, pdn, tbl, dma_window); 562 563 pci->iommu_table = iommu_init_table(tbl); 564 } 565 566 if (pdn != dn) 567 PCI_DN(dn)->iommu_table = pci->iommu_table; 568 } 569 570 static void iommu_bus_setup_null(struct pci_bus *b) { } 571 static void iommu_dev_setup_null(struct pci_dev *d) { } 572 573 /* These are called very early. */ 574 void iommu_init_early_pSeries(void) 575 { 576 if (of_chosen && get_property(of_chosen, "linux,iommu-off", NULL)) { 577 /* Direct I/O, IOMMU off */ 578 ppc_md.iommu_dev_setup = iommu_dev_setup_null; 579 ppc_md.iommu_bus_setup = iommu_bus_setup_null; 580 pci_direct_iommu_init(); 581 582 return; 583 } 584 585 if (systemcfg->platform & PLATFORM_LPAR) { 586 if (firmware_has_feature(FW_FEATURE_MULTITCE)) { 587 ppc_md.tce_build = tce_buildmulti_pSeriesLP; 588 ppc_md.tce_free = tce_freemulti_pSeriesLP; 589 } else { 590 ppc_md.tce_build = tce_build_pSeriesLP; 591 ppc_md.tce_free = tce_free_pSeriesLP; 592 } 593 ppc_md.iommu_bus_setup = iommu_bus_setup_pSeriesLP; 594 ppc_md.iommu_dev_setup = iommu_dev_setup_pSeriesLP; 595 } else { 596 ppc_md.tce_build = tce_build_pSeries; 597 ppc_md.tce_free = tce_free_pSeries; 598 ppc_md.iommu_bus_setup = iommu_bus_setup_pSeries; 599 ppc_md.iommu_dev_setup = iommu_dev_setup_pSeries; 600 } 601 602 603 pSeries_reconfig_notifier_register(&iommu_reconfig_nb); 604 605 pci_iommu_init(); 606 } 607 608