1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <commonlib/bsd/helpers.h>
4 #include <cpu/x86/save_state.h>
5 #include <cpu/x86/smm.h>
6 #include <types.h>
7
8 /* These are weakly linked such that platforms can link only the save state
9 ops they actually require. */
10 const struct smm_save_state_ops *legacy_ops __weak = NULL;
11 const struct smm_save_state_ops *em64t100_ops __weak = NULL;
12 const struct smm_save_state_ops *em64t101_ops __weak = NULL;
13 const struct smm_save_state_ops *amd64_ops __weak = NULL;
14
15 static const struct smm_save_state_ops *save_state;
16
17 /* Returns -1 on failure, 0 on success */
init_save_state(void)18 static int init_save_state(void)
19 {
20 const uint32_t revision = smm_revision();
21 int i;
22 static bool initialized = false;
23 const struct smm_save_state_ops *save_state_ops[] = {
24 legacy_ops,
25 em64t100_ops,
26 em64t101_ops,
27 amd64_ops,
28 };
29
30 if (initialized)
31 return 0;
32
33 for (i = 0; i < ARRAY_SIZE(save_state_ops); i++) {
34 const struct smm_save_state_ops *ops = save_state_ops[i];
35 const uint32_t *rev;
36
37 if (ops == NULL)
38 continue;
39
40 for (rev = ops->revision_table; *rev != SMM_REV_INVALID; rev++)
41 if (*rev == revision) {
42 save_state = ops;
43 initialized = true;
44 return 0;
45 }
46 }
47
48 return -1;
49 }
50
get_apmc_node(u8 cmd)51 int get_apmc_node(u8 cmd)
52 {
53 if (init_save_state())
54 return -1;
55
56 return save_state->apmc_node(cmd);
57 }
58
get_save_state_reg(const enum cpu_reg reg,const int node,void * out,const uint8_t length)59 int get_save_state_reg(const enum cpu_reg reg, const int node, void *out, const uint8_t length)
60 {
61 if (init_save_state())
62 return -1;
63
64 if (node > CONFIG_MAX_CPUS)
65 return -1;
66
67 return save_state->get_reg(reg, node, out, length);
68 }
69
set_save_state_reg(const enum cpu_reg reg,const int node,void * in,const uint8_t length)70 int set_save_state_reg(const enum cpu_reg reg, const int node, void *in, const uint8_t length)
71 {
72 if (init_save_state())
73 return -1;
74
75 if (node > CONFIG_MAX_CPUS)
76 return -1;
77
78 return save_state->set_reg(reg, node, in, length);
79 }
80