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
27 /* #define DEBUG_DMA */
29 #define dolog(...) fprintf (stderr, "dma: " __VA_ARGS__)
31 #define lwarn(...) fprintf (stderr, "dma: " __VA_ARGS__)
32 #define linfo(...) fprintf (stderr, "dma: " __VA_ARGS__)
33 #define ldebug(...) fprintf (stderr, "dma: " __VA_ARGS__)
48 DMA_transfer_handler transfer_handler
;
55 static struct dma_cont
{
61 struct dma_regs regs
[4];
65 CMD_MEMORY_TO_MEMORY
= 0x01,
66 CMD_FIXED_ADDRESS
= 0x02,
67 CMD_BLOCK_CONTROLLER
= 0x04,
68 CMD_COMPRESSED_TIME
= 0x08,
69 CMD_CYCLIC_PRIORITY
= 0x10,
70 CMD_EXTENDED_WRITE
= 0x20,
73 CMD_NOT_SUPPORTED
= CMD_MEMORY_TO_MEMORY
| CMD_FIXED_ADDRESS
74 | CMD_COMPRESSED_TIME
| CMD_CYCLIC_PRIORITY
| CMD_EXTENDED_WRITE
75 | CMD_LOW_DREQ
| CMD_LOW_DACK
79 static void DMA_run (void);
81 static int channels
[8] = {-1, 2, 3, 1, -1, -1, -1, 0};
83 static void write_page (void *opaque
, uint32_t nport
, uint32_t data
)
85 struct dma_cont
*d
= opaque
;
88 ichan
= channels
[nport
& 7];
90 dolog ("invalid channel %#x %#x\n", nport
, data
);
93 d
->regs
[ichan
].page
= data
;
96 static void write_pageh (void *opaque
, uint32_t nport
, uint32_t data
)
98 struct dma_cont
*d
= opaque
;
101 ichan
= channels
[nport
& 7];
103 dolog ("invalid channel %#x %#x\n", nport
, data
);
106 d
->regs
[ichan
].pageh
= data
;
109 static uint32_t read_page (void *opaque
, uint32_t nport
)
111 struct dma_cont
*d
= opaque
;
114 ichan
= channels
[nport
& 7];
116 dolog ("invalid channel read %#x\n", nport
);
119 return d
->regs
[ichan
].page
;
122 static uint32_t read_pageh (void *opaque
, uint32_t nport
)
124 struct dma_cont
*d
= opaque
;
127 ichan
= channels
[nport
& 7];
129 dolog ("invalid channel read %#x\n", nport
);
132 return d
->regs
[ichan
].pageh
;
135 static inline void init_chan (struct dma_cont
*d
, int ichan
)
140 r
->now
[ADDR
] = r
->base
[ADDR
] << d
->dshift
;
144 static inline int getff (struct dma_cont
*d
)
153 static uint32_t read_chan (void *opaque
, uint32_t nport
)
155 struct dma_cont
*d
= opaque
;
156 int ichan
, nreg
, iport
, ff
, val
, dir
;
159 iport
= (nport
>> d
->dshift
) & 0x0f;
164 dir
= ((r
->mode
>> 5) & 1) ? -1 : 1;
167 val
= (r
->base
[COUNT
] << d
->dshift
) - r
->now
[COUNT
];
169 val
= r
->now
[ADDR
] + r
->now
[COUNT
] * dir
;
171 ldebug ("read_chan %#x -> %d\n", iport
, val
);
172 return (val
>> (d
->dshift
+ (ff
<< 3))) & 0xff;
175 static void write_chan (void *opaque
, uint32_t nport
, uint32_t data
)
177 struct dma_cont
*d
= opaque
;
178 int iport
, ichan
, nreg
;
181 iport
= (nport
>> d
->dshift
) & 0x0f;
186 r
->base
[nreg
] = (r
->base
[nreg
] & 0xff) | ((data
<< 8) & 0xff00);
187 init_chan (d
, ichan
);
189 r
->base
[nreg
] = (r
->base
[nreg
] & 0xff00) | (data
& 0xff);
193 static void write_cont (void *opaque
, uint32_t nport
, uint32_t data
)
195 struct dma_cont
*d
= opaque
;
196 int iport
, ichan
= 0;
198 iport
= (nport
>> d
->dshift
) & 0x0f;
200 case 0x08: /* command */
201 if ((data
!= 0) && (data
& CMD_NOT_SUPPORTED
)) {
202 dolog ("command %#x not supported\n", data
);
211 d
->status
|= 1 << (ichan
+ 4);
214 d
->status
&= ~(1 << (ichan
+ 4));
216 d
->status
&= ~(1 << ichan
);
220 case 0x0a: /* single mask */
222 d
->mask
|= 1 << (data
& 3);
224 d
->mask
&= ~(1 << (data
& 3));
228 case 0x0b: /* mode */
233 int op
, ai
, dir
, opmode
;
234 op
= (data
>> 2) & 3;
235 ai
= (data
>> 4) & 1;
236 dir
= (data
>> 5) & 1;
237 opmode
= (data
>> 6) & 3;
239 linfo ("ichan %d, op %d, ai %d, dir %d, opmode %d\n",
240 ichan
, op
, ai
, dir
, opmode
);
243 d
->regs
[ichan
].mode
= data
;
247 case 0x0c: /* clear flip flop */
251 case 0x0d: /* reset */
258 case 0x0e: /* clear mask for all channels */
263 case 0x0f: /* write mask for all channels */
269 dolog ("unknown iport %#x\n", iport
);
275 linfo ("write_cont: nport %#06x, ichan % 2d, val %#06x\n",
281 static uint32_t read_cont (void *opaque
, uint32_t nport
)
283 struct dma_cont
*d
= opaque
;
286 iport
= (nport
>> d
->dshift
) & 0x0f;
288 case 0x08: /* status */
292 case 0x0f: /* mask */
300 ldebug ("read_cont: nport %#06x, iport %#04x val %#x\n", nport
, iport
, val
);
304 int DMA_get_channel_mode (int nchan
)
306 return dma_controllers
[nchan
> 3].regs
[nchan
& 3].mode
;
309 void DMA_hold_DREQ (int nchan
)
315 linfo ("held cont=%d chan=%d\n", ncont
, ichan
);
316 dma_controllers
[ncont
].status
|= 1 << (ichan
+ 4);
320 void DMA_release_DREQ (int nchan
)
326 linfo ("released cont=%d chan=%d\n", ncont
, ichan
);
327 dma_controllers
[ncont
].status
&= ~(1 << (ichan
+ 4));
331 static void channel_run (int ncont
, int ichan
)
334 struct dma_regs
*r
= &dma_controllers
[ncont
].regs
[ichan
];
338 dir
= (r
->mode
>> 5) & 1;
339 opmode
= (r
->mode
>> 6) & 3;
342 dolog ("DMA in address decrement mode\n");
345 dolog ("DMA not in single mode select %#x\n", opmode
);
349 r
= dma_controllers
[ncont
].regs
+ ichan
;
350 n
= r
->transfer_handler (r
->opaque
, ichan
+ (ncont
<< 2),
351 r
->now
[COUNT
], (r
->base
[COUNT
] + 1) << ncont
);
353 ldebug ("dma_pos %d size %d\n", n
, (r
->base
[COUNT
] + 1) << ncont
);
356 static QEMUBH
*dma_bh
;
358 static void DMA_run (void)
366 for (icont
= 0; icont
< 2; icont
++, d
++) {
367 for (ichan
= 0; ichan
< 4; ichan
++) {
372 if ((0 == (d
->mask
& mask
)) && (0 != (d
->status
& (mask
<< 4)))) {
373 channel_run (icont
, ichan
);
380 qemu_bh_schedule_idle(dma_bh
);
383 static void DMA_run_bh(void *unused
)
388 void DMA_register_channel (int nchan
,
389 DMA_transfer_handler transfer_handler
,
398 r
= dma_controllers
[ncont
].regs
+ ichan
;
399 r
->transfer_handler
= transfer_handler
;
403 int DMA_read_memory (int nchan
, void *buf
, int pos
, int len
)
405 struct dma_regs
*r
= &dma_controllers
[nchan
> 3].regs
[nchan
& 3];
406 target_phys_addr_t addr
= ((r
->pageh
& 0x7f) << 24) | (r
->page
<< 16) | r
->now
[ADDR
];
408 if (r
->mode
& 0x20) {
412 cpu_physical_memory_read (addr
- pos
- len
, buf
, len
);
413 /* What about 16bit transfers? */
414 for (i
= 0; i
< len
>> 1; i
++) {
415 uint8_t b
= p
[len
- i
- 1];
420 cpu_physical_memory_read (addr
+ pos
, buf
, len
);
425 int DMA_write_memory (int nchan
, void *buf
, int pos
, int len
)
427 struct dma_regs
*r
= &dma_controllers
[nchan
> 3].regs
[nchan
& 3];
428 target_phys_addr_t addr
= ((r
->pageh
& 0x7f) << 24) | (r
->page
<< 16) | r
->now
[ADDR
];
430 if (r
->mode
& 0x20) {
434 cpu_physical_memory_write (addr
- pos
- len
, buf
, len
);
435 /* What about 16bit transfers? */
436 for (i
= 0; i
< len
; i
++) {
437 uint8_t b
= p
[len
- i
- 1];
442 cpu_physical_memory_write (addr
+ pos
, buf
, len
);
447 /* request the emulator to transfer a new DMA memory block ASAP */
448 void DMA_schedule(int nchan
)
450 CPUState
*env
= cpu_single_env
;
452 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
455 static void dma_reset(void *opaque
)
457 struct dma_cont
*d
= opaque
;
458 write_cont (d
, (0x0d << d
->dshift
), 0);
461 static int dma_phony_handler (void *opaque
, int nchan
, int dma_pos
, int dma_len
)
463 dolog ("unregistered DMA channel used nchan=%d dma_pos=%d dma_len=%d\n",
464 nchan
, dma_pos
, dma_len
);
468 /* dshift = 0: 8 bit DMA, 1 = 16 bit DMA */
469 static void dma_init2(struct dma_cont
*d
, int base
, int dshift
,
470 int page_base
, int pageh_base
)
472 static const int page_port_list
[] = { 0x1, 0x2, 0x3, 0x7 };
476 for (i
= 0; i
< 8; i
++) {
477 register_ioport_write (base
+ (i
<< dshift
), 1, 1, write_chan
, d
);
478 register_ioport_read (base
+ (i
<< dshift
), 1, 1, read_chan
, d
);
480 for (i
= 0; i
< ARRAY_SIZE (page_port_list
); i
++) {
481 register_ioport_write (page_base
+ page_port_list
[i
], 1, 1,
483 register_ioport_read (page_base
+ page_port_list
[i
], 1, 1,
485 if (pageh_base
>= 0) {
486 register_ioport_write (pageh_base
+ page_port_list
[i
], 1, 1,
488 register_ioport_read (pageh_base
+ page_port_list
[i
], 1, 1,
492 for (i
= 0; i
< 8; i
++) {
493 register_ioport_write (base
+ ((i
+ 8) << dshift
), 1, 1,
495 register_ioport_read (base
+ ((i
+ 8) << dshift
), 1, 1,
498 qemu_register_reset(dma_reset
, d
);
500 for (i
= 0; i
< ARRAY_SIZE (d
->regs
); ++i
) {
501 d
->regs
[i
].transfer_handler
= dma_phony_handler
;
505 static void dma_save (QEMUFile
*f
, void *opaque
)
507 struct dma_cont
*d
= opaque
;
510 /* qemu_put_8s (f, &d->status); */
511 qemu_put_8s (f
, &d
->command
);
512 qemu_put_8s (f
, &d
->mask
);
513 qemu_put_8s (f
, &d
->flip_flop
);
514 qemu_put_be32 (f
, d
->dshift
);
516 for (i
= 0; i
< 4; ++i
) {
517 struct dma_regs
*r
= &d
->regs
[i
];
518 qemu_put_be32 (f
, r
->now
[0]);
519 qemu_put_be32 (f
, r
->now
[1]);
520 qemu_put_be16s (f
, &r
->base
[0]);
521 qemu_put_be16s (f
, &r
->base
[1]);
522 qemu_put_8s (f
, &r
->mode
);
523 qemu_put_8s (f
, &r
->page
);
524 qemu_put_8s (f
, &r
->pageh
);
525 qemu_put_8s (f
, &r
->dack
);
526 qemu_put_8s (f
, &r
->eop
);
530 static int dma_load (QEMUFile
*f
, void *opaque
, int version_id
)
532 struct dma_cont
*d
= opaque
;
538 /* qemu_get_8s (f, &d->status); */
539 qemu_get_8s (f
, &d
->command
);
540 qemu_get_8s (f
, &d
->mask
);
541 qemu_get_8s (f
, &d
->flip_flop
);
542 d
->dshift
=qemu_get_be32 (f
);
544 for (i
= 0; i
< 4; ++i
) {
545 struct dma_regs
*r
= &d
->regs
[i
];
546 r
->now
[0]=qemu_get_be32 (f
);
547 r
->now
[1]=qemu_get_be32 (f
);
548 qemu_get_be16s (f
, &r
->base
[0]);
549 qemu_get_be16s (f
, &r
->base
[1]);
550 qemu_get_8s (f
, &r
->mode
);
551 qemu_get_8s (f
, &r
->page
);
552 qemu_get_8s (f
, &r
->pageh
);
553 qemu_get_8s (f
, &r
->dack
);
554 qemu_get_8s (f
, &r
->eop
);
562 void DMA_init (int high_page_enable
)
564 dma_init2(&dma_controllers
[0], 0x00, 0, 0x80,
565 high_page_enable
? 0x480 : -1);
566 dma_init2(&dma_controllers
[1], 0xc0, 1, 0x88,
567 high_page_enable
? 0x488 : -1);
568 register_savevm ("dma", 0, 1, dma_save
, dma_load
, &dma_controllers
[0]);
569 register_savevm ("dma", 1, 1, dma_save
, dma_load
, &dma_controllers
[1]);
571 dma_bh
= qemu_bh_new(DMA_run_bh
, NULL
);