drm_vm.c 17.3 KB
Newer Older
L
Linus Torvalds 已提交
1
/**
D
Dave Airlie 已提交
2
 * \file drm_vm.c
L
Linus Torvalds 已提交
3
 * Memory mapping for DRM
D
Dave Airlie 已提交
4
 *
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 * \author Rickard E. (Rik) Faith <faith@valinux.com>
 * \author Gareth Hughes <gareth@valinux.com>
 */

/*
 * Created: Mon Jan  4 08:58:31 1999 by faith@valinux.com
 *
 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

36
#include <drm/drmP.h>
37
#include <linux/export.h>
38
#include <linux/seq_file.h>
L
Linus Torvalds 已提交
39 40
#if defined(__ia64__)
#include <linux/efi.h>
41
#include <linux/slab.h>
L
Linus Torvalds 已提交
42
#endif
43
#include <linux/mem_encrypt.h>
44
#include <asm/pgtable.h>
V
Ville Syrjälä 已提交
45
#include "drm_internal.h"
46
#include "drm_legacy.h"
L
Linus Torvalds 已提交
47

48 49 50 51 52 53
struct drm_vma_entry {
	struct list_head head;
	struct vm_area_struct *vma;
	pid_t pid;
};

D
Dave Airlie 已提交
54 55
static void drm_vm_open(struct vm_area_struct *vma);
static void drm_vm_close(struct vm_area_struct *vma);
L
Linus Torvalds 已提交
56

57 58
static pgprot_t drm_io_prot(struct drm_local_map *map,
			    struct vm_area_struct *vma)
59 60 61
{
	pgprot_t tmp = vm_get_page_prot(vma->vm_flags);

62 63 64
	/* We don't want graphics memory to be mapped encrypted */
	tmp = pgprot_decrypted(tmp);

65
#if defined(__i386__) || defined(__x86_64__) || defined(__powerpc__)
66 67 68 69
	if (map->type == _DRM_REGISTERS && !(map->flags & _DRM_WRITE_COMBINING))
		tmp = pgprot_noncached(tmp);
	else
		tmp = pgprot_writecombine(tmp);
70
#elif defined(__ia64__)
71 72 73 74 75
	if (efi_range_is_wc(vma->vm_start, vma->vm_end -
				    vma->vm_start))
		tmp = pgprot_writecombine(tmp);
	else
		tmp = pgprot_noncached(tmp);
76
#elif defined(__sparc__) || defined(__arm__) || defined(__mips__)
77 78 79 80 81 82 83 84 85 86
	tmp = pgprot_noncached(tmp);
#endif
	return tmp;
}

static pgprot_t drm_dma_prot(uint32_t map_type, struct vm_area_struct *vma)
{
	pgprot_t tmp = vm_get_page_prot(vma->vm_flags);

#if defined(__powerpc__) && defined(CONFIG_NOT_COHERENT_CACHE)
87
	tmp = pgprot_noncached_wc(tmp);
88 89 90 91
#endif
	return tmp;
}

L
Linus Torvalds 已提交
92
/**
93
 * \c fault method for AGP virtual memory.
L
Linus Torvalds 已提交
94 95 96 97
 *
 * \param vma virtual memory area.
 * \param address access address.
 * \return pointer to the page structure.
D
Dave Airlie 已提交
98
 *
L
Linus Torvalds 已提交
99 100 101
 * Find the right map and if it's AGP memory find the real physical page to
 * map, get the page, increment the use count and return it.
 */
D
Daniel Vetter 已提交
102
#if IS_ENABLED(CONFIG_AGP)
103
static int drm_vm_fault(struct vm_fault *vmf)
L
Linus Torvalds 已提交
104
{
105
	struct vm_area_struct *vma = vmf->vma;
106
	struct drm_file *priv = vma->vm_file->private_data;
107
	struct drm_device *dev = priv->minor->dev;
108
	struct drm_local_map *map = NULL;
D
Dave Airlie 已提交
109
	struct drm_map_list *r_list;
110
	struct drm_hash_item *hash;
L
Linus Torvalds 已提交
111 112

	/*
D
Dave Airlie 已提交
113 114
	 * Find the right map
	 */
D
Daniel Vetter 已提交
115
	if (!dev->agp)
116
		goto vm_fault_error;
L
Linus Torvalds 已提交
117

D
Dave Airlie 已提交
118
	if (!dev->agp || !dev->agp->cant_use_aperture)
119
		goto vm_fault_error;
L
Linus Torvalds 已提交
120

121
	if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash))
122
		goto vm_fault_error;
123

D
Dave Airlie 已提交
124
	r_list = drm_hash_entry(hash, struct drm_map_list, hash);
125
	map = r_list->map;
L
Linus Torvalds 已提交
126 127

	if (map && map->type == _DRM_AGP) {
128 129 130 131
		/*
		 * Using vm_pgoff as a selector forces us to use this unusual
		 * addressing scheme.
		 */
132
		resource_size_t offset = vmf->address - vma->vm_start;
133
		resource_size_t baddr = map->offset + offset;
L
Linus Torvalds 已提交
134 135 136 137 138
		struct drm_agp_mem *agpmem;
		struct page *page;

#ifdef __alpha__
		/*
D
Dave Airlie 已提交
139 140
		 * Adjust to a bus-relative address
		 */
L
Linus Torvalds 已提交
141 142 143 144
		baddr -= dev->hose->mem_space->start;
#endif

		/*
D
Dave Airlie 已提交
145 146
		 * It's AGP memory - find the real physical page to map
		 */
147
		list_for_each_entry(agpmem, &dev->agp->memory, head) {
L
Linus Torvalds 已提交
148
			if (agpmem->bound <= baddr &&
D
Dave Airlie 已提交
149
			    agpmem->bound + agpmem->pages * PAGE_SIZE > baddr)
L
Linus Torvalds 已提交
150 151 152
				break;
		}

D
Dan Carpenter 已提交
153
		if (&agpmem->head == &dev->agp->memory)
154
			goto vm_fault_error;
L
Linus Torvalds 已提交
155 156

		/*
D
Dave Airlie 已提交
157 158
		 * Get the page, inc the use count, and return it
		 */
L
Linus Torvalds 已提交
159
		offset = (baddr - agpmem->bound) >> PAGE_SHIFT;
160
		page = agpmem->memory->pages[offset];
L
Linus Torvalds 已提交
161
		get_page(page);
162
		vmf->page = page;
L
Linus Torvalds 已提交
163

D
Dave Airlie 已提交
164
		DRM_DEBUG
165 166
		    ("baddr = 0x%llx page = 0x%p, offset = 0x%llx, count=%d\n",
		     (unsigned long long)baddr,
167
		     agpmem->memory->pages[offset],
168
		     (unsigned long long)offset,
D
Dave Airlie 已提交
169
		     page_count(page));
170
		return 0;
D
Dave Airlie 已提交
171
	}
172 173
vm_fault_error:
	return VM_FAULT_SIGBUS;	/* Disallow mremap */
L
Linus Torvalds 已提交
174
}
D
Daniel Vetter 已提交
175
#else
176
static int drm_vm_fault(struct vm_fault *vmf)
L
Linus Torvalds 已提交
177
{
178
	return VM_FAULT_SIGBUS;
L
Linus Torvalds 已提交
179
}
D
Daniel Vetter 已提交
180
#endif
L
Linus Torvalds 已提交
181 182 183 184 185 186 187

/**
 * \c nopage method for shared virtual memory.
 *
 * \param vma virtual memory area.
 * \param address access address.
 * \return pointer to the page structure.
D
Dave Airlie 已提交
188
 *
189
 * Get the mapping, find the real physical page to map, get the page, and
L
Linus Torvalds 已提交
190 191
 * return it.
 */
192
static int drm_vm_shm_fault(struct vm_fault *vmf)
L
Linus Torvalds 已提交
193
{
194
	struct vm_area_struct *vma = vmf->vma;
195
	struct drm_local_map *map = vma->vm_private_data;
D
Dave Airlie 已提交
196 197 198
	unsigned long offset;
	unsigned long i;
	struct page *page;
L
Linus Torvalds 已提交
199

D
Dave Airlie 已提交
200
	if (!map)
201
		return VM_FAULT_SIGBUS;	/* Nothing allocated */
L
Linus Torvalds 已提交
202

203
	offset = vmf->address - vma->vm_start;
L
Linus Torvalds 已提交
204
	i = (unsigned long)map->handle + offset;
H
Hugh Dickins 已提交
205
	page = vmalloc_to_page((void *)i);
L
Linus Torvalds 已提交
206
	if (!page)
207
		return VM_FAULT_SIGBUS;
L
Linus Torvalds 已提交
208
	get_page(page);
209
	vmf->page = page;
L
Linus Torvalds 已提交
210

211 212
	DRM_DEBUG("shm_fault 0x%lx\n", offset);
	return 0;
L
Linus Torvalds 已提交
213 214 215 216
}

/**
 * \c close method for shared virtual memory.
D
Dave Airlie 已提交
217
 *
L
Linus Torvalds 已提交
218
 * \param vma virtual memory area.
D
Dave Airlie 已提交
219
 *
L
Linus Torvalds 已提交
220 221 222
 * Deletes map information if we are the last
 * person to close a mapping and it's not in the global maplist.
 */
D
Dave Airlie 已提交
223
static void drm_vm_shm_close(struct vm_area_struct *vma)
L
Linus Torvalds 已提交
224
{
225
	struct drm_file *priv = vma->vm_file->private_data;
226
	struct drm_device *dev = priv->minor->dev;
227
	struct drm_vma_entry *pt, *temp;
228
	struct drm_local_map *map;
D
Dave Airlie 已提交
229
	struct drm_map_list *r_list;
L
Linus Torvalds 已提交
230 231 232 233 234 235 236
	int found_maps = 0;

	DRM_DEBUG("0x%08lx,0x%08lx\n",
		  vma->vm_start, vma->vm_end - vma->vm_start);

	map = vma->vm_private_data;

D
Dave Airlie 已提交
237
	mutex_lock(&dev->struct_mutex);
238
	list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
D
Dave Airlie 已提交
239 240
		if (pt->vma->vm_private_data == map)
			found_maps++;
L
Linus Torvalds 已提交
241
		if (pt->vma == vma) {
242
			list_del(&pt->head);
243
			kfree(pt);
L
Linus Torvalds 已提交
244 245
		}
	}
246

L
Linus Torvalds 已提交
247
	/* We were the only map that was found */
D
Dave Airlie 已提交
248
	if (found_maps == 1 && map->flags & _DRM_REMOVABLE) {
L
Linus Torvalds 已提交
249 250 251 252
		/* Check to see if we are in the maplist, if we are not, then
		 * we delete this mappings information.
		 */
		found_maps = 0;
253
		list_for_each_entry(r_list, &dev->maplist, head) {
D
Dave Airlie 已提交
254 255
			if (r_list->map == map)
				found_maps++;
L
Linus Torvalds 已提交
256 257
		}

D
Dave Airlie 已提交
258
		if (!found_maps) {
259 260
			drm_dma_handle_t dmah;

L
Linus Torvalds 已提交
261 262 263
			switch (map->type) {
			case _DRM_REGISTERS:
			case _DRM_FRAME_BUFFER:
264
				arch_phys_wc_del(map->mtrr);
265
				iounmap(map->handle);
L
Linus Torvalds 已提交
266 267 268 269 270 271 272
				break;
			case _DRM_SHM:
				vfree(map->handle);
				break;
			case _DRM_AGP:
			case _DRM_SCATTER_GATHER:
				break;
D
Dave Airlie 已提交
273
			case _DRM_CONSISTENT:
274 275 276
				dmah.vaddr = map->handle;
				dmah.busaddr = map->offset;
				dmah.size = map->size;
277
				__drm_legacy_pci_free(dev, &dmah);
D
Dave Airlie 已提交
278
				break;
L
Linus Torvalds 已提交
279
			}
280
			kfree(map);
L
Linus Torvalds 已提交
281 282
		}
	}
D
Dave Airlie 已提交
283
	mutex_unlock(&dev->struct_mutex);
L
Linus Torvalds 已提交
284 285 286
}

/**
287
 * \c fault method for DMA virtual memory.
L
Linus Torvalds 已提交
288 289 290
 *
 * \param address access address.
 * \return pointer to the page structure.
D
Dave Airlie 已提交
291
 *
L
Linus Torvalds 已提交
292 293
 * Determine the page number from the page offset and get it from drm_device_dma::pagelist.
 */
294
static int drm_vm_dma_fault(struct vm_fault *vmf)
L
Linus Torvalds 已提交
295
{
296
	struct vm_area_struct *vma = vmf->vma;
297
	struct drm_file *priv = vma->vm_file->private_data;
298
	struct drm_device *dev = priv->minor->dev;
299
	struct drm_device_dma *dma = dev->dma;
D
Dave Airlie 已提交
300 301 302 303 304
	unsigned long offset;
	unsigned long page_nr;
	struct page *page;

	if (!dma)
305
		return VM_FAULT_SIGBUS;	/* Error */
D
Dave Airlie 已提交
306
	if (!dma->pagelist)
307
		return VM_FAULT_SIGBUS;	/* Nothing allocated */
D
Dave Airlie 已提交
308

309 310
	offset = vmf->address - vma->vm_start;
					/* vm_[pg]off[set] should be 0 */
311
	page_nr = offset >> PAGE_SHIFT; /* page_nr could just be vmf->pgoff */
312
	page = virt_to_page((void *)dma->pagelist[page_nr]);
L
Linus Torvalds 已提交
313 314

	get_page(page);
315
	vmf->page = page;
L
Linus Torvalds 已提交
316

317 318
	DRM_DEBUG("dma_fault 0x%lx (page %lu)\n", offset, page_nr);
	return 0;
L
Linus Torvalds 已提交
319 320 321
}

/**
322
 * \c fault method for scatter-gather virtual memory.
L
Linus Torvalds 已提交
323 324 325
 *
 * \param address access address.
 * \return pointer to the page structure.
D
Dave Airlie 已提交
326
 *
L
Linus Torvalds 已提交
327 328
 * Determine the map offset from the page offset and get it from drm_sg_mem::pagelist.
 */
329
static int drm_vm_sg_fault(struct vm_fault *vmf)
L
Linus Torvalds 已提交
330
{
331
	struct vm_area_struct *vma = vmf->vma;
332
	struct drm_local_map *map = vma->vm_private_data;
333
	struct drm_file *priv = vma->vm_file->private_data;
334
	struct drm_device *dev = priv->minor->dev;
D
Dave Airlie 已提交
335
	struct drm_sg_mem *entry = dev->sg;
L
Linus Torvalds 已提交
336 337 338 339 340
	unsigned long offset;
	unsigned long map_offset;
	unsigned long page_offset;
	struct page *page;

D
Dave Airlie 已提交
341
	if (!entry)
342
		return VM_FAULT_SIGBUS;	/* Error */
D
Dave Airlie 已提交
343
	if (!entry->pagelist)
344
		return VM_FAULT_SIGBUS;	/* Nothing allocated */
L
Linus Torvalds 已提交
345

346
	offset = vmf->address - vma->vm_start;
347
	map_offset = map->offset - (unsigned long)dev->sg->virtual;
L
Linus Torvalds 已提交
348 349 350
	page_offset = (offset >> PAGE_SHIFT) + (map_offset >> PAGE_SHIFT);
	page = entry->pagelist[page_offset];
	get_page(page);
351
	vmf->page = page;
L
Linus Torvalds 已提交
352

353
	return 0;
L
Linus Torvalds 已提交
354 355 356
}

/** AGP virtual memory operations */
357
static const struct vm_operations_struct drm_vm_ops = {
358
	.fault = drm_vm_fault,
D
Dave Airlie 已提交
359 360
	.open = drm_vm_open,
	.close = drm_vm_close,
L
Linus Torvalds 已提交
361 362 363
};

/** Shared virtual memory operations */
364
static const struct vm_operations_struct drm_vm_shm_ops = {
365
	.fault = drm_vm_shm_fault,
D
Dave Airlie 已提交
366 367
	.open = drm_vm_open,
	.close = drm_vm_shm_close,
L
Linus Torvalds 已提交
368 369 370
};

/** DMA virtual memory operations */
371
static const struct vm_operations_struct drm_vm_dma_ops = {
372
	.fault = drm_vm_dma_fault,
D
Dave Airlie 已提交
373 374
	.open = drm_vm_open,
	.close = drm_vm_close,
L
Linus Torvalds 已提交
375 376 377
};

/** Scatter-gather virtual memory operations */
378
static const struct vm_operations_struct drm_vm_sg_ops = {
379
	.fault = drm_vm_sg_fault,
D
Dave Airlie 已提交
380 381
	.open = drm_vm_open,
	.close = drm_vm_close,
L
Linus Torvalds 已提交
382 383
};

384 385
static void drm_vm_open_locked(struct drm_device *dev,
			       struct vm_area_struct *vma)
L
Linus Torvalds 已提交
386
{
387
	struct drm_vma_entry *vma_entry;
L
Linus Torvalds 已提交
388 389 390 391

	DRM_DEBUG("0x%08lx,0x%08lx\n",
		  vma->vm_start, vma->vm_end - vma->vm_start);

392
	vma_entry = kmalloc(sizeof(*vma_entry), GFP_KERNEL);
L
Linus Torvalds 已提交
393
	if (vma_entry) {
D
Dave Airlie 已提交
394 395
		vma_entry->vma = vma;
		vma_entry->pid = current->pid;
396
		list_add(&vma_entry->head, &dev->vmalist);
L
Linus Torvalds 已提交
397 398 399
	}
}

400 401
static void drm_vm_open(struct vm_area_struct *vma)
{
402
	struct drm_file *priv = vma->vm_file->private_data;
403
	struct drm_device *dev = priv->minor->dev;
404 405

	mutex_lock(&dev->struct_mutex);
406
	drm_vm_open_locked(dev, vma);
407 408 409
	mutex_unlock(&dev->struct_mutex);
}

410 411
static void drm_vm_close_locked(struct drm_device *dev,
				struct vm_area_struct *vma)
L
Linus Torvalds 已提交
412
{
413
	struct drm_vma_entry *pt, *temp;
L
Linus Torvalds 已提交
414 415 416 417

	DRM_DEBUG("0x%08lx,0x%08lx\n",
		  vma->vm_start, vma->vm_end - vma->vm_start);

418
	list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
L
Linus Torvalds 已提交
419
		if (pt->vma == vma) {
420
			list_del(&pt->head);
421
			kfree(pt);
L
Linus Torvalds 已提交
422 423 424
			break;
		}
	}
C
Chris Wilson 已提交
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
}

/**
 * \c close method for all virtual memory types.
 *
 * \param vma virtual memory area.
 *
 * Search the \p vma private data entry in drm_device::vmalist, unlink it, and
 * free it.
 */
static void drm_vm_close(struct vm_area_struct *vma)
{
	struct drm_file *priv = vma->vm_file->private_data;
	struct drm_device *dev = priv->minor->dev;

	mutex_lock(&dev->struct_mutex);
441
	drm_vm_close_locked(dev, vma);
D
Dave Airlie 已提交
442
	mutex_unlock(&dev->struct_mutex);
L
Linus Torvalds 已提交
443 444 445 446 447
}

/**
 * mmap DMA memory.
 *
448
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
449 450
 * \param vma virtual memory area.
 * \return zero on success or a negative number on failure.
D
Dave Airlie 已提交
451
 *
L
Linus Torvalds 已提交
452 453 454
 * Sets the virtual memory area operations structure to vm_dma_ops, the file
 * pointer, and calls vm_open().
 */
D
Dave Airlie 已提交
455
static int drm_mmap_dma(struct file *filp, struct vm_area_struct *vma)
L
Linus Torvalds 已提交
456
{
457 458
	struct drm_file *priv = filp->private_data;
	struct drm_device *dev;
459
	struct drm_device_dma *dma;
D
Dave Airlie 已提交
460
	unsigned long length = vma->vm_end - vma->vm_start;
L
Linus Torvalds 已提交
461

462
	dev = priv->minor->dev;
D
Dave Airlie 已提交
463
	dma = dev->dma;
464 465
	DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
		  vma->vm_start, vma->vm_end, vma->vm_pgoff);
L
Linus Torvalds 已提交
466

D
Dave Airlie 已提交
467
	/* Length must match exact page count */
L
Linus Torvalds 已提交
468 469 470 471
	if (!dma || (length >> PAGE_SHIFT) != dma->page_count) {
		return -EINVAL;
	}

472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
	if (!capable(CAP_SYS_ADMIN) &&
	    (dma->flags & _DRM_DMA_USE_PCI_RO)) {
		vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
#if defined(__i386__) || defined(__x86_64__)
		pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
#else
		/* Ye gads this is ugly.  With more thought
		   we could move this up higher and use
		   `protection_map' instead.  */
		vma->vm_page_prot =
		    __pgprot(pte_val
			     (pte_wrprotect
			      (__pte(pgprot_val(vma->vm_page_prot)))));
#endif
	}

D
Dave Airlie 已提交
488
	vma->vm_ops = &drm_vm_dma_ops;
L
Linus Torvalds 已提交
489

490
	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
L
Linus Torvalds 已提交
491

492
	drm_vm_open_locked(dev, vma);
L
Linus Torvalds 已提交
493 494 495
	return 0;
}

D
Daniel Vetter 已提交
496
static resource_size_t drm_core_get_reg_ofs(struct drm_device *dev)
L
Linus Torvalds 已提交
497 498
{
#ifdef __alpha__
499
	return dev->hose->dense_mem_base;
L
Linus Torvalds 已提交
500 501 502 503
#else
	return 0;
#endif
}
D
Dave Airlie 已提交
504

L
Linus Torvalds 已提交
505 506 507
/**
 * mmap DMA memory.
 *
508
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
509 510
 * \param vma virtual memory area.
 * \return zero on success or a negative number on failure.
D
Dave Airlie 已提交
511
 *
L
Linus Torvalds 已提交
512 513 514 515 516 517
 * If the virtual memory area has no offset associated with it then it's a DMA
 * area, so calls mmap_dma(). Otherwise searches the map in drm_device::maplist,
 * checks that the restricted flag is not set, sets the virtual memory operations
 * according to the mapping type and remaps the pages. Finally sets the file
 * pointer and calls vm_open().
 */
518
static int drm_mmap_locked(struct file *filp, struct vm_area_struct *vma)
L
Linus Torvalds 已提交
519
{
520
	struct drm_file *priv = filp->private_data;
521
	struct drm_device *dev = priv->minor->dev;
522
	struct drm_local_map *map = NULL;
523
	resource_size_t offset = 0;
524
	struct drm_hash_item *hash;
L
Linus Torvalds 已提交
525

526 527
	DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
		  vma->vm_start, vma->vm_end, vma->vm_pgoff);
L
Linus Torvalds 已提交
528

D
Dave Airlie 已提交
529 530
	if (!priv->authenticated)
		return -EACCES;
L
Linus Torvalds 已提交
531 532 533 534 535

	/* We check for "dma". On Apple's UniNorth, it's valid to have
	 * the AGP mapped at physical address 0
	 * --BenH.
	 */
536
	if (!vma->vm_pgoff
D
Daniel Vetter 已提交
537
#if IS_ENABLED(CONFIG_AGP)
D
Dave Airlie 已提交
538 539
	    && (!dev->agp
		|| dev->agp->agp_info.device->vendor != PCI_VENDOR_ID_APPLE)
L
Linus Torvalds 已提交
540 541 542 543
#endif
	    )
		return drm_mmap_dma(filp, vma);

544
	if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash)) {
545 546
		DRM_ERROR("Could not find map\n");
		return -EINVAL;
L
Linus Torvalds 已提交
547 548
	}

D
Dave Airlie 已提交
549
	map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
D
Dave Airlie 已提交
550
	if (!map || ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN)))
L
Linus Torvalds 已提交
551 552
		return -EPERM;

D
Dave Airlie 已提交
553
	/* Check for valid size. */
554
	if (map->size < vma->vm_end - vma->vm_start)
D
Dave Airlie 已提交
555
		return -EINVAL;
L
Linus Torvalds 已提交
556 557 558 559 560 561

	if (!capable(CAP_SYS_ADMIN) && (map->flags & _DRM_READ_ONLY)) {
		vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
#if defined(__i386__) || defined(__x86_64__)
		pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
#else
D
Dave Airlie 已提交
562 563 564 565 566 567 568
		/* Ye gads this is ugly.  With more thought
		   we could move this up higher and use
		   `protection_map' instead.  */
		vma->vm_page_prot =
		    __pgprot(pte_val
			     (pte_wrprotect
			      (__pte(pgprot_val(vma->vm_page_prot)))));
L
Linus Torvalds 已提交
569 570 571 572
#endif
	}

	switch (map->type) {
J
Jordan Crouse 已提交
573
#if !defined(__arm__)
D
Dave Airlie 已提交
574
	case _DRM_AGP:
D
Daniel Vetter 已提交
575
		if (dev->agp && dev->agp->cant_use_aperture) {
D
Dave Airlie 已提交
576 577 578
			/*
			 * On some platforms we can't talk to bus dma address from the CPU, so for
			 * memory of type DRM_AGP, we'll deal with sorting out the real physical
579
			 * pages and mappings in fault()
D
Dave Airlie 已提交
580
			 */
L
Linus Torvalds 已提交
581
#if defined(__powerpc__)
582
			vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
L
Linus Torvalds 已提交
583
#endif
D
Dave Airlie 已提交
584 585 586 587
			vma->vm_ops = &drm_vm_ops;
			break;
		}
		/* fall through to _DRM_FRAME_BUFFER... */
J
Jordan Crouse 已提交
588
#endif
L
Linus Torvalds 已提交
589 590
	case _DRM_FRAME_BUFFER:
	case _DRM_REGISTERS:
D
Daniel Vetter 已提交
591
		offset = drm_core_get_reg_ofs(dev);
592
		vma->vm_page_prot = drm_io_prot(map, vma);
593
		if (io_remap_pfn_range(vma, vma->vm_start,
D
Dave Airlie 已提交
594 595 596 597
				       (map->offset + offset) >> PAGE_SHIFT,
				       vma->vm_end - vma->vm_start,
				       vma->vm_page_prot))
			return -EAGAIN;
L
Linus Torvalds 已提交
598
		DRM_DEBUG("   Type = %d; start = 0x%lx, end = 0x%lx,"
599
			  " offset = 0x%llx\n",
L
Linus Torvalds 已提交
600
			  map->type,
601
			  vma->vm_start, vma->vm_end, (unsigned long long)(map->offset + offset));
J
Jordan Crouse 已提交
602

L
Linus Torvalds 已提交
603 604
		vma->vm_ops = &drm_vm_ops;
		break;
D
Dave Airlie 已提交
605
	case _DRM_CONSISTENT:
H
Hugh Dickins 已提交
606
		/* Consistent memory is really like shared memory. But
607
		 * it's allocated in a different way, so avoid fault */
H
Hugh Dickins 已提交
608 609 610 611
		if (remap_pfn_range(vma, vma->vm_start,
		    page_to_pfn(virt_to_page(map->handle)),
		    vma->vm_end - vma->vm_start, vma->vm_page_prot))
			return -EAGAIN;
612
		vma->vm_page_prot = drm_dma_prot(map->type, vma);
H
Hugh Dickins 已提交
613 614
	/* fall through to _DRM_SHM */
	case _DRM_SHM:
L
Linus Torvalds 已提交
615 616 617 618 619 620
		vma->vm_ops = &drm_vm_shm_ops;
		vma->vm_private_data = (void *)map;
		break;
	case _DRM_SCATTER_GATHER:
		vma->vm_ops = &drm_vm_sg_ops;
		vma->vm_private_data = (void *)map;
621
		vma->vm_page_prot = drm_dma_prot(map->type, vma);
D
Dave Airlie 已提交
622
		break;
L
Linus Torvalds 已提交
623 624 625
	default:
		return -EINVAL;	/* This should never happen. */
	}
626
	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
L
Linus Torvalds 已提交
627

628
	drm_vm_open_locked(dev, vma);
L
Linus Torvalds 已提交
629 630
	return 0;
}
D
Dave Airlie 已提交
631

632
int drm_legacy_mmap(struct file *filp, struct vm_area_struct *vma)
633
{
634
	struct drm_file *priv = filp->private_data;
635
	struct drm_device *dev = priv->minor->dev;
636 637
	int ret;

638 639 640
	if (drm_device_is_unplugged(dev))
		return -ENODEV;

641 642 643 644 645 646
	mutex_lock(&dev->struct_mutex);
	ret = drm_mmap_locked(filp, vma);
	mutex_unlock(&dev->struct_mutex);

	return ret;
}
647
EXPORT_SYMBOL(drm_legacy_mmap);
648 649 650 651 652 653 654 655 656 657 658

void drm_legacy_vma_flush(struct drm_device *dev)
{
	struct drm_vma_entry *vma, *vma_temp;

	/* Clear vma list (only needed for legacy drivers) */
	list_for_each_entry_safe(vma, vma_temp, &dev->vmalist, head) {
		list_del(&vma->head);
		kfree(vma);
	}
}