1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (c) 2016-2021, Linaro Limited 4 */ 5 6 #ifndef __KERNEL_DT_H 7 #define __KERNEL_DT_H 8 9 #include <compiler.h> 10 #include <kernel/panic.h> 11 #include <scattered_array.h> 12 #include <stdint.h> 13 #include <tee_api_types.h> 14 #include <types_ext.h> 15 #include <util.h> 16 17 /* 18 * Bitfield to reflect status and secure-status values ("okay", "disabled" 19 * or not present) 20 */ 21 #define DT_STATUS_DISABLED U(0) 22 #define DT_STATUS_OK_NSEC BIT(0) 23 #define DT_STATUS_OK_SEC BIT(1) 24 25 #define DT_INFO_INVALID_REG ((paddr_t)-1) 26 #define DT_INFO_INVALID_REG_SIZE ((size_t)-1) 27 #define DT_INFO_INVALID_CLOCK -1 28 #define DT_INFO_INVALID_RESET -1 29 #define DT_INFO_INVALID_INTERRUPT -1 30 31 /* 32 * @status: Bit mask for DT_STATUS_* 33 * @reg: Device register physical base address or DT_INFO_INVALID_REG 34 * @reg_size: Device register size or DT_INFO_INVALID_REG_SIZE 35 * @clock: Device identifier (positive value) or DT_INFO_INVALID_CLOCK 36 * @reset: Device reset identifier (positive value) or DT_INFO_INVALID_CLOCK 37 * @interrupt: Device interrupt identifier (positive value) or 38 * DT_INFO_INVALID_INTERRUPT 39 * @type: IRQ_TYPE_* value parsed from interrupts properties or IRQ_TYPE_NONE if 40 * not present 41 * @prio: interrupt priority parsed from interrupts properties or 0 if not 42 * present 43 */ 44 struct dt_node_info { 45 unsigned int status; 46 paddr_t reg; 47 size_t reg_size; 48 int clock; 49 int reset; 50 int interrupt; 51 uint32_t type; 52 uint32_t prio; 53 }; 54 55 /* 56 * DT-aware drivers 57 */ 58 59 struct dt_device_match { 60 const char *compatible; 61 const void *compat_data; 62 }; 63 64 /* 65 * DT_MAP_AUTO: Uses status properties from device tree to determine mapping. 66 * DT_MAP_SECURE: Force mapping for device to be secure. 67 * DT_MAP_NON_SECURE: Force mapping for device to be non-secure. 68 */ 69 enum dt_map_dev_directive { 70 DT_MAP_AUTO, 71 DT_MAP_SECURE, 72 DT_MAP_NON_SECURE 73 }; 74 75 /* 76 * struct dt_descriptor - Descriptor of the device tree 77 * @blob: Pointer to the device tree binary 78 * @frag_id: Used ID of fragments for device tree overlay 79 */ 80 struct dt_descriptor { 81 void *blob; 82 #ifdef _CFG_USE_DTB_OVERLAY 83 int frag_id; 84 #endif 85 }; 86 87 extern uint8_t embedded_secure_dtb[]; 88 89 #ifdef CFG_DT 90 /* 91 * dt_getprop_as_number() - get a DT property a unsigned number 92 * @fdt: DT base address 93 * @nodeoffset: node offset 94 * @name: property string name 95 * @num: output number read 96 * Return 0 on success and a negative FDT error value on error 97 * 98 * The size of the property determines if it is read as an unsigned 32-bit 99 * or 64-bit integer. 100 */ 101 int dt_getprop_as_number(const void *fdt, int nodeoffset, const char *name, 102 uint64_t *num); 103 104 /* 105 * Find a driver that is suitable for the given DT node, that is, with 106 * a matching "compatible" property. 107 * 108 * @fdt: pointer to the device tree 109 * @offs: node offset 110 */ 111 const struct dt_driver *dt_find_compatible_driver(const void *fdt, int offs); 112 113 /* 114 * Map a device into secure or non-secure memory and return the base VA and 115 * the mapping size. The mapping is done with type MEM_AREA_IO_SEC or 116 * MEM_AREA_IO_NSEC, depending on the device status. 117 * If the mapping already exists, the function simply returns the @vbase and 118 * @size information. 119 * 120 * @offs is the offset of the node that describes the device in @fdt. 121 * @base receives the base virtual address corresponding to the base physical 122 * address of the "reg" property 123 * @size receives the size of the mapping 124 * @mapping what kind of mapping is done for memory. 125 * 126 * Returns 0 on success or -1 in case of error. 127 */ 128 int dt_map_dev(const void *fdt, int offs, vaddr_t *base, size_t *size, 129 enum dt_map_dev_directive mapping); 130 131 /* 132 * Check whether the node at @offs contains the property with propname or not. 133 * 134 * @offs is the offset of the node that describes the device in @fdt. 135 * @propname is the property that need to check 136 * 137 * Returns true on success or false if no propname. 138 */ 139 bool dt_have_prop(const void *fdt, int offs, const char *propname); 140 141 /* 142 * Modify or add "status" property to "disabled" 143 * 144 * @fdt reference to the Device Tree 145 * @node is the node offset to modify 146 * 147 * Returns 0 on success or -1 on failure 148 */ 149 int dt_disable_status(void *fdt, int node); 150 151 /* 152 * Force secure-status = "okay" and status="disabled" for the target node. 153 * 154 * @fdt reference to the Device Tree 155 * @node is the node offset to modify 156 * 157 * Returns 0 on success or -1 on failure 158 */ 159 int dt_enable_secure_status(void *fdt, int node); 160 161 /* 162 * FDT manipulation functions, not provided by <libfdt.h> 163 */ 164 165 /* 166 * Return the base address for the "reg" property of the specified node or 167 * (paddr_t)-1 in case of error 168 */ 169 paddr_t fdt_reg_base_address(const void *fdt, int offs); 170 171 /* 172 * Return the reg size for the reg property of the specified node or -1 in case 173 * of error 174 */ 175 size_t fdt_reg_size(const void *fdt, int offs); 176 177 /* 178 * Read the status and secure-status properties into a bitfield. 179 * Return -1 on failure, DT_STATUS_DISABLED if the node is disabled, 180 * otherwise return a combination of DT_STATUS_OK_NSEC and DT_STATUS_OK_SEC. 181 */ 182 int fdt_get_status(const void *fdt, int offs); 183 184 /* 185 * fdt_fill_device_info - Get generic device info from a node 186 * 187 * This function fills the generic information from a given node. 188 * Currently supports a single base register, a single clock, 189 * a single reset ID line and a single interrupt ID. 190 * Default DT_INFO_* macros are used when the relate property is not found. 191 */ 192 void fdt_fill_device_info(const void *fdt, struct dt_node_info *info, 193 int node); 194 /* 195 * Read cells from a given property of the given node. Any number of 32-bit 196 * cells of the property can be read. Returns 0 on success, or a negative 197 * FDT error value otherwise. 198 */ 199 int fdt_read_uint32_array(const void *fdt, int node, const char *prop_name, 200 uint32_t *array, size_t count); 201 202 /* 203 * Read one cell from a given multi-value property of the given node. 204 * Returns 0 on success, or a negative FDT error value otherwise. 205 */ 206 int fdt_read_uint32_index(const void *fdt, int node, const char *prop_name, 207 int index, uint32_t *value); 208 209 /* 210 * Read one cell from a given property of the given node. 211 * Returns 0 on success, or a negative FDT error value otherwise. 212 */ 213 int fdt_read_uint32(const void *fdt, int node, const char *prop_name, 214 uint32_t *value); 215 216 /* 217 * Read one cell from a property of a cell or default to a given value 218 * Returns the 32bit cell value or @dflt_value on failure. 219 */ 220 uint32_t fdt_read_uint32_default(const void *fdt, int node, 221 const char *prop_name, uint32_t dflt_value); 222 223 /* 224 * This function fills reg node info (base & size) with an index. 225 * 226 * Returns 0 on success and a negative FDT error code on failure. 227 */ 228 int fdt_get_reg_props_by_index(const void *fdt, int node, int index, 229 paddr_t *base, size_t *size); 230 231 /* 232 * This function fills reg node info (base & size) with an index found by 233 * checking the reg-names node. 234 * 235 * Returns 0 on success and a negative FDT error code on failure. 236 */ 237 int fdt_get_reg_props_by_name(const void *fdt, int node, const char *name, 238 paddr_t *base, size_t *size); 239 240 /* Returns embedded DTB if present, then external DTB if found, then NULL */ 241 void *get_dt(void); 242 243 /* 244 * get_secure_dt() - returns secure DTB for drivers 245 * 246 * Returns device tree that is considered secure for drivers to use. 247 * 248 * 1. Returns embedded DTB if available, 249 * 2. Secure external DTB if available, 250 * 3. If neither then NULL 251 */ 252 void *get_secure_dt(void); 253 254 /* Returns embedded DTB location if present, otherwise NULL */ 255 void *get_embedded_dt(void); 256 257 /* Returns true if passed DTB is same as Embedded DTB, otherwise false */ 258 static inline bool is_embedded_dt(void *fdt) 259 { 260 return fdt && fdt == get_embedded_dt(); 261 } 262 263 /* Returns DTB descriptor of the external DTB if present, otherwise NULL */ 264 struct dt_descriptor *get_external_dt_desc(void); 265 266 /* 267 * init_external_dt() - Initialize the external DTB located at given address. 268 * @phys_dt: Physical address where the external DTB located. 269 * @dt_sz: Maximum size of the external DTB. 270 * 271 * Initialize the external DTB. 272 * 273 * 1. Add MMU mapping of the external DTB, 274 * 2. Initialize device tree overlay 275 */ 276 void init_external_dt(unsigned long phys_dt, size_t dt_sz); 277 278 /* Returns external DTB if present, otherwise NULL */ 279 void *get_external_dt(void); 280 281 /* 282 * add_dt_path_subnode() - Add new child node into a parent node. 283 * @dt: Pointer to a device tree descriptor which has DTB. 284 * @path: Path to the parent node. 285 * @subnode: Name of the child node. 286 * 287 * Returns the offset of the child node in DTB on success or a negative libfdt 288 * error number. 289 */ 290 int add_dt_path_subnode(struct dt_descriptor *dt, const char *path, 291 const char *subnode); 292 293 /* 294 * add_res_mem_dt_node() - Create "reserved-memory" parent and child nodes. 295 * @dt: Pointer to a device tree descriptor which has DTB. 296 * @name: Name of the child node. 297 * @pa: Physical address of specific reserved memory region. 298 * @size: Size of specific reserved memory region. 299 * 300 * Returns 0 if succeeds, otherwise a negative libfdt error number. 301 */ 302 int add_res_mem_dt_node(struct dt_descriptor *dt, const char *name, 303 paddr_t pa, size_t size); 304 305 #else /* !CFG_DT */ 306 307 static inline const struct dt_driver *dt_find_compatible_driver( 308 const void *fdt __unused, 309 int offs __unused) 310 { 311 return NULL; 312 } 313 314 static inline int dt_map_dev(const void *fdt __unused, int offs __unused, 315 vaddr_t *vbase __unused, size_t *size __unused, 316 enum dt_map_dev_directive mapping __unused) 317 { 318 return -1; 319 } 320 321 static inline paddr_t fdt_reg_base_address(const void *fdt __unused, 322 int offs __unused) 323 { 324 return (paddr_t)-1; 325 } 326 327 static inline size_t fdt_reg_size(const void *fdt __unused, 328 int offs __unused) 329 { 330 return (size_t)-1; 331 } 332 333 static inline int fdt_get_status(const void *fdt __unused, int offs __unused) 334 { 335 return -1; 336 } 337 338 __noreturn 339 static inline void fdt_fill_device_info(const void *fdt __unused, 340 struct dt_node_info *info __unused, 341 int node __unused) 342 { 343 panic(); 344 } 345 346 static inline int fdt_read_uint32_array(const void *fdt __unused, 347 int node __unused, 348 const char *prop_name __unused, 349 uint32_t *array __unused, 350 size_t count __unused) 351 { 352 return -1; 353 } 354 355 static inline int fdt_read_uint32(const void *fdt __unused, 356 int node __unused, 357 const char *prop_name __unused, 358 uint32_t *value __unused) 359 { 360 return -1; 361 } 362 363 static inline uint32_t fdt_read_uint32_default(const void *fdt __unused, 364 int node __unused, 365 const char *prop_name __unused, 366 uint32_t dflt_value __unused) 367 { 368 return dflt_value; 369 } 370 371 static inline int fdt_read_uint32_index(const void *fdt __unused, 372 int node __unused, 373 const char *prop_name __unused, 374 int index __unused, 375 uint32_t *value __unused) 376 { 377 return -1; 378 } 379 380 static inline int fdt_get_reg_props_by_index(const void *fdt __unused, 381 int node __unused, 382 int index __unused, 383 paddr_t *base __unused, 384 size_t *size __unused) 385 { 386 return -1; 387 } 388 389 static inline int fdt_get_reg_props_by_name(const void *fdt __unused, 390 int node __unused, 391 const char *name __unused, 392 paddr_t *base __unused, 393 size_t *size __unused) 394 { 395 return -1; 396 } 397 398 static inline int dt_getprop_as_number(const void *fdt __unused, 399 int nodeoffset __unused, 400 const char *name __unused, 401 uint64_t *num __unused) 402 { 403 return -1; 404 } 405 406 static inline void *get_dt(void) 407 { 408 return NULL; 409 } 410 411 static inline void *get_secure_dt(void) 412 { 413 return NULL; 414 } 415 416 static inline void *get_embedded_dt(void) 417 { 418 return NULL; 419 } 420 421 static inline bool is_embedded_dt(void *fdt __unused) 422 { 423 return false; 424 } 425 426 static inline struct dt_descriptor *get_external_dt_desc(void) 427 { 428 return NULL; 429 } 430 431 static inline void init_external_dt(unsigned long phys_dt __unused, 432 size_t dt_sz __unused) 433 { 434 } 435 436 static inline void *get_external_dt(void) 437 { 438 return NULL; 439 } 440 441 static inline int add_dt_path_subnode(struct dt_descriptor *dt __unused, 442 const char *path __unused, 443 const char *subnode __unused) 444 { 445 return -1; 446 } 447 448 static inline int add_res_mem_dt_node(struct dt_descriptor *dt __unused, 449 const char *name __unused, 450 paddr_t pa __unused, 451 size_t size __unused) 452 { 453 return -1; 454 } 455 456 #endif /* !CFG_DT */ 457 #endif /* __KERNEL_DT_H */ 458