097be89487bf5c5a96f01d569c1691088db4f0ca
[linux-block.git] / include / linux / fwnode.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * fwnode.h - Firmware device node object handle type definition.
4  *
5  * This header file provides low-level data types and definitions for firmware
6  * and device property providers. The respective API header files supplied by
7  * them should contain all of the requisite data types and definitions for end
8  * users, so including it directly should not be necessary.
9  *
10  * Copyright (C) 2015, Intel Corporation
11  * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
12  */
13
14 #ifndef _LINUX_FWNODE_H_
15 #define _LINUX_FWNODE_H_
16
17 #include <linux/bits.h>
18 #include <linux/err.h>
19 #include <linux/list.h>
20 #include <linux/types.h>
21
22 enum dev_dma_attr {
23         DEV_DMA_NOT_SUPPORTED,
24         DEV_DMA_NON_COHERENT,
25         DEV_DMA_COHERENT,
26 };
27
28 struct fwnode_operations;
29 struct device;
30
31 /*
32  * fwnode flags
33  *
34  * LINKS_ADDED: The fwnode has already be parsed to add fwnode links.
35  * NOT_DEVICE:  The fwnode will never be populated as a struct device.
36  * INITIALIZED: The hardware corresponding to fwnode has been initialized.
37  * NEEDS_CHILD_BOUND_ON_ADD: For this fwnode/device to probe successfully, its
38  *                           driver needs its child devices to be bound with
39  *                           their respective drivers as soon as they are
40  *                           added.
41  * BEST_EFFORT: The fwnode/device needs to probe early and might be missing some
42  *              suppliers. Only enforce ordering with suppliers that have
43  *              drivers.
44  */
45 #define FWNODE_FLAG_LINKS_ADDED                 BIT(0)
46 #define FWNODE_FLAG_NOT_DEVICE                  BIT(1)
47 #define FWNODE_FLAG_INITIALIZED                 BIT(2)
48 #define FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD    BIT(3)
49 #define FWNODE_FLAG_BEST_EFFORT                 BIT(4)
50 #define FWNODE_FLAG_VISITED                     BIT(5)
51
52 struct fwnode_handle {
53         struct fwnode_handle *secondary;
54         const struct fwnode_operations *ops;
55
56         /* The below is used solely by device links, don't use otherwise */
57         struct device *dev;
58         struct list_head suppliers;
59         struct list_head consumers;
60         u8 flags;
61 };
62
63 /*
64  * fwnode link flags
65  *
66  * CYCLE:       The fwnode link is part of a cycle. Don't defer probe.
67  * IGNORE:      Completely ignore this link, even during cycle detection.
68  */
69 #define FWLINK_FLAG_CYCLE                       BIT(0)
70 #define FWLINK_FLAG_IGNORE                      BIT(1)
71
72 struct fwnode_link {
73         struct fwnode_handle *supplier;
74         struct list_head s_hook;
75         struct fwnode_handle *consumer;
76         struct list_head c_hook;
77         u8 flags;
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_bool: Return a boolean property value.
121  * @property_read_int_array: Read an array of integer properties. Return zero on
122  *                           success, a negative error code otherwise.
123  * @property_read_string_array: Read an array of string properties. Return zero
124  *                              on success, a negative error code otherwise.
125  * @get_name: Return the name of an fwnode.
126  * @get_name_prefix: Get a prefix for a node (for printing purposes).
127  * @get_parent: Return the parent of an fwnode.
128  * @get_next_child_node: Return the next child node in an iteration.
129  * @get_named_child_node: Return a child node with a given name.
130  * @get_reference_args: Return a reference pointed to by a property, with args
131  * @graph_get_next_endpoint: Return an endpoint node in an iteration.
132  * @graph_get_remote_endpoint: Return the remote endpoint node of a local
133  *                             endpoint node.
134  * @graph_get_port_parent: Return the parent node of a port node.
135  * @graph_parse_endpoint: Parse endpoint for port and endpoint id.
136  * @add_links:  Create fwnode links to all the suppliers of the fwnode. Return
137  *              zero on success, a negative error code otherwise.
138  */
139 struct fwnode_operations {
140         struct fwnode_handle *(*get)(struct fwnode_handle *fwnode);
141         void (*put)(struct fwnode_handle *fwnode);
142         bool (*device_is_available)(const struct fwnode_handle *fwnode);
143         const void *(*device_get_match_data)(const struct fwnode_handle *fwnode,
144                                              const struct device *dev);
145         bool (*device_dma_supported)(const struct fwnode_handle *fwnode);
146         enum dev_dma_attr
147         (*device_get_dma_attr)(const struct fwnode_handle *fwnode);
148         bool (*property_present)(const struct fwnode_handle *fwnode,
149                                  const char *propname);
150         bool (*property_read_bool)(const struct fwnode_handle *fwnode,
151                                    const char *propname);
152         int (*property_read_int_array)(const struct fwnode_handle *fwnode,
153                                        const char *propname,
154                                        unsigned int elem_size, void *val,
155                                        size_t nval);
156         int
157         (*property_read_string_array)(const struct fwnode_handle *fwnode_handle,
158                                       const char *propname, const char **val,
159                                       size_t nval);
160         const char *(*get_name)(const struct fwnode_handle *fwnode);
161         const char *(*get_name_prefix)(const struct fwnode_handle *fwnode);
162         struct fwnode_handle *(*get_parent)(const struct fwnode_handle *fwnode);
163         struct fwnode_handle *
164         (*get_next_child_node)(const struct fwnode_handle *fwnode,
165                                struct fwnode_handle *child);
166         struct fwnode_handle *
167         (*get_named_child_node)(const struct fwnode_handle *fwnode,
168                                 const char *name);
169         int (*get_reference_args)(const struct fwnode_handle *fwnode,
170                                   const char *prop, const char *nargs_prop,
171                                   unsigned int nargs, unsigned int index,
172                                   struct fwnode_reference_args *args);
173         struct fwnode_handle *
174         (*graph_get_next_endpoint)(const struct fwnode_handle *fwnode,
175                                    struct fwnode_handle *prev);
176         struct fwnode_handle *
177         (*graph_get_remote_endpoint)(const struct fwnode_handle *fwnode);
178         struct fwnode_handle *
179         (*graph_get_port_parent)(struct fwnode_handle *fwnode);
180         int (*graph_parse_endpoint)(const struct fwnode_handle *fwnode,
181                                     struct fwnode_endpoint *endpoint);
182         void __iomem *(*iomap)(struct fwnode_handle *fwnode, int index);
183         int (*irq_get)(const struct fwnode_handle *fwnode, unsigned int index);
184         int (*add_links)(struct fwnode_handle *fwnode);
185 };
186
187 #define fwnode_has_op(fwnode, op)                                       \
188         (!IS_ERR_OR_NULL(fwnode) && (fwnode)->ops && (fwnode)->ops->op)
189
190 #define fwnode_call_int_op(fwnode, op, ...)                             \
191         (fwnode_has_op(fwnode, op) ?                                    \
192          (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : (IS_ERR_OR_NULL(fwnode) ? -EINVAL : -ENXIO))
193
194 #define fwnode_call_bool_op(fwnode, op, ...)            \
195         (fwnode_has_op(fwnode, op) ?                    \
196          (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : false)
197
198 #define fwnode_call_ptr_op(fwnode, op, ...)             \
199         (fwnode_has_op(fwnode, op) ?                    \
200          (fwnode)->ops->op(fwnode, ## __VA_ARGS__) : NULL)
201 #define fwnode_call_void_op(fwnode, op, ...)                            \
202         do {                                                            \
203                 if (fwnode_has_op(fwnode, op))                          \
204                         (fwnode)->ops->op(fwnode, ## __VA_ARGS__);      \
205         } while (false)
206
207 static inline void fwnode_init(struct fwnode_handle *fwnode,
208                                const struct fwnode_operations *ops)
209 {
210         fwnode->ops = ops;
211         INIT_LIST_HEAD(&fwnode->consumers);
212         INIT_LIST_HEAD(&fwnode->suppliers);
213 }
214
215 static inline void fwnode_dev_initialized(struct fwnode_handle *fwnode,
216                                           bool initialized)
217 {
218         if (IS_ERR_OR_NULL(fwnode))
219                 return;
220
221         if (initialized)
222                 fwnode->flags |= FWNODE_FLAG_INITIALIZED;
223         else
224                 fwnode->flags &= ~FWNODE_FLAG_INITIALIZED;
225 }
226
227 int fwnode_link_add(struct fwnode_handle *con, struct fwnode_handle *sup,
228                     u8 flags);
229 void fwnode_links_purge(struct fwnode_handle *fwnode);
230 void fw_devlink_purge_absent_suppliers(struct fwnode_handle *fwnode);
231 bool fw_devlink_is_strict(void);
232
233 #endif