2
0

pl190.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Arm PrimeCell PL190 Vector Interrupt Controller
  3. *
  4. * Copyright (c) 2006 CodeSourcery.
  5. * Written by Paul Brook
  6. *
  7. * This code is licensed under the GPL.
  8. */
  9. #include "qemu/osdep.h"
  10. #include "hw/irq.h"
  11. #include "hw/sysbus.h"
  12. #include "migration/vmstate.h"
  13. #include "qemu/log.h"
  14. #include "qemu/module.h"
  15. /* The number of virtual priority levels. 16 user vectors plus the
  16. unvectored IRQ. Chained interrupts would require an additional level
  17. if implemented. */
  18. #define PL190_NUM_PRIO 17
  19. #define TYPE_PL190 "pl190"
  20. #define PL190(obj) OBJECT_CHECK(PL190State, (obj), TYPE_PL190)
  21. typedef struct PL190State {
  22. SysBusDevice parent_obj;
  23. MemoryRegion iomem;
  24. uint32_t level;
  25. uint32_t soft_level;
  26. uint32_t irq_enable;
  27. uint32_t fiq_select;
  28. uint8_t vect_control[16];
  29. uint32_t vect_addr[PL190_NUM_PRIO];
  30. /* Mask containing interrupts with higher priority than this one. */
  31. uint32_t prio_mask[PL190_NUM_PRIO + 1];
  32. int protected;
  33. /* Current priority level. */
  34. int priority;
  35. int prev_prio[PL190_NUM_PRIO];
  36. qemu_irq irq;
  37. qemu_irq fiq;
  38. } PL190State;
  39. static const unsigned char pl190_id[] =
  40. { 0x90, 0x11, 0x04, 0x00, 0x0D, 0xf0, 0x05, 0xb1 };
  41. static inline uint32_t pl190_irq_level(PL190State *s)
  42. {
  43. return (s->level | s->soft_level) & s->irq_enable & ~s->fiq_select;
  44. }
  45. /* Update interrupts. */
  46. static void pl190_update(PL190State *s)
  47. {
  48. uint32_t level = pl190_irq_level(s);
  49. int set;
  50. set = (level & s->prio_mask[s->priority]) != 0;
  51. qemu_set_irq(s->irq, set);
  52. set = ((s->level | s->soft_level) & s->fiq_select) != 0;
  53. qemu_set_irq(s->fiq, set);
  54. }
  55. static void pl190_set_irq(void *opaque, int irq, int level)
  56. {
  57. PL190State *s = (PL190State *)opaque;
  58. if (level)
  59. s->level |= 1u << irq;
  60. else
  61. s->level &= ~(1u << irq);
  62. pl190_update(s);
  63. }
  64. static void pl190_update_vectors(PL190State *s)
  65. {
  66. uint32_t mask;
  67. int i;
  68. int n;
  69. mask = 0;
  70. for (i = 0; i < 16; i++)
  71. {
  72. s->prio_mask[i] = mask;
  73. if (s->vect_control[i] & 0x20)
  74. {
  75. n = s->vect_control[i] & 0x1f;
  76. mask |= 1 << n;
  77. }
  78. }
  79. s->prio_mask[16] = mask;
  80. pl190_update(s);
  81. }
  82. static uint64_t pl190_read(void *opaque, hwaddr offset,
  83. unsigned size)
  84. {
  85. PL190State *s = (PL190State *)opaque;
  86. int i;
  87. if (offset >= 0xfe0 && offset < 0x1000) {
  88. return pl190_id[(offset - 0xfe0) >> 2];
  89. }
  90. if (offset >= 0x100 && offset < 0x140) {
  91. return s->vect_addr[(offset - 0x100) >> 2];
  92. }
  93. if (offset >= 0x200 && offset < 0x240) {
  94. return s->vect_control[(offset - 0x200) >> 2];
  95. }
  96. switch (offset >> 2) {
  97. case 0: /* IRQSTATUS */
  98. return pl190_irq_level(s);
  99. case 1: /* FIQSATUS */
  100. return (s->level | s->soft_level) & s->fiq_select;
  101. case 2: /* RAWINTR */
  102. return s->level | s->soft_level;
  103. case 3: /* INTSELECT */
  104. return s->fiq_select;
  105. case 4: /* INTENABLE */
  106. return s->irq_enable;
  107. case 6: /* SOFTINT */
  108. return s->soft_level;
  109. case 8: /* PROTECTION */
  110. return s->protected;
  111. case 12: /* VECTADDR */
  112. /* Read vector address at the start of an ISR. Increases the
  113. * current priority level to that of the current interrupt.
  114. *
  115. * Since an enabled interrupt X at priority P causes prio_mask[Y]
  116. * to have bit X set for all Y > P, this loop will stop with
  117. * i == the priority of the highest priority set interrupt.
  118. */
  119. for (i = 0; i < s->priority; i++) {
  120. if ((s->level | s->soft_level) & s->prio_mask[i + 1]) {
  121. break;
  122. }
  123. }
  124. /* Reading this value with no pending interrupts is undefined.
  125. We return the default address. */
  126. if (i == PL190_NUM_PRIO)
  127. return s->vect_addr[16];
  128. if (i < s->priority)
  129. {
  130. s->prev_prio[i] = s->priority;
  131. s->priority = i;
  132. pl190_update(s);
  133. }
  134. return s->vect_addr[s->priority];
  135. case 13: /* DEFVECTADDR */
  136. return s->vect_addr[16];
  137. default:
  138. qemu_log_mask(LOG_GUEST_ERROR,
  139. "pl190_read: Bad offset %x\n", (int)offset);
  140. return 0;
  141. }
  142. }
  143. static void pl190_write(void *opaque, hwaddr offset,
  144. uint64_t val, unsigned size)
  145. {
  146. PL190State *s = (PL190State *)opaque;
  147. if (offset >= 0x100 && offset < 0x140) {
  148. s->vect_addr[(offset - 0x100) >> 2] = val;
  149. pl190_update_vectors(s);
  150. return;
  151. }
  152. if (offset >= 0x200 && offset < 0x240) {
  153. s->vect_control[(offset - 0x200) >> 2] = val;
  154. pl190_update_vectors(s);
  155. return;
  156. }
  157. switch (offset >> 2) {
  158. case 0: /* SELECT */
  159. /* This is a readonly register, but linux tries to write to it
  160. anyway. Ignore the write. */
  161. break;
  162. case 3: /* INTSELECT */
  163. s->fiq_select = val;
  164. break;
  165. case 4: /* INTENABLE */
  166. s->irq_enable |= val;
  167. break;
  168. case 5: /* INTENCLEAR */
  169. s->irq_enable &= ~val;
  170. break;
  171. case 6: /* SOFTINT */
  172. s->soft_level |= val;
  173. break;
  174. case 7: /* SOFTINTCLEAR */
  175. s->soft_level &= ~val;
  176. break;
  177. case 8: /* PROTECTION */
  178. /* TODO: Protection (supervisor only access) is not implemented. */
  179. s->protected = val & 1;
  180. break;
  181. case 12: /* VECTADDR */
  182. /* Restore the previous priority level. The value written is
  183. ignored. */
  184. if (s->priority < PL190_NUM_PRIO)
  185. s->priority = s->prev_prio[s->priority];
  186. break;
  187. case 13: /* DEFVECTADDR */
  188. s->vect_addr[16] = val;
  189. break;
  190. case 0xc0: /* ITCR */
  191. if (val) {
  192. qemu_log_mask(LOG_UNIMP, "pl190: Test mode not implemented\n");
  193. }
  194. break;
  195. default:
  196. qemu_log_mask(LOG_GUEST_ERROR,
  197. "pl190_write: Bad offset %x\n", (int)offset);
  198. return;
  199. }
  200. pl190_update(s);
  201. }
  202. static const MemoryRegionOps pl190_ops = {
  203. .read = pl190_read,
  204. .write = pl190_write,
  205. .endianness = DEVICE_NATIVE_ENDIAN,
  206. };
  207. static void pl190_reset(DeviceState *d)
  208. {
  209. PL190State *s = PL190(d);
  210. int i;
  211. for (i = 0; i < 16; i++) {
  212. s->vect_addr[i] = 0;
  213. s->vect_control[i] = 0;
  214. }
  215. s->vect_addr[16] = 0;
  216. s->prio_mask[17] = 0xffffffff;
  217. s->priority = PL190_NUM_PRIO;
  218. pl190_update_vectors(s);
  219. }
  220. static void pl190_init(Object *obj)
  221. {
  222. DeviceState *dev = DEVICE(obj);
  223. PL190State *s = PL190(obj);
  224. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  225. memory_region_init_io(&s->iomem, obj, &pl190_ops, s, "pl190", 0x1000);
  226. sysbus_init_mmio(sbd, &s->iomem);
  227. qdev_init_gpio_in(dev, pl190_set_irq, 32);
  228. sysbus_init_irq(sbd, &s->irq);
  229. sysbus_init_irq(sbd, &s->fiq);
  230. }
  231. static const VMStateDescription vmstate_pl190 = {
  232. .name = "pl190",
  233. .version_id = 1,
  234. .minimum_version_id = 1,
  235. .fields = (VMStateField[]) {
  236. VMSTATE_UINT32(level, PL190State),
  237. VMSTATE_UINT32(soft_level, PL190State),
  238. VMSTATE_UINT32(irq_enable, PL190State),
  239. VMSTATE_UINT32(fiq_select, PL190State),
  240. VMSTATE_UINT8_ARRAY(vect_control, PL190State, 16),
  241. VMSTATE_UINT32_ARRAY(vect_addr, PL190State, PL190_NUM_PRIO),
  242. VMSTATE_UINT32_ARRAY(prio_mask, PL190State, PL190_NUM_PRIO+1),
  243. VMSTATE_INT32(protected, PL190State),
  244. VMSTATE_INT32(priority, PL190State),
  245. VMSTATE_INT32_ARRAY(prev_prio, PL190State, PL190_NUM_PRIO),
  246. VMSTATE_END_OF_LIST()
  247. }
  248. };
  249. static void pl190_class_init(ObjectClass *klass, void *data)
  250. {
  251. DeviceClass *dc = DEVICE_CLASS(klass);
  252. dc->reset = pl190_reset;
  253. dc->vmsd = &vmstate_pl190;
  254. }
  255. static const TypeInfo pl190_info = {
  256. .name = TYPE_PL190,
  257. .parent = TYPE_SYS_BUS_DEVICE,
  258. .instance_size = sizeof(PL190State),
  259. .instance_init = pl190_init,
  260. .class_init = pl190_class_init,
  261. };
  262. static void pl190_register_types(void)
  263. {
  264. type_register_static(&pl190_info);
  265. }
  266. type_init(pl190_register_types)