apic.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. #ifndef _ASM_X86_APIC_H
  2. #define _ASM_X86_APIC_H
  3. #include <linux/cpumask.h>
  4. #include <linux/pm.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/idle.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 unsigned int apic_verbosity;
  43. extern int local_apic_timer_c2_ok;
  44. extern int disable_apic;
  45. extern unsigned int lapic_timer_frequency;
  46. #ifdef CONFIG_SMP
  47. extern void __inquire_remote_apic(int apicid);
  48. #else /* CONFIG_SMP */
  49. static inline void __inquire_remote_apic(int apicid)
  50. {
  51. }
  52. #endif /* CONFIG_SMP */
  53. static inline void default_inquire_remote_apic(int apicid)
  54. {
  55. if (apic_verbosity >= APIC_DEBUG)
  56. __inquire_remote_apic(apicid);
  57. }
  58. /*
  59. * With 82489DX we can't rely on apic feature bit
  60. * retrieved via cpuid but still have to deal with
  61. * such an apic chip so we assume that SMP configuration
  62. * is found from MP table (64bit case uses ACPI mostly
  63. * which set smp presence flag as well so we are safe
  64. * to use this helper too).
  65. */
  66. static inline bool apic_from_smp_config(void)
  67. {
  68. return smp_found_config && !disable_apic;
  69. }
  70. /*
  71. * Basic functions accessing APICs.
  72. */
  73. #ifdef CONFIG_PARAVIRT
  74. #include <asm/paravirt.h>
  75. #endif
  76. extern int setup_profiling_timer(unsigned int);
  77. static inline void native_apic_mem_write(u32 reg, u32 v)
  78. {
  79. volatile u32 *addr = (volatile u32 *)(APIC_BASE + reg);
  80. alternative_io("movl %0, %P1", "xchgl %0, %P1", X86_BUG_11AP,
  81. ASM_OUTPUT2("=r" (v), "=m" (*addr)),
  82. ASM_OUTPUT2("0" (v), "m" (*addr)));
  83. }
  84. static inline u32 native_apic_mem_read(u32 reg)
  85. {
  86. return *((volatile u32 *)(APIC_BASE + reg));
  87. }
  88. extern void native_apic_wait_icr_idle(void);
  89. extern u32 native_safe_apic_wait_icr_idle(void);
  90. extern void native_apic_icr_write(u32 low, u32 id);
  91. extern u64 native_apic_icr_read(void);
  92. static inline bool apic_is_x2apic_enabled(void)
  93. {
  94. u64 msr;
  95. if (rdmsrl_safe(MSR_IA32_APICBASE, &msr))
  96. return false;
  97. return msr & X2APIC_ENABLE;
  98. }
  99. extern void enable_IR_x2apic(void);
  100. extern int get_physical_broadcast(void);
  101. extern int lapic_get_maxlvt(void);
  102. extern void clear_local_APIC(void);
  103. extern void disconnect_bsp_APIC(int virt_wire_setup);
  104. extern void disable_local_APIC(void);
  105. extern void lapic_shutdown(void);
  106. extern void sync_Arb_IDs(void);
  107. extern void init_bsp_APIC(void);
  108. extern void setup_local_APIC(void);
  109. extern void init_apic_mappings(void);
  110. void register_lapic_address(unsigned long address);
  111. extern void setup_boot_APIC_clock(void);
  112. extern void setup_secondary_APIC_clock(void);
  113. extern void lapic_update_tsc_freq(void);
  114. extern int APIC_init_uniprocessor(void);
  115. #ifdef CONFIG_X86_64
  116. static inline int apic_force_enable(unsigned long addr)
  117. {
  118. return -1;
  119. }
  120. #else
  121. extern int apic_force_enable(unsigned long addr);
  122. #endif
  123. extern int apic_bsp_setup(bool upmode);
  124. extern void apic_ap_setup(void);
  125. /*
  126. * On 32bit this is mach-xxx local
  127. */
  128. #ifdef CONFIG_X86_64
  129. extern int apic_is_clustered_box(void);
  130. #else
  131. static inline int apic_is_clustered_box(void)
  132. {
  133. return 0;
  134. }
  135. #endif
  136. extern int setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask);
  137. #else /* !CONFIG_X86_LOCAL_APIC */
  138. static inline void lapic_shutdown(void) { }
  139. #define local_apic_timer_c2_ok 1
  140. static inline void init_apic_mappings(void) { }
  141. static inline void disable_local_APIC(void) { }
  142. # define setup_boot_APIC_clock x86_init_noop
  143. # define setup_secondary_APIC_clock x86_init_noop
  144. static inline void lapic_update_tsc_freq(void) { }
  145. #endif /* !CONFIG_X86_LOCAL_APIC */
  146. #ifdef CONFIG_X86_X2APIC
  147. /*
  148. * Make previous memory operations globally visible before
  149. * sending the IPI through x2apic wrmsr. We need a serializing instruction or
  150. * mfence for this.
  151. */
  152. static inline void x2apic_wrmsr_fence(void)
  153. {
  154. asm volatile("mfence" : : : "memory");
  155. }
  156. static inline void native_apic_msr_write(u32 reg, u32 v)
  157. {
  158. if (reg == APIC_DFR || reg == APIC_ID || reg == APIC_LDR ||
  159. reg == APIC_LVR)
  160. return;
  161. wrmsr(APIC_BASE_MSR + (reg >> 4), v, 0);
  162. }
  163. static inline void native_apic_msr_eoi_write(u32 reg, u32 v)
  164. {
  165. wrmsr(APIC_BASE_MSR + (APIC_EOI >> 4), APIC_EOI_ACK, 0);
  166. }
  167. static inline u32 native_apic_msr_read(u32 reg)
  168. {
  169. u64 msr;
  170. if (reg == APIC_DFR)
  171. return -1;
  172. rdmsrl(APIC_BASE_MSR + (reg >> 4), msr);
  173. return (u32)msr;
  174. }
  175. static inline void native_x2apic_wait_icr_idle(void)
  176. {
  177. /* no need to wait for icr idle in x2apic */
  178. return;
  179. }
  180. static inline u32 native_safe_x2apic_wait_icr_idle(void)
  181. {
  182. /* no need to wait for icr idle in x2apic */
  183. return 0;
  184. }
  185. static inline void native_x2apic_icr_write(u32 low, u32 id)
  186. {
  187. wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low);
  188. }
  189. static inline u64 native_x2apic_icr_read(void)
  190. {
  191. unsigned long val;
  192. rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val);
  193. return val;
  194. }
  195. extern int x2apic_mode;
  196. extern int x2apic_phys;
  197. extern void __init check_x2apic(void);
  198. extern void x2apic_setup(void);
  199. static inline int x2apic_enabled(void)
  200. {
  201. return boot_cpu_has(X86_FEATURE_X2APIC) && apic_is_x2apic_enabled();
  202. }
  203. #define x2apic_supported() (boot_cpu_has(X86_FEATURE_X2APIC))
  204. #else /* !CONFIG_X86_X2APIC */
  205. static inline void check_x2apic(void) { }
  206. static inline void x2apic_setup(void) { }
  207. static inline int x2apic_enabled(void) { return 0; }
  208. #define x2apic_mode (0)
  209. #define x2apic_supported() (0)
  210. #endif /* !CONFIG_X86_X2APIC */
  211. #ifdef CONFIG_X86_64
  212. #define SET_APIC_ID(x) (apic->set_apic_id(x))
  213. #else
  214. #endif
  215. /*
  216. * Copyright 2004 James Cleverdon, IBM.
  217. * Subject to the GNU Public License, v.2
  218. *
  219. * Generic APIC sub-arch data struct.
  220. *
  221. * Hacked for x86-64 by James Cleverdon from i386 architecture code by
  222. * Martin Bligh, Andi Kleen, James Bottomley, John Stultz, and
  223. * James Cleverdon.
  224. */
  225. struct apic {
  226. char *name;
  227. int (*probe)(void);
  228. int (*acpi_madt_oem_check)(char *oem_id, char *oem_table_id);
  229. int (*apic_id_valid)(int apicid);
  230. int (*apic_id_registered)(void);
  231. u32 irq_delivery_mode;
  232. u32 irq_dest_mode;
  233. const struct cpumask *(*target_cpus)(void);
  234. int disable_esr;
  235. int dest_logical;
  236. unsigned long (*check_apicid_used)(physid_mask_t *map, int apicid);
  237. void (*vector_allocation_domain)(int cpu, struct cpumask *retmask,
  238. const struct cpumask *mask);
  239. void (*init_apic_ldr)(void);
  240. void (*ioapic_phys_id_map)(physid_mask_t *phys_map, physid_mask_t *retmap);
  241. void (*setup_apic_routing)(void);
  242. int (*cpu_present_to_apicid)(int mps_cpu);
  243. void (*apicid_to_cpu_present)(int phys_apicid, physid_mask_t *retmap);
  244. int (*check_phys_apicid_present)(int phys_apicid);
  245. int (*phys_pkg_id)(int cpuid_apic, int index_msb);
  246. unsigned int (*get_apic_id)(unsigned long x);
  247. unsigned long (*set_apic_id)(unsigned int id);
  248. int (*cpu_mask_to_apicid_and)(const struct cpumask *cpumask,
  249. const struct cpumask *andmask,
  250. unsigned int *apicid);
  251. /* ipi */
  252. void (*send_IPI)(int cpu, int vector);
  253. void (*send_IPI_mask)(const struct cpumask *mask, int vector);
  254. void (*send_IPI_mask_allbutself)(const struct cpumask *mask,
  255. int vector);
  256. void (*send_IPI_allbutself)(int vector);
  257. void (*send_IPI_all)(int vector);
  258. void (*send_IPI_self)(int vector);
  259. /* wakeup_secondary_cpu */
  260. int (*wakeup_secondary_cpu)(int apicid, unsigned long start_eip);
  261. void (*inquire_remote_apic)(int apicid);
  262. /* apic ops */
  263. u32 (*read)(u32 reg);
  264. void (*write)(u32 reg, u32 v);
  265. /*
  266. * ->eoi_write() has the same signature as ->write().
  267. *
  268. * Drivers can support both ->eoi_write() and ->write() by passing the same
  269. * callback value. Kernel can override ->eoi_write() and fall back
  270. * on write for EOI.
  271. */
  272. void (*eoi_write)(u32 reg, u32 v);
  273. u64 (*icr_read)(void);
  274. void (*icr_write)(u32 low, u32 high);
  275. void (*wait_icr_idle)(void);
  276. u32 (*safe_wait_icr_idle)(void);
  277. #ifdef CONFIG_X86_32
  278. /*
  279. * Called very early during boot from get_smp_config(). It should
  280. * return the logical apicid. x86_[bios]_cpu_to_apicid is
  281. * initialized before this function is called.
  282. *
  283. * If logical apicid can't be determined that early, the function
  284. * may return BAD_APICID. Logical apicid will be configured after
  285. * init_apic_ldr() while bringing up CPUs. Note that NUMA affinity
  286. * won't be applied properly during early boot in this case.
  287. */
  288. int (*x86_32_early_logical_apicid)(int cpu);
  289. #endif
  290. };
  291. /*
  292. * Pointer to the local APIC driver in use on this system (there's
  293. * always just one such driver in use - the kernel decides via an
  294. * early probing process which one it picks - and then sticks to it):
  295. */
  296. extern struct apic *apic;
  297. /*
  298. * APIC drivers are probed based on how they are listed in the .apicdrivers
  299. * section. So the order is important and enforced by the ordering
  300. * of different apic driver files in the Makefile.
  301. *
  302. * For the files having two apic drivers, we use apic_drivers()
  303. * to enforce the order with in them.
  304. */
  305. #define apic_driver(sym) \
  306. static const struct apic *__apicdrivers_##sym __used \
  307. __aligned(sizeof(struct apic *)) \
  308. __section(.apicdrivers) = { &sym }
  309. #define apic_drivers(sym1, sym2) \
  310. static struct apic *__apicdrivers_##sym1##sym2[2] __used \
  311. __aligned(sizeof(struct apic *)) \
  312. __section(.apicdrivers) = { &sym1, &sym2 }
  313. extern struct apic *__apicdrivers[], *__apicdrivers_end[];
  314. /*
  315. * APIC functionality to boot other CPUs - only used on SMP:
  316. */
  317. #ifdef CONFIG_SMP
  318. extern int wakeup_secondary_cpu_via_nmi(int apicid, unsigned long start_eip);
  319. #endif
  320. #ifdef CONFIG_X86_LOCAL_APIC
  321. static inline u32 apic_read(u32 reg)
  322. {
  323. return apic->read(reg);
  324. }
  325. static inline void apic_write(u32 reg, u32 val)
  326. {
  327. apic->write(reg, val);
  328. }
  329. static inline void apic_eoi(void)
  330. {
  331. apic->eoi_write(APIC_EOI, APIC_EOI_ACK);
  332. }
  333. static inline u64 apic_icr_read(void)
  334. {
  335. return apic->icr_read();
  336. }
  337. static inline void apic_icr_write(u32 low, u32 high)
  338. {
  339. apic->icr_write(low, high);
  340. }
  341. static inline void apic_wait_icr_idle(void)
  342. {
  343. apic->wait_icr_idle();
  344. }
  345. static inline u32 safe_apic_wait_icr_idle(void)
  346. {
  347. return apic->safe_wait_icr_idle();
  348. }
  349. extern void __init apic_set_eoi_write(void (*eoi_write)(u32 reg, u32 v));
  350. #else /* CONFIG_X86_LOCAL_APIC */
  351. static inline u32 apic_read(u32 reg) { return 0; }
  352. static inline void apic_write(u32 reg, u32 val) { }
  353. static inline void apic_eoi(void) { }
  354. static inline u64 apic_icr_read(void) { return 0; }
  355. static inline void apic_icr_write(u32 low, u32 high) { }
  356. static inline void apic_wait_icr_idle(void) { }
  357. static inline u32 safe_apic_wait_icr_idle(void) { return 0; }
  358. static inline void apic_set_eoi_write(void (*eoi_write)(u32 reg, u32 v)) {}
  359. #endif /* CONFIG_X86_LOCAL_APIC */
  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 unsigned default_get_apic_id(unsigned long x)
  369. {
  370. unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
  371. if (APIC_XAPIC(ver) || boot_cpu_has(X86_FEATURE_EXTD_APICID))
  372. return (x >> 24) & 0xFF;
  373. else
  374. return (x >> 24) & 0x0F;
  375. }
  376. /*
  377. * Warm reset vector position:
  378. */
  379. #define TRAMPOLINE_PHYS_LOW 0x467
  380. #define TRAMPOLINE_PHYS_HIGH 0x469
  381. #ifdef CONFIG_X86_64
  382. extern void apic_send_IPI_self(int vector);
  383. DECLARE_PER_CPU(int, x2apic_extra_bits);
  384. extern int default_cpu_present_to_apicid(int mps_cpu);
  385. extern int default_check_phys_apicid_present(int phys_apicid);
  386. #endif
  387. extern void generic_bigsmp_probe(void);
  388. #ifdef CONFIG_X86_LOCAL_APIC
  389. #include <asm/smp.h>
  390. #define APIC_DFR_VALUE (APIC_DFR_FLAT)
  391. static inline const struct cpumask *default_target_cpus(void)
  392. {
  393. #ifdef CONFIG_SMP
  394. return cpu_online_mask;
  395. #else
  396. return cpumask_of(0);
  397. #endif
  398. }
  399. static inline const struct cpumask *online_target_cpus(void)
  400. {
  401. return cpu_online_mask;
  402. }
  403. DECLARE_EARLY_PER_CPU_READ_MOSTLY(u16, x86_bios_cpu_apicid);
  404. static inline unsigned int read_apic_id(void)
  405. {
  406. unsigned int reg;
  407. reg = apic_read(APIC_ID);
  408. return apic->get_apic_id(reg);
  409. }
  410. static inline int default_apic_id_valid(int apicid)
  411. {
  412. return (apicid < 255);
  413. }
  414. extern int default_acpi_madt_oem_check(char *, char *);
  415. extern void default_setup_apic_routing(void);
  416. extern struct apic apic_noop;
  417. #ifdef CONFIG_X86_32
  418. static inline int noop_x86_32_early_logical_apicid(int cpu)
  419. {
  420. return BAD_APICID;
  421. }
  422. /*
  423. * Set up the logical destination ID.
  424. *
  425. * Intel recommends to set DFR, LDR and TPR before enabling
  426. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  427. * document number 292116). So here it goes...
  428. */
  429. extern void default_init_apic_ldr(void);
  430. static inline int default_apic_id_registered(void)
  431. {
  432. return physid_isset(read_apic_id(), phys_cpu_present_map);
  433. }
  434. static inline int default_phys_pkg_id(int cpuid_apic, int index_msb)
  435. {
  436. return cpuid_apic >> index_msb;
  437. }
  438. #endif
  439. static inline int
  440. flat_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
  441. const struct cpumask *andmask,
  442. unsigned int *apicid)
  443. {
  444. unsigned long cpu_mask = cpumask_bits(cpumask)[0] &
  445. cpumask_bits(andmask)[0] &
  446. cpumask_bits(cpu_online_mask)[0] &
  447. APIC_ALL_CPUS;
  448. if (likely(cpu_mask)) {
  449. *apicid = (unsigned int)cpu_mask;
  450. return 0;
  451. } else {
  452. return -EINVAL;
  453. }
  454. }
  455. extern int
  456. default_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
  457. const struct cpumask *andmask,
  458. unsigned int *apicid);
  459. static inline void
  460. flat_vector_allocation_domain(int cpu, struct cpumask *retmask,
  461. const struct cpumask *mask)
  462. {
  463. /* Careful. Some cpus do not strictly honor the set of cpus
  464. * specified in the interrupt destination when using lowest
  465. * priority interrupt delivery mode.
  466. *
  467. * In particular there was a hyperthreading cpu observed to
  468. * deliver interrupts to the wrong hyperthread when only one
  469. * hyperthread was specified in the interrupt desitination.
  470. */
  471. cpumask_clear(retmask);
  472. cpumask_bits(retmask)[0] = APIC_ALL_CPUS;
  473. }
  474. static inline void
  475. default_vector_allocation_domain(int cpu, struct cpumask *retmask,
  476. const struct cpumask *mask)
  477. {
  478. cpumask_copy(retmask, cpumask_of(cpu));
  479. }
  480. static inline unsigned long default_check_apicid_used(physid_mask_t *map, int apicid)
  481. {
  482. return physid_isset(apicid, *map);
  483. }
  484. static inline void default_ioapic_phys_id_map(physid_mask_t *phys_map, physid_mask_t *retmap)
  485. {
  486. *retmap = *phys_map;
  487. }
  488. static inline int __default_cpu_present_to_apicid(int mps_cpu)
  489. {
  490. if (mps_cpu < nr_cpu_ids && cpu_present(mps_cpu))
  491. return (int)per_cpu(x86_bios_cpu_apicid, mps_cpu);
  492. else
  493. return BAD_APICID;
  494. }
  495. static inline int
  496. __default_check_phys_apicid_present(int phys_apicid)
  497. {
  498. return physid_isset(phys_apicid, phys_cpu_present_map);
  499. }
  500. #ifdef CONFIG_X86_32
  501. static inline int default_cpu_present_to_apicid(int mps_cpu)
  502. {
  503. return __default_cpu_present_to_apicid(mps_cpu);
  504. }
  505. static inline int
  506. default_check_phys_apicid_present(int phys_apicid)
  507. {
  508. return __default_check_phys_apicid_present(phys_apicid);
  509. }
  510. #else
  511. extern int default_cpu_present_to_apicid(int mps_cpu);
  512. extern int default_check_phys_apicid_present(int phys_apicid);
  513. #endif
  514. #endif /* CONFIG_X86_LOCAL_APIC */
  515. extern void irq_enter(void);
  516. extern void irq_exit(void);
  517. static inline void entering_irq(void)
  518. {
  519. irq_enter();
  520. exit_idle();
  521. }
  522. static inline void entering_ack_irq(void)
  523. {
  524. entering_irq();
  525. ack_APIC_irq();
  526. }
  527. static inline void ipi_entering_ack_irq(void)
  528. {
  529. irq_enter();
  530. ack_APIC_irq();
  531. }
  532. static inline void exiting_irq(void)
  533. {
  534. irq_exit();
  535. }
  536. static inline void exiting_ack_irq(void)
  537. {
  538. ack_APIC_irq();
  539. irq_exit();
  540. }
  541. extern void ioapic_zap_locks(void);
  542. #endif /* _ASM_X86_APIC_H */