1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_IOASID_H
3 #define __LINUX_IOASID_H
4 
5 #include <linux/types.h>
6 #include <linux/errno.h>
7 
8 #define INVALID_IOASID ((ioasid_t)-1)
9 typedef unsigned int ioasid_t;
10 typedef ioasid_t (*ioasid_alloc_fn_t)(ioasid_t min, ioasid_t max, void *data);
11 typedef void (*ioasid_free_fn_t)(ioasid_t ioasid, void *data);
12 
13 struct ioasid_set {
14 	int dummy;
15 };
16 
17 /**
18  * struct ioasid_allocator_ops - IOASID allocator helper functions and data
19  *
20  * @alloc:	helper function to allocate IOASID
21  * @free:	helper function to free IOASID
22  * @list:	for tracking ops that share helper functions but not data
23  * @pdata:	data belong to the allocator, provided when calling alloc()
24  */
25 struct ioasid_allocator_ops {
26 	ioasid_alloc_fn_t alloc;
27 	ioasid_free_fn_t free;
28 	struct list_head list;
29 	void *pdata;
30 };
31 
32 #define DECLARE_IOASID_SET(name) struct ioasid_set name = { 0 }
33 
34 #if IS_ENABLED(CONFIG_IOASID)
35 ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max,
36 		      void *private);
37 void ioasid_free(ioasid_t ioasid);
38 void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid,
39 		  bool (*getter)(void *));
40 int ioasid_register_allocator(struct ioasid_allocator_ops *allocator);
41 void ioasid_unregister_allocator(struct ioasid_allocator_ops *allocator);
42 int ioasid_set_data(ioasid_t ioasid, void *data);
pasid_valid(ioasid_t ioasid)43 static inline bool pasid_valid(ioasid_t ioasid)
44 {
45 	return ioasid != INVALID_IOASID;
46 }
47 
48 #else /* !CONFIG_IOASID */
ioasid_alloc(struct ioasid_set * set,ioasid_t min,ioasid_t max,void * private)49 static inline ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min,
50 				    ioasid_t max, void *private)
51 {
52 	return INVALID_IOASID;
53 }
54 
ioasid_free(ioasid_t ioasid)55 static inline void ioasid_free(ioasid_t ioasid) { }
56 
ioasid_find(struct ioasid_set * set,ioasid_t ioasid,bool (* getter)(void *))57 static inline void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid,
58 				bool (*getter)(void *))
59 {
60 	return NULL;
61 }
62 
ioasid_register_allocator(struct ioasid_allocator_ops * allocator)63 static inline int ioasid_register_allocator(struct ioasid_allocator_ops *allocator)
64 {
65 	return -ENOTSUPP;
66 }
67 
ioasid_unregister_allocator(struct ioasid_allocator_ops * allocator)68 static inline void ioasid_unregister_allocator(struct ioasid_allocator_ops *allocator)
69 {
70 }
71 
ioasid_set_data(ioasid_t ioasid,void * data)72 static inline int ioasid_set_data(ioasid_t ioasid, void *data)
73 {
74 	return -ENOTSUPP;
75 }
76 
pasid_valid(ioasid_t ioasid)77 static inline bool pasid_valid(ioasid_t ioasid)
78 {
79 	return false;
80 }
81 
82 #endif /* CONFIG_IOASID */
83 #endif /* __LINUX_IOASID_H */
84