apic.h 14 KB

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