heathrow_pic.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "hw.h"
  26. #include "ppc_mac.h"
  27. /* debug PIC */
  28. //#define DEBUG_PIC
  29. #ifdef DEBUG_PIC
  30. #define PIC_DPRINTF(fmt, args...) \
  31. do { printf("PIC: " fmt , ##args); } while (0)
  32. #else
  33. #define PIC_DPRINTF(fmt, args...)
  34. #endif
  35. typedef struct HeathrowPIC {
  36. uint32_t events;
  37. uint32_t mask;
  38. uint32_t levels;
  39. uint32_t level_triggered;
  40. } HeathrowPIC;
  41. typedef struct HeathrowPICS {
  42. HeathrowPIC pics[2];
  43. qemu_irq *irqs;
  44. } HeathrowPICS;
  45. static inline int check_irq(HeathrowPIC *pic)
  46. {
  47. return (pic->events | (pic->levels & pic->level_triggered)) & pic->mask;
  48. }
  49. /* update the CPU irq state */
  50. static void heathrow_pic_update(HeathrowPICS *s)
  51. {
  52. if (check_irq(&s->pics[0]) || check_irq(&s->pics[1])) {
  53. qemu_irq_raise(s->irqs[0]);
  54. } else {
  55. qemu_irq_lower(s->irqs[0]);
  56. }
  57. }
  58. static void pic_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
  59. {
  60. HeathrowPICS *s = opaque;
  61. HeathrowPIC *pic;
  62. unsigned int n;
  63. #ifdef TARGET_WORDS_BIGENDIAN
  64. value = bswap32(value);
  65. #endif
  66. n = ((addr & 0xfff) - 0x10) >> 4;
  67. PIC_DPRINTF("writel: " TARGET_FMT_plx " %u: %08x\n", addr, n, value);
  68. if (n >= 2)
  69. return;
  70. pic = &s->pics[n];
  71. switch(addr & 0xf) {
  72. case 0x04:
  73. pic->mask = value;
  74. heathrow_pic_update(s);
  75. break;
  76. case 0x08:
  77. /* do not reset level triggered IRQs */
  78. value &= ~pic->level_triggered;
  79. pic->events &= ~value;
  80. heathrow_pic_update(s);
  81. break;
  82. default:
  83. break;
  84. }
  85. }
  86. static uint32_t pic_readl (void *opaque, target_phys_addr_t addr)
  87. {
  88. HeathrowPICS *s = opaque;
  89. HeathrowPIC *pic;
  90. unsigned int n;
  91. uint32_t value;
  92. n = ((addr & 0xfff) - 0x10) >> 4;
  93. if (n >= 2) {
  94. value = 0;
  95. } else {
  96. pic = &s->pics[n];
  97. switch(addr & 0xf) {
  98. case 0x0:
  99. value = pic->events;
  100. break;
  101. case 0x4:
  102. value = pic->mask;
  103. break;
  104. case 0xc:
  105. value = pic->levels;
  106. break;
  107. default:
  108. value = 0;
  109. break;
  110. }
  111. }
  112. PIC_DPRINTF("readl: " TARGET_FMT_plx " %u: %08x\n", addr, n, value);
  113. #ifdef TARGET_WORDS_BIGENDIAN
  114. value = bswap32(value);
  115. #endif
  116. return value;
  117. }
  118. static CPUWriteMemoryFunc *pic_write[] = {
  119. &pic_writel,
  120. &pic_writel,
  121. &pic_writel,
  122. };
  123. static CPUReadMemoryFunc *pic_read[] = {
  124. &pic_readl,
  125. &pic_readl,
  126. &pic_readl,
  127. };
  128. static void heathrow_pic_set_irq(void *opaque, int num, int level)
  129. {
  130. HeathrowPICS *s = opaque;
  131. HeathrowPIC *pic;
  132. unsigned int irq_bit;
  133. #if defined(DEBUG)
  134. {
  135. static int last_level[64];
  136. if (last_level[num] != level) {
  137. PIC_DPRINTF("set_irq: num=0x%02x level=%d\n", num, level);
  138. last_level[num] = level;
  139. }
  140. }
  141. #endif
  142. pic = &s->pics[1 - (num >> 5)];
  143. irq_bit = 1 << (num & 0x1f);
  144. if (level) {
  145. pic->events |= irq_bit & ~pic->level_triggered;
  146. pic->levels |= irq_bit;
  147. } else {
  148. pic->levels &= ~irq_bit;
  149. }
  150. heathrow_pic_update(s);
  151. }
  152. static void heathrow_pic_save_one(QEMUFile *f, HeathrowPIC *s)
  153. {
  154. qemu_put_be32s(f, &s->events);
  155. qemu_put_be32s(f, &s->mask);
  156. qemu_put_be32s(f, &s->levels);
  157. qemu_put_be32s(f, &s->level_triggered);
  158. }
  159. static void heathrow_pic_save(QEMUFile *f, void *opaque)
  160. {
  161. HeathrowPICS *s = (HeathrowPICS *)opaque;
  162. heathrow_pic_save_one(f, &s->pics[0]);
  163. heathrow_pic_save_one(f, &s->pics[1]);
  164. }
  165. static void heathrow_pic_load_one(QEMUFile *f, HeathrowPIC *s)
  166. {
  167. qemu_get_be32s(f, &s->events);
  168. qemu_get_be32s(f, &s->mask);
  169. qemu_get_be32s(f, &s->levels);
  170. qemu_get_be32s(f, &s->level_triggered);
  171. }
  172. static int heathrow_pic_load(QEMUFile *f, void *opaque, int version_id)
  173. {
  174. HeathrowPICS *s = (HeathrowPICS *)opaque;
  175. if (version_id != 1)
  176. return -EINVAL;
  177. heathrow_pic_load_one(f, &s->pics[0]);
  178. heathrow_pic_load_one(f, &s->pics[1]);
  179. return 0;
  180. }
  181. static void heathrow_pic_reset_one(HeathrowPIC *s)
  182. {
  183. memset(s, '\0', sizeof(HeathrowPIC));
  184. }
  185. static void heathrow_pic_reset(void *opaque)
  186. {
  187. HeathrowPICS *s = opaque;
  188. heathrow_pic_reset_one(&s->pics[0]);
  189. heathrow_pic_reset_one(&s->pics[1]);
  190. s->pics[0].level_triggered = 0;
  191. s->pics[1].level_triggered = 0x1ff00000;
  192. }
  193. qemu_irq *heathrow_pic_init(int *pmem_index,
  194. int nb_cpus, qemu_irq **irqs)
  195. {
  196. HeathrowPICS *s;
  197. s = qemu_mallocz(sizeof(HeathrowPICS));
  198. /* only 1 CPU */
  199. s->irqs = irqs[0];
  200. *pmem_index = cpu_register_io_memory(0, pic_read, pic_write, s);
  201. register_savevm("heathrow_pic", -1, 1, heathrow_pic_save,
  202. heathrow_pic_load, s);
  203. qemu_register_reset(heathrow_pic_reset, s);
  204. heathrow_pic_reset(s);
  205. return qemu_allocate_irqs(heathrow_pic_set_irq, s, 64);
  206. }