drm_vm.c 18.2 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 37 38 39 40
 * \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"
#if defined(__ia64__)
#include <linux/efi.h>
#endif

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

A
Adrian Bunk 已提交
44
static pgprot_t drm_io_prot(uint32_t map_type, struct vm_area_struct *vma)
45 46 47 48 49 50 51 52 53 54 55 56
{
	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;
57
#elif defined(__ia64__)
58 59 60 61 62
	if (efi_range_is_wc(vma->vm_start, vma->vm_end -
				    vma->vm_start))
		tmp = pgprot_writecombine(tmp);
	else
		tmp = pgprot_noncached(tmp);
63 64 65 66 67 68 69 70 71 72 73 74
#elif defined(__sparc__)
	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;
75 76 77 78
#endif
	return tmp;
}

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

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

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

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

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

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

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

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

D
Dave Airlie 已提交
140
		if (!agpmem)
141
			goto vm_fault_error;
L
Linus Torvalds 已提交
142 143

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

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

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

D
Dave Airlie 已提交
184
	if (!map)
185
		return VM_FAULT_SIGBUS;	/* Nothing allocated */
L
Linus Torvalds 已提交
186

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

195 196
	DRM_DEBUG("shm_fault 0x%lx\n", offset);
	return 0;
L
Linus Torvalds 已提交
197 198 199 200
}

/**
 * \c close method for shared virtual memory.
D
Dave Airlie 已提交
201
 *
L
Linus Torvalds 已提交
202
 * \param vma virtual memory area.
D
Dave Airlie 已提交
203
 *
L
Linus Torvalds 已提交
204 205 206
 * Deletes map information if we are the last
 * person to close a mapping and it's not in the global maplist.
 */
D
Dave Airlie 已提交
207
static void drm_vm_shm_close(struct vm_area_struct *vma)
L
Linus Torvalds 已提交
208
{
209
	struct drm_file *priv = vma->vm_file->private_data;
210
	struct drm_device *dev = priv->minor->dev;
211
	struct drm_vma_entry *pt, *temp;
212
	struct drm_map *map;
D
Dave Airlie 已提交
213
	struct drm_map_list *r_list;
L
Linus Torvalds 已提交
214 215 216 217 218 219 220 221
	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 已提交
222
	mutex_lock(&dev->struct_mutex);
223
	list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
D
Dave Airlie 已提交
224 225
		if (pt->vma->vm_private_data == map)
			found_maps++;
L
Linus Torvalds 已提交
226
		if (pt->vma == vma) {
227
			list_del(&pt->head);
L
Linus Torvalds 已提交
228 229 230
			drm_free(pt, sizeof(*pt), DRM_MEM_VMAS);
		}
	}
231

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

D
Dave Airlie 已提交
243
		if (!found_maps) {
244 245
			drm_dma_handle_t dmah;

L
Linus Torvalds 已提交
246 247 248 249 250 251 252 253 254 255
			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);
				}
256
				iounmap(map->handle);
L
Linus Torvalds 已提交
257 258 259 260 261 262 263
				break;
			case _DRM_SHM:
				vfree(map->handle);
				break;
			case _DRM_AGP:
			case _DRM_SCATTER_GATHER:
				break;
D
Dave Airlie 已提交
264
			case _DRM_CONSISTENT:
265 266 267 268
				dmah.vaddr = map->handle;
				dmah.busaddr = map->offset;
				dmah.size = map->size;
				__drm_pci_free(dev, &dmah);
D
Dave Airlie 已提交
269
				break;
L
Linus Torvalds 已提交
270 271 272 273
			}
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
		}
	}
D
Dave Airlie 已提交
274
	mutex_unlock(&dev->struct_mutex);
L
Linus Torvalds 已提交
275 276 277
}

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

	if (!dma)
296
		return VM_FAULT_SIGBUS;	/* Error */
D
Dave Airlie 已提交
297
	if (!dma->pagelist)
298
		return VM_FAULT_SIGBUS;	/* Nothing allocated */
D
Dave Airlie 已提交
299

300 301
	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 已提交
302
	page = virt_to_page((dma->pagelist[page_nr] + (offset & (~PAGE_MASK))));
L
Linus Torvalds 已提交
303 304

	get_page(page);
305
	vmf->page = page;
L
Linus Torvalds 已提交
306

307 308
	DRM_DEBUG("dma_fault 0x%lx (page %lu)\n", offset, page_nr);
	return 0;
L
Linus Torvalds 已提交
309 310 311
}

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

D
Dave Airlie 已提交
331
	if (!entry)
332
		return VM_FAULT_SIGBUS;	/* Error */
D
Dave Airlie 已提交
333
	if (!entry->pagelist)
334
		return VM_FAULT_SIGBUS;	/* Nothing allocated */
L
Linus Torvalds 已提交
335

336
	offset = (unsigned long)vmf->virtual_address - vma->vm_start;
337
	map_offset = map->offset - (unsigned long)dev->sg->virtual;
L
Linus Torvalds 已提交
338 339 340
	page_offset = (offset >> PAGE_SHIFT) + (map_offset >> PAGE_SHIFT);
	page = entry->pagelist[page_offset];
	get_page(page);
341
	vmf->page = page;
L
Linus Torvalds 已提交
342

343
	return 0;
L
Linus Torvalds 已提交
344 345
}

346
static int drm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
D
Dave Airlie 已提交
347
{
348
	return drm_do_vm_fault(vma, vmf);
L
Linus Torvalds 已提交
349 350
}

351
static int drm_vm_shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
D
Dave Airlie 已提交
352
{
353
	return drm_do_vm_shm_fault(vma, vmf);
L
Linus Torvalds 已提交
354 355
}

356
static int drm_vm_dma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
D
Dave Airlie 已提交
357
{
358
	return drm_do_vm_dma_fault(vma, vmf);
L
Linus Torvalds 已提交
359 360
}

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

/** AGP virtual memory operations */
D
Dave Airlie 已提交
367
static struct vm_operations_struct drm_vm_ops = {
368
	.fault = drm_vm_fault,
D
Dave Airlie 已提交
369 370
	.open = drm_vm_open,
	.close = drm_vm_close,
L
Linus Torvalds 已提交
371 372 373
};

/** Shared virtual memory operations */
D
Dave Airlie 已提交
374
static struct vm_operations_struct drm_vm_shm_ops = {
375
	.fault = drm_vm_shm_fault,
D
Dave Airlie 已提交
376 377
	.open = drm_vm_open,
	.close = drm_vm_shm_close,
L
Linus Torvalds 已提交
378 379 380
};

/** DMA virtual memory operations */
D
Dave Airlie 已提交
381
static struct vm_operations_struct drm_vm_dma_ops = {
382
	.fault = drm_vm_dma_fault,
D
Dave Airlie 已提交
383 384
	.open = drm_vm_open,
	.close = drm_vm_close,
L
Linus Torvalds 已提交
385 386 387
};

/** Scatter-gather virtual memory operations */
D
Dave Airlie 已提交
388
static struct vm_operations_struct drm_vm_sg_ops = {
389
	.fault = drm_vm_sg_fault,
D
Dave Airlie 已提交
390 391
	.open = drm_vm_open,
	.close = drm_vm_close,
L
Linus Torvalds 已提交
392 393 394 395
};

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

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

	vma_entry = drm_alloc(sizeof(*vma_entry), DRM_MEM_VMAS);
	if (vma_entry) {
D
Dave Airlie 已提交
414 415
		vma_entry->vma = vma;
		vma_entry->pid = current->pid;
416
		list_add(&vma_entry->head, &dev->vmalist);
L
Linus Torvalds 已提交
417 418 419
	}
}

420 421
static void drm_vm_open(struct vm_area_struct *vma)
{
422
	struct drm_file *priv = vma->vm_file->private_data;
423
	struct drm_device *dev = priv->minor->dev;
424 425 426 427 428 429

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

L
Linus Torvalds 已提交
430 431
/**
 * \c close method for all virtual memory types.
D
Dave Airlie 已提交
432
 *
L
Linus Torvalds 已提交
433
 * \param vma virtual memory area.
D
Dave Airlie 已提交
434
 *
L
Linus Torvalds 已提交
435 436 437
 * Search the \p vma private data entry in drm_device::vmalist, unlink it, and
 * free it.
 */
D
Dave Airlie 已提交
438
static void drm_vm_close(struct vm_area_struct *vma)
L
Linus Torvalds 已提交
439
{
440
	struct drm_file *priv = vma->vm_file->private_data;
441
	struct drm_device *dev = priv->minor->dev;
442
	struct drm_vma_entry *pt, *temp;
L
Linus Torvalds 已提交
443 444 445 446 447

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

D
Dave Airlie 已提交
448
	mutex_lock(&dev->struct_mutex);
449
	list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
L
Linus Torvalds 已提交
450
		if (pt->vma == vma) {
451
			list_del(&pt->head);
L
Linus Torvalds 已提交
452 453 454 455
			drm_free(pt, sizeof(*pt), DRM_MEM_VMAS);
			break;
		}
	}
D
Dave Airlie 已提交
456
	mutex_unlock(&dev->struct_mutex);
L
Linus Torvalds 已提交
457 458 459 460 461
}

/**
 * mmap DMA memory.
 *
462
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
463 464
 * \param vma virtual memory area.
 * \return zero on success or a negative number on failure.
D
Dave Airlie 已提交
465
 *
L
Linus Torvalds 已提交
466 467 468
 * Sets the virtual memory area operations structure to vm_dma_ops, the file
 * pointer, and calls vm_open().
 */
D
Dave Airlie 已提交
469
static int drm_mmap_dma(struct file *filp, struct vm_area_struct *vma)
L
Linus Torvalds 已提交
470
{
471 472
	struct drm_file *priv = filp->private_data;
	struct drm_device *dev;
473
	struct drm_device_dma *dma;
D
Dave Airlie 已提交
474
	unsigned long length = vma->vm_end - vma->vm_start;
L
Linus Torvalds 已提交
475

476
	dev = priv->minor->dev;
D
Dave Airlie 已提交
477
	dma = dev->dma;
478 479
	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 已提交
480

D
Dave Airlie 已提交
481
	/* Length must match exact page count */
L
Linus Torvalds 已提交
482 483 484 485
	if (!dma || (length >> PAGE_SHIFT) != dma->page_count) {
		return -EINVAL;
	}

486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
	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 已提交
502
	vma->vm_ops = &drm_vm_dma_ops;
L
Linus Torvalds 已提交
503

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

D
Dave Airlie 已提交
507
	vma->vm_file = filp;	/* Needed for drm_vm_open() */
508
	drm_vm_open_locked(vma);
L
Linus Torvalds 已提交
509 510 511
	return 0;
}

512
unsigned long drm_core_get_map_ofs(struct drm_map * map)
L
Linus Torvalds 已提交
513 514 515
{
	return map->offset;
}
D
Dave Airlie 已提交
516

L
Linus Torvalds 已提交
517 518 519 520 521 522 523 524 525 526
EXPORT_SYMBOL(drm_core_get_map_ofs);

unsigned long drm_core_get_reg_ofs(struct drm_device *dev)
{
#ifdef __alpha__
	return dev->hose->dense_mem_base - dev->hose->mem_space->start;
#else
	return 0;
#endif
}
D
Dave Airlie 已提交
527

L
Linus Torvalds 已提交
528 529 530 531 532
EXPORT_SYMBOL(drm_core_get_reg_ofs);

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

551 552
	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 已提交
553

D
Dave Airlie 已提交
554 555
	if (!priv->authenticated)
		return -EACCES;
L
Linus Torvalds 已提交
556 557 558 559 560

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

569
	if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash)) {
570 571
		DRM_ERROR("Could not find map\n");
		return -EINVAL;
L
Linus Torvalds 已提交
572 573
	}

D
Dave Airlie 已提交
574
	map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
D
Dave Airlie 已提交
575
	if (!map || ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN)))
L
Linus Torvalds 已提交
576 577
		return -EPERM;

D
Dave Airlie 已提交
578
	/* Check for valid size. */
579
	if (map->size < vma->vm_end - vma->vm_start)
D
Dave Airlie 已提交
580
		return -EINVAL;
L
Linus Torvalds 已提交
581 582 583 584 585 586

	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 已提交
587 588 589 590 591 592 593
		/* 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 已提交
594 595 596 597
#endif
	}

	switch (map->type) {
D
Dave Airlie 已提交
598 599 600 601 602
	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
603
			 * pages and mappings in fault()
D
Dave Airlie 已提交
604
			 */
L
Linus Torvalds 已提交
605
#if defined(__powerpc__)
D
Dave Airlie 已提交
606
			pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
L
Linus Torvalds 已提交
607
#endif
D
Dave Airlie 已提交
608 609 610 611
			vma->vm_ops = &drm_vm_ops;
			break;
		}
		/* fall through to _DRM_FRAME_BUFFER... */
L
Linus Torvalds 已提交
612 613 614
	case _DRM_FRAME_BUFFER:
	case _DRM_REGISTERS:
		offset = dev->driver->get_reg_ofs(dev);
615 616
		vma->vm_flags |= VM_IO;	/* not in core dump */
		vma->vm_page_prot = drm_io_prot(map->type, vma);
617
		if (io_remap_pfn_range(vma, vma->vm_start,
D
Dave Airlie 已提交
618 619 620 621
				       (map->offset + offset) >> PAGE_SHIFT,
				       vma->vm_end - vma->vm_start,
				       vma->vm_page_prot))
			return -EAGAIN;
L
Linus Torvalds 已提交
622 623 624
		DRM_DEBUG("   Type = %d; start = 0x%lx, end = 0x%lx,"
			  " offset = 0x%lx\n",
			  map->type,
625
			  vma->vm_start, vma->vm_end, map->offset + offset);
L
Linus Torvalds 已提交
626 627
		vma->vm_ops = &drm_vm_ops;
		break;
D
Dave Airlie 已提交
628
	case _DRM_CONSISTENT:
H
Hugh Dickins 已提交
629
		/* Consistent memory is really like shared memory. But
630
		 * it's allocated in a different way, so avoid fault */
H
Hugh Dickins 已提交
631 632 633 634
		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;
635
		vma->vm_page_prot = drm_dma_prot(map->type, vma);
H
Hugh Dickins 已提交
636 637
	/* fall through to _DRM_SHM */
	case _DRM_SHM:
L
Linus Torvalds 已提交
638 639
		vma->vm_ops = &drm_vm_shm_ops;
		vma->vm_private_data = (void *)map;
D
Dave Airlie 已提交
640 641
		/* Don't let this area swap.  Change when
		   DRM_KERNEL advisory is supported. */
L
Linus Torvalds 已提交
642 643 644 645 646 647
		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;
648
		vma->vm_page_prot = drm_dma_prot(map->type, vma);
D
Dave Airlie 已提交
649
		break;
L
Linus Torvalds 已提交
650 651 652
	default:
		return -EINVAL;	/* This should never happen. */
	}
D
Dave Airlie 已提交
653
	vma->vm_flags |= VM_RESERVED;	/* Don't swap */
654
	vma->vm_flags |= VM_DONTEXPAND;
L
Linus Torvalds 已提交
655

D
Dave Airlie 已提交
656
	vma->vm_file = filp;	/* Needed for drm_vm_open() */
657
	drm_vm_open_locked(vma);
L
Linus Torvalds 已提交
658 659
	return 0;
}
D
Dave Airlie 已提交
660

661 662
int drm_mmap(struct file *filp, struct vm_area_struct *vma)
{
663
	struct drm_file *priv = filp->private_data;
664
	struct drm_device *dev = priv->minor->dev;
665 666 667 668 669 670 671 672
	int ret;

	mutex_lock(&dev->struct_mutex);
	ret = drm_mmap_locked(filp, vma);
	mutex_unlock(&dev->struct_mutex);

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