drm/amd/pm: unify the interface for loading SMU microcode
[linux-2.6-microblaze.git] / drivers / gpu / drm / amd / pm / inc / amdgpu_smu.h
1 /*
2  * Copyright 2019 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  */
22 #ifndef __AMDGPU_SMU_H__
23 #define __AMDGPU_SMU_H__
24
25 #include "amdgpu.h"
26 #include "kgd_pp_interface.h"
27 #include "dm_pp_interface.h"
28 #include "dm_pp_smu.h"
29 #include "smu_types.h"
30
31 #define SMU_THERMAL_MINIMUM_ALERT_TEMP          0
32 #define SMU_THERMAL_MAXIMUM_ALERT_TEMP          255
33 #define SMU_TEMPERATURE_UNITS_PER_CENTIGRADES   1000
34 #define SMU_FW_NAME_LEN                 0x24
35
36 #define SMU_DPM_USER_PROFILE_RESTORE (1 << 0)
37
38 struct smu_hw_power_state {
39         unsigned int magic;
40 };
41
42 struct smu_power_state;
43
44 enum smu_state_ui_label {
45         SMU_STATE_UI_LABEL_NONE,
46         SMU_STATE_UI_LABEL_BATTERY,
47         SMU_STATE_UI_TABEL_MIDDLE_LOW,
48         SMU_STATE_UI_LABEL_BALLANCED,
49         SMU_STATE_UI_LABEL_MIDDLE_HIGHT,
50         SMU_STATE_UI_LABEL_PERFORMANCE,
51         SMU_STATE_UI_LABEL_BACO,
52 };
53
54 enum smu_state_classification_flag {
55         SMU_STATE_CLASSIFICATION_FLAG_BOOT                     = 0x0001,
56         SMU_STATE_CLASSIFICATION_FLAG_THERMAL                  = 0x0002,
57         SMU_STATE_CLASSIFICATIN_FLAG_LIMITED_POWER_SOURCE      = 0x0004,
58         SMU_STATE_CLASSIFICATION_FLAG_RESET                    = 0x0008,
59         SMU_STATE_CLASSIFICATION_FLAG_FORCED                   = 0x0010,
60         SMU_STATE_CLASSIFICATION_FLAG_USER_3D_PERFORMANCE      = 0x0020,
61         SMU_STATE_CLASSIFICATION_FLAG_USER_2D_PERFORMANCE      = 0x0040,
62         SMU_STATE_CLASSIFICATION_FLAG_3D_PERFORMANCE           = 0x0080,
63         SMU_STATE_CLASSIFICATION_FLAG_AC_OVERDIRVER_TEMPLATE   = 0x0100,
64         SMU_STATE_CLASSIFICATION_FLAG_UVD                      = 0x0200,
65         SMU_STATE_CLASSIFICATION_FLAG_3D_PERFORMANCE_LOW       = 0x0400,
66         SMU_STATE_CLASSIFICATION_FLAG_ACPI                     = 0x0800,
67         SMU_STATE_CLASSIFICATION_FLAG_HD2                      = 0x1000,
68         SMU_STATE_CLASSIFICATION_FLAG_UVD_HD                   = 0x2000,
69         SMU_STATE_CLASSIFICATION_FLAG_UVD_SD                   = 0x4000,
70         SMU_STATE_CLASSIFICATION_FLAG_USER_DC_PERFORMANCE      = 0x8000,
71         SMU_STATE_CLASSIFICATION_FLAG_DC_OVERDIRVER_TEMPLATE   = 0x10000,
72         SMU_STATE_CLASSIFICATION_FLAG_BACO                     = 0x20000,
73         SMU_STATE_CLASSIFICATIN_FLAG_LIMITED_POWER_SOURCE2      = 0x40000,
74         SMU_STATE_CLASSIFICATION_FLAG_ULV                      = 0x80000,
75         SMU_STATE_CLASSIFICATION_FLAG_UVD_MVC                  = 0x100000,
76 };
77
78 struct smu_state_classification_block {
79         enum smu_state_ui_label         ui_label;
80         enum smu_state_classification_flag  flags;
81         int                          bios_index;
82         bool                      temporary_state;
83         bool                      to_be_deleted;
84 };
85
86 struct smu_state_pcie_block {
87         unsigned int lanes;
88 };
89
90 enum smu_refreshrate_source {
91         SMU_REFRESHRATE_SOURCE_EDID,
92         SMU_REFRESHRATE_SOURCE_EXPLICIT
93 };
94
95 struct smu_state_display_block {
96         bool              disable_frame_modulation;
97         bool              limit_refreshrate;
98         enum smu_refreshrate_source refreshrate_source;
99         int                  explicit_refreshrate;
100         int                  edid_refreshrate_index;
101         bool              enable_vari_bright;
102 };
103
104 struct smu_state_memory_block {
105         bool              dll_off;
106         uint8_t                 m3arb;
107         uint8_t                 unused[3];
108 };
109
110 struct smu_state_software_algorithm_block {
111         bool disable_load_balancing;
112         bool enable_sleep_for_timestamps;
113 };
114
115 struct smu_temperature_range {
116         int min;
117         int max;
118         int edge_emergency_max;
119         int hotspot_min;
120         int hotspot_crit_max;
121         int hotspot_emergency_max;
122         int mem_min;
123         int mem_crit_max;
124         int mem_emergency_max;
125         int software_shutdown_temp;
126 };
127
128 struct smu_state_validation_block {
129         bool single_display_only;
130         bool disallow_on_dc;
131         uint8_t supported_power_levels;
132 };
133
134 struct smu_uvd_clocks {
135         uint32_t vclk;
136         uint32_t dclk;
137 };
138
139 /**
140 * Structure to hold a SMU Power State.
141 */
142 struct smu_power_state {
143         uint32_t                                      id;
144         struct list_head                              ordered_list;
145         struct list_head                              all_states_list;
146
147         struct smu_state_classification_block         classification;
148         struct smu_state_validation_block             validation;
149         struct smu_state_pcie_block                   pcie;
150         struct smu_state_display_block                display;
151         struct smu_state_memory_block                 memory;
152         struct smu_state_software_algorithm_block     software;
153         struct smu_uvd_clocks                         uvd_clocks;
154         struct smu_hw_power_state                     hardware;
155 };
156
157 enum smu_power_src_type
158 {
159         SMU_POWER_SOURCE_AC,
160         SMU_POWER_SOURCE_DC,
161         SMU_POWER_SOURCE_COUNT,
162 };
163
164 enum smu_ppt_limit_type
165 {
166         SMU_DEFAULT_PPT_LIMIT = 0,
167         SMU_FAST_PPT_LIMIT,
168 };
169
170 enum smu_ppt_limit_level
171 {
172         SMU_PPT_LIMIT_MIN = -1,
173         SMU_PPT_LIMIT_CURRENT,
174         SMU_PPT_LIMIT_DEFAULT,
175         SMU_PPT_LIMIT_MAX,
176 };
177
178 enum smu_memory_pool_size
179 {
180     SMU_MEMORY_POOL_SIZE_ZERO   = 0,
181     SMU_MEMORY_POOL_SIZE_256_MB = 0x10000000,
182     SMU_MEMORY_POOL_SIZE_512_MB = 0x20000000,
183     SMU_MEMORY_POOL_SIZE_1_GB   = 0x40000000,
184     SMU_MEMORY_POOL_SIZE_2_GB   = 0x80000000,
185 };
186
187 struct smu_user_dpm_profile {
188         uint32_t fan_mode;
189         uint32_t power_limit;
190         uint32_t fan_speed_percent;
191         uint32_t flags;
192
193         /* user clock state information */
194         uint32_t clk_mask[SMU_CLK_COUNT];
195         uint32_t clk_dependency;
196 };
197
198 enum smu_event_type {
199
200         SMU_EVENT_RESET_COMPLETE = 0,
201 };
202
203 #define SMU_TABLE_INIT(tables, table_id, s, a, d)       \
204         do {                                            \
205                 tables[table_id].size = s;              \
206                 tables[table_id].align = a;             \
207                 tables[table_id].domain = d;            \
208         } while (0)
209
210 struct smu_table {
211         uint64_t size;
212         uint32_t align;
213         uint8_t domain;
214         uint64_t mc_address;
215         void *cpu_addr;
216         struct amdgpu_bo *bo;
217 };
218
219 enum smu_perf_level_designation {
220         PERF_LEVEL_ACTIVITY,
221         PERF_LEVEL_POWER_CONTAINMENT,
222 };
223
224 struct smu_performance_level {
225         uint32_t core_clock;
226         uint32_t memory_clock;
227         uint32_t vddc;
228         uint32_t vddci;
229         uint32_t non_local_mem_freq;
230         uint32_t non_local_mem_width;
231 };
232
233 struct smu_clock_info {
234         uint32_t min_mem_clk;
235         uint32_t max_mem_clk;
236         uint32_t min_eng_clk;
237         uint32_t max_eng_clk;
238         uint32_t min_bus_bandwidth;
239         uint32_t max_bus_bandwidth;
240 };
241
242 struct smu_bios_boot_up_values
243 {
244         uint32_t                        revision;
245         uint32_t                        gfxclk;
246         uint32_t                        uclk;
247         uint32_t                        socclk;
248         uint32_t                        dcefclk;
249         uint32_t                        eclk;
250         uint32_t                        vclk;
251         uint32_t                        dclk;
252         uint16_t                        vddc;
253         uint16_t                        vddci;
254         uint16_t                        mvddc;
255         uint16_t                        vdd_gfx;
256         uint8_t                         cooling_id;
257         uint32_t                        pp_table_id;
258         uint32_t                        format_revision;
259         uint32_t                        content_revision;
260         uint32_t                        fclk;
261         uint32_t                        lclk;
262         uint32_t                        firmware_caps;
263 };
264
265 enum smu_table_id
266 {
267         SMU_TABLE_PPTABLE = 0,
268         SMU_TABLE_WATERMARKS,
269         SMU_TABLE_CUSTOM_DPM,
270         SMU_TABLE_DPMCLOCKS,
271         SMU_TABLE_AVFS,
272         SMU_TABLE_AVFS_PSM_DEBUG,
273         SMU_TABLE_AVFS_FUSE_OVERRIDE,
274         SMU_TABLE_PMSTATUSLOG,
275         SMU_TABLE_SMU_METRICS,
276         SMU_TABLE_DRIVER_SMU_CONFIG,
277         SMU_TABLE_ACTIVITY_MONITOR_COEFF,
278         SMU_TABLE_OVERDRIVE,
279         SMU_TABLE_I2C_COMMANDS,
280         SMU_TABLE_PACE,
281         SMU_TABLE_COUNT,
282 };
283
284 struct smu_table_context
285 {
286         void                            *power_play_table;
287         uint32_t                        power_play_table_size;
288         void                            *hardcode_pptable;
289         unsigned long                   metrics_time;
290         void                            *metrics_table;
291         void                            *clocks_table;
292         void                            *watermarks_table;
293
294         void                            *max_sustainable_clocks;
295         struct smu_bios_boot_up_values  boot_values;
296         void                            *driver_pptable;
297         struct smu_table                tables[SMU_TABLE_COUNT];
298         /*
299          * The driver table is just a staging buffer for
300          * uploading/downloading content from the SMU.
301          *
302          * And the table_id for SMU_MSG_TransferTableSmu2Dram/
303          * SMU_MSG_TransferTableDram2Smu instructs SMU
304          * which content driver is interested.
305          */
306         struct smu_table                driver_table;
307         struct smu_table                memory_pool;
308         struct smu_table                dummy_read_1_table;
309         uint8_t                         thermal_controller_type;
310
311         void                            *overdrive_table;
312         void                            *boot_overdrive_table;
313
314         uint32_t                        gpu_metrics_table_size;
315         void                            *gpu_metrics_table;
316 };
317
318 struct smu_dpm_context {
319         uint32_t dpm_context_size;
320         void *dpm_context;
321         void *golden_dpm_context;
322         bool enable_umd_pstate;
323         enum amd_dpm_forced_level dpm_level;
324         enum amd_dpm_forced_level saved_dpm_level;
325         enum amd_dpm_forced_level requested_dpm_level;
326         struct smu_power_state *dpm_request_power_state;
327         struct smu_power_state *dpm_current_power_state;
328         struct mclock_latency_table *mclk_latency_table;
329 };
330
331 struct smu_power_gate {
332         bool uvd_gated;
333         bool vce_gated;
334         atomic_t vcn_gated;
335         atomic_t jpeg_gated;
336         struct mutex vcn_gate_lock;
337         struct mutex jpeg_gate_lock;
338 };
339
340 struct smu_power_context {
341         void *power_context;
342         uint32_t power_context_size;
343         struct smu_power_gate power_gate;
344 };
345
346 #define SMU_FEATURE_MAX (64)
347 struct smu_feature
348 {
349         uint32_t feature_num;
350         DECLARE_BITMAP(supported, SMU_FEATURE_MAX);
351         DECLARE_BITMAP(allowed, SMU_FEATURE_MAX);
352         DECLARE_BITMAP(enabled, SMU_FEATURE_MAX);
353         struct mutex mutex;
354 };
355
356 struct smu_clocks {
357         uint32_t engine_clock;
358         uint32_t memory_clock;
359         uint32_t bus_bandwidth;
360         uint32_t engine_clock_in_sr;
361         uint32_t dcef_clock;
362         uint32_t dcef_clock_in_sr;
363 };
364
365 #define MAX_REGULAR_DPM_NUM 16
366 struct mclk_latency_entries {
367         uint32_t  frequency;
368         uint32_t  latency;
369 };
370 struct mclock_latency_table {
371         uint32_t  count;
372         struct mclk_latency_entries  entries[MAX_REGULAR_DPM_NUM];
373 };
374
375 enum smu_reset_mode
376 {
377     SMU_RESET_MODE_0,
378     SMU_RESET_MODE_1,
379     SMU_RESET_MODE_2,
380 };
381
382 enum smu_baco_state
383 {
384         SMU_BACO_STATE_ENTER = 0,
385         SMU_BACO_STATE_EXIT,
386 };
387
388 struct smu_baco_context
389 {
390         struct mutex mutex;
391         uint32_t state;
392         bool platform_support;
393 };
394
395 struct pstates_clk_freq {
396         uint32_t                        min;
397         uint32_t                        standard;
398         uint32_t                        peak;
399 };
400
401 struct smu_umd_pstate_table {
402         struct pstates_clk_freq         gfxclk_pstate;
403         struct pstates_clk_freq         socclk_pstate;
404         struct pstates_clk_freq         uclk_pstate;
405         struct pstates_clk_freq         vclk_pstate;
406         struct pstates_clk_freq         dclk_pstate;
407 };
408
409 struct cmn2asic_msg_mapping {
410         int     valid_mapping;
411         int     map_to;
412         int     valid_in_vf;
413 };
414
415 struct cmn2asic_mapping {
416         int     valid_mapping;
417         int     map_to;
418 };
419
420 #define WORKLOAD_POLICY_MAX 7
421 struct smu_context
422 {
423         struct amdgpu_device            *adev;
424         struct amdgpu_irq_src           irq_source;
425
426         const struct pptable_funcs      *ppt_funcs;
427         const struct cmn2asic_msg_mapping       *message_map;
428         const struct cmn2asic_mapping   *clock_map;
429         const struct cmn2asic_mapping   *feature_map;
430         const struct cmn2asic_mapping   *table_map;
431         const struct cmn2asic_mapping   *pwr_src_map;
432         const struct cmn2asic_mapping   *workload_map;
433         struct mutex                    mutex;
434         struct mutex                    sensor_lock;
435         struct mutex                    metrics_lock;
436         struct mutex                    message_lock;
437         uint64_t pool_size;
438
439         struct smu_table_context        smu_table;
440         struct smu_dpm_context          smu_dpm;
441         struct smu_power_context        smu_power;
442         struct smu_feature              smu_feature;
443         struct amd_pp_display_configuration  *display_config;
444         struct smu_baco_context         smu_baco;
445         struct smu_temperature_range    thermal_range;
446         void *od_settings;
447
448         struct smu_umd_pstate_table     pstate_table;
449         uint32_t pstate_sclk;
450         uint32_t pstate_mclk;
451
452         bool od_enabled;
453         uint32_t current_power_limit;
454         uint32_t default_power_limit;
455         uint32_t max_power_limit;
456
457         /* soft pptable */
458         uint32_t ppt_offset_bytes;
459         uint32_t ppt_size_bytes;
460         uint8_t  *ppt_start_addr;
461
462         bool support_power_containment;
463         bool disable_watermark;
464
465 #define WATERMARKS_EXIST        (1 << 0)
466 #define WATERMARKS_LOADED       (1 << 1)
467         uint32_t watermarks_bitmap;
468         uint32_t hard_min_uclk_req_from_dal;
469         bool disable_uclk_switch;
470
471         uint32_t workload_mask;
472         uint32_t workload_prority[WORKLOAD_POLICY_MAX];
473         uint32_t workload_setting[WORKLOAD_POLICY_MAX];
474         uint32_t power_profile_mode;
475         uint32_t default_power_profile_mode;
476         bool pm_enabled;
477         bool is_apu;
478
479         uint32_t smc_driver_if_version;
480         uint32_t smc_fw_if_version;
481         uint32_t smc_fw_version;
482
483         bool uploading_custom_pp_table;
484         bool dc_controlled_by_gpio;
485
486         struct work_struct throttling_logging_work;
487         atomic64_t throttle_int_counter;
488         struct work_struct interrupt_work;
489
490         unsigned fan_max_rpm;
491         unsigned manual_fan_speed_percent;
492
493         uint32_t gfx_default_hard_min_freq;
494         uint32_t gfx_default_soft_max_freq;
495         uint32_t gfx_actual_hard_min_freq;
496         uint32_t gfx_actual_soft_max_freq;
497
498         /* APU only */
499         uint32_t cpu_default_soft_min_freq;
500         uint32_t cpu_default_soft_max_freq;
501         uint32_t cpu_actual_soft_min_freq;
502         uint32_t cpu_actual_soft_max_freq;
503         uint32_t cpu_core_id_select;
504         uint16_t cpu_core_num;
505
506         struct smu_user_dpm_profile user_dpm_profile;
507 };
508
509 struct i2c_adapter;
510
511 /**
512  * struct pptable_funcs - Callbacks used to interact with the SMU.
513  */
514 struct pptable_funcs {
515         /**
516          * @run_btc: Calibrate voltage/frequency curve to fit the system's
517          *           power delivery and voltage margins. Required for adaptive
518          *           voltage frequency scaling (AVFS).
519          */
520         int (*run_btc)(struct smu_context *smu);
521
522         /**
523          * @get_allowed_feature_mask: Get allowed feature mask.
524          * &feature_mask: Array to store feature mask.
525          * &num: Elements in &feature_mask.
526          */
527         int (*get_allowed_feature_mask)(struct smu_context *smu, uint32_t *feature_mask, uint32_t num);
528
529         /**
530          * @get_current_power_state: Get the current power state.
531          *
532          * Return: Current power state on success, negative errno on failure.
533          */
534         enum amd_pm_state_type (*get_current_power_state)(struct smu_context *smu);
535
536         /**
537          * @set_default_dpm_table: Retrieve the default overdrive settings from
538          *                         the SMU.
539          */
540         int (*set_default_dpm_table)(struct smu_context *smu);
541
542         int (*set_power_state)(struct smu_context *smu);
543
544         /**
545          * @populate_umd_state_clk: Populate the UMD power state table with
546          *                          defaults.
547          */
548         int (*populate_umd_state_clk)(struct smu_context *smu);
549
550         /**
551          * @print_clk_levels: Print DPM clock levels for a clock domain
552          *                    to buffer. Star current level.
553          *
554          * Used for sysfs interfaces.
555          */
556         int (*print_clk_levels)(struct smu_context *smu, enum smu_clk_type clk_type, char *buf);
557
558         /**
559          * @force_clk_levels: Set a range of allowed DPM levels for a clock
560          *                    domain.
561          * &clk_type: Clock domain.
562          * &mask: Range of allowed DPM levels.
563          */
564         int (*force_clk_levels)(struct smu_context *smu, enum smu_clk_type clk_type, uint32_t mask);
565
566         /**
567          * @od_edit_dpm_table: Edit the custom overdrive DPM table.
568          * &type: Type of edit.
569          * &input: Edit parameters.
570          * &size: Size of &input.
571          */
572         int (*od_edit_dpm_table)(struct smu_context *smu,
573                                  enum PP_OD_DPM_TABLE_COMMAND type,
574                                  long *input, uint32_t size);
575
576         /**
577          * @get_clock_by_type_with_latency: Get the speed and latency of a clock
578          *                                  domain.
579          */
580         int (*get_clock_by_type_with_latency)(struct smu_context *smu,
581                                               enum smu_clk_type clk_type,
582                                               struct
583                                               pp_clock_levels_with_latency
584                                               *clocks);
585         /**
586          * @get_clock_by_type_with_voltage: Get the speed and voltage of a clock
587          *                                  domain.
588          */
589         int (*get_clock_by_type_with_voltage)(struct smu_context *smu,
590                                               enum amd_pp_clock_type type,
591                                               struct
592                                               pp_clock_levels_with_voltage
593                                               *clocks);
594
595         /**
596          * @get_power_profile_mode: Print all power profile modes to
597          *                          buffer. Star current mode.
598          */
599         int (*get_power_profile_mode)(struct smu_context *smu, char *buf);
600
601         /**
602          * @set_power_profile_mode: Set a power profile mode. Also used to
603          *                          create/set custom power profile modes.
604          * &input: Power profile mode parameters.
605          * &size: Size of &input.
606          */
607         int (*set_power_profile_mode)(struct smu_context *smu, long *input, uint32_t size);
608
609         /**
610          * @dpm_set_vcn_enable: Enable/disable VCN engine dynamic power
611          *                      management.
612          */
613         int (*dpm_set_vcn_enable)(struct smu_context *smu, bool enable);
614
615         /**
616          * @dpm_set_jpeg_enable: Enable/disable JPEG engine dynamic power
617          *                       management.
618          */
619         int (*dpm_set_jpeg_enable)(struct smu_context *smu, bool enable);
620
621         /**
622          * @read_sensor: Read data from a sensor.
623          * &sensor: Sensor to read data from.
624          * &data: Sensor reading.
625          * &size: Size of &data.
626          */
627         int (*read_sensor)(struct smu_context *smu, enum amd_pp_sensors sensor,
628                            void *data, uint32_t *size);
629
630         /**
631          * @pre_display_config_changed: Prepare GPU for a display configuration
632          *                              change.
633          *
634          * Disable display tracking and pin memory clock speed to maximum. Used
635          * in display component synchronization.
636          */
637         int (*pre_display_config_changed)(struct smu_context *smu);
638
639         /**
640          * @display_config_changed: Notify the SMU of the current display
641          *                          configuration.
642          *
643          * Allows SMU to properly track blanking periods for memory clock
644          * adjustment. Used in display component synchronization.
645          */
646         int (*display_config_changed)(struct smu_context *smu);
647
648         int (*apply_clocks_adjust_rules)(struct smu_context *smu);
649
650         /**
651          * @notify_smc_display_config: Applies display requirements to the
652          *                             current power state.
653          *
654          * Optimize deep sleep DCEFclk and mclk for the current display
655          * configuration. Used in display component synchronization.
656          */
657         int (*notify_smc_display_config)(struct smu_context *smu);
658
659         /**
660          * @is_dpm_running: Check if DPM is running.
661          *
662          * Return: True if DPM is running, false otherwise.
663          */
664         bool (*is_dpm_running)(struct smu_context *smu);
665
666         /**
667          * @get_fan_speed_percent: Get the current fan speed in percent.
668          */
669         int (*get_fan_speed_percent)(struct smu_context *smu, uint32_t *speed);
670
671         /**
672          * @set_watermarks_table: Configure and upload the watermarks tables to
673          *                        the SMU.
674          */
675         int (*set_watermarks_table)(struct smu_context *smu,
676                                     struct pp_smu_wm_range_sets *clock_ranges);
677
678         /**
679          * @get_thermal_temperature_range: Get safe thermal limits in Celcius.
680          */
681         int (*get_thermal_temperature_range)(struct smu_context *smu, struct smu_temperature_range *range);
682
683         /**
684          * @get_uclk_dpm_states: Get memory clock DPM levels in kHz.
685          * &clocks_in_khz: Array of DPM levels.
686          * &num_states: Elements in &clocks_in_khz.
687          */
688         int (*get_uclk_dpm_states)(struct smu_context *smu, uint32_t *clocks_in_khz, uint32_t *num_states);
689
690         /**
691          * @set_default_od_settings: Set the overdrive tables to defaults.
692          */
693         int (*set_default_od_settings)(struct smu_context *smu);
694
695         /**
696          * @set_performance_level: Set a performance level.
697          */
698         int (*set_performance_level)(struct smu_context *smu, enum amd_dpm_forced_level level);
699
700         /**
701          * @display_disable_memory_clock_switch: Enable/disable dynamic memory
702          *                                       clock switching.
703          *
704          * Disabling this feature forces memory clock speed to maximum.
705          * Enabling sets the minimum memory clock capable of driving the
706          * current display configuration.
707          */
708         int (*display_disable_memory_clock_switch)(struct smu_context *smu, bool disable_memory_clock_switch);
709
710         /**
711          * @dump_pptable: Print the power play table to the system log.
712          */
713         void (*dump_pptable)(struct smu_context *smu);
714
715         /**
716          * @get_power_limit: Get the device's power limits.
717          */
718         int (*get_power_limit)(struct smu_context *smu);
719
720         /**
721          * @get_ppt_limit: Get the device's ppt limits.
722          */
723         int (*get_ppt_limit)(struct smu_context *smu, uint32_t *ppt_limit,
724                         enum smu_ppt_limit_type limit_type, enum smu_ppt_limit_level limit_level);
725
726         /**
727          * @set_df_cstate: Set data fabric cstate.
728          */
729         int (*set_df_cstate)(struct smu_context *smu, enum pp_df_cstate state);
730
731         /**
732          * @allow_xgmi_power_down: Enable/disable external global memory
733          *                         interconnect power down.
734          */
735         int (*allow_xgmi_power_down)(struct smu_context *smu, bool en);
736
737         /**
738          * @update_pcie_parameters: Update and upload the system's PCIe
739          *                          capabilites to the SMU.
740          * &pcie_gen_cap: Maximum allowed PCIe generation.
741          * &pcie_width_cap: Maximum allowed PCIe width.
742          */
743         int (*update_pcie_parameters)(struct smu_context *smu, uint32_t pcie_gen_cap, uint32_t pcie_width_cap);
744
745         /**
746          * @i2c_init: Initialize i2c.
747          *
748          * The i2c bus is used internally by the SMU voltage regulators and
749          * other devices. The i2c's EEPROM also stores bad page tables on boards
750          * with ECC.
751          */
752         int (*i2c_init)(struct smu_context *smu, struct i2c_adapter *control);
753
754         /**
755          * @i2c_fini: Tear down i2c.
756          */
757         void (*i2c_fini)(struct smu_context *smu, struct i2c_adapter *control);
758
759         /**
760          * @get_unique_id: Get the GPU's unique id. Used for asset tracking.
761          */
762         void (*get_unique_id)(struct smu_context *smu);
763
764         /**
765          * @get_dpm_clock_table: Get a copy of the DPM clock table.
766          *
767          * Used by display component in bandwidth and watermark calculations.
768          */
769         int (*get_dpm_clock_table)(struct smu_context *smu, struct dpm_clocks *clock_table);
770
771         /**
772          * @init_microcode: Request the SMU's firmware from the kernel.
773          */
774         int (*init_microcode)(struct smu_context *smu);
775
776         /**
777          * @load_microcode: Load firmware onto the SMU.
778          */
779         int (*load_microcode)(struct smu_context *smu);
780
781         /**
782          * @fini_microcode: Release the SMU's firmware.
783          */
784         void (*fini_microcode)(struct smu_context *smu);
785
786         /**
787          * @init_smc_tables: Initialize the SMU tables.
788          */
789         int (*init_smc_tables)(struct smu_context *smu);
790
791         /**
792          * @fini_smc_tables: Release the SMU tables.
793          */
794         int (*fini_smc_tables)(struct smu_context *smu);
795
796         /**
797          * @init_power: Initialize the power gate table context.
798          */
799         int (*init_power)(struct smu_context *smu);
800
801         /**
802          * @fini_power: Release the power gate table context.
803          */
804         int (*fini_power)(struct smu_context *smu);
805
806         /**
807          * @check_fw_status: Check the SMU's firmware status.
808          *
809          * Return: Zero if check passes, negative errno on failure.
810          */
811         int (*check_fw_status)(struct smu_context *smu);
812
813         /**
814          * @set_mp1_state: put SMU into a correct state for comming
815          *                 resume from runpm or gpu reset.
816          */
817         int (*set_mp1_state)(struct smu_context *smu,
818                              enum pp_mp1_state mp1_state);
819
820         /**
821          * @setup_pptable: Initialize the power play table and populate it with
822          *                 default values.
823          */
824         int (*setup_pptable)(struct smu_context *smu);
825
826         /**
827          * @get_vbios_bootup_values: Get default boot values from the VBIOS.
828          */
829         int (*get_vbios_bootup_values)(struct smu_context *smu);
830
831         /**
832          * @check_fw_version: Print driver and SMU interface versions to the
833          *                    system log.
834          *
835          * Interface mismatch is not a critical failure.
836          */
837         int (*check_fw_version)(struct smu_context *smu);
838
839         /**
840          * @powergate_sdma: Power up/down system direct memory access.
841          */
842         int (*powergate_sdma)(struct smu_context *smu, bool gate);
843
844         /**
845          * @set_gfx_cgpg: Enable/disable graphics engine course grain power
846          *                gating.
847          */
848         int (*set_gfx_cgpg)(struct smu_context *smu, bool enable);
849
850         /**
851          * @write_pptable: Write the power play table to the SMU.
852          */
853         int (*write_pptable)(struct smu_context *smu);
854
855         /**
856          * @set_driver_table_location: Send the location of the driver table to
857          *                             the SMU.
858          */
859         int (*set_driver_table_location)(struct smu_context *smu);
860
861         /**
862          * @set_tool_table_location: Send the location of the tool table to the
863          *                           SMU.
864          */
865         int (*set_tool_table_location)(struct smu_context *smu);
866
867         /**
868          * @notify_memory_pool_location: Send the location of the memory pool to
869          *                               the SMU.
870          */
871         int (*notify_memory_pool_location)(struct smu_context *smu);
872
873         /**
874          * @system_features_control: Enable/disable all SMU features.
875          */
876         int (*system_features_control)(struct smu_context *smu, bool en);
877
878         /**
879          * @send_smc_msg_with_param: Send a message with a parameter to the SMU.
880          * &msg: Type of message.
881          * &param: Message parameter.
882          * &read_arg: SMU response (optional).
883          */
884         int (*send_smc_msg_with_param)(struct smu_context *smu,
885                                        enum smu_message_type msg, uint32_t param, uint32_t *read_arg);
886
887         /**
888          * @send_smc_msg: Send a message to the SMU.
889          * &msg: Type of message.
890          * &read_arg: SMU response (optional).
891          */
892         int (*send_smc_msg)(struct smu_context *smu,
893                             enum smu_message_type msg,
894                             uint32_t *read_arg);
895
896         /**
897          * @init_display_count: Notify the SMU of the number of display
898          *                      components in current display configuration.
899          */
900         int (*init_display_count)(struct smu_context *smu, uint32_t count);
901
902         /**
903          * @set_allowed_mask: Notify the SMU of the features currently allowed
904          *                    by the driver.
905          */
906         int (*set_allowed_mask)(struct smu_context *smu);
907
908         /**
909          * @get_enabled_mask: Get a mask of features that are currently enabled
910          *                    on the SMU.
911          * &feature_mask: Array representing enabled feature mask.
912          * &num: Elements in &feature_mask.
913          */
914         int (*get_enabled_mask)(struct smu_context *smu, uint32_t *feature_mask, uint32_t num);
915
916         /**
917          * @feature_is_enabled: Test if a feature is enabled.
918          *
919          * Return: One if enabled, zero if disabled.
920          */
921         int (*feature_is_enabled)(struct smu_context *smu, enum smu_feature_mask mask);
922
923         /**
924          * @disable_all_features_with_exception: Disable all features with
925          *                                       exception to those in &mask.
926          */
927         int (*disable_all_features_with_exception)(struct smu_context *smu, enum smu_feature_mask mask);
928
929         /**
930          * @notify_display_change: Enable fast memory clock switching.
931          *
932          * Allows for fine grained memory clock switching but has more stringent
933          * timing requirements.
934          */
935         int (*notify_display_change)(struct smu_context *smu);
936
937         /**
938          * @set_power_limit: Set power limit in watts.
939          */
940         int (*set_power_limit)(struct smu_context *smu, uint32_t n);
941
942         /**
943          * @init_max_sustainable_clocks: Populate max sustainable clock speed
944          *                               table with values from the SMU.
945          */
946         int (*init_max_sustainable_clocks)(struct smu_context *smu);
947
948         /**
949          * @enable_thermal_alert: Enable thermal alert interrupts.
950          */
951         int (*enable_thermal_alert)(struct smu_context *smu);
952
953         /**
954          * @disable_thermal_alert: Disable thermal alert interrupts.
955          */
956         int (*disable_thermal_alert)(struct smu_context *smu);
957
958         /**
959          * @set_min_dcef_deep_sleep: Set a minimum display fabric deep sleep
960          *                           clock speed in MHz.
961          */
962         int (*set_min_dcef_deep_sleep)(struct smu_context *smu, uint32_t clk);
963
964         /**
965          * @display_clock_voltage_request: Set a hard minimum frequency
966          * for a clock domain.
967          */
968         int (*display_clock_voltage_request)(struct smu_context *smu, struct
969                                              pp_display_clock_request
970                                              *clock_req);
971
972         /**
973          * @get_fan_control_mode: Get the current fan control mode.
974          */
975         uint32_t (*get_fan_control_mode)(struct smu_context *smu);
976
977         /**
978          * @set_fan_control_mode: Set the fan control mode.
979          */
980         int (*set_fan_control_mode)(struct smu_context *smu, uint32_t mode);
981
982         /**
983          * @set_fan_speed_percent: Set a static fan speed in percent.
984          */
985         int (*set_fan_speed_percent)(struct smu_context *smu, uint32_t speed);
986
987         /**
988          * @set_xgmi_pstate: Set inter-chip global memory interconnect pstate.
989          * &pstate: Pstate to set. D0 if Nonzero, D3 otherwise.
990          */
991         int (*set_xgmi_pstate)(struct smu_context *smu, uint32_t pstate);
992
993         /**
994          * @gfx_off_control: Enable/disable graphics engine poweroff.
995          */
996         int (*gfx_off_control)(struct smu_context *smu, bool enable);
997
998
999         /**
1000          * @get_gfx_off_status: Get graphics engine poweroff status.
1001          *
1002          * Return:
1003          * 0 - GFXOFF(default).
1004          * 1 - Transition out of GFX State.
1005          * 2 - Not in GFXOFF.
1006          * 3 - Transition into GFXOFF.
1007          */
1008         uint32_t (*get_gfx_off_status)(struct smu_context *smu);
1009
1010         /**
1011          * @register_irq_handler: Register interupt request handlers.
1012          */
1013         int (*register_irq_handler)(struct smu_context *smu);
1014
1015         /**
1016          * @set_azalia_d3_pme: Wake the audio decode engine from d3 sleep.
1017          */
1018         int (*set_azalia_d3_pme)(struct smu_context *smu);
1019
1020         /**
1021          * @get_max_sustainable_clocks_by_dc: Get a copy of the max sustainable
1022          *                                    clock speeds table.
1023          *
1024          * Provides a way for the display component (DC) to get the max
1025          * sustainable clocks from the SMU.
1026          */
1027         int (*get_max_sustainable_clocks_by_dc)(struct smu_context *smu, struct pp_smu_nv_clock_table *max_clocks);
1028
1029         /**
1030          * @baco_is_support: Check if GPU supports BACO (Bus Active, Chip Off).
1031          */
1032         bool (*baco_is_support)(struct smu_context *smu);
1033
1034         /**
1035          * @baco_get_state: Get the current BACO state.
1036          *
1037          * Return: Current BACO state.
1038          */
1039         enum smu_baco_state (*baco_get_state)(struct smu_context *smu);
1040
1041         /**
1042          * @baco_set_state: Enter/exit BACO.
1043          */
1044         int (*baco_set_state)(struct smu_context *smu, enum smu_baco_state state);
1045
1046         /**
1047          * @baco_enter: Enter BACO.
1048          */
1049         int (*baco_enter)(struct smu_context *smu);
1050
1051         /**
1052          * @baco_exit: Exit Baco.
1053          */
1054         int (*baco_exit)(struct smu_context *smu);
1055
1056         /**
1057          * @mode1_reset_is_support: Check if GPU supports mode1 reset.
1058          */
1059         bool (*mode1_reset_is_support)(struct smu_context *smu);
1060         /**
1061          * @mode2_reset_is_support: Check if GPU supports mode2 reset.
1062          */
1063         bool (*mode2_reset_is_support)(struct smu_context *smu);
1064
1065         /**
1066          * @mode1_reset: Perform mode1 reset.
1067          *
1068          * Complete GPU reset.
1069          */
1070         int (*mode1_reset)(struct smu_context *smu);
1071
1072         /**
1073          * @mode2_reset: Perform mode2 reset.
1074          *
1075          * Mode2 reset generally does not reset as many IPs as mode1 reset. The
1076          * IPs reset varies by asic.
1077          */
1078         int (*mode2_reset)(struct smu_context *smu);
1079
1080         /**
1081          * @get_dpm_ultimate_freq: Get the hard frequency range of a clock
1082          *                         domain in MHz.
1083          */
1084         int (*get_dpm_ultimate_freq)(struct smu_context *smu, enum smu_clk_type clk_type, uint32_t *min, uint32_t *max);
1085
1086         /**
1087          * @set_soft_freq_limited_range: Set the soft frequency range of a clock
1088          *                               domain in MHz.
1089          */
1090         int (*set_soft_freq_limited_range)(struct smu_context *smu, enum smu_clk_type clk_type, uint32_t min, uint32_t max);
1091
1092         /**
1093          * @set_power_source: Notify the SMU of the current power source.
1094          */
1095         int (*set_power_source)(struct smu_context *smu, enum smu_power_src_type power_src);
1096
1097         /**
1098          * @log_thermal_throttling_event: Print a thermal throttling warning to
1099          *                                the system's log.
1100          */
1101         void (*log_thermal_throttling_event)(struct smu_context *smu);
1102
1103         /**
1104          * @get_pp_feature_mask: Print a human readable table of enabled
1105          *                       features to buffer.
1106          */
1107         size_t (*get_pp_feature_mask)(struct smu_context *smu, char *buf);
1108
1109         /**
1110          * @set_pp_feature_mask: Request the SMU enable/disable features to
1111          *                       match those enabled in &new_mask.
1112          */
1113         int (*set_pp_feature_mask)(struct smu_context *smu, uint64_t new_mask);
1114
1115         /**
1116          * @get_gpu_metrics: Get a copy of the GPU metrics table from the SMU.
1117          *
1118          * Return: Size of &table
1119          */
1120         ssize_t (*get_gpu_metrics)(struct smu_context *smu, void **table);
1121
1122         /**
1123          * @enable_mgpu_fan_boost: Enable multi-GPU fan boost.
1124          */
1125         int (*enable_mgpu_fan_boost)(struct smu_context *smu);
1126
1127         /**
1128          * @gfx_ulv_control: Enable/disable ultra low voltage.
1129          */
1130         int (*gfx_ulv_control)(struct smu_context *smu, bool enablement);
1131
1132         /**
1133          * @deep_sleep_control: Enable/disable deep sleep.
1134          */
1135         int (*deep_sleep_control)(struct smu_context *smu, bool enablement);
1136
1137         /**
1138          * @get_fan_parameters: Get fan parameters.
1139          *
1140          * Get maximum fan speed from the power play table.
1141          */
1142         int (*get_fan_parameters)(struct smu_context *smu);
1143
1144         /**
1145          * @post_init: Helper function for asic specific workarounds.
1146          */
1147         int (*post_init)(struct smu_context *smu);
1148
1149         /**
1150          * @interrupt_work: Work task scheduled from SMU interrupt handler.
1151          */
1152         void (*interrupt_work)(struct smu_context *smu);
1153
1154         /**
1155          * @gpo_control: Enable/disable graphics power optimization if supported.
1156          */
1157         int (*gpo_control)(struct smu_context *smu, bool enablement);
1158
1159         /**
1160          * @gfx_state_change_set: Send the current graphics state to the SMU.
1161          */
1162         int (*gfx_state_change_set)(struct smu_context *smu, uint32_t state);
1163
1164         /**
1165          * @set_fine_grain_gfx_freq_parameters: Set fine grain graphics clock
1166          *                                      parameters to defaults.
1167          */
1168         int (*set_fine_grain_gfx_freq_parameters)(struct smu_context *smu);
1169
1170         /**
1171          * @set_light_sbr:  Set light sbr mode for the SMU.
1172          */
1173         int (*set_light_sbr)(struct smu_context *smu, bool enable);
1174
1175         /**
1176          * @wait_for_event:  Wait for events from SMU.
1177          */
1178         int (*wait_for_event)(struct smu_context *smu,
1179                               enum smu_event_type event, uint64_t event_arg);
1180 };
1181
1182 typedef enum {
1183         METRICS_CURR_GFXCLK,
1184         METRICS_CURR_SOCCLK,
1185         METRICS_CURR_UCLK,
1186         METRICS_CURR_VCLK,
1187         METRICS_CURR_VCLK1,
1188         METRICS_CURR_DCLK,
1189         METRICS_CURR_DCLK1,
1190         METRICS_CURR_FCLK,
1191         METRICS_CURR_DCEFCLK,
1192         METRICS_AVERAGE_CPUCLK,
1193         METRICS_AVERAGE_GFXCLK,
1194         METRICS_AVERAGE_SOCCLK,
1195         METRICS_AVERAGE_FCLK,
1196         METRICS_AVERAGE_UCLK,
1197         METRICS_AVERAGE_VCLK,
1198         METRICS_AVERAGE_DCLK,
1199         METRICS_AVERAGE_GFXACTIVITY,
1200         METRICS_AVERAGE_MEMACTIVITY,
1201         METRICS_AVERAGE_VCNACTIVITY,
1202         METRICS_AVERAGE_SOCKETPOWER,
1203         METRICS_TEMPERATURE_EDGE,
1204         METRICS_TEMPERATURE_HOTSPOT,
1205         METRICS_TEMPERATURE_MEM,
1206         METRICS_TEMPERATURE_VRGFX,
1207         METRICS_TEMPERATURE_VRSOC,
1208         METRICS_TEMPERATURE_VRMEM,
1209         METRICS_THROTTLER_STATUS,
1210         METRICS_CURR_FANSPEED,
1211         METRICS_VOLTAGE_VDDSOC,
1212         METRICS_VOLTAGE_VDDGFX,
1213 } MetricsMember_t;
1214
1215 enum smu_cmn2asic_mapping_type {
1216         CMN2ASIC_MAPPING_MSG,
1217         CMN2ASIC_MAPPING_CLK,
1218         CMN2ASIC_MAPPING_FEATURE,
1219         CMN2ASIC_MAPPING_TABLE,
1220         CMN2ASIC_MAPPING_PWR,
1221         CMN2ASIC_MAPPING_WORKLOAD,
1222 };
1223
1224 #define MSG_MAP(msg, index, valid_in_vf) \
1225         [SMU_MSG_##msg] = {1, (index), (valid_in_vf)}
1226
1227 #define CLK_MAP(clk, index) \
1228         [SMU_##clk] = {1, (index)}
1229
1230 #define FEA_MAP(fea) \
1231         [SMU_FEATURE_##fea##_BIT] = {1, FEATURE_##fea##_BIT}
1232
1233 #define FEA_MAP_REVERSE(fea) \
1234         [SMU_FEATURE_DPM_##fea##_BIT] = {1, FEATURE_##fea##_DPM_BIT}
1235
1236 #define FEA_MAP_HALF_REVERSE(fea) \
1237         [SMU_FEATURE_DPM_##fea##CLK_BIT] = {1, FEATURE_##fea##_DPM_BIT}
1238
1239 #define TAB_MAP(tab) \
1240         [SMU_TABLE_##tab] = {1, TABLE_##tab}
1241
1242 #define TAB_MAP_VALID(tab) \
1243         [SMU_TABLE_##tab] = {1, TABLE_##tab}
1244
1245 #define TAB_MAP_INVALID(tab) \
1246         [SMU_TABLE_##tab] = {0, TABLE_##tab}
1247
1248 #define PWR_MAP(tab) \
1249         [SMU_POWER_SOURCE_##tab] = {1, POWER_SOURCE_##tab}
1250
1251 #define WORKLOAD_MAP(profile, workload) \
1252         [profile] = {1, (workload)}
1253
1254 #if !defined(SWSMU_CODE_LAYER_L2) && !defined(SWSMU_CODE_LAYER_L3) && !defined(SWSMU_CODE_LAYER_L4)
1255 int smu_get_power_limit(struct smu_context *smu,
1256                         uint32_t *limit,
1257                         enum smu_ppt_limit_level limit_level);
1258
1259 bool smu_mode1_reset_is_support(struct smu_context *smu);
1260 bool smu_mode2_reset_is_support(struct smu_context *smu);
1261 int smu_mode1_reset(struct smu_context *smu);
1262
1263 extern const struct amd_ip_funcs smu_ip_funcs;
1264
1265 extern const struct amdgpu_ip_block_version smu_v11_0_ip_block;
1266 extern const struct amdgpu_ip_block_version smu_v12_0_ip_block;
1267 extern const struct amdgpu_ip_block_version smu_v13_0_ip_block;
1268
1269 bool is_support_sw_smu(struct amdgpu_device *adev);
1270 bool is_support_cclk_dpm(struct amdgpu_device *adev);
1271 int smu_write_watermarks_table(struct smu_context *smu);
1272
1273 /* smu to display interface */
1274 extern int smu_dpm_set_power_gate(void *handle, uint32_t block_type, bool gate);
1275
1276 int smu_get_dpm_freq_range(struct smu_context *smu, enum smu_clk_type clk_type,
1277                            uint32_t *min, uint32_t *max);
1278
1279 int smu_set_soft_freq_range(struct smu_context *smu, enum smu_clk_type clk_type,
1280                             uint32_t min, uint32_t max);
1281
1282 int smu_set_ac_dc(struct smu_context *smu);
1283
1284 int smu_allow_xgmi_power_down(struct smu_context *smu, bool en);
1285
1286 int smu_get_status_gfxoff(struct amdgpu_device *adev, uint32_t *value);
1287
1288 int smu_gfx_state_change_set(struct smu_context *smu, uint32_t state);
1289
1290 int smu_set_light_sbr(struct smu_context *smu, bool enable);
1291
1292 int smu_wait_for_event(struct amdgpu_device *adev, enum smu_event_type event,
1293                        uint64_t event_arg);
1294
1295 #endif
1296 #endif