apic.c 23 KB

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