lapic.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. /*
  2. * Local APIC virtualization
  3. *
  4. * Copyright (C) 2006 Qumranet, Inc.
  5. * Copyright (C) 2007 Novell
  6. * Copyright (C) 2007 Intel
  7. *
  8. * Authors:
  9. * Dor Laor <dor.laor@qumranet.com>
  10. * Gregory Haskins <ghaskins@novell.com>
  11. * Yaozu (Eddie) Dong <eddie.dong@intel.com>
  12. *
  13. * Based on Xen 3.1 code, Copyright (c) 2004, Intel Corporation.
  14. *
  15. * This work is licensed under the terms of the GNU GPL, version 2. See
  16. * the COPYING file in the top-level directory.
  17. */
  18. #include "kvm.h"
  19. #include <linux/kvm.h>
  20. #include <linux/mm.h>
  21. #include <linux/highmem.h>
  22. #include <linux/smp.h>
  23. #include <linux/hrtimer.h>
  24. #include <linux/io.h>
  25. #include <linux/module.h>
  26. #include <asm/processor.h>
  27. #include <asm/msr.h>
  28. #include <asm/page.h>
  29. #include <asm/current.h>
  30. #include <asm/apicdef.h>
  31. #include <asm/atomic.h>
  32. #include <asm/div64.h>
  33. #include "irq.h"
  34. #define PRId64 "d"
  35. #define PRIx64 "llx"
  36. #define PRIu64 "u"
  37. #define PRIo64 "o"
  38. #define APIC_BUS_CYCLE_NS 1
  39. /* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
  40. #define apic_debug(fmt, arg...)
  41. #define APIC_LVT_NUM 6
  42. /* 14 is the version for Xeon and Pentium 8.4.8*/
  43. #define APIC_VERSION (0x14UL | ((APIC_LVT_NUM - 1) << 16))
  44. #define LAPIC_MMIO_LENGTH (1 << 12)
  45. /* followed define is not in apicdef.h */
  46. #define APIC_SHORT_MASK 0xc0000
  47. #define APIC_DEST_NOSHORT 0x0
  48. #define APIC_DEST_MASK 0x800
  49. #define MAX_APIC_VECTOR 256
  50. #define VEC_POS(v) ((v) & (32 - 1))
  51. #define REG_POS(v) (((v) >> 5) << 4)
  52. static inline u32 apic_get_reg(struct kvm_lapic *apic, int reg_off)
  53. {
  54. return *((u32 *) (apic->regs + reg_off));
  55. }
  56. static inline void apic_set_reg(struct kvm_lapic *apic, int reg_off, u32 val)
  57. {
  58. *((u32 *) (apic->regs + reg_off)) = val;
  59. }
  60. static inline int apic_test_and_set_vector(int vec, void *bitmap)
  61. {
  62. return test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  63. }
  64. static inline int apic_test_and_clear_vector(int vec, void *bitmap)
  65. {
  66. return test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  67. }
  68. static inline void apic_set_vector(int vec, void *bitmap)
  69. {
  70. set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  71. }
  72. static inline void apic_clear_vector(int vec, void *bitmap)
  73. {
  74. clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
  75. }
  76. static inline int apic_hw_enabled(struct kvm_lapic *apic)
  77. {
  78. return (apic)->vcpu->apic_base & MSR_IA32_APICBASE_ENABLE;
  79. }
  80. static inline int apic_sw_enabled(struct kvm_lapic *apic)
  81. {
  82. return apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_APIC_ENABLED;
  83. }
  84. static inline int apic_enabled(struct kvm_lapic *apic)
  85. {
  86. return apic_sw_enabled(apic) && apic_hw_enabled(apic);
  87. }
  88. #define LVT_MASK \
  89. (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
  90. #define LINT_MASK \
  91. (LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
  92. APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
  93. static inline int kvm_apic_id(struct kvm_lapic *apic)
  94. {
  95. return (apic_get_reg(apic, APIC_ID) >> 24) & 0xff;
  96. }
  97. static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
  98. {
  99. return !(apic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
  100. }
  101. static inline int apic_lvt_vector(struct kvm_lapic *apic, int lvt_type)
  102. {
  103. return apic_get_reg(apic, lvt_type) & APIC_VECTOR_MASK;
  104. }
  105. static inline int apic_lvtt_period(struct kvm_lapic *apic)
  106. {
  107. return apic_get_reg(apic, APIC_LVTT) & APIC_LVT_TIMER_PERIODIC;
  108. }
  109. static unsigned int apic_lvt_mask[APIC_LVT_NUM] = {
  110. LVT_MASK | APIC_LVT_TIMER_PERIODIC, /* LVTT */
  111. LVT_MASK | APIC_MODE_MASK, /* LVTTHMR */
  112. LVT_MASK | APIC_MODE_MASK, /* LVTPC */
  113. LINT_MASK, LINT_MASK, /* LVT0-1 */
  114. LVT_MASK /* LVTERR */
  115. };
  116. static int find_highest_vector(void *bitmap)
  117. {
  118. u32 *word = bitmap;
  119. int word_offset = MAX_APIC_VECTOR >> 5;
  120. while ((word_offset != 0) && (word[(--word_offset) << 2] == 0))
  121. continue;
  122. if (likely(!word_offset && !word[0]))
  123. return -1;
  124. else
  125. return fls(word[word_offset << 2]) - 1 + (word_offset << 5);
  126. }
  127. static inline int apic_test_and_set_irr(int vec, struct kvm_lapic *apic)
  128. {
  129. return apic_test_and_set_vector(vec, apic->regs + APIC_IRR);
  130. }
  131. static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
  132. {
  133. apic_clear_vector(vec, apic->regs + APIC_IRR);
  134. }
  135. static inline int apic_find_highest_irr(struct kvm_lapic *apic)
  136. {
  137. int result;
  138. result = find_highest_vector(apic->regs + APIC_IRR);
  139. ASSERT(result == -1 || result >= 16);
  140. return result;
  141. }
  142. int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
  143. {
  144. struct kvm_lapic *apic = (struct kvm_lapic *)vcpu->apic;
  145. int highest_irr;
  146. if (!apic)
  147. return 0;
  148. highest_irr = apic_find_highest_irr(apic);
  149. return highest_irr;
  150. }
  151. EXPORT_SYMBOL_GPL(kvm_lapic_find_highest_irr);
  152. int kvm_apic_set_irq(struct kvm_lapic *apic, u8 vec, u8 trig)
  153. {
  154. if (!apic_test_and_set_irr(vec, apic)) {
  155. /* a new pending irq is set in IRR */
  156. if (trig)
  157. apic_set_vector(vec, apic->regs + APIC_TMR);
  158. else
  159. apic_clear_vector(vec, apic->regs + APIC_TMR);
  160. kvm_vcpu_kick(apic->vcpu);
  161. return 1;
  162. }
  163. return 0;
  164. }
  165. static inline int apic_find_highest_isr(struct kvm_lapic *apic)
  166. {
  167. int result;
  168. result = find_highest_vector(apic->regs + APIC_ISR);
  169. ASSERT(result == -1 || result >= 16);
  170. return result;
  171. }
  172. static void apic_update_ppr(struct kvm_lapic *apic)
  173. {
  174. u32 tpr, isrv, ppr;
  175. int isr;
  176. tpr = apic_get_reg(apic, APIC_TASKPRI);
  177. isr = apic_find_highest_isr(apic);
  178. isrv = (isr != -1) ? isr : 0;
  179. if ((tpr & 0xf0) >= (isrv & 0xf0))
  180. ppr = tpr & 0xff;
  181. else
  182. ppr = isrv & 0xf0;
  183. apic_debug("vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x",
  184. apic, ppr, isr, isrv);
  185. apic_set_reg(apic, APIC_PROCPRI, ppr);
  186. }
  187. static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
  188. {
  189. apic_set_reg(apic, APIC_TASKPRI, tpr);
  190. apic_update_ppr(apic);
  191. }
  192. int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest)
  193. {
  194. return kvm_apic_id(apic) == dest;
  195. }
  196. int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda)
  197. {
  198. int result = 0;
  199. u8 logical_id;
  200. logical_id = GET_APIC_LOGICAL_ID(apic_get_reg(apic, APIC_LDR));
  201. switch (apic_get_reg(apic, APIC_DFR)) {
  202. case APIC_DFR_FLAT:
  203. if (logical_id & mda)
  204. result = 1;
  205. break;
  206. case APIC_DFR_CLUSTER:
  207. if (((logical_id >> 4) == (mda >> 0x4))
  208. && (logical_id & mda & 0xf))
  209. result = 1;
  210. break;
  211. default:
  212. printk(KERN_WARNING "Bad DFR vcpu %d: %08x\n",
  213. apic->vcpu->vcpu_id, apic_get_reg(apic, APIC_DFR));
  214. break;
  215. }
  216. return result;
  217. }
  218. static int apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
  219. int short_hand, int dest, int dest_mode)
  220. {
  221. int result = 0;
  222. struct kvm_lapic *target = vcpu->apic;
  223. apic_debug("target %p, source %p, dest 0x%x, "
  224. "dest_mode 0x%x, short_hand 0x%x",
  225. target, source, dest, dest_mode, short_hand);
  226. ASSERT(!target);
  227. switch (short_hand) {
  228. case APIC_DEST_NOSHORT:
  229. if (dest_mode == 0) {
  230. /* Physical mode. */
  231. if ((dest == 0xFF) || (dest == kvm_apic_id(target)))
  232. result = 1;
  233. } else
  234. /* Logical mode. */
  235. result = kvm_apic_match_logical_addr(target, dest);
  236. break;
  237. case APIC_DEST_SELF:
  238. if (target == source)
  239. result = 1;
  240. break;
  241. case APIC_DEST_ALLINC:
  242. result = 1;
  243. break;
  244. case APIC_DEST_ALLBUT:
  245. if (target != source)
  246. result = 1;
  247. break;
  248. default:
  249. printk(KERN_WARNING "Bad dest shorthand value %x\n",
  250. short_hand);
  251. break;
  252. }
  253. return result;
  254. }
  255. /*
  256. * Add a pending IRQ into lapic.
  257. * Return 1 if successfully added and 0 if discarded.
  258. */
  259. static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
  260. int vector, int level, int trig_mode)
  261. {
  262. int orig_irr, result = 0;
  263. struct kvm_vcpu *vcpu = apic->vcpu;
  264. switch (delivery_mode) {
  265. case APIC_DM_FIXED:
  266. case APIC_DM_LOWEST:
  267. /* FIXME add logic for vcpu on reset */
  268. if (unlikely(!apic_enabled(apic)))
  269. break;
  270. orig_irr = apic_test_and_set_irr(vector, apic);
  271. if (orig_irr && trig_mode) {
  272. apic_debug("level trig mode repeatedly for vector %d",
  273. vector);
  274. break;
  275. }
  276. if (trig_mode) {
  277. apic_debug("level trig mode for vector %d", vector);
  278. apic_set_vector(vector, apic->regs + APIC_TMR);
  279. } else
  280. apic_clear_vector(vector, apic->regs + APIC_TMR);
  281. if (vcpu->mp_state == VCPU_MP_STATE_RUNNABLE)
  282. kvm_vcpu_kick(vcpu);
  283. else if (vcpu->mp_state == VCPU_MP_STATE_HALTED) {
  284. vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
  285. if (waitqueue_active(&vcpu->wq))
  286. wake_up_interruptible(&vcpu->wq);
  287. }
  288. result = (orig_irr == 0);
  289. break;
  290. case APIC_DM_REMRD:
  291. printk(KERN_DEBUG "Ignoring delivery mode 3\n");
  292. break;
  293. case APIC_DM_SMI:
  294. printk(KERN_DEBUG "Ignoring guest SMI\n");
  295. break;
  296. case APIC_DM_NMI:
  297. printk(KERN_DEBUG "Ignoring guest NMI\n");
  298. break;
  299. case APIC_DM_INIT:
  300. if (level) {
  301. if (vcpu->mp_state == VCPU_MP_STATE_RUNNABLE)
  302. printk(KERN_DEBUG
  303. "INIT on a runnable vcpu %d\n",
  304. vcpu->vcpu_id);
  305. vcpu->mp_state = VCPU_MP_STATE_INIT_RECEIVED;
  306. kvm_vcpu_kick(vcpu);
  307. } else {
  308. printk(KERN_DEBUG
  309. "Ignoring de-assert INIT to vcpu %d\n",
  310. vcpu->vcpu_id);
  311. }
  312. break;
  313. case APIC_DM_STARTUP:
  314. printk(KERN_DEBUG "SIPI to vcpu %d vector 0x%02x\n",
  315. vcpu->vcpu_id, vector);
  316. if (vcpu->mp_state == VCPU_MP_STATE_INIT_RECEIVED) {
  317. vcpu->sipi_vector = vector;
  318. vcpu->mp_state = VCPU_MP_STATE_SIPI_RECEIVED;
  319. if (waitqueue_active(&vcpu->wq))
  320. wake_up_interruptible(&vcpu->wq);
  321. }
  322. break;
  323. default:
  324. printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
  325. delivery_mode);
  326. break;
  327. }
  328. return result;
  329. }
  330. struct kvm_lapic *kvm_apic_round_robin(struct kvm *kvm, u8 vector,
  331. unsigned long bitmap)
  332. {
  333. int vcpu_id;
  334. int last;
  335. int next;
  336. struct kvm_lapic *apic;
  337. last = kvm->round_robin_prev_vcpu;
  338. next = last;
  339. do {
  340. if (++next == KVM_MAX_VCPUS)
  341. next = 0;
  342. if (kvm->vcpus[next] == NULL || !test_bit(next, &bitmap))
  343. continue;
  344. apic = kvm->vcpus[next]->apic;
  345. if (apic && apic_enabled(apic))
  346. break;
  347. apic = NULL;
  348. } while (next != last);
  349. kvm->round_robin_prev_vcpu = next;
  350. if (!apic) {
  351. vcpu_id = ffs(bitmap) - 1;
  352. if (vcpu_id < 0) {
  353. vcpu_id = 0;
  354. printk(KERN_DEBUG "vcpu not ready for apic_round_robin\n");
  355. }
  356. apic = kvm->vcpus[vcpu_id]->apic;
  357. }
  358. return apic;
  359. }
  360. static void apic_set_eoi(struct kvm_lapic *apic)
  361. {
  362. int vector = apic_find_highest_isr(apic);
  363. /*
  364. * Not every write EOI will has corresponding ISR,
  365. * one example is when Kernel check timer on setup_IO_APIC
  366. */
  367. if (vector == -1)
  368. return;
  369. apic_clear_vector(vector, apic->regs + APIC_ISR);
  370. apic_update_ppr(apic);
  371. if (apic_test_and_clear_vector(vector, apic->regs + APIC_TMR))
  372. kvm_ioapic_update_eoi(apic->vcpu->kvm, vector);
  373. }
  374. static void apic_send_ipi(struct kvm_lapic *apic)
  375. {
  376. u32 icr_low = apic_get_reg(apic, APIC_ICR);
  377. u32 icr_high = apic_get_reg(apic, APIC_ICR2);
  378. unsigned int dest = GET_APIC_DEST_FIELD(icr_high);
  379. unsigned int short_hand = icr_low & APIC_SHORT_MASK;
  380. unsigned int trig_mode = icr_low & APIC_INT_LEVELTRIG;
  381. unsigned int level = icr_low & APIC_INT_ASSERT;
  382. unsigned int dest_mode = icr_low & APIC_DEST_MASK;
  383. unsigned int delivery_mode = icr_low & APIC_MODE_MASK;
  384. unsigned int vector = icr_low & APIC_VECTOR_MASK;
  385. struct kvm_lapic *target;
  386. struct kvm_vcpu *vcpu;
  387. unsigned long lpr_map = 0;
  388. int i;
  389. apic_debug("icr_high 0x%x, icr_low 0x%x, "
  390. "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, "
  391. "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x\n",
  392. icr_high, icr_low, short_hand, dest,
  393. trig_mode, level, dest_mode, delivery_mode, vector);
  394. for (i = 0; i < KVM_MAX_VCPUS; i++) {
  395. vcpu = apic->vcpu->kvm->vcpus[i];
  396. if (!vcpu)
  397. continue;
  398. if (vcpu->apic &&
  399. apic_match_dest(vcpu, apic, short_hand, dest, dest_mode)) {
  400. if (delivery_mode == APIC_DM_LOWEST)
  401. set_bit(vcpu->vcpu_id, &lpr_map);
  402. else
  403. __apic_accept_irq(vcpu->apic, delivery_mode,
  404. vector, level, trig_mode);
  405. }
  406. }
  407. if (delivery_mode == APIC_DM_LOWEST) {
  408. target = kvm_apic_round_robin(vcpu->kvm, vector, lpr_map);
  409. if (target != NULL)
  410. __apic_accept_irq(target, delivery_mode,
  411. vector, level, trig_mode);
  412. }
  413. }
  414. static u32 apic_get_tmcct(struct kvm_lapic *apic)
  415. {
  416. u64 counter_passed;
  417. ktime_t passed, now;
  418. u32 tmcct;
  419. ASSERT(apic != NULL);
  420. now = apic->timer.dev.base->get_time();
  421. tmcct = apic_get_reg(apic, APIC_TMICT);
  422. /* if initial count is 0, current count should also be 0 */
  423. if (tmcct == 0)
  424. return 0;
  425. if (unlikely(ktime_to_ns(now) <=
  426. ktime_to_ns(apic->timer.last_update))) {
  427. /* Wrap around */
  428. passed = ktime_add(( {
  429. (ktime_t) {
  430. .tv64 = KTIME_MAX -
  431. (apic->timer.last_update).tv64}; }
  432. ), now);
  433. apic_debug("time elapsed\n");
  434. } else
  435. passed = ktime_sub(now, apic->timer.last_update);
  436. counter_passed = div64_64(ktime_to_ns(passed),
  437. (APIC_BUS_CYCLE_NS * apic->timer.divide_count));
  438. if (counter_passed > tmcct) {
  439. if (unlikely(!apic_lvtt_period(apic))) {
  440. /* one-shot timers stick at 0 until reset */
  441. tmcct = 0;
  442. } else {
  443. /*
  444. * periodic timers reset to APIC_TMICT when they
  445. * hit 0. The while loop simulates this happening N
  446. * times. (counter_passed %= tmcct) would also work,
  447. * but might be slower or not work on 32-bit??
  448. */
  449. while (counter_passed > tmcct)
  450. counter_passed -= tmcct;
  451. tmcct -= counter_passed;
  452. }
  453. } else {
  454. tmcct -= counter_passed;
  455. }
  456. return tmcct;
  457. }
  458. static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
  459. {
  460. u32 val = 0;
  461. if (offset >= LAPIC_MMIO_LENGTH)
  462. return 0;
  463. switch (offset) {
  464. case APIC_ARBPRI:
  465. printk(KERN_WARNING "Access APIC ARBPRI register "
  466. "which is for P6\n");
  467. break;
  468. case APIC_TMCCT: /* Timer CCR */
  469. val = apic_get_tmcct(apic);
  470. break;
  471. default:
  472. apic_update_ppr(apic);
  473. val = apic_get_reg(apic, offset);
  474. break;
  475. }
  476. return val;
  477. }
  478. static void apic_mmio_read(struct kvm_io_device *this,
  479. gpa_t address, int len, void *data)
  480. {
  481. struct kvm_lapic *apic = (struct kvm_lapic *)this->private;
  482. unsigned int offset = address - apic->base_address;
  483. unsigned char alignment = offset & 0xf;
  484. u32 result;
  485. if ((alignment + len) > 4) {
  486. printk(KERN_ERR "KVM_APIC_READ: alignment error %lx %d",
  487. (unsigned long)address, len);
  488. return;
  489. }
  490. result = __apic_read(apic, offset & ~0xf);
  491. switch (len) {
  492. case 1:
  493. case 2:
  494. case 4:
  495. memcpy(data, (char *)&result + alignment, len);
  496. break;
  497. default:
  498. printk(KERN_ERR "Local APIC read with len = %x, "
  499. "should be 1,2, or 4 instead\n", len);
  500. break;
  501. }
  502. }
  503. static void update_divide_count(struct kvm_lapic *apic)
  504. {
  505. u32 tmp1, tmp2, tdcr;
  506. tdcr = apic_get_reg(apic, APIC_TDCR);
  507. tmp1 = tdcr & 0xf;
  508. tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
  509. apic->timer.divide_count = 0x1 << (tmp2 & 0x7);
  510. apic_debug("timer divide count is 0x%x\n",
  511. apic->timer.divide_count);
  512. }
  513. static void start_apic_timer(struct kvm_lapic *apic)
  514. {
  515. ktime_t now = apic->timer.dev.base->get_time();
  516. apic->timer.last_update = now;
  517. apic->timer.period = apic_get_reg(apic, APIC_TMICT) *
  518. APIC_BUS_CYCLE_NS * apic->timer.divide_count;
  519. atomic_set(&apic->timer.pending, 0);
  520. hrtimer_start(&apic->timer.dev,
  521. ktime_add_ns(now, apic->timer.period),
  522. HRTIMER_MODE_ABS);
  523. apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
  524. PRIx64 ", "
  525. "timer initial count 0x%x, period %lldns, "
  526. "expire @ 0x%016" PRIx64 ".\n", __FUNCTION__,
  527. APIC_BUS_CYCLE_NS, ktime_to_ns(now),
  528. apic_get_reg(apic, APIC_TMICT),
  529. apic->timer.period,
  530. ktime_to_ns(ktime_add_ns(now,
  531. apic->timer.period)));
  532. }
  533. static void apic_mmio_write(struct kvm_io_device *this,
  534. gpa_t address, int len, const void *data)
  535. {
  536. struct kvm_lapic *apic = (struct kvm_lapic *)this->private;
  537. unsigned int offset = address - apic->base_address;
  538. unsigned char alignment = offset & 0xf;
  539. u32 val;
  540. /*
  541. * APIC register must be aligned on 128-bits boundary.
  542. * 32/64/128 bits registers must be accessed thru 32 bits.
  543. * Refer SDM 8.4.1
  544. */
  545. if (len != 4 || alignment) {
  546. if (printk_ratelimit())
  547. printk(KERN_ERR "apic write: bad size=%d %lx\n",
  548. len, (long)address);
  549. return;
  550. }
  551. val = *(u32 *) data;
  552. /* too common printing */
  553. if (offset != APIC_EOI)
  554. apic_debug("%s: offset 0x%x with length 0x%x, and value is "
  555. "0x%x\n", __FUNCTION__, offset, len, val);
  556. offset &= 0xff0;
  557. switch (offset) {
  558. case APIC_ID: /* Local APIC ID */
  559. apic_set_reg(apic, APIC_ID, val);
  560. break;
  561. case APIC_TASKPRI:
  562. apic_set_tpr(apic, val & 0xff);
  563. break;
  564. case APIC_EOI:
  565. apic_set_eoi(apic);
  566. break;
  567. case APIC_LDR:
  568. apic_set_reg(apic, APIC_LDR, val & APIC_LDR_MASK);
  569. break;
  570. case APIC_DFR:
  571. apic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
  572. break;
  573. case APIC_SPIV:
  574. apic_set_reg(apic, APIC_SPIV, val & 0x3ff);
  575. if (!(val & APIC_SPIV_APIC_ENABLED)) {
  576. int i;
  577. u32 lvt_val;
  578. for (i = 0; i < APIC_LVT_NUM; i++) {
  579. lvt_val = apic_get_reg(apic,
  580. APIC_LVTT + 0x10 * i);
  581. apic_set_reg(apic, APIC_LVTT + 0x10 * i,
  582. lvt_val | APIC_LVT_MASKED);
  583. }
  584. atomic_set(&apic->timer.pending, 0);
  585. }
  586. break;
  587. case APIC_ICR:
  588. /* No delay here, so we always clear the pending bit */
  589. apic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
  590. apic_send_ipi(apic);
  591. break;
  592. case APIC_ICR2:
  593. apic_set_reg(apic, APIC_ICR2, val & 0xff000000);
  594. break;
  595. case APIC_LVTT:
  596. case APIC_LVTTHMR:
  597. case APIC_LVTPC:
  598. case APIC_LVT0:
  599. case APIC_LVT1:
  600. case APIC_LVTERR:
  601. /* TODO: Check vector */
  602. if (!apic_sw_enabled(apic))
  603. val |= APIC_LVT_MASKED;
  604. val &= apic_lvt_mask[(offset - APIC_LVTT) >> 4];
  605. apic_set_reg(apic, offset, val);
  606. break;
  607. case APIC_TMICT:
  608. hrtimer_cancel(&apic->timer.dev);
  609. apic_set_reg(apic, APIC_TMICT, val);
  610. start_apic_timer(apic);
  611. return;
  612. case APIC_TDCR:
  613. if (val & 4)
  614. printk(KERN_ERR "KVM_WRITE:TDCR %x\n", val);
  615. apic_set_reg(apic, APIC_TDCR, val);
  616. update_divide_count(apic);
  617. break;
  618. default:
  619. apic_debug("Local APIC Write to read-only register %x\n",
  620. offset);
  621. break;
  622. }
  623. }
  624. static int apic_mmio_range(struct kvm_io_device *this, gpa_t addr)
  625. {
  626. struct kvm_lapic *apic = (struct kvm_lapic *)this->private;
  627. int ret = 0;
  628. if (apic_hw_enabled(apic) &&
  629. (addr >= apic->base_address) &&
  630. (addr < (apic->base_address + LAPIC_MMIO_LENGTH)))
  631. ret = 1;
  632. return ret;
  633. }
  634. void kvm_free_apic(struct kvm_lapic *apic)
  635. {
  636. if (!apic)
  637. return;
  638. hrtimer_cancel(&apic->timer.dev);
  639. if (apic->regs_page) {
  640. __free_page(apic->regs_page);
  641. apic->regs_page = 0;
  642. }
  643. kfree(apic);
  644. }
  645. /*
  646. *----------------------------------------------------------------------
  647. * LAPIC interface
  648. *----------------------------------------------------------------------
  649. */
  650. void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
  651. {
  652. struct kvm_lapic *apic = (struct kvm_lapic *)vcpu->apic;
  653. if (!apic)
  654. return;
  655. apic_set_tpr(apic, ((cr8 & 0x0f) << 4));
  656. }
  657. u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
  658. {
  659. struct kvm_lapic *apic = (struct kvm_lapic *)vcpu->apic;
  660. u64 tpr;
  661. if (!apic)
  662. return 0;
  663. tpr = (u64) apic_get_reg(apic, APIC_TASKPRI);
  664. return (tpr & 0xf0) >> 4;
  665. }
  666. EXPORT_SYMBOL_GPL(kvm_lapic_get_cr8);
  667. void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
  668. {
  669. struct kvm_lapic *apic = (struct kvm_lapic *)vcpu->apic;
  670. if (!apic) {
  671. value |= MSR_IA32_APICBASE_BSP;
  672. vcpu->apic_base = value;
  673. return;
  674. }
  675. if (apic->vcpu->vcpu_id)
  676. value &= ~MSR_IA32_APICBASE_BSP;
  677. vcpu->apic_base = value;
  678. apic->base_address = apic->vcpu->apic_base &
  679. MSR_IA32_APICBASE_BASE;
  680. /* with FSB delivery interrupt, we can restart APIC functionality */
  681. apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
  682. "0x%lx.\n", apic->apic_base, apic->base_address);
  683. }
  684. u64 kvm_lapic_get_base(struct kvm_vcpu *vcpu)
  685. {
  686. return vcpu->apic_base;
  687. }
  688. EXPORT_SYMBOL_GPL(kvm_lapic_get_base);
  689. void kvm_lapic_reset(struct kvm_vcpu *vcpu)
  690. {
  691. struct kvm_lapic *apic;
  692. int i;
  693. apic_debug("%s\n", __FUNCTION__);
  694. ASSERT(vcpu);
  695. apic = vcpu->apic;
  696. ASSERT(apic != NULL);
  697. /* Stop the timer in case it's a reset to an active apic */
  698. hrtimer_cancel(&apic->timer.dev);
  699. apic_set_reg(apic, APIC_ID, vcpu->vcpu_id << 24);
  700. apic_set_reg(apic, APIC_LVR, APIC_VERSION);
  701. for (i = 0; i < APIC_LVT_NUM; i++)
  702. apic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
  703. apic_set_reg(apic, APIC_LVT0,
  704. SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
  705. apic_set_reg(apic, APIC_DFR, 0xffffffffU);
  706. apic_set_reg(apic, APIC_SPIV, 0xff);
  707. apic_set_reg(apic, APIC_TASKPRI, 0);
  708. apic_set_reg(apic, APIC_LDR, 0);
  709. apic_set_reg(apic, APIC_ESR, 0);
  710. apic_set_reg(apic, APIC_ICR, 0);
  711. apic_set_reg(apic, APIC_ICR2, 0);
  712. apic_set_reg(apic, APIC_TDCR, 0);
  713. apic_set_reg(apic, APIC_TMICT, 0);
  714. for (i = 0; i < 8; i++) {
  715. apic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
  716. apic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
  717. apic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
  718. }
  719. update_divide_count(apic);
  720. atomic_set(&apic->timer.pending, 0);
  721. if (vcpu->vcpu_id == 0)
  722. vcpu->apic_base |= MSR_IA32_APICBASE_BSP;
  723. apic_update_ppr(apic);
  724. apic_debug(KERN_INFO "%s: vcpu=%p, id=%d, base_msr="
  725. "0x%016" PRIx64 ", base_address=0x%0lx.\n", __FUNCTION__,
  726. vcpu, kvm_apic_id(apic),
  727. vcpu->apic_base, apic->base_address);
  728. }
  729. EXPORT_SYMBOL_GPL(kvm_lapic_reset);
  730. int kvm_lapic_enabled(struct kvm_vcpu *vcpu)
  731. {
  732. struct kvm_lapic *apic = (struct kvm_lapic *)vcpu->apic;
  733. int ret = 0;
  734. if (!apic)
  735. return 0;
  736. ret = apic_enabled(apic);
  737. return ret;
  738. }
  739. EXPORT_SYMBOL_GPL(kvm_lapic_enabled);
  740. /*
  741. *----------------------------------------------------------------------
  742. * timer interface
  743. *----------------------------------------------------------------------
  744. */
  745. /* TODO: make sure __apic_timer_fn runs in current pCPU */
  746. static int __apic_timer_fn(struct kvm_lapic *apic)
  747. {
  748. int result = 0;
  749. wait_queue_head_t *q = &apic->vcpu->wq;
  750. atomic_inc(&apic->timer.pending);
  751. if (waitqueue_active(q))
  752. {
  753. apic->vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
  754. wake_up_interruptible(q);
  755. }
  756. if (apic_lvtt_period(apic)) {
  757. result = 1;
  758. apic->timer.dev.expires = ktime_add_ns(
  759. apic->timer.dev.expires,
  760. apic->timer.period);
  761. }
  762. return result;
  763. }
  764. static int __inject_apic_timer_irq(struct kvm_lapic *apic)
  765. {
  766. int vector;
  767. vector = apic_lvt_vector(apic, APIC_LVTT);
  768. return __apic_accept_irq(apic, APIC_DM_FIXED, vector, 1, 0);
  769. }
  770. static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
  771. {
  772. struct kvm_lapic *apic;
  773. int restart_timer = 0;
  774. apic = container_of(data, struct kvm_lapic, timer.dev);
  775. restart_timer = __apic_timer_fn(apic);
  776. if (restart_timer)
  777. return HRTIMER_RESTART;
  778. else
  779. return HRTIMER_NORESTART;
  780. }
  781. int kvm_create_lapic(struct kvm_vcpu *vcpu)
  782. {
  783. struct kvm_lapic *apic;
  784. ASSERT(vcpu != NULL);
  785. apic_debug("apic_init %d\n", vcpu->vcpu_id);
  786. apic = kzalloc(sizeof(*apic), GFP_KERNEL);
  787. if (!apic)
  788. goto nomem;
  789. vcpu->apic = apic;
  790. apic->regs_page = alloc_page(GFP_KERNEL);
  791. if (apic->regs_page == NULL) {
  792. printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
  793. vcpu->vcpu_id);
  794. goto nomem;
  795. }
  796. apic->regs = page_address(apic->regs_page);
  797. memset(apic->regs, 0, PAGE_SIZE);
  798. apic->vcpu = vcpu;
  799. hrtimer_init(&apic->timer.dev, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  800. apic->timer.dev.function = apic_timer_fn;
  801. apic->base_address = APIC_DEFAULT_PHYS_BASE;
  802. vcpu->apic_base = APIC_DEFAULT_PHYS_BASE;
  803. kvm_lapic_reset(vcpu);
  804. apic->dev.read = apic_mmio_read;
  805. apic->dev.write = apic_mmio_write;
  806. apic->dev.in_range = apic_mmio_range;
  807. apic->dev.private = apic;
  808. return 0;
  809. nomem:
  810. kvm_free_apic(apic);
  811. return -ENOMEM;
  812. }
  813. EXPORT_SYMBOL_GPL(kvm_create_lapic);
  814. int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
  815. {
  816. struct kvm_lapic *apic = vcpu->apic;
  817. int highest_irr;
  818. if (!apic || !apic_enabled(apic))
  819. return -1;
  820. apic_update_ppr(apic);
  821. highest_irr = apic_find_highest_irr(apic);
  822. if ((highest_irr == -1) ||
  823. ((highest_irr & 0xF0) <= apic_get_reg(apic, APIC_PROCPRI)))
  824. return -1;
  825. return highest_irr;
  826. }
  827. int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu)
  828. {
  829. u32 lvt0 = apic_get_reg(vcpu->apic, APIC_LVT0);
  830. int r = 0;
  831. if (vcpu->vcpu_id == 0) {
  832. if (!apic_hw_enabled(vcpu->apic))
  833. r = 1;
  834. if ((lvt0 & APIC_LVT_MASKED) == 0 &&
  835. GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
  836. r = 1;
  837. }
  838. return r;
  839. }
  840. void kvm_inject_apic_timer_irqs(struct kvm_vcpu *vcpu)
  841. {
  842. struct kvm_lapic *apic = vcpu->apic;
  843. if (apic && apic_lvt_enabled(apic, APIC_LVTT) &&
  844. atomic_read(&apic->timer.pending) > 0) {
  845. if (__inject_apic_timer_irq(apic))
  846. atomic_dec(&apic->timer.pending);
  847. }
  848. }
  849. void kvm_apic_timer_intr_post(struct kvm_vcpu *vcpu, int vec)
  850. {
  851. struct kvm_lapic *apic = vcpu->apic;
  852. if (apic && apic_lvt_vector(apic, APIC_LVTT) == vec)
  853. apic->timer.last_update = ktime_add_ns(
  854. apic->timer.last_update,
  855. apic->timer.period);
  856. }
  857. int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
  858. {
  859. int vector = kvm_apic_has_interrupt(vcpu);
  860. struct kvm_lapic *apic = vcpu->apic;
  861. if (vector == -1)
  862. return -1;
  863. apic_set_vector(vector, apic->regs + APIC_ISR);
  864. apic_update_ppr(apic);
  865. apic_clear_irr(vector, apic);
  866. return vector;
  867. }
  868. void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu)
  869. {
  870. struct kvm_lapic *apic = vcpu->apic;
  871. apic->base_address = vcpu->apic_base &
  872. MSR_IA32_APICBASE_BASE;
  873. apic_set_reg(apic, APIC_LVR, APIC_VERSION);
  874. apic_update_ppr(apic);
  875. hrtimer_cancel(&apic->timer.dev);
  876. update_divide_count(apic);
  877. start_apic_timer(apic);
  878. }
  879. void kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
  880. {
  881. struct kvm_lapic *apic = vcpu->apic;
  882. struct hrtimer *timer;
  883. if (!apic)
  884. return;
  885. timer = &apic->timer.dev;
  886. if (hrtimer_cancel(timer))
  887. hrtimer_start(timer, timer->expires, HRTIMER_MODE_ABS);
  888. }
  889. EXPORT_SYMBOL_GPL(kvm_migrate_apic_timer);