blob: bb7cb9a42d3f8393805575f4dc0b522436277e46 [file] [log] [blame]
Shrenuj Bansala419c792016-10-20 14:05:11 -07001/* Copyright (c) 2008-2017, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13#ifndef __ADRENO_H
14#define __ADRENO_H
15
16#include "kgsl_device.h"
17#include "kgsl_sharedmem.h"
18#include "adreno_drawctxt.h"
19#include "adreno_ringbuffer.h"
20#include "adreno_profile.h"
21#include "adreno_dispatch.h"
22#include "kgsl_iommu.h"
23#include "adreno_perfcounter.h"
24#include <linux/stat.h>
25#include <linux/delay.h>
26
27#include "a4xx_reg.h"
28
29#ifdef CONFIG_QCOM_OCMEM
30#include <soc/qcom/ocmem.h>
31#endif
32
33#define DEVICE_3D_NAME "kgsl-3d"
34#define DEVICE_3D0_NAME "kgsl-3d0"
35
36/* ADRENO_DEVICE - Given a kgsl_device return the adreno device struct */
37#define ADRENO_DEVICE(device) \
38 container_of(device, struct adreno_device, dev)
39
40/* KGSL_DEVICE - given an adreno_device, return the KGSL device struct */
41#define KGSL_DEVICE(_dev) (&((_dev)->dev))
42
43/* ADRENO_CONTEXT - Given a context return the adreno context struct */
44#define ADRENO_CONTEXT(context) \
45 container_of(context, struct adreno_context, base)
46
47/* ADRENO_GPU_DEVICE - Given an adreno device return the GPU specific struct */
48#define ADRENO_GPU_DEVICE(_a) ((_a)->gpucore->gpudev)
49
50#define ADRENO_CHIPID_CORE(_id) (((_id) >> 24) & 0xFF)
51#define ADRENO_CHIPID_MAJOR(_id) (((_id) >> 16) & 0xFF)
52#define ADRENO_CHIPID_MINOR(_id) (((_id) >> 8) & 0xFF)
53#define ADRENO_CHIPID_PATCH(_id) ((_id) & 0xFF)
54
55/* ADRENO_GPUREV - Return the GPU ID for the given adreno_device */
56#define ADRENO_GPUREV(_a) ((_a)->gpucore->gpurev)
57
58/*
59 * ADRENO_FEATURE - return true if the specified feature is supported by the GPU
60 * core
61 */
62#define ADRENO_FEATURE(_dev, _bit) \
63 ((_dev)->gpucore->features & (_bit))
64
65/**
66 * ADRENO_QUIRK - return true if the specified quirk is required by the GPU
67 */
68#define ADRENO_QUIRK(_dev, _bit) \
69 ((_dev)->quirks & (_bit))
70
71/*
72 * ADRENO_PREEMPT_STYLE - return preemption style
73 */
74#define ADRENO_PREEMPT_STYLE(flags) \
75 ((flags & KGSL_CONTEXT_PREEMPT_STYLE_MASK) >> \
76 KGSL_CONTEXT_PREEMPT_STYLE_SHIFT)
77
78/*
79 * return the dispatcher drawqueue in which the given drawobj should
80 * be submitted
81 */
82#define ADRENO_DRAWOBJ_DISPATCH_DRAWQUEUE(c) \
83 (&((ADRENO_CONTEXT(c->context))->rb->dispatch_q))
84
85#define ADRENO_DRAWOBJ_RB(c) \
86 ((ADRENO_CONTEXT(c->context))->rb)
87
88/* Adreno core features */
89/* The core uses OCMEM for GMEM/binning memory */
90#define ADRENO_USES_OCMEM BIT(0)
91/* The core supports an accelerated warm start */
92#define ADRENO_WARM_START BIT(1)
93/* The core supports the microcode bootstrap functionality */
94#define ADRENO_USE_BOOTSTRAP BIT(2)
95/* The core supports SP/TP hw controlled power collapse */
96#define ADRENO_SPTP_PC BIT(3)
97/* The core supports Peak Power Detection(PPD)*/
98#define ADRENO_PPD BIT(4)
99/* The GPU supports content protection */
100#define ADRENO_CONTENT_PROTECTION BIT(5)
101/* The GPU supports preemption */
102#define ADRENO_PREEMPTION BIT(6)
103/* The core uses GPMU for power and limit management */
104#define ADRENO_GPMU BIT(7)
105/* The GPMU supports Limits Management */
106#define ADRENO_LM BIT(8)
107/* The core uses 64 bit GPU addresses */
108#define ADRENO_64BIT BIT(9)
109/* The GPU supports retention for cpz registers */
110#define ADRENO_CPZ_RETENTION BIT(10)
Shrenuj Bansalae672812016-02-24 14:17:30 -0800111/* The core has soft fault detection available */
112#define ADRENO_SOFT_FAULT_DETECT BIT(11)
Shrenuj Bansala419c792016-10-20 14:05:11 -0700113
114/*
115 * Adreno GPU quirks - control bits for various workarounds
116 */
117
118/* Set TWOPASSUSEWFI in PC_DBG_ECO_CNTL (5XX) */
119#define ADRENO_QUIRK_TWO_PASS_USE_WFI BIT(0)
120/* Lock/unlock mutex to sync with the IOMMU */
121#define ADRENO_QUIRK_IOMMU_SYNC BIT(1)
122/* Submit critical packets at GPU wake up */
123#define ADRENO_QUIRK_CRITICAL_PACKETS BIT(2)
124/* Mask out RB1-3 activity signals from HW hang detection logic */
125#define ADRENO_QUIRK_FAULT_DETECT_MASK BIT(3)
126/* Disable RB sampler datapath clock gating optimization */
127#define ADRENO_QUIRK_DISABLE_RB_DP2CLOCKGATING BIT(4)
128/* Disable local memory(LM) feature to avoid corner case error */
129#define ADRENO_QUIRK_DISABLE_LMLOADKILL BIT(5)
130
131/* Flags to control command packet settings */
132#define KGSL_CMD_FLAGS_NONE 0
133#define KGSL_CMD_FLAGS_PMODE BIT(0)
134#define KGSL_CMD_FLAGS_INTERNAL_ISSUE BIT(1)
135#define KGSL_CMD_FLAGS_WFI BIT(2)
136#define KGSL_CMD_FLAGS_PROFILE BIT(3)
137#define KGSL_CMD_FLAGS_PWRON_FIXUP BIT(4)
138
139/* Command identifiers */
140#define KGSL_CONTEXT_TO_MEM_IDENTIFIER 0x2EADBEEF
141#define KGSL_CMD_IDENTIFIER 0x2EEDFACE
142#define KGSL_CMD_INTERNAL_IDENTIFIER 0x2EEDD00D
143#define KGSL_START_OF_IB_IDENTIFIER 0x2EADEABE
144#define KGSL_END_OF_IB_IDENTIFIER 0x2ABEDEAD
145#define KGSL_START_OF_PROFILE_IDENTIFIER 0x2DEFADE1
146#define KGSL_END_OF_PROFILE_IDENTIFIER 0x2DEFADE2
147#define KGSL_PWRON_FIXUP_IDENTIFIER 0x2AFAFAFA
148
149/* One cannot wait forever for the core to idle, so set an upper limit to the
150 * amount of time to wait for the core to go idle
151 */
152
153#define ADRENO_IDLE_TIMEOUT (20 * 1000)
154
155#define ADRENO_UCHE_GMEM_BASE 0x100000
156
157enum adreno_gpurev {
158 ADRENO_REV_UNKNOWN = 0,
159 ADRENO_REV_A304 = 304,
160 ADRENO_REV_A305 = 305,
161 ADRENO_REV_A305C = 306,
162 ADRENO_REV_A306 = 307,
163 ADRENO_REV_A306A = 308,
164 ADRENO_REV_A310 = 310,
165 ADRENO_REV_A320 = 320,
166 ADRENO_REV_A330 = 330,
167 ADRENO_REV_A305B = 335,
168 ADRENO_REV_A405 = 405,
169 ADRENO_REV_A418 = 418,
170 ADRENO_REV_A420 = 420,
171 ADRENO_REV_A430 = 430,
172 ADRENO_REV_A505 = 505,
173 ADRENO_REV_A506 = 506,
174 ADRENO_REV_A510 = 510,
175 ADRENO_REV_A512 = 512,
176 ADRENO_REV_A530 = 530,
177 ADRENO_REV_A540 = 540,
178};
179
180#define ADRENO_START_WARM 0
181#define ADRENO_START_COLD 1
182
183#define ADRENO_SOFT_FAULT BIT(0)
184#define ADRENO_HARD_FAULT BIT(1)
185#define ADRENO_TIMEOUT_FAULT BIT(2)
186#define ADRENO_IOMMU_PAGE_FAULT BIT(3)
187#define ADRENO_PREEMPT_FAULT BIT(4)
188
189#define ADRENO_SPTP_PC_CTRL 0
190#define ADRENO_PPD_CTRL 1
191#define ADRENO_LM_CTRL 2
192#define ADRENO_HWCG_CTRL 3
193#define ADRENO_THROTTLING_CTRL 4
194
195
196/* number of throttle counters for DCVS adjustment */
197#define ADRENO_GPMU_THROTTLE_COUNTERS 4
198/* base for throttle counters */
199#define ADRENO_GPMU_THROTTLE_COUNTERS_BASE_REG 43
200
201struct adreno_gpudev;
202
203/* Time to allow preemption to complete (in ms) */
204#define ADRENO_PREEMPT_TIMEOUT 10000
205
206#define ADRENO_INT_BIT(a, _bit) (((a)->gpucore->gpudev->int_bits) ? \
207 (adreno_get_int(a, _bit) < 0 ? 0 : \
208 BIT(adreno_get_int(a, _bit))) : 0)
209
210/**
211 * enum adreno_preempt_states
212 * ADRENO_PREEMPT_NONE: No preemption is scheduled
213 * ADRENO_PREEMPT_START: The S/W has started
214 * ADRENO_PREEMPT_TRIGGERED: A preeempt has been triggered in the HW
215 * ADRENO_PREEMPT_FAULTED: The preempt timer has fired
216 * ADRENO_PREEMPT_PENDING: The H/W has signaled preemption complete
217 * ADRENO_PREEMPT_COMPLETE: Preemption could not be finished in the IRQ handler,
218 * worker has been scheduled
219 */
220enum adreno_preempt_states {
221 ADRENO_PREEMPT_NONE = 0,
222 ADRENO_PREEMPT_START,
223 ADRENO_PREEMPT_TRIGGERED,
224 ADRENO_PREEMPT_FAULTED,
225 ADRENO_PREEMPT_PENDING,
226 ADRENO_PREEMPT_COMPLETE,
227};
228
229/**
230 * struct adreno_preemption
231 * @state: The current state of preemption
232 * @counters: Memory descriptor for the memory where the GPU writes the
233 * preemption counters on switch
234 * @timer: A timer to make sure preemption doesn't stall
235 * @work: A work struct for the preemption worker (for 5XX)
236 * @token_submit: Indicates if a preempt token has been submitted in
237 * current ringbuffer (for 4XX)
238 */
239struct adreno_preemption {
240 atomic_t state;
241 struct kgsl_memdesc counters;
242 struct timer_list timer;
243 struct work_struct work;
244 bool token_submit;
245};
246
247
248struct adreno_busy_data {
249 unsigned int gpu_busy;
250 unsigned int vbif_ram_cycles;
251 unsigned int vbif_starved_ram;
252 unsigned int throttle_cycles[ADRENO_GPMU_THROTTLE_COUNTERS];
253};
254
255/**
256 * struct adreno_gpu_core - A specific GPU core definition
257 * @gpurev: Unique GPU revision identifier
258 * @core: Match for the core version of the GPU
259 * @major: Match for the major version of the GPU
260 * @minor: Match for the minor version of the GPU
261 * @patchid: Match for the patch revision of the GPU
262 * @features: Common adreno features supported by this core
263 * @pm4fw_name: Filename for th PM4 firmware
264 * @pfpfw_name: Filename for the PFP firmware
265 * @zap_name: Filename for the Zap Shader ucode
266 * @gpudev: Pointer to the GPU family specific functions for this core
267 * @gmem_size: Amount of binning memory (GMEM/OCMEM) to reserve for the core
268 * @pm4_jt_idx: Index of the jump table in the PM4 microcode
269 * @pm4_jt_addr: Address offset to load the jump table for the PM4 microcode
270 * @pfp_jt_idx: Index of the jump table in the PFP microcode
271 * @pfp_jt_addr: Address offset to load the jump table for the PFP microcode
272 * @pm4_bstrp_size: Size of the bootstrap loader for PM4 microcode
273 * @pfp_bstrp_size: Size of the bootstrap loader for PFP microcde
274 * @pfp_bstrp_ver: Version of the PFP microcode that supports bootstraping
275 * @shader_offset: Offset of shader from gpu reg base
276 * @shader_size: Shader size
277 * @num_protected_regs: number of protected registers
278 * @gpmufw_name: Filename for the GPMU firmware
279 * @gpmu_major: Match for the GPMU & firmware, major revision
280 * @gpmu_minor: Match for the GPMU & firmware, minor revision
281 * @gpmu_features: Supported features for any given GPMU version
282 * @busy_mask: mask to check if GPU is busy in RBBM_STATUS
283 * @lm_major: Limits Management register sequence, major revision
284 * @lm_minor: LM register sequence, minor revision
285 * @regfw_name: Filename for the register sequence firmware
286 * @gpmu_tsens: ID for the temporature sensor used by the GPMU
287 * @max_power: Max possible power draw of a core, units elephant tail hairs
288 */
289struct adreno_gpu_core {
290 enum adreno_gpurev gpurev;
291 unsigned int core, major, minor, patchid;
292 unsigned long features;
293 const char *pm4fw_name;
294 const char *pfpfw_name;
295 const char *zap_name;
296 struct adreno_gpudev *gpudev;
297 size_t gmem_size;
298 unsigned int pm4_jt_idx;
299 unsigned int pm4_jt_addr;
300 unsigned int pfp_jt_idx;
301 unsigned int pfp_jt_addr;
302 unsigned int pm4_bstrp_size;
303 unsigned int pfp_bstrp_size;
304 unsigned int pfp_bstrp_ver;
305 unsigned long shader_offset;
306 unsigned int shader_size;
307 unsigned int num_protected_regs;
308 const char *gpmufw_name;
309 unsigned int gpmu_major;
310 unsigned int gpmu_minor;
311 unsigned int gpmu_features;
312 unsigned int busy_mask;
313 unsigned int lm_major, lm_minor;
314 const char *regfw_name;
315 unsigned int gpmu_tsens;
316 unsigned int max_power;
317};
318
319/**
320 * struct adreno_device - The mothership structure for all adreno related info
321 * @dev: Reference to struct kgsl_device
322 * @priv: Holds the private flags specific to the adreno_device
323 * @chipid: Chip ID specific to the GPU
324 * @gmem_base: Base physical address of GMEM
325 * @gmem_size: GMEM size
326 * @gpucore: Pointer to the adreno_gpu_core structure
327 * @pfp_fw: Buffer which holds the pfp ucode
328 * @pfp_fw_size: Size of pfp ucode buffer
329 * @pfp_fw_version: Version of pfp ucode
330 * @pfp: Memory descriptor which holds pfp ucode buffer info
331 * @pm4_fw: Buffer which holds the pm4 ucode
332 * @pm4_fw_size: Size of pm4 ucode buffer
333 * @pm4_fw_version: Version of pm4 ucode
334 * @pm4: Memory descriptor which holds pm4 ucode buffer info
335 * @gpmu_cmds_size: Length of gpmu cmd stream
336 * @gpmu_cmds: gpmu cmd stream
337 * @ringbuffers: Array of pointers to adreno_ringbuffers
338 * @num_ringbuffers: Number of ringbuffers for the GPU
339 * @cur_rb: Pointer to the current ringbuffer
340 * @next_rb: Ringbuffer we are switching to during preemption
341 * @prev_rb: Ringbuffer we are switching from during preemption
342 * @fast_hang_detect: Software fault detection availability
343 * @ft_policy: Defines the fault tolerance policy
344 * @long_ib_detect: Long IB detection availability
345 * @ft_pf_policy: Defines the fault policy for page faults
346 * @ocmem_hdl: Handle to the ocmem allocated buffer
347 * @profile: Container for adreno profiler information
348 * @dispatcher: Container for adreno GPU dispatcher
349 * @pwron_fixup: Command buffer to run a post-power collapse shader workaround
350 * @pwron_fixup_dwords: Number of dwords in the command buffer
351 * @input_work: Work struct for turning on the GPU after a touch event
352 * @busy_data: Struct holding GPU VBIF busy stats
353 * @ram_cycles_lo: Number of DDR clock cycles for the monitor session
354 * @perfctr_pwr_lo: Number of cycles VBIF is stalled by DDR
355 * @halt: Atomic variable to check whether the GPU is currently halted
356 * @ctx_d_debugfs: Context debugfs node
357 * @pwrctrl_flag: Flag to hold adreno specific power attributes
358 * @profile_buffer: Memdesc holding the drawobj profiling buffer
359 * @profile_index: Index to store the start/stop ticks in the profiling
360 * buffer
361 * @sp_local_gpuaddr: Base GPU virtual address for SP local memory
362 * @sp_pvt_gpuaddr: Base GPU virtual address for SP private memory
363 * @lm_fw: The LM firmware handle
364 * @lm_sequence: Pointer to the start of the register write sequence for LM
365 * @lm_size: The dword size of the LM sequence
366 * @lm_limit: limiting value for LM
367 * @lm_threshold_count: register value for counter for lm threshold breakin
368 * @lm_threshold_cross: number of current peaks exceeding threshold
369 * @speed_bin: Indicate which power level set to use
370 * @csdev: Pointer to a coresight device (if applicable)
371 * @gpmu_throttle_counters - counteers for number of throttled clocks
372 * @irq_storm_work: Worker to handle possible interrupt storms
373 * @active_list: List to track active contexts
374 * @active_list_lock: Lock to protect active_list
375 */
376struct adreno_device {
377 struct kgsl_device dev; /* Must be first field in this struct */
378 unsigned long priv;
379 unsigned int chipid;
380 unsigned long gmem_base;
381 unsigned long gmem_size;
382 const struct adreno_gpu_core *gpucore;
383 unsigned int *pfp_fw;
384 size_t pfp_fw_size;
385 unsigned int pfp_fw_version;
386 struct kgsl_memdesc pfp;
387 unsigned int *pm4_fw;
388 size_t pm4_fw_size;
389 unsigned int pm4_fw_version;
390 struct kgsl_memdesc pm4;
391 size_t gpmu_cmds_size;
392 unsigned int *gpmu_cmds;
393 struct adreno_ringbuffer ringbuffers[KGSL_PRIORITY_MAX_RB_LEVELS];
394 int num_ringbuffers;
395 struct adreno_ringbuffer *cur_rb;
396 struct adreno_ringbuffer *next_rb;
397 struct adreno_ringbuffer *prev_rb;
398 unsigned int fast_hang_detect;
399 unsigned long ft_policy;
400 unsigned int long_ib_detect;
401 unsigned long ft_pf_policy;
402 struct ocmem_buf *ocmem_hdl;
403 struct adreno_profile profile;
404 struct adreno_dispatcher dispatcher;
405 struct kgsl_memdesc pwron_fixup;
406 unsigned int pwron_fixup_dwords;
407 struct work_struct input_work;
408 struct adreno_busy_data busy_data;
409 unsigned int ram_cycles_lo;
410 unsigned int starved_ram_lo;
411 unsigned int perfctr_pwr_lo;
412 atomic_t halt;
413 struct dentry *ctx_d_debugfs;
414 unsigned long pwrctrl_flag;
415
416 struct kgsl_memdesc profile_buffer;
417 unsigned int profile_index;
418 uint64_t sp_local_gpuaddr;
419 uint64_t sp_pvt_gpuaddr;
420 const struct firmware *lm_fw;
421 uint32_t *lm_sequence;
422 uint32_t lm_size;
423 struct adreno_preemption preempt;
424 struct work_struct gpmu_work;
425 uint32_t lm_leakage;
426 uint32_t lm_limit;
427 uint32_t lm_threshold_count;
428 uint32_t lm_threshold_cross;
429
430 unsigned int speed_bin;
431 unsigned int quirks;
432
433 struct coresight_device *csdev;
434 uint32_t gpmu_throttle_counters[ADRENO_GPMU_THROTTLE_COUNTERS];
435 struct work_struct irq_storm_work;
436
437 struct list_head active_list;
438 spinlock_t active_list_lock;
439};
440
441/**
442 * enum adreno_device_flags - Private flags for the adreno_device
443 * @ADRENO_DEVICE_PWRON - Set during init after a power collapse
444 * @ADRENO_DEVICE_PWRON_FIXUP - Set if the target requires the shader fixup
445 * after power collapse
446 * @ADRENO_DEVICE_CORESIGHT - Set if the coresight (trace bus) registers should
447 * be restored after power collapse
448 * @ADRENO_DEVICE_HANG_INTR - Set if the hang interrupt should be enabled for
449 * this target
450 * @ADRENO_DEVICE_STARTED - Set if the device start sequence is in progress
451 * @ADRENO_DEVICE_FAULT - Set if the device is currently in fault (and shouldn't
452 * send any more commands to the ringbuffer)
453 * @ADRENO_DEVICE_DRAWOBJ_PROFILE - Set if the device supports drawobj
454 * profiling via the ALWAYSON counter
455 * @ADRENO_DEVICE_PREEMPTION - Turn on/off preemption
456 * @ADRENO_DEVICE_SOFT_FAULT_DETECT - Set if soft fault detect is enabled
457 * @ADRENO_DEVICE_GPMU_INITIALIZED - Set if GPMU firmware initialization succeed
458 * @ADRENO_DEVICE_ISDB_ENABLED - Set if the Integrated Shader DeBugger is
459 * attached and enabled
460 * @ADRENO_DEVICE_CACHE_FLUSH_TS_SUSPENDED - Set if a CACHE_FLUSH_TS irq storm
461 * is in progress
462 */
463enum adreno_device_flags {
464 ADRENO_DEVICE_PWRON = 0,
465 ADRENO_DEVICE_PWRON_FIXUP = 1,
466 ADRENO_DEVICE_INITIALIZED = 2,
467 ADRENO_DEVICE_CORESIGHT = 3,
468 ADRENO_DEVICE_HANG_INTR = 4,
469 ADRENO_DEVICE_STARTED = 5,
470 ADRENO_DEVICE_FAULT = 6,
471 ADRENO_DEVICE_DRAWOBJ_PROFILE = 7,
472 ADRENO_DEVICE_GPU_REGULATOR_ENABLED = 8,
473 ADRENO_DEVICE_PREEMPTION = 9,
474 ADRENO_DEVICE_SOFT_FAULT_DETECT = 10,
475 ADRENO_DEVICE_GPMU_INITIALIZED = 11,
476 ADRENO_DEVICE_ISDB_ENABLED = 12,
477 ADRENO_DEVICE_CACHE_FLUSH_TS_SUSPENDED = 13,
478};
479
480/**
481 * struct adreno_drawobj_profile_entry - a single drawobj entry in the
482 * kernel profiling buffer
483 * @started: Number of GPU ticks at start of the drawobj
484 * @retired: Number of GPU ticks at the end of the drawobj
485 */
486struct adreno_drawobj_profile_entry {
487 uint64_t started;
488 uint64_t retired;
489};
490
491#define ADRENO_DRAWOBJ_PROFILE_COUNT \
492 (PAGE_SIZE / sizeof(struct adreno_drawobj_profile_entry))
493
494#define ADRENO_DRAWOBJ_PROFILE_OFFSET(_index, _member) \
495 ((_index) * sizeof(struct adreno_drawobj_profile_entry) \
496 + offsetof(struct adreno_drawobj_profile_entry, _member))
497
498
499/**
500 * adreno_regs: List of registers that are used in kgsl driver for all
501 * 3D devices. Each device type has different offset value for the same
502 * register, so an array of register offsets are declared for every device
503 * and are indexed by the enumeration values defined in this enum
504 */
505enum adreno_regs {
506 ADRENO_REG_CP_ME_RAM_WADDR,
507 ADRENO_REG_CP_ME_RAM_DATA,
508 ADRENO_REG_CP_PFP_UCODE_DATA,
509 ADRENO_REG_CP_PFP_UCODE_ADDR,
510 ADRENO_REG_CP_WFI_PEND_CTR,
511 ADRENO_REG_CP_RB_BASE,
512 ADRENO_REG_CP_RB_BASE_HI,
513 ADRENO_REG_CP_RB_RPTR_ADDR_LO,
514 ADRENO_REG_CP_RB_RPTR_ADDR_HI,
515 ADRENO_REG_CP_RB_RPTR,
516 ADRENO_REG_CP_RB_WPTR,
517 ADRENO_REG_CP_CNTL,
518 ADRENO_REG_CP_ME_CNTL,
519 ADRENO_REG_CP_RB_CNTL,
520 ADRENO_REG_CP_IB1_BASE,
521 ADRENO_REG_CP_IB1_BASE_HI,
522 ADRENO_REG_CP_IB1_BUFSZ,
523 ADRENO_REG_CP_IB2_BASE,
524 ADRENO_REG_CP_IB2_BASE_HI,
525 ADRENO_REG_CP_IB2_BUFSZ,
526 ADRENO_REG_CP_TIMESTAMP,
527 ADRENO_REG_CP_SCRATCH_REG6,
528 ADRENO_REG_CP_SCRATCH_REG7,
529 ADRENO_REG_CP_ME_RAM_RADDR,
530 ADRENO_REG_CP_ROQ_ADDR,
531 ADRENO_REG_CP_ROQ_DATA,
532 ADRENO_REG_CP_MERCIU_ADDR,
533 ADRENO_REG_CP_MERCIU_DATA,
534 ADRENO_REG_CP_MERCIU_DATA2,
535 ADRENO_REG_CP_MEQ_ADDR,
536 ADRENO_REG_CP_MEQ_DATA,
537 ADRENO_REG_CP_HW_FAULT,
538 ADRENO_REG_CP_PROTECT_STATUS,
539 ADRENO_REG_CP_PREEMPT,
540 ADRENO_REG_CP_PREEMPT_DEBUG,
541 ADRENO_REG_CP_PREEMPT_DISABLE,
542 ADRENO_REG_CP_PROTECT_REG_0,
543 ADRENO_REG_CP_CONTEXT_SWITCH_SMMU_INFO_LO,
544 ADRENO_REG_CP_CONTEXT_SWITCH_SMMU_INFO_HI,
545 ADRENO_REG_RBBM_STATUS,
546 ADRENO_REG_RBBM_STATUS3,
547 ADRENO_REG_RBBM_PERFCTR_CTL,
548 ADRENO_REG_RBBM_PERFCTR_LOAD_CMD0,
549 ADRENO_REG_RBBM_PERFCTR_LOAD_CMD1,
550 ADRENO_REG_RBBM_PERFCTR_LOAD_CMD2,
551 ADRENO_REG_RBBM_PERFCTR_LOAD_CMD3,
552 ADRENO_REG_RBBM_PERFCTR_PWR_1_LO,
553 ADRENO_REG_RBBM_INT_0_MASK,
554 ADRENO_REG_RBBM_INT_0_STATUS,
555 ADRENO_REG_RBBM_PM_OVERRIDE2,
556 ADRENO_REG_RBBM_INT_CLEAR_CMD,
557 ADRENO_REG_RBBM_SW_RESET_CMD,
558 ADRENO_REG_RBBM_BLOCK_SW_RESET_CMD,
559 ADRENO_REG_RBBM_BLOCK_SW_RESET_CMD2,
560 ADRENO_REG_RBBM_CLOCK_CTL,
561 ADRENO_REG_VPC_DEBUG_RAM_SEL,
562 ADRENO_REG_VPC_DEBUG_RAM_READ,
563 ADRENO_REG_PA_SC_AA_CONFIG,
564 ADRENO_REG_SQ_GPR_MANAGEMENT,
565 ADRENO_REG_SQ_INST_STORE_MANAGEMENT,
566 ADRENO_REG_TP0_CHICKEN,
567 ADRENO_REG_RBBM_RBBM_CTL,
568 ADRENO_REG_UCHE_INVALIDATE0,
569 ADRENO_REG_UCHE_INVALIDATE1,
570 ADRENO_REG_RBBM_PERFCTR_LOAD_VALUE_LO,
571 ADRENO_REG_RBBM_PERFCTR_LOAD_VALUE_HI,
572 ADRENO_REG_RBBM_SECVID_TRUST_CONTROL,
573 ADRENO_REG_RBBM_ALWAYSON_COUNTER_LO,
574 ADRENO_REG_RBBM_ALWAYSON_COUNTER_HI,
575 ADRENO_REG_RBBM_SECVID_TRUST_CONFIG,
576 ADRENO_REG_RBBM_SECVID_TSB_CONTROL,
577 ADRENO_REG_RBBM_SECVID_TSB_TRUSTED_BASE,
578 ADRENO_REG_RBBM_SECVID_TSB_TRUSTED_BASE_HI,
579 ADRENO_REG_RBBM_SECVID_TSB_TRUSTED_SIZE,
580 ADRENO_REG_VBIF_XIN_HALT_CTRL0,
581 ADRENO_REG_VBIF_XIN_HALT_CTRL1,
582 ADRENO_REG_VBIF_VERSION,
583 ADRENO_REG_REGISTER_MAX,
584};
585
586enum adreno_int_bits {
587 ADRENO_INT_RBBM_AHB_ERROR,
588 ADRENO_INT_BITS_MAX,
589};
590
591/**
592 * adreno_reg_offsets: Holds array of register offsets
593 * @offsets: Offset array of size defined by enum adreno_regs
594 * @offset_0: This is the index of the register in offset array whose value
595 * is 0. 0 is a valid register offset and during initialization of the
596 * offset array we need to know if an offset value is correctly defined to 0
597 */
598struct adreno_reg_offsets {
599 unsigned int *const offsets;
600 enum adreno_regs offset_0;
601};
602
603#define ADRENO_REG_UNUSED 0xFFFFFFFF
604#define ADRENO_REG_SKIP 0xFFFFFFFE
605#define ADRENO_REG_DEFINE(_offset, _reg) [_offset] = _reg
606#define ADRENO_INT_DEFINE(_offset, _val) ADRENO_REG_DEFINE(_offset, _val)
607
608/*
609 * struct adreno_vbif_data - Describes vbif register value pair
610 * @reg: Offset to vbif register
611 * @val: The value that should be programmed in the register at reg
612 */
613struct adreno_vbif_data {
614 unsigned int reg;
615 unsigned int val;
616};
617
618/*
619 * struct adreno_vbif_platform - Holds an array of vbif reg value pairs
620 * for a particular core
621 * @devfunc: Pointer to platform/core identification function
622 * @vbif: Array of reg value pairs for vbif registers
623 */
624struct adreno_vbif_platform {
625 int (*devfunc)(struct adreno_device *);
626 const struct adreno_vbif_data *vbif;
627};
628
629/*
630 * struct adreno_vbif_snapshot_registers - Holds an array of vbif registers
631 * listed for snapshot dump for a particular core
632 * @version: vbif version
633 * @mask: vbif revision mask
634 * @registers: vbif registers listed for snapshot dump
635 * @count: count of vbif registers listed for snapshot
636 */
637struct adreno_vbif_snapshot_registers {
638 const unsigned int version;
639 const unsigned int mask;
640 const unsigned int *registers;
641 const int count;
642};
643
644/**
645 * struct adreno_coresight_register - Definition for a coresight (tracebus)
646 * debug register
647 * @offset: Offset of the debug register in the KGSL mmio region
648 * @initial: Default value to write when coresight is enabled
649 * @value: Current shadow value of the register (to be reprogrammed after power
650 * collapse)
651 */
652struct adreno_coresight_register {
653 unsigned int offset;
654 unsigned int initial;
655 unsigned int value;
656};
657
658struct adreno_coresight_attr {
659 struct device_attribute attr;
660 struct adreno_coresight_register *reg;
661};
662
663ssize_t adreno_coresight_show_register(struct device *device,
664 struct device_attribute *attr, char *buf);
665
666ssize_t adreno_coresight_store_register(struct device *dev,
667 struct device_attribute *attr, const char *buf, size_t size);
668
669#define ADRENO_CORESIGHT_ATTR(_attrname, _reg) \
670 struct adreno_coresight_attr coresight_attr_##_attrname = { \
671 __ATTR(_attrname, 0644, \
672 adreno_coresight_show_register, \
673 adreno_coresight_store_register), \
674 (_reg), }
675
676/**
677 * struct adreno_coresight - GPU specific coresight definition
678 * @registers - Array of GPU specific registers to configure trace bus output
679 * @count - Number of registers in the array
680 * @groups - Pointer to an attribute list of control files
681 * @atid - The unique ATID value of the coresight device
682 */
683struct adreno_coresight {
684 struct adreno_coresight_register *registers;
685 unsigned int count;
686 const struct attribute_group **groups;
687 unsigned int atid;
688};
689
690
691struct adreno_irq_funcs {
692 void (*func)(struct adreno_device *, int);
693};
694#define ADRENO_IRQ_CALLBACK(_c) { .func = _c }
695
696struct adreno_irq {
697 unsigned int mask;
698 struct adreno_irq_funcs *funcs;
699};
700
701/*
702 * struct adreno_debugbus_block - Holds info about debug buses of a chip
703 * @block_id: Bus identifier
704 * @dwords: Number of dwords of data that this block holds
705 */
706struct adreno_debugbus_block {
707 unsigned int block_id;
708 unsigned int dwords;
709};
710
711/*
712 * struct adreno_snapshot_section_sizes - Structure holding the size of
713 * different sections dumped during device snapshot
714 * @cp_pfp: CP PFP data section size
715 * @cp_me: CP ME data section size
716 * @vpc_mem: VPC memory section size
717 * @cp_meq: CP MEQ size
718 * @shader_mem: Size of shader memory of 1 shader section
719 * @cp_merciu: CP MERCIU size
720 * @roq: ROQ size
721 */
722struct adreno_snapshot_sizes {
723 int cp_pfp;
724 int cp_me;
725 int vpc_mem;
726 int cp_meq;
727 int shader_mem;
728 int cp_merciu;
729 int roq;
730};
731
732/*
733 * struct adreno_snapshot_data - Holds data used in snapshot
734 * @sect_sizes: Has sections sizes
735 */
736struct adreno_snapshot_data {
737 struct adreno_snapshot_sizes *sect_sizes;
738};
739
740struct adreno_gpudev {
741 /*
742 * These registers are in a different location on different devices,
743 * so define them in the structure and use them as variables.
744 */
745 const struct adreno_reg_offsets *reg_offsets;
746 unsigned int *const int_bits;
747 const struct adreno_ft_perf_counters *ft_perf_counters;
748 unsigned int ft_perf_counters_count;
749
750 struct adreno_perfcounters *perfcounters;
751 const struct adreno_invalid_countables *invalid_countables;
752 struct adreno_snapshot_data *snapshot_data;
753
754 struct adreno_coresight *coresight;
755
756 struct adreno_irq *irq;
757 int num_prio_levels;
758 unsigned int vbif_xin_halt_ctrl0_mask;
759 /* GPU specific function hooks */
760 void (*irq_trace)(struct adreno_device *, unsigned int status);
761 void (*snapshot)(struct adreno_device *, struct kgsl_snapshot *);
762 void (*platform_setup)(struct adreno_device *);
763 void (*init)(struct adreno_device *);
764 void (*remove)(struct adreno_device *);
765 int (*rb_start)(struct adreno_device *, unsigned int start_type);
766 int (*microcode_read)(struct adreno_device *);
767 void (*perfcounter_init)(struct adreno_device *);
768 void (*perfcounter_close)(struct adreno_device *);
769 void (*start)(struct adreno_device *);
770 bool (*is_sptp_idle)(struct adreno_device *);
771 int (*regulator_enable)(struct adreno_device *);
772 void (*regulator_disable)(struct adreno_device *);
773 void (*pwrlevel_change_settings)(struct adreno_device *,
774 unsigned int prelevel, unsigned int postlevel,
775 bool post);
776 uint64_t (*read_throttling_counters)(struct adreno_device *);
777 void (*count_throttles)(struct adreno_device *, uint64_t adj);
778 int (*enable_pwr_counters)(struct adreno_device *,
779 unsigned int counter);
780 unsigned int (*preemption_pre_ibsubmit)(
781 struct adreno_device *adreno_dev,
782 struct adreno_ringbuffer *rb,
783 unsigned int *cmds,
784 struct kgsl_context *context);
785 int (*preemption_yield_enable)(unsigned int *);
786 unsigned int (*preemption_post_ibsubmit)(
787 struct adreno_device *adreno_dev,
788 unsigned int *cmds);
789 int (*preemption_init)(struct adreno_device *);
790 void (*preemption_schedule)(struct adreno_device *);
791 void (*enable_64bit)(struct adreno_device *);
792 void (*clk_set_options)(struct adreno_device *,
793 const char *, struct clk *);
794};
795
796/**
797 * enum kgsl_ft_policy_bits - KGSL fault tolerance policy bits
798 * @KGSL_FT_OFF: Disable fault detection (not used)
799 * @KGSL_FT_REPLAY: Replay the faulting command
800 * @KGSL_FT_SKIPIB: Skip the faulting indirect buffer
801 * @KGSL_FT_SKIPFRAME: Skip the frame containing the faulting IB
802 * @KGSL_FT_DISABLE: Tells the dispatcher to disable FT for the command obj
803 * @KGSL_FT_TEMP_DISABLE: Disables FT for all commands
804 * @KGSL_FT_THROTTLE: Disable the context if it faults too often
805 * @KGSL_FT_SKIPCMD: Skip the command containing the faulting IB
806 */
807enum kgsl_ft_policy_bits {
808 KGSL_FT_OFF = 0,
809 KGSL_FT_REPLAY = 1,
810 KGSL_FT_SKIPIB = 2,
811 KGSL_FT_SKIPFRAME = 3,
812 KGSL_FT_DISABLE = 4,
813 KGSL_FT_TEMP_DISABLE = 5,
814 KGSL_FT_THROTTLE = 6,
815 KGSL_FT_SKIPCMD = 7,
816 /* KGSL_FT_MAX_BITS is used to calculate the mask */
817 KGSL_FT_MAX_BITS,
818 /* Internal bits - set during GFT */
819 /* Skip the PM dump on replayed command obj's */
820 KGSL_FT_SKIP_PMDUMP = 31,
821};
822
823#define KGSL_FT_POLICY_MASK GENMASK(KGSL_FT_MAX_BITS - 1, 0)
824
825#define KGSL_FT_DEFAULT_POLICY \
826 (BIT(KGSL_FT_REPLAY) | \
827 BIT(KGSL_FT_SKIPCMD) | \
828 BIT(KGSL_FT_THROTTLE))
829
830#define ADRENO_FT_TYPES \
831 { BIT(KGSL_FT_OFF), "off" }, \
832 { BIT(KGSL_FT_REPLAY), "replay" }, \
833 { BIT(KGSL_FT_SKIPIB), "skipib" }, \
834 { BIT(KGSL_FT_SKIPFRAME), "skipframe" }, \
835 { BIT(KGSL_FT_DISABLE), "disable" }, \
836 { BIT(KGSL_FT_TEMP_DISABLE), "temp" }, \
837 { BIT(KGSL_FT_THROTTLE), "throttle"}, \
838 { BIT(KGSL_FT_SKIPCMD), "skipcmd" }
839
840/**
841 * enum kgsl_ft_pagefault_policy_bits - KGSL pagefault policy bits
842 * @KGSL_FT_PAGEFAULT_INT_ENABLE: No longer used, but retained for compatibility
843 * @KGSL_FT_PAGEFAULT_GPUHALT_ENABLE: enable GPU halt on pagefaults
844 * @KGSL_FT_PAGEFAULT_LOG_ONE_PER_PAGE: log one pagefault per page
845 * @KGSL_FT_PAGEFAULT_LOG_ONE_PER_INT: log one pagefault per interrupt
846 */
847enum {
848 KGSL_FT_PAGEFAULT_INT_ENABLE = 0,
849 KGSL_FT_PAGEFAULT_GPUHALT_ENABLE = 1,
850 KGSL_FT_PAGEFAULT_LOG_ONE_PER_PAGE = 2,
851 KGSL_FT_PAGEFAULT_LOG_ONE_PER_INT = 3,
852 /* KGSL_FT_PAGEFAULT_MAX_BITS is used to calculate the mask */
853 KGSL_FT_PAGEFAULT_MAX_BITS,
854};
855
856#define KGSL_FT_PAGEFAULT_MASK GENMASK(KGSL_FT_PAGEFAULT_MAX_BITS - 1, 0)
857
858#define KGSL_FT_PAGEFAULT_DEFAULT_POLICY 0
859
860#define FOR_EACH_RINGBUFFER(_dev, _rb, _i) \
861 for ((_i) = 0, (_rb) = &((_dev)->ringbuffers[0]); \
862 (_i) < (_dev)->num_ringbuffers; \
863 (_i)++, (_rb)++)
864
865struct adreno_ft_perf_counters {
866 unsigned int counter;
867 unsigned int countable;
868};
869
870extern unsigned int *adreno_ft_regs;
871extern unsigned int adreno_ft_regs_num;
872extern unsigned int *adreno_ft_regs_val;
873
874extern struct adreno_gpudev adreno_a3xx_gpudev;
875extern struct adreno_gpudev adreno_a4xx_gpudev;
876extern struct adreno_gpudev adreno_a5xx_gpudev;
877
878extern int adreno_wake_nice;
879extern unsigned int adreno_wake_timeout;
880
881long adreno_ioctl(struct kgsl_device_private *dev_priv,
882 unsigned int cmd, unsigned long arg);
883
884long adreno_ioctl_helper(struct kgsl_device_private *dev_priv,
885 unsigned int cmd, unsigned long arg,
886 const struct kgsl_ioctl *cmds, int len);
887
888int adreno_spin_idle(struct adreno_device *device, unsigned int timeout);
889int adreno_idle(struct kgsl_device *device);
890bool adreno_isidle(struct kgsl_device *device);
891
892int adreno_set_constraint(struct kgsl_device *device,
893 struct kgsl_context *context,
894 struct kgsl_device_constraint *constraint);
895
896void adreno_shadermem_regread(struct kgsl_device *device,
897 unsigned int offsetwords,
898 unsigned int *value);
899
900void adreno_snapshot(struct kgsl_device *device,
901 struct kgsl_snapshot *snapshot,
902 struct kgsl_context *context);
903
904int adreno_reset(struct kgsl_device *device, int fault);
905
906void adreno_fault_skipcmd_detached(struct adreno_device *adreno_dev,
907 struct adreno_context *drawctxt,
908 struct kgsl_drawobj *drawobj);
909
910int adreno_coresight_init(struct adreno_device *adreno_dev);
911
912void adreno_coresight_start(struct adreno_device *adreno_dev);
913void adreno_coresight_stop(struct adreno_device *adreno_dev);
914
915void adreno_coresight_remove(struct adreno_device *adreno_dev);
916
917bool adreno_hw_isidle(struct adreno_device *adreno_dev);
918
919void adreno_fault_detect_start(struct adreno_device *adreno_dev);
920void adreno_fault_detect_stop(struct adreno_device *adreno_dev);
921
922void adreno_hang_int_callback(struct adreno_device *adreno_dev, int bit);
923void adreno_cp_callback(struct adreno_device *adreno_dev, int bit);
924
925int adreno_sysfs_init(struct adreno_device *adreno_dev);
926void adreno_sysfs_close(struct adreno_device *adreno_dev);
927
928void adreno_irqctrl(struct adreno_device *adreno_dev, int state);
929
930long adreno_ioctl_perfcounter_get(struct kgsl_device_private *dev_priv,
931 unsigned int cmd, void *data);
932
933long adreno_ioctl_perfcounter_put(struct kgsl_device_private *dev_priv,
934 unsigned int cmd, void *data);
935
936int adreno_efuse_map(struct adreno_device *adreno_dev);
937int adreno_efuse_read_u32(struct adreno_device *adreno_dev, unsigned int offset,
938 unsigned int *val);
939void adreno_efuse_unmap(struct adreno_device *adreno_dev);
940
941#define ADRENO_TARGET(_name, _id) \
942static inline int adreno_is_##_name(struct adreno_device *adreno_dev) \
943{ \
944 return (ADRENO_GPUREV(adreno_dev) == (_id)); \
945}
946
947static inline int adreno_is_a3xx(struct adreno_device *adreno_dev)
948{
949 return ((ADRENO_GPUREV(adreno_dev) >= 300) &&
950 (ADRENO_GPUREV(adreno_dev) < 400));
951}
952
953ADRENO_TARGET(a304, ADRENO_REV_A304)
954ADRENO_TARGET(a305, ADRENO_REV_A305)
955ADRENO_TARGET(a305b, ADRENO_REV_A305B)
956ADRENO_TARGET(a305c, ADRENO_REV_A305C)
957ADRENO_TARGET(a306, ADRENO_REV_A306)
958ADRENO_TARGET(a306a, ADRENO_REV_A306A)
959ADRENO_TARGET(a310, ADRENO_REV_A310)
960ADRENO_TARGET(a320, ADRENO_REV_A320)
961ADRENO_TARGET(a330, ADRENO_REV_A330)
962
963static inline int adreno_is_a330v2(struct adreno_device *adreno_dev)
964{
965 return ((ADRENO_GPUREV(adreno_dev) == ADRENO_REV_A330) &&
966 (ADRENO_CHIPID_PATCH(adreno_dev->chipid) > 0));
967}
968
969static inline int adreno_is_a330v21(struct adreno_device *adreno_dev)
970{
971 return ((ADRENO_GPUREV(adreno_dev) == ADRENO_REV_A330) &&
972 (ADRENO_CHIPID_PATCH(adreno_dev->chipid) > 0xF));
973}
974
975static inline int adreno_is_a4xx(struct adreno_device *adreno_dev)
976{
977 return ADRENO_GPUREV(adreno_dev) >= 400 &&
978 ADRENO_GPUREV(adreno_dev) < 500;
979}
980
981ADRENO_TARGET(a405, ADRENO_REV_A405);
982
983static inline int adreno_is_a405v2(struct adreno_device *adreno_dev)
984{
985 return (ADRENO_GPUREV(adreno_dev) == ADRENO_REV_A405) &&
986 (ADRENO_CHIPID_PATCH(adreno_dev->chipid) == 0x10);
987}
988
989ADRENO_TARGET(a418, ADRENO_REV_A418)
990ADRENO_TARGET(a420, ADRENO_REV_A420)
991ADRENO_TARGET(a430, ADRENO_REV_A430)
992
993static inline int adreno_is_a430v2(struct adreno_device *adreno_dev)
994{
995 return ((ADRENO_GPUREV(adreno_dev) == ADRENO_REV_A430) &&
996 (ADRENO_CHIPID_PATCH(adreno_dev->chipid) == 1));
997}
998
999static inline int adreno_is_a5xx(struct adreno_device *adreno_dev)
1000{
1001 return ADRENO_GPUREV(adreno_dev) >= 500 &&
1002 ADRENO_GPUREV(adreno_dev) < 600;
1003}
1004
1005ADRENO_TARGET(a505, ADRENO_REV_A505)
1006ADRENO_TARGET(a506, ADRENO_REV_A506)
1007ADRENO_TARGET(a510, ADRENO_REV_A510)
1008ADRENO_TARGET(a512, ADRENO_REV_A512)
1009ADRENO_TARGET(a530, ADRENO_REV_A530)
1010ADRENO_TARGET(a540, ADRENO_REV_A540)
1011
1012static inline int adreno_is_a530v1(struct adreno_device *adreno_dev)
1013{
1014 return (ADRENO_GPUREV(adreno_dev) == ADRENO_REV_A530) &&
1015 (ADRENO_CHIPID_PATCH(adreno_dev->chipid) == 0);
1016}
1017
1018static inline int adreno_is_a530v2(struct adreno_device *adreno_dev)
1019{
1020 return (ADRENO_GPUREV(adreno_dev) == ADRENO_REV_A530) &&
1021 (ADRENO_CHIPID_PATCH(adreno_dev->chipid) == 1);
1022}
1023
1024static inline int adreno_is_a530v3(struct adreno_device *adreno_dev)
1025{
1026 return (ADRENO_GPUREV(adreno_dev) == ADRENO_REV_A530) &&
1027 (ADRENO_CHIPID_PATCH(adreno_dev->chipid) == 2);
1028}
1029
1030static inline int adreno_is_a505_or_a506(struct adreno_device *adreno_dev)
1031{
1032 return ADRENO_GPUREV(adreno_dev) >= 505 &&
1033 ADRENO_GPUREV(adreno_dev) <= 506;
1034}
1035
1036static inline int adreno_is_a540v1(struct adreno_device *adreno_dev)
1037{
1038 return (ADRENO_GPUREV(adreno_dev) == ADRENO_REV_A540) &&
1039 (ADRENO_CHIPID_PATCH(adreno_dev->chipid) == 0);
1040}
1041
1042static inline int adreno_is_a540v2(struct adreno_device *adreno_dev)
1043{
1044 return (ADRENO_GPUREV(adreno_dev) == ADRENO_REV_A540) &&
1045 (ADRENO_CHIPID_PATCH(adreno_dev->chipid) == 1);
1046}
1047
1048/*
1049 * adreno_checkreg_off() - Checks the validity of a register enum
1050 * @adreno_dev: Pointer to adreno device
1051 * @offset_name: The register enum that is checked
1052 */
1053static inline bool adreno_checkreg_off(struct adreno_device *adreno_dev,
1054 enum adreno_regs offset_name)
1055{
1056 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
1057
1058 if (offset_name >= ADRENO_REG_REGISTER_MAX ||
1059 gpudev->reg_offsets->offsets[offset_name] == ADRENO_REG_UNUSED)
1060 return false;
1061
1062 /*
1063 * GPU register programming is kept common as much as possible
1064 * across the cores, Use ADRENO_REG_SKIP when certain register
1065 * programming needs to be skipped for certain GPU cores.
1066 * Example: Certain registers on a5xx like IB1_BASE are 64 bit.
1067 * Common programming programs 64bit register but upper 32 bits
1068 * are skipped in a4xx and a3xx using ADRENO_REG_SKIP.
1069 */
1070 if (gpudev->reg_offsets->offsets[offset_name] == ADRENO_REG_SKIP)
1071 return false;
1072
1073 return true;
1074}
1075
1076/*
1077 * adreno_readreg() - Read a register by getting its offset from the
1078 * offset array defined in gpudev node
1079 * @adreno_dev: Pointer to the the adreno device
1080 * @offset_name: The register enum that is to be read
1081 * @val: Register value read is placed here
1082 */
1083static inline void adreno_readreg(struct adreno_device *adreno_dev,
1084 enum adreno_regs offset_name, unsigned int *val)
1085{
1086 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
1087
1088 if (adreno_checkreg_off(adreno_dev, offset_name))
1089 kgsl_regread(KGSL_DEVICE(adreno_dev),
1090 gpudev->reg_offsets->offsets[offset_name], val);
1091 else
1092 *val = 0;
1093}
1094
1095/*
1096 * adreno_writereg() - Write a register by getting its offset from the
1097 * offset array defined in gpudev node
1098 * @adreno_dev: Pointer to the the adreno device
1099 * @offset_name: The register enum that is to be written
1100 * @val: Value to write
1101 */
1102static inline void adreno_writereg(struct adreno_device *adreno_dev,
1103 enum adreno_regs offset_name, unsigned int val)
1104{
1105 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
1106
1107 if (adreno_checkreg_off(adreno_dev, offset_name))
1108 kgsl_regwrite(KGSL_DEVICE(adreno_dev),
1109 gpudev->reg_offsets->offsets[offset_name], val);
1110}
1111
1112/*
1113 * adreno_getreg() - Returns the offset value of a register from the
1114 * register offset array in the gpudev node
1115 * @adreno_dev: Pointer to the the adreno device
1116 * @offset_name: The register enum whore offset is returned
1117 */
1118static inline unsigned int adreno_getreg(struct adreno_device *adreno_dev,
1119 enum adreno_regs offset_name)
1120{
1121 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
1122
1123 if (!adreno_checkreg_off(adreno_dev, offset_name))
1124 return ADRENO_REG_REGISTER_MAX;
1125 return gpudev->reg_offsets->offsets[offset_name];
1126}
1127
1128/*
1129 * adreno_get_int() - Returns the offset value of an interrupt bit from
1130 * the interrupt bit array in the gpudev node
1131 * @adreno_dev: Pointer to the the adreno device
1132 * @bit_name: The interrupt bit enum whose bit is returned
1133 */
1134static inline unsigned int adreno_get_int(struct adreno_device *adreno_dev,
1135 enum adreno_int_bits bit_name)
1136{
1137 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
1138
1139 if (bit_name >= ADRENO_INT_BITS_MAX)
1140 return -ERANGE;
1141
1142 return gpudev->int_bits[bit_name];
1143}
1144
1145/**
1146 * adreno_gpu_fault() - Return the current state of the GPU
1147 * @adreno_dev: A pointer to the adreno_device to query
1148 *
1149 * Return 0 if there is no fault or positive with the last type of fault that
1150 * occurred
1151 */
1152static inline unsigned int adreno_gpu_fault(struct adreno_device *adreno_dev)
1153{
1154 /* make sure we're reading the latest value */
1155 smp_rmb();
1156 return atomic_read(&adreno_dev->dispatcher.fault);
1157}
1158
1159/**
1160 * adreno_set_gpu_fault() - Set the current fault status of the GPU
1161 * @adreno_dev: A pointer to the adreno_device to set
1162 * @state: fault state to set
1163 *
1164 */
1165static inline void adreno_set_gpu_fault(struct adreno_device *adreno_dev,
1166 int state)
1167{
1168 /* only set the fault bit w/o overwriting other bits */
1169 atomic_add(state, &adreno_dev->dispatcher.fault);
1170
1171 /* make sure other CPUs see the update */
1172 smp_wmb();
1173}
1174
1175
1176/**
1177 * adreno_clear_gpu_fault() - Clear the GPU fault register
1178 * @adreno_dev: A pointer to an adreno_device structure
1179 *
1180 * Clear the GPU fault status for the adreno device
1181 */
1182
1183static inline void adreno_clear_gpu_fault(struct adreno_device *adreno_dev)
1184{
1185 atomic_set(&adreno_dev->dispatcher.fault, 0);
1186
1187 /* make sure other CPUs see the update */
1188 smp_wmb();
1189}
1190
1191/**
1192 * adreno_gpu_halt() - Return the GPU halt refcount
1193 * @adreno_dev: A pointer to the adreno_device
1194 */
1195static inline int adreno_gpu_halt(struct adreno_device *adreno_dev)
1196{
1197 /* make sure we're reading the latest value */
1198 smp_rmb();
1199 return atomic_read(&adreno_dev->halt);
1200}
1201
1202
1203/**
1204 * adreno_clear_gpu_halt() - Clear the GPU halt refcount
1205 * @adreno_dev: A pointer to the adreno_device
1206 */
1207static inline void adreno_clear_gpu_halt(struct adreno_device *adreno_dev)
1208{
1209 atomic_set(&adreno_dev->halt, 0);
1210
1211 /* make sure other CPUs see the update */
1212 smp_wmb();
1213}
1214
1215/**
1216 * adreno_get_gpu_halt() - Increment GPU halt refcount
1217 * @adreno_dev: A pointer to the adreno_device
1218 */
1219static inline void adreno_get_gpu_halt(struct adreno_device *adreno_dev)
1220{
1221 atomic_inc(&adreno_dev->halt);
1222}
1223
1224/**
1225 * adreno_put_gpu_halt() - Decrement GPU halt refcount
1226 * @adreno_dev: A pointer to the adreno_device
1227 */
1228static inline void adreno_put_gpu_halt(struct adreno_device *adreno_dev)
1229{
1230 /* Make sure the refcount is good */
1231 int ret = atomic_dec_if_positive(&adreno_dev->halt);
1232
1233 WARN(ret < 0, "GPU halt refcount unbalanced\n");
1234}
1235
1236
1237/*
1238 * adreno_vbif_start() - Program VBIF registers, called in device start
1239 * @adreno_dev: Pointer to device whose vbif data is to be programmed
1240 * @vbif_platforms: list register value pair of vbif for a family
1241 * of adreno cores
1242 * @num_platforms: Number of platforms contained in vbif_platforms
1243 */
1244static inline void adreno_vbif_start(struct adreno_device *adreno_dev,
1245 const struct adreno_vbif_platform *vbif_platforms,
1246 int num_platforms)
1247{
1248 int i;
1249 const struct adreno_vbif_data *vbif = NULL;
1250
1251 for (i = 0; i < num_platforms; i++) {
1252 if (vbif_platforms[i].devfunc(adreno_dev)) {
1253 vbif = vbif_platforms[i].vbif;
1254 break;
1255 }
1256 }
1257
1258 while ((vbif != NULL) && (vbif->reg != 0)) {
1259 kgsl_regwrite(KGSL_DEVICE(adreno_dev), vbif->reg, vbif->val);
1260 vbif++;
1261 }
1262}
1263
1264/**
1265 * adreno_set_protected_registers() - Protect the specified range of registers
1266 * from being accessed by the GPU
1267 * @adreno_dev: pointer to the Adreno device
1268 * @index: Pointer to the index of the protect mode register to write to
1269 * @reg: Starting dword register to write
1270 * @mask_len: Size of the mask to protect (# of registers = 2 ** mask_len)
1271 *
1272 * Add the range of registers to the list of protected mode registers that will
1273 * cause an exception if the GPU accesses them. There are 16 available
1274 * protected mode registers. Index is used to specify which register to write
1275 * to - the intent is to call this function multiple times with the same index
1276 * pointer for each range and the registers will be magically programmed in
1277 * incremental fashion
1278 */
1279static inline void adreno_set_protected_registers(
1280 struct adreno_device *adreno_dev, unsigned int *index,
1281 unsigned int reg, int mask_len)
1282{
1283 unsigned int val;
1284 unsigned int base =
1285 adreno_getreg(adreno_dev, ADRENO_REG_CP_PROTECT_REG_0);
1286 unsigned int offset = *index;
1287 unsigned int max_slots = adreno_dev->gpucore->num_protected_regs ?
1288 adreno_dev->gpucore->num_protected_regs : 16;
1289
1290 /* Do we have a free slot? */
1291 if (WARN(*index >= max_slots, "Protected register slots full: %d/%d\n",
1292 *index, max_slots))
1293 return;
1294
1295 /*
1296 * On A4XX targets with more than 16 protected mode registers
1297 * the upper registers are not contiguous with the lower 16
1298 * registers so we have to adjust the base and offset accordingly
1299 */
1300
1301 if (adreno_is_a4xx(adreno_dev) && *index >= 0x10) {
1302 base = A4XX_CP_PROTECT_REG_10;
1303 offset = *index - 0x10;
1304 }
1305
1306 val = 0x60000000 | ((mask_len & 0x1F) << 24) | ((reg << 2) & 0xFFFFF);
1307
1308 kgsl_regwrite(KGSL_DEVICE(adreno_dev), base + offset, val);
1309 *index = *index + 1;
1310}
1311
1312#ifdef CONFIG_DEBUG_FS
1313void adreno_debugfs_init(struct adreno_device *adreno_dev);
1314void adreno_context_debugfs_init(struct adreno_device *adreno_dev,
1315 struct adreno_context *ctx);
1316#else
1317static inline void adreno_debugfs_init(struct adreno_device *adreno_dev) { }
1318static inline void adreno_context_debugfs_init(struct adreno_device *device,
1319 struct adreno_context *context)
1320 { }
1321#endif
1322
1323/**
1324 * adreno_compare_pm4_version() - Compare the PM4 microcode version
1325 * @adreno_dev: Pointer to the adreno_device struct
1326 * @version: Version number to compare again
1327 *
1328 * Compare the current version against the specified version and return -1 if
1329 * the current code is older, 0 if equal or 1 if newer.
1330 */
1331static inline int adreno_compare_pm4_version(struct adreno_device *adreno_dev,
1332 unsigned int version)
1333{
1334 if (adreno_dev->pm4_fw_version == version)
1335 return 0;
1336
1337 return (adreno_dev->pm4_fw_version > version) ? 1 : -1;
1338}
1339
1340/**
1341 * adreno_compare_pfp_version() - Compare the PFP microcode version
1342 * @adreno_dev: Pointer to the adreno_device struct
1343 * @version: Version number to compare against
1344 *
1345 * Compare the current version against the specified version and return -1 if
1346 * the current code is older, 0 if equal or 1 if newer.
1347 */
1348static inline int adreno_compare_pfp_version(struct adreno_device *adreno_dev,
1349 unsigned int version)
1350{
1351 if (adreno_dev->pfp_fw_version == version)
1352 return 0;
1353
1354 return (adreno_dev->pfp_fw_version > version) ? 1 : -1;
1355}
1356
1357/*
1358 * adreno_bootstrap_ucode() - Checks if Ucode bootstrapping is supported
1359 * @adreno_dev: Pointer to the the adreno device
1360 */
1361static inline int adreno_bootstrap_ucode(struct adreno_device *adreno_dev)
1362{
1363 return (ADRENO_FEATURE(adreno_dev, ADRENO_USE_BOOTSTRAP) &&
1364 adreno_compare_pfp_version(adreno_dev,
1365 adreno_dev->gpucore->pfp_bstrp_ver) >= 0) ? 1 : 0;
1366}
1367
1368/**
1369 * adreno_in_preempt_state() - Check if preemption state is equal to given state
1370 * @adreno_dev: Device whose preemption state is checked
1371 * @state: State to compare against
1372 */
1373static inline bool adreno_in_preempt_state(struct adreno_device *adreno_dev,
1374 enum adreno_preempt_states state)
1375{
1376 return atomic_read(&adreno_dev->preempt.state) == state;
1377}
1378/**
1379 * adreno_set_preempt_state() - Set the specified preemption state
1380 * @adreno_dev: Device to change preemption state
1381 * @state: State to set
1382 */
1383static inline void adreno_set_preempt_state(struct adreno_device *adreno_dev,
1384 enum adreno_preempt_states state)
1385{
1386 /*
1387 * atomic_set doesn't use barriers, so we need to do it ourselves. One
1388 * before...
1389 */
1390 smp_wmb();
1391 atomic_set(&adreno_dev->preempt.state, state);
1392
1393 /* ... and one after */
1394 smp_wmb();
1395}
1396
1397static inline bool adreno_is_preemption_enabled(
1398 struct adreno_device *adreno_dev)
1399{
1400 return test_bit(ADRENO_DEVICE_PREEMPTION, &adreno_dev->priv);
1401}
1402/**
1403 * adreno_ctx_get_rb() - Return the ringbuffer that a context should
1404 * use based on priority
1405 * @adreno_dev: The adreno device that context is using
1406 * @drawctxt: The context pointer
1407 */
1408static inline struct adreno_ringbuffer *adreno_ctx_get_rb(
1409 struct adreno_device *adreno_dev,
1410 struct adreno_context *drawctxt)
1411{
1412 struct kgsl_context *context;
1413 int level;
1414
1415 if (!drawctxt)
1416 return NULL;
1417
1418 context = &(drawctxt->base);
1419
1420 /*
1421 * If preemption is disabled then everybody needs to go on the same
1422 * ringbuffer
1423 */
1424
1425 if (!adreno_is_preemption_enabled(adreno_dev))
1426 return &(adreno_dev->ringbuffers[0]);
1427
1428 /*
1429 * Math to convert the priority field in context structure to an RB ID.
1430 * Divide up the context priority based on number of ringbuffer levels.
1431 */
1432 level = context->priority / adreno_dev->num_ringbuffers;
1433 if (level < adreno_dev->num_ringbuffers)
1434 return &(adreno_dev->ringbuffers[level]);
1435 else
1436 return &(adreno_dev->ringbuffers[
1437 adreno_dev->num_ringbuffers - 1]);
1438}
1439
1440/*
1441 * adreno_compare_prio_level() - Compares 2 priority levels based on enum values
1442 * @p1: First priority level
1443 * @p2: Second priority level
1444 *
1445 * Returns greater than 0 if p1 is higher priority, 0 if levels are equal else
1446 * less than 0
1447 */
1448static inline int adreno_compare_prio_level(int p1, int p2)
1449{
1450 return p2 - p1;
1451}
1452
1453void adreno_readreg64(struct adreno_device *adreno_dev,
1454 enum adreno_regs lo, enum adreno_regs hi, uint64_t *val);
1455
1456void adreno_writereg64(struct adreno_device *adreno_dev,
1457 enum adreno_regs lo, enum adreno_regs hi, uint64_t val);
1458
1459unsigned int adreno_get_rptr(struct adreno_ringbuffer *rb);
1460
1461static inline bool adreno_rb_empty(struct adreno_ringbuffer *rb)
1462{
1463 return (adreno_get_rptr(rb) == rb->wptr);
1464}
1465
1466static inline bool adreno_soft_fault_detect(struct adreno_device *adreno_dev)
1467{
1468 return adreno_dev->fast_hang_detect &&
1469 !test_bit(ADRENO_DEVICE_ISDB_ENABLED, &adreno_dev->priv);
1470}
1471
1472static inline bool adreno_long_ib_detect(struct adreno_device *adreno_dev)
1473{
1474 return adreno_dev->long_ib_detect &&
1475 !test_bit(ADRENO_DEVICE_ISDB_ENABLED, &adreno_dev->priv);
1476}
1477
1478/*
1479 * adreno_support_64bit() - Check the feature flag only if it is in
1480 * 64bit kernel otherwise return false
1481 * adreno_dev: The adreno device
1482 */
1483#if BITS_PER_LONG == 64
1484static inline bool adreno_support_64bit(struct adreno_device *adreno_dev)
1485{
1486 return ADRENO_FEATURE(adreno_dev, ADRENO_64BIT);
1487}
1488#else
1489static inline bool adreno_support_64bit(struct adreno_device *adreno_dev)
1490{
1491 return false;
1492}
1493#endif /*BITS_PER_LONG*/
1494
1495static inline void adreno_ringbuffer_set_global(
1496 struct adreno_device *adreno_dev, int name)
1497{
1498 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
1499
1500 kgsl_sharedmem_writel(device,
1501 &adreno_dev->ringbuffers[0].pagetable_desc,
1502 PT_INFO_OFFSET(current_global_ptname), name);
1503}
1504
1505static inline void adreno_ringbuffer_set_pagetable(struct adreno_ringbuffer *rb,
1506 struct kgsl_pagetable *pt)
1507{
1508 struct adreno_device *adreno_dev = ADRENO_RB_DEVICE(rb);
1509 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
1510 unsigned long flags;
1511
1512 spin_lock_irqsave(&rb->preempt_lock, flags);
1513
1514 kgsl_sharedmem_writel(device, &rb->pagetable_desc,
1515 PT_INFO_OFFSET(current_rb_ptname), pt->name);
1516
1517 kgsl_sharedmem_writeq(device, &rb->pagetable_desc,
1518 PT_INFO_OFFSET(ttbr0), kgsl_mmu_pagetable_get_ttbr0(pt));
1519
1520 kgsl_sharedmem_writel(device, &rb->pagetable_desc,
1521 PT_INFO_OFFSET(contextidr),
1522 kgsl_mmu_pagetable_get_contextidr(pt));
1523
1524 spin_unlock_irqrestore(&rb->preempt_lock, flags);
1525}
1526
1527static inline unsigned int counter_delta(struct kgsl_device *device,
1528 unsigned int reg, unsigned int *counter)
1529{
1530 unsigned int val;
1531 unsigned int ret = 0;
1532
1533 /* Read the value */
1534 kgsl_regread(device, reg, &val);
1535
1536 /* Return 0 for the first read */
1537 if (*counter != 0) {
1538 if (val < *counter)
1539 ret = (0xFFFFFFFF - *counter) + val;
1540 else
1541 ret = val - *counter;
1542 }
1543
1544 *counter = val;
1545 return ret;
1546}
1547#endif /*__ADRENO_H */