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 struct pci_bus
*dca_pci_rc_from_dev(struct device
*dev
)
44 struct pci_dev
*pdev
= to_pci_dev(dev
);
45 struct pci_bus
*bus
= pdev
->bus
;
53 static struct dca_domain
*dca_allocate_domain(struct pci_bus
*rc
)
55 struct dca_domain
*domain
;
57 domain
= kzalloc(sizeof(*domain
), GFP_NOWAIT
);
61 INIT_LIST_HEAD(&domain
->dca_providers
);
67 static void dca_free_domain(struct dca_domain
*domain
)
69 list_del(&domain
->node
);
73 static struct dca_domain
*dca_find_domain(struct pci_bus
*rc
)
75 struct dca_domain
*domain
;
77 list_for_each_entry(domain
, &dca_domains
, node
)
78 if (domain
->pci_rc
== rc
)
84 static struct dca_domain
*dca_get_domain(struct device
*dev
)
87 struct dca_domain
*domain
;
89 rc
= dca_pci_rc_from_dev(dev
);
90 domain
= dca_find_domain(rc
);
93 domain
= dca_allocate_domain(rc
);
95 list_add(&domain
->node
, &dca_domains
);
101 static struct dca_provider
*dca_find_provider_by_dev(struct device
*dev
)
103 struct dca_provider
*dca
;
105 struct dca_domain
*domain
;
108 rc
= dca_pci_rc_from_dev(dev
);
109 domain
= dca_find_domain(rc
);
113 if (!list_empty(&dca_domains
))
114 domain
= list_first_entry(&dca_domains
,
121 list_for_each_entry(dca
, &domain
->dca_providers
, node
)
122 if ((!dev
) || (dca
->ops
->dev_managed(dca
, dev
)))
129 * dca_add_requester - add a dca client to the list
130 * @dev - the device that wants dca service
132 int dca_add_requester(struct device
*dev
)
134 struct dca_provider
*dca
;
135 int err
, slot
= -ENODEV
;
137 struct pci_bus
*pci_rc
;
138 struct dca_domain
*domain
;
143 spin_lock_irqsave(&dca_lock
, flags
);
145 /* check if the requester has not been added already */
146 dca
= dca_find_provider_by_dev(dev
);
148 spin_unlock_irqrestore(&dca_lock
, flags
);
152 pci_rc
= dca_pci_rc_from_dev(dev
);
153 domain
= dca_find_domain(pci_rc
);
155 spin_unlock_irqrestore(&dca_lock
, flags
);
159 list_for_each_entry(dca
, &domain
->dca_providers
, node
) {
160 slot
= dca
->ops
->add_requester(dca
, dev
);
165 spin_unlock_irqrestore(&dca_lock
, flags
);
170 err
= dca_sysfs_add_req(dca
, dev
, slot
);
172 spin_lock_irqsave(&dca_lock
, flags
);
173 if (dca
== dca_find_provider_by_dev(dev
))
174 dca
->ops
->remove_requester(dca
, dev
);
175 spin_unlock_irqrestore(&dca_lock
, flags
);
181 EXPORT_SYMBOL_GPL(dca_add_requester
);
184 * dca_remove_requester - remove a dca client from the list
185 * @dev - the device that wants dca service
187 int dca_remove_requester(struct device
*dev
)
189 struct dca_provider
*dca
;
196 spin_lock_irqsave(&dca_lock
, flags
);
197 dca
= dca_find_provider_by_dev(dev
);
199 spin_unlock_irqrestore(&dca_lock
, flags
);
202 slot
= dca
->ops
->remove_requester(dca
, dev
);
203 spin_unlock_irqrestore(&dca_lock
, flags
);
208 dca_sysfs_remove_req(dca
, slot
);
212 EXPORT_SYMBOL_GPL(dca_remove_requester
);
215 * dca_common_get_tag - return the dca tag (serves both new and old api)
216 * @dev - the device that wants dca service
217 * @cpu - the cpuid as returned by get_cpu()
219 u8
dca_common_get_tag(struct device
*dev
, int cpu
)
221 struct dca_provider
*dca
;
225 spin_lock_irqsave(&dca_lock
, flags
);
227 dca
= dca_find_provider_by_dev(dev
);
229 spin_unlock_irqrestore(&dca_lock
, flags
);
232 tag
= dca
->ops
->get_tag(dca
, dev
, cpu
);
234 spin_unlock_irqrestore(&dca_lock
, flags
);
239 * dca3_get_tag - return the dca tag to the requester device
240 * for the given cpu (new api)
241 * @dev - the device that wants dca service
242 * @cpu - the cpuid as returned by get_cpu()
244 u8
dca3_get_tag(struct device
*dev
, int cpu
)
249 return dca_common_get_tag(dev
, cpu
);
251 EXPORT_SYMBOL_GPL(dca3_get_tag
);
254 * dca_get_tag - return the dca tag for the given cpu (old api)
255 * @cpu - the cpuid as returned by get_cpu()
257 u8
dca_get_tag(int cpu
)
259 struct device
*dev
= NULL
;
261 return dca_common_get_tag(dev
, cpu
);
263 EXPORT_SYMBOL_GPL(dca_get_tag
);
266 * alloc_dca_provider - get data struct for describing a dca provider
267 * @ops - pointer to struct of dca operation function pointers
268 * @priv_size - size of extra mem to be added for provider's needs
270 struct dca_provider
*alloc_dca_provider(struct dca_ops
*ops
, int priv_size
)
272 struct dca_provider
*dca
;
275 alloc_size
= (sizeof(*dca
) + priv_size
);
276 dca
= kzalloc(alloc_size
, GFP_KERNEL
);
283 EXPORT_SYMBOL_GPL(alloc_dca_provider
);
286 * free_dca_provider - release the dca provider data struct
287 * @ops - pointer to struct of dca operation function pointers
288 * @priv_size - size of extra mem to be added for provider's needs
290 void free_dca_provider(struct dca_provider
*dca
)
294 EXPORT_SYMBOL_GPL(free_dca_provider
);
296 static BLOCKING_NOTIFIER_HEAD(dca_provider_chain
);
299 * register_dca_provider - register a dca provider
300 * @dca - struct created by alloc_dca_provider()
301 * @dev - device providing dca services
303 int register_dca_provider(struct dca_provider
*dca
, struct device
*dev
)
307 struct dca_domain
*domain
;
309 err
= dca_sysfs_add_provider(dca
, dev
);
313 spin_lock_irqsave(&dca_lock
, flags
);
314 domain
= dca_get_domain(dev
);
316 spin_unlock_irqrestore(&dca_lock
, flags
);
319 list_add(&dca
->node
, &domain
->dca_providers
);
320 spin_unlock_irqrestore(&dca_lock
, flags
);
322 blocking_notifier_call_chain(&dca_provider_chain
,
323 DCA_PROVIDER_ADD
, NULL
);
326 EXPORT_SYMBOL_GPL(register_dca_provider
);
329 * unregister_dca_provider - remove a dca provider
330 * @dca - struct created by alloc_dca_provider()
332 void unregister_dca_provider(struct dca_provider
*dca
, struct device
*dev
)
335 struct pci_bus
*pci_rc
;
336 struct dca_domain
*domain
;
338 blocking_notifier_call_chain(&dca_provider_chain
,
339 DCA_PROVIDER_REMOVE
, NULL
);
341 spin_lock_irqsave(&dca_lock
, flags
);
343 list_del(&dca
->node
);
345 pci_rc
= dca_pci_rc_from_dev(dev
);
346 domain
= dca_find_domain(pci_rc
);
347 if (list_empty(&domain
->dca_providers
))
348 dca_free_domain(domain
);
350 spin_unlock_irqrestore(&dca_lock
, flags
);
352 dca_sysfs_remove_provider(dca
);
354 EXPORT_SYMBOL_GPL(unregister_dca_provider
);
357 * dca_register_notify - register a client's notifier callback
359 void dca_register_notify(struct notifier_block
*nb
)
361 blocking_notifier_chain_register(&dca_provider_chain
, nb
);
363 EXPORT_SYMBOL_GPL(dca_register_notify
);
366 * dca_unregister_notify - remove a client's notifier callback
368 void dca_unregister_notify(struct notifier_block
*nb
)
370 blocking_notifier_chain_unregister(&dca_provider_chain
, nb
);
372 EXPORT_SYMBOL_GPL(dca_unregister_notify
);
374 static int __init
dca_init(void)
376 pr_info("dca service started, version %s\n", DCA_VERSION
);
377 return dca_sysfs_init();
380 static void __exit
dca_exit(void)
385 arch_initcall(dca_init
);
386 module_exit(dca_exit
);