Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-btrfs-devel.git] / drivers / dca / dca-core.c
blob25ec0bb05198f1587fb8687d057941c5f09c09d6
1 /*
2 * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
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.
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.
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.
18 * The full GNU General Public License is included in this distribution in the
19 * file called COPYING.
23 * This driver supports an interface for DCA clients and providers to meet.
26 #include <linux/kernel.h>
27 #include <linux/notifier.h>
28 #include <linux/device.h>
29 #include <linux/dca.h>
30 #include <linux/slab.h>
32 #define DCA_VERSION "1.12.1"
34 MODULE_VERSION(DCA_VERSION);
35 MODULE_LICENSE("GPL");
36 MODULE_AUTHOR("Intel Corporation");
38 static DEFINE_RAW_SPINLOCK(dca_lock);
40 static LIST_HEAD(dca_domains);
42 static BLOCKING_NOTIFIER_HEAD(dca_provider_chain);
44 static int dca_providers_blocked;
46 static struct pci_bus *dca_pci_rc_from_dev(struct device *dev)
48 struct pci_dev *pdev = to_pci_dev(dev);
49 struct pci_bus *bus = pdev->bus;
51 while (bus->parent)
52 bus = bus->parent;
54 return bus;
57 static struct dca_domain *dca_allocate_domain(struct pci_bus *rc)
59 struct dca_domain *domain;
61 domain = kzalloc(sizeof(*domain), GFP_NOWAIT);
62 if (!domain)
63 return NULL;
65 INIT_LIST_HEAD(&domain->dca_providers);
66 domain->pci_rc = rc;
68 return domain;
71 static void dca_free_domain(struct dca_domain *domain)
73 list_del(&domain->node);
74 kfree(domain);
77 static int dca_provider_ioat_ver_3_0(struct device *dev)
79 struct pci_dev *pdev = to_pci_dev(dev);
81 return ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
82 ((pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG0) ||
83 (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG1) ||
84 (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG2) ||
85 (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG3) ||
86 (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG4) ||
87 (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG5) ||
88 (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG6) ||
89 (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG7)));
92 static void unregister_dca_providers(void)
94 struct dca_provider *dca, *_dca;
95 struct list_head unregistered_providers;
96 struct dca_domain *domain;
97 unsigned long flags;
99 blocking_notifier_call_chain(&dca_provider_chain,
100 DCA_PROVIDER_REMOVE, NULL);
102 INIT_LIST_HEAD(&unregistered_providers);
104 raw_spin_lock_irqsave(&dca_lock, flags);
106 if (list_empty(&dca_domains)) {
107 raw_spin_unlock_irqrestore(&dca_lock, flags);
108 return;
111 /* at this point only one domain in the list is expected */
112 domain = list_first_entry(&dca_domains, struct dca_domain, node);
114 list_for_each_entry_safe(dca, _dca, &domain->dca_providers, node)
115 list_move(&dca->node, &unregistered_providers);
117 dca_free_domain(domain);
119 raw_spin_unlock_irqrestore(&dca_lock, flags);
121 list_for_each_entry_safe(dca, _dca, &unregistered_providers, node) {
122 dca_sysfs_remove_provider(dca);
123 list_del(&dca->node);
127 static struct dca_domain *dca_find_domain(struct pci_bus *rc)
129 struct dca_domain *domain;
131 list_for_each_entry(domain, &dca_domains, node)
132 if (domain->pci_rc == rc)
133 return domain;
135 return NULL;
138 static struct dca_domain *dca_get_domain(struct device *dev)
140 struct pci_bus *rc;
141 struct dca_domain *domain;
143 rc = dca_pci_rc_from_dev(dev);
144 domain = dca_find_domain(rc);
146 if (!domain) {
147 if (dca_provider_ioat_ver_3_0(dev) && !list_empty(&dca_domains))
148 dca_providers_blocked = 1;
151 return domain;
154 static struct dca_provider *dca_find_provider_by_dev(struct device *dev)
156 struct dca_provider *dca;
157 struct pci_bus *rc;
158 struct dca_domain *domain;
160 if (dev) {
161 rc = dca_pci_rc_from_dev(dev);
162 domain = dca_find_domain(rc);
163 if (!domain)
164 return NULL;
165 } else {
166 if (!list_empty(&dca_domains))
167 domain = list_first_entry(&dca_domains,
168 struct dca_domain,
169 node);
170 else
171 return NULL;
174 list_for_each_entry(dca, &domain->dca_providers, node)
175 if ((!dev) || (dca->ops->dev_managed(dca, dev)))
176 return dca;
178 return NULL;
182 * dca_add_requester - add a dca client to the list
183 * @dev - the device that wants dca service
185 int dca_add_requester(struct device *dev)
187 struct dca_provider *dca;
188 int err, slot = -ENODEV;
189 unsigned long flags;
190 struct pci_bus *pci_rc;
191 struct dca_domain *domain;
193 if (!dev)
194 return -EFAULT;
196 raw_spin_lock_irqsave(&dca_lock, flags);
198 /* check if the requester has not been added already */
199 dca = dca_find_provider_by_dev(dev);
200 if (dca) {
201 raw_spin_unlock_irqrestore(&dca_lock, flags);
202 return -EEXIST;
205 pci_rc = dca_pci_rc_from_dev(dev);
206 domain = dca_find_domain(pci_rc);
207 if (!domain) {
208 raw_spin_unlock_irqrestore(&dca_lock, flags);
209 return -ENODEV;
212 list_for_each_entry(dca, &domain->dca_providers, node) {
213 slot = dca->ops->add_requester(dca, dev);
214 if (slot >= 0)
215 break;
218 raw_spin_unlock_irqrestore(&dca_lock, flags);
220 if (slot < 0)
221 return slot;
223 err = dca_sysfs_add_req(dca, dev, slot);
224 if (err) {
225 raw_spin_lock_irqsave(&dca_lock, flags);
226 if (dca == dca_find_provider_by_dev(dev))
227 dca->ops->remove_requester(dca, dev);
228 raw_spin_unlock_irqrestore(&dca_lock, flags);
229 return err;
232 return 0;
234 EXPORT_SYMBOL_GPL(dca_add_requester);
237 * dca_remove_requester - remove a dca client from the list
238 * @dev - the device that wants dca service
240 int dca_remove_requester(struct device *dev)
242 struct dca_provider *dca;
243 int slot;
244 unsigned long flags;
246 if (!dev)
247 return -EFAULT;
249 raw_spin_lock_irqsave(&dca_lock, flags);
250 dca = dca_find_provider_by_dev(dev);
251 if (!dca) {
252 raw_spin_unlock_irqrestore(&dca_lock, flags);
253 return -ENODEV;
255 slot = dca->ops->remove_requester(dca, dev);
256 raw_spin_unlock_irqrestore(&dca_lock, flags);
258 if (slot < 0)
259 return slot;
261 dca_sysfs_remove_req(dca, slot);
263 return 0;
265 EXPORT_SYMBOL_GPL(dca_remove_requester);
268 * dca_common_get_tag - return the dca tag (serves both new and old api)
269 * @dev - the device that wants dca service
270 * @cpu - the cpuid as returned by get_cpu()
272 u8 dca_common_get_tag(struct device *dev, int cpu)
274 struct dca_provider *dca;
275 u8 tag;
276 unsigned long flags;
278 raw_spin_lock_irqsave(&dca_lock, flags);
280 dca = dca_find_provider_by_dev(dev);
281 if (!dca) {
282 raw_spin_unlock_irqrestore(&dca_lock, flags);
283 return -ENODEV;
285 tag = dca->ops->get_tag(dca, dev, cpu);
287 raw_spin_unlock_irqrestore(&dca_lock, flags);
288 return tag;
292 * dca3_get_tag - return the dca tag to the requester device
293 * for the given cpu (new api)
294 * @dev - the device that wants dca service
295 * @cpu - the cpuid as returned by get_cpu()
297 u8 dca3_get_tag(struct device *dev, int cpu)
299 if (!dev)
300 return -EFAULT;
302 return dca_common_get_tag(dev, cpu);
304 EXPORT_SYMBOL_GPL(dca3_get_tag);
307 * dca_get_tag - return the dca tag for the given cpu (old api)
308 * @cpu - the cpuid as returned by get_cpu()
310 u8 dca_get_tag(int cpu)
312 struct device *dev = NULL;
314 return dca_common_get_tag(dev, cpu);
316 EXPORT_SYMBOL_GPL(dca_get_tag);
319 * alloc_dca_provider - get data struct for describing a dca provider
320 * @ops - pointer to struct of dca operation function pointers
321 * @priv_size - size of extra mem to be added for provider's needs
323 struct dca_provider *alloc_dca_provider(struct dca_ops *ops, int priv_size)
325 struct dca_provider *dca;
326 int alloc_size;
328 alloc_size = (sizeof(*dca) + priv_size);
329 dca = kzalloc(alloc_size, GFP_KERNEL);
330 if (!dca)
331 return NULL;
332 dca->ops = ops;
334 return dca;
336 EXPORT_SYMBOL_GPL(alloc_dca_provider);
339 * free_dca_provider - release the dca provider data struct
340 * @ops - pointer to struct of dca operation function pointers
341 * @priv_size - size of extra mem to be added for provider's needs
343 void free_dca_provider(struct dca_provider *dca)
345 kfree(dca);
347 EXPORT_SYMBOL_GPL(free_dca_provider);
350 * register_dca_provider - register a dca provider
351 * @dca - struct created by alloc_dca_provider()
352 * @dev - device providing dca services
354 int register_dca_provider(struct dca_provider *dca, struct device *dev)
356 int err;
357 unsigned long flags;
358 struct dca_domain *domain, *newdomain = NULL;
360 raw_spin_lock_irqsave(&dca_lock, flags);
361 if (dca_providers_blocked) {
362 raw_spin_unlock_irqrestore(&dca_lock, flags);
363 return -ENODEV;
365 raw_spin_unlock_irqrestore(&dca_lock, flags);
367 err = dca_sysfs_add_provider(dca, dev);
368 if (err)
369 return err;
371 raw_spin_lock_irqsave(&dca_lock, flags);
372 domain = dca_get_domain(dev);
373 if (!domain) {
374 struct pci_bus *rc;
376 if (dca_providers_blocked) {
377 raw_spin_unlock_irqrestore(&dca_lock, flags);
378 dca_sysfs_remove_provider(dca);
379 unregister_dca_providers();
380 return -ENODEV;
383 raw_spin_unlock_irqrestore(&dca_lock, flags);
384 rc = dca_pci_rc_from_dev(dev);
385 newdomain = dca_allocate_domain(rc);
386 if (!newdomain)
387 return -ENODEV;
388 raw_spin_lock_irqsave(&dca_lock, flags);
389 /* Recheck, we might have raced after dropping the lock */
390 domain = dca_get_domain(dev);
391 if (!domain) {
392 domain = newdomain;
393 newdomain = NULL;
394 list_add(&domain->node, &dca_domains);
397 list_add(&dca->node, &domain->dca_providers);
398 raw_spin_unlock_irqrestore(&dca_lock, flags);
400 blocking_notifier_call_chain(&dca_provider_chain,
401 DCA_PROVIDER_ADD, NULL);
402 kfree(newdomain);
403 return 0;
405 EXPORT_SYMBOL_GPL(register_dca_provider);
408 * unregister_dca_provider - remove a dca provider
409 * @dca - struct created by alloc_dca_provider()
411 void unregister_dca_provider(struct dca_provider *dca, struct device *dev)
413 unsigned long flags;
414 struct pci_bus *pci_rc;
415 struct dca_domain *domain;
417 blocking_notifier_call_chain(&dca_provider_chain,
418 DCA_PROVIDER_REMOVE, NULL);
420 raw_spin_lock_irqsave(&dca_lock, flags);
422 list_del(&dca->node);
424 pci_rc = dca_pci_rc_from_dev(dev);
425 domain = dca_find_domain(pci_rc);
426 if (list_empty(&domain->dca_providers))
427 dca_free_domain(domain);
429 raw_spin_unlock_irqrestore(&dca_lock, flags);
431 dca_sysfs_remove_provider(dca);
433 EXPORT_SYMBOL_GPL(unregister_dca_provider);
436 * dca_register_notify - register a client's notifier callback
438 void dca_register_notify(struct notifier_block *nb)
440 blocking_notifier_chain_register(&dca_provider_chain, nb);
442 EXPORT_SYMBOL_GPL(dca_register_notify);
445 * dca_unregister_notify - remove a client's notifier callback
447 void dca_unregister_notify(struct notifier_block *nb)
449 blocking_notifier_chain_unregister(&dca_provider_chain, nb);
451 EXPORT_SYMBOL_GPL(dca_unregister_notify);
453 static int __init dca_init(void)
455 pr_info("dca service started, version %s\n", DCA_VERSION);
456 return dca_sysfs_init();
459 static void __exit dca_exit(void)
461 dca_sysfs_exit();
464 arch_initcall(dca_init);
465 module_exit(dca_exit);