162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * \file drm_vm.c
362306a36Sopenharmony_ci * Memory mapping for DRM
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * \author Rickard E. (Rik) Faith <faith@valinux.com>
662306a36Sopenharmony_ci * \author Gareth Hughes <gareth@valinux.com>
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci/*
1062306a36Sopenharmony_ci * Created: Mon Jan  4 08:58:31 1999 by faith@valinux.com
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
1362306a36Sopenharmony_ci * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
1462306a36Sopenharmony_ci * All Rights Reserved.
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
1762306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
1862306a36Sopenharmony_ci * to deal in the Software without restriction, including without limitation
1962306a36Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
2062306a36Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
2162306a36Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
2262306a36Sopenharmony_ci *
2362306a36Sopenharmony_ci * The above copyright notice and this permission notice (including the next
2462306a36Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
2562306a36Sopenharmony_ci * Software.
2662306a36Sopenharmony_ci *
2762306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2862306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2962306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
3062306a36Sopenharmony_ci * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
3162306a36Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
3262306a36Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
3362306a36Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
3462306a36Sopenharmony_ci */
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci#include <linux/export.h>
3762306a36Sopenharmony_ci#include <linux/pci.h>
3862306a36Sopenharmony_ci#include <linux/seq_file.h>
3962306a36Sopenharmony_ci#include <linux/vmalloc.h>
4062306a36Sopenharmony_ci#include <linux/pgtable.h>
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci#if defined(__ia64__)
4362306a36Sopenharmony_ci#include <linux/efi.h>
4462306a36Sopenharmony_ci#include <linux/slab.h>
4562306a36Sopenharmony_ci#endif
4662306a36Sopenharmony_ci#include <linux/mem_encrypt.h>
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci#include <drm/drm_device.h>
4962306a36Sopenharmony_ci#include <drm/drm_drv.h>
5062306a36Sopenharmony_ci#include <drm/drm_file.h>
5162306a36Sopenharmony_ci#include <drm/drm_framebuffer.h>
5262306a36Sopenharmony_ci#include <drm/drm_print.h>
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci#include "drm_internal.h"
5562306a36Sopenharmony_ci#include "drm_legacy.h"
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_cistruct drm_vma_entry {
5862306a36Sopenharmony_ci	struct list_head head;
5962306a36Sopenharmony_ci	struct vm_area_struct *vma;
6062306a36Sopenharmony_ci	pid_t pid;
6162306a36Sopenharmony_ci};
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_cistatic void drm_vm_open(struct vm_area_struct *vma);
6462306a36Sopenharmony_cistatic void drm_vm_close(struct vm_area_struct *vma);
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_cistatic pgprot_t drm_io_prot(struct drm_local_map *map,
6762306a36Sopenharmony_ci			    struct vm_area_struct *vma)
6862306a36Sopenharmony_ci{
6962306a36Sopenharmony_ci	pgprot_t tmp = vm_get_page_prot(vma->vm_flags);
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci#if defined(__i386__) || defined(__x86_64__) || defined(__powerpc__) || \
7262306a36Sopenharmony_ci    defined(__mips__) || defined(__loongarch__)
7362306a36Sopenharmony_ci	if (map->type == _DRM_REGISTERS && !(map->flags & _DRM_WRITE_COMBINING))
7462306a36Sopenharmony_ci		tmp = pgprot_noncached(tmp);
7562306a36Sopenharmony_ci	else
7662306a36Sopenharmony_ci		tmp = pgprot_writecombine(tmp);
7762306a36Sopenharmony_ci#elif defined(__ia64__)
7862306a36Sopenharmony_ci	if (efi_range_is_wc(vma->vm_start, vma->vm_end -
7962306a36Sopenharmony_ci				    vma->vm_start))
8062306a36Sopenharmony_ci		tmp = pgprot_writecombine(tmp);
8162306a36Sopenharmony_ci	else
8262306a36Sopenharmony_ci		tmp = pgprot_noncached(tmp);
8362306a36Sopenharmony_ci#elif defined(__sparc__) || defined(__arm__)
8462306a36Sopenharmony_ci	tmp = pgprot_noncached(tmp);
8562306a36Sopenharmony_ci#endif
8662306a36Sopenharmony_ci	return tmp;
8762306a36Sopenharmony_ci}
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_cistatic pgprot_t drm_dma_prot(uint32_t map_type, struct vm_area_struct *vma)
9062306a36Sopenharmony_ci{
9162306a36Sopenharmony_ci	pgprot_t tmp = vm_get_page_prot(vma->vm_flags);
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci#if defined(__powerpc__) && defined(CONFIG_NOT_COHERENT_CACHE)
9462306a36Sopenharmony_ci	tmp = pgprot_noncached_wc(tmp);
9562306a36Sopenharmony_ci#endif
9662306a36Sopenharmony_ci	return tmp;
9762306a36Sopenharmony_ci}
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci/*
10062306a36Sopenharmony_ci * \c fault method for AGP virtual memory.
10162306a36Sopenharmony_ci *
10262306a36Sopenharmony_ci * \param vma virtual memory area.
10362306a36Sopenharmony_ci * \param address access address.
10462306a36Sopenharmony_ci * \return pointer to the page structure.
10562306a36Sopenharmony_ci *
10662306a36Sopenharmony_ci * Find the right map and if it's AGP memory find the real physical page to
10762306a36Sopenharmony_ci * map, get the page, increment the use count and return it.
10862306a36Sopenharmony_ci */
10962306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_AGP)
11062306a36Sopenharmony_cistatic vm_fault_t drm_vm_fault(struct vm_fault *vmf)
11162306a36Sopenharmony_ci{
11262306a36Sopenharmony_ci	struct vm_area_struct *vma = vmf->vma;
11362306a36Sopenharmony_ci	struct drm_file *priv = vma->vm_file->private_data;
11462306a36Sopenharmony_ci	struct drm_device *dev = priv->minor->dev;
11562306a36Sopenharmony_ci	struct drm_local_map *map = NULL;
11662306a36Sopenharmony_ci	struct drm_map_list *r_list;
11762306a36Sopenharmony_ci	struct drm_hash_item *hash;
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	/*
12062306a36Sopenharmony_ci	 * Find the right map
12162306a36Sopenharmony_ci	 */
12262306a36Sopenharmony_ci	if (!dev->agp)
12362306a36Sopenharmony_ci		goto vm_fault_error;
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	if (!dev->agp || !dev->agp->cant_use_aperture)
12662306a36Sopenharmony_ci		goto vm_fault_error;
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci	if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash))
12962306a36Sopenharmony_ci		goto vm_fault_error;
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	r_list = drm_hash_entry(hash, struct drm_map_list, hash);
13262306a36Sopenharmony_ci	map = r_list->map;
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci	if (map && map->type == _DRM_AGP) {
13562306a36Sopenharmony_ci		/*
13662306a36Sopenharmony_ci		 * Using vm_pgoff as a selector forces us to use this unusual
13762306a36Sopenharmony_ci		 * addressing scheme.
13862306a36Sopenharmony_ci		 */
13962306a36Sopenharmony_ci		resource_size_t offset = vmf->address - vma->vm_start;
14062306a36Sopenharmony_ci		resource_size_t baddr = map->offset + offset;
14162306a36Sopenharmony_ci		struct drm_agp_mem *agpmem;
14262306a36Sopenharmony_ci		struct page *page;
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci#ifdef __alpha__
14562306a36Sopenharmony_ci		/*
14662306a36Sopenharmony_ci		 * Adjust to a bus-relative address
14762306a36Sopenharmony_ci		 */
14862306a36Sopenharmony_ci		baddr -= dev->hose->mem_space->start;
14962306a36Sopenharmony_ci#endif
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci		/*
15262306a36Sopenharmony_ci		 * It's AGP memory - find the real physical page to map
15362306a36Sopenharmony_ci		 */
15462306a36Sopenharmony_ci		list_for_each_entry(agpmem, &dev->agp->memory, head) {
15562306a36Sopenharmony_ci			if (agpmem->bound <= baddr &&
15662306a36Sopenharmony_ci			    agpmem->bound + agpmem->pages * PAGE_SIZE > baddr)
15762306a36Sopenharmony_ci				break;
15862306a36Sopenharmony_ci		}
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci		if (&agpmem->head == &dev->agp->memory)
16162306a36Sopenharmony_ci			goto vm_fault_error;
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci		/*
16462306a36Sopenharmony_ci		 * Get the page, inc the use count, and return it
16562306a36Sopenharmony_ci		 */
16662306a36Sopenharmony_ci		offset = (baddr - agpmem->bound) >> PAGE_SHIFT;
16762306a36Sopenharmony_ci		page = agpmem->memory->pages[offset];
16862306a36Sopenharmony_ci		get_page(page);
16962306a36Sopenharmony_ci		vmf->page = page;
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci		DRM_DEBUG
17262306a36Sopenharmony_ci		    ("baddr = 0x%llx page = 0x%p, offset = 0x%llx, count=%d\n",
17362306a36Sopenharmony_ci		     (unsigned long long)baddr,
17462306a36Sopenharmony_ci		     agpmem->memory->pages[offset],
17562306a36Sopenharmony_ci		     (unsigned long long)offset,
17662306a36Sopenharmony_ci		     page_count(page));
17762306a36Sopenharmony_ci		return 0;
17862306a36Sopenharmony_ci	}
17962306a36Sopenharmony_civm_fault_error:
18062306a36Sopenharmony_ci	return VM_FAULT_SIGBUS;	/* Disallow mremap */
18162306a36Sopenharmony_ci}
18262306a36Sopenharmony_ci#else
18362306a36Sopenharmony_cistatic vm_fault_t drm_vm_fault(struct vm_fault *vmf)
18462306a36Sopenharmony_ci{
18562306a36Sopenharmony_ci	return VM_FAULT_SIGBUS;
18662306a36Sopenharmony_ci}
18762306a36Sopenharmony_ci#endif
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci/*
19062306a36Sopenharmony_ci * \c nopage method for shared virtual memory.
19162306a36Sopenharmony_ci *
19262306a36Sopenharmony_ci * \param vma virtual memory area.
19362306a36Sopenharmony_ci * \param address access address.
19462306a36Sopenharmony_ci * \return pointer to the page structure.
19562306a36Sopenharmony_ci *
19662306a36Sopenharmony_ci * Get the mapping, find the real physical page to map, get the page, and
19762306a36Sopenharmony_ci * return it.
19862306a36Sopenharmony_ci */
19962306a36Sopenharmony_cistatic vm_fault_t drm_vm_shm_fault(struct vm_fault *vmf)
20062306a36Sopenharmony_ci{
20162306a36Sopenharmony_ci	struct vm_area_struct *vma = vmf->vma;
20262306a36Sopenharmony_ci	struct drm_local_map *map = vma->vm_private_data;
20362306a36Sopenharmony_ci	unsigned long offset;
20462306a36Sopenharmony_ci	unsigned long i;
20562306a36Sopenharmony_ci	struct page *page;
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci	if (!map)
20862306a36Sopenharmony_ci		return VM_FAULT_SIGBUS;	/* Nothing allocated */
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci	offset = vmf->address - vma->vm_start;
21162306a36Sopenharmony_ci	i = (unsigned long)map->handle + offset;
21262306a36Sopenharmony_ci	page = vmalloc_to_page((void *)i);
21362306a36Sopenharmony_ci	if (!page)
21462306a36Sopenharmony_ci		return VM_FAULT_SIGBUS;
21562306a36Sopenharmony_ci	get_page(page);
21662306a36Sopenharmony_ci	vmf->page = page;
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci	DRM_DEBUG("shm_fault 0x%lx\n", offset);
21962306a36Sopenharmony_ci	return 0;
22062306a36Sopenharmony_ci}
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci/*
22362306a36Sopenharmony_ci * \c close method for shared virtual memory.
22462306a36Sopenharmony_ci *
22562306a36Sopenharmony_ci * \param vma virtual memory area.
22662306a36Sopenharmony_ci *
22762306a36Sopenharmony_ci * Deletes map information if we are the last
22862306a36Sopenharmony_ci * person to close a mapping and it's not in the global maplist.
22962306a36Sopenharmony_ci */
23062306a36Sopenharmony_cistatic void drm_vm_shm_close(struct vm_area_struct *vma)
23162306a36Sopenharmony_ci{
23262306a36Sopenharmony_ci	struct drm_file *priv = vma->vm_file->private_data;
23362306a36Sopenharmony_ci	struct drm_device *dev = priv->minor->dev;
23462306a36Sopenharmony_ci	struct drm_vma_entry *pt, *temp;
23562306a36Sopenharmony_ci	struct drm_local_map *map;
23662306a36Sopenharmony_ci	struct drm_map_list *r_list;
23762306a36Sopenharmony_ci	int found_maps = 0;
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci	DRM_DEBUG("0x%08lx,0x%08lx\n",
24062306a36Sopenharmony_ci		  vma->vm_start, vma->vm_end - vma->vm_start);
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci	map = vma->vm_private_data;
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci	mutex_lock(&dev->struct_mutex);
24562306a36Sopenharmony_ci	list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
24662306a36Sopenharmony_ci		if (pt->vma->vm_private_data == map)
24762306a36Sopenharmony_ci			found_maps++;
24862306a36Sopenharmony_ci		if (pt->vma == vma) {
24962306a36Sopenharmony_ci			list_del(&pt->head);
25062306a36Sopenharmony_ci			kfree(pt);
25162306a36Sopenharmony_ci		}
25262306a36Sopenharmony_ci	}
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci	/* We were the only map that was found */
25562306a36Sopenharmony_ci	if (found_maps == 1 && map->flags & _DRM_REMOVABLE) {
25662306a36Sopenharmony_ci		/* Check to see if we are in the maplist, if we are not, then
25762306a36Sopenharmony_ci		 * we delete this mappings information.
25862306a36Sopenharmony_ci		 */
25962306a36Sopenharmony_ci		found_maps = 0;
26062306a36Sopenharmony_ci		list_for_each_entry(r_list, &dev->maplist, head) {
26162306a36Sopenharmony_ci			if (r_list->map == map)
26262306a36Sopenharmony_ci				found_maps++;
26362306a36Sopenharmony_ci		}
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci		if (!found_maps) {
26662306a36Sopenharmony_ci			switch (map->type) {
26762306a36Sopenharmony_ci			case _DRM_REGISTERS:
26862306a36Sopenharmony_ci			case _DRM_FRAME_BUFFER:
26962306a36Sopenharmony_ci				arch_phys_wc_del(map->mtrr);
27062306a36Sopenharmony_ci				iounmap(map->handle);
27162306a36Sopenharmony_ci				break;
27262306a36Sopenharmony_ci			case _DRM_SHM:
27362306a36Sopenharmony_ci				vfree(map->handle);
27462306a36Sopenharmony_ci				break;
27562306a36Sopenharmony_ci			case _DRM_AGP:
27662306a36Sopenharmony_ci			case _DRM_SCATTER_GATHER:
27762306a36Sopenharmony_ci				break;
27862306a36Sopenharmony_ci			case _DRM_CONSISTENT:
27962306a36Sopenharmony_ci				dma_free_coherent(dev->dev,
28062306a36Sopenharmony_ci						  map->size,
28162306a36Sopenharmony_ci						  map->handle,
28262306a36Sopenharmony_ci						  map->offset);
28362306a36Sopenharmony_ci				break;
28462306a36Sopenharmony_ci			}
28562306a36Sopenharmony_ci			kfree(map);
28662306a36Sopenharmony_ci		}
28762306a36Sopenharmony_ci	}
28862306a36Sopenharmony_ci	mutex_unlock(&dev->struct_mutex);
28962306a36Sopenharmony_ci}
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ci/*
29262306a36Sopenharmony_ci * \c fault method for DMA virtual memory.
29362306a36Sopenharmony_ci *
29462306a36Sopenharmony_ci * \param address access address.
29562306a36Sopenharmony_ci * \return pointer to the page structure.
29662306a36Sopenharmony_ci *
29762306a36Sopenharmony_ci * Determine the page number from the page offset and get it from drm_device_dma::pagelist.
29862306a36Sopenharmony_ci */
29962306a36Sopenharmony_cistatic vm_fault_t drm_vm_dma_fault(struct vm_fault *vmf)
30062306a36Sopenharmony_ci{
30162306a36Sopenharmony_ci	struct vm_area_struct *vma = vmf->vma;
30262306a36Sopenharmony_ci	struct drm_file *priv = vma->vm_file->private_data;
30362306a36Sopenharmony_ci	struct drm_device *dev = priv->minor->dev;
30462306a36Sopenharmony_ci	struct drm_device_dma *dma = dev->dma;
30562306a36Sopenharmony_ci	unsigned long offset;
30662306a36Sopenharmony_ci	unsigned long page_nr;
30762306a36Sopenharmony_ci	struct page *page;
30862306a36Sopenharmony_ci
30962306a36Sopenharmony_ci	if (!dma)
31062306a36Sopenharmony_ci		return VM_FAULT_SIGBUS;	/* Error */
31162306a36Sopenharmony_ci	if (!dma->pagelist)
31262306a36Sopenharmony_ci		return VM_FAULT_SIGBUS;	/* Nothing allocated */
31362306a36Sopenharmony_ci
31462306a36Sopenharmony_ci	offset = vmf->address - vma->vm_start;
31562306a36Sopenharmony_ci					/* vm_[pg]off[set] should be 0 */
31662306a36Sopenharmony_ci	page_nr = offset >> PAGE_SHIFT; /* page_nr could just be vmf->pgoff */
31762306a36Sopenharmony_ci	page = virt_to_page((void *)dma->pagelist[page_nr]);
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ci	get_page(page);
32062306a36Sopenharmony_ci	vmf->page = page;
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ci	DRM_DEBUG("dma_fault 0x%lx (page %lu)\n", offset, page_nr);
32362306a36Sopenharmony_ci	return 0;
32462306a36Sopenharmony_ci}
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_ci/*
32762306a36Sopenharmony_ci * \c fault method for scatter-gather virtual memory.
32862306a36Sopenharmony_ci *
32962306a36Sopenharmony_ci * \param address access address.
33062306a36Sopenharmony_ci * \return pointer to the page structure.
33162306a36Sopenharmony_ci *
33262306a36Sopenharmony_ci * Determine the map offset from the page offset and get it from drm_sg_mem::pagelist.
33362306a36Sopenharmony_ci */
33462306a36Sopenharmony_cistatic vm_fault_t drm_vm_sg_fault(struct vm_fault *vmf)
33562306a36Sopenharmony_ci{
33662306a36Sopenharmony_ci	struct vm_area_struct *vma = vmf->vma;
33762306a36Sopenharmony_ci	struct drm_local_map *map = vma->vm_private_data;
33862306a36Sopenharmony_ci	struct drm_file *priv = vma->vm_file->private_data;
33962306a36Sopenharmony_ci	struct drm_device *dev = priv->minor->dev;
34062306a36Sopenharmony_ci	struct drm_sg_mem *entry = dev->sg;
34162306a36Sopenharmony_ci	unsigned long offset;
34262306a36Sopenharmony_ci	unsigned long map_offset;
34362306a36Sopenharmony_ci	unsigned long page_offset;
34462306a36Sopenharmony_ci	struct page *page;
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci	if (!entry)
34762306a36Sopenharmony_ci		return VM_FAULT_SIGBUS;	/* Error */
34862306a36Sopenharmony_ci	if (!entry->pagelist)
34962306a36Sopenharmony_ci		return VM_FAULT_SIGBUS;	/* Nothing allocated */
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_ci	offset = vmf->address - vma->vm_start;
35262306a36Sopenharmony_ci	map_offset = map->offset - (unsigned long)dev->sg->virtual;
35362306a36Sopenharmony_ci	page_offset = (offset >> PAGE_SHIFT) + (map_offset >> PAGE_SHIFT);
35462306a36Sopenharmony_ci	page = entry->pagelist[page_offset];
35562306a36Sopenharmony_ci	get_page(page);
35662306a36Sopenharmony_ci	vmf->page = page;
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ci	return 0;
35962306a36Sopenharmony_ci}
36062306a36Sopenharmony_ci
36162306a36Sopenharmony_ci/** AGP virtual memory operations */
36262306a36Sopenharmony_cistatic const struct vm_operations_struct drm_vm_ops = {
36362306a36Sopenharmony_ci	.fault = drm_vm_fault,
36462306a36Sopenharmony_ci	.open = drm_vm_open,
36562306a36Sopenharmony_ci	.close = drm_vm_close,
36662306a36Sopenharmony_ci};
36762306a36Sopenharmony_ci
36862306a36Sopenharmony_ci/** Shared virtual memory operations */
36962306a36Sopenharmony_cistatic const struct vm_operations_struct drm_vm_shm_ops = {
37062306a36Sopenharmony_ci	.fault = drm_vm_shm_fault,
37162306a36Sopenharmony_ci	.open = drm_vm_open,
37262306a36Sopenharmony_ci	.close = drm_vm_shm_close,
37362306a36Sopenharmony_ci};
37462306a36Sopenharmony_ci
37562306a36Sopenharmony_ci/** DMA virtual memory operations */
37662306a36Sopenharmony_cistatic const struct vm_operations_struct drm_vm_dma_ops = {
37762306a36Sopenharmony_ci	.fault = drm_vm_dma_fault,
37862306a36Sopenharmony_ci	.open = drm_vm_open,
37962306a36Sopenharmony_ci	.close = drm_vm_close,
38062306a36Sopenharmony_ci};
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_ci/** Scatter-gather virtual memory operations */
38362306a36Sopenharmony_cistatic const struct vm_operations_struct drm_vm_sg_ops = {
38462306a36Sopenharmony_ci	.fault = drm_vm_sg_fault,
38562306a36Sopenharmony_ci	.open = drm_vm_open,
38662306a36Sopenharmony_ci	.close = drm_vm_close,
38762306a36Sopenharmony_ci};
38862306a36Sopenharmony_ci
38962306a36Sopenharmony_cistatic void drm_vm_open_locked(struct drm_device *dev,
39062306a36Sopenharmony_ci			       struct vm_area_struct *vma)
39162306a36Sopenharmony_ci{
39262306a36Sopenharmony_ci	struct drm_vma_entry *vma_entry;
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci	DRM_DEBUG("0x%08lx,0x%08lx\n",
39562306a36Sopenharmony_ci		  vma->vm_start, vma->vm_end - vma->vm_start);
39662306a36Sopenharmony_ci
39762306a36Sopenharmony_ci	vma_entry = kmalloc(sizeof(*vma_entry), GFP_KERNEL);
39862306a36Sopenharmony_ci	if (vma_entry) {
39962306a36Sopenharmony_ci		vma_entry->vma = vma;
40062306a36Sopenharmony_ci		vma_entry->pid = current->pid;
40162306a36Sopenharmony_ci		list_add(&vma_entry->head, &dev->vmalist);
40262306a36Sopenharmony_ci	}
40362306a36Sopenharmony_ci}
40462306a36Sopenharmony_ci
40562306a36Sopenharmony_cistatic void drm_vm_open(struct vm_area_struct *vma)
40662306a36Sopenharmony_ci{
40762306a36Sopenharmony_ci	struct drm_file *priv = vma->vm_file->private_data;
40862306a36Sopenharmony_ci	struct drm_device *dev = priv->minor->dev;
40962306a36Sopenharmony_ci
41062306a36Sopenharmony_ci	mutex_lock(&dev->struct_mutex);
41162306a36Sopenharmony_ci	drm_vm_open_locked(dev, vma);
41262306a36Sopenharmony_ci	mutex_unlock(&dev->struct_mutex);
41362306a36Sopenharmony_ci}
41462306a36Sopenharmony_ci
41562306a36Sopenharmony_cistatic void drm_vm_close_locked(struct drm_device *dev,
41662306a36Sopenharmony_ci				struct vm_area_struct *vma)
41762306a36Sopenharmony_ci{
41862306a36Sopenharmony_ci	struct drm_vma_entry *pt, *temp;
41962306a36Sopenharmony_ci
42062306a36Sopenharmony_ci	DRM_DEBUG("0x%08lx,0x%08lx\n",
42162306a36Sopenharmony_ci		  vma->vm_start, vma->vm_end - vma->vm_start);
42262306a36Sopenharmony_ci
42362306a36Sopenharmony_ci	list_for_each_entry_safe(pt, temp, &dev->vmalist, head) {
42462306a36Sopenharmony_ci		if (pt->vma == vma) {
42562306a36Sopenharmony_ci			list_del(&pt->head);
42662306a36Sopenharmony_ci			kfree(pt);
42762306a36Sopenharmony_ci			break;
42862306a36Sopenharmony_ci		}
42962306a36Sopenharmony_ci	}
43062306a36Sopenharmony_ci}
43162306a36Sopenharmony_ci
43262306a36Sopenharmony_ci/*
43362306a36Sopenharmony_ci * \c close method for all virtual memory types.
43462306a36Sopenharmony_ci *
43562306a36Sopenharmony_ci * \param vma virtual memory area.
43662306a36Sopenharmony_ci *
43762306a36Sopenharmony_ci * Search the \p vma private data entry in drm_device::vmalist, unlink it, and
43862306a36Sopenharmony_ci * free it.
43962306a36Sopenharmony_ci */
44062306a36Sopenharmony_cistatic void drm_vm_close(struct vm_area_struct *vma)
44162306a36Sopenharmony_ci{
44262306a36Sopenharmony_ci	struct drm_file *priv = vma->vm_file->private_data;
44362306a36Sopenharmony_ci	struct drm_device *dev = priv->minor->dev;
44462306a36Sopenharmony_ci
44562306a36Sopenharmony_ci	mutex_lock(&dev->struct_mutex);
44662306a36Sopenharmony_ci	drm_vm_close_locked(dev, vma);
44762306a36Sopenharmony_ci	mutex_unlock(&dev->struct_mutex);
44862306a36Sopenharmony_ci}
44962306a36Sopenharmony_ci
45062306a36Sopenharmony_ci/*
45162306a36Sopenharmony_ci * mmap DMA memory.
45262306a36Sopenharmony_ci *
45362306a36Sopenharmony_ci * \param file_priv DRM file private.
45462306a36Sopenharmony_ci * \param vma virtual memory area.
45562306a36Sopenharmony_ci * \return zero on success or a negative number on failure.
45662306a36Sopenharmony_ci *
45762306a36Sopenharmony_ci * Sets the virtual memory area operations structure to vm_dma_ops, the file
45862306a36Sopenharmony_ci * pointer, and calls vm_open().
45962306a36Sopenharmony_ci */
46062306a36Sopenharmony_cistatic int drm_mmap_dma(struct file *filp, struct vm_area_struct *vma)
46162306a36Sopenharmony_ci{
46262306a36Sopenharmony_ci	struct drm_file *priv = filp->private_data;
46362306a36Sopenharmony_ci	struct drm_device *dev;
46462306a36Sopenharmony_ci	struct drm_device_dma *dma;
46562306a36Sopenharmony_ci	unsigned long length = vma->vm_end - vma->vm_start;
46662306a36Sopenharmony_ci
46762306a36Sopenharmony_ci	dev = priv->minor->dev;
46862306a36Sopenharmony_ci	dma = dev->dma;
46962306a36Sopenharmony_ci	DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
47062306a36Sopenharmony_ci		  vma->vm_start, vma->vm_end, vma->vm_pgoff);
47162306a36Sopenharmony_ci
47262306a36Sopenharmony_ci	/* Length must match exact page count */
47362306a36Sopenharmony_ci	if (!dma || (length >> PAGE_SHIFT) != dma->page_count) {
47462306a36Sopenharmony_ci		return -EINVAL;
47562306a36Sopenharmony_ci	}
47662306a36Sopenharmony_ci
47762306a36Sopenharmony_ci	if (!capable(CAP_SYS_ADMIN) &&
47862306a36Sopenharmony_ci	    (dma->flags & _DRM_DMA_USE_PCI_RO)) {
47962306a36Sopenharmony_ci		vm_flags_clear(vma, VM_WRITE | VM_MAYWRITE);
48062306a36Sopenharmony_ci#if defined(__i386__) || defined(__x86_64__)
48162306a36Sopenharmony_ci		pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
48262306a36Sopenharmony_ci#else
48362306a36Sopenharmony_ci		/* Ye gads this is ugly.  With more thought
48462306a36Sopenharmony_ci		   we could move this up higher and use
48562306a36Sopenharmony_ci		   `protection_map' instead.  */
48662306a36Sopenharmony_ci		vma->vm_page_prot =
48762306a36Sopenharmony_ci		    __pgprot(pte_val
48862306a36Sopenharmony_ci			     (pte_wrprotect
48962306a36Sopenharmony_ci			      (__pte(pgprot_val(vma->vm_page_prot)))));
49062306a36Sopenharmony_ci#endif
49162306a36Sopenharmony_ci	}
49262306a36Sopenharmony_ci
49362306a36Sopenharmony_ci	vma->vm_ops = &drm_vm_dma_ops;
49462306a36Sopenharmony_ci
49562306a36Sopenharmony_ci	vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP);
49662306a36Sopenharmony_ci
49762306a36Sopenharmony_ci	drm_vm_open_locked(dev, vma);
49862306a36Sopenharmony_ci	return 0;
49962306a36Sopenharmony_ci}
50062306a36Sopenharmony_ci
50162306a36Sopenharmony_cistatic resource_size_t drm_core_get_reg_ofs(struct drm_device *dev)
50262306a36Sopenharmony_ci{
50362306a36Sopenharmony_ci#ifdef __alpha__
50462306a36Sopenharmony_ci	return dev->hose->dense_mem_base;
50562306a36Sopenharmony_ci#else
50662306a36Sopenharmony_ci	return 0;
50762306a36Sopenharmony_ci#endif
50862306a36Sopenharmony_ci}
50962306a36Sopenharmony_ci
51062306a36Sopenharmony_ci/*
51162306a36Sopenharmony_ci * mmap DMA memory.
51262306a36Sopenharmony_ci *
51362306a36Sopenharmony_ci * \param file_priv DRM file private.
51462306a36Sopenharmony_ci * \param vma virtual memory area.
51562306a36Sopenharmony_ci * \return zero on success or a negative number on failure.
51662306a36Sopenharmony_ci *
51762306a36Sopenharmony_ci * If the virtual memory area has no offset associated with it then it's a DMA
51862306a36Sopenharmony_ci * area, so calls mmap_dma(). Otherwise searches the map in drm_device::maplist,
51962306a36Sopenharmony_ci * checks that the restricted flag is not set, sets the virtual memory operations
52062306a36Sopenharmony_ci * according to the mapping type and remaps the pages. Finally sets the file
52162306a36Sopenharmony_ci * pointer and calls vm_open().
52262306a36Sopenharmony_ci */
52362306a36Sopenharmony_cistatic int drm_mmap_locked(struct file *filp, struct vm_area_struct *vma)
52462306a36Sopenharmony_ci{
52562306a36Sopenharmony_ci	struct drm_file *priv = filp->private_data;
52662306a36Sopenharmony_ci	struct drm_device *dev = priv->minor->dev;
52762306a36Sopenharmony_ci	struct drm_local_map *map = NULL;
52862306a36Sopenharmony_ci	resource_size_t offset = 0;
52962306a36Sopenharmony_ci	struct drm_hash_item *hash;
53062306a36Sopenharmony_ci
53162306a36Sopenharmony_ci	DRM_DEBUG("start = 0x%lx, end = 0x%lx, page offset = 0x%lx\n",
53262306a36Sopenharmony_ci		  vma->vm_start, vma->vm_end, vma->vm_pgoff);
53362306a36Sopenharmony_ci
53462306a36Sopenharmony_ci	if (!priv->authenticated)
53562306a36Sopenharmony_ci		return -EACCES;
53662306a36Sopenharmony_ci
53762306a36Sopenharmony_ci	/* We check for "dma". On Apple's UniNorth, it's valid to have
53862306a36Sopenharmony_ci	 * the AGP mapped at physical address 0
53962306a36Sopenharmony_ci	 * --BenH.
54062306a36Sopenharmony_ci	 */
54162306a36Sopenharmony_ci	if (!vma->vm_pgoff
54262306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_AGP)
54362306a36Sopenharmony_ci	    && (!dev->agp
54462306a36Sopenharmony_ci		|| dev->agp->agp_info.device->vendor != PCI_VENDOR_ID_APPLE)
54562306a36Sopenharmony_ci#endif
54662306a36Sopenharmony_ci	    )
54762306a36Sopenharmony_ci		return drm_mmap_dma(filp, vma);
54862306a36Sopenharmony_ci
54962306a36Sopenharmony_ci	if (drm_ht_find_item(&dev->map_hash, vma->vm_pgoff, &hash)) {
55062306a36Sopenharmony_ci		DRM_ERROR("Could not find map\n");
55162306a36Sopenharmony_ci		return -EINVAL;
55262306a36Sopenharmony_ci	}
55362306a36Sopenharmony_ci
55462306a36Sopenharmony_ci	map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
55562306a36Sopenharmony_ci	if (!map || ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN)))
55662306a36Sopenharmony_ci		return -EPERM;
55762306a36Sopenharmony_ci
55862306a36Sopenharmony_ci	/* Check for valid size. */
55962306a36Sopenharmony_ci	if (map->size < vma->vm_end - vma->vm_start)
56062306a36Sopenharmony_ci		return -EINVAL;
56162306a36Sopenharmony_ci
56262306a36Sopenharmony_ci	if (!capable(CAP_SYS_ADMIN) && (map->flags & _DRM_READ_ONLY)) {
56362306a36Sopenharmony_ci		vm_flags_clear(vma, VM_WRITE | VM_MAYWRITE);
56462306a36Sopenharmony_ci#if defined(__i386__) || defined(__x86_64__)
56562306a36Sopenharmony_ci		pgprot_val(vma->vm_page_prot) &= ~_PAGE_RW;
56662306a36Sopenharmony_ci#else
56762306a36Sopenharmony_ci		/* Ye gads this is ugly.  With more thought
56862306a36Sopenharmony_ci		   we could move this up higher and use
56962306a36Sopenharmony_ci		   `protection_map' instead.  */
57062306a36Sopenharmony_ci		vma->vm_page_prot =
57162306a36Sopenharmony_ci		    __pgprot(pte_val
57262306a36Sopenharmony_ci			     (pte_wrprotect
57362306a36Sopenharmony_ci			      (__pte(pgprot_val(vma->vm_page_prot)))));
57462306a36Sopenharmony_ci#endif
57562306a36Sopenharmony_ci	}
57662306a36Sopenharmony_ci
57762306a36Sopenharmony_ci	switch (map->type) {
57862306a36Sopenharmony_ci#if !defined(__arm__)
57962306a36Sopenharmony_ci	case _DRM_AGP:
58062306a36Sopenharmony_ci		if (dev->agp && dev->agp->cant_use_aperture) {
58162306a36Sopenharmony_ci			/*
58262306a36Sopenharmony_ci			 * On some platforms we can't talk to bus dma address from the CPU, so for
58362306a36Sopenharmony_ci			 * memory of type DRM_AGP, we'll deal with sorting out the real physical
58462306a36Sopenharmony_ci			 * pages and mappings in fault()
58562306a36Sopenharmony_ci			 */
58662306a36Sopenharmony_ci#if defined(__powerpc__)
58762306a36Sopenharmony_ci			vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
58862306a36Sopenharmony_ci#endif
58962306a36Sopenharmony_ci			vma->vm_ops = &drm_vm_ops;
59062306a36Sopenharmony_ci			break;
59162306a36Sopenharmony_ci		}
59262306a36Sopenharmony_ci		fallthrough;	/* to _DRM_FRAME_BUFFER... */
59362306a36Sopenharmony_ci#endif
59462306a36Sopenharmony_ci	case _DRM_FRAME_BUFFER:
59562306a36Sopenharmony_ci	case _DRM_REGISTERS:
59662306a36Sopenharmony_ci		offset = drm_core_get_reg_ofs(dev);
59762306a36Sopenharmony_ci		vma->vm_page_prot = drm_io_prot(map, vma);
59862306a36Sopenharmony_ci		if (io_remap_pfn_range(vma, vma->vm_start,
59962306a36Sopenharmony_ci				       (map->offset + offset) >> PAGE_SHIFT,
60062306a36Sopenharmony_ci				       vma->vm_end - vma->vm_start,
60162306a36Sopenharmony_ci				       vma->vm_page_prot))
60262306a36Sopenharmony_ci			return -EAGAIN;
60362306a36Sopenharmony_ci		DRM_DEBUG("   Type = %d; start = 0x%lx, end = 0x%lx,"
60462306a36Sopenharmony_ci			  " offset = 0x%llx\n",
60562306a36Sopenharmony_ci			  map->type,
60662306a36Sopenharmony_ci			  vma->vm_start, vma->vm_end, (unsigned long long)(map->offset + offset));
60762306a36Sopenharmony_ci
60862306a36Sopenharmony_ci		vma->vm_ops = &drm_vm_ops;
60962306a36Sopenharmony_ci		break;
61062306a36Sopenharmony_ci	case _DRM_CONSISTENT:
61162306a36Sopenharmony_ci		/* Consistent memory is really like shared memory. But
61262306a36Sopenharmony_ci		 * it's allocated in a different way, so avoid fault */
61362306a36Sopenharmony_ci		if (remap_pfn_range(vma, vma->vm_start,
61462306a36Sopenharmony_ci		    page_to_pfn(virt_to_page(map->handle)),
61562306a36Sopenharmony_ci		    vma->vm_end - vma->vm_start, vma->vm_page_prot))
61662306a36Sopenharmony_ci			return -EAGAIN;
61762306a36Sopenharmony_ci		vma->vm_page_prot = drm_dma_prot(map->type, vma);
61862306a36Sopenharmony_ci		fallthrough;	/* to _DRM_SHM */
61962306a36Sopenharmony_ci	case _DRM_SHM:
62062306a36Sopenharmony_ci		vma->vm_ops = &drm_vm_shm_ops;
62162306a36Sopenharmony_ci		vma->vm_private_data = (void *)map;
62262306a36Sopenharmony_ci		break;
62362306a36Sopenharmony_ci	case _DRM_SCATTER_GATHER:
62462306a36Sopenharmony_ci		vma->vm_ops = &drm_vm_sg_ops;
62562306a36Sopenharmony_ci		vma->vm_private_data = (void *)map;
62662306a36Sopenharmony_ci		vma->vm_page_prot = drm_dma_prot(map->type, vma);
62762306a36Sopenharmony_ci		break;
62862306a36Sopenharmony_ci	default:
62962306a36Sopenharmony_ci		return -EINVAL;	/* This should never happen. */
63062306a36Sopenharmony_ci	}
63162306a36Sopenharmony_ci	vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP);
63262306a36Sopenharmony_ci
63362306a36Sopenharmony_ci	drm_vm_open_locked(dev, vma);
63462306a36Sopenharmony_ci	return 0;
63562306a36Sopenharmony_ci}
63662306a36Sopenharmony_ci
63762306a36Sopenharmony_ciint drm_legacy_mmap(struct file *filp, struct vm_area_struct *vma)
63862306a36Sopenharmony_ci{
63962306a36Sopenharmony_ci	struct drm_file *priv = filp->private_data;
64062306a36Sopenharmony_ci	struct drm_device *dev = priv->minor->dev;
64162306a36Sopenharmony_ci	int ret;
64262306a36Sopenharmony_ci
64362306a36Sopenharmony_ci	if (drm_dev_is_unplugged(dev))
64462306a36Sopenharmony_ci		return -ENODEV;
64562306a36Sopenharmony_ci
64662306a36Sopenharmony_ci	mutex_lock(&dev->struct_mutex);
64762306a36Sopenharmony_ci	ret = drm_mmap_locked(filp, vma);
64862306a36Sopenharmony_ci	mutex_unlock(&dev->struct_mutex);
64962306a36Sopenharmony_ci
65062306a36Sopenharmony_ci	return ret;
65162306a36Sopenharmony_ci}
65262306a36Sopenharmony_ciEXPORT_SYMBOL(drm_legacy_mmap);
65362306a36Sopenharmony_ci
65462306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_DRM_LEGACY)
65562306a36Sopenharmony_civoid drm_legacy_vma_flush(struct drm_device *dev)
65662306a36Sopenharmony_ci{
65762306a36Sopenharmony_ci	struct drm_vma_entry *vma, *vma_temp;
65862306a36Sopenharmony_ci
65962306a36Sopenharmony_ci	/* Clear vma list (only needed for legacy drivers) */
66062306a36Sopenharmony_ci	list_for_each_entry_safe(vma, vma_temp, &dev->vmalist, head) {
66162306a36Sopenharmony_ci		list_del(&vma->head);
66262306a36Sopenharmony_ci		kfree(vma);
66362306a36Sopenharmony_ci	}
66462306a36Sopenharmony_ci}
66562306a36Sopenharmony_ci#endif
666