[SPARC64]: Fix streaming buffer flushing on PCI and SBUS.

Firstly, if the direction is TODEVICE, then dirty data in the
streaming cache is impossible so we can elide the flush-flag
synchronization in that case.

Next, the context allocator is broken. It is highly likely
that contexts get used multiple times for different dma
mappings, which confuses the strbuf flushing code and makes
it run inefficiently.

Signed-off-by: David S. Miller <davem@davemloft.net>

+94 -32
+70 -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; 395 + if (pci_iommu_read(matchreg) == 0) 396 + goto do_flush_sync; 397 + 426 398 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); 399 + if ((val = pci_iommu_read(matchreg)) == 0) 400 + goto do_flush_sync; 401 + 402 + val &= 0xffff; 403 + while (val) { 404 + if (val & 0x1) 405 + pci_iommu_write(flushreg, ctx); 406 + val >>= 1; 434 407 } 435 - if (!limit) 408 + val = pci_iommu_read(matchreg); 409 + if (unlikely(val)) { 436 410 printk(KERN_WARNING "pci_strbuf_flush: ctx flush " 437 - "timeout vaddr[%08x] ctx[%lx]\n", 438 - vaddr, ctx); 411 + "timeout matchreg[%lx] ctx[%lx]\n", 412 + val, ctx); 413 + goto do_page_flush; 414 + } 439 415 } else { 440 416 unsigned long i; 441 417 418 + do_page_flush: 442 419 for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE) 443 420 pci_iommu_write(strbuf->strbuf_pflush, vaddr); 444 421 } 445 422 423 + do_flush_sync: 424 + /* If the device could not have possibly put dirty data into 425 + * the streaming cache, no flush-flag synchronization needs 426 + * to be performed. 427 + */ 428 + if (direction == PCI_DMA_TODEVICE) 429 + return; 430 + 431 + PCI_STC_FLUSHFLAG_INIT(strbuf); 446 432 pci_iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa); 447 433 (void) pci_iommu_read(iommu->write_complete_reg); 448 434 ··· 512 466 513 467 /* Step 1: Kick data out of streaming buffers if necessary. */ 514 468 if (strbuf->strbuf_enabled) 515 - pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages); 469 + pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction); 516 470 517 471 /* Step 2: Clear out first TSB entry. */ 518 472 iopte_make_dummy(iommu, base); 519 473 520 474 free_streaming_cluster(iommu, bus_addr - iommu->page_table_map_base, 521 475 npages, ctx); 476 + 477 + iommu_free_ctx(iommu, ctx); 522 478 523 479 spin_unlock_irqrestore(&iommu->lock, flags); 524 480 } ··· 661 613 /* Step 4: Choose a context if necessary. */ 662 614 ctx = 0; 663 615 if (iommu->iommu_ctxflush) 664 - ctx = iommu->iommu_cur_ctx++; 616 + ctx = iommu_alloc_ctx(iommu); 665 617 666 618 /* Step 5: Create the mappings. */ 667 619 if (strbuf->strbuf_enabled) ··· 726 678 727 679 /* Step 1: Kick data out of streaming buffers if necessary. */ 728 680 if (strbuf->strbuf_enabled) 729 - pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages); 681 + pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction); 730 682 731 683 /* Step 2: Clear out first TSB entry. */ 732 684 iopte_make_dummy(iommu, base); 733 685 734 686 free_streaming_cluster(iommu, bus_addr - iommu->page_table_map_base, 735 687 npages, ctx); 688 + 689 + iommu_free_ctx(iommu, ctx); 736 690 737 691 spin_unlock_irqrestore(&iommu->lock, flags); 738 692 } ··· 774 724 } 775 725 776 726 /* Step 2: Kick data out of streaming buffers. */ 777 - pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages); 727 + pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction); 778 728 779 729 spin_unlock_irqrestore(&iommu->lock, flags); 780 730 } ··· 818 768 i--; 819 769 npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) 820 770 - bus_addr) >> IO_PAGE_SHIFT; 821 - pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages); 771 + pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction); 822 772 823 773 spin_unlock_irqrestore(&iommu->lock, flags); 824 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