blob: 08cd2d859b91e4c7d2c0071c0bce0b3cb144caf2 [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Dan Handleye83b0ca2014-01-14 18:17:09 +00002 * Copyright (c) 2013-2014, 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
Dan Handley2bd4ef22014-04-09 13:14:54 +010031#include <debug.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010032#include <errno.h>
Achin Guptac8afc782013-11-25 18:45:02 +000033#include <runtime_svc.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010034#include <string.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010035
36/*******************************************************************************
Achin Gupta7421b462014-02-01 18:53:26 +000037 * The 'rt_svc_descs' array holds the runtime service descriptors exported by
38 * services by placing them in the 'rt_svc_descs' linker section.
39 * The 'rt_svc_descs_indices' array holds the index of a descriptor in the
40 * 'rt_svc_descs' array. When an SMC arrives, the OEN[29:24] bits and the call
41 * type[31] bit in the function id are combined to get an index into the
42 * 'rt_svc_descs_indices' array. This gives the index of the descriptor in the
43 * 'rt_svc_descs' array which contains the SMC handler.
Achin Gupta4f6ad662013-10-25 09:08:21 +010044 ******************************************************************************/
Achin Gupta7421b462014-02-01 18:53:26 +000045#define RT_SVC_DESCS_START ((uint64_t) (&__RT_SVC_DESCS_START__))
46#define RT_SVC_DESCS_END ((uint64_t) (&__RT_SVC_DESCS_END__))
47uint8_t rt_svc_descs_indices[MAX_RT_SVCS];
Dan Handleye2712bc2014-04-10 15:37:22 +010048static rt_svc_desc_t *rt_svc_descs;
Achin Gupta7421b462014-02-01 18:53:26 +000049
50/*******************************************************************************
51 * Simple routine to sanity check a runtime service descriptor before using it
52 ******************************************************************************/
Dan Handleye2712bc2014-04-10 15:37:22 +010053static int32_t validate_rt_svc_desc(rt_svc_desc_t *desc)
Achin Gupta4f6ad662013-10-25 09:08:21 +010054{
Achin Gupta7421b462014-02-01 18:53:26 +000055 if (desc == NULL)
56 return -EINVAL;
57
58 if (desc->start_oen > desc->end_oen)
59 return -EINVAL;
60
61 if (desc->end_oen >= OEN_LIMIT)
62 return -EINVAL;
63
64 if (desc->call_type != SMC_TYPE_FAST && desc->call_type != SMC_TYPE_STD)
65 return -EINVAL;
66
67 /* A runtime service having no init or handle function doesn't make sense */
68 if (desc->init == NULL && desc->handle == NULL)
69 return -EINVAL;
70
71 return 0;
72}
73
74/*******************************************************************************
75 * This function calls the initialisation routine in the descriptor exported by
76 * a runtime service. Once a descriptor has been validated, its start & end
77 * owning entity numbers and the call type are combined to form a unique oen.
78 * The unique oen is used as an index into the 'rt_svc_descs_indices' array.
79 * The index of the runtime service descriptor is stored at this index.
80 ******************************************************************************/
81void runtime_svc_init()
82{
83 int32_t rc = 0;
84 uint32_t index, start_idx, end_idx;
85 uint64_t rt_svc_descs_num;
86
87 /* If no runtime services are implemented then simply bail out */
88 rt_svc_descs_num = RT_SVC_DESCS_END - RT_SVC_DESCS_START;
Dan Handleye2712bc2014-04-10 15:37:22 +010089 rt_svc_descs_num /= sizeof(rt_svc_desc_t);
Achin Gupta7421b462014-02-01 18:53:26 +000090 if (rt_svc_descs_num == 0)
91 return;
92
93 /* Initialise internal variables to invalid state */
94 memset(rt_svc_descs_indices, -1, sizeof(rt_svc_descs_indices));
95
Dan Handleye2712bc2014-04-10 15:37:22 +010096 rt_svc_descs = (rt_svc_desc_t *) RT_SVC_DESCS_START;
Achin Gupta7421b462014-02-01 18:53:26 +000097 for (index = 0; index < rt_svc_descs_num; index++) {
98
99 /*
100 * An invalid descriptor is an error condition since it is
101 * difficult to predict the system behaviour in the absence
102 * of this service.
103 */
104 rc = validate_rt_svc_desc(&rt_svc_descs[index]);
105 if (rc) {
106 ERROR("Invalid runtime service descriptor 0x%x (%s)\n",
107 &rt_svc_descs[index],
108 rt_svc_descs[index].name);
109 goto error;
110 }
111
Soby Mathew9f71f702014-05-09 20:49:17 +0100112 /*
113 * The runtime service may have seperate rt_svc_desc_t
114 * for its fast smc and standard smc. Since the service itself
115 * need to be initialized only once, only one of them will have
116 * an initialisation routine defined. Call the initialisation
117 * routine for this runtime service, if it is defined.
118 */
119 if (rt_svc_descs[index].init) {
120 rc = rt_svc_descs[index].init();
121 if (rc) {
122 ERROR("Error initializing runtime service %s\n",
123 rt_svc_descs[index].name);
124 continue;
125 }
Achin Gupta7421b462014-02-01 18:53:26 +0000126 }
Soby Mathew9f71f702014-05-09 20:49:17 +0100127
128 /*
129 * Fill the indices corresponding to the start and end
130 * owning entity numbers with the index of the
131 * descriptor which will handle the SMCs for this owning
132 * entity range.
133 */
134 start_idx = get_unique_oen(rt_svc_descs[index].start_oen,
135 rt_svc_descs[index].call_type);
136 end_idx = get_unique_oen(rt_svc_descs[index].end_oen,
137 rt_svc_descs[index].call_type);
138
139 for (; start_idx <= end_idx; start_idx++)
140 rt_svc_descs_indices[start_idx] = index;
Achin Gupta7421b462014-02-01 18:53:26 +0000141 }
142
Achin Gupta4f6ad662013-10-25 09:08:21 +0100143 return;
Achin Gupta7421b462014-02-01 18:53:26 +0000144error:
145 panic();
Achin Gupta4f6ad662013-10-25 09:08:21 +0100146}