blob: d61a9147dce809fcaf3094e0736a07ac51039f32 [file] [log] [blame]
Marshall Dawson68243a52017-06-15 16:59:20 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2017 Advanced Micro Devices, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <arch/io.h>
17#include <delay.h>
18#include <cbmem.h>
19#include <timer.h>
20#include <device/pci_def.h>
21#include <console/console.h>
22#include <amdblocks/psp.h>
23
24static const char *psp_status_nobase = "error: PSP BAR3 not assigned";
25static const char *psp_status_halted = "error: PSP in halted state";
26static const char *psp_status_recovery = "error: PSP recovery required";
27static const char *psp_status_errcmd = "error sending command";
28static const char *psp_status_init_timeout = "error: PSP init timeout";
29static const char *psp_status_cmd_timeout = "error: PSP command timeout";
30static const char *psp_status_noerror = "";
31
32static const char *status_to_string(int err)
33{
34 switch (err) {
35 case -PSPSTS_NOBASE:
36 return psp_status_nobase;
37 case -PSPSTS_HALTED:
38 return psp_status_halted;
39 case -PSPSTS_RECOVERY:
40 return psp_status_recovery;
41 case -PSPSTS_SEND_ERROR:
42 return psp_status_errcmd;
43 case -PSPSTS_INIT_TIMEOUT:
44 return psp_status_init_timeout;
45 case -PSPSTS_CMD_TIMEOUT:
46 return psp_status_cmd_timeout;
47 default:
48 return psp_status_noerror;
49 }
50}
51
52static struct psp_mbox *get_mbox_address(void)
53{
54 UINT32 base; /* UINT32 for compatibility with PspBaseLib */
55 BOOLEAN bar3_status;
56 uintptr_t baseptr;
57
58 bar3_status = GetPspBar3Addr(&base);
59 if (!bar3_status) {
60 PspBarInitEarly();
61 bar3_status = GetPspBar3Addr(&base);
62 }
63 if (!bar3_status)
64 return NULL;
65
66 baseptr = base;
67 return (struct psp_mbox *)(baseptr + PSP_MAILBOX_BASE);
68}
69
70static u32 rd_mbox_sts(struct psp_mbox *mbox)
71{
72 return read32(&mbox->mbox_status);
73}
74
75static void wr_mbox_cmd(struct psp_mbox *mbox, u32 cmd)
76{
77 write32(&mbox->mbox_command, cmd);
78}
79
80static u32 rd_mbox_cmd(struct psp_mbox *mbox)
81{
82 return read32(&mbox->mbox_command);
83}
84
85static void wr_mbox_cmd_resp(struct psp_mbox *mbox, void *buffer)
86{
87 write64(&mbox->cmd_response, (uintptr_t)buffer);
88}
89
90static u32 rd_resp_sts(struct mbox_default_buffer *buffer)
91{
92 return read32(&buffer->header.status);
93}
94
95static int wait_initialized(struct psp_mbox *mbox)
96{
97 struct stopwatch sw;
98
99 stopwatch_init_msecs_expire(&sw, PSP_INIT_TIMEOUT);
100
101 do {
102 if (rd_mbox_sts(mbox) & STATUS_INITIALIZED)
103 return 0;
104 } while (!stopwatch_expired(&sw));
105
106 return -PSPSTS_INIT_TIMEOUT;
107}
108
109static int wait_command(struct psp_mbox *mbox)
110{
111 struct stopwatch sw;
112
113 stopwatch_init_msecs_expire(&sw, PSP_CMD_TIMEOUT);
114
115 do {
116 if (!rd_mbox_cmd(mbox))
117 return 0;
118 } while (!stopwatch_expired(&sw));
119
120 return -PSPSTS_CMD_TIMEOUT;
121}
122
123static int send_psp_command(u32 command, void *buffer)
124{
125 u32 command_reg;
126 int status = 0;
127
128 struct psp_mbox *mbox = get_mbox_address();
129 if (!mbox)
130 return -PSPSTS_NOBASE;
131
132 command_reg = pci_read_config32(PSP_DEV, PCI_COMMAND);
133 pci_write_config32(PSP_DEV, PCI_COMMAND, command_reg |
134 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
135
136 /* check for PSP error conditions */
137 if (rd_mbox_sts(mbox) & STATUS_HALT) {
138 status = -PSPSTS_HALTED;
139 goto exit;
140 }
141 if (rd_mbox_sts(mbox) & STATUS_RECOVERY) {
142 status = -PSPSTS_RECOVERY;
143 goto exit;
144 }
145
146 /* PSP must be finished with init and ready to accept a command */
147 if (wait_initialized(mbox)) {
148 status = -PSPSTS_INIT_TIMEOUT;
149 goto exit;
150 }
151 if (wait_command(mbox)) {
152 status = -PSPSTS_CMD_TIMEOUT;
153 goto exit;
154 }
155
156 /* set address of command-response buffer and write command register */
157 wr_mbox_cmd_resp(mbox, buffer);
158 wr_mbox_cmd(mbox, command);
159
160 /* PSP clears command register when complete */
161 if (wait_command(mbox)) {
162 status = -PSPSTS_CMD_TIMEOUT;
163 goto exit;
164 }
165
166 /* check delivery status */
167 if (rd_mbox_sts(mbox) & (STATUS_ERROR | STATUS_TERMINATED)) {
168 status = -PSPSTS_SEND_ERROR;
169 goto exit;
170 }
171exit:
172 /* restore command register to original value */
173 pci_write_config32(PSP_DEV, PCI_COMMAND, command_reg);
174 return status;
175}
176
177/*
178 * Notify the PSP that DRAM is present. Upon receiving this command, the PSP
179 * will load its OS into fenced DRAM that is not accessible to the x86 cores.
180 */
181int psp_notify_dram(void)
182{
183 struct mbox_default_buffer buffer;
184 int cmd_status;
185
186 printk(BIOS_DEBUG, "PSP: Notify that DRAM is available... ");
187
188 buffer.header.size = sizeof(struct mbox_default_buffer);
189 buffer.header.status = 0; /* PSP does not report status for this cmd */
190
191 cmd_status = send_psp_command(MBOX_BIOS_CMD_DRAM_INFO, &buffer);
192
193 /* buffer's status shouldn't change but report it if it does */
194 if (rd_resp_sts(&buffer))
195 printk(BIOS_DEBUG, "buffer status=0x%x ",
196 rd_resp_sts(&buffer));
197 if (cmd_status)
198 printk(BIOS_DEBUG, "%s\n", status_to_string(cmd_status));
199 else
200 printk(BIOS_DEBUG, "OK\n");
201
202 return cmd_status;
203}