]> rtime.felk.cvut.cz Git - jailhouse.git/blob - hypervisor/arch/x86/vtd.c
x86: Migrate VT-d interrupt remapping emulation to generic MMIO dispatcher
[jailhouse.git] / hypervisor / arch / x86 / vtd.c
1 /*
2  * Jailhouse, a Linux-based partitioning hypervisor
3  *
4  * Copyright (c) Siemens AG, 2013-2015
5  * Copyright (c) Valentine Sinitsyn, 2014
6  *
7  * Authors:
8  *  Jan Kiszka <jan.kiszka@siemens.com>
9  *  Valentine Sinitsyn <valentine.sinitsyn@gmail.com>
10  *
11  * This work is licensed under the terms of the GNU GPL, version 2.  See
12  * the COPYING file in the top-level directory.
13  */
14
15 #include <jailhouse/control.h>
16 #include <jailhouse/mmio.h>
17 #include <jailhouse/paging.h>
18 #include <jailhouse/pci.h>
19 #include <jailhouse/printk.h>
20 #include <jailhouse/string.h>
21 #include <asm/vtd.h>
22 #include <asm/apic.h>
23 #include <asm/iommu.h>
24 #include <asm/bitops.h>
25 #include <asm/ioapic.h>
26 #include <asm/spinlock.h>
27 #include <asm/vtd.h>
28
29 struct vtd_irte_usage {
30         u16 device_id;
31         u16 vector:10,
32             used:1;
33 } __attribute__((packed));
34
35 struct vtd_emulation {
36         u64 irta;
37         unsigned int irt_entries;
38         struct vtd_irte_usage *irte_map;
39
40         u64 iqa;
41         u16 iqh;
42
43         u32 fectl;
44         u32 fedata;
45         u32 feaddr, feuaddr;
46 };
47
48 static const struct vtd_entry inv_global_context = {
49         .lo_word = VTD_REQ_INV_CONTEXT | VTD_INV_CONTEXT_GLOBAL,
50 };
51 static const struct vtd_entry inv_global_iotlb = {
52         .lo_word = VTD_REQ_INV_IOTLB | VTD_INV_IOTLB_GLOBAL |
53                 VTD_INV_IOTLB_DW | VTD_INV_IOTLB_DR,
54 };
55 static const struct vtd_entry inv_global_int = {
56         .lo_word = VTD_REQ_INV_INT | VTD_INV_INT_GLOBAL,
57 };
58
59 /* TODO: Support multiple segments */
60 static struct vtd_entry __attribute__((aligned(PAGE_SIZE)))
61         root_entry_table[256];
62 static union vtd_irte *int_remap_table;
63 static unsigned int int_remap_table_size_log2;
64 static struct paging vtd_paging[VTD_MAX_PAGE_TABLE_LEVELS];
65 static void *dmar_reg_base;
66 static void *unit_inv_queue;
67 static unsigned int dmar_units;
68 static unsigned int dmar_pt_levels;
69 static unsigned int dmar_num_did = ~0U;
70 static unsigned int fault_reporting_cpu_id;
71 static DEFINE_SPINLOCK(inv_queue_lock);
72 static struct vtd_emulation root_cell_units[JAILHOUSE_MAX_IOMMU_UNITS];
73 static bool dmar_units_initialized;
74
75 unsigned int iommu_mmio_count_regions(struct cell *cell)
76 {
77         return cell == &root_cell ? iommu_count_units() : 0;
78 }
79
80 static unsigned int inv_queue_write(void *inv_queue, unsigned int index,
81                                     struct vtd_entry content)
82 {
83         struct vtd_entry *entry = inv_queue;
84
85         entry[index] = content;
86         arch_paging_flush_cpu_caches(&entry[index], sizeof(*entry));
87
88         return (index + 1) % (PAGE_SIZE / sizeof(*entry));
89 }
90
91 static void vtd_submit_iq_request(void *reg_base, void *inv_queue,
92                                   const struct vtd_entry *inv_request)
93 {
94         volatile u32 completed = 0;
95         struct vtd_entry inv_wait = {
96                 .lo_word = VTD_REQ_INV_WAIT | VTD_INV_WAIT_SW |
97                         VTD_INV_WAIT_FN | (1UL << VTD_INV_WAIT_SDATA_SHIFT),
98                 .hi_word = paging_hvirt2phys(&completed),
99         };
100         unsigned int index;
101
102         spin_lock(&inv_queue_lock);
103
104         index = mmio_read64_field(reg_base + VTD_IQT_REG, VTD_IQT_QT_MASK);
105
106         if (inv_request)
107                 index = inv_queue_write(inv_queue, index, *inv_request);
108         index = inv_queue_write(inv_queue, index, inv_wait);
109
110         mmio_write64_field(reg_base + VTD_IQT_REG, VTD_IQT_QT_MASK, index);
111
112         while (!completed)
113                 cpu_relax();
114
115         spin_unlock(&inv_queue_lock);
116 }
117
118 static void vtd_flush_domain_caches(unsigned int did)
119 {
120         const struct vtd_entry inv_context = {
121                 .lo_word = VTD_REQ_INV_CONTEXT | VTD_INV_CONTEXT_DOMAIN |
122                         (did << VTD_INV_CONTEXT_DOMAIN_SHIFT),
123         };
124         const struct vtd_entry inv_iotlb = {
125                 .lo_word = VTD_REQ_INV_IOTLB | VTD_INV_IOTLB_DOMAIN |
126                         VTD_INV_IOTLB_DW | VTD_INV_IOTLB_DR |
127                         (did << VTD_INV_IOTLB_DOMAIN_SHIFT),
128         };
129         void *inv_queue = unit_inv_queue;
130         void *reg_base = dmar_reg_base;
131         unsigned int n;
132
133         for (n = 0; n < dmar_units; n++) {
134                 vtd_submit_iq_request(reg_base, inv_queue, &inv_context);
135                 vtd_submit_iq_request(reg_base, inv_queue, &inv_iotlb);
136                 reg_base += PAGE_SIZE;
137                 inv_queue += PAGE_SIZE;
138         }
139 }
140
141 static void vtd_update_gcmd_reg(void *reg_base, u32 mask, unsigned int set)
142 {
143         u32 val = mmio_read32(reg_base + VTD_GSTS_REG) & VTD_GSTS_USED_CTRLS;
144
145         if (set)
146                 val |= mask;
147         else
148                 val &= ~mask;
149         mmio_write32(reg_base + VTD_GCMD_REG, val);
150
151         /* Note: This test is built on the fact related bits are at the same
152          * position in VTD_GCMD_REG and VTD_GSTS_REG. */
153         while ((mmio_read32(reg_base + VTD_GSTS_REG) & mask) != (val & mask))
154                 cpu_relax();
155 }
156
157 static void vtd_set_next_pt(pt_entry_t pte, unsigned long next_pt)
158 {
159         *pte = (next_pt & 0x000ffffffffff000UL) | VTD_PAGE_READ |
160                 VTD_PAGE_WRITE;
161 }
162
163 static void vtd_init_fault_nmi(void)
164 {
165         union x86_msi_vector msi = { .native.address = MSI_ADDRESS_VALUE };
166         void *reg_base = dmar_reg_base;
167         struct per_cpu *cpu_data;
168         unsigned int n;
169
170         /* This assumes that at least one bit is set somewhere because we
171          * don't support configurations where Linux is left with no CPUs. */
172         for (n = 0; root_cell.cpu_set->bitmap[n] == 0; n++)
173                 /* Empty loop */;
174         cpu_data = per_cpu(ffsl(root_cell.cpu_set->bitmap[n]));
175
176         /* We only support 8-bit APIC IDs. */
177         msi.native.destination = (u8)cpu_data->apic_id;
178
179         /* Save this value globally to avoid multiple reports of the same
180          * case from different CPUs */
181         fault_reporting_cpu_id = cpu_data->cpu_id;
182
183         for (n = 0; n < dmar_units; n++, reg_base += PAGE_SIZE) {
184                 /* Mask events */
185                 mmio_write32_field(reg_base + VTD_FECTL_REG, VTD_FECTL_IM, 1);
186
187                 /*
188                  * VT-d spec rev. 2.3 section 7.4 suggests that only reading
189                  * back FSTS or FECTL ensures no interrupt messages are still
190                  * in-flight when we change their destination below.
191                  */
192                 mmio_read32(reg_base + VTD_FECTL_REG);
193
194                 /* Program MSI message to send NMIs to the target CPU */
195                 mmio_write32(reg_base + VTD_FEDATA_REG, MSI_DM_NMI);
196                 mmio_write32(reg_base + VTD_FEADDR_REG, (u32)msi.raw.address);
197                 mmio_write32(reg_base + VTD_FEUADDR_REG, 0);
198
199                 /* Unmask events */
200                 mmio_write32_field(reg_base + VTD_FECTL_REG, VTD_FECTL_IM, 0);
201         }
202
203         /*
204          * There is a race window between setting the new reporting CPU ID and
205          * updating the target programming in the register. If a fault hits us
206          * in this window and no other NMIs arrive after that, the event will
207          * not be reported. Address this by triggering an NMI on the new
208          * reporting CPU.
209          */
210         apic_send_nmi_ipi(cpu_data);
211 }
212
213 static void *vtd_get_fault_rec_reg_addr(void *reg_base)
214 {
215         return reg_base + 16 *
216                 mmio_read64_field(reg_base + VTD_CAP_REG, VTD_CAP_FRO_MASK);
217 }
218
219 static void vtd_print_fault_record_reg_status(unsigned int unit_no,
220                                               void *reg_base)
221 {
222         unsigned int sid = mmio_read64_field(reg_base + VTD_FRCD_HI_REG,
223                                              VTD_FRCD_HI_SID_MASK);
224         unsigned int fr = mmio_read64_field(reg_base + VTD_FRCD_HI_REG,
225                                             VTD_FRCD_HI_FR_MASK);
226         unsigned long fi = mmio_read64_field(reg_base + VTD_FRCD_LO_REG,
227                                              VTD_FRCD_LO_FI_MASK);
228         unsigned int type = mmio_read64_field(reg_base + VTD_FRCD_HI_REG,
229                                               VTD_FRCD_HI_TYPE);
230
231         printk("VT-d fault event reported by IOMMU %d:\n", unit_no);
232         printk(" Source Identifier (bus:dev.func): %02x:%02x.%x\n",
233                PCI_BDF_PARAMS(sid));
234         printk(" Fault Reason: 0x%x Fault Info: %lx Type %d\n", fr, fi, type);
235 }
236
237 void iommu_check_pending_faults(void)
238 {
239         unsigned int fr_index;
240         void *reg_base = dmar_reg_base;
241         unsigned int n;
242         void *fault_reg_addr, *rec_reg_addr;
243
244         if (this_cpu_id() != fault_reporting_cpu_id)
245                 return;
246
247         for (n = 0; n < dmar_units; n++, reg_base += PAGE_SIZE)
248                 if (mmio_read32_field(reg_base + VTD_FSTS_REG, VTD_FSTS_PPF)) {
249                         fr_index = mmio_read32_field(reg_base + VTD_FSTS_REG,
250                                                      VTD_FSTS_FRI_MASK);
251                         fault_reg_addr = vtd_get_fault_rec_reg_addr(reg_base);
252                         rec_reg_addr = fault_reg_addr + 16 * fr_index;
253                         vtd_print_fault_record_reg_status(n, rec_reg_addr);
254
255                         /* Clear faults in record registers */
256                         mmio_write64_field(rec_reg_addr + VTD_FRCD_HI_REG,
257                                            VTD_FRCD_HI_F, VTD_FRCD_HI_F_CLEAR);
258                 }
259 }
260
261 static int vtd_emulate_inv_int(unsigned int unit_no, unsigned int index)
262 {
263         struct vtd_irte_usage *irte_usage;
264         struct apic_irq_message irq_msg;
265         struct pci_device *device;
266
267         if (index >= root_cell_units[unit_no].irt_entries)
268                 return 0;
269         irte_usage = &root_cell_units[unit_no].irte_map[index];
270         if (!irte_usage->used)
271                 return 0;
272
273         device = pci_get_assigned_device(&root_cell, irte_usage->device_id);
274         if (device && device->info->type == JAILHOUSE_PCI_TYPE_IVSHMEM)
275                 return pci_ivshmem_update_msix(device);
276
277         irq_msg = iommu_get_remapped_root_int(unit_no, irte_usage->device_id,
278                                               irte_usage->vector, index);
279         return iommu_map_interrupt(&root_cell, irte_usage->device_id,
280                                    irte_usage->vector, irq_msg);
281 }
282
283 static int vtd_emulate_qi_request(unsigned int unit_no,
284                                   struct vtd_entry inv_desc)
285 {
286         unsigned int start, count, n;
287         void *status_page;
288         int err;
289
290         switch (inv_desc.lo_word & VTD_REQ_INV_MASK) {
291         case VTD_REQ_INV_INT:
292                 if (inv_desc.lo_word & VTD_INV_INT_INDEX) {
293                         start = (inv_desc.lo_word & VTD_INV_INT_IIDX_MASK) >>
294                                 VTD_INV_INT_IIDX_SHIFT;
295                         count =
296                             1 << ((inv_desc.lo_word & VTD_INV_INT_IM_MASK) >>
297                                   VTD_INV_INT_IM_SHIFT);
298                 } else {
299                         start = 0;
300                         count = root_cell_units[unit_no].irt_entries;
301                 }
302                 for (n = start; n < start + count; n++) {
303                         err = vtd_emulate_inv_int(unit_no, n);
304                         if (err < 0)
305                                 return err;
306                 }
307                 return 0;
308         case VTD_REQ_INV_WAIT:
309                 if (inv_desc.lo_word & VTD_INV_WAIT_IF ||
310                     !(inv_desc.lo_word & VTD_INV_WAIT_SW))
311                         return -EINVAL;
312
313                 status_page = paging_get_guest_pages(NULL, inv_desc.hi_word, 1,
314                                                      PAGE_DEFAULT_FLAGS);
315                 if (!status_page)
316                         return -EINVAL;
317
318                 *(u32 *)(status_page + (inv_desc.hi_word & ~PAGE_MASK)) =
319                         inv_desc.lo_word >> 32;
320
321                 return 0;
322         }
323         return -EINVAL;
324 }
325
326 static enum mmio_result vtd_unit_access_handler(void *arg,
327                                                 struct mmio_access *mmio)
328 {
329         struct vtd_emulation *unit = arg;
330         unsigned int unit_no = unit - root_cell_units;
331         struct vtd_entry inv_desc;
332         void *inv_desc_page;
333
334         if (mmio->address == VTD_FSTS_REG && !mmio->is_write) {
335                 /*
336                  * Nothing to report this way, vtd_check_pending_faults takes
337                  * care for the whole system.
338                  */
339                 mmio->value = 0;
340                 return MMIO_HANDLED;
341         }
342         if (mmio->address == VTD_IQT_REG && mmio->is_write) {
343                 while (unit->iqh != (mmio->value & ~PAGE_MASK)) {
344                         inv_desc_page =
345                                 paging_get_guest_pages(NULL, unit->iqa, 1,
346                                                        PAGE_READONLY_FLAGS);
347                         if (!inv_desc_page)
348                                 goto invalid_iq_entry;
349
350                         inv_desc =
351                             *(struct vtd_entry *)(inv_desc_page + unit->iqh);
352
353                         if (vtd_emulate_qi_request(unit_no, inv_desc) != 0)
354                                 goto invalid_iq_entry;
355
356                         unit->iqh += 1 << VTD_IQH_QH_SHIFT;
357                         unit->iqh &= ~PAGE_MASK;
358                 }
359                 return MMIO_HANDLED;
360         }
361         panic_printk("FATAL: Unhandled DMAR unit %s access, register %02x\n",
362                      mmio->is_write ? "write" : "read", mmio->address);
363         return MMIO_ERROR;
364
365 invalid_iq_entry:
366         panic_printk("FATAL: Invalid/unsupported invalidation queue entry\n");
367         return -1;
368 }
369
370 static void vtd_init_unit(void *reg_base, void *inv_queue)
371 {
372         void *fault_reg_base;
373         unsigned int nfr, n;
374
375         /* Disabled QI and IR in case it was already on */
376         vtd_update_gcmd_reg(reg_base, VTD_GCMD_QIE, 0);
377         vtd_update_gcmd_reg(reg_base, VTD_GCMD_IRE, 0);
378
379         nfr = mmio_read64_field(reg_base + VTD_CAP_REG, VTD_CAP_NFR_MASK);
380         fault_reg_base = vtd_get_fault_rec_reg_addr(reg_base);
381
382         for (n = 0; n < nfr; n++)
383                 /* Clear fault recording register status */
384                 mmio_write64_field(fault_reg_base + 16 * n + VTD_FRCD_HI_REG,
385                                    VTD_FRCD_HI_F, VTD_FRCD_HI_F_CLEAR);
386
387         /* Clear fault overflow status */
388         mmio_write32_field(reg_base + VTD_FSTS_REG, VTD_FSTS_PFO,
389                            VTD_FSTS_PFO_CLEAR);
390
391         /* Set root entry table pointer */
392         mmio_write64(reg_base + VTD_RTADDR_REG,
393                      paging_hvirt2phys(root_entry_table));
394         vtd_update_gcmd_reg(reg_base, VTD_GCMD_SRTP, 1);
395
396         /* Set interrupt remapping table pointer */
397         mmio_write64(reg_base + VTD_IRTA_REG,
398                      paging_hvirt2phys(int_remap_table) |
399                      (using_x2apic ? VTD_IRTA_EIME : 0) |
400                      (int_remap_table_size_log2 - 1));
401         vtd_update_gcmd_reg(reg_base, VTD_GCMD_SIRTP, 1);
402
403         /* Setup and activate invalidation queue */
404         mmio_write64(reg_base + VTD_IQT_REG, 0);
405         mmio_write64(reg_base + VTD_IQA_REG, paging_hvirt2phys(inv_queue));
406         vtd_update_gcmd_reg(reg_base, VTD_GCMD_QIE, 1);
407
408         vtd_submit_iq_request(reg_base, inv_queue, &inv_global_context);
409         vtd_submit_iq_request(reg_base, inv_queue, &inv_global_iotlb);
410         vtd_submit_iq_request(reg_base, inv_queue, &inv_global_int);
411
412         vtd_update_gcmd_reg(reg_base, VTD_GCMD_TE, 1);
413         vtd_update_gcmd_reg(reg_base, VTD_GCMD_IRE, 1);
414 }
415
416 static int vtd_init_ir_emulation(unsigned int unit_no, void *reg_base)
417 {
418         struct vtd_emulation *unit = &root_cell_units[unit_no];
419         unsigned long base, size;
420         u64 iqt;
421
422         root_cell.arch.vtd.ir_emulation = true;
423
424         base = system_config->platform_info.x86.iommu_base[unit_no];
425         mmio_region_register(&root_cell, base, PAGE_SIZE,
426                              vtd_unit_access_handler, unit);
427
428         unit->irta = mmio_read64(reg_base + VTD_IRTA_REG);
429         unit->irt_entries = 2 << (unit->irta & VTD_IRTA_SIZE_MASK);
430
431         size = PAGE_ALIGN(sizeof(struct vtd_irte_usage) * unit->irt_entries);
432         unit->irte_map = page_alloc(&mem_pool, size / PAGE_SIZE);
433         if (!unit->irte_map)
434                 return -ENOMEM;
435
436         iqt = mmio_read64(reg_base + VTD_IQT_REG);
437         while (mmio_read64(reg_base + VTD_IQH_REG) != iqt)
438                 cpu_relax();
439         unit->iqh = iqt;
440
441         unit->iqa = mmio_read64(reg_base + VTD_IQA_REG);
442         if (unit->iqa & ~VTD_IQA_ADDR_MASK)
443                 return trace_error(-EIO);
444
445         unit->fectl = mmio_read32(reg_base + VTD_FECTL_REG);
446         unit->fedata = mmio_read32(reg_base + VTD_FEDATA_REG);
447         unit->feaddr = mmio_read32(reg_base + VTD_FEADDR_REG);
448         unit->feuaddr = mmio_read32(reg_base + VTD_FEUADDR_REG);
449
450         return 0;
451 }
452
453 int iommu_init(void)
454 {
455         unsigned long version, caps, ecaps, ctrls, sllps_caps = ~0UL;
456         unsigned int units, pt_levels, num_did, n;
457         void *reg_base;
458         u64 base_addr;
459         int err;
460
461         /* n = roundup(log2(system_config->interrupt_limit)) */
462         for (n = 0; (1UL << n) < (system_config->interrupt_limit); n++)
463                 ; /* empty loop */
464         if (n >= 16)
465                 return trace_error(-EINVAL);
466
467         int_remap_table =
468                 page_alloc(&mem_pool, PAGES(sizeof(union vtd_irte) << n));
469         if (!int_remap_table)
470                 return -ENOMEM;
471
472         int_remap_table_size_log2 = n;
473
474         units = iommu_count_units();
475         if (units == 0)
476                 return trace_error(-EINVAL);
477
478         dmar_reg_base = page_alloc(&remap_pool, units);
479         if (!dmar_reg_base)
480                 return trace_error(-ENOMEM);
481
482         unit_inv_queue = page_alloc(&mem_pool, units);
483         if (!unit_inv_queue)
484                 return -ENOMEM;
485
486         for (n = 0; n < units; n++) {
487                 base_addr = system_config->platform_info.x86.iommu_base[n];
488
489                 reg_base = dmar_reg_base + n * PAGE_SIZE;
490
491                 err = paging_create(&hv_paging_structs, base_addr, PAGE_SIZE,
492                                     (unsigned long)reg_base,
493                                     PAGE_DEFAULT_FLAGS | PAGE_FLAG_DEVICE,
494                                     PAGING_NON_COHERENT);
495                 if (err)
496                         return err;
497
498                 version = mmio_read64(reg_base + VTD_VER_REG) & VTD_VER_MASK;
499                 if (version < VTD_VER_MIN || version == 0xff) {
500                         //return -EIO;
501                         // HACK for QEMU
502                         printk("WARNING: No VT-d support found!\n");
503                         return 0;
504                 }
505
506                 printk("Found DMAR @%p\n", base_addr);
507
508                 caps = mmio_read64(reg_base + VTD_CAP_REG);
509                 if (caps & VTD_CAP_SAGAW39)
510                         pt_levels = 3;
511                 else if (caps & VTD_CAP_SAGAW48)
512                         pt_levels = 4;
513                 else
514                         return trace_error(-EIO);
515                 sllps_caps &= caps;
516
517                 if (dmar_pt_levels > 0 && dmar_pt_levels != pt_levels)
518                         return trace_error(-EIO);
519                 dmar_pt_levels = pt_levels;
520
521                 if (caps & VTD_CAP_CM)
522                         return trace_error(-EIO);
523
524                 ecaps = mmio_read64(reg_base + VTD_ECAP_REG);
525                 if (!(ecaps & VTD_ECAP_QI) || !(ecaps & VTD_ECAP_IR) ||
526                     (using_x2apic && !(ecaps & VTD_ECAP_EIM)))
527                         return trace_error(-EIO);
528
529                 ctrls = mmio_read32(reg_base + VTD_GSTS_REG) &
530                         VTD_GSTS_USED_CTRLS;
531                 if (ctrls != 0) {
532                         if (ctrls != (VTD_GSTS_IRES | VTD_GSTS_QIES))
533                                 return trace_error(-EBUSY);
534                         err = vtd_init_ir_emulation(n, reg_base);
535                         if (err)
536                                 return err;
537                 } else if (root_cell.arch.vtd.ir_emulation) {
538                         /* IR+QI must be either on or off in all units */
539                         return trace_error(-EIO);
540                 }
541
542                 num_did = 1 << (4 + (caps & VTD_CAP_NUM_DID_MASK) * 2);
543                 if (num_did < dmar_num_did)
544                         dmar_num_did = num_did;
545         }
546
547         dmar_units = units;
548
549         /*
550          * Derive vdt_paging from very similar x86_64_paging,
551          * replicating 0..3 for 4 levels and 1..3 for 3 levels.
552          */
553         memcpy(vtd_paging, &x86_64_paging[4 - dmar_pt_levels],
554                sizeof(struct paging) * dmar_pt_levels);
555         for (n = 0; n < dmar_pt_levels; n++)
556                 vtd_paging[n].set_next_pt = vtd_set_next_pt;
557         if (!(sllps_caps & VTD_CAP_SLLPS1G))
558                 vtd_paging[dmar_pt_levels - 3].page_size = 0;
559         if (!(sllps_caps & VTD_CAP_SLLPS2M))
560                 vtd_paging[dmar_pt_levels - 2].page_size = 0;
561
562         return iommu_cell_init(&root_cell);
563 }
564
565 static void vtd_update_irte(unsigned int index, union vtd_irte content)
566 {
567         const struct vtd_entry inv_int = {
568                 .lo_word = VTD_REQ_INV_INT | VTD_INV_INT_INDEX |
569                         ((u64)index << VTD_INV_INT_IIDX_SHIFT),
570         };
571         union vtd_irte *irte = &int_remap_table[index];
572         void *inv_queue = unit_inv_queue;
573         void *reg_base = dmar_reg_base;
574         unsigned int n;
575
576         if (content.field.p) {
577                 /*
578                  * Write upper half first to preserve non-presence.
579                  * If the entry was present before, we are only modifying the
580                  * lower half's content (destination etc.), so writing the
581                  * upper half becomes a nop and is safely done first.
582                  */
583                 irte->raw[1] = content.raw[1];
584                 memory_barrier();
585                 irte->raw[0] = content.raw[0];
586         } else {
587                 /*
588                  * Write only lower half - we are clearing presence and
589                  * assignment.
590                  */
591                 irte->raw[0] = content.raw[0];
592         }
593         arch_paging_flush_cpu_caches(irte, sizeof(*irte));
594
595         for (n = 0; n < dmar_units; n++) {
596                 vtd_submit_iq_request(reg_base, inv_queue, &inv_int);
597                 reg_base += PAGE_SIZE;
598                 inv_queue += PAGE_SIZE;
599         }
600 }
601
602 static int vtd_find_int_remap_region(u16 device_id)
603 {
604         int n;
605
606         /* interrupt_limit is < 2^16, see vtd_init */
607         for (n = 0; n < system_config->interrupt_limit; n++)
608                 if (int_remap_table[n].field.assigned &&
609                     int_remap_table[n].field.sid == device_id)
610                         return n;
611
612         return -ENOENT;
613 }
614
615 static int vtd_reserve_int_remap_region(u16 device_id, unsigned int length)
616 {
617         int n, start = -E2BIG;
618
619         if (length == 0 || vtd_find_int_remap_region(device_id) >= 0)
620                 return 0;
621
622         for (n = 0; n < system_config->interrupt_limit; n++) {
623                 if (int_remap_table[n].field.assigned) {
624                         start = -E2BIG;
625                         continue;
626                 }
627                 if (start < 0)
628                         start = n;
629                 if (n + 1 == start + length) {
630                         printk("Reserving %u interrupt(s) for device %04x "
631                                "at index %d\n", length, device_id, start);
632                         for (n = start; n < start + length; n++) {
633                                 int_remap_table[n].field.assigned = 1;
634                                 int_remap_table[n].field.sid = device_id;
635                         }
636                         return start;
637                 }
638         }
639         return trace_error(-E2BIG);
640 }
641
642 static void vtd_free_int_remap_region(u16 device_id, unsigned int length)
643 {
644         union vtd_irte free_irte = { .field.p = 0, .field.assigned = 0 };
645         int pos = vtd_find_int_remap_region(device_id);
646
647         if (pos >= 0) {
648                 printk("Freeing %u interrupt(s) for device %04x at index %d\n",
649                        length, device_id, pos);
650                 while (length-- > 0)
651                         vtd_update_irte(pos++, free_irte);
652         }
653 }
654
655 int iommu_add_pci_device(struct cell *cell, struct pci_device *device)
656 {
657         unsigned int max_vectors = MAX(device->info->num_msi_vectors,
658                                        device->info->num_msix_vectors);
659         u16 bdf = device->info->bdf;
660         u64 *root_entry_lo = &root_entry_table[PCI_BUS(bdf)].lo_word;
661         struct vtd_entry *context_entry_table, *context_entry;
662         int result;
663
664         // HACK for QEMU
665         if (dmar_units == 0)
666                 return 0;
667
668         result = vtd_reserve_int_remap_region(bdf, max_vectors);
669         if (result < 0)
670                 return result;
671
672         if (*root_entry_lo & VTD_ROOT_PRESENT) {
673                 context_entry_table =
674                         paging_phys2hvirt(*root_entry_lo & PAGE_MASK);
675         } else {
676                 context_entry_table = page_alloc(&mem_pool, 1);
677                 if (!context_entry_table)
678                         goto error_nomem;
679                 *root_entry_lo = VTD_ROOT_PRESENT |
680                         paging_hvirt2phys(context_entry_table);
681                 arch_paging_flush_cpu_caches(root_entry_lo, sizeof(u64));
682         }
683
684         context_entry = &context_entry_table[PCI_DEVFN(bdf)];
685         context_entry->lo_word = VTD_CTX_PRESENT | VTD_CTX_TTYPE_MLP_UNTRANS |
686                 paging_hvirt2phys(cell->arch.vtd.pg_structs.root_table);
687         context_entry->hi_word =
688                 (dmar_pt_levels == 3 ? VTD_CTX_AGAW_39 : VTD_CTX_AGAW_48) |
689                 (cell->id << VTD_CTX_DID_SHIFT);
690         arch_paging_flush_cpu_caches(context_entry, sizeof(*context_entry));
691
692         return 0;
693
694 error_nomem:
695         vtd_free_int_remap_region(bdf, max_vectors);
696         return -ENOMEM;
697 }
698
699 void iommu_remove_pci_device(struct pci_device *device)
700 {
701         u16 bdf = device->info->bdf;
702         u64 *root_entry_lo = &root_entry_table[PCI_BUS(bdf)].lo_word;
703         struct vtd_entry *context_entry_table;
704         struct vtd_entry *context_entry;
705         unsigned int n;
706
707         // HACK for QEMU
708         if (dmar_units == 0)
709                 return;
710
711         vtd_free_int_remap_region(bdf, MAX(device->info->num_msi_vectors,
712                                            device->info->num_msix_vectors));
713
714         context_entry_table = paging_phys2hvirt(*root_entry_lo & PAGE_MASK);
715         context_entry = &context_entry_table[PCI_DEVFN(bdf)];
716
717         context_entry->lo_word &= ~VTD_CTX_PRESENT;
718         arch_paging_flush_cpu_caches(&context_entry->lo_word, sizeof(u64));
719
720         for (n = 0; n < 256; n++)
721                 if (context_entry_table[n].lo_word & VTD_CTX_PRESENT)
722                         return;
723
724         *root_entry_lo &= ~VTD_ROOT_PRESENT;
725         arch_paging_flush_cpu_caches(root_entry_lo, sizeof(u64));
726         page_free(&mem_pool, context_entry_table, 1);
727 }
728
729 int iommu_cell_init(struct cell *cell)
730 {
731         const struct jailhouse_irqchip *irqchip =
732                 jailhouse_cell_irqchips(cell->config);
733         unsigned int n;
734         int result;
735
736         // HACK for QEMU
737         if (dmar_units == 0)
738                 return 0;
739
740         if (cell->id >= dmar_num_did)
741                 return trace_error(-ERANGE);
742
743         cell->arch.vtd.pg_structs.root_paging = vtd_paging;
744         cell->arch.vtd.pg_structs.root_table = page_alloc(&mem_pool, 1);
745         if (!cell->arch.vtd.pg_structs.root_table)
746                 return -ENOMEM;
747
748         /* reserve regions for IRQ chips (if not done already) */
749         for (n = 0; n < cell->config->num_irqchips; n++, irqchip++) {
750                 result = vtd_reserve_int_remap_region(irqchip->id,
751                                                       IOAPIC_NUM_PINS);
752                 if (result < 0) {
753                         iommu_cell_exit(cell);
754                         return result;
755                 }
756         }
757
758         return 0;
759 }
760
761 int iommu_map_memory_region(struct cell *cell,
762                             const struct jailhouse_memory *mem)
763 {
764         u32 flags = 0;
765
766         // HACK for QEMU
767         if (dmar_units == 0)
768                 return 0;
769
770         if (!(mem->flags & JAILHOUSE_MEM_DMA))
771                 return 0;
772
773         if (mem->flags & JAILHOUSE_MEM_READ)
774                 flags |= VTD_PAGE_READ;
775         if (mem->flags & JAILHOUSE_MEM_WRITE)
776                 flags |= VTD_PAGE_WRITE;
777
778         return paging_create(&cell->arch.vtd.pg_structs, mem->phys_start,
779                              mem->size, mem->virt_start, flags,
780                              PAGING_COHERENT);
781 }
782
783 int iommu_unmap_memory_region(struct cell *cell,
784                               const struct jailhouse_memory *mem)
785 {
786         // HACK for QEMU
787         if (dmar_units == 0)
788                 return 0;
789
790         if (!(mem->flags & JAILHOUSE_MEM_DMA))
791                 return 0;
792
793         return paging_destroy(&cell->arch.vtd.pg_structs, mem->virt_start,
794                               mem->size, PAGING_COHERENT);
795 }
796
797 struct apic_irq_message
798 iommu_get_remapped_root_int(unsigned int iommu, u16 device_id,
799                             unsigned int vector, unsigned int remap_index)
800 {
801         struct vtd_emulation *unit = &root_cell_units[iommu];
802         struct apic_irq_message irq_msg = { .valid = 0 };
803         union vtd_irte root_irte;
804         unsigned long irte_addr;
805         void *irte_page;
806
807         if (remap_index >= unit->irt_entries)
808                 return irq_msg;
809         unit->irte_map[remap_index].used = 0;
810
811         irte_addr = (unit->irta & VTD_IRTA_ADDR_MASK) +
812                 remap_index * sizeof(union vtd_irte);
813         irte_page = paging_get_guest_pages(NULL, irte_addr, 1,
814                                            PAGE_READONLY_FLAGS);
815         if (!irte_page)
816                 return irq_msg;
817
818         root_irte = *(union vtd_irte *)(irte_page + (irte_addr & ~PAGE_MASK));
819
820         irq_msg.valid =
821                 (root_irte.field.p && root_irte.field.sid == device_id);
822         irq_msg.vector = root_irte.field.vector;
823         irq_msg.delivery_mode = root_irte.field.delivery_mode;
824         irq_msg.dest_logical = root_irte.field.dest_logical;
825         irq_msg.level_triggered = root_irte.field.level_triggered;
826         irq_msg.redir_hint = root_irte.field.redir_hint;
827         irq_msg.destination = root_irte.field.destination;
828         if (!using_x2apic)
829                 /* xAPIC in flat mode: APIC ID in 47:40 (of 63:32) */
830                 irq_msg.destination >>= 8;
831
832         unit->irte_map[remap_index].device_id = device_id;
833         unit->irte_map[remap_index].vector = vector;
834         unit->irte_map[remap_index].used = 1;
835
836         return irq_msg;
837 }
838
839 int iommu_map_interrupt(struct cell *cell, u16 device_id, unsigned int vector,
840                         struct apic_irq_message irq_msg)
841 {
842         union vtd_irte irte;
843         int base_index;
844
845         // HACK for QEMU
846         if (dmar_units == 0)
847                 return -ENOSYS;
848
849         base_index = vtd_find_int_remap_region(device_id);
850         if (base_index < 0)
851                 return base_index;
852
853         if (vector >= system_config->interrupt_limit ||
854             base_index >= system_config->interrupt_limit - vector)
855                 return -ERANGE;
856
857         irte = int_remap_table[base_index + vector];
858         if (!irte.field.assigned || irte.field.sid != device_id)
859                 return -ERANGE;
860
861         irte.field.p = irq_msg.valid;
862         if (!irte.field.p)
863                 /*
864                  * Do not validate non-present entries, they may contain
865                  * invalid data and cause false-positives.
866                  */
867                 goto update_irte;
868
869         /*
870          * Validate delivery mode and destination(s).
871          * Note that we do support redirection hint only in logical
872          * destination mode.
873          */
874         if ((irq_msg.delivery_mode != APIC_MSG_DLVR_FIXED &&
875              irq_msg.delivery_mode != APIC_MSG_DLVR_LOWPRI) ||
876             irq_msg.dest_logical != irq_msg.redir_hint)
877                 return -EINVAL;
878         if (!apic_filter_irq_dest(cell, &irq_msg))
879                 return -EPERM;
880
881         irte.field.dest_logical = irq_msg.dest_logical;
882         irte.field.redir_hint = irq_msg.redir_hint;
883         irte.field.level_triggered = irq_msg.level_triggered;
884         irte.field.delivery_mode = irq_msg.delivery_mode;
885         irte.field.vector = irq_msg.vector;
886         irte.field.destination = irq_msg.destination;
887         if (!using_x2apic)
888                 /* xAPIC in flat mode: APIC ID in 47:40 (of 63:32) */
889                 irte.field.destination <<= 8;
890         irte.field.sq = VTD_IRTE_SQ_VERIFY_FULL_SID;
891         irte.field.svt = VTD_IRTE_SVT_VERIFY_SID_SQ;
892
893 update_irte:
894         vtd_update_irte(base_index + vector, irte);
895
896         return base_index + vector;
897 }
898
899 void iommu_cell_exit(struct cell *cell)
900 {
901         // HACK for QEMU
902         if (dmar_units == 0)
903                 return;
904
905         page_free(&mem_pool, cell->arch.vtd.pg_structs.root_table, 1);
906
907         /*
908          * Note that reservation regions of IOAPICs won't be released because
909          * they might be shared with other cells
910          */
911 }
912
913 void iommu_config_commit(struct cell *cell_added_removed)
914 {
915         void *inv_queue = unit_inv_queue;
916         void *reg_base = dmar_reg_base;
917         int n;
918
919         // HACK for QEMU
920         if (dmar_units == 0)
921                 return;
922
923         if (cell_added_removed)
924                 vtd_init_fault_nmi();
925
926         if (cell_added_removed == &root_cell) {
927                 for (n = 0; n < dmar_units; n++) {
928                         vtd_init_unit(reg_base, inv_queue);
929                         reg_base += PAGE_SIZE;
930                         inv_queue += PAGE_SIZE;
931                 }
932                 dmar_units_initialized = true;
933         } else {
934                 if (cell_added_removed)
935                         vtd_flush_domain_caches(cell_added_removed->id);
936                 vtd_flush_domain_caches(root_cell.id);
937         }
938 }
939
940 static void vtd_restore_ir(unsigned int unit_no, void *reg_base)
941 {
942         struct vtd_emulation *unit = &root_cell_units[unit_no];
943         void *inv_queue = unit_inv_queue + unit_no * PAGE_SIZE;
944         void *root_inv_queue;
945         u64 iqh;
946
947         mmio_write64(reg_base + VTD_IRTA_REG, unit->irta);
948         vtd_update_gcmd_reg(reg_base, VTD_GCMD_SIRTP, 1);
949         vtd_submit_iq_request(reg_base, inv_queue, &inv_global_int);
950
951         vtd_update_gcmd_reg(reg_base, VTD_GCMD_QIE, 0);
952         mmio_write64(reg_base + VTD_IQT_REG, 0);
953         mmio_write64(reg_base + VTD_IQA_REG, unit->iqa);
954         vtd_update_gcmd_reg(reg_base, VTD_GCMD_QIE, 1);
955
956         /*
957          * Restore invalidation queue head pointer by issuing dummy requests
958          * until the hardware is in sync with the Linux state again.
959          */
960         iqh =unit->iqh;
961         root_inv_queue = paging_get_guest_pages(NULL, unit->iqa, 1,
962                                                 PAGE_DEFAULT_FLAGS);
963         if (root_inv_queue)
964                 while (mmio_read64(reg_base + VTD_IQH_REG) != iqh)
965                         vtd_submit_iq_request(reg_base, root_inv_queue, NULL);
966         else
967                 printk("WARNING: Failed to restore invalidation queue head\n");
968
969         vtd_update_gcmd_reg(reg_base, VTD_GCMD_IRE, 1);
970
971         mmio_write32(reg_base + VTD_FEDATA_REG, unit->fedata);
972         mmio_write32(reg_base + VTD_FEADDR_REG, unit->feaddr);
973         mmio_write32(reg_base + VTD_FEUADDR_REG, unit->feuaddr);
974         mmio_write32(reg_base + VTD_FECTL_REG, unit->fectl);
975 }
976
977 void iommu_shutdown(void)
978 {
979         void *reg_base = dmar_reg_base;
980         unsigned int n;
981
982         if (dmar_units_initialized)
983                 for (n = 0; n < dmar_units; n++, reg_base += PAGE_SIZE) {
984                         vtd_update_gcmd_reg(reg_base, VTD_GCMD_TE, 0);
985                         vtd_update_gcmd_reg(reg_base, VTD_GCMD_IRE, 0);
986                         if (root_cell.arch.vtd.ir_emulation)
987                                 vtd_restore_ir(n, reg_base);
988                         else
989                                 vtd_update_gcmd_reg(reg_base, VTD_GCMD_QIE, 0);
990                 }
991 }
992
993 bool iommu_cell_emulates_ir(struct cell *cell)
994 {
995         return cell->arch.vtd.ir_emulation;
996 }