4 * Copyright (c) 2003-2004 Vassili Karpov (malc)
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 /* #define DEBUG_DMA */
28 #define dolog(...) fprintf (stderr, "dma: " __VA_ARGS__)
30 #define lwarn(...) fprintf (stderr, "dma: " __VA_ARGS__)
31 #define linfo(...) fprintf (stderr, "dma: " __VA_ARGS__)
32 #define ldebug(...) fprintf (stderr, "dma: " __VA_ARGS__)
39 #define LENOFA(a) ((int) (sizeof(a)/sizeof(a[0])))
49 DMA_transfer_handler transfer_handler
;
56 static struct dma_cont
{
62 struct dma_regs regs
[4];
66 CMD_MEMORY_TO_MEMORY
= 0x01,
67 CMD_FIXED_ADDRESS
= 0x02,
68 CMD_BLOCK_CONTROLLER
= 0x04,
69 CMD_COMPRESSED_TIME
= 0x08,
70 CMD_CYCLIC_PRIORITY
= 0x10,
71 CMD_EXTENDED_WRITE
= 0x20,
74 CMD_NOT_SUPPORTED
= CMD_MEMORY_TO_MEMORY
| CMD_FIXED_ADDRESS
75 | CMD_COMPRESSED_TIME
| CMD_CYCLIC_PRIORITY
| CMD_EXTENDED_WRITE
76 | CMD_LOW_DREQ
| CMD_LOW_DACK
80 static int channels
[8] = {-1, 2, 3, 1, -1, -1, -1, 0};
82 static void write_page (void *opaque
, uint32_t nport
, uint32_t data
)
84 struct dma_cont
*d
= opaque
;
87 ichan
= channels
[nport
& 7];
89 dolog ("invalid channel %#x %#x\n", nport
, data
);
92 d
->regs
[ichan
].page
= data
;
95 static void write_pageh (void *opaque
, uint32_t nport
, uint32_t data
)
97 struct dma_cont
*d
= opaque
;
100 ichan
= channels
[nport
& 7];
102 dolog ("invalid channel %#x %#x\n", nport
, data
);
105 d
->regs
[ichan
].pageh
= data
;
108 static uint32_t read_page (void *opaque
, uint32_t nport
)
110 struct dma_cont
*d
= opaque
;
113 ichan
= channels
[nport
& 7];
115 dolog ("invalid channel read %#x\n", nport
);
118 return d
->regs
[ichan
].page
;
121 static uint32_t read_pageh (void *opaque
, uint32_t nport
)
123 struct dma_cont
*d
= opaque
;
126 ichan
= channels
[nport
& 7];
128 dolog ("invalid channel read %#x\n", nport
);
131 return d
->regs
[ichan
].pageh
;
134 static inline void init_chan (struct dma_cont
*d
, int ichan
)
139 r
->now
[ADDR
] = r
->base
[ADDR
] << d
->dshift
;
143 static inline int getff (struct dma_cont
*d
)
152 static uint32_t read_chan (void *opaque
, uint32_t nport
)
154 struct dma_cont
*d
= opaque
;
155 int ichan
, nreg
, iport
, ff
, val
, dir
;
158 iport
= (nport
>> d
->dshift
) & 0x0f;
163 dir
= ((r
->mode
>> 5) & 1) ? -1 : 1;
166 val
= (r
->base
[COUNT
] << d
->dshift
) - r
->now
[COUNT
];
168 val
= r
->now
[ADDR
] + r
->now
[COUNT
] * dir
;
170 ldebug ("read_chan %#x -> %d\n", iport
, val
);
171 return (val
>> (d
->dshift
+ (ff
<< 3))) & 0xff;
174 static void write_chan (void *opaque
, uint32_t nport
, uint32_t data
)
176 struct dma_cont
*d
= opaque
;
177 int iport
, ichan
, nreg
;
180 iport
= (nport
>> d
->dshift
) & 0x0f;
185 r
->base
[nreg
] = (r
->base
[nreg
] & 0xff) | ((data
<< 8) & 0xff00);
186 init_chan (d
, ichan
);
188 r
->base
[nreg
] = (r
->base
[nreg
] & 0xff00) | (data
& 0xff);
192 static void write_cont (void *opaque
, uint32_t nport
, uint32_t data
)
194 struct dma_cont
*d
= opaque
;
195 int iport
, ichan
= 0;
197 iport
= (nport
>> d
->dshift
) & 0x0f;
199 case 0x08: /* command */
200 if ((data
!= 0) && (data
& CMD_NOT_SUPPORTED
)) {
201 dolog ("command %#x not supported\n", data
);
210 d
->status
|= 1 << (ichan
+ 4);
213 d
->status
&= ~(1 << (ichan
+ 4));
215 d
->status
&= ~(1 << ichan
);
218 case 0x0a: /* single mask */
220 d
->mask
|= 1 << (data
& 3);
222 d
->mask
&= ~(1 << (data
& 3));
225 case 0x0b: /* mode */
230 int op
, ai
, dir
, opmode
;
231 op
= (data
>> 2) & 3;
232 ai
= (data
>> 4) & 1;
233 dir
= (data
>> 5) & 1;
234 opmode
= (data
>> 6) & 3;
236 linfo ("ichan %d, op %d, ai %d, dir %d, opmode %d\n",
237 ichan
, op
, ai
, dir
, opmode
);
240 d
->regs
[ichan
].mode
= data
;
244 case 0x0c: /* clear flip flop */
248 case 0x0d: /* reset */
255 case 0x0e: /* clear mask for all channels */
259 case 0x0f: /* write mask for all channels */
264 dolog ("unknown iport %#x\n", iport
);
270 linfo ("write_cont: nport %#06x, ichan % 2d, val %#06x\n",
276 static uint32_t read_cont (void *opaque
, uint32_t nport
)
278 struct dma_cont
*d
= opaque
;
281 iport
= (nport
>> d
->dshift
) & 0x0f;
283 case 0x08: /* status */
287 case 0x0f: /* mask */
295 ldebug ("read_cont: nport %#06x, iport %#04x val %#x\n", nport
, iport
, val
);
299 int DMA_get_channel_mode (int nchan
)
301 return dma_controllers
[nchan
> 3].regs
[nchan
& 3].mode
;
304 void DMA_hold_DREQ (int nchan
)
310 linfo ("held cont=%d chan=%d\n", ncont
, ichan
);
311 dma_controllers
[ncont
].status
|= 1 << (ichan
+ 4);
314 void DMA_release_DREQ (int nchan
)
320 linfo ("released cont=%d chan=%d\n", ncont
, ichan
);
321 dma_controllers
[ncont
].status
&= ~(1 << (ichan
+ 4));
324 static void channel_run (int ncont
, int ichan
)
327 struct dma_regs
*r
= &dma_controllers
[ncont
].regs
[ichan
];
331 dir
= (r
->mode
>> 5) & 1;
332 opmode
= (r
->mode
>> 6) & 3;
335 dolog ("DMA in address decrement mode\n");
338 dolog ("DMA not in single mode select %#x\n", opmode
);
342 r
= dma_controllers
[ncont
].regs
+ ichan
;
343 n
= r
->transfer_handler (r
->opaque
, ichan
+ (ncont
<< 2),
344 r
->now
[COUNT
], (r
->base
[COUNT
] + 1) << ncont
);
346 ldebug ("dma_pos %d size %d\n", n
, (r
->base
[COUNT
] + 1) << ncont
);
356 for (icont
= 0; icont
< 2; icont
++, d
++) {
357 for (ichan
= 0; ichan
< 4; ichan
++) {
362 if ((0 == (d
->mask
& mask
)) && (0 != (d
->status
& (mask
<< 4))))
363 channel_run (icont
, ichan
);
368 void DMA_register_channel (int nchan
,
369 DMA_transfer_handler transfer_handler
,
378 r
= dma_controllers
[ncont
].regs
+ ichan
;
379 r
->transfer_handler
= transfer_handler
;
383 int DMA_read_memory (int nchan
, void *buf
, int pos
, int len
)
385 struct dma_regs
*r
= &dma_controllers
[nchan
> 3].regs
[nchan
& 3];
386 target_phys_addr_t addr
= ((r
->pageh
& 0x7f) << 24) | (r
->page
<< 16) | r
->now
[ADDR
];
388 if (r
->mode
& 0x20) {
392 cpu_physical_memory_read (addr
- pos
- len
, buf
, len
);
393 /* What about 16bit transfers? */
394 for (i
= 0; i
< len
>> 1; i
++) {
395 uint8_t b
= p
[len
- i
- 1];
400 cpu_physical_memory_read (addr
+ pos
, buf
, len
);
405 int DMA_write_memory (int nchan
, void *buf
, int pos
, int len
)
407 struct dma_regs
*r
= &dma_controllers
[nchan
> 3].regs
[nchan
& 3];
408 target_phys_addr_t addr
= ((r
->pageh
& 0x7f) << 24) | (r
->page
<< 16) | r
->now
[ADDR
];
410 if (r
->mode
& 0x20) {
414 cpu_physical_memory_write (addr
- pos
- len
, buf
, len
);
415 /* What about 16bit transfers? */
416 for (i
= 0; i
< len
; i
++) {
417 uint8_t b
= p
[len
- i
- 1];
422 cpu_physical_memory_write (addr
+ pos
, buf
, len
);
427 /* request the emulator to transfer a new DMA memory block ASAP */
428 void DMA_schedule(int nchan
)
430 CPUState
*env
= cpu_single_env
;
432 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
435 static void dma_reset(void *opaque
)
437 struct dma_cont
*d
= opaque
;
438 write_cont (d
, (0x0d << d
->dshift
), 0);
441 /* dshift = 0: 8 bit DMA, 1 = 16 bit DMA */
442 static void dma_init2(struct dma_cont
*d
, int base
, int dshift
,
443 int page_base
, int pageh_base
)
445 const static int page_port_list
[] = { 0x1, 0x2, 0x3, 0x7 };
449 for (i
= 0; i
< 8; i
++) {
450 register_ioport_write (base
+ (i
<< dshift
), 1, 1, write_chan
, d
);
451 register_ioport_read (base
+ (i
<< dshift
), 1, 1, read_chan
, d
);
453 for (i
= 0; i
< LENOFA (page_port_list
); i
++) {
454 register_ioport_write (page_base
+ page_port_list
[i
], 1, 1,
456 register_ioport_read (page_base
+ page_port_list
[i
], 1, 1,
458 if (pageh_base
>= 0) {
459 register_ioport_write (pageh_base
+ page_port_list
[i
], 1, 1,
461 register_ioport_read (pageh_base
+ page_port_list
[i
], 1, 1,
465 for (i
= 0; i
< 8; i
++) {
466 register_ioport_write (base
+ ((i
+ 8) << dshift
), 1, 1,
468 register_ioport_read (base
+ ((i
+ 8) << dshift
), 1, 1,
471 qemu_register_reset(dma_reset
, d
);
475 static void dma_save (QEMUFile
*f
, void *opaque
)
477 struct dma_cont
*d
= opaque
;
480 /* qemu_put_8s (f, &d->status); */
481 qemu_put_8s (f
, &d
->command
);
482 qemu_put_8s (f
, &d
->mask
);
483 qemu_put_8s (f
, &d
->flip_flop
);
484 qemu_put_be32s (f
, &d
->dshift
);
486 for (i
= 0; i
< 4; ++i
) {
487 struct dma_regs
*r
= &d
->regs
[i
];
488 qemu_put_be32s (f
, &r
->now
[0]);
489 qemu_put_be32s (f
, &r
->now
[1]);
490 qemu_put_be16s (f
, &r
->base
[0]);
491 qemu_put_be16s (f
, &r
->base
[1]);
492 qemu_put_8s (f
, &r
->mode
);
493 qemu_put_8s (f
, &r
->page
);
494 qemu_put_8s (f
, &r
->pageh
);
495 qemu_put_8s (f
, &r
->dack
);
496 qemu_put_8s (f
, &r
->eop
);
500 static int dma_load (QEMUFile
*f
, void *opaque
, int version_id
)
502 struct dma_cont
*d
= opaque
;
508 /* qemu_get_8s (f, &d->status); */
509 qemu_get_8s (f
, &d
->command
);
510 qemu_get_8s (f
, &d
->mask
);
511 qemu_get_8s (f
, &d
->flip_flop
);
512 qemu_get_be32s (f
, &d
->dshift
);
514 for (i
= 0; i
< 4; ++i
) {
515 struct dma_regs
*r
= &d
->regs
[i
];
516 qemu_get_be32s (f
, &r
->now
[0]);
517 qemu_get_be32s (f
, &r
->now
[1]);
518 qemu_get_be16s (f
, &r
->base
[0]);
519 qemu_get_be16s (f
, &r
->base
[1]);
520 qemu_get_8s (f
, &r
->mode
);
521 qemu_get_8s (f
, &r
->page
);
522 qemu_get_8s (f
, &r
->pageh
);
523 qemu_get_8s (f
, &r
->dack
);
524 qemu_get_8s (f
, &r
->eop
);
529 void DMA_init (int high_page_enable
)
531 dma_init2(&dma_controllers
[0], 0x00, 0, 0x80,
532 high_page_enable
? 0x480 : -1);
533 dma_init2(&dma_controllers
[1], 0xc0, 1, 0x88,
534 high_page_enable
? 0x488 : -1);
535 register_savevm ("dma", 0, 1, dma_save
, dma_load
, &dma_controllers
[0]);
536 register_savevm ("dma", 1, 1, dma_save
, dma_load
, &dma_controllers
[1]);