xref: /freebsd/sys/arm/nvidia/drm2/tegra_bo.c (revision edf8578117e8844e02c0121147f45e4609b30680)
1 /*-
2  * Copyright (c) 2015 Michal Meloun
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/bus.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 
34 #include <machine/bus.h>
35 
36 #include <dev/extres/clk/clk.h>
37 #include <dev/drm2/drmP.h>
38 #include <dev/drm2/drm_crtc_helper.h>
39 #include <dev/drm2/drm_fb_helper.h>
40 
41 #include <arm/nvidia/drm2/tegra_drm.h>
42 
43 #include <sys/vmem.h>
44 #include <sys/vmem.h>
45 #include <vm/vm.h>
46 #include <vm/vm_pageout.h>
47 
48 static void
49 tegra_bo_destruct(struct tegra_bo *bo)
50 {
51 	vm_page_t m;
52 	size_t size;
53 	int i;
54 
55 	if (bo->cdev_pager == NULL)
56 		return;
57 
58 	size = round_page(bo->gem_obj.size);
59 	if (bo->vbase != 0)
60 		pmap_qremove(bo->vbase, bo->npages);
61 
62 	VM_OBJECT_WLOCK(bo->cdev_pager);
63 	for (i = 0; i < bo->npages; i++) {
64 		m = bo->m[i];
65 		vm_page_busy_acquire(m, 0);
66 		cdev_pager_free_page(bo->cdev_pager, m);
67 		m->flags &= ~PG_FICTITIOUS;
68 		vm_page_unwire_noq(m);
69 		vm_page_free(m);
70 	}
71 	VM_OBJECT_WUNLOCK(bo->cdev_pager);
72 
73 	vm_object_deallocate(bo->cdev_pager);
74 	if (bo->vbase != 0)
75 		vmem_free(kmem_arena, bo->vbase, size);
76 }
77 
78 static void
79 tegra_bo_free_object(struct drm_gem_object *gem_obj)
80 {
81 	struct tegra_bo *bo;
82 
83 	bo = container_of(gem_obj, struct tegra_bo, gem_obj);
84 	drm_gem_free_mmap_offset(gem_obj);
85 	drm_gem_object_release(gem_obj);
86 
87 	tegra_bo_destruct(bo);
88 
89 	free(bo->m, DRM_MEM_DRIVER);
90 	free(bo, DRM_MEM_DRIVER);
91 }
92 
93 static int
94 tegra_bo_alloc_contig(size_t npages, u_long alignment, vm_memattr_t memattr,
95     vm_page_t **ret_page)
96 {
97 	vm_page_t m;
98 	int tries, i;
99 	vm_paddr_t low, high, boundary;
100 
101 	low = 0;
102 	high = -1UL;
103 	boundary = 0;
104 	tries = 0;
105 retry:
106 	m = vm_page_alloc_noobj_contig(VM_ALLOC_WIRED | VM_ALLOC_ZERO, npages,
107 	    low, high, alignment, boundary, memattr);
108 	if (m == NULL) {
109 		if (tries < 3) {
110 			if (!vm_page_reclaim_contig(0, npages, low, high,
111 			    alignment, boundary))
112 				vm_wait(NULL);
113 			tries++;
114 			goto retry;
115 		}
116 		return (ENOMEM);
117 	}
118 
119 	for (i = 0; i < npages; i++, m++) {
120 		m->valid = VM_PAGE_BITS_ALL;
121 		(*ret_page)[i] = m;
122 	}
123 
124 	return (0);
125 }
126 
127 /* Initialize pager and insert all object pages to it*/
128 static int
129 tegra_bo_init_pager(struct tegra_bo *bo)
130 {
131 	vm_page_t m;
132 	size_t size;
133 	int i;
134 
135 	size = round_page(bo->gem_obj.size);
136 
137 	bo->pbase = VM_PAGE_TO_PHYS(bo->m[0]);
138 	if (vmem_alloc(kmem_arena, size, M_WAITOK | M_BESTFIT, &bo->vbase))
139 		return (ENOMEM);
140 
141 	VM_OBJECT_WLOCK(bo->cdev_pager);
142 	for (i = 0; i < bo->npages; i++) {
143 		m = bo->m[i];
144 		/*
145 		 * XXX This is a temporary hack.
146 		 * We need pager suitable for paging (mmap) managed
147 		 * real (non-fictitious) pages.
148 		 * - managed pages are needed for clean module unload.
149 		 * - aliasing fictitious page to real one is bad,
150 		 *   pmap cannot handle this situation without issues
151 		 *   It expects that
152 		 *    paddr = PHYS_TO_VM_PAGE(VM_PAGE_TO_PHYS(paddr))
153 		 *   for every single page passed to pmap.
154 		 */
155 		m->oflags &= ~VPO_UNMANAGED;
156 		m->flags |= PG_FICTITIOUS;
157 		if (vm_page_insert(m, bo->cdev_pager, i) != 0)
158 			return (EINVAL);
159 	}
160 	VM_OBJECT_WUNLOCK(bo->cdev_pager);
161 
162 	pmap_qenter(bo->vbase, bo->m, bo->npages);
163 	return (0);
164 }
165 
166 /* Allocate memory for frame buffer */
167 static int
168 tegra_bo_alloc(struct drm_device *drm, struct tegra_bo *bo)
169 {
170 	size_t size;
171 	int rv;
172 
173 	size = bo->gem_obj.size;
174 
175 	bo->npages = atop(size);
176 	bo->m = malloc(sizeof(vm_page_t *) * bo->npages, DRM_MEM_DRIVER,
177 	    M_WAITOK | M_ZERO);
178 
179 	rv = tegra_bo_alloc_contig(bo->npages, PAGE_SIZE,
180 	    VM_MEMATTR_WRITE_COMBINING, &(bo->m));
181 	if (rv != 0) {
182 		DRM_WARNING("Cannot allocate memory for gem object.\n");
183 		return (rv);
184 	}
185 	rv = tegra_bo_init_pager(bo);
186 	if (rv != 0) {
187 		DRM_WARNING("Cannot initialize gem object pager.\n");
188 		return (rv);
189 	}
190 	return (0);
191 }
192 
193 int
194 tegra_bo_create(struct drm_device *drm, size_t size, struct tegra_bo **res_bo)
195 {
196 	struct tegra_bo *bo;
197 	int rv;
198 
199 	if (size <= 0)
200 		return (-EINVAL);
201 
202 	bo = malloc(sizeof(*bo), DRM_MEM_DRIVER, M_WAITOK | M_ZERO);
203 
204 	size = round_page(size);
205 	rv = drm_gem_object_init(drm, &bo->gem_obj, size);
206 	if (rv != 0) {
207 		free(bo, DRM_MEM_DRIVER);
208 		return (rv);
209 	}
210 	rv = drm_gem_create_mmap_offset(&bo->gem_obj);
211 	if (rv != 0) {
212 		drm_gem_object_release(&bo->gem_obj);
213 		free(bo, DRM_MEM_DRIVER);
214 		return (rv);
215 	}
216 
217 	bo->cdev_pager = cdev_pager_allocate(&bo->gem_obj, OBJT_MGTDEVICE,
218 	    drm->driver->gem_pager_ops, size, 0, 0, NULL);
219 	rv = tegra_bo_alloc(drm, bo);
220 	if (rv != 0) {
221 		tegra_bo_free_object(&bo->gem_obj);
222 		return (rv);
223 	}
224 
225 	*res_bo = bo;
226 	return (0);
227 }
228 
229 static int
230 tegra_bo_create_with_handle(struct drm_file *file, struct drm_device *drm,
231     size_t size, uint32_t *handle, struct tegra_bo **res_bo)
232 {
233 	int rv;
234 	struct tegra_bo *bo;
235 
236 	rv = tegra_bo_create(drm, size, &bo);
237 	if (rv != 0)
238 		return (rv);
239 
240 	rv = drm_gem_handle_create(file, &bo->gem_obj, handle);
241 	if (rv != 0) {
242 		tegra_bo_free_object(&bo->gem_obj);
243 		drm_gem_object_release(&bo->gem_obj);
244 		return (rv);
245 	}
246 
247 	drm_gem_object_unreference_unlocked(&bo->gem_obj);
248 
249 	*res_bo = bo;
250 	return (0);
251 }
252 
253 static int
254 tegra_bo_dumb_create(struct drm_file *file, struct drm_device *drm_dev,
255     struct drm_mode_create_dumb *args)
256 {
257 	struct tegra_drm *drm;
258 	struct tegra_bo *bo;
259 	int rv;
260 
261 	drm = container_of(drm_dev, struct tegra_drm, drm_dev);
262 
263 	args->pitch= (args->width * args->bpp + 7) / 8;
264 	args->pitch = roundup(args->pitch, drm->pitch_align);
265 	args->size = args->pitch * args->height;
266 	rv = tegra_bo_create_with_handle(file, drm_dev, args->size,
267 	    &args->handle, &bo);
268 
269 	return (rv);
270 }
271 
272 static int
273 tegra_bo_dumb_map_offset(struct drm_file *file_priv,
274     struct drm_device *drm_dev, uint32_t handle, uint64_t *offset)
275 {
276 	struct drm_gem_object *gem_obj;
277 	int rv;
278 
279 	DRM_LOCK(drm_dev);
280 	gem_obj = drm_gem_object_lookup(drm_dev, file_priv, handle);
281 	if (gem_obj == NULL) {
282 		device_printf(drm_dev->dev, "Object not found\n");
283 		DRM_UNLOCK(drm_dev);
284 		return (-EINVAL);
285 	}
286 	rv = drm_gem_create_mmap_offset(gem_obj);
287 	if (rv != 0)
288 		goto fail;
289 
290 	*offset = DRM_GEM_MAPPING_OFF(gem_obj->map_list.key) |
291 	    DRM_GEM_MAPPING_KEY;
292 
293 	drm_gem_object_unreference(gem_obj);
294 	DRM_UNLOCK(drm_dev);
295 	return (0);
296 
297 fail:
298 	drm_gem_object_unreference(gem_obj);
299 	DRM_UNLOCK(drm_dev);
300 	return (rv);
301 }
302 
303 static int
304 tegra_bo_dumb_destroy(struct drm_file *file_priv, struct drm_device *drm_dev,
305     unsigned int handle)
306 {
307 	int rv;
308 
309 	rv = drm_gem_handle_delete(file_priv, handle);
310 	return (rv);
311 }
312 
313 /*
314  * mmap support
315  */
316 static int
317 tegra_gem_pager_fault(vm_object_t vm_obj, vm_ooffset_t offset, int prot,
318     vm_page_t *mres)
319 {
320 
321 #ifdef DRM_PAGER_DEBUG
322 	DRM_DEBUG("object %p offset %jd prot %d mres %p\n",
323 	    vm_obj, (intmax_t)offset, prot, mres);
324 #endif
325 	return (VM_PAGER_FAIL);
326 
327 }
328 
329 static int
330 tegra_gem_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
331     vm_ooffset_t foff, struct ucred *cred, u_short *color)
332 {
333 
334 	if (color != NULL)
335 		*color = 0;
336 	return (0);
337 }
338 
339 static void
340 tegra_gem_pager_dtor(void *handle)
341 {
342 
343 }
344 
345 static struct cdev_pager_ops tegra_gem_pager_ops = {
346 	.cdev_pg_fault = tegra_gem_pager_fault,
347 	.cdev_pg_ctor  = tegra_gem_pager_ctor,
348 	.cdev_pg_dtor  = tegra_gem_pager_dtor
349 };
350 
351 /* Fill up relevant fields in drm_driver ops */
352 void
353 tegra_bo_driver_register(struct drm_driver *drm_drv)
354 {
355 	drm_drv->gem_free_object = tegra_bo_free_object;
356 	drm_drv->gem_pager_ops = &tegra_gem_pager_ops;
357 	drm_drv->dumb_create = tegra_bo_dumb_create;
358 	drm_drv->dumb_map_offset = tegra_bo_dumb_map_offset;
359 	drm_drv->dumb_destroy = tegra_bo_dumb_destroy;
360 }
361