xref: /openbmc/linux/drivers/dca/dca-core.c (revision a1e58bbd)
1 /*
2  * Copyright(c) 2007 Intel Corporation. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the Free
6  * Software Foundation; either version 2 of the License, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59
16  * Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  * The full GNU General Public License is included in this distribution in the
19  * file called COPYING.
20  */
21 
22 /*
23  * This driver supports an interface for DCA clients and providers to meet.
24  */
25 
26 #include <linux/kernel.h>
27 #include <linux/notifier.h>
28 #include <linux/device.h>
29 #include <linux/dca.h>
30 
31 MODULE_LICENSE("GPL");
32 
33 /* For now we're assuming a single, global, DCA provider for the system. */
34 
35 static DEFINE_SPINLOCK(dca_lock);
36 
37 static struct dca_provider *global_dca = NULL;
38 
39 /**
40  * dca_add_requester - add a dca client to the list
41  * @dev - the device that wants dca service
42  */
43 int dca_add_requester(struct device *dev)
44 {
45 	int err, slot;
46 
47 	if (!global_dca)
48 		return -ENODEV;
49 
50 	spin_lock(&dca_lock);
51 	slot = global_dca->ops->add_requester(global_dca, dev);
52 	spin_unlock(&dca_lock);
53 	if (slot < 0)
54 		return slot;
55 
56 	err = dca_sysfs_add_req(global_dca, dev, slot);
57 	if (err) {
58 		spin_lock(&dca_lock);
59 		global_dca->ops->remove_requester(global_dca, dev);
60 		spin_unlock(&dca_lock);
61 		return err;
62 	}
63 
64 	return 0;
65 }
66 EXPORT_SYMBOL_GPL(dca_add_requester);
67 
68 /**
69  * dca_remove_requester - remove a dca client from the list
70  * @dev - the device that wants dca service
71  */
72 int dca_remove_requester(struct device *dev)
73 {
74 	int slot;
75 	if (!global_dca)
76 		return -ENODEV;
77 
78 	spin_lock(&dca_lock);
79 	slot = global_dca->ops->remove_requester(global_dca, dev);
80 	spin_unlock(&dca_lock);
81 	if (slot < 0)
82 		return slot;
83 
84 	dca_sysfs_remove_req(global_dca, slot);
85 	return 0;
86 }
87 EXPORT_SYMBOL_GPL(dca_remove_requester);
88 
89 /**
90  * dca_get_tag - return the dca tag for the given cpu
91  * @cpu - the cpuid as returned by get_cpu()
92  */
93 u8 dca_get_tag(int cpu)
94 {
95 	if (!global_dca)
96 		return -ENODEV;
97 	return global_dca->ops->get_tag(global_dca, cpu);
98 }
99 EXPORT_SYMBOL_GPL(dca_get_tag);
100 
101 /**
102  * alloc_dca_provider - get data struct for describing a dca provider
103  * @ops - pointer to struct of dca operation function pointers
104  * @priv_size - size of extra mem to be added for provider's needs
105  */
106 struct dca_provider *alloc_dca_provider(struct dca_ops *ops, int priv_size)
107 {
108 	struct dca_provider *dca;
109 	int alloc_size;
110 
111 	alloc_size = (sizeof(*dca) + priv_size);
112 	dca = kzalloc(alloc_size, GFP_KERNEL);
113 	if (!dca)
114 		return NULL;
115 	dca->ops = ops;
116 
117 	return dca;
118 }
119 EXPORT_SYMBOL_GPL(alloc_dca_provider);
120 
121 /**
122  * free_dca_provider - release the dca provider data struct
123  * @ops - pointer to struct of dca operation function pointers
124  * @priv_size - size of extra mem to be added for provider's needs
125  */
126 void free_dca_provider(struct dca_provider *dca)
127 {
128 	kfree(dca);
129 }
130 EXPORT_SYMBOL_GPL(free_dca_provider);
131 
132 static BLOCKING_NOTIFIER_HEAD(dca_provider_chain);
133 
134 /**
135  * register_dca_provider - register a dca provider
136  * @dca - struct created by alloc_dca_provider()
137  * @dev - device providing dca services
138  */
139 int register_dca_provider(struct dca_provider *dca, struct device *dev)
140 {
141 	int err;
142 
143 	if (global_dca)
144 		return -EEXIST;
145 	err = dca_sysfs_add_provider(dca, dev);
146 	if (err)
147 		return err;
148 	global_dca = dca;
149 	blocking_notifier_call_chain(&dca_provider_chain,
150 				     DCA_PROVIDER_ADD, NULL);
151 	return 0;
152 }
153 EXPORT_SYMBOL_GPL(register_dca_provider);
154 
155 /**
156  * unregister_dca_provider - remove a dca provider
157  * @dca - struct created by alloc_dca_provider()
158  */
159 void unregister_dca_provider(struct dca_provider *dca)
160 {
161 	if (!global_dca)
162 		return;
163 	blocking_notifier_call_chain(&dca_provider_chain,
164 				     DCA_PROVIDER_REMOVE, NULL);
165 	global_dca = NULL;
166 	dca_sysfs_remove_provider(dca);
167 }
168 EXPORT_SYMBOL_GPL(unregister_dca_provider);
169 
170 /**
171  * dca_register_notify - register a client's notifier callback
172  */
173 void dca_register_notify(struct notifier_block *nb)
174 {
175 	blocking_notifier_chain_register(&dca_provider_chain, nb);
176 }
177 EXPORT_SYMBOL_GPL(dca_register_notify);
178 
179 /**
180  * dca_unregister_notify - remove a client's notifier callback
181  */
182 void dca_unregister_notify(struct notifier_block *nb)
183 {
184 	blocking_notifier_chain_unregister(&dca_provider_chain, nb);
185 }
186 EXPORT_SYMBOL_GPL(dca_unregister_notify);
187 
188 static int __init dca_init(void)
189 {
190 	return dca_sysfs_init();
191 }
192 
193 static void __exit dca_exit(void)
194 {
195 	dca_sysfs_exit();
196 }
197 
198 module_init(dca_init);
199 module_exit(dca_exit);
200 
201