apic.h 14 KB

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