linux/drivers/gpu/drm/msm/msm_gpu.h
<<
>>
Prefs
   1/* SPDX-License-Identifier: GPL-2.0-only */
   2/*
   3 * Copyright (C) 2013 Red Hat
   4 * Author: Rob Clark <robdclark@gmail.com>
   5 */
   6
   7#ifndef __MSM_GPU_H__
   8#define __MSM_GPU_H__
   9
  10#include <linux/adreno-smmu-priv.h>
  11#include <linux/clk.h>
  12#include <linux/devfreq.h>
  13#include <linux/interconnect.h>
  14#include <linux/pm_opp.h>
  15#include <linux/regulator/consumer.h>
  16
  17#include "msm_drv.h"
  18#include "msm_fence.h"
  19#include "msm_ringbuffer.h"
  20#include "msm_gem.h"
  21
  22struct msm_gem_submit;
  23struct msm_gpu_perfcntr;
  24struct msm_gpu_state;
  25struct msm_file_private;
  26
  27struct msm_gpu_config {
  28        const char *ioname;
  29        unsigned int nr_rings;
  30};
  31
  32/* So far, with hardware that I've seen to date, we can have:
  33 *  + zero, one, or two z180 2d cores
  34 *  + a3xx or a2xx 3d core, which share a common CP (the firmware
  35 *    for the CP seems to implement some different PM4 packet types
  36 *    but the basics of cmdstream submission are the same)
  37 *
  38 * Which means that the eventual complete "class" hierarchy, once
  39 * support for all past and present hw is in place, becomes:
  40 *  + msm_gpu
  41 *    + adreno_gpu
  42 *      + a3xx_gpu
  43 *      + a2xx_gpu
  44 *    + z180_gpu
  45 */
  46struct msm_gpu_funcs {
  47        int (*get_param)(struct msm_gpu *gpu, struct msm_file_private *ctx,
  48                         uint32_t param, uint64_t *value, uint32_t *len);
  49        int (*set_param)(struct msm_gpu *gpu, struct msm_file_private *ctx,
  50                         uint32_t param, uint64_t value, uint32_t len);
  51        int (*hw_init)(struct msm_gpu *gpu);
  52        int (*pm_suspend)(struct msm_gpu *gpu);
  53        int (*pm_resume)(struct msm_gpu *gpu);
  54        void (*submit)(struct msm_gpu *gpu, struct msm_gem_submit *submit);
  55        void (*flush)(struct msm_gpu *gpu, struct msm_ringbuffer *ring);
  56        irqreturn_t (*irq)(struct msm_gpu *irq);
  57        struct msm_ringbuffer *(*active_ring)(struct msm_gpu *gpu);
  58        void (*recover)(struct msm_gpu *gpu);
  59        void (*destroy)(struct msm_gpu *gpu);
  60#if defined(CONFIG_DEBUG_FS) || defined(CONFIG_DEV_COREDUMP)
  61        /* show GPU status in debugfs: */
  62        void (*show)(struct msm_gpu *gpu, struct msm_gpu_state *state,
  63                        struct drm_printer *p);
  64        /* for generation specific debugfs: */
  65        void (*debugfs_init)(struct msm_gpu *gpu, struct drm_minor *minor);
  66#endif
  67        u64 (*gpu_busy)(struct msm_gpu *gpu, unsigned long *out_sample_rate);
  68        struct msm_gpu_state *(*gpu_state_get)(struct msm_gpu *gpu);
  69        int (*gpu_state_put)(struct msm_gpu_state *state);
  70        unsigned long (*gpu_get_freq)(struct msm_gpu *gpu);
  71        void (*gpu_set_freq)(struct msm_gpu *gpu, struct dev_pm_opp *opp);
  72        struct msm_gem_address_space *(*create_address_space)
  73                (struct msm_gpu *gpu, struct platform_device *pdev);
  74        struct msm_gem_address_space *(*create_private_address_space)
  75                (struct msm_gpu *gpu);
  76        uint32_t (*get_rptr)(struct msm_gpu *gpu, struct msm_ringbuffer *ring);
  77};
  78
  79/* Additional state for iommu faults: */
  80struct msm_gpu_fault_info {
  81        u64 ttbr0;
  82        unsigned long iova;
  83        int flags;
  84        const char *type;
  85        const char *block;
  86};
  87
  88/**
  89 * struct msm_gpu_devfreq - devfreq related state
  90 */
  91struct msm_gpu_devfreq {
  92        /** devfreq: devfreq instance */
  93        struct devfreq *devfreq;
  94
  95        /**
  96         * idle_constraint:
  97         *
  98         * A PM QoS constraint to limit max freq while the GPU is idle.
  99         */
 100        struct dev_pm_qos_request idle_freq;
 101
 102        /**
 103         * boost_constraint:
 104         *
 105         * A PM QoS constraint to boost min freq for a period of time
 106         * until the boost expires.
 107         */
 108        struct dev_pm_qos_request boost_freq;
 109
 110        /**
 111         * busy_cycles: Last busy counter value, for calculating elapsed busy
 112         * cycles since last sampling period.
 113         */
 114        u64 busy_cycles;
 115
 116        /** time: Time of last sampling period. */
 117        ktime_t time;
 118
 119        /** idle_time: Time of last transition to idle: */
 120        ktime_t idle_time;
 121
 122        struct devfreq_dev_status average_status;
 123
 124        /**
 125         * idle_work:
 126         *
 127         * Used to delay clamping to idle freq on active->idle transition.
 128         */
 129        struct msm_hrtimer_work idle_work;
 130
 131        /**
 132         * boost_work:
 133         *
 134         * Used to reset the boost_constraint after the boost period has
 135         * elapsed
 136         */
 137        struct msm_hrtimer_work boost_work;
 138};
 139
 140struct msm_gpu {
 141        const char *name;
 142        struct drm_device *dev;
 143        struct platform_device *pdev;
 144        const struct msm_gpu_funcs *funcs;
 145
 146        struct adreno_smmu_priv adreno_smmu;
 147
 148        /* performance counters (hw & sw): */
 149        spinlock_t perf_lock;
 150        bool perfcntr_active;
 151        struct {
 152                bool active;
 153                ktime_t time;
 154        } last_sample;
 155        uint32_t totaltime, activetime;    /* sw counters */
 156        uint32_t last_cntrs[5];            /* hw counters */
 157        const struct msm_gpu_perfcntr *perfcntrs;
 158        uint32_t num_perfcntrs;
 159
 160        struct msm_ringbuffer *rb[MSM_GPU_MAX_RINGS];
 161        int nr_rings;
 162
 163        /**
 164         * sysprof_active:
 165         *
 166         * The count of contexts that have enabled system profiling.
 167         */
 168        refcount_t sysprof_active;
 169
 170        /**
 171         * cur_ctx_seqno:
 172         *
 173         * The ctx->seqno value of the last context to submit rendering,
 174         * and the one with current pgtables installed (for generations
 175         * that support per-context pgtables).  Tracked by seqno rather
 176         * than pointer value to avoid dangling pointers, and cases where
 177         * a ctx can be freed and a new one created with the same address.
 178         */
 179        int cur_ctx_seqno;
 180
 181        /*
 182         * List of GEM active objects on this gpu.  Protected by
 183         * msm_drm_private::mm_lock
 184         */
 185        struct list_head active_list;
 186
 187        /**
 188         * lock:
 189         *
 190         * General lock for serializing all the gpu things.
 191         *
 192         * TODO move to per-ring locking where feasible (ie. submit/retire
 193         * path, etc)
 194         */
 195        struct mutex lock;
 196
 197        /**
 198         * active_submits:
 199         *
 200         * The number of submitted but not yet retired submits, used to
 201         * determine transitions between active and idle.
 202         *
 203         * Protected by active_lock
 204         */
 205        int active_submits;
 206
 207        /** lock: protects active_submits and idle/active transitions */
 208        struct mutex active_lock;
 209
 210        /* does gpu need hw_init? */
 211        bool needs_hw_init;
 212
 213        /**
 214         * global_faults: number of GPU hangs not attributed to a particular
 215         * address space
 216         */
 217        int global_faults;
 218
 219        void __iomem *mmio;
 220        int irq;
 221
 222        struct msm_gem_address_space *aspace;
 223
 224        /* Power Control: */
 225        struct regulator *gpu_reg, *gpu_cx;
 226        struct clk_bulk_data *grp_clks;
 227        int nr_clocks;
 228        struct clk *ebi1_clk, *core_clk, *rbbmtimer_clk;
 229        uint32_t fast_rate;
 230
 231        /* Hang and Inactivity Detection:
 232         */
 233#define DRM_MSM_INACTIVE_PERIOD   66 /* in ms (roughly four frames) */
 234
 235#define DRM_MSM_HANGCHECK_DEFAULT_PERIOD 500 /* in ms */
 236        struct timer_list hangcheck_timer;
 237
 238        /* Fault info for most recent iova fault: */
 239        struct msm_gpu_fault_info fault_info;
 240
 241        /* work for handling GPU ioval faults: */
 242        struct kthread_work fault_work;
 243
 244        /* work for handling GPU recovery: */
 245        struct kthread_work recover_work;
 246
 247        /** retire_event: notified when submits are retired: */
 248        wait_queue_head_t retire_event;
 249
 250        /* work for handling active-list retiring: */
 251        struct kthread_work retire_work;
 252
 253        /* worker for retire/recover: */
 254        struct kthread_worker *worker;
 255
 256        struct drm_gem_object *memptrs_bo;
 257
 258        struct msm_gpu_devfreq devfreq;
 259
 260        uint32_t suspend_count;
 261
 262        struct msm_gpu_state *crashstate;
 263
 264        /* Enable clamping to idle freq when inactive: */
 265        bool clamp_to_idle;
 266
 267        /* True if the hardware supports expanded apriv (a650 and newer) */
 268        bool hw_apriv;
 269
 270        struct thermal_cooling_device *cooling;
 271};
 272
 273static inline struct msm_gpu *dev_to_gpu(struct device *dev)
 274{
 275        struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(dev);
 276        return container_of(adreno_smmu, struct msm_gpu, adreno_smmu);
 277}
 278
 279/* It turns out that all targets use the same ringbuffer size */
 280#define MSM_GPU_RINGBUFFER_SZ SZ_32K
 281#define MSM_GPU_RINGBUFFER_BLKSIZE 32
 282
 283#define MSM_GPU_RB_CNTL_DEFAULT \
 284                (AXXX_CP_RB_CNTL_BUFSZ(ilog2(MSM_GPU_RINGBUFFER_SZ / 8)) | \
 285                AXXX_CP_RB_CNTL_BLKSZ(ilog2(MSM_GPU_RINGBUFFER_BLKSIZE / 8)))
 286
 287static inline bool msm_gpu_active(struct msm_gpu *gpu)
 288{
 289        int i;
 290
 291        for (i = 0; i < gpu->nr_rings; i++) {
 292                struct msm_ringbuffer *ring = gpu->rb[i];
 293
 294                if (fence_after(ring->fctx->last_fence, ring->memptrs->fence))
 295                        return true;
 296        }
 297
 298        return false;
 299}
 300
 301/* Perf-Counters:
 302 * The select_reg and select_val are just there for the benefit of the child
 303 * class that actually enables the perf counter..  but msm_gpu base class
 304 * will handle sampling/displaying the counters.
 305 */
 306
 307struct msm_gpu_perfcntr {
 308        uint32_t select_reg;
 309        uint32_t sample_reg;
 310        uint32_t select_val;
 311        const char *name;
 312};
 313
 314/*
 315 * The number of priority levels provided by drm gpu scheduler.  The
 316 * DRM_SCHED_PRIORITY_KERNEL priority level is treated specially in some
 317 * cases, so we don't use it (no need for kernel generated jobs).
 318 */
 319#define NR_SCHED_PRIORITIES (1 + DRM_SCHED_PRIORITY_HIGH - DRM_SCHED_PRIORITY_MIN)
 320
 321/**
 322 * struct msm_file_private - per-drm_file context
 323 *
 324 * @queuelock:    synchronizes access to submitqueues list
 325 * @submitqueues: list of &msm_gpu_submitqueue created by userspace
 326 * @queueid:      counter incremented each time a submitqueue is created,
 327 *                used to assign &msm_gpu_submitqueue.id
 328 * @aspace:       the per-process GPU address-space
 329 * @ref:          reference count
 330 * @seqno:        unique per process seqno
 331 */
 332struct msm_file_private {
 333        rwlock_t queuelock;
 334        struct list_head submitqueues;
 335        int queueid;
 336        struct msm_gem_address_space *aspace;
 337        struct kref ref;
 338        int seqno;
 339
 340        /**
 341         * sysprof:
 342         *
 343         * The value of MSM_PARAM_SYSPROF set by userspace.  This is
 344         * intended to be used by system profiling tools like Mesa's
 345         * pps-producer (perfetto), and restricted to CAP_SYS_ADMIN.
 346         *
 347         * Setting a value of 1 will preserve performance counters across
 348         * context switches.  Setting a value of 2 will in addition
 349         * suppress suspend.  (Performance counters lose state across
 350         * power collapse, which is undesirable for profiling in some
 351         * cases.)
 352         *
 353         * The value automatically reverts to zero when the drm device
 354         * file is closed.
 355         */
 356        int sysprof;
 357
 358        /** comm: Overridden task comm, see MSM_PARAM_COMM */
 359        char *comm;
 360
 361        /** cmdline: Overridden task cmdline, see MSM_PARAM_CMDLINE */
 362        char *cmdline;
 363
 364        /**
 365         * entities:
 366         *
 367         * Table of per-priority-level sched entities used by submitqueues
 368         * associated with this &drm_file.  Because some userspace apps
 369         * make assumptions about rendering from multiple gl contexts
 370         * (of the same priority) within the process happening in FIFO
 371         * order without requiring any fencing beyond MakeCurrent(), we
 372         * create at most one &drm_sched_entity per-process per-priority-
 373         * level.
 374         */
 375        struct drm_sched_entity *entities[NR_SCHED_PRIORITIES * MSM_GPU_MAX_RINGS];
 376};
 377
 378/**
 379 * msm_gpu_convert_priority - Map userspace priority to ring # and sched priority
 380 *
 381 * @gpu:        the gpu instance
 382 * @prio:       the userspace priority level
 383 * @ring_nr:    [out] the ringbuffer the userspace priority maps to
 384 * @sched_prio: [out] the gpu scheduler priority level which the userspace
 385 *              priority maps to
 386 *
 387 * With drm/scheduler providing it's own level of prioritization, our total
 388 * number of available priority levels is (nr_rings * NR_SCHED_PRIORITIES).
 389 * Each ring is associated with it's own scheduler instance.  However, our
 390 * UABI is that lower numerical values are higher priority.  So mapping the
 391 * single userspace priority level into ring_nr and sched_prio takes some
 392 * care.  The userspace provided priority (when a submitqueue is created)
 393 * is mapped to ring nr and scheduler priority as such:
 394 *
 395 *   ring_nr    = userspace_prio / NR_SCHED_PRIORITIES
 396 *   sched_prio = NR_SCHED_PRIORITIES -
 397 *                (userspace_prio % NR_SCHED_PRIORITIES) - 1
 398 *
 399 * This allows generations without preemption (nr_rings==1) to have some
 400 * amount of prioritization, and provides more priority levels for gens
 401 * that do have preemption.
 402 */
 403static inline int msm_gpu_convert_priority(struct msm_gpu *gpu, int prio,
 404                unsigned *ring_nr, enum drm_sched_priority *sched_prio)
 405{
 406        unsigned rn, sp;
 407
 408        rn = div_u64_rem(prio, NR_SCHED_PRIORITIES, &sp);
 409
 410        /* invert sched priority to map to higher-numeric-is-higher-
 411         * priority convention
 412         */
 413        sp = NR_SCHED_PRIORITIES - sp - 1;
 414
 415        if (rn >= gpu->nr_rings)
 416                return -EINVAL;
 417
 418        *ring_nr = rn;
 419        *sched_prio = sp;
 420
 421        return 0;
 422}
 423
 424/**
 425 * struct msm_gpu_submitqueues - Userspace created context.
 426 *
 427 * A submitqueue is associated with a gl context or vk queue (or equiv)
 428 * in userspace.
 429 *
 430 * @id:        userspace id for the submitqueue, unique within the drm_file
 431 * @flags:     userspace flags for the submitqueue, specified at creation
 432 *             (currently unusued)
 433 * @ring_nr:   the ringbuffer used by this submitqueue, which is determined
 434 *             by the submitqueue's priority
 435 * @faults:    the number of GPU hangs associated with this submitqueue
 436 * @last_fence: the sequence number of the last allocated fence (for error
 437 *             checking)
 438 * @ctx:       the per-drm_file context associated with the submitqueue (ie.
 439 *             which set of pgtables do submits jobs associated with the
 440 *             submitqueue use)
 441 * @node:      node in the context's list of submitqueues
 442 * @fence_idr: maps fence-id to dma_fence for userspace visible fence
 443 *             seqno, protected by submitqueue lock
 444 * @lock:      submitqueue lock
 445 * @ref:       reference count
 446 * @entity:    the submit job-queue
 447 */
 448struct msm_gpu_submitqueue {
 449        int id;
 450        u32 flags;
 451        u32 ring_nr;
 452        int faults;
 453        uint32_t last_fence;
 454        struct msm_file_private *ctx;
 455        struct list_head node;
 456        struct idr fence_idr;
 457        struct mutex lock;
 458        struct kref ref;
 459        struct drm_sched_entity *entity;
 460};
 461
 462struct msm_gpu_state_bo {
 463        u64 iova;
 464        size_t size;
 465        void *data;
 466        bool encoded;
 467};
 468
 469struct msm_gpu_state {
 470        struct kref ref;
 471        struct timespec64 time;
 472
 473        struct {
 474                u64 iova;
 475                u32 fence;
 476                u32 seqno;
 477                u32 rptr;
 478                u32 wptr;
 479                void *data;
 480                int data_size;
 481                bool encoded;
 482        } ring[MSM_GPU_MAX_RINGS];
 483
 484        int nr_registers;
 485        u32 *registers;
 486
 487        u32 rbbm_status;
 488
 489        char *comm;
 490        char *cmd;
 491
 492        struct msm_gpu_fault_info fault_info;
 493
 494        int nr_bos;
 495        struct msm_gpu_state_bo *bos;
 496};
 497
 498static inline void gpu_write(struct msm_gpu *gpu, u32 reg, u32 data)
 499{
 500        msm_writel(data, gpu->mmio + (reg << 2));
 501}
 502
 503static inline u32 gpu_read(struct msm_gpu *gpu, u32 reg)
 504{
 505        return msm_readl(gpu->mmio + (reg << 2));
 506}
 507
 508static inline void gpu_rmw(struct msm_gpu *gpu, u32 reg, u32 mask, u32 or)
 509{
 510        msm_rmw(gpu->mmio + (reg << 2), mask, or);
 511}
 512
 513static inline u64 gpu_read64(struct msm_gpu *gpu, u32 lo, u32 hi)
 514{
 515        u64 val;
 516
 517        /*
 518         * Why not a readq here? Two reasons: 1) many of the LO registers are
 519         * not quad word aligned and 2) the GPU hardware designers have a bit
 520         * of a history of putting registers where they fit, especially in
 521         * spins. The longer a GPU family goes the higher the chance that
 522         * we'll get burned.  We could do a series of validity checks if we
 523         * wanted to, but really is a readq() that much better? Nah.
 524         */
 525
 526        /*
 527         * For some lo/hi registers (like perfcounters), the hi value is latched
 528         * when the lo is read, so make sure to read the lo first to trigger
 529         * that
 530         */
 531        val = (u64) msm_readl(gpu->mmio + (lo << 2));
 532        val |= ((u64) msm_readl(gpu->mmio + (hi << 2)) << 32);
 533
 534        return val;
 535}
 536
 537static inline void gpu_write64(struct msm_gpu *gpu, u32 lo, u32 hi, u64 val)
 538{
 539        /* Why not a writeq here? Read the screed above */
 540        msm_writel(lower_32_bits(val), gpu->mmio + (lo << 2));
 541        msm_writel(upper_32_bits(val), gpu->mmio + (hi << 2));
 542}
 543
 544int msm_gpu_pm_suspend(struct msm_gpu *gpu);
 545int msm_gpu_pm_resume(struct msm_gpu *gpu);
 546
 547int msm_submitqueue_init(struct drm_device *drm, struct msm_file_private *ctx);
 548struct msm_gpu_submitqueue *msm_submitqueue_get(struct msm_file_private *ctx,
 549                u32 id);
 550int msm_submitqueue_create(struct drm_device *drm,
 551                struct msm_file_private *ctx,
 552                u32 prio, u32 flags, u32 *id);
 553int msm_submitqueue_query(struct drm_device *drm, struct msm_file_private *ctx,
 554                struct drm_msm_submitqueue_query *args);
 555int msm_submitqueue_remove(struct msm_file_private *ctx, u32 id);
 556void msm_submitqueue_close(struct msm_file_private *ctx);
 557
 558void msm_submitqueue_destroy(struct kref *kref);
 559
 560int msm_file_private_set_sysprof(struct msm_file_private *ctx,
 561                                 struct msm_gpu *gpu, int sysprof);
 562void __msm_file_private_destroy(struct kref *kref);
 563
 564static inline void msm_file_private_put(struct msm_file_private *ctx)
 565{
 566        kref_put(&ctx->ref, __msm_file_private_destroy);
 567}
 568
 569static inline struct msm_file_private *msm_file_private_get(
 570        struct msm_file_private *ctx)
 571{
 572        kref_get(&ctx->ref);
 573        return ctx;
 574}
 575
 576void msm_devfreq_init(struct msm_gpu *gpu);
 577void msm_devfreq_cleanup(struct msm_gpu *gpu);
 578void msm_devfreq_resume(struct msm_gpu *gpu);
 579void msm_devfreq_suspend(struct msm_gpu *gpu);
 580void msm_devfreq_boost(struct msm_gpu *gpu, unsigned factor);
 581void msm_devfreq_active(struct msm_gpu *gpu);
 582void msm_devfreq_idle(struct msm_gpu *gpu);
 583
 584int msm_gpu_hw_init(struct msm_gpu *gpu);
 585
 586void msm_gpu_perfcntr_start(struct msm_gpu *gpu);
 587void msm_gpu_perfcntr_stop(struct msm_gpu *gpu);
 588int msm_gpu_perfcntr_sample(struct msm_gpu *gpu, uint32_t *activetime,
 589                uint32_t *totaltime, uint32_t ncntrs, uint32_t *cntrs);
 590
 591void msm_gpu_retire(struct msm_gpu *gpu);
 592void msm_gpu_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit);
 593
 594int msm_gpu_init(struct drm_device *drm, struct platform_device *pdev,
 595                struct msm_gpu *gpu, const struct msm_gpu_funcs *funcs,
 596                const char *name, struct msm_gpu_config *config);
 597
 598struct msm_gem_address_space *
 599msm_gpu_create_private_address_space(struct msm_gpu *gpu, struct task_struct *task);
 600
 601void msm_gpu_cleanup(struct msm_gpu *gpu);
 602
 603struct msm_gpu *adreno_load_gpu(struct drm_device *dev);
 604void __init adreno_register(void);
 605void __exit adreno_unregister(void);
 606
 607static inline void msm_submitqueue_put(struct msm_gpu_submitqueue *queue)
 608{
 609        if (queue)
 610                kref_put(&queue->ref, msm_submitqueue_destroy);
 611}
 612
 613static inline struct msm_gpu_state *msm_gpu_crashstate_get(struct msm_gpu *gpu)
 614{
 615        struct msm_gpu_state *state = NULL;
 616
 617        mutex_lock(&gpu->lock);
 618
 619        if (gpu->crashstate) {
 620                kref_get(&gpu->crashstate->ref);
 621                state = gpu->crashstate;
 622        }
 623
 624        mutex_unlock(&gpu->lock);
 625
 626        return state;
 627}
 628
 629static inline void msm_gpu_crashstate_put(struct msm_gpu *gpu)
 630{
 631        mutex_lock(&gpu->lock);
 632
 633        if (gpu->crashstate) {
 634                if (gpu->funcs->gpu_state_put(gpu->crashstate))
 635                        gpu->crashstate = NULL;
 636        }
 637
 638        mutex_unlock(&gpu->lock);
 639}
 640
 641/*
 642 * Simple macro to semi-cleanly add the MAP_PRIV flag for targets that can
 643 * support expanded privileges
 644 */
 645#define check_apriv(gpu, flags) \
 646        (((gpu)->hw_apriv ? MSM_BO_MAP_PRIV : 0) | (flags))
 647
 648
 649#endif /* __MSM_GPU_H__ */
 650