xref: /freebsd/sys/arm/nvidia/drm2/tegra_bo.c (revision 036d2e814bf0f5d88ffb4b24c159320894541757)
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 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 
36 #include <machine/bus.h>
37 
38 #include <dev/extres/clk/clk.h>
39 #include <dev/drm2/drmP.h>
40 #include <dev/drm2/drm_crtc_helper.h>
41 #include <dev/drm2/drm_fb_helper.h>
42 
43 #include <arm/nvidia/drm2/tegra_drm.h>
44 
45 #include <sys/vmem.h>
46 #include <sys/vmem.h>
47 #include <vm/vm.h>
48 #include <vm/vm_pageout.h>
49 
50 static void
51 tegra_bo_destruct(struct tegra_bo *bo)
52 {
53 	vm_page_t m;
54 	size_t size;
55 	int i;
56 
57 	if (bo->cdev_pager == NULL)
58 		return;
59 
60 	size = round_page(bo->gem_obj.size);
61 	if (bo->vbase != 0)
62 		pmap_qremove(bo->vbase, bo->npages);
63 
64 	VM_OBJECT_WLOCK(bo->cdev_pager);
65 	for (i = 0; i < bo->npages; i++) {
66 		m = bo->m[i];
67 		cdev_pager_free_page(bo->cdev_pager, m);
68 		m->flags &= ~PG_FICTITIOUS;
69 		vm_page_unwire_noq(m);
70 		vm_page_free(m);
71 	}
72 	VM_OBJECT_WUNLOCK(bo->cdev_pager);
73 
74 	vm_object_deallocate(bo->cdev_pager);
75 	if (bo->vbase != 0)
76 		vmem_free(kmem_arena, bo->vbase, size);
77 }
78 
79 static void
80 tegra_bo_free_object(struct drm_gem_object *gem_obj)
81 {
82 	struct tegra_bo *bo;
83 
84 	bo = container_of(gem_obj, struct tegra_bo, gem_obj);
85 	drm_gem_free_mmap_offset(gem_obj);
86 	drm_gem_object_release(gem_obj);
87 
88 	tegra_bo_destruct(bo);
89 
90 	free(bo->m, DRM_MEM_DRIVER);
91 	free(bo, DRM_MEM_DRIVER);
92 }
93 
94 static int
95 tegra_bo_alloc_contig(size_t npages, u_long alignment, vm_memattr_t memattr,
96     vm_page_t **ret_page)
97 {
98 	vm_page_t m;
99 	int pflags, tries, i;
100 	vm_paddr_t low, high, boundary;
101 
102 	low = 0;
103 	high = -1UL;
104 	boundary = 0;
105 	pflags = VM_ALLOC_NORMAL  | VM_ALLOC_NOOBJ | VM_ALLOC_NOBUSY |
106 	    VM_ALLOC_WIRED | VM_ALLOC_ZERO;
107 	tries = 0;
108 retry:
109 	m = vm_page_alloc_contig(NULL, 0, pflags, npages, low, high, alignment,
110 	    boundary, memattr);
111 	if (m == NULL) {
112 		if (tries < 3) {
113 			if (!vm_page_reclaim_contig(pflags, npages, low, high,
114 			    alignment, boundary))
115 				vm_wait(NULL);
116 			tries++;
117 			goto retry;
118 		}
119 		return (ENOMEM);
120 	}
121 
122 	for (i = 0; i < npages; i++, m++) {
123 		if ((m->flags & PG_ZERO) == 0)
124 			pmap_zero_page(m);
125 		m->valid = VM_PAGE_BITS_ALL;
126 		(*ret_page)[i] = m;
127 	}
128 
129 	return (0);
130 }
131 
132 /* Initialize pager and insert all object pages to it*/
133 static int
134 tegra_bo_init_pager(struct tegra_bo *bo)
135 {
136 	vm_page_t m;
137 	size_t size;
138 	int i;
139 
140 	size = round_page(bo->gem_obj.size);
141 
142 	bo->pbase = VM_PAGE_TO_PHYS(bo->m[0]);
143 	if (vmem_alloc(kmem_arena, size, M_WAITOK | M_BESTFIT, &bo->vbase))
144 		return (ENOMEM);
145 
146 	VM_OBJECT_WLOCK(bo->cdev_pager);
147 	for (i = 0; i < bo->npages; i++) {
148 		m = bo->m[i];
149 		/*
150 		 * XXX This is a temporary hack.
151 		 * We need pager suitable for paging (mmap) managed
152 		 * real (non-fictitious) pages.
153 		 * - managed pages are needed for clean module unload.
154 		 * - aliasing fictitious page to real one is bad,
155 		 *   pmap cannot handle this situation without issues
156 		 *   It expects that
157 		 *    paddr = PHYS_TO_VM_PAGE(VM_PAGE_TO_PHYS(paddr))
158 		 *   for every single page passed to pmap.
159 		 */
160 		m->oflags &= ~VPO_UNMANAGED;
161 		m->flags |= PG_FICTITIOUS;
162 		if (vm_page_insert(m, bo->cdev_pager, i) != 0)
163 			return (EINVAL);
164 	}
165 	VM_OBJECT_WUNLOCK(bo->cdev_pager);
166 
167 	pmap_qenter(bo->vbase, bo->m, bo->npages);
168 	return (0);
169 }
170 
171 /* Allocate memory for frame buffer */
172 static int
173 tegra_bo_alloc(struct drm_device *drm, struct tegra_bo *bo)
174 {
175 	size_t size;
176 	int rv;
177 
178 	size = bo->gem_obj.size;
179 
180 	bo->npages = atop(size);
181 	bo->m = malloc(sizeof(vm_page_t *) * bo->npages, DRM_MEM_DRIVER,
182 	    M_WAITOK | M_ZERO);
183 
184 	rv = tegra_bo_alloc_contig(bo->npages, PAGE_SIZE,
185 	    VM_MEMATTR_WRITE_COMBINING, &(bo->m));
186 	if (rv != 0) {
187 		DRM_WARNING("Cannot allocate memory for gem object.\n");
188 		return (rv);
189 	}
190 	rv = tegra_bo_init_pager(bo);
191 	if (rv != 0) {
192 		DRM_WARNING("Cannot initialize gem object pager.\n");
193 		return (rv);
194 	}
195 	return (0);
196 }
197 
198 int
199 tegra_bo_create(struct drm_device *drm, size_t size, struct tegra_bo **res_bo)
200 {
201 	struct tegra_bo *bo;
202 	int rv;
203 
204 	if (size <= 0)
205 		return (-EINVAL);
206 
207 	bo = malloc(sizeof(*bo), DRM_MEM_DRIVER, M_WAITOK | M_ZERO);
208 
209 	size = round_page(size);
210 	rv = drm_gem_object_init(drm, &bo->gem_obj, size);
211 	if (rv != 0) {
212 		free(bo, DRM_MEM_DRIVER);
213 		return (rv);
214 	}
215 	rv = drm_gem_create_mmap_offset(&bo->gem_obj);
216 	if (rv != 0) {
217 		drm_gem_object_release(&bo->gem_obj);
218 		free(bo, DRM_MEM_DRIVER);
219 		return (rv);
220 	}
221 
222 	bo->cdev_pager = cdev_pager_allocate(&bo->gem_obj, OBJT_MGTDEVICE,
223 	    drm->driver->gem_pager_ops, size, 0, 0, NULL);
224 	rv = tegra_bo_alloc(drm, bo);
225 	if (rv != 0) {
226 		tegra_bo_free_object(&bo->gem_obj);
227 		return (rv);
228 	}
229 
230 	*res_bo = bo;
231 	return (0);
232 }
233 
234 
235 
236 static int
237 tegra_bo_create_with_handle(struct drm_file *file, struct drm_device *drm,
238     size_t size, uint32_t *handle, struct tegra_bo **res_bo)
239 {
240 	int rv;
241 	struct tegra_bo *bo;
242 
243 	rv = tegra_bo_create(drm, size, &bo);
244 	if (rv != 0)
245 		return (rv);
246 
247 	rv = drm_gem_handle_create(file, &bo->gem_obj, handle);
248 	if (rv != 0) {
249 		tegra_bo_free_object(&bo->gem_obj);
250 		drm_gem_object_release(&bo->gem_obj);
251 		return (rv);
252 	}
253 
254 	drm_gem_object_unreference_unlocked(&bo->gem_obj);
255 
256 	*res_bo = bo;
257 	return (0);
258 }
259 
260 static int
261 tegra_bo_dumb_create(struct drm_file *file, struct drm_device *drm_dev,
262     struct drm_mode_create_dumb *args)
263 {
264 	struct tegra_drm *drm;
265 	struct tegra_bo *bo;
266 	int rv;
267 
268 	drm = container_of(drm_dev, struct tegra_drm, drm_dev);
269 
270 	args->pitch= (args->width * args->bpp + 7) / 8;
271 	args->pitch = roundup(args->pitch, drm->pitch_align);
272 	args->size = args->pitch * args->height;
273 	rv = tegra_bo_create_with_handle(file, drm_dev, args->size,
274 	    &args->handle, &bo);
275 
276 	return (rv);
277 }
278 
279 static int
280 tegra_bo_dumb_map_offset(struct drm_file *file_priv,
281     struct drm_device *drm_dev, uint32_t handle, uint64_t *offset)
282 {
283 	struct drm_gem_object *gem_obj;
284 	int rv;
285 
286 	DRM_LOCK(drm_dev);
287 	gem_obj = drm_gem_object_lookup(drm_dev, file_priv, handle);
288 	if (gem_obj == NULL) {
289 		device_printf(drm_dev->dev, "Object not found\n");
290 		DRM_UNLOCK(drm_dev);
291 		return (-EINVAL);
292 	}
293 	rv = drm_gem_create_mmap_offset(gem_obj);
294 	if (rv != 0)
295 		goto fail;
296 
297 	*offset = DRM_GEM_MAPPING_OFF(gem_obj->map_list.key) |
298 	    DRM_GEM_MAPPING_KEY;
299 
300 	drm_gem_object_unreference(gem_obj);
301 	DRM_UNLOCK(drm_dev);
302 	return (0);
303 
304 fail:
305 	drm_gem_object_unreference(gem_obj);
306 	DRM_UNLOCK(drm_dev);
307 	return (rv);
308 }
309 
310 static int
311 tegra_bo_dumb_destroy(struct drm_file *file_priv, struct drm_device *drm_dev,
312     unsigned int handle)
313 {
314 	int rv;
315 
316 	rv = drm_gem_handle_delete(file_priv, handle);
317 	return (rv);
318 }
319 
320 /*
321  * mmap support
322  */
323 static int
324 tegra_gem_pager_fault(vm_object_t vm_obj, vm_ooffset_t offset, int prot,
325     vm_page_t *mres)
326 {
327 
328 #ifdef DRM_PAGER_DEBUG
329 	DRM_DEBUG("object %p offset %jd prot %d mres %p\n",
330 	    vm_obj, (intmax_t)offset, prot, mres);
331 #endif
332 	return (VM_PAGER_FAIL);
333 
334 }
335 
336 static int
337 tegra_gem_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
338     vm_ooffset_t foff, struct ucred *cred, u_short *color)
339 {
340 
341 	if (color != NULL)
342 		*color = 0;
343 	return (0);
344 }
345 
346 static void
347 tegra_gem_pager_dtor(void *handle)
348 {
349 
350 }
351 
352 static struct cdev_pager_ops tegra_gem_pager_ops = {
353 	.cdev_pg_fault = tegra_gem_pager_fault,
354 	.cdev_pg_ctor  = tegra_gem_pager_ctor,
355 	.cdev_pg_dtor  = tegra_gem_pager_dtor
356 };
357 
358 /* Fill up relevant fields in drm_driver ops */
359 void
360 tegra_bo_driver_register(struct drm_driver *drm_drv)
361 {
362 	drm_drv->gem_free_object = tegra_bo_free_object;
363 	drm_drv->gem_pager_ops = &tegra_gem_pager_ops;
364 	drm_drv->dumb_create = tegra_bo_dumb_create;
365 	drm_drv->dumb_map_offset = tegra_bo_dumb_map_offset;
366 	drm_drv->dumb_destroy = tegra_bo_dumb_destroy;
367 }
368