Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright 2014 IBM Corp.
4 */
5
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"
13
14/* protected by rcu */
15static struct cxl_calls *cxl_calls;
16
17atomic_t cxl_use_count = ATOMIC_INIT(0);
18EXPORT_SYMBOL(cxl_use_count);
19
20#ifdef CONFIG_CXL_MODULE
21
22static inline struct cxl_calls *cxl_calls_get(void)
23{
24 struct cxl_calls *calls = NULL;
25
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();
31
32 return calls;
33}
34
35static inline void cxl_calls_put(struct cxl_calls *calls)
36{
37 BUG_ON(calls != cxl_calls);
38
39 /* we don't need to rcu this, as we hold a reference to the module */
40 module_put(cxl_calls->owner);
41}
42
43#else /* !defined CONFIG_CXL_MODULE */
44
45static inline struct cxl_calls *cxl_calls_get(void)
46{
47 return cxl_calls;
48}
49
50static inline void cxl_calls_put(struct cxl_calls *calls) { }
51
52#endif /* CONFIG_CXL_MODULE */
53
54/* AFU refcount management */
55struct cxl_afu *cxl_afu_get(struct cxl_afu *afu)
56{
57 return (get_device(&afu->dev) == NULL) ? NULL : afu;
58}
59EXPORT_SYMBOL_GPL(cxl_afu_get);
60
61void cxl_afu_put(struct cxl_afu *afu)
62{
63 put_device(&afu->dev);
64}
65EXPORT_SYMBOL_GPL(cxl_afu_put);
66
67void cxl_slbia(struct mm_struct *mm)
68{
69 struct cxl_calls *calls;
70
71 calls = cxl_calls_get();
72 if (!calls)
73 return;
74
75 if (cxl_ctx_in_use())
76 calls->cxl_slbia(mm);
77
78 cxl_calls_put(calls);
79}
80
81int register_cxl_calls(struct cxl_calls *calls)
82{
83 if (cxl_calls)
84 return -EBUSY;
85
86 rcu_assign_pointer(cxl_calls, calls);
87 return 0;
88}
89EXPORT_SYMBOL_GPL(register_cxl_calls);
90
91void unregister_cxl_calls(struct cxl_calls *calls)
92{
93 BUG_ON(cxl_calls->owner != calls->owner);
94 RCU_INIT_POINTER(cxl_calls, NULL);
95 synchronize_rcu();
96}
97EXPORT_SYMBOL_GPL(unregister_cxl_calls);
98
99int cxl_update_properties(struct device_node *dn,
100 struct property *new_prop)
101{
102 return of_update_property(dn, new_prop);
103}
104EXPORT_SYMBOL_GPL(cxl_update_properties);
105
106static int __init cxl_base_init(void)
107{
108 struct device_node *np;
109 struct platform_device *dev;
110 int count = 0;
111
112 /*
113 * Scan for compatible devices in guest only
114 */
115 if (cpu_has_feature(CPU_FTR_HVMODE))
116 return 0;
117
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++;
122 }
123 pr_devel("Found %d cxl device(s)\n", count);
124 return 0;
125}
126device_initcall(cxl_base_init);
1/*
2 * Copyright 2014 IBM Corp.
3 *
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.
8 */
9
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>
15#include "cxl.h"
16
17/* protected by rcu */
18static struct cxl_calls *cxl_calls;
19
20atomic_t cxl_use_count = ATOMIC_INIT(0);
21EXPORT_SYMBOL(cxl_use_count);
22
23#ifdef CONFIG_CXL_MODULE
24
25static inline struct cxl_calls *cxl_calls_get(void)
26{
27 struct cxl_calls *calls = NULL;
28
29 rcu_read_lock();
30 calls = rcu_dereference(cxl_calls);
31 if (calls && !try_module_get(calls->owner))
32 calls = NULL;
33 rcu_read_unlock();
34
35 return calls;
36}
37
38static inline void cxl_calls_put(struct cxl_calls *calls)
39{
40 BUG_ON(calls != cxl_calls);
41
42 /* we don't need to rcu this, as we hold a reference to the module */
43 module_put(cxl_calls->owner);
44}
45
46#else /* !defined CONFIG_CXL_MODULE */
47
48static inline struct cxl_calls *cxl_calls_get(void)
49{
50 return cxl_calls;
51}
52
53static inline void cxl_calls_put(struct cxl_calls *calls) { }
54
55#endif /* CONFIG_CXL_MODULE */
56
57void cxl_slbia(struct mm_struct *mm)
58{
59 struct cxl_calls *calls;
60
61 calls = cxl_calls_get();
62 if (!calls)
63 return;
64
65 if (cxl_ctx_in_use())
66 calls->cxl_slbia(mm);
67
68 cxl_calls_put(calls);
69}
70
71int register_cxl_calls(struct cxl_calls *calls)
72{
73 if (cxl_calls)
74 return -EBUSY;
75
76 rcu_assign_pointer(cxl_calls, calls);
77 return 0;
78}
79EXPORT_SYMBOL_GPL(register_cxl_calls);
80
81void unregister_cxl_calls(struct cxl_calls *calls)
82{
83 BUG_ON(cxl_calls->owner != calls->owner);
84 RCU_INIT_POINTER(cxl_calls, NULL);
85 synchronize_rcu();
86}
87EXPORT_SYMBOL_GPL(unregister_cxl_calls);
88
89int cxl_update_properties(struct device_node *dn,
90 struct property *new_prop)
91{
92 return of_update_property(dn, new_prop);
93}
94EXPORT_SYMBOL_GPL(cxl_update_properties);
95
96static int __init cxl_base_init(void)
97{
98 struct device_node *np = NULL;
99 struct platform_device *dev;
100 int count = 0;
101
102 /*
103 * Scan for compatible devices in guest only
104 */
105 if (cpu_has_feature(CPU_FTR_HVMODE))
106 return 0;
107
108 while ((np = of_find_compatible_node(np, NULL,
109 "ibm,coherent-platform-facility"))) {
110 dev = of_platform_device_create(np, NULL, NULL);
111 if (dev)
112 count++;
113 }
114 pr_devel("Found %d cxl device(s)\n", count);
115 return 0;
116}
117
118module_init(cxl_base_init);