apic.c 24 KB

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