ioapic.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * ioapic.c IOAPIC emulation logic
  3. *
  4. * Copyright (c) 2004-2005 Fabrice Bellard
  5. *
  6. * Split the ioapic logic from apic.c
  7. * Xiantao Zhang <xiantao.zhang@intel.com>
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include "hw/hw.h"
  23. #include "hw/i386/pc.h"
  24. #include "hw/i386/ioapic.h"
  25. #include "hw/i386/ioapic_internal.h"
  26. //#define DEBUG_IOAPIC
  27. #ifdef DEBUG_IOAPIC
  28. #define DPRINTF(fmt, ...) \
  29. do { printf("ioapic: " fmt , ## __VA_ARGS__); } while (0)
  30. #else
  31. #define DPRINTF(fmt, ...)
  32. #endif
  33. static IOAPICCommonState *ioapics[MAX_IOAPICS];
  34. /* global variable from ioapic_common.c */
  35. extern int ioapic_no;
  36. static void ioapic_service(IOAPICCommonState *s)
  37. {
  38. uint8_t i;
  39. uint8_t trig_mode;
  40. uint8_t vector;
  41. uint8_t delivery_mode;
  42. uint32_t mask;
  43. uint64_t entry;
  44. uint8_t dest;
  45. uint8_t dest_mode;
  46. for (i = 0; i < IOAPIC_NUM_PINS; i++) {
  47. mask = 1 << i;
  48. if (s->irr & mask) {
  49. entry = s->ioredtbl[i];
  50. if (!(entry & IOAPIC_LVT_MASKED)) {
  51. trig_mode = ((entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1);
  52. dest = entry >> IOAPIC_LVT_DEST_SHIFT;
  53. dest_mode = (entry >> IOAPIC_LVT_DEST_MODE_SHIFT) & 1;
  54. delivery_mode =
  55. (entry >> IOAPIC_LVT_DELIV_MODE_SHIFT) & IOAPIC_DM_MASK;
  56. if (trig_mode == IOAPIC_TRIGGER_EDGE) {
  57. s->irr &= ~mask;
  58. } else {
  59. s->ioredtbl[i] |= IOAPIC_LVT_REMOTE_IRR;
  60. }
  61. if (delivery_mode == IOAPIC_DM_EXTINT) {
  62. vector = pic_read_irq(isa_pic);
  63. } else {
  64. vector = entry & IOAPIC_VECTOR_MASK;
  65. }
  66. apic_deliver_irq(dest, dest_mode, delivery_mode,
  67. vector, trig_mode);
  68. }
  69. }
  70. }
  71. }
  72. static void ioapic_set_irq(void *opaque, int vector, int level)
  73. {
  74. IOAPICCommonState *s = opaque;
  75. /* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
  76. * to GSI 2. GSI maps to ioapic 1-1. This is not
  77. * the cleanest way of doing it but it should work. */
  78. DPRINTF("%s: %s vec %x\n", __func__, level ? "raise" : "lower", vector);
  79. if (vector == 0) {
  80. vector = 2;
  81. }
  82. if (vector >= 0 && vector < IOAPIC_NUM_PINS) {
  83. uint32_t mask = 1 << vector;
  84. uint64_t entry = s->ioredtbl[vector];
  85. if (((entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1) ==
  86. IOAPIC_TRIGGER_LEVEL) {
  87. /* level triggered */
  88. if (level) {
  89. s->irr |= mask;
  90. ioapic_service(s);
  91. } else {
  92. s->irr &= ~mask;
  93. }
  94. } else {
  95. /* According to the 82093AA manual, we must ignore edge requests
  96. * if the input pin is masked. */
  97. if (level && !(entry & IOAPIC_LVT_MASKED)) {
  98. s->irr |= mask;
  99. ioapic_service(s);
  100. }
  101. }
  102. }
  103. }
  104. void ioapic_eoi_broadcast(int vector)
  105. {
  106. IOAPICCommonState *s;
  107. uint64_t entry;
  108. int i, n;
  109. for (i = 0; i < MAX_IOAPICS; i++) {
  110. s = ioapics[i];
  111. if (!s) {
  112. continue;
  113. }
  114. for (n = 0; n < IOAPIC_NUM_PINS; n++) {
  115. entry = s->ioredtbl[n];
  116. if ((entry & IOAPIC_LVT_REMOTE_IRR)
  117. && (entry & IOAPIC_VECTOR_MASK) == vector) {
  118. s->ioredtbl[n] = entry & ~IOAPIC_LVT_REMOTE_IRR;
  119. if (!(entry & IOAPIC_LVT_MASKED) && (s->irr & (1 << n))) {
  120. ioapic_service(s);
  121. }
  122. }
  123. }
  124. }
  125. }
  126. static uint64_t
  127. ioapic_mem_read(void *opaque, hwaddr addr, unsigned int size)
  128. {
  129. IOAPICCommonState *s = opaque;
  130. int index;
  131. uint32_t val = 0;
  132. switch (addr & 0xff) {
  133. case IOAPIC_IOREGSEL:
  134. val = s->ioregsel;
  135. break;
  136. case IOAPIC_IOWIN:
  137. if (size != 4) {
  138. break;
  139. }
  140. switch (s->ioregsel) {
  141. case IOAPIC_REG_ID:
  142. val = s->id << IOAPIC_ID_SHIFT;
  143. break;
  144. case IOAPIC_REG_VER:
  145. val = IOAPIC_VERSION |
  146. ((IOAPIC_NUM_PINS - 1) << IOAPIC_VER_ENTRIES_SHIFT);
  147. break;
  148. case IOAPIC_REG_ARB:
  149. val = 0;
  150. break;
  151. default:
  152. index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1;
  153. if (index >= 0 && index < IOAPIC_NUM_PINS) {
  154. if (s->ioregsel & 1) {
  155. val = s->ioredtbl[index] >> 32;
  156. } else {
  157. val = s->ioredtbl[index] & 0xffffffff;
  158. }
  159. }
  160. }
  161. DPRINTF("read: %08x = %08x\n", s->ioregsel, val);
  162. break;
  163. }
  164. return val;
  165. }
  166. static void
  167. ioapic_mem_write(void *opaque, hwaddr addr, uint64_t val,
  168. unsigned int size)
  169. {
  170. IOAPICCommonState *s = opaque;
  171. int index;
  172. switch (addr & 0xff) {
  173. case IOAPIC_IOREGSEL:
  174. s->ioregsel = val;
  175. break;
  176. case IOAPIC_IOWIN:
  177. if (size != 4) {
  178. break;
  179. }
  180. DPRINTF("write: %08x = %08" PRIx64 "\n", s->ioregsel, val);
  181. switch (s->ioregsel) {
  182. case IOAPIC_REG_ID:
  183. s->id = (val >> IOAPIC_ID_SHIFT) & IOAPIC_ID_MASK;
  184. break;
  185. case IOAPIC_REG_VER:
  186. case IOAPIC_REG_ARB:
  187. break;
  188. default:
  189. index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1;
  190. if (index >= 0 && index < IOAPIC_NUM_PINS) {
  191. if (s->ioregsel & 1) {
  192. s->ioredtbl[index] &= 0xffffffff;
  193. s->ioredtbl[index] |= (uint64_t)val << 32;
  194. } else {
  195. s->ioredtbl[index] &= ~0xffffffffULL;
  196. s->ioredtbl[index] |= val;
  197. }
  198. ioapic_service(s);
  199. }
  200. }
  201. break;
  202. }
  203. }
  204. static const MemoryRegionOps ioapic_io_ops = {
  205. .read = ioapic_mem_read,
  206. .write = ioapic_mem_write,
  207. .endianness = DEVICE_NATIVE_ENDIAN,
  208. };
  209. static void ioapic_realize(DeviceState *dev, Error **errp)
  210. {
  211. IOAPICCommonState *s = IOAPIC_COMMON(dev);
  212. memory_region_init_io(&s->io_memory, OBJECT(s), &ioapic_io_ops, s,
  213. "ioapic", 0x1000);
  214. qdev_init_gpio_in(dev, ioapic_set_irq, IOAPIC_NUM_PINS);
  215. ioapics[ioapic_no] = s;
  216. }
  217. static void ioapic_class_init(ObjectClass *klass, void *data)
  218. {
  219. IOAPICCommonClass *k = IOAPIC_COMMON_CLASS(klass);
  220. DeviceClass *dc = DEVICE_CLASS(klass);
  221. k->realize = ioapic_realize;
  222. dc->reset = ioapic_reset_common;
  223. }
  224. static const TypeInfo ioapic_info = {
  225. .name = "ioapic",
  226. .parent = TYPE_IOAPIC_COMMON,
  227. .instance_size = sizeof(IOAPICCommonState),
  228. .class_init = ioapic_class_init,
  229. };
  230. static void ioapic_register_types(void)
  231. {
  232. type_register_static(&ioapic_info);
  233. }
  234. type_init(ioapic_register_types)