2 * Copyright (c) 2015 Michal Meloun
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
36 #include <machine/bus.h>
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>
43 #include <arm/nvidia/drm2/tegra_drm.h>
48 #include <vm/vm_pageout.h>
51 tegra_bo_destruct(struct tegra_bo *bo)
57 if (bo->cdev_pager == NULL)
60 size = round_page(bo->gem_obj.size);
62 pmap_qremove(bo->vbase, bo->npages);
64 VM_OBJECT_WLOCK(bo->cdev_pager);
65 for (i = 0; i < bo->npages; i++) {
67 vm_page_busy_acquire(m, 0);
68 cdev_pager_free_page(bo->cdev_pager, m);
69 m->flags &= ~PG_FICTITIOUS;
70 vm_page_unwire_noq(m);
73 VM_OBJECT_WUNLOCK(bo->cdev_pager);
75 vm_object_deallocate(bo->cdev_pager);
77 vmem_free(kmem_arena, bo->vbase, size);
81 tegra_bo_free_object(struct drm_gem_object *gem_obj)
85 bo = container_of(gem_obj, struct tegra_bo, gem_obj);
86 drm_gem_free_mmap_offset(gem_obj);
87 drm_gem_object_release(gem_obj);
89 tegra_bo_destruct(bo);
91 free(bo->m, DRM_MEM_DRIVER);
92 free(bo, DRM_MEM_DRIVER);
96 tegra_bo_alloc_contig(size_t npages, u_long alignment, vm_memattr_t memattr,
101 vm_paddr_t low, high, boundary;
108 m = vm_page_alloc_noobj_contig(VM_ALLOC_WIRED | VM_ALLOC_ZERO, npages,
109 low, high, alignment, boundary, memattr);
112 if (!vm_page_reclaim_contig(0, npages, low, high,
113 alignment, boundary))
121 for (i = 0; i < npages; i++, m++) {
122 m->valid = VM_PAGE_BITS_ALL;
129 /* Initialize pager and insert all object pages to it*/
131 tegra_bo_init_pager(struct tegra_bo *bo)
137 size = round_page(bo->gem_obj.size);
139 bo->pbase = VM_PAGE_TO_PHYS(bo->m[0]);
140 if (vmem_alloc(kmem_arena, size, M_WAITOK | M_BESTFIT, &bo->vbase))
143 VM_OBJECT_WLOCK(bo->cdev_pager);
144 for (i = 0; i < bo->npages; i++) {
147 * XXX This is a temporary hack.
148 * We need pager suitable for paging (mmap) managed
149 * real (non-fictitious) pages.
150 * - managed pages are needed for clean module unload.
151 * - aliasing fictitious page to real one is bad,
152 * pmap cannot handle this situation without issues
154 * paddr = PHYS_TO_VM_PAGE(VM_PAGE_TO_PHYS(paddr))
155 * for every single page passed to pmap.
157 m->oflags &= ~VPO_UNMANAGED;
158 m->flags |= PG_FICTITIOUS;
159 if (vm_page_insert(m, bo->cdev_pager, i) != 0)
162 VM_OBJECT_WUNLOCK(bo->cdev_pager);
164 pmap_qenter(bo->vbase, bo->m, bo->npages);
168 /* Allocate memory for frame buffer */
170 tegra_bo_alloc(struct drm_device *drm, struct tegra_bo *bo)
175 size = bo->gem_obj.size;
177 bo->npages = atop(size);
178 bo->m = malloc(sizeof(vm_page_t *) * bo->npages, DRM_MEM_DRIVER,
181 rv = tegra_bo_alloc_contig(bo->npages, PAGE_SIZE,
182 VM_MEMATTR_WRITE_COMBINING, &(bo->m));
184 DRM_WARNING("Cannot allocate memory for gem object.\n");
187 rv = tegra_bo_init_pager(bo);
189 DRM_WARNING("Cannot initialize gem object pager.\n");
196 tegra_bo_create(struct drm_device *drm, size_t size, struct tegra_bo **res_bo)
204 bo = malloc(sizeof(*bo), DRM_MEM_DRIVER, M_WAITOK | M_ZERO);
206 size = round_page(size);
207 rv = drm_gem_object_init(drm, &bo->gem_obj, size);
209 free(bo, DRM_MEM_DRIVER);
212 rv = drm_gem_create_mmap_offset(&bo->gem_obj);
214 drm_gem_object_release(&bo->gem_obj);
215 free(bo, DRM_MEM_DRIVER);
219 bo->cdev_pager = cdev_pager_allocate(&bo->gem_obj, OBJT_MGTDEVICE,
220 drm->driver->gem_pager_ops, size, 0, 0, NULL);
221 rv = tegra_bo_alloc(drm, bo);
223 tegra_bo_free_object(&bo->gem_obj);
232 tegra_bo_create_with_handle(struct drm_file *file, struct drm_device *drm,
233 size_t size, uint32_t *handle, struct tegra_bo **res_bo)
238 rv = tegra_bo_create(drm, size, &bo);
242 rv = drm_gem_handle_create(file, &bo->gem_obj, handle);
244 tegra_bo_free_object(&bo->gem_obj);
245 drm_gem_object_release(&bo->gem_obj);
249 drm_gem_object_unreference_unlocked(&bo->gem_obj);
256 tegra_bo_dumb_create(struct drm_file *file, struct drm_device *drm_dev,
257 struct drm_mode_create_dumb *args)
259 struct tegra_drm *drm;
263 drm = container_of(drm_dev, struct tegra_drm, drm_dev);
265 args->pitch= (args->width * args->bpp + 7) / 8;
266 args->pitch = roundup(args->pitch, drm->pitch_align);
267 args->size = args->pitch * args->height;
268 rv = tegra_bo_create_with_handle(file, drm_dev, args->size,
275 tegra_bo_dumb_map_offset(struct drm_file *file_priv,
276 struct drm_device *drm_dev, uint32_t handle, uint64_t *offset)
278 struct drm_gem_object *gem_obj;
282 gem_obj = drm_gem_object_lookup(drm_dev, file_priv, handle);
283 if (gem_obj == NULL) {
284 device_printf(drm_dev->dev, "Object not found\n");
288 rv = drm_gem_create_mmap_offset(gem_obj);
292 *offset = DRM_GEM_MAPPING_OFF(gem_obj->map_list.key) |
295 drm_gem_object_unreference(gem_obj);
300 drm_gem_object_unreference(gem_obj);
306 tegra_bo_dumb_destroy(struct drm_file *file_priv, struct drm_device *drm_dev,
311 rv = drm_gem_handle_delete(file_priv, handle);
319 tegra_gem_pager_fault(vm_object_t vm_obj, vm_ooffset_t offset, int prot,
323 #ifdef DRM_PAGER_DEBUG
324 DRM_DEBUG("object %p offset %jd prot %d mres %p\n",
325 vm_obj, (intmax_t)offset, prot, mres);
327 return (VM_PAGER_FAIL);
332 tegra_gem_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
333 vm_ooffset_t foff, struct ucred *cred, u_short *color)
342 tegra_gem_pager_dtor(void *handle)
347 static struct cdev_pager_ops tegra_gem_pager_ops = {
348 .cdev_pg_fault = tegra_gem_pager_fault,
349 .cdev_pg_ctor = tegra_gem_pager_ctor,
350 .cdev_pg_dtor = tegra_gem_pager_dtor
353 /* Fill up relevant fields in drm_driver ops */
355 tegra_bo_driver_register(struct drm_driver *drm_drv)
357 drm_drv->gem_free_object = tegra_bo_free_object;
358 drm_drv->gem_pager_ops = &tegra_gem_pager_ops;
359 drm_drv->dumb_create = tegra_bo_dumb_create;
360 drm_drv->dumb_map_offset = tegra_bo_dumb_map_offset;
361 drm_drv->dumb_destroy = tegra_bo_dumb_destroy;