1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 #ifndef _ASM_X86_APIC_H 3 #define _ASM_X86_APIC_H 4 5 #include <linux/cpumask.h> 6 7 #include <asm/alternative.h> 8 #include <asm/cpufeature.h> 9 #include <asm/apicdef.h> 10 #include <linux/atomic.h> 11 #include <asm/fixmap.h> 12 #include <asm/mpspec.h> 13 #include <asm/msr.h> 14 #include <asm/hardirq.h> 15 16 #define ARCH_APICTIMER_STOPS_ON_C3 1 17 18 /* 19 * Debugging macros 20 */ 21 #define APIC_QUIET 0 22 #define APIC_VERBOSE 1 23 #define APIC_DEBUG 2 24 25 /* Macros for apic_extnmi which controls external NMI masking */ 26 #define APIC_EXTNMI_BSP 0 /* Default */ 27 #define APIC_EXTNMI_ALL 1 28 #define APIC_EXTNMI_NONE 2 29 30 /* 31 * Define the default level of output to be very little 32 * This can be turned up by using apic=verbose for more 33 * information and apic=debug for _lots_ of information. 34 * apic_verbosity is defined in apic.c 35 */ 36 #define apic_printk(v, s, a...) do { \ 37 if ((v) <= apic_verbosity) \ 38 printk(s, ##a); \ 39 } while (0) 40 41 42 #if defined(CONFIG_X86_LOCAL_APIC) && defined(CONFIG_X86_32) 43 extern void x86_32_probe_apic(void); 44 #else 45 static inline void x86_32_probe_apic(void) { } 46 #endif 47 48 #ifdef CONFIG_X86_LOCAL_APIC 49 50 extern int apic_verbosity; 51 extern int local_apic_timer_c2_ok; 52 53 extern bool apic_is_disabled; 54 extern unsigned int lapic_timer_period; 55 56 extern int cpuid_to_apicid[]; 57 58 extern enum apic_intr_mode_id apic_intr_mode; 59 enum apic_intr_mode_id { 60 APIC_PIC, 61 APIC_VIRTUAL_WIRE, 62 APIC_VIRTUAL_WIRE_NO_CONFIG, 63 APIC_SYMMETRIC_IO, 64 APIC_SYMMETRIC_IO_NO_ROUTING 65 }; 66 67 /* 68 * With 82489DX we can't rely on apic feature bit 69 * retrieved via cpuid but still have to deal with 70 * such an apic chip so we assume that SMP configuration 71 * is found from MP table (64bit case uses ACPI mostly 72 * which set smp presence flag as well so we are safe 73 * to use this helper too). 74 */ 75 static inline bool apic_from_smp_config(void) 76 { 77 return smp_found_config && !apic_is_disabled; 78 } 79 80 /* 81 * Basic functions accessing APICs. 82 */ 83 #ifdef CONFIG_PARAVIRT 84 #include <asm/paravirt.h> 85 #endif 86 87 static inline void native_apic_mem_write(u32 reg, u32 v) 88 { 89 volatile u32 *addr = (volatile u32 *)(APIC_BASE + reg); 90 91 alternative_io("movl %0, %P1", "xchgl %0, %P1", X86_BUG_11AP, 92 ASM_OUTPUT2("=r" (v), "=m" (*addr)), 93 ASM_OUTPUT2("0" (v), "m" (*addr))); 94 } 95 96 static inline u32 native_apic_mem_read(u32 reg) 97 { 98 return *((volatile u32 *)(APIC_BASE + reg)); 99 } 100 101 extern void native_apic_icr_write(u32 low, u32 id); 102 extern u64 native_apic_icr_read(void); 103 104 static inline bool apic_is_x2apic_enabled(void) 105 { 106 u64 msr; 107 108 if (rdmsrl_safe(MSR_IA32_APICBASE, &msr)) 109 return false; 110 return msr & X2APIC_ENABLE; 111 } 112 113 extern void enable_IR_x2apic(void); 114 115 extern int get_physical_broadcast(void); 116 117 extern int lapic_get_maxlvt(void); 118 extern void clear_local_APIC(void); 119 extern void disconnect_bsp_APIC(int virt_wire_setup); 120 extern void disable_local_APIC(void); 121 extern void apic_soft_disable(void); 122 extern void lapic_shutdown(void); 123 extern void sync_Arb_IDs(void); 124 extern void init_bsp_APIC(void); 125 extern void apic_intr_mode_select(void); 126 extern void apic_intr_mode_init(void); 127 extern void init_apic_mappings(void); 128 void register_lapic_address(unsigned long address); 129 extern void setup_boot_APIC_clock(void); 130 extern void setup_secondary_APIC_clock(void); 131 extern void lapic_update_tsc_freq(void); 132 133 #ifdef CONFIG_X86_64 134 static inline bool apic_force_enable(unsigned long addr) 135 { 136 return false; 137 } 138 #else 139 extern bool apic_force_enable(unsigned long addr); 140 #endif 141 142 extern void apic_ap_setup(void); 143 144 /* 145 * On 32bit this is mach-xxx local 146 */ 147 #ifdef CONFIG_X86_64 148 extern int apic_is_clustered_box(void); 149 #else 150 static inline int apic_is_clustered_box(void) 151 { 152 return 0; 153 } 154 #endif 155 156 extern int setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask); 157 extern void lapic_assign_system_vectors(void); 158 extern void lapic_assign_legacy_vector(unsigned int isairq, bool replace); 159 extern void lapic_update_legacy_vectors(void); 160 extern void lapic_online(void); 161 extern void lapic_offline(void); 162 extern bool apic_needs_pit(void); 163 164 extern void apic_send_IPI_allbutself(unsigned int vector); 165 166 #else /* !CONFIG_X86_LOCAL_APIC */ 167 static inline void lapic_shutdown(void) { } 168 #define local_apic_timer_c2_ok 1 169 static inline void init_apic_mappings(void) { } 170 static inline void disable_local_APIC(void) { } 171 # define setup_boot_APIC_clock x86_init_noop 172 # define setup_secondary_APIC_clock x86_init_noop 173 static inline void lapic_update_tsc_freq(void) { } 174 static inline void init_bsp_APIC(void) { } 175 static inline void apic_intr_mode_select(void) { } 176 static inline void apic_intr_mode_init(void) { } 177 static inline void lapic_assign_system_vectors(void) { } 178 static inline void lapic_assign_legacy_vector(unsigned int i, bool r) { } 179 static inline bool apic_needs_pit(void) { return true; } 180 #endif /* !CONFIG_X86_LOCAL_APIC */ 181 182 #ifdef CONFIG_X86_X2APIC 183 static inline void native_apic_msr_write(u32 reg, u32 v) 184 { 185 if (reg == APIC_DFR || reg == APIC_ID || reg == APIC_LDR || 186 reg == APIC_LVR) 187 return; 188 189 wrmsr(APIC_BASE_MSR + (reg >> 4), v, 0); 190 } 191 192 static inline void native_apic_msr_eoi_write(u32 reg, u32 v) 193 { 194 __wrmsr(APIC_BASE_MSR + (APIC_EOI >> 4), APIC_EOI_ACK, 0); 195 } 196 197 static inline u32 native_apic_msr_read(u32 reg) 198 { 199 u64 msr; 200 201 if (reg == APIC_DFR) 202 return -1; 203 204 rdmsrl(APIC_BASE_MSR + (reg >> 4), msr); 205 return (u32)msr; 206 } 207 208 static inline void native_x2apic_icr_write(u32 low, u32 id) 209 { 210 wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low); 211 } 212 213 static inline u64 native_x2apic_icr_read(void) 214 { 215 unsigned long val; 216 217 rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val); 218 return val; 219 } 220 221 extern int x2apic_mode; 222 extern int x2apic_phys; 223 extern void __init x2apic_set_max_apicid(u32 apicid); 224 extern void x2apic_setup(void); 225 static inline int x2apic_enabled(void) 226 { 227 return boot_cpu_has(X86_FEATURE_X2APIC) && apic_is_x2apic_enabled(); 228 } 229 230 #define x2apic_supported() (boot_cpu_has(X86_FEATURE_X2APIC)) 231 #else /* !CONFIG_X86_X2APIC */ 232 static inline void x2apic_setup(void) { } 233 static inline int x2apic_enabled(void) { return 0; } 234 static inline u32 native_apic_msr_read(u32 reg) { BUG(); } 235 #define x2apic_mode (0) 236 #define x2apic_supported() (0) 237 #endif /* !CONFIG_X86_X2APIC */ 238 extern void __init check_x2apic(void); 239 240 struct irq_data; 241 242 /* 243 * Copyright 2004 James Cleverdon, IBM. 244 * 245 * Generic APIC sub-arch data struct. 246 * 247 * Hacked for x86-64 by James Cleverdon from i386 architecture code by 248 * Martin Bligh, Andi Kleen, James Bottomley, John Stultz, and 249 * James Cleverdon. 250 */ 251 struct apic { 252 /* Hotpath functions first */ 253 void (*eoi_write)(u32 reg, u32 v); 254 void (*native_eoi_write)(u32 reg, u32 v); 255 void (*write)(u32 reg, u32 v); 256 u32 (*read)(u32 reg); 257 258 /* IPI related functions */ 259 void (*wait_icr_idle)(void); 260 u32 (*safe_wait_icr_idle)(void); 261 262 void (*send_IPI)(int cpu, int vector); 263 void (*send_IPI_mask)(const struct cpumask *mask, int vector); 264 void (*send_IPI_mask_allbutself)(const struct cpumask *msk, int vec); 265 void (*send_IPI_allbutself)(int vector); 266 void (*send_IPI_all)(int vector); 267 void (*send_IPI_self)(int vector); 268 269 u32 disable_esr; 270 271 enum apic_delivery_modes delivery_mode; 272 bool dest_mode_logical; 273 274 u32 (*calc_dest_apicid)(unsigned int cpu); 275 276 /* ICR related functions */ 277 u64 (*icr_read)(void); 278 void (*icr_write)(u32 low, u32 high); 279 280 /* The limit of the APIC ID space. */ 281 u32 max_apic_id; 282 283 /* Probe, setup and smpboot functions */ 284 int (*probe)(void); 285 int (*acpi_madt_oem_check)(char *oem_id, char *oem_table_id); 286 int (*apic_id_valid)(u32 apicid); 287 bool (*apic_id_registered)(void); 288 289 bool (*check_apicid_used)(physid_mask_t *map, int apicid); 290 void (*init_apic_ldr)(void); 291 void (*ioapic_phys_id_map)(physid_mask_t *phys_map, physid_mask_t *retmap); 292 int (*cpu_present_to_apicid)(int mps_cpu); 293 int (*phys_pkg_id)(int cpuid_apic, int index_msb); 294 295 u32 (*get_apic_id)(unsigned long x); 296 u32 (*set_apic_id)(unsigned int id); 297 298 /* wakeup_secondary_cpu */ 299 int (*wakeup_secondary_cpu)(int apicid, unsigned long start_eip); 300 /* wakeup secondary CPU using 64-bit wakeup point */ 301 int (*wakeup_secondary_cpu_64)(int apicid, unsigned long start_eip); 302 303 char *name; 304 }; 305 306 /* 307 * Pointer to the local APIC driver in use on this system (there's 308 * always just one such driver in use - the kernel decides via an 309 * early probing process which one it picks - and then sticks to it): 310 */ 311 extern struct apic *apic; 312 313 /* 314 * APIC drivers are probed based on how they are listed in the .apicdrivers 315 * section. So the order is important and enforced by the ordering 316 * of different apic driver files in the Makefile. 317 * 318 * For the files having two apic drivers, we use apic_drivers() 319 * to enforce the order with in them. 320 */ 321 #define apic_driver(sym) \ 322 static const struct apic *__apicdrivers_##sym __used \ 323 __aligned(sizeof(struct apic *)) \ 324 __section(".apicdrivers") = { &sym } 325 326 #define apic_drivers(sym1, sym2) \ 327 static struct apic *__apicdrivers_##sym1##sym2[2] __used \ 328 __aligned(sizeof(struct apic *)) \ 329 __section(".apicdrivers") = { &sym1, &sym2 } 330 331 extern struct apic *__apicdrivers[], *__apicdrivers_end[]; 332 333 /* 334 * APIC functionality to boot other CPUs - only used on SMP: 335 */ 336 #ifdef CONFIG_SMP 337 extern int lapic_can_unplug_cpu(void); 338 #endif 339 340 #ifdef CONFIG_X86_LOCAL_APIC 341 342 static inline u32 apic_read(u32 reg) 343 { 344 return apic->read(reg); 345 } 346 347 static inline void apic_write(u32 reg, u32 val) 348 { 349 apic->write(reg, val); 350 } 351 352 static inline void apic_eoi(void) 353 { 354 apic->eoi_write(APIC_EOI, APIC_EOI_ACK); 355 } 356 357 static inline u64 apic_icr_read(void) 358 { 359 return apic->icr_read(); 360 } 361 362 static inline void apic_icr_write(u32 low, u32 high) 363 { 364 apic->icr_write(low, high); 365 } 366 367 static inline void apic_wait_icr_idle(void) 368 { 369 if (apic->wait_icr_idle) 370 apic->wait_icr_idle(); 371 } 372 373 static inline u32 safe_apic_wait_icr_idle(void) 374 { 375 return apic->safe_wait_icr_idle ? apic->safe_wait_icr_idle() : 0; 376 } 377 378 static inline bool apic_id_valid(u32 apic_id) 379 { 380 return apic->apic_id_valid(apic_id); 381 } 382 383 extern void __init apic_set_eoi_write(void (*eoi_write)(u32 reg, u32 v)); 384 385 #else /* CONFIG_X86_LOCAL_APIC */ 386 387 static inline u32 apic_read(u32 reg) { return 0; } 388 static inline void apic_write(u32 reg, u32 val) { } 389 static inline void apic_eoi(void) { } 390 static inline u64 apic_icr_read(void) { return 0; } 391 static inline void apic_icr_write(u32 low, u32 high) { } 392 static inline void apic_wait_icr_idle(void) { } 393 static inline u32 safe_apic_wait_icr_idle(void) { return 0; } 394 static inline void apic_set_eoi_write(void (*eoi_write)(u32 reg, u32 v)) {} 395 396 #endif /* CONFIG_X86_LOCAL_APIC */ 397 398 extern void apic_ack_irq(struct irq_data *data); 399 400 static inline void ack_APIC_irq(void) 401 { 402 /* 403 * ack_APIC_irq() actually gets compiled as a single instruction 404 * ... yummie. 405 */ 406 apic_eoi(); 407 } 408 409 410 static inline bool lapic_vector_set_in_irr(unsigned int vector) 411 { 412 u32 irr = apic_read(APIC_IRR + (vector / 32 * 0x10)); 413 414 return !!(irr & (1U << (vector % 32))); 415 } 416 417 static inline unsigned default_get_apic_id(unsigned long x) 418 { 419 unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR)); 420 421 if (APIC_XAPIC(ver) || boot_cpu_has(X86_FEATURE_EXTD_APICID)) 422 return (x >> 24) & 0xFF; 423 else 424 return (x >> 24) & 0x0F; 425 } 426 427 /* 428 * Warm reset vector position: 429 */ 430 #define TRAMPOLINE_PHYS_LOW 0x467 431 #define TRAMPOLINE_PHYS_HIGH 0x469 432 433 extern void generic_bigsmp_probe(void); 434 435 #ifdef CONFIG_X86_LOCAL_APIC 436 437 #include <asm/smp.h> 438 439 extern struct apic apic_noop; 440 441 static inline unsigned int read_apic_id(void) 442 { 443 unsigned int reg = apic_read(APIC_ID); 444 445 return apic->get_apic_id(reg); 446 } 447 448 #ifdef CONFIG_X86_64 449 typedef int (*wakeup_cpu_handler)(int apicid, unsigned long start_eip); 450 extern void acpi_wake_cpu_handler_update(wakeup_cpu_handler handler); 451 extern int default_acpi_madt_oem_check(char *, char *); 452 extern void x86_64_probe_apic(void); 453 #else 454 static inline int default_acpi_madt_oem_check(char *a, char *b) { return 0; } 455 static inline void x86_64_probe_apic(void) { } 456 #endif 457 458 extern int default_apic_id_valid(u32 apicid); 459 460 extern u32 apic_default_calc_apicid(unsigned int cpu); 461 extern u32 apic_flat_calc_apicid(unsigned int cpu); 462 463 extern bool default_check_apicid_used(physid_mask_t *map, int apicid); 464 extern void default_ioapic_phys_id_map(physid_mask_t *phys_map, physid_mask_t *retmap); 465 extern int default_cpu_present_to_apicid(int mps_cpu); 466 467 #else /* CONFIG_X86_LOCAL_APIC */ 468 469 static inline unsigned int read_apic_id(void) { return 0; } 470 471 #endif /* !CONFIG_X86_LOCAL_APIC */ 472 473 #ifdef CONFIG_SMP 474 void apic_smt_update(void); 475 #else 476 static inline void apic_smt_update(void) { } 477 #endif 478 479 struct msi_msg; 480 struct irq_cfg; 481 482 extern void __irq_msi_compose_msg(struct irq_cfg *cfg, struct msi_msg *msg, 483 bool dmar); 484 485 extern void ioapic_zap_locks(void); 486 487 #endif /* _ASM_X86_APIC_H */ 488