ioapic.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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.h"
  23. #include "pc.h"
  24. #include "apic.h"
  25. #include "ioapic.h"
  26. #include "ioapic_internal.h"
  27. //#define DEBUG_IOAPIC
  28. #ifdef DEBUG_IOAPIC
  29. #define DPRINTF(fmt, ...) \
  30. do { printf("ioapic: " fmt , ## __VA_ARGS__); } while (0)
  31. #else
  32. #define DPRINTF(fmt, ...)
  33. #endif
  34. static IOAPICCommonState *ioapics[MAX_IOAPICS];
  35. static void ioapic_service(IOAPICCommonState *s)
  36. {
  37. uint8_t i;
  38. uint8_t trig_mode;
  39. uint8_t vector;
  40. uint8_t delivery_mode;
  41. uint32_t mask;
  42. uint64_t entry;
  43. uint8_t dest;
  44. uint8_t dest_mode;
  45. for (i = 0; i < IOAPIC_NUM_PINS; i++) {
  46. mask = 1 << i;
  47. if (s->irr & mask) {
  48. entry = s->ioredtbl[i];
  49. if (!(entry & IOAPIC_LVT_MASKED)) {
  50. trig_mode = ((entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1);
  51. dest = entry >> IOAPIC_LVT_DEST_SHIFT;
  52. dest_mode = (entry >> IOAPIC_LVT_DEST_MODE_SHIFT) & 1;
  53. delivery_mode =
  54. (entry >> IOAPIC_LVT_DELIV_MODE_SHIFT) & IOAPIC_DM_MASK;
  55. if (trig_mode == IOAPIC_TRIGGER_EDGE) {
  56. s->irr &= ~mask;
  57. } else {
  58. s->ioredtbl[i] |= IOAPIC_LVT_REMOTE_IRR;
  59. }
  60. if (delivery_mode == IOAPIC_DM_EXTINT) {
  61. vector = pic_read_irq(isa_pic);
  62. } else {
  63. vector = entry & IOAPIC_VECTOR_MASK;
  64. }
  65. apic_deliver_irq(dest, dest_mode, delivery_mode,
  66. vector, trig_mode);
  67. }
  68. }
  69. }
  70. }
  71. static void ioapic_set_irq(void *opaque, int vector, int level)
  72. {
  73. IOAPICCommonState *s = opaque;
  74. /* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
  75. * to GSI 2. GSI maps to ioapic 1-1. This is not
  76. * the cleanest way of doing it but it should work. */
  77. DPRINTF("%s: %s vec %x\n", __func__, level ? "raise" : "lower", vector);
  78. if (vector == 0) {
  79. vector = 2;
  80. }
  81. if (vector >= 0 && vector < IOAPIC_NUM_PINS) {
  82. uint32_t mask = 1 << vector;
  83. uint64_t entry = s->ioredtbl[vector];
  84. if (entry & (1 << IOAPIC_LVT_POLARITY_SHIFT)) {
  85. level = !level;
  86. }
  87. if (((entry >> IOAPIC_LVT_TRIGGER_MODE_SHIFT) & 1) ==
  88. IOAPIC_TRIGGER_LEVEL) {
  89. /* level triggered */
  90. if (level) {
  91. s->irr |= mask;
  92. ioapic_service(s);
  93. } else {
  94. s->irr &= ~mask;
  95. }
  96. } else {
  97. /* According to the 82093AA manual, we must ignore edge requests
  98. * if the input pin is masked. */
  99. if (level && !(entry & IOAPIC_LVT_MASKED)) {
  100. s->irr |= mask;
  101. ioapic_service(s);
  102. }
  103. }
  104. }
  105. }
  106. void ioapic_eoi_broadcast(int vector)
  107. {
  108. IOAPICCommonState *s;
  109. uint64_t entry;
  110. int i, n;
  111. for (i = 0; i < MAX_IOAPICS; i++) {
  112. s = ioapics[i];
  113. if (!s) {
  114. continue;
  115. }
  116. for (n = 0; n < IOAPIC_NUM_PINS; n++) {
  117. entry = s->ioredtbl[n];
  118. if ((entry & IOAPIC_LVT_REMOTE_IRR)
  119. && (entry & IOAPIC_VECTOR_MASK) == vector) {
  120. s->ioredtbl[n] = entry & ~IOAPIC_LVT_REMOTE_IRR;
  121. if (!(entry & IOAPIC_LVT_MASKED) && (s->irr & (1 << n))) {
  122. ioapic_service(s);
  123. }
  124. }
  125. }
  126. }
  127. }
  128. static uint64_t
  129. ioapic_mem_read(void *opaque, hwaddr addr, unsigned int size)
  130. {
  131. IOAPICCommonState *s = opaque;
  132. int index;
  133. uint32_t val = 0;
  134. switch (addr & 0xff) {
  135. case IOAPIC_IOREGSEL:
  136. val = s->ioregsel;
  137. break;
  138. case IOAPIC_IOWIN:
  139. if (size != 4) {
  140. break;
  141. }
  142. switch (s->ioregsel) {
  143. case IOAPIC_REG_ID:
  144. val = s->id << IOAPIC_ID_SHIFT;
  145. break;
  146. case IOAPIC_REG_VER:
  147. val = IOAPIC_VERSION |
  148. ((IOAPIC_NUM_PINS - 1) << IOAPIC_VER_ENTRIES_SHIFT);
  149. break;
  150. case IOAPIC_REG_ARB:
  151. val = 0;
  152. break;
  153. default:
  154. index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1;
  155. if (index >= 0 && index < IOAPIC_NUM_PINS) {
  156. if (s->ioregsel & 1) {
  157. val = s->ioredtbl[index] >> 32;
  158. } else {
  159. val = s->ioredtbl[index] & 0xffffffff;
  160. }
  161. }
  162. }
  163. DPRINTF("read: %08x = %08x\n", s->ioregsel, val);
  164. break;
  165. }
  166. return val;
  167. }
  168. static void
  169. ioapic_mem_write(void *opaque, hwaddr addr, uint64_t val,
  170. unsigned int size)
  171. {
  172. IOAPICCommonState *s = opaque;
  173. int index;
  174. switch (addr & 0xff) {
  175. case IOAPIC_IOREGSEL:
  176. s->ioregsel = val;
  177. break;
  178. case IOAPIC_IOWIN:
  179. if (size != 4) {
  180. break;
  181. }
  182. DPRINTF("write: %08x = %08" PRIx64 "\n", s->ioregsel, val);
  183. switch (s->ioregsel) {
  184. case IOAPIC_REG_ID:
  185. s->id = (val >> IOAPIC_ID_SHIFT) & IOAPIC_ID_MASK;
  186. break;
  187. case IOAPIC_REG_VER:
  188. case IOAPIC_REG_ARB:
  189. break;
  190. default:
  191. index = (s->ioregsel - IOAPIC_REG_REDTBL_BASE) >> 1;
  192. if (index >= 0 && index < IOAPIC_NUM_PINS) {
  193. if (s->ioregsel & 1) {
  194. s->ioredtbl[index] &= 0xffffffff;
  195. s->ioredtbl[index] |= (uint64_t)val << 32;
  196. } else {
  197. s->ioredtbl[index] &= ~0xffffffffULL;
  198. s->ioredtbl[index] |= val;
  199. }
  200. ioapic_service(s);
  201. }
  202. }
  203. break;
  204. }
  205. }
  206. static const MemoryRegionOps ioapic_io_ops = {
  207. .read = ioapic_mem_read,
  208. .write = ioapic_mem_write,
  209. .endianness = DEVICE_NATIVE_ENDIAN,
  210. };
  211. static void ioapic_init(IOAPICCommonState *s, int instance_no)
  212. {
  213. memory_region_init_io(&s->io_memory, &ioapic_io_ops, s, "ioapic", 0x1000);
  214. qdev_init_gpio_in(&s->busdev.qdev, ioapic_set_irq, IOAPIC_NUM_PINS);
  215. ioapics[instance_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->init = ioapic_init;
  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)