summaryrefslogtreecommitdiff
path: root/src/soc/amd/common/block/smu/smu.c
blob: 5e1ad0a737e8d8b4cc98bf87708de798a9ff9759 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* SPDX-License-Identifier: GPL-2.0-only */

#include <timer.h>
#include <console/console.h>
#include <amdblocks/smn.h>
#include <amdblocks/smu.h>
#include <soc/smu.h>
#include <types.h>

#define SMU_MESG_RESP_TIMEOUT	0x00
#define SMU_MESG_RESP_OK	0x01

/* returns SMU_MESG_RESP_OK, SMU_MESG_RESP_TIMEOUT or a negative number */
static int32_t smu_poll_response(bool print_command_duration)
{
	struct stopwatch sw;
	const long timeout_ms = 10 * MSECS_PER_SEC;
	int32_t result;

	stopwatch_init_msecs_expire(&sw, timeout_ms);

	do {
		result = smn_read32(REG_ADDR_MESG_RESP);
		if (result) {
			if (print_command_duration)
				printk(BIOS_SPEW, "SMU command consumed %ld usecs\n",
					stopwatch_duration_usecs(&sw));
			return result;
		}
	} while (!stopwatch_expired(&sw));

	printk(BIOS_ERR, "Error: timeout sending SMU message\n");
	return SMU_MESG_RESP_TIMEOUT;
}

/*
 * Send a message and bi-directional payload to the SMU. SMU response, if any, is returned via
 * *arg.
 */
enum cb_err send_smu_message(enum smu_message_id message_id, struct smu_payload *arg)
{
	size_t i;

	/* wait until SMU can process a new request; don't care if an old request failed */
	if (smu_poll_response(false) == SMU_MESG_RESP_TIMEOUT)
		return CB_ERR;

	/* clear response register */
	smn_write32(REG_ADDR_MESG_RESP, 0);

	/* populate arguments */
	for (i = 0 ; i < SMU_NUM_ARGS ; i++)
		smn_write32(REG_ADDR_MESG_ARG(i), arg->msg[i]);

	/* send message to SMU */
	smn_write32(REG_ADDR_MESG_ID, message_id);

	/* wait until SMU has processed the message and check if it was successful */
	if (smu_poll_response(true) != SMU_MESG_RESP_OK)
		return CB_ERR;

	/* copy returned values */
	for (i = 0 ; i < SMU_NUM_ARGS ; i++)
		arg->msg[i] = smn_read32(REG_ADDR_MESG_ARG(i));

	return CB_SUCCESS;
}