blob: 52e6bb70a490d8bebf26745191bcf80ce5338ca2 [file] [log] [blame]
Shannon Nelson75896702007-10-16 01:27:41 -07001/*
Maciej Sosnowski211a22c2009-02-26 11:05:43 +01002 * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
Shannon Nelson75896702007-10-16 01:27:41 -07003 *
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
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +020031#define DCA_VERSION "1.12.1"
Shannon Nelson75896702007-10-16 01:27:41 -070032
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -070033MODULE_VERSION(DCA_VERSION);
34MODULE_LICENSE("GPL");
35MODULE_AUTHOR("Intel Corporation");
Shannon Nelson75896702007-10-16 01:27:41 -070036
37static DEFINE_SPINLOCK(dca_lock);
38
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +020039static LIST_HEAD(dca_domains);
40
41static struct pci_bus *dca_pci_rc_from_dev(struct device *dev)
42{
43 struct pci_dev *pdev = to_pci_dev(dev);
44 struct pci_bus *bus = pdev->bus;
45
46 while (bus->parent)
47 bus = bus->parent;
48
49 return bus;
50}
51
52static struct dca_domain *dca_allocate_domain(struct pci_bus *rc)
53{
54 struct dca_domain *domain;
55
56 domain = kzalloc(sizeof(*domain), GFP_NOWAIT);
57 if (!domain)
58 return NULL;
59
60 INIT_LIST_HEAD(&domain->dca_providers);
61 domain->pci_rc = rc;
62
63 return domain;
64}
65
66static void dca_free_domain(struct dca_domain *domain)
67{
68 list_del(&domain->node);
69 kfree(domain);
70}
71
72static struct dca_domain *dca_find_domain(struct pci_bus *rc)
73{
74 struct dca_domain *domain;
75
76 list_for_each_entry(domain, &dca_domains, node)
77 if (domain->pci_rc == rc)
78 return domain;
79
80 return NULL;
81}
82
83static struct dca_domain *dca_get_domain(struct device *dev)
84{
85 struct pci_bus *rc;
86 struct dca_domain *domain;
87
88 rc = dca_pci_rc_from_dev(dev);
89 domain = dca_find_domain(rc);
90
91 if (!domain) {
92 domain = dca_allocate_domain(rc);
93 if (domain)
94 list_add(&domain->node, &dca_domains);
95 }
96
97 return domain;
98}
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -070099
100static struct dca_provider *dca_find_provider_by_dev(struct device *dev)
101{
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200102 struct dca_provider *dca;
103 struct pci_bus *rc;
104 struct dca_domain *domain;
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700105
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200106 if (dev) {
107 rc = dca_pci_rc_from_dev(dev);
108 domain = dca_find_domain(rc);
109 if (!domain)
110 return NULL;
111 } else {
112 if (!list_empty(&dca_domains))
113 domain = list_first_entry(&dca_domains,
114 struct dca_domain,
115 node);
116 else
117 return NULL;
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700118 }
119
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200120 list_for_each_entry(dca, &domain->dca_providers, node)
121 if ((!dev) || (dca->ops->dev_managed(dca, dev)))
122 return dca;
123
124 return NULL;
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700125}
Shannon Nelson75896702007-10-16 01:27:41 -0700126
127/**
128 * dca_add_requester - add a dca client to the list
129 * @dev - the device that wants dca service
130 */
131int dca_add_requester(struct device *dev)
132{
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700133 struct dca_provider *dca;
134 int err, slot = -ENODEV;
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800135 unsigned long flags;
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200136 struct pci_bus *pci_rc;
137 struct dca_domain *domain;
Shannon Nelson75896702007-10-16 01:27:41 -0700138
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700139 if (!dev)
140 return -EFAULT;
Shannon Nelson75896702007-10-16 01:27:41 -0700141
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800142 spin_lock_irqsave(&dca_lock, flags);
Shannon Nelson75896702007-10-16 01:27:41 -0700143
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700144 /* check if the requester has not been added already */
145 dca = dca_find_provider_by_dev(dev);
146 if (dca) {
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800147 spin_unlock_irqrestore(&dca_lock, flags);
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700148 return -EEXIST;
149 }
150
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200151 pci_rc = dca_pci_rc_from_dev(dev);
152 domain = dca_find_domain(pci_rc);
153 if (!domain) {
154 spin_unlock_irqrestore(&dca_lock, flags);
155 return -ENODEV;
156 }
157
158 list_for_each_entry(dca, &domain->dca_providers, node) {
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700159 slot = dca->ops->add_requester(dca, dev);
160 if (slot >= 0)
161 break;
162 }
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800163
164 spin_unlock_irqrestore(&dca_lock, flags);
165
166 if (slot < 0)
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700167 return slot;
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700168
169 err = dca_sysfs_add_req(dca, dev, slot);
Shannon Nelson75896702007-10-16 01:27:41 -0700170 if (err) {
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800171 spin_lock_irqsave(&dca_lock, flags);
172 if (dca == dca_find_provider_by_dev(dev))
173 dca->ops->remove_requester(dca, dev);
174 spin_unlock_irqrestore(&dca_lock, flags);
Shannon Nelson75896702007-10-16 01:27:41 -0700175 return err;
176 }
177
178 return 0;
179}
180EXPORT_SYMBOL_GPL(dca_add_requester);
181
182/**
183 * dca_remove_requester - remove a dca client from the list
184 * @dev - the device that wants dca service
185 */
186int dca_remove_requester(struct device *dev)
187{
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700188 struct dca_provider *dca;
Shannon Nelson75896702007-10-16 01:27:41 -0700189 int slot;
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800190 unsigned long flags;
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700191
192 if (!dev)
193 return -EFAULT;
Shannon Nelson75896702007-10-16 01:27:41 -0700194
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800195 spin_lock_irqsave(&dca_lock, flags);
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700196 dca = dca_find_provider_by_dev(dev);
197 if (!dca) {
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800198 spin_unlock_irqrestore(&dca_lock, flags);
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700199 return -ENODEV;
200 }
201 slot = dca->ops->remove_requester(dca, dev);
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800202 spin_unlock_irqrestore(&dca_lock, flags);
203
204 if (slot < 0)
Shannon Nelson75896702007-10-16 01:27:41 -0700205 return slot;
206
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700207 dca_sysfs_remove_req(dca, slot);
208
Shannon Nelson75896702007-10-16 01:27:41 -0700209 return 0;
210}
211EXPORT_SYMBOL_GPL(dca_remove_requester);
212
213/**
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700214 * dca_common_get_tag - return the dca tag (serves both new and old api)
215 * @dev - the device that wants dca service
216 * @cpu - the cpuid as returned by get_cpu()
217 */
218u8 dca_common_get_tag(struct device *dev, int cpu)
219{
220 struct dca_provider *dca;
221 u8 tag;
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800222 unsigned long flags;
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700223
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800224 spin_lock_irqsave(&dca_lock, flags);
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700225
226 dca = dca_find_provider_by_dev(dev);
227 if (!dca) {
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800228 spin_unlock_irqrestore(&dca_lock, flags);
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700229 return -ENODEV;
230 }
231 tag = dca->ops->get_tag(dca, dev, cpu);
232
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800233 spin_unlock_irqrestore(&dca_lock, flags);
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700234 return tag;
235}
236
237/**
238 * dca3_get_tag - return the dca tag to the requester device
239 * for the given cpu (new api)
240 * @dev - the device that wants dca service
241 * @cpu - the cpuid as returned by get_cpu()
242 */
243u8 dca3_get_tag(struct device *dev, int cpu)
244{
245 if (!dev)
246 return -EFAULT;
247
248 return dca_common_get_tag(dev, cpu);
249}
250EXPORT_SYMBOL_GPL(dca3_get_tag);
251
252/**
253 * dca_get_tag - return the dca tag for the given cpu (old api)
Shannon Nelson75896702007-10-16 01:27:41 -0700254 * @cpu - the cpuid as returned by get_cpu()
255 */
256u8 dca_get_tag(int cpu)
257{
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700258 struct device *dev = NULL;
259
260 return dca_common_get_tag(dev, cpu);
Shannon Nelson75896702007-10-16 01:27:41 -0700261}
262EXPORT_SYMBOL_GPL(dca_get_tag);
263
264/**
265 * alloc_dca_provider - get data struct for describing a dca provider
266 * @ops - pointer to struct of dca operation function pointers
267 * @priv_size - size of extra mem to be added for provider's needs
268 */
269struct dca_provider *alloc_dca_provider(struct dca_ops *ops, int priv_size)
270{
271 struct dca_provider *dca;
272 int alloc_size;
273
274 alloc_size = (sizeof(*dca) + priv_size);
275 dca = kzalloc(alloc_size, GFP_KERNEL);
276 if (!dca)
277 return NULL;
278 dca->ops = ops;
279
280 return dca;
281}
282EXPORT_SYMBOL_GPL(alloc_dca_provider);
283
284/**
285 * free_dca_provider - release the dca provider data struct
286 * @ops - pointer to struct of dca operation function pointers
287 * @priv_size - size of extra mem to be added for provider's needs
288 */
289void free_dca_provider(struct dca_provider *dca)
290{
291 kfree(dca);
292}
293EXPORT_SYMBOL_GPL(free_dca_provider);
294
295static BLOCKING_NOTIFIER_HEAD(dca_provider_chain);
296
297/**
298 * register_dca_provider - register a dca provider
299 * @dca - struct created by alloc_dca_provider()
300 * @dev - device providing dca services
301 */
302int register_dca_provider(struct dca_provider *dca, struct device *dev)
303{
304 int err;
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800305 unsigned long flags;
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200306 struct dca_domain *domain;
Shannon Nelson75896702007-10-16 01:27:41 -0700307
Shannon Nelson75896702007-10-16 01:27:41 -0700308 err = dca_sysfs_add_provider(dca, dev);
309 if (err)
310 return err;
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800311
312 spin_lock_irqsave(&dca_lock, flags);
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200313 domain = dca_get_domain(dev);
314 if (!domain) {
315 spin_unlock_irqrestore(&dca_lock, flags);
316 return -ENODEV;
317 }
318 list_add(&dca->node, &domain->dca_providers);
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800319 spin_unlock_irqrestore(&dca_lock, flags);
320
Shannon Nelson75896702007-10-16 01:27:41 -0700321 blocking_notifier_call_chain(&dca_provider_chain,
322 DCA_PROVIDER_ADD, NULL);
323 return 0;
324}
325EXPORT_SYMBOL_GPL(register_dca_provider);
326
327/**
328 * unregister_dca_provider - remove a dca provider
329 * @dca - struct created by alloc_dca_provider()
330 */
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200331void unregister_dca_provider(struct dca_provider *dca, struct device *dev)
Shannon Nelson75896702007-10-16 01:27:41 -0700332{
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800333 unsigned long flags;
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200334 struct pci_bus *pci_rc;
335 struct dca_domain *domain;
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800336
Shannon Nelson75896702007-10-16 01:27:41 -0700337 blocking_notifier_call_chain(&dca_provider_chain,
338 DCA_PROVIDER_REMOVE, NULL);
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800339
340 spin_lock_irqsave(&dca_lock, flags);
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200341
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700342 list_del(&dca->node);
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200343
344 pci_rc = dca_pci_rc_from_dev(dev);
345 domain = dca_find_domain(pci_rc);
346 if (list_empty(&domain->dca_providers))
347 dca_free_domain(domain);
348
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800349 spin_unlock_irqrestore(&dca_lock, flags);
350
Shannon Nelson75896702007-10-16 01:27:41 -0700351 dca_sysfs_remove_provider(dca);
352}
353EXPORT_SYMBOL_GPL(unregister_dca_provider);
354
355/**
356 * dca_register_notify - register a client's notifier callback
357 */
358void dca_register_notify(struct notifier_block *nb)
359{
360 blocking_notifier_chain_register(&dca_provider_chain, nb);
361}
362EXPORT_SYMBOL_GPL(dca_register_notify);
363
364/**
365 * dca_unregister_notify - remove a client's notifier callback
366 */
367void dca_unregister_notify(struct notifier_block *nb)
368{
369 blocking_notifier_chain_unregister(&dca_provider_chain, nb);
370}
371EXPORT_SYMBOL_GPL(dca_unregister_notify);
372
373static int __init dca_init(void)
374{
Stephen Hemminger084dac52009-09-13 09:07:37 -0700375 pr_info("dca service started, version %s\n", DCA_VERSION);
Shannon Nelson75896702007-10-16 01:27:41 -0700376 return dca_sysfs_init();
377}
378
379static void __exit dca_exit(void)
380{
381 dca_sysfs_exit();
382}
383
Dan Williams652afc22009-01-06 11:38:22 -0700384arch_initcall(dca_init);
Shannon Nelson75896702007-10-16 01:27:41 -0700385module_exit(dca_exit);
386