Xen Test Framework
apic.h
Go to the documentation of this file.
1
10#ifndef XTF_X86_APIC_H
11#define XTF_X86_APIC_H
12
13#include <xtf/types.h>
14#include <xen/errno.h>
15
16#include <arch/msr-index.h>
17
18/* Local APIC register definitions. */
19#define APIC_ID 0x020
20#define APIC_LVR 0x030
21#define APIC_SPIV 0x0f0
22#define APIC_SPIV_APIC_ENABLED 0x00100
23
24#define APIC_ESR 0x280
25#define APIC_SIV 0x00020 /* Send Illegal Vector */
26#define APIC_RIV 0x00040 /* Recieve Illegal Vector */
27
28#define APIC_ICR 0x300
29#define APIC_DM_FIXED 0x00000
30#define APIC_DM_NMI 0x00400
31#define APIC_ICR_BUSY 0x01000
32#define APIC_DEST_SELF 0x40000
33
34#define APIC_ICR2 0x310
35#define APIC_LVTERR 0x370
36
37#define APIC_DEFAULT_BASE 0xfee00000ul
38
39/* Utilities. */
40
47};
48
53int apic_init(enum apic_mode mode);
54
55static inline uint32_t apic_mmio_read(unsigned int reg)
56{
57 return *(volatile uint32_t *)(_p(APIC_DEFAULT_BASE) + reg);
58}
59
60static inline void apic_mmio_write(unsigned int reg, uint32_t val)
61{
62 *(volatile uint32_t *)(_p(APIC_DEFAULT_BASE) + reg) = val;
63}
64
65static inline void apic_mmio_icr_write(uint64_t val)
66{
67 apic_mmio_write(APIC_ICR2, (uint32_t)(val >> 32));
69}
70
71static inline uint32_t apic_msr_read(unsigned int reg)
72{
73 unsigned long val;
74
75 asm volatile ("rdmsr" : "=a" (val)
76 : "c" (MSR_X2APIC_REGS + (reg >> 4)) : "edx");
77
78 return val;
79}
80
81static inline void apic_msr_write(unsigned int reg, uint32_t val)
82{
83 asm volatile ("wrmsr" ::
84 "a" (val), "d" (0),
85 "c" (MSR_X2APIC_REGS + (reg >> 4)));
86}
87
88static inline void apic_msr_icr_write(uint64_t val)
89{
90 asm volatile ("wrmsr" ::
91 "a" ((uint32_t)val), "d" ((uint32_t)(val >> 32)),
92 "c" (MSR_X2APIC_REGS + (APIC_ICR >> 4)));
93}
94
95extern enum apic_mode cur_apic_mode;
96
97/*
98 * Allow tests to force code generation for a single APIC mode.
99 */
100#ifndef TEST_APIC_MODE
101#define TEST_APIC_MODE 0
102#endif
103#define CUR_APIC_MODE (TEST_APIC_MODE ?: cur_apic_mode)
104
105static inline uint32_t apic_read(unsigned int reg)
106{
108 return apic_mmio_read(reg);
109 else
110 return apic_msr_read(reg);
111}
112
113static inline void apic_write(unsigned int reg, uint32_t val)
114{
116 return apic_mmio_write(reg, val);
117 else
118 return apic_msr_write(reg, val);
119}
120
121static inline void apic_icr_write(uint64_t val)
122{
124 return apic_mmio_icr_write(val);
125 else
126 return apic_msr_icr_write(val);
127}
128
129#endif /* XTF_X86_APIC_H */
130
131/*
132 * Local variables:
133 * mode: C
134 * c-file-style: "BSD"
135 * c-basic-offset: 4
136 * tab-width: 4
137 * indent-tabs-mode: nil
138 * End:
139 */
apic_mode
Definition: apic.h:41
@ APIC_MODE_XAPIC
Definition: apic.h:45
@ APIC_MODE_X2APIC
Definition: apic.h:46
@ APIC_MODE_DISABLED
Definition: apic.h:44
@ APIC_MODE_NONE
Definition: apic.h:43
@ APIC_MODE_UNKNOWN
Definition: apic.h:42
static uint32_t apic_mmio_read(unsigned int reg)
Definition: apic.h:55
static void apic_msr_write(unsigned int reg, uint32_t val)
Definition: apic.h:81
enum apic_mode cur_apic_mode
Definition: apic.c:13
static void apic_msr_icr_write(uint64_t val)
Definition: apic.h:88
static void apic_mmio_write(unsigned int reg, uint32_t val)
Definition: apic.h:60
#define APIC_DEFAULT_BASE
Definition: apic.h:37
static void apic_icr_write(uint64_t val)
Definition: apic.h:121
static void apic_mmio_icr_write(uint64_t val)
Definition: apic.h:65
#define CUR_APIC_MODE
Definition: apic.h:103
static uint32_t apic_read(unsigned int reg)
Definition: apic.h:105
#define APIC_ICR2
Definition: apic.h:34
static uint32_t apic_msr_read(unsigned int reg)
Definition: apic.h:71
static void apic_write(unsigned int reg, uint32_t val)
Definition: apic.h:113
int apic_init(enum apic_mode mode)
Discover and initialise the local APIC to the requested mode.
Definition: apic.c:33
#define APIC_ICR
Definition: apic.h:28
#define MSR_X2APIC_REGS
Definition: msr-index.h:47
#define _p(v)
Express an abitrary integer v as void *.
Definition: numbers.h:48
mode
Definition: main.c:102
__UINT32_TYPE__ uint32_t
Definition: stdint.h:16
__UINT64_TYPE__ uint64_t
Definition: stdint.h:17
Common declarations for all tests.