xref: /openbmc/qemu/hw/intc/loongson_liointc.c (revision 9b4b4e51)
1c012e0b1SHuacai Chen /*
2*9b4b4e51SMichael Tokarev  * QEMU Loongson Local I/O interrupt controller.
3c012e0b1SHuacai Chen  *
4dea96332SHuacai Chen  * Copyright (c) 2020 Huacai Chen <chenhc@lemote.com>
5c012e0b1SHuacai Chen  * Copyright (c) 2020 Jiaxun Yang <jiaxun.yang@flygoat.com>
6c012e0b1SHuacai Chen  *
7c012e0b1SHuacai Chen  * This program is free software: you can redistribute it and/or modify
8c012e0b1SHuacai Chen  * it under the terms of the GNU General Public License as published by
9c012e0b1SHuacai Chen  * the Free Software Foundation, either version 2 of the License, or
10c012e0b1SHuacai Chen  * (at your option) any later version.
11c012e0b1SHuacai Chen  *
12c012e0b1SHuacai Chen  * This program is distributed in the hope that it will be useful,
13c012e0b1SHuacai Chen  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14c012e0b1SHuacai Chen  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15c012e0b1SHuacai Chen  * GNU General Public License for more details.
16c012e0b1SHuacai Chen  *
17c012e0b1SHuacai Chen  * You should have received a copy of the GNU General Public License
18c012e0b1SHuacai Chen  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19c012e0b1SHuacai Chen  *
20c012e0b1SHuacai Chen  */
21c012e0b1SHuacai Chen 
22c012e0b1SHuacai Chen #include "qemu/osdep.h"
23c012e0b1SHuacai Chen #include "qemu/module.h"
24dea96332SHuacai Chen #include "qemu/log.h"
25c012e0b1SHuacai Chen #include "hw/irq.h"
26c012e0b1SHuacai Chen #include "hw/qdev-properties.h"
27dea96332SHuacai Chen #include "hw/intc/loongson_liointc.h"
28c012e0b1SHuacai Chen 
29c012e0b1SHuacai Chen #define NUM_IRQS                32
30c012e0b1SHuacai Chen 
31c012e0b1SHuacai Chen #define NUM_CORES               4
32c012e0b1SHuacai Chen #define NUM_IPS                 4
33c012e0b1SHuacai Chen #define NUM_PARENTS             (NUM_CORES * NUM_IPS)
34c012e0b1SHuacai Chen #define PARENT_COREx_IPy(x, y)  (NUM_IPS * x + y)
35c012e0b1SHuacai Chen 
36c012e0b1SHuacai Chen #define R_MAPPER_START          0x0
37c012e0b1SHuacai Chen #define R_MAPPER_END            0x20
38c012e0b1SHuacai Chen #define R_ISR                   R_MAPPER_END
39c012e0b1SHuacai Chen #define R_IEN                   0x24
40c012e0b1SHuacai Chen #define R_IEN_SET               0x28
41c012e0b1SHuacai Chen #define R_IEN_CLR               0x2c
42dea96332SHuacai Chen #define R_ISR_SIZE              0x8
43dea96332SHuacai Chen #define R_START                 0x40
4469027599SJiaxun Yang #define R_END                   (R_START + R_ISR_SIZE * NUM_CORES)
45c012e0b1SHuacai Chen 
46c012e0b1SHuacai Chen struct loongson_liointc {
47c012e0b1SHuacai Chen     SysBusDevice parent_obj;
48c012e0b1SHuacai Chen 
49c012e0b1SHuacai Chen     MemoryRegion mmio;
50c012e0b1SHuacai Chen     qemu_irq parent_irq[NUM_PARENTS];
51c012e0b1SHuacai Chen 
52c012e0b1SHuacai Chen     uint8_t mapper[NUM_IRQS]; /* 0:3 for core, 4:7 for IP */
53c012e0b1SHuacai Chen     uint32_t isr;
54c012e0b1SHuacai Chen     uint32_t ien;
55c012e0b1SHuacai Chen     uint32_t per_core_isr[NUM_CORES];
56c012e0b1SHuacai Chen 
57c012e0b1SHuacai Chen     /* state of the interrupt input pins */
58c012e0b1SHuacai Chen     uint32_t pin_state;
59c012e0b1SHuacai Chen     bool parent_state[NUM_PARENTS];
60c012e0b1SHuacai Chen };
61c012e0b1SHuacai Chen 
update_irq(struct loongson_liointc * p)62c012e0b1SHuacai Chen static void update_irq(struct loongson_liointc *p)
63c012e0b1SHuacai Chen {
64c012e0b1SHuacai Chen     uint32_t irq, core, ip;
65c012e0b1SHuacai Chen     uint32_t per_ip_isr[NUM_IPS] = {0};
66c012e0b1SHuacai Chen 
67c012e0b1SHuacai Chen     /* level triggered interrupt */
68c012e0b1SHuacai Chen     p->isr = p->pin_state;
69c012e0b1SHuacai Chen 
70c012e0b1SHuacai Chen     /* Clear disabled IRQs */
71c012e0b1SHuacai Chen     p->isr &= p->ien;
72c012e0b1SHuacai Chen 
73c012e0b1SHuacai Chen     /* Clear per_core_isr */
74c012e0b1SHuacai Chen     for (core = 0; core < NUM_CORES; core++) {
75c012e0b1SHuacai Chen         p->per_core_isr[core] = 0;
76c012e0b1SHuacai Chen     }
77c012e0b1SHuacai Chen 
78c012e0b1SHuacai Chen     /* Update per_core_isr and per_ip_isr */
79c012e0b1SHuacai Chen     for (irq = 0; irq < NUM_IRQS; irq++) {
80c012e0b1SHuacai Chen         if (!(p->isr & (1 << irq))) {
81c012e0b1SHuacai Chen             continue;
82c012e0b1SHuacai Chen         }
83c012e0b1SHuacai Chen 
84c012e0b1SHuacai Chen         for (core = 0; core < NUM_CORES; core++) {
85c012e0b1SHuacai Chen             if ((p->mapper[irq] & (1 << core))) {
86c012e0b1SHuacai Chen                 p->per_core_isr[core] |= (1 << irq);
87c012e0b1SHuacai Chen             }
88c012e0b1SHuacai Chen         }
89c012e0b1SHuacai Chen 
90c012e0b1SHuacai Chen         for (ip = 0; ip < NUM_IPS; ip++) {
91c012e0b1SHuacai Chen             if ((p->mapper[irq] & (1 << (ip + 4)))) {
92c012e0b1SHuacai Chen                 per_ip_isr[ip] |= (1 << irq);
93c012e0b1SHuacai Chen             }
94c012e0b1SHuacai Chen         }
95c012e0b1SHuacai Chen     }
96c012e0b1SHuacai Chen 
97c012e0b1SHuacai Chen     /* Emit IRQ to parent! */
98c012e0b1SHuacai Chen     for (core = 0; core < NUM_CORES; core++) {
99c012e0b1SHuacai Chen         for (ip = 0; ip < NUM_IPS; ip++) {
100c012e0b1SHuacai Chen             int parent = PARENT_COREx_IPy(core, ip);
101c012e0b1SHuacai Chen             if (p->parent_state[parent] !=
102c012e0b1SHuacai Chen                 (!!p->per_core_isr[core] && !!per_ip_isr[ip])) {
103c012e0b1SHuacai Chen                 p->parent_state[parent] = !p->parent_state[parent];
104c012e0b1SHuacai Chen                 qemu_set_irq(p->parent_irq[parent], p->parent_state[parent]);
105c012e0b1SHuacai Chen             }
106c012e0b1SHuacai Chen         }
107c012e0b1SHuacai Chen     }
108c012e0b1SHuacai Chen }
109c012e0b1SHuacai Chen 
110c012e0b1SHuacai Chen static uint64_t
liointc_read(void * opaque,hwaddr addr,unsigned int size)111c012e0b1SHuacai Chen liointc_read(void *opaque, hwaddr addr, unsigned int size)
112c012e0b1SHuacai Chen {
113c012e0b1SHuacai Chen     struct loongson_liointc *p = opaque;
114c012e0b1SHuacai Chen     uint32_t r = 0;
115c012e0b1SHuacai Chen 
116c012e0b1SHuacai Chen     /* Mapper is 1 byte */
117c012e0b1SHuacai Chen     if (size == 1 && addr < R_MAPPER_END) {
118c012e0b1SHuacai Chen         r = p->mapper[addr];
119c012e0b1SHuacai Chen         goto out;
120c012e0b1SHuacai Chen     }
121c012e0b1SHuacai Chen 
122dea96332SHuacai Chen     /* Rest are 4 bytes */
123c012e0b1SHuacai Chen     if (size != 4 || (addr % 4)) {
124c012e0b1SHuacai Chen         goto out;
125c012e0b1SHuacai Chen     }
126c012e0b1SHuacai Chen 
127dea96332SHuacai Chen     if (addr >= R_START && addr < R_END) {
12869027599SJiaxun Yang         hwaddr offset = addr - R_START;
12969027599SJiaxun Yang         int core = offset / R_ISR_SIZE;
13069027599SJiaxun Yang 
13169027599SJiaxun Yang         if (offset % R_ISR_SIZE) {
13269027599SJiaxun Yang             goto out;
13369027599SJiaxun Yang         }
134c012e0b1SHuacai Chen         r = p->per_core_isr[core];
135c012e0b1SHuacai Chen         goto out;
136c012e0b1SHuacai Chen     }
137c012e0b1SHuacai Chen 
138c012e0b1SHuacai Chen     switch (addr) {
139c012e0b1SHuacai Chen     case R_ISR:
140c012e0b1SHuacai Chen         r = p->isr;
141c012e0b1SHuacai Chen         break;
142c012e0b1SHuacai Chen     case R_IEN:
143c012e0b1SHuacai Chen         r = p->ien;
144c012e0b1SHuacai Chen         break;
145c012e0b1SHuacai Chen     default:
146c012e0b1SHuacai Chen         break;
147c012e0b1SHuacai Chen     }
148c012e0b1SHuacai Chen 
149c012e0b1SHuacai Chen out:
150dea96332SHuacai Chen     qemu_log_mask(CPU_LOG_INT, "%s: size=%d, addr=%"HWADDR_PRIx", val=%x\n",
151dea96332SHuacai Chen                   __func__, size, addr, r);
152c012e0b1SHuacai Chen     return r;
153c012e0b1SHuacai Chen }
154c012e0b1SHuacai Chen 
155c012e0b1SHuacai Chen static void
liointc_write(void * opaque,hwaddr addr,uint64_t val64,unsigned int size)156c012e0b1SHuacai Chen liointc_write(void *opaque, hwaddr addr,
157c012e0b1SHuacai Chen           uint64_t val64, unsigned int size)
158c012e0b1SHuacai Chen {
159c012e0b1SHuacai Chen     struct loongson_liointc *p = opaque;
160c012e0b1SHuacai Chen     uint32_t value = val64;
161c012e0b1SHuacai Chen 
162dea96332SHuacai Chen     qemu_log_mask(CPU_LOG_INT, "%s: size=%d, addr=%"HWADDR_PRIx", val=%x\n",
163dea96332SHuacai Chen                   __func__, size, addr, value);
164c012e0b1SHuacai Chen 
165c012e0b1SHuacai Chen     /* Mapper is 1 byte */
166c012e0b1SHuacai Chen     if (size == 1 && addr < R_MAPPER_END) {
167c012e0b1SHuacai Chen         p->mapper[addr] = value;
168c012e0b1SHuacai Chen         goto out;
169c012e0b1SHuacai Chen     }
170c012e0b1SHuacai Chen 
171dea96332SHuacai Chen     /* Rest are 4 bytes */
172c012e0b1SHuacai Chen     if (size != 4 || (addr % 4)) {
173c012e0b1SHuacai Chen         goto out;
174c012e0b1SHuacai Chen     }
175c012e0b1SHuacai Chen 
176dea96332SHuacai Chen     if (addr >= R_START && addr < R_END) {
17769027599SJiaxun Yang         hwaddr offset = addr - R_START;
17869027599SJiaxun Yang         int core = offset / R_ISR_SIZE;
17969027599SJiaxun Yang 
18069027599SJiaxun Yang         if (offset % R_ISR_SIZE) {
18169027599SJiaxun Yang             goto out;
18269027599SJiaxun Yang         }
183c012e0b1SHuacai Chen         p->per_core_isr[core] = value;
184c012e0b1SHuacai Chen         goto out;
185c012e0b1SHuacai Chen     }
186c012e0b1SHuacai Chen 
187c012e0b1SHuacai Chen     switch (addr) {
188c012e0b1SHuacai Chen     case R_IEN_SET:
189c012e0b1SHuacai Chen         p->ien |= value;
190c012e0b1SHuacai Chen         break;
191c012e0b1SHuacai Chen     case R_IEN_CLR:
192c012e0b1SHuacai Chen         p->ien &= ~value;
193c012e0b1SHuacai Chen         break;
194c012e0b1SHuacai Chen     default:
195c012e0b1SHuacai Chen         break;
196c012e0b1SHuacai Chen     }
197c012e0b1SHuacai Chen 
198c012e0b1SHuacai Chen out:
199c012e0b1SHuacai Chen     update_irq(p);
200c012e0b1SHuacai Chen }
201c012e0b1SHuacai Chen 
202c012e0b1SHuacai Chen static const MemoryRegionOps pic_ops = {
203c012e0b1SHuacai Chen     .read = liointc_read,
204c012e0b1SHuacai Chen     .write = liointc_write,
205c012e0b1SHuacai Chen     .endianness = DEVICE_NATIVE_ENDIAN,
206c012e0b1SHuacai Chen     .valid = {
207c012e0b1SHuacai Chen         .min_access_size = 1,
208c012e0b1SHuacai Chen         .max_access_size = 4
209c012e0b1SHuacai Chen     }
210c012e0b1SHuacai Chen };
211c012e0b1SHuacai Chen 
irq_handler(void * opaque,int irq,int level)212c012e0b1SHuacai Chen static void irq_handler(void *opaque, int irq, int level)
213c012e0b1SHuacai Chen {
214c012e0b1SHuacai Chen     struct loongson_liointc *p = opaque;
215c012e0b1SHuacai Chen 
216c012e0b1SHuacai Chen     p->pin_state &= ~(1 << irq);
217c012e0b1SHuacai Chen     p->pin_state |= level << irq;
218c012e0b1SHuacai Chen     update_irq(p);
219c012e0b1SHuacai Chen }
220c012e0b1SHuacai Chen 
loongson_liointc_init(Object * obj)221c012e0b1SHuacai Chen static void loongson_liointc_init(Object *obj)
222c012e0b1SHuacai Chen {
223c012e0b1SHuacai Chen     struct loongson_liointc *p = LOONGSON_LIOINTC(obj);
224c012e0b1SHuacai Chen     int i;
225c012e0b1SHuacai Chen 
226c012e0b1SHuacai Chen     qdev_init_gpio_in(DEVICE(obj), irq_handler, 32);
227c012e0b1SHuacai Chen 
228c012e0b1SHuacai Chen     for (i = 0; i < NUM_PARENTS; i++) {
229c012e0b1SHuacai Chen         sysbus_init_irq(SYS_BUS_DEVICE(obj), &p->parent_irq[i]);
230c012e0b1SHuacai Chen     }
231c012e0b1SHuacai Chen 
232c012e0b1SHuacai Chen     memory_region_init_io(&p->mmio, obj, &pic_ops, p,
233dea96332SHuacai Chen                          TYPE_LOONGSON_LIOINTC, R_END);
234c012e0b1SHuacai Chen     sysbus_init_mmio(SYS_BUS_DEVICE(obj), &p->mmio);
235c012e0b1SHuacai Chen }
236c012e0b1SHuacai Chen 
237c012e0b1SHuacai Chen static const TypeInfo loongson_liointc_info = {
238c012e0b1SHuacai Chen     .name          = TYPE_LOONGSON_LIOINTC,
239c012e0b1SHuacai Chen     .parent        = TYPE_SYS_BUS_DEVICE,
240c012e0b1SHuacai Chen     .instance_size = sizeof(struct loongson_liointc),
241c012e0b1SHuacai Chen     .instance_init = loongson_liointc_init,
242c012e0b1SHuacai Chen };
243c012e0b1SHuacai Chen 
loongson_liointc_register_types(void)244c012e0b1SHuacai Chen static void loongson_liointc_register_types(void)
245c012e0b1SHuacai Chen {
246c012e0b1SHuacai Chen     type_register_static(&loongson_liointc_info);
247c012e0b1SHuacai Chen }
248c012e0b1SHuacai Chen 
249c012e0b1SHuacai Chen type_init(loongson_liointc_register_types)
250