18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * drivers/base/devres.c - device resource management
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2006  SUSE Linux Products GmbH
68c2ecf20Sopenharmony_ci * Copyright (c) 2006  Tejun Heo <teheo@suse.de>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/device.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/slab.h>
128c2ecf20Sopenharmony_ci#include <linux/percpu.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <asm/sections.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include "base.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cistruct devres_node {
198c2ecf20Sopenharmony_ci	struct list_head		entry;
208c2ecf20Sopenharmony_ci	dr_release_t			release;
218c2ecf20Sopenharmony_ci#ifdef CONFIG_DEBUG_DEVRES
228c2ecf20Sopenharmony_ci	const char			*name;
238c2ecf20Sopenharmony_ci	size_t				size;
248c2ecf20Sopenharmony_ci#endif
258c2ecf20Sopenharmony_ci};
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistruct devres {
288c2ecf20Sopenharmony_ci	struct devres_node		node;
298c2ecf20Sopenharmony_ci	/*
308c2ecf20Sopenharmony_ci	 * Some archs want to perform DMA into kmalloc caches
318c2ecf20Sopenharmony_ci	 * and need a guaranteed alignment larger than
328c2ecf20Sopenharmony_ci	 * the alignment of a 64-bit integer.
338c2ecf20Sopenharmony_ci	 * Thus we use ARCH_KMALLOC_MINALIGN here and get exactly the same
348c2ecf20Sopenharmony_ci	 * buffer alignment as if it was allocated by plain kmalloc().
358c2ecf20Sopenharmony_ci	 */
368c2ecf20Sopenharmony_ci	u8 __aligned(ARCH_KMALLOC_MINALIGN) data[];
378c2ecf20Sopenharmony_ci};
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistruct devres_group {
408c2ecf20Sopenharmony_ci	struct devres_node		node[2];
418c2ecf20Sopenharmony_ci	void				*id;
428c2ecf20Sopenharmony_ci	int				color;
438c2ecf20Sopenharmony_ci	/* -- 8 pointers */
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#ifdef CONFIG_DEBUG_DEVRES
478c2ecf20Sopenharmony_cistatic int log_devres = 0;
488c2ecf20Sopenharmony_cimodule_param_named(log, log_devres, int, S_IRUGO | S_IWUSR);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic void set_node_dbginfo(struct devres_node *node, const char *name,
518c2ecf20Sopenharmony_ci			     size_t size)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	node->name = name;
548c2ecf20Sopenharmony_ci	node->size = size;
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic void devres_log(struct device *dev, struct devres_node *node,
588c2ecf20Sopenharmony_ci		       const char *op)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	if (unlikely(log_devres))
618c2ecf20Sopenharmony_ci		dev_err(dev, "DEVRES %3s %p %s (%lu bytes)\n",
628c2ecf20Sopenharmony_ci			op, node, node->name, (unsigned long)node->size);
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci#else /* CONFIG_DEBUG_DEVRES */
658c2ecf20Sopenharmony_ci#define set_node_dbginfo(node, n, s)	do {} while (0)
668c2ecf20Sopenharmony_ci#define devres_log(dev, node, op)	do {} while (0)
678c2ecf20Sopenharmony_ci#endif /* CONFIG_DEBUG_DEVRES */
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/*
708c2ecf20Sopenharmony_ci * Release functions for devres group.  These callbacks are used only
718c2ecf20Sopenharmony_ci * for identification.
728c2ecf20Sopenharmony_ci */
738c2ecf20Sopenharmony_cistatic void group_open_release(struct device *dev, void *res)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	/* noop */
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic void group_close_release(struct device *dev, void *res)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	/* noop */
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic struct devres_group * node_to_group(struct devres_node *node)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	if (node->release == &group_open_release)
868c2ecf20Sopenharmony_ci		return container_of(node, struct devres_group, node[0]);
878c2ecf20Sopenharmony_ci	if (node->release == &group_close_release)
888c2ecf20Sopenharmony_ci		return container_of(node, struct devres_group, node[1]);
898c2ecf20Sopenharmony_ci	return NULL;
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistatic bool check_dr_size(size_t size, size_t *tot_size)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	/* We must catch any near-SIZE_MAX cases that could overflow. */
958c2ecf20Sopenharmony_ci	if (unlikely(check_add_overflow(sizeof(struct devres),
968c2ecf20Sopenharmony_ci					size, tot_size)))
978c2ecf20Sopenharmony_ci		return false;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	return true;
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic __always_inline struct devres * alloc_dr(dr_release_t release,
1038c2ecf20Sopenharmony_ci						size_t size, gfp_t gfp, int nid)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	size_t tot_size;
1068c2ecf20Sopenharmony_ci	struct devres *dr;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	if (!check_dr_size(size, &tot_size))
1098c2ecf20Sopenharmony_ci		return NULL;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	dr = kmalloc_node_track_caller(tot_size, gfp, nid);
1128c2ecf20Sopenharmony_ci	if (unlikely(!dr))
1138c2ecf20Sopenharmony_ci		return NULL;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	memset(dr, 0, offsetof(struct devres, data));
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&dr->node.entry);
1188c2ecf20Sopenharmony_ci	dr->node.release = release;
1198c2ecf20Sopenharmony_ci	return dr;
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic void add_dr(struct device *dev, struct devres_node *node)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	devres_log(dev, node, "ADD");
1258c2ecf20Sopenharmony_ci	BUG_ON(!list_empty(&node->entry));
1268c2ecf20Sopenharmony_ci	list_add_tail(&node->entry, &dev->devres_head);
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic void replace_dr(struct device *dev,
1308c2ecf20Sopenharmony_ci		       struct devres_node *old, struct devres_node *new)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	devres_log(dev, old, "REPLACE");
1338c2ecf20Sopenharmony_ci	BUG_ON(!list_empty(&new->entry));
1348c2ecf20Sopenharmony_ci	list_replace(&old->entry, &new->entry);
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci#ifdef CONFIG_DEBUG_DEVRES
1388c2ecf20Sopenharmony_civoid * __devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid,
1398c2ecf20Sopenharmony_ci		      const char *name)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	struct devres *dr;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	dr = alloc_dr(release, size, gfp | __GFP_ZERO, nid);
1448c2ecf20Sopenharmony_ci	if (unlikely(!dr))
1458c2ecf20Sopenharmony_ci		return NULL;
1468c2ecf20Sopenharmony_ci	set_node_dbginfo(&dr->node, name, size);
1478c2ecf20Sopenharmony_ci	return dr->data;
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(__devres_alloc_node);
1508c2ecf20Sopenharmony_ci#else
1518c2ecf20Sopenharmony_ci/**
1528c2ecf20Sopenharmony_ci * devres_alloc - Allocate device resource data
1538c2ecf20Sopenharmony_ci * @release: Release function devres will be associated with
1548c2ecf20Sopenharmony_ci * @size: Allocation size
1558c2ecf20Sopenharmony_ci * @gfp: Allocation flags
1568c2ecf20Sopenharmony_ci * @nid: NUMA node
1578c2ecf20Sopenharmony_ci *
1588c2ecf20Sopenharmony_ci * Allocate devres of @size bytes.  The allocated area is zeroed, then
1598c2ecf20Sopenharmony_ci * associated with @release.  The returned pointer can be passed to
1608c2ecf20Sopenharmony_ci * other devres_*() functions.
1618c2ecf20Sopenharmony_ci *
1628c2ecf20Sopenharmony_ci * RETURNS:
1638c2ecf20Sopenharmony_ci * Pointer to allocated devres on success, NULL on failure.
1648c2ecf20Sopenharmony_ci */
1658c2ecf20Sopenharmony_civoid * devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	struct devres *dr;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	dr = alloc_dr(release, size, gfp | __GFP_ZERO, nid);
1708c2ecf20Sopenharmony_ci	if (unlikely(!dr))
1718c2ecf20Sopenharmony_ci		return NULL;
1728c2ecf20Sopenharmony_ci	return dr->data;
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devres_alloc_node);
1758c2ecf20Sopenharmony_ci#endif
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci/**
1788c2ecf20Sopenharmony_ci * devres_for_each_res - Resource iterator
1798c2ecf20Sopenharmony_ci * @dev: Device to iterate resource from
1808c2ecf20Sopenharmony_ci * @release: Look for resources associated with this release function
1818c2ecf20Sopenharmony_ci * @match: Match function (optional)
1828c2ecf20Sopenharmony_ci * @match_data: Data for the match function
1838c2ecf20Sopenharmony_ci * @fn: Function to be called for each matched resource.
1848c2ecf20Sopenharmony_ci * @data: Data for @fn, the 3rd parameter of @fn
1858c2ecf20Sopenharmony_ci *
1868c2ecf20Sopenharmony_ci * Call @fn for each devres of @dev which is associated with @release
1878c2ecf20Sopenharmony_ci * and for which @match returns 1.
1888c2ecf20Sopenharmony_ci *
1898c2ecf20Sopenharmony_ci * RETURNS:
1908c2ecf20Sopenharmony_ci * 	void
1918c2ecf20Sopenharmony_ci */
1928c2ecf20Sopenharmony_civoid devres_for_each_res(struct device *dev, dr_release_t release,
1938c2ecf20Sopenharmony_ci			dr_match_t match, void *match_data,
1948c2ecf20Sopenharmony_ci			void (*fn)(struct device *, void *, void *),
1958c2ecf20Sopenharmony_ci			void *data)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	struct devres_node *node;
1988c2ecf20Sopenharmony_ci	struct devres_node *tmp;
1998c2ecf20Sopenharmony_ci	unsigned long flags;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	if (!fn)
2028c2ecf20Sopenharmony_ci		return;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dev->devres_lock, flags);
2058c2ecf20Sopenharmony_ci	list_for_each_entry_safe_reverse(node, tmp,
2068c2ecf20Sopenharmony_ci			&dev->devres_head, entry) {
2078c2ecf20Sopenharmony_ci		struct devres *dr = container_of(node, struct devres, node);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci		if (node->release != release)
2108c2ecf20Sopenharmony_ci			continue;
2118c2ecf20Sopenharmony_ci		if (match && !match(dev, dr->data, match_data))
2128c2ecf20Sopenharmony_ci			continue;
2138c2ecf20Sopenharmony_ci		fn(dev, dr->data, data);
2148c2ecf20Sopenharmony_ci	}
2158c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dev->devres_lock, flags);
2168c2ecf20Sopenharmony_ci}
2178c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devres_for_each_res);
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci/**
2208c2ecf20Sopenharmony_ci * devres_free - Free device resource data
2218c2ecf20Sopenharmony_ci * @res: Pointer to devres data to free
2228c2ecf20Sopenharmony_ci *
2238c2ecf20Sopenharmony_ci * Free devres created with devres_alloc().
2248c2ecf20Sopenharmony_ci */
2258c2ecf20Sopenharmony_civoid devres_free(void *res)
2268c2ecf20Sopenharmony_ci{
2278c2ecf20Sopenharmony_ci	if (res) {
2288c2ecf20Sopenharmony_ci		struct devres *dr = container_of(res, struct devres, data);
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci		BUG_ON(!list_empty(&dr->node.entry));
2318c2ecf20Sopenharmony_ci		kfree(dr);
2328c2ecf20Sopenharmony_ci	}
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devres_free);
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci/**
2378c2ecf20Sopenharmony_ci * devres_add - Register device resource
2388c2ecf20Sopenharmony_ci * @dev: Device to add resource to
2398c2ecf20Sopenharmony_ci * @res: Resource to register
2408c2ecf20Sopenharmony_ci *
2418c2ecf20Sopenharmony_ci * Register devres @res to @dev.  @res should have been allocated
2428c2ecf20Sopenharmony_ci * using devres_alloc().  On driver detach, the associated release
2438c2ecf20Sopenharmony_ci * function will be invoked and devres will be freed automatically.
2448c2ecf20Sopenharmony_ci */
2458c2ecf20Sopenharmony_civoid devres_add(struct device *dev, void *res)
2468c2ecf20Sopenharmony_ci{
2478c2ecf20Sopenharmony_ci	struct devres *dr = container_of(res, struct devres, data);
2488c2ecf20Sopenharmony_ci	unsigned long flags;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dev->devres_lock, flags);
2518c2ecf20Sopenharmony_ci	add_dr(dev, &dr->node);
2528c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dev->devres_lock, flags);
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devres_add);
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_cistatic struct devres *find_dr(struct device *dev, dr_release_t release,
2578c2ecf20Sopenharmony_ci			      dr_match_t match, void *match_data)
2588c2ecf20Sopenharmony_ci{
2598c2ecf20Sopenharmony_ci	struct devres_node *node;
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	list_for_each_entry_reverse(node, &dev->devres_head, entry) {
2628c2ecf20Sopenharmony_ci		struct devres *dr = container_of(node, struct devres, node);
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci		if (node->release != release)
2658c2ecf20Sopenharmony_ci			continue;
2668c2ecf20Sopenharmony_ci		if (match && !match(dev, dr->data, match_data))
2678c2ecf20Sopenharmony_ci			continue;
2688c2ecf20Sopenharmony_ci		return dr;
2698c2ecf20Sopenharmony_ci	}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	return NULL;
2728c2ecf20Sopenharmony_ci}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci/**
2758c2ecf20Sopenharmony_ci * devres_find - Find device resource
2768c2ecf20Sopenharmony_ci * @dev: Device to lookup resource from
2778c2ecf20Sopenharmony_ci * @release: Look for resources associated with this release function
2788c2ecf20Sopenharmony_ci * @match: Match function (optional)
2798c2ecf20Sopenharmony_ci * @match_data: Data for the match function
2808c2ecf20Sopenharmony_ci *
2818c2ecf20Sopenharmony_ci * Find the latest devres of @dev which is associated with @release
2828c2ecf20Sopenharmony_ci * and for which @match returns 1.  If @match is NULL, it's considered
2838c2ecf20Sopenharmony_ci * to match all.
2848c2ecf20Sopenharmony_ci *
2858c2ecf20Sopenharmony_ci * RETURNS:
2868c2ecf20Sopenharmony_ci * Pointer to found devres, NULL if not found.
2878c2ecf20Sopenharmony_ci */
2888c2ecf20Sopenharmony_civoid * devres_find(struct device *dev, dr_release_t release,
2898c2ecf20Sopenharmony_ci		   dr_match_t match, void *match_data)
2908c2ecf20Sopenharmony_ci{
2918c2ecf20Sopenharmony_ci	struct devres *dr;
2928c2ecf20Sopenharmony_ci	unsigned long flags;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dev->devres_lock, flags);
2958c2ecf20Sopenharmony_ci	dr = find_dr(dev, release, match, match_data);
2968c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dev->devres_lock, flags);
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	if (dr)
2998c2ecf20Sopenharmony_ci		return dr->data;
3008c2ecf20Sopenharmony_ci	return NULL;
3018c2ecf20Sopenharmony_ci}
3028c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devres_find);
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci/**
3058c2ecf20Sopenharmony_ci * devres_get - Find devres, if non-existent, add one atomically
3068c2ecf20Sopenharmony_ci * @dev: Device to lookup or add devres for
3078c2ecf20Sopenharmony_ci * @new_res: Pointer to new initialized devres to add if not found
3088c2ecf20Sopenharmony_ci * @match: Match function (optional)
3098c2ecf20Sopenharmony_ci * @match_data: Data for the match function
3108c2ecf20Sopenharmony_ci *
3118c2ecf20Sopenharmony_ci * Find the latest devres of @dev which has the same release function
3128c2ecf20Sopenharmony_ci * as @new_res and for which @match return 1.  If found, @new_res is
3138c2ecf20Sopenharmony_ci * freed; otherwise, @new_res is added atomically.
3148c2ecf20Sopenharmony_ci *
3158c2ecf20Sopenharmony_ci * RETURNS:
3168c2ecf20Sopenharmony_ci * Pointer to found or added devres.
3178c2ecf20Sopenharmony_ci */
3188c2ecf20Sopenharmony_civoid * devres_get(struct device *dev, void *new_res,
3198c2ecf20Sopenharmony_ci		  dr_match_t match, void *match_data)
3208c2ecf20Sopenharmony_ci{
3218c2ecf20Sopenharmony_ci	struct devres *new_dr = container_of(new_res, struct devres, data);
3228c2ecf20Sopenharmony_ci	struct devres *dr;
3238c2ecf20Sopenharmony_ci	unsigned long flags;
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dev->devres_lock, flags);
3268c2ecf20Sopenharmony_ci	dr = find_dr(dev, new_dr->node.release, match, match_data);
3278c2ecf20Sopenharmony_ci	if (!dr) {
3288c2ecf20Sopenharmony_ci		add_dr(dev, &new_dr->node);
3298c2ecf20Sopenharmony_ci		dr = new_dr;
3308c2ecf20Sopenharmony_ci		new_res = NULL;
3318c2ecf20Sopenharmony_ci	}
3328c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dev->devres_lock, flags);
3338c2ecf20Sopenharmony_ci	devres_free(new_res);
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	return dr->data;
3368c2ecf20Sopenharmony_ci}
3378c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devres_get);
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci/**
3408c2ecf20Sopenharmony_ci * devres_remove - Find a device resource and remove it
3418c2ecf20Sopenharmony_ci * @dev: Device to find resource from
3428c2ecf20Sopenharmony_ci * @release: Look for resources associated with this release function
3438c2ecf20Sopenharmony_ci * @match: Match function (optional)
3448c2ecf20Sopenharmony_ci * @match_data: Data for the match function
3458c2ecf20Sopenharmony_ci *
3468c2ecf20Sopenharmony_ci * Find the latest devres of @dev associated with @release and for
3478c2ecf20Sopenharmony_ci * which @match returns 1.  If @match is NULL, it's considered to
3488c2ecf20Sopenharmony_ci * match all.  If found, the resource is removed atomically and
3498c2ecf20Sopenharmony_ci * returned.
3508c2ecf20Sopenharmony_ci *
3518c2ecf20Sopenharmony_ci * RETURNS:
3528c2ecf20Sopenharmony_ci * Pointer to removed devres on success, NULL if not found.
3538c2ecf20Sopenharmony_ci */
3548c2ecf20Sopenharmony_civoid * devres_remove(struct device *dev, dr_release_t release,
3558c2ecf20Sopenharmony_ci		     dr_match_t match, void *match_data)
3568c2ecf20Sopenharmony_ci{
3578c2ecf20Sopenharmony_ci	struct devres *dr;
3588c2ecf20Sopenharmony_ci	unsigned long flags;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dev->devres_lock, flags);
3618c2ecf20Sopenharmony_ci	dr = find_dr(dev, release, match, match_data);
3628c2ecf20Sopenharmony_ci	if (dr) {
3638c2ecf20Sopenharmony_ci		list_del_init(&dr->node.entry);
3648c2ecf20Sopenharmony_ci		devres_log(dev, &dr->node, "REM");
3658c2ecf20Sopenharmony_ci	}
3668c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dev->devres_lock, flags);
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	if (dr)
3698c2ecf20Sopenharmony_ci		return dr->data;
3708c2ecf20Sopenharmony_ci	return NULL;
3718c2ecf20Sopenharmony_ci}
3728c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devres_remove);
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci/**
3758c2ecf20Sopenharmony_ci * devres_destroy - Find a device resource and destroy it
3768c2ecf20Sopenharmony_ci * @dev: Device to find resource from
3778c2ecf20Sopenharmony_ci * @release: Look for resources associated with this release function
3788c2ecf20Sopenharmony_ci * @match: Match function (optional)
3798c2ecf20Sopenharmony_ci * @match_data: Data for the match function
3808c2ecf20Sopenharmony_ci *
3818c2ecf20Sopenharmony_ci * Find the latest devres of @dev associated with @release and for
3828c2ecf20Sopenharmony_ci * which @match returns 1.  If @match is NULL, it's considered to
3838c2ecf20Sopenharmony_ci * match all.  If found, the resource is removed atomically and freed.
3848c2ecf20Sopenharmony_ci *
3858c2ecf20Sopenharmony_ci * Note that the release function for the resource will not be called,
3868c2ecf20Sopenharmony_ci * only the devres-allocated data will be freed.  The caller becomes
3878c2ecf20Sopenharmony_ci * responsible for freeing any other data.
3888c2ecf20Sopenharmony_ci *
3898c2ecf20Sopenharmony_ci * RETURNS:
3908c2ecf20Sopenharmony_ci * 0 if devres is found and freed, -ENOENT if not found.
3918c2ecf20Sopenharmony_ci */
3928c2ecf20Sopenharmony_ciint devres_destroy(struct device *dev, dr_release_t release,
3938c2ecf20Sopenharmony_ci		   dr_match_t match, void *match_data)
3948c2ecf20Sopenharmony_ci{
3958c2ecf20Sopenharmony_ci	void *res;
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	res = devres_remove(dev, release, match, match_data);
3988c2ecf20Sopenharmony_ci	if (unlikely(!res))
3998c2ecf20Sopenharmony_ci		return -ENOENT;
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	devres_free(res);
4028c2ecf20Sopenharmony_ci	return 0;
4038c2ecf20Sopenharmony_ci}
4048c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devres_destroy);
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci/**
4088c2ecf20Sopenharmony_ci * devres_release - Find a device resource and destroy it, calling release
4098c2ecf20Sopenharmony_ci * @dev: Device to find resource from
4108c2ecf20Sopenharmony_ci * @release: Look for resources associated with this release function
4118c2ecf20Sopenharmony_ci * @match: Match function (optional)
4128c2ecf20Sopenharmony_ci * @match_data: Data for the match function
4138c2ecf20Sopenharmony_ci *
4148c2ecf20Sopenharmony_ci * Find the latest devres of @dev associated with @release and for
4158c2ecf20Sopenharmony_ci * which @match returns 1.  If @match is NULL, it's considered to
4168c2ecf20Sopenharmony_ci * match all.  If found, the resource is removed atomically, the
4178c2ecf20Sopenharmony_ci * release function called and the resource freed.
4188c2ecf20Sopenharmony_ci *
4198c2ecf20Sopenharmony_ci * RETURNS:
4208c2ecf20Sopenharmony_ci * 0 if devres is found and freed, -ENOENT if not found.
4218c2ecf20Sopenharmony_ci */
4228c2ecf20Sopenharmony_ciint devres_release(struct device *dev, dr_release_t release,
4238c2ecf20Sopenharmony_ci		   dr_match_t match, void *match_data)
4248c2ecf20Sopenharmony_ci{
4258c2ecf20Sopenharmony_ci	void *res;
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	res = devres_remove(dev, release, match, match_data);
4288c2ecf20Sopenharmony_ci	if (unlikely(!res))
4298c2ecf20Sopenharmony_ci		return -ENOENT;
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	(*release)(dev, res);
4328c2ecf20Sopenharmony_ci	devres_free(res);
4338c2ecf20Sopenharmony_ci	return 0;
4348c2ecf20Sopenharmony_ci}
4358c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devres_release);
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_cistatic int remove_nodes(struct device *dev,
4388c2ecf20Sopenharmony_ci			struct list_head *first, struct list_head *end,
4398c2ecf20Sopenharmony_ci			struct list_head *todo)
4408c2ecf20Sopenharmony_ci{
4418c2ecf20Sopenharmony_ci	int cnt = 0, nr_groups = 0;
4428c2ecf20Sopenharmony_ci	struct list_head *cur;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	/* First pass - move normal devres entries to @todo and clear
4458c2ecf20Sopenharmony_ci	 * devres_group colors.
4468c2ecf20Sopenharmony_ci	 */
4478c2ecf20Sopenharmony_ci	cur = first;
4488c2ecf20Sopenharmony_ci	while (cur != end) {
4498c2ecf20Sopenharmony_ci		struct devres_node *node;
4508c2ecf20Sopenharmony_ci		struct devres_group *grp;
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci		node = list_entry(cur, struct devres_node, entry);
4538c2ecf20Sopenharmony_ci		cur = cur->next;
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci		grp = node_to_group(node);
4568c2ecf20Sopenharmony_ci		if (grp) {
4578c2ecf20Sopenharmony_ci			/* clear color of group markers in the first pass */
4588c2ecf20Sopenharmony_ci			grp->color = 0;
4598c2ecf20Sopenharmony_ci			nr_groups++;
4608c2ecf20Sopenharmony_ci		} else {
4618c2ecf20Sopenharmony_ci			/* regular devres entry */
4628c2ecf20Sopenharmony_ci			if (&node->entry == first)
4638c2ecf20Sopenharmony_ci				first = first->next;
4648c2ecf20Sopenharmony_ci			list_move_tail(&node->entry, todo);
4658c2ecf20Sopenharmony_ci			cnt++;
4668c2ecf20Sopenharmony_ci		}
4678c2ecf20Sopenharmony_ci	}
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	if (!nr_groups)
4708c2ecf20Sopenharmony_ci		return cnt;
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	/* Second pass - Scan groups and color them.  A group gets
4738c2ecf20Sopenharmony_ci	 * color value of two iff the group is wholly contained in
4748c2ecf20Sopenharmony_ci	 * [cur, end).  That is, for a closed group, both opening and
4758c2ecf20Sopenharmony_ci	 * closing markers should be in the range, while just the
4768c2ecf20Sopenharmony_ci	 * opening marker is enough for an open group.
4778c2ecf20Sopenharmony_ci	 */
4788c2ecf20Sopenharmony_ci	cur = first;
4798c2ecf20Sopenharmony_ci	while (cur != end) {
4808c2ecf20Sopenharmony_ci		struct devres_node *node;
4818c2ecf20Sopenharmony_ci		struct devres_group *grp;
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci		node = list_entry(cur, struct devres_node, entry);
4848c2ecf20Sopenharmony_ci		cur = cur->next;
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci		grp = node_to_group(node);
4878c2ecf20Sopenharmony_ci		BUG_ON(!grp || list_empty(&grp->node[0].entry));
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci		grp->color++;
4908c2ecf20Sopenharmony_ci		if (list_empty(&grp->node[1].entry))
4918c2ecf20Sopenharmony_ci			grp->color++;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci		BUG_ON(grp->color <= 0 || grp->color > 2);
4948c2ecf20Sopenharmony_ci		if (grp->color == 2) {
4958c2ecf20Sopenharmony_ci			/* No need to update cur or end.  The removed
4968c2ecf20Sopenharmony_ci			 * nodes are always before both.
4978c2ecf20Sopenharmony_ci			 */
4988c2ecf20Sopenharmony_ci			list_move_tail(&grp->node[0].entry, todo);
4998c2ecf20Sopenharmony_ci			list_del_init(&grp->node[1].entry);
5008c2ecf20Sopenharmony_ci		}
5018c2ecf20Sopenharmony_ci	}
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	return cnt;
5048c2ecf20Sopenharmony_ci}
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_cistatic int release_nodes(struct device *dev, struct list_head *first,
5078c2ecf20Sopenharmony_ci			 struct list_head *end, unsigned long flags)
5088c2ecf20Sopenharmony_ci	__releases(&dev->devres_lock)
5098c2ecf20Sopenharmony_ci{
5108c2ecf20Sopenharmony_ci	LIST_HEAD(todo);
5118c2ecf20Sopenharmony_ci	int cnt;
5128c2ecf20Sopenharmony_ci	struct devres *dr, *tmp;
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	cnt = remove_nodes(dev, first, end, &todo);
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dev->devres_lock, flags);
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	/* Release.  Note that both devres and devres_group are
5198c2ecf20Sopenharmony_ci	 * handled as devres in the following loop.  This is safe.
5208c2ecf20Sopenharmony_ci	 */
5218c2ecf20Sopenharmony_ci	list_for_each_entry_safe_reverse(dr, tmp, &todo, node.entry) {
5228c2ecf20Sopenharmony_ci		devres_log(dev, &dr->node, "REL");
5238c2ecf20Sopenharmony_ci		dr->node.release(dev, dr->data);
5248c2ecf20Sopenharmony_ci		kfree(dr);
5258c2ecf20Sopenharmony_ci	}
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	return cnt;
5288c2ecf20Sopenharmony_ci}
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci/**
5318c2ecf20Sopenharmony_ci * devres_release_all - Release all managed resources
5328c2ecf20Sopenharmony_ci * @dev: Device to release resources for
5338c2ecf20Sopenharmony_ci *
5348c2ecf20Sopenharmony_ci * Release all resources associated with @dev.  This function is
5358c2ecf20Sopenharmony_ci * called on driver detach.
5368c2ecf20Sopenharmony_ci */
5378c2ecf20Sopenharmony_ciint devres_release_all(struct device *dev)
5388c2ecf20Sopenharmony_ci{
5398c2ecf20Sopenharmony_ci	unsigned long flags;
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	/* Looks like an uninitialized device structure */
5428c2ecf20Sopenharmony_ci	if (WARN_ON(dev->devres_head.next == NULL))
5438c2ecf20Sopenharmony_ci		return -ENODEV;
5448c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dev->devres_lock, flags);
5458c2ecf20Sopenharmony_ci	return release_nodes(dev, dev->devres_head.next, &dev->devres_head,
5468c2ecf20Sopenharmony_ci			     flags);
5478c2ecf20Sopenharmony_ci}
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci/**
5508c2ecf20Sopenharmony_ci * devres_open_group - Open a new devres group
5518c2ecf20Sopenharmony_ci * @dev: Device to open devres group for
5528c2ecf20Sopenharmony_ci * @id: Separator ID
5538c2ecf20Sopenharmony_ci * @gfp: Allocation flags
5548c2ecf20Sopenharmony_ci *
5558c2ecf20Sopenharmony_ci * Open a new devres group for @dev with @id.  For @id, using a
5568c2ecf20Sopenharmony_ci * pointer to an object which won't be used for another group is
5578c2ecf20Sopenharmony_ci * recommended.  If @id is NULL, address-wise unique ID is created.
5588c2ecf20Sopenharmony_ci *
5598c2ecf20Sopenharmony_ci * RETURNS:
5608c2ecf20Sopenharmony_ci * ID of the new group, NULL on failure.
5618c2ecf20Sopenharmony_ci */
5628c2ecf20Sopenharmony_civoid * devres_open_group(struct device *dev, void *id, gfp_t gfp)
5638c2ecf20Sopenharmony_ci{
5648c2ecf20Sopenharmony_ci	struct devres_group *grp;
5658c2ecf20Sopenharmony_ci	unsigned long flags;
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	grp = kmalloc(sizeof(*grp), gfp);
5688c2ecf20Sopenharmony_ci	if (unlikely(!grp))
5698c2ecf20Sopenharmony_ci		return NULL;
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	grp->node[0].release = &group_open_release;
5728c2ecf20Sopenharmony_ci	grp->node[1].release = &group_close_release;
5738c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&grp->node[0].entry);
5748c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&grp->node[1].entry);
5758c2ecf20Sopenharmony_ci	set_node_dbginfo(&grp->node[0], "grp<", 0);
5768c2ecf20Sopenharmony_ci	set_node_dbginfo(&grp->node[1], "grp>", 0);
5778c2ecf20Sopenharmony_ci	grp->id = grp;
5788c2ecf20Sopenharmony_ci	if (id)
5798c2ecf20Sopenharmony_ci		grp->id = id;
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dev->devres_lock, flags);
5828c2ecf20Sopenharmony_ci	add_dr(dev, &grp->node[0]);
5838c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dev->devres_lock, flags);
5848c2ecf20Sopenharmony_ci	return grp->id;
5858c2ecf20Sopenharmony_ci}
5868c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devres_open_group);
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci/* Find devres group with ID @id.  If @id is NULL, look for the latest. */
5898c2ecf20Sopenharmony_cistatic struct devres_group * find_group(struct device *dev, void *id)
5908c2ecf20Sopenharmony_ci{
5918c2ecf20Sopenharmony_ci	struct devres_node *node;
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	list_for_each_entry_reverse(node, &dev->devres_head, entry) {
5948c2ecf20Sopenharmony_ci		struct devres_group *grp;
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci		if (node->release != &group_open_release)
5978c2ecf20Sopenharmony_ci			continue;
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci		grp = container_of(node, struct devres_group, node[0]);
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci		if (id) {
6028c2ecf20Sopenharmony_ci			if (grp->id == id)
6038c2ecf20Sopenharmony_ci				return grp;
6048c2ecf20Sopenharmony_ci		} else if (list_empty(&grp->node[1].entry))
6058c2ecf20Sopenharmony_ci			return grp;
6068c2ecf20Sopenharmony_ci	}
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	return NULL;
6098c2ecf20Sopenharmony_ci}
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci/**
6128c2ecf20Sopenharmony_ci * devres_close_group - Close a devres group
6138c2ecf20Sopenharmony_ci * @dev: Device to close devres group for
6148c2ecf20Sopenharmony_ci * @id: ID of target group, can be NULL
6158c2ecf20Sopenharmony_ci *
6168c2ecf20Sopenharmony_ci * Close the group identified by @id.  If @id is NULL, the latest open
6178c2ecf20Sopenharmony_ci * group is selected.
6188c2ecf20Sopenharmony_ci */
6198c2ecf20Sopenharmony_civoid devres_close_group(struct device *dev, void *id)
6208c2ecf20Sopenharmony_ci{
6218c2ecf20Sopenharmony_ci	struct devres_group *grp;
6228c2ecf20Sopenharmony_ci	unsigned long flags;
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dev->devres_lock, flags);
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	grp = find_group(dev, id);
6278c2ecf20Sopenharmony_ci	if (grp)
6288c2ecf20Sopenharmony_ci		add_dr(dev, &grp->node[1]);
6298c2ecf20Sopenharmony_ci	else
6308c2ecf20Sopenharmony_ci		WARN_ON(1);
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dev->devres_lock, flags);
6338c2ecf20Sopenharmony_ci}
6348c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devres_close_group);
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci/**
6378c2ecf20Sopenharmony_ci * devres_remove_group - Remove a devres group
6388c2ecf20Sopenharmony_ci * @dev: Device to remove group for
6398c2ecf20Sopenharmony_ci * @id: ID of target group, can be NULL
6408c2ecf20Sopenharmony_ci *
6418c2ecf20Sopenharmony_ci * Remove the group identified by @id.  If @id is NULL, the latest
6428c2ecf20Sopenharmony_ci * open group is selected.  Note that removing a group doesn't affect
6438c2ecf20Sopenharmony_ci * any other resources.
6448c2ecf20Sopenharmony_ci */
6458c2ecf20Sopenharmony_civoid devres_remove_group(struct device *dev, void *id)
6468c2ecf20Sopenharmony_ci{
6478c2ecf20Sopenharmony_ci	struct devres_group *grp;
6488c2ecf20Sopenharmony_ci	unsigned long flags;
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dev->devres_lock, flags);
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	grp = find_group(dev, id);
6538c2ecf20Sopenharmony_ci	if (grp) {
6548c2ecf20Sopenharmony_ci		list_del_init(&grp->node[0].entry);
6558c2ecf20Sopenharmony_ci		list_del_init(&grp->node[1].entry);
6568c2ecf20Sopenharmony_ci		devres_log(dev, &grp->node[0], "REM");
6578c2ecf20Sopenharmony_ci	} else
6588c2ecf20Sopenharmony_ci		WARN_ON(1);
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dev->devres_lock, flags);
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	kfree(grp);
6638c2ecf20Sopenharmony_ci}
6648c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devres_remove_group);
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci/**
6678c2ecf20Sopenharmony_ci * devres_release_group - Release resources in a devres group
6688c2ecf20Sopenharmony_ci * @dev: Device to release group for
6698c2ecf20Sopenharmony_ci * @id: ID of target group, can be NULL
6708c2ecf20Sopenharmony_ci *
6718c2ecf20Sopenharmony_ci * Release all resources in the group identified by @id.  If @id is
6728c2ecf20Sopenharmony_ci * NULL, the latest open group is selected.  The selected group and
6738c2ecf20Sopenharmony_ci * groups properly nested inside the selected group are removed.
6748c2ecf20Sopenharmony_ci *
6758c2ecf20Sopenharmony_ci * RETURNS:
6768c2ecf20Sopenharmony_ci * The number of released non-group resources.
6778c2ecf20Sopenharmony_ci */
6788c2ecf20Sopenharmony_ciint devres_release_group(struct device *dev, void *id)
6798c2ecf20Sopenharmony_ci{
6808c2ecf20Sopenharmony_ci	struct devres_group *grp;
6818c2ecf20Sopenharmony_ci	unsigned long flags;
6828c2ecf20Sopenharmony_ci	int cnt = 0;
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dev->devres_lock, flags);
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	grp = find_group(dev, id);
6878c2ecf20Sopenharmony_ci	if (grp) {
6888c2ecf20Sopenharmony_ci		struct list_head *first = &grp->node[0].entry;
6898c2ecf20Sopenharmony_ci		struct list_head *end = &dev->devres_head;
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci		if (!list_empty(&grp->node[1].entry))
6928c2ecf20Sopenharmony_ci			end = grp->node[1].entry.next;
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci		cnt = release_nodes(dev, first, end, flags);
6958c2ecf20Sopenharmony_ci	} else {
6968c2ecf20Sopenharmony_ci		WARN_ON(1);
6978c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&dev->devres_lock, flags);
6988c2ecf20Sopenharmony_ci	}
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	return cnt;
7018c2ecf20Sopenharmony_ci}
7028c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devres_release_group);
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci/*
7058c2ecf20Sopenharmony_ci * Custom devres actions allow inserting a simple function call
7068c2ecf20Sopenharmony_ci * into the teadown sequence.
7078c2ecf20Sopenharmony_ci */
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_cistruct action_devres {
7108c2ecf20Sopenharmony_ci	void *data;
7118c2ecf20Sopenharmony_ci	void (*action)(void *);
7128c2ecf20Sopenharmony_ci};
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_cistatic int devm_action_match(struct device *dev, void *res, void *p)
7158c2ecf20Sopenharmony_ci{
7168c2ecf20Sopenharmony_ci	struct action_devres *devres = res;
7178c2ecf20Sopenharmony_ci	struct action_devres *target = p;
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	return devres->action == target->action &&
7208c2ecf20Sopenharmony_ci	       devres->data == target->data;
7218c2ecf20Sopenharmony_ci}
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_cistatic void devm_action_release(struct device *dev, void *res)
7248c2ecf20Sopenharmony_ci{
7258c2ecf20Sopenharmony_ci	struct action_devres *devres = res;
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	devres->action(devres->data);
7288c2ecf20Sopenharmony_ci}
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci/**
7318c2ecf20Sopenharmony_ci * devm_add_action() - add a custom action to list of managed resources
7328c2ecf20Sopenharmony_ci * @dev: Device that owns the action
7338c2ecf20Sopenharmony_ci * @action: Function that should be called
7348c2ecf20Sopenharmony_ci * @data: Pointer to data passed to @action implementation
7358c2ecf20Sopenharmony_ci *
7368c2ecf20Sopenharmony_ci * This adds a custom action to the list of managed resources so that
7378c2ecf20Sopenharmony_ci * it gets executed as part of standard resource unwinding.
7388c2ecf20Sopenharmony_ci */
7398c2ecf20Sopenharmony_ciint devm_add_action(struct device *dev, void (*action)(void *), void *data)
7408c2ecf20Sopenharmony_ci{
7418c2ecf20Sopenharmony_ci	struct action_devres *devres;
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	devres = devres_alloc(devm_action_release,
7448c2ecf20Sopenharmony_ci			      sizeof(struct action_devres), GFP_KERNEL);
7458c2ecf20Sopenharmony_ci	if (!devres)
7468c2ecf20Sopenharmony_ci		return -ENOMEM;
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci	devres->data = data;
7498c2ecf20Sopenharmony_ci	devres->action = action;
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci	devres_add(dev, devres);
7528c2ecf20Sopenharmony_ci	return 0;
7538c2ecf20Sopenharmony_ci}
7548c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devm_add_action);
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci/**
7578c2ecf20Sopenharmony_ci * devm_remove_action() - removes previously added custom action
7588c2ecf20Sopenharmony_ci * @dev: Device that owns the action
7598c2ecf20Sopenharmony_ci * @action: Function implementing the action
7608c2ecf20Sopenharmony_ci * @data: Pointer to data passed to @action implementation
7618c2ecf20Sopenharmony_ci *
7628c2ecf20Sopenharmony_ci * Removes instance of @action previously added by devm_add_action().
7638c2ecf20Sopenharmony_ci * Both action and data should match one of the existing entries.
7648c2ecf20Sopenharmony_ci */
7658c2ecf20Sopenharmony_civoid devm_remove_action(struct device *dev, void (*action)(void *), void *data)
7668c2ecf20Sopenharmony_ci{
7678c2ecf20Sopenharmony_ci	struct action_devres devres = {
7688c2ecf20Sopenharmony_ci		.data = data,
7698c2ecf20Sopenharmony_ci		.action = action,
7708c2ecf20Sopenharmony_ci	};
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci	WARN_ON(devres_destroy(dev, devm_action_release, devm_action_match,
7738c2ecf20Sopenharmony_ci			       &devres));
7748c2ecf20Sopenharmony_ci}
7758c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devm_remove_action);
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci/**
7788c2ecf20Sopenharmony_ci * devm_release_action() - release previously added custom action
7798c2ecf20Sopenharmony_ci * @dev: Device that owns the action
7808c2ecf20Sopenharmony_ci * @action: Function implementing the action
7818c2ecf20Sopenharmony_ci * @data: Pointer to data passed to @action implementation
7828c2ecf20Sopenharmony_ci *
7838c2ecf20Sopenharmony_ci * Releases and removes instance of @action previously added by
7848c2ecf20Sopenharmony_ci * devm_add_action().  Both action and data should match one of the
7858c2ecf20Sopenharmony_ci * existing entries.
7868c2ecf20Sopenharmony_ci */
7878c2ecf20Sopenharmony_civoid devm_release_action(struct device *dev, void (*action)(void *), void *data)
7888c2ecf20Sopenharmony_ci{
7898c2ecf20Sopenharmony_ci	struct action_devres devres = {
7908c2ecf20Sopenharmony_ci		.data = data,
7918c2ecf20Sopenharmony_ci		.action = action,
7928c2ecf20Sopenharmony_ci	};
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	WARN_ON(devres_release(dev, devm_action_release, devm_action_match,
7958c2ecf20Sopenharmony_ci			       &devres));
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci}
7988c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devm_release_action);
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci/*
8018c2ecf20Sopenharmony_ci * Managed kmalloc/kfree
8028c2ecf20Sopenharmony_ci */
8038c2ecf20Sopenharmony_cistatic void devm_kmalloc_release(struct device *dev, void *res)
8048c2ecf20Sopenharmony_ci{
8058c2ecf20Sopenharmony_ci	/* noop */
8068c2ecf20Sopenharmony_ci}
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_cistatic int devm_kmalloc_match(struct device *dev, void *res, void *data)
8098c2ecf20Sopenharmony_ci{
8108c2ecf20Sopenharmony_ci	return res == data;
8118c2ecf20Sopenharmony_ci}
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci/**
8148c2ecf20Sopenharmony_ci * devm_kmalloc - Resource-managed kmalloc
8158c2ecf20Sopenharmony_ci * @dev: Device to allocate memory for
8168c2ecf20Sopenharmony_ci * @size: Allocation size
8178c2ecf20Sopenharmony_ci * @gfp: Allocation gfp flags
8188c2ecf20Sopenharmony_ci *
8198c2ecf20Sopenharmony_ci * Managed kmalloc.  Memory allocated with this function is
8208c2ecf20Sopenharmony_ci * automatically freed on driver detach.  Like all other devres
8218c2ecf20Sopenharmony_ci * resources, guaranteed alignment is unsigned long long.
8228c2ecf20Sopenharmony_ci *
8238c2ecf20Sopenharmony_ci * RETURNS:
8248c2ecf20Sopenharmony_ci * Pointer to allocated memory on success, NULL on failure.
8258c2ecf20Sopenharmony_ci */
8268c2ecf20Sopenharmony_civoid *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp)
8278c2ecf20Sopenharmony_ci{
8288c2ecf20Sopenharmony_ci	struct devres *dr;
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	if (unlikely(!size))
8318c2ecf20Sopenharmony_ci		return ZERO_SIZE_PTR;
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	/* use raw alloc_dr for kmalloc caller tracing */
8348c2ecf20Sopenharmony_ci	dr = alloc_dr(devm_kmalloc_release, size, gfp, dev_to_node(dev));
8358c2ecf20Sopenharmony_ci	if (unlikely(!dr))
8368c2ecf20Sopenharmony_ci		return NULL;
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci	/*
8398c2ecf20Sopenharmony_ci	 * This is named devm_kzalloc_release for historical reasons
8408c2ecf20Sopenharmony_ci	 * The initial implementation did not support kmalloc, only kzalloc
8418c2ecf20Sopenharmony_ci	 */
8428c2ecf20Sopenharmony_ci	set_node_dbginfo(&dr->node, "devm_kzalloc_release", size);
8438c2ecf20Sopenharmony_ci	devres_add(dev, dr->data);
8448c2ecf20Sopenharmony_ci	return dr->data;
8458c2ecf20Sopenharmony_ci}
8468c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devm_kmalloc);
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci/**
8498c2ecf20Sopenharmony_ci * devm_krealloc - Resource-managed krealloc()
8508c2ecf20Sopenharmony_ci * @dev: Device to re-allocate memory for
8518c2ecf20Sopenharmony_ci * @ptr: Pointer to the memory chunk to re-allocate
8528c2ecf20Sopenharmony_ci * @new_size: New allocation size
8538c2ecf20Sopenharmony_ci * @gfp: Allocation gfp flags
8548c2ecf20Sopenharmony_ci *
8558c2ecf20Sopenharmony_ci * Managed krealloc(). Resizes the memory chunk allocated with devm_kmalloc().
8568c2ecf20Sopenharmony_ci * Behaves similarly to regular krealloc(): if @ptr is NULL or ZERO_SIZE_PTR,
8578c2ecf20Sopenharmony_ci * it's the equivalent of devm_kmalloc(). If new_size is zero, it frees the
8588c2ecf20Sopenharmony_ci * previously allocated memory and returns ZERO_SIZE_PTR. This function doesn't
8598c2ecf20Sopenharmony_ci * change the order in which the release callback for the re-alloc'ed devres
8608c2ecf20Sopenharmony_ci * will be called (except when falling back to devm_kmalloc() or when freeing
8618c2ecf20Sopenharmony_ci * resources when new_size is zero). The contents of the memory are preserved
8628c2ecf20Sopenharmony_ci * up to the lesser of new and old sizes.
8638c2ecf20Sopenharmony_ci */
8648c2ecf20Sopenharmony_civoid *devm_krealloc(struct device *dev, void *ptr, size_t new_size, gfp_t gfp)
8658c2ecf20Sopenharmony_ci{
8668c2ecf20Sopenharmony_ci	size_t total_new_size, total_old_size;
8678c2ecf20Sopenharmony_ci	struct devres *old_dr, *new_dr;
8688c2ecf20Sopenharmony_ci	unsigned long flags;
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_ci	if (unlikely(!new_size)) {
8718c2ecf20Sopenharmony_ci		devm_kfree(dev, ptr);
8728c2ecf20Sopenharmony_ci		return ZERO_SIZE_PTR;
8738c2ecf20Sopenharmony_ci	}
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_ci	if (unlikely(ZERO_OR_NULL_PTR(ptr)))
8768c2ecf20Sopenharmony_ci		return devm_kmalloc(dev, new_size, gfp);
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci	if (WARN_ON(is_kernel_rodata((unsigned long)ptr)))
8798c2ecf20Sopenharmony_ci		/*
8808c2ecf20Sopenharmony_ci		 * We cannot reliably realloc a const string returned by
8818c2ecf20Sopenharmony_ci		 * devm_kstrdup_const().
8828c2ecf20Sopenharmony_ci		 */
8838c2ecf20Sopenharmony_ci		return NULL;
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	if (!check_dr_size(new_size, &total_new_size))
8868c2ecf20Sopenharmony_ci		return NULL;
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci	total_old_size = ksize(container_of(ptr, struct devres, data));
8898c2ecf20Sopenharmony_ci	if (total_old_size == 0) {
8908c2ecf20Sopenharmony_ci		WARN(1, "Pointer doesn't point to dynamically allocated memory.");
8918c2ecf20Sopenharmony_ci		return NULL;
8928c2ecf20Sopenharmony_ci	}
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci	/*
8958c2ecf20Sopenharmony_ci	 * If new size is smaller or equal to the actual number of bytes
8968c2ecf20Sopenharmony_ci	 * allocated previously - just return the same pointer.
8978c2ecf20Sopenharmony_ci	 */
8988c2ecf20Sopenharmony_ci	if (total_new_size <= total_old_size)
8998c2ecf20Sopenharmony_ci		return ptr;
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci	/*
9028c2ecf20Sopenharmony_ci	 * Otherwise: allocate new, larger chunk. We need to allocate before
9038c2ecf20Sopenharmony_ci	 * taking the lock as most probably the caller uses GFP_KERNEL.
9048c2ecf20Sopenharmony_ci	 */
9058c2ecf20Sopenharmony_ci	new_dr = alloc_dr(devm_kmalloc_release,
9068c2ecf20Sopenharmony_ci			  total_new_size, gfp, dev_to_node(dev));
9078c2ecf20Sopenharmony_ci	if (!new_dr)
9088c2ecf20Sopenharmony_ci		return NULL;
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_ci	/*
9118c2ecf20Sopenharmony_ci	 * The spinlock protects the linked list against concurrent
9128c2ecf20Sopenharmony_ci	 * modifications but not the resource itself.
9138c2ecf20Sopenharmony_ci	 */
9148c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dev->devres_lock, flags);
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_ci	old_dr = find_dr(dev, devm_kmalloc_release, devm_kmalloc_match, ptr);
9178c2ecf20Sopenharmony_ci	if (!old_dr) {
9188c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&dev->devres_lock, flags);
9198c2ecf20Sopenharmony_ci		kfree(new_dr);
9208c2ecf20Sopenharmony_ci		WARN(1, "Memory chunk not managed or managed by a different device.");
9218c2ecf20Sopenharmony_ci		return NULL;
9228c2ecf20Sopenharmony_ci	}
9238c2ecf20Sopenharmony_ci
9248c2ecf20Sopenharmony_ci	replace_dr(dev, &old_dr->node, &new_dr->node);
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dev->devres_lock, flags);
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci	/*
9298c2ecf20Sopenharmony_ci	 * We can copy the memory contents after releasing the lock as we're
9308c2ecf20Sopenharmony_ci	 * no longer modyfing the list links.
9318c2ecf20Sopenharmony_ci	 */
9328c2ecf20Sopenharmony_ci	memcpy(new_dr->data, old_dr->data,
9338c2ecf20Sopenharmony_ci	       total_old_size - offsetof(struct devres, data));
9348c2ecf20Sopenharmony_ci	/*
9358c2ecf20Sopenharmony_ci	 * Same for releasing the old devres - it's now been removed from the
9368c2ecf20Sopenharmony_ci	 * list. This is also the reason why we must not use devm_kfree() - the
9378c2ecf20Sopenharmony_ci	 * links are no longer valid.
9388c2ecf20Sopenharmony_ci	 */
9398c2ecf20Sopenharmony_ci	kfree(old_dr);
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_ci	return new_dr->data;
9428c2ecf20Sopenharmony_ci}
9438c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devm_krealloc);
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci/**
9468c2ecf20Sopenharmony_ci * devm_kstrdup - Allocate resource managed space and
9478c2ecf20Sopenharmony_ci *                copy an existing string into that.
9488c2ecf20Sopenharmony_ci * @dev: Device to allocate memory for
9498c2ecf20Sopenharmony_ci * @s: the string to duplicate
9508c2ecf20Sopenharmony_ci * @gfp: the GFP mask used in the devm_kmalloc() call when
9518c2ecf20Sopenharmony_ci *       allocating memory
9528c2ecf20Sopenharmony_ci * RETURNS:
9538c2ecf20Sopenharmony_ci * Pointer to allocated string on success, NULL on failure.
9548c2ecf20Sopenharmony_ci */
9558c2ecf20Sopenharmony_cichar *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp)
9568c2ecf20Sopenharmony_ci{
9578c2ecf20Sopenharmony_ci	size_t size;
9588c2ecf20Sopenharmony_ci	char *buf;
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ci	if (!s)
9618c2ecf20Sopenharmony_ci		return NULL;
9628c2ecf20Sopenharmony_ci
9638c2ecf20Sopenharmony_ci	size = strlen(s) + 1;
9648c2ecf20Sopenharmony_ci	buf = devm_kmalloc(dev, size, gfp);
9658c2ecf20Sopenharmony_ci	if (buf)
9668c2ecf20Sopenharmony_ci		memcpy(buf, s, size);
9678c2ecf20Sopenharmony_ci	return buf;
9688c2ecf20Sopenharmony_ci}
9698c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devm_kstrdup);
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_ci/**
9728c2ecf20Sopenharmony_ci * devm_kstrdup_const - resource managed conditional string duplication
9738c2ecf20Sopenharmony_ci * @dev: device for which to duplicate the string
9748c2ecf20Sopenharmony_ci * @s: the string to duplicate
9758c2ecf20Sopenharmony_ci * @gfp: the GFP mask used in the kmalloc() call when allocating memory
9768c2ecf20Sopenharmony_ci *
9778c2ecf20Sopenharmony_ci * Strings allocated by devm_kstrdup_const will be automatically freed when
9788c2ecf20Sopenharmony_ci * the associated device is detached.
9798c2ecf20Sopenharmony_ci *
9808c2ecf20Sopenharmony_ci * RETURNS:
9818c2ecf20Sopenharmony_ci * Source string if it is in .rodata section otherwise it falls back to
9828c2ecf20Sopenharmony_ci * devm_kstrdup.
9838c2ecf20Sopenharmony_ci */
9848c2ecf20Sopenharmony_ciconst char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp)
9858c2ecf20Sopenharmony_ci{
9868c2ecf20Sopenharmony_ci	if (is_kernel_rodata((unsigned long)s))
9878c2ecf20Sopenharmony_ci		return s;
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_ci	return devm_kstrdup(dev, s, gfp);
9908c2ecf20Sopenharmony_ci}
9918c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devm_kstrdup_const);
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_ci/**
9948c2ecf20Sopenharmony_ci * devm_kvasprintf - Allocate resource managed space and format a string
9958c2ecf20Sopenharmony_ci *		     into that.
9968c2ecf20Sopenharmony_ci * @dev: Device to allocate memory for
9978c2ecf20Sopenharmony_ci * @gfp: the GFP mask used in the devm_kmalloc() call when
9988c2ecf20Sopenharmony_ci *       allocating memory
9998c2ecf20Sopenharmony_ci * @fmt: The printf()-style format string
10008c2ecf20Sopenharmony_ci * @ap: Arguments for the format string
10018c2ecf20Sopenharmony_ci * RETURNS:
10028c2ecf20Sopenharmony_ci * Pointer to allocated string on success, NULL on failure.
10038c2ecf20Sopenharmony_ci */
10048c2ecf20Sopenharmony_cichar *devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt,
10058c2ecf20Sopenharmony_ci		      va_list ap)
10068c2ecf20Sopenharmony_ci{
10078c2ecf20Sopenharmony_ci	unsigned int len;
10088c2ecf20Sopenharmony_ci	char *p;
10098c2ecf20Sopenharmony_ci	va_list aq;
10108c2ecf20Sopenharmony_ci
10118c2ecf20Sopenharmony_ci	va_copy(aq, ap);
10128c2ecf20Sopenharmony_ci	len = vsnprintf(NULL, 0, fmt, aq);
10138c2ecf20Sopenharmony_ci	va_end(aq);
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_ci	p = devm_kmalloc(dev, len+1, gfp);
10168c2ecf20Sopenharmony_ci	if (!p)
10178c2ecf20Sopenharmony_ci		return NULL;
10188c2ecf20Sopenharmony_ci
10198c2ecf20Sopenharmony_ci	vsnprintf(p, len+1, fmt, ap);
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci	return p;
10228c2ecf20Sopenharmony_ci}
10238c2ecf20Sopenharmony_ciEXPORT_SYMBOL(devm_kvasprintf);
10248c2ecf20Sopenharmony_ci
10258c2ecf20Sopenharmony_ci/**
10268c2ecf20Sopenharmony_ci * devm_kasprintf - Allocate resource managed space and format a string
10278c2ecf20Sopenharmony_ci *		    into that.
10288c2ecf20Sopenharmony_ci * @dev: Device to allocate memory for
10298c2ecf20Sopenharmony_ci * @gfp: the GFP mask used in the devm_kmalloc() call when
10308c2ecf20Sopenharmony_ci *       allocating memory
10318c2ecf20Sopenharmony_ci * @fmt: The printf()-style format string
10328c2ecf20Sopenharmony_ci * @...: Arguments for the format string
10338c2ecf20Sopenharmony_ci * RETURNS:
10348c2ecf20Sopenharmony_ci * Pointer to allocated string on success, NULL on failure.
10358c2ecf20Sopenharmony_ci */
10368c2ecf20Sopenharmony_cichar *devm_kasprintf(struct device *dev, gfp_t gfp, const char *fmt, ...)
10378c2ecf20Sopenharmony_ci{
10388c2ecf20Sopenharmony_ci	va_list ap;
10398c2ecf20Sopenharmony_ci	char *p;
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_ci	va_start(ap, fmt);
10428c2ecf20Sopenharmony_ci	p = devm_kvasprintf(dev, gfp, fmt, ap);
10438c2ecf20Sopenharmony_ci	va_end(ap);
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_ci	return p;
10468c2ecf20Sopenharmony_ci}
10478c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devm_kasprintf);
10488c2ecf20Sopenharmony_ci
10498c2ecf20Sopenharmony_ci/**
10508c2ecf20Sopenharmony_ci * devm_kfree - Resource-managed kfree
10518c2ecf20Sopenharmony_ci * @dev: Device this memory belongs to
10528c2ecf20Sopenharmony_ci * @p: Memory to free
10538c2ecf20Sopenharmony_ci *
10548c2ecf20Sopenharmony_ci * Free memory allocated with devm_kmalloc().
10558c2ecf20Sopenharmony_ci */
10568c2ecf20Sopenharmony_civoid devm_kfree(struct device *dev, const void *p)
10578c2ecf20Sopenharmony_ci{
10588c2ecf20Sopenharmony_ci	int rc;
10598c2ecf20Sopenharmony_ci
10608c2ecf20Sopenharmony_ci	/*
10618c2ecf20Sopenharmony_ci	 * Special cases: pointer to a string in .rodata returned by
10628c2ecf20Sopenharmony_ci	 * devm_kstrdup_const() or NULL/ZERO ptr.
10638c2ecf20Sopenharmony_ci	 */
10648c2ecf20Sopenharmony_ci	if (unlikely(is_kernel_rodata((unsigned long)p) || ZERO_OR_NULL_PTR(p)))
10658c2ecf20Sopenharmony_ci		return;
10668c2ecf20Sopenharmony_ci
10678c2ecf20Sopenharmony_ci	rc = devres_destroy(dev, devm_kmalloc_release,
10688c2ecf20Sopenharmony_ci			    devm_kmalloc_match, (void *)p);
10698c2ecf20Sopenharmony_ci	WARN_ON(rc);
10708c2ecf20Sopenharmony_ci}
10718c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devm_kfree);
10728c2ecf20Sopenharmony_ci
10738c2ecf20Sopenharmony_ci/**
10748c2ecf20Sopenharmony_ci * devm_kmemdup - Resource-managed kmemdup
10758c2ecf20Sopenharmony_ci * @dev: Device this memory belongs to
10768c2ecf20Sopenharmony_ci * @src: Memory region to duplicate
10778c2ecf20Sopenharmony_ci * @len: Memory region length
10788c2ecf20Sopenharmony_ci * @gfp: GFP mask to use
10798c2ecf20Sopenharmony_ci *
10808c2ecf20Sopenharmony_ci * Duplicate region of a memory using resource managed kmalloc
10818c2ecf20Sopenharmony_ci */
10828c2ecf20Sopenharmony_civoid *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp)
10838c2ecf20Sopenharmony_ci{
10848c2ecf20Sopenharmony_ci	void *p;
10858c2ecf20Sopenharmony_ci
10868c2ecf20Sopenharmony_ci	p = devm_kmalloc(dev, len, gfp);
10878c2ecf20Sopenharmony_ci	if (p)
10888c2ecf20Sopenharmony_ci		memcpy(p, src, len);
10898c2ecf20Sopenharmony_ci
10908c2ecf20Sopenharmony_ci	return p;
10918c2ecf20Sopenharmony_ci}
10928c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devm_kmemdup);
10938c2ecf20Sopenharmony_ci
10948c2ecf20Sopenharmony_cistruct pages_devres {
10958c2ecf20Sopenharmony_ci	unsigned long addr;
10968c2ecf20Sopenharmony_ci	unsigned int order;
10978c2ecf20Sopenharmony_ci};
10988c2ecf20Sopenharmony_ci
10998c2ecf20Sopenharmony_cistatic int devm_pages_match(struct device *dev, void *res, void *p)
11008c2ecf20Sopenharmony_ci{
11018c2ecf20Sopenharmony_ci	struct pages_devres *devres = res;
11028c2ecf20Sopenharmony_ci	struct pages_devres *target = p;
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_ci	return devres->addr == target->addr;
11058c2ecf20Sopenharmony_ci}
11068c2ecf20Sopenharmony_ci
11078c2ecf20Sopenharmony_cistatic void devm_pages_release(struct device *dev, void *res)
11088c2ecf20Sopenharmony_ci{
11098c2ecf20Sopenharmony_ci	struct pages_devres *devres = res;
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_ci	free_pages(devres->addr, devres->order);
11128c2ecf20Sopenharmony_ci}
11138c2ecf20Sopenharmony_ci
11148c2ecf20Sopenharmony_ci/**
11158c2ecf20Sopenharmony_ci * devm_get_free_pages - Resource-managed __get_free_pages
11168c2ecf20Sopenharmony_ci * @dev: Device to allocate memory for
11178c2ecf20Sopenharmony_ci * @gfp_mask: Allocation gfp flags
11188c2ecf20Sopenharmony_ci * @order: Allocation size is (1 << order) pages
11198c2ecf20Sopenharmony_ci *
11208c2ecf20Sopenharmony_ci * Managed get_free_pages.  Memory allocated with this function is
11218c2ecf20Sopenharmony_ci * automatically freed on driver detach.
11228c2ecf20Sopenharmony_ci *
11238c2ecf20Sopenharmony_ci * RETURNS:
11248c2ecf20Sopenharmony_ci * Address of allocated memory on success, 0 on failure.
11258c2ecf20Sopenharmony_ci */
11268c2ecf20Sopenharmony_ci
11278c2ecf20Sopenharmony_ciunsigned long devm_get_free_pages(struct device *dev,
11288c2ecf20Sopenharmony_ci				  gfp_t gfp_mask, unsigned int order)
11298c2ecf20Sopenharmony_ci{
11308c2ecf20Sopenharmony_ci	struct pages_devres *devres;
11318c2ecf20Sopenharmony_ci	unsigned long addr;
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci	addr = __get_free_pages(gfp_mask, order);
11348c2ecf20Sopenharmony_ci
11358c2ecf20Sopenharmony_ci	if (unlikely(!addr))
11368c2ecf20Sopenharmony_ci		return 0;
11378c2ecf20Sopenharmony_ci
11388c2ecf20Sopenharmony_ci	devres = devres_alloc(devm_pages_release,
11398c2ecf20Sopenharmony_ci			      sizeof(struct pages_devres), GFP_KERNEL);
11408c2ecf20Sopenharmony_ci	if (unlikely(!devres)) {
11418c2ecf20Sopenharmony_ci		free_pages(addr, order);
11428c2ecf20Sopenharmony_ci		return 0;
11438c2ecf20Sopenharmony_ci	}
11448c2ecf20Sopenharmony_ci
11458c2ecf20Sopenharmony_ci	devres->addr = addr;
11468c2ecf20Sopenharmony_ci	devres->order = order;
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_ci	devres_add(dev, devres);
11498c2ecf20Sopenharmony_ci	return addr;
11508c2ecf20Sopenharmony_ci}
11518c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devm_get_free_pages);
11528c2ecf20Sopenharmony_ci
11538c2ecf20Sopenharmony_ci/**
11548c2ecf20Sopenharmony_ci * devm_free_pages - Resource-managed free_pages
11558c2ecf20Sopenharmony_ci * @dev: Device this memory belongs to
11568c2ecf20Sopenharmony_ci * @addr: Memory to free
11578c2ecf20Sopenharmony_ci *
11588c2ecf20Sopenharmony_ci * Free memory allocated with devm_get_free_pages(). Unlike free_pages,
11598c2ecf20Sopenharmony_ci * there is no need to supply the @order.
11608c2ecf20Sopenharmony_ci */
11618c2ecf20Sopenharmony_civoid devm_free_pages(struct device *dev, unsigned long addr)
11628c2ecf20Sopenharmony_ci{
11638c2ecf20Sopenharmony_ci	struct pages_devres devres = { .addr = addr };
11648c2ecf20Sopenharmony_ci
11658c2ecf20Sopenharmony_ci	WARN_ON(devres_release(dev, devm_pages_release, devm_pages_match,
11668c2ecf20Sopenharmony_ci			       &devres));
11678c2ecf20Sopenharmony_ci}
11688c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devm_free_pages);
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_cistatic void devm_percpu_release(struct device *dev, void *pdata)
11718c2ecf20Sopenharmony_ci{
11728c2ecf20Sopenharmony_ci	void __percpu *p;
11738c2ecf20Sopenharmony_ci
11748c2ecf20Sopenharmony_ci	p = *(void __percpu **)pdata;
11758c2ecf20Sopenharmony_ci	free_percpu(p);
11768c2ecf20Sopenharmony_ci}
11778c2ecf20Sopenharmony_ci
11788c2ecf20Sopenharmony_cistatic int devm_percpu_match(struct device *dev, void *data, void *p)
11798c2ecf20Sopenharmony_ci{
11808c2ecf20Sopenharmony_ci	struct devres *devr = container_of(data, struct devres, data);
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_ci	return *(void **)devr->data == p;
11838c2ecf20Sopenharmony_ci}
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_ci/**
11868c2ecf20Sopenharmony_ci * __devm_alloc_percpu - Resource-managed alloc_percpu
11878c2ecf20Sopenharmony_ci * @dev: Device to allocate per-cpu memory for
11888c2ecf20Sopenharmony_ci * @size: Size of per-cpu memory to allocate
11898c2ecf20Sopenharmony_ci * @align: Alignment of per-cpu memory to allocate
11908c2ecf20Sopenharmony_ci *
11918c2ecf20Sopenharmony_ci * Managed alloc_percpu. Per-cpu memory allocated with this function is
11928c2ecf20Sopenharmony_ci * automatically freed on driver detach.
11938c2ecf20Sopenharmony_ci *
11948c2ecf20Sopenharmony_ci * RETURNS:
11958c2ecf20Sopenharmony_ci * Pointer to allocated memory on success, NULL on failure.
11968c2ecf20Sopenharmony_ci */
11978c2ecf20Sopenharmony_civoid __percpu *__devm_alloc_percpu(struct device *dev, size_t size,
11988c2ecf20Sopenharmony_ci		size_t align)
11998c2ecf20Sopenharmony_ci{
12008c2ecf20Sopenharmony_ci	void *p;
12018c2ecf20Sopenharmony_ci	void __percpu *pcpu;
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_ci	pcpu = __alloc_percpu(size, align);
12048c2ecf20Sopenharmony_ci	if (!pcpu)
12058c2ecf20Sopenharmony_ci		return NULL;
12068c2ecf20Sopenharmony_ci
12078c2ecf20Sopenharmony_ci	p = devres_alloc(devm_percpu_release, sizeof(void *), GFP_KERNEL);
12088c2ecf20Sopenharmony_ci	if (!p) {
12098c2ecf20Sopenharmony_ci		free_percpu(pcpu);
12108c2ecf20Sopenharmony_ci		return NULL;
12118c2ecf20Sopenharmony_ci	}
12128c2ecf20Sopenharmony_ci
12138c2ecf20Sopenharmony_ci	*(void __percpu **)p = pcpu;
12148c2ecf20Sopenharmony_ci
12158c2ecf20Sopenharmony_ci	devres_add(dev, p);
12168c2ecf20Sopenharmony_ci
12178c2ecf20Sopenharmony_ci	return pcpu;
12188c2ecf20Sopenharmony_ci}
12198c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(__devm_alloc_percpu);
12208c2ecf20Sopenharmony_ci
12218c2ecf20Sopenharmony_ci/**
12228c2ecf20Sopenharmony_ci * devm_free_percpu - Resource-managed free_percpu
12238c2ecf20Sopenharmony_ci * @dev: Device this memory belongs to
12248c2ecf20Sopenharmony_ci * @pdata: Per-cpu memory to free
12258c2ecf20Sopenharmony_ci *
12268c2ecf20Sopenharmony_ci * Free memory allocated with devm_alloc_percpu().
12278c2ecf20Sopenharmony_ci */
12288c2ecf20Sopenharmony_civoid devm_free_percpu(struct device *dev, void __percpu *pdata)
12298c2ecf20Sopenharmony_ci{
12308c2ecf20Sopenharmony_ci	/*
12318c2ecf20Sopenharmony_ci	 * Use devres_release() to prevent memory leakage as
12328c2ecf20Sopenharmony_ci	 * devm_free_pages() does.
12338c2ecf20Sopenharmony_ci	 */
12348c2ecf20Sopenharmony_ci	WARN_ON(devres_release(dev, devm_percpu_release, devm_percpu_match,
12358c2ecf20Sopenharmony_ci			       (__force void *)pdata));
12368c2ecf20Sopenharmony_ci}
12378c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devm_free_percpu);
1238