1 // SPDX-License-Identifier: BSD-3-Clause 2 /* 3 * Copyright (c) 2017-2021, STMicroelectronics 4 */ 5 6 #include <assert.h> 7 #include <config.h> 8 #include <drivers/stm32_bsec.h> 9 #include <io.h> 10 #include <kernel/delay.h> 11 #include <kernel/dt.h> 12 #include <kernel/boot.h> 13 #include <kernel/pm.h> 14 #include <kernel/spinlock.h> 15 #include <libfdt.h> 16 #include <limits.h> 17 #include <mm/core_memprot.h> 18 #include <platform_config.h> 19 #include <stm32_util.h> 20 #include <string.h> 21 #include <tee_api_defines.h> 22 #include <types_ext.h> 23 #include <util.h> 24 25 #ifdef CFG_STM32MP13 26 #define DT_BSEC_COMPAT "st,stm32mp13-bsec" 27 #endif 28 #ifdef CFG_STM32MP15 29 #define DT_BSEC_COMPAT "st,stm32mp15-bsec" 30 #endif 31 32 #define BSEC_OTP_MASK GENMASK_32(4, 0) 33 #define BSEC_OTP_BANK_SHIFT U(5) 34 35 /* Permanent lock bitmasks */ 36 #define DATA_LOWER_OTP_PERLOCK_BIT U(3) 37 #define DATA_UPPER_OTP_PERLOCK_BIT U(1) 38 39 /* BSEC register offset */ 40 #define BSEC_OTP_CONF_OFF U(0x000) 41 #define BSEC_OTP_CTRL_OFF U(0x004) 42 #define BSEC_OTP_WRDATA_OFF U(0x008) 43 #define BSEC_OTP_STATUS_OFF U(0x00C) 44 #define BSEC_OTP_LOCK_OFF U(0x010) 45 #define BSEC_DEN_OFF U(0x014) 46 #define BSEC_FEN_OFF U(0x018) 47 #define BSEC_DISTURBED_OFF U(0x01C) 48 #define BSEC_DISTURBED1_OFF U(0x020) 49 #define BSEC_DISTURBED2_OFF U(0x024) 50 #define BSEC_ERROR_OFF U(0x034) 51 #define BSEC_ERROR1_OFF U(0x038) 52 #define BSEC_ERROR2_OFF U(0x03C) 53 #define BSEC_WRLOCK_OFF U(0x04C) 54 #define BSEC_WRLOCK1_OFF U(0x050) 55 #define BSEC_WRLOCK2_OFF U(0x054) 56 #define BSEC_SPLOCK_OFF U(0x064) 57 #define BSEC_SPLOCK1_OFF U(0x068) 58 #define BSEC_SPLOCK2_OFF U(0x06C) 59 #define BSEC_SWLOCK_OFF U(0x07C) 60 #define BSEC_SWLOCK1_OFF U(0x080) 61 #define BSEC_SWLOCK2_OFF U(0x084) 62 #define BSEC_SRLOCK_OFF U(0x094) 63 #define BSEC_SRLOCK1_OFF U(0x098) 64 #define BSEC_SRLOCK2_OFF U(0x09C) 65 #define BSEC_JTAG_IN_OFF U(0x0AC) 66 #define BSEC_JTAG_OUT_OFF U(0x0B0) 67 #define BSEC_SCRATCH_OFF U(0x0B4) 68 #define BSEC_OTP_DATA_OFF U(0x200) 69 #define BSEC_IPHW_CFG_OFF U(0xFF0) 70 #define BSEC_IPVR_OFF U(0xFF4) 71 #define BSEC_IP_ID_OFF U(0xFF8) 72 #define BSEC_IP_MAGIC_ID_OFF U(0xFFC) 73 74 /* BSEC_CONFIGURATION Register */ 75 #define BSEC_CONF_POWER_UP_MASK BIT(0) 76 #define BSEC_CONF_POWER_UP_SHIFT U(0) 77 #define BSEC_CONF_FRQ_MASK GENMASK_32(2, 1) 78 #define BSEC_CONF_FRQ_SHIFT U(1) 79 #define BSEC_CONF_PRG_WIDTH_MASK GENMASK_32(6, 3) 80 #define BSEC_CONF_PRG_WIDTH_SHIFT U(3) 81 #define BSEC_CONF_TREAD_MASK GENMASK_32(8, 7) 82 #define BSEC_CONF_TREAD_SHIFT U(7) 83 84 /* BSEC_CONTROL Register */ 85 #define BSEC_READ U(0x000) 86 #define BSEC_WRITE U(0x100) 87 #define BSEC_LOCK U(0x200) 88 89 /* BSEC_STATUS Register */ 90 #define BSEC_MODE_SECURED BIT(0) 91 #define BSEC_MODE_INVALID BIT(2) 92 #define BSEC_MODE_BUSY BIT(3) 93 #define BSEC_MODE_PROGFAIL BIT(4) 94 #define BSEC_MODE_PWR BIT(5) 95 #define BSEC_MODE_CLOSED BIT(8) 96 97 /* 98 * OTP Lock services definition 99 * Value must corresponding to the bit position in the register 100 */ 101 #define BSEC_LOCK_UPPER_OTP U(0x00) 102 #define BSEC_LOCK_DEBUG U(0x02) 103 #define BSEC_LOCK_PROGRAM U(0x04) 104 105 /* Timeout when polling on status */ 106 #define BSEC_TIMEOUT_US U(10000) 107 108 struct bsec_dev { 109 struct io_pa_va base; 110 unsigned int upper_base; 111 unsigned int max_id; 112 uint32_t *nsec_access; 113 }; 114 115 /* Only 1 instance of BSEC is expected per platform */ 116 static struct bsec_dev bsec_dev; 117 118 /* BSEC access protection */ 119 static unsigned int lock = SPINLOCK_UNLOCK; 120 121 static uint32_t bsec_lock(void) 122 { 123 return may_spin_lock(&lock); 124 } 125 126 static void bsec_unlock(uint32_t exceptions) 127 { 128 may_spin_unlock(&lock, exceptions); 129 } 130 131 static uint32_t otp_max_id(void) 132 { 133 return bsec_dev.max_id; 134 } 135 136 static uint32_t otp_upper_base(void) 137 { 138 return bsec_dev.upper_base; 139 } 140 141 static uint32_t otp_bank_offset(uint32_t otp_id) 142 { 143 assert(otp_id <= otp_max_id()); 144 145 return ((otp_id & ~BSEC_OTP_MASK) >> BSEC_OTP_BANK_SHIFT) * 146 sizeof(uint32_t); 147 } 148 149 static vaddr_t bsec_base(void) 150 { 151 return io_pa_or_va_secure(&bsec_dev.base, BSEC_IP_MAGIC_ID_OFF + 1); 152 } 153 154 static uint32_t bsec_status(void) 155 { 156 return io_read32(bsec_base() + BSEC_OTP_STATUS_OFF); 157 } 158 159 static bool state_is_invalid_mode(void) 160 { 161 return bsec_status() & BSEC_MODE_INVALID; 162 } 163 164 static bool state_is_secured_mode(void) 165 { 166 return bsec_status() & BSEC_MODE_SECURED; 167 } 168 169 static bool state_is_closed_mode(void) 170 { 171 uint32_t close_mode = 0; 172 173 if (IS_ENABLED(CFG_STM32MP13)) 174 return bsec_status() & BSEC_MODE_CLOSED; 175 176 if (stm32_bsec_read_otp(&close_mode, CFG0_OTP)) 177 panic("Unable to read OTP"); 178 179 return close_mode & CFG0_OTP_CLOSED_DEVICE; 180 } 181 182 /* 183 * Check that BSEC interface does not report an error 184 * @otp_id : OTP number 185 * @check_disturbed: check only error (false) or all sources (true) 186 * Return a TEE_Result compliant value 187 */ 188 static TEE_Result check_no_error(uint32_t otp_id, bool check_disturbed) 189 { 190 uint32_t bit = BIT(otp_id & BSEC_OTP_MASK); 191 uint32_t bank = otp_bank_offset(otp_id); 192 193 if (io_read32(bsec_base() + BSEC_ERROR_OFF + bank) & bit) 194 return TEE_ERROR_GENERIC; 195 196 if (check_disturbed && 197 io_read32(bsec_base() + BSEC_DISTURBED_OFF + bank) & bit) 198 return TEE_ERROR_GENERIC; 199 200 return TEE_SUCCESS; 201 } 202 203 static TEE_Result power_up_safmem(void) 204 { 205 uint64_t timeout_ref = timeout_init_us(BSEC_TIMEOUT_US); 206 207 io_mask32(bsec_base() + BSEC_OTP_CONF_OFF, BSEC_CONF_POWER_UP_MASK, 208 BSEC_CONF_POWER_UP_MASK); 209 210 /* 211 * If a timeout is detected, test the condition again to consider 212 * cases where timeout is due to the executing TEE thread rescheduling. 213 */ 214 while (!timeout_elapsed(timeout_ref)) 215 if (bsec_status() & BSEC_MODE_PWR) 216 break; 217 218 if (bsec_status() & BSEC_MODE_PWR) 219 return TEE_SUCCESS; 220 221 return TEE_ERROR_GENERIC; 222 } 223 224 static TEE_Result power_down_safmem(void) 225 { 226 uint64_t timeout_ref = timeout_init_us(BSEC_TIMEOUT_US); 227 228 io_mask32(bsec_base() + BSEC_OTP_CONF_OFF, 0, BSEC_CONF_POWER_UP_MASK); 229 230 /* 231 * If a timeout is detected, test the condition again to consider 232 * cases where timeout is due to the executing TEE thread rescheduling. 233 */ 234 while (!timeout_elapsed(timeout_ref)) 235 if (!(bsec_status() & BSEC_MODE_PWR)) 236 break; 237 238 if (!(bsec_status() & BSEC_MODE_PWR)) 239 return TEE_SUCCESS; 240 241 return TEE_ERROR_GENERIC; 242 } 243 244 static TEE_Result stm32_bsec_shadow_register(uint32_t otp_id) 245 { 246 TEE_Result result = 0; 247 uint32_t exceptions = 0; 248 uint64_t timeout_ref = 0; 249 bool locked = false; 250 251 /* Check if shadowing of OTP is locked, informative only */ 252 result = stm32_bsec_read_sr_lock(otp_id, &locked); 253 if (result) 254 return result; 255 256 if (locked) 257 DMSG("BSEC shadow warning: OTP locked"); 258 259 if (state_is_invalid_mode()) 260 return TEE_ERROR_SECURITY; 261 262 exceptions = bsec_lock(); 263 264 result = power_up_safmem(); 265 if (result) 266 goto out; 267 268 io_write32(bsec_base() + BSEC_OTP_CTRL_OFF, otp_id | BSEC_READ); 269 270 timeout_ref = timeout_init_us(BSEC_TIMEOUT_US); 271 while (!timeout_elapsed(timeout_ref)) 272 if (!(bsec_status() & BSEC_MODE_BUSY)) 273 break; 274 275 if (bsec_status() & BSEC_MODE_BUSY) 276 result = TEE_ERROR_BUSY; 277 else 278 result = check_no_error(otp_id, true /* check-disturbed */); 279 280 power_down_safmem(); 281 282 out: 283 bsec_unlock(exceptions); 284 285 return result; 286 } 287 288 TEE_Result stm32_bsec_read_otp(uint32_t *value, uint32_t otp_id) 289 { 290 if (otp_id > otp_max_id()) 291 return TEE_ERROR_BAD_PARAMETERS; 292 293 if (state_is_invalid_mode()) 294 return TEE_ERROR_SECURITY; 295 296 *value = io_read32(bsec_base() + BSEC_OTP_DATA_OFF + 297 (otp_id * sizeof(uint32_t))); 298 299 return TEE_SUCCESS; 300 } 301 302 TEE_Result stm32_bsec_shadow_read_otp(uint32_t *otp_value, uint32_t otp_id) 303 { 304 TEE_Result result = 0; 305 306 result = stm32_bsec_shadow_register(otp_id); 307 if (result) { 308 EMSG("BSEC %"PRIu32" Shadowing Error %#"PRIx32, otp_id, result); 309 return result; 310 } 311 312 result = stm32_bsec_read_otp(otp_value, otp_id); 313 if (result) 314 EMSG("BSEC %"PRIu32" Read Error %#"PRIx32, otp_id, result); 315 316 return result; 317 } 318 319 TEE_Result stm32_bsec_write_otp(uint32_t value, uint32_t otp_id) 320 { 321 TEE_Result result = 0; 322 uint32_t exceptions = 0; 323 vaddr_t otp_data_base = bsec_base() + BSEC_OTP_DATA_OFF; 324 bool locked = false; 325 326 /* Check if write of OTP is locked, informative only */ 327 result = stm32_bsec_read_sw_lock(otp_id, &locked); 328 if (result) 329 return result; 330 331 if (locked) 332 DMSG("BSEC write warning: OTP locked"); 333 334 if (state_is_invalid_mode()) 335 return TEE_ERROR_SECURITY; 336 337 exceptions = bsec_lock(); 338 339 io_write32(otp_data_base + (otp_id * sizeof(uint32_t)), value); 340 341 bsec_unlock(exceptions); 342 343 return TEE_SUCCESS; 344 } 345 346 #ifdef CFG_STM32_BSEC_WRITE 347 TEE_Result stm32_bsec_program_otp(uint32_t value, uint32_t otp_id) 348 { 349 TEE_Result result = 0; 350 uint32_t exceptions = 0; 351 uint64_t timeout_ref = 0; 352 bool locked = false; 353 354 /* Check if shadowing of OTP is locked, informative only */ 355 result = stm32_bsec_read_sp_lock(otp_id, &locked); 356 if (result) 357 return result; 358 359 if (locked) 360 DMSG("BSEC program warning: OTP locked"); 361 362 if (io_read32(bsec_base() + BSEC_OTP_LOCK_OFF) & BIT(BSEC_LOCK_PROGRAM)) 363 DMSG("BSEC program warning: GPLOCK activated"); 364 365 if (state_is_invalid_mode()) 366 return TEE_ERROR_SECURITY; 367 368 exceptions = bsec_lock(); 369 370 result = power_up_safmem(); 371 if (result) 372 goto out; 373 374 io_write32(bsec_base() + BSEC_OTP_WRDATA_OFF, value); 375 io_write32(bsec_base() + BSEC_OTP_CTRL_OFF, otp_id | BSEC_WRITE); 376 377 timeout_ref = timeout_init_us(BSEC_TIMEOUT_US); 378 while (!timeout_elapsed(timeout_ref)) 379 if (!(bsec_status() & BSEC_MODE_BUSY)) 380 break; 381 382 if (bsec_status() & BSEC_MODE_BUSY) 383 result = TEE_ERROR_BUSY; 384 else if (bsec_status() & BSEC_MODE_PROGFAIL) 385 result = TEE_ERROR_BAD_PARAMETERS; 386 else 387 result = check_no_error(otp_id, true /* check-disturbed */); 388 389 power_down_safmem(); 390 391 out: 392 bsec_unlock(exceptions); 393 394 return result; 395 } 396 #endif /*CFG_STM32_BSEC_WRITE*/ 397 398 TEE_Result stm32_bsec_permanent_lock_otp(uint32_t otp_id) 399 { 400 TEE_Result result = 0; 401 uint32_t data = 0; 402 uint32_t addr = 0; 403 uint32_t exceptions = 0; 404 vaddr_t base = bsec_base(); 405 uint64_t timeout_ref = 0; 406 uint32_t upper_base = otp_upper_base(); 407 408 if (otp_id > otp_max_id()) 409 return TEE_ERROR_BAD_PARAMETERS; 410 411 /* 412 * 2 bits per words for lower OTPs: 2:1 Redundancy 413 * 1 bit per word for upper OTPs : ECC support 414 * e.g with 32 lower and 64 upper OTPs: 415 * OTP word to be ADDR[6:0] WRDATA[31:0] 416 * locked 417 * 0 0x00 0x0000 0003 418 * 1 0x00 0x0000 000C 419 * ... ... ... 420 * 7 0x00 0x0000 C000 421 * 8 0x01 0x0000 0003 422 * ... ... ... 423 * 31 0x03 0x0000 C000 424 * 32 0x04 0x0000 0001 425 * 33 0x04 0x0000 0002 426 * 95 0x07 0x0000 8000 427 */ 428 if (otp_id < upper_base) { 429 addr = otp_id / 8U; 430 data = DATA_LOWER_OTP_PERLOCK_BIT << ((otp_id * 2U) & 0xF); 431 } else { 432 addr = upper_base / 8U + (otp_id - upper_base) / 16U; 433 data = DATA_UPPER_OTP_PERLOCK_BIT << (otp_id & 0xF); 434 } 435 436 if (state_is_invalid_mode()) 437 return TEE_ERROR_SECURITY; 438 439 exceptions = bsec_lock(); 440 441 result = power_up_safmem(); 442 if (result) 443 goto out; 444 445 io_write32(base + BSEC_OTP_WRDATA_OFF, data); 446 io_write32(base + BSEC_OTP_CTRL_OFF, addr | BSEC_WRITE | BSEC_LOCK); 447 448 timeout_ref = timeout_init_us(BSEC_TIMEOUT_US); 449 while (!timeout_elapsed(timeout_ref)) 450 if (!(bsec_status() & BSEC_MODE_BUSY)) 451 break; 452 453 if (bsec_status() & BSEC_MODE_BUSY) 454 result = TEE_ERROR_BUSY; 455 else if (bsec_status() & BSEC_MODE_PROGFAIL) 456 result = TEE_ERROR_BAD_PARAMETERS; 457 else 458 result = check_no_error(otp_id, false /* not-disturbed */); 459 460 #ifdef CFG_STM32MP13 461 io_write32(base + BSEC_OTP_CTRL_OFF, addr | BSEC_READ | BSEC_LOCK); 462 #endif 463 464 power_down_safmem(); 465 466 out: 467 bsec_unlock(exceptions); 468 469 return result; 470 } 471 472 #ifdef CFG_STM32_BSEC_WRITE 473 TEE_Result stm32_bsec_write_debug_conf(uint32_t value) 474 { 475 TEE_Result result = TEE_ERROR_GENERIC; 476 uint32_t exceptions = 0; 477 478 if (state_is_invalid_mode()) 479 return TEE_ERROR_SECURITY; 480 481 exceptions = bsec_lock(); 482 483 io_write32(bsec_base() + BSEC_DEN_OFF, value); 484 485 if ((io_read32(bsec_base() + BSEC_DEN_OFF) ^ value) == 0U) 486 result = TEE_SUCCESS; 487 488 bsec_unlock(exceptions); 489 490 return result; 491 } 492 #endif /*CFG_STM32_BSEC_WRITE*/ 493 494 uint32_t stm32_bsec_read_debug_conf(void) 495 { 496 return io_read32(bsec_base() + BSEC_DEN_OFF); 497 } 498 499 static TEE_Result set_bsec_lock(uint32_t otp_id, size_t lock_offset) 500 { 501 uint32_t bank = otp_bank_offset(otp_id); 502 uint32_t otp_mask = BIT(otp_id & BSEC_OTP_MASK); 503 vaddr_t lock_addr = bsec_base() + bank + lock_offset; 504 uint32_t exceptions = 0; 505 506 if (otp_id > STM32MP1_OTP_MAX_ID) 507 return TEE_ERROR_BAD_PARAMETERS; 508 509 if (state_is_invalid_mode()) 510 return TEE_ERROR_SECURITY; 511 512 exceptions = bsec_lock(); 513 514 io_write32(lock_addr, otp_mask); 515 516 bsec_unlock(exceptions); 517 518 return TEE_SUCCESS; 519 } 520 521 TEE_Result stm32_bsec_set_sr_lock(uint32_t otp_id) 522 { 523 return set_bsec_lock(otp_id, BSEC_SRLOCK_OFF); 524 } 525 526 TEE_Result stm32_bsec_set_sw_lock(uint32_t otp_id) 527 { 528 return set_bsec_lock(otp_id, BSEC_SWLOCK_OFF); 529 } 530 531 TEE_Result stm32_bsec_set_sp_lock(uint32_t otp_id) 532 { 533 return set_bsec_lock(otp_id, BSEC_SPLOCK_OFF); 534 } 535 536 static TEE_Result read_bsec_lock(uint32_t otp_id, bool *locked, 537 size_t lock_offset) 538 { 539 uint32_t bank = otp_bank_offset(otp_id); 540 uint32_t otp_mask = BIT(otp_id & BSEC_OTP_MASK); 541 vaddr_t lock_addr = bsec_base() + bank + lock_offset; 542 543 if (otp_id > STM32MP1_OTP_MAX_ID) 544 return TEE_ERROR_BAD_PARAMETERS; 545 546 if (state_is_invalid_mode()) 547 return TEE_ERROR_SECURITY; 548 549 *locked = (io_read32(lock_addr) & otp_mask) != 0; 550 551 return TEE_SUCCESS; 552 } 553 554 TEE_Result stm32_bsec_read_sr_lock(uint32_t otp_id, bool *locked) 555 { 556 return read_bsec_lock(otp_id, locked, BSEC_SRLOCK_OFF); 557 } 558 559 TEE_Result stm32_bsec_read_sw_lock(uint32_t otp_id, bool *locked) 560 { 561 return read_bsec_lock(otp_id, locked, BSEC_SWLOCK_OFF); 562 } 563 564 TEE_Result stm32_bsec_read_sp_lock(uint32_t otp_id, bool *locked) 565 { 566 return read_bsec_lock(otp_id, locked, BSEC_SPLOCK_OFF); 567 } 568 569 TEE_Result stm32_bsec_read_permanent_lock(uint32_t otp_id, bool *locked) 570 { 571 return read_bsec_lock(otp_id, locked, BSEC_WRLOCK_OFF); 572 } 573 574 static size_t nsec_access_array_size(void) 575 { 576 size_t upper_count = otp_max_id() - otp_upper_base() + 1; 577 578 return ROUNDUP_DIV(upper_count, BSEC_BITS_PER_WORD); 579 } 580 581 static bool nsec_access_granted(unsigned int index) 582 { 583 uint32_t *array = bsec_dev.nsec_access; 584 585 return array && 586 (index / BSEC_BITS_PER_WORD) < nsec_access_array_size() && 587 array[index / BSEC_BITS_PER_WORD] & 588 BIT(index % BSEC_BITS_PER_WORD); 589 } 590 591 bool stm32_bsec_can_access_otp(uint32_t otp_id) 592 { 593 return (otp_id <= otp_max_id()) && !state_is_invalid_mode(); 594 } 595 596 bool stm32_bsec_nsec_can_access_otp(uint32_t otp_id) 597 { 598 return otp_id < otp_upper_base() || 599 nsec_access_granted(otp_id - otp_upper_base()); 600 } 601 602 struct nvmem_layout { 603 char *name; 604 uint32_t otp_id; 605 size_t bit_len; 606 }; 607 608 static struct nvmem_layout *nvmem_layout; 609 static size_t nvmem_layout_count; 610 611 TEE_Result stm32_bsec_find_otp_in_nvmem_layout(const char *name, 612 uint32_t *otp_id, 613 size_t *otp_bit_len) 614 { 615 size_t i = 0; 616 617 if (!name) 618 return TEE_ERROR_BAD_PARAMETERS; 619 620 for (i = 0; i < nvmem_layout_count; i++) { 621 if (!nvmem_layout[i].name || strcmp(name, nvmem_layout[i].name)) 622 continue; 623 624 if (otp_id) 625 *otp_id = nvmem_layout[i].otp_id; 626 627 if (otp_bit_len) 628 *otp_bit_len = nvmem_layout[i].bit_len; 629 630 DMSG("nvmem %s = %zu: %"PRId32" %zu", name, i, 631 nvmem_layout[i].otp_id, nvmem_layout[i].bit_len); 632 633 return TEE_SUCCESS; 634 } 635 636 DMSG("nvmem %s failed", name); 637 638 return TEE_ERROR_ITEM_NOT_FOUND; 639 }; 640 641 TEE_Result stm32_bsec_get_state(uint32_t *state) 642 { 643 if (!state) 644 return TEE_ERROR_BAD_PARAMETERS; 645 646 if (state_is_invalid_mode() || !state_is_secured_mode()) { 647 *state = BSEC_STATE_INVALID; 648 } else { 649 if (state_is_closed_mode()) 650 *state = BSEC_STATE_SEC_CLOSED; 651 else 652 *state = BSEC_STATE_SEC_OPEN; 653 } 654 655 return TEE_SUCCESS; 656 } 657 658 #ifdef CFG_EMBED_DTB 659 static void enable_nsec_access(unsigned int otp_id) 660 { 661 unsigned int idx = (otp_id - otp_upper_base()) / BSEC_BITS_PER_WORD; 662 663 if (otp_id < otp_upper_base()) 664 return; 665 666 if (otp_id > otp_max_id() || stm32_bsec_shadow_register(otp_id)) 667 panic(); 668 669 bsec_dev.nsec_access[idx] |= BIT(otp_id % BSEC_BITS_PER_WORD); 670 } 671 672 static void bsec_dt_otp_nsec_access(void *fdt, int bsec_node) 673 { 674 int bsec_subnode = 0; 675 676 bsec_dev.nsec_access = calloc(nsec_access_array_size(), 677 sizeof(*bsec_dev.nsec_access)); 678 if (!bsec_dev.nsec_access) 679 panic(); 680 681 fdt_for_each_subnode(bsec_subnode, fdt, bsec_node) { 682 unsigned int reg_offset = 0; 683 unsigned int reg_size = 0; 684 unsigned int otp_id = 0; 685 unsigned int i = 0; 686 size_t size = 0; 687 688 reg_offset = _fdt_reg_base_address(fdt, bsec_subnode); 689 reg_size = _fdt_reg_size(fdt, bsec_subnode); 690 691 assert(reg_offset != DT_INFO_INVALID_REG && 692 reg_size != DT_INFO_INVALID_REG_SIZE); 693 694 otp_id = reg_offset / sizeof(uint32_t); 695 696 if (otp_id < STM32MP1_UPPER_OTP_START) { 697 unsigned int otp_end = 698 ROUNDUP_DIV(reg_offset + reg_size, 699 sizeof(uint32_t)); 700 701 if (otp_end > STM32MP1_UPPER_OTP_START) { 702 /* 703 * OTP crosses Lower/Upper boundary, consider 704 * only the upper part. 705 */ 706 otp_id = STM32MP1_UPPER_OTP_START; 707 reg_size -= (STM32MP1_UPPER_OTP_START * 708 sizeof(uint32_t)) - reg_offset; 709 reg_offset = STM32MP1_UPPER_OTP_START * 710 sizeof(uint32_t); 711 712 DMSG("OTP crosses Lower/Upper boundary"); 713 } else { 714 continue; 715 } 716 } 717 718 if (!fdt_getprop(fdt, bsec_subnode, "st,non-secure-otp", NULL)) 719 continue; 720 721 if ((reg_offset % sizeof(uint32_t)) || 722 (reg_size % sizeof(uint32_t))) 723 panic("Unaligned non-secure OTP"); 724 725 size = reg_size / sizeof(uint32_t); 726 727 if (otp_id + size > OTP_MAX_SIZE) 728 panic("OTP range oversized"); 729 730 for (i = otp_id; i < otp_id + size; i++) 731 enable_nsec_access(i); 732 } 733 } 734 735 static void save_dt_nvmem_layout(void *fdt, int bsec_node) 736 { 737 int cell_max = 0; 738 int cell_cnt = 0; 739 int node = 0; 740 741 fdt_for_each_subnode(node, fdt, bsec_node) 742 cell_max++; 743 if (!cell_max) 744 return; 745 746 nvmem_layout = calloc(cell_max, sizeof(*nvmem_layout)); 747 if (!nvmem_layout) 748 panic(); 749 750 fdt_for_each_subnode(node, fdt, bsec_node) { 751 unsigned int reg_offset = 0; 752 unsigned int reg_length = 0; 753 const char *string = NULL; 754 const char *s = NULL; 755 int len = 0; 756 struct nvmem_layout *layout_cell = &nvmem_layout[cell_cnt]; 757 758 string = fdt_get_name(fdt, node, &len); 759 if (!string || !len) 760 continue; 761 762 reg_offset = _fdt_reg_base_address(fdt, node); 763 reg_length = _fdt_reg_size(fdt, node); 764 765 if (reg_offset == DT_INFO_INVALID_REG || 766 reg_length == DT_INFO_INVALID_REG_SIZE) { 767 DMSG("Malformed nvmem %s: ignored", string); 768 continue; 769 } 770 771 if (reg_offset % sizeof(uint32_t)) { 772 DMSG("Misaligned nvmem %s: ignored", string); 773 continue; 774 } 775 layout_cell->otp_id = reg_offset / sizeof(uint32_t); 776 layout_cell->bit_len = reg_length * CHAR_BIT; 777 778 s = strchr(string, '@'); 779 if (s) 780 len = s - string; 781 782 layout_cell->name = strndup(string, len); 783 if (!layout_cell->name) 784 panic(); 785 cell_cnt++; 786 DMSG("nvmem[%d] = %s %"PRId32" %zu", cell_cnt, 787 layout_cell->name, layout_cell->otp_id, 788 layout_cell->bit_len); 789 } 790 791 if (cell_cnt != cell_max) { 792 nvmem_layout = realloc(nvmem_layout, 793 cell_cnt * sizeof(*nvmem_layout)); 794 if (!nvmem_layout) 795 panic(); 796 } 797 798 nvmem_layout_count = cell_cnt; 799 } 800 801 static void initialize_bsec_from_dt(void) 802 { 803 void *fdt = NULL; 804 int node = 0; 805 struct dt_node_info bsec_info = { }; 806 807 fdt = get_embedded_dt(); 808 node = fdt_node_offset_by_compatible(fdt, 0, DT_BSEC_COMPAT); 809 if (node < 0) 810 panic(); 811 812 _fdt_fill_device_info(fdt, &bsec_info, node); 813 814 if (bsec_info.reg != bsec_dev.base.pa || 815 !(bsec_info.status & DT_STATUS_OK_SEC)) 816 panic(); 817 818 bsec_dt_otp_nsec_access(fdt, node); 819 820 save_dt_nvmem_layout(fdt, node); 821 } 822 #else 823 static void initialize_bsec_from_dt(void) 824 { 825 } 826 #endif /*CFG_EMBED_DTB*/ 827 828 static TEE_Result bsec_pm(enum pm_op op, uint32_t pm_hint __unused, 829 const struct pm_callback_handle *hdl __unused) 830 { 831 static uint32_t debug_conf; 832 833 assert(op == PM_OP_SUSPEND || op == PM_OP_RESUME); 834 835 if (op == PM_OP_SUSPEND) 836 debug_conf = stm32_bsec_read_debug_conf(); 837 else 838 stm32_bsec_write_debug_conf(debug_conf); 839 840 return TEE_SUCCESS; 841 } 842 DECLARE_KEEP_PAGER(bsec_pm); 843 844 static TEE_Result initialize_bsec(void) 845 { 846 struct stm32_bsec_static_cfg cfg = { }; 847 848 stm32mp_get_bsec_static_cfg(&cfg); 849 850 bsec_dev.base.pa = cfg.base; 851 bsec_dev.upper_base = cfg.upper_start; 852 bsec_dev.max_id = cfg.max_id; 853 854 if (state_is_invalid_mode()) 855 panic(); 856 857 if (IS_ENABLED(CFG_EMBED_DTB)) 858 initialize_bsec_from_dt(); 859 860 register_pm_core_service_cb(bsec_pm, NULL, "stm32_bsec"); 861 862 return TEE_SUCCESS; 863 } 864 865 early_init(initialize_bsec); 866