blob: b8af6cd846a6b7c33c86c6e54d019d81eb0892ef [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Soby Mathewa0fedc42016-06-16 14:52:04 +01002 * Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved.
Achin Gupta4f6ad662013-10-25 09:08:21 +01003 *
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 Mathewa0fedc42016-06-16 14:52:04 +010031#include <assert.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010032#include <debug.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010033#include <errno.h>
Achin Guptac8afc782013-11-25 18:45:02 +000034#include <runtime_svc.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010035#include <string.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010036
37/*******************************************************************************
Achin Gupta7421b462014-02-01 18:53:26 +000038 * 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 Gupta4f6ad662013-10-25 09:08:21 +010045 ******************************************************************************/
Soby Mathewa0fedc42016-06-16 14:52:04 +010046#define RT_SVC_DESCS_START ((uintptr_t) (&__RT_SVC_DESCS_START__))
47#define RT_SVC_DESCS_END ((uintptr_t) (&__RT_SVC_DESCS_END__))
Achin Gupta7421b462014-02-01 18:53:26 +000048uint8_t rt_svc_descs_indices[MAX_RT_SVCS];
Dan Handleye2712bc2014-04-10 15:37:22 +010049static rt_svc_desc_t *rt_svc_descs;
Achin Gupta7421b462014-02-01 18:53:26 +000050
Soby Mathewa0fedc42016-06-16 14:52:04 +010051#define RT_SVC_DECS_NUM ((RT_SVC_DESCS_END - RT_SVC_DESCS_START)\
52 / sizeof(rt_svc_desc_t))
53
Achin Gupta7421b462014-02-01 18:53:26 +000054/*******************************************************************************
55 * Simple routine to sanity check a runtime service descriptor before using it
56 ******************************************************************************/
Sandrine Bailleux47110582016-06-28 16:44:28 +010057static int32_t validate_rt_svc_desc(const rt_svc_desc_t *desc)
Achin Gupta4f6ad662013-10-25 09:08:21 +010058{
Achin Gupta7421b462014-02-01 18:53:26 +000059 if (desc == NULL)
60 return -EINVAL;
61
62 if (desc->start_oen > desc->end_oen)
63 return -EINVAL;
64
65 if (desc->end_oen >= OEN_LIMIT)
66 return -EINVAL;
67
68 if (desc->call_type != SMC_TYPE_FAST && desc->call_type != SMC_TYPE_STD)
69 return -EINVAL;
70
71 /* A runtime service having no init or handle function doesn't make sense */
72 if (desc->init == NULL && desc->handle == NULL)
73 return -EINVAL;
74
75 return 0;
76}
77
78/*******************************************************************************
79 * This function calls the initialisation routine in the descriptor exported by
80 * a runtime service. Once a descriptor has been validated, its start & end
81 * owning entity numbers and the call type are combined to form a unique oen.
82 * The unique oen is used as an index into the 'rt_svc_descs_indices' array.
83 * The index of the runtime service descriptor is stored at this index.
84 ******************************************************************************/
Juan Castillo2d552402014-06-13 17:05:10 +010085void runtime_svc_init(void)
Achin Gupta7421b462014-02-01 18:53:26 +000086{
Soby Mathewa0fedc42016-06-16 14:52:04 +010087 int rc = 0, index, start_idx, end_idx;
88
89 /* Assert the number of descriptors detected are less than maximum indices */
Soby Mathewff1c2292016-07-26 16:09:44 +010090 assert((RT_SVC_DESCS_END >= RT_SVC_DESCS_START) &&
91 (RT_SVC_DECS_NUM < MAX_RT_SVCS));
Achin Gupta7421b462014-02-01 18:53:26 +000092
93 /* If no runtime services are implemented then simply bail out */
Soby Mathewa0fedc42016-06-16 14:52:04 +010094 if (RT_SVC_DECS_NUM == 0)
Achin Gupta7421b462014-02-01 18:53:26 +000095 return;
96
97 /* Initialise internal variables to invalid state */
98 memset(rt_svc_descs_indices, -1, sizeof(rt_svc_descs_indices));
99
Dan Handleye2712bc2014-04-10 15:37:22 +0100100 rt_svc_descs = (rt_svc_desc_t *) RT_SVC_DESCS_START;
Soby Mathewa0fedc42016-06-16 14:52:04 +0100101 for (index = 0; index < RT_SVC_DECS_NUM; index++) {
Sandrine Bailleux47110582016-06-28 16:44:28 +0100102 rt_svc_desc_t *service = &rt_svc_descs[index];
Achin Gupta7421b462014-02-01 18:53:26 +0000103
104 /*
105 * An invalid descriptor is an error condition since it is
106 * difficult to predict the system behaviour in the absence
107 * of this service.
108 */
Sandrine Bailleux47110582016-06-28 16:44:28 +0100109 rc = validate_rt_svc_desc(service);
Achin Gupta7421b462014-02-01 18:53:26 +0000110 if (rc) {
Sandrine Bailleux7304f452016-06-28 16:48:30 +0100111 ERROR("Invalid runtime service descriptor %p\n",
112 (void *) service);
Sandrine Bailleux47110582016-06-28 16:44:28 +0100113 panic();
Achin Gupta7421b462014-02-01 18:53:26 +0000114 }
115
Soby Mathew9f71f702014-05-09 20:49:17 +0100116 /*
Sandrine Bailleuxf4119ec2015-12-17 13:58:58 +0000117 * The runtime service may have separate rt_svc_desc_t
Soby Mathew9f71f702014-05-09 20:49:17 +0100118 * for its fast smc and standard smc. Since the service itself
119 * need to be initialized only once, only one of them will have
120 * an initialisation routine defined. Call the initialisation
121 * routine for this runtime service, if it is defined.
122 */
Sandrine Bailleux47110582016-06-28 16:44:28 +0100123 if (service->init) {
124 rc = service->init();
Soby Mathew9f71f702014-05-09 20:49:17 +0100125 if (rc) {
126 ERROR("Error initializing runtime service %s\n",
Sandrine Bailleux47110582016-06-28 16:44:28 +0100127 service->name);
Soby Mathew9f71f702014-05-09 20:49:17 +0100128 continue;
129 }
Achin Gupta7421b462014-02-01 18:53:26 +0000130 }
Soby Mathew9f71f702014-05-09 20:49:17 +0100131
132 /*
133 * Fill the indices corresponding to the start and end
134 * owning entity numbers with the index of the
135 * descriptor which will handle the SMCs for this owning
136 * entity range.
137 */
138 start_idx = get_unique_oen(rt_svc_descs[index].start_oen,
Sandrine Bailleux47110582016-06-28 16:44:28 +0100139 service->call_type);
Sandrine Bailleux7304f452016-06-28 16:48:30 +0100140 assert(start_idx < MAX_RT_SVCS);
Soby Mathew9f71f702014-05-09 20:49:17 +0100141 end_idx = get_unique_oen(rt_svc_descs[index].end_oen,
Sandrine Bailleux47110582016-06-28 16:44:28 +0100142 service->call_type);
Sandrine Bailleux7304f452016-06-28 16:48:30 +0100143 assert(end_idx < MAX_RT_SVCS);
Soby Mathew9f71f702014-05-09 20:49:17 +0100144 for (; start_idx <= end_idx; start_idx++)
145 rt_svc_descs_indices[start_idx] = index;
Achin Gupta7421b462014-02-01 18:53:26 +0000146 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100147}