1532ed618SSoby Mathew /* 2*16292f54SDavid Cunado * Copyright (c) 2013-2017, ARM Limited and Contributors. All rights reserved. 3532ed618SSoby Mathew * 4532ed618SSoby Mathew * Redistribution and use in source and binary forms, with or without 5532ed618SSoby Mathew * modification, are permitted provided that the following conditions are met: 6532ed618SSoby Mathew * 7532ed618SSoby Mathew * Redistributions of source code must retain the above copyright notice, this 8532ed618SSoby Mathew * list of conditions and the following disclaimer. 9532ed618SSoby Mathew * 10532ed618SSoby Mathew * Redistributions in binary form must reproduce the above copyright notice, 11532ed618SSoby Mathew * this list of conditions and the following disclaimer in the documentation 12532ed618SSoby Mathew * and/or other materials provided with the distribution. 13532ed618SSoby Mathew * 14532ed618SSoby Mathew * Neither the name of ARM nor the names of its contributors may be used 15532ed618SSoby Mathew * to endorse or promote products derived from this software without specific 16532ed618SSoby Mathew * prior written permission. 17532ed618SSoby Mathew * 18532ed618SSoby Mathew * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19532ed618SSoby Mathew * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20532ed618SSoby Mathew * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21532ed618SSoby Mathew * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22532ed618SSoby Mathew * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23532ed618SSoby Mathew * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24532ed618SSoby Mathew * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25532ed618SSoby Mathew * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26532ed618SSoby Mathew * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27532ed618SSoby Mathew * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28532ed618SSoby Mathew * POSSIBILITY OF SUCH DAMAGE. 29532ed618SSoby Mathew */ 30532ed618SSoby Mathew 31532ed618SSoby Mathew #include <assert.h> 32532ed618SSoby Mathew #include <debug.h> 33532ed618SSoby Mathew #include <errno.h> 34532ed618SSoby Mathew #include <runtime_svc.h> 35532ed618SSoby Mathew #include <string.h> 36532ed618SSoby Mathew 37532ed618SSoby Mathew /******************************************************************************* 38532ed618SSoby Mathew * The 'rt_svc_descs' array holds the runtime service descriptors exported by 39532ed618SSoby Mathew * services by placing them in the 'rt_svc_descs' linker section. 40532ed618SSoby Mathew * The 'rt_svc_descs_indices' array holds the index of a descriptor in the 41532ed618SSoby Mathew * 'rt_svc_descs' array. When an SMC arrives, the OEN[29:24] bits and the call 42532ed618SSoby Mathew * type[31] bit in the function id are combined to get an index into the 43532ed618SSoby Mathew * 'rt_svc_descs_indices' array. This gives the index of the descriptor in the 44532ed618SSoby Mathew * 'rt_svc_descs' array which contains the SMC handler. 45532ed618SSoby Mathew ******************************************************************************/ 46532ed618SSoby Mathew #define RT_SVC_DESCS_START ((uintptr_t) (&__RT_SVC_DESCS_START__)) 47532ed618SSoby Mathew #define RT_SVC_DESCS_END ((uintptr_t) (&__RT_SVC_DESCS_END__)) 48532ed618SSoby Mathew uint8_t rt_svc_descs_indices[MAX_RT_SVCS]; 49532ed618SSoby Mathew static rt_svc_desc_t *rt_svc_descs; 50532ed618SSoby Mathew 51532ed618SSoby Mathew #define RT_SVC_DECS_NUM ((RT_SVC_DESCS_END - RT_SVC_DESCS_START)\ 52532ed618SSoby Mathew / sizeof(rt_svc_desc_t)) 53532ed618SSoby Mathew 54532ed618SSoby Mathew /******************************************************************************* 551ae0a49aSSoby Mathew * Function to invoke the registered `handle` corresponding to the smc_fid. 561ae0a49aSSoby Mathew ******************************************************************************/ 571ae0a49aSSoby Mathew uintptr_t handle_runtime_svc(uint32_t smc_fid, 581ae0a49aSSoby Mathew void *cookie, 591ae0a49aSSoby Mathew void *handle, 601ae0a49aSSoby Mathew unsigned int flags) 611ae0a49aSSoby Mathew { 621ae0a49aSSoby Mathew u_register_t x1, x2, x3, x4; 631ae0a49aSSoby Mathew int index, idx; 641ae0a49aSSoby Mathew const rt_svc_desc_t *rt_svc_descs; 651ae0a49aSSoby Mathew 661ae0a49aSSoby Mathew assert(handle); 671ae0a49aSSoby Mathew idx = get_unique_oen_from_smc_fid(smc_fid); 681ae0a49aSSoby Mathew assert(idx >= 0 && idx < MAX_RT_SVCS); 691ae0a49aSSoby Mathew 701ae0a49aSSoby Mathew index = rt_svc_descs_indices[idx]; 711ae0a49aSSoby Mathew if (index < 0 || index >= RT_SVC_DECS_NUM) 721ae0a49aSSoby Mathew SMC_RET1(handle, SMC_UNK); 731ae0a49aSSoby Mathew 741ae0a49aSSoby Mathew rt_svc_descs = (rt_svc_desc_t *) RT_SVC_DESCS_START; 751ae0a49aSSoby Mathew 761ae0a49aSSoby Mathew get_smc_params_from_ctx(handle, x1, x2, x3, x4); 771ae0a49aSSoby Mathew 781ae0a49aSSoby Mathew return rt_svc_descs[index].handle(smc_fid, x1, x2, x3, x4, cookie, 791ae0a49aSSoby Mathew handle, flags); 801ae0a49aSSoby Mathew } 811ae0a49aSSoby Mathew 821ae0a49aSSoby Mathew /******************************************************************************* 83532ed618SSoby Mathew * Simple routine to sanity check a runtime service descriptor before using it 84532ed618SSoby Mathew ******************************************************************************/ 859d24d353SSandrine Bailleux static int32_t validate_rt_svc_desc(const rt_svc_desc_t *desc) 86532ed618SSoby Mathew { 87532ed618SSoby Mathew if (desc == NULL) 88532ed618SSoby Mathew return -EINVAL; 89532ed618SSoby Mathew 90532ed618SSoby Mathew if (desc->start_oen > desc->end_oen) 91532ed618SSoby Mathew return -EINVAL; 92532ed618SSoby Mathew 93532ed618SSoby Mathew if (desc->end_oen >= OEN_LIMIT) 94532ed618SSoby Mathew return -EINVAL; 95532ed618SSoby Mathew 96*16292f54SDavid Cunado if (desc->call_type != SMC_TYPE_FAST && 97*16292f54SDavid Cunado desc->call_type != SMC_TYPE_YIELD) 98532ed618SSoby Mathew return -EINVAL; 99532ed618SSoby Mathew 100532ed618SSoby Mathew /* A runtime service having no init or handle function doesn't make sense */ 101532ed618SSoby Mathew if (desc->init == NULL && desc->handle == NULL) 102532ed618SSoby Mathew return -EINVAL; 103532ed618SSoby Mathew 104532ed618SSoby Mathew return 0; 105532ed618SSoby Mathew } 106532ed618SSoby Mathew 107532ed618SSoby Mathew /******************************************************************************* 108532ed618SSoby Mathew * This function calls the initialisation routine in the descriptor exported by 109532ed618SSoby Mathew * a runtime service. Once a descriptor has been validated, its start & end 110532ed618SSoby Mathew * owning entity numbers and the call type are combined to form a unique oen. 111532ed618SSoby Mathew * The unique oen is used as an index into the 'rt_svc_descs_indices' array. 112532ed618SSoby Mathew * The index of the runtime service descriptor is stored at this index. 113532ed618SSoby Mathew ******************************************************************************/ 114532ed618SSoby Mathew void runtime_svc_init(void) 115532ed618SSoby Mathew { 116532ed618SSoby Mathew int rc = 0, index, start_idx, end_idx; 117532ed618SSoby Mathew 118532ed618SSoby Mathew /* Assert the number of descriptors detected are less than maximum indices */ 1195e5e4162SSoby Mathew assert((RT_SVC_DESCS_END >= RT_SVC_DESCS_START) && 1205e5e4162SSoby Mathew (RT_SVC_DECS_NUM < MAX_RT_SVCS)); 121532ed618SSoby Mathew 122532ed618SSoby Mathew /* If no runtime services are implemented then simply bail out */ 123532ed618SSoby Mathew if (RT_SVC_DECS_NUM == 0) 124532ed618SSoby Mathew return; 125532ed618SSoby Mathew 126532ed618SSoby Mathew /* Initialise internal variables to invalid state */ 127532ed618SSoby Mathew memset(rt_svc_descs_indices, -1, sizeof(rt_svc_descs_indices)); 128532ed618SSoby Mathew 129532ed618SSoby Mathew rt_svc_descs = (rt_svc_desc_t *) RT_SVC_DESCS_START; 130532ed618SSoby Mathew for (index = 0; index < RT_SVC_DECS_NUM; index++) { 1319d24d353SSandrine Bailleux rt_svc_desc_t *service = &rt_svc_descs[index]; 132532ed618SSoby Mathew 133532ed618SSoby Mathew /* 134532ed618SSoby Mathew * An invalid descriptor is an error condition since it is 135532ed618SSoby Mathew * difficult to predict the system behaviour in the absence 136532ed618SSoby Mathew * of this service. 137532ed618SSoby Mathew */ 1389d24d353SSandrine Bailleux rc = validate_rt_svc_desc(service); 139532ed618SSoby Mathew if (rc) { 1403a26a28cSSandrine Bailleux ERROR("Invalid runtime service descriptor %p\n", 1413a26a28cSSandrine Bailleux (void *) service); 1429d24d353SSandrine Bailleux panic(); 143532ed618SSoby Mathew } 144532ed618SSoby Mathew 145532ed618SSoby Mathew /* 146532ed618SSoby Mathew * The runtime service may have separate rt_svc_desc_t 147*16292f54SDavid Cunado * for its fast smc and yielding smc. Since the service itself 148532ed618SSoby Mathew * need to be initialized only once, only one of them will have 149532ed618SSoby Mathew * an initialisation routine defined. Call the initialisation 150532ed618SSoby Mathew * routine for this runtime service, if it is defined. 151532ed618SSoby Mathew */ 1529d24d353SSandrine Bailleux if (service->init) { 1539d24d353SSandrine Bailleux rc = service->init(); 154532ed618SSoby Mathew if (rc) { 155532ed618SSoby Mathew ERROR("Error initializing runtime service %s\n", 1569d24d353SSandrine Bailleux service->name); 157532ed618SSoby Mathew continue; 158532ed618SSoby Mathew } 159532ed618SSoby Mathew } 160532ed618SSoby Mathew 161532ed618SSoby Mathew /* 162532ed618SSoby Mathew * Fill the indices corresponding to the start and end 163532ed618SSoby Mathew * owning entity numbers with the index of the 164532ed618SSoby Mathew * descriptor which will handle the SMCs for this owning 165532ed618SSoby Mathew * entity range. 166532ed618SSoby Mathew */ 167532ed618SSoby Mathew start_idx = get_unique_oen(rt_svc_descs[index].start_oen, 1689d24d353SSandrine Bailleux service->call_type); 1693a26a28cSSandrine Bailleux assert(start_idx < MAX_RT_SVCS); 170532ed618SSoby Mathew end_idx = get_unique_oen(rt_svc_descs[index].end_oen, 1719d24d353SSandrine Bailleux service->call_type); 1723a26a28cSSandrine Bailleux assert(end_idx < MAX_RT_SVCS); 173532ed618SSoby Mathew for (; start_idx <= end_idx; start_idx++) 174532ed618SSoby Mathew rt_svc_descs_indices[start_idx] = index; 175532ed618SSoby Mathew } 176532ed618SSoby Mathew } 177