1 1.2 riastrad /* $NetBSD: drm_vm.c,v 1.3 2021/12/18 23:44:57 riastradh Exp $ */ 2 1.2 riastrad 3 1.3 riastrad /* 4 1.1 riastrad * \file drm_vm.c 5 1.1 riastrad * Memory mapping for DRM 6 1.1 riastrad * 7 1.1 riastrad * \author Rickard E. (Rik) Faith <faith (at) valinux.com> 8 1.1 riastrad * \author Gareth Hughes <gareth (at) valinux.com> 9 1.1 riastrad */ 10 1.1 riastrad 11 1.1 riastrad /* 12 1.1 riastrad * Created: Mon Jan 4 08:58:31 1999 by faith (at) valinux.com 13 1.1 riastrad * 14 1.1 riastrad * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. 15 1.1 riastrad * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. 16 1.1 riastrad * All Rights Reserved. 17 1.1 riastrad * 18 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 19 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 20 1.1 riastrad * to deal in the Software without restriction, including without limitation 21 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 22 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 23 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 24 1.1 riastrad * 25 1.1 riastrad * The above copyright notice and this permission notice (including the next 26 1.1 riastrad * paragraph) shall be included in all copies or substantial portions of the 27 1.1 riastrad * Software. 28 1.1 riastrad * 29 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 30 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 31 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 32 1.1 riastrad * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 33 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 34 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 35 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE. 36 1.1 riastrad */ 37 1.1 riastrad 38 1.2 riastrad #include <sys/cdefs.h> 39 1.2 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_vm.c,v 1.3 2021/12/18 23:44:57 riastradh Exp $"); 40 1.2 riastrad 41 1.1 riastrad #include <linux/export.h> 42 1.3 riastrad #include <linux/pci.h> 43 1.2 riastrad #include <linux/seq_file.h> 44 1.3 riastrad #include <linux/vmalloc.h> 45 1.3 riastrad 46 1.1 riastrad #if defined(__ia64__) 47 1.1 riastrad #include <linux/efi.h> 48 1.1 riastrad #include <linux/slab.h> 49 1.1 riastrad #endif 50 1.3 riastrad #include <linux/mem_encrypt.h> 51 1.3 riastrad 52 1.2 riastrad #include <asm/pgtable.h> 53 1.3 riastrad 54 1.3 riastrad #include <drm/drm_agpsupport.h> 55 1.3 riastrad #include <drm/drm_device.h> 56 1.3 riastrad #include <drm/drm_drv.h> 57 1.3 riastrad #include <drm/drm_file.h> 58 1.3 riastrad #include <drm/drm_framebuffer.h> 59 1.3 riastrad #include <drm/drm_gem.h> 60 1.3 riastrad #include <drm/drm_print.h> 61 1.3 riastrad 62 1.2 riastrad #include "drm_internal.h" 63 1.2 riastrad #include "drm_legacy.h" 64 1.2 riastrad 65 1.2 riastrad struct drm_vma_entry { 66 1.2 riastrad struct list_head head; 67 1.2 riastrad struct vm_area_struct *vma; 68 1.2 riastrad pid_t pid; 69 1.2 riastrad }; 70 1.1 riastrad 71 1.1 riastrad static void drm_vm_open(struct vm_area_struct *vma); 72 1.1 riastrad static void drm_vm_close(struct vm_area_struct *vma); 73 1.1 riastrad 74 1.2 riastrad static pgprot_t drm_io_prot(struct drm_local_map *map, 75 1.2 riastrad struct vm_area_struct *vma) 76 1.1 riastrad { 77 1.1 riastrad pgprot_t tmp = vm_get_page_prot(vma->vm_flags); 78 1.1 riastrad 79 1.3 riastrad /* We don't want graphics memory to be mapped encrypted */ 80 1.3 riastrad tmp = pgprot_decrypted(tmp); 81 1.3 riastrad 82 1.3 riastrad #if defined(__i386__) || defined(__x86_64__) || defined(__powerpc__) || \ 83 1.3 riastrad defined(__mips__) 84 1.2 riastrad if (map->type == _DRM_REGISTERS && !(map->flags & _DRM_WRITE_COMBINING)) 85 1.2 riastrad tmp = pgprot_noncached(tmp); 86 1.2 riastrad else 87 1.2 riastrad tmp = pgprot_writecombine(tmp); 88 1.1 riastrad #elif defined(__ia64__) 89 1.1 riastrad if (efi_range_is_wc(vma->vm_start, vma->vm_end - 90 1.1 riastrad vma->vm_start)) 91 1.1 riastrad tmp = pgprot_writecombine(tmp); 92 1.1 riastrad else 93 1.1 riastrad tmp = pgprot_noncached(tmp); 94 1.3 riastrad #elif defined(__sparc__) || defined(__arm__) 95 1.1 riastrad tmp = pgprot_noncached(tmp); 96 1.1 riastrad #endif 97 1.1 riastrad return tmp; 98 1.1 riastrad } 99 1.1 riastrad 100 1.1 riastrad static pgprot_t drm_dma_prot(uint32_t map_type, struct vm_area_struct *vma) 101 1.1 riastrad { 102 1.1 riastrad pgprot_t tmp = vm_get_page_prot(vma->vm_flags); 103 1.1 riastrad 104 1.1 riastrad #if defined(__powerpc__) && defined(CONFIG_NOT_COHERENT_CACHE) 105 1.3 riastrad tmp = pgprot_noncached_wc(tmp); 106 1.1 riastrad #endif 107 1.1 riastrad return tmp; 108 1.1 riastrad } 109 1.1 riastrad 110 1.1 riastrad /** 111 1.1 riastrad * \c fault method for AGP virtual memory. 112 1.1 riastrad * 113 1.1 riastrad * \param vma virtual memory area. 114 1.1 riastrad * \param address access address. 115 1.1 riastrad * \return pointer to the page structure. 116 1.1 riastrad * 117 1.1 riastrad * Find the right map and if it's AGP memory find the real physical page to 118 1.1 riastrad * map, get the page, increment the use count and return it. 119 1.1 riastrad */ 120 1.2 riastrad #if IS_ENABLED(CONFIG_AGP) 121 1.3 riastrad static vm_fault_t drm_vm_fault(struct vm_fault *vmf) 122 1.1 riastrad { 123 1.3 riastrad struct vm_area_struct *vma = vmf->vma; 124 1.1 riastrad struct drm_file *priv = vma->vm_file->private_data; 125 1.1 riastrad struct drm_device *dev = priv->minor->dev; 126 1.1 riastrad struct drm_local_map *map = NULL; 127 1.1 riastrad struct drm_map_list *r_list; 128 1.1 riastrad struct drm_hash_item *hash; 129 1.1 riastrad 130 1.1 riastrad /* 131 1.1 riastrad * Find the right map 132 1.1 riastrad */ 133 1.2 riastrad if (!dev->agp) 134 1.1 riastrad goto vm_fault_error; 135 1.1 riastrad 136 1.1 riastrad if (!dev->agp || !dev->agp->cant_use_aperture) 137 1.1 riastrad goto vm_fault_error; 138 1.1 riastrad 139 1.1 riastrad if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash)) 140 1.1 riastrad goto vm_fault_error; 141 1.1 riastrad 142 1.1 riastrad r_list = drm_hash_entry(hash, struct drm_map_list, hash); 143 1.1 riastrad map = r_list->map; 144 1.1 riastrad 145 1.1 riastrad if (map && map->type == _DRM_AGP) { 146 1.1 riastrad /* 147 1.1 riastrad * Using vm_pgoff as a selector forces us to use this unusual 148 1.1 riastrad * addressing scheme. 149 1.1 riastrad */ 150 1.3 riastrad resource_size_t offset = vmf->address - vma->vm_start; 151 1.1 riastrad resource_size_t baddr = map->offset + offset; 152 1.1 riastrad struct drm_agp_mem *agpmem; 153 1.1 riastrad struct page *page; 154 1.1 riastrad 155 1.1 riastrad #ifdef __alpha__ 156 1.1 riastrad /* 157 1.1 riastrad * Adjust to a bus-relative address 158 1.1 riastrad */ 159 1.1 riastrad baddr -= dev->hose->mem_space->start; 160 1.1 riastrad #endif 161 1.1 riastrad 162 1.1 riastrad /* 163 1.1 riastrad * It's AGP memory - find the real physical page to map 164 1.1 riastrad */ 165 1.1 riastrad list_for_each_entry(agpmem, &dev->agp->memory, head) { 166 1.1 riastrad if (agpmem->bound <= baddr && 167 1.1 riastrad agpmem->bound + agpmem->pages * PAGE_SIZE > baddr) 168 1.1 riastrad break; 169 1.1 riastrad } 170 1.1 riastrad 171 1.1 riastrad if (&agpmem->head == &dev->agp->memory) 172 1.1 riastrad goto vm_fault_error; 173 1.1 riastrad 174 1.1 riastrad /* 175 1.1 riastrad * Get the page, inc the use count, and return it 176 1.1 riastrad */ 177 1.1 riastrad offset = (baddr - agpmem->bound) >> PAGE_SHIFT; 178 1.1 riastrad page = agpmem->memory->pages[offset]; 179 1.1 riastrad get_page(page); 180 1.1 riastrad vmf->page = page; 181 1.1 riastrad 182 1.1 riastrad DRM_DEBUG 183 1.1 riastrad ("baddr = 0x%llx page = 0x%p, offset = 0x%llx, count=%d\n", 184 1.1 riastrad (unsigned long long)baddr, 185 1.1 riastrad agpmem->memory->pages[offset], 186 1.1 riastrad (unsigned long long)offset, 187 1.1 riastrad page_count(page)); 188 1.1 riastrad return 0; 189 1.1 riastrad } 190 1.1 riastrad vm_fault_error: 191 1.1 riastrad return VM_FAULT_SIGBUS; /* Disallow mremap */ 192 1.1 riastrad } 193 1.2 riastrad #else 194 1.3 riastrad static vm_fault_t drm_vm_fault(struct vm_fault *vmf) 195 1.1 riastrad { 196 1.1 riastrad return VM_FAULT_SIGBUS; 197 1.1 riastrad } 198 1.2 riastrad #endif 199 1.1 riastrad 200 1.1 riastrad /** 201 1.1 riastrad * \c nopage method for shared virtual memory. 202 1.1 riastrad * 203 1.1 riastrad * \param vma virtual memory area. 204 1.1 riastrad * \param address access address. 205 1.1 riastrad * \return pointer to the page structure. 206 1.1 riastrad * 207 1.1 riastrad * Get the mapping, find the real physical page to map, get the page, and 208 1.1 riastrad * return it. 209 1.1 riastrad */ 210 1.3 riastrad static vm_fault_t drm_vm_shm_fault(struct vm_fault *vmf) 211 1.1 riastrad { 212 1.3 riastrad struct vm_area_struct *vma = vmf->vma; 213 1.1 riastrad struct drm_local_map *map = vma->vm_private_data; 214 1.1 riastrad unsigned long offset; 215 1.1 riastrad unsigned long i; 216 1.1 riastrad struct page *page; 217 1.1 riastrad 218 1.1 riastrad if (!map) 219 1.1 riastrad return VM_FAULT_SIGBUS; /* Nothing allocated */ 220 1.1 riastrad 221 1.3 riastrad offset = vmf->address - vma->vm_start; 222 1.1 riastrad i = (unsigned long)map->handle + offset; 223 1.1 riastrad page = vmalloc_to_page((void *)i); 224 1.1 riastrad if (!page) 225 1.1 riastrad return VM_FAULT_SIGBUS; 226 1.1 riastrad get_page(page); 227 1.1 riastrad vmf->page = page; 228 1.1 riastrad 229 1.1 riastrad DRM_DEBUG("shm_fault 0x%lx\n", offset); 230 1.1 riastrad return 0; 231 1.1 riastrad } 232 1.1 riastrad 233 1.1 riastrad /** 234 1.1 riastrad * \c close method for shared virtual memory. 235 1.1 riastrad * 236 1.1 riastrad * \param vma virtual memory area. 237 1.1 riastrad * 238 1.1 riastrad * Deletes map information if we are the last 239 1.1 riastrad * person to close a mapping and it's not in the global maplist. 240 1.1 riastrad */ 241 1.1 riastrad static void drm_vm_shm_close(struct vm_area_struct *vma) 242 1.1 riastrad { 243 1.1 riastrad struct drm_file *priv = vma->vm_file->private_data; 244 1.1 riastrad struct drm_device *dev = priv->minor->dev; 245 1.1 riastrad struct drm_vma_entry *pt, *temp; 246 1.1 riastrad struct drm_local_map *map; 247 1.1 riastrad struct drm_map_list *r_list; 248 1.1 riastrad int found_maps = 0; 249 1.1 riastrad 250 1.1 riastrad DRM_DEBUG("0x%08lx,0x%08lx\n", 251 1.1 riastrad vma->vm_start, vma->vm_end - vma->vm_start); 252 1.1 riastrad 253 1.1 riastrad map = vma->vm_private_data; 254 1.1 riastrad 255 1.1 riastrad mutex_lock(&dev->struct_mutex); 256 1.1 riastrad list_for_each_entry_safe(pt, temp, &dev->vmalist, head) { 257 1.1 riastrad if (pt->vma->vm_private_data == map) 258 1.1 riastrad found_maps++; 259 1.1 riastrad if (pt->vma == vma) { 260 1.1 riastrad list_del(&pt->head); 261 1.1 riastrad kfree(pt); 262 1.1 riastrad } 263 1.1 riastrad } 264 1.1 riastrad 265 1.1 riastrad /* We were the only map that was found */ 266 1.1 riastrad if (found_maps == 1 && map->flags & _DRM_REMOVABLE) { 267 1.1 riastrad /* Check to see if we are in the maplist, if we are not, then 268 1.1 riastrad * we delete this mappings information. 269 1.1 riastrad */ 270 1.1 riastrad found_maps = 0; 271 1.1 riastrad list_for_each_entry(r_list, &dev->maplist, head) { 272 1.1 riastrad if (r_list->map == map) 273 1.1 riastrad found_maps++; 274 1.1 riastrad } 275 1.1 riastrad 276 1.1 riastrad if (!found_maps) { 277 1.1 riastrad drm_dma_handle_t dmah; 278 1.1 riastrad 279 1.1 riastrad switch (map->type) { 280 1.1 riastrad case _DRM_REGISTERS: 281 1.1 riastrad case _DRM_FRAME_BUFFER: 282 1.2 riastrad arch_phys_wc_del(map->mtrr); 283 1.1 riastrad iounmap(map->handle); 284 1.1 riastrad break; 285 1.1 riastrad case _DRM_SHM: 286 1.1 riastrad vfree(map->handle); 287 1.1 riastrad break; 288 1.1 riastrad case _DRM_AGP: 289 1.1 riastrad case _DRM_SCATTER_GATHER: 290 1.1 riastrad break; 291 1.1 riastrad case _DRM_CONSISTENT: 292 1.1 riastrad dmah.vaddr = map->handle; 293 1.1 riastrad dmah.busaddr = map->offset; 294 1.1 riastrad dmah.size = map->size; 295 1.2 riastrad __drm_legacy_pci_free(dev, &dmah); 296 1.1 riastrad break; 297 1.1 riastrad } 298 1.1 riastrad kfree(map); 299 1.1 riastrad } 300 1.1 riastrad } 301 1.1 riastrad mutex_unlock(&dev->struct_mutex); 302 1.1 riastrad } 303 1.1 riastrad 304 1.1 riastrad /** 305 1.1 riastrad * \c fault method for DMA virtual memory. 306 1.1 riastrad * 307 1.1 riastrad * \param address access address. 308 1.1 riastrad * \return pointer to the page structure. 309 1.1 riastrad * 310 1.1 riastrad * Determine the page number from the page offset and get it from drm_device_dma::pagelist. 311 1.1 riastrad */ 312 1.3 riastrad static vm_fault_t drm_vm_dma_fault(struct vm_fault *vmf) 313 1.1 riastrad { 314 1.3 riastrad struct vm_area_struct *vma = vmf->vma; 315 1.1 riastrad struct drm_file *priv = vma->vm_file->private_data; 316 1.1 riastrad struct drm_device *dev = priv->minor->dev; 317 1.1 riastrad struct drm_device_dma *dma = dev->dma; 318 1.1 riastrad unsigned long offset; 319 1.1 riastrad unsigned long page_nr; 320 1.1 riastrad struct page *page; 321 1.1 riastrad 322 1.1 riastrad if (!dma) 323 1.1 riastrad return VM_FAULT_SIGBUS; /* Error */ 324 1.1 riastrad if (!dma->pagelist) 325 1.1 riastrad return VM_FAULT_SIGBUS; /* Nothing allocated */ 326 1.1 riastrad 327 1.3 riastrad offset = vmf->address - vma->vm_start; 328 1.3 riastrad /* vm_[pg]off[set] should be 0 */ 329 1.1 riastrad page_nr = offset >> PAGE_SHIFT; /* page_nr could just be vmf->pgoff */ 330 1.2 riastrad page = virt_to_page((void *)dma->pagelist[page_nr]); 331 1.1 riastrad 332 1.1 riastrad get_page(page); 333 1.1 riastrad vmf->page = page; 334 1.1 riastrad 335 1.1 riastrad DRM_DEBUG("dma_fault 0x%lx (page %lu)\n", offset, page_nr); 336 1.1 riastrad return 0; 337 1.1 riastrad } 338 1.1 riastrad 339 1.1 riastrad /** 340 1.1 riastrad * \c fault method for scatter-gather virtual memory. 341 1.1 riastrad * 342 1.1 riastrad * \param address access address. 343 1.1 riastrad * \return pointer to the page structure. 344 1.1 riastrad * 345 1.1 riastrad * Determine the map offset from the page offset and get it from drm_sg_mem::pagelist. 346 1.1 riastrad */ 347 1.3 riastrad static vm_fault_t drm_vm_sg_fault(struct vm_fault *vmf) 348 1.1 riastrad { 349 1.3 riastrad struct vm_area_struct *vma = vmf->vma; 350 1.1 riastrad struct drm_local_map *map = vma->vm_private_data; 351 1.1 riastrad struct drm_file *priv = vma->vm_file->private_data; 352 1.1 riastrad struct drm_device *dev = priv->minor->dev; 353 1.1 riastrad struct drm_sg_mem *entry = dev->sg; 354 1.1 riastrad unsigned long offset; 355 1.1 riastrad unsigned long map_offset; 356 1.1 riastrad unsigned long page_offset; 357 1.1 riastrad struct page *page; 358 1.1 riastrad 359 1.1 riastrad if (!entry) 360 1.1 riastrad return VM_FAULT_SIGBUS; /* Error */ 361 1.1 riastrad if (!entry->pagelist) 362 1.1 riastrad return VM_FAULT_SIGBUS; /* Nothing allocated */ 363 1.1 riastrad 364 1.3 riastrad offset = vmf->address - vma->vm_start; 365 1.1 riastrad map_offset = map->offset - (unsigned long)dev->sg->virtual; 366 1.1 riastrad page_offset = (offset >> PAGE_SHIFT) + (map_offset >> PAGE_SHIFT); 367 1.1 riastrad page = entry->pagelist[page_offset]; 368 1.1 riastrad get_page(page); 369 1.1 riastrad vmf->page = page; 370 1.1 riastrad 371 1.1 riastrad return 0; 372 1.1 riastrad } 373 1.1 riastrad 374 1.1 riastrad /** AGP virtual memory operations */ 375 1.1 riastrad static const struct vm_operations_struct drm_vm_ops = { 376 1.1 riastrad .fault = drm_vm_fault, 377 1.1 riastrad .open = drm_vm_open, 378 1.1 riastrad .close = drm_vm_close, 379 1.1 riastrad }; 380 1.1 riastrad 381 1.1 riastrad /** Shared virtual memory operations */ 382 1.1 riastrad static const struct vm_operations_struct drm_vm_shm_ops = { 383 1.1 riastrad .fault = drm_vm_shm_fault, 384 1.1 riastrad .open = drm_vm_open, 385 1.1 riastrad .close = drm_vm_shm_close, 386 1.1 riastrad }; 387 1.1 riastrad 388 1.1 riastrad /** DMA virtual memory operations */ 389 1.1 riastrad static const struct vm_operations_struct drm_vm_dma_ops = { 390 1.1 riastrad .fault = drm_vm_dma_fault, 391 1.1 riastrad .open = drm_vm_open, 392 1.1 riastrad .close = drm_vm_close, 393 1.1 riastrad }; 394 1.1 riastrad 395 1.1 riastrad /** Scatter-gather virtual memory operations */ 396 1.1 riastrad static const struct vm_operations_struct drm_vm_sg_ops = { 397 1.1 riastrad .fault = drm_vm_sg_fault, 398 1.1 riastrad .open = drm_vm_open, 399 1.1 riastrad .close = drm_vm_close, 400 1.1 riastrad }; 401 1.1 riastrad 402 1.3 riastrad static void drm_vm_open_locked(struct drm_device *dev, 403 1.3 riastrad struct vm_area_struct *vma) 404 1.1 riastrad { 405 1.1 riastrad struct drm_vma_entry *vma_entry; 406 1.1 riastrad 407 1.1 riastrad DRM_DEBUG("0x%08lx,0x%08lx\n", 408 1.1 riastrad vma->vm_start, vma->vm_end - vma->vm_start); 409 1.1 riastrad 410 1.1 riastrad vma_entry = kmalloc(sizeof(*vma_entry), GFP_KERNEL); 411 1.1 riastrad if (vma_entry) { 412 1.1 riastrad vma_entry->vma = vma; 413 1.1 riastrad vma_entry->pid = current->pid; 414 1.1 riastrad list_add(&vma_entry->head, &dev->vmalist); 415 1.1 riastrad } 416 1.1 riastrad } 417 1.1 riastrad 418 1.1 riastrad static void drm_vm_open(struct vm_area_struct *vma) 419 1.1 riastrad { 420 1.1 riastrad struct drm_file *priv = vma->vm_file->private_data; 421 1.1 riastrad struct drm_device *dev = priv->minor->dev; 422 1.1 riastrad 423 1.1 riastrad mutex_lock(&dev->struct_mutex); 424 1.1 riastrad drm_vm_open_locked(dev, vma); 425 1.1 riastrad mutex_unlock(&dev->struct_mutex); 426 1.1 riastrad } 427 1.1 riastrad 428 1.3 riastrad static void drm_vm_close_locked(struct drm_device *dev, 429 1.3 riastrad struct vm_area_struct *vma) 430 1.1 riastrad { 431 1.1 riastrad struct drm_vma_entry *pt, *temp; 432 1.1 riastrad 433 1.1 riastrad DRM_DEBUG("0x%08lx,0x%08lx\n", 434 1.1 riastrad vma->vm_start, vma->vm_end - vma->vm_start); 435 1.1 riastrad 436 1.1 riastrad list_for_each_entry_safe(pt, temp, &dev->vmalist, head) { 437 1.1 riastrad if (pt->vma == vma) { 438 1.1 riastrad list_del(&pt->head); 439 1.1 riastrad kfree(pt); 440 1.1 riastrad break; 441 1.1 riastrad } 442 1.1 riastrad } 443 1.1 riastrad } 444 1.1 riastrad 445 1.1 riastrad /** 446 1.1 riastrad * \c close method for all virtual memory types. 447 1.1 riastrad * 448 1.1 riastrad * \param vma virtual memory area. 449 1.1 riastrad * 450 1.1 riastrad * Search the \p vma private data entry in drm_device::vmalist, unlink it, and 451 1.1 riastrad * free it. 452 1.1 riastrad */ 453 1.1 riastrad static void drm_vm_close(struct vm_area_struct *vma) 454 1.1 riastrad { 455 1.1 riastrad struct drm_file *priv = vma->vm_file->private_data; 456 1.1 riastrad struct drm_device *dev = priv->minor->dev; 457 1.1 riastrad 458 1.1 riastrad mutex_lock(&dev->struct_mutex); 459 1.1 riastrad drm_vm_close_locked(dev, vma); 460 1.1 riastrad mutex_unlock(&dev->struct_mutex); 461 1.1 riastrad } 462 1.1 riastrad 463 1.1 riastrad /** 464 1.1 riastrad * mmap DMA memory. 465 1.1 riastrad * 466 1.1 riastrad * \param file_priv DRM file private. 467 1.1 riastrad * \param vma virtual memory area. 468 1.1 riastrad * \return zero on success or a negative number on failure. 469 1.1 riastrad * 470 1.1 riastrad * Sets the virtual memory area operations structure to vm_dma_ops, the file 471 1.1 riastrad * pointer, and calls vm_open(). 472 1.1 riastrad */ 473 1.1 riastrad static int drm_mmap_dma(struct file *filp, struct vm_area_struct *vma) 474 1.1 riastrad { 475 1.1 riastrad struct drm_file *priv = filp->private_data; 476 1.1 riastrad struct drm_device *dev; 477 1.1 riastrad struct drm_device_dma *dma; 478 1.1 riastrad unsigned long length = vma->vm_end - vma->vm_start; 479 1.1 riastrad 480 1.1 riastrad dev = priv->minor->dev; 481 1.1 riastrad dma = dev->dma; 482 1.1 riastrad DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n", 483 1.1 riastrad vma->vm_start, vma->vm_end, vma->vm_pgoff); 484 1.1 riastrad 485 1.1 riastrad /* Length must match exact page count */ 486 1.1 riastrad if (!dma || (length >> PAGE_SHIFT) != dma->page_count) { 487 1.1 riastrad return -EINVAL; 488 1.1 riastrad } 489 1.1 riastrad 490 1.1 riastrad if (!capable(CAP_SYS_ADMIN) && 491 1.1 riastrad (dma->flags & _DRM_DMA_USE_PCI_RO)) { 492 1.1 riastrad vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE); 493 1.1 riastrad #if defined(__i386__) || defined(__x86_64__) 494 1.1 riastrad pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW; 495 1.1 riastrad #else 496 1.1 riastrad /* Ye gads this is ugly. With more thought 497 1.1 riastrad we could move this up higher and use 498 1.1 riastrad `protection_map' instead. */ 499 1.1 riastrad vma->vm_page_prot = 500 1.1 riastrad __pgprot(pte_val 501 1.1 riastrad (pte_wrprotect 502 1.1 riastrad (__pte(pgprot_val(vma->vm_page_prot))))); 503 1.1 riastrad #endif 504 1.1 riastrad } 505 1.1 riastrad 506 1.1 riastrad vma->vm_ops = &drm_vm_dma_ops; 507 1.1 riastrad 508 1.1 riastrad vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 509 1.1 riastrad 510 1.1 riastrad drm_vm_open_locked(dev, vma); 511 1.1 riastrad return 0; 512 1.1 riastrad } 513 1.1 riastrad 514 1.1 riastrad static resource_size_t drm_core_get_reg_ofs(struct drm_device *dev) 515 1.1 riastrad { 516 1.1 riastrad #ifdef __alpha__ 517 1.1 riastrad return dev->hose->dense_mem_base; 518 1.1 riastrad #else 519 1.1 riastrad return 0; 520 1.1 riastrad #endif 521 1.1 riastrad } 522 1.1 riastrad 523 1.1 riastrad /** 524 1.1 riastrad * mmap DMA memory. 525 1.1 riastrad * 526 1.1 riastrad * \param file_priv DRM file private. 527 1.1 riastrad * \param vma virtual memory area. 528 1.1 riastrad * \return zero on success or a negative number on failure. 529 1.1 riastrad * 530 1.1 riastrad * If the virtual memory area has no offset associated with it then it's a DMA 531 1.1 riastrad * area, so calls mmap_dma(). Otherwise searches the map in drm_device::maplist, 532 1.1 riastrad * checks that the restricted flag is not set, sets the virtual memory operations 533 1.1 riastrad * according to the mapping type and remaps the pages. Finally sets the file 534 1.1 riastrad * pointer and calls vm_open(). 535 1.1 riastrad */ 536 1.2 riastrad static int drm_mmap_locked(struct file *filp, struct vm_area_struct *vma) 537 1.1 riastrad { 538 1.1 riastrad struct drm_file *priv = filp->private_data; 539 1.1 riastrad struct drm_device *dev = priv->minor->dev; 540 1.1 riastrad struct drm_local_map *map = NULL; 541 1.1 riastrad resource_size_t offset = 0; 542 1.1 riastrad struct drm_hash_item *hash; 543 1.1 riastrad 544 1.1 riastrad DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n", 545 1.1 riastrad vma->vm_start, vma->vm_end, vma->vm_pgoff); 546 1.1 riastrad 547 1.1 riastrad if (!priv->authenticated) 548 1.1 riastrad return -EACCES; 549 1.1 riastrad 550 1.1 riastrad /* We check for "dma". On Apple's UniNorth, it's valid to have 551 1.1 riastrad * the AGP mapped at physical address 0 552 1.1 riastrad * --BenH. 553 1.1 riastrad */ 554 1.1 riastrad if (!vma->vm_pgoff 555 1.2 riastrad #if IS_ENABLED(CONFIG_AGP) 556 1.1 riastrad && (!dev->agp 557 1.1 riastrad || dev->agp->agp_info.device->vendor != PCI_VENDOR_ID_APPLE) 558 1.1 riastrad #endif 559 1.1 riastrad ) 560 1.1 riastrad return drm_mmap_dma(filp, vma); 561 1.1 riastrad 562 1.1 riastrad if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash)) { 563 1.1 riastrad DRM_ERROR("Could not find map\n"); 564 1.1 riastrad return -EINVAL; 565 1.1 riastrad } 566 1.1 riastrad 567 1.1 riastrad map = drm_hash_entry(hash, struct drm_map_list, hash)->map; 568 1.1 riastrad if (!map || ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) 569 1.1 riastrad return -EPERM; 570 1.1 riastrad 571 1.1 riastrad /* Check for valid size. */ 572 1.1 riastrad if (map->size < vma->vm_end - vma->vm_start) 573 1.1 riastrad return -EINVAL; 574 1.1 riastrad 575 1.1 riastrad if (!capable(CAP_SYS_ADMIN) && (map->flags & _DRM_READ_ONLY)) { 576 1.1 riastrad vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE); 577 1.1 riastrad #if defined(__i386__) || defined(__x86_64__) 578 1.1 riastrad pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW; 579 1.1 riastrad #else 580 1.1 riastrad /* Ye gads this is ugly. With more thought 581 1.1 riastrad we could move this up higher and use 582 1.1 riastrad `protection_map' instead. */ 583 1.1 riastrad vma->vm_page_prot = 584 1.1 riastrad __pgprot(pte_val 585 1.1 riastrad (pte_wrprotect 586 1.1 riastrad (__pte(pgprot_val(vma->vm_page_prot))))); 587 1.1 riastrad #endif 588 1.1 riastrad } 589 1.1 riastrad 590 1.1 riastrad switch (map->type) { 591 1.1 riastrad #if !defined(__arm__) 592 1.1 riastrad case _DRM_AGP: 593 1.2 riastrad if (dev->agp && dev->agp->cant_use_aperture) { 594 1.1 riastrad /* 595 1.1 riastrad * On some platforms we can't talk to bus dma address from the CPU, so for 596 1.1 riastrad * memory of type DRM_AGP, we'll deal with sorting out the real physical 597 1.1 riastrad * pages and mappings in fault() 598 1.1 riastrad */ 599 1.1 riastrad #if defined(__powerpc__) 600 1.3 riastrad vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 601 1.1 riastrad #endif 602 1.1 riastrad vma->vm_ops = &drm_vm_ops; 603 1.1 riastrad break; 604 1.1 riastrad } 605 1.1 riastrad #endif 606 1.3 riastrad /* fall through - to _DRM_FRAME_BUFFER... */ 607 1.1 riastrad case _DRM_FRAME_BUFFER: 608 1.1 riastrad case _DRM_REGISTERS: 609 1.1 riastrad offset = drm_core_get_reg_ofs(dev); 610 1.2 riastrad vma->vm_page_prot = drm_io_prot(map, vma); 611 1.1 riastrad if (io_remap_pfn_range(vma, vma->vm_start, 612 1.1 riastrad (map->offset + offset) >> PAGE_SHIFT, 613 1.1 riastrad vma->vm_end - vma->vm_start, 614 1.1 riastrad vma->vm_page_prot)) 615 1.1 riastrad return -EAGAIN; 616 1.1 riastrad DRM_DEBUG(" Type = %d; start = 0x%lx, end = 0x%lx," 617 1.1 riastrad " offset = 0x%llx\n", 618 1.1 riastrad map->type, 619 1.1 riastrad vma->vm_start, vma->vm_end, (unsigned long long)(map->offset + offset)); 620 1.1 riastrad 621 1.1 riastrad vma->vm_ops = &drm_vm_ops; 622 1.1 riastrad break; 623 1.1 riastrad case _DRM_CONSISTENT: 624 1.1 riastrad /* Consistent memory is really like shared memory. But 625 1.1 riastrad * it's allocated in a different way, so avoid fault */ 626 1.1 riastrad if (remap_pfn_range(vma, vma->vm_start, 627 1.1 riastrad page_to_pfn(virt_to_page(map->handle)), 628 1.1 riastrad vma->vm_end - vma->vm_start, vma->vm_page_prot)) 629 1.1 riastrad return -EAGAIN; 630 1.1 riastrad vma->vm_page_prot = drm_dma_prot(map->type, vma); 631 1.3 riastrad /* fall through - to _DRM_SHM */ 632 1.1 riastrad case _DRM_SHM: 633 1.1 riastrad vma->vm_ops = &drm_vm_shm_ops; 634 1.1 riastrad vma->vm_private_data = (void *)map; 635 1.1 riastrad break; 636 1.1 riastrad case _DRM_SCATTER_GATHER: 637 1.1 riastrad vma->vm_ops = &drm_vm_sg_ops; 638 1.1 riastrad vma->vm_private_data = (void *)map; 639 1.1 riastrad vma->vm_page_prot = drm_dma_prot(map->type, vma); 640 1.1 riastrad break; 641 1.1 riastrad default: 642 1.1 riastrad return -EINVAL; /* This should never happen. */ 643 1.1 riastrad } 644 1.1 riastrad vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 645 1.1 riastrad 646 1.1 riastrad drm_vm_open_locked(dev, vma); 647 1.1 riastrad return 0; 648 1.1 riastrad } 649 1.1 riastrad 650 1.2 riastrad int drm_legacy_mmap(struct file *filp, struct vm_area_struct *vma) 651 1.1 riastrad { 652 1.1 riastrad struct drm_file *priv = filp->private_data; 653 1.1 riastrad struct drm_device *dev = priv->minor->dev; 654 1.1 riastrad int ret; 655 1.1 riastrad 656 1.3 riastrad if (drm_dev_is_unplugged(dev)) 657 1.1 riastrad return -ENODEV; 658 1.1 riastrad 659 1.1 riastrad mutex_lock(&dev->struct_mutex); 660 1.1 riastrad ret = drm_mmap_locked(filp, vma); 661 1.1 riastrad mutex_unlock(&dev->struct_mutex); 662 1.1 riastrad 663 1.1 riastrad return ret; 664 1.1 riastrad } 665 1.2 riastrad EXPORT_SYMBOL(drm_legacy_mmap); 666 1.2 riastrad 667 1.3 riastrad #if IS_ENABLED(CONFIG_DRM_LEGACY) 668 1.2 riastrad void drm_legacy_vma_flush(struct drm_device *dev) 669 1.2 riastrad { 670 1.2 riastrad struct drm_vma_entry *vma, *vma_temp; 671 1.2 riastrad 672 1.2 riastrad /* Clear vma list (only needed for legacy drivers) */ 673 1.2 riastrad list_for_each_entry_safe(vma, vma_temp, &dev->vmalist, head) { 674 1.2 riastrad list_del(&vma->head); 675 1.2 riastrad kfree(vma); 676 1.2 riastrad } 677 1.2 riastrad } 678 1.2 riastrad #endif 679