Merge tag 'at91-ab-4.8-defconfig2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6/btrfs-unstable.git] / drivers / misc / cxl / base.c
blob9b90ec6c07cd78fa3047ef3e39ba1626a318e8f3
1 /*
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.
8 */
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"
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;
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();
35 return calls;
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)
50 return cxl_calls;
53 static inline void cxl_calls_put(struct cxl_calls *calls) { }
55 #endif /* CONFIG_CXL_MODULE */
57 void cxl_slbia(struct mm_struct *mm)
59 struct cxl_calls *calls;
61 calls = cxl_calls_get();
62 if (!calls)
63 return;
65 if (cxl_ctx_in_use())
66 calls->cxl_slbia(mm);
68 cxl_calls_put(calls);
71 int register_cxl_calls(struct cxl_calls *calls)
73 if (cxl_calls)
74 return -EBUSY;
76 rcu_assign_pointer(cxl_calls, calls);
77 return 0;
79 EXPORT_SYMBOL_GPL(register_cxl_calls);
81 void unregister_cxl_calls(struct cxl_calls *calls)
83 BUG_ON(cxl_calls->owner != calls->owner);
84 RCU_INIT_POINTER(cxl_calls, NULL);
85 synchronize_rcu();
87 EXPORT_SYMBOL_GPL(unregister_cxl_calls);
89 int cxl_update_properties(struct device_node *dn,
90 struct property *new_prop)
92 return of_update_property(dn, new_prop);
94 EXPORT_SYMBOL_GPL(cxl_update_properties);
96 static int __init cxl_base_init(void)
98 struct device_node *np = NULL;
99 struct platform_device *dev;
100 int count = 0;
103 * Scan for compatible devices in guest only
105 if (cpu_has_feature(CPU_FTR_HVMODE))
106 return 0;
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++;
114 pr_devel("Found %d cxl device(s)\n", count);
115 return 0;
118 module_init(cxl_base_init);