apic.c 23 KB

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