drm_vm.c 18.5 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 36
 * \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.
 */

#include "drmP.h"
37
#include <linux/export.h>
L
Linus Torvalds 已提交
38 39
#if defined(__ia64__)
#include <linux/efi.h>
40
#include <linux/slab.h>
L
Linus Torvalds 已提交
41 42
#endif

D
Dave Airlie 已提交
43 44
static void drm_vm_open(struct vm_area_struct *vma);
static void drm_vm_close(struct vm_area_struct *vma);
L
Linus Torvalds 已提交
45

A
Adrian Bunk 已提交
46
static pgprot_t drm_io_prot(uint32_t map_type, struct vm_area_struct *vma)
47 48 49 50 51 52 53 54 55 56 57 58
{
	pgprot_t tmp = vm_get_page_prot(vma->vm_flags);

#if defined(__i386__) || defined(__x86_64__)
	if (boot_cpu_data.x86 > 3 && map_type != _DRM_AGP) {
		pgprot_val(tmp) |= _PAGE_PCD;
		pgprot_val(tmp) &= ~_PAGE_PWT;
	}
#elif defined(__powerpc__)
	pgprot_val(tmp) |= _PAGE_NO_CACHE;
	if (map_type == _DRM_REGISTERS)
		pgprot_val(tmp) |= _PAGE_GUARDED;
59
#elif defined(__ia64__)
60 61 62 63 64
	if (efi_range_is_wc(vma->vm_start, vma->vm_end -
				    vma->vm_start))
		tmp = pgprot_writecombine(tmp);
	else
		tmp = pgprot_noncached(tmp);
J
Jordan Crouse 已提交
65
#elif defined(__sparc__) || defined(__arm__)
66 67 68 69 70 71 72 73 74 75 76
	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)
	tmp |= _PAGE_NO_CACHE;
77 78 79 80
#endif
	return tmp;
}

L
Linus Torvalds 已提交
81
/**
82
 * \c fault method for AGP virtual memory.
L
Linus Torvalds 已提交
83 84 85 86
 *
 * \param vma virtual memory area.
 * \param address access address.
 * \return pointer to the page structure.
D
Dave Airlie 已提交
87
 *
L
Linus Torvalds 已提交
88 89 90 91
 * 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.
 */
#if __OS_HAS_AGP
92
static int drm_do_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
L
Linus Torvalds 已提交
93
{
94
	struct drm_file *priv = vma->vm_file->private_data;
95
	struct drm_device *dev = priv->minor->dev;
96
	struct drm_local_map *map = NULL;
D
Dave Airlie 已提交
97
	struct drm_map_list *r_list;
98
	struct drm_hash_item *hash;
L
Linus Torvalds 已提交
99 100

	/*
D
Dave Airlie 已提交
101 102
	 * Find the right map
	 */
L
Linus Torvalds 已提交
103
	if (!drm_core_has_AGP(dev))
104
		goto vm_fault_error;
L
Linus Torvalds 已提交
105

D
Dave Airlie 已提交
106
	if (!dev->agp || !dev->agp->cant_use_aperture)
107
		goto vm_fault_error;
L
Linus Torvalds 已提交
108

109
	if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash))
110
		goto vm_fault_error;
111

D
Dave Airlie 已提交
112
	r_list = drm_hash_entry(hash, struct drm_map_list, hash);
113
	map = r_list->map;
L
Linus Torvalds 已提交
114 115

	if (map && map->type == _DRM_AGP) {
116 117 118 119
		/*
		 * Using vm_pgoff as a selector forces us to use this unusual
		 * addressing scheme.
		 */
120 121 122
		resource_size_t offset = (unsigned long)vmf->virtual_address -
			vma->vm_start;
		resource_size_t baddr = map->offset + offset;
L
Linus Torvalds 已提交
123 124 125 126 127
		struct drm_agp_mem *agpmem;
		struct page *page;

#ifdef __alpha__
		/*
D
Dave Airlie 已提交
128 129
		 * Adjust to a bus-relative address
		 */
L
Linus Torvalds 已提交
130 131 132 133
		baddr -= dev->hose->mem_space->start;
#endif

		/*
D
Dave Airlie 已提交
134 135
		 * It's AGP memory - find the real physical page to map
		 */
136
		list_for_each_entry(agpmem, &dev->agp->memory, head) {
L
Linus Torvalds 已提交
137
			if (agpmem->bound <= baddr &&
D
Dave Airlie 已提交
138
			    agpmem->bound + agpmem->pages * PAGE_SIZE > baddr)
L
Linus Torvalds 已提交
139 140 141
				break;
		}

D
Dan Carpenter 已提交
142
		if (&agpmem->head == &dev->agp->memory)
143
			goto vm_fault_error;
L
Linus Torvalds 已提交
144 145

		/*
D
Dave Airlie 已提交
146 147
		 * Get the page, inc the use count, and return it
		 */
L
Linus Torvalds 已提交
148
		offset = (baddr - agpmem->bound) >> PAGE_SHIFT;
149
		page = agpmem->memory->pages[offset];
L
Linus Torvalds 已提交
150
		get_page(page);
151
		vmf->page = page;
L
Linus Torvalds 已提交
152

D
Dave Airlie 已提交
153
		DRM_DEBUG
154 155
		    ("baddr = 0x%llx page = 0x%p, offset = 0x%llx, count=%d\n",
		     (unsigned long long)baddr,
156
		     agpmem->memory->pages[offset],
157
		     (unsigned long long)offset,
D
Dave Airlie 已提交
158
		     page_count(page));
159
		return 0;
D
Dave Airlie 已提交
160
	}
161 162
vm_fault_error:
	return VM_FAULT_SIGBUS;	/* Disallow mremap */
L
Linus Torvalds 已提交
163
}
D
Dave Airlie 已提交
164
#else				/* __OS_HAS_AGP */
165
static int drm_do_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
L
Linus Torvalds 已提交
166
{
167
	return VM_FAULT_SIGBUS;
L
Linus Torvalds 已提交
168
}
D
Dave Airlie 已提交
169
#endif				/* __OS_HAS_AGP */
L
Linus Torvalds 已提交
170 171 172 173 174 175 176

/**
 * \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 已提交
177
 *
178
 * Get the mapping, find the real physical page to map, get the page, and
L
Linus Torvalds 已提交
179 180
 * return it.
 */
181
static int drm_do_vm_shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
L
Linus Torvalds 已提交
182
{
183
	struct drm_local_map *map = vma->vm_private_data;
D
Dave Airlie 已提交
184 185 186
	unsigned long offset;
	unsigned long i;
	struct page *page;
L
Linus Torvalds 已提交
187

D
Dave Airlie 已提交
188
	if (!map)
189
		return VM_FAULT_SIGBUS;	/* Nothing allocated */
L
Linus Torvalds 已提交
190

191
	offset = (unsigned long)vmf->virtual_address - vma->vm_start;
L
Linus Torvalds 已提交
192
	i = (unsigned long)map->handle + offset;
H
Hugh Dickins 已提交
193
	page = vmalloc_to_page((void *)i);
L
Linus Torvalds 已提交
194
	if (!page)
195
		return VM_FAULT_SIGBUS;
L
Linus Torvalds 已提交
196
	get_page(page);
197
	vmf->page = page;
L
Linus Torvalds 已提交
198

199 200
	DRM_DEBUG("shm_fault 0x%lx\n", offset);
	return 0;
L
Linus Torvalds 已提交
201 202 203 204
}

/**
 * \c close method for shared virtual memory.
D
Dave Airlie 已提交
205
 *
L
Linus Torvalds 已提交
206
 * \param vma virtual memory area.
D
Dave Airlie 已提交
207
 *
L
Linus Torvalds 已提交
208 209 210
 * Deletes map information if we are the last
 * person to close a mapping and it's not in the global maplist.
 */
D
Dave Airlie 已提交
211
static void drm_vm_shm_close(struct vm_area_struct *vma)
L
Linus Torvalds 已提交
212
{
213
	struct drm_file *priv = vma->vm_file->private_data;
214
	struct drm_device *dev = priv->minor->dev;
215
	struct drm_vma_entry *pt, *temp;
216
	struct drm_local_map *map;
D
Dave Airlie 已提交
217
	struct drm_map_list *r_list;
L
Linus Torvalds 已提交
218 219 220 221 222 223 224 225
	int found_maps = 0;

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

	map = vma->vm_private_data;

D
Dave Airlie 已提交
226
	mutex_lock(&dev->struct_mutex);
227
	list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
D
Dave Airlie 已提交
228 229
		if (pt->vma->vm_private_data == map)
			found_maps++;
L
Linus Torvalds 已提交
230
		if (pt->vma == vma) {
231
			list_del(&pt->head);
232
			kfree(pt);
L
Linus Torvalds 已提交
233 234
		}
	}
235

L
Linus Torvalds 已提交
236
	/* We were the only map that was found */
D
Dave Airlie 已提交
237
	if (found_maps == 1 && map->flags & _DRM_REMOVABLE) {
L
Linus Torvalds 已提交
238 239 240 241
		/* Check to see if we are in the maplist, if we are not, then
		 * we delete this mappings information.
		 */
		found_maps = 0;
242
		list_for_each_entry(r_list, &dev->maplist, head) {
D
Dave Airlie 已提交
243 244
			if (r_list->map == map)
				found_maps++;
L
Linus Torvalds 已提交
245 246
		}

D
Dave Airlie 已提交
247
		if (!found_maps) {
248 249
			drm_dma_handle_t dmah;

L
Linus Torvalds 已提交
250 251 252 253 254 255 256 257 258 259
			switch (map->type) {
			case _DRM_REGISTERS:
			case _DRM_FRAME_BUFFER:
				if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
					int retcode;
					retcode = mtrr_del(map->mtrr,
							   map->offset,
							   map->size);
					DRM_DEBUG("mtrr_del = %d\n", retcode);
				}
260
				iounmap(map->handle);
L
Linus Torvalds 已提交
261 262 263 264 265 266 267
				break;
			case _DRM_SHM:
				vfree(map->handle);
				break;
			case _DRM_AGP:
			case _DRM_SCATTER_GATHER:
				break;
D
Dave Airlie 已提交
268
			case _DRM_CONSISTENT:
269 270 271 272
				dmah.vaddr = map->handle;
				dmah.busaddr = map->offset;
				dmah.size = map->size;
				__drm_pci_free(dev, &dmah);
D
Dave Airlie 已提交
273
				break;
J
Jesse Barnes 已提交
274 275 276
			case _DRM_GEM:
				DRM_ERROR("tried to rmmap GEM object\n");
				break;
L
Linus Torvalds 已提交
277
			}
278
			kfree(map);
L
Linus Torvalds 已提交
279 280
		}
	}
D
Dave Airlie 已提交
281
	mutex_unlock(&dev->struct_mutex);
L
Linus Torvalds 已提交
282 283 284
}

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

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

307 308
	offset = (unsigned long)vmf->virtual_address - vma->vm_start;	/* vm_[pg]off[set] should be 0 */
	page_nr = offset >> PAGE_SHIFT; /* page_nr could just be vmf->pgoff */
D
Dave Airlie 已提交
309
	page = virt_to_page((dma->pagelist[page_nr] + (offset & (~PAGE_MASK))));
L
Linus Torvalds 已提交
310 311

	get_page(page);
312
	vmf->page = page;
L
Linus Torvalds 已提交
313

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

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

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

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

350
	return 0;
L
Linus Torvalds 已提交
351 352
}

353
static int drm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
D
Dave Airlie 已提交
354
{
355
	return drm_do_vm_fault(vma, vmf);
L
Linus Torvalds 已提交
356 357
}

358
static int drm_vm_shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
D
Dave Airlie 已提交
359
{
360
	return drm_do_vm_shm_fault(vma, vmf);
L
Linus Torvalds 已提交
361 362
}

363
static int drm_vm_dma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
D
Dave Airlie 已提交
364
{
365
	return drm_do_vm_dma_fault(vma, vmf);
L
Linus Torvalds 已提交
366 367
}

368
static int drm_vm_sg_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
D
Dave Airlie 已提交
369
{
370
	return drm_do_vm_sg_fault(vma, vmf);
L
Linus Torvalds 已提交
371 372 373
}

/** AGP virtual memory operations */
374
static const struct vm_operations_struct drm_vm_ops = {
375
	.fault = drm_vm_fault,
D
Dave Airlie 已提交
376 377
	.open = drm_vm_open,
	.close = drm_vm_close,
L
Linus Torvalds 已提交
378 379 380
};

/** Shared virtual memory operations */
381
static const struct vm_operations_struct drm_vm_shm_ops = {
382
	.fault = drm_vm_shm_fault,
D
Dave Airlie 已提交
383 384
	.open = drm_vm_open,
	.close = drm_vm_shm_close,
L
Linus Torvalds 已提交
385 386 387
};

/** DMA virtual memory operations */
388
static const struct vm_operations_struct drm_vm_dma_ops = {
389
	.fault = drm_vm_dma_fault,
D
Dave Airlie 已提交
390 391
	.open = drm_vm_open,
	.close = drm_vm_close,
L
Linus Torvalds 已提交
392 393 394
};

/** Scatter-gather virtual memory operations */
395
static const struct vm_operations_struct drm_vm_sg_ops = {
396
	.fault = drm_vm_sg_fault,
D
Dave Airlie 已提交
397 398
	.open = drm_vm_open,
	.close = drm_vm_close,
L
Linus Torvalds 已提交
399 400 401 402
};

/**
 * \c open method for shared virtual memory.
D
Dave Airlie 已提交
403
 *
L
Linus Torvalds 已提交
404
 * \param vma virtual memory area.
D
Dave Airlie 已提交
405
 *
L
Linus Torvalds 已提交
406 407 408
 * Create a new drm_vma_entry structure as the \p vma private data entry and
 * add it to drm_device::vmalist.
 */
J
Jesse Barnes 已提交
409
void drm_vm_open_locked(struct vm_area_struct *vma)
L
Linus Torvalds 已提交
410
{
411
	struct drm_file *priv = vma->vm_file->private_data;
412
	struct drm_device *dev = priv->minor->dev;
413
	struct drm_vma_entry *vma_entry;
L
Linus Torvalds 已提交
414 415 416 417 418

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

419
	vma_entry = kmalloc(sizeof(*vma_entry), GFP_KERNEL);
L
Linus Torvalds 已提交
420
	if (vma_entry) {
D
Dave Airlie 已提交
421 422
		vma_entry->vma = vma;
		vma_entry->pid = current->pid;
423
		list_add(&vma_entry->head, &dev->vmalist);
L
Linus Torvalds 已提交
424 425 426
	}
}

427 428
static void drm_vm_open(struct vm_area_struct *vma)
{
429
	struct drm_file *priv = vma->vm_file->private_data;
430
	struct drm_device *dev = priv->minor->dev;
431 432 433 434 435 436

	mutex_lock(&dev->struct_mutex);
	drm_vm_open_locked(vma);
	mutex_unlock(&dev->struct_mutex);
}

C
Chris Wilson 已提交
437
void drm_vm_close_locked(struct vm_area_struct *vma)
L
Linus Torvalds 已提交
438
{
439
	struct drm_file *priv = vma->vm_file->private_data;
440
	struct drm_device *dev = priv->minor->dev;
441
	struct drm_vma_entry *pt, *temp;
L
Linus Torvalds 已提交
442 443 444 445 446

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

447
	list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
L
Linus Torvalds 已提交
448
		if (pt->vma == vma) {
449
			list_del(&pt->head);
450
			kfree(pt);
L
Linus Torvalds 已提交
451 452 453
			break;
		}
	}
C
Chris Wilson 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
}

/**
 * \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);
	drm_vm_close_locked(vma);
D
Dave Airlie 已提交
471
	mutex_unlock(&dev->struct_mutex);
L
Linus Torvalds 已提交
472 473 474 475 476
}

/**
 * mmap DMA memory.
 *
477
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
478 479
 * \param vma virtual memory area.
 * \return zero on success or a negative number on failure.
D
Dave Airlie 已提交
480
 *
L
Linus Torvalds 已提交
481 482 483
 * Sets the virtual memory area operations structure to vm_dma_ops, the file
 * pointer, and calls vm_open().
 */
D
Dave Airlie 已提交
484
static int drm_mmap_dma(struct file *filp, struct vm_area_struct *vma)
L
Linus Torvalds 已提交
485
{
486 487
	struct drm_file *priv = filp->private_data;
	struct drm_device *dev;
488
	struct drm_device_dma *dma;
D
Dave Airlie 已提交
489
	unsigned long length = vma->vm_end - vma->vm_start;
L
Linus Torvalds 已提交
490

491
	dev = priv->minor->dev;
D
Dave Airlie 已提交
492
	dma = dev->dma;
493 494
	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 已提交
495

D
Dave Airlie 已提交
496
	/* Length must match exact page count */
L
Linus Torvalds 已提交
497 498 499 500
	if (!dma || (length >> PAGE_SHIFT) != dma->page_count) {
		return -EINVAL;
	}

501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
	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 已提交
517
	vma->vm_ops = &drm_vm_dma_ops;
L
Linus Torvalds 已提交
518

D
Dave Airlie 已提交
519
	vma->vm_flags |= VM_RESERVED;	/* Don't swap */
520
	vma->vm_flags |= VM_DONTEXPAND;
L
Linus Torvalds 已提交
521

522
	drm_vm_open_locked(vma);
L
Linus Torvalds 已提交
523 524 525
	return 0;
}

D
Daniel Vetter 已提交
526
static resource_size_t drm_core_get_reg_ofs(struct drm_device *dev)
L
Linus Torvalds 已提交
527 528
{
#ifdef __alpha__
529
	return dev->hose->dense_mem_base;
L
Linus Torvalds 已提交
530 531 532 533
#else
	return 0;
#endif
}
D
Dave Airlie 已提交
534

L
Linus Torvalds 已提交
535 536 537
/**
 * mmap DMA memory.
 *
538
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
539 540
 * \param vma virtual memory area.
 * \return zero on success or a negative number on failure.
D
Dave Airlie 已提交
541
 *
L
Linus Torvalds 已提交
542 543 544 545 546 547
 * 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().
 */
J
Jesse Barnes 已提交
548
int drm_mmap_locked(struct file *filp, struct vm_area_struct *vma)
L
Linus Torvalds 已提交
549
{
550
	struct drm_file *priv = filp->private_data;
551
	struct drm_device *dev = priv->minor->dev;
552
	struct drm_local_map *map = NULL;
553
	resource_size_t offset = 0;
554
	struct drm_hash_item *hash;
L
Linus Torvalds 已提交
555

556 557
	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 已提交
558

D
Dave Airlie 已提交
559 560
	if (!priv->authenticated)
		return -EACCES;
L
Linus Torvalds 已提交
561 562 563 564 565

	/* We check for "dma". On Apple's UniNorth, it's valid to have
	 * the AGP mapped at physical address 0
	 * --BenH.
	 */
566
	if (!vma->vm_pgoff
L
Linus Torvalds 已提交
567
#if __OS_HAS_AGP
D
Dave Airlie 已提交
568 569
	    && (!dev->agp
		|| dev->agp->agp_info.device->vendor != PCI_VENDOR_ID_APPLE)
L
Linus Torvalds 已提交
570 571 572 573
#endif
	    )
		return drm_mmap_dma(filp, vma);

574
	if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash)) {
575 576
		DRM_ERROR("Could not find map\n");
		return -EINVAL;
L
Linus Torvalds 已提交
577 578
	}

D
Dave Airlie 已提交
579
	map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
D
Dave Airlie 已提交
580
	if (!map || ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN)))
L
Linus Torvalds 已提交
581 582
		return -EPERM;

D
Dave Airlie 已提交
583
	/* Check for valid size. */
584
	if (map->size < vma->vm_end - vma->vm_start)
D
Dave Airlie 已提交
585
		return -EINVAL;
L
Linus Torvalds 已提交
586 587 588 589 590 591

	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 已提交
592 593 594 595 596 597 598
		/* 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 已提交
599 600 601 602
#endif
	}

	switch (map->type) {
J
Jordan Crouse 已提交
603
#if !defined(__arm__)
D
Dave Airlie 已提交
604 605 606 607 608
	case _DRM_AGP:
		if (drm_core_has_AGP(dev) && dev->agp->cant_use_aperture) {
			/*
			 * 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
609
			 * pages and mappings in fault()
D
Dave Airlie 已提交
610
			 */
L
Linus Torvalds 已提交
611
#if defined(__powerpc__)
D
Dave Airlie 已提交
612
			pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
L
Linus Torvalds 已提交
613
#endif
D
Dave Airlie 已提交
614 615 616 617
			vma->vm_ops = &drm_vm_ops;
			break;
		}
		/* fall through to _DRM_FRAME_BUFFER... */
J
Jordan Crouse 已提交
618
#endif
L
Linus Torvalds 已提交
619 620
	case _DRM_FRAME_BUFFER:
	case _DRM_REGISTERS:
D
Daniel Vetter 已提交
621
		offset = drm_core_get_reg_ofs(dev);
622 623
		vma->vm_flags |= VM_IO;	/* not in core dump */
		vma->vm_page_prot = drm_io_prot(map->type, vma);
J
Jordan Crouse 已提交
624
#if !defined(__arm__)
625
		if (io_remap_pfn_range(vma, vma->vm_start,
D
Dave Airlie 已提交
626 627 628 629
				       (map->offset + offset) >> PAGE_SHIFT,
				       vma->vm_end - vma->vm_start,
				       vma->vm_page_prot))
			return -EAGAIN;
J
Jordan Crouse 已提交
630 631 632 633 634 635 636 637
#else
		if (remap_pfn_range(vma, vma->vm_start,
					(map->offset + offset) >> PAGE_SHIFT,
					vma->vm_end - vma->vm_start,
					vma->vm_page_prot))
			return -EAGAIN;
#endif

L
Linus Torvalds 已提交
638
		DRM_DEBUG("   Type = %d; start = 0x%lx, end = 0x%lx,"
639
			  " offset = 0x%llx\n",
L
Linus Torvalds 已提交
640
			  map->type,
641
			  vma->vm_start, vma->vm_end, (unsigned long long)(map->offset + offset));
J
Jordan Crouse 已提交
642

L
Linus Torvalds 已提交
643 644
		vma->vm_ops = &drm_vm_ops;
		break;
D
Dave Airlie 已提交
645
	case _DRM_CONSISTENT:
H
Hugh Dickins 已提交
646
		/* Consistent memory is really like shared memory. But
647
		 * it's allocated in a different way, so avoid fault */
H
Hugh Dickins 已提交
648 649 650 651
		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;
652
		vma->vm_page_prot = drm_dma_prot(map->type, vma);
H
Hugh Dickins 已提交
653 654
	/* fall through to _DRM_SHM */
	case _DRM_SHM:
L
Linus Torvalds 已提交
655 656
		vma->vm_ops = &drm_vm_shm_ops;
		vma->vm_private_data = (void *)map;
D
Dave Airlie 已提交
657 658
		/* Don't let this area swap.  Change when
		   DRM_KERNEL advisory is supported. */
L
Linus Torvalds 已提交
659 660 661 662 663 664
		vma->vm_flags |= VM_RESERVED;
		break;
	case _DRM_SCATTER_GATHER:
		vma->vm_ops = &drm_vm_sg_ops;
		vma->vm_private_data = (void *)map;
		vma->vm_flags |= VM_RESERVED;
665
		vma->vm_page_prot = drm_dma_prot(map->type, vma);
D
Dave Airlie 已提交
666
		break;
L
Linus Torvalds 已提交
667 668 669
	default:
		return -EINVAL;	/* This should never happen. */
	}
D
Dave Airlie 已提交
670
	vma->vm_flags |= VM_RESERVED;	/* Don't swap */
671
	vma->vm_flags |= VM_DONTEXPAND;
L
Linus Torvalds 已提交
672

673
	drm_vm_open_locked(vma);
L
Linus Torvalds 已提交
674 675
	return 0;
}
D
Dave Airlie 已提交
676

677 678
int drm_mmap(struct file *filp, struct vm_area_struct *vma)
{
679
	struct drm_file *priv = filp->private_data;
680
	struct drm_device *dev = priv->minor->dev;
681 682
	int ret;

683 684 685
	if (drm_device_is_unplugged(dev))
		return -ENODEV;

686 687 688 689 690 691
	mutex_lock(&dev->struct_mutex);
	ret = drm_mmap_locked(filp, vma);
	mutex_unlock(&dev->struct_mutex);

	return ret;
}
L
Linus Torvalds 已提交
692
EXPORT_SYMBOL(drm_mmap);