heathrow_pic.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Heathrow PIC support (OldWorld PowerMac)
  3. *
  4. * Copyright (c) 2005-2007 Fabrice Bellard
  5. * Copyright (c) 2007 Jocelyn Mayer
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "hw/ppc/mac.h"
  27. #include "migration/vmstate.h"
  28. #include "qemu/module.h"
  29. #include "hw/intc/heathrow_pic.h"
  30. #include "hw/irq.h"
  31. #include "trace.h"
  32. static inline int heathrow_check_irq(HeathrowPICState *pic)
  33. {
  34. return (pic->events | (pic->levels & pic->level_triggered)) & pic->mask;
  35. }
  36. /* update the CPU irq state */
  37. static void heathrow_update_irq(HeathrowState *s)
  38. {
  39. if (heathrow_check_irq(&s->pics[0]) ||
  40. heathrow_check_irq(&s->pics[1])) {
  41. qemu_irq_raise(s->irqs[0]);
  42. } else {
  43. qemu_irq_lower(s->irqs[0]);
  44. }
  45. }
  46. static void heathrow_write(void *opaque, hwaddr addr,
  47. uint64_t value, unsigned size)
  48. {
  49. HeathrowState *s = opaque;
  50. HeathrowPICState *pic;
  51. unsigned int n;
  52. n = ((addr & 0xfff) - 0x10) >> 4;
  53. trace_heathrow_write(addr, n, value);
  54. if (n >= 2)
  55. return;
  56. pic = &s->pics[n];
  57. switch(addr & 0xf) {
  58. case 0x04:
  59. pic->mask = value;
  60. heathrow_update_irq(s);
  61. break;
  62. case 0x08:
  63. /* do not reset level triggered IRQs */
  64. value &= ~pic->level_triggered;
  65. pic->events &= ~value;
  66. heathrow_update_irq(s);
  67. break;
  68. default:
  69. break;
  70. }
  71. }
  72. static uint64_t heathrow_read(void *opaque, hwaddr addr,
  73. unsigned size)
  74. {
  75. HeathrowState *s = opaque;
  76. HeathrowPICState *pic;
  77. unsigned int n;
  78. uint32_t value;
  79. n = ((addr & 0xfff) - 0x10) >> 4;
  80. if (n >= 2) {
  81. value = 0;
  82. } else {
  83. pic = &s->pics[n];
  84. switch(addr & 0xf) {
  85. case 0x0:
  86. value = pic->events;
  87. break;
  88. case 0x4:
  89. value = pic->mask;
  90. break;
  91. case 0xc:
  92. value = pic->levels;
  93. break;
  94. default:
  95. value = 0;
  96. break;
  97. }
  98. }
  99. trace_heathrow_read(addr, n, value);
  100. return value;
  101. }
  102. static const MemoryRegionOps heathrow_ops = {
  103. .read = heathrow_read,
  104. .write = heathrow_write,
  105. .endianness = DEVICE_LITTLE_ENDIAN,
  106. };
  107. static void heathrow_set_irq(void *opaque, int num, int level)
  108. {
  109. HeathrowState *s = opaque;
  110. HeathrowPICState *pic;
  111. unsigned int irq_bit;
  112. int last_level;
  113. pic = &s->pics[1 - (num >> 5)];
  114. irq_bit = 1 << (num & 0x1f);
  115. last_level = (pic->levels & irq_bit) ? 1 : 0;
  116. if (level) {
  117. pic->events |= irq_bit & ~pic->level_triggered;
  118. pic->levels |= irq_bit;
  119. } else {
  120. pic->levels &= ~irq_bit;
  121. }
  122. if (last_level != level) {
  123. trace_heathrow_set_irq(num, level);
  124. }
  125. heathrow_update_irq(s);
  126. }
  127. static const VMStateDescription vmstate_heathrow_pic_one = {
  128. .name = "heathrow_pic_one",
  129. .version_id = 0,
  130. .minimum_version_id = 0,
  131. .fields = (VMStateField[]) {
  132. VMSTATE_UINT32(events, HeathrowPICState),
  133. VMSTATE_UINT32(mask, HeathrowPICState),
  134. VMSTATE_UINT32(levels, HeathrowPICState),
  135. VMSTATE_UINT32(level_triggered, HeathrowPICState),
  136. VMSTATE_END_OF_LIST()
  137. }
  138. };
  139. static const VMStateDescription vmstate_heathrow = {
  140. .name = "heathrow_pic",
  141. .version_id = 1,
  142. .minimum_version_id = 1,
  143. .fields = (VMStateField[]) {
  144. VMSTATE_STRUCT_ARRAY(pics, HeathrowState, 2, 1,
  145. vmstate_heathrow_pic_one, HeathrowPICState),
  146. VMSTATE_END_OF_LIST()
  147. }
  148. };
  149. static void heathrow_reset(DeviceState *d)
  150. {
  151. HeathrowState *s = HEATHROW(d);
  152. s->pics[0].level_triggered = 0;
  153. s->pics[1].level_triggered = 0x1ff00000;
  154. }
  155. static void heathrow_init(Object *obj)
  156. {
  157. HeathrowState *s = HEATHROW(obj);
  158. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  159. /* only 1 CPU */
  160. qdev_init_gpio_out(DEVICE(obj), s->irqs, 1);
  161. qdev_init_gpio_in(DEVICE(obj), heathrow_set_irq, HEATHROW_NUM_IRQS);
  162. memory_region_init_io(&s->mem, OBJECT(s), &heathrow_ops, s,
  163. "heathrow-pic", 0x1000);
  164. sysbus_init_mmio(sbd, &s->mem);
  165. }
  166. static void heathrow_class_init(ObjectClass *oc, void *data)
  167. {
  168. DeviceClass *dc = DEVICE_CLASS(oc);
  169. dc->reset = heathrow_reset;
  170. dc->vmsd = &vmstate_heathrow;
  171. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  172. }
  173. static const TypeInfo heathrow_type_info = {
  174. .name = TYPE_HEATHROW,
  175. .parent = TYPE_SYS_BUS_DEVICE,
  176. .instance_size = sizeof(HeathrowState),
  177. .instance_init = heathrow_init,
  178. .class_init = heathrow_class_init,
  179. };
  180. static void heathrow_register_types(void)
  181. {
  182. type_register_static(&heathrow_type_info);
  183. }
  184. type_init(heathrow_register_types)