Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / misc / cxl / base.c
blobb054562c046e2b119a721d2fad1e2ff5ce50b276
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright 2014 IBM Corp.
4 */
6 #include <linux/module.h>
7 #include <linux/rcupdate.h>
8 #include <asm/errno.h>
9 #include <misc/cxl-base.h>
10 #include <linux/of.h>
11 #include <linux/of_platform.h>
12 #include "cxl.h"
14 /* protected by rcu */
15 static struct cxl_calls *cxl_calls;
17 atomic_t cxl_use_count = ATOMIC_INIT(0);
18 EXPORT_SYMBOL(cxl_use_count);
20 #ifdef CONFIG_CXL_MODULE
22 static inline struct cxl_calls *cxl_calls_get(void)
24 struct cxl_calls *calls = NULL;
26 rcu_read_lock();
27 calls = rcu_dereference(cxl_calls);
28 if (calls && !try_module_get(calls->owner))
29 calls = NULL;
30 rcu_read_unlock();
32 return calls;
35 static inline void cxl_calls_put(struct cxl_calls *calls)
37 BUG_ON(calls != cxl_calls);
39 /* we don't need to rcu this, as we hold a reference to the module */
40 module_put(cxl_calls->owner);
43 #else /* !defined CONFIG_CXL_MODULE */
45 static inline struct cxl_calls *cxl_calls_get(void)
47 return cxl_calls;
50 static inline void cxl_calls_put(struct cxl_calls *calls) { }
52 #endif /* CONFIG_CXL_MODULE */
54 /* AFU refcount management */
55 struct cxl_afu *cxl_afu_get(struct cxl_afu *afu)
57 return (get_device(&afu->dev) == NULL) ? NULL : afu;
59 EXPORT_SYMBOL_GPL(cxl_afu_get);
61 void cxl_afu_put(struct cxl_afu *afu)
63 put_device(&afu->dev);
65 EXPORT_SYMBOL_GPL(cxl_afu_put);
67 void cxl_slbia(struct mm_struct *mm)
69 struct cxl_calls *calls;
71 calls = cxl_calls_get();
72 if (!calls)
73 return;
75 if (cxl_ctx_in_use())
76 calls->cxl_slbia(mm);
78 cxl_calls_put(calls);
81 int register_cxl_calls(struct cxl_calls *calls)
83 if (cxl_calls)
84 return -EBUSY;
86 rcu_assign_pointer(cxl_calls, calls);
87 return 0;
89 EXPORT_SYMBOL_GPL(register_cxl_calls);
91 void unregister_cxl_calls(struct cxl_calls *calls)
93 BUG_ON(cxl_calls->owner != calls->owner);
94 RCU_INIT_POINTER(cxl_calls, NULL);
95 synchronize_rcu();
97 EXPORT_SYMBOL_GPL(unregister_cxl_calls);
99 int cxl_update_properties(struct device_node *dn,
100 struct property *new_prop)
102 return of_update_property(dn, new_prop);
104 EXPORT_SYMBOL_GPL(cxl_update_properties);
106 static int __init cxl_base_init(void)
108 struct device_node *np;
109 struct platform_device *dev;
110 int count = 0;
113 * Scan for compatible devices in guest only
115 if (cpu_has_feature(CPU_FTR_HVMODE))
116 return 0;
118 for_each_compatible_node(np, NULL, "ibm,coherent-platform-facility") {
119 dev = of_platform_device_create(np, NULL, NULL);
120 if (dev)
121 count++;
123 pr_devel("Found %d cxl device(s)\n", count);
124 return 0;
126 device_initcall(cxl_base_init);