apic.c 24 KB

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