pci_sun4v.c
来自「LINUX 2.6.17.4的源码」· C语言 代码 · 共 1,260 行 · 第 1/2 页
C
1,260 行
/* pci_sun4v.c: SUN4V specific PCI controller support. * * Copyright (C) 2006 David S. Miller (davem@davemloft.net) */#include <linux/kernel.h>#include <linux/types.h>#include <linux/pci.h>#include <linux/init.h>#include <linux/slab.h>#include <linux/interrupt.h>#include <linux/percpu.h>#include <asm/pbm.h>#include <asm/iommu.h>#include <asm/irq.h>#include <asm/upa.h>#include <asm/pstate.h>#include <asm/oplib.h>#include <asm/hypervisor.h>#include "pci_impl.h"#include "iommu_common.h"#include "pci_sun4v.h"#define PGLIST_NENTS (PAGE_SIZE / sizeof(u64))struct pci_iommu_batch { struct pci_dev *pdev; /* Device mapping is for. */ unsigned long prot; /* IOMMU page protections */ unsigned long entry; /* Index into IOTSB. */ u64 *pglist; /* List of physical pages */ unsigned long npages; /* Number of pages in list. */};static DEFINE_PER_CPU(struct pci_iommu_batch, pci_iommu_batch);/* Interrupts must be disabled. */static inline void pci_iommu_batch_start(struct pci_dev *pdev, unsigned long prot, unsigned long entry){ struct pci_iommu_batch *p = &__get_cpu_var(pci_iommu_batch); p->pdev = pdev; p->prot = prot; p->entry = entry; p->npages = 0;}/* Interrupts must be disabled. */static long pci_iommu_batch_flush(struct pci_iommu_batch *p){ struct pcidev_cookie *pcp = p->pdev->sysdata; unsigned long devhandle = pcp->pbm->devhandle; unsigned long prot = p->prot; unsigned long entry = p->entry; u64 *pglist = p->pglist; unsigned long npages = p->npages; while (npages != 0) { long num; num = pci_sun4v_iommu_map(devhandle, HV_PCI_TSBID(0, entry), npages, prot, __pa(pglist)); if (unlikely(num < 0)) { if (printk_ratelimit()) printk("pci_iommu_batch_flush: IOMMU map of " "[%08lx:%08lx:%lx:%lx:%lx] failed with " "status %ld\n", devhandle, HV_PCI_TSBID(0, entry), npages, prot, __pa(pglist), num); return -1; } entry += num; npages -= num; pglist += num; } p->entry = entry; p->npages = 0; return 0;}/* Interrupts must be disabled. */static inline long pci_iommu_batch_add(u64 phys_page){ struct pci_iommu_batch *p = &__get_cpu_var(pci_iommu_batch); BUG_ON(p->npages >= PGLIST_NENTS); p->pglist[p->npages++] = phys_page; if (p->npages == PGLIST_NENTS) return pci_iommu_batch_flush(p); return 0;}/* Interrupts must be disabled. */static inline long pci_iommu_batch_end(void){ struct pci_iommu_batch *p = &__get_cpu_var(pci_iommu_batch); BUG_ON(p->npages >= PGLIST_NENTS); return pci_iommu_batch_flush(p);}static long pci_arena_alloc(struct pci_iommu_arena *arena, unsigned long npages){ 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; 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);}static void *pci_4v_alloc_consistent(struct pci_dev *pdev, size_t size, dma_addr_t *dma_addrp, gfp_t gfp){ struct pcidev_cookie *pcp; struct pci_iommu *iommu; unsigned long flags, order, first_page, npages, n; void *ret; long entry; size = IO_PAGE_ALIGN(size); order = get_order(size); if (unlikely(order >= MAX_ORDER)) return NULL; npages = size >> IO_PAGE_SHIFT; first_page = __get_free_pages(gfp, order); if (unlikely(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); entry = pci_arena_alloc(&iommu->arena, npages); spin_unlock_irqrestore(&iommu->lock, flags); if (unlikely(entry < 0L)) goto arena_alloc_fail; *dma_addrp = (iommu->page_table_map_base + (entry << IO_PAGE_SHIFT)); ret = (void *) first_page; first_page = __pa(first_page); local_irq_save(flags); pci_iommu_batch_start(pdev, (HV_PCI_MAP_ATTR_READ | HV_PCI_MAP_ATTR_WRITE), entry); for (n = 0; n < npages; n++) { long err = pci_iommu_batch_add(first_page + (n * PAGE_SIZE)); if (unlikely(err < 0L)) goto iommu_map_fail; } if (unlikely(pci_iommu_batch_end() < 0L)) goto iommu_map_fail; local_irq_restore(flags); return ret;iommu_map_fail: /* Interrupts are disabled. */ spin_lock(&iommu->lock); pci_arena_free(&iommu->arena, entry, npages); spin_unlock_irqrestore(&iommu->lock, flags);arena_alloc_fail: free_pages(first_page, order); return NULL;}static void pci_4v_free_consistent(struct pci_dev *pdev, size_t size, void *cpu, dma_addr_t dvma){ struct pcidev_cookie *pcp; struct pci_iommu *iommu; unsigned long flags, order, npages, entry; u32 devhandle; npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT; pcp = pdev->sysdata; iommu = pcp->pbm->iommu; devhandle = pcp->pbm->devhandle; entry = ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT); spin_lock_irqsave(&iommu->lock, flags); pci_arena_free(&iommu->arena, entry, npages); do { unsigned long num; num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry), npages); entry += num; npages -= num; } while (npages != 0); spin_unlock_irqrestore(&iommu->lock, flags); order = get_order(size); if (order < 10) free_pages((unsigned long)cpu, order);}static dma_addr_t pci_4v_map_single(struct pci_dev *pdev, void *ptr, size_t sz, int direction){ struct pcidev_cookie *pcp; struct pci_iommu *iommu; unsigned long flags, npages, oaddr; unsigned long i, base_paddr; u32 bus_addr, ret; unsigned long prot; long entry; pcp = pdev->sysdata; iommu = pcp->pbm->iommu; if (unlikely(direction == PCI_DMA_NONE)) goto bad; oaddr = (unsigned long)ptr; npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK); npages >>= IO_PAGE_SHIFT; spin_lock_irqsave(&iommu->lock, flags); entry = pci_arena_alloc(&iommu->arena, npages); spin_unlock_irqrestore(&iommu->lock, flags); if (unlikely(entry < 0L)) goto bad; bus_addr = (iommu->page_table_map_base + (entry << IO_PAGE_SHIFT)); ret = bus_addr | (oaddr & ~IO_PAGE_MASK); base_paddr = __pa(oaddr & IO_PAGE_MASK); prot = HV_PCI_MAP_ATTR_READ; if (direction != PCI_DMA_TODEVICE) prot |= HV_PCI_MAP_ATTR_WRITE; local_irq_save(flags); pci_iommu_batch_start(pdev, prot, entry); for (i = 0; i < npages; i++, base_paddr += IO_PAGE_SIZE) { long err = pci_iommu_batch_add(base_paddr); if (unlikely(err < 0L)) goto iommu_map_fail; } if (unlikely(pci_iommu_batch_end() < 0L)) goto iommu_map_fail; local_irq_restore(flags); return ret;bad: if (printk_ratelimit()) WARN_ON(1); return PCI_DMA_ERROR_CODE;iommu_map_fail: /* Interrupts are disabled. */ spin_lock(&iommu->lock); pci_arena_free(&iommu->arena, entry, npages); spin_unlock_irqrestore(&iommu->lock, flags); return PCI_DMA_ERROR_CODE;}static void pci_4v_unmap_single(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction){ struct pcidev_cookie *pcp; struct pci_iommu *iommu; unsigned long flags, npages; long entry; u32 devhandle; if (unlikely(direction == PCI_DMA_NONE)) { if (printk_ratelimit()) WARN_ON(1); return; } pcp = pdev->sysdata; iommu = pcp->pbm->iommu; devhandle = pcp->pbm->devhandle; npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK); npages >>= IO_PAGE_SHIFT; bus_addr &= IO_PAGE_MASK; spin_lock_irqsave(&iommu->lock, flags); entry = (bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT; pci_arena_free(&iommu->arena, entry, npages); do { unsigned long num; num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry), npages); entry += num; npages -= num; } while (npages != 0); spin_unlock_irqrestore(&iommu->lock, flags);}#define SG_ENT_PHYS_ADDRESS(SG) \ (__pa(page_address((SG)->page)) + (SG)->offset)static inline long fill_sg(long entry, struct pci_dev *pdev, struct scatterlist *sg, int nused, int nelems, unsigned long prot){ struct scatterlist *dma_sg = sg; struct scatterlist *sg_end = sg + nelems; unsigned long flags; int i; local_irq_save(flags); pci_iommu_batch_start(pdev, prot, entry); for (i = 0; i < nused; i++) { unsigned long pteval = ~0UL; u32 dma_npages; dma_npages = ((dma_sg->dma_address & (IO_PAGE_SIZE - 1UL)) + dma_sg->dma_length + ((IO_PAGE_SIZE - 1UL))) >> IO_PAGE_SHIFT; do { unsigned long offset; signed int len; /* If we are here, we know we have at least one * more page to map. So walk forward until we * hit a page crossing, and begin creating new * mappings from that spot. */ for (;;) { unsigned long tmp; tmp = SG_ENT_PHYS_ADDRESS(sg); len = sg->length; if (((tmp ^ pteval) >> IO_PAGE_SHIFT) != 0UL) { pteval = tmp & IO_PAGE_MASK; offset = tmp & (IO_PAGE_SIZE - 1UL); break; } if (((tmp ^ (tmp + len - 1UL)) >> IO_PAGE_SHIFT) != 0UL) { pteval = (tmp + IO_PAGE_SIZE) & IO_PAGE_MASK; offset = 0UL; len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL))); break; } sg++; } pteval = (pteval & IOPTE_PAGE); while (len > 0) { long err; err = pci_iommu_batch_add(pteval); if (unlikely(err < 0L)) goto iommu_map_failed; pteval += IO_PAGE_SIZE; len -= (IO_PAGE_SIZE - offset); offset = 0; dma_npages--; } pteval = (pteval & IOPTE_PAGE) + len; sg++; /* Skip over any tail mappings we've fully mapped, * adjusting pteval along the way. Stop when we * detect a page crossing event. */ while (sg < sg_end && (pteval << (64 - IO_PAGE_SHIFT)) != 0UL && (pteval == SG_ENT_PHYS_ADDRESS(sg)) && ((pteval ^ (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) { pteval += sg->length; sg++; } if ((pteval << (64 - IO_PAGE_SHIFT)) == 0UL) pteval = ~0UL; } while (dma_npages != 0); dma_sg++; } if (unlikely(pci_iommu_batch_end() < 0L)) goto iommu_map_failed; local_irq_restore(flags); return 0;iommu_map_failed: local_irq_restore(flags); return -1L;}static int pci_4v_map_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction){ struct pcidev_cookie *pcp; struct pci_iommu *iommu; unsigned long flags, npages, prot; u32 dma_base; struct scatterlist *sgtmp; long entry, err; int used; /* Fast path single entry scatterlists. */ if (nelems == 1) { sglist->dma_address = pci_4v_map_single(pdev, (page_address(sglist->page) + sglist->offset), sglist->length, direction); if (unlikely(sglist->dma_address == PCI_DMA_ERROR_CODE)) return 0; sglist->dma_length = sglist->length; return 1; } pcp = pdev->sysdata; iommu = pcp->pbm->iommu; if (unlikely(direction == PCI_DMA_NONE)) goto bad; /* Step 1: Prepare scatter list. */ npages = prepare_sg(sglist, nelems); /* Step 2: Allocate a cluster and context, if necessary. */ spin_lock_irqsave(&iommu->lock, flags); entry = pci_arena_alloc(&iommu->arena, npages); spin_unlock_irqrestore(&iommu->lock, flags); if (unlikely(entry < 0L)) goto bad; dma_base = iommu->page_table_map_base + (entry << IO_PAGE_SHIFT); /* Step 3: Normalize DMA addresses. */ used = nelems; sgtmp = sglist; while (used && sgtmp->dma_length) { sgtmp->dma_address += dma_base; sgtmp++; used--; } used = nelems - used; /* Step 4: Create the mappings. */ prot = HV_PCI_MAP_ATTR_READ; if (direction != PCI_DMA_TODEVICE) prot |= HV_PCI_MAP_ATTR_WRITE; err = fill_sg(entry, pdev, sglist, used, nelems, prot); if (unlikely(err < 0L)) goto iommu_map_failed; return used;bad: if (printk_ratelimit()) WARN_ON(1); return 0;iommu_map_failed: spin_lock_irqsave(&iommu->lock, flags); pci_arena_free(&iommu->arena, entry, npages); spin_unlock_irqrestore(&iommu->lock, flags); return 0;}static void pci_4v_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction){ struct pcidev_cookie *pcp; struct pci_iommu *iommu; unsigned long flags, i, npages; long entry; u32 devhandle, bus_addr; if (unlikely(direction == PCI_DMA_NONE)) { if (printk_ratelimit()) WARN_ON(1); } pcp = pdev->sysdata; iommu = pcp->pbm->iommu; devhandle = pcp->pbm->devhandle; bus_addr = sglist->dma_address & IO_PAGE_MASK; for (i = 1; i < nelems; i++) if (sglist[i].dma_length == 0) break; i--; npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) - bus_addr) >> IO_PAGE_SHIFT; entry = ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT); spin_lock_irqsave(&iommu->lock, flags); pci_arena_free(&iommu->arena, entry, npages); do { unsigned long num; num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry), npages); entry += num; npages -= num; } while (npages != 0); spin_unlock_irqrestore(&iommu->lock, flags);}static void pci_4v_dma_sync_single_for_cpu(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction){ /* Nothing to do... */}static void pci_4v_dma_sync_sg_for_cpu(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction){ /* Nothing to do... */}struct pci_iommu_ops pci_sun4v_iommu_ops = { .alloc_consistent = pci_4v_alloc_consistent, .free_consistent = pci_4v_free_consistent, .map_single = pci_4v_map_single, .unmap_single = pci_4v_unmap_single, .map_sg = pci_4v_map_sg, .unmap_sg = pci_4v_unmap_sg, .dma_sync_single_for_cpu = pci_4v_dma_sync_single_for_cpu, .dma_sync_sg_for_cpu = pci_4v_dma_sync_sg_for_cpu,};/* SUN4V PCI configuration space accessors. */struct pdev_entry { struct pdev_entry *next; u32 devhandle; unsigned int bus; unsigned int device; unsigned int func;};#define PDEV_HTAB_SIZE 16#define PDEV_HTAB_MASK (PDEV_HTAB_SIZE - 1)static struct pdev_entry *pdev_htab[PDEV_HTAB_SIZE];static inline unsigned int pdev_hashfn(u32 devhandle, unsigned int bus, unsigned int device, unsigned int func){ unsigned int val; val = (devhandle ^ (devhandle >> 4)); val ^= bus; val ^= device; val ^= func; return val & PDEV_HTAB_MASK;}static int pdev_htab_add(u32 devhandle, unsigned int bus, unsigned int device, unsigned int func){ struct pdev_entry *p = kmalloc(sizeof(*p), GFP_KERNEL); struct pdev_entry **slot;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?