| 1 |
|
| 2 |
|
| 3 |
#include <amdblocks/spi.h> |
| 4 |
#include <boot_device.h> |
| 5 |
#include <commonlib/region.h> |
| 6 |
#include <console/console.h> |
| 7 |
#include <device/mmio.h> |
| 8 |
#include <fmap.h> |
| 9 |
#include <spi_flash.h> |
| 10 |
#include <types.h> |
| 11 |
#include "psp_def.h" |
| 12 |
#include "psp_smi_flash.h" |
| 13 |
|
| 14 |
static bool is_valid_rw_byte_count(struct mbox_psp_cmd_spi_read_write *cmd_buf, |
| 15 |
uint64_t num_bytes) |
| 16 |
{ |
| 17 |
const uint32_t cmd_buf_size = read32(&cmd_buf->header.size); |
| 18 |
const size_t payload_buffer_offset = |
| 19 |
offsetof(struct mbox_psp_cmd_spi_read_write, req) + |
| 20 |
offsetof(struct psp_spi_read_write_request, buffer); |
| 21 |
return num_bytes <= cmd_buf_size - payload_buffer_offset; |
| 22 |
} |
| 23 |
|
| 24 |
static const char *id_to_region_name(uint64_t target_nv_id) |
| 25 |
{ |
| 26 |
switch (target_nv_id) { |
| 27 |
case SMI_TARGET_NVRAM: |
| 28 |
return "PSP_NVRAM"; |
| 29 |
case SMI_TARGET_RPMC_NVRAM: |
| 30 |
return "PSP_RPMC_NVRAM"; |
| 31 |
} |
| 32 |
return NULL; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
static int lookup_store(uint64_t target_nv_id, struct region_device *rstore) |
| 40 |
{ |
| 41 |
|
| 42 |
|
| 43 |
static struct region_device read_rdev, write_rdev; |
| 44 |
static struct incoherent_rdev store_irdev; |
| 45 |
const char *name; |
| 46 |
struct region region; |
| 47 |
const struct region_device *rdev; |
| 48 |
|
| 49 |
name = id_to_region_name(target_nv_id); |
| 50 |
if (!name) |
| 51 |
return -1; |
| 52 |
|
| 53 |
if (fmap_locate_area(name, ®ion) < 0) |
| 54 |
return -1; |
| 55 |
|
| 56 |
if (boot_device_ro_subregion(®ion, &read_rdev) < 0) |
| 57 |
return -1; |
| 58 |
|
| 59 |
if (boot_device_rw_subregion(®ion, &write_rdev) < 0) |
| 60 |
return -1; |
| 61 |
|
| 62 |
rdev = incoherent_rdev_init(&store_irdev, ®ion, &read_rdev, &write_rdev); |
| 63 |
if (rdev == NULL) |
| 64 |
return -1; |
| 65 |
|
| 66 |
return rdev_chain(rstore, rdev, 0, region_device_sz(rdev)); |
| 67 |
} |
| 68 |
|
| 69 |
static enum mbox_p2c_status get_flash_device(const struct spi_flash **flash) |
| 70 |
{ |
| 71 |
*flash = boot_device_spi_flash(); |
| 72 |
if (*flash == NULL) { |
| 73 |
printk(BIOS_ERR, "PSP: Unable to find SPI device\n"); |
| 74 |
return MBOX_PSP_COMMAND_PROCESS_ERROR; |
| 75 |
} |
| 76 |
|
| 77 |
return MBOX_PSP_SUCCESS; |
| 78 |
} |
| 79 |
|
| 80 |
static enum mbox_p2c_status find_psp_spi_flash_device_region(uint64_t target_nv_id, |
| 81 |
struct region_device *store, |
| 82 |
const struct spi_flash **flash) |
| 83 |
{ |
| 84 |
if (get_flash_device(flash) != MBOX_PSP_SUCCESS) |
| 85 |
return MBOX_PSP_COMMAND_PROCESS_ERROR; |
| 86 |
|
| 87 |
if (lookup_store(target_nv_id, store) < 0) { |
| 88 |
printk(BIOS_ERR, "PSP: Unable to find PSP SPI region\n"); |
| 89 |
return MBOX_PSP_COMMAND_PROCESS_ERROR; |
| 90 |
} |
| 91 |
|
| 92 |
return MBOX_PSP_SUCCESS; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
static bool spi_controller_busy(void) |
| 104 |
{ |
| 105 |
bool busy; |
| 106 |
|
| 107 |
|
| 108 |
busy = (spi_read8(SPI_MISC_CNTRL) & SPI_SEMAPHORE_BIOS_LOCKED); |
| 109 |
if (busy) { |
| 110 |
printk(BIOS_NOTICE, "PSP: SPI controller blocked by coreboot (ring 0)\n"); |
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
if (spi_read32(SPI_STATUS) & SPI_BUSY) |
| 119 |
busy = true; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
if (CONFIG(SPI_FLASH) && !busy) { |
| 128 |
const struct spi_flash *spi_flash_dev; |
| 129 |
uint8_t sr1 = 0; |
| 130 |
|
| 131 |
spi_flash_dev = boot_device_spi_flash(); |
| 132 |
assert(spi_flash_dev); |
| 133 |
if (spi_flash_dev) { |
| 134 |
|
| 135 |
if (spi_flash_status(spi_flash_dev, &sr1) < 0) |
| 136 |
busy = true; |
| 137 |
else if (sr1 & BIT(0)) |
| 138 |
busy = true; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
if (CONFIG(SOC_AMD_PICASSO) && !busy) { |
| 143 |
|
| 144 |
busy = (spi_read8(SPI_MISC_CNTRL) & SPI_SEMAPHORE_DRIVER_LOCKED); |
| 145 |
} |
| 146 |
|
| 147 |
if (busy) |
| 148 |
printk(BIOS_NOTICE, "PSP: SPI controller or SPI flash busy\n"); |
| 149 |
|
| 150 |
return busy; |
| 151 |
} |
| 152 |
|
| 153 |
enum mbox_p2c_status psp_smi_spi_get_info(struct mbox_default_buffer *buffer) |
| 154 |
{ |
| 155 |
struct mbox_psp_cmd_spi_info *const cmd_buf = |
| 156 |
(struct mbox_psp_cmd_spi_info *)buffer; |
| 157 |
const struct spi_flash *flash; |
| 158 |
struct region_device store; |
| 159 |
uint64_t target_nv_id; |
| 160 |
uint64_t block_size; |
| 161 |
uint64_t num_blocks; |
| 162 |
enum mbox_p2c_status ret; |
| 163 |
|
| 164 |
printk(BIOS_SPEW, "PSP: SPI info request\n"); |
| 165 |
|
| 166 |
if (!is_valid_psp_spi_info(cmd_buf)) |
| 167 |
return MBOX_PSP_COMMAND_PROCESS_ERROR; |
| 168 |
|
| 169 |
if (spi_controller_busy()) { |
| 170 |
return MBOX_PSP_SPI_BUSY; |
| 171 |
} |
| 172 |
|
| 173 |
target_nv_id = get_psp_spi_info_id(cmd_buf); |
| 174 |
|
| 175 |
ret = find_psp_spi_flash_device_region(target_nv_id, &store, &flash); |
| 176 |
|
| 177 |
if (ret != MBOX_PSP_SUCCESS) |
| 178 |
return ret; |
| 179 |
|
| 180 |
block_size = flash->sector_size; |
| 181 |
|
| 182 |
if (!block_size) |
| 183 |
return MBOX_PSP_COMMAND_PROCESS_ERROR; |
| 184 |
|
| 185 |
num_blocks = region_device_sz(&store) / block_size; |
| 186 |
|
| 187 |
set_psp_spi_info(cmd_buf, 0, block_size, num_blocks); |
| 188 |
|
| 189 |
return MBOX_PSP_SUCCESS; |
| 190 |
} |
| 191 |
|
| 192 |
enum mbox_p2c_status psp_smi_spi_read(struct mbox_default_buffer *buffer) |
| 193 |
{ |
| 194 |
struct mbox_psp_cmd_spi_read_write *const cmd_buf = |
| 195 |
(struct mbox_psp_cmd_spi_read_write *)buffer; |
| 196 |
enum mbox_p2c_status ret; |
| 197 |
uint64_t target_nv_id; |
| 198 |
uint64_t lba; |
| 199 |
uint64_t offset; |
| 200 |
uint64_t num_bytes; |
| 201 |
uint8_t *data; |
| 202 |
size_t addr; |
| 203 |
const struct spi_flash *flash; |
| 204 |
struct region_device store; |
| 205 |
|
| 206 |
printk(BIOS_SPEW, "PSP: SPI read request\n"); |
| 207 |
|
| 208 |
if (!is_valid_psp_spi_read_write(cmd_buf)) |
| 209 |
return MBOX_PSP_COMMAND_PROCESS_ERROR; |
| 210 |
|
| 211 |
if (spi_controller_busy()) { |
| 212 |
return MBOX_PSP_SPI_BUSY; |
| 213 |
} |
| 214 |
|
| 215 |
get_psp_spi_read_write(cmd_buf, &target_nv_id, &lba, &offset, &num_bytes, &data); |
| 216 |
|
| 217 |
if (!is_valid_rw_byte_count(cmd_buf, num_bytes)) { |
| 218 |
printk(BIOS_ERR, "PSP: Read command requested more bytes than we have space " |
| 219 |
"for in the buffer\n"); |
| 220 |
return MBOX_PSP_COMMAND_PROCESS_ERROR; |
| 221 |
} |
| 222 |
|
| 223 |
ret = find_psp_spi_flash_device_region(target_nv_id, &store, &flash); |
| 224 |
|
| 225 |
if (ret != MBOX_PSP_SUCCESS) |
| 226 |
return ret; |
| 227 |
|
| 228 |
addr = (lba * flash->sector_size) + offset; |
| 229 |
|
| 230 |
printk(BIOS_SPEW, "PSP: SPI read 0x%llx bytes at 0x%zx\n", num_bytes, addr); |
| 231 |
|
| 232 |
if (rdev_readat(&store, data, addr, (size_t)num_bytes) != (size_t)num_bytes) { |
| 233 |
printk(BIOS_ERR, "PSP: Failed to read NVRAM data\n"); |
| 234 |
return MBOX_PSP_COMMAND_PROCESS_ERROR; |
| 235 |
} |
| 236 |
|
| 237 |
return MBOX_PSP_SUCCESS; |
| 238 |
} |
| 239 |
|
| 240 |
enum mbox_p2c_status psp_smi_spi_write(struct mbox_default_buffer *buffer) |
| 241 |
{ |
| 242 |
struct mbox_psp_cmd_spi_read_write *const cmd_buf = |
| 243 |
(struct mbox_psp_cmd_spi_read_write *)buffer; |
| 244 |
enum mbox_p2c_status ret; |
| 245 |
uint64_t target_nv_id; |
| 246 |
uint64_t lba; |
| 247 |
uint64_t offset; |
| 248 |
uint64_t num_bytes; |
| 249 |
uint8_t *data; |
| 250 |
size_t addr; |
| 251 |
const struct spi_flash *flash; |
| 252 |
struct region_device store; |
| 253 |
|
| 254 |
printk(BIOS_SPEW, "PSP: SPI write request\n"); |
| 255 |
|
| 256 |
if (!is_valid_psp_spi_read_write(cmd_buf)) |
| 257 |
return MBOX_PSP_COMMAND_PROCESS_ERROR; |
| 258 |
|
| 259 |
if (spi_controller_busy()) { |
| 260 |
return MBOX_PSP_SPI_BUSY; |
| 261 |
} |
| 262 |
|
| 263 |
get_psp_spi_read_write(cmd_buf, &target_nv_id, &lba, &offset, &num_bytes, &data); |
| 264 |
|
| 265 |
if (!is_valid_rw_byte_count(cmd_buf, num_bytes)) { |
| 266 |
printk(BIOS_ERR, "PSP: Write command contains more bytes than we have space " |
| 267 |
"for in the buffer\n"); |
| 268 |
return MBOX_PSP_COMMAND_PROCESS_ERROR; |
| 269 |
} |
| 270 |
|
| 271 |
ret = find_psp_spi_flash_device_region(target_nv_id, &store, &flash); |
| 272 |
|
| 273 |
if (ret != MBOX_PSP_SUCCESS) |
| 274 |
return ret; |
| 275 |
|
| 276 |
addr = (lba * flash->sector_size) + offset; |
| 277 |
|
| 278 |
printk(BIOS_SPEW, "PSP: SPI write 0x%llx bytes at 0x%zx\n", num_bytes, addr); |
| 279 |
|
| 280 |
if (rdev_writeat(&store, data, addr, (size_t)num_bytes) != (size_t)num_bytes) { |
| 281 |
printk(BIOS_ERR, "PSP: Failed to write NVRAM data\n"); |
| 282 |
return MBOX_PSP_COMMAND_PROCESS_ERROR; |
| 283 |
} |
| 284 |
|
| 285 |
return MBOX_PSP_SUCCESS; |
| 286 |
} |
| 287 |
|
| 288 |
enum mbox_p2c_status psp_smi_spi_erase(struct mbox_default_buffer *buffer) |
| 289 |
{ |
| 290 |
struct mbox_psp_cmd_spi_erase *const cmd_buf = |
| 291 |
(struct mbox_psp_cmd_spi_erase *)buffer; |
| 292 |
enum mbox_p2c_status ret; |
| 293 |
uint64_t target_nv_id; |
| 294 |
uint64_t lba; |
| 295 |
uint64_t num_blocks; |
| 296 |
size_t addr; |
| 297 |
size_t num_bytes; |
| 298 |
const struct spi_flash *flash; |
| 299 |
struct region_device store; |
| 300 |
|
| 301 |
printk(BIOS_SPEW, "PSP: SPI erase request\n"); |
| 302 |
|
| 303 |
if (!is_valid_psp_spi_erase(cmd_buf)) |
| 304 |
return MBOX_PSP_COMMAND_PROCESS_ERROR; |
| 305 |
|
| 306 |
if (spi_controller_busy()) { |
| 307 |
return MBOX_PSP_SPI_BUSY; |
| 308 |
} |
| 309 |
|
| 310 |
get_psp_spi_erase(cmd_buf, &target_nv_id, &lba, &num_blocks); |
| 311 |
|
| 312 |
ret = find_psp_spi_flash_device_region(target_nv_id, &store, &flash); |
| 313 |
|
| 314 |
if (ret != MBOX_PSP_SUCCESS) |
| 315 |
return ret; |
| 316 |
|
| 317 |
addr = lba * flash->sector_size; |
| 318 |
num_bytes = (size_t)num_blocks * flash->sector_size; |
| 319 |
|
| 320 |
printk(BIOS_SPEW, "PSP: SPI erase 0x%zx bytes at 0x%zx\n", num_bytes, addr); |
| 321 |
|
| 322 |
if (rdev_eraseat(&store, addr, num_bytes) != num_bytes) { |
| 323 |
printk(BIOS_ERR, "PSP: Failed to erase SPI NVRAM data\n"); |
| 324 |
return MBOX_PSP_COMMAND_PROCESS_ERROR; |
| 325 |
} |
| 326 |
|
| 327 |
return MBOX_PSP_SUCCESS; |
| 328 |
} |
| 329 |
|
| 330 |
enum mbox_p2c_status psp_smi_spi_rpmc_inc_mc(struct mbox_default_buffer *buffer) |
| 331 |
{ |
| 332 |
struct mbox_psp_cmd_spi_rpmc_inc_mc *const cmd_buf = |
| 333 |
(struct mbox_psp_cmd_spi_rpmc_inc_mc *)buffer; |
| 334 |
const struct spi_flash *flash; |
| 335 |
|
| 336 |
printk(BIOS_SPEW, "PSP: SPI RPMC increment monotonic counter request\n"); |
| 337 |
|
| 338 |
if (!CONFIG(SOC_AMD_COMMON_BLOCK_PSP_RPMC)) |
| 339 |
return MBOX_PSP_UNSUPPORTED; |
| 340 |
|
| 341 |
if (spi_controller_busy()) { |
| 342 |
return MBOX_PSP_SPI_BUSY; |
| 343 |
} |
| 344 |
|
| 345 |
if (get_flash_device(&flash) != MBOX_PSP_SUCCESS) |
| 346 |
return MBOX_PSP_COMMAND_PROCESS_ERROR; |
| 347 |
|
| 348 |
if (spi_flash_rpmc_increment(flash, cmd_buf->req.counter_address, |
| 349 |
cmd_buf->req.counter_data, cmd_buf->req.signature) |
| 350 |
!= CB_SUCCESS) |
| 351 |
return MBOX_PSP_COMMAND_PROCESS_ERROR; |
| 352 |
|
| 353 |
return MBOX_PSP_SUCCESS; |
| 354 |
} |
| 355 |
|
| 356 |
enum mbox_p2c_status psp_smi_spi_rpmc_req_mc(struct mbox_default_buffer *buffer) |
| 357 |
{ |
| 358 |
struct mbox_psp_cmd_spi_rpmc_req_mc *const cmd_buf = |
| 359 |
(struct mbox_psp_cmd_spi_rpmc_req_mc *)buffer; |
| 360 |
const struct spi_flash *flash; |
| 361 |
|
| 362 |
printk(BIOS_SPEW, "PSP: SPI RPMC request monotonic counter request\n"); |
| 363 |
|
| 364 |
if (!CONFIG(SOC_AMD_COMMON_BLOCK_PSP_RPMC)) |
| 365 |
return MBOX_PSP_UNSUPPORTED; |
| 366 |
|
| 367 |
if (spi_controller_busy()) { |
| 368 |
return MBOX_PSP_SPI_BUSY; |
| 369 |
} |
| 370 |
|
| 371 |
if (get_flash_device(&flash) != MBOX_PSP_SUCCESS) |
| 372 |
return MBOX_PSP_COMMAND_PROCESS_ERROR; |
| 373 |
|
| 374 |
if (spi_flash_rpmc_request(flash, cmd_buf->req.counter_address, cmd_buf->req.tag, |
| 375 |
cmd_buf->req.signature, cmd_buf->req.output_counter_data, |
| 376 |
cmd_buf->req.output_signature) != CB_SUCCESS) |
| 377 |
return MBOX_PSP_COMMAND_PROCESS_ERROR; |
| 378 |
|
| 379 |
return MBOX_PSP_SUCCESS; |
| 380 |
} |