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