1 /* 2 * Copyright (C) 2013 Red Hat 3 * Author: Rob Clark <robdclark@gmail.com> 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 as published by 7 * the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 * more details. 13 * 14 * You should have received a copy of the GNU General Public License along with 15 * this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #include <drm/drm_of.h> 19 20 #include "msm_drv.h" 21 #include "msm_debugfs.h" 22 #include "msm_fence.h" 23 #include "msm_gpu.h" 24 #include "msm_kms.h" 25 26 27 /* 28 * MSM driver version: 29 * - 1.0.0 - initial interface 30 * - 1.1.0 - adds madvise, and support for submits with > 4 cmd buffers 31 * - 1.2.0 - adds explicit fence support for submit ioctl 32 */ 33 #define MSM_VERSION_MAJOR 1 34 #define MSM_VERSION_MINOR 2 35 #define MSM_VERSION_PATCHLEVEL 0 36 37 static void msm_fb_output_poll_changed(struct drm_device *dev) 38 { 39 struct msm_drm_private *priv = dev->dev_private; 40 if (priv->fbdev) 41 drm_fb_helper_hotplug_event(priv->fbdev); 42 } 43 44 static const struct drm_mode_config_funcs mode_config_funcs = { 45 .fb_create = msm_framebuffer_create, 46 .output_poll_changed = msm_fb_output_poll_changed, 47 .atomic_check = msm_atomic_check, 48 .atomic_commit = msm_atomic_commit, 49 .atomic_state_alloc = msm_atomic_state_alloc, 50 .atomic_state_clear = msm_atomic_state_clear, 51 .atomic_state_free = msm_atomic_state_free, 52 }; 53 54 #ifdef CONFIG_DRM_MSM_REGISTER_LOGGING 55 static bool reglog = false; 56 MODULE_PARM_DESC(reglog, "Enable register read/write logging"); 57 module_param(reglog, bool, 0600); 58 #else 59 #define reglog 0 60 #endif 61 62 #ifdef CONFIG_DRM_FBDEV_EMULATION 63 static bool fbdev = true; 64 MODULE_PARM_DESC(fbdev, "Enable fbdev compat layer"); 65 module_param(fbdev, bool, 0600); 66 #endif 67 68 static char *vram = "16m"; 69 MODULE_PARM_DESC(vram, "Configure VRAM size (for devices without IOMMU/GPUMMU)"); 70 module_param(vram, charp, 0); 71 72 bool dumpstate = false; 73 MODULE_PARM_DESC(dumpstate, "Dump KMS state on errors"); 74 module_param(dumpstate, bool, 0600); 75 76 static bool modeset = true; 77 MODULE_PARM_DESC(modeset, "Use kernel modesetting [KMS] (1=on (default), 0=disable)"); 78 module_param(modeset, bool, 0600); 79 80 /* 81 * Util/helpers: 82 */ 83 84 struct clk *msm_clk_get(struct platform_device *pdev, const char *name) 85 { 86 struct clk *clk; 87 char name2[32]; 88 89 clk = devm_clk_get(&pdev->dev, name); 90 if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER) 91 return clk; 92 93 snprintf(name2, sizeof(name2), "%s_clk", name); 94 95 clk = devm_clk_get(&pdev->dev, name2); 96 if (!IS_ERR(clk)) 97 dev_warn(&pdev->dev, "Using legacy clk name binding. Use " 98 "\"%s\" instead of \"%s\"\n", name, name2); 99 100 return clk; 101 } 102 103 void __iomem *msm_ioremap(struct platform_device *pdev, const char *name, 104 const char *dbgname) 105 { 106 struct resource *res; 107 unsigned long size; 108 void __iomem *ptr; 109 110 if (name) 111 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name); 112 else 113 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 114 115 if (!res) { 116 dev_err(&pdev->dev, "failed to get memory resource: %s\n", name); 117 return ERR_PTR(-EINVAL); 118 } 119 120 size = resource_size(res); 121 122 ptr = devm_ioremap_nocache(&pdev->dev, res->start, size); 123 if (!ptr) { 124 dev_err(&pdev->dev, "failed to ioremap: %s\n", name); 125 return ERR_PTR(-ENOMEM); 126 } 127 128 if (reglog) 129 printk(KERN_DEBUG "IO:region %s %p %08lx\n", dbgname, ptr, size); 130 131 return ptr; 132 } 133 134 void msm_writel(u32 data, void __iomem *addr) 135 { 136 if (reglog) 137 printk(KERN_DEBUG "IO:W %p %08x\n", addr, data); 138 writel(data, addr); 139 } 140 141 u32 msm_readl(const void __iomem *addr) 142 { 143 u32 val = readl(addr); 144 if (reglog) 145 pr_err("IO:R %p %08x\n", addr, val); 146 return val; 147 } 148 149 struct vblank_event { 150 struct list_head node; 151 int crtc_id; 152 bool enable; 153 }; 154 155 static void vblank_ctrl_worker(struct work_struct *work) 156 { 157 struct msm_vblank_ctrl *vbl_ctrl = container_of(work, 158 struct msm_vblank_ctrl, work); 159 struct msm_drm_private *priv = container_of(vbl_ctrl, 160 struct msm_drm_private, vblank_ctrl); 161 struct msm_kms *kms = priv->kms; 162 struct vblank_event *vbl_ev, *tmp; 163 unsigned long flags; 164 165 spin_lock_irqsave(&vbl_ctrl->lock, flags); 166 list_for_each_entry_safe(vbl_ev, tmp, &vbl_ctrl->event_list, node) { 167 list_del(&vbl_ev->node); 168 spin_unlock_irqrestore(&vbl_ctrl->lock, flags); 169 170 if (vbl_ev->enable) 171 kms->funcs->enable_vblank(kms, 172 priv->crtcs[vbl_ev->crtc_id]); 173 else 174 kms->funcs->disable_vblank(kms, 175 priv->crtcs[vbl_ev->crtc_id]); 176 177 kfree(vbl_ev); 178 179 spin_lock_irqsave(&vbl_ctrl->lock, flags); 180 } 181 182 spin_unlock_irqrestore(&vbl_ctrl->lock, flags); 183 } 184 185 static int vblank_ctrl_queue_work(struct msm_drm_private *priv, 186 int crtc_id, bool enable) 187 { 188 struct msm_vblank_ctrl *vbl_ctrl = &priv->vblank_ctrl; 189 struct vblank_event *vbl_ev; 190 unsigned long flags; 191 192 vbl_ev = kzalloc(sizeof(*vbl_ev), GFP_ATOMIC); 193 if (!vbl_ev) 194 return -ENOMEM; 195 196 vbl_ev->crtc_id = crtc_id; 197 vbl_ev->enable = enable; 198 199 spin_lock_irqsave(&vbl_ctrl->lock, flags); 200 list_add_tail(&vbl_ev->node, &vbl_ctrl->event_list); 201 spin_unlock_irqrestore(&vbl_ctrl->lock, flags); 202 203 queue_work(priv->wq, &vbl_ctrl->work); 204 205 return 0; 206 } 207 208 static int msm_drm_uninit(struct device *dev) 209 { 210 struct platform_device *pdev = to_platform_device(dev); 211 struct drm_device *ddev = platform_get_drvdata(pdev); 212 struct msm_drm_private *priv = ddev->dev_private; 213 struct msm_kms *kms = priv->kms; 214 struct msm_gpu *gpu = priv->gpu; 215 struct msm_vblank_ctrl *vbl_ctrl = &priv->vblank_ctrl; 216 struct vblank_event *vbl_ev, *tmp; 217 218 /* We must cancel and cleanup any pending vblank enable/disable 219 * work before drm_irq_uninstall() to avoid work re-enabling an 220 * irq after uninstall has disabled it. 221 */ 222 cancel_work_sync(&vbl_ctrl->work); 223 list_for_each_entry_safe(vbl_ev, tmp, &vbl_ctrl->event_list, node) { 224 list_del(&vbl_ev->node); 225 kfree(vbl_ev); 226 } 227 228 msm_gem_shrinker_cleanup(ddev); 229 230 drm_kms_helper_poll_fini(ddev); 231 232 drm_dev_unregister(ddev); 233 234 msm_perf_debugfs_cleanup(priv); 235 msm_rd_debugfs_cleanup(priv); 236 237 #ifdef CONFIG_DRM_FBDEV_EMULATION 238 if (fbdev && priv->fbdev) 239 msm_fbdev_free(ddev); 240 #endif 241 drm_mode_config_cleanup(ddev); 242 243 pm_runtime_get_sync(dev); 244 drm_irq_uninstall(ddev); 245 pm_runtime_put_sync(dev); 246 247 flush_workqueue(priv->wq); 248 destroy_workqueue(priv->wq); 249 250 flush_workqueue(priv->atomic_wq); 251 destroy_workqueue(priv->atomic_wq); 252 253 if (kms && kms->funcs) 254 kms->funcs->destroy(kms); 255 256 if (gpu) { 257 mutex_lock(&ddev->struct_mutex); 258 // XXX what do we do here? 259 //pm_runtime_enable(&pdev->dev); 260 gpu->funcs->pm_suspend(gpu); 261 mutex_unlock(&ddev->struct_mutex); 262 gpu->funcs->destroy(gpu); 263 } 264 265 if (priv->vram.paddr) { 266 unsigned long attrs = DMA_ATTR_NO_KERNEL_MAPPING; 267 drm_mm_takedown(&priv->vram.mm); 268 dma_free_attrs(dev, priv->vram.size, NULL, 269 priv->vram.paddr, attrs); 270 } 271 272 component_unbind_all(dev, ddev); 273 274 msm_mdss_destroy(ddev); 275 276 ddev->dev_private = NULL; 277 drm_dev_unref(ddev); 278 279 kfree(priv); 280 281 return 0; 282 } 283 284 static int get_mdp_ver(struct platform_device *pdev) 285 { 286 struct device *dev = &pdev->dev; 287 288 return (int) (unsigned long) of_device_get_match_data(dev); 289 } 290 291 #include <linux/of_address.h> 292 293 static int msm_init_vram(struct drm_device *dev) 294 { 295 struct msm_drm_private *priv = dev->dev_private; 296 struct device_node *node; 297 unsigned long size = 0; 298 int ret = 0; 299 300 /* In the device-tree world, we could have a 'memory-region' 301 * phandle, which gives us a link to our "vram". Allocating 302 * is all nicely abstracted behind the dma api, but we need 303 * to know the entire size to allocate it all in one go. There 304 * are two cases: 305 * 1) device with no IOMMU, in which case we need exclusive 306 * access to a VRAM carveout big enough for all gpu 307 * buffers 308 * 2) device with IOMMU, but where the bootloader puts up 309 * a splash screen. In this case, the VRAM carveout 310 * need only be large enough for fbdev fb. But we need 311 * exclusive access to the buffer to avoid the kernel 312 * using those pages for other purposes (which appears 313 * as corruption on screen before we have a chance to 314 * load and do initial modeset) 315 */ 316 317 node = of_parse_phandle(dev->dev->of_node, "memory-region", 0); 318 if (node) { 319 struct resource r; 320 ret = of_address_to_resource(node, 0, &r); 321 of_node_put(node); 322 if (ret) 323 return ret; 324 size = r.end - r.start; 325 DRM_INFO("using VRAM carveout: %lx@%pa\n", size, &r.start); 326 327 /* if we have no IOMMU, then we need to use carveout allocator. 328 * Grab the entire CMA chunk carved out in early startup in 329 * mach-msm: 330 */ 331 } else if (!iommu_present(&platform_bus_type)) { 332 DRM_INFO("using %s VRAM carveout\n", vram); 333 size = memparse(vram, NULL); 334 } 335 336 if (size) { 337 unsigned long attrs = 0; 338 void *p; 339 340 priv->vram.size = size; 341 342 drm_mm_init(&priv->vram.mm, 0, (size >> PAGE_SHIFT) - 1); 343 spin_lock_init(&priv->vram.lock); 344 345 attrs |= DMA_ATTR_NO_KERNEL_MAPPING; 346 attrs |= DMA_ATTR_WRITE_COMBINE; 347 348 /* note that for no-kernel-mapping, the vaddr returned 349 * is bogus, but non-null if allocation succeeded: 350 */ 351 p = dma_alloc_attrs(dev->dev, size, 352 &priv->vram.paddr, GFP_KERNEL, attrs); 353 if (!p) { 354 dev_err(dev->dev, "failed to allocate VRAM\n"); 355 priv->vram.paddr = 0; 356 return -ENOMEM; 357 } 358 359 dev_info(dev->dev, "VRAM: %08x->%08x\n", 360 (uint32_t)priv->vram.paddr, 361 (uint32_t)(priv->vram.paddr + size)); 362 } 363 364 return ret; 365 } 366 367 static int msm_drm_init(struct device *dev, struct drm_driver *drv) 368 { 369 struct platform_device *pdev = to_platform_device(dev); 370 struct drm_device *ddev; 371 struct msm_drm_private *priv; 372 struct msm_kms *kms; 373 int ret; 374 375 ddev = drm_dev_alloc(drv, dev); 376 if (IS_ERR(ddev)) { 377 dev_err(dev, "failed to allocate drm_device\n"); 378 return PTR_ERR(ddev); 379 } 380 381 platform_set_drvdata(pdev, ddev); 382 383 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 384 if (!priv) { 385 drm_dev_unref(ddev); 386 return -ENOMEM; 387 } 388 389 ddev->dev_private = priv; 390 priv->dev = ddev; 391 392 ret = msm_mdss_init(ddev); 393 if (ret) { 394 kfree(priv); 395 drm_dev_unref(ddev); 396 return ret; 397 } 398 399 priv->wq = alloc_ordered_workqueue("msm", 0); 400 priv->atomic_wq = alloc_ordered_workqueue("msm:atomic", 0); 401 init_waitqueue_head(&priv->pending_crtcs_event); 402 403 INIT_LIST_HEAD(&priv->inactive_list); 404 INIT_LIST_HEAD(&priv->vblank_ctrl.event_list); 405 INIT_WORK(&priv->vblank_ctrl.work, vblank_ctrl_worker); 406 spin_lock_init(&priv->vblank_ctrl.lock); 407 408 drm_mode_config_init(ddev); 409 410 /* Bind all our sub-components: */ 411 ret = component_bind_all(dev, ddev); 412 if (ret) { 413 msm_mdss_destroy(ddev); 414 kfree(priv); 415 drm_dev_unref(ddev); 416 return ret; 417 } 418 419 ret = msm_init_vram(ddev); 420 if (ret) 421 goto fail; 422 423 msm_gem_shrinker_init(ddev); 424 425 switch (get_mdp_ver(pdev)) { 426 case 4: 427 kms = mdp4_kms_init(ddev); 428 priv->kms = kms; 429 break; 430 case 5: 431 kms = mdp5_kms_init(ddev); 432 break; 433 default: 434 kms = ERR_PTR(-ENODEV); 435 break; 436 } 437 438 if (IS_ERR(kms)) { 439 /* 440 * NOTE: once we have GPU support, having no kms should not 441 * be considered fatal.. ideally we would still support gpu 442 * and (for example) use dmabuf/prime to share buffers with 443 * imx drm driver on iMX5 444 */ 445 dev_err(dev, "failed to load kms\n"); 446 ret = PTR_ERR(kms); 447 goto fail; 448 } 449 450 if (kms) { 451 ret = kms->funcs->hw_init(kms); 452 if (ret) { 453 dev_err(dev, "kms hw init failed: %d\n", ret); 454 goto fail; 455 } 456 } 457 458 ddev->mode_config.funcs = &mode_config_funcs; 459 460 ret = drm_vblank_init(ddev, priv->num_crtcs); 461 if (ret < 0) { 462 dev_err(dev, "failed to initialize vblank\n"); 463 goto fail; 464 } 465 466 if (kms) { 467 pm_runtime_get_sync(dev); 468 ret = drm_irq_install(ddev, kms->irq); 469 pm_runtime_put_sync(dev); 470 if (ret < 0) { 471 dev_err(dev, "failed to install IRQ handler\n"); 472 goto fail; 473 } 474 } 475 476 ret = drm_dev_register(ddev, 0); 477 if (ret) 478 goto fail; 479 480 drm_mode_config_reset(ddev); 481 482 #ifdef CONFIG_DRM_FBDEV_EMULATION 483 if (fbdev) 484 priv->fbdev = msm_fbdev_init(ddev); 485 #endif 486 487 ret = msm_debugfs_late_init(ddev); 488 if (ret) 489 goto fail; 490 491 drm_kms_helper_poll_init(ddev); 492 493 return 0; 494 495 fail: 496 msm_drm_uninit(dev); 497 return ret; 498 } 499 500 /* 501 * DRM operations: 502 */ 503 504 static void load_gpu(struct drm_device *dev) 505 { 506 static DEFINE_MUTEX(init_lock); 507 struct msm_drm_private *priv = dev->dev_private; 508 509 mutex_lock(&init_lock); 510 511 if (!priv->gpu) 512 priv->gpu = adreno_load_gpu(dev); 513 514 mutex_unlock(&init_lock); 515 } 516 517 static int msm_open(struct drm_device *dev, struct drm_file *file) 518 { 519 struct msm_file_private *ctx; 520 521 /* For now, load gpu on open.. to avoid the requirement of having 522 * firmware in the initrd. 523 */ 524 load_gpu(dev); 525 526 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 527 if (!ctx) 528 return -ENOMEM; 529 530 file->driver_priv = ctx; 531 532 return 0; 533 } 534 535 static void msm_postclose(struct drm_device *dev, struct drm_file *file) 536 { 537 struct msm_drm_private *priv = dev->dev_private; 538 struct msm_file_private *ctx = file->driver_priv; 539 540 mutex_lock(&dev->struct_mutex); 541 if (ctx == priv->lastctx) 542 priv->lastctx = NULL; 543 mutex_unlock(&dev->struct_mutex); 544 545 kfree(ctx); 546 } 547 548 static void msm_lastclose(struct drm_device *dev) 549 { 550 struct msm_drm_private *priv = dev->dev_private; 551 if (priv->fbdev) 552 drm_fb_helper_restore_fbdev_mode_unlocked(priv->fbdev); 553 } 554 555 static irqreturn_t msm_irq(int irq, void *arg) 556 { 557 struct drm_device *dev = arg; 558 struct msm_drm_private *priv = dev->dev_private; 559 struct msm_kms *kms = priv->kms; 560 BUG_ON(!kms); 561 return kms->funcs->irq(kms); 562 } 563 564 static void msm_irq_preinstall(struct drm_device *dev) 565 { 566 struct msm_drm_private *priv = dev->dev_private; 567 struct msm_kms *kms = priv->kms; 568 BUG_ON(!kms); 569 kms->funcs->irq_preinstall(kms); 570 } 571 572 static int msm_irq_postinstall(struct drm_device *dev) 573 { 574 struct msm_drm_private *priv = dev->dev_private; 575 struct msm_kms *kms = priv->kms; 576 BUG_ON(!kms); 577 return kms->funcs->irq_postinstall(kms); 578 } 579 580 static void msm_irq_uninstall(struct drm_device *dev) 581 { 582 struct msm_drm_private *priv = dev->dev_private; 583 struct msm_kms *kms = priv->kms; 584 BUG_ON(!kms); 585 kms->funcs->irq_uninstall(kms); 586 } 587 588 static int msm_enable_vblank(struct drm_device *dev, unsigned int pipe) 589 { 590 struct msm_drm_private *priv = dev->dev_private; 591 struct msm_kms *kms = priv->kms; 592 if (!kms) 593 return -ENXIO; 594 DBG("dev=%p, crtc=%u", dev, pipe); 595 return vblank_ctrl_queue_work(priv, pipe, true); 596 } 597 598 static void msm_disable_vblank(struct drm_device *dev, unsigned int pipe) 599 { 600 struct msm_drm_private *priv = dev->dev_private; 601 struct msm_kms *kms = priv->kms; 602 if (!kms) 603 return; 604 DBG("dev=%p, crtc=%u", dev, pipe); 605 vblank_ctrl_queue_work(priv, pipe, false); 606 } 607 608 /* 609 * DRM ioctls: 610 */ 611 612 static int msm_ioctl_get_param(struct drm_device *dev, void *data, 613 struct drm_file *file) 614 { 615 struct msm_drm_private *priv = dev->dev_private; 616 struct drm_msm_param *args = data; 617 struct msm_gpu *gpu; 618 619 /* for now, we just have 3d pipe.. eventually this would need to 620 * be more clever to dispatch to appropriate gpu module: 621 */ 622 if (args->pipe != MSM_PIPE_3D0) 623 return -EINVAL; 624 625 gpu = priv->gpu; 626 627 if (!gpu) 628 return -ENXIO; 629 630 return gpu->funcs->get_param(gpu, args->param, &args->value); 631 } 632 633 static int msm_ioctl_gem_new(struct drm_device *dev, void *data, 634 struct drm_file *file) 635 { 636 struct drm_msm_gem_new *args = data; 637 638 if (args->flags & ~MSM_BO_FLAGS) { 639 DRM_ERROR("invalid flags: %08x\n", args->flags); 640 return -EINVAL; 641 } 642 643 return msm_gem_new_handle(dev, file, args->size, 644 args->flags, &args->handle); 645 } 646 647 static inline ktime_t to_ktime(struct drm_msm_timespec timeout) 648 { 649 return ktime_set(timeout.tv_sec, timeout.tv_nsec); 650 } 651 652 static int msm_ioctl_gem_cpu_prep(struct drm_device *dev, void *data, 653 struct drm_file *file) 654 { 655 struct drm_msm_gem_cpu_prep *args = data; 656 struct drm_gem_object *obj; 657 ktime_t timeout = to_ktime(args->timeout); 658 int ret; 659 660 if (args->op & ~MSM_PREP_FLAGS) { 661 DRM_ERROR("invalid op: %08x\n", args->op); 662 return -EINVAL; 663 } 664 665 obj = drm_gem_object_lookup(file, args->handle); 666 if (!obj) 667 return -ENOENT; 668 669 ret = msm_gem_cpu_prep(obj, args->op, &timeout); 670 671 drm_gem_object_unreference_unlocked(obj); 672 673 return ret; 674 } 675 676 static int msm_ioctl_gem_cpu_fini(struct drm_device *dev, void *data, 677 struct drm_file *file) 678 { 679 struct drm_msm_gem_cpu_fini *args = data; 680 struct drm_gem_object *obj; 681 int ret; 682 683 obj = drm_gem_object_lookup(file, args->handle); 684 if (!obj) 685 return -ENOENT; 686 687 ret = msm_gem_cpu_fini(obj); 688 689 drm_gem_object_unreference_unlocked(obj); 690 691 return ret; 692 } 693 694 static int msm_ioctl_gem_info_iova(struct drm_device *dev, 695 struct drm_gem_object *obj, uint64_t *iova) 696 { 697 struct msm_drm_private *priv = dev->dev_private; 698 699 if (!priv->gpu) 700 return -EINVAL; 701 702 return msm_gem_get_iova(obj, priv->gpu->aspace, iova); 703 } 704 705 static int msm_ioctl_gem_info(struct drm_device *dev, void *data, 706 struct drm_file *file) 707 { 708 struct drm_msm_gem_info *args = data; 709 struct drm_gem_object *obj; 710 int ret = 0; 711 712 if (args->flags & ~MSM_INFO_FLAGS) 713 return -EINVAL; 714 715 obj = drm_gem_object_lookup(file, args->handle); 716 if (!obj) 717 return -ENOENT; 718 719 if (args->flags & MSM_INFO_IOVA) { 720 uint64_t iova; 721 722 ret = msm_ioctl_gem_info_iova(dev, obj, &iova); 723 if (!ret) 724 args->offset = iova; 725 } else { 726 args->offset = msm_gem_mmap_offset(obj); 727 } 728 729 drm_gem_object_unreference_unlocked(obj); 730 731 return ret; 732 } 733 734 static int msm_ioctl_wait_fence(struct drm_device *dev, void *data, 735 struct drm_file *file) 736 { 737 struct msm_drm_private *priv = dev->dev_private; 738 struct drm_msm_wait_fence *args = data; 739 ktime_t timeout = to_ktime(args->timeout); 740 741 if (args->pad) { 742 DRM_ERROR("invalid pad: %08x\n", args->pad); 743 return -EINVAL; 744 } 745 746 if (!priv->gpu) 747 return 0; 748 749 return msm_wait_fence(priv->gpu->fctx, args->fence, &timeout, true); 750 } 751 752 static int msm_ioctl_gem_madvise(struct drm_device *dev, void *data, 753 struct drm_file *file) 754 { 755 struct drm_msm_gem_madvise *args = data; 756 struct drm_gem_object *obj; 757 int ret; 758 759 switch (args->madv) { 760 case MSM_MADV_DONTNEED: 761 case MSM_MADV_WILLNEED: 762 break; 763 default: 764 return -EINVAL; 765 } 766 767 ret = mutex_lock_interruptible(&dev->struct_mutex); 768 if (ret) 769 return ret; 770 771 obj = drm_gem_object_lookup(file, args->handle); 772 if (!obj) { 773 ret = -ENOENT; 774 goto unlock; 775 } 776 777 ret = msm_gem_madvise(obj, args->madv); 778 if (ret >= 0) { 779 args->retained = ret; 780 ret = 0; 781 } 782 783 drm_gem_object_unreference(obj); 784 785 unlock: 786 mutex_unlock(&dev->struct_mutex); 787 return ret; 788 } 789 790 static const struct drm_ioctl_desc msm_ioctls[] = { 791 DRM_IOCTL_DEF_DRV(MSM_GET_PARAM, msm_ioctl_get_param, DRM_AUTH|DRM_RENDER_ALLOW), 792 DRM_IOCTL_DEF_DRV(MSM_GEM_NEW, msm_ioctl_gem_new, DRM_AUTH|DRM_RENDER_ALLOW), 793 DRM_IOCTL_DEF_DRV(MSM_GEM_INFO, msm_ioctl_gem_info, DRM_AUTH|DRM_RENDER_ALLOW), 794 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_PREP, msm_ioctl_gem_cpu_prep, DRM_AUTH|DRM_RENDER_ALLOW), 795 DRM_IOCTL_DEF_DRV(MSM_GEM_CPU_FINI, msm_ioctl_gem_cpu_fini, DRM_AUTH|DRM_RENDER_ALLOW), 796 DRM_IOCTL_DEF_DRV(MSM_GEM_SUBMIT, msm_ioctl_gem_submit, DRM_AUTH|DRM_RENDER_ALLOW), 797 DRM_IOCTL_DEF_DRV(MSM_WAIT_FENCE, msm_ioctl_wait_fence, DRM_AUTH|DRM_RENDER_ALLOW), 798 DRM_IOCTL_DEF_DRV(MSM_GEM_MADVISE, msm_ioctl_gem_madvise, DRM_AUTH|DRM_RENDER_ALLOW), 799 }; 800 801 static const struct vm_operations_struct vm_ops = { 802 .fault = msm_gem_fault, 803 .open = drm_gem_vm_open, 804 .close = drm_gem_vm_close, 805 }; 806 807 static const struct file_operations fops = { 808 .owner = THIS_MODULE, 809 .open = drm_open, 810 .release = drm_release, 811 .unlocked_ioctl = drm_ioctl, 812 .compat_ioctl = drm_compat_ioctl, 813 .poll = drm_poll, 814 .read = drm_read, 815 .llseek = no_llseek, 816 .mmap = msm_gem_mmap, 817 }; 818 819 static struct drm_driver msm_driver = { 820 .driver_features = DRIVER_HAVE_IRQ | 821 DRIVER_GEM | 822 DRIVER_PRIME | 823 DRIVER_RENDER | 824 DRIVER_ATOMIC | 825 DRIVER_MODESET, 826 .open = msm_open, 827 .postclose = msm_postclose, 828 .lastclose = msm_lastclose, 829 .irq_handler = msm_irq, 830 .irq_preinstall = msm_irq_preinstall, 831 .irq_postinstall = msm_irq_postinstall, 832 .irq_uninstall = msm_irq_uninstall, 833 .enable_vblank = msm_enable_vblank, 834 .disable_vblank = msm_disable_vblank, 835 .gem_free_object = msm_gem_free_object, 836 .gem_vm_ops = &vm_ops, 837 .dumb_create = msm_gem_dumb_create, 838 .dumb_map_offset = msm_gem_dumb_map_offset, 839 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, 840 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, 841 .gem_prime_export = drm_gem_prime_export, 842 .gem_prime_import = drm_gem_prime_import, 843 .gem_prime_res_obj = msm_gem_prime_res_obj, 844 .gem_prime_pin = msm_gem_prime_pin, 845 .gem_prime_unpin = msm_gem_prime_unpin, 846 .gem_prime_get_sg_table = msm_gem_prime_get_sg_table, 847 .gem_prime_import_sg_table = msm_gem_prime_import_sg_table, 848 .gem_prime_vmap = msm_gem_prime_vmap, 849 .gem_prime_vunmap = msm_gem_prime_vunmap, 850 .gem_prime_mmap = msm_gem_prime_mmap, 851 #ifdef CONFIG_DEBUG_FS 852 .debugfs_init = msm_debugfs_init, 853 #endif 854 .ioctls = msm_ioctls, 855 .num_ioctls = ARRAY_SIZE(msm_ioctls), 856 .fops = &fops, 857 .name = "msm", 858 .desc = "MSM Snapdragon DRM", 859 .date = "20130625", 860 .major = MSM_VERSION_MAJOR, 861 .minor = MSM_VERSION_MINOR, 862 .patchlevel = MSM_VERSION_PATCHLEVEL, 863 }; 864 865 #ifdef CONFIG_PM_SLEEP 866 static int msm_pm_suspend(struct device *dev) 867 { 868 struct drm_device *ddev = dev_get_drvdata(dev); 869 870 drm_kms_helper_poll_disable(ddev); 871 872 return 0; 873 } 874 875 static int msm_pm_resume(struct device *dev) 876 { 877 struct drm_device *ddev = dev_get_drvdata(dev); 878 879 drm_kms_helper_poll_enable(ddev); 880 881 return 0; 882 } 883 #endif 884 885 #ifdef CONFIG_PM 886 static int msm_runtime_suspend(struct device *dev) 887 { 888 struct drm_device *ddev = dev_get_drvdata(dev); 889 struct msm_drm_private *priv = ddev->dev_private; 890 891 DBG(""); 892 893 if (priv->mdss) 894 return msm_mdss_disable(priv->mdss); 895 896 return 0; 897 } 898 899 static int msm_runtime_resume(struct device *dev) 900 { 901 struct drm_device *ddev = dev_get_drvdata(dev); 902 struct msm_drm_private *priv = ddev->dev_private; 903 904 DBG(""); 905 906 if (priv->mdss) 907 return msm_mdss_enable(priv->mdss); 908 909 return 0; 910 } 911 #endif 912 913 static const struct dev_pm_ops msm_pm_ops = { 914 SET_SYSTEM_SLEEP_PM_OPS(msm_pm_suspend, msm_pm_resume) 915 SET_RUNTIME_PM_OPS(msm_runtime_suspend, msm_runtime_resume, NULL) 916 }; 917 918 /* 919 * Componentized driver support: 920 */ 921 922 /* 923 * NOTE: duplication of the same code as exynos or imx (or probably any other). 924 * so probably some room for some helpers 925 */ 926 static int compare_of(struct device *dev, void *data) 927 { 928 return dev->of_node == data; 929 } 930 931 /* 932 * Identify what components need to be added by parsing what remote-endpoints 933 * our MDP output ports are connected to. In the case of LVDS on MDP4, there 934 * is no external component that we need to add since LVDS is within MDP4 935 * itself. 936 */ 937 static int add_components_mdp(struct device *mdp_dev, 938 struct component_match **matchptr) 939 { 940 struct device_node *np = mdp_dev->of_node; 941 struct device_node *ep_node; 942 struct device *master_dev; 943 944 /* 945 * on MDP4 based platforms, the MDP platform device is the component 946 * master that adds other display interface components to itself. 947 * 948 * on MDP5 based platforms, the MDSS platform device is the component 949 * master that adds MDP5 and other display interface components to 950 * itself. 951 */ 952 if (of_device_is_compatible(np, "qcom,mdp4")) 953 master_dev = mdp_dev; 954 else 955 master_dev = mdp_dev->parent; 956 957 for_each_endpoint_of_node(np, ep_node) { 958 struct device_node *intf; 959 struct of_endpoint ep; 960 int ret; 961 962 ret = of_graph_parse_endpoint(ep_node, &ep); 963 if (ret) { 964 dev_err(mdp_dev, "unable to parse port endpoint\n"); 965 of_node_put(ep_node); 966 return ret; 967 } 968 969 /* 970 * The LCDC/LVDS port on MDP4 is a speacial case where the 971 * remote-endpoint isn't a component that we need to add 972 */ 973 if (of_device_is_compatible(np, "qcom,mdp4") && 974 ep.port == 0) 975 continue; 976 977 /* 978 * It's okay if some of the ports don't have a remote endpoint 979 * specified. It just means that the port isn't connected to 980 * any external interface. 981 */ 982 intf = of_graph_get_remote_port_parent(ep_node); 983 if (!intf) 984 continue; 985 986 drm_of_component_match_add(master_dev, matchptr, compare_of, 987 intf); 988 of_node_put(intf); 989 } 990 991 return 0; 992 } 993 994 static int compare_name_mdp(struct device *dev, void *data) 995 { 996 return (strstr(dev_name(dev), "mdp") != NULL); 997 } 998 999 static int add_display_components(struct device *dev, 1000 struct component_match **matchptr) 1001 { 1002 struct device *mdp_dev; 1003 int ret; 1004 1005 /* 1006 * MDP5 based devices don't have a flat hierarchy. There is a top level 1007 * parent: MDSS, and children: MDP5, DSI, HDMI, eDP etc. Populate the 1008 * children devices, find the MDP5 node, and then add the interfaces 1009 * to our components list. 1010 */ 1011 if (of_device_is_compatible(dev->of_node, "qcom,mdss")) { 1012 ret = of_platform_populate(dev->of_node, NULL, NULL, dev); 1013 if (ret) { 1014 dev_err(dev, "failed to populate children devices\n"); 1015 return ret; 1016 } 1017 1018 mdp_dev = device_find_child(dev, NULL, compare_name_mdp); 1019 if (!mdp_dev) { 1020 dev_err(dev, "failed to find MDSS MDP node\n"); 1021 of_platform_depopulate(dev); 1022 return -ENODEV; 1023 } 1024 1025 put_device(mdp_dev); 1026 1027 /* add the MDP component itself */ 1028 drm_of_component_match_add(dev, matchptr, compare_of, 1029 mdp_dev->of_node); 1030 } else { 1031 /* MDP4 */ 1032 mdp_dev = dev; 1033 } 1034 1035 ret = add_components_mdp(mdp_dev, matchptr); 1036 if (ret) 1037 of_platform_depopulate(dev); 1038 1039 return ret; 1040 } 1041 1042 /* 1043 * We don't know what's the best binding to link the gpu with the drm device. 1044 * Fow now, we just hunt for all the possible gpus that we support, and add them 1045 * as components. 1046 */ 1047 static const struct of_device_id msm_gpu_match[] = { 1048 { .compatible = "qcom,adreno" }, 1049 { .compatible = "qcom,adreno-3xx" }, 1050 { .compatible = "qcom,kgsl-3d0" }, 1051 { }, 1052 }; 1053 1054 static int add_gpu_components(struct device *dev, 1055 struct component_match **matchptr) 1056 { 1057 struct device_node *np; 1058 1059 np = of_find_matching_node(NULL, msm_gpu_match); 1060 if (!np) 1061 return 0; 1062 1063 drm_of_component_match_add(dev, matchptr, compare_of, np); 1064 1065 of_node_put(np); 1066 1067 return 0; 1068 } 1069 1070 static int msm_drm_bind(struct device *dev) 1071 { 1072 return msm_drm_init(dev, &msm_driver); 1073 } 1074 1075 static void msm_drm_unbind(struct device *dev) 1076 { 1077 msm_drm_uninit(dev); 1078 } 1079 1080 static const struct component_master_ops msm_drm_ops = { 1081 .bind = msm_drm_bind, 1082 .unbind = msm_drm_unbind, 1083 }; 1084 1085 /* 1086 * Platform driver: 1087 */ 1088 1089 static int msm_pdev_probe(struct platform_device *pdev) 1090 { 1091 struct component_match *match = NULL; 1092 int ret; 1093 1094 ret = add_display_components(&pdev->dev, &match); 1095 if (ret) 1096 return ret; 1097 1098 ret = add_gpu_components(&pdev->dev, &match); 1099 if (ret) 1100 return ret; 1101 1102 /* on all devices that I am aware of, iommu's which can map 1103 * any address the cpu can see are used: 1104 */ 1105 ret = dma_set_mask_and_coherent(&pdev->dev, ~0); 1106 if (ret) 1107 return ret; 1108 1109 return component_master_add_with_match(&pdev->dev, &msm_drm_ops, match); 1110 } 1111 1112 static int msm_pdev_remove(struct platform_device *pdev) 1113 { 1114 component_master_del(&pdev->dev, &msm_drm_ops); 1115 of_platform_depopulate(&pdev->dev); 1116 1117 return 0; 1118 } 1119 1120 static const struct of_device_id dt_match[] = { 1121 { .compatible = "qcom,mdp4", .data = (void *)4 }, /* MDP4 */ 1122 { .compatible = "qcom,mdss", .data = (void *)5 }, /* MDP5 MDSS */ 1123 {} 1124 }; 1125 MODULE_DEVICE_TABLE(of, dt_match); 1126 1127 static struct platform_driver msm_platform_driver = { 1128 .probe = msm_pdev_probe, 1129 .remove = msm_pdev_remove, 1130 .driver = { 1131 .name = "msm", 1132 .of_match_table = dt_match, 1133 .pm = &msm_pm_ops, 1134 }, 1135 }; 1136 1137 static int __init msm_drm_register(void) 1138 { 1139 if (!modeset) 1140 return -EINVAL; 1141 1142 DBG("init"); 1143 msm_mdp_register(); 1144 msm_dsi_register(); 1145 msm_edp_register(); 1146 msm_hdmi_register(); 1147 adreno_register(); 1148 return platform_driver_register(&msm_platform_driver); 1149 } 1150 1151 static void __exit msm_drm_unregister(void) 1152 { 1153 DBG("fini"); 1154 platform_driver_unregister(&msm_platform_driver); 1155 msm_hdmi_unregister(); 1156 adreno_unregister(); 1157 msm_edp_unregister(); 1158 msm_dsi_unregister(); 1159 msm_mdp_unregister(); 1160 } 1161 1162 module_init(msm_drm_register); 1163 module_exit(msm_drm_unregister); 1164 1165 MODULE_AUTHOR("Rob Clark <robdclark@gmail.com"); 1166 MODULE_DESCRIPTION("MSM DRM Driver"); 1167 MODULE_LICENSE("GPL"); 1168