1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2016-2018, 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/kthread.h> 10 #include <linux/sched/mm.h> 11 #include <linux/uaccess.h> 12 #include <uapi/linux/sched/types.h> 13 14 #include <drm/drm_drv.h> 15 #include <drm/drm_file.h> 16 #include <drm/drm_ioctl.h> 17 #include <drm/drm_irq.h> 18 #include <drm/drm_prime.h> 19 #include <drm/drm_of.h> 20 #include <drm/drm_vblank.h> 21 22 #include "msm_drv.h" 23 #include "msm_debugfs.h" 24 #include "msm_fence.h" 25 #include "msm_gem.h" 26 #include "msm_gpu.h" 27 #include "msm_kms.h" 28 #include "adreno/adreno_gpu.h" 29 30 /* 31 * MSM driver version: 32 * - 1.0.0 - initial interface 33 * - 1.1.0 - adds madvise, and support for submits with > 4 cmd buffers 34 * - 1.2.0 - adds explicit fence support for submit ioctl 35 * - 1.3.0 - adds GMEM_BASE + NR_RINGS params, SUBMITQUEUE_NEW + 36 * SUBMITQUEUE_CLOSE ioctls, and MSM_INFO_IOVA flag for 37 * MSM_GEM_INFO ioctl. 38 * - 1.4.0 - softpin, MSM_RELOC_BO_DUMP, and GEM_INFO support to set/get 39 * GEM object's debug name 40 * - 1.5.0 - Add SUBMITQUERY_QUERY ioctl 41 * - 1.6.0 - Syncobj support 42 */ 43 #define MSM_VERSION_MAJOR 1 44 #define MSM_VERSION_MINOR 6 45 #define MSM_VERSION_PATCHLEVEL 0 46 47 static const struct drm_mode_config_funcs mode_config_funcs = { 48 .fb_create = msm_framebuffer_create, 49 .output_poll_changed = drm_fb_helper_output_poll_changed, 50 .atomic_check = drm_atomic_helper_check, 51 .atomic_commit = drm_atomic_helper_commit, 52 }; 53 54 static const struct drm_mode_config_helper_funcs mode_config_helper_funcs = { 55 .atomic_commit_tail = msm_atomic_commit_tail, 56 }; 57 58 #ifdef CONFIG_DRM_MSM_REGISTER_LOGGING 59 static bool reglog = false; 60 MODULE_PARM_DESC(reglog, "Enable register read/write logging"); 61 module_param(reglog, bool, 0600); 62 #else 63 #define reglog 0 64 #endif 65 66 #ifdef CONFIG_DRM_FBDEV_EMULATION 67 static bool fbdev = true; 68 MODULE_PARM_DESC(fbdev, "Enable fbdev compat layer"); 69 module_param(fbdev, bool, 0600); 70 #endif 71 72 static char *vram = "16m"; 73 MODULE_PARM_DESC(vram, "Configure VRAM size (for devices without IOMMU/GPUMMU)"); 74 module_param(vram, charp, 0); 75 76 bool dumpstate = false; 77 MODULE_PARM_DESC(dumpstate, "Dump KMS state on errors"); 78 module_param(dumpstate, bool, 0600); 79 80 static bool modeset = true; 81 MODULE_PARM_DESC(modeset, "Use kernel modesetting [KMS] (1=on (default), 0=disable)"); 82 module_param(modeset, bool, 0600); 83 84 /* 85 * Util/helpers: 86 */ 87 88 struct clk *msm_clk_bulk_get_clock(struct clk_bulk_data *bulk, int count, 89 const char *name) 90 { 91 int i; 92 char n[32]; 93 94 snprintf(n, sizeof(n), "%s_clk", name); 95 96 for (i = 0; bulk && i < count; i++) { 97 if (!strcmp(bulk[i].id, name) || !strcmp(bulk[i].id, n)) 98 return bulk[i].clk; 99 } 100 101 102 return NULL; 103 } 104 105 struct clk *msm_clk_get(struct platform_device *pdev, const char *name) 106 { 107 struct clk *clk; 108 char name2[32]; 109 110 clk = devm_clk_get(&pdev->dev, name); 111 if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER) 112 return clk; 113 114 snprintf(name2, sizeof(name2), "%s_clk", name); 115 116 clk = devm_clk_get(&pdev->dev, name2); 117 if (!IS_ERR(clk)) 118 dev_warn(&pdev->dev, "Using legacy clk name binding. Use " 119 "\"%s\" instead of \"%s\"\n", name, name2); 120 121 return clk; 122 } 123 124 static void __iomem *_msm_ioremap(struct platform_device *pdev, const char *name, 125 const char *dbgname, bool quiet) 126 { 127 struct resource *res; 128 unsigned long size; 129 void __iomem *ptr; 130 131 if (name) 132 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name); 133 else 134 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 135 136 if (!res) { 137 if (!quiet) 138 DRM_DEV_ERROR(&pdev->dev, "failed to get memory resource: %s\n", name); 139 return ERR_PTR(-EINVAL); 140 } 141 142 size = resource_size(res); 143 144 ptr = devm_ioremap(&pdev->dev, res->start, size); 145 if (!ptr) { 146 if (!quiet) 147 DRM_DEV_ERROR(&pdev->dev, "failed to ioremap: %s\n", name); 148 return ERR_PTR(-ENOMEM); 149 } 150 151 if (reglog) 152 printk(KERN_DEBUG "IO:region %s %p %08lx\n", dbgname, ptr, size); 153 154 return ptr; 155 } 156 157 void __iomem *msm_ioremap(struct platform_device *pdev, const char *name, 158 const char *dbgname) 159 { 160 return _msm_ioremap(pdev, name, dbgname, false); 161 } 162 163 void __iomem *msm_ioremap_quiet(struct platform_device *pdev, const char *name, 164 const char *dbgname) 165 { 166 return _msm_ioremap(pdev, name, dbgname, true); 167 } 168 169 void msm_writel(u32 data, void __iomem *addr) 170 { 171 if (reglog) 172 printk(KERN_DEBUG "IO:W %p %08x\n", addr, data); 173 writel(data, addr); 174 } 175 176 u32 msm_readl(const void __iomem *addr) 177 { 178 u32 val = readl(addr); 179 if (reglog) 180 pr_err("IO:R %p %08x\n", addr, val); 181 return val; 182 } 183 184 void msm_rmw(void __iomem *addr, u32 mask, u32 or) 185 { 186 u32 val = msm_readl(addr); 187 188 val &= ~mask; 189 msm_writel(val | or, addr); 190 } 191 192 struct msm_vblank_work { 193 struct work_struct work; 194 int crtc_id; 195 bool enable; 196 struct msm_drm_private *priv; 197 }; 198 199 static void vblank_ctrl_worker(struct work_struct *work) 200 { 201 struct msm_vblank_work *vbl_work = container_of(work, 202 struct msm_vblank_work, work); 203 struct msm_drm_private *priv = vbl_work->priv; 204 struct msm_kms *kms = priv->kms; 205 206 if (vbl_work->enable) 207 kms->funcs->enable_vblank(kms, priv->crtcs[vbl_work->crtc_id]); 208 else 209 kms->funcs->disable_vblank(kms, priv->crtcs[vbl_work->crtc_id]); 210 211 kfree(vbl_work); 212 } 213 214 static int vblank_ctrl_queue_work(struct msm_drm_private *priv, 215 int crtc_id, bool enable) 216 { 217 struct msm_vblank_work *vbl_work; 218 219 vbl_work = kzalloc(sizeof(*vbl_work), GFP_ATOMIC); 220 if (!vbl_work) 221 return -ENOMEM; 222 223 INIT_WORK(&vbl_work->work, vblank_ctrl_worker); 224 225 vbl_work->crtc_id = crtc_id; 226 vbl_work->enable = enable; 227 vbl_work->priv = priv; 228 229 queue_work(priv->wq, &vbl_work->work); 230 231 return 0; 232 } 233 234 static int msm_drm_uninit(struct device *dev) 235 { 236 struct platform_device *pdev = to_platform_device(dev); 237 struct drm_device *ddev = platform_get_drvdata(pdev); 238 struct msm_drm_private *priv = ddev->dev_private; 239 struct msm_kms *kms = priv->kms; 240 struct msm_mdss *mdss = priv->mdss; 241 int i; 242 243 /* 244 * Shutdown the hw if we're far enough along where things might be on. 245 * If we run this too early, we'll end up panicking in any variety of 246 * places. Since we don't register the drm device until late in 247 * msm_drm_init, drm_dev->registered is used as an indicator that the 248 * shutdown will be successful. 249 */ 250 if (ddev->registered) { 251 drm_dev_unregister(ddev); 252 drm_atomic_helper_shutdown(ddev); 253 } 254 255 /* We must cancel and cleanup any pending vblank enable/disable 256 * work before drm_irq_uninstall() to avoid work re-enabling an 257 * irq after uninstall has disabled it. 258 */ 259 260 flush_workqueue(priv->wq); 261 262 /* clean up event worker threads */ 263 for (i = 0; i < priv->num_crtcs; i++) { 264 if (priv->event_thread[i].worker) 265 kthread_destroy_worker(priv->event_thread[i].worker); 266 } 267 268 msm_gem_shrinker_cleanup(ddev); 269 270 drm_kms_helper_poll_fini(ddev); 271 272 msm_perf_debugfs_cleanup(priv); 273 msm_rd_debugfs_cleanup(priv); 274 275 #ifdef CONFIG_DRM_FBDEV_EMULATION 276 if (fbdev && priv->fbdev) 277 msm_fbdev_free(ddev); 278 #endif 279 280 drm_mode_config_cleanup(ddev); 281 282 pm_runtime_get_sync(dev); 283 drm_irq_uninstall(ddev); 284 pm_runtime_put_sync(dev); 285 286 if (kms && kms->funcs) 287 kms->funcs->destroy(kms); 288 289 if (priv->vram.paddr) { 290 unsigned long attrs = DMA_ATTR_NO_KERNEL_MAPPING; 291 drm_mm_takedown(&priv->vram.mm); 292 dma_free_attrs(dev, priv->vram.size, NULL, 293 priv->vram.paddr, attrs); 294 } 295 296 component_unbind_all(dev, ddev); 297 298 if (mdss && mdss->funcs) 299 mdss->funcs->destroy(ddev); 300 301 ddev->dev_private = NULL; 302 drm_dev_put(ddev); 303 304 destroy_workqueue(priv->wq); 305 kfree(priv); 306 307 return 0; 308 } 309 310 #define KMS_MDP4 4 311 #define KMS_MDP5 5 312 #define KMS_DPU 3 313 314 static int get_mdp_ver(struct platform_device *pdev) 315 { 316 struct device *dev = &pdev->dev; 317 318 return (int) (unsigned long) of_device_get_match_data(dev); 319 } 320 321 #include <linux/of_address.h> 322 323 bool msm_use_mmu(struct drm_device *dev) 324 { 325 struct msm_drm_private *priv = dev->dev_private; 326 327 /* a2xx comes with its own MMU */ 328 return priv->is_a2xx || iommu_present(&platform_bus_type); 329 } 330 331 static int msm_init_vram(struct drm_device *dev) 332 { 333 struct msm_drm_private *priv = dev->dev_private; 334 struct device_node *node; 335 unsigned long size = 0; 336 int ret = 0; 337 338 /* In the device-tree world, we could have a 'memory-region' 339 * phandle, which gives us a link to our "vram". Allocating 340 * is all nicely abstracted behind the dma api, but we need 341 * to know the entire size to allocate it all in one go. There 342 * are two cases: 343 * 1) device with no IOMMU, in which case we need exclusive 344 * access to a VRAM carveout big enough for all gpu 345 * buffers 346 * 2) device with IOMMU, but where the bootloader puts up 347 * a splash screen. In this case, the VRAM carveout 348 * need only be large enough for fbdev fb. But we need 349 * exclusive access to the buffer to avoid the kernel 350 * using those pages for other purposes (which appears 351 * as corruption on screen before we have a chance to 352 * load and do initial modeset) 353 */ 354 355 node = of_parse_phandle(dev->dev->of_node, "memory-region", 0); 356 if (node) { 357 struct resource r; 358 ret = of_address_to_resource(node, 0, &r); 359 of_node_put(node); 360 if (ret) 361 return ret; 362 size = r.end - r.start; 363 DRM_INFO("using VRAM carveout: %lx@%pa\n", size, &r.start); 364 365 /* if we have no IOMMU, then we need to use carveout allocator. 366 * Grab the entire CMA chunk carved out in early startup in 367 * mach-msm: 368 */ 369 } else if (!msm_use_mmu(dev)) { 370 DRM_INFO("using %s VRAM carveout\n", vram); 371 size = memparse(vram, NULL); 372 } 373 374 if (size) { 375 unsigned long attrs = 0; 376 void *p; 377 378 priv->vram.size = size; 379 380 drm_mm_init(&priv->vram.mm, 0, (size >> PAGE_SHIFT) - 1); 381 spin_lock_init(&priv->vram.lock); 382 383 attrs |= DMA_ATTR_NO_KERNEL_MAPPING; 384 attrs |= DMA_ATTR_WRITE_COMBINE; 385 386 /* note that for no-kernel-mapping, the vaddr returned 387 * is bogus, but non-null if allocation succeeded: 388 */ 389 p = dma_alloc_attrs(dev->dev, size, 390 &priv->vram.paddr, GFP_KERNEL, attrs); 391 if (!p) { 392 DRM_DEV_ERROR(dev->dev, "failed to allocate VRAM\n"); 393 priv->vram.paddr = 0; 394 return -ENOMEM; 395 } 396 397 DRM_DEV_INFO(dev->dev, "VRAM: %08x->%08x\n", 398 (uint32_t)priv->vram.paddr, 399 (uint32_t)(priv->vram.paddr + size)); 400 } 401 402 return ret; 403 } 404 405 static int msm_drm_init(struct device *dev, const struct drm_driver *drv) 406 { 407 struct platform_device *pdev = to_platform_device(dev); 408 struct drm_device *ddev; 409 struct msm_drm_private *priv; 410 struct msm_kms *kms; 411 struct msm_mdss *mdss; 412 int ret, i; 413 414 ddev = drm_dev_alloc(drv, dev); 415 if (IS_ERR(ddev)) { 416 DRM_DEV_ERROR(dev, "failed to allocate drm_device\n"); 417 return PTR_ERR(ddev); 418 } 419 420 platform_set_drvdata(pdev, ddev); 421 422 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 423 if (!priv) { 424 ret = -ENOMEM; 425 goto err_put_drm_dev; 426 } 427 428 ddev->dev_private = priv; 429 priv->dev = ddev; 430 431 switch (get_mdp_ver(pdev)) { 432 case KMS_MDP5: 433 ret = mdp5_mdss_init(ddev); 434 break; 435 case KMS_DPU: 436 ret = dpu_mdss_init(ddev); 437 break; 438 default: 439 ret = 0; 440 break; 441 } 442 if (ret) 443 goto err_free_priv; 444 445 mdss = priv->mdss; 446 447 priv->wq = alloc_ordered_workqueue("msm", 0); 448 449 INIT_LIST_HEAD(&priv->inactive_willneed); 450 INIT_LIST_HEAD(&priv->inactive_dontneed); 451 mutex_init(&priv->mm_lock); 452 453 /* Teach lockdep about lock ordering wrt. shrinker: */ 454 fs_reclaim_acquire(GFP_KERNEL); 455 might_lock(&priv->mm_lock); 456 fs_reclaim_release(GFP_KERNEL); 457 458 drm_mode_config_init(ddev); 459 460 ret = msm_init_vram(ddev); 461 if (ret) 462 goto err_destroy_mdss; 463 464 /* Bind all our sub-components: */ 465 ret = component_bind_all(dev, ddev); 466 if (ret) 467 goto err_destroy_mdss; 468 469 dma_set_max_seg_size(dev, UINT_MAX); 470 471 msm_gem_shrinker_init(ddev); 472 473 switch (get_mdp_ver(pdev)) { 474 case KMS_MDP4: 475 kms = mdp4_kms_init(ddev); 476 priv->kms = kms; 477 break; 478 case KMS_MDP5: 479 kms = mdp5_kms_init(ddev); 480 break; 481 case KMS_DPU: 482 kms = dpu_kms_init(ddev); 483 priv->kms = kms; 484 break; 485 default: 486 /* valid only for the dummy headless case, where of_node=NULL */ 487 WARN_ON(dev->of_node); 488 kms = NULL; 489 break; 490 } 491 492 if (IS_ERR(kms)) { 493 DRM_DEV_ERROR(dev, "failed to load kms\n"); 494 ret = PTR_ERR(kms); 495 priv->kms = NULL; 496 goto err_msm_uninit; 497 } 498 499 /* Enable normalization of plane zpos */ 500 ddev->mode_config.normalize_zpos = true; 501 502 if (kms) { 503 kms->dev = ddev; 504 ret = kms->funcs->hw_init(kms); 505 if (ret) { 506 DRM_DEV_ERROR(dev, "kms hw init failed: %d\n", ret); 507 goto err_msm_uninit; 508 } 509 } 510 511 ddev->mode_config.funcs = &mode_config_funcs; 512 ddev->mode_config.helper_private = &mode_config_helper_funcs; 513 514 for (i = 0; i < priv->num_crtcs; i++) { 515 /* initialize event thread */ 516 priv->event_thread[i].crtc_id = priv->crtcs[i]->base.id; 517 priv->event_thread[i].dev = ddev; 518 priv->event_thread[i].worker = kthread_create_worker(0, 519 "crtc_event:%d", priv->event_thread[i].crtc_id); 520 if (IS_ERR(priv->event_thread[i].worker)) { 521 DRM_DEV_ERROR(dev, "failed to create crtc_event kthread\n"); 522 goto err_msm_uninit; 523 } 524 525 sched_set_fifo(priv->event_thread[i].worker->task); 526 } 527 528 ret = drm_vblank_init(ddev, priv->num_crtcs); 529 if (ret < 0) { 530 DRM_DEV_ERROR(dev, "failed to initialize vblank\n"); 531 goto err_msm_uninit; 532 } 533 534 if (kms) { 535 pm_runtime_get_sync(dev); 536 ret = drm_irq_install(ddev, kms->irq); 537 pm_runtime_put_sync(dev); 538 if (ret < 0) { 539 DRM_DEV_ERROR(dev, "failed to install IRQ handler\n"); 540 goto err_msm_uninit; 541 } 542 } 543 544 ret = drm_dev_register(ddev, 0); 545 if (ret) 546 goto err_msm_uninit; 547 548 drm_mode_config_reset(ddev); 549 550 #ifdef CONFIG_DRM_FBDEV_EMULATION 551 if (kms && fbdev) 552 priv->fbdev = msm_fbdev_init(ddev); 553 #endif 554 555 ret = msm_debugfs_late_init(ddev); 556 if (ret) 557 goto err_msm_uninit; 558 559 drm_kms_helper_poll_init(ddev); 560 561 return 0; 562 563 err_msm_uninit: 564 msm_drm_uninit(dev); 565 return ret; 566 err_destroy_mdss: 567 if (mdss && mdss->funcs) 568 mdss->funcs->destroy(ddev); 569 err_free_priv: 570 kfree(priv); 571 err_put_drm_dev: 572 drm_dev_put(ddev); 573 return ret; 574 } 575 576 /* 577 * DRM operations: 578 */ 579 580 static void load_gpu(struct drm_device *dev) 581 { 582 static DEFINE_MUTEX(init_lock); 583 struct msm_drm_private *priv = dev->dev_private; 584 585 mutex_lock(&init_lock); 586 587 if (!priv->gpu) 588 priv->gpu = adreno_load_gpu(dev); 589 590 mutex_unlock(&init_lock); 591 } 592 593 static int context_init(struct drm_device *dev, struct drm_file *file) 594 { 595 struct msm_drm_private *priv = dev->dev_private; 596 struct msm_file_private *ctx; 597 598 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 599 if (!ctx) 600 return -ENOMEM; 601 602 kref_init(&ctx->ref); 603 msm_submitqueue_init(dev, ctx); 604 605 ctx->aspace = msm_gpu_create_private_address_space(priv->gpu, current); 606 file->driver_priv = ctx; 607 608 return 0; 609 } 610 611 static int msm_open(struct drm_device *dev, struct drm_file *file) 612 { 613 /* For now, load gpu on open.. to avoid the requirement of having 614 * firmware in the initrd. 615 */ 616 load_gpu(dev); 617 618 return context_init(dev, file); 619 } 620 621 static void context_close(struct msm_file_private *ctx) 622 { 623 msm_submitqueue_close(ctx); 624 msm_file_private_put(ctx); 625 } 626 627 static void msm_postclose(struct drm_device *dev, struct drm_file *file) 628 { 629 struct msm_drm_private *priv = dev->dev_private; 630 struct msm_file_private *ctx = file->driver_priv; 631 632 mutex_lock(&dev->struct_mutex); 633 if (ctx == priv->lastctx) 634 priv->lastctx = NULL; 635 mutex_unlock(&dev->struct_mutex); 636 637 context_close(ctx); 638 } 639 640 static irqreturn_t msm_irq(int irq, void *arg) 641 { 642 struct drm_device *dev = arg; 643 struct msm_drm_private *priv = dev->dev_private; 644 struct msm_kms *kms = priv->kms; 645 BUG_ON(!kms); 646 return kms->funcs->irq(kms); 647 } 648 649 static void msm_irq_preinstall(struct drm_device *dev) 650 { 651 struct msm_drm_private *priv = dev->dev_private; 652 struct msm_kms *kms = priv->kms; 653 BUG_ON(!kms); 654 kms->funcs->irq_preinstall(kms); 655 } 656 657 static int msm_irq_postinstall(struct drm_device *dev) 658 { 659 struct msm_drm_private *priv = dev->dev_private; 660 struct msm_kms *kms = priv->kms; 661 BUG_ON(!kms); 662 663 if (kms->funcs->irq_postinstall) 664 return kms->funcs->irq_postinstall(kms); 665 666 return 0; 667 } 668 669 static void msm_irq_uninstall(struct drm_device *dev) 670 { 671 struct msm_drm_private *priv = dev->dev_private; 672 struct msm_kms *kms = priv->kms; 673 BUG_ON(!kms); 674 kms->funcs->irq_uninstall(kms); 675 } 676 677 int msm_crtc_enable_vblank(struct drm_crtc *crtc) 678 { 679 struct drm_device *dev = crtc->dev; 680 unsigned int pipe = crtc->index; 681 struct msm_drm_private *priv = dev->dev_private; 682 struct msm_kms *kms = priv->kms; 683 if (!kms) 684 return -ENXIO; 685 DBG("dev=%p, crtc=%u", dev, pipe); 686 return vblank_ctrl_queue_work(priv, pipe, true); 687 } 688 689 void msm_crtc_disable_vblank(struct drm_crtc *crtc) 690 { 691 struct drm_device *dev = crtc->dev; 692 unsigned int pipe = crtc->index; 693 struct msm_drm_private *priv = dev->dev_private; 694 struct msm_kms *kms = priv->kms; 695 if (!kms) 696 return; 697 DBG("dev=%p, crtc=%u", dev, pipe); 698 vblank_ctrl_queue_work(priv, pipe, false); 699 } 700 701 /* 702 * DRM ioctls: 703 */ 704 705 static int msm_ioctl_get_param(struct drm_device *dev, void *data, 706 struct drm_file *file) 707 { 708 struct msm_drm_private *priv = dev->dev_private; 709 struct drm_msm_param *args = data; 710 struct msm_gpu *gpu; 711 712 /* for now, we just have 3d pipe.. eventually this would need to 713 * be more clever to dispatch to appropriate gpu module: 714 */ 715 if (args->pipe != MSM_PIPE_3D0) 716 return -EINVAL; 717 718 gpu = priv->gpu; 719 720 if (!gpu) 721 return -ENXIO; 722 723 return gpu->funcs->get_param(gpu, args->param, &args->value); 724 } 725 726 static int msm_ioctl_gem_new(struct drm_device *dev, void *data, 727 struct drm_file *file) 728 { 729 struct drm_msm_gem_new *args = data; 730 731 if (args->flags & ~MSM_BO_FLAGS) { 732 DRM_ERROR("invalid flags: %08x\n", args->flags); 733 return -EINVAL; 734 } 735 736 return msm_gem_new_handle(dev, file, args->size, 737 args->flags, &args->handle, NULL); 738 } 739 740 static inline ktime_t to_ktime(struct drm_msm_timespec timeout) 741 { 742 return ktime_set(timeout.tv_sec, timeout.tv_nsec); 743 } 744 745 static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data, 746 struct drm_file *file) 747 { 748 struct drm_msm_gem_cpu_prep *args = data; 749 struct drm_gem_object *obj; 750 ktime_t timeout = to_ktime(args->timeout); 751 int ret; 752 753 if (args->op & ~MSM_PREP_FLAGS) { 754 DRM_ERROR("invalid op: %08x\n", args->op); 755 return -EINVAL; 756 } 757 758 obj = drm_gem_object_lookup(file, args->handle); 759 if (!obj) 760 return -ENOENT; 761 762 ret = msm_gem_cpu_prep(obj, args->op, &timeout); 763 764 drm_gem_object_put(obj); 765 766 return ret; 767 } 768 769 static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data, 770 struct drm_file *file) 771 { 772 struct drm_msm_gem_cpu_fini *args = data; 773 struct drm_gem_object *obj; 774 int ret; 775 776 obj = drm_gem_object_lookup(file, args->handle); 777 if (!obj) 778 return -ENOENT; 779 780 ret = msm_gem_cpu_fini(obj); 781 782 drm_gem_object_put(obj); 783 784 return ret; 785 } 786 787 static int msm_ioctl_gem_info_iova(struct drm_device *dev, 788 struct drm_file *file, struct drm_gem_object *obj, 789 uint64_t *iova) 790 { 791 struct msm_drm_private *priv = dev->dev_private; 792 struct msm_file_private *ctx = file->driver_priv; 793 794 if (!priv->gpu) 795 return -EINVAL; 796 797 /* 798 * Don't pin the memory here - just get an address so that userspace can 799 * be productive 800 */ 801 return msm_gem_get_iova(obj, ctx->aspace, iova); 802 } 803 804 static int msm_ioctl_gem_info(struct drm_device *dev, void *data, 805 struct drm_file *file) 806 { 807 struct drm_msm_gem_info *args = data; 808 struct drm_gem_object *obj; 809 struct msm_gem_object *msm_obj; 810 int i, ret = 0; 811 812 if (args->pad) 813 return -EINVAL; 814 815 switch (args->info) { 816 case MSM_INFO_GET_OFFSET: 817 case MSM_INFO_GET_IOVA: 818 /* value returned as immediate, not pointer, so len==0: */ 819 if (args->len) 820 return -EINVAL; 821 break; 822 case MSM_INFO_SET_NAME: 823 case MSM_INFO_GET_NAME: 824 break; 825 default: 826 return -EINVAL; 827 } 828 829 obj = drm_gem_object_lookup(file, args->handle); 830 if (!obj) 831 return -ENOENT; 832 833 msm_obj = to_msm_bo(obj); 834 835 switch (args->info) { 836 case MSM_INFO_GET_OFFSET: 837 args->value = msm_gem_mmap_offset(obj); 838 break; 839 case MSM_INFO_GET_IOVA: 840 ret = msm_ioctl_gem_info_iova(dev, file, obj, &args->value); 841 break; 842 case MSM_INFO_SET_NAME: 843 /* length check should leave room for terminating null: */ 844 if (args->len >= sizeof(msm_obj->name)) { 845 ret = -EINVAL; 846 break; 847 } 848 if (copy_from_user(msm_obj->name, u64_to_user_ptr(args->value), 849 args->len)) { 850 msm_obj->name[0] = '\0'; 851 ret = -EFAULT; 852 break; 853 } 854 msm_obj->name[args->len] = '\0'; 855 for (i = 0; i < args->len; i++) { 856 if (!isprint(msm_obj->name[i])) { 857 msm_obj->name[i] = '\0'; 858 break; 859 } 860 } 861 break; 862 case MSM_INFO_GET_NAME: 863 if (args->value && (args->len < strlen(msm_obj->name))) { 864 ret = -EINVAL; 865 break; 866 } 867 args->len = strlen(msm_obj->name); 868 if (args->value) { 869 if (copy_to_user(u64_to_user_ptr(args->value), 870 msm_obj->name, args->len)) 871 ret = -EFAULT; 872 } 873 break; 874 } 875 876 drm_gem_object_put(obj); 877 878 return ret; 879 } 880 881 static int msm_ioctl_wait_fence(struct drm_device *dev, void *data, 882 struct drm_file *file) 883 { 884 struct msm_drm_private *priv = dev->dev_private; 885 struct drm_msm_wait_fence *args = data; 886 ktime_t timeout = to_ktime(args->timeout); 887 struct msm_gpu_submitqueue *queue; 888 struct msm_gpu *gpu = priv->gpu; 889 int ret; 890 891 if (args->pad) { 892 DRM_ERROR("invalid pad: %08x\n", args->pad); 893 return -EINVAL; 894 } 895 896 if (!gpu) 897 return 0; 898 899 queue = msm_submitqueue_get(file->driver_priv, args->queueid); 900 if (!queue) 901 return -ENOENT; 902 903 ret = msm_wait_fence(gpu->rb[queue->prio]->fctx, args->fence, &timeout, 904 true); 905 906 msm_submitqueue_put(queue); 907 return ret; 908 } 909 910 static int msm_ioctl_gem_madvise(struct drm_device *dev, void *data, 911 struct drm_file *file) 912 { 913 struct drm_msm_gem_madvise *args = data; 914 struct drm_gem_object *obj; 915 int ret; 916 917 switch (args->madv) { 918 case MSM_MADV_DONTNEED: 919 case MSM_MADV_WILLNEED: 920 break; 921 default: 922 return -EINVAL; 923 } 924 925 obj = drm_gem_object_lookup(file, args->handle); 926 if (!obj) { 927 return -ENOENT; 928 } 929 930 ret = msm_gem_madvise(obj, args->madv); 931 if (ret >= 0) { 932 args->retained = ret; 933 ret = 0; 934 } 935 936 drm_gem_object_put(obj); 937 938 return ret; 939 } 940 941 942 static int msm_ioctl_submitqueue_new(struct drm_device *dev, void *data, 943 struct drm_file *file) 944 { 945 struct drm_msm_submitqueue *args = data; 946 947 if (args->flags & ~MSM_SUBMITQUEUE_FLAGS) 948 return -EINVAL; 949 950 return msm_submitqueue_create(dev, file->driver_priv, args->prio, 951 args->flags, &args->id); 952 } 953 954 static int msm_ioctl_submitqueue_query(struct drm_device *dev, void *data, 955 struct drm_file *file) 956 { 957 return msm_submitqueue_query(dev, file->driver_priv, data); 958 } 959 960 static int msm_ioctl_submitqueue_close(struct drm_device *dev, void *data, 961 struct drm_file *file) 962 { 963 u32 id = *(u32 *) data; 964 965 return msm_submitqueue_remove(file->driver_priv, id); 966 } 967 968 static const struct drm_ioctl_desc msm_ioctls[] = { 969 DRM_IOCTL_DEF_DRV(MSM_GET_PARAM, msm_ioctl_get_param, DRM_RENDER_ALLOW), 970 DRM_IOCTL_DEF_DRV(MSM_GEM_NEW, msm_ioctl_gem_new, DRM_RENDER_ALLOW), 971 DRM_IOCTL_DEF_DRV(MSM_GEM_INFO, msm_ioctl_gem_info, DRM_RENDER_ALLOW), 972 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_RENDER_ALLOW), 973 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_RENDER_ALLOW), 974 DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT, msm_ioctl_gem_submit, DRM_RENDER_ALLOW), 975 DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE, msm_ioctl_wait_fence, DRM_RENDER_ALLOW), 976 DRM_IOCTL_DEF_DRV(MSM_GEM_MADVISE, msm_ioctl_gem_madvise, DRM_RENDER_ALLOW), 977 DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_NEW, msm_ioctl_submitqueue_new, DRM_RENDER_ALLOW), 978 DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_CLOSE, msm_ioctl_submitqueue_close, DRM_RENDER_ALLOW), 979 DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_QUERY, msm_ioctl_submitqueue_query, DRM_RENDER_ALLOW), 980 }; 981 982 static const struct file_operations fops = { 983 .owner = THIS_MODULE, 984 .open = drm_open, 985 .release = drm_release, 986 .unlocked_ioctl = drm_ioctl, 987 .compat_ioctl = drm_compat_ioctl, 988 .poll = drm_poll, 989 .read = drm_read, 990 .llseek = no_llseek, 991 .mmap = msm_gem_mmap, 992 }; 993 994 static const struct drm_driver msm_driver = { 995 .driver_features = DRIVER_GEM | 996 DRIVER_RENDER | 997 DRIVER_ATOMIC | 998 DRIVER_MODESET | 999 DRIVER_SYNCOBJ, 1000 .open = msm_open, 1001 .postclose = msm_postclose, 1002 .lastclose = drm_fb_helper_lastclose, 1003 .irq_handler = msm_irq, 1004 .irq_preinstall = msm_irq_preinstall, 1005 .irq_postinstall = msm_irq_postinstall, 1006 .irq_uninstall = msm_irq_uninstall, 1007 .dumb_create = msm_gem_dumb_create, 1008 .dumb_map_offset = msm_gem_dumb_map_offset, 1009 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, 1010 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, 1011 .gem_prime_import_sg_table = msm_gem_prime_import_sg_table, 1012 .gem_prime_mmap = msm_gem_prime_mmap, 1013 #ifdef CONFIG_DEBUG_FS 1014 .debugfs_init = msm_debugfs_init, 1015 #endif 1016 .ioctls = msm_ioctls, 1017 .num_ioctls = ARRAY_SIZE(msm_ioctls), 1018 .fops = &fops, 1019 .name = "msm", 1020 .desc = "MSM Snapdragon DRM", 1021 .date = "20130625", 1022 .major = MSM_VERSION_MAJOR, 1023 .minor = MSM_VERSION_MINOR, 1024 .patchlevel = MSM_VERSION_PATCHLEVEL, 1025 }; 1026 1027 static int __maybe_unused msm_runtime_suspend(struct device *dev) 1028 { 1029 struct drm_device *ddev = dev_get_drvdata(dev); 1030 struct msm_drm_private *priv = ddev->dev_private; 1031 struct msm_mdss *mdss = priv->mdss; 1032 1033 DBG(""); 1034 1035 if (mdss && mdss->funcs) 1036 return mdss->funcs->disable(mdss); 1037 1038 return 0; 1039 } 1040 1041 static int __maybe_unused msm_runtime_resume(struct device *dev) 1042 { 1043 struct drm_device *ddev = dev_get_drvdata(dev); 1044 struct msm_drm_private *priv = ddev->dev_private; 1045 struct msm_mdss *mdss = priv->mdss; 1046 1047 DBG(""); 1048 1049 if (mdss && mdss->funcs) 1050 return mdss->funcs->enable(mdss); 1051 1052 return 0; 1053 } 1054 1055 static int __maybe_unused msm_pm_suspend(struct device *dev) 1056 { 1057 1058 if (pm_runtime_suspended(dev)) 1059 return 0; 1060 1061 return msm_runtime_suspend(dev); 1062 } 1063 1064 static int __maybe_unused msm_pm_resume(struct device *dev) 1065 { 1066 if (pm_runtime_suspended(dev)) 1067 return 0; 1068 1069 return msm_runtime_resume(dev); 1070 } 1071 1072 static int __maybe_unused msm_pm_prepare(struct device *dev) 1073 { 1074 struct drm_device *ddev = dev_get_drvdata(dev); 1075 1076 return drm_mode_config_helper_suspend(ddev); 1077 } 1078 1079 static void __maybe_unused msm_pm_complete(struct device *dev) 1080 { 1081 struct drm_device *ddev = dev_get_drvdata(dev); 1082 1083 drm_mode_config_helper_resume(ddev); 1084 } 1085 1086 static const struct dev_pm_ops msm_pm_ops = { 1087 SET_SYSTEM_SLEEP_PM_OPS(msm_pm_suspend, msm_pm_resume) 1088 SET_RUNTIME_PM_OPS(msm_runtime_suspend, msm_runtime_resume, NULL) 1089 .prepare = msm_pm_prepare, 1090 .complete = msm_pm_complete, 1091 }; 1092 1093 /* 1094 * Componentized driver support: 1095 */ 1096 1097 /* 1098 * NOTE: duplication of the same code as exynos or imx (or probably any other). 1099 * so probably some room for some helpers 1100 */ 1101 static int compare_of(struct device *dev, void *data) 1102 { 1103 return dev->of_node == data; 1104 } 1105 1106 /* 1107 * Identify what components need to be added by parsing what remote-endpoints 1108 * our MDP output ports are connected to. In the case of LVDS on MDP4, there 1109 * is no external component that we need to add since LVDS is within MDP4 1110 * itself. 1111 */ 1112 static int add_components_mdp(struct device *mdp_dev, 1113 struct component_match **matchptr) 1114 { 1115 struct device_node *np = mdp_dev->of_node; 1116 struct device_node *ep_node; 1117 struct device *master_dev; 1118 1119 /* 1120 * on MDP4 based platforms, the MDP platform device is the component 1121 * master that adds other display interface components to itself. 1122 * 1123 * on MDP5 based platforms, the MDSS platform device is the component 1124 * master that adds MDP5 and other display interface components to 1125 * itself. 1126 */ 1127 if (of_device_is_compatible(np, "qcom,mdp4")) 1128 master_dev = mdp_dev; 1129 else 1130 master_dev = mdp_dev->parent; 1131 1132 for_each_endpoint_of_node(np, ep_node) { 1133 struct device_node *intf; 1134 struct of_endpoint ep; 1135 int ret; 1136 1137 ret = of_graph_parse_endpoint(ep_node, &ep); 1138 if (ret) { 1139 DRM_DEV_ERROR(mdp_dev, "unable to parse port endpoint\n"); 1140 of_node_put(ep_node); 1141 return ret; 1142 } 1143 1144 /* 1145 * The LCDC/LVDS port on MDP4 is a speacial case where the 1146 * remote-endpoint isn't a component that we need to add 1147 */ 1148 if (of_device_is_compatible(np, "qcom,mdp4") && 1149 ep.port == 0) 1150 continue; 1151 1152 /* 1153 * It's okay if some of the ports don't have a remote endpoint 1154 * specified. It just means that the port isn't connected to 1155 * any external interface. 1156 */ 1157 intf = of_graph_get_remote_port_parent(ep_node); 1158 if (!intf) 1159 continue; 1160 1161 if (of_device_is_available(intf)) 1162 drm_of_component_match_add(master_dev, matchptr, 1163 compare_of, intf); 1164 1165 of_node_put(intf); 1166 } 1167 1168 return 0; 1169 } 1170 1171 static int compare_name_mdp(struct device *dev, void *data) 1172 { 1173 return (strstr(dev_name(dev), "mdp") != NULL); 1174 } 1175 1176 static int add_display_components(struct device *dev, 1177 struct component_match **matchptr) 1178 { 1179 struct device *mdp_dev; 1180 int ret; 1181 1182 /* 1183 * MDP5/DPU based devices don't have a flat hierarchy. There is a top 1184 * level parent: MDSS, and children: MDP5/DPU, DSI, HDMI, eDP etc. 1185 * Populate the children devices, find the MDP5/DPU node, and then add 1186 * the interfaces to our components list. 1187 */ 1188 if (of_device_is_compatible(dev->of_node, "qcom,mdss") || 1189 of_device_is_compatible(dev->of_node, "qcom,sdm845-mdss") || 1190 of_device_is_compatible(dev->of_node, "qcom,sc7180-mdss")) { 1191 ret = of_platform_populate(dev->of_node, NULL, NULL, dev); 1192 if (ret) { 1193 DRM_DEV_ERROR(dev, "failed to populate children devices\n"); 1194 return ret; 1195 } 1196 1197 mdp_dev = device_find_child(dev, NULL, compare_name_mdp); 1198 if (!mdp_dev) { 1199 DRM_DEV_ERROR(dev, "failed to find MDSS MDP node\n"); 1200 of_platform_depopulate(dev); 1201 return -ENODEV; 1202 } 1203 1204 put_device(mdp_dev); 1205 1206 /* add the MDP component itself */ 1207 drm_of_component_match_add(dev, matchptr, compare_of, 1208 mdp_dev->of_node); 1209 } else { 1210 /* MDP4 */ 1211 mdp_dev = dev; 1212 } 1213 1214 ret = add_components_mdp(mdp_dev, matchptr); 1215 if (ret) 1216 of_platform_depopulate(dev); 1217 1218 return ret; 1219 } 1220 1221 /* 1222 * We don't know what's the best binding to link the gpu with the drm device. 1223 * Fow now, we just hunt for all the possible gpus that we support, and add them 1224 * as components. 1225 */ 1226 static const struct of_device_id msm_gpu_match[] = { 1227 { .compatible = "qcom,adreno" }, 1228 { .compatible = "qcom,adreno-3xx" }, 1229 { .compatible = "amd,imageon" }, 1230 { .compatible = "qcom,kgsl-3d0" }, 1231 { }, 1232 }; 1233 1234 static int add_gpu_components(struct device *dev, 1235 struct component_match **matchptr) 1236 { 1237 struct device_node *np; 1238 1239 np = of_find_matching_node(NULL, msm_gpu_match); 1240 if (!np) 1241 return 0; 1242 1243 if (of_device_is_available(np)) 1244 drm_of_component_match_add(dev, matchptr, compare_of, np); 1245 1246 of_node_put(np); 1247 1248 return 0; 1249 } 1250 1251 static int msm_drm_bind(struct device *dev) 1252 { 1253 return msm_drm_init(dev, &msm_driver); 1254 } 1255 1256 static void msm_drm_unbind(struct device *dev) 1257 { 1258 msm_drm_uninit(dev); 1259 } 1260 1261 static const struct component_master_ops msm_drm_ops = { 1262 .bind = msm_drm_bind, 1263 .unbind = msm_drm_unbind, 1264 }; 1265 1266 /* 1267 * Platform driver: 1268 */ 1269 1270 static int msm_pdev_probe(struct platform_device *pdev) 1271 { 1272 struct component_match *match = NULL; 1273 int ret; 1274 1275 if (get_mdp_ver(pdev)) { 1276 ret = add_display_components(&pdev->dev, &match); 1277 if (ret) 1278 return ret; 1279 } 1280 1281 ret = add_gpu_components(&pdev->dev, &match); 1282 if (ret) 1283 goto fail; 1284 1285 /* on all devices that I am aware of, iommu's which can map 1286 * any address the cpu can see are used: 1287 */ 1288 ret = dma_set_mask_and_coherent(&pdev->dev, ~0); 1289 if (ret) 1290 goto fail; 1291 1292 ret = component_master_add_with_match(&pdev->dev, &msm_drm_ops, match); 1293 if (ret) 1294 goto fail; 1295 1296 return 0; 1297 1298 fail: 1299 of_platform_depopulate(&pdev->dev); 1300 return ret; 1301 } 1302 1303 static int msm_pdev_remove(struct platform_device *pdev) 1304 { 1305 component_master_del(&pdev->dev, &msm_drm_ops); 1306 of_platform_depopulate(&pdev->dev); 1307 1308 return 0; 1309 } 1310 1311 static void msm_pdev_shutdown(struct platform_device *pdev) 1312 { 1313 struct drm_device *drm = platform_get_drvdata(pdev); 1314 1315 drm_atomic_helper_shutdown(drm); 1316 } 1317 1318 static const struct of_device_id dt_match[] = { 1319 { .compatible = "qcom,mdp4", .data = (void *)KMS_MDP4 }, 1320 { .compatible = "qcom,mdss", .data = (void *)KMS_MDP5 }, 1321 { .compatible = "qcom,sdm845-mdss", .data = (void *)KMS_DPU }, 1322 { .compatible = "qcom,sc7180-mdss", .data = (void *)KMS_DPU }, 1323 {} 1324 }; 1325 MODULE_DEVICE_TABLE(of, dt_match); 1326 1327 static struct platform_driver msm_platform_driver = { 1328 .probe = msm_pdev_probe, 1329 .remove = msm_pdev_remove, 1330 .shutdown = msm_pdev_shutdown, 1331 .driver = { 1332 .name = "msm", 1333 .of_match_table = dt_match, 1334 .pm = &msm_pm_ops, 1335 }, 1336 }; 1337 1338 static int __init msm_drm_register(void) 1339 { 1340 if (!modeset) 1341 return -EINVAL; 1342 1343 DBG("init"); 1344 msm_mdp_register(); 1345 msm_dpu_register(); 1346 msm_dsi_register(); 1347 msm_edp_register(); 1348 msm_hdmi_register(); 1349 msm_dp_register(); 1350 adreno_register(); 1351 return platform_driver_register(&msm_platform_driver); 1352 } 1353 1354 static void __exit msm_drm_unregister(void) 1355 { 1356 DBG("fini"); 1357 platform_driver_unregister(&msm_platform_driver); 1358 msm_dp_unregister(); 1359 msm_hdmi_unregister(); 1360 adreno_unregister(); 1361 msm_edp_unregister(); 1362 msm_dsi_unregister(); 1363 msm_mdp_unregister(); 1364 msm_dpu_unregister(); 1365 } 1366 1367 module_init(msm_drm_register); 1368 module_exit(msm_drm_unregister); 1369 1370 MODULE_AUTHOR("Rob Clark <robdclark@gmail.com"); 1371 MODULE_DESCRIPTION("MSM DRM Driver"); 1372 MODULE_LICENSE("GPL"); 1373