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__)
40 #define LENOFA(a) ((int) (sizeof(a)/sizeof(a[0])))
50 DMA_transfer_handler transfer_handler
;
57 static struct dma_cont
{
63 struct dma_regs regs
[4];
67 CMD_MEMORY_TO_MEMORY
= 0x01,
68 CMD_FIXED_ADDRESS
= 0x02,
69 CMD_BLOCK_CONTROLLER
= 0x04,
70 CMD_COMPRESSED_TIME
= 0x08,
71 CMD_CYCLIC_PRIORITY
= 0x10,
72 CMD_EXTENDED_WRITE
= 0x20,
75 CMD_NOT_SUPPORTED
= CMD_MEMORY_TO_MEMORY
| CMD_FIXED_ADDRESS
76 | CMD_COMPRESSED_TIME
| CMD_CYCLIC_PRIORITY
| CMD_EXTENDED_WRITE
77 | CMD_LOW_DREQ
| CMD_LOW_DACK
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
);
219 case 0x0a: /* single mask */
221 d
->mask
|= 1 << (data
& 3);
223 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 */
260 case 0x0f: /* write mask for all channels */
265 dolog ("unknown iport %#x\n", iport
);
271 linfo ("write_cont: nport %#06x, ichan % 2d, val %#06x\n",
277 static uint32_t read_cont (void *opaque
, uint32_t nport
)
279 struct dma_cont
*d
= opaque
;
282 iport
= (nport
>> d
->dshift
) & 0x0f;
284 case 0x08: /* status */
288 case 0x0f: /* mask */
296 ldebug ("read_cont: nport %#06x, iport %#04x val %#x\n", nport
, iport
, val
);
300 int DMA_get_channel_mode (int nchan
)
302 return dma_controllers
[nchan
> 3].regs
[nchan
& 3].mode
;
305 void DMA_hold_DREQ (int nchan
)
311 linfo ("held cont=%d chan=%d\n", ncont
, ichan
);
312 dma_controllers
[ncont
].status
|= 1 << (ichan
+ 4);
315 void DMA_release_DREQ (int nchan
)
321 linfo ("released cont=%d chan=%d\n", ncont
, ichan
);
322 dma_controllers
[ncont
].status
&= ~(1 << (ichan
+ 4));
325 static void channel_run (int ncont
, int ichan
)
328 struct dma_regs
*r
= &dma_controllers
[ncont
].regs
[ichan
];
332 dir
= (r
->mode
>> 5) & 1;
333 opmode
= (r
->mode
>> 6) & 3;
336 dolog ("DMA in address decrement mode\n");
339 dolog ("DMA not in single mode select %#x\n", opmode
);
343 r
= dma_controllers
[ncont
].regs
+ ichan
;
344 n
= r
->transfer_handler (r
->opaque
, ichan
+ (ncont
<< 2),
345 r
->now
[COUNT
], (r
->base
[COUNT
] + 1) << ncont
);
347 ldebug ("dma_pos %d size %d\n", n
, (r
->base
[COUNT
] + 1) << ncont
);
357 for (icont
= 0; icont
< 2; icont
++, d
++) {
358 for (ichan
= 0; ichan
< 4; ichan
++) {
363 if ((0 == (d
->mask
& mask
)) && (0 != (d
->status
& (mask
<< 4))))
364 channel_run (icont
, ichan
);
369 void DMA_register_channel (int nchan
,
370 DMA_transfer_handler transfer_handler
,
379 r
= dma_controllers
[ncont
].regs
+ ichan
;
380 r
->transfer_handler
= transfer_handler
;
384 int DMA_read_memory (int nchan
, void *buf
, int pos
, int len
)
386 struct dma_regs
*r
= &dma_controllers
[nchan
> 3].regs
[nchan
& 3];
387 target_phys_addr_t addr
= ((r
->pageh
& 0x7f) << 24) | (r
->page
<< 16) | r
->now
[ADDR
];
389 if (r
->mode
& 0x20) {
393 cpu_physical_memory_read (addr
- pos
- len
, buf
, len
);
394 /* What about 16bit transfers? */
395 for (i
= 0; i
< len
>> 1; i
++) {
396 uint8_t b
= p
[len
- i
- 1];
401 cpu_physical_memory_read (addr
+ pos
, buf
, len
);
406 int DMA_write_memory (int nchan
, void *buf
, int pos
, int len
)
408 struct dma_regs
*r
= &dma_controllers
[nchan
> 3].regs
[nchan
& 3];
409 target_phys_addr_t addr
= ((r
->pageh
& 0x7f) << 24) | (r
->page
<< 16) | r
->now
[ADDR
];
411 if (r
->mode
& 0x20) {
415 cpu_physical_memory_write (addr
- pos
- len
, buf
, len
);
416 /* What about 16bit transfers? */
417 for (i
= 0; i
< len
; i
++) {
418 uint8_t b
= p
[len
- i
- 1];
423 cpu_physical_memory_write (addr
+ pos
, buf
, len
);
428 /* request the emulator to transfer a new DMA memory block ASAP */
429 void DMA_schedule(int nchan
)
431 CPUState
*env
= cpu_single_env
;
433 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
436 static void dma_reset(void *opaque
)
438 struct dma_cont
*d
= opaque
;
439 write_cont (d
, (0x0d << d
->dshift
), 0);
442 static int dma_phony_handler (void *opaque
, int nchan
, int dma_pos
, int dma_len
)
444 dolog ("unregistered DMA channel used nchan=%d dma_pos=%d dma_len=%d\n",
445 nchan
, dma_pos
, dma_len
);
449 /* dshift = 0: 8 bit DMA, 1 = 16 bit DMA */
450 static void dma_init2(struct dma_cont
*d
, int base
, int dshift
,
451 int page_base
, int pageh_base
)
453 const static int page_port_list
[] = { 0x1, 0x2, 0x3, 0x7 };
457 for (i
= 0; i
< 8; i
++) {
458 register_ioport_write (base
+ (i
<< dshift
), 1, 1, write_chan
, d
);
459 register_ioport_read (base
+ (i
<< dshift
), 1, 1, read_chan
, d
);
461 for (i
= 0; i
< LENOFA (page_port_list
); i
++) {
462 register_ioport_write (page_base
+ page_port_list
[i
], 1, 1,
464 register_ioport_read (page_base
+ page_port_list
[i
], 1, 1,
466 if (pageh_base
>= 0) {
467 register_ioport_write (pageh_base
+ page_port_list
[i
], 1, 1,
469 register_ioport_read (pageh_base
+ page_port_list
[i
], 1, 1,
473 for (i
= 0; i
< 8; i
++) {
474 register_ioport_write (base
+ ((i
+ 8) << dshift
), 1, 1,
476 register_ioport_read (base
+ ((i
+ 8) << dshift
), 1, 1,
479 qemu_register_reset(dma_reset
, d
);
481 for (i
= 0; i
< LENOFA (d
->regs
); ++i
) {
482 d
->regs
[i
].transfer_handler
= dma_phony_handler
;
486 static void dma_save (QEMUFile
*f
, void *opaque
)
488 struct dma_cont
*d
= opaque
;
491 /* qemu_put_8s (f, &d->status); */
492 qemu_put_8s (f
, &d
->command
);
493 qemu_put_8s (f
, &d
->mask
);
494 qemu_put_8s (f
, &d
->flip_flop
);
495 qemu_put_be32 (f
, d
->dshift
);
497 for (i
= 0; i
< 4; ++i
) {
498 struct dma_regs
*r
= &d
->regs
[i
];
499 qemu_put_be32 (f
, r
->now
[0]);
500 qemu_put_be32 (f
, r
->now
[1]);
501 qemu_put_be16s (f
, &r
->base
[0]);
502 qemu_put_be16s (f
, &r
->base
[1]);
503 qemu_put_8s (f
, &r
->mode
);
504 qemu_put_8s (f
, &r
->page
);
505 qemu_put_8s (f
, &r
->pageh
);
506 qemu_put_8s (f
, &r
->dack
);
507 qemu_put_8s (f
, &r
->eop
);
511 static int dma_load (QEMUFile
*f
, void *opaque
, int version_id
)
513 struct dma_cont
*d
= opaque
;
519 /* qemu_get_8s (f, &d->status); */
520 qemu_get_8s (f
, &d
->command
);
521 qemu_get_8s (f
, &d
->mask
);
522 qemu_get_8s (f
, &d
->flip_flop
);
523 d
->dshift
=qemu_get_be32 (f
);
525 for (i
= 0; i
< 4; ++i
) {
526 struct dma_regs
*r
= &d
->regs
[i
];
527 r
->now
[0]=qemu_get_be32 (f
);
528 r
->now
[1]=qemu_get_be32 (f
);
529 qemu_get_be16s (f
, &r
->base
[0]);
530 qemu_get_be16s (f
, &r
->base
[1]);
531 qemu_get_8s (f
, &r
->mode
);
532 qemu_get_8s (f
, &r
->page
);
533 qemu_get_8s (f
, &r
->pageh
);
534 qemu_get_8s (f
, &r
->dack
);
535 qemu_get_8s (f
, &r
->eop
);
540 void DMA_init (int high_page_enable
)
542 dma_init2(&dma_controllers
[0], 0x00, 0, 0x80,
543 high_page_enable
? 0x480 : -1);
544 dma_init2(&dma_controllers
[1], 0xc0, 1, 0x88,
545 high_page_enable
? 0x488 : -1);
546 register_savevm ("dma", 0, 1, dma_save
, dma_load
, &dma_controllers
[0]);
547 register_savevm ("dma", 1, 1, dma_save
, dma_load
, &dma_controllers
[1]);