1 // SPDX-License-Identifier: GPL-2.0
3 * I/O Address Space ID allocator. There is one global IOASID space, split into
4 * subsets. Users create a subset with DECLARE_IOASID_SET, then allocate and
5 * free IOASIDs with ioasid_alloc() and ioasid_free().
7 #include <linux/ioasid.h>
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/spinlock.h>
11 #include <linux/xarray.h>
15 struct ioasid_set *set;
21 * struct ioasid_allocator_data - Internal data structure to hold information
22 * about an allocator. There are two types of allocators:
24 * - Default allocator always has its own XArray to track the IOASIDs allocated.
25 * - Custom allocators may share allocation helpers with different private data.
26 * Custom allocators that share the same helper functions also share the same
29 * 1. Default allocator is always available, not dynamically registered. This is
30 * to prevent race conditions with early boot code that want to register
31 * custom allocators or allocate IOASIDs.
32 * 2. Custom allocators take precedence over the default allocator.
33 * 3. When all custom allocators sharing the same helper functions are
34 * unregistered (e.g. due to hotplug), all outstanding IOASIDs must be
35 * freed. Otherwise, outstanding IOASIDs will be lost and orphaned.
36 * 4. When switching between custom allocators sharing the same helper
37 * functions, outstanding IOASIDs are preserved.
38 * 5. When switching between custom allocator and default allocator, all IOASIDs
39 * must be freed to ensure unadulterated space for the new allocator.
41 * @ops: allocator helper functions and its data
42 * @list: registered custom allocators
43 * @slist: allocators share the same ops but different data
44 * @flags: attributes of the allocator
45 * @xa: xarray holds the IOASID space
46 * @rcu: used for kfree_rcu when unregistering allocator
48 struct ioasid_allocator_data {
49 struct ioasid_allocator_ops *ops;
50 struct list_head list;
51 struct list_head slist;
52 #define IOASID_ALLOCATOR_CUSTOM BIT(0) /* Needs framework to track results */
58 static DEFINE_SPINLOCK(ioasid_allocator_lock);
59 static LIST_HEAD(allocators_list);
61 static ioasid_t default_alloc(ioasid_t min, ioasid_t max, void *opaque);
62 static void default_free(ioasid_t ioasid, void *opaque);
64 static struct ioasid_allocator_ops default_ops = {
65 .alloc = default_alloc,
69 static struct ioasid_allocator_data default_allocator = {
72 .xa = XARRAY_INIT(ioasid_xa, XA_FLAGS_ALLOC),
75 static struct ioasid_allocator_data *active_allocator = &default_allocator;
77 static ioasid_t default_alloc(ioasid_t min, ioasid_t max, void *opaque)
81 if (xa_alloc(&default_allocator.xa, &id, opaque, XA_LIMIT(min, max), GFP_ATOMIC)) {
82 pr_err("Failed to alloc ioasid from %d to %d\n", min, max);
83 return INVALID_IOASID;
89 static void default_free(ioasid_t ioasid, void *opaque)
91 struct ioasid_data *ioasid_data;
93 ioasid_data = xa_erase(&default_allocator.xa, ioasid);
94 kfree_rcu(ioasid_data, rcu);
97 /* Allocate and initialize a new custom allocator with its helper functions */
98 static struct ioasid_allocator_data *ioasid_alloc_allocator(struct ioasid_allocator_ops *ops)
100 struct ioasid_allocator_data *ia_data;
102 ia_data = kzalloc(sizeof(*ia_data), GFP_ATOMIC);
106 xa_init_flags(&ia_data->xa, XA_FLAGS_ALLOC);
107 INIT_LIST_HEAD(&ia_data->slist);
108 ia_data->flags |= IOASID_ALLOCATOR_CUSTOM;
111 /* For tracking custom allocators that share the same ops */
112 list_add_tail(&ops->list, &ia_data->slist);
117 static bool use_same_ops(struct ioasid_allocator_ops *a, struct ioasid_allocator_ops *b)
119 return (a->free == b->free) && (a->alloc == b->alloc);
123 * ioasid_register_allocator - register a custom allocator
124 * @ops: the custom allocator ops to be registered
126 * Custom allocators take precedence over the default xarray based allocator.
127 * Private data associated with the IOASID allocated by the custom allocators
128 * are managed by IOASID framework similar to data stored in xa by default
131 * There can be multiple allocators registered but only one is active. In case
132 * of runtime removal of a custom allocator, the next one is activated based
133 * on the registration ordering.
135 * Multiple allocators can share the same alloc() function, in this case the
136 * IOASID space is shared.
138 int ioasid_register_allocator(struct ioasid_allocator_ops *ops)
140 struct ioasid_allocator_data *ia_data;
141 struct ioasid_allocator_data *pallocator;
144 spin_lock(&ioasid_allocator_lock);
146 ia_data = ioasid_alloc_allocator(ops);
153 * No particular preference, we activate the first one and keep
154 * the later registered allocators in a list in case the first one gets
155 * removed due to hotplug.
157 if (list_empty(&allocators_list)) {
158 WARN_ON(active_allocator != &default_allocator);
159 /* Use this new allocator if default is not active */
160 if (xa_empty(&active_allocator->xa)) {
161 rcu_assign_pointer(active_allocator, ia_data);
162 list_add_tail(&ia_data->list, &allocators_list);
165 pr_warn("Default allocator active with outstanding IOASID\n");
170 /* Check if the allocator is already registered */
171 list_for_each_entry(pallocator, &allocators_list, list) {
172 if (pallocator->ops == ops) {
173 pr_err("IOASID allocator already registered\n");
176 } else if (use_same_ops(pallocator->ops, ops)) {
178 * If the new allocator shares the same ops,
179 * then they will share the same IOASID space.
180 * We should put them under the same xarray.
182 list_add_tail(&ops->list, &pallocator->slist);
186 list_add_tail(&ia_data->list, &allocators_list);
188 spin_unlock(&ioasid_allocator_lock);
193 spin_unlock(&ioasid_allocator_lock);
196 EXPORT_SYMBOL_GPL(ioasid_register_allocator);
199 * ioasid_unregister_allocator - Remove a custom IOASID allocator ops
200 * @ops: the custom allocator to be removed
202 * Remove an allocator from the list, activate the next allocator in
203 * the order it was registered. Or revert to default allocator if all
204 * custom allocators are unregistered without outstanding IOASIDs.
206 void ioasid_unregister_allocator(struct ioasid_allocator_ops *ops)
208 struct ioasid_allocator_data *pallocator;
209 struct ioasid_allocator_ops *sops;
211 spin_lock(&ioasid_allocator_lock);
212 if (list_empty(&allocators_list)) {
213 pr_warn("No custom IOASID allocators active!\n");
217 list_for_each_entry(pallocator, &allocators_list, list) {
218 if (!use_same_ops(pallocator->ops, ops))
221 if (list_is_singular(&pallocator->slist)) {
222 /* No shared helper functions */
223 list_del(&pallocator->list);
225 * All IOASIDs should have been freed before
226 * the last allocator that shares the same ops
229 WARN_ON(!xa_empty(&pallocator->xa));
230 if (list_empty(&allocators_list)) {
231 pr_info("No custom IOASID allocators, switch to default.\n");
232 rcu_assign_pointer(active_allocator, &default_allocator);
233 } else if (pallocator == active_allocator) {
234 rcu_assign_pointer(active_allocator,
235 list_first_entry(&allocators_list,
236 struct ioasid_allocator_data, list));
237 pr_info("IOASID allocator changed");
239 kfree_rcu(pallocator, rcu);
243 * Find the matching shared ops to delete,
244 * but keep outstanding IOASIDs
246 list_for_each_entry(sops, &pallocator->slist, list) {
248 list_del(&ops->list);
256 spin_unlock(&ioasid_allocator_lock);
258 EXPORT_SYMBOL_GPL(ioasid_unregister_allocator);
261 * ioasid_set_data - Set private data for an allocated ioasid
262 * @ioasid: the ID to set data
263 * @data: the private data
265 * For IOASID that is already allocated, private data can be set
266 * via this API. Future lookup can be done via ioasid_find.
268 int ioasid_set_data(ioasid_t ioasid, void *data)
270 struct ioasid_data *ioasid_data;
273 spin_lock(&ioasid_allocator_lock);
274 ioasid_data = xa_load(&active_allocator->xa, ioasid);
276 rcu_assign_pointer(ioasid_data->private, data);
279 spin_unlock(&ioasid_allocator_lock);
282 * Wait for readers to stop accessing the old private data, so the
283 * caller can free it.
290 EXPORT_SYMBOL_GPL(ioasid_set_data);
293 * ioasid_alloc - Allocate an IOASID
294 * @set: the IOASID set
295 * @min: the minimum ID (inclusive)
296 * @max: the maximum ID (inclusive)
297 * @private: data private to the caller
299 * Allocate an ID between @min and @max. The @private pointer is stored
300 * internally and can be retrieved with ioasid_find().
302 * Return: the allocated ID on success, or %INVALID_IOASID on failure.
304 ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max,
307 struct ioasid_data *data;
311 data = kzalloc(sizeof(*data), GFP_ATOMIC);
313 return INVALID_IOASID;
316 data->private = private;
319 * Custom allocator needs allocator data to perform platform specific
322 spin_lock(&ioasid_allocator_lock);
323 adata = active_allocator->flags & IOASID_ALLOCATOR_CUSTOM ? active_allocator->ops->pdata : data;
324 id = active_allocator->ops->alloc(min, max, adata);
325 if (id == INVALID_IOASID) {
326 pr_err("Failed ASID allocation %lu\n", active_allocator->flags);
330 if ((active_allocator->flags & IOASID_ALLOCATOR_CUSTOM) &&
331 xa_alloc(&active_allocator->xa, &id, data, XA_LIMIT(id, id), GFP_ATOMIC)) {
332 /* Custom allocator needs framework to store and track allocation results */
333 pr_err("Failed to alloc ioasid from %d\n", id);
334 active_allocator->ops->free(id, active_allocator->ops->pdata);
339 spin_unlock(&ioasid_allocator_lock);
342 spin_unlock(&ioasid_allocator_lock);
344 return INVALID_IOASID;
346 EXPORT_SYMBOL_GPL(ioasid_alloc);
349 * ioasid_free - Free an ioasid
350 * @ioasid: the ID to remove
352 void ioasid_free(ioasid_t ioasid)
354 struct ioasid_data *ioasid_data;
356 spin_lock(&ioasid_allocator_lock);
357 ioasid_data = xa_load(&active_allocator->xa, ioasid);
359 pr_err("Trying to free unknown IOASID %u\n", ioasid);
363 active_allocator->ops->free(ioasid, active_allocator->ops->pdata);
364 /* Custom allocator needs additional steps to free the xa element */
365 if (active_allocator->flags & IOASID_ALLOCATOR_CUSTOM) {
366 ioasid_data = xa_erase(&active_allocator->xa, ioasid);
367 kfree_rcu(ioasid_data, rcu);
371 spin_unlock(&ioasid_allocator_lock);
373 EXPORT_SYMBOL_GPL(ioasid_free);
376 * ioasid_find - Find IOASID data
377 * @set: the IOASID set
378 * @ioasid: the IOASID to find
379 * @getter: function to call on the found object
381 * The optional getter function allows to take a reference to the found object
382 * under the rcu lock. The function can also check if the object is still valid:
383 * if @getter returns false, then the object is invalid and NULL is returned.
385 * If the IOASID exists, return the private pointer passed to ioasid_alloc.
386 * Private data can be NULL if not set. Return an error if the IOASID is not
387 * found, or if @set is not NULL and the IOASID does not belong to the set.
389 void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid,
390 bool (*getter)(void *))
393 struct ioasid_data *ioasid_data;
394 struct ioasid_allocator_data *idata;
397 idata = rcu_dereference(active_allocator);
398 ioasid_data = xa_load(&idata->xa, ioasid);
400 priv = ERR_PTR(-ENOENT);
403 if (set && ioasid_data->set != set) {
404 /* data found but does not belong to the set */
405 priv = ERR_PTR(-EACCES);
408 /* Now IOASID and its set is verified, we can return the private data */
409 priv = rcu_dereference(ioasid_data->private);
410 if (getter && !getter(priv))
417 EXPORT_SYMBOL_GPL(ioasid_find);
419 MODULE_AUTHOR("Jean-Philippe Brucker <jean-philippe.brucker@arm.com>");
420 MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@linux.intel.com>");
421 MODULE_DESCRIPTION("IO Address Space ID (IOASID) allocator");
422 MODULE_LICENSE("GPL");