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];
60 qemu_irq
*cpu_request_exit
;
61 MemoryRegion channel_io
;
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 void DMA_run (void);
82 static int channels
[8] = {-1, 2, 3, 1, -1, -1, -1, 0};
84 static void write_page (void *opaque
, uint32_t nport
, uint32_t data
)
86 struct dma_cont
*d
= opaque
;
89 ichan
= channels
[nport
& 7];
91 dolog ("invalid channel %#x %#x\n", nport
, data
);
94 d
->regs
[ichan
].page
= data
;
97 static void write_pageh (void *opaque
, uint32_t nport
, uint32_t data
)
99 struct dma_cont
*d
= opaque
;
102 ichan
= channels
[nport
& 7];
104 dolog ("invalid channel %#x %#x\n", nport
, data
);
107 d
->regs
[ichan
].pageh
= data
;
110 static uint32_t read_page (void *opaque
, uint32_t nport
)
112 struct dma_cont
*d
= opaque
;
115 ichan
= channels
[nport
& 7];
117 dolog ("invalid channel read %#x\n", nport
);
120 return d
->regs
[ichan
].page
;
123 static uint32_t read_pageh (void *opaque
, uint32_t nport
)
125 struct dma_cont
*d
= opaque
;
128 ichan
= channels
[nport
& 7];
130 dolog ("invalid channel read %#x\n", nport
);
133 return d
->regs
[ichan
].pageh
;
136 static inline void init_chan (struct dma_cont
*d
, int ichan
)
141 r
->now
[ADDR
] = r
->base
[ADDR
] << d
->dshift
;
145 static inline int getff (struct dma_cont
*d
)
154 static uint64_t read_chan(void *opaque
, hwaddr nport
, unsigned size
)
156 struct dma_cont
*d
= opaque
;
157 int ichan
, nreg
, iport
, ff
, val
, dir
;
160 iport
= (nport
>> d
->dshift
) & 0x0f;
165 dir
= ((r
->mode
>> 5) & 1) ? -1 : 1;
168 val
= (r
->base
[COUNT
] << d
->dshift
) - r
->now
[COUNT
];
170 val
= r
->now
[ADDR
] + r
->now
[COUNT
] * dir
;
172 ldebug ("read_chan %#x -> %d\n", iport
, val
);
173 return (val
>> (d
->dshift
+ (ff
<< 3))) & 0xff;
176 static void write_chan(void *opaque
, hwaddr nport
, uint64_t data
,
179 struct dma_cont
*d
= opaque
;
180 int iport
, ichan
, nreg
;
183 iport
= (nport
>> d
->dshift
) & 0x0f;
188 r
->base
[nreg
] = (r
->base
[nreg
] & 0xff) | ((data
<< 8) & 0xff00);
189 init_chan (d
, ichan
);
191 r
->base
[nreg
] = (r
->base
[nreg
] & 0xff00) | (data
& 0xff);
195 static void write_cont(void *opaque
, hwaddr nport
, uint64_t data
,
198 struct dma_cont
*d
= opaque
;
199 int iport
, ichan
= 0;
201 iport
= (nport
>> d
->dshift
) & 0x0f;
203 case 0x01: /* command */
204 if ((data
!= 0) && (data
& CMD_NOT_SUPPORTED
)) {
205 dolog("command %"PRIx64
" not supported\n", data
);
214 d
->status
|= 1 << (ichan
+ 4);
217 d
->status
&= ~(1 << (ichan
+ 4));
219 d
->status
&= ~(1 << ichan
);
223 case 0x03: /* single mask */
225 d
->mask
|= 1 << (data
& 3);
227 d
->mask
&= ~(1 << (data
& 3));
231 case 0x04: /* mode */
236 int op
, ai
, dir
, opmode
;
237 op
= (data
>> 2) & 3;
238 ai
= (data
>> 4) & 1;
239 dir
= (data
>> 5) & 1;
240 opmode
= (data
>> 6) & 3;
242 linfo ("ichan %d, op %d, ai %d, dir %d, opmode %d\n",
243 ichan
, op
, ai
, dir
, opmode
);
246 d
->regs
[ichan
].mode
= data
;
250 case 0x05: /* clear flip flop */
254 case 0x06: /* reset */
261 case 0x07: /* clear mask for all channels */
266 case 0x08: /* write mask for all channels */
272 dolog ("unknown iport %#x\n", iport
);
278 linfo ("write_cont: nport %#06x, ichan % 2d, val %#06x\n",
284 static uint64_t read_cont(void *opaque
, hwaddr nport
, unsigned size
)
286 struct dma_cont
*d
= opaque
;
289 iport
= (nport
>> d
->dshift
) & 0x0f;
291 case 0x08: /* status */
295 case 0x0f: /* mask */
303 ldebug ("read_cont: nport %#06x, iport %#04x val %#x\n", nport
, iport
, val
);
307 int DMA_get_channel_mode (int nchan
)
309 return dma_controllers
[nchan
> 3].regs
[nchan
& 3].mode
;
312 void DMA_hold_DREQ (int nchan
)
318 linfo ("held cont=%d chan=%d\n", ncont
, ichan
);
319 dma_controllers
[ncont
].status
|= 1 << (ichan
+ 4);
323 void DMA_release_DREQ (int nchan
)
329 linfo ("released cont=%d chan=%d\n", ncont
, ichan
);
330 dma_controllers
[ncont
].status
&= ~(1 << (ichan
+ 4));
334 static void channel_run (int ncont
, int ichan
)
337 struct dma_regs
*r
= &dma_controllers
[ncont
].regs
[ichan
];
341 dir
= (r
->mode
>> 5) & 1;
342 opmode
= (r
->mode
>> 6) & 3;
345 dolog ("DMA in address decrement mode\n");
348 dolog ("DMA not in single mode select %#x\n", opmode
);
352 n
= r
->transfer_handler (r
->opaque
, ichan
+ (ncont
<< 2),
353 r
->now
[COUNT
], (r
->base
[COUNT
] + 1) << ncont
);
355 ldebug ("dma_pos %d size %d\n", n
, (r
->base
[COUNT
] + 1) << ncont
);
358 static QEMUBH
*dma_bh
;
360 static void DMA_run (void)
365 static int running
= 0;
376 for (icont
= 0; icont
< 2; icont
++, d
++) {
377 for (ichan
= 0; ichan
< 4; ichan
++) {
382 if ((0 == (d
->mask
& mask
)) && (0 != (d
->status
& (mask
<< 4)))) {
383 channel_run (icont
, ichan
);
392 qemu_bh_schedule_idle(dma_bh
);
395 static void DMA_run_bh(void *unused
)
400 void DMA_register_channel (int nchan
,
401 DMA_transfer_handler transfer_handler
,
410 r
= dma_controllers
[ncont
].regs
+ ichan
;
411 r
->transfer_handler
= transfer_handler
;
415 int DMA_read_memory (int nchan
, void *buf
, int pos
, int len
)
417 struct dma_regs
*r
= &dma_controllers
[nchan
> 3].regs
[nchan
& 3];
418 hwaddr addr
= ((r
->pageh
& 0x7f) << 24) | (r
->page
<< 16) | r
->now
[ADDR
];
420 if (r
->mode
& 0x20) {
424 cpu_physical_memory_read (addr
- pos
- len
, buf
, len
);
425 /* What about 16bit transfers? */
426 for (i
= 0; i
< len
>> 1; i
++) {
427 uint8_t b
= p
[len
- i
- 1];
432 cpu_physical_memory_read (addr
+ pos
, buf
, len
);
437 int DMA_write_memory (int nchan
, void *buf
, int pos
, int len
)
439 struct dma_regs
*r
= &dma_controllers
[nchan
> 3].regs
[nchan
& 3];
440 hwaddr addr
= ((r
->pageh
& 0x7f) << 24) | (r
->page
<< 16) | r
->now
[ADDR
];
442 if (r
->mode
& 0x20) {
446 cpu_physical_memory_write (addr
- pos
- len
, buf
, len
);
447 /* What about 16bit transfers? */
448 for (i
= 0; i
< len
; i
++) {
449 uint8_t b
= p
[len
- i
- 1];
454 cpu_physical_memory_write (addr
+ pos
, buf
, len
);
459 /* request the emulator to transfer a new DMA memory block ASAP */
460 void DMA_schedule(int nchan
)
462 struct dma_cont
*d
= &dma_controllers
[nchan
> 3];
464 qemu_irq_pulse(*d
->cpu_request_exit
);
467 static void dma_reset(void *opaque
)
469 struct dma_cont
*d
= opaque
;
470 write_cont(d
, (0x06 << d
->dshift
), 0, 1);
473 static int dma_phony_handler (void *opaque
, int nchan
, int dma_pos
, int dma_len
)
475 dolog ("unregistered DMA channel used nchan=%d dma_pos=%d dma_len=%d\n",
476 nchan
, dma_pos
, dma_len
);
481 static const MemoryRegionOps channel_io_ops
= {
484 .endianness
= DEVICE_NATIVE_ENDIAN
,
486 .min_access_size
= 1,
487 .max_access_size
= 1,
491 /* IOport from page_base */
492 static const MemoryRegionPortio page_portio_list
[] = {
493 { 0x01, 3, 1, .write
= write_page
, .read
= read_page
, },
494 { 0x07, 1, 1, .write
= write_page
, .read
= read_page
, },
495 PORTIO_END_OF_LIST(),
498 /* IOport from pageh_base */
499 static const MemoryRegionPortio pageh_portio_list
[] = {
500 { 0x01, 3, 1, .write
= write_pageh
, .read
= read_pageh
, },
501 { 0x07, 3, 1, .write
= write_pageh
, .read
= read_pageh
, },
502 PORTIO_END_OF_LIST(),
505 static const MemoryRegionOps cont_io_ops
= {
508 .endianness
= DEVICE_NATIVE_ENDIAN
,
510 .min_access_size
= 1,
511 .max_access_size
= 1,
515 /* dshift = 0: 8 bit DMA, 1 = 16 bit DMA */
516 static void dma_init2(struct dma_cont
*d
, int base
, int dshift
,
517 int page_base
, int pageh_base
,
518 qemu_irq
*cpu_request_exit
)
523 d
->cpu_request_exit
= cpu_request_exit
;
525 memory_region_init_io(&d
->channel_io
, &channel_io_ops
, d
,
526 "dma-chan", 8 << d
->dshift
);
527 memory_region_add_subregion(isa_address_space_io(NULL
),
528 base
, &d
->channel_io
);
530 isa_register_portio_list(NULL
, page_base
, page_portio_list
, d
,
532 if (pageh_base
>= 0) {
533 isa_register_portio_list(NULL
, pageh_base
, pageh_portio_list
, d
,
537 memory_region_init_io(&d
->cont_io
, &cont_io_ops
, d
, "dma-cont",
539 memory_region_add_subregion(isa_address_space_io(NULL
),
540 base
+ (8 << d
->dshift
), &d
->cont_io
);
542 qemu_register_reset(dma_reset
, d
);
544 for (i
= 0; i
< ARRAY_SIZE (d
->regs
); ++i
) {
545 d
->regs
[i
].transfer_handler
= dma_phony_handler
;
549 static const VMStateDescription vmstate_dma_regs
= {
552 .minimum_version_id
= 1,
553 .minimum_version_id_old
= 1,
554 .fields
= (VMStateField
[]) {
555 VMSTATE_INT32_ARRAY(now
, struct dma_regs
, 2),
556 VMSTATE_UINT16_ARRAY(base
, struct dma_regs
, 2),
557 VMSTATE_UINT8(mode
, struct dma_regs
),
558 VMSTATE_UINT8(page
, struct dma_regs
),
559 VMSTATE_UINT8(pageh
, struct dma_regs
),
560 VMSTATE_UINT8(dack
, struct dma_regs
),
561 VMSTATE_UINT8(eop
, struct dma_regs
),
562 VMSTATE_END_OF_LIST()
566 static int dma_post_load(void *opaque
, int version_id
)
573 static const VMStateDescription vmstate_dma
= {
576 .minimum_version_id
= 1,
577 .minimum_version_id_old
= 1,
578 .post_load
= dma_post_load
,
579 .fields
= (VMStateField
[]) {
580 VMSTATE_UINT8(command
, struct dma_cont
),
581 VMSTATE_UINT8(mask
, struct dma_cont
),
582 VMSTATE_UINT8(flip_flop
, struct dma_cont
),
583 VMSTATE_INT32(dshift
, struct dma_cont
),
584 VMSTATE_STRUCT_ARRAY(regs
, struct dma_cont
, 4, 1, vmstate_dma_regs
, struct dma_regs
),
585 VMSTATE_END_OF_LIST()
589 void DMA_init(int high_page_enable
, qemu_irq
*cpu_request_exit
)
591 dma_init2(&dma_controllers
[0], 0x00, 0, 0x80,
592 high_page_enable
? 0x480 : -1, cpu_request_exit
);
593 dma_init2(&dma_controllers
[1], 0xc0, 1, 0x88,
594 high_page_enable
? 0x488 : -1, cpu_request_exit
);
595 vmstate_register (NULL
, 0, &vmstate_dma
, &dma_controllers
[0]);
596 vmstate_register (NULL
, 1, &vmstate_dma
, &dma_controllers
[1]);
598 dma_bh
= qemu_bh_new(DMA_run_bh
, NULL
);