xref: /optee_os/core/include/kernel/dt.h (revision 75200110483dcee11cdcf4cef3d0ac4d92f63c14)
1 /*
2  * Copyright (c) 2016, Linaro Limited
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef KERNEL_DT_H
29 #define KERNEL_DT_H
30 
31 #include <compiler.h>
32 #include <stdint.h>
33 #include <types_ext.h>
34 #include <util.h>
35 
36 /*
37  * Bitfield to reflect status and secure-status values ("okay", "disabled"
38  * or not present)
39  */
40 #define DT_STATUS_DISABLED 0
41 #define DT_STATUS_OK_NSEC  BIT(0)
42 #define DT_STATUS_OK_SEC   BIT(1)
43 
44 #if defined(CFG_DT)
45 
46 /*
47  * DT-aware drivers
48  */
49 
50 struct dt_device_match {
51 	const char *compatible;
52 };
53 
54 struct dt_driver {
55 	const char *name;
56 	const struct dt_device_match *match_table; /* null-terminated */
57 	const void *driver;
58 };
59 
60 #define __dt_driver __section(".rodata.dtdrv")
61 
62 /*
63  * Find a driver that is suitable for the given DT node, that is, with
64  * a matching "compatible" property.
65  *
66  * @fdt: pointer to the device tree
67  * @offs: node offset
68  */
69 const struct dt_driver *dt_find_compatible_driver(const void *fdt, int offs);
70 
71 const struct dt_driver *__dt_driver_start(void);
72 
73 const struct dt_driver *__dt_driver_end(void);
74 
75 /*
76  * Map a device into secure or non-secure memory and return the base VA and
77  * the mapping size. The mapping is done with type MEM_AREA_IO_SEC or
78  * MEM_AREA_IO_NSEC, depending on the device status.
79  * If the mapping already exists, the function simply returns the @vbase and
80  * @size information.
81  *
82  * @offs is the offset of the node that describes the device in @fdt.
83  * @base receives the base virtual address corresponding to the base physical
84  * address of the "reg" property
85  * @size receives the size of the mapping
86  *
87  * Returns 0 on success or -1 in case of error.
88  */
89 int dt_map_dev(const void *fdt, int offs, vaddr_t *base, size_t *size);
90 
91 /*
92  * Check whether the node at @offs contains the property with propname or not.
93  *
94  * @offs is the offset of the node that describes the device in @fdt.
95  * @propname is the property that need to check
96  *
97  * Returns true on success or false if no propname.
98  */
99 bool dt_have_prop(const void *fdt, int offs, const char *propname);
100 
101 /*
102  * FDT manipulation functions, not provided by <libfdt.h>
103  */
104 
105 /*
106  * Return the base address for the "reg" property of the specified node or
107  * (paddr_t)-1 in case of error
108  */
109 paddr_t _fdt_reg_base_address(const void *fdt, int offs);
110 
111 /*
112  * Return the reg size for the reg property of the specified node or -1 in case
113  * of error
114  */
115 ssize_t _fdt_reg_size(const void *fdt, int offs);
116 
117 /*
118  * Read the status and secure-status properties into a bitfield.
119  * @status is set to DT_STATUS_DISABLED or a combination of DT_STATUS_OK_NSEC
120  * and DT_STATUS_OK_SEC
121  * Returns 0 on success or -1 in case of error.
122  */
123 int _fdt_get_status(const void *fdt, int offs);
124 
125 #else /* !CFG_DT */
126 
127 static inline const struct dt_driver *__dt_driver_start(void)
128 {
129 	return NULL;
130 }
131 
132 static inline const struct dt_driver *__dt_driver_end(void)
133 {
134 	return NULL;
135 }
136 
137 static inline const struct dt_driver *dt_find_compatible_driver(
138 					const void *fdt __unused,
139 					int offs __unused)
140 {
141 	return NULL;
142 }
143 
144 static inline int dt_map_dev(const void *fdt __unused, int offs __unused,
145 			     vaddr_t *vbase __unused, size_t *size __unused)
146 {
147 	return -1;
148 }
149 
150 static inline paddr_t _fdt_reg_base_address(const void *fdt __unused,
151 					    int offs __unused)
152 {
153 	return (paddr_t)-1;
154 }
155 
156 static inline ssize_t _fdt_reg_size(const void *fdt __unused,
157 				    int offs __unused)
158 {
159 	return -1;
160 }
161 
162 static inline int _fdt_get_status(const void *fdt __unused, int offs __unused)
163 {
164 	return -1;
165 }
166 
167 #endif /* !CFG_DT */
168 
169 #define for_each_dt_driver(drv) \
170 	for (drv = __dt_driver_start(); drv < __dt_driver_end(); drv++)
171 
172 #endif /* KERNEL_DT_H */
173