pl190.c 8.0 KB

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