Automatic merge of rsync://rsync.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6

+92 -32
+68 -20
arch/sparc64/kernel/pci_iommu.c
··· 196 196 return NULL; 197 197 } 198 198 199 + static int iommu_alloc_ctx(struct pci_iommu *iommu) 200 + { 201 + int lowest = iommu->ctx_lowest_free; 202 + int sz = IOMMU_NUM_CTXS - lowest; 203 + int n = find_next_zero_bit(iommu->ctx_bitmap, sz, lowest); 204 + 205 + if (unlikely(n == sz)) { 206 + n = find_next_zero_bit(iommu->ctx_bitmap, lowest, 1); 207 + if (unlikely(n == lowest)) { 208 + printk(KERN_WARNING "IOMMU: Ran out of contexts.\n"); 209 + n = 0; 210 + } 211 + } 212 + if (n) 213 + __set_bit(n, iommu->ctx_bitmap); 214 + 215 + return n; 216 + } 217 + 218 + static inline void iommu_free_ctx(struct pci_iommu *iommu, int ctx) 219 + { 220 + if (likely(ctx)) { 221 + __clear_bit(ctx, iommu->ctx_bitmap); 222 + if (ctx < iommu->ctx_lowest_free) 223 + iommu->ctx_lowest_free = ctx; 224 + } 225 + } 226 + 199 227 /* Allocate and map kernel buffer of size SIZE using consistent mode 200 228 * DMA for PCI device PDEV. Return non-NULL cpu-side address if 201 229 * successful and set *DMA_ADDRP to the PCI side dma address. ··· 264 236 npages = size >> IO_PAGE_SHIFT; 265 237 ctx = 0; 266 238 if (iommu->iommu_ctxflush) 267 - ctx = iommu->iommu_cur_ctx++; 239 + ctx = iommu_alloc_ctx(iommu); 268 240 first_page = __pa(first_page); 269 241 while (npages--) { 270 242 iopte_val(*iopte) = (IOPTE_CONSISTENT(ctx) | ··· 345 317 } 346 318 } 347 319 320 + iommu_free_ctx(iommu, ctx); 321 + 348 322 spin_unlock_irqrestore(&iommu->lock, flags); 349 323 350 324 order = get_order(size); ··· 390 360 base_paddr = __pa(oaddr & IO_PAGE_MASK); 391 361 ctx = 0; 392 362 if (iommu->iommu_ctxflush) 393 - ctx = iommu->iommu_cur_ctx++; 363 + ctx = iommu_alloc_ctx(iommu); 394 364 if (strbuf->strbuf_enabled) 395 365 iopte_protection = IOPTE_STREAMING(ctx); 396 366 else ··· 410 380 return PCI_DMA_ERROR_CODE; 411 381 } 412 382 413 - static void pci_strbuf_flush(struct pci_strbuf *strbuf, struct pci_iommu *iommu, u32 vaddr, unsigned long ctx, unsigned long npages) 383 + static void pci_strbuf_flush(struct pci_strbuf *strbuf, struct pci_iommu *iommu, u32 vaddr, unsigned long ctx, unsigned long npages, int direction) 414 384 { 415 385 int limit; 416 386 417 - PCI_STC_FLUSHFLAG_INIT(strbuf); 418 387 if (strbuf->strbuf_ctxflush && 419 388 iommu->iommu_ctxflush) { 420 389 unsigned long matchreg, flushreg; 390 + u64 val; 421 391 422 392 flushreg = strbuf->strbuf_ctxflush; 423 393 matchreg = PCI_STC_CTXMATCH_ADDR(strbuf, ctx); 424 394 425 - limit = 100000; 426 395 pci_iommu_write(flushreg, ctx); 427 - for(;;) { 428 - if (((long)pci_iommu_read(matchreg)) >= 0L) 429 - break; 430 - limit--; 431 - if (!limit) 432 - break; 433 - udelay(1); 396 + val = pci_iommu_read(matchreg); 397 + val &= 0xffff; 398 + if (!val) 399 + goto do_flush_sync; 400 + 401 + while (val) { 402 + if (val & 0x1) 403 + pci_iommu_write(flushreg, ctx); 404 + val >>= 1; 434 405 } 435 - if (!limit) 406 + val = pci_iommu_read(matchreg); 407 + if (unlikely(val)) { 436 408 printk(KERN_WARNING "pci_strbuf_flush: ctx flush " 437 - "timeout vaddr[%08x] ctx[%lx]\n", 438 - vaddr, ctx); 409 + "timeout matchreg[%lx] ctx[%lx]\n", 410 + val, ctx); 411 + goto do_page_flush; 412 + } 439 413 } else { 440 414 unsigned long i; 441 415 416 + do_page_flush: 442 417 for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE) 443 418 pci_iommu_write(strbuf->strbuf_pflush, vaddr); 444 419 } 445 420 421 + do_flush_sync: 422 + /* If the device could not have possibly put dirty data into 423 + * the streaming cache, no flush-flag synchronization needs 424 + * to be performed. 425 + */ 426 + if (direction == PCI_DMA_TODEVICE) 427 + return; 428 + 429 + PCI_STC_FLUSHFLAG_INIT(strbuf); 446 430 pci_iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa); 447 431 (void) pci_iommu_read(iommu->write_complete_reg); 448 432 ··· 510 466 511 467 /* Step 1: Kick data out of streaming buffers if necessary. */ 512 468 if (strbuf->strbuf_enabled) 513 - pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages); 469 + pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction); 514 470 515 471 /* Step 2: Clear out first TSB entry. */ 516 472 iopte_make_dummy(iommu, base); 517 473 518 474 free_streaming_cluster(iommu, bus_addr - iommu->page_table_map_base, 519 475 npages, ctx); 476 + 477 + iommu_free_ctx(iommu, ctx); 520 478 521 479 spin_unlock_irqrestore(&iommu->lock, flags); 522 480 } ··· 659 613 /* Step 4: Choose a context if necessary. */ 660 614 ctx = 0; 661 615 if (iommu->iommu_ctxflush) 662 - ctx = iommu->iommu_cur_ctx++; 616 + ctx = iommu_alloc_ctx(iommu); 663 617 664 618 /* Step 5: Create the mappings. */ 665 619 if (strbuf->strbuf_enabled) ··· 724 678 725 679 /* Step 1: Kick data out of streaming buffers if necessary. */ 726 680 if (strbuf->strbuf_enabled) 727 - pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages); 681 + pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction); 728 682 729 683 /* Step 2: Clear out first TSB entry. */ 730 684 iopte_make_dummy(iommu, base); 731 685 732 686 free_streaming_cluster(iommu, bus_addr - iommu->page_table_map_base, 733 687 npages, ctx); 688 + 689 + iommu_free_ctx(iommu, ctx); 734 690 735 691 spin_unlock_irqrestore(&iommu->lock, flags); 736 692 } ··· 772 724 } 773 725 774 726 /* Step 2: Kick data out of streaming buffers. */ 775 - pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages); 727 + pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction); 776 728 777 729 spin_unlock_irqrestore(&iommu->lock, flags); 778 730 } ··· 816 768 i--; 817 769 npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) 818 770 - bus_addr) >> IO_PAGE_SHIFT; 819 - pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages); 771 + pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction); 820 772 821 773 spin_unlock_irqrestore(&iommu->lock, flags); 822 774 }
+1 -1
arch/sparc64/kernel/pci_psycho.c
··· 1212 1212 1213 1213 /* Setup initial software IOMMU state. */ 1214 1214 spin_lock_init(&iommu->lock); 1215 - iommu->iommu_cur_ctx = 0; 1215 + iommu->ctx_lowest_free = 1; 1216 1216 1217 1217 /* Register addresses. */ 1218 1218 iommu->iommu_control = p->pbm_A.controller_regs + PSYCHO_IOMMU_CONTROL;
+1 -1
arch/sparc64/kernel/pci_sabre.c
··· 1265 1265 1266 1266 /* Setup initial software IOMMU state. */ 1267 1267 spin_lock_init(&iommu->lock); 1268 - iommu->iommu_cur_ctx = 0; 1268 + iommu->ctx_lowest_free = 1; 1269 1269 1270 1270 /* Register addresses. */ 1271 1271 iommu->iommu_control = p->pbm_A.controller_regs + SABRE_IOMMU_CONTROL;
+1 -1
arch/sparc64/kernel/pci_schizo.c
··· 1753 1753 1754 1754 /* Setup initial software IOMMU state. */ 1755 1755 spin_lock_init(&iommu->lock); 1756 - iommu->iommu_cur_ctx = 0; 1756 + iommu->ctx_lowest_free = 1; 1757 1757 1758 1758 /* Register addresses, SCHIZO has iommu ctx flushing. */ 1759 1759 iommu->iommu_control = pbm->pbm_regs + SCHIZO_IOMMU_CONTROL;
+14 -6
arch/sparc64/kernel/sbus.c
··· 117 117 118 118 #define STRBUF_TAG_VALID 0x02UL 119 119 120 - static void sbus_strbuf_flush(struct sbus_iommu *iommu, u32 base, unsigned long npages) 120 + static void sbus_strbuf_flush(struct sbus_iommu *iommu, u32 base, unsigned long npages, int direction) 121 121 { 122 122 unsigned long n; 123 123 int limit; 124 124 125 - iommu->strbuf_flushflag = 0UL; 126 125 n = npages; 127 126 while (n--) 128 127 upa_writeq(base + (n << IO_PAGE_SHIFT), 129 128 iommu->strbuf_regs + STRBUF_PFLUSH); 129 + 130 + /* If the device could not have possibly put dirty data into 131 + * the streaming cache, no flush-flag synchronization needs 132 + * to be performed. 133 + */ 134 + if (direction == SBUS_DMA_TODEVICE) 135 + return; 136 + 137 + iommu->strbuf_flushflag = 0UL; 130 138 131 139 /* Whoopee cushion! */ 132 140 upa_writeq(__pa(&iommu->strbuf_flushflag), ··· 429 421 430 422 spin_lock_irqsave(&iommu->lock, flags); 431 423 free_streaming_cluster(iommu, dma_base, size >> IO_PAGE_SHIFT); 432 - sbus_strbuf_flush(iommu, dma_base, size >> IO_PAGE_SHIFT); 424 + sbus_strbuf_flush(iommu, dma_base, size >> IO_PAGE_SHIFT, direction); 433 425 spin_unlock_irqrestore(&iommu->lock, flags); 434 426 } 435 427 ··· 592 584 iommu = sdev->bus->iommu; 593 585 spin_lock_irqsave(&iommu->lock, flags); 594 586 free_streaming_cluster(iommu, dvma_base, size >> IO_PAGE_SHIFT); 595 - sbus_strbuf_flush(iommu, dvma_base, size >> IO_PAGE_SHIFT); 587 + sbus_strbuf_flush(iommu, dvma_base, size >> IO_PAGE_SHIFT, direction); 596 588 spin_unlock_irqrestore(&iommu->lock, flags); 597 589 } 598 590 ··· 604 596 size = (IO_PAGE_ALIGN(base + size) - (base & IO_PAGE_MASK)); 605 597 606 598 spin_lock_irqsave(&iommu->lock, flags); 607 - sbus_strbuf_flush(iommu, base & IO_PAGE_MASK, size >> IO_PAGE_SHIFT); 599 + sbus_strbuf_flush(iommu, base & IO_PAGE_MASK, size >> IO_PAGE_SHIFT, direction); 608 600 spin_unlock_irqrestore(&iommu->lock, flags); 609 601 } 610 602 ··· 628 620 size = IO_PAGE_ALIGN(sg[i].dma_address + sg[i].dma_length) - base; 629 621 630 622 spin_lock_irqsave(&iommu->lock, flags); 631 - sbus_strbuf_flush(iommu, base, size >> IO_PAGE_SHIFT); 623 + sbus_strbuf_flush(iommu, base, size >> IO_PAGE_SHIFT, direction); 632 624 spin_unlock_irqrestore(&iommu->lock, flags); 633 625 } 634 626
+2
include/asm-sparc64/iommu.h
··· 16 16 #define IOPTE_CACHE 0x0000000000000010UL /* Cached (in UPA E-cache) */ 17 17 #define IOPTE_WRITE 0x0000000000000002UL /* Writeable */ 18 18 19 + #define IOMMU_NUM_CTXS 4096 20 + 19 21 #endif /* !(_SPARC_IOMMU_H) */
+5 -3
include/asm-sparc64/pbm.h
··· 15 15 #include <asm/io.h> 16 16 #include <asm/page.h> 17 17 #include <asm/oplib.h> 18 + #include <asm/iommu.h> 18 19 19 20 /* The abstraction used here is that there are PCI controllers, 20 21 * each with one (Sabre) or two (PSYCHO/SCHIZO) PCI bus modules ··· 40 39 * streaming buffers underneath. 41 40 */ 42 41 spinlock_t lock; 43 - 44 - /* Context allocator. */ 45 - unsigned int iommu_cur_ctx; 46 42 47 43 /* IOMMU page table, a linear array of ioptes. */ 48 44 iopte_t *page_table; /* The page table itself. */ ··· 84 86 u16 next; 85 87 u16 flush; 86 88 } alloc_info[PBM_NCLUSTERS]; 89 + 90 + /* CTX allocation. */ 91 + unsigned long ctx_lowest_free; 92 + unsigned long ctx_bitmap[IOMMU_NUM_CTXS / (sizeof(unsigned long) * 8)]; 87 93 88 94 /* Here a PCI controller driver describes the areas of 89 95 * PCI memory space where DMA to/from physical memory