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 #include "qemu/main-loop.h"
28 /* #define DEBUG_DMA */
30 #define dolog(...) fprintf (stderr, "dma: " __VA_ARGS__)
32 #define linfo(...) fprintf (stderr, "dma: " __VA_ARGS__)
33 #define ldebug(...) fprintf (stderr, "dma: " __VA_ARGS__)
47 DMA_transfer_handler transfer_handler;
54 static struct dma_cont {
60 struct dma_regs regs[4];
61 qemu_irq *cpu_request_exit;
62 MemoryRegion channel_io;
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 void DMA_run (void);
83 static int channels[8] = {-1, 2, 3, 1, -1, -1, -1, 0};
85 static void write_page (void *opaque, uint32_t nport, uint32_t data)
87 struct dma_cont *d = opaque;
90 ichan = channels[nport & 7];
92 dolog ("invalid channel %#x %#x\n", nport, data);
95 d->regs[ichan].page = data;
98 static void write_pageh (void *opaque, uint32_t nport, uint32_t data)
100 struct dma_cont *d = opaque;
103 ichan = channels[nport & 7];
105 dolog ("invalid channel %#x %#x\n", nport, data);
108 d->regs[ichan].pageh = data;
111 static uint32_t read_page (void *opaque, uint32_t nport)
113 struct dma_cont *d = opaque;
116 ichan = channels[nport & 7];
118 dolog ("invalid channel read %#x\n", nport);
121 return d->regs[ichan].page;
124 static uint32_t read_pageh (void *opaque, uint32_t nport)
126 struct dma_cont *d = opaque;
129 ichan = channels[nport & 7];
131 dolog ("invalid channel read %#x\n", nport);
134 return d->regs[ichan].pageh;
137 static inline void init_chan (struct dma_cont *d, int ichan)
142 r->now[ADDR] = r->base[ADDR] << d->dshift;
146 static inline int getff (struct dma_cont *d)
155 static uint64_t read_chan(void *opaque, hwaddr nport, unsigned size)
157 struct dma_cont *d = opaque;
158 int ichan, nreg, iport, ff, val, dir;
161 iport = (nport >> d->dshift) & 0x0f;
166 dir = ((r->mode >> 5) & 1) ? -1 : 1;
169 val = (r->base[COUNT] << d->dshift) - r->now[COUNT];
171 val = r->now[ADDR] + r->now[COUNT] * dir;
173 ldebug ("read_chan %#x -> %d\n", iport, val);
174 return (val >> (d->dshift + (ff << 3))) & 0xff;
177 static void write_chan(void *opaque, hwaddr nport, uint64_t data,
180 struct dma_cont *d = opaque;
181 int iport, ichan, nreg;
184 iport = (nport >> d->dshift) & 0x0f;
189 r->base[nreg] = (r->base[nreg] & 0xff) | ((data << 8) & 0xff00);
190 init_chan (d, ichan);
192 r->base[nreg] = (r->base[nreg] & 0xff00) | (data & 0xff);
196 static void write_cont(void *opaque, hwaddr nport, uint64_t data,
199 struct dma_cont *d = opaque;
200 int iport, ichan = 0;
202 iport = (nport >> d->dshift) & 0x0f;
204 case 0x00: /* command */
205 if ((data != 0) && (data & CMD_NOT_SUPPORTED)) {
206 dolog("command %"PRIx64" not supported\n", data);
215 d->status |= 1 << (ichan + 4);
218 d->status &= ~(1 << (ichan + 4));
220 d->status &= ~(1 << ichan);
224 case 0x02: /* single mask */
226 d->mask |= 1 << (data & 3);
228 d->mask &= ~(1 << (data & 3));
232 case 0x03: /* mode */
237 int op, ai, dir, opmode;
238 op = (data >> 2) & 3;
239 ai = (data >> 4) & 1;
240 dir = (data >> 5) & 1;
241 opmode = (data >> 6) & 3;
243 linfo ("ichan %d, op %d, ai %d, dir %d, opmode %d\n",
244 ichan, op, ai, dir, opmode);
247 d->regs[ichan].mode = data;
251 case 0x04: /* clear flip flop */
255 case 0x05: /* reset */
262 case 0x06: /* clear mask for all channels */
267 case 0x07: /* write mask for all channels */
273 dolog ("unknown iport %#x\n", iport);
279 linfo ("write_cont: nport %#06x, ichan % 2d, val %#06x\n",
285 static uint64_t read_cont(void *opaque, hwaddr nport, unsigned size)
287 struct dma_cont *d = opaque;
290 iport = (nport >> d->dshift) & 0x0f;
292 case 0x00: /* status */
296 case 0x01: /* mask */
304 ldebug ("read_cont: nport %#06x, iport %#04x val %#x\n", nport, iport, val);
308 int DMA_get_channel_mode (int nchan)
310 return dma_controllers[nchan > 3].regs[nchan & 3].mode;
313 void DMA_hold_DREQ (int nchan)
319 linfo ("held cont=%d chan=%d\n", ncont, ichan);
320 dma_controllers[ncont].status |= 1 << (ichan + 4);
324 void DMA_release_DREQ (int nchan)
330 linfo ("released cont=%d chan=%d\n", ncont, ichan);
331 dma_controllers[ncont].status &= ~(1 << (ichan + 4));
335 static void channel_run (int ncont, int ichan)
338 struct dma_regs *r = &dma_controllers[ncont].regs[ichan];
342 dir = (r->mode >> 5) & 1;
343 opmode = (r->mode >> 6) & 3;
346 dolog ("DMA in address decrement mode\n");
349 dolog ("DMA not in single mode select %#x\n", opmode);
353 n = r->transfer_handler (r->opaque, ichan + (ncont << 2),
354 r->now[COUNT], (r->base[COUNT] + 1) << ncont);
356 ldebug ("dma_pos %d size %d\n", n, (r->base[COUNT] + 1) << ncont);
359 static QEMUBH *dma_bh;
361 static void DMA_run (void)
366 static int running = 0;
377 for (icont = 0; icont < 2; icont++, d++) {
378 for (ichan = 0; ichan < 4; ichan++) {
383 if ((0 == (d->mask & mask)) && (0 != (d->status & (mask << 4)))) {
384 channel_run (icont, ichan);
393 qemu_bh_schedule_idle(dma_bh);
396 static void DMA_run_bh(void *unused)
401 void DMA_register_channel (int nchan,
402 DMA_transfer_handler transfer_handler,
411 r = dma_controllers[ncont].regs + ichan;
412 r->transfer_handler = transfer_handler;
416 int DMA_read_memory (int nchan, void *buf, int pos, int len)
418 struct dma_regs *r = &dma_controllers[nchan > 3].regs[nchan & 3];
419 hwaddr addr = ((r->pageh & 0x7f) << 24) | (r->page << 16) | r->now[ADDR];
421 if (r->mode & 0x20) {
425 cpu_physical_memory_read (addr - pos - len, buf, len);
426 /* What about 16bit transfers? */
427 for (i = 0; i < len >> 1; i++) {
428 uint8_t b = p[len - i - 1];
433 cpu_physical_memory_read (addr + pos, buf, len);
438 int DMA_write_memory (int nchan, void *buf, int pos, int len)
440 struct dma_regs *r = &dma_controllers[nchan > 3].regs[nchan & 3];
441 hwaddr addr = ((r->pageh & 0x7f) << 24) | (r->page << 16) | r->now[ADDR];
443 if (r->mode & 0x20) {
447 cpu_physical_memory_write (addr - pos - len, buf, len);
448 /* What about 16bit transfers? */
449 for (i = 0; i < len; i++) {
450 uint8_t b = p[len - i - 1];
455 cpu_physical_memory_write (addr + pos, buf, len);
460 /* request the emulator to transfer a new DMA memory block ASAP */
461 void DMA_schedule(int nchan)
463 struct dma_cont *d = &dma_controllers[nchan > 3];
465 qemu_irq_pulse(*d->cpu_request_exit);
468 static void dma_reset(void *opaque)
470 struct dma_cont *d = opaque;
471 write_cont(d, (0x05 << d->dshift), 0, 1);
474 static int dma_phony_handler (void *opaque, int nchan, int dma_pos, int dma_len)
476 dolog ("unregistered DMA channel used nchan=%d dma_pos=%d dma_len=%d\n",
477 nchan, dma_pos, dma_len);
482 static const MemoryRegionOps channel_io_ops = {
485 .endianness = DEVICE_NATIVE_ENDIAN,
487 .min_access_size = 1,
488 .max_access_size = 1,
492 /* IOport from page_base */
493 static const MemoryRegionPortio page_portio_list[] = {
494 { 0x01, 3, 1, .write = write_page, .read = read_page, },
495 { 0x07, 1, 1, .write = write_page, .read = read_page, },
496 PORTIO_END_OF_LIST(),
499 /* IOport from pageh_base */
500 static const MemoryRegionPortio pageh_portio_list[] = {
501 { 0x01, 3, 1, .write = write_pageh, .read = read_pageh, },
502 { 0x07, 3, 1, .write = write_pageh, .read = read_pageh, },
503 PORTIO_END_OF_LIST(),
506 static const MemoryRegionOps cont_io_ops = {
509 .endianness = DEVICE_NATIVE_ENDIAN,
511 .min_access_size = 1,
512 .max_access_size = 1,
516 /* dshift = 0: 8 bit DMA, 1 = 16 bit DMA */
517 static void dma_init2(struct dma_cont *d, int base, int dshift,
518 int page_base, int pageh_base,
519 qemu_irq *cpu_request_exit)
524 d->cpu_request_exit = cpu_request_exit;
526 memory_region_init_io(&d->channel_io, &channel_io_ops, d,
527 "dma-chan", 8 << d->dshift);
528 memory_region_add_subregion(isa_address_space_io(NULL),
529 base, &d->channel_io);
531 isa_register_portio_list(NULL, page_base, page_portio_list, d,
533 if (pageh_base >= 0) {
534 isa_register_portio_list(NULL, pageh_base, pageh_portio_list, d,
538 memory_region_init_io(&d->cont_io, &cont_io_ops, d, "dma-cont",
540 memory_region_add_subregion(isa_address_space_io(NULL),
541 base + (8 << d->dshift), &d->cont_io);
543 qemu_register_reset(dma_reset, d);
545 for (i = 0; i < ARRAY_SIZE (d->regs); ++i) {
546 d->regs[i].transfer_handler = dma_phony_handler;
550 static const VMStateDescription vmstate_dma_regs = {
553 .minimum_version_id = 1,
554 .minimum_version_id_old = 1,
555 .fields = (VMStateField []) {
556 VMSTATE_INT32_ARRAY(now, struct dma_regs, 2),
557 VMSTATE_UINT16_ARRAY(base, struct dma_regs, 2),
558 VMSTATE_UINT8(mode, struct dma_regs),
559 VMSTATE_UINT8(page, struct dma_regs),
560 VMSTATE_UINT8(pageh, struct dma_regs),
561 VMSTATE_UINT8(dack, struct dma_regs),
562 VMSTATE_UINT8(eop, struct dma_regs),
563 VMSTATE_END_OF_LIST()
567 static int dma_post_load(void *opaque, int version_id)
574 static const VMStateDescription vmstate_dma = {
577 .minimum_version_id = 1,
578 .minimum_version_id_old = 1,
579 .post_load = dma_post_load,
580 .fields = (VMStateField []) {
581 VMSTATE_UINT8(command, struct dma_cont),
582 VMSTATE_UINT8(mask, struct dma_cont),
583 VMSTATE_UINT8(flip_flop, struct dma_cont),
584 VMSTATE_INT32(dshift, struct dma_cont),
585 VMSTATE_STRUCT_ARRAY(regs, struct dma_cont, 4, 1, vmstate_dma_regs, struct dma_regs),
586 VMSTATE_END_OF_LIST()
590 void DMA_init(int high_page_enable, qemu_irq *cpu_request_exit)
592 dma_init2(&dma_controllers[0], 0x00, 0, 0x80,
593 high_page_enable ? 0x480 : -1, cpu_request_exit);
594 dma_init2(&dma_controllers[1], 0xc0, 1, 0x88,
595 high_page_enable ? 0x488 : -1, cpu_request_exit);
596 vmstate_register (NULL, 0, &vmstate_dma, &dma_controllers[0]);
597 vmstate_register (NULL, 1, &vmstate_dma, &dma_controllers[1]);
599 dma_bh = qemu_bh_new(DMA_run_bh, NULL);