mc_trustzone.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (C) 2018 Marvell International Ltd.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. * https://spdx.org/licenses
  6. */
  7. #include <inttypes.h>
  8. #include <stdint.h>
  9. #include <common/debug.h>
  10. #include <drivers/marvell/addr_map.h>
  11. #include <lib/mmio.h>
  12. #include <mvebu_def.h>
  13. #include "mc_trustzone.h"
  14. #define TZ_SIZE(x) ((x) >> 13)
  15. static int fls(int x)
  16. {
  17. if (!x)
  18. return 0;
  19. return 32 - __builtin_clz(x);
  20. }
  21. /* To not duplicate types, the addr_map_win is used, but the "target"
  22. * filed is referring to attributes instead of "target".
  23. */
  24. void tz_enable_win(int ap_index, const struct addr_map_win *win, int win_id)
  25. {
  26. int tz_size;
  27. uint32_t val, base = win->base_addr;
  28. if ((win_id < 0) || (win_id > MVEBU_TZ_MAX_WINS)) {
  29. ERROR("Enabling wrong MC TrustZone window %d!\n", win_id);
  30. return;
  31. }
  32. /* map the window size to trustzone register convention */
  33. tz_size = fls(TZ_SIZE(win->win_size));
  34. VERBOSE("%s: window size = 0x%" PRIx64 " maps to tz_size %d\n",
  35. __func__, win->win_size, tz_size);
  36. if (tz_size < 0 || tz_size > 31) {
  37. ERROR("Using not allowed size for MC TrustZone window %d!\n",
  38. win_id);
  39. return;
  40. }
  41. if (base & 0xfff) {
  42. base = base & ~0xfff;
  43. WARN("Attempt to open MC TZ win. at 0x%" PRIx64 ", truncate to 0x%x\n",
  44. win->base_addr, base);
  45. }
  46. val = base | (tz_size << 7) | win->target_id | TZ_VALID;
  47. VERBOSE("%s: base 0x%x, tz_size moved 0x%x, attr 0x%x, val 0x%x\n",
  48. __func__, base, (tz_size << 7), win->target_id, val);
  49. mmio_write_32(MVEBU_AP_MC_TRUSTZONE_REG_LOW(ap_index, win_id), val);
  50. VERBOSE("%s: Win%d[0x%x] configured to 0x%x\n", __func__, win_id,
  51. MVEBU_AP_MC_TRUSTZONE_REG_LOW(ap_index, win_id),
  52. mmio_read_32(MVEBU_AP_MC_TRUSTZONE_REG_LOW(ap_index, win_id)));
  53. mmio_write_32(MVEBU_AP_MC_TRUSTZONE_REG_HIGH(ap_index, win_id),
  54. (win->base_addr >> 32));
  55. VERBOSE("%s: Win%d[0x%x] configured to 0x%x\n", __func__, win_id,
  56. MVEBU_AP_MC_TRUSTZONE_REG_HIGH(ap_index, win_id),
  57. mmio_read_32(MVEBU_AP_MC_TRUSTZONE_REG_HIGH(ap_index, win_id)));
  58. }