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