Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 1 | /* |
David Cunado | 16292f5 | 2017-04-05 11:34:03 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2013-2017, ARM Limited and Contributors. All rights reserved. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this |
| 8 | * list of conditions and the following disclaimer. |
| 9 | * |
| 10 | * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * Neither the name of ARM nor the names of its contributors may be used |
| 15 | * to endorse or promote products derived from this software without specific |
| 16 | * prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
Soby Mathew | 4c0d039 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 31 | #include <assert.h> |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 32 | #include <debug.h> |
Dan Handley | 97043ac | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 33 | #include <errno.h> |
| 34 | #include <runtime_svc.h> |
| 35 | #include <string.h> |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 36 | |
| 37 | /******************************************************************************* |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 38 | * The 'rt_svc_descs' array holds the runtime service descriptors exported by |
| 39 | * services by placing them in the 'rt_svc_descs' linker section. |
| 40 | * The 'rt_svc_descs_indices' array holds the index of a descriptor in the |
| 41 | * 'rt_svc_descs' array. When an SMC arrives, the OEN[29:24] bits and the call |
| 42 | * type[31] bit in the function id are combined to get an index into the |
| 43 | * 'rt_svc_descs_indices' array. This gives the index of the descriptor in the |
| 44 | * 'rt_svc_descs' array which contains the SMC handler. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 45 | ******************************************************************************/ |
Soby Mathew | 4c0d039 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 46 | #define RT_SVC_DESCS_START ((uintptr_t) (&__RT_SVC_DESCS_START__)) |
| 47 | #define RT_SVC_DESCS_END ((uintptr_t) (&__RT_SVC_DESCS_END__)) |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 48 | uint8_t rt_svc_descs_indices[MAX_RT_SVCS]; |
Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 49 | static rt_svc_desc_t *rt_svc_descs; |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 50 | |
Soby Mathew | 4c0d039 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 51 | #define RT_SVC_DECS_NUM ((RT_SVC_DESCS_END - RT_SVC_DESCS_START)\ |
| 52 | / sizeof(rt_svc_desc_t)) |
| 53 | |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 54 | /******************************************************************************* |
Soby Mathew | 1ae0a49 | 2016-05-05 12:49:09 +0100 | [diff] [blame] | 55 | * Function to invoke the registered `handle` corresponding to the smc_fid. |
| 56 | ******************************************************************************/ |
| 57 | uintptr_t handle_runtime_svc(uint32_t smc_fid, |
| 58 | void *cookie, |
| 59 | void *handle, |
| 60 | unsigned int flags) |
| 61 | { |
| 62 | u_register_t x1, x2, x3, x4; |
| 63 | int index, idx; |
| 64 | const rt_svc_desc_t *rt_svc_descs; |
| 65 | |
| 66 | assert(handle); |
| 67 | idx = get_unique_oen_from_smc_fid(smc_fid); |
| 68 | assert(idx >= 0 && idx < MAX_RT_SVCS); |
| 69 | |
| 70 | index = rt_svc_descs_indices[idx]; |
| 71 | if (index < 0 || index >= RT_SVC_DECS_NUM) |
| 72 | SMC_RET1(handle, SMC_UNK); |
| 73 | |
| 74 | rt_svc_descs = (rt_svc_desc_t *) RT_SVC_DESCS_START; |
| 75 | |
| 76 | get_smc_params_from_ctx(handle, x1, x2, x3, x4); |
| 77 | |
| 78 | return rt_svc_descs[index].handle(smc_fid, x1, x2, x3, x4, cookie, |
| 79 | handle, flags); |
| 80 | } |
| 81 | |
| 82 | /******************************************************************************* |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 83 | * Simple routine to sanity check a runtime service descriptor before using it |
| 84 | ******************************************************************************/ |
Sandrine Bailleux | 9d24d35 | 2016-06-28 16:44:28 +0100 | [diff] [blame] | 85 | static int32_t validate_rt_svc_desc(const rt_svc_desc_t *desc) |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 86 | { |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 87 | if (desc == NULL) |
| 88 | return -EINVAL; |
| 89 | |
| 90 | if (desc->start_oen > desc->end_oen) |
| 91 | return -EINVAL; |
| 92 | |
| 93 | if (desc->end_oen >= OEN_LIMIT) |
| 94 | return -EINVAL; |
| 95 | |
David Cunado | 16292f5 | 2017-04-05 11:34:03 +0100 | [diff] [blame^] | 96 | if (desc->call_type != SMC_TYPE_FAST && |
| 97 | desc->call_type != SMC_TYPE_YIELD) |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 98 | return -EINVAL; |
| 99 | |
| 100 | /* A runtime service having no init or handle function doesn't make sense */ |
| 101 | if (desc->init == NULL && desc->handle == NULL) |
| 102 | return -EINVAL; |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | /******************************************************************************* |
| 108 | * This function calls the initialisation routine in the descriptor exported by |
| 109 | * a runtime service. Once a descriptor has been validated, its start & end |
| 110 | * owning entity numbers and the call type are combined to form a unique oen. |
| 111 | * The unique oen is used as an index into the 'rt_svc_descs_indices' array. |
| 112 | * The index of the runtime service descriptor is stored at this index. |
| 113 | ******************************************************************************/ |
Juan Castillo | 4f2104f | 2014-06-13 17:05:10 +0100 | [diff] [blame] | 114 | void runtime_svc_init(void) |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 115 | { |
Soby Mathew | 4c0d039 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 116 | int rc = 0, index, start_idx, end_idx; |
| 117 | |
| 118 | /* Assert the number of descriptors detected are less than maximum indices */ |
Soby Mathew | 5e5e416 | 2016-07-26 16:09:44 +0100 | [diff] [blame] | 119 | assert((RT_SVC_DESCS_END >= RT_SVC_DESCS_START) && |
| 120 | (RT_SVC_DECS_NUM < MAX_RT_SVCS)); |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 121 | |
| 122 | /* If no runtime services are implemented then simply bail out */ |
Soby Mathew | 4c0d039 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 123 | if (RT_SVC_DECS_NUM == 0) |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 124 | return; |
| 125 | |
| 126 | /* Initialise internal variables to invalid state */ |
| 127 | memset(rt_svc_descs_indices, -1, sizeof(rt_svc_descs_indices)); |
| 128 | |
Dan Handley | fb037bf | 2014-04-10 15:37:22 +0100 | [diff] [blame] | 129 | rt_svc_descs = (rt_svc_desc_t *) RT_SVC_DESCS_START; |
Soby Mathew | 4c0d039 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 130 | for (index = 0; index < RT_SVC_DECS_NUM; index++) { |
Sandrine Bailleux | 9d24d35 | 2016-06-28 16:44:28 +0100 | [diff] [blame] | 131 | rt_svc_desc_t *service = &rt_svc_descs[index]; |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 132 | |
| 133 | /* |
| 134 | * An invalid descriptor is an error condition since it is |
| 135 | * difficult to predict the system behaviour in the absence |
| 136 | * of this service. |
| 137 | */ |
Sandrine Bailleux | 9d24d35 | 2016-06-28 16:44:28 +0100 | [diff] [blame] | 138 | rc = validate_rt_svc_desc(service); |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 139 | if (rc) { |
Sandrine Bailleux | 3a26a28 | 2016-06-28 16:48:30 +0100 | [diff] [blame] | 140 | ERROR("Invalid runtime service descriptor %p\n", |
| 141 | (void *) service); |
Sandrine Bailleux | 9d24d35 | 2016-06-28 16:44:28 +0100 | [diff] [blame] | 142 | panic(); |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Soby Mathew | 239b04f | 2014-05-09 20:49:17 +0100 | [diff] [blame] | 145 | /* |
Sandrine Bailleux | 1645d3e | 2015-12-17 13:58:58 +0000 | [diff] [blame] | 146 | * The runtime service may have separate rt_svc_desc_t |
David Cunado | 16292f5 | 2017-04-05 11:34:03 +0100 | [diff] [blame^] | 147 | * for its fast smc and yielding smc. Since the service itself |
Soby Mathew | 239b04f | 2014-05-09 20:49:17 +0100 | [diff] [blame] | 148 | * need to be initialized only once, only one of them will have |
| 149 | * an initialisation routine defined. Call the initialisation |
| 150 | * routine for this runtime service, if it is defined. |
| 151 | */ |
Sandrine Bailleux | 9d24d35 | 2016-06-28 16:44:28 +0100 | [diff] [blame] | 152 | if (service->init) { |
| 153 | rc = service->init(); |
Soby Mathew | 239b04f | 2014-05-09 20:49:17 +0100 | [diff] [blame] | 154 | if (rc) { |
| 155 | ERROR("Error initializing runtime service %s\n", |
Sandrine Bailleux | 9d24d35 | 2016-06-28 16:44:28 +0100 | [diff] [blame] | 156 | service->name); |
Soby Mathew | 239b04f | 2014-05-09 20:49:17 +0100 | [diff] [blame] | 157 | continue; |
| 158 | } |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 159 | } |
Soby Mathew | 239b04f | 2014-05-09 20:49:17 +0100 | [diff] [blame] | 160 | |
| 161 | /* |
| 162 | * Fill the indices corresponding to the start and end |
| 163 | * owning entity numbers with the index of the |
| 164 | * descriptor which will handle the SMCs for this owning |
| 165 | * entity range. |
| 166 | */ |
| 167 | start_idx = get_unique_oen(rt_svc_descs[index].start_oen, |
Sandrine Bailleux | 9d24d35 | 2016-06-28 16:44:28 +0100 | [diff] [blame] | 168 | service->call_type); |
Sandrine Bailleux | 3a26a28 | 2016-06-28 16:48:30 +0100 | [diff] [blame] | 169 | assert(start_idx < MAX_RT_SVCS); |
Soby Mathew | 239b04f | 2014-05-09 20:49:17 +0100 | [diff] [blame] | 170 | end_idx = get_unique_oen(rt_svc_descs[index].end_oen, |
Sandrine Bailleux | 9d24d35 | 2016-06-28 16:44:28 +0100 | [diff] [blame] | 171 | service->call_type); |
Sandrine Bailleux | 3a26a28 | 2016-06-28 16:48:30 +0100 | [diff] [blame] | 172 | assert(end_idx < MAX_RT_SVCS); |
Soby Mathew | 239b04f | 2014-05-09 20:49:17 +0100 | [diff] [blame] | 173 | for (; start_idx <= end_idx; start_idx++) |
| 174 | rt_svc_descs_indices[start_idx] = index; |
Achin Gupta | 7421b46 | 2014-02-01 18:53:26 +0000 | [diff] [blame] | 175 | } |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 176 | } |