blob: 2660543ad86a571dab759a45370369d43fbb16c0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
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
David Howells760285e2012-10-02 18:01:07 +010036#include <drm/drmP.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040037#include <linux/export.h>
David Herrmann03decbe2014-08-29 12:12:29 +020038#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#if defined(__ia64__)
40#include <linux/efi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#endif
Tom Lendacky95cf9262017-07-17 16:10:26 -050043#include <linux/mem_encrypt.h>
David Herrmann2791ee82014-08-29 12:12:33 +020044#include <asm/pgtable.h>
Ville Syrjälä43fc8842015-03-13 14:51:25 +020045#include "drm_internal.h"
David Herrmanncc5ea592014-08-29 12:12:32 +020046#include "drm_legacy.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
David Herrmann03decbe2014-08-29 12:12:29 +020048struct drm_vma_entry {
49 struct list_head head;
50 struct vm_area_struct *vma;
51 pid_t pid;
52};
53
Dave Airliec94f7022005-07-07 21:03:38 +100054static void drm_vm_open(struct vm_area_struct *vma);
55static void drm_vm_close(struct vm_area_struct *vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Andy Lutomirskiff47eaf2013-05-13 23:58:42 +000057static pgprot_t drm_io_prot(struct drm_local_map *map,
58 struct vm_area_struct *vma)
Dave Airlie5cc7f9a2007-02-10 11:53:13 +110059{
60 pgprot_t tmp = vm_get_page_prot(vma->vm_flags);
61
Tom Lendacky95cf9262017-07-17 16:10:26 -050062 /* We don't want graphics memory to be mapped encrypted */
63 tmp = pgprot_decrypted(tmp);
64
Benjamin Herrenschmidt6bd31102014-09-04 17:46:45 +100065#if defined(__i386__) || defined(__x86_64__) || defined(__powerpc__)
Andy Lutomirskif4350462013-05-13 23:58:43 +000066 if (map->type == _DRM_REGISTERS && !(map->flags & _DRM_WRITE_COMBINING))
67 tmp = pgprot_noncached(tmp);
68 else
69 tmp = pgprot_writecombine(tmp);
Benjamin Herrenschmidt6876b3b2008-03-28 14:23:07 -070070#elif defined(__ia64__)
Dave Airlie5cc7f9a2007-02-10 11:53:13 +110071 if (efi_range_is_wc(vma->vm_start, vma->vm_end -
72 vma->vm_start))
73 tmp = pgprot_writecombine(tmp);
74 else
75 tmp = pgprot_noncached(tmp);
Huacai Chen04cf55e2012-08-11 09:32:17 +000076#elif defined(__sparc__) || defined(__arm__) || defined(__mips__)
Benjamin Herrenschmidt6876b3b2008-03-28 14:23:07 -070077 tmp = pgprot_noncached(tmp);
78#endif
79 return tmp;
80}
81
82static pgprot_t drm_dma_prot(uint32_t map_type, struct vm_area_struct *vma)
83{
84 pgprot_t tmp = vm_get_page_prot(vma->vm_flags);
85
86#if defined(__powerpc__) && defined(CONFIG_NOT_COHERENT_CACHE)
Benjamin Herrenschmidt37035e72016-06-27 19:00:23 +100087 tmp = pgprot_noncached_wc(tmp);
Dave Airlie5cc7f9a2007-02-10 11:53:13 +110088#endif
89 return tmp;
90}
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/**
Nick Pigginca0b07d2008-02-07 16:20:50 +100093 * \c fault method for AGP virtual memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 *
95 * \param vma virtual memory area.
96 * \param address access address.
97 * \return pointer to the page structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100098 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * Find the right map and if it's AGP memory find the real physical page to
100 * map, get the page, increment the use count and return it.
101 */
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200102#if IS_ENABLED(CONFIG_AGP)
Ross Zwisler8f526e12017-02-24 14:59:04 -0800103static int drm_vm_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Dave Jiang11bac802017-02-24 14:56:41 -0800105 struct vm_area_struct *vma = vmf->vma;
Dave Airlie84b1fd12007-07-11 15:53:27 +1000106 struct drm_file *priv = vma->vm_file->private_data;
Dave Airlie2c14f282008-04-21 16:47:32 +1000107 struct drm_device *dev = priv->minor->dev;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100108 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000109 struct drm_map_list *r_list;
Dave Airliee0be4282007-07-12 10:26:44 +1000110 struct drm_hash_item *hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 /*
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000113 * Find the right map
114 */
Daniel Vetterd9906752013-12-11 11:34:35 +0100115 if (!dev->agp)
Nick Pigginca0b07d2008-02-07 16:20:50 +1000116 goto vm_fault_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000118 if (!dev->agp || !dev->agp->cant_use_aperture)
Nick Pigginca0b07d2008-02-07 16:20:50 +1000119 goto vm_fault_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Thomas Hellstrom15450852007-02-08 16:14:05 +1100121 if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash))
Nick Pigginca0b07d2008-02-07 16:20:50 +1000122 goto vm_fault_error;
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000123
Dave Airlie55910512007-07-11 16:53:40 +1000124 r_list = drm_hash_entry(hash, struct drm_map_list, hash);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000125 map = r_list->map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 if (map && map->type == _DRM_AGP) {
Nick Pigginca0b07d2008-02-07 16:20:50 +1000128 /*
129 * Using vm_pgoff as a selector forces us to use this unusual
130 * addressing scheme.
131 */
Jan Kara1a29d852016-12-14 15:07:01 -0800132 resource_size_t offset = vmf->address - vma->vm_start;
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100133 resource_size_t baddr = map->offset + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 struct drm_agp_mem *agpmem;
135 struct page *page;
136
137#ifdef __alpha__
138 /*
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000139 * Adjust to a bus-relative address
140 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 baddr -= dev->hose->mem_space->start;
142#endif
143
144 /*
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000145 * It's AGP memory - find the real physical page to map
146 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000147 list_for_each_entry(agpmem, &dev->agp->memory, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (agpmem->bound <= baddr &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000149 agpmem->bound + agpmem->pages * PAGE_SIZE > baddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 break;
151 }
152
Dan Carpenter161c4812010-08-19 11:39:57 +0200153 if (&agpmem->head == &dev->agp->memory)
Nick Pigginca0b07d2008-02-07 16:20:50 +1000154 goto vm_fault_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 /*
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000157 * Get the page, inc the use count, and return it
158 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 offset = (baddr - agpmem->bound) >> PAGE_SHIFT;
Dave Airlie07613ba2009-06-12 14:11:41 +1000160 page = agpmem->memory->pages[offset];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 get_page(page);
Nick Pigginca0b07d2008-02-07 16:20:50 +1000162 vmf->page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000164 DRM_DEBUG
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100165 ("baddr = 0x%llx page = 0x%p, offset = 0x%llx, count=%d\n",
166 (unsigned long long)baddr,
Dave Airlie07613ba2009-06-12 14:11:41 +1000167 agpmem->memory->pages[offset],
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100168 (unsigned long long)offset,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000169 page_count(page));
Nick Pigginca0b07d2008-02-07 16:20:50 +1000170 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000171 }
Nick Pigginca0b07d2008-02-07 16:20:50 +1000172vm_fault_error:
173 return VM_FAULT_SIGBUS; /* Disallow mremap */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200175#else
Ross Zwisler8f526e12017-02-24 14:59:04 -0800176static int drm_vm_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Nick Pigginca0b07d2008-02-07 16:20:50 +1000178 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200180#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182/**
183 * \c nopage method for shared virtual memory.
184 *
185 * \param vma virtual memory area.
186 * \param address access address.
187 * \return pointer to the page structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000188 *
Michael Opdenacker59c51592007-05-09 08:57:56 +0200189 * Get the mapping, find the real physical page to map, get the page, and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * return it.
191 */
Ross Zwisler8f526e12017-02-24 14:59:04 -0800192static int drm_vm_shm_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Dave Jiang11bac802017-02-24 14:56:41 -0800194 struct vm_area_struct *vma = vmf->vma;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100195 struct drm_local_map *map = vma->vm_private_data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000196 unsigned long offset;
197 unsigned long i;
198 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000200 if (!map)
Nick Pigginca0b07d2008-02-07 16:20:50 +1000201 return VM_FAULT_SIGBUS; /* Nothing allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Jan Kara1a29d852016-12-14 15:07:01 -0800203 offset = vmf->address - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 i = (unsigned long)map->handle + offset;
Hugh Dickins38315872007-03-24 17:55:16 +1100205 page = vmalloc_to_page((void *)i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (!page)
Nick Pigginca0b07d2008-02-07 16:20:50 +1000207 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 get_page(page);
Nick Pigginca0b07d2008-02-07 16:20:50 +1000209 vmf->page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Nick Pigginca0b07d2008-02-07 16:20:50 +1000211 DRM_DEBUG("shm_fault 0x%lx\n", offset);
212 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215/**
216 * \c close method for shared virtual memory.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000217 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * \param vma virtual memory area.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000219 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 * Deletes map information if we are the last
221 * person to close a mapping and it's not in the global maplist.
222 */
Dave Airliec94f7022005-07-07 21:03:38 +1000223static void drm_vm_shm_close(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000225 struct drm_file *priv = vma->vm_file->private_data;
Dave Airlie2c14f282008-04-21 16:47:32 +1000226 struct drm_device *dev = priv->minor->dev;
Dave Airlie8fc2fdf2007-07-11 16:21:47 +1000227 struct drm_vma_entry *pt, *temp;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100228 struct drm_local_map *map;
Dave Airlie55910512007-07-11 16:53:40 +1000229 struct drm_map_list *r_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 int found_maps = 0;
231
232 DRM_DEBUG("0x%08lx,0x%08lx\n",
233 vma->vm_start, vma->vm_end - vma->vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 map = vma->vm_private_data;
236
Dave Airlie30e2fb12006-02-02 19:37:46 +1100237 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000238 list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000239 if (pt->vma->vm_private_data == map)
240 found_maps++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (pt->vma == vma) {
Dave Airliebd1b3312007-05-26 05:01:51 +1000242 list_del(&pt->head);
Eric Anholt9a298b22009-03-24 12:23:04 -0700243 kfree(pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /* We were the only map that was found */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000248 if (found_maps == 1 && map->flags & _DRM_REMOVABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 /* Check to see if we are in the maplist, if we are not, then
250 * we delete this mappings information.
251 */
252 found_maps = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000253 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000254 if (r_list->map == map)
255 found_maps++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000258 if (!found_maps) {
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000259 drm_dma_handle_t dmah;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 switch (map->type) {
262 case _DRM_REGISTERS:
263 case _DRM_FRAME_BUFFER:
Daniel Vetter28185642013-08-08 15:41:27 +0200264 arch_phys_wc_del(map->mtrr);
Christoph Hellwig004a7722007-01-08 21:56:59 +1100265 iounmap(map->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 break;
267 case _DRM_SHM:
268 vfree(map->handle);
269 break;
270 case _DRM_AGP:
271 case _DRM_SCATTER_GATHER:
272 break;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000273 case _DRM_CONSISTENT:
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000274 dmah.vaddr = map->handle;
275 dmah.busaddr = map->offset;
276 dmah.size = map->size;
Daniel Vetter1c96e842014-09-10 12:43:51 +0200277 __drm_legacy_pci_free(dev, &dmah);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000278 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700280 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282 }
Dave Airlie30e2fb12006-02-02 19:37:46 +1100283 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
286/**
Nick Pigginca0b07d2008-02-07 16:20:50 +1000287 * \c fault method for DMA virtual memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 * \param address access address.
290 * \return pointer to the page structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000291 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 * Determine the page number from the page offset and get it from drm_device_dma::pagelist.
293 */
Ross Zwisler8f526e12017-02-24 14:59:04 -0800294static int drm_vm_dma_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Dave Jiang11bac802017-02-24 14:56:41 -0800296 struct vm_area_struct *vma = vmf->vma;
Dave Airlie84b1fd12007-07-11 15:53:27 +1000297 struct drm_file *priv = vma->vm_file->private_data;
Dave Airlie2c14f282008-04-21 16:47:32 +1000298 struct drm_device *dev = priv->minor->dev;
Dave Airliecdd55a22007-07-11 16:32:08 +1000299 struct drm_device_dma *dma = dev->dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000300 unsigned long offset;
301 unsigned long page_nr;
302 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000304 if (!dma)
Nick Pigginca0b07d2008-02-07 16:20:50 +1000305 return VM_FAULT_SIGBUS; /* Error */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000306 if (!dma->pagelist)
Nick Pigginca0b07d2008-02-07 16:20:50 +1000307 return VM_FAULT_SIGBUS; /* Nothing allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Jan Kara1a29d852016-12-14 15:07:01 -0800309 offset = vmf->address - vma->vm_start;
310 /* vm_[pg]off[set] should be 0 */
Nick Pigginca0b07d2008-02-07 16:20:50 +1000311 page_nr = offset >> PAGE_SHIFT; /* page_nr could just be vmf->pgoff */
Ben Hutchingse1e78532013-10-27 21:52:39 +0000312 page = virt_to_page((void *)dma->pagelist[page_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 get_page(page);
Nick Pigginca0b07d2008-02-07 16:20:50 +1000315 vmf->page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Nick Pigginca0b07d2008-02-07 16:20:50 +1000317 DRM_DEBUG("dma_fault 0x%lx (page %lu)\n", offset, page_nr);
318 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
321/**
Nick Pigginca0b07d2008-02-07 16:20:50 +1000322 * \c fault method for scatter-gather virtual memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 * \param address access address.
325 * \return pointer to the page structure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000326 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 * Determine the map offset from the page offset and get it from drm_sg_mem::pagelist.
328 */
Ross Zwisler8f526e12017-02-24 14:59:04 -0800329static int drm_vm_sg_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Dave Jiang11bac802017-02-24 14:56:41 -0800331 struct vm_area_struct *vma = vmf->vma;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100332 struct drm_local_map *map = vma->vm_private_data;
Dave Airlie84b1fd12007-07-11 15:53:27 +1000333 struct drm_file *priv = vma->vm_file->private_data;
Dave Airlie2c14f282008-04-21 16:47:32 +1000334 struct drm_device *dev = priv->minor->dev;
Dave Airlie55910512007-07-11 16:53:40 +1000335 struct drm_sg_mem *entry = dev->sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 unsigned long offset;
337 unsigned long map_offset;
338 unsigned long page_offset;
339 struct page *page;
340
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000341 if (!entry)
Nick Pigginca0b07d2008-02-07 16:20:50 +1000342 return VM_FAULT_SIGBUS; /* Error */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000343 if (!entry->pagelist)
Nick Pigginca0b07d2008-02-07 16:20:50 +1000344 return VM_FAULT_SIGBUS; /* Nothing allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Jan Kara1a29d852016-12-14 15:07:01 -0800346 offset = vmf->address - vma->vm_start;
Dave Airlied1f2b552005-08-05 22:11:22 +1000347 map_offset = map->offset - (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 page_offset = (offset >> PAGE_SHIFT) + (map_offset >> PAGE_SHIFT);
349 page = entry->pagelist[page_offset];
350 get_page(page);
Nick Pigginca0b07d2008-02-07 16:20:50 +1000351 vmf->page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Nick Pigginca0b07d2008-02-07 16:20:50 +1000353 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/** AGP virtual memory operations */
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400357static const struct vm_operations_struct drm_vm_ops = {
Nick Pigginca0b07d2008-02-07 16:20:50 +1000358 .fault = drm_vm_fault,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000359 .open = drm_vm_open,
360 .close = drm_vm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361};
362
363/** Shared virtual memory operations */
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400364static const struct vm_operations_struct drm_vm_shm_ops = {
Nick Pigginca0b07d2008-02-07 16:20:50 +1000365 .fault = drm_vm_shm_fault,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000366 .open = drm_vm_open,
367 .close = drm_vm_shm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368};
369
370/** DMA virtual memory operations */
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400371static const struct vm_operations_struct drm_vm_dma_ops = {
Nick Pigginca0b07d2008-02-07 16:20:50 +1000372 .fault = drm_vm_dma_fault,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000373 .open = drm_vm_open,
374 .close = drm_vm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375};
376
377/** Scatter-gather virtual memory operations */
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400378static const struct vm_operations_struct drm_vm_sg_ops = {
Nick Pigginca0b07d2008-02-07 16:20:50 +1000379 .fault = drm_vm_sg_fault,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000380 .open = drm_vm_open,
381 .close = drm_vm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382};
383
Daniel Vetterf47dbdd2016-04-26 19:29:40 +0200384static void drm_vm_open_locked(struct drm_device *dev,
385 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Dave Airlie8fc2fdf2007-07-11 16:21:47 +1000387 struct drm_vma_entry *vma_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 DRM_DEBUG("0x%08lx,0x%08lx\n",
390 vma->vm_start, vma->vm_end - vma->vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Eric Anholt9a298b22009-03-24 12:23:04 -0700392 vma_entry = kmalloc(sizeof(*vma_entry), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (vma_entry) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000394 vma_entry->vma = vma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000395 vma_entry->pid = current->pid;
Dave Airliebd1b3312007-05-26 05:01:51 +1000396 list_add(&vma_entry->head, &dev->vmalist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398}
399
Thomas Hellstromd7d8aac2007-03-24 17:52:49 +1100400static void drm_vm_open(struct vm_area_struct *vma)
401{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000402 struct drm_file *priv = vma->vm_file->private_data;
Dave Airlie2c14f282008-04-21 16:47:32 +1000403 struct drm_device *dev = priv->minor->dev;
Thomas Hellstromd7d8aac2007-03-24 17:52:49 +1100404
405 mutex_lock(&dev->struct_mutex);
Rob Clarkb06d66b2012-05-01 11:04:51 -0500406 drm_vm_open_locked(dev, vma);
Thomas Hellstromd7d8aac2007-03-24 17:52:49 +1100407 mutex_unlock(&dev->struct_mutex);
408}
409
Daniel Vetterf47dbdd2016-04-26 19:29:40 +0200410static void drm_vm_close_locked(struct drm_device *dev,
411 struct vm_area_struct *vma)
Chris Wilson31dfbc92010-09-27 21:28:30 +0100412{
Chris Wilson31dfbc92010-09-27 21:28:30 +0100413 struct drm_vma_entry *pt, *temp;
414
415 DRM_DEBUG("0x%08lx,0x%08lx\n",
416 vma->vm_start, vma->vm_end - vma->vm_start);
Chris Wilson31dfbc92010-09-27 21:28:30 +0100417
418 list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
419 if (pt->vma == vma) {
420 list_del(&pt->head);
421 kfree(pt);
422 break;
423 }
424 }
425}
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427/**
428 * \c close method for all virtual memory types.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000429 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 * \param vma virtual memory area.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000431 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 * Search the \p vma private data entry in drm_device::vmalist, unlink it, and
433 * free it.
434 */
Dave Airliec94f7022005-07-07 21:03:38 +1000435static void drm_vm_close(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000437 struct drm_file *priv = vma->vm_file->private_data;
Dave Airlie2c14f282008-04-21 16:47:32 +1000438 struct drm_device *dev = priv->minor->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Dave Airlie30e2fb12006-02-02 19:37:46 +1100440 mutex_lock(&dev->struct_mutex);
Rob Clarkb06d66b2012-05-01 11:04:51 -0500441 drm_vm_close_locked(dev, vma);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100442 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
445/**
446 * mmap DMA memory.
447 *
Eric Anholt6c340ea2007-08-25 20:23:09 +1000448 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 * \param vma virtual memory area.
450 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000451 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 * Sets the virtual memory area operations structure to vm_dma_ops, the file
453 * pointer, and calls vm_open().
454 */
Dave Airliec94f7022005-07-07 21:03:38 +1000455static int drm_mmap_dma(struct file *filp, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000457 struct drm_file *priv = filp->private_data;
458 struct drm_device *dev;
Dave Airliecdd55a22007-07-11 16:32:08 +1000459 struct drm_device_dma *dma;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000460 unsigned long length = vma->vm_end - vma->vm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Dave Airlie2c14f282008-04-21 16:47:32 +1000462 dev = priv->minor->dev;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000463 dma = dev->dma;
Thomas Hellstrom15450852007-02-08 16:14:05 +1100464 DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
465 vma->vm_start, vma->vm_end, vma->vm_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000467 /* Length must match exact page count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (!dma || (length >> PAGE_SHIFT) != dma->page_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return -EINVAL;
470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
George Sapountzis3417f332006-10-24 12:03:04 -0700472 if (!capable(CAP_SYS_ADMIN) &&
473 (dma->flags & _DRM_DMA_USE_PCI_RO)) {
474 vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
475#if defined(__i386__) || defined(__x86_64__)
476 pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
477#else
478 /* Ye gads this is ugly. With more thought
479 we could move this up higher and use
480 `protection_map' instead. */
481 vma->vm_page_prot =
482 __pgprot(pte_val
483 (pte_wrprotect
484 (__pte(pgprot_val(vma->vm_page_prot)))));
485#endif
486 }
487
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000488 vma->vm_ops = &drm_vm_dma_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700490 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Rob Clarkb06d66b2012-05-01 11:04:51 -0500492 drm_vm_open_locked(dev, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return 0;
494}
495
Daniel Vettercbc60ca2010-08-23 22:53:28 +0200496static resource_size_t drm_core_get_reg_ofs(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498#ifdef __alpha__
Jay Estabrook82ba3fe2011-06-09 18:18:39 -0400499 return dev->hose->dense_mem_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500#else
501 return 0;
502#endif
503}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505/**
506 * mmap DMA memory.
507 *
Eric Anholt6c340ea2007-08-25 20:23:09 +1000508 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 * \param vma virtual memory area.
510 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000511 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 * If the virtual memory area has no offset associated with it then it's a DMA
513 * area, so calls mmap_dma(). Otherwise searches the map in drm_device::maplist,
514 * checks that the restricted flag is not set, sets the virtual memory operations
515 * according to the mapping type and remaps the pages. Finally sets the file
516 * pointer and calls vm_open().
517 */
Daniel Vetterbfbf3c82014-09-23 15:46:49 +0200518static int drm_mmap_locked(struct file *filp, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000520 struct drm_file *priv = filp->private_data;
Dave Airlie2c14f282008-04-21 16:47:32 +1000521 struct drm_device *dev = priv->minor->dev;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100522 struct drm_local_map *map = NULL;
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100523 resource_size_t offset = 0;
Dave Airliee0be4282007-07-12 10:26:44 +1000524 struct drm_hash_item *hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Thomas Hellstrom15450852007-02-08 16:14:05 +1100526 DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
527 vma->vm_start, vma->vm_end, vma->vm_pgoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000529 if (!priv->authenticated)
530 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 /* We check for "dma". On Apple's UniNorth, it's valid to have
533 * the AGP mapped at physical address 0
534 * --BenH.
535 */
Thomas Hellstrom15450852007-02-08 16:14:05 +1100536 if (!vma->vm_pgoff
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200537#if IS_ENABLED(CONFIG_AGP)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000538 && (!dev->agp
539 || dev->agp->agp_info.device->vendor != PCI_VENDOR_ID_APPLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540#endif
541 )
542 return drm_mmap_dma(filp, vma);
543
Thomas Hellstrom15450852007-02-08 16:14:05 +1100544 if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash)) {
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000545 DRM_ERROR("Could not find map\n");
546 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548
Dave Airlie55910512007-07-11 16:53:40 +1000549 map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000550 if (!map || ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return -EPERM;
552
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000553 /* Check for valid size. */
Dave Airlie54ba2f72007-02-10 12:07:47 +1100554 if (map->size < vma->vm_end - vma->vm_start)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000555 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 if (!capable(CAP_SYS_ADMIN) && (map->flags & _DRM_READ_ONLY)) {
558 vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
559#if defined(__i386__) || defined(__x86_64__)
560 pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
561#else
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000562 /* Ye gads this is ugly. With more thought
563 we could move this up higher and use
564 `protection_map' instead. */
565 vma->vm_page_prot =
566 __pgprot(pte_val
567 (pte_wrprotect
568 (__pte(pgprot_val(vma->vm_page_prot)))));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569#endif
570 }
571
572 switch (map->type) {
Jordan Crouse4b7fb9b2010-05-27 13:40:26 -0600573#if !defined(__arm__)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000574 case _DRM_AGP:
Daniel Vetterd9906752013-12-11 11:34:35 +0100575 if (dev->agp && dev->agp->cant_use_aperture) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000576 /*
577 * On some platforms we can't talk to bus dma address from the CPU, so for
578 * memory of type DRM_AGP, we'll deal with sorting out the real physical
Nick Pigginca0b07d2008-02-07 16:20:50 +1000579 * pages and mappings in fault()
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000580 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581#if defined(__powerpc__)
Benjamin Herrenschmidt37035e72016-06-27 19:00:23 +1000582 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583#endif
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000584 vma->vm_ops = &drm_vm_ops;
585 break;
586 }
587 /* fall through to _DRM_FRAME_BUFFER... */
Jordan Crouse4b7fb9b2010-05-27 13:40:26 -0600588#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 case _DRM_FRAME_BUFFER:
590 case _DRM_REGISTERS:
Daniel Vettercbc60ca2010-08-23 22:53:28 +0200591 offset = drm_core_get_reg_ofs(dev);
Andy Lutomirskiff47eaf2013-05-13 23:58:42 +0000592 vma->vm_page_prot = drm_io_prot(map, vma);
Dave Airlie9e9c1322007-03-24 17:57:54 +1100593 if (io_remap_pfn_range(vma, vma->vm_start,
594 (map->offset + offset) >> PAGE_SHIFT,
595 vma->vm_end - vma->vm_start,
596 vma->vm_page_prot))
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000597 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 DRM_DEBUG(" Type = %d; start = 0x%lx, end = 0x%lx,"
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100599 " offset = 0x%llx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 map->type,
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100601 vma->vm_start, vma->vm_end, (unsigned long long)(map->offset + offset));
Jordan Crouse4b7fb9b2010-05-27 13:40:26 -0600602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 vma->vm_ops = &drm_vm_ops;
604 break;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000605 case _DRM_CONSISTENT:
Hugh Dickins38315872007-03-24 17:55:16 +1100606 /* Consistent memory is really like shared memory. But
Nick Pigginca0b07d2008-02-07 16:20:50 +1000607 * it's allocated in a different way, so avoid fault */
Hugh Dickins38315872007-03-24 17:55:16 +1100608 if (remap_pfn_range(vma, vma->vm_start,
609 page_to_pfn(virt_to_page(map->handle)),
610 vma->vm_end - vma->vm_start, vma->vm_page_prot))
611 return -EAGAIN;
Benjamin Herrenschmidt6876b3b2008-03-28 14:23:07 -0700612 vma->vm_page_prot = drm_dma_prot(map->type, vma);
Hugh Dickins38315872007-03-24 17:55:16 +1100613 /* fall through to _DRM_SHM */
614 case _DRM_SHM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 vma->vm_ops = &drm_vm_shm_ops;
616 vma->vm_private_data = (void *)map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 break;
618 case _DRM_SCATTER_GATHER:
619 vma->vm_ops = &drm_vm_sg_ops;
620 vma->vm_private_data = (void *)map;
Benjamin Herrenschmidt5ff64612008-04-20 10:26:25 +1000621 vma->vm_page_prot = drm_dma_prot(map->type, vma);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000622 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 default:
624 return -EINVAL; /* This should never happen. */
625 }
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700626 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Rob Clarkb06d66b2012-05-01 11:04:51 -0500628 drm_vm_open_locked(dev, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return 0;
630}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000631
Daniel Vetterbfbf3c82014-09-23 15:46:49 +0200632int drm_legacy_mmap(struct file *filp, struct vm_area_struct *vma)
Thomas Hellstromd7d8aac2007-03-24 17:52:49 +1100633{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000634 struct drm_file *priv = filp->private_data;
Dave Airlie2c14f282008-04-21 16:47:32 +1000635 struct drm_device *dev = priv->minor->dev;
Thomas Hellstromd7d8aac2007-03-24 17:52:49 +1100636 int ret;
637
Daniel Vetterc07dcd62017-08-02 13:56:02 +0200638 if (drm_dev_is_unplugged(dev))
Dave Airlie2c07a212012-02-20 14:18:07 +0000639 return -ENODEV;
640
Thomas Hellstromd7d8aac2007-03-24 17:52:49 +1100641 mutex_lock(&dev->struct_mutex);
642 ret = drm_mmap_locked(filp, vma);
643 mutex_unlock(&dev->struct_mutex);
644
645 return ret;
646}
Daniel Vetterbfbf3c82014-09-23 15:46:49 +0200647EXPORT_SYMBOL(drm_legacy_mmap);
David Herrmann03decbe2014-08-29 12:12:29 +0200648
649void drm_legacy_vma_flush(struct drm_device *dev)
650{
651 struct drm_vma_entry *vma, *vma_temp;
652
653 /* Clear vma list (only needed for legacy drivers) */
654 list_for_each_entry_safe(vma, vma_temp, &dev->vmalist, head) {
655 list_del(&vma->head);
656 kfree(vma);
657 }
658}