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.
10 #include <linux/module.h>
11 #include <linux/vmalloc.h>
12 #include <linux/slab.h>
18 void dma_prog_region_init(struct dma_prog_region *prog)
26 int dma_prog_region_alloc(struct dma_prog_region *prog, unsigned long n_bytes, struct pci_dev *dev)
28 /* round up to page size */
29 n_bytes = round_up_to_page(n_bytes);
31 prog->n_pages = n_bytes / PAGE_SIZE;
33 prog->kvirt = pci_alloc_consistent(dev, prog->n_pages * PAGE_SIZE, &prog->bus_addr);
35 printk(KERN_ERR "dma_prog_region_alloc: pci_alloc_consistent() failed\n");
36 dma_prog_region_free(prog);
45 void dma_prog_region_free(struct dma_prog_region *prog)
48 pci_free_consistent(prog->dev, prog->n_pages * PAGE_SIZE, prog->kvirt, prog->bus_addr);
59 void dma_region_init(struct dma_region *dma)
68 int dma_region_alloc(struct dma_region *dma, unsigned long n_bytes, struct pci_dev *dev, int direction)
70 unsigned int i, n_pages;
72 /* round up to page size */
73 n_bytes = round_up_to_page(n_bytes);
75 n_pages = n_bytes / PAGE_SIZE;
77 dma->kvirt = vmalloc_32(n_pages * PAGE_SIZE);
79 printk(KERN_ERR "dma_region_alloc: vmalloc_32() failed\n");
83 dma->n_pages = n_pages;
85 /* Clear the ram out, no junk to the user */
86 memset(dma->kvirt, 0, n_pages * PAGE_SIZE);
88 /* allocate scatter/gather list */
89 dma->sglist = vmalloc(dma->n_pages * sizeof(struct scatterlist));
91 printk(KERN_ERR "dma_region_alloc: kmalloc(sglist) failed\n");
95 /* just to be safe - this will become unnecessary once sglist->address goes away */
96 memset(dma->sglist, 0, dma->n_pages * sizeof(struct scatterlist));
98 /* fill scatter/gather list with pages */
99 for (i = 0; i < dma->n_pages; i++) {
100 unsigned long va = (unsigned long) dma->kvirt + i * PAGE_SIZE;
102 dma->sglist[i].page = vmalloc_to_page((void *)va);
103 dma->sglist[i].length = PAGE_SIZE;
106 /* map sglist to the IOMMU */
107 dma->n_dma_pages = pci_map_sg(dev, &dma->sglist[0], dma->n_pages, direction);
109 if (dma->n_dma_pages == 0) {
110 printk(KERN_ERR "dma_region_alloc: pci_map_sg() failed\n");
115 dma->direction = direction;
120 dma_region_free(dma);
124 void dma_region_free(struct dma_region *dma)
126 if (dma->n_dma_pages) {
127 pci_unmap_sg(dma->dev, dma->sglist, dma->n_pages, dma->direction);
128 dma->n_dma_pages = 0;
144 /* find the scatterlist index and remaining offset corresponding to a
145 given offset from the beginning of the buffer */
146 static inline int dma_region_find(struct dma_region *dma, unsigned long offset, unsigned long *rem)
149 unsigned long off = offset;
151 for (i = 0; i < dma->n_dma_pages; i++) {
152 if (off < sg_dma_len(&dma->sglist[i])) {
157 off -= sg_dma_len(&dma->sglist[i]);
160 BUG_ON(i >= dma->n_dma_pages);
165 dma_addr_t dma_region_offset_to_bus(struct dma_region *dma, unsigned long offset)
169 struct scatterlist *sg = &dma->sglist[dma_region_find(dma, offset, &rem)];
170 return sg_dma_address(sg) + rem;
173 void dma_region_sync(struct dma_region *dma, unsigned long offset, unsigned long len)
181 first = dma_region_find(dma, offset, &rem);
182 last = dma_region_find(dma, offset + len - 1, &rem);
184 pci_dma_sync_sg(dma->dev, &dma->sglist[first], last - first + 1, dma->direction);
187 /* nopage() handler for mmap access */
190 dma_region_pagefault(struct vm_area_struct *area, unsigned long address, int write_access)
192 unsigned long offset;
193 unsigned long kernel_virt_addr;
194 struct page *ret = NOPAGE_SIGBUS;
196 struct dma_region *dma = (struct dma_region*) area->vm_private_data;
201 if ( (address < (unsigned long) area->vm_start) ||
202 (address > (unsigned long) area->vm_start + (PAGE_SIZE * dma->n_pages)) )
205 offset = address - area->vm_start;
206 kernel_virt_addr = (unsigned long) dma->kvirt + offset;
207 ret = vmalloc_to_page((void*) kernel_virt_addr);
213 static struct vm_operations_struct dma_region_vm_ops = {
214 .nopage = dma_region_pagefault,
217 int dma_region_mmap(struct dma_region *dma, struct file *file, struct vm_area_struct *vma)
224 /* must be page-aligned */
225 if (vma->vm_pgoff != 0)
228 /* check the length */
229 size = vma->vm_end - vma->vm_start;
230 if (size > (PAGE_SIZE * dma->n_pages))
233 vma->vm_ops = &dma_region_vm_ops;
234 vma->vm_private_data = dma;
236 vma->vm_flags |= VM_RESERVED;