core: Introduce and use cell_owns_cpu

This helper combines the check for the maximum CPU number with the
consultation of the CPU set's bitmap. Both helps to make the code more
compact and avoid to forget the former test in the future.

We keep it inline as it generates quite a bit boilerplate code
otherwise, and IPI transmission depends on efficient execution.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Rework APIC_INVALID_ID to CPU_ID_INVALID

APIC_INVALID_ID is actually an invalid logical CPU ID. Rename the
constant and also ensure that no CPU is registered with this ID. That
way we can drop one extra test from apic_send_ipi as this ID will never
be included in any CPU set.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
core: Enforce DWORD-alignment of MMCONFIG accesses

We only support DWORDs, thus the access address has to be DWORD-aligned
to fulfill the PCI standard - and to match our handling logic.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Use unsigned counter in vtd_init_fault_nmi

Basically just a style cleanup.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
tools: config-create: make sure we are root when reading input files

File access permissions allow users and root to read PCI config spaces
of devices via sysfs. But depending on your uid you will read different
content.
That is why we have to make sure we open those files as root and can not
just rely on getting an EPERM.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
[Jan: removed extra blank line]
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
tools: config-create: more fine grained parsing of /proc/iomem

Parse /proc/iomem keeping the tree structure. So far we only looked at
the top level entries, mainly at the type strings. But for more fine
grained selection of memory regions we need to look deeper into the
tree.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
tools: config-create: fix shell template

Not every shell supports the "-ne" operator in its builtin test, use
string compare instead. Also $UID is not always set.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
tools: config-create: Exclude DMAR units from memory regions

The cells must not be able to access the physical units. We will emulate
what is required for interrupt remapping.

At this chance, clarify in a comment that the IOAPIC is also excluded.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
tools: config-create: Sort helper functions to the top

Separate them from all the specific parser functions. No functional
changes.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
tools: config-create: Simplify parse_dmar_devscope

We do not support complex device scope paths yet, code this into the
parsing function. This allows to simplify the call sites as well because
parse_dmar_devscope will now read as many bytes as supported or fail.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
tools: config-create: Initial support for non-Intel boards

DMAR is Intel-specific. For AMD, we need to parse IVRS table, but
this code is currently missing since there is no IOMMU support in
AMD-V port.

Initially, we just make DMAR optional and ignore possible errors
on AMD (Intel will still fail at signature check stage if DMAR is
missing, however the error message will be somewhat misleading).
The template was also modified so missing bits of information are
not included (and IOAPIC ID is set to zero).

Signed-off-by: Valentine Sinitsyn <valentine.sinitsyn@gmail.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
tools: config-create: Improve error reporting

In case an ACPI table file is broken, report what we expected, not only
what we got (can be empty).

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
tools: config-create: Remove write restriction for ACPI regions

We don't depend on ACPI in the hypervisor anymore. Also, the NVS memory
may contain some semaphore the kernel updates to synchronize with the
chipset-embedded controller. So let's give Linux full access to this
memory again.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
configs/tools: Add number of MSI vectors to PCI device config structure

This avoids having to scan in the hypervisor the MSI/MSI-X capabilities
regarding the maximum number of vectors a PCI device may use.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
configs/tools: Introduce device and interrupt number limit

VT-d interrupt remapping but also (one day) AMD IOMMUs require us to
dimension related tables during setup. Introduce two parameters to the
config file, one set an upper limit of interrupts (all types) that the
system may have to control (for all cells) and another one for devices
in the system. The former will be used for VT-d, the latter should once
be helpful for AMD support, thus it can remain unset in Intel configs.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Add support for sending interrupts to CPUs

Will be used to inject events from temporarily masked edge-triggered
sources or to generate interrupts from virtual devices.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Switch VT-d to queued invalidation

Required for interrupt remapping support.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Factor out vtd_update_gcmd_reg

This encapsulates changes to the global command register that preserve
the current state and wait for the state change to become effective.
Both setting and clearing is supported.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
x86: Provide stub for programming VT-d interrupt remapping table entries

This allows us to step-wise build up the infrastructure on the IOAPIC
and PCI subsystem side.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
configs: Prepare h87i for interrupt remapping

Add the missing configuration bits that will allow us to remap IOAPIC
interrupts and MSIs.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>