mm, hwpoison: try to recover from copy-on write faults
[linux-2.6-block.git] / include / linux / property.h
CommitLineData
d2912cb1 1/* SPDX-License-Identifier: GPL-2.0-only */
b31384fa
RW
2/*
3 * property.h - Unified device property interface.
4 *
5 * Copyright (C) 2014, Intel Corporation
6 * Authors: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7 * Mika Westerberg <mika.westerberg@linux.intel.com>
b31384fa
RW
8 */
9
10#ifndef _LINUX_PROPERTY_H_
11#define _LINUX_PROPERTY_H_
12
0fcc2bdc 13#include <linux/bits.h>
ce793486 14#include <linux/fwnode.h>
b31384fa
RW
15#include <linux/types.h>
16
17struct device;
d9eb4490 18struct net_device;
b31384fa
RW
19
20enum dev_prop_type {
21 DEV_PROP_U8,
22 DEV_PROP_U16,
23 DEV_PROP_U32,
24 DEV_PROP_U64,
25 DEV_PROP_STRING,
e64b674b 26 DEV_PROP_REF,
b31384fa
RW
27};
28
1b9863c6
SS
29enum dev_dma_attr {
30 DEV_DMA_NOT_SUPPORTED,
31 DEV_DMA_NON_COHERENT,
32 DEV_DMA_COHERENT,
33};
34
aade55c8 35struct fwnode_handle *dev_fwnode(const struct device *dev);
e44bb0cb 36
b31384fa
RW
37bool device_property_present(struct device *dev, const char *propname);
38int device_property_read_u8_array(struct device *dev, const char *propname,
39 u8 *val, size_t nval);
40int device_property_read_u16_array(struct device *dev, const char *propname,
41 u16 *val, size_t nval);
42int device_property_read_u32_array(struct device *dev, const char *propname,
43 u32 *val, size_t nval);
44int device_property_read_u64_array(struct device *dev, const char *propname,
45 u64 *val, size_t nval);
46int device_property_read_string_array(struct device *dev, const char *propname,
47 const char **val, size_t nval);
48int device_property_read_string(struct device *dev, const char *propname,
49 const char **val);
3f5c8d31
MW
50int device_property_match_string(struct device *dev,
51 const char *propname, const char *string);
b31384fa 52
37ba983c
SA
53bool fwnode_device_is_available(const struct fwnode_handle *fwnode);
54bool fwnode_property_present(const struct fwnode_handle *fwnode,
55 const char *propname);
56int fwnode_property_read_u8_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
57 const char *propname, u8 *val,
58 size_t nval);
37ba983c 59int fwnode_property_read_u16_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
60 const char *propname, u16 *val,
61 size_t nval);
37ba983c 62int fwnode_property_read_u32_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
63 const char *propname, u32 *val,
64 size_t nval);
37ba983c 65int fwnode_property_read_u64_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
66 const char *propname, u64 *val,
67 size_t nval);
37ba983c 68int fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
8a0662d9
RW
69 const char *propname, const char **val,
70 size_t nval);
37ba983c 71int fwnode_property_read_string(const struct fwnode_handle *fwnode,
8a0662d9 72 const char *propname, const char **val);
37ba983c 73int fwnode_property_match_string(const struct fwnode_handle *fwnode,
3f5c8d31 74 const char *propname, const char *string);
3e3119d3
SA
75int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
76 const char *prop, const char *nargs_prop,
77 unsigned int nargs, unsigned int index,
78 struct fwnode_reference_args *args);
8a0662d9 79
83b34afb
HK
80struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
81 const char *name,
82 unsigned int index);
83
bc0500c1 84const char *fwnode_get_name(const struct fwnode_handle *fwnode);
e7e242bc 85const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode);
87ffea09 86
37ba983c 87struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode);
87ffea09
AS
88struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode);
89
90#define fwnode_for_each_parent_node(fwnode, parent) \
91 for (parent = fwnode_get_parent(fwnode); parent; \
92 parent = fwnode_get_next_parent(parent))
93
b5d3e2fb 94struct device *fwnode_get_next_parent_dev(struct fwnode_handle *fwnode);
87e5e95d
SA
95unsigned int fwnode_count_parents(const struct fwnode_handle *fwn);
96struct fwnode_handle *fwnode_get_nth_parent(struct fwnode_handle *fwn,
97 unsigned int depth);
022fe6bc 98bool fwnode_is_ancestor_of(struct fwnode_handle *ancestor, struct fwnode_handle *child);
37ba983c
SA
99struct fwnode_handle *fwnode_get_next_child_node(
100 const struct fwnode_handle *fwnode, struct fwnode_handle *child);
3395de96
MW
101struct fwnode_handle *fwnode_get_next_available_child_node(
102 const struct fwnode_handle *fwnode, struct fwnode_handle *child);
34055190
MW
103
104#define fwnode_for_each_child_node(fwnode, child) \
105 for (child = fwnode_get_next_child_node(fwnode, NULL); child; \
106 child = fwnode_get_next_child_node(fwnode, child))
afaf26fd 107
3395de96
MW
108#define fwnode_for_each_available_child_node(fwnode, child) \
109 for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
110 child = fwnode_get_next_available_child_node(fwnode, child))
111
37ba983c
SA
112struct fwnode_handle *device_get_next_child_node(
113 struct device *dev, struct fwnode_handle *child);
8a0662d9 114
1d656fb7
AS
115#define device_for_each_child_node(dev, child) \
116 for (child = device_get_next_child_node(dev, NULL); child; \
8a0662d9
RW
117 child = device_get_next_child_node(dev, child))
118
37ba983c
SA
119struct fwnode_handle *fwnode_get_named_child_node(
120 const struct fwnode_handle *fwnode, const char *childname);
613e9721
AT
121struct fwnode_handle *device_get_named_child_node(struct device *dev,
122 const char *childname);
123
cf89a31c 124struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode);
8a0662d9
RW
125void fwnode_handle_put(struct fwnode_handle *fwnode);
126
fb38f314 127int fwnode_irq_get(const struct fwnode_handle *fwnode, unsigned int index);
ca0acb51 128int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name);
7c6c57f2 129
8a0662d9
RW
130unsigned int device_get_child_node_count(struct device *dev);
131
b31384fa
RW
132static inline bool device_property_read_bool(struct device *dev,
133 const char *propname)
134{
135 return device_property_present(dev, propname);
136}
137
138static inline int device_property_read_u8(struct device *dev,
139 const char *propname, u8 *val)
140{
141 return device_property_read_u8_array(dev, propname, val, 1);
142}
143
144static inline int device_property_read_u16(struct device *dev,
145 const char *propname, u16 *val)
146{
147 return device_property_read_u16_array(dev, propname, val, 1);
148}
149
150static inline int device_property_read_u32(struct device *dev,
151 const char *propname, u32 *val)
152{
153 return device_property_read_u32_array(dev, propname, val, 1);
154}
155
156static inline int device_property_read_u64(struct device *dev,
157 const char *propname, u64 *val)
158{
159 return device_property_read_u64_array(dev, propname, val, 1);
160}
161
33ee09cd
AS
162static inline int device_property_count_u8(struct device *dev, const char *propname)
163{
164 return device_property_read_u8_array(dev, propname, NULL, 0);
165}
166
167static inline int device_property_count_u16(struct device *dev, const char *propname)
168{
169 return device_property_read_u16_array(dev, propname, NULL, 0);
170}
171
172static inline int device_property_count_u32(struct device *dev, const char *propname)
173{
174 return device_property_read_u32_array(dev, propname, NULL, 0);
175}
176
177static inline int device_property_count_u64(struct device *dev, const char *propname)
178{
179 return device_property_read_u64_array(dev, propname, NULL, 0);
180}
181
6b6ff4ac
BG
182static inline int device_property_string_array_count(struct device *dev,
183 const char *propname)
184{
185 return device_property_read_string_array(dev, propname, NULL, 0);
186}
187
37ba983c 188static inline bool fwnode_property_read_bool(const struct fwnode_handle *fwnode,
8a0662d9
RW
189 const char *propname)
190{
191 return fwnode_property_present(fwnode, propname);
192}
193
37ba983c 194static inline int fwnode_property_read_u8(const struct fwnode_handle *fwnode,
8a0662d9
RW
195 const char *propname, u8 *val)
196{
197 return fwnode_property_read_u8_array(fwnode, propname, val, 1);
198}
199
37ba983c 200static inline int fwnode_property_read_u16(const struct fwnode_handle *fwnode,
8a0662d9
RW
201 const char *propname, u16 *val)
202{
203 return fwnode_property_read_u16_array(fwnode, propname, val, 1);
204}
205
37ba983c 206static inline int fwnode_property_read_u32(const struct fwnode_handle *fwnode,
8a0662d9
RW
207 const char *propname, u32 *val)
208{
209 return fwnode_property_read_u32_array(fwnode, propname, val, 1);
210}
211
37ba983c 212static inline int fwnode_property_read_u64(const struct fwnode_handle *fwnode,
8a0662d9
RW
213 const char *propname, u64 *val)
214{
215 return fwnode_property_read_u64_array(fwnode, propname, val, 1);
216}
217
33ee09cd
AS
218static inline int fwnode_property_count_u8(const struct fwnode_handle *fwnode,
219 const char *propname)
220{
221 return fwnode_property_read_u8_array(fwnode, propname, NULL, 0);
222}
223
224static inline int fwnode_property_count_u16(const struct fwnode_handle *fwnode,
225 const char *propname)
226{
227 return fwnode_property_read_u16_array(fwnode, propname, NULL, 0);
228}
229
230static inline int fwnode_property_count_u32(const struct fwnode_handle *fwnode,
231 const char *propname)
232{
233 return fwnode_property_read_u32_array(fwnode, propname, NULL, 0);
234}
235
236static inline int fwnode_property_count_u64(const struct fwnode_handle *fwnode,
237 const char *propname)
238{
239 return fwnode_property_read_u64_array(fwnode, propname, NULL, 0);
240}
241
6b6ff4ac
BG
242static inline int
243fwnode_property_string_array_count(const struct fwnode_handle *fwnode,
244 const char *propname)
245{
246 return fwnode_property_read_string_array(fwnode, propname, NULL, 0);
247}
248
e64b674b
DT
249struct software_node;
250
251/**
252 * struct software_node_ref_args - Reference property with additional arguments
253 * @node: Reference to a software node
254 * @nargs: Number of elements in @args array
255 * @args: Integer arguments
256 */
257struct software_node_ref_args {
258 const struct software_node *node;
259 unsigned int nargs;
260 u64 args[NR_FWNODE_REFERENCE_ARGS];
261};
262
e588fead
AS
263#define SOFTWARE_NODE_REFERENCE(_ref_, ...) \
264(const struct software_node_ref_args) { \
265 .node = _ref_, \
266 .nargs = ARRAY_SIZE(((u64[]){ 0, ##__VA_ARGS__ })) - 1, \
267 .args = { __VA_ARGS__ }, \
268}
269
16ba08d5
RW
270/**
271 * struct property_entry - "Built-in" device property representation.
272 * @name: Name of the property.
318a1971 273 * @length: Length of data making up the value.
e6bff466 274 * @is_inline: True when the property value is stored inline.
63dcc709 275 * @type: Type of the data in unions.
e6bff466
DT
276 * @pointer: Pointer to the property when it is not stored inline.
277 * @value: Value of the property when it is stored inline.
16ba08d5
RW
278 */
279struct property_entry {
280 const char *name;
318a1971 281 size_t length;
e6bff466 282 bool is_inline;
63dcc709 283 enum dev_prop_type type;
16ba08d5 284 union {
1f74d70f 285 const void *pointer;
66586bab 286 union {
996b0830
DT
287 u8 u8_data[sizeof(u64) / sizeof(u8)];
288 u16 u16_data[sizeof(u64) / sizeof(u16)];
289 u32 u32_data[sizeof(u64) / sizeof(u32)];
290 u64 u64_data[sizeof(u64) / sizeof(u64)];
291 const char *str[sizeof(u64) / sizeof(char *)];
66586bab
AS
292 } value;
293 };
16ba08d5
RW
294};
295
d76eebfa 296/*
daeba9bf 297 * Note: the below initializers for the anonymous union are carefully
d76eebfa
AM
298 * crafted to avoid gcc-4.4.4's problems with initialization of anon unions
299 * and structs.
300 */
301
daeba9bf 302#define __PROPERTY_ENTRY_ELEMENT_SIZE(_elem_) \
996b0830 303 sizeof(((struct property_entry *)NULL)->value._elem_[0])
daeba9bf 304
e64b674b
DT
305#define __PROPERTY_ENTRY_ARRAY_ELSIZE_LEN(_name_, _elsize_, _Type_, \
306 _val_, _len_) \
63dcc709
AS
307(struct property_entry) { \
308 .name = _name_, \
e64b674b 309 .length = (_len_) * (_elsize_), \
63dcc709 310 .type = DEV_PROP_##_Type_, \
1f74d70f 311 { .pointer = _val_ }, \
a85f4204
HK
312}
313
e64b674b
DT
314#define __PROPERTY_ENTRY_ARRAY_LEN(_name_, _elem_, _Type_, _val_, _len_)\
315 __PROPERTY_ENTRY_ARRAY_ELSIZE_LEN(_name_, \
316 __PROPERTY_ENTRY_ELEMENT_SIZE(_elem_), \
317 _Type_, _val_, _len_)
318
1741cfac 319#define PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, _len_) \
daeba9bf 320 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u8_data, U8, _val_, _len_)
1741cfac 321#define PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, _len_) \
daeba9bf 322 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u16_data, U16, _val_, _len_)
1741cfac 323#define PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, _len_) \
daeba9bf 324 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u32_data, U32, _val_, _len_)
1741cfac 325#define PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, _len_) \
daeba9bf 326 __PROPERTY_ENTRY_ARRAY_LEN(_name_, u64_data, U64, _val_, _len_)
1741cfac 327#define PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, _len_) \
daeba9bf 328 __PROPERTY_ENTRY_ARRAY_LEN(_name_, str, STRING, _val_, _len_)
e64b674b
DT
329#define PROPERTY_ENTRY_REF_ARRAY_LEN(_name_, _val_, _len_) \
330 __PROPERTY_ENTRY_ARRAY_ELSIZE_LEN(_name_, \
331 sizeof(struct software_node_ref_args), \
332 REF, _val_, _len_)
a85f4204 333
1741cfac
DT
334#define PROPERTY_ENTRY_U8_ARRAY(_name_, _val_) \
335 PROPERTY_ENTRY_U8_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
336#define PROPERTY_ENTRY_U16_ARRAY(_name_, _val_) \
337 PROPERTY_ENTRY_U16_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
338#define PROPERTY_ENTRY_U32_ARRAY(_name_, _val_) \
339 PROPERTY_ENTRY_U32_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
340#define PROPERTY_ENTRY_U64_ARRAY(_name_, _val_) \
341 PROPERTY_ENTRY_U64_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
342#define PROPERTY_ENTRY_STRING_ARRAY(_name_, _val_) \
343 PROPERTY_ENTRY_STRING_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
e64b674b
DT
344#define PROPERTY_ENTRY_REF_ARRAY(_name_, _val_) \
345 PROPERTY_ENTRY_REF_ARRAY_LEN(_name_, _val_, ARRAY_SIZE(_val_))
1741cfac 346
daeba9bf
DT
347#define __PROPERTY_ENTRY_ELEMENT(_name_, _elem_, _Type_, _val_) \
348(struct property_entry) { \
349 .name = _name_, \
350 .length = __PROPERTY_ENTRY_ELEMENT_SIZE(_elem_), \
e6bff466 351 .is_inline = true, \
daeba9bf 352 .type = DEV_PROP_##_Type_, \
996b0830 353 { .value = { ._elem_[0] = _val_ } }, \
a85f4204
HK
354}
355
daeba9bf
DT
356#define PROPERTY_ENTRY_U8(_name_, _val_) \
357 __PROPERTY_ENTRY_ELEMENT(_name_, u8_data, U8, _val_)
358#define PROPERTY_ENTRY_U16(_name_, _val_) \
359 __PROPERTY_ENTRY_ELEMENT(_name_, u16_data, U16, _val_)
360#define PROPERTY_ENTRY_U32(_name_, _val_) \
361 __PROPERTY_ENTRY_ELEMENT(_name_, u32_data, U32, _val_)
362#define PROPERTY_ENTRY_U64(_name_, _val_) \
363 __PROPERTY_ENTRY_ELEMENT(_name_, u64_data, U64, _val_)
364#define PROPERTY_ENTRY_STRING(_name_, _val_) \
365 __PROPERTY_ENTRY_ELEMENT(_name_, str, STRING, _val_)
a85f4204
HK
366
367#define PROPERTY_ENTRY_BOOL(_name_) \
c505cbd4 368(struct property_entry) { \
a85f4204 369 .name = _name_, \
e6bff466 370 .is_inline = true, \
a85f4204
HK
371}
372
e64b674b
DT
373#define PROPERTY_ENTRY_REF(_name_, _ref_, ...) \
374(struct property_entry) { \
375 .name = _name_, \
376 .length = sizeof(struct software_node_ref_args), \
377 .type = DEV_PROP_REF, \
e588fead 378 { .pointer = &SOFTWARE_NODE_REFERENCE(_ref_, ##__VA_ARGS__), }, \
e64b674b
DT
379}
380
2d479e1f
DT
381struct property_entry *
382property_entries_dup(const struct property_entry *properties);
383
384void property_entries_free(const struct property_entry *properties);
385
e5e55864
SS
386bool device_dma_supported(struct device *dev);
387
388enum dev_dma_attr device_get_dma_attr(struct device *dev);
b283f157 389
aade55c8 390const void *device_get_match_data(const struct device *dev);
e5e55864 391
4c96b7dc 392int device_get_phy_mode(struct device *dev);
b28f263b 393int fwnode_get_phy_mode(struct fwnode_handle *fwnode);
b2638e56
AS
394
395void __iomem *fwnode_iomap(struct fwnode_handle *fwnode, int index);
396
07bb80d4 397struct fwnode_handle *fwnode_graph_get_next_endpoint(
37ba983c 398 const struct fwnode_handle *fwnode, struct fwnode_handle *prev);
6a71d8d7 399struct fwnode_handle *
37ba983c 400fwnode_graph_get_port_parent(const struct fwnode_handle *fwnode);
07bb80d4 401struct fwnode_handle *fwnode_graph_get_remote_port_parent(
37ba983c 402 const struct fwnode_handle *fwnode);
07bb80d4 403struct fwnode_handle *fwnode_graph_get_remote_port(
37ba983c 404 const struct fwnode_handle *fwnode);
07bb80d4 405struct fwnode_handle *fwnode_graph_get_remote_endpoint(
37ba983c 406 const struct fwnode_handle *fwnode);
07bb80d4 407
35694afc
LP
408static inline bool fwnode_graph_is_endpoint(struct fwnode_handle *fwnode)
409{
410 return fwnode_property_present(fwnode, "remote-endpoint");
411}
412
0fcc2bdc
SA
413/*
414 * Fwnode lookup flags
415 *
416 * @FWNODE_GRAPH_ENDPOINT_NEXT: In the case of no exact match, look for the
417 * closest endpoint ID greater than the specified
418 * one.
419 * @FWNODE_GRAPH_DEVICE_DISABLED: That the device to which the remote
420 * endpoint of the given endpoint belongs to,
49f39cb0
SA
421 * may be disabled, or that the endpoint is not
422 * connected.
0fcc2bdc
SA
423 */
424#define FWNODE_GRAPH_ENDPOINT_NEXT BIT(0)
425#define FWNODE_GRAPH_DEVICE_DISABLED BIT(1)
426
427struct fwnode_handle *
428fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
429 u32 port, u32 endpoint, unsigned long flags);
c87b8fc5
SA
430unsigned int fwnode_graph_get_endpoint_count(struct fwnode_handle *fwnode,
431 unsigned long flags);
0fcc2bdc 432
5e4b1b70
SA
433#define fwnode_graph_for_each_endpoint(fwnode, child) \
434 for (child = NULL; \
435 (child = fwnode_graph_get_next_endpoint(fwnode, child)); )
436
37ba983c 437int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
2bd5452d
SA
438 struct fwnode_endpoint *endpoint);
439
d7cf5590
HK
440typedef void *(*devcon_match_fn_t)(struct fwnode_handle *fwnode, const char *id,
441 void *data);
442
443void *fwnode_connection_find_match(struct fwnode_handle *fwnode,
444 const char *con_id, void *data,
445 devcon_match_fn_t match);
446
447static inline void *device_connection_find_match(struct device *dev,
448 const char *con_id, void *data,
449 devcon_match_fn_t match)
450{
451 return fwnode_connection_find_match(dev_fwnode(dev), con_id, data, match);
452}
453
7a20917d
BA
454int fwnode_connection_find_matches(struct fwnode_handle *fwnode,
455 const char *con_id, void *data,
456 devcon_match_fn_t match,
457 void **matches, unsigned int matches_len);
458
59abd836
HK
459/* -------------------------------------------------------------------------- */
460/* Software fwnode support - when HW description is incomplete or missing */
461
80488a6b
HK
462/**
463 * struct software_node - Software node description
464 * @name: Name of the software node
465 * @parent: Parent of the software node
466 * @properties: Array of device properties
467 */
468struct software_node {
469 const char *name;
470 const struct software_node *parent;
471 const struct property_entry *properties;
472};
473
59abd836 474bool is_software_node(const struct fwnode_handle *fwnode);
56c9aa07
SA
475const struct software_node *
476to_software_node(const struct fwnode_handle *fwnode);
80488a6b
HK
477struct fwnode_handle *software_node_fwnode(const struct software_node *node);
478
1666faed
HK
479const struct software_node *
480software_node_find_by_name(const struct software_node *parent,
481 const char *name);
482
80488a6b
HK
483int software_node_register_nodes(const struct software_node *nodes);
484void software_node_unregister_nodes(const struct software_node *nodes);
485
02094d54
AS
486int software_node_register_node_group(const struct software_node **node_group);
487void software_node_unregister_node_group(const struct software_node **node_group);
488
80488a6b 489int software_node_register(const struct software_node *node);
46d26819 490void software_node_unregister(const struct software_node *node);
59abd836 491
59abd836
HK
492struct fwnode_handle *
493fwnode_create_software_node(const struct property_entry *properties,
494 const struct fwnode_handle *parent);
495void fwnode_remove_software_node(struct fwnode_handle *fwnode);
496
2a92c90f 497int device_add_software_node(struct device *dev, const struct software_node *node);
e68d0119
HK
498void device_remove_software_node(struct device *dev);
499
151f6ff7
HK
500int device_create_managed_software_node(struct device *dev,
501 const struct property_entry *properties,
502 const struct software_node *parent);
503
b31384fa 504#endif /* _LINUX_PROPERTY_H_ */