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