blob: a021b7b0eb69af85d557684c064842ed87ebcc1b [file] [log] [blame]
Keith Busch185a3832016-01-12 13:18:10 -07001/*
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 */
37static 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 */
49struct 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 */
64struct 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
72struct vmd_dev {
73 struct pci_dev *dev;
74
75 spinlock_t cfg_lock;
76 char __iomem *cfgbar;
77
78 int msix_count;
79 struct msix_entry *msix_entries;
80 struct vmd_irq_list *irqs;
81
82 struct pci_sysdata sysdata;
83 struct resource resources[3];
84 struct irq_domain *irq_domain;
85 struct pci_bus *bus;
86
87#ifdef CONFIG_X86_DEV_DMA_OPS
88 struct dma_map_ops dma_ops;
89 struct dma_domain dma_domain;
90#endif
91};
92
93static inline struct vmd_dev *vmd_from_bus(struct pci_bus *bus)
94{
95 return container_of(bus->sysdata, struct vmd_dev, sysdata);
96}
97
98/*
99 * Drivers managing a device in a VMD domain allocate their own IRQs as before,
100 * but the MSI entry for the hardware it's driving will be programmed with a
101 * destination ID for the VMD MSI-X table. The VMD muxes interrupts in its
102 * domain into one of its own, and the VMD driver de-muxes these for the
103 * handlers sharing that VMD IRQ. The vmd irq_domain provides the operations
104 * and irq_chip to set this up.
105 */
106static void vmd_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
107{
108 struct vmd_irq *vmdirq = data->chip_data;
109 struct vmd_irq_list *irq = vmdirq->irq;
110
111 msg->address_hi = MSI_ADDR_BASE_HI;
112 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_DEST_ID(irq->index);
113 msg->data = 0;
114}
115
116/*
117 * We rely on MSI_FLAG_USE_DEF_CHIP_OPS to set the IRQ mask/unmask ops.
118 */
119static void vmd_irq_enable(struct irq_data *data)
120{
121 struct vmd_irq *vmdirq = data->chip_data;
Jon Derrick3f57ff42016-06-20 09:39:51 -0600122 unsigned long flags;
Keith Busch185a3832016-01-12 13:18:10 -0700123
Jon Derrick3f57ff42016-06-20 09:39:51 -0600124 raw_spin_lock_irqsave(&list_lock, flags);
Keith Busch185a3832016-01-12 13:18:10 -0700125 list_add_tail_rcu(&vmdirq->node, &vmdirq->irq->irq_list);
Jon Derrick3f57ff42016-06-20 09:39:51 -0600126 raw_spin_unlock_irqrestore(&list_lock, flags);
Keith Busch185a3832016-01-12 13:18:10 -0700127
128 data->chip->irq_unmask(data);
129}
130
131static void vmd_irq_disable(struct irq_data *data)
132{
133 struct vmd_irq *vmdirq = data->chip_data;
Jon Derrick3f57ff42016-06-20 09:39:51 -0600134 unsigned long flags;
Keith Busch185a3832016-01-12 13:18:10 -0700135
136 data->chip->irq_mask(data);
137
Jon Derrick3f57ff42016-06-20 09:39:51 -0600138 raw_spin_lock_irqsave(&list_lock, flags);
Keith Busch185a3832016-01-12 13:18:10 -0700139 list_del_rcu(&vmdirq->node);
Keith Busch97e92302016-05-17 11:22:18 -0600140 INIT_LIST_HEAD_RCU(&vmdirq->node);
Jon Derrick3f57ff42016-06-20 09:39:51 -0600141 raw_spin_unlock_irqrestore(&list_lock, flags);
Keith Busch185a3832016-01-12 13:18:10 -0700142}
143
144/*
145 * XXX: Stubbed until we develop acceptable way to not create conflicts with
146 * other devices sharing the same vector.
147 */
148static int vmd_irq_set_affinity(struct irq_data *data,
149 const struct cpumask *dest, bool force)
150{
151 return -EINVAL;
152}
153
154static struct irq_chip vmd_msi_controller = {
155 .name = "VMD-MSI",
156 .irq_enable = vmd_irq_enable,
157 .irq_disable = vmd_irq_disable,
158 .irq_compose_msi_msg = vmd_compose_msi_msg,
159 .irq_set_affinity = vmd_irq_set_affinity,
160};
161
162static irq_hw_number_t vmd_get_hwirq(struct msi_domain_info *info,
163 msi_alloc_info_t *arg)
164{
165 return 0;
166}
167
168/*
169 * XXX: We can be even smarter selecting the best IRQ once we solve the
170 * affinity problem.
171 */
Keith Busch9c205302016-06-20 09:39:53 -0600172static struct vmd_irq_list *vmd_next_irq(struct vmd_dev *vmd, struct msi_desc *desc)
Keith Busch185a3832016-01-12 13:18:10 -0700173{
Keith Busch9c205302016-06-20 09:39:53 -0600174 int i, best = 1;
Jon Derrick3f57ff42016-06-20 09:39:51 -0600175 unsigned long flags;
Keith Busch185a3832016-01-12 13:18:10 -0700176
Keith Busch9c205302016-06-20 09:39:53 -0600177 if (!desc->msi_attrib.is_msix || vmd->msix_count == 1)
178 return &vmd->irqs[0];
179
Jon Derrick3f57ff42016-06-20 09:39:51 -0600180 raw_spin_lock_irqsave(&list_lock, flags);
Keith Busch185a3832016-01-12 13:18:10 -0700181 for (i = 1; i < vmd->msix_count; i++)
182 if (vmd->irqs[i].count < vmd->irqs[best].count)
183 best = i;
184 vmd->irqs[best].count++;
Jon Derrick3f57ff42016-06-20 09:39:51 -0600185 raw_spin_unlock_irqrestore(&list_lock, flags);
Keith Busch185a3832016-01-12 13:18:10 -0700186
187 return &vmd->irqs[best];
188}
189
190static int vmd_msi_init(struct irq_domain *domain, struct msi_domain_info *info,
191 unsigned int virq, irq_hw_number_t hwirq,
192 msi_alloc_info_t *arg)
193{
Keith Busch9c205302016-06-20 09:39:53 -0600194 struct msi_desc *desc = arg->desc;
195 struct vmd_dev *vmd = vmd_from_bus(msi_desc_to_pci_dev(desc)->bus);
Keith Busch185a3832016-01-12 13:18:10 -0700196 struct vmd_irq *vmdirq = kzalloc(sizeof(*vmdirq), GFP_KERNEL);
197
198 if (!vmdirq)
199 return -ENOMEM;
200
201 INIT_LIST_HEAD(&vmdirq->node);
Keith Busch9c205302016-06-20 09:39:53 -0600202 vmdirq->irq = vmd_next_irq(vmd, desc);
Keith Busch185a3832016-01-12 13:18:10 -0700203 vmdirq->virq = virq;
204
205 irq_domain_set_info(domain, virq, vmdirq->irq->vmd_vector, info->chip,
Keith Busch30ce0352016-06-17 16:00:21 -0600206 vmdirq, handle_untracked_irq, vmd, NULL);
Keith Busch185a3832016-01-12 13:18:10 -0700207 return 0;
208}
209
210static void vmd_msi_free(struct irq_domain *domain,
211 struct msi_domain_info *info, unsigned int virq)
212{
213 struct vmd_irq *vmdirq = irq_get_chip_data(virq);
Jon Derrick3f57ff42016-06-20 09:39:51 -0600214 unsigned long flags;
Keith Busch185a3832016-01-12 13:18:10 -0700215
216 /* XXX: Potential optimization to rebalance */
Jon Derrick3f57ff42016-06-20 09:39:51 -0600217 raw_spin_lock_irqsave(&list_lock, flags);
Keith Busch185a3832016-01-12 13:18:10 -0700218 vmdirq->irq->count--;
Jon Derrick3f57ff42016-06-20 09:39:51 -0600219 raw_spin_unlock_irqrestore(&list_lock, flags);
Keith Busch185a3832016-01-12 13:18:10 -0700220
221 kfree_rcu(vmdirq, rcu);
222}
223
224static int vmd_msi_prepare(struct irq_domain *domain, struct device *dev,
225 int nvec, msi_alloc_info_t *arg)
226{
227 struct pci_dev *pdev = to_pci_dev(dev);
228 struct vmd_dev *vmd = vmd_from_bus(pdev->bus);
229
230 if (nvec > vmd->msix_count)
231 return vmd->msix_count;
232
233 memset(arg, 0, sizeof(*arg));
234 return 0;
235}
236
237static void vmd_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
238{
239 arg->desc = desc;
240}
241
242static struct msi_domain_ops vmd_msi_domain_ops = {
243 .get_hwirq = vmd_get_hwirq,
244 .msi_init = vmd_msi_init,
245 .msi_free = vmd_msi_free,
246 .msi_prepare = vmd_msi_prepare,
247 .set_desc = vmd_set_desc,
248};
249
250static struct msi_domain_info vmd_msi_domain_info = {
251 .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
252 MSI_FLAG_PCI_MSIX,
253 .ops = &vmd_msi_domain_ops,
254 .chip = &vmd_msi_controller,
255};
256
257#ifdef CONFIG_X86_DEV_DMA_OPS
258/*
259 * VMD replaces the requester ID with its own. DMA mappings for devices in a
260 * VMD domain need to be mapped for the VMD, not the device requiring
261 * the mapping.
262 */
263static struct device *to_vmd_dev(struct device *dev)
264{
265 struct pci_dev *pdev = to_pci_dev(dev);
266 struct vmd_dev *vmd = vmd_from_bus(pdev->bus);
267
268 return &vmd->dev->dev;
269}
270
271static struct dma_map_ops *vmd_dma_ops(struct device *dev)
272{
Keith Buschca8a8fa2016-05-17 11:13:24 -0600273 return get_dma_ops(to_vmd_dev(dev));
Keith Busch185a3832016-01-12 13:18:10 -0700274}
275
276static void *vmd_alloc(struct device *dev, size_t size, dma_addr_t *addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700277 gfp_t flag, unsigned long attrs)
Keith Busch185a3832016-01-12 13:18:10 -0700278{
279 return vmd_dma_ops(dev)->alloc(to_vmd_dev(dev), size, addr, flag,
280 attrs);
281}
282
283static void vmd_free(struct device *dev, size_t size, void *vaddr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700284 dma_addr_t addr, unsigned long attrs)
Keith Busch185a3832016-01-12 13:18:10 -0700285{
286 return vmd_dma_ops(dev)->free(to_vmd_dev(dev), size, vaddr, addr,
287 attrs);
288}
289
290static int vmd_mmap(struct device *dev, struct vm_area_struct *vma,
291 void *cpu_addr, dma_addr_t addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700292 unsigned long attrs)
Keith Busch185a3832016-01-12 13:18:10 -0700293{
294 return vmd_dma_ops(dev)->mmap(to_vmd_dev(dev), vma, cpu_addr, addr,
295 size, attrs);
296}
297
298static int vmd_get_sgtable(struct device *dev, struct sg_table *sgt,
299 void *cpu_addr, dma_addr_t addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700300 unsigned long attrs)
Keith Busch185a3832016-01-12 13:18:10 -0700301{
302 return vmd_dma_ops(dev)->get_sgtable(to_vmd_dev(dev), sgt, cpu_addr,
303 addr, size, attrs);
304}
305
306static dma_addr_t vmd_map_page(struct device *dev, struct page *page,
307 unsigned long offset, size_t size,
308 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700309 unsigned long attrs)
Keith Busch185a3832016-01-12 13:18:10 -0700310{
311 return vmd_dma_ops(dev)->map_page(to_vmd_dev(dev), page, offset, size,
312 dir, attrs);
313}
314
315static void vmd_unmap_page(struct device *dev, dma_addr_t addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700316 enum dma_data_direction dir, unsigned long attrs)
Keith Busch185a3832016-01-12 13:18:10 -0700317{
318 vmd_dma_ops(dev)->unmap_page(to_vmd_dev(dev), addr, size, dir, attrs);
319}
320
321static int vmd_map_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700322 enum dma_data_direction dir, unsigned long attrs)
Keith Busch185a3832016-01-12 13:18:10 -0700323{
324 return vmd_dma_ops(dev)->map_sg(to_vmd_dev(dev), sg, nents, dir, attrs);
325}
326
327static void vmd_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700328 enum dma_data_direction dir, unsigned long attrs)
Keith Busch185a3832016-01-12 13:18:10 -0700329{
330 vmd_dma_ops(dev)->unmap_sg(to_vmd_dev(dev), sg, nents, dir, attrs);
331}
332
333static void vmd_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
334 size_t size, enum dma_data_direction dir)
335{
336 vmd_dma_ops(dev)->sync_single_for_cpu(to_vmd_dev(dev), addr, size, dir);
337}
338
339static void vmd_sync_single_for_device(struct device *dev, dma_addr_t addr,
340 size_t size, enum dma_data_direction dir)
341{
342 vmd_dma_ops(dev)->sync_single_for_device(to_vmd_dev(dev), addr, size,
343 dir);
344}
345
346static void vmd_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
347 int nents, enum dma_data_direction dir)
348{
349 vmd_dma_ops(dev)->sync_sg_for_cpu(to_vmd_dev(dev), sg, nents, dir);
350}
351
352static void vmd_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
353 int nents, enum dma_data_direction dir)
354{
355 vmd_dma_ops(dev)->sync_sg_for_device(to_vmd_dev(dev), sg, nents, dir);
356}
357
358static int vmd_mapping_error(struct device *dev, dma_addr_t addr)
359{
360 return vmd_dma_ops(dev)->mapping_error(to_vmd_dev(dev), addr);
361}
362
363static int vmd_dma_supported(struct device *dev, u64 mask)
364{
365 return vmd_dma_ops(dev)->dma_supported(to_vmd_dev(dev), mask);
366}
367
368#ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK
369static u64 vmd_get_required_mask(struct device *dev)
370{
371 return vmd_dma_ops(dev)->get_required_mask(to_vmd_dev(dev));
372}
373#endif
374
375static void vmd_teardown_dma_ops(struct vmd_dev *vmd)
376{
377 struct dma_domain *domain = &vmd->dma_domain;
378
Keith Buschca8a8fa2016-05-17 11:13:24 -0600379 if (get_dma_ops(&vmd->dev->dev))
Keith Busch185a3832016-01-12 13:18:10 -0700380 del_dma_domain(domain);
381}
382
383#define ASSIGN_VMD_DMA_OPS(source, dest, fn) \
384 do { \
385 if (source->fn) \
386 dest->fn = vmd_##fn; \
387 } while (0)
388
389static void vmd_setup_dma_ops(struct vmd_dev *vmd)
390{
Keith Buschca8a8fa2016-05-17 11:13:24 -0600391 const struct dma_map_ops *source = get_dma_ops(&vmd->dev->dev);
Keith Busch185a3832016-01-12 13:18:10 -0700392 struct dma_map_ops *dest = &vmd->dma_ops;
393 struct dma_domain *domain = &vmd->dma_domain;
394
395 domain->domain_nr = vmd->sysdata.domain;
396 domain->dma_ops = dest;
397
398 if (!source)
399 return;
400 ASSIGN_VMD_DMA_OPS(source, dest, alloc);
401 ASSIGN_VMD_DMA_OPS(source, dest, free);
402 ASSIGN_VMD_DMA_OPS(source, dest, mmap);
403 ASSIGN_VMD_DMA_OPS(source, dest, get_sgtable);
404 ASSIGN_VMD_DMA_OPS(source, dest, map_page);
405 ASSIGN_VMD_DMA_OPS(source, dest, unmap_page);
406 ASSIGN_VMD_DMA_OPS(source, dest, map_sg);
407 ASSIGN_VMD_DMA_OPS(source, dest, unmap_sg);
408 ASSIGN_VMD_DMA_OPS(source, dest, sync_single_for_cpu);
409 ASSIGN_VMD_DMA_OPS(source, dest, sync_single_for_device);
410 ASSIGN_VMD_DMA_OPS(source, dest, sync_sg_for_cpu);
411 ASSIGN_VMD_DMA_OPS(source, dest, sync_sg_for_device);
412 ASSIGN_VMD_DMA_OPS(source, dest, mapping_error);
413 ASSIGN_VMD_DMA_OPS(source, dest, dma_supported);
414#ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK
415 ASSIGN_VMD_DMA_OPS(source, dest, get_required_mask);
416#endif
417 add_dma_domain(domain);
418}
419#undef ASSIGN_VMD_DMA_OPS
420#else
421static void vmd_teardown_dma_ops(struct vmd_dev *vmd) {}
422static void vmd_setup_dma_ops(struct vmd_dev *vmd) {}
423#endif
424
425static char __iomem *vmd_cfg_addr(struct vmd_dev *vmd, struct pci_bus *bus,
426 unsigned int devfn, int reg, int len)
427{
428 char __iomem *addr = vmd->cfgbar +
429 (bus->number << 20) + (devfn << 12) + reg;
430
431 if ((addr - vmd->cfgbar) + len >=
432 resource_size(&vmd->dev->resource[VMD_CFGBAR]))
433 return NULL;
434
435 return addr;
436}
437
438/*
439 * CPU may deadlock if config space is not serialized on some versions of this
440 * hardware, so all config space access is done under a spinlock.
441 */
442static int vmd_pci_read(struct pci_bus *bus, unsigned int devfn, int reg,
443 int len, u32 *value)
444{
445 struct vmd_dev *vmd = vmd_from_bus(bus);
446 char __iomem *addr = vmd_cfg_addr(vmd, bus, devfn, reg, len);
447 unsigned long flags;
448 int ret = 0;
449
450 if (!addr)
451 return -EFAULT;
452
453 spin_lock_irqsave(&vmd->cfg_lock, flags);
454 switch (len) {
455 case 1:
456 *value = readb(addr);
457 break;
458 case 2:
459 *value = readw(addr);
460 break;
461 case 4:
462 *value = readl(addr);
463 break;
464 default:
465 ret = -EINVAL;
466 break;
467 }
468 spin_unlock_irqrestore(&vmd->cfg_lock, flags);
469 return ret;
470}
471
472/*
473 * VMD h/w converts non-posted config writes to posted memory writes. The
474 * read-back in this function forces the completion so it returns only after
475 * the config space was written, as expected.
476 */
477static int vmd_pci_write(struct pci_bus *bus, unsigned int devfn, int reg,
478 int len, u32 value)
479{
480 struct vmd_dev *vmd = vmd_from_bus(bus);
481 char __iomem *addr = vmd_cfg_addr(vmd, bus, devfn, reg, len);
482 unsigned long flags;
483 int ret = 0;
484
485 if (!addr)
486 return -EFAULT;
487
488 spin_lock_irqsave(&vmd->cfg_lock, flags);
489 switch (len) {
490 case 1:
491 writeb(value, addr);
492 readb(addr);
493 break;
494 case 2:
495 writew(value, addr);
496 readw(addr);
497 break;
498 case 4:
499 writel(value, addr);
500 readl(addr);
501 break;
502 default:
503 ret = -EINVAL;
504 break;
505 }
506 spin_unlock_irqrestore(&vmd->cfg_lock, flags);
507 return ret;
508}
509
510static struct pci_ops vmd_ops = {
511 .read = vmd_pci_read,
512 .write = vmd_pci_write,
513};
514
Jon Derrick2c2c5c52016-02-24 10:06:37 -0700515static void vmd_attach_resources(struct vmd_dev *vmd)
516{
517 vmd->dev->resource[VMD_MEMBAR1].child = &vmd->resources[1];
518 vmd->dev->resource[VMD_MEMBAR2].child = &vmd->resources[2];
519}
520
521static void vmd_detach_resources(struct vmd_dev *vmd)
522{
523 vmd->dev->resource[VMD_MEMBAR1].child = NULL;
524 vmd->dev->resource[VMD_MEMBAR2].child = NULL;
525}
526
Keith Busch185a3832016-01-12 13:18:10 -0700527/*
528 * VMD domains start at 0x1000 to not clash with ACPI _SEG domains.
529 */
530static int vmd_find_free_domain(void)
531{
532 int domain = 0xffff;
533 struct pci_bus *bus = NULL;
534
535 while ((bus = pci_find_next_bus(bus)) != NULL)
536 domain = max_t(int, domain, pci_domain_nr(bus));
537 return domain + 1;
538}
539
540static int vmd_enable_domain(struct vmd_dev *vmd)
541{
542 struct pci_sysdata *sd = &vmd->sysdata;
543 struct resource *res;
544 u32 upper_bits;
545 unsigned long flags;
546 LIST_HEAD(resources);
547
548 res = &vmd->dev->resource[VMD_CFGBAR];
549 vmd->resources[0] = (struct resource) {
550 .name = "VMD CFGBAR",
Keith Buschd068c352016-03-02 15:31:04 -0700551 .start = 0,
Keith Busch185a3832016-01-12 13:18:10 -0700552 .end = (resource_size(res) >> 20) - 1,
553 .flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED,
554 };
555
Keith Busch83cc54a2016-03-02 15:31:03 -0700556 /*
557 * If the window is below 4GB, clear IORESOURCE_MEM_64 so we can
558 * put 32-bit resources in the window.
559 *
560 * There's no hardware reason why a 64-bit window *couldn't*
561 * contain a 32-bit resource, but pbus_size_mem() computes the
562 * bridge window size assuming a 64-bit window will contain no
563 * 32-bit resources. __pci_assign_resource() enforces that
564 * artificial restriction to make sure everything will fit.
565 *
566 * The only way we could use a 64-bit non-prefechable MEMBAR is
567 * if its address is <4GB so that we can convert it to a 32-bit
568 * resource. To be visible to the host OS, all VMD endpoints must
569 * be initially configured by platform BIOS, which includes setting
570 * up these resources. We can assume the device is configured
571 * according to the platform needs.
572 */
Keith Busch185a3832016-01-12 13:18:10 -0700573 res = &vmd->dev->resource[VMD_MEMBAR1];
574 upper_bits = upper_32_bits(res->end);
575 flags = res->flags & ~IORESOURCE_SIZEALIGN;
576 if (!upper_bits)
577 flags &= ~IORESOURCE_MEM_64;
578 vmd->resources[1] = (struct resource) {
579 .name = "VMD MEMBAR1",
580 .start = res->start,
581 .end = res->end,
582 .flags = flags,
Jon Derrick2c2c5c52016-02-24 10:06:37 -0700583 .parent = res,
Keith Busch185a3832016-01-12 13:18:10 -0700584 };
585
586 res = &vmd->dev->resource[VMD_MEMBAR2];
587 upper_bits = upper_32_bits(res->end);
588 flags = res->flags & ~IORESOURCE_SIZEALIGN;
589 if (!upper_bits)
590 flags &= ~IORESOURCE_MEM_64;
591 vmd->resources[2] = (struct resource) {
592 .name = "VMD MEMBAR2",
593 .start = res->start + 0x2000,
594 .end = res->end,
595 .flags = flags,
Jon Derrick2c2c5c52016-02-24 10:06:37 -0700596 .parent = res,
Keith Busch185a3832016-01-12 13:18:10 -0700597 };
598
Keith Busch31618322016-09-13 09:05:40 -0600599 sd->vmd_domain = true;
Keith Busch185a3832016-01-12 13:18:10 -0700600 sd->domain = vmd_find_free_domain();
601 if (sd->domain < 0)
602 return sd->domain;
603
604 sd->node = pcibus_to_node(vmd->dev->bus);
605
606 vmd->irq_domain = pci_msi_create_irq_domain(NULL, &vmd_msi_domain_info,
Keith Busche382dff2016-06-20 09:39:52 -0600607 x86_vector_domain);
Keith Busch185a3832016-01-12 13:18:10 -0700608 if (!vmd->irq_domain)
609 return -ENODEV;
610
611 pci_add_resource(&resources, &vmd->resources[0]);
612 pci_add_resource(&resources, &vmd->resources[1]);
613 pci_add_resource(&resources, &vmd->resources[2]);
614 vmd->bus = pci_create_root_bus(&vmd->dev->dev, 0, &vmd_ops, sd,
615 &resources);
616 if (!vmd->bus) {
617 pci_free_resource_list(&resources);
618 irq_domain_remove(vmd->irq_domain);
619 return -ENODEV;
620 }
621
Jon Derrick2c2c5c52016-02-24 10:06:37 -0700622 vmd_attach_resources(vmd);
Keith Busch185a3832016-01-12 13:18:10 -0700623 vmd_setup_dma_ops(vmd);
624 dev_set_msi_domain(&vmd->bus->dev, vmd->irq_domain);
625 pci_rescan_bus(vmd->bus);
626
627 WARN(sysfs_create_link(&vmd->dev->dev.kobj, &vmd->bus->dev.kobj,
628 "domain"), "Can't create symlink to domain\n");
629 return 0;
630}
631
632static irqreturn_t vmd_irq(int irq, void *data)
633{
634 struct vmd_irq_list *irqs = data;
635 struct vmd_irq *vmdirq;
636
637 rcu_read_lock();
638 list_for_each_entry_rcu(vmdirq, &irqs->irq_list, node)
639 generic_handle_irq(vmdirq->virq);
640 rcu_read_unlock();
641
642 return IRQ_HANDLED;
643}
644
645static int vmd_probe(struct pci_dev *dev, const struct pci_device_id *id)
646{
647 struct vmd_dev *vmd;
648 int i, err;
649
650 if (resource_size(&dev->resource[VMD_CFGBAR]) < (1 << 20))
651 return -ENOMEM;
652
653 vmd = devm_kzalloc(&dev->dev, sizeof(*vmd), GFP_KERNEL);
654 if (!vmd)
655 return -ENOMEM;
656
657 vmd->dev = dev;
658 err = pcim_enable_device(dev);
659 if (err < 0)
660 return err;
661
662 vmd->cfgbar = pcim_iomap(dev, VMD_CFGBAR, 0);
663 if (!vmd->cfgbar)
664 return -ENOMEM;
665
666 pci_set_master(dev);
667 if (dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(64)) &&
668 dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32)))
669 return -ENODEV;
670
671 vmd->msix_count = pci_msix_vec_count(dev);
672 if (vmd->msix_count < 0)
673 return -ENODEV;
674
675 vmd->irqs = devm_kcalloc(&dev->dev, vmd->msix_count, sizeof(*vmd->irqs),
676 GFP_KERNEL);
677 if (!vmd->irqs)
678 return -ENOMEM;
679
680 vmd->msix_entries = devm_kcalloc(&dev->dev, vmd->msix_count,
681 sizeof(*vmd->msix_entries),
682 GFP_KERNEL);
683 if (!vmd->msix_entries)
684 return -ENOMEM;
685 for (i = 0; i < vmd->msix_count; i++)
686 vmd->msix_entries[i].entry = i;
687
688 vmd->msix_count = pci_enable_msix_range(vmd->dev, vmd->msix_entries, 1,
689 vmd->msix_count);
690 if (vmd->msix_count < 0)
691 return vmd->msix_count;
692
693 for (i = 0; i < vmd->msix_count; i++) {
694 INIT_LIST_HEAD(&vmd->irqs[i].irq_list);
695 vmd->irqs[i].vmd_vector = vmd->msix_entries[i].vector;
696 vmd->irqs[i].index = i;
697
698 err = devm_request_irq(&dev->dev, vmd->irqs[i].vmd_vector,
699 vmd_irq, 0, "vmd", &vmd->irqs[i]);
700 if (err)
701 return err;
702 }
703
704 spin_lock_init(&vmd->cfg_lock);
705 pci_set_drvdata(dev, vmd);
706 err = vmd_enable_domain(vmd);
707 if (err)
708 return err;
709
710 dev_info(&vmd->dev->dev, "Bound to PCI domain %04x\n",
711 vmd->sysdata.domain);
712 return 0;
713}
714
715static void vmd_remove(struct pci_dev *dev)
716{
717 struct vmd_dev *vmd = pci_get_drvdata(dev);
718
Jon Derrick2c2c5c52016-02-24 10:06:37 -0700719 vmd_detach_resources(vmd);
Keith Busch185a3832016-01-12 13:18:10 -0700720 pci_set_drvdata(dev, NULL);
721 sysfs_remove_link(&vmd->dev->dev.kobj, "domain");
722 pci_stop_root_bus(vmd->bus);
723 pci_remove_root_bus(vmd->bus);
724 vmd_teardown_dma_ops(vmd);
725 irq_domain_remove(vmd->irq_domain);
726}
727
728#ifdef CONFIG_PM
729static int vmd_suspend(struct device *dev)
730{
731 struct pci_dev *pdev = to_pci_dev(dev);
732
733 pci_save_state(pdev);
734 return 0;
735}
736
737static int vmd_resume(struct device *dev)
738{
739 struct pci_dev *pdev = to_pci_dev(dev);
740
741 pci_restore_state(pdev);
742 return 0;
743}
744#endif
745static SIMPLE_DEV_PM_OPS(vmd_dev_pm_ops, vmd_suspend, vmd_resume);
746
747static const struct pci_device_id vmd_ids[] = {
748 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x201d),},
749 {0,}
750};
751MODULE_DEVICE_TABLE(pci, vmd_ids);
752
753static struct pci_driver vmd_drv = {
754 .name = "vmd",
755 .id_table = vmd_ids,
756 .probe = vmd_probe,
757 .remove = vmd_remove,
758 .driver = {
759 .pm = &vmd_dev_pm_ops,
760 },
761};
762module_pci_driver(vmd_drv);
763
764MODULE_AUTHOR("Intel Corporation");
765MODULE_LICENSE("GPL v2");
766MODULE_VERSION("0.6");