4356 lines · 116.2 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0/* * drivers/base/core.c - core driver model code (device registration, etc) * * Copyright (c) 2002-3 Patrick Mochel * Copyright (c) 2002-3 Open Source Development Labs * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de> * Copyright (c) 2006 Novell, Inc. */ #include <linux/acpi.h>#include <linux/cpufreq.h>#include <linux/device.h>#include <linux/err.h>#include <linux/fwnode.h>#include <linux/init.h>#include <linux/module.h>#include <linux/slab.h>#include <linux/string.h>#include <linux/kdev_t.h>#include <linux/notifier.h>#include <linux/of.h>#include <linux/of_device.h>#include <linux/genhd.h>#include <linux/mutex.h>#include <linux/pm_runtime.h>#include <linux/netdevice.h>#include <linux/sched/signal.h>#include <linux/sysfs.h> #include "base.h"#include "power/power.h" #ifdef CONFIG_SYSFS_DEPRECATED#ifdef CONFIG_SYSFS_DEPRECATED_V2long sysfs_deprecated = 1;#elselong sysfs_deprecated = 0;#endifstatic int __init sysfs_deprecated_setup(char *arg){	return kstrtol(arg, 10, &sysfs_deprecated);}early_param("sysfs.deprecated", sysfs_deprecated_setup);#endif /* Device links support. */static LIST_HEAD(wait_for_suppliers);static DEFINE_MUTEX(wfs_lock);static LIST_HEAD(deferred_sync);static unsigned int defer_sync_state_count = 1;static unsigned int defer_fw_devlink_count;static LIST_HEAD(deferred_fw_devlink);static DEFINE_MUTEX(defer_fw_devlink_lock);static bool fw_devlink_is_permissive(void); #ifdef CONFIG_SRCUstatic DEFINE_MUTEX(device_links_lock);DEFINE_STATIC_SRCU(device_links_srcu); static inline void device_links_write_lock(void){	mutex_lock(&device_links_lock);} static inline void device_links_write_unlock(void){	mutex_unlock(&device_links_lock);} int device_links_read_lock(void) __acquires(&device_links_srcu){	return srcu_read_lock(&device_links_srcu);} void device_links_read_unlock(int idx) __releases(&device_links_srcu){	srcu_read_unlock(&device_links_srcu, idx);} int device_links_read_lock_held(void){	return srcu_read_lock_held(&device_links_srcu);}#else /* !CONFIG_SRCU */static DECLARE_RWSEM(device_links_lock); static inline void device_links_write_lock(void){	down_write(&device_links_lock);} static inline void device_links_write_unlock(void){	up_write(&device_links_lock);} int device_links_read_lock(void){	down_read(&device_links_lock);	return 0;} void device_links_read_unlock(int not_used){	up_read(&device_links_lock);} #ifdef CONFIG_DEBUG_LOCK_ALLOCint device_links_read_lock_held(void){	return lockdep_is_held(&device_links_lock);}#endif#endif /* !CONFIG_SRCU */ /** * device_is_dependent - Check if one device depends on another one * @dev: Device to check dependencies for. * @target: Device to check against. * * Check if @target depends on @dev or any device dependent on it (its child or * its consumer etc).  Return 1 if that is the case or 0 otherwise. */int device_is_dependent(struct device *dev, void *target){	struct device_link *link;	int ret; 	if (dev == target)		return 1; 	ret = device_for_each_child(dev, target, device_is_dependent);	if (ret)		return ret; 	list_for_each_entry(link, &dev->links.consumers, s_node) {		if (link->flags == (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))			continue; 		if (link->consumer == target)			return 1; 		ret = device_is_dependent(link->consumer, target);		if (ret)			break;	}	return ret;} static void device_link_init_status(struct device_link *link,				    struct device *consumer,				    struct device *supplier){	switch (supplier->links.status) {	case DL_DEV_PROBING:		switch (consumer->links.status) {		case DL_DEV_PROBING:			/*			 * A consumer driver can create a link to a supplier			 * that has not completed its probing yet as long as it			 * knows that the supplier is already functional (for			 * example, it has just acquired some resources from the			 * supplier).			 */			link->status = DL_STATE_CONSUMER_PROBE;			break;		default:			link->status = DL_STATE_DORMANT;			break;		}		break;	case DL_DEV_DRIVER_BOUND:		switch (consumer->links.status) {		case DL_DEV_PROBING:			link->status = DL_STATE_CONSUMER_PROBE;			break;		case DL_DEV_DRIVER_BOUND:			link->status = DL_STATE_ACTIVE;			break;		default:			link->status = DL_STATE_AVAILABLE;			break;		}		break;	case DL_DEV_UNBINDING:		link->status = DL_STATE_SUPPLIER_UNBIND;		break;	default:		link->status = DL_STATE_DORMANT;		break;	}} static int device_reorder_to_tail(struct device *dev, void *not_used){	struct device_link *link; 	/*	 * Devices that have not been registered yet will be put to the ends	 * of the lists during the registration, so skip them here.	 */	if (device_is_registered(dev))		devices_kset_move_last(dev); 	if (device_pm_initialized(dev))		device_pm_move_last(dev); 	device_for_each_child(dev, NULL, device_reorder_to_tail);	list_for_each_entry(link, &dev->links.consumers, s_node) {		if (link->flags == (DL_FLAG_SYNC_STATE_ONLY | DL_FLAG_MANAGED))			continue;		device_reorder_to_tail(link->consumer, NULL);	} 	return 0;} /** * device_pm_move_to_tail - Move set of devices to the end of device lists * @dev: Device to move * * This is a device_reorder_to_tail() wrapper taking the requisite locks. * * It moves the @dev along with all of its children and all of its consumers * to the ends of the device_kset and dpm_list, recursively. */void device_pm_move_to_tail(struct device *dev){	int idx; 	idx = device_links_read_lock();	device_pm_lock();	device_reorder_to_tail(dev, NULL);	device_pm_unlock();	device_links_read_unlock(idx);} #define to_devlink(dev)	container_of((dev), struct device_link, link_dev) static ssize_t status_show(struct device *dev,			  struct device_attribute *attr, char *buf){	char *status; 	switch (to_devlink(dev)->status) {	case DL_STATE_NONE:		status = "not tracked"; break;	case DL_STATE_DORMANT:		status = "dormant"; break;	case DL_STATE_AVAILABLE:		status = "available"; break;	case DL_STATE_CONSUMER_PROBE:		status = "consumer probing"; break;	case DL_STATE_ACTIVE:		status = "active"; break;	case DL_STATE_SUPPLIER_UNBIND:		status = "supplier unbinding"; break;	default:		status = "unknown"; break;	}	return sprintf(buf, "%s\n", status);}static DEVICE_ATTR_RO(status); static ssize_t auto_remove_on_show(struct device *dev,				   struct device_attribute *attr, char *buf){	struct device_link *link = to_devlink(dev);	char *str; 	if (link->flags & DL_FLAG_AUTOREMOVE_SUPPLIER)		str = "supplier unbind";	else if (link->flags & DL_FLAG_AUTOREMOVE_CONSUMER)		str = "consumer unbind";	else		str = "never"; 	return sprintf(buf, "%s\n", str);}static DEVICE_ATTR_RO(auto_remove_on); static ssize_t runtime_pm_show(struct device *dev,			       struct device_attribute *attr, char *buf){	struct device_link *link = to_devlink(dev); 	return sprintf(buf, "%d\n", !!(link->flags & DL_FLAG_PM_RUNTIME));}static DEVICE_ATTR_RO(runtime_pm); static ssize_t sync_state_only_show(struct device *dev,				    struct device_attribute *attr, char *buf){	struct device_link *link = to_devlink(dev); 	return sprintf(buf, "%d\n", !!(link->flags & DL_FLAG_SYNC_STATE_ONLY));}static DEVICE_ATTR_RO(sync_state_only); static struct attribute *devlink_attrs[] = {	&dev_attr_status.attr,	&dev_attr_auto_remove_on.attr,	&dev_attr_runtime_pm.attr,	&dev_attr_sync_state_only.attr,	NULL,};ATTRIBUTE_GROUPS(devlink); static void device_link_free(struct device_link *link){	while (refcount_dec_not_one(&link->rpm_active))		pm_runtime_put(link->supplier); 	put_device(link->consumer);	put_device(link->supplier);	kfree(link);} #ifdef CONFIG_SRCUstatic void __device_link_free_srcu(struct rcu_head *rhead){	device_link_free(container_of(rhead, struct device_link, rcu_head));} static void devlink_dev_release(struct device *dev){	struct device_link *link = to_devlink(dev); 	call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);}#elsestatic void devlink_dev_release(struct device *dev){	device_link_free(to_devlink(dev));}#endif static struct class devlink_class = {	.name = "devlink",	.owner = THIS_MODULE,	.dev_groups = devlink_groups,	.dev_release = devlink_dev_release,}; static int devlink_add_symlinks(struct device *dev,				struct class_interface *class_intf){	int ret;	size_t len;	struct device_link *link = to_devlink(dev);	struct device *sup = link->supplier;	struct device *con = link->consumer;	char *buf; 	len = max(strlen(dev_name(sup)), strlen(dev_name(con)));	len += strlen("supplier:") + 1;	buf = kzalloc(len, GFP_KERNEL);	if (!buf)		return -ENOMEM; 	ret = sysfs_create_link(&link->link_dev.kobj, &sup->kobj, "supplier");	if (ret)		goto out; 	ret = sysfs_create_link(&link->link_dev.kobj, &con->kobj, "consumer");	if (ret)		goto err_con; 	snprintf(buf, len, "consumer:%s", dev_name(con));	ret = sysfs_create_link(&sup->kobj, &link->link_dev.kobj, buf);	if (ret)		goto err_con_dev; 	snprintf(buf, len, "supplier:%s", dev_name(sup));	ret = sysfs_create_link(&con->kobj, &link->link_dev.kobj, buf);	if (ret)		goto err_sup_dev; 	goto out; err_sup_dev:	snprintf(buf, len, "consumer:%s", dev_name(con));	sysfs_remove_link(&sup->kobj, buf);err_con_dev:	sysfs_remove_link(&link->link_dev.kobj, "consumer");err_con:	sysfs_remove_link(&link->link_dev.kobj, "supplier");out:	kfree(buf);	return ret;} static void devlink_remove_symlinks(struct device *dev,				   struct class_interface *class_intf){	struct device_link *link = to_devlink(dev);	size_t len;	struct device *sup = link->supplier;	struct device *con = link->consumer;	char *buf; 	sysfs_remove_link(&link->link_dev.kobj, "consumer");	sysfs_remove_link(&link->link_dev.kobj, "supplier"); 	len = max(strlen(dev_name(sup)), strlen(dev_name(con)));	len += strlen("supplier:") + 1;	buf = kzalloc(len, GFP_KERNEL);	if (!buf) {		WARN(1, "Unable to properly free device link symlinks!\n");		return;	} 	snprintf(buf, len, "supplier:%s", dev_name(sup));	sysfs_remove_link(&con->kobj, buf);	snprintf(buf, len, "consumer:%s", dev_name(con));	sysfs_remove_link(&sup->kobj, buf);	kfree(buf);} static struct class_interface devlink_class_intf = {	.class = &devlink_class,	.add_dev = devlink_add_symlinks,	.remove_dev = devlink_remove_symlinks,}; static int __init devlink_class_init(void){	int ret; 	ret = class_register(&devlink_class);	if (ret)		return ret; 	ret = class_interface_register(&devlink_class_intf);	if (ret)		class_unregister(&devlink_class); 	return ret;}postcore_initcall(devlink_class_init); #define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \			       DL_FLAG_AUTOREMOVE_SUPPLIER | \			       DL_FLAG_AUTOPROBE_CONSUMER  | \			       DL_FLAG_SYNC_STATE_ONLY) #define DL_ADD_VALID_FLAGS (DL_MANAGED_LINK_FLAGS | DL_FLAG_STATELESS | \			    DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE) /** * device_link_add - Create a link between two devices. * @consumer: Consumer end of the link. * @supplier: Supplier end of the link. * @flags: Link flags. * * The caller is responsible for the proper synchronization of the link creation * with runtime PM.  First, setting the DL_FLAG_PM_RUNTIME flag will cause the * runtime PM framework to take the link into account.  Second, if the * DL_FLAG_RPM_ACTIVE flag is set in addition to it, the supplier devices will * be forced into the active metastate and reference-counted upon the creation * of the link.  If DL_FLAG_PM_RUNTIME is not set, DL_FLAG_RPM_ACTIVE will be * ignored. * * If DL_FLAG_STATELESS is set in @flags, the caller of this function is * expected to release the link returned by it directly with the help of either * device_link_del() or device_link_remove(). * * If that flag is not set, however, the caller of this function is handing the * management of the link over to the driver core entirely and its return value * can only be used to check whether or not the link is present.  In that case, * the DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER device link * flags can be used to indicate to the driver core when the link can be safely * deleted.  Namely, setting one of them in @flags indicates to the driver core * that the link is not going to be used (by the given caller of this function) * after unbinding the consumer or supplier driver, respectively, from its * device, so the link can be deleted at that point.  If none of them is set, * the link will be maintained until one of the devices pointed to by it (either * the consumer or the supplier) is unregistered. * * Also, if DL_FLAG_STATELESS, DL_FLAG_AUTOREMOVE_CONSUMER and * DL_FLAG_AUTOREMOVE_SUPPLIER are not set in @flags (that is, a persistent * managed device link is being added), the DL_FLAG_AUTOPROBE_CONSUMER flag can * be used to request the driver core to automaticall probe for a consmer * driver after successfully binding a driver to the supplier device. * * The combination of DL_FLAG_STATELESS and one of DL_FLAG_AUTOREMOVE_CONSUMER, * DL_FLAG_AUTOREMOVE_SUPPLIER, or DL_FLAG_AUTOPROBE_CONSUMER set in @flags at * the same time is invalid and will cause NULL to be returned upfront. * However, if a device link between the given @consumer and @supplier pair * exists already when this function is called for them, the existing link will * be returned regardless of its current type and status (the link's flags may * be modified then).  The caller of this function is then expected to treat * the link as though it has just been created, so (in particular) if * DL_FLAG_STATELESS was passed in @flags, the link needs to be released * explicitly when not needed any more (as stated above). * * A side effect of the link creation is re-ordering of dpm_list and the * devices_kset list by moving the consumer device and all devices depending * on it to the ends of these lists (that does not happen to devices that have

Lines 1–500 of 4356.