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 | |
| 65 | void dma_region_init(struct dma_region *dma) |
| 66 | { |
| 67 | dma->kvirt = NULL; |
| 68 | dma->dev = NULL; |
| 69 | dma->n_pages = 0; |
| 70 | dma->n_dma_pages = 0; |
| 71 | dma->sglist = NULL; |
| 72 | } |
| 73 | |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 74 | int dma_region_alloc(struct dma_region *dma, unsigned long n_bytes, |
| 75 | struct pci_dev *dev, int direction) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
| 77 | unsigned int i; |
| 78 | |
| 79 | /* round up to page size */ |
| 80 | n_bytes = PAGE_ALIGN(n_bytes); |
| 81 | |
| 82 | dma->n_pages = n_bytes >> PAGE_SHIFT; |
| 83 | |
| 84 | dma->kvirt = vmalloc_32(n_bytes); |
| 85 | if (!dma->kvirt) { |
| 86 | printk(KERN_ERR "dma_region_alloc: vmalloc_32() failed\n"); |
| 87 | goto err; |
| 88 | } |
| 89 | |
| 90 | /* Clear the ram out, no junk to the user */ |
| 91 | memset(dma->kvirt, 0, n_bytes); |
| 92 | |
| 93 | /* allocate scatter/gather list */ |
| 94 | dma->sglist = vmalloc(dma->n_pages * sizeof(*dma->sglist)); |
| 95 | if (!dma->sglist) { |
| 96 | printk(KERN_ERR "dma_region_alloc: vmalloc(sglist) failed\n"); |
| 97 | goto err; |
| 98 | } |
| 99 | |
| 100 | /* just to be safe - this will become unnecessary once sglist->address goes away */ |
| 101 | memset(dma->sglist, 0, dma->n_pages * sizeof(*dma->sglist)); |
| 102 | |
| 103 | /* fill scatter/gather list with pages */ |
| 104 | for (i = 0; i < dma->n_pages; i++) { |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 105 | unsigned long va = |
| 106 | (unsigned long)dma->kvirt + (i << PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | dma->sglist[i].page = vmalloc_to_page((void *)va); |
| 109 | dma->sglist[i].length = PAGE_SIZE; |
| 110 | } |
| 111 | |
| 112 | /* map sglist to the IOMMU */ |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 113 | dma->n_dma_pages = |
| 114 | pci_map_sg(dev, dma->sglist, dma->n_pages, direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | if (dma->n_dma_pages == 0) { |
| 117 | printk(KERN_ERR "dma_region_alloc: pci_map_sg() failed\n"); |
| 118 | goto err; |
| 119 | } |
| 120 | |
| 121 | dma->dev = dev; |
| 122 | dma->direction = direction; |
| 123 | |
| 124 | return 0; |
| 125 | |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 126 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | dma_region_free(dma); |
| 128 | return -ENOMEM; |
| 129 | } |
| 130 | |
| 131 | void dma_region_free(struct dma_region *dma) |
| 132 | { |
| 133 | if (dma->n_dma_pages) { |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 134 | pci_unmap_sg(dma->dev, dma->sglist, dma->n_pages, |
| 135 | dma->direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | dma->n_dma_pages = 0; |
| 137 | dma->dev = NULL; |
| 138 | } |
| 139 | |
| 140 | vfree(dma->sglist); |
| 141 | dma->sglist = NULL; |
| 142 | |
| 143 | vfree(dma->kvirt); |
| 144 | dma->kvirt = NULL; |
| 145 | dma->n_pages = 0; |
| 146 | } |
| 147 | |
| 148 | /* find the scatterlist index and remaining offset corresponding to a |
| 149 | given offset from the beginning of the buffer */ |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 150 | 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] | 151 | unsigned int start, unsigned long *rem) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | { |
| 153 | int i; |
| 154 | unsigned long off = offset; |
| 155 | |
Ben Collins | 9bb2bcd | 2006-06-12 17:52:59 -0400 | [diff] [blame] | 156 | for (i = start; i < dma->n_dma_pages; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | if (off < sg_dma_len(&dma->sglist[i])) { |
| 158 | *rem = off; |
| 159 | break; |
| 160 | } |
| 161 | |
| 162 | off -= sg_dma_len(&dma->sglist[i]); |
| 163 | } |
| 164 | |
| 165 | BUG_ON(i >= dma->n_dma_pages); |
| 166 | |
| 167 | return i; |
| 168 | } |
| 169 | |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 170 | dma_addr_t dma_region_offset_to_bus(struct dma_region * dma, |
| 171 | unsigned long offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
Ben Collins | 1934b8b | 2005-07-09 20:01:23 -0400 | [diff] [blame] | 173 | unsigned long rem = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 175 | struct scatterlist *sg = |
Ben Collins | 9bb2bcd | 2006-06-12 17:52:59 -0400 | [diff] [blame] | 176 | &dma->sglist[dma_region_find(dma, offset, 0, &rem)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | return sg_dma_address(sg) + rem; |
| 178 | } |
| 179 | |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 180 | void dma_region_sync_for_cpu(struct dma_region *dma, unsigned long offset, |
| 181 | unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
| 183 | int first, last; |
Ben Collins | 9bb2bcd | 2006-06-12 17:52:59 -0400 | [diff] [blame] | 184 | unsigned long rem = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | if (!len) |
| 187 | len = 1; |
| 188 | |
Ben Collins | 9bb2bcd | 2006-06-12 17:52:59 -0400 | [diff] [blame] | 189 | first = dma_region_find(dma, offset, 0, &rem); |
| 190 | last = dma_region_find(dma, rem + len - 1, first, &rem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 192 | pci_dma_sync_sg_for_cpu(dma->dev, &dma->sglist[first], last - first + 1, |
| 193 | dma->direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 196 | void dma_region_sync_for_device(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_device(dma->dev, &dma->sglist[first], |
| 209 | last - first + 1, dma->direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | #ifdef CONFIG_MMU |
| 213 | |
| 214 | /* nopage() handler for mmap access */ |
| 215 | |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 216 | static struct page *dma_region_pagefault(struct vm_area_struct *area, |
| 217 | unsigned long address, int *type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
| 219 | unsigned long offset; |
| 220 | unsigned long kernel_virt_addr; |
| 221 | struct page *ret = NOPAGE_SIGBUS; |
| 222 | |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 223 | struct dma_region *dma = (struct dma_region *)area->vm_private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
| 225 | if (!dma->kvirt) |
| 226 | goto out; |
| 227 | |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 228 | if ((address < (unsigned long)area->vm_start) || |
| 229 | (address > |
| 230 | (unsigned long)area->vm_start + (dma->n_pages << PAGE_SHIFT))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | goto out; |
| 232 | |
| 233 | if (type) |
| 234 | *type = VM_FAULT_MINOR; |
| 235 | offset = address - area->vm_start; |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 236 | kernel_virt_addr = (unsigned long)dma->kvirt + offset; |
| 237 | ret = vmalloc_to_page((void *)kernel_virt_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | get_page(ret); |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 239 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | return ret; |
| 241 | } |
| 242 | |
| 243 | static struct vm_operations_struct dma_region_vm_ops = { |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 244 | .nopage = dma_region_pagefault, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | }; |
| 246 | |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 247 | int dma_region_mmap(struct dma_region *dma, struct file *file, |
| 248 | struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { |
| 250 | unsigned long size; |
| 251 | |
| 252 | if (!dma->kvirt) |
| 253 | return -EINVAL; |
| 254 | |
| 255 | /* must be page-aligned */ |
| 256 | if (vma->vm_pgoff != 0) |
| 257 | return -EINVAL; |
| 258 | |
| 259 | /* check the length */ |
| 260 | size = vma->vm_end - vma->vm_start; |
| 261 | if (size > (dma->n_pages << PAGE_SHIFT)) |
| 262 | return -EINVAL; |
| 263 | |
| 264 | vma->vm_ops = &dma_region_vm_ops; |
| 265 | vma->vm_private_data = dma; |
| 266 | vma->vm_file = file; |
| 267 | vma->vm_flags |= VM_RESERVED; |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 272 | #else /* CONFIG_MMU */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 274 | int dma_region_mmap(struct dma_region *dma, struct file *file, |
| 275 | struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
| 277 | return -EINVAL; |
| 278 | } |
| 279 | |
Jens-Michael Hoffmann | 6649e92 | 2005-11-22 12:18:28 -0500 | [diff] [blame] | 280 | #endif /* CONFIG_MMU */ |