apic.h 15 KB

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