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