blob: e785907acb79112996f877bcdd87e8d68755b7f0 [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.
Keith Busch185a3832016-01-12 13:18:10 -070059 * @count: number of child IRQs assigned to this vector; used to track
60 * sharing.
61 */
62struct vmd_irq_list {
63 struct list_head irq_list;
Keith Busch185a3832016-01-12 13:18:10 -070064 unsigned int count;
65};
66
67struct vmd_dev {
68 struct pci_dev *dev;
69
70 spinlock_t cfg_lock;
71 char __iomem *cfgbar;
72
73 int msix_count;
Keith Busch185a3832016-01-12 13:18:10 -070074 struct vmd_irq_list *irqs;
75
76 struct pci_sysdata sysdata;
77 struct resource resources[3];
78 struct irq_domain *irq_domain;
79 struct pci_bus *bus;
80
81#ifdef CONFIG_X86_DEV_DMA_OPS
82 struct dma_map_ops dma_ops;
83 struct dma_domain dma_domain;
84#endif
85};
86
87static inline struct vmd_dev *vmd_from_bus(struct pci_bus *bus)
88{
89 return container_of(bus->sysdata, struct vmd_dev, sysdata);
90}
91
Jon Derrickb3182222016-09-02 11:53:05 -060092static inline unsigned int index_from_irqs(struct vmd_dev *vmd,
93 struct vmd_irq_list *irqs)
94{
95 return irqs - vmd->irqs;
96}
97
Keith Busch185a3832016-01-12 13:18:10 -070098/*
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;
Jon Derrickb3182222016-09-02 11:53:05 -0600110 struct vmd_dev *vmd = irq_data_get_irq_handler_data(data);
Keith Busch185a3832016-01-12 13:18:10 -0700111
112 msg->address_hi = MSI_ADDR_BASE_HI;
Jon Derrickb3182222016-09-02 11:53:05 -0600113 msg->address_lo = MSI_ADDR_BASE_LO |
114 MSI_ADDR_DEST_ID(index_from_irqs(vmd, irq));
Keith Busch185a3832016-01-12 13:18:10 -0700115 msg->data = 0;
116}
117
118/*
119 * We rely on MSI_FLAG_USE_DEF_CHIP_OPS to set the IRQ mask/unmask ops.
120 */
121static void vmd_irq_enable(struct irq_data *data)
122{
123 struct vmd_irq *vmdirq = data->chip_data;
Jon Derrick3f57ff42016-06-20 09:39:51 -0600124 unsigned long flags;
Keith Busch185a3832016-01-12 13:18:10 -0700125
Jon Derrick3f57ff42016-06-20 09:39:51 -0600126 raw_spin_lock_irqsave(&list_lock, flags);
Keith Busch185a3832016-01-12 13:18:10 -0700127 list_add_tail_rcu(&vmdirq->node, &vmdirq->irq->irq_list);
Jon Derrick3f57ff42016-06-20 09:39:51 -0600128 raw_spin_unlock_irqrestore(&list_lock, flags);
Keith Busch185a3832016-01-12 13:18:10 -0700129
130 data->chip->irq_unmask(data);
131}
132
133static void vmd_irq_disable(struct irq_data *data)
134{
135 struct vmd_irq *vmdirq = data->chip_data;
Jon Derrick3f57ff42016-06-20 09:39:51 -0600136 unsigned long flags;
Keith Busch185a3832016-01-12 13:18:10 -0700137
138 data->chip->irq_mask(data);
139
Jon Derrick3f57ff42016-06-20 09:39:51 -0600140 raw_spin_lock_irqsave(&list_lock, flags);
Keith Busch185a3832016-01-12 13:18:10 -0700141 list_del_rcu(&vmdirq->node);
Keith Busch97e92302016-05-17 11:22:18 -0600142 INIT_LIST_HEAD_RCU(&vmdirq->node);
Jon Derrick3f57ff42016-06-20 09:39:51 -0600143 raw_spin_unlock_irqrestore(&list_lock, flags);
Keith Busch185a3832016-01-12 13:18:10 -0700144}
145
146/*
147 * XXX: Stubbed until we develop acceptable way to not create conflicts with
148 * other devices sharing the same vector.
149 */
150static int vmd_irq_set_affinity(struct irq_data *data,
151 const struct cpumask *dest, bool force)
152{
153 return -EINVAL;
154}
155
156static struct irq_chip vmd_msi_controller = {
157 .name = "VMD-MSI",
158 .irq_enable = vmd_irq_enable,
159 .irq_disable = vmd_irq_disable,
160 .irq_compose_msi_msg = vmd_compose_msi_msg,
161 .irq_set_affinity = vmd_irq_set_affinity,
162};
163
164static irq_hw_number_t vmd_get_hwirq(struct msi_domain_info *info,
165 msi_alloc_info_t *arg)
166{
167 return 0;
168}
169
170/*
171 * XXX: We can be even smarter selecting the best IRQ once we solve the
172 * affinity problem.
173 */
Keith Busch9c205302016-06-20 09:39:53 -0600174static struct vmd_irq_list *vmd_next_irq(struct vmd_dev *vmd, struct msi_desc *desc)
Keith Busch185a3832016-01-12 13:18:10 -0700175{
Keith Busch9c205302016-06-20 09:39:53 -0600176 int i, best = 1;
Jon Derrick3f57ff42016-06-20 09:39:51 -0600177 unsigned long flags;
Keith Busch185a3832016-01-12 13:18:10 -0700178
Keith Busch9c205302016-06-20 09:39:53 -0600179 if (!desc->msi_attrib.is_msix || vmd->msix_count == 1)
180 return &vmd->irqs[0];
181
Jon Derrick3f57ff42016-06-20 09:39:51 -0600182 raw_spin_lock_irqsave(&list_lock, flags);
Keith Busch185a3832016-01-12 13:18:10 -0700183 for (i = 1; i < vmd->msix_count; i++)
184 if (vmd->irqs[i].count < vmd->irqs[best].count)
185 best = i;
186 vmd->irqs[best].count++;
Jon Derrick3f57ff42016-06-20 09:39:51 -0600187 raw_spin_unlock_irqrestore(&list_lock, flags);
Keith Busch185a3832016-01-12 13:18:10 -0700188
189 return &vmd->irqs[best];
190}
191
192static int vmd_msi_init(struct irq_domain *domain, struct msi_domain_info *info,
193 unsigned int virq, irq_hw_number_t hwirq,
194 msi_alloc_info_t *arg)
195{
Keith Busch9c205302016-06-20 09:39:53 -0600196 struct msi_desc *desc = arg->desc;
197 struct vmd_dev *vmd = vmd_from_bus(msi_desc_to_pci_dev(desc)->bus);
Keith Busch185a3832016-01-12 13:18:10 -0700198 struct vmd_irq *vmdirq = kzalloc(sizeof(*vmdirq), GFP_KERNEL);
Jon Derrickb3182222016-09-02 11:53:05 -0600199 unsigned int index, vector;
Keith Busch185a3832016-01-12 13:18:10 -0700200
201 if (!vmdirq)
202 return -ENOMEM;
203
204 INIT_LIST_HEAD(&vmdirq->node);
Keith Busch9c205302016-06-20 09:39:53 -0600205 vmdirq->irq = vmd_next_irq(vmd, desc);
Keith Busch185a3832016-01-12 13:18:10 -0700206 vmdirq->virq = virq;
Jon Derrickb3182222016-09-02 11:53:05 -0600207 index = index_from_irqs(vmd, vmdirq->irq);
208 vector = pci_irq_vector(vmd->dev, index);
Keith Busch185a3832016-01-12 13:18:10 -0700209
Jon Derrickb3182222016-09-02 11:53:05 -0600210 irq_domain_set_info(domain, virq, vector, info->chip, vmdirq,
Jon Derrick53db86a2016-09-02 11:53:04 -0600211 handle_untracked_irq, vmd, NULL);
Keith Busch185a3832016-01-12 13:18:10 -0700212 return 0;
213}
214
215static void vmd_msi_free(struct irq_domain *domain,
216 struct msi_domain_info *info, unsigned int virq)
217{
218 struct vmd_irq *vmdirq = irq_get_chip_data(virq);
Jon Derrick3f57ff42016-06-20 09:39:51 -0600219 unsigned long flags;
Keith Busch185a3832016-01-12 13:18:10 -0700220
221 /* XXX: Potential optimization to rebalance */
Jon Derrick3f57ff42016-06-20 09:39:51 -0600222 raw_spin_lock_irqsave(&list_lock, flags);
Keith Busch185a3832016-01-12 13:18:10 -0700223 vmdirq->irq->count--;
Jon Derrick3f57ff42016-06-20 09:39:51 -0600224 raw_spin_unlock_irqrestore(&list_lock, flags);
Keith Busch185a3832016-01-12 13:18:10 -0700225
226 kfree_rcu(vmdirq, rcu);
227}
228
229static int vmd_msi_prepare(struct irq_domain *domain, struct device *dev,
230 int nvec, msi_alloc_info_t *arg)
231{
232 struct pci_dev *pdev = to_pci_dev(dev);
233 struct vmd_dev *vmd = vmd_from_bus(pdev->bus);
234
235 if (nvec > vmd->msix_count)
236 return vmd->msix_count;
237
238 memset(arg, 0, sizeof(*arg));
239 return 0;
240}
241
242static void vmd_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
243{
244 arg->desc = desc;
245}
246
247static struct msi_domain_ops vmd_msi_domain_ops = {
248 .get_hwirq = vmd_get_hwirq,
249 .msi_init = vmd_msi_init,
250 .msi_free = vmd_msi_free,
251 .msi_prepare = vmd_msi_prepare,
252 .set_desc = vmd_set_desc,
253};
254
255static struct msi_domain_info vmd_msi_domain_info = {
256 .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
257 MSI_FLAG_PCI_MSIX,
258 .ops = &vmd_msi_domain_ops,
259 .chip = &vmd_msi_controller,
260};
261
262#ifdef CONFIG_X86_DEV_DMA_OPS
263/*
264 * VMD replaces the requester ID with its own. DMA mappings for devices in a
265 * VMD domain need to be mapped for the VMD, not the device requiring
266 * the mapping.
267 */
268static struct device *to_vmd_dev(struct device *dev)
269{
270 struct pci_dev *pdev = to_pci_dev(dev);
271 struct vmd_dev *vmd = vmd_from_bus(pdev->bus);
272
273 return &vmd->dev->dev;
274}
275
276static struct dma_map_ops *vmd_dma_ops(struct device *dev)
277{
Keith Buschca8a8fa2016-05-17 11:13:24 -0600278 return get_dma_ops(to_vmd_dev(dev));
Keith Busch185a3832016-01-12 13:18:10 -0700279}
280
281static void *vmd_alloc(struct device *dev, size_t size, dma_addr_t *addr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700282 gfp_t flag, unsigned long attrs)
Keith Busch185a3832016-01-12 13:18:10 -0700283{
284 return vmd_dma_ops(dev)->alloc(to_vmd_dev(dev), size, addr, flag,
285 attrs);
286}
287
288static void vmd_free(struct device *dev, size_t size, void *vaddr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700289 dma_addr_t addr, unsigned long attrs)
Keith Busch185a3832016-01-12 13:18:10 -0700290{
291 return vmd_dma_ops(dev)->free(to_vmd_dev(dev), size, vaddr, addr,
292 attrs);
293}
294
295static int vmd_mmap(struct device *dev, struct vm_area_struct *vma,
296 void *cpu_addr, dma_addr_t addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700297 unsigned long attrs)
Keith Busch185a3832016-01-12 13:18:10 -0700298{
299 return vmd_dma_ops(dev)->mmap(to_vmd_dev(dev), vma, cpu_addr, addr,
300 size, attrs);
301}
302
303static int vmd_get_sgtable(struct device *dev, struct sg_table *sgt,
304 void *cpu_addr, dma_addr_t addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700305 unsigned long attrs)
Keith Busch185a3832016-01-12 13:18:10 -0700306{
307 return vmd_dma_ops(dev)->get_sgtable(to_vmd_dev(dev), sgt, cpu_addr,
308 addr, size, attrs);
309}
310
311static dma_addr_t vmd_map_page(struct device *dev, struct page *page,
312 unsigned long offset, size_t size,
313 enum dma_data_direction dir,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700314 unsigned long attrs)
Keith Busch185a3832016-01-12 13:18:10 -0700315{
316 return vmd_dma_ops(dev)->map_page(to_vmd_dev(dev), page, offset, size,
317 dir, attrs);
318}
319
320static void vmd_unmap_page(struct device *dev, dma_addr_t addr, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700321 enum dma_data_direction dir, unsigned long attrs)
Keith Busch185a3832016-01-12 13:18:10 -0700322{
323 vmd_dma_ops(dev)->unmap_page(to_vmd_dev(dev), addr, size, dir, attrs);
324}
325
326static int vmd_map_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700327 enum dma_data_direction dir, unsigned long attrs)
Keith Busch185a3832016-01-12 13:18:10 -0700328{
329 return vmd_dma_ops(dev)->map_sg(to_vmd_dev(dev), sg, nents, dir, attrs);
330}
331
332static void vmd_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700333 enum dma_data_direction dir, unsigned long attrs)
Keith Busch185a3832016-01-12 13:18:10 -0700334{
335 vmd_dma_ops(dev)->unmap_sg(to_vmd_dev(dev), sg, nents, dir, attrs);
336}
337
338static void vmd_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
339 size_t size, enum dma_data_direction dir)
340{
341 vmd_dma_ops(dev)->sync_single_for_cpu(to_vmd_dev(dev), addr, size, dir);
342}
343
344static void vmd_sync_single_for_device(struct device *dev, dma_addr_t addr,
345 size_t size, enum dma_data_direction dir)
346{
347 vmd_dma_ops(dev)->sync_single_for_device(to_vmd_dev(dev), addr, size,
348 dir);
349}
350
351static void vmd_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
352 int nents, enum dma_data_direction dir)
353{
354 vmd_dma_ops(dev)->sync_sg_for_cpu(to_vmd_dev(dev), sg, nents, dir);
355}
356
357static void vmd_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
358 int nents, enum dma_data_direction dir)
359{
360 vmd_dma_ops(dev)->sync_sg_for_device(to_vmd_dev(dev), sg, nents, dir);
361}
362
363static int vmd_mapping_error(struct device *dev, dma_addr_t addr)
364{
365 return vmd_dma_ops(dev)->mapping_error(to_vmd_dev(dev), addr);
366}
367
368static int vmd_dma_supported(struct device *dev, u64 mask)
369{
370 return vmd_dma_ops(dev)->dma_supported(to_vmd_dev(dev), mask);
371}
372
373#ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK
374static u64 vmd_get_required_mask(struct device *dev)
375{
376 return vmd_dma_ops(dev)->get_required_mask(to_vmd_dev(dev));
377}
378#endif
379
380static void vmd_teardown_dma_ops(struct vmd_dev *vmd)
381{
382 struct dma_domain *domain = &vmd->dma_domain;
383
Keith Buschca8a8fa2016-05-17 11:13:24 -0600384 if (get_dma_ops(&vmd->dev->dev))
Keith Busch185a3832016-01-12 13:18:10 -0700385 del_dma_domain(domain);
386}
387
388#define ASSIGN_VMD_DMA_OPS(source, dest, fn) \
389 do { \
390 if (source->fn) \
391 dest->fn = vmd_##fn; \
392 } while (0)
393
394static void vmd_setup_dma_ops(struct vmd_dev *vmd)
395{
Keith Buschca8a8fa2016-05-17 11:13:24 -0600396 const struct dma_map_ops *source = get_dma_ops(&vmd->dev->dev);
Keith Busch185a3832016-01-12 13:18:10 -0700397 struct dma_map_ops *dest = &vmd->dma_ops;
398 struct dma_domain *domain = &vmd->dma_domain;
399
400 domain->domain_nr = vmd->sysdata.domain;
401 domain->dma_ops = dest;
402
403 if (!source)
404 return;
405 ASSIGN_VMD_DMA_OPS(source, dest, alloc);
406 ASSIGN_VMD_DMA_OPS(source, dest, free);
407 ASSIGN_VMD_DMA_OPS(source, dest, mmap);
408 ASSIGN_VMD_DMA_OPS(source, dest, get_sgtable);
409 ASSIGN_VMD_DMA_OPS(source, dest, map_page);
410 ASSIGN_VMD_DMA_OPS(source, dest, unmap_page);
411 ASSIGN_VMD_DMA_OPS(source, dest, map_sg);
412 ASSIGN_VMD_DMA_OPS(source, dest, unmap_sg);
413 ASSIGN_VMD_DMA_OPS(source, dest, sync_single_for_cpu);
414 ASSIGN_VMD_DMA_OPS(source, dest, sync_single_for_device);
415 ASSIGN_VMD_DMA_OPS(source, dest, sync_sg_for_cpu);
416 ASSIGN_VMD_DMA_OPS(source, dest, sync_sg_for_device);
417 ASSIGN_VMD_DMA_OPS(source, dest, mapping_error);
418 ASSIGN_VMD_DMA_OPS(source, dest, dma_supported);
419#ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK
420 ASSIGN_VMD_DMA_OPS(source, dest, get_required_mask);
421#endif
422 add_dma_domain(domain);
423}
424#undef ASSIGN_VMD_DMA_OPS
425#else
426static void vmd_teardown_dma_ops(struct vmd_dev *vmd) {}
427static void vmd_setup_dma_ops(struct vmd_dev *vmd) {}
428#endif
429
430static char __iomem *vmd_cfg_addr(struct vmd_dev *vmd, struct pci_bus *bus,
431 unsigned int devfn, int reg, int len)
432{
433 char __iomem *addr = vmd->cfgbar +
434 (bus->number << 20) + (devfn << 12) + reg;
435
436 if ((addr - vmd->cfgbar) + len >=
437 resource_size(&vmd->dev->resource[VMD_CFGBAR]))
438 return NULL;
439
440 return addr;
441}
442
443/*
444 * CPU may deadlock if config space is not serialized on some versions of this
445 * hardware, so all config space access is done under a spinlock.
446 */
447static int vmd_pci_read(struct pci_bus *bus, unsigned int devfn, int reg,
448 int len, u32 *value)
449{
450 struct vmd_dev *vmd = vmd_from_bus(bus);
451 char __iomem *addr = vmd_cfg_addr(vmd, bus, devfn, reg, len);
452 unsigned long flags;
453 int ret = 0;
454
455 if (!addr)
456 return -EFAULT;
457
458 spin_lock_irqsave(&vmd->cfg_lock, flags);
459 switch (len) {
460 case 1:
461 *value = readb(addr);
462 break;
463 case 2:
464 *value = readw(addr);
465 break;
466 case 4:
467 *value = readl(addr);
468 break;
469 default:
470 ret = -EINVAL;
471 break;
472 }
473 spin_unlock_irqrestore(&vmd->cfg_lock, flags);
474 return ret;
475}
476
477/*
478 * VMD h/w converts non-posted config writes to posted memory writes. The
479 * read-back in this function forces the completion so it returns only after
480 * the config space was written, as expected.
481 */
482static int vmd_pci_write(struct pci_bus *bus, unsigned int devfn, int reg,
483 int len, u32 value)
484{
485 struct vmd_dev *vmd = vmd_from_bus(bus);
486 char __iomem *addr = vmd_cfg_addr(vmd, bus, devfn, reg, len);
487 unsigned long flags;
488 int ret = 0;
489
490 if (!addr)
491 return -EFAULT;
492
493 spin_lock_irqsave(&vmd->cfg_lock, flags);
494 switch (len) {
495 case 1:
496 writeb(value, addr);
497 readb(addr);
498 break;
499 case 2:
500 writew(value, addr);
501 readw(addr);
502 break;
503 case 4:
504 writel(value, addr);
505 readl(addr);
506 break;
507 default:
508 ret = -EINVAL;
509 break;
510 }
511 spin_unlock_irqrestore(&vmd->cfg_lock, flags);
512 return ret;
513}
514
515static struct pci_ops vmd_ops = {
516 .read = vmd_pci_read,
517 .write = vmd_pci_write,
518};
519
Jon Derrick2c2c5c52016-02-24 10:06:37 -0700520static void vmd_attach_resources(struct vmd_dev *vmd)
521{
522 vmd->dev->resource[VMD_MEMBAR1].child = &vmd->resources[1];
523 vmd->dev->resource[VMD_MEMBAR2].child = &vmd->resources[2];
524}
525
526static void vmd_detach_resources(struct vmd_dev *vmd)
527{
528 vmd->dev->resource[VMD_MEMBAR1].child = NULL;
529 vmd->dev->resource[VMD_MEMBAR2].child = NULL;
530}
531
Keith Busch185a3832016-01-12 13:18:10 -0700532/*
533 * VMD domains start at 0x1000 to not clash with ACPI _SEG domains.
534 */
535static int vmd_find_free_domain(void)
536{
537 int domain = 0xffff;
538 struct pci_bus *bus = NULL;
539
540 while ((bus = pci_find_next_bus(bus)) != NULL)
541 domain = max_t(int, domain, pci_domain_nr(bus));
542 return domain + 1;
543}
544
545static int vmd_enable_domain(struct vmd_dev *vmd)
546{
547 struct pci_sysdata *sd = &vmd->sysdata;
548 struct resource *res;
549 u32 upper_bits;
550 unsigned long flags;
551 LIST_HEAD(resources);
552
553 res = &vmd->dev->resource[VMD_CFGBAR];
554 vmd->resources[0] = (struct resource) {
555 .name = "VMD CFGBAR",
Keith Buschd068c352016-03-02 15:31:04 -0700556 .start = 0,
Keith Busch185a3832016-01-12 13:18:10 -0700557 .end = (resource_size(res) >> 20) - 1,
558 .flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED,
559 };
560
Keith Busch83cc54a2016-03-02 15:31:03 -0700561 /*
562 * If the window is below 4GB, clear IORESOURCE_MEM_64 so we can
563 * put 32-bit resources in the window.
564 *
565 * There's no hardware reason why a 64-bit window *couldn't*
566 * contain a 32-bit resource, but pbus_size_mem() computes the
567 * bridge window size assuming a 64-bit window will contain no
568 * 32-bit resources. __pci_assign_resource() enforces that
569 * artificial restriction to make sure everything will fit.
570 *
571 * The only way we could use a 64-bit non-prefechable MEMBAR is
572 * if its address is <4GB so that we can convert it to a 32-bit
573 * resource. To be visible to the host OS, all VMD endpoints must
574 * be initially configured by platform BIOS, which includes setting
575 * up these resources. We can assume the device is configured
576 * according to the platform needs.
577 */
Keith Busch185a3832016-01-12 13:18:10 -0700578 res = &vmd->dev->resource[VMD_MEMBAR1];
579 upper_bits = upper_32_bits(res->end);
580 flags = res->flags & ~IORESOURCE_SIZEALIGN;
581 if (!upper_bits)
582 flags &= ~IORESOURCE_MEM_64;
583 vmd->resources[1] = (struct resource) {
584 .name = "VMD MEMBAR1",
585 .start = res->start,
586 .end = res->end,
587 .flags = flags,
Jon Derrick2c2c5c52016-02-24 10:06:37 -0700588 .parent = res,
Keith Busch185a3832016-01-12 13:18:10 -0700589 };
590
591 res = &vmd->dev->resource[VMD_MEMBAR2];
592 upper_bits = upper_32_bits(res->end);
593 flags = res->flags & ~IORESOURCE_SIZEALIGN;
594 if (!upper_bits)
595 flags &= ~IORESOURCE_MEM_64;
596 vmd->resources[2] = (struct resource) {
597 .name = "VMD MEMBAR2",
598 .start = res->start + 0x2000,
599 .end = res->end,
600 .flags = flags,
Jon Derrick2c2c5c52016-02-24 10:06:37 -0700601 .parent = res,
Keith Busch185a3832016-01-12 13:18:10 -0700602 };
603
604 sd->domain = vmd_find_free_domain();
605 if (sd->domain < 0)
606 return sd->domain;
607
608 sd->node = pcibus_to_node(vmd->dev->bus);
609
610 vmd->irq_domain = pci_msi_create_irq_domain(NULL, &vmd_msi_domain_info,
Keith Busche382dff2016-06-20 09:39:52 -0600611 x86_vector_domain);
Keith Busch185a3832016-01-12 13:18:10 -0700612 if (!vmd->irq_domain)
613 return -ENODEV;
614
615 pci_add_resource(&resources, &vmd->resources[0]);
616 pci_add_resource(&resources, &vmd->resources[1]);
617 pci_add_resource(&resources, &vmd->resources[2]);
618 vmd->bus = pci_create_root_bus(&vmd->dev->dev, 0, &vmd_ops, sd,
619 &resources);
620 if (!vmd->bus) {
621 pci_free_resource_list(&resources);
622 irq_domain_remove(vmd->irq_domain);
623 return -ENODEV;
624 }
625
Jon Derrick2c2c5c52016-02-24 10:06:37 -0700626 vmd_attach_resources(vmd);
Keith Busch185a3832016-01-12 13:18:10 -0700627 vmd_setup_dma_ops(vmd);
628 dev_set_msi_domain(&vmd->bus->dev, vmd->irq_domain);
629 pci_rescan_bus(vmd->bus);
630
631 WARN(sysfs_create_link(&vmd->dev->dev.kobj, &vmd->bus->dev.kobj,
632 "domain"), "Can't create symlink to domain\n");
633 return 0;
634}
635
636static irqreturn_t vmd_irq(int irq, void *data)
637{
638 struct vmd_irq_list *irqs = data;
639 struct vmd_irq *vmdirq;
640
641 rcu_read_lock();
642 list_for_each_entry_rcu(vmdirq, &irqs->irq_list, node)
643 generic_handle_irq(vmdirq->virq);
644 rcu_read_unlock();
645
646 return IRQ_HANDLED;
647}
648
649static int vmd_probe(struct pci_dev *dev, const struct pci_device_id *id)
650{
651 struct vmd_dev *vmd;
652 int i, err;
653
654 if (resource_size(&dev->resource[VMD_CFGBAR]) < (1 << 20))
655 return -ENOMEM;
656
657 vmd = devm_kzalloc(&dev->dev, sizeof(*vmd), GFP_KERNEL);
658 if (!vmd)
659 return -ENOMEM;
660
661 vmd->dev = dev;
662 err = pcim_enable_device(dev);
663 if (err < 0)
664 return err;
665
666 vmd->cfgbar = pcim_iomap(dev, VMD_CFGBAR, 0);
667 if (!vmd->cfgbar)
668 return -ENOMEM;
669
670 pci_set_master(dev);
671 if (dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(64)) &&
672 dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32)))
673 return -ENODEV;
674
675 vmd->msix_count = pci_msix_vec_count(dev);
676 if (vmd->msix_count < 0)
677 return -ENODEV;
678
Jon Derrick75de9b42016-08-29 11:19:02 -0600679 vmd->msix_count = pci_alloc_irq_vectors(dev, 1, vmd->msix_count,
680 PCI_IRQ_MSIX | PCI_IRQ_AFFINITY);
Keith Busch185a3832016-01-12 13:18:10 -0700681 if (vmd->msix_count < 0)
682 return vmd->msix_count;
683
Jon Derrickc68db512016-08-29 11:19:01 -0600684 vmd->irqs = devm_kcalloc(&dev->dev, vmd->msix_count, sizeof(*vmd->irqs),
685 GFP_KERNEL);
686 if (!vmd->irqs)
687 return -ENOMEM;
688
Keith Busch185a3832016-01-12 13:18:10 -0700689 for (i = 0; i < vmd->msix_count; i++) {
690 INIT_LIST_HEAD(&vmd->irqs[i].irq_list);
Jon Derrick53db86a2016-09-02 11:53:04 -0600691 err = devm_request_irq(&dev->dev, pci_irq_vector(dev, i),
Keith Busch185a3832016-01-12 13:18:10 -0700692 vmd_irq, 0, "vmd", &vmd->irqs[i]);
693 if (err)
694 return err;
695 }
696
697 spin_lock_init(&vmd->cfg_lock);
698 pci_set_drvdata(dev, vmd);
699 err = vmd_enable_domain(vmd);
700 if (err)
701 return err;
702
703 dev_info(&vmd->dev->dev, "Bound to PCI domain %04x\n",
704 vmd->sysdata.domain);
705 return 0;
706}
707
708static void vmd_remove(struct pci_dev *dev)
709{
710 struct vmd_dev *vmd = pci_get_drvdata(dev);
711
Jon Derrick2c2c5c52016-02-24 10:06:37 -0700712 vmd_detach_resources(vmd);
Keith Busch185a3832016-01-12 13:18:10 -0700713 pci_set_drvdata(dev, NULL);
714 sysfs_remove_link(&vmd->dev->dev.kobj, "domain");
715 pci_stop_root_bus(vmd->bus);
716 pci_remove_root_bus(vmd->bus);
717 vmd_teardown_dma_ops(vmd);
718 irq_domain_remove(vmd->irq_domain);
719}
720
721#ifdef CONFIG_PM
722static int vmd_suspend(struct device *dev)
723{
724 struct pci_dev *pdev = to_pci_dev(dev);
725
726 pci_save_state(pdev);
727 return 0;
728}
729
730static int vmd_resume(struct device *dev)
731{
732 struct pci_dev *pdev = to_pci_dev(dev);
733
734 pci_restore_state(pdev);
735 return 0;
736}
737#endif
738static SIMPLE_DEV_PM_OPS(vmd_dev_pm_ops, vmd_suspend, vmd_resume);
739
740static const struct pci_device_id vmd_ids[] = {
741 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x201d),},
742 {0,}
743};
744MODULE_DEVICE_TABLE(pci, vmd_ids);
745
746static struct pci_driver vmd_drv = {
747 .name = "vmd",
748 .id_table = vmd_ids,
749 .probe = vmd_probe,
750 .remove = vmd_remove,
751 .driver = {
752 .pm = &vmd_dev_pm_ops,
753 },
754};
755module_pci_driver(vmd_drv);
756
757MODULE_AUTHOR("Intel Corporation");
758MODULE_LICENSE("GPL v2");
759MODULE_VERSION("0.6");