apic.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
  19. */
  20. #include "hw.h"
  21. #include "pc.h"
  22. #include "qemu-timer.h"
  23. #include "host-utils.h"
  24. //#define DEBUG_APIC
  25. //#define DEBUG_IOAPIC
  26. /* APIC Local Vector Table */
  27. #define APIC_LVT_TIMER 0
  28. #define APIC_LVT_THERMAL 1
  29. #define APIC_LVT_PERFORM 2
  30. #define APIC_LVT_LINT0 3
  31. #define APIC_LVT_LINT1 4
  32. #define APIC_LVT_ERROR 5
  33. #define APIC_LVT_NB 6
  34. /* APIC delivery modes */
  35. #define APIC_DM_FIXED 0
  36. #define APIC_DM_LOWPRI 1
  37. #define APIC_DM_SMI 2
  38. #define APIC_DM_NMI 4
  39. #define APIC_DM_INIT 5
  40. #define APIC_DM_SIPI 6
  41. #define APIC_DM_EXTINT 7
  42. /* APIC destination mode */
  43. #define APIC_DESTMODE_FLAT 0xf
  44. #define APIC_DESTMODE_CLUSTER 1
  45. #define APIC_TRIGGER_EDGE 0
  46. #define APIC_TRIGGER_LEVEL 1
  47. #define APIC_LVT_TIMER_PERIODIC (1<<17)
  48. #define APIC_LVT_MASKED (1<<16)
  49. #define APIC_LVT_LEVEL_TRIGGER (1<<15)
  50. #define APIC_LVT_REMOTE_IRR (1<<14)
  51. #define APIC_INPUT_POLARITY (1<<13)
  52. #define APIC_SEND_PENDING (1<<12)
  53. #define IOAPIC_NUM_PINS 0x18
  54. #define ESR_ILLEGAL_ADDRESS (1 << 7)
  55. #define APIC_SV_ENABLE (1 << 8)
  56. #define MAX_APICS 255
  57. #define MAX_APIC_WORDS 8
  58. typedef struct APICState {
  59. CPUState *cpu_env;
  60. uint32_t apicbase;
  61. uint8_t id;
  62. uint8_t arb_id;
  63. uint8_t tpr;
  64. uint32_t spurious_vec;
  65. uint8_t log_dest;
  66. uint8_t dest_mode;
  67. uint32_t isr[8]; /* in service register */
  68. uint32_t tmr[8]; /* trigger mode register */
  69. uint32_t irr[8]; /* interrupt request register */
  70. uint32_t lvt[APIC_LVT_NB];
  71. uint32_t esr; /* error register */
  72. uint32_t icr[2];
  73. uint32_t divide_conf;
  74. int count_shift;
  75. uint32_t initial_count;
  76. int64_t initial_count_load_time, next_time;
  77. QEMUTimer *timer;
  78. } APICState;
  79. struct IOAPICState {
  80. uint8_t id;
  81. uint8_t ioregsel;
  82. uint32_t irr;
  83. uint64_t ioredtbl[IOAPIC_NUM_PINS];
  84. };
  85. static int apic_io_memory;
  86. static APICState *local_apics[MAX_APICS + 1];
  87. static int last_apic_id = 0;
  88. static int apic_irq_delivered;
  89. static void apic_init_ipi(APICState *s);
  90. static void apic_set_irq(APICState *s, int vector_num, int trigger_mode);
  91. static void apic_update_irq(APICState *s);
  92. /* Find first bit starting from msb */
  93. static int fls_bit(uint32_t value)
  94. {
  95. return 31 - clz32(value);
  96. }
  97. /* Find first bit starting from lsb */
  98. static int ffs_bit(uint32_t value)
  99. {
  100. return ctz32(value);
  101. }
  102. static inline void set_bit(uint32_t *tab, int index)
  103. {
  104. int i, mask;
  105. i = index >> 5;
  106. mask = 1 << (index & 0x1f);
  107. tab[i] |= mask;
  108. }
  109. static inline void reset_bit(uint32_t *tab, int index)
  110. {
  111. int i, mask;
  112. i = index >> 5;
  113. mask = 1 << (index & 0x1f);
  114. tab[i] &= ~mask;
  115. }
  116. static inline int get_bit(uint32_t *tab, int index)
  117. {
  118. int i, mask;
  119. i = index >> 5;
  120. mask = 1 << (index & 0x1f);
  121. return !!(tab[i] & mask);
  122. }
  123. static void apic_local_deliver(CPUState *env, int vector)
  124. {
  125. APICState *s = env->apic_state;
  126. uint32_t lvt = s->lvt[vector];
  127. int trigger_mode;
  128. if (lvt & APIC_LVT_MASKED)
  129. return;
  130. switch ((lvt >> 8) & 7) {
  131. case APIC_DM_SMI:
  132. cpu_interrupt(env, CPU_INTERRUPT_SMI);
  133. break;
  134. case APIC_DM_NMI:
  135. cpu_interrupt(env, CPU_INTERRUPT_NMI);
  136. break;
  137. case APIC_DM_EXTINT:
  138. cpu_interrupt(env, 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(CPUState *env, int level)
  149. {
  150. if (level)
  151. apic_local_deliver(env, APIC_LVT_LINT0);
  152. else {
  153. APICState *s = env->apic_state;
  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. reset_bit(s->irr, lvt & 0xff);
  160. /* fall through */
  161. case APIC_DM_EXTINT:
  162. cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
  163. break;
  164. }
  165. }
  166. }
  167. #define foreach_apic(apic, deliver_bitmask, code) \
  168. {\
  169. int __i, __j, __mask;\
  170. for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
  171. __mask = deliver_bitmask[__i];\
  172. if (__mask) {\
  173. for(__j = 0; __j < 32; __j++) {\
  174. if (__mask & (1 << __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,
  186. uint8_t vector_num, uint8_t polarity,
  187. uint8_t trigger_mode)
  188. {
  189. APICState *apic_iter;
  190. switch (delivery_mode) {
  191. case APIC_DM_LOWPRI:
  192. /* XXX: search for focus processor, arbitration */
  193. {
  194. int i, d;
  195. d = -1;
  196. for(i = 0; i < MAX_APIC_WORDS; i++) {
  197. if (deliver_bitmask[i]) {
  198. d = i * 32 + ffs_bit(deliver_bitmask[i]);
  199. break;
  200. }
  201. }
  202. if (d >= 0) {
  203. apic_iter = local_apics[d];
  204. if (apic_iter) {
  205. apic_set_irq(apic_iter, vector_num, trigger_mode);
  206. }
  207. }
  208. }
  209. return;
  210. case APIC_DM_FIXED:
  211. break;
  212. case APIC_DM_SMI:
  213. foreach_apic(apic_iter, deliver_bitmask,
  214. cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_SMI) );
  215. return;
  216. case APIC_DM_NMI:
  217. foreach_apic(apic_iter, deliver_bitmask,
  218. cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_NMI) );
  219. return;
  220. case APIC_DM_INIT:
  221. /* normal INIT IPI sent to processors */
  222. foreach_apic(apic_iter, deliver_bitmask,
  223. apic_init_ipi(apic_iter) );
  224. return;
  225. case APIC_DM_EXTINT:
  226. /* handled in I/O APIC code */
  227. break;
  228. default:
  229. return;
  230. }
  231. foreach_apic(apic_iter, deliver_bitmask,
  232. apic_set_irq(apic_iter, vector_num, trigger_mode) );
  233. }
  234. void cpu_set_apic_base(CPUState *env, uint64_t val)
  235. {
  236. APICState *s = env->apic_state;
  237. #ifdef DEBUG_APIC
  238. printf("cpu_set_apic_base: %016" PRIx64 "\n", val);
  239. #endif
  240. s->apicbase = (val & 0xfffff000) |
  241. (s->apicbase & (MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE));
  242. /* if disabled, cannot be enabled again */
  243. if (!(val & MSR_IA32_APICBASE_ENABLE)) {
  244. s->apicbase &= ~MSR_IA32_APICBASE_ENABLE;
  245. env->cpuid_features &= ~CPUID_APIC;
  246. s->spurious_vec &= ~APIC_SV_ENABLE;
  247. }
  248. }
  249. uint64_t cpu_get_apic_base(CPUState *env)
  250. {
  251. APICState *s = env->apic_state;
  252. #ifdef DEBUG_APIC
  253. printf("cpu_get_apic_base: %016" PRIx64 "\n", (uint64_t)s->apicbase);
  254. #endif
  255. return s->apicbase;
  256. }
  257. void cpu_set_apic_tpr(CPUX86State *env, uint8_t val)
  258. {
  259. APICState *s = env->apic_state;
  260. s->tpr = (val & 0x0f) << 4;
  261. apic_update_irq(s);
  262. }
  263. uint8_t cpu_get_apic_tpr(CPUX86State *env)
  264. {
  265. APICState *s = env->apic_state;
  266. return s->tpr >> 4;
  267. }
  268. /* return -1 if no bit is set */
  269. static int get_highest_priority_int(uint32_t *tab)
  270. {
  271. int i;
  272. for(i = 7; i >= 0; i--) {
  273. if (tab[i] != 0) {
  274. return i * 32 + fls_bit(tab[i]);
  275. }
  276. }
  277. return -1;
  278. }
  279. static int apic_get_ppr(APICState *s)
  280. {
  281. int tpr, isrv, ppr;
  282. tpr = (s->tpr >> 4);
  283. isrv = get_highest_priority_int(s->isr);
  284. if (isrv < 0)
  285. isrv = 0;
  286. isrv >>= 4;
  287. if (tpr >= isrv)
  288. ppr = s->tpr;
  289. else
  290. ppr = isrv << 4;
  291. return ppr;
  292. }
  293. static int apic_get_arb_pri(APICState *s)
  294. {
  295. /* XXX: arbitration */
  296. return 0;
  297. }
  298. /* signal the CPU if an irq is pending */
  299. static void apic_update_irq(APICState *s)
  300. {
  301. int irrv, ppr;
  302. if (!(s->spurious_vec & APIC_SV_ENABLE))
  303. return;
  304. irrv = get_highest_priority_int(s->irr);
  305. if (irrv < 0)
  306. return;
  307. ppr = apic_get_ppr(s);
  308. if (ppr && (irrv & 0xf0) <= (ppr & 0xf0))
  309. return;
  310. cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
  311. }
  312. void apic_reset_irq_delivered(void)
  313. {
  314. apic_irq_delivered = 0;
  315. }
  316. int apic_get_irq_delivered(void)
  317. {
  318. return apic_irq_delivered;
  319. }
  320. static void apic_set_irq(APICState *s, int vector_num, int trigger_mode)
  321. {
  322. apic_irq_delivered += !get_bit(s->irr, vector_num);
  323. set_bit(s->irr, vector_num);
  324. if (trigger_mode)
  325. set_bit(s->tmr, vector_num);
  326. else
  327. reset_bit(s->tmr, vector_num);
  328. apic_update_irq(s);
  329. }
  330. static void apic_eoi(APICState *s)
  331. {
  332. int isrv;
  333. isrv = get_highest_priority_int(s->isr);
  334. if (isrv < 0)
  335. return;
  336. reset_bit(s->isr, isrv);
  337. /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
  338. set the remote IRR bit for level triggered interrupts. */
  339. apic_update_irq(s);
  340. }
  341. static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
  342. uint8_t dest, uint8_t dest_mode)
  343. {
  344. APICState *apic_iter;
  345. int i;
  346. if (dest_mode == 0) {
  347. if (dest == 0xff) {
  348. memset(deliver_bitmask, 0xff, MAX_APIC_WORDS * sizeof(uint32_t));
  349. } else {
  350. memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
  351. set_bit(deliver_bitmask, dest);
  352. }
  353. } else {
  354. /* XXX: cluster mode */
  355. memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
  356. for(i = 0; i < MAX_APICS; i++) {
  357. apic_iter = local_apics[i];
  358. if (apic_iter) {
  359. if (apic_iter->dest_mode == 0xf) {
  360. if (dest & apic_iter->log_dest)
  361. set_bit(deliver_bitmask, i);
  362. } else if (apic_iter->dest_mode == 0x0) {
  363. if ((dest & 0xf0) == (apic_iter->log_dest & 0xf0) &&
  364. (dest & apic_iter->log_dest & 0x0f)) {
  365. set_bit(deliver_bitmask, i);
  366. }
  367. }
  368. }
  369. }
  370. }
  371. }
  372. static void apic_init_ipi(APICState *s)
  373. {
  374. int i;
  375. s->tpr = 0;
  376. s->spurious_vec = 0xff;
  377. s->log_dest = 0;
  378. s->dest_mode = 0xf;
  379. memset(s->isr, 0, sizeof(s->isr));
  380. memset(s->tmr, 0, sizeof(s->tmr));
  381. memset(s->irr, 0, sizeof(s->irr));
  382. for(i = 0; i < APIC_LVT_NB; i++)
  383. s->lvt[i] = 1 << 16; /* mask LVT */
  384. s->esr = 0;
  385. memset(s->icr, 0, sizeof(s->icr));
  386. s->divide_conf = 0;
  387. s->count_shift = 0;
  388. s->initial_count = 0;
  389. s->initial_count_load_time = 0;
  390. s->next_time = 0;
  391. cpu_reset(s->cpu_env);
  392. if (!(s->apicbase & MSR_IA32_APICBASE_BSP))
  393. s->cpu_env->halted = 1;
  394. }
  395. /* send a SIPI message to the CPU to start it */
  396. static void apic_startup(APICState *s, int vector_num)
  397. {
  398. CPUState *env = s->cpu_env;
  399. if (!env->halted)
  400. return;
  401. env->eip = 0;
  402. cpu_x86_load_seg_cache(env, R_CS, vector_num << 8, vector_num << 12,
  403. 0xffff, 0);
  404. env->halted = 0;
  405. }
  406. static void apic_deliver(APICState *s, uint8_t dest, uint8_t dest_mode,
  407. uint8_t delivery_mode, uint8_t vector_num,
  408. uint8_t polarity, uint8_t trigger_mode)
  409. {
  410. uint32_t deliver_bitmask[MAX_APIC_WORDS];
  411. int dest_shorthand = (s->icr[0] >> 18) & 3;
  412. APICState *apic_iter;
  413. switch (dest_shorthand) {
  414. case 0:
  415. apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
  416. break;
  417. case 1:
  418. memset(deliver_bitmask, 0x00, sizeof(deliver_bitmask));
  419. set_bit(deliver_bitmask, s->id);
  420. break;
  421. case 2:
  422. memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
  423. break;
  424. case 3:
  425. memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
  426. reset_bit(deliver_bitmask, s->id);
  427. break;
  428. }
  429. switch (delivery_mode) {
  430. case APIC_DM_INIT:
  431. {
  432. int trig_mode = (s->icr[0] >> 15) & 1;
  433. int level = (s->icr[0] >> 14) & 1;
  434. if (level == 0 && trig_mode == 1) {
  435. foreach_apic(apic_iter, deliver_bitmask,
  436. apic_iter->arb_id = apic_iter->id );
  437. return;
  438. }
  439. }
  440. break;
  441. case APIC_DM_SIPI:
  442. foreach_apic(apic_iter, deliver_bitmask,
  443. apic_startup(apic_iter, vector_num) );
  444. return;
  445. }
  446. apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, polarity,
  447. trigger_mode);
  448. }
  449. int apic_get_interrupt(CPUState *env)
  450. {
  451. APICState *s = env->apic_state;
  452. int intno;
  453. /* if the APIC is installed or enabled, we let the 8259 handle the
  454. IRQs */
  455. if (!s)
  456. return -1;
  457. if (!(s->spurious_vec & APIC_SV_ENABLE))
  458. return -1;
  459. /* XXX: spurious IRQ handling */
  460. intno = get_highest_priority_int(s->irr);
  461. if (intno < 0)
  462. return -1;
  463. if (s->tpr && intno <= s->tpr)
  464. return s->spurious_vec & 0xff;
  465. reset_bit(s->irr, intno);
  466. set_bit(s->isr, intno);
  467. apic_update_irq(s);
  468. return intno;
  469. }
  470. int apic_accept_pic_intr(CPUState *env)
  471. {
  472. APICState *s = env->apic_state;
  473. uint32_t lvt0;
  474. if (!s)
  475. return -1;
  476. lvt0 = s->lvt[APIC_LVT_LINT0];
  477. if ((s->apicbase & MSR_IA32_APICBASE_ENABLE) == 0 ||
  478. (lvt0 & APIC_LVT_MASKED) == 0)
  479. return 1;
  480. return 0;
  481. }
  482. static uint32_t apic_get_current_count(APICState *s)
  483. {
  484. int64_t d;
  485. uint32_t val;
  486. d = (qemu_get_clock(vm_clock) - s->initial_count_load_time) >>
  487. s->count_shift;
  488. if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
  489. /* periodic */
  490. val = s->initial_count - (d % ((uint64_t)s->initial_count + 1));
  491. } else {
  492. if (d >= s->initial_count)
  493. val = 0;
  494. else
  495. val = s->initial_count - d;
  496. }
  497. return val;
  498. }
  499. static void apic_timer_update(APICState *s, int64_t current_time)
  500. {
  501. int64_t next_time, d;
  502. if (!(s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED)) {
  503. d = (current_time - s->initial_count_load_time) >>
  504. s->count_shift;
  505. if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
  506. if (!s->initial_count)
  507. goto no_timer;
  508. d = ((d / ((uint64_t)s->initial_count + 1)) + 1) * ((uint64_t)s->initial_count + 1);
  509. } else {
  510. if (d >= s->initial_count)
  511. goto no_timer;
  512. d = (uint64_t)s->initial_count + 1;
  513. }
  514. next_time = s->initial_count_load_time + (d << s->count_shift);
  515. qemu_mod_timer(s->timer, next_time);
  516. s->next_time = next_time;
  517. } else {
  518. no_timer:
  519. qemu_del_timer(s->timer);
  520. }
  521. }
  522. static void apic_timer(void *opaque)
  523. {
  524. APICState *s = opaque;
  525. apic_local_deliver(s->cpu_env, APIC_LVT_TIMER);
  526. apic_timer_update(s, s->next_time);
  527. }
  528. static uint32_t apic_mem_readb(void *opaque, target_phys_addr_t addr)
  529. {
  530. return 0;
  531. }
  532. static uint32_t apic_mem_readw(void *opaque, target_phys_addr_t addr)
  533. {
  534. return 0;
  535. }
  536. static void apic_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
  537. {
  538. }
  539. static void apic_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
  540. {
  541. }
  542. static uint32_t apic_mem_readl(void *opaque, target_phys_addr_t addr)
  543. {
  544. CPUState *env;
  545. APICState *s;
  546. uint32_t val;
  547. int index;
  548. env = cpu_single_env;
  549. if (!env)
  550. return 0;
  551. s = env->apic_state;
  552. index = (addr >> 4) & 0xff;
  553. switch(index) {
  554. case 0x02: /* id */
  555. val = s->id << 24;
  556. break;
  557. case 0x03: /* version */
  558. val = 0x11 | ((APIC_LVT_NB - 1) << 16); /* version 0x11 */
  559. break;
  560. case 0x08:
  561. val = s->tpr;
  562. break;
  563. case 0x09:
  564. val = apic_get_arb_pri(s);
  565. break;
  566. case 0x0a:
  567. /* ppr */
  568. val = apic_get_ppr(s);
  569. break;
  570. case 0x0b:
  571. val = 0;
  572. break;
  573. case 0x0d:
  574. val = s->log_dest << 24;
  575. break;
  576. case 0x0e:
  577. val = s->dest_mode << 28;
  578. break;
  579. case 0x0f:
  580. val = s->spurious_vec;
  581. break;
  582. case 0x10 ... 0x17:
  583. val = s->isr[index & 7];
  584. break;
  585. case 0x18 ... 0x1f:
  586. val = s->tmr[index & 7];
  587. break;
  588. case 0x20 ... 0x27:
  589. val = s->irr[index & 7];
  590. break;
  591. case 0x28:
  592. val = s->esr;
  593. break;
  594. case 0x30:
  595. case 0x31:
  596. val = s->icr[index & 1];
  597. break;
  598. case 0x32 ... 0x37:
  599. val = s->lvt[index - 0x32];
  600. break;
  601. case 0x38:
  602. val = s->initial_count;
  603. break;
  604. case 0x39:
  605. val = apic_get_current_count(s);
  606. break;
  607. case 0x3e:
  608. val = s->divide_conf;
  609. break;
  610. default:
  611. s->esr |= ESR_ILLEGAL_ADDRESS;
  612. val = 0;
  613. break;
  614. }
  615. #ifdef DEBUG_APIC
  616. printf("APIC read: %08x = %08x\n", (uint32_t)addr, val);
  617. #endif
  618. return val;
  619. }
  620. static void apic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
  621. {
  622. CPUState *env;
  623. APICState *s;
  624. int index;
  625. env = cpu_single_env;
  626. if (!env)
  627. return;
  628. s = env->apic_state;
  629. #ifdef DEBUG_APIC
  630. printf("APIC write: %08x = %08x\n", (uint32_t)addr, val);
  631. #endif
  632. index = (addr >> 4) & 0xff;
  633. switch(index) {
  634. case 0x02:
  635. s->id = (val >> 24);
  636. break;
  637. case 0x03:
  638. break;
  639. case 0x08:
  640. s->tpr = val;
  641. apic_update_irq(s);
  642. break;
  643. case 0x09:
  644. case 0x0a:
  645. break;
  646. case 0x0b: /* EOI */
  647. apic_eoi(s);
  648. break;
  649. case 0x0d:
  650. s->log_dest = val >> 24;
  651. break;
  652. case 0x0e:
  653. s->dest_mode = val >> 28;
  654. break;
  655. case 0x0f:
  656. s->spurious_vec = val & 0x1ff;
  657. apic_update_irq(s);
  658. break;
  659. case 0x10 ... 0x17:
  660. case 0x18 ... 0x1f:
  661. case 0x20 ... 0x27:
  662. case 0x28:
  663. break;
  664. case 0x30:
  665. s->icr[0] = val;
  666. apic_deliver(s, (s->icr[1] >> 24) & 0xff, (s->icr[0] >> 11) & 1,
  667. (s->icr[0] >> 8) & 7, (s->icr[0] & 0xff),
  668. (s->icr[0] >> 14) & 1, (s->icr[0] >> 15) & 1);
  669. break;
  670. case 0x31:
  671. s->icr[1] = val;
  672. break;
  673. case 0x32 ... 0x37:
  674. {
  675. int n = index - 0x32;
  676. s->lvt[n] = val;
  677. if (n == APIC_LVT_TIMER)
  678. apic_timer_update(s, qemu_get_clock(vm_clock));
  679. }
  680. break;
  681. case 0x38:
  682. s->initial_count = val;
  683. s->initial_count_load_time = qemu_get_clock(vm_clock);
  684. apic_timer_update(s, s->initial_count_load_time);
  685. break;
  686. case 0x39:
  687. break;
  688. case 0x3e:
  689. {
  690. int v;
  691. s->divide_conf = val & 0xb;
  692. v = (s->divide_conf & 3) | ((s->divide_conf >> 1) & 4);
  693. s->count_shift = (v + 1) & 7;
  694. }
  695. break;
  696. default:
  697. s->esr |= ESR_ILLEGAL_ADDRESS;
  698. break;
  699. }
  700. }
  701. static void apic_save(QEMUFile *f, void *opaque)
  702. {
  703. APICState *s = opaque;
  704. int i;
  705. qemu_put_be32s(f, &s->apicbase);
  706. qemu_put_8s(f, &s->id);
  707. qemu_put_8s(f, &s->arb_id);
  708. qemu_put_8s(f, &s->tpr);
  709. qemu_put_be32s(f, &s->spurious_vec);
  710. qemu_put_8s(f, &s->log_dest);
  711. qemu_put_8s(f, &s->dest_mode);
  712. for (i = 0; i < 8; i++) {
  713. qemu_put_be32s(f, &s->isr[i]);
  714. qemu_put_be32s(f, &s->tmr[i]);
  715. qemu_put_be32s(f, &s->irr[i]);
  716. }
  717. for (i = 0; i < APIC_LVT_NB; i++) {
  718. qemu_put_be32s(f, &s->lvt[i]);
  719. }
  720. qemu_put_be32s(f, &s->esr);
  721. qemu_put_be32s(f, &s->icr[0]);
  722. qemu_put_be32s(f, &s->icr[1]);
  723. qemu_put_be32s(f, &s->divide_conf);
  724. qemu_put_be32(f, s->count_shift);
  725. qemu_put_be32s(f, &s->initial_count);
  726. qemu_put_be64(f, s->initial_count_load_time);
  727. qemu_put_be64(f, s->next_time);
  728. qemu_put_timer(f, s->timer);
  729. }
  730. static int apic_load(QEMUFile *f, void *opaque, int version_id)
  731. {
  732. APICState *s = opaque;
  733. int i;
  734. if (version_id > 2)
  735. return -EINVAL;
  736. /* XXX: what if the base changes? (registered memory regions) */
  737. qemu_get_be32s(f, &s->apicbase);
  738. qemu_get_8s(f, &s->id);
  739. qemu_get_8s(f, &s->arb_id);
  740. qemu_get_8s(f, &s->tpr);
  741. qemu_get_be32s(f, &s->spurious_vec);
  742. qemu_get_8s(f, &s->log_dest);
  743. qemu_get_8s(f, &s->dest_mode);
  744. for (i = 0; i < 8; i++) {
  745. qemu_get_be32s(f, &s->isr[i]);
  746. qemu_get_be32s(f, &s->tmr[i]);
  747. qemu_get_be32s(f, &s->irr[i]);
  748. }
  749. for (i = 0; i < APIC_LVT_NB; i++) {
  750. qemu_get_be32s(f, &s->lvt[i]);
  751. }
  752. qemu_get_be32s(f, &s->esr);
  753. qemu_get_be32s(f, &s->icr[0]);
  754. qemu_get_be32s(f, &s->icr[1]);
  755. qemu_get_be32s(f, &s->divide_conf);
  756. s->count_shift=qemu_get_be32(f);
  757. qemu_get_be32s(f, &s->initial_count);
  758. s->initial_count_load_time=qemu_get_be64(f);
  759. s->next_time=qemu_get_be64(f);
  760. if (version_id >= 2)
  761. qemu_get_timer(f, s->timer);
  762. return 0;
  763. }
  764. static void apic_reset(void *opaque)
  765. {
  766. APICState *s = opaque;
  767. s->apicbase = 0xfee00000 |
  768. (s->id ? 0 : MSR_IA32_APICBASE_BSP) | MSR_IA32_APICBASE_ENABLE;
  769. apic_init_ipi(s);
  770. if (s->id == 0) {
  771. /*
  772. * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
  773. * time typically by BIOS, so PIC interrupt can be delivered to the
  774. * processor when local APIC is enabled.
  775. */
  776. s->lvt[APIC_LVT_LINT0] = 0x700;
  777. }
  778. }
  779. static CPUReadMemoryFunc *apic_mem_read[3] = {
  780. apic_mem_readb,
  781. apic_mem_readw,
  782. apic_mem_readl,
  783. };
  784. static CPUWriteMemoryFunc *apic_mem_write[3] = {
  785. apic_mem_writeb,
  786. apic_mem_writew,
  787. apic_mem_writel,
  788. };
  789. int apic_init(CPUState *env)
  790. {
  791. APICState *s;
  792. if (last_apic_id >= MAX_APICS)
  793. return -1;
  794. s = qemu_mallocz(sizeof(APICState));
  795. env->apic_state = s;
  796. s->id = last_apic_id++;
  797. env->cpuid_apic_id = s->id;
  798. s->cpu_env = env;
  799. apic_reset(s);
  800. /* XXX: mapping more APICs at the same memory location */
  801. if (apic_io_memory == 0) {
  802. /* NOTE: the APIC is directly connected to the CPU - it is not
  803. on the global memory bus. */
  804. apic_io_memory = cpu_register_io_memory(0, apic_mem_read,
  805. apic_mem_write, NULL);
  806. cpu_register_physical_memory(s->apicbase & ~0xfff, 0x1000,
  807. apic_io_memory);
  808. }
  809. s->timer = qemu_new_timer(vm_clock, apic_timer, s);
  810. register_savevm("apic", s->id, 2, apic_save, apic_load, s);
  811. qemu_register_reset(apic_reset, s);
  812. local_apics[s->id] = s;
  813. return 0;
  814. }
  815. static void ioapic_service(IOAPICState *s)
  816. {
  817. uint8_t i;
  818. uint8_t trig_mode;
  819. uint8_t vector;
  820. uint8_t delivery_mode;
  821. uint32_t mask;
  822. uint64_t entry;
  823. uint8_t dest;
  824. uint8_t dest_mode;
  825. uint8_t polarity;
  826. uint32_t deliver_bitmask[MAX_APIC_WORDS];
  827. for (i = 0; i < IOAPIC_NUM_PINS; i++) {
  828. mask = 1 << i;
  829. if (s->irr & mask) {
  830. entry = s->ioredtbl[i];
  831. if (!(entry & APIC_LVT_MASKED)) {
  832. trig_mode = ((entry >> 15) & 1);
  833. dest = entry >> 56;
  834. dest_mode = (entry >> 11) & 1;
  835. delivery_mode = (entry >> 8) & 7;
  836. polarity = (entry >> 13) & 1;
  837. if (trig_mode == APIC_TRIGGER_EDGE)
  838. s->irr &= ~mask;
  839. if (delivery_mode == APIC_DM_EXTINT)
  840. vector = pic_read_irq(isa_pic);
  841. else
  842. vector = entry & 0xff;
  843. apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
  844. apic_bus_deliver(deliver_bitmask, delivery_mode,
  845. vector, polarity, trig_mode);
  846. }
  847. }
  848. }
  849. }
  850. void ioapic_set_irq(void *opaque, int vector, int level)
  851. {
  852. IOAPICState *s = opaque;
  853. /* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
  854. * to GSI 2. GSI maps to ioapic 1-1. This is not
  855. * the cleanest way of doing it but it should work. */
  856. if (vector == 0)
  857. vector = 2;
  858. if (vector >= 0 && vector < IOAPIC_NUM_PINS) {
  859. uint32_t mask = 1 << vector;
  860. uint64_t entry = s->ioredtbl[vector];
  861. if ((entry >> 15) & 1) {
  862. /* level triggered */
  863. if (level) {
  864. s->irr |= mask;
  865. ioapic_service(s);
  866. } else {
  867. s->irr &= ~mask;
  868. }
  869. } else {
  870. /* edge triggered */
  871. if (level) {
  872. s->irr |= mask;
  873. ioapic_service(s);
  874. }
  875. }
  876. }
  877. }
  878. static uint32_t ioapic_mem_readl(void *opaque, target_phys_addr_t addr)
  879. {
  880. IOAPICState *s = opaque;
  881. int index;
  882. uint32_t val = 0;
  883. addr &= 0xff;
  884. if (addr == 0x00) {
  885. val = s->ioregsel;
  886. } else if (addr == 0x10) {
  887. switch (s->ioregsel) {
  888. case 0x00:
  889. val = s->id << 24;
  890. break;
  891. case 0x01:
  892. val = 0x11 | ((IOAPIC_NUM_PINS - 1) << 16); /* version 0x11 */
  893. break;
  894. case 0x02:
  895. val = 0;
  896. break;
  897. default:
  898. index = (s->ioregsel - 0x10) >> 1;
  899. if (index >= 0 && index < IOAPIC_NUM_PINS) {
  900. if (s->ioregsel & 1)
  901. val = s->ioredtbl[index] >> 32;
  902. else
  903. val = s->ioredtbl[index] & 0xffffffff;
  904. }
  905. }
  906. #ifdef DEBUG_IOAPIC
  907. printf("I/O APIC read: %08x = %08x\n", s->ioregsel, val);
  908. #endif
  909. }
  910. return val;
  911. }
  912. static void ioapic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
  913. {
  914. IOAPICState *s = opaque;
  915. int index;
  916. addr &= 0xff;
  917. if (addr == 0x00) {
  918. s->ioregsel = val;
  919. return;
  920. } else if (addr == 0x10) {
  921. #ifdef DEBUG_IOAPIC
  922. printf("I/O APIC write: %08x = %08x\n", s->ioregsel, val);
  923. #endif
  924. switch (s->ioregsel) {
  925. case 0x00:
  926. s->id = (val >> 24) & 0xff;
  927. return;
  928. case 0x01:
  929. case 0x02:
  930. return;
  931. default:
  932. index = (s->ioregsel - 0x10) >> 1;
  933. if (index >= 0 && index < IOAPIC_NUM_PINS) {
  934. if (s->ioregsel & 1) {
  935. s->ioredtbl[index] &= 0xffffffff;
  936. s->ioredtbl[index] |= (uint64_t)val << 32;
  937. } else {
  938. s->ioredtbl[index] &= ~0xffffffffULL;
  939. s->ioredtbl[index] |= val;
  940. }
  941. ioapic_service(s);
  942. }
  943. }
  944. }
  945. }
  946. static void ioapic_save(QEMUFile *f, void *opaque)
  947. {
  948. IOAPICState *s = opaque;
  949. int i;
  950. qemu_put_8s(f, &s->id);
  951. qemu_put_8s(f, &s->ioregsel);
  952. for (i = 0; i < IOAPIC_NUM_PINS; i++) {
  953. qemu_put_be64s(f, &s->ioredtbl[i]);
  954. }
  955. }
  956. static int ioapic_load(QEMUFile *f, void *opaque, int version_id)
  957. {
  958. IOAPICState *s = opaque;
  959. int i;
  960. if (version_id != 1)
  961. return -EINVAL;
  962. qemu_get_8s(f, &s->id);
  963. qemu_get_8s(f, &s->ioregsel);
  964. for (i = 0; i < IOAPIC_NUM_PINS; i++) {
  965. qemu_get_be64s(f, &s->ioredtbl[i]);
  966. }
  967. return 0;
  968. }
  969. static void ioapic_reset(void *opaque)
  970. {
  971. IOAPICState *s = opaque;
  972. int i;
  973. memset(s, 0, sizeof(*s));
  974. for(i = 0; i < IOAPIC_NUM_PINS; i++)
  975. s->ioredtbl[i] = 1 << 16; /* mask LVT */
  976. }
  977. static CPUReadMemoryFunc *ioapic_mem_read[3] = {
  978. ioapic_mem_readl,
  979. ioapic_mem_readl,
  980. ioapic_mem_readl,
  981. };
  982. static CPUWriteMemoryFunc *ioapic_mem_write[3] = {
  983. ioapic_mem_writel,
  984. ioapic_mem_writel,
  985. ioapic_mem_writel,
  986. };
  987. IOAPICState *ioapic_init(void)
  988. {
  989. IOAPICState *s;
  990. int io_memory;
  991. s = qemu_mallocz(sizeof(IOAPICState));
  992. ioapic_reset(s);
  993. s->id = last_apic_id++;
  994. io_memory = cpu_register_io_memory(0, ioapic_mem_read,
  995. ioapic_mem_write, s);
  996. cpu_register_physical_memory(0xfec00000, 0x1000, io_memory);
  997. register_savevm("ioapic", 0, 1, ioapic_save, ioapic_load, s);
  998. qemu_register_reset(ioapic_reset, s);
  999. return s;
  1000. }