apic_common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * APIC support - common bits of emulated and KVM kernel model
  3. *
  4. * Copyright (c) 2004-2005 Fabrice Bellard
  5. * Copyright (c) 2011 Jan Kiszka, Siemens AG
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, see <http://www.gnu.org/licenses/>
  19. */
  20. #include "apic.h"
  21. #include "apic_internal.h"
  22. #include "trace.h"
  23. #include "kvm.h"
  24. static int apic_irq_delivered;
  25. bool apic_report_tpr_access;
  26. void cpu_set_apic_base(DeviceState *d, uint64_t val)
  27. {
  28. trace_cpu_set_apic_base(val);
  29. if (d) {
  30. APICCommonState *s = APIC_COMMON(d);
  31. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  32. info->set_base(s, val);
  33. }
  34. }
  35. uint64_t cpu_get_apic_base(DeviceState *d)
  36. {
  37. if (d) {
  38. APICCommonState *s = APIC_COMMON(d);
  39. trace_cpu_get_apic_base((uint64_t)s->apicbase);
  40. return s->apicbase;
  41. } else {
  42. trace_cpu_get_apic_base(MSR_IA32_APICBASE_BSP);
  43. return MSR_IA32_APICBASE_BSP;
  44. }
  45. }
  46. void cpu_set_apic_tpr(DeviceState *d, uint8_t val)
  47. {
  48. APICCommonState *s;
  49. APICCommonClass *info;
  50. if (!d) {
  51. return;
  52. }
  53. s = APIC_COMMON(d);
  54. info = APIC_COMMON_GET_CLASS(s);
  55. info->set_tpr(s, val);
  56. }
  57. uint8_t cpu_get_apic_tpr(DeviceState *d)
  58. {
  59. APICCommonState *s;
  60. APICCommonClass *info;
  61. if (!d) {
  62. return 0;
  63. }
  64. s = APIC_COMMON(d);
  65. info = APIC_COMMON_GET_CLASS(s);
  66. return info->get_tpr(s);
  67. }
  68. void apic_enable_tpr_access_reporting(DeviceState *d, bool enable)
  69. {
  70. APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
  71. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  72. apic_report_tpr_access = enable;
  73. if (info->enable_tpr_reporting) {
  74. info->enable_tpr_reporting(s, enable);
  75. }
  76. }
  77. void apic_enable_vapic(DeviceState *d, target_phys_addr_t paddr)
  78. {
  79. APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
  80. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  81. s->vapic_paddr = paddr;
  82. info->vapic_base_update(s);
  83. }
  84. void apic_handle_tpr_access_report(DeviceState *d, target_ulong ip,
  85. TPRAccess access)
  86. {
  87. APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
  88. vapic_report_tpr_access(s->vapic, s->cpu_env, ip, access);
  89. }
  90. void apic_report_irq_delivered(int delivered)
  91. {
  92. apic_irq_delivered += delivered;
  93. trace_apic_report_irq_delivered(apic_irq_delivered);
  94. }
  95. void apic_reset_irq_delivered(void)
  96. {
  97. trace_apic_reset_irq_delivered(apic_irq_delivered);
  98. apic_irq_delivered = 0;
  99. }
  100. int apic_get_irq_delivered(void)
  101. {
  102. trace_apic_get_irq_delivered(apic_irq_delivered);
  103. return apic_irq_delivered;
  104. }
  105. void apic_deliver_nmi(DeviceState *d)
  106. {
  107. APICCommonState *s = APIC_COMMON(d);
  108. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  109. info->external_nmi(s);
  110. }
  111. bool apic_next_timer(APICCommonState *s, int64_t current_time)
  112. {
  113. int64_t d;
  114. /* We need to store the timer state separately to support APIC
  115. * implementations that maintain a non-QEMU timer, e.g. inside the
  116. * host kernel. This open-coded state allows us to migrate between
  117. * both models. */
  118. s->timer_expiry = -1;
  119. if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED) {
  120. return false;
  121. }
  122. d = (current_time - s->initial_count_load_time) >> s->count_shift;
  123. if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
  124. if (!s->initial_count) {
  125. return false;
  126. }
  127. d = ((d / ((uint64_t)s->initial_count + 1)) + 1) *
  128. ((uint64_t)s->initial_count + 1);
  129. } else {
  130. if (d >= s->initial_count) {
  131. return false;
  132. }
  133. d = (uint64_t)s->initial_count + 1;
  134. }
  135. s->next_time = s->initial_count_load_time + (d << s->count_shift);
  136. s->timer_expiry = s->next_time;
  137. return true;
  138. }
  139. void apic_init_reset(DeviceState *d)
  140. {
  141. APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
  142. int i;
  143. if (!s) {
  144. return;
  145. }
  146. s->tpr = 0;
  147. s->spurious_vec = 0xff;
  148. s->log_dest = 0;
  149. s->dest_mode = 0xf;
  150. memset(s->isr, 0, sizeof(s->isr));
  151. memset(s->tmr, 0, sizeof(s->tmr));
  152. memset(s->irr, 0, sizeof(s->irr));
  153. for (i = 0; i < APIC_LVT_NB; i++) {
  154. s->lvt[i] = APIC_LVT_MASKED;
  155. }
  156. s->esr = 0;
  157. memset(s->icr, 0, sizeof(s->icr));
  158. s->divide_conf = 0;
  159. s->count_shift = 0;
  160. s->initial_count = 0;
  161. s->initial_count_load_time = 0;
  162. s->next_time = 0;
  163. s->wait_for_sipi = 1;
  164. if (s->timer) {
  165. qemu_del_timer(s->timer);
  166. }
  167. s->timer_expiry = -1;
  168. }
  169. void apic_designate_bsp(DeviceState *d)
  170. {
  171. if (d == NULL) {
  172. return;
  173. }
  174. APICCommonState *s = APIC_COMMON(d);
  175. s->apicbase |= MSR_IA32_APICBASE_BSP;
  176. }
  177. static void apic_reset_common(DeviceState *d)
  178. {
  179. APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
  180. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  181. bool bsp;
  182. bsp = cpu_is_bsp(x86_env_get_cpu(s->cpu_env));
  183. s->apicbase = 0xfee00000 |
  184. (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;
  185. s->vapic_paddr = 0;
  186. info->vapic_base_update(s);
  187. apic_init_reset(d);
  188. if (bsp) {
  189. /*
  190. * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
  191. * time typically by BIOS, so PIC interrupt can be delivered to the
  192. * processor when local APIC is enabled.
  193. */
  194. s->lvt[APIC_LVT_LINT0] = 0x700;
  195. }
  196. }
  197. /* This function is only used for old state version 1 and 2 */
  198. static int apic_load_old(QEMUFile *f, void *opaque, int version_id)
  199. {
  200. APICCommonState *s = opaque;
  201. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  202. int i;
  203. if (version_id > 2) {
  204. return -EINVAL;
  205. }
  206. /* XXX: what if the base changes? (registered memory regions) */
  207. qemu_get_be32s(f, &s->apicbase);
  208. qemu_get_8s(f, &s->id);
  209. qemu_get_8s(f, &s->arb_id);
  210. qemu_get_8s(f, &s->tpr);
  211. qemu_get_be32s(f, &s->spurious_vec);
  212. qemu_get_8s(f, &s->log_dest);
  213. qemu_get_8s(f, &s->dest_mode);
  214. for (i = 0; i < 8; i++) {
  215. qemu_get_be32s(f, &s->isr[i]);
  216. qemu_get_be32s(f, &s->tmr[i]);
  217. qemu_get_be32s(f, &s->irr[i]);
  218. }
  219. for (i = 0; i < APIC_LVT_NB; i++) {
  220. qemu_get_be32s(f, &s->lvt[i]);
  221. }
  222. qemu_get_be32s(f, &s->esr);
  223. qemu_get_be32s(f, &s->icr[0]);
  224. qemu_get_be32s(f, &s->icr[1]);
  225. qemu_get_be32s(f, &s->divide_conf);
  226. s->count_shift = qemu_get_be32(f);
  227. qemu_get_be32s(f, &s->initial_count);
  228. s->initial_count_load_time = qemu_get_be64(f);
  229. s->next_time = qemu_get_be64(f);
  230. if (version_id >= 2) {
  231. s->timer_expiry = qemu_get_be64(f);
  232. }
  233. if (info->post_load) {
  234. info->post_load(s);
  235. }
  236. return 0;
  237. }
  238. static int apic_init_common(SysBusDevice *dev)
  239. {
  240. APICCommonState *s = APIC_COMMON(dev);
  241. APICCommonClass *info;
  242. static DeviceState *vapic;
  243. static int apic_no;
  244. if (apic_no >= MAX_APICS) {
  245. return -1;
  246. }
  247. s->idx = apic_no++;
  248. info = APIC_COMMON_GET_CLASS(s);
  249. info->init(s);
  250. sysbus_init_mmio(dev, &s->io_memory);
  251. /* Note: We need at least 1M to map the VAPIC option ROM */
  252. if (!vapic && s->vapic_control & VAPIC_ENABLE_MASK &&
  253. ram_size >= 1024 * 1024) {
  254. vapic = sysbus_create_simple("kvmvapic", -1, NULL);
  255. }
  256. s->vapic = vapic;
  257. if (apic_report_tpr_access && info->enable_tpr_reporting) {
  258. info->enable_tpr_reporting(s, true);
  259. }
  260. return 0;
  261. }
  262. static void apic_dispatch_pre_save(void *opaque)
  263. {
  264. APICCommonState *s = APIC_COMMON(opaque);
  265. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  266. if (info->pre_save) {
  267. info->pre_save(s);
  268. }
  269. }
  270. static int apic_dispatch_post_load(void *opaque, int version_id)
  271. {
  272. APICCommonState *s = APIC_COMMON(opaque);
  273. APICCommonClass *info = APIC_COMMON_GET_CLASS(s);
  274. if (info->post_load) {
  275. info->post_load(s);
  276. }
  277. return 0;
  278. }
  279. static const VMStateDescription vmstate_apic_common = {
  280. .name = "apic",
  281. .version_id = 3,
  282. .minimum_version_id = 3,
  283. .minimum_version_id_old = 1,
  284. .load_state_old = apic_load_old,
  285. .pre_save = apic_dispatch_pre_save,
  286. .post_load = apic_dispatch_post_load,
  287. .fields = (VMStateField[]) {
  288. VMSTATE_UINT32(apicbase, APICCommonState),
  289. VMSTATE_UINT8(id, APICCommonState),
  290. VMSTATE_UINT8(arb_id, APICCommonState),
  291. VMSTATE_UINT8(tpr, APICCommonState),
  292. VMSTATE_UINT32(spurious_vec, APICCommonState),
  293. VMSTATE_UINT8(log_dest, APICCommonState),
  294. VMSTATE_UINT8(dest_mode, APICCommonState),
  295. VMSTATE_UINT32_ARRAY(isr, APICCommonState, 8),
  296. VMSTATE_UINT32_ARRAY(tmr, APICCommonState, 8),
  297. VMSTATE_UINT32_ARRAY(irr, APICCommonState, 8),
  298. VMSTATE_UINT32_ARRAY(lvt, APICCommonState, APIC_LVT_NB),
  299. VMSTATE_UINT32(esr, APICCommonState),
  300. VMSTATE_UINT32_ARRAY(icr, APICCommonState, 2),
  301. VMSTATE_UINT32(divide_conf, APICCommonState),
  302. VMSTATE_INT32(count_shift, APICCommonState),
  303. VMSTATE_UINT32(initial_count, APICCommonState),
  304. VMSTATE_INT64(initial_count_load_time, APICCommonState),
  305. VMSTATE_INT64(next_time, APICCommonState),
  306. VMSTATE_INT64(timer_expiry,
  307. APICCommonState), /* open-coded timer state */
  308. VMSTATE_END_OF_LIST()
  309. }
  310. };
  311. static Property apic_properties_common[] = {
  312. DEFINE_PROP_UINT8("id", APICCommonState, id, -1),
  313. DEFINE_PROP_PTR("cpu_env", APICCommonState, cpu_env),
  314. DEFINE_PROP_BIT("vapic", APICCommonState, vapic_control, VAPIC_ENABLE_BIT,
  315. true),
  316. DEFINE_PROP_END_OF_LIST(),
  317. };
  318. static void apic_common_class_init(ObjectClass *klass, void *data)
  319. {
  320. SysBusDeviceClass *sc = SYS_BUS_DEVICE_CLASS(klass);
  321. DeviceClass *dc = DEVICE_CLASS(klass);
  322. dc->vmsd = &vmstate_apic_common;
  323. dc->reset = apic_reset_common;
  324. dc->no_user = 1;
  325. dc->props = apic_properties_common;
  326. sc->init = apic_init_common;
  327. }
  328. static TypeInfo apic_common_type = {
  329. .name = TYPE_APIC_COMMON,
  330. .parent = TYPE_SYS_BUS_DEVICE,
  331. .instance_size = sizeof(APICCommonState),
  332. .class_size = sizeof(APICCommonClass),
  333. .class_init = apic_common_class_init,
  334. .abstract = true,
  335. };
  336. static void register_types(void)
  337. {
  338. type_register_static(&apic_common_type);
  339. }
  340. type_init(register_types)