VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/DevDMA.cpp@ 83967

Last change on this file since 83967 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 43.6 KB
Line 
1/* $Id: DevDMA.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * DevDMA - DMA Controller Device.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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 loosely based on:
19 *
20 * QEMU DMA emulation
21 *
22 * Copyright (c) 2003 Vassili Karpov (malc)
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* Header Files *
46*********************************************************************************************************************************/
47#define LOG_GROUP LOG_GROUP_DEV_DMA
48#include <VBox/vmm/pdmdev.h>
49#include <VBox/err.h>
50
51#include <VBox/AssertGuest.h>
52#include <VBox/log.h>
53#include <iprt/assert.h>
54#include <iprt/string.h>
55
56#include "VBoxDD.h"
57
58
59/** @page pg_dev_dma DMA Overview and notes
60 *
61 * Modern PCs typically emulate AT-compatible DMA. The IBM PC/AT used dual
62 * cascaded 8237A DMA controllers, augmented with a 74LS612 memory mapper.
63 * The 8237As are 8-bit parts, only capable of addressing up to 64KB; the
64 * 74LS612 extends addressing to 24 bits. That leads to well known and
65 * inconvenient DMA limitations:
66 * - DMA can only access physical memory under the 16MB line
67 * - DMA transfers must occur within a 64KB/128KB 'page'
68 *
69 * The 16-bit DMA controller added in the PC/AT shifts all 8237A addresses
70 * left by one, including the control registers addresses. The DMA register
71 * offsets (except for the page registers) are therefore "double spaced".
72 *
73 * Due to the address shifting, the DMA controller decodes more addresses
74 * than are usually documented, with aliasing. See the ICH8 datasheet.
75 *
76 * In the IBM PC and PC/XT, DMA channel 0 was used for memory refresh, thus
77 * preventing the use of memory-to-memory DMA transfers (which use channels
78 * 0 and 1). In the PC/AT, memory-to-memory DMA was theoretically possible.
79 * However, it would transfer a single byte at a time, while the CPU can
80 * transfer two (on a 286) or four (on a 386+) bytes at a time. On many
81 * compatibles, memory-to-memory DMA is not even implemented at all, and
82 * therefore has no practical use.
83 *
84 * Auto-init mode is handled implicitly; a device's transfer handler may
85 * return an end count lower than the start count.
86 *
87 * Naming convention: 'channel' refers to a system-wide DMA channel (0-7)
88 * while 'chidx' refers to a DMA channel index within a controller (0-3).
89 *
90 * References:
91 * - IBM Personal Computer AT Technical Reference, 1984
92 * - Intel 8237A-5 Datasheet, 1993
93 * - Frank van Gilluwe, The Undocumented PC, 1994
94 * - OPTi 82C206 Data Book, 1996 (or Chips & Tech 82C206)
95 * - Intel ICH8 Datasheet, 2007
96 */
97
98
99/* Saved state versions. */
100#define DMA_SAVESTATE_OLD 1 /* The original saved state. */
101#define DMA_SAVESTATE_CURRENT 2 /* The new and improved saved state. */
102
103/* State information for a single DMA channel. */
104typedef struct {
105 RTR3PTR pvUser; /* User specific context. */
106 R3PTRTYPE(PFNDMATRANSFERHANDLER) pfnXferHandler; /* Transfer handler for channel. */
107 uint16_t u16BaseAddr; /* Base address for transfers. */
108 uint16_t u16BaseCount; /* Base count for transfers. */
109 uint16_t u16CurAddr; /* Current address. */
110 uint16_t u16CurCount; /* Current count. */
111 uint8_t u8Mode; /* Channel mode. */
112 uint8_t abPadding[7];
113} DMAChannel, DMACHANNEL;
114typedef DMACHANNEL *PDMACHANNEL;
115
116/* State information for a DMA controller (DMA8 or DMA16). */
117typedef struct {
118 DMAChannel ChState[4]; /* Per-channel state. */
119 uint8_t au8Page[8]; /* Page registers (A16-A23). */
120 uint8_t au8PageHi[8]; /* High page registers (A24-A31). */
121 uint8_t u8Command; /* Command register. */
122 uint8_t u8Status; /* Status register. */
123 uint8_t u8Mask; /* Mask register. */
124 uint8_t u8Temp; /* Temporary (mem/mem) register. */
125 uint8_t u8ModeCtr; /* Mode register counter for reads. */
126 bool fHiByte; /* Byte pointer (T/F -> high/low). */
127 uint8_t abPadding0[2];
128 uint32_t is16bit; /* True for 16-bit DMA. */
129 uint8_t abPadding1[4];
130 /** The base abd current address I/O port registration. */
131 IOMIOPORTHANDLE hIoPortBase;
132 /** The control register I/O port registration. */
133 IOMIOPORTHANDLE hIoPortCtl;
134 /** The page registers I/O port registration. */
135 IOMIOPORTHANDLE hIoPortPage;
136 /** The EISA style high page registers I/O port registration. */
137 IOMIOPORTHANDLE hIoPortHi;
138} DMAControl, DMACONTROLLER;
139/** Pointer to the shared DMA controller state. */
140typedef DMACONTROLLER *PDMACONTROLLER;
141
142/* Complete DMA state information. */
143typedef struct {
144 DMAControl DMAC[2]; /* Two DMA controllers. */
145 PPDMDEVINSR3 pDevIns; /* Device instance. */
146 R3PTRTYPE(PCPDMDMACHLP) pHlp; /* PDM DMA helpers. */
147 STAMPROFILE StatRun;
148} DMAState, DMASTATE;
149/** Pointer to the shared DMA state information. */
150typedef DMASTATE *PDMASTATE;
151
152/* DMA command register bits. */
153enum {
154 CMD_MEMTOMEM = 0x01, /* Enable mem-to-mem trasfers. */
155 CMD_ADRHOLD = 0x02, /* Address hold for mem-to-mem. */
156 CMD_DISABLE = 0x04, /* Disable controller. */
157 CMD_COMPRTIME = 0x08, /* Compressed timing. */
158 CMD_ROTPRIO = 0x10, /* Rotating priority. */
159 CMD_EXTWR = 0x20, /* Extended write. */
160 CMD_DREQHI = 0x40, /* DREQ is active high if set. */
161 CMD_DACKHI = 0x80, /* DACK is active high if set. */
162 CMD_UNSUPPORTED = CMD_MEMTOMEM | CMD_ADRHOLD | CMD_COMPRTIME
163 | CMD_EXTWR | CMD_DREQHI | CMD_DACKHI
164};
165
166/* DMA control register offsets for read accesses. */
167enum {
168 CTL_R_STAT, /* Read status registers. */
169 CTL_R_DMAREQ, /* Read DRQ register. */
170 CTL_R_CMD, /* Read command register. */
171 CTL_R_MODE, /* Read mode register. */
172 CTL_R_SETBPTR, /* Set byte pointer flip-flop. */
173 CTL_R_TEMP, /* Read temporary register. */
174 CTL_R_CLRMODE, /* Clear mode register counter. */
175 CTL_R_MASK /* Read all DRQ mask bits. */
176};
177
178/* DMA control register offsets for read accesses. */
179enum {
180 CTL_W_CMD, /* Write command register. */
181 CTL_W_DMAREQ, /* Write DRQ register. */
182 CTL_W_MASKONE, /* Write single DRQ mask bit. */
183 CTL_W_MODE, /* Write mode register. */
184 CTL_W_CLRBPTR, /* Clear byte pointer flip-flop. */
185 CTL_W_MASTRCLR, /* Master clear. */
186 CTL_W_CLRMASK, /* Clear all DRQ mask bits. */
187 CTL_W_MASK /* Write all DRQ mask bits. */
188};
189
190/* DMA transfer modes. */
191enum {
192 DMODE_DEMAND, /* Demand transfer mode. */
193 DMODE_SINGLE, /* Single transfer mode. */
194 DMODE_BLOCK, /* Block transfer mode. */
195 DMODE_CASCADE /* Cascade mode. */
196};
197
198/* DMA transfer types. */
199enum {
200 DTYPE_VERIFY, /* Verify transfer type. */
201 DTYPE_WRITE, /* Write transfer type. */
202 DTYPE_READ, /* Read transfer type. */
203 DTYPE_ILLEGAL /* Undefined. */
204};
205
206#ifndef VBOX_DEVICE_STRUCT_TESTCASE
207
208
209/* Convert DMA channel number (0-7) to controller number (0-1). */
210#define DMACH2C(c) (c < 4 ? 0 : 1)
211
212#ifdef LOG_ENABLED
213static int const g_aiDmaChannelMap[8] = {-1, 2, 3, 1, -1, -1, -1, 0};
214/* Map a DMA page register offset (0-7) to channel index (0-3). */
215# define DMAPG2CX(c) (g_aiDmaChannelMap[c])
216#endif
217
218#ifdef IN_RING3
219static int const g_aiDmaMapChannel[4] = {7, 3, 1, 2};
220/* Map a channel index (0-3) to DMA page register offset (0-7). */
221# define DMACX2PG(c) (g_aiDmaMapChannel[c])
222/* Map a channel number (0-7) to DMA page register offset (0-7). */
223# define DMACH2PG(c) (g_aiDmaMapChannel[c & 3])
224#endif
225
226/* Test the decrement bit of mode register. */
227#define IS_MODE_DEC(c) ((c) & 0x20)
228/* Test the auto-init bit of mode register. */
229#define IS_MODE_AI(c) ((c) & 0x10)
230/* Extract the transfer type bits of mode register. */
231#define GET_MODE_XTYP(c) (((c) & 0x0c) >> 2)
232
233
234/* Perform a master clear (reset) on a DMA controller. */
235static void dmaClear(DMAControl *dc)
236{
237 dc->u8Command = 0;
238 dc->u8Status = 0;
239 dc->u8Temp = 0;
240 dc->u8ModeCtr = 0;
241 dc->fHiByte = false;
242 dc->u8Mask = UINT8_MAX;
243}
244
245
246/** Read the byte pointer and flip it. */
247DECLINLINE(bool) dmaReadBytePtr(DMAControl *dc)
248{
249 bool fHighByte = !!dc->fHiByte;
250 dc->fHiByte ^= 1;
251 return fHighByte;
252}
253
254
255/* DMA address registers writes and reads. */
256
257/**
258 * @callback_method_impl{FNIOMIOPORTOUT, Ports 0-7 & 0xc0-0xcf}
259 */
260static DECLCALLBACK(VBOXSTRICTRC) dmaWriteAddr(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t u32, unsigned cb)
261{
262 PDMACONTROLLER dc = (PDMACONTROLLER)pvUser;
263 RT_NOREF(pDevIns);
264 if (cb == 1)
265 {
266 unsigned const reg = (offPort >> dc->is16bit) & 0x0f;
267 unsigned const chidx = reg >> 1;
268 unsigned const is_count = reg & 1;
269 PDMACHANNEL ch = &RT_SAFE_SUBSCRIPT(dc->ChState, chidx);
270 Assert(!(u32 & ~0xff)); /* Check for garbage in high bits. */
271
272 if (dmaReadBytePtr(dc))
273 {
274 /* Write the high byte. */
275 if (is_count)
276 ch->u16BaseCount = RT_MAKE_U16(ch->u16BaseCount, u32);
277 else
278 ch->u16BaseAddr = RT_MAKE_U16(ch->u16BaseAddr, u32);
279
280 ch->u16CurCount = 0;
281 ch->u16CurAddr = ch->u16BaseAddr;
282 }
283 else
284 {
285 /* Write the low byte. */
286 if (is_count)
287 ch->u16BaseCount = RT_MAKE_U16(u32, RT_HIBYTE(ch->u16BaseCount));
288 else
289 ch->u16BaseAddr = RT_MAKE_U16(u32, RT_HIBYTE(ch->u16BaseAddr));
290 }
291 Log2(("dmaWriteAddr/%u: offPort %#06x, chidx %d, data %#02x\n", dc->is16bit, offPort, chidx, u32));
292 }
293 else
294 {
295 /* Likely a guest bug. */
296 Log(("dmaWriteAddr/%u: Bad size write to count register %#x (size %d, data %#x)\n", dc->is16bit, offPort, cb, u32));
297 }
298 return VINF_SUCCESS;
299}
300
301
302/**
303 * @callback_method_impl{FNIOMIOPORTIN, Ports 0-7 & 0xc0-0xcf}
304 */
305static DECLCALLBACK(VBOXSTRICTRC) dmaReadAddr(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t *pu32, unsigned cb)
306{
307 RT_NOREF(pDevIns);
308 if (cb == 1)
309 {
310 PDMACONTROLLER dc = (PDMACONTROLLER)pvUser;
311 unsigned const reg = (offPort >> dc->is16bit) & 0x0f;
312 unsigned const chidx = reg >> 1;
313 PDMACHANNEL ch = &RT_SAFE_SUBSCRIPT(dc->ChState, chidx);
314 int const dir = IS_MODE_DEC(ch->u8Mode) ? -1 : 1;
315 int val;
316 int bptr;
317
318 if (reg & 1)
319 val = ch->u16BaseCount - ch->u16CurCount;
320 else
321 val = ch->u16CurAddr + ch->u16CurCount * dir;
322
323 bptr = dmaReadBytePtr(dc);
324 *pu32 = RT_LOBYTE(val >> (bptr * 8));
325
326 Log(("dmaReadAddr/%u: Count read: offPort %#06x, reg %#04x, data %#x\n", dc->is16bit, offPort, reg, val));
327 return VINF_SUCCESS;
328 }
329 return VERR_IOM_IOPORT_UNUSED;
330}
331
332/* DMA control registers writes and reads. */
333
334/**
335 * @callback_method_impl{FNIOMIOPORTOUT, Ports 0x8-0xf & 0xd0-0xdf}
336 */
337static DECLCALLBACK(VBOXSTRICTRC) dmaWriteCtl(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t u32, unsigned cb)
338{
339 PDMACONTROLLER dc = (PDMACONTROLLER)pvUser;
340 RT_NOREF(pDevIns);
341 if (cb == 1)
342 {
343 unsigned chidx = 0;
344 unsigned const reg = (offPort >> dc->is16bit) & 0x0f;
345 Assert((int)reg >= CTL_W_CMD && reg <= CTL_W_MASK);
346 Assert(!(u32 & ~0xff)); /* Check for garbage in high bits. */
347
348 switch (reg) {
349 case CTL_W_CMD:
350 /* Unsupported commands are entirely ignored. */
351 if (u32 & CMD_UNSUPPORTED)
352 {
353 Log(("dmaWriteCtl/%u: DMA command %#x is not supported, ignoring!\n", dc->is16bit, u32));
354 break;
355 }
356 dc->u8Command = u32;
357 break;
358 case CTL_W_DMAREQ:
359 chidx = u32 & 3;
360 if (u32 & 4)
361 dc->u8Status |= 1 << (chidx + 4);
362 else
363 dc->u8Status &= ~(1 << (chidx + 4));
364 dc->u8Status &= ~(1 << chidx); /* Clear TC for channel. */
365 break;
366 case CTL_W_MASKONE:
367 chidx = u32 & 3;
368 if (u32 & 4)
369 dc->u8Mask |= 1 << chidx;
370 else
371 dc->u8Mask &= ~(1 << chidx);
372 break;
373 case CTL_W_MODE:
374 chidx = u32 & 3;
375 dc->ChState[chidx].u8Mode = u32;
376 Log2(("dmaWriteCtl/%u: chidx %d, op %d, %sauto-init, %screment, opmode %d\n", dc->is16bit,
377 chidx, (u32 >> 2) & 3, IS_MODE_AI(u32) ? "" : "no ", IS_MODE_DEC(u32) ? "de" : "in", (u32 >> 6) & 3));
378 break;
379 case CTL_W_CLRBPTR:
380 dc->fHiByte = false;
381 break;
382 case CTL_W_MASTRCLR:
383 dmaClear(dc);
384 break;
385 case CTL_W_CLRMASK:
386 dc->u8Mask = 0;
387 break;
388 case CTL_W_MASK:
389 dc->u8Mask = u32;
390 break;
391 default:
392 ASSERT_GUEST_MSG_FAILED(("reg=%u\n", reg));
393 break;
394 }
395 Log(("dmaWriteCtl/%u: offPort %#06x, chidx %d, data %#02x\n", dc->is16bit, offPort, chidx, u32));
396 }
397 else
398 {
399 /* Likely a guest bug. */
400 Log(("dmaWriteCtl/%u: Bad size write to controller register %#x (size %d, data %#x)\n", dc->is16bit, offPort, cb, u32));
401 }
402 return VINF_SUCCESS;
403}
404
405
406/**
407 * @callback_method_impl{FNIOMIOPORTIN, Ports 0x8-0xf & 0xd0-0xdf}
408 */
409static DECLCALLBACK(VBOXSTRICTRC) dmaReadCtl(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t *pu32, unsigned cb)
410{
411 RT_NOREF(pDevIns);
412 if (cb == 1)
413 {
414 PDMACONTROLLER dc = (PDMACONTROLLER)pvUser;
415 uint8_t val = 0;
416
417 unsigned const reg = (offPort >> dc->is16bit) & 0x0f;
418 Assert((int)reg >= CTL_R_STAT && reg <= CTL_R_MASK);
419
420 switch (reg)
421 {
422 case CTL_R_STAT:
423 val = dc->u8Status;
424 dc->u8Status &= 0xf0; /* A read clears all TCs. */
425 break;
426 case CTL_R_DMAREQ:
427 val = (dc->u8Status >> 4) | 0xf0;
428 break;
429 case CTL_R_CMD:
430 val = dc->u8Command;
431 break;
432 case CTL_R_MODE:
433 val = RT_SAFE_SUBSCRIPT(dc->ChState, dc->u8ModeCtr).u8Mode | 3;
434 dc->u8ModeCtr = (dc->u8ModeCtr + 1) & 3;
435 break;
436 case CTL_R_SETBPTR:
437 dc->fHiByte = true;
438 break;
439 case CTL_R_TEMP:
440 val = dc->u8Temp;
441 break;
442 case CTL_R_CLRMODE:
443 dc->u8ModeCtr = 0;
444 break;
445 case CTL_R_MASK:
446 val = dc->u8Mask;
447 break;
448 default:
449 Assert(0);
450 break;
451 }
452
453 Log(("dmaReadCtl/%u: Ctrl read: offPort %#06x, reg %#04x, data %#x\n", dc->is16bit, offPort, reg, val));
454 *pu32 = val;
455
456 return VINF_SUCCESS;
457 }
458 return VERR_IOM_IOPORT_UNUSED;
459}
460
461
462
463/**
464 * @callback_method_impl{FNIOMIOPORTIN,
465 * DMA page registers - Ports 0x80-0x87 & 0x88-0x8f}
466 *
467 * There are 16 R/W page registers for compatibility with the IBM PC/AT; only
468 * some of those registers are used for DMA. The page register accessible via
469 * port 80h may be read to insert small delays or used as a scratch register by
470 * a BIOS.
471 */
472static DECLCALLBACK(VBOXSTRICTRC) dmaReadPage(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t *pu32, unsigned cb)
473{
474 RT_NOREF(pDevIns);
475 PDMACONTROLLER dc = (PDMACONTROLLER)pvUser;
476 int reg;
477
478 if (cb == 1)
479 {
480 reg = offPort & 7;
481 *pu32 = dc->au8Page[reg];
482 Log2(("dmaReadPage/%u: Read %#x (byte) from page register %#x (channel %d)\n", dc->is16bit, *pu32, offPort, DMAPG2CX(reg)));
483 return VINF_SUCCESS;
484 }
485
486 if (cb == 2)
487 {
488 reg = offPort & 7;
489 *pu32 = dc->au8Page[reg] | (dc->au8Page[(reg + 1) & 7] << 8);
490 Log2(("dmaReadPage/%u: Read %#x (word) from page register %#x (channel %d)\n", dc->is16bit, *pu32, offPort, DMAPG2CX(reg)));
491 return VINF_SUCCESS;
492 }
493
494 return VERR_IOM_IOPORT_UNUSED;
495}
496
497
498/**
499 * @callback_method_impl{FNIOMIOPORTOUT,
500 * DMA page registers - Ports 0x80-0x87 & 0x88-0x8f}
501 */
502static DECLCALLBACK(VBOXSTRICTRC) dmaWritePage(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t u32, unsigned cb)
503{
504 RT_NOREF(pDevIns);
505 PDMACONTROLLER dc = (PDMACONTROLLER)pvUser;
506 unsigned reg;
507
508 if (cb == 1)
509 {
510 Assert(!(u32 & ~0xff)); /* Check for garbage in high bits. */
511 reg = offPort & 7;
512 dc->au8Page[reg] = u32;
513 dc->au8PageHi[reg] = 0; /* Corresponding high page cleared. */
514 Log2(("dmaWritePage/%u: Wrote %#x to page register %#x (channel %d)\n", dc->is16bit, u32, offPort, DMAPG2CX(reg)));
515 }
516 else if (cb == 2)
517 {
518 Assert(!(u32 & ~0xffff)); /* Check for garbage in high bits. */
519 reg = offPort & 7;
520 dc->au8Page[reg] = u32;
521 dc->au8PageHi[reg] = 0; /* Corresponding high page cleared. */
522 reg = (offPort + 1) & 7;
523 dc->au8Page[reg] = u32 >> 8;
524 dc->au8PageHi[reg] = 0; /* Corresponding high page cleared. */
525 }
526 else
527 {
528 /* Likely a guest bug. */
529 Log(("dmaWritePage/%u: Bad size write to page register %#x (size %d, data %#x)\n", dc->is16bit, offPort, cb, u32));
530 }
531 return VINF_SUCCESS;
532}
533
534
535/**
536 * @callback_method_impl{FNIOMIOPORTIN,
537 * EISA style high page registers for extending the DMA addresses to cover
538 * the entire 32-bit address space. Ports 0x480-0x487 & 0x488-0x48f}
539 */
540static DECLCALLBACK(VBOXSTRICTRC) dmaReadHiPage(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t *pu32, unsigned cb)
541{
542 RT_NOREF(pDevIns);
543 if (cb == 1)
544 {
545 PDMACONTROLLER dc = (PDMACONTROLLER)pvUser;
546 unsigned const reg = offPort & 7;
547
548 *pu32 = dc->au8PageHi[reg];
549 Log2(("dmaReadHiPage/%u: Read %#x to from high page register %#x (channel %d)\n", dc->is16bit, *pu32, offPort, DMAPG2CX(reg)));
550 return VINF_SUCCESS;
551 }
552 return VERR_IOM_IOPORT_UNUSED;
553}
554
555
556/**
557 * @callback_method_impl{FNIOMIOPORTOUT, Ports 0x480-0x487 & 0x488-0x48f}
558 */
559static DECLCALLBACK(VBOXSTRICTRC) dmaWriteHiPage(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t u32, unsigned cb)
560{
561 RT_NOREF(pDevIns);
562 PDMACONTROLLER dc = (PDMACONTROLLER)pvUser;
563 if (cb == 1)
564 {
565 unsigned const reg = offPort & 7;
566
567 Assert(!(u32 & ~0xff)); /* Check for garbage in high bits. */
568 dc->au8PageHi[reg] = u32;
569 Log2(("dmaWriteHiPage/%u: Wrote %#x to high page register %#x (channel %d)\n", dc->is16bit, u32, offPort, DMAPG2CX(reg)));
570 }
571 else
572 {
573 /* Likely a guest bug. */
574 Log(("dmaWriteHiPage/%u: Bad size write to high page register %#x (size %d, data %#x)\n", dc->is16bit, offPort, cb, u32));
575 }
576 return VINF_SUCCESS;
577}
578
579
580#ifdef IN_RING3
581
582/** Perform any pending transfers on a single DMA channel. */
583static void dmaR3RunChannel(DMAState *pThis, int ctlidx, int chidx)
584{
585 DMAControl *dc = &pThis->DMAC[ctlidx];
586 DMAChannel *ch = &dc->ChState[chidx];
587 uint32_t start_cnt, end_cnt;
588 int opmode;
589
590 opmode = (ch->u8Mode >> 6) & 3;
591
592 Log3(("DMA address %screment, mode %d\n", IS_MODE_DEC(ch->u8Mode) ? "de" : "in", ch->u8Mode >> 6));
593
594 /* Addresses and counts are shifted for 16-bit channels. */
595 start_cnt = ch->u16CurCount << dc->is16bit;
596 /* NB: The device is responsible for examining the DMA mode and not
597 * transferring more than it should if auto-init is not in use.
598 */
599 end_cnt = ch->pfnXferHandler(pThis->pDevIns, ch->pvUser, (ctlidx * 4) + chidx,
600 start_cnt, (ch->u16BaseCount + 1) << dc->is16bit);
601 ch->u16CurCount = end_cnt >> dc->is16bit;
602 /* Set the TC (Terminal Count) bit if transfer was completed. */
603 if (ch->u16CurCount == ch->u16BaseCount + 1)
604 switch (opmode)
605 {
606 case DMODE_DEMAND:
607 case DMODE_SINGLE:
608 case DMODE_BLOCK:
609 dc->u8Status |= RT_BIT(chidx);
610 Log3(("TC set for DMA channel %d\n", (ctlidx * 4) + chidx));
611 break;
612 default:
613 break;
614 }
615 Log3(("DMA position %d, size %d\n", end_cnt, (ch->u16BaseCount + 1) << dc->is16bit));
616}
617
618/**
619 * @interface_method_impl{PDMDMAREG,pfnRun}
620 */
621static DECLCALLBACK(bool) dmaR3Run(PPDMDEVINS pDevIns)
622{
623 DMAState *pThis = PDMDEVINS_2_DATA(pDevIns, PDMASTATE);
624 DMAControl *dc;
625 int chidx, mask;
626 STAM_PROFILE_START(&pThis->StatRun, a);
627 PDMDevHlpCritSectEnter(pDevIns, pDevIns->pCritSectRoR3, VERR_IGNORED);
628
629 /* Run all controllers and channels. */
630 for (unsigned ctlidx = 0; ctlidx < RT_ELEMENTS(pThis->DMAC); ++ctlidx)
631 {
632 dc = &pThis->DMAC[ctlidx];
633
634 /* If controller is disabled, don't even bother. */
635 if (dc->u8Command & CMD_DISABLE)
636 continue;
637
638 for (chidx = 0; chidx < 4; ++chidx)
639 {
640 mask = 1 << chidx;
641 if (!(dc->u8Mask & mask) && (dc->u8Status & (mask << 4)))
642 dmaR3RunChannel(pThis, ctlidx, chidx);
643 }
644 }
645
646 PDMDevHlpCritSectLeave(pDevIns, pDevIns->pCritSectRoR3);
647 STAM_PROFILE_STOP(&pThis->StatRun, a);
648 return 0;
649}
650
651/**
652 * @interface_method_impl{PDMDMAREG,pfnRegister}
653 */
654static DECLCALLBACK(void) dmaR3Register(PPDMDEVINS pDevIns, unsigned uChannel,
655 PFNDMATRANSFERHANDLER pfnTransferHandler, void *pvUser)
656{
657 DMAState *pThis = PDMDEVINS_2_DATA(pDevIns, PDMASTATE);
658 DMAChannel *ch = &pThis->DMAC[DMACH2C(uChannel)].ChState[uChannel & 3];
659
660 LogFlow(("dmaR3Register: pThis=%p uChannel=%u pfnTransferHandler=%p pvUser=%p\n", pThis, uChannel, pfnTransferHandler, pvUser));
661
662 PDMDevHlpCritSectEnter(pDevIns, pDevIns->pCritSectRoR3, VERR_IGNORED);
663 ch->pfnXferHandler = pfnTransferHandler;
664 ch->pvUser = pvUser;
665 PDMDevHlpCritSectLeave(pDevIns, pDevIns->pCritSectRoR3);
666}
667
668/** Reverse the order of bytes in a memory buffer. */
669static void dmaReverseBuf8(void *buf, unsigned len)
670{
671 uint8_t *pBeg, *pEnd;
672 uint8_t temp;
673
674 pBeg = (uint8_t *)buf;
675 pEnd = pBeg + len - 1;
676 for (len = len / 2; len; --len)
677 {
678 temp = *pBeg;
679 *pBeg++ = *pEnd;
680 *pEnd-- = temp;
681 }
682}
683
684/** Reverse the order of words in a memory buffer. */
685static void dmaReverseBuf16(void *buf, unsigned len)
686{
687 uint16_t *pBeg, *pEnd;
688 uint16_t temp;
689
690 Assert(!(len & 1));
691 len /= 2; /* Convert to word count. */
692 pBeg = (uint16_t *)buf;
693 pEnd = pBeg + len - 1;
694 for (len = len / 2; len; --len)
695 {
696 temp = *pBeg;
697 *pBeg++ = *pEnd;
698 *pEnd-- = temp;
699 }
700}
701
702/**
703 * @interface_method_impl{PDMDMAREG,pfnReadMemory}
704 */
705static DECLCALLBACK(uint32_t) dmaR3ReadMemory(PPDMDEVINS pDevIns, unsigned uChannel,
706 void *pvBuffer, uint32_t off, uint32_t cbBlock)
707{
708 DMAState *pThis = PDMDEVINS_2_DATA(pDevIns, PDMASTATE);
709 DMAControl *dc = &pThis->DMAC[DMACH2C(uChannel)];
710 DMAChannel *ch = &dc->ChState[uChannel & 3];
711 uint32_t page, pagehi;
712 uint32_t addr;
713
714 LogFlow(("dmaR3ReadMemory: pThis=%p uChannel=%u pvBuffer=%p off=%u cbBlock=%u\n", pThis, uChannel, pvBuffer, off, cbBlock));
715
716 PDMDevHlpCritSectEnter(pDevIns, pDevIns->pCritSectRoR3, VERR_IGNORED);
717
718 /* Build the address for this transfer. */
719 page = dc->au8Page[DMACH2PG(uChannel)] & ~dc->is16bit;
720 pagehi = dc->au8PageHi[DMACH2PG(uChannel)];
721 addr = (pagehi << 24) | (page << 16) | (ch->u16CurAddr << dc->is16bit);
722
723 if (IS_MODE_DEC(ch->u8Mode))
724 {
725 PDMDevHlpPhysRead(pThis->pDevIns, addr - off - cbBlock, pvBuffer, cbBlock);
726 if (dc->is16bit)
727 dmaReverseBuf16(pvBuffer, cbBlock);
728 else
729 dmaReverseBuf8(pvBuffer, cbBlock);
730 }
731 else
732 PDMDevHlpPhysRead(pThis->pDevIns, addr + off, pvBuffer, cbBlock);
733
734 PDMDevHlpCritSectLeave(pDevIns, pDevIns->pCritSectRoR3);
735 return cbBlock;
736}
737
738/**
739 * @interface_method_impl{PDMDMAREG,pfnWriteMemory}
740 */
741static DECLCALLBACK(uint32_t) dmaR3WriteMemory(PPDMDEVINS pDevIns, unsigned uChannel,
742 const void *pvBuffer, uint32_t off, uint32_t cbBlock)
743{
744 DMAState *pThis = PDMDEVINS_2_DATA(pDevIns, PDMASTATE);
745 DMAControl *dc = &pThis->DMAC[DMACH2C(uChannel)];
746 DMAChannel *ch = &dc->ChState[uChannel & 3];
747 uint32_t page, pagehi;
748 uint32_t addr;
749
750 LogFlow(("dmaR3WriteMemory: pThis=%p uChannel=%u pvBuffer=%p off=%u cbBlock=%u\n", pThis, uChannel, pvBuffer, off, cbBlock));
751 if (GET_MODE_XTYP(ch->u8Mode) == DTYPE_VERIFY)
752 {
753 Log(("DMA verify transfer, ignoring write.\n"));
754 return cbBlock;
755 }
756
757 PDMDevHlpCritSectEnter(pDevIns, pDevIns->pCritSectRoR3, VERR_IGNORED);
758
759 /* Build the address for this transfer. */
760 page = dc->au8Page[DMACH2PG(uChannel)] & ~dc->is16bit;
761 pagehi = dc->au8PageHi[DMACH2PG(uChannel)];
762 addr = (pagehi << 24) | (page << 16) | (ch->u16CurAddr << dc->is16bit);
763
764 if (IS_MODE_DEC(ch->u8Mode))
765 {
766 /// @todo This would need a temporary buffer.
767 Assert(0);
768#if 0
769 if (dc->is16bit)
770 dmaReverseBuf16(pvBuffer, cbBlock);
771 else
772 dmaReverseBuf8(pvBuffer, cbBlock);
773#endif
774 PDMDevHlpPhysWrite(pThis->pDevIns, addr - off - cbBlock, pvBuffer, cbBlock);
775 }
776 else
777 PDMDevHlpPhysWrite(pThis->pDevIns, addr + off, pvBuffer, cbBlock);
778
779 PDMDevHlpCritSectLeave(pDevIns, pDevIns->pCritSectRoR3);
780 return cbBlock;
781}
782
783/**
784 * @interface_method_impl{PDMDMAREG,pfnSetDREQ}
785 */
786static DECLCALLBACK(void) dmaR3SetDREQ(PPDMDEVINS pDevIns, unsigned uChannel, unsigned uLevel)
787{
788 DMAState *pThis = PDMDEVINS_2_DATA(pDevIns, PDMASTATE);
789 DMAControl *dc = &pThis->DMAC[DMACH2C(uChannel)];
790 int chidx;
791
792 LogFlow(("dmaR3SetDREQ: pThis=%p uChannel=%u uLevel=%u\n", pThis, uChannel, uLevel));
793
794 PDMDevHlpCritSectEnter(pDevIns, pDevIns->pCritSectRoR3, VERR_IGNORED);
795 chidx = uChannel & 3;
796 if (uLevel)
797 dc->u8Status |= 1 << (chidx + 4);
798 else
799 dc->u8Status &= ~(1 << (chidx + 4));
800 PDMDevHlpCritSectLeave(pDevIns, pDevIns->pCritSectRoR3);
801}
802
803/**
804 * @interface_method_impl{PDMDMAREG,pfnGetChannelMode}
805 */
806static DECLCALLBACK(uint8_t) dmaR3GetChannelMode(PPDMDEVINS pDevIns, unsigned uChannel)
807{
808 PDMASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDMASTATE);
809
810 LogFlow(("dmaR3GetChannelMode: pThis=%p uChannel=%u\n", pThis, uChannel));
811
812 PDMDevHlpCritSectEnter(pDevIns, pDevIns->pCritSectRoR3, VERR_IGNORED);
813 uint8_t u8Mode = pThis->DMAC[DMACH2C(uChannel)].ChState[uChannel & 3].u8Mode;
814 PDMDevHlpCritSectLeave(pDevIns, pDevIns->pCritSectRoR3);
815 return u8Mode;
816}
817
818
819static void dmaR3SaveController(PCPDMDEVHLPR3 pHlp, PSSMHANDLE pSSM, DMAControl *dc)
820{
821 /* Save controller state... */
822 pHlp->pfnSSMPutU8(pSSM, dc->u8Command);
823 pHlp->pfnSSMPutU8(pSSM, dc->u8Mask);
824 pHlp->pfnSSMPutU8(pSSM, dc->fHiByte);
825 pHlp->pfnSSMPutU32(pSSM, dc->is16bit);
826 pHlp->pfnSSMPutU8(pSSM, dc->u8Status);
827 pHlp->pfnSSMPutU8(pSSM, dc->u8Temp);
828 pHlp->pfnSSMPutU8(pSSM, dc->u8ModeCtr);
829 pHlp->pfnSSMPutMem(pSSM, &dc->au8Page, sizeof(dc->au8Page));
830 pHlp->pfnSSMPutMem(pSSM, &dc->au8PageHi, sizeof(dc->au8PageHi));
831
832 /* ...and all four of its channels. */
833 for (unsigned chidx = 0; chidx < RT_ELEMENTS(dc->ChState); ++chidx)
834 {
835 DMAChannel *ch = &dc->ChState[chidx];
836
837 pHlp->pfnSSMPutU16(pSSM, ch->u16CurAddr);
838 pHlp->pfnSSMPutU16(pSSM, ch->u16CurCount);
839 pHlp->pfnSSMPutU16(pSSM, ch->u16BaseAddr);
840 pHlp->pfnSSMPutU16(pSSM, ch->u16BaseCount);
841 pHlp->pfnSSMPutU8(pSSM, ch->u8Mode);
842 }
843}
844
845static int dmaR3LoadController(PCPDMDEVHLPR3 pHlp, PSSMHANDLE pSSM, DMAControl *dc, int version)
846{
847 uint8_t u8val;
848 uint32_t u32val;
849
850 pHlp->pfnSSMGetU8(pSSM, &dc->u8Command);
851 pHlp->pfnSSMGetU8(pSSM, &dc->u8Mask);
852 pHlp->pfnSSMGetU8(pSSM, &u8val);
853 dc->fHiByte = !!u8val;
854 pHlp->pfnSSMGetU32(pSSM, &dc->is16bit);
855 if (version > DMA_SAVESTATE_OLD)
856 {
857 pHlp->pfnSSMGetU8(pSSM, &dc->u8Status);
858 pHlp->pfnSSMGetU8(pSSM, &dc->u8Temp);
859 pHlp->pfnSSMGetU8(pSSM, &dc->u8ModeCtr);
860 pHlp->pfnSSMGetMem(pSSM, &dc->au8Page, sizeof(dc->au8Page));
861 pHlp->pfnSSMGetMem(pSSM, &dc->au8PageHi, sizeof(dc->au8PageHi));
862 }
863
864 for (unsigned chidx = 0; chidx < RT_ELEMENTS(dc->ChState); ++chidx)
865 {
866 DMAChannel *ch = &dc->ChState[chidx];
867
868 if (version == DMA_SAVESTATE_OLD)
869 {
870 /* Convert from 17-bit to 16-bit format. */
871 pHlp->pfnSSMGetU32(pSSM, &u32val);
872 ch->u16CurAddr = u32val >> dc->is16bit;
873 pHlp->pfnSSMGetU32(pSSM, &u32val);
874 ch->u16CurCount = u32val >> dc->is16bit;
875 }
876 else
877 {
878 pHlp->pfnSSMGetU16(pSSM, &ch->u16CurAddr);
879 pHlp->pfnSSMGetU16(pSSM, &ch->u16CurCount);
880 }
881 pHlp->pfnSSMGetU16(pSSM, &ch->u16BaseAddr);
882 pHlp->pfnSSMGetU16(pSSM, &ch->u16BaseCount);
883 pHlp->pfnSSMGetU8(pSSM, &ch->u8Mode);
884 /* Convert from old save state. */
885 if (version == DMA_SAVESTATE_OLD)
886 {
887 /* Remap page register contents. */
888 pHlp->pfnSSMGetU8(pSSM, &u8val);
889 dc->au8Page[DMACX2PG(chidx)] = u8val;
890 pHlp->pfnSSMGetU8(pSSM, &u8val);
891 dc->au8PageHi[DMACX2PG(chidx)] = u8val;
892 /* Throw away dack, eop. */
893 pHlp->pfnSSMGetU8(pSSM, &u8val);
894 pHlp->pfnSSMGetU8(pSSM, &u8val);
895 }
896 }
897 return 0;
898}
899
900/** @callback_method_impl{FNSSMDEVSAVEEXEC} */
901static DECLCALLBACK(int) dmaR3SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
902{
903 PDMASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDMASTATE);
904 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
905
906 dmaR3SaveController(pHlp, pSSM, &pThis->DMAC[0]);
907 dmaR3SaveController(pHlp, pSSM, &pThis->DMAC[1]);
908 return VINF_SUCCESS;
909}
910
911/** @callback_method_impl{FNSSMDEVLOADEXEC} */
912static DECLCALLBACK(int) dmaR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
913{
914 PDMASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDMASTATE);
915 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
916
917 AssertMsgReturn(uVersion <= DMA_SAVESTATE_CURRENT, ("%d\n", uVersion), VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION);
918 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
919
920 dmaR3LoadController(pHlp, pSSM, &pThis->DMAC[0], uVersion);
921 return dmaR3LoadController(pHlp, pSSM, &pThis->DMAC[1], uVersion);
922}
923
924/** @callback_method_impl{FNDBGFHANDLERDEV} */
925static DECLCALLBACK(void) dmaR3Info(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
926{
927 PDMASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDMASTATE);
928 NOREF(pszArgs);
929
930 /*
931 * Show info.
932 */
933 for (unsigned i = 0; i < RT_ELEMENTS(pThis->DMAC); i++)
934 {
935 PDMACONTROLLER pDmac = &pThis->DMAC[i];
936
937 pHlp->pfnPrintf(pHlp, "\nDMAC%d:\n", i);
938 pHlp->pfnPrintf(pHlp, " Status : %02X - DRQ 3210 TC 3210\n", pDmac->u8Status);
939 pHlp->pfnPrintf(pHlp, " %u%u%u%u %u%u%u%u\n",
940 !!(pDmac->u8Status & RT_BIT(7)), !!(pDmac->u8Status & RT_BIT(6)),
941 !!(pDmac->u8Status & RT_BIT(5)), !!(pDmac->u8Status & RT_BIT(4)),
942 !!(pDmac->u8Status & RT_BIT(3)), !!(pDmac->u8Status & RT_BIT(2)),
943 !!(pDmac->u8Status & RT_BIT(1)), !!(pDmac->u8Status & RT_BIT(0)));
944 pHlp->pfnPrintf(pHlp, " Mask : %02X - Chn 3210\n", pDmac->u8Mask);
945 pHlp->pfnPrintf(pHlp, " %u%u%u%u\n",
946 !!(pDmac->u8Mask & RT_BIT(3)), !!(pDmac->u8Mask & RT_BIT(2)),
947 !!(pDmac->u8Mask & RT_BIT(1)), !!(pDmac->u8Mask & RT_BIT(0)));
948 pHlp->pfnPrintf(pHlp, " Temp : %02x\n", pDmac->u8Temp);
949 pHlp->pfnPrintf(pHlp, " Command: %02X\n", pDmac->u8Command);
950 pHlp->pfnPrintf(pHlp, " DACK: active %s DREQ: active %s\n",
951 pDmac->u8Command & RT_BIT(7) ? "high" : "low ",
952 pDmac->u8Command & RT_BIT(6) ? "high" : "low ");
953 pHlp->pfnPrintf(pHlp, " Extended write: %s Priority: %s\n",
954 pDmac->u8Command & RT_BIT(5) ? "enabled " : "disabled",
955 pDmac->u8Command & RT_BIT(4) ? "rotating" : "fixed ");
956 pHlp->pfnPrintf(pHlp, " Timing: %s Controller: %s\n",
957 pDmac->u8Command & RT_BIT(3) ? "normal " : "compressed",
958 pDmac->u8Command & RT_BIT(2) ? "enabled " : "disabled");
959 pHlp->pfnPrintf(pHlp, " Adress Hold: %s Mem-to-Mem Ch 0/1: %s\n",
960 pDmac->u8Command & RT_BIT(1) ? "enabled " : "disabled",
961 pDmac->u8Command & RT_BIT(0) ? "enabled " : "disabled");
962
963 for (unsigned ch = 0; ch < RT_ELEMENTS(pDmac->ChState); ch++)
964 {
965 PDMACHANNEL pChan = &pDmac->ChState[ch];
966 const char *apszChanMode[] = { "demand ", "single ", "block ", "cascade" };
967 const char *apszChanType[] = { "verify ", "write ", "read ", "illegal" };
968
969 pHlp->pfnPrintf(pHlp, "\n DMA Channel %d: Page:%02X\n",
970 ch, pDmac->au8Page[DMACX2PG(ch)]);
971 pHlp->pfnPrintf(pHlp, " Mode : %02X Auto-init: %s %screment\n",
972 pChan->u8Mode, pChan->u8Mode & RT_BIT(4) ? "yes" : "no",
973 pChan->u8Mode & RT_BIT(5) ? "De" : "In" );
974 pHlp->pfnPrintf(pHlp, " Xfer Type: %s Mode: %s\n",
975 apszChanType[((pChan->u8Mode >> 2) & 3)],
976 apszChanMode[((pChan->u8Mode >> 6) & 3)]);
977 pHlp->pfnPrintf(pHlp, " Base address:%04X count:%04X\n",
978 pChan->u16BaseAddr, pChan->u16BaseCount);
979 pHlp->pfnPrintf(pHlp, " Current address:%04X count:%04X\n",
980 pChan->u16CurAddr, pChan->u16CurCount);
981 }
982 }
983}
984
985/**
986 * @interface_method_impl{PDMDEVREG,pfnReset}
987 */
988static DECLCALLBACK(void) dmaR3Reset(PPDMDEVINS pDevIns)
989{
990 PDMASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDMASTATE);
991
992 LogFlow(("dmaR3Reset: pThis=%p\n", pThis));
993
994 /* NB: The page and address registers are unaffected by a reset
995 * and in an undefined state after power-up.
996 */
997 dmaClear(&pThis->DMAC[0]);
998 dmaClear(&pThis->DMAC[1]);
999}
1000
1001/**
1002 * @interface_method_impl{PDMDEVREG,pfnConstruct}
1003 */
1004static DECLCALLBACK(int) dmaR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
1005{
1006 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
1007 PDMASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDMASTATE);
1008 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1009 RT_NOREF(iInstance);
1010
1011 /*
1012 * Initialize data.
1013 */
1014 pThis->pDevIns = pDevIns;
1015
1016 DMAControl *pDC8 = &pThis->DMAC[0];
1017 DMAControl *pDC16 = &pThis->DMAC[1];
1018 pDC8->is16bit = false;
1019 pDC16->is16bit = true;
1020
1021 /*
1022 * Validate and read the configuration.
1023 */
1024 PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns, "HighPageEnable", "");
1025
1026 bool fHighPage = false;
1027 int rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "HighPageEnable", &fHighPage, false);
1028 AssertRCReturn(rc, rc);
1029
1030 /*
1031 * Register I/O callbacks.
1032 */
1033 /* Base and current address for each channel. */
1034 rc = PDMDevHlpIoPortCreateUAndMap(pDevIns, 0x00, 8, dmaWriteAddr, dmaReadAddr, pDC8, "DMA8 Address", NULL, &pDC8->hIoPortBase);
1035 AssertLogRelRCReturn(rc, rc);
1036 rc = PDMDevHlpIoPortCreateUAndMap(pDevIns, 0xc0, 16, dmaWriteAddr, dmaReadAddr, pDC16, "DMA16 Address", NULL, &pDC16->hIoPortBase);
1037 AssertLogRelRCReturn(rc, rc);
1038
1039 /* Control registers for both DMA controllers. */
1040 rc = PDMDevHlpIoPortCreateUAndMap(pDevIns, 0x08, 8, dmaWriteCtl, dmaReadCtl, pDC8, "DMA8 Control", NULL, &pDC8->hIoPortCtl);
1041 AssertLogRelRCReturn(rc, rc);
1042 rc = PDMDevHlpIoPortCreateUAndMap(pDevIns, 0xd0, 16, dmaWriteCtl, dmaReadCtl, pDC16, "DMA16 Control", NULL, &pDC16->hIoPortCtl);
1043 AssertLogRelRCReturn(rc, rc);
1044
1045 /* Page registers for each channel (plus a few unused ones). */
1046 rc = PDMDevHlpIoPortCreateUAndMap(pDevIns, 0x80, 8, dmaWritePage, dmaReadPage, pDC8, "DMA8 Page", NULL, &pDC8->hIoPortPage);
1047 AssertLogRelRCReturn(rc, rc);
1048 rc = PDMDevHlpIoPortCreateUAndMap(pDevIns, 0x88, 8, dmaWritePage, dmaReadPage, pDC16, "DMA16 Page", NULL, &pDC16->hIoPortPage);
1049 AssertLogRelRCReturn(rc, rc);
1050
1051 /* Optional EISA style high page registers (address bits 24-31). */
1052 if (fHighPage)
1053 {
1054 rc = PDMDevHlpIoPortCreateUAndMap(pDevIns, 0x480, 8, dmaWriteHiPage, dmaReadHiPage, pDC8, "DMA8 Page High", NULL, &pDC8->hIoPortHi);
1055 AssertLogRelRCReturn(rc, rc);
1056 rc = PDMDevHlpIoPortCreateUAndMap(pDevIns, 0x488, 8, dmaWriteHiPage, dmaReadHiPage, pDC16, "DMA16 Page High", NULL, &pDC16->hIoPortHi);
1057 AssertLogRelRCReturn(rc, rc);
1058 }
1059 else
1060 {
1061 pDC8->hIoPortHi = NIL_IOMIOPORTHANDLE;
1062 pDC16->hIoPortHi = NIL_IOMIOPORTHANDLE;
1063 }
1064
1065 /*
1066 * Reset controller state.
1067 */
1068 dmaR3Reset(pDevIns);
1069
1070 /*
1071 * Register ourselves with PDM as the DMA controller.
1072 */
1073 PDMDMACREG Reg;
1074 Reg.u32Version = PDM_DMACREG_VERSION;
1075 Reg.pfnRun = dmaR3Run;
1076 Reg.pfnRegister = dmaR3Register;
1077 Reg.pfnReadMemory = dmaR3ReadMemory;
1078 Reg.pfnWriteMemory = dmaR3WriteMemory;
1079 Reg.pfnSetDREQ = dmaR3SetDREQ;
1080 Reg.pfnGetChannelMode = dmaR3GetChannelMode;
1081
1082 rc = PDMDevHlpDMACRegister(pDevIns, &Reg, &pThis->pHlp);
1083 AssertRCReturn(rc, rc);
1084
1085 /*
1086 * Register the saved state.
1087 */
1088 rc = PDMDevHlpSSMRegister(pDevIns, DMA_SAVESTATE_CURRENT, sizeof(*pThis), dmaR3SaveExec, dmaR3LoadExec);
1089 AssertRCReturn(rc, rc);
1090
1091 /*
1092 * Statistics.
1093 */
1094 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRun, STAMTYPE_PROFILE, "DmaRun", STAMUNIT_TICKS_PER_CALL, "Profiling dmaR3Run().");
1095
1096 /*
1097 * Register the info item.
1098 */
1099 PDMDevHlpDBGFInfoRegister(pDevIns, "dmac", "DMA controller info.", dmaR3Info);
1100
1101 return VINF_SUCCESS;
1102}
1103
1104#else /* !IN_RING3 */
1105
1106/**
1107 * @callback_method_impl{PDMDEVREGR0,pfnConstruct}
1108 */
1109static DECLCALLBACK(int) dmaRZConstruct(PPDMDEVINS pDevIns)
1110{
1111 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
1112 PDMASTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDMASTATE);
1113 int rc;
1114
1115 for (unsigned i = 0; i < RT_ELEMENTS(pThis->DMAC); i++)
1116 {
1117 PDMACONTROLLER pCtl = &pThis->DMAC[i];
1118
1119 rc = PDMDevHlpIoPortSetUpContext(pDevIns, pCtl->hIoPortBase, dmaWriteAddr, dmaReadAddr, pCtl);
1120 AssertLogRelRCReturn(rc, rc);
1121
1122 rc = PDMDevHlpIoPortSetUpContext(pDevIns, pCtl->hIoPortCtl, dmaWriteCtl, dmaReadCtl, pCtl);
1123 AssertLogRelRCReturn(rc, rc);
1124
1125 rc = PDMDevHlpIoPortSetUpContext(pDevIns, pCtl->hIoPortPage, dmaWritePage, dmaReadPage, pCtl);
1126 AssertLogRelRCReturn(rc, rc);
1127
1128 if (pCtl->hIoPortHi != NIL_IOMIOPORTHANDLE)
1129 {
1130 rc = PDMDevHlpIoPortSetUpContext(pDevIns, pCtl->hIoPortHi, dmaWriteHiPage, dmaReadHiPage, pCtl);
1131 AssertLogRelRCReturn(rc, rc);
1132 }
1133 }
1134
1135 return VINF_SUCCESS;
1136}
1137
1138#endif /* !IN_RING3 */
1139
1140/**
1141 * The device registration structure.
1142 */
1143const PDMDEVREG g_DeviceDMA =
1144{
1145 /* .u32Version = */ PDM_DEVREG_VERSION,
1146 /* .uReserved0 = */ 0,
1147 /* .szName = */ "8237A",
1148 /* .fFlags = */ PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RZ | PDM_DEVREG_FLAGS_NEW_STYLE,
1149 /* .fClass = */ PDM_DEVREG_CLASS_DMA,
1150 /* .cMaxInstances = */ 1,
1151 /* .uSharedVersion = */ 42,
1152 /* .cbInstanceShared = */ sizeof(DMAState),
1153 /* .cbInstanceCC = */ 0,
1154 /* .cbInstanceRC = */ 0,
1155 /* .cMaxPciDevices = */ 0,
1156 /* .cMaxMsixVectors = */ 0,
1157 /* .pszDescription = */ "DMA Controller Device",
1158#if defined(IN_RING3)
1159 /* .pszRCMod = */ "VBoxDDRC.rc",
1160 /* .pszR0Mod = */ "VBoxDDR0.r0",
1161 /* .pfnConstruct = */ dmaR3Construct,
1162 /* .pfnDestruct = */ NULL,
1163 /* .pfnRelocate = */ NULL,
1164 /* .pfnMemSetup = */ NULL,
1165 /* .pfnPowerOn = */ NULL,
1166 /* .pfnReset = */ dmaR3Reset,
1167 /* .pfnSuspend = */ NULL,
1168 /* .pfnResume = */ NULL,
1169 /* .pfnAttach = */ NULL,
1170 /* .pfnDetach = */ NULL,
1171 /* .pfnQueryInterface = */ NULL,
1172 /* .pfnInitComplete = */ NULL,
1173 /* .pfnPowerOff = */ NULL,
1174 /* .pfnSoftReset = */ NULL,
1175 /* .pfnReserved0 = */ NULL,
1176 /* .pfnReserved1 = */ NULL,
1177 /* .pfnReserved2 = */ NULL,
1178 /* .pfnReserved3 = */ NULL,
1179 /* .pfnReserved4 = */ NULL,
1180 /* .pfnReserved5 = */ NULL,
1181 /* .pfnReserved6 = */ NULL,
1182 /* .pfnReserved7 = */ NULL,
1183#elif defined(IN_RING0)
1184 /* .pfnEarlyConstruct = */ NULL,
1185 /* .pfnConstruct = */ dmaRZConstruct,
1186 /* .pfnDestruct = */ NULL,
1187 /* .pfnFinalDestruct = */ NULL,
1188 /* .pfnRequest = */ NULL,
1189 /* .pfnReserved0 = */ NULL,
1190 /* .pfnReserved1 = */ NULL,
1191 /* .pfnReserved2 = */ NULL,
1192 /* .pfnReserved3 = */ NULL,
1193 /* .pfnReserved4 = */ NULL,
1194 /* .pfnReserved5 = */ NULL,
1195 /* .pfnReserved6 = */ NULL,
1196 /* .pfnReserved7 = */ NULL,
1197#elif defined(IN_RC)
1198 /* .pfnConstruct = */ dmaRZConstruct,
1199 /* .pfnReserved0 = */ NULL,
1200 /* .pfnReserved1 = */ NULL,
1201 /* .pfnReserved2 = */ NULL,
1202 /* .pfnReserved3 = */ NULL,
1203 /* .pfnReserved4 = */ NULL,
1204 /* .pfnReserved5 = */ NULL,
1205 /* .pfnReserved6 = */ NULL,
1206 /* .pfnReserved7 = */ NULL,
1207#else
1208# error "Not in IN_RING3, IN_RING0 or IN_RC!"
1209#endif
1210 /* .u32VersionEnd = */ PDM_DEVREG_VERSION
1211};
1212
1213#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
1214
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette