2
0

mac_dbdma.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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 "sysemu/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));
  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));
  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. if (conditional_wait(ch))
  317. goto wait;
  318. current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
  319. dbdma_cmdptr_save(ch);
  320. ch->regs[DBDMA_STATUS] &= ~FLUSH;
  321. conditional_interrupt(ch);
  322. next(ch);
  323. wait:
  324. DBDMA_kick(dbdma_from_ch(ch));
  325. }
  326. static void store_word(DBDMA_channel *ch, int key, uint32_t addr,
  327. uint16_t len)
  328. {
  329. dbdma_cmd *current = &ch->current;
  330. DBDMA_DPRINTFCH(ch, "store_word %d bytes, addr=%08x pa=%x\n",
  331. len, addr, le32_to_cpu(current->cmd_dep));
  332. /* only implements KEY_SYSTEM */
  333. if (key != KEY_SYSTEM) {
  334. printf("DBDMA: STORE_WORD, unimplemented key %x\n", key);
  335. kill_channel(ch);
  336. return;
  337. }
  338. dma_memory_write(&address_space_memory, addr, &current->cmd_dep, len);
  339. if (conditional_wait(ch))
  340. goto wait;
  341. current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
  342. dbdma_cmdptr_save(ch);
  343. ch->regs[DBDMA_STATUS] &= ~FLUSH;
  344. conditional_interrupt(ch);
  345. next(ch);
  346. wait:
  347. DBDMA_kick(dbdma_from_ch(ch));
  348. }
  349. static void nop(DBDMA_channel *ch)
  350. {
  351. dbdma_cmd *current = &ch->current;
  352. if (conditional_wait(ch))
  353. goto wait;
  354. current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
  355. dbdma_cmdptr_save(ch);
  356. conditional_interrupt(ch);
  357. conditional_branch(ch);
  358. wait:
  359. DBDMA_kick(dbdma_from_ch(ch));
  360. }
  361. static void stop(DBDMA_channel *ch)
  362. {
  363. ch->regs[DBDMA_STATUS] &= ~(ACTIVE);
  364. /* the stop command does not increment command pointer */
  365. }
  366. static void channel_run(DBDMA_channel *ch)
  367. {
  368. dbdma_cmd *current = &ch->current;
  369. uint16_t cmd, key;
  370. uint16_t req_count;
  371. uint32_t phy_addr;
  372. DBDMA_DPRINTFCH(ch, "channel_run\n");
  373. dump_dbdma_cmd(ch, current);
  374. /* clear WAKE flag at command fetch */
  375. ch->regs[DBDMA_STATUS] &= ~WAKE;
  376. cmd = le16_to_cpu(current->command) & COMMAND_MASK;
  377. switch (cmd) {
  378. case DBDMA_NOP:
  379. nop(ch);
  380. return;
  381. case DBDMA_STOP:
  382. stop(ch);
  383. return;
  384. }
  385. key = le16_to_cpu(current->command) & 0x0700;
  386. req_count = le16_to_cpu(current->req_count);
  387. phy_addr = le32_to_cpu(current->phy_addr);
  388. if (key == KEY_STREAM4) {
  389. printf("command %x, invalid key 4\n", cmd);
  390. kill_channel(ch);
  391. return;
  392. }
  393. switch (cmd) {
  394. case OUTPUT_MORE:
  395. DBDMA_DPRINTFCH(ch, "* OUTPUT_MORE *\n");
  396. start_output(ch, key, phy_addr, req_count, 0);
  397. return;
  398. case OUTPUT_LAST:
  399. DBDMA_DPRINTFCH(ch, "* OUTPUT_LAST *\n");
  400. start_output(ch, key, phy_addr, req_count, 1);
  401. return;
  402. case INPUT_MORE:
  403. DBDMA_DPRINTFCH(ch, "* INPUT_MORE *\n");
  404. start_input(ch, key, phy_addr, req_count, 0);
  405. return;
  406. case INPUT_LAST:
  407. DBDMA_DPRINTFCH(ch, "* INPUT_LAST *\n");
  408. start_input(ch, key, phy_addr, req_count, 1);
  409. return;
  410. }
  411. if (key < KEY_REGS) {
  412. printf("command %x, invalid key %x\n", cmd, key);
  413. key = KEY_SYSTEM;
  414. }
  415. /* for LOAD_WORD and STORE_WORD, req_count is on 3 bits
  416. * and BRANCH is invalid
  417. */
  418. req_count = req_count & 0x0007;
  419. if (req_count & 0x4) {
  420. req_count = 4;
  421. phy_addr &= ~3;
  422. } else if (req_count & 0x2) {
  423. req_count = 2;
  424. phy_addr &= ~1;
  425. } else
  426. req_count = 1;
  427. switch (cmd) {
  428. case LOAD_WORD:
  429. DBDMA_DPRINTFCH(ch, "* LOAD_WORD *\n");
  430. load_word(ch, key, phy_addr, req_count);
  431. return;
  432. case STORE_WORD:
  433. DBDMA_DPRINTFCH(ch, "* STORE_WORD *\n");
  434. store_word(ch, key, phy_addr, req_count);
  435. return;
  436. }
  437. }
  438. static void DBDMA_run(DBDMAState *s)
  439. {
  440. int channel;
  441. for (channel = 0; channel < DBDMA_CHANNELS; channel++) {
  442. DBDMA_channel *ch = &s->channels[channel];
  443. uint32_t status = ch->regs[DBDMA_STATUS];
  444. if (!ch->io.processing && (status & RUN) && (status & ACTIVE)) {
  445. channel_run(ch);
  446. }
  447. }
  448. }
  449. static void DBDMA_run_bh(void *opaque)
  450. {
  451. DBDMAState *s = opaque;
  452. DBDMA_DPRINTF("-> DBDMA_run_bh\n");
  453. DBDMA_run(s);
  454. DBDMA_DPRINTF("<- DBDMA_run_bh\n");
  455. }
  456. void DBDMA_kick(DBDMAState *dbdma)
  457. {
  458. qemu_bh_schedule(dbdma->bh);
  459. }
  460. void DBDMA_register_channel(void *dbdma, int nchan, qemu_irq irq,
  461. DBDMA_rw rw, DBDMA_flush flush,
  462. void *opaque)
  463. {
  464. DBDMAState *s = dbdma;
  465. DBDMA_channel *ch = &s->channels[nchan];
  466. DBDMA_DPRINTFCH(ch, "DBDMA_register_channel 0x%x\n", nchan);
  467. assert(rw);
  468. assert(flush);
  469. ch->irq = irq;
  470. ch->rw = rw;
  471. ch->flush = flush;
  472. ch->io.opaque = opaque;
  473. }
  474. static void dbdma_control_write(DBDMA_channel *ch)
  475. {
  476. uint16_t mask, value;
  477. uint32_t status;
  478. bool do_flush = false;
  479. mask = (ch->regs[DBDMA_CONTROL] >> 16) & 0xffff;
  480. value = ch->regs[DBDMA_CONTROL] & 0xffff;
  481. /* This is the status register which we'll update
  482. * appropriately and store back
  483. */
  484. status = ch->regs[DBDMA_STATUS];
  485. /* RUN and PAUSE are bits under SW control only
  486. * FLUSH and WAKE are set by SW and cleared by HW
  487. * DEAD, ACTIVE and BT are only under HW control
  488. *
  489. * We handle ACTIVE separately at the end of the
  490. * logic to ensure all cases are covered.
  491. */
  492. /* Setting RUN will tentatively activate the channel
  493. */
  494. if ((mask & RUN) && (value & RUN)) {
  495. status |= RUN;
  496. DBDMA_DPRINTFCH(ch, " Setting RUN !\n");
  497. }
  498. /* Clearing RUN 1->0 will stop the channel */
  499. if ((mask & RUN) && !(value & RUN)) {
  500. /* This has the side effect of clearing the DEAD bit */
  501. status &= ~(DEAD | RUN);
  502. DBDMA_DPRINTFCH(ch, " Clearing RUN !\n");
  503. }
  504. /* Setting WAKE wakes up an idle channel if it's running
  505. *
  506. * Note: The doc doesn't say so but assume that only works
  507. * on a channel whose RUN bit is set.
  508. *
  509. * We set WAKE in status, it's not terribly useful as it will
  510. * be cleared on the next command fetch but it seems to mimmic
  511. * the HW behaviour and is useful for the way we handle
  512. * ACTIVE further down.
  513. */
  514. if ((mask & WAKE) && (value & WAKE) && (status & RUN)) {
  515. status |= WAKE;
  516. DBDMA_DPRINTFCH(ch, " Setting WAKE !\n");
  517. }
  518. /* PAUSE being set will deactivate (or prevent activation)
  519. * of the channel. We just copy it over for now, ACTIVE will
  520. * be re-evaluated later.
  521. */
  522. if (mask & PAUSE) {
  523. status = (status & ~PAUSE) | (value & PAUSE);
  524. DBDMA_DPRINTFCH(ch, " %sing PAUSE !\n",
  525. (value & PAUSE) ? "sett" : "clear");
  526. }
  527. /* FLUSH is its own thing */
  528. if ((mask & FLUSH) && (value & FLUSH)) {
  529. DBDMA_DPRINTFCH(ch, " Setting FLUSH !\n");
  530. /* We set flush directly in the status register, we do *NOT*
  531. * set it in "status" so that it gets naturally cleared when
  532. * we update the status register further down. That way it
  533. * will be set only during the HW flush operation so it is
  534. * visible to any completions happening during that time.
  535. */
  536. ch->regs[DBDMA_STATUS] |= FLUSH;
  537. do_flush = true;
  538. }
  539. /* If either RUN or PAUSE is clear, so should ACTIVE be,
  540. * otherwise, ACTIVE will be set if we modified RUN, PAUSE or
  541. * set WAKE. That means that PAUSE was just cleared, RUN was
  542. * just set or WAKE was just set.
  543. */
  544. if ((status & PAUSE) || !(status & RUN)) {
  545. status &= ~ACTIVE;
  546. DBDMA_DPRINTFCH(ch, " -> ACTIVE down !\n");
  547. /* We stopped processing, we want the underlying HW command
  548. * to complete *before* we clear the ACTIVE bit. Otherwise
  549. * we can get into a situation where the command status will
  550. * have RUN or ACTIVE not set which is going to confuse the
  551. * MacOS driver.
  552. */
  553. do_flush = true;
  554. } else if (mask & (RUN | PAUSE)) {
  555. status |= ACTIVE;
  556. DBDMA_DPRINTFCH(ch, " -> ACTIVE up !\n");
  557. } else if ((mask & WAKE) && (value & WAKE)) {
  558. status |= ACTIVE;
  559. DBDMA_DPRINTFCH(ch, " -> ACTIVE up !\n");
  560. }
  561. DBDMA_DPRINTFCH(ch, " new status=0x%08x\n", status);
  562. /* If we need to flush the underlying HW, do it now, this happens
  563. * both on FLUSH commands and when stopping the channel for safety.
  564. */
  565. if (do_flush && ch->flush) {
  566. ch->flush(&ch->io);
  567. }
  568. /* Finally update the status register image */
  569. ch->regs[DBDMA_STATUS] = status;
  570. /* If active, make sure the BH gets to run */
  571. if (status & ACTIVE) {
  572. DBDMA_kick(dbdma_from_ch(ch));
  573. }
  574. }
  575. static void dbdma_write(void *opaque, hwaddr addr,
  576. uint64_t value, unsigned size)
  577. {
  578. int channel = addr >> DBDMA_CHANNEL_SHIFT;
  579. DBDMAState *s = opaque;
  580. DBDMA_channel *ch = &s->channels[channel];
  581. int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2;
  582. DBDMA_DPRINTFCH(ch, "writel 0x" TARGET_FMT_plx " <= 0x%08"PRIx64"\n",
  583. addr, value);
  584. DBDMA_DPRINTFCH(ch, "channel 0x%x reg 0x%x\n",
  585. (uint32_t)addr >> DBDMA_CHANNEL_SHIFT, reg);
  586. /* cmdptr cannot be modified if channel is ACTIVE */
  587. if (reg == DBDMA_CMDPTR_LO && (ch->regs[DBDMA_STATUS] & ACTIVE)) {
  588. return;
  589. }
  590. ch->regs[reg] = value;
  591. switch(reg) {
  592. case DBDMA_CONTROL:
  593. dbdma_control_write(ch);
  594. break;
  595. case DBDMA_CMDPTR_LO:
  596. /* 16-byte aligned */
  597. ch->regs[DBDMA_CMDPTR_LO] &= ~0xf;
  598. dbdma_cmdptr_load(ch);
  599. break;
  600. case DBDMA_STATUS:
  601. case DBDMA_INTR_SEL:
  602. case DBDMA_BRANCH_SEL:
  603. case DBDMA_WAIT_SEL:
  604. /* nothing to do */
  605. break;
  606. case DBDMA_XFER_MODE:
  607. case DBDMA_CMDPTR_HI:
  608. case DBDMA_DATA2PTR_HI:
  609. case DBDMA_DATA2PTR_LO:
  610. case DBDMA_ADDRESS_HI:
  611. case DBDMA_BRANCH_ADDR_HI:
  612. case DBDMA_RES1:
  613. case DBDMA_RES2:
  614. case DBDMA_RES3:
  615. case DBDMA_RES4:
  616. /* unused */
  617. break;
  618. }
  619. }
  620. static uint64_t dbdma_read(void *opaque, hwaddr addr,
  621. unsigned size)
  622. {
  623. uint32_t value;
  624. int channel = addr >> DBDMA_CHANNEL_SHIFT;
  625. DBDMAState *s = opaque;
  626. DBDMA_channel *ch = &s->channels[channel];
  627. int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2;
  628. value = ch->regs[reg];
  629. switch(reg) {
  630. case DBDMA_CONTROL:
  631. value = ch->regs[DBDMA_STATUS];
  632. break;
  633. case DBDMA_STATUS:
  634. case DBDMA_CMDPTR_LO:
  635. case DBDMA_INTR_SEL:
  636. case DBDMA_BRANCH_SEL:
  637. case DBDMA_WAIT_SEL:
  638. /* nothing to do */
  639. break;
  640. case DBDMA_XFER_MODE:
  641. case DBDMA_CMDPTR_HI:
  642. case DBDMA_DATA2PTR_HI:
  643. case DBDMA_DATA2PTR_LO:
  644. case DBDMA_ADDRESS_HI:
  645. case DBDMA_BRANCH_ADDR_HI:
  646. /* unused */
  647. value = 0;
  648. break;
  649. case DBDMA_RES1:
  650. case DBDMA_RES2:
  651. case DBDMA_RES3:
  652. case DBDMA_RES4:
  653. /* reserved */
  654. break;
  655. }
  656. DBDMA_DPRINTFCH(ch, "readl 0x" TARGET_FMT_plx " => 0x%08x\n", addr, value);
  657. DBDMA_DPRINTFCH(ch, "channel 0x%x reg 0x%x\n",
  658. (uint32_t)addr >> DBDMA_CHANNEL_SHIFT, reg);
  659. return value;
  660. }
  661. static const MemoryRegionOps dbdma_ops = {
  662. .read = dbdma_read,
  663. .write = dbdma_write,
  664. .endianness = DEVICE_LITTLE_ENDIAN,
  665. .valid = {
  666. .min_access_size = 4,
  667. .max_access_size = 4,
  668. },
  669. };
  670. static const VMStateDescription vmstate_dbdma_io = {
  671. .name = "dbdma_io",
  672. .version_id = 0,
  673. .minimum_version_id = 0,
  674. .fields = (VMStateField[]) {
  675. VMSTATE_UINT64(addr, struct DBDMA_io),
  676. VMSTATE_INT32(len, struct DBDMA_io),
  677. VMSTATE_INT32(is_last, struct DBDMA_io),
  678. VMSTATE_INT32(is_dma_out, struct DBDMA_io),
  679. VMSTATE_BOOL(processing, struct DBDMA_io),
  680. VMSTATE_END_OF_LIST()
  681. }
  682. };
  683. static const VMStateDescription vmstate_dbdma_cmd = {
  684. .name = "dbdma_cmd",
  685. .version_id = 0,
  686. .minimum_version_id = 0,
  687. .fields = (VMStateField[]) {
  688. VMSTATE_UINT16(req_count, dbdma_cmd),
  689. VMSTATE_UINT16(command, dbdma_cmd),
  690. VMSTATE_UINT32(phy_addr, dbdma_cmd),
  691. VMSTATE_UINT32(cmd_dep, dbdma_cmd),
  692. VMSTATE_UINT16(res_count, dbdma_cmd),
  693. VMSTATE_UINT16(xfer_status, dbdma_cmd),
  694. VMSTATE_END_OF_LIST()
  695. }
  696. };
  697. static const VMStateDescription vmstate_dbdma_channel = {
  698. .name = "dbdma_channel",
  699. .version_id = 1,
  700. .minimum_version_id = 1,
  701. .fields = (VMStateField[]) {
  702. VMSTATE_UINT32_ARRAY(regs, struct DBDMA_channel, DBDMA_REGS),
  703. VMSTATE_STRUCT(io, struct DBDMA_channel, 0, vmstate_dbdma_io, DBDMA_io),
  704. VMSTATE_STRUCT(current, struct DBDMA_channel, 0, vmstate_dbdma_cmd,
  705. dbdma_cmd),
  706. VMSTATE_END_OF_LIST()
  707. }
  708. };
  709. static const VMStateDescription vmstate_dbdma = {
  710. .name = "dbdma",
  711. .version_id = 3,
  712. .minimum_version_id = 3,
  713. .fields = (VMStateField[]) {
  714. VMSTATE_STRUCT_ARRAY(channels, DBDMAState, DBDMA_CHANNELS, 1,
  715. vmstate_dbdma_channel, DBDMA_channel),
  716. VMSTATE_END_OF_LIST()
  717. }
  718. };
  719. static void mac_dbdma_reset(DeviceState *d)
  720. {
  721. DBDMAState *s = MAC_DBDMA(d);
  722. int i;
  723. for (i = 0; i < DBDMA_CHANNELS; i++) {
  724. memset(s->channels[i].regs, 0, DBDMA_SIZE);
  725. }
  726. }
  727. static void dbdma_unassigned_rw(DBDMA_io *io)
  728. {
  729. DBDMA_channel *ch = io->channel;
  730. dbdma_cmd *current = &ch->current;
  731. uint16_t cmd;
  732. qemu_log_mask(LOG_GUEST_ERROR, "%s: use of unassigned channel %d\n",
  733. __func__, ch->channel);
  734. ch->io.processing = false;
  735. cmd = le16_to_cpu(current->command) & COMMAND_MASK;
  736. if (cmd == OUTPUT_MORE || cmd == OUTPUT_LAST ||
  737. cmd == INPUT_MORE || cmd == INPUT_LAST) {
  738. current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
  739. current->res_count = cpu_to_le16(io->len);
  740. dbdma_cmdptr_save(ch);
  741. }
  742. }
  743. static void dbdma_unassigned_flush(DBDMA_io *io)
  744. {
  745. DBDMA_channel *ch = io->channel;
  746. qemu_log_mask(LOG_GUEST_ERROR, "%s: use of unassigned channel %d\n",
  747. __func__, ch->channel);
  748. }
  749. static void mac_dbdma_init(Object *obj)
  750. {
  751. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  752. DBDMAState *s = MAC_DBDMA(obj);
  753. int i;
  754. for (i = 0; i < DBDMA_CHANNELS; i++) {
  755. DBDMA_channel *ch = &s->channels[i];
  756. ch->rw = dbdma_unassigned_rw;
  757. ch->flush = dbdma_unassigned_flush;
  758. ch->channel = i;
  759. ch->io.channel = ch;
  760. }
  761. memory_region_init_io(&s->mem, obj, &dbdma_ops, s, "dbdma", 0x1000);
  762. sysbus_init_mmio(sbd, &s->mem);
  763. }
  764. static void mac_dbdma_realize(DeviceState *dev, Error **errp)
  765. {
  766. DBDMAState *s = MAC_DBDMA(dev);
  767. s->bh = qemu_bh_new(DBDMA_run_bh, s);
  768. }
  769. static void mac_dbdma_class_init(ObjectClass *oc, void *data)
  770. {
  771. DeviceClass *dc = DEVICE_CLASS(oc);
  772. dc->realize = mac_dbdma_realize;
  773. dc->reset = mac_dbdma_reset;
  774. dc->vmsd = &vmstate_dbdma;
  775. }
  776. static const TypeInfo mac_dbdma_type_info = {
  777. .name = TYPE_MAC_DBDMA,
  778. .parent = TYPE_SYS_BUS_DEVICE,
  779. .instance_size = sizeof(DBDMAState),
  780. .instance_init = mac_dbdma_init,
  781. .class_init = mac_dbdma_class_init
  782. };
  783. static void mac_dbdma_register_types(void)
  784. {
  785. type_register_static(&mac_dbdma_type_info);
  786. }
  787. type_init(mac_dbdma_register_types)