Zephyr Project API  3.3.0
A Scalable Open Source RTOS
loapic.h
Go to the documentation of this file.
1/* loapic.h - public LOAPIC APIs */
2
3/*
4 * Copyright (c) 2015 Wind River Systems, Inc.
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9#ifndef ZEPHYR_INCLUDE_DRIVERS_LOAPIC_H_
10#define ZEPHYR_INCLUDE_DRIVERS_LOAPIC_H_
11
12#include <zephyr/arch/cpu.h>
13#include <zephyr/arch/x86/msr.h>
15
16/* Local APIC Register Offset */
17
18#define LOAPIC_ID 0x020 /* Local APIC ID Reg */
19#define LOAPIC_VER 0x030 /* Local APIC Version Reg */
20#define LOAPIC_TPR 0x080 /* Task Priority Reg */
21#define LOAPIC_APR 0x090 /* Arbitration Priority Reg */
22#define LOAPIC_PPR 0x0a0 /* Processor Priority Reg */
23#define LOAPIC_EOI 0x0b0 /* EOI Reg */
24#define LOAPIC_LDR 0x0d0 /* Logical Destination Reg */
25#define LOAPIC_DFR 0x0e0 /* Destination Format Reg */
26#define LOAPIC_SVR 0x0f0 /* Spurious Interrupt Reg */
27#define LOAPIC_ISR 0x100 /* In-service Reg */
28#define LOAPIC_TMR 0x180 /* Trigger Mode Reg */
29#define LOAPIC_IRR 0x200 /* Interrupt Request Reg */
30#define LOAPIC_ESR 0x280 /* Error Status Reg */
31#define LOAPIC_ICRLO 0x300 /* Interrupt Command Reg */
32#define LOAPIC_ICRHI 0x310 /* Interrupt Command Reg */
33#define LOAPIC_TIMER 0x320 /* LVT (Timer) */
34#define LOAPIC_THERMAL 0x330 /* LVT (Thermal) */
35#define LOAPIC_PMC 0x340 /* LVT (PMC) */
36#define LOAPIC_LINT0 0x350 /* LVT (LINT0) */
37#define LOAPIC_LINT1 0x360 /* LVT (LINT1) */
38#define LOAPIC_ERROR 0x370 /* LVT (ERROR) */
39#define LOAPIC_TIMER_ICR 0x380 /* Timer Initial Count Reg */
40#define LOAPIC_TIMER_CCR 0x390 /* Timer Current Count Reg */
41#define LOAPIC_TIMER_CONFIG 0x3e0 /* Timer Divide Config Reg */
42#define LOAPIC_SELF_IPI 0x3f0 /* Self IPI Reg, only support in X2APIC mode */
43
44#define LOAPIC_ICR_BUSY 0x00001000 /* delivery status: 1 = busy */
45
46#define LOAPIC_ICR_IPI_OTHERS 0x000C4000U /* normal IPI to other CPUs */
47#define LOAPIC_ICR_IPI_INIT 0x00004500U
48#define LOAPIC_ICR_IPI_STARTUP 0x00004600U
49
50#define LOAPIC_LVT_MASKED 0x00010000 /* mask */
51
52#ifndef _ASMLANGUAGE
53
54#ifdef __cplusplus
55extern "C" {
56#endif
57
58extern uint32_t z_loapic_irq_base(void);
59extern void z_loapic_enable(unsigned char cpu_number);
60extern void z_loapic_int_vec_set(unsigned int irq, unsigned int vector);
61extern void z_loapic_irq_enable(unsigned int irq);
62extern void z_loapic_irq_disable(unsigned int irq);
63
69static inline uint64_t x86_read_x2apic(unsigned int reg)
70{
71 reg >>= 4;
72 return z_x86_msr_read(X86_X2APIC_BASE_MSR + reg);
73}
74
75/* Defined in intc_loapic.c */
76#ifdef DEVICE_MMIO_IS_IN_RAM
77extern mm_reg_t z_loapic_regs;
78#endif
79
85static inline uint32_t x86_read_xapic(unsigned int reg)
86{
87 mm_reg_t base;
88#ifdef DEVICE_MMIO_IS_IN_RAM
89 base = z_loapic_regs;
90#else
91 base = CONFIG_LOAPIC_BASE_ADDRESS;
92#endif
93 return sys_read32(base + reg);
94}
95
106static inline uint32_t x86_read_loapic(unsigned int reg)
107{
108#ifdef CONFIG_X2APIC
109 return x86_read_x2apic(reg);
110#else
111 return x86_read_xapic(reg);
112#endif
113}
114
121static inline void x86_write_x2apic(unsigned int reg, uint64_t val)
122{
123 reg >>= 4;
124 z_x86_msr_write(X86_X2APIC_BASE_MSR + reg, val);
125}
126
133static inline void x86_write_xapic(unsigned int reg, uint32_t val)
134{
135 mm_reg_t base;
136#ifdef DEVICE_MMIO_IS_IN_RAM
137 base = z_loapic_regs;
138#else
139 base = CONFIG_LOAPIC_BASE_ADDRESS;
140#endif
141 sys_write32(val, base + reg);
142}
143
155static inline void x86_write_loapic(unsigned int reg, uint32_t val)
156{
157#ifdef CONFIG_X2APIC
158 x86_write_x2apic(reg, val);
159#else
160 x86_write_xapic(reg, val);
161#endif
162}
163
171static inline void z_loapic_ipi(uint8_t apic_id, uint32_t ipi, uint8_t vector)
172{
173 ipi |= vector;
174
175#ifndef CONFIG_X2APIC
176 /*
177 * Legacy xAPIC mode: first wait for any previous IPI to be delivered.
178 */
179
181 }
182
183 x86_write_xapic(LOAPIC_ICRHI, apic_id << 24);
185#else
186 /*
187 * x2APIC mode is greatly simplified: one write, no delivery status.
188 */
189
190 x86_write_x2apic(LOAPIC_ICRLO, (((uint64_t) apic_id) << 32) | ipi);
191#endif
192}
193
194#ifdef __cplusplus
195}
196#endif
197
198#endif /* _ASMLANGUAGE */
199
200#endif /* ZEPHYR_INCLUDE_DRIVERS_LOAPIC_H_ */
static void x86_write_x2apic(unsigned int reg, uint64_t val)
Write 64-bit value to the local APIC in x2APIC mode.
Definition: loapic.h:121
#define LOAPIC_ICRLO
Definition: loapic.h:31
static uint32_t x86_read_xapic(unsigned int reg)
Read 32-bit value from the local APIC in xAPIC (MMIO) mode.
Definition: loapic.h:85
#define LOAPIC_ICRHI
Definition: loapic.h:32
static uint32_t x86_read_loapic(unsigned int reg)
Read value from the local APIC using the default mode.
Definition: loapic.h:106
static uint64_t x86_read_x2apic(unsigned int reg)
Read 64-bit value from the local APIC in x2APIC mode.
Definition: loapic.h:69
#define LOAPIC_ICR_BUSY
Definition: loapic.h:44
static void x86_write_loapic(unsigned int reg, uint32_t val)
Write 32-bit value to the local APIC using the default mode.
Definition: loapic.h:155
static void x86_write_xapic(unsigned int reg, uint32_t val)
Write 32-bit value to the local APIC in xAPIC (MMIO) mode.
Definition: loapic.h:133
#define X86_X2APIC_BASE_MSR
Definition: msr.h:25
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__UINT64_TYPE__ uint64_t
Definition: stdint.h:91
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
static ALWAYS_INLINE void sys_write32(uint32_t data, mem_addr_t addr)
Definition: sys-io-common.h:70
static ALWAYS_INLINE uint32_t sys_read32(mem_addr_t addr)
Definition: sys-io-common.h:59
uintptr_t mm_reg_t
Definition: sys_io.h:20