blob: 21c2e0f7006c2d4d25455c6e887c24f5ee8b861f [file] [log] [blame]
Damjan Marionf508e072024-08-27 18:21:02 +02001/* SPDX-License-Identifier: Apache-2.0
2 * Copyright (c) 2024 Cisco Systems, Inc.
3 */
4
5#ifndef CLIB_DEVICETREE_H_
6#define CLIB_DEVICETREE_H_
7
8#include <vppinfra/clib.h>
9#include <vlib/vlib.h>
10
11#ifdef __linux
12#define CLIB_DT_LINUX_PREFIX "/sys/firmware/devicetree/base"
13#endif
14
15typedef struct
16{
17 char name[32];
18 u32 size;
19 u8 data[];
20} clib_dt_property_t;
21
22typedef struct clib_dt_main clib_dt_main_t;
23
24typedef struct clib_dt_node
25{
26 u8 *path;
27 struct clib_dt_node *parent;
28 struct clib_dt_node *prev;
29 struct clib_dt_node *next;
30 struct clib_dt_node **child_nodes;
31 u8 depth;
32 clib_dt_property_t *name;
33 clib_dt_property_t **properties;
34 clib_dt_main_t *dt_main;
35} clib_dt_node_t;
36
37typedef struct clib_dt_main
38{
39 clib_dt_node_t **nodes;
40 clib_dt_node_t *root;
41 uword *node_by_path;
42 uword *node_by_phandle;
43} clib_dt_main_t;
44
45clib_dt_node_t *clib_dt_get_node_with_path (clib_dt_main_t *dm, char *fmt,
46 ...);
47clib_dt_property_t *clib_dt_get_node_property_by_name (clib_dt_node_t *,
48 char *);
49int clib_dt_node_is_compatible (clib_dt_node_t *, char *);
50clib_dt_node_t *clib_dt_dereference_node (clib_dt_node_t *, char *);
51#ifdef __linux
52clib_error_t *clib_dt_read_from_sysfs (clib_dt_main_t *dm);
53#endif
54
55format_function_t format_clib_dt_desc;
56format_function_t format_clib_dt_property_data;
57
58static_always_inline int
59clib_dt_proprerty_is_u32 (clib_dt_property_t *p)
60{
61 if (p == 0 || p->size != 4)
62 return 0;
63 return 1;
64}
65
66static_always_inline u32
67clib_dt_proprerty_get_u32 (clib_dt_property_t *p)
68{
69 return clib_net_to_host_u32 (*(u32u *) p->data);
70}
71
72#endif /* CLIB_DEVICETREE_H_ */