2015-04-11 01:44:37 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1984-2008, 2011-2015 Wind River Systems, Inc.
|
2017-01-19 02:01:01 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-04-11 01:44:37 +02:00
|
|
|
*/
|
|
|
|
|
2019-06-06 23:36:44 +02:00
|
|
|
/*
|
|
|
|
* driver for x86 CPU local APIC (as an interrupt controller)
|
2015-07-01 23:22:39 +02:00
|
|
|
*/
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2016-12-04 21:59:37 +01:00
|
|
|
#include <kernel.h>
|
jailhouse: add support for x2APIC mode for all LOAPIC accesses
Besides the fact that we did not have that for the current supported
boards, that makes sense for this new, virtualized mode, that is meant
to be run on top of full-fledged x86 64 CPUs.
By having xAPIC mode access only, Jailhouse has to intercept those MMIO
reads and writes, in order to examine what they do and arbitrate if it's
safe or not (e.g. not all values are accepted to ICR register). This
means that we can't run away from having a VM-exit event for each and
every access to APIC memory region and this impacts the latency the
guest OS observes over bare metal a lot.
When in x2APIC mode, Jailhouse does not require VM-exits for MSR
accesses other that writes to the ICR register, so the latency the guest
observes is reduced to almost zero.
Here are some outputs of the the command line
$ sudo ./tools/jailhouse cell stats tiny-demo
on a Jailhouse's root cell console, for one of the Zephyr demos using
LOAPIC timers, left for a couple of seconds:
Statistics for tiny-demo cell (x2APIC root, x2APIC inmate)
COUNTER SUM PER SEC
vmexits_total 7 0
vmexits_management 3 0
vmexits_cr 2 0
vmexits_cpuid 1 0
vmexits_msr 1 0
vmexits_exception 0 0
vmexits_hypercall 0 0
vmexits_mmio 0 0
vmexits_pio 0 0
vmexits_xapic 0 0
vmexits_xsetbv 0 0
Statistics for tiny-demo cell (xAPIC root, xAPIC inmate)
COUNTER SUM PER SEC
vmexits_total 4087 40
vmexits_xapic 4080 40
vmexits_management 3 0
vmexits_cr 2 0
vmexits_cpuid 1 0
vmexits_msr 1 0
vmexits_exception 0 0
vmexits_hypercall 0 0
vmexits_mmio 0 0
vmexits_pio 0 0
vmexits_xsetbv 0 0
Statistics for tiny-demo cell (xAPIC root, x2APIC inmate)
COUNTER SUM PER SEC
vmexits_total 4087 40
vmexits_msr 4080 40
vmexits_management 3 0
vmexits_cr 2 0
vmexits_cpuid 1 0
vmexits_exception 0 0
vmexits_hypercall 0 0
vmexits_mmio 0 0
vmexits_pio 0 0
vmexits_xapic 0 0
vmexits_xsetbv 0 0
See that under x2APIC mode on both Jailhouse/root-cell and guest, the
interruptions from the hypervisor are minimal. That is not the case when
Jailhouse is on xAPIC mode, though. Note also that, as a plus, x2APIC
accesses on the guest will map to xAPIC MMIO on the hypervisor just
fine.
Signed-off-by: Gustavo Lima Chaves <gustavo.lima.chaves@intel.com>
2017-10-11 23:17:12 +02:00
|
|
|
#include <kernel_structs.h>
|
2015-05-28 19:56:47 +02:00
|
|
|
#include <arch/cpu.h>
|
Introduce new sized integer typedefs
This is a start to move away from the C99 {u}int{8,16,32,64}_t types to
Zephyr defined u{8,16,32,64}_t and s{8,16,32,64}_t. This allows Zephyr
to define the sized types in a consistent manor across all the
architectures we support and not conflict with what various compilers
and libc might do with regards to the C99 types.
We introduce <zephyr/types.h> as part of this and have it include
<stdint.h> for now until we transition all the code away from the C99
types.
We go with u{8,16,32,64}_t and s{8,16,32,64}_t as there are some
existing variables defined u8 & u16 as well as to be consistent with
Zephyr naming conventions.
Jira: ZEP-2051
Change-Id: I451fed0623b029d65866622e478225dfab2c0ca8
Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2017-04-19 17:32:08 +02:00
|
|
|
#include <zephyr/types.h>
|
2016-05-07 06:55:51 +02:00
|
|
|
#include <string.h>
|
2019-06-26 16:33:39 +02:00
|
|
|
#include <sys/__assert.h>
|
2019-06-08 01:26:44 +02:00
|
|
|
#include <arch/x86/msr.h>
|
2015-04-11 01:44:37 +02:00
|
|
|
|
|
|
|
#include <toolchain.h>
|
2017-06-17 17:30:47 +02:00
|
|
|
#include <linker/sections.h>
|
2019-06-21 18:54:15 +02:00
|
|
|
#include <drivers/interrupt_controller/loapic.h> /* public API declarations */
|
2020-03-13 16:20:49 +01:00
|
|
|
#include <device.h>
|
2019-06-21 18:54:15 +02:00
|
|
|
#include <drivers/interrupt_controller/sysapic.h>
|
2021-03-09 19:54:42 +01:00
|
|
|
#include <drivers/interrupt_controller/ioapic.h>
|
2015-04-11 01:44:37 +02:00
|
|
|
|
|
|
|
/* Local APIC Version Register Bits */
|
|
|
|
|
|
|
|
#define LOAPIC_VERSION_MASK 0x000000ff /* LO APIC Version mask */
|
|
|
|
#define LOAPIC_MAXLVT_MASK 0x00ff0000 /* LO APIC Max LVT mask */
|
|
|
|
#define LOAPIC_PENTIUM4 0x00000014 /* LO APIC in Pentium4 */
|
|
|
|
#define LOAPIC_LVT_PENTIUM4 5 /* LO APIC LVT - Pentium4 */
|
|
|
|
#define LOAPIC_LVT_P6 4 /* LO APIC LVT - P6 */
|
|
|
|
#define LOAPIC_LVT_P5 3 /* LO APIC LVT - P5 */
|
|
|
|
|
|
|
|
/* Local APIC Vector Table Bits */
|
|
|
|
|
|
|
|
#define LOAPIC_VECTOR 0x000000ff /* vectorNo */
|
|
|
|
#define LOAPIC_MODE 0x00000700 /* delivery mode */
|
|
|
|
#define LOAPIC_FIXED 0x00000000 /* delivery mode: FIXED */
|
|
|
|
#define LOAPIC_SMI 0x00000200 /* delivery mode: SMI */
|
|
|
|
#define LOAPIC_NMI 0x00000400 /* delivery mode: NMI */
|
|
|
|
#define LOAPIC_EXT 0x00000700 /* delivery mode: ExtINT */
|
|
|
|
#define LOAPIC_IDLE 0x00000000 /* delivery status: Idle */
|
|
|
|
#define LOAPIC_PEND 0x00001000 /* delivery status: Pend */
|
|
|
|
#define LOAPIC_HIGH 0x00000000 /* polarity: High */
|
|
|
|
#define LOAPIC_LOW 0x00002000 /* polarity: Low */
|
|
|
|
#define LOAPIC_REMOTE 0x00004000 /* remote IRR */
|
|
|
|
#define LOAPIC_EDGE 0x00000000 /* trigger mode: Edge */
|
|
|
|
#define LOAPIC_LEVEL 0x00008000 /* trigger mode: Level */
|
|
|
|
|
|
|
|
/* Local APIC Spurious-Interrupt Register Bits */
|
|
|
|
|
|
|
|
#define LOAPIC_ENABLE 0x100 /* APIC Enabled */
|
|
|
|
#define LOAPIC_FOCUS_DISABLE 0x200 /* Focus Processor Checking */
|
|
|
|
|
2015-12-09 23:53:41 +01:00
|
|
|
#if CONFIG_LOAPIC_SPURIOUS_VECTOR_ID == -1
|
|
|
|
#define LOAPIC_SPURIOUS_VECTOR_ID (CONFIG_IDT_NUM_VECTORS - 1)
|
|
|
|
#else
|
|
|
|
#define LOAPIC_SPURIOUS_VECTOR_ID CONFIG_LOAPIC_SPURIOUS_VECTOR_ID
|
|
|
|
#endif
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2016-05-07 06:55:51 +02:00
|
|
|
#define LOPIC_SSPND_BITS_PER_IRQ 1 /* Just the one for enable disable*/
|
|
|
|
#define LOPIC_SUSPEND_BITS_REQD (ROUND_UP((LOAPIC_IRQ_COUNT * LOPIC_SSPND_BITS_PER_IRQ), 32))
|
2020-09-02 00:31:40 +02:00
|
|
|
#ifdef CONFIG_PM_DEVICE
|
2021-05-03 17:26:38 +02:00
|
|
|
#include <pm/device.h>
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t loapic_suspend_buf[LOPIC_SUSPEND_BITS_REQD / 32] = {0};
|
2021-05-07 23:18:57 +02:00
|
|
|
static uint32_t loapic_device_power_state = PM_DEVICE_STATE_ACTIVE;
|
2016-05-07 06:55:51 +02:00
|
|
|
#endif
|
|
|
|
|
2020-06-26 21:09:01 +02:00
|
|
|
#ifdef DEVICE_MMIO_IS_IN_RAM
|
|
|
|
mm_reg_t z_loapic_regs;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void send_eoi(void)
|
|
|
|
{
|
|
|
|
x86_write_xapic(LOAPIC_EOI, 0);
|
|
|
|
}
|
|
|
|
|
2015-07-01 23:22:39 +02:00
|
|
|
/**
|
2019-09-28 22:58:22 +02:00
|
|
|
* @brief Enable and initialize the local APIC.
|
2015-07-01 23:22:39 +02:00
|
|
|
*
|
2019-09-28 22:58:22 +02:00
|
|
|
* Called from early assembly layer (e.g., crt0.S).
|
2015-07-01 23:22:39 +02:00
|
|
|
*/
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2020-02-10 23:09:05 +01:00
|
|
|
void z_loapic_enable(unsigned char cpu_number)
|
2015-04-11 01:44:37 +02:00
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
int32_t loApicMaxLvt; /* local APIC Max LVT */
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2020-06-26 21:09:01 +02:00
|
|
|
#ifdef DEVICE_MMIO_IS_IN_RAM
|
|
|
|
device_map(&z_loapic_regs, CONFIG_LOAPIC_BASE_ADDRESS, 0x1000,
|
|
|
|
K_MEM_CACHE_NONE);
|
|
|
|
#endif /* DEVICE_MMIO_IS_IN_RAM */
|
2020-02-10 23:09:05 +01:00
|
|
|
#ifndef CONFIG_X2APIC
|
|
|
|
/*
|
|
|
|
* in xAPIC and flat model, bits 24-31 in LDR (Logical APIC ID) are
|
|
|
|
* bitmap of target logical APIC ID and it supports maximum 8 local
|
|
|
|
* APICs.
|
|
|
|
*
|
|
|
|
* The logical APIC ID could be arbitrarily selected by system software
|
|
|
|
* and is different from local APIC ID in local APIC ID register.
|
|
|
|
*
|
|
|
|
* We choose 0 for BSP, and the index to x86_cpuboot[] for secondary
|
|
|
|
* CPUs.
|
|
|
|
*
|
|
|
|
* in X2APIC, LDR is read-only.
|
|
|
|
*/
|
|
|
|
x86_write_xapic(LOAPIC_LDR, 1 << (cpu_number + 24));
|
|
|
|
#endif
|
|
|
|
|
2019-06-08 01:26:44 +02:00
|
|
|
/*
|
|
|
|
* enable the local APIC. note that we use xAPIC mode here, since
|
|
|
|
* x2APIC access is not enabled until the next step (if at all).
|
|
|
|
*/
|
|
|
|
|
|
|
|
x86_write_xapic(LOAPIC_SVR,
|
|
|
|
x86_read_xapic(LOAPIC_SVR) | LOAPIC_ENABLE);
|
|
|
|
|
|
|
|
#ifdef CONFIG_X2APIC
|
|
|
|
/*
|
|
|
|
* turn on x2APIC mode. we trust the config option, so
|
|
|
|
* we don't check CPUID to see if x2APIC is supported.
|
|
|
|
*/
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
uint64_t msr = z_x86_msr_read(X86_APIC_BASE_MSR);
|
2019-06-08 01:26:44 +02:00
|
|
|
msr |= X86_APIC_BASE_MSR_X2APIC;
|
|
|
|
z_x86_msr_write(X86_APIC_BASE_MSR, msr);
|
|
|
|
#endif
|
|
|
|
|
2019-06-06 23:36:44 +02:00
|
|
|
loApicMaxLvt = (x86_read_loapic(LOAPIC_VER) & LOAPIC_MAXLVT_MASK) >> 16;
|
2015-04-11 01:44:37 +02:00
|
|
|
|
|
|
|
/* reset the DFR, TPR, TIMER_CONFIG, and TIMER_ICR */
|
|
|
|
|
2019-06-05 19:28:38 +02:00
|
|
|
#ifndef CONFIG_X2APIC
|
2020-02-10 23:09:05 +01:00
|
|
|
/* Flat model */
|
2019-06-06 23:36:44 +02:00
|
|
|
x86_write_loapic(LOAPIC_DFR, 0xffffffff); /* no DFR in x2APIC mode */
|
jailhouse: add support for x2APIC mode for all LOAPIC accesses
Besides the fact that we did not have that for the current supported
boards, that makes sense for this new, virtualized mode, that is meant
to be run on top of full-fledged x86 64 CPUs.
By having xAPIC mode access only, Jailhouse has to intercept those MMIO
reads and writes, in order to examine what they do and arbitrate if it's
safe or not (e.g. not all values are accepted to ICR register). This
means that we can't run away from having a VM-exit event for each and
every access to APIC memory region and this impacts the latency the
guest OS observes over bare metal a lot.
When in x2APIC mode, Jailhouse does not require VM-exits for MSR
accesses other that writes to the ICR register, so the latency the guest
observes is reduced to almost zero.
Here are some outputs of the the command line
$ sudo ./tools/jailhouse cell stats tiny-demo
on a Jailhouse's root cell console, for one of the Zephyr demos using
LOAPIC timers, left for a couple of seconds:
Statistics for tiny-demo cell (x2APIC root, x2APIC inmate)
COUNTER SUM PER SEC
vmexits_total 7 0
vmexits_management 3 0
vmexits_cr 2 0
vmexits_cpuid 1 0
vmexits_msr 1 0
vmexits_exception 0 0
vmexits_hypercall 0 0
vmexits_mmio 0 0
vmexits_pio 0 0
vmexits_xapic 0 0
vmexits_xsetbv 0 0
Statistics for tiny-demo cell (xAPIC root, xAPIC inmate)
COUNTER SUM PER SEC
vmexits_total 4087 40
vmexits_xapic 4080 40
vmexits_management 3 0
vmexits_cr 2 0
vmexits_cpuid 1 0
vmexits_msr 1 0
vmexits_exception 0 0
vmexits_hypercall 0 0
vmexits_mmio 0 0
vmexits_pio 0 0
vmexits_xsetbv 0 0
Statistics for tiny-demo cell (xAPIC root, x2APIC inmate)
COUNTER SUM PER SEC
vmexits_total 4087 40
vmexits_msr 4080 40
vmexits_management 3 0
vmexits_cr 2 0
vmexits_cpuid 1 0
vmexits_exception 0 0
vmexits_hypercall 0 0
vmexits_mmio 0 0
vmexits_pio 0 0
vmexits_xapic 0 0
vmexits_xsetbv 0 0
See that under x2APIC mode on both Jailhouse/root-cell and guest, the
interruptions from the hypervisor are minimal. That is not the case when
Jailhouse is on xAPIC mode, though. Note also that, as a plus, x2APIC
accesses on the guest will map to xAPIC MMIO on the hypervisor just
fine.
Signed-off-by: Gustavo Lima Chaves <gustavo.lima.chaves@intel.com>
2017-10-11 23:17:12 +02:00
|
|
|
#endif
|
2017-10-11 23:08:17 +02:00
|
|
|
|
2019-06-06 23:36:44 +02:00
|
|
|
x86_write_loapic(LOAPIC_TPR, 0x0);
|
|
|
|
x86_write_loapic(LOAPIC_TIMER_CONFIG, 0x0);
|
|
|
|
x86_write_loapic(LOAPIC_TIMER_ICR, 0x0);
|
2015-04-11 01:44:37 +02:00
|
|
|
|
|
|
|
/* program Local Vector Table for the Virtual Wire Mode */
|
|
|
|
|
x86: Jailhouse port, tested for UART (# 0, polling) and LOAPIC timer
This is an introductory port for Zephyr to be run as a Jailhouse
hypervisor[1]'s "inmate cell", on x86 64-bit CPUs (running on 32-bit
mode). This was tested with their "tiny-demo" inmate demo cell
configuration, which takes one of the CPUs of the QEMU-VM root cell
config, along with some RAM and serial controller access (it will even
do nice things like reserving some L3 cache for it via Intel CAT) and
Zephyr samples:
- hello_world
- philosophers
- synchronization
The final binary receives an additional boot sequence preamble that
conforms to Jailhouse's expectations (starts at 0x0 in real mode). It
will put the processor in 32-bit protected mode and then proceed to
Zephyr's __start function.
Testing it is just a matter of:
$ mmake -C samples/<sample_dir> BOARD=x86_jailhouse JAILHOUSE_QEMU_IMG_FILE=<path_to_image.qcow2> run
$ sudo insmod <path to jailhouse.ko>
$ sudo jailhouse enable <path to configs/qemu-x86.cell>
$ sudo jailhouse cell create <path to configs/tiny-demo.cell>
$ sudo mount -t 9p -o trans/virtio host /mnt
$ sudo jailhouse cell load tiny-demo /mnt/zephyr.bin
$ sudo jailhouse cell start tiny-demo
$ sudo jailhouse cell destroy tiny-demo
$ sudo jailhouse disable
$ sudo rmmod jailhouse
For the hello_world demo case, one should then get QEMU's serial port
output similar to:
"""
Created cell "tiny-demo"
Page pool usage after cell creation: mem 275/1480, remap 65607/131072
Cell "tiny-demo" can be loaded
CPU 3 received SIPI, vector 100
Started cell "tiny-demo"
***** BOOTING ZEPHYR OS v1.9.0 - BUILD: Sep 12 2017 20:03:22 *****
Hello World! x86
"""
Note that the Jailhouse's root cell *has to be started in xAPIC
mode* (kernel command line argument 'nox2apic') in order for this to
work. x2APIC support and its reasoning will come on a separate commit.
As a reminder, the make run target introduced for x86_jailhouse board
involves a root cell image with Jailhouse in it, to be launched and then
partitioned (with >= 2 64-bit CPUs in it).
Inmate cell configs with no JAILHOUSE_CELL_PASSIVE_COMMREG flag
set (e.g. apic-demo one) would need extra code in Zephyr to deal with
cell shutdown command responses from the hypervisor.
You may want to fine tune CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC for your
specific CPU—there is no detection from Zephyr with regard to that.
Other config differences from pristine QEMU defaults worth of mention
are:
- there is no HPET when running as Jailhouse guest. We use the LOAPIC
timer, instead
- there is no PIC_DISABLE, because there is no 8259A PIC when running
as a Jailhouse guest
- XIP makes no sense also when running as Jailhouse guest, and both
PHYS_RAM_ADDR/PHYS_LOAD_ADD are set to zero, what tiny-demo cell
config is set to
This opens up new possibilities for Zephyr, so that usages beyond just
MCUs come to the table. I see special demand coming from
functional-safety related use cases on industry, automotive, etc.
[1] https://github.com/siemens/jailhouse
Reference to Jailhouse's booting preamble code:
Origin: Jailhouse
License: BSD 2-Clause
URL: https://github.com/siemens/jailhouse
commit: 607251b44397666a3cbbf859d784dccf20aba016
Purpose: Dual-licensing of inmate lib code
Maintained-by: Zephyr
Signed-off-by: Gustavo Lima Chaves <gustavo.lima.chaves@intel.com>
2017-10-11 23:13:00 +02:00
|
|
|
/* skip LINT0/LINT1 for Jailhouse guest case, because we won't
|
|
|
|
* ever be waiting for interrupts on those
|
|
|
|
*/
|
2015-04-11 01:44:37 +02:00
|
|
|
/* set LINT0: extInt, high-polarity, edge-trigger, not-masked */
|
|
|
|
|
2019-06-06 23:36:44 +02:00
|
|
|
x86_write_loapic(LOAPIC_LINT0, (x86_read_loapic(LOAPIC_LINT0) &
|
2017-10-11 23:08:17 +02:00
|
|
|
~(LOAPIC_MODE | LOAPIC_LOW |
|
|
|
|
LOAPIC_LEVEL | LOAPIC_LVT_MASKED)) |
|
|
|
|
(LOAPIC_EXT | LOAPIC_HIGH | LOAPIC_EDGE));
|
2015-04-11 01:44:37 +02:00
|
|
|
|
|
|
|
/* set LINT1: NMI, high-polarity, edge-trigger, not-masked */
|
|
|
|
|
2019-06-06 23:36:44 +02:00
|
|
|
x86_write_loapic(LOAPIC_LINT1, (x86_read_loapic(LOAPIC_LINT1) &
|
2017-10-11 23:08:17 +02:00
|
|
|
~(LOAPIC_MODE | LOAPIC_LOW |
|
|
|
|
LOAPIC_LEVEL | LOAPIC_LVT_MASKED)) |
|
|
|
|
(LOAPIC_NMI | LOAPIC_HIGH | LOAPIC_EDGE));
|
2015-04-11 01:44:37 +02:00
|
|
|
|
|
|
|
/* lock the Local APIC interrupts */
|
|
|
|
|
2019-06-06 23:36:44 +02:00
|
|
|
x86_write_loapic(LOAPIC_TIMER, LOAPIC_LVT_MASKED);
|
|
|
|
x86_write_loapic(LOAPIC_ERROR, LOAPIC_LVT_MASKED);
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2019-06-04 16:52:23 +02:00
|
|
|
if (loApicMaxLvt >= LOAPIC_LVT_P6) {
|
2019-06-06 23:36:44 +02:00
|
|
|
x86_write_loapic(LOAPIC_PMC, LOAPIC_LVT_MASKED);
|
2019-06-04 16:52:23 +02:00
|
|
|
}
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2019-06-04 16:52:23 +02:00
|
|
|
if (loApicMaxLvt >= LOAPIC_LVT_PENTIUM4) {
|
2019-06-06 23:36:44 +02:00
|
|
|
x86_write_loapic(LOAPIC_THERMAL, LOAPIC_LVT_MASKED);
|
2019-06-04 16:52:23 +02:00
|
|
|
}
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2015-12-09 23:53:41 +01:00
|
|
|
#if CONFIG_LOAPIC_SPURIOUS_VECTOR
|
2019-06-06 23:36:44 +02:00
|
|
|
x86_write_loapic(LOAPIC_SVR, (x86_read_loapic(LOAPIC_SVR) & 0xFFFFFF00) |
|
2017-10-11 23:08:17 +02:00
|
|
|
(LOAPIC_SPURIOUS_VECTOR_ID & 0xFF));
|
2015-12-09 23:53:41 +01:00
|
|
|
#endif
|
|
|
|
|
2015-04-11 01:44:37 +02:00
|
|
|
/* discard a pending interrupt if any */
|
2019-06-06 23:36:44 +02:00
|
|
|
x86_write_loapic(LOAPIC_EOI, 0);
|
2019-09-28 22:58:22 +02:00
|
|
|
}
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2019-09-28 22:58:22 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @brief Dummy initialization function.
|
|
|
|
*
|
|
|
|
* The local APIC is initialized via z_loapic_enable() long before the
|
|
|
|
* kernel runs through its device initializations, so this is unneeded.
|
|
|
|
*/
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int loapic_init(const struct device *unused)
|
2019-09-28 22:58:22 +02:00
|
|
|
{
|
|
|
|
ARG_UNUSED(unused);
|
2016-08-02 21:05:08 +02:00
|
|
|
return 0;
|
2015-04-11 01:44:37 +02:00
|
|
|
}
|
|
|
|
|
2021-03-09 19:54:42 +01:00
|
|
|
|
|
|
|
uint32_t z_loapic_irq_base(void)
|
|
|
|
{
|
|
|
|
return z_ioapic_num_rtes();
|
|
|
|
}
|
|
|
|
|
2015-07-01 23:22:39 +02:00
|
|
|
/**
|
|
|
|
*
|
2015-07-01 23:51:40 +02:00
|
|
|
* @brief Set the vector field in the specified RTE
|
2015-07-01 23:22:39 +02:00
|
|
|
*
|
2016-09-22 20:20:26 +02:00
|
|
|
* This associates an IRQ with the desired vector in the IDT.
|
2015-07-01 23:22:39 +02:00
|
|
|
*
|
2015-07-01 23:29:04 +02:00
|
|
|
* @return N/A
|
2015-07-01 23:22:39 +02:00
|
|
|
*/
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2019-03-08 22:19:05 +01:00
|
|
|
void z_loapic_int_vec_set(unsigned int irq, /* IRQ number of the interrupt */
|
2015-07-27 17:02:41 +02:00
|
|
|
unsigned int vector /* vector to copy into the LVT */
|
2015-04-11 01:44:37 +02:00
|
|
|
)
|
|
|
|
{
|
2018-08-15 02:57:08 +02:00
|
|
|
unsigned int oldLevel; /* previous interrupt lock level */
|
2015-04-11 01:44:37 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The following mappings are used:
|
|
|
|
*
|
|
|
|
* IRQ0 -> LOAPIC_TIMER
|
|
|
|
* IRQ1 -> LOAPIC_THERMAL
|
|
|
|
* IRQ2 -> LOAPIC_PMC
|
|
|
|
* IRQ3 -> LOAPIC_LINT0
|
|
|
|
* IRQ4 -> LOAPIC_LINT1
|
|
|
|
* IRQ5 -> LOAPIC_ERROR
|
|
|
|
*
|
|
|
|
* It's assumed that LVTs are spaced by 0x10 bytes
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* update the 'vector' bits in the LVT */
|
|
|
|
|
|
|
|
oldLevel = irq_lock();
|
2019-06-06 23:36:44 +02:00
|
|
|
x86_write_loapic(LOAPIC_TIMER + (irq * 0x10),
|
|
|
|
(x86_read_loapic(LOAPIC_TIMER + (irq * 0x10)) &
|
2017-10-11 23:08:17 +02:00
|
|
|
~LOAPIC_VECTOR) | vector);
|
2015-04-11 01:44:37 +02:00
|
|
|
irq_unlock(oldLevel);
|
|
|
|
}
|
|
|
|
|
2015-07-01 23:22:39 +02:00
|
|
|
/**
|
|
|
|
*
|
2015-07-01 23:51:40 +02:00
|
|
|
* @brief Enable an individual LOAPIC interrupt (IRQ)
|
2015-07-01 23:22:39 +02:00
|
|
|
*
|
2015-10-20 18:42:33 +02:00
|
|
|
* @param irq the IRQ number of the interrupt
|
|
|
|
*
|
2015-07-01 23:22:39 +02:00
|
|
|
* This routine clears the interrupt mask bit in the LVT for the specified IRQ
|
|
|
|
*
|
2015-07-01 23:29:04 +02:00
|
|
|
* @return N/A
|
2015-07-01 23:22:39 +02:00
|
|
|
*/
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2019-03-08 22:19:05 +01:00
|
|
|
void z_loapic_irq_enable(unsigned int irq)
|
2015-04-11 01:44:37 +02:00
|
|
|
{
|
2018-08-15 02:57:08 +02:00
|
|
|
unsigned int oldLevel; /* previous interrupt lock level */
|
2015-04-11 01:44:37 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* See the comments in _LoApicLvtVecSet() regarding IRQ to LVT mappings
|
|
|
|
* and ths assumption concerning LVT spacing.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* clear the mask bit in the LVT */
|
|
|
|
|
|
|
|
oldLevel = irq_lock();
|
2019-06-06 23:36:44 +02:00
|
|
|
x86_write_loapic(LOAPIC_TIMER + (irq * 0x10),
|
|
|
|
x86_read_loapic(LOAPIC_TIMER + (irq * 0x10)) &
|
2017-10-11 23:08:17 +02:00
|
|
|
~LOAPIC_LVT_MASKED);
|
2015-04-11 01:44:37 +02:00
|
|
|
irq_unlock(oldLevel);
|
|
|
|
}
|
|
|
|
|
2015-07-01 23:22:39 +02:00
|
|
|
/**
|
|
|
|
*
|
2015-07-01 23:51:40 +02:00
|
|
|
* @brief Disable an individual LOAPIC interrupt (IRQ)
|
2015-07-01 23:22:39 +02:00
|
|
|
*
|
2015-10-20 18:42:33 +02:00
|
|
|
* @param irq the IRQ number of the interrupt
|
|
|
|
*
|
2015-07-01 23:22:39 +02:00
|
|
|
* This routine clears the interrupt mask bit in the LVT for the specified IRQ
|
|
|
|
*
|
2015-07-01 23:29:04 +02:00
|
|
|
* @return N/A
|
2015-07-01 23:22:39 +02:00
|
|
|
*/
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2019-03-08 22:19:05 +01:00
|
|
|
void z_loapic_irq_disable(unsigned int irq)
|
2015-04-11 01:44:37 +02:00
|
|
|
{
|
2018-08-15 02:57:08 +02:00
|
|
|
unsigned int oldLevel; /* previous interrupt lock level */
|
2015-04-11 01:44:37 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* See the comments in _LoApicLvtVecSet() regarding IRQ to LVT mappings
|
|
|
|
* and ths assumption concerning LVT spacing.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* set the mask bit in the LVT */
|
|
|
|
|
|
|
|
oldLevel = irq_lock();
|
2019-06-06 23:36:44 +02:00
|
|
|
x86_write_loapic(LOAPIC_TIMER + (irq * 0x10),
|
|
|
|
x86_read_loapic(LOAPIC_TIMER + (irq * 0x10)) |
|
2017-10-11 23:08:17 +02:00
|
|
|
LOAPIC_LVT_MASKED);
|
2015-04-11 01:44:37 +02:00
|
|
|
irq_unlock(oldLevel);
|
|
|
|
}
|
2015-08-22 02:10:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Find the currently executing interrupt vector, if any
|
|
|
|
*
|
|
|
|
* This routine finds the vector of the interrupt that is being processed.
|
|
|
|
* The ISR (In-Service Register) register contain the vectors of the interrupts
|
2017-04-19 19:45:34 +02:00
|
|
|
* in service. And the higher vector is the identification of the interrupt
|
2015-08-22 02:10:32 +02:00
|
|
|
* being currently processed.
|
|
|
|
*
|
2016-07-12 20:42:18 +02:00
|
|
|
* This function must be called with interrupts locked in interrupt context.
|
|
|
|
*
|
2015-08-22 02:10:32 +02:00
|
|
|
* ISR registers' offsets:
|
|
|
|
* --------------------
|
|
|
|
* | Offset | bits |
|
|
|
|
* --------------------
|
|
|
|
* | 0100H | 0:31 |
|
|
|
|
* | 0110H | 32:63 |
|
|
|
|
* | 0120H | 64:95 |
|
|
|
|
* | 0130H | 96:127 |
|
|
|
|
* | 0140H | 128:159 |
|
|
|
|
* | 0150H | 160:191 |
|
|
|
|
* | 0160H | 192:223 |
|
|
|
|
* | 0170H | 224:255 |
|
|
|
|
* --------------------
|
|
|
|
*
|
2016-09-08 20:01:23 +02:00
|
|
|
* @return The vector of the interrupt that is currently being processed, or -1
|
|
|
|
* if no IRQ is being serviced.
|
2015-08-22 02:10:32 +02:00
|
|
|
*/
|
2019-06-28 22:06:37 +02:00
|
|
|
int z_irq_controller_isr_vector_get(void)
|
2015-08-22 02:10:32 +02:00
|
|
|
{
|
2016-07-12 20:42:18 +02:00
|
|
|
int pReg, block;
|
|
|
|
|
2016-09-08 20:01:23 +02:00
|
|
|
/* Block 0 bits never lit up as these are all exception or reserved
|
|
|
|
* vectors
|
|
|
|
*/
|
|
|
|
for (block = 7; likely(block > 0); block--) {
|
2019-06-06 23:36:44 +02:00
|
|
|
pReg = x86_read_loapic(LOAPIC_ISR + (block * 0x10));
|
2016-07-12 20:42:18 +02:00
|
|
|
if (pReg) {
|
|
|
|
return (block * 32) + (find_msb_set(pReg) - 1);
|
2015-08-22 02:10:32 +02:00
|
|
|
}
|
|
|
|
|
2016-07-12 20:42:18 +02:00
|
|
|
}
|
2016-09-08 20:01:23 +02:00
|
|
|
return -1;
|
2015-08-22 02:10:32 +02:00
|
|
|
}
|
2015-12-08 20:17:56 +01:00
|
|
|
|
2020-09-02 00:31:40 +02:00
|
|
|
#ifdef CONFIG_PM_DEVICE
|
2020-04-30 20:33:38 +02:00
|
|
|
static int loapic_suspend(const struct device *port)
|
2016-05-07 06:55:51 +02:00
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
volatile uint32_t lvt; /* local vector table entry value */
|
2016-05-07 06:55:51 +02:00
|
|
|
int loapic_irq;
|
|
|
|
|
|
|
|
ARG_UNUSED(port);
|
|
|
|
|
2018-09-12 04:09:03 +02:00
|
|
|
(void)memset(loapic_suspend_buf, 0, (LOPIC_SUSPEND_BITS_REQD >> 3));
|
2016-05-07 06:55:51 +02:00
|
|
|
|
|
|
|
for (loapic_irq = 0; loapic_irq < LOAPIC_IRQ_COUNT; loapic_irq++) {
|
|
|
|
|
2021-03-09 19:54:42 +01:00
|
|
|
if (_irq_to_interrupt_vector[z_loapic_irq_base() + loapic_irq]) {
|
2016-05-07 06:55:51 +02:00
|
|
|
|
|
|
|
/* Since vector numbers are already present in RAM/ROM,
|
|
|
|
* We save only the mask bits here.
|
|
|
|
*/
|
2019-06-06 23:36:44 +02:00
|
|
|
lvt = x86_read_loapic(LOAPIC_TIMER + (loapic_irq * 0x10));
|
2016-05-07 06:55:51 +02:00
|
|
|
|
2019-03-27 02:57:45 +01:00
|
|
|
if ((lvt & LOAPIC_LVT_MASKED) == 0U) {
|
2016-05-07 06:55:51 +02:00
|
|
|
sys_bitfield_set_bit((mem_addr_t)loapic_suspend_buf,
|
|
|
|
loapic_irq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-07 23:18:57 +02:00
|
|
|
loapic_device_power_state = PM_DEVICE_STATE_SUSPEND;
|
2016-05-07 06:55:51 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
int loapic_resume(const struct device *port)
|
2016-05-07 06:55:51 +02:00
|
|
|
{
|
|
|
|
int loapic_irq;
|
|
|
|
|
|
|
|
ARG_UNUSED(port);
|
|
|
|
|
|
|
|
/* Assuming all loapic device registers lose their state, the call to
|
2019-03-12 22:15:42 +01:00
|
|
|
* z_loapic_init(), should bring all the registers to a sane state.
|
2016-05-07 06:55:51 +02:00
|
|
|
*/
|
2019-03-12 22:15:42 +01:00
|
|
|
loapic_init(NULL);
|
2016-05-07 06:55:51 +02:00
|
|
|
|
|
|
|
for (loapic_irq = 0; loapic_irq < LOAPIC_IRQ_COUNT; loapic_irq++) {
|
|
|
|
|
2021-03-09 19:54:42 +01:00
|
|
|
if (_irq_to_interrupt_vector[z_loapic_irq_base() + loapic_irq]) {
|
2016-05-07 06:55:51 +02:00
|
|
|
/* Configure vector and enable the required ones*/
|
2019-03-08 22:19:05 +01:00
|
|
|
z_loapic_int_vec_set(loapic_irq,
|
2021-03-09 19:54:42 +01:00
|
|
|
_irq_to_interrupt_vector[z_loapic_irq_base() +
|
|
|
|
loapic_irq]);
|
2016-05-07 06:55:51 +02:00
|
|
|
|
|
|
|
if (sys_bitfield_test_bit((mem_addr_t) loapic_suspend_buf,
|
|
|
|
loapic_irq)) {
|
2019-03-08 22:19:05 +01:00
|
|
|
z_loapic_irq_enable(loapic_irq);
|
2016-05-07 06:55:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-07 23:18:57 +02:00
|
|
|
loapic_device_power_state = PM_DEVICE_STATE_ACTIVE;
|
2016-09-11 18:17:19 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implements the driver control management functionality
|
|
|
|
* the *context may include IN data or/and OUT data
|
|
|
|
*/
|
2020-04-30 20:33:38 +02:00
|
|
|
static int loapic_device_ctrl(const struct device *port,
|
|
|
|
uint32_t ctrl_command,
|
2021-04-01 08:06:33 +02:00
|
|
|
uint32_t *context, pm_device_cb cb, void *arg)
|
2016-09-11 18:17:19 +02:00
|
|
|
{
|
2019-02-14 05:05:42 +01:00
|
|
|
int ret = 0;
|
|
|
|
|
2021-05-03 18:32:53 +02:00
|
|
|
if (ctrl_command == PM_DEVICE_STATE_SET) {
|
2021-05-07 23:18:57 +02:00
|
|
|
if (*context == PM_DEVICE_STATE_SUSPEND) {
|
2019-02-14 05:05:42 +01:00
|
|
|
ret = loapic_suspend(port);
|
2021-05-07 23:18:57 +02:00
|
|
|
} else if (*context == PM_DEVICE_STATE_ACTIVE) {
|
2019-02-14 05:05:42 +01:00
|
|
|
ret = loapic_resume(port);
|
2016-09-11 18:17:19 +02:00
|
|
|
}
|
2021-05-03 18:32:53 +02:00
|
|
|
} else if (ctrl_command == PM_DEVICE_STATE_GET) {
|
2021-04-01 08:06:33 +02:00
|
|
|
*context = loapic_device_power_state;
|
2016-09-11 18:17:19 +02:00
|
|
|
}
|
2016-05-07 06:55:51 +02:00
|
|
|
|
2019-02-14 05:05:42 +01:00
|
|
|
if (cb) {
|
|
|
|
cb(port, ret, context, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2016-05-07 06:55:51 +02:00
|
|
|
}
|
|
|
|
|
2019-03-12 22:15:42 +01:00
|
|
|
SYS_DEVICE_DEFINE("loapic", loapic_init, loapic_device_ctrl, PRE_KERNEL_1,
|
2016-09-11 18:17:19 +02:00
|
|
|
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
|
2016-05-07 06:55:51 +02:00
|
|
|
#else
|
2019-03-12 22:15:42 +01:00
|
|
|
SYS_INIT(loapic_init, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
|
2020-09-02 00:31:40 +02:00
|
|
|
#endif /* CONFIG_PM_DEVICE */
|
2016-05-07 06:55:51 +02:00
|
|
|
|
2015-12-09 23:53:41 +01:00
|
|
|
|
|
|
|
#if CONFIG_LOAPIC_SPURIOUS_VECTOR
|
2019-03-12 22:15:42 +01:00
|
|
|
extern void z_loapic_spurious_handler(void);
|
2015-12-09 23:53:41 +01:00
|
|
|
|
2019-03-12 22:15:42 +01:00
|
|
|
NANO_CPU_INT_REGISTER(z_loapic_spurious_handler, NANO_SOFT_IRQ,
|
2015-12-09 23:53:41 +01:00
|
|
|
LOAPIC_SPURIOUS_VECTOR_ID >> 4,
|
|
|
|
LOAPIC_SPURIOUS_VECTOR_ID, 0);
|
|
|
|
#endif
|