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>
31 #include <linux/module.h>
33 #define DCA_VERSION "1.12.1"
35 MODULE_VERSION(DCA_VERSION
);
36 MODULE_LICENSE("GPL");
37 MODULE_AUTHOR("Intel Corporation");
39 static DEFINE_RAW_SPINLOCK(dca_lock
);
41 static LIST_HEAD(dca_domains
);
43 static BLOCKING_NOTIFIER_HEAD(dca_provider_chain
);
45 static int dca_providers_blocked
;
47 static struct pci_bus
*dca_pci_rc_from_dev(struct device
*dev
)
49 struct pci_dev
*pdev
= to_pci_dev(dev
);
50 struct pci_bus
*bus
= pdev
->bus
;
58 static struct dca_domain
*dca_allocate_domain(struct pci_bus
*rc
)
60 struct dca_domain
*domain
;
62 domain
= kzalloc(sizeof(*domain
), GFP_NOWAIT
);
66 INIT_LIST_HEAD(&domain
->dca_providers
);
72 static void dca_free_domain(struct dca_domain
*domain
)
74 list_del(&domain
->node
);
78 static int dca_provider_ioat_ver_3_0(struct device
*dev
)
80 struct pci_dev
*pdev
= to_pci_dev(dev
);
82 return ((pdev
->vendor
== PCI_VENDOR_ID_INTEL
) &&
83 ((pdev
->device
== PCI_DEVICE_ID_INTEL_IOAT_TBG0
) ||
84 (pdev
->device
== PCI_DEVICE_ID_INTEL_IOAT_TBG1
) ||
85 (pdev
->device
== PCI_DEVICE_ID_INTEL_IOAT_TBG2
) ||
86 (pdev
->device
== PCI_DEVICE_ID_INTEL_IOAT_TBG3
) ||
87 (pdev
->device
== PCI_DEVICE_ID_INTEL_IOAT_TBG4
) ||
88 (pdev
->device
== PCI_DEVICE_ID_INTEL_IOAT_TBG5
) ||
89 (pdev
->device
== PCI_DEVICE_ID_INTEL_IOAT_TBG6
) ||
90 (pdev
->device
== PCI_DEVICE_ID_INTEL_IOAT_TBG7
)));
93 static void unregister_dca_providers(void)
95 struct dca_provider
*dca
, *_dca
;
96 struct list_head unregistered_providers
;
97 struct dca_domain
*domain
;
100 blocking_notifier_call_chain(&dca_provider_chain
,
101 DCA_PROVIDER_REMOVE
, NULL
);
103 INIT_LIST_HEAD(&unregistered_providers
);
105 raw_spin_lock_irqsave(&dca_lock
, flags
);
107 if (list_empty(&dca_domains
)) {
108 raw_spin_unlock_irqrestore(&dca_lock
, flags
);
112 /* at this point only one domain in the list is expected */
113 domain
= list_first_entry(&dca_domains
, struct dca_domain
, node
);
115 list_for_each_entry_safe(dca
, _dca
, &domain
->dca_providers
, node
)
116 list_move(&dca
->node
, &unregistered_providers
);
118 dca_free_domain(domain
);
120 raw_spin_unlock_irqrestore(&dca_lock
, flags
);
122 list_for_each_entry_safe(dca
, _dca
, &unregistered_providers
, node
) {
123 dca_sysfs_remove_provider(dca
);
124 list_del(&dca
->node
);
128 static struct dca_domain
*dca_find_domain(struct pci_bus
*rc
)
130 struct dca_domain
*domain
;
132 list_for_each_entry(domain
, &dca_domains
, node
)
133 if (domain
->pci_rc
== rc
)
139 static struct dca_domain
*dca_get_domain(struct device
*dev
)
142 struct dca_domain
*domain
;
144 rc
= dca_pci_rc_from_dev(dev
);
145 domain
= dca_find_domain(rc
);
148 if (dca_provider_ioat_ver_3_0(dev
) && !list_empty(&dca_domains
))
149 dca_providers_blocked
= 1;
155 static struct dca_provider
*dca_find_provider_by_dev(struct device
*dev
)
157 struct dca_provider
*dca
;
159 struct dca_domain
*domain
;
162 rc
= dca_pci_rc_from_dev(dev
);
163 domain
= dca_find_domain(rc
);
167 if (!list_empty(&dca_domains
))
168 domain
= list_first_entry(&dca_domains
,
175 list_for_each_entry(dca
, &domain
->dca_providers
, node
)
176 if ((!dev
) || (dca
->ops
->dev_managed(dca
, dev
)))
183 * dca_add_requester - add a dca client to the list
184 * @dev - the device that wants dca service
186 int dca_add_requester(struct device
*dev
)
188 struct dca_provider
*dca
;
189 int err
, slot
= -ENODEV
;
191 struct pci_bus
*pci_rc
;
192 struct dca_domain
*domain
;
197 raw_spin_lock_irqsave(&dca_lock
, flags
);
199 /* check if the requester has not been added already */
200 dca
= dca_find_provider_by_dev(dev
);
202 raw_spin_unlock_irqrestore(&dca_lock
, flags
);
206 pci_rc
= dca_pci_rc_from_dev(dev
);
207 domain
= dca_find_domain(pci_rc
);
209 raw_spin_unlock_irqrestore(&dca_lock
, flags
);
213 list_for_each_entry(dca
, &domain
->dca_providers
, node
) {
214 slot
= dca
->ops
->add_requester(dca
, dev
);
219 raw_spin_unlock_irqrestore(&dca_lock
, flags
);
224 err
= dca_sysfs_add_req(dca
, dev
, slot
);
226 raw_spin_lock_irqsave(&dca_lock
, flags
);
227 if (dca
== dca_find_provider_by_dev(dev
))
228 dca
->ops
->remove_requester(dca
, dev
);
229 raw_spin_unlock_irqrestore(&dca_lock
, flags
);
235 EXPORT_SYMBOL_GPL(dca_add_requester
);
238 * dca_remove_requester - remove a dca client from the list
239 * @dev - the device that wants dca service
241 int dca_remove_requester(struct device
*dev
)
243 struct dca_provider
*dca
;
250 raw_spin_lock_irqsave(&dca_lock
, flags
);
251 dca
= dca_find_provider_by_dev(dev
);
253 raw_spin_unlock_irqrestore(&dca_lock
, flags
);
256 slot
= dca
->ops
->remove_requester(dca
, dev
);
257 raw_spin_unlock_irqrestore(&dca_lock
, flags
);
262 dca_sysfs_remove_req(dca
, slot
);
266 EXPORT_SYMBOL_GPL(dca_remove_requester
);
269 * dca_common_get_tag - return the dca tag (serves both new and old api)
270 * @dev - the device that wants dca service
271 * @cpu - the cpuid as returned by get_cpu()
273 u8
dca_common_get_tag(struct device
*dev
, int cpu
)
275 struct dca_provider
*dca
;
279 raw_spin_lock_irqsave(&dca_lock
, flags
);
281 dca
= dca_find_provider_by_dev(dev
);
283 raw_spin_unlock_irqrestore(&dca_lock
, flags
);
286 tag
= dca
->ops
->get_tag(dca
, dev
, cpu
);
288 raw_spin_unlock_irqrestore(&dca_lock
, flags
);
293 * dca3_get_tag - return the dca tag to the requester device
294 * for the given cpu (new api)
295 * @dev - the device that wants dca service
296 * @cpu - the cpuid as returned by get_cpu()
298 u8
dca3_get_tag(struct device
*dev
, int cpu
)
303 return dca_common_get_tag(dev
, cpu
);
305 EXPORT_SYMBOL_GPL(dca3_get_tag
);
308 * dca_get_tag - return the dca tag for the given cpu (old api)
309 * @cpu - the cpuid as returned by get_cpu()
311 u8
dca_get_tag(int cpu
)
313 struct device
*dev
= NULL
;
315 return dca_common_get_tag(dev
, cpu
);
317 EXPORT_SYMBOL_GPL(dca_get_tag
);
320 * alloc_dca_provider - get data struct for describing a dca provider
321 * @ops - pointer to struct of dca operation function pointers
322 * @priv_size - size of extra mem to be added for provider's needs
324 struct dca_provider
*alloc_dca_provider(struct dca_ops
*ops
, int priv_size
)
326 struct dca_provider
*dca
;
329 alloc_size
= (sizeof(*dca
) + priv_size
);
330 dca
= kzalloc(alloc_size
, GFP_KERNEL
);
337 EXPORT_SYMBOL_GPL(alloc_dca_provider
);
340 * free_dca_provider - release the dca provider data struct
341 * @ops - pointer to struct of dca operation function pointers
342 * @priv_size - size of extra mem to be added for provider's needs
344 void free_dca_provider(struct dca_provider
*dca
)
348 EXPORT_SYMBOL_GPL(free_dca_provider
);
351 * register_dca_provider - register a dca provider
352 * @dca - struct created by alloc_dca_provider()
353 * @dev - device providing dca services
355 int register_dca_provider(struct dca_provider
*dca
, struct device
*dev
)
359 struct dca_domain
*domain
, *newdomain
= NULL
;
361 raw_spin_lock_irqsave(&dca_lock
, flags
);
362 if (dca_providers_blocked
) {
363 raw_spin_unlock_irqrestore(&dca_lock
, flags
);
366 raw_spin_unlock_irqrestore(&dca_lock
, flags
);
368 err
= dca_sysfs_add_provider(dca
, dev
);
372 raw_spin_lock_irqsave(&dca_lock
, flags
);
373 domain
= dca_get_domain(dev
);
377 if (dca_providers_blocked
) {
378 raw_spin_unlock_irqrestore(&dca_lock
, flags
);
379 dca_sysfs_remove_provider(dca
);
380 unregister_dca_providers();
384 raw_spin_unlock_irqrestore(&dca_lock
, flags
);
385 rc
= dca_pci_rc_from_dev(dev
);
386 newdomain
= dca_allocate_domain(rc
);
389 raw_spin_lock_irqsave(&dca_lock
, flags
);
390 /* Recheck, we might have raced after dropping the lock */
391 domain
= dca_get_domain(dev
);
395 list_add(&domain
->node
, &dca_domains
);
398 list_add(&dca
->node
, &domain
->dca_providers
);
399 raw_spin_unlock_irqrestore(&dca_lock
, flags
);
401 blocking_notifier_call_chain(&dca_provider_chain
,
402 DCA_PROVIDER_ADD
, NULL
);
406 EXPORT_SYMBOL_GPL(register_dca_provider
);
409 * unregister_dca_provider - remove a dca provider
410 * @dca - struct created by alloc_dca_provider()
412 void unregister_dca_provider(struct dca_provider
*dca
, struct device
*dev
)
415 struct pci_bus
*pci_rc
;
416 struct dca_domain
*domain
;
418 blocking_notifier_call_chain(&dca_provider_chain
,
419 DCA_PROVIDER_REMOVE
, NULL
);
421 raw_spin_lock_irqsave(&dca_lock
, flags
);
423 if (list_empty(&dca_domains
)) {
424 raw_spin_unlock_irqrestore(&dca_lock
, flags
);
428 list_del(&dca
->node
);
430 pci_rc
= dca_pci_rc_from_dev(dev
);
431 domain
= dca_find_domain(pci_rc
);
432 if (list_empty(&domain
->dca_providers
))
433 dca_free_domain(domain
);
435 raw_spin_unlock_irqrestore(&dca_lock
, flags
);
437 dca_sysfs_remove_provider(dca
);
439 EXPORT_SYMBOL_GPL(unregister_dca_provider
);
442 * dca_register_notify - register a client's notifier callback
444 void dca_register_notify(struct notifier_block
*nb
)
446 blocking_notifier_chain_register(&dca_provider_chain
, nb
);
448 EXPORT_SYMBOL_GPL(dca_register_notify
);
451 * dca_unregister_notify - remove a client's notifier callback
453 void dca_unregister_notify(struct notifier_block
*nb
)
455 blocking_notifier_chain_unregister(&dca_provider_chain
, nb
);
457 EXPORT_SYMBOL_GPL(dca_unregister_notify
);
459 static int __init
dca_init(void)
461 pr_info("dca service started, version %s\n", DCA_VERSION
);
462 return dca_sysfs_init();
465 static void __exit
dca_exit(void)
470 arch_initcall(dca_init
);
471 module_exit(dca_exit
);