2 * Copyright 2014 IBM Corp.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
10 #include <linux/module.h>
11 #include <linux/rcupdate.h>
12 #include <asm/errno.h>
13 #include <misc/cxl-base.h>
14 #include <linux/of_platform.h>
17 /* protected by rcu */
18 static struct cxl_calls
*cxl_calls
;
20 atomic_t cxl_use_count
= ATOMIC_INIT(0);
21 EXPORT_SYMBOL(cxl_use_count
);
23 #ifdef CONFIG_CXL_MODULE
25 static inline struct cxl_calls
*cxl_calls_get(void)
27 struct cxl_calls
*calls
= NULL
;
30 calls
= rcu_dereference(cxl_calls
);
31 if (calls
&& !try_module_get(calls
->owner
))
38 static inline void cxl_calls_put(struct cxl_calls
*calls
)
40 BUG_ON(calls
!= cxl_calls
);
42 /* we don't need to rcu this, as we hold a reference to the module */
43 module_put(cxl_calls
->owner
);
46 #else /* !defined CONFIG_CXL_MODULE */
48 static inline struct cxl_calls
*cxl_calls_get(void)
53 static inline void cxl_calls_put(struct cxl_calls
*calls
) { }
55 #endif /* CONFIG_CXL_MODULE */
57 /* AFU refcount management */
58 struct cxl_afu
*cxl_afu_get(struct cxl_afu
*afu
)
60 return (get_device(&afu
->dev
) == NULL
) ? NULL
: afu
;
62 EXPORT_SYMBOL_GPL(cxl_afu_get
);
64 void cxl_afu_put(struct cxl_afu
*afu
)
66 put_device(&afu
->dev
);
68 EXPORT_SYMBOL_GPL(cxl_afu_put
);
70 void cxl_slbia(struct mm_struct
*mm
)
72 struct cxl_calls
*calls
;
74 calls
= cxl_calls_get();
84 int register_cxl_calls(struct cxl_calls
*calls
)
89 rcu_assign_pointer(cxl_calls
, calls
);
92 EXPORT_SYMBOL_GPL(register_cxl_calls
);
94 void unregister_cxl_calls(struct cxl_calls
*calls
)
96 BUG_ON(cxl_calls
->owner
!= calls
->owner
);
97 RCU_INIT_POINTER(cxl_calls
, NULL
);
100 EXPORT_SYMBOL_GPL(unregister_cxl_calls
);
102 int cxl_update_properties(struct device_node
*dn
,
103 struct property
*new_prop
)
105 return of_update_property(dn
, new_prop
);
107 EXPORT_SYMBOL_GPL(cxl_update_properties
);
110 * API calls into the driver that may be called from the PHB code and must be
113 bool cxl_pci_associate_default_context(struct pci_dev
*dev
, struct cxl_afu
*afu
)
116 struct cxl_calls
*calls
;
118 calls
= cxl_calls_get();
122 ret
= calls
->cxl_pci_associate_default_context(dev
, afu
);
124 cxl_calls_put(calls
);
128 EXPORT_SYMBOL_GPL(cxl_pci_associate_default_context
);
130 void cxl_pci_disable_device(struct pci_dev
*dev
)
132 struct cxl_calls
*calls
;
134 calls
= cxl_calls_get();
138 calls
->cxl_pci_disable_device(dev
);
140 cxl_calls_put(calls
);
142 EXPORT_SYMBOL_GPL(cxl_pci_disable_device
);
144 int cxl_next_msi_hwirq(struct pci_dev
*pdev
, struct cxl_context
**ctx
, int *afu_irq
)
147 struct cxl_calls
*calls
;
149 calls
= cxl_calls_get();
153 ret
= calls
->cxl_next_msi_hwirq(pdev
, ctx
, afu_irq
);
155 cxl_calls_put(calls
);
159 EXPORT_SYMBOL_GPL(cxl_next_msi_hwirq
);
161 int cxl_cx4_setup_msi_irqs(struct pci_dev
*pdev
, int nvec
, int type
)
164 struct cxl_calls
*calls
;
166 calls
= cxl_calls_get();
170 ret
= calls
->cxl_cx4_setup_msi_irqs(pdev
, nvec
, type
);
172 cxl_calls_put(calls
);
176 EXPORT_SYMBOL_GPL(cxl_cx4_setup_msi_irqs
);
178 void cxl_cx4_teardown_msi_irqs(struct pci_dev
*pdev
)
180 struct cxl_calls
*calls
;
182 calls
= cxl_calls_get();
186 calls
->cxl_cx4_teardown_msi_irqs(pdev
);
188 cxl_calls_put(calls
);
190 EXPORT_SYMBOL_GPL(cxl_cx4_teardown_msi_irqs
);
192 static int __init
cxl_base_init(void)
194 struct device_node
*np
;
195 struct platform_device
*dev
;
199 * Scan for compatible devices in guest only
201 if (cpu_has_feature(CPU_FTR_HVMODE
))
204 for_each_compatible_node(np
, NULL
, "ibm,coherent-platform-facility") {
205 dev
= of_platform_device_create(np
, NULL
, NULL
);
209 pr_devel("Found %d cxl device(s)\n", count
);
212 device_initcall(cxl_base_init
);