mac_dbdma.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. /*
  2. * PowerMac descriptor-based DMA emulation
  3. *
  4. * Copyright (c) 2005-2007 Fabrice Bellard
  5. * Copyright (c) 2007 Jocelyn Mayer
  6. * Copyright (c) 2009 Laurent Vivier
  7. *
  8. * some parts from linux-2.6.28, arch/powerpc/include/asm/dbdma.h
  9. *
  10. * Definitions for using the Apple Descriptor-Based DMA controller
  11. * in Power Macintosh computers.
  12. *
  13. * Copyright (C) 1996 Paul Mackerras.
  14. *
  15. * some parts from mol 0.9.71
  16. *
  17. * Descriptor based DMA emulation
  18. *
  19. * Copyright (C) 1998-2004 Samuel Rydh (samuel@ibrium.se)
  20. *
  21. * Permission is hereby granted, free of charge, to any person obtaining a copy
  22. * of this software and associated documentation files (the "Software"), to deal
  23. * in the Software without restriction, including without limitation the rights
  24. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  25. * copies of the Software, and to permit persons to whom the Software is
  26. * furnished to do so, subject to the following conditions:
  27. *
  28. * The above copyright notice and this permission notice shall be included in
  29. * all copies or substantial portions of the Software.
  30. *
  31. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  32. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  33. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  34. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  35. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  36. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  37. * THE SOFTWARE.
  38. */
  39. #include "qemu/osdep.h"
  40. #include "hw/irq.h"
  41. #include "hw/ppc/mac_dbdma.h"
  42. #include "migration/vmstate.h"
  43. #include "qemu/main-loop.h"
  44. #include "qemu/module.h"
  45. #include "qemu/log.h"
  46. #include "system/dma.h"
  47. /* debug DBDMA */
  48. #define DEBUG_DBDMA 0
  49. #define DEBUG_DBDMA_CHANMASK ((1ull << DBDMA_CHANNELS) - 1)
  50. #define DBDMA_DPRINTF(fmt, ...) do { \
  51. if (DEBUG_DBDMA) { \
  52. printf("DBDMA: " fmt , ## __VA_ARGS__); \
  53. } \
  54. } while (0)
  55. #define DBDMA_DPRINTFCH(ch, fmt, ...) do { \
  56. if (DEBUG_DBDMA) { \
  57. if ((1ul << (ch)->channel) & DEBUG_DBDMA_CHANMASK) { \
  58. printf("DBDMA[%02x]: " fmt , (ch)->channel, ## __VA_ARGS__); \
  59. } \
  60. } \
  61. } while (0)
  62. /*
  63. */
  64. static DBDMAState *dbdma_from_ch(DBDMA_channel *ch)
  65. {
  66. return container_of(ch, DBDMAState, channels[ch->channel]);
  67. }
  68. #if DEBUG_DBDMA
  69. static void dump_dbdma_cmd(DBDMA_channel *ch, dbdma_cmd *cmd)
  70. {
  71. DBDMA_DPRINTFCH(ch, "dbdma_cmd %p\n", cmd);
  72. DBDMA_DPRINTFCH(ch, " req_count 0x%04x\n", le16_to_cpu(cmd->req_count));
  73. DBDMA_DPRINTFCH(ch, " command 0x%04x\n", le16_to_cpu(cmd->command));
  74. DBDMA_DPRINTFCH(ch, " phy_addr 0x%08x\n", le32_to_cpu(cmd->phy_addr));
  75. DBDMA_DPRINTFCH(ch, " cmd_dep 0x%08x\n", le32_to_cpu(cmd->cmd_dep));
  76. DBDMA_DPRINTFCH(ch, " res_count 0x%04x\n", le16_to_cpu(cmd->res_count));
  77. DBDMA_DPRINTFCH(ch, " xfer_status 0x%04x\n",
  78. le16_to_cpu(cmd->xfer_status));
  79. }
  80. #else
  81. static void dump_dbdma_cmd(DBDMA_channel *ch, dbdma_cmd *cmd)
  82. {
  83. }
  84. #endif
  85. static void dbdma_cmdptr_load(DBDMA_channel *ch)
  86. {
  87. DBDMA_DPRINTFCH(ch, "dbdma_cmdptr_load 0x%08x\n",
  88. ch->regs[DBDMA_CMDPTR_LO]);
  89. dma_memory_read(&address_space_memory, ch->regs[DBDMA_CMDPTR_LO],
  90. &ch->current, sizeof(dbdma_cmd), MEMTXATTRS_UNSPECIFIED);
  91. }
  92. static void dbdma_cmdptr_save(DBDMA_channel *ch)
  93. {
  94. DBDMA_DPRINTFCH(ch, "-> update 0x%08x stat=0x%08x, res=0x%04x\n",
  95. ch->regs[DBDMA_CMDPTR_LO],
  96. le16_to_cpu(ch->current.xfer_status),
  97. le16_to_cpu(ch->current.res_count));
  98. dma_memory_write(&address_space_memory, ch->regs[DBDMA_CMDPTR_LO],
  99. &ch->current, sizeof(dbdma_cmd), MEMTXATTRS_UNSPECIFIED);
  100. }
  101. static void kill_channel(DBDMA_channel *ch)
  102. {
  103. DBDMA_DPRINTFCH(ch, "kill_channel\n");
  104. ch->regs[DBDMA_STATUS] |= DEAD;
  105. ch->regs[DBDMA_STATUS] &= ~ACTIVE;
  106. qemu_irq_raise(ch->irq);
  107. }
  108. static void conditional_interrupt(DBDMA_channel *ch)
  109. {
  110. dbdma_cmd *current = &ch->current;
  111. uint16_t intr;
  112. uint16_t sel_mask, sel_value;
  113. uint32_t status;
  114. int cond;
  115. DBDMA_DPRINTFCH(ch, "%s\n", __func__);
  116. intr = le16_to_cpu(current->command) & INTR_MASK;
  117. switch(intr) {
  118. case INTR_NEVER: /* don't interrupt */
  119. return;
  120. case INTR_ALWAYS: /* always interrupt */
  121. qemu_irq_raise(ch->irq);
  122. DBDMA_DPRINTFCH(ch, "%s: raise\n", __func__);
  123. return;
  124. }
  125. status = ch->regs[DBDMA_STATUS] & DEVSTAT;
  126. sel_mask = (ch->regs[DBDMA_INTR_SEL] >> 16) & 0x0f;
  127. sel_value = ch->regs[DBDMA_INTR_SEL] & 0x0f;
  128. cond = (status & sel_mask) == (sel_value & sel_mask);
  129. switch(intr) {
  130. case INTR_IFSET: /* intr if condition bit is 1 */
  131. if (cond) {
  132. qemu_irq_raise(ch->irq);
  133. DBDMA_DPRINTFCH(ch, "%s: raise\n", __func__);
  134. }
  135. return;
  136. case INTR_IFCLR: /* intr if condition bit is 0 */
  137. if (!cond) {
  138. qemu_irq_raise(ch->irq);
  139. DBDMA_DPRINTFCH(ch, "%s: raise\n", __func__);
  140. }
  141. return;
  142. }
  143. }
  144. static int conditional_wait(DBDMA_channel *ch)
  145. {
  146. dbdma_cmd *current = &ch->current;
  147. uint16_t wait;
  148. uint16_t sel_mask, sel_value;
  149. uint32_t status;
  150. int cond;
  151. int res = 0;
  152. wait = le16_to_cpu(current->command) & WAIT_MASK;
  153. switch(wait) {
  154. case WAIT_NEVER: /* don't wait */
  155. return 0;
  156. case WAIT_ALWAYS: /* always wait */
  157. DBDMA_DPRINTFCH(ch, " [WAIT_ALWAYS]\n");
  158. return 1;
  159. }
  160. status = ch->regs[DBDMA_STATUS] & DEVSTAT;
  161. sel_mask = (ch->regs[DBDMA_WAIT_SEL] >> 16) & 0x0f;
  162. sel_value = ch->regs[DBDMA_WAIT_SEL] & 0x0f;
  163. cond = (status & sel_mask) == (sel_value & sel_mask);
  164. switch(wait) {
  165. case WAIT_IFSET: /* wait if condition bit is 1 */
  166. if (cond) {
  167. res = 1;
  168. }
  169. DBDMA_DPRINTFCH(ch, " [WAIT_IFSET=%d]\n", res);
  170. break;
  171. case WAIT_IFCLR: /* wait if condition bit is 0 */
  172. if (!cond) {
  173. res = 1;
  174. }
  175. DBDMA_DPRINTFCH(ch, " [WAIT_IFCLR=%d]\n", res);
  176. break;
  177. }
  178. return res;
  179. }
  180. static void next(DBDMA_channel *ch)
  181. {
  182. uint32_t cp;
  183. ch->regs[DBDMA_STATUS] &= ~BT;
  184. cp = ch->regs[DBDMA_CMDPTR_LO];
  185. ch->regs[DBDMA_CMDPTR_LO] = cp + sizeof(dbdma_cmd);
  186. dbdma_cmdptr_load(ch);
  187. }
  188. static void branch(DBDMA_channel *ch)
  189. {
  190. dbdma_cmd *current = &ch->current;
  191. ch->regs[DBDMA_CMDPTR_LO] = le32_to_cpu(current->cmd_dep);
  192. ch->regs[DBDMA_STATUS] |= BT;
  193. dbdma_cmdptr_load(ch);
  194. }
  195. static void conditional_branch(DBDMA_channel *ch)
  196. {
  197. dbdma_cmd *current = &ch->current;
  198. uint16_t br;
  199. uint16_t sel_mask, sel_value;
  200. uint32_t status;
  201. int cond;
  202. /* check if we must branch */
  203. br = le16_to_cpu(current->command) & BR_MASK;
  204. switch(br) {
  205. case BR_NEVER: /* don't branch */
  206. next(ch);
  207. return;
  208. case BR_ALWAYS: /* always branch */
  209. DBDMA_DPRINTFCH(ch, " [BR_ALWAYS]\n");
  210. branch(ch);
  211. return;
  212. }
  213. status = ch->regs[DBDMA_STATUS] & DEVSTAT;
  214. sel_mask = (ch->regs[DBDMA_BRANCH_SEL] >> 16) & 0x0f;
  215. sel_value = ch->regs[DBDMA_BRANCH_SEL] & 0x0f;
  216. cond = (status & sel_mask) == (sel_value & sel_mask);
  217. switch(br) {
  218. case BR_IFSET: /* branch if condition bit is 1 */
  219. if (cond) {
  220. DBDMA_DPRINTFCH(ch, " [BR_IFSET = 1]\n");
  221. branch(ch);
  222. } else {
  223. DBDMA_DPRINTFCH(ch, " [BR_IFSET = 0]\n");
  224. next(ch);
  225. }
  226. return;
  227. case BR_IFCLR: /* branch if condition bit is 0 */
  228. if (!cond) {
  229. DBDMA_DPRINTFCH(ch, " [BR_IFCLR = 1]\n");
  230. branch(ch);
  231. } else {
  232. DBDMA_DPRINTFCH(ch, " [BR_IFCLR = 0]\n");
  233. next(ch);
  234. }
  235. return;
  236. }
  237. }
  238. static void channel_run(DBDMA_channel *ch);
  239. static void dbdma_end(DBDMA_io *io)
  240. {
  241. DBDMA_channel *ch = io->channel;
  242. dbdma_cmd *current = &ch->current;
  243. DBDMA_DPRINTFCH(ch, "%s\n", __func__);
  244. if (conditional_wait(ch))
  245. goto wait;
  246. current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
  247. current->res_count = cpu_to_le16(io->len);
  248. dbdma_cmdptr_save(ch);
  249. if (io->is_last)
  250. ch->regs[DBDMA_STATUS] &= ~FLUSH;
  251. conditional_interrupt(ch);
  252. conditional_branch(ch);
  253. wait:
  254. /* Indicate that we're ready for a new DMA round */
  255. ch->io.processing = false;
  256. if ((ch->regs[DBDMA_STATUS] & RUN) &&
  257. (ch->regs[DBDMA_STATUS] & ACTIVE))
  258. channel_run(ch);
  259. }
  260. static void start_output(DBDMA_channel *ch, int key, uint32_t addr,
  261. uint16_t req_count, int is_last)
  262. {
  263. DBDMA_DPRINTFCH(ch, "start_output\n");
  264. /* KEY_REGS, KEY_DEVICE and KEY_STREAM
  265. * are not implemented in the mac-io chip
  266. */
  267. DBDMA_DPRINTFCH(ch, "addr 0x%x key 0x%x\n", addr, key);
  268. if (!addr || key > KEY_STREAM3) {
  269. kill_channel(ch);
  270. return;
  271. }
  272. ch->io.addr = addr;
  273. ch->io.len = req_count;
  274. ch->io.is_last = is_last;
  275. ch->io.dma_end = dbdma_end;
  276. ch->io.is_dma_out = 1;
  277. ch->io.processing = true;
  278. if (ch->rw) {
  279. ch->rw(&ch->io);
  280. }
  281. }
  282. static void start_input(DBDMA_channel *ch, int key, uint32_t addr,
  283. uint16_t req_count, int is_last)
  284. {
  285. DBDMA_DPRINTFCH(ch, "start_input\n");
  286. /* KEY_REGS, KEY_DEVICE and KEY_STREAM
  287. * are not implemented in the mac-io chip
  288. */
  289. DBDMA_DPRINTFCH(ch, "addr 0x%x key 0x%x\n", addr, key);
  290. if (!addr || key > KEY_STREAM3) {
  291. kill_channel(ch);
  292. return;
  293. }
  294. ch->io.addr = addr;
  295. ch->io.len = req_count;
  296. ch->io.is_last = is_last;
  297. ch->io.dma_end = dbdma_end;
  298. ch->io.is_dma_out = 0;
  299. ch->io.processing = true;
  300. if (ch->rw) {
  301. ch->rw(&ch->io);
  302. }
  303. }
  304. static void load_word(DBDMA_channel *ch, int key, uint32_t addr,
  305. uint16_t len)
  306. {
  307. dbdma_cmd *current = &ch->current;
  308. DBDMA_DPRINTFCH(ch, "load_word %d bytes, addr=%08x\n", len, addr);
  309. /* only implements KEY_SYSTEM */
  310. if (key != KEY_SYSTEM) {
  311. printf("DBDMA: LOAD_WORD, unimplemented key %x\n", key);
  312. kill_channel(ch);
  313. return;
  314. }
  315. dma_memory_read(&address_space_memory, addr, &current->cmd_dep, len,
  316. MEMTXATTRS_UNSPECIFIED);
  317. if (conditional_wait(ch))
  318. goto wait;
  319. current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
  320. dbdma_cmdptr_save(ch);
  321. ch->regs[DBDMA_STATUS] &= ~FLUSH;
  322. conditional_interrupt(ch);
  323. next(ch);
  324. wait:
  325. DBDMA_kick(dbdma_from_ch(ch));
  326. }
  327. static void store_word(DBDMA_channel *ch, int key, uint32_t addr,
  328. uint16_t len)
  329. {
  330. dbdma_cmd *current = &ch->current;
  331. DBDMA_DPRINTFCH(ch, "store_word %d bytes, addr=%08x pa=%x\n",
  332. len, addr, le32_to_cpu(current->cmd_dep));
  333. /* only implements KEY_SYSTEM */
  334. if (key != KEY_SYSTEM) {
  335. printf("DBDMA: STORE_WORD, unimplemented key %x\n", key);
  336. kill_channel(ch);
  337. return;
  338. }
  339. dma_memory_write(&address_space_memory, addr, &current->cmd_dep, len,
  340. MEMTXATTRS_UNSPECIFIED);
  341. if (conditional_wait(ch))
  342. goto wait;
  343. current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
  344. dbdma_cmdptr_save(ch);
  345. ch->regs[DBDMA_STATUS] &= ~FLUSH;
  346. conditional_interrupt(ch);
  347. next(ch);
  348. wait:
  349. DBDMA_kick(dbdma_from_ch(ch));
  350. }
  351. static void nop(DBDMA_channel *ch)
  352. {
  353. dbdma_cmd *current = &ch->current;
  354. if (conditional_wait(ch))
  355. goto wait;
  356. current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
  357. dbdma_cmdptr_save(ch);
  358. conditional_interrupt(ch);
  359. conditional_branch(ch);
  360. wait:
  361. DBDMA_kick(dbdma_from_ch(ch));
  362. }
  363. static void stop(DBDMA_channel *ch)
  364. {
  365. ch->regs[DBDMA_STATUS] &= ~(ACTIVE);
  366. /* the stop command does not increment command pointer */
  367. }
  368. static void channel_run(DBDMA_channel *ch)
  369. {
  370. dbdma_cmd *current = &ch->current;
  371. uint16_t cmd, key;
  372. uint16_t req_count;
  373. uint32_t phy_addr;
  374. DBDMA_DPRINTFCH(ch, "channel_run\n");
  375. dump_dbdma_cmd(ch, current);
  376. /* clear WAKE flag at command fetch */
  377. ch->regs[DBDMA_STATUS] &= ~WAKE;
  378. cmd = le16_to_cpu(current->command) & COMMAND_MASK;
  379. switch (cmd) {
  380. case DBDMA_NOP:
  381. nop(ch);
  382. return;
  383. case DBDMA_STOP:
  384. stop(ch);
  385. return;
  386. }
  387. key = le16_to_cpu(current->command) & 0x0700;
  388. req_count = le16_to_cpu(current->req_count);
  389. phy_addr = le32_to_cpu(current->phy_addr);
  390. if (key == KEY_STREAM4) {
  391. printf("command %x, invalid key 4\n", cmd);
  392. kill_channel(ch);
  393. return;
  394. }
  395. switch (cmd) {
  396. case OUTPUT_MORE:
  397. DBDMA_DPRINTFCH(ch, "* OUTPUT_MORE *\n");
  398. start_output(ch, key, phy_addr, req_count, 0);
  399. return;
  400. case OUTPUT_LAST:
  401. DBDMA_DPRINTFCH(ch, "* OUTPUT_LAST *\n");
  402. start_output(ch, key, phy_addr, req_count, 1);
  403. return;
  404. case INPUT_MORE:
  405. DBDMA_DPRINTFCH(ch, "* INPUT_MORE *\n");
  406. start_input(ch, key, phy_addr, req_count, 0);
  407. return;
  408. case INPUT_LAST:
  409. DBDMA_DPRINTFCH(ch, "* INPUT_LAST *\n");
  410. start_input(ch, key, phy_addr, req_count, 1);
  411. return;
  412. }
  413. if (key < KEY_REGS) {
  414. printf("command %x, invalid key %x\n", cmd, key);
  415. key = KEY_SYSTEM;
  416. }
  417. /* for LOAD_WORD and STORE_WORD, req_count is on 3 bits
  418. * and BRANCH is invalid
  419. */
  420. req_count = req_count & 0x0007;
  421. if (req_count & 0x4) {
  422. req_count = 4;
  423. phy_addr &= ~3;
  424. } else if (req_count & 0x2) {
  425. req_count = 2;
  426. phy_addr &= ~1;
  427. } else
  428. req_count = 1;
  429. switch (cmd) {
  430. case LOAD_WORD:
  431. DBDMA_DPRINTFCH(ch, "* LOAD_WORD *\n");
  432. load_word(ch, key, phy_addr, req_count);
  433. return;
  434. case STORE_WORD:
  435. DBDMA_DPRINTFCH(ch, "* STORE_WORD *\n");
  436. store_word(ch, key, phy_addr, req_count);
  437. return;
  438. }
  439. }
  440. static void DBDMA_run(DBDMAState *s)
  441. {
  442. int channel;
  443. for (channel = 0; channel < DBDMA_CHANNELS; channel++) {
  444. DBDMA_channel *ch = &s->channels[channel];
  445. uint32_t status = ch->regs[DBDMA_STATUS];
  446. if (!ch->io.processing && (status & RUN) && (status & ACTIVE)) {
  447. channel_run(ch);
  448. }
  449. }
  450. }
  451. static void DBDMA_run_bh(void *opaque)
  452. {
  453. DBDMAState *s = opaque;
  454. DBDMA_DPRINTF("-> DBDMA_run_bh\n");
  455. DBDMA_run(s);
  456. DBDMA_DPRINTF("<- DBDMA_run_bh\n");
  457. }
  458. void DBDMA_kick(DBDMAState *dbdma)
  459. {
  460. qemu_bh_schedule(dbdma->bh);
  461. }
  462. void DBDMA_register_channel(void *dbdma, int nchan, qemu_irq irq,
  463. DBDMA_rw rw, DBDMA_flush flush,
  464. void *opaque)
  465. {
  466. DBDMAState *s = dbdma;
  467. DBDMA_channel *ch = &s->channels[nchan];
  468. DBDMA_DPRINTFCH(ch, "DBDMA_register_channel 0x%x\n", nchan);
  469. assert(rw);
  470. assert(flush);
  471. ch->irq = irq;
  472. ch->rw = rw;
  473. ch->flush = flush;
  474. ch->io.opaque = opaque;
  475. }
  476. static void dbdma_control_write(DBDMA_channel *ch)
  477. {
  478. uint16_t mask, value;
  479. uint32_t status;
  480. bool do_flush = false;
  481. mask = (ch->regs[DBDMA_CONTROL] >> 16) & 0xffff;
  482. value = ch->regs[DBDMA_CONTROL] & 0xffff;
  483. /* This is the status register which we'll update
  484. * appropriately and store back
  485. */
  486. status = ch->regs[DBDMA_STATUS];
  487. /* RUN and PAUSE are bits under SW control only
  488. * FLUSH and WAKE are set by SW and cleared by HW
  489. * DEAD, ACTIVE and BT are only under HW control
  490. *
  491. * We handle ACTIVE separately at the end of the
  492. * logic to ensure all cases are covered.
  493. */
  494. /* Setting RUN will tentatively activate the channel
  495. */
  496. if ((mask & RUN) && (value & RUN)) {
  497. status |= RUN;
  498. DBDMA_DPRINTFCH(ch, " Setting RUN !\n");
  499. }
  500. /* Clearing RUN 1->0 will stop the channel */
  501. if ((mask & RUN) && !(value & RUN)) {
  502. /* This has the side effect of clearing the DEAD bit */
  503. status &= ~(DEAD | RUN);
  504. DBDMA_DPRINTFCH(ch, " Clearing RUN !\n");
  505. }
  506. /* Setting WAKE wakes up an idle channel if it's running
  507. *
  508. * Note: The doc doesn't say so but assume that only works
  509. * on a channel whose RUN bit is set.
  510. *
  511. * We set WAKE in status, it's not terribly useful as it will
  512. * be cleared on the next command fetch but it seems to mimmic
  513. * the HW behaviour and is useful for the way we handle
  514. * ACTIVE further down.
  515. */
  516. if ((mask & WAKE) && (value & WAKE) && (status & RUN)) {
  517. status |= WAKE;
  518. DBDMA_DPRINTFCH(ch, " Setting WAKE !\n");
  519. }
  520. /* PAUSE being set will deactivate (or prevent activation)
  521. * of the channel. We just copy it over for now, ACTIVE will
  522. * be re-evaluated later.
  523. */
  524. if (mask & PAUSE) {
  525. status = (status & ~PAUSE) | (value & PAUSE);
  526. DBDMA_DPRINTFCH(ch, " %sing PAUSE !\n",
  527. (value & PAUSE) ? "sett" : "clear");
  528. }
  529. /* FLUSH is its own thing */
  530. if ((mask & FLUSH) && (value & FLUSH)) {
  531. DBDMA_DPRINTFCH(ch, " Setting FLUSH !\n");
  532. /* We set flush directly in the status register, we do *NOT*
  533. * set it in "status" so that it gets naturally cleared when
  534. * we update the status register further down. That way it
  535. * will be set only during the HW flush operation so it is
  536. * visible to any completions happening during that time.
  537. */
  538. ch->regs[DBDMA_STATUS] |= FLUSH;
  539. do_flush = true;
  540. }
  541. /* If either RUN or PAUSE is clear, so should ACTIVE be,
  542. * otherwise, ACTIVE will be set if we modified RUN, PAUSE or
  543. * set WAKE. That means that PAUSE was just cleared, RUN was
  544. * just set or WAKE was just set.
  545. */
  546. if ((status & PAUSE) || !(status & RUN)) {
  547. status &= ~ACTIVE;
  548. DBDMA_DPRINTFCH(ch, " -> ACTIVE down !\n");
  549. /* We stopped processing, we want the underlying HW command
  550. * to complete *before* we clear the ACTIVE bit. Otherwise
  551. * we can get into a situation where the command status will
  552. * have RUN or ACTIVE not set which is going to confuse the
  553. * MacOS driver.
  554. */
  555. do_flush = true;
  556. } else if (mask & (RUN | PAUSE)) {
  557. status |= ACTIVE;
  558. DBDMA_DPRINTFCH(ch, " -> ACTIVE up !\n");
  559. } else if ((mask & WAKE) && (value & WAKE)) {
  560. status |= ACTIVE;
  561. DBDMA_DPRINTFCH(ch, " -> ACTIVE up !\n");
  562. }
  563. DBDMA_DPRINTFCH(ch, " new status=0x%08x\n", status);
  564. /* If we need to flush the underlying HW, do it now, this happens
  565. * both on FLUSH commands and when stopping the channel for safety.
  566. */
  567. if (do_flush && ch->flush) {
  568. ch->flush(&ch->io);
  569. }
  570. /* Finally update the status register image */
  571. ch->regs[DBDMA_STATUS] = status;
  572. /* If active, make sure the BH gets to run */
  573. if (status & ACTIVE) {
  574. DBDMA_kick(dbdma_from_ch(ch));
  575. }
  576. }
  577. static void dbdma_write(void *opaque, hwaddr addr,
  578. uint64_t value, unsigned size)
  579. {
  580. int channel = addr >> DBDMA_CHANNEL_SHIFT;
  581. DBDMAState *s = opaque;
  582. DBDMA_channel *ch = &s->channels[channel];
  583. int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2;
  584. DBDMA_DPRINTFCH(ch, "writel 0x" HWADDR_FMT_plx " <= 0x%08"PRIx64"\n",
  585. addr, value);
  586. DBDMA_DPRINTFCH(ch, "channel 0x%x reg 0x%x\n",
  587. (uint32_t)addr >> DBDMA_CHANNEL_SHIFT, reg);
  588. /* cmdptr cannot be modified if channel is ACTIVE */
  589. if (reg == DBDMA_CMDPTR_LO && (ch->regs[DBDMA_STATUS] & ACTIVE)) {
  590. return;
  591. }
  592. ch->regs[reg] = value;
  593. switch(reg) {
  594. case DBDMA_CONTROL:
  595. dbdma_control_write(ch);
  596. break;
  597. case DBDMA_CMDPTR_LO:
  598. /* 16-byte aligned */
  599. ch->regs[DBDMA_CMDPTR_LO] &= ~0xf;
  600. dbdma_cmdptr_load(ch);
  601. break;
  602. case DBDMA_STATUS:
  603. case DBDMA_INTR_SEL:
  604. case DBDMA_BRANCH_SEL:
  605. case DBDMA_WAIT_SEL:
  606. /* nothing to do */
  607. break;
  608. case DBDMA_XFER_MODE:
  609. case DBDMA_CMDPTR_HI:
  610. case DBDMA_DATA2PTR_HI:
  611. case DBDMA_DATA2PTR_LO:
  612. case DBDMA_ADDRESS_HI:
  613. case DBDMA_BRANCH_ADDR_HI:
  614. case DBDMA_RES1:
  615. case DBDMA_RES2:
  616. case DBDMA_RES3:
  617. case DBDMA_RES4:
  618. /* unused */
  619. break;
  620. }
  621. }
  622. static uint64_t dbdma_read(void *opaque, hwaddr addr,
  623. unsigned size)
  624. {
  625. uint32_t value;
  626. int channel = addr >> DBDMA_CHANNEL_SHIFT;
  627. DBDMAState *s = opaque;
  628. DBDMA_channel *ch = &s->channels[channel];
  629. int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2;
  630. value = ch->regs[reg];
  631. switch(reg) {
  632. case DBDMA_CONTROL:
  633. value = ch->regs[DBDMA_STATUS];
  634. break;
  635. case DBDMA_STATUS:
  636. case DBDMA_CMDPTR_LO:
  637. case DBDMA_INTR_SEL:
  638. case DBDMA_BRANCH_SEL:
  639. case DBDMA_WAIT_SEL:
  640. /* nothing to do */
  641. break;
  642. case DBDMA_XFER_MODE:
  643. case DBDMA_CMDPTR_HI:
  644. case DBDMA_DATA2PTR_HI:
  645. case DBDMA_DATA2PTR_LO:
  646. case DBDMA_ADDRESS_HI:
  647. case DBDMA_BRANCH_ADDR_HI:
  648. /* unused */
  649. value = 0;
  650. break;
  651. case DBDMA_RES1:
  652. case DBDMA_RES2:
  653. case DBDMA_RES3:
  654. case DBDMA_RES4:
  655. /* reserved */
  656. break;
  657. }
  658. DBDMA_DPRINTFCH(ch, "readl 0x" HWADDR_FMT_plx " => 0x%08x\n", addr, value);
  659. DBDMA_DPRINTFCH(ch, "channel 0x%x reg 0x%x\n",
  660. (uint32_t)addr >> DBDMA_CHANNEL_SHIFT, reg);
  661. return value;
  662. }
  663. static const MemoryRegionOps dbdma_ops = {
  664. .read = dbdma_read,
  665. .write = dbdma_write,
  666. .endianness = DEVICE_LITTLE_ENDIAN,
  667. .valid = {
  668. .min_access_size = 4,
  669. .max_access_size = 4,
  670. },
  671. };
  672. static const VMStateDescription vmstate_dbdma_io = {
  673. .name = "dbdma_io",
  674. .version_id = 0,
  675. .minimum_version_id = 0,
  676. .fields = (const VMStateField[]) {
  677. VMSTATE_UINT64(addr, struct DBDMA_io),
  678. VMSTATE_INT32(len, struct DBDMA_io),
  679. VMSTATE_INT32(is_last, struct DBDMA_io),
  680. VMSTATE_INT32(is_dma_out, struct DBDMA_io),
  681. VMSTATE_BOOL(processing, struct DBDMA_io),
  682. VMSTATE_END_OF_LIST()
  683. }
  684. };
  685. static const VMStateDescription vmstate_dbdma_cmd = {
  686. .name = "dbdma_cmd",
  687. .version_id = 0,
  688. .minimum_version_id = 0,
  689. .fields = (const VMStateField[]) {
  690. VMSTATE_UINT16(req_count, dbdma_cmd),
  691. VMSTATE_UINT16(command, dbdma_cmd),
  692. VMSTATE_UINT32(phy_addr, dbdma_cmd),
  693. VMSTATE_UINT32(cmd_dep, dbdma_cmd),
  694. VMSTATE_UINT16(res_count, dbdma_cmd),
  695. VMSTATE_UINT16(xfer_status, dbdma_cmd),
  696. VMSTATE_END_OF_LIST()
  697. }
  698. };
  699. static const VMStateDescription vmstate_dbdma_channel = {
  700. .name = "dbdma_channel",
  701. .version_id = 1,
  702. .minimum_version_id = 1,
  703. .fields = (const VMStateField[]) {
  704. VMSTATE_UINT32_ARRAY(regs, struct DBDMA_channel, DBDMA_REGS),
  705. VMSTATE_STRUCT(io, struct DBDMA_channel, 0, vmstate_dbdma_io, DBDMA_io),
  706. VMSTATE_STRUCT(current, struct DBDMA_channel, 0, vmstate_dbdma_cmd,
  707. dbdma_cmd),
  708. VMSTATE_END_OF_LIST()
  709. }
  710. };
  711. static const VMStateDescription vmstate_dbdma = {
  712. .name = "dbdma",
  713. .version_id = 3,
  714. .minimum_version_id = 3,
  715. .fields = (const VMStateField[]) {
  716. VMSTATE_STRUCT_ARRAY(channels, DBDMAState, DBDMA_CHANNELS, 1,
  717. vmstate_dbdma_channel, DBDMA_channel),
  718. VMSTATE_END_OF_LIST()
  719. }
  720. };
  721. static void mac_dbdma_reset(DeviceState *d)
  722. {
  723. DBDMAState *s = MAC_DBDMA(d);
  724. int i;
  725. for (i = 0; i < DBDMA_CHANNELS; i++) {
  726. memset(s->channels[i].regs, 0, DBDMA_SIZE);
  727. }
  728. }
  729. static void dbdma_unassigned_rw(DBDMA_io *io)
  730. {
  731. DBDMA_channel *ch = io->channel;
  732. dbdma_cmd *current = &ch->current;
  733. uint16_t cmd;
  734. qemu_log_mask(LOG_GUEST_ERROR, "%s: use of unassigned channel %d\n",
  735. __func__, ch->channel);
  736. ch->io.processing = false;
  737. cmd = le16_to_cpu(current->command) & COMMAND_MASK;
  738. if (cmd == OUTPUT_MORE || cmd == OUTPUT_LAST ||
  739. cmd == INPUT_MORE || cmd == INPUT_LAST) {
  740. current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
  741. current->res_count = cpu_to_le16(io->len);
  742. dbdma_cmdptr_save(ch);
  743. }
  744. }
  745. static void dbdma_unassigned_flush(DBDMA_io *io)
  746. {
  747. DBDMA_channel *ch = io->channel;
  748. qemu_log_mask(LOG_GUEST_ERROR, "%s: use of unassigned channel %d\n",
  749. __func__, ch->channel);
  750. }
  751. static void mac_dbdma_init(Object *obj)
  752. {
  753. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  754. DBDMAState *s = MAC_DBDMA(obj);
  755. int i;
  756. for (i = 0; i < DBDMA_CHANNELS; i++) {
  757. DBDMA_channel *ch = &s->channels[i];
  758. ch->rw = dbdma_unassigned_rw;
  759. ch->flush = dbdma_unassigned_flush;
  760. ch->channel = i;
  761. ch->io.channel = ch;
  762. }
  763. memory_region_init_io(&s->mem, obj, &dbdma_ops, s, "dbdma", 0x1000);
  764. sysbus_init_mmio(sbd, &s->mem);
  765. }
  766. static void mac_dbdma_realize(DeviceState *dev, Error **errp)
  767. {
  768. DBDMAState *s = MAC_DBDMA(dev);
  769. s->bh = qemu_bh_new_guarded(DBDMA_run_bh, s, &dev->mem_reentrancy_guard);
  770. }
  771. static void mac_dbdma_class_init(ObjectClass *oc, void *data)
  772. {
  773. DeviceClass *dc = DEVICE_CLASS(oc);
  774. dc->realize = mac_dbdma_realize;
  775. device_class_set_legacy_reset(dc, mac_dbdma_reset);
  776. dc->vmsd = &vmstate_dbdma;
  777. }
  778. static const TypeInfo mac_dbdma_type_info = {
  779. .name = TYPE_MAC_DBDMA,
  780. .parent = TYPE_SYS_BUS_DEVICE,
  781. .instance_size = sizeof(DBDMAState),
  782. .instance_init = mac_dbdma_init,
  783. .class_init = mac_dbdma_class_init
  784. };
  785. static void mac_dbdma_register_types(void)
  786. {
  787. type_register_static(&mac_dbdma_type_info);
  788. }
  789. type_init(mac_dbdma_register_types)