2
0

heathrow_pic.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "migration/vmstate.h"
  27. #include "qemu/module.h"
  28. #include "hw/intc/heathrow_pic.h"
  29. #include "hw/irq.h"
  30. #include "trace.h"
  31. static inline int heathrow_check_irq(HeathrowPICState *pic)
  32. {
  33. return (pic->events | (pic->levels & pic->level_triggered)) & pic->mask;
  34. }
  35. /* update the CPU irq state */
  36. static void heathrow_update_irq(HeathrowState *s)
  37. {
  38. if (heathrow_check_irq(&s->pics[0]) ||
  39. heathrow_check_irq(&s->pics[1])) {
  40. qemu_irq_raise(s->irqs[0]);
  41. } else {
  42. qemu_irq_lower(s->irqs[0]);
  43. }
  44. }
  45. static void heathrow_write(void *opaque, hwaddr addr,
  46. uint64_t value, unsigned size)
  47. {
  48. HeathrowState *s = opaque;
  49. HeathrowPICState *pic;
  50. unsigned int n;
  51. n = ((addr & 0xfff) - 0x10) >> 4;
  52. trace_heathrow_write(addr, n, value);
  53. if (n >= 2)
  54. return;
  55. pic = &s->pics[n];
  56. switch(addr & 0xf) {
  57. case 0x04:
  58. pic->mask = value;
  59. heathrow_update_irq(s);
  60. break;
  61. case 0x08:
  62. /* do not reset level triggered IRQs */
  63. value &= ~pic->level_triggered;
  64. pic->events &= ~value;
  65. heathrow_update_irq(s);
  66. break;
  67. default:
  68. break;
  69. }
  70. }
  71. static uint64_t heathrow_read(void *opaque, hwaddr addr,
  72. unsigned size)
  73. {
  74. HeathrowState *s = opaque;
  75. HeathrowPICState *pic;
  76. unsigned int n;
  77. uint32_t value;
  78. n = ((addr & 0xfff) - 0x10) >> 4;
  79. if (n >= 2) {
  80. value = 0;
  81. } else {
  82. pic = &s->pics[n];
  83. switch(addr & 0xf) {
  84. case 0x0:
  85. value = pic->events;
  86. break;
  87. case 0x4:
  88. value = pic->mask;
  89. break;
  90. case 0xc:
  91. value = pic->levels;
  92. break;
  93. default:
  94. value = 0;
  95. break;
  96. }
  97. }
  98. trace_heathrow_read(addr, n, value);
  99. return value;
  100. }
  101. static const MemoryRegionOps heathrow_ops = {
  102. .read = heathrow_read,
  103. .write = heathrow_write,
  104. .endianness = DEVICE_LITTLE_ENDIAN,
  105. };
  106. static void heathrow_set_irq(void *opaque, int num, int level)
  107. {
  108. HeathrowState *s = opaque;
  109. HeathrowPICState *pic;
  110. unsigned int irq_bit;
  111. int last_level;
  112. pic = &s->pics[1 - (num >> 5)];
  113. irq_bit = 1 << (num & 0x1f);
  114. last_level = (pic->levels & irq_bit) ? 1 : 0;
  115. if (level) {
  116. pic->events |= irq_bit & ~pic->level_triggered;
  117. pic->levels |= irq_bit;
  118. } else {
  119. pic->levels &= ~irq_bit;
  120. }
  121. if (last_level != level) {
  122. trace_heathrow_set_irq(num, level);
  123. }
  124. heathrow_update_irq(s);
  125. }
  126. static const VMStateDescription vmstate_heathrow_pic_one = {
  127. .name = "heathrow_pic_one",
  128. .version_id = 0,
  129. .minimum_version_id = 0,
  130. .fields = (const VMStateField[]) {
  131. VMSTATE_UINT32(events, HeathrowPICState),
  132. VMSTATE_UINT32(mask, HeathrowPICState),
  133. VMSTATE_UINT32(levels, HeathrowPICState),
  134. VMSTATE_UINT32(level_triggered, HeathrowPICState),
  135. VMSTATE_END_OF_LIST()
  136. }
  137. };
  138. static const VMStateDescription vmstate_heathrow = {
  139. .name = "heathrow_pic",
  140. .version_id = 1,
  141. .minimum_version_id = 1,
  142. .fields = (const VMStateField[]) {
  143. VMSTATE_STRUCT_ARRAY(pics, HeathrowState, 2, 1,
  144. vmstate_heathrow_pic_one, HeathrowPICState),
  145. VMSTATE_END_OF_LIST()
  146. }
  147. };
  148. static void heathrow_reset(DeviceState *d)
  149. {
  150. HeathrowState *s = HEATHROW(d);
  151. s->pics[0].level_triggered = 0;
  152. s->pics[1].level_triggered = 0x1ff00000;
  153. }
  154. static void heathrow_init(Object *obj)
  155. {
  156. HeathrowState *s = HEATHROW(obj);
  157. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  158. /* only 1 CPU */
  159. qdev_init_gpio_out(DEVICE(obj), s->irqs, 1);
  160. qdev_init_gpio_in(DEVICE(obj), heathrow_set_irq, HEATHROW_NUM_IRQS);
  161. memory_region_init_io(&s->mem, OBJECT(s), &heathrow_ops, s,
  162. "heathrow-pic", 0x1000);
  163. sysbus_init_mmio(sbd, &s->mem);
  164. }
  165. static void heathrow_class_init(ObjectClass *oc, void *data)
  166. {
  167. DeviceClass *dc = DEVICE_CLASS(oc);
  168. device_class_set_legacy_reset(dc, heathrow_reset);
  169. dc->vmsd = &vmstate_heathrow;
  170. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  171. }
  172. static const TypeInfo heathrow_type_info = {
  173. .name = TYPE_HEATHROW,
  174. .parent = TYPE_SYS_BUS_DEVICE,
  175. .instance_size = sizeof(HeathrowState),
  176. .instance_init = heathrow_init,
  177. .class_init = heathrow_class_init,
  178. };
  179. static void heathrow_register_types(void)
  180. {
  181. type_register_static(&heathrow_type_info);
  182. }
  183. type_init(heathrow_register_types)