Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 1 | /* vm.c -- Memory mapping for DRM -*- linux-c -*- |
| 2 | * Created: Mon Jan 4 08:58:31 1999 by faith@precisioninsight.com |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 3 | * |
Kevin E Martin | 99efe3c | 2000-05-18 06:14:27 +0000 | [diff] [blame^] | 4 | * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas. |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 5 | * All Rights Reserved. |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | * copy of this software and associated documentation files (the "Software"), |
| 9 | * to deal in the Software without restriction, including without limitation |
| 10 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 | * and/or sell copies of the Software, and to permit persons to whom the |
| 12 | * Software is furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice (including the next |
| 15 | * paragraph) shall be included in all copies or substantial portions of the |
| 16 | * Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 21 | * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 22 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 23 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 24 | * DEALINGS IN THE SOFTWARE. |
| 25 | * |
Jeff Hartmann | 5bd8014 | 2000-03-16 03:37:30 +0000 | [diff] [blame] | 26 | * $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/kernel/vm.c,v 1.5 2000/02/23 04:47:31 martin Exp $ |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 27 | * |
| 28 | */ |
| 29 | |
| 30 | #define __NO_VERSION__ |
| 31 | #include "drmP.h" |
| 32 | |
| 33 | struct vm_operations_struct drm_vm_ops = { |
| 34 | nopage: drm_vm_nopage, |
| 35 | open: drm_vm_open, |
| 36 | close: drm_vm_close, |
| 37 | }; |
| 38 | |
| 39 | struct vm_operations_struct drm_vm_shm_ops = { |
| 40 | nopage: drm_vm_shm_nopage, |
| 41 | open: drm_vm_open, |
| 42 | close: drm_vm_close, |
| 43 | }; |
| 44 | |
| 45 | struct vm_operations_struct drm_vm_dma_ops = { |
| 46 | nopage: drm_vm_dma_nopage, |
| 47 | open: drm_vm_open, |
| 48 | close: drm_vm_close, |
| 49 | }; |
| 50 | |
Daryll Strauss | e1dba5c | 1999-12-07 03:37:16 +0000 | [diff] [blame] | 51 | #if LINUX_VERSION_CODE < 0x020317 |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 52 | unsigned long drm_vm_nopage(struct vm_area_struct *vma, |
| 53 | unsigned long address, |
| 54 | int write_access) |
Daryll Strauss | e1dba5c | 1999-12-07 03:37:16 +0000 | [diff] [blame] | 55 | #else |
| 56 | /* Return type changed in 2.3.23 */ |
| 57 | struct page *drm_vm_nopage(struct vm_area_struct *vma, |
| 58 | unsigned long address, |
| 59 | int write_access) |
| 60 | #endif |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 61 | { |
| 62 | DRM_DEBUG("0x%08lx, %d\n", address, write_access); |
| 63 | |
Daryll Strauss | e1dba5c | 1999-12-07 03:37:16 +0000 | [diff] [blame] | 64 | return NOPAGE_SIGBUS; /* Disallow mremap */ |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Daryll Strauss | e1dba5c | 1999-12-07 03:37:16 +0000 | [diff] [blame] | 67 | #if LINUX_VERSION_CODE < 0x020317 |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 68 | unsigned long drm_vm_shm_nopage(struct vm_area_struct *vma, |
| 69 | unsigned long address, |
| 70 | int write_access) |
Daryll Strauss | e1dba5c | 1999-12-07 03:37:16 +0000 | [diff] [blame] | 71 | #else |
| 72 | /* Return type changed in 2.3.23 */ |
| 73 | struct page *drm_vm_shm_nopage(struct vm_area_struct *vma, |
| 74 | unsigned long address, |
| 75 | int write_access) |
| 76 | #endif |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 77 | { |
| 78 | drm_file_t *priv = vma->vm_file->private_data; |
| 79 | drm_device_t *dev = priv->dev; |
| 80 | unsigned long physical; |
| 81 | unsigned long offset; |
| 82 | unsigned long page; |
| 83 | |
Daryll Strauss | e1dba5c | 1999-12-07 03:37:16 +0000 | [diff] [blame] | 84 | if (address > vma->vm_end) return NOPAGE_SIGBUS; /* Disallow mremap */ |
| 85 | if (!dev->lock.hw_lock) return NOPAGE_OOM; /* Nothing allocated */ |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 86 | |
| 87 | offset = address - vma->vm_start; |
| 88 | page = offset >> PAGE_SHIFT; |
| 89 | physical = (unsigned long)dev->lock.hw_lock + (offset & (~PAGE_MASK)); |
| 90 | atomic_inc(&mem_map[MAP_NR(physical)].count); /* Dec. by kernel */ |
| 91 | |
| 92 | DRM_DEBUG("0x%08lx (page %lu) => 0x%08lx\n", address, page, physical); |
Daryll Strauss | e1dba5c | 1999-12-07 03:37:16 +0000 | [diff] [blame] | 93 | #if LINUX_VERSION_CODE < 0x020317 |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 94 | return physical; |
Daryll Strauss | e1dba5c | 1999-12-07 03:37:16 +0000 | [diff] [blame] | 95 | #else |
| 96 | return mem_map + MAP_NR(physical); |
| 97 | #endif |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Daryll Strauss | e1dba5c | 1999-12-07 03:37:16 +0000 | [diff] [blame] | 100 | #if LINUX_VERSION_CODE < 0x020317 |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 101 | unsigned long drm_vm_dma_nopage(struct vm_area_struct *vma, |
| 102 | unsigned long address, |
| 103 | int write_access) |
Daryll Strauss | e1dba5c | 1999-12-07 03:37:16 +0000 | [diff] [blame] | 104 | #else |
| 105 | /* Return type changed in 2.3.23 */ |
| 106 | struct page *drm_vm_dma_nopage(struct vm_area_struct *vma, |
| 107 | unsigned long address, |
| 108 | int write_access) |
| 109 | #endif |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 110 | { |
| 111 | drm_file_t *priv = vma->vm_file->private_data; |
| 112 | drm_device_t *dev = priv->dev; |
| 113 | drm_device_dma_t *dma = dev->dma; |
| 114 | unsigned long physical; |
| 115 | unsigned long offset; |
| 116 | unsigned long page; |
| 117 | |
Daryll Strauss | e1dba5c | 1999-12-07 03:37:16 +0000 | [diff] [blame] | 118 | if (!dma) return NOPAGE_SIGBUS; /* Error */ |
| 119 | if (address > vma->vm_end) return NOPAGE_SIGBUS; /* Disallow mremap */ |
| 120 | if (!dma->pagelist) return NOPAGE_OOM ; /* Nothing allocated */ |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 121 | |
Daryll Strauss | e1dba5c | 1999-12-07 03:37:16 +0000 | [diff] [blame] | 122 | offset = address - vma->vm_start; /* vm_[pg]off[set] should be 0 */ |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 123 | page = offset >> PAGE_SHIFT; |
| 124 | physical = dma->pagelist[page] + (offset & (~PAGE_MASK)); |
| 125 | atomic_inc(&mem_map[MAP_NR(physical)].count); /* Dec. by kernel */ |
| 126 | |
| 127 | DRM_DEBUG("0x%08lx (page %lu) => 0x%08lx\n", address, page, physical); |
Daryll Strauss | e1dba5c | 1999-12-07 03:37:16 +0000 | [diff] [blame] | 128 | #if LINUX_VERSION_CODE < 0x020317 |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 129 | return physical; |
Daryll Strauss | e1dba5c | 1999-12-07 03:37:16 +0000 | [diff] [blame] | 130 | #else |
| 131 | return mem_map + MAP_NR(physical); |
| 132 | #endif |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void drm_vm_open(struct vm_area_struct *vma) |
| 136 | { |
| 137 | drm_file_t *priv = vma->vm_file->private_data; |
| 138 | drm_device_t *dev = priv->dev; |
| 139 | #if DRM_DEBUG_CODE |
| 140 | drm_vma_entry_t *vma_entry; |
| 141 | #endif |
| 142 | |
| 143 | DRM_DEBUG("0x%08lx,0x%08lx\n", |
| 144 | vma->vm_start, vma->vm_end - vma->vm_start); |
| 145 | atomic_inc(&dev->vma_count); |
| 146 | MOD_INC_USE_COUNT; |
| 147 | |
| 148 | #if DRM_DEBUG_CODE |
| 149 | vma_entry = drm_alloc(sizeof(*vma_entry), DRM_MEM_VMAS); |
| 150 | if (vma_entry) { |
| 151 | down(&dev->struct_sem); |
| 152 | vma_entry->vma = vma; |
| 153 | vma_entry->next = dev->vmalist; |
| 154 | vma_entry->pid = current->pid; |
| 155 | dev->vmalist = vma_entry; |
| 156 | up(&dev->struct_sem); |
| 157 | } |
| 158 | #endif |
| 159 | } |
| 160 | |
| 161 | void drm_vm_close(struct vm_area_struct *vma) |
| 162 | { |
| 163 | drm_file_t *priv = vma->vm_file->private_data; |
| 164 | drm_device_t *dev = priv->dev; |
| 165 | #if DRM_DEBUG_CODE |
| 166 | drm_vma_entry_t *pt, *prev; |
| 167 | #endif |
| 168 | |
| 169 | DRM_DEBUG("0x%08lx,0x%08lx\n", |
| 170 | vma->vm_start, vma->vm_end - vma->vm_start); |
| 171 | MOD_DEC_USE_COUNT; |
| 172 | atomic_dec(&dev->vma_count); |
| 173 | |
| 174 | #if DRM_DEBUG_CODE |
| 175 | down(&dev->struct_sem); |
| 176 | for (pt = dev->vmalist, prev = NULL; pt; prev = pt, pt = pt->next) { |
| 177 | if (pt->vma == vma) { |
| 178 | if (prev) { |
| 179 | prev->next = pt->next; |
| 180 | } else { |
| 181 | dev->vmalist = pt->next; |
| 182 | } |
| 183 | drm_free(pt, sizeof(*pt), DRM_MEM_VMAS); |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | up(&dev->struct_sem); |
| 188 | #endif |
| 189 | } |
| 190 | |
| 191 | int drm_mmap_dma(struct file *filp, struct vm_area_struct *vma) |
| 192 | { |
| 193 | drm_file_t *priv = filp->private_data; |
| 194 | drm_device_t *dev = priv->dev; |
| 195 | drm_device_dma_t *dma = dev->dma; |
| 196 | unsigned long length = vma->vm_end - vma->vm_start; |
| 197 | |
| 198 | DRM_DEBUG("start = 0x%lx, end = 0x%lx, offset = 0x%lx\n", |
Daryll Strauss | e1dba5c | 1999-12-07 03:37:16 +0000 | [diff] [blame] | 199 | vma->vm_start, vma->vm_end, VM_OFFSET(vma)); |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 200 | |
| 201 | /* Length must match exact page count */ |
| 202 | if ((length >> PAGE_SHIFT) != dma->page_count) return -EINVAL; |
| 203 | |
| 204 | vma->vm_ops = &drm_vm_dma_ops; |
| 205 | vma->vm_flags |= VM_LOCKED | VM_SHM; /* Don't swap */ |
| 206 | |
| 207 | #if LINUX_VERSION_CODE < 0x020203 /* KERNEL_VERSION(2,2,3) */ |
| 208 | /* In Linux 2.2.3 and above, this is |
| 209 | handled in do_mmap() in mm/mmap.c. */ |
| 210 | ++filp->f_count; |
| 211 | #endif |
| 212 | vma->vm_file = filp; /* Needed for drm_vm_open() */ |
| 213 | drm_vm_open(vma); |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | int drm_mmap(struct file *filp, struct vm_area_struct *vma) |
| 218 | { |
| 219 | drm_file_t *priv = filp->private_data; |
| 220 | drm_device_t *dev = priv->dev; |
| 221 | drm_map_t *map = NULL; |
| 222 | int i; |
| 223 | |
| 224 | DRM_DEBUG("start = 0x%lx, end = 0x%lx, offset = 0x%lx\n", |
Daryll Strauss | e1dba5c | 1999-12-07 03:37:16 +0000 | [diff] [blame] | 225 | vma->vm_start, vma->vm_end, VM_OFFSET(vma)); |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 226 | |
Daryll Strauss | e1dba5c | 1999-12-07 03:37:16 +0000 | [diff] [blame] | 227 | if (!VM_OFFSET(vma)) return drm_mmap_dma(filp, vma); |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 228 | |
| 229 | /* A sequential search of a linked list is |
| 230 | fine here because: 1) there will only be |
| 231 | about 5-10 entries in the list and, 2) a |
| 232 | DRI client only has to do this mapping |
| 233 | once, so it doesn't have to be optimized |
| 234 | for performance, even if the list was a |
| 235 | bit longer. */ |
| 236 | for (i = 0; i < dev->map_count; i++) { |
| 237 | map = dev->maplist[i]; |
Daryll Strauss | e1dba5c | 1999-12-07 03:37:16 +0000 | [diff] [blame] | 238 | if (map->offset == VM_OFFSET(vma)) break; |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | if (i >= dev->map_count) return -EINVAL; |
| 242 | if (!map || ((map->flags&_DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) |
| 243 | return -EPERM; |
| 244 | |
| 245 | /* Check for valid size. */ |
| 246 | if (map->size != vma->vm_end - vma->vm_start) return -EINVAL; |
| 247 | |
Kevin E Martin | 99efe3c | 2000-05-18 06:14:27 +0000 | [diff] [blame^] | 248 | if (!capable(CAP_SYS_ADMIN) && (map->flags & _DRM_READ_ONLY)) { |
| 249 | vma->vm_flags &= VM_MAYWRITE; |
| 250 | #if defined(__i386__) |
| 251 | pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW; |
| 252 | #else |
| 253 | /* Ye gads this is ugly. With more thought |
| 254 | we could move this up higher and use |
| 255 | `protection_map' instead. */ |
| 256 | vma->vm_page_prot = __pgprot(pte_val(pte_wrprotect( |
| 257 | __pte(pgprot_val(vma->vm_page_prot))))); |
| 258 | #endif |
| 259 | } |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 260 | |
| 261 | switch (map->type) { |
| 262 | case _DRM_FRAME_BUFFER: |
| 263 | case _DRM_REGISTERS: |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 264 | case _DRM_AGP: |
Daryll Strauss | e1dba5c | 1999-12-07 03:37:16 +0000 | [diff] [blame] | 265 | if (VM_OFFSET(vma) >= __pa(high_memory)) { |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 266 | #if defined(__i386__) |
Jeff Hartmann | ba1b1ae | 2000-04-04 22:08:14 +0000 | [diff] [blame] | 267 | if (boot_cpu_data.x86 > 3 && map->type != _DRM_AGP) { |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 268 | pgprot_val(vma->vm_page_prot) |= _PAGE_PCD; |
| 269 | pgprot_val(vma->vm_page_prot) &= ~_PAGE_PWT; |
| 270 | } |
| 271 | #endif |
| 272 | vma->vm_flags |= VM_IO; /* not in core dump */ |
| 273 | } |
| 274 | if (remap_page_range(vma->vm_start, |
Daryll Strauss | e1dba5c | 1999-12-07 03:37:16 +0000 | [diff] [blame] | 275 | VM_OFFSET(vma), |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 276 | vma->vm_end - vma->vm_start, |
| 277 | vma->vm_page_prot)) |
| 278 | return -EAGAIN; |
Kevin E Martin | 99efe3c | 2000-05-18 06:14:27 +0000 | [diff] [blame^] | 279 | DRM_DEBUG(" Type = %d; start = 0x%lx, end = 0x%lx," |
| 280 | " offset = 0x%lx\n", |
| 281 | map->type, |
| 282 | vma->vm_start, vma->vm_end, VM_OFFSET(vma)); |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 283 | vma->vm_ops = &drm_vm_ops; |
| 284 | break; |
| 285 | case _DRM_SHM: |
| 286 | vma->vm_ops = &drm_vm_shm_ops; |
| 287 | /* Don't let this area swap. Change when |
| 288 | DRM_KERNEL advisory is supported. */ |
| 289 | vma->vm_flags |= VM_LOCKED; |
| 290 | break; |
| 291 | default: |
| 292 | return -EINVAL; /* This should never happen. */ |
| 293 | } |
| 294 | vma->vm_flags |= VM_LOCKED | VM_SHM; /* Don't swap */ |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 295 | |
Daryll Strauss | b6a28bf | 1999-12-05 23:10:37 +0000 | [diff] [blame] | 296 | #if LINUX_VERSION_CODE < 0x020203 /* KERNEL_VERSION(2,2,3) */ |
| 297 | /* In Linux 2.2.3 and above, this is |
| 298 | handled in do_mmap() in mm/mmap.c. */ |
| 299 | ++filp->f_count; |
| 300 | #endif |
| 301 | vma->vm_file = filp; /* Needed for drm_vm_open() */ |
| 302 | drm_vm_open(vma); |
| 303 | return 0; |
| 304 | } |