blob: 1a636963378947cc247ee46c6573ecf978362060 [file] [log] [blame]
Qian Cai45b2fda2019-07-15 09:42:53 -04001/*
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002 * \file drm_vm.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Memory mapping for DRM
Dave Airlieb5e89ed2005-09-25 14:28:13 +10004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
7 */
8
9/*
10 * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
11 *
12 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
22 *
23 * The above copyright notice and this permission notice (including the next
24 * paragraph) shall be included in all copies or substantial portions of the
25 * Software.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 * OTHER DEALINGS IN THE SOFTWARE.
34 */
35
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040036#include <linux/export.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020037#include <linux/pci.h>
David Herrmann03decbe2014-08-29 12:12:29 +020038#include <linux/seq_file.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020039#include <linux/vmalloc.h>
Mike Rapoport65fddcf2020-06-08 21:32:42 -070040#include <linux/pgtable.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#if defined(__ia64__)
43#include <linux/efi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#endif
Tom Lendacky95cf9262017-07-17 16:10:26 -050046#include <linux/mem_encrypt.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020047
Sam Ravnborg0500c042019-05-26 19:35:35 +020048
49#include <drm/drm_agpsupport.h>
50#include <drm/drm_device.h>
51#include <drm/drm_drv.h>
52#include <drm/drm_file.h>
53#include <drm/drm_framebuffer.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020054#include <drm/drm_print.h>
55
Ville Syrjälä43fc8842015-03-13 14:51:25 +020056#include "drm_internal.h"
David Herrmanncc5ea592014-08-29 12:12:32 +020057#include "drm_legacy.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
David Herrmann03decbe2014-08-29 12:12:29 +020059struct drm_vma_entry {
60 struct list_head head;
61 struct vm_area_struct *vma;
62 pid_t pid;
63};
64
Dave Airliec94f7022005-07-07 21:03:38 +100065static void drm_vm_open(struct vm_area_struct *vma);
66static void drm_vm_close(struct vm_area_struct *vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Andy Lutomirskiff47eaf2013-05-13 23:58:42 +000068static pgprot_t drm_io_prot(struct drm_local_map *map,
69 struct vm_area_struct *vma)
Dave Airlie5cc7f9a2007-02-10 11:53:13 +110070{
71 pgprot_t tmp = vm_get_page_prot(vma->vm_flags);
72
Tom Lendacky95cf9262017-07-17 16:10:26 -050073 /* We don't want graphics memory to be mapped encrypted */
74 tmp = pgprot_decrypted(tmp);
75
Serge Semin8a08e502019-04-23 15:31:22 +030076#if defined(__i386__) || defined(__x86_64__) || defined(__powerpc__) || \
77 defined(__mips__)
Andy Lutomirskif4350462013-05-13 23:58:43 +000078 if (map->type == _DRM_REGISTERS && !(map->flags & _DRM_WRITE_COMBINING))
79 tmp = pgprot_noncached(tmp);
80 else
81 tmp = pgprot_writecombine(tmp);
Benjamin Herrenschmidt6876b3b2008-03-28 14:23:07 -070082#elif defined(__ia64__)
Dave Airlie5cc7f9a2007-02-10 11:53:13 +110083 if (efi_range_is_wc(vma->vm_start, vma->vm_end -
84 vma->vm_start))
85 tmp = pgprot_writecombine(tmp);
86 else
87 tmp = pgprot_noncached(tmp);
Serge Semin8a08e502019-04-23 15:31:22 +030088#elif defined(__sparc__) || defined(__arm__)
Benjamin Herrenschmidt6876b3b2008-03-28 14:23:07 -070089 tmp = pgprot_noncached(tmp);
90#endif
91 return tmp;
92}
93
94static pgprot_t drm_dma_prot(uint32_t map_type, struct vm_area_struct *vma)
95{
96 pgprot_t tmp = vm_get_page_prot(vma->vm_flags);
97
98#if defined(__powerpc__) && defined(CONFIG_NOT_COHERENT_CACHE)
Benjamin Herrenschmidt37035e72016-06-27 19:00:23 +100099 tmp = pgprot_noncached_wc(tmp);
Dave Airlie5cc7f9a2007-02-10 11:53:13 +1100100#endif
101 return tmp;
102}
103
Benjamin Gaignard40e5f352020-03-06 11:29:34 +0100104/*
Nick Pigginca0b07d2008-02-07 16:20:50 +1000105 * \c fault method for AGP virtual memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 *
107 * \param vma virtual memory area.
108 * \param address access address.
109 * \return pointer to the page structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000110 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * Find the right map and if it's AGP memory find the real physical page to
112 * map, get the page, increment the use count and return it.
113 */
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200114#if IS_ENABLED(CONFIG_AGP)
Souptick Joarder6c259092018-05-10 19:12:04 +0530115static vm_fault_t drm_vm_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Dave Jiang11bac802017-02-24 14:56:41 -0800117 struct vm_area_struct *vma = vmf->vma;
Dave Airlie84b1fd12007-07-11 15:53:27 +1000118 struct drm_file *priv = vma->vm_file->private_data;
Dave Airlie2c14f282008-04-21 16:47:32 +1000119 struct drm_device *dev = priv->minor->dev;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100120 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000121 struct drm_map_list *r_list;
Dave Airliee0be4282007-07-12 10:26:44 +1000122 struct drm_hash_item *hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 /*
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000125 * Find the right map
126 */
Daniel Vetterd9906752013-12-11 11:34:35 +0100127 if (!dev->agp)
Nick Pigginca0b07d2008-02-07 16:20:50 +1000128 goto vm_fault_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000130 if (!dev->agp || !dev->agp->cant_use_aperture)
Nick Pigginca0b07d2008-02-07 16:20:50 +1000131 goto vm_fault_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Thomas Hellstrom15450852007-02-08 16:14:05 +1100133 if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash))
Nick Pigginca0b07d2008-02-07 16:20:50 +1000134 goto vm_fault_error;
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000135
Dave Airlie55910512007-07-11 16:53:40 +1000136 r_list = drm_hash_entry(hash, struct drm_map_list, hash);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000137 map = r_list->map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 if (map && map->type == _DRM_AGP) {
Nick Pigginca0b07d2008-02-07 16:20:50 +1000140 /*
141 * Using vm_pgoff as a selector forces us to use this unusual
142 * addressing scheme.
143 */
Jan Kara1a29d852016-12-14 15:07:01 -0800144 resource_size_t offset = vmf->address - vma->vm_start;
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100145 resource_size_t baddr = map->offset + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct drm_agp_mem *agpmem;
147 struct page *page;
148
149#ifdef __alpha__
150 /*
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000151 * Adjust to a bus-relative address
152 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 baddr -= dev->hose->mem_space->start;
154#endif
155
156 /*
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000157 * It's AGP memory - find the real physical page to map
158 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000159 list_for_each_entry(agpmem, &dev->agp->memory, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (agpmem->bound <= baddr &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000161 agpmem->bound + agpmem->pages * PAGE_SIZE > baddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 break;
163 }
164
Dan Carpenter161c4812010-08-19 11:39:57 +0200165 if (&agpmem->head == &dev->agp->memory)
Nick Pigginca0b07d2008-02-07 16:20:50 +1000166 goto vm_fault_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 /*
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000169 * Get the page, inc the use count, and return it
170 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 offset = (baddr - agpmem->bound) >> PAGE_SHIFT;
Dave Airlie07613ba2009-06-12 14:11:41 +1000172 page = agpmem->memory->pages[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 get_page(page);
Nick Pigginca0b07d2008-02-07 16:20:50 +1000174 vmf->page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000176 DRM_DEBUG
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100177 ("baddr = 0x%llx page = 0x%p, offset = 0x%llx, count=%d\n",
178 (unsigned long long)baddr,
Dave Airlie07613ba2009-06-12 14:11:41 +1000179 agpmem->memory->pages[offset],
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100180 (unsigned long long)offset,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000181 page_count(page));
Nick Pigginca0b07d2008-02-07 16:20:50 +1000182 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000183 }
Nick Pigginca0b07d2008-02-07 16:20:50 +1000184vm_fault_error:
185 return VM_FAULT_SIGBUS; /* Disallow mremap */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200187#else
Souptick Joarder6c259092018-05-10 19:12:04 +0530188static vm_fault_t drm_vm_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Nick Pigginca0b07d2008-02-07 16:20:50 +1000190 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200192#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Benjamin Gaignard40e5f352020-03-06 11:29:34 +0100194/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 * \c nopage method for shared virtual memory.
196 *
197 * \param vma virtual memory area.
198 * \param address access address.
199 * \return pointer to the page structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000200 *
Michael Opdenacker59c51592007-05-09 08:57:56 +0200201 * Get the mapping, find the real physical page to map, get the page, and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 * return it.
203 */
Souptick Joarder6c259092018-05-10 19:12:04 +0530204static vm_fault_t drm_vm_shm_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Dave Jiang11bac802017-02-24 14:56:41 -0800206 struct vm_area_struct *vma = vmf->vma;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100207 struct drm_local_map *map = vma->vm_private_data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000208 unsigned long offset;
209 unsigned long i;
210 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000212 if (!map)
Nick Pigginca0b07d2008-02-07 16:20:50 +1000213 return VM_FAULT_SIGBUS; /* Nothing allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Jan Kara1a29d852016-12-14 15:07:01 -0800215 offset = vmf->address - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 i = (unsigned long)map->handle + offset;
Hugh Dickins38315872007-03-24 17:55:16 +1100217 page = vmalloc_to_page((void *)i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (!page)
Nick Pigginca0b07d2008-02-07 16:20:50 +1000219 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 get_page(page);
Nick Pigginca0b07d2008-02-07 16:20:50 +1000221 vmf->page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Nick Pigginca0b07d2008-02-07 16:20:50 +1000223 DRM_DEBUG("shm_fault 0x%lx\n", offset);
224 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Benjamin Gaignard40e5f352020-03-06 11:29:34 +0100227/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 * \c close method for shared virtual memory.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000229 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 * \param vma virtual memory area.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000231 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 * Deletes map information if we are the last
233 * person to close a mapping and it's not in the global maplist.
234 */
Dave Airliec94f7022005-07-07 21:03:38 +1000235static void drm_vm_shm_close(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000237 struct drm_file *priv = vma->vm_file->private_data;
Dave Airlie2c14f282008-04-21 16:47:32 +1000238 struct drm_device *dev = priv->minor->dev;
Dave Airlie8fc2fdf2007-07-11 16:21:47 +1000239 struct drm_vma_entry *pt, *temp;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100240 struct drm_local_map *map;
Dave Airlie55910512007-07-11 16:53:40 +1000241 struct drm_map_list *r_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 int found_maps = 0;
243
244 DRM_DEBUG("0x%08lx,0x%08lx\n",
245 vma->vm_start, vma->vm_end - vma->vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 map = vma->vm_private_data;
248
Dave Airlie30e2fb12006-02-02 19:37:46 +1100249 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000250 list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000251 if (pt->vma->vm_private_data == map)
252 found_maps++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (pt->vma == vma) {
Dave Airliebd1b3312007-05-26 05:01:51 +1000254 list_del(&pt->head);
Eric Anholt9a298b22009-03-24 12:23:04 -0700255 kfree(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 /* We were the only map that was found */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000260 if (found_maps == 1 && map->flags & _DRM_REMOVABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 /* Check to see if we are in the maplist, if we are not, then
262 * we delete this mappings information.
263 */
264 found_maps = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000265 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000266 if (r_list->map == map)
267 found_maps++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000270 if (!found_maps) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 switch (map->type) {
272 case _DRM_REGISTERS:
273 case _DRM_FRAME_BUFFER:
Daniel Vetter28185642013-08-08 15:41:27 +0200274 arch_phys_wc_del(map->mtrr);
Christoph Hellwig004a7722007-01-08 21:56:59 +1100275 iounmap(map->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 break;
277 case _DRM_SHM:
278 vfree(map->handle);
279 break;
280 case _DRM_AGP:
281 case _DRM_SCATTER_GATHER:
282 break;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000283 case _DRM_CONSISTENT:
Chris Wilson8e4ff9b2020-02-02 17:16:32 +0000284 dma_free_coherent(&dev->pdev->dev,
285 map->size,
286 map->handle,
287 map->offset);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000288 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700290 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292 }
Dave Airlie30e2fb12006-02-02 19:37:46 +1100293 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Benjamin Gaignard40e5f352020-03-06 11:29:34 +0100296/*
Nick Pigginca0b07d2008-02-07 16:20:50 +1000297 * \c fault method for DMA virtual memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * \param address access address.
300 * \return pointer to the page structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000301 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * Determine the page number from the page offset and get it from drm_device_dma::pagelist.
303 */
Souptick Joarder6c259092018-05-10 19:12:04 +0530304static vm_fault_t drm_vm_dma_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Dave Jiang11bac802017-02-24 14:56:41 -0800306 struct vm_area_struct *vma = vmf->vma;
Dave Airlie84b1fd12007-07-11 15:53:27 +1000307 struct drm_file *priv = vma->vm_file->private_data;
Dave Airlie2c14f282008-04-21 16:47:32 +1000308 struct drm_device *dev = priv->minor->dev;
Dave Airliecdd55a22007-07-11 16:32:08 +1000309 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000310 unsigned long offset;
311 unsigned long page_nr;
312 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000314 if (!dma)
Nick Pigginca0b07d2008-02-07 16:20:50 +1000315 return VM_FAULT_SIGBUS; /* Error */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000316 if (!dma->pagelist)
Nick Pigginca0b07d2008-02-07 16:20:50 +1000317 return VM_FAULT_SIGBUS; /* Nothing allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Jan Kara1a29d852016-12-14 15:07:01 -0800319 offset = vmf->address - vma->vm_start;
320 /* vm_[pg]off[set] should be 0 */
Nick Pigginca0b07d2008-02-07 16:20:50 +1000321 page_nr = offset >> PAGE_SHIFT; /* page_nr could just be vmf->pgoff */
Ben Hutchingse1e78532013-10-27 21:52:39 +0000322 page = virt_to_page((void *)dma->pagelist[page_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 get_page(page);
Nick Pigginca0b07d2008-02-07 16:20:50 +1000325 vmf->page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Nick Pigginca0b07d2008-02-07 16:20:50 +1000327 DRM_DEBUG("dma_fault 0x%lx (page %lu)\n", offset, page_nr);
328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Benjamin Gaignard40e5f352020-03-06 11:29:34 +0100331/*
Nick Pigginca0b07d2008-02-07 16:20:50 +1000332 * \c fault method for scatter-gather virtual memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 * \param address access address.
335 * \return pointer to the page structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000336 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 * Determine the map offset from the page offset and get it from drm_sg_mem::pagelist.
338 */
Souptick Joarder6c259092018-05-10 19:12:04 +0530339static vm_fault_t drm_vm_sg_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Dave Jiang11bac802017-02-24 14:56:41 -0800341 struct vm_area_struct *vma = vmf->vma;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100342 struct drm_local_map *map = vma->vm_private_data;
Dave Airlie84b1fd12007-07-11 15:53:27 +1000343 struct drm_file *priv = vma->vm_file->private_data;
Dave Airlie2c14f282008-04-21 16:47:32 +1000344 struct drm_device *dev = priv->minor->dev;
Dave Airlie55910512007-07-11 16:53:40 +1000345 struct drm_sg_mem *entry = dev->sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 unsigned long offset;
347 unsigned long map_offset;
348 unsigned long page_offset;
349 struct page *page;
350
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000351 if (!entry)
Nick Pigginca0b07d2008-02-07 16:20:50 +1000352 return VM_FAULT_SIGBUS; /* Error */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000353 if (!entry->pagelist)
Nick Pigginca0b07d2008-02-07 16:20:50 +1000354 return VM_FAULT_SIGBUS; /* Nothing allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Jan Kara1a29d852016-12-14 15:07:01 -0800356 offset = vmf->address - vma->vm_start;
Dave Airlied1f2b552005-08-05 22:11:22 +1000357 map_offset = map->offset - (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 page_offset = (offset >> PAGE_SHIFT) + (map_offset >> PAGE_SHIFT);
359 page = entry->pagelist[page_offset];
360 get_page(page);
Nick Pigginca0b07d2008-02-07 16:20:50 +1000361 vmf->page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Nick Pigginca0b07d2008-02-07 16:20:50 +1000363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366/** AGP virtual memory operations */
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400367static const struct vm_operations_struct drm_vm_ops = {
Nick Pigginca0b07d2008-02-07 16:20:50 +1000368 .fault = drm_vm_fault,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000369 .open = drm_vm_open,
370 .close = drm_vm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371};
372
373/** Shared virtual memory operations */
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400374static const struct vm_operations_struct drm_vm_shm_ops = {
Nick Pigginca0b07d2008-02-07 16:20:50 +1000375 .fault = drm_vm_shm_fault,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000376 .open = drm_vm_open,
377 .close = drm_vm_shm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378};
379
380/** DMA virtual memory operations */
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400381static const struct vm_operations_struct drm_vm_dma_ops = {
Nick Pigginca0b07d2008-02-07 16:20:50 +1000382 .fault = drm_vm_dma_fault,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000383 .open = drm_vm_open,
384 .close = drm_vm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385};
386
387/** Scatter-gather virtual memory operations */
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400388static const struct vm_operations_struct drm_vm_sg_ops = {
Nick Pigginca0b07d2008-02-07 16:20:50 +1000389 .fault = drm_vm_sg_fault,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000390 .open = drm_vm_open,
391 .close = drm_vm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392};
393
Daniel Vetterf47dbdd2016-04-26 19:29:40 +0200394static void drm_vm_open_locked(struct drm_device *dev,
395 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Dave Airlie8fc2fdf2007-07-11 16:21:47 +1000397 struct drm_vma_entry *vma_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 DRM_DEBUG("0x%08lx,0x%08lx\n",
400 vma->vm_start, vma->vm_end - vma->vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Eric Anholt9a298b22009-03-24 12:23:04 -0700402 vma_entry = kmalloc(sizeof(*vma_entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (vma_entry) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000404 vma_entry->vma = vma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000405 vma_entry->pid = current->pid;
Dave Airliebd1b3312007-05-26 05:01:51 +1000406 list_add(&vma_entry->head, &dev->vmalist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408}
409
Thomas Hellstromd7d8aac2007-03-24 17:52:49 +1100410static void drm_vm_open(struct vm_area_struct *vma)
411{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000412 struct drm_file *priv = vma->vm_file->private_data;
Dave Airlie2c14f282008-04-21 16:47:32 +1000413 struct drm_device *dev = priv->minor->dev;
Thomas Hellstromd7d8aac2007-03-24 17:52:49 +1100414
415 mutex_lock(&dev->struct_mutex);
Rob Clarkb06d66b2012-05-01 11:04:51 -0500416 drm_vm_open_locked(dev, vma);
Thomas Hellstromd7d8aac2007-03-24 17:52:49 +1100417 mutex_unlock(&dev->struct_mutex);
418}
419
Daniel Vetterf47dbdd2016-04-26 19:29:40 +0200420static void drm_vm_close_locked(struct drm_device *dev,
421 struct vm_area_struct *vma)
Chris Wilson31dfbc92010-09-27 21:28:30 +0100422{
Chris Wilson31dfbc92010-09-27 21:28:30 +0100423 struct drm_vma_entry *pt, *temp;
424
425 DRM_DEBUG("0x%08lx,0x%08lx\n",
426 vma->vm_start, vma->vm_end - vma->vm_start);
Chris Wilson31dfbc92010-09-27 21:28:30 +0100427
428 list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
429 if (pt->vma == vma) {
430 list_del(&pt->head);
431 kfree(pt);
432 break;
433 }
434 }
435}
436
Benjamin Gaignard40e5f352020-03-06 11:29:34 +0100437/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 * \c close method for all virtual memory types.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000439 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 * \param vma virtual memory area.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000441 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * Search the \p vma private data entry in drm_device::vmalist, unlink it, and
443 * free it.
444 */
Dave Airliec94f7022005-07-07 21:03:38 +1000445static void drm_vm_close(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000447 struct drm_file *priv = vma->vm_file->private_data;
Dave Airlie2c14f282008-04-21 16:47:32 +1000448 struct drm_device *dev = priv->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Dave Airlie30e2fb12006-02-02 19:37:46 +1100450 mutex_lock(&dev->struct_mutex);
Rob Clarkb06d66b2012-05-01 11:04:51 -0500451 drm_vm_close_locked(dev, vma);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100452 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Benjamin Gaignard40e5f352020-03-06 11:29:34 +0100455/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * mmap DMA memory.
457 *
Eric Anholt6c340ea2007-08-25 20:23:09 +1000458 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 * \param vma virtual memory area.
460 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000461 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 * Sets the virtual memory area operations structure to vm_dma_ops, the file
463 * pointer, and calls vm_open().
464 */
Dave Airliec94f7022005-07-07 21:03:38 +1000465static int drm_mmap_dma(struct file *filp, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000467 struct drm_file *priv = filp->private_data;
468 struct drm_device *dev;
Dave Airliecdd55a22007-07-11 16:32:08 +1000469 struct drm_device_dma *dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000470 unsigned long length = vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Dave Airlie2c14f282008-04-21 16:47:32 +1000472 dev = priv->minor->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000473 dma = dev->dma;
Thomas Hellstrom15450852007-02-08 16:14:05 +1100474 DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
475 vma->vm_start, vma->vm_end, vma->vm_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000477 /* Length must match exact page count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (!dma || (length >> PAGE_SHIFT) != dma->page_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return -EINVAL;
480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
George Sapountzis3417f332006-10-24 12:03:04 -0700482 if (!capable(CAP_SYS_ADMIN) &&
483 (dma->flags & _DRM_DMA_USE_PCI_RO)) {
484 vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
485#if defined(__i386__) || defined(__x86_64__)
486 pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
487#else
488 /* Ye gads this is ugly. With more thought
489 we could move this up higher and use
490 `protection_map' instead. */
491 vma->vm_page_prot =
492 __pgprot(pte_val
493 (pte_wrprotect
494 (__pte(pgprot_val(vma->vm_page_prot)))));
495#endif
496 }
497
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000498 vma->vm_ops = &drm_vm_dma_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700500 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Rob Clarkb06d66b2012-05-01 11:04:51 -0500502 drm_vm_open_locked(dev, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return 0;
504}
505
Daniel Vettercbc60ca2010-08-23 22:53:28 +0200506static resource_size_t drm_core_get_reg_ofs(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508#ifdef __alpha__
Jay Estabrook82ba3fe2011-06-09 18:18:39 -0400509 return dev->hose->dense_mem_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510#else
511 return 0;
512#endif
513}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000514
Benjamin Gaignard40e5f352020-03-06 11:29:34 +0100515/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 * mmap DMA memory.
517 *
Eric Anholt6c340ea2007-08-25 20:23:09 +1000518 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 * \param vma virtual memory area.
520 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000521 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 * If the virtual memory area has no offset associated with it then it's a DMA
523 * area, so calls mmap_dma(). Otherwise searches the map in drm_device::maplist,
524 * checks that the restricted flag is not set, sets the virtual memory operations
525 * according to the mapping type and remaps the pages. Finally sets the file
526 * pointer and calls vm_open().
527 */
Daniel Vetterbfbf3c82014-09-23 15:46:49 +0200528static int drm_mmap_locked(struct file *filp, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000530 struct drm_file *priv = filp->private_data;
Dave Airlie2c14f282008-04-21 16:47:32 +1000531 struct drm_device *dev = priv->minor->dev;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100532 struct drm_local_map *map = NULL;
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100533 resource_size_t offset = 0;
Dave Airliee0be4282007-07-12 10:26:44 +1000534 struct drm_hash_item *hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Thomas Hellstrom15450852007-02-08 16:14:05 +1100536 DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
537 vma->vm_start, vma->vm_end, vma->vm_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000539 if (!priv->authenticated)
540 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 /* We check for "dma". On Apple's UniNorth, it's valid to have
543 * the AGP mapped at physical address 0
544 * --BenH.
545 */
Thomas Hellstrom15450852007-02-08 16:14:05 +1100546 if (!vma->vm_pgoff
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200547#if IS_ENABLED(CONFIG_AGP)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000548 && (!dev->agp
549 || dev->agp->agp_info.device->vendor != PCI_VENDOR_ID_APPLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550#endif
551 )
552 return drm_mmap_dma(filp, vma);
553
Thomas Hellstrom15450852007-02-08 16:14:05 +1100554 if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash)) {
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000555 DRM_ERROR("Could not find map\n");
556 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558
Dave Airlie55910512007-07-11 16:53:40 +1000559 map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000560 if (!map || ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return -EPERM;
562
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000563 /* Check for valid size. */
Dave Airlie54ba2f72007-02-10 12:07:47 +1100564 if (map->size < vma->vm_end - vma->vm_start)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000565 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 if (!capable(CAP_SYS_ADMIN) && (map->flags & _DRM_READ_ONLY)) {
568 vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
569#if defined(__i386__) || defined(__x86_64__)
570 pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
571#else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000572 /* Ye gads this is ugly. With more thought
573 we could move this up higher and use
574 `protection_map' instead. */
575 vma->vm_page_prot =
576 __pgprot(pte_val
577 (pte_wrprotect
578 (__pte(pgprot_val(vma->vm_page_prot)))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579#endif
580 }
581
582 switch (map->type) {
Jordan Crouse4b7fb9b2010-05-27 13:40:26 -0600583#if !defined(__arm__)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000584 case _DRM_AGP:
Daniel Vetterd9906752013-12-11 11:34:35 +0100585 if (dev->agp && dev->agp->cant_use_aperture) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000586 /*
587 * On some platforms we can't talk to bus dma address from the CPU, so for
588 * memory of type DRM_AGP, we'll deal with sorting out the real physical
Nick Pigginca0b07d2008-02-07 16:20:50 +1000589 * pages and mappings in fault()
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000590 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591#if defined(__powerpc__)
Benjamin Herrenschmidt37035e72016-06-27 19:00:23 +1000592 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593#endif
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000594 vma->vm_ops = &drm_vm_ops;
595 break;
596 }
Joe Perches84551af2020-03-12 12:17:12 -0700597 fallthrough; /* to _DRM_FRAME_BUFFER... */
Jordan Crouse4b7fb9b2010-05-27 13:40:26 -0600598#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 case _DRM_FRAME_BUFFER:
600 case _DRM_REGISTERS:
Daniel Vettercbc60ca2010-08-23 22:53:28 +0200601 offset = drm_core_get_reg_ofs(dev);
Andy Lutomirskiff47eaf2013-05-13 23:58:42 +0000602 vma->vm_page_prot = drm_io_prot(map, vma);
Dave Airlie9e9c1322007-03-24 17:57:54 +1100603 if (io_remap_pfn_range(vma, vma->vm_start,
604 (map->offset + offset) >> PAGE_SHIFT,
605 vma->vm_end - vma->vm_start,
606 vma->vm_page_prot))
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000607 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 DRM_DEBUG(" Type = %d; start = 0x%lx, end = 0x%lx,"
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100609 " offset = 0x%llx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 map->type,
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100611 vma->vm_start, vma->vm_end, (unsigned long long)(map->offset + offset));
Jordan Crouse4b7fb9b2010-05-27 13:40:26 -0600612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 vma->vm_ops = &drm_vm_ops;
614 break;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000615 case _DRM_CONSISTENT:
Hugh Dickins38315872007-03-24 17:55:16 +1100616 /* Consistent memory is really like shared memory. But
Nick Pigginca0b07d2008-02-07 16:20:50 +1000617 * it's allocated in a different way, so avoid fault */
Hugh Dickins38315872007-03-24 17:55:16 +1100618 if (remap_pfn_range(vma, vma->vm_start,
619 page_to_pfn(virt_to_page(map->handle)),
620 vma->vm_end - vma->vm_start, vma->vm_page_prot))
621 return -EAGAIN;
Benjamin Herrenschmidt6876b3b2008-03-28 14:23:07 -0700622 vma->vm_page_prot = drm_dma_prot(map->type, vma);
Joe Perches84551af2020-03-12 12:17:12 -0700623 fallthrough; /* to _DRM_SHM */
Hugh Dickins38315872007-03-24 17:55:16 +1100624 case _DRM_SHM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 vma->vm_ops = &drm_vm_shm_ops;
626 vma->vm_private_data = (void *)map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 break;
628 case _DRM_SCATTER_GATHER:
629 vma->vm_ops = &drm_vm_sg_ops;
630 vma->vm_private_data = (void *)map;
Benjamin Herrenschmidt5ff64612008-04-20 10:26:25 +1000631 vma->vm_page_prot = drm_dma_prot(map->type, vma);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000632 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 default:
634 return -EINVAL; /* This should never happen. */
635 }
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700636 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Rob Clarkb06d66b2012-05-01 11:04:51 -0500638 drm_vm_open_locked(dev, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return 0;
640}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000641
Daniel Vetterbfbf3c82014-09-23 15:46:49 +0200642int drm_legacy_mmap(struct file *filp, struct vm_area_struct *vma)
Thomas Hellstromd7d8aac2007-03-24 17:52:49 +1100643{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000644 struct drm_file *priv = filp->private_data;
Dave Airlie2c14f282008-04-21 16:47:32 +1000645 struct drm_device *dev = priv->minor->dev;
Thomas Hellstromd7d8aac2007-03-24 17:52:49 +1100646 int ret;
647
Daniel Vetterc07dcd62017-08-02 13:56:02 +0200648 if (drm_dev_is_unplugged(dev))
Dave Airlie2c07a212012-02-20 14:18:07 +0000649 return -ENODEV;
650
Thomas Hellstromd7d8aac2007-03-24 17:52:49 +1100651 mutex_lock(&dev->struct_mutex);
652 ret = drm_mmap_locked(filp, vma);
653 mutex_unlock(&dev->struct_mutex);
654
655 return ret;
656}
Daniel Vetterbfbf3c82014-09-23 15:46:49 +0200657EXPORT_SYMBOL(drm_legacy_mmap);
David Herrmann03decbe2014-08-29 12:12:29 +0200658
Dave Airlie61ae2272019-04-18 17:10:40 +1000659#if IS_ENABLED(CONFIG_DRM_LEGACY)
David Herrmann03decbe2014-08-29 12:12:29 +0200660void drm_legacy_vma_flush(struct drm_device *dev)
661{
662 struct drm_vma_entry *vma, *vma_temp;
663
664 /* Clear vma list (only needed for legacy drivers) */
665 list_for_each_entry_safe(vma, vma_temp, &dev->vmalist, head) {
666 list_del(&vma->head);
667 kfree(vma);
668 }
669}
Dave Airlie61ae2272019-04-18 17:10:40 +1000670#endif