xref: /netbsd-src/sys/external/bsd/drm2/drm/drm_vm.c (revision 413d532bcc3f62d122e56d92e13ac64825a40baf)
1 /*	$NetBSD: drm_vm.c,v 1.2 2014/03/18 18:20:42 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Taylor R. Campbell.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: drm_vm.c,v 1.2 2014/03/18 18:20:42 riastradh Exp $");
34 
35 #include <sys/types.h>
36 #include <sys/conf.h>
37 
38 #include <uvm/uvm_extern.h>
39 #include <uvm/uvm_device.h>
40 
41 #include <drm/drmP.h>
42 
43 static paddr_t	drm_mmap_paddr_locked(struct drm_device *, off_t, int);
44 static paddr_t	drm_mmap_dma_paddr(struct drm_device *, off_t, int);
45 static paddr_t	drm_mmap_map_paddr(struct drm_device *, struct drm_local_map *,
46 		    off_t, int);
47 
48 int
49 drm_mmap_object(struct drm_device *dev, off_t offset, size_t size, int prot,
50     struct uvm_object **uobjp)
51 {
52 	dev_t devno = cdevsw_lookup_major(&drm_cdevsw);
53 	struct uvm_object *uobj;
54 
55 	uobj = udv_attach(&devno, prot, offset, size);
56 	if (uobj == NULL)
57 		return -EINVAL;
58 
59 	*uobjp = uobj;
60 	return 0;
61 }
62 
63 paddr_t
64 drm_mmap_paddr(struct drm_device *dev, off_t byte_offset, int prot)
65 {
66 	paddr_t paddr;
67 
68 	mutex_lock(&dev->struct_mutex);
69 	paddr = drm_mmap_paddr_locked(dev, byte_offset, prot);
70 	mutex_unlock(&dev->struct_mutex);
71 
72 	return paddr;
73 }
74 
75 static paddr_t
76 drm_mmap_paddr_locked(struct drm_device *dev, off_t byte_offset, int prot)
77 {
78 	const off_t page_offset = (byte_offset >> PAGE_SHIFT);
79 	struct drm_hash_item *hash;
80 
81 	KASSERT(mutex_is_locked(&dev->struct_mutex));
82 
83 	if (byte_offset != (byte_offset & ~(PAGE_SIZE-1)))
84 		return -1;
85 
86 	if ((dev->dma != NULL) &&
87 	    (0 <= byte_offset) &&
88 	    (byte_offset <= (dev->dma->page_count << PAGE_SHIFT)))
89 		return drm_mmap_dma_paddr(dev, byte_offset, prot);
90 
91 	if (drm_ht_find_item(&dev->map_hash, page_offset, &hash))
92 		return -1;
93 
94 	struct drm_local_map *const map = drm_hash_entry(hash,
95 	    struct drm_map_list, hash)->map;
96 	if (map == NULL)
97 		return -1;
98 
99 	/*
100 	 * XXX FreeBSD drops the mutex at this point, which would be
101 	 * nice, to allow sleeping in bus_dma cruft, but I don't know
102 	 * what guarantees the map will continue to exist.
103 	 */
104 
105 	if (ISSET(map->flags, _DRM_RESTRICTED) && !DRM_SUSER())
106 		return -1;
107 
108 	if (byte_offset < map->offset)
109 		return -1;
110 
111 	return drm_mmap_map_paddr(dev, map, (map->offset - byte_offset),
112 	    prot);
113 }
114 
115 static paddr_t
116 drm_mmap_dma_paddr(struct drm_device *dev, off_t byte_offset, int prot)
117 {
118 	const off_t page_offset = (byte_offset >> PAGE_SHIFT);
119 
120 	KASSERT(mutex_is_locked(&dev->struct_mutex));
121 
122 	if (dev->dma->pagelist == NULL)
123 		return (paddr_t)-1;
124 
125 	if (page_offset >= dev->dma->page_count)
126 		return (paddr_t)-1;
127 
128 	return dev->dma->pagelist[page_offset];
129 }
130 
131 static paddr_t
132 drm_mmap_map_paddr(struct drm_device *dev, struct drm_local_map *map,
133     off_t byte_offset, int prot)
134 {
135 	int flags = 0;
136 
137 	switch (map->type) {
138 	case _DRM_FRAME_BUFFER:
139 	case _DRM_AGP:
140 		flags |= BUS_SPACE_MAP_PREFETCHABLE;
141 		/* Fall through.  */
142 
143 	case _DRM_REGISTERS:
144 		flags |= BUS_SPACE_MAP_LINEAR; /* XXX Why?  */
145 
146 		return bus_space_mmap(dev->bst, map->offset, byte_offset, prot,
147 		    flags);
148 
149 	case _DRM_CONSISTENT: {
150 		struct drm_dma_handle *const dmah = map->lm_data.dmah;
151 
152 		return bus_dmamem_mmap(dev->dmat, &dmah->dmah_seg, 1,
153 		    byte_offset, prot,
154 		    /* XXX BUS_DMA_WAITOK?  We're holding a mutex...  */
155 		    /* XXX What else?  BUS_DMA_COHERENT?  */
156 		    (BUS_DMA_WAITOK | BUS_DMA_NOCACHE));
157 	}
158 
159 	case _DRM_SCATTER_GATHER: {
160 		struct drm_sg_mem *const sg = dev->sg;
161 
162 #if 0				/* XXX */
163 		KASSERT(sg == map->lm_data.sg);
164 #endif
165 
166 		return bus_dmamem_mmap(dev->dmat, sg->sg_segs, sg->sg_nsegs,
167 		    byte_offset, prot,
168 		    /* XXX BUS_DMA_WAITOK?  We're holding a mutex...  */
169 		    /* XXX What else?  BUS_DMA_COHERENT?  */
170 		    (BUS_DMA_WAITOK | BUS_DMA_NOCACHE));
171 	}
172 
173 	case _DRM_SHM:
174 	case _DRM_GEM:
175 	default:
176 		return (paddr_t)-1;
177 	}
178 }
179