Thomas Gleixner | e62d949 | 2019-05-20 19:07:58 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 2 | /* |
Maciej Sosnowski | 211a22c | 2009-02-26 11:05:43 +0100 | [diff] [blame] | 3 | * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved. |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * This driver supports an interface for DCA clients and providers to meet. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/notifier.h> |
| 12 | #include <linux/device.h> |
| 13 | #include <linux/dca.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Paul Gortmaker | d229807 | 2011-07-03 13:37:11 -0400 | [diff] [blame] | 15 | #include <linux/module.h> |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 16 | |
Maciej Sosnowski | 1a5aeee | 2009-09-10 15:05:58 +0200 | [diff] [blame] | 17 | #define DCA_VERSION "1.12.1" |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 18 | |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 19 | MODULE_VERSION(DCA_VERSION); |
| 20 | MODULE_LICENSE("GPL"); |
| 21 | MODULE_AUTHOR("Intel Corporation"); |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 22 | |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 23 | static DEFINE_RAW_SPINLOCK(dca_lock); |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 24 | |
Maciej Sosnowski | 1a5aeee | 2009-09-10 15:05:58 +0200 | [diff] [blame] | 25 | static LIST_HEAD(dca_domains); |
| 26 | |
Sosnowski, Maciej | 4e8cec2 | 2010-09-16 06:02:26 +0000 | [diff] [blame] | 27 | static BLOCKING_NOTIFIER_HEAD(dca_provider_chain); |
| 28 | |
| 29 | static int dca_providers_blocked; |
| 30 | |
Maciej Sosnowski | 1a5aeee | 2009-09-10 15:05:58 +0200 | [diff] [blame] | 31 | static struct pci_bus *dca_pci_rc_from_dev(struct device *dev) |
| 32 | { |
| 33 | struct pci_dev *pdev = to_pci_dev(dev); |
| 34 | struct pci_bus *bus = pdev->bus; |
| 35 | |
| 36 | while (bus->parent) |
| 37 | bus = bus->parent; |
| 38 | |
| 39 | return bus; |
| 40 | } |
| 41 | |
| 42 | static struct dca_domain *dca_allocate_domain(struct pci_bus *rc) |
| 43 | { |
| 44 | struct dca_domain *domain; |
| 45 | |
| 46 | domain = kzalloc(sizeof(*domain), GFP_NOWAIT); |
| 47 | if (!domain) |
| 48 | return NULL; |
| 49 | |
| 50 | INIT_LIST_HEAD(&domain->dca_providers); |
| 51 | domain->pci_rc = rc; |
| 52 | |
| 53 | return domain; |
| 54 | } |
| 55 | |
| 56 | static void dca_free_domain(struct dca_domain *domain) |
| 57 | { |
| 58 | list_del(&domain->node); |
| 59 | kfree(domain); |
| 60 | } |
| 61 | |
Sosnowski, Maciej | 4e8cec2 | 2010-09-16 06:02:26 +0000 | [diff] [blame] | 62 | static int dca_provider_ioat_ver_3_0(struct device *dev) |
| 63 | { |
| 64 | struct pci_dev *pdev = to_pci_dev(dev); |
| 65 | |
| 66 | return ((pdev->vendor == PCI_VENDOR_ID_INTEL) && |
| 67 | ((pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG0) || |
| 68 | (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG1) || |
| 69 | (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG2) || |
| 70 | (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG3) || |
| 71 | (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG4) || |
| 72 | (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG5) || |
| 73 | (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG6) || |
| 74 | (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG7))); |
| 75 | } |
| 76 | |
| 77 | static void unregister_dca_providers(void) |
| 78 | { |
| 79 | struct dca_provider *dca, *_dca; |
| 80 | struct list_head unregistered_providers; |
| 81 | struct dca_domain *domain; |
| 82 | unsigned long flags; |
| 83 | |
| 84 | blocking_notifier_call_chain(&dca_provider_chain, |
| 85 | DCA_PROVIDER_REMOVE, NULL); |
| 86 | |
| 87 | INIT_LIST_HEAD(&unregistered_providers); |
| 88 | |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 89 | raw_spin_lock_irqsave(&dca_lock, flags); |
Sosnowski, Maciej | 4e8cec2 | 2010-09-16 06:02:26 +0000 | [diff] [blame] | 90 | |
| 91 | if (list_empty(&dca_domains)) { |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 92 | raw_spin_unlock_irqrestore(&dca_lock, flags); |
Sosnowski, Maciej | 4e8cec2 | 2010-09-16 06:02:26 +0000 | [diff] [blame] | 93 | return; |
| 94 | } |
| 95 | |
| 96 | /* at this point only one domain in the list is expected */ |
| 97 | domain = list_first_entry(&dca_domains, struct dca_domain, node); |
Sosnowski, Maciej | 4e8cec2 | 2010-09-16 06:02:26 +0000 | [diff] [blame] | 98 | |
Kirill A. Shutemov | 3bb598f | 2011-03-22 16:34:18 -0700 | [diff] [blame] | 99 | list_for_each_entry_safe(dca, _dca, &domain->dca_providers, node) |
| 100 | list_move(&dca->node, &unregistered_providers); |
Sosnowski, Maciej | 4e8cec2 | 2010-09-16 06:02:26 +0000 | [diff] [blame] | 101 | |
| 102 | dca_free_domain(domain); |
| 103 | |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 104 | raw_spin_unlock_irqrestore(&dca_lock, flags); |
Sosnowski, Maciej | 4e8cec2 | 2010-09-16 06:02:26 +0000 | [diff] [blame] | 105 | |
| 106 | list_for_each_entry_safe(dca, _dca, &unregistered_providers, node) { |
| 107 | dca_sysfs_remove_provider(dca); |
| 108 | list_del(&dca->node); |
| 109 | } |
| 110 | } |
| 111 | |
Maciej Sosnowski | 1a5aeee | 2009-09-10 15:05:58 +0200 | [diff] [blame] | 112 | static struct dca_domain *dca_find_domain(struct pci_bus *rc) |
| 113 | { |
| 114 | struct dca_domain *domain; |
| 115 | |
| 116 | list_for_each_entry(domain, &dca_domains, node) |
| 117 | if (domain->pci_rc == rc) |
| 118 | return domain; |
| 119 | |
| 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | static struct dca_domain *dca_get_domain(struct device *dev) |
| 124 | { |
| 125 | struct pci_bus *rc; |
| 126 | struct dca_domain *domain; |
| 127 | |
| 128 | rc = dca_pci_rc_from_dev(dev); |
| 129 | domain = dca_find_domain(rc); |
| 130 | |
| 131 | if (!domain) { |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 132 | if (dca_provider_ioat_ver_3_0(dev) && !list_empty(&dca_domains)) |
Sosnowski, Maciej | 4e8cec2 | 2010-09-16 06:02:26 +0000 | [diff] [blame] | 133 | dca_providers_blocked = 1; |
Maciej Sosnowski | 1a5aeee | 2009-09-10 15:05:58 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | return domain; |
| 137 | } |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 138 | |
| 139 | static struct dca_provider *dca_find_provider_by_dev(struct device *dev) |
| 140 | { |
Maciej Sosnowski | 1a5aeee | 2009-09-10 15:05:58 +0200 | [diff] [blame] | 141 | struct dca_provider *dca; |
| 142 | struct pci_bus *rc; |
| 143 | struct dca_domain *domain; |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 144 | |
Maciej Sosnowski | 1a5aeee | 2009-09-10 15:05:58 +0200 | [diff] [blame] | 145 | if (dev) { |
| 146 | rc = dca_pci_rc_from_dev(dev); |
| 147 | domain = dca_find_domain(rc); |
| 148 | if (!domain) |
| 149 | return NULL; |
| 150 | } else { |
| 151 | if (!list_empty(&dca_domains)) |
| 152 | domain = list_first_entry(&dca_domains, |
| 153 | struct dca_domain, |
| 154 | node); |
| 155 | else |
| 156 | return NULL; |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Maciej Sosnowski | 1a5aeee | 2009-09-10 15:05:58 +0200 | [diff] [blame] | 159 | list_for_each_entry(dca, &domain->dca_providers, node) |
| 160 | if ((!dev) || (dca->ops->dev_managed(dca, dev))) |
| 161 | return dca; |
| 162 | |
| 163 | return NULL; |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 164 | } |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 165 | |
| 166 | /** |
| 167 | * dca_add_requester - add a dca client to the list |
| 168 | * @dev - the device that wants dca service |
| 169 | */ |
| 170 | int dca_add_requester(struct device *dev) |
| 171 | { |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 172 | struct dca_provider *dca; |
| 173 | int err, slot = -ENODEV; |
Maciej Sosnowski | eb4400e | 2009-02-02 23:26:57 -0800 | [diff] [blame] | 174 | unsigned long flags; |
Maciej Sosnowski | 1a5aeee | 2009-09-10 15:05:58 +0200 | [diff] [blame] | 175 | struct pci_bus *pci_rc; |
| 176 | struct dca_domain *domain; |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 177 | |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 178 | if (!dev) |
| 179 | return -EFAULT; |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 180 | |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 181 | raw_spin_lock_irqsave(&dca_lock, flags); |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 182 | |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 183 | /* check if the requester has not been added already */ |
| 184 | dca = dca_find_provider_by_dev(dev); |
| 185 | if (dca) { |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 186 | raw_spin_unlock_irqrestore(&dca_lock, flags); |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 187 | return -EEXIST; |
| 188 | } |
| 189 | |
Maciej Sosnowski | 1a5aeee | 2009-09-10 15:05:58 +0200 | [diff] [blame] | 190 | pci_rc = dca_pci_rc_from_dev(dev); |
| 191 | domain = dca_find_domain(pci_rc); |
| 192 | if (!domain) { |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 193 | raw_spin_unlock_irqrestore(&dca_lock, flags); |
Maciej Sosnowski | 1a5aeee | 2009-09-10 15:05:58 +0200 | [diff] [blame] | 194 | return -ENODEV; |
| 195 | } |
| 196 | |
| 197 | list_for_each_entry(dca, &domain->dca_providers, node) { |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 198 | slot = dca->ops->add_requester(dca, dev); |
| 199 | if (slot >= 0) |
| 200 | break; |
| 201 | } |
Maciej Sosnowski | eb4400e | 2009-02-02 23:26:57 -0800 | [diff] [blame] | 202 | |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 203 | raw_spin_unlock_irqrestore(&dca_lock, flags); |
Maciej Sosnowski | eb4400e | 2009-02-02 23:26:57 -0800 | [diff] [blame] | 204 | |
| 205 | if (slot < 0) |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 206 | return slot; |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 207 | |
| 208 | err = dca_sysfs_add_req(dca, dev, slot); |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 209 | if (err) { |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 210 | raw_spin_lock_irqsave(&dca_lock, flags); |
Maciej Sosnowski | eb4400e | 2009-02-02 23:26:57 -0800 | [diff] [blame] | 211 | if (dca == dca_find_provider_by_dev(dev)) |
| 212 | dca->ops->remove_requester(dca, dev); |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 213 | raw_spin_unlock_irqrestore(&dca_lock, flags); |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 214 | return err; |
| 215 | } |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | EXPORT_SYMBOL_GPL(dca_add_requester); |
| 220 | |
| 221 | /** |
| 222 | * dca_remove_requester - remove a dca client from the list |
| 223 | * @dev - the device that wants dca service |
| 224 | */ |
| 225 | int dca_remove_requester(struct device *dev) |
| 226 | { |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 227 | struct dca_provider *dca; |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 228 | int slot; |
Maciej Sosnowski | eb4400e | 2009-02-02 23:26:57 -0800 | [diff] [blame] | 229 | unsigned long flags; |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 230 | |
| 231 | if (!dev) |
| 232 | return -EFAULT; |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 233 | |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 234 | raw_spin_lock_irqsave(&dca_lock, flags); |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 235 | dca = dca_find_provider_by_dev(dev); |
| 236 | if (!dca) { |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 237 | raw_spin_unlock_irqrestore(&dca_lock, flags); |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 238 | return -ENODEV; |
| 239 | } |
| 240 | slot = dca->ops->remove_requester(dca, dev); |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 241 | raw_spin_unlock_irqrestore(&dca_lock, flags); |
Maciej Sosnowski | eb4400e | 2009-02-02 23:26:57 -0800 | [diff] [blame] | 242 | |
| 243 | if (slot < 0) |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 244 | return slot; |
| 245 | |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 246 | dca_sysfs_remove_req(dca, slot); |
| 247 | |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 248 | return 0; |
| 249 | } |
| 250 | EXPORT_SYMBOL_GPL(dca_remove_requester); |
| 251 | |
| 252 | /** |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 253 | * dca_common_get_tag - return the dca tag (serves both new and old api) |
| 254 | * @dev - the device that wants dca service |
| 255 | * @cpu - the cpuid as returned by get_cpu() |
| 256 | */ |
Colin Ian King | 064223c | 2018-04-23 13:49:38 +0100 | [diff] [blame] | 257 | static u8 dca_common_get_tag(struct device *dev, int cpu) |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 258 | { |
| 259 | struct dca_provider *dca; |
| 260 | u8 tag; |
Maciej Sosnowski | eb4400e | 2009-02-02 23:26:57 -0800 | [diff] [blame] | 261 | unsigned long flags; |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 262 | |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 263 | raw_spin_lock_irqsave(&dca_lock, flags); |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 264 | |
| 265 | dca = dca_find_provider_by_dev(dev); |
| 266 | if (!dca) { |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 267 | raw_spin_unlock_irqrestore(&dca_lock, flags); |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 268 | return -ENODEV; |
| 269 | } |
| 270 | tag = dca->ops->get_tag(dca, dev, cpu); |
| 271 | |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 272 | raw_spin_unlock_irqrestore(&dca_lock, flags); |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 273 | return tag; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * dca3_get_tag - return the dca tag to the requester device |
| 278 | * for the given cpu (new api) |
| 279 | * @dev - the device that wants dca service |
| 280 | * @cpu - the cpuid as returned by get_cpu() |
| 281 | */ |
| 282 | u8 dca3_get_tag(struct device *dev, int cpu) |
| 283 | { |
| 284 | if (!dev) |
| 285 | return -EFAULT; |
| 286 | |
| 287 | return dca_common_get_tag(dev, cpu); |
| 288 | } |
| 289 | EXPORT_SYMBOL_GPL(dca3_get_tag); |
| 290 | |
| 291 | /** |
| 292 | * dca_get_tag - return the dca tag for the given cpu (old api) |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 293 | * @cpu - the cpuid as returned by get_cpu() |
| 294 | */ |
| 295 | u8 dca_get_tag(int cpu) |
| 296 | { |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 297 | struct device *dev = NULL; |
| 298 | |
| 299 | return dca_common_get_tag(dev, cpu); |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 300 | } |
| 301 | EXPORT_SYMBOL_GPL(dca_get_tag); |
| 302 | |
| 303 | /** |
| 304 | * alloc_dca_provider - get data struct for describing a dca provider |
| 305 | * @ops - pointer to struct of dca operation function pointers |
| 306 | * @priv_size - size of extra mem to be added for provider's needs |
| 307 | */ |
Julia Lawall | 2bb129e | 2015-11-13 12:46:00 +0100 | [diff] [blame] | 308 | struct dca_provider *alloc_dca_provider(const struct dca_ops *ops, |
| 309 | int priv_size) |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 310 | { |
| 311 | struct dca_provider *dca; |
| 312 | int alloc_size; |
| 313 | |
| 314 | alloc_size = (sizeof(*dca) + priv_size); |
| 315 | dca = kzalloc(alloc_size, GFP_KERNEL); |
| 316 | if (!dca) |
| 317 | return NULL; |
| 318 | dca->ops = ops; |
| 319 | |
| 320 | return dca; |
| 321 | } |
| 322 | EXPORT_SYMBOL_GPL(alloc_dca_provider); |
| 323 | |
| 324 | /** |
| 325 | * free_dca_provider - release the dca provider data struct |
| 326 | * @ops - pointer to struct of dca operation function pointers |
| 327 | * @priv_size - size of extra mem to be added for provider's needs |
| 328 | */ |
| 329 | void free_dca_provider(struct dca_provider *dca) |
| 330 | { |
| 331 | kfree(dca); |
| 332 | } |
| 333 | EXPORT_SYMBOL_GPL(free_dca_provider); |
| 334 | |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 335 | /** |
| 336 | * register_dca_provider - register a dca provider |
| 337 | * @dca - struct created by alloc_dca_provider() |
| 338 | * @dev - device providing dca services |
| 339 | */ |
| 340 | int register_dca_provider(struct dca_provider *dca, struct device *dev) |
| 341 | { |
| 342 | int err; |
Maciej Sosnowski | eb4400e | 2009-02-02 23:26:57 -0800 | [diff] [blame] | 343 | unsigned long flags; |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 344 | struct dca_domain *domain, *newdomain = NULL; |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 345 | |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 346 | raw_spin_lock_irqsave(&dca_lock, flags); |
Sosnowski, Maciej | 4e8cec2 | 2010-09-16 06:02:26 +0000 | [diff] [blame] | 347 | if (dca_providers_blocked) { |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 348 | raw_spin_unlock_irqrestore(&dca_lock, flags); |
Sosnowski, Maciej | 4e8cec2 | 2010-09-16 06:02:26 +0000 | [diff] [blame] | 349 | return -ENODEV; |
| 350 | } |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 351 | raw_spin_unlock_irqrestore(&dca_lock, flags); |
Sosnowski, Maciej | 4e8cec2 | 2010-09-16 06:02:26 +0000 | [diff] [blame] | 352 | |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 353 | err = dca_sysfs_add_provider(dca, dev); |
| 354 | if (err) |
| 355 | return err; |
Maciej Sosnowski | eb4400e | 2009-02-02 23:26:57 -0800 | [diff] [blame] | 356 | |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 357 | raw_spin_lock_irqsave(&dca_lock, flags); |
Maciej Sosnowski | 1a5aeee | 2009-09-10 15:05:58 +0200 | [diff] [blame] | 358 | domain = dca_get_domain(dev); |
| 359 | if (!domain) { |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 360 | struct pci_bus *rc; |
| 361 | |
Sosnowski, Maciej | 4e8cec2 | 2010-09-16 06:02:26 +0000 | [diff] [blame] | 362 | if (dca_providers_blocked) { |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 363 | raw_spin_unlock_irqrestore(&dca_lock, flags); |
Sosnowski, Maciej | 4e8cec2 | 2010-09-16 06:02:26 +0000 | [diff] [blame] | 364 | dca_sysfs_remove_provider(dca); |
| 365 | unregister_dca_providers(); |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 366 | return -ENODEV; |
Sosnowski, Maciej | 4e8cec2 | 2010-09-16 06:02:26 +0000 | [diff] [blame] | 367 | } |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 368 | |
| 369 | raw_spin_unlock_irqrestore(&dca_lock, flags); |
| 370 | rc = dca_pci_rc_from_dev(dev); |
| 371 | newdomain = dca_allocate_domain(rc); |
| 372 | if (!newdomain) |
| 373 | return -ENODEV; |
| 374 | raw_spin_lock_irqsave(&dca_lock, flags); |
| 375 | /* Recheck, we might have raced after dropping the lock */ |
| 376 | domain = dca_get_domain(dev); |
| 377 | if (!domain) { |
| 378 | domain = newdomain; |
| 379 | newdomain = NULL; |
| 380 | list_add(&domain->node, &dca_domains); |
| 381 | } |
Maciej Sosnowski | 1a5aeee | 2009-09-10 15:05:58 +0200 | [diff] [blame] | 382 | } |
| 383 | list_add(&dca->node, &domain->dca_providers); |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 384 | raw_spin_unlock_irqrestore(&dca_lock, flags); |
Maciej Sosnowski | eb4400e | 2009-02-02 23:26:57 -0800 | [diff] [blame] | 385 | |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 386 | blocking_notifier_call_chain(&dca_provider_chain, |
| 387 | DCA_PROVIDER_ADD, NULL); |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 388 | kfree(newdomain); |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 389 | return 0; |
| 390 | } |
| 391 | EXPORT_SYMBOL_GPL(register_dca_provider); |
| 392 | |
| 393 | /** |
| 394 | * unregister_dca_provider - remove a dca provider |
| 395 | * @dca - struct created by alloc_dca_provider() |
| 396 | */ |
Maciej Sosnowski | 1a5aeee | 2009-09-10 15:05:58 +0200 | [diff] [blame] | 397 | void unregister_dca_provider(struct dca_provider *dca, struct device *dev) |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 398 | { |
Maciej Sosnowski | eb4400e | 2009-02-02 23:26:57 -0800 | [diff] [blame] | 399 | unsigned long flags; |
Maciej Sosnowski | 1a5aeee | 2009-09-10 15:05:58 +0200 | [diff] [blame] | 400 | struct pci_bus *pci_rc; |
| 401 | struct dca_domain *domain; |
Maciej Sosnowski | eb4400e | 2009-02-02 23:26:57 -0800 | [diff] [blame] | 402 | |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 403 | blocking_notifier_call_chain(&dca_provider_chain, |
| 404 | DCA_PROVIDER_REMOVE, NULL); |
Maciej Sosnowski | eb4400e | 2009-02-02 23:26:57 -0800 | [diff] [blame] | 405 | |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 406 | raw_spin_lock_irqsave(&dca_lock, flags); |
Maciej Sosnowski | 1a5aeee | 2009-09-10 15:05:58 +0200 | [diff] [blame] | 407 | |
Maciej Sosnowski | c419fcf | 2012-05-23 17:27:07 +0200 | [diff] [blame] | 408 | if (list_empty(&dca_domains)) { |
| 409 | raw_spin_unlock_irqrestore(&dca_lock, flags); |
| 410 | return; |
| 411 | } |
| 412 | |
Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 413 | list_del(&dca->node); |
Maciej Sosnowski | 1a5aeee | 2009-09-10 15:05:58 +0200 | [diff] [blame] | 414 | |
| 415 | pci_rc = dca_pci_rc_from_dev(dev); |
| 416 | domain = dca_find_domain(pci_rc); |
| 417 | if (list_empty(&domain->dca_providers)) |
| 418 | dca_free_domain(domain); |
| 419 | |
Mike Galbraith | a1741e7 | 2010-07-07 10:29:01 +0200 | [diff] [blame] | 420 | raw_spin_unlock_irqrestore(&dca_lock, flags); |
Maciej Sosnowski | eb4400e | 2009-02-02 23:26:57 -0800 | [diff] [blame] | 421 | |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 422 | dca_sysfs_remove_provider(dca); |
| 423 | } |
| 424 | EXPORT_SYMBOL_GPL(unregister_dca_provider); |
| 425 | |
| 426 | /** |
| 427 | * dca_register_notify - register a client's notifier callback |
| 428 | */ |
| 429 | void dca_register_notify(struct notifier_block *nb) |
| 430 | { |
| 431 | blocking_notifier_chain_register(&dca_provider_chain, nb); |
| 432 | } |
| 433 | EXPORT_SYMBOL_GPL(dca_register_notify); |
| 434 | |
| 435 | /** |
| 436 | * dca_unregister_notify - remove a client's notifier callback |
| 437 | */ |
| 438 | void dca_unregister_notify(struct notifier_block *nb) |
| 439 | { |
| 440 | blocking_notifier_chain_unregister(&dca_provider_chain, nb); |
| 441 | } |
| 442 | EXPORT_SYMBOL_GPL(dca_unregister_notify); |
| 443 | |
| 444 | static int __init dca_init(void) |
| 445 | { |
Stephen Hemminger | 084dac5 | 2009-09-13 09:07:37 -0700 | [diff] [blame] | 446 | pr_info("dca service started, version %s\n", DCA_VERSION); |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 447 | return dca_sysfs_init(); |
| 448 | } |
| 449 | |
| 450 | static void __exit dca_exit(void) |
| 451 | { |
| 452 | dca_sysfs_exit(); |
| 453 | } |
| 454 | |
Dan Williams | 652afc2 | 2009-01-06 11:38:22 -0700 | [diff] [blame] | 455 | arch_initcall(dca_init); |
Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 456 | module_exit(dca_exit); |
| 457 | |