Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | /* * Copyright (c) 2019 Intel Corp. * SPDX-License-Identifier: Apache-2.0 */ #ifndef ZEPHYR_INCLUDE_ARCH_X86_ARCH_H_ #define ZEPHYR_INCLUDE_ARCH_X86_ARCH_H_ #include <devicetree.h> /* Changing this value will require manual changes to exception and IDT setup * in locore.S for intel64 */ #define Z_X86_OOPS_VECTOR 32 #if !defined(_ASMLANGUAGE) #include <sys/sys_io.h> #include <zephyr/types.h> #include <stddef.h> #include <stdbool.h> #include <irq.h> #include <arch/x86/mmustructs.h> #include <arch/x86/thread_stack.h> #ifdef __cplusplus extern "C" { #endif #ifdef CONFIG_PCIE_MSI struct x86_msi_vector { unsigned int irq; uint8_t vector; #ifdef CONFIG_INTEL_VTD_ICTL bool remap; uint8_t irte; #endif }; typedef struct x86_msi_vector arch_msi_vector_t; #endif /* CONFIG_PCIE_MSI */ static ALWAYS_INLINE void arch_irq_unlock(unsigned int key) { if ((key & 0x00000200U) != 0U) { /* 'IF' bit */ __asm__ volatile ("sti" ::: "memory"); } } static ALWAYS_INLINE void sys_out8(uint8_t data, io_port_t port) { __asm__ volatile("outb %b0, %w1" :: "a"(data), "Nd"(port)); } static ALWAYS_INLINE uint8_t sys_in8(io_port_t port) { uint8_t ret; __asm__ volatile("inb %w1, %b0" : "=a"(ret) : "Nd"(port)); return ret; } static ALWAYS_INLINE void sys_out16(uint16_t data, io_port_t port) { __asm__ volatile("outw %w0, %w1" :: "a"(data), "Nd"(port)); } static ALWAYS_INLINE uint16_t sys_in16(io_port_t port) { uint16_t ret; __asm__ volatile("inw %w1, %w0" : "=a"(ret) : "Nd"(port)); return ret; } static ALWAYS_INLINE void sys_out32(uint32_t data, io_port_t port) { __asm__ volatile("outl %0, %w1" :: "a"(data), "Nd"(port)); } static ALWAYS_INLINE uint32_t sys_in32(io_port_t port) { uint32_t ret; __asm__ volatile("inl %w1, %0" : "=a"(ret) : "Nd"(port)); return ret; } static ALWAYS_INLINE void sys_write8(uint8_t data, mm_reg_t addr) { __asm__ volatile("movb %0, %1" : : "q"(data), "m" (*(volatile uint8_t *)(uintptr_t) addr) : "memory"); } static ALWAYS_INLINE uint8_t sys_read8(mm_reg_t addr) { uint8_t ret; __asm__ volatile("movb %1, %0" : "=q"(ret) : "m" (*(volatile uint8_t *)(uintptr_t) addr) : "memory"); return ret; } static ALWAYS_INLINE void sys_write16(uint16_t data, mm_reg_t addr) { __asm__ volatile("movw %0, %1" : : "r"(data), "m" (*(volatile uint16_t *)(uintptr_t) addr) : "memory"); } static ALWAYS_INLINE uint16_t sys_read16(mm_reg_t addr) { uint16_t ret; __asm__ volatile("movw %1, %0" : "=r"(ret) : "m" (*(volatile uint16_t *)(uintptr_t) addr) : "memory"); return ret; } static ALWAYS_INLINE void sys_write32(uint32_t data, mm_reg_t addr) { __asm__ volatile("movl %0, %1" : : "r"(data), "m" (*(volatile uint32_t *)(uintptr_t) addr) : "memory"); } static ALWAYS_INLINE uint32_t sys_read32(mm_reg_t addr) { uint32_t ret; __asm__ volatile("movl %1, %0" : "=r"(ret) : "m" (*(volatile uint32_t *)(uintptr_t) addr) : "memory"); return ret; } static ALWAYS_INLINE void sys_set_bit(mem_addr_t addr, unsigned int bit) { __asm__ volatile("btsl %1, %0" : "+m" (*(volatile uint32_t *) (addr)) : "Ir" (bit) : "memory"); } static ALWAYS_INLINE void sys_clear_bit(mem_addr_t addr, unsigned int bit) { __asm__ volatile("btrl %1, %0" : "+m" (*(volatile uint32_t *) (addr)) : "Ir" (bit)); } static ALWAYS_INLINE int sys_test_bit(mem_addr_t addr, unsigned int bit) { int ret; __asm__ volatile("btl %2, %1;" "sbb %0, %0" : "=r" (ret), "+m" (*(volatile uint32_t *) (addr)) : "Ir" (bit)); return ret; } static ALWAYS_INLINE int sys_test_and_set_bit(mem_addr_t addr, unsigned int bit) { int ret; __asm__ volatile("btsl %2, %1;" "sbb %0, %0" : "=r" (ret), "+m" (*(volatile uint32_t *) (addr)) : "Ir" (bit)); return ret; } static ALWAYS_INLINE int sys_test_and_clear_bit(mem_addr_t addr, unsigned int bit) { int ret; __asm__ volatile("btrl %2, %1;" "sbb %0, %0" : "=r" (ret), "+m" (*(volatile uint32_t *) (addr)) : "Ir" (bit)); return ret; } #define sys_bitfield_set_bit sys_set_bit #define sys_bitfield_clear_bit sys_clear_bit #define sys_bitfield_test_bit sys_test_bit #define sys_bitfield_test_and_set_bit sys_test_and_set_bit #define sys_bitfield_test_and_clear_bit sys_test_and_clear_bit /* * Map of IRQ numbers to their assigned vectors. On IA32, this is generated * at build time and defined via the linker script. On Intel64, it's an array. */ extern unsigned char _irq_to_interrupt_vector[]; #define Z_IRQ_TO_INTERRUPT_VECTOR(irq) \ ((unsigned int) _irq_to_interrupt_vector[irq]) #endif /* _ASMLANGUAGE */ #ifdef __cplusplus } #endif #include <drivers/interrupt_controller/sysapic.h> #ifdef CONFIG_X86_64 #include <arch/x86/intel64/arch.h> #else #include <arch/x86/ia32/arch.h> #endif #include <arch/common/ffs.h> #ifdef __cplusplus extern "C" { #endif #ifndef _ASMLANGUAGE extern void arch_irq_enable(unsigned int irq); extern void arch_irq_disable(unsigned int irq); extern uint32_t z_timer_cycle_get_32(void); static inline uint32_t arch_k_cycle_get_32(void) { return z_timer_cycle_get_32(); } static ALWAYS_INLINE bool arch_irq_unlocked(unsigned int key) { return (key & 0x200) != 0; } /** * @brief read timestamp register, 32-bits only, unserialized */ static ALWAYS_INLINE uint32_t z_do_read_cpu_timestamp32(void) { uint32_t rv; __asm__ volatile("rdtsc" : "=a" (rv) : : "%edx"); return rv; } /** * @brief read timestamp register ensuring serialization */ static inline uint64_t z_tsc_read(void) { union { struct { uint32_t lo; uint32_t hi; }; uint64_t value; } rv; #ifdef CONFIG_X86_64 /* * According to Intel 64 and IA-32 Architectures Software * Developer’s Manual, volume 3, chapter 8.2.5, LFENCE provides * a more efficient method of controlling memory ordering than * the CPUID instruction. So use LFENCE here, as all 64-bit * CPUs have LFENCE. */ __asm__ volatile ("lfence"); #else /* rdtsc & cpuid clobbers eax, ebx, ecx and edx registers */ __asm__ volatile (/* serialize */ "xorl %%eax,%%eax;" "cpuid" : : : "%eax", "%ebx", "%ecx", "%edx" ); #endif #ifdef CONFIG_X86_64 /* * We cannot use "=A", since this would use %rax on x86_64 and * return only the lower 32bits of the TSC */ __asm__ volatile ("rdtsc" : "=a" (rv.lo), "=d" (rv.hi)); #else /* "=A" means that value is in eax:edx pair. */ __asm__ volatile ("rdtsc" : "=A" (rv.value)); #endif return rv.value; } static ALWAYS_INLINE void arch_nop(void) { __asm__ volatile("nop"); } #endif /* _ASMLANGUAGE */ #ifdef __cplusplus } #endif #endif /* ZEPHYR_INCLUDE_ARCH_X86_ARCH_H_ */ |