-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathpsp-irq.c
More file actions
263 lines (225 loc) · 8.6 KB
/
Copy pathpsp-irq.c
File metadata and controls
263 lines (225 loc) · 8.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/** @file
* PSP Emulator - Interrupt controller.
*/
/*
* Copyright (C) 2020 Alexander Eichner <alexander.eichner@campus.tu-berlin.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <stdlib.h>
#include <common/types.h>
#include <common/cdefs.h>
#include <common/status.h>
#include <psp/irq.h>
#include <psp-irq.h>
#include <psp-trace.h>
/*********************************************************************************************************************************
* Structures and Typedefs *
*********************************************************************************************************************************/
/**
* Interrupt controller instance data.
*/
typedef struct PSPIRQINT
{
/** The PSP core to forward interrupt requests to. */
PSPCORE hPspCore;
/** I/O manager for the MMIO register interface. */
PSPIOM hIoMgr;
/** The MMIO region handle for the register interface. */
PSPIOMREGIONHANDLE hMmio;
/** Interrupt request groups pending. */
uint32_t cGrpPending;
/** The individual groups. */
uint32_t abmGrpDev[4];
/** Hidden individual groups to trigger an IRQ only on a rising edge. */
uint32_t abmGrpDevLast[4];
} PSPIRQINT;
/** Pointer to the internal interrupt controller instance data. */
typedef PSPIRQINT *PPSPIRQINT;
/*********************************************************************************************************************************
* Internal Functions *
*********************************************************************************************************************************/
/**
* @copydoc{PFNPSPIOMMMIOREAD, IRQ controller read handler}
*/
static void pspIrqMmioRead(PSPADDR offMmio, size_t cbRead, void *pvDst, void *pvUser)
{
PPSPIRQINT pThis = (PPSPIRQINT)pvUser;
if (cbRead != sizeof(uint32_t))
{
PSPEmuTraceEvtAddString(NULL, PSPTRACEEVTSEVERITY_ERROR, PSPTRACEEVTORIGIN_IRQ,
"%s: offMmio=%#x cbRead=%zu -> Unsupported access width\n", __FUNCTION__, offMmio, cbRead);
return;
}
uint32_t *pu32Dst = (uint32_t *)pvDst;
*pu32Dst = 0;
switch (offMmio)
{
case PSP_IRQ_REG_ACK_PRIO0_OFF:
case PSP_IRQ_REG_ACK_PRIO1_OFF:
case PSP_IRQ_REG_ACK_PRIO2_OFF:
case PSP_IRQ_REG_ACK_PRIO3_OFF:
{
uint32_t idPrio = (offMmio - PSP_IRQ_REG_ACK_PRIO0_OFF) / sizeof(uint32_t);
*pu32Dst = pThis->abmGrpDev[idPrio];
break;
}
case PSP_IRQ_REG_PEN_OFF:
{
if (pThis->cGrpPending)
*pu32Dst = PSP_IRQ_REG_PEN_PENDING;
else
*pu32Dst = PSP_IRQ_REG_PEN_NOT_PENDING;
break;
}
case PSP_IRQ_REG_ID_OFF:
{
/*
* How it is decided which interrupt source to return on real hardware is not
* known actually at the moment.
*
* We just start from the lowest priority/group and device ID (highest priority) for now.
*/
for (uint32_t i = 0; i < ELEMENTS(pThis->abmGrpDev); i++)
{
if (pThis->abmGrpDev[i] != 0)
{
/** @todo Optimize. */
uint32_t uPrio = pThis->abmGrpDev[i];
uint32_t idDev = 0;
while (uPrio)
{
if (uPrio & 1)
break; /* Found the device. */
uPrio >>= 1;
idDev++;
}
*pu32Dst = PSP_IRQ_REG_ID_MAKE(i, idDev);
break;
}
}
break;
}
default:
break;
}
}
/**
* @copydoc{PFNPSPIOMMMIOWRITE, IRQ controller write handler}
*/
static void pspIrqMmioWrite(PSPADDR offMmio, size_t cbWrite, const void *pvVal, void *pvUser)
{
PPSPIRQINT pThis = (PPSPIRQINT)pvUser;
if (cbWrite != sizeof(uint32_t))
{
PSPEmuTraceEvtAddString(NULL, PSPTRACEEVTSEVERITY_ERROR, PSPTRACEEVTORIGIN_IRQ,
"%s: offMmio=%#x cbWrite=%zu -> Unsupported access width\n", __FUNCTION__, offMmio, cbWrite);
return;
}
uint32_t uVal = *(uint32_t *)pvVal;
switch (offMmio)
{
case PSP_IRQ_REG_ACK_PRIO0_OFF:
case PSP_IRQ_REG_ACK_PRIO1_OFF:
case PSP_IRQ_REG_ACK_PRIO2_OFF:
case PSP_IRQ_REG_ACK_PRIO3_OFF:
{
uint32_t idPrio = (offMmio - PSP_IRQ_REG_ACK_PRIO0_OFF) / sizeof(uint32_t);
pThis->abmGrpDev[idPrio] &= ~uVal;
if (!pThis->abmGrpDev[idPrio])
pThis->cGrpPending--;
if (!pThis->cGrpPending) /* Reset the interrupt line. */
{
PSPEmuTraceEvtAddString(NULL, PSPTRACEEVTSEVERITY_INFO, PSPTRACEEVTORIGIN_IRQ, "De-Asserting IRQ\n");
PSPEmuCoreIrqSet(pThis->hPspCore, false /*fAssert*/);
}
break;
}
case PSP_IRQ_REG_PEN_OFF:
case PSP_IRQ_REG_ID_OFF:
default:
break; /* Ignore. */
}
}
int PSPIrqCreate(PPSPIRQ phIrq, PSPCORE hPspCore, PSPIOM hIoMgr)
{
int rc = STS_INF_SUCCESS;
PPSPIRQINT pThis = (PPSPIRQINT)calloc(1, sizeof(*pThis));
if (pThis)
{
pThis->hPspCore = hPspCore;
pThis->hIoMgr = hIoMgr;
rc = PSPEmuIoMgrMmioRegister(hIoMgr, PSP_IRQ_MMIO_ADDR_BASE, PSP_IRQ_MMIO_SZ,
pspIrqMmioRead, pspIrqMmioWrite, pThis,
"IRQ Ctrl", &pThis->hMmio);
if (STS_SUCCESS(rc))
{
*phIrq = pThis;
PSPIrqReset(pThis);
return STS_INF_SUCCESS;
}
free(pThis);
}
else
rc = STS_ERR_NO_MEMORY;
return rc;
}
void PSPIrqDestroy(PSPIRQ hIrq)
{
PPSPIRQINT pThis = hIrq;
PSPEmuIoMgrDeregister(pThis->hMmio);
free(pThis);
}
void PSPIrqReset(PSPIRQ hIrq)
{
PPSPIRQINT pThis = hIrq;
pThis->cGrpPending = 0;
for (uint32_t i = 0; i < ELEMENTS(pThis->abmGrpDev); i++)
{
pThis->abmGrpDev[i] = 0;
pThis->abmGrpDevLast[i] = 0;
}
}
int PSPIrqSet(PSPIRQ hIrq, uint32_t uPrioGrp, uint8_t uIrq, bool fAssert)
{
PPSPIRQINT pThis = hIrq;
if ( uPrioGrp < ELEMENTS(pThis->abmGrpDev)
&& uIrq < sizeof(uint32_t) * 8)
{
/** @todo Check somehow whether interrupts are level or rising edge triggered.
* For now we assume that interrupts are triggered on the rising edge as this
* makes the most sense with how the interrupt handlers we've seen process interrupts so far.
*/
if ( !(pThis->abmGrpDevLast[uPrioGrp] & BIT(uIrq))
&& fAssert)
{
if (!pThis->abmGrpDev[uPrioGrp])
pThis->cGrpPending++;
pThis->abmGrpDevLast[uPrioGrp] |= BIT(uIrq);
pThis->abmGrpDev[uPrioGrp] |= BIT(uIrq);
if (pThis->cGrpPending == 1)
{
PSPEmuTraceEvtAddString(NULL, PSPTRACEEVTSEVERITY_INFO, PSPTRACEEVTORIGIN_IRQ,
"Asserting IRQ caused by idPrio=%u idDev=%u\n", uPrioGrp, uIrq);
PSPEmuCoreIrqSet(pThis->hPspCore, true /*fAssert*/);
}
}
else if (!fAssert)
pThis->abmGrpDevLast[uPrioGrp] &= ~BIT(uIrq);
return STS_INF_SUCCESS;
}
return STS_ERR_INVALID_PARAMETER;
}