1c012e0b1SHuacai Chen /* 2c012e0b1SHuacai Chen * QEMU Loongson Local I/O interrupt controler. 3c012e0b1SHuacai Chen * 4*dea96332SHuacai 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" 24*dea96332SHuacai Chen #include "qemu/log.h" 25c012e0b1SHuacai Chen #include "hw/irq.h" 26c012e0b1SHuacai Chen #include "hw/qdev-properties.h" 27*dea96332SHuacai 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 42*dea96332SHuacai Chen #define R_ISR_SIZE 0x8 43*dea96332SHuacai Chen #define R_START 0x40 44c012e0b1SHuacai Chen #define R_END 0x64 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 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 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 122*dea96332SHuacai Chen /* Rest are 4 bytes */ 123c012e0b1SHuacai Chen if (size != 4 || (addr % 4)) { 124c012e0b1SHuacai Chen goto out; 125c012e0b1SHuacai Chen } 126c012e0b1SHuacai Chen 127*dea96332SHuacai Chen if (addr >= R_START && addr < R_END) { 128*dea96332SHuacai Chen int core = (addr - R_START) / R_ISR_SIZE; 129c012e0b1SHuacai Chen r = p->per_core_isr[core]; 130c012e0b1SHuacai Chen goto out; 131c012e0b1SHuacai Chen } 132c012e0b1SHuacai Chen 133c012e0b1SHuacai Chen switch (addr) { 134c012e0b1SHuacai Chen case R_ISR: 135c012e0b1SHuacai Chen r = p->isr; 136c012e0b1SHuacai Chen break; 137c012e0b1SHuacai Chen case R_IEN: 138c012e0b1SHuacai Chen r = p->ien; 139c012e0b1SHuacai Chen break; 140c012e0b1SHuacai Chen default: 141c012e0b1SHuacai Chen break; 142c012e0b1SHuacai Chen } 143c012e0b1SHuacai Chen 144c012e0b1SHuacai Chen out: 145*dea96332SHuacai Chen qemu_log_mask(CPU_LOG_INT, "%s: size=%d, addr=%"HWADDR_PRIx", val=%x\n", 146*dea96332SHuacai Chen __func__, size, addr, r); 147c012e0b1SHuacai Chen return r; 148c012e0b1SHuacai Chen } 149c012e0b1SHuacai Chen 150c012e0b1SHuacai Chen static void 151c012e0b1SHuacai Chen liointc_write(void *opaque, hwaddr addr, 152c012e0b1SHuacai Chen uint64_t val64, unsigned int size) 153c012e0b1SHuacai Chen { 154c012e0b1SHuacai Chen struct loongson_liointc *p = opaque; 155c012e0b1SHuacai Chen uint32_t value = val64; 156c012e0b1SHuacai Chen 157*dea96332SHuacai Chen qemu_log_mask(CPU_LOG_INT, "%s: size=%d, addr=%"HWADDR_PRIx", val=%x\n", 158*dea96332SHuacai Chen __func__, size, addr, value); 159c012e0b1SHuacai Chen 160c012e0b1SHuacai Chen /* Mapper is 1 byte */ 161c012e0b1SHuacai Chen if (size == 1 && addr < R_MAPPER_END) { 162c012e0b1SHuacai Chen p->mapper[addr] = value; 163c012e0b1SHuacai Chen goto out; 164c012e0b1SHuacai Chen } 165c012e0b1SHuacai Chen 166*dea96332SHuacai Chen /* Rest are 4 bytes */ 167c012e0b1SHuacai Chen if (size != 4 || (addr % 4)) { 168c012e0b1SHuacai Chen goto out; 169c012e0b1SHuacai Chen } 170c012e0b1SHuacai Chen 171*dea96332SHuacai Chen if (addr >= R_START && addr < R_END) { 172*dea96332SHuacai Chen int core = (addr - R_START) / R_ISR_SIZE; 173c012e0b1SHuacai Chen p->per_core_isr[core] = value; 174c012e0b1SHuacai Chen goto out; 175c012e0b1SHuacai Chen } 176c012e0b1SHuacai Chen 177c012e0b1SHuacai Chen switch (addr) { 178c012e0b1SHuacai Chen case R_IEN_SET: 179c012e0b1SHuacai Chen p->ien |= value; 180c012e0b1SHuacai Chen break; 181c012e0b1SHuacai Chen case R_IEN_CLR: 182c012e0b1SHuacai Chen p->ien &= ~value; 183c012e0b1SHuacai Chen break; 184c012e0b1SHuacai Chen default: 185c012e0b1SHuacai Chen break; 186c012e0b1SHuacai Chen } 187c012e0b1SHuacai Chen 188c012e0b1SHuacai Chen out: 189c012e0b1SHuacai Chen update_irq(p); 190c012e0b1SHuacai Chen } 191c012e0b1SHuacai Chen 192c012e0b1SHuacai Chen static const MemoryRegionOps pic_ops = { 193c012e0b1SHuacai Chen .read = liointc_read, 194c012e0b1SHuacai Chen .write = liointc_write, 195c012e0b1SHuacai Chen .endianness = DEVICE_NATIVE_ENDIAN, 196c012e0b1SHuacai Chen .valid = { 197c012e0b1SHuacai Chen .min_access_size = 1, 198c012e0b1SHuacai Chen .max_access_size = 4 199c012e0b1SHuacai Chen } 200c012e0b1SHuacai Chen }; 201c012e0b1SHuacai Chen 202c012e0b1SHuacai Chen static void irq_handler(void *opaque, int irq, int level) 203c012e0b1SHuacai Chen { 204c012e0b1SHuacai Chen struct loongson_liointc *p = opaque; 205c012e0b1SHuacai Chen 206c012e0b1SHuacai Chen p->pin_state &= ~(1 << irq); 207c012e0b1SHuacai Chen p->pin_state |= level << irq; 208c012e0b1SHuacai Chen update_irq(p); 209c012e0b1SHuacai Chen } 210c012e0b1SHuacai Chen 211c012e0b1SHuacai Chen static void loongson_liointc_init(Object *obj) 212c012e0b1SHuacai Chen { 213c012e0b1SHuacai Chen struct loongson_liointc *p = LOONGSON_LIOINTC(obj); 214c012e0b1SHuacai Chen int i; 215c012e0b1SHuacai Chen 216c012e0b1SHuacai Chen qdev_init_gpio_in(DEVICE(obj), irq_handler, 32); 217c012e0b1SHuacai Chen 218c012e0b1SHuacai Chen for (i = 0; i < NUM_PARENTS; i++) { 219c012e0b1SHuacai Chen sysbus_init_irq(SYS_BUS_DEVICE(obj), &p->parent_irq[i]); 220c012e0b1SHuacai Chen } 221c012e0b1SHuacai Chen 222c012e0b1SHuacai Chen memory_region_init_io(&p->mmio, obj, &pic_ops, p, 223*dea96332SHuacai Chen TYPE_LOONGSON_LIOINTC, R_END); 224c012e0b1SHuacai Chen sysbus_init_mmio(SYS_BUS_DEVICE(obj), &p->mmio); 225c012e0b1SHuacai Chen } 226c012e0b1SHuacai Chen 227c012e0b1SHuacai Chen static const TypeInfo loongson_liointc_info = { 228c012e0b1SHuacai Chen .name = TYPE_LOONGSON_LIOINTC, 229c012e0b1SHuacai Chen .parent = TYPE_SYS_BUS_DEVICE, 230c012e0b1SHuacai Chen .instance_size = sizeof(struct loongson_liointc), 231c012e0b1SHuacai Chen .instance_init = loongson_liointc_init, 232c012e0b1SHuacai Chen }; 233c012e0b1SHuacai Chen 234c012e0b1SHuacai Chen static void loongson_liointc_register_types(void) 235c012e0b1SHuacai Chen { 236c012e0b1SHuacai Chen type_register_static(&loongson_liointc_info); 237c012e0b1SHuacai Chen } 238c012e0b1SHuacai Chen 239c012e0b1SHuacai Chen type_init(loongson_liointc_register_types) 240