2003-05-26 18:37:33 -06:00
|
|
|
/**
|
2004-10-18 08:16:41 -06:00
|
|
|
* \file drm_vm.c
|
2003-05-26 18:37:33 -06:00
|
|
|
* Memory mapping for DRM
|
2004-09-30 15:12:10 -06:00
|
|
|
*
|
2003-05-26 18:37:33 -06:00
|
|
|
* \author Rickard E. (Rik) Faith <faith@valinux.com>
|
|
|
|
* \author Gareth Hughes <gareth@valinux.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2001-02-15 01:12:14 -07:00
|
|
|
* Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
|
1999-12-05 16:10:37 -07:00
|
|
|
*
|
2000-06-08 08:38:22 -06:00
|
|
|
* Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
|
|
|
|
* Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
|
1999-12-05 16:10:37 -07:00
|
|
|
* 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:
|
2001-02-15 01:12:14 -07:00
|
|
|
*
|
1999-12-05 16:10:37 -07:00
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
2001-02-15 01:12:14 -07:00
|
|
|
*
|
1999-12-05 16:10:37 -07:00
|
|
|
* 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
|
2001-02-15 01:12:14 -07:00
|
|
|
* VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
1999-12-05 16:10:37 -07:00
|
|
|
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
2001-02-15 01:12:14 -07:00
|
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
1999-12-05 16:10:37 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "drmP.h"
|
2006-08-22 01:47:33 -06:00
|
|
|
|
2005-03-26 23:58:12 -07:00
|
|
|
#if defined(__ia64__)
|
|
|
|
#include <linux/efi.h>
|
|
|
|
#endif
|
1999-12-05 16:10:37 -07:00
|
|
|
|
2005-06-04 00:18:11 -06:00
|
|
|
static void drm_vm_open(struct vm_area_struct *vma);
|
2006-02-17 19:53:36 -07:00
|
|
|
static void drm_vm_close(struct vm_area_struct *vma);
|
2007-02-02 06:47:44 -07:00
|
|
|
static int drm_bo_mmap_locked(struct vm_area_struct *vma,
|
|
|
|
struct file *filp,
|
|
|
|
drm_local_map_t *map);
|
2006-08-22 01:47:33 -06:00
|
|
|
|
|
|
|
|
|
|
|
pgprot_t drm_io_prot(uint32_t map_type, struct vm_area_struct *vma)
|
|
|
|
{
|
2006-08-25 10:14:22 -06:00
|
|
|
pgprot_t tmp = vm_get_page_prot(vma->vm_flags);
|
2006-08-22 01:47:33 -06:00
|
|
|
|
|
|
|
#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;
|
2006-10-30 03:18:44 -07:00
|
|
|
if (map_type == _DRM_REGISTERS)
|
2006-08-22 01:47:33 -06:00
|
|
|
pgprot_val(tmp) |= _PAGE_GUARDED;
|
2008-03-05 02:25:03 -07:00
|
|
|
#elif defined(__ia64__)
|
2006-08-22 01:47:33 -06:00
|
|
|
if (efi_range_is_wc(vma->vm_start, vma->vm_end -
|
|
|
|
vma->vm_start))
|
|
|
|
tmp = pgprot_writecombine(tmp);
|
|
|
|
else
|
|
|
|
tmp = pgprot_noncached(tmp);
|
2008-03-05 02:25:03 -07:00
|
|
|
#elif defined(__sparc__)
|
|
|
|
tmp = pgprot_noncached(tmp);
|
2006-08-22 01:47:33 -06:00
|
|
|
#endif
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2008-03-05 02:25:03 -07:00
|
|
|
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;
|
|
|
|
#endif
|
|
|
|
return tmp;
|
|
|
|
}
|
2005-06-04 00:18:11 -06:00
|
|
|
|
2008-05-06 23:10:23 -06:00
|
|
|
#ifndef DRM_VM_NOPAGE
|
2003-05-26 18:37:33 -06:00
|
|
|
/**
|
2008-05-06 23:10:23 -06:00
|
|
|
* \c fault method for AGP virtual memory.
|
2003-05-26 18:37:33 -06:00
|
|
|
*
|
|
|
|
* \param vma virtual memory area.
|
|
|
|
* \param address access address.
|
|
|
|
* \return pointer to the page structure.
|
2004-09-30 15:12:10 -06:00
|
|
|
*
|
2003-05-26 18:37:33 -06:00
|
|
|
* 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.
|
|
|
|
*/
|
2004-09-04 17:21:40 -06:00
|
|
|
#if __OS_HAS_AGP
|
2008-05-06 23:10:23 -06:00
|
|
|
static int drm_do_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
|
1999-12-05 16:10:37 -07:00
|
|
|
{
|
2007-07-15 20:32:51 -06:00
|
|
|
struct drm_file *priv = vma->vm_file->private_data;
|
2008-03-05 12:21:50 -07:00
|
|
|
struct drm_device *dev = priv->minor->dev;
|
2007-07-15 19:22:15 -06:00
|
|
|
struct drm_map *map = NULL;
|
2007-07-15 20:32:51 -06:00
|
|
|
struct drm_map_list *r_list;
|
|
|
|
struct drm_hash_item *hash;
|
2001-08-10 10:29:21 -06:00
|
|
|
|
2001-08-13 18:35:07 -06:00
|
|
|
/*
|
2004-09-30 15:12:10 -06:00
|
|
|
* Find the right map
|
|
|
|
*/
|
2004-09-04 17:21:40 -06:00
|
|
|
if (!drm_core_has_AGP(dev))
|
2008-05-06 23:10:23 -06:00
|
|
|
goto vm_fault_error;
|
2001-09-25 03:32:16 -06:00
|
|
|
|
2004-09-30 15:12:10 -06:00
|
|
|
if (!dev->agp || !dev->agp->cant_use_aperture)
|
2008-05-06 23:10:23 -06:00
|
|
|
goto vm_fault_error;
|
2001-09-25 03:32:16 -06:00
|
|
|
|
2006-10-02 05:49:43 -06:00
|
|
|
if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash))
|
2008-05-06 23:10:23 -06:00
|
|
|
goto vm_fault_error;
|
2006-07-10 07:00:21 -06:00
|
|
|
|
2007-07-15 20:32:51 -06:00
|
|
|
r_list = drm_hash_entry(hash, struct drm_map_list, hash);
|
2006-07-10 07:00:21 -06:00
|
|
|
map = r_list->map;
|
2001-08-10 10:29:21 -06:00
|
|
|
|
2001-08-13 18:35:07 -06:00
|
|
|
if (map && map->type == _DRM_AGP) {
|
2008-05-06 23:10:23 -06:00
|
|
|
/*
|
|
|
|
* 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;
|
2005-08-16 06:51:57 -06:00
|
|
|
unsigned long baddr = map->offset + offset;
|
2001-08-13 18:35:07 -06:00
|
|
|
struct drm_agp_mem *agpmem;
|
|
|
|
struct page *page;
|
2001-08-10 10:29:21 -06:00
|
|
|
|
2004-07-20 05:26:13 -06:00
|
|
|
#ifdef __alpha__
|
2001-08-13 18:35:07 -06:00
|
|
|
/*
|
2004-09-30 15:12:10 -06:00
|
|
|
* Adjust to a bus-relative address
|
|
|
|
*/
|
2001-08-13 18:35:07 -06:00
|
|
|
baddr -= dev->hose->mem_space->start;
|
2001-09-25 03:32:16 -06:00
|
|
|
#endif
|
2001-08-10 10:29:21 -06:00
|
|
|
|
2001-08-13 18:35:07 -06:00
|
|
|
/*
|
2004-09-30 15:12:10 -06:00
|
|
|
* It's AGP memory - find the real physical page to map
|
|
|
|
*/
|
2007-05-25 12:26:24 -06:00
|
|
|
list_for_each_entry(agpmem, &dev->agp->memory, head) {
|
2001-08-13 18:35:07 -06:00
|
|
|
if (agpmem->bound <= baddr &&
|
2004-09-30 15:12:10 -06:00
|
|
|
agpmem->bound + agpmem->pages * PAGE_SIZE > baddr)
|
2001-08-13 18:35:07 -06:00
|
|
|
break;
|
|
|
|
}
|
2001-08-10 10:29:21 -06:00
|
|
|
|
2004-09-30 15:12:10 -06:00
|
|
|
if (!agpmem)
|
2008-05-06 23:10:23 -06:00
|
|
|
goto vm_fault_error;
|
2001-08-10 10:29:21 -06:00
|
|
|
|
2001-08-13 18:35:07 -06:00
|
|
|
/*
|
2004-09-30 15:12:10 -06:00
|
|
|
* Get the page, inc the use count, and return it
|
|
|
|
*/
|
2001-08-13 18:35:07 -06:00
|
|
|
offset = (baddr - agpmem->bound) >> PAGE_SHIFT;
|
|
|
|
page = virt_to_page(__va(agpmem->memory->memory[offset]));
|
|
|
|
get_page(page);
|
2008-05-06 23:10:23 -06:00
|
|
|
vmf->page = page;
|
2001-09-25 03:32:16 -06:00
|
|
|
|
2004-09-30 15:12:10 -06:00
|
|
|
DRM_DEBUG
|
|
|
|
("baddr = 0x%lx page = 0x%p, offset = 0x%lx, count=%d\n",
|
|
|
|
baddr, __va(agpmem->memory->memory[offset]), offset,
|
|
|
|
page_count(page));
|
2008-05-06 23:10:23 -06:00
|
|
|
return 0;
|
2004-09-30 15:12:10 -06:00
|
|
|
}
|
2008-05-06 23:10:23 -06:00
|
|
|
vm_fault_error:
|
|
|
|
return VM_FAULT_SIGBUS; /* Disallow mremap */
|
1999-12-05 16:10:37 -07:00
|
|
|
}
|
2004-09-30 15:12:10 -06:00
|
|
|
#else /* __OS_HAS_AGP */
|
2008-05-06 23:10:23 -06:00
|
|
|
static int drm_do_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
|
2004-09-04 17:21:40 -06:00
|
|
|
{
|
2008-05-06 23:10:23 -06:00
|
|
|
return VM_FAULT_SIGBUS;
|
2004-09-04 17:21:40 -06:00
|
|
|
}
|
2004-09-30 15:12:10 -06:00
|
|
|
#endif /* __OS_HAS_AGP */
|
1999-12-05 16:10:37 -07:00
|
|
|
|
2003-05-26 18:37:33 -06:00
|
|
|
/**
|
|
|
|
* \c nopage method for shared virtual memory.
|
|
|
|
*
|
|
|
|
* \param vma virtual memory area.
|
|
|
|
* \param address access address.
|
|
|
|
* \return pointer to the page structure.
|
2004-09-30 15:12:10 -06:00
|
|
|
*
|
2007-11-05 02:14:23 -07:00
|
|
|
* Get the mapping, find the real physical page to map, get the page, and
|
2003-05-26 18:37:33 -06:00
|
|
|
* return it.
|
|
|
|
*/
|
2008-05-06 23:10:23 -06:00
|
|
|
static int drm_do_vm_shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
|
2000-08-07 10:55:10 -06:00
|
|
|
{
|
2007-07-15 19:22:15 -06:00
|
|
|
struct drm_map *map = (struct drm_map *) vma->vm_private_data;
|
2004-09-30 15:12:10 -06:00
|
|
|
unsigned long offset;
|
|
|
|
unsigned long i;
|
|
|
|
struct page *page;
|
2000-08-07 10:55:10 -06:00
|
|
|
|
2004-09-30 15:12:10 -06:00
|
|
|
if (!map)
|
2008-05-06 23:10:23 -06:00
|
|
|
return VM_FAULT_SIGBUS; /* Nothing allocated */
|
2000-08-07 10:55:10 -06:00
|
|
|
|
2008-05-06 23:10:23 -06:00
|
|
|
offset = (unsigned long)vmf->virtual_address - vma->vm_start;
|
2001-03-14 15:22:50 -07:00
|
|
|
i = (unsigned long)map->handle + offset;
|
2005-11-29 02:46:27 -07:00
|
|
|
page = vmalloc_to_page((void *)i);
|
2002-05-16 17:47:15 -06:00
|
|
|
if (!page)
|
2008-05-06 23:10:23 -06:00
|
|
|
return VM_FAULT_SIGBUS;
|
2001-08-13 17:23:47 -06:00
|
|
|
get_page(page);
|
2008-05-06 23:10:23 -06:00
|
|
|
vmf->page = page;
|
2001-08-13 17:23:47 -06:00
|
|
|
|
2008-05-06 23:10:23 -06:00
|
|
|
DRM_DEBUG("shm_fault 0x%lx\n", offset);
|
|
|
|
return 0;
|
2000-08-07 10:55:10 -06:00
|
|
|
}
|
2008-05-06 23:10:23 -06:00
|
|
|
#endif
|
2000-08-07 10:55:10 -06:00
|
|
|
|
2003-05-26 18:37:33 -06:00
|
|
|
/**
|
|
|
|
* \c close method for shared virtual memory.
|
2004-09-30 15:12:10 -06:00
|
|
|
*
|
2003-05-26 18:37:33 -06:00
|
|
|
* \param vma virtual memory area.
|
2004-09-30 15:12:10 -06:00
|
|
|
*
|
2003-05-26 18:37:33 -06:00
|
|
|
* Deletes map information if we are the last
|
2003-03-30 00:23:03 -07:00
|
|
|
* person to close a mapping and it's not in the global maplist.
|
2001-03-14 15:22:50 -07:00
|
|
|
*/
|
2005-06-04 00:18:11 -06:00
|
|
|
static void drm_vm_shm_close(struct vm_area_struct *vma)
|
1999-12-05 16:10:37 -07:00
|
|
|
{
|
2007-07-15 20:32:51 -06:00
|
|
|
struct drm_file *priv = vma->vm_file->private_data;
|
2008-03-05 12:21:50 -07:00
|
|
|
struct drm_device *dev = priv->minor->dev;
|
2007-07-15 20:32:51 -06:00
|
|
|
struct drm_vma_entry *pt, *temp;
|
2007-07-15 19:22:15 -06:00
|
|
|
struct drm_map *map;
|
2007-07-15 20:32:51 -06:00
|
|
|
struct drm_map_list *r_list;
|
2001-03-14 15:22:50 -07:00
|
|
|
int found_maps = 0;
|
1999-12-05 16:10:37 -07:00
|
|
|
|
2001-03-14 15:22:50 -07:00
|
|
|
DRM_DEBUG("0x%08lx,0x%08lx\n",
|
|
|
|
vma->vm_start, vma->vm_end - vma->vm_start);
|
|
|
|
atomic_dec(&dev->vma_count);
|
1999-12-05 16:10:37 -07:00
|
|
|
|
2001-03-14 15:22:50 -07:00
|
|
|
map = vma->vm_private_data;
|
1999-12-05 16:10:37 -07:00
|
|
|
|
2006-07-23 18:51:27 -06:00
|
|
|
mutex_lock(&dev->struct_mutex);
|
2007-05-25 12:26:24 -06:00
|
|
|
list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
|
2004-09-30 15:12:10 -06:00
|
|
|
if (pt->vma->vm_private_data == map)
|
|
|
|
found_maps++;
|
2001-03-14 15:22:50 -07:00
|
|
|
if (pt->vma == vma) {
|
2007-05-25 12:26:24 -06:00
|
|
|
list_del(&pt->head);
|
2006-10-17 11:52:34 -06:00
|
|
|
drm_ctl_free(pt, sizeof(*pt), DRM_MEM_VMAS);
|
2001-03-14 15:22:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* We were the only map that was found */
|
2004-09-30 15:12:10 -06:00
|
|
|
if (found_maps == 1 && map->flags & _DRM_REMOVABLE) {
|
2001-03-14 15:22:50 -07:00
|
|
|
/* Check to see if we are in the maplist, if we are not, then
|
|
|
|
* we delete this mappings information.
|
|
|
|
*/
|
|
|
|
found_maps = 0;
|
2007-05-25 12:26:24 -06:00
|
|
|
list_for_each_entry(r_list, &dev->maplist, head) {
|
2004-09-30 15:12:10 -06:00
|
|
|
if (r_list->map == map)
|
|
|
|
found_maps++;
|
2001-03-14 15:22:50 -07:00
|
|
|
}
|
|
|
|
|
2004-09-30 15:12:10 -06:00
|
|
|
if (!found_maps) {
|
2005-04-25 23:19:11 -06:00
|
|
|
drm_dma_handle_t dmah;
|
|
|
|
|
2001-03-14 15:22:50 -07:00
|
|
|
switch (map->type) {
|
|
|
|
case _DRM_REGISTERS:
|
|
|
|
case _DRM_FRAME_BUFFER:
|
2004-09-04 17:21:40 -06:00
|
|
|
if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
|
2001-03-14 15:22:50 -07:00
|
|
|
int retcode;
|
|
|
|
retcode = mtrr_del(map->mtrr,
|
|
|
|
map->offset,
|
|
|
|
map->size);
|
|
|
|
DRM_DEBUG("mtrr_del = %d\n", retcode);
|
|
|
|
}
|
2007-01-07 16:55:49 -07:00
|
|
|
iounmap(map->handle);
|
2001-03-14 15:22:50 -07:00
|
|
|
break;
|
|
|
|
case _DRM_SHM:
|
|
|
|
vfree(map->handle);
|
|
|
|
break;
|
|
|
|
case _DRM_AGP:
|
2001-05-01 11:07:59 -06:00
|
|
|
case _DRM_SCATTER_GATHER:
|
2001-03-14 15:22:50 -07:00
|
|
|
break;
|
2005-01-01 13:03:15 -07:00
|
|
|
case _DRM_CONSISTENT:
|
2005-04-25 23:19:11 -06:00
|
|
|
dmah.vaddr = map->handle;
|
|
|
|
dmah.busaddr = map->offset;
|
|
|
|
dmah.size = map->size;
|
2005-05-28 14:36:22 -06:00
|
|
|
__drm_pci_free(dev, &dmah);
|
2005-01-01 13:03:15 -07:00
|
|
|
break;
|
2007-11-21 23:10:36 -07:00
|
|
|
case _DRM_TTM:
|
2006-08-22 01:47:33 -06:00
|
|
|
BUG_ON(1);
|
|
|
|
break;
|
2001-03-14 15:22:50 -07:00
|
|
|
}
|
2004-09-27 13:51:38 -06:00
|
|
|
drm_free(map, sizeof(*map), DRM_MEM_MAPS);
|
2001-03-14 15:22:50 -07:00
|
|
|
}
|
|
|
|
}
|
2006-07-23 18:51:27 -06:00
|
|
|
mutex_unlock(&dev->struct_mutex);
|
1999-12-05 16:10:37 -07:00
|
|
|
}
|
|
|
|
|
2008-05-06 23:10:23 -06:00
|
|
|
#ifndef DRM_VM_NOPAGE
|
2003-05-26 18:37:33 -06:00
|
|
|
/**
|
2008-05-06 23:10:23 -06:00
|
|
|
* \c fault method for DMA virtual memory.
|
2003-05-26 18:37:33 -06:00
|
|
|
*
|
|
|
|
* \param vma virtual memory area.
|
|
|
|
* \param address access address.
|
|
|
|
* \return pointer to the page structure.
|
2004-09-30 15:12:10 -06:00
|
|
|
*
|
2003-05-26 18:37:33 -06:00
|
|
|
* Determine the page number from the page offset and get it from drm_device_dma::pagelist.
|
|
|
|
*/
|
2008-05-06 23:10:23 -06:00
|
|
|
static int drm_do_vm_dma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
|
1999-12-05 16:10:37 -07:00
|
|
|
{
|
2007-07-15 20:32:51 -06:00
|
|
|
struct drm_file *priv = vma->vm_file->private_data;
|
2008-03-05 12:21:50 -07:00
|
|
|
struct drm_device *dev = priv->minor->dev;
|
2007-07-15 20:32:51 -06:00
|
|
|
struct drm_device_dma *dma = dev->dma;
|
2004-09-30 15:12:10 -06:00
|
|
|
unsigned long offset;
|
|
|
|
unsigned long page_nr;
|
|
|
|
struct page *page;
|
|
|
|
|
|
|
|
if (!dma)
|
2008-05-06 23:10:23 -06:00
|
|
|
return VM_FAULT_SIGBUS; /* Error */
|
2004-09-30 15:12:10 -06:00
|
|
|
if (!dma->pagelist)
|
2008-05-06 23:10:23 -06:00
|
|
|
return VM_FAULT_SIGBUS; /* Nothing allocated */
|
2004-09-30 15:12:10 -06:00
|
|
|
|
2008-05-06 23:10:23 -06:00
|
|
|
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 */
|
2004-09-30 15:12:10 -06:00
|
|
|
page = virt_to_page((dma->pagelist[page_nr] + (offset & (~PAGE_MASK))));
|
2001-08-13 17:23:47 -06:00
|
|
|
|
|
|
|
get_page(page);
|
2008-05-06 23:10:23 -06:00
|
|
|
vmf->page = page;
|
1999-12-05 16:10:37 -07:00
|
|
|
|
2008-05-06 23:10:23 -06:00
|
|
|
DRM_DEBUG("dma_fault 0x%lx (page %lu)\n", offset, page_nr);
|
|
|
|
return 0;
|
1999-12-05 16:10:37 -07:00
|
|
|
}
|
|
|
|
|
2003-05-26 18:37:33 -06:00
|
|
|
/**
|
2008-05-06 23:10:23 -06:00
|
|
|
* \c fault method for scatter-gather virtual memory.
|
2003-05-26 18:37:33 -06:00
|
|
|
*
|
|
|
|
* \param vma virtual memory area.
|
|
|
|
* \param address access address.
|
|
|
|
* \return pointer to the page structure.
|
2004-09-30 15:12:10 -06:00
|
|
|
*
|
2003-05-26 18:37:33 -06:00
|
|
|
* Determine the map offset from the page offset and get it from drm_sg_mem::pagelist.
|
|
|
|
*/
|
2008-05-06 23:10:23 -06:00
|
|
|
static int drm_do_vm_sg_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
|
2001-05-01 11:07:59 -06:00
|
|
|
{
|
2007-07-15 19:22:15 -06:00
|
|
|
struct drm_map *map = (struct drm_map *) vma->vm_private_data;
|
2007-07-15 20:32:51 -06:00
|
|
|
struct drm_file *priv = vma->vm_file->private_data;
|
2008-03-05 12:21:50 -07:00
|
|
|
struct drm_device *dev = priv->minor->dev;
|
2007-07-15 20:32:51 -06:00
|
|
|
struct drm_sg_mem *entry = dev->sg;
|
2001-05-01 11:07:59 -06:00
|
|
|
unsigned long offset;
|
|
|
|
unsigned long map_offset;
|
|
|
|
unsigned long page_offset;
|
|
|
|
struct page *page;
|
|
|
|
|
2004-09-30 15:12:10 -06:00
|
|
|
if (!entry)
|
2008-05-06 23:10:23 -06:00
|
|
|
return VM_FAULT_SIGBUS; /* Error */
|
2004-09-30 15:12:10 -06:00
|
|
|
if (!entry->pagelist)
|
2008-05-06 23:10:23 -06:00
|
|
|
return VM_FAULT_SIGBUS; /* Nothing allocated */
|
2001-05-01 11:07:59 -06:00
|
|
|
|
2008-05-06 23:10:23 -06:00
|
|
|
offset = (unsigned long)vmf->virtual_address - vma->vm_start;
|
2005-08-16 06:51:57 -06:00
|
|
|
map_offset = map->offset - (unsigned long)dev->sg->virtual;
|
2001-05-01 11:07:59 -06:00
|
|
|
page_offset = (offset >> PAGE_SHIFT) + (map_offset >> PAGE_SHIFT);
|
|
|
|
page = entry->pagelist[page_offset];
|
2001-08-13 17:23:47 -06:00
|
|
|
get_page(page);
|
2008-05-06 23:10:23 -06:00
|
|
|
vmf->page = page;
|
2001-05-01 11:07:59 -06:00
|
|
|
|
2008-05-06 23:10:23 -06:00
|
|
|
return 0;
|
2004-01-10 17:14:28 -07:00
|
|
|
}
|
2008-05-06 23:10:23 -06:00
|
|
|
#endif
|
2006-08-22 01:47:33 -06:00
|
|
|
|
2004-01-10 17:14:28 -07:00
|
|
|
/** AGP virtual memory operations */
|
2004-09-30 15:12:10 -06:00
|
|
|
static struct vm_operations_struct drm_vm_ops = {
|
2008-05-06 23:10:23 -06:00
|
|
|
#ifdef DRM_VM_NOPAGE
|
2004-09-27 13:51:38 -06:00
|
|
|
.nopage = drm_vm_nopage,
|
2008-05-06 23:10:23 -06:00
|
|
|
#else
|
|
|
|
.fault = drm_do_vm_fault,
|
|
|
|
#endif
|
2004-09-30 15:12:10 -06:00
|
|
|
.open = drm_vm_open,
|
|
|
|
.close = drm_vm_close,
|
2004-01-10 17:14:28 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Shared virtual memory operations */
|
2004-09-30 15:12:10 -06:00
|
|
|
static struct vm_operations_struct drm_vm_shm_ops = {
|
2008-05-06 23:10:23 -06:00
|
|
|
#ifdef DRM_VM_NOPAGE
|
2004-09-27 13:51:38 -06:00
|
|
|
.nopage = drm_vm_shm_nopage,
|
2008-05-06 23:10:23 -06:00
|
|
|
#else
|
|
|
|
.fault = drm_do_vm_shm_fault,
|
|
|
|
#endif
|
2004-09-30 15:12:10 -06:00
|
|
|
.open = drm_vm_open,
|
|
|
|
.close = drm_vm_shm_close,
|
2004-01-10 17:14:28 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/** DMA virtual memory operations */
|
2004-09-30 15:12:10 -06:00
|
|
|
static struct vm_operations_struct drm_vm_dma_ops = {
|
2008-05-06 23:10:23 -06:00
|
|
|
#ifdef DRM_VM_NOPAGE
|
2004-09-27 13:51:38 -06:00
|
|
|
.nopage = drm_vm_dma_nopage,
|
2008-05-06 23:10:23 -06:00
|
|
|
#else
|
|
|
|
.fault = drm_do_vm_dma_fault,
|
|
|
|
#endif
|
2004-09-30 15:12:10 -06:00
|
|
|
.open = drm_vm_open,
|
|
|
|
.close = drm_vm_close,
|
2004-01-10 17:14:28 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Scatter-gather virtual memory operations */
|
2004-09-30 15:12:10 -06:00
|
|
|
static struct vm_operations_struct drm_vm_sg_ops = {
|
2008-05-06 23:10:23 -06:00
|
|
|
#ifdef DRM_VM_NOPAGE
|
2004-09-27 13:51:38 -06:00
|
|
|
.nopage = drm_vm_sg_nopage,
|
2008-05-06 23:10:23 -06:00
|
|
|
#else
|
|
|
|
.fault = drm_do_vm_sg_fault,
|
|
|
|
#endif
|
2004-09-30 15:12:10 -06:00
|
|
|
.open = drm_vm_open,
|
|
|
|
.close = drm_vm_close,
|
2004-01-10 17:14:28 -07:00
|
|
|
};
|
|
|
|
|
2003-05-26 18:37:33 -06:00
|
|
|
/**
|
|
|
|
* \c open method for shared virtual memory.
|
2004-09-30 15:12:10 -06:00
|
|
|
*
|
2003-05-26 18:37:33 -06:00
|
|
|
* \param vma virtual memory area.
|
2004-09-30 15:12:10 -06:00
|
|
|
*
|
2003-05-26 18:37:33 -06:00
|
|
|
* Create a new drm_vma_entry structure as the \p vma private data entry and
|
|
|
|
* add it to drm_device::vmalist.
|
|
|
|
*/
|
2007-01-31 16:38:57 -07:00
|
|
|
static void drm_vm_open_locked(struct vm_area_struct *vma)
|
1999-12-05 16:10:37 -07:00
|
|
|
{
|
2007-07-15 20:32:51 -06:00
|
|
|
struct drm_file *priv = vma->vm_file->private_data;
|
2008-03-05 12:21:50 -07:00
|
|
|
struct drm_device *dev = priv->minor->dev;
|
2007-07-15 20:32:51 -06:00
|
|
|
struct drm_vma_entry *vma_entry;
|
1999-12-05 16:10:37 -07:00
|
|
|
|
|
|
|
DRM_DEBUG("0x%08lx,0x%08lx\n",
|
|
|
|
vma->vm_start, vma->vm_end - vma->vm_start);
|
|
|
|
atomic_inc(&dev->vma_count);
|
2000-08-04 04:04:55 -06:00
|
|
|
|
2006-10-17 11:52:34 -06:00
|
|
|
vma_entry = drm_ctl_alloc(sizeof(*vma_entry), DRM_MEM_VMAS);
|
1999-12-05 16:10:37 -07:00
|
|
|
if (vma_entry) {
|
2004-09-30 15:12:10 -06:00
|
|
|
vma_entry->vma = vma;
|
|
|
|
vma_entry->pid = current->pid;
|
2007-05-25 12:26:24 -06:00
|
|
|
list_add(&vma_entry->head, &dev->vmalist);
|
1999-12-05 16:10:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-31 16:38:57 -07:00
|
|
|
static void drm_vm_open(struct vm_area_struct *vma)
|
|
|
|
{
|
2007-07-15 20:32:51 -06:00
|
|
|
struct drm_file *priv = vma->vm_file->private_data;
|
2008-03-05 12:21:50 -07:00
|
|
|
struct drm_device *dev = priv->minor->dev;
|
2006-08-22 01:47:33 -06:00
|
|
|
|
|
|
|
mutex_lock(&dev->struct_mutex);
|
2007-01-31 16:38:57 -07:00
|
|
|
drm_vm_open_locked(vma);
|
|
|
|
mutex_unlock(&dev->struct_mutex);
|
|
|
|
}
|
|
|
|
|
2003-05-26 18:37:33 -06:00
|
|
|
/**
|
|
|
|
* \c close method for all virtual memory types.
|
2004-09-30 15:12:10 -06:00
|
|
|
*
|
2003-05-26 18:37:33 -06:00
|
|
|
* \param vma virtual memory area.
|
2004-09-30 15:12:10 -06:00
|
|
|
*
|
2003-05-26 18:37:33 -06:00
|
|
|
* Search the \p vma private data entry in drm_device::vmalist, unlink it, and
|
|
|
|
* free it.
|
|
|
|
*/
|
2005-06-04 00:18:11 -06:00
|
|
|
static void drm_vm_close(struct vm_area_struct *vma)
|
1999-12-05 16:10:37 -07:00
|
|
|
{
|
2007-07-15 20:32:51 -06:00
|
|
|
struct drm_file *priv = vma->vm_file->private_data;
|
2008-03-05 12:21:50 -07:00
|
|
|
struct drm_device *dev = priv->minor->dev;
|
2007-07-15 20:32:51 -06:00
|
|
|
struct drm_vma_entry *pt, *temp;
|
1999-12-05 16:10:37 -07:00
|
|
|
|
|
|
|
DRM_DEBUG("0x%08lx,0x%08lx\n",
|
|
|
|
vma->vm_start, vma->vm_end - vma->vm_start);
|
|
|
|
atomic_dec(&dev->vma_count);
|
|
|
|
|
2006-07-23 18:51:27 -06:00
|
|
|
mutex_lock(&dev->struct_mutex);
|
2007-05-25 12:26:24 -06:00
|
|
|
list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
|
1999-12-05 16:10:37 -07:00
|
|
|
if (pt->vma == vma) {
|
2007-05-25 12:26:24 -06:00
|
|
|
list_del(&pt->head);
|
2006-10-17 11:52:34 -06:00
|
|
|
drm_ctl_free(pt, sizeof(*pt), DRM_MEM_VMAS);
|
1999-12-05 16:10:37 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-07-23 18:51:27 -06:00
|
|
|
mutex_unlock(&dev->struct_mutex);
|
1999-12-05 16:10:37 -07:00
|
|
|
}
|
|
|
|
|
2006-08-22 01:47:33 -06:00
|
|
|
|
2003-05-26 18:37:33 -06:00
|
|
|
/**
|
|
|
|
* mmap DMA memory.
|
|
|
|
*
|
2007-07-20 07:39:25 -06:00
|
|
|
* \param file_priv DRM file private.
|
2003-05-26 18:37:33 -06:00
|
|
|
* \param vma virtual memory area.
|
|
|
|
* \return zero on success or a negative number on failure.
|
2004-09-30 15:12:10 -06:00
|
|
|
*
|
2003-05-26 18:37:33 -06:00
|
|
|
* Sets the virtual memory area operations structure to vm_dma_ops, the file
|
|
|
|
* pointer, and calls vm_open().
|
|
|
|
*/
|
2005-06-04 00:18:11 -06:00
|
|
|
static int drm_mmap_dma(struct file *filp, struct vm_area_struct *vma)
|
1999-12-05 16:10:37 -07:00
|
|
|
{
|
2007-07-15 20:32:51 -06:00
|
|
|
struct drm_file *priv = filp->private_data;
|
|
|
|
struct drm_device *dev;
|
|
|
|
struct drm_device_dma *dma;
|
2004-09-30 15:12:10 -06:00
|
|
|
unsigned long length = vma->vm_end - vma->vm_start;
|
2001-02-15 01:12:14 -07:00
|
|
|
|
2008-03-05 12:21:50 -07:00
|
|
|
dev = priv->minor->dev;
|
2004-09-30 15:12:10 -06:00
|
|
|
dma = dev->dma;
|
2006-10-02 05:49:43 -06:00
|
|
|
DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
|
|
|
|
vma->vm_start, vma->vm_end, vma->vm_pgoff);
|
1999-12-05 16:10:37 -07:00
|
|
|
|
2004-09-30 15:12:10 -06:00
|
|
|
/* Length must match exact page count */
|
2000-08-18 07:55:23 -06:00
|
|
|
if (!dma || (length >> PAGE_SHIFT) != dma->page_count) {
|
2000-08-04 04:04:55 -06:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
1999-12-05 16:10:37 -07:00
|
|
|
|
2007-04-26 05:16:28 -06:00
|
|
|
if (!capable(CAP_SYS_ADMIN) && (dma->flags & _DRM_DMA_USE_PCI_RO)) {
|
2006-10-01 20:46:42 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2004-09-30 15:12:10 -06:00
|
|
|
vma->vm_ops = &drm_vm_dma_ops;
|
|
|
|
vma->vm_flags |= VM_RESERVED; /* Don't swap */
|
2001-02-15 01:12:14 -07:00
|
|
|
|
2004-09-30 15:12:10 -06:00
|
|
|
vma->vm_file = filp; /* Needed for drm_vm_open() */
|
2007-01-31 16:38:57 -07:00
|
|
|
drm_vm_open_locked(vma);
|
1999-12-05 16:10:37 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-07-15 19:22:15 -06:00
|
|
|
unsigned long drm_core_get_map_ofs(struct drm_map * map)
|
2004-08-24 05:15:53 -06:00
|
|
|
{
|
2004-09-05 04:54:59 -06:00
|
|
|
return map->offset;
|
2004-08-24 05:15:53 -06:00
|
|
|
}
|
2004-09-27 13:51:38 -06:00
|
|
|
EXPORT_SYMBOL(drm_core_get_map_ofs);
|
2001-08-07 12:15:10 -06:00
|
|
|
|
2004-09-27 13:51:38 -06:00
|
|
|
unsigned long drm_core_get_reg_ofs(struct drm_device *dev)
|
2004-08-24 05:15:53 -06:00
|
|
|
{
|
2001-08-07 12:15:10 -06:00
|
|
|
#ifdef __alpha__
|
2004-09-05 04:54:59 -06:00
|
|
|
return dev->hose->dense_mem_base - dev->hose->mem_space->start;
|
2001-08-07 12:15:10 -06:00
|
|
|
#else
|
2004-09-05 04:54:59 -06:00
|
|
|
return 0;
|
2001-08-07 12:15:10 -06:00
|
|
|
#endif
|
2004-08-24 05:15:53 -06:00
|
|
|
}
|
2004-09-27 13:51:38 -06:00
|
|
|
EXPORT_SYMBOL(drm_core_get_reg_ofs);
|
2001-08-07 12:15:10 -06:00
|
|
|
|
2003-05-26 18:37:33 -06:00
|
|
|
/**
|
|
|
|
* mmap DMA memory.
|
|
|
|
*
|
2007-07-20 07:39:25 -06:00
|
|
|
* \param file_priv DRM file private.
|
2003-05-26 18:37:33 -06:00
|
|
|
* \param vma virtual memory area.
|
|
|
|
* \return zero on success or a negative number on failure.
|
2004-09-30 15:12:10 -06:00
|
|
|
*
|
2003-05-26 18:37:33 -06:00
|
|
|
* 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().
|
|
|
|
*/
|
2007-01-31 16:38:57 -07:00
|
|
|
static int drm_mmap_locked(struct file *filp, struct vm_area_struct *vma)
|
1999-12-05 16:10:37 -07:00
|
|
|
{
|
2007-07-15 20:32:51 -06:00
|
|
|
struct drm_file *priv = filp->private_data;
|
2008-03-05 12:21:50 -07:00
|
|
|
struct drm_device *dev = priv->minor->dev;
|
2007-07-15 19:22:15 -06:00
|
|
|
struct drm_map *map = NULL;
|
2004-09-30 15:12:10 -06:00
|
|
|
unsigned long offset = 0;
|
2007-07-15 19:22:15 -06:00
|
|
|
struct drm_hash_item *hash;
|
2001-02-15 01:12:14 -07:00
|
|
|
|
2006-10-02 05:49:43 -06:00
|
|
|
DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
|
|
|
|
vma->vm_start, vma->vm_end, vma->vm_pgoff);
|
1999-12-05 16:10:37 -07:00
|
|
|
|
2004-09-30 15:12:10 -06:00
|
|
|
if (!priv->authenticated)
|
|
|
|
return -EACCES;
|
2001-05-23 11:41:31 -06:00
|
|
|
|
2003-05-16 17:41:27 -06:00
|
|
|
/* We check for "dma". On Apple's UniNorth, it's valid to have
|
|
|
|
* the AGP mapped at physical address 0
|
|
|
|
* --BenH.
|
|
|
|
*/
|
2007-02-07 04:52:23 -07:00
|
|
|
|
2006-10-02 05:49:43 -06:00
|
|
|
if (!vma->vm_pgoff
|
2004-08-24 05:15:53 -06:00
|
|
|
#if __OS_HAS_AGP
|
2004-09-30 15:12:10 -06:00
|
|
|
&& (!dev->agp
|
|
|
|
|| dev->agp->agp_info.device->vendor != PCI_VENDOR_ID_APPLE)
|
2003-05-16 17:41:27 -06:00
|
|
|
#endif
|
|
|
|
)
|
2004-09-27 13:51:38 -06:00
|
|
|
return drm_mmap_dma(filp, vma);
|
1999-12-05 16:10:37 -07:00
|
|
|
|
2007-03-24 00:58:27 -06:00
|
|
|
if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash)) {
|
2006-08-19 01:59:18 -06:00
|
|
|
DRM_ERROR("Could not find map\n");
|
2006-07-10 07:00:21 -06:00
|
|
|
return -EINVAL;
|
2006-08-19 01:59:18 -06:00
|
|
|
}
|
|
|
|
|
2007-07-15 20:32:51 -06:00
|
|
|
map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
|
2004-09-30 15:12:10 -06:00
|
|
|
if (!map || ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN)))
|
1999-12-05 16:10:37 -07:00
|
|
|
return -EPERM;
|
|
|
|
|
2004-09-30 15:12:10 -06:00
|
|
|
/* Check for valid size. */
|
|
|
|
if (map->size < vma->vm_end - vma->vm_start)
|
|
|
|
return -EINVAL;
|
2001-02-15 01:12:14 -07:00
|
|
|
|
2000-05-18 00:14:27 -06:00
|
|
|
if (!capable(CAP_SYS_ADMIN) && (map->flags & _DRM_READ_ONLY)) {
|
2003-08-15 04:31:54 -06:00
|
|
|
vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
|
2004-09-04 20:36:48 -06:00
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
2000-05-18 00:14:27 -06:00
|
|
|
pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
|
|
|
|
#else
|
2004-09-30 15:12:10 -06:00
|
|
|
/* 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)))));
|
2000-05-18 00:14:27 -06:00
|
|
|
#endif
|
|
|
|
}
|
1999-12-05 16:10:37 -07:00
|
|
|
|
|
|
|
switch (map->type) {
|
2004-09-30 15:12:10 -06:00
|
|
|
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
|
|
|
|
* pages and mappings in nopage()
|
|
|
|
*/
|
2003-05-16 17:41:27 -06:00
|
|
|
#if defined(__powerpc__)
|
2004-09-30 15:12:10 -06:00
|
|
|
pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
|
2003-05-16 17:41:27 -06:00
|
|
|
#endif
|
2004-09-30 15:12:10 -06:00
|
|
|
vma->vm_ops = &drm_vm_ops;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* fall through to _DRM_FRAME_BUFFER... */
|
1999-12-05 16:10:37 -07:00
|
|
|
case _DRM_FRAME_BUFFER:
|
|
|
|
case _DRM_REGISTERS:
|
2004-10-10 16:54:55 -06:00
|
|
|
offset = dev->driver->get_reg_ofs(dev);
|
2006-08-22 01:47:33 -06:00
|
|
|
vma->vm_flags |= VM_IO; /* not in core dump */
|
|
|
|
vma->vm_page_prot = drm_io_prot(map->type, vma);
|
2005-08-16 06:51:57 -06:00
|
|
|
if (io_remap_pfn_range(vma, vma->vm_start,
|
2007-11-21 23:10:36 -07:00
|
|
|
(map->offset + offset) >> PAGE_SHIFT,
|
|
|
|
vma->vm_end - vma->vm_start,
|
|
|
|
vma->vm_page_prot))
|
2004-09-30 15:12:10 -06:00
|
|
|
return -EAGAIN;
|
2000-05-18 00:14:27 -06:00
|
|
|
DRM_DEBUG(" Type = %d; start = 0x%lx, end = 0x%lx,"
|
|
|
|
" offset = 0x%lx\n",
|
|
|
|
map->type,
|
2005-08-16 06:51:57 -06:00
|
|
|
vma->vm_start, vma->vm_end, map->offset + offset);
|
2004-09-27 13:51:38 -06:00
|
|
|
vma->vm_ops = &drm_vm_ops;
|
1999-12-05 16:10:37 -07:00
|
|
|
break;
|
2005-01-01 13:03:15 -07:00
|
|
|
case _DRM_CONSISTENT:
|
2005-11-29 02:46:27 -07:00
|
|
|
/* Consistent memory is really like shared memory. But
|
|
|
|
* it's allocated in a different way, so avoid nopage */
|
|
|
|
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;
|
2008-03-05 02:25:03 -07:00
|
|
|
vma->vm_page_prot = drm_dma_prot(map->type, vma);
|
2005-11-29 02:46:27 -07:00
|
|
|
/* fall through to _DRM_SHM */
|
|
|
|
case _DRM_SHM:
|
2004-09-27 13:51:38 -06:00
|
|
|
vma->vm_ops = &drm_vm_shm_ops;
|
2001-03-14 15:22:50 -07:00
|
|
|
vma->vm_private_data = (void *)map;
|
2004-09-30 15:12:10 -06:00
|
|
|
/* Don't let this area swap. Change when
|
|
|
|
DRM_KERNEL advisory is supported. */
|
2001-12-10 16:29:37 -07:00
|
|
|
vma->vm_flags |= VM_RESERVED;
|
1999-12-05 16:10:37 -07:00
|
|
|
break;
|
2001-05-01 11:07:59 -06:00
|
|
|
case _DRM_SCATTER_GATHER:
|
2004-09-27 13:51:38 -06:00
|
|
|
vma->vm_ops = &drm_vm_sg_ops;
|
2001-05-01 11:07:59 -06:00
|
|
|
vma->vm_private_data = (void *)map;
|
2001-12-10 16:29:37 -07:00
|
|
|
vma->vm_flags |= VM_RESERVED;
|
2008-03-05 02:25:03 -07:00
|
|
|
vma->vm_page_prot = drm_dma_prot(map->type, vma);
|
2004-09-30 15:12:10 -06:00
|
|
|
break;
|
2007-03-22 21:56:39 -06:00
|
|
|
case _DRM_TTM:
|
2007-02-02 06:47:44 -07:00
|
|
|
return drm_bo_mmap_locked(vma, filp, map);
|
1999-12-05 16:10:37 -07:00
|
|
|
default:
|
|
|
|
return -EINVAL; /* This should never happen. */
|
|
|
|
}
|
2004-09-30 15:12:10 -06:00
|
|
|
vma->vm_flags |= VM_RESERVED; /* Don't swap */
|
1999-12-05 16:10:37 -07:00
|
|
|
|
2004-09-30 15:12:10 -06:00
|
|
|
vma->vm_file = filp; /* Needed for drm_vm_open() */
|
2007-01-31 16:38:57 -07:00
|
|
|
drm_vm_open_locked(vma);
|
1999-12-05 16:10:37 -07:00
|
|
|
return 0;
|
|
|
|
}
|
2007-01-31 16:38:57 -07:00
|
|
|
|
|
|
|
int drm_mmap(struct file *filp, struct vm_area_struct *vma)
|
|
|
|
{
|
2007-07-15 20:32:51 -06:00
|
|
|
struct drm_file *priv = filp->private_data;
|
2008-03-05 12:21:50 -07:00
|
|
|
struct drm_device *dev = priv->minor->dev;
|
2007-01-31 16:38:57 -07:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
mutex_lock(&dev->struct_mutex);
|
|
|
|
ret = drm_mmap_locked(filp, vma);
|
|
|
|
mutex_unlock(&dev->struct_mutex);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2004-09-30 12:13:33 -06:00
|
|
|
EXPORT_SYMBOL(drm_mmap);
|
2007-02-02 06:47:44 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* buffer object vm functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \c Pagefault method for buffer objects.
|
|
|
|
*
|
|
|
|
* \param vma Virtual memory area.
|
2008-07-30 21:12:36 -06:00
|
|
|
* \param vmf vm fault data
|
|
|
|
* \return Error or VM_FAULT_NOPAGE:. The pfn is manually inserted.
|
2007-02-14 02:49:37 -07:00
|
|
|
*
|
|
|
|
* It's important that pfns are inserted while holding the bo->mutex lock.
|
|
|
|
* otherwise we might race with unmap_mapping_range() which is always
|
|
|
|
* called with the bo->mutex lock held.
|
|
|
|
*
|
2007-02-26 10:17:54 -07:00
|
|
|
* We're modifying the page attribute bits of the vma->vm_page_prot field,
|
|
|
|
* without holding the mmap_sem in write mode. Only in read mode.
|
|
|
|
* These bits are not used by the mm subsystem code, and we consider them
|
|
|
|
* protected by the bo->mutex lock.
|
2007-02-02 06:47:44 -07:00
|
|
|
*/
|
|
|
|
|
2008-08-10 18:58:41 -06:00
|
|
|
#if defined(DRM_FULL_MM_COMPAT) && !defined(DRM_NO_FAULT)
|
2008-07-30 21:12:36 -06:00
|
|
|
static int drm_bo_vm_fault(struct vm_area_struct *vma,
|
|
|
|
struct vm_fault *vmf)
|
2007-02-02 06:47:44 -07:00
|
|
|
{
|
2007-07-15 21:37:02 -06:00
|
|
|
struct drm_buffer_object *bo = (struct drm_buffer_object *) vma->vm_private_data;
|
2007-02-02 06:47:44 -07:00
|
|
|
unsigned long page_offset;
|
|
|
|
struct page *page = NULL;
|
2007-07-15 21:37:02 -06:00
|
|
|
struct drm_ttm *ttm;
|
2007-07-15 20:32:51 -06:00
|
|
|
struct drm_device *dev;
|
2007-02-02 06:47:44 -07:00
|
|
|
unsigned long pfn;
|
|
|
|
int err;
|
|
|
|
unsigned long bus_base;
|
|
|
|
unsigned long bus_offset;
|
|
|
|
unsigned long bus_size;
|
2008-07-30 21:12:36 -06:00
|
|
|
unsigned long ret = VM_FAULT_NOPAGE;
|
2007-03-22 21:56:39 -06:00
|
|
|
|
2007-10-21 04:20:56 -06:00
|
|
|
dev = bo->dev;
|
2008-12-10 16:47:28 -07:00
|
|
|
err = drm_bo_read_lock(&dev->bm.bm_lock, 1);
|
|
|
|
if (err)
|
|
|
|
return VM_FAULT_NOPAGE;
|
2007-02-02 06:47:44 -07:00
|
|
|
|
2007-10-21 04:20:56 -06:00
|
|
|
err = mutex_lock_interruptible(&bo->mutex);
|
|
|
|
if (err) {
|
2008-12-10 16:47:28 -07:00
|
|
|
drm_bo_read_unlock(&dev->bm.bm_lock);
|
2008-07-30 21:12:36 -06:00
|
|
|
return VM_FAULT_NOPAGE;
|
2007-10-21 04:20:56 -06:00
|
|
|
}
|
|
|
|
|
2008-04-13 06:49:14 -06:00
|
|
|
err = drm_bo_wait(bo, 0, 1, 0, 1);
|
2007-02-10 04:06:36 -07:00
|
|
|
if (err) {
|
2008-07-30 21:12:36 -06:00
|
|
|
ret = (err != -EAGAIN) ? VM_FAULT_SIGBUS : VM_FAULT_NOPAGE;
|
2008-04-23 04:43:30 -06:00
|
|
|
bo->priv_flags &= ~_DRM_BO_FLAG_UNLOCKED;
|
2007-02-10 04:06:36 -07:00
|
|
|
goto out_unlock;
|
|
|
|
}
|
2007-02-26 10:17:54 -07:00
|
|
|
|
2008-04-13 06:49:14 -06:00
|
|
|
bo->priv_flags &= ~_DRM_BO_FLAG_UNLOCKED;
|
|
|
|
|
2007-02-08 16:02:02 -07:00
|
|
|
/*
|
|
|
|
* If buffer happens to be in a non-mappable location,
|
|
|
|
* move it to a mappable.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!(bo->mem.flags & DRM_BO_FLAG_MAPPABLE)) {
|
2007-12-16 21:16:50 -07:00
|
|
|
uint32_t new_flags = bo->mem.proposed_flags |
|
2007-03-22 21:56:39 -06:00
|
|
|
DRM_BO_FLAG_MAPPABLE |
|
2007-02-08 16:02:02 -07:00
|
|
|
DRM_BO_FLAG_FORCE_MAPPABLE;
|
2007-12-16 21:16:50 -07:00
|
|
|
err = drm_bo_move_buffer(bo, new_flags, 0, 0);
|
2007-02-08 16:02:02 -07:00
|
|
|
if (err) {
|
2008-07-30 21:12:36 -06:00
|
|
|
ret = (err != -EAGAIN) ? VM_FAULT_SIGBUS : VM_FAULT_NOPAGE;
|
2007-02-08 16:02:02 -07:00
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
}
|
2007-02-02 06:47:44 -07:00
|
|
|
|
2007-03-22 21:56:39 -06:00
|
|
|
err = drm_bo_pci_offset(dev, &bo->mem, &bus_base, &bus_offset,
|
2007-02-06 08:59:45 -07:00
|
|
|
&bus_size);
|
2007-02-02 06:47:44 -07:00
|
|
|
|
|
|
|
if (err) {
|
2008-07-30 21:12:36 -06:00
|
|
|
ret = VM_FAULT_SIGBUS;
|
2007-02-02 06:47:44 -07:00
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
2008-07-30 21:12:36 -06:00
|
|
|
page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >> PAGE_SHIFT;
|
2007-02-02 06:47:44 -07:00
|
|
|
|
|
|
|
if (bus_size) {
|
2007-07-15 21:37:02 -06:00
|
|
|
struct drm_mem_type_manager *man = &dev->bm.man[bo->mem.mem_type];
|
2007-02-14 04:39:02 -07:00
|
|
|
|
2007-02-02 06:47:44 -07:00
|
|
|
pfn = ((bus_base + bus_offset) >> PAGE_SHIFT) + page_offset;
|
2007-02-14 04:39:02 -07:00
|
|
|
vma->vm_page_prot = drm_io_prot(man->drm_bus_maptype, vma);
|
2007-02-02 06:47:44 -07:00
|
|
|
} else {
|
|
|
|
ttm = bo->ttm;
|
|
|
|
|
2007-02-06 08:59:45 -07:00
|
|
|
drm_ttm_fixup_caching(ttm);
|
2007-02-08 05:29:08 -07:00
|
|
|
page = drm_ttm_get_page(ttm, page_offset);
|
2007-02-02 06:47:44 -07:00
|
|
|
if (!page) {
|
2008-07-30 21:12:36 -06:00
|
|
|
ret = VM_FAULT_OOM;
|
2007-02-08 05:29:08 -07:00
|
|
|
goto out_unlock;
|
2007-02-02 06:47:44 -07:00
|
|
|
}
|
|
|
|
pfn = page_to_pfn(page);
|
2007-03-29 13:33:41 -06:00
|
|
|
vma->vm_page_prot = (bo->mem.flags & DRM_BO_FLAG_CACHED) ?
|
|
|
|
vm_get_page_prot(vma->vm_flags) :
|
|
|
|
drm_io_prot(_DRM_TTM, vma);
|
2007-02-02 06:47:44 -07:00
|
|
|
}
|
2007-03-22 21:56:39 -06:00
|
|
|
|
2008-07-30 21:12:36 -06:00
|
|
|
err = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
|
2007-02-26 10:17:54 -07:00
|
|
|
if (err) {
|
2008-07-30 21:12:36 -06:00
|
|
|
ret = (err != -EAGAIN) ? VM_FAULT_OOM : VM_FAULT_NOPAGE;
|
2007-02-26 10:17:54 -07:00
|
|
|
goto out_unlock;
|
|
|
|
}
|
2007-02-02 06:47:44 -07:00
|
|
|
out_unlock:
|
2008-04-13 06:49:14 -06:00
|
|
|
BUG_ON(bo->priv_flags & _DRM_BO_FLAG_UNLOCKED);
|
2007-02-02 06:47:44 -07:00
|
|
|
mutex_unlock(&bo->mutex);
|
2008-12-10 16:47:28 -07:00
|
|
|
drm_bo_read_unlock(&dev->bm.bm_lock);
|
2007-02-26 10:17:54 -07:00
|
|
|
return ret;
|
2007-02-02 06:47:44 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void drm_bo_vm_open_locked(struct vm_area_struct *vma)
|
|
|
|
{
|
2007-07-15 21:37:02 -06:00
|
|
|
struct drm_buffer_object *bo = (struct drm_buffer_object *) vma->vm_private_data;
|
2007-02-02 06:47:44 -07:00
|
|
|
|
|
|
|
drm_vm_open_locked(vma);
|
|
|
|
atomic_inc(&bo->usage);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \c vma open method for buffer objects.
|
|
|
|
*
|
|
|
|
* \param vma virtual memory area.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void drm_bo_vm_open(struct vm_area_struct *vma)
|
|
|
|
{
|
2007-07-15 21:37:02 -06:00
|
|
|
struct drm_buffer_object *bo = (struct drm_buffer_object *) vma->vm_private_data;
|
2007-07-15 20:32:51 -06:00
|
|
|
struct drm_device *dev = bo->dev;
|
2007-02-02 06:47:44 -07:00
|
|
|
|
|
|
|
mutex_lock(&dev->struct_mutex);
|
|
|
|
drm_bo_vm_open_locked(vma);
|
|
|
|
mutex_unlock(&dev->struct_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \c vma close method for buffer objects.
|
|
|
|
*
|
|
|
|
* \param vma virtual memory area.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void drm_bo_vm_close(struct vm_area_struct *vma)
|
|
|
|
{
|
2007-07-15 21:37:02 -06:00
|
|
|
struct drm_buffer_object *bo = (struct drm_buffer_object *) vma->vm_private_data;
|
2007-07-15 20:32:51 -06:00
|
|
|
struct drm_device *dev = bo->dev;
|
2007-02-02 06:47:44 -07:00
|
|
|
|
|
|
|
drm_vm_close(vma);
|
|
|
|
if (bo) {
|
|
|
|
mutex_lock(&dev->struct_mutex);
|
2007-06-29 04:50:12 -06:00
|
|
|
drm_bo_usage_deref_locked((struct drm_buffer_object **)
|
|
|
|
&vma->vm_private_data);
|
2007-02-02 06:47:44 -07:00
|
|
|
mutex_unlock(&dev->struct_mutex);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct vm_operations_struct drm_bo_vm_ops = {
|
2007-02-14 02:49:37 -07:00
|
|
|
#ifdef DRM_FULL_MM_COMPAT
|
2008-07-30 21:12:36 -06:00
|
|
|
#ifdef DRM_NO_FAULT
|
2007-02-26 10:17:54 -07:00
|
|
|
.nopfn = drm_bo_vm_nopfn,
|
2008-07-30 21:12:36 -06:00
|
|
|
#else
|
|
|
|
.fault = drm_bo_vm_fault,
|
|
|
|
#endif
|
2007-02-14 02:49:37 -07:00
|
|
|
#else
|
|
|
|
.nopfn = drm_bo_vm_nopfn,
|
2007-02-02 06:47:44 -07:00
|
|
|
#endif
|
|
|
|
.open = drm_bo_vm_open,
|
|
|
|
.close = drm_bo_vm_close,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* mmap buffer object memory.
|
|
|
|
*
|
|
|
|
* \param vma virtual memory area.
|
2007-07-20 07:39:25 -06:00
|
|
|
* \param file_priv DRM file private.
|
2007-02-02 06:47:44 -07:00
|
|
|
* \param map The buffer object drm map.
|
|
|
|
* \return zero on success or a negative number on failure.
|
|
|
|
*/
|
2009-03-01 14:53:02 -07:00
|
|
|
static int drm_bo_mmap_locked(struct vm_area_struct *vma,
|
2007-02-02 06:47:44 -07:00
|
|
|
struct file *filp,
|
|
|
|
drm_local_map_t *map)
|
|
|
|
{
|
|
|
|
vma->vm_ops = &drm_bo_vm_ops;
|
|
|
|
vma->vm_private_data = map->handle;
|
|
|
|
vma->vm_file = filp;
|
|
|
|
vma->vm_flags |= VM_RESERVED | VM_IO;
|
2007-02-14 02:49:37 -07:00
|
|
|
vma->vm_flags |= VM_PFNMAP;
|
2007-02-02 06:47:44 -07:00
|
|
|
drm_bo_vm_open_locked(vma);
|
|
|
|
return 0;
|
|
|
|
}
|