VirtualBox

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

Last change on this file since 64369 was 64369, checked in by vboxsync, 8 years ago

Devices/PC: Doxygen fixes

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