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
= PAGE_ALIGN(n_bytes
);
31 prog
->n_pages
= n_bytes
>> PAGE_SHIFT
;
34 prog
->kvirt
= pci_alloc_consistent(dev
, n_bytes
, &prog
->bus_addr
);
36 prog
->kvirt
= kmalloc(n_bytes
, in_interrupt()? GFP_ATOMIC
: GFP_KERNEL
);
39 printk(KERN_ERR
"dma_prog_region_alloc: pci_alloc_consistent() failed\n");
40 dma_prog_region_free(prog
);
49 void dma_prog_region_free(struct dma_prog_region
*prog
)
53 pci_free_consistent(prog
->dev
, prog
->n_pages
<< PAGE_SHIFT
, prog
->kvirt
, prog
->bus_addr
);
67 void dma_region_init(struct dma_region
*dma
)
76 int dma_region_alloc(struct dma_region
*dma
, unsigned long n_bytes
, struct pci_dev
*dev
, int direction
)
80 /* round up to page size */
81 n_bytes
= PAGE_ALIGN(n_bytes
);
83 dma
->n_pages
= n_bytes
>> PAGE_SHIFT
;
85 dma
->kvirt
= vmalloc_32(n_bytes
);
87 printk(KERN_ERR
"dma_region_alloc: vmalloc_32() failed\n");
91 /* Clear the ram out, no junk to the user */
92 memset(dma
->kvirt
, 0, n_bytes
);
94 /* allocate scatter/gather list */
95 dma
->sglist
= vmalloc(dma
->n_pages
* sizeof(*dma
->sglist
));
97 printk(KERN_ERR
"dma_region_alloc: vmalloc(sglist) failed\n");
101 /* just to be safe - this will become unnecessary once sglist->address goes away */
102 memset(dma
->sglist
, 0, dma
->n_pages
* sizeof(*dma
->sglist
));
104 /* fill scatter/gather list with pages */
105 for (i
= 0; i
< dma
->n_pages
; i
++) {
106 unsigned long va
= (unsigned long) dma
->kvirt
+ (i
<< PAGE_SHIFT
);
108 dma
->sglist
[i
].page
= vmalloc_to_page((void *)va
);
109 dma
->sglist
[i
].length
= PAGE_SIZE
;
112 /* map sglist to the IOMMU */
114 dma
->n_dma_pages
= pci_map_sg(dev
, dma
->sglist
, dma
->n_pages
, direction
);
116 dma
->n_dma_pages
= dma
->n_pages
;
117 for (i
= 0; i
< dma
->n_pages
; i
++) {
118 dma
->sglist
[i
].dma_address
= virt_to_bus(page_address(dma
->sglist
[i
].page
));
121 if (dma
->n_dma_pages
== 0) {
122 printk(KERN_ERR
"dma_region_alloc: pci_map_sg() failed\n");
127 dma
->direction
= direction
;
132 dma_region_free(dma
);
136 void dma_region_free(struct dma_region
*dma
)
138 if (dma
->n_dma_pages
) {
140 pci_unmap_sg(dma
->dev
, dma
->sglist
, dma
->n_pages
, dma
->direction
);
142 dma
->n_dma_pages
= 0;
158 /* find the scatterlist index and remaining offset corresponding to a
159 given offset from the beginning of the buffer */
160 static inline int dma_region_find(struct dma_region
*dma
, unsigned long offset
, unsigned long *rem
)
163 unsigned long off
= offset
;
165 for (i
= 0; 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
);
179 dma_addr_t
dma_region_offset_to_bus(struct dma_region
*dma
, unsigned long offset
)
183 struct scatterlist
*sg
= &dma
->sglist
[dma_region_find(dma
, offset
, &rem
)];
184 return sg_dma_address(sg
) + rem
;
187 void dma_region_sync_for_cpu(struct dma_region
*dma
, unsigned long offset
, unsigned long len
)
196 first
= dma_region_find(dma
, offset
, &rem
);
197 last
= dma_region_find(dma
, offset
+ len
- 1, &rem
);
199 pci_dma_sync_sg_for_cpu(dma
->dev
, &dma
->sglist
[first
], last
- first
+ 1, dma
->direction
);
203 void dma_region_sync_for_device(struct dma_region
*dma
, unsigned long offset
, unsigned long len
)
212 first
= dma_region_find(dma
, offset
, &rem
);
213 last
= dma_region_find(dma
, offset
+ len
- 1, &rem
);
215 pci_dma_sync_sg_for_device(dma
->dev
, &dma
->sglist
[first
], last
- first
+ 1, dma
->direction
);
221 /* nopage() handler for mmap access */
224 dma_region_pagefault(struct vm_area_struct
*area
, unsigned long address
, int *type
)
226 unsigned long offset
;
227 unsigned long kernel_virt_addr
;
228 struct page
*ret
= NOPAGE_SIGBUS
;
230 struct dma_region
*dma
= (struct dma_region
*) area
->vm_private_data
;
235 if ( (address
< (unsigned long) area
->vm_start
) ||
236 (address
> (unsigned long) area
->vm_start
+ (dma
->n_pages
<< PAGE_SHIFT
)) )
240 *type
= VM_FAULT_MINOR
;
241 offset
= address
- area
->vm_start
;
242 kernel_virt_addr
= (unsigned long) dma
->kvirt
+ offset
;
243 ret
= vmalloc_to_page((void*) kernel_virt_addr
);
249 static struct vm_operations_struct dma_region_vm_ops
= {
250 .nopage
= dma_region_pagefault
,
253 int dma_region_mmap(struct dma_region
*dma
, struct file
*file
, struct vm_area_struct
*vma
)
260 /* must be page-aligned */
261 if (vma
->vm_pgoff
!= 0)
264 /* check the length */
265 size
= vma
->vm_end
- vma
->vm_start
;
266 if (size
> (dma
->n_pages
<< PAGE_SHIFT
))
269 vma
->vm_ops
= &dma_region_vm_ops
;
270 vma
->vm_private_data
= dma
;
272 vma
->vm_flags
|= VM_RESERVED
;
277 #else /* CONFIG_MMU */
279 int dma_region_mmap(struct dma_region
*dma
, struct file
*file
, struct vm_area_struct
*vma
)
284 #endif /* CONFIG_MMU */