1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * fwnode.h - Firmware device node object handle type definition.
4  *
5  * Copyright (C) 2015, Intel Corporation
6  * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7  */
8 
9 #ifndef _LINUX_FWNODE_H_
10 #define _LINUX_FWNODE_H_
11 
12 #include <linux/bits.h>
13 #include <linux/err.h>
14 #include <linux/list.h>
15 #include <linux/types.h>
16 #include <linux/android_kabi.h>
17 
18 enum dev_dma_attr {
19 	DEV_DMA_NOT_SUPPORTED,
20 	DEV_DMA_NON_COHERENT,
21 	DEV_DMA_COHERENT,
22 };
23 
24 struct fwnode_operations;
25 struct device;
26 
27 /*
28  * fwnode flags
29  *
30  * LINKS_ADDED:	The fwnode has already be parsed to add fwnode links.
31  * NOT_DEVICE:	The fwnode will never be populated as a struct device.
32  * INITIALIZED: The hardware corresponding to fwnode has been initialized.
33  * NEEDS_CHILD_BOUND_ON_ADD: For this fwnode/device to probe successfully, its
34  *			     driver needs its child devices to be bound with
35  *			     their respective drivers as soon as they are
36  *			     added.
37  * BEST_EFFORT: The fwnode/device needs to probe early and might be missing some
38  *		suppliers. Only enforce ordering with suppliers that have
39  *		drivers.
40  */
41 #define FWNODE_FLAG_LINKS_ADDED			BIT(0)
42 #define FWNODE_FLAG_NOT_DEVICE			BIT(1)
43 #define FWNODE_FLAG_INITIALIZED			BIT(2)
44 #define FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD	BIT(3)
45 #define FWNODE_FLAG_BEST_EFFORT			BIT(4)
46 #define FWNODE_FLAG_VISITED			BIT(5)
47 
48 struct fwnode_handle {
49 	struct fwnode_handle *secondary;
50 	const struct fwnode_operations *ops;
51 
52 	/* The below is used solely by device links, don't use otherwise */
53 	struct device *dev;
54 	struct list_head suppliers;
55 	struct list_head consumers;
56 	u8 flags;
57 	ANDROID_KABI_RESERVE(1);
58 };
59 
60 /*
61  * fwnode link flags
62  *
63  * CYCLE:	The fwnode link is part of a cycle. Don't defer probe.
64  * IGNORE:	Completely ignore this link, even during cycle detection.
65  */
66 #define FWLINK_FLAG_CYCLE			BIT(0)
67 #define FWLINK_FLAG_IGNORE			BIT(1)
68 
69 struct fwnode_link {
70 	struct fwnode_handle *supplier;
71 	struct list_head s_hook;
72 	struct fwnode_handle *consumer;
73 	struct list_head c_hook;
74 	u8 flags;
75 	ANDROID_KABI_RESERVE(1);
76 	ANDROID_KABI_RESERVE(2);
77 	ANDROID_KABI_RESERVE(3);
78 };
79 
80 /**
81  * struct fwnode_endpoint - Fwnode graph endpoint
82  * @port: Port number
83  * @id: Endpoint id
84  * @local_fwnode: reference to the related fwnode
85  */
86 struct fwnode_endpoint {
87 	unsigned int port;
88 	unsigned int id;
89 	const struct fwnode_handle *local_fwnode;
90 };
91 
92 /*
93  * ports and endpoints defined as software_nodes should all follow a common
94  * naming scheme; use these macros to ensure commonality.
95  */
96 #define SWNODE_GRAPH_PORT_NAME_FMT		"port@%u"
97 #define SWNODE_GRAPH_ENDPOINT_NAME_FMT		"endpoint@%u"
98 
99 #define NR_FWNODE_REFERENCE_ARGS	16
100 
101 /**
102  * struct fwnode_reference_args - Fwnode reference with additional arguments
103  * @fwnode:- A reference to the base fwnode
104  * @nargs: Number of elements in @args array
105  * @args: Integer arguments on the fwnode
106  */
107 struct fwnode_reference_args {
108 	struct fwnode_handle *fwnode;
109 	unsigned int nargs;
110 	u64 args[NR_FWNODE_REFERENCE_ARGS];
111 };
112 
113 /**
114  * struct fwnode_operations - Operations for fwnode interface
115  * @get: Get a reference to an fwnode.
116  * @put: Put a reference to an fwnode.
117  * @device_is_available: Return true if the device is available.
118  * @device_get_match_data: Return the device driver match data.
119  * @property_present: Return true if a property is present.
120  * @property_read_int_array: Read an array of integer properties. Return zero on
121  *			     success, a negative error code otherwise.
122  * @property_read_string_array: Read an array of string properties. Return zero
123  *				on success, a negative error code otherwise.
124  * @get_name: Return the name of an fwnode.
125  * @get_name_prefix: Get a prefix for a node (for printing purposes).
126  * @get_parent: Return the parent of an fwnode.
127  * @get_next_child_node: Return the next child node in an iteration.
128  * @get_named_child_node: Return a child node with a given name.
129  * @get_reference_args: Return a reference pointed to by a property, with args
130  * @graph_get_next_endpoint: Return an endpoint node in an iteration.
131  * @graph_get_remote_endpoint: Return the remote endpoint node of a local
132  *			       endpoint node.
133  * @graph_get_port_parent: Return the parent node of a port node.
134  * @graph_parse_endpoint: Parse endpoint for port and endpoint id.
135  * @add_links:	Create fwnode links to all the suppliers of the fwnode. Return
136  *		zero on success, a negative error code otherwise.
137  */
138 struct fwnode_operations {
139 	struct fwnode_handle *(*get)(struct fwnode_handle *fwnode);
140 	void (*put)(struct fwnode_handle *fwnode);
141 	bool (*device_is_available)(const struct fwnode_handle *fwnode);
142 	const void *(*device_get_match_data)(const struct fwnode_handle *fwnode,
143 					     const struct device *dev);
144 	bool (*device_dma_supported)(const struct fwnode_handle *fwnode);
145 	enum dev_dma_attr
146 	(*device_get_dma_attr)(const struct fwnode_handle *fwnode);
147 	bool (*property_present)(const struct fwnode_handle *fwnode,
148 				 const char *propname);
149 	int (*property_read_int_array)(const struct fwnode_handle *fwnode,
150 				       const char *propname,
151 				       unsigned int elem_size, void *val,
152 				       size_t nval);
153 	int
154 	(*property_read_string_array)(const struct fwnode_handle *fwnode_handle,
155 				      const char *propname, const char **val,
156 				      size_t nval);
157 	const char *(*get_name)(const struct fwnode_handle *fwnode);
158 	const char *(*get_name_prefix)(const struct fwnode_handle *fwnode);
159 	struct fwnode_handle *(*get_parent)(const struct fwnode_handle *fwnode);
160 	struct fwnode_handle *
161 	(*get_next_child_node)(const struct fwnode_handle *fwnode,
162 			       struct fwnode_handle *child);
163 	struct fwnode_handle *
164 	(*get_named_child_node)(const struct fwnode_handle *fwnode,
165 				const char *name);
166 	int (*get_reference_args)(const struct fwnode_handle *fwnode,
167 				  const char *prop, const char *nargs_prop,
168 				  unsigned int nargs, unsigned int index,
169 				  struct fwnode_reference_args *args);
170 	struct fwnode_handle *
171 	(*graph_get_next_endpoint)(const struct fwnode_handle *fwnode,
172 				   struct fwnode_handle *prev);
173 	struct fwnode_handle *
174 	(*graph_get_remote_endpoint)(const struct fwnode_handle *fwnode);
175 	struct fwnode_handle *
176 	(*graph_get_port_parent)(struct fwnode_handle *fwnode);
177 	int (*graph_parse_endpoint)(const struct fwnode_handle *fwnode,
178 				    struct fwnode_endpoint *endpoint);
179 	void __iomem *(*iomap)(struct fwnode_handle *fwnode, int index);
180 	int (*irq_get)(const struct fwnode_handle *fwnode, unsigned int index);
181 	int (*add_links)(struct fwnode_handle *fwnode);
182 };
183 
184 #define fwnode_has_op(fwnode, op)					\
185 	(!IS_ERR_OR_NULL(fwnode) && (fwnode)->ops && (fwnode)->ops->op)
186 
187 #define fwnode_call_int_op(fwnode, op, ...)				\
188 	(fwnode_has_op(fwnode, op) ?					\
189 	 (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : (IS_ERR_OR_NULL(fwnode) ? -EINVAL : -ENXIO))
190 
191 #define fwnode_call_bool_op(fwnode, op, ...)		\
192 	(fwnode_has_op(fwnode, op) ?			\
193 	 (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : false)
194 
195 #define fwnode_call_ptr_op(fwnode, op, ...)		\
196 	(fwnode_has_op(fwnode, op) ?			\
197 	 (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : NULL)
198 #define fwnode_call_void_op(fwnode, op, ...)				\
199 	do {								\
200 		if (fwnode_has_op(fwnode, op))				\
201 			(fwnode)->ops->op(fwnode, ## __VA_ARGS__);	\
202 	} while (false)
203 
fwnode_init(struct fwnode_handle * fwnode,const struct fwnode_operations * ops)204 static inline void fwnode_init(struct fwnode_handle *fwnode,
205 			       const struct fwnode_operations *ops)
206 {
207 	fwnode->ops = ops;
208 	INIT_LIST_HEAD(&fwnode->consumers);
209 	INIT_LIST_HEAD(&fwnode->suppliers);
210 }
211 
fwnode_dev_initialized(struct fwnode_handle * fwnode,bool initialized)212 static inline void fwnode_dev_initialized(struct fwnode_handle *fwnode,
213 					  bool initialized)
214 {
215 	if (IS_ERR_OR_NULL(fwnode))
216 		return;
217 
218 	if (initialized)
219 		fwnode->flags |= FWNODE_FLAG_INITIALIZED;
220 	else
221 		fwnode->flags &= ~FWNODE_FLAG_INITIALIZED;
222 }
223 
224 int fwnode_link_add(struct fwnode_handle *con, struct fwnode_handle *sup,
225 		    u8 flags);
226 void fwnode_links_purge(struct fwnode_handle *fwnode);
227 void fw_devlink_purge_absent_suppliers(struct fwnode_handle *fwnode);
228 bool fw_devlink_is_strict(void);
229 
230 #endif
231