[NETFILTER]: nf_conntrack_sip: kill request URI "header" definitions
[linux-2.6/mini2440.git] / drivers / ieee1394 / dma.c
blob73685e7dc7e4c9e6acea24c454e737686768b7e3
1 /*
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.
8 */
10 #include <linux/mm.h>
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/slab.h>
14 #include <linux/vmalloc.h>
15 #include <linux/scatterlist.h>
17 #include "dma.h"
19 /* dma_prog_region */
21 void dma_prog_region_init(struct dma_prog_region *prog)
23 prog->kvirt = NULL;
24 prog->dev = NULL;
25 prog->n_pages = 0;
26 prog->bus_addr = 0;
29 int dma_prog_region_alloc(struct dma_prog_region *prog, unsigned long n_bytes,
30 struct pci_dev *dev)
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);
38 if (!prog->kvirt) {
39 printk(KERN_ERR
40 "dma_prog_region_alloc: pci_alloc_consistent() failed\n");
41 dma_prog_region_free(prog);
42 return -ENOMEM;
45 prog->dev = dev;
47 return 0;
50 void dma_prog_region_free(struct dma_prog_region *prog)
52 if (prog->kvirt) {
53 pci_free_consistent(prog->dev, prog->n_pages << PAGE_SHIFT,
54 prog->kvirt, prog->bus_addr);
57 prog->kvirt = NULL;
58 prog->dev = NULL;
59 prog->n_pages = 0;
60 prog->bus_addr = 0;
63 /* dma_region */
65 /**
66 * dma_region_init - clear out all fields but do not allocate anything
68 void dma_region_init(struct dma_region *dma)
70 dma->kvirt = NULL;
71 dma->dev = NULL;
72 dma->n_pages = 0;
73 dma->n_dma_pages = 0;
74 dma->sglist = NULL;
77 /**
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)
83 unsigned int i;
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);
91 if (!dma->kvirt) {
92 printk(KERN_ERR "dma_region_alloc: vmalloc_32() failed\n");
93 goto err;
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));
101 if (!dma->sglist) {
102 printk(KERN_ERR "dma_region_alloc: vmalloc(sglist) failed\n");
103 goto err;
106 sg_init_table(dma->sglist, dma->n_pages);
108 /* fill scatter/gather list with pages */
109 for (i = 0; i < dma->n_pages; i++) {
110 unsigned long va =
111 (unsigned long)dma->kvirt + (i << PAGE_SHIFT);
113 sg_set_page(&dma->sglist[i], vmalloc_to_page((void *)va),
114 PAGE_SIZE, 0);
117 /* map sglist to the IOMMU */
118 dma->n_dma_pages =
119 pci_map_sg(dev, dma->sglist, dma->n_pages, direction);
121 if (dma->n_dma_pages == 0) {
122 printk(KERN_ERR "dma_region_alloc: pci_map_sg() failed\n");
123 goto err;
126 dma->dev = dev;
127 dma->direction = direction;
129 return 0;
131 err:
132 dma_region_free(dma);
133 return -ENOMEM;
137 * dma_region_free - unmap and free the buffer
139 void dma_region_free(struct dma_region *dma)
141 if (dma->n_dma_pages) {
142 pci_unmap_sg(dma->dev, dma->sglist, dma->n_pages,
143 dma->direction);
144 dma->n_dma_pages = 0;
145 dma->dev = NULL;
148 vfree(dma->sglist);
149 dma->sglist = NULL;
151 vfree(dma->kvirt);
152 dma->kvirt = NULL;
153 dma->n_pages = 0;
156 /* find the scatterlist index and remaining offset corresponding to a
157 given offset from the beginning of the buffer */
158 static inline int dma_region_find(struct dma_region *dma, unsigned long offset,
159 unsigned int start, unsigned long *rem)
161 int i;
162 unsigned long off = offset;
164 for (i = start; i < dma->n_dma_pages; i++) {
165 if (off < sg_dma_len(&dma->sglist[i])) {
166 *rem = off;
167 break;
170 off -= sg_dma_len(&dma->sglist[i]);
173 BUG_ON(i >= dma->n_dma_pages);
175 return i;
179 * dma_region_offset_to_bus - get bus address of an offset within a DMA region
181 * Returns the DMA bus address of the byte with the given @offset relative to
182 * the beginning of the @dma.
184 dma_addr_t dma_region_offset_to_bus(struct dma_region * dma,
185 unsigned long offset)
187 unsigned long rem = 0;
189 struct scatterlist *sg =
190 &dma->sglist[dma_region_find(dma, offset, 0, &rem)];
191 return sg_dma_address(sg) + rem;
195 * dma_region_sync_for_cpu - sync the CPU's view of the buffer
197 void dma_region_sync_for_cpu(struct dma_region *dma, unsigned long offset,
198 unsigned long len)
200 int first, last;
201 unsigned long rem = 0;
203 if (!len)
204 len = 1;
206 first = dma_region_find(dma, offset, 0, &rem);
207 last = dma_region_find(dma, rem + len - 1, first, &rem);
209 pci_dma_sync_sg_for_cpu(dma->dev, &dma->sglist[first], last - first + 1,
210 dma->direction);
214 * dma_region_sync_for_device - sync the IO bus' view of the buffer
216 void dma_region_sync_for_device(struct dma_region *dma, unsigned long offset,
217 unsigned long len)
219 int first, last;
220 unsigned long rem = 0;
222 if (!len)
223 len = 1;
225 first = dma_region_find(dma, offset, 0, &rem);
226 last = dma_region_find(dma, rem + len - 1, first, &rem);
228 pci_dma_sync_sg_for_device(dma->dev, &dma->sglist[first],
229 last - first + 1, dma->direction);
232 #ifdef CONFIG_MMU
234 static int dma_region_pagefault(struct vm_area_struct *vma,
235 struct vm_fault *vmf)
237 struct dma_region *dma = (struct dma_region *)vma->vm_private_data;
239 if (!dma->kvirt)
240 return VM_FAULT_SIGBUS;
242 if (vmf->pgoff >= dma->n_pages)
243 return VM_FAULT_SIGBUS;
245 vmf->page = vmalloc_to_page(dma->kvirt + (vmf->pgoff << PAGE_SHIFT));
246 get_page(vmf->page);
247 return 0;
250 static struct vm_operations_struct dma_region_vm_ops = {
251 .fault = dma_region_pagefault,
255 * dma_region_mmap - map the buffer into a user space process
257 int dma_region_mmap(struct dma_region *dma, struct file *file,
258 struct vm_area_struct *vma)
260 unsigned long size;
262 if (!dma->kvirt)
263 return -EINVAL;
265 /* must be page-aligned (XXX: comment is wrong, we could allow pgoff) */
266 if (vma->vm_pgoff != 0)
267 return -EINVAL;
269 /* check the length */
270 size = vma->vm_end - vma->vm_start;
271 if (size > (dma->n_pages << PAGE_SHIFT))
272 return -EINVAL;
274 vma->vm_ops = &dma_region_vm_ops;
275 vma->vm_private_data = dma;
276 vma->vm_file = file;
277 vma->vm_flags |= VM_RESERVED;
279 return 0;
282 #else /* CONFIG_MMU */
284 int dma_region_mmap(struct dma_region *dma, struct file *file,
285 struct vm_area_struct *vma)
287 return -EINVAL;
290 #endif /* CONFIG_MMU */