blob: c461eda6241159ac52ee279bf681778d2f6a7390 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Shannon Nelson75896702007-10-16 01:27:41 -070031
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +020032#define DCA_VERSION "1.12.1"
Shannon Nelson75896702007-10-16 01:27:41 -070033
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -070034MODULE_VERSION(DCA_VERSION);
35MODULE_LICENSE("GPL");
36MODULE_AUTHOR("Intel Corporation");
Shannon Nelson75896702007-10-16 01:27:41 -070037
38static DEFINE_SPINLOCK(dca_lock);
39
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +020040static LIST_HEAD(dca_domains);
41
Sosnowski, Maciej4e8cec22010-09-16 06:02:26 +000042static BLOCKING_NOTIFIER_HEAD(dca_provider_chain);
43
44static int dca_providers_blocked;
45
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +020046static struct pci_bus *dca_pci_rc_from_dev(struct device *dev)
47{
48 struct pci_dev *pdev = to_pci_dev(dev);
49 struct pci_bus *bus = pdev->bus;
50
51 while (bus->parent)
52 bus = bus->parent;
53
54 return bus;
55}
56
57static struct dca_domain *dca_allocate_domain(struct pci_bus *rc)
58{
59 struct dca_domain *domain;
60
61 domain = kzalloc(sizeof(*domain), GFP_NOWAIT);
62 if (!domain)
63 return NULL;
64
65 INIT_LIST_HEAD(&domain->dca_providers);
66 domain->pci_rc = rc;
67
68 return domain;
69}
70
71static void dca_free_domain(struct dca_domain *domain)
72{
73 list_del(&domain->node);
74 kfree(domain);
75}
76
Sosnowski, Maciej4e8cec22010-09-16 06:02:26 +000077static int dca_provider_ioat_ver_3_0(struct device *dev)
78{
79 struct pci_dev *pdev = to_pci_dev(dev);
80
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)));
90}
91
92static void unregister_dca_providers(void)
93{
94 struct dca_provider *dca, *_dca;
95 struct list_head unregistered_providers;
96 struct dca_domain *domain;
97 unsigned long flags;
98
99 blocking_notifier_call_chain(&dca_provider_chain,
100 DCA_PROVIDER_REMOVE, NULL);
101
102 INIT_LIST_HEAD(&unregistered_providers);
103
104 spin_lock_irqsave(&dca_lock, flags);
105
106 if (list_empty(&dca_domains)) {
107 spin_unlock_irqrestore(&dca_lock, flags);
108 return;
109 }
110
111 /* at this point only one domain in the list is expected */
112 domain = list_first_entry(&dca_domains, struct dca_domain, node);
Sosnowski, Maciej4e8cec22010-09-16 06:02:26 +0000113
114 list_for_each_entry_safe(dca, _dca, &domain->dca_providers, node) {
115 list_del(&dca->node);
116 list_add(&dca->node, &unregistered_providers);
117 }
118
119 dca_free_domain(domain);
120
121 spin_unlock_irqrestore(&dca_lock, flags);
122
123 list_for_each_entry_safe(dca, _dca, &unregistered_providers, node) {
124 dca_sysfs_remove_provider(dca);
125 list_del(&dca->node);
126 }
127}
128
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200129static struct dca_domain *dca_find_domain(struct pci_bus *rc)
130{
131 struct dca_domain *domain;
132
133 list_for_each_entry(domain, &dca_domains, node)
134 if (domain->pci_rc == rc)
135 return domain;
136
137 return NULL;
138}
139
140static struct dca_domain *dca_get_domain(struct device *dev)
141{
142 struct pci_bus *rc;
143 struct dca_domain *domain;
144
145 rc = dca_pci_rc_from_dev(dev);
146 domain = dca_find_domain(rc);
147
148 if (!domain) {
Sosnowski, Maciej4e8cec22010-09-16 06:02:26 +0000149 if (dca_provider_ioat_ver_3_0(dev) && !list_empty(&dca_domains)) {
150 dca_providers_blocked = 1;
151 } else {
152 domain = dca_allocate_domain(rc);
153 if (domain)
154 list_add(&domain->node, &dca_domains);
155 }
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200156 }
157
158 return domain;
159}
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700160
161static struct dca_provider *dca_find_provider_by_dev(struct device *dev)
162{
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200163 struct dca_provider *dca;
164 struct pci_bus *rc;
165 struct dca_domain *domain;
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700166
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200167 if (dev) {
168 rc = dca_pci_rc_from_dev(dev);
169 domain = dca_find_domain(rc);
170 if (!domain)
171 return NULL;
172 } else {
173 if (!list_empty(&dca_domains))
174 domain = list_first_entry(&dca_domains,
175 struct dca_domain,
176 node);
177 else
178 return NULL;
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700179 }
180
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200181 list_for_each_entry(dca, &domain->dca_providers, node)
182 if ((!dev) || (dca->ops->dev_managed(dca, dev)))
183 return dca;
184
185 return NULL;
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700186}
Shannon Nelson75896702007-10-16 01:27:41 -0700187
188/**
189 * dca_add_requester - add a dca client to the list
190 * @dev - the device that wants dca service
191 */
192int dca_add_requester(struct device *dev)
193{
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700194 struct dca_provider *dca;
195 int err, slot = -ENODEV;
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800196 unsigned long flags;
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200197 struct pci_bus *pci_rc;
198 struct dca_domain *domain;
Shannon Nelson75896702007-10-16 01:27:41 -0700199
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700200 if (!dev)
201 return -EFAULT;
Shannon Nelson75896702007-10-16 01:27:41 -0700202
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800203 spin_lock_irqsave(&dca_lock, flags);
Shannon Nelson75896702007-10-16 01:27:41 -0700204
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700205 /* check if the requester has not been added already */
206 dca = dca_find_provider_by_dev(dev);
207 if (dca) {
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800208 spin_unlock_irqrestore(&dca_lock, flags);
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700209 return -EEXIST;
210 }
211
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200212 pci_rc = dca_pci_rc_from_dev(dev);
213 domain = dca_find_domain(pci_rc);
214 if (!domain) {
215 spin_unlock_irqrestore(&dca_lock, flags);
216 return -ENODEV;
217 }
218
219 list_for_each_entry(dca, &domain->dca_providers, node) {
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700220 slot = dca->ops->add_requester(dca, dev);
221 if (slot >= 0)
222 break;
223 }
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800224
225 spin_unlock_irqrestore(&dca_lock, flags);
226
227 if (slot < 0)
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700228 return slot;
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700229
230 err = dca_sysfs_add_req(dca, dev, slot);
Shannon Nelson75896702007-10-16 01:27:41 -0700231 if (err) {
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800232 spin_lock_irqsave(&dca_lock, flags);
233 if (dca == dca_find_provider_by_dev(dev))
234 dca->ops->remove_requester(dca, dev);
235 spin_unlock_irqrestore(&dca_lock, flags);
Shannon Nelson75896702007-10-16 01:27:41 -0700236 return err;
237 }
238
239 return 0;
240}
241EXPORT_SYMBOL_GPL(dca_add_requester);
242
243/**
244 * dca_remove_requester - remove a dca client from the list
245 * @dev - the device that wants dca service
246 */
247int dca_remove_requester(struct device *dev)
248{
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700249 struct dca_provider *dca;
Shannon Nelson75896702007-10-16 01:27:41 -0700250 int slot;
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800251 unsigned long flags;
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700252
253 if (!dev)
254 return -EFAULT;
Shannon Nelson75896702007-10-16 01:27:41 -0700255
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800256 spin_lock_irqsave(&dca_lock, flags);
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700257 dca = dca_find_provider_by_dev(dev);
258 if (!dca) {
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800259 spin_unlock_irqrestore(&dca_lock, flags);
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700260 return -ENODEV;
261 }
262 slot = dca->ops->remove_requester(dca, dev);
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800263 spin_unlock_irqrestore(&dca_lock, flags);
264
265 if (slot < 0)
Shannon Nelson75896702007-10-16 01:27:41 -0700266 return slot;
267
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700268 dca_sysfs_remove_req(dca, slot);
269
Shannon Nelson75896702007-10-16 01:27:41 -0700270 return 0;
271}
272EXPORT_SYMBOL_GPL(dca_remove_requester);
273
274/**
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700275 * dca_common_get_tag - return the dca tag (serves both new and old api)
276 * @dev - the device that wants dca service
277 * @cpu - the cpuid as returned by get_cpu()
278 */
279u8 dca_common_get_tag(struct device *dev, int cpu)
280{
281 struct dca_provider *dca;
282 u8 tag;
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800283 unsigned long flags;
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700284
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800285 spin_lock_irqsave(&dca_lock, flags);
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700286
287 dca = dca_find_provider_by_dev(dev);
288 if (!dca) {
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800289 spin_unlock_irqrestore(&dca_lock, flags);
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700290 return -ENODEV;
291 }
292 tag = dca->ops->get_tag(dca, dev, cpu);
293
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800294 spin_unlock_irqrestore(&dca_lock, flags);
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700295 return tag;
296}
297
298/**
299 * dca3_get_tag - return the dca tag to the requester device
300 * for the given cpu (new api)
301 * @dev - the device that wants dca service
302 * @cpu - the cpuid as returned by get_cpu()
303 */
304u8 dca3_get_tag(struct device *dev, int cpu)
305{
306 if (!dev)
307 return -EFAULT;
308
309 return dca_common_get_tag(dev, cpu);
310}
311EXPORT_SYMBOL_GPL(dca3_get_tag);
312
313/**
314 * dca_get_tag - return the dca tag for the given cpu (old api)
Shannon Nelson75896702007-10-16 01:27:41 -0700315 * @cpu - the cpuid as returned by get_cpu()
316 */
317u8 dca_get_tag(int cpu)
318{
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700319 struct device *dev = NULL;
320
321 return dca_common_get_tag(dev, cpu);
Shannon Nelson75896702007-10-16 01:27:41 -0700322}
323EXPORT_SYMBOL_GPL(dca_get_tag);
324
325/**
326 * alloc_dca_provider - get data struct for describing a dca provider
327 * @ops - pointer to struct of dca operation function pointers
328 * @priv_size - size of extra mem to be added for provider's needs
329 */
330struct dca_provider *alloc_dca_provider(struct dca_ops *ops, int priv_size)
331{
332 struct dca_provider *dca;
333 int alloc_size;
334
335 alloc_size = (sizeof(*dca) + priv_size);
336 dca = kzalloc(alloc_size, GFP_KERNEL);
337 if (!dca)
338 return NULL;
339 dca->ops = ops;
340
341 return dca;
342}
343EXPORT_SYMBOL_GPL(alloc_dca_provider);
344
345/**
346 * free_dca_provider - release the dca provider data struct
347 * @ops - pointer to struct of dca operation function pointers
348 * @priv_size - size of extra mem to be added for provider's needs
349 */
350void free_dca_provider(struct dca_provider *dca)
351{
352 kfree(dca);
353}
354EXPORT_SYMBOL_GPL(free_dca_provider);
355
Shannon Nelson75896702007-10-16 01:27:41 -0700356/**
357 * register_dca_provider - register a dca provider
358 * @dca - struct created by alloc_dca_provider()
359 * @dev - device providing dca services
360 */
361int register_dca_provider(struct dca_provider *dca, struct device *dev)
362{
363 int err;
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800364 unsigned long flags;
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200365 struct dca_domain *domain;
Shannon Nelson75896702007-10-16 01:27:41 -0700366
Sosnowski, Maciej4e8cec22010-09-16 06:02:26 +0000367 spin_lock_irqsave(&dca_lock, flags);
368 if (dca_providers_blocked) {
369 spin_unlock_irqrestore(&dca_lock, flags);
370 return -ENODEV;
371 }
372 spin_unlock_irqrestore(&dca_lock, flags);
373
Shannon Nelson75896702007-10-16 01:27:41 -0700374 err = dca_sysfs_add_provider(dca, dev);
375 if (err)
376 return err;
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800377
378 spin_lock_irqsave(&dca_lock, flags);
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200379 domain = dca_get_domain(dev);
380 if (!domain) {
Sosnowski, Maciej4e8cec22010-09-16 06:02:26 +0000381 if (dca_providers_blocked) {
382 spin_unlock_irqrestore(&dca_lock, flags);
383 dca_sysfs_remove_provider(dca);
384 unregister_dca_providers();
385 } else {
386 spin_unlock_irqrestore(&dca_lock, flags);
387 }
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200388 return -ENODEV;
389 }
390 list_add(&dca->node, &domain->dca_providers);
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800391 spin_unlock_irqrestore(&dca_lock, flags);
392
Shannon Nelson75896702007-10-16 01:27:41 -0700393 blocking_notifier_call_chain(&dca_provider_chain,
394 DCA_PROVIDER_ADD, NULL);
395 return 0;
396}
397EXPORT_SYMBOL_GPL(register_dca_provider);
398
399/**
400 * unregister_dca_provider - remove a dca provider
401 * @dca - struct created by alloc_dca_provider()
402 */
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200403void unregister_dca_provider(struct dca_provider *dca, struct device *dev)
Shannon Nelson75896702007-10-16 01:27:41 -0700404{
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800405 unsigned long flags;
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200406 struct pci_bus *pci_rc;
407 struct dca_domain *domain;
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800408
Shannon Nelson75896702007-10-16 01:27:41 -0700409 blocking_notifier_call_chain(&dca_provider_chain,
410 DCA_PROVIDER_REMOVE, NULL);
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800411
412 spin_lock_irqsave(&dca_lock, flags);
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200413
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700414 list_del(&dca->node);
Maciej Sosnowski1a5aeee2009-09-10 15:05:58 +0200415
416 pci_rc = dca_pci_rc_from_dev(dev);
417 domain = dca_find_domain(pci_rc);
418 if (list_empty(&domain->dca_providers))
419 dca_free_domain(domain);
420
Maciej Sosnowskieb4400e2009-02-02 23:26:57 -0800421 spin_unlock_irqrestore(&dca_lock, flags);
422
Shannon Nelson75896702007-10-16 01:27:41 -0700423 dca_sysfs_remove_provider(dca);
424}
425EXPORT_SYMBOL_GPL(unregister_dca_provider);
426
427/**
428 * dca_register_notify - register a client's notifier callback
429 */
430void dca_register_notify(struct notifier_block *nb)
431{
432 blocking_notifier_chain_register(&dca_provider_chain, nb);
433}
434EXPORT_SYMBOL_GPL(dca_register_notify);
435
436/**
437 * dca_unregister_notify - remove a client's notifier callback
438 */
439void dca_unregister_notify(struct notifier_block *nb)
440{
441 blocking_notifier_chain_unregister(&dca_provider_chain, nb);
442}
443EXPORT_SYMBOL_GPL(dca_unregister_notify);
444
445static int __init dca_init(void)
446{
Stephen Hemminger084dac52009-09-13 09:07:37 -0700447 pr_info("dca service started, version %s\n", DCA_VERSION);
Shannon Nelson75896702007-10-16 01:27:41 -0700448 return dca_sysfs_init();
449}
450
451static void __exit dca_exit(void)
452{
453 dca_sysfs_exit();
454}
455
Dan Williams652afc22009-01-06 11:38:22 -0700456arch_initcall(dca_init);
Shannon Nelson75896702007-10-16 01:27:41 -0700457module_exit(dca_exit);
458