xref: /linux-6.15/include/linux/device.h (revision 3ef9f710)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * device.h - generic, centralized driver model
4  *
5  * Copyright (c) 2001-2003 Patrick Mochel <[email protected]>
6  * Copyright (c) 2004-2009 Greg Kroah-Hartman <[email protected]>
7  * Copyright (c) 2008-2009 Novell Inc.
8  *
9  * See Documentation/driver-api/driver-model/ for more information.
10  */
11 
12 #ifndef _DEVICE_H_
13 #define _DEVICE_H_
14 
15 #include <linux/dev_printk.h>
16 #include <linux/energy_model.h>
17 #include <linux/ioport.h>
18 #include <linux/kobject.h>
19 #include <linux/klist.h>
20 #include <linux/list.h>
21 #include <linux/lockdep.h>
22 #include <linux/compiler.h>
23 #include <linux/types.h>
24 #include <linux/mutex.h>
25 #include <linux/pm.h>
26 #include <linux/atomic.h>
27 #include <linux/uidgid.h>
28 #include <linux/gfp.h>
29 #include <linux/device/bus.h>
30 #include <linux/device/class.h>
31 #include <linux/device/devres.h>
32 #include <linux/device/driver.h>
33 #include <linux/cleanup.h>
34 #include <asm/device.h>
35 
36 struct device;
37 struct device_private;
38 struct device_driver;
39 struct driver_private;
40 struct module;
41 struct class;
42 struct subsys_private;
43 struct device_node;
44 struct fwnode_handle;
45 struct iommu_group;
46 struct dev_pin_info;
47 struct dev_iommu;
48 struct msi_device_data;
49 
50 /**
51  * struct subsys_interface - interfaces to device functions
52  * @name:       name of the device function
53  * @subsys:     subsystem of the devices to attach to
54  * @node:       the list of functions registered at the subsystem
55  * @add_dev:    device hookup to device function handler
56  * @remove_dev: device hookup to device function handler
57  *
58  * Simple interfaces attached to a subsystem. Multiple interfaces can
59  * attach to a subsystem and its devices. Unlike drivers, they do not
60  * exclusively claim or control devices. Interfaces usually represent
61  * a specific functionality of a subsystem/class of devices.
62  */
63 struct subsys_interface {
64 	const char *name;
65 	const struct bus_type *subsys;
66 	struct list_head node;
67 	int (*add_dev)(struct device *dev, struct subsys_interface *sif);
68 	void (*remove_dev)(struct device *dev, struct subsys_interface *sif);
69 };
70 
71 int subsys_interface_register(struct subsys_interface *sif);
72 void subsys_interface_unregister(struct subsys_interface *sif);
73 
74 int subsys_system_register(const struct bus_type *subsys,
75 			   const struct attribute_group **groups);
76 int subsys_virtual_register(const struct bus_type *subsys,
77 			    const struct attribute_group **groups);
78 
79 /*
80  * The type of device, "struct device" is embedded in. A class
81  * or bus can contain devices of different types
82  * like "partitions" and "disks", "mouse" and "event".
83  * This identifies the device type and carries type-specific
84  * information, equivalent to the kobj_type of a kobject.
85  * If "name" is specified, the uevent will contain it in
86  * the DEVTYPE variable.
87  */
88 struct device_type {
89 	const char *name;
90 	const struct attribute_group **groups;
91 	int (*uevent)(const struct device *dev, struct kobj_uevent_env *env);
92 	char *(*devnode)(const struct device *dev, umode_t *mode,
93 			 kuid_t *uid, kgid_t *gid);
94 	void (*release)(struct device *dev);
95 
96 	const struct dev_pm_ops *pm;
97 };
98 
99 /**
100  * struct device_attribute - Interface for exporting device attributes.
101  * @attr: sysfs attribute definition.
102  * @show: Show handler.
103  * @store: Store handler.
104  */
105 struct device_attribute {
106 	struct attribute	attr;
107 	ssize_t (*show)(struct device *dev, struct device_attribute *attr,
108 			char *buf);
109 	ssize_t (*store)(struct device *dev, struct device_attribute *attr,
110 			 const char *buf, size_t count);
111 };
112 
113 /**
114  * struct dev_ext_attribute - Exported device attribute with extra context.
115  * @attr: Exported device attribute.
116  * @var: Pointer to context.
117  */
118 struct dev_ext_attribute {
119 	struct device_attribute attr;
120 	void *var;
121 };
122 
123 ssize_t device_show_ulong(struct device *dev, struct device_attribute *attr,
124 			  char *buf);
125 ssize_t device_store_ulong(struct device *dev, struct device_attribute *attr,
126 			   const char *buf, size_t count);
127 ssize_t device_show_int(struct device *dev, struct device_attribute *attr,
128 			char *buf);
129 ssize_t device_store_int(struct device *dev, struct device_attribute *attr,
130 			 const char *buf, size_t count);
131 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
132 			char *buf);
133 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
134 			 const char *buf, size_t count);
135 ssize_t device_show_string(struct device *dev, struct device_attribute *attr,
136 			   char *buf);
137 
138 /**
139  * DEVICE_ATTR - Define a device attribute.
140  * @_name: Attribute name.
141  * @_mode: File mode.
142  * @_show: Show handler. Optional, but mandatory if attribute is readable.
143  * @_store: Store handler. Optional, but mandatory if attribute is writable.
144  *
145  * Convenience macro for defining a struct device_attribute.
146  *
147  * For example, ``DEVICE_ATTR(foo, 0644, foo_show, foo_store);`` expands to:
148  *
149  * .. code-block:: c
150  *
151  *	struct device_attribute dev_attr_foo = {
152  *		.attr	= { .name = "foo", .mode = 0644 },
153  *		.show	= foo_show,
154  *		.store	= foo_store,
155  *	};
156  */
157 #define DEVICE_ATTR(_name, _mode, _show, _store) \
158 	struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
159 
160 /**
161  * DEVICE_ATTR_PREALLOC - Define a preallocated device attribute.
162  * @_name: Attribute name.
163  * @_mode: File mode.
164  * @_show: Show handler. Optional, but mandatory if attribute is readable.
165  * @_store: Store handler. Optional, but mandatory if attribute is writable.
166  *
167  * Like DEVICE_ATTR(), but ``SYSFS_PREALLOC`` is set on @_mode.
168  */
169 #define DEVICE_ATTR_PREALLOC(_name, _mode, _show, _store) \
170 	struct device_attribute dev_attr_##_name = \
171 		__ATTR_PREALLOC(_name, _mode, _show, _store)
172 
173 /**
174  * DEVICE_ATTR_RW - Define a read-write device attribute.
175  * @_name: Attribute name.
176  *
177  * Like DEVICE_ATTR(), but @_mode is 0644, @_show is <_name>_show,
178  * and @_store is <_name>_store.
179  */
180 #define DEVICE_ATTR_RW(_name) \
181 	struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
182 
183 /**
184  * DEVICE_ATTR_ADMIN_RW - Define an admin-only read-write device attribute.
185  * @_name: Attribute name.
186  *
187  * Like DEVICE_ATTR_RW(), but @_mode is 0600.
188  */
189 #define DEVICE_ATTR_ADMIN_RW(_name) \
190 	struct device_attribute dev_attr_##_name = __ATTR_RW_MODE(_name, 0600)
191 
192 /**
193  * DEVICE_ATTR_RO - Define a readable device attribute.
194  * @_name: Attribute name.
195  *
196  * Like DEVICE_ATTR(), but @_mode is 0444 and @_show is <_name>_show.
197  */
198 #define DEVICE_ATTR_RO(_name) \
199 	struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
200 
201 /**
202  * DEVICE_ATTR_ADMIN_RO - Define an admin-only readable device attribute.
203  * @_name: Attribute name.
204  *
205  * Like DEVICE_ATTR_RO(), but @_mode is 0400.
206  */
207 #define DEVICE_ATTR_ADMIN_RO(_name) \
208 	struct device_attribute dev_attr_##_name = __ATTR_RO_MODE(_name, 0400)
209 
210 /**
211  * DEVICE_ATTR_WO - Define an admin-only writable device attribute.
212  * @_name: Attribute name.
213  *
214  * Like DEVICE_ATTR(), but @_mode is 0200 and @_store is <_name>_store.
215  */
216 #define DEVICE_ATTR_WO(_name) \
217 	struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
218 
219 /**
220  * DEVICE_ULONG_ATTR - Define a device attribute backed by an unsigned long.
221  * @_name: Attribute name.
222  * @_mode: File mode.
223  * @_var: Identifier of unsigned long.
224  *
225  * Like DEVICE_ATTR(), but @_show and @_store are automatically provided
226  * such that reads and writes to the attribute from userspace affect @_var.
227  */
228 #define DEVICE_ULONG_ATTR(_name, _mode, _var) \
229 	struct dev_ext_attribute dev_attr_##_name = \
230 		{ __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) }
231 
232 /**
233  * DEVICE_INT_ATTR - Define a device attribute backed by an int.
234  * @_name: Attribute name.
235  * @_mode: File mode.
236  * @_var: Identifier of int.
237  *
238  * Like DEVICE_ULONG_ATTR(), but @_var is an int.
239  */
240 #define DEVICE_INT_ATTR(_name, _mode, _var) \
241 	struct dev_ext_attribute dev_attr_##_name = \
242 		{ __ATTR(_name, _mode, device_show_int, device_store_int), &(_var) }
243 
244 /**
245  * DEVICE_BOOL_ATTR - Define a device attribute backed by a bool.
246  * @_name: Attribute name.
247  * @_mode: File mode.
248  * @_var: Identifier of bool.
249  *
250  * Like DEVICE_ULONG_ATTR(), but @_var is a bool.
251  */
252 #define DEVICE_BOOL_ATTR(_name, _mode, _var) \
253 	struct dev_ext_attribute dev_attr_##_name = \
254 		{ __ATTR(_name, _mode, device_show_bool, device_store_bool), &(_var) }
255 
256 /**
257  * DEVICE_STRING_ATTR_RO - Define a device attribute backed by a r/o string.
258  * @_name: Attribute name.
259  * @_mode: File mode.
260  * @_var: Identifier of string.
261  *
262  * Like DEVICE_ULONG_ATTR(), but @_var is a string. Because the length of the
263  * string allocation is unknown, the attribute must be read-only.
264  */
265 #define DEVICE_STRING_ATTR_RO(_name, _mode, _var) \
266 	struct dev_ext_attribute dev_attr_##_name = \
267 		{ __ATTR(_name, (_mode) & ~0222, device_show_string, NULL), (_var) }
268 
269 #define DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
270 	struct device_attribute dev_attr_##_name =		\
271 		__ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
272 
273 int device_create_file(struct device *device,
274 		       const struct device_attribute *entry);
275 void device_remove_file(struct device *dev,
276 			const struct device_attribute *attr);
277 bool device_remove_file_self(struct device *dev,
278 			     const struct device_attribute *attr);
279 int __must_check device_create_bin_file(struct device *dev,
280 					const struct bin_attribute *attr);
281 void device_remove_bin_file(struct device *dev,
282 			    const struct bin_attribute *attr);
283 
284 /* allows to add/remove a custom action to devres stack */
285 int devm_remove_action_nowarn(struct device *dev, void (*action)(void *), void *data);
286 
287 /**
288  * devm_remove_action() - removes previously added custom action
289  * @dev: Device that owns the action
290  * @action: Function implementing the action
291  * @data: Pointer to data passed to @action implementation
292  *
293  * Removes instance of @action previously added by devm_add_action().
294  * Both action and data should match one of the existing entries.
295  */
296 static inline
297 void devm_remove_action(struct device *dev, void (*action)(void *), void *data)
298 {
299 	WARN_ON(devm_remove_action_nowarn(dev, action, data));
300 }
301 
302 void devm_release_action(struct device *dev, void (*action)(void *), void *data);
303 
304 int __devm_add_action(struct device *dev, void (*action)(void *), void *data, const char *name);
305 #define devm_add_action(dev, action, data) \
306 	__devm_add_action(dev, action, data, #action)
307 
308 static inline int __devm_add_action_or_reset(struct device *dev, void (*action)(void *),
309 					     void *data, const char *name)
310 {
311 	int ret;
312 
313 	ret = __devm_add_action(dev, action, data, name);
314 	if (ret)
315 		action(data);
316 
317 	return ret;
318 }
319 #define devm_add_action_or_reset(dev, action, data) \
320 	__devm_add_action_or_reset(dev, action, data, #action)
321 
322 /**
323  * devm_alloc_percpu - Resource-managed alloc_percpu
324  * @dev: Device to allocate per-cpu memory for
325  * @type: Type to allocate per-cpu memory for
326  *
327  * Managed alloc_percpu. Per-cpu memory allocated with this function is
328  * automatically freed on driver detach.
329  *
330  * RETURNS:
331  * Pointer to allocated memory on success, NULL on failure.
332  */
333 #define devm_alloc_percpu(dev, type)      \
334 	((typeof(type) __percpu *)__devm_alloc_percpu((dev), sizeof(type), \
335 						      __alignof__(type)))
336 
337 void __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
338 				   size_t align);
339 void devm_free_percpu(struct device *dev, void __percpu *pdata);
340 
341 struct device_dma_parameters {
342 	/*
343 	 * a low level driver may set these to teach IOMMU code about
344 	 * sg limitations.
345 	 */
346 	unsigned int max_segment_size;
347 	unsigned int min_align_mask;
348 	unsigned long segment_boundary_mask;
349 };
350 
351 /**
352  * enum device_link_state - Device link states.
353  * @DL_STATE_NONE: The presence of the drivers is not being tracked.
354  * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present.
355  * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not.
356  * @DL_STATE_CONSUMER_PROBE: The consumer is probing (supplier driver present).
357  * @DL_STATE_ACTIVE: Both the supplier and consumer drivers are present.
358  * @DL_STATE_SUPPLIER_UNBIND: The supplier driver is unbinding.
359  */
360 enum device_link_state {
361 	DL_STATE_NONE = -1,
362 	DL_STATE_DORMANT = 0,
363 	DL_STATE_AVAILABLE,
364 	DL_STATE_CONSUMER_PROBE,
365 	DL_STATE_ACTIVE,
366 	DL_STATE_SUPPLIER_UNBIND,
367 };
368 
369 /*
370  * Device link flags.
371  *
372  * STATELESS: The core will not remove this link automatically.
373  * AUTOREMOVE_CONSUMER: Remove the link automatically on consumer driver unbind.
374  * PM_RUNTIME: If set, the runtime PM framework will use this link.
375  * RPM_ACTIVE: Run pm_runtime_get_sync() on the supplier during link creation.
376  * AUTOREMOVE_SUPPLIER: Remove the link automatically on supplier driver unbind.
377  * AUTOPROBE_CONSUMER: Probe consumer driver automatically after supplier binds.
378  * MANAGED: The core tracks presence of supplier/consumer drivers (internal).
379  * SYNC_STATE_ONLY: Link only affects sync_state() behavior.
380  * INFERRED: Inferred from data (eg: firmware) and not from driver actions.
381  */
382 #define DL_FLAG_STATELESS		BIT(0)
383 #define DL_FLAG_AUTOREMOVE_CONSUMER	BIT(1)
384 #define DL_FLAG_PM_RUNTIME		BIT(2)
385 #define DL_FLAG_RPM_ACTIVE		BIT(3)
386 #define DL_FLAG_AUTOREMOVE_SUPPLIER	BIT(4)
387 #define DL_FLAG_AUTOPROBE_CONSUMER	BIT(5)
388 #define DL_FLAG_MANAGED			BIT(6)
389 #define DL_FLAG_SYNC_STATE_ONLY		BIT(7)
390 #define DL_FLAG_INFERRED		BIT(8)
391 #define DL_FLAG_CYCLE			BIT(9)
392 
393 /**
394  * enum dl_dev_state - Device driver presence tracking information.
395  * @DL_DEV_NO_DRIVER: There is no driver attached to the device.
396  * @DL_DEV_PROBING: A driver is probing.
397  * @DL_DEV_DRIVER_BOUND: The driver has been bound to the device.
398  * @DL_DEV_UNBINDING: The driver is unbinding from the device.
399  */
400 enum dl_dev_state {
401 	DL_DEV_NO_DRIVER = 0,
402 	DL_DEV_PROBING,
403 	DL_DEV_DRIVER_BOUND,
404 	DL_DEV_UNBINDING,
405 };
406 
407 /**
408  * enum device_removable - Whether the device is removable. The criteria for a
409  * device to be classified as removable is determined by its subsystem or bus.
410  * @DEVICE_REMOVABLE_NOT_SUPPORTED: This attribute is not supported for this
411  *				    device (default).
412  * @DEVICE_REMOVABLE_UNKNOWN:  Device location is Unknown.
413  * @DEVICE_FIXED: Device is not removable by the user.
414  * @DEVICE_REMOVABLE: Device is removable by the user.
415  */
416 enum device_removable {
417 	DEVICE_REMOVABLE_NOT_SUPPORTED = 0, /* must be 0 */
418 	DEVICE_REMOVABLE_UNKNOWN,
419 	DEVICE_FIXED,
420 	DEVICE_REMOVABLE,
421 };
422 
423 /**
424  * struct dev_links_info - Device data related to device links.
425  * @suppliers: List of links to supplier devices.
426  * @consumers: List of links to consumer devices.
427  * @defer_sync: Hook to global list of devices that have deferred sync_state.
428  * @status: Driver status information.
429  */
430 struct dev_links_info {
431 	struct list_head suppliers;
432 	struct list_head consumers;
433 	struct list_head defer_sync;
434 	enum dl_dev_state status;
435 };
436 
437 /**
438  * struct dev_msi_info - Device data related to MSI
439  * @domain:	The MSI interrupt domain associated to the device
440  * @data:	Pointer to MSI device data
441  */
442 struct dev_msi_info {
443 #ifdef CONFIG_GENERIC_MSI_IRQ
444 	struct irq_domain	*domain;
445 	struct msi_device_data	*data;
446 #endif
447 };
448 
449 /**
450  * enum device_physical_location_panel - Describes which panel surface of the
451  * system's housing the device connection point resides on.
452  * @DEVICE_PANEL_TOP: Device connection point is on the top panel.
453  * @DEVICE_PANEL_BOTTOM: Device connection point is on the bottom panel.
454  * @DEVICE_PANEL_LEFT: Device connection point is on the left panel.
455  * @DEVICE_PANEL_RIGHT: Device connection point is on the right panel.
456  * @DEVICE_PANEL_FRONT: Device connection point is on the front panel.
457  * @DEVICE_PANEL_BACK: Device connection point is on the back panel.
458  * @DEVICE_PANEL_UNKNOWN: The panel with device connection point is unknown.
459  */
460 enum device_physical_location_panel {
461 	DEVICE_PANEL_TOP,
462 	DEVICE_PANEL_BOTTOM,
463 	DEVICE_PANEL_LEFT,
464 	DEVICE_PANEL_RIGHT,
465 	DEVICE_PANEL_FRONT,
466 	DEVICE_PANEL_BACK,
467 	DEVICE_PANEL_UNKNOWN,
468 };
469 
470 /**
471  * enum device_physical_location_vertical_position - Describes vertical
472  * position of the device connection point on the panel surface.
473  * @DEVICE_VERT_POS_UPPER: Device connection point is at upper part of panel.
474  * @DEVICE_VERT_POS_CENTER: Device connection point is at center part of panel.
475  * @DEVICE_VERT_POS_LOWER: Device connection point is at lower part of panel.
476  */
477 enum device_physical_location_vertical_position {
478 	DEVICE_VERT_POS_UPPER,
479 	DEVICE_VERT_POS_CENTER,
480 	DEVICE_VERT_POS_LOWER,
481 };
482 
483 /**
484  * enum device_physical_location_horizontal_position - Describes horizontal
485  * position of the device connection point on the panel surface.
486  * @DEVICE_HORI_POS_LEFT: Device connection point is at left part of panel.
487  * @DEVICE_HORI_POS_CENTER: Device connection point is at center part of panel.
488  * @DEVICE_HORI_POS_RIGHT: Device connection point is at right part of panel.
489  */
490 enum device_physical_location_horizontal_position {
491 	DEVICE_HORI_POS_LEFT,
492 	DEVICE_HORI_POS_CENTER,
493 	DEVICE_HORI_POS_RIGHT,
494 };
495 
496 /**
497  * struct device_physical_location - Device data related to physical location
498  * of the device connection point.
499  * @panel: Panel surface of the system's housing that the device connection
500  *         point resides on.
501  * @vertical_position: Vertical position of the device connection point within
502  *                     the panel.
503  * @horizontal_position: Horizontal position of the device connection point
504  *                       within the panel.
505  * @dock: Set if the device connection point resides in a docking station or
506  *        port replicator.
507  * @lid: Set if this device connection point resides on the lid of laptop
508  *       system.
509  */
510 struct device_physical_location {
511 	enum device_physical_location_panel panel;
512 	enum device_physical_location_vertical_position vertical_position;
513 	enum device_physical_location_horizontal_position horizontal_position;
514 	bool dock;
515 	bool lid;
516 };
517 
518 /**
519  * struct device - The basic device structure
520  * @parent:	The device's "parent" device, the device to which it is attached.
521  * 		In most cases, a parent device is some sort of bus or host
522  * 		controller. If parent is NULL, the device, is a top-level device,
523  * 		which is not usually what you want.
524  * @p:		Holds the private data of the driver core portions of the device.
525  * 		See the comment of the struct device_private for detail.
526  * @kobj:	A top-level, abstract class from which other classes are derived.
527  * @init_name:	Initial name of the device.
528  * @type:	The type of device.
529  * 		This identifies the device type and carries type-specific
530  * 		information.
531  * @mutex:	Mutex to synchronize calls to its driver.
532  * @bus:	Type of bus device is on.
533  * @driver:	Which driver has allocated this
534  * @platform_data: Platform data specific to the device.
535  * 		Example: For devices on custom boards, as typical of embedded
536  * 		and SOC based hardware, Linux often uses platform_data to point
537  * 		to board-specific structures describing devices and how they
538  * 		are wired.  That can include what ports are available, chip
539  * 		variants, which GPIO pins act in what additional roles, and so
540  * 		on.  This shrinks the "Board Support Packages" (BSPs) and
541  * 		minimizes board-specific #ifdefs in drivers.
542  * @driver_data: Private pointer for driver specific info.
543  * @links:	Links to suppliers and consumers of this device.
544  * @power:	For device power management.
545  *		See Documentation/driver-api/pm/devices.rst for details.
546  * @pm_domain:	Provide callbacks that are executed during system suspend,
547  * 		hibernation, system resume and during runtime PM transitions
548  * 		along with subsystem-level and driver-level callbacks.
549  * @em_pd:	device's energy model performance domain
550  * @pins:	For device pin management.
551  *		See Documentation/driver-api/pin-control.rst for details.
552  * @msi:	MSI related data
553  * @numa_node:	NUMA node this device is close to.
554  * @dma_ops:    DMA mapping operations for this device.
555  * @dma_mask:	Dma mask (if dma'ble device).
556  * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
557  * 		hardware supports 64-bit addresses for consistent allocations
558  * 		such descriptors.
559  * @bus_dma_limit: Limit of an upstream bridge or bus which imposes a smaller
560  *		DMA limit than the device itself supports.
561  * @dma_range_map: map for DMA memory ranges relative to that of RAM
562  * @dma_parms:	A low level driver may set these to teach IOMMU code about
563  * 		segment limitations.
564  * @dma_pools:	Dma pools (if dma'ble device).
565  * @dma_mem:	Internal for coherent mem override.
566  * @cma_area:	Contiguous memory area for dma allocations
567  * @dma_io_tlb_mem: Software IO TLB allocator.  Not for driver use.
568  * @dma_io_tlb_pools:	List of transient swiotlb memory pools.
569  * @dma_io_tlb_lock:	Protects changes to the list of active pools.
570  * @dma_uses_io_tlb: %true if device has used the software IO TLB.
571  * @archdata:	For arch-specific additions.
572  * @of_node:	Associated device tree node.
573  * @fwnode:	Associated device node supplied by platform firmware.
574  * @devt:	For creating the sysfs "dev".
575  * @id:		device instance
576  * @devres_lock: Spinlock to protect the resource of the device.
577  * @devres_head: The resources list of the device.
578  * @class:	The class of the device.
579  * @groups:	Optional attribute groups.
580  * @release:	Callback to free the device after all references have
581  * 		gone away. This should be set by the allocator of the
582  * 		device (i.e. the bus driver that discovered the device).
583  * @iommu_group: IOMMU group the device belongs to.
584  * @iommu:	Per device generic IOMMU runtime data
585  * @physical_location: Describes physical location of the device connection
586  *		point in the system housing.
587  * @removable:  Whether the device can be removed from the system. This
588  *              should be set by the subsystem / bus driver that discovered
589  *              the device.
590  *
591  * @offline_disabled: If set, the device is permanently online.
592  * @offline:	Set after successful invocation of bus type's .offline().
593  * @of_node_reused: Set if the device-tree node is shared with an ancestor
594  *              device.
595  * @state_synced: The hardware state of this device has been synced to match
596  *		  the software state of this device by calling the driver/bus
597  *		  sync_state() callback.
598  * @can_match:	The device has matched with a driver at least once or it is in
599  *		a bus (like AMBA) which can't check for matching drivers until
600  *		other devices probe successfully.
601  * @dma_coherent: this particular device is dma coherent, even if the
602  *		architecture supports non-coherent devices.
603  * @dma_ops_bypass: If set to %true then the dma_ops are bypassed for the
604  *		streaming DMA operations (->map_* / ->unmap_* / ->sync_*),
605  *		and optionall (if the coherent mask is large enough) also
606  *		for dma allocations.  This flag is managed by the dma ops
607  *		instance from ->dma_supported.
608  * @dma_skip_sync: DMA sync operations can be skipped for coherent buffers.
609  * @dma_iommu: Device is using default IOMMU implementation for DMA and
610  *		doesn't rely on dma_ops structure.
611  *
612  * At the lowest level, every device in a Linux system is represented by an
613  * instance of struct device. The device structure contains the information
614  * that the device model core needs to model the system. Most subsystems,
615  * however, track additional information about the devices they host. As a
616  * result, it is rare for devices to be represented by bare device structures;
617  * instead, that structure, like kobject structures, is usually embedded within
618  * a higher-level representation of the device.
619  */
620 struct device {
621 	struct kobject kobj;
622 	struct device		*parent;
623 
624 	struct device_private	*p;
625 
626 	const char		*init_name; /* initial name of the device */
627 	const struct device_type *type;
628 
629 	const struct bus_type	*bus;	/* type of bus device is on */
630 	struct device_driver *driver;	/* which driver has allocated this
631 					   device */
632 	void		*platform_data;	/* Platform specific data, device
633 					   core doesn't touch it */
634 	void		*driver_data;	/* Driver data, set and get with
635 					   dev_set_drvdata/dev_get_drvdata */
636 	struct mutex		mutex;	/* mutex to synchronize calls to
637 					 * its driver.
638 					 */
639 
640 	struct dev_links_info	links;
641 	struct dev_pm_info	power;
642 	struct dev_pm_domain	*pm_domain;
643 
644 #ifdef CONFIG_ENERGY_MODEL
645 	struct em_perf_domain	*em_pd;
646 #endif
647 
648 #ifdef CONFIG_PINCTRL
649 	struct dev_pin_info	*pins;
650 #endif
651 	struct dev_msi_info	msi;
652 #ifdef CONFIG_ARCH_HAS_DMA_OPS
653 	const struct dma_map_ops *dma_ops;
654 #endif
655 	u64		*dma_mask;	/* dma mask (if dma'able device) */
656 	u64		coherent_dma_mask;/* Like dma_mask, but for
657 					     alloc_coherent mappings as
658 					     not all hardware supports
659 					     64 bit addresses for consistent
660 					     allocations such descriptors. */
661 	u64		bus_dma_limit;	/* upstream dma constraint */
662 	const struct bus_dma_region *dma_range_map;
663 
664 	struct device_dma_parameters *dma_parms;
665 
666 	struct list_head	dma_pools;	/* dma pools (if dma'ble) */
667 
668 #ifdef CONFIG_DMA_DECLARE_COHERENT
669 	struct dma_coherent_mem	*dma_mem; /* internal for coherent mem
670 					     override */
671 #endif
672 #ifdef CONFIG_DMA_CMA
673 	struct cma *cma_area;		/* contiguous memory area for dma
674 					   allocations */
675 #endif
676 #ifdef CONFIG_SWIOTLB
677 	struct io_tlb_mem *dma_io_tlb_mem;
678 #endif
679 #ifdef CONFIG_SWIOTLB_DYNAMIC
680 	struct list_head dma_io_tlb_pools;
681 	spinlock_t dma_io_tlb_lock;
682 	bool dma_uses_io_tlb;
683 #endif
684 	/* arch specific additions */
685 	struct dev_archdata	archdata;
686 
687 	struct device_node	*of_node; /* associated device tree node */
688 	struct fwnode_handle	*fwnode; /* firmware device node */
689 
690 #ifdef CONFIG_NUMA
691 	int		numa_node;	/* NUMA node this device is close to */
692 #endif
693 	dev_t			devt;	/* dev_t, creates the sysfs "dev" */
694 	u32			id;	/* device instance */
695 
696 	spinlock_t		devres_lock;
697 	struct list_head	devres_head;
698 
699 	const struct class	*class;
700 	const struct attribute_group **groups;	/* optional groups */
701 
702 	void	(*release)(struct device *dev);
703 	struct iommu_group	*iommu_group;
704 	struct dev_iommu	*iommu;
705 
706 	struct device_physical_location *physical_location;
707 
708 	enum device_removable	removable;
709 
710 	bool			offline_disabled:1;
711 	bool			offline:1;
712 	bool			of_node_reused:1;
713 	bool			state_synced:1;
714 	bool			can_match:1;
715 #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
716     defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
717     defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
718 	bool			dma_coherent:1;
719 #endif
720 #ifdef CONFIG_DMA_OPS_BYPASS
721 	bool			dma_ops_bypass : 1;
722 #endif
723 #ifdef CONFIG_DMA_NEED_SYNC
724 	bool			dma_skip_sync:1;
725 #endif
726 #ifdef CONFIG_IOMMU_DMA
727 	bool			dma_iommu:1;
728 #endif
729 };
730 
731 /**
732  * struct device_link - Device link representation.
733  * @supplier: The device on the supplier end of the link.
734  * @s_node: Hook to the supplier device's list of links to consumers.
735  * @consumer: The device on the consumer end of the link.
736  * @c_node: Hook to the consumer device's list of links to suppliers.
737  * @link_dev: device used to expose link details in sysfs
738  * @status: The state of the link (with respect to the presence of drivers).
739  * @flags: Link flags.
740  * @rpm_active: Whether or not the consumer device is runtime-PM-active.
741  * @kref: Count repeated addition of the same link.
742  * @rm_work: Work structure used for removing the link.
743  * @supplier_preactivated: Supplier has been made active before consumer probe.
744  */
745 struct device_link {
746 	struct device *supplier;
747 	struct list_head s_node;
748 	struct device *consumer;
749 	struct list_head c_node;
750 	struct device link_dev;
751 	enum device_link_state status;
752 	u32 flags;
753 	refcount_t rpm_active;
754 	struct kref kref;
755 	struct work_struct rm_work;
756 	bool supplier_preactivated; /* Owned by consumer probe. */
757 };
758 
759 #define kobj_to_dev(__kobj)	container_of_const(__kobj, struct device, kobj)
760 
761 /**
762  * device_iommu_mapped - Returns true when the device DMA is translated
763  *			 by an IOMMU
764  * @dev: Device to perform the check on
765  */
766 static inline bool device_iommu_mapped(struct device *dev)
767 {
768 	return (dev->iommu_group != NULL);
769 }
770 
771 /* Get the wakeup routines, which depend on struct device */
772 #include <linux/pm_wakeup.h>
773 
774 /**
775  * dev_name - Return a device's name.
776  * @dev: Device with name to get.
777  * Return: The kobject name of the device, or its initial name if unavailable.
778  */
779 static inline const char *dev_name(const struct device *dev)
780 {
781 	/* Use the init name until the kobject becomes available */
782 	if (dev->init_name)
783 		return dev->init_name;
784 
785 	return kobject_name(&dev->kobj);
786 }
787 
788 /**
789  * dev_bus_name - Return a device's bus/class name, if at all possible
790  * @dev: struct device to get the bus/class name of
791  *
792  * Will return the name of the bus/class the device is attached to.  If it is
793  * not attached to a bus/class, an empty string will be returned.
794  */
795 static inline const char *dev_bus_name(const struct device *dev)
796 {
797 	return dev->bus ? dev->bus->name : (dev->class ? dev->class->name : "");
798 }
799 
800 __printf(2, 3) int dev_set_name(struct device *dev, const char *name, ...);
801 
802 #ifdef CONFIG_NUMA
803 static inline int dev_to_node(struct device *dev)
804 {
805 	return dev->numa_node;
806 }
807 static inline void set_dev_node(struct device *dev, int node)
808 {
809 	dev->numa_node = node;
810 }
811 #else
812 static inline int dev_to_node(struct device *dev)
813 {
814 	return NUMA_NO_NODE;
815 }
816 static inline void set_dev_node(struct device *dev, int node)
817 {
818 }
819 #endif
820 
821 static inline struct irq_domain *dev_get_msi_domain(const struct device *dev)
822 {
823 #ifdef CONFIG_GENERIC_MSI_IRQ
824 	return dev->msi.domain;
825 #else
826 	return NULL;
827 #endif
828 }
829 
830 static inline void dev_set_msi_domain(struct device *dev, struct irq_domain *d)
831 {
832 #ifdef CONFIG_GENERIC_MSI_IRQ
833 	dev->msi.domain = d;
834 #endif
835 }
836 
837 static inline void *dev_get_drvdata(const struct device *dev)
838 {
839 	return dev->driver_data;
840 }
841 
842 static inline void dev_set_drvdata(struct device *dev, void *data)
843 {
844 	dev->driver_data = data;
845 }
846 
847 static inline struct pm_subsys_data *dev_to_psd(struct device *dev)
848 {
849 	return dev ? dev->power.subsys_data : NULL;
850 }
851 
852 static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
853 {
854 	return dev->kobj.uevent_suppress;
855 }
856 
857 static inline void dev_set_uevent_suppress(struct device *dev, int val)
858 {
859 	dev->kobj.uevent_suppress = val;
860 }
861 
862 static inline int device_is_registered(struct device *dev)
863 {
864 	return dev->kobj.state_in_sysfs;
865 }
866 
867 static inline void device_enable_async_suspend(struct device *dev)
868 {
869 	if (!dev->power.is_prepared)
870 		dev->power.async_suspend = true;
871 }
872 
873 static inline void device_disable_async_suspend(struct device *dev)
874 {
875 	if (!dev->power.is_prepared)
876 		dev->power.async_suspend = false;
877 }
878 
879 static inline bool device_async_suspend_enabled(struct device *dev)
880 {
881 	return !!dev->power.async_suspend;
882 }
883 
884 static inline bool device_pm_not_required(struct device *dev)
885 {
886 	return dev->power.no_pm;
887 }
888 
889 static inline void device_set_pm_not_required(struct device *dev)
890 {
891 	dev->power.no_pm = true;
892 }
893 
894 static inline void dev_pm_syscore_device(struct device *dev, bool val)
895 {
896 #ifdef CONFIG_PM_SLEEP
897 	dev->power.syscore = val;
898 #endif
899 }
900 
901 static inline void dev_pm_set_driver_flags(struct device *dev, u32 flags)
902 {
903 	dev->power.driver_flags = flags;
904 }
905 
906 static inline bool dev_pm_test_driver_flags(struct device *dev, u32 flags)
907 {
908 	return !!(dev->power.driver_flags & flags);
909 }
910 
911 static inline void device_lock(struct device *dev)
912 {
913 	mutex_lock(&dev->mutex);
914 }
915 
916 static inline int device_lock_interruptible(struct device *dev)
917 {
918 	return mutex_lock_interruptible(&dev->mutex);
919 }
920 
921 static inline int device_trylock(struct device *dev)
922 {
923 	return mutex_trylock(&dev->mutex);
924 }
925 
926 static inline void device_unlock(struct device *dev)
927 {
928 	mutex_unlock(&dev->mutex);
929 }
930 
931 DEFINE_GUARD(device, struct device *, device_lock(_T), device_unlock(_T))
932 
933 static inline void device_lock_assert(struct device *dev)
934 {
935 	lockdep_assert_held(&dev->mutex);
936 }
937 
938 static inline bool dev_has_sync_state(struct device *dev)
939 {
940 	if (!dev)
941 		return false;
942 	if (dev->driver && dev->driver->sync_state)
943 		return true;
944 	if (dev->bus && dev->bus->sync_state)
945 		return true;
946 	return false;
947 }
948 
949 static inline void dev_set_removable(struct device *dev,
950 				     enum device_removable removable)
951 {
952 	dev->removable = removable;
953 }
954 
955 static inline bool dev_is_removable(struct device *dev)
956 {
957 	return dev->removable == DEVICE_REMOVABLE;
958 }
959 
960 static inline bool dev_removable_is_valid(struct device *dev)
961 {
962 	return dev->removable != DEVICE_REMOVABLE_NOT_SUPPORTED;
963 }
964 
965 /*
966  * High level routines for use by the bus drivers
967  */
968 int __must_check device_register(struct device *dev);
969 void device_unregister(struct device *dev);
970 void device_initialize(struct device *dev);
971 int __must_check device_add(struct device *dev);
972 void device_del(struct device *dev);
973 
974 DEFINE_FREE(device_del, struct device *, if (_T) device_del(_T))
975 
976 int device_for_each_child(struct device *parent, void *data,
977 			  device_iter_t fn);
978 int device_for_each_child_reverse(struct device *parent, void *data,
979 				  device_iter_t fn);
980 int device_for_each_child_reverse_from(struct device *parent,
981 				       struct device *from, void *data,
982 				       device_iter_t fn);
983 struct device *device_find_child(struct device *parent, const void *data,
984 				 device_match_t match);
985 /**
986  * device_find_child_by_name - device iterator for locating a child device.
987  * @parent: parent struct device
988  * @name: name of the child device
989  *
990  * This is similar to the device_find_child() function above, but it
991  * returns a reference to a device that has the name @name.
992  *
993  * NOTE: you will need to drop the reference with put_device() after use.
994  */
995 static inline struct device *device_find_child_by_name(struct device *parent,
996 						       const char *name)
997 {
998 	return device_find_child(parent, name, device_match_name);
999 }
1000 
1001 /**
1002  * device_find_any_child - device iterator for locating a child device, if any.
1003  * @parent: parent struct device
1004  *
1005  * This is similar to the device_find_child() function above, but it
1006  * returns a reference to a child device, if any.
1007  *
1008  * NOTE: you will need to drop the reference with put_device() after use.
1009  */
1010 static inline struct device *device_find_any_child(struct device *parent)
1011 {
1012 	return device_find_child(parent, NULL, device_match_any);
1013 }
1014 
1015 int device_rename(struct device *dev, const char *new_name);
1016 int device_move(struct device *dev, struct device *new_parent,
1017 		enum dpm_order dpm_order);
1018 int device_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid);
1019 
1020 static inline bool device_supports_offline(struct device *dev)
1021 {
1022 	return dev->bus && dev->bus->offline && dev->bus->online;
1023 }
1024 
1025 #define __device_lock_set_class(dev, name, key)                        \
1026 do {                                                                   \
1027 	struct device *__d2 __maybe_unused = dev;                      \
1028 	lock_set_class(&__d2->mutex.dep_map, name, key, 0, _THIS_IP_); \
1029 } while (0)
1030 
1031 /**
1032  * device_lock_set_class - Specify a temporary lock class while a device
1033  *			   is attached to a driver
1034  * @dev: device to modify
1035  * @key: lock class key data
1036  *
1037  * This must be called with the device_lock() already held, for example
1038  * from driver ->probe(). Take care to only override the default
1039  * lockdep_no_validate class.
1040  */
1041 #ifdef CONFIG_LOCKDEP
1042 #define device_lock_set_class(dev, key)                                    \
1043 do {                                                                       \
1044 	struct device *__d = dev;                                          \
1045 	dev_WARN_ONCE(__d, !lockdep_match_class(&__d->mutex,               \
1046 						&__lockdep_no_validate__), \
1047 		 "overriding existing custom lock class\n");               \
1048 	__device_lock_set_class(__d, #key, key);                           \
1049 } while (0)
1050 #else
1051 #define device_lock_set_class(dev, key) __device_lock_set_class(dev, #key, key)
1052 #endif
1053 
1054 /**
1055  * device_lock_reset_class - Return a device to the default lockdep novalidate state
1056  * @dev: device to modify
1057  *
1058  * This must be called with the device_lock() already held, for example
1059  * from driver ->remove().
1060  */
1061 #define device_lock_reset_class(dev) \
1062 do { \
1063 	struct device *__d __maybe_unused = dev;                       \
1064 	lock_set_novalidate_class(&__d->mutex.dep_map, "&dev->mutex",  \
1065 				  _THIS_IP_);                          \
1066 } while (0)
1067 
1068 void lock_device_hotplug(void);
1069 void unlock_device_hotplug(void);
1070 int lock_device_hotplug_sysfs(void);
1071 int device_offline(struct device *dev);
1072 int device_online(struct device *dev);
1073 
1074 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
1075 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
1076 void device_set_node(struct device *dev, struct fwnode_handle *fwnode);
1077 void device_set_of_node_from_dev(struct device *dev, const struct device *dev2);
1078 
1079 static inline struct device_node *dev_of_node(struct device *dev)
1080 {
1081 	if (!IS_ENABLED(CONFIG_OF) || !dev)
1082 		return NULL;
1083 	return dev->of_node;
1084 }
1085 
1086 static inline int dev_num_vf(struct device *dev)
1087 {
1088 	if (dev->bus && dev->bus->num_vf)
1089 		return dev->bus->num_vf(dev);
1090 	return 0;
1091 }
1092 
1093 /*
1094  * Root device objects for grouping under /sys/devices
1095  */
1096 struct device *__root_device_register(const char *name, struct module *owner);
1097 
1098 /* This is a macro to avoid include problems with THIS_MODULE */
1099 #define root_device_register(name) \
1100 	__root_device_register(name, THIS_MODULE)
1101 
1102 void root_device_unregister(struct device *root);
1103 
1104 static inline void *dev_get_platdata(const struct device *dev)
1105 {
1106 	return dev->platform_data;
1107 }
1108 
1109 /*
1110  * Manual binding of a device to driver. See drivers/base/bus.c
1111  * for information on use.
1112  */
1113 int __must_check device_driver_attach(const struct device_driver *drv,
1114 				      struct device *dev);
1115 int __must_check device_bind_driver(struct device *dev);
1116 void device_release_driver(struct device *dev);
1117 int  __must_check device_attach(struct device *dev);
1118 int __must_check driver_attach(const struct device_driver *drv);
1119 void device_initial_probe(struct device *dev);
1120 int __must_check device_reprobe(struct device *dev);
1121 
1122 bool device_is_bound(struct device *dev);
1123 
1124 /*
1125  * Easy functions for dynamically creating devices on the fly
1126  */
1127 __printf(5, 6) struct device *
1128 device_create(const struct class *cls, struct device *parent, dev_t devt,
1129 	      void *drvdata, const char *fmt, ...);
1130 __printf(6, 7) struct device *
1131 device_create_with_groups(const struct class *cls, struct device *parent, dev_t devt,
1132 			  void *drvdata, const struct attribute_group **groups,
1133 			  const char *fmt, ...);
1134 void device_destroy(const struct class *cls, dev_t devt);
1135 
1136 int __must_check device_add_groups(struct device *dev,
1137 				   const struct attribute_group **groups);
1138 void device_remove_groups(struct device *dev,
1139 			  const struct attribute_group **groups);
1140 
1141 static inline int __must_check device_add_group(struct device *dev,
1142 					const struct attribute_group *grp)
1143 {
1144 	const struct attribute_group *groups[] = { grp, NULL };
1145 
1146 	return device_add_groups(dev, groups);
1147 }
1148 
1149 static inline void device_remove_group(struct device *dev,
1150 				       const struct attribute_group *grp)
1151 {
1152 	const struct attribute_group *groups[] = { grp, NULL };
1153 
1154 	return device_remove_groups(dev, groups);
1155 }
1156 
1157 int __must_check devm_device_add_group(struct device *dev,
1158 				       const struct attribute_group *grp);
1159 
1160 /*
1161  * get_device - atomically increment the reference count for the device.
1162  *
1163  */
1164 struct device *get_device(struct device *dev);
1165 void put_device(struct device *dev);
1166 
1167 DEFINE_FREE(put_device, struct device *, if (_T) put_device(_T))
1168 
1169 bool kill_device(struct device *dev);
1170 
1171 #ifdef CONFIG_DEVTMPFS
1172 int devtmpfs_mount(void);
1173 #else
1174 static inline int devtmpfs_mount(void) { return 0; }
1175 #endif
1176 
1177 /* drivers/base/power/shutdown.c */
1178 void device_shutdown(void);
1179 
1180 /* debugging and troubleshooting/diagnostic helpers. */
1181 const char *dev_driver_string(const struct device *dev);
1182 
1183 /* Device links interface. */
1184 struct device_link *device_link_add(struct device *consumer,
1185 				    struct device *supplier, u32 flags);
1186 void device_link_del(struct device_link *link);
1187 void device_link_remove(void *consumer, struct device *supplier);
1188 void device_links_supplier_sync_state_pause(void);
1189 void device_links_supplier_sync_state_resume(void);
1190 void device_link_wait_removal(void);
1191 
1192 /* Create alias, so I can be autoloaded. */
1193 #define MODULE_ALIAS_CHARDEV(major,minor) \
1194 	MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
1195 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
1196 	MODULE_ALIAS("char-major-" __stringify(major) "-*")
1197 
1198 #endif /* _DEVICE_H_ */
1199