]> git.proxmox.com Git - qemu.git/blame - hw/dma.c
Break up vl.h.
[qemu.git] / hw / dma.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 */
87ecb68b
PB
24#include "hw.h"
25#include "isa.h"
27503323 26
85571bc7 27/* #define DEBUG_DMA */
7ebb5e41 28
85571bc7 29#define dolog(...) fprintf (stderr, "dma: " __VA_ARGS__)
27503323
FB
30#ifdef DEBUG_DMA
31#define lwarn(...) fprintf (stderr, "dma: " __VA_ARGS__)
32#define linfo(...) fprintf (stderr, "dma: " __VA_ARGS__)
33#define ldebug(...) fprintf (stderr, "dma: " __VA_ARGS__)
34#else
35#define lwarn(...)
36#define linfo(...)
37#define ldebug(...)
38#endif
39
27503323
FB
40#define LENOFA(a) ((int) (sizeof(a)/sizeof(a[0])))
41
42struct dma_regs {
43 int now[2];
44 uint16_t base[2];
45 uint8_t mode;
46 uint8_t page;
b0bda528 47 uint8_t pageh;
27503323
FB
48 uint8_t dack;
49 uint8_t eop;
16f62432
FB
50 DMA_transfer_handler transfer_handler;
51 void *opaque;
27503323
FB
52};
53
54#define ADDR 0
55#define COUNT 1
56
57static struct dma_cont {
58 uint8_t status;
59 uint8_t command;
60 uint8_t mask;
61 uint8_t flip_flop;
9eb153f1 62 int dshift;
27503323
FB
63 struct dma_regs regs[4];
64} dma_controllers[2];
65
66enum {
e875c40a
FB
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,
73 CMD_LOW_DREQ = 0x40,
74 CMD_LOW_DACK = 0x80,
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
27503323
FB
78
79};
80
9eb153f1
FB
81static int channels[8] = {-1, 2, 3, 1, -1, -1, -1, 0};
82
7d977de7 83static void write_page (void *opaque, uint32_t nport, uint32_t data)
27503323 84{
9eb153f1 85 struct dma_cont *d = opaque;
27503323 86 int ichan;
27503323 87
9eb153f1 88 ichan = channels[nport & 7];
27503323 89 if (-1 == ichan) {
85571bc7 90 dolog ("invalid channel %#x %#x\n", nport, data);
27503323
FB
91 return;
92 }
9eb153f1
FB
93 d->regs[ichan].page = data;
94}
95
b0bda528 96static void write_pageh (void *opaque, uint32_t nport, uint32_t data)
9eb153f1
FB
97{
98 struct dma_cont *d = opaque;
99 int ichan;
27503323 100
9eb153f1 101 ichan = channels[nport & 7];
b0bda528 102 if (-1 == ichan) {
85571bc7 103 dolog ("invalid channel %#x %#x\n", nport, data);
b0bda528
FB
104 return;
105 }
106 d->regs[ichan].pageh = data;
107}
9eb153f1 108
b0bda528
FB
109static uint32_t read_page (void *opaque, uint32_t nport)
110{
111 struct dma_cont *d = opaque;
112 int ichan;
113
114 ichan = channels[nport & 7];
9eb153f1 115 if (-1 == ichan) {
85571bc7 116 dolog ("invalid channel read %#x\n", nport);
9eb153f1
FB
117 return 0;
118 }
119 return d->regs[ichan].page;
27503323
FB
120}
121
b0bda528
FB
122static uint32_t read_pageh (void *opaque, uint32_t nport)
123{
124 struct dma_cont *d = opaque;
125 int ichan;
126
127 ichan = channels[nport & 7];
128 if (-1 == ichan) {
85571bc7 129 dolog ("invalid channel read %#x\n", nport);
b0bda528
FB
130 return 0;
131 }
132 return d->regs[ichan].pageh;
133}
134
9eb153f1 135static inline void init_chan (struct dma_cont *d, int ichan)
27503323
FB
136{
137 struct dma_regs *r;
138
9eb153f1 139 r = d->regs + ichan;
85571bc7 140 r->now[ADDR] = r->base[ADDR] << d->dshift;
27503323
FB
141 r->now[COUNT] = 0;
142}
143
9eb153f1 144static inline int getff (struct dma_cont *d)
27503323
FB
145{
146 int ff;
147
9eb153f1
FB
148 ff = d->flip_flop;
149 d->flip_flop = !ff;
27503323
FB
150 return ff;
151}
152
7d977de7 153static uint32_t read_chan (void *opaque, uint32_t nport)
27503323 154{
9eb153f1 155 struct dma_cont *d = opaque;
85571bc7 156 int ichan, nreg, iport, ff, val, dir;
27503323 157 struct dma_regs *r;
27503323 158
9eb153f1
FB
159 iport = (nport >> d->dshift) & 0x0f;
160 ichan = iport >> 1;
161 nreg = iport & 1;
162 r = d->regs + ichan;
27503323 163
85571bc7 164 dir = ((r->mode >> 5) & 1) ? -1 : 1;
9eb153f1 165 ff = getff (d);
27503323 166 if (nreg)
9eb153f1 167 val = (r->base[COUNT] << d->dshift) - r->now[COUNT];
27503323 168 else
85571bc7 169 val = r->now[ADDR] + r->now[COUNT] * dir;
27503323 170
85571bc7 171 ldebug ("read_chan %#x -> %d\n", iport, val);
9eb153f1 172 return (val >> (d->dshift + (ff << 3))) & 0xff;
27503323
FB
173}
174
7d977de7 175static void write_chan (void *opaque, uint32_t nport, uint32_t data)
27503323 176{
9eb153f1
FB
177 struct dma_cont *d = opaque;
178 int iport, ichan, nreg;
27503323
FB
179 struct dma_regs *r;
180
9eb153f1
FB
181 iport = (nport >> d->dshift) & 0x0f;
182 ichan = iport >> 1;
183 nreg = iport & 1;
184 r = d->regs + ichan;
185 if (getff (d)) {
3504fe17 186 r->base[nreg] = (r->base[nreg] & 0xff) | ((data << 8) & 0xff00);
9eb153f1 187 init_chan (d, ichan);
3504fe17
FB
188 } else {
189 r->base[nreg] = (r->base[nreg] & 0xff00) | (data & 0xff);
27503323 190 }
27503323
FB
191}
192
7d977de7 193static void write_cont (void *opaque, uint32_t nport, uint32_t data)
27503323 194{
9eb153f1 195 struct dma_cont *d = opaque;
85571bc7 196 int iport, ichan = 0;
27503323 197
9eb153f1 198 iport = (nport >> d->dshift) & 0x0f;
27503323 199 switch (iport) {
85571bc7 200 case 0x08: /* command */
df475d18 201 if ((data != 0) && (data & CMD_NOT_SUPPORTED)) {
85571bc7 202 dolog ("command %#x not supported\n", data);
df475d18 203 return;
27503323
FB
204 }
205 d->command = data;
206 break;
207
85571bc7 208 case 0x09:
27503323
FB
209 ichan = data & 3;
210 if (data & 4) {
211 d->status |= 1 << (ichan + 4);
212 }
213 else {
214 d->status &= ~(1 << (ichan + 4));
215 }
216 d->status &= ~(1 << ichan);
217 break;
218
85571bc7 219 case 0x0a: /* single mask */
27503323
FB
220 if (data & 4)
221 d->mask |= 1 << (data & 3);
222 else
223 d->mask &= ~(1 << (data & 3));
224 break;
225
85571bc7 226 case 0x0b: /* mode */
27503323 227 {
16d17fdb
FB
228 ichan = data & 3;
229#ifdef DEBUG_DMA
85571bc7
FB
230 {
231 int op, ai, dir, opmode;
e875c40a
FB
232 op = (data >> 2) & 3;
233 ai = (data >> 4) & 1;
234 dir = (data >> 5) & 1;
235 opmode = (data >> 6) & 3;
27503323 236
e875c40a
FB
237 linfo ("ichan %d, op %d, ai %d, dir %d, opmode %d\n",
238 ichan, op, ai, dir, opmode);
85571bc7 239 }
27503323 240#endif
27503323
FB
241 d->regs[ichan].mode = data;
242 break;
243 }
244
85571bc7 245 case 0x0c: /* clear flip flop */
27503323
FB
246 d->flip_flop = 0;
247 break;
248
85571bc7 249 case 0x0d: /* reset */
27503323
FB
250 d->flip_flop = 0;
251 d->mask = ~0;
252 d->status = 0;
253 d->command = 0;
254 break;
255
85571bc7 256 case 0x0e: /* clear mask for all channels */
27503323
FB
257 d->mask = 0;
258 break;
259
85571bc7 260 case 0x0f: /* write mask for all channels */
27503323
FB
261 d->mask = data;
262 break;
263
264 default:
85571bc7 265 dolog ("unknown iport %#x\n", iport);
df475d18 266 break;
27503323
FB
267 }
268
16d17fdb 269#ifdef DEBUG_DMA
27503323 270 if (0xc != iport) {
85571bc7 271 linfo ("write_cont: nport %#06x, ichan % 2d, val %#06x\n",
9eb153f1 272 nport, ichan, data);
27503323
FB
273 }
274#endif
27503323
FB
275}
276
9eb153f1
FB
277static uint32_t read_cont (void *opaque, uint32_t nport)
278{
279 struct dma_cont *d = opaque;
280 int iport, val;
85571bc7 281
9eb153f1
FB
282 iport = (nport >> d->dshift) & 0x0f;
283 switch (iport) {
85571bc7 284 case 0x08: /* status */
9eb153f1
FB
285 val = d->status;
286 d->status &= 0xf0;
287 break;
85571bc7 288 case 0x0f: /* mask */
9eb153f1
FB
289 val = d->mask;
290 break;
291 default:
292 val = 0;
293 break;
294 }
85571bc7
FB
295
296 ldebug ("read_cont: nport %#06x, iport %#04x val %#x\n", nport, iport, val);
9eb153f1
FB
297 return val;
298}
299
27503323
FB
300int DMA_get_channel_mode (int nchan)
301{
302 return dma_controllers[nchan > 3].regs[nchan & 3].mode;
303}
304
305void DMA_hold_DREQ (int nchan)
306{
307 int ncont, ichan;
308
309 ncont = nchan > 3;
310 ichan = nchan & 3;
311 linfo ("held cont=%d chan=%d\n", ncont, ichan);
312 dma_controllers[ncont].status |= 1 << (ichan + 4);
313}
314
315void DMA_release_DREQ (int nchan)
316{
317 int ncont, ichan;
318
319 ncont = nchan > 3;
320 ichan = nchan & 3;
321 linfo ("released cont=%d chan=%d\n", ncont, ichan);
322 dma_controllers[ncont].status &= ~(1 << (ichan + 4));
323}
324
325static void channel_run (int ncont, int ichan)
326{
27503323 327 int n;
85571bc7
FB
328 struct dma_regs *r = &dma_controllers[ncont].regs[ichan];
329#ifdef DEBUG_DMA
330 int dir, opmode;
27503323 331
85571bc7
FB
332 dir = (r->mode >> 5) & 1;
333 opmode = (r->mode >> 6) & 3;
27503323 334
85571bc7
FB
335 if (dir) {
336 dolog ("DMA in address decrement mode\n");
337 }
338 if (opmode != 1) {
339 dolog ("DMA not in single mode select %#x\n", opmode);
340 }
341#endif
27503323 342
85571bc7
FB
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);
346 r->now[COUNT] = n;
347 ldebug ("dma_pos %d size %d\n", n, (r->base[COUNT] + 1) << ncont);
27503323
FB
348}
349
350void DMA_run (void)
351{
27503323
FB
352 struct dma_cont *d;
353 int icont, ichan;
354
27503323
FB
355 d = dma_controllers;
356
357 for (icont = 0; icont < 2; icont++, d++) {
358 for (ichan = 0; ichan < 4; ichan++) {
359 int mask;
360
361 mask = 1 << ichan;
362
363 if ((0 == (d->mask & mask)) && (0 != (d->status & (mask << 4))))
364 channel_run (icont, ichan);
365 }
366 }
27503323
FB
367}
368
369void DMA_register_channel (int nchan,
85571bc7 370 DMA_transfer_handler transfer_handler,
16f62432 371 void *opaque)
27503323
FB
372{
373 struct dma_regs *r;
374 int ichan, ncont;
375
376 ncont = nchan > 3;
377 ichan = nchan & 3;
378
379 r = dma_controllers[ncont].regs + ichan;
16f62432
FB
380 r->transfer_handler = transfer_handler;
381 r->opaque = opaque;
382}
383
85571bc7
FB
384int DMA_read_memory (int nchan, void *buf, int pos, int len)
385{
386 struct dma_regs *r = &dma_controllers[nchan > 3].regs[nchan & 3];
71db710f 387 target_phys_addr_t addr = ((r->pageh & 0x7f) << 24) | (r->page << 16) | r->now[ADDR];
85571bc7
FB
388
389 if (r->mode & 0x20) {
390 int i;
391 uint8_t *p = buf;
392
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];
397 p[i] = b;
398 }
399 }
400 else
401 cpu_physical_memory_read (addr + pos, buf, len);
402
403 return len;
404}
405
406int DMA_write_memory (int nchan, void *buf, int pos, int len)
407{
408 struct dma_regs *r = &dma_controllers[nchan > 3].regs[nchan & 3];
71db710f 409 target_phys_addr_t addr = ((r->pageh & 0x7f) << 24) | (r->page << 16) | r->now[ADDR];
85571bc7
FB
410
411 if (r->mode & 0x20) {
412 int i;
413 uint8_t *p = buf;
414
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];
419 p[i] = b;
420 }
421 }
422 else
423 cpu_physical_memory_write (addr + pos, buf, len);
424
425 return len;
426}
427
16f62432
FB
428/* request the emulator to transfer a new DMA memory block ASAP */
429void DMA_schedule(int nchan)
430{
c68ea704
FB
431 CPUState *env = cpu_single_env;
432 if (env)
433 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
27503323
FB
434}
435
d7d02e3c
FB
436static void dma_reset(void *opaque)
437{
438 struct dma_cont *d = opaque;
439 write_cont (d, (0x0d << d->dshift), 0);
440}
441
9eb153f1 442/* dshift = 0: 8 bit DMA, 1 = 16 bit DMA */
85571bc7 443static void dma_init2(struct dma_cont *d, int base, int dshift,
b0bda528 444 int page_base, int pageh_base)
27503323 445{
9eb153f1 446 const static int page_port_list[] = { 0x1, 0x2, 0x3, 0x7 };
27503323 447 int i;
27503323 448
9eb153f1 449 d->dshift = dshift;
27503323 450 for (i = 0; i < 8; i++) {
9eb153f1
FB
451 register_ioport_write (base + (i << dshift), 1, 1, write_chan, d);
452 register_ioport_read (base + (i << dshift), 1, 1, read_chan, d);
27503323 453 }
27503323 454 for (i = 0; i < LENOFA (page_port_list); i++) {
85571bc7 455 register_ioport_write (page_base + page_port_list[i], 1, 1,
9eb153f1 456 write_page, d);
85571bc7 457 register_ioport_read (page_base + page_port_list[i], 1, 1,
9eb153f1 458 read_page, d);
b0bda528 459 if (pageh_base >= 0) {
85571bc7 460 register_ioport_write (pageh_base + page_port_list[i], 1, 1,
b0bda528 461 write_pageh, d);
85571bc7 462 register_ioport_read (pageh_base + page_port_list[i], 1, 1,
b0bda528
FB
463 read_pageh, d);
464 }
27503323 465 }
27503323 466 for (i = 0; i < 8; i++) {
85571bc7 467 register_ioport_write (base + ((i + 8) << dshift), 1, 1,
9eb153f1 468 write_cont, d);
85571bc7 469 register_ioport_read (base + ((i + 8) << dshift), 1, 1,
9eb153f1 470 read_cont, d);
27503323 471 }
d7d02e3c
FB
472 qemu_register_reset(dma_reset, d);
473 dma_reset(d);
9eb153f1 474}
27503323 475
85571bc7
FB
476static void dma_save (QEMUFile *f, void *opaque)
477{
478 struct dma_cont *d = opaque;
479 int i;
480
481 /* qemu_put_8s (f, &d->status); */
482 qemu_put_8s (f, &d->command);
483 qemu_put_8s (f, &d->mask);
484 qemu_put_8s (f, &d->flip_flop);
485 qemu_put_be32s (f, &d->dshift);
486
487 for (i = 0; i < 4; ++i) {
488 struct dma_regs *r = &d->regs[i];
489 qemu_put_be32s (f, &r->now[0]);
490 qemu_put_be32s (f, &r->now[1]);
491 qemu_put_be16s (f, &r->base[0]);
492 qemu_put_be16s (f, &r->base[1]);
493 qemu_put_8s (f, &r->mode);
494 qemu_put_8s (f, &r->page);
495 qemu_put_8s (f, &r->pageh);
496 qemu_put_8s (f, &r->dack);
497 qemu_put_8s (f, &r->eop);
498 }
499}
500
501static int dma_load (QEMUFile *f, void *opaque, int version_id)
502{
503 struct dma_cont *d = opaque;
504 int i;
505
506 if (version_id != 1)
507 return -EINVAL;
508
509 /* qemu_get_8s (f, &d->status); */
510 qemu_get_8s (f, &d->command);
511 qemu_get_8s (f, &d->mask);
512 qemu_get_8s (f, &d->flip_flop);
513 qemu_get_be32s (f, &d->dshift);
514
515 for (i = 0; i < 4; ++i) {
516 struct dma_regs *r = &d->regs[i];
517 qemu_get_be32s (f, &r->now[0]);
518 qemu_get_be32s (f, &r->now[1]);
519 qemu_get_be16s (f, &r->base[0]);
520 qemu_get_be16s (f, &r->base[1]);
521 qemu_get_8s (f, &r->mode);
522 qemu_get_8s (f, &r->page);
523 qemu_get_8s (f, &r->pageh);
524 qemu_get_8s (f, &r->dack);
525 qemu_get_8s (f, &r->eop);
526 }
527 return 0;
528}
529
b0bda528 530void DMA_init (int high_page_enable)
9eb153f1 531{
85571bc7 532 dma_init2(&dma_controllers[0], 0x00, 0, 0x80,
b0bda528
FB
533 high_page_enable ? 0x480 : -1);
534 dma_init2(&dma_controllers[1], 0xc0, 1, 0x88,
535 high_page_enable ? 0x488 : -1);
85571bc7
FB
536 register_savevm ("dma", 0, 1, dma_save, dma_load, &dma_controllers[0]);
537 register_savevm ("dma", 1, 1, dma_save, dma_load, &dma_controllers[1]);
27503323 538}