Marcin Juszkiewicz | b6839fb | 2023-05-10 10:03:01 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2023, Linaro Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
| 8 | |
Marcin Juszkiewicz | 79ee1c4 | 2023-05-15 11:07:54 +0200 | [diff] [blame] | 9 | #include <common/fdt_wrappers.h> |
Marcin Juszkiewicz | b6839fb | 2023-05-10 10:03:01 +0200 | [diff] [blame] | 10 | #include <common/runtime_svc.h> |
| 11 | #include <libfdt.h> |
| 12 | #include <smccc_helpers.h> |
| 13 | |
| 14 | /* default platform version is 0.0 */ |
| 15 | static int platform_version_major; |
| 16 | static int platform_version_minor; |
| 17 | |
| 18 | #define SMC_FASTCALL 0x80000000 |
| 19 | #define SMC64_FUNCTION (SMC_FASTCALL | 0x40000000) |
| 20 | #define SIP_FUNCTION (SMC64_FUNCTION | 0x02000000) |
| 21 | #define SIP_FUNCTION_ID(n) (SIP_FUNCTION | (n)) |
| 22 | |
| 23 | /* |
| 24 | * We do not use SMCCC_ARCH_SOC_ID here because qemu_sbsa is virtual platform |
| 25 | * which uses SoC present in QEMU. And they can change on their own while we |
| 26 | * need version of whole 'virtual hardware platform'. |
| 27 | */ |
| 28 | #define SIP_SVC_VERSION SIP_FUNCTION_ID(1) |
| 29 | |
Marcin Juszkiewicz | 79ee1c4 | 2023-05-15 11:07:54 +0200 | [diff] [blame] | 30 | #define SIP_SVC_GET_GIC SIP_FUNCTION_ID(100) |
| 31 | |
| 32 | void sbsa_set_gic_bases(const uintptr_t gicd_base, const uintptr_t gicr_base); |
| 33 | uintptr_t sbsa_get_gicd(void); |
| 34 | uintptr_t sbsa_get_gicr(void); |
| 35 | |
| 36 | void read_platform_config_from_dt(void *dtb) |
| 37 | { |
| 38 | int node; |
| 39 | const fdt64_t *data; |
| 40 | int err; |
| 41 | uintptr_t gicd_base; |
| 42 | uintptr_t gicr_base; |
| 43 | |
| 44 | /* |
| 45 | * QEMU gives us this DeviceTree node: |
| 46 | * |
| 47 | * intc { |
| 48 | reg = < 0x00 0x40060000 0x00 0x10000 |
| 49 | 0x00 0x40080000 0x00 0x4000000>; |
| 50 | }; |
| 51 | */ |
| 52 | node = fdt_path_offset(dtb, "/intc"); |
| 53 | if (node < 0) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | data = fdt_getprop(dtb, node, "reg", NULL); |
| 58 | if (data == NULL) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | err = fdt_get_reg_props_by_index(dtb, node, 0, &gicd_base, NULL); |
| 63 | if (err < 0) { |
| 64 | ERROR("Failed to read GICD reg property of GIC node\n"); |
| 65 | return; |
| 66 | } |
| 67 | INFO("GICD base = 0x%lx\n", gicd_base); |
| 68 | |
| 69 | err = fdt_get_reg_props_by_index(dtb, node, 1, &gicr_base, NULL); |
| 70 | if (err < 0) { |
| 71 | ERROR("Failed to read GICR reg property of GIC node\n"); |
| 72 | return; |
| 73 | } |
| 74 | INFO("GICR base = 0x%lx\n", gicr_base); |
| 75 | |
| 76 | sbsa_set_gic_bases(gicd_base, gicr_base); |
| 77 | } |
| 78 | |
Marcin Juszkiewicz | b6839fb | 2023-05-10 10:03:01 +0200 | [diff] [blame] | 79 | void read_platform_version(void *dtb) |
| 80 | { |
| 81 | int node; |
| 82 | |
| 83 | node = fdt_path_offset(dtb, "/"); |
| 84 | if (node >= 0) { |
| 85 | platform_version_major = fdt32_ld(fdt_getprop(dtb, node, |
| 86 | "machine-version-major", NULL)); |
| 87 | platform_version_minor = fdt32_ld(fdt_getprop(dtb, node, |
| 88 | "machine-version-minor", NULL)); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void sip_svc_init(void) |
| 93 | { |
| 94 | /* Read DeviceTree data before MMU is enabled */ |
| 95 | |
| 96 | void *dtb = (void *)(uintptr_t)ARM_PRELOADED_DTB_BASE; |
| 97 | int err; |
| 98 | |
| 99 | err = fdt_open_into(dtb, dtb, PLAT_QEMU_DT_MAX_SIZE); |
| 100 | if (err < 0) { |
| 101 | ERROR("Invalid Device Tree at %p: error %d\n", dtb, err); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | err = fdt_check_header(dtb); |
| 106 | if (err < 0) { |
| 107 | ERROR("Invalid DTB file passed\n"); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | read_platform_version(dtb); |
| 112 | INFO("Platform version: %d.%d\n", platform_version_major, platform_version_minor); |
Marcin Juszkiewicz | 79ee1c4 | 2023-05-15 11:07:54 +0200 | [diff] [blame] | 113 | |
| 114 | read_platform_config_from_dt(dtb); |
Marcin Juszkiewicz | b6839fb | 2023-05-10 10:03:01 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /* |
| 118 | * This function is responsible for handling all SiP calls from the NS world |
| 119 | */ |
| 120 | uintptr_t sbsa_sip_smc_handler(uint32_t smc_fid, |
| 121 | u_register_t x1, |
| 122 | u_register_t x2, |
| 123 | u_register_t x3, |
| 124 | u_register_t x4, |
| 125 | void *cookie, |
| 126 | void *handle, |
| 127 | u_register_t flags) |
| 128 | { |
| 129 | uint32_t ns; |
| 130 | |
| 131 | /* Determine which security state this SMC originated from */ |
| 132 | ns = is_caller_non_secure(flags); |
| 133 | if (!ns) { |
| 134 | ERROR("%s: wrong world SMC (0x%x)\n", __func__, smc_fid); |
| 135 | SMC_RET1(handle, SMC_UNK); |
| 136 | } |
| 137 | |
| 138 | switch (smc_fid) { |
| 139 | case SIP_SVC_VERSION: |
| 140 | INFO("Platform version requested\n"); |
| 141 | SMC_RET3(handle, NULL, platform_version_major, platform_version_minor); |
| 142 | |
Marcin Juszkiewicz | 79ee1c4 | 2023-05-15 11:07:54 +0200 | [diff] [blame] | 143 | case SIP_SVC_GET_GIC: |
| 144 | SMC_RET3(handle, NULL, sbsa_get_gicd(), sbsa_get_gicr()); |
| 145 | |
Marcin Juszkiewicz | b6839fb | 2023-05-10 10:03:01 +0200 | [diff] [blame] | 146 | default: |
| 147 | ERROR("%s: unhandled SMC (0x%x) (function id: %d)\n", __func__, smc_fid, |
| 148 | smc_fid - SIP_FUNCTION); |
| 149 | SMC_RET1(handle, SMC_UNK); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | int sbsa_sip_smc_setup(void) |
| 154 | { |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | /* Define a runtime service descriptor for fast SMC calls */ |
| 159 | DECLARE_RT_SVC( |
| 160 | sbsa_sip_svc, |
| 161 | OEN_SIP_START, |
| 162 | OEN_SIP_END, |
| 163 | SMC_TYPE_FAST, |
| 164 | sbsa_sip_smc_setup, |
| 165 | sbsa_sip_smc_handler |
| 166 | ); |