1 | /* $Id: DevPIC.cpp 93832 2022-02-17 17:45:10Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DevPIC - Intel 8259 Programmable Interrupt Controller (PIC) Device.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | * -------------------------------------------------------------------
|
---|
17 | *
|
---|
18 | * This code is based on:
|
---|
19 | *
|
---|
20 | * QEMU 8259 interrupt controller emulation
|
---|
21 | *
|
---|
22 | * Copyright (c) 2003-2004 Fabrice Bellard
|
---|
23 | *
|
---|
24 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
25 | * of this software and associated documentation files (the "Software"), to deal
|
---|
26 | * in the Software without restriction, including without limitation the rights
|
---|
27 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
28 | * copies of the Software, and to permit persons to whom the Software is
|
---|
29 | * furnished to do so, subject to the following conditions:
|
---|
30 | *
|
---|
31 | * The above copyright notice and this permission notice shall be included in
|
---|
32 | * all copies or substantial portions of the Software.
|
---|
33 | *
|
---|
34 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
35 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
36 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
---|
37 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
38 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
---|
39 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
---|
40 | * THE SOFTWARE.
|
---|
41 | *
|
---|
42 | */
|
---|
43 |
|
---|
44 |
|
---|
45 | /*********************************************************************************************************************************
|
---|
46 | * Header Files *
|
---|
47 | *********************************************************************************************************************************/
|
---|
48 | #define LOG_GROUP LOG_GROUP_DEV_PIC
|
---|
49 | #include <VBox/vmm/pdmdev.h>
|
---|
50 | #include <VBox/log.h>
|
---|
51 | #include <iprt/assert.h>
|
---|
52 | #include <iprt/string.h>
|
---|
53 |
|
---|
54 | #include "VBoxDD.h"
|
---|
55 |
|
---|
56 |
|
---|
57 | /*********************************************************************************************************************************
|
---|
58 | * Defined Constants And Macros *
|
---|
59 | *********************************************************************************************************************************/
|
---|
60 | /** @def PIC_LOCK_RET
|
---|
61 | * Acquires the PDM lock. This is a NOP if locking is disabled. */
|
---|
62 | #define PIC_LOCK_RET(a_pDevIns, a_pThisCC, rcBusy) \
|
---|
63 | do { \
|
---|
64 | int const rcLock = (a_pThisCC)->pPicHlp->pfnLock((a_pDevIns), rcBusy); \
|
---|
65 | if (rcLock == VINF_SUCCESS) \
|
---|
66 | { /* likely */ } \
|
---|
67 | else \
|
---|
68 | return rcLock; \
|
---|
69 | } while (0)
|
---|
70 | /** @def PIC_UNLOCK
|
---|
71 | * Releases the PDM lock. This is a NOP if locking is disabled. */
|
---|
72 | #define PIC_UNLOCK(a_pDevIns, a_pThisCC) \
|
---|
73 | (a_pThisCC)->pPicHlp->pfnUnlock((a_pDevIns))
|
---|
74 |
|
---|
75 |
|
---|
76 | /*********************************************************************************************************************************
|
---|
77 | * Structures and Typedefs *
|
---|
78 | *********************************************************************************************************************************/
|
---|
79 | /**
|
---|
80 | * The instance data of one (1) PIC.
|
---|
81 | */
|
---|
82 | typedef struct PICSTATE
|
---|
83 | {
|
---|
84 | uint8_t last_irr; /**< edge detection */
|
---|
85 | uint8_t irr; /**< interrupt request register */
|
---|
86 | uint8_t imr; /**< interrupt mask register */
|
---|
87 | uint8_t isr; /**< interrupt service register */
|
---|
88 | uint8_t priority_add; /**< highest irq priority */
|
---|
89 | uint8_t irq_base;
|
---|
90 | uint8_t read_reg_select;
|
---|
91 | uint8_t poll;
|
---|
92 | uint8_t special_mask;
|
---|
93 | uint8_t init_state;
|
---|
94 | uint8_t auto_eoi;
|
---|
95 | uint8_t rotate_on_auto_eoi;
|
---|
96 | uint8_t special_fully_nested_mode;
|
---|
97 | uint8_t init4; /**< true if 4 byte init */
|
---|
98 | uint8_t elcr; /**< PIIX edge/trigger selection*/
|
---|
99 | uint8_t elcr_mask;
|
---|
100 | /** The IRQ tags and source IDs for each (tracing purposes). */
|
---|
101 | uint32_t auTags[8];
|
---|
102 | /** The PIC index (0 or 1). */
|
---|
103 | uint8_t idxPic;
|
---|
104 | uint8_t abAlignment0[7]; /**< Alignment padding. */
|
---|
105 | /** The two I/O ports at 0x20 or 0xa0. */
|
---|
106 | IOMIOPORTHANDLE hIoPorts0;
|
---|
107 | /** The ELCR I/O port at 0x4d0 or 0x4d1. */
|
---|
108 | IOMIOPORTHANDLE hIoPorts1;
|
---|
109 | } PICSTATE;
|
---|
110 | AssertCompileMemberAlignment(PICSTATE, hIoPorts0, 8);
|
---|
111 | /** Pointer to the state of one PIC. */
|
---|
112 | typedef PICSTATE *PPICSTATE;
|
---|
113 |
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * The shared PIC device instance data.
|
---|
117 | */
|
---|
118 | typedef struct DEVPIC
|
---|
119 | {
|
---|
120 | /** The two interrupt controllers. */
|
---|
121 | PICSTATE aPics[2];
|
---|
122 | /** Number of release log entries. Used to prevent flooding. */
|
---|
123 | uint32_t cRelLogEntries;
|
---|
124 | uint32_t u32Padding;
|
---|
125 | #ifdef VBOX_WITH_STATISTICS
|
---|
126 | STAMCOUNTER StatSetIrqRZ;
|
---|
127 | STAMCOUNTER StatSetIrqR3;
|
---|
128 | STAMCOUNTER StatClearedActiveIRQ2;
|
---|
129 | STAMCOUNTER StatClearedActiveMasterIRQ;
|
---|
130 | STAMCOUNTER StatClearedActiveSlaveIRQ;
|
---|
131 | #endif
|
---|
132 | } DEVPIC;
|
---|
133 | /** Pointer to the shared PIC instance data. */
|
---|
134 | typedef DEVPIC *PDEVPIC;
|
---|
135 |
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * The PIC device instance data for ring-3.
|
---|
139 | */
|
---|
140 | typedef struct DEVPICR3
|
---|
141 | {
|
---|
142 | /** Pointer to the PIC ring-3 helpers. */
|
---|
143 | R3PTRTYPE(PCPDMPICHLP) pPicHlp;
|
---|
144 | } DEVPICR3;
|
---|
145 | /** Pointer to the ring-3 PIC instance data. */
|
---|
146 | typedef DEVPICR3 *PDEVPICR3;
|
---|
147 |
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * The PIC device instance data for ring-0.
|
---|
151 | */
|
---|
152 | typedef struct DEVPICR0
|
---|
153 | {
|
---|
154 | /** Pointer to the PIC ring-0 helpers. */
|
---|
155 | R0PTRTYPE(PCPDMPICHLP) pPicHlp;
|
---|
156 | } DEVPICR0;
|
---|
157 | /** Pointer to the ring-0 PIC instance data. */
|
---|
158 | typedef DEVPICR0 *PDEVPICR0;
|
---|
159 |
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * The PIC device instance data for raw-mode.
|
---|
163 | */
|
---|
164 | typedef struct DEVPICRC
|
---|
165 | {
|
---|
166 | /** Pointer to the PIC raw-mode helpers. */
|
---|
167 | RCPTRTYPE(PCPDMPICHLP) pPicHlp;
|
---|
168 | } DEVPICRC;
|
---|
169 | /** Pointer to the raw-mode PIC instance data. */
|
---|
170 | typedef DEVPICRC *PDEVPICRC;
|
---|
171 |
|
---|
172 |
|
---|
173 | /** The PIC instance data for the current context. */
|
---|
174 | typedef CTX_SUFF(DEVPIC) DEVPICCC;
|
---|
175 | /** Pointer to the PIC instance data for the current context. */
|
---|
176 | typedef CTX_SUFF(PDEVPIC) PDEVPICCC;
|
---|
177 |
|
---|
178 |
|
---|
179 |
|
---|
180 | #ifndef VBOX_DEVICE_STRUCT_TESTCASE /* The rest of the file! */
|
---|
181 |
|
---|
182 | #ifdef LOG_ENABLED
|
---|
183 | DECLINLINE(void) DumpPICState(PPICSTATE pPic, const char *pszFn)
|
---|
184 | {
|
---|
185 | Log2(("%s: pic%d: elcr=%x last_irr=%x irr=%x imr=%x isr=%x irq_base=%x\n",
|
---|
186 | pszFn, pPic->idxPic, pPic->elcr, pPic->last_irr, pPic->irr, pPic->imr, pPic->isr, pPic->irq_base));
|
---|
187 | }
|
---|
188 | #else
|
---|
189 | # define DumpPICState(pThis, szFn) do { } while (0)
|
---|
190 | #endif
|
---|
191 |
|
---|
192 | /* set irq level. If an edge is detected, then the IRR is set to 1 */
|
---|
193 | DECLINLINE(void) pic_set_irq1(PPICSTATE pPic, int irq, int level, uint32_t uTagSrc)
|
---|
194 | {
|
---|
195 | Log(("pic_set_irq1: irq=%d level=%d\n", irq, level));
|
---|
196 | int mask = 1 << irq;
|
---|
197 | if (pPic->elcr & mask)
|
---|
198 | {
|
---|
199 | /* level triggered */
|
---|
200 | if (level)
|
---|
201 | {
|
---|
202 | Log2(("pic_set_irq1(ls) irr=%d irrnew=%d\n", pPic->irr, pPic->irr | mask));
|
---|
203 | pPic->irr |= mask;
|
---|
204 | pPic->last_irr |= mask;
|
---|
205 | }
|
---|
206 | else
|
---|
207 | {
|
---|
208 | Log2(("pic_set_irq1(lc) irr=%d irrnew=%d\n", pPic->irr, pPic->irr & ~mask));
|
---|
209 | pPic->irr &= ~mask;
|
---|
210 | pPic->last_irr &= ~mask;
|
---|
211 | }
|
---|
212 | }
|
---|
213 | else
|
---|
214 | {
|
---|
215 | /* edge triggered */
|
---|
216 | if (level)
|
---|
217 | {
|
---|
218 | if ((pPic->last_irr & mask) == 0)
|
---|
219 | {
|
---|
220 | Log2(("pic_set_irq1 irr=%x last_irr=%x\n", pPic->irr | mask, pPic->last_irr));
|
---|
221 | pPic->irr |= mask;
|
---|
222 | }
|
---|
223 | pPic->last_irr |= mask;
|
---|
224 | }
|
---|
225 | else
|
---|
226 | {
|
---|
227 | pPic->irr &= ~mask;
|
---|
228 | pPic->last_irr &= ~mask;
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | /* Save the tag. */
|
---|
233 | if (level)
|
---|
234 | {
|
---|
235 | if (!pPic->auTags[irq])
|
---|
236 | pPic->auTags[irq] = uTagSrc;
|
---|
237 | else
|
---|
238 | pPic->auTags[irq] |= RT_BIT_32(31);
|
---|
239 | }
|
---|
240 |
|
---|
241 | DumpPICState(pPic, "pic_set_irq1");
|
---|
242 | }
|
---|
243 |
|
---|
244 | /* return the highest priority found in mask (highest = smallest
|
---|
245 | number). Return 8 if no irq */
|
---|
246 | DECLINLINE(int) get_priority(PPICSTATE pPic, int mask)
|
---|
247 | {
|
---|
248 | int priority;
|
---|
249 | if (mask == 0)
|
---|
250 | return 8;
|
---|
251 | priority = 0;
|
---|
252 | while ((mask & (1 << ((priority + pPic->priority_add) & 7))) == 0)
|
---|
253 | priority++;
|
---|
254 | return priority;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /* return the pic wanted interrupt. return -1 if none */
|
---|
258 | static int pic_get_irq(PPICSTATE pPic)
|
---|
259 | {
|
---|
260 | int mask, cur_priority, priority;
|
---|
261 | Log(("pic_get_irq%d: mask=%x\n", pPic->idxPic, pPic->irr & ~pPic->imr));
|
---|
262 | DumpPICState(pPic, "pic_get_irq");
|
---|
263 |
|
---|
264 | mask = pPic->irr & ~pPic->imr;
|
---|
265 | priority = get_priority(pPic, mask);
|
---|
266 | Log(("pic_get_irq: priority=%x\n", priority));
|
---|
267 | if (priority == 8)
|
---|
268 | return -1;
|
---|
269 | /* compute current priority. If special fully nested mode on the
|
---|
270 | master, the IRQ coming from the slave is not taken into account
|
---|
271 | for the priority computation. */
|
---|
272 | mask = pPic->isr;
|
---|
273 | if (pPic->special_mask)
|
---|
274 | mask &= ~pPic->imr;
|
---|
275 | if (pPic->special_fully_nested_mode && pPic->idxPic == 0)
|
---|
276 | mask &= ~(1 << 2);
|
---|
277 | cur_priority = get_priority(pPic, mask);
|
---|
278 | Log(("pic_get_irq%d: cur_priority=%x pending=%d\n", pPic->idxPic,
|
---|
279 | cur_priority, (priority == 8) ? -1 : (priority + pPic->priority_add) & 7));
|
---|
280 | if (priority < cur_priority)
|
---|
281 | {
|
---|
282 | /* higher priority found: an irq should be generated */
|
---|
283 | return (priority + pPic->priority_add) & 7;
|
---|
284 | }
|
---|
285 | return -1;
|
---|
286 | }
|
---|
287 |
|
---|
288 | /* raise irq to CPU if necessary. must be called every time the active
|
---|
289 | irq may change */
|
---|
290 | static int pic_update_irq(PPDMDEVINS pDevIns, PDEVPIC pThis, PDEVPICCC pThisCC)
|
---|
291 | {
|
---|
292 | int irq2, irq;
|
---|
293 |
|
---|
294 | /* first look at slave pic */
|
---|
295 | irq2 = pic_get_irq(&pThis->aPics[1]);
|
---|
296 | Log(("pic_update_irq irq2=%d\n", irq2));
|
---|
297 | if (irq2 >= 0)
|
---|
298 | {
|
---|
299 | /* if irq request by slave pic, signal master PIC */
|
---|
300 | pic_set_irq1(&pThis->aPics[0], 2, 1, pThis->aPics[1].auTags[irq2]);
|
---|
301 | }
|
---|
302 | else
|
---|
303 | {
|
---|
304 | /* If not, clear the IR on the master PIC. */
|
---|
305 | pic_set_irq1(&pThis->aPics[0], 2, 0, 0 /*uTagSrc*/);
|
---|
306 | }
|
---|
307 | /* look at requested irq */
|
---|
308 | irq = pic_get_irq(&pThis->aPics[0]);
|
---|
309 | if (irq >= 0)
|
---|
310 | {
|
---|
311 | /* If irq 2 is pending on the master pic, then there must be one pending on the slave pic too! Otherwise we'll get
|
---|
312 | * spurious slave interrupts in picGetInterrupt.
|
---|
313 | */
|
---|
314 | if (irq != 2 || irq2 != -1)
|
---|
315 | {
|
---|
316 | for (int i = 0; i < 2; i++)
|
---|
317 | Log(("pic%d: imr=%x irr=%x padd=%d\n", i, pThis->aPics[i].imr, pThis->aPics[i].irr, pThis->aPics[i].priority_add));
|
---|
318 | Log(("pic: cpu_interrupt\n"));
|
---|
319 | pThisCC->pPicHlp->pfnSetInterruptFF(pDevIns);
|
---|
320 | }
|
---|
321 | else
|
---|
322 | {
|
---|
323 | STAM_COUNTER_INC(&pThis->StatClearedActiveIRQ2);
|
---|
324 | Log(("pic_update_irq: irq 2 is active, but no interrupt is pending on the slave pic!!\n"));
|
---|
325 | /* Clear it here, so lower priority interrupts can still be dispatched. */
|
---|
326 |
|
---|
327 | /* if this was the only pending irq, then we must clear the interrupt ff flag */
|
---|
328 | pThisCC->pPicHlp->pfnClearInterruptFF(pDevIns);
|
---|
329 |
|
---|
330 | /** @todo Is this correct? */
|
---|
331 | pThis->aPics[0].irr &= ~(1 << 2);
|
---|
332 |
|
---|
333 | /* Call ourselves again just in case other interrupts are pending */
|
---|
334 | return pic_update_irq(pDevIns, pThis, pThisCC);
|
---|
335 | }
|
---|
336 | }
|
---|
337 | else
|
---|
338 | {
|
---|
339 | Log(("pic_update_irq: no interrupt is pending!!\n"));
|
---|
340 |
|
---|
341 | /* we must clear the interrupt ff flag */
|
---|
342 | pThisCC->pPicHlp->pfnClearInterruptFF(pDevIns);
|
---|
343 | }
|
---|
344 | return VINF_SUCCESS;
|
---|
345 | }
|
---|
346 |
|
---|
347 | /**
|
---|
348 | * Set the an IRQ.
|
---|
349 | *
|
---|
350 | * @param pDevIns Device instance of the PICs.
|
---|
351 | * @param iIrq IRQ number to set.
|
---|
352 | * @param iLevel IRQ level.
|
---|
353 | * @param uTagSrc The IRQ tag and source ID (for tracing).
|
---|
354 | */
|
---|
355 | static DECLCALLBACK(void) picSetIrq(PPDMDEVINS pDevIns, int iIrq, int iLevel, uint32_t uTagSrc)
|
---|
356 | {
|
---|
357 | PDEVPIC pThis = PDMDEVINS_2_DATA(pDevIns, PDEVPIC);
|
---|
358 | PDEVPICCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PDEVPICCC);
|
---|
359 | AssertMsgReturnVoid(iIrq < 16, ("iIrq=%d\n", iIrq));
|
---|
360 |
|
---|
361 | Log(("picSetIrq %d %d\n", iIrq, iLevel));
|
---|
362 | DumpPICState(&pThis->aPics[0], "picSetIrq");
|
---|
363 | DumpPICState(&pThis->aPics[1], "picSetIrq");
|
---|
364 | STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatSetIrq));
|
---|
365 | if ((iLevel & PDM_IRQ_LEVEL_FLIP_FLOP) == PDM_IRQ_LEVEL_FLIP_FLOP)
|
---|
366 | {
|
---|
367 | /* A flip-flop lowers the IRQ line and immediately raises it, so
|
---|
368 | * that a rising edge is guaranteed to occur. Note that the IRQ
|
---|
369 | * line must be held high for a while to avoid spurious interrupts.
|
---|
370 | */
|
---|
371 | pic_set_irq1(&RT_SAFE_SUBSCRIPT(pThis->aPics, iIrq >> 3), iIrq & 7, 0, uTagSrc);
|
---|
372 | pic_update_irq(pDevIns, pThis, pThisCC);
|
---|
373 | }
|
---|
374 | pic_set_irq1(&RT_SAFE_SUBSCRIPT(pThis->aPics, iIrq >> 3), iIrq & 7, iLevel & PDM_IRQ_LEVEL_HIGH, uTagSrc);
|
---|
375 | pic_update_irq(pDevIns, pThis, pThisCC);
|
---|
376 | }
|
---|
377 |
|
---|
378 |
|
---|
379 | /* acknowledge interrupt 'irq' */
|
---|
380 | DECLINLINE(void) pic_intack(PPICSTATE pPic, int irq)
|
---|
381 | {
|
---|
382 | if (pPic->auto_eoi)
|
---|
383 | {
|
---|
384 | if (pPic->rotate_on_auto_eoi)
|
---|
385 | pPic->priority_add = (irq + 1) & 7;
|
---|
386 | }
|
---|
387 | else
|
---|
388 | pPic->isr |= (1 << irq);
|
---|
389 |
|
---|
390 | /* We don't clear a level sensitive interrupt here */
|
---|
391 | if (!(pPic->elcr & (1 << irq)))
|
---|
392 | {
|
---|
393 | Log2(("pic_intack: irr=%x irrnew=%x\n", pPic->irr, pPic->irr & ~(1 << irq)));
|
---|
394 | pPic->irr &= ~(1 << irq);
|
---|
395 | }
|
---|
396 | }
|
---|
397 |
|
---|
398 |
|
---|
399 | /**
|
---|
400 | * Get a pending interrupt.
|
---|
401 | *
|
---|
402 | * @returns Pending interrupt number.
|
---|
403 | * @param pDevIns Device instance of the PICs.
|
---|
404 | * @param puTagSrc Where to return the IRQ tag and source ID.
|
---|
405 | */
|
---|
406 | static DECLCALLBACK(int) picGetInterrupt(PPDMDEVINS pDevIns, uint32_t *puTagSrc)
|
---|
407 | {
|
---|
408 | PDEVPIC pThis = PDMDEVINS_2_DATA(pDevIns, PDEVPIC);
|
---|
409 | PDEVPICCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PDEVPICCC);
|
---|
410 | int irq;
|
---|
411 | int irq2;
|
---|
412 | int intno;
|
---|
413 |
|
---|
414 | /* read the irq from the PIC */
|
---|
415 | DumpPICState(&pThis->aPics[0], "picGetInterrupt");
|
---|
416 | DumpPICState(&pThis->aPics[1], "picGetInterrupt");
|
---|
417 |
|
---|
418 | irq = pic_get_irq(&pThis->aPics[0]);
|
---|
419 | if (irq >= 0)
|
---|
420 | {
|
---|
421 | pic_intack(&pThis->aPics[0], irq);
|
---|
422 | if (irq == 2)
|
---|
423 | {
|
---|
424 | irq2 = pic_get_irq(&pThis->aPics[1]);
|
---|
425 | if (irq2 >= 0)
|
---|
426 | pic_intack(&pThis->aPics[1], irq2);
|
---|
427 | else
|
---|
428 | {
|
---|
429 | /* Interrupt went away or is now masked. */
|
---|
430 | Log(("picGetInterrupt: spurious IRQ on slave controller, converted to IRQ15\n"));
|
---|
431 | irq2 = 7;
|
---|
432 | }
|
---|
433 | intno = pThis->aPics[1].irq_base + irq2;
|
---|
434 | *puTagSrc = pThis->aPics[0].auTags[irq2];
|
---|
435 | pThis->aPics[0].auTags[irq2] = 0;
|
---|
436 | Log2(("picGetInterrupt1: %x base=%x irq=%x uTagSrc=%#x\n", intno, pThis->aPics[1].irq_base, irq2, *puTagSrc));
|
---|
437 | irq = irq2 + 8;
|
---|
438 | }
|
---|
439 | else
|
---|
440 | {
|
---|
441 | intno = pThis->aPics[0].irq_base + irq;
|
---|
442 | *puTagSrc = pThis->aPics[0].auTags[irq];
|
---|
443 | pThis->aPics[0].auTags[irq] = 0;
|
---|
444 | Log2(("picGetInterrupt0: %x base=%x irq=%x uTagSrc=%#x\n", intno, pThis->aPics[0].irq_base, irq, *puTagSrc));
|
---|
445 | }
|
---|
446 | }
|
---|
447 | else
|
---|
448 | {
|
---|
449 | /* Interrupt went away or is now masked. */
|
---|
450 | Log(("picGetInterrupt: spurious IRQ on master controller, converted to IRQ7\n"));
|
---|
451 | irq = 7;
|
---|
452 | intno = pThis->aPics[0].irq_base + irq;
|
---|
453 | *puTagSrc = 0;
|
---|
454 | }
|
---|
455 | pic_update_irq(pDevIns, pThis, pThisCC);
|
---|
456 |
|
---|
457 | Log(("picGetInterrupt: 0x%02x pending 0:%d 1:%d\n", intno, pic_get_irq(&pThis->aPics[0]), pic_get_irq(&pThis->aPics[1])));
|
---|
458 |
|
---|
459 | return intno;
|
---|
460 | }
|
---|
461 |
|
---|
462 | static void pic_reset(PPICSTATE pPic)
|
---|
463 | {
|
---|
464 | pPic->last_irr = 0;
|
---|
465 | pPic->irr = 0;
|
---|
466 | pPic->imr = 0;
|
---|
467 | pPic->isr = 0;
|
---|
468 | pPic->priority_add = 0;
|
---|
469 | pPic->irq_base = 0;
|
---|
470 | pPic->read_reg_select = 0;
|
---|
471 | pPic->poll = 0;
|
---|
472 | pPic->special_mask = 0;
|
---|
473 | pPic->init_state = 0;
|
---|
474 | pPic->auto_eoi = 0;
|
---|
475 | pPic->rotate_on_auto_eoi = 0;
|
---|
476 | pPic->special_fully_nested_mode = 0;
|
---|
477 | pPic->init4 = 0;
|
---|
478 | //pPic->elcr - not cleared;
|
---|
479 | //pPic->elcr_mask - not cleared;
|
---|
480 | RT_ZERO(pPic->auTags);
|
---|
481 | }
|
---|
482 |
|
---|
483 |
|
---|
484 | static VBOXSTRICTRC pic_ioport_write(PPDMDEVINS pDevIns, PDEVPIC pThis, PDEVPICCC pThisCC, PPICSTATE pPic,
|
---|
485 | uint32_t addr, uint32_t val)
|
---|
486 | {
|
---|
487 | VBOXSTRICTRC rc = VINF_SUCCESS;
|
---|
488 | int irq;
|
---|
489 |
|
---|
490 | Log(("pic_write/%zu: addr=0x%02x val=0x%02x\n", pPic - pThis->aPics, addr, val));
|
---|
491 | addr &= 1;
|
---|
492 | if (addr == 0)
|
---|
493 | {
|
---|
494 | if (val & 0x10)
|
---|
495 | {
|
---|
496 | /* init */
|
---|
497 | pic_reset(pPic);
|
---|
498 | /* deassert a pending interrupt */
|
---|
499 | pThisCC->pPicHlp->pfnClearInterruptFF(pDevIns);
|
---|
500 |
|
---|
501 | pPic->init_state = 1;
|
---|
502 | pPic->init4 = val & 1;
|
---|
503 | if (!(val & 0x0a))
|
---|
504 | { /* likely */ }
|
---|
505 | else if (pThis->cRelLogEntries++ < 64)
|
---|
506 | {
|
---|
507 | if (val & 0x02)
|
---|
508 | LogRel(("PIC: Single mode not supported, ignored.\n"));
|
---|
509 | if (val & 0x08)
|
---|
510 | LogRel(("PIC: Level sensitive IRQ setting ignored.\n"));
|
---|
511 | }
|
---|
512 | }
|
---|
513 | else if (val & 0x08)
|
---|
514 | {
|
---|
515 | if (val & 0x04)
|
---|
516 | pPic->poll = 1;
|
---|
517 | if (val & 0x02)
|
---|
518 | pPic->read_reg_select = val & 1;
|
---|
519 | if (val & 0x40)
|
---|
520 | pPic->special_mask = (val >> 5) & 1;
|
---|
521 | }
|
---|
522 | else
|
---|
523 | {
|
---|
524 | int cmd = val >> 5;
|
---|
525 | switch (cmd)
|
---|
526 | {
|
---|
527 | case 0:
|
---|
528 | case 4:
|
---|
529 | pPic->rotate_on_auto_eoi = cmd >> 2;
|
---|
530 | break;
|
---|
531 | case 1: /* end of interrupt */
|
---|
532 | case 5:
|
---|
533 | {
|
---|
534 | int priority = get_priority(pPic, pPic->isr);
|
---|
535 | if (priority != 8) {
|
---|
536 | irq = (priority + pPic->priority_add) & 7;
|
---|
537 | Log(("pic_write: EOI prio=%d irq=%d\n", priority, irq));
|
---|
538 | pPic->isr &= ~(1 << irq);
|
---|
539 | if (cmd == 5)
|
---|
540 | pPic->priority_add = (irq + 1) & 7;
|
---|
541 | rc = pic_update_irq(pDevIns, pThis, pThisCC);
|
---|
542 | Assert(rc == VINF_SUCCESS);
|
---|
543 | DumpPICState(pPic, "eoi");
|
---|
544 | }
|
---|
545 | break;
|
---|
546 | }
|
---|
547 | case 3:
|
---|
548 | {
|
---|
549 | irq = val & 7;
|
---|
550 | Log(("pic_write: EOI2 for irq %d\n", irq));
|
---|
551 | pPic->isr &= ~(1 << irq);
|
---|
552 | rc = pic_update_irq(pDevIns, pThis, pThisCC);
|
---|
553 | Assert(rc == VINF_SUCCESS);
|
---|
554 | DumpPICState(pPic, "eoi2");
|
---|
555 | break;
|
---|
556 | }
|
---|
557 | case 6:
|
---|
558 | {
|
---|
559 | pPic->priority_add = (val + 1) & 7;
|
---|
560 | Log(("pic_write: lowest priority %d (highest %d)\n", val & 7, pPic->priority_add));
|
---|
561 | rc = pic_update_irq(pDevIns, pThis, pThisCC);
|
---|
562 | Assert(rc == VINF_SUCCESS);
|
---|
563 | break;
|
---|
564 | }
|
---|
565 | case 7:
|
---|
566 | {
|
---|
567 | irq = val & 7;
|
---|
568 | Log(("pic_write: EOI3 for irq %d\n", irq));
|
---|
569 | pPic->isr &= ~(1 << irq);
|
---|
570 | pPic->priority_add = (irq + 1) & 7;
|
---|
571 | rc = pic_update_irq(pDevIns, pThis, pThisCC);
|
---|
572 | Assert(rc == VINF_SUCCESS);
|
---|
573 | DumpPICState(pPic, "eoi3");
|
---|
574 | break;
|
---|
575 | }
|
---|
576 | default:
|
---|
577 | /* no operation */
|
---|
578 | break;
|
---|
579 | }
|
---|
580 | }
|
---|
581 | }
|
---|
582 | else
|
---|
583 | {
|
---|
584 | switch (pPic->init_state)
|
---|
585 | {
|
---|
586 | case 0:
|
---|
587 | /* normal mode */
|
---|
588 | pPic->imr = val;
|
---|
589 | rc = pic_update_irq(pDevIns, pThis, pThisCC);
|
---|
590 | Assert(rc == VINF_SUCCESS);
|
---|
591 | break;
|
---|
592 | case 1:
|
---|
593 | pPic->irq_base = val & 0xf8;
|
---|
594 | pPic->init_state = 2;
|
---|
595 | Log(("pic_write: set irq base to %x\n", pPic->irq_base));
|
---|
596 | break;
|
---|
597 | case 2:
|
---|
598 | if (pPic->init4)
|
---|
599 | pPic->init_state = 3;
|
---|
600 | else
|
---|
601 | pPic->init_state = 0;
|
---|
602 | break;
|
---|
603 | case 3:
|
---|
604 | pPic->special_fully_nested_mode = (val >> 4) & 1;
|
---|
605 | pPic->auto_eoi = (val >> 1) & 1;
|
---|
606 | pPic->init_state = 0;
|
---|
607 | Log(("pic_write: special_fully_nested_mode=%d auto_eoi=%d\n", pPic->special_fully_nested_mode, pPic->auto_eoi));
|
---|
608 | break;
|
---|
609 | }
|
---|
610 | }
|
---|
611 | return rc;
|
---|
612 | }
|
---|
613 |
|
---|
614 |
|
---|
615 | static uint32_t pic_poll_read(PPDMDEVINS pDevIns, PDEVPIC pThis, PDEVPICCC pThisCC, PPICSTATE pPic, uint32_t addr1)
|
---|
616 | {
|
---|
617 | int ret = pic_get_irq(pPic);
|
---|
618 | if (ret >= 0)
|
---|
619 | {
|
---|
620 | if (addr1 >> 7)
|
---|
621 | {
|
---|
622 | Log2(("pic_poll_read: clear slave irq (isr)\n"));
|
---|
623 | pThis->aPics[0].isr &= ~(1 << 2);
|
---|
624 | pThis->aPics[0].irr &= ~(1 << 2);
|
---|
625 | }
|
---|
626 | Log2(("pic_poll_read: clear irq %d (isr)\n", ret));
|
---|
627 | pPic->irr &= ~(1 << ret);
|
---|
628 | pPic->isr &= ~(1 << ret);
|
---|
629 | if (addr1 >> 7 || ret != 2)
|
---|
630 | pic_update_irq(pDevIns, pThis, pThisCC);
|
---|
631 | }
|
---|
632 | else
|
---|
633 | {
|
---|
634 | ret = 0;
|
---|
635 | pic_update_irq(pDevIns, pThis, pThisCC);
|
---|
636 | }
|
---|
637 |
|
---|
638 | return ret;
|
---|
639 | }
|
---|
640 |
|
---|
641 |
|
---|
642 | static uint32_t pic_ioport_read(PPDMDEVINS pDevIns, PDEVPIC pThis, PDEVPICCC pThisCC, PPICSTATE pPic, uint32_t addr1, int *pRC)
|
---|
643 | {
|
---|
644 | unsigned int addr;
|
---|
645 | int ret;
|
---|
646 |
|
---|
647 | *pRC = VINF_SUCCESS;
|
---|
648 |
|
---|
649 | addr = addr1;
|
---|
650 | addr &= 1;
|
---|
651 | if (pPic->poll)
|
---|
652 | {
|
---|
653 | ret = pic_poll_read(pDevIns, pThis, pThisCC, pPic, addr1);
|
---|
654 | pPic->poll = 0;
|
---|
655 | }
|
---|
656 | else
|
---|
657 | {
|
---|
658 | if (addr == 0)
|
---|
659 | {
|
---|
660 | if (pPic->read_reg_select)
|
---|
661 | ret = pPic->isr;
|
---|
662 | else
|
---|
663 | ret = pPic->irr;
|
---|
664 | }
|
---|
665 | else
|
---|
666 | ret = pPic->imr;
|
---|
667 | }
|
---|
668 | Log(("pic_read: addr=0x%02x val=0x%02x\n", addr1, ret));
|
---|
669 | return ret;
|
---|
670 | }
|
---|
671 |
|
---|
672 |
|
---|
673 |
|
---|
674 | /* -=-=-=-=-=- I/O ports -=-=-=-=-=- */
|
---|
675 |
|
---|
676 | /**
|
---|
677 | * @callback_method_impl{FNIOMIOPORTNEWIN}
|
---|
678 | */
|
---|
679 | static DECLCALLBACK(VBOXSTRICTRC) picIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t *pu32, unsigned cb)
|
---|
680 | {
|
---|
681 | PDEVPIC pThis = PDMDEVINS_2_DATA(pDevIns, PDEVPIC);
|
---|
682 | PDEVPICCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PDEVPICCC);
|
---|
683 | uint32_t iPic = (uint32_t)(uintptr_t)pvUser;
|
---|
684 | int rc;
|
---|
685 |
|
---|
686 | Assert(iPic == 0 || iPic == 1);
|
---|
687 | if (cb == 1)
|
---|
688 | {
|
---|
689 | PIC_LOCK_RET(pDevIns, pThisCC, VINF_IOM_R3_IOPORT_READ);
|
---|
690 | *pu32 = pic_ioport_read(pDevIns, pThis, pThisCC, &RT_SAFE_SUBSCRIPT(pThis->aPics, iPic), offPort, &rc);
|
---|
691 | PIC_UNLOCK(pDevIns, pThisCC);
|
---|
692 | return rc;
|
---|
693 | }
|
---|
694 | else if (cb == 2)
|
---|
695 | {
|
---|
696 | uint8_t u8Lo, u8Hi = 0;
|
---|
697 | /* Manually split access. Probably not 100% accurate! */
|
---|
698 | PIC_LOCK_RET(pDevIns, pThisCC, VINF_IOM_R3_IOPORT_READ);
|
---|
699 | u8Lo = pic_ioport_read(pDevIns, pThis, pThisCC, &RT_SAFE_SUBSCRIPT(pThis->aPics, iPic), offPort, &rc);
|
---|
700 | Assert(rc == VINF_SUCCESS);
|
---|
701 | if (!(offPort & 1))
|
---|
702 | u8Hi = pic_ioport_read(pDevIns, pThis, pThisCC, &RT_SAFE_SUBSCRIPT(pThis->aPics, iPic), offPort + 1, &rc);
|
---|
703 | PIC_UNLOCK(pDevIns, pThisCC);
|
---|
704 | *pu32 = RT_MAKE_U16(u8Lo, u8Hi);
|
---|
705 | return rc;
|
---|
706 | }
|
---|
707 | return VERR_IOM_IOPORT_UNUSED;
|
---|
708 | }
|
---|
709 |
|
---|
710 |
|
---|
711 | /**
|
---|
712 | * @callback_method_impl{FNIOMIOPORTNEWOUT}
|
---|
713 | */
|
---|
714 | static DECLCALLBACK(VBOXSTRICTRC) picIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t u32, unsigned cb)
|
---|
715 | {
|
---|
716 | PDEVPIC pThis = PDMDEVINS_2_DATA(pDevIns, PDEVPIC);
|
---|
717 | PDEVPICCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PDEVPICCC);
|
---|
718 | uint32_t iPic = (uint32_t)(uintptr_t)pvUser;
|
---|
719 | VBOXSTRICTRC rc;
|
---|
720 |
|
---|
721 | Assert(iPic == 0 || iPic == 1);
|
---|
722 |
|
---|
723 | if (cb == 1)
|
---|
724 | {
|
---|
725 | PIC_LOCK_RET(pDevIns, pThisCC, VINF_IOM_R3_IOPORT_WRITE);
|
---|
726 | rc = pic_ioport_write(pDevIns, pThis, pThisCC, &RT_SAFE_SUBSCRIPT(pThis->aPics, iPic), offPort, u32);
|
---|
727 | PIC_UNLOCK(pDevIns, pThisCC);
|
---|
728 | return rc;
|
---|
729 | }
|
---|
730 | else if (cb == 2)
|
---|
731 | {
|
---|
732 | PIC_LOCK_RET(pDevIns, pThisCC, VINF_IOM_R3_IOPORT_WRITE);
|
---|
733 | /* Manually split access. Probably not 100% accurate! */
|
---|
734 | rc = pic_ioport_write(pDevIns, pThis, pThisCC, &RT_SAFE_SUBSCRIPT(pThis->aPics, iPic), offPort, RT_LOBYTE(u32));
|
---|
735 | if (RT_SUCCESS(rc) && !(offPort & 1))
|
---|
736 | rc = pic_ioport_write(pDevIns, pThis, pThisCC, &RT_SAFE_SUBSCRIPT(pThis->aPics, iPic), offPort + 1, RT_HIBYTE(u32));
|
---|
737 | PIC_UNLOCK(pDevIns, pThisCC);
|
---|
738 | return rc;
|
---|
739 | }
|
---|
740 | return VINF_SUCCESS;
|
---|
741 | }
|
---|
742 |
|
---|
743 |
|
---|
744 | /**
|
---|
745 | * @callback_method_impl{FNIOMIOPORTNEWIN, ELCR}
|
---|
746 | */
|
---|
747 | static DECLCALLBACK(VBOXSTRICTRC) picIOPortElcrRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t *pu32, unsigned cb)
|
---|
748 | {
|
---|
749 | if (cb == 1)
|
---|
750 | {
|
---|
751 | PDEVPICCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PDEVPICCC);
|
---|
752 | PPICSTATE pPic = (PPICSTATE)pvUser;
|
---|
753 | PIC_LOCK_RET(pDevIns, pThisCC, VINF_IOM_R3_IOPORT_READ);
|
---|
754 | *pu32 = pPic->elcr;
|
---|
755 | PIC_UNLOCK(pDevIns, pThisCC);
|
---|
756 | return VINF_SUCCESS;
|
---|
757 | }
|
---|
758 | RT_NOREF(offPort);
|
---|
759 | return VERR_IOM_IOPORT_UNUSED;
|
---|
760 | }
|
---|
761 |
|
---|
762 |
|
---|
763 | /**
|
---|
764 | * @callback_method_impl{FNIOMIOPORTNEWOUT, ELCR}
|
---|
765 | */
|
---|
766 | static DECLCALLBACK(VBOXSTRICTRC) picIOPortElcrWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t u32, unsigned cb)
|
---|
767 | {
|
---|
768 | if (cb == 1)
|
---|
769 | {
|
---|
770 | PDEVPICCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PDEVPICCC);
|
---|
771 | PPICSTATE pPic = (PPICSTATE)pvUser;
|
---|
772 | PIC_LOCK_RET(pDevIns, pThisCC, VINF_IOM_R3_IOPORT_WRITE);
|
---|
773 | pPic->elcr = u32 & pPic->elcr_mask;
|
---|
774 | PIC_UNLOCK(pDevIns, pThisCC);
|
---|
775 | }
|
---|
776 | RT_NOREF(offPort);
|
---|
777 | return VINF_SUCCESS;
|
---|
778 | }
|
---|
779 |
|
---|
780 |
|
---|
781 | #ifdef IN_RING3
|
---|
782 |
|
---|
783 | /**
|
---|
784 | * @callback_method_impl{FNDBGFHANDLERDEV}
|
---|
785 | */
|
---|
786 | static DECLCALLBACK(void) picR3Info(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
|
---|
787 | {
|
---|
788 | PDEVPIC pThis = PDMDEVINS_2_DATA(pDevIns, PDEVPIC);
|
---|
789 | NOREF(pszArgs);
|
---|
790 |
|
---|
791 | /*
|
---|
792 | * Show info.
|
---|
793 | */
|
---|
794 | for (unsigned i = 0; i < RT_ELEMENTS(pThis->aPics); i++)
|
---|
795 | {
|
---|
796 | PPICSTATE pPic = &pThis->aPics[i];
|
---|
797 |
|
---|
798 | pHlp->pfnPrintf(pHlp, "PIC%d:\n", i);
|
---|
799 | pHlp->pfnPrintf(pHlp, " IMR :%02x ISR :%02x IRR :%02x LIRR:%02x\n",
|
---|
800 | pPic->imr, pPic->isr, pPic->irr, pPic->last_irr);
|
---|
801 | pHlp->pfnPrintf(pHlp, " Base:%02x PriAdd:%02x RegSel:%02x\n",
|
---|
802 | pPic->irq_base, pPic->priority_add, pPic->read_reg_select);
|
---|
803 | pHlp->pfnPrintf(pHlp, " Poll:%02x SpMask:%02x IState:%02x\n",
|
---|
804 | pPic->poll, pPic->special_mask, pPic->init_state);
|
---|
805 | pHlp->pfnPrintf(pHlp, " AEOI:%02x Rotate:%02x FNest :%02x Ini4:%02x\n",
|
---|
806 | pPic->auto_eoi, pPic->rotate_on_auto_eoi,
|
---|
807 | pPic->special_fully_nested_mode, pPic->init4);
|
---|
808 | pHlp->pfnPrintf(pHlp, " ELCR:%02x ELMask:%02x\n", pPic->elcr, pPic->elcr_mask);
|
---|
809 | }
|
---|
810 | }
|
---|
811 |
|
---|
812 |
|
---|
813 | /* -=-=-=-=-=- Saved State -=-=-=-=-=- */
|
---|
814 |
|
---|
815 | /**
|
---|
816 | * @callback_method_impl{FNSSMDEVSAVEEXEC}
|
---|
817 | */
|
---|
818 | static DECLCALLBACK(int) picR3SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
|
---|
819 | {
|
---|
820 | PDEVPIC pThis = PDMDEVINS_2_DATA(pDevIns, PDEVPIC);
|
---|
821 | PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
|
---|
822 |
|
---|
823 | for (unsigned i = 0; i < RT_ELEMENTS(pThis->aPics); i++)
|
---|
824 | {
|
---|
825 | pHlp->pfnSSMPutU8(pSSM, pThis->aPics[i].last_irr);
|
---|
826 | pHlp->pfnSSMPutU8(pSSM, pThis->aPics[i].irr);
|
---|
827 | pHlp->pfnSSMPutU8(pSSM, pThis->aPics[i].imr);
|
---|
828 | pHlp->pfnSSMPutU8(pSSM, pThis->aPics[i].isr);
|
---|
829 | pHlp->pfnSSMPutU8(pSSM, pThis->aPics[i].priority_add);
|
---|
830 | pHlp->pfnSSMPutU8(pSSM, pThis->aPics[i].irq_base);
|
---|
831 | pHlp->pfnSSMPutU8(pSSM, pThis->aPics[i].read_reg_select);
|
---|
832 | pHlp->pfnSSMPutU8(pSSM, pThis->aPics[i].poll);
|
---|
833 | pHlp->pfnSSMPutU8(pSSM, pThis->aPics[i].special_mask);
|
---|
834 | pHlp->pfnSSMPutU8(pSSM, pThis->aPics[i].init_state);
|
---|
835 | pHlp->pfnSSMPutU8(pSSM, pThis->aPics[i].auto_eoi);
|
---|
836 | pHlp->pfnSSMPutU8(pSSM, pThis->aPics[i].rotate_on_auto_eoi);
|
---|
837 | pHlp->pfnSSMPutU8(pSSM, pThis->aPics[i].special_fully_nested_mode);
|
---|
838 | pHlp->pfnSSMPutU8(pSSM, pThis->aPics[i].init4);
|
---|
839 | pHlp->pfnSSMPutU8(pSSM, pThis->aPics[i].elcr);
|
---|
840 | }
|
---|
841 | return VINF_SUCCESS;
|
---|
842 | }
|
---|
843 |
|
---|
844 |
|
---|
845 | /**
|
---|
846 | * @callback_method_impl{FNSSMDEVLOADEXEC}
|
---|
847 | */
|
---|
848 | static DECLCALLBACK(int) picR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
|
---|
849 | {
|
---|
850 | PDEVPIC pThis = PDMDEVINS_2_DATA(pDevIns, PDEVPIC);
|
---|
851 | PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
|
---|
852 |
|
---|
853 | if (uVersion != 1)
|
---|
854 | return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
|
---|
855 | Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
|
---|
856 |
|
---|
857 | for (unsigned i = 0; i < RT_ELEMENTS(pThis->aPics); i++)
|
---|
858 | {
|
---|
859 | pHlp->pfnSSMGetU8(pSSM, &pThis->aPics[i].last_irr);
|
---|
860 | pHlp->pfnSSMGetU8(pSSM, &pThis->aPics[i].irr);
|
---|
861 | pHlp->pfnSSMGetU8(pSSM, &pThis->aPics[i].imr);
|
---|
862 | pHlp->pfnSSMGetU8(pSSM, &pThis->aPics[i].isr);
|
---|
863 | pHlp->pfnSSMGetU8(pSSM, &pThis->aPics[i].priority_add);
|
---|
864 | pHlp->pfnSSMGetU8(pSSM, &pThis->aPics[i].irq_base);
|
---|
865 | pHlp->pfnSSMGetU8(pSSM, &pThis->aPics[i].read_reg_select);
|
---|
866 | pHlp->pfnSSMGetU8(pSSM, &pThis->aPics[i].poll);
|
---|
867 | pHlp->pfnSSMGetU8(pSSM, &pThis->aPics[i].special_mask);
|
---|
868 | pHlp->pfnSSMGetU8(pSSM, &pThis->aPics[i].init_state);
|
---|
869 | pHlp->pfnSSMGetU8(pSSM, &pThis->aPics[i].auto_eoi);
|
---|
870 | pHlp->pfnSSMGetU8(pSSM, &pThis->aPics[i].rotate_on_auto_eoi);
|
---|
871 | pHlp->pfnSSMGetU8(pSSM, &pThis->aPics[i].special_fully_nested_mode);
|
---|
872 | pHlp->pfnSSMGetU8(pSSM, &pThis->aPics[i].init4);
|
---|
873 | pHlp->pfnSSMGetU8(pSSM, &pThis->aPics[i].elcr);
|
---|
874 | }
|
---|
875 |
|
---|
876 | /* Note! PDM will restore the VMCPU_FF_INTERRUPT_PIC state. */
|
---|
877 | return VINF_SUCCESS;
|
---|
878 | }
|
---|
879 |
|
---|
880 |
|
---|
881 | /* -=-=-=-=-=- PDMDEVREG -=-=-=-=-=- */
|
---|
882 |
|
---|
883 | /**
|
---|
884 | * @interface_method_impl{PDMDEVREG,pfnReset}
|
---|
885 | */
|
---|
886 | static DECLCALLBACK(void) picR3Reset(PPDMDEVINS pDevIns)
|
---|
887 | {
|
---|
888 | PDEVPIC pThis = PDMDEVINS_2_DATA(pDevIns, PDEVPIC);
|
---|
889 | PDEVPICCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PDEVPICCC);
|
---|
890 | unsigned i;
|
---|
891 | LogFlow(("picR3Reset:\n"));
|
---|
892 | pThisCC->pPicHlp->pfnLock(pDevIns, VERR_INTERNAL_ERROR);
|
---|
893 |
|
---|
894 | for (i = 0; i < RT_ELEMENTS(pThis->aPics); i++)
|
---|
895 | pic_reset(&pThis->aPics[i]);
|
---|
896 |
|
---|
897 | PIC_UNLOCK(pDevIns, pThisCC);
|
---|
898 | }
|
---|
899 |
|
---|
900 |
|
---|
901 | /**
|
---|
902 | * @interface_method_impl{PDMDEVREG,pfnRelocate}
|
---|
903 | */
|
---|
904 | static DECLCALLBACK(void) picR3Relocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
|
---|
905 | {
|
---|
906 | PDEVPICRC pThisRC = PDMINS_2_DATA_RC(pDevIns, PDEVPICRC);
|
---|
907 | pThisRC->pPicHlp += offDelta;
|
---|
908 | }
|
---|
909 |
|
---|
910 |
|
---|
911 | /**
|
---|
912 | * @interface_method_impl{PDMDEVREG,pfnConstruct}
|
---|
913 | */
|
---|
914 | static DECLCALLBACK(int) picR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
|
---|
915 | {
|
---|
916 | PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
|
---|
917 | PDEVPIC pThis = PDMDEVINS_2_DATA(pDevIns, PDEVPIC);
|
---|
918 | PDEVPICCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PDEVPICCC);
|
---|
919 | int rc;
|
---|
920 | RT_NOREF(iInstance, pCfg);
|
---|
921 |
|
---|
922 | Assert(iInstance == 0);
|
---|
923 |
|
---|
924 | /*
|
---|
925 | * Validate and read configuration.
|
---|
926 | */
|
---|
927 | PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns, "", "");
|
---|
928 | Log(("DevPIC: fRCEnabled=%RTbool fR0Enabled=%RTbool\n", pDevIns->fRCEnabled, pDevIns->fR0Enabled));
|
---|
929 |
|
---|
930 | /*
|
---|
931 | * Init the data.
|
---|
932 | */
|
---|
933 | Assert(RT_ELEMENTS(pThis->aPics) == 2);
|
---|
934 | pThis->aPics[0].elcr_mask = 0xf8;
|
---|
935 | pThis->aPics[1].elcr_mask = 0xde;
|
---|
936 | pThis->aPics[0].idxPic = 0;
|
---|
937 | pThis->aPics[1].idxPic = 1;
|
---|
938 | pThis->cRelLogEntries = 0;
|
---|
939 |
|
---|
940 | /*
|
---|
941 | * Register us as the PIC with PDM.
|
---|
942 | */
|
---|
943 | PDMPICREG PicReg;
|
---|
944 | PicReg.u32Version = PDM_PICREG_VERSION;
|
---|
945 | PicReg.pfnSetIrq = picSetIrq;
|
---|
946 | PicReg.pfnGetInterrupt = picGetInterrupt;
|
---|
947 | PicReg.u32TheEnd = PDM_PICREG_VERSION;
|
---|
948 | rc = PDMDevHlpPICRegister(pDevIns, &PicReg, &pThisCC->pPicHlp);
|
---|
949 | AssertLogRelMsgRCReturn(rc, ("PDMDevHlpPICRegister -> %Rrc\n", rc), rc);
|
---|
950 | AssertReturn(pThisCC->pPicHlp->u32Version == PDM_PICHLP_VERSION, VERR_VERSION_MISMATCH);
|
---|
951 | AssertReturn(pThisCC->pPicHlp->u32TheEnd == PDM_PICHLP_VERSION, VERR_VERSION_MISMATCH);
|
---|
952 |
|
---|
953 | /*
|
---|
954 | * Since the PIC helper interface provides access to the PDM lock,
|
---|
955 | * we need no device level critical section.
|
---|
956 | */
|
---|
957 | rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
|
---|
958 | AssertRCReturn(rc, rc);
|
---|
959 |
|
---|
960 | /*
|
---|
961 | * Register I/O ports and save state.
|
---|
962 | */
|
---|
963 | rc = PDMDevHlpIoPortCreateUAndMap(pDevIns, 0x20 /*uPort*/, 2 /*cPorts*/, picIOPortWrite, picIOPortRead, (void *)0,
|
---|
964 | "i8259 PIC #0", NULL /*paExtDesc*/, &pThis->aPics[0].hIoPorts0);
|
---|
965 | AssertRCReturn(rc, rc);
|
---|
966 | rc = PDMDevHlpIoPortCreateUAndMap(pDevIns, 0xa0 /*uPort*/, 2 /*cPorts*/, picIOPortWrite, picIOPortRead, (void *)1,
|
---|
967 | "i8259 PIC #1", NULL /*paExtDesc*/, &pThis->aPics[1].hIoPorts0);
|
---|
968 | AssertRCReturn(rc, rc);
|
---|
969 |
|
---|
970 |
|
---|
971 | rc = PDMDevHlpIoPortCreateUAndMap(pDevIns, 0x4d0 /*uPort*/, 1 /*cPorts*/, picIOPortElcrWrite, picIOPortElcrRead,
|
---|
972 | &pThis->aPics[0], "i8259 PIC #0 - elcr", NULL /*paExtDesc*/, &pThis->aPics[0].hIoPorts1);
|
---|
973 | AssertRCReturn(rc, rc);
|
---|
974 | rc = PDMDevHlpIoPortCreateUAndMap(pDevIns, 0x4d1 /*uPort*/, 1 /*cPorts*/, picIOPortElcrWrite, picIOPortElcrRead,
|
---|
975 | &pThis->aPics[1], "i8259 PIC #1 - elcr", NULL /*paExtDesc*/, &pThis->aPics[1].hIoPorts1);
|
---|
976 | AssertRCReturn(rc, rc);
|
---|
977 |
|
---|
978 | /*
|
---|
979 | * Saved state.
|
---|
980 | */
|
---|
981 | rc = PDMDevHlpSSMRegister(pDevIns, 1 /* uVersion */, sizeof(*pThis), picR3SaveExec, picR3LoadExec);
|
---|
982 | AssertRCReturn(rc, rc);
|
---|
983 |
|
---|
984 | /*
|
---|
985 | * Register the info item.
|
---|
986 | */
|
---|
987 | PDMDevHlpDBGFInfoRegister(pDevIns, "pic", "PIC info.", picR3Info);
|
---|
988 |
|
---|
989 | /*
|
---|
990 | * Initialize the device state.
|
---|
991 | */
|
---|
992 | picR3Reset(pDevIns);
|
---|
993 |
|
---|
994 | # ifdef VBOX_WITH_STATISTICS
|
---|
995 | /*
|
---|
996 | * Statistics.
|
---|
997 | */
|
---|
998 | PDMDevHlpSTAMRegister(pDevIns, &pThis->StatSetIrqRZ, STAMTYPE_COUNTER, "SetIrqRZ", STAMUNIT_OCCURENCES, "Number of PIC SetIrq calls in ring-0/raw-mode.");
|
---|
999 | PDMDevHlpSTAMRegister(pDevIns, &pThis->StatSetIrqR3, STAMTYPE_COUNTER, "SetIrqR3", STAMUNIT_OCCURENCES, "Number of PIC SetIrq calls in ring-3.");
|
---|
1000 |
|
---|
1001 | PDMDevHlpSTAMRegister(pDevIns, &pThis->StatClearedActiveIRQ2, STAMTYPE_COUNTER, "Masked/ActiveIRQ2", STAMUNIT_OCCURENCES, "Number of cleared irq 2.");
|
---|
1002 | PDMDevHlpSTAMRegister(pDevIns, &pThis->StatClearedActiveMasterIRQ, STAMTYPE_COUNTER, "Masked/ActiveMaster", STAMUNIT_OCCURENCES, "Number of cleared master irqs.");
|
---|
1003 | PDMDevHlpSTAMRegister(pDevIns, &pThis->StatClearedActiveSlaveIRQ, STAMTYPE_COUNTER, "Masked/ActiveSlave", STAMUNIT_OCCURENCES, "Number of cleared slave irqs.");
|
---|
1004 | # endif
|
---|
1005 |
|
---|
1006 | return VINF_SUCCESS;
|
---|
1007 | }
|
---|
1008 |
|
---|
1009 | #else /* !IN_RING3 */
|
---|
1010 |
|
---|
1011 | /**
|
---|
1012 | * @callback_method_impl{PDMDEVREGR0,pfnConstruct}
|
---|
1013 | */
|
---|
1014 | static DECLCALLBACK(int) picRZConstruct(PPDMDEVINS pDevIns)
|
---|
1015 | {
|
---|
1016 | PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
|
---|
1017 | PDEVPIC pThis = PDMDEVINS_2_DATA(pDevIns, PDEVPIC);
|
---|
1018 | PDEVPICCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PDEVPICCC);
|
---|
1019 |
|
---|
1020 | /* NOP the critsect: */
|
---|
1021 | int rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
|
---|
1022 | AssertRCReturn(rc, rc);
|
---|
1023 |
|
---|
1024 | /* Set up the PIC callbacks: */
|
---|
1025 | PDMPICREG PicReg;
|
---|
1026 | PicReg.u32Version = PDM_PICREG_VERSION;
|
---|
1027 | PicReg.pfnSetIrq = picSetIrq;
|
---|
1028 | PicReg.pfnGetInterrupt = picGetInterrupt;
|
---|
1029 | PicReg.u32TheEnd = PDM_PICREG_VERSION;
|
---|
1030 | rc = PDMDevHlpPICSetUpContext(pDevIns, &PicReg, &pThisCC->pPicHlp);
|
---|
1031 | AssertLogRelMsgRCReturn(rc, ("PDMDevHlpPICSetUpContext -> %Rrc\n", rc), rc);
|
---|
1032 | AssertPtrReturn(pThisCC->pPicHlp, VERR_INTERNAL_ERROR_3);
|
---|
1033 | AssertReturn(pThisCC->pPicHlp->u32Version == PDM_PICHLP_VERSION, VERR_VERSION_MISMATCH);
|
---|
1034 | AssertReturn(pThisCC->pPicHlp->u32TheEnd == PDM_PICHLP_VERSION, VERR_VERSION_MISMATCH);
|
---|
1035 |
|
---|
1036 | /* I/O port callbacks: */
|
---|
1037 | Assert(RT_ELEMENTS(pThis->aPics) == 2);
|
---|
1038 | rc = PDMDevHlpIoPortSetUpContext(pDevIns, pThis->aPics[0].hIoPorts0, picIOPortWrite, picIOPortRead, (void *)0);
|
---|
1039 | AssertRCReturn(rc, rc);
|
---|
1040 | rc = PDMDevHlpIoPortSetUpContext(pDevIns, pThis->aPics[1].hIoPorts0, picIOPortWrite, picIOPortRead, (void *)1);
|
---|
1041 | AssertRCReturn(rc, rc);
|
---|
1042 |
|
---|
1043 | rc = PDMDevHlpIoPortSetUpContext(pDevIns, pThis->aPics[0].hIoPorts1, picIOPortElcrWrite, picIOPortElcrRead, &pThis->aPics[0]);
|
---|
1044 | AssertRCReturn(rc, rc);
|
---|
1045 | rc = PDMDevHlpIoPortSetUpContext(pDevIns, pThis->aPics[1].hIoPorts1, picIOPortElcrWrite, picIOPortElcrRead, &pThis->aPics[1]);
|
---|
1046 | AssertRCReturn(rc, rc);
|
---|
1047 |
|
---|
1048 | return VINF_SUCCESS;
|
---|
1049 | }
|
---|
1050 |
|
---|
1051 | #endif /* !IN_RING3 */
|
---|
1052 |
|
---|
1053 | /**
|
---|
1054 | * The device registration structure.
|
---|
1055 | */
|
---|
1056 | const PDMDEVREG g_DeviceI8259 =
|
---|
1057 | {
|
---|
1058 | /* .u32Version = */ PDM_DEVREG_VERSION,
|
---|
1059 | /* .uReserved0 = */ 0,
|
---|
1060 | /* .szName = */ "i8259",
|
---|
1061 | /* .fFlags = */ PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RZ | PDM_DEVREG_FLAGS_NEW_STYLE
|
---|
1062 | | PDM_DEVREG_FLAGS_REQUIRE_R0 | PDM_DEVREG_FLAGS_REQUIRE_RC,
|
---|
1063 | /* .fClass = */ PDM_DEVREG_CLASS_PIC,
|
---|
1064 | /* .cMaxInstances = */ 1,
|
---|
1065 | /* .uSharedVersion = */ 42,
|
---|
1066 | /* .cbInstanceShared = */ sizeof(DEVPIC),
|
---|
1067 | /* .cbInstanceCC = */ sizeof(DEVPICCC),
|
---|
1068 | /* .cbInstanceRC = */ sizeof(DEVPICRC),
|
---|
1069 | /* .cMaxPciDevices = */ 0,
|
---|
1070 | /* .cMaxMsixVectors = */ 0,
|
---|
1071 | /* .pszDescription = */ "Intel 8259 Programmable Interrupt Controller (PIC) Device.",
|
---|
1072 | #if defined(IN_RING3)
|
---|
1073 | /* .pszRCMod = */ "VBoxDDRC.rc",
|
---|
1074 | /* .pszR0Mod = */ "VBoxDDR0.r0",
|
---|
1075 | /* .pfnConstruct = */ picR3Construct,
|
---|
1076 | /* .pfnDestruct = */ NULL,
|
---|
1077 | /* .pfnRelocate = */ picR3Relocate,
|
---|
1078 | /* .pfnMemSetup = */ NULL,
|
---|
1079 | /* .pfnPowerOn = */ NULL,
|
---|
1080 | /* .pfnReset = */ picR3Reset,
|
---|
1081 | /* .pfnSuspend = */ NULL,
|
---|
1082 | /* .pfnResume = */ NULL,
|
---|
1083 | /* .pfnAttach = */ NULL,
|
---|
1084 | /* .pfnDetach = */ NULL,
|
---|
1085 | /* .pfnQueryInterface = */ NULL,
|
---|
1086 | /* .pfnInitComplete = */ NULL,
|
---|
1087 | /* .pfnPowerOff = */ NULL,
|
---|
1088 | /* .pfnSoftReset = */ NULL,
|
---|
1089 | /* .pfnReserved0 = */ NULL,
|
---|
1090 | /* .pfnReserved1 = */ NULL,
|
---|
1091 | /* .pfnReserved2 = */ NULL,
|
---|
1092 | /* .pfnReserved3 = */ NULL,
|
---|
1093 | /* .pfnReserved4 = */ NULL,
|
---|
1094 | /* .pfnReserved5 = */ NULL,
|
---|
1095 | /* .pfnReserved6 = */ NULL,
|
---|
1096 | /* .pfnReserved7 = */ NULL,
|
---|
1097 | #elif defined(IN_RING0)
|
---|
1098 | /* .pfnEarlyConstruct = */ NULL,
|
---|
1099 | /* .pfnConstruct = */ picRZConstruct,
|
---|
1100 | /* .pfnDestruct = */ NULL,
|
---|
1101 | /* .pfnFinalDestruct = */ NULL,
|
---|
1102 | /* .pfnRequest = */ NULL,
|
---|
1103 | /* .pfnReserved0 = */ NULL,
|
---|
1104 | /* .pfnReserved1 = */ NULL,
|
---|
1105 | /* .pfnReserved2 = */ NULL,
|
---|
1106 | /* .pfnReserved3 = */ NULL,
|
---|
1107 | /* .pfnReserved4 = */ NULL,
|
---|
1108 | /* .pfnReserved5 = */ NULL,
|
---|
1109 | /* .pfnReserved6 = */ NULL,
|
---|
1110 | /* .pfnReserved7 = */ NULL,
|
---|
1111 | #elif defined(IN_RC)
|
---|
1112 | /* .pfnConstruct = */ picRZConstruct,
|
---|
1113 | /* .pfnReserved0 = */ NULL,
|
---|
1114 | /* .pfnReserved1 = */ NULL,
|
---|
1115 | /* .pfnReserved2 = */ NULL,
|
---|
1116 | /* .pfnReserved3 = */ NULL,
|
---|
1117 | /* .pfnReserved4 = */ NULL,
|
---|
1118 | /* .pfnReserved5 = */ NULL,
|
---|
1119 | /* .pfnReserved6 = */ NULL,
|
---|
1120 | /* .pfnReserved7 = */ NULL,
|
---|
1121 | #else
|
---|
1122 | # error "Not in IN_RING3, IN_RING0 or IN_RC!"
|
---|
1123 | #endif
|
---|
1124 | /* .u32VersionEnd = */ PDM_DEVREG_VERSION
|
---|
1125 | };
|
---|
1126 |
|
---|
1127 | #endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
|
---|
1128 |
|
---|