xref: /openbmc/qemu/hw/intc/loongarch_pch_pic.c (revision a6fdd0032ce5c7cd5c49a1bfbcedf6c08e136682)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * QEMU Loongson 7A1000 I/O interrupt controller.
4  *
5  * Copyright (C) 2021 Loongson Technology Corporation Limited
6  */
7 
8 #include "qemu/osdep.h"
9 #include "qemu/bitops.h"
10 #include "qemu/log.h"
11 #include "hw/irq.h"
12 #include "hw/intc/loongarch_pch_pic.h"
13 #include "trace.h"
14 #include "qapi/error.h"
15 
16 static void pch_pic_update_irq(LoongArchPICCommonState *s, uint64_t mask,
17                                int level)
18 {
19     uint64_t val;
20     int irq;
21 
22     if (level) {
23         val = mask & s->intirr & ~s->int_mask;
24         if (val) {
25             irq = ctz64(val);
26             s->intisr |= MAKE_64BIT_MASK(irq, 1);
27             qemu_set_irq(s->parent_irq[s->htmsi_vector[irq]], 1);
28         }
29     } else {
30         /*
31          * intirr means requested pending irq
32          * do not clear pending irq for edge-triggered on lowering edge
33          */
34         val = mask & s->intisr & ~s->intirr;
35         if (val) {
36             irq = ctz64(val);
37             s->intisr &= ~MAKE_64BIT_MASK(irq, 1);
38             qemu_set_irq(s->parent_irq[s->htmsi_vector[irq]], 0);
39         }
40     }
41 }
42 
43 static void pch_pic_irq_handler(void *opaque, int irq, int level)
44 {
45     LoongArchPICCommonState *s = LOONGARCH_PIC_COMMON(opaque);
46     uint64_t mask = 1ULL << irq;
47 
48     assert(irq < s->irq_num);
49     trace_loongarch_pch_pic_irq_handler(irq, level);
50 
51     if (s->intedge & mask) {
52         /* Edge triggered */
53         if (level) {
54             if ((s->last_intirr & mask) == 0) {
55                 /* marked pending on a rising edge */
56                 s->intirr |= mask;
57             }
58             s->last_intirr |= mask;
59         } else {
60             s->last_intirr &= ~mask;
61         }
62     } else {
63         /* Level triggered */
64         if (level) {
65             s->intirr |= mask;
66             s->last_intirr |= mask;
67         } else {
68             s->intirr &= ~mask;
69             s->last_intirr &= ~mask;
70         }
71     }
72     pch_pic_update_irq(s, mask, level);
73 }
74 
75 static uint64_t pch_pic_read(void *opaque, hwaddr addr, uint64_t field_mask)
76 {
77     LoongArchPICCommonState *s = LOONGARCH_PIC_COMMON(opaque);
78     uint64_t val = 0;
79     uint32_t offset;
80 
81     offset = addr & 7;
82     addr -= offset;
83     switch (addr) {
84     case PCH_PIC_INT_ID:
85         val = s->id.data;
86         break;
87     case PCH_PIC_INT_MASK:
88         val = s->int_mask;
89         break;
90     case PCH_PIC_INT_EDGE:
91         val = s->intedge;
92         break;
93     case PCH_PIC_HTMSI_EN:
94         val = s->htmsi_en;
95         break;
96     case PCH_PIC_AUTO_CTRL0:
97     case PCH_PIC_AUTO_CTRL1:
98         /* PCH PIC connect to EXTIOI always, discard auto_ctrl access */
99         break;
100     case PCH_PIC_INT_STATUS:
101         val = s->intisr & (~s->int_mask);
102         break;
103     case PCH_PIC_INT_POL:
104         val = s->int_polarity;
105         break;
106     case PCH_PIC_HTMSI_VEC ... PCH_PIC_HTMSI_VEC_END:
107         val = *(uint64_t *)(s->htmsi_vector + addr - PCH_PIC_HTMSI_VEC);
108         break;
109     case PCH_PIC_ROUTE_ENTRY ... PCH_PIC_ROUTE_ENTRY_END:
110         val = *(uint64_t *)(s->route_entry + addr - PCH_PIC_ROUTE_ENTRY);
111         break;
112     default:
113         qemu_log_mask(LOG_GUEST_ERROR,
114                       "pch_pic_read: Bad address 0x%"PRIx64"\n", addr);
115         break;
116     }
117 
118     return (val >> (offset * 8)) & field_mask;
119 }
120 
121 static void pch_pic_write(void *opaque, hwaddr addr, uint64_t value,
122                           uint64_t field_mask)
123 {
124     LoongArchPICCommonState *s = LOONGARCH_PIC_COMMON(opaque);
125     uint32_t offset;
126     uint64_t old, mask, data, *ptemp;
127 
128     offset = addr & 7;
129     addr -= offset;
130     mask = field_mask << (offset * 8);
131     data = (value & field_mask) << (offset * 8);
132     switch (addr) {
133     case PCH_PIC_INT_MASK:
134         old = s->int_mask;
135         s->int_mask = (old & ~mask) | data;
136         if (old & ~data) {
137             pch_pic_update_irq(s, old & ~data, 1);
138         }
139 
140         if (~old & data) {
141             pch_pic_update_irq(s, ~old & data, 0);
142         }
143         break;
144     case PCH_PIC_INT_EDGE:
145         s->intedge = (s->intedge & ~mask) | data;
146         break;
147     case PCH_PIC_INT_CLEAR:
148         if (s->intedge & data) {
149             s->intirr &= ~data;
150             pch_pic_update_irq(s, data, 0);
151             s->intisr &= ~data;
152         }
153         break;
154     case PCH_PIC_HTMSI_EN:
155         s->htmsi_en = (s->htmsi_en & ~mask) | data;
156         break;
157     case PCH_PIC_AUTO_CTRL0:
158     case PCH_PIC_AUTO_CTRL1:
159         /* Discard auto_ctrl access */
160         break;
161     case PCH_PIC_INT_POL:
162         s->int_polarity = (s->int_polarity & ~mask) | data;
163         break;
164     case PCH_PIC_HTMSI_VEC ... PCH_PIC_HTMSI_VEC_END:
165         ptemp = (uint64_t *)(s->htmsi_vector + addr - PCH_PIC_HTMSI_VEC);
166         *ptemp = (*ptemp & ~mask) | data;
167         break;
168     case PCH_PIC_ROUTE_ENTRY ... PCH_PIC_ROUTE_ENTRY_END:
169         ptemp = (uint64_t *)(s->route_entry + addr - PCH_PIC_ROUTE_ENTRY);
170         *ptemp = (*ptemp & ~mask) | data;
171         break;
172     default:
173         qemu_log_mask(LOG_GUEST_ERROR,
174                       "pch_pic_write: Bad address 0x%"PRIx64"\n", addr);
175         break;
176     }
177 }
178 
179 static uint64_t loongarch_pch_pic_read(void *opaque, hwaddr addr,
180                                        unsigned size)
181 {
182     uint64_t val = 0;
183 
184     switch (size) {
185     case 1:
186         val = pch_pic_read(opaque, addr, UCHAR_MAX);
187         break;
188     case 2:
189         val = pch_pic_read(opaque, addr, USHRT_MAX);
190         break;
191     case 4:
192         val = pch_pic_read(opaque, addr, UINT_MAX);
193         break;
194     case 8:
195         val = pch_pic_read(opaque, addr, UINT64_MAX);
196         break;
197     default:
198         qemu_log_mask(LOG_GUEST_ERROR,
199                       "loongarch_pch_pic_read: Bad size %d\n", size);
200         break;
201     }
202 
203     return val;
204 }
205 
206 static void loongarch_pch_pic_write(void *opaque, hwaddr addr,
207                                     uint64_t value, unsigned size)
208 {
209     switch (size) {
210     case 1:
211         pch_pic_write(opaque, addr, value, UCHAR_MAX);
212         break;
213     case 2:
214         pch_pic_write(opaque, addr, value, USHRT_MAX);
215         break;
216         break;
217     case 4:
218         pch_pic_write(opaque, addr, value, UINT_MAX);
219         break;
220     case 8:
221         pch_pic_write(opaque, addr, value, UINT64_MAX);
222         break;
223     default:
224         qemu_log_mask(LOG_GUEST_ERROR,
225                       "loongarch_pch_pic_write: Bad size %d\n", size);
226         break;
227     }
228 }
229 
230 static uint64_t loongarch_pch_pic_low_readw(void *opaque, hwaddr addr,
231                                             unsigned size)
232 {
233     uint64_t val;
234 
235     val = loongarch_pch_pic_read(opaque, addr, size);
236     trace_loongarch_pch_pic_low_readw(size, addr, val);
237     return val;
238 }
239 
240 static void loongarch_pch_pic_low_writew(void *opaque, hwaddr addr,
241                                          uint64_t value, unsigned size)
242 {
243     trace_loongarch_pch_pic_low_writew(size, addr, value);
244     loongarch_pch_pic_write(opaque, addr, value, size);
245 }
246 
247 static uint64_t loongarch_pch_pic_high_readw(void *opaque, hwaddr addr,
248                                         unsigned size)
249 {
250     uint64_t val;
251 
252     addr += PCH_PIC_INT_STATUS;
253     val = loongarch_pch_pic_read(opaque, addr, size);
254     trace_loongarch_pch_pic_high_readw(size, addr, val);
255     return val;
256 }
257 
258 static void loongarch_pch_pic_high_writew(void *opaque, hwaddr addr,
259                                      uint64_t value, unsigned size)
260 {
261     addr += PCH_PIC_INT_STATUS;
262     trace_loongarch_pch_pic_high_writew(size, addr, value);
263     loongarch_pch_pic_write(opaque, addr, value, size);
264 }
265 
266 static uint64_t loongarch_pch_pic_readb(void *opaque, hwaddr addr,
267                                         unsigned size)
268 {
269     uint64_t val;
270 
271     addr += PCH_PIC_ROUTE_ENTRY;
272     val = loongarch_pch_pic_read(opaque, addr, size);
273     trace_loongarch_pch_pic_readb(size, addr, val);
274     return val;
275 }
276 
277 static void loongarch_pch_pic_writeb(void *opaque, hwaddr addr,
278                                      uint64_t data, unsigned size)
279 {
280     addr += PCH_PIC_ROUTE_ENTRY;
281     trace_loongarch_pch_pic_writeb(size, addr, data);
282     loongarch_pch_pic_write(opaque, addr, data, size);
283 }
284 
285 static const MemoryRegionOps loongarch_pch_pic_reg32_low_ops = {
286     .read = loongarch_pch_pic_low_readw,
287     .write = loongarch_pch_pic_low_writew,
288     .valid = {
289         .min_access_size = 4,
290         .max_access_size = 8,
291     },
292     .impl = {
293         .min_access_size = 4,
294         .max_access_size = 4,
295     },
296     .endianness = DEVICE_LITTLE_ENDIAN,
297 };
298 
299 static const MemoryRegionOps loongarch_pch_pic_reg32_high_ops = {
300     .read = loongarch_pch_pic_high_readw,
301     .write = loongarch_pch_pic_high_writew,
302     .valid = {
303         .min_access_size = 4,
304         .max_access_size = 8,
305     },
306     .impl = {
307         .min_access_size = 4,
308         .max_access_size = 4,
309     },
310     .endianness = DEVICE_LITTLE_ENDIAN,
311 };
312 
313 static const MemoryRegionOps loongarch_pch_pic_reg8_ops = {
314     .read = loongarch_pch_pic_readb,
315     .write = loongarch_pch_pic_writeb,
316     .valid = {
317         .min_access_size = 1,
318         .max_access_size = 1,
319     },
320     .impl = {
321         .min_access_size = 1,
322         .max_access_size = 1,
323     },
324     .endianness = DEVICE_LITTLE_ENDIAN,
325 };
326 
327 static void loongarch_pic_reset_hold(Object *obj, ResetType type)
328 {
329     LoongarchPICClass *lpc = LOONGARCH_PIC_GET_CLASS(obj);
330 
331     if (lpc->parent_phases.hold) {
332         lpc->parent_phases.hold(obj, type);
333     }
334 }
335 
336 static void loongarch_pic_realize(DeviceState *dev, Error **errp)
337 {
338     LoongArchPICCommonState *s = LOONGARCH_PIC_COMMON(dev);
339     LoongarchPICClass *lpc = LOONGARCH_PIC_GET_CLASS(dev);
340     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
341     Error *local_err = NULL;
342 
343     lpc->parent_realize(dev, &local_err);
344     if (local_err) {
345         error_propagate(errp, local_err);
346         return;
347     }
348 
349     qdev_init_gpio_out(dev, s->parent_irq, s->irq_num);
350     qdev_init_gpio_in(dev, pch_pic_irq_handler, s->irq_num);
351     memory_region_init_io(&s->iomem32_low, OBJECT(dev),
352                           &loongarch_pch_pic_reg32_low_ops,
353                           s, PCH_PIC_NAME(.reg32_part1), 0x100);
354     memory_region_init_io(&s->iomem8, OBJECT(dev), &loongarch_pch_pic_reg8_ops,
355                           s, PCH_PIC_NAME(.reg8), 0x2a0);
356     memory_region_init_io(&s->iomem32_high, OBJECT(dev),
357                           &loongarch_pch_pic_reg32_high_ops,
358                           s, PCH_PIC_NAME(.reg32_part2), 0xc60);
359     sysbus_init_mmio(sbd, &s->iomem32_low);
360     sysbus_init_mmio(sbd, &s->iomem8);
361     sysbus_init_mmio(sbd, &s->iomem32_high);
362 
363 }
364 
365 static void loongarch_pic_class_init(ObjectClass *klass, const void *data)
366 {
367     DeviceClass *dc = DEVICE_CLASS(klass);
368     LoongarchPICClass *lpc = LOONGARCH_PIC_CLASS(klass);
369     ResettableClass *rc = RESETTABLE_CLASS(klass);
370 
371     resettable_class_set_parent_phases(rc, NULL, loongarch_pic_reset_hold,
372                                        NULL, &lpc->parent_phases);
373     device_class_set_parent_realize(dc, loongarch_pic_realize,
374                                     &lpc->parent_realize);
375 }
376 
377 static const TypeInfo loongarch_pic_types[] = {
378    {
379         .name               = TYPE_LOONGARCH_PIC,
380         .parent             = TYPE_LOONGARCH_PIC_COMMON,
381         .instance_size      = sizeof(LoongarchPICState),
382         .class_size         = sizeof(LoongarchPICClass),
383         .class_init         = loongarch_pic_class_init,
384     }
385 };
386 
387 DEFINE_TYPES(loongarch_pic_types)
388