1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2014 IBM Corp.
6 #include <linux/module.h>
7 #include <linux/rcupdate.h>
9 #include <misc/cxl-base.h>
10 #include <linux/of_platform.h>
13 /* protected by rcu */
14 static struct cxl_calls
*cxl_calls
;
16 atomic_t cxl_use_count
= ATOMIC_INIT(0);
17 EXPORT_SYMBOL(cxl_use_count
);
19 #ifdef CONFIG_CXL_MODULE
21 static inline struct cxl_calls
*cxl_calls_get(void)
23 struct cxl_calls
*calls
= NULL
;
26 calls
= rcu_dereference(cxl_calls
);
27 if (calls
&& !try_module_get(calls
->owner
))
34 static inline void cxl_calls_put(struct cxl_calls
*calls
)
36 BUG_ON(calls
!= cxl_calls
);
38 /* we don't need to rcu this, as we hold a reference to the module */
39 module_put(cxl_calls
->owner
);
42 #else /* !defined CONFIG_CXL_MODULE */
44 static inline struct cxl_calls
*cxl_calls_get(void)
49 static inline void cxl_calls_put(struct cxl_calls
*calls
) { }
51 #endif /* CONFIG_CXL_MODULE */
53 /* AFU refcount management */
54 struct cxl_afu
*cxl_afu_get(struct cxl_afu
*afu
)
56 return (get_device(&afu
->dev
) == NULL
) ? NULL
: afu
;
58 EXPORT_SYMBOL_GPL(cxl_afu_get
);
60 void cxl_afu_put(struct cxl_afu
*afu
)
62 put_device(&afu
->dev
);
64 EXPORT_SYMBOL_GPL(cxl_afu_put
);
66 void cxl_slbia(struct mm_struct
*mm
)
68 struct cxl_calls
*calls
;
70 calls
= cxl_calls_get();
80 int register_cxl_calls(struct cxl_calls
*calls
)
85 rcu_assign_pointer(cxl_calls
, calls
);
88 EXPORT_SYMBOL_GPL(register_cxl_calls
);
90 void unregister_cxl_calls(struct cxl_calls
*calls
)
92 BUG_ON(cxl_calls
->owner
!= calls
->owner
);
93 RCU_INIT_POINTER(cxl_calls
, NULL
);
96 EXPORT_SYMBOL_GPL(unregister_cxl_calls
);
98 int cxl_update_properties(struct device_node
*dn
,
99 struct property
*new_prop
)
101 return of_update_property(dn
, new_prop
);
103 EXPORT_SYMBOL_GPL(cxl_update_properties
);
105 static int __init
cxl_base_init(void)
107 struct device_node
*np
;
108 struct platform_device
*dev
;
112 * Scan for compatible devices in guest only
114 if (cpu_has_feature(CPU_FTR_HVMODE
))
117 for_each_compatible_node(np
, NULL
, "ibm,coherent-platform-facility") {
118 dev
= of_platform_device_create(np
, NULL
, NULL
);
122 pr_devel("Found %d cxl device(s)\n", count
);
125 device_initcall(cxl_base_init
);