1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/arch/alpha/kernel/pci_iommu.c 4 */ 5 6 #include <linux/kernel.h> 7 #include <linux/mm.h> 8 #include <linux/pci.h> 9 #include <linux/gfp.h> 10 #include <linux/memblock.h> 11 #include <linux/export.h> 12 #include <linux/scatterlist.h> 13 #include <linux/log2.h> 14 #include <linux/dma-map-ops.h> 15 #include <linux/iommu-helper.h> 16 #include <linux/string_choices.h> 17 18 #include <asm/io.h> 19 #include <asm/hwrpb.h> 20 21 #include "proto.h" 22 #include "pci_impl.h" 23 24 25 #define DEBUG_ALLOC 0 26 #if DEBUG_ALLOC > 0 27 # define DBGA(args...) printk(KERN_DEBUG args) 28 #else 29 # define DBGA(args...) 30 #endif 31 #if DEBUG_ALLOC > 1 32 # define DBGA2(args...) printk(KERN_DEBUG args) 33 #else 34 # define DBGA2(args...) 35 #endif 36 37 #define DEBUG_NODIRECT 0 38 39 #define ISA_DMA_MASK 0x00ffffff 40 41 static inline unsigned long 42 mk_iommu_pte(unsigned long paddr) 43 { 44 return (paddr >> (PAGE_SHIFT-1)) | 1; 45 } 46 47 /* Return the minimum of MAX or the first power of two larger 48 than main memory. */ 49 50 unsigned long 51 size_for_memory(unsigned long max) 52 { 53 unsigned long mem = max_low_pfn << PAGE_SHIFT; 54 if (mem < max) 55 max = roundup_pow_of_two(mem); 56 return max; 57 } 58 59 struct pci_iommu_arena * __init 60 iommu_arena_new_node(int nid, struct pci_controller *hose, dma_addr_t base, 61 unsigned long window_size, unsigned long align) 62 { 63 unsigned long mem_size; 64 struct pci_iommu_arena *arena; 65 66 mem_size = window_size / (PAGE_SIZE / sizeof(unsigned long)); 67 68 /* Note that the TLB lookup logic uses bitwise concatenation, 69 not addition, so the required arena alignment is based on 70 the size of the window. Retain the align parameter so that 71 particular systems can over-align the arena. */ 72 if (align < mem_size) 73 align = mem_size; 74 75 arena = memblock_alloc_or_panic(sizeof(*arena), SMP_CACHE_BYTES); 76 arena->ptes = memblock_alloc_or_panic(mem_size, align); 77 78 spin_lock_init(&arena->lock); 79 arena->hose = hose; 80 arena->dma_base = base; 81 arena->size = window_size; 82 arena->next_entry = 0; 83 84 /* Align allocations to a multiple of a page size. Not needed 85 unless there are chip bugs. */ 86 arena->align_entry = 1; 87 88 return arena; 89 } 90 91 struct pci_iommu_arena * __init 92 iommu_arena_new(struct pci_controller *hose, dma_addr_t base, 93 unsigned long window_size, unsigned long align) 94 { 95 return iommu_arena_new_node(0, hose, base, window_size, align); 96 } 97 98 /* Must be called with the arena lock held */ 99 static long 100 iommu_arena_find_pages(struct device *dev, struct pci_iommu_arena *arena, 101 long n, long mask) 102 { 103 unsigned long *ptes; 104 long i, p, nent; 105 int pass = 0; 106 unsigned long base; 107 unsigned long boundary_size; 108 109 base = arena->dma_base >> PAGE_SHIFT; 110 boundary_size = dma_get_seg_boundary_nr_pages(dev, PAGE_SHIFT); 111 112 /* Search forward for the first mask-aligned sequence of N free ptes */ 113 ptes = arena->ptes; 114 nent = arena->size >> PAGE_SHIFT; 115 p = ALIGN(arena->next_entry, mask + 1); 116 i = 0; 117 118 again: 119 while (i < n && p+i < nent) { 120 if (!i && iommu_is_span_boundary(p, n, base, boundary_size)) { 121 p = ALIGN(p + 1, mask + 1); 122 goto again; 123 } 124 125 if (ptes[p+i]) { 126 p = ALIGN(p + i + 1, mask + 1); 127 i = 0; 128 } else { 129 i = i + 1; 130 } 131 } 132 133 if (i < n) { 134 if (pass < 1) { 135 /* 136 * Reached the end. Flush the TLB and restart 137 * the search from the beginning. 138 */ 139 alpha_mv.mv_pci_tbi(arena->hose, 0, -1); 140 141 pass++; 142 p = 0; 143 i = 0; 144 goto again; 145 } else 146 return -1; 147 } 148 149 /* Success. It's the responsibility of the caller to mark them 150 in use before releasing the lock */ 151 return p; 152 } 153 154 static long 155 iommu_arena_alloc(struct device *dev, struct pci_iommu_arena *arena, long n, 156 unsigned int align) 157 { 158 unsigned long flags; 159 unsigned long *ptes; 160 long i, p, mask; 161 162 spin_lock_irqsave(&arena->lock, flags); 163 164 /* Search for N empty ptes */ 165 ptes = arena->ptes; 166 mask = max(align, arena->align_entry) - 1; 167 p = iommu_arena_find_pages(dev, arena, n, mask); 168 if (p < 0) { 169 spin_unlock_irqrestore(&arena->lock, flags); 170 return -1; 171 } 172 173 /* Success. Mark them all in use, ie not zero and invalid 174 for the iommu tlb that could load them from under us. 175 The chip specific bits will fill this in with something 176 kosher when we return. */ 177 for (i = 0; i < n; ++i) 178 ptes[p+i] = IOMMU_INVALID_PTE; 179 180 arena->next_entry = p + n; 181 spin_unlock_irqrestore(&arena->lock, flags); 182 183 return p; 184 } 185 186 static void 187 iommu_arena_free(struct pci_iommu_arena *arena, long ofs, long n) 188 { 189 unsigned long *p; 190 long i; 191 192 p = arena->ptes + ofs; 193 for (i = 0; i < n; ++i) 194 p[i] = 0; 195 } 196 197 /* 198 * True if the machine supports DAC addressing, and DEV can 199 * make use of it given MASK. 200 */ 201 static int pci_dac_dma_supported(struct pci_dev *dev, u64 mask) 202 { 203 dma_addr_t dac_offset = alpha_mv.pci_dac_offset; 204 int ok = 1; 205 206 /* If this is not set, the machine doesn't support DAC at all. */ 207 if (dac_offset == 0) 208 ok = 0; 209 210 /* The device has to be able to address our DAC bit. */ 211 if ((dac_offset & dev->dma_mask) != dac_offset) 212 ok = 0; 213 214 /* If both conditions above are met, we are fine. */ 215 DBGA("pci_dac_dma_supported %s from %ps\n", 216 str_yes_no(ok), __builtin_return_address(0)); 217 218 return ok; 219 } 220 221 /* Map a single buffer of the indicated size for PCI DMA in streaming 222 mode. The 32-bit PCI bus mastering address to use is returned. 223 Once the device is given the dma address, the device owns this memory 224 until either pci_unmap_single or pci_dma_sync_single is performed. */ 225 226 static dma_addr_t 227 pci_map_single_1(struct pci_dev *pdev, void *cpu_addr, size_t size, 228 int dac_allowed) 229 { 230 struct pci_controller *hose = pdev ? pdev->sysdata : pci_isa_hose; 231 dma_addr_t max_dma = pdev ? pdev->dma_mask : ISA_DMA_MASK; 232 struct pci_iommu_arena *arena; 233 long npages, dma_ofs, i; 234 unsigned long paddr; 235 dma_addr_t ret; 236 unsigned int align = 0; 237 struct device *dev = pdev ? &pdev->dev : NULL; 238 239 paddr = __pa(cpu_addr); 240 241 #if !DEBUG_NODIRECT 242 /* First check to see if we can use the direct map window. */ 243 if (paddr + size + __direct_map_base - 1 <= max_dma 244 && paddr + size <= __direct_map_size) { 245 ret = paddr + __direct_map_base; 246 247 DBGA2("pci_map_single: [%p,%zx] -> direct %llx from %ps\n", 248 cpu_addr, size, ret, __builtin_return_address(0)); 249 250 return ret; 251 } 252 #endif 253 254 /* Next, use DAC if selected earlier. */ 255 if (dac_allowed) { 256 ret = paddr + alpha_mv.pci_dac_offset; 257 258 DBGA2("pci_map_single: [%p,%zx] -> DAC %llx from %ps\n", 259 cpu_addr, size, ret, __builtin_return_address(0)); 260 261 return ret; 262 } 263 264 /* If the machine doesn't define a pci_tbi routine, we have to 265 assume it doesn't support sg mapping, and, since we tried to 266 use direct_map above, it now must be considered an error. */ 267 if (! alpha_mv.mv_pci_tbi) { 268 printk_once(KERN_WARNING "pci_map_single: no HW sg\n"); 269 return DMA_MAPPING_ERROR; 270 } 271 272 arena = hose->sg_pci; 273 if (!arena || arena->dma_base + arena->size - 1 > max_dma) 274 arena = hose->sg_isa; 275 276 npages = iommu_num_pages(paddr, size, PAGE_SIZE); 277 278 /* Force allocation to 64KB boundary for ISA bridges. */ 279 if (pdev && pdev == isa_bridge) 280 align = 8; 281 dma_ofs = iommu_arena_alloc(dev, arena, npages, align); 282 if (dma_ofs < 0) { 283 printk(KERN_WARNING "pci_map_single failed: " 284 "could not allocate dma page tables\n"); 285 return DMA_MAPPING_ERROR; 286 } 287 288 paddr &= PAGE_MASK; 289 for (i = 0; i < npages; ++i, paddr += PAGE_SIZE) 290 arena->ptes[i + dma_ofs] = mk_iommu_pte(paddr); 291 292 ret = arena->dma_base + dma_ofs * PAGE_SIZE; 293 ret += (unsigned long)cpu_addr & ~PAGE_MASK; 294 295 DBGA2("pci_map_single: [%p,%zx] np %ld -> sg %llx from %ps\n", 296 cpu_addr, size, npages, ret, __builtin_return_address(0)); 297 298 return ret; 299 } 300 301 /* Helper for generic DMA-mapping functions. */ 302 static struct pci_dev *alpha_gendev_to_pci(struct device *dev) 303 { 304 if (dev && dev_is_pci(dev)) 305 return to_pci_dev(dev); 306 307 /* Assume that non-PCI devices asking for DMA are either ISA or EISA, 308 BUG() otherwise. */ 309 BUG_ON(!isa_bridge); 310 311 /* Assume non-busmaster ISA DMA when dma_mask is not set (the ISA 312 bridge is bus master then). */ 313 if (!dev || !dev->dma_mask || !*dev->dma_mask) 314 return isa_bridge; 315 316 /* For EISA bus masters, return isa_bridge (it might have smaller 317 dma_mask due to wiring limitations). */ 318 if (*dev->dma_mask >= isa_bridge->dma_mask) 319 return isa_bridge; 320 321 /* This assumes ISA bus master with dma_mask 0xffffff. */ 322 return NULL; 323 } 324 325 static dma_addr_t alpha_pci_map_page(struct device *dev, struct page *page, 326 unsigned long offset, size_t size, 327 enum dma_data_direction dir, 328 unsigned long attrs) 329 { 330 struct pci_dev *pdev = alpha_gendev_to_pci(dev); 331 int dac_allowed; 332 333 BUG_ON(dir == DMA_NONE); 334 335 dac_allowed = pdev ? pci_dac_dma_supported(pdev, pdev->dma_mask) : 0; 336 return pci_map_single_1(pdev, (char *)page_address(page) + offset, 337 size, dac_allowed); 338 } 339 340 /* Unmap a single streaming mode DMA translation. The DMA_ADDR and 341 SIZE must match what was provided for in a previous pci_map_single 342 call. All other usages are undefined. After this call, reads by 343 the cpu to the buffer are guaranteed to see whatever the device 344 wrote there. */ 345 346 static void alpha_pci_unmap_page(struct device *dev, dma_addr_t dma_addr, 347 size_t size, enum dma_data_direction dir, 348 unsigned long attrs) 349 { 350 unsigned long flags; 351 struct pci_dev *pdev = alpha_gendev_to_pci(dev); 352 struct pci_controller *hose = pdev ? pdev->sysdata : pci_isa_hose; 353 struct pci_iommu_arena *arena; 354 long dma_ofs, npages; 355 356 BUG_ON(dir == DMA_NONE); 357 358 if (dma_addr >= __direct_map_base 359 && dma_addr < __direct_map_base + __direct_map_size) { 360 /* Nothing to do. */ 361 362 DBGA2("pci_unmap_single: direct [%llx,%zx] from %ps\n", 363 dma_addr, size, __builtin_return_address(0)); 364 365 return; 366 } 367 368 if (dma_addr > 0xffffffff) { 369 DBGA2("pci64_unmap_single: DAC [%llx,%zx] from %ps\n", 370 dma_addr, size, __builtin_return_address(0)); 371 return; 372 } 373 374 arena = hose->sg_pci; 375 if (!arena || dma_addr < arena->dma_base) 376 arena = hose->sg_isa; 377 378 dma_ofs = (dma_addr - arena->dma_base) >> PAGE_SHIFT; 379 if (dma_ofs * PAGE_SIZE >= arena->size) { 380 printk(KERN_ERR "Bogus pci_unmap_single: dma_addr %llx " 381 " base %llx size %x\n", 382 dma_addr, arena->dma_base, arena->size); 383 return; 384 BUG(); 385 } 386 387 npages = iommu_num_pages(dma_addr, size, PAGE_SIZE); 388 389 spin_lock_irqsave(&arena->lock, flags); 390 391 iommu_arena_free(arena, dma_ofs, npages); 392 393 /* If we're freeing ptes above the `next_entry' pointer (they 394 may have snuck back into the TLB since the last wrap flush), 395 we need to flush the TLB before reallocating the latter. */ 396 if (dma_ofs >= arena->next_entry) 397 alpha_mv.mv_pci_tbi(hose, dma_addr, dma_addr + size - 1); 398 399 spin_unlock_irqrestore(&arena->lock, flags); 400 401 DBGA2("pci_unmap_single: sg [%llx,%zx] np %ld from %ps\n", 402 dma_addr, size, npages, __builtin_return_address(0)); 403 } 404 405 /* Allocate and map kernel buffer using consistent mode DMA for PCI 406 device. Returns non-NULL cpu-view pointer to the buffer if 407 successful and sets *DMA_ADDRP to the pci side dma address as well, 408 else DMA_ADDRP is undefined. */ 409 410 static void *alpha_pci_alloc_coherent(struct device *dev, size_t size, 411 dma_addr_t *dma_addrp, gfp_t gfp, 412 unsigned long attrs) 413 { 414 struct pci_dev *pdev = alpha_gendev_to_pci(dev); 415 void *cpu_addr; 416 long order = get_order(size); 417 418 gfp &= ~GFP_DMA; 419 420 try_again: 421 cpu_addr = (void *)__get_free_pages(gfp | __GFP_ZERO, order); 422 if (! cpu_addr) { 423 printk(KERN_INFO "pci_alloc_consistent: " 424 "get_free_pages failed from %ps\n", 425 __builtin_return_address(0)); 426 /* ??? Really atomic allocation? Otherwise we could play 427 with vmalloc and sg if we can't find contiguous memory. */ 428 return NULL; 429 } 430 memset(cpu_addr, 0, size); 431 432 *dma_addrp = pci_map_single_1(pdev, cpu_addr, size, 0); 433 if (*dma_addrp == DMA_MAPPING_ERROR) { 434 free_pages((unsigned long)cpu_addr, order); 435 if (alpha_mv.mv_pci_tbi || (gfp & GFP_DMA)) 436 return NULL; 437 /* The address doesn't fit required mask and we 438 do not have iommu. Try again with GFP_DMA. */ 439 gfp |= GFP_DMA; 440 goto try_again; 441 } 442 443 DBGA2("pci_alloc_consistent: %zx -> [%p,%llx] from %ps\n", 444 size, cpu_addr, *dma_addrp, __builtin_return_address(0)); 445 446 return cpu_addr; 447 } 448 449 /* Free and unmap a consistent DMA buffer. CPU_ADDR and DMA_ADDR must 450 be values that were returned from pci_alloc_consistent. SIZE must 451 be the same as what as passed into pci_alloc_consistent. 452 References to the memory and mappings associated with CPU_ADDR or 453 DMA_ADDR past this call are illegal. */ 454 455 static void alpha_pci_free_coherent(struct device *dev, size_t size, 456 void *cpu_addr, dma_addr_t dma_addr, 457 unsigned long attrs) 458 { 459 struct pci_dev *pdev = alpha_gendev_to_pci(dev); 460 dma_unmap_single(&pdev->dev, dma_addr, size, DMA_BIDIRECTIONAL); 461 free_pages((unsigned long)cpu_addr, get_order(size)); 462 463 DBGA2("pci_free_consistent: [%llx,%zx] from %ps\n", 464 dma_addr, size, __builtin_return_address(0)); 465 } 466 467 /* Classify the elements of the scatterlist. Write dma_address 468 of each element with: 469 0 : Followers all physically adjacent. 470 1 : Followers all virtually adjacent. 471 -1 : Not leader, physically adjacent to previous. 472 -2 : Not leader, virtually adjacent to previous. 473 Write dma_length of each leader with the combined lengths of 474 the mergable followers. */ 475 476 #define SG_ENT_VIRT_ADDRESS(SG) (sg_virt((SG))) 477 #define SG_ENT_PHYS_ADDRESS(SG) __pa(SG_ENT_VIRT_ADDRESS(SG)) 478 479 static void 480 sg_classify(struct device *dev, struct scatterlist *sg, struct scatterlist *end, 481 int virt_ok) 482 { 483 unsigned long next_paddr; 484 struct scatterlist *leader; 485 long leader_flag, leader_length; 486 unsigned int max_seg_size; 487 488 leader = sg; 489 leader_flag = 0; 490 leader_length = leader->length; 491 next_paddr = SG_ENT_PHYS_ADDRESS(leader) + leader_length; 492 493 /* we will not marge sg without device. */ 494 max_seg_size = dev ? dma_get_max_seg_size(dev) : 0; 495 for (++sg; sg < end; ++sg) { 496 unsigned long addr, len; 497 addr = SG_ENT_PHYS_ADDRESS(sg); 498 len = sg->length; 499 500 if (leader_length + len > max_seg_size) 501 goto new_segment; 502 503 if (next_paddr == addr) { 504 sg->dma_address = -1; 505 leader_length += len; 506 } else if (((next_paddr | addr) & ~PAGE_MASK) == 0 && virt_ok) { 507 sg->dma_address = -2; 508 leader_flag = 1; 509 leader_length += len; 510 } else { 511 new_segment: 512 leader->dma_address = leader_flag; 513 leader->dma_length = leader_length; 514 leader = sg; 515 leader_flag = 0; 516 leader_length = len; 517 } 518 519 next_paddr = addr + len; 520 } 521 522 leader->dma_address = leader_flag; 523 leader->dma_length = leader_length; 524 } 525 526 /* Given a scatterlist leader, choose an allocation method and fill 527 in the blanks. */ 528 529 static int 530 sg_fill(struct device *dev, struct scatterlist *leader, struct scatterlist *end, 531 struct scatterlist *out, struct pci_iommu_arena *arena, 532 dma_addr_t max_dma, int dac_allowed) 533 { 534 unsigned long paddr = SG_ENT_PHYS_ADDRESS(leader); 535 long size = leader->dma_length; 536 struct scatterlist *sg; 537 unsigned long *ptes; 538 long npages, dma_ofs, i; 539 540 #if !DEBUG_NODIRECT 541 /* If everything is physically contiguous, and the addresses 542 fall into the direct-map window, use it. */ 543 if (leader->dma_address == 0 544 && paddr + size + __direct_map_base - 1 <= max_dma 545 && paddr + size <= __direct_map_size) { 546 out->dma_address = paddr + __direct_map_base; 547 out->dma_length = size; 548 549 DBGA(" sg_fill: [%p,%lx] -> direct %llx\n", 550 __va(paddr), size, out->dma_address); 551 552 return 0; 553 } 554 #endif 555 556 /* If physically contiguous and DAC is available, use it. */ 557 if (leader->dma_address == 0 && dac_allowed) { 558 out->dma_address = paddr + alpha_mv.pci_dac_offset; 559 out->dma_length = size; 560 561 DBGA(" sg_fill: [%p,%lx] -> DAC %llx\n", 562 __va(paddr), size, out->dma_address); 563 564 return 0; 565 } 566 567 /* Otherwise, we'll use the iommu to make the pages virtually 568 contiguous. */ 569 570 paddr &= ~PAGE_MASK; 571 npages = iommu_num_pages(paddr, size, PAGE_SIZE); 572 dma_ofs = iommu_arena_alloc(dev, arena, npages, 0); 573 if (dma_ofs < 0) { 574 /* If we attempted a direct map above but failed, die. */ 575 if (leader->dma_address == 0) 576 return -1; 577 578 /* Otherwise, break up the remaining virtually contiguous 579 hunks into individual direct maps and retry. */ 580 sg_classify(dev, leader, end, 0); 581 return sg_fill(dev, leader, end, out, arena, max_dma, dac_allowed); 582 } 583 584 out->dma_address = arena->dma_base + dma_ofs*PAGE_SIZE + paddr; 585 out->dma_length = size; 586 587 DBGA(" sg_fill: [%p,%lx] -> sg %llx np %ld\n", 588 __va(paddr), size, out->dma_address, npages); 589 590 /* All virtually contiguous. We need to find the length of each 591 physically contiguous subsegment to fill in the ptes. */ 592 ptes = &arena->ptes[dma_ofs]; 593 sg = leader; 594 do { 595 #if DEBUG_ALLOC > 0 596 struct scatterlist *last_sg = sg; 597 #endif 598 599 size = sg->length; 600 paddr = SG_ENT_PHYS_ADDRESS(sg); 601 602 while (sg+1 < end && (int) sg[1].dma_address == -1) { 603 size += sg[1].length; 604 sg = sg_next(sg); 605 } 606 607 npages = iommu_num_pages(paddr, size, PAGE_SIZE); 608 609 paddr &= PAGE_MASK; 610 for (i = 0; i < npages; ++i, paddr += PAGE_SIZE) 611 *ptes++ = mk_iommu_pte(paddr); 612 613 #if DEBUG_ALLOC > 0 614 DBGA(" (%ld) [%p,%x] np %ld\n", 615 last_sg - leader, SG_ENT_VIRT_ADDRESS(last_sg), 616 last_sg->length, npages); 617 while (++last_sg <= sg) { 618 DBGA(" (%ld) [%p,%x] cont\n", 619 last_sg - leader, SG_ENT_VIRT_ADDRESS(last_sg), 620 last_sg->length); 621 } 622 #endif 623 } while (++sg < end && (int) sg->dma_address < 0); 624 625 return 1; 626 } 627 628 static int alpha_pci_map_sg(struct device *dev, struct scatterlist *sg, 629 int nents, enum dma_data_direction dir, 630 unsigned long attrs) 631 { 632 struct pci_dev *pdev = alpha_gendev_to_pci(dev); 633 struct scatterlist *start, *end, *out; 634 struct pci_controller *hose; 635 struct pci_iommu_arena *arena; 636 dma_addr_t max_dma; 637 int dac_allowed; 638 639 BUG_ON(dir == DMA_NONE); 640 641 dac_allowed = dev ? pci_dac_dma_supported(pdev, pdev->dma_mask) : 0; 642 643 /* Fast path single entry scatterlists. */ 644 if (nents == 1) { 645 sg->dma_length = sg->length; 646 sg->dma_address 647 = pci_map_single_1(pdev, SG_ENT_VIRT_ADDRESS(sg), 648 sg->length, dac_allowed); 649 if (sg->dma_address == DMA_MAPPING_ERROR) 650 return -EIO; 651 return 1; 652 } 653 654 start = sg; 655 end = sg + nents; 656 657 /* First, prepare information about the entries. */ 658 sg_classify(dev, sg, end, alpha_mv.mv_pci_tbi != 0); 659 660 /* Second, figure out where we're going to map things. */ 661 if (alpha_mv.mv_pci_tbi) { 662 hose = pdev ? pdev->sysdata : pci_isa_hose; 663 max_dma = pdev ? pdev->dma_mask : ISA_DMA_MASK; 664 arena = hose->sg_pci; 665 if (!arena || arena->dma_base + arena->size - 1 > max_dma) 666 arena = hose->sg_isa; 667 } else { 668 max_dma = -1; 669 arena = NULL; 670 hose = NULL; 671 } 672 673 /* Third, iterate over the scatterlist leaders and allocate 674 dma space as needed. */ 675 for (out = sg; sg < end; ++sg) { 676 if ((int) sg->dma_address < 0) 677 continue; 678 if (sg_fill(dev, sg, end, out, arena, max_dma, dac_allowed) < 0) 679 goto error; 680 out++; 681 } 682 683 /* Mark the end of the list for pci_unmap_sg. */ 684 if (out < end) 685 out->dma_length = 0; 686 687 if (out - start == 0) { 688 printk(KERN_WARNING "pci_map_sg failed: no entries?\n"); 689 return -ENOMEM; 690 } 691 DBGA("pci_map_sg: %ld entries\n", out - start); 692 693 return out - start; 694 695 error: 696 printk(KERN_WARNING "pci_map_sg failed: " 697 "could not allocate dma page tables\n"); 698 699 /* Some allocation failed while mapping the scatterlist 700 entries. Unmap them now. */ 701 if (out > start) 702 dma_unmap_sg(&pdev->dev, start, out - start, dir); 703 return -ENOMEM; 704 } 705 706 /* Unmap a set of streaming mode DMA translations. Again, cpu read 707 rules concerning calls here are the same as for pci_unmap_single() 708 above. */ 709 710 static void alpha_pci_unmap_sg(struct device *dev, struct scatterlist *sg, 711 int nents, enum dma_data_direction dir, 712 unsigned long attrs) 713 { 714 struct pci_dev *pdev = alpha_gendev_to_pci(dev); 715 unsigned long flags; 716 struct pci_controller *hose; 717 struct pci_iommu_arena *arena; 718 struct scatterlist *end; 719 dma_addr_t max_dma; 720 dma_addr_t fbeg, fend; 721 722 BUG_ON(dir == DMA_NONE); 723 724 if (! alpha_mv.mv_pci_tbi) 725 return; 726 727 hose = pdev ? pdev->sysdata : pci_isa_hose; 728 max_dma = pdev ? pdev->dma_mask : ISA_DMA_MASK; 729 arena = hose->sg_pci; 730 if (!arena || arena->dma_base + arena->size - 1 > max_dma) 731 arena = hose->sg_isa; 732 733 fbeg = -1, fend = 0; 734 735 spin_lock_irqsave(&arena->lock, flags); 736 737 for (end = sg + nents; sg < end; ++sg) { 738 dma_addr_t addr; 739 size_t size; 740 long npages, ofs; 741 dma_addr_t tend; 742 743 addr = sg->dma_address; 744 size = sg->dma_length; 745 if (!size) 746 break; 747 748 if (addr > 0xffffffff) { 749 /* It's a DAC address -- nothing to do. */ 750 DBGA(" (%ld) DAC [%llx,%zx]\n", 751 sg - end + nents, addr, size); 752 continue; 753 } 754 755 if (addr >= __direct_map_base 756 && addr < __direct_map_base + __direct_map_size) { 757 /* Nothing to do. */ 758 DBGA(" (%ld) direct [%llx,%zx]\n", 759 sg - end + nents, addr, size); 760 continue; 761 } 762 763 DBGA(" (%ld) sg [%llx,%zx]\n", 764 sg - end + nents, addr, size); 765 766 npages = iommu_num_pages(addr, size, PAGE_SIZE); 767 ofs = (addr - arena->dma_base) >> PAGE_SHIFT; 768 iommu_arena_free(arena, ofs, npages); 769 770 tend = addr + size - 1; 771 if (fbeg > addr) fbeg = addr; 772 if (fend < tend) fend = tend; 773 } 774 775 /* If we're freeing ptes above the `next_entry' pointer (they 776 may have snuck back into the TLB since the last wrap flush), 777 we need to flush the TLB before reallocating the latter. */ 778 if ((fend - arena->dma_base) >> PAGE_SHIFT >= arena->next_entry) 779 alpha_mv.mv_pci_tbi(hose, fbeg, fend); 780 781 spin_unlock_irqrestore(&arena->lock, flags); 782 783 DBGA("pci_unmap_sg: %ld entries\n", nents - (end - sg)); 784 } 785 786 /* Return whether the given PCI device DMA address mask can be 787 supported properly. */ 788 789 static int alpha_pci_supported(struct device *dev, u64 mask) 790 { 791 struct pci_dev *pdev = alpha_gendev_to_pci(dev); 792 struct pci_controller *hose; 793 struct pci_iommu_arena *arena; 794 795 /* If there exists a direct map, and the mask fits either 796 the entire direct mapped space or the total system memory as 797 shifted by the map base */ 798 if (__direct_map_size != 0 799 && (__direct_map_base + __direct_map_size - 1 <= mask || 800 __direct_map_base + (max_low_pfn << PAGE_SHIFT) - 1 <= mask)) 801 return 1; 802 803 /* Check that we have a scatter-gather arena that fits. */ 804 hose = pdev ? pdev->sysdata : pci_isa_hose; 805 arena = hose->sg_isa; 806 if (arena && arena->dma_base + arena->size - 1 <= mask) 807 return 1; 808 arena = hose->sg_pci; 809 if (arena && arena->dma_base + arena->size - 1 <= mask) 810 return 1; 811 812 /* As last resort try ZONE_DMA. */ 813 if (!__direct_map_base && MAX_DMA_ADDRESS - IDENT_ADDR - 1 <= mask) 814 return 1; 815 816 return 0; 817 } 818 819 820 /* 821 * AGP GART extensions to the IOMMU 822 */ 823 int 824 iommu_reserve(struct pci_iommu_arena *arena, long pg_count, long align_mask) 825 { 826 unsigned long flags; 827 unsigned long *ptes; 828 long i, p; 829 830 if (!arena) return -EINVAL; 831 832 spin_lock_irqsave(&arena->lock, flags); 833 834 /* Search for N empty ptes. */ 835 ptes = arena->ptes; 836 p = iommu_arena_find_pages(NULL, arena, pg_count, align_mask); 837 if (p < 0) { 838 spin_unlock_irqrestore(&arena->lock, flags); 839 return -1; 840 } 841 842 /* Success. Mark them all reserved (ie not zero and invalid) 843 for the iommu tlb that could load them from under us. 844 They will be filled in with valid bits by _bind() */ 845 for (i = 0; i < pg_count; ++i) 846 ptes[p+i] = IOMMU_RESERVED_PTE; 847 848 arena->next_entry = p + pg_count; 849 spin_unlock_irqrestore(&arena->lock, flags); 850 851 return p; 852 } 853 854 int 855 iommu_release(struct pci_iommu_arena *arena, long pg_start, long pg_count) 856 { 857 unsigned long *ptes; 858 long i; 859 860 if (!arena) return -EINVAL; 861 862 ptes = arena->ptes; 863 864 /* Make sure they're all reserved first... */ 865 for(i = pg_start; i < pg_start + pg_count; i++) 866 if (ptes[i] != IOMMU_RESERVED_PTE) 867 return -EBUSY; 868 869 iommu_arena_free(arena, pg_start, pg_count); 870 return 0; 871 } 872 873 int 874 iommu_bind(struct pci_iommu_arena *arena, long pg_start, long pg_count, 875 struct page **pages) 876 { 877 unsigned long flags; 878 unsigned long *ptes; 879 long i, j; 880 881 if (!arena) return -EINVAL; 882 883 spin_lock_irqsave(&arena->lock, flags); 884 885 ptes = arena->ptes; 886 887 for(j = pg_start; j < pg_start + pg_count; j++) { 888 if (ptes[j] != IOMMU_RESERVED_PTE) { 889 spin_unlock_irqrestore(&arena->lock, flags); 890 return -EBUSY; 891 } 892 } 893 894 for(i = 0, j = pg_start; i < pg_count; i++, j++) 895 ptes[j] = mk_iommu_pte(page_to_phys(pages[i])); 896 897 spin_unlock_irqrestore(&arena->lock, flags); 898 899 return 0; 900 } 901 902 int 903 iommu_unbind(struct pci_iommu_arena *arena, long pg_start, long pg_count) 904 { 905 unsigned long *p; 906 long i; 907 908 if (!arena) return -EINVAL; 909 910 p = arena->ptes + pg_start; 911 for(i = 0; i < pg_count; i++) 912 p[i] = IOMMU_RESERVED_PTE; 913 914 return 0; 915 } 916 917 const struct dma_map_ops alpha_pci_ops = { 918 .alloc = alpha_pci_alloc_coherent, 919 .free = alpha_pci_free_coherent, 920 .map_page = alpha_pci_map_page, 921 .unmap_page = alpha_pci_unmap_page, 922 .map_sg = alpha_pci_map_sg, 923 .unmap_sg = alpha_pci_unmap_sg, 924 .dma_supported = alpha_pci_supported, 925 .mmap = dma_common_mmap, 926 .get_sgtable = dma_common_get_sgtable, 927 .alloc_pages_op = dma_common_alloc_pages, 928 .free_pages = dma_common_free_pages, 929 }; 930 EXPORT_SYMBOL(alpha_pci_ops); 931