apic.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. /*
  2. * APIC support
  3. *
  4. * Copyright (c) 2004-2005 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu/thread.h"
  21. #include "qemu/error-report.h"
  22. #include "hw/i386/apic_internal.h"
  23. #include "hw/i386/apic.h"
  24. #include "hw/intc/ioapic.h"
  25. #include "hw/intc/i8259.h"
  26. #include "hw/intc/kvm_irqcount.h"
  27. #include "hw/pci/msi.h"
  28. #include "qemu/host-utils.h"
  29. #include "sysemu/kvm.h"
  30. #include "trace.h"
  31. #include "hw/i386/apic-msidef.h"
  32. #include "qapi/error.h"
  33. #include "qom/object.h"
  34. #define MAX_APICS 255
  35. #define MAX_APIC_WORDS 8
  36. #define SYNC_FROM_VAPIC 0x1
  37. #define SYNC_TO_VAPIC 0x2
  38. #define SYNC_ISR_IRR_TO_VAPIC 0x4
  39. static APICCommonState *local_apics[MAX_APICS + 1];
  40. #define TYPE_APIC "apic"
  41. /*This is reusing the APICCommonState typedef from APIC_COMMON */
  42. DECLARE_INSTANCE_CHECKER(APICCommonState, APIC,
  43. TYPE_APIC)
  44. static void apic_set_irq(APICCommonState *s, int vector_num, int trigger_mode);
  45. static void apic_update_irq(APICCommonState *s);
  46. static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
  47. uint8_t dest, uint8_t dest_mode);
  48. /* Find first bit starting from msb */
  49. static int apic_fls_bit(uint32_t value)
  50. {
  51. return 31 - clz32(value);
  52. }
  53. /* Find first bit starting from lsb */
  54. static int apic_ffs_bit(uint32_t value)
  55. {
  56. return ctz32(value);
  57. }
  58. static inline void apic_reset_bit(uint32_t *tab, int index)
  59. {
  60. int i, mask;
  61. i = index >> 5;
  62. mask = 1 << (index & 0x1f);
  63. tab[i] &= ~mask;
  64. }
  65. /* return -1 if no bit is set */
  66. static int get_highest_priority_int(uint32_t *tab)
  67. {
  68. int i;
  69. for (i = 7; i >= 0; i--) {
  70. if (tab[i] != 0) {
  71. return i * 32 + apic_fls_bit(tab[i]);
  72. }
  73. }
  74. return -1;
  75. }
  76. static void apic_sync_vapic(APICCommonState *s, int sync_type)
  77. {
  78. VAPICState vapic_state;
  79. size_t length;
  80. off_t start;
  81. int vector;
  82. if (!s->vapic_paddr) {
  83. return;
  84. }
  85. if (sync_type & SYNC_FROM_VAPIC) {
  86. cpu_physical_memory_read(s->vapic_paddr, &vapic_state,
  87. sizeof(vapic_state));
  88. s->tpr = vapic_state.tpr;
  89. }
  90. if (sync_type & (SYNC_TO_VAPIC | SYNC_ISR_IRR_TO_VAPIC)) {
  91. start = offsetof(VAPICState, isr);
  92. length = offsetof(VAPICState, enabled) - offsetof(VAPICState, isr);
  93. if (sync_type & SYNC_TO_VAPIC) {
  94. assert(qemu_cpu_is_self(CPU(s->cpu)));
  95. vapic_state.tpr = s->tpr;
  96. vapic_state.enabled = 1;
  97. start = 0;
  98. length = sizeof(VAPICState);
  99. }
  100. vector = get_highest_priority_int(s->isr);
  101. if (vector < 0) {
  102. vector = 0;
  103. }
  104. vapic_state.isr = vector & 0xf0;
  105. vapic_state.zero = 0;
  106. vector = get_highest_priority_int(s->irr);
  107. if (vector < 0) {
  108. vector = 0;
  109. }
  110. vapic_state.irr = vector & 0xff;
  111. address_space_write_rom(&address_space_memory,
  112. s->vapic_paddr + start,
  113. MEMTXATTRS_UNSPECIFIED,
  114. ((void *)&vapic_state) + start, length);
  115. }
  116. }
  117. static void apic_vapic_base_update(APICCommonState *s)
  118. {
  119. apic_sync_vapic(s, SYNC_TO_VAPIC);
  120. }
  121. static void apic_local_deliver(APICCommonState *s, int vector)
  122. {
  123. uint32_t lvt = s->lvt[vector];
  124. int trigger_mode;
  125. trace_apic_local_deliver(vector, (lvt >> 8) & 7);
  126. if (lvt & APIC_LVT_MASKED)
  127. return;
  128. switch ((lvt >> 8) & 7) {
  129. case APIC_DM_SMI:
  130. cpu_interrupt(CPU(s->cpu), CPU_INTERRUPT_SMI);
  131. break;
  132. case APIC_DM_NMI:
  133. cpu_interrupt(CPU(s->cpu), CPU_INTERRUPT_NMI);
  134. break;
  135. case APIC_DM_EXTINT:
  136. cpu_interrupt(CPU(s->cpu), CPU_INTERRUPT_HARD);
  137. break;
  138. case APIC_DM_FIXED:
  139. trigger_mode = APIC_TRIGGER_EDGE;
  140. if ((vector == APIC_LVT_LINT0 || vector == APIC_LVT_LINT1) &&
  141. (lvt & APIC_LVT_LEVEL_TRIGGER))
  142. trigger_mode = APIC_TRIGGER_LEVEL;
  143. apic_set_irq(s, lvt & 0xff, trigger_mode);
  144. }
  145. }
  146. void apic_deliver_pic_intr(DeviceState *dev, int level)
  147. {
  148. APICCommonState *s = APIC(dev);
  149. if (level) {
  150. apic_local_deliver(s, APIC_LVT_LINT0);
  151. } else {
  152. uint32_t lvt = s->lvt[APIC_LVT_LINT0];
  153. switch ((lvt >> 8) & 7) {
  154. case APIC_DM_FIXED:
  155. if (!(lvt & APIC_LVT_LEVEL_TRIGGER))
  156. break;
  157. apic_reset_bit(s->irr, lvt & 0xff);
  158. /* fall through */
  159. case APIC_DM_EXTINT:
  160. apic_update_irq(s);
  161. break;
  162. }
  163. }
  164. }
  165. static void apic_external_nmi(APICCommonState *s)
  166. {
  167. apic_local_deliver(s, APIC_LVT_LINT1);
  168. }
  169. #define foreach_apic(apic, deliver_bitmask, code) \
  170. {\
  171. int __i, __j;\
  172. for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
  173. uint32_t __mask = deliver_bitmask[__i];\
  174. if (__mask) {\
  175. for(__j = 0; __j < 32; __j++) {\
  176. if (__mask & (1U << __j)) {\
  177. apic = local_apics[__i * 32 + __j];\
  178. if (apic) {\
  179. code;\
  180. }\
  181. }\
  182. }\
  183. }\
  184. }\
  185. }
  186. static void apic_bus_deliver(const uint32_t *deliver_bitmask,
  187. uint8_t delivery_mode, uint8_t vector_num,
  188. uint8_t trigger_mode)
  189. {
  190. APICCommonState *apic_iter;
  191. switch (delivery_mode) {
  192. case APIC_DM_LOWPRI:
  193. /* XXX: search for focus processor, arbitration */
  194. {
  195. int i, d;
  196. d = -1;
  197. for(i = 0; i < MAX_APIC_WORDS; i++) {
  198. if (deliver_bitmask[i]) {
  199. d = i * 32 + apic_ffs_bit(deliver_bitmask[i]);
  200. break;
  201. }
  202. }
  203. if (d >= 0) {
  204. apic_iter = local_apics[d];
  205. if (apic_iter) {
  206. apic_set_irq(apic_iter, vector_num, trigger_mode);
  207. }
  208. }
  209. }
  210. return;
  211. case APIC_DM_FIXED:
  212. break;
  213. case APIC_DM_SMI:
  214. foreach_apic(apic_iter, deliver_bitmask,
  215. cpu_interrupt(CPU(apic_iter->cpu), CPU_INTERRUPT_SMI)
  216. );
  217. return;
  218. case APIC_DM_NMI:
  219. foreach_apic(apic_iter, deliver_bitmask,
  220. cpu_interrupt(CPU(apic_iter->cpu), CPU_INTERRUPT_NMI)
  221. );
  222. return;
  223. case APIC_DM_INIT:
  224. /* normal INIT IPI sent to processors */
  225. foreach_apic(apic_iter, deliver_bitmask,
  226. cpu_interrupt(CPU(apic_iter->cpu),
  227. CPU_INTERRUPT_INIT)
  228. );
  229. return;
  230. case APIC_DM_EXTINT:
  231. /* handled in I/O APIC code */
  232. break;
  233. default:
  234. return;
  235. }
  236. foreach_apic(apic_iter, deliver_bitmask,
  237. apic_set_irq(apic_iter, vector_num, trigger_mode) );
  238. }
  239. void apic_deliver_irq(uint8_t dest, uint8_t dest_mode, uint8_t delivery_mode,
  240. uint8_t vector_num, uint8_t trigger_mode)
  241. {
  242. uint32_t deliver_bitmask[MAX_APIC_WORDS];
  243. trace_apic_deliver_irq(dest, dest_mode, delivery_mode, vector_num,
  244. trigger_mode);
  245. apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
  246. apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, trigger_mode);
  247. }
  248. static void apic_set_base(APICCommonState *s, uint64_t val)
  249. {
  250. s->apicbase = (val & 0xfffff000) |
  251. (s->apicbase & (MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE));
  252. /* if disabled, cannot be enabled again */
  253. if (!(val & MSR_IA32_APICBASE_ENABLE)) {
  254. s->apicbase &= ~MSR_IA32_APICBASE_ENABLE;
  255. cpu_clear_apic_feature(&s->cpu->env);
  256. s->spurious_vec &= ~APIC_SV_ENABLE;
  257. }
  258. }
  259. static void apic_set_tpr(APICCommonState *s, uint8_t val)
  260. {
  261. /* Updates from cr8 are ignored while the VAPIC is active */
  262. if (!s->vapic_paddr) {
  263. s->tpr = val << 4;
  264. apic_update_irq(s);
  265. }
  266. }
  267. int apic_get_highest_priority_irr(DeviceState *dev)
  268. {
  269. APICCommonState *s;
  270. if (!dev) {
  271. /* no interrupts */
  272. return -1;
  273. }
  274. s = APIC_COMMON(dev);
  275. return get_highest_priority_int(s->irr);
  276. }
  277. static uint8_t apic_get_tpr(APICCommonState *s)
  278. {
  279. apic_sync_vapic(s, SYNC_FROM_VAPIC);
  280. return s->tpr >> 4;
  281. }
  282. int apic_get_ppr(APICCommonState *s)
  283. {
  284. int tpr, isrv, ppr;
  285. tpr = (s->tpr >> 4);
  286. isrv = get_highest_priority_int(s->isr);
  287. if (isrv < 0)
  288. isrv = 0;
  289. isrv >>= 4;
  290. if (tpr >= isrv)
  291. ppr = s->tpr;
  292. else
  293. ppr = isrv << 4;
  294. return ppr;
  295. }
  296. static int apic_get_arb_pri(APICCommonState *s)
  297. {
  298. /* XXX: arbitration */
  299. return 0;
  300. }
  301. /*
  302. * <0 - low prio interrupt,
  303. * 0 - no interrupt,
  304. * >0 - interrupt number
  305. */
  306. static int apic_irq_pending(APICCommonState *s)
  307. {
  308. int irrv, ppr;
  309. if (!(s->spurious_vec & APIC_SV_ENABLE)) {
  310. return 0;
  311. }
  312. irrv = get_highest_priority_int(s->irr);
  313. if (irrv < 0) {
  314. return 0;
  315. }
  316. ppr = apic_get_ppr(s);
  317. if (ppr && (irrv & 0xf0) <= (ppr & 0xf0)) {
  318. return -1;
  319. }
  320. return irrv;
  321. }
  322. /* signal the CPU if an irq is pending */
  323. static void apic_update_irq(APICCommonState *s)
  324. {
  325. CPUState *cpu;
  326. DeviceState *dev = (DeviceState *)s;
  327. cpu = CPU(s->cpu);
  328. if (!qemu_cpu_is_self(cpu)) {
  329. cpu_interrupt(cpu, CPU_INTERRUPT_POLL);
  330. } else if (apic_irq_pending(s) > 0) {
  331. cpu_interrupt(cpu, CPU_INTERRUPT_HARD);
  332. } else if (!apic_accept_pic_intr(dev) || !pic_get_output(isa_pic)) {
  333. cpu_reset_interrupt(cpu, CPU_INTERRUPT_HARD);
  334. }
  335. }
  336. void apic_poll_irq(DeviceState *dev)
  337. {
  338. APICCommonState *s = APIC(dev);
  339. apic_sync_vapic(s, SYNC_FROM_VAPIC);
  340. apic_update_irq(s);
  341. }
  342. static void apic_set_irq(APICCommonState *s, int vector_num, int trigger_mode)
  343. {
  344. kvm_report_irq_delivered(!apic_get_bit(s->irr, vector_num));
  345. apic_set_bit(s->irr, vector_num);
  346. if (trigger_mode)
  347. apic_set_bit(s->tmr, vector_num);
  348. else
  349. apic_reset_bit(s->tmr, vector_num);
  350. if (s->vapic_paddr) {
  351. apic_sync_vapic(s, SYNC_ISR_IRR_TO_VAPIC);
  352. /*
  353. * The vcpu thread needs to see the new IRR before we pull its current
  354. * TPR value. That way, if we miss a lowering of the TRP, the guest
  355. * has the chance to notice the new IRR and poll for IRQs on its own.
  356. */
  357. smp_wmb();
  358. apic_sync_vapic(s, SYNC_FROM_VAPIC);
  359. }
  360. apic_update_irq(s);
  361. }
  362. static void apic_eoi(APICCommonState *s)
  363. {
  364. int isrv;
  365. isrv = get_highest_priority_int(s->isr);
  366. if (isrv < 0)
  367. return;
  368. apic_reset_bit(s->isr, isrv);
  369. if (!(s->spurious_vec & APIC_SV_DIRECTED_IO) && apic_get_bit(s->tmr, isrv)) {
  370. ioapic_eoi_broadcast(isrv);
  371. }
  372. apic_sync_vapic(s, SYNC_FROM_VAPIC | SYNC_TO_VAPIC);
  373. apic_update_irq(s);
  374. }
  375. static int apic_find_dest(uint8_t dest)
  376. {
  377. APICCommonState *apic = local_apics[dest];
  378. int i;
  379. if (apic && apic->id == dest)
  380. return dest; /* shortcut in case apic->id == local_apics[dest]->id */
  381. for (i = 0; i < MAX_APICS; i++) {
  382. apic = local_apics[i];
  383. if (apic && apic->id == dest)
  384. return i;
  385. if (!apic)
  386. break;
  387. }
  388. return -1;
  389. }
  390. static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
  391. uint8_t dest, uint8_t dest_mode)
  392. {
  393. APICCommonState *apic_iter;
  394. int i;
  395. if (dest_mode == 0) {
  396. if (dest == 0xff) {
  397. memset(deliver_bitmask, 0xff, MAX_APIC_WORDS * sizeof(uint32_t));
  398. } else {
  399. int idx = apic_find_dest(dest);
  400. memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
  401. if (idx >= 0)
  402. apic_set_bit(deliver_bitmask, idx);
  403. }
  404. } else {
  405. /* XXX: cluster mode */
  406. memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
  407. for(i = 0; i < MAX_APICS; i++) {
  408. apic_iter = local_apics[i];
  409. if (apic_iter) {
  410. if (apic_iter->dest_mode == 0xf) {
  411. if (dest & apic_iter->log_dest)
  412. apic_set_bit(deliver_bitmask, i);
  413. } else if (apic_iter->dest_mode == 0x0) {
  414. if ((dest & 0xf0) == (apic_iter->log_dest & 0xf0) &&
  415. (dest & apic_iter->log_dest & 0x0f)) {
  416. apic_set_bit(deliver_bitmask, i);
  417. }
  418. }
  419. } else {
  420. break;
  421. }
  422. }
  423. }
  424. }
  425. static void apic_startup(APICCommonState *s, int vector_num)
  426. {
  427. s->sipi_vector = vector_num;
  428. cpu_interrupt(CPU(s->cpu), CPU_INTERRUPT_SIPI);
  429. }
  430. void apic_sipi(DeviceState *dev)
  431. {
  432. APICCommonState *s = APIC(dev);
  433. cpu_reset_interrupt(CPU(s->cpu), CPU_INTERRUPT_SIPI);
  434. if (!s->wait_for_sipi)
  435. return;
  436. cpu_x86_load_seg_cache_sipi(s->cpu, s->sipi_vector);
  437. s->wait_for_sipi = 0;
  438. }
  439. static void apic_deliver(DeviceState *dev, uint8_t dest, uint8_t dest_mode,
  440. uint8_t delivery_mode, uint8_t vector_num,
  441. uint8_t trigger_mode)
  442. {
  443. APICCommonState *s = APIC(dev);
  444. uint32_t deliver_bitmask[MAX_APIC_WORDS];
  445. int dest_shorthand = (s->icr[0] >> 18) & 3;
  446. APICCommonState *apic_iter;
  447. switch (dest_shorthand) {
  448. case 0:
  449. apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
  450. break;
  451. case 1:
  452. memset(deliver_bitmask, 0x00, sizeof(deliver_bitmask));
  453. apic_set_bit(deliver_bitmask, s->id);
  454. break;
  455. case 2:
  456. memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
  457. break;
  458. case 3:
  459. memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
  460. apic_reset_bit(deliver_bitmask, s->id);
  461. break;
  462. }
  463. switch (delivery_mode) {
  464. case APIC_DM_INIT:
  465. {
  466. int trig_mode = (s->icr[0] >> 15) & 1;
  467. int level = (s->icr[0] >> 14) & 1;
  468. if (level == 0 && trig_mode == 1) {
  469. foreach_apic(apic_iter, deliver_bitmask,
  470. apic_iter->arb_id = apic_iter->id );
  471. return;
  472. }
  473. }
  474. break;
  475. case APIC_DM_SIPI:
  476. foreach_apic(apic_iter, deliver_bitmask,
  477. apic_startup(apic_iter, vector_num) );
  478. return;
  479. }
  480. apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, trigger_mode);
  481. }
  482. static bool apic_check_pic(APICCommonState *s)
  483. {
  484. DeviceState *dev = (DeviceState *)s;
  485. if (!apic_accept_pic_intr(dev) || !pic_get_output(isa_pic)) {
  486. return false;
  487. }
  488. apic_deliver_pic_intr(dev, 1);
  489. return true;
  490. }
  491. int apic_get_interrupt(DeviceState *dev)
  492. {
  493. APICCommonState *s = APIC(dev);
  494. int intno;
  495. /* if the APIC is installed or enabled, we let the 8259 handle the
  496. IRQs */
  497. if (!s)
  498. return -1;
  499. if (!(s->spurious_vec & APIC_SV_ENABLE))
  500. return -1;
  501. apic_sync_vapic(s, SYNC_FROM_VAPIC);
  502. intno = apic_irq_pending(s);
  503. /* if there is an interrupt from the 8259, let the caller handle
  504. * that first since ExtINT interrupts ignore the priority.
  505. */
  506. if (intno == 0 || apic_check_pic(s)) {
  507. apic_sync_vapic(s, SYNC_TO_VAPIC);
  508. return -1;
  509. } else if (intno < 0) {
  510. apic_sync_vapic(s, SYNC_TO_VAPIC);
  511. return s->spurious_vec & 0xff;
  512. }
  513. apic_reset_bit(s->irr, intno);
  514. apic_set_bit(s->isr, intno);
  515. apic_sync_vapic(s, SYNC_TO_VAPIC);
  516. apic_update_irq(s);
  517. return intno;
  518. }
  519. int apic_accept_pic_intr(DeviceState *dev)
  520. {
  521. APICCommonState *s = APIC(dev);
  522. uint32_t lvt0;
  523. if (!s)
  524. return -1;
  525. lvt0 = s->lvt[APIC_LVT_LINT0];
  526. if ((s->apicbase & MSR_IA32_APICBASE_ENABLE) == 0 ||
  527. (lvt0 & APIC_LVT_MASKED) == 0)
  528. return isa_pic != NULL;
  529. return 0;
  530. }
  531. static void apic_timer_update(APICCommonState *s, int64_t current_time)
  532. {
  533. if (apic_next_timer(s, current_time)) {
  534. timer_mod(s->timer, s->next_time);
  535. } else {
  536. timer_del(s->timer);
  537. }
  538. }
  539. static void apic_timer(void *opaque)
  540. {
  541. APICCommonState *s = opaque;
  542. apic_local_deliver(s, APIC_LVT_TIMER);
  543. apic_timer_update(s, s->next_time);
  544. }
  545. static uint64_t apic_mem_read(void *opaque, hwaddr addr, unsigned size)
  546. {
  547. DeviceState *dev;
  548. APICCommonState *s;
  549. uint32_t val;
  550. int index;
  551. if (size < 4) {
  552. return 0;
  553. }
  554. dev = cpu_get_current_apic();
  555. if (!dev) {
  556. return 0;
  557. }
  558. s = APIC(dev);
  559. index = (addr >> 4) & 0xff;
  560. switch(index) {
  561. case 0x02: /* id */
  562. val = s->id << 24;
  563. break;
  564. case 0x03: /* version */
  565. val = s->version | ((APIC_LVT_NB - 1) << 16);
  566. break;
  567. case 0x08:
  568. apic_sync_vapic(s, SYNC_FROM_VAPIC);
  569. if (apic_report_tpr_access) {
  570. cpu_report_tpr_access(&s->cpu->env, TPR_ACCESS_READ);
  571. }
  572. val = s->tpr;
  573. break;
  574. case 0x09:
  575. val = apic_get_arb_pri(s);
  576. break;
  577. case 0x0a:
  578. /* ppr */
  579. val = apic_get_ppr(s);
  580. break;
  581. case 0x0b:
  582. val = 0;
  583. break;
  584. case 0x0d:
  585. val = s->log_dest << 24;
  586. break;
  587. case 0x0e:
  588. val = (s->dest_mode << 28) | 0xfffffff;
  589. break;
  590. case 0x0f:
  591. val = s->spurious_vec;
  592. break;
  593. case 0x10 ... 0x17:
  594. val = s->isr[index & 7];
  595. break;
  596. case 0x18 ... 0x1f:
  597. val = s->tmr[index & 7];
  598. break;
  599. case 0x20 ... 0x27:
  600. val = s->irr[index & 7];
  601. break;
  602. case 0x28:
  603. val = s->esr;
  604. break;
  605. case 0x30:
  606. case 0x31:
  607. val = s->icr[index & 1];
  608. break;
  609. case 0x32 ... 0x37:
  610. val = s->lvt[index - 0x32];
  611. break;
  612. case 0x38:
  613. val = s->initial_count;
  614. break;
  615. case 0x39:
  616. val = apic_get_current_count(s);
  617. break;
  618. case 0x3e:
  619. val = s->divide_conf;
  620. break;
  621. default:
  622. s->esr |= APIC_ESR_ILLEGAL_ADDRESS;
  623. val = 0;
  624. break;
  625. }
  626. trace_apic_mem_readl(addr, val);
  627. return val;
  628. }
  629. static void apic_send_msi(MSIMessage *msi)
  630. {
  631. uint64_t addr = msi->address;
  632. uint32_t data = msi->data;
  633. uint8_t dest = (addr & MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
  634. uint8_t vector = (data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
  635. uint8_t dest_mode = (addr >> MSI_ADDR_DEST_MODE_SHIFT) & 0x1;
  636. uint8_t trigger_mode = (data >> MSI_DATA_TRIGGER_SHIFT) & 0x1;
  637. uint8_t delivery = (data >> MSI_DATA_DELIVERY_MODE_SHIFT) & 0x7;
  638. /* XXX: Ignore redirection hint. */
  639. apic_deliver_irq(dest, dest_mode, delivery, vector, trigger_mode);
  640. }
  641. static void apic_mem_write(void *opaque, hwaddr addr, uint64_t val,
  642. unsigned size)
  643. {
  644. DeviceState *dev;
  645. APICCommonState *s;
  646. int index = (addr >> 4) & 0xff;
  647. if (size < 4) {
  648. return;
  649. }
  650. if (addr > 0xfff || !index) {
  651. /* MSI and MMIO APIC are at the same memory location,
  652. * but actually not on the global bus: MSI is on PCI bus
  653. * APIC is connected directly to the CPU.
  654. * Mapping them on the global bus happens to work because
  655. * MSI registers are reserved in APIC MMIO and vice versa. */
  656. MSIMessage msi = { .address = addr, .data = val };
  657. apic_send_msi(&msi);
  658. return;
  659. }
  660. dev = cpu_get_current_apic();
  661. if (!dev) {
  662. return;
  663. }
  664. s = APIC(dev);
  665. trace_apic_mem_writel(addr, val);
  666. switch(index) {
  667. case 0x02:
  668. s->id = (val >> 24);
  669. break;
  670. case 0x03:
  671. break;
  672. case 0x08:
  673. if (apic_report_tpr_access) {
  674. cpu_report_tpr_access(&s->cpu->env, TPR_ACCESS_WRITE);
  675. }
  676. s->tpr = val;
  677. apic_sync_vapic(s, SYNC_TO_VAPIC);
  678. apic_update_irq(s);
  679. break;
  680. case 0x09:
  681. case 0x0a:
  682. break;
  683. case 0x0b: /* EOI */
  684. apic_eoi(s);
  685. break;
  686. case 0x0d:
  687. s->log_dest = val >> 24;
  688. break;
  689. case 0x0e:
  690. s->dest_mode = val >> 28;
  691. break;
  692. case 0x0f:
  693. s->spurious_vec = val & 0x1ff;
  694. apic_update_irq(s);
  695. break;
  696. case 0x10 ... 0x17:
  697. case 0x18 ... 0x1f:
  698. case 0x20 ... 0x27:
  699. case 0x28:
  700. break;
  701. case 0x30:
  702. s->icr[0] = val;
  703. apic_deliver(dev, (s->icr[1] >> 24) & 0xff, (s->icr[0] >> 11) & 1,
  704. (s->icr[0] >> 8) & 7, (s->icr[0] & 0xff),
  705. (s->icr[0] >> 15) & 1);
  706. break;
  707. case 0x31:
  708. s->icr[1] = val;
  709. break;
  710. case 0x32 ... 0x37:
  711. {
  712. int n = index - 0x32;
  713. s->lvt[n] = val;
  714. if (n == APIC_LVT_TIMER) {
  715. apic_timer_update(s, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  716. } else if (n == APIC_LVT_LINT0 && apic_check_pic(s)) {
  717. apic_update_irq(s);
  718. }
  719. }
  720. break;
  721. case 0x38:
  722. s->initial_count = val;
  723. s->initial_count_load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  724. apic_timer_update(s, s->initial_count_load_time);
  725. break;
  726. case 0x39:
  727. break;
  728. case 0x3e:
  729. {
  730. int v;
  731. s->divide_conf = val & 0xb;
  732. v = (s->divide_conf & 3) | ((s->divide_conf >> 1) & 4);
  733. s->count_shift = (v + 1) & 7;
  734. }
  735. break;
  736. default:
  737. s->esr |= APIC_ESR_ILLEGAL_ADDRESS;
  738. break;
  739. }
  740. }
  741. static void apic_pre_save(APICCommonState *s)
  742. {
  743. apic_sync_vapic(s, SYNC_FROM_VAPIC);
  744. }
  745. static void apic_post_load(APICCommonState *s)
  746. {
  747. if (s->timer_expiry != -1) {
  748. timer_mod(s->timer, s->timer_expiry);
  749. } else {
  750. timer_del(s->timer);
  751. }
  752. }
  753. static const MemoryRegionOps apic_io_ops = {
  754. .read = apic_mem_read,
  755. .write = apic_mem_write,
  756. .impl.min_access_size = 1,
  757. .impl.max_access_size = 4,
  758. .valid.min_access_size = 1,
  759. .valid.max_access_size = 4,
  760. .endianness = DEVICE_NATIVE_ENDIAN,
  761. };
  762. static void apic_realize(DeviceState *dev, Error **errp)
  763. {
  764. APICCommonState *s = APIC(dev);
  765. if (s->id >= MAX_APICS) {
  766. error_setg(errp, "%s initialization failed. APIC ID %d is invalid",
  767. object_get_typename(OBJECT(dev)), s->id);
  768. return;
  769. }
  770. if (kvm_enabled()) {
  771. warn_report("Userspace local APIC is deprecated for KVM.");
  772. warn_report("Do not use kernel-irqchip except for the -M isapc machine type.");
  773. }
  774. memory_region_init_io(&s->io_memory, OBJECT(s), &apic_io_ops, s, "apic-msi",
  775. APIC_SPACE_SIZE);
  776. s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, apic_timer, s);
  777. local_apics[s->id] = s;
  778. msi_nonbroken = true;
  779. }
  780. static void apic_unrealize(DeviceState *dev)
  781. {
  782. APICCommonState *s = APIC(dev);
  783. timer_free(s->timer);
  784. local_apics[s->id] = NULL;
  785. }
  786. static void apic_class_init(ObjectClass *klass, void *data)
  787. {
  788. APICCommonClass *k = APIC_COMMON_CLASS(klass);
  789. k->realize = apic_realize;
  790. k->unrealize = apic_unrealize;
  791. k->set_base = apic_set_base;
  792. k->set_tpr = apic_set_tpr;
  793. k->get_tpr = apic_get_tpr;
  794. k->vapic_base_update = apic_vapic_base_update;
  795. k->external_nmi = apic_external_nmi;
  796. k->pre_save = apic_pre_save;
  797. k->post_load = apic_post_load;
  798. k->send_msi = apic_send_msi;
  799. }
  800. static const TypeInfo apic_info = {
  801. .name = TYPE_APIC,
  802. .instance_size = sizeof(APICCommonState),
  803. .parent = TYPE_APIC_COMMON,
  804. .class_init = apic_class_init,
  805. };
  806. static void apic_register_types(void)
  807. {
  808. type_register_static(&apic_info);
  809. }
  810. type_init(apic_register_types)