[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / drivers / ieee1394 / dma.c
blob758819d1999dd054b18f7084cb98072bdfc718a9
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/module.h>
11 #include <linux/vmalloc.h>
12 #include <linux/slab.h>
13 #include <linux/mm.h>
14 #include "dma.h"
16 /* dma_prog_region */
18 void dma_prog_region_init(struct dma_prog_region *prog)
20 prog->kvirt = NULL;
21 prog->dev = NULL;
22 prog->n_pages = 0;
23 prog->bus_addr = 0;
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 = PAGE_ALIGN(n_bytes);
31 prog->n_pages = n_bytes >> PAGE_SHIFT;
33 prog->kvirt = pci_alloc_consistent(dev, n_bytes, &prog->bus_addr);
34 if (!prog->kvirt) {
35 printk(KERN_ERR "dma_prog_region_alloc: pci_alloc_consistent() failed\n");
36 dma_prog_region_free(prog);
37 return -ENOMEM;
40 prog->dev = dev;
42 return 0;
45 void dma_prog_region_free(struct dma_prog_region *prog)
47 if (prog->kvirt) {
48 pci_free_consistent(prog->dev, prog->n_pages << PAGE_SHIFT, prog->kvirt, prog->bus_addr);
51 prog->kvirt = NULL;
52 prog->dev = NULL;
53 prog->n_pages = 0;
54 prog->bus_addr = 0;
57 /* dma_region */
59 void dma_region_init(struct dma_region *dma)
61 dma->kvirt = NULL;
62 dma->dev = NULL;
63 dma->n_pages = 0;
64 dma->n_dma_pages = 0;
65 dma->sglist = NULL;
68 int dma_region_alloc(struct dma_region *dma, unsigned long n_bytes, struct pci_dev *dev, int direction)
70 unsigned int i;
72 /* round up to page size */
73 n_bytes = PAGE_ALIGN(n_bytes);
75 dma->n_pages = n_bytes >> PAGE_SHIFT;
77 dma->kvirt = vmalloc_32(n_bytes);
78 if (!dma->kvirt) {
79 printk(KERN_ERR "dma_region_alloc: vmalloc_32() failed\n");
80 goto err;
83 /* Clear the ram out, no junk to the user */
84 memset(dma->kvirt, 0, n_bytes);
86 /* allocate scatter/gather list */
87 dma->sglist = vmalloc(dma->n_pages * sizeof(*dma->sglist));
88 if (!dma->sglist) {
89 printk(KERN_ERR "dma_region_alloc: vmalloc(sglist) failed\n");
90 goto err;
93 /* just to be safe - this will become unnecessary once sglist->address goes away */
94 memset(dma->sglist, 0, dma->n_pages * sizeof(*dma->sglist));
96 /* fill scatter/gather list with pages */
97 for (i = 0; i < dma->n_pages; i++) {
98 unsigned long va = (unsigned long) dma->kvirt + (i << PAGE_SHIFT);
100 dma->sglist[i].page = vmalloc_to_page((void *)va);
101 dma->sglist[i].length = PAGE_SIZE;
104 /* map sglist to the IOMMU */
105 dma->n_dma_pages = pci_map_sg(dev, dma->sglist, dma->n_pages, direction);
107 if (dma->n_dma_pages == 0) {
108 printk(KERN_ERR "dma_region_alloc: pci_map_sg() failed\n");
109 goto err;
112 dma->dev = dev;
113 dma->direction = direction;
115 return 0;
117 err:
118 dma_region_free(dma);
119 return -ENOMEM;
122 void dma_region_free(struct dma_region *dma)
124 if (dma->n_dma_pages) {
125 pci_unmap_sg(dma->dev, dma->sglist, dma->n_pages, dma->direction);
126 dma->n_dma_pages = 0;
127 dma->dev = NULL;
130 vfree(dma->sglist);
131 dma->sglist = NULL;
133 vfree(dma->kvirt);
134 dma->kvirt = NULL;
135 dma->n_pages = 0;
138 /* find the scatterlist index and remaining offset corresponding to a
139 given offset from the beginning of the buffer */
140 static inline int dma_region_find(struct dma_region *dma, unsigned long offset, unsigned long *rem)
142 int i;
143 unsigned long off = offset;
145 for (i = 0; i < dma->n_dma_pages; i++) {
146 if (off < sg_dma_len(&dma->sglist[i])) {
147 *rem = off;
148 break;
151 off -= sg_dma_len(&dma->sglist[i]);
154 BUG_ON(i >= dma->n_dma_pages);
156 return i;
159 dma_addr_t dma_region_offset_to_bus(struct dma_region *dma, unsigned long offset)
161 unsigned long rem;
163 struct scatterlist *sg = &dma->sglist[dma_region_find(dma, offset, &rem)];
164 return sg_dma_address(sg) + rem;
167 void dma_region_sync_for_cpu(struct dma_region *dma, unsigned long offset, unsigned long len)
169 int first, last;
170 unsigned long rem;
172 if (!len)
173 len = 1;
175 first = dma_region_find(dma, offset, &rem);
176 last = dma_region_find(dma, offset + len - 1, &rem);
178 pci_dma_sync_sg_for_cpu(dma->dev, &dma->sglist[first], last - first + 1, dma->direction);
181 void dma_region_sync_for_device(struct dma_region *dma, unsigned long offset, unsigned long len)
183 int first, last;
184 unsigned long rem;
186 if (!len)
187 len = 1;
189 first = dma_region_find(dma, offset, &rem);
190 last = dma_region_find(dma, offset + len - 1, &rem);
192 pci_dma_sync_sg_for_device(dma->dev, &dma->sglist[first], last - first + 1, dma->direction);
195 #ifdef CONFIG_MMU
197 /* nopage() handler for mmap access */
199 static struct page*
200 dma_region_pagefault(struct vm_area_struct *area, unsigned long address, int *type)
202 unsigned long offset;
203 unsigned long kernel_virt_addr;
204 struct page *ret = NOPAGE_SIGBUS;
206 struct dma_region *dma = (struct dma_region*) area->vm_private_data;
208 if (!dma->kvirt)
209 goto out;
211 if ( (address < (unsigned long) area->vm_start) ||
212 (address > (unsigned long) area->vm_start + (dma->n_pages << PAGE_SHIFT)) )
213 goto out;
215 if (type)
216 *type = VM_FAULT_MINOR;
217 offset = address - area->vm_start;
218 kernel_virt_addr = (unsigned long) dma->kvirt + offset;
219 ret = vmalloc_to_page((void*) kernel_virt_addr);
220 get_page(ret);
221 out:
222 return ret;
225 static struct vm_operations_struct dma_region_vm_ops = {
226 .nopage = dma_region_pagefault,
229 int dma_region_mmap(struct dma_region *dma, struct file *file, struct vm_area_struct *vma)
231 unsigned long size;
233 if (!dma->kvirt)
234 return -EINVAL;
236 /* must be page-aligned */
237 if (vma->vm_pgoff != 0)
238 return -EINVAL;
240 /* check the length */
241 size = vma->vm_end - vma->vm_start;
242 if (size > (dma->n_pages << PAGE_SHIFT))
243 return -EINVAL;
245 vma->vm_ops = &dma_region_vm_ops;
246 vma->vm_private_data = dma;
247 vma->vm_file = file;
248 vma->vm_flags |= VM_RESERVED;
250 return 0;
253 #else /* CONFIG_MMU */
255 int dma_region_mmap(struct dma_region *dma, struct file *file, struct vm_area_struct *vma)
257 return -EINVAL;
260 #endif /* CONFIG_MMU */