154f59d78SCédric Le Goater /* 254f59d78SCédric Le Goater * QEMU PowerPC PowerNV Processor Service Interface (PSI) model 354f59d78SCédric Le Goater * 454f59d78SCédric Le Goater * Copyright (c) 2015-2017, IBM Corporation. 554f59d78SCédric Le Goater * 654f59d78SCédric Le Goater * This library is free software; you can redistribute it and/or 754f59d78SCédric Le Goater * modify it under the terms of the GNU Lesser General Public 854f59d78SCédric Le Goater * License as published by the Free Software Foundation; either 954f59d78SCédric Le Goater * version 2 of the License, or (at your option) any later version. 1054f59d78SCédric Le Goater * 1154f59d78SCédric Le Goater * This library is distributed in the hope that it will be useful, 1254f59d78SCédric Le Goater * but WITHOUT ANY WARRANTY; without even the implied warranty of 1354f59d78SCédric Le Goater * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1454f59d78SCédric Le Goater * Lesser General Public License for more details. 1554f59d78SCédric Le Goater * 1654f59d78SCédric Le Goater * You should have received a copy of the GNU Lesser General Public 1754f59d78SCédric Le Goater * License along with this library; if not, see <http://www.gnu.org/licenses/>. 1854f59d78SCédric Le Goater */ 1954f59d78SCédric Le Goater 2054f59d78SCédric Le Goater #include "qemu/osdep.h" 2154f59d78SCédric Le Goater #include "hw/hw.h" 2254f59d78SCédric Le Goater #include "target/ppc/cpu.h" 2354f59d78SCédric Le Goater #include "qemu/log.h" 240b8fa32fSMarkus Armbruster #include "qemu/module.h" 25*71e8a915SMarkus Armbruster #include "sysemu/reset.h" 2654f59d78SCédric Le Goater #include "qapi/error.h" 27c38536bcSCédric Le Goater #include "monitor/monitor.h" 2854f59d78SCédric Le Goater 2954f59d78SCédric Le Goater #include "exec/address-spaces.h" 3054f59d78SCédric Le Goater 3154f59d78SCédric Le Goater #include "hw/ppc/fdt.h" 3254f59d78SCédric Le Goater #include "hw/ppc/pnv.h" 3354f59d78SCédric Le Goater #include "hw/ppc/pnv_xscom.h" 3454f59d78SCédric Le Goater #include "hw/ppc/pnv_psi.h" 3554f59d78SCédric Le Goater 3654f59d78SCédric Le Goater #include <libfdt.h> 3754f59d78SCédric Le Goater 3854f59d78SCédric Le Goater #define PSIHB_XSCOM_FIR_RW 0x00 3954f59d78SCédric Le Goater #define PSIHB_XSCOM_FIR_AND 0x01 4054f59d78SCédric Le Goater #define PSIHB_XSCOM_FIR_OR 0x02 4154f59d78SCédric Le Goater #define PSIHB_XSCOM_FIRMASK_RW 0x03 4254f59d78SCédric Le Goater #define PSIHB_XSCOM_FIRMASK_AND 0x04 4354f59d78SCédric Le Goater #define PSIHB_XSCOM_FIRMASK_OR 0x05 4454f59d78SCédric Le Goater #define PSIHB_XSCOM_FIRACT0 0x06 4554f59d78SCédric Le Goater #define PSIHB_XSCOM_FIRACT1 0x07 4654f59d78SCédric Le Goater 4754f59d78SCédric Le Goater /* Host Bridge Base Address Register */ 4854f59d78SCédric Le Goater #define PSIHB_XSCOM_BAR 0x0a 4954f59d78SCédric Le Goater #define PSIHB_BAR_EN 0x0000000000000001ull 5054f59d78SCédric Le Goater 5154f59d78SCédric Le Goater /* FSP Base Address Register */ 5254f59d78SCédric Le Goater #define PSIHB_XSCOM_FSPBAR 0x0b 5354f59d78SCédric Le Goater 5454f59d78SCédric Le Goater /* PSI Host Bridge Control/Status Register */ 5554f59d78SCédric Le Goater #define PSIHB_XSCOM_CR 0x0e 5654f59d78SCédric Le Goater #define PSIHB_CR_FSP_CMD_ENABLE 0x8000000000000000ull 5754f59d78SCédric Le Goater #define PSIHB_CR_FSP_MMIO_ENABLE 0x4000000000000000ull 5854f59d78SCédric Le Goater #define PSIHB_CR_FSP_IRQ_ENABLE 0x1000000000000000ull 5954f59d78SCédric Le Goater #define PSIHB_CR_FSP_ERR_RSP_ENABLE 0x0800000000000000ull 6054f59d78SCédric Le Goater #define PSIHB_CR_PSI_LINK_ENABLE 0x0400000000000000ull 6154f59d78SCédric Le Goater #define PSIHB_CR_FSP_RESET 0x0200000000000000ull 6254f59d78SCédric Le Goater #define PSIHB_CR_PSIHB_RESET 0x0100000000000000ull 6354f59d78SCédric Le Goater #define PSIHB_CR_PSI_IRQ 0x0000800000000000ull 6454f59d78SCédric Le Goater #define PSIHB_CR_FSP_IRQ 0x0000400000000000ull 6554f59d78SCédric Le Goater #define PSIHB_CR_FSP_LINK_ACTIVE 0x0000200000000000ull 6654f59d78SCédric Le Goater #define PSIHB_CR_IRQ_CMD_EXPECT 0x0000010000000000ull 6754f59d78SCédric Le Goater /* and more ... */ 6854f59d78SCédric Le Goater 6954f59d78SCédric Le Goater /* PSIHB Status / Error Mask Register */ 7054f59d78SCédric Le Goater #define PSIHB_XSCOM_SEMR 0x0f 7154f59d78SCédric Le Goater 7254f59d78SCédric Le Goater /* XIVR, to signal interrupts to the CEC firmware. more XIVR below. */ 7354f59d78SCédric Le Goater #define PSIHB_XSCOM_XIVR_FSP 0x10 7454f59d78SCédric Le Goater #define PSIHB_XIVR_SERVER_SH 40 7554f59d78SCédric Le Goater #define PSIHB_XIVR_SERVER_MSK (0xffffull << PSIHB_XIVR_SERVER_SH) 7654f59d78SCédric Le Goater #define PSIHB_XIVR_PRIO_SH 32 7754f59d78SCédric Le Goater #define PSIHB_XIVR_PRIO_MSK (0xffull << PSIHB_XIVR_PRIO_SH) 7854f59d78SCédric Le Goater #define PSIHB_XIVR_SRC_SH 29 7954f59d78SCédric Le Goater #define PSIHB_XIVR_SRC_MSK (0x7ull << PSIHB_XIVR_SRC_SH) 8054f59d78SCédric Le Goater #define PSIHB_XIVR_PENDING 0x01000000ull 8154f59d78SCédric Le Goater 8254f59d78SCédric Le Goater /* PSI Host Bridge Set Control/ Status Register */ 8354f59d78SCédric Le Goater #define PSIHB_XSCOM_SCR 0x12 8454f59d78SCédric Le Goater 8554f59d78SCédric Le Goater /* PSI Host Bridge Clear Control/ Status Register */ 8654f59d78SCédric Le Goater #define PSIHB_XSCOM_CCR 0x13 8754f59d78SCédric Le Goater 8854f59d78SCédric Le Goater /* DMA Upper Address Register */ 8954f59d78SCédric Le Goater #define PSIHB_XSCOM_DMA_UPADD 0x14 9054f59d78SCédric Le Goater 9154f59d78SCédric Le Goater /* Interrupt Status */ 9254f59d78SCédric Le Goater #define PSIHB_XSCOM_IRQ_STAT 0x15 9354f59d78SCédric Le Goater #define PSIHB_IRQ_STAT_OCC 0x0000001000000000ull 9454f59d78SCédric Le Goater #define PSIHB_IRQ_STAT_FSI 0x0000000800000000ull 9554f59d78SCédric Le Goater #define PSIHB_IRQ_STAT_LPCI2C 0x0000000400000000ull 9654f59d78SCédric Le Goater #define PSIHB_IRQ_STAT_LOCERR 0x0000000200000000ull 9754f59d78SCédric Le Goater #define PSIHB_IRQ_STAT_EXT 0x0000000100000000ull 9854f59d78SCédric Le Goater 9954f59d78SCédric Le Goater /* remaining XIVR */ 10054f59d78SCédric Le Goater #define PSIHB_XSCOM_XIVR_OCC 0x16 10154f59d78SCédric Le Goater #define PSIHB_XSCOM_XIVR_FSI 0x17 10254f59d78SCédric Le Goater #define PSIHB_XSCOM_XIVR_LPCI2C 0x18 10354f59d78SCédric Le Goater #define PSIHB_XSCOM_XIVR_LOCERR 0x19 10454f59d78SCédric Le Goater #define PSIHB_XSCOM_XIVR_EXT 0x1a 10554f59d78SCédric Le Goater 10654f59d78SCédric Le Goater /* Interrupt Requester Source Compare Register */ 10754f59d78SCédric Le Goater #define PSIHB_XSCOM_IRSN 0x1b 10854f59d78SCédric Le Goater #define PSIHB_IRSN_COMP_SH 45 10954f59d78SCédric Le Goater #define PSIHB_IRSN_COMP_MSK (0x7ffffull << PSIHB_IRSN_COMP_SH) 11054f59d78SCédric Le Goater #define PSIHB_IRSN_IRQ_MUX 0x0000000800000000ull 11154f59d78SCédric Le Goater #define PSIHB_IRSN_IRQ_RESET 0x0000000400000000ull 11254f59d78SCédric Le Goater #define PSIHB_IRSN_DOWNSTREAM_EN 0x0000000200000000ull 11354f59d78SCédric Le Goater #define PSIHB_IRSN_UPSTREAM_EN 0x0000000100000000ull 11454f59d78SCédric Le Goater #define PSIHB_IRSN_COMPMASK_SH 13 11554f59d78SCédric Le Goater #define PSIHB_IRSN_COMPMASK_MSK (0x7ffffull << PSIHB_IRSN_COMPMASK_SH) 11654f59d78SCédric Le Goater 11754f59d78SCédric Le Goater #define PSIHB_BAR_MASK 0x0003fffffff00000ull 11854f59d78SCédric Le Goater #define PSIHB_FSPBAR_MASK 0x0003ffff00000000ull 11954f59d78SCédric Le Goater 120c38536bcSCédric Le Goater #define PSIHB9_BAR_MASK 0x00fffffffff00000ull 121c38536bcSCédric Le Goater #define PSIHB9_FSPBAR_MASK 0x00ffffff00000000ull 122c38536bcSCédric Le Goater 123029699aaSCédric Le Goater #define PSIHB_REG(addr) (((addr) >> 3) + PSIHB_XSCOM_BAR) 124029699aaSCédric Le Goater 12554f59d78SCédric Le Goater static void pnv_psi_set_bar(PnvPsi *psi, uint64_t bar) 12654f59d78SCédric Le Goater { 127ae856055SCédric Le Goater PnvPsiClass *ppc = PNV_PSI_GET_CLASS(psi); 12854f59d78SCédric Le Goater MemoryRegion *sysmem = get_system_memory(); 12954f59d78SCédric Le Goater uint64_t old = psi->regs[PSIHB_XSCOM_BAR]; 13054f59d78SCédric Le Goater 131ae856055SCédric Le Goater psi->regs[PSIHB_XSCOM_BAR] = bar & (ppc->bar_mask | PSIHB_BAR_EN); 13254f59d78SCédric Le Goater 13354f59d78SCédric Le Goater /* Update MR, always remove it first */ 13454f59d78SCédric Le Goater if (old & PSIHB_BAR_EN) { 13554f59d78SCédric Le Goater memory_region_del_subregion(sysmem, &psi->regs_mr); 13654f59d78SCédric Le Goater } 13754f59d78SCédric Le Goater 13854f59d78SCédric Le Goater /* Then add it back if needed */ 13954f59d78SCédric Le Goater if (bar & PSIHB_BAR_EN) { 140ae856055SCédric Le Goater uint64_t addr = bar & ppc->bar_mask; 14154f59d78SCédric Le Goater memory_region_add_subregion(sysmem, addr, &psi->regs_mr); 14254f59d78SCédric Le Goater } 14354f59d78SCédric Le Goater } 14454f59d78SCédric Le Goater 14554f59d78SCédric Le Goater static void pnv_psi_update_fsp_mr(PnvPsi *psi) 14654f59d78SCédric Le Goater { 14754f59d78SCédric Le Goater /* TODO: Update FSP MR if/when we support FSP BAR */ 14854f59d78SCédric Le Goater } 14954f59d78SCédric Le Goater 15054f59d78SCédric Le Goater static void pnv_psi_set_cr(PnvPsi *psi, uint64_t cr) 15154f59d78SCédric Le Goater { 15254f59d78SCédric Le Goater uint64_t old = psi->regs[PSIHB_XSCOM_CR]; 15354f59d78SCédric Le Goater 15454f59d78SCédric Le Goater psi->regs[PSIHB_XSCOM_CR] = cr; 15554f59d78SCédric Le Goater 15654f59d78SCédric Le Goater /* Check some bit changes */ 15754f59d78SCédric Le Goater if ((old ^ psi->regs[PSIHB_XSCOM_CR]) & PSIHB_CR_FSP_MMIO_ENABLE) { 15854f59d78SCédric Le Goater pnv_psi_update_fsp_mr(psi); 15954f59d78SCédric Le Goater } 16054f59d78SCédric Le Goater } 16154f59d78SCédric Le Goater 16254f59d78SCédric Le Goater static void pnv_psi_set_irsn(PnvPsi *psi, uint64_t val) 16354f59d78SCédric Le Goater { 164ae856055SCédric Le Goater ICSState *ics = &PNV8_PSI(psi)->ics; 16554f59d78SCédric Le Goater 16654f59d78SCédric Le Goater /* In this model we ignore the up/down enable bits for now 16754f59d78SCédric Le Goater * as SW doesn't use them (other than setting them at boot). 16854f59d78SCédric Le Goater * We ignore IRQ_MUX, its meaning isn't clear and we don't use 16954f59d78SCédric Le Goater * it and finally we ignore reset (XXX fix that ?) 17054f59d78SCédric Le Goater */ 17154f59d78SCédric Le Goater psi->regs[PSIHB_XSCOM_IRSN] = val & (PSIHB_IRSN_COMP_MSK | 17254f59d78SCédric Le Goater PSIHB_IRSN_IRQ_MUX | 17354f59d78SCédric Le Goater PSIHB_IRSN_IRQ_RESET | 17454f59d78SCédric Le Goater PSIHB_IRSN_DOWNSTREAM_EN | 17554f59d78SCédric Le Goater PSIHB_IRSN_UPSTREAM_EN); 17654f59d78SCédric Le Goater 17754f59d78SCédric Le Goater /* We ignore the compare mask as well, our ICS emulation is too 17854f59d78SCédric Le Goater * simplistic to make any use if it, and we extract the offset 17954f59d78SCédric Le Goater * from the compare value 18054f59d78SCédric Le Goater */ 18154f59d78SCédric Le Goater ics->offset = (val & PSIHB_IRSN_COMP_MSK) >> PSIHB_IRSN_COMP_SH; 18254f59d78SCédric Le Goater } 18354f59d78SCédric Le Goater 18454f59d78SCédric Le Goater /* 18554f59d78SCédric Le Goater * FSP and PSI interrupts are muxed under the same number. 18654f59d78SCédric Le Goater */ 18754f59d78SCédric Le Goater static const uint32_t xivr_regs[] = { 18854f59d78SCédric Le Goater [PSIHB_IRQ_PSI] = PSIHB_XSCOM_XIVR_FSP, 18954f59d78SCédric Le Goater [PSIHB_IRQ_FSP] = PSIHB_XSCOM_XIVR_FSP, 19054f59d78SCédric Le Goater [PSIHB_IRQ_OCC] = PSIHB_XSCOM_XIVR_OCC, 19154f59d78SCédric Le Goater [PSIHB_IRQ_FSI] = PSIHB_XSCOM_XIVR_FSI, 19254f59d78SCédric Le Goater [PSIHB_IRQ_LPC_I2C] = PSIHB_XSCOM_XIVR_LPCI2C, 19354f59d78SCédric Le Goater [PSIHB_IRQ_LOCAL_ERR] = PSIHB_XSCOM_XIVR_LOCERR, 19454f59d78SCédric Le Goater [PSIHB_IRQ_EXTERNAL] = PSIHB_XSCOM_XIVR_EXT, 19554f59d78SCédric Le Goater }; 19654f59d78SCédric Le Goater 19754f59d78SCédric Le Goater static const uint32_t stat_regs[] = { 19854f59d78SCédric Le Goater [PSIHB_IRQ_PSI] = PSIHB_XSCOM_CR, 19954f59d78SCédric Le Goater [PSIHB_IRQ_FSP] = PSIHB_XSCOM_CR, 20054f59d78SCédric Le Goater [PSIHB_IRQ_OCC] = PSIHB_XSCOM_IRQ_STAT, 20154f59d78SCédric Le Goater [PSIHB_IRQ_FSI] = PSIHB_XSCOM_IRQ_STAT, 20254f59d78SCédric Le Goater [PSIHB_IRQ_LPC_I2C] = PSIHB_XSCOM_IRQ_STAT, 20354f59d78SCédric Le Goater [PSIHB_IRQ_LOCAL_ERR] = PSIHB_XSCOM_IRQ_STAT, 20454f59d78SCédric Le Goater [PSIHB_IRQ_EXTERNAL] = PSIHB_XSCOM_IRQ_STAT, 20554f59d78SCédric Le Goater }; 20654f59d78SCédric Le Goater 20754f59d78SCédric Le Goater static const uint64_t stat_bits[] = { 20854f59d78SCédric Le Goater [PSIHB_IRQ_PSI] = PSIHB_CR_PSI_IRQ, 20954f59d78SCédric Le Goater [PSIHB_IRQ_FSP] = PSIHB_CR_FSP_IRQ, 21054f59d78SCédric Le Goater [PSIHB_IRQ_OCC] = PSIHB_IRQ_STAT_OCC, 21154f59d78SCédric Le Goater [PSIHB_IRQ_FSI] = PSIHB_IRQ_STAT_FSI, 21254f59d78SCédric Le Goater [PSIHB_IRQ_LPC_I2C] = PSIHB_IRQ_STAT_LPCI2C, 21354f59d78SCédric Le Goater [PSIHB_IRQ_LOCAL_ERR] = PSIHB_IRQ_STAT_LOCERR, 21454f59d78SCédric Le Goater [PSIHB_IRQ_EXTERNAL] = PSIHB_IRQ_STAT_EXT, 21554f59d78SCédric Le Goater }; 21654f59d78SCédric Le Goater 217ae856055SCédric Le Goater void pnv_psi_irq_set(PnvPsi *psi, int irq, bool state) 218ae856055SCédric Le Goater { 219ae856055SCédric Le Goater PNV_PSI_GET_CLASS(psi)->irq_set(psi, irq, state); 220ae856055SCédric Le Goater } 221ae856055SCédric Le Goater 222ae856055SCédric Le Goater static void pnv_psi_power8_irq_set(PnvPsi *psi, int irq, bool state) 22354f59d78SCédric Le Goater { 22454f59d78SCédric Le Goater uint32_t xivr_reg; 22554f59d78SCédric Le Goater uint32_t stat_reg; 22654f59d78SCédric Le Goater uint32_t src; 22754f59d78SCédric Le Goater bool masked; 22854f59d78SCédric Le Goater 22954f59d78SCédric Le Goater if (irq > PSIHB_IRQ_EXTERNAL) { 23054f59d78SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "PSI: Unsupported irq %d\n", irq); 23154f59d78SCédric Le Goater return; 23254f59d78SCédric Le Goater } 23354f59d78SCédric Le Goater 23454f59d78SCédric Le Goater xivr_reg = xivr_regs[irq]; 23554f59d78SCédric Le Goater stat_reg = stat_regs[irq]; 23654f59d78SCédric Le Goater 23754f59d78SCédric Le Goater src = (psi->regs[xivr_reg] & PSIHB_XIVR_SRC_MSK) >> PSIHB_XIVR_SRC_SH; 23854f59d78SCédric Le Goater if (state) { 23954f59d78SCédric Le Goater psi->regs[stat_reg] |= stat_bits[irq]; 24054f59d78SCédric Le Goater /* TODO: optimization, check mask here. That means 24154f59d78SCédric Le Goater * re-evaluating when unmasking 24254f59d78SCédric Le Goater */ 243f8df9003SCédric Le Goater qemu_irq_raise(psi->qirqs[src]); 24454f59d78SCédric Le Goater } else { 24554f59d78SCédric Le Goater psi->regs[stat_reg] &= ~stat_bits[irq]; 24654f59d78SCédric Le Goater 24754f59d78SCédric Le Goater /* FSP and PSI are muxed so don't lower if either is still set */ 24854f59d78SCédric Le Goater if (stat_reg != PSIHB_XSCOM_CR || 24954f59d78SCédric Le Goater !(psi->regs[stat_reg] & (PSIHB_CR_PSI_IRQ | PSIHB_CR_FSP_IRQ))) { 250f8df9003SCédric Le Goater qemu_irq_lower(psi->qirqs[src]); 25154f59d78SCédric Le Goater } else { 25254f59d78SCédric Le Goater state = true; 25354f59d78SCédric Le Goater } 25454f59d78SCédric Le Goater } 25554f59d78SCédric Le Goater 25654f59d78SCédric Le Goater /* Note about the emulation of the pending bit: This isn't 25754f59d78SCédric Le Goater * entirely correct. The pending bit should be cleared when the 25854f59d78SCédric Le Goater * EOI has been received. However, we don't have callbacks on EOI 25954f59d78SCédric Le Goater * (especially not under KVM) so no way to emulate that properly, 26054f59d78SCédric Le Goater * so instead we just set that bit as the logical "output" of the 26154f59d78SCédric Le Goater * XIVR (ie pending & !masked) 26254f59d78SCédric Le Goater * 26354f59d78SCédric Le Goater * CLG: We could define a new ICS object with a custom eoi() 26454f59d78SCédric Le Goater * handler to clear the pending bit. But I am not sure this would 26554f59d78SCédric Le Goater * be useful for the software anyhow. 26654f59d78SCédric Le Goater */ 26754f59d78SCédric Le Goater masked = (psi->regs[xivr_reg] & PSIHB_XIVR_PRIO_MSK) == PSIHB_XIVR_PRIO_MSK; 26854f59d78SCédric Le Goater if (state && !masked) { 26954f59d78SCédric Le Goater psi->regs[xivr_reg] |= PSIHB_XIVR_PENDING; 27054f59d78SCédric Le Goater } else { 27154f59d78SCédric Le Goater psi->regs[xivr_reg] &= ~PSIHB_XIVR_PENDING; 27254f59d78SCédric Le Goater } 27354f59d78SCédric Le Goater } 27454f59d78SCédric Le Goater 27554f59d78SCédric Le Goater static void pnv_psi_set_xivr(PnvPsi *psi, uint32_t reg, uint64_t val) 27654f59d78SCédric Le Goater { 277ae856055SCédric Le Goater ICSState *ics = &PNV8_PSI(psi)->ics; 27854f59d78SCédric Le Goater uint16_t server; 27954f59d78SCédric Le Goater uint8_t prio; 28054f59d78SCédric Le Goater uint8_t src; 28154f59d78SCédric Le Goater 28254f59d78SCédric Le Goater psi->regs[reg] = (psi->regs[reg] & PSIHB_XIVR_PENDING) | 28354f59d78SCédric Le Goater (val & (PSIHB_XIVR_SERVER_MSK | 28454f59d78SCédric Le Goater PSIHB_XIVR_PRIO_MSK | 28554f59d78SCédric Le Goater PSIHB_XIVR_SRC_MSK)); 28654f59d78SCédric Le Goater val = psi->regs[reg]; 28754f59d78SCédric Le Goater server = (val & PSIHB_XIVR_SERVER_MSK) >> PSIHB_XIVR_SERVER_SH; 28854f59d78SCédric Le Goater prio = (val & PSIHB_XIVR_PRIO_MSK) >> PSIHB_XIVR_PRIO_SH; 28954f59d78SCédric Le Goater src = (val & PSIHB_XIVR_SRC_MSK) >> PSIHB_XIVR_SRC_SH; 29054f59d78SCédric Le Goater 29154f59d78SCédric Le Goater if (src >= PSI_NUM_INTERRUPTS) { 29254f59d78SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "PSI: Unsupported irq %d\n", src); 29354f59d78SCédric Le Goater return; 29454f59d78SCédric Le Goater } 29554f59d78SCédric Le Goater 29654f59d78SCédric Le Goater /* Remove pending bit if the IRQ is masked */ 29754f59d78SCédric Le Goater if ((psi->regs[reg] & PSIHB_XIVR_PRIO_MSK) == PSIHB_XIVR_PRIO_MSK) { 29854f59d78SCédric Le Goater psi->regs[reg] &= ~PSIHB_XIVR_PENDING; 29954f59d78SCédric Le Goater } 30054f59d78SCédric Le Goater 30154f59d78SCédric Le Goater /* The low order 2 bits are the link pointer (Type II interrupts). 30254f59d78SCédric Le Goater * Shift back to get a valid IRQ server. 30354f59d78SCédric Le Goater */ 30454f59d78SCédric Le Goater server >>= 2; 30554f59d78SCédric Le Goater 30654f59d78SCédric Le Goater /* Now because of source remapping, weird things can happen 30754f59d78SCédric Le Goater * if you change the source number dynamically, our simple ICS 30854f59d78SCédric Le Goater * doesn't deal with remapping. So we just poke a different 30954f59d78SCédric Le Goater * ICS entry based on what source number was written. This will 31054f59d78SCédric Le Goater * do for now but a more accurate implementation would instead 31154f59d78SCédric Le Goater * use a fixed server/prio and a remapper of the generated irq. 31254f59d78SCédric Le Goater */ 31354f59d78SCédric Le Goater ics_simple_write_xive(ics, src, server, prio, prio); 31454f59d78SCédric Le Goater } 31554f59d78SCédric Le Goater 31654f59d78SCédric Le Goater static uint64_t pnv_psi_reg_read(PnvPsi *psi, uint32_t offset, bool mmio) 31754f59d78SCédric Le Goater { 31854f59d78SCédric Le Goater uint64_t val = 0xffffffffffffffffull; 31954f59d78SCédric Le Goater 32054f59d78SCédric Le Goater switch (offset) { 32154f59d78SCédric Le Goater case PSIHB_XSCOM_FIR_RW: 32254f59d78SCédric Le Goater case PSIHB_XSCOM_FIRACT0: 32354f59d78SCédric Le Goater case PSIHB_XSCOM_FIRACT1: 32454f59d78SCédric Le Goater case PSIHB_XSCOM_BAR: 32554f59d78SCédric Le Goater case PSIHB_XSCOM_FSPBAR: 32654f59d78SCédric Le Goater case PSIHB_XSCOM_CR: 32754f59d78SCédric Le Goater case PSIHB_XSCOM_XIVR_FSP: 32854f59d78SCédric Le Goater case PSIHB_XSCOM_XIVR_OCC: 32954f59d78SCédric Le Goater case PSIHB_XSCOM_XIVR_FSI: 33054f59d78SCédric Le Goater case PSIHB_XSCOM_XIVR_LPCI2C: 33154f59d78SCédric Le Goater case PSIHB_XSCOM_XIVR_LOCERR: 33254f59d78SCédric Le Goater case PSIHB_XSCOM_XIVR_EXT: 33354f59d78SCédric Le Goater case PSIHB_XSCOM_IRQ_STAT: 33454f59d78SCédric Le Goater case PSIHB_XSCOM_SEMR: 33554f59d78SCédric Le Goater case PSIHB_XSCOM_DMA_UPADD: 33654f59d78SCédric Le Goater case PSIHB_XSCOM_IRSN: 33754f59d78SCédric Le Goater val = psi->regs[offset]; 33854f59d78SCédric Le Goater break; 33954f59d78SCédric Le Goater default: 340cdbaf8cdSCédric Le Goater qemu_log_mask(LOG_UNIMP, "PSI: read at 0x%" PRIx32 "\n", offset); 34154f59d78SCédric Le Goater } 34254f59d78SCédric Le Goater return val; 34354f59d78SCédric Le Goater } 34454f59d78SCédric Le Goater 34554f59d78SCédric Le Goater static void pnv_psi_reg_write(PnvPsi *psi, uint32_t offset, uint64_t val, 34654f59d78SCédric Le Goater bool mmio) 34754f59d78SCédric Le Goater { 34854f59d78SCédric Le Goater switch (offset) { 34954f59d78SCédric Le Goater case PSIHB_XSCOM_FIR_RW: 35054f59d78SCédric Le Goater case PSIHB_XSCOM_FIRACT0: 35154f59d78SCédric Le Goater case PSIHB_XSCOM_FIRACT1: 35254f59d78SCédric Le Goater case PSIHB_XSCOM_SEMR: 35354f59d78SCédric Le Goater case PSIHB_XSCOM_DMA_UPADD: 35454f59d78SCédric Le Goater psi->regs[offset] = val; 35554f59d78SCédric Le Goater break; 35654f59d78SCédric Le Goater case PSIHB_XSCOM_FIR_OR: 35754f59d78SCédric Le Goater psi->regs[PSIHB_XSCOM_FIR_RW] |= val; 35854f59d78SCédric Le Goater break; 35954f59d78SCédric Le Goater case PSIHB_XSCOM_FIR_AND: 36054f59d78SCédric Le Goater psi->regs[PSIHB_XSCOM_FIR_RW] &= val; 36154f59d78SCédric Le Goater break; 36254f59d78SCédric Le Goater case PSIHB_XSCOM_BAR: 36354f59d78SCédric Le Goater /* Only XSCOM can write this one */ 36454f59d78SCédric Le Goater if (!mmio) { 36554f59d78SCédric Le Goater pnv_psi_set_bar(psi, val); 36654f59d78SCédric Le Goater } else { 36754f59d78SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "PSI: invalid write of BAR\n"); 36854f59d78SCédric Le Goater } 36954f59d78SCédric Le Goater break; 37054f59d78SCédric Le Goater case PSIHB_XSCOM_FSPBAR: 37154f59d78SCédric Le Goater psi->regs[PSIHB_XSCOM_FSPBAR] = val & PSIHB_FSPBAR_MASK; 37254f59d78SCédric Le Goater pnv_psi_update_fsp_mr(psi); 37354f59d78SCédric Le Goater break; 37454f59d78SCédric Le Goater case PSIHB_XSCOM_CR: 37554f59d78SCédric Le Goater pnv_psi_set_cr(psi, val); 37654f59d78SCédric Le Goater break; 37754f59d78SCédric Le Goater case PSIHB_XSCOM_SCR: 37854f59d78SCédric Le Goater pnv_psi_set_cr(psi, psi->regs[PSIHB_XSCOM_CR] | val); 37954f59d78SCédric Le Goater break; 38054f59d78SCédric Le Goater case PSIHB_XSCOM_CCR: 38154f59d78SCédric Le Goater pnv_psi_set_cr(psi, psi->regs[PSIHB_XSCOM_CR] & ~val); 38254f59d78SCédric Le Goater break; 38354f59d78SCédric Le Goater case PSIHB_XSCOM_XIVR_FSP: 38454f59d78SCédric Le Goater case PSIHB_XSCOM_XIVR_OCC: 38554f59d78SCédric Le Goater case PSIHB_XSCOM_XIVR_FSI: 38654f59d78SCédric Le Goater case PSIHB_XSCOM_XIVR_LPCI2C: 38754f59d78SCédric Le Goater case PSIHB_XSCOM_XIVR_LOCERR: 38854f59d78SCédric Le Goater case PSIHB_XSCOM_XIVR_EXT: 38954f59d78SCédric Le Goater pnv_psi_set_xivr(psi, offset, val); 39054f59d78SCédric Le Goater break; 39154f59d78SCédric Le Goater case PSIHB_XSCOM_IRQ_STAT: 39254f59d78SCédric Le Goater /* Read only */ 39354f59d78SCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "PSI: invalid write of IRQ_STAT\n"); 39454f59d78SCédric Le Goater break; 39554f59d78SCédric Le Goater case PSIHB_XSCOM_IRSN: 39654f59d78SCédric Le Goater pnv_psi_set_irsn(psi, val); 39754f59d78SCédric Le Goater break; 39854f59d78SCédric Le Goater default: 399cdbaf8cdSCédric Le Goater qemu_log_mask(LOG_UNIMP, "PSI: write at 0x%" PRIx32 "\n", offset); 40054f59d78SCédric Le Goater } 40154f59d78SCédric Le Goater } 40254f59d78SCédric Le Goater 40354f59d78SCédric Le Goater /* 40454f59d78SCédric Le Goater * The values of the registers when accessed through the MMIO region 40554f59d78SCédric Le Goater * follow the relation : xscom = (mmio + 0x50) >> 3 40654f59d78SCédric Le Goater */ 40754f59d78SCédric Le Goater static uint64_t pnv_psi_mmio_read(void *opaque, hwaddr addr, unsigned size) 40854f59d78SCédric Le Goater { 409029699aaSCédric Le Goater return pnv_psi_reg_read(opaque, PSIHB_REG(addr), true); 41054f59d78SCédric Le Goater } 41154f59d78SCédric Le Goater 41254f59d78SCédric Le Goater static void pnv_psi_mmio_write(void *opaque, hwaddr addr, 41354f59d78SCédric Le Goater uint64_t val, unsigned size) 41454f59d78SCédric Le Goater { 415029699aaSCédric Le Goater pnv_psi_reg_write(opaque, PSIHB_REG(addr), val, true); 41654f59d78SCédric Le Goater } 41754f59d78SCédric Le Goater 41854f59d78SCédric Le Goater static const MemoryRegionOps psi_mmio_ops = { 41954f59d78SCédric Le Goater .read = pnv_psi_mmio_read, 42054f59d78SCédric Le Goater .write = pnv_psi_mmio_write, 42154f59d78SCédric Le Goater .endianness = DEVICE_BIG_ENDIAN, 42254f59d78SCédric Le Goater .valid = { 42354f59d78SCédric Le Goater .min_access_size = 8, 42454f59d78SCédric Le Goater .max_access_size = 8, 42554f59d78SCédric Le Goater }, 42654f59d78SCédric Le Goater .impl = { 42754f59d78SCédric Le Goater .min_access_size = 8, 42854f59d78SCédric Le Goater .max_access_size = 8, 42954f59d78SCédric Le Goater }, 43054f59d78SCédric Le Goater }; 43154f59d78SCédric Le Goater 43254f59d78SCédric Le Goater static uint64_t pnv_psi_xscom_read(void *opaque, hwaddr addr, unsigned size) 43354f59d78SCédric Le Goater { 43454f59d78SCédric Le Goater return pnv_psi_reg_read(opaque, addr >> 3, false); 43554f59d78SCédric Le Goater } 43654f59d78SCédric Le Goater 43754f59d78SCédric Le Goater static void pnv_psi_xscom_write(void *opaque, hwaddr addr, 43854f59d78SCédric Le Goater uint64_t val, unsigned size) 43954f59d78SCédric Le Goater { 44054f59d78SCédric Le Goater pnv_psi_reg_write(opaque, addr >> 3, val, false); 44154f59d78SCédric Le Goater } 44254f59d78SCédric Le Goater 44354f59d78SCédric Le Goater static const MemoryRegionOps pnv_psi_xscom_ops = { 44454f59d78SCédric Le Goater .read = pnv_psi_xscom_read, 44554f59d78SCédric Le Goater .write = pnv_psi_xscom_write, 44654f59d78SCédric Le Goater .endianness = DEVICE_BIG_ENDIAN, 44754f59d78SCédric Le Goater .valid = { 44854f59d78SCédric Le Goater .min_access_size = 8, 44954f59d78SCédric Le Goater .max_access_size = 8, 45054f59d78SCédric Le Goater }, 45154f59d78SCédric Le Goater .impl = { 45254f59d78SCédric Le Goater .min_access_size = 8, 45354f59d78SCédric Le Goater .max_access_size = 8, 45454f59d78SCédric Le Goater } 45554f59d78SCédric Le Goater }; 45654f59d78SCédric Le Goater 457f7eb6a0aSCédric Le Goater static void pnv_psi_reset(void *dev) 458f7eb6a0aSCédric Le Goater { 459f7eb6a0aSCédric Le Goater PnvPsi *psi = PNV_PSI(dev); 460f7eb6a0aSCédric Le Goater 461f7eb6a0aSCédric Le Goater memset(psi->regs, 0x0, sizeof(psi->regs)); 462f7eb6a0aSCédric Le Goater 463f7eb6a0aSCédric Le Goater psi->regs[PSIHB_XSCOM_BAR] = psi->bar | PSIHB_BAR_EN; 464f7eb6a0aSCédric Le Goater } 465f7eb6a0aSCédric Le Goater 466ae856055SCédric Le Goater static void pnv_psi_power8_instance_init(Object *obj) 46754f59d78SCédric Le Goater { 468ae856055SCédric Le Goater Pnv8Psi *psi8 = PNV8_PSI(obj); 46954f59d78SCédric Le Goater 470ae856055SCédric Le Goater object_initialize_child(obj, "ics-psi", &psi8->ics, sizeof(psi8->ics), 471f6d4dca8SThomas Huth TYPE_ICS_SIMPLE, &error_abort, NULL); 47254f59d78SCédric Le Goater } 47354f59d78SCédric Le Goater 47454f59d78SCédric Le Goater static const uint8_t irq_to_xivr[] = { 47554f59d78SCédric Le Goater PSIHB_XSCOM_XIVR_FSP, 47654f59d78SCédric Le Goater PSIHB_XSCOM_XIVR_OCC, 47754f59d78SCédric Le Goater PSIHB_XSCOM_XIVR_FSI, 47854f59d78SCédric Le Goater PSIHB_XSCOM_XIVR_LPCI2C, 47954f59d78SCédric Le Goater PSIHB_XSCOM_XIVR_LOCERR, 48054f59d78SCédric Le Goater PSIHB_XSCOM_XIVR_EXT, 48154f59d78SCédric Le Goater }; 48254f59d78SCédric Le Goater 483ae856055SCédric Le Goater static void pnv_psi_power8_realize(DeviceState *dev, Error **errp) 48454f59d78SCédric Le Goater { 48554f59d78SCédric Le Goater PnvPsi *psi = PNV_PSI(dev); 486ae856055SCédric Le Goater ICSState *ics = &PNV8_PSI(psi)->ics; 48754f59d78SCédric Le Goater Object *obj; 48854f59d78SCédric Le Goater Error *err = NULL; 48954f59d78SCédric Le Goater unsigned int i; 49054f59d78SCédric Le Goater 49154f59d78SCédric Le Goater obj = object_property_get_link(OBJECT(dev), "xics", &err); 49254f59d78SCédric Le Goater if (!obj) { 49354f59d78SCédric Le Goater error_setg(errp, "%s: required link 'xics' not found: %s", 49454f59d78SCédric Le Goater __func__, error_get_pretty(err)); 49554f59d78SCédric Le Goater return; 49654f59d78SCédric Le Goater } 49754f59d78SCédric Le Goater 49854f59d78SCédric Le Goater /* Create PSI interrupt control source */ 499ad265631SGreg Kurz object_property_add_const_link(OBJECT(ics), ICS_PROP_XICS, obj, 500ad265631SGreg Kurz &error_abort); 50154f59d78SCédric Le Goater object_property_set_int(OBJECT(ics), PSI_NUM_INTERRUPTS, "nr-irqs", &err); 50254f59d78SCédric Le Goater if (err) { 50354f59d78SCédric Le Goater error_propagate(errp, err); 50454f59d78SCédric Le Goater return; 50554f59d78SCédric Le Goater } 50654f59d78SCédric Le Goater object_property_set_bool(OBJECT(ics), true, "realized", &err); 50754f59d78SCédric Le Goater if (err) { 50854f59d78SCédric Le Goater error_propagate(errp, err); 50954f59d78SCédric Le Goater return; 51054f59d78SCédric Le Goater } 51154f59d78SCédric Le Goater 51254f59d78SCédric Le Goater for (i = 0; i < ics->nr_irqs; i++) { 51354f59d78SCédric Le Goater ics_set_irq_type(ics, i, true); 51454f59d78SCédric Le Goater } 51554f59d78SCédric Le Goater 516f8df9003SCédric Le Goater psi->qirqs = qemu_allocate_irqs(ics_simple_set_irq, ics, ics->nr_irqs); 517f8df9003SCédric Le Goater 51854f59d78SCédric Le Goater /* XSCOM region for PSI registers */ 51954f59d78SCédric Le Goater pnv_xscom_region_init(&psi->xscom_regs, OBJECT(dev), &pnv_psi_xscom_ops, 52054f59d78SCédric Le Goater psi, "xscom-psi", PNV_XSCOM_PSIHB_SIZE); 52154f59d78SCédric Le Goater 52254f59d78SCédric Le Goater /* Initialize MMIO region */ 52354f59d78SCédric Le Goater memory_region_init_io(&psi->regs_mr, OBJECT(dev), &psi_mmio_ops, psi, 52454f59d78SCédric Le Goater "psihb", PNV_PSIHB_SIZE); 52554f59d78SCédric Le Goater 52654f59d78SCédric Le Goater /* Default BAR for MMIO region */ 52754f59d78SCédric Le Goater pnv_psi_set_bar(psi, psi->bar | PSIHB_BAR_EN); 52854f59d78SCédric Le Goater 52954f59d78SCédric Le Goater /* Default sources in XIVR */ 53054f59d78SCédric Le Goater for (i = 0; i < PSI_NUM_INTERRUPTS; i++) { 53154f59d78SCédric Le Goater uint8_t xivr = irq_to_xivr[i]; 53254f59d78SCédric Le Goater psi->regs[xivr] = PSIHB_XIVR_PRIO_MSK | 53354f59d78SCédric Le Goater ((uint64_t) i << PSIHB_XIVR_SRC_SH); 53454f59d78SCédric Le Goater } 535f7eb6a0aSCédric Le Goater 536f7eb6a0aSCédric Le Goater qemu_register_reset(pnv_psi_reset, dev); 53754f59d78SCédric Le Goater } 53854f59d78SCédric Le Goater 539ae856055SCédric Le Goater static const char compat_p8[] = "ibm,power8-psihb-x\0ibm,psihb-x"; 540c38536bcSCédric Le Goater static const char compat_p9[] = "ibm,power9-psihb-x\0ibm,psihb-x"; 541ae856055SCédric Le Goater 542b168a138SCédric Le Goater static int pnv_psi_dt_xscom(PnvXScomInterface *dev, void *fdt, int xscom_offset) 54354f59d78SCédric Le Goater { 544ae856055SCédric Le Goater PnvPsiClass *ppc = PNV_PSI_GET_CLASS(dev); 54554f59d78SCédric Le Goater char *name; 54654f59d78SCédric Le Goater int offset; 54754f59d78SCédric Le Goater uint32_t reg[] = { 548ae856055SCédric Le Goater cpu_to_be32(ppc->xscom_pcba), 549ae856055SCédric Le Goater cpu_to_be32(ppc->xscom_size) 55054f59d78SCédric Le Goater }; 55154f59d78SCédric Le Goater 552ae856055SCédric Le Goater name = g_strdup_printf("psihb@%x", ppc->xscom_pcba); 55354f59d78SCédric Le Goater offset = fdt_add_subnode(fdt, xscom_offset, name); 55454f59d78SCédric Le Goater _FDT(offset); 55554f59d78SCédric Le Goater g_free(name); 55654f59d78SCédric Le Goater 557ae856055SCédric Le Goater _FDT(fdt_setprop(fdt, offset, "reg", reg, sizeof(reg))); 558ae856055SCédric Le Goater _FDT(fdt_setprop_cell(fdt, offset, "#address-cells", 2)); 559ae856055SCédric Le Goater _FDT(fdt_setprop_cell(fdt, offset, "#size-cells", 1)); 560c38536bcSCédric Le Goater if (ppc->chip_type == PNV_CHIP_POWER9) { 561c38536bcSCédric Le Goater _FDT(fdt_setprop(fdt, offset, "compatible", compat_p9, 562c38536bcSCédric Le Goater sizeof(compat_p9))); 563c38536bcSCédric Le Goater } else { 564ae856055SCédric Le Goater _FDT(fdt_setprop(fdt, offset, "compatible", compat_p8, 565ae856055SCédric Le Goater sizeof(compat_p8))); 566c38536bcSCédric Le Goater } 56754f59d78SCédric Le Goater return 0; 56854f59d78SCédric Le Goater } 56954f59d78SCédric Le Goater 57054f59d78SCédric Le Goater static Property pnv_psi_properties[] = { 57154f59d78SCédric Le Goater DEFINE_PROP_UINT64("bar", PnvPsi, bar, 0), 57254f59d78SCédric Le Goater DEFINE_PROP_UINT64("fsp-bar", PnvPsi, fsp_bar, 0), 57354f59d78SCédric Le Goater DEFINE_PROP_END_OF_LIST(), 57454f59d78SCédric Le Goater }; 57554f59d78SCédric Le Goater 576ae856055SCédric Le Goater static void pnv_psi_power8_class_init(ObjectClass *klass, void *data) 577ae856055SCédric Le Goater { 578ae856055SCédric Le Goater DeviceClass *dc = DEVICE_CLASS(klass); 579ae856055SCédric Le Goater PnvPsiClass *ppc = PNV_PSI_CLASS(klass); 580ae856055SCédric Le Goater 581ae856055SCédric Le Goater dc->desc = "PowerNV PSI Controller POWER8"; 582ae856055SCédric Le Goater dc->realize = pnv_psi_power8_realize; 583ae856055SCédric Le Goater 584ae856055SCédric Le Goater ppc->chip_type = PNV_CHIP_POWER8; 585ae856055SCédric Le Goater ppc->xscom_pcba = PNV_XSCOM_PSIHB_BASE; 586ae856055SCédric Le Goater ppc->xscom_size = PNV_XSCOM_PSIHB_SIZE; 587ae856055SCédric Le Goater ppc->bar_mask = PSIHB_BAR_MASK; 588ae856055SCédric Le Goater ppc->irq_set = pnv_psi_power8_irq_set; 589ae856055SCédric Le Goater } 590ae856055SCédric Le Goater 591ae856055SCédric Le Goater static const TypeInfo pnv_psi_power8_info = { 592ae856055SCédric Le Goater .name = TYPE_PNV8_PSI, 593ae856055SCédric Le Goater .parent = TYPE_PNV_PSI, 594ae856055SCédric Le Goater .instance_size = sizeof(Pnv8Psi), 595ae856055SCédric Le Goater .instance_init = pnv_psi_power8_instance_init, 596ae856055SCédric Le Goater .class_init = pnv_psi_power8_class_init, 597ae856055SCédric Le Goater }; 598ae856055SCédric Le Goater 599c38536bcSCédric Le Goater 600c38536bcSCédric Le Goater /* Common registers */ 601c38536bcSCédric Le Goater 602c38536bcSCédric Le Goater #define PSIHB9_CR 0x20 603c38536bcSCédric Le Goater #define PSIHB9_SEMR 0x28 604c38536bcSCédric Le Goater 605c38536bcSCédric Le Goater /* P9 registers */ 606c38536bcSCédric Le Goater 607c38536bcSCédric Le Goater #define PSIHB9_INTERRUPT_CONTROL 0x58 608c38536bcSCédric Le Goater #define PSIHB9_IRQ_METHOD PPC_BIT(0) 609c38536bcSCédric Le Goater #define PSIHB9_IRQ_RESET PPC_BIT(1) 610c38536bcSCédric Le Goater #define PSIHB9_ESB_CI_BASE 0x60 611c38536bcSCédric Le Goater #define PSIHB9_ESB_CI_VALID 1 612c38536bcSCédric Le Goater #define PSIHB9_ESB_NOTIF_ADDR 0x68 613c38536bcSCédric Le Goater #define PSIHB9_ESB_NOTIF_VALID 1 614c38536bcSCédric Le Goater #define PSIHB9_IVT_OFFSET 0x70 615c38536bcSCédric Le Goater #define PSIHB9_IVT_OFF_SHIFT 32 616c38536bcSCédric Le Goater 617c38536bcSCédric Le Goater #define PSIHB9_IRQ_LEVEL 0x78 /* assertion */ 618c38536bcSCédric Le Goater #define PSIHB9_IRQ_LEVEL_PSI PPC_BIT(0) 619c38536bcSCédric Le Goater #define PSIHB9_IRQ_LEVEL_OCC PPC_BIT(1) 620c38536bcSCédric Le Goater #define PSIHB9_IRQ_LEVEL_FSI PPC_BIT(2) 621c38536bcSCédric Le Goater #define PSIHB9_IRQ_LEVEL_LPCHC PPC_BIT(3) 622c38536bcSCédric Le Goater #define PSIHB9_IRQ_LEVEL_LOCAL_ERR PPC_BIT(4) 623c38536bcSCédric Le Goater #define PSIHB9_IRQ_LEVEL_GLOBAL_ERR PPC_BIT(5) 624c38536bcSCédric Le Goater #define PSIHB9_IRQ_LEVEL_TPM PPC_BIT(6) 625c38536bcSCédric Le Goater #define PSIHB9_IRQ_LEVEL_LPC_SIRQ1 PPC_BIT(7) 626c38536bcSCédric Le Goater #define PSIHB9_IRQ_LEVEL_LPC_SIRQ2 PPC_BIT(8) 627c38536bcSCédric Le Goater #define PSIHB9_IRQ_LEVEL_LPC_SIRQ3 PPC_BIT(9) 628c38536bcSCédric Le Goater #define PSIHB9_IRQ_LEVEL_LPC_SIRQ4 PPC_BIT(10) 629c38536bcSCédric Le Goater #define PSIHB9_IRQ_LEVEL_SBE_I2C PPC_BIT(11) 630c38536bcSCédric Le Goater #define PSIHB9_IRQ_LEVEL_DIO PPC_BIT(12) 631c38536bcSCédric Le Goater #define PSIHB9_IRQ_LEVEL_PSU PPC_BIT(13) 632c38536bcSCédric Le Goater #define PSIHB9_IRQ_LEVEL_I2C_C PPC_BIT(14) 633c38536bcSCédric Le Goater #define PSIHB9_IRQ_LEVEL_I2C_D PPC_BIT(15) 634c38536bcSCédric Le Goater #define PSIHB9_IRQ_LEVEL_I2C_E PPC_BIT(16) 635c38536bcSCédric Le Goater #define PSIHB9_IRQ_LEVEL_SBE PPC_BIT(19) 636c38536bcSCédric Le Goater 637c38536bcSCédric Le Goater #define PSIHB9_IRQ_STAT 0x80 /* P bit */ 638c38536bcSCédric Le Goater #define PSIHB9_IRQ_STAT_PSI PPC_BIT(0) 639c38536bcSCédric Le Goater #define PSIHB9_IRQ_STAT_OCC PPC_BIT(1) 640c38536bcSCédric Le Goater #define PSIHB9_IRQ_STAT_FSI PPC_BIT(2) 641c38536bcSCédric Le Goater #define PSIHB9_IRQ_STAT_LPCHC PPC_BIT(3) 642c38536bcSCédric Le Goater #define PSIHB9_IRQ_STAT_LOCAL_ERR PPC_BIT(4) 643c38536bcSCédric Le Goater #define PSIHB9_IRQ_STAT_GLOBAL_ERR PPC_BIT(5) 644c38536bcSCédric Le Goater #define PSIHB9_IRQ_STAT_TPM PPC_BIT(6) 645c38536bcSCédric Le Goater #define PSIHB9_IRQ_STAT_LPC_SIRQ1 PPC_BIT(7) 646c38536bcSCédric Le Goater #define PSIHB9_IRQ_STAT_LPC_SIRQ2 PPC_BIT(8) 647c38536bcSCédric Le Goater #define PSIHB9_IRQ_STAT_LPC_SIRQ3 PPC_BIT(9) 648c38536bcSCédric Le Goater #define PSIHB9_IRQ_STAT_LPC_SIRQ4 PPC_BIT(10) 649c38536bcSCédric Le Goater #define PSIHB9_IRQ_STAT_SBE_I2C PPC_BIT(11) 650c38536bcSCédric Le Goater #define PSIHB9_IRQ_STAT_DIO PPC_BIT(12) 651c38536bcSCédric Le Goater #define PSIHB9_IRQ_STAT_PSU PPC_BIT(13) 652c38536bcSCédric Le Goater 653c38536bcSCédric Le Goater static void pnv_psi_notify(XiveNotifier *xf, uint32_t srcno) 654c38536bcSCédric Le Goater { 655c38536bcSCédric Le Goater PnvPsi *psi = PNV_PSI(xf); 656c38536bcSCédric Le Goater uint64_t notif_port = psi->regs[PSIHB_REG(PSIHB9_ESB_NOTIF_ADDR)]; 657c38536bcSCédric Le Goater bool valid = notif_port & PSIHB9_ESB_NOTIF_VALID; 658c38536bcSCédric Le Goater uint64_t notify_addr = notif_port & ~PSIHB9_ESB_NOTIF_VALID; 659c38536bcSCédric Le Goater 660c38536bcSCédric Le Goater uint32_t offset = 661c38536bcSCédric Le Goater (psi->regs[PSIHB_REG(PSIHB9_IVT_OFFSET)] >> PSIHB9_IVT_OFF_SHIFT); 662c38536bcSCédric Le Goater uint64_t lisn = cpu_to_be64(offset + srcno); 663c38536bcSCédric Le Goater 664c38536bcSCédric Le Goater if (valid) { 665c38536bcSCédric Le Goater cpu_physical_memory_write(notify_addr, &lisn, sizeof(lisn)); 666c38536bcSCédric Le Goater } 667c38536bcSCédric Le Goater } 668c38536bcSCédric Le Goater 669c38536bcSCédric Le Goater static uint64_t pnv_psi_p9_mmio_read(void *opaque, hwaddr addr, unsigned size) 670c38536bcSCédric Le Goater { 671c38536bcSCédric Le Goater PnvPsi *psi = PNV_PSI(opaque); 672c38536bcSCédric Le Goater uint32_t reg = PSIHB_REG(addr); 673c38536bcSCédric Le Goater uint64_t val = -1; 674c38536bcSCédric Le Goater 675c38536bcSCédric Le Goater switch (addr) { 676c38536bcSCédric Le Goater case PSIHB9_CR: 677c38536bcSCédric Le Goater case PSIHB9_SEMR: 678c38536bcSCédric Le Goater /* FSP stuff */ 679c38536bcSCédric Le Goater case PSIHB9_INTERRUPT_CONTROL: 680c38536bcSCédric Le Goater case PSIHB9_ESB_CI_BASE: 681c38536bcSCédric Le Goater case PSIHB9_ESB_NOTIF_ADDR: 682c38536bcSCédric Le Goater case PSIHB9_IVT_OFFSET: 683c38536bcSCédric Le Goater val = psi->regs[reg]; 684c38536bcSCédric Le Goater break; 685c38536bcSCédric Le Goater default: 686c38536bcSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "PSI: read at 0x%" PRIx64 "\n", addr); 687c38536bcSCédric Le Goater } 688c38536bcSCédric Le Goater 689c38536bcSCédric Le Goater return val; 690c38536bcSCédric Le Goater } 691c38536bcSCédric Le Goater 692c38536bcSCédric Le Goater static void pnv_psi_p9_mmio_write(void *opaque, hwaddr addr, 693c38536bcSCédric Le Goater uint64_t val, unsigned size) 694c38536bcSCédric Le Goater { 695c38536bcSCédric Le Goater PnvPsi *psi = PNV_PSI(opaque); 696c38536bcSCédric Le Goater Pnv9Psi *psi9 = PNV9_PSI(psi); 697c38536bcSCédric Le Goater uint32_t reg = PSIHB_REG(addr); 698c38536bcSCédric Le Goater MemoryRegion *sysmem = get_system_memory(); 699c38536bcSCédric Le Goater 700c38536bcSCédric Le Goater switch (addr) { 701c38536bcSCédric Le Goater case PSIHB9_CR: 702c38536bcSCédric Le Goater case PSIHB9_SEMR: 703c38536bcSCédric Le Goater /* FSP stuff */ 704c38536bcSCédric Le Goater break; 705c38536bcSCédric Le Goater case PSIHB9_INTERRUPT_CONTROL: 706c38536bcSCédric Le Goater if (val & PSIHB9_IRQ_RESET) { 707c38536bcSCédric Le Goater device_reset(DEVICE(&psi9->source)); 708c38536bcSCédric Le Goater } 709c38536bcSCédric Le Goater psi->regs[reg] = val; 710c38536bcSCédric Le Goater break; 711c38536bcSCédric Le Goater 712c38536bcSCédric Le Goater case PSIHB9_ESB_CI_BASE: 713c38536bcSCédric Le Goater if (!(val & PSIHB9_ESB_CI_VALID)) { 714c38536bcSCédric Le Goater if (psi->regs[reg] & PSIHB9_ESB_CI_VALID) { 715c38536bcSCédric Le Goater memory_region_del_subregion(sysmem, &psi9->source.esb_mmio); 716c38536bcSCédric Le Goater } 717c38536bcSCédric Le Goater } else { 718c38536bcSCédric Le Goater if (!(psi->regs[reg] & PSIHB9_ESB_CI_VALID)) { 719c38536bcSCédric Le Goater memory_region_add_subregion(sysmem, 720c38536bcSCédric Le Goater val & ~PSIHB9_ESB_CI_VALID, 721c38536bcSCédric Le Goater &psi9->source.esb_mmio); 722c38536bcSCédric Le Goater } 723c38536bcSCédric Le Goater } 724c38536bcSCédric Le Goater psi->regs[reg] = val; 725c38536bcSCédric Le Goater break; 726c38536bcSCédric Le Goater 727c38536bcSCédric Le Goater case PSIHB9_ESB_NOTIF_ADDR: 728c38536bcSCédric Le Goater psi->regs[reg] = val; 729c38536bcSCédric Le Goater break; 730c38536bcSCédric Le Goater case PSIHB9_IVT_OFFSET: 731c38536bcSCédric Le Goater psi->regs[reg] = val; 732c38536bcSCédric Le Goater break; 733c38536bcSCédric Le Goater default: 734c38536bcSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "PSI: write at 0x%" PRIx64 "\n", addr); 735c38536bcSCédric Le Goater } 736c38536bcSCédric Le Goater } 737c38536bcSCédric Le Goater 738c38536bcSCédric Le Goater static const MemoryRegionOps pnv_psi_p9_mmio_ops = { 739c38536bcSCédric Le Goater .read = pnv_psi_p9_mmio_read, 740c38536bcSCédric Le Goater .write = pnv_psi_p9_mmio_write, 741c38536bcSCédric Le Goater .endianness = DEVICE_BIG_ENDIAN, 742c38536bcSCédric Le Goater .valid = { 743c38536bcSCédric Le Goater .min_access_size = 8, 744c38536bcSCédric Le Goater .max_access_size = 8, 745c38536bcSCédric Le Goater }, 746c38536bcSCédric Le Goater .impl = { 747c38536bcSCédric Le Goater .min_access_size = 8, 748c38536bcSCédric Le Goater .max_access_size = 8, 749c38536bcSCédric Le Goater }, 750c38536bcSCédric Le Goater }; 751c38536bcSCédric Le Goater 752c38536bcSCédric Le Goater static uint64_t pnv_psi_p9_xscom_read(void *opaque, hwaddr addr, unsigned size) 753c38536bcSCédric Le Goater { 754c38536bcSCédric Le Goater /* No read are expected */ 755c38536bcSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "PSI: xscom read at 0x%" PRIx64 "\n", addr); 756c38536bcSCédric Le Goater return -1; 757c38536bcSCédric Le Goater } 758c38536bcSCédric Le Goater 759c38536bcSCédric Le Goater static void pnv_psi_p9_xscom_write(void *opaque, hwaddr addr, 760c38536bcSCédric Le Goater uint64_t val, unsigned size) 761c38536bcSCédric Le Goater { 762c38536bcSCédric Le Goater PnvPsi *psi = PNV_PSI(opaque); 763c38536bcSCédric Le Goater 764c38536bcSCédric Le Goater /* XSCOM is only used to set the PSIHB MMIO region */ 765c38536bcSCédric Le Goater switch (addr >> 3) { 766c38536bcSCédric Le Goater case PSIHB_XSCOM_BAR: 767c38536bcSCédric Le Goater pnv_psi_set_bar(psi, val); 768c38536bcSCédric Le Goater break; 769c38536bcSCédric Le Goater default: 770c38536bcSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "PSI: xscom write at 0x%" PRIx64 "\n", 771c38536bcSCédric Le Goater addr); 772c38536bcSCédric Le Goater } 773c38536bcSCédric Le Goater } 774c38536bcSCédric Le Goater 775c38536bcSCédric Le Goater static const MemoryRegionOps pnv_psi_p9_xscom_ops = { 776c38536bcSCédric Le Goater .read = pnv_psi_p9_xscom_read, 777c38536bcSCédric Le Goater .write = pnv_psi_p9_xscom_write, 778c38536bcSCédric Le Goater .endianness = DEVICE_BIG_ENDIAN, 779c38536bcSCédric Le Goater .valid = { 780c38536bcSCédric Le Goater .min_access_size = 8, 781c38536bcSCédric Le Goater .max_access_size = 8, 782c38536bcSCédric Le Goater }, 783c38536bcSCédric Le Goater .impl = { 784c38536bcSCédric Le Goater .min_access_size = 8, 785c38536bcSCédric Le Goater .max_access_size = 8, 786c38536bcSCédric Le Goater } 787c38536bcSCédric Le Goater }; 788c38536bcSCédric Le Goater 789c38536bcSCédric Le Goater static void pnv_psi_power9_irq_set(PnvPsi *psi, int irq, bool state) 790c38536bcSCédric Le Goater { 791f3e971acSGreg Kurz uint64_t irq_method = psi->regs[PSIHB_REG(PSIHB9_INTERRUPT_CONTROL)]; 792c38536bcSCédric Le Goater 793c38536bcSCédric Le Goater if (irq > PSIHB9_NUM_IRQS) { 794c38536bcSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "PSI: Unsupported irq %d\n", irq); 795c38536bcSCédric Le Goater return; 796c38536bcSCédric Le Goater } 797c38536bcSCédric Le Goater 798c38536bcSCédric Le Goater if (irq_method & PSIHB9_IRQ_METHOD) { 799c38536bcSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "PSI: LSI IRQ method no supported\n"); 800c38536bcSCédric Le Goater return; 801c38536bcSCédric Le Goater } 802c38536bcSCédric Le Goater 803c38536bcSCédric Le Goater /* Update LSI levels */ 804c38536bcSCédric Le Goater if (state) { 805c38536bcSCédric Le Goater psi->regs[PSIHB_REG(PSIHB9_IRQ_LEVEL)] |= PPC_BIT(irq); 806c38536bcSCédric Le Goater } else { 807c38536bcSCédric Le Goater psi->regs[PSIHB_REG(PSIHB9_IRQ_LEVEL)] &= ~PPC_BIT(irq); 808c38536bcSCédric Le Goater } 809c38536bcSCédric Le Goater 810c38536bcSCédric Le Goater qemu_set_irq(psi->qirqs[irq], state); 811c38536bcSCédric Le Goater } 812c38536bcSCédric Le Goater 813c38536bcSCédric Le Goater static void pnv_psi_power9_reset(void *dev) 814c38536bcSCédric Le Goater { 815c38536bcSCédric Le Goater Pnv9Psi *psi = PNV9_PSI(dev); 816c38536bcSCédric Le Goater 817c38536bcSCédric Le Goater pnv_psi_reset(dev); 818c38536bcSCédric Le Goater 819c38536bcSCédric Le Goater if (memory_region_is_mapped(&psi->source.esb_mmio)) { 820c38536bcSCédric Le Goater memory_region_del_subregion(get_system_memory(), &psi->source.esb_mmio); 821c38536bcSCédric Le Goater } 822c38536bcSCédric Le Goater } 823c38536bcSCédric Le Goater 824c38536bcSCédric Le Goater static void pnv_psi_power9_instance_init(Object *obj) 825c38536bcSCédric Le Goater { 826c38536bcSCédric Le Goater Pnv9Psi *psi = PNV9_PSI(obj); 827c38536bcSCédric Le Goater 828c38536bcSCédric Le Goater object_initialize_child(obj, "source", &psi->source, sizeof(psi->source), 829c38536bcSCédric Le Goater TYPE_XIVE_SOURCE, &error_abort, NULL); 830c38536bcSCédric Le Goater } 831c38536bcSCédric Le Goater 832c38536bcSCédric Le Goater static void pnv_psi_power9_realize(DeviceState *dev, Error **errp) 833c38536bcSCédric Le Goater { 834c38536bcSCédric Le Goater PnvPsi *psi = PNV_PSI(dev); 835c38536bcSCédric Le Goater XiveSource *xsrc = &PNV9_PSI(psi)->source; 836c38536bcSCédric Le Goater Error *local_err = NULL; 837c38536bcSCédric Le Goater int i; 838c38536bcSCédric Le Goater 839c38536bcSCédric Le Goater /* This is the only device with 4k ESB pages */ 840c38536bcSCédric Le Goater object_property_set_int(OBJECT(xsrc), XIVE_ESB_4K, "shift", 841c38536bcSCédric Le Goater &error_fatal); 842c38536bcSCédric Le Goater object_property_set_int(OBJECT(xsrc), PSIHB9_NUM_IRQS, "nr-irqs", 843c38536bcSCédric Le Goater &error_fatal); 844c38536bcSCédric Le Goater object_property_add_const_link(OBJECT(xsrc), "xive", OBJECT(psi), 845c38536bcSCédric Le Goater &error_fatal); 846c38536bcSCédric Le Goater object_property_set_bool(OBJECT(xsrc), true, "realized", &local_err); 847c38536bcSCédric Le Goater if (local_err) { 848c38536bcSCédric Le Goater error_propagate(errp, local_err); 849c38536bcSCédric Le Goater return; 850c38536bcSCédric Le Goater } 851c38536bcSCédric Le Goater 852c38536bcSCédric Le Goater for (i = 0; i < xsrc->nr_irqs; i++) { 853c38536bcSCédric Le Goater xive_source_irq_set_lsi(xsrc, i); 854c38536bcSCédric Le Goater } 855c38536bcSCédric Le Goater 856c38536bcSCédric Le Goater psi->qirqs = qemu_allocate_irqs(xive_source_set_irq, xsrc, xsrc->nr_irqs); 857c38536bcSCédric Le Goater 858c38536bcSCédric Le Goater /* XSCOM region for PSI registers */ 859c38536bcSCédric Le Goater pnv_xscom_region_init(&psi->xscom_regs, OBJECT(dev), &pnv_psi_p9_xscom_ops, 860c38536bcSCédric Le Goater psi, "xscom-psi", PNV9_XSCOM_PSIHB_SIZE); 861c38536bcSCédric Le Goater 862c38536bcSCédric Le Goater /* MMIO region for PSI registers */ 863c38536bcSCédric Le Goater memory_region_init_io(&psi->regs_mr, OBJECT(dev), &pnv_psi_p9_mmio_ops, psi, 864c38536bcSCédric Le Goater "psihb", PNV9_PSIHB_SIZE); 865c38536bcSCédric Le Goater 866c38536bcSCédric Le Goater pnv_psi_set_bar(psi, psi->bar | PSIHB_BAR_EN); 867c38536bcSCédric Le Goater 868c38536bcSCédric Le Goater qemu_register_reset(pnv_psi_power9_reset, dev); 869c38536bcSCédric Le Goater } 870c38536bcSCédric Le Goater 871c38536bcSCédric Le Goater static void pnv_psi_power9_class_init(ObjectClass *klass, void *data) 872c38536bcSCédric Le Goater { 873c38536bcSCédric Le Goater DeviceClass *dc = DEVICE_CLASS(klass); 874c38536bcSCédric Le Goater PnvPsiClass *ppc = PNV_PSI_CLASS(klass); 875c38536bcSCédric Le Goater XiveNotifierClass *xfc = XIVE_NOTIFIER_CLASS(klass); 876c38536bcSCédric Le Goater 877c38536bcSCédric Le Goater dc->desc = "PowerNV PSI Controller POWER9"; 878c38536bcSCédric Le Goater dc->realize = pnv_psi_power9_realize; 879c38536bcSCédric Le Goater 880c38536bcSCédric Le Goater ppc->chip_type = PNV_CHIP_POWER9; 881c38536bcSCédric Le Goater ppc->xscom_pcba = PNV9_XSCOM_PSIHB_BASE; 882c38536bcSCédric Le Goater ppc->xscom_size = PNV9_XSCOM_PSIHB_SIZE; 883c38536bcSCédric Le Goater ppc->bar_mask = PSIHB9_BAR_MASK; 884c38536bcSCédric Le Goater ppc->irq_set = pnv_psi_power9_irq_set; 885c38536bcSCédric Le Goater 886c38536bcSCédric Le Goater xfc->notify = pnv_psi_notify; 887c38536bcSCédric Le Goater } 888c38536bcSCédric Le Goater 889c38536bcSCédric Le Goater static const TypeInfo pnv_psi_power9_info = { 890c38536bcSCédric Le Goater .name = TYPE_PNV9_PSI, 891c38536bcSCédric Le Goater .parent = TYPE_PNV_PSI, 892c38536bcSCédric Le Goater .instance_size = sizeof(Pnv9Psi), 893c38536bcSCédric Le Goater .instance_init = pnv_psi_power9_instance_init, 894c38536bcSCédric Le Goater .class_init = pnv_psi_power9_class_init, 895c38536bcSCédric Le Goater .interfaces = (InterfaceInfo[]) { 896c38536bcSCédric Le Goater { TYPE_XIVE_NOTIFIER }, 897c38536bcSCédric Le Goater { }, 898c38536bcSCédric Le Goater }, 899c38536bcSCédric Le Goater }; 900c38536bcSCédric Le Goater 90154f59d78SCédric Le Goater static void pnv_psi_class_init(ObjectClass *klass, void *data) 90254f59d78SCédric Le Goater { 90354f59d78SCédric Le Goater DeviceClass *dc = DEVICE_CLASS(klass); 90454f59d78SCédric Le Goater PnvXScomInterfaceClass *xdc = PNV_XSCOM_INTERFACE_CLASS(klass); 90554f59d78SCédric Le Goater 906b168a138SCédric Le Goater xdc->dt_xscom = pnv_psi_dt_xscom; 90754f59d78SCédric Le Goater 908ae856055SCédric Le Goater dc->desc = "PowerNV PSI Controller"; 90954f59d78SCédric Le Goater dc->props = pnv_psi_properties; 91054f59d78SCédric Le Goater } 91154f59d78SCédric Le Goater 91254f59d78SCédric Le Goater static const TypeInfo pnv_psi_info = { 91354f59d78SCédric Le Goater .name = TYPE_PNV_PSI, 91454f59d78SCédric Le Goater .parent = TYPE_SYS_BUS_DEVICE, 91554f59d78SCédric Le Goater .instance_size = sizeof(PnvPsi), 91654f59d78SCédric Le Goater .class_init = pnv_psi_class_init, 917ae856055SCédric Le Goater .class_size = sizeof(PnvPsiClass), 918ae856055SCédric Le Goater .abstract = true, 91954f59d78SCédric Le Goater .interfaces = (InterfaceInfo[]) { 92054f59d78SCédric Le Goater { TYPE_PNV_XSCOM_INTERFACE }, 92154f59d78SCédric Le Goater { } 92254f59d78SCédric Le Goater } 92354f59d78SCédric Le Goater }; 92454f59d78SCédric Le Goater 92554f59d78SCédric Le Goater static void pnv_psi_register_types(void) 92654f59d78SCédric Le Goater { 92754f59d78SCédric Le Goater type_register_static(&pnv_psi_info); 928ae856055SCédric Le Goater type_register_static(&pnv_psi_power8_info); 929c38536bcSCédric Le Goater type_register_static(&pnv_psi_power9_info); 93054f59d78SCédric Le Goater } 93154f59d78SCédric Le Goater 932ae856055SCédric Le Goater type_init(pnv_psi_register_types); 933c38536bcSCédric Le Goater 934c38536bcSCédric Le Goater void pnv_psi_pic_print_info(Pnv9Psi *psi9, Monitor *mon) 935c38536bcSCédric Le Goater { 936c38536bcSCédric Le Goater PnvPsi *psi = PNV_PSI(psi9); 937c38536bcSCédric Le Goater 938c38536bcSCédric Le Goater uint32_t offset = 939c38536bcSCédric Le Goater (psi->regs[PSIHB_REG(PSIHB9_IVT_OFFSET)] >> PSIHB9_IVT_OFF_SHIFT); 940c38536bcSCédric Le Goater 941c38536bcSCédric Le Goater monitor_printf(mon, "PSIHB Source %08x .. %08x\n", 942c38536bcSCédric Le Goater offset, offset + psi9->source.nr_irqs - 1); 943c38536bcSCédric Le Goater xive_source_pic_print_info(&psi9->source, offset, mon); 944c38536bcSCédric Le Goater } 945