2  * DMA region bookkeeping routines
 
   4  * Copyright (C) 2002 Maas Digital LLC
 
   6  * This code is licensed under the GPL.  See the file COPYING in the root
 
   7  * directory of the kernel sources for details.
 
  11 #include <linux/module.h>
 
  12 #include <linux/pci.h>
 
  13 #include <linux/slab.h>
 
  14 #include <linux/vmalloc.h>
 
  15 #include <asm/scatterlist.h>
 
  21 void dma_prog_region_init(struct dma_prog_region *prog)
 
  29 int dma_prog_region_alloc(struct dma_prog_region *prog, unsigned long n_bytes,
 
  32         /* round up to page size */
 
  33         n_bytes = PAGE_ALIGN(n_bytes);
 
  35         prog->n_pages = n_bytes >> PAGE_SHIFT;
 
  37         prog->kvirt = pci_alloc_consistent(dev, n_bytes, &prog->bus_addr);
 
  40                        "dma_prog_region_alloc: pci_alloc_consistent() failed\n");
 
  41                 dma_prog_region_free(prog);
 
  50 void dma_prog_region_free(struct dma_prog_region *prog)
 
  53                 pci_free_consistent(prog->dev, prog->n_pages << PAGE_SHIFT,
 
  54                                     prog->kvirt, prog->bus_addr);
 
  66  * dma_region_init - clear out all fields but do not allocate anything
 
  68 void dma_region_init(struct dma_region *dma)
 
  78  * dma_region_alloc - allocate the buffer and map it to the IOMMU
 
  80 int dma_region_alloc(struct dma_region *dma, unsigned long n_bytes,
 
  81                      struct pci_dev *dev, int direction)
 
  85         /* round up to page size */
 
  86         n_bytes = PAGE_ALIGN(n_bytes);
 
  88         dma->n_pages = n_bytes >> PAGE_SHIFT;
 
  90         dma->kvirt = vmalloc_32(n_bytes);
 
  92                 printk(KERN_ERR "dma_region_alloc: vmalloc_32() failed\n");
 
  96         /* Clear the ram out, no junk to the user */
 
  97         memset(dma->kvirt, 0, n_bytes);
 
  99         /* allocate scatter/gather list */
 
 100         dma->sglist = vmalloc(dma->n_pages * sizeof(*dma->sglist));
 
 102                 printk(KERN_ERR "dma_region_alloc: vmalloc(sglist) failed\n");
 
 106         /* just to be safe - this will become unnecessary once sglist->address goes away */
 
 107         memset(dma->sglist, 0, dma->n_pages * sizeof(*dma->sglist));
 
 109         /* fill scatter/gather list with pages */
 
 110         for (i = 0; i < dma->n_pages; i++) {
 
 112                     (unsigned long)dma->kvirt + (i << PAGE_SHIFT);
 
 114                 dma->sglist[i].page = vmalloc_to_page((void *)va);
 
 115                 dma->sglist[i].length = PAGE_SIZE;
 
 118         /* map sglist to the IOMMU */
 
 120             pci_map_sg(dev, dma->sglist, dma->n_pages, direction);
 
 122         if (dma->n_dma_pages == 0) {
 
 123                 printk(KERN_ERR "dma_region_alloc: pci_map_sg() failed\n");
 
 128         dma->direction = direction;
 
 133         dma_region_free(dma);
 
 138  * dma_region_free - unmap and free the buffer
 
 140 void dma_region_free(struct dma_region *dma)
 
 142         if (dma->n_dma_pages) {
 
 143                 pci_unmap_sg(dma->dev, dma->sglist, dma->n_pages,
 
 145                 dma->n_dma_pages = 0;
 
 157 /* find the scatterlist index and remaining offset corresponding to a
 
 158    given offset from the beginning of the buffer */
 
 159 static inline int dma_region_find(struct dma_region *dma, unsigned long offset,
 
 160                                   unsigned int start, unsigned long *rem)
 
 163         unsigned long off = offset;
 
 165         for (i = start; i < dma->n_dma_pages; i++) {
 
 166                 if (off < sg_dma_len(&dma->sglist[i])) {
 
 171                 off -= sg_dma_len(&dma->sglist[i]);
 
 174         BUG_ON(i >= dma->n_dma_pages);
 
 180  * dma_region_offset_to_bus - get bus address of an offset within a DMA region
 
 182  * Returns the DMA bus address of the byte with the given @offset relative to
 
 183  * the beginning of the @dma.
 
 185 dma_addr_t dma_region_offset_to_bus(struct dma_region * dma,
 
 186                                     unsigned long offset)
 
 188         unsigned long rem = 0;
 
 190         struct scatterlist *sg =
 
 191             &dma->sglist[dma_region_find(dma, offset, 0, &rem)];
 
 192         return sg_dma_address(sg) + rem;
 
 196  * dma_region_sync_for_cpu - sync the CPU's view of the buffer
 
 198 void dma_region_sync_for_cpu(struct dma_region *dma, unsigned long offset,
 
 202         unsigned long rem = 0;
 
 207         first = dma_region_find(dma, offset, 0, &rem);
 
 208         last = dma_region_find(dma, rem + len - 1, first, &rem);
 
 210         pci_dma_sync_sg_for_cpu(dma->dev, &dma->sglist[first], last - first + 1,
 
 215  * dma_region_sync_for_device - sync the IO bus' view of the buffer
 
 217 void dma_region_sync_for_device(struct dma_region *dma, unsigned long offset,
 
 221         unsigned long rem = 0;
 
 226         first = dma_region_find(dma, offset, 0, &rem);
 
 227         last = dma_region_find(dma, rem + len - 1, first, &rem);
 
 229         pci_dma_sync_sg_for_device(dma->dev, &dma->sglist[first],
 
 230                                    last - first + 1, dma->direction);
 
 235 /* nopage() handler for mmap access */
 
 237 static struct page *dma_region_pagefault(struct vm_area_struct *area,
 
 238                                          unsigned long address, int *type)
 
 240         unsigned long offset;
 
 241         unsigned long kernel_virt_addr;
 
 242         struct page *ret = NOPAGE_SIGBUS;
 
 244         struct dma_region *dma = (struct dma_region *)area->vm_private_data;
 
 249         if ((address < (unsigned long)area->vm_start) ||
 
 251              (unsigned long)area->vm_start + (dma->n_pages << PAGE_SHIFT)))
 
 255                 *type = VM_FAULT_MINOR;
 
 256         offset = address - area->vm_start;
 
 257         kernel_virt_addr = (unsigned long)dma->kvirt + offset;
 
 258         ret = vmalloc_to_page((void *)kernel_virt_addr);
 
 264 static struct vm_operations_struct dma_region_vm_ops = {
 
 265         .nopage = dma_region_pagefault,
 
 269  * dma_region_mmap - map the buffer into a user space process
 
 271 int dma_region_mmap(struct dma_region *dma, struct file *file,
 
 272                     struct vm_area_struct *vma)
 
 279         /* must be page-aligned */
 
 280         if (vma->vm_pgoff != 0)
 
 283         /* check the length */
 
 284         size = vma->vm_end - vma->vm_start;
 
 285         if (size > (dma->n_pages << PAGE_SHIFT))
 
 288         vma->vm_ops = &dma_region_vm_ops;
 
 289         vma->vm_private_data = dma;
 
 291         vma->vm_flags |= VM_RESERVED;
 
 296 #else                           /* CONFIG_MMU */
 
 298 int dma_region_mmap(struct dma_region *dma, struct file *file,
 
 299                     struct vm_area_struct *vma)
 
 304 #endif                          /* CONFIG_MMU */