blob: 7aa80dc214453ea14476e947bc1c4677f65aec1f [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 */
172static struct vmd_irq_list *vmd_next_irq(struct vmd_dev *vmd)
173{
174 int i, best = 0;
Jon Derrick3f57ff42016-06-20 09:39:51 -0600175 unsigned long flags;
Keith Busch185a3832016-01-12 13:18:10 -0700176
Jon Derrick3f57ff42016-06-20 09:39:51 -0600177 raw_spin_lock_irqsave(&list_lock, flags);
Keith Busch185a3832016-01-12 13:18:10 -0700178 for (i = 1; i < vmd->msix_count; i++)
179 if (vmd->irqs[i].count < vmd->irqs[best].count)
180 best = i;
181 vmd->irqs[best].count++;
Jon Derrick3f57ff42016-06-20 09:39:51 -0600182 raw_spin_unlock_irqrestore(&list_lock, flags);
Keith Busch185a3832016-01-12 13:18:10 -0700183
184 return &vmd->irqs[best];
185}
186
187static int vmd_msi_init(struct irq_domain *domain, struct msi_domain_info *info,
188 unsigned int virq, irq_hw_number_t hwirq,
189 msi_alloc_info_t *arg)
190{
191 struct vmd_dev *vmd = vmd_from_bus(msi_desc_to_pci_dev(arg->desc)->bus);
192 struct vmd_irq *vmdirq = kzalloc(sizeof(*vmdirq), GFP_KERNEL);
193
194 if (!vmdirq)
195 return -ENOMEM;
196
197 INIT_LIST_HEAD(&vmdirq->node);
198 vmdirq->irq = vmd_next_irq(vmd);
199 vmdirq->virq = virq;
200
201 irq_domain_set_info(domain, virq, vmdirq->irq->vmd_vector, info->chip,
202 vmdirq, handle_simple_irq, vmd, NULL);
203 return 0;
204}
205
206static void vmd_msi_free(struct irq_domain *domain,
207 struct msi_domain_info *info, unsigned int virq)
208{
209 struct vmd_irq *vmdirq = irq_get_chip_data(virq);
Jon Derrick3f57ff42016-06-20 09:39:51 -0600210 unsigned long flags;
Keith Busch185a3832016-01-12 13:18:10 -0700211
212 /* XXX: Potential optimization to rebalance */
Jon Derrick3f57ff42016-06-20 09:39:51 -0600213 raw_spin_lock_irqsave(&list_lock, flags);
Keith Busch185a3832016-01-12 13:18:10 -0700214 vmdirq->irq->count--;
Jon Derrick3f57ff42016-06-20 09:39:51 -0600215 raw_spin_unlock_irqrestore(&list_lock, flags);
Keith Busch185a3832016-01-12 13:18:10 -0700216
217 kfree_rcu(vmdirq, rcu);
218}
219
220static int vmd_msi_prepare(struct irq_domain *domain, struct device *dev,
221 int nvec, msi_alloc_info_t *arg)
222{
223 struct pci_dev *pdev = to_pci_dev(dev);
224 struct vmd_dev *vmd = vmd_from_bus(pdev->bus);
225
226 if (nvec > vmd->msix_count)
227 return vmd->msix_count;
228
229 memset(arg, 0, sizeof(*arg));
230 return 0;
231}
232
233static void vmd_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
234{
235 arg->desc = desc;
236}
237
238static struct msi_domain_ops vmd_msi_domain_ops = {
239 .get_hwirq = vmd_get_hwirq,
240 .msi_init = vmd_msi_init,
241 .msi_free = vmd_msi_free,
242 .msi_prepare = vmd_msi_prepare,
243 .set_desc = vmd_set_desc,
244};
245
246static struct msi_domain_info vmd_msi_domain_info = {
247 .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
248 MSI_FLAG_PCI_MSIX,
249 .ops = &vmd_msi_domain_ops,
250 .chip = &vmd_msi_controller,
251};
252
253#ifdef CONFIG_X86_DEV_DMA_OPS
254/*
255 * VMD replaces the requester ID with its own. DMA mappings for devices in a
256 * VMD domain need to be mapped for the VMD, not the device requiring
257 * the mapping.
258 */
259static struct device *to_vmd_dev(struct device *dev)
260{
261 struct pci_dev *pdev = to_pci_dev(dev);
262 struct vmd_dev *vmd = vmd_from_bus(pdev->bus);
263
264 return &vmd->dev->dev;
265}
266
267static struct dma_map_ops *vmd_dma_ops(struct device *dev)
268{
Keith Buschca8a8fa2016-05-17 11:13:24 -0600269 return get_dma_ops(to_vmd_dev(dev));
Keith Busch185a3832016-01-12 13:18:10 -0700270}
271
272static void *vmd_alloc(struct device *dev, size_t size, dma_addr_t *addr,
273 gfp_t flag, struct dma_attrs *attrs)
274{
275 return vmd_dma_ops(dev)->alloc(to_vmd_dev(dev), size, addr, flag,
276 attrs);
277}
278
279static void vmd_free(struct device *dev, size_t size, void *vaddr,
280 dma_addr_t addr, struct dma_attrs *attrs)
281{
282 return vmd_dma_ops(dev)->free(to_vmd_dev(dev), size, vaddr, addr,
283 attrs);
284}
285
286static int vmd_mmap(struct device *dev, struct vm_area_struct *vma,
287 void *cpu_addr, dma_addr_t addr, size_t size,
288 struct dma_attrs *attrs)
289{
290 return vmd_dma_ops(dev)->mmap(to_vmd_dev(dev), vma, cpu_addr, addr,
291 size, attrs);
292}
293
294static int vmd_get_sgtable(struct device *dev, struct sg_table *sgt,
295 void *cpu_addr, dma_addr_t addr, size_t size,
296 struct dma_attrs *attrs)
297{
298 return vmd_dma_ops(dev)->get_sgtable(to_vmd_dev(dev), sgt, cpu_addr,
299 addr, size, attrs);
300}
301
302static dma_addr_t vmd_map_page(struct device *dev, struct page *page,
303 unsigned long offset, size_t size,
304 enum dma_data_direction dir,
305 struct dma_attrs *attrs)
306{
307 return vmd_dma_ops(dev)->map_page(to_vmd_dev(dev), page, offset, size,
308 dir, attrs);
309}
310
311static void vmd_unmap_page(struct device *dev, dma_addr_t addr, size_t size,
312 enum dma_data_direction dir, struct dma_attrs *attrs)
313{
314 vmd_dma_ops(dev)->unmap_page(to_vmd_dev(dev), addr, size, dir, attrs);
315}
316
317static int vmd_map_sg(struct device *dev, struct scatterlist *sg, int nents,
318 enum dma_data_direction dir, struct dma_attrs *attrs)
319{
320 return vmd_dma_ops(dev)->map_sg(to_vmd_dev(dev), sg, nents, dir, attrs);
321}
322
323static void vmd_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
324 enum dma_data_direction dir, struct dma_attrs *attrs)
325{
326 vmd_dma_ops(dev)->unmap_sg(to_vmd_dev(dev), sg, nents, dir, attrs);
327}
328
329static void vmd_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
330 size_t size, enum dma_data_direction dir)
331{
332 vmd_dma_ops(dev)->sync_single_for_cpu(to_vmd_dev(dev), addr, size, dir);
333}
334
335static void vmd_sync_single_for_device(struct device *dev, dma_addr_t addr,
336 size_t size, enum dma_data_direction dir)
337{
338 vmd_dma_ops(dev)->sync_single_for_device(to_vmd_dev(dev), addr, size,
339 dir);
340}
341
342static void vmd_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
343 int nents, enum dma_data_direction dir)
344{
345 vmd_dma_ops(dev)->sync_sg_for_cpu(to_vmd_dev(dev), sg, nents, dir);
346}
347
348static void vmd_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
349 int nents, enum dma_data_direction dir)
350{
351 vmd_dma_ops(dev)->sync_sg_for_device(to_vmd_dev(dev), sg, nents, dir);
352}
353
354static int vmd_mapping_error(struct device *dev, dma_addr_t addr)
355{
356 return vmd_dma_ops(dev)->mapping_error(to_vmd_dev(dev), addr);
357}
358
359static int vmd_dma_supported(struct device *dev, u64 mask)
360{
361 return vmd_dma_ops(dev)->dma_supported(to_vmd_dev(dev), mask);
362}
363
364#ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK
365static u64 vmd_get_required_mask(struct device *dev)
366{
367 return vmd_dma_ops(dev)->get_required_mask(to_vmd_dev(dev));
368}
369#endif
370
371static void vmd_teardown_dma_ops(struct vmd_dev *vmd)
372{
373 struct dma_domain *domain = &vmd->dma_domain;
374
Keith Buschca8a8fa2016-05-17 11:13:24 -0600375 if (get_dma_ops(&vmd->dev->dev))
Keith Busch185a3832016-01-12 13:18:10 -0700376 del_dma_domain(domain);
377}
378
379#define ASSIGN_VMD_DMA_OPS(source, dest, fn) \
380 do { \
381 if (source->fn) \
382 dest->fn = vmd_##fn; \
383 } while (0)
384
385static void vmd_setup_dma_ops(struct vmd_dev *vmd)
386{
Keith Buschca8a8fa2016-05-17 11:13:24 -0600387 const struct dma_map_ops *source = get_dma_ops(&vmd->dev->dev);
Keith Busch185a3832016-01-12 13:18:10 -0700388 struct dma_map_ops *dest = &vmd->dma_ops;
389 struct dma_domain *domain = &vmd->dma_domain;
390
391 domain->domain_nr = vmd->sysdata.domain;
392 domain->dma_ops = dest;
393
394 if (!source)
395 return;
396 ASSIGN_VMD_DMA_OPS(source, dest, alloc);
397 ASSIGN_VMD_DMA_OPS(source, dest, free);
398 ASSIGN_VMD_DMA_OPS(source, dest, mmap);
399 ASSIGN_VMD_DMA_OPS(source, dest, get_sgtable);
400 ASSIGN_VMD_DMA_OPS(source, dest, map_page);
401 ASSIGN_VMD_DMA_OPS(source, dest, unmap_page);
402 ASSIGN_VMD_DMA_OPS(source, dest, map_sg);
403 ASSIGN_VMD_DMA_OPS(source, dest, unmap_sg);
404 ASSIGN_VMD_DMA_OPS(source, dest, sync_single_for_cpu);
405 ASSIGN_VMD_DMA_OPS(source, dest, sync_single_for_device);
406 ASSIGN_VMD_DMA_OPS(source, dest, sync_sg_for_cpu);
407 ASSIGN_VMD_DMA_OPS(source, dest, sync_sg_for_device);
408 ASSIGN_VMD_DMA_OPS(source, dest, mapping_error);
409 ASSIGN_VMD_DMA_OPS(source, dest, dma_supported);
410#ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK
411 ASSIGN_VMD_DMA_OPS(source, dest, get_required_mask);
412#endif
413 add_dma_domain(domain);
414}
415#undef ASSIGN_VMD_DMA_OPS
416#else
417static void vmd_teardown_dma_ops(struct vmd_dev *vmd) {}
418static void vmd_setup_dma_ops(struct vmd_dev *vmd) {}
419#endif
420
421static char __iomem *vmd_cfg_addr(struct vmd_dev *vmd, struct pci_bus *bus,
422 unsigned int devfn, int reg, int len)
423{
424 char __iomem *addr = vmd->cfgbar +
425 (bus->number << 20) + (devfn << 12) + reg;
426
427 if ((addr - vmd->cfgbar) + len >=
428 resource_size(&vmd->dev->resource[VMD_CFGBAR]))
429 return NULL;
430
431 return addr;
432}
433
434/*
435 * CPU may deadlock if config space is not serialized on some versions of this
436 * hardware, so all config space access is done under a spinlock.
437 */
438static int vmd_pci_read(struct pci_bus *bus, unsigned int devfn, int reg,
439 int len, u32 *value)
440{
441 struct vmd_dev *vmd = vmd_from_bus(bus);
442 char __iomem *addr = vmd_cfg_addr(vmd, bus, devfn, reg, len);
443 unsigned long flags;
444 int ret = 0;
445
446 if (!addr)
447 return -EFAULT;
448
449 spin_lock_irqsave(&vmd->cfg_lock, flags);
450 switch (len) {
451 case 1:
452 *value = readb(addr);
453 break;
454 case 2:
455 *value = readw(addr);
456 break;
457 case 4:
458 *value = readl(addr);
459 break;
460 default:
461 ret = -EINVAL;
462 break;
463 }
464 spin_unlock_irqrestore(&vmd->cfg_lock, flags);
465 return ret;
466}
467
468/*
469 * VMD h/w converts non-posted config writes to posted memory writes. The
470 * read-back in this function forces the completion so it returns only after
471 * the config space was written, as expected.
472 */
473static int vmd_pci_write(struct pci_bus *bus, unsigned int devfn, int reg,
474 int len, u32 value)
475{
476 struct vmd_dev *vmd = vmd_from_bus(bus);
477 char __iomem *addr = vmd_cfg_addr(vmd, bus, devfn, reg, len);
478 unsigned long flags;
479 int ret = 0;
480
481 if (!addr)
482 return -EFAULT;
483
484 spin_lock_irqsave(&vmd->cfg_lock, flags);
485 switch (len) {
486 case 1:
487 writeb(value, addr);
488 readb(addr);
489 break;
490 case 2:
491 writew(value, addr);
492 readw(addr);
493 break;
494 case 4:
495 writel(value, addr);
496 readl(addr);
497 break;
498 default:
499 ret = -EINVAL;
500 break;
501 }
502 spin_unlock_irqrestore(&vmd->cfg_lock, flags);
503 return ret;
504}
505
506static struct pci_ops vmd_ops = {
507 .read = vmd_pci_read,
508 .write = vmd_pci_write,
509};
510
Jon Derrick2c2c5c52016-02-24 10:06:37 -0700511static void vmd_attach_resources(struct vmd_dev *vmd)
512{
513 vmd->dev->resource[VMD_MEMBAR1].child = &vmd->resources[1];
514 vmd->dev->resource[VMD_MEMBAR2].child = &vmd->resources[2];
515}
516
517static void vmd_detach_resources(struct vmd_dev *vmd)
518{
519 vmd->dev->resource[VMD_MEMBAR1].child = NULL;
520 vmd->dev->resource[VMD_MEMBAR2].child = NULL;
521}
522
Keith Busch185a3832016-01-12 13:18:10 -0700523/*
524 * VMD domains start at 0x1000 to not clash with ACPI _SEG domains.
525 */
526static int vmd_find_free_domain(void)
527{
528 int domain = 0xffff;
529 struct pci_bus *bus = NULL;
530
531 while ((bus = pci_find_next_bus(bus)) != NULL)
532 domain = max_t(int, domain, pci_domain_nr(bus));
533 return domain + 1;
534}
535
536static int vmd_enable_domain(struct vmd_dev *vmd)
537{
538 struct pci_sysdata *sd = &vmd->sysdata;
539 struct resource *res;
540 u32 upper_bits;
541 unsigned long flags;
542 LIST_HEAD(resources);
543
544 res = &vmd->dev->resource[VMD_CFGBAR];
545 vmd->resources[0] = (struct resource) {
546 .name = "VMD CFGBAR",
Keith Buschd068c352016-03-02 15:31:04 -0700547 .start = 0,
Keith Busch185a3832016-01-12 13:18:10 -0700548 .end = (resource_size(res) >> 20) - 1,
549 .flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED,
550 };
551
Keith Busch83cc54a2016-03-02 15:31:03 -0700552 /*
553 * If the window is below 4GB, clear IORESOURCE_MEM_64 so we can
554 * put 32-bit resources in the window.
555 *
556 * There's no hardware reason why a 64-bit window *couldn't*
557 * contain a 32-bit resource, but pbus_size_mem() computes the
558 * bridge window size assuming a 64-bit window will contain no
559 * 32-bit resources. __pci_assign_resource() enforces that
560 * artificial restriction to make sure everything will fit.
561 *
562 * The only way we could use a 64-bit non-prefechable MEMBAR is
563 * if its address is <4GB so that we can convert it to a 32-bit
564 * resource. To be visible to the host OS, all VMD endpoints must
565 * be initially configured by platform BIOS, which includes setting
566 * up these resources. We can assume the device is configured
567 * according to the platform needs.
568 */
Keith Busch185a3832016-01-12 13:18:10 -0700569 res = &vmd->dev->resource[VMD_MEMBAR1];
570 upper_bits = upper_32_bits(res->end);
571 flags = res->flags & ~IORESOURCE_SIZEALIGN;
572 if (!upper_bits)
573 flags &= ~IORESOURCE_MEM_64;
574 vmd->resources[1] = (struct resource) {
575 .name = "VMD MEMBAR1",
576 .start = res->start,
577 .end = res->end,
578 .flags = flags,
Jon Derrick2c2c5c52016-02-24 10:06:37 -0700579 .parent = res,
Keith Busch185a3832016-01-12 13:18:10 -0700580 };
581
582 res = &vmd->dev->resource[VMD_MEMBAR2];
583 upper_bits = upper_32_bits(res->end);
584 flags = res->flags & ~IORESOURCE_SIZEALIGN;
585 if (!upper_bits)
586 flags &= ~IORESOURCE_MEM_64;
587 vmd->resources[2] = (struct resource) {
588 .name = "VMD MEMBAR2",
589 .start = res->start + 0x2000,
590 .end = res->end,
591 .flags = flags,
Jon Derrick2c2c5c52016-02-24 10:06:37 -0700592 .parent = res,
Keith Busch185a3832016-01-12 13:18:10 -0700593 };
594
595 sd->domain = vmd_find_free_domain();
596 if (sd->domain < 0)
597 return sd->domain;
598
599 sd->node = pcibus_to_node(vmd->dev->bus);
600
601 vmd->irq_domain = pci_msi_create_irq_domain(NULL, &vmd_msi_domain_info,
602 NULL);
603 if (!vmd->irq_domain)
604 return -ENODEV;
605
606 pci_add_resource(&resources, &vmd->resources[0]);
607 pci_add_resource(&resources, &vmd->resources[1]);
608 pci_add_resource(&resources, &vmd->resources[2]);
609 vmd->bus = pci_create_root_bus(&vmd->dev->dev, 0, &vmd_ops, sd,
610 &resources);
611 if (!vmd->bus) {
612 pci_free_resource_list(&resources);
613 irq_domain_remove(vmd->irq_domain);
614 return -ENODEV;
615 }
616
Jon Derrick2c2c5c52016-02-24 10:06:37 -0700617 vmd_attach_resources(vmd);
Keith Busch185a3832016-01-12 13:18:10 -0700618 vmd_setup_dma_ops(vmd);
619 dev_set_msi_domain(&vmd->bus->dev, vmd->irq_domain);
620 pci_rescan_bus(vmd->bus);
621
622 WARN(sysfs_create_link(&vmd->dev->dev.kobj, &vmd->bus->dev.kobj,
623 "domain"), "Can't create symlink to domain\n");
624 return 0;
625}
626
627static irqreturn_t vmd_irq(int irq, void *data)
628{
629 struct vmd_irq_list *irqs = data;
630 struct vmd_irq *vmdirq;
631
632 rcu_read_lock();
633 list_for_each_entry_rcu(vmdirq, &irqs->irq_list, node)
634 generic_handle_irq(vmdirq->virq);
635 rcu_read_unlock();
636
637 return IRQ_HANDLED;
638}
639
640static int vmd_probe(struct pci_dev *dev, const struct pci_device_id *id)
641{
642 struct vmd_dev *vmd;
643 int i, err;
644
645 if (resource_size(&dev->resource[VMD_CFGBAR]) < (1 << 20))
646 return -ENOMEM;
647
648 vmd = devm_kzalloc(&dev->dev, sizeof(*vmd), GFP_KERNEL);
649 if (!vmd)
650 return -ENOMEM;
651
652 vmd->dev = dev;
653 err = pcim_enable_device(dev);
654 if (err < 0)
655 return err;
656
657 vmd->cfgbar = pcim_iomap(dev, VMD_CFGBAR, 0);
658 if (!vmd->cfgbar)
659 return -ENOMEM;
660
661 pci_set_master(dev);
662 if (dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(64)) &&
663 dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32)))
664 return -ENODEV;
665
666 vmd->msix_count = pci_msix_vec_count(dev);
667 if (vmd->msix_count < 0)
668 return -ENODEV;
669
670 vmd->irqs = devm_kcalloc(&dev->dev, vmd->msix_count, sizeof(*vmd->irqs),
671 GFP_KERNEL);
672 if (!vmd->irqs)
673 return -ENOMEM;
674
675 vmd->msix_entries = devm_kcalloc(&dev->dev, vmd->msix_count,
676 sizeof(*vmd->msix_entries),
677 GFP_KERNEL);
678 if (!vmd->msix_entries)
679 return -ENOMEM;
680 for (i = 0; i < vmd->msix_count; i++)
681 vmd->msix_entries[i].entry = i;
682
683 vmd->msix_count = pci_enable_msix_range(vmd->dev, vmd->msix_entries, 1,
684 vmd->msix_count);
685 if (vmd->msix_count < 0)
686 return vmd->msix_count;
687
688 for (i = 0; i < vmd->msix_count; i++) {
689 INIT_LIST_HEAD(&vmd->irqs[i].irq_list);
690 vmd->irqs[i].vmd_vector = vmd->msix_entries[i].vector;
691 vmd->irqs[i].index = i;
692
693 err = devm_request_irq(&dev->dev, vmd->irqs[i].vmd_vector,
694 vmd_irq, 0, "vmd", &vmd->irqs[i]);
695 if (err)
696 return err;
697 }
698
699 spin_lock_init(&vmd->cfg_lock);
700 pci_set_drvdata(dev, vmd);
701 err = vmd_enable_domain(vmd);
702 if (err)
703 return err;
704
705 dev_info(&vmd->dev->dev, "Bound to PCI domain %04x\n",
706 vmd->sysdata.domain);
707 return 0;
708}
709
710static void vmd_remove(struct pci_dev *dev)
711{
712 struct vmd_dev *vmd = pci_get_drvdata(dev);
713
Jon Derrick2c2c5c52016-02-24 10:06:37 -0700714 vmd_detach_resources(vmd);
Keith Busch185a3832016-01-12 13:18:10 -0700715 pci_set_drvdata(dev, NULL);
716 sysfs_remove_link(&vmd->dev->dev.kobj, "domain");
717 pci_stop_root_bus(vmd->bus);
718 pci_remove_root_bus(vmd->bus);
719 vmd_teardown_dma_ops(vmd);
720 irq_domain_remove(vmd->irq_domain);
721}
722
723#ifdef CONFIG_PM
724static int vmd_suspend(struct device *dev)
725{
726 struct pci_dev *pdev = to_pci_dev(dev);
727
728 pci_save_state(pdev);
729 return 0;
730}
731
732static int vmd_resume(struct device *dev)
733{
734 struct pci_dev *pdev = to_pci_dev(dev);
735
736 pci_restore_state(pdev);
737 return 0;
738}
739#endif
740static SIMPLE_DEV_PM_OPS(vmd_dev_pm_ops, vmd_suspend, vmd_resume);
741
742static const struct pci_device_id vmd_ids[] = {
743 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x201d),},
744 {0,}
745};
746MODULE_DEVICE_TABLE(pci, vmd_ids);
747
748static struct pci_driver vmd_drv = {
749 .name = "vmd",
750 .id_table = vmd_ids,
751 .probe = vmd_probe,
752 .remove = vmd_remove,
753 .driver = {
754 .pm = &vmd_dev_pm_ops,
755 },
756};
757module_pci_driver(vmd_drv);
758
759MODULE_AUTHOR("Intel Corporation");
760MODULE_LICENSE("GPL v2");
761MODULE_VERSION("0.6");