drm_vm.c 17.8 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>
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

46 47
static pgprot_t drm_io_prot(struct drm_local_map *map,
			    struct vm_area_struct *vma)
48 49 50 51
{
	pgprot_t tmp = vm_get_page_prot(vma->vm_flags);

#if defined(__i386__) || defined(__x86_64__)
52 53 54 55
	if (map->type == _DRM_REGISTERS && !(map->flags & _DRM_WRITE_COMBINING))
		tmp = pgprot_noncached(tmp);
	else
		tmp = pgprot_writecombine(tmp);
56 57
#elif defined(__powerpc__)
	pgprot_val(tmp) |= _PAGE_NO_CACHE;
58
	if (map->type == _DRM_REGISTERS)
59
		pgprot_val(tmp) |= _PAGE_GUARDED;
60
#elif defined(__ia64__)
61 62 63 64 65
	if (efi_range_is_wc(vma->vm_start, vma->vm_end -
				    vma->vm_start))
		tmp = pgprot_writecombine(tmp);
	else
		tmp = pgprot_noncached(tmp);
66
#elif defined(__sparc__) || defined(__arm__) || defined(__mips__)
67 68 69 70 71 72 73 74 75 76 77
	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;
78 79 80 81
#endif
	return tmp;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

L
Linus Torvalds 已提交
251 252 253
			switch (map->type) {
			case _DRM_REGISTERS:
			case _DRM_FRAME_BUFFER:
254
				arch_phys_wc_del(map->mtrr);
255
				iounmap(map->handle);
L
Linus Torvalds 已提交
256 257 258 259 260 261 262
				break;
			case _DRM_SHM:
				vfree(map->handle);
				break;
			case _DRM_AGP:
			case _DRM_SCATTER_GATHER:
				break;
D
Dave Airlie 已提交
263
			case _DRM_CONSISTENT:
264 265 266 267
				dmah.vaddr = map->handle;
				dmah.busaddr = map->offset;
				dmah.size = map->size;
				__drm_pci_free(dev, &dmah);
D
Dave Airlie 已提交
268
				break;
L
Linus Torvalds 已提交
269
			}
270
			kfree(map);
L
Linus Torvalds 已提交
271 272
		}
	}
D
Dave Airlie 已提交
273
	mutex_unlock(&dev->struct_mutex);
L
Linus Torvalds 已提交
274 275 276
}

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

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

299 300
	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 */
301
	page = virt_to_page((void *)dma->pagelist[page_nr]);
L
Linus Torvalds 已提交
302 303

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

410
	vma_entry = kmalloc(sizeof(*vma_entry), GFP_KERNEL);
L
Linus Torvalds 已提交
411
	if (vma_entry) {
D
Dave Airlie 已提交
412 413
		vma_entry->vma = vma;
		vma_entry->pid = current->pid;
414
		list_add(&vma_entry->head, &dev->vmalist);
L
Linus Torvalds 已提交
415 416
	}
}
A
Arnd Bergmann 已提交
417
EXPORT_SYMBOL_GPL(drm_vm_open_locked);
L
Linus Torvalds 已提交
418

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

	mutex_lock(&dev->struct_mutex);
425
	drm_vm_open_locked(dev, vma);
426 427 428
	mutex_unlock(&dev->struct_mutex);
}

429 430
void drm_vm_close_locked(struct drm_device *dev,
		struct vm_area_struct *vma)
L
Linus Torvalds 已提交
431
{
432
	struct drm_vma_entry *pt, *temp;
L
Linus Torvalds 已提交
433 434 435 436 437

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

438
	list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
L
Linus Torvalds 已提交
439
		if (pt->vma == vma) {
440
			list_del(&pt->head);
441
			kfree(pt);
L
Linus Torvalds 已提交
442 443 444
			break;
		}
	}
C
Chris Wilson 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
}

/**
 * \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);
461
	drm_vm_close_locked(dev, vma);
D
Dave Airlie 已提交
462
	mutex_unlock(&dev->struct_mutex);
L
Linus Torvalds 已提交
463 464 465 466 467
}

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

482
	dev = priv->minor->dev;
D
Dave Airlie 已提交
483
	dma = dev->dma;
484 485
	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 已提交
486

D
Dave Airlie 已提交
487
	/* Length must match exact page count */
L
Linus Torvalds 已提交
488 489 490 491
	if (!dma || (length >> PAGE_SHIFT) != dma->page_count) {
		return -EINVAL;
	}

492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
	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 已提交
508
	vma->vm_ops = &drm_vm_dma_ops;
L
Linus Torvalds 已提交
509

510
	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
L
Linus Torvalds 已提交
511

512
	drm_vm_open_locked(dev, vma);
L
Linus Torvalds 已提交
513 514 515
	return 0;
}

D
Daniel Vetter 已提交
516
static resource_size_t drm_core_get_reg_ofs(struct drm_device *dev)
L
Linus Torvalds 已提交
517 518
{
#ifdef __alpha__
519
	return dev->hose->dense_mem_base;
L
Linus Torvalds 已提交
520 521 522 523
#else
	return 0;
#endif
}
D
Dave Airlie 已提交
524

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

546 547
	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 已提交
548

D
Dave Airlie 已提交
549 550
	if (!priv->authenticated)
		return -EACCES;
L
Linus Torvalds 已提交
551 552 553 554 555

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

564
	if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash)) {
565 566
		DRM_ERROR("Could not find map\n");
		return -EINVAL;
L
Linus Torvalds 已提交
567 568
	}

D
Dave Airlie 已提交
569
	map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
D
Dave Airlie 已提交
570
	if (!map || ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN)))
L
Linus Torvalds 已提交
571 572
		return -EPERM;

D
Dave Airlie 已提交
573
	/* Check for valid size. */
574
	if (map->size < vma->vm_end - vma->vm_start)
D
Dave Airlie 已提交
575
		return -EINVAL;
L
Linus Torvalds 已提交
576 577 578 579 580 581

	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 已提交
582 583 584 585 586 587 588
		/* 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 已提交
589 590 591 592
#endif
	}

	switch (map->type) {
J
Jordan Crouse 已提交
593
#if !defined(__arm__)
D
Dave Airlie 已提交
594 595 596 597 598
	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
599
			 * pages and mappings in fault()
D
Dave Airlie 已提交
600
			 */
L
Linus Torvalds 已提交
601
#if defined(__powerpc__)
D
Dave Airlie 已提交
602
			pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
L
Linus Torvalds 已提交
603
#endif
D
Dave Airlie 已提交
604 605 606 607
			vma->vm_ops = &drm_vm_ops;
			break;
		}
		/* fall through to _DRM_FRAME_BUFFER... */
J
Jordan Crouse 已提交
608
#endif
L
Linus Torvalds 已提交
609 610
	case _DRM_FRAME_BUFFER:
	case _DRM_REGISTERS:
D
Daniel Vetter 已提交
611
		offset = drm_core_get_reg_ofs(dev);
612
		vma->vm_page_prot = drm_io_prot(map, vma);
613
		if (io_remap_pfn_range(vma, vma->vm_start,
D
Dave Airlie 已提交
614 615 616 617
				       (map->offset + offset) >> PAGE_SHIFT,
				       vma->vm_end - vma->vm_start,
				       vma->vm_page_prot))
			return -EAGAIN;
L
Linus Torvalds 已提交
618
		DRM_DEBUG("   Type = %d; start = 0x%lx, end = 0x%lx,"
619
			  " offset = 0x%llx\n",
L
Linus Torvalds 已提交
620
			  map->type,
621
			  vma->vm_start, vma->vm_end, (unsigned long long)(map->offset + offset));
J
Jordan Crouse 已提交
622

L
Linus Torvalds 已提交
623 624
		vma->vm_ops = &drm_vm_ops;
		break;
D
Dave Airlie 已提交
625
	case _DRM_CONSISTENT:
H
Hugh Dickins 已提交
626
		/* Consistent memory is really like shared memory. But
627
		 * it's allocated in a different way, so avoid fault */
H
Hugh Dickins 已提交
628 629 630 631
		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;
632
		vma->vm_page_prot = drm_dma_prot(map->type, vma);
H
Hugh Dickins 已提交
633 634
	/* fall through to _DRM_SHM */
	case _DRM_SHM:
L
Linus Torvalds 已提交
635 636 637 638 639 640
		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;
641
		vma->vm_page_prot = drm_dma_prot(map->type, vma);
D
Dave Airlie 已提交
642
		break;
L
Linus Torvalds 已提交
643 644 645
	default:
		return -EINVAL;	/* This should never happen. */
	}
646
	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
L
Linus Torvalds 已提交
647

648
	drm_vm_open_locked(dev, vma);
L
Linus Torvalds 已提交
649 650
	return 0;
}
D
Dave Airlie 已提交
651

652 653
int drm_mmap(struct file *filp, struct vm_area_struct *vma)
{
654
	struct drm_file *priv = filp->private_data;
655
	struct drm_device *dev = priv->minor->dev;
656 657
	int ret;

658 659 660
	if (drm_device_is_unplugged(dev))
		return -ENODEV;

661 662 663 664 665 666
	mutex_lock(&dev->struct_mutex);
	ret = drm_mmap_locked(filp, vma);
	mutex_unlock(&dev->struct_mutex);

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