1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2016-2018, 2020-2021 The Linux Foundation. All rights reserved. 4 * Copyright (C) 2013 Red Hat 5 * Author: Rob Clark <robdclark@gmail.com> 6 */ 7 8 #include <linux/dma-mapping.h> 9 #include <linux/fault-inject.h> 10 #include <linux/debugfs.h> 11 #include <linux/of_address.h> 12 #include <linux/uaccess.h> 13 14 #include <drm/drm_drv.h> 15 #include <drm/drm_file.h> 16 #include <drm/drm_ioctl.h> 17 #include <drm/drm_of.h> 18 19 #include "msm_drv.h" 20 #include "msm_debugfs.h" 21 #include "msm_gem.h" 22 #include "msm_gpu.h" 23 #include "msm_kms.h" 24 25 /* 26 * MSM driver version: 27 * - 1.0.0 - initial interface 28 * - 1.1.0 - adds madvise, and support for submits with > 4 cmd buffers 29 * - 1.2.0 - adds explicit fence support for submit ioctl 30 * - 1.3.0 - adds GMEM_BASE + NR_RINGS params, SUBMITQUEUE_NEW + 31 * SUBMITQUEUE_CLOSE ioctls, and MSM_INFO_IOVA flag for 32 * MSM_GEM_INFO ioctl. 33 * - 1.4.0 - softpin, MSM_RELOC_BO_DUMP, and GEM_INFO support to set/get 34 * GEM object's debug name 35 * - 1.5.0 - Add SUBMITQUERY_QUERY ioctl 36 * - 1.6.0 - Syncobj support 37 * - 1.7.0 - Add MSM_PARAM_SUSPENDS to access suspend count 38 * - 1.8.0 - Add MSM_BO_CACHED_COHERENT for supported GPUs (a6xx) 39 * - 1.9.0 - Add MSM_SUBMIT_FENCE_SN_IN 40 * - 1.10.0 - Add MSM_SUBMIT_BO_NO_IMPLICIT 41 * - 1.11.0 - Add wait boost (MSM_WAIT_FENCE_BOOST, MSM_PREP_BOOST) 42 * - 1.12.0 - Add MSM_INFO_SET_METADATA and MSM_INFO_GET_METADATA 43 * - 1.13.0 - Add VM_BIND 44 */ 45 #define MSM_VERSION_MAJOR 1 46 #define MSM_VERSION_MINOR 13 47 #define MSM_VERSION_PATCHLEVEL 0 48 49 bool dumpstate; 50 MODULE_PARM_DESC(dumpstate, "Dump KMS state on errors"); 51 module_param(dumpstate, bool, 0600); 52 53 static bool modeset = true; 54 MODULE_PARM_DESC(modeset, "Use kernel modesetting [KMS] (1=on (default), 0=disable)"); 55 module_param(modeset, bool, 0600); 56 57 /* 58 * separate_gpu_kms (tristate): 59 * -1 (default): decide automatically based on hardware topology. Split devices 60 * if there is more than one GPU or more than one display master. 61 * 0: force single DRM device (bind display + GPU) 62 * 1: force separate DRM devices 63 */ 64 static int separate_gpu_kms = -1; 65 MODULE_PARM_DESC(separate_gpu_kms, 66 "Use separate DRM device for the GPU (-1=auto (default), 0=single DRM device, 1=separate DRM devices)"); 67 module_param(separate_gpu_kms, int, 0400); 68 69 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 70 static bool transparent_hugepage = true; 71 module_param(transparent_hugepage, bool, 0400); 72 MODULE_PARM_DESC(transparent_hugepage, "Use a dedicated tmpfs mount point with Transparent Hugepage enabled (true = default)"); 73 #else 74 #define transparent_hugepage false 75 #endif 76 77 DECLARE_FAULT_ATTR(fail_gem_alloc); 78 DECLARE_FAULT_ATTR(fail_gem_iova); 79 80 static void msm_gem_thp_init(struct drm_device *dev) 81 { 82 int err; 83 84 if (!transparent_hugepage) 85 return; 86 87 err = drm_gem_huge_mnt_create(dev, "within_size"); 88 if (drm_gem_get_huge_mnt(dev)) 89 drm_info(dev, "Using Transparent Hugepage\n"); 90 else if (err) 91 drm_warn(dev, "Can't use Transparent Hugepage (%d)\n", err); 92 } 93 94 static const struct of_device_id msm_gpu_match[]; 95 static inline bool msm_gpu_node_present(struct device_node *np) 96 { 97 return np && of_device_is_available(np) && adreno_has_gpu(np); 98 } 99 100 static int msm_count_gpus(void) 101 { 102 struct device_node *np; 103 int count = 0; 104 105 for_each_matching_node(np, msm_gpu_match) { 106 if (msm_gpu_node_present(np)) 107 count++; 108 } 109 110 return count; 111 } 112 113 static bool msm_separate_gpu_kms_auto(void) 114 { 115 int gpus = msm_count_gpus(); 116 int display_subsystems = msm_count_mdss() + msm_count_mdp4(); 117 118 if (gpus <= 0 || display_subsystems <= 0) 119 return false; 120 121 /* Single card if there is exactly one GPU and one display subsystem */ 122 return gpus > 1 || display_subsystems > 1; 123 } 124 125 bool msm_separate_gpu_kms_components(void) 126 { 127 if (msm_count_gpus() == 0) 128 return true; 129 130 switch (separate_gpu_kms) { 131 case 0: 132 return false; 133 case 1: 134 return true; 135 default: 136 return msm_separate_gpu_kms_auto(); 137 } 138 } 139 140 static int msm_drm_uninit(struct device *dev, const struct component_ops *gpu_ops) 141 { 142 struct platform_device *pdev = to_platform_device(dev); 143 struct msm_drm_private *priv = platform_get_drvdata(pdev); 144 struct drm_device *ddev = priv->dev; 145 146 /* 147 * Shutdown the hw if we're far enough along where things might be on. 148 * If we run this too early, we'll end up panicking in any variety of 149 * places. Since we don't register the drm device until late in 150 * msm_drm_init, drm_dev->registered is used as an indicator that the 151 * shutdown will be successful. 152 */ 153 if (ddev->registered) { 154 drm_dev_unregister(ddev); 155 if (priv->kms) 156 msm_drm_kms_unregister(dev); 157 } 158 159 msm_gem_shrinker_cleanup(ddev); 160 161 msm_rd_debugfs_cleanup(priv); 162 163 if (priv->kms) 164 msm_drm_kms_uninit(dev); 165 166 if (gpu_ops) 167 gpu_ops->unbind(dev, dev, NULL); 168 else 169 component_unbind_all(dev, ddev); 170 171 ddev->dev_private = NULL; 172 drm_dev_put(ddev); 173 174 return 0; 175 } 176 177 static int msm_drm_init(struct device *dev, const struct drm_driver *drv, 178 const struct component_ops *gpu_ops) 179 { 180 struct msm_drm_private *priv = dev_get_drvdata(dev); 181 struct drm_device *ddev; 182 int ret; 183 184 if (drm_firmware_drivers_only()) 185 return -ENODEV; 186 187 ddev = drm_dev_alloc(drv, dev); 188 if (IS_ERR(ddev)) { 189 DRM_DEV_ERROR(dev, "failed to allocate drm_device\n"); 190 return PTR_ERR(ddev); 191 } 192 ddev->dev_private = priv; 193 priv->dev = ddev; 194 195 INIT_LIST_HEAD(&priv->objects); 196 mutex_init(&priv->obj_lock); 197 198 /* 199 * Initialize the LRUs: 200 */ 201 drm_gem_lru_init(&priv->lru.unbacked); 202 drm_gem_lru_init(&priv->lru.pinned); 203 drm_gem_lru_init(&priv->lru.willneed); 204 drm_gem_lru_init(&priv->lru.dontneed); 205 206 /* Initialize stall-on-fault */ 207 spin_lock_init(&priv->fault_stall_lock); 208 priv->stall_enabled = true; 209 210 /* Teach lockdep about lock ordering wrt. shrinker: */ 211 fs_reclaim_acquire(GFP_KERNEL); 212 might_lock(&ddev->gem_lru_mutex); 213 fs_reclaim_release(GFP_KERNEL); 214 215 if (priv->kms_init) { 216 ret = drmm_mode_config_init(ddev); 217 if (ret) 218 goto err_put_dev; 219 } 220 221 dma_set_max_seg_size(dev, UINT_MAX); 222 223 /* Bind all our sub-components: */ 224 if (gpu_ops) 225 ret = gpu_ops->bind(dev, dev, NULL); 226 else 227 ret = component_bind_all(dev, ddev); 228 if (ret) 229 goto err_put_dev; 230 231 ret = msm_gem_shrinker_init(ddev); 232 if (ret) 233 goto err_msm_uninit; 234 235 msm_gem_thp_init(ddev); 236 237 if (priv->kms_init) { 238 ret = msm_drm_kms_init(dev, drv); 239 if (ret) 240 goto err_msm_uninit; 241 } 242 243 ret = drm_dev_register(ddev, 0); 244 if (ret) 245 goto err_msm_uninit; 246 247 ret = msm_debugfs_late_init(ddev); 248 if (ret) 249 goto err_msm_uninit; 250 251 if (priv->kms_init) 252 msm_drm_kms_post_init(dev); 253 254 return 0; 255 256 err_msm_uninit: 257 msm_drm_uninit(dev, gpu_ops); 258 259 return ret; 260 261 err_put_dev: 262 drm_dev_put(ddev); 263 264 return ret; 265 } 266 267 /* 268 * DRM operations: 269 */ 270 271 static void load_gpu(struct drm_device *dev) 272 { 273 static DEFINE_MUTEX(init_lock); 274 struct msm_drm_private *priv = dev->dev_private; 275 276 mutex_lock(&init_lock); 277 278 if (!priv->gpu) 279 priv->gpu = adreno_load_gpu(dev); 280 281 mutex_unlock(&init_lock); 282 } 283 284 /** 285 * msm_context_vm - lazily create the context's VM 286 * 287 * @dev: the drm device 288 * @ctx: the context 289 * 290 * The VM is lazily created, so that userspace has a chance to opt-in to having 291 * a userspace managed VM before the VM is created. 292 * 293 * Note that this does not return a reference to the VM. Once the VM is created, 294 * it exists for the lifetime of the context. 295 */ 296 struct drm_gpuvm *msm_context_vm(struct drm_device *dev, struct msm_context *ctx) 297 { 298 struct msm_drm_private *priv = dev->dev_private; 299 struct drm_gpuvm *vm = smp_load_acquire(&ctx->vm); 300 301 /* Once ctx->vm is created it is valid for the lifetime of the context: */ 302 if (vm) 303 return vm; 304 305 guard(rwsem_write)(&ctx->ctxlock); 306 307 if (!ctx->vm) { 308 vm = msm_gpu_create_private_vm( 309 priv->gpu, current, !ctx->userspace_managed_vm); 310 if (!IS_ERR_OR_NULL(vm)) 311 smp_store_release(&ctx->vm, vm); 312 } 313 314 return ctx->vm; 315 } 316 317 static int context_init(struct drm_device *dev, struct drm_file *file) 318 { 319 static atomic_t ident = ATOMIC_INIT(0); 320 struct msm_context *ctx; 321 322 ctx = kzalloc_obj(*ctx); 323 if (!ctx) 324 return -ENOMEM; 325 326 INIT_LIST_HEAD(&ctx->submitqueues); 327 init_rwsem(&ctx->ctxlock); 328 329 kref_init(&ctx->ref); 330 msm_submitqueue_init(dev, ctx); 331 332 file->driver_priv = ctx; 333 334 ctx->seqno = atomic_inc_return(&ident); 335 336 return 0; 337 } 338 339 static int msm_open(struct drm_device *dev, struct drm_file *file) 340 { 341 /* For now, load gpu on open.. to avoid the requirement of having 342 * firmware in the initrd. 343 */ 344 load_gpu(dev); 345 346 return context_init(dev, file); 347 } 348 349 static void context_close(struct msm_context *ctx) 350 { 351 ctx->closed = true; 352 msm_submitqueue_close(ctx); 353 msm_context_put(ctx); 354 } 355 356 static void msm_postclose(struct drm_device *dev, struct drm_file *file) 357 { 358 struct msm_drm_private *priv = dev->dev_private; 359 struct msm_context *ctx = file->driver_priv; 360 361 /* 362 * It is not possible to set sysprof param to non-zero if gpu 363 * is not initialized: 364 */ 365 if (priv->gpu) 366 msm_context_set_sysprof(ctx, priv->gpu, 0); 367 368 context_close(ctx); 369 } 370 371 /* 372 * DRM ioctls: 373 */ 374 375 static int msm_ioctl_get_param(struct drm_device *dev, void *data, 376 struct drm_file *file) 377 { 378 struct msm_drm_private *priv = dev->dev_private; 379 struct drm_msm_param *args = data; 380 struct msm_gpu *gpu; 381 382 /* for now, we just have 3d pipe.. eventually this would need to 383 * be more clever to dispatch to appropriate gpu module: 384 */ 385 if ((args->pipe != MSM_PIPE_3D0) || (args->pad != 0)) 386 return -EINVAL; 387 388 gpu = priv->gpu; 389 390 if (!gpu) 391 return -ENXIO; 392 393 return gpu->funcs->get_param(gpu, file->driver_priv, 394 args->param, &args->value, &args->len); 395 } 396 397 static int msm_ioctl_set_param(struct drm_device *dev, void *data, 398 struct drm_file *file) 399 { 400 struct msm_drm_private *priv = dev->dev_private; 401 struct drm_msm_param *args = data; 402 struct msm_gpu *gpu; 403 404 if ((args->pipe != MSM_PIPE_3D0) || (args->pad != 0)) 405 return -EINVAL; 406 407 gpu = priv->gpu; 408 409 if (!gpu) 410 return -ENXIO; 411 412 return gpu->funcs->set_param(gpu, file->driver_priv, 413 args->param, args->value, args->len); 414 } 415 416 static int msm_ioctl_gem_new(struct drm_device *dev, void *data, 417 struct drm_file *file) 418 { 419 struct drm_msm_gem_new *args = data; 420 uint32_t flags = args->flags; 421 422 if (args->flags & ~MSM_BO_FLAGS) { 423 DRM_ERROR("invalid flags: %08x\n", args->flags); 424 return -EINVAL; 425 } 426 427 /* 428 * Uncached CPU mappings are deprecated, as of: 429 * 430 * 9ef364432db4 ("drm/msm: deprecate MSM_BO_UNCACHED (map as writecombine instead)") 431 * 432 * So promote them to WC. 433 */ 434 if (flags & MSM_BO_UNCACHED) { 435 flags &= ~MSM_BO_CACHED; 436 flags |= MSM_BO_WC; 437 } 438 439 if (should_fail(&fail_gem_alloc, args->size)) 440 return -ENOMEM; 441 442 return msm_gem_new_handle(dev, file, args->size, 443 args->flags, &args->handle, NULL); 444 } 445 446 static inline ktime_t to_ktime(struct drm_msm_timespec timeout) 447 { 448 return ktime_set(timeout.tv_sec, timeout.tv_nsec); 449 } 450 451 static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data, 452 struct drm_file *file) 453 { 454 struct drm_msm_gem_cpu_prep *args = data; 455 struct drm_gem_object *obj; 456 ktime_t timeout = to_ktime(args->timeout); 457 int ret; 458 459 if (args->op & ~MSM_PREP_FLAGS) { 460 DRM_ERROR("invalid op: %08x\n", args->op); 461 return -EINVAL; 462 } 463 464 obj = drm_gem_object_lookup(file, args->handle); 465 if (!obj) 466 return -ENOENT; 467 468 ret = msm_gem_cpu_prep(obj, args->op, &timeout); 469 470 drm_gem_object_put(obj); 471 472 return ret; 473 } 474 475 static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data, 476 struct drm_file *file) 477 { 478 struct drm_msm_gem_cpu_fini *args = data; 479 struct drm_gem_object *obj; 480 int ret; 481 482 obj = drm_gem_object_lookup(file, args->handle); 483 if (!obj) 484 return -ENOENT; 485 486 ret = msm_gem_cpu_fini(obj); 487 488 drm_gem_object_put(obj); 489 490 return ret; 491 } 492 493 static int msm_ioctl_gem_info_iova(struct drm_device *dev, 494 struct drm_file *file, struct drm_gem_object *obj, 495 uint64_t *iova) 496 { 497 struct msm_drm_private *priv = dev->dev_private; 498 struct msm_context *ctx = file->driver_priv; 499 struct drm_gpuvm *vm = msm_context_vm(dev, ctx); 500 501 if (!priv->gpu) 502 return -EINVAL; 503 504 if (!vm) 505 return UERR(ENOMEM, dev, "no VM"); 506 507 if (msm_context_is_vmbind(ctx)) 508 return UERR(EINVAL, dev, "VM_BIND is enabled"); 509 510 if (should_fail(&fail_gem_iova, obj->size)) 511 return -ENOMEM; 512 513 /* 514 * Don't pin the memory here - just get an address so that userspace can 515 * be productive 516 */ 517 return msm_gem_get_iova(obj, vm, iova); 518 } 519 520 static int msm_ioctl_gem_info_set_iova(struct drm_device *dev, 521 struct drm_file *file, struct drm_gem_object *obj, 522 uint64_t iova) 523 { 524 struct msm_drm_private *priv = dev->dev_private; 525 struct msm_context *ctx = file->driver_priv; 526 struct drm_gpuvm *vm = msm_context_vm(dev, ctx); 527 528 if (!priv->gpu) 529 return -EINVAL; 530 531 if (!vm) 532 return UERR(ENOMEM, dev, "no VM"); 533 534 if (msm_context_is_vmbind(ctx)) 535 return UERR(EINVAL, dev, "VM_BIND is enabled"); 536 537 /* Only supported if per-process address space is supported: */ 538 if (priv->gpu->vm == vm) 539 return UERR(EOPNOTSUPP, dev, "requires per-process pgtables"); 540 541 if (should_fail(&fail_gem_iova, obj->size)) 542 return -ENOMEM; 543 544 return msm_gem_set_iova(obj, vm, iova); 545 } 546 547 static int msm_ioctl_gem_info_set_metadata(struct drm_gem_object *obj, 548 __user void *metadata, 549 u32 metadata_size) 550 { 551 struct msm_gem_object *msm_obj = to_msm_bo(obj); 552 void *new_metadata; 553 void *buf; 554 int ret; 555 556 /* Impose a moderate upper bound on metadata size: */ 557 if (metadata_size > 128) { 558 return -EOVERFLOW; 559 } 560 561 /* Use a temporary buf to keep copy_from_user() outside of gem obj lock: */ 562 buf = memdup_user(metadata, metadata_size); 563 if (IS_ERR(buf)) 564 return PTR_ERR(buf); 565 566 ret = msm_gem_lock_interruptible(obj); 567 if (ret) 568 goto out; 569 570 new_metadata = 571 krealloc(msm_obj->metadata, metadata_size, GFP_KERNEL); 572 if (!new_metadata) { 573 ret = -ENOMEM; 574 goto unlock; 575 } 576 577 msm_obj->metadata = new_metadata; 578 msm_obj->metadata_size = metadata_size; 579 memcpy(msm_obj->metadata, buf, metadata_size); 580 581 unlock: 582 msm_gem_unlock(obj); 583 584 out: 585 kfree(buf); 586 587 return ret; 588 } 589 590 static int msm_ioctl_gem_info_get_metadata(struct drm_gem_object *obj, 591 __user void *metadata, 592 u32 *metadata_size) 593 { 594 struct msm_gem_object *msm_obj = to_msm_bo(obj); 595 void *buf; 596 int ret, len; 597 598 if (!metadata) { 599 /* 600 * Querying the size is inherently racey, but 601 * EXT_external_objects expects the app to confirm 602 * via device and driver UUIDs that the exporter and 603 * importer versions match. All we can do from the 604 * kernel side is check the length under obj lock 605 * when userspace tries to retrieve the metadata 606 */ 607 *metadata_size = msm_obj->metadata_size; 608 return 0; 609 } 610 611 ret = msm_gem_lock_interruptible(obj); 612 if (ret) 613 return ret; 614 615 /* Avoid copy_to_user() under gem obj lock: */ 616 len = msm_obj->metadata_size; 617 buf = kmemdup(msm_obj->metadata, len, GFP_KERNEL); 618 619 if (!buf) { 620 msm_gem_unlock(obj); 621 return -ENOMEM; 622 } 623 624 msm_gem_unlock(obj); 625 626 if (*metadata_size < len) { 627 ret = -ETOOSMALL; 628 } else if (copy_to_user(metadata, buf, len)) { 629 ret = -EFAULT; 630 } else { 631 *metadata_size = len; 632 } 633 634 kfree(buf); 635 636 return ret; 637 } 638 639 static int msm_ioctl_gem_info(struct drm_device *dev, void *data, 640 struct drm_file *file) 641 { 642 struct drm_msm_gem_info *args = data; 643 struct drm_gem_object *obj; 644 struct msm_gem_object *msm_obj; 645 int i, ret = 0; 646 647 if (args->pad) 648 return -EINVAL; 649 650 switch (args->info) { 651 case MSM_INFO_GET_OFFSET: 652 case MSM_INFO_GET_IOVA: 653 case MSM_INFO_SET_IOVA: 654 case MSM_INFO_GET_FLAGS: 655 /* value returned as immediate, not pointer, so len==0: */ 656 if (args->len) 657 return -EINVAL; 658 break; 659 case MSM_INFO_SET_NAME: 660 case MSM_INFO_GET_NAME: 661 case MSM_INFO_SET_METADATA: 662 case MSM_INFO_GET_METADATA: 663 break; 664 default: 665 return -EINVAL; 666 } 667 668 obj = drm_gem_object_lookup(file, args->handle); 669 if (!obj) 670 return -ENOENT; 671 672 msm_obj = to_msm_bo(obj); 673 674 switch (args->info) { 675 case MSM_INFO_GET_OFFSET: 676 ret = drm_gem_create_mmap_offset(obj); 677 if (ret == 0) 678 args->value = drm_vma_node_offset_addr(&obj->vma_node); 679 break; 680 case MSM_INFO_GET_IOVA: 681 ret = msm_ioctl_gem_info_iova(dev, file, obj, &args->value); 682 break; 683 case MSM_INFO_SET_IOVA: 684 ret = msm_ioctl_gem_info_set_iova(dev, file, obj, args->value); 685 break; 686 case MSM_INFO_GET_FLAGS: 687 if (drm_gem_is_imported(obj)) { 688 ret = -EINVAL; 689 break; 690 } 691 /* Hide internal kernel-only flags: */ 692 args->value = to_msm_bo(obj)->flags & MSM_BO_FLAGS; 693 ret = 0; 694 break; 695 case MSM_INFO_SET_NAME: 696 /* length check should leave room for terminating null: */ 697 if (args->len >= sizeof(msm_obj->name)) { 698 ret = -EINVAL; 699 break; 700 } 701 if (copy_from_user(msm_obj->name, u64_to_user_ptr(args->value), 702 args->len)) { 703 msm_obj->name[0] = '\0'; 704 ret = -EFAULT; 705 break; 706 } 707 msm_obj->name[args->len] = '\0'; 708 for (i = 0; i < args->len; i++) { 709 if (!isprint(msm_obj->name[i])) { 710 msm_obj->name[i] = '\0'; 711 break; 712 } 713 } 714 break; 715 case MSM_INFO_GET_NAME: 716 if (args->value && (args->len < strlen(msm_obj->name))) { 717 ret = -ETOOSMALL; 718 break; 719 } 720 args->len = strlen(msm_obj->name); 721 if (args->value) { 722 if (copy_to_user(u64_to_user_ptr(args->value), 723 msm_obj->name, args->len)) 724 ret = -EFAULT; 725 } 726 break; 727 case MSM_INFO_SET_METADATA: 728 ret = msm_ioctl_gem_info_set_metadata( 729 obj, u64_to_user_ptr(args->value), args->len); 730 break; 731 case MSM_INFO_GET_METADATA: 732 ret = msm_ioctl_gem_info_get_metadata( 733 obj, u64_to_user_ptr(args->value), &args->len); 734 break; 735 } 736 737 drm_gem_object_put(obj); 738 739 return ret; 740 } 741 742 static int wait_fence(struct msm_gpu_submitqueue *queue, uint32_t fence_id, 743 ktime_t timeout, uint32_t flags) 744 { 745 struct dma_fence *fence; 746 int ret; 747 748 if (fence_after(fence_id, queue->last_fence)) { 749 DRM_ERROR_RATELIMITED("waiting on invalid fence: %u (of %u)\n", 750 fence_id, queue->last_fence); 751 return -EINVAL; 752 } 753 754 /* 755 * Map submitqueue scoped "seqno" (which is actually an idr key) 756 * back to underlying dma-fence 757 * 758 * The fence is removed from the fence_idr when the submit is 759 * retired, so if the fence is not found it means there is nothing 760 * to wait for 761 */ 762 spin_lock(&queue->idr_lock); 763 fence = idr_find(&queue->fence_idr, fence_id); 764 if (fence) 765 fence = dma_fence_get_rcu(fence); 766 spin_unlock(&queue->idr_lock); 767 768 if (!fence) 769 return 0; 770 771 if (flags & MSM_WAIT_FENCE_BOOST) 772 dma_fence_set_deadline(fence, ktime_get()); 773 774 ret = dma_fence_wait_timeout(fence, true, timeout_to_jiffies(&timeout)); 775 if (ret == 0) { 776 ret = -ETIMEDOUT; 777 } else if (ret != -ERESTARTSYS) { 778 ret = 0; 779 } 780 781 dma_fence_put(fence); 782 783 return ret; 784 } 785 786 static int msm_ioctl_wait_fence(struct drm_device *dev, void *data, 787 struct drm_file *file) 788 { 789 struct msm_drm_private *priv = dev->dev_private; 790 struct drm_msm_wait_fence *args = data; 791 struct msm_gpu_submitqueue *queue; 792 int ret; 793 794 if (args->flags & ~MSM_WAIT_FENCE_FLAGS) { 795 DRM_ERROR("invalid flags: %08x\n", args->flags); 796 return -EINVAL; 797 } 798 799 if (!priv->gpu) 800 return 0; 801 802 queue = msm_submitqueue_get(file->driver_priv, args->queueid); 803 if (!queue) 804 return -ENOENT; 805 806 ret = wait_fence(queue, args->fence, to_ktime(args->timeout), args->flags); 807 808 msm_submitqueue_put(queue); 809 810 return ret; 811 } 812 813 static int msm_ioctl_gem_madvise(struct drm_device *dev, void *data, 814 struct drm_file *file) 815 { 816 struct drm_msm_gem_madvise *args = data; 817 struct drm_gem_object *obj; 818 int ret; 819 820 switch (args->madv) { 821 case MSM_MADV_DONTNEED: 822 case MSM_MADV_WILLNEED: 823 break; 824 default: 825 return -EINVAL; 826 } 827 828 obj = drm_gem_object_lookup(file, args->handle); 829 if (!obj) { 830 return -ENOENT; 831 } 832 833 ret = msm_gem_madvise(obj, args->madv); 834 if (ret >= 0) { 835 args->retained = ret; 836 ret = 0; 837 } 838 839 drm_gem_object_put(obj); 840 841 return ret; 842 } 843 844 845 static int msm_ioctl_submitqueue_new(struct drm_device *dev, void *data, 846 struct drm_file *file) 847 { 848 struct drm_msm_submitqueue *args = data; 849 850 if (args->flags & ~MSM_SUBMITQUEUE_FLAGS) 851 return -EINVAL; 852 853 return msm_submitqueue_create(dev, file->driver_priv, args->prio, 854 args->flags, &args->id); 855 } 856 857 static int msm_ioctl_submitqueue_query(struct drm_device *dev, void *data, 858 struct drm_file *file) 859 { 860 return msm_submitqueue_query(dev, file->driver_priv, data); 861 } 862 863 static int msm_ioctl_submitqueue_close(struct drm_device *dev, void *data, 864 struct drm_file *file) 865 { 866 u32 id = *(u32 *) data; 867 868 return msm_submitqueue_remove(file->driver_priv, id); 869 } 870 871 static const struct drm_ioctl_desc msm_ioctls[] = { 872 DRM_IOCTL_DEF_DRV(MSM_GET_PARAM, msm_ioctl_get_param, DRM_RENDER_ALLOW), 873 DRM_IOCTL_DEF_DRV(MSM_SET_PARAM, msm_ioctl_set_param, DRM_RENDER_ALLOW), 874 DRM_IOCTL_DEF_DRV(MSM_GEM_NEW, msm_ioctl_gem_new, DRM_RENDER_ALLOW), 875 DRM_IOCTL_DEF_DRV(MSM_GEM_INFO, msm_ioctl_gem_info, DRM_RENDER_ALLOW), 876 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_RENDER_ALLOW), 877 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_RENDER_ALLOW), 878 DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT, msm_ioctl_gem_submit, DRM_RENDER_ALLOW), 879 DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE, msm_ioctl_wait_fence, DRM_RENDER_ALLOW), 880 DRM_IOCTL_DEF_DRV(MSM_GEM_MADVISE, msm_ioctl_gem_madvise, DRM_RENDER_ALLOW), 881 DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_NEW, msm_ioctl_submitqueue_new, DRM_RENDER_ALLOW), 882 DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_CLOSE, msm_ioctl_submitqueue_close, DRM_RENDER_ALLOW), 883 DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_QUERY, msm_ioctl_submitqueue_query, DRM_RENDER_ALLOW), 884 DRM_IOCTL_DEF_DRV(MSM_VM_BIND, msm_ioctl_vm_bind, DRM_RENDER_ALLOW), 885 DRM_IOCTL_DEF_DRV(MSM_PERFCNTR_CONFIG, msm_ioctl_perfcntr_config, DRM_RENDER_ALLOW), 886 }; 887 888 static void msm_show_fdinfo(struct drm_printer *p, struct drm_file *file) 889 { 890 struct drm_device *dev = file->minor->dev; 891 struct msm_drm_private *priv = dev->dev_private; 892 893 if (!priv->gpu) 894 return; 895 896 msm_gpu_show_fdinfo(priv->gpu, file->driver_priv, p); 897 898 drm_show_memory_stats(p, file); 899 } 900 901 static const struct file_operations fops = { 902 .owner = THIS_MODULE, 903 DRM_GEM_FOPS, 904 .show_fdinfo = drm_show_fdinfo, 905 }; 906 907 #define DRIVER_FEATURES_GPU ( \ 908 DRIVER_GEM | \ 909 DRIVER_RENDER | \ 910 DRIVER_SYNCOBJ | \ 911 DRIVER_SYNCOBJ_TIMELINE | \ 912 0 ) 913 914 #define DRIVER_FEATURES_KMS ( \ 915 DRIVER_GEM | \ 916 DRIVER_ATOMIC | \ 917 DRIVER_MODESET | \ 918 0 ) 919 920 static const struct drm_driver msm_driver = { 921 .driver_features = DRIVER_FEATURES_GPU | DRIVER_FEATURES_KMS, 922 .open = msm_open, 923 .postclose = msm_postclose, 924 .dumb_create = msm_gem_dumb_create, 925 .dumb_map_offset = drm_gem_dumb_map_offset, 926 .gem_prime_import = msm_gem_prime_import, 927 .gem_prime_import_sg_table = msm_gem_prime_import_sg_table, 928 #ifdef CONFIG_DEBUG_FS 929 .debugfs_init = msm_debugfs_init, 930 #endif 931 MSM_FBDEV_DRIVER_OPS, 932 .show_fdinfo = msm_show_fdinfo, 933 .ioctls = msm_ioctls, 934 .num_ioctls = ARRAY_SIZE(msm_ioctls), 935 .fops = &fops, 936 .name = "msm", 937 .desc = "MSM Snapdragon DRM", 938 .major = MSM_VERSION_MAJOR, 939 .minor = MSM_VERSION_MINOR, 940 .patchlevel = MSM_VERSION_PATCHLEVEL, 941 }; 942 943 static const struct drm_driver msm_kms_driver = { 944 .driver_features = DRIVER_FEATURES_KMS, 945 .open = msm_open, 946 .postclose = msm_postclose, 947 .dumb_create = msm_gem_dumb_create, 948 .dumb_map_offset = drm_gem_dumb_map_offset, 949 .gem_prime_import_sg_table = msm_gem_prime_import_sg_table, 950 #ifdef CONFIG_DEBUG_FS 951 .debugfs_init = msm_debugfs_init, 952 #endif 953 MSM_FBDEV_DRIVER_OPS, 954 .show_fdinfo = msm_show_fdinfo, 955 .fops = &fops, 956 .name = "msm-kms", 957 .desc = "MSM Snapdragon DRM", 958 .major = MSM_VERSION_MAJOR, 959 .minor = MSM_VERSION_MINOR, 960 .patchlevel = MSM_VERSION_PATCHLEVEL, 961 }; 962 963 static const struct drm_driver msm_gpu_driver = { 964 .driver_features = DRIVER_FEATURES_GPU, 965 .open = msm_open, 966 .postclose = msm_postclose, 967 .gem_prime_import_sg_table = msm_gem_prime_import_sg_table, 968 #ifdef CONFIG_DEBUG_FS 969 .debugfs_init = msm_debugfs_init, 970 #endif 971 .show_fdinfo = msm_show_fdinfo, 972 .ioctls = msm_ioctls, 973 .num_ioctls = ARRAY_SIZE(msm_ioctls), 974 .fops = &fops, 975 .name = "msm", 976 .desc = "MSM Snapdragon DRM", 977 .major = MSM_VERSION_MAJOR, 978 .minor = MSM_VERSION_MINOR, 979 .patchlevel = MSM_VERSION_PATCHLEVEL, 980 }; 981 982 /* 983 * Componentized driver support: 984 */ 985 986 /* 987 * Identify what components need to be added by parsing what remote-endpoints 988 * our MDP output ports are connected to. In the case of LVDS on MDP4, there 989 * is no external component that we need to add since LVDS is within MDP4 990 * itself. 991 */ 992 static int add_mdp_components(struct device *master_dev, 993 struct component_match **matchptr) 994 { 995 struct device_node *np = master_dev->of_node; 996 struct device_node *ep_node; 997 998 for_each_endpoint_of_node(np, ep_node) { 999 struct device_node *intf; 1000 struct of_endpoint ep; 1001 int ret; 1002 1003 ret = of_graph_parse_endpoint(ep_node, &ep); 1004 if (ret) { 1005 DRM_DEV_ERROR(master_dev, "unable to parse port endpoint\n"); 1006 of_node_put(ep_node); 1007 return ret; 1008 } 1009 1010 /* 1011 * The LCDC/LVDS port on MDP4 is a speacial case where the 1012 * remote-endpoint isn't a component that we need to add 1013 */ 1014 if (of_device_is_compatible(np, "qcom,mdp4") && 1015 ep.port == 0) 1016 continue; 1017 1018 /* 1019 * It's okay if some of the ports don't have a remote endpoint 1020 * specified. It just means that the port isn't connected to 1021 * any external interface. 1022 */ 1023 intf = of_graph_get_remote_port_parent(ep_node); 1024 if (!intf) 1025 continue; 1026 1027 if (of_device_is_available(intf)) 1028 drm_of_component_match_add(master_dev, matchptr, 1029 component_compare_of, intf); 1030 1031 of_node_put(intf); 1032 } 1033 1034 return 0; 1035 } 1036 1037 #if !IS_REACHABLE(CONFIG_DRM_MSM_MDP5) || !IS_REACHABLE(CONFIG_DRM_MSM_DPU) 1038 bool msm_disp_drv_should_bind(struct device *dev, bool dpu_driver) 1039 { 1040 /* If just a single driver is enabled, use it no matter what */ 1041 return true; 1042 } 1043 #else 1044 1045 static bool prefer_mdp5 = true; 1046 MODULE_PARM_DESC(prefer_mdp5, "Select whether MDP5 or DPU driver should be preferred"); 1047 module_param(prefer_mdp5, bool, 0444); 1048 1049 /* list all platforms that have been migrated from mdp5 to dpu driver */ 1050 static const char *const msm_mdp5_dpu_migrated[] = { 1051 /* there never was qcom,msm8998-mdp5 */ 1052 "qcom,sdm630-mdp5", 1053 "qcom,sdm660-mdp5", 1054 NULL 1055 }; 1056 1057 /* list all platforms supported by both mdp5 and dpu drivers */ 1058 static const char *const msm_mdp5_dpu_migration[] = { 1059 "qcom,msm8917-mdp5", 1060 "qcom,msm8937-mdp5", 1061 "qcom,msm8953-mdp5", 1062 "qcom,msm8996-mdp5", 1063 NULL, 1064 }; 1065 1066 bool msm_disp_drv_should_bind(struct device *dev, bool dpu_driver) 1067 { 1068 /* If it is not an MDP5 device, use DPU */ 1069 if (!of_device_is_compatible(dev->of_node, "qcom,mdp5")) 1070 return dpu_driver; 1071 1072 /* If it is no longer supported by MDP5, use DPU */ 1073 if (of_device_compatible_match(dev->of_node, msm_mdp5_dpu_migrated)) 1074 return dpu_driver; 1075 1076 /* If it is not in the migration list, use MDP5 */ 1077 if (!of_device_compatible_match(dev->of_node, msm_mdp5_dpu_migration)) 1078 return !dpu_driver; 1079 1080 return prefer_mdp5 ? !dpu_driver : dpu_driver; 1081 } 1082 #endif 1083 1084 /* 1085 * We don't know what's the best binding to link the gpu with the drm device. 1086 * Fow now, we just hunt for all the possible gpus that we support, and add them 1087 * as components. 1088 */ 1089 static const struct of_device_id msm_gpu_match[] = { 1090 { .compatible = "qcom,adreno" }, 1091 { .compatible = "qcom,adreno-3xx" }, 1092 { .compatible = "amd,imageon" }, 1093 { .compatible = "qcom,kgsl-3d0" }, 1094 { }, 1095 }; 1096 1097 static int add_gpu_components(struct device *dev, 1098 struct component_match **matchptr) 1099 { 1100 struct device_node *np; 1101 1102 np = of_find_matching_node(NULL, msm_gpu_match); 1103 if (!np) 1104 return 0; 1105 1106 if (msm_gpu_node_present(np)) 1107 drm_of_component_match_add(dev, matchptr, component_compare_of, np); 1108 1109 of_node_put(np); 1110 1111 return 0; 1112 } 1113 1114 static int msm_drm_bind(struct device *dev) 1115 { 1116 return msm_drm_init(dev, 1117 msm_separate_gpu_kms_components() ? 1118 &msm_kms_driver : 1119 &msm_driver, 1120 NULL); 1121 } 1122 1123 static void msm_drm_unbind(struct device *dev) 1124 { 1125 msm_drm_uninit(dev, NULL); 1126 } 1127 1128 const struct component_master_ops msm_drm_ops = { 1129 .bind = msm_drm_bind, 1130 .unbind = msm_drm_unbind, 1131 }; 1132 1133 int msm_drv_probe(struct device *master_dev, 1134 int (*kms_init)(struct drm_device *dev), 1135 struct msm_kms *kms) 1136 { 1137 struct msm_drm_private *priv; 1138 struct component_match *match = NULL; 1139 int ret; 1140 1141 priv = devm_kzalloc_obj(master_dev, *priv); 1142 if (!priv) 1143 return -ENOMEM; 1144 1145 priv->kms = kms; 1146 priv->kms_init = kms_init; 1147 dev_set_drvdata(master_dev, priv); 1148 1149 /* Add mdp components if we have KMS. */ 1150 if (kms_init) { 1151 ret = add_mdp_components(master_dev, &match); 1152 if (ret) 1153 return ret; 1154 } 1155 1156 if (!msm_separate_gpu_kms_components()) { 1157 ret = add_gpu_components(master_dev, &match); 1158 if (ret) 1159 return ret; 1160 } 1161 1162 /* on all devices that I am aware of, iommu's which can map 1163 * any address the cpu can see are used: 1164 */ 1165 ret = dma_set_mask_and_coherent(master_dev, ~0); 1166 if (ret) 1167 return ret; 1168 1169 ret = component_master_add_with_match(master_dev, &msm_drm_ops, match); 1170 if (ret) 1171 return ret; 1172 1173 return 0; 1174 } 1175 1176 int msm_gpu_probe(struct platform_device *pdev, 1177 const struct component_ops *ops) 1178 { 1179 struct msm_drm_private *priv; 1180 int ret; 1181 1182 priv = devm_kzalloc_obj(&pdev->dev, *priv); 1183 if (!priv) 1184 return -ENOMEM; 1185 1186 platform_set_drvdata(pdev, priv); 1187 1188 /* on all devices that I am aware of, iommu's which can map 1189 * any address the cpu can see are used: 1190 */ 1191 ret = dma_set_mask_and_coherent(&pdev->dev, ~0); 1192 if (ret) 1193 return ret; 1194 1195 return msm_drm_init(&pdev->dev, &msm_gpu_driver, ops); 1196 } 1197 1198 void msm_gpu_remove(struct platform_device *pdev, 1199 const struct component_ops *ops) 1200 { 1201 msm_drm_uninit(&pdev->dev, ops); 1202 } 1203 1204 static int __init msm_drm_register(void) 1205 { 1206 if (!modeset) 1207 return -EINVAL; 1208 1209 DBG("init"); 1210 msm_mdp_register(); 1211 msm_dpu_register(); 1212 msm_dsi_register(); 1213 msm_hdmi_register(); 1214 msm_dp_register(); 1215 adreno_register(); 1216 msm_mdp4_register(); 1217 msm_mdss_register(); 1218 1219 return 0; 1220 } 1221 1222 static void __exit msm_drm_unregister(void) 1223 { 1224 DBG("fini"); 1225 msm_mdss_unregister(); 1226 msm_mdp4_unregister(); 1227 msm_dp_unregister(); 1228 msm_hdmi_unregister(); 1229 adreno_unregister(); 1230 msm_dsi_unregister(); 1231 msm_mdp_unregister(); 1232 msm_dpu_unregister(); 1233 } 1234 1235 module_init(msm_drm_register); 1236 module_exit(msm_drm_unregister); 1237 1238 MODULE_AUTHOR("Rob Clark <robdclark@gmail.com"); 1239 MODULE_DESCRIPTION("MSM DRM Driver"); 1240 MODULE_LICENSE("GPL"); 1241