1 /*
2 * Copyright 2020 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
23 #define SWSMU_CODE_LAYER_L4
24
25 #include "amdgpu.h"
26 #include "amdgpu_smu.h"
27 #include "smu_cmn.h"
28 #include "soc15_common.h"
29
30 /*
31 * DO NOT use these for err/warn/info/debug messages.
32 * Use dev_err, dev_warn, dev_info and dev_dbg instead.
33 * They are more MGPU friendly.
34 */
35 #undef pr_err
36 #undef pr_warn
37 #undef pr_info
38 #undef pr_debug
39
40 #define MP1_C2PMSG_90__CONTENT_MASK 0xFFFFFFFFL
41
42 const int link_speed[] = {25, 50, 80, 160, 320, 640};
43
44 #undef __SMU_DUMMY_MAP
45 #define __SMU_DUMMY_MAP(type) #type
46 static const char * const __smu_message_names[] = {
47 SMU_MESSAGE_TYPES
48 };
49
50 #define smu_cmn_call_asic_func(intf, smu, args...) \
51 ((smu)->ppt_funcs ? ((smu)->ppt_funcs->intf ? \
52 (smu)->ppt_funcs->intf(smu, ##args) : \
53 -ENOTSUPP) : \
54 -EINVAL)
55
smu_get_message_name(struct smu_context * smu,enum smu_message_type type)56 static const char *smu_get_message_name(struct smu_context *smu,
57 enum smu_message_type type)
58 {
59 if (type >= SMU_MSG_MAX_COUNT)
60 return "unknown smu message";
61
62 return __smu_message_names[type];
63 }
64
smu_cmn_read_arg(struct smu_context * smu,uint32_t * arg)65 static void smu_cmn_read_arg(struct smu_context *smu,
66 uint32_t *arg)
67 {
68 struct amdgpu_device *adev = smu->adev;
69
70 *arg = RREG32(smu->param_reg);
71 }
72
73 /* Redefine the SMU error codes here.
74 *
75 * Note that these definitions are redundant and should be removed
76 * when the SMU has exported a unified header file containing these
77 * macros, which header file we can just include and use the SMU's
78 * macros. At the moment, these error codes are defined by the SMU
79 * per-ASIC unfortunately, yet we're a one driver for all ASICs.
80 */
81 #define SMU_RESP_NONE 0
82 #define SMU_RESP_OK 1
83 #define SMU_RESP_CMD_FAIL 0xFF
84 #define SMU_RESP_CMD_UNKNOWN 0xFE
85 #define SMU_RESP_CMD_BAD_PREREQ 0xFD
86 #define SMU_RESP_BUSY_OTHER 0xFC
87 #define SMU_RESP_DEBUG_END 0xFB
88
89 /**
90 * __smu_cmn_poll_stat -- poll for a status from the SMU
91 * @smu: a pointer to SMU context
92 *
93 * Returns the status of the SMU, which could be,
94 * 0, the SMU is busy with your command;
95 * 1, execution status: success, execution result: success;
96 * 0xFF, execution status: success, execution result: failure;
97 * 0xFE, unknown command;
98 * 0xFD, valid command, but bad (command) prerequisites;
99 * 0xFC, the command was rejected as the SMU is busy;
100 * 0xFB, "SMC_Result_DebugDataDumpEnd".
101 *
102 * The values here are not defined by macros, because I'd rather we
103 * include a single header file which defines them, which is
104 * maintained by the SMU FW team, so that we're impervious to firmware
105 * changes. At the moment those values are defined in various header
106 * files, one for each ASIC, yet here we're a single ASIC-agnostic
107 * interface. Such a change can be followed-up by a subsequent patch.
108 */
__smu_cmn_poll_stat(struct smu_context * smu)109 static u32 __smu_cmn_poll_stat(struct smu_context *smu)
110 {
111 struct amdgpu_device *adev = smu->adev;
112 int timeout = adev->usec_timeout * 20;
113 u32 reg;
114
115 for ( ; timeout > 0; timeout--) {
116 reg = RREG32(smu->resp_reg);
117 if ((reg & MP1_C2PMSG_90__CONTENT_MASK) != 0)
118 break;
119
120 udelay(1);
121 }
122
123 return reg;
124 }
125
__smu_cmn_reg_print_error(struct smu_context * smu,u32 reg_c2pmsg_90,int msg_index,u32 param,enum smu_message_type msg)126 static void __smu_cmn_reg_print_error(struct smu_context *smu,
127 u32 reg_c2pmsg_90,
128 int msg_index,
129 u32 param,
130 enum smu_message_type msg)
131 {
132 struct amdgpu_device *adev = smu->adev;
133 const char *message = smu_get_message_name(smu, msg);
134 u32 msg_idx, prm;
135
136 switch (reg_c2pmsg_90) {
137 case SMU_RESP_NONE: {
138 msg_idx = RREG32(smu->msg_reg);
139 prm = RREG32(smu->param_reg);
140 dev_err_ratelimited(adev->dev,
141 "SMU: I'm not done with your previous command: SMN_C2PMSG_66:0x%08X SMN_C2PMSG_82:0x%08X",
142 msg_idx, prm);
143 }
144 break;
145 case SMU_RESP_OK:
146 /* The SMU executed the command. It completed with a
147 * successful result.
148 */
149 break;
150 case SMU_RESP_CMD_FAIL:
151 /* The SMU executed the command. It completed with an
152 * unsuccessful result.
153 */
154 break;
155 case SMU_RESP_CMD_UNKNOWN:
156 dev_err_ratelimited(adev->dev,
157 "SMU: unknown command: index:%d param:0x%08X message:%s",
158 msg_index, param, message);
159 break;
160 case SMU_RESP_CMD_BAD_PREREQ:
161 dev_err_ratelimited(adev->dev,
162 "SMU: valid command, bad prerequisites: index:%d param:0x%08X message:%s",
163 msg_index, param, message);
164 break;
165 case SMU_RESP_BUSY_OTHER:
166 dev_err_ratelimited(adev->dev,
167 "SMU: I'm very busy for your command: index:%d param:0x%08X message:%s",
168 msg_index, param, message);
169 break;
170 case SMU_RESP_DEBUG_END:
171 dev_err_ratelimited(adev->dev,
172 "SMU: I'm debugging!");
173 break;
174 default:
175 dev_err_ratelimited(adev->dev,
176 "SMU: response:0x%08X for index:%d param:0x%08X message:%s?",
177 reg_c2pmsg_90, msg_index, param, message);
178 break;
179 }
180 }
181
__smu_cmn_reg2errno(struct smu_context * smu,u32 reg_c2pmsg_90)182 static int __smu_cmn_reg2errno(struct smu_context *smu, u32 reg_c2pmsg_90)
183 {
184 int res;
185
186 switch (reg_c2pmsg_90) {
187 case SMU_RESP_NONE:
188 /* The SMU is busy--still executing your command.
189 */
190 res = -ETIME;
191 break;
192 case SMU_RESP_OK:
193 res = 0;
194 break;
195 case SMU_RESP_CMD_FAIL:
196 /* Command completed successfully, but the command
197 * status was failure.
198 */
199 res = -EIO;
200 break;
201 case SMU_RESP_CMD_UNKNOWN:
202 /* Unknown command--ignored by the SMU.
203 */
204 res = -EOPNOTSUPP;
205 break;
206 case SMU_RESP_CMD_BAD_PREREQ:
207 /* Valid command--bad prerequisites.
208 */
209 res = -EINVAL;
210 break;
211 case SMU_RESP_BUSY_OTHER:
212 /* The SMU is busy with other commands. The client
213 * should retry in 10 us.
214 */
215 res = -EBUSY;
216 break;
217 default:
218 /* Unknown or debug response from the SMU.
219 */
220 res = -EREMOTEIO;
221 break;
222 }
223
224 return res;
225 }
226
__smu_cmn_send_msg(struct smu_context * smu,u16 msg,u32 param)227 static void __smu_cmn_send_msg(struct smu_context *smu,
228 u16 msg,
229 u32 param)
230 {
231 struct amdgpu_device *adev = smu->adev;
232
233 WREG32(smu->resp_reg, 0);
234 WREG32(smu->param_reg, param);
235 WREG32(smu->msg_reg, msg);
236 }
237
__smu_cmn_get_msg_flags(struct smu_context * smu,enum smu_message_type msg)238 static inline uint32_t __smu_cmn_get_msg_flags(struct smu_context *smu,
239 enum smu_message_type msg)
240 {
241 return smu->message_map[msg].flags;
242 }
243
__smu_cmn_ras_filter_msg(struct smu_context * smu,enum smu_message_type msg,bool * poll)244 static int __smu_cmn_ras_filter_msg(struct smu_context *smu,
245 enum smu_message_type msg, bool *poll)
246 {
247 struct amdgpu_device *adev = smu->adev;
248 uint32_t flags, resp;
249 bool fed_status;
250
251 flags = __smu_cmn_get_msg_flags(smu, msg);
252 *poll = true;
253
254 /* When there is RAS fatal error, FW won't process non-RAS priority
255 * messages. Don't allow any messages other than RAS priority messages.
256 */
257 fed_status = amdgpu_ras_get_fed_status(adev);
258 if (fed_status) {
259 if (!(flags & SMU_MSG_RAS_PRI)) {
260 dev_dbg(adev->dev,
261 "RAS error detected, skip sending %s",
262 smu_get_message_name(smu, msg));
263 return -EACCES;
264 }
265
266 /* FW will ignore non-priority messages when a RAS fatal error
267 * is detected. Hence it is possible that a previous message
268 * wouldn't have got response. Allow to continue without polling
269 * for response status for priority messages.
270 */
271 resp = RREG32(smu->resp_reg);
272 dev_dbg(adev->dev,
273 "Sending RAS priority message %s response status: %x",
274 smu_get_message_name(smu, msg), resp);
275 if (resp == 0)
276 *poll = false;
277 }
278
279 return 0;
280 }
281
__smu_cmn_send_debug_msg(struct smu_context * smu,u32 msg,u32 param)282 static int __smu_cmn_send_debug_msg(struct smu_context *smu,
283 u32 msg,
284 u32 param)
285 {
286 struct amdgpu_device *adev = smu->adev;
287
288 WREG32(smu->debug_param_reg, param);
289 WREG32(smu->debug_msg_reg, msg);
290 WREG32(smu->debug_resp_reg, 0);
291
292 return 0;
293 }
294 /**
295 * smu_cmn_send_msg_without_waiting -- send the message; don't wait for status
296 * @smu: pointer to an SMU context
297 * @msg_index: message index
298 * @param: message parameter to send to the SMU
299 *
300 * Send a message to the SMU with the parameter passed. Do not wait
301 * for status/result of the message, thus the "without_waiting".
302 *
303 * Return 0 on success, -errno on error if we weren't able to _send_
304 * the message for some reason. See __smu_cmn_reg2errno() for details
305 * of the -errno.
306 */
smu_cmn_send_msg_without_waiting(struct smu_context * smu,uint16_t msg_index,uint32_t param)307 int smu_cmn_send_msg_without_waiting(struct smu_context *smu,
308 uint16_t msg_index,
309 uint32_t param)
310 {
311 struct amdgpu_device *adev = smu->adev;
312 u32 reg;
313 int res;
314
315 if (adev->no_hw_access)
316 return 0;
317
318 if (smu->smc_fw_state == SMU_FW_HANG) {
319 dev_err(adev->dev, "SMU is in hanged state, failed to send smu message!\n");
320 res = -EREMOTEIO;
321 goto Out;
322 }
323
324 if (smu->smc_fw_state == SMU_FW_INIT) {
325 smu->smc_fw_state = SMU_FW_RUNTIME;
326 } else {
327 reg = __smu_cmn_poll_stat(smu);
328 res = __smu_cmn_reg2errno(smu, reg);
329 if (reg == SMU_RESP_NONE || res == -EREMOTEIO)
330 goto Out;
331 }
332
333 __smu_cmn_send_msg(smu, msg_index, param);
334 res = 0;
335 Out:
336 if (unlikely(adev->pm.smu_debug_mask & SMU_DEBUG_HALT_ON_ERROR) &&
337 res && (res != -ETIME)) {
338 amdgpu_device_halt(adev);
339 WARN_ON(1);
340 }
341
342 return res;
343 }
344
345 /**
346 * smu_cmn_wait_for_response -- wait for response from the SMU
347 * @smu: pointer to an SMU context
348 *
349 * Wait for status from the SMU.
350 *
351 * Return 0 on success, -errno on error, indicating the execution
352 * status and result of the message being waited for. See
353 * __smu_cmn_reg2errno() for details of the -errno.
354 */
smu_cmn_wait_for_response(struct smu_context * smu)355 int smu_cmn_wait_for_response(struct smu_context *smu)
356 {
357 u32 reg;
358 int res;
359
360 reg = __smu_cmn_poll_stat(smu);
361 res = __smu_cmn_reg2errno(smu, reg);
362
363 if (res == -EREMOTEIO)
364 smu->smc_fw_state = SMU_FW_HANG;
365
366 if (unlikely(smu->adev->pm.smu_debug_mask & SMU_DEBUG_HALT_ON_ERROR) &&
367 res && (res != -ETIME)) {
368 amdgpu_device_halt(smu->adev);
369 WARN_ON(1);
370 }
371
372 return res;
373 }
374
375 /**
376 * smu_cmn_send_smc_msg_with_param -- send a message with parameter
377 * @smu: pointer to an SMU context
378 * @msg: message to send
379 * @param: parameter to send to the SMU
380 * @read_arg: pointer to u32 to return a value from the SMU back
381 * to the caller
382 *
383 * Send the message @msg with parameter @param to the SMU, wait for
384 * completion of the command, and return back a value from the SMU in
385 * @read_arg pointer.
386 *
387 * Return 0 on success, -errno when a problem is encountered sending
388 * message or receiving reply. If there is a PCI bus recovery or
389 * the destination is a virtual GPU which does not allow this message
390 * type, the message is simply dropped and success is also returned.
391 * See __smu_cmn_reg2errno() for details of the -errno.
392 *
393 * If we weren't able to send the message to the SMU, we also print
394 * the error to the standard log.
395 *
396 * Command completion status is printed only if the -errno is
397 * -EREMOTEIO, indicating that the SMU returned back an
398 * undefined/unknown/unspecified result. All other cases are
399 * well-defined, not printed, but instead given back to the client to
400 * decide what further to do.
401 *
402 * The return value, @read_arg is read back regardless, to give back
403 * more information to the client, which on error would most likely be
404 * @param, but we can't assume that. This also eliminates more
405 * conditionals.
406 */
smu_cmn_send_smc_msg_with_param(struct smu_context * smu,enum smu_message_type msg,uint32_t param,uint32_t * read_arg)407 int smu_cmn_send_smc_msg_with_param(struct smu_context *smu,
408 enum smu_message_type msg,
409 uint32_t param,
410 uint32_t *read_arg)
411 {
412 struct amdgpu_device *adev = smu->adev;
413 int res, index;
414 bool poll = true;
415 u32 reg;
416
417 if (adev->no_hw_access)
418 return 0;
419
420 index = smu_cmn_to_asic_specific_index(smu,
421 CMN2ASIC_MAPPING_MSG,
422 msg);
423 if (index < 0)
424 return index == -EACCES ? 0 : index;
425
426 mutex_lock(&smu->message_lock);
427
428 if (smu->smc_fw_caps & SMU_FW_CAP_RAS_PRI) {
429 res = __smu_cmn_ras_filter_msg(smu, msg, &poll);
430 if (res)
431 goto Out;
432 }
433
434 if (smu->smc_fw_state == SMU_FW_HANG) {
435 dev_err(adev->dev, "SMU is in hanged state, failed to send smu message!\n");
436 res = -EREMOTEIO;
437 goto Out;
438 } else if (smu->smc_fw_state == SMU_FW_INIT) {
439 /* Ignore initial smu response register value */
440 poll = false;
441 smu->smc_fw_state = SMU_FW_RUNTIME;
442 }
443
444 if (poll) {
445 reg = __smu_cmn_poll_stat(smu);
446 res = __smu_cmn_reg2errno(smu, reg);
447 if (reg == SMU_RESP_NONE || res == -EREMOTEIO) {
448 __smu_cmn_reg_print_error(smu, reg, index, param, msg);
449 goto Out;
450 }
451 }
452 __smu_cmn_send_msg(smu, (uint16_t) index, param);
453 reg = __smu_cmn_poll_stat(smu);
454 res = __smu_cmn_reg2errno(smu, reg);
455 if (res != 0) {
456 if (res == -EREMOTEIO)
457 smu->smc_fw_state = SMU_FW_HANG;
458 __smu_cmn_reg_print_error(smu, reg, index, param, msg);
459 }
460 if (read_arg) {
461 smu_cmn_read_arg(smu, read_arg);
462 dev_dbg(adev->dev, "smu send message: %s(%d) param: 0x%08x, resp: 0x%08x, readval: 0x%08x\n",
463 smu_get_message_name(smu, msg), index, param, reg, *read_arg);
464 } else {
465 dev_dbg(adev->dev, "smu send message: %s(%d) param: 0x%08x, resp: 0x%08x\n",
466 smu_get_message_name(smu, msg), index, param, reg);
467 }
468 Out:
469 if (unlikely(adev->pm.smu_debug_mask & SMU_DEBUG_HALT_ON_ERROR) && res) {
470 amdgpu_device_halt(adev);
471 WARN_ON(1);
472 }
473
474 mutex_unlock(&smu->message_lock);
475 return res;
476 }
477
smu_cmn_send_smc_msg(struct smu_context * smu,enum smu_message_type msg,uint32_t * read_arg)478 int smu_cmn_send_smc_msg(struct smu_context *smu,
479 enum smu_message_type msg,
480 uint32_t *read_arg)
481 {
482 return smu_cmn_send_smc_msg_with_param(smu,
483 msg,
484 0,
485 read_arg);
486 }
487
smu_cmn_send_debug_smc_msg(struct smu_context * smu,uint32_t msg)488 int smu_cmn_send_debug_smc_msg(struct smu_context *smu,
489 uint32_t msg)
490 {
491 return __smu_cmn_send_debug_msg(smu, msg, 0);
492 }
493
smu_cmn_send_debug_smc_msg_with_param(struct smu_context * smu,uint32_t msg,uint32_t param)494 int smu_cmn_send_debug_smc_msg_with_param(struct smu_context *smu,
495 uint32_t msg, uint32_t param)
496 {
497 return __smu_cmn_send_debug_msg(smu, msg, param);
498 }
499
smu_cmn_to_asic_specific_index(struct smu_context * smu,enum smu_cmn2asic_mapping_type type,uint32_t index)500 int smu_cmn_to_asic_specific_index(struct smu_context *smu,
501 enum smu_cmn2asic_mapping_type type,
502 uint32_t index)
503 {
504 struct cmn2asic_msg_mapping msg_mapping;
505 struct cmn2asic_mapping mapping;
506
507 switch (type) {
508 case CMN2ASIC_MAPPING_MSG:
509 if (index >= SMU_MSG_MAX_COUNT ||
510 !smu->message_map)
511 return -EINVAL;
512
513 msg_mapping = smu->message_map[index];
514 if (!msg_mapping.valid_mapping)
515 return -EINVAL;
516
517 if (amdgpu_sriov_vf(smu->adev) &&
518 !(msg_mapping.flags & SMU_MSG_VF_FLAG))
519 return -EACCES;
520
521 return msg_mapping.map_to;
522
523 case CMN2ASIC_MAPPING_CLK:
524 if (index >= SMU_CLK_COUNT ||
525 !smu->clock_map)
526 return -EINVAL;
527
528 mapping = smu->clock_map[index];
529 if (!mapping.valid_mapping)
530 return -EINVAL;
531
532 return mapping.map_to;
533
534 case CMN2ASIC_MAPPING_FEATURE:
535 if (index >= SMU_FEATURE_COUNT ||
536 !smu->feature_map)
537 return -EINVAL;
538
539 mapping = smu->feature_map[index];
540 if (!mapping.valid_mapping)
541 return -EINVAL;
542
543 return mapping.map_to;
544
545 case CMN2ASIC_MAPPING_TABLE:
546 if (index >= SMU_TABLE_COUNT ||
547 !smu->table_map)
548 return -EINVAL;
549
550 mapping = smu->table_map[index];
551 if (!mapping.valid_mapping)
552 return -EINVAL;
553
554 return mapping.map_to;
555
556 case CMN2ASIC_MAPPING_PWR:
557 if (index >= SMU_POWER_SOURCE_COUNT ||
558 !smu->pwr_src_map)
559 return -EINVAL;
560
561 mapping = smu->pwr_src_map[index];
562 if (!mapping.valid_mapping)
563 return -EINVAL;
564
565 return mapping.map_to;
566
567 case CMN2ASIC_MAPPING_WORKLOAD:
568 if (index >= PP_SMC_POWER_PROFILE_COUNT ||
569 !smu->workload_map)
570 return -EINVAL;
571
572 mapping = smu->workload_map[index];
573 if (!mapping.valid_mapping)
574 return -ENOTSUPP;
575
576 return mapping.map_to;
577
578 default:
579 return -EINVAL;
580 }
581 }
582
smu_cmn_feature_is_supported(struct smu_context * smu,enum smu_feature_mask mask)583 int smu_cmn_feature_is_supported(struct smu_context *smu,
584 enum smu_feature_mask mask)
585 {
586 struct smu_feature *feature = &smu->smu_feature;
587 int feature_id;
588
589 feature_id = smu_cmn_to_asic_specific_index(smu,
590 CMN2ASIC_MAPPING_FEATURE,
591 mask);
592 if (feature_id < 0)
593 return 0;
594
595 WARN_ON(feature_id > feature->feature_num);
596
597 return test_bit(feature_id, feature->supported);
598 }
599
__smu_get_enabled_features(struct smu_context * smu,uint64_t * enabled_features)600 static int __smu_get_enabled_features(struct smu_context *smu,
601 uint64_t *enabled_features)
602 {
603 return smu_cmn_call_asic_func(get_enabled_mask, smu, enabled_features);
604 }
605
smu_cmn_feature_is_enabled(struct smu_context * smu,enum smu_feature_mask mask)606 int smu_cmn_feature_is_enabled(struct smu_context *smu,
607 enum smu_feature_mask mask)
608 {
609 struct amdgpu_device *adev = smu->adev;
610 uint64_t enabled_features;
611 int feature_id;
612
613 if (__smu_get_enabled_features(smu, &enabled_features)) {
614 dev_err(adev->dev, "Failed to retrieve enabled ppfeatures!\n");
615 return 0;
616 }
617
618 /*
619 * For Renoir and Cyan Skillfish, they are assumed to have all features
620 * enabled. Also considering they have no feature_map available, the
621 * check here can avoid unwanted feature_map check below.
622 */
623 if (enabled_features == ULLONG_MAX)
624 return 1;
625
626 feature_id = smu_cmn_to_asic_specific_index(smu,
627 CMN2ASIC_MAPPING_FEATURE,
628 mask);
629 if (feature_id < 0)
630 return 0;
631
632 return test_bit(feature_id, (unsigned long *)&enabled_features);
633 }
634
smu_cmn_clk_dpm_is_enabled(struct smu_context * smu,enum smu_clk_type clk_type)635 bool smu_cmn_clk_dpm_is_enabled(struct smu_context *smu,
636 enum smu_clk_type clk_type)
637 {
638 enum smu_feature_mask feature_id = 0;
639
640 switch (clk_type) {
641 case SMU_MCLK:
642 case SMU_UCLK:
643 feature_id = SMU_FEATURE_DPM_UCLK_BIT;
644 break;
645 case SMU_GFXCLK:
646 case SMU_SCLK:
647 feature_id = SMU_FEATURE_DPM_GFXCLK_BIT;
648 break;
649 case SMU_SOCCLK:
650 feature_id = SMU_FEATURE_DPM_SOCCLK_BIT;
651 break;
652 case SMU_VCLK:
653 case SMU_VCLK1:
654 feature_id = SMU_FEATURE_DPM_VCLK_BIT;
655 break;
656 case SMU_DCLK:
657 case SMU_DCLK1:
658 feature_id = SMU_FEATURE_DPM_DCLK_BIT;
659 break;
660 case SMU_FCLK:
661 feature_id = SMU_FEATURE_DPM_FCLK_BIT;
662 break;
663 default:
664 return true;
665 }
666
667 if (!smu_cmn_feature_is_enabled(smu, feature_id))
668 return false;
669
670 return true;
671 }
672
smu_cmn_get_enabled_mask(struct smu_context * smu,uint64_t * feature_mask)673 int smu_cmn_get_enabled_mask(struct smu_context *smu,
674 uint64_t *feature_mask)
675 {
676 uint32_t *feature_mask_high;
677 uint32_t *feature_mask_low;
678 int ret = 0, index = 0;
679
680 if (!feature_mask)
681 return -EINVAL;
682
683 feature_mask_low = &((uint32_t *)feature_mask)[0];
684 feature_mask_high = &((uint32_t *)feature_mask)[1];
685
686 index = smu_cmn_to_asic_specific_index(smu,
687 CMN2ASIC_MAPPING_MSG,
688 SMU_MSG_GetEnabledSmuFeatures);
689 if (index > 0) {
690 ret = smu_cmn_send_smc_msg_with_param(smu,
691 SMU_MSG_GetEnabledSmuFeatures,
692 0,
693 feature_mask_low);
694 if (ret)
695 return ret;
696
697 ret = smu_cmn_send_smc_msg_with_param(smu,
698 SMU_MSG_GetEnabledSmuFeatures,
699 1,
700 feature_mask_high);
701 } else {
702 ret = smu_cmn_send_smc_msg(smu,
703 SMU_MSG_GetEnabledSmuFeaturesHigh,
704 feature_mask_high);
705 if (ret)
706 return ret;
707
708 ret = smu_cmn_send_smc_msg(smu,
709 SMU_MSG_GetEnabledSmuFeaturesLow,
710 feature_mask_low);
711 }
712
713 return ret;
714 }
715
smu_cmn_get_indep_throttler_status(const unsigned long dep_status,const uint8_t * throttler_map)716 uint64_t smu_cmn_get_indep_throttler_status(
717 const unsigned long dep_status,
718 const uint8_t *throttler_map)
719 {
720 uint64_t indep_status = 0;
721 uint8_t dep_bit = 0;
722
723 for_each_set_bit(dep_bit, &dep_status, 32)
724 indep_status |= 1ULL << throttler_map[dep_bit];
725
726 return indep_status;
727 }
728
smu_cmn_feature_update_enable_state(struct smu_context * smu,uint64_t feature_mask,bool enabled)729 int smu_cmn_feature_update_enable_state(struct smu_context *smu,
730 uint64_t feature_mask,
731 bool enabled)
732 {
733 int ret = 0;
734
735 if (enabled) {
736 ret = smu_cmn_send_smc_msg_with_param(smu,
737 SMU_MSG_EnableSmuFeaturesLow,
738 lower_32_bits(feature_mask),
739 NULL);
740 if (ret)
741 return ret;
742 ret = smu_cmn_send_smc_msg_with_param(smu,
743 SMU_MSG_EnableSmuFeaturesHigh,
744 upper_32_bits(feature_mask),
745 NULL);
746 } else {
747 ret = smu_cmn_send_smc_msg_with_param(smu,
748 SMU_MSG_DisableSmuFeaturesLow,
749 lower_32_bits(feature_mask),
750 NULL);
751 if (ret)
752 return ret;
753 ret = smu_cmn_send_smc_msg_with_param(smu,
754 SMU_MSG_DisableSmuFeaturesHigh,
755 upper_32_bits(feature_mask),
756 NULL);
757 }
758
759 return ret;
760 }
761
smu_cmn_feature_set_enabled(struct smu_context * smu,enum smu_feature_mask mask,bool enable)762 int smu_cmn_feature_set_enabled(struct smu_context *smu,
763 enum smu_feature_mask mask,
764 bool enable)
765 {
766 int feature_id;
767
768 feature_id = smu_cmn_to_asic_specific_index(smu,
769 CMN2ASIC_MAPPING_FEATURE,
770 mask);
771 if (feature_id < 0)
772 return -EINVAL;
773
774 return smu_cmn_feature_update_enable_state(smu,
775 1ULL << feature_id,
776 enable);
777 }
778
779 #undef __SMU_DUMMY_MAP
780 #define __SMU_DUMMY_MAP(fea) #fea
781 static const char *__smu_feature_names[] = {
782 SMU_FEATURE_MASKS
783 };
784
smu_get_feature_name(struct smu_context * smu,enum smu_feature_mask feature)785 static const char *smu_get_feature_name(struct smu_context *smu,
786 enum smu_feature_mask feature)
787 {
788 if (feature >= SMU_FEATURE_COUNT)
789 return "unknown smu feature";
790 return __smu_feature_names[feature];
791 }
792
smu_cmn_get_pp_feature_mask(struct smu_context * smu,char * buf)793 size_t smu_cmn_get_pp_feature_mask(struct smu_context *smu,
794 char *buf)
795 {
796 int8_t sort_feature[MAX(SMU_FEATURE_COUNT, SMU_FEATURE_MAX)];
797 uint64_t feature_mask;
798 int i, feature_index;
799 uint32_t count = 0;
800 size_t size = 0;
801
802 if (__smu_get_enabled_features(smu, &feature_mask))
803 return 0;
804
805 size = sysfs_emit_at(buf, size, "features high: 0x%08x low: 0x%08x\n",
806 upper_32_bits(feature_mask), lower_32_bits(feature_mask));
807
808 memset(sort_feature, -1, sizeof(sort_feature));
809
810 for (i = 0; i < SMU_FEATURE_COUNT; i++) {
811 feature_index = smu_cmn_to_asic_specific_index(smu,
812 CMN2ASIC_MAPPING_FEATURE,
813 i);
814 if (feature_index < 0)
815 continue;
816
817 sort_feature[feature_index] = i;
818 }
819
820 size += sysfs_emit_at(buf, size, "%-2s. %-20s %-3s : %-s\n",
821 "No", "Feature", "Bit", "State");
822
823 for (feature_index = 0; feature_index < SMU_FEATURE_MAX; feature_index++) {
824 if (sort_feature[feature_index] < 0)
825 continue;
826
827 size += sysfs_emit_at(buf, size, "%02d. %-20s (%2d) : %s\n",
828 count++,
829 smu_get_feature_name(smu, sort_feature[feature_index]),
830 feature_index,
831 !!test_bit(feature_index, (unsigned long *)&feature_mask) ?
832 "enabled" : "disabled");
833 }
834
835 return size;
836 }
837
smu_cmn_set_pp_feature_mask(struct smu_context * smu,uint64_t new_mask)838 int smu_cmn_set_pp_feature_mask(struct smu_context *smu,
839 uint64_t new_mask)
840 {
841 int ret = 0;
842 uint64_t feature_mask;
843 uint64_t feature_2_enabled = 0;
844 uint64_t feature_2_disabled = 0;
845
846 ret = __smu_get_enabled_features(smu, &feature_mask);
847 if (ret)
848 return ret;
849
850 feature_2_enabled = ~feature_mask & new_mask;
851 feature_2_disabled = feature_mask & ~new_mask;
852
853 if (feature_2_enabled) {
854 ret = smu_cmn_feature_update_enable_state(smu,
855 feature_2_enabled,
856 true);
857 if (ret)
858 return ret;
859 }
860 if (feature_2_disabled) {
861 ret = smu_cmn_feature_update_enable_state(smu,
862 feature_2_disabled,
863 false);
864 if (ret)
865 return ret;
866 }
867
868 return ret;
869 }
870
871 /**
872 * smu_cmn_disable_all_features_with_exception - disable all dpm features
873 * except this specified by
874 * @mask
875 *
876 * @smu: smu_context pointer
877 * @mask: the dpm feature which should not be disabled
878 * SMU_FEATURE_COUNT: no exception, all dpm features
879 * to disable
880 *
881 * Returns:
882 * 0 on success or a negative error code on failure.
883 */
smu_cmn_disable_all_features_with_exception(struct smu_context * smu,enum smu_feature_mask mask)884 int smu_cmn_disable_all_features_with_exception(struct smu_context *smu,
885 enum smu_feature_mask mask)
886 {
887 uint64_t features_to_disable = U64_MAX;
888 int skipped_feature_id;
889
890 if (mask != SMU_FEATURE_COUNT) {
891 skipped_feature_id = smu_cmn_to_asic_specific_index(smu,
892 CMN2ASIC_MAPPING_FEATURE,
893 mask);
894 if (skipped_feature_id < 0)
895 return -EINVAL;
896
897 features_to_disable &= ~(1ULL << skipped_feature_id);
898 }
899
900 return smu_cmn_feature_update_enable_state(smu,
901 features_to_disable,
902 0);
903 }
904
smu_cmn_get_smc_version(struct smu_context * smu,uint32_t * if_version,uint32_t * smu_version)905 int smu_cmn_get_smc_version(struct smu_context *smu,
906 uint32_t *if_version,
907 uint32_t *smu_version)
908 {
909 int ret = 0;
910
911 if (!if_version && !smu_version)
912 return -EINVAL;
913
914 if (smu->smc_fw_if_version && smu->smc_fw_version)
915 {
916 if (if_version)
917 *if_version = smu->smc_fw_if_version;
918
919 if (smu_version)
920 *smu_version = smu->smc_fw_version;
921
922 return 0;
923 }
924
925 if (if_version) {
926 ret = smu_cmn_send_smc_msg(smu, SMU_MSG_GetDriverIfVersion, if_version);
927 if (ret)
928 return ret;
929
930 smu->smc_fw_if_version = *if_version;
931 }
932
933 if (smu_version) {
934 ret = smu_cmn_send_smc_msg(smu, SMU_MSG_GetSmuVersion, smu_version);
935 if (ret)
936 return ret;
937
938 smu->smc_fw_version = *smu_version;
939 }
940
941 return ret;
942 }
943
smu_cmn_update_table(struct smu_context * smu,enum smu_table_id table_index,int argument,void * table_data,bool drv2smu)944 int smu_cmn_update_table(struct smu_context *smu,
945 enum smu_table_id table_index,
946 int argument,
947 void *table_data,
948 bool drv2smu)
949 {
950 struct smu_table_context *smu_table = &smu->smu_table;
951 struct amdgpu_device *adev = smu->adev;
952 struct smu_table *table = &smu_table->driver_table;
953 int table_id = smu_cmn_to_asic_specific_index(smu,
954 CMN2ASIC_MAPPING_TABLE,
955 table_index);
956 uint32_t table_size;
957 int ret = 0;
958 if (!table_data || table_id >= SMU_TABLE_COUNT || table_id < 0)
959 return -EINVAL;
960
961 table_size = smu_table->tables[table_index].size;
962
963 if (drv2smu) {
964 memcpy(table->cpu_addr, table_data, table_size);
965 /*
966 * Flush hdp cache: to guard the content seen by
967 * GPU is consitent with CPU.
968 */
969 amdgpu_asic_flush_hdp(adev, NULL);
970 }
971
972 ret = smu_cmn_send_smc_msg_with_param(smu, drv2smu ?
973 SMU_MSG_TransferTableDram2Smu :
974 SMU_MSG_TransferTableSmu2Dram,
975 table_id | ((argument & 0xFFFF) << 16),
976 NULL);
977 if (ret)
978 return ret;
979
980 if (!drv2smu) {
981 amdgpu_asic_invalidate_hdp(adev, NULL);
982 memcpy(table_data, table->cpu_addr, table_size);
983 }
984
985 return 0;
986 }
987
smu_cmn_write_watermarks_table(struct smu_context * smu)988 int smu_cmn_write_watermarks_table(struct smu_context *smu)
989 {
990 void *watermarks_table = smu->smu_table.watermarks_table;
991
992 if (!watermarks_table)
993 return -EINVAL;
994
995 return smu_cmn_update_table(smu,
996 SMU_TABLE_WATERMARKS,
997 0,
998 watermarks_table,
999 true);
1000 }
1001
smu_cmn_write_pptable(struct smu_context * smu)1002 int smu_cmn_write_pptable(struct smu_context *smu)
1003 {
1004 void *pptable = smu->smu_table.driver_pptable;
1005
1006 return smu_cmn_update_table(smu,
1007 SMU_TABLE_PPTABLE,
1008 0,
1009 pptable,
1010 true);
1011 }
1012
smu_cmn_get_metrics_table(struct smu_context * smu,void * metrics_table,bool bypass_cache)1013 int smu_cmn_get_metrics_table(struct smu_context *smu,
1014 void *metrics_table,
1015 bool bypass_cache)
1016 {
1017 struct smu_table_context *smu_table = &smu->smu_table;
1018 uint32_t table_size =
1019 smu_table->tables[SMU_TABLE_SMU_METRICS].size;
1020 int ret = 0;
1021
1022 if (bypass_cache ||
1023 !smu_table->metrics_time ||
1024 time_after(jiffies, smu_table->metrics_time + msecs_to_jiffies(1))) {
1025 ret = smu_cmn_update_table(smu,
1026 SMU_TABLE_SMU_METRICS,
1027 0,
1028 smu_table->metrics_table,
1029 false);
1030 if (ret) {
1031 dev_info(smu->adev->dev, "Failed to export SMU metrics table!\n");
1032 return ret;
1033 }
1034 smu_table->metrics_time = jiffies;
1035 }
1036
1037 if (metrics_table)
1038 memcpy(metrics_table, smu_table->metrics_table, table_size);
1039
1040 return 0;
1041 }
1042
smu_cmn_get_combo_pptable(struct smu_context * smu)1043 int smu_cmn_get_combo_pptable(struct smu_context *smu)
1044 {
1045 void *pptable = smu->smu_table.combo_pptable;
1046
1047 return smu_cmn_update_table(smu,
1048 SMU_TABLE_COMBO_PPTABLE,
1049 0,
1050 pptable,
1051 false);
1052 }
1053
smu_cmn_init_soft_gpu_metrics(void * table,uint8_t frev,uint8_t crev)1054 void smu_cmn_init_soft_gpu_metrics(void *table, uint8_t frev, uint8_t crev)
1055 {
1056 struct metrics_table_header *header = (struct metrics_table_header *)table;
1057 uint16_t structure_size;
1058
1059 #define METRICS_VERSION(a, b) ((a << 16) | b)
1060
1061 switch (METRICS_VERSION(frev, crev)) {
1062 case METRICS_VERSION(1, 0):
1063 structure_size = sizeof(struct gpu_metrics_v1_0);
1064 break;
1065 case METRICS_VERSION(1, 1):
1066 structure_size = sizeof(struct gpu_metrics_v1_1);
1067 break;
1068 case METRICS_VERSION(1, 2):
1069 structure_size = sizeof(struct gpu_metrics_v1_2);
1070 break;
1071 case METRICS_VERSION(1, 3):
1072 structure_size = sizeof(struct gpu_metrics_v1_3);
1073 break;
1074 case METRICS_VERSION(1, 4):
1075 structure_size = sizeof(struct gpu_metrics_v1_4);
1076 break;
1077 case METRICS_VERSION(1, 5):
1078 structure_size = sizeof(struct gpu_metrics_v1_5);
1079 break;
1080 case METRICS_VERSION(1, 6):
1081 structure_size = sizeof(struct gpu_metrics_v1_6);
1082 break;
1083 case METRICS_VERSION(1, 7):
1084 structure_size = sizeof(struct gpu_metrics_v1_7);
1085 break;
1086 case METRICS_VERSION(2, 0):
1087 structure_size = sizeof(struct gpu_metrics_v2_0);
1088 break;
1089 case METRICS_VERSION(2, 1):
1090 structure_size = sizeof(struct gpu_metrics_v2_1);
1091 break;
1092 case METRICS_VERSION(2, 2):
1093 structure_size = sizeof(struct gpu_metrics_v2_2);
1094 break;
1095 case METRICS_VERSION(2, 3):
1096 structure_size = sizeof(struct gpu_metrics_v2_3);
1097 break;
1098 case METRICS_VERSION(2, 4):
1099 structure_size = sizeof(struct gpu_metrics_v2_4);
1100 break;
1101 case METRICS_VERSION(3, 0):
1102 structure_size = sizeof(struct gpu_metrics_v3_0);
1103 break;
1104 default:
1105 return;
1106 }
1107
1108 #undef METRICS_VERSION
1109
1110 memset(header, 0xFF, structure_size);
1111
1112 header->format_revision = frev;
1113 header->content_revision = crev;
1114 header->structure_size = structure_size;
1115
1116 }
1117
smu_cmn_set_mp1_state(struct smu_context * smu,enum pp_mp1_state mp1_state)1118 int smu_cmn_set_mp1_state(struct smu_context *smu,
1119 enum pp_mp1_state mp1_state)
1120 {
1121 enum smu_message_type msg;
1122 int ret;
1123
1124 switch (mp1_state) {
1125 case PP_MP1_STATE_SHUTDOWN:
1126 msg = SMU_MSG_PrepareMp1ForShutdown;
1127 break;
1128 case PP_MP1_STATE_UNLOAD:
1129 msg = SMU_MSG_PrepareMp1ForUnload;
1130 break;
1131 case PP_MP1_STATE_RESET:
1132 msg = SMU_MSG_PrepareMp1ForReset;
1133 break;
1134 case PP_MP1_STATE_NONE:
1135 default:
1136 return 0;
1137 }
1138
1139 ret = smu_cmn_send_smc_msg(smu, msg, NULL);
1140 if (ret)
1141 dev_err(smu->adev->dev, "[PrepareMp1] Failed!\n");
1142
1143 return ret;
1144 }
1145
smu_cmn_is_audio_func_enabled(struct amdgpu_device * adev)1146 bool smu_cmn_is_audio_func_enabled(struct amdgpu_device *adev)
1147 {
1148 struct pci_dev *p = NULL;
1149 bool snd_driver_loaded;
1150
1151 /*
1152 * If the ASIC comes with no audio function, we always assume
1153 * it is "enabled".
1154 */
1155 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
1156 adev->pdev->bus->number, 1);
1157 if (!p)
1158 return true;
1159
1160 snd_driver_loaded = pci_is_enabled(p) ? true : false;
1161
1162 pci_dev_put(p);
1163
1164 return snd_driver_loaded;
1165 }
1166
smu_soc_policy_get_desc(struct smu_dpm_policy * policy,int level)1167 static char *smu_soc_policy_get_desc(struct smu_dpm_policy *policy, int level)
1168 {
1169 if (level < 0 || !(policy->level_mask & BIT(level)))
1170 return "Invalid";
1171
1172 switch (level) {
1173 case SOC_PSTATE_DEFAULT:
1174 return "soc_pstate_default";
1175 case SOC_PSTATE_0:
1176 return "soc_pstate_0";
1177 case SOC_PSTATE_1:
1178 return "soc_pstate_1";
1179 case SOC_PSTATE_2:
1180 return "soc_pstate_2";
1181 }
1182
1183 return "Invalid";
1184 }
1185
1186 static struct smu_dpm_policy_desc pstate_policy_desc = {
1187 .name = STR_SOC_PSTATE_POLICY,
1188 .get_desc = smu_soc_policy_get_desc,
1189 };
1190
smu_cmn_generic_soc_policy_desc(struct smu_dpm_policy * policy)1191 void smu_cmn_generic_soc_policy_desc(struct smu_dpm_policy *policy)
1192 {
1193 policy->desc = &pstate_policy_desc;
1194 }
1195
smu_xgmi_plpd_policy_get_desc(struct smu_dpm_policy * policy,int level)1196 static char *smu_xgmi_plpd_policy_get_desc(struct smu_dpm_policy *policy,
1197 int level)
1198 {
1199 if (level < 0 || !(policy->level_mask & BIT(level)))
1200 return "Invalid";
1201
1202 switch (level) {
1203 case XGMI_PLPD_DISALLOW:
1204 return "plpd_disallow";
1205 case XGMI_PLPD_DEFAULT:
1206 return "plpd_default";
1207 case XGMI_PLPD_OPTIMIZED:
1208 return "plpd_optimized";
1209 }
1210
1211 return "Invalid";
1212 }
1213
1214 static struct smu_dpm_policy_desc xgmi_plpd_policy_desc = {
1215 .name = STR_XGMI_PLPD_POLICY,
1216 .get_desc = smu_xgmi_plpd_policy_get_desc,
1217 };
1218
smu_cmn_generic_plpd_policy_desc(struct smu_dpm_policy * policy)1219 void smu_cmn_generic_plpd_policy_desc(struct smu_dpm_policy *policy)
1220 {
1221 policy->desc = &xgmi_plpd_policy_desc;
1222 }
1223
smu_cmn_get_backend_workload_mask(struct smu_context * smu,u32 workload_mask,u32 * backend_workload_mask)1224 void smu_cmn_get_backend_workload_mask(struct smu_context *smu,
1225 u32 workload_mask,
1226 u32 *backend_workload_mask)
1227 {
1228 int workload_type;
1229 u32 profile_mode;
1230
1231 *backend_workload_mask = 0;
1232
1233 for (profile_mode = 0; profile_mode < PP_SMC_POWER_PROFILE_COUNT; profile_mode++) {
1234 if (!(workload_mask & (1 << profile_mode)))
1235 continue;
1236
1237 /* conv PP_SMC_POWER_PROFILE* to WORKLOAD_PPLIB_*_BIT */
1238 workload_type = smu_cmn_to_asic_specific_index(smu,
1239 CMN2ASIC_MAPPING_WORKLOAD,
1240 profile_mode);
1241
1242 if (workload_type < 0)
1243 continue;
1244
1245 *backend_workload_mask |= 1 << workload_type;
1246 }
1247 }
1248