xref: /optee_os/core/include/kernel/dt.h (revision ba6d8df98e3cf376aab45d0d958204c498a94123)
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 ("ok", "disabled" or not
38  * 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 extern const struct dt_driver __rodata_dtdrv_start, __rodata_dtdrv_end;
63 
64 /*
65  * Find a driver that is suitable for the given DT node, that is, with
66  * a matching "compatible" property.
67  *
68  * @fdt: pointer to the device tree
69  * @offs: node offset
70  */
71 const struct dt_driver *dt_find_compatible_driver(const void *fdt, int offs);
72 
73 const struct dt_driver *__dt_driver_start(void);
74 
75 const struct dt_driver *__dt_driver_end(void);
76 
77 /*
78  * Map a device into secure or non-secure memory and return the base VA and
79  * the mapping size. The mapping is done with type MEM_AREA_IO_SEC or
80  * MEM_AREA_IO_NSEC, depending on the device status.
81  * If the mapping already exists, the function simply returns the @vbase and
82  * @size information.
83  *
84  * @offs is the offset of the node that describes the device in @fdt.
85  * @base receives the base virtual address corresponding to the base physical
86  * address of the "reg" property
87  * @size receives the size of the mapping
88  *
89  * Returns 0 on success or -1 in case of error.
90  */
91 int dt_map_dev(const void *fdt, int offs, vaddr_t *base, size_t *size);
92 
93 /*
94  * FDT manipulation functions, not provided by <libfdt.h>
95  */
96 
97 /*
98  * Return the base address for the "reg" property of the specified node or
99  * (paddr_t)-1 in case of error
100  */
101 paddr_t _fdt_reg_base_address(const void *fdt, int offs);
102 
103 /*
104  * Return the reg size for the reg property of the specified node or -1 in case
105  * of error
106  */
107 ssize_t _fdt_reg_size(const void *fdt, int offs);
108 
109 /*
110  * Read the status and secure-status properties into a bitfield.
111  * @status is set to DT_STATUS_DISABLED or a combination of DT_STATUS_OK_NSEC
112  * and DT_STATUS_OK_SEC
113  * Returns 0 on success or -1 in case of error.
114  */
115 int _fdt_get_status(const void *fdt, int offs);
116 
117 #else /* !CFG_DT */
118 
119 static inline const struct dt_driver *__dt_driver_start(void)
120 {
121 	return NULL;
122 }
123 
124 static inline const struct dt_driver *__dt_driver_end(void)
125 {
126 	return NULL;
127 }
128 
129 static inline const struct dt_driver *dt_find_compatible_driver(
130 					const void *fdt __unused,
131 					int offs __unused)
132 {
133 	return NULL;
134 }
135 
136 static inline int dt_map_dev(const void *fdt __unused, int offs __unused,
137 			     vaddr_t *vbase __unused, size_t *size __unused)
138 {
139 	return -1;
140 }
141 
142 static inline paddr_t _fdt_reg_base_address(const void *fdt __unused,
143 					    int offs __unused)
144 {
145 	return (paddr_t)-1;
146 }
147 
148 static inline ssize_t _fdt_reg_size(const void *fdt __unused,
149 				    int offs __unused)
150 {
151 	return -1;
152 }
153 
154 static inline int _fdt_get_status(const void *fdt __unused, int offs __unused)
155 {
156 	return -1;
157 }
158 
159 #endif /* !CFG_DT */
160 
161 #define for_each_dt_driver(drv) \
162 	for (drv = __dt_driver_start(); drv < __dt_driver_end(); drv++)
163 
164 #endif /* KERNEL_DT_H */
165