xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu.h (revision 5b0764a53235d3c77e1ed674e836e1b3fe1a4b1f)
1 /*
2  * Copyright 2008 Advanced Micro Devices, Inc.
3  * Copyright 2008 Red Hat Inc.
4  * Copyright 2009 Jerome Glisse.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Authors: Dave Airlie
25  *          Alex Deucher
26  *          Jerome Glisse
27  */
28 #ifndef __AMDGPU_H__
29 #define __AMDGPU_H__
30 
31 #ifdef pr_fmt
32 #undef pr_fmt
33 #endif
34 
35 #define pr_fmt(fmt) "amdgpu: " fmt
36 
37 #include "amdgpu_ctx.h"
38 
39 #include <linux/atomic.h>
40 #include <linux/wait.h>
41 #include <linux/list.h>
42 #include <linux/kref.h>
43 #include <linux/rbtree.h>
44 #include <linux/hashtable.h>
45 #include <linux/dma-fence.h>
46 #include <linux/pci.h>
47 #include <linux/xarray.h>
48 
49 #include <drm/ttm/ttm_bo.h>
50 #include <drm/ttm/ttm_placement.h>
51 
52 #include <drm/amdgpu_drm.h>
53 #include <drm/drm_gem.h>
54 #include <drm/drm_ioctl.h>
55 
56 #include <kgd_kfd_interface.h>
57 #include "dm_pp_interface.h"
58 #include "kgd_pp_interface.h"
59 
60 #include "amd_shared.h"
61 #include "amdgpu_utils.h"
62 #include "amdgpu_mode.h"
63 #include "amdgpu_ih.h"
64 #include "amdgpu_irq.h"
65 #include "amdgpu_ucode.h"
66 #include "amdgpu_ttm.h"
67 #include "amdgpu_psp.h"
68 #include "amdgpu_gds.h"
69 #include "amdgpu_sync.h"
70 #include "amdgpu_ring.h"
71 #include "amdgpu_vm.h"
72 #include "amdgpu_dpm.h"
73 #include "amdgpu_acp.h"
74 #include "amdgpu_uvd.h"
75 #include "amdgpu_vce.h"
76 #include "amdgpu_vcn.h"
77 #include "amdgpu_jpeg.h"
78 #include "amdgpu_vpe.h"
79 #include "amdgpu_umsch_mm.h"
80 #include "amdgpu_gmc.h"
81 #include "amdgpu_gfx.h"
82 #include "amdgpu_sdma_types.h"
83 #include "amdgpu_lsdma.h"
84 #include "amdgpu_nbio.h"
85 #include "amdgpu_reg_access.h"
86 #include "amdgpu_hdp.h"
87 #include "amdgpu_dm.h"
88 #include "amdgpu_virt.h"
89 #include "amdgpu_csa.h"
90 #include "amdgpu_mes_ctx.h"
91 #include "amdgpu_gart.h"
92 #include "amdgpu_debugfs.h"
93 #include "amdgpu_job.h"
94 #include "amdgpu_bo_list.h"
95 #include "amdgpu_gem.h"
96 #include "amdgpu_doorbell.h"
97 #include "amdgpu_amdkfd.h"
98 #include "amdgpu_discovery.h"
99 #include "amdgpu_umc.h"
100 #include "amdgpu_mmhub.h"
101 #include "amdgpu_gfxhub.h"
102 #include "amdgpu_df.h"
103 #include "amdgpu_smuio.h"
104 #include "amdgpu_fdinfo.h"
105 #include "amdgpu_mca.h"
106 #include "amdgpu_ras.h"
107 #include "amdgpu_lockdep.h"
108 #include "amdgpu_cper.h"
109 #include "amdgpu_xcp.h"
110 #include "amdgpu_seq64.h"
111 #include "amdgpu_reg_state.h"
112 #include "amdgpu_userq.h"
113 #include "amdgpu_eviction_fence.h"
114 #include "amdgpu_wb.h"
115 #include "amdgpu_ip.h"
116 #include "amdgpu_mes.h"
117 #include "amdgpu_sa.h"
118 #include "amdgpu_acpi.h"
119 #include "amdgpu_ualink.h"
120 #if defined(CONFIG_DRM_AMD_ISP)
121 #include "amdgpu_isp.h"
122 #endif
123 
124 #define MAX_GPU_INSTANCE		64
125 
126 #define GFX_SLICE_PERIOD_MS		250
127 
128 struct amdgpu_gpu_instance {
129 	struct amdgpu_device		*adev;
130 	int				mgpu_fan_enabled;
131 };
132 
133 struct amdgpu_mgpu_info {
134 	struct amdgpu_gpu_instance	gpu_ins[MAX_GPU_INSTANCE];
135 	struct mutex			mutex;
136 	uint32_t			num_gpu;
137 	uint32_t			num_dgpu;
138 	uint32_t			num_apu;
139 };
140 
141 struct amdgpu_hwip_reg_entry {
142 	u32		hwip;
143 	u32		inst;
144 	u32		seg;
145 	u32		reg_offset;
146 	const char	*reg_name;
147 };
148 
149 struct amdgpu_watchdog_timer {
150 	bool timeout_fatal_disable;
151 	uint32_t period; /* maxCycles = (1 << period), the number of cycles before a timeout */
152 };
153 
154 #define AMDGPU_MAX_TIMEOUT_PARAM_LENGTH	256
155 
156 /*
157  * Modules parameters.
158  */
159 extern int amdgpu_modeset;
160 extern int amdgpu_iommu_perfopt;
161 extern unsigned int amdgpu_vram_limit;
162 extern int amdgpu_vis_vram_limit;
163 extern int amdgpu_gart_size;
164 extern int amdgpu_gtt_size;
165 extern int amdgpu_moverate;
166 extern int amdgpu_audio;
167 extern int amdgpu_disp_priority;
168 extern int amdgpu_hw_i2c;
169 extern int amdgpu_pcie_gen2;
170 extern int amdgpu_msi;
171 extern char amdgpu_lockup_timeout[AMDGPU_MAX_TIMEOUT_PARAM_LENGTH];
172 extern int amdgpu_dpm;
173 extern int amdgpu_fw_load_type;
174 extern int amdgpu_aspm;
175 extern int amdgpu_runtime_pm;
176 extern uint amdgpu_ip_block_mask;
177 extern int amdgpu_bapm;
178 extern int amdgpu_deep_color;
179 extern int amdgpu_vm_size;
180 extern int amdgpu_vm_block_size;
181 extern int amdgpu_vm_fragment_size;
182 extern int amdgpu_vm_fault_stop;
183 extern int amdgpu_vm_debug;
184 extern int amdgpu_vm_update_mode;
185 extern int amdgpu_exp_hw_support;
186 extern int amdgpu_dc;
187 extern int amdgpu_sched_jobs;
188 extern int amdgpu_sched_hw_submission;
189 extern uint amdgpu_pcie_gen_cap;
190 extern uint amdgpu_pcie_lane_cap;
191 extern u64 amdgpu_cg_mask;
192 extern uint amdgpu_pg_mask;
193 extern uint amdgpu_sdma_phase_quantum;
194 extern char *amdgpu_disable_cu;
195 extern char *amdgpu_virtual_display;
196 extern uint amdgpu_pp_feature_mask;
197 extern uint amdgpu_force_long_training;
198 extern int amdgpu_lbpw;
199 extern int amdgpu_compute_multipipe;
200 extern int amdgpu_gpu_recovery;
201 extern int amdgpu_emu_mode;
202 extern uint amdgpu_smu_memory_pool_size;
203 extern int amdgpu_smu_pptable_id;
204 extern uint amdgpu_dc_feature_mask;
205 extern uint amdgpu_freesync_vid_mode;
206 extern uint amdgpu_dc_debug_mask;
207 extern uint amdgpu_dc_visual_confirm;
208 extern int amdgpu_dm_abm_level;
209 extern int amdgpu_backlight;
210 extern int amdgpu_damage_clips;
211 extern struct amdgpu_mgpu_info mgpu_info;
212 extern int amdgpu_ras_enable;
213 extern u64 amdgpu_ras_mask;
214 extern int amdgpu_bad_page_threshold;
215 extern bool amdgpu_ignore_bad_page_threshold;
216 extern struct amdgpu_watchdog_timer amdgpu_watchdog_timer;
217 extern int amdgpu_async_gfx_ring;
218 extern int amdgpu_mcbp;
219 extern int amdgpu_discovery;
220 extern int amdgpu_mes_log_enable;
221 extern int amdgpu_mes_dbgext_buffer_size;
222 extern int amdgpu_mes_dbgext_options;
223 extern int amdgpu_uni_mes;
224 extern int amdgpu_noretry;
225 extern int amdgpu_force_asic_type;
226 extern int amdgpu_smartshift_bias;
227 extern int amdgpu_use_xgmi_p2p;
228 extern int amdgpu_mtype_local;
229 extern int amdgpu_mtype_remote;
230 extern int amdgpu_enforce_isolation;
231 extern uint amdgpu_debug_mask;
232 #ifdef CONFIG_HSA_AMD
233 extern int sched_policy;
234 extern bool debug_evictions;
235 extern bool no_system_mem_limit;
236 extern int halt_if_hws_hang;
237 extern uint amdgpu_svm_default_granularity;
238 #else
239 static const int __maybe_unused sched_policy = KFD_SCHED_POLICY_HWS;
240 static const bool __maybe_unused debug_evictions; /* = false */
241 static const bool __maybe_unused no_system_mem_limit;
242 static const int __maybe_unused halt_if_hws_hang;
243 #endif
244 #ifdef CONFIG_HSA_AMD_P2P
245 extern bool pcie_p2p;
246 #endif
247 
248 extern int amdgpu_tmz;
249 extern int amdgpu_reset_method;
250 
251 #ifdef CONFIG_DRM_AMDGPU_SI
252 extern int amdgpu_si_support;
253 #endif
254 #ifdef CONFIG_DRM_AMDGPU_CIK
255 extern int amdgpu_cik_support;
256 #endif
257 extern int amdgpu_num_kcq;
258 
259 #define AMDGPU_VCNFW_LOG_SIZE (32 * 1024)
260 #define AMDGPU_UMSCHFW_LOG_SIZE (32 * 1024)
261 extern int amdgpu_vcnfw_log;
262 extern int amdgpu_sg_display;
263 extern int amdgpu_umsch_mm;
264 extern int amdgpu_seamless;
265 extern int amdgpu_umsch_mm_fwlog;
266 
267 extern int amdgpu_user_partt_mode;
268 extern int amdgpu_agp;
269 extern int amdgpu_rebar;
270 
271 extern int amdgpu_wbrf;
272 extern int amdgpu_user_queue;
273 extern int amdgpu_ptl;
274 
275 extern uint amdgpu_hdmi_hpd_debounce_delay_ms;
276 
277 #define AMDGPU_SG_THRESHOLD			(256*1024*1024)
278 #define AMDGPU_WAIT_IDLE_TIMEOUT_IN_MS	        3000
279 #define AMDGPU_MAX_USEC_TIMEOUT			100000	/* 100 ms */
280 #define AMDGPU_FENCE_JIFFIES_TIMEOUT		(HZ / 2)
281 #define AMDGPU_DEBUGFS_MAX_COMPONENTS		32
282 #define AMDGPUFB_CONN_LIMIT			4
283 #define AMDGPU_BIOS_NUM_SCRATCH			16
284 
285 #define AMDGPU_VBIOS_VGA_ALLOCATION		(9 * 1024 * 1024) /* reserve 8MB for vga emulator and 1 MB for FB */
286 
287 /* hard reset data */
288 #define AMDGPU_ASIC_RESET_DATA                  0x39d5e86b
289 
290 /* reset flags */
291 #define AMDGPU_RESET_GFX			(1 << 0)
292 #define AMDGPU_RESET_COMPUTE			(1 << 1)
293 #define AMDGPU_RESET_DMA			(1 << 2)
294 #define AMDGPU_RESET_CP				(1 << 3)
295 #define AMDGPU_RESET_GRBM			(1 << 4)
296 #define AMDGPU_RESET_DMA1			(1 << 5)
297 #define AMDGPU_RESET_RLC			(1 << 6)
298 #define AMDGPU_RESET_SEM			(1 << 7)
299 #define AMDGPU_RESET_IH				(1 << 8)
300 #define AMDGPU_RESET_VMC			(1 << 9)
301 #define AMDGPU_RESET_MC				(1 << 10)
302 #define AMDGPU_RESET_DISPLAY			(1 << 11)
303 #define AMDGPU_RESET_UVD			(1 << 12)
304 #define AMDGPU_RESET_VCE			(1 << 13)
305 #define AMDGPU_RESET_VCE1			(1 << 14)
306 
307 /* reset mask */
308 #define AMDGPU_RESET_TYPE_FULL (1 << 0) /* full adapter reset, mode1/mode2/BACO/etc. */
309 #define AMDGPU_RESET_TYPE_SOFT_RECOVERY (1 << 1) /* soft recovery, eg. kill shaders */
310 #define AMDGPU_RESET_TYPE_PER_QUEUE (1 << 2) /* per queue */
311 #define AMDGPU_RESET_TYPE_PER_PIPE (1 << 3) /* per pipe */
312 #define AMDGPU_RESET_TYPE_IP_BLOCK_SOFT_RESET (1 << 4) /* soft-resets an IP block */
313 
314 /* max cursor sizes (in pixels) */
315 #define CIK_CURSOR_WIDTH 128
316 #define CIK_CURSOR_HEIGHT 128
317 
318 /* smart shift bias level limits */
319 #define AMDGPU_SMARTSHIFT_MAX_BIAS (100)
320 #define AMDGPU_SMARTSHIFT_MIN_BIAS (-100)
321 
322 /* Extra time delay(in ms) to eliminate the influence of temperature momentary fluctuation */
323 #define AMDGPU_SWCTF_EXTRA_DELAY		50
324 
325 struct amdgpu_xcp_mgr;
326 struct amdgpu_device;
327 struct amdgpu_irq_src;
328 struct amdgpu_fpriv;
329 struct amdgpu_bo_va_mapping;
330 struct kfd_vm_fault_info;
331 struct amdgpu_hive_info;
332 struct amdgpu_reset_context;
333 struct amdgpu_reset_control;
334 struct amdgpu_coredump_info;
335 struct amdgpu_video_codecs;
336 
337 enum amdgpu_cp_irq {
338 	AMDGPU_CP_IRQ_GFX_ME0_PIPE0_EOP = 0,
339 	AMDGPU_CP_IRQ_GFX_ME0_PIPE1_EOP,
340 	AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE0_EOP,
341 	AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE1_EOP,
342 	AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE2_EOP,
343 	AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE3_EOP,
344 	AMDGPU_CP_IRQ_COMPUTE_MEC2_PIPE0_EOP,
345 	AMDGPU_CP_IRQ_COMPUTE_MEC2_PIPE1_EOP,
346 	AMDGPU_CP_IRQ_COMPUTE_MEC2_PIPE2_EOP,
347 	AMDGPU_CP_IRQ_COMPUTE_MEC2_PIPE3_EOP,
348 
349 	AMDGPU_CP_IRQ_LAST
350 };
351 
352 enum amdgpu_thermal_irq {
353 	AMDGPU_THERMAL_IRQ_LOW_TO_HIGH = 0,
354 	AMDGPU_THERMAL_IRQ_HIGH_TO_LOW,
355 
356 	AMDGPU_THERMAL_IRQ_LAST
357 };
358 
359 enum amdgpu_kiq_irq {
360 	AMDGPU_CP_KIQ_IRQ_DRIVER0 = 0,
361 	AMDGPU_CP_KIQ_IRQ_LAST
362 };
363 #define MAX_KIQ_REG_WAIT       5000 /* in usecs, 5ms */
364 #define MAX_KIQ_REG_BAILOUT_INTERVAL   5 /* in msecs, 5ms */
365 #define MAX_KIQ_REG_TRY 1000
366 
367 /*
368  * BIOS.
369  */
370 bool amdgpu_get_bios(struct amdgpu_device *adev);
371 bool amdgpu_read_bios(struct amdgpu_device *adev);
372 bool amdgpu_soc15_read_bios_from_rom(struct amdgpu_device *adev,
373 				     u8 *bios, u32 length_bytes);
374 void amdgpu_bios_release(struct amdgpu_device *adev);
375 /*
376  * Clocks
377  */
378 
379 #define AMDGPU_MAX_PPLL 3
380 
381 struct amdgpu_clock {
382 	struct amdgpu_pll ppll[AMDGPU_MAX_PPLL];
383 	struct amdgpu_pll spll;
384 	struct amdgpu_pll mpll;
385 	/* 10 Khz units */
386 	uint32_t default_mclk;
387 	uint32_t default_sclk;
388 	uint32_t default_dispclk;
389 	uint32_t dp_extclk;
390 	uint32_t max_pixel_clock;
391 };
392 
393 /*
394  * IRQS.
395  */
396 
397 struct amdgpu_flip_work {
398 	struct delayed_work		flip_work;
399 	struct work_struct		unpin_work;
400 	struct amdgpu_device		*adev;
401 	int				crtc_id;
402 	u32				target_vblank;
403 	uint64_t			base;
404 	struct drm_pending_vblank_event *event;
405 	struct amdgpu_bo		*old_abo;
406 	unsigned			shared_count;
407 	struct dma_fence		**shared;
408 	struct dma_fence_cb		cb;
409 	bool				async;
410 };
411 
412 /*
413  * file private structure
414  */
415 
416 struct amdgpu_fpriv {
417 	struct amdgpu_vm	vm;
418 	struct amdgpu_bo_va	*prt_va;
419 	struct amdgpu_bo_va	*csa_va;
420 	struct amdgpu_bo_va	*seq64_va;
421 	struct xarray		bo_list_handles;
422 	struct amdgpu_ctx_mgr	ctx_mgr;
423 	struct amdgpu_userq_mgr	userq_mgr;
424 
425 	/* Eviction fence infra */
426 	struct amdgpu_eviction_fence_mgr evf_mgr;
427 
428 	/** GPU partition selection */
429 	uint32_t		xcp_id;
430 };
431 
432 int amdgpu_file_to_fpriv(struct file *filp, struct amdgpu_fpriv **fpriv);
433 
434 /*
435  * Benchmarking
436  */
437 int amdgpu_benchmark(struct amdgpu_device *adev, int test_number);
438 
439 /*
440  * ASIC specific functions.
441  */
442 struct amdgpu_asic_funcs {
443 	bool (*read_disabled_bios)(struct amdgpu_device *adev);
444 	bool (*read_bios_from_rom)(struct amdgpu_device *adev,
445 				   u8 *bios, u32 length_bytes);
446 	int (*read_register)(struct amdgpu_device *adev, u32 se_num,
447 			     u32 sh_num, u32 reg_offset, u32 *value);
448 	void (*set_vga_state)(struct amdgpu_device *adev, bool state);
449 	int (*reset)(struct amdgpu_device *adev);
450 	enum amd_reset_method (*reset_method)(struct amdgpu_device *adev);
451 	/* get the reference clock */
452 	u32 (*get_xclk)(struct amdgpu_device *adev);
453 	/* MM block clocks */
454 	int (*set_uvd_clocks)(struct amdgpu_device *adev, u32 vclk, u32 dclk);
455 	int (*set_vce_clocks)(struct amdgpu_device *adev, u32 evclk, u32 ecclk);
456 	/* static power management */
457 	int (*get_pcie_lanes)(struct amdgpu_device *adev);
458 	void (*set_pcie_lanes)(struct amdgpu_device *adev, int lanes);
459 	/* get config memsize register */
460 	u32 (*get_config_memsize)(struct amdgpu_device *adev);
461 	/* flush hdp write queue */
462 	void (*flush_hdp)(struct amdgpu_device *adev, struct amdgpu_ring *ring);
463 	/* invalidate hdp read cache */
464 	void (*invalidate_hdp)(struct amdgpu_device *adev,
465 			       struct amdgpu_ring *ring);
466 	/* initialize doorbell layout for specific asic*/
467 	void (*init_doorbell_index)(struct amdgpu_device *adev);
468 	/* PCIe bandwidth usage */
469 	void (*get_pcie_usage)(struct amdgpu_device *adev, uint64_t *count0,
470 			       uint64_t *count1);
471 	/* do we need to reset the asic at init time (e.g., kexec) */
472 	bool (*need_reset_on_init)(struct amdgpu_device *adev);
473 	/* PCIe replay counter */
474 	uint64_t (*get_pcie_replay_count)(struct amdgpu_device *adev);
475 	/* device supports BACO */
476 	int (*supports_baco)(struct amdgpu_device *adev);
477 	/* pre asic_init quirks */
478 	void (*pre_asic_init)(struct amdgpu_device *adev);
479 	/* enter/exit umd stable pstate */
480 	int (*update_umd_stable_pstate)(struct amdgpu_device *adev, bool enter);
481 	/* query video codecs */
482 	int (*query_video_codecs)(struct amdgpu_device *adev, bool encode,
483 				  const struct amdgpu_video_codecs **codecs);
484 	/* encode "> 32bits" smn addressing */
485 	u64 (*encode_ext_smn_addressing)(int ext_id);
486 
487 	ssize_t (*get_reg_state)(struct amdgpu_device *adev,
488 				 enum amdgpu_reg_state reg_state, void *buf,
489 				 size_t max_size);
490 	/* query FW reserved region (size/offset) from discovery */
491 	void (*get_fw_reserved_info)(struct amdgpu_device *adev,
492 				     u64 *reserve_size, u64 *offset);
493 };
494 
495 /*
496  * IOCTL.
497  */
498 int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
499 				struct drm_file *filp);
500 
501 int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
502 int amdgpu_cs_fence_to_handle_ioctl(struct drm_device *dev, void *data,
503 				    struct drm_file *filp);
504 int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
505 int amdgpu_cs_wait_fences_ioctl(struct drm_device *dev, void *data,
506 				struct drm_file *filp);
507 
508 /* VRAM scratch page for HDP bug, default vram page */
509 struct amdgpu_mem_scratch {
510 	struct amdgpu_bo		*robj;
511 	uint32_t			*ptr;
512 	u64				gpu_addr;
513 };
514 
515 /*
516  * CGS
517  */
518 struct cgs_device *amdgpu_cgs_create_device(struct amdgpu_device *adev);
519 void amdgpu_cgs_destroy_device(struct cgs_device *cgs_device);
520 
521 /*
522  * Core structure, functions and helpers.
523  */
524 struct amdgpu_mmio_remap {
525 	u32 reg_offset;
526 	resource_size_t bus_addr;
527 	struct amdgpu_bo *bo;
528 };
529 
530 enum amdgpu_uid_type {
531 	AMDGPU_UID_TYPE_XCD,
532 	AMDGPU_UID_TYPE_AID,
533 	AMDGPU_UID_TYPE_SOC,
534 	AMDGPU_UID_TYPE_MID,
535 	AMDGPU_UID_TYPE_MAX
536 };
537 
538 #define AMDGPU_UID_INST_MAX 8 /* max number of instances for each UID type */
539 
540 struct amdgpu_uid {
541 	uint64_t uid[AMDGPU_UID_TYPE_MAX][AMDGPU_UID_INST_MAX];
542 	struct amdgpu_device *adev;
543 };
544 
545 struct amd_powerplay {
546 	void *pp_handle;
547 	const struct amd_pm_funcs *pp_funcs;
548 };
549 
550 /* polaris10 kickers */
551 #define ASICID_IS_P20(did, rid)		(((did == 0x67DF) && \
552 					 ((rid == 0xE3) || \
553 					  (rid == 0xE4) || \
554 					  (rid == 0xE5) || \
555 					  (rid == 0xE7) || \
556 					  (rid == 0xEF))) || \
557 					 ((did == 0x6FDF) && \
558 					 ((rid == 0xE7) || \
559 					  (rid == 0xEF) || \
560 					  (rid == 0xFF))))
561 
562 #define ASICID_IS_P30(did, rid)		((did == 0x67DF) && \
563 					((rid == 0xE1) || \
564 					 (rid == 0xF7)))
565 
566 /* polaris11 kickers */
567 #define ASICID_IS_P21(did, rid)		(((did == 0x67EF) && \
568 					 ((rid == 0xE0) || \
569 					  (rid == 0xE5))) || \
570 					 ((did == 0x67FF) && \
571 					 ((rid == 0xCF) || \
572 					  (rid == 0xEF) || \
573 					  (rid == 0xFF))))
574 
575 #define ASICID_IS_P31(did, rid)		((did == 0x67EF) && \
576 					((rid == 0xE2)))
577 
578 /* polaris12 kickers */
579 #define ASICID_IS_P23(did, rid)		(((did == 0x6987) && \
580 					 ((rid == 0xC0) || \
581 					  (rid == 0xC1) || \
582 					  (rid == 0xC3) || \
583 					  (rid == 0xC7))) || \
584 					 ((did == 0x6981) && \
585 					 ((rid == 0x00) || \
586 					  (rid == 0x01) || \
587 					  (rid == 0x10))))
588 
589 struct amdgpu_pcie_reset_ctx {
590 	bool in_link_reset;
591 	bool occurs_dpc;
592 	bool audio_suspended;
593 	struct pci_dev *swus;
594 	struct pci_saved_state *swus_pcistate;
595 	struct pci_saved_state *swds_pcistate;
596 };
597 
598 /*
599  * Custom Init levels could be defined for different situations where a full
600  * initialization of all hardware blocks are not expected. Sample cases are
601  * custom init sequences after resume after S0i3/S3, reset on initialization,
602  * partial reset of blocks etc. Presently, this defines only two levels. Levels
603  * are described in corresponding struct definitions - amdgpu_init_default,
604  * amdgpu_init_minimal_xgmi.
605  */
606 enum amdgpu_init_lvl_id {
607 	AMDGPU_INIT_LEVEL_DEFAULT,
608 	AMDGPU_INIT_LEVEL_MINIMAL_XGMI,
609 	AMDGPU_INIT_LEVEL_RESET_RECOVERY,
610 };
611 
612 struct amdgpu_init_level {
613 	enum amdgpu_init_lvl_id level;
614 	uint32_t hwini_ip_block_mask;
615 };
616 
617 #define AMDGPU_RESET_MAGIC_NUM 64
618 #define AMDGPU_MAX_DF_PERFMONS 4
619 struct amdgpu_reset_domain;
620 struct amdgpu_fru_info;
621 
622 enum amdgpu_enforce_isolation_mode {
623 	AMDGPU_ENFORCE_ISOLATION_DISABLE = 0,
624 	AMDGPU_ENFORCE_ISOLATION_ENABLE = 1,
625 	AMDGPU_ENFORCE_ISOLATION_ENABLE_LEGACY = 2,
626 	AMDGPU_ENFORCE_ISOLATION_NO_CLEANER_SHADER = 3,
627 };
628 
629 struct amdgpu_device {
630 	struct device			*dev;
631 	struct pci_dev			*pdev;
632 	/* The two ends of the physical PCIe link outside the device. */
633 	struct pci_dev			*link_dev;
634 	struct pci_dev			*link_partner;
635 	struct drm_device		ddev;
636 
637 #ifdef CONFIG_DRM_AMD_ACP
638 	struct amdgpu_acp		acp;
639 #endif
640 	struct amdgpu_hive_info *hive;
641 	struct amdgpu_xcp_mgr *xcp_mgr;
642 	/* ASIC */
643 	enum amd_asic_type		asic_type;
644 	uint32_t			family;
645 	uint32_t			rev_id;
646 	uint32_t			external_rev_id;
647 	unsigned long			flags;
648 	unsigned long			apu_flags;
649 	int				usec_timeout;
650 	const struct amdgpu_asic_funcs	*asic_funcs;
651 	bool				shutdown;
652 	bool				need_swiotlb;
653 	bool				accel_working;
654 	struct notifier_block		acpi_nb;
655 	struct notifier_block		pm_nb;
656 	struct amdgpu_i2c_chan		*i2c_bus[AMDGPU_MAX_I2C_BUS];
657 	struct debugfs_blob_wrapper debugfs_vbios_blob;
658 	struct mutex			srbm_mutex;
659 	/* GRBM index mutex. Protects concurrent access to GRBM index */
660 	struct mutex                    grbm_idx_mutex;
661 	struct dev_pm_domain		vga_pm_domain;
662 	bool				have_disp_power_ref;
663 	bool                            have_atomics_support;
664 	bool				is_sw_smu;
665 
666 	/* BIOS */
667 	bool				is_atom_fw;
668 	uint8_t				*bios;
669 	uint32_t			bios_size;
670 	uint32_t			bios_scratch_reg_offset;
671 	uint32_t			bios_scratch[AMDGPU_BIOS_NUM_SCRATCH];
672 
673 	/* Register/doorbell mmio */
674 	resource_size_t			rmmio_base;
675 	resource_size_t			rmmio_size;
676 	void __iomem			*rmmio;
677 	/* protects concurrent MM_INDEX/DATA based register access */
678 	spinlock_t mmio_idx_lock;
679 	struct amdgpu_mmio_remap        rmmio_remap;
680 	/* Indirect register access blocks */
681 	struct amdgpu_reg_access reg;
682 	struct amdgpu_doorbell		doorbell;
683 
684 	/* clock/pll info */
685 	struct amdgpu_clock            clock;
686 
687 	/* MC */
688 	struct amdgpu_gmc		gmc;
689 	struct amdgpu_gart		gart;
690 	dma_addr_t			dummy_page_addr;
691 	struct amdgpu_vm_manager	vm_manager;
692 	struct amdgpu_vmhub             vmhub[AMDGPU_MAX_VMHUBS];
693 	DECLARE_BITMAP(vmhubs_mask, AMDGPU_MAX_VMHUBS);
694 
695 	/* memory management */
696 	struct amdgpu_mman		mman;
697 	struct amdgpu_mem_scratch	mem_scratch;
698 	struct amdgpu_wb		wb;
699 	atomic64_t			num_bytes_moved;
700 	atomic64_t			num_evictions;
701 	atomic64_t			num_vram_cpu_page_faults;
702 	atomic_t			gpu_reset_counter;
703 	atomic_t			vram_lost_counter;
704 
705 	/* data for buffer migration throttling */
706 	struct {
707 		spinlock_t		lock;
708 		s64			last_update_us;
709 		s64			accum_us; /* accumulated microseconds */
710 		s64			accum_us_vis; /* for visible VRAM */
711 		u32			log2_max_MBps;
712 	} mm_stats;
713 
714 	/* discovery*/
715 	struct amdgpu_discovery_info discovery;
716 
717 	/* display */
718 	bool				enable_virtual_display;
719 	struct amdgpu_vkms_output       *amdgpu_vkms_output;
720 	struct amdgpu_mode_info		mode_info;
721 	/* For pre-DCE11. DCE11 and later are in "struct amdgpu_device->dm" */
722 	struct delayed_work         hotplug_work;
723 	struct amdgpu_irq_src		crtc_irq;
724 	struct amdgpu_irq_src		vline0_irq;
725 	struct amdgpu_irq_src		vupdate_irq;
726 	struct amdgpu_irq_src		pageflip_irq;
727 	struct amdgpu_irq_src		hpd_irq;
728 	struct amdgpu_irq_src		dmub_trace_irq;
729 	struct amdgpu_irq_src		dmub_outbox_irq;
730 
731 	/* rings */
732 	u64				fence_context;
733 	unsigned			num_rings;
734 	struct amdgpu_ring		*rings[AMDGPU_MAX_RINGS];
735 	struct dma_fence __rcu		*gang_submit;
736 	bool				ib_pool_ready;
737 	struct amdgpu_sa_manager	ib_pools[AMDGPU_IB_POOL_MAX];
738 	struct amdgpu_sched		gpu_sched[AMDGPU_HW_IP_NUM][AMDGPU_RING_PRIO_MAX];
739 
740 	/* interrupts */
741 	struct amdgpu_irq		irq;
742 
743 	/* powerplay */
744 	struct amd_powerplay		powerplay;
745 	struct amdgpu_pm		pm;
746 	u64				cg_flags;
747 	u32				pg_flags;
748 
749 	/* nbio */
750 	struct amdgpu_nbio		nbio;
751 
752 	/* hdp */
753 	struct amdgpu_hdp		hdp;
754 
755 	/* smuio */
756 	struct amdgpu_smuio		smuio;
757 
758 	/* mmhub */
759 	struct amdgpu_mmhub		mmhub;
760 
761 	/* gfxhub */
762 	struct amdgpu_gfxhub		gfxhub;
763 
764 	/* gfx */
765 	struct amdgpu_gfx		gfx;
766 
767 	/* sdma */
768 	struct amdgpu_sdma		sdma;
769 
770 	/* lsdma */
771 	struct amdgpu_lsdma		lsdma;
772 
773 	/* uvd */
774 	struct amdgpu_uvd		uvd;
775 
776 	/* vce */
777 	struct amdgpu_vce		vce;
778 
779 	/* vcn */
780 	struct amdgpu_vcn		vcn;
781 
782 	/* jpeg */
783 	struct amdgpu_jpeg		jpeg;
784 
785 	/* vpe */
786 	struct amdgpu_vpe		vpe;
787 
788 	/* umsch */
789 	struct amdgpu_umsch_mm		umsch_mm;
790 	bool				enable_umsch_mm;
791 
792 	/* firmwares */
793 	struct amdgpu_firmware		firmware;
794 
795 	/* PSP */
796 	struct psp_context		psp;
797 
798 	/* GDS */
799 	struct amdgpu_gds		gds;
800 
801 	/* for userq and VM fences */
802 	struct amdgpu_seq64		seq64;
803 
804 	/* UMC */
805 	struct amdgpu_umc		umc;
806 
807 	/* display related functionality */
808 	struct amdgpu_display_manager dm;
809 
810 	/* UALink manager */
811 	struct amdgpu_ualink_mgr	ualink;
812 
813 #if defined(CONFIG_DRM_AMD_ISP)
814 	/* isp */
815 	struct amdgpu_isp		isp;
816 #endif
817 
818 	/* mes */
819 	bool                            enable_mes;
820 	bool                            enable_mes_kiq;
821 	bool                            enable_uni_mes;
822 	struct amdgpu_mes               mes;
823 	struct amdgpu_mqd               mqds[AMDGPU_HW_IP_NUM];
824 	const struct amdgpu_userq_funcs *userq_funcs[AMDGPU_HW_IP_NUM];
825 
826 	/**
827 	 * @userq_doorbell_xa: Global user queue map (doorbell index → queue)
828 	 * Key: doorbell_index (unique global identifier for the queue)
829 	 * Value: struct amdgpu_usermode_queue
830 	 */
831 	struct xarray userq_doorbell_xa;
832 
833 	/* df */
834 	struct amdgpu_df                df;
835 
836 	/* MCA */
837 	struct amdgpu_mca               mca;
838 
839 	/* CPER */
840 	struct amdgpu_cper		cper;
841 
842 	struct amdgpu_ip_block          ip_blocks[AMDGPU_MAX_IP_NUM];
843 	uint32_t		        harvest_ip_mask;
844 	int				num_ip_blocks;
845 	struct mutex	mn_lock;
846 	DECLARE_HASHTABLE(mn_hash, 7);
847 
848 	/* tracking pinned memory */
849 	atomic64_t vram_pin_size;
850 	atomic64_t visible_pin_size;
851 	atomic64_t gart_pin_size;
852 
853 	/* soc15 register offset based on ip, instance and  segment */
854 	uint32_t		*reg_offset[MAX_HWIP][HWIP_MAX_INSTANCE];
855 	struct amdgpu_ip_map_info	ip_map;
856 
857 	/* delayed work_func for deferring clockgating during resume */
858 	struct delayed_work     delayed_init_work;
859 
860 	struct amdgpu_virt	virt;
861 
862 	/* record hw reset is performed */
863 	bool has_hw_reset;
864 	u8				reset_magic[AMDGPU_RESET_MAGIC_NUM];
865 
866 	/* s3/s4 mask */
867 	bool                            in_suspend;
868 	bool				in_s3;
869 	bool				in_s4;
870 	bool				in_s0ix;
871 	suspend_state_t			last_suspend_state;
872 
873 	enum pp_mp1_state               mp1_state;
874 	struct amdgpu_doorbell_index doorbell_index;
875 
876 	struct mutex			notifier_lock;
877 
878 	int asic_reset_res;
879 	struct work_struct		xgmi_reset_work;
880 	struct list_head		reset_list;
881 
882 	long				gfx_timeout;
883 	long				sdma_timeout;
884 	long				video_timeout;
885 	long				compute_timeout;
886 	long				psp_timeout;
887 
888 	uint64_t			unique_id;
889 	uint8_t				unitid;
890 	uint64_t	df_perfmon_config_assign_mask[AMDGPU_MAX_DF_PERFMONS];
891 
892 	/* enable runtime pm on the device */
893 	bool                            in_runpm;
894 	bool                            has_pr3;
895 
896 	bool                            ucode_sysfs_en;
897 
898 	struct amdgpu_fru_info		*fru_info;
899 	atomic_t			throttling_logging_enabled;
900 	struct ratelimit_state		throttling_logging_rs;
901 	uint32_t                        ras_hw_enabled;
902 	uint32_t                        ras_enabled;
903 	bool                            ras_default_ecc_enabled;
904 
905 	bool                            no_hw_access;
906 	struct pci_saved_state          *pci_state;
907 	pci_channel_state_t		pci_channel_state;
908 
909 	struct amdgpu_pcie_reset_ctx	pcie_reset_ctx;
910 
911 	/* Track auto wait count on s_barrier settings */
912 	bool				barrier_has_auto_waitcnt;
913 
914 	struct amdgpu_reset_control     *reset_cntl;
915 	uint32_t                        ip_versions[MAX_HWIP][HWIP_MAX_INSTANCE];
916 
917 	bool				ram_is_direct_mapped;
918 
919 	struct list_head                ras_list;
920 
921 	struct amdgpu_reset_domain	*reset_domain;
922 
923 #ifdef CONFIG_DEV_COREDUMP
924 	struct amdgpu_coredump_info	*coredump;
925 	struct work_struct		coredump_work;
926 #endif
927 
928 	struct mutex			benchmark_mutex;
929 
930 	bool                            scpm_enabled;
931 	uint32_t                        scpm_status;
932 
933 	struct work_struct		reset_work;
934 
935 	bool                            dc_enabled;
936 	/* Mask of active clusters */
937 	uint32_t			aid_mask;
938 
939 	/* Debug */
940 	bool                            debug_vm;
941 	bool                            debug_largebar;
942 	bool                            debug_disable_soft_recovery;
943 	bool                            debug_use_vram_fw_buf;
944 	bool                            debug_exp_resets;
945 	bool                            debug_disable_gpu_ring_reset;
946 	bool                            debug_vm_userptr;
947 	bool                            debug_disable_ce_logs;
948 	bool                            debug_enable_ce_cs;
949 	bool                            debug_hibernation_thaw_resume_gpu;
950 	bool                            debug_disable_ip_block_soft_reset;
951 
952 	/* Protection for the following isolation structure */
953 	struct mutex                    enforce_isolation_mutex;
954 	enum amdgpu_enforce_isolation_mode	enforce_isolation[MAX_XCP];
955 	struct amdgpu_isolation {
956 		void			*owner;
957 		struct dma_fence	*spearhead;
958 		struct amdgpu_sync	active;
959 		struct amdgpu_sync	prev;
960 	} isolation[MAX_XCP];
961 
962 	struct amdgpu_init_level *init_lvl;
963 
964 	/* This flag is used to determine how VRAM allocations are handled for APUs
965 	 * in KFD: VRAM or GTT.
966 	 */
967 	bool                            apu_prefer_gtt;
968 
969 	bool                            userq_halt_for_enforce_isolation;
970 	struct amdgpu_uid *uid_info;
971 
972 	struct amdgpu_uma_carveout_info uma_info;
973 
974 	/* KFD
975 	 * Must be last --ends in a flexible-array member.
976 	 */
977 	struct amdgpu_kfd_dev		kfd;
978 };
979 
980 static inline uint32_t amdgpu_ip_version(const struct amdgpu_device *adev,
981 					 uint8_t ip, uint8_t inst)
982 {
983 	/* This considers only major/minor/rev and ignores
984 	 * subrevision/variant fields.
985 	 */
986 	return adev->ip_versions[ip][inst] & ~0xFFU;
987 }
988 
989 static inline uint32_t amdgpu_ip_version_full(const struct amdgpu_device *adev,
990 					      uint8_t ip, uint8_t inst)
991 {
992 	/* This returns full version - major/minor/rev/variant/subrevision */
993 	return adev->ip_versions[ip][inst];
994 }
995 
996 static inline struct amdgpu_device *drm_to_adev(struct drm_device *ddev)
997 {
998 	return container_of(ddev, struct amdgpu_device, ddev);
999 }
1000 
1001 static inline struct drm_device *adev_to_drm(struct amdgpu_device *adev)
1002 {
1003 	return &adev->ddev;
1004 }
1005 
1006 static inline struct amdgpu_device *amdgpu_ttm_adev(struct ttm_device *bdev)
1007 {
1008 	return container_of(bdev, struct amdgpu_device, mman.bdev);
1009 }
1010 
1011 static inline bool amdgpu_is_multi_aid(struct amdgpu_device *adev)
1012 {
1013 	return !!adev->aid_mask;
1014 }
1015 
1016 int amdgpu_device_init(struct amdgpu_device *adev,
1017 		       uint32_t flags);
1018 void amdgpu_device_fini_hw(struct amdgpu_device *adev);
1019 void amdgpu_device_fini_sw(struct amdgpu_device *adev);
1020 
1021 int amdgpu_gpu_wait_for_idle(struct amdgpu_device *adev);
1022 
1023 void amdgpu_device_mm_access(struct amdgpu_device *adev, loff_t pos,
1024 			     void *buf, size_t size, bool write);
1025 size_t amdgpu_device_aper_access(struct amdgpu_device *adev, loff_t pos,
1026 				 void *buf, size_t size, bool write);
1027 
1028 void amdgpu_device_vram_access(struct amdgpu_device *adev, loff_t pos,
1029 			       void *buf, size_t size, bool write);
1030 u32 amdgpu_device_get_rev_id(struct amdgpu_device *adev);
1031 bool amdgpu_device_asic_has_dc_support(struct pci_dev *pdev,
1032 				       enum amd_asic_type asic_type);
1033 bool amdgpu_device_has_dc_support(struct amdgpu_device *adev);
1034 
1035 void amdgpu_device_set_sriov_virtual_display(struct amdgpu_device *adev);
1036 
1037 int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
1038 				 struct amdgpu_reset_context *reset_context);
1039 
1040 int amdgpu_do_asic_reset(struct list_head *device_list_handle,
1041 			 struct amdgpu_reset_context *reset_context);
1042 
1043 int amdgpu_device_reinit_after_reset(struct amdgpu_reset_context *reset_context);
1044 
1045 int emu_soc_asic_init(struct amdgpu_device *adev);
1046 
1047 /*
1048  * Registers read & write functions.
1049  */
1050 #define AMDGPU_REGS_NO_KIQ    (1<<1)
1051 #define AMDGPU_REGS_RLC	(1<<2)
1052 
1053 #define RREG32_NO_KIQ(reg) amdgpu_device_rreg(adev, (reg), AMDGPU_REGS_NO_KIQ)
1054 #define WREG32_NO_KIQ(reg, v) amdgpu_device_wreg(adev, (reg), (v), AMDGPU_REGS_NO_KIQ)
1055 
1056 #define RREG32_KIQ(reg) amdgpu_kiq_rreg(adev, (reg), 0)
1057 #define WREG32_KIQ(reg, v) amdgpu_kiq_wreg(adev, (reg), (v), 0)
1058 
1059 #define RREG8(reg) amdgpu_mm_rreg8(adev, (reg))
1060 #define WREG8(reg, v) amdgpu_mm_wreg8(adev, (reg), (v))
1061 
1062 #define RREG32(reg) amdgpu_device_rreg(adev, (reg), 0)
1063 #define DREG32(reg) printk(KERN_INFO "REGISTER: " #reg " : 0x%08X\n", amdgpu_device_rreg(adev, (reg), 0))
1064 #define WREG32(reg, v) amdgpu_device_wreg(adev, (reg), (v), 0)
1065 #define REG_SET(FIELD, v) (((v) << FIELD##_SHIFT) & FIELD##_MASK)
1066 #define REG_GET(FIELD, v) (((v) << FIELD##_SHIFT) & FIELD##_MASK)
1067 #define RREG32_XCC(reg, inst) amdgpu_device_xcc_rreg(adev, (reg), 0, inst)
1068 #define WREG32_XCC(reg, v, inst) amdgpu_device_xcc_wreg(adev, (reg), (v), 0, inst)
1069 #define RREG32_PCIE(reg) amdgpu_reg_pcie_rd32(adev, (reg))
1070 #define WREG32_PCIE(reg, v) amdgpu_reg_pcie_wr32(adev, (reg), (v))
1071 #define RREG32_PCIE_PORT(reg) amdgpu_reg_pciep_rd32(adev, (reg))
1072 #define WREG32_PCIE_PORT(reg, v) amdgpu_reg_pciep_wr32(adev, (reg), (v))
1073 #define RREG32_PCIE_EXT(reg) amdgpu_reg_pcie_ext_rd32(adev, (reg))
1074 #define WREG32_PCIE_EXT(reg, v) amdgpu_reg_pcie_ext_wr32(adev, (reg), (v))
1075 #define RREG64_PCIE(reg) amdgpu_reg_pcie_rd64(adev, (reg))
1076 #define WREG64_PCIE(reg, v) amdgpu_reg_pcie_wr64(adev, (reg), (v))
1077 #define RREG64_PCIE_EXT(reg) amdgpu_reg_pcie_ext_rd64(adev, (reg))
1078 #define WREG64_PCIE_EXT(reg, v) amdgpu_reg_pcie_ext_wr64(adev, (reg), (v))
1079 #define RREG32_SMC(reg) amdgpu_reg_smc_rd32(adev, (reg))
1080 #define WREG32_SMC(reg, v) amdgpu_reg_smc_wr32(adev, (reg), (v))
1081 #define RREG32_UVD_CTX(reg) amdgpu_reg_uvd_ctx_rd32(adev, (reg))
1082 #define WREG32_UVD_CTX(reg, v) amdgpu_reg_uvd_ctx_wr32(adev, (reg), (v))
1083 #define RREG32_DIDT(reg) amdgpu_reg_didt_rd32(adev, (reg))
1084 #define WREG32_DIDT(reg, v) amdgpu_reg_didt_wr32(adev, (reg), (v))
1085 #define RREG32_GC_CAC(reg) amdgpu_reg_gc_cac_rd32(adev, (reg))
1086 #define WREG32_GC_CAC(reg, v) amdgpu_reg_gc_cac_wr32(adev, (reg), (v))
1087 #define RREG32_SE_CAC(reg) amdgpu_reg_se_cac_rd32(adev, (reg))
1088 #define WREG32_SE_CAC(reg, v) amdgpu_reg_se_cac_wr32(adev, (reg), (v))
1089 #define RREG32_AUDIO_ENDPT(block, reg) \
1090 	amdgpu_reg_audio_endpt_rd32(adev, (block), (reg))
1091 #define WREG32_AUDIO_ENDPT(block, reg, v) \
1092 	amdgpu_reg_audio_endpt_wr32(adev, (block), (reg), (v))
1093 #define WREG32_P(reg, val, mask)				\
1094 	do {							\
1095 		uint32_t tmp_ = RREG32(reg);			\
1096 		tmp_ &= (mask);					\
1097 		tmp_ |= ((val) & ~(mask));			\
1098 		WREG32(reg, tmp_);				\
1099 	} while (0)
1100 #define WREG32_AND(reg, and) WREG32_P(reg, 0, and)
1101 #define WREG32_OR(reg, or) WREG32_P(reg, or, ~(or))
1102 #define WREG32_PLL_P(reg, val, mask)				\
1103 	do {							\
1104 		uint32_t tmp_ = RREG32_PLL(reg);		\
1105 		tmp_ &= (mask);					\
1106 		tmp_ |= ((val) & ~(mask));			\
1107 		WREG32_PLL(reg, tmp_);				\
1108 	} while (0)
1109 
1110 #define WREG32_SMC_P(_Reg, _Val, _Mask)                         \
1111 	do {                                                    \
1112 		u32 tmp = RREG32_SMC(_Reg);                     \
1113 		tmp &= (_Mask);                                 \
1114 		tmp |= ((_Val) & ~(_Mask));                     \
1115 		WREG32_SMC(_Reg, tmp);                          \
1116 	} while (0)
1117 
1118 #define DREG32_SYS(sqf, adev, reg) seq_printf((sqf), #reg " : 0x%08X\n", amdgpu_device_rreg((adev), (reg), false))
1119 
1120 #define REG_FIELD_SHIFT(reg, field) reg##__##field##__SHIFT
1121 #define REG_FIELD_MASK(reg, field) reg##__##field##_MASK
1122 
1123 #define REG_SET_FIELD(orig_val, reg, field, field_val)			\
1124 	(((orig_val) & ~REG_FIELD_MASK(reg, field)) |			\
1125 	 (REG_FIELD_MASK(reg, field) & ((field_val) << REG_FIELD_SHIFT(reg, field))))
1126 
1127 #define REG_GET_FIELD(value, reg, field)				\
1128 	(((value) & REG_FIELD_MASK(reg, field)) >> REG_FIELD_SHIFT(reg, field))
1129 
1130 #define WREG32_FIELD(reg, field, val)	\
1131 	WREG32(mm##reg, (RREG32(mm##reg) & ~REG_FIELD_MASK(reg, field)) | (val) << REG_FIELD_SHIFT(reg, field))
1132 
1133 #define WREG32_FIELD_OFFSET(reg, offset, field, val)	\
1134 	WREG32(mm##reg + offset, (RREG32(mm##reg + offset) & ~REG_FIELD_MASK(reg, field)) | (val) << REG_FIELD_SHIFT(reg, field))
1135 
1136 #define AMDGPU_GET_REG_FIELD(x, h, l) (((x) & GENMASK_ULL(h, l)) >> (l))
1137 /*
1138  * BIOS helpers.
1139  */
1140 #define RBIOS8(i) (adev->bios[i])
1141 #define RBIOS16(i) (RBIOS8(i) | (RBIOS8((i)+1) << 8))
1142 #define RBIOS32(i) ((RBIOS16(i)) | (RBIOS16((i)+2) << 16))
1143 
1144 #include "amdgpu_reset.h"
1145 
1146 /*
1147  * ASICs macro.
1148  */
1149 #define amdgpu_asic_set_vga_state(adev, state) \
1150     ((adev)->asic_funcs->set_vga_state ? (adev)->asic_funcs->set_vga_state((adev), (state)) : 0)
1151 #define amdgpu_asic_reset(adev) (adev)->asic_funcs->reset((adev))
1152 #define amdgpu_asic_reset_method(adev) (adev)->asic_funcs->reset_method((adev))
1153 #define amdgpu_asic_get_xclk(adev) (adev)->asic_funcs->get_xclk((adev))
1154 #define amdgpu_asic_set_uvd_clocks(adev, v, d) (adev)->asic_funcs->set_uvd_clocks((adev), (v), (d))
1155 #define amdgpu_asic_set_vce_clocks(adev, ev, ec) (adev)->asic_funcs->set_vce_clocks((adev), (ev), (ec))
1156 #define amdgpu_get_pcie_lanes(adev) (adev)->asic_funcs->get_pcie_lanes((adev))
1157 #define amdgpu_set_pcie_lanes(adev, l) (adev)->asic_funcs->set_pcie_lanes((adev), (l))
1158 #define amdgpu_asic_get_gpu_clock_counter(adev) (adev)->asic_funcs->get_gpu_clock_counter((adev))
1159 #define amdgpu_asic_read_disabled_bios(adev) (adev)->asic_funcs->read_disabled_bios((adev))
1160 #define amdgpu_asic_read_bios_from_rom(adev, b, l) (adev)->asic_funcs->read_bios_from_rom((adev), (b), (l))
1161 #define amdgpu_asic_read_register(adev, se, sh, offset, v)((adev)->asic_funcs->read_register((adev), (se), (sh), (offset), (v)))
1162 #define amdgpu_asic_get_config_memsize(adev) (adev)->asic_funcs->get_config_memsize((adev))
1163 #define amdgpu_asic_init_doorbell_index(adev) (adev)->asic_funcs->init_doorbell_index((adev))
1164 #define amdgpu_asic_get_pcie_usage(adev, cnt0, cnt1) ((adev)->asic_funcs->get_pcie_usage((adev), (cnt0), (cnt1)))
1165 #define amdgpu_asic_need_reset_on_init(adev) (adev)->asic_funcs->need_reset_on_init((adev))
1166 #define amdgpu_asic_get_pcie_replay_count(adev) ((adev)->asic_funcs->get_pcie_replay_count((adev)))
1167 #define amdgpu_asic_supports_baco(adev) \
1168     ((adev)->asic_funcs->supports_baco ? (adev)->asic_funcs->supports_baco((adev)) : 0)
1169 #define amdgpu_asic_pre_asic_init(adev)                                      \
1170 	{                                                                    \
1171 		if ((adev)->asic_funcs && (adev)->asic_funcs->pre_asic_init) \
1172 			(adev)->asic_funcs->pre_asic_init((adev));           \
1173 	}
1174 #define amdgpu_asic_update_umd_stable_pstate(adev, enter) \
1175 	((adev)->asic_funcs->update_umd_stable_pstate ? (adev)->asic_funcs->update_umd_stable_pstate((adev), (enter)) : 0)
1176 #define amdgpu_asic_query_video_codecs(adev, e, c) (adev)->asic_funcs->query_video_codecs((adev), (e), (c))
1177 
1178 #define amdgpu_inc_vram_lost(adev) atomic_inc(&((adev)->vram_lost_counter))
1179 
1180 #define BIT_MASK_UPPER(i) ((i) >= BITS_PER_LONG ? 0 : ~0UL << (i))
1181 #define for_each_inst(i, inst_mask)        \
1182 	for (i = ffs(inst_mask); i-- != 0; \
1183 	     i = ffs(inst_mask & BIT_MASK_UPPER(i + 1)))
1184 
1185 /* Common functions */
1186 bool amdgpu_device_has_job_running(struct amdgpu_device *adev);
1187 bool amdgpu_device_should_recover_gpu(struct amdgpu_device *adev);
1188 int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
1189 			      struct amdgpu_job *job,
1190 			      struct amdgpu_reset_context *reset_context);
1191 void amdgpu_device_pci_config_reset(struct amdgpu_device *adev);
1192 int amdgpu_device_pci_reset(struct amdgpu_device *adev);
1193 bool amdgpu_device_need_post(struct amdgpu_device *adev);
1194 bool amdgpu_device_seamless_boot_supported(struct amdgpu_device *adev);
1195 bool amdgpu_device_should_use_aspm(struct amdgpu_device *adev);
1196 
1197 void amdgpu_cs_report_moved_bytes(struct amdgpu_device *adev, u64 num_bytes,
1198 				  u64 num_vis_bytes);
1199 int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev);
1200 void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
1201 					     const u32 *registers,
1202 					     const u32 array_size);
1203 
1204 int amdgpu_device_mode1_reset(struct amdgpu_device *adev);
1205 int amdgpu_device_link_reset(struct amdgpu_device *adev);
1206 bool amdgpu_device_supports_atpx(struct amdgpu_device *adev);
1207 bool amdgpu_device_supports_px(struct amdgpu_device *adev);
1208 bool amdgpu_device_supports_boco(struct amdgpu_device *adev);
1209 bool amdgpu_device_supports_smart_shift(struct amdgpu_device *adev);
1210 int amdgpu_device_supports_baco(struct amdgpu_device *adev);
1211 void amdgpu_device_detect_runtime_pm_mode(struct amdgpu_device *adev);
1212 bool amdgpu_device_is_peer_accessible(struct amdgpu_device *adev,
1213 				      struct amdgpu_device *peer_adev);
1214 int amdgpu_device_baco_enter(struct amdgpu_device *adev);
1215 int amdgpu_device_baco_exit(struct amdgpu_device *adev);
1216 
1217 void amdgpu_device_flush_hdp(struct amdgpu_device *adev,
1218 		struct amdgpu_ring *ring);
1219 void amdgpu_device_invalidate_hdp(struct amdgpu_device *adev,
1220 		struct amdgpu_ring *ring);
1221 
1222 void amdgpu_device_halt(struct amdgpu_device *adev);
1223 struct dma_fence *amdgpu_device_get_gang(struct amdgpu_device *adev);
1224 struct dma_fence *amdgpu_device_switch_gang(struct amdgpu_device *adev,
1225 					    struct dma_fence *gang);
1226 struct dma_fence *amdgpu_device_enforce_isolation(struct amdgpu_device *adev,
1227 						  struct amdgpu_ring *ring,
1228 						  struct amdgpu_job *job);
1229 bool amdgpu_device_has_display_hardware(struct amdgpu_device *adev);
1230 ssize_t amdgpu_get_soft_full_reset_mask(struct amdgpu_ring *ring);
1231 ssize_t amdgpu_show_reset_mask(char *buf, uint32_t supported_reset);
1232 void amdgpu_sdma_set_vm_pte_scheds(struct amdgpu_device *adev,
1233 				   const struct amdgpu_vm_pte_funcs *vm_pte_funcs);
1234 void amdgpu_sdma_set_buffer_funcs_scheds(struct amdgpu_device *adev,
1235 					 const struct amdgpu_buffer_funcs *buffer_funcs);
1236 
1237 /* atpx handler */
1238 #if defined(CONFIG_VGA_SWITCHEROO)
1239 void amdgpu_register_atpx_handler(void);
1240 void amdgpu_unregister_atpx_handler(void);
1241 bool amdgpu_has_atpx_dgpu_power_cntl(void);
1242 bool amdgpu_is_atpx_hybrid(void);
1243 bool amdgpu_has_atpx(void);
1244 #else
1245 static inline void amdgpu_register_atpx_handler(void) {}
1246 static inline void amdgpu_unregister_atpx_handler(void) {}
1247 static inline bool amdgpu_has_atpx_dgpu_power_cntl(void) { return false; }
1248 static inline bool amdgpu_is_atpx_hybrid(void) { return false; }
1249 static inline bool amdgpu_has_atpx(void) { return false; }
1250 #endif
1251 
1252 /*
1253  * KMS
1254  */
1255 extern const struct drm_ioctl_desc amdgpu_ioctls_kms[];
1256 extern const int amdgpu_max_kms_ioctl;
1257 
1258 int amdgpu_driver_load_kms(struct amdgpu_device *adev, unsigned long flags);
1259 void amdgpu_driver_unload_kms(struct drm_device *dev);
1260 int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv);
1261 void amdgpu_driver_postclose_kms(struct drm_device *dev,
1262 				 struct drm_file *file_priv);
1263 void amdgpu_driver_release_kms(struct drm_device *dev);
1264 
1265 int amdgpu_device_prepare(struct drm_device *dev);
1266 void amdgpu_device_complete(struct drm_device *dev);
1267 int amdgpu_device_suspend(struct drm_device *dev, bool fbcon);
1268 int amdgpu_device_resume(struct drm_device *dev, bool fbcon);
1269 u32 amdgpu_get_vblank_counter_kms(struct drm_crtc *crtc);
1270 int amdgpu_enable_vblank_kms(struct drm_crtc *crtc);
1271 void amdgpu_disable_vblank_kms(struct drm_crtc *crtc);
1272 int amdgpu_info_ioctl(struct drm_device *dev, void *data,
1273 		      struct drm_file *filp);
1274 int amdgpu_proc_options_ioctl(struct drm_device *dev, void *data,
1275 			      struct drm_file *filp);
1276 
1277 /*
1278  * functions used by amdgpu_encoder.c
1279  */
1280 struct amdgpu_afmt_acr {
1281 	u32 clock;
1282 
1283 	int n_32khz;
1284 	int cts_32khz;
1285 
1286 	int n_44_1khz;
1287 	int cts_44_1khz;
1288 
1289 	int n_48khz;
1290 	int cts_48khz;
1291 
1292 };
1293 
1294 struct amdgpu_afmt_acr amdgpu_afmt_acr(uint32_t clock);
1295 
1296 void amdgpu_register_gpu_instance(struct amdgpu_device *adev);
1297 void amdgpu_unregister_gpu_instance(struct amdgpu_device *adev);
1298 
1299 pci_ers_result_t amdgpu_pci_error_detected(struct pci_dev *pdev,
1300 					   pci_channel_state_t state);
1301 pci_ers_result_t amdgpu_pci_mmio_enabled(struct pci_dev *pdev);
1302 pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev);
1303 void amdgpu_pci_resume(struct pci_dev *pdev);
1304 
1305 bool amdgpu_device_cache_pci_state(struct pci_dev *pdev);
1306 bool amdgpu_device_load_pci_state(struct pci_dev *pdev);
1307 
1308 bool amdgpu_device_skip_hw_access(struct amdgpu_device *adev);
1309 
1310 int amdgpu_device_set_cg_state(struct amdgpu_device *adev,
1311 			       enum amd_clockgating_state state);
1312 int amdgpu_device_set_pg_state(struct amdgpu_device *adev,
1313 			       enum amd_powergating_state state);
1314 
1315 static inline bool amdgpu_device_has_timeouts_enabled(struct amdgpu_device *adev)
1316 {
1317 	return amdgpu_gpu_recovery != 0 &&
1318 		adev->gfx_timeout != MAX_SCHEDULE_TIMEOUT &&
1319 		adev->compute_timeout != MAX_SCHEDULE_TIMEOUT &&
1320 		adev->sdma_timeout != MAX_SCHEDULE_TIMEOUT &&
1321 		adev->video_timeout != MAX_SCHEDULE_TIMEOUT;
1322 }
1323 
1324 #include "amdgpu_object.h"
1325 
1326 static inline bool amdgpu_is_tmz(struct amdgpu_device *adev)
1327 {
1328        return adev->gmc.tmz_enabled;
1329 }
1330 
1331 int amdgpu_in_reset(struct amdgpu_device *adev);
1332 
1333 extern const struct attribute_group amdgpu_vram_mgr_attr_group;
1334 extern const struct attribute_group amdgpu_gtt_mgr_attr_group;
1335 extern const struct attribute_group amdgpu_flash_attr_group;
1336 
1337 void amdgpu_set_init_level(struct amdgpu_device *adev,
1338 			   enum amdgpu_init_lvl_id lvl);
1339 
1340 static inline int amdgpu_device_bus_status_check(struct amdgpu_device *adev)
1341 {
1342        u32 status;
1343        int r;
1344 
1345        r = pci_read_config_dword(adev->pdev, PCI_COMMAND, &status);
1346        if (r || PCI_POSSIBLE_ERROR(status)) {
1347 		dev_err(adev->dev, "device lost from bus!");
1348 		return -ENODEV;
1349        }
1350 
1351        return 0;
1352 }
1353 
1354 void amdgpu_device_set_uid(struct amdgpu_uid *uid_info,
1355 			   enum amdgpu_uid_type type, uint8_t inst,
1356 			   uint64_t uid);
1357 uint64_t amdgpu_device_get_uid(struct amdgpu_uid *uid_info,
1358 			       enum amdgpu_uid_type type, uint8_t inst);
1359 #endif
1360