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 linfo(...) fprintf (stderr, "dma: " __VA_ARGS__)
32 #define ldebug(...) fprintf (stderr, "dma: " __VA_ARGS__)
46 DMA_transfer_handler transfer_handler
;
53 static struct dma_cont
{
59 struct dma_regs regs
[4];
63 CMD_MEMORY_TO_MEMORY
= 0x01,
64 CMD_FIXED_ADDRESS
= 0x02,
65 CMD_BLOCK_CONTROLLER
= 0x04,
66 CMD_COMPRESSED_TIME
= 0x08,
67 CMD_CYCLIC_PRIORITY
= 0x10,
68 CMD_EXTENDED_WRITE
= 0x20,
71 CMD_NOT_SUPPORTED
= CMD_MEMORY_TO_MEMORY
| CMD_FIXED_ADDRESS
72 | CMD_COMPRESSED_TIME
| CMD_CYCLIC_PRIORITY
| CMD_EXTENDED_WRITE
73 | CMD_LOW_DREQ
| CMD_LOW_DACK
77 static void DMA_run (void);
79 static int channels
[8] = {-1, 2, 3, 1, -1, -1, -1, 0};
81 static void write_page (void *opaque
, uint32_t nport
, uint32_t data
)
83 struct dma_cont
*d
= opaque
;
86 ichan
= channels
[nport
& 7];
88 dolog ("invalid channel %#x %#x\n", nport
, data
);
91 d
->regs
[ichan
].page
= data
;
94 static void write_pageh (void *opaque
, uint32_t nport
, uint32_t data
)
96 struct dma_cont
*d
= opaque
;
99 ichan
= channels
[nport
& 7];
101 dolog ("invalid channel %#x %#x\n", nport
, data
);
104 d
->regs
[ichan
].pageh
= data
;
107 static uint32_t read_page (void *opaque
, uint32_t nport
)
109 struct dma_cont
*d
= opaque
;
112 ichan
= channels
[nport
& 7];
114 dolog ("invalid channel read %#x\n", nport
);
117 return d
->regs
[ichan
].page
;
120 static uint32_t read_pageh (void *opaque
, uint32_t nport
)
122 struct dma_cont
*d
= opaque
;
125 ichan
= channels
[nport
& 7];
127 dolog ("invalid channel read %#x\n", nport
);
130 return d
->regs
[ichan
].pageh
;
133 static inline void init_chan (struct dma_cont
*d
, int ichan
)
138 r
->now
[ADDR
] = r
->base
[ADDR
] << d
->dshift
;
142 static inline int getff (struct dma_cont
*d
)
151 static uint32_t read_chan (void *opaque
, uint32_t nport
)
153 struct dma_cont
*d
= opaque
;
154 int ichan
, nreg
, iport
, ff
, val
, dir
;
157 iport
= (nport
>> d
->dshift
) & 0x0f;
162 dir
= ((r
->mode
>> 5) & 1) ? -1 : 1;
165 val
= (r
->base
[COUNT
] << d
->dshift
) - r
->now
[COUNT
];
167 val
= r
->now
[ADDR
] + r
->now
[COUNT
] * dir
;
169 ldebug ("read_chan %#x -> %d\n", iport
, val
);
170 return (val
>> (d
->dshift
+ (ff
<< 3))) & 0xff;
173 static void write_chan (void *opaque
, uint32_t nport
, uint32_t data
)
175 struct dma_cont
*d
= opaque
;
176 int iport
, ichan
, nreg
;
179 iport
= (nport
>> d
->dshift
) & 0x0f;
184 r
->base
[nreg
] = (r
->base
[nreg
] & 0xff) | ((data
<< 8) & 0xff00);
185 init_chan (d
, ichan
);
187 r
->base
[nreg
] = (r
->base
[nreg
] & 0xff00) | (data
& 0xff);
191 static void write_cont (void *opaque
, uint32_t nport
, uint32_t data
)
193 struct dma_cont
*d
= opaque
;
194 int iport
, ichan
= 0;
196 iport
= (nport
>> d
->dshift
) & 0x0f;
198 case 0x08: /* command */
199 if ((data
!= 0) && (data
& CMD_NOT_SUPPORTED
)) {
200 dolog ("command %#x not supported\n", data
);
209 d
->status
|= 1 << (ichan
+ 4);
212 d
->status
&= ~(1 << (ichan
+ 4));
214 d
->status
&= ~(1 << ichan
);
218 case 0x0a: /* single mask */
220 d
->mask
|= 1 << (data
& 3);
222 d
->mask
&= ~(1 << (data
& 3));
226 case 0x0b: /* mode */
231 int op
, ai
, dir
, opmode
;
232 op
= (data
>> 2) & 3;
233 ai
= (data
>> 4) & 1;
234 dir
= (data
>> 5) & 1;
235 opmode
= (data
>> 6) & 3;
237 linfo ("ichan %d, op %d, ai %d, dir %d, opmode %d\n",
238 ichan
, op
, ai
, dir
, opmode
);
241 d
->regs
[ichan
].mode
= data
;
245 case 0x0c: /* clear flip flop */
249 case 0x0d: /* reset */
256 case 0x0e: /* clear mask for all channels */
261 case 0x0f: /* write mask for all channels */
267 dolog ("unknown iport %#x\n", iport
);
273 linfo ("write_cont: nport %#06x, ichan % 2d, val %#06x\n",
279 static uint32_t read_cont (void *opaque
, uint32_t nport
)
281 struct dma_cont
*d
= opaque
;
284 iport
= (nport
>> d
->dshift
) & 0x0f;
286 case 0x08: /* status */
290 case 0x0f: /* mask */
298 ldebug ("read_cont: nport %#06x, iport %#04x val %#x\n", nport
, iport
, val
);
302 int DMA_get_channel_mode (int nchan
)
304 return dma_controllers
[nchan
> 3].regs
[nchan
& 3].mode
;
307 void DMA_hold_DREQ (int nchan
)
313 linfo ("held cont=%d chan=%d\n", ncont
, ichan
);
314 dma_controllers
[ncont
].status
|= 1 << (ichan
+ 4);
318 void DMA_release_DREQ (int nchan
)
324 linfo ("released cont=%d chan=%d\n", ncont
, ichan
);
325 dma_controllers
[ncont
].status
&= ~(1 << (ichan
+ 4));
329 static void channel_run (int ncont
, int ichan
)
332 struct dma_regs
*r
= &dma_controllers
[ncont
].regs
[ichan
];
336 dir
= (r
->mode
>> 5) & 1;
337 opmode
= (r
->mode
>> 6) & 3;
340 dolog ("DMA in address decrement mode\n");
343 dolog ("DMA not in single mode select %#x\n", opmode
);
347 r
= dma_controllers
[ncont
].regs
+ ichan
;
348 n
= r
->transfer_handler (r
->opaque
, ichan
+ (ncont
<< 2),
349 r
->now
[COUNT
], (r
->base
[COUNT
] + 1) << ncont
);
351 ldebug ("dma_pos %d size %d\n", n
, (r
->base
[COUNT
] + 1) << ncont
);
354 static QEMUBH
*dma_bh
;
356 static void DMA_run (void)
364 for (icont
= 0; icont
< 2; icont
++, d
++) {
365 for (ichan
= 0; ichan
< 4; ichan
++) {
370 if ((0 == (d
->mask
& mask
)) && (0 != (d
->status
& (mask
<< 4)))) {
371 channel_run (icont
, ichan
);
378 qemu_bh_schedule_idle(dma_bh
);
381 static void DMA_run_bh(void *unused
)
386 void DMA_register_channel (int nchan
,
387 DMA_transfer_handler transfer_handler
,
396 r
= dma_controllers
[ncont
].regs
+ ichan
;
397 r
->transfer_handler
= transfer_handler
;
401 int DMA_read_memory (int nchan
, void *buf
, int pos
, int len
)
403 struct dma_regs
*r
= &dma_controllers
[nchan
> 3].regs
[nchan
& 3];
404 target_phys_addr_t addr
= ((r
->pageh
& 0x7f) << 24) | (r
->page
<< 16) | r
->now
[ADDR
];
406 if (r
->mode
& 0x20) {
410 cpu_physical_memory_read (addr
- pos
- len
, buf
, len
);
411 /* What about 16bit transfers? */
412 for (i
= 0; i
< len
>> 1; i
++) {
413 uint8_t b
= p
[len
- i
- 1];
418 cpu_physical_memory_read (addr
+ pos
, buf
, len
);
423 int DMA_write_memory (int nchan
, void *buf
, int pos
, int len
)
425 struct dma_regs
*r
= &dma_controllers
[nchan
> 3].regs
[nchan
& 3];
426 target_phys_addr_t addr
= ((r
->pageh
& 0x7f) << 24) | (r
->page
<< 16) | r
->now
[ADDR
];
428 if (r
->mode
& 0x20) {
432 cpu_physical_memory_write (addr
- pos
- len
, buf
, len
);
433 /* What about 16bit transfers? */
434 for (i
= 0; i
< len
; i
++) {
435 uint8_t b
= p
[len
- i
- 1];
440 cpu_physical_memory_write (addr
+ pos
, buf
, len
);
445 /* request the emulator to transfer a new DMA memory block ASAP */
446 void DMA_schedule(int nchan
)
448 CPUState
*env
= cpu_single_env
;
453 static void dma_reset(void *opaque
)
455 struct dma_cont
*d
= opaque
;
456 write_cont (d
, (0x0d << d
->dshift
), 0);
459 static int dma_phony_handler (void *opaque
, int nchan
, int dma_pos
, int dma_len
)
461 dolog ("unregistered DMA channel used nchan=%d dma_pos=%d dma_len=%d\n",
462 nchan
, dma_pos
, dma_len
);
466 /* dshift = 0: 8 bit DMA, 1 = 16 bit DMA */
467 static void dma_init2(struct dma_cont
*d
, int base
, int dshift
,
468 int page_base
, int pageh_base
)
470 static const int page_port_list
[] = { 0x1, 0x2, 0x3, 0x7 };
474 for (i
= 0; i
< 8; i
++) {
475 register_ioport_write (base
+ (i
<< dshift
), 1, 1, write_chan
, d
);
476 register_ioport_read (base
+ (i
<< dshift
), 1, 1, read_chan
, d
);
478 for (i
= 0; i
< ARRAY_SIZE (page_port_list
); i
++) {
479 register_ioport_write (page_base
+ page_port_list
[i
], 1, 1,
481 register_ioport_read (page_base
+ page_port_list
[i
], 1, 1,
483 if (pageh_base
>= 0) {
484 register_ioport_write (pageh_base
+ page_port_list
[i
], 1, 1,
486 register_ioport_read (pageh_base
+ page_port_list
[i
], 1, 1,
490 for (i
= 0; i
< 8; i
++) {
491 register_ioport_write (base
+ ((i
+ 8) << dshift
), 1, 1,
493 register_ioport_read (base
+ ((i
+ 8) << dshift
), 1, 1,
496 qemu_register_reset(dma_reset
, 0, d
);
498 for (i
= 0; i
< ARRAY_SIZE (d
->regs
); ++i
) {
499 d
->regs
[i
].transfer_handler
= dma_phony_handler
;
503 static void dma_save (QEMUFile
*f
, void *opaque
)
505 struct dma_cont
*d
= opaque
;
508 /* qemu_put_8s (f, &d->status); */
509 qemu_put_8s (f
, &d
->command
);
510 qemu_put_8s (f
, &d
->mask
);
511 qemu_put_8s (f
, &d
->flip_flop
);
512 qemu_put_be32 (f
, d
->dshift
);
514 for (i
= 0; i
< 4; ++i
) {
515 struct dma_regs
*r
= &d
->regs
[i
];
516 qemu_put_be32 (f
, r
->now
[0]);
517 qemu_put_be32 (f
, r
->now
[1]);
518 qemu_put_be16s (f
, &r
->base
[0]);
519 qemu_put_be16s (f
, &r
->base
[1]);
520 qemu_put_8s (f
, &r
->mode
);
521 qemu_put_8s (f
, &r
->page
);
522 qemu_put_8s (f
, &r
->pageh
);
523 qemu_put_8s (f
, &r
->dack
);
524 qemu_put_8s (f
, &r
->eop
);
528 static int dma_load (QEMUFile
*f
, void *opaque
, int version_id
)
530 struct dma_cont
*d
= opaque
;
536 /* qemu_get_8s (f, &d->status); */
537 qemu_get_8s (f
, &d
->command
);
538 qemu_get_8s (f
, &d
->mask
);
539 qemu_get_8s (f
, &d
->flip_flop
);
540 d
->dshift
=qemu_get_be32 (f
);
542 for (i
= 0; i
< 4; ++i
) {
543 struct dma_regs
*r
= &d
->regs
[i
];
544 r
->now
[0]=qemu_get_be32 (f
);
545 r
->now
[1]=qemu_get_be32 (f
);
546 qemu_get_be16s (f
, &r
->base
[0]);
547 qemu_get_be16s (f
, &r
->base
[1]);
548 qemu_get_8s (f
, &r
->mode
);
549 qemu_get_8s (f
, &r
->page
);
550 qemu_get_8s (f
, &r
->pageh
);
551 qemu_get_8s (f
, &r
->dack
);
552 qemu_get_8s (f
, &r
->eop
);
560 void DMA_init (int high_page_enable
)
562 dma_init2(&dma_controllers
[0], 0x00, 0, 0x80,
563 high_page_enable
? 0x480 : -1);
564 dma_init2(&dma_controllers
[1], 0xc0, 1, 0x88,
565 high_page_enable
? 0x488 : -1);
566 register_savevm ("dma", 0, 1, dma_save
, dma_load
, &dma_controllers
[0]);
567 register_savevm ("dma", 1, 1, dma_save
, dma_load
, &dma_controllers
[1]);
569 dma_bh
= qemu_bh_new(DMA_run_bh
, NULL
);