xref: /optee_os/core/kernel/dt.c (revision a5d5bbc82de95f531512fafa76a42c879e81b4c4)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2016, Linaro Limited
4  */
5 
6 #include <assert.h>
7 #include <kernel/dt.h>
8 #include <kernel/interrupt.h>
9 #include <kernel/linker.h>
10 #include <libfdt.h>
11 #include <mm/core_memprot.h>
12 #include <mm/core_mmu.h>
13 #include <string.h>
14 #include <trace.h>
15 
16 const struct dt_driver *dt_find_compatible_driver(const void *fdt, int offs)
17 {
18 	const struct dt_device_match *dm;
19 	const struct dt_driver *drv;
20 
21 	for_each_dt_driver(drv) {
22 		for (dm = drv->match_table; dm; dm++) {
23 			if (!dm->compatible) {
24 				break;
25 			}
26 			if (!fdt_node_check_compatible(fdt, offs,
27 						       dm->compatible)) {
28 				return drv;
29 			}
30 		}
31 	}
32 
33 	return NULL;
34 }
35 
36 bool dt_have_prop(const void *fdt, int offs, const char *propname)
37 {
38 	const void *prop;
39 
40 	prop = fdt_getprop(fdt, offs, propname, NULL);
41 
42 	return prop;
43 }
44 
45 int dt_disable_status(void *fdt, int node)
46 {
47 	const char *prop = NULL;
48 	int len = 0;
49 
50 	prop = fdt_getprop(fdt, node, "status", &len);
51 	if (!prop) {
52 		if (fdt_setprop_string(fdt, node, "status", "disabled"))
53 			return -1;
54 	} else {
55 		/*
56 		 * Status is there, modify it.
57 		 * Ask to set "disabled" value to the property. The value
58 		 * will be automatically truncated with "len" size by the
59 		 * fdt_setprop_inplace function.
60 		 * Setting a value different from "ok" or "okay" will disable
61 		 * the property.
62 		 * Setting a truncated value of "disabled" with the original
63 		 * property "len" is preferred to not increase the DT size and
64 		 * losing time in recalculating the overall DT offsets.
65 		 * If original length of the status property is larger than
66 		 * "disabled", the property will start with "disabled" and be
67 		 * completed with the rest of the original property.
68 		 */
69 		if (fdt_setprop_inplace(fdt, node, "status", "disabled", len))
70 			return -1;
71 	}
72 
73 	return 0;
74 }
75 
76 int dt_enable_secure_status(void *fdt, int node)
77 {
78 	if (dt_disable_status(fdt, node)) {
79 		EMSG("Unable to disable Normal Status");
80 		return -1;
81 	}
82 
83 	if (fdt_setprop_string(fdt, node, "secure-status", "okay"))
84 		return -1;
85 
86 	return 0;
87 }
88 
89 int dt_map_dev(const void *fdt, int offs, vaddr_t *base, size_t *size,
90 	       enum dt_map_dev_directive mapping)
91 {
92 	enum teecore_memtypes mtype;
93 	paddr_t pbase;
94 	vaddr_t vbase;
95 	size_t sz;
96 	int st;
97 
98 	assert(cpu_mmu_enabled());
99 
100 	st = _fdt_get_status(fdt, offs);
101 	if (st == DT_STATUS_DISABLED)
102 		return -1;
103 
104 	pbase = _fdt_reg_base_address(fdt, offs);
105 	if (pbase == DT_INFO_INVALID_REG)
106 		return -1;
107 	sz = _fdt_reg_size(fdt, offs);
108 	if (sz == DT_INFO_INVALID_REG_SIZE)
109 		return -1;
110 
111 	switch (mapping) {
112 	case DT_MAP_AUTO:
113 		if ((st & DT_STATUS_OK_SEC) && !(st & DT_STATUS_OK_NSEC))
114 			mtype = MEM_AREA_IO_SEC;
115 		else
116 			mtype = MEM_AREA_IO_NSEC;
117 		break;
118 	case DT_MAP_SECURE:
119 		mtype = MEM_AREA_IO_SEC;
120 		break;
121 	case DT_MAP_NON_SECURE:
122 		mtype = MEM_AREA_IO_NSEC;
123 		break;
124 	default:
125 		panic("Invalid mapping specified");
126 		break;
127 	}
128 
129 	/* Check if we have a mapping, create one if needed */
130 	vbase = (vaddr_t)core_mmu_add_mapping(mtype, pbase, sz);
131 	if (!vbase) {
132 		EMSG("Failed to map %zu bytes at PA 0x%"PRIxPA,
133 		     (size_t)sz, pbase);
134 		return -1;
135 	}
136 
137 	*base = vbase;
138 	*size = sz;
139 	return 0;
140 }
141 
142 /* Read a physical address (n=1 or 2 cells) */
143 static paddr_t _fdt_read_paddr(const uint32_t *cell, int n)
144 {
145 	paddr_t addr;
146 
147 	if (n < 1 || n > 2)
148 		goto bad;
149 
150 	addr = fdt32_to_cpu(*cell);
151 	cell++;
152 	if (n == 2) {
153 #ifdef ARM32
154 		if (addr) {
155 			/* High order 32 bits can't be nonzero */
156 			goto bad;
157 		}
158 		addr = fdt32_to_cpu(*cell);
159 #else
160 		addr = (addr << 32) | fdt32_to_cpu(*cell);
161 #endif
162 	}
163 
164 	if (!addr)
165 		goto bad;
166 
167 	return addr;
168 bad:
169 	return DT_INFO_INVALID_REG;
170 
171 }
172 
173 paddr_t _fdt_reg_base_address(const void *fdt, int offs)
174 {
175 	const void *reg;
176 	int ncells;
177 	int len;
178 	int parent;
179 
180 	parent = fdt_parent_offset(fdt, offs);
181 	if (parent < 0)
182 		return DT_INFO_INVALID_REG;
183 
184 	reg = fdt_getprop(fdt, offs, "reg", &len);
185 	if (!reg)
186 		return DT_INFO_INVALID_REG;
187 
188 	ncells = fdt_address_cells(fdt, parent);
189 	if (ncells < 0)
190 		return DT_INFO_INVALID_REG;
191 
192 	return _fdt_read_paddr(reg, ncells);
193 }
194 
195 size_t _fdt_reg_size(const void *fdt, int offs)
196 {
197 	const uint32_t *reg;
198 	uint32_t sz;
199 	int n;
200 	int len;
201 	int parent;
202 
203 	parent = fdt_parent_offset(fdt, offs);
204 	if (parent < 0)
205 		return DT_INFO_INVALID_REG_SIZE;
206 
207 	reg = (const uint32_t *)fdt_getprop(fdt, offs, "reg", &len);
208 	if (!reg)
209 		return DT_INFO_INVALID_REG_SIZE;
210 
211 	n = fdt_address_cells(fdt, parent);
212 	if (n < 1 || n > 2)
213 		return DT_INFO_INVALID_REG_SIZE;
214 
215 	reg += n;
216 
217 	n = fdt_size_cells(fdt, parent);
218 	if (n < 1 || n > 2)
219 		return DT_INFO_INVALID_REG_SIZE;
220 
221 	sz = fdt32_to_cpu(*reg);
222 	if (n == 2) {
223 		if (sz)
224 			return DT_INFO_INVALID_REG_SIZE;
225 		reg++;
226 		sz = fdt32_to_cpu(*reg);
227 	}
228 
229 	return sz;
230 }
231 
232 static bool is_okay(const char *st, int len)
233 {
234 	return !strncmp(st, "ok", len) || !strncmp(st, "okay", len);
235 }
236 
237 int _fdt_get_status(const void *fdt, int offs)
238 {
239 	const char *prop;
240 	int st = 0;
241 	int len;
242 
243 	prop = fdt_getprop(fdt, offs, "status", &len);
244 	if (!prop || is_okay(prop, len)) {
245 		/* If status is not specified, it defaults to "okay" */
246 		st |= DT_STATUS_OK_NSEC;
247 	}
248 
249 	prop = fdt_getprop(fdt, offs, "secure-status", &len);
250 	if (!prop) {
251 		/*
252 		 * When secure-status is not specified it defaults to the same
253 		 * value as status
254 		 */
255 		if (st & DT_STATUS_OK_NSEC)
256 			st |= DT_STATUS_OK_SEC;
257 	} else {
258 		if (is_okay(prop, len))
259 			st |= DT_STATUS_OK_SEC;
260 	}
261 
262 	return st;
263 }
264 
265 void _fdt_fill_device_info(const void *fdt, struct dt_node_info *info, int offs)
266 {
267 	struct dt_node_info dinfo = {
268 		.reg = DT_INFO_INVALID_REG,
269 		.reg_size = DT_INFO_INVALID_REG_SIZE,
270 		.clock = DT_INFO_INVALID_CLOCK,
271 		.reset = DT_INFO_INVALID_RESET,
272 		.interrupt = DT_INFO_INVALID_INTERRUPT,
273 	};
274 	const fdt32_t *cuint;
275 
276 	dinfo.reg = _fdt_reg_base_address(fdt, offs);
277 	dinfo.reg_size = _fdt_reg_size(fdt, offs);
278 
279 	cuint = fdt_getprop(fdt, offs, "clocks", NULL);
280 	if (cuint) {
281 		cuint++;
282 		dinfo.clock = (int)fdt32_to_cpu(*cuint);
283 	}
284 
285 	cuint = fdt_getprop(fdt, offs, "resets", NULL);
286 	if (cuint) {
287 		cuint++;
288 		dinfo.reset = (int)fdt32_to_cpu(*cuint);
289 	}
290 
291 	dinfo.interrupt = dt_get_irq_type_prio(fdt, offs, &dinfo.type,
292 					       &dinfo.prio);
293 
294 	dinfo.status = _fdt_get_status(fdt, offs);
295 
296 	*info = dinfo;
297 }
298 
299 int _fdt_read_uint32_array(const void *fdt, int node, const char *prop_name,
300 			   uint32_t *array, size_t count)
301 {
302 	const fdt32_t *cuint = NULL;
303 	int len = 0;
304 	uint32_t i = 0;
305 
306 	cuint = fdt_getprop(fdt, node, prop_name, &len);
307 	if (!cuint)
308 		return -FDT_ERR_NOTFOUND;
309 
310 	if ((uint32_t)len != (count * sizeof(uint32_t)))
311 		return -FDT_ERR_BADLAYOUT;
312 
313 	for (i = 0; i < ((uint32_t)len / sizeof(uint32_t)); i++) {
314 		*array = fdt32_to_cpu(*cuint);
315 		array++;
316 		cuint++;
317 	}
318 
319 	return 0;
320 }
321 
322 int _fdt_read_uint32(const void *fdt, int node, const char *prop_name,
323 		     uint32_t *value)
324 {
325 	return _fdt_read_uint32_array(fdt, node, prop_name, value, 1);
326 }
327 
328 uint32_t _fdt_read_uint32_default(const void *fdt, int node,
329 				  const char *prop_name, uint32_t dflt_value)
330 {
331 	uint32_t value = 0;
332 
333 	if (_fdt_read_uint32(fdt, node, prop_name, &value) < 0)
334 		return dflt_value;
335 
336 	return value;
337 }
338