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
24 #include "qemu/osdep.h"
26 #include "hw/isa/isa.h"
27 #include "hw/dma/i8257.h"
28 #include "qemu/main-loop.h"
33 OBJECT_CHECK(I8257State, (obj), TYPE_I8257)
35 /* #define DEBUG_DMA */
37 #define dolog(...) fprintf (stderr, "dma: " __VA_ARGS__)
39 #define linfo(...) fprintf (stderr, "dma: " __VA_ARGS__)
40 #define ldebug(...) fprintf (stderr, "dma: " __VA_ARGS__)
50 CMD_MEMORY_TO_MEMORY
= 0x01,
51 CMD_FIXED_ADDRESS
= 0x02,
52 CMD_BLOCK_CONTROLLER
= 0x04,
53 CMD_COMPRESSED_TIME
= 0x08,
54 CMD_CYCLIC_PRIORITY
= 0x10,
55 CMD_EXTENDED_WRITE
= 0x20,
58 CMD_NOT_SUPPORTED
= CMD_MEMORY_TO_MEMORY
| CMD_FIXED_ADDRESS
59 | CMD_COMPRESSED_TIME
| CMD_CYCLIC_PRIORITY
| CMD_EXTENDED_WRITE
60 | CMD_LOW_DREQ
| CMD_LOW_DACK
64 static void i8257_dma_run(void *opaque
);
66 static const int channels
[8] = {-1, 2, 3, 1, -1, -1, -1, 0};
68 static void i8257_write_page(void *opaque
, uint32_t nport
, uint32_t data
)
70 I8257State
*d
= opaque
;
73 ichan
= channels
[nport
& 7];
75 dolog ("invalid channel %#x %#x\n", nport
, data
);
78 d
->regs
[ichan
].page
= data
;
81 static void i8257_write_pageh(void *opaque
, uint32_t nport
, uint32_t data
)
83 I8257State
*d
= opaque
;
86 ichan
= channels
[nport
& 7];
88 dolog ("invalid channel %#x %#x\n", nport
, data
);
91 d
->regs
[ichan
].pageh
= data
;
94 static uint32_t i8257_read_page(void *opaque
, uint32_t nport
)
96 I8257State
*d
= opaque
;
99 ichan
= channels
[nport
& 7];
101 dolog ("invalid channel read %#x\n", nport
);
104 return d
->regs
[ichan
].page
;
107 static uint32_t i8257_read_pageh(void *opaque
, uint32_t nport
)
109 I8257State
*d
= opaque
;
112 ichan
= channels
[nport
& 7];
114 dolog ("invalid channel read %#x\n", nport
);
117 return d
->regs
[ichan
].pageh
;
120 static inline void i8257_init_chan(I8257State
*d
, int ichan
)
125 r
->now
[ADDR
] = r
->base
[ADDR
] << d
->dshift
;
129 static inline int i8257_getff(I8257State
*d
)
138 static uint64_t i8257_read_chan(void *opaque
, hwaddr nport
, unsigned size
)
140 I8257State
*d
= opaque
;
141 int ichan
, nreg
, iport
, ff
, val
, dir
;
144 iport
= (nport
>> d
->dshift
) & 0x0f;
149 dir
= ((r
->mode
>> 5) & 1) ? -1 : 1;
152 val
= (r
->base
[COUNT
] << d
->dshift
) - r
->now
[COUNT
];
154 val
= r
->now
[ADDR
] + r
->now
[COUNT
] * dir
;
156 ldebug ("read_chan %#x -> %d\n", iport
, val
);
157 return (val
>> (d
->dshift
+ (ff
<< 3))) & 0xff;
160 static void i8257_write_chan(void *opaque
, hwaddr nport
, uint64_t data
,
163 I8257State
*d
= opaque
;
164 int iport
, ichan
, nreg
;
167 iport
= (nport
>> d
->dshift
) & 0x0f;
171 if (i8257_getff(d
)) {
172 r
->base
[nreg
] = (r
->base
[nreg
] & 0xff) | ((data
<< 8) & 0xff00);
173 i8257_init_chan(d
, ichan
);
175 r
->base
[nreg
] = (r
->base
[nreg
] & 0xff00) | (data
& 0xff);
179 static void i8257_write_cont(void *opaque
, hwaddr nport
, uint64_t data
,
182 I8257State
*d
= opaque
;
183 int iport
, ichan
= 0;
185 iport
= (nport
>> d
->dshift
) & 0x0f;
187 case 0x00: /* command */
188 if ((data
!= 0) && (data
& CMD_NOT_SUPPORTED
)) {
189 qemu_log_mask(LOG_UNIMP
, "%s: cmd 0x%02"PRIx64
" not supported\n",
199 d
->status
|= 1 << (ichan
+ 4);
202 d
->status
&= ~(1 << (ichan
+ 4));
204 d
->status
&= ~(1 << ichan
);
208 case 0x02: /* single mask */
210 d
->mask
|= 1 << (data
& 3);
212 d
->mask
&= ~(1 << (data
& 3));
216 case 0x03: /* mode */
221 int op
, ai
, dir
, opmode
;
222 op
= (data
>> 2) & 3;
223 ai
= (data
>> 4) & 1;
224 dir
= (data
>> 5) & 1;
225 opmode
= (data
>> 6) & 3;
227 linfo ("ichan %d, op %d, ai %d, dir %d, opmode %d\n",
228 ichan
, op
, ai
, dir
, opmode
);
231 d
->regs
[ichan
].mode
= data
;
235 case 0x04: /* clear flip flop */
239 case 0x05: /* reset */
246 case 0x06: /* clear mask for all channels */
251 case 0x07: /* write mask for all channels */
257 dolog ("unknown iport %#x\n", iport
);
263 linfo ("write_cont: nport %#06x, ichan % 2d, val %#06x\n",
269 static uint64_t i8257_read_cont(void *opaque
, hwaddr nport
, unsigned size
)
271 I8257State
*d
= opaque
;
274 iport
= (nport
>> d
->dshift
) & 0x0f;
276 case 0x00: /* status */
280 case 0x01: /* mask */
288 ldebug ("read_cont: nport %#06x, iport %#04x val %#x\n", nport
, iport
, val
);
292 static IsaDmaTransferMode
i8257_dma_get_transfer_mode(IsaDma
*obj
, int nchan
)
294 I8257State
*d
= I8257(obj
);
295 return (d
->regs
[nchan
& 3].mode
>> 2) & 3;
298 static bool i8257_dma_has_autoinitialization(IsaDma
*obj
, int nchan
)
300 I8257State
*d
= I8257(obj
);
301 return (d
->regs
[nchan
& 3].mode
>> 4) & 1;
304 static void i8257_dma_hold_DREQ(IsaDma
*obj
, int nchan
)
306 I8257State
*d
= I8257(obj
);
310 d
->status
|= 1 << (ichan
+ 4);
314 static void i8257_dma_release_DREQ(IsaDma
*obj
, int nchan
)
316 I8257State
*d
= I8257(obj
);
320 d
->status
&= ~(1 << (ichan
+ 4));
324 static void i8257_channel_run(I8257State
*d
, int ichan
)
326 int ncont
= d
->dshift
;
328 I8257Regs
*r
= &d
->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 n
= r
->transfer_handler (r
->opaque
, ichan
+ (ncont
<< 2),
344 r
->now
[COUNT
], (r
->base
[COUNT
] + 1) << ncont
);
346 ldebug ("dma_pos %d size %d\n", n
, (r
->base
[COUNT
] + 1) << ncont
);
347 if (n
== (r
->base
[COUNT
] + 1) << ncont
) {
348 ldebug("transfer done\n");
349 d
->status
|= (1 << ichan
);
353 static void i8257_dma_run(void *opaque
)
355 I8257State
*d
= opaque
;
366 for (ichan
= 0; ichan
< 4; ichan
++) {
371 if ((0 == (d
->mask
& mask
)) && (0 != (d
->status
& (mask
<< 4)))) {
372 i8257_channel_run(d
, ichan
);
380 qemu_bh_schedule_idle(d
->dma_bh
);
381 d
->dma_bh_scheduled
= true;
385 static void i8257_dma_register_channel(IsaDma
*obj
, int nchan
,
386 IsaDmaTransferHandler transfer_handler
,
389 I8257State
*d
= I8257(obj
);
396 r
->transfer_handler
= transfer_handler
;
400 static int i8257_dma_read_memory(IsaDma
*obj
, int nchan
, void *buf
, int pos
,
403 I8257State
*d
= I8257(obj
);
404 I8257Regs
*r
= &d
->regs
[nchan
& 3];
405 hwaddr addr
= ((r
->pageh
& 0x7f) << 24) | (r
->page
<< 16) | r
->now
[ADDR
];
407 if (r
->mode
& 0x20) {
411 cpu_physical_memory_read (addr
- pos
- len
, buf
, len
);
412 /* What about 16bit transfers? */
413 for (i
= 0; i
< len
>> 1; i
++) {
414 uint8_t b
= p
[len
- i
- 1];
419 cpu_physical_memory_read (addr
+ pos
, buf
, len
);
424 static int i8257_dma_write_memory(IsaDma
*obj
, int nchan
, void *buf
, int pos
,
427 I8257State
*s
= I8257(obj
);
428 I8257Regs
*r
= &s
->regs
[nchan
& 3];
429 hwaddr addr
= ((r
->pageh
& 0x7f) << 24) | (r
->page
<< 16) | r
->now
[ADDR
];
431 if (r
->mode
& 0x20) {
435 cpu_physical_memory_write (addr
- pos
- len
, buf
, len
);
436 /* What about 16bit transfers? */
437 for (i
= 0; i
< len
; i
++) {
438 uint8_t b
= p
[len
- i
- 1];
443 cpu_physical_memory_write (addr
+ pos
, buf
, len
);
448 /* request the emulator to transfer a new DMA memory block ASAP (even
449 * if the idle bottom half would not have exited the iothread yet).
451 static void i8257_dma_schedule(IsaDma
*obj
)
453 I8257State
*d
= I8257(obj
);
454 if (d
->dma_bh_scheduled
) {
459 static void i8257_reset(DeviceState
*dev
)
461 I8257State
*d
= I8257(dev
);
462 i8257_write_cont(d
, (0x05 << d
->dshift
), 0, 1);
465 static int i8257_phony_handler(void *opaque
, int nchan
, int dma_pos
,
468 trace_i8257_unregistered_dma(nchan
, dma_pos
, dma_len
);
473 static const MemoryRegionOps channel_io_ops
= {
474 .read
= i8257_read_chan
,
475 .write
= i8257_write_chan
,
476 .endianness
= DEVICE_NATIVE_ENDIAN
,
478 .min_access_size
= 1,
479 .max_access_size
= 1,
483 /* IOport from page_base */
484 static const MemoryRegionPortio page_portio_list
[] = {
485 { 0x01, 3, 1, .write
= i8257_write_page
, .read
= i8257_read_page
, },
486 { 0x07, 1, 1, .write
= i8257_write_page
, .read
= i8257_read_page
, },
487 PORTIO_END_OF_LIST(),
490 /* IOport from pageh_base */
491 static const MemoryRegionPortio pageh_portio_list
[] = {
492 { 0x01, 3, 1, .write
= i8257_write_pageh
, .read
= i8257_read_pageh
, },
493 { 0x07, 3, 1, .write
= i8257_write_pageh
, .read
= i8257_read_pageh
, },
494 PORTIO_END_OF_LIST(),
497 static const MemoryRegionOps cont_io_ops
= {
498 .read
= i8257_read_cont
,
499 .write
= i8257_write_cont
,
500 .endianness
= DEVICE_NATIVE_ENDIAN
,
502 .min_access_size
= 1,
503 .max_access_size
= 1,
507 static const VMStateDescription vmstate_i8257_regs
= {
510 .minimum_version_id
= 1,
511 .fields
= (VMStateField
[]) {
512 VMSTATE_INT32_ARRAY(now
, I8257Regs
, 2),
513 VMSTATE_UINT16_ARRAY(base
, I8257Regs
, 2),
514 VMSTATE_UINT8(mode
, I8257Regs
),
515 VMSTATE_UINT8(page
, I8257Regs
),
516 VMSTATE_UINT8(pageh
, I8257Regs
),
517 VMSTATE_UINT8(dack
, I8257Regs
),
518 VMSTATE_UINT8(eop
, I8257Regs
),
519 VMSTATE_END_OF_LIST()
523 static int i8257_post_load(void *opaque
, int version_id
)
525 I8257State
*d
= opaque
;
531 static const VMStateDescription vmstate_i8257
= {
534 .minimum_version_id
= 1,
535 .post_load
= i8257_post_load
,
536 .fields
= (VMStateField
[]) {
537 VMSTATE_UINT8(command
, I8257State
),
538 VMSTATE_UINT8(mask
, I8257State
),
539 VMSTATE_UINT8(flip_flop
, I8257State
),
540 VMSTATE_INT32(dshift
, I8257State
),
541 VMSTATE_STRUCT_ARRAY(regs
, I8257State
, 4, 1, vmstate_i8257_regs
,
543 VMSTATE_END_OF_LIST()
547 static void i8257_realize(DeviceState
*dev
, Error
**errp
)
549 ISADevice
*isa
= ISA_DEVICE(dev
);
550 I8257State
*d
= I8257(dev
);
553 memory_region_init_io(&d
->channel_io
, NULL
, &channel_io_ops
, d
,
554 "dma-chan", 8 << d
->dshift
);
555 memory_region_add_subregion(isa_address_space_io(isa
),
556 d
->base
, &d
->channel_io
);
558 isa_register_portio_list(isa
, &d
->portio_page
,
559 d
->page_base
, page_portio_list
, d
,
561 if (d
->pageh_base
>= 0) {
562 isa_register_portio_list(isa
, &d
->portio_pageh
,
563 d
->pageh_base
, pageh_portio_list
, d
,
567 memory_region_init_io(&d
->cont_io
, OBJECT(isa
), &cont_io_ops
, d
,
568 "dma-cont", 8 << d
->dshift
);
569 memory_region_add_subregion(isa_address_space_io(isa
),
570 d
->base
+ (8 << d
->dshift
), &d
->cont_io
);
572 for (i
= 0; i
< ARRAY_SIZE(d
->regs
); ++i
) {
573 d
->regs
[i
].transfer_handler
= i8257_phony_handler
;
576 d
->dma_bh
= qemu_bh_new(i8257_dma_run
, d
);
579 static Property i8257_properties
[] = {
580 DEFINE_PROP_INT32("base", I8257State
, base
, 0x00),
581 DEFINE_PROP_INT32("page-base", I8257State
, page_base
, 0x80),
582 DEFINE_PROP_INT32("pageh-base", I8257State
, pageh_base
, 0x480),
583 DEFINE_PROP_INT32("dshift", I8257State
, dshift
, 0),
584 DEFINE_PROP_END_OF_LIST()
587 static void i8257_class_init(ObjectClass
*klass
, void *data
)
589 DeviceClass
*dc
= DEVICE_CLASS(klass
);
590 IsaDmaClass
*idc
= ISADMA_CLASS(klass
);
592 dc
->realize
= i8257_realize
;
593 dc
->reset
= i8257_reset
;
594 dc
->vmsd
= &vmstate_i8257
;
595 dc
->props
= i8257_properties
;
597 idc
->get_transfer_mode
= i8257_dma_get_transfer_mode
;
598 idc
->has_autoinitialization
= i8257_dma_has_autoinitialization
;
599 idc
->read_memory
= i8257_dma_read_memory
;
600 idc
->write_memory
= i8257_dma_write_memory
;
601 idc
->hold_DREQ
= i8257_dma_hold_DREQ
;
602 idc
->release_DREQ
= i8257_dma_release_DREQ
;
603 idc
->schedule
= i8257_dma_schedule
;
604 idc
->register_channel
= i8257_dma_register_channel
;
605 /* Reason: needs to be wired up by isa_bus_dma() to work */
606 dc
->user_creatable
= false;
609 static const TypeInfo i8257_info
= {
611 .parent
= TYPE_ISA_DEVICE
,
612 .instance_size
= sizeof(I8257State
),
613 .class_init
= i8257_class_init
,
614 .interfaces
= (InterfaceInfo
[]) {
620 static void i8257_register_types(void)
622 type_register_static(&i8257_info
);
625 type_init(i8257_register_types
)
627 void i8257_dma_init(ISABus
*bus
, bool high_page_enable
)
629 ISADevice
*isa1
, *isa2
;
632 isa1
= isa_create(bus
, TYPE_I8257
);
634 qdev_prop_set_int32(d
, "base", 0x00);
635 qdev_prop_set_int32(d
, "page-base", 0x80);
636 qdev_prop_set_int32(d
, "pageh-base", high_page_enable
? 0x480 : -1);
637 qdev_prop_set_int32(d
, "dshift", 0);
640 isa2
= isa_create(bus
, TYPE_I8257
);
642 qdev_prop_set_int32(d
, "base", 0xc0);
643 qdev_prop_set_int32(d
, "page-base", 0x88);
644 qdev_prop_set_int32(d
, "pageh-base", high_page_enable
? 0x488 : -1);
645 qdev_prop_set_int32(d
, "dshift", 1);
648 isa_bus_dma(bus
, ISADMA(isa1
), ISADMA(isa2
));