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