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