lapic.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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_apic_set_irq(struct kvm_lapic *apic, u8 vec, u8 trig)
  143. {
  144. if (!apic_test_and_set_irr(vec, apic)) {
  145. /* a new pending irq is set in IRR */
  146. if (trig)
  147. apic_set_vector(vec, apic->regs + APIC_TMR);
  148. else
  149. apic_clear_vector(vec, apic->regs + APIC_TMR);
  150. kvm_vcpu_kick(apic->vcpu);
  151. return 1;
  152. }
  153. return 0;
  154. }
  155. static inline int apic_find_highest_isr(struct kvm_lapic *apic)
  156. {
  157. int result;
  158. result = find_highest_vector(apic->regs + APIC_ISR);
  159. ASSERT(result == -1 || result >= 16);
  160. return result;
  161. }
  162. static void apic_update_ppr(struct kvm_lapic *apic)
  163. {
  164. u32 tpr, isrv, ppr;
  165. int isr;
  166. tpr = apic_get_reg(apic, APIC_TASKPRI);
  167. isr = apic_find_highest_isr(apic);
  168. isrv = (isr != -1) ? isr : 0;
  169. if ((tpr & 0xf0) >= (isrv & 0xf0))
  170. ppr = tpr & 0xff;
  171. else
  172. ppr = isrv & 0xf0;
  173. apic_debug("vlapic %p, ppr 0x%x, isr 0x%x, isrv 0x%x",
  174. apic, ppr, isr, isrv);
  175. apic_set_reg(apic, APIC_PROCPRI, ppr);
  176. }
  177. static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
  178. {
  179. apic_set_reg(apic, APIC_TASKPRI, tpr);
  180. apic_update_ppr(apic);
  181. }
  182. int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest)
  183. {
  184. return kvm_apic_id(apic) == dest;
  185. }
  186. int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda)
  187. {
  188. int result = 0;
  189. u8 logical_id;
  190. logical_id = GET_APIC_LOGICAL_ID(apic_get_reg(apic, APIC_LDR));
  191. switch (apic_get_reg(apic, APIC_DFR)) {
  192. case APIC_DFR_FLAT:
  193. if (logical_id & mda)
  194. result = 1;
  195. break;
  196. case APIC_DFR_CLUSTER:
  197. if (((logical_id >> 4) == (mda >> 0x4))
  198. && (logical_id & mda & 0xf))
  199. result = 1;
  200. break;
  201. default:
  202. printk(KERN_WARNING "Bad DFR vcpu %d: %08x\n",
  203. apic->vcpu->vcpu_id, apic_get_reg(apic, APIC_DFR));
  204. break;
  205. }
  206. return result;
  207. }
  208. static int apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
  209. int short_hand, int dest, int dest_mode)
  210. {
  211. int result = 0;
  212. struct kvm_lapic *target = vcpu->apic;
  213. apic_debug("target %p, source %p, dest 0x%x, "
  214. "dest_mode 0x%x, short_hand 0x%x",
  215. target, source, dest, dest_mode, short_hand);
  216. ASSERT(!target);
  217. switch (short_hand) {
  218. case APIC_DEST_NOSHORT:
  219. if (dest_mode == 0) {
  220. /* Physical mode. */
  221. if ((dest == 0xFF) || (dest == kvm_apic_id(target)))
  222. result = 1;
  223. } else
  224. /* Logical mode. */
  225. result = kvm_apic_match_logical_addr(target, dest);
  226. break;
  227. case APIC_DEST_SELF:
  228. if (target == source)
  229. result = 1;
  230. break;
  231. case APIC_DEST_ALLINC:
  232. result = 1;
  233. break;
  234. case APIC_DEST_ALLBUT:
  235. if (target != source)
  236. result = 1;
  237. break;
  238. default:
  239. printk(KERN_WARNING "Bad dest shorthand value %x\n",
  240. short_hand);
  241. break;
  242. }
  243. return result;
  244. }
  245. /*
  246. * Add a pending IRQ into lapic.
  247. * Return 1 if successfully added and 0 if discarded.
  248. */
  249. static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
  250. int vector, int level, int trig_mode)
  251. {
  252. int result = 0;
  253. switch (delivery_mode) {
  254. case APIC_DM_FIXED:
  255. case APIC_DM_LOWEST:
  256. /* FIXME add logic for vcpu on reset */
  257. if (unlikely(!apic_enabled(apic)))
  258. break;
  259. if (apic_test_and_set_irr(vector, apic) && trig_mode) {
  260. apic_debug("level trig mode repeatedly for vector %d",
  261. vector);
  262. break;
  263. }
  264. if (trig_mode) {
  265. apic_debug("level trig mode for vector %d", vector);
  266. apic_set_vector(vector, apic->regs + APIC_TMR);
  267. } else
  268. apic_clear_vector(vector, apic->regs + APIC_TMR);
  269. kvm_vcpu_kick(apic->vcpu);
  270. result = 1;
  271. break;
  272. case APIC_DM_REMRD:
  273. printk(KERN_DEBUG "Ignoring delivery mode 3\n");
  274. break;
  275. case APIC_DM_SMI:
  276. printk(KERN_DEBUG "Ignoring guest SMI\n");
  277. break;
  278. case APIC_DM_NMI:
  279. printk(KERN_DEBUG "Ignoring guest NMI\n");
  280. break;
  281. case APIC_DM_INIT:
  282. printk(KERN_DEBUG "Ignoring guest INIT\n");
  283. break;
  284. case APIC_DM_STARTUP:
  285. printk(KERN_DEBUG "Ignoring guest STARTUP\n");
  286. break;
  287. default:
  288. printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
  289. delivery_mode);
  290. break;
  291. }
  292. return result;
  293. }
  294. struct kvm_lapic *kvm_apic_round_robin(struct kvm *kvm, u8 vector,
  295. unsigned long bitmap)
  296. {
  297. int vcpu_id;
  298. /* TODO for real round robin */
  299. vcpu_id = fls(bitmap) - 1;
  300. if (vcpu_id < 0)
  301. printk(KERN_DEBUG "vcpu not ready for apic_round_robin\n");
  302. return kvm->vcpus[vcpu_id]->apic;
  303. }
  304. static void apic_set_eoi(struct kvm_lapic *apic)
  305. {
  306. int vector = apic_find_highest_isr(apic);
  307. /*
  308. * Not every write EOI will has corresponding ISR,
  309. * one example is when Kernel check timer on setup_IO_APIC
  310. */
  311. if (vector == -1)
  312. return;
  313. apic_clear_vector(vector, apic->regs + APIC_ISR);
  314. apic_update_ppr(apic);
  315. if (apic_test_and_clear_vector(vector, apic->regs + APIC_TMR))
  316. kvm_ioapic_update_eoi(apic->vcpu->kvm, vector);
  317. }
  318. static void apic_send_ipi(struct kvm_lapic *apic)
  319. {
  320. u32 icr_low = apic_get_reg(apic, APIC_ICR);
  321. u32 icr_high = apic_get_reg(apic, APIC_ICR2);
  322. unsigned int dest = GET_APIC_DEST_FIELD(icr_high);
  323. unsigned int short_hand = icr_low & APIC_SHORT_MASK;
  324. unsigned int trig_mode = icr_low & APIC_INT_LEVELTRIG;
  325. unsigned int level = icr_low & APIC_INT_ASSERT;
  326. unsigned int dest_mode = icr_low & APIC_DEST_MASK;
  327. unsigned int delivery_mode = icr_low & APIC_MODE_MASK;
  328. unsigned int vector = icr_low & APIC_VECTOR_MASK;
  329. struct kvm_lapic *target;
  330. struct kvm_vcpu *vcpu;
  331. unsigned long lpr_map = 0;
  332. int i;
  333. apic_debug("icr_high 0x%x, icr_low 0x%x, "
  334. "short_hand 0x%x, dest 0x%x, trig_mode 0x%x, level 0x%x, "
  335. "dest_mode 0x%x, delivery_mode 0x%x, vector 0x%x\n",
  336. icr_high, icr_low, short_hand, dest,
  337. trig_mode, level, dest_mode, delivery_mode, vector);
  338. for (i = 0; i < KVM_MAX_VCPUS; i++) {
  339. vcpu = apic->vcpu->kvm->vcpus[i];
  340. if (!vcpu)
  341. continue;
  342. if (vcpu->apic &&
  343. apic_match_dest(vcpu, apic, short_hand, dest, dest_mode)) {
  344. if (delivery_mode == APIC_DM_LOWEST)
  345. set_bit(vcpu->vcpu_id, &lpr_map);
  346. else
  347. __apic_accept_irq(vcpu->apic, delivery_mode,
  348. vector, level, trig_mode);
  349. }
  350. }
  351. if (delivery_mode == APIC_DM_LOWEST) {
  352. target = kvm_apic_round_robin(vcpu->kvm, vector, lpr_map);
  353. if (target != NULL)
  354. __apic_accept_irq(target, delivery_mode,
  355. vector, level, trig_mode);
  356. }
  357. }
  358. static u32 apic_get_tmcct(struct kvm_lapic *apic)
  359. {
  360. u32 counter_passed;
  361. ktime_t passed, now = apic->timer.dev.base->get_time();
  362. u32 tmcct = apic_get_reg(apic, APIC_TMICT);
  363. ASSERT(apic != NULL);
  364. if (unlikely(ktime_to_ns(now) <=
  365. ktime_to_ns(apic->timer.last_update))) {
  366. /* Wrap around */
  367. passed = ktime_add(( {
  368. (ktime_t) {
  369. .tv64 = KTIME_MAX -
  370. (apic->timer.last_update).tv64}; }
  371. ), now);
  372. apic_debug("time elapsed\n");
  373. } else
  374. passed = ktime_sub(now, apic->timer.last_update);
  375. counter_passed = div64_64(ktime_to_ns(passed),
  376. (APIC_BUS_CYCLE_NS * apic->timer.divide_count));
  377. tmcct -= counter_passed;
  378. if (tmcct <= 0) {
  379. if (unlikely(!apic_lvtt_period(apic)))
  380. tmcct = 0;
  381. else
  382. do {
  383. tmcct += apic_get_reg(apic, APIC_TMICT);
  384. } while (tmcct <= 0);
  385. }
  386. return tmcct;
  387. }
  388. static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
  389. {
  390. u32 val = 0;
  391. if (offset >= LAPIC_MMIO_LENGTH)
  392. return 0;
  393. switch (offset) {
  394. case APIC_ARBPRI:
  395. printk(KERN_WARNING "Access APIC ARBPRI register "
  396. "which is for P6\n");
  397. break;
  398. case APIC_TMCCT: /* Timer CCR */
  399. val = apic_get_tmcct(apic);
  400. break;
  401. default:
  402. val = apic_get_reg(apic, offset);
  403. break;
  404. }
  405. return val;
  406. }
  407. static void apic_mmio_read(struct kvm_io_device *this,
  408. gpa_t address, int len, void *data)
  409. {
  410. struct kvm_lapic *apic = (struct kvm_lapic *)this->private;
  411. unsigned int offset = address - apic->base_address;
  412. unsigned char alignment = offset & 0xf;
  413. u32 result;
  414. if ((alignment + len) > 4) {
  415. printk(KERN_ERR "KVM_APIC_READ: alignment error %lx %d",
  416. (unsigned long)address, len);
  417. return;
  418. }
  419. result = __apic_read(apic, offset & ~0xf);
  420. switch (len) {
  421. case 1:
  422. case 2:
  423. case 4:
  424. memcpy(data, (char *)&result + alignment, len);
  425. break;
  426. default:
  427. printk(KERN_ERR "Local APIC read with len = %x, "
  428. "should be 1,2, or 4 instead\n", len);
  429. break;
  430. }
  431. }
  432. static void update_divide_count(struct kvm_lapic *apic)
  433. {
  434. u32 tmp1, tmp2, tdcr;
  435. tdcr = apic_get_reg(apic, APIC_TDCR);
  436. tmp1 = tdcr & 0xf;
  437. tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
  438. apic->timer.divide_count = 0x1 << (tmp2 & 0x7);
  439. apic_debug("timer divide count is 0x%x\n",
  440. apic->timer.divide_count);
  441. }
  442. static void start_apic_timer(struct kvm_lapic *apic)
  443. {
  444. ktime_t now = apic->timer.dev.base->get_time();
  445. apic->timer.last_update = now;
  446. apic->timer.period = apic_get_reg(apic, APIC_TMICT) *
  447. APIC_BUS_CYCLE_NS * apic->timer.divide_count;
  448. atomic_set(&apic->timer.pending, 0);
  449. hrtimer_start(&apic->timer.dev,
  450. ktime_add_ns(now, apic->timer.period),
  451. HRTIMER_MODE_ABS);
  452. apic_debug("%s: bus cycle is %" PRId64 "ns, now 0x%016"
  453. PRIx64 ", "
  454. "timer initial count 0x%x, period %lldns, "
  455. "expire @ 0x%016" PRIx64 ".\n", __FUNCTION__,
  456. APIC_BUS_CYCLE_NS, ktime_to_ns(now),
  457. apic_get_reg(apic, APIC_TMICT),
  458. apic->timer.period,
  459. ktime_to_ns(ktime_add_ns(now,
  460. apic->timer.period)));
  461. }
  462. static void apic_mmio_write(struct kvm_io_device *this,
  463. gpa_t address, int len, const void *data)
  464. {
  465. struct kvm_lapic *apic = (struct kvm_lapic *)this->private;
  466. unsigned int offset = address - apic->base_address;
  467. unsigned char alignment = offset & 0xf;
  468. u32 val;
  469. /*
  470. * APIC register must be aligned on 128-bits boundary.
  471. * 32/64/128 bits registers must be accessed thru 32 bits.
  472. * Refer SDM 8.4.1
  473. */
  474. if (len != 4 || alignment) {
  475. if (printk_ratelimit())
  476. printk(KERN_ERR "apic write: bad size=%d %lx\n",
  477. len, (long)address);
  478. return;
  479. }
  480. val = *(u32 *) data;
  481. /* too common printing */
  482. if (offset != APIC_EOI)
  483. apic_debug("%s: offset 0x%x with length 0x%x, and value is "
  484. "0x%x\n", __FUNCTION__, offset, len, val);
  485. offset &= 0xff0;
  486. switch (offset) {
  487. case APIC_ID: /* Local APIC ID */
  488. apic_set_reg(apic, APIC_ID, val);
  489. break;
  490. case APIC_TASKPRI:
  491. apic_set_tpr(apic, val & 0xff);
  492. break;
  493. case APIC_EOI:
  494. apic_set_eoi(apic);
  495. break;
  496. case APIC_LDR:
  497. apic_set_reg(apic, APIC_LDR, val & APIC_LDR_MASK);
  498. break;
  499. case APIC_DFR:
  500. apic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
  501. break;
  502. case APIC_SPIV:
  503. apic_set_reg(apic, APIC_SPIV, val & 0x3ff);
  504. if (!(val & APIC_SPIV_APIC_ENABLED)) {
  505. int i;
  506. u32 lvt_val;
  507. for (i = 0; i < APIC_LVT_NUM; i++) {
  508. lvt_val = apic_get_reg(apic,
  509. APIC_LVTT + 0x10 * i);
  510. apic_set_reg(apic, APIC_LVTT + 0x10 * i,
  511. lvt_val | APIC_LVT_MASKED);
  512. }
  513. atomic_set(&apic->timer.pending, 0);
  514. }
  515. break;
  516. case APIC_ICR:
  517. /* No delay here, so we always clear the pending bit */
  518. apic_set_reg(apic, APIC_ICR, val & ~(1 << 12));
  519. apic_send_ipi(apic);
  520. break;
  521. case APIC_ICR2:
  522. apic_set_reg(apic, APIC_ICR2, val & 0xff000000);
  523. break;
  524. case APIC_LVTT:
  525. case APIC_LVTTHMR:
  526. case APIC_LVTPC:
  527. case APIC_LVT0:
  528. case APIC_LVT1:
  529. case APIC_LVTERR:
  530. /* TODO: Check vector */
  531. if (!apic_sw_enabled(apic))
  532. val |= APIC_LVT_MASKED;
  533. val &= apic_lvt_mask[(offset - APIC_LVTT) >> 4];
  534. apic_set_reg(apic, offset, val);
  535. break;
  536. case APIC_TMICT:
  537. hrtimer_cancel(&apic->timer.dev);
  538. apic_set_reg(apic, APIC_TMICT, val);
  539. start_apic_timer(apic);
  540. return;
  541. case APIC_TDCR:
  542. if (val & 4)
  543. printk(KERN_ERR "KVM_WRITE:TDCR %x\n", val);
  544. apic_set_reg(apic, APIC_TDCR, val);
  545. update_divide_count(apic);
  546. break;
  547. default:
  548. apic_debug("Local APIC Write to read-only register %x\n",
  549. offset);
  550. break;
  551. }
  552. }
  553. static int apic_mmio_range(struct kvm_io_device *this, gpa_t addr)
  554. {
  555. struct kvm_lapic *apic = (struct kvm_lapic *)this->private;
  556. int ret = 0;
  557. if (apic_hw_enabled(apic) &&
  558. (addr >= apic->base_address) &&
  559. (addr < (apic->base_address + LAPIC_MMIO_LENGTH)))
  560. ret = 1;
  561. return ret;
  562. }
  563. void kvm_free_apic(struct kvm_lapic *apic)
  564. {
  565. if (!apic)
  566. return;
  567. hrtimer_cancel(&apic->timer.dev);
  568. if (apic->regs_page) {
  569. __free_page(apic->regs_page);
  570. apic->regs_page = 0;
  571. }
  572. kfree(apic);
  573. }
  574. /*
  575. *----------------------------------------------------------------------
  576. * LAPIC interface
  577. *----------------------------------------------------------------------
  578. */
  579. void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8)
  580. {
  581. struct kvm_lapic *apic = (struct kvm_lapic *)vcpu->apic;
  582. if (!apic)
  583. return;
  584. apic_set_tpr(apic, ((cr8 & 0x0f) << 4));
  585. }
  586. u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu)
  587. {
  588. struct kvm_lapic *apic = (struct kvm_lapic *)vcpu->apic;
  589. u64 tpr;
  590. if (!apic)
  591. return 0;
  592. tpr = (u64) apic_get_reg(apic, APIC_TASKPRI);
  593. return (tpr & 0xf0) >> 4;
  594. }
  595. void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
  596. {
  597. struct kvm_lapic *apic = (struct kvm_lapic *)vcpu->apic;
  598. if (!apic) {
  599. value |= MSR_IA32_APICBASE_BSP;
  600. vcpu->apic_base = value;
  601. return;
  602. }
  603. if (apic->vcpu->vcpu_id)
  604. value &= ~MSR_IA32_APICBASE_BSP;
  605. vcpu->apic_base = value;
  606. apic->base_address = apic->vcpu->apic_base &
  607. MSR_IA32_APICBASE_BASE;
  608. /* with FSB delivery interrupt, we can restart APIC functionality */
  609. apic_debug("apic base msr is 0x%016" PRIx64 ", and base address is "
  610. "0x%lx.\n", apic->apic_base, apic->base_address);
  611. }
  612. u64 kvm_lapic_get_base(struct kvm_vcpu *vcpu)
  613. {
  614. return vcpu->apic_base;
  615. }
  616. EXPORT_SYMBOL_GPL(kvm_lapic_get_base);
  617. static void lapic_reset(struct kvm_vcpu *vcpu)
  618. {
  619. struct kvm_lapic *apic;
  620. int i;
  621. apic_debug("%s\n", __FUNCTION__);
  622. ASSERT(vcpu);
  623. apic = vcpu->apic;
  624. ASSERT(apic != NULL);
  625. /* Stop the timer in case it's a reset to an active apic */
  626. hrtimer_cancel(&apic->timer.dev);
  627. apic_set_reg(apic, APIC_ID, vcpu->vcpu_id << 24);
  628. apic_set_reg(apic, APIC_LVR, APIC_VERSION);
  629. for (i = 0; i < APIC_LVT_NUM; i++)
  630. apic_set_reg(apic, APIC_LVTT + 0x10 * i, APIC_LVT_MASKED);
  631. apic_set_reg(apic, APIC_DFR, 0xffffffffU);
  632. apic_set_reg(apic, APIC_SPIV, 0xff);
  633. apic_set_reg(apic, APIC_TASKPRI, 0);
  634. apic_set_reg(apic, APIC_LDR, 0);
  635. apic_set_reg(apic, APIC_ESR, 0);
  636. apic_set_reg(apic, APIC_ICR, 0);
  637. apic_set_reg(apic, APIC_ICR2, 0);
  638. apic_set_reg(apic, APIC_TDCR, 0);
  639. apic_set_reg(apic, APIC_TMICT, 0);
  640. for (i = 0; i < 8; i++) {
  641. apic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
  642. apic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
  643. apic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
  644. }
  645. apic->timer.divide_count = 0;
  646. atomic_set(&apic->timer.pending, 0);
  647. if (vcpu->vcpu_id == 0)
  648. vcpu->apic_base |= MSR_IA32_APICBASE_BSP;
  649. apic_update_ppr(apic);
  650. apic_debug(KERN_INFO "%s: vcpu=%p, id=%d, base_msr="
  651. "0x%016" PRIx64 ", base_address=0x%0lx.\n", __FUNCTION__,
  652. vcpu, kvm_apic_id(apic),
  653. vcpu->apic_base, apic->base_address);
  654. }
  655. int kvm_lapic_enabled(struct kvm_vcpu *vcpu)
  656. {
  657. struct kvm_lapic *apic = (struct kvm_lapic *)vcpu->apic;
  658. int ret = 0;
  659. if (!apic)
  660. return 0;
  661. ret = apic_enabled(apic);
  662. return ret;
  663. }
  664. /*
  665. *----------------------------------------------------------------------
  666. * timer interface
  667. *----------------------------------------------------------------------
  668. */
  669. static int __apic_timer_fn(struct kvm_lapic *apic)
  670. {
  671. u32 vector;
  672. int result = 0;
  673. if (unlikely(!apic_enabled(apic) ||
  674. !apic_lvt_enabled(apic, APIC_LVTT))) {
  675. apic_debug("%s: time interrupt although apic is down\n",
  676. __FUNCTION__);
  677. return 0;
  678. }
  679. vector = apic_lvt_vector(apic, APIC_LVTT);
  680. apic->timer.last_update = apic->timer.dev.expires;
  681. atomic_inc(&apic->timer.pending);
  682. __apic_accept_irq(apic, APIC_DM_FIXED, vector, 1, 0);
  683. if (apic_lvtt_period(apic)) {
  684. u32 offset;
  685. u32 tmict = apic_get_reg(apic, APIC_TMICT);
  686. offset = APIC_BUS_CYCLE_NS * apic->timer.divide_count * tmict;
  687. result = 1;
  688. apic->timer.dev.expires = ktime_add_ns(
  689. apic->timer.dev.expires,
  690. apic->timer.period);
  691. }
  692. return result;
  693. }
  694. static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
  695. {
  696. struct kvm_lapic *apic;
  697. int restart_timer = 0;
  698. apic = container_of(data, struct kvm_lapic, timer.dev);
  699. restart_timer = __apic_timer_fn(apic);
  700. if (restart_timer)
  701. return HRTIMER_RESTART;
  702. else
  703. return HRTIMER_NORESTART;
  704. }
  705. int kvm_create_lapic(struct kvm_vcpu *vcpu)
  706. {
  707. struct kvm_lapic *apic;
  708. ASSERT(vcpu != NULL);
  709. apic_debug("apic_init %d\n", vcpu->vcpu_id);
  710. apic = kzalloc(sizeof(*apic), GFP_KERNEL);
  711. if (!apic)
  712. goto nomem;
  713. vcpu->apic = apic;
  714. apic->regs_page = alloc_page(GFP_KERNEL);
  715. if (apic->regs_page == NULL) {
  716. printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
  717. vcpu->vcpu_id);
  718. goto nomem;
  719. }
  720. apic->regs = page_address(apic->regs_page);
  721. memset(apic->regs, 0, PAGE_SIZE);
  722. apic->vcpu = vcpu;
  723. hrtimer_init(&apic->timer.dev, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  724. apic->timer.dev.function = apic_timer_fn;
  725. apic->base_address = APIC_DEFAULT_PHYS_BASE;
  726. vcpu->apic_base = APIC_DEFAULT_PHYS_BASE;
  727. lapic_reset(vcpu);
  728. apic->dev.read = apic_mmio_read;
  729. apic->dev.write = apic_mmio_write;
  730. apic->dev.in_range = apic_mmio_range;
  731. apic->dev.private = apic;
  732. return 0;
  733. nomem:
  734. kvm_free_apic(apic);
  735. return -ENOMEM;
  736. }
  737. EXPORT_SYMBOL_GPL(kvm_create_lapic);
  738. int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
  739. {
  740. struct kvm_lapic *apic = vcpu->apic;
  741. int highest_irr;
  742. if (!apic || !apic_enabled(apic))
  743. return -1;
  744. highest_irr = apic_find_highest_irr(apic);
  745. if ((highest_irr == -1) ||
  746. ((highest_irr & 0xF0) <= apic_get_reg(apic, APIC_PROCPRI)))
  747. return -1;
  748. return highest_irr;
  749. }
  750. int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu)
  751. {
  752. int vector = kvm_apic_has_interrupt(vcpu);
  753. struct kvm_lapic *apic = vcpu->apic;
  754. if (vector == -1)
  755. return -1;
  756. apic_set_vector(vector, apic->regs + APIC_ISR);
  757. apic_update_ppr(apic);
  758. apic_clear_irr(vector, apic);
  759. return vector;
  760. }
  761. void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu)
  762. {
  763. struct kvm_lapic *apic = vcpu->apic;
  764. apic->base_address = vcpu->apic_base &
  765. MSR_IA32_APICBASE_BASE;
  766. apic_set_reg(apic, APIC_LVR, APIC_VERSION);
  767. apic_update_ppr(apic);
  768. hrtimer_cancel(&apic->timer.dev);
  769. update_divide_count(apic);
  770. start_apic_timer(apic);
  771. }