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