Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Volume Management Device driver |
| 3 | * Copyright (c) 2015, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope 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 | |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/irq.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/msi.h> |
| 21 | #include <linux/pci.h> |
| 22 | #include <linux/rculist.h> |
| 23 | #include <linux/rcupdate.h> |
| 24 | |
| 25 | #include <asm/irqdomain.h> |
| 26 | #include <asm/device.h> |
| 27 | #include <asm/msi.h> |
| 28 | #include <asm/msidef.h> |
| 29 | |
| 30 | #define VMD_CFGBAR 0 |
| 31 | #define VMD_MEMBAR1 2 |
| 32 | #define VMD_MEMBAR2 4 |
| 33 | |
| 34 | /* |
| 35 | * Lock for manipulating VMD IRQ lists. |
| 36 | */ |
| 37 | static DEFINE_RAW_SPINLOCK(list_lock); |
| 38 | |
| 39 | /** |
| 40 | * struct vmd_irq - private data to map driver IRQ to the VMD shared vector |
| 41 | * @node: list item for parent traversal. |
| 42 | * @rcu: RCU callback item for freeing. |
| 43 | * @irq: back pointer to parent. |
| 44 | * @virq: the virtual IRQ value provided to the requesting driver. |
| 45 | * |
| 46 | * Every MSI/MSI-X IRQ requested for a device in a VMD domain will be mapped to |
| 47 | * a VMD IRQ using this structure. |
| 48 | */ |
| 49 | struct vmd_irq { |
| 50 | struct list_head node; |
| 51 | struct rcu_head rcu; |
| 52 | struct vmd_irq_list *irq; |
| 53 | unsigned int virq; |
| 54 | }; |
| 55 | |
| 56 | /** |
| 57 | * struct vmd_irq_list - list of driver requested IRQs mapping to a VMD vector |
| 58 | * @irq_list: the list of irq's the VMD one demuxes to. |
| 59 | * @vmd_vector: the h/w IRQ assigned to the VMD. |
| 60 | * @index: index into the VMD MSI-X table; used for message routing. |
| 61 | * @count: number of child IRQs assigned to this vector; used to track |
| 62 | * sharing. |
| 63 | */ |
| 64 | struct vmd_irq_list { |
| 65 | struct list_head irq_list; |
| 66 | struct vmd_dev *vmd; |
| 67 | unsigned int vmd_vector; |
| 68 | unsigned int index; |
| 69 | unsigned int count; |
| 70 | }; |
| 71 | |
| 72 | struct vmd_dev { |
| 73 | struct pci_dev *dev; |
| 74 | |
| 75 | spinlock_t cfg_lock; |
| 76 | char __iomem *cfgbar; |
| 77 | |
| 78 | int msix_count; |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 79 | struct vmd_irq_list *irqs; |
| 80 | |
| 81 | struct pci_sysdata sysdata; |
| 82 | struct resource resources[3]; |
| 83 | struct irq_domain *irq_domain; |
| 84 | struct pci_bus *bus; |
| 85 | |
| 86 | #ifdef CONFIG_X86_DEV_DMA_OPS |
| 87 | struct dma_map_ops dma_ops; |
| 88 | struct dma_domain dma_domain; |
| 89 | #endif |
| 90 | }; |
| 91 | |
| 92 | static inline struct vmd_dev *vmd_from_bus(struct pci_bus *bus) |
| 93 | { |
| 94 | return container_of(bus->sysdata, struct vmd_dev, sysdata); |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Drivers managing a device in a VMD domain allocate their own IRQs as before, |
| 99 | * but the MSI entry for the hardware it's driving will be programmed with a |
| 100 | * destination ID for the VMD MSI-X table. The VMD muxes interrupts in its |
| 101 | * domain into one of its own, and the VMD driver de-muxes these for the |
| 102 | * handlers sharing that VMD IRQ. The vmd irq_domain provides the operations |
| 103 | * and irq_chip to set this up. |
| 104 | */ |
| 105 | static void vmd_compose_msi_msg(struct irq_data *data, struct msi_msg *msg) |
| 106 | { |
| 107 | struct vmd_irq *vmdirq = data->chip_data; |
| 108 | struct vmd_irq_list *irq = vmdirq->irq; |
| 109 | |
| 110 | msg->address_hi = MSI_ADDR_BASE_HI; |
| 111 | msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_DEST_ID(irq->index); |
| 112 | msg->data = 0; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * We rely on MSI_FLAG_USE_DEF_CHIP_OPS to set the IRQ mask/unmask ops. |
| 117 | */ |
| 118 | static void vmd_irq_enable(struct irq_data *data) |
| 119 | { |
| 120 | struct vmd_irq *vmdirq = data->chip_data; |
Jon Derrick | 3f57ff4 | 2016-06-20 09:39:51 -0600 | [diff] [blame] | 121 | unsigned long flags; |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 122 | |
Jon Derrick | 3f57ff4 | 2016-06-20 09:39:51 -0600 | [diff] [blame] | 123 | raw_spin_lock_irqsave(&list_lock, flags); |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 124 | list_add_tail_rcu(&vmdirq->node, &vmdirq->irq->irq_list); |
Jon Derrick | 3f57ff4 | 2016-06-20 09:39:51 -0600 | [diff] [blame] | 125 | raw_spin_unlock_irqrestore(&list_lock, flags); |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 126 | |
| 127 | data->chip->irq_unmask(data); |
| 128 | } |
| 129 | |
| 130 | static void vmd_irq_disable(struct irq_data *data) |
| 131 | { |
| 132 | struct vmd_irq *vmdirq = data->chip_data; |
Jon Derrick | 3f57ff4 | 2016-06-20 09:39:51 -0600 | [diff] [blame] | 133 | unsigned long flags; |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 134 | |
| 135 | data->chip->irq_mask(data); |
| 136 | |
Jon Derrick | 3f57ff4 | 2016-06-20 09:39:51 -0600 | [diff] [blame] | 137 | raw_spin_lock_irqsave(&list_lock, flags); |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 138 | list_del_rcu(&vmdirq->node); |
Keith Busch | 97e9230 | 2016-05-17 11:22:18 -0600 | [diff] [blame] | 139 | INIT_LIST_HEAD_RCU(&vmdirq->node); |
Jon Derrick | 3f57ff4 | 2016-06-20 09:39:51 -0600 | [diff] [blame] | 140 | raw_spin_unlock_irqrestore(&list_lock, flags); |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /* |
| 144 | * XXX: Stubbed until we develop acceptable way to not create conflicts with |
| 145 | * other devices sharing the same vector. |
| 146 | */ |
| 147 | static int vmd_irq_set_affinity(struct irq_data *data, |
| 148 | const struct cpumask *dest, bool force) |
| 149 | { |
| 150 | return -EINVAL; |
| 151 | } |
| 152 | |
| 153 | static struct irq_chip vmd_msi_controller = { |
| 154 | .name = "VMD-MSI", |
| 155 | .irq_enable = vmd_irq_enable, |
| 156 | .irq_disable = vmd_irq_disable, |
| 157 | .irq_compose_msi_msg = vmd_compose_msi_msg, |
| 158 | .irq_set_affinity = vmd_irq_set_affinity, |
| 159 | }; |
| 160 | |
| 161 | static irq_hw_number_t vmd_get_hwirq(struct msi_domain_info *info, |
| 162 | msi_alloc_info_t *arg) |
| 163 | { |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * XXX: We can be even smarter selecting the best IRQ once we solve the |
| 169 | * affinity problem. |
| 170 | */ |
Keith Busch | 9c20530 | 2016-06-20 09:39:53 -0600 | [diff] [blame] | 171 | static struct vmd_irq_list *vmd_next_irq(struct vmd_dev *vmd, struct msi_desc *desc) |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 172 | { |
Keith Busch | 9c20530 | 2016-06-20 09:39:53 -0600 | [diff] [blame] | 173 | int i, best = 1; |
Jon Derrick | 3f57ff4 | 2016-06-20 09:39:51 -0600 | [diff] [blame] | 174 | unsigned long flags; |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 175 | |
Keith Busch | 9c20530 | 2016-06-20 09:39:53 -0600 | [diff] [blame] | 176 | if (!desc->msi_attrib.is_msix || vmd->msix_count == 1) |
| 177 | return &vmd->irqs[0]; |
| 178 | |
Jon Derrick | 3f57ff4 | 2016-06-20 09:39:51 -0600 | [diff] [blame] | 179 | raw_spin_lock_irqsave(&list_lock, flags); |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 180 | for (i = 1; i < vmd->msix_count; i++) |
| 181 | if (vmd->irqs[i].count < vmd->irqs[best].count) |
| 182 | best = i; |
| 183 | vmd->irqs[best].count++; |
Jon Derrick | 3f57ff4 | 2016-06-20 09:39:51 -0600 | [diff] [blame] | 184 | raw_spin_unlock_irqrestore(&list_lock, flags); |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 185 | |
| 186 | return &vmd->irqs[best]; |
| 187 | } |
| 188 | |
| 189 | static int vmd_msi_init(struct irq_domain *domain, struct msi_domain_info *info, |
| 190 | unsigned int virq, irq_hw_number_t hwirq, |
| 191 | msi_alloc_info_t *arg) |
| 192 | { |
Keith Busch | 9c20530 | 2016-06-20 09:39:53 -0600 | [diff] [blame] | 193 | struct msi_desc *desc = arg->desc; |
| 194 | struct vmd_dev *vmd = vmd_from_bus(msi_desc_to_pci_dev(desc)->bus); |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 195 | struct vmd_irq *vmdirq = kzalloc(sizeof(*vmdirq), GFP_KERNEL); |
| 196 | |
| 197 | if (!vmdirq) |
| 198 | return -ENOMEM; |
| 199 | |
| 200 | INIT_LIST_HEAD(&vmdirq->node); |
Keith Busch | 9c20530 | 2016-06-20 09:39:53 -0600 | [diff] [blame] | 201 | vmdirq->irq = vmd_next_irq(vmd, desc); |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 202 | vmdirq->virq = virq; |
| 203 | |
| 204 | irq_domain_set_info(domain, virq, vmdirq->irq->vmd_vector, info->chip, |
Keith Busch | 30ce035 | 2016-06-17 16:00:21 -0600 | [diff] [blame] | 205 | vmdirq, handle_untracked_irq, vmd, NULL); |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static void vmd_msi_free(struct irq_domain *domain, |
| 210 | struct msi_domain_info *info, unsigned int virq) |
| 211 | { |
| 212 | struct vmd_irq *vmdirq = irq_get_chip_data(virq); |
Jon Derrick | 3f57ff4 | 2016-06-20 09:39:51 -0600 | [diff] [blame] | 213 | unsigned long flags; |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 214 | |
| 215 | /* XXX: Potential optimization to rebalance */ |
Jon Derrick | 3f57ff4 | 2016-06-20 09:39:51 -0600 | [diff] [blame] | 216 | raw_spin_lock_irqsave(&list_lock, flags); |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 217 | vmdirq->irq->count--; |
Jon Derrick | 3f57ff4 | 2016-06-20 09:39:51 -0600 | [diff] [blame] | 218 | raw_spin_unlock_irqrestore(&list_lock, flags); |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 219 | |
| 220 | kfree_rcu(vmdirq, rcu); |
| 221 | } |
| 222 | |
| 223 | static int vmd_msi_prepare(struct irq_domain *domain, struct device *dev, |
| 224 | int nvec, msi_alloc_info_t *arg) |
| 225 | { |
| 226 | struct pci_dev *pdev = to_pci_dev(dev); |
| 227 | struct vmd_dev *vmd = vmd_from_bus(pdev->bus); |
| 228 | |
| 229 | if (nvec > vmd->msix_count) |
| 230 | return vmd->msix_count; |
| 231 | |
| 232 | memset(arg, 0, sizeof(*arg)); |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | static void vmd_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc) |
| 237 | { |
| 238 | arg->desc = desc; |
| 239 | } |
| 240 | |
| 241 | static struct msi_domain_ops vmd_msi_domain_ops = { |
| 242 | .get_hwirq = vmd_get_hwirq, |
| 243 | .msi_init = vmd_msi_init, |
| 244 | .msi_free = vmd_msi_free, |
| 245 | .msi_prepare = vmd_msi_prepare, |
| 246 | .set_desc = vmd_set_desc, |
| 247 | }; |
| 248 | |
| 249 | static struct msi_domain_info vmd_msi_domain_info = { |
| 250 | .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS | |
| 251 | MSI_FLAG_PCI_MSIX, |
| 252 | .ops = &vmd_msi_domain_ops, |
| 253 | .chip = &vmd_msi_controller, |
| 254 | }; |
| 255 | |
| 256 | #ifdef CONFIG_X86_DEV_DMA_OPS |
| 257 | /* |
| 258 | * VMD replaces the requester ID with its own. DMA mappings for devices in a |
| 259 | * VMD domain need to be mapped for the VMD, not the device requiring |
| 260 | * the mapping. |
| 261 | */ |
| 262 | static struct device *to_vmd_dev(struct device *dev) |
| 263 | { |
| 264 | struct pci_dev *pdev = to_pci_dev(dev); |
| 265 | struct vmd_dev *vmd = vmd_from_bus(pdev->bus); |
| 266 | |
| 267 | return &vmd->dev->dev; |
| 268 | } |
| 269 | |
| 270 | static struct dma_map_ops *vmd_dma_ops(struct device *dev) |
| 271 | { |
Keith Busch | ca8a8fa | 2016-05-17 11:13:24 -0600 | [diff] [blame] | 272 | return get_dma_ops(to_vmd_dev(dev)); |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static void *vmd_alloc(struct device *dev, size_t size, dma_addr_t *addr, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 276 | gfp_t flag, unsigned long attrs) |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 277 | { |
| 278 | return vmd_dma_ops(dev)->alloc(to_vmd_dev(dev), size, addr, flag, |
| 279 | attrs); |
| 280 | } |
| 281 | |
| 282 | static void vmd_free(struct device *dev, size_t size, void *vaddr, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 283 | dma_addr_t addr, unsigned long attrs) |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 284 | { |
| 285 | return vmd_dma_ops(dev)->free(to_vmd_dev(dev), size, vaddr, addr, |
| 286 | attrs); |
| 287 | } |
| 288 | |
| 289 | static int vmd_mmap(struct device *dev, struct vm_area_struct *vma, |
| 290 | void *cpu_addr, dma_addr_t addr, size_t size, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 291 | unsigned long attrs) |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 292 | { |
| 293 | return vmd_dma_ops(dev)->mmap(to_vmd_dev(dev), vma, cpu_addr, addr, |
| 294 | size, attrs); |
| 295 | } |
| 296 | |
| 297 | static int vmd_get_sgtable(struct device *dev, struct sg_table *sgt, |
| 298 | void *cpu_addr, dma_addr_t addr, size_t size, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 299 | unsigned long attrs) |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 300 | { |
| 301 | return vmd_dma_ops(dev)->get_sgtable(to_vmd_dev(dev), sgt, cpu_addr, |
| 302 | addr, size, attrs); |
| 303 | } |
| 304 | |
| 305 | static dma_addr_t vmd_map_page(struct device *dev, struct page *page, |
| 306 | unsigned long offset, size_t size, |
| 307 | enum dma_data_direction dir, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 308 | unsigned long attrs) |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 309 | { |
| 310 | return vmd_dma_ops(dev)->map_page(to_vmd_dev(dev), page, offset, size, |
| 311 | dir, attrs); |
| 312 | } |
| 313 | |
| 314 | static void vmd_unmap_page(struct device *dev, dma_addr_t addr, size_t size, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 315 | enum dma_data_direction dir, unsigned long attrs) |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 316 | { |
| 317 | vmd_dma_ops(dev)->unmap_page(to_vmd_dev(dev), addr, size, dir, attrs); |
| 318 | } |
| 319 | |
| 320 | static int vmd_map_sg(struct device *dev, struct scatterlist *sg, int nents, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 321 | enum dma_data_direction dir, unsigned long attrs) |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 322 | { |
| 323 | return vmd_dma_ops(dev)->map_sg(to_vmd_dev(dev), sg, nents, dir, attrs); |
| 324 | } |
| 325 | |
| 326 | static void vmd_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 327 | enum dma_data_direction dir, unsigned long attrs) |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 328 | { |
| 329 | vmd_dma_ops(dev)->unmap_sg(to_vmd_dev(dev), sg, nents, dir, attrs); |
| 330 | } |
| 331 | |
| 332 | static void vmd_sync_single_for_cpu(struct device *dev, dma_addr_t addr, |
| 333 | size_t size, enum dma_data_direction dir) |
| 334 | { |
| 335 | vmd_dma_ops(dev)->sync_single_for_cpu(to_vmd_dev(dev), addr, size, dir); |
| 336 | } |
| 337 | |
| 338 | static void vmd_sync_single_for_device(struct device *dev, dma_addr_t addr, |
| 339 | size_t size, enum dma_data_direction dir) |
| 340 | { |
| 341 | vmd_dma_ops(dev)->sync_single_for_device(to_vmd_dev(dev), addr, size, |
| 342 | dir); |
| 343 | } |
| 344 | |
| 345 | static void vmd_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, |
| 346 | int nents, enum dma_data_direction dir) |
| 347 | { |
| 348 | vmd_dma_ops(dev)->sync_sg_for_cpu(to_vmd_dev(dev), sg, nents, dir); |
| 349 | } |
| 350 | |
| 351 | static void vmd_sync_sg_for_device(struct device *dev, struct scatterlist *sg, |
| 352 | int nents, enum dma_data_direction dir) |
| 353 | { |
| 354 | vmd_dma_ops(dev)->sync_sg_for_device(to_vmd_dev(dev), sg, nents, dir); |
| 355 | } |
| 356 | |
| 357 | static int vmd_mapping_error(struct device *dev, dma_addr_t addr) |
| 358 | { |
| 359 | return vmd_dma_ops(dev)->mapping_error(to_vmd_dev(dev), addr); |
| 360 | } |
| 361 | |
| 362 | static int vmd_dma_supported(struct device *dev, u64 mask) |
| 363 | { |
| 364 | return vmd_dma_ops(dev)->dma_supported(to_vmd_dev(dev), mask); |
| 365 | } |
| 366 | |
| 367 | #ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK |
| 368 | static u64 vmd_get_required_mask(struct device *dev) |
| 369 | { |
| 370 | return vmd_dma_ops(dev)->get_required_mask(to_vmd_dev(dev)); |
| 371 | } |
| 372 | #endif |
| 373 | |
| 374 | static void vmd_teardown_dma_ops(struct vmd_dev *vmd) |
| 375 | { |
| 376 | struct dma_domain *domain = &vmd->dma_domain; |
| 377 | |
Keith Busch | ca8a8fa | 2016-05-17 11:13:24 -0600 | [diff] [blame] | 378 | if (get_dma_ops(&vmd->dev->dev)) |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 379 | del_dma_domain(domain); |
| 380 | } |
| 381 | |
| 382 | #define ASSIGN_VMD_DMA_OPS(source, dest, fn) \ |
| 383 | do { \ |
| 384 | if (source->fn) \ |
| 385 | dest->fn = vmd_##fn; \ |
| 386 | } while (0) |
| 387 | |
| 388 | static void vmd_setup_dma_ops(struct vmd_dev *vmd) |
| 389 | { |
Keith Busch | ca8a8fa | 2016-05-17 11:13:24 -0600 | [diff] [blame] | 390 | const struct dma_map_ops *source = get_dma_ops(&vmd->dev->dev); |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 391 | struct dma_map_ops *dest = &vmd->dma_ops; |
| 392 | struct dma_domain *domain = &vmd->dma_domain; |
| 393 | |
| 394 | domain->domain_nr = vmd->sysdata.domain; |
| 395 | domain->dma_ops = dest; |
| 396 | |
| 397 | if (!source) |
| 398 | return; |
| 399 | ASSIGN_VMD_DMA_OPS(source, dest, alloc); |
| 400 | ASSIGN_VMD_DMA_OPS(source, dest, free); |
| 401 | ASSIGN_VMD_DMA_OPS(source, dest, mmap); |
| 402 | ASSIGN_VMD_DMA_OPS(source, dest, get_sgtable); |
| 403 | ASSIGN_VMD_DMA_OPS(source, dest, map_page); |
| 404 | ASSIGN_VMD_DMA_OPS(source, dest, unmap_page); |
| 405 | ASSIGN_VMD_DMA_OPS(source, dest, map_sg); |
| 406 | ASSIGN_VMD_DMA_OPS(source, dest, unmap_sg); |
| 407 | ASSIGN_VMD_DMA_OPS(source, dest, sync_single_for_cpu); |
| 408 | ASSIGN_VMD_DMA_OPS(source, dest, sync_single_for_device); |
| 409 | ASSIGN_VMD_DMA_OPS(source, dest, sync_sg_for_cpu); |
| 410 | ASSIGN_VMD_DMA_OPS(source, dest, sync_sg_for_device); |
| 411 | ASSIGN_VMD_DMA_OPS(source, dest, mapping_error); |
| 412 | ASSIGN_VMD_DMA_OPS(source, dest, dma_supported); |
| 413 | #ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK |
| 414 | ASSIGN_VMD_DMA_OPS(source, dest, get_required_mask); |
| 415 | #endif |
| 416 | add_dma_domain(domain); |
| 417 | } |
| 418 | #undef ASSIGN_VMD_DMA_OPS |
| 419 | #else |
| 420 | static void vmd_teardown_dma_ops(struct vmd_dev *vmd) {} |
| 421 | static void vmd_setup_dma_ops(struct vmd_dev *vmd) {} |
| 422 | #endif |
| 423 | |
| 424 | static char __iomem *vmd_cfg_addr(struct vmd_dev *vmd, struct pci_bus *bus, |
| 425 | unsigned int devfn, int reg, int len) |
| 426 | { |
| 427 | char __iomem *addr = vmd->cfgbar + |
| 428 | (bus->number << 20) + (devfn << 12) + reg; |
| 429 | |
| 430 | if ((addr - vmd->cfgbar) + len >= |
| 431 | resource_size(&vmd->dev->resource[VMD_CFGBAR])) |
| 432 | return NULL; |
| 433 | |
| 434 | return addr; |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | * CPU may deadlock if config space is not serialized on some versions of this |
| 439 | * hardware, so all config space access is done under a spinlock. |
| 440 | */ |
| 441 | static int vmd_pci_read(struct pci_bus *bus, unsigned int devfn, int reg, |
| 442 | int len, u32 *value) |
| 443 | { |
| 444 | struct vmd_dev *vmd = vmd_from_bus(bus); |
| 445 | char __iomem *addr = vmd_cfg_addr(vmd, bus, devfn, reg, len); |
| 446 | unsigned long flags; |
| 447 | int ret = 0; |
| 448 | |
| 449 | if (!addr) |
| 450 | return -EFAULT; |
| 451 | |
| 452 | spin_lock_irqsave(&vmd->cfg_lock, flags); |
| 453 | switch (len) { |
| 454 | case 1: |
| 455 | *value = readb(addr); |
| 456 | break; |
| 457 | case 2: |
| 458 | *value = readw(addr); |
| 459 | break; |
| 460 | case 4: |
| 461 | *value = readl(addr); |
| 462 | break; |
| 463 | default: |
| 464 | ret = -EINVAL; |
| 465 | break; |
| 466 | } |
| 467 | spin_unlock_irqrestore(&vmd->cfg_lock, flags); |
| 468 | return ret; |
| 469 | } |
| 470 | |
| 471 | /* |
| 472 | * VMD h/w converts non-posted config writes to posted memory writes. The |
| 473 | * read-back in this function forces the completion so it returns only after |
| 474 | * the config space was written, as expected. |
| 475 | */ |
| 476 | static int vmd_pci_write(struct pci_bus *bus, unsigned int devfn, int reg, |
| 477 | int len, u32 value) |
| 478 | { |
| 479 | struct vmd_dev *vmd = vmd_from_bus(bus); |
| 480 | char __iomem *addr = vmd_cfg_addr(vmd, bus, devfn, reg, len); |
| 481 | unsigned long flags; |
| 482 | int ret = 0; |
| 483 | |
| 484 | if (!addr) |
| 485 | return -EFAULT; |
| 486 | |
| 487 | spin_lock_irqsave(&vmd->cfg_lock, flags); |
| 488 | switch (len) { |
| 489 | case 1: |
| 490 | writeb(value, addr); |
| 491 | readb(addr); |
| 492 | break; |
| 493 | case 2: |
| 494 | writew(value, addr); |
| 495 | readw(addr); |
| 496 | break; |
| 497 | case 4: |
| 498 | writel(value, addr); |
| 499 | readl(addr); |
| 500 | break; |
| 501 | default: |
| 502 | ret = -EINVAL; |
| 503 | break; |
| 504 | } |
| 505 | spin_unlock_irqrestore(&vmd->cfg_lock, flags); |
| 506 | return ret; |
| 507 | } |
| 508 | |
| 509 | static struct pci_ops vmd_ops = { |
| 510 | .read = vmd_pci_read, |
| 511 | .write = vmd_pci_write, |
| 512 | }; |
| 513 | |
Jon Derrick | 2c2c5c5 | 2016-02-24 10:06:37 -0700 | [diff] [blame] | 514 | static void vmd_attach_resources(struct vmd_dev *vmd) |
| 515 | { |
| 516 | vmd->dev->resource[VMD_MEMBAR1].child = &vmd->resources[1]; |
| 517 | vmd->dev->resource[VMD_MEMBAR2].child = &vmd->resources[2]; |
| 518 | } |
| 519 | |
| 520 | static void vmd_detach_resources(struct vmd_dev *vmd) |
| 521 | { |
| 522 | vmd->dev->resource[VMD_MEMBAR1].child = NULL; |
| 523 | vmd->dev->resource[VMD_MEMBAR2].child = NULL; |
| 524 | } |
| 525 | |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 526 | /* |
| 527 | * VMD domains start at 0x1000 to not clash with ACPI _SEG domains. |
| 528 | */ |
| 529 | static int vmd_find_free_domain(void) |
| 530 | { |
| 531 | int domain = 0xffff; |
| 532 | struct pci_bus *bus = NULL; |
| 533 | |
| 534 | while ((bus = pci_find_next_bus(bus)) != NULL) |
| 535 | domain = max_t(int, domain, pci_domain_nr(bus)); |
| 536 | return domain + 1; |
| 537 | } |
| 538 | |
| 539 | static int vmd_enable_domain(struct vmd_dev *vmd) |
| 540 | { |
| 541 | struct pci_sysdata *sd = &vmd->sysdata; |
| 542 | struct resource *res; |
| 543 | u32 upper_bits; |
| 544 | unsigned long flags; |
| 545 | LIST_HEAD(resources); |
| 546 | |
| 547 | res = &vmd->dev->resource[VMD_CFGBAR]; |
| 548 | vmd->resources[0] = (struct resource) { |
| 549 | .name = "VMD CFGBAR", |
Keith Busch | d068c35 | 2016-03-02 15:31:04 -0700 | [diff] [blame] | 550 | .start = 0, |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 551 | .end = (resource_size(res) >> 20) - 1, |
| 552 | .flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED, |
| 553 | }; |
| 554 | |
Keith Busch | 83cc54a | 2016-03-02 15:31:03 -0700 | [diff] [blame] | 555 | /* |
| 556 | * If the window is below 4GB, clear IORESOURCE_MEM_64 so we can |
| 557 | * put 32-bit resources in the window. |
| 558 | * |
| 559 | * There's no hardware reason why a 64-bit window *couldn't* |
| 560 | * contain a 32-bit resource, but pbus_size_mem() computes the |
| 561 | * bridge window size assuming a 64-bit window will contain no |
| 562 | * 32-bit resources. __pci_assign_resource() enforces that |
| 563 | * artificial restriction to make sure everything will fit. |
| 564 | * |
| 565 | * The only way we could use a 64-bit non-prefechable MEMBAR is |
| 566 | * if its address is <4GB so that we can convert it to a 32-bit |
| 567 | * resource. To be visible to the host OS, all VMD endpoints must |
| 568 | * be initially configured by platform BIOS, which includes setting |
| 569 | * up these resources. We can assume the device is configured |
| 570 | * according to the platform needs. |
| 571 | */ |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 572 | res = &vmd->dev->resource[VMD_MEMBAR1]; |
| 573 | upper_bits = upper_32_bits(res->end); |
| 574 | flags = res->flags & ~IORESOURCE_SIZEALIGN; |
| 575 | if (!upper_bits) |
| 576 | flags &= ~IORESOURCE_MEM_64; |
| 577 | vmd->resources[1] = (struct resource) { |
| 578 | .name = "VMD MEMBAR1", |
| 579 | .start = res->start, |
| 580 | .end = res->end, |
| 581 | .flags = flags, |
Jon Derrick | 2c2c5c5 | 2016-02-24 10:06:37 -0700 | [diff] [blame] | 582 | .parent = res, |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 583 | }; |
| 584 | |
| 585 | res = &vmd->dev->resource[VMD_MEMBAR2]; |
| 586 | upper_bits = upper_32_bits(res->end); |
| 587 | flags = res->flags & ~IORESOURCE_SIZEALIGN; |
| 588 | if (!upper_bits) |
| 589 | flags &= ~IORESOURCE_MEM_64; |
| 590 | vmd->resources[2] = (struct resource) { |
| 591 | .name = "VMD MEMBAR2", |
| 592 | .start = res->start + 0x2000, |
| 593 | .end = res->end, |
| 594 | .flags = flags, |
Jon Derrick | 2c2c5c5 | 2016-02-24 10:06:37 -0700 | [diff] [blame] | 595 | .parent = res, |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 596 | }; |
| 597 | |
| 598 | sd->domain = vmd_find_free_domain(); |
| 599 | if (sd->domain < 0) |
| 600 | return sd->domain; |
| 601 | |
| 602 | sd->node = pcibus_to_node(vmd->dev->bus); |
| 603 | |
| 604 | vmd->irq_domain = pci_msi_create_irq_domain(NULL, &vmd_msi_domain_info, |
Keith Busch | e382dff | 2016-06-20 09:39:52 -0600 | [diff] [blame] | 605 | x86_vector_domain); |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 606 | if (!vmd->irq_domain) |
| 607 | return -ENODEV; |
| 608 | |
| 609 | pci_add_resource(&resources, &vmd->resources[0]); |
| 610 | pci_add_resource(&resources, &vmd->resources[1]); |
| 611 | pci_add_resource(&resources, &vmd->resources[2]); |
| 612 | vmd->bus = pci_create_root_bus(&vmd->dev->dev, 0, &vmd_ops, sd, |
| 613 | &resources); |
| 614 | if (!vmd->bus) { |
| 615 | pci_free_resource_list(&resources); |
| 616 | irq_domain_remove(vmd->irq_domain); |
| 617 | return -ENODEV; |
| 618 | } |
| 619 | |
Jon Derrick | 2c2c5c5 | 2016-02-24 10:06:37 -0700 | [diff] [blame] | 620 | vmd_attach_resources(vmd); |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 621 | vmd_setup_dma_ops(vmd); |
| 622 | dev_set_msi_domain(&vmd->bus->dev, vmd->irq_domain); |
| 623 | pci_rescan_bus(vmd->bus); |
| 624 | |
| 625 | WARN(sysfs_create_link(&vmd->dev->dev.kobj, &vmd->bus->dev.kobj, |
| 626 | "domain"), "Can't create symlink to domain\n"); |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | static irqreturn_t vmd_irq(int irq, void *data) |
| 631 | { |
| 632 | struct vmd_irq_list *irqs = data; |
| 633 | struct vmd_irq *vmdirq; |
| 634 | |
| 635 | rcu_read_lock(); |
| 636 | list_for_each_entry_rcu(vmdirq, &irqs->irq_list, node) |
| 637 | generic_handle_irq(vmdirq->virq); |
| 638 | rcu_read_unlock(); |
| 639 | |
| 640 | return IRQ_HANDLED; |
| 641 | } |
| 642 | |
| 643 | static int vmd_probe(struct pci_dev *dev, const struct pci_device_id *id) |
| 644 | { |
| 645 | struct vmd_dev *vmd; |
| 646 | int i, err; |
| 647 | |
| 648 | if (resource_size(&dev->resource[VMD_CFGBAR]) < (1 << 20)) |
| 649 | return -ENOMEM; |
| 650 | |
| 651 | vmd = devm_kzalloc(&dev->dev, sizeof(*vmd), GFP_KERNEL); |
| 652 | if (!vmd) |
| 653 | return -ENOMEM; |
| 654 | |
| 655 | vmd->dev = dev; |
| 656 | err = pcim_enable_device(dev); |
| 657 | if (err < 0) |
| 658 | return err; |
| 659 | |
| 660 | vmd->cfgbar = pcim_iomap(dev, VMD_CFGBAR, 0); |
| 661 | if (!vmd->cfgbar) |
| 662 | return -ENOMEM; |
| 663 | |
| 664 | pci_set_master(dev); |
| 665 | if (dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(64)) && |
| 666 | dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32))) |
| 667 | return -ENODEV; |
| 668 | |
| 669 | vmd->msix_count = pci_msix_vec_count(dev); |
| 670 | if (vmd->msix_count < 0) |
| 671 | return -ENODEV; |
| 672 | |
Jon Derrick | 75de9b4 | 2016-08-29 11:19:02 -0600 | [diff] [blame^] | 673 | vmd->msix_count = pci_alloc_irq_vectors(dev, 1, vmd->msix_count, |
| 674 | PCI_IRQ_MSIX | PCI_IRQ_AFFINITY); |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 675 | if (vmd->msix_count < 0) |
| 676 | return vmd->msix_count; |
| 677 | |
Jon Derrick | c68db51 | 2016-08-29 11:19:01 -0600 | [diff] [blame] | 678 | vmd->irqs = devm_kcalloc(&dev->dev, vmd->msix_count, sizeof(*vmd->irqs), |
| 679 | GFP_KERNEL); |
| 680 | if (!vmd->irqs) |
| 681 | return -ENOMEM; |
| 682 | |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 683 | for (i = 0; i < vmd->msix_count; i++) { |
| 684 | INIT_LIST_HEAD(&vmd->irqs[i].irq_list); |
Jon Derrick | 75de9b4 | 2016-08-29 11:19:02 -0600 | [diff] [blame^] | 685 | vmd->irqs[i].vmd_vector = pci_irq_vector(dev, i); |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 686 | vmd->irqs[i].index = i; |
| 687 | |
| 688 | err = devm_request_irq(&dev->dev, vmd->irqs[i].vmd_vector, |
| 689 | vmd_irq, 0, "vmd", &vmd->irqs[i]); |
| 690 | if (err) |
| 691 | return err; |
| 692 | } |
| 693 | |
| 694 | spin_lock_init(&vmd->cfg_lock); |
| 695 | pci_set_drvdata(dev, vmd); |
| 696 | err = vmd_enable_domain(vmd); |
| 697 | if (err) |
| 698 | return err; |
| 699 | |
| 700 | dev_info(&vmd->dev->dev, "Bound to PCI domain %04x\n", |
| 701 | vmd->sysdata.domain); |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | static void vmd_remove(struct pci_dev *dev) |
| 706 | { |
| 707 | struct vmd_dev *vmd = pci_get_drvdata(dev); |
| 708 | |
Jon Derrick | 2c2c5c5 | 2016-02-24 10:06:37 -0700 | [diff] [blame] | 709 | vmd_detach_resources(vmd); |
Keith Busch | 185a383 | 2016-01-12 13:18:10 -0700 | [diff] [blame] | 710 | pci_set_drvdata(dev, NULL); |
| 711 | sysfs_remove_link(&vmd->dev->dev.kobj, "domain"); |
| 712 | pci_stop_root_bus(vmd->bus); |
| 713 | pci_remove_root_bus(vmd->bus); |
| 714 | vmd_teardown_dma_ops(vmd); |
| 715 | irq_domain_remove(vmd->irq_domain); |
| 716 | } |
| 717 | |
| 718 | #ifdef CONFIG_PM |
| 719 | static int vmd_suspend(struct device *dev) |
| 720 | { |
| 721 | struct pci_dev *pdev = to_pci_dev(dev); |
| 722 | |
| 723 | pci_save_state(pdev); |
| 724 | return 0; |
| 725 | } |
| 726 | |
| 727 | static int vmd_resume(struct device *dev) |
| 728 | { |
| 729 | struct pci_dev *pdev = to_pci_dev(dev); |
| 730 | |
| 731 | pci_restore_state(pdev); |
| 732 | return 0; |
| 733 | } |
| 734 | #endif |
| 735 | static SIMPLE_DEV_PM_OPS(vmd_dev_pm_ops, vmd_suspend, vmd_resume); |
| 736 | |
| 737 | static const struct pci_device_id vmd_ids[] = { |
| 738 | {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x201d),}, |
| 739 | {0,} |
| 740 | }; |
| 741 | MODULE_DEVICE_TABLE(pci, vmd_ids); |
| 742 | |
| 743 | static struct pci_driver vmd_drv = { |
| 744 | .name = "vmd", |
| 745 | .id_table = vmd_ids, |
| 746 | .probe = vmd_probe, |
| 747 | .remove = vmd_remove, |
| 748 | .driver = { |
| 749 | .pm = &vmd_dev_pm_ops, |
| 750 | }, |
| 751 | }; |
| 752 | module_pci_driver(vmd_drv); |
| 753 | |
| 754 | MODULE_AUTHOR("Intel Corporation"); |
| 755 | MODULE_LICENSE("GPL v2"); |
| 756 | MODULE_VERSION("0.6"); |