]> git.proxmox.com Git - mirror_qemu.git/blame - hw/dma/i8257.c
i8257: rename struct dma_regs to I8257Regs
[mirror_qemu.git] / hw / dma / i8257.c
CommitLineData
27503323
FB
1/*
2 * QEMU DMA emulation
85571bc7
FB
3 *
4 * Copyright (c) 2003-2004 Vassili Karpov (malc)
5 *
27503323
FB
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
b6a0aa05 24#include "qemu/osdep.h"
83c9f4ca 25#include "hw/hw.h"
0d09e41a 26#include "hw/isa/isa.h"
1de7afc9 27#include "qemu/main-loop.h"
7dbb4c49 28#include "trace.h"
27503323 29
85571bc7 30/* #define DEBUG_DMA */
7ebb5e41 31
85571bc7 32#define dolog(...) fprintf (stderr, "dma: " __VA_ARGS__)
27503323 33#ifdef DEBUG_DMA
27503323
FB
34#define linfo(...) fprintf (stderr, "dma: " __VA_ARGS__)
35#define ldebug(...) fprintf (stderr, "dma: " __VA_ARGS__)
36#else
27503323
FB
37#define linfo(...)
38#define ldebug(...)
39#endif
40
0eee6d62 41typedef struct I8257Regs {
27503323
FB
42 int now[2];
43 uint16_t base[2];
44 uint8_t mode;
45 uint8_t page;
b0bda528 46 uint8_t pageh;
27503323
FB
47 uint8_t dack;
48 uint8_t eop;
16f62432
FB
49 DMA_transfer_handler transfer_handler;
50 void *opaque;
0eee6d62 51} I8257Regs;
27503323
FB
52
53#define ADDR 0
54#define COUNT 1
55
6a128b13 56typedef struct I8257State {
27503323
FB
57 uint8_t status;
58 uint8_t command;
59 uint8_t mask;
60 uint8_t flip_flop;
9eb153f1 61 int dshift;
0eee6d62 62 I8257Regs regs[4];
58229933
JG
63 MemoryRegion channel_io;
64 MemoryRegion cont_io;
6a128b13
HP
65} I8257State;
66
67static I8257State dma_controllers[2];
27503323
FB
68
69enum {
e875c40a
FB
70 CMD_MEMORY_TO_MEMORY = 0x01,
71 CMD_FIXED_ADDRESS = 0x02,
72 CMD_BLOCK_CONTROLLER = 0x04,
73 CMD_COMPRESSED_TIME = 0x08,
74 CMD_CYCLIC_PRIORITY = 0x10,
75 CMD_EXTENDED_WRITE = 0x20,
76 CMD_LOW_DREQ = 0x40,
77 CMD_LOW_DACK = 0x80,
78 CMD_NOT_SUPPORTED = CMD_MEMORY_TO_MEMORY | CMD_FIXED_ADDRESS
79 | CMD_COMPRESSED_TIME | CMD_CYCLIC_PRIORITY | CMD_EXTENDED_WRITE
80 | CMD_LOW_DREQ | CMD_LOW_DACK
27503323
FB
81
82};
83
492c30af
AL
84static void DMA_run (void);
85
9eb153f1
FB
86static int channels[8] = {-1, 2, 3, 1, -1, -1, -1, 0};
87
7d977de7 88static void write_page (void *opaque, uint32_t nport, uint32_t data)
27503323 89{
6a128b13 90 I8257State *d = opaque;
27503323 91 int ichan;
27503323 92
9eb153f1 93 ichan = channels[nport & 7];
27503323 94 if (-1 == ichan) {
85571bc7 95 dolog ("invalid channel %#x %#x\n", nport, data);
27503323
FB
96 return;
97 }
9eb153f1
FB
98 d->regs[ichan].page = data;
99}
100
b0bda528 101static void write_pageh (void *opaque, uint32_t nport, uint32_t data)
9eb153f1 102{
6a128b13 103 I8257State *d = opaque;
9eb153f1 104 int ichan;
27503323 105
9eb153f1 106 ichan = channels[nport & 7];
b0bda528 107 if (-1 == ichan) {
85571bc7 108 dolog ("invalid channel %#x %#x\n", nport, data);
b0bda528
FB
109 return;
110 }
111 d->regs[ichan].pageh = data;
112}
9eb153f1 113
b0bda528
FB
114static uint32_t read_page (void *opaque, uint32_t nport)
115{
6a128b13 116 I8257State *d = opaque;
b0bda528
FB
117 int ichan;
118
119 ichan = channels[nport & 7];
9eb153f1 120 if (-1 == ichan) {
85571bc7 121 dolog ("invalid channel read %#x\n", nport);
9eb153f1
FB
122 return 0;
123 }
124 return d->regs[ichan].page;
27503323
FB
125}
126
b0bda528
FB
127static uint32_t read_pageh (void *opaque, uint32_t nport)
128{
6a128b13 129 I8257State *d = opaque;
b0bda528
FB
130 int ichan;
131
132 ichan = channels[nport & 7];
133 if (-1 == ichan) {
85571bc7 134 dolog ("invalid channel read %#x\n", nport);
b0bda528
FB
135 return 0;
136 }
137 return d->regs[ichan].pageh;
138}
139
6a128b13 140static inline void init_chan(I8257State *d, int ichan)
27503323 141{
0eee6d62 142 I8257Regs *r;
27503323 143
9eb153f1 144 r = d->regs + ichan;
85571bc7 145 r->now[ADDR] = r->base[ADDR] << d->dshift;
27503323
FB
146 r->now[COUNT] = 0;
147}
148
6a128b13 149static inline int getff(I8257State *d)
27503323
FB
150{
151 int ff;
152
9eb153f1
FB
153 ff = d->flip_flop;
154 d->flip_flop = !ff;
27503323
FB
155 return ff;
156}
157
58229933 158static uint64_t read_chan(void *opaque, hwaddr nport, unsigned size)
27503323 159{
6a128b13 160 I8257State *d = opaque;
85571bc7 161 int ichan, nreg, iport, ff, val, dir;
0eee6d62 162 I8257Regs *r;
27503323 163
9eb153f1
FB
164 iport = (nport >> d->dshift) & 0x0f;
165 ichan = iport >> 1;
166 nreg = iport & 1;
167 r = d->regs + ichan;
27503323 168
85571bc7 169 dir = ((r->mode >> 5) & 1) ? -1 : 1;
9eb153f1 170 ff = getff (d);
27503323 171 if (nreg)
9eb153f1 172 val = (r->base[COUNT] << d->dshift) - r->now[COUNT];
27503323 173 else
85571bc7 174 val = r->now[ADDR] + r->now[COUNT] * dir;
27503323 175
85571bc7 176 ldebug ("read_chan %#x -> %d\n", iport, val);
9eb153f1 177 return (val >> (d->dshift + (ff << 3))) & 0xff;
27503323
FB
178}
179
58229933
JG
180static void write_chan(void *opaque, hwaddr nport, uint64_t data,
181 unsigned size)
27503323 182{
6a128b13 183 I8257State *d = opaque;
9eb153f1 184 int iport, ichan, nreg;
0eee6d62 185 I8257Regs *r;
27503323 186
9eb153f1
FB
187 iport = (nport >> d->dshift) & 0x0f;
188 ichan = iport >> 1;
189 nreg = iport & 1;
190 r = d->regs + ichan;
191 if (getff (d)) {
3504fe17 192 r->base[nreg] = (r->base[nreg] & 0xff) | ((data << 8) & 0xff00);
9eb153f1 193 init_chan (d, ichan);
3504fe17
FB
194 } else {
195 r->base[nreg] = (r->base[nreg] & 0xff00) | (data & 0xff);
27503323 196 }
27503323
FB
197}
198
58229933
JG
199static void write_cont(void *opaque, hwaddr nport, uint64_t data,
200 unsigned size)
27503323 201{
6a128b13 202 I8257State *d = opaque;
85571bc7 203 int iport, ichan = 0;
27503323 204
9eb153f1 205 iport = (nport >> d->dshift) & 0x0f;
27503323 206 switch (iport) {
ecd584b8 207 case 0x00: /* command */
df475d18 208 if ((data != 0) && (data & CMD_NOT_SUPPORTED)) {
58229933 209 dolog("command %"PRIx64" not supported\n", data);
df475d18 210 return;
27503323
FB
211 }
212 d->command = data;
213 break;
214
ecd584b8 215 case 0x01:
27503323
FB
216 ichan = data & 3;
217 if (data & 4) {
218 d->status |= 1 << (ichan + 4);
219 }
220 else {
221 d->status &= ~(1 << (ichan + 4));
222 }
223 d->status &= ~(1 << ichan);
492c30af 224 DMA_run();
27503323
FB
225 break;
226
ecd584b8 227 case 0x02: /* single mask */
27503323
FB
228 if (data & 4)
229 d->mask |= 1 << (data & 3);
230 else
231 d->mask &= ~(1 << (data & 3));
492c30af 232 DMA_run();
27503323
FB
233 break;
234
ecd584b8 235 case 0x03: /* mode */
27503323 236 {
16d17fdb
FB
237 ichan = data & 3;
238#ifdef DEBUG_DMA
85571bc7
FB
239 {
240 int op, ai, dir, opmode;
e875c40a
FB
241 op = (data >> 2) & 3;
242 ai = (data >> 4) & 1;
243 dir = (data >> 5) & 1;
244 opmode = (data >> 6) & 3;
27503323 245
e875c40a
FB
246 linfo ("ichan %d, op %d, ai %d, dir %d, opmode %d\n",
247 ichan, op, ai, dir, opmode);
85571bc7 248 }
27503323 249#endif
27503323
FB
250 d->regs[ichan].mode = data;
251 break;
252 }
253
ecd584b8 254 case 0x04: /* clear flip flop */
27503323
FB
255 d->flip_flop = 0;
256 break;
257
ecd584b8 258 case 0x05: /* reset */
27503323
FB
259 d->flip_flop = 0;
260 d->mask = ~0;
261 d->status = 0;
262 d->command = 0;
263 break;
264
ecd584b8 265 case 0x06: /* clear mask for all channels */
27503323 266 d->mask = 0;
492c30af 267 DMA_run();
27503323
FB
268 break;
269
ecd584b8 270 case 0x07: /* write mask for all channels */
27503323 271 d->mask = data;
492c30af 272 DMA_run();
27503323
FB
273 break;
274
275 default:
85571bc7 276 dolog ("unknown iport %#x\n", iport);
df475d18 277 break;
27503323
FB
278 }
279
16d17fdb 280#ifdef DEBUG_DMA
27503323 281 if (0xc != iport) {
85571bc7 282 linfo ("write_cont: nport %#06x, ichan % 2d, val %#06x\n",
9eb153f1 283 nport, ichan, data);
27503323
FB
284 }
285#endif
27503323
FB
286}
287
58229933 288static uint64_t read_cont(void *opaque, hwaddr nport, unsigned size)
9eb153f1 289{
6a128b13 290 I8257State *d = opaque;
9eb153f1 291 int iport, val;
85571bc7 292
9eb153f1
FB
293 iport = (nport >> d->dshift) & 0x0f;
294 switch (iport) {
ecd584b8 295 case 0x00: /* status */
9eb153f1
FB
296 val = d->status;
297 d->status &= 0xf0;
298 break;
ecd584b8 299 case 0x01: /* mask */
9eb153f1
FB
300 val = d->mask;
301 break;
302 default:
303 val = 0;
304 break;
305 }
85571bc7
FB
306
307 ldebug ("read_cont: nport %#06x, iport %#04x val %#x\n", nport, iport, val);
9eb153f1
FB
308 return val;
309}
310
27503323
FB
311int DMA_get_channel_mode (int nchan)
312{
313 return dma_controllers[nchan > 3].regs[nchan & 3].mode;
314}
315
316void DMA_hold_DREQ (int nchan)
317{
318 int ncont, ichan;
319
320 ncont = nchan > 3;
321 ichan = nchan & 3;
322 linfo ("held cont=%d chan=%d\n", ncont, ichan);
323 dma_controllers[ncont].status |= 1 << (ichan + 4);
492c30af 324 DMA_run();
27503323
FB
325}
326
327void DMA_release_DREQ (int nchan)
328{
329 int ncont, ichan;
330
331 ncont = nchan > 3;
332 ichan = nchan & 3;
333 linfo ("released cont=%d chan=%d\n", ncont, ichan);
334 dma_controllers[ncont].status &= ~(1 << (ichan + 4));
492c30af 335 DMA_run();
27503323
FB
336}
337
338static void channel_run (int ncont, int ichan)
339{
27503323 340 int n;
0eee6d62 341 I8257Regs *r = &dma_controllers[ncont].regs[ichan];
85571bc7
FB
342#ifdef DEBUG_DMA
343 int dir, opmode;
27503323 344
85571bc7
FB
345 dir = (r->mode >> 5) & 1;
346 opmode = (r->mode >> 6) & 3;
27503323 347
85571bc7
FB
348 if (dir) {
349 dolog ("DMA in address decrement mode\n");
350 }
351 if (opmode != 1) {
352 dolog ("DMA not in single mode select %#x\n", opmode);
353 }
354#endif
27503323 355
85571bc7
FB
356 n = r->transfer_handler (r->opaque, ichan + (ncont << 2),
357 r->now[COUNT], (r->base[COUNT] + 1) << ncont);
358 r->now[COUNT] = n;
359 ldebug ("dma_pos %d size %d\n", n, (r->base[COUNT] + 1) << ncont);
27503323
FB
360}
361
492c30af 362static QEMUBH *dma_bh;
19d2b5e6 363static bool dma_bh_scheduled;
492c30af
AL
364
365static void DMA_run (void)
27503323 366{
6a128b13 367 I8257State *d;
27503323 368 int icont, ichan;
492c30af 369 int rearm = 0;
acae6f1c
KW
370 static int running = 0;
371
372 if (running) {
373 rearm = 1;
374 goto out;
375 } else {
376 running = 1;
377 }
27503323 378
27503323
FB
379 d = dma_controllers;
380
381 for (icont = 0; icont < 2; icont++, d++) {
382 for (ichan = 0; ichan < 4; ichan++) {
383 int mask;
384
385 mask = 1 << ichan;
386
492c30af 387 if ((0 == (d->mask & mask)) && (0 != (d->status & (mask << 4)))) {
27503323 388 channel_run (icont, ichan);
492c30af
AL
389 rearm = 1;
390 }
27503323
FB
391 }
392 }
492c30af 393
acae6f1c
KW
394 running = 0;
395out:
19d2b5e6 396 if (rearm) {
492c30af 397 qemu_bh_schedule_idle(dma_bh);
19d2b5e6
PB
398 dma_bh_scheduled = true;
399 }
492c30af
AL
400}
401
402static void DMA_run_bh(void *unused)
403{
19d2b5e6 404 dma_bh_scheduled = false;
492c30af 405 DMA_run();
27503323
FB
406}
407
408void DMA_register_channel (int nchan,
85571bc7 409 DMA_transfer_handler transfer_handler,
16f62432 410 void *opaque)
27503323 411{
0eee6d62 412 I8257Regs *r;
27503323
FB
413 int ichan, ncont;
414
415 ncont = nchan > 3;
416 ichan = nchan & 3;
417
418 r = dma_controllers[ncont].regs + ichan;
16f62432
FB
419 r->transfer_handler = transfer_handler;
420 r->opaque = opaque;
421}
422
85571bc7
FB
423int DMA_read_memory (int nchan, void *buf, int pos, int len)
424{
0eee6d62 425 I8257Regs *r = &dma_controllers[nchan > 3].regs[nchan & 3];
a8170e5e 426 hwaddr addr = ((r->pageh & 0x7f) << 24) | (r->page << 16) | r->now[ADDR];
85571bc7
FB
427
428 if (r->mode & 0x20) {
429 int i;
430 uint8_t *p = buf;
431
432 cpu_physical_memory_read (addr - pos - len, buf, len);
433 /* What about 16bit transfers? */
434 for (i = 0; i < len >> 1; i++) {
435 uint8_t b = p[len - i - 1];
436 p[i] = b;
437 }
438 }
439 else
440 cpu_physical_memory_read (addr + pos, buf, len);
441
442 return len;
443}
444
445int DMA_write_memory (int nchan, void *buf, int pos, int len)
446{
0eee6d62 447 I8257Regs *r = &dma_controllers[nchan > 3].regs[nchan & 3];
a8170e5e 448 hwaddr addr = ((r->pageh & 0x7f) << 24) | (r->page << 16) | r->now[ADDR];
85571bc7
FB
449
450 if (r->mode & 0x20) {
451 int i;
452 uint8_t *p = buf;
453
454 cpu_physical_memory_write (addr - pos - len, buf, len);
455 /* What about 16bit transfers? */
456 for (i = 0; i < len; i++) {
457 uint8_t b = p[len - i - 1];
458 p[i] = b;
459 }
460 }
461 else
462 cpu_physical_memory_write (addr + pos, buf, len);
463
464 return len;
465}
466
19d2b5e6
PB
467/* request the emulator to transfer a new DMA memory block ASAP (even
468 * if the idle bottom half would not have exited the iothread yet).
469 */
470void DMA_schedule(void)
16f62432 471{
19d2b5e6
PB
472 if (dma_bh_scheduled) {
473 qemu_notify_event();
474 }
27503323
FB
475}
476
d7d02e3c
FB
477static void dma_reset(void *opaque)
478{
6a128b13 479 I8257State *d = opaque;
ecd584b8 480 write_cont(d, (0x05 << d->dshift), 0, 1);
d7d02e3c
FB
481}
482
ca9cc28c
AZ
483static int dma_phony_handler (void *opaque, int nchan, int dma_pos, int dma_len)
484{
7dbb4c49 485 trace_i8257_unregistered_dma(nchan, dma_pos, dma_len);
ca9cc28c
AZ
486 return dma_pos;
487}
488
58229933
JG
489
490static const MemoryRegionOps channel_io_ops = {
491 .read = read_chan,
492 .write = write_chan,
493 .endianness = DEVICE_NATIVE_ENDIAN,
494 .impl = {
495 .min_access_size = 1,
496 .max_access_size = 1,
497 },
498};
499
500/* IOport from page_base */
501static const MemoryRegionPortio page_portio_list[] = {
502 { 0x01, 3, 1, .write = write_page, .read = read_page, },
503 { 0x07, 1, 1, .write = write_page, .read = read_page, },
504 PORTIO_END_OF_LIST(),
505};
506
507/* IOport from pageh_base */
508static const MemoryRegionPortio pageh_portio_list[] = {
509 { 0x01, 3, 1, .write = write_pageh, .read = read_pageh, },
510 { 0x07, 3, 1, .write = write_pageh, .read = read_pageh, },
511 PORTIO_END_OF_LIST(),
512};
513
514static const MemoryRegionOps cont_io_ops = {
515 .read = read_cont,
516 .write = write_cont,
517 .endianness = DEVICE_NATIVE_ENDIAN,
518 .impl = {
519 .min_access_size = 1,
520 .max_access_size = 1,
521 },
522};
523
9eb153f1 524/* dshift = 0: 8 bit DMA, 1 = 16 bit DMA */
6a128b13 525static void dma_init2(I8257State *d, int base, int dshift,
5039d6e2 526 int page_base, int pageh_base)
27503323
FB
527{
528 int i;
27503323 529
9eb153f1 530 d->dshift = dshift;
58229933 531
2c9b15ca 532 memory_region_init_io(&d->channel_io, NULL, &channel_io_ops, d,
58229933
JG
533 "dma-chan", 8 << d->dshift);
534 memory_region_add_subregion(isa_address_space_io(NULL),
535 base, &d->channel_io);
536
537 isa_register_portio_list(NULL, page_base, page_portio_list, d,
538 "dma-page");
539 if (pageh_base >= 0) {
540 isa_register_portio_list(NULL, pageh_base, pageh_portio_list, d,
541 "dma-pageh");
27503323 542 }
58229933 543
2c9b15ca 544 memory_region_init_io(&d->cont_io, NULL, &cont_io_ops, d, "dma-cont",
58229933
JG
545 8 << d->dshift);
546 memory_region_add_subregion(isa_address_space_io(NULL),
547 base + (8 << d->dshift), &d->cont_io);
548
a08d4367 549 qemu_register_reset(dma_reset, d);
d7d02e3c 550 dma_reset(d);
b1503cda 551 for (i = 0; i < ARRAY_SIZE (d->regs); ++i) {
ca9cc28c
AZ
552 d->regs[i].transfer_handler = dma_phony_handler;
553 }
9eb153f1 554}
27503323 555
0eee6d62 556static const VMStateDescription vmstate_i8257_regs = {
7b5045c5
JQ
557 .name = "dma_regs",
558 .version_id = 1,
559 .minimum_version_id = 1,
d49805ae 560 .fields = (VMStateField[]) {
0eee6d62
HP
561 VMSTATE_INT32_ARRAY(now, I8257Regs, 2),
562 VMSTATE_UINT16_ARRAY(base, I8257Regs, 2),
563 VMSTATE_UINT8(mode, I8257Regs),
564 VMSTATE_UINT8(page, I8257Regs),
565 VMSTATE_UINT8(pageh, I8257Regs),
566 VMSTATE_UINT8(dack, I8257Regs),
567 VMSTATE_UINT8(eop, I8257Regs),
7b5045c5 568 VMSTATE_END_OF_LIST()
85571bc7 569 }
7b5045c5 570};
85571bc7 571
e59fb374 572static int dma_post_load(void *opaque, int version_id)
85571bc7 573{
492c30af
AL
574 DMA_run();
575
85571bc7
FB
576 return 0;
577}
578
7b5045c5
JQ
579static const VMStateDescription vmstate_dma = {
580 .name = "dma",
581 .version_id = 1,
582 .minimum_version_id = 1,
7b5045c5 583 .post_load = dma_post_load,
d49805ae 584 .fields = (VMStateField[]) {
6a128b13
HP
585 VMSTATE_UINT8(command, I8257State),
586 VMSTATE_UINT8(mask, I8257State),
587 VMSTATE_UINT8(flip_flop, I8257State),
588 VMSTATE_INT32(dshift, I8257State),
0eee6d62
HP
589 VMSTATE_STRUCT_ARRAY(regs, I8257State, 4, 1, vmstate_i8257_regs,
590 I8257Regs),
7b5045c5
JQ
591 VMSTATE_END_OF_LIST()
592 }
593};
594
57146941 595void DMA_init(ISABus *bus, int high_page_enable)
9eb153f1 596{
5039d6e2
PB
597 dma_init2(&dma_controllers[0], 0x00, 0, 0x80, high_page_enable ? 0x480 : -1);
598 dma_init2(&dma_controllers[1], 0xc0, 1, 0x88, high_page_enable ? 0x488 : -1);
0be71e32
AW
599 vmstate_register (NULL, 0, &vmstate_dma, &dma_controllers[0]);
600 vmstate_register (NULL, 1, &vmstate_dma, &dma_controllers[1]);
492c30af
AL
601
602 dma_bh = qemu_bh_new(DMA_run_bh, NULL);
27503323 603}