xref: /linux/drivers/iommu/intel/irq_remapping.c (revision f49040c7aaa5532a1f94355ef5073c49e6b32349)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #define pr_fmt(fmt)     "DMAR-IR: " fmt
4 
5 #include <linux/interrupt.h>
6 #include <linux/dmar.h>
7 #include <linux/spinlock.h>
8 #include <linux/slab.h>
9 #include <linux/jiffies.h>
10 #include <linux/hpet.h>
11 #include <linux/pci.h>
12 #include <linux/irq.h>
13 #include <linux/acpi.h>
14 #include <linux/irqdomain.h>
15 #include <linux/crash_dump.h>
16 #include <asm/io_apic.h>
17 #include <asm/apic.h>
18 #include <asm/smp.h>
19 #include <asm/cpu.h>
20 #include <asm/irq_remapping.h>
21 #include <asm/pci-direct.h>
22 #include <asm/posted_intr.h>
23 
24 #include "iommu.h"
25 #include "../irq_remapping.h"
26 #include "../iommu-pages.h"
27 
28 struct ioapic_scope {
29 	struct intel_iommu *iommu;
30 	unsigned int id;
31 	unsigned int bus;	/* PCI bus number */
32 	unsigned int devfn;	/* PCI devfn number */
33 };
34 
35 struct hpet_scope {
36 	struct intel_iommu *iommu;
37 	u8 id;
38 	unsigned int bus;
39 	unsigned int devfn;
40 };
41 
42 struct irq_2_iommu {
43 	struct intel_iommu *iommu;
44 	u16 irte_index;
45 	u16 sub_handle;
46 	u8  irte_mask;
47 	bool posted_msi;
48 	bool posted_vcpu;
49 };
50 
51 struct intel_ir_data {
52 	struct irq_2_iommu			irq_2_iommu;
53 	struct irte				irte_entry;
54 	union {
55 		struct msi_msg			msi_entry;
56 	};
57 };
58 
59 #define IR_X2APIC_MODE(mode) (mode ? (1 << 11) : 0)
60 #define IRTE_DEST(dest) ((eim_mode) ? dest : dest << 8)
61 
62 static int __read_mostly eim_mode;
63 static struct ioapic_scope ir_ioapic[MAX_IO_APICS];
64 static struct hpet_scope ir_hpet[MAX_HPET_TBS];
65 
66 /*
67  * Lock ordering:
68  * ->dmar_global_lock
69  *	->irq_2_ir_lock
70  *		->qi->q_lock
71  *	->iommu->register_lock
72  * Note:
73  * intel_irq_remap_ops.{supported,prepare,enable,disable,reenable} are called
74  * in single-threaded environment with interrupt disabled, so no need to tabke
75  * the dmar_global_lock.
76  */
77 DEFINE_RAW_SPINLOCK(irq_2_ir_lock);
78 static const struct irq_domain_ops intel_ir_domain_ops;
79 
80 static void iommu_disable_irq_remapping(struct intel_iommu *iommu);
81 static int __init parse_ioapics_under_ir(void);
82 static const struct msi_parent_ops dmar_msi_parent_ops;
83 
ir_pre_enabled(struct intel_iommu * iommu)84 static bool ir_pre_enabled(struct intel_iommu *iommu)
85 {
86 	return (iommu->flags & VTD_FLAG_IRQ_REMAP_PRE_ENABLED);
87 }
88 
clear_ir_pre_enabled(struct intel_iommu * iommu)89 static void clear_ir_pre_enabled(struct intel_iommu *iommu)
90 {
91 	iommu->flags &= ~VTD_FLAG_IRQ_REMAP_PRE_ENABLED;
92 }
93 
init_ir_status(struct intel_iommu * iommu)94 static void init_ir_status(struct intel_iommu *iommu)
95 {
96 	u32 gsts;
97 
98 	gsts = readl(iommu->reg + DMAR_GSTS_REG);
99 	if (gsts & DMA_GSTS_IRES)
100 		iommu->flags |= VTD_FLAG_IRQ_REMAP_PRE_ENABLED;
101 }
102 
alloc_irte(struct intel_iommu * iommu,struct irq_2_iommu * irq_iommu,u16 count)103 static int alloc_irte(struct intel_iommu *iommu,
104 		      struct irq_2_iommu *irq_iommu, u16 count)
105 {
106 	struct ir_table *table = iommu->ir_table;
107 	unsigned int mask = 0;
108 	unsigned long flags;
109 	int index;
110 
111 	if (!count || !irq_iommu)
112 		return -1;
113 
114 	if (count > 1) {
115 		count = __roundup_pow_of_two(count);
116 		mask = ilog2(count);
117 	}
118 
119 	if (mask > ecap_max_handle_mask(iommu->ecap)) {
120 		pr_err("Requested mask %x exceeds the max invalidation handle"
121 		       " mask value %Lx\n", mask,
122 		       ecap_max_handle_mask(iommu->ecap));
123 		return -1;
124 	}
125 
126 	raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
127 	index = bitmap_find_free_region(table->bitmap,
128 					INTR_REMAP_TABLE_ENTRIES, mask);
129 	if (index < 0) {
130 		pr_warn("IR%d: can't allocate an IRTE\n", iommu->seq_id);
131 	} else {
132 		irq_iommu->iommu = iommu;
133 		irq_iommu->irte_index =  index;
134 		irq_iommu->sub_handle = 0;
135 		irq_iommu->irte_mask = mask;
136 	}
137 	raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
138 
139 	return index;
140 }
141 
qi_flush_iec(struct intel_iommu * iommu,int index,int mask)142 static int qi_flush_iec(struct intel_iommu *iommu, int index, int mask)
143 {
144 	struct qi_desc desc;
145 
146 	desc.qw0 = QI_IEC_IIDEX(index) | QI_IEC_TYPE | QI_IEC_IM(mask)
147 		   | QI_IEC_SELECTIVE;
148 	desc.qw1 = 0;
149 	desc.qw2 = 0;
150 	desc.qw3 = 0;
151 
152 	return qi_submit_sync(iommu, &desc, 1, 0);
153 }
154 
modify_irte(struct irq_2_iommu * irq_iommu,struct irte * irte_modified)155 static int modify_irte(struct irq_2_iommu *irq_iommu,
156 		       struct irte *irte_modified)
157 {
158 	struct intel_iommu *iommu;
159 	unsigned long flags;
160 	struct irte *irte;
161 	int rc, index;
162 
163 	if (!irq_iommu)
164 		return -1;
165 
166 	raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
167 
168 	iommu = irq_iommu->iommu;
169 
170 	index = irq_iommu->irte_index + irq_iommu->sub_handle;
171 	irte = &iommu->ir_table->base[index];
172 
173 	if ((irte->pst == 1) || (irte_modified->pst == 1)) {
174 		/*
175 		 * We use cmpxchg16 to atomically update the 128-bit IRTE,
176 		 * and it cannot be updated by the hardware or other processors
177 		 * behind us, so the return value of cmpxchg16 should be the
178 		 * same as the old value.
179 		 */
180 		u128 old = irte->irte;
181 		WARN_ON(!try_cmpxchg128(&irte->irte, &old, irte_modified->irte));
182 	} else {
183 		WRITE_ONCE(irte->low, irte_modified->low);
184 		WRITE_ONCE(irte->high, irte_modified->high);
185 	}
186 	__iommu_flush_cache(iommu, irte, sizeof(*irte));
187 
188 	rc = qi_flush_iec(iommu, index, 0);
189 
190 	raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
191 
192 	return rc;
193 }
194 
map_hpet_to_iommu(u8 hpet_id)195 static struct intel_iommu *map_hpet_to_iommu(u8 hpet_id)
196 {
197 	int i;
198 
199 	for (i = 0; i < MAX_HPET_TBS; i++) {
200 		if (ir_hpet[i].id == hpet_id && ir_hpet[i].iommu)
201 			return ir_hpet[i].iommu;
202 	}
203 	return NULL;
204 }
205 
map_ioapic_to_iommu(int apic)206 static struct intel_iommu *map_ioapic_to_iommu(int apic)
207 {
208 	int i;
209 
210 	for (i = 0; i < MAX_IO_APICS; i++) {
211 		if (ir_ioapic[i].id == apic && ir_ioapic[i].iommu)
212 			return ir_ioapic[i].iommu;
213 	}
214 	return NULL;
215 }
216 
map_dev_to_ir(struct pci_dev * dev)217 static struct irq_domain *map_dev_to_ir(struct pci_dev *dev)
218 {
219 	struct dmar_drhd_unit *drhd = dmar_find_matched_drhd_unit(dev);
220 
221 	return drhd ? drhd->iommu->ir_domain : NULL;
222 }
223 
clear_entries(struct irq_2_iommu * irq_iommu)224 static int clear_entries(struct irq_2_iommu *irq_iommu)
225 {
226 	struct irte *start, *entry, *end;
227 	struct intel_iommu *iommu;
228 	int index;
229 
230 	if (irq_iommu->sub_handle)
231 		return 0;
232 
233 	iommu = irq_iommu->iommu;
234 	index = irq_iommu->irte_index;
235 
236 	start = iommu->ir_table->base + index;
237 	end = start + (1 << irq_iommu->irte_mask);
238 
239 	for (entry = start; entry < end; entry++) {
240 		WRITE_ONCE(entry->low, 0);
241 		WRITE_ONCE(entry->high, 0);
242 	}
243 	bitmap_release_region(iommu->ir_table->bitmap, index,
244 			      irq_iommu->irte_mask);
245 
246 	return qi_flush_iec(iommu, index, irq_iommu->irte_mask);
247 }
248 
249 /*
250  * source validation type
251  */
252 #define SVT_NO_VERIFY		0x0  /* no verification is required */
253 #define SVT_VERIFY_SID_SQ	0x1  /* verify using SID and SQ fields */
254 #define SVT_VERIFY_BUS		0x2  /* verify bus of request-id */
255 
256 /*
257  * source-id qualifier
258  */
259 #define SQ_ALL_16	0x0  /* verify all 16 bits of request-id */
260 #define SQ_13_IGNORE_1	0x1  /* verify most significant 13 bits, ignore
261 			      * the third least significant bit
262 			      */
263 #define SQ_13_IGNORE_2	0x2  /* verify most significant 13 bits, ignore
264 			      * the second and third least significant bits
265 			      */
266 #define SQ_13_IGNORE_3	0x3  /* verify most significant 13 bits, ignore
267 			      * the least three significant bits
268 			      */
269 
270 /*
271  * set SVT, SQ and SID fields of irte to verify
272  * source ids of interrupt requests
273  */
set_irte_sid(struct irte * irte,unsigned int svt,unsigned int sq,unsigned int sid)274 static void set_irte_sid(struct irte *irte, unsigned int svt,
275 			 unsigned int sq, unsigned int sid)
276 {
277 	if (disable_sourceid_checking)
278 		svt = SVT_NO_VERIFY;
279 	irte->svt = svt;
280 	irte->sq = sq;
281 	irte->sid = sid;
282 }
283 
284 /*
285  * Set an IRTE to match only the bus number. Interrupt requests that reference
286  * this IRTE must have a requester-id whose bus number is between or equal
287  * to the start_bus and end_bus arguments.
288  */
set_irte_verify_bus(struct irte * irte,unsigned int start_bus,unsigned int end_bus)289 static void set_irte_verify_bus(struct irte *irte, unsigned int start_bus,
290 				unsigned int end_bus)
291 {
292 	set_irte_sid(irte, SVT_VERIFY_BUS, SQ_ALL_16,
293 		     (start_bus << 8) | end_bus);
294 }
295 
set_ioapic_sid(struct irte * irte,int apic)296 static int set_ioapic_sid(struct irte *irte, int apic)
297 {
298 	int i;
299 	u16 sid = 0;
300 
301 	if (!irte)
302 		return -1;
303 
304 	for (i = 0; i < MAX_IO_APICS; i++) {
305 		if (ir_ioapic[i].iommu && ir_ioapic[i].id == apic) {
306 			sid = PCI_DEVID(ir_ioapic[i].bus, ir_ioapic[i].devfn);
307 			break;
308 		}
309 	}
310 
311 	if (sid == 0) {
312 		pr_warn("Failed to set source-id of IOAPIC (%d)\n", apic);
313 		return -1;
314 	}
315 
316 	set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, sid);
317 
318 	return 0;
319 }
320 
set_hpet_sid(struct irte * irte,u8 id)321 static int set_hpet_sid(struct irte *irte, u8 id)
322 {
323 	int i;
324 	u16 sid = 0;
325 
326 	if (!irte)
327 		return -1;
328 
329 	for (i = 0; i < MAX_HPET_TBS; i++) {
330 		if (ir_hpet[i].iommu && ir_hpet[i].id == id) {
331 			sid = PCI_DEVID(ir_hpet[i].bus, ir_hpet[i].devfn);
332 			break;
333 		}
334 	}
335 
336 	if (sid == 0) {
337 		pr_warn("Failed to set source-id of HPET block (%d)\n", id);
338 		return -1;
339 	}
340 
341 	/*
342 	 * Should really use SQ_ALL_16. Some platforms are broken.
343 	 * While we figure out the right quirks for these broken platforms, use
344 	 * SQ_13_IGNORE_3 for now.
345 	 */
346 	set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_13_IGNORE_3, sid);
347 
348 	return 0;
349 }
350 
351 struct set_msi_sid_data {
352 	struct pci_dev *pdev;
353 	u16 alias;
354 	int count;
355 	int busmatch_count;
356 };
357 
set_msi_sid_cb(struct pci_dev * pdev,u16 alias,void * opaque)358 static int set_msi_sid_cb(struct pci_dev *pdev, u16 alias, void *opaque)
359 {
360 	struct set_msi_sid_data *data = opaque;
361 
362 	if (data->count == 0 || PCI_BUS_NUM(alias) == PCI_BUS_NUM(data->alias))
363 		data->busmatch_count++;
364 
365 	data->pdev = pdev;
366 	data->alias = alias;
367 	data->count++;
368 
369 	return 0;
370 }
371 
set_msi_sid(struct irte * irte,struct pci_dev * dev)372 static int set_msi_sid(struct irte *irte, struct pci_dev *dev)
373 {
374 	struct set_msi_sid_data data;
375 
376 	if (!irte || !dev)
377 		return -1;
378 
379 	data.count = 0;
380 	data.busmatch_count = 0;
381 	pci_for_each_dma_alias(dev, set_msi_sid_cb, &data);
382 
383 	/*
384 	 * DMA alias provides us with a PCI device and alias.  The only case
385 	 * where the it will return an alias on a different bus than the
386 	 * device is the case of a PCIe-to-PCI bridge, where the alias is for
387 	 * the subordinate bus.  In this case we can only verify the bus.
388 	 *
389 	 * If there are multiple aliases, all with the same bus number,
390 	 * then all we can do is verify the bus. This is typical in NTB
391 	 * hardware which use proxy IDs where the device will generate traffic
392 	 * from multiple devfn numbers on the same bus.
393 	 *
394 	 * If the alias device is on a different bus than our source device
395 	 * then we have a topology based alias, use it.
396 	 *
397 	 * Otherwise, the alias is for a device DMA quirk and we cannot
398 	 * assume that MSI uses the same requester ID.  Therefore use the
399 	 * original device.
400 	 */
401 	if (PCI_BUS_NUM(data.alias) != data.pdev->bus->number)
402 		set_irte_verify_bus(irte, PCI_BUS_NUM(data.alias),
403 				    dev->bus->number);
404 	else if (data.count >= 2 && data.busmatch_count == data.count)
405 		set_irte_verify_bus(irte, dev->bus->number, dev->bus->number);
406 	else if (data.pdev->bus->number != dev->bus->number)
407 		set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16, data.alias);
408 	else
409 		set_irte_sid(irte, SVT_VERIFY_SID_SQ, SQ_ALL_16,
410 			     pci_dev_id(dev));
411 
412 	return 0;
413 }
414 
iommu_load_old_irte(struct intel_iommu * iommu)415 static int iommu_load_old_irte(struct intel_iommu *iommu)
416 {
417 	struct irte *old_ir_table;
418 	phys_addr_t irt_phys;
419 	unsigned int i;
420 	size_t size;
421 	u64 irta;
422 
423 	/* Check whether the old ir-table has the same size as ours */
424 	irta = dmar_readq(iommu->reg + DMAR_IRTA_REG);
425 	if ((irta & INTR_REMAP_TABLE_REG_SIZE_MASK)
426 	     != INTR_REMAP_TABLE_REG_SIZE)
427 		return -EINVAL;
428 
429 	irt_phys = irta & VTD_PAGE_MASK;
430 	size     = INTR_REMAP_TABLE_ENTRIES*sizeof(struct irte);
431 
432 	/* Map the old IR table */
433 	old_ir_table = memremap(irt_phys, size, MEMREMAP_WB);
434 	if (!old_ir_table)
435 		return -ENOMEM;
436 
437 	/* Copy data over */
438 	memcpy(iommu->ir_table->base, old_ir_table, size);
439 
440 	__iommu_flush_cache(iommu, iommu->ir_table->base, size);
441 
442 	/*
443 	 * Now check the table for used entries and mark those as
444 	 * allocated in the bitmap
445 	 */
446 	for (i = 0; i < INTR_REMAP_TABLE_ENTRIES; i++) {
447 		if (iommu->ir_table->base[i].present)
448 			bitmap_set(iommu->ir_table->bitmap, i, 1);
449 	}
450 
451 	memunmap(old_ir_table);
452 
453 	return 0;
454 }
455 
456 
iommu_set_irq_remapping(struct intel_iommu * iommu,int mode)457 static void iommu_set_irq_remapping(struct intel_iommu *iommu, int mode)
458 {
459 	unsigned long flags;
460 	u64 addr;
461 	u32 sts;
462 
463 	addr = virt_to_phys((void *)iommu->ir_table->base);
464 
465 	raw_spin_lock_irqsave(&iommu->register_lock, flags);
466 
467 	dmar_writeq(iommu->reg + DMAR_IRTA_REG,
468 		    (addr) | IR_X2APIC_MODE(mode) | INTR_REMAP_TABLE_REG_SIZE);
469 
470 	/* Set interrupt-remapping table pointer */
471 	writel(iommu->gcmd | DMA_GCMD_SIRTP, iommu->reg + DMAR_GCMD_REG);
472 
473 	IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
474 		      readl, (sts & DMA_GSTS_IRTPS), sts);
475 	raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
476 
477 	/*
478 	 * Global invalidation of interrupt entry cache to make sure the
479 	 * hardware uses the new irq remapping table.
480 	 */
481 	if (!cap_esirtps(iommu->cap))
482 		qi_global_iec(iommu);
483 }
484 
iommu_enable_irq_remapping(struct intel_iommu * iommu)485 static void iommu_enable_irq_remapping(struct intel_iommu *iommu)
486 {
487 	unsigned long flags;
488 	u32 sts;
489 
490 	raw_spin_lock_irqsave(&iommu->register_lock, flags);
491 
492 	/* Enable interrupt-remapping */
493 	iommu->gcmd |= DMA_GCMD_IRE;
494 	writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
495 	IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
496 		      readl, (sts & DMA_GSTS_IRES), sts);
497 
498 	/* Block compatibility-format MSIs */
499 	if (sts & DMA_GSTS_CFIS) {
500 		iommu->gcmd &= ~DMA_GCMD_CFI;
501 		writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
502 		IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
503 			      readl, !(sts & DMA_GSTS_CFIS), sts);
504 	}
505 
506 	/*
507 	 * With CFI clear in the Global Command register, we should be
508 	 * protected from dangerous (i.e. compatibility) interrupts
509 	 * regardless of x2apic status.  Check just to be sure.
510 	 */
511 	if (sts & DMA_GSTS_CFIS)
512 		WARN(1, KERN_WARNING
513 			"Compatibility-format IRQs enabled despite intr remapping;\n"
514 			"you are vulnerable to IRQ injection.\n");
515 
516 	raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
517 }
518 
intel_setup_irq_remapping(struct intel_iommu * iommu)519 static int intel_setup_irq_remapping(struct intel_iommu *iommu)
520 {
521 	struct ir_table *ir_table;
522 	struct fwnode_handle *fn;
523 	unsigned long *bitmap;
524 	void *ir_table_base;
525 
526 	if (iommu->ir_table)
527 		return 0;
528 
529 	ir_table = kzalloc(sizeof(struct ir_table), GFP_KERNEL);
530 	if (!ir_table)
531 		return -ENOMEM;
532 
533 	ir_table_base = iommu_alloc_pages_node(iommu->node, GFP_KERNEL,
534 					       INTR_REMAP_PAGE_ORDER);
535 	if (!ir_table_base) {
536 		pr_err("IR%d: failed to allocate pages of order %d\n",
537 		       iommu->seq_id, INTR_REMAP_PAGE_ORDER);
538 		goto out_free_table;
539 	}
540 
541 	bitmap = bitmap_zalloc(INTR_REMAP_TABLE_ENTRIES, GFP_KERNEL);
542 	if (bitmap == NULL) {
543 		pr_err("IR%d: failed to allocate bitmap\n", iommu->seq_id);
544 		goto out_free_pages;
545 	}
546 
547 	fn = irq_domain_alloc_named_id_fwnode("INTEL-IR", iommu->seq_id);
548 	if (!fn)
549 		goto out_free_bitmap;
550 
551 	iommu->ir_domain =
552 		irq_domain_create_hierarchy(arch_get_ir_parent_domain(),
553 					    0, INTR_REMAP_TABLE_ENTRIES,
554 					    fn, &intel_ir_domain_ops,
555 					    iommu);
556 	if (!iommu->ir_domain) {
557 		pr_err("IR%d: failed to allocate irqdomain\n", iommu->seq_id);
558 		goto out_free_fwnode;
559 	}
560 
561 	irq_domain_update_bus_token(iommu->ir_domain,  DOMAIN_BUS_DMAR);
562 	iommu->ir_domain->flags |= IRQ_DOMAIN_FLAG_MSI_PARENT |
563 				   IRQ_DOMAIN_FLAG_ISOLATED_MSI;
564 	iommu->ir_domain->msi_parent_ops = &dmar_msi_parent_ops;
565 
566 	ir_table->base = ir_table_base;
567 	ir_table->bitmap = bitmap;
568 	iommu->ir_table = ir_table;
569 
570 	/*
571 	 * If the queued invalidation is already initialized,
572 	 * shouldn't disable it.
573 	 */
574 	if (!iommu->qi) {
575 		/*
576 		 * Clear previous faults.
577 		 */
578 		dmar_fault(-1, iommu);
579 		dmar_disable_qi(iommu);
580 
581 		if (dmar_enable_qi(iommu)) {
582 			pr_err("Failed to enable queued invalidation\n");
583 			goto out_free_ir_domain;
584 		}
585 	}
586 
587 	init_ir_status(iommu);
588 
589 	if (ir_pre_enabled(iommu)) {
590 		if (!is_kdump_kernel()) {
591 			pr_info_once("IRQ remapping was enabled on %s but we are not in kdump mode\n",
592 				     iommu->name);
593 			clear_ir_pre_enabled(iommu);
594 			iommu_disable_irq_remapping(iommu);
595 		} else if (iommu_load_old_irte(iommu))
596 			pr_err("Failed to copy IR table for %s from previous kernel\n",
597 			       iommu->name);
598 		else
599 			pr_info("Copied IR table for %s from previous kernel\n",
600 				iommu->name);
601 	}
602 
603 	iommu_set_irq_remapping(iommu, eim_mode);
604 
605 	return 0;
606 
607 out_free_ir_domain:
608 	irq_domain_remove(iommu->ir_domain);
609 	iommu->ir_domain = NULL;
610 out_free_fwnode:
611 	irq_domain_free_fwnode(fn);
612 out_free_bitmap:
613 	bitmap_free(bitmap);
614 out_free_pages:
615 	iommu_free_pages(ir_table_base, INTR_REMAP_PAGE_ORDER);
616 out_free_table:
617 	kfree(ir_table);
618 
619 	iommu->ir_table  = NULL;
620 
621 	return -ENOMEM;
622 }
623 
intel_teardown_irq_remapping(struct intel_iommu * iommu)624 static void intel_teardown_irq_remapping(struct intel_iommu *iommu)
625 {
626 	struct fwnode_handle *fn;
627 
628 	if (iommu && iommu->ir_table) {
629 		if (iommu->ir_domain) {
630 			fn = iommu->ir_domain->fwnode;
631 
632 			irq_domain_remove(iommu->ir_domain);
633 			irq_domain_free_fwnode(fn);
634 			iommu->ir_domain = NULL;
635 		}
636 		iommu_free_pages(iommu->ir_table->base, INTR_REMAP_PAGE_ORDER);
637 		bitmap_free(iommu->ir_table->bitmap);
638 		kfree(iommu->ir_table);
639 		iommu->ir_table = NULL;
640 	}
641 }
642 
643 /*
644  * Disable Interrupt Remapping.
645  */
iommu_disable_irq_remapping(struct intel_iommu * iommu)646 static void iommu_disable_irq_remapping(struct intel_iommu *iommu)
647 {
648 	unsigned long flags;
649 	u32 sts;
650 
651 	if (!ecap_ir_support(iommu->ecap))
652 		return;
653 
654 	/*
655 	 * global invalidation of interrupt entry cache before disabling
656 	 * interrupt-remapping.
657 	 */
658 	if (!cap_esirtps(iommu->cap))
659 		qi_global_iec(iommu);
660 
661 	raw_spin_lock_irqsave(&iommu->register_lock, flags);
662 
663 	sts = readl(iommu->reg + DMAR_GSTS_REG);
664 	if (!(sts & DMA_GSTS_IRES))
665 		goto end;
666 
667 	iommu->gcmd &= ~DMA_GCMD_IRE;
668 	writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
669 
670 	IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
671 		      readl, !(sts & DMA_GSTS_IRES), sts);
672 
673 end:
674 	raw_spin_unlock_irqrestore(&iommu->register_lock, flags);
675 }
676 
dmar_x2apic_optout(void)677 static int __init dmar_x2apic_optout(void)
678 {
679 	struct acpi_table_dmar *dmar;
680 	dmar = (struct acpi_table_dmar *)dmar_tbl;
681 	if (!dmar || no_x2apic_optout)
682 		return 0;
683 	return dmar->flags & DMAR_X2APIC_OPT_OUT;
684 }
685 
intel_cleanup_irq_remapping(void)686 static void __init intel_cleanup_irq_remapping(void)
687 {
688 	struct dmar_drhd_unit *drhd;
689 	struct intel_iommu *iommu;
690 
691 	for_each_iommu(iommu, drhd) {
692 		if (ecap_ir_support(iommu->ecap)) {
693 			iommu_disable_irq_remapping(iommu);
694 			intel_teardown_irq_remapping(iommu);
695 		}
696 	}
697 
698 	if (x2apic_supported())
699 		pr_warn("Failed to enable irq remapping. You are vulnerable to irq-injection attacks.\n");
700 }
701 
intel_prepare_irq_remapping(void)702 static int __init intel_prepare_irq_remapping(void)
703 {
704 	struct dmar_drhd_unit *drhd;
705 	struct intel_iommu *iommu;
706 	int eim = 0;
707 
708 	if (irq_remap_broken) {
709 		pr_warn("This system BIOS has enabled interrupt remapping\n"
710 			"on a chipset that contains an erratum making that\n"
711 			"feature unstable.  To maintain system stability\n"
712 			"interrupt remapping is being disabled.  Please\n"
713 			"contact your BIOS vendor for an update\n");
714 		add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
715 		return -ENODEV;
716 	}
717 
718 	if (dmar_table_init() < 0)
719 		return -ENODEV;
720 
721 	if (!dmar_ir_support())
722 		return -ENODEV;
723 
724 	if (parse_ioapics_under_ir()) {
725 		pr_info("Not enabling interrupt remapping\n");
726 		goto error;
727 	}
728 
729 	/* First make sure all IOMMUs support IRQ remapping */
730 	for_each_iommu(iommu, drhd)
731 		if (!ecap_ir_support(iommu->ecap))
732 			goto error;
733 
734 	/* Detect remapping mode: lapic or x2apic */
735 	if (x2apic_supported()) {
736 		eim = !dmar_x2apic_optout();
737 		if (!eim) {
738 			pr_info("x2apic is disabled because BIOS sets x2apic opt out bit.");
739 			pr_info("Use 'intremap=no_x2apic_optout' to override the BIOS setting.\n");
740 		}
741 	}
742 
743 	for_each_iommu(iommu, drhd) {
744 		if (eim && !ecap_eim_support(iommu->ecap)) {
745 			pr_info("%s does not support EIM\n", iommu->name);
746 			eim = 0;
747 		}
748 	}
749 
750 	eim_mode = eim;
751 	if (eim)
752 		pr_info("Queued invalidation will be enabled to support x2apic and Intr-remapping.\n");
753 
754 	/* Do the initializations early */
755 	for_each_iommu(iommu, drhd) {
756 		if (intel_setup_irq_remapping(iommu)) {
757 			pr_err("Failed to setup irq remapping for %s\n",
758 			       iommu->name);
759 			goto error;
760 		}
761 	}
762 
763 	return 0;
764 
765 error:
766 	intel_cleanup_irq_remapping();
767 	return -ENODEV;
768 }
769 
770 /*
771  * Set Posted-Interrupts capability.
772  */
set_irq_posting_cap(void)773 static inline void set_irq_posting_cap(void)
774 {
775 	struct dmar_drhd_unit *drhd;
776 	struct intel_iommu *iommu;
777 
778 	if (!disable_irq_post) {
779 		/*
780 		 * If IRTE is in posted format, the 'pda' field goes across the
781 		 * 64-bit boundary, we need use cmpxchg16b to atomically update
782 		 * it. We only expose posted-interrupt when X86_FEATURE_CX16
783 		 * is supported. Actually, hardware platforms supporting PI
784 		 * should have X86_FEATURE_CX16 support, this has been confirmed
785 		 * with Intel hardware guys.
786 		 */
787 		if (boot_cpu_has(X86_FEATURE_CX16))
788 			intel_irq_remap_ops.capability |= 1 << IRQ_POSTING_CAP;
789 
790 		for_each_iommu(iommu, drhd)
791 			if (!cap_pi_support(iommu->cap)) {
792 				intel_irq_remap_ops.capability &=
793 						~(1 << IRQ_POSTING_CAP);
794 				break;
795 			}
796 	}
797 }
798 
intel_enable_irq_remapping(void)799 static int __init intel_enable_irq_remapping(void)
800 {
801 	struct dmar_drhd_unit *drhd;
802 	struct intel_iommu *iommu;
803 	bool setup = false;
804 
805 	/*
806 	 * Setup Interrupt-remapping for all the DRHD's now.
807 	 */
808 	for_each_iommu(iommu, drhd) {
809 		if (!ir_pre_enabled(iommu))
810 			iommu_enable_irq_remapping(iommu);
811 		setup = true;
812 	}
813 
814 	if (!setup)
815 		goto error;
816 
817 	irq_remapping_enabled = 1;
818 
819 	set_irq_posting_cap();
820 
821 	pr_info("Enabled IRQ remapping in %s mode\n", eim_mode ? "x2apic" : "xapic");
822 
823 	return eim_mode ? IRQ_REMAP_X2APIC_MODE : IRQ_REMAP_XAPIC_MODE;
824 
825 error:
826 	intel_cleanup_irq_remapping();
827 	return -1;
828 }
829 
ir_parse_one_hpet_scope(struct acpi_dmar_device_scope * scope,struct intel_iommu * iommu,struct acpi_dmar_hardware_unit * drhd)830 static int ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
831 				   struct intel_iommu *iommu,
832 				   struct acpi_dmar_hardware_unit *drhd)
833 {
834 	struct acpi_dmar_pci_path *path;
835 	u8 bus;
836 	int count, free = -1;
837 
838 	bus = scope->bus;
839 	path = (struct acpi_dmar_pci_path *)(scope + 1);
840 	count = (scope->length - sizeof(struct acpi_dmar_device_scope))
841 		/ sizeof(struct acpi_dmar_pci_path);
842 
843 	while (--count > 0) {
844 		/*
845 		 * Access PCI directly due to the PCI
846 		 * subsystem isn't initialized yet.
847 		 */
848 		bus = read_pci_config_byte(bus, path->device, path->function,
849 					   PCI_SECONDARY_BUS);
850 		path++;
851 	}
852 
853 	for (count = 0; count < MAX_HPET_TBS; count++) {
854 		if (ir_hpet[count].iommu == iommu &&
855 		    ir_hpet[count].id == scope->enumeration_id)
856 			return 0;
857 		else if (ir_hpet[count].iommu == NULL && free == -1)
858 			free = count;
859 	}
860 	if (free == -1) {
861 		pr_warn("Exceeded Max HPET blocks\n");
862 		return -ENOSPC;
863 	}
864 
865 	ir_hpet[free].iommu = iommu;
866 	ir_hpet[free].id    = scope->enumeration_id;
867 	ir_hpet[free].bus   = bus;
868 	ir_hpet[free].devfn = PCI_DEVFN(path->device, path->function);
869 	pr_info("HPET id %d under DRHD base 0x%Lx\n",
870 		scope->enumeration_id, drhd->address);
871 
872 	return 0;
873 }
874 
ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope * scope,struct intel_iommu * iommu,struct acpi_dmar_hardware_unit * drhd)875 static int ir_parse_one_ioapic_scope(struct acpi_dmar_device_scope *scope,
876 				     struct intel_iommu *iommu,
877 				     struct acpi_dmar_hardware_unit *drhd)
878 {
879 	struct acpi_dmar_pci_path *path;
880 	u8 bus;
881 	int count, free = -1;
882 
883 	bus = scope->bus;
884 	path = (struct acpi_dmar_pci_path *)(scope + 1);
885 	count = (scope->length - sizeof(struct acpi_dmar_device_scope))
886 		/ sizeof(struct acpi_dmar_pci_path);
887 
888 	while (--count > 0) {
889 		/*
890 		 * Access PCI directly due to the PCI
891 		 * subsystem isn't initialized yet.
892 		 */
893 		bus = read_pci_config_byte(bus, path->device, path->function,
894 					   PCI_SECONDARY_BUS);
895 		path++;
896 	}
897 
898 	for (count = 0; count < MAX_IO_APICS; count++) {
899 		if (ir_ioapic[count].iommu == iommu &&
900 		    ir_ioapic[count].id == scope->enumeration_id)
901 			return 0;
902 		else if (ir_ioapic[count].iommu == NULL && free == -1)
903 			free = count;
904 	}
905 	if (free == -1) {
906 		pr_warn("Exceeded Max IO APICS\n");
907 		return -ENOSPC;
908 	}
909 
910 	ir_ioapic[free].bus   = bus;
911 	ir_ioapic[free].devfn = PCI_DEVFN(path->device, path->function);
912 	ir_ioapic[free].iommu = iommu;
913 	ir_ioapic[free].id    = scope->enumeration_id;
914 	pr_info("IOAPIC id %d under DRHD base  0x%Lx IOMMU %d\n",
915 		scope->enumeration_id, drhd->address, iommu->seq_id);
916 
917 	return 0;
918 }
919 
ir_parse_ioapic_hpet_scope(struct acpi_dmar_header * header,struct intel_iommu * iommu)920 static int ir_parse_ioapic_hpet_scope(struct acpi_dmar_header *header,
921 				      struct intel_iommu *iommu)
922 {
923 	int ret = 0;
924 	struct acpi_dmar_hardware_unit *drhd;
925 	struct acpi_dmar_device_scope *scope;
926 	void *start, *end;
927 
928 	drhd = (struct acpi_dmar_hardware_unit *)header;
929 	start = (void *)(drhd + 1);
930 	end = ((void *)drhd) + header->length;
931 
932 	while (start < end && ret == 0) {
933 		scope = start;
934 		if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC)
935 			ret = ir_parse_one_ioapic_scope(scope, iommu, drhd);
936 		else if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET)
937 			ret = ir_parse_one_hpet_scope(scope, iommu, drhd);
938 		start += scope->length;
939 	}
940 
941 	return ret;
942 }
943 
ir_remove_ioapic_hpet_scope(struct intel_iommu * iommu)944 static void ir_remove_ioapic_hpet_scope(struct intel_iommu *iommu)
945 {
946 	int i;
947 
948 	for (i = 0; i < MAX_HPET_TBS; i++)
949 		if (ir_hpet[i].iommu == iommu)
950 			ir_hpet[i].iommu = NULL;
951 
952 	for (i = 0; i < MAX_IO_APICS; i++)
953 		if (ir_ioapic[i].iommu == iommu)
954 			ir_ioapic[i].iommu = NULL;
955 }
956 
957 /*
958  * Finds the assocaition between IOAPIC's and its Interrupt-remapping
959  * hardware unit.
960  */
parse_ioapics_under_ir(void)961 static int __init parse_ioapics_under_ir(void)
962 {
963 	struct dmar_drhd_unit *drhd;
964 	struct intel_iommu *iommu;
965 	bool ir_supported = false;
966 	int ioapic_idx;
967 
968 	for_each_iommu(iommu, drhd) {
969 		int ret;
970 
971 		if (!ecap_ir_support(iommu->ecap))
972 			continue;
973 
974 		ret = ir_parse_ioapic_hpet_scope(drhd->hdr, iommu);
975 		if (ret)
976 			return ret;
977 
978 		ir_supported = true;
979 	}
980 
981 	if (!ir_supported)
982 		return -ENODEV;
983 
984 	for (ioapic_idx = 0; ioapic_idx < nr_ioapics; ioapic_idx++) {
985 		int ioapic_id = mpc_ioapic_id(ioapic_idx);
986 		if (!map_ioapic_to_iommu(ioapic_id)) {
987 			pr_err(FW_BUG "ioapic %d has no mapping iommu, "
988 			       "interrupt remapping will be disabled\n",
989 			       ioapic_id);
990 			return -1;
991 		}
992 	}
993 
994 	return 0;
995 }
996 
ir_dev_scope_init(void)997 static int __init ir_dev_scope_init(void)
998 {
999 	int ret;
1000 
1001 	if (!irq_remapping_enabled)
1002 		return 0;
1003 
1004 	down_write(&dmar_global_lock);
1005 	ret = dmar_dev_scope_init();
1006 	up_write(&dmar_global_lock);
1007 
1008 	return ret;
1009 }
1010 rootfs_initcall(ir_dev_scope_init);
1011 
disable_irq_remapping(void)1012 static void disable_irq_remapping(void)
1013 {
1014 	struct dmar_drhd_unit *drhd;
1015 	struct intel_iommu *iommu = NULL;
1016 
1017 	/*
1018 	 * Disable Interrupt-remapping for all the DRHD's now.
1019 	 */
1020 	for_each_iommu(iommu, drhd) {
1021 		if (!ecap_ir_support(iommu->ecap))
1022 			continue;
1023 
1024 		iommu_disable_irq_remapping(iommu);
1025 	}
1026 
1027 	/*
1028 	 * Clear Posted-Interrupts capability.
1029 	 */
1030 	if (!disable_irq_post)
1031 		intel_irq_remap_ops.capability &= ~(1 << IRQ_POSTING_CAP);
1032 }
1033 
reenable_irq_remapping(int eim)1034 static int reenable_irq_remapping(int eim)
1035 {
1036 	struct dmar_drhd_unit *drhd;
1037 	bool setup = false;
1038 	struct intel_iommu *iommu = NULL;
1039 
1040 	for_each_iommu(iommu, drhd)
1041 		if (iommu->qi)
1042 			dmar_reenable_qi(iommu);
1043 
1044 	/*
1045 	 * Setup Interrupt-remapping for all the DRHD's now.
1046 	 */
1047 	for_each_iommu(iommu, drhd) {
1048 		if (!ecap_ir_support(iommu->ecap))
1049 			continue;
1050 
1051 		/* Set up interrupt remapping for iommu.*/
1052 		iommu_set_irq_remapping(iommu, eim);
1053 		iommu_enable_irq_remapping(iommu);
1054 		setup = true;
1055 	}
1056 
1057 	if (!setup)
1058 		goto error;
1059 
1060 	set_irq_posting_cap();
1061 
1062 	return 0;
1063 
1064 error:
1065 	/*
1066 	 * handle error condition gracefully here!
1067 	 */
1068 	return -1;
1069 }
1070 
1071 /*
1072  * Store the MSI remapping domain pointer in the device if enabled.
1073  *
1074  * This is called from dmar_pci_bus_add_dev() so it works even when DMA
1075  * remapping is disabled. Only update the pointer if the device is not
1076  * already handled by a non default PCI/MSI interrupt domain. This protects
1077  * e.g. VMD devices.
1078  */
intel_irq_remap_add_device(struct dmar_pci_notify_info * info)1079 void intel_irq_remap_add_device(struct dmar_pci_notify_info *info)
1080 {
1081 	if (!irq_remapping_enabled || !pci_dev_has_default_msi_parent_domain(info->dev))
1082 		return;
1083 
1084 	dev_set_msi_domain(&info->dev->dev, map_dev_to_ir(info->dev));
1085 }
1086 
prepare_irte(struct irte * irte,int vector,unsigned int dest)1087 static void prepare_irte(struct irte *irte, int vector, unsigned int dest)
1088 {
1089 	memset(irte, 0, sizeof(*irte));
1090 
1091 	irte->present = 1;
1092 	irte->dst_mode = apic->dest_mode_logical;
1093 	/*
1094 	 * Trigger mode in the IRTE will always be edge, and for IO-APIC, the
1095 	 * actual level or edge trigger will be setup in the IO-APIC
1096 	 * RTE. This will help simplify level triggered irq migration.
1097 	 * For more details, see the comments (in io_apic.c) explainig IO-APIC
1098 	 * irq migration in the presence of interrupt-remapping.
1099 	*/
1100 	irte->trigger_mode = 0;
1101 	irte->dlvry_mode = APIC_DELIVERY_MODE_FIXED;
1102 	irte->vector = vector;
1103 	irte->dest_id = IRTE_DEST(dest);
1104 	irte->redir_hint = 1;
1105 }
1106 
prepare_irte_posted(struct irte * irte)1107 static void prepare_irte_posted(struct irte *irte)
1108 {
1109 	memset(irte, 0, sizeof(*irte));
1110 
1111 	irte->present = 1;
1112 	irte->p_pst = 1;
1113 }
1114 
1115 struct irq_remap_ops intel_irq_remap_ops = {
1116 	.prepare		= intel_prepare_irq_remapping,
1117 	.enable			= intel_enable_irq_remapping,
1118 	.disable		= disable_irq_remapping,
1119 	.reenable		= reenable_irq_remapping,
1120 	.enable_faulting	= enable_drhd_fault_handling,
1121 };
1122 
1123 #ifdef CONFIG_X86_POSTED_MSI
1124 
get_pi_desc_addr(struct irq_data * irqd)1125 static phys_addr_t get_pi_desc_addr(struct irq_data *irqd)
1126 {
1127 	int cpu = cpumask_first(irq_data_get_effective_affinity_mask(irqd));
1128 
1129 	if (WARN_ON(cpu >= nr_cpu_ids))
1130 		return 0;
1131 
1132 	return __pa(per_cpu_ptr(&posted_msi_pi_desc, cpu));
1133 }
1134 
intel_ir_reconfigure_irte_posted(struct irq_data * irqd)1135 static void intel_ir_reconfigure_irte_posted(struct irq_data *irqd)
1136 {
1137 	struct intel_ir_data *ir_data = irqd->chip_data;
1138 	struct irte *irte = &ir_data->irte_entry;
1139 	struct irte irte_pi;
1140 	u64 pid_addr;
1141 
1142 	pid_addr = get_pi_desc_addr(irqd);
1143 
1144 	if (!pid_addr) {
1145 		pr_warn("Failed to setup IRQ %d for posted mode", irqd->irq);
1146 		return;
1147 	}
1148 
1149 	memset(&irte_pi, 0, sizeof(irte_pi));
1150 
1151 	/* The shared IRTE already be set up as posted during alloc_irte */
1152 	dmar_copy_shared_irte(&irte_pi, irte);
1153 
1154 	irte_pi.pda_l = (pid_addr >> (32 - PDA_LOW_BIT)) & ~(-1UL << PDA_LOW_BIT);
1155 	irte_pi.pda_h = (pid_addr >> 32) & ~(-1UL << PDA_HIGH_BIT);
1156 
1157 	modify_irte(&ir_data->irq_2_iommu, &irte_pi);
1158 }
1159 
1160 #else
intel_ir_reconfigure_irte_posted(struct irq_data * irqd)1161 static inline void intel_ir_reconfigure_irte_posted(struct irq_data *irqd) {}
1162 #endif
1163 
__intel_ir_reconfigure_irte(struct irq_data * irqd,bool force_host)1164 static void __intel_ir_reconfigure_irte(struct irq_data *irqd, bool force_host)
1165 {
1166 	struct intel_ir_data *ir_data = irqd->chip_data;
1167 
1168 	/*
1169 	 * Don't modify IRTEs for IRQs that are being posted to vCPUs if the
1170 	 * host CPU affinity changes.
1171 	 */
1172 	if (ir_data->irq_2_iommu.posted_vcpu && !force_host)
1173 		return;
1174 
1175 	ir_data->irq_2_iommu.posted_vcpu = false;
1176 
1177 	if (ir_data->irq_2_iommu.posted_msi)
1178 		intel_ir_reconfigure_irte_posted(irqd);
1179 	else
1180 		modify_irte(&ir_data->irq_2_iommu, &ir_data->irte_entry);
1181 }
1182 
intel_ir_reconfigure_irte(struct irq_data * irqd,bool force_host)1183 static void intel_ir_reconfigure_irte(struct irq_data *irqd, bool force_host)
1184 {
1185 	struct intel_ir_data *ir_data = irqd->chip_data;
1186 	struct irte *irte = &ir_data->irte_entry;
1187 	struct irq_cfg *cfg = irqd_cfg(irqd);
1188 
1189 	/*
1190 	 * Atomically updates the IRTE with the new destination, vector
1191 	 * and flushes the interrupt entry cache.
1192 	 */
1193 	irte->vector = cfg->vector;
1194 	irte->dest_id = IRTE_DEST(cfg->dest_apicid);
1195 
1196 	__intel_ir_reconfigure_irte(irqd, force_host);
1197 }
1198 
1199 /*
1200  * Migrate the IO-APIC irq in the presence of intr-remapping.
1201  *
1202  * For both level and edge triggered, irq migration is a simple atomic
1203  * update(of vector and cpu destination) of IRTE and flush the hardware cache.
1204  *
1205  * For level triggered, we eliminate the io-apic RTE modification (with the
1206  * updated vector information), by using a virtual vector (io-apic pin number).
1207  * Real vector that is used for interrupting cpu will be coming from
1208  * the interrupt-remapping table entry.
1209  *
1210  * As the migration is a simple atomic update of IRTE, the same mechanism
1211  * is used to migrate MSI irq's in the presence of interrupt-remapping.
1212  */
1213 static int
intel_ir_set_affinity(struct irq_data * data,const struct cpumask * mask,bool force)1214 intel_ir_set_affinity(struct irq_data *data, const struct cpumask *mask,
1215 		      bool force)
1216 {
1217 	struct irq_data *parent = data->parent_data;
1218 	struct irq_cfg *cfg = irqd_cfg(data);
1219 	int ret;
1220 
1221 	ret = parent->chip->irq_set_affinity(parent, mask, force);
1222 	if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
1223 		return ret;
1224 
1225 	intel_ir_reconfigure_irte(data, false);
1226 	/*
1227 	 * After this point, all the interrupts will start arriving
1228 	 * at the new destination. So, time to cleanup the previous
1229 	 * vector allocation.
1230 	 */
1231 	vector_schedule_cleanup(cfg);
1232 
1233 	return IRQ_SET_MASK_OK_DONE;
1234 }
1235 
intel_ir_compose_msi_msg(struct irq_data * irq_data,struct msi_msg * msg)1236 static void intel_ir_compose_msi_msg(struct irq_data *irq_data,
1237 				     struct msi_msg *msg)
1238 {
1239 	struct intel_ir_data *ir_data = irq_data->chip_data;
1240 
1241 	*msg = ir_data->msi_entry;
1242 }
1243 
intel_ir_set_vcpu_affinity(struct irq_data * data,void * info)1244 static int intel_ir_set_vcpu_affinity(struct irq_data *data, void *info)
1245 {
1246 	struct intel_ir_data *ir_data = data->chip_data;
1247 	struct vcpu_data *vcpu_pi_info = info;
1248 
1249 	/* stop posting interrupts, back to the default mode */
1250 	if (!vcpu_pi_info) {
1251 		__intel_ir_reconfigure_irte(data, true);
1252 	} else {
1253 		struct irte irte_pi;
1254 
1255 		/*
1256 		 * We are not caching the posted interrupt entry. We
1257 		 * copy the data from the remapped entry and modify
1258 		 * the fields which are relevant for posted mode. The
1259 		 * cached remapped entry is used for switching back to
1260 		 * remapped mode.
1261 		 */
1262 		memset(&irte_pi, 0, sizeof(irte_pi));
1263 		dmar_copy_shared_irte(&irte_pi, &ir_data->irte_entry);
1264 
1265 		/* Update the posted mode fields */
1266 		irte_pi.p_pst = 1;
1267 		irte_pi.p_urgent = 0;
1268 		irte_pi.p_vector = vcpu_pi_info->vector;
1269 		irte_pi.pda_l = (vcpu_pi_info->pi_desc_addr >>
1270 				(32 - PDA_LOW_BIT)) & ~(-1UL << PDA_LOW_BIT);
1271 		irte_pi.pda_h = (vcpu_pi_info->pi_desc_addr >> 32) &
1272 				~(-1UL << PDA_HIGH_BIT);
1273 
1274 		ir_data->irq_2_iommu.posted_vcpu = true;
1275 		modify_irte(&ir_data->irq_2_iommu, &irte_pi);
1276 	}
1277 
1278 	return 0;
1279 }
1280 
1281 static struct irq_chip intel_ir_chip = {
1282 	.name			= "INTEL-IR",
1283 	.irq_ack		= apic_ack_irq,
1284 	.irq_set_affinity	= intel_ir_set_affinity,
1285 	.irq_compose_msi_msg	= intel_ir_compose_msi_msg,
1286 	.irq_set_vcpu_affinity	= intel_ir_set_vcpu_affinity,
1287 };
1288 
1289 /*
1290  * With posted MSIs, all vectors are multiplexed into a single notification
1291  * vector. Devices MSIs are then dispatched in a demux loop where
1292  * EOIs can be coalesced as well.
1293  *
1294  * "INTEL-IR-POST" IRQ chip does not do EOI on ACK, thus the dummy irq_ack()
1295  * function. Instead EOI is performed by the posted interrupt notification
1296  * handler.
1297  *
1298  * For the example below, 3 MSIs are coalesced into one CPU notification. Only
1299  * one apic_eoi() is needed.
1300  *
1301  * __sysvec_posted_msi_notification()
1302  *	irq_enter();
1303  *		handle_edge_irq()
1304  *			irq_chip_ack_parent()
1305  *				dummy(); // No EOI
1306  *			handle_irq_event()
1307  *				driver_handler()
1308  *		handle_edge_irq()
1309  *			irq_chip_ack_parent()
1310  *				dummy(); // No EOI
1311  *			handle_irq_event()
1312  *				driver_handler()
1313  *		handle_edge_irq()
1314  *			irq_chip_ack_parent()
1315  *				dummy(); // No EOI
1316  *			handle_irq_event()
1317  *				driver_handler()
1318  *	apic_eoi()
1319  *	irq_exit()
1320  */
1321 
dummy_ack(struct irq_data * d)1322 static void dummy_ack(struct irq_data *d) { }
1323 
1324 static struct irq_chip intel_ir_chip_post_msi = {
1325 	.name			= "INTEL-IR-POST",
1326 	.irq_ack		= dummy_ack,
1327 	.irq_set_affinity	= intel_ir_set_affinity,
1328 	.irq_compose_msi_msg	= intel_ir_compose_msi_msg,
1329 	.irq_set_vcpu_affinity	= intel_ir_set_vcpu_affinity,
1330 };
1331 
fill_msi_msg(struct msi_msg * msg,u32 index,u32 subhandle)1332 static void fill_msi_msg(struct msi_msg *msg, u32 index, u32 subhandle)
1333 {
1334 	memset(msg, 0, sizeof(*msg));
1335 
1336 	msg->arch_addr_lo.dmar_base_address = X86_MSI_BASE_ADDRESS_LOW;
1337 	msg->arch_addr_lo.dmar_subhandle_valid = true;
1338 	msg->arch_addr_lo.dmar_format = true;
1339 	msg->arch_addr_lo.dmar_index_0_14 = index & 0x7FFF;
1340 	msg->arch_addr_lo.dmar_index_15 = !!(index & 0x8000);
1341 
1342 	msg->address_hi = X86_MSI_BASE_ADDRESS_HIGH;
1343 
1344 	msg->arch_data.dmar_subhandle = subhandle;
1345 }
1346 
intel_irq_remapping_prepare_irte(struct intel_ir_data * data,struct irq_cfg * irq_cfg,struct irq_alloc_info * info,int index,int sub_handle)1347 static void intel_irq_remapping_prepare_irte(struct intel_ir_data *data,
1348 					     struct irq_cfg *irq_cfg,
1349 					     struct irq_alloc_info *info,
1350 					     int index, int sub_handle)
1351 {
1352 	struct irte *irte = &data->irte_entry;
1353 
1354 	prepare_irte(irte, irq_cfg->vector, irq_cfg->dest_apicid);
1355 
1356 	switch (info->type) {
1357 	case X86_IRQ_ALLOC_TYPE_IOAPIC:
1358 		/* Set source-id of interrupt request */
1359 		set_ioapic_sid(irte, info->devid);
1360 		apic_pr_verbose("IOAPIC[%d]: Set IRTE entry (P:%d FPD:%d Dst_Mode:%d Redir_hint:%d Trig_Mode:%d Dlvry_Mode:%X Avail:%X Vector:%02X Dest:%08X SID:%04X SQ:%X SVT:%X)\n",
1361 				info->devid, irte->present, irte->fpd, irte->dst_mode,
1362 				irte->redir_hint, irte->trigger_mode, irte->dlvry_mode,
1363 				irte->avail, irte->vector, irte->dest_id, irte->sid,
1364 				irte->sq, irte->svt);
1365 		sub_handle = info->ioapic.pin;
1366 		break;
1367 	case X86_IRQ_ALLOC_TYPE_HPET:
1368 		set_hpet_sid(irte, info->devid);
1369 		break;
1370 	case X86_IRQ_ALLOC_TYPE_PCI_MSI:
1371 	case X86_IRQ_ALLOC_TYPE_PCI_MSIX:
1372 		if (posted_msi_supported()) {
1373 			prepare_irte_posted(irte);
1374 			data->irq_2_iommu.posted_msi = 1;
1375 		}
1376 
1377 		set_msi_sid(irte,
1378 			    pci_real_dma_dev(msi_desc_to_pci_dev(info->desc)));
1379 		break;
1380 	default:
1381 		BUG_ON(1);
1382 		break;
1383 	}
1384 	fill_msi_msg(&data->msi_entry, index, sub_handle);
1385 }
1386 
intel_free_irq_resources(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)1387 static void intel_free_irq_resources(struct irq_domain *domain,
1388 				     unsigned int virq, unsigned int nr_irqs)
1389 {
1390 	struct irq_data *irq_data;
1391 	struct intel_ir_data *data;
1392 	struct irq_2_iommu *irq_iommu;
1393 	unsigned long flags;
1394 	int i;
1395 	for (i = 0; i < nr_irqs; i++) {
1396 		irq_data = irq_domain_get_irq_data(domain, virq  + i);
1397 		if (irq_data && irq_data->chip_data) {
1398 			data = irq_data->chip_data;
1399 			irq_iommu = &data->irq_2_iommu;
1400 			raw_spin_lock_irqsave(&irq_2_ir_lock, flags);
1401 			clear_entries(irq_iommu);
1402 			raw_spin_unlock_irqrestore(&irq_2_ir_lock, flags);
1403 			irq_domain_reset_irq_data(irq_data);
1404 			kfree(data);
1405 		}
1406 	}
1407 }
1408 
intel_irq_remapping_alloc(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs,void * arg)1409 static int intel_irq_remapping_alloc(struct irq_domain *domain,
1410 				     unsigned int virq, unsigned int nr_irqs,
1411 				     void *arg)
1412 {
1413 	struct intel_iommu *iommu = domain->host_data;
1414 	struct irq_alloc_info *info = arg;
1415 	struct intel_ir_data *data, *ird;
1416 	struct irq_data *irq_data;
1417 	struct irq_cfg *irq_cfg;
1418 	int i, ret, index;
1419 
1420 	if (!info || !iommu)
1421 		return -EINVAL;
1422 	if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_PCI_MSI)
1423 		return -EINVAL;
1424 
1425 	ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
1426 	if (ret < 0)
1427 		return ret;
1428 
1429 	ret = -ENOMEM;
1430 	data = kzalloc(sizeof(*data), GFP_KERNEL);
1431 	if (!data)
1432 		goto out_free_parent;
1433 
1434 	index = alloc_irte(iommu, &data->irq_2_iommu, nr_irqs);
1435 	if (index < 0) {
1436 		pr_warn("Failed to allocate IRTE\n");
1437 		kfree(data);
1438 		goto out_free_parent;
1439 	}
1440 
1441 	for (i = 0; i < nr_irqs; i++) {
1442 		irq_data = irq_domain_get_irq_data(domain, virq + i);
1443 		irq_cfg = irqd_cfg(irq_data);
1444 		if (!irq_data || !irq_cfg) {
1445 			if (!i)
1446 				kfree(data);
1447 			ret = -EINVAL;
1448 			goto out_free_data;
1449 		}
1450 
1451 		if (i > 0) {
1452 			ird = kzalloc(sizeof(*ird), GFP_KERNEL);
1453 			if (!ird)
1454 				goto out_free_data;
1455 			/* Initialize the common data */
1456 			ird->irq_2_iommu = data->irq_2_iommu;
1457 			ird->irq_2_iommu.sub_handle = i;
1458 		} else {
1459 			ird = data;
1460 		}
1461 
1462 		irq_data->hwirq = (index << 16) + i;
1463 		irq_data->chip_data = ird;
1464 		if (posted_msi_supported() &&
1465 		    ((info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI) ||
1466 		     (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSIX)))
1467 			irq_data->chip = &intel_ir_chip_post_msi;
1468 		else
1469 			irq_data->chip = &intel_ir_chip;
1470 		intel_irq_remapping_prepare_irte(ird, irq_cfg, info, index, i);
1471 	}
1472 	return 0;
1473 
1474 out_free_data:
1475 	intel_free_irq_resources(domain, virq, i);
1476 out_free_parent:
1477 	irq_domain_free_irqs_common(domain, virq, nr_irqs);
1478 	return ret;
1479 }
1480 
intel_irq_remapping_free(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)1481 static void intel_irq_remapping_free(struct irq_domain *domain,
1482 				     unsigned int virq, unsigned int nr_irqs)
1483 {
1484 	intel_free_irq_resources(domain, virq, nr_irqs);
1485 	irq_domain_free_irqs_common(domain, virq, nr_irqs);
1486 }
1487 
intel_irq_remapping_activate(struct irq_domain * domain,struct irq_data * irq_data,bool reserve)1488 static int intel_irq_remapping_activate(struct irq_domain *domain,
1489 					struct irq_data *irq_data, bool reserve)
1490 {
1491 	intel_ir_reconfigure_irte(irq_data, true);
1492 	return 0;
1493 }
1494 
intel_irq_remapping_deactivate(struct irq_domain * domain,struct irq_data * irq_data)1495 static void intel_irq_remapping_deactivate(struct irq_domain *domain,
1496 					   struct irq_data *irq_data)
1497 {
1498 	struct intel_ir_data *data = irq_data->chip_data;
1499 	struct irte entry;
1500 
1501 	WARN_ON_ONCE(data->irq_2_iommu.posted_vcpu);
1502 	data->irq_2_iommu.posted_vcpu = false;
1503 
1504 	memset(&entry, 0, sizeof(entry));
1505 	modify_irte(&data->irq_2_iommu, &entry);
1506 }
1507 
intel_irq_remapping_select(struct irq_domain * d,struct irq_fwspec * fwspec,enum irq_domain_bus_token bus_token)1508 static int intel_irq_remapping_select(struct irq_domain *d,
1509 				      struct irq_fwspec *fwspec,
1510 				      enum irq_domain_bus_token bus_token)
1511 {
1512 	struct intel_iommu *iommu = NULL;
1513 
1514 	if (x86_fwspec_is_ioapic(fwspec))
1515 		iommu = map_ioapic_to_iommu(fwspec->param[0]);
1516 	else if (x86_fwspec_is_hpet(fwspec))
1517 		iommu = map_hpet_to_iommu(fwspec->param[0]);
1518 
1519 	return iommu && d == iommu->ir_domain;
1520 }
1521 
1522 static const struct irq_domain_ops intel_ir_domain_ops = {
1523 	.select = intel_irq_remapping_select,
1524 	.alloc = intel_irq_remapping_alloc,
1525 	.free = intel_irq_remapping_free,
1526 	.activate = intel_irq_remapping_activate,
1527 	.deactivate = intel_irq_remapping_deactivate,
1528 };
1529 
1530 static const struct msi_parent_ops dmar_msi_parent_ops = {
1531 	.supported_flags	= X86_VECTOR_MSI_FLAGS_SUPPORTED | MSI_FLAG_MULTI_PCI_MSI,
1532 	.prefix			= "IR-",
1533 	.init_dev_msi_info	= msi_parent_init_dev_msi_info,
1534 };
1535 
1536 /*
1537  * Support of Interrupt Remapping Unit Hotplug
1538  */
dmar_ir_add(struct dmar_drhd_unit * dmaru,struct intel_iommu * iommu)1539 static int dmar_ir_add(struct dmar_drhd_unit *dmaru, struct intel_iommu *iommu)
1540 {
1541 	int ret;
1542 	int eim = x2apic_enabled();
1543 
1544 	if (eim && !ecap_eim_support(iommu->ecap)) {
1545 		pr_info("DRHD %Lx: EIM not supported by DRHD, ecap %Lx\n",
1546 			iommu->reg_phys, iommu->ecap);
1547 		return -ENODEV;
1548 	}
1549 
1550 	if (ir_parse_ioapic_hpet_scope(dmaru->hdr, iommu)) {
1551 		pr_warn("DRHD %Lx: failed to parse managed IOAPIC/HPET\n",
1552 			iommu->reg_phys);
1553 		return -ENODEV;
1554 	}
1555 
1556 	/* TODO: check all IOAPICs are covered by IOMMU */
1557 
1558 	/* Setup Interrupt-remapping now. */
1559 	ret = intel_setup_irq_remapping(iommu);
1560 	if (ret) {
1561 		pr_err("Failed to setup irq remapping for %s\n",
1562 		       iommu->name);
1563 		intel_teardown_irq_remapping(iommu);
1564 		ir_remove_ioapic_hpet_scope(iommu);
1565 	} else {
1566 		iommu_enable_irq_remapping(iommu);
1567 	}
1568 
1569 	return ret;
1570 }
1571 
dmar_ir_hotplug(struct dmar_drhd_unit * dmaru,bool insert)1572 int dmar_ir_hotplug(struct dmar_drhd_unit *dmaru, bool insert)
1573 {
1574 	int ret = 0;
1575 	struct intel_iommu *iommu = dmaru->iommu;
1576 
1577 	if (!irq_remapping_enabled)
1578 		return 0;
1579 	if (iommu == NULL)
1580 		return -EINVAL;
1581 	if (!ecap_ir_support(iommu->ecap))
1582 		return 0;
1583 	if (irq_remapping_cap(IRQ_POSTING_CAP) &&
1584 	    !cap_pi_support(iommu->cap))
1585 		return -EBUSY;
1586 
1587 	if (insert) {
1588 		if (!iommu->ir_table)
1589 			ret = dmar_ir_add(dmaru, iommu);
1590 	} else {
1591 		if (iommu->ir_table) {
1592 			if (!bitmap_empty(iommu->ir_table->bitmap,
1593 					  INTR_REMAP_TABLE_ENTRIES)) {
1594 				ret = -EBUSY;
1595 			} else {
1596 				iommu_disable_irq_remapping(iommu);
1597 				intel_teardown_irq_remapping(iommu);
1598 				ir_remove_ioapic_hpet_scope(iommu);
1599 			}
1600 		}
1601 	}
1602 
1603 	return ret;
1604 }
1605