xref: /dflybsd-src/sys/dev/drm/drm_vm.c (revision 789731325bde747251c28a37e0a00ed4efb88c46)
17f3c3d6fSHasso Tepper /*-
27f3c3d6fSHasso Tepper  * Copyright 2003 Eric Anholt
37f3c3d6fSHasso Tepper  * All Rights Reserved.
47f3c3d6fSHasso Tepper  *
57f3c3d6fSHasso Tepper  * Permission is hereby granted, free of charge, to any person obtaining a
67f3c3d6fSHasso Tepper  * copy of this software and associated documentation files (the "Software"),
77f3c3d6fSHasso Tepper  * to deal in the Software without restriction, including without limitation
87f3c3d6fSHasso Tepper  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
97f3c3d6fSHasso Tepper  * and/or sell copies of the Software, and to permit persons to whom the
107f3c3d6fSHasso Tepper  * Software is furnished to do so, subject to the following conditions:
117f3c3d6fSHasso Tepper  *
127f3c3d6fSHasso Tepper  * The above copyright notice and this permission notice (including the next
137f3c3d6fSHasso Tepper  * paragraph) shall be included in all copies or substantial portions of the
147f3c3d6fSHasso Tepper  * Software.
157f3c3d6fSHasso Tepper  *
167f3c3d6fSHasso Tepper  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
177f3c3d6fSHasso Tepper  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
187f3c3d6fSHasso Tepper  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
197f3c3d6fSHasso Tepper  * ERIC ANHOLT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
207f3c3d6fSHasso Tepper  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
217f3c3d6fSHasso Tepper  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
225718399fSFrançois Tigeot  *
235718399fSFrançois Tigeot  * $FreeBSD: head/sys/dev/drm2/drm_vm.c 235783 2012-05-22 11:07:44Z kib $"
247f3c3d6fSHasso Tepper  */
257f3c3d6fSHasso Tepper 
267f3c3d6fSHasso Tepper /** @file drm_vm.c
277f3c3d6fSHasso Tepper  * Support code for mmaping of DRM maps.
287f3c3d6fSHasso Tepper  */
297f3c3d6fSHasso Tepper 
3018e26a6dSFrançois Tigeot #include <drm/drmP.h>
31c4bb8e01SFrançois Tigeot #include <linux/export.h>
32c4bb8e01SFrançois Tigeot #include <linux/seq_file.h>
33c4bb8e01SFrançois Tigeot #if defined(__ia64__)
34c4bb8e01SFrançois Tigeot #include <linux/efi.h>
35c4bb8e01SFrançois Tigeot #include <linux/slab.h>
36c4bb8e01SFrançois Tigeot #endif
373f2dd94aSFrançois Tigeot #include <linux/mem_encrypt.h>
381b13d190SFrançois Tigeot #include <asm/pgtable.h>
39c4bb8e01SFrançois Tigeot #include "drm_internal.h"
401b13d190SFrançois Tigeot #include "drm_legacy.h"
417f3c3d6fSHasso Tepper 
42c4bb8e01SFrançois Tigeot #include <sys/mutex2.h>
43c4bb8e01SFrançois Tigeot 
44*78973132SSergey Zigachev int
drm_mmap(struct dev_mmap_args * ap)45*78973132SSergey Zigachev drm_mmap(struct dev_mmap_args *ap)
467f3c3d6fSHasso Tepper {
47c4bb8e01SFrançois Tigeot 	struct file *filp = ap->a_fp;
48*78973132SSergey Zigachev 	struct drm_file *priv;
497f3c3d6fSHasso Tepper 	struct cdev *kdev = ap->a_head.a_dev;
507f3c3d6fSHasso Tepper 	vm_offset_t offset = ap->a_offset;
51b3705d71SHasso Tepper 	struct drm_device *dev = drm_get_device_from_kdev(kdev);
52f599cd46SFrançois Tigeot 	struct drm_local_map *map = NULL;
535c002123SFrançois Tigeot 	struct drm_hash_item *hash;
54b3705d71SHasso Tepper 	enum drm_map_type type;
557f3c3d6fSHasso Tepper 	vm_paddr_t phys;
567f3c3d6fSHasso Tepper 
57*78973132SSergey Zigachev 	/*
58*78973132SSergey Zigachev 	 * NOTE: If ddev->drm_ttm_bdev is not setup properly, this path
59*78973132SSergey Zigachev 	 *	 may be hit with a NULL filp and panic.
60*78973132SSergey Zigachev 	 */
61*78973132SSergey Zigachev 	priv = filp->private_data;
62c4bb8e01SFrançois Tigeot 	if (!priv->authenticated)
63c4bb8e01SFrançois Tigeot 		return -EACCES;
647f3c3d6fSHasso Tepper 
655718399fSFrançois Tigeot 	DRM_DEBUG("called with offset %016jx\n", (uintmax_t)offset);
66b3705d71SHasso Tepper 	if (dev->dma && offset < ptoa(dev->dma->page_count)) {
674250aa95Szrj 		struct drm_device_dma *dma = dev->dma;
687f3c3d6fSHasso Tepper 
697f3c3d6fSHasso Tepper 		if (dma->pagelist != NULL) {
707f3c3d6fSHasso Tepper 			unsigned long page = offset >> PAGE_SHIFT;
717f3c3d6fSHasso Tepper 			unsigned long phys = dma->pagelist[page];
725718399fSFrançois Tigeot 
735718399fSFrançois Tigeot 			// XXX *paddr = phys;
745718399fSFrançois Tigeot 			ap->a_result = phys;
75b3705d71SHasso Tepper 			return 0;
767f3c3d6fSHasso Tepper 		} else {
777f3c3d6fSHasso Tepper 			return -1;
787f3c3d6fSHasso Tepper 		}
797f3c3d6fSHasso Tepper 	}
807f3c3d6fSHasso Tepper 
817f3c3d6fSHasso Tepper 	/* A sequential search of a linked list is
827f3c3d6fSHasso Tepper 	   fine here because: 1) there will only be
837f3c3d6fSHasso Tepper 	   about 5-10 entries in the list and, 2) a
847f3c3d6fSHasso Tepper 	   DRI client only has to do this mapping
857f3c3d6fSHasso Tepper 	   once, so it doesn't have to be optimized
867f3c3d6fSHasso Tepper 	   for performance, even if the list was a
8799f70504SFrançois Tigeot 	   bit longer.
8899f70504SFrançois Tigeot 	*/
895718399fSFrançois Tigeot 	DRM_LOCK(dev);
905c002123SFrançois Tigeot 
915c002123SFrançois Tigeot 	if (drm_ht_find_item(&dev->map_hash, offset, &hash)) {
925c002123SFrançois Tigeot 		DRM_ERROR("Could not find map\n");
93*78973132SSergey Zigachev 		DRM_UNLOCK(dev);
945c002123SFrançois Tigeot 		return -EINVAL;
95b36dbd34SSascha Wildner 	}
967f3c3d6fSHasso Tepper 
975c002123SFrançois Tigeot 	map = drm_hash_entry(hash, struct drm_map_list, hash)->map;
987f3c3d6fSHasso Tepper 	if (map == NULL) {
995718399fSFrançois Tigeot 		DRM_DEBUG("Can't find map, request offset = %016jx\n",
1005718399fSFrançois Tigeot 		    (uintmax_t)offset);
1015718399fSFrançois Tigeot 		DRM_UNLOCK(dev);
1027f3c3d6fSHasso Tepper 		return -1;
1037f3c3d6fSHasso Tepper 	}
104c6f73aabSFrançois Tigeot 	if (((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) {
1055718399fSFrançois Tigeot 		DRM_UNLOCK(dev);
1067f3c3d6fSHasso Tepper 		DRM_DEBUG("restricted map\n");
1077f3c3d6fSHasso Tepper 		return -1;
1087f3c3d6fSHasso Tepper 	}
1095c002123SFrançois Tigeot 
1107f3c3d6fSHasso Tepper 	type = map->type;
1115718399fSFrançois Tigeot 	DRM_UNLOCK(dev);
1127f3c3d6fSHasso Tepper 
1137f3c3d6fSHasso Tepper 	switch (type) {
1147f3c3d6fSHasso Tepper 	case _DRM_FRAME_BUFFER:
1157f3c3d6fSHasso Tepper 	case _DRM_AGP:
11699f70504SFrançois Tigeot #if 0	/* XXX */
11799f70504SFrançois Tigeot 		*memattr = VM_MEMATTR_WRITE_COMBINING;
11899f70504SFrançois Tigeot #endif
11999f70504SFrançois Tigeot 		/* FALLTHROUGH */
12099f70504SFrançois Tigeot 	case _DRM_REGISTERS:
12199f70504SFrançois Tigeot 		phys = map->offset + offset;
1227f3c3d6fSHasso Tepper 		break;
1237f3c3d6fSHasso Tepper 	case _DRM_SCATTER_GATHER:
12499f70504SFrançois Tigeot #if 0	/* XXX */
12599f70504SFrançois Tigeot 		*memattr = VM_MEMATTR_WRITE_COMBINING;
12699f70504SFrançois Tigeot #endif
12799f70504SFrançois Tigeot 		/* FALLTHROUGH */
12899f70504SFrançois Tigeot 	case _DRM_CONSISTENT:
1297f3c3d6fSHasso Tepper 	case _DRM_SHM:
1305c002123SFrançois Tigeot 		phys = vtophys((char *)map->handle + offset);
1317f3c3d6fSHasso Tepper 		break;
1327f3c3d6fSHasso Tepper 	default:
1337f3c3d6fSHasso Tepper 		DRM_ERROR("bad map type %d\n", type);
1347f3c3d6fSHasso Tepper 		return -1;	/* This should never happen. */
1357f3c3d6fSHasso Tepper 	}
1367f3c3d6fSHasso Tepper 
1377f3c3d6fSHasso Tepper 	ap->a_result = atop(phys);
138b3705d71SHasso Tepper 	return 0;
1397f3c3d6fSHasso Tepper }
1407f3c3d6fSHasso Tepper 
1415718399fSFrançois Tigeot /* XXX The following is just temporary hack to replace the
1425718399fSFrançois Tigeot  * vm_phys_fictitious functions available on FreeBSD
1435718399fSFrançois Tigeot  */
1445718399fSFrançois Tigeot #define VM_PHYS_FICTITIOUS_NSEGS        8
1455718399fSFrançois Tigeot static struct vm_phys_fictitious_seg {
1465718399fSFrançois Tigeot         vm_paddr_t      start;
1475718399fSFrançois Tigeot         vm_paddr_t      end;
1485718399fSFrançois Tigeot         vm_page_t       first_page;
1495718399fSFrançois Tigeot } vm_phys_fictitious_segs[VM_PHYS_FICTITIOUS_NSEGS];
150cabfc9f6SMatthew Dillon static struct mtx vm_phys_fictitious_reg_mtx = MTX_INITIALIZER("vmphy");
1515718399fSFrançois Tigeot 
1525718399fSFrançois Tigeot vm_page_t
vm_phys_fictitious_to_vm_page(vm_paddr_t pa)1535718399fSFrançois Tigeot vm_phys_fictitious_to_vm_page(vm_paddr_t pa)
1545718399fSFrançois Tigeot {
1555718399fSFrançois Tigeot         struct vm_phys_fictitious_seg *seg;
1565718399fSFrançois Tigeot         vm_page_t m;
1575718399fSFrançois Tigeot         int segind;
1585718399fSFrançois Tigeot 
1595718399fSFrançois Tigeot         m = NULL;
1605718399fSFrançois Tigeot         for (segind = 0; segind < VM_PHYS_FICTITIOUS_NSEGS; segind++) {
1615718399fSFrançois Tigeot                 seg = &vm_phys_fictitious_segs[segind];
1625718399fSFrançois Tigeot                 if (pa >= seg->start && pa < seg->end) {
1635718399fSFrançois Tigeot                         m = &seg->first_page[atop(pa - seg->start)];
1645718399fSFrançois Tigeot                         KASSERT((m->flags & PG_FICTITIOUS) != 0,
1655718399fSFrançois Tigeot                             ("%p not fictitious", m));
1665718399fSFrançois Tigeot                         break;
1675718399fSFrançois Tigeot                 }
1685718399fSFrançois Tigeot         }
1695718399fSFrançois Tigeot         return (m);
1705718399fSFrançois Tigeot }
1715718399fSFrançois Tigeot 
1725718399fSFrançois Tigeot int
vm_phys_fictitious_reg_range(vm_paddr_t start,vm_paddr_t end,vm_memattr_t memattr)1733023924aSFrançois Tigeot vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
1743023924aSFrançois Tigeot 			     vm_memattr_t memattr)
1755718399fSFrançois Tigeot {
1765718399fSFrançois Tigeot         struct vm_phys_fictitious_seg *seg;
1775718399fSFrançois Tigeot         vm_page_t fp;
1785718399fSFrançois Tigeot         long i, page_count;
1795718399fSFrançois Tigeot         int segind;
1805718399fSFrançois Tigeot 
1815718399fSFrançois Tigeot         page_count = (end - start) / PAGE_SIZE;
1825718399fSFrançois Tigeot 
1835a3b77d5SFrançois Tigeot         fp = kmalloc(page_count * sizeof(struct vm_page), M_DRM,
1845718399fSFrançois Tigeot                     M_WAITOK | M_ZERO);
1855718399fSFrançois Tigeot 
1865718399fSFrançois Tigeot         for (i = 0; i < page_count; i++) {
1873023924aSFrançois Tigeot 		vm_page_initfake(&fp[i], start + PAGE_SIZE * i, memattr);
188bc0aa189SMatthew Dillon 		atomic_clear_int(&fp[i].busy_count, PBUSY_LOCKED);
1895718399fSFrançois Tigeot         }
1905718399fSFrançois Tigeot         mtx_lock(&vm_phys_fictitious_reg_mtx);
1915718399fSFrançois Tigeot         for (segind = 0; segind < VM_PHYS_FICTITIOUS_NSEGS; segind++) {
1925718399fSFrançois Tigeot                 seg = &vm_phys_fictitious_segs[segind];
1935718399fSFrançois Tigeot                 if (seg->start == 0 && seg->end == 0) {
1945718399fSFrançois Tigeot                         seg->start = start;
1955718399fSFrançois Tigeot                         seg->end = end;
1965718399fSFrançois Tigeot                         seg->first_page = fp;
1975718399fSFrançois Tigeot                         mtx_unlock(&vm_phys_fictitious_reg_mtx);
1985718399fSFrançois Tigeot                         return (0);
1995718399fSFrançois Tigeot                 }
2005718399fSFrançois Tigeot         }
2015718399fSFrançois Tigeot         mtx_unlock(&vm_phys_fictitious_reg_mtx);
202158486a6SFrançois Tigeot         kfree(fp);
2035718399fSFrançois Tigeot         return (EBUSY);
2045718399fSFrançois Tigeot }
205ab30917fSFrançois Tigeot 
206ab30917fSFrançois Tigeot void
vm_phys_fictitious_unreg_range(vm_paddr_t start,vm_paddr_t end)207ab30917fSFrançois Tigeot vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end)
208ab30917fSFrançois Tigeot {
209ab30917fSFrançois Tigeot 	struct vm_phys_fictitious_seg *seg;
210ab30917fSFrançois Tigeot 	vm_page_t fp;
211ab30917fSFrançois Tigeot 	int segind;
212ab30917fSFrançois Tigeot 
213ab30917fSFrançois Tigeot 	mtx_lock(&vm_phys_fictitious_reg_mtx);
214ab30917fSFrançois Tigeot 	for (segind = 0; segind < VM_PHYS_FICTITIOUS_NSEGS; segind++) {
215ab30917fSFrançois Tigeot 		seg = &vm_phys_fictitious_segs[segind];
216ab30917fSFrançois Tigeot 		if (seg->start == start && seg->end == end) {
217ab30917fSFrançois Tigeot 			seg->start = seg->end = 0;
218ab30917fSFrançois Tigeot 			fp = seg->first_page;
219ab30917fSFrançois Tigeot 			seg->first_page = NULL;
220ab30917fSFrançois Tigeot 			mtx_unlock(&vm_phys_fictitious_reg_mtx);
221158486a6SFrançois Tigeot 			kfree(fp);
222ab30917fSFrançois Tigeot 			return;
223ab30917fSFrançois Tigeot 		}
224ab30917fSFrançois Tigeot 	}
225ab30917fSFrançois Tigeot 	mtx_unlock(&vm_phys_fictitious_reg_mtx);
226ab30917fSFrançois Tigeot 	KASSERT(0, ("Unregistering not registered fictitious range"));
227ab30917fSFrançois Tigeot }
228