Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * DMA region bookkeeping routines |
| 3 | * |
| 4 | * Copyright (C) 2002 Maas Digital LLC |
| 5 | * |
| 6 | * This code is licensed under the GPL. See the file COPYING in the root |
| 7 | * directory of the kernel sources for details. |
| 8 | */ |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/mm.h> |
Stefan Richter | de4394f | 2006-07-03 12:02:29 -0400 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/pci.h> |
Stefan Richter | de4394f | 2006-07-03 12:02:29 -0400 | [diff] [blame] | 13 | #include <linux/vmalloc.h> |
Ralf Baechle | 1176360 | 2007-10-23 20:42:11 +0200 | [diff] [blame] | 14 | #include <linux/scatterlist.h> |
Stefan Richter | de4394f | 2006-07-03 12:02:29 -0400 | [diff] [blame] | 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include "dma.h" |
| 17 | |
| 18 | /* dma_prog_region */ |
| 19 | |
| 20 | void dma_prog_region_init(struct dma_prog_region *prog) |
| 21 | { |
| 22 | prog->kvirt = NULL; |
| 23 | prog->dev = NULL; |
| 24 | prog->n_pages = 0; |
| 25 | prog->bus_addr = 0; |
| 26 | } |
| 27 | |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 28 | int dma_prog_region_alloc(struct dma_prog_region *prog, unsigned long n_bytes, |
| 29 | struct pci_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | { |
| 31 | /* round up to page size */ |
| 32 | n_bytes = PAGE_ALIGN(n_bytes); |
| 33 | |
| 34 | prog->n_pages = n_bytes >> PAGE_SHIFT; |
| 35 | |
| 36 | prog->kvirt = pci_alloc_consistent(dev, n_bytes, &prog->bus_addr); |
| 37 | if (!prog->kvirt) { |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 38 | printk(KERN_ERR |
| 39 | "dma_prog_region_alloc: pci_alloc_consistent() failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | dma_prog_region_free(prog); |
| 41 | return -ENOMEM; |
| 42 | } |
| 43 | |
| 44 | prog->dev = dev; |
| 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | void dma_prog_region_free(struct dma_prog_region *prog) |
| 50 | { |
| 51 | if (prog->kvirt) { |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 52 | pci_free_consistent(prog->dev, prog->n_pages << PAGE_SHIFT, |
| 53 | prog->kvirt, prog->bus_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | prog->kvirt = NULL; |
| 57 | prog->dev = NULL; |
| 58 | prog->n_pages = 0; |
| 59 | prog->bus_addr = 0; |
| 60 | } |
| 61 | |
| 62 | /* dma_region */ |
| 63 | |
Stefan Richter | afd6546 | 2007-03-05 03:06:23 +0100 | [diff] [blame] | 64 | /** |
| 65 | * dma_region_init - clear out all fields but do not allocate anything |
| 66 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | void dma_region_init(struct dma_region *dma) |
| 68 | { |
| 69 | dma->kvirt = NULL; |
| 70 | dma->dev = NULL; |
| 71 | dma->n_pages = 0; |
| 72 | dma->n_dma_pages = 0; |
| 73 | dma->sglist = NULL; |
| 74 | } |
| 75 | |
Stefan Richter | afd6546 | 2007-03-05 03:06:23 +0100 | [diff] [blame] | 76 | /** |
| 77 | * dma_region_alloc - allocate the buffer and map it to the IOMMU |
| 78 | */ |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 79 | int dma_region_alloc(struct dma_region *dma, unsigned long n_bytes, |
| 80 | struct pci_dev *dev, int direction) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
| 82 | unsigned int i; |
| 83 | |
| 84 | /* round up to page size */ |
| 85 | n_bytes = PAGE_ALIGN(n_bytes); |
| 86 | |
| 87 | dma->n_pages = n_bytes >> PAGE_SHIFT; |
| 88 | |
| 89 | dma->kvirt = vmalloc_32(n_bytes); |
| 90 | if (!dma->kvirt) { |
| 91 | printk(KERN_ERR "dma_region_alloc: vmalloc_32() failed\n"); |
| 92 | goto err; |
| 93 | } |
| 94 | |
| 95 | /* Clear the ram out, no junk to the user */ |
| 96 | memset(dma->kvirt, 0, n_bytes); |
| 97 | |
| 98 | /* allocate scatter/gather list */ |
| 99 | dma->sglist = vmalloc(dma->n_pages * sizeof(*dma->sglist)); |
| 100 | if (!dma->sglist) { |
| 101 | printk(KERN_ERR "dma_region_alloc: vmalloc(sglist) failed\n"); |
| 102 | goto err; |
| 103 | } |
| 104 | |
Jens Axboe | 9e66269 | 2007-11-04 09:44:56 +0100 | [diff] [blame] | 105 | sg_init_table(dma->sglist, dma->n_pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | /* fill scatter/gather list with pages */ |
| 108 | for (i = 0; i < dma->n_pages; i++) { |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 109 | unsigned long va = |
| 110 | (unsigned long)dma->kvirt + (i << PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Jens Axboe | 642f149 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 112 | sg_set_page(&dma->sglist[i], vmalloc_to_page((void *)va), |
| 113 | PAGE_SIZE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /* map sglist to the IOMMU */ |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 117 | dma->n_dma_pages = |
| 118 | pci_map_sg(dev, dma->sglist, dma->n_pages, direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
| 120 | if (dma->n_dma_pages == 0) { |
| 121 | printk(KERN_ERR "dma_region_alloc: pci_map_sg() failed\n"); |
| 122 | goto err; |
| 123 | } |
| 124 | |
| 125 | dma->dev = dev; |
| 126 | dma->direction = direction; |
| 127 | |
| 128 | return 0; |
| 129 | |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 130 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | dma_region_free(dma); |
| 132 | return -ENOMEM; |
| 133 | } |
| 134 | |
Stefan Richter | afd6546 | 2007-03-05 03:06:23 +0100 | [diff] [blame] | 135 | /** |
| 136 | * dma_region_free - unmap and free the buffer |
| 137 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | void dma_region_free(struct dma_region *dma) |
| 139 | { |
| 140 | if (dma->n_dma_pages) { |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 141 | pci_unmap_sg(dma->dev, dma->sglist, dma->n_pages, |
| 142 | dma->direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | dma->n_dma_pages = 0; |
| 144 | dma->dev = NULL; |
| 145 | } |
| 146 | |
| 147 | vfree(dma->sglist); |
| 148 | dma->sglist = NULL; |
| 149 | |
| 150 | vfree(dma->kvirt); |
| 151 | dma->kvirt = NULL; |
| 152 | dma->n_pages = 0; |
| 153 | } |
| 154 | |
| 155 | /* find the scatterlist index and remaining offset corresponding to a |
| 156 | given offset from the beginning of the buffer */ |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 157 | static inline int dma_region_find(struct dma_region *dma, unsigned long offset, |
Ben Collins | 9bb2bcd | 2006-06-12 17:52:59 -0400 | [diff] [blame] | 158 | unsigned int start, unsigned long *rem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { |
| 160 | int i; |
| 161 | unsigned long off = offset; |
| 162 | |
Ben Collins | 9bb2bcd | 2006-06-12 17:52:59 -0400 | [diff] [blame] | 163 | for (i = start; i < dma->n_dma_pages; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | if (off < sg_dma_len(&dma->sglist[i])) { |
| 165 | *rem = off; |
| 166 | break; |
| 167 | } |
| 168 | |
| 169 | off -= sg_dma_len(&dma->sglist[i]); |
| 170 | } |
| 171 | |
| 172 | BUG_ON(i >= dma->n_dma_pages); |
| 173 | |
| 174 | return i; |
| 175 | } |
| 176 | |
Stefan Richter | afd6546 | 2007-03-05 03:06:23 +0100 | [diff] [blame] | 177 | /** |
| 178 | * dma_region_offset_to_bus - get bus address of an offset within a DMA region |
| 179 | * |
| 180 | * Returns the DMA bus address of the byte with the given @offset relative to |
| 181 | * the beginning of the @dma. |
| 182 | */ |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 183 | dma_addr_t dma_region_offset_to_bus(struct dma_region * dma, |
| 184 | unsigned long offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
Ben Collins | 1934b8b | 2005-07-09 20:01:23 -0400 | [diff] [blame] | 186 | unsigned long rem = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 188 | struct scatterlist *sg = |
Ben Collins | 9bb2bcd | 2006-06-12 17:52:59 -0400 | [diff] [blame] | 189 | &dma->sglist[dma_region_find(dma, offset, 0, &rem)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | return sg_dma_address(sg) + rem; |
| 191 | } |
| 192 | |
Stefan Richter | afd6546 | 2007-03-05 03:06:23 +0100 | [diff] [blame] | 193 | /** |
| 194 | * dma_region_sync_for_cpu - sync the CPU's view of the buffer |
| 195 | */ |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 196 | void dma_region_sync_for_cpu(struct dma_region *dma, unsigned long offset, |
| 197 | unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
| 199 | int first, last; |
Ben Collins | 9bb2bcd | 2006-06-12 17:52:59 -0400 | [diff] [blame] | 200 | unsigned long rem = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
| 202 | if (!len) |
| 203 | len = 1; |
| 204 | |
Ben Collins | 9bb2bcd | 2006-06-12 17:52:59 -0400 | [diff] [blame] | 205 | first = dma_region_find(dma, offset, 0, &rem); |
| 206 | last = dma_region_find(dma, rem + len - 1, first, &rem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 208 | pci_dma_sync_sg_for_cpu(dma->dev, &dma->sglist[first], last - first + 1, |
| 209 | dma->direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Stefan Richter | afd6546 | 2007-03-05 03:06:23 +0100 | [diff] [blame] | 212 | /** |
| 213 | * dma_region_sync_for_device - sync the IO bus' view of the buffer |
| 214 | */ |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 215 | void dma_region_sync_for_device(struct dma_region *dma, unsigned long offset, |
| 216 | unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | { |
| 218 | int first, last; |
Ben Collins | 9bb2bcd | 2006-06-12 17:52:59 -0400 | [diff] [blame] | 219 | unsigned long rem = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
| 221 | if (!len) |
| 222 | len = 1; |
| 223 | |
Ben Collins | 9bb2bcd | 2006-06-12 17:52:59 -0400 | [diff] [blame] | 224 | first = dma_region_find(dma, offset, 0, &rem); |
| 225 | last = dma_region_find(dma, rem + len - 1, first, &rem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 227 | pci_dma_sync_sg_for_device(dma->dev, &dma->sglist[first], |
| 228 | last - first + 1, dma->direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | #ifdef CONFIG_MMU |
| 232 | |
Nick Piggin | 61db812 | 2007-12-05 18:15:53 +1100 | [diff] [blame] | 233 | static int dma_region_pagefault(struct vm_area_struct *vma, |
Stefan Richter | c7ea990 | 2007-12-15 14:04:42 +0100 | [diff] [blame] | 234 | struct vm_fault *vmf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | { |
Nick Piggin | 61db812 | 2007-12-05 18:15:53 +1100 | [diff] [blame] | 236 | struct dma_region *dma = (struct dma_region *)vma->vm_private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
| 238 | if (!dma->kvirt) |
Stefan Richter | c7ea990 | 2007-12-15 14:04:42 +0100 | [diff] [blame] | 239 | return VM_FAULT_SIGBUS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
Nick Piggin | 61db812 | 2007-12-05 18:15:53 +1100 | [diff] [blame] | 241 | if (vmf->pgoff >= dma->n_pages) |
Stefan Richter | c7ea990 | 2007-12-15 14:04:42 +0100 | [diff] [blame] | 242 | return VM_FAULT_SIGBUS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Stefan Richter | c7ea990 | 2007-12-15 14:04:42 +0100 | [diff] [blame] | 244 | vmf->page = vmalloc_to_page(dma->kvirt + (vmf->pgoff << PAGE_SHIFT)); |
Nick Piggin | 61db812 | 2007-12-05 18:15:53 +1100 | [diff] [blame] | 245 | get_page(vmf->page); |
| 246 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Alexey Dobriyan | f0f37e2 | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 249 | static const struct vm_operations_struct dma_region_vm_ops = { |
Nick Piggin | 61db812 | 2007-12-05 18:15:53 +1100 | [diff] [blame] | 250 | .fault = dma_region_pagefault, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | }; |
| 252 | |
Stefan Richter | afd6546 | 2007-03-05 03:06:23 +0100 | [diff] [blame] | 253 | /** |
| 254 | * dma_region_mmap - map the buffer into a user space process |
| 255 | */ |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 256 | int dma_region_mmap(struct dma_region *dma, struct file *file, |
| 257 | struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | { |
| 259 | unsigned long size; |
| 260 | |
| 261 | if (!dma->kvirt) |
| 262 | return -EINVAL; |
| 263 | |
Nick Piggin | 61db812 | 2007-12-05 18:15:53 +1100 | [diff] [blame] | 264 | /* must be page-aligned (XXX: comment is wrong, we could allow pgoff) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | if (vma->vm_pgoff != 0) |
| 266 | return -EINVAL; |
| 267 | |
| 268 | /* check the length */ |
| 269 | size = vma->vm_end - vma->vm_start; |
| 270 | if (size > (dma->n_pages << PAGE_SHIFT)) |
| 271 | return -EINVAL; |
| 272 | |
| 273 | vma->vm_ops = &dma_region_vm_ops; |
| 274 | vma->vm_private_data = dma; |
| 275 | vma->vm_file = file; |
Philippe De Muyter | 435f972 | 2008-07-03 18:52:28 +0200 | [diff] [blame] | 276 | vma->vm_flags |= VM_RESERVED | VM_ALWAYSDUMP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 281 | #else /* CONFIG_MMU */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 283 | int dma_region_mmap(struct dma_region *dma, struct file *file, |
| 284 | struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | { |
| 286 | return -EINVAL; |
| 287 | } |
| 288 | |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 289 | #endif /* CONFIG_MMU */ |