blob: ab36e5ca1aca39ddf643f22d777cf009fe7bd633 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Volume Management Device driver
4 * Copyright (c) 2015, Intel Corporation.
5 */
6
7#include <linux/device.h>
8#include <linux/interrupt.h>
9#include <linux/irq.h>
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/msi.h>
13#include <linux/pci.h>
14#include <linux/srcu.h>
15#include <linux/rculist.h>
16#include <linux/rcupdate.h>
17
18#include <asm/irqdomain.h>
19#include <asm/device.h>
20#include <asm/msi.h>
21#include <asm/msidef.h>
22
23#define VMD_CFGBAR 0
24#define VMD_MEMBAR1 2
25#define VMD_MEMBAR2 4
26
27#define PCI_REG_VMCAP 0x40
28#define BUS_RESTRICT_CAP(vmcap) (vmcap & 0x1)
29#define PCI_REG_VMCONFIG 0x44
30#define BUS_RESTRICT_CFG(vmcfg) ((vmcfg >> 8) & 0x3)
31#define PCI_REG_VMLOCK 0x70
32#define MB2_SHADOW_EN(vmlock) (vmlock & 0x2)
33
34#define MB2_SHADOW_OFFSET 0x2000
35#define MB2_SHADOW_SIZE 16
36
37enum vmd_features {
38 /*
39 * Device may contain registers which hint the physical location of the
40 * membars, in order to allow proper address translation during
41 * resource assignment to enable guest virtualization
42 */
43 VMD_FEAT_HAS_MEMBAR_SHADOW = (1 << 0),
44
45 /*
46 * Device may provide root port configuration information which limits
47 * bus numbering
48 */
49 VMD_FEAT_HAS_BUS_RESTRICTIONS = (1 << 1),
50};
51
52/*
53 * Lock for manipulating VMD IRQ lists.
54 */
55static DEFINE_RAW_SPINLOCK(list_lock);
56
57/**
58 * struct vmd_irq - private data to map driver IRQ to the VMD shared vector
59 * @node: list item for parent traversal.
60 * @irq: back pointer to parent.
61 * @enabled: true if driver enabled IRQ
62 * @virq: the virtual IRQ value provided to the requesting driver.
63 *
64 * Every MSI/MSI-X IRQ requested for a device in a VMD domain will be mapped to
65 * a VMD IRQ using this structure.
66 */
67struct vmd_irq {
68 struct list_head node;
69 struct vmd_irq_list *irq;
70 bool enabled;
71 unsigned int virq;
72};
73
74/**
75 * struct vmd_irq_list - list of driver requested IRQs mapping to a VMD vector
76 * @irq_list: the list of irq's the VMD one demuxes to.
77 * @srcu: SRCU struct for local synchronization.
78 * @count: number of child IRQs assigned to this vector; used to track
79 * sharing.
80 */
81struct vmd_irq_list {
82 struct list_head irq_list;
83 struct srcu_struct srcu;
84 unsigned int count;
85};
86
87struct vmd_dev {
88 struct pci_dev *dev;
89
90 spinlock_t cfg_lock;
91 char __iomem *cfgbar;
92
93 int msix_count;
94 struct vmd_irq_list *irqs;
95
96 struct pci_sysdata sysdata;
97 struct resource resources[3];
98 struct irq_domain *irq_domain;
99 struct pci_bus *bus;
100 u8 busn_start;
101
102#ifdef CONFIG_X86_DEV_DMA_OPS
103 struct dma_map_ops dma_ops;
104 struct dma_domain dma_domain;
105#endif
106};
107
108static inline struct vmd_dev *vmd_from_bus(struct pci_bus *bus)
109{
110 return container_of(bus->sysdata, struct vmd_dev, sysdata);
111}
112
113static inline unsigned int index_from_irqs(struct vmd_dev *vmd,
114 struct vmd_irq_list *irqs)
115{
116 return irqs - vmd->irqs;
117}
118
119/*
120 * Drivers managing a device in a VMD domain allocate their own IRQs as before,
121 * but the MSI entry for the hardware it's driving will be programmed with a
122 * destination ID for the VMD MSI-X table. The VMD muxes interrupts in its
123 * domain into one of its own, and the VMD driver de-muxes these for the
124 * handlers sharing that VMD IRQ. The vmd irq_domain provides the operations
125 * and irq_chip to set this up.
126 */
127static void vmd_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
128{
129 struct vmd_irq *vmdirq = data->chip_data;
130 struct vmd_irq_list *irq = vmdirq->irq;
131 struct vmd_dev *vmd = irq_data_get_irq_handler_data(data);
132
133 msg->address_hi = MSI_ADDR_BASE_HI;
134 msg->address_lo = MSI_ADDR_BASE_LO |
135 MSI_ADDR_DEST_ID(index_from_irqs(vmd, irq));
136 msg->data = 0;
137}
138
139/*
140 * We rely on MSI_FLAG_USE_DEF_CHIP_OPS to set the IRQ mask/unmask ops.
141 */
142static void vmd_irq_enable(struct irq_data *data)
143{
144 struct vmd_irq *vmdirq = data->chip_data;
145 unsigned long flags;
146
147 raw_spin_lock_irqsave(&list_lock, flags);
148 WARN_ON(vmdirq->enabled);
149 list_add_tail_rcu(&vmdirq->node, &vmdirq->irq->irq_list);
150 vmdirq->enabled = true;
151 raw_spin_unlock_irqrestore(&list_lock, flags);
152
153 data->chip->irq_unmask(data);
154}
155
156static void vmd_irq_disable(struct irq_data *data)
157{
158 struct vmd_irq *vmdirq = data->chip_data;
159 unsigned long flags;
160
161 data->chip->irq_mask(data);
162
163 raw_spin_lock_irqsave(&list_lock, flags);
164 if (vmdirq->enabled) {
165 list_del_rcu(&vmdirq->node);
166 vmdirq->enabled = false;
167 }
168 raw_spin_unlock_irqrestore(&list_lock, flags);
169}
170
171/*
172 * XXX: Stubbed until we develop acceptable way to not create conflicts with
173 * other devices sharing the same vector.
174 */
175static int vmd_irq_set_affinity(struct irq_data *data,
176 const struct cpumask *dest, bool force)
177{
178 return -EINVAL;
179}
180
181static struct irq_chip vmd_msi_controller = {
182 .name = "VMD-MSI",
183 .irq_enable = vmd_irq_enable,
184 .irq_disable = vmd_irq_disable,
185 .irq_compose_msi_msg = vmd_compose_msi_msg,
186 .irq_set_affinity = vmd_irq_set_affinity,
187};
188
189static irq_hw_number_t vmd_get_hwirq(struct msi_domain_info *info,
190 msi_alloc_info_t *arg)
191{
192 return 0;
193}
194
195/*
196 * XXX: We can be even smarter selecting the best IRQ once we solve the
197 * affinity problem.
198 */
199static struct vmd_irq_list *vmd_next_irq(struct vmd_dev *vmd, struct msi_desc *desc)
200{
201 int i, best = 1;
202 unsigned long flags;
203
204 if (vmd->msix_count == 1)
205 return &vmd->irqs[0];
206
207 /*
208 * White list for fast-interrupt handlers. All others will share the
209 * "slow" interrupt vector.
210 */
211 switch (msi_desc_to_pci_dev(desc)->class) {
212 case PCI_CLASS_STORAGE_EXPRESS:
213 break;
214 default:
215 return &vmd->irqs[0];
216 }
217
218 raw_spin_lock_irqsave(&list_lock, flags);
219 for (i = 1; i < vmd->msix_count; i++)
220 if (vmd->irqs[i].count < vmd->irqs[best].count)
221 best = i;
222 vmd->irqs[best].count++;
223 raw_spin_unlock_irqrestore(&list_lock, flags);
224
225 return &vmd->irqs[best];
226}
227
228static int vmd_msi_init(struct irq_domain *domain, struct msi_domain_info *info,
229 unsigned int virq, irq_hw_number_t hwirq,
230 msi_alloc_info_t *arg)
231{
232 struct msi_desc *desc = arg->desc;
233 struct vmd_dev *vmd = vmd_from_bus(msi_desc_to_pci_dev(desc)->bus);
234 struct vmd_irq *vmdirq = kzalloc(sizeof(*vmdirq), GFP_KERNEL);
235 unsigned int index, vector;
236
237 if (!vmdirq)
238 return -ENOMEM;
239
240 INIT_LIST_HEAD(&vmdirq->node);
241 vmdirq->irq = vmd_next_irq(vmd, desc);
242 vmdirq->virq = virq;
243 index = index_from_irqs(vmd, vmdirq->irq);
244 vector = pci_irq_vector(vmd->dev, index);
245
246 irq_domain_set_info(domain, virq, vector, info->chip, vmdirq,
247 handle_untracked_irq, vmd, NULL);
248 return 0;
249}
250
251static void vmd_msi_free(struct irq_domain *domain,
252 struct msi_domain_info *info, unsigned int virq)
253{
254 struct vmd_irq *vmdirq = irq_get_chip_data(virq);
255 unsigned long flags;
256
257 synchronize_srcu(&vmdirq->irq->srcu);
258
259 /* XXX: Potential optimization to rebalance */
260 raw_spin_lock_irqsave(&list_lock, flags);
261 vmdirq->irq->count--;
262 raw_spin_unlock_irqrestore(&list_lock, flags);
263
264 kfree(vmdirq);
265}
266
267static int vmd_msi_prepare(struct irq_domain *domain, struct device *dev,
268 int nvec, msi_alloc_info_t *arg)
269{
270 struct pci_dev *pdev = to_pci_dev(dev);
271 struct vmd_dev *vmd = vmd_from_bus(pdev->bus);
272
273 if (nvec > vmd->msix_count)
274 return vmd->msix_count;
275
276 memset(arg, 0, sizeof(*arg));
277 return 0;
278}
279
280static void vmd_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
281{
282 arg->desc = desc;
283}
284
285static struct msi_domain_ops vmd_msi_domain_ops = {
286 .get_hwirq = vmd_get_hwirq,
287 .msi_init = vmd_msi_init,
288 .msi_free = vmd_msi_free,
289 .msi_prepare = vmd_msi_prepare,
290 .set_desc = vmd_set_desc,
291};
292
293static struct msi_domain_info vmd_msi_domain_info = {
294 .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
295 MSI_FLAG_PCI_MSIX,
296 .ops = &vmd_msi_domain_ops,
297 .chip = &vmd_msi_controller,
298};
299
300#ifdef CONFIG_X86_DEV_DMA_OPS
301/*
302 * VMD replaces the requester ID with its own. DMA mappings for devices in a
303 * VMD domain need to be mapped for the VMD, not the device requiring
304 * the mapping.
305 */
306static struct device *to_vmd_dev(struct device *dev)
307{
308 struct pci_dev *pdev = to_pci_dev(dev);
309 struct vmd_dev *vmd = vmd_from_bus(pdev->bus);
310
311 return &vmd->dev->dev;
312}
313
314static const struct dma_map_ops *vmd_dma_ops(struct device *dev)
315{
316 return get_dma_ops(to_vmd_dev(dev));
317}
318
319static void *vmd_alloc(struct device *dev, size_t size, dma_addr_t *addr,
320 gfp_t flag, unsigned long attrs)
321{
322 return vmd_dma_ops(dev)->alloc(to_vmd_dev(dev), size, addr, flag,
323 attrs);
324}
325
326static void vmd_free(struct device *dev, size_t size, void *vaddr,
327 dma_addr_t addr, unsigned long attrs)
328{
329 return vmd_dma_ops(dev)->free(to_vmd_dev(dev), size, vaddr, addr,
330 attrs);
331}
332
333static int vmd_mmap(struct device *dev, struct vm_area_struct *vma,
334 void *cpu_addr, dma_addr_t addr, size_t size,
335 unsigned long attrs)
336{
337 return vmd_dma_ops(dev)->mmap(to_vmd_dev(dev), vma, cpu_addr, addr,
338 size, attrs);
339}
340
341static int vmd_get_sgtable(struct device *dev, struct sg_table *sgt,
342 void *cpu_addr, dma_addr_t addr, size_t size,
343 unsigned long attrs)
344{
345 return vmd_dma_ops(dev)->get_sgtable(to_vmd_dev(dev), sgt, cpu_addr,
346 addr, size, attrs);
347}
348
349static dma_addr_t vmd_map_page(struct device *dev, struct page *page,
350 unsigned long offset, size_t size,
351 enum dma_data_direction dir,
352 unsigned long attrs)
353{
354 return vmd_dma_ops(dev)->map_page(to_vmd_dev(dev), page, offset, size,
355 dir, attrs);
356}
357
358static void vmd_unmap_page(struct device *dev, dma_addr_t addr, size_t size,
359 enum dma_data_direction dir, unsigned long attrs)
360{
361 vmd_dma_ops(dev)->unmap_page(to_vmd_dev(dev), addr, size, dir, attrs);
362}
363
364static int vmd_map_sg(struct device *dev, struct scatterlist *sg, int nents,
365 enum dma_data_direction dir, unsigned long attrs)
366{
367 return vmd_dma_ops(dev)->map_sg(to_vmd_dev(dev), sg, nents, dir, attrs);
368}
369
370static void vmd_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
371 enum dma_data_direction dir, unsigned long attrs)
372{
373 vmd_dma_ops(dev)->unmap_sg(to_vmd_dev(dev), sg, nents, dir, attrs);
374}
375
376static void vmd_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
377 size_t size, enum dma_data_direction dir)
378{
379 vmd_dma_ops(dev)->sync_single_for_cpu(to_vmd_dev(dev), addr, size, dir);
380}
381
382static void vmd_sync_single_for_device(struct device *dev, dma_addr_t addr,
383 size_t size, enum dma_data_direction dir)
384{
385 vmd_dma_ops(dev)->sync_single_for_device(to_vmd_dev(dev), addr, size,
386 dir);
387}
388
389static void vmd_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
390 int nents, enum dma_data_direction dir)
391{
392 vmd_dma_ops(dev)->sync_sg_for_cpu(to_vmd_dev(dev), sg, nents, dir);
393}
394
395static void vmd_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
396 int nents, enum dma_data_direction dir)
397{
398 vmd_dma_ops(dev)->sync_sg_for_device(to_vmd_dev(dev), sg, nents, dir);
399}
400
401static int vmd_mapping_error(struct device *dev, dma_addr_t addr)
402{
403 return vmd_dma_ops(dev)->mapping_error(to_vmd_dev(dev), addr);
404}
405
406static int vmd_dma_supported(struct device *dev, u64 mask)
407{
408 return vmd_dma_ops(dev)->dma_supported(to_vmd_dev(dev), mask);
409}
410
411#ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK
412static u64 vmd_get_required_mask(struct device *dev)
413{
414 return vmd_dma_ops(dev)->get_required_mask(to_vmd_dev(dev));
415}
416#endif
417
418static void vmd_teardown_dma_ops(struct vmd_dev *vmd)
419{
420 struct dma_domain *domain = &vmd->dma_domain;
421
422 if (get_dma_ops(&vmd->dev->dev))
423 del_dma_domain(domain);
424}
425
426#define ASSIGN_VMD_DMA_OPS(source, dest, fn) \
427 do { \
428 if (source->fn) \
429 dest->fn = vmd_##fn; \
430 } while (0)
431
432static void vmd_setup_dma_ops(struct vmd_dev *vmd)
433{
434 const struct dma_map_ops *source = get_dma_ops(&vmd->dev->dev);
435 struct dma_map_ops *dest = &vmd->dma_ops;
436 struct dma_domain *domain = &vmd->dma_domain;
437
438 domain->domain_nr = vmd->sysdata.domain;
439 domain->dma_ops = dest;
440
441 if (!source)
442 return;
443 ASSIGN_VMD_DMA_OPS(source, dest, alloc);
444 ASSIGN_VMD_DMA_OPS(source, dest, free);
445 ASSIGN_VMD_DMA_OPS(source, dest, mmap);
446 ASSIGN_VMD_DMA_OPS(source, dest, get_sgtable);
447 ASSIGN_VMD_DMA_OPS(source, dest, map_page);
448 ASSIGN_VMD_DMA_OPS(source, dest, unmap_page);
449 ASSIGN_VMD_DMA_OPS(source, dest, map_sg);
450 ASSIGN_VMD_DMA_OPS(source, dest, unmap_sg);
451 ASSIGN_VMD_DMA_OPS(source, dest, sync_single_for_cpu);
452 ASSIGN_VMD_DMA_OPS(source, dest, sync_single_for_device);
453 ASSIGN_VMD_DMA_OPS(source, dest, sync_sg_for_cpu);
454 ASSIGN_VMD_DMA_OPS(source, dest, sync_sg_for_device);
455 ASSIGN_VMD_DMA_OPS(source, dest, mapping_error);
456 ASSIGN_VMD_DMA_OPS(source, dest, dma_supported);
457#ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK
458 ASSIGN_VMD_DMA_OPS(source, dest, get_required_mask);
459#endif
460 add_dma_domain(domain);
461}
462#undef ASSIGN_VMD_DMA_OPS
463#else
464static void vmd_teardown_dma_ops(struct vmd_dev *vmd) {}
465static void vmd_setup_dma_ops(struct vmd_dev *vmd) {}
466#endif
467
468static char __iomem *vmd_cfg_addr(struct vmd_dev *vmd, struct pci_bus *bus,
469 unsigned int devfn, int reg, int len)
470{
471 char __iomem *addr = vmd->cfgbar +
472 ((bus->number - vmd->busn_start) << 20) +
473 (devfn << 12) + reg;
474
475 if ((addr - vmd->cfgbar) + len >=
476 resource_size(&vmd->dev->resource[VMD_CFGBAR]))
477 return NULL;
478
479 return addr;
480}
481
482/*
483 * CPU may deadlock if config space is not serialized on some versions of this
484 * hardware, so all config space access is done under a spinlock.
485 */
486static int vmd_pci_read(struct pci_bus *bus, unsigned int devfn, int reg,
487 int len, u32 *value)
488{
489 struct vmd_dev *vmd = vmd_from_bus(bus);
490 char __iomem *addr = vmd_cfg_addr(vmd, bus, devfn, reg, len);
491 unsigned long flags;
492 int ret = 0;
493
494 if (!addr)
495 return -EFAULT;
496
497 spin_lock_irqsave(&vmd->cfg_lock, flags);
498 switch (len) {
499 case 1:
500 *value = readb(addr);
501 break;
502 case 2:
503 *value = readw(addr);
504 break;
505 case 4:
506 *value = readl(addr);
507 break;
508 default:
509 ret = -EINVAL;
510 break;
511 }
512 spin_unlock_irqrestore(&vmd->cfg_lock, flags);
513 return ret;
514}
515
516/*
517 * VMD h/w converts non-posted config writes to posted memory writes. The
518 * read-back in this function forces the completion so it returns only after
519 * the config space was written, as expected.
520 */
521static int vmd_pci_write(struct pci_bus *bus, unsigned int devfn, int reg,
522 int len, u32 value)
523{
524 struct vmd_dev *vmd = vmd_from_bus(bus);
525 char __iomem *addr = vmd_cfg_addr(vmd, bus, devfn, reg, len);
526 unsigned long flags;
527 int ret = 0;
528
529 if (!addr)
530 return -EFAULT;
531
532 spin_lock_irqsave(&vmd->cfg_lock, flags);
533 switch (len) {
534 case 1:
535 writeb(value, addr);
536 readb(addr);
537 break;
538 case 2:
539 writew(value, addr);
540 readw(addr);
541 break;
542 case 4:
543 writel(value, addr);
544 readl(addr);
545 break;
546 default:
547 ret = -EINVAL;
548 break;
549 }
550 spin_unlock_irqrestore(&vmd->cfg_lock, flags);
551 return ret;
552}
553
554static struct pci_ops vmd_ops = {
555 .read = vmd_pci_read,
556 .write = vmd_pci_write,
557};
558
559static void vmd_attach_resources(struct vmd_dev *vmd)
560{
561 vmd->dev->resource[VMD_MEMBAR1].child = &vmd->resources[1];
562 vmd->dev->resource[VMD_MEMBAR2].child = &vmd->resources[2];
563}
564
565static void vmd_detach_resources(struct vmd_dev *vmd)
566{
567 vmd->dev->resource[VMD_MEMBAR1].child = NULL;
568 vmd->dev->resource[VMD_MEMBAR2].child = NULL;
569}
570
571/*
572 * VMD domains start at 0x10000 to not clash with ACPI _SEG domains.
573 * Per ACPI r6.0, sec 6.5.6, _SEG returns an integer, of which the lower
574 * 16 bits are the PCI Segment Group (domain) number. Other bits are
575 * currently reserved.
576 */
577static int vmd_find_free_domain(void)
578{
579 int domain = 0xffff;
580 struct pci_bus *bus = NULL;
581
582 while ((bus = pci_find_next_bus(bus)) != NULL)
583 domain = max_t(int, domain, pci_domain_nr(bus));
584 return domain + 1;
585}
586
587static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features)
588{
589 struct pci_sysdata *sd = &vmd->sysdata;
590 struct fwnode_handle *fn;
591 struct resource *res;
592 u32 upper_bits;
593 unsigned long flags;
594 LIST_HEAD(resources);
595 resource_size_t offset[2] = {0};
596 resource_size_t membar2_offset = 0x2000;
597
598 /*
599 * Shadow registers may exist in certain VMD device ids which allow
600 * guests to correctly assign host physical addresses to the root ports
601 * and child devices. These registers will either return the host value
602 * or 0, depending on an enable bit in the VMD device.
603 */
604 if (features & VMD_FEAT_HAS_MEMBAR_SHADOW) {
605 u32 vmlock;
606 int ret;
607
608 membar2_offset = MB2_SHADOW_OFFSET + MB2_SHADOW_SIZE;
609 ret = pci_read_config_dword(vmd->dev, PCI_REG_VMLOCK, &vmlock);
610 if (ret || vmlock == ~0)
611 return -ENODEV;
612
613 if (MB2_SHADOW_EN(vmlock)) {
614 void __iomem *membar2;
615
616 membar2 = pci_iomap(vmd->dev, VMD_MEMBAR2, 0);
617 if (!membar2)
618 return -ENOMEM;
619 offset[0] = vmd->dev->resource[VMD_MEMBAR1].start -
620 readq(membar2 + MB2_SHADOW_OFFSET);
621 offset[1] = vmd->dev->resource[VMD_MEMBAR2].start -
622 readq(membar2 + MB2_SHADOW_OFFSET + 8);
623 pci_iounmap(vmd->dev, membar2);
624 }
625 }
626
627 /*
628 * Certain VMD devices may have a root port configuration option which
629 * limits the bus range to between 0-127 or 128-255
630 */
631 if (features & VMD_FEAT_HAS_BUS_RESTRICTIONS) {
632 u32 vmcap, vmconfig;
633
634 pci_read_config_dword(vmd->dev, PCI_REG_VMCAP, &vmcap);
635 pci_read_config_dword(vmd->dev, PCI_REG_VMCONFIG, &vmconfig);
636 if (BUS_RESTRICT_CAP(vmcap) &&
637 (BUS_RESTRICT_CFG(vmconfig) == 0x1))
638 vmd->busn_start = 128;
639 }
640
641 res = &vmd->dev->resource[VMD_CFGBAR];
642 vmd->resources[0] = (struct resource) {
643 .name = "VMD CFGBAR",
644 .start = vmd->busn_start,
645 .end = vmd->busn_start + (resource_size(res) >> 20) - 1,
646 .flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED,
647 };
648
649 /*
650 * If the window is below 4GB, clear IORESOURCE_MEM_64 so we can
651 * put 32-bit resources in the window.
652 *
653 * There's no hardware reason why a 64-bit window *couldn't*
654 * contain a 32-bit resource, but pbus_size_mem() computes the
655 * bridge window size assuming a 64-bit window will contain no
656 * 32-bit resources. __pci_assign_resource() enforces that
657 * artificial restriction to make sure everything will fit.
658 *
659 * The only way we could use a 64-bit non-prefechable MEMBAR is
660 * if its address is <4GB so that we can convert it to a 32-bit
661 * resource. To be visible to the host OS, all VMD endpoints must
662 * be initially configured by platform BIOS, which includes setting
663 * up these resources. We can assume the device is configured
664 * according to the platform needs.
665 */
666 res = &vmd->dev->resource[VMD_MEMBAR1];
667 upper_bits = upper_32_bits(res->end);
668 flags = res->flags & ~IORESOURCE_SIZEALIGN;
669 if (!upper_bits)
670 flags &= ~IORESOURCE_MEM_64;
671 vmd->resources[1] = (struct resource) {
672 .name = "VMD MEMBAR1",
673 .start = res->start,
674 .end = res->end,
675 .flags = flags,
676 .parent = res,
677 };
678
679 res = &vmd->dev->resource[VMD_MEMBAR2];
680 upper_bits = upper_32_bits(res->end);
681 flags = res->flags & ~IORESOURCE_SIZEALIGN;
682 if (!upper_bits)
683 flags &= ~IORESOURCE_MEM_64;
684 vmd->resources[2] = (struct resource) {
685 .name = "VMD MEMBAR2",
686 .start = res->start + membar2_offset,
687 .end = res->end,
688 .flags = flags,
689 .parent = res,
690 };
691
692 sd->vmd_domain = true;
693 sd->domain = vmd_find_free_domain();
694 if (sd->domain < 0)
695 return sd->domain;
696
697 sd->node = pcibus_to_node(vmd->dev->bus);
698
699 fn = irq_domain_alloc_named_id_fwnode("VMD-MSI", vmd->sysdata.domain);
700 if (!fn)
701 return -ENODEV;
702
703 vmd->irq_domain = pci_msi_create_irq_domain(fn, &vmd_msi_domain_info,
704 x86_vector_domain);
705 irq_domain_free_fwnode(fn);
706 if (!vmd->irq_domain)
707 return -ENODEV;
708
709 pci_add_resource(&resources, &vmd->resources[0]);
710 pci_add_resource_offset(&resources, &vmd->resources[1], offset[0]);
711 pci_add_resource_offset(&resources, &vmd->resources[2], offset[1]);
712
713 vmd->bus = pci_create_root_bus(&vmd->dev->dev, vmd->busn_start,
714 &vmd_ops, sd, &resources);
715 if (!vmd->bus) {
716 pci_free_resource_list(&resources);
717 irq_domain_remove(vmd->irq_domain);
718 return -ENODEV;
719 }
720
721 vmd_attach_resources(vmd);
722 vmd_setup_dma_ops(vmd);
723 dev_set_msi_domain(&vmd->bus->dev, vmd->irq_domain);
724 pci_rescan_bus(vmd->bus);
725
726 WARN(sysfs_create_link(&vmd->dev->dev.kobj, &vmd->bus->dev.kobj,
727 "domain"), "Can't create symlink to domain\n");
728 return 0;
729}
730
731static irqreturn_t vmd_irq(int irq, void *data)
732{
733 struct vmd_irq_list *irqs = data;
734 struct vmd_irq *vmdirq;
735 int idx;
736
737 idx = srcu_read_lock(&irqs->srcu);
738 list_for_each_entry_rcu(vmdirq, &irqs->irq_list, node)
739 generic_handle_irq(vmdirq->virq);
740 srcu_read_unlock(&irqs->srcu, idx);
741
742 return IRQ_HANDLED;
743}
744
745static int vmd_probe(struct pci_dev *dev, const struct pci_device_id *id)
746{
747 struct vmd_dev *vmd;
748 int i, err;
749
750 if (resource_size(&dev->resource[VMD_CFGBAR]) < (1 << 20))
751 return -ENOMEM;
752
753 vmd = devm_kzalloc(&dev->dev, sizeof(*vmd), GFP_KERNEL);
754 if (!vmd)
755 return -ENOMEM;
756
757 vmd->dev = dev;
758 err = pcim_enable_device(dev);
759 if (err < 0)
760 return err;
761
762 vmd->cfgbar = pcim_iomap(dev, VMD_CFGBAR, 0);
763 if (!vmd->cfgbar)
764 return -ENOMEM;
765
766 pci_set_master(dev);
767 if (dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(64)) &&
768 dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32)))
769 return -ENODEV;
770
771 vmd->msix_count = pci_msix_vec_count(dev);
772 if (vmd->msix_count < 0)
773 return -ENODEV;
774
775 vmd->msix_count = pci_alloc_irq_vectors(dev, 1, vmd->msix_count,
776 PCI_IRQ_MSIX);
777 if (vmd->msix_count < 0)
778 return vmd->msix_count;
779
780 vmd->irqs = devm_kcalloc(&dev->dev, vmd->msix_count, sizeof(*vmd->irqs),
781 GFP_KERNEL);
782 if (!vmd->irqs)
783 return -ENOMEM;
784
785 for (i = 0; i < vmd->msix_count; i++) {
786 err = init_srcu_struct(&vmd->irqs[i].srcu);
787 if (err)
788 return err;
789
790 INIT_LIST_HEAD(&vmd->irqs[i].irq_list);
791 err = devm_request_irq(&dev->dev, pci_irq_vector(dev, i),
792 vmd_irq, IRQF_NO_THREAD,
793 "vmd", &vmd->irqs[i]);
794 if (err)
795 return err;
796 }
797
798 spin_lock_init(&vmd->cfg_lock);
799 pci_set_drvdata(dev, vmd);
800 err = vmd_enable_domain(vmd, (unsigned long) id->driver_data);
801 if (err)
802 return err;
803
804 dev_info(&vmd->dev->dev, "Bound to PCI domain %04x\n",
805 vmd->sysdata.domain);
806 return 0;
807}
808
809static void vmd_cleanup_srcu(struct vmd_dev *vmd)
810{
811 int i;
812
813 for (i = 0; i < vmd->msix_count; i++)
814 cleanup_srcu_struct(&vmd->irqs[i].srcu);
815}
816
817static void vmd_remove(struct pci_dev *dev)
818{
819 struct vmd_dev *vmd = pci_get_drvdata(dev);
820
821 sysfs_remove_link(&vmd->dev->dev.kobj, "domain");
822 pci_stop_root_bus(vmd->bus);
823 pci_remove_root_bus(vmd->bus);
824 vmd_cleanup_srcu(vmd);
825 vmd_teardown_dma_ops(vmd);
826 vmd_detach_resources(vmd);
827 irq_domain_remove(vmd->irq_domain);
828}
829
830#ifdef CONFIG_PM_SLEEP
831static int vmd_suspend(struct device *dev)
832{
833 struct pci_dev *pdev = to_pci_dev(dev);
834 struct vmd_dev *vmd = pci_get_drvdata(pdev);
835 int i;
836
837 for (i = 0; i < vmd->msix_count; i++)
838 devm_free_irq(dev, pci_irq_vector(pdev, i), &vmd->irqs[i]);
839
840 pci_save_state(pdev);
841 return 0;
842}
843
844static int vmd_resume(struct device *dev)
845{
846 struct pci_dev *pdev = to_pci_dev(dev);
847 struct vmd_dev *vmd = pci_get_drvdata(pdev);
848 int err, i;
849
850 for (i = 0; i < vmd->msix_count; i++) {
851 err = devm_request_irq(dev, pci_irq_vector(pdev, i),
852 vmd_irq, IRQF_NO_THREAD,
853 "vmd", &vmd->irqs[i]);
854 if (err)
855 return err;
856 }
857
858 pci_restore_state(pdev);
859 return 0;
860}
861#endif
862static SIMPLE_DEV_PM_OPS(vmd_dev_pm_ops, vmd_suspend, vmd_resume);
863
864static const struct pci_device_id vmd_ids[] = {
865 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_VMD_201D),},
866 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_VMD_28C0),
867 .driver_data = VMD_FEAT_HAS_MEMBAR_SHADOW |
868 VMD_FEAT_HAS_BUS_RESTRICTIONS,},
869 {0,}
870};
871MODULE_DEVICE_TABLE(pci, vmd_ids);
872
873static struct pci_driver vmd_drv = {
874 .name = "vmd",
875 .id_table = vmd_ids,
876 .probe = vmd_probe,
877 .remove = vmd_remove,
878 .driver = {
879 .pm = &vmd_dev_pm_ops,
880 },
881};
882module_pci_driver(vmd_drv);
883
884MODULE_AUTHOR("Intel Corporation");
885MODULE_LICENSE("GPL v2");
886MODULE_VERSION("0.6");