apic.h 15 KB

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