xref: /openbmc/linux/drivers/pci/controller/vmd.c (revision d7955ce4)
1 // 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/pci-acpi.h>
15 #include <linux/pci-ecam.h>
16 #include <linux/srcu.h>
17 #include <linux/rculist.h>
18 #include <linux/rcupdate.h>
19 
20 #include <asm/irqdomain.h>
21 
22 #define VMD_CFGBAR	0
23 #define VMD_MEMBAR1	2
24 #define VMD_MEMBAR2	4
25 
26 #define PCI_REG_VMCAP		0x40
27 #define BUS_RESTRICT_CAP(vmcap)	(vmcap & 0x1)
28 #define PCI_REG_VMCONFIG	0x44
29 #define BUS_RESTRICT_CFG(vmcfg)	((vmcfg >> 8) & 0x3)
30 #define VMCONFIG_MSI_REMAP	0x2
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 
37 enum 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 	 * Device contains physical location shadow registers in
53 	 * vendor-specific capability space
54 	 */
55 	VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP	= (1 << 2),
56 
57 	/*
58 	 * Device may use MSI-X vector 0 for software triggering and will not
59 	 * be used for MSI remapping
60 	 */
61 	VMD_FEAT_OFFSET_FIRST_VECTOR		= (1 << 3),
62 
63 	/*
64 	 * Device can bypass remapping MSI-X transactions into its MSI-X table,
65 	 * avoiding the requirement of a VMD MSI domain for child device
66 	 * interrupt handling.
67 	 */
68 	VMD_FEAT_CAN_BYPASS_MSI_REMAP		= (1 << 4),
69 
70 	/*
71 	 * Enable ASPM on the PCIE root ports and set the default LTR of the
72 	 * storage devices on platforms where these values are not configured by
73 	 * BIOS. This is needed for laptops, which require these settings for
74 	 * proper power management of the SoC.
75 	 */
76 	VMD_FEAT_BIOS_PM_QUIRK		= (1 << 5),
77 };
78 
79 #define VMD_BIOS_PM_QUIRK_LTR	0x1003	/* 3145728 ns */
80 
81 #define VMD_FEATS_CLIENT	(VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP |	\
82 				 VMD_FEAT_HAS_BUS_RESTRICTIONS |	\
83 				 VMD_FEAT_OFFSET_FIRST_VECTOR |		\
84 				 VMD_FEAT_BIOS_PM_QUIRK)
85 
86 static DEFINE_IDA(vmd_instance_ida);
87 
88 /*
89  * Lock for manipulating VMD IRQ lists.
90  */
91 static DEFINE_RAW_SPINLOCK(list_lock);
92 
93 /**
94  * struct vmd_irq - private data to map driver IRQ to the VMD shared vector
95  * @node:	list item for parent traversal.
96  * @irq:	back pointer to parent.
97  * @enabled:	true if driver enabled IRQ
98  * @virq:	the virtual IRQ value provided to the requesting driver.
99  *
100  * Every MSI/MSI-X IRQ requested for a device in a VMD domain will be mapped to
101  * a VMD IRQ using this structure.
102  */
103 struct vmd_irq {
104 	struct list_head	node;
105 	struct vmd_irq_list	*irq;
106 	bool			enabled;
107 	unsigned int		virq;
108 };
109 
110 /**
111  * struct vmd_irq_list - list of driver requested IRQs mapping to a VMD vector
112  * @irq_list:	the list of irq's the VMD one demuxes to.
113  * @srcu:	SRCU struct for local synchronization.
114  * @count:	number of child IRQs assigned to this vector; used to track
115  *		sharing.
116  * @virq:	The underlying VMD Linux interrupt number
117  */
118 struct vmd_irq_list {
119 	struct list_head	irq_list;
120 	struct srcu_struct	srcu;
121 	unsigned int		count;
122 	unsigned int		virq;
123 };
124 
125 struct vmd_dev {
126 	struct pci_dev		*dev;
127 
128 	spinlock_t		cfg_lock;
129 	void __iomem		*cfgbar;
130 
131 	int msix_count;
132 	struct vmd_irq_list	*irqs;
133 
134 	struct pci_sysdata	sysdata;
135 	struct resource		resources[3];
136 	struct irq_domain	*irq_domain;
137 	struct pci_bus		*bus;
138 	u8			busn_start;
139 	u8			first_vec;
140 	char			*name;
141 	int			instance;
142 };
143 
144 static inline struct vmd_dev *vmd_from_bus(struct pci_bus *bus)
145 {
146 	return container_of(bus->sysdata, struct vmd_dev, sysdata);
147 }
148 
149 static inline unsigned int index_from_irqs(struct vmd_dev *vmd,
150 					   struct vmd_irq_list *irqs)
151 {
152 	return irqs - vmd->irqs;
153 }
154 
155 /*
156  * Drivers managing a device in a VMD domain allocate their own IRQs as before,
157  * but the MSI entry for the hardware it's driving will be programmed with a
158  * destination ID for the VMD MSI-X table.  The VMD muxes interrupts in its
159  * domain into one of its own, and the VMD driver de-muxes these for the
160  * handlers sharing that VMD IRQ.  The vmd irq_domain provides the operations
161  * and irq_chip to set this up.
162  */
163 static void vmd_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
164 {
165 	struct vmd_irq *vmdirq = data->chip_data;
166 	struct vmd_irq_list *irq = vmdirq->irq;
167 	struct vmd_dev *vmd = irq_data_get_irq_handler_data(data);
168 
169 	memset(msg, 0, sizeof(*msg));
170 	msg->address_hi = X86_MSI_BASE_ADDRESS_HIGH;
171 	msg->arch_addr_lo.base_address = X86_MSI_BASE_ADDRESS_LOW;
172 	msg->arch_addr_lo.destid_0_7 = index_from_irqs(vmd, irq);
173 }
174 
175 /*
176  * We rely on MSI_FLAG_USE_DEF_CHIP_OPS to set the IRQ mask/unmask ops.
177  */
178 static void vmd_irq_enable(struct irq_data *data)
179 {
180 	struct vmd_irq *vmdirq = data->chip_data;
181 	unsigned long flags;
182 
183 	raw_spin_lock_irqsave(&list_lock, flags);
184 	WARN_ON(vmdirq->enabled);
185 	list_add_tail_rcu(&vmdirq->node, &vmdirq->irq->irq_list);
186 	vmdirq->enabled = true;
187 	raw_spin_unlock_irqrestore(&list_lock, flags);
188 
189 	data->chip->irq_unmask(data);
190 }
191 
192 static void vmd_irq_disable(struct irq_data *data)
193 {
194 	struct vmd_irq *vmdirq = data->chip_data;
195 	unsigned long flags;
196 
197 	data->chip->irq_mask(data);
198 
199 	raw_spin_lock_irqsave(&list_lock, flags);
200 	if (vmdirq->enabled) {
201 		list_del_rcu(&vmdirq->node);
202 		vmdirq->enabled = false;
203 	}
204 	raw_spin_unlock_irqrestore(&list_lock, flags);
205 }
206 
207 /*
208  * XXX: Stubbed until we develop acceptable way to not create conflicts with
209  * other devices sharing the same vector.
210  */
211 static int vmd_irq_set_affinity(struct irq_data *data,
212 				const struct cpumask *dest, bool force)
213 {
214 	return -EINVAL;
215 }
216 
217 static struct irq_chip vmd_msi_controller = {
218 	.name			= "VMD-MSI",
219 	.irq_enable		= vmd_irq_enable,
220 	.irq_disable		= vmd_irq_disable,
221 	.irq_compose_msi_msg	= vmd_compose_msi_msg,
222 	.irq_set_affinity	= vmd_irq_set_affinity,
223 };
224 
225 static irq_hw_number_t vmd_get_hwirq(struct msi_domain_info *info,
226 				     msi_alloc_info_t *arg)
227 {
228 	return 0;
229 }
230 
231 /*
232  * XXX: We can be even smarter selecting the best IRQ once we solve the
233  * affinity problem.
234  */
235 static struct vmd_irq_list *vmd_next_irq(struct vmd_dev *vmd, struct msi_desc *desc)
236 {
237 	unsigned long flags;
238 	int i, best;
239 
240 	if (vmd->msix_count == 1 + vmd->first_vec)
241 		return &vmd->irqs[vmd->first_vec];
242 
243 	/*
244 	 * White list for fast-interrupt handlers. All others will share the
245 	 * "slow" interrupt vector.
246 	 */
247 	switch (msi_desc_to_pci_dev(desc)->class) {
248 	case PCI_CLASS_STORAGE_EXPRESS:
249 		break;
250 	default:
251 		return &vmd->irqs[vmd->first_vec];
252 	}
253 
254 	raw_spin_lock_irqsave(&list_lock, flags);
255 	best = vmd->first_vec + 1;
256 	for (i = best; i < vmd->msix_count; i++)
257 		if (vmd->irqs[i].count < vmd->irqs[best].count)
258 			best = i;
259 	vmd->irqs[best].count++;
260 	raw_spin_unlock_irqrestore(&list_lock, flags);
261 
262 	return &vmd->irqs[best];
263 }
264 
265 static int vmd_msi_init(struct irq_domain *domain, struct msi_domain_info *info,
266 			unsigned int virq, irq_hw_number_t hwirq,
267 			msi_alloc_info_t *arg)
268 {
269 	struct msi_desc *desc = arg->desc;
270 	struct vmd_dev *vmd = vmd_from_bus(msi_desc_to_pci_dev(desc)->bus);
271 	struct vmd_irq *vmdirq = kzalloc(sizeof(*vmdirq), GFP_KERNEL);
272 
273 	if (!vmdirq)
274 		return -ENOMEM;
275 
276 	INIT_LIST_HEAD(&vmdirq->node);
277 	vmdirq->irq = vmd_next_irq(vmd, desc);
278 	vmdirq->virq = virq;
279 
280 	irq_domain_set_info(domain, virq, vmdirq->irq->virq, info->chip, vmdirq,
281 			    handle_untracked_irq, vmd, NULL);
282 	return 0;
283 }
284 
285 static void vmd_msi_free(struct irq_domain *domain,
286 			struct msi_domain_info *info, unsigned int virq)
287 {
288 	struct vmd_irq *vmdirq = irq_get_chip_data(virq);
289 	unsigned long flags;
290 
291 	synchronize_srcu(&vmdirq->irq->srcu);
292 
293 	/* XXX: Potential optimization to rebalance */
294 	raw_spin_lock_irqsave(&list_lock, flags);
295 	vmdirq->irq->count--;
296 	raw_spin_unlock_irqrestore(&list_lock, flags);
297 
298 	kfree(vmdirq);
299 }
300 
301 static int vmd_msi_prepare(struct irq_domain *domain, struct device *dev,
302 			   int nvec, msi_alloc_info_t *arg)
303 {
304 	struct pci_dev *pdev = to_pci_dev(dev);
305 	struct vmd_dev *vmd = vmd_from_bus(pdev->bus);
306 
307 	if (nvec > vmd->msix_count)
308 		return vmd->msix_count;
309 
310 	memset(arg, 0, sizeof(*arg));
311 	return 0;
312 }
313 
314 static void vmd_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
315 {
316 	arg->desc = desc;
317 }
318 
319 static struct msi_domain_ops vmd_msi_domain_ops = {
320 	.get_hwirq	= vmd_get_hwirq,
321 	.msi_init	= vmd_msi_init,
322 	.msi_free	= vmd_msi_free,
323 	.msi_prepare	= vmd_msi_prepare,
324 	.set_desc	= vmd_set_desc,
325 };
326 
327 static struct msi_domain_info vmd_msi_domain_info = {
328 	.flags		= MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
329 			  MSI_FLAG_PCI_MSIX,
330 	.ops		= &vmd_msi_domain_ops,
331 	.chip		= &vmd_msi_controller,
332 };
333 
334 static void vmd_set_msi_remapping(struct vmd_dev *vmd, bool enable)
335 {
336 	u16 reg;
337 
338 	pci_read_config_word(vmd->dev, PCI_REG_VMCONFIG, &reg);
339 	reg = enable ? (reg & ~VMCONFIG_MSI_REMAP) :
340 		       (reg | VMCONFIG_MSI_REMAP);
341 	pci_write_config_word(vmd->dev, PCI_REG_VMCONFIG, reg);
342 }
343 
344 static int vmd_create_irq_domain(struct vmd_dev *vmd)
345 {
346 	struct fwnode_handle *fn;
347 
348 	fn = irq_domain_alloc_named_id_fwnode("VMD-MSI", vmd->sysdata.domain);
349 	if (!fn)
350 		return -ENODEV;
351 
352 	vmd->irq_domain = pci_msi_create_irq_domain(fn, &vmd_msi_domain_info, NULL);
353 	if (!vmd->irq_domain) {
354 		irq_domain_free_fwnode(fn);
355 		return -ENODEV;
356 	}
357 
358 	return 0;
359 }
360 
361 static void vmd_remove_irq_domain(struct vmd_dev *vmd)
362 {
363 	/*
364 	 * Some production BIOS won't enable remapping between soft reboots.
365 	 * Ensure remapping is restored before unloading the driver.
366 	 */
367 	if (!vmd->msix_count)
368 		vmd_set_msi_remapping(vmd, true);
369 
370 	if (vmd->irq_domain) {
371 		struct fwnode_handle *fn = vmd->irq_domain->fwnode;
372 
373 		irq_domain_remove(vmd->irq_domain);
374 		irq_domain_free_fwnode(fn);
375 	}
376 }
377 
378 static void __iomem *vmd_cfg_addr(struct vmd_dev *vmd, struct pci_bus *bus,
379 				  unsigned int devfn, int reg, int len)
380 {
381 	unsigned int busnr_ecam = bus->number - vmd->busn_start;
382 	u32 offset = PCIE_ECAM_OFFSET(busnr_ecam, devfn, reg);
383 
384 	if (offset + len >= resource_size(&vmd->dev->resource[VMD_CFGBAR]))
385 		return NULL;
386 
387 	return vmd->cfgbar + offset;
388 }
389 
390 /*
391  * CPU may deadlock if config space is not serialized on some versions of this
392  * hardware, so all config space access is done under a spinlock.
393  */
394 static int vmd_pci_read(struct pci_bus *bus, unsigned int devfn, int reg,
395 			int len, u32 *value)
396 {
397 	struct vmd_dev *vmd = vmd_from_bus(bus);
398 	void __iomem *addr = vmd_cfg_addr(vmd, bus, devfn, reg, len);
399 	unsigned long flags;
400 	int ret = 0;
401 
402 	if (!addr)
403 		return -EFAULT;
404 
405 	spin_lock_irqsave(&vmd->cfg_lock, flags);
406 	switch (len) {
407 	case 1:
408 		*value = readb(addr);
409 		break;
410 	case 2:
411 		*value = readw(addr);
412 		break;
413 	case 4:
414 		*value = readl(addr);
415 		break;
416 	default:
417 		ret = -EINVAL;
418 		break;
419 	}
420 	spin_unlock_irqrestore(&vmd->cfg_lock, flags);
421 	return ret;
422 }
423 
424 /*
425  * VMD h/w converts non-posted config writes to posted memory writes. The
426  * read-back in this function forces the completion so it returns only after
427  * the config space was written, as expected.
428  */
429 static int vmd_pci_write(struct pci_bus *bus, unsigned int devfn, int reg,
430 			 int len, u32 value)
431 {
432 	struct vmd_dev *vmd = vmd_from_bus(bus);
433 	void __iomem *addr = vmd_cfg_addr(vmd, bus, devfn, reg, len);
434 	unsigned long flags;
435 	int ret = 0;
436 
437 	if (!addr)
438 		return -EFAULT;
439 
440 	spin_lock_irqsave(&vmd->cfg_lock, flags);
441 	switch (len) {
442 	case 1:
443 		writeb(value, addr);
444 		readb(addr);
445 		break;
446 	case 2:
447 		writew(value, addr);
448 		readw(addr);
449 		break;
450 	case 4:
451 		writel(value, addr);
452 		readl(addr);
453 		break;
454 	default:
455 		ret = -EINVAL;
456 		break;
457 	}
458 	spin_unlock_irqrestore(&vmd->cfg_lock, flags);
459 	return ret;
460 }
461 
462 static struct pci_ops vmd_ops = {
463 	.read		= vmd_pci_read,
464 	.write		= vmd_pci_write,
465 };
466 
467 #ifdef CONFIG_ACPI
468 static struct acpi_device *vmd_acpi_find_companion(struct pci_dev *pci_dev)
469 {
470 	struct pci_host_bridge *bridge;
471 	u32 busnr, addr;
472 
473 	if (pci_dev->bus->ops != &vmd_ops)
474 		return NULL;
475 
476 	bridge = pci_find_host_bridge(pci_dev->bus);
477 	busnr = pci_dev->bus->number - bridge->bus->number;
478 	/*
479 	 * The address computation below is only applicable to relative bus
480 	 * numbers below 32.
481 	 */
482 	if (busnr > 31)
483 		return NULL;
484 
485 	addr = (busnr << 24) | ((u32)pci_dev->devfn << 16) | 0x8000FFFFU;
486 
487 	dev_dbg(&pci_dev->dev, "Looking for ACPI companion (address 0x%x)\n",
488 		addr);
489 
490 	return acpi_find_child_device(ACPI_COMPANION(bridge->dev.parent), addr,
491 				      false);
492 }
493 
494 static bool hook_installed;
495 
496 static void vmd_acpi_begin(void)
497 {
498 	if (pci_acpi_set_companion_lookup_hook(vmd_acpi_find_companion))
499 		return;
500 
501 	hook_installed = true;
502 }
503 
504 static void vmd_acpi_end(void)
505 {
506 	if (!hook_installed)
507 		return;
508 
509 	pci_acpi_clear_companion_lookup_hook();
510 	hook_installed = false;
511 }
512 #else
513 static inline void vmd_acpi_begin(void) { }
514 static inline void vmd_acpi_end(void) { }
515 #endif /* CONFIG_ACPI */
516 
517 static void vmd_domain_reset(struct vmd_dev *vmd)
518 {
519 	u16 bus, max_buses = resource_size(&vmd->resources[0]);
520 	u8 dev, functions, fn, hdr_type;
521 	char __iomem *base;
522 
523 	for (bus = 0; bus < max_buses; bus++) {
524 		for (dev = 0; dev < 32; dev++) {
525 			base = vmd->cfgbar + PCIE_ECAM_OFFSET(bus,
526 						PCI_DEVFN(dev, 0), 0);
527 
528 			hdr_type = readb(base + PCI_HEADER_TYPE) &
529 					 PCI_HEADER_TYPE_MASK;
530 
531 			functions = (hdr_type & 0x80) ? 8 : 1;
532 			for (fn = 0; fn < functions; fn++) {
533 				base = vmd->cfgbar + PCIE_ECAM_OFFSET(bus,
534 						PCI_DEVFN(dev, fn), 0);
535 
536 				hdr_type = readb(base + PCI_HEADER_TYPE) &
537 						PCI_HEADER_TYPE_MASK;
538 
539 				if (hdr_type != PCI_HEADER_TYPE_BRIDGE ||
540 				    (readw(base + PCI_CLASS_DEVICE) !=
541 				     PCI_CLASS_BRIDGE_PCI))
542 					continue;
543 
544 				memset_io(base + PCI_IO_BASE, 0,
545 					  PCI_ROM_ADDRESS1 - PCI_IO_BASE);
546 			}
547 		}
548 	}
549 }
550 
551 static void vmd_attach_resources(struct vmd_dev *vmd)
552 {
553 	vmd->dev->resource[VMD_MEMBAR1].child = &vmd->resources[1];
554 	vmd->dev->resource[VMD_MEMBAR2].child = &vmd->resources[2];
555 }
556 
557 static void vmd_detach_resources(struct vmd_dev *vmd)
558 {
559 	vmd->dev->resource[VMD_MEMBAR1].child = NULL;
560 	vmd->dev->resource[VMD_MEMBAR2].child = NULL;
561 }
562 
563 /*
564  * VMD domains start at 0x10000 to not clash with ACPI _SEG domains.
565  * Per ACPI r6.0, sec 6.5.6,  _SEG returns an integer, of which the lower
566  * 16 bits are the PCI Segment Group (domain) number.  Other bits are
567  * currently reserved.
568  */
569 static int vmd_find_free_domain(void)
570 {
571 	int domain = 0xffff;
572 	struct pci_bus *bus = NULL;
573 
574 	while ((bus = pci_find_next_bus(bus)) != NULL)
575 		domain = max_t(int, domain, pci_domain_nr(bus));
576 	return domain + 1;
577 }
578 
579 static int vmd_get_phys_offsets(struct vmd_dev *vmd, bool native_hint,
580 				resource_size_t *offset1,
581 				resource_size_t *offset2)
582 {
583 	struct pci_dev *dev = vmd->dev;
584 	u64 phys1, phys2;
585 
586 	if (native_hint) {
587 		u32 vmlock;
588 		int ret;
589 
590 		ret = pci_read_config_dword(dev, PCI_REG_VMLOCK, &vmlock);
591 		if (ret || PCI_POSSIBLE_ERROR(vmlock))
592 			return -ENODEV;
593 
594 		if (MB2_SHADOW_EN(vmlock)) {
595 			void __iomem *membar2;
596 
597 			membar2 = pci_iomap(dev, VMD_MEMBAR2, 0);
598 			if (!membar2)
599 				return -ENOMEM;
600 			phys1 = readq(membar2 + MB2_SHADOW_OFFSET);
601 			phys2 = readq(membar2 + MB2_SHADOW_OFFSET + 8);
602 			pci_iounmap(dev, membar2);
603 		} else
604 			return 0;
605 	} else {
606 		/* Hypervisor-Emulated Vendor-Specific Capability */
607 		int pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
608 		u32 reg, regu;
609 
610 		pci_read_config_dword(dev, pos + 4, &reg);
611 
612 		/* "SHDW" */
613 		if (pos && reg == 0x53484457) {
614 			pci_read_config_dword(dev, pos + 8, &reg);
615 			pci_read_config_dword(dev, pos + 12, &regu);
616 			phys1 = (u64) regu << 32 | reg;
617 
618 			pci_read_config_dword(dev, pos + 16, &reg);
619 			pci_read_config_dword(dev, pos + 20, &regu);
620 			phys2 = (u64) regu << 32 | reg;
621 		} else
622 			return 0;
623 	}
624 
625 	*offset1 = dev->resource[VMD_MEMBAR1].start -
626 			(phys1 & PCI_BASE_ADDRESS_MEM_MASK);
627 	*offset2 = dev->resource[VMD_MEMBAR2].start -
628 			(phys2 & PCI_BASE_ADDRESS_MEM_MASK);
629 
630 	return 0;
631 }
632 
633 static int vmd_get_bus_number_start(struct vmd_dev *vmd)
634 {
635 	struct pci_dev *dev = vmd->dev;
636 	u16 reg;
637 
638 	pci_read_config_word(dev, PCI_REG_VMCAP, &reg);
639 	if (BUS_RESTRICT_CAP(reg)) {
640 		pci_read_config_word(dev, PCI_REG_VMCONFIG, &reg);
641 
642 		switch (BUS_RESTRICT_CFG(reg)) {
643 		case 0:
644 			vmd->busn_start = 0;
645 			break;
646 		case 1:
647 			vmd->busn_start = 128;
648 			break;
649 		case 2:
650 			vmd->busn_start = 224;
651 			break;
652 		default:
653 			pci_err(dev, "Unknown Bus Offset Setting (%d)\n",
654 				BUS_RESTRICT_CFG(reg));
655 			return -ENODEV;
656 		}
657 	}
658 
659 	return 0;
660 }
661 
662 static irqreturn_t vmd_irq(int irq, void *data)
663 {
664 	struct vmd_irq_list *irqs = data;
665 	struct vmd_irq *vmdirq;
666 	int idx;
667 
668 	idx = srcu_read_lock(&irqs->srcu);
669 	list_for_each_entry_rcu(vmdirq, &irqs->irq_list, node)
670 		generic_handle_irq(vmdirq->virq);
671 	srcu_read_unlock(&irqs->srcu, idx);
672 
673 	return IRQ_HANDLED;
674 }
675 
676 static int vmd_alloc_irqs(struct vmd_dev *vmd)
677 {
678 	struct pci_dev *dev = vmd->dev;
679 	int i, err;
680 
681 	vmd->msix_count = pci_msix_vec_count(dev);
682 	if (vmd->msix_count < 0)
683 		return -ENODEV;
684 
685 	vmd->msix_count = pci_alloc_irq_vectors(dev, vmd->first_vec + 1,
686 						vmd->msix_count, PCI_IRQ_MSIX);
687 	if (vmd->msix_count < 0)
688 		return vmd->msix_count;
689 
690 	vmd->irqs = devm_kcalloc(&dev->dev, vmd->msix_count, sizeof(*vmd->irqs),
691 				 GFP_KERNEL);
692 	if (!vmd->irqs)
693 		return -ENOMEM;
694 
695 	for (i = 0; i < vmd->msix_count; i++) {
696 		err = init_srcu_struct(&vmd->irqs[i].srcu);
697 		if (err)
698 			return err;
699 
700 		INIT_LIST_HEAD(&vmd->irqs[i].irq_list);
701 		vmd->irqs[i].virq = pci_irq_vector(dev, i);
702 		err = devm_request_irq(&dev->dev, vmd->irqs[i].virq,
703 				       vmd_irq, IRQF_NO_THREAD,
704 				       vmd->name, &vmd->irqs[i]);
705 		if (err)
706 			return err;
707 	}
708 
709 	return 0;
710 }
711 
712 /*
713  * Since VMD is an aperture to regular PCIe root ports, only allow it to
714  * control features that the OS is allowed to control on the physical PCI bus.
715  */
716 static void vmd_copy_host_bridge_flags(struct pci_host_bridge *root_bridge,
717 				       struct pci_host_bridge *vmd_bridge)
718 {
719 	vmd_bridge->native_pcie_hotplug = root_bridge->native_pcie_hotplug;
720 	vmd_bridge->native_shpc_hotplug = root_bridge->native_shpc_hotplug;
721 	vmd_bridge->native_aer = root_bridge->native_aer;
722 	vmd_bridge->native_pme = root_bridge->native_pme;
723 	vmd_bridge->native_ltr = root_bridge->native_ltr;
724 	vmd_bridge->native_dpc = root_bridge->native_dpc;
725 }
726 
727 /*
728  * Enable ASPM and LTR settings on devices that aren't configured by BIOS.
729  */
730 static int vmd_pm_enable_quirk(struct pci_dev *pdev, void *userdata)
731 {
732 	unsigned long features = *(unsigned long *)userdata;
733 	u16 ltr = VMD_BIOS_PM_QUIRK_LTR;
734 	u32 ltr_reg;
735 	int pos;
736 
737 	if (!(features & VMD_FEAT_BIOS_PM_QUIRK))
738 		return 0;
739 
740 	pci_enable_link_state(pdev, PCIE_LINK_STATE_ALL);
741 
742 	pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_LTR);
743 	if (!pos)
744 		return 0;
745 
746 	/*
747 	 * Skip if the max snoop LTR is non-zero, indicating BIOS has set it
748 	 * so the LTR quirk is not needed.
749 	 */
750 	pci_read_config_dword(pdev, pos + PCI_LTR_MAX_SNOOP_LAT, &ltr_reg);
751 	if (!!(ltr_reg & (PCI_LTR_VALUE_MASK | PCI_LTR_SCALE_MASK)))
752 		return 0;
753 
754 	/*
755 	 * Set the default values to the maximum required by the platform to
756 	 * allow the deepest power management savings. Write as a DWORD where
757 	 * the lower word is the max snoop latency and the upper word is the
758 	 * max non-snoop latency.
759 	 */
760 	ltr_reg = (ltr << 16) | ltr;
761 	pci_write_config_dword(pdev, pos + PCI_LTR_MAX_SNOOP_LAT, ltr_reg);
762 	pci_info(pdev, "VMD: Default LTR value set by driver\n");
763 
764 	return 0;
765 }
766 
767 static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features)
768 {
769 	struct pci_sysdata *sd = &vmd->sysdata;
770 	struct resource *res;
771 	u32 upper_bits;
772 	unsigned long flags;
773 	LIST_HEAD(resources);
774 	resource_size_t offset[2] = {0};
775 	resource_size_t membar2_offset = 0x2000;
776 	struct pci_bus *child;
777 	struct pci_dev *dev;
778 	int ret;
779 
780 	/*
781 	 * Shadow registers may exist in certain VMD device ids which allow
782 	 * guests to correctly assign host physical addresses to the root ports
783 	 * and child devices. These registers will either return the host value
784 	 * or 0, depending on an enable bit in the VMD device.
785 	 */
786 	if (features & VMD_FEAT_HAS_MEMBAR_SHADOW) {
787 		membar2_offset = MB2_SHADOW_OFFSET + MB2_SHADOW_SIZE;
788 		ret = vmd_get_phys_offsets(vmd, true, &offset[0], &offset[1]);
789 		if (ret)
790 			return ret;
791 	} else if (features & VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP) {
792 		ret = vmd_get_phys_offsets(vmd, false, &offset[0], &offset[1]);
793 		if (ret)
794 			return ret;
795 	}
796 
797 	/*
798 	 * Certain VMD devices may have a root port configuration option which
799 	 * limits the bus range to between 0-127, 128-255, or 224-255
800 	 */
801 	if (features & VMD_FEAT_HAS_BUS_RESTRICTIONS) {
802 		ret = vmd_get_bus_number_start(vmd);
803 		if (ret)
804 			return ret;
805 	}
806 
807 	res = &vmd->dev->resource[VMD_CFGBAR];
808 	vmd->resources[0] = (struct resource) {
809 		.name  = "VMD CFGBAR",
810 		.start = vmd->busn_start,
811 		.end   = vmd->busn_start + (resource_size(res) >> 20) - 1,
812 		.flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED,
813 	};
814 
815 	/*
816 	 * If the window is below 4GB, clear IORESOURCE_MEM_64 so we can
817 	 * put 32-bit resources in the window.
818 	 *
819 	 * There's no hardware reason why a 64-bit window *couldn't*
820 	 * contain a 32-bit resource, but pbus_size_mem() computes the
821 	 * bridge window size assuming a 64-bit window will contain no
822 	 * 32-bit resources.  __pci_assign_resource() enforces that
823 	 * artificial restriction to make sure everything will fit.
824 	 *
825 	 * The only way we could use a 64-bit non-prefetchable MEMBAR is
826 	 * if its address is <4GB so that we can convert it to a 32-bit
827 	 * resource.  To be visible to the host OS, all VMD endpoints must
828 	 * be initially configured by platform BIOS, which includes setting
829 	 * up these resources.  We can assume the device is configured
830 	 * according to the platform needs.
831 	 */
832 	res = &vmd->dev->resource[VMD_MEMBAR1];
833 	upper_bits = upper_32_bits(res->end);
834 	flags = res->flags & ~IORESOURCE_SIZEALIGN;
835 	if (!upper_bits)
836 		flags &= ~IORESOURCE_MEM_64;
837 	vmd->resources[1] = (struct resource) {
838 		.name  = "VMD MEMBAR1",
839 		.start = res->start,
840 		.end   = res->end,
841 		.flags = flags,
842 		.parent = res,
843 	};
844 
845 	res = &vmd->dev->resource[VMD_MEMBAR2];
846 	upper_bits = upper_32_bits(res->end);
847 	flags = res->flags & ~IORESOURCE_SIZEALIGN;
848 	if (!upper_bits)
849 		flags &= ~IORESOURCE_MEM_64;
850 	vmd->resources[2] = (struct resource) {
851 		.name  = "VMD MEMBAR2",
852 		.start = res->start + membar2_offset,
853 		.end   = res->end,
854 		.flags = flags,
855 		.parent = res,
856 	};
857 
858 	sd->vmd_dev = vmd->dev;
859 	sd->domain = vmd_find_free_domain();
860 	if (sd->domain < 0)
861 		return sd->domain;
862 
863 	sd->node = pcibus_to_node(vmd->dev->bus);
864 
865 	/*
866 	 * Currently MSI remapping must be enabled in guest passthrough mode
867 	 * due to some missing interrupt remapping plumbing. This is probably
868 	 * acceptable because the guest is usually CPU-limited and MSI
869 	 * remapping doesn't become a performance bottleneck.
870 	 */
871 	if (!(features & VMD_FEAT_CAN_BYPASS_MSI_REMAP) ||
872 	    offset[0] || offset[1]) {
873 		ret = vmd_alloc_irqs(vmd);
874 		if (ret)
875 			return ret;
876 
877 		vmd_set_msi_remapping(vmd, true);
878 
879 		ret = vmd_create_irq_domain(vmd);
880 		if (ret)
881 			return ret;
882 
883 		/*
884 		 * Override the IRQ domain bus token so the domain can be
885 		 * distinguished from a regular PCI/MSI domain.
886 		 */
887 		irq_domain_update_bus_token(vmd->irq_domain, DOMAIN_BUS_VMD_MSI);
888 	} else {
889 		vmd_set_msi_remapping(vmd, false);
890 	}
891 
892 	pci_add_resource(&resources, &vmd->resources[0]);
893 	pci_add_resource_offset(&resources, &vmd->resources[1], offset[0]);
894 	pci_add_resource_offset(&resources, &vmd->resources[2], offset[1]);
895 
896 	vmd->bus = pci_create_root_bus(&vmd->dev->dev, vmd->busn_start,
897 				       &vmd_ops, sd, &resources);
898 	if (!vmd->bus) {
899 		pci_free_resource_list(&resources);
900 		vmd_remove_irq_domain(vmd);
901 		return -ENODEV;
902 	}
903 
904 	vmd_copy_host_bridge_flags(pci_find_host_bridge(vmd->dev->bus),
905 				   to_pci_host_bridge(vmd->bus->bridge));
906 
907 	vmd_attach_resources(vmd);
908 	if (vmd->irq_domain)
909 		dev_set_msi_domain(&vmd->bus->dev, vmd->irq_domain);
910 	else
911 		dev_set_msi_domain(&vmd->bus->dev,
912 				   dev_get_msi_domain(&vmd->dev->dev));
913 
914 	vmd_acpi_begin();
915 
916 	pci_scan_child_bus(vmd->bus);
917 	vmd_domain_reset(vmd);
918 
919 	/* When Intel VMD is enabled, the OS does not discover the Root Ports
920 	 * owned by Intel VMD within the MMCFG space. pci_reset_bus() applies
921 	 * a reset to the parent of the PCI device supplied as argument. This
922 	 * is why we pass a child device, so the reset can be triggered at
923 	 * the Intel bridge level and propagated to all the children in the
924 	 * hierarchy.
925 	 */
926 	list_for_each_entry(child, &vmd->bus->children, node) {
927 		if (!list_empty(&child->devices)) {
928 			dev = list_first_entry(&child->devices,
929 					       struct pci_dev, bus_list);
930 			ret = pci_reset_bus(dev);
931 			if (ret)
932 				pci_warn(dev, "can't reset device: %d\n", ret);
933 
934 			break;
935 		}
936 	}
937 
938 	pci_assign_unassigned_bus_resources(vmd->bus);
939 
940 	pci_walk_bus(vmd->bus, vmd_pm_enable_quirk, &features);
941 
942 	/*
943 	 * VMD root buses are virtual and don't return true on pci_is_pcie()
944 	 * and will fail pcie_bus_configure_settings() early. It can instead be
945 	 * run on each of the real root ports.
946 	 */
947 	list_for_each_entry(child, &vmd->bus->children, node)
948 		pcie_bus_configure_settings(child);
949 
950 	pci_bus_add_devices(vmd->bus);
951 
952 	vmd_acpi_end();
953 
954 	WARN(sysfs_create_link(&vmd->dev->dev.kobj, &vmd->bus->dev.kobj,
955 			       "domain"), "Can't create symlink to domain\n");
956 	return 0;
957 }
958 
959 static int vmd_probe(struct pci_dev *dev, const struct pci_device_id *id)
960 {
961 	unsigned long features = (unsigned long) id->driver_data;
962 	struct vmd_dev *vmd;
963 	int err;
964 
965 	if (resource_size(&dev->resource[VMD_CFGBAR]) < (1 << 20))
966 		return -ENOMEM;
967 
968 	vmd = devm_kzalloc(&dev->dev, sizeof(*vmd), GFP_KERNEL);
969 	if (!vmd)
970 		return -ENOMEM;
971 
972 	vmd->dev = dev;
973 	vmd->instance = ida_simple_get(&vmd_instance_ida, 0, 0, GFP_KERNEL);
974 	if (vmd->instance < 0)
975 		return vmd->instance;
976 
977 	vmd->name = devm_kasprintf(&dev->dev, GFP_KERNEL, "vmd%d",
978 				   vmd->instance);
979 	if (!vmd->name) {
980 		err = -ENOMEM;
981 		goto out_release_instance;
982 	}
983 
984 	err = pcim_enable_device(dev);
985 	if (err < 0)
986 		goto out_release_instance;
987 
988 	vmd->cfgbar = pcim_iomap(dev, VMD_CFGBAR, 0);
989 	if (!vmd->cfgbar) {
990 		err = -ENOMEM;
991 		goto out_release_instance;
992 	}
993 
994 	pci_set_master(dev);
995 	if (dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(64)) &&
996 	    dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32))) {
997 		err = -ENODEV;
998 		goto out_release_instance;
999 	}
1000 
1001 	if (features & VMD_FEAT_OFFSET_FIRST_VECTOR)
1002 		vmd->first_vec = 1;
1003 
1004 	spin_lock_init(&vmd->cfg_lock);
1005 	pci_set_drvdata(dev, vmd);
1006 	err = vmd_enable_domain(vmd, features);
1007 	if (err)
1008 		goto out_release_instance;
1009 
1010 	dev_info(&vmd->dev->dev, "Bound to PCI domain %04x\n",
1011 		 vmd->sysdata.domain);
1012 	return 0;
1013 
1014  out_release_instance:
1015 	ida_simple_remove(&vmd_instance_ida, vmd->instance);
1016 	return err;
1017 }
1018 
1019 static void vmd_cleanup_srcu(struct vmd_dev *vmd)
1020 {
1021 	int i;
1022 
1023 	for (i = 0; i < vmd->msix_count; i++)
1024 		cleanup_srcu_struct(&vmd->irqs[i].srcu);
1025 }
1026 
1027 static void vmd_remove(struct pci_dev *dev)
1028 {
1029 	struct vmd_dev *vmd = pci_get_drvdata(dev);
1030 
1031 	sysfs_remove_link(&vmd->dev->dev.kobj, "domain");
1032 	pci_stop_root_bus(vmd->bus);
1033 	pci_remove_root_bus(vmd->bus);
1034 	vmd_cleanup_srcu(vmd);
1035 	vmd_detach_resources(vmd);
1036 	vmd_remove_irq_domain(vmd);
1037 	ida_simple_remove(&vmd_instance_ida, vmd->instance);
1038 }
1039 
1040 static void vmd_shutdown(struct pci_dev *dev)
1041 {
1042         struct vmd_dev *vmd = pci_get_drvdata(dev);
1043 
1044         vmd_remove_irq_domain(vmd);
1045 }
1046 
1047 #ifdef CONFIG_PM_SLEEP
1048 static int vmd_suspend(struct device *dev)
1049 {
1050 	struct pci_dev *pdev = to_pci_dev(dev);
1051 	struct vmd_dev *vmd = pci_get_drvdata(pdev);
1052 	int i;
1053 
1054 	for (i = 0; i < vmd->msix_count; i++)
1055 		devm_free_irq(dev, vmd->irqs[i].virq, &vmd->irqs[i]);
1056 
1057 	return 0;
1058 }
1059 
1060 static int vmd_resume(struct device *dev)
1061 {
1062 	struct pci_dev *pdev = to_pci_dev(dev);
1063 	struct vmd_dev *vmd = pci_get_drvdata(pdev);
1064 	int err, i;
1065 
1066        if (vmd->irq_domain)
1067                vmd_set_msi_remapping(vmd, true);
1068        else
1069                vmd_set_msi_remapping(vmd, false);
1070 
1071 	for (i = 0; i < vmd->msix_count; i++) {
1072 		err = devm_request_irq(dev, vmd->irqs[i].virq,
1073 				       vmd_irq, IRQF_NO_THREAD,
1074 				       vmd->name, &vmd->irqs[i]);
1075 		if (err)
1076 			return err;
1077 	}
1078 
1079 	return 0;
1080 }
1081 #endif
1082 static SIMPLE_DEV_PM_OPS(vmd_dev_pm_ops, vmd_suspend, vmd_resume);
1083 
1084 static const struct pci_device_id vmd_ids[] = {
1085 	{PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_VMD_201D),
1086 		.driver_data = VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP,},
1087 	{PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_VMD_28C0),
1088 		.driver_data = VMD_FEAT_HAS_MEMBAR_SHADOW |
1089 				VMD_FEAT_HAS_BUS_RESTRICTIONS |
1090 				VMD_FEAT_CAN_BYPASS_MSI_REMAP,},
1091 	{PCI_VDEVICE(INTEL, 0x467f),
1092 		.driver_data = VMD_FEATS_CLIENT,},
1093 	{PCI_VDEVICE(INTEL, 0x4c3d),
1094 		.driver_data = VMD_FEATS_CLIENT,},
1095 	{PCI_VDEVICE(INTEL, 0xa77f),
1096 		.driver_data = VMD_FEATS_CLIENT,},
1097 	{PCI_VDEVICE(INTEL, 0x7d0b),
1098 		.driver_data = VMD_FEATS_CLIENT,},
1099 	{PCI_VDEVICE(INTEL, 0xad0b),
1100 		.driver_data = VMD_FEATS_CLIENT,},
1101 	{PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_VMD_9A0B),
1102 		.driver_data = VMD_FEATS_CLIENT,},
1103 	{0,}
1104 };
1105 MODULE_DEVICE_TABLE(pci, vmd_ids);
1106 
1107 static struct pci_driver vmd_drv = {
1108 	.name		= "vmd",
1109 	.id_table	= vmd_ids,
1110 	.probe		= vmd_probe,
1111 	.remove		= vmd_remove,
1112 	.shutdown	= vmd_shutdown,
1113 	.driver		= {
1114 		.pm	= &vmd_dev_pm_ops,
1115 	},
1116 };
1117 module_pci_driver(vmd_drv);
1118 
1119 MODULE_AUTHOR("Intel Corporation");
1120 MODULE_LICENSE("GPL v2");
1121 MODULE_VERSION("0.6");
1122