apic.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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 init_bsp_APIC(void);
  114. extern void apic_intr_mode_init(void);
  115. extern void setup_local_APIC(void);
  116. extern void init_apic_mappings(void);
  117. void register_lapic_address(unsigned long address);
  118. extern void setup_boot_APIC_clock(void);
  119. extern void setup_secondary_APIC_clock(void);
  120. extern void lapic_update_tsc_freq(void);
  121. #ifdef CONFIG_X86_64
  122. static inline int apic_force_enable(unsigned long addr)
  123. {
  124. return -1;
  125. }
  126. #else
  127. extern int apic_force_enable(unsigned long addr);
  128. #endif
  129. extern void apic_bsp_setup(bool upmode);
  130. extern void apic_ap_setup(void);
  131. /*
  132. * On 32bit this is mach-xxx local
  133. */
  134. #ifdef CONFIG_X86_64
  135. extern int apic_is_clustered_box(void);
  136. #else
  137. static inline int apic_is_clustered_box(void)
  138. {
  139. return 0;
  140. }
  141. #endif
  142. extern int setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask);
  143. extern void lapic_assign_system_vectors(void);
  144. extern void lapic_assign_legacy_vector(unsigned int isairq, bool replace);
  145. extern void lapic_online(void);
  146. extern void lapic_offline(void);
  147. #else /* !CONFIG_X86_LOCAL_APIC */
  148. static inline void lapic_shutdown(void) { }
  149. #define local_apic_timer_c2_ok 1
  150. static inline void init_apic_mappings(void) { }
  151. static inline void disable_local_APIC(void) { }
  152. # define setup_boot_APIC_clock x86_init_noop
  153. # define setup_secondary_APIC_clock x86_init_noop
  154. static inline void lapic_update_tsc_freq(void) { }
  155. static inline void apic_intr_mode_init(void) { }
  156. static inline void lapic_assign_system_vectors(void) { }
  157. static inline void lapic_assign_legacy_vector(unsigned int i, bool r) { }
  158. #endif /* !CONFIG_X86_LOCAL_APIC */
  159. #ifdef CONFIG_X86_X2APIC
  160. /*
  161. * Make previous memory operations globally visible before
  162. * sending the IPI through x2apic wrmsr. We need a serializing instruction or
  163. * mfence for this.
  164. */
  165. static inline void x2apic_wrmsr_fence(void)
  166. {
  167. asm volatile("mfence" : : : "memory");
  168. }
  169. static inline void native_apic_msr_write(u32 reg, u32 v)
  170. {
  171. if (reg == APIC_DFR || reg == APIC_ID || reg == APIC_LDR ||
  172. reg == APIC_LVR)
  173. return;
  174. wrmsr(APIC_BASE_MSR + (reg >> 4), v, 0);
  175. }
  176. static inline void native_apic_msr_eoi_write(u32 reg, u32 v)
  177. {
  178. __wrmsr(APIC_BASE_MSR + (APIC_EOI >> 4), APIC_EOI_ACK, 0);
  179. }
  180. static inline u32 native_apic_msr_read(u32 reg)
  181. {
  182. u64 msr;
  183. if (reg == APIC_DFR)
  184. return -1;
  185. rdmsrl(APIC_BASE_MSR + (reg >> 4), msr);
  186. return (u32)msr;
  187. }
  188. static inline void native_x2apic_wait_icr_idle(void)
  189. {
  190. /* no need to wait for icr idle in x2apic */
  191. return;
  192. }
  193. static inline u32 native_safe_x2apic_wait_icr_idle(void)
  194. {
  195. /* no need to wait for icr idle in x2apic */
  196. return 0;
  197. }
  198. static inline void native_x2apic_icr_write(u32 low, u32 id)
  199. {
  200. wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low);
  201. }
  202. static inline u64 native_x2apic_icr_read(void)
  203. {
  204. unsigned long val;
  205. rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val);
  206. return val;
  207. }
  208. extern int x2apic_mode;
  209. extern int x2apic_phys;
  210. extern void __init check_x2apic(void);
  211. extern void x2apic_setup(void);
  212. static inline int x2apic_enabled(void)
  213. {
  214. return boot_cpu_has(X86_FEATURE_X2APIC) && apic_is_x2apic_enabled();
  215. }
  216. #define x2apic_supported() (boot_cpu_has(X86_FEATURE_X2APIC))
  217. #else /* !CONFIG_X86_X2APIC */
  218. static inline void check_x2apic(void) { }
  219. static inline void x2apic_setup(void) { }
  220. static inline int x2apic_enabled(void) { return 0; }
  221. #define x2apic_mode (0)
  222. #define x2apic_supported() (0)
  223. #endif /* !CONFIG_X86_X2APIC */
  224. struct irq_data;
  225. /*
  226. * Copyright 2004 James Cleverdon, IBM.
  227. * Subject to the GNU Public License, v.2
  228. *
  229. * Generic APIC sub-arch data struct.
  230. *
  231. * Hacked for x86-64 by James Cleverdon from i386 architecture code by
  232. * Martin Bligh, Andi Kleen, James Bottomley, John Stultz, and
  233. * James Cleverdon.
  234. */
  235. struct apic {
  236. /* Hotpath functions first */
  237. void (*eoi_write)(u32 reg, u32 v);
  238. void (*native_eoi_write)(u32 reg, u32 v);
  239. void (*write)(u32 reg, u32 v);
  240. u32 (*read)(u32 reg);
  241. /* IPI related functions */
  242. void (*wait_icr_idle)(void);
  243. u32 (*safe_wait_icr_idle)(void);
  244. void (*send_IPI)(int cpu, int vector);
  245. void (*send_IPI_mask)(const struct cpumask *mask, int vector);
  246. void (*send_IPI_mask_allbutself)(const struct cpumask *msk, int vec);
  247. void (*send_IPI_allbutself)(int vector);
  248. void (*send_IPI_all)(int vector);
  249. void (*send_IPI_self)(int vector);
  250. /* dest_logical is used by the IPI functions */
  251. u32 dest_logical;
  252. u32 disable_esr;
  253. u32 irq_delivery_mode;
  254. u32 irq_dest_mode;
  255. /* Functions and data related to vector allocation */
  256. void (*vector_allocation_domain)(int cpu, struct cpumask *retmask,
  257. const struct cpumask *mask);
  258. int (*cpu_mask_to_apicid)(const struct cpumask *cpumask,
  259. struct irq_data *irqdata,
  260. unsigned int *apicid);
  261. u32 (*calc_dest_apicid)(unsigned int cpu);
  262. /* ICR related functions */
  263. u64 (*icr_read)(void);
  264. void (*icr_write)(u32 low, u32 high);
  265. /* Probe, setup and smpboot functions */
  266. int (*probe)(void);
  267. int (*acpi_madt_oem_check)(char *oem_id, char *oem_table_id);
  268. int (*apic_id_valid)(u32 apicid);
  269. int (*apic_id_registered)(void);
  270. bool (*check_apicid_used)(physid_mask_t *map, int apicid);
  271. void (*init_apic_ldr)(void);
  272. void (*ioapic_phys_id_map)(physid_mask_t *phys_map, physid_mask_t *retmap);
  273. void (*setup_apic_routing)(void);
  274. int (*cpu_present_to_apicid)(int mps_cpu);
  275. void (*apicid_to_cpu_present)(int phys_apicid, physid_mask_t *retmap);
  276. int (*check_phys_apicid_present)(int phys_apicid);
  277. int (*phys_pkg_id)(int cpuid_apic, int index_msb);
  278. u32 (*get_apic_id)(unsigned long x);
  279. u32 (*set_apic_id)(unsigned int id);
  280. /* wakeup_secondary_cpu */
  281. int (*wakeup_secondary_cpu)(int apicid, unsigned long start_eip);
  282. void (*inquire_remote_apic)(int apicid);
  283. #ifdef CONFIG_X86_32
  284. /*
  285. * Called very early during boot from get_smp_config(). It should
  286. * return the logical apicid. x86_[bios]_cpu_to_apicid is
  287. * initialized before this function is called.
  288. *
  289. * If logical apicid can't be determined that early, the function
  290. * may return BAD_APICID. Logical apicid will be configured after
  291. * init_apic_ldr() while bringing up CPUs. Note that NUMA affinity
  292. * won't be applied properly during early boot in this case.
  293. */
  294. int (*x86_32_early_logical_apicid)(int cpu);
  295. #endif
  296. char *name;
  297. };
  298. /*
  299. * Pointer to the local APIC driver in use on this system (there's
  300. * always just one such driver in use - the kernel decides via an
  301. * early probing process which one it picks - and then sticks to it):
  302. */
  303. extern struct apic *apic;
  304. /*
  305. * APIC drivers are probed based on how they are listed in the .apicdrivers
  306. * section. So the order is important and enforced by the ordering
  307. * of different apic driver files in the Makefile.
  308. *
  309. * For the files having two apic drivers, we use apic_drivers()
  310. * to enforce the order with in them.
  311. */
  312. #define apic_driver(sym) \
  313. static const struct apic *__apicdrivers_##sym __used \
  314. __aligned(sizeof(struct apic *)) \
  315. __section(.apicdrivers) = { &sym }
  316. #define apic_drivers(sym1, sym2) \
  317. static struct apic *__apicdrivers_##sym1##sym2[2] __used \
  318. __aligned(sizeof(struct apic *)) \
  319. __section(.apicdrivers) = { &sym1, &sym2 }
  320. extern struct apic *__apicdrivers[], *__apicdrivers_end[];
  321. /*
  322. * APIC functionality to boot other CPUs - only used on SMP:
  323. */
  324. #ifdef CONFIG_SMP
  325. extern int wakeup_secondary_cpu_via_nmi(int apicid, unsigned long start_eip);
  326. extern int lapic_can_unplug_cpu(void);
  327. #endif
  328. #ifdef CONFIG_X86_LOCAL_APIC
  329. static inline u32 apic_read(u32 reg)
  330. {
  331. return apic->read(reg);
  332. }
  333. static inline void apic_write(u32 reg, u32 val)
  334. {
  335. apic->write(reg, val);
  336. }
  337. static inline void apic_eoi(void)
  338. {
  339. apic->eoi_write(APIC_EOI, APIC_EOI_ACK);
  340. }
  341. static inline u64 apic_icr_read(void)
  342. {
  343. return apic->icr_read();
  344. }
  345. static inline void apic_icr_write(u32 low, u32 high)
  346. {
  347. apic->icr_write(low, high);
  348. }
  349. static inline void apic_wait_icr_idle(void)
  350. {
  351. apic->wait_icr_idle();
  352. }
  353. static inline u32 safe_apic_wait_icr_idle(void)
  354. {
  355. return apic->safe_wait_icr_idle();
  356. }
  357. extern void __init apic_set_eoi_write(void (*eoi_write)(u32 reg, u32 v));
  358. #else /* CONFIG_X86_LOCAL_APIC */
  359. static inline u32 apic_read(u32 reg) { return 0; }
  360. static inline void apic_write(u32 reg, u32 val) { }
  361. static inline void apic_eoi(void) { }
  362. static inline u64 apic_icr_read(void) { return 0; }
  363. static inline void apic_icr_write(u32 low, u32 high) { }
  364. static inline void apic_wait_icr_idle(void) { }
  365. static inline u32 safe_apic_wait_icr_idle(void) { return 0; }
  366. static inline void apic_set_eoi_write(void (*eoi_write)(u32 reg, u32 v)) {}
  367. #endif /* CONFIG_X86_LOCAL_APIC */
  368. static inline void ack_APIC_irq(void)
  369. {
  370. /*
  371. * ack_APIC_irq() actually gets compiled as a single instruction
  372. * ... yummie.
  373. */
  374. apic_eoi();
  375. }
  376. static inline unsigned default_get_apic_id(unsigned long x)
  377. {
  378. unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
  379. if (APIC_XAPIC(ver) || boot_cpu_has(X86_FEATURE_EXTD_APICID))
  380. return (x >> 24) & 0xFF;
  381. else
  382. return (x >> 24) & 0x0F;
  383. }
  384. /*
  385. * Warm reset vector position:
  386. */
  387. #define TRAMPOLINE_PHYS_LOW 0x467
  388. #define TRAMPOLINE_PHYS_HIGH 0x469
  389. #ifdef CONFIG_X86_64
  390. extern void apic_send_IPI_self(int vector);
  391. DECLARE_PER_CPU(int, x2apic_extra_bits);
  392. #endif
  393. extern void generic_bigsmp_probe(void);
  394. #ifdef CONFIG_X86_LOCAL_APIC
  395. #include <asm/smp.h>
  396. #define APIC_DFR_VALUE (APIC_DFR_FLAT)
  397. DECLARE_EARLY_PER_CPU_READ_MOSTLY(u16, x86_bios_cpu_apicid);
  398. extern struct apic apic_noop;
  399. static inline unsigned int read_apic_id(void)
  400. {
  401. unsigned int reg = apic_read(APIC_ID);
  402. return apic->get_apic_id(reg);
  403. }
  404. extern int default_apic_id_valid(u32 apicid);
  405. extern int default_acpi_madt_oem_check(char *, char *);
  406. extern void default_setup_apic_routing(void);
  407. extern u32 apic_default_calc_apicid(unsigned int cpu);
  408. extern u32 apic_flat_calc_apicid(unsigned int cpu);
  409. extern int flat_cpu_mask_to_apicid(const struct cpumask *cpumask,
  410. struct irq_data *irqdata,
  411. unsigned int *apicid);
  412. extern int default_cpu_mask_to_apicid(const struct cpumask *cpumask,
  413. struct irq_data *irqdata,
  414. unsigned int *apicid);
  415. extern bool default_check_apicid_used(physid_mask_t *map, int apicid);
  416. extern void flat_vector_allocation_domain(int cpu, struct cpumask *retmask,
  417. const struct cpumask *mask);
  418. extern void default_vector_allocation_domain(int cpu, struct cpumask *retmask,
  419. const struct cpumask *mask);
  420. extern void default_ioapic_phys_id_map(physid_mask_t *phys_map, physid_mask_t *retmap);
  421. extern int default_cpu_present_to_apicid(int mps_cpu);
  422. extern int default_check_phys_apicid_present(int phys_apicid);
  423. #endif /* CONFIG_X86_LOCAL_APIC */
  424. extern void irq_enter(void);
  425. extern void irq_exit(void);
  426. static inline void entering_irq(void)
  427. {
  428. irq_enter();
  429. }
  430. static inline void entering_ack_irq(void)
  431. {
  432. entering_irq();
  433. ack_APIC_irq();
  434. }
  435. static inline void ipi_entering_ack_irq(void)
  436. {
  437. irq_enter();
  438. ack_APIC_irq();
  439. }
  440. static inline void exiting_irq(void)
  441. {
  442. irq_exit();
  443. }
  444. static inline void exiting_ack_irq(void)
  445. {
  446. ack_APIC_irq();
  447. irq_exit();
  448. }
  449. extern void ioapic_zap_locks(void);
  450. #endif /* _ASM_X86_APIC_H */