2
0

dma.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. * QEMU DMA emulation
  3. *
  4. * Copyright (c) 2003-2004 Vassili Karpov (malc)
  5. *
  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. */
  24. #include "hw.h"
  25. #include "isa.h"
  26. #include "qemu/main-loop.h"
  27. /* #define DEBUG_DMA */
  28. #define dolog(...) fprintf (stderr, "dma: " __VA_ARGS__)
  29. #ifdef DEBUG_DMA
  30. #define linfo(...) fprintf (stderr, "dma: " __VA_ARGS__)
  31. #define ldebug(...) fprintf (stderr, "dma: " __VA_ARGS__)
  32. #else
  33. #define linfo(...)
  34. #define ldebug(...)
  35. #endif
  36. struct dma_regs {
  37. int now[2];
  38. uint16_t base[2];
  39. uint8_t mode;
  40. uint8_t page;
  41. uint8_t pageh;
  42. uint8_t dack;
  43. uint8_t eop;
  44. DMA_transfer_handler transfer_handler;
  45. void *opaque;
  46. };
  47. #define ADDR 0
  48. #define COUNT 1
  49. static struct dma_cont {
  50. uint8_t status;
  51. uint8_t command;
  52. uint8_t mask;
  53. uint8_t flip_flop;
  54. int dshift;
  55. struct dma_regs regs[4];
  56. qemu_irq *cpu_request_exit;
  57. MemoryRegion channel_io;
  58. MemoryRegion cont_io;
  59. } dma_controllers[2];
  60. enum {
  61. CMD_MEMORY_TO_MEMORY = 0x01,
  62. CMD_FIXED_ADDRESS = 0x02,
  63. CMD_BLOCK_CONTROLLER = 0x04,
  64. CMD_COMPRESSED_TIME = 0x08,
  65. CMD_CYCLIC_PRIORITY = 0x10,
  66. CMD_EXTENDED_WRITE = 0x20,
  67. CMD_LOW_DREQ = 0x40,
  68. CMD_LOW_DACK = 0x80,
  69. CMD_NOT_SUPPORTED = CMD_MEMORY_TO_MEMORY | CMD_FIXED_ADDRESS
  70. | CMD_COMPRESSED_TIME | CMD_CYCLIC_PRIORITY | CMD_EXTENDED_WRITE
  71. | CMD_LOW_DREQ | CMD_LOW_DACK
  72. };
  73. static void DMA_run (void);
  74. static int channels[8] = {-1, 2, 3, 1, -1, -1, -1, 0};
  75. static void write_page (void *opaque, uint32_t nport, uint32_t data)
  76. {
  77. struct dma_cont *d = opaque;
  78. int ichan;
  79. ichan = channels[nport & 7];
  80. if (-1 == ichan) {
  81. dolog ("invalid channel %#x %#x\n", nport, data);
  82. return;
  83. }
  84. d->regs[ichan].page = data;
  85. }
  86. static void write_pageh (void *opaque, uint32_t nport, uint32_t data)
  87. {
  88. struct dma_cont *d = opaque;
  89. int ichan;
  90. ichan = channels[nport & 7];
  91. if (-1 == ichan) {
  92. dolog ("invalid channel %#x %#x\n", nport, data);
  93. return;
  94. }
  95. d->regs[ichan].pageh = data;
  96. }
  97. static uint32_t read_page (void *opaque, uint32_t nport)
  98. {
  99. struct dma_cont *d = opaque;
  100. int ichan;
  101. ichan = channels[nport & 7];
  102. if (-1 == ichan) {
  103. dolog ("invalid channel read %#x\n", nport);
  104. return 0;
  105. }
  106. return d->regs[ichan].page;
  107. }
  108. static uint32_t read_pageh (void *opaque, uint32_t nport)
  109. {
  110. struct dma_cont *d = opaque;
  111. int ichan;
  112. ichan = channels[nport & 7];
  113. if (-1 == ichan) {
  114. dolog ("invalid channel read %#x\n", nport);
  115. return 0;
  116. }
  117. return d->regs[ichan].pageh;
  118. }
  119. static inline void init_chan (struct dma_cont *d, int ichan)
  120. {
  121. struct dma_regs *r;
  122. r = d->regs + ichan;
  123. r->now[ADDR] = r->base[ADDR] << d->dshift;
  124. r->now[COUNT] = 0;
  125. }
  126. static inline int getff (struct dma_cont *d)
  127. {
  128. int ff;
  129. ff = d->flip_flop;
  130. d->flip_flop = !ff;
  131. return ff;
  132. }
  133. static uint64_t read_chan(void *opaque, hwaddr nport, unsigned size)
  134. {
  135. struct dma_cont *d = opaque;
  136. int ichan, nreg, iport, ff, val, dir;
  137. struct dma_regs *r;
  138. iport = (nport >> d->dshift) & 0x0f;
  139. ichan = iport >> 1;
  140. nreg = iport & 1;
  141. r = d->regs + ichan;
  142. dir = ((r->mode >> 5) & 1) ? -1 : 1;
  143. ff = getff (d);
  144. if (nreg)
  145. val = (r->base[COUNT] << d->dshift) - r->now[COUNT];
  146. else
  147. val = r->now[ADDR] + r->now[COUNT] * dir;
  148. ldebug ("read_chan %#x -> %d\n", iport, val);
  149. return (val >> (d->dshift + (ff << 3))) & 0xff;
  150. }
  151. static void write_chan(void *opaque, hwaddr nport, uint64_t data,
  152. unsigned size)
  153. {
  154. struct dma_cont *d = opaque;
  155. int iport, ichan, nreg;
  156. struct dma_regs *r;
  157. iport = (nport >> d->dshift) & 0x0f;
  158. ichan = iport >> 1;
  159. nreg = iport & 1;
  160. r = d->regs + ichan;
  161. if (getff (d)) {
  162. r->base[nreg] = (r->base[nreg] & 0xff) | ((data << 8) & 0xff00);
  163. init_chan (d, ichan);
  164. } else {
  165. r->base[nreg] = (r->base[nreg] & 0xff00) | (data & 0xff);
  166. }
  167. }
  168. static void write_cont(void *opaque, hwaddr nport, uint64_t data,
  169. unsigned size)
  170. {
  171. struct dma_cont *d = opaque;
  172. int iport, ichan = 0;
  173. iport = (nport >> d->dshift) & 0x0f;
  174. switch (iport) {
  175. case 0x00: /* command */
  176. if ((data != 0) && (data & CMD_NOT_SUPPORTED)) {
  177. dolog("command %"PRIx64" not supported\n", data);
  178. return;
  179. }
  180. d->command = data;
  181. break;
  182. case 0x01:
  183. ichan = data & 3;
  184. if (data & 4) {
  185. d->status |= 1 << (ichan + 4);
  186. }
  187. else {
  188. d->status &= ~(1 << (ichan + 4));
  189. }
  190. d->status &= ~(1 << ichan);
  191. DMA_run();
  192. break;
  193. case 0x02: /* single mask */
  194. if (data & 4)
  195. d->mask |= 1 << (data & 3);
  196. else
  197. d->mask &= ~(1 << (data & 3));
  198. DMA_run();
  199. break;
  200. case 0x03: /* mode */
  201. {
  202. ichan = data & 3;
  203. #ifdef DEBUG_DMA
  204. {
  205. int op, ai, dir, opmode;
  206. op = (data >> 2) & 3;
  207. ai = (data >> 4) & 1;
  208. dir = (data >> 5) & 1;
  209. opmode = (data >> 6) & 3;
  210. linfo ("ichan %d, op %d, ai %d, dir %d, opmode %d\n",
  211. ichan, op, ai, dir, opmode);
  212. }
  213. #endif
  214. d->regs[ichan].mode = data;
  215. break;
  216. }
  217. case 0x04: /* clear flip flop */
  218. d->flip_flop = 0;
  219. break;
  220. case 0x05: /* reset */
  221. d->flip_flop = 0;
  222. d->mask = ~0;
  223. d->status = 0;
  224. d->command = 0;
  225. break;
  226. case 0x06: /* clear mask for all channels */
  227. d->mask = 0;
  228. DMA_run();
  229. break;
  230. case 0x07: /* write mask for all channels */
  231. d->mask = data;
  232. DMA_run();
  233. break;
  234. default:
  235. dolog ("unknown iport %#x\n", iport);
  236. break;
  237. }
  238. #ifdef DEBUG_DMA
  239. if (0xc != iport) {
  240. linfo ("write_cont: nport %#06x, ichan % 2d, val %#06x\n",
  241. nport, ichan, data);
  242. }
  243. #endif
  244. }
  245. static uint64_t read_cont(void *opaque, hwaddr nport, unsigned size)
  246. {
  247. struct dma_cont *d = opaque;
  248. int iport, val;
  249. iport = (nport >> d->dshift) & 0x0f;
  250. switch (iport) {
  251. case 0x00: /* status */
  252. val = d->status;
  253. d->status &= 0xf0;
  254. break;
  255. case 0x01: /* mask */
  256. val = d->mask;
  257. break;
  258. default:
  259. val = 0;
  260. break;
  261. }
  262. ldebug ("read_cont: nport %#06x, iport %#04x val %#x\n", nport, iport, val);
  263. return val;
  264. }
  265. int DMA_get_channel_mode (int nchan)
  266. {
  267. return dma_controllers[nchan > 3].regs[nchan & 3].mode;
  268. }
  269. void DMA_hold_DREQ (int nchan)
  270. {
  271. int ncont, ichan;
  272. ncont = nchan > 3;
  273. ichan = nchan & 3;
  274. linfo ("held cont=%d chan=%d\n", ncont, ichan);
  275. dma_controllers[ncont].status |= 1 << (ichan + 4);
  276. DMA_run();
  277. }
  278. void DMA_release_DREQ (int nchan)
  279. {
  280. int ncont, ichan;
  281. ncont = nchan > 3;
  282. ichan = nchan & 3;
  283. linfo ("released cont=%d chan=%d\n", ncont, ichan);
  284. dma_controllers[ncont].status &= ~(1 << (ichan + 4));
  285. DMA_run();
  286. }
  287. static void channel_run (int ncont, int ichan)
  288. {
  289. int n;
  290. struct dma_regs *r = &dma_controllers[ncont].regs[ichan];
  291. #ifdef DEBUG_DMA
  292. int dir, opmode;
  293. dir = (r->mode >> 5) & 1;
  294. opmode = (r->mode >> 6) & 3;
  295. if (dir) {
  296. dolog ("DMA in address decrement mode\n");
  297. }
  298. if (opmode != 1) {
  299. dolog ("DMA not in single mode select %#x\n", opmode);
  300. }
  301. #endif
  302. n = r->transfer_handler (r->opaque, ichan + (ncont << 2),
  303. r->now[COUNT], (r->base[COUNT] + 1) << ncont);
  304. r->now[COUNT] = n;
  305. ldebug ("dma_pos %d size %d\n", n, (r->base[COUNT] + 1) << ncont);
  306. }
  307. static QEMUBH *dma_bh;
  308. static void DMA_run (void)
  309. {
  310. struct dma_cont *d;
  311. int icont, ichan;
  312. int rearm = 0;
  313. static int running = 0;
  314. if (running) {
  315. rearm = 1;
  316. goto out;
  317. } else {
  318. running = 1;
  319. }
  320. d = dma_controllers;
  321. for (icont = 0; icont < 2; icont++, d++) {
  322. for (ichan = 0; ichan < 4; ichan++) {
  323. int mask;
  324. mask = 1 << ichan;
  325. if ((0 == (d->mask & mask)) && (0 != (d->status & (mask << 4)))) {
  326. channel_run (icont, ichan);
  327. rearm = 1;
  328. }
  329. }
  330. }
  331. running = 0;
  332. out:
  333. if (rearm)
  334. qemu_bh_schedule_idle(dma_bh);
  335. }
  336. static void DMA_run_bh(void *unused)
  337. {
  338. DMA_run();
  339. }
  340. void DMA_register_channel (int nchan,
  341. DMA_transfer_handler transfer_handler,
  342. void *opaque)
  343. {
  344. struct dma_regs *r;
  345. int ichan, ncont;
  346. ncont = nchan > 3;
  347. ichan = nchan & 3;
  348. r = dma_controllers[ncont].regs + ichan;
  349. r->transfer_handler = transfer_handler;
  350. r->opaque = opaque;
  351. }
  352. int DMA_read_memory (int nchan, void *buf, int pos, int len)
  353. {
  354. struct dma_regs *r = &dma_controllers[nchan > 3].regs[nchan & 3];
  355. hwaddr addr = ((r->pageh & 0x7f) << 24) | (r->page << 16) | r->now[ADDR];
  356. if (r->mode & 0x20) {
  357. int i;
  358. uint8_t *p = buf;
  359. cpu_physical_memory_read (addr - pos - len, buf, len);
  360. /* What about 16bit transfers? */
  361. for (i = 0; i < len >> 1; i++) {
  362. uint8_t b = p[len - i - 1];
  363. p[i] = b;
  364. }
  365. }
  366. else
  367. cpu_physical_memory_read (addr + pos, buf, len);
  368. return len;
  369. }
  370. int DMA_write_memory (int nchan, void *buf, int pos, int len)
  371. {
  372. struct dma_regs *r = &dma_controllers[nchan > 3].regs[nchan & 3];
  373. hwaddr addr = ((r->pageh & 0x7f) << 24) | (r->page << 16) | r->now[ADDR];
  374. if (r->mode & 0x20) {
  375. int i;
  376. uint8_t *p = buf;
  377. cpu_physical_memory_write (addr - pos - len, buf, len);
  378. /* What about 16bit transfers? */
  379. for (i = 0; i < len; i++) {
  380. uint8_t b = p[len - i - 1];
  381. p[i] = b;
  382. }
  383. }
  384. else
  385. cpu_physical_memory_write (addr + pos, buf, len);
  386. return len;
  387. }
  388. /* request the emulator to transfer a new DMA memory block ASAP */
  389. void DMA_schedule(int nchan)
  390. {
  391. struct dma_cont *d = &dma_controllers[nchan > 3];
  392. qemu_irq_pulse(*d->cpu_request_exit);
  393. }
  394. static void dma_reset(void *opaque)
  395. {
  396. struct dma_cont *d = opaque;
  397. write_cont(d, (0x05 << d->dshift), 0, 1);
  398. }
  399. static int dma_phony_handler (void *opaque, int nchan, int dma_pos, int dma_len)
  400. {
  401. dolog ("unregistered DMA channel used nchan=%d dma_pos=%d dma_len=%d\n",
  402. nchan, dma_pos, dma_len);
  403. return dma_pos;
  404. }
  405. static const MemoryRegionOps channel_io_ops = {
  406. .read = read_chan,
  407. .write = write_chan,
  408. .endianness = DEVICE_NATIVE_ENDIAN,
  409. .impl = {
  410. .min_access_size = 1,
  411. .max_access_size = 1,
  412. },
  413. };
  414. /* IOport from page_base */
  415. static const MemoryRegionPortio page_portio_list[] = {
  416. { 0x01, 3, 1, .write = write_page, .read = read_page, },
  417. { 0x07, 1, 1, .write = write_page, .read = read_page, },
  418. PORTIO_END_OF_LIST(),
  419. };
  420. /* IOport from pageh_base */
  421. static const MemoryRegionPortio pageh_portio_list[] = {
  422. { 0x01, 3, 1, .write = write_pageh, .read = read_pageh, },
  423. { 0x07, 3, 1, .write = write_pageh, .read = read_pageh, },
  424. PORTIO_END_OF_LIST(),
  425. };
  426. static const MemoryRegionOps cont_io_ops = {
  427. .read = read_cont,
  428. .write = write_cont,
  429. .endianness = DEVICE_NATIVE_ENDIAN,
  430. .impl = {
  431. .min_access_size = 1,
  432. .max_access_size = 1,
  433. },
  434. };
  435. /* dshift = 0: 8 bit DMA, 1 = 16 bit DMA */
  436. static void dma_init2(struct dma_cont *d, int base, int dshift,
  437. int page_base, int pageh_base,
  438. qemu_irq *cpu_request_exit)
  439. {
  440. int i;
  441. d->dshift = dshift;
  442. d->cpu_request_exit = cpu_request_exit;
  443. memory_region_init_io(&d->channel_io, &channel_io_ops, d,
  444. "dma-chan", 8 << d->dshift);
  445. memory_region_add_subregion(isa_address_space_io(NULL),
  446. base, &d->channel_io);
  447. isa_register_portio_list(NULL, page_base, page_portio_list, d,
  448. "dma-page");
  449. if (pageh_base >= 0) {
  450. isa_register_portio_list(NULL, pageh_base, pageh_portio_list, d,
  451. "dma-pageh");
  452. }
  453. memory_region_init_io(&d->cont_io, &cont_io_ops, d, "dma-cont",
  454. 8 << d->dshift);
  455. memory_region_add_subregion(isa_address_space_io(NULL),
  456. base + (8 << d->dshift), &d->cont_io);
  457. qemu_register_reset(dma_reset, d);
  458. dma_reset(d);
  459. for (i = 0; i < ARRAY_SIZE (d->regs); ++i) {
  460. d->regs[i].transfer_handler = dma_phony_handler;
  461. }
  462. }
  463. static const VMStateDescription vmstate_dma_regs = {
  464. .name = "dma_regs",
  465. .version_id = 1,
  466. .minimum_version_id = 1,
  467. .minimum_version_id_old = 1,
  468. .fields = (VMStateField []) {
  469. VMSTATE_INT32_ARRAY(now, struct dma_regs, 2),
  470. VMSTATE_UINT16_ARRAY(base, struct dma_regs, 2),
  471. VMSTATE_UINT8(mode, struct dma_regs),
  472. VMSTATE_UINT8(page, struct dma_regs),
  473. VMSTATE_UINT8(pageh, struct dma_regs),
  474. VMSTATE_UINT8(dack, struct dma_regs),
  475. VMSTATE_UINT8(eop, struct dma_regs),
  476. VMSTATE_END_OF_LIST()
  477. }
  478. };
  479. static int dma_post_load(void *opaque, int version_id)
  480. {
  481. DMA_run();
  482. return 0;
  483. }
  484. static const VMStateDescription vmstate_dma = {
  485. .name = "dma",
  486. .version_id = 1,
  487. .minimum_version_id = 1,
  488. .minimum_version_id_old = 1,
  489. .post_load = dma_post_load,
  490. .fields = (VMStateField []) {
  491. VMSTATE_UINT8(command, struct dma_cont),
  492. VMSTATE_UINT8(mask, struct dma_cont),
  493. VMSTATE_UINT8(flip_flop, struct dma_cont),
  494. VMSTATE_INT32(dshift, struct dma_cont),
  495. VMSTATE_STRUCT_ARRAY(regs, struct dma_cont, 4, 1, vmstate_dma_regs, struct dma_regs),
  496. VMSTATE_END_OF_LIST()
  497. }
  498. };
  499. void DMA_init(int high_page_enable, qemu_irq *cpu_request_exit)
  500. {
  501. dma_init2(&dma_controllers[0], 0x00, 0, 0x80,
  502. high_page_enable ? 0x480 : -1, cpu_request_exit);
  503. dma_init2(&dma_controllers[1], 0xc0, 1, 0x88,
  504. high_page_enable ? 0x488 : -1, cpu_request_exit);
  505. vmstate_register (NULL, 0, &vmstate_dma, &dma_controllers[0]);
  506. vmstate_register (NULL, 1, &vmstate_dma, &dma_controllers[1]);
  507. dma_bh = qemu_bh_new(DMA_run_bh, NULL);
  508. }