1c6603c74SDaniel Vetter // SPDX-License-Identifier: GPL-2.0
2c6603c74SDaniel Vetter /*
3c6603c74SDaniel Vetter * Copyright (C) 2020 Intel
4c6603c74SDaniel Vetter *
5c6603c74SDaniel Vetter * Based on drivers/base/devres.c
6c6603c74SDaniel Vetter */
7c6603c74SDaniel Vetter
8c6603c74SDaniel Vetter #include <drm/drm_managed.h>
9c6603c74SDaniel Vetter
10c6603c74SDaniel Vetter #include <linux/list.h>
11e13f13e0SThomas Zimmermann #include <linux/mutex.h>
12c6603c74SDaniel Vetter #include <linux/slab.h>
13c6603c74SDaniel Vetter #include <linux/spinlock.h>
14c6603c74SDaniel Vetter
15c6603c74SDaniel Vetter #include <drm/drm_device.h>
16c6603c74SDaniel Vetter #include <drm/drm_print.h>
17c6603c74SDaniel Vetter
183df6fad4SChris Wilson #include "drm_internal.h"
193df6fad4SChris Wilson
20c6603c74SDaniel Vetter /**
21c6603c74SDaniel Vetter * DOC: managed resources
22c6603c74SDaniel Vetter *
23c6603c74SDaniel Vetter * Inspired by struct &device managed resources, but tied to the lifetime of
24c6603c74SDaniel Vetter * struct &drm_device, which can outlive the underlying physical device, usually
25c6603c74SDaniel Vetter * when userspace has some open files and other handles to resources still open.
269e1ed9fbSDaniel Vetter *
279e1ed9fbSDaniel Vetter * Release actions can be added with drmm_add_action(), memory allocations can
289e1ed9fbSDaniel Vetter * be done directly with drmm_kmalloc() and the related functions. Everything
299e1ed9fbSDaniel Vetter * will be released on the final drm_dev_put() in reverse order of how the
309e1ed9fbSDaniel Vetter * release actions have been added and memory has been allocated since driver
314c8e84b8SDaniel Vetter * loading started with devm_drm_dev_alloc().
329e1ed9fbSDaniel Vetter *
339e1ed9fbSDaniel Vetter * Note that release actions and managed memory can also be added and removed
349e1ed9fbSDaniel Vetter * during the lifetime of the driver, all the functions are fully concurrent
359e1ed9fbSDaniel Vetter * safe. But it is recommended to use managed resources only for resources that
369e1ed9fbSDaniel Vetter * change rarely, if ever, during the lifetime of the &drm_device instance.
37c6603c74SDaniel Vetter */
389e1ed9fbSDaniel Vetter
39c6603c74SDaniel Vetter struct drmres_node {
40c6603c74SDaniel Vetter struct list_head entry;
41c6603c74SDaniel Vetter drmres_release_t release;
42c6603c74SDaniel Vetter const char *name;
43c6603c74SDaniel Vetter size_t size;
44c6603c74SDaniel Vetter };
45c6603c74SDaniel Vetter
46c6603c74SDaniel Vetter struct drmres {
47c6603c74SDaniel Vetter struct drmres_node node;
48c6603c74SDaniel Vetter /*
49c6603c74SDaniel Vetter * Some archs want to perform DMA into kmalloc caches
50c6603c74SDaniel Vetter * and need a guaranteed alignment larger than
51c6603c74SDaniel Vetter * the alignment of a 64-bit integer.
526716ccafSCatalin Marinas * Thus we use ARCH_DMA_MINALIGN for data[] which will force the same
536716ccafSCatalin Marinas * alignment for struct drmres when allocated by kmalloc().
54c6603c74SDaniel Vetter */
556716ccafSCatalin Marinas u8 __aligned(ARCH_DMA_MINALIGN) data[];
56c6603c74SDaniel Vetter };
57c6603c74SDaniel Vetter
free_dr(struct drmres * dr)58c6603c74SDaniel Vetter static void free_dr(struct drmres *dr)
59c6603c74SDaniel Vetter {
60c6603c74SDaniel Vetter kfree_const(dr->node.name);
61c6603c74SDaniel Vetter kfree(dr);
62c6603c74SDaniel Vetter }
63c6603c74SDaniel Vetter
drm_managed_release(struct drm_device * dev)64c6603c74SDaniel Vetter void drm_managed_release(struct drm_device *dev)
65c6603c74SDaniel Vetter {
66c6603c74SDaniel Vetter struct drmres *dr, *tmp;
67c6603c74SDaniel Vetter
68c6603c74SDaniel Vetter drm_dbg_drmres(dev, "drmres release begin\n");
69c6603c74SDaniel Vetter list_for_each_entry_safe(dr, tmp, &dev->managed.resources, node.entry) {
70c6603c74SDaniel Vetter drm_dbg_drmres(dev, "REL %p %s (%zu bytes)\n",
71c6603c74SDaniel Vetter dr, dr->node.name, dr->node.size);
72c6603c74SDaniel Vetter
73c6603c74SDaniel Vetter if (dr->node.release)
74c6603c74SDaniel Vetter dr->node.release(dev, dr->node.size ? *(void **)&dr->data : NULL);
75c6603c74SDaniel Vetter
76c6603c74SDaniel Vetter list_del(&dr->node.entry);
77c6603c74SDaniel Vetter free_dr(dr);
78c6603c74SDaniel Vetter }
79c6603c74SDaniel Vetter drm_dbg_drmres(dev, "drmres release end\n");
80c6603c74SDaniel Vetter }
81c6603c74SDaniel Vetter
82c6603c74SDaniel Vetter /*
83c6603c74SDaniel Vetter * Always inline so that kmalloc_track_caller tracks the actual interesting
84c6603c74SDaniel Vetter * caller outside of drm_managed.c.
85c6603c74SDaniel Vetter */
alloc_dr(drmres_release_t release,size_t size,gfp_t gfp,int nid)86c6603c74SDaniel Vetter static __always_inline struct drmres * alloc_dr(drmres_release_t release,
87c6603c74SDaniel Vetter size_t size, gfp_t gfp, int nid)
88c6603c74SDaniel Vetter {
89c6603c74SDaniel Vetter size_t tot_size;
90c6603c74SDaniel Vetter struct drmres *dr;
91c6603c74SDaniel Vetter
92c6603c74SDaniel Vetter /* We must catch any near-SIZE_MAX cases that could overflow. */
93c6603c74SDaniel Vetter if (unlikely(check_add_overflow(sizeof(*dr), size, &tot_size)))
94c6603c74SDaniel Vetter return NULL;
95c6603c74SDaniel Vetter
96c6603c74SDaniel Vetter dr = kmalloc_node_track_caller(tot_size, gfp, nid);
97c6603c74SDaniel Vetter if (unlikely(!dr))
98c6603c74SDaniel Vetter return NULL;
99c6603c74SDaniel Vetter
100c6603c74SDaniel Vetter memset(dr, 0, offsetof(struct drmres, data));
101c6603c74SDaniel Vetter
102c6603c74SDaniel Vetter INIT_LIST_HEAD(&dr->node.entry);
103c6603c74SDaniel Vetter dr->node.release = release;
104c6603c74SDaniel Vetter dr->node.size = size;
105c6603c74SDaniel Vetter
106c6603c74SDaniel Vetter return dr;
107c6603c74SDaniel Vetter }
108c6603c74SDaniel Vetter
del_dr(struct drm_device * dev,struct drmres * dr)109c6603c74SDaniel Vetter static void del_dr(struct drm_device *dev, struct drmres *dr)
110c6603c74SDaniel Vetter {
111c6603c74SDaniel Vetter list_del_init(&dr->node.entry);
112c6603c74SDaniel Vetter
113c6603c74SDaniel Vetter drm_dbg_drmres(dev, "DEL %p %s (%lu bytes)\n",
114c6603c74SDaniel Vetter dr, dr->node.name, (unsigned long) dr->node.size);
115c6603c74SDaniel Vetter }
116c6603c74SDaniel Vetter
add_dr(struct drm_device * dev,struct drmres * dr)117c6603c74SDaniel Vetter static void add_dr(struct drm_device *dev, struct drmres *dr)
118c6603c74SDaniel Vetter {
119c6603c74SDaniel Vetter unsigned long flags;
120c6603c74SDaniel Vetter
121c6603c74SDaniel Vetter spin_lock_irqsave(&dev->managed.lock, flags);
122c6603c74SDaniel Vetter list_add(&dr->node.entry, &dev->managed.resources);
123c6603c74SDaniel Vetter spin_unlock_irqrestore(&dev->managed.lock, flags);
124c6603c74SDaniel Vetter
125c6603c74SDaniel Vetter drm_dbg_drmres(dev, "ADD %p %s (%lu bytes)\n",
126c6603c74SDaniel Vetter dr, dr->node.name, (unsigned long) dr->node.size);
127c6603c74SDaniel Vetter }
128c6603c74SDaniel Vetter
drmm_add_final_kfree(struct drm_device * dev,void * container)129c6603c74SDaniel Vetter void drmm_add_final_kfree(struct drm_device *dev, void *container)
130c6603c74SDaniel Vetter {
131c6603c74SDaniel Vetter WARN_ON(dev->managed.final_kfree);
132c6603c74SDaniel Vetter WARN_ON(dev < (struct drm_device *) container);
133c7da606eSDaniel Vetter WARN_ON(dev + 1 > (struct drm_device *) (container + ksize(container)));
134c6603c74SDaniel Vetter dev->managed.final_kfree = container;
135c6603c74SDaniel Vetter }
136c6603c74SDaniel Vetter
__drmm_add_action(struct drm_device * dev,drmres_release_t action,void * data,const char * name)137c6603c74SDaniel Vetter int __drmm_add_action(struct drm_device *dev,
138c6603c74SDaniel Vetter drmres_release_t action,
139c6603c74SDaniel Vetter void *data, const char *name)
140c6603c74SDaniel Vetter {
141c6603c74SDaniel Vetter struct drmres *dr;
142c6603c74SDaniel Vetter void **void_ptr;
143c6603c74SDaniel Vetter
144c6603c74SDaniel Vetter dr = alloc_dr(action, data ? sizeof(void*) : 0,
145c6603c74SDaniel Vetter GFP_KERNEL | __GFP_ZERO,
146c6603c74SDaniel Vetter dev_to_node(dev->dev));
147c6603c74SDaniel Vetter if (!dr) {
148c6603c74SDaniel Vetter drm_dbg_drmres(dev, "failed to add action %s for %p\n",
149c6603c74SDaniel Vetter name, data);
150c6603c74SDaniel Vetter return -ENOMEM;
151c6603c74SDaniel Vetter }
152c6603c74SDaniel Vetter
153c6603c74SDaniel Vetter dr->node.name = kstrdup_const(name, GFP_KERNEL);
154c6603c74SDaniel Vetter if (data) {
155c6603c74SDaniel Vetter void_ptr = (void **)&dr->data;
156c6603c74SDaniel Vetter *void_ptr = data;
157c6603c74SDaniel Vetter }
158c6603c74SDaniel Vetter
159c6603c74SDaniel Vetter add_dr(dev, dr);
160c6603c74SDaniel Vetter
161c6603c74SDaniel Vetter return 0;
162c6603c74SDaniel Vetter }
163c6603c74SDaniel Vetter EXPORT_SYMBOL(__drmm_add_action);
164c6603c74SDaniel Vetter
__drmm_add_action_or_reset(struct drm_device * dev,drmres_release_t action,void * data,const char * name)165f96306f9SDaniel Vetter int __drmm_add_action_or_reset(struct drm_device *dev,
166f96306f9SDaniel Vetter drmres_release_t action,
167f96306f9SDaniel Vetter void *data, const char *name)
168f96306f9SDaniel Vetter {
169f96306f9SDaniel Vetter int ret;
170f96306f9SDaniel Vetter
171f96306f9SDaniel Vetter ret = __drmm_add_action(dev, action, data, name);
172f96306f9SDaniel Vetter if (ret)
173f96306f9SDaniel Vetter action(dev, data);
174f96306f9SDaniel Vetter
175f96306f9SDaniel Vetter return ret;
176f96306f9SDaniel Vetter }
177f96306f9SDaniel Vetter EXPORT_SYMBOL(__drmm_add_action_or_reset);
178f96306f9SDaniel Vetter
1799e1ed9fbSDaniel Vetter /**
1809e1ed9fbSDaniel Vetter * drmm_kmalloc - &drm_device managed kmalloc()
1819e1ed9fbSDaniel Vetter * @dev: DRM device
1829e1ed9fbSDaniel Vetter * @size: size of the memory allocation
1839e1ed9fbSDaniel Vetter * @gfp: GFP allocation flags
1849e1ed9fbSDaniel Vetter *
1859e1ed9fbSDaniel Vetter * This is a &drm_device managed version of kmalloc(). The allocated memory is
1869e1ed9fbSDaniel Vetter * automatically freed on the final drm_dev_put(). Memory can also be freed
1879e1ed9fbSDaniel Vetter * before the final drm_dev_put() by calling drmm_kfree().
1889e1ed9fbSDaniel Vetter */
drmm_kmalloc(struct drm_device * dev,size_t size,gfp_t gfp)189c6603c74SDaniel Vetter void *drmm_kmalloc(struct drm_device *dev, size_t size, gfp_t gfp)
190c6603c74SDaniel Vetter {
191c6603c74SDaniel Vetter struct drmres *dr;
192c6603c74SDaniel Vetter
193c6603c74SDaniel Vetter dr = alloc_dr(NULL, size, gfp, dev_to_node(dev->dev));
194c6603c74SDaniel Vetter if (!dr) {
195c6603c74SDaniel Vetter drm_dbg_drmres(dev, "failed to allocate %zu bytes, %u flags\n",
196c6603c74SDaniel Vetter size, gfp);
197c6603c74SDaniel Vetter return NULL;
198c6603c74SDaniel Vetter }
199*c3f698d8SDan Carpenter dr->node.name = kstrdup_const("kmalloc", gfp);
200c6603c74SDaniel Vetter
201c6603c74SDaniel Vetter add_dr(dev, dr);
202c6603c74SDaniel Vetter
203c6603c74SDaniel Vetter return dr->data;
204c6603c74SDaniel Vetter }
205c6603c74SDaniel Vetter EXPORT_SYMBOL(drmm_kmalloc);
206c6603c74SDaniel Vetter
2079e1ed9fbSDaniel Vetter /**
2089e1ed9fbSDaniel Vetter * drmm_kstrdup - &drm_device managed kstrdup()
2099e1ed9fbSDaniel Vetter * @dev: DRM device
2109e1ed9fbSDaniel Vetter * @s: 0-terminated string to be duplicated
2119e1ed9fbSDaniel Vetter * @gfp: GFP allocation flags
2129e1ed9fbSDaniel Vetter *
2139e1ed9fbSDaniel Vetter * This is a &drm_device managed version of kstrdup(). The allocated memory is
2149e1ed9fbSDaniel Vetter * automatically freed on the final drm_dev_put() and works exactly like a
2159e1ed9fbSDaniel Vetter * memory allocation obtained by drmm_kmalloc().
2169e1ed9fbSDaniel Vetter */
drmm_kstrdup(struct drm_device * dev,const char * s,gfp_t gfp)217a5c71fdbSDaniel Vetter char *drmm_kstrdup(struct drm_device *dev, const char *s, gfp_t gfp)
218a5c71fdbSDaniel Vetter {
219a5c71fdbSDaniel Vetter size_t size;
220a5c71fdbSDaniel Vetter char *buf;
221a5c71fdbSDaniel Vetter
222a5c71fdbSDaniel Vetter if (!s)
223a5c71fdbSDaniel Vetter return NULL;
224a5c71fdbSDaniel Vetter
225a5c71fdbSDaniel Vetter size = strlen(s) + 1;
226a5c71fdbSDaniel Vetter buf = drmm_kmalloc(dev, size, gfp);
227a5c71fdbSDaniel Vetter if (buf)
228a5c71fdbSDaniel Vetter memcpy(buf, s, size);
229a5c71fdbSDaniel Vetter return buf;
230a5c71fdbSDaniel Vetter }
231a5c71fdbSDaniel Vetter EXPORT_SYMBOL_GPL(drmm_kstrdup);
232a5c71fdbSDaniel Vetter
2339e1ed9fbSDaniel Vetter /**
2349e1ed9fbSDaniel Vetter * drmm_kfree - &drm_device managed kfree()
2359e1ed9fbSDaniel Vetter * @dev: DRM device
2369e1ed9fbSDaniel Vetter * @data: memory allocation to be freed
2379e1ed9fbSDaniel Vetter *
2389e1ed9fbSDaniel Vetter * This is a &drm_device managed version of kfree() which can be used to
2399e1ed9fbSDaniel Vetter * release memory allocated through drmm_kmalloc() or any of its related
2409e1ed9fbSDaniel Vetter * functions before the final drm_dev_put() of @dev.
2419e1ed9fbSDaniel Vetter */
drmm_kfree(struct drm_device * dev,void * data)242c6603c74SDaniel Vetter void drmm_kfree(struct drm_device *dev, void *data)
243c6603c74SDaniel Vetter {
244c6603c74SDaniel Vetter struct drmres *dr_match = NULL, *dr;
245c6603c74SDaniel Vetter unsigned long flags;
246c6603c74SDaniel Vetter
247c6603c74SDaniel Vetter if (!data)
248c6603c74SDaniel Vetter return;
249c6603c74SDaniel Vetter
250c6603c74SDaniel Vetter spin_lock_irqsave(&dev->managed.lock, flags);
251c6603c74SDaniel Vetter list_for_each_entry(dr, &dev->managed.resources, node.entry) {
252c6603c74SDaniel Vetter if (dr->data == data) {
253c6603c74SDaniel Vetter dr_match = dr;
254c6603c74SDaniel Vetter del_dr(dev, dr_match);
255c6603c74SDaniel Vetter break;
256c6603c74SDaniel Vetter }
257c6603c74SDaniel Vetter }
258c6603c74SDaniel Vetter spin_unlock_irqrestore(&dev->managed.lock, flags);
259c6603c74SDaniel Vetter
260c6603c74SDaniel Vetter if (WARN_ON(!dr_match))
261c6603c74SDaniel Vetter return;
262c6603c74SDaniel Vetter
263c6603c74SDaniel Vetter free_dr(dr_match);
264c6603c74SDaniel Vetter }
265c6603c74SDaniel Vetter EXPORT_SYMBOL(drmm_kfree);
266e13f13e0SThomas Zimmermann
__drmm_mutex_release(struct drm_device * dev,void * res)267c21f11d1SMatthew Auld void __drmm_mutex_release(struct drm_device *dev, void *res)
268e13f13e0SThomas Zimmermann {
269e13f13e0SThomas Zimmermann struct mutex *lock = res;
270e13f13e0SThomas Zimmermann
271e13f13e0SThomas Zimmermann mutex_destroy(lock);
272e13f13e0SThomas Zimmermann }
273c21f11d1SMatthew Auld EXPORT_SYMBOL(__drmm_mutex_release);
274