1 /* 2 * Copyright (c) 2019-2022, ARM Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <assert.h> 8 #include <common/debug.h> 9 #include <common/runtime_svc.h> 10 #include <lib/mmio.h> 11 #include <tools_share/uuid.h> 12 13 #include "socfpga_fcs.h" 14 #include "socfpga_mailbox.h" 15 #include "socfpga_reset_manager.h" 16 #include "socfpga_sip_svc.h" 17 18 19 /* Total buffer the driver can hold */ 20 #define FPGA_CONFIG_BUFFER_SIZE 4 21 22 static int current_block, current_buffer; 23 static int read_block, max_blocks; 24 static uint32_t send_id, rcv_id; 25 static uint32_t bytes_per_block, blocks_submitted; 26 static bool is_full_reconfig; 27 28 29 /* SiP Service UUID */ 30 DEFINE_SVC_UUID2(intl_svc_uid, 31 0xa85273b0, 0xe85a, 0x4862, 0xa6, 0x2a, 32 0xfa, 0x88, 0x88, 0x17, 0x68, 0x81); 33 34 static uint64_t socfpga_sip_handler(uint32_t smc_fid, 35 uint64_t x1, 36 uint64_t x2, 37 uint64_t x3, 38 uint64_t x4, 39 void *cookie, 40 void *handle, 41 uint64_t flags) 42 { 43 ERROR("%s: unhandled SMC (0x%x)\n", __func__, smc_fid); 44 SMC_RET1(handle, SMC_UNK); 45 } 46 47 struct fpga_config_info fpga_config_buffers[FPGA_CONFIG_BUFFER_SIZE]; 48 49 static int intel_fpga_sdm_write_buffer(struct fpga_config_info *buffer) 50 { 51 uint32_t args[3]; 52 53 while (max_blocks > 0 && buffer->size > buffer->size_written) { 54 args[0] = (1<<8); 55 args[1] = buffer->addr + buffer->size_written; 56 if (buffer->size - buffer->size_written <= bytes_per_block) { 57 args[2] = buffer->size - buffer->size_written; 58 current_buffer++; 59 current_buffer %= FPGA_CONFIG_BUFFER_SIZE; 60 } else 61 args[2] = bytes_per_block; 62 63 buffer->size_written += args[2]; 64 mailbox_send_cmd_async(&send_id, MBOX_RECONFIG_DATA, args, 65 3U, CMD_INDIRECT); 66 67 buffer->subblocks_sent++; 68 max_blocks--; 69 } 70 71 return !max_blocks; 72 } 73 74 static int intel_fpga_sdm_write_all(void) 75 { 76 for (int i = 0; i < FPGA_CONFIG_BUFFER_SIZE; i++) 77 if (intel_fpga_sdm_write_buffer( 78 &fpga_config_buffers[current_buffer])) 79 break; 80 return 0; 81 } 82 83 static uint32_t intel_mailbox_fpga_config_isdone(uint32_t query_type) 84 { 85 uint32_t ret; 86 87 if (query_type == 1) 88 ret = intel_mailbox_get_config_status(MBOX_CONFIG_STATUS, false); 89 else 90 ret = intel_mailbox_get_config_status(MBOX_RECONFIG_STATUS, true); 91 92 if (ret) { 93 if (ret == MBOX_CFGSTAT_STATE_CONFIG) 94 return INTEL_SIP_SMC_STATUS_BUSY; 95 else 96 return INTEL_SIP_SMC_STATUS_ERROR; 97 } 98 99 if (query_type != 1) { 100 /* full reconfiguration */ 101 if (is_full_reconfig) 102 socfpga_bridges_enable(); /* Enable bridge */ 103 } 104 105 return INTEL_SIP_SMC_STATUS_OK; 106 } 107 108 static int mark_last_buffer_xfer_completed(uint32_t *buffer_addr_completed) 109 { 110 int i; 111 112 for (i = 0; i < FPGA_CONFIG_BUFFER_SIZE; i++) { 113 if (fpga_config_buffers[i].block_number == current_block) { 114 fpga_config_buffers[i].subblocks_sent--; 115 if (fpga_config_buffers[i].subblocks_sent == 0 116 && fpga_config_buffers[i].size <= 117 fpga_config_buffers[i].size_written) { 118 fpga_config_buffers[i].write_requested = 0; 119 current_block++; 120 *buffer_addr_completed = 121 fpga_config_buffers[i].addr; 122 return 0; 123 } 124 } 125 } 126 127 return -1; 128 } 129 130 static int intel_fpga_config_completed_write(uint32_t *completed_addr, 131 uint32_t *count, uint32_t *job_id) 132 { 133 uint32_t resp[5]; 134 unsigned int resp_len = ARRAY_SIZE(resp); 135 int status = INTEL_SIP_SMC_STATUS_OK; 136 int all_completed = 1; 137 *count = 0; 138 139 while (*count < 3) { 140 141 status = mailbox_read_response(job_id, 142 resp, &resp_len); 143 144 if (status < 0) { 145 break; 146 } 147 148 max_blocks++; 149 150 if (mark_last_buffer_xfer_completed( 151 &completed_addr[*count]) == 0) { 152 *count = *count + 1; 153 } else { 154 break; 155 } 156 } 157 158 if (*count <= 0) { 159 if (status != MBOX_NO_RESPONSE && 160 status != MBOX_TIMEOUT && resp_len != 0) { 161 mailbox_clear_response(); 162 return INTEL_SIP_SMC_STATUS_ERROR; 163 } 164 165 *count = 0; 166 } 167 168 intel_fpga_sdm_write_all(); 169 170 if (*count > 0) 171 status = INTEL_SIP_SMC_STATUS_OK; 172 else if (*count == 0) 173 status = INTEL_SIP_SMC_STATUS_BUSY; 174 175 for (int i = 0; i < FPGA_CONFIG_BUFFER_SIZE; i++) { 176 if (fpga_config_buffers[i].write_requested != 0) { 177 all_completed = 0; 178 break; 179 } 180 } 181 182 if (all_completed == 1) 183 return INTEL_SIP_SMC_STATUS_OK; 184 185 return status; 186 } 187 188 static int intel_fpga_config_start(uint32_t type) 189 { 190 uint32_t argument = 0x1; 191 uint32_t response[3]; 192 int status = 0; 193 unsigned int size = 0; 194 unsigned int resp_len = ARRAY_SIZE(response); 195 196 if ((config_type)type == FULL_CONFIG) { 197 is_full_reconfig = true; 198 } 199 200 mailbox_clear_response(); 201 202 mailbox_send_cmd(MBOX_JOB_ID, MBOX_CMD_CANCEL, NULL, 0U, 203 CMD_CASUAL, NULL, NULL); 204 205 status = mailbox_send_cmd(MBOX_JOB_ID, MBOX_RECONFIG, &argument, size, 206 CMD_CASUAL, response, &resp_len); 207 208 if (status < 0) 209 return status; 210 211 max_blocks = response[0]; 212 bytes_per_block = response[1]; 213 214 for (int i = 0; i < FPGA_CONFIG_BUFFER_SIZE; i++) { 215 fpga_config_buffers[i].size = 0; 216 fpga_config_buffers[i].size_written = 0; 217 fpga_config_buffers[i].addr = 0; 218 fpga_config_buffers[i].write_requested = 0; 219 fpga_config_buffers[i].block_number = 0; 220 fpga_config_buffers[i].subblocks_sent = 0; 221 } 222 223 blocks_submitted = 0; 224 current_block = 0; 225 read_block = 0; 226 current_buffer = 0; 227 228 /* full reconfiguration */ 229 if (is_full_reconfig) { 230 /* Disable bridge */ 231 socfpga_bridges_disable(); 232 } 233 234 return 0; 235 } 236 237 static bool is_fpga_config_buffer_full(void) 238 { 239 for (int i = 0; i < FPGA_CONFIG_BUFFER_SIZE; i++) 240 if (!fpga_config_buffers[i].write_requested) 241 return false; 242 return true; 243 } 244 245 bool is_address_in_ddr_range(uint64_t addr, uint64_t size) 246 { 247 if (!addr && !size) { 248 return true; 249 } 250 if (size > (UINT64_MAX - addr)) 251 return false; 252 if (addr < BL31_LIMIT) 253 return false; 254 if (addr + size > DRAM_BASE + DRAM_SIZE) 255 return false; 256 257 return true; 258 } 259 260 static uint32_t intel_fpga_config_write(uint64_t mem, uint64_t size) 261 { 262 int i; 263 264 intel_fpga_sdm_write_all(); 265 266 if (!is_address_in_ddr_range(mem, size) || 267 is_fpga_config_buffer_full()) 268 return INTEL_SIP_SMC_STATUS_REJECTED; 269 270 for (i = 0; i < FPGA_CONFIG_BUFFER_SIZE; i++) { 271 int j = (i + current_buffer) % FPGA_CONFIG_BUFFER_SIZE; 272 273 if (!fpga_config_buffers[j].write_requested) { 274 fpga_config_buffers[j].addr = mem; 275 fpga_config_buffers[j].size = size; 276 fpga_config_buffers[j].size_written = 0; 277 fpga_config_buffers[j].write_requested = 1; 278 fpga_config_buffers[j].block_number = 279 blocks_submitted++; 280 fpga_config_buffers[j].subblocks_sent = 0; 281 break; 282 } 283 } 284 285 if (is_fpga_config_buffer_full()) 286 return INTEL_SIP_SMC_STATUS_BUSY; 287 288 return INTEL_SIP_SMC_STATUS_OK; 289 } 290 291 static int is_out_of_sec_range(uint64_t reg_addr) 292 { 293 switch (reg_addr) { 294 case(0xF8011100): /* ECCCTRL1 */ 295 case(0xF8011104): /* ECCCTRL2 */ 296 case(0xF8011110): /* ERRINTEN */ 297 case(0xF8011114): /* ERRINTENS */ 298 case(0xF8011118): /* ERRINTENR */ 299 case(0xF801111C): /* INTMODE */ 300 case(0xF8011120): /* INTSTAT */ 301 case(0xF8011124): /* DIAGINTTEST */ 302 case(0xF801112C): /* DERRADDRA */ 303 case(0xFFD12028): /* SDMMCGRP_CTRL */ 304 case(0xFFD12044): /* EMAC0 */ 305 case(0xFFD12048): /* EMAC1 */ 306 case(0xFFD1204C): /* EMAC2 */ 307 case(0xFFD12090): /* ECC_INT_MASK_VALUE */ 308 case(0xFFD12094): /* ECC_INT_MASK_SET */ 309 case(0xFFD12098): /* ECC_INT_MASK_CLEAR */ 310 case(0xFFD1209C): /* ECC_INTSTATUS_SERR */ 311 case(0xFFD120A0): /* ECC_INTSTATUS_DERR */ 312 case(0xFFD120C0): /* NOC_TIMEOUT */ 313 case(0xFFD120C4): /* NOC_IDLEREQ_SET */ 314 case(0xFFD120C8): /* NOC_IDLEREQ_CLR */ 315 case(0xFFD120D0): /* NOC_IDLEACK */ 316 case(0xFFD120D4): /* NOC_IDLESTATUS */ 317 case(0xFFD12200): /* BOOT_SCRATCH_COLD0 */ 318 case(0xFFD12204): /* BOOT_SCRATCH_COLD1 */ 319 case(0xFFD12220): /* BOOT_SCRATCH_COLD8 */ 320 case(0xFFD12224): /* BOOT_SCRATCH_COLD9 */ 321 return 0; 322 323 default: 324 break; 325 } 326 327 return -1; 328 } 329 330 /* Secure register access */ 331 uint32_t intel_secure_reg_read(uint64_t reg_addr, uint32_t *retval) 332 { 333 if (is_out_of_sec_range(reg_addr)) 334 return INTEL_SIP_SMC_STATUS_ERROR; 335 336 *retval = mmio_read_32(reg_addr); 337 338 return INTEL_SIP_SMC_STATUS_OK; 339 } 340 341 uint32_t intel_secure_reg_write(uint64_t reg_addr, uint32_t val, 342 uint32_t *retval) 343 { 344 if (is_out_of_sec_range(reg_addr)) 345 return INTEL_SIP_SMC_STATUS_ERROR; 346 347 mmio_write_32(reg_addr, val); 348 349 return intel_secure_reg_read(reg_addr, retval); 350 } 351 352 uint32_t intel_secure_reg_update(uint64_t reg_addr, uint32_t mask, 353 uint32_t val, uint32_t *retval) 354 { 355 if (!intel_secure_reg_read(reg_addr, retval)) { 356 *retval &= ~mask; 357 *retval |= val & mask; 358 return intel_secure_reg_write(reg_addr, *retval, retval); 359 } 360 361 return INTEL_SIP_SMC_STATUS_ERROR; 362 } 363 364 /* Intel Remote System Update (RSU) services */ 365 uint64_t intel_rsu_update_address; 366 367 static uint32_t intel_rsu_status(uint64_t *respbuf, unsigned int respbuf_sz) 368 { 369 if (mailbox_rsu_status((uint32_t *)respbuf, respbuf_sz) < 0) 370 return INTEL_SIP_SMC_RSU_ERROR; 371 372 return INTEL_SIP_SMC_STATUS_OK; 373 } 374 375 static uint32_t intel_rsu_update(uint64_t update_address) 376 { 377 intel_rsu_update_address = update_address; 378 return INTEL_SIP_SMC_STATUS_OK; 379 } 380 381 static uint32_t intel_rsu_notify(uint32_t execution_stage) 382 { 383 if (mailbox_hps_stage_notify(execution_stage) < 0) 384 return INTEL_SIP_SMC_RSU_ERROR; 385 386 return INTEL_SIP_SMC_STATUS_OK; 387 } 388 389 static uint32_t intel_rsu_retry_counter(uint32_t *respbuf, uint32_t respbuf_sz, 390 uint32_t *ret_stat) 391 { 392 if (mailbox_rsu_status((uint32_t *)respbuf, respbuf_sz) < 0) 393 return INTEL_SIP_SMC_RSU_ERROR; 394 395 *ret_stat = respbuf[8]; 396 return INTEL_SIP_SMC_STATUS_OK; 397 } 398 399 /* Mailbox services */ 400 static uint32_t intel_mbox_send_cmd(uint32_t cmd, uint32_t *args, 401 unsigned int len, 402 uint32_t urgent, uint32_t *response, 403 unsigned int resp_len, int *mbox_status, 404 unsigned int *len_in_resp) 405 { 406 *len_in_resp = 0; 407 *mbox_status = 0; 408 409 if (!is_address_in_ddr_range((uint64_t)args, sizeof(uint32_t) * len)) 410 return INTEL_SIP_SMC_STATUS_REJECTED; 411 412 int status = mailbox_send_cmd(MBOX_JOB_ID, cmd, args, len, urgent, 413 response, &resp_len); 414 415 if (status < 0) { 416 *mbox_status = -status; 417 return INTEL_SIP_SMC_STATUS_ERROR; 418 } 419 420 *mbox_status = 0; 421 *len_in_resp = resp_len; 422 return INTEL_SIP_SMC_STATUS_OK; 423 } 424 425 /* 426 * This function is responsible for handling all SiP calls from the NS world 427 */ 428 429 uintptr_t sip_smc_handler(uint32_t smc_fid, 430 u_register_t x1, 431 u_register_t x2, 432 u_register_t x3, 433 u_register_t x4, 434 void *cookie, 435 void *handle, 436 u_register_t flags) 437 { 438 uint32_t retval = 0; 439 uint32_t mbox_error = 0; 440 uint32_t completed_addr[3]; 441 uint64_t retval64, rsu_respbuf[9]; 442 int status = INTEL_SIP_SMC_STATUS_OK; 443 int mbox_status; 444 unsigned int len_in_resp; 445 u_register_t x5, x6; 446 447 switch (smc_fid) { 448 case SIP_SVC_UID: 449 /* Return UID to the caller */ 450 SMC_UUID_RET(handle, intl_svc_uid); 451 452 case INTEL_SIP_SMC_FPGA_CONFIG_ISDONE: 453 status = intel_mailbox_fpga_config_isdone(x1); 454 SMC_RET4(handle, status, 0, 0, 0); 455 456 case INTEL_SIP_SMC_FPGA_CONFIG_GET_MEM: 457 SMC_RET3(handle, INTEL_SIP_SMC_STATUS_OK, 458 INTEL_SIP_SMC_FPGA_CONFIG_ADDR, 459 INTEL_SIP_SMC_FPGA_CONFIG_SIZE - 460 INTEL_SIP_SMC_FPGA_CONFIG_ADDR); 461 462 case INTEL_SIP_SMC_FPGA_CONFIG_START: 463 status = intel_fpga_config_start(x1); 464 SMC_RET4(handle, status, 0, 0, 0); 465 466 case INTEL_SIP_SMC_FPGA_CONFIG_WRITE: 467 status = intel_fpga_config_write(x1, x2); 468 SMC_RET4(handle, status, 0, 0, 0); 469 470 case INTEL_SIP_SMC_FPGA_CONFIG_COMPLETED_WRITE: 471 status = intel_fpga_config_completed_write(completed_addr, 472 &retval, &rcv_id); 473 switch (retval) { 474 case 1: 475 SMC_RET4(handle, INTEL_SIP_SMC_STATUS_OK, 476 completed_addr[0], 0, 0); 477 478 case 2: 479 SMC_RET4(handle, INTEL_SIP_SMC_STATUS_OK, 480 completed_addr[0], 481 completed_addr[1], 0); 482 483 case 3: 484 SMC_RET4(handle, INTEL_SIP_SMC_STATUS_OK, 485 completed_addr[0], 486 completed_addr[1], 487 completed_addr[2]); 488 489 case 0: 490 SMC_RET4(handle, status, 0, 0, 0); 491 492 default: 493 mailbox_clear_response(); 494 SMC_RET1(handle, INTEL_SIP_SMC_STATUS_ERROR); 495 } 496 497 case INTEL_SIP_SMC_REG_READ: 498 status = intel_secure_reg_read(x1, &retval); 499 SMC_RET3(handle, status, retval, x1); 500 501 case INTEL_SIP_SMC_REG_WRITE: 502 status = intel_secure_reg_write(x1, (uint32_t)x2, &retval); 503 SMC_RET3(handle, status, retval, x1); 504 505 case INTEL_SIP_SMC_REG_UPDATE: 506 status = intel_secure_reg_update(x1, (uint32_t)x2, 507 (uint32_t)x3, &retval); 508 SMC_RET3(handle, status, retval, x1); 509 510 case INTEL_SIP_SMC_RSU_STATUS: 511 status = intel_rsu_status(rsu_respbuf, 512 ARRAY_SIZE(rsu_respbuf)); 513 if (status) { 514 SMC_RET1(handle, status); 515 } else { 516 SMC_RET4(handle, rsu_respbuf[0], rsu_respbuf[1], 517 rsu_respbuf[2], rsu_respbuf[3]); 518 } 519 520 case INTEL_SIP_SMC_RSU_UPDATE: 521 status = intel_rsu_update(x1); 522 SMC_RET1(handle, status); 523 524 case INTEL_SIP_SMC_RSU_NOTIFY: 525 status = intel_rsu_notify(x1); 526 SMC_RET1(handle, status); 527 528 case INTEL_SIP_SMC_RSU_RETRY_COUNTER: 529 status = intel_rsu_retry_counter((uint32_t *)rsu_respbuf, 530 ARRAY_SIZE(rsu_respbuf), &retval); 531 if (status) { 532 SMC_RET1(handle, status); 533 } else { 534 SMC_RET2(handle, status, retval); 535 } 536 537 case INTEL_SIP_SMC_ECC_DBE: 538 status = intel_ecc_dbe_notification(x1); 539 SMC_RET1(handle, status); 540 541 case INTEL_SIP_SMC_MBOX_SEND_CMD: 542 x5 = SMC_GET_GP(handle, CTX_GPREG_X5); 543 x6 = SMC_GET_GP(handle, CTX_GPREG_X6); 544 status = intel_mbox_send_cmd(x1, (uint32_t *)x2, x3, x4, 545 (uint32_t *)x5, x6, &mbox_status, 546 &len_in_resp); 547 SMC_RET3(handle, status, mbox_status, len_in_resp); 548 549 case INTEL_SIP_SMC_GET_ROM_PATCH_SHA384: 550 status = intel_fcs_get_rom_patch_sha384(x1, &retval64, 551 &mbox_error); 552 SMC_RET4(handle, status, mbox_error, x1, retval64); 553 554 case INTEL_SIP_SMC_SVC_VERSION: 555 SMC_RET3(handle, INTEL_SIP_SMC_STATUS_OK, 556 SIP_SVC_VERSION_MAJOR, 557 SIP_SVC_VERSION_MINOR); 558 559 default: 560 return socfpga_sip_handler(smc_fid, x1, x2, x3, x4, 561 cookie, handle, flags); 562 } 563 } 564 565 DECLARE_RT_SVC( 566 socfpga_sip_svc, 567 OEN_SIP_START, 568 OEN_SIP_END, 569 SMC_TYPE_FAST, 570 NULL, 571 sip_smc_handler 572 ); 573 574 DECLARE_RT_SVC( 575 socfpga_sip_svc_std, 576 OEN_SIP_START, 577 OEN_SIP_END, 578 SMC_TYPE_YIELD, 579 NULL, 580 sip_smc_handler 581 ); 582