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)
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
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_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;
57 static struct dca_domain *dca_allocate_domain(struct pci_bus *rc)
59 struct dca_domain *domain;
61 domain = kzalloc(sizeof(*domain), GFP_NOWAIT);
65 INIT_LIST_HEAD(&domain->dca_providers);
71 static void dca_free_domain(struct dca_domain *domain)
73 list_del(&domain->node);
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;
99 blocking_notifier_call_chain(&dca_provider_chain,
100 DCA_PROVIDER_REMOVE, NULL);
102 INIT_LIST_HEAD(&unregistered_providers);
104 spin_lock_irqsave(&dca_lock, flags);
106 if (list_empty(&dca_domains)) {
107 spin_unlock_irqrestore(&dca_lock, flags);
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 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)
138 static struct dca_domain *dca_get_domain(struct device *dev)
141 struct dca_domain *domain;
143 rc = dca_pci_rc_from_dev(dev);
144 domain = dca_find_domain(rc);
147 if (dca_provider_ioat_ver_3_0(dev) && !list_empty(&dca_domains)) {
148 dca_providers_blocked = 1;
150 domain = dca_allocate_domain(rc);
152 list_add(&domain->node, &dca_domains);
159 static struct dca_provider *dca_find_provider_by_dev(struct device *dev)
161 struct dca_provider *dca;
163 struct dca_domain *domain;
166 rc = dca_pci_rc_from_dev(dev);
167 domain = dca_find_domain(rc);
171 if (!list_empty(&dca_domains))
172 domain = list_first_entry(&dca_domains,
179 list_for_each_entry(dca, &domain->dca_providers, node)
180 if ((!dev) || (dca->ops->dev_managed(dca, dev)))
187 * dca_add_requester - add a dca client to the list
188 * @dev - the device that wants dca service
190 int dca_add_requester(struct device *dev)
192 struct dca_provider *dca;
193 int err, slot = -ENODEV;
195 struct pci_bus *pci_rc;
196 struct dca_domain *domain;
201 spin_lock_irqsave(&dca_lock, flags);
203 /* check if the requester has not been added already */
204 dca = dca_find_provider_by_dev(dev);
206 spin_unlock_irqrestore(&dca_lock, flags);
210 pci_rc = dca_pci_rc_from_dev(dev);
211 domain = dca_find_domain(pci_rc);
213 spin_unlock_irqrestore(&dca_lock, flags);
217 list_for_each_entry(dca, &domain->dca_providers, node) {
218 slot = dca->ops->add_requester(dca, dev);
223 spin_unlock_irqrestore(&dca_lock, flags);
228 err = dca_sysfs_add_req(dca, dev, slot);
230 spin_lock_irqsave(&dca_lock, flags);
231 if (dca == dca_find_provider_by_dev(dev))
232 dca->ops->remove_requester(dca, dev);
233 spin_unlock_irqrestore(&dca_lock, flags);
239 EXPORT_SYMBOL_GPL(dca_add_requester);
242 * dca_remove_requester - remove a dca client from the list
243 * @dev - the device that wants dca service
245 int dca_remove_requester(struct device *dev)
247 struct dca_provider *dca;
254 spin_lock_irqsave(&dca_lock, flags);
255 dca = dca_find_provider_by_dev(dev);
257 spin_unlock_irqrestore(&dca_lock, flags);
260 slot = dca->ops->remove_requester(dca, dev);
261 spin_unlock_irqrestore(&dca_lock, flags);
266 dca_sysfs_remove_req(dca, slot);
270 EXPORT_SYMBOL_GPL(dca_remove_requester);
273 * dca_common_get_tag - return the dca tag (serves both new and old api)
274 * @dev - the device that wants dca service
275 * @cpu - the cpuid as returned by get_cpu()
277 u8 dca_common_get_tag(struct device *dev, int cpu)
279 struct dca_provider *dca;
283 spin_lock_irqsave(&dca_lock, flags);
285 dca = dca_find_provider_by_dev(dev);
287 spin_unlock_irqrestore(&dca_lock, flags);
290 tag = dca->ops->get_tag(dca, dev, cpu);
292 spin_unlock_irqrestore(&dca_lock, flags);
297 * dca3_get_tag - return the dca tag to the requester device
298 * for the given cpu (new api)
299 * @dev - the device that wants dca service
300 * @cpu - the cpuid as returned by get_cpu()
302 u8 dca3_get_tag(struct device *dev, int cpu)
307 return dca_common_get_tag(dev, cpu);
309 EXPORT_SYMBOL_GPL(dca3_get_tag);
312 * dca_get_tag - return the dca tag for the given cpu (old api)
313 * @cpu - the cpuid as returned by get_cpu()
315 u8 dca_get_tag(int cpu)
317 struct device *dev = NULL;
319 return dca_common_get_tag(dev, cpu);
321 EXPORT_SYMBOL_GPL(dca_get_tag);
324 * alloc_dca_provider - get data struct for describing a dca provider
325 * @ops - pointer to struct of dca operation function pointers
326 * @priv_size - size of extra mem to be added for provider's needs
328 struct dca_provider *alloc_dca_provider(struct dca_ops *ops, int priv_size)
330 struct dca_provider *dca;
333 alloc_size = (sizeof(*dca) + priv_size);
334 dca = kzalloc(alloc_size, GFP_KERNEL);
341 EXPORT_SYMBOL_GPL(alloc_dca_provider);
344 * free_dca_provider - release the dca provider data struct
345 * @ops - pointer to struct of dca operation function pointers
346 * @priv_size - size of extra mem to be added for provider's needs
348 void free_dca_provider(struct dca_provider *dca)
352 EXPORT_SYMBOL_GPL(free_dca_provider);
355 * register_dca_provider - register a dca provider
356 * @dca - struct created by alloc_dca_provider()
357 * @dev - device providing dca services
359 int register_dca_provider(struct dca_provider *dca, struct device *dev)
363 struct dca_domain *domain;
365 spin_lock_irqsave(&dca_lock, flags);
366 if (dca_providers_blocked) {
367 spin_unlock_irqrestore(&dca_lock, flags);
370 spin_unlock_irqrestore(&dca_lock, flags);
372 err = dca_sysfs_add_provider(dca, dev);
376 spin_lock_irqsave(&dca_lock, flags);
377 domain = dca_get_domain(dev);
379 if (dca_providers_blocked) {
380 spin_unlock_irqrestore(&dca_lock, flags);
381 dca_sysfs_remove_provider(dca);
382 unregister_dca_providers();
384 spin_unlock_irqrestore(&dca_lock, flags);
388 list_add(&dca->node, &domain->dca_providers);
389 spin_unlock_irqrestore(&dca_lock, flags);
391 blocking_notifier_call_chain(&dca_provider_chain,
392 DCA_PROVIDER_ADD, NULL);
395 EXPORT_SYMBOL_GPL(register_dca_provider);
398 * unregister_dca_provider - remove a dca provider
399 * @dca - struct created by alloc_dca_provider()
401 void unregister_dca_provider(struct dca_provider *dca, struct device *dev)
404 struct pci_bus *pci_rc;
405 struct dca_domain *domain;
407 blocking_notifier_call_chain(&dca_provider_chain,
408 DCA_PROVIDER_REMOVE, NULL);
410 spin_lock_irqsave(&dca_lock, flags);
412 list_del(&dca->node);
414 pci_rc = dca_pci_rc_from_dev(dev);
415 domain = dca_find_domain(pci_rc);
416 if (list_empty(&domain->dca_providers))
417 dca_free_domain(domain);
419 spin_unlock_irqrestore(&dca_lock, flags);
421 dca_sysfs_remove_provider(dca);
423 EXPORT_SYMBOL_GPL(unregister_dca_provider);
426 * dca_register_notify - register a client's notifier callback
428 void dca_register_notify(struct notifier_block *nb)
430 blocking_notifier_chain_register(&dca_provider_chain, nb);
432 EXPORT_SYMBOL_GPL(dca_register_notify);
435 * dca_unregister_notify - remove a client's notifier callback
437 void dca_unregister_notify(struct notifier_block *nb)
439 blocking_notifier_chain_unregister(&dca_provider_chain, nb);
441 EXPORT_SYMBOL_GPL(dca_unregister_notify);
443 static int __init dca_init(void)
445 pr_info("dca service started, version %s\n", DCA_VERSION);
446 return dca_sysfs_init();
449 static void __exit dca_exit(void)
454 arch_initcall(dca_init);
455 module_exit(dca_exit);