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