1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* exynos_drm_gem.c 3 * 4 * Copyright (c) 2011 Samsung Electronics Co., Ltd. 5 * Author: Inki Dae <inki.dae@samsung.com> 6 */ 7 8 9 #include <linux/dma-buf.h> 10 #include <linux/pfn_t.h> 11 #include <linux/shmem_fs.h> 12 13 #include <drm/drm_prime.h> 14 #include <drm/drm_vma_manager.h> 15 #include <drm/exynos_drm.h> 16 17 #include "exynos_drm_drv.h" 18 #include "exynos_drm_gem.h" 19 20 static int exynos_drm_alloc_buf(struct exynos_drm_gem *exynos_gem, bool kvmap) 21 { 22 struct drm_device *dev = exynos_gem->base.dev; 23 unsigned long attr = 0; 24 25 if (exynos_gem->dma_addr) { 26 DRM_DEV_DEBUG_KMS(to_dma_dev(dev), "already allocated.\n"); 27 return 0; 28 } 29 30 /* 31 * if EXYNOS_BO_CONTIG, fully physically contiguous memory 32 * region will be allocated else physically contiguous 33 * as possible. 34 */ 35 if (!(exynos_gem->flags & EXYNOS_BO_NONCONTIG)) 36 attr |= DMA_ATTR_FORCE_CONTIGUOUS; 37 38 /* 39 * if EXYNOS_BO_WC or EXYNOS_BO_NONCACHABLE, writecombine mapping 40 * else cachable mapping. 41 */ 42 if (exynos_gem->flags & EXYNOS_BO_WC || 43 !(exynos_gem->flags & EXYNOS_BO_CACHABLE)) 44 attr |= DMA_ATTR_WRITE_COMBINE; 45 else 46 attr |= DMA_ATTR_NON_CONSISTENT; 47 48 /* FBDev emulation requires kernel mapping */ 49 if (!kvmap) 50 attr |= DMA_ATTR_NO_KERNEL_MAPPING; 51 52 exynos_gem->dma_attrs = attr; 53 exynos_gem->cookie = dma_alloc_attrs(to_dma_dev(dev), exynos_gem->size, 54 &exynos_gem->dma_addr, GFP_KERNEL, 55 exynos_gem->dma_attrs); 56 if (!exynos_gem->cookie) { 57 DRM_DEV_ERROR(to_dma_dev(dev), "failed to allocate buffer.\n"); 58 return -ENOMEM; 59 } 60 61 if (kvmap) 62 exynos_gem->kvaddr = exynos_gem->cookie; 63 64 DRM_DEV_DEBUG_KMS(to_dma_dev(dev), "dma_addr(0x%lx), size(0x%lx)\n", 65 (unsigned long)exynos_gem->dma_addr, exynos_gem->size); 66 return 0; 67 } 68 69 static void exynos_drm_free_buf(struct exynos_drm_gem *exynos_gem) 70 { 71 struct drm_device *dev = exynos_gem->base.dev; 72 73 if (!exynos_gem->dma_addr) { 74 DRM_DEV_DEBUG_KMS(dev->dev, "dma_addr is invalid.\n"); 75 return; 76 } 77 78 DRM_DEV_DEBUG_KMS(dev->dev, "dma_addr(0x%lx), size(0x%lx)\n", 79 (unsigned long)exynos_gem->dma_addr, exynos_gem->size); 80 81 dma_free_attrs(to_dma_dev(dev), exynos_gem->size, exynos_gem->cookie, 82 (dma_addr_t)exynos_gem->dma_addr, 83 exynos_gem->dma_attrs); 84 } 85 86 static int exynos_drm_gem_handle_create(struct drm_gem_object *obj, 87 struct drm_file *file_priv, 88 unsigned int *handle) 89 { 90 int ret; 91 92 /* 93 * allocate a id of idr table where the obj is registered 94 * and handle has the id what user can see. 95 */ 96 ret = drm_gem_handle_create(file_priv, obj, handle); 97 if (ret) 98 return ret; 99 100 DRM_DEV_DEBUG_KMS(to_dma_dev(obj->dev), "gem handle = 0x%x\n", *handle); 101 102 /* drop reference from allocate - handle holds it now. */ 103 drm_gem_object_put_unlocked(obj); 104 105 return 0; 106 } 107 108 void exynos_drm_gem_destroy(struct exynos_drm_gem *exynos_gem) 109 { 110 struct drm_gem_object *obj = &exynos_gem->base; 111 112 DRM_DEV_DEBUG_KMS(to_dma_dev(obj->dev), "handle count = %d\n", 113 obj->handle_count); 114 115 /* 116 * do not release memory region from exporter. 117 * 118 * the region will be released by exporter 119 * once dmabuf's refcount becomes 0. 120 */ 121 if (obj->import_attach) 122 drm_prime_gem_destroy(obj, exynos_gem->sgt); 123 else 124 exynos_drm_free_buf(exynos_gem); 125 126 /* release file pointer to gem object. */ 127 drm_gem_object_release(obj); 128 129 kfree(exynos_gem); 130 } 131 132 static struct exynos_drm_gem *exynos_drm_gem_init(struct drm_device *dev, 133 unsigned long size) 134 { 135 struct exynos_drm_gem *exynos_gem; 136 struct drm_gem_object *obj; 137 int ret; 138 139 exynos_gem = kzalloc(sizeof(*exynos_gem), GFP_KERNEL); 140 if (!exynos_gem) 141 return ERR_PTR(-ENOMEM); 142 143 exynos_gem->size = size; 144 obj = &exynos_gem->base; 145 146 ret = drm_gem_object_init(dev, obj, size); 147 if (ret < 0) { 148 DRM_DEV_ERROR(dev->dev, "failed to initialize gem object\n"); 149 kfree(exynos_gem); 150 return ERR_PTR(ret); 151 } 152 153 ret = drm_gem_create_mmap_offset(obj); 154 if (ret < 0) { 155 drm_gem_object_release(obj); 156 kfree(exynos_gem); 157 return ERR_PTR(ret); 158 } 159 160 DRM_DEV_DEBUG_KMS(dev->dev, "created file object = %pK\n", obj->filp); 161 162 return exynos_gem; 163 } 164 165 struct exynos_drm_gem *exynos_drm_gem_create(struct drm_device *dev, 166 unsigned int flags, 167 unsigned long size, 168 bool kvmap) 169 { 170 struct exynos_drm_gem *exynos_gem; 171 int ret; 172 173 if (flags & ~(EXYNOS_BO_MASK)) { 174 DRM_DEV_ERROR(dev->dev, 175 "invalid GEM buffer flags: %u\n", flags); 176 return ERR_PTR(-EINVAL); 177 } 178 179 if (!size) { 180 DRM_DEV_ERROR(dev->dev, "invalid GEM buffer size: %lu\n", size); 181 return ERR_PTR(-EINVAL); 182 } 183 184 size = roundup(size, PAGE_SIZE); 185 186 exynos_gem = exynos_drm_gem_init(dev, size); 187 if (IS_ERR(exynos_gem)) 188 return exynos_gem; 189 190 if (!is_drm_iommu_supported(dev) && (flags & EXYNOS_BO_NONCONTIG)) { 191 /* 192 * when no IOMMU is available, all allocated buffers are 193 * contiguous anyway, so drop EXYNOS_BO_NONCONTIG flag 194 */ 195 flags &= ~EXYNOS_BO_NONCONTIG; 196 DRM_WARN("Non-contiguous allocation is not supported without IOMMU, falling back to contiguous buffer\n"); 197 } 198 199 /* set memory type and cache attribute from user side. */ 200 exynos_gem->flags = flags; 201 202 ret = exynos_drm_alloc_buf(exynos_gem, kvmap); 203 if (ret < 0) { 204 drm_gem_object_release(&exynos_gem->base); 205 kfree(exynos_gem); 206 return ERR_PTR(ret); 207 } 208 209 return exynos_gem; 210 } 211 212 int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data, 213 struct drm_file *file_priv) 214 { 215 struct drm_exynos_gem_create *args = data; 216 struct exynos_drm_gem *exynos_gem; 217 int ret; 218 219 exynos_gem = exynos_drm_gem_create(dev, args->flags, args->size, false); 220 if (IS_ERR(exynos_gem)) 221 return PTR_ERR(exynos_gem); 222 223 ret = exynos_drm_gem_handle_create(&exynos_gem->base, file_priv, 224 &args->handle); 225 if (ret) { 226 exynos_drm_gem_destroy(exynos_gem); 227 return ret; 228 } 229 230 return 0; 231 } 232 233 int exynos_drm_gem_map_ioctl(struct drm_device *dev, void *data, 234 struct drm_file *file_priv) 235 { 236 struct drm_exynos_gem_map *args = data; 237 238 return drm_gem_dumb_map_offset(file_priv, dev, args->handle, 239 &args->offset); 240 } 241 242 struct exynos_drm_gem *exynos_drm_gem_get(struct drm_file *filp, 243 unsigned int gem_handle) 244 { 245 struct drm_gem_object *obj; 246 247 obj = drm_gem_object_lookup(filp, gem_handle); 248 if (!obj) 249 return NULL; 250 return to_exynos_gem(obj); 251 } 252 253 static int exynos_drm_gem_mmap_buffer(struct exynos_drm_gem *exynos_gem, 254 struct vm_area_struct *vma) 255 { 256 struct drm_device *drm_dev = exynos_gem->base.dev; 257 unsigned long vm_size; 258 int ret; 259 260 vma->vm_flags &= ~VM_PFNMAP; 261 vma->vm_pgoff = 0; 262 263 vm_size = vma->vm_end - vma->vm_start; 264 265 /* check if user-requested size is valid. */ 266 if (vm_size > exynos_gem->size) 267 return -EINVAL; 268 269 ret = dma_mmap_attrs(to_dma_dev(drm_dev), vma, exynos_gem->cookie, 270 exynos_gem->dma_addr, exynos_gem->size, 271 exynos_gem->dma_attrs); 272 if (ret < 0) { 273 DRM_ERROR("failed to mmap.\n"); 274 return ret; 275 } 276 277 return 0; 278 } 279 280 int exynos_drm_gem_get_ioctl(struct drm_device *dev, void *data, 281 struct drm_file *file_priv) 282 { 283 struct exynos_drm_gem *exynos_gem; 284 struct drm_exynos_gem_info *args = data; 285 struct drm_gem_object *obj; 286 287 obj = drm_gem_object_lookup(file_priv, args->handle); 288 if (!obj) { 289 DRM_DEV_ERROR(dev->dev, "failed to lookup gem object.\n"); 290 return -EINVAL; 291 } 292 293 exynos_gem = to_exynos_gem(obj); 294 295 args->flags = exynos_gem->flags; 296 args->size = exynos_gem->size; 297 298 drm_gem_object_put_unlocked(obj); 299 300 return 0; 301 } 302 303 void exynos_drm_gem_free_object(struct drm_gem_object *obj) 304 { 305 exynos_drm_gem_destroy(to_exynos_gem(obj)); 306 } 307 308 int exynos_drm_gem_dumb_create(struct drm_file *file_priv, 309 struct drm_device *dev, 310 struct drm_mode_create_dumb *args) 311 { 312 struct exynos_drm_gem *exynos_gem; 313 unsigned int flags; 314 int ret; 315 316 /* 317 * allocate memory to be used for framebuffer. 318 * - this callback would be called by user application 319 * with DRM_IOCTL_MODE_CREATE_DUMB command. 320 */ 321 322 args->pitch = args->width * ((args->bpp + 7) / 8); 323 args->size = args->pitch * args->height; 324 325 if (is_drm_iommu_supported(dev)) 326 flags = EXYNOS_BO_NONCONTIG | EXYNOS_BO_WC; 327 else 328 flags = EXYNOS_BO_CONTIG | EXYNOS_BO_WC; 329 330 exynos_gem = exynos_drm_gem_create(dev, flags, args->size, false); 331 if (IS_ERR(exynos_gem)) { 332 dev_warn(dev->dev, "FB allocation failed.\n"); 333 return PTR_ERR(exynos_gem); 334 } 335 336 ret = exynos_drm_gem_handle_create(&exynos_gem->base, file_priv, 337 &args->handle); 338 if (ret) { 339 exynos_drm_gem_destroy(exynos_gem); 340 return ret; 341 } 342 343 return 0; 344 } 345 346 static int exynos_drm_gem_mmap_obj(struct drm_gem_object *obj, 347 struct vm_area_struct *vma) 348 { 349 struct exynos_drm_gem *exynos_gem = to_exynos_gem(obj); 350 int ret; 351 352 DRM_DEV_DEBUG_KMS(to_dma_dev(obj->dev), "flags = 0x%x\n", 353 exynos_gem->flags); 354 355 /* non-cachable as default. */ 356 if (exynos_gem->flags & EXYNOS_BO_CACHABLE) 357 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); 358 else if (exynos_gem->flags & EXYNOS_BO_WC) 359 vma->vm_page_prot = 360 pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); 361 else 362 vma->vm_page_prot = 363 pgprot_noncached(vm_get_page_prot(vma->vm_flags)); 364 365 ret = exynos_drm_gem_mmap_buffer(exynos_gem, vma); 366 if (ret) 367 goto err_close_vm; 368 369 return ret; 370 371 err_close_vm: 372 drm_gem_vm_close(vma); 373 374 return ret; 375 } 376 377 int exynos_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma) 378 { 379 struct drm_gem_object *obj; 380 int ret; 381 382 /* set vm_area_struct. */ 383 ret = drm_gem_mmap(filp, vma); 384 if (ret < 0) { 385 DRM_ERROR("failed to mmap.\n"); 386 return ret; 387 } 388 389 obj = vma->vm_private_data; 390 391 if (obj->import_attach) 392 return dma_buf_mmap(obj->dma_buf, vma, 0); 393 394 return exynos_drm_gem_mmap_obj(obj, vma); 395 } 396 397 /* low-level interface prime helpers */ 398 struct drm_gem_object *exynos_drm_gem_prime_import(struct drm_device *dev, 399 struct dma_buf *dma_buf) 400 { 401 return drm_gem_prime_import_dev(dev, dma_buf, to_dma_dev(dev)); 402 } 403 404 struct sg_table *exynos_drm_gem_prime_get_sg_table(struct drm_gem_object *obj) 405 { 406 struct exynos_drm_gem *exynos_gem = to_exynos_gem(obj); 407 struct drm_device *drm_dev = obj->dev; 408 struct sg_table *sgt; 409 int ret; 410 411 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL); 412 if (!sgt) 413 return ERR_PTR(-ENOMEM); 414 415 ret = dma_get_sgtable_attrs(to_dma_dev(drm_dev), sgt, exynos_gem->cookie, 416 exynos_gem->dma_addr, exynos_gem->size, 417 exynos_gem->dma_attrs); 418 if (ret) { 419 DRM_ERROR("failed to get sgtable, %d\n", ret); 420 kfree(sgt); 421 return ERR_PTR(ret); 422 } 423 424 return sgt; 425 } 426 427 struct drm_gem_object * 428 exynos_drm_gem_prime_import_sg_table(struct drm_device *dev, 429 struct dma_buf_attachment *attach, 430 struct sg_table *sgt) 431 { 432 struct exynos_drm_gem *exynos_gem; 433 434 if (sgt->nents < 1) 435 return ERR_PTR(-EINVAL); 436 437 /* 438 * Check if the provided buffer has been mapped as contiguous 439 * into DMA address space. 440 */ 441 if (sgt->nents > 1) { 442 dma_addr_t next_addr = sg_dma_address(sgt->sgl); 443 struct scatterlist *s; 444 unsigned int i; 445 446 for_each_sg(sgt->sgl, s, sgt->nents, i) { 447 if (!sg_dma_len(s)) 448 break; 449 if (sg_dma_address(s) != next_addr) { 450 DRM_ERROR("buffer chunks must be mapped contiguously"); 451 return ERR_PTR(-EINVAL); 452 } 453 next_addr = sg_dma_address(s) + sg_dma_len(s); 454 } 455 } 456 457 exynos_gem = exynos_drm_gem_init(dev, attach->dmabuf->size); 458 if (IS_ERR(exynos_gem)) 459 return ERR_CAST(exynos_gem); 460 461 /* 462 * Buffer has been mapped as contiguous into DMA address space, 463 * but if there is IOMMU, it can be either CONTIG or NONCONTIG. 464 * We assume a simplified logic below: 465 */ 466 if (is_drm_iommu_supported(dev)) 467 exynos_gem->flags |= EXYNOS_BO_NONCONTIG; 468 else 469 exynos_gem->flags |= EXYNOS_BO_CONTIG; 470 471 exynos_gem->dma_addr = sg_dma_address(sgt->sgl); 472 exynos_gem->sgt = sgt; 473 return &exynos_gem->base; 474 } 475 476 void *exynos_drm_gem_prime_vmap(struct drm_gem_object *obj) 477 { 478 return NULL; 479 } 480 481 void exynos_drm_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr) 482 { 483 /* Nothing to do */ 484 } 485 486 int exynos_drm_gem_prime_mmap(struct drm_gem_object *obj, 487 struct vm_area_struct *vma) 488 { 489 int ret; 490 491 ret = drm_gem_mmap_obj(obj, obj->size, vma); 492 if (ret < 0) 493 return ret; 494 495 return exynos_drm_gem_mmap_obj(obj, vma); 496 } 497