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 int APIC_init_uniprocessor(void);
  114. #ifdef CONFIG_X86_64
  115. static inline int apic_force_enable(unsigned long addr)
  116. {
  117. return -1;
  118. }
  119. #else
  120. extern int apic_force_enable(unsigned long addr);
  121. #endif
  122. extern int apic_bsp_setup(bool upmode);
  123. extern void apic_ap_setup(void);
  124. /*
  125. * On 32bit this is mach-xxx local
  126. */
  127. #ifdef CONFIG_X86_64
  128. extern int apic_is_clustered_box(void);
  129. #else
  130. static inline int apic_is_clustered_box(void)
  131. {
  132. return 0;
  133. }
  134. #endif
  135. extern int setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask);
  136. #else /* !CONFIG_X86_LOCAL_APIC */
  137. static inline void lapic_shutdown(void) { }
  138. #define local_apic_timer_c2_ok 1
  139. static inline void init_apic_mappings(void) { }
  140. static inline void disable_local_APIC(void) { }
  141. # define setup_boot_APIC_clock x86_init_noop
  142. # define setup_secondary_APIC_clock x86_init_noop
  143. #endif /* !CONFIG_X86_LOCAL_APIC */
  144. #ifdef CONFIG_X86_X2APIC
  145. /*
  146. * Make previous memory operations globally visible before
  147. * sending the IPI through x2apic wrmsr. We need a serializing instruction or
  148. * mfence for this.
  149. */
  150. static inline void x2apic_wrmsr_fence(void)
  151. {
  152. asm volatile("mfence" : : : "memory");
  153. }
  154. static inline void native_apic_msr_write(u32 reg, u32 v)
  155. {
  156. if (reg == APIC_DFR || reg == APIC_ID || reg == APIC_LDR ||
  157. reg == APIC_LVR)
  158. return;
  159. wrmsr(APIC_BASE_MSR + (reg >> 4), v, 0);
  160. }
  161. static inline void native_apic_msr_eoi_write(u32 reg, u32 v)
  162. {
  163. wrmsr(APIC_BASE_MSR + (APIC_EOI >> 4), APIC_EOI_ACK, 0);
  164. }
  165. static inline u32 native_apic_msr_read(u32 reg)
  166. {
  167. u64 msr;
  168. if (reg == APIC_DFR)
  169. return -1;
  170. rdmsrl(APIC_BASE_MSR + (reg >> 4), msr);
  171. return (u32)msr;
  172. }
  173. static inline void native_x2apic_wait_icr_idle(void)
  174. {
  175. /* no need to wait for icr idle in x2apic */
  176. return;
  177. }
  178. static inline u32 native_safe_x2apic_wait_icr_idle(void)
  179. {
  180. /* no need to wait for icr idle in x2apic */
  181. return 0;
  182. }
  183. static inline void native_x2apic_icr_write(u32 low, u32 id)
  184. {
  185. wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low);
  186. }
  187. static inline u64 native_x2apic_icr_read(void)
  188. {
  189. unsigned long val;
  190. rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val);
  191. return val;
  192. }
  193. extern int x2apic_mode;
  194. extern int x2apic_phys;
  195. extern void __init check_x2apic(void);
  196. extern void x2apic_setup(void);
  197. static inline int x2apic_enabled(void)
  198. {
  199. return cpu_has_x2apic && apic_is_x2apic_enabled();
  200. }
  201. #define x2apic_supported() (cpu_has_x2apic)
  202. #else /* !CONFIG_X86_X2APIC */
  203. static inline void check_x2apic(void) { }
  204. static inline void x2apic_setup(void) { }
  205. static inline int x2apic_enabled(void) { return 0; }
  206. #define x2apic_mode (0)
  207. #define x2apic_supported() (0)
  208. #endif /* !CONFIG_X86_X2APIC */
  209. #ifdef CONFIG_X86_64
  210. #define SET_APIC_ID(x) (apic->set_apic_id(x))
  211. #else
  212. #endif
  213. /*
  214. * Copyright 2004 James Cleverdon, IBM.
  215. * Subject to the GNU Public License, v.2
  216. *
  217. * Generic APIC sub-arch data struct.
  218. *
  219. * Hacked for x86-64 by James Cleverdon from i386 architecture code by
  220. * Martin Bligh, Andi Kleen, James Bottomley, John Stultz, and
  221. * James Cleverdon.
  222. */
  223. struct apic {
  224. char *name;
  225. int (*probe)(void);
  226. int (*acpi_madt_oem_check)(char *oem_id, char *oem_table_id);
  227. int (*apic_id_valid)(int apicid);
  228. int (*apic_id_registered)(void);
  229. u32 irq_delivery_mode;
  230. u32 irq_dest_mode;
  231. const struct cpumask *(*target_cpus)(void);
  232. int disable_esr;
  233. int dest_logical;
  234. unsigned long (*check_apicid_used)(physid_mask_t *map, int apicid);
  235. void (*vector_allocation_domain)(int cpu, struct cpumask *retmask,
  236. const struct cpumask *mask);
  237. void (*init_apic_ldr)(void);
  238. void (*ioapic_phys_id_map)(physid_mask_t *phys_map, physid_mask_t *retmap);
  239. void (*setup_apic_routing)(void);
  240. int (*cpu_present_to_apicid)(int mps_cpu);
  241. void (*apicid_to_cpu_present)(int phys_apicid, physid_mask_t *retmap);
  242. int (*check_phys_apicid_present)(int phys_apicid);
  243. int (*phys_pkg_id)(int cpuid_apic, int index_msb);
  244. unsigned int (*get_apic_id)(unsigned long x);
  245. unsigned long (*set_apic_id)(unsigned int id);
  246. unsigned long apic_id_mask;
  247. int (*cpu_mask_to_apicid_and)(const struct cpumask *cpumask,
  248. const struct cpumask *andmask,
  249. unsigned int *apicid);
  250. /* ipi */
  251. void (*send_IPI)(int cpu, int vector);
  252. void (*send_IPI_mask)(const struct cpumask *mask, int vector);
  253. void (*send_IPI_mask_allbutself)(const struct cpumask *mask,
  254. int vector);
  255. void (*send_IPI_allbutself)(int vector);
  256. void (*send_IPI_all)(int vector);
  257. void (*send_IPI_self)(int vector);
  258. /* wakeup_secondary_cpu */
  259. int (*wakeup_secondary_cpu)(int apicid, unsigned long start_eip);
  260. void (*inquire_remote_apic)(int apicid);
  261. /* apic ops */
  262. u32 (*read)(u32 reg);
  263. void (*write)(u32 reg, u32 v);
  264. /*
  265. * ->eoi_write() has the same signature as ->write().
  266. *
  267. * Drivers can support both ->eoi_write() and ->write() by passing the same
  268. * callback value. Kernel can override ->eoi_write() and fall back
  269. * on write for EOI.
  270. */
  271. void (*eoi_write)(u32 reg, u32 v);
  272. u64 (*icr_read)(void);
  273. void (*icr_write)(u32 low, u32 high);
  274. void (*wait_icr_idle)(void);
  275. u32 (*safe_wait_icr_idle)(void);
  276. #ifdef CONFIG_X86_32
  277. /*
  278. * Called very early during boot from get_smp_config(). It should
  279. * return the logical apicid. x86_[bios]_cpu_to_apicid is
  280. * initialized before this function is called.
  281. *
  282. * If logical apicid can't be determined that early, the function
  283. * may return BAD_APICID. Logical apicid will be configured after
  284. * init_apic_ldr() while bringing up CPUs. Note that NUMA affinity
  285. * won't be applied properly during early boot in this case.
  286. */
  287. int (*x86_32_early_logical_apicid)(int cpu);
  288. #endif
  289. };
  290. /*
  291. * Pointer to the local APIC driver in use on this system (there's
  292. * always just one such driver in use - the kernel decides via an
  293. * early probing process which one it picks - and then sticks to it):
  294. */
  295. extern struct apic *apic;
  296. /*
  297. * APIC drivers are probed based on how they are listed in the .apicdrivers
  298. * section. So the order is important and enforced by the ordering
  299. * of different apic driver files in the Makefile.
  300. *
  301. * For the files having two apic drivers, we use apic_drivers()
  302. * to enforce the order with in them.
  303. */
  304. #define apic_driver(sym) \
  305. static const struct apic *__apicdrivers_##sym __used \
  306. __aligned(sizeof(struct apic *)) \
  307. __section(.apicdrivers) = { &sym }
  308. #define apic_drivers(sym1, sym2) \
  309. static struct apic *__apicdrivers_##sym1##sym2[2] __used \
  310. __aligned(sizeof(struct apic *)) \
  311. __section(.apicdrivers) = { &sym1, &sym2 }
  312. extern struct apic *__apicdrivers[], *__apicdrivers_end[];
  313. /*
  314. * APIC functionality to boot other CPUs - only used on SMP:
  315. */
  316. #ifdef CONFIG_SMP
  317. extern int wakeup_secondary_cpu_via_nmi(int apicid, unsigned long start_eip);
  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. static inline void ack_APIC_irq(void)
  360. {
  361. /*
  362. * ack_APIC_irq() actually gets compiled as a single instruction
  363. * ... yummie.
  364. */
  365. apic_eoi();
  366. }
  367. static inline unsigned default_get_apic_id(unsigned long x)
  368. {
  369. unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
  370. if (APIC_XAPIC(ver) || boot_cpu_has(X86_FEATURE_EXTD_APICID))
  371. return (x >> 24) & 0xFF;
  372. else
  373. return (x >> 24) & 0x0F;
  374. }
  375. /*
  376. * Warm reset vector position:
  377. */
  378. #define TRAMPOLINE_PHYS_LOW 0x467
  379. #define TRAMPOLINE_PHYS_HIGH 0x469
  380. #ifdef CONFIG_X86_64
  381. extern void apic_send_IPI_self(int vector);
  382. DECLARE_PER_CPU(int, x2apic_extra_bits);
  383. extern int default_cpu_present_to_apicid(int mps_cpu);
  384. extern int default_check_phys_apicid_present(int phys_apicid);
  385. #endif
  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. static inline const struct cpumask *default_target_cpus(void)
  391. {
  392. #ifdef CONFIG_SMP
  393. return cpu_online_mask;
  394. #else
  395. return cpumask_of(0);
  396. #endif
  397. }
  398. static inline const struct cpumask *online_target_cpus(void)
  399. {
  400. return cpu_online_mask;
  401. }
  402. DECLARE_EARLY_PER_CPU_READ_MOSTLY(u16, x86_bios_cpu_apicid);
  403. static inline unsigned int read_apic_id(void)
  404. {
  405. unsigned int reg;
  406. reg = apic_read(APIC_ID);
  407. return apic->get_apic_id(reg);
  408. }
  409. static inline int default_apic_id_valid(int apicid)
  410. {
  411. return (apicid < 255);
  412. }
  413. extern int default_acpi_madt_oem_check(char *, char *);
  414. extern void default_setup_apic_routing(void);
  415. extern struct apic apic_noop;
  416. #ifdef CONFIG_X86_32
  417. static inline int noop_x86_32_early_logical_apicid(int cpu)
  418. {
  419. return BAD_APICID;
  420. }
  421. /*
  422. * Set up the logical destination ID.
  423. *
  424. * Intel recommends to set DFR, LDR and TPR before enabling
  425. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  426. * document number 292116). So here it goes...
  427. */
  428. extern void default_init_apic_ldr(void);
  429. static inline int default_apic_id_registered(void)
  430. {
  431. return physid_isset(read_apic_id(), phys_cpu_present_map);
  432. }
  433. static inline int default_phys_pkg_id(int cpuid_apic, int index_msb)
  434. {
  435. return cpuid_apic >> index_msb;
  436. }
  437. #endif
  438. static inline int
  439. flat_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
  440. const struct cpumask *andmask,
  441. unsigned int *apicid)
  442. {
  443. unsigned long cpu_mask = cpumask_bits(cpumask)[0] &
  444. cpumask_bits(andmask)[0] &
  445. cpumask_bits(cpu_online_mask)[0] &
  446. APIC_ALL_CPUS;
  447. if (likely(cpu_mask)) {
  448. *apicid = (unsigned int)cpu_mask;
  449. return 0;
  450. } else {
  451. return -EINVAL;
  452. }
  453. }
  454. extern int
  455. default_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
  456. const struct cpumask *andmask,
  457. unsigned int *apicid);
  458. static inline void
  459. flat_vector_allocation_domain(int cpu, struct cpumask *retmask,
  460. const struct cpumask *mask)
  461. {
  462. /* Careful. Some cpus do not strictly honor the set of cpus
  463. * specified in the interrupt destination when using lowest
  464. * priority interrupt delivery mode.
  465. *
  466. * In particular there was a hyperthreading cpu observed to
  467. * deliver interrupts to the wrong hyperthread when only one
  468. * hyperthread was specified in the interrupt desitination.
  469. */
  470. cpumask_clear(retmask);
  471. cpumask_bits(retmask)[0] = APIC_ALL_CPUS;
  472. }
  473. static inline void
  474. default_vector_allocation_domain(int cpu, struct cpumask *retmask,
  475. const struct cpumask *mask)
  476. {
  477. cpumask_copy(retmask, cpumask_of(cpu));
  478. }
  479. static inline unsigned long default_check_apicid_used(physid_mask_t *map, int apicid)
  480. {
  481. return physid_isset(apicid, *map);
  482. }
  483. static inline void default_ioapic_phys_id_map(physid_mask_t *phys_map, physid_mask_t *retmap)
  484. {
  485. *retmap = *phys_map;
  486. }
  487. static inline int __default_cpu_present_to_apicid(int mps_cpu)
  488. {
  489. if (mps_cpu < nr_cpu_ids && cpu_present(mps_cpu))
  490. return (int)per_cpu(x86_bios_cpu_apicid, mps_cpu);
  491. else
  492. return BAD_APICID;
  493. }
  494. static inline int
  495. __default_check_phys_apicid_present(int phys_apicid)
  496. {
  497. return physid_isset(phys_apicid, phys_cpu_present_map);
  498. }
  499. #ifdef CONFIG_X86_32
  500. static inline int default_cpu_present_to_apicid(int mps_cpu)
  501. {
  502. return __default_cpu_present_to_apicid(mps_cpu);
  503. }
  504. static inline int
  505. default_check_phys_apicid_present(int phys_apicid)
  506. {
  507. return __default_check_phys_apicid_present(phys_apicid);
  508. }
  509. #else
  510. extern int default_cpu_present_to_apicid(int mps_cpu);
  511. extern int default_check_phys_apicid_present(int phys_apicid);
  512. #endif
  513. #endif /* CONFIG_X86_LOCAL_APIC */
  514. extern void irq_enter(void);
  515. extern void irq_exit(void);
  516. static inline void entering_irq(void)
  517. {
  518. irq_enter();
  519. exit_idle();
  520. }
  521. static inline void entering_ack_irq(void)
  522. {
  523. entering_irq();
  524. ack_APIC_irq();
  525. }
  526. static inline void ipi_entering_ack_irq(void)
  527. {
  528. ack_APIC_irq();
  529. irq_enter();
  530. }
  531. static inline void exiting_irq(void)
  532. {
  533. irq_exit();
  534. }
  535. static inline void exiting_ack_irq(void)
  536. {
  537. irq_exit();
  538. /* Ack only at the end to avoid potential reentry */
  539. ack_APIC_irq();
  540. }
  541. extern void ioapic_zap_locks(void);
  542. #endif /* _ASM_X86_APIC_H */