summaryrefslogtreecommitdiff
path: root/src/cpu/x86/smm/save_state.c
blob: d8c44a85e0fb938cb1adb83d59d79c74c4b87069 (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
68
69
70
71
72
73
74
75
76
77
78
79
/* SPDX-License-Identifier: GPL-2.0-only */

#include <commonlib/bsd/helpers.h>
#include <cpu/x86/save_state.h>
#include <cpu/x86/smm.h>
#include <types.h>

/* These are weakly linked such that platforms can link only the save state
   ops they actually require. */
const struct smm_save_state_ops *legacy_ops __weak = NULL;
const struct smm_save_state_ops *em64t100_ops __weak = NULL;
const struct smm_save_state_ops *em64t101_ops __weak = NULL;
const struct smm_save_state_ops *amd64_ops __weak = NULL;

static const struct smm_save_state_ops *save_state;

/* Returns -1 on failure, 0 on success */
static int init_save_state(void)
{
	const uint32_t revision = smm_revision();
	int i;
	static bool initialized = false;
	const struct smm_save_state_ops *save_state_ops[] = {
		legacy_ops,
		em64t100_ops,
		em64t101_ops,
		amd64_ops,
	};

	if (initialized)
		return 0;

	for (i = 0; i < ARRAY_SIZE(save_state_ops); i++) {
		const struct smm_save_state_ops *ops = save_state_ops[i];
		const uint32_t *rev;

		if (ops == NULL)
			continue;

		for (rev = ops->revision_table; *rev != SMM_REV_INVALID; rev++)
			if (*rev == revision) {
				save_state = ops;
				initialized = true;
				return 0;
			}
	}

	return -1;
}

int get_apmc_node(u8 cmd)
{
	if (init_save_state())
		return -1;

	return save_state->apmc_node(cmd);
}

int get_save_state_reg(const enum cpu_reg reg, const int node, void *out, const uint8_t length)
{
	if (init_save_state())
		return -1;

	if (node > CONFIG_MAX_CPUS)
		return -1;

	return save_state->get_reg(reg, node, out, length);
}

int set_save_state_reg(const enum cpu_reg reg, const int node, void *in, const uint8_t length)
{
	if (init_save_state())
		return -1;

	if (node > CONFIG_MAX_CPUS)
		return -1;

	return save_state->set_reg(reg, node, in, length);
}