📄 pci_iommu.c
字号:
/* $Id: pci_iommu.c,v 1.17 2001/12/17 07:05:09 davem Exp $ * pci_iommu.c: UltraSparc PCI controller IOM/STC support. * * Copyright (C) 1999 David S. Miller (davem@redhat.com) * Copyright (C) 1999, 2000 Jakub Jelinek (jakub@redhat.com) */#include <linux/kernel.h>#include <linux/sched.h>#include <linux/mm.h>#include <linux/delay.h>#include <asm/pbm.h>#include "iommu_common.h"#define PCI_STC_CTXMATCH_ADDR(STC, CTX) \ ((STC)->strbuf_ctxmatch_base + ((CTX) << 3))/* Accessing IOMMU and Streaming Buffer registers. * REG parameter is a physical address. All registers * are 64-bits in size. */#define pci_iommu_read(__reg) \({ u64 __ret; \ __asm__ __volatile__("ldxa [%1] %2, %0" \ : "=r" (__ret) \ : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \ : "memory"); \ __ret; \})#define pci_iommu_write(__reg, __val) \ __asm__ __volatile__("stxa %0, [%1] %2" \ : /* no outputs */ \ : "r" (__val), "r" (__reg), \ "i" (ASI_PHYS_BYPASS_EC_E))/* Must be invoked under the IOMMU lock. */static void __iommu_flushall(struct pci_iommu *iommu){ unsigned long tag; int entry; tag = iommu->iommu_flush + (0xa580UL - 0x0210UL); for (entry = 0; entry < 16; entry++) { pci_iommu_write(tag, 0); tag += 8; } /* Ensure completion of previous PIO writes. */ (void) pci_iommu_read(iommu->write_complete_reg);}#define IOPTE_CONSISTENT(CTX) \ (IOPTE_VALID | IOPTE_CACHE | \ (((CTX) << 47) & IOPTE_CONTEXT))#define IOPTE_STREAMING(CTX) \ (IOPTE_CONSISTENT(CTX) | IOPTE_STBUF)/* Existing mappings are never marked invalid, instead they * are pointed to a dummy page. */#define IOPTE_IS_DUMMY(iommu, iopte) \ ((iopte_val(*iopte) & IOPTE_PAGE) == (iommu)->dummy_page_pa)static void inline iopte_make_dummy(struct pci_iommu *iommu, iopte_t *iopte){ unsigned long val = iopte_val(*iopte); val &= ~IOPTE_PAGE; val |= iommu->dummy_page_pa; iopte_val(*iopte) = val;}/* Based largely upon the ppc64 iommu allocator. */static long pci_arena_alloc(struct pci_iommu *iommu, unsigned long npages){ struct pci_iommu_arena *arena = &iommu->arena; unsigned long n, i, start, end, limit; int pass; limit = arena->limit; start = arena->hint; pass = 0;again: n = find_next_zero_bit(arena->map, limit, start); end = n + npages; if (unlikely(end >= limit)) { if (likely(pass < 1)) { limit = start; start = 0; __iommu_flushall(iommu); pass++; goto again; } else { /* Scanned the whole thing, give up. */ return -1; } } for (i = n; i < end; i++) { if (test_bit(i, arena->map)) { start = i + 1; goto again; } } for (i = n; i < end; i++) __set_bit(i, arena->map); arena->hint = end; return n;}static void pci_arena_free(struct pci_iommu_arena *arena, unsigned long base, unsigned long npages){ unsigned long i; for (i = base; i < (base + npages); i++) __clear_bit(i, arena->map);}void pci_iommu_table_init(struct pci_iommu *iommu, int tsbsize, u32 dma_offset, u32 dma_addr_mask){ unsigned long i, tsbbase, order, sz, num_tsb_entries; num_tsb_entries = tsbsize / sizeof(iopte_t); /* Setup initial software IOMMU state. */ spin_lock_init(&iommu->lock); iommu->ctx_lowest_free = 1; iommu->page_table_map_base = dma_offset; iommu->dma_addr_mask = dma_addr_mask; /* Allocate and initialize the free area map. */ sz = num_tsb_entries / 8; sz = (sz + 7UL) & ~7UL; iommu->arena.map = kmalloc(sz, GFP_KERNEL); if (!iommu->arena.map) { prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n"); prom_halt(); } memset(iommu->arena.map, 0, sz); iommu->arena.limit = num_tsb_entries; /* Allocate and initialize the dummy page which we * set inactive IO PTEs to point to. */ iommu->dummy_page = __get_free_pages(GFP_KERNEL, 0); if (!iommu->dummy_page) { prom_printf("PCI_IOMMU: Error, gfp(dummy_page) failed.\n"); prom_halt(); } memset((void *)iommu->dummy_page, 0, PAGE_SIZE); iommu->dummy_page_pa = (unsigned long) __pa(iommu->dummy_page); /* Now allocate and setup the IOMMU page table itself. */ order = get_order(tsbsize); tsbbase = __get_free_pages(GFP_KERNEL, order); if (!tsbbase) { prom_printf("PCI_IOMMU: Error, gfp(tsb) failed.\n"); prom_halt(); } iommu->page_table = (iopte_t *)tsbbase; for (i = 0; i < num_tsb_entries; i++) iopte_make_dummy(iommu, &iommu->page_table[i]);}static inline iopte_t *alloc_npages(struct pci_iommu *iommu, unsigned long npages){ long entry; entry = pci_arena_alloc(iommu, npages); if (unlikely(entry < 0)) return NULL; return iommu->page_table + entry;}static inline void free_npages(struct pci_iommu *iommu, dma_addr_t base, unsigned long npages){ pci_arena_free(&iommu->arena, base >> IO_PAGE_SHIFT, npages);}static int iommu_alloc_ctx(struct pci_iommu *iommu){ int lowest = iommu->ctx_lowest_free; int sz = IOMMU_NUM_CTXS - lowest; int n = find_next_zero_bit(iommu->ctx_bitmap, sz, lowest); if (unlikely(n == sz)) { n = find_next_zero_bit(iommu->ctx_bitmap, lowest, 1); if (unlikely(n == lowest)) { printk(KERN_WARNING "IOMMU: Ran out of contexts.\n"); n = 0; } } if (n) __set_bit(n, iommu->ctx_bitmap); return n;}static inline void iommu_free_ctx(struct pci_iommu *iommu, int ctx){ if (likely(ctx)) { __clear_bit(ctx, iommu->ctx_bitmap); if (ctx < iommu->ctx_lowest_free) iommu->ctx_lowest_free = ctx; }}/* Allocate and map kernel buffer of size SIZE using consistent mode * DMA for PCI device PDEV. Return non-NULL cpu-side address if * successful and set *DMA_ADDRP to the PCI side dma address. */void *pci_alloc_consistent(struct pci_dev *pdev, size_t size, dma_addr_t *dma_addrp){ struct pcidev_cookie *pcp; struct pci_iommu *iommu; iopte_t *iopte; unsigned long flags, order, first_page; void *ret; int npages; size = IO_PAGE_ALIGN(size); order = get_order(size); if (order >= 10) return NULL; first_page = __get_free_pages(GFP_ATOMIC, order); if (first_page == 0UL) return NULL; memset((char *)first_page, 0, PAGE_SIZE << order); pcp = pdev->sysdata; iommu = pcp->pbm->iommu; spin_lock_irqsave(&iommu->lock, flags); iopte = alloc_npages(iommu, size >> IO_PAGE_SHIFT); spin_unlock_irqrestore(&iommu->lock, flags); if (unlikely(iopte == NULL)) { free_pages(first_page, order); return NULL; } *dma_addrp = (iommu->page_table_map_base + ((iopte - iommu->page_table) << IO_PAGE_SHIFT)); ret = (void *) first_page; npages = size >> IO_PAGE_SHIFT; first_page = __pa(first_page); while (npages--) { iopte_val(*iopte) = (IOPTE_CONSISTENT(0UL) | IOPTE_WRITE | (first_page & IOPTE_PAGE)); iopte++; first_page += IO_PAGE_SIZE; } return ret;}/* Free and unmap a consistent DMA translation. */void pci_free_consistent(struct pci_dev *pdev, size_t size, void *cpu, dma_addr_t dvma){ struct pcidev_cookie *pcp; struct pci_iommu *iommu; iopte_t *iopte; unsigned long flags, order, npages; npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT; pcp = pdev->sysdata; iommu = pcp->pbm->iommu; iopte = iommu->page_table + ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT); spin_lock_irqsave(&iommu->lock, flags); free_npages(iommu, dvma, npages); spin_unlock_irqrestore(&iommu->lock, flags); order = get_order(size); if (order < 10) free_pages((unsigned long)cpu, order);}/* Map a single buffer at PTR of SZ bytes for PCI DMA * in streaming mode. */dma_addr_t pci_map_single(struct pci_dev *pdev, void *ptr, size_t sz, int direction){ struct pcidev_cookie *pcp; struct pci_iommu *iommu; struct pci_strbuf *strbuf; iopte_t *base; unsigned long flags, npages, oaddr; unsigned long i, base_paddr, ctx; u32 bus_addr, ret; unsigned long iopte_protection; pcp = pdev->sysdata; iommu = pcp->pbm->iommu; strbuf = &pcp->pbm->stc; if (unlikely(direction == PCI_DMA_NONE)) goto bad_no_ctx; oaddr = (unsigned long)ptr; npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK); npages >>= IO_PAGE_SHIFT; spin_lock_irqsave(&iommu->lock, flags); base = alloc_npages(iommu, npages); ctx = 0; if (iommu->iommu_ctxflush) ctx = iommu_alloc_ctx(iommu); spin_unlock_irqrestore(&iommu->lock, flags); if (unlikely(!base)) goto bad; bus_addr = (iommu->page_table_map_base + ((base - iommu->page_table) << IO_PAGE_SHIFT)); ret = bus_addr | (oaddr & ~IO_PAGE_MASK); base_paddr = __pa(oaddr & IO_PAGE_MASK); if (strbuf->strbuf_enabled) iopte_protection = IOPTE_STREAMING(ctx); else iopte_protection = IOPTE_CONSISTENT(ctx); if (direction != PCI_DMA_TODEVICE) iopte_protection |= IOPTE_WRITE; for (i = 0; i < npages; i++, base++, base_paddr += IO_PAGE_SIZE) iopte_val(*base) = iopte_protection | base_paddr; return ret;bad: iommu_free_ctx(iommu, ctx);bad_no_ctx: if (printk_ratelimit()) WARN_ON(1); return PCI_DMA_ERROR_CODE;}static void pci_strbuf_flush(struct pci_strbuf *strbuf, struct pci_iommu *iommu, u32 vaddr, unsigned long ctx, unsigned long npages, int direction){ int limit; if (strbuf->strbuf_ctxflush && iommu->iommu_ctxflush) { unsigned long matchreg, flushreg; u64 val; flushreg = strbuf->strbuf_ctxflush; matchreg = PCI_STC_CTXMATCH_ADDR(strbuf, ctx); pci_iommu_write(flushreg, ctx); val = pci_iommu_read(matchreg); val &= 0xffff; if (!val) goto do_flush_sync; while (val) { if (val & 0x1) pci_iommu_write(flushreg, ctx); val >>= 1; } val = pci_iommu_read(matchreg); if (unlikely(val)) { printk(KERN_WARNING "pci_strbuf_flush: ctx flush " "timeout matchreg[%lx] ctx[%lx]\n", val, ctx); goto do_page_flush; } } else { unsigned long i; do_page_flush: for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE) pci_iommu_write(strbuf->strbuf_pflush, vaddr); }do_flush_sync: /* If the device could not have possibly put dirty data into * the streaming cache, no flush-flag synchronization needs * to be performed. */ if (direction == PCI_DMA_TODEVICE) return; PCI_STC_FLUSHFLAG_INIT(strbuf); pci_iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa); (void) pci_iommu_read(iommu->write_complete_reg); limit = 100000; while (!PCI_STC_FLUSHFLAG_SET(strbuf)) { limit--; if (!limit) break; udelay(1); rmb(); } if (!limit) printk(KERN_WARNING "pci_strbuf_flush: flushflag timeout " "vaddr[%08x] ctx[%lx] npages[%ld]\n",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -