Home | History | Annotate | Line # | Download | only in drm
drm_vm.c revision 1.1
      1  1.1  riastrad /**
      2  1.1  riastrad  * \file drm_vm.c
      3  1.1  riastrad  * Memory mapping for DRM
      4  1.1  riastrad  *
      5  1.1  riastrad  * \author Rickard E. (Rik) Faith <faith (at) valinux.com>
      6  1.1  riastrad  * \author Gareth Hughes <gareth (at) valinux.com>
      7  1.1  riastrad  */
      8  1.1  riastrad 
      9  1.1  riastrad /*
     10  1.1  riastrad  * Created: Mon Jan  4 08:58:31 1999 by faith (at) valinux.com
     11  1.1  riastrad  *
     12  1.1  riastrad  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
     13  1.1  riastrad  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
     14  1.1  riastrad  * All Rights Reserved.
     15  1.1  riastrad  *
     16  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
     17  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
     18  1.1  riastrad  * to deal in the Software without restriction, including without limitation
     19  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     20  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     21  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     22  1.1  riastrad  *
     23  1.1  riastrad  * The above copyright notice and this permission notice (including the next
     24  1.1  riastrad  * paragraph) shall be included in all copies or substantial portions of the
     25  1.1  riastrad  * Software.
     26  1.1  riastrad  *
     27  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     28  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     29  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     30  1.1  riastrad  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
     31  1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     32  1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     33  1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     34  1.1  riastrad  */
     35  1.1  riastrad 
     36  1.1  riastrad #include <drm/drmP.h>
     37  1.1  riastrad #include <linux/export.h>
     38  1.1  riastrad #if defined(__ia64__)
     39  1.1  riastrad #include <linux/efi.h>
     40  1.1  riastrad #include <linux/slab.h>
     41  1.1  riastrad #endif
     42  1.1  riastrad 
     43  1.1  riastrad static void drm_vm_open(struct vm_area_struct *vma);
     44  1.1  riastrad static void drm_vm_close(struct vm_area_struct *vma);
     45  1.1  riastrad 
     46  1.1  riastrad static pgprot_t drm_io_prot(uint32_t map_type, struct vm_area_struct *vma)
     47  1.1  riastrad {
     48  1.1  riastrad 	pgprot_t tmp = vm_get_page_prot(vma->vm_flags);
     49  1.1  riastrad 
     50  1.1  riastrad #if defined(__i386__) || defined(__x86_64__)
     51  1.1  riastrad 	if (boot_cpu_data.x86 > 3 && map_type != _DRM_AGP) {
     52  1.1  riastrad 		pgprot_val(tmp) |= _PAGE_PCD;
     53  1.1  riastrad 		pgprot_val(tmp) &= ~_PAGE_PWT;
     54  1.1  riastrad 	}
     55  1.1  riastrad #elif defined(__powerpc__)
     56  1.1  riastrad 	pgprot_val(tmp) |= _PAGE_NO_CACHE;
     57  1.1  riastrad 	if (map_type == _DRM_REGISTERS)
     58  1.1  riastrad 		pgprot_val(tmp) |= _PAGE_GUARDED;
     59  1.1  riastrad #elif defined(__ia64__)
     60  1.1  riastrad 	if (efi_range_is_wc(vma->vm_start, vma->vm_end -
     61  1.1  riastrad 				    vma->vm_start))
     62  1.1  riastrad 		tmp = pgprot_writecombine(tmp);
     63  1.1  riastrad 	else
     64  1.1  riastrad 		tmp = pgprot_noncached(tmp);
     65  1.1  riastrad #elif defined(__sparc__) || defined(__arm__) || defined(__mips__)
     66  1.1  riastrad 	tmp = pgprot_noncached(tmp);
     67  1.1  riastrad #endif
     68  1.1  riastrad 	return tmp;
     69  1.1  riastrad }
     70  1.1  riastrad 
     71  1.1  riastrad static pgprot_t drm_dma_prot(uint32_t map_type, struct vm_area_struct *vma)
     72  1.1  riastrad {
     73  1.1  riastrad 	pgprot_t tmp = vm_get_page_prot(vma->vm_flags);
     74  1.1  riastrad 
     75  1.1  riastrad #if defined(__powerpc__) && defined(CONFIG_NOT_COHERENT_CACHE)
     76  1.1  riastrad 	tmp |= _PAGE_NO_CACHE;
     77  1.1  riastrad #endif
     78  1.1  riastrad 	return tmp;
     79  1.1  riastrad }
     80  1.1  riastrad 
     81  1.1  riastrad /**
     82  1.1  riastrad  * \c fault method for AGP virtual memory.
     83  1.1  riastrad  *
     84  1.1  riastrad  * \param vma virtual memory area.
     85  1.1  riastrad  * \param address access address.
     86  1.1  riastrad  * \return pointer to the page structure.
     87  1.1  riastrad  *
     88  1.1  riastrad  * Find the right map and if it's AGP memory find the real physical page to
     89  1.1  riastrad  * map, get the page, increment the use count and return it.
     90  1.1  riastrad  */
     91  1.1  riastrad #if __OS_HAS_AGP
     92  1.1  riastrad static int drm_do_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
     93  1.1  riastrad {
     94  1.1  riastrad 	struct drm_file *priv = vma->vm_file->private_data;
     95  1.1  riastrad 	struct drm_device *dev = priv->minor->dev;
     96  1.1  riastrad 	struct drm_local_map *map = NULL;
     97  1.1  riastrad 	struct drm_map_list *r_list;
     98  1.1  riastrad 	struct drm_hash_item *hash;
     99  1.1  riastrad 
    100  1.1  riastrad 	/*
    101  1.1  riastrad 	 * Find the right map
    102  1.1  riastrad 	 */
    103  1.1  riastrad 	if (!drm_core_has_AGP(dev))
    104  1.1  riastrad 		goto vm_fault_error;
    105  1.1  riastrad 
    106  1.1  riastrad 	if (!dev->agp || !dev->agp->cant_use_aperture)
    107  1.1  riastrad 		goto vm_fault_error;
    108  1.1  riastrad 
    109  1.1  riastrad 	if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash))
    110  1.1  riastrad 		goto vm_fault_error;
    111  1.1  riastrad 
    112  1.1  riastrad 	r_list = drm_hash_entry(hash, struct drm_map_list, hash);
    113  1.1  riastrad 	map = r_list->map;
    114  1.1  riastrad 
    115  1.1  riastrad 	if (map && map->type == _DRM_AGP) {
    116  1.1  riastrad 		/*
    117  1.1  riastrad 		 * Using vm_pgoff as a selector forces us to use this unusual
    118  1.1  riastrad 		 * addressing scheme.
    119  1.1  riastrad 		 */
    120  1.1  riastrad 		resource_size_t offset = (unsigned long)vmf->virtual_address -
    121  1.1  riastrad 			vma->vm_start;
    122  1.1  riastrad 		resource_size_t baddr = map->offset + offset;
    123  1.1  riastrad 		struct drm_agp_mem *agpmem;
    124  1.1  riastrad 		struct page *page;
    125  1.1  riastrad 
    126  1.1  riastrad #ifdef __alpha__
    127  1.1  riastrad 		/*
    128  1.1  riastrad 		 * Adjust to a bus-relative address
    129  1.1  riastrad 		 */
    130  1.1  riastrad 		baddr -= dev->hose->mem_space->start;
    131  1.1  riastrad #endif
    132  1.1  riastrad 
    133  1.1  riastrad 		/*
    134  1.1  riastrad 		 * It's AGP memory - find the real physical page to map
    135  1.1  riastrad 		 */
    136  1.1  riastrad 		list_for_each_entry(agpmem, &dev->agp->memory, head) {
    137  1.1  riastrad 			if (agpmem->bound <= baddr &&
    138  1.1  riastrad 			    agpmem->bound + agpmem->pages * PAGE_SIZE > baddr)
    139  1.1  riastrad 				break;
    140  1.1  riastrad 		}
    141  1.1  riastrad 
    142  1.1  riastrad 		if (&agpmem->head == &dev->agp->memory)
    143  1.1  riastrad 			goto vm_fault_error;
    144  1.1  riastrad 
    145  1.1  riastrad 		/*
    146  1.1  riastrad 		 * Get the page, inc the use count, and return it
    147  1.1  riastrad 		 */
    148  1.1  riastrad 		offset = (baddr - agpmem->bound) >> PAGE_SHIFT;
    149  1.1  riastrad 		page = agpmem->memory->pages[offset];
    150  1.1  riastrad 		get_page(page);
    151  1.1  riastrad 		vmf->page = page;
    152  1.1  riastrad 
    153  1.1  riastrad 		DRM_DEBUG
    154  1.1  riastrad 		    ("baddr = 0x%llx page = 0x%p, offset = 0x%llx, count=%d\n",
    155  1.1  riastrad 		     (unsigned long long)baddr,
    156  1.1  riastrad 		     agpmem->memory->pages[offset],
    157  1.1  riastrad 		     (unsigned long long)offset,
    158  1.1  riastrad 		     page_count(page));
    159  1.1  riastrad 		return 0;
    160  1.1  riastrad 	}
    161  1.1  riastrad vm_fault_error:
    162  1.1  riastrad 	return VM_FAULT_SIGBUS;	/* Disallow mremap */
    163  1.1  riastrad }
    164  1.1  riastrad #else				/* __OS_HAS_AGP */
    165  1.1  riastrad static int drm_do_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
    166  1.1  riastrad {
    167  1.1  riastrad 	return VM_FAULT_SIGBUS;
    168  1.1  riastrad }
    169  1.1  riastrad #endif				/* __OS_HAS_AGP */
    170  1.1  riastrad 
    171  1.1  riastrad /**
    172  1.1  riastrad  * \c nopage method for shared virtual memory.
    173  1.1  riastrad  *
    174  1.1  riastrad  * \param vma virtual memory area.
    175  1.1  riastrad  * \param address access address.
    176  1.1  riastrad  * \return pointer to the page structure.
    177  1.1  riastrad  *
    178  1.1  riastrad  * Get the mapping, find the real physical page to map, get the page, and
    179  1.1  riastrad  * return it.
    180  1.1  riastrad  */
    181  1.1  riastrad static int drm_do_vm_shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
    182  1.1  riastrad {
    183  1.1  riastrad 	struct drm_local_map *map = vma->vm_private_data;
    184  1.1  riastrad 	unsigned long offset;
    185  1.1  riastrad 	unsigned long i;
    186  1.1  riastrad 	struct page *page;
    187  1.1  riastrad 
    188  1.1  riastrad 	if (!map)
    189  1.1  riastrad 		return VM_FAULT_SIGBUS;	/* Nothing allocated */
    190  1.1  riastrad 
    191  1.1  riastrad 	offset = (unsigned long)vmf->virtual_address - vma->vm_start;
    192  1.1  riastrad 	i = (unsigned long)map->handle + offset;
    193  1.1  riastrad 	page = vmalloc_to_page((void *)i);
    194  1.1  riastrad 	if (!page)
    195  1.1  riastrad 		return VM_FAULT_SIGBUS;
    196  1.1  riastrad 	get_page(page);
    197  1.1  riastrad 	vmf->page = page;
    198  1.1  riastrad 
    199  1.1  riastrad 	DRM_DEBUG("shm_fault 0x%lx\n", offset);
    200  1.1  riastrad 	return 0;
    201  1.1  riastrad }
    202  1.1  riastrad 
    203  1.1  riastrad /**
    204  1.1  riastrad  * \c close method for shared virtual memory.
    205  1.1  riastrad  *
    206  1.1  riastrad  * \param vma virtual memory area.
    207  1.1  riastrad  *
    208  1.1  riastrad  * Deletes map information if we are the last
    209  1.1  riastrad  * person to close a mapping and it's not in the global maplist.
    210  1.1  riastrad  */
    211  1.1  riastrad static void drm_vm_shm_close(struct vm_area_struct *vma)
    212  1.1  riastrad {
    213  1.1  riastrad 	struct drm_file *priv = vma->vm_file->private_data;
    214  1.1  riastrad 	struct drm_device *dev = priv->minor->dev;
    215  1.1  riastrad 	struct drm_vma_entry *pt, *temp;
    216  1.1  riastrad 	struct drm_local_map *map;
    217  1.1  riastrad 	struct drm_map_list *r_list;
    218  1.1  riastrad 	int found_maps = 0;
    219  1.1  riastrad 
    220  1.1  riastrad 	DRM_DEBUG("0x%08lx,0x%08lx\n",
    221  1.1  riastrad 		  vma->vm_start, vma->vm_end - vma->vm_start);
    222  1.1  riastrad 	atomic_dec(&dev->vma_count);
    223  1.1  riastrad 
    224  1.1  riastrad 	map = vma->vm_private_data;
    225  1.1  riastrad 
    226  1.1  riastrad 	mutex_lock(&dev->struct_mutex);
    227  1.1  riastrad 	list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
    228  1.1  riastrad 		if (pt->vma->vm_private_data == map)
    229  1.1  riastrad 			found_maps++;
    230  1.1  riastrad 		if (pt->vma == vma) {
    231  1.1  riastrad 			list_del(&pt->head);
    232  1.1  riastrad 			kfree(pt);
    233  1.1  riastrad 		}
    234  1.1  riastrad 	}
    235  1.1  riastrad 
    236  1.1  riastrad 	/* We were the only map that was found */
    237  1.1  riastrad 	if (found_maps == 1 && map->flags & _DRM_REMOVABLE) {
    238  1.1  riastrad 		/* Check to see if we are in the maplist, if we are not, then
    239  1.1  riastrad 		 * we delete this mappings information.
    240  1.1  riastrad 		 */
    241  1.1  riastrad 		found_maps = 0;
    242  1.1  riastrad 		list_for_each_entry(r_list, &dev->maplist, head) {
    243  1.1  riastrad 			if (r_list->map == map)
    244  1.1  riastrad 				found_maps++;
    245  1.1  riastrad 		}
    246  1.1  riastrad 
    247  1.1  riastrad 		if (!found_maps) {
    248  1.1  riastrad 			drm_dma_handle_t dmah;
    249  1.1  riastrad 
    250  1.1  riastrad 			switch (map->type) {
    251  1.1  riastrad 			case _DRM_REGISTERS:
    252  1.1  riastrad 			case _DRM_FRAME_BUFFER:
    253  1.1  riastrad 				if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
    254  1.1  riastrad 					int retcode;
    255  1.1  riastrad 					retcode = mtrr_del(map->mtrr,
    256  1.1  riastrad 							   map->offset,
    257  1.1  riastrad 							   map->size);
    258  1.1  riastrad 					DRM_DEBUG("mtrr_del = %d\n", retcode);
    259  1.1  riastrad 				}
    260  1.1  riastrad 				iounmap(map->handle);
    261  1.1  riastrad 				break;
    262  1.1  riastrad 			case _DRM_SHM:
    263  1.1  riastrad 				vfree(map->handle);
    264  1.1  riastrad 				break;
    265  1.1  riastrad 			case _DRM_AGP:
    266  1.1  riastrad 			case _DRM_SCATTER_GATHER:
    267  1.1  riastrad 				break;
    268  1.1  riastrad 			case _DRM_CONSISTENT:
    269  1.1  riastrad 				dmah.vaddr = map->handle;
    270  1.1  riastrad 				dmah.busaddr = map->offset;
    271  1.1  riastrad 				dmah.size = map->size;
    272  1.1  riastrad 				__drm_pci_free(dev, &dmah);
    273  1.1  riastrad 				break;
    274  1.1  riastrad 			case _DRM_GEM:
    275  1.1  riastrad 				DRM_ERROR("tried to rmmap GEM object\n");
    276  1.1  riastrad 				break;
    277  1.1  riastrad 			}
    278  1.1  riastrad 			kfree(map);
    279  1.1  riastrad 		}
    280  1.1  riastrad 	}
    281  1.1  riastrad 	mutex_unlock(&dev->struct_mutex);
    282  1.1  riastrad }
    283  1.1  riastrad 
    284  1.1  riastrad /**
    285  1.1  riastrad  * \c fault method for DMA virtual memory.
    286  1.1  riastrad  *
    287  1.1  riastrad  * \param vma virtual memory area.
    288  1.1  riastrad  * \param address access address.
    289  1.1  riastrad  * \return pointer to the page structure.
    290  1.1  riastrad  *
    291  1.1  riastrad  * Determine the page number from the page offset and get it from drm_device_dma::pagelist.
    292  1.1  riastrad  */
    293  1.1  riastrad static int drm_do_vm_dma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
    294  1.1  riastrad {
    295  1.1  riastrad 	struct drm_file *priv = vma->vm_file->private_data;
    296  1.1  riastrad 	struct drm_device *dev = priv->minor->dev;
    297  1.1  riastrad 	struct drm_device_dma *dma = dev->dma;
    298  1.1  riastrad 	unsigned long offset;
    299  1.1  riastrad 	unsigned long page_nr;
    300  1.1  riastrad 	struct page *page;
    301  1.1  riastrad 
    302  1.1  riastrad 	if (!dma)
    303  1.1  riastrad 		return VM_FAULT_SIGBUS;	/* Error */
    304  1.1  riastrad 	if (!dma->pagelist)
    305  1.1  riastrad 		return VM_FAULT_SIGBUS;	/* Nothing allocated */
    306  1.1  riastrad 
    307  1.1  riastrad 	offset = (unsigned long)vmf->virtual_address - vma->vm_start;	/* vm_[pg]off[set] should be 0 */
    308  1.1  riastrad 	page_nr = offset >> PAGE_SHIFT; /* page_nr could just be vmf->pgoff */
    309  1.1  riastrad 	page = virt_to_page((dma->pagelist[page_nr] + (offset & (~PAGE_MASK))));
    310  1.1  riastrad 
    311  1.1  riastrad 	get_page(page);
    312  1.1  riastrad 	vmf->page = page;
    313  1.1  riastrad 
    314  1.1  riastrad 	DRM_DEBUG("dma_fault 0x%lx (page %lu)\n", offset, page_nr);
    315  1.1  riastrad 	return 0;
    316  1.1  riastrad }
    317  1.1  riastrad 
    318  1.1  riastrad /**
    319  1.1  riastrad  * \c fault method for scatter-gather virtual memory.
    320  1.1  riastrad  *
    321  1.1  riastrad  * \param vma virtual memory area.
    322  1.1  riastrad  * \param address access address.
    323  1.1  riastrad  * \return pointer to the page structure.
    324  1.1  riastrad  *
    325  1.1  riastrad  * Determine the map offset from the page offset and get it from drm_sg_mem::pagelist.
    326  1.1  riastrad  */
    327  1.1  riastrad static int drm_do_vm_sg_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
    328  1.1  riastrad {
    329  1.1  riastrad 	struct drm_local_map *map = vma->vm_private_data;
    330  1.1  riastrad 	struct drm_file *priv = vma->vm_file->private_data;
    331  1.1  riastrad 	struct drm_device *dev = priv->minor->dev;
    332  1.1  riastrad 	struct drm_sg_mem *entry = dev->sg;
    333  1.1  riastrad 	unsigned long offset;
    334  1.1  riastrad 	unsigned long map_offset;
    335  1.1  riastrad 	unsigned long page_offset;
    336  1.1  riastrad 	struct page *page;
    337  1.1  riastrad 
    338  1.1  riastrad 	if (!entry)
    339  1.1  riastrad 		return VM_FAULT_SIGBUS;	/* Error */
    340  1.1  riastrad 	if (!entry->pagelist)
    341  1.1  riastrad 		return VM_FAULT_SIGBUS;	/* Nothing allocated */
    342  1.1  riastrad 
    343  1.1  riastrad 	offset = (unsigned long)vmf->virtual_address - vma->vm_start;
    344  1.1  riastrad 	map_offset = map->offset - (unsigned long)dev->sg->virtual;
    345  1.1  riastrad 	page_offset = (offset >> PAGE_SHIFT) + (map_offset >> PAGE_SHIFT);
    346  1.1  riastrad 	page = entry->pagelist[page_offset];
    347  1.1  riastrad 	get_page(page);
    348  1.1  riastrad 	vmf->page = page;
    349  1.1  riastrad 
    350  1.1  riastrad 	return 0;
    351  1.1  riastrad }
    352  1.1  riastrad 
    353  1.1  riastrad static int drm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
    354  1.1  riastrad {
    355  1.1  riastrad 	return drm_do_vm_fault(vma, vmf);
    356  1.1  riastrad }
    357  1.1  riastrad 
    358  1.1  riastrad static int drm_vm_shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
    359  1.1  riastrad {
    360  1.1  riastrad 	return drm_do_vm_shm_fault(vma, vmf);
    361  1.1  riastrad }
    362  1.1  riastrad 
    363  1.1  riastrad static int drm_vm_dma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
    364  1.1  riastrad {
    365  1.1  riastrad 	return drm_do_vm_dma_fault(vma, vmf);
    366  1.1  riastrad }
    367  1.1  riastrad 
    368  1.1  riastrad static int drm_vm_sg_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
    369  1.1  riastrad {
    370  1.1  riastrad 	return drm_do_vm_sg_fault(vma, vmf);
    371  1.1  riastrad }
    372  1.1  riastrad 
    373  1.1  riastrad /** AGP virtual memory operations */
    374  1.1  riastrad static const struct vm_operations_struct drm_vm_ops = {
    375  1.1  riastrad 	.fault = drm_vm_fault,
    376  1.1  riastrad 	.open = drm_vm_open,
    377  1.1  riastrad 	.close = drm_vm_close,
    378  1.1  riastrad };
    379  1.1  riastrad 
    380  1.1  riastrad /** Shared virtual memory operations */
    381  1.1  riastrad static const struct vm_operations_struct drm_vm_shm_ops = {
    382  1.1  riastrad 	.fault = drm_vm_shm_fault,
    383  1.1  riastrad 	.open = drm_vm_open,
    384  1.1  riastrad 	.close = drm_vm_shm_close,
    385  1.1  riastrad };
    386  1.1  riastrad 
    387  1.1  riastrad /** DMA virtual memory operations */
    388  1.1  riastrad static const struct vm_operations_struct drm_vm_dma_ops = {
    389  1.1  riastrad 	.fault = drm_vm_dma_fault,
    390  1.1  riastrad 	.open = drm_vm_open,
    391  1.1  riastrad 	.close = drm_vm_close,
    392  1.1  riastrad };
    393  1.1  riastrad 
    394  1.1  riastrad /** Scatter-gather virtual memory operations */
    395  1.1  riastrad static const struct vm_operations_struct drm_vm_sg_ops = {
    396  1.1  riastrad 	.fault = drm_vm_sg_fault,
    397  1.1  riastrad 	.open = drm_vm_open,
    398  1.1  riastrad 	.close = drm_vm_close,
    399  1.1  riastrad };
    400  1.1  riastrad 
    401  1.1  riastrad /**
    402  1.1  riastrad  * \c open method for shared virtual memory.
    403  1.1  riastrad  *
    404  1.1  riastrad  * \param vma virtual memory area.
    405  1.1  riastrad  *
    406  1.1  riastrad  * Create a new drm_vma_entry structure as the \p vma private data entry and
    407  1.1  riastrad  * add it to drm_device::vmalist.
    408  1.1  riastrad  */
    409  1.1  riastrad void drm_vm_open_locked(struct drm_device *dev,
    410  1.1  riastrad 		struct vm_area_struct *vma)
    411  1.1  riastrad {
    412  1.1  riastrad 	struct drm_vma_entry *vma_entry;
    413  1.1  riastrad 
    414  1.1  riastrad 	DRM_DEBUG("0x%08lx,0x%08lx\n",
    415  1.1  riastrad 		  vma->vm_start, vma->vm_end - vma->vm_start);
    416  1.1  riastrad 	atomic_inc(&dev->vma_count);
    417  1.1  riastrad 
    418  1.1  riastrad 	vma_entry = kmalloc(sizeof(*vma_entry), GFP_KERNEL);
    419  1.1  riastrad 	if (vma_entry) {
    420  1.1  riastrad 		vma_entry->vma = vma;
    421  1.1  riastrad 		vma_entry->pid = current->pid;
    422  1.1  riastrad 		list_add(&vma_entry->head, &dev->vmalist);
    423  1.1  riastrad 	}
    424  1.1  riastrad }
    425  1.1  riastrad 
    426  1.1  riastrad static void drm_vm_open(struct vm_area_struct *vma)
    427  1.1  riastrad {
    428  1.1  riastrad 	struct drm_file *priv = vma->vm_file->private_data;
    429  1.1  riastrad 	struct drm_device *dev = priv->minor->dev;
    430  1.1  riastrad 
    431  1.1  riastrad 	mutex_lock(&dev->struct_mutex);
    432  1.1  riastrad 	drm_vm_open_locked(dev, vma);
    433  1.1  riastrad 	mutex_unlock(&dev->struct_mutex);
    434  1.1  riastrad }
    435  1.1  riastrad 
    436  1.1  riastrad void drm_vm_close_locked(struct drm_device *dev,
    437  1.1  riastrad 		struct vm_area_struct *vma)
    438  1.1  riastrad {
    439  1.1  riastrad 	struct drm_vma_entry *pt, *temp;
    440  1.1  riastrad 
    441  1.1  riastrad 	DRM_DEBUG("0x%08lx,0x%08lx\n",
    442  1.1  riastrad 		  vma->vm_start, vma->vm_end - vma->vm_start);
    443  1.1  riastrad 	atomic_dec(&dev->vma_count);
    444  1.1  riastrad 
    445  1.1  riastrad 	list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
    446  1.1  riastrad 		if (pt->vma == vma) {
    447  1.1  riastrad 			list_del(&pt->head);
    448  1.1  riastrad 			kfree(pt);
    449  1.1  riastrad 			break;
    450  1.1  riastrad 		}
    451  1.1  riastrad 	}
    452  1.1  riastrad }
    453  1.1  riastrad 
    454  1.1  riastrad /**
    455  1.1  riastrad  * \c close method for all virtual memory types.
    456  1.1  riastrad  *
    457  1.1  riastrad  * \param vma virtual memory area.
    458  1.1  riastrad  *
    459  1.1  riastrad  * Search the \p vma private data entry in drm_device::vmalist, unlink it, and
    460  1.1  riastrad  * free it.
    461  1.1  riastrad  */
    462  1.1  riastrad static void drm_vm_close(struct vm_area_struct *vma)
    463  1.1  riastrad {
    464  1.1  riastrad 	struct drm_file *priv = vma->vm_file->private_data;
    465  1.1  riastrad 	struct drm_device *dev = priv->minor->dev;
    466  1.1  riastrad 
    467  1.1  riastrad 	mutex_lock(&dev->struct_mutex);
    468  1.1  riastrad 	drm_vm_close_locked(dev, vma);
    469  1.1  riastrad 	mutex_unlock(&dev->struct_mutex);
    470  1.1  riastrad }
    471  1.1  riastrad 
    472  1.1  riastrad /**
    473  1.1  riastrad  * mmap DMA memory.
    474  1.1  riastrad  *
    475  1.1  riastrad  * \param file_priv DRM file private.
    476  1.1  riastrad  * \param vma virtual memory area.
    477  1.1  riastrad  * \return zero on success or a negative number on failure.
    478  1.1  riastrad  *
    479  1.1  riastrad  * Sets the virtual memory area operations structure to vm_dma_ops, the file
    480  1.1  riastrad  * pointer, and calls vm_open().
    481  1.1  riastrad  */
    482  1.1  riastrad static int drm_mmap_dma(struct file *filp, struct vm_area_struct *vma)
    483  1.1  riastrad {
    484  1.1  riastrad 	struct drm_file *priv = filp->private_data;
    485  1.1  riastrad 	struct drm_device *dev;
    486  1.1  riastrad 	struct drm_device_dma *dma;
    487  1.1  riastrad 	unsigned long length = vma->vm_end - vma->vm_start;
    488  1.1  riastrad 
    489  1.1  riastrad 	dev = priv->minor->dev;
    490  1.1  riastrad 	dma = dev->dma;
    491  1.1  riastrad 	DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
    492  1.1  riastrad 		  vma->vm_start, vma->vm_end, vma->vm_pgoff);
    493  1.1  riastrad 
    494  1.1  riastrad 	/* Length must match exact page count */
    495  1.1  riastrad 	if (!dma || (length >> PAGE_SHIFT) != dma->page_count) {
    496  1.1  riastrad 		return -EINVAL;
    497  1.1  riastrad 	}
    498  1.1  riastrad 
    499  1.1  riastrad 	if (!capable(CAP_SYS_ADMIN) &&
    500  1.1  riastrad 	    (dma->flags & _DRM_DMA_USE_PCI_RO)) {
    501  1.1  riastrad 		vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
    502  1.1  riastrad #if defined(__i386__) || defined(__x86_64__)
    503  1.1  riastrad 		pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
    504  1.1  riastrad #else
    505  1.1  riastrad 		/* Ye gads this is ugly.  With more thought
    506  1.1  riastrad 		   we could move this up higher and use
    507  1.1  riastrad 		   `protection_map' instead.  */
    508  1.1  riastrad 		vma->vm_page_prot =
    509  1.1  riastrad 		    __pgprot(pte_val
    510  1.1  riastrad 			     (pte_wrprotect
    511  1.1  riastrad 			      (__pte(pgprot_val(vma->vm_page_prot)))));
    512  1.1  riastrad #endif
    513  1.1  riastrad 	}
    514  1.1  riastrad 
    515  1.1  riastrad 	vma->vm_ops = &drm_vm_dma_ops;
    516  1.1  riastrad 
    517  1.1  riastrad 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
    518  1.1  riastrad 
    519  1.1  riastrad 	drm_vm_open_locked(dev, vma);
    520  1.1  riastrad 	return 0;
    521  1.1  riastrad }
    522  1.1  riastrad 
    523  1.1  riastrad static resource_size_t drm_core_get_reg_ofs(struct drm_device *dev)
    524  1.1  riastrad {
    525  1.1  riastrad #ifdef __alpha__
    526  1.1  riastrad 	return dev->hose->dense_mem_base;
    527  1.1  riastrad #else
    528  1.1  riastrad 	return 0;
    529  1.1  riastrad #endif
    530  1.1  riastrad }
    531  1.1  riastrad 
    532  1.1  riastrad /**
    533  1.1  riastrad  * mmap DMA memory.
    534  1.1  riastrad  *
    535  1.1  riastrad  * \param file_priv DRM file private.
    536  1.1  riastrad  * \param vma virtual memory area.
    537  1.1  riastrad  * \return zero on success or a negative number on failure.
    538  1.1  riastrad  *
    539  1.1  riastrad  * If the virtual memory area has no offset associated with it then it's a DMA
    540  1.1  riastrad  * area, so calls mmap_dma(). Otherwise searches the map in drm_device::maplist,
    541  1.1  riastrad  * checks that the restricted flag is not set, sets the virtual memory operations
    542  1.1  riastrad  * according to the mapping type and remaps the pages. Finally sets the file
    543  1.1  riastrad  * pointer and calls vm_open().
    544  1.1  riastrad  */
    545  1.1  riastrad int drm_mmap_locked(struct file *filp, struct vm_area_struct *vma)
    546  1.1  riastrad {
    547  1.1  riastrad 	struct drm_file *priv = filp->private_data;
    548  1.1  riastrad 	struct drm_device *dev = priv->minor->dev;
    549  1.1  riastrad 	struct drm_local_map *map = NULL;
    550  1.1  riastrad 	resource_size_t offset = 0;
    551  1.1  riastrad 	struct drm_hash_item *hash;
    552  1.1  riastrad 
    553  1.1  riastrad 	DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
    554  1.1  riastrad 		  vma->vm_start, vma->vm_end, vma->vm_pgoff);
    555  1.1  riastrad 
    556  1.1  riastrad 	if (!priv->authenticated)
    557  1.1  riastrad 		return -EACCES;
    558  1.1  riastrad 
    559  1.1  riastrad 	/* We check for "dma". On Apple's UniNorth, it's valid to have
    560  1.1  riastrad 	 * the AGP mapped at physical address 0
    561  1.1  riastrad 	 * --BenH.
    562  1.1  riastrad 	 */
    563  1.1  riastrad 	if (!vma->vm_pgoff
    564  1.1  riastrad #if __OS_HAS_AGP
    565  1.1  riastrad 	    && (!dev->agp
    566  1.1  riastrad 		|| dev->agp->agp_info.device->vendor != PCI_VENDOR_ID_APPLE)
    567  1.1  riastrad #endif
    568  1.1  riastrad 	    )
    569  1.1  riastrad 		return drm_mmap_dma(filp, vma);
    570  1.1  riastrad 
    571  1.1  riastrad 	if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash)) {
    572  1.1  riastrad 		DRM_ERROR("Could not find map\n");
    573  1.1  riastrad 		return -EINVAL;
    574  1.1  riastrad 	}
    575  1.1  riastrad 
    576  1.1  riastrad 	map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
    577  1.1  riastrad 	if (!map || ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN)))
    578  1.1  riastrad 		return -EPERM;
    579  1.1  riastrad 
    580  1.1  riastrad 	/* Check for valid size. */
    581  1.1  riastrad 	if (map->size < vma->vm_end - vma->vm_start)
    582  1.1  riastrad 		return -EINVAL;
    583  1.1  riastrad 
    584  1.1  riastrad 	if (!capable(CAP_SYS_ADMIN) && (map->flags & _DRM_READ_ONLY)) {
    585  1.1  riastrad 		vma->vm_flags &= ~(VM_WRITE | VM_MAYWRITE);
    586  1.1  riastrad #if defined(__i386__) || defined(__x86_64__)
    587  1.1  riastrad 		pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
    588  1.1  riastrad #else
    589  1.1  riastrad 		/* Ye gads this is ugly.  With more thought
    590  1.1  riastrad 		   we could move this up higher and use
    591  1.1  riastrad 		   `protection_map' instead.  */
    592  1.1  riastrad 		vma->vm_page_prot =
    593  1.1  riastrad 		    __pgprot(pte_val
    594  1.1  riastrad 			     (pte_wrprotect
    595  1.1  riastrad 			      (__pte(pgprot_val(vma->vm_page_prot)))));
    596  1.1  riastrad #endif
    597  1.1  riastrad 	}
    598  1.1  riastrad 
    599  1.1  riastrad 	switch (map->type) {
    600  1.1  riastrad #if !defined(__arm__)
    601  1.1  riastrad 	case _DRM_AGP:
    602  1.1  riastrad 		if (drm_core_has_AGP(dev) && dev->agp->cant_use_aperture) {
    603  1.1  riastrad 			/*
    604  1.1  riastrad 			 * On some platforms we can't talk to bus dma address from the CPU, so for
    605  1.1  riastrad 			 * memory of type DRM_AGP, we'll deal with sorting out the real physical
    606  1.1  riastrad 			 * pages and mappings in fault()
    607  1.1  riastrad 			 */
    608  1.1  riastrad #if defined(__powerpc__)
    609  1.1  riastrad 			pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE;
    610  1.1  riastrad #endif
    611  1.1  riastrad 			vma->vm_ops = &drm_vm_ops;
    612  1.1  riastrad 			break;
    613  1.1  riastrad 		}
    614  1.1  riastrad 		/* fall through to _DRM_FRAME_BUFFER... */
    615  1.1  riastrad #endif
    616  1.1  riastrad 	case _DRM_FRAME_BUFFER:
    617  1.1  riastrad 	case _DRM_REGISTERS:
    618  1.1  riastrad 		offset = drm_core_get_reg_ofs(dev);
    619  1.1  riastrad 		vma->vm_flags |= VM_IO;	/* not in core dump */
    620  1.1  riastrad 		vma->vm_page_prot = drm_io_prot(map->type, vma);
    621  1.1  riastrad 		if (io_remap_pfn_range(vma, vma->vm_start,
    622  1.1  riastrad 				       (map->offset + offset) >> PAGE_SHIFT,
    623  1.1  riastrad 				       vma->vm_end - vma->vm_start,
    624  1.1  riastrad 				       vma->vm_page_prot))
    625  1.1  riastrad 			return -EAGAIN;
    626  1.1  riastrad 		DRM_DEBUG("   Type = %d; start = 0x%lx, end = 0x%lx,"
    627  1.1  riastrad 			  " offset = 0x%llx\n",
    628  1.1  riastrad 			  map->type,
    629  1.1  riastrad 			  vma->vm_start, vma->vm_end, (unsigned long long)(map->offset + offset));
    630  1.1  riastrad 
    631  1.1  riastrad 		vma->vm_ops = &drm_vm_ops;
    632  1.1  riastrad 		break;
    633  1.1  riastrad 	case _DRM_CONSISTENT:
    634  1.1  riastrad 		/* Consistent memory is really like shared memory. But
    635  1.1  riastrad 		 * it's allocated in a different way, so avoid fault */
    636  1.1  riastrad 		if (remap_pfn_range(vma, vma->vm_start,
    637  1.1  riastrad 		    page_to_pfn(virt_to_page(map->handle)),
    638  1.1  riastrad 		    vma->vm_end - vma->vm_start, vma->vm_page_prot))
    639  1.1  riastrad 			return -EAGAIN;
    640  1.1  riastrad 		vma->vm_page_prot = drm_dma_prot(map->type, vma);
    641  1.1  riastrad 	/* fall through to _DRM_SHM */
    642  1.1  riastrad 	case _DRM_SHM:
    643  1.1  riastrad 		vma->vm_ops = &drm_vm_shm_ops;
    644  1.1  riastrad 		vma->vm_private_data = (void *)map;
    645  1.1  riastrad 		break;
    646  1.1  riastrad 	case _DRM_SCATTER_GATHER:
    647  1.1  riastrad 		vma->vm_ops = &drm_vm_sg_ops;
    648  1.1  riastrad 		vma->vm_private_data = (void *)map;
    649  1.1  riastrad 		vma->vm_page_prot = drm_dma_prot(map->type, vma);
    650  1.1  riastrad 		break;
    651  1.1  riastrad 	default:
    652  1.1  riastrad 		return -EINVAL;	/* This should never happen. */
    653  1.1  riastrad 	}
    654  1.1  riastrad 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
    655  1.1  riastrad 
    656  1.1  riastrad 	drm_vm_open_locked(dev, vma);
    657  1.1  riastrad 	return 0;
    658  1.1  riastrad }
    659  1.1  riastrad 
    660  1.1  riastrad int drm_mmap(struct file *filp, struct vm_area_struct *vma)
    661  1.1  riastrad {
    662  1.1  riastrad 	struct drm_file *priv = filp->private_data;
    663  1.1  riastrad 	struct drm_device *dev = priv->minor->dev;
    664  1.1  riastrad 	int ret;
    665  1.1  riastrad 
    666  1.1  riastrad 	if (drm_device_is_unplugged(dev))
    667  1.1  riastrad 		return -ENODEV;
    668  1.1  riastrad 
    669  1.1  riastrad 	mutex_lock(&dev->struct_mutex);
    670  1.1  riastrad 	ret = drm_mmap_locked(filp, vma);
    671  1.1  riastrad 	mutex_unlock(&dev->struct_mutex);
    672  1.1  riastrad 
    673  1.1  riastrad 	return ret;
    674  1.1  riastrad }
    675  1.1  riastrad EXPORT_SYMBOL(drm_mmap);
    676