apic.h 16 KB

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