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