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