1085e80ecSAntonio Nino Diaz /* 2*e073e070SOlivier Deprez * Copyright (c) 2016-2020, ARM Limited and Contributors. All rights reserved. 3085e80ecSAntonio Nino Diaz * 4085e80ecSAntonio Nino Diaz * SPDX-License-Identifier: BSD-3-Clause 5085e80ecSAntonio Nino Diaz */ 6085e80ecSAntonio Nino Diaz 7c3cf06f1SAntonio Nino Diaz #ifndef SMCCC_H 8c3cf06f1SAntonio Nino Diaz #define SMCCC_H 9085e80ecSAntonio Nino Diaz 1009d40e0eSAntonio Nino Diaz #include <lib/utils_def.h> 11085e80ecSAntonio Nino Diaz 122f370465SAntonio Nino Diaz #define SMCCC_VERSION_MAJOR_SHIFT U(16) 132f370465SAntonio Nino Diaz #define SMCCC_VERSION_MAJOR_MASK U(0x7FFF) 142f370465SAntonio Nino Diaz #define SMCCC_VERSION_MINOR_SHIFT U(0) 152f370465SAntonio Nino Diaz #define SMCCC_VERSION_MINOR_MASK U(0xFFFF) 162f370465SAntonio Nino Diaz #define MAKE_SMCCC_VERSION(_major, _minor) \ 170c487ea4SAntonio Nino Diaz ((((uint32_t)(_major) & SMCCC_VERSION_MAJOR_MASK) << \ 180c487ea4SAntonio Nino Diaz SMCCC_VERSION_MAJOR_SHIFT) \ 190c487ea4SAntonio Nino Diaz | (((uint32_t)(_minor) & SMCCC_VERSION_MINOR_MASK) << \ 200c487ea4SAntonio Nino Diaz SMCCC_VERSION_MINOR_SHIFT)) 21085e80ecSAntonio Nino Diaz 220709055eSAntonio Nino Diaz #define SMCCC_MAJOR_VERSION U(1) 23e34cc0ceSMadhukar Pappireddy #define SMCCC_MINOR_VERSION U(2) 240709055eSAntonio Nino Diaz 250709055eSAntonio Nino Diaz /******************************************************************************* 260709055eSAntonio Nino Diaz * Bit definitions inside the function id as per the SMC calling convention 270709055eSAntonio Nino Diaz ******************************************************************************/ 280709055eSAntonio Nino Diaz #define FUNCID_TYPE_SHIFT U(31) 290709055eSAntonio Nino Diaz #define FUNCID_TYPE_MASK U(0x1) 300709055eSAntonio Nino Diaz #define FUNCID_TYPE_WIDTH U(1) 310709055eSAntonio Nino Diaz 320709055eSAntonio Nino Diaz #define FUNCID_CC_SHIFT U(30) 330709055eSAntonio Nino Diaz #define FUNCID_CC_MASK U(0x1) 340709055eSAntonio Nino Diaz #define FUNCID_CC_WIDTH U(1) 350709055eSAntonio Nino Diaz 360709055eSAntonio Nino Diaz #define FUNCID_OEN_SHIFT U(24) 370709055eSAntonio Nino Diaz #define FUNCID_OEN_MASK U(0x3f) 380709055eSAntonio Nino Diaz #define FUNCID_OEN_WIDTH U(6) 390709055eSAntonio Nino Diaz 400709055eSAntonio Nino Diaz #define FUNCID_NUM_SHIFT U(0) 410709055eSAntonio Nino Diaz #define FUNCID_NUM_MASK U(0xffff) 420709055eSAntonio Nino Diaz #define FUNCID_NUM_WIDTH U(16) 430709055eSAntonio Nino Diaz 44*e073e070SOlivier Deprez #define GET_SMC_NUM(id) (((id) >> FUNCID_NUM_SHIFT) & \ 45*e073e070SOlivier Deprez FUNCID_NUM_MASK) 460709055eSAntonio Nino Diaz #define GET_SMC_TYPE(id) (((id) >> FUNCID_TYPE_SHIFT) & \ 470709055eSAntonio Nino Diaz FUNCID_TYPE_MASK) 480709055eSAntonio Nino Diaz #define GET_SMC_CC(id) (((id) >> FUNCID_CC_SHIFT) & \ 490709055eSAntonio Nino Diaz FUNCID_CC_MASK) 500709055eSAntonio Nino Diaz #define GET_SMC_OEN(id) (((id) >> FUNCID_OEN_SHIFT) & \ 510709055eSAntonio Nino Diaz FUNCID_OEN_MASK) 520709055eSAntonio Nino Diaz 530709055eSAntonio Nino Diaz /******************************************************************************* 540709055eSAntonio Nino Diaz * Owning entity number definitions inside the function id as per the SMC 550709055eSAntonio Nino Diaz * calling convention 560709055eSAntonio Nino Diaz ******************************************************************************/ 570709055eSAntonio Nino Diaz #define OEN_ARM_START U(0) 580709055eSAntonio Nino Diaz #define OEN_ARM_END U(0) 590709055eSAntonio Nino Diaz #define OEN_CPU_START U(1) 600709055eSAntonio Nino Diaz #define OEN_CPU_END U(1) 610709055eSAntonio Nino Diaz #define OEN_SIP_START U(2) 620709055eSAntonio Nino Diaz #define OEN_SIP_END U(2) 630709055eSAntonio Nino Diaz #define OEN_OEM_START U(3) 640709055eSAntonio Nino Diaz #define OEN_OEM_END U(3) 650709055eSAntonio Nino Diaz #define OEN_STD_START U(4) /* Standard Service Calls */ 660709055eSAntonio Nino Diaz #define OEN_STD_END U(4) 670709055eSAntonio Nino Diaz #define OEN_STD_HYP_START U(5) /* Standard Hypervisor Service calls */ 680709055eSAntonio Nino Diaz #define OEN_STD_HYP_END U(5) 690709055eSAntonio Nino Diaz #define OEN_VEN_HYP_START U(6) /* Vendor Hypervisor Service calls */ 700709055eSAntonio Nino Diaz #define OEN_VEN_HYP_END U(6) 710709055eSAntonio Nino Diaz #define OEN_TAP_START U(48) /* Trusted Applications */ 720709055eSAntonio Nino Diaz #define OEN_TAP_END U(49) 730709055eSAntonio Nino Diaz #define OEN_TOS_START U(50) /* Trusted OS */ 740709055eSAntonio Nino Diaz #define OEN_TOS_END U(63) 750709055eSAntonio Nino Diaz #define OEN_LIMIT U(64) 760709055eSAntonio Nino Diaz 770709055eSAntonio Nino Diaz /* Flags and error codes */ 780709055eSAntonio Nino Diaz #define SMC_64 U(1) 790709055eSAntonio Nino Diaz #define SMC_32 U(0) 800709055eSAntonio Nino Diaz 810709055eSAntonio Nino Diaz #define SMC_TYPE_FAST ULL(1) 820709055eSAntonio Nino Diaz #define SMC_TYPE_YIELD ULL(0) 830709055eSAntonio Nino Diaz 840709055eSAntonio Nino Diaz #define SMC_OK ULL(0) 850709055eSAntonio Nino Diaz #define SMC_UNK -1 860709055eSAntonio Nino Diaz #define SMC_PREEMPTED -2 /* Not defined by the SMCCC */ 872f370465SAntonio Nino Diaz 88e34cc0ceSMadhukar Pappireddy /* Return codes for Arm Architecture Service SMC calls */ 89e34cc0ceSMadhukar Pappireddy #define SMC_ARCH_CALL_SUCCESS 0 90e34cc0ceSMadhukar Pappireddy #define SMC_ARCH_CALL_NOT_SUPPORTED -1 91e34cc0ceSMadhukar Pappireddy #define SMC_ARCH_CALL_NOT_REQUIRED -2 92e34cc0ceSMadhukar Pappireddy #define SMC_ARCH_CALL_INVAL_PARAM -3 93e34cc0ceSMadhukar Pappireddy 942f370465SAntonio Nino Diaz /* Various flags passed to SMC handlers */ 952f370465SAntonio Nino Diaz #define SMC_FROM_SECURE (U(0) << 0) 962f370465SAntonio Nino Diaz #define SMC_FROM_NON_SECURE (U(1) << 0) 97085e80ecSAntonio Nino Diaz 98d5dfdeb6SJulius Werner #ifndef __ASSEMBLER__ 99085e80ecSAntonio Nino Diaz 100085e80ecSAntonio Nino Diaz #include <stdint.h> 101085e80ecSAntonio Nino Diaz 10209d40e0eSAntonio Nino Diaz #include <lib/cassert.h> 10309d40e0eSAntonio Nino Diaz 104b3323cd6SAntonio Nino Diaz #define is_caller_non_secure(_f) (((_f) & SMC_FROM_NON_SECURE) != U(0)) 105b3323cd6SAntonio Nino Diaz #define is_caller_secure(_f) (!is_caller_non_secure(_f)) 106085e80ecSAntonio Nino Diaz 107085e80ecSAntonio Nino Diaz /* The macro below is used to identify a Standard Service SMC call */ 1082f370465SAntonio Nino Diaz #define is_std_svc_call(_fid) (GET_SMC_OEN(_fid) == OEN_STD_START) 109085e80ecSAntonio Nino Diaz 110085e80ecSAntonio Nino Diaz /* The macro below is used to identify a Arm Architectural Service SMC call */ 1112f370465SAntonio Nino Diaz #define is_arm_arch_svc_call(_fid) (GET_SMC_OEN(_fid) == OEN_ARM_START) 112085e80ecSAntonio Nino Diaz 113085e80ecSAntonio Nino Diaz /* The macro below is used to identify a valid Fast SMC call */ 114085e80ecSAntonio Nino Diaz #define is_valid_fast_smc(_fid) ((!(((_fid) >> 16) & U(0xff))) && \ 115085e80ecSAntonio Nino Diaz (GET_SMC_TYPE(_fid) == SMC_TYPE_FAST)) 116085e80ecSAntonio Nino Diaz 117085e80ecSAntonio Nino Diaz /* 118085e80ecSAntonio Nino Diaz * Macro to define UUID for services. Apart from defining and initializing a 119085e80ecSAntonio Nino Diaz * uuid_t structure, this macro verifies that the first word of the defined UUID 120085e80ecSAntonio Nino Diaz * does not equal SMC_UNK. This is to ensure that the caller won't mistake the 121085e80ecSAntonio Nino Diaz * returned UUID in x0 for an invalid SMC error return 122085e80ecSAntonio Nino Diaz */ 12303364865SRoberto Vargas #define DEFINE_SVC_UUID2(_name, _tl, _tm, _th, _cl, _ch, \ 12403364865SRoberto Vargas _n0, _n1, _n2, _n3, _n4, _n5) \ 12503364865SRoberto Vargas CASSERT((uint32_t)(_tl) != (uint32_t) SMC_UNK, invalid_svc_uuid);\ 12603364865SRoberto Vargas static const uuid_t _name = { \ 12703364865SRoberto Vargas {(_tl >> 24) & 0xFF, \ 12803364865SRoberto Vargas (_tl >> 16) & 0xFF, \ 12903364865SRoberto Vargas (_tl >> 8) & 0xFF, \ 13003364865SRoberto Vargas (_tl & 0xFF)}, \ 13103364865SRoberto Vargas {(_tm >> 8) & 0xFF, \ 13203364865SRoberto Vargas (_tm & 0xFF)}, \ 13303364865SRoberto Vargas {(_th >> 8) & 0xFF, \ 13403364865SRoberto Vargas (_th & 0xFF)}, \ 13503364865SRoberto Vargas _cl, _ch, \ 13603364865SRoberto Vargas { _n0, _n1, _n2, _n3, _n4, _n5 } \ 13703364865SRoberto Vargas } 138085e80ecSAntonio Nino Diaz 13943b8fa8eSSandrine Bailleux /* 14043b8fa8eSSandrine Bailleux * Return a UUID in the SMC return registers. 14143b8fa8eSSandrine Bailleux * 14243b8fa8eSSandrine Bailleux * Acccording to section 5.3 of the SMCCC, UUIDs are returned as a single 14343b8fa8eSSandrine Bailleux * 128-bit value using the SMC32 calling convention. This value is mapped to 14443b8fa8eSSandrine Bailleux * argument registers x0-x3 on AArch64 (resp. r0-r3 on AArch32). x0 for example 14543b8fa8eSSandrine Bailleux * shall hold bytes 0 to 3, with byte 0 in the low-order bits. 14643b8fa8eSSandrine Bailleux */ 14743b8fa8eSSandrine Bailleux static inline uint32_t smc_uuid_word(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3) 14843b8fa8eSSandrine Bailleux { 14943b8fa8eSSandrine Bailleux return ((uint32_t) b0) | (((uint32_t) b1) << 8) | 15043b8fa8eSSandrine Bailleux (((uint32_t) b2) << 16) | (((uint32_t) b3) << 24); 15143b8fa8eSSandrine Bailleux } 15243b8fa8eSSandrine Bailleux 15343b8fa8eSSandrine Bailleux #define SMC_UUID_RET(_h, _uuid) \ 15443b8fa8eSSandrine Bailleux SMC_RET4(handle, \ 15543b8fa8eSSandrine Bailleux smc_uuid_word((_uuid).time_low[0], (_uuid).time_low[1], \ 15643b8fa8eSSandrine Bailleux (_uuid).time_low[2], (_uuid).time_low[3]), \ 15743b8fa8eSSandrine Bailleux smc_uuid_word((_uuid).time_mid[0], (_uuid).time_mid[1], \ 15843b8fa8eSSandrine Bailleux (_uuid).time_hi_and_version[0], \ 15943b8fa8eSSandrine Bailleux (_uuid).time_hi_and_version[1]), \ 16043b8fa8eSSandrine Bailleux smc_uuid_word((_uuid).clock_seq_hi_and_reserved, \ 16143b8fa8eSSandrine Bailleux (_uuid).clock_seq_low, (_uuid).node[0], \ 16243b8fa8eSSandrine Bailleux (_uuid).node[1]), \ 16343b8fa8eSSandrine Bailleux smc_uuid_word((_uuid).node[2], (_uuid).node[3], \ 16443b8fa8eSSandrine Bailleux (_uuid).node[4], (_uuid).node[5])) 16543b8fa8eSSandrine Bailleux 166d5dfdeb6SJulius Werner #endif /*__ASSEMBLER__*/ 167c3cf06f1SAntonio Nino Diaz #endif /* SMCCC_H */ 168