blob: e220f1a92771dc9378bc2f61174701b2e14d709f [file] [log] [blame]
j_mayer3cbee152007-10-28 23:42:18 +00001/*
2 * PowerMac descriptor-based DMA emulation
3 *
4 * Copyright (c) 2005-2007 Fabrice Bellard
5 * Copyright (c) 2007 Jocelyn Mayer
aurel3228ce5ce2009-01-30 20:39:32 +00006 * 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)
j_mayer3cbee152007-10-28 23:42:18 +000020 *
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 */
Markus Armbruster0b8fa322019-05-23 16:35:07 +020039
Peter Maydell0d755902016-01-26 18:16:58 +000040#include "qemu/osdep.h"
Markus Armbruster64552b62019-08-12 07:23:42 +020041#include "hw/irq.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010042#include "hw/ppc/mac_dbdma.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +020043#include "migration/vmstate.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010044#include "qemu/main-loop.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020045#include "qemu/module.h"
Paolo Bonzini03dd0242015-12-15 13:16:16 +010046#include "qemu/log.h"
Mark Cave-Ayland88655882016-06-05 23:36:43 +010047#include "sysemu/dma.h"
j_mayer3cbee152007-10-28 23:42:18 +000048
blueswir1ea026b22008-12-24 09:38:16 +000049/* debug DBDMA */
Mark Cave-Aylandba0b17d2016-07-10 19:08:53 +010050#define DEBUG_DBDMA 0
Mark Cave-Ayland3e49c432016-07-10 19:08:54 +010051#define DEBUG_DBDMA_CHANMASK ((1ull << DBDMA_CHANNELS) - 1)
blueswir1ea026b22008-12-24 09:38:16 +000052
Mark Cave-Aylandba0b17d2016-07-10 19:08:53 +010053#define DBDMA_DPRINTF(fmt, ...) do { \
54 if (DEBUG_DBDMA) { \
55 printf("DBDMA: " fmt , ## __VA_ARGS__); \
56 } \
Eric Blake25627552017-12-01 17:24:32 -060057} while (0)
blueswir1ea026b22008-12-24 09:38:16 +000058
Mark Cave-Ayland3e49c432016-07-10 19:08:54 +010059#define DBDMA_DPRINTFCH(ch, fmt, ...) do { \
60 if (DEBUG_DBDMA) { \
61 if ((1ul << (ch)->channel) & DEBUG_DBDMA_CHANMASK) { \
62 printf("DBDMA[%02x]: " fmt , (ch)->channel, ## __VA_ARGS__); \
63 } \
64 } \
Eric Blake25627552017-12-01 17:24:32 -060065} while (0)
Mark Cave-Ayland3e49c432016-07-10 19:08:54 +010066
aurel3228ce5ce2009-01-30 20:39:32 +000067/*
68 */
j_mayer3cbee152007-10-28 23:42:18 +000069
Alexander Grafd2f0ce22013-06-30 02:22:41 +020070static DBDMAState *dbdma_from_ch(DBDMA_channel *ch)
71{
72 return container_of(ch, DBDMAState, channels[ch->channel]);
73}
74
Mark Cave-Aylandba0b17d2016-07-10 19:08:53 +010075#if DEBUG_DBDMA
Mark Cave-Aylandb7d67812018-06-22 09:00:08 +010076static void dump_dbdma_cmd(DBDMA_channel *ch, dbdma_cmd *cmd)
j_mayer3cbee152007-10-28 23:42:18 +000077{
Mark Cave-Aylandb7d67812018-06-22 09:00:08 +010078 DBDMA_DPRINTFCH(ch, "dbdma_cmd %p\n", cmd);
79 DBDMA_DPRINTFCH(ch, " req_count 0x%04x\n", le16_to_cpu(cmd->req_count));
80 DBDMA_DPRINTFCH(ch, " command 0x%04x\n", le16_to_cpu(cmd->command));
81 DBDMA_DPRINTFCH(ch, " phy_addr 0x%08x\n", le32_to_cpu(cmd->phy_addr));
82 DBDMA_DPRINTFCH(ch, " cmd_dep 0x%08x\n", le32_to_cpu(cmd->cmd_dep));
83 DBDMA_DPRINTFCH(ch, " res_count 0x%04x\n", le16_to_cpu(cmd->res_count));
84 DBDMA_DPRINTFCH(ch, " xfer_status 0x%04x\n",
85 le16_to_cpu(cmd->xfer_status));
aurel3228ce5ce2009-01-30 20:39:32 +000086}
87#else
Mark Cave-Aylandb7d67812018-06-22 09:00:08 +010088static void dump_dbdma_cmd(DBDMA_channel *ch, dbdma_cmd *cmd)
aurel3228ce5ce2009-01-30 20:39:32 +000089{
90}
91#endif
92static void dbdma_cmdptr_load(DBDMA_channel *ch)
93{
Mark Cave-Ayland3e49c432016-07-10 19:08:54 +010094 DBDMA_DPRINTFCH(ch, "dbdma_cmdptr_load 0x%08x\n",
95 ch->regs[DBDMA_CMDPTR_LO]);
Mark Cave-Ayland88655882016-06-05 23:36:43 +010096 dma_memory_read(&address_space_memory, ch->regs[DBDMA_CMDPTR_LO],
97 &ch->current, sizeof(dbdma_cmd));
j_mayer3cbee152007-10-28 23:42:18 +000098}
99
aurel3228ce5ce2009-01-30 20:39:32 +0000100static void dbdma_cmdptr_save(DBDMA_channel *ch)
j_mayer3cbee152007-10-28 23:42:18 +0000101{
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100102 DBDMA_DPRINTFCH(ch, "-> update 0x%08x stat=0x%08x, res=0x%04x\n",
103 ch->regs[DBDMA_CMDPTR_LO],
Mark Cave-Ayland3e49c432016-07-10 19:08:54 +0100104 le16_to_cpu(ch->current.xfer_status),
105 le16_to_cpu(ch->current.res_count));
Mark Cave-Ayland88655882016-06-05 23:36:43 +0100106 dma_memory_write(&address_space_memory, ch->regs[DBDMA_CMDPTR_LO],
107 &ch->current, sizeof(dbdma_cmd));
aurel3228ce5ce2009-01-30 20:39:32 +0000108}
109
110static void kill_channel(DBDMA_channel *ch)
111{
Mark Cave-Ayland3e49c432016-07-10 19:08:54 +0100112 DBDMA_DPRINTFCH(ch, "kill_channel\n");
aurel3228ce5ce2009-01-30 20:39:32 +0000113
Aurelien Jarnoad674e52009-12-24 01:00:41 +0100114 ch->regs[DBDMA_STATUS] |= DEAD;
115 ch->regs[DBDMA_STATUS] &= ~ACTIVE;
aurel3228ce5ce2009-01-30 20:39:32 +0000116
117 qemu_irq_raise(ch->irq);
118}
119
120static void conditional_interrupt(DBDMA_channel *ch)
121{
122 dbdma_cmd *current = &ch->current;
123 uint16_t intr;
124 uint16_t sel_mask, sel_value;
125 uint32_t status;
126 int cond;
127
Mark Cave-Ayland3e49c432016-07-10 19:08:54 +0100128 DBDMA_DPRINTFCH(ch, "%s\n", __func__);
aurel3228ce5ce2009-01-30 20:39:32 +0000129
aurel32b42ec422009-03-03 09:14:10 +0000130 intr = le16_to_cpu(current->command) & INTR_MASK;
aurel3228ce5ce2009-01-30 20:39:32 +0000131
132 switch(intr) {
133 case INTR_NEVER: /* don't interrupt */
134 return;
135 case INTR_ALWAYS: /* always interrupt */
136 qemu_irq_raise(ch->irq);
Mark Cave-Ayland3e49c432016-07-10 19:08:54 +0100137 DBDMA_DPRINTFCH(ch, "%s: raise\n", __func__);
aurel3228ce5ce2009-01-30 20:39:32 +0000138 return;
139 }
140
Aurelien Jarnoad674e52009-12-24 01:00:41 +0100141 status = ch->regs[DBDMA_STATUS] & DEVSTAT;
aurel3228ce5ce2009-01-30 20:39:32 +0000142
Aurelien Jarnoad674e52009-12-24 01:00:41 +0100143 sel_mask = (ch->regs[DBDMA_INTR_SEL] >> 16) & 0x0f;
144 sel_value = ch->regs[DBDMA_INTR_SEL] & 0x0f;
aurel3228ce5ce2009-01-30 20:39:32 +0000145
146 cond = (status & sel_mask) == (sel_value & sel_mask);
147
148 switch(intr) {
149 case INTR_IFSET: /* intr if condition bit is 1 */
Alexander Graf33ce36b2013-06-30 01:23:45 +0200150 if (cond) {
aurel3228ce5ce2009-01-30 20:39:32 +0000151 qemu_irq_raise(ch->irq);
Mark Cave-Ayland3e49c432016-07-10 19:08:54 +0100152 DBDMA_DPRINTFCH(ch, "%s: raise\n", __func__);
Alexander Graf33ce36b2013-06-30 01:23:45 +0200153 }
aurel3228ce5ce2009-01-30 20:39:32 +0000154 return;
155 case INTR_IFCLR: /* intr if condition bit is 0 */
Alexander Graf33ce36b2013-06-30 01:23:45 +0200156 if (!cond) {
aurel3228ce5ce2009-01-30 20:39:32 +0000157 qemu_irq_raise(ch->irq);
Mark Cave-Ayland3e49c432016-07-10 19:08:54 +0100158 DBDMA_DPRINTFCH(ch, "%s: raise\n", __func__);
Alexander Graf33ce36b2013-06-30 01:23:45 +0200159 }
aurel3228ce5ce2009-01-30 20:39:32 +0000160 return;
161 }
162}
163
164static int conditional_wait(DBDMA_channel *ch)
165{
166 dbdma_cmd *current = &ch->current;
167 uint16_t wait;
168 uint16_t sel_mask, sel_value;
169 uint32_t status;
170 int cond;
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100171 int res = 0;
aurel3228ce5ce2009-01-30 20:39:32 +0000172
aurel32b42ec422009-03-03 09:14:10 +0000173 wait = le16_to_cpu(current->command) & WAIT_MASK;
aurel3228ce5ce2009-01-30 20:39:32 +0000174 switch(wait) {
175 case WAIT_NEVER: /* don't wait */
176 return 0;
177 case WAIT_ALWAYS: /* always wait */
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100178 DBDMA_DPRINTFCH(ch, " [WAIT_ALWAYS]\n");
aurel3228ce5ce2009-01-30 20:39:32 +0000179 return 1;
180 }
181
Aurelien Jarnoad674e52009-12-24 01:00:41 +0100182 status = ch->regs[DBDMA_STATUS] & DEVSTAT;
aurel3228ce5ce2009-01-30 20:39:32 +0000183
Aurelien Jarnoad674e52009-12-24 01:00:41 +0100184 sel_mask = (ch->regs[DBDMA_WAIT_SEL] >> 16) & 0x0f;
185 sel_value = ch->regs[DBDMA_WAIT_SEL] & 0x0f;
aurel3228ce5ce2009-01-30 20:39:32 +0000186
187 cond = (status & sel_mask) == (sel_value & sel_mask);
188
189 switch(wait) {
190 case WAIT_IFSET: /* wait if condition bit is 1 */
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100191 if (cond) {
192 res = 1;
193 }
194 DBDMA_DPRINTFCH(ch, " [WAIT_IFSET=%d]\n", res);
195 break;
aurel3228ce5ce2009-01-30 20:39:32 +0000196 case WAIT_IFCLR: /* wait if condition bit is 0 */
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100197 if (!cond) {
198 res = 1;
199 }
200 DBDMA_DPRINTFCH(ch, " [WAIT_IFCLR=%d]\n", res);
201 break;
aurel3228ce5ce2009-01-30 20:39:32 +0000202 }
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100203 return res;
aurel3228ce5ce2009-01-30 20:39:32 +0000204}
205
206static void next(DBDMA_channel *ch)
207{
208 uint32_t cp;
209
Aurelien Jarnoad674e52009-12-24 01:00:41 +0100210 ch->regs[DBDMA_STATUS] &= ~BT;
aurel3228ce5ce2009-01-30 20:39:32 +0000211
Aurelien Jarnoad674e52009-12-24 01:00:41 +0100212 cp = ch->regs[DBDMA_CMDPTR_LO];
213 ch->regs[DBDMA_CMDPTR_LO] = cp + sizeof(dbdma_cmd);
aurel3228ce5ce2009-01-30 20:39:32 +0000214 dbdma_cmdptr_load(ch);
215}
216
217static void branch(DBDMA_channel *ch)
218{
219 dbdma_cmd *current = &ch->current;
220
Mark Cave-Ayland3f0d4122016-07-10 19:08:55 +0100221 ch->regs[DBDMA_CMDPTR_LO] = le32_to_cpu(current->cmd_dep);
Aurelien Jarnoad674e52009-12-24 01:00:41 +0100222 ch->regs[DBDMA_STATUS] |= BT;
aurel3228ce5ce2009-01-30 20:39:32 +0000223 dbdma_cmdptr_load(ch);
224}
225
226static void conditional_branch(DBDMA_channel *ch)
227{
228 dbdma_cmd *current = &ch->current;
229 uint16_t br;
230 uint16_t sel_mask, sel_value;
231 uint32_t status;
232 int cond;
233
aurel3228ce5ce2009-01-30 20:39:32 +0000234 /* check if we must branch */
235
aurel32b42ec422009-03-03 09:14:10 +0000236 br = le16_to_cpu(current->command) & BR_MASK;
aurel3228ce5ce2009-01-30 20:39:32 +0000237
238 switch(br) {
239 case BR_NEVER: /* don't branch */
240 next(ch);
241 return;
242 case BR_ALWAYS: /* always branch */
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100243 DBDMA_DPRINTFCH(ch, " [BR_ALWAYS]\n");
aurel3228ce5ce2009-01-30 20:39:32 +0000244 branch(ch);
245 return;
246 }
247
Aurelien Jarnoad674e52009-12-24 01:00:41 +0100248 status = ch->regs[DBDMA_STATUS] & DEVSTAT;
aurel3228ce5ce2009-01-30 20:39:32 +0000249
Aurelien Jarnoad674e52009-12-24 01:00:41 +0100250 sel_mask = (ch->regs[DBDMA_BRANCH_SEL] >> 16) & 0x0f;
251 sel_value = ch->regs[DBDMA_BRANCH_SEL] & 0x0f;
aurel3228ce5ce2009-01-30 20:39:32 +0000252
253 cond = (status & sel_mask) == (sel_value & sel_mask);
254
255 switch(br) {
256 case BR_IFSET: /* branch if condition bit is 1 */
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100257 if (cond) {
258 DBDMA_DPRINTFCH(ch, " [BR_IFSET = 1]\n");
aurel3228ce5ce2009-01-30 20:39:32 +0000259 branch(ch);
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100260 } else {
261 DBDMA_DPRINTFCH(ch, " [BR_IFSET = 0]\n");
aurel3228ce5ce2009-01-30 20:39:32 +0000262 next(ch);
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100263 }
aurel3228ce5ce2009-01-30 20:39:32 +0000264 return;
265 case BR_IFCLR: /* branch if condition bit is 0 */
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100266 if (!cond) {
267 DBDMA_DPRINTFCH(ch, " [BR_IFCLR = 1]\n");
aurel3228ce5ce2009-01-30 20:39:32 +0000268 branch(ch);
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100269 } else {
270 DBDMA_DPRINTFCH(ch, " [BR_IFCLR = 0]\n");
aurel3228ce5ce2009-01-30 20:39:32 +0000271 next(ch);
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100272 }
aurel3228ce5ce2009-01-30 20:39:32 +0000273 return;
274 }
275}
276
aurel32b42ec422009-03-03 09:14:10 +0000277static void channel_run(DBDMA_channel *ch);
278
279static void dbdma_end(DBDMA_io *io)
aurel3228ce5ce2009-01-30 20:39:32 +0000280{
281 DBDMA_channel *ch = io->channel;
282 dbdma_cmd *current = &ch->current;
283
Mark Cave-Ayland3e49c432016-07-10 19:08:54 +0100284 DBDMA_DPRINTFCH(ch, "%s\n", __func__);
Alexander Graf33ce36b2013-06-30 01:23:45 +0200285
aurel32b42ec422009-03-03 09:14:10 +0000286 if (conditional_wait(ch))
287 goto wait;
aurel3228ce5ce2009-01-30 20:39:32 +0000288
Aurelien Jarnoad674e52009-12-24 01:00:41 +0100289 current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
290 current->res_count = cpu_to_le16(io->len);
aurel32b42ec422009-03-03 09:14:10 +0000291 dbdma_cmdptr_save(ch);
aurel32862c9282009-03-04 07:20:40 +0000292 if (io->is_last)
Aurelien Jarnoad674e52009-12-24 01:00:41 +0100293 ch->regs[DBDMA_STATUS] &= ~FLUSH;
aurel3228ce5ce2009-01-30 20:39:32 +0000294
aurel32b42ec422009-03-03 09:14:10 +0000295 conditional_interrupt(ch);
296 conditional_branch(ch);
297
298wait:
Alexander Graf03ee3b12013-06-30 02:47:20 +0200299 /* Indicate that we're ready for a new DMA round */
300 ch->io.processing = false;
301
Aurelien Jarnoad674e52009-12-24 01:00:41 +0100302 if ((ch->regs[DBDMA_STATUS] & RUN) &&
303 (ch->regs[DBDMA_STATUS] & ACTIVE))
aurel32b42ec422009-03-03 09:14:10 +0000304 channel_run(ch);
aurel3228ce5ce2009-01-30 20:39:32 +0000305}
306
aurel32b42ec422009-03-03 09:14:10 +0000307static void start_output(DBDMA_channel *ch, int key, uint32_t addr,
aurel3228ce5ce2009-01-30 20:39:32 +0000308 uint16_t req_count, int is_last)
309{
Mark Cave-Ayland3e49c432016-07-10 19:08:54 +0100310 DBDMA_DPRINTFCH(ch, "start_output\n");
aurel3228ce5ce2009-01-30 20:39:32 +0000311
312 /* KEY_REGS, KEY_DEVICE and KEY_STREAM
313 * are not implemented in the mac-io chip
314 */
315
Mark Cave-Ayland3e49c432016-07-10 19:08:54 +0100316 DBDMA_DPRINTFCH(ch, "addr 0x%x key 0x%x\n", addr, key);
aurel3228ce5ce2009-01-30 20:39:32 +0000317 if (!addr || key > KEY_STREAM3) {
318 kill_channel(ch);
aurel32b42ec422009-03-03 09:14:10 +0000319 return;
aurel3228ce5ce2009-01-30 20:39:32 +0000320 }
321
aurel32b42ec422009-03-03 09:14:10 +0000322 ch->io.addr = addr;
aurel3228ce5ce2009-01-30 20:39:32 +0000323 ch->io.len = req_count;
324 ch->io.is_last = is_last;
aurel32b42ec422009-03-03 09:14:10 +0000325 ch->io.dma_end = dbdma_end;
326 ch->io.is_dma_out = 1;
Alexander Graf03ee3b12013-06-30 02:47:20 +0200327 ch->io.processing = true;
Alexander Grafa9ceb762010-02-09 17:37:07 +0100328 if (ch->rw) {
329 ch->rw(&ch->io);
330 }
aurel3228ce5ce2009-01-30 20:39:32 +0000331}
332
aurel32b42ec422009-03-03 09:14:10 +0000333static void start_input(DBDMA_channel *ch, int key, uint32_t addr,
aurel3228ce5ce2009-01-30 20:39:32 +0000334 uint16_t req_count, int is_last)
335{
Mark Cave-Ayland3e49c432016-07-10 19:08:54 +0100336 DBDMA_DPRINTFCH(ch, "start_input\n");
aurel3228ce5ce2009-01-30 20:39:32 +0000337
338 /* KEY_REGS, KEY_DEVICE and KEY_STREAM
339 * are not implemented in the mac-io chip
340 */
341
Mark Cave-Ayland3e49c432016-07-10 19:08:54 +0100342 DBDMA_DPRINTFCH(ch, "addr 0x%x key 0x%x\n", addr, key);
aurel3228ce5ce2009-01-30 20:39:32 +0000343 if (!addr || key > KEY_STREAM3) {
344 kill_channel(ch);
aurel32b42ec422009-03-03 09:14:10 +0000345 return;
aurel3228ce5ce2009-01-30 20:39:32 +0000346 }
347
aurel32b42ec422009-03-03 09:14:10 +0000348 ch->io.addr = addr;
aurel3228ce5ce2009-01-30 20:39:32 +0000349 ch->io.len = req_count;
350 ch->io.is_last = is_last;
aurel32b42ec422009-03-03 09:14:10 +0000351 ch->io.dma_end = dbdma_end;
352 ch->io.is_dma_out = 0;
Alexander Graf03ee3b12013-06-30 02:47:20 +0200353 ch->io.processing = true;
Alexander Grafa9ceb762010-02-09 17:37:07 +0100354 if (ch->rw) {
355 ch->rw(&ch->io);
356 }
aurel3228ce5ce2009-01-30 20:39:32 +0000357}
358
aurel32b42ec422009-03-03 09:14:10 +0000359static void load_word(DBDMA_channel *ch, int key, uint32_t addr,
aurel3228ce5ce2009-01-30 20:39:32 +0000360 uint16_t len)
361{
362 dbdma_cmd *current = &ch->current;
aurel3228ce5ce2009-01-30 20:39:32 +0000363
Mark Cave-Aylande12f50b2016-07-10 19:08:56 +0100364 DBDMA_DPRINTFCH(ch, "load_word %d bytes, addr=%08x\n", len, addr);
aurel3228ce5ce2009-01-30 20:39:32 +0000365
366 /* only implements KEY_SYSTEM */
367
368 if (key != KEY_SYSTEM) {
369 printf("DBDMA: LOAD_WORD, unimplemented key %x\n", key);
370 kill_channel(ch);
aurel32b42ec422009-03-03 09:14:10 +0000371 return;
aurel3228ce5ce2009-01-30 20:39:32 +0000372 }
373
Mark Cave-Aylande12f50b2016-07-10 19:08:56 +0100374 dma_memory_read(&address_space_memory, addr, &current->cmd_dep, len);
aurel3228ce5ce2009-01-30 20:39:32 +0000375
376 if (conditional_wait(ch))
aurel32b42ec422009-03-03 09:14:10 +0000377 goto wait;
aurel3228ce5ce2009-01-30 20:39:32 +0000378
Aurelien Jarnoad674e52009-12-24 01:00:41 +0100379 current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
aurel3228ce5ce2009-01-30 20:39:32 +0000380 dbdma_cmdptr_save(ch);
Aurelien Jarnoad674e52009-12-24 01:00:41 +0100381 ch->regs[DBDMA_STATUS] &= ~FLUSH;
aurel3228ce5ce2009-01-30 20:39:32 +0000382
383 conditional_interrupt(ch);
384 next(ch);
385
aurel32b42ec422009-03-03 09:14:10 +0000386wait:
Alexander Grafd2f0ce22013-06-30 02:22:41 +0200387 DBDMA_kick(dbdma_from_ch(ch));
aurel3228ce5ce2009-01-30 20:39:32 +0000388}
389
aurel32b42ec422009-03-03 09:14:10 +0000390static void store_word(DBDMA_channel *ch, int key, uint32_t addr,
aurel3228ce5ce2009-01-30 20:39:32 +0000391 uint16_t len)
392{
393 dbdma_cmd *current = &ch->current;
aurel3228ce5ce2009-01-30 20:39:32 +0000394
Mark Cave-Aylande12f50b2016-07-10 19:08:56 +0100395 DBDMA_DPRINTFCH(ch, "store_word %d bytes, addr=%08x pa=%x\n",
396 len, addr, le32_to_cpu(current->cmd_dep));
aurel3228ce5ce2009-01-30 20:39:32 +0000397
398 /* only implements KEY_SYSTEM */
399
400 if (key != KEY_SYSTEM) {
401 printf("DBDMA: STORE_WORD, unimplemented key %x\n", key);
402 kill_channel(ch);
aurel32b42ec422009-03-03 09:14:10 +0000403 return;
aurel3228ce5ce2009-01-30 20:39:32 +0000404 }
405
Mark Cave-Aylande12f50b2016-07-10 19:08:56 +0100406 dma_memory_write(&address_space_memory, addr, &current->cmd_dep, len);
aurel3228ce5ce2009-01-30 20:39:32 +0000407
408 if (conditional_wait(ch))
aurel32b42ec422009-03-03 09:14:10 +0000409 goto wait;
aurel3228ce5ce2009-01-30 20:39:32 +0000410
Aurelien Jarnoad674e52009-12-24 01:00:41 +0100411 current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
aurel3228ce5ce2009-01-30 20:39:32 +0000412 dbdma_cmdptr_save(ch);
Aurelien Jarnoad674e52009-12-24 01:00:41 +0100413 ch->regs[DBDMA_STATUS] &= ~FLUSH;
aurel3228ce5ce2009-01-30 20:39:32 +0000414
415 conditional_interrupt(ch);
416 next(ch);
417
aurel32b42ec422009-03-03 09:14:10 +0000418wait:
Alexander Grafd2f0ce22013-06-30 02:22:41 +0200419 DBDMA_kick(dbdma_from_ch(ch));
aurel3228ce5ce2009-01-30 20:39:32 +0000420}
421
aurel32b42ec422009-03-03 09:14:10 +0000422static void nop(DBDMA_channel *ch)
aurel3228ce5ce2009-01-30 20:39:32 +0000423{
424 dbdma_cmd *current = &ch->current;
425
426 if (conditional_wait(ch))
aurel32b42ec422009-03-03 09:14:10 +0000427 goto wait;
aurel3228ce5ce2009-01-30 20:39:32 +0000428
Aurelien Jarnoad674e52009-12-24 01:00:41 +0100429 current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
aurel3228ce5ce2009-01-30 20:39:32 +0000430 dbdma_cmdptr_save(ch);
431
432 conditional_interrupt(ch);
433 conditional_branch(ch);
434
aurel32b42ec422009-03-03 09:14:10 +0000435wait:
Alexander Grafd2f0ce22013-06-30 02:22:41 +0200436 DBDMA_kick(dbdma_from_ch(ch));
aurel3228ce5ce2009-01-30 20:39:32 +0000437}
438
aurel32b42ec422009-03-03 09:14:10 +0000439static void stop(DBDMA_channel *ch)
aurel3228ce5ce2009-01-30 20:39:32 +0000440{
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100441 ch->regs[DBDMA_STATUS] &= ~(ACTIVE);
aurel3228ce5ce2009-01-30 20:39:32 +0000442
443 /* the stop command does not increment command pointer */
aurel3228ce5ce2009-01-30 20:39:32 +0000444}
445
aurel32b42ec422009-03-03 09:14:10 +0000446static void channel_run(DBDMA_channel *ch)
aurel3228ce5ce2009-01-30 20:39:32 +0000447{
448 dbdma_cmd *current = &ch->current;
449 uint16_t cmd, key;
450 uint16_t req_count;
451 uint32_t phy_addr;
452
Mark Cave-Ayland3e49c432016-07-10 19:08:54 +0100453 DBDMA_DPRINTFCH(ch, "channel_run\n");
Mark Cave-Aylandb7d67812018-06-22 09:00:08 +0100454 dump_dbdma_cmd(ch, current);
aurel3228ce5ce2009-01-30 20:39:32 +0000455
456 /* clear WAKE flag at command fetch */
457
Aurelien Jarnoad674e52009-12-24 01:00:41 +0100458 ch->regs[DBDMA_STATUS] &= ~WAKE;
aurel3228ce5ce2009-01-30 20:39:32 +0000459
460 cmd = le16_to_cpu(current->command) & COMMAND_MASK;
461
462 switch (cmd) {
463 case DBDMA_NOP:
aurel32b42ec422009-03-03 09:14:10 +0000464 nop(ch);
Alexander Graf9e232422013-06-30 01:45:22 +0200465 return;
aurel3228ce5ce2009-01-30 20:39:32 +0000466
467 case DBDMA_STOP:
aurel32b42ec422009-03-03 09:14:10 +0000468 stop(ch);
Alexander Graf9e232422013-06-30 01:45:22 +0200469 return;
aurel3228ce5ce2009-01-30 20:39:32 +0000470 }
471
472 key = le16_to_cpu(current->command) & 0x0700;
473 req_count = le16_to_cpu(current->req_count);
474 phy_addr = le32_to_cpu(current->phy_addr);
475
476 if (key == KEY_STREAM4) {
477 printf("command %x, invalid key 4\n", cmd);
478 kill_channel(ch);
aurel32b42ec422009-03-03 09:14:10 +0000479 return;
aurel3228ce5ce2009-01-30 20:39:32 +0000480 }
481
482 switch (cmd) {
483 case OUTPUT_MORE:
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100484 DBDMA_DPRINTFCH(ch, "* OUTPUT_MORE *\n");
aurel32b42ec422009-03-03 09:14:10 +0000485 start_output(ch, key, phy_addr, req_count, 0);
Alexander Graf9e232422013-06-30 01:45:22 +0200486 return;
aurel3228ce5ce2009-01-30 20:39:32 +0000487
488 case OUTPUT_LAST:
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100489 DBDMA_DPRINTFCH(ch, "* OUTPUT_LAST *\n");
aurel32b42ec422009-03-03 09:14:10 +0000490 start_output(ch, key, phy_addr, req_count, 1);
Alexander Graf9e232422013-06-30 01:45:22 +0200491 return;
aurel3228ce5ce2009-01-30 20:39:32 +0000492
493 case INPUT_MORE:
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100494 DBDMA_DPRINTFCH(ch, "* INPUT_MORE *\n");
aurel32b42ec422009-03-03 09:14:10 +0000495 start_input(ch, key, phy_addr, req_count, 0);
Alexander Graf9e232422013-06-30 01:45:22 +0200496 return;
aurel3228ce5ce2009-01-30 20:39:32 +0000497
498 case INPUT_LAST:
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100499 DBDMA_DPRINTFCH(ch, "* INPUT_LAST *\n");
aurel32b42ec422009-03-03 09:14:10 +0000500 start_input(ch, key, phy_addr, req_count, 1);
Alexander Graf9e232422013-06-30 01:45:22 +0200501 return;
aurel3228ce5ce2009-01-30 20:39:32 +0000502 }
503
504 if (key < KEY_REGS) {
505 printf("command %x, invalid key %x\n", cmd, key);
506 key = KEY_SYSTEM;
507 }
508
509 /* for LOAD_WORD and STORE_WORD, req_count is on 3 bits
510 * and BRANCH is invalid
511 */
512
513 req_count = req_count & 0x0007;
514 if (req_count & 0x4) {
515 req_count = 4;
516 phy_addr &= ~3;
517 } else if (req_count & 0x2) {
518 req_count = 2;
519 phy_addr &= ~1;
520 } else
521 req_count = 1;
522
523 switch (cmd) {
524 case LOAD_WORD:
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100525 DBDMA_DPRINTFCH(ch, "* LOAD_WORD *\n");
aurel32b42ec422009-03-03 09:14:10 +0000526 load_word(ch, key, phy_addr, req_count);
Alexander Graf9e232422013-06-30 01:45:22 +0200527 return;
aurel3228ce5ce2009-01-30 20:39:32 +0000528
529 case STORE_WORD:
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100530 DBDMA_DPRINTFCH(ch, "* STORE_WORD *\n");
aurel32b42ec422009-03-03 09:14:10 +0000531 store_word(ch, key, phy_addr, req_count);
Alexander Graf9e232422013-06-30 01:45:22 +0200532 return;
aurel3228ce5ce2009-01-30 20:39:32 +0000533 }
aurel3228ce5ce2009-01-30 20:39:32 +0000534}
535
Juan Quintelac20df142010-12-03 00:04:02 +0100536static void DBDMA_run(DBDMAState *s)
aurel3228ce5ce2009-01-30 20:39:32 +0000537{
538 int channel;
aurel3228ce5ce2009-01-30 20:39:32 +0000539
Juan Quintelac20df142010-12-03 00:04:02 +0100540 for (channel = 0; channel < DBDMA_CHANNELS; channel++) {
541 DBDMA_channel *ch = &s->channels[channel];
542 uint32_t status = ch->regs[DBDMA_STATUS];
Alexander Graf03ee3b12013-06-30 02:47:20 +0200543 if (!ch->io.processing && (status & RUN) && (status & ACTIVE)) {
Juan Quintelac20df142010-12-03 00:04:02 +0100544 channel_run(ch);
545 }
aurel3228ce5ce2009-01-30 20:39:32 +0000546 }
aurel3228ce5ce2009-01-30 20:39:32 +0000547}
548
549static void DBDMA_run_bh(void *opaque)
550{
Juan Quintelac20df142010-12-03 00:04:02 +0100551 DBDMAState *s = opaque;
aurel3228ce5ce2009-01-30 20:39:32 +0000552
Mark Cave-Ayland3e49c432016-07-10 19:08:54 +0100553 DBDMA_DPRINTF("-> DBDMA_run_bh\n");
Juan Quintelac20df142010-12-03 00:04:02 +0100554 DBDMA_run(s);
Mark Cave-Ayland3e49c432016-07-10 19:08:54 +0100555 DBDMA_DPRINTF("<- DBDMA_run_bh\n");
aurel3228ce5ce2009-01-30 20:39:32 +0000556}
557
Alexander Grafd1e562d2013-06-30 02:18:54 +0200558void DBDMA_kick(DBDMAState *dbdma)
559{
Alexander Grafd2f0ce22013-06-30 02:22:41 +0200560 qemu_bh_schedule(dbdma->bh);
Alexander Grafd1e562d2013-06-30 02:18:54 +0200561}
562
aurel3228ce5ce2009-01-30 20:39:32 +0000563void DBDMA_register_channel(void *dbdma, int nchan, qemu_irq irq,
aurel32862c9282009-03-04 07:20:40 +0000564 DBDMA_rw rw, DBDMA_flush flush,
aurel3228ce5ce2009-01-30 20:39:32 +0000565 void *opaque)
566{
Juan Quintelac20df142010-12-03 00:04:02 +0100567 DBDMAState *s = dbdma;
568 DBDMA_channel *ch = &s->channels[nchan];
aurel3228ce5ce2009-01-30 20:39:32 +0000569
Mark Cave-Ayland3e49c432016-07-10 19:08:54 +0100570 DBDMA_DPRINTFCH(ch, "DBDMA_register_channel 0x%x\n", nchan);
aurel3228ce5ce2009-01-30 20:39:32 +0000571
Hervé Poussineau2d7d06d2016-02-25 13:02:11 +0100572 assert(rw);
573 assert(flush);
574
aurel3228ce5ce2009-01-30 20:39:32 +0000575 ch->irq = irq;
aurel32b42ec422009-03-03 09:14:10 +0000576 ch->rw = rw;
aurel32862c9282009-03-04 07:20:40 +0000577 ch->flush = flush;
aurel3228ce5ce2009-01-30 20:39:32 +0000578 ch->io.opaque = opaque;
aurel3228ce5ce2009-01-30 20:39:32 +0000579}
580
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100581static void dbdma_control_write(DBDMA_channel *ch)
aurel3228ce5ce2009-01-30 20:39:32 +0000582{
583 uint16_t mask, value;
584 uint32_t status;
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100585 bool do_flush = false;
aurel3228ce5ce2009-01-30 20:39:32 +0000586
Aurelien Jarnoad674e52009-12-24 01:00:41 +0100587 mask = (ch->regs[DBDMA_CONTROL] >> 16) & 0xffff;
588 value = ch->regs[DBDMA_CONTROL] & 0xffff;
aurel3228ce5ce2009-01-30 20:39:32 +0000589
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100590 /* This is the status register which we'll update
591 * appropriately and store back
592 */
Aurelien Jarnoad674e52009-12-24 01:00:41 +0100593 status = ch->regs[DBDMA_STATUS];
aurel3228ce5ce2009-01-30 20:39:32 +0000594
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100595 /* RUN and PAUSE are bits under SW control only
596 * FLUSH and WAKE are set by SW and cleared by HW
597 * DEAD, ACTIVE and BT are only under HW control
598 *
599 * We handle ACTIVE separately at the end of the
600 * logic to ensure all cases are covered.
601 */
aurel3228ce5ce2009-01-30 20:39:32 +0000602
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100603 /* Setting RUN will tentatively activate the channel
604 */
605 if ((mask & RUN) && (value & RUN)) {
606 status |= RUN;
607 DBDMA_DPRINTFCH(ch, " Setting RUN !\n");
aurel3228ce5ce2009-01-30 20:39:32 +0000608 }
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100609
610 /* Clearing RUN 1->0 will stop the channel */
611 if ((mask & RUN) && !(value & RUN)) {
612 /* This has the side effect of clearing the DEAD bit */
613 status &= ~(DEAD | RUN);
614 DBDMA_DPRINTFCH(ch, " Clearing RUN !\n");
615 }
616
617 /* Setting WAKE wakes up an idle channel if it's running
618 *
619 * Note: The doc doesn't say so but assume that only works
620 * on a channel whose RUN bit is set.
621 *
622 * We set WAKE in status, it's not terribly useful as it will
623 * be cleared on the next command fetch but it seems to mimmic
624 * the HW behaviour and is useful for the way we handle
625 * ACTIVE further down.
626 */
627 if ((mask & WAKE) && (value & WAKE) && (status & RUN)) {
628 status |= WAKE;
629 DBDMA_DPRINTFCH(ch, " Setting WAKE !\n");
630 }
631
632 /* PAUSE being set will deactivate (or prevent activation)
633 * of the channel. We just copy it over for now, ACTIVE will
634 * be re-evaluated later.
635 */
636 if (mask & PAUSE) {
637 status = (status & ~PAUSE) | (value & PAUSE);
638 DBDMA_DPRINTFCH(ch, " %sing PAUSE !\n",
639 (value & PAUSE) ? "sett" : "clear");
640 }
641
642 /* FLUSH is its own thing */
643 if ((mask & FLUSH) && (value & FLUSH)) {
644 DBDMA_DPRINTFCH(ch, " Setting FLUSH !\n");
645 /* We set flush directly in the status register, we do *NOT*
646 * set it in "status" so that it gets naturally cleared when
647 * we update the status register further down. That way it
648 * will be set only during the HW flush operation so it is
649 * visible to any completions happening during that time.
650 */
651 ch->regs[DBDMA_STATUS] |= FLUSH;
652 do_flush = true;
653 }
654
655 /* If either RUN or PAUSE is clear, so should ACTIVE be,
656 * otherwise, ACTIVE will be set if we modified RUN, PAUSE or
657 * set WAKE. That means that PAUSE was just cleared, RUN was
658 * just set or WAKE was just set.
659 */
660 if ((status & PAUSE) || !(status & RUN)) {
aurel3228ce5ce2009-01-30 20:39:32 +0000661 status &= ~ACTIVE;
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100662 DBDMA_DPRINTFCH(ch, " -> ACTIVE down !\n");
663
664 /* We stopped processing, we want the underlying HW command
665 * to complete *before* we clear the ACTIVE bit. Otherwise
666 * we can get into a situation where the command status will
667 * have RUN or ACTIVE not set which is going to confuse the
668 * MacOS driver.
669 */
670 do_flush = true;
671 } else if (mask & (RUN | PAUSE)) {
672 status |= ACTIVE;
673 DBDMA_DPRINTFCH(ch, " -> ACTIVE up !\n");
674 } else if ((mask & WAKE) && (value & WAKE)) {
675 status |= ACTIVE;
676 DBDMA_DPRINTFCH(ch, " -> ACTIVE up !\n");
Mark Cave-Ayland1cde7322015-08-23 11:50:55 +0100677 }
678
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100679 DBDMA_DPRINTFCH(ch, " new status=0x%08x\n", status);
680
681 /* If we need to flush the underlying HW, do it now, this happens
682 * both on FLUSH commands and when stopping the channel for safety.
683 */
684 if (do_flush && ch->flush) {
Mark Cave-Ayland1cde7322015-08-23 11:50:55 +0100685 ch->flush(&ch->io);
aurel3228ce5ce2009-01-30 20:39:32 +0000686 }
687
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100688 /* Finally update the status register image */
Aurelien Jarnoad674e52009-12-24 01:00:41 +0100689 ch->regs[DBDMA_STATUS] = status;
aurel3228ce5ce2009-01-30 20:39:32 +0000690
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100691 /* If active, make sure the BH gets to run */
Alexander Grafd2f0ce22013-06-30 02:22:41 +0200692 if (status & ACTIVE) {
693 DBDMA_kick(dbdma_from_ch(ch));
694 }
j_mayer3cbee152007-10-28 23:42:18 +0000695}
696
Avi Kivitya8170e52012-10-23 12:30:10 +0200697static void dbdma_write(void *opaque, hwaddr addr,
Avi Kivity23c5e4c2011-08-08 16:09:17 +0300698 uint64_t value, unsigned size)
j_mayer3cbee152007-10-28 23:42:18 +0000699{
aurel3228ce5ce2009-01-30 20:39:32 +0000700 int channel = addr >> DBDMA_CHANNEL_SHIFT;
Juan Quintelac20df142010-12-03 00:04:02 +0100701 DBDMAState *s = opaque;
702 DBDMA_channel *ch = &s->channels[channel];
aurel3228ce5ce2009-01-30 20:39:32 +0000703 int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2;
704
Mark Cave-Ayland3e49c432016-07-10 19:08:54 +0100705 DBDMA_DPRINTFCH(ch, "writel 0x" TARGET_FMT_plx " <= 0x%08"PRIx64"\n",
706 addr, value);
707 DBDMA_DPRINTFCH(ch, "channel 0x%x reg 0x%x\n",
708 (uint32_t)addr >> DBDMA_CHANNEL_SHIFT, reg);
j_mayer3cbee152007-10-28 23:42:18 +0000709
Alexander Graf7eaba822013-06-30 01:53:51 +0200710 /* cmdptr cannot be modified if channel is ACTIVE */
j_mayer3cbee152007-10-28 23:42:18 +0000711
Alexander Graf7eaba822013-06-30 01:53:51 +0200712 if (reg == DBDMA_CMDPTR_LO && (ch->regs[DBDMA_STATUS] & ACTIVE)) {
Alexander Graf9e232422013-06-30 01:45:22 +0200713 return;
Alexander Graf7eaba822013-06-30 01:53:51 +0200714 }
j_mayer3cbee152007-10-28 23:42:18 +0000715
aurel3228ce5ce2009-01-30 20:39:32 +0000716 ch->regs[reg] = value;
blueswir1ea026b22008-12-24 09:38:16 +0000717
aurel3228ce5ce2009-01-30 20:39:32 +0000718 switch(reg) {
719 case DBDMA_CONTROL:
720 dbdma_control_write(ch);
721 break;
722 case DBDMA_CMDPTR_LO:
723 /* 16-byte aligned */
Aurelien Jarnoad674e52009-12-24 01:00:41 +0100724 ch->regs[DBDMA_CMDPTR_LO] &= ~0xf;
aurel3228ce5ce2009-01-30 20:39:32 +0000725 dbdma_cmdptr_load(ch);
726 break;
727 case DBDMA_STATUS:
728 case DBDMA_INTR_SEL:
729 case DBDMA_BRANCH_SEL:
730 case DBDMA_WAIT_SEL:
731 /* nothing to do */
732 break;
733 case DBDMA_XFER_MODE:
734 case DBDMA_CMDPTR_HI:
735 case DBDMA_DATA2PTR_HI:
736 case DBDMA_DATA2PTR_LO:
737 case DBDMA_ADDRESS_HI:
738 case DBDMA_BRANCH_ADDR_HI:
739 case DBDMA_RES1:
740 case DBDMA_RES2:
741 case DBDMA_RES3:
742 case DBDMA_RES4:
743 /* unused */
744 break;
745 }
j_mayer3cbee152007-10-28 23:42:18 +0000746}
747
Avi Kivitya8170e52012-10-23 12:30:10 +0200748static uint64_t dbdma_read(void *opaque, hwaddr addr,
Avi Kivity23c5e4c2011-08-08 16:09:17 +0300749 unsigned size)
j_mayer3cbee152007-10-28 23:42:18 +0000750{
aurel3228ce5ce2009-01-30 20:39:32 +0000751 uint32_t value;
752 int channel = addr >> DBDMA_CHANNEL_SHIFT;
Juan Quintelac20df142010-12-03 00:04:02 +0100753 DBDMAState *s = opaque;
754 DBDMA_channel *ch = &s->channels[channel];
aurel3228ce5ce2009-01-30 20:39:32 +0000755 int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2;
blueswir1ea026b22008-12-24 09:38:16 +0000756
aurel3228ce5ce2009-01-30 20:39:32 +0000757 value = ch->regs[reg];
758
aurel3228ce5ce2009-01-30 20:39:32 +0000759 switch(reg) {
760 case DBDMA_CONTROL:
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100761 value = ch->regs[DBDMA_STATUS];
aurel3228ce5ce2009-01-30 20:39:32 +0000762 break;
763 case DBDMA_STATUS:
764 case DBDMA_CMDPTR_LO:
765 case DBDMA_INTR_SEL:
766 case DBDMA_BRANCH_SEL:
767 case DBDMA_WAIT_SEL:
768 /* nothing to do */
769 break;
770 case DBDMA_XFER_MODE:
771 case DBDMA_CMDPTR_HI:
772 case DBDMA_DATA2PTR_HI:
773 case DBDMA_DATA2PTR_LO:
774 case DBDMA_ADDRESS_HI:
775 case DBDMA_BRANCH_ADDR_HI:
776 /* unused */
777 value = 0;
778 break;
779 case DBDMA_RES1:
780 case DBDMA_RES2:
781 case DBDMA_RES3:
782 case DBDMA_RES4:
783 /* reserved */
784 break;
785 }
786
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100787 DBDMA_DPRINTFCH(ch, "readl 0x" TARGET_FMT_plx " => 0x%08x\n", addr, value);
788 DBDMA_DPRINTFCH(ch, "channel 0x%x reg 0x%x\n",
789 (uint32_t)addr >> DBDMA_CHANNEL_SHIFT, reg);
790
aurel3228ce5ce2009-01-30 20:39:32 +0000791 return value;
j_mayer3cbee152007-10-28 23:42:18 +0000792}
793
Avi Kivity23c5e4c2011-08-08 16:09:17 +0300794static const MemoryRegionOps dbdma_ops = {
795 .read = dbdma_read,
796 .write = dbdma_write,
797 .endianness = DEVICE_LITTLE_ENDIAN,
798 .valid = {
799 .min_access_size = 4,
800 .max_access_size = 4,
801 },
j_mayer3cbee152007-10-28 23:42:18 +0000802};
803
Mark Cave-Ayland627be2f2016-01-06 20:37:25 +0000804static const VMStateDescription vmstate_dbdma_io = {
805 .name = "dbdma_io",
Juan Quintelada26fdc2010-12-03 00:07:26 +0100806 .version_id = 0,
807 .minimum_version_id = 0,
Juan Quintela35d08452014-04-16 16:01:33 +0200808 .fields = (VMStateField[]) {
Mark Cave-Ayland627be2f2016-01-06 20:37:25 +0000809 VMSTATE_UINT64(addr, struct DBDMA_io),
810 VMSTATE_INT32(len, struct DBDMA_io),
811 VMSTATE_INT32(is_last, struct DBDMA_io),
812 VMSTATE_INT32(is_dma_out, struct DBDMA_io),
813 VMSTATE_BOOL(processing, struct DBDMA_io),
814 VMSTATE_END_OF_LIST()
815 }
816};
817
818static const VMStateDescription vmstate_dbdma_cmd = {
819 .name = "dbdma_cmd",
820 .version_id = 0,
821 .minimum_version_id = 0,
822 .fields = (VMStateField[]) {
823 VMSTATE_UINT16(req_count, dbdma_cmd),
824 VMSTATE_UINT16(command, dbdma_cmd),
825 VMSTATE_UINT32(phy_addr, dbdma_cmd),
826 VMSTATE_UINT32(cmd_dep, dbdma_cmd),
827 VMSTATE_UINT16(res_count, dbdma_cmd),
828 VMSTATE_UINT16(xfer_status, dbdma_cmd),
829 VMSTATE_END_OF_LIST()
830 }
831};
832
833static const VMStateDescription vmstate_dbdma_channel = {
834 .name = "dbdma_channel",
835 .version_id = 1,
836 .minimum_version_id = 1,
837 .fields = (VMStateField[]) {
Juan Quintelada26fdc2010-12-03 00:07:26 +0100838 VMSTATE_UINT32_ARRAY(regs, struct DBDMA_channel, DBDMA_REGS),
Mark Cave-Ayland627be2f2016-01-06 20:37:25 +0000839 VMSTATE_STRUCT(io, struct DBDMA_channel, 0, vmstate_dbdma_io, DBDMA_io),
840 VMSTATE_STRUCT(current, struct DBDMA_channel, 0, vmstate_dbdma_cmd,
841 dbdma_cmd),
Juan Quintelada26fdc2010-12-03 00:07:26 +0100842 VMSTATE_END_OF_LIST()
843 }
844};
aurel3228ce5ce2009-01-30 20:39:32 +0000845
Juan Quintelada26fdc2010-12-03 00:07:26 +0100846static const VMStateDescription vmstate_dbdma = {
847 .name = "dbdma",
Mark Cave-Ayland627be2f2016-01-06 20:37:25 +0000848 .version_id = 3,
849 .minimum_version_id = 3,
Juan Quintela35d08452014-04-16 16:01:33 +0200850 .fields = (VMStateField[]) {
Juan Quintelada26fdc2010-12-03 00:07:26 +0100851 VMSTATE_STRUCT_ARRAY(channels, DBDMAState, DBDMA_CHANNELS, 1,
852 vmstate_dbdma_channel, DBDMA_channel),
853 VMSTATE_END_OF_LIST()
854 }
855};
blueswir19b649972008-12-30 19:01:19 +0000856
Mark Cave-Ayland1d27f352017-09-24 15:47:41 +0100857static void mac_dbdma_reset(DeviceState *d)
blueswir16e6b7362008-12-28 18:27:10 +0000858{
Mark Cave-Ayland1d27f352017-09-24 15:47:41 +0100859 DBDMAState *s = MAC_DBDMA(d);
aurel3228ce5ce2009-01-30 20:39:32 +0000860 int i;
861
Mark Cave-Ayland1d27f352017-09-24 15:47:41 +0100862 for (i = 0; i < DBDMA_CHANNELS; i++) {
Juan Quintelac20df142010-12-03 00:04:02 +0100863 memset(s->channels[i].regs, 0, DBDMA_SIZE);
Mark Cave-Ayland1d27f352017-09-24 15:47:41 +0100864 }
blueswir16e6b7362008-12-28 18:27:10 +0000865}
866
Hervé Poussineau2d7d06d2016-02-25 13:02:11 +0100867static void dbdma_unassigned_rw(DBDMA_io *io)
868{
869 DBDMA_channel *ch = io->channel;
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100870 dbdma_cmd *current = &ch->current;
871 uint16_t cmd;
Hervé Poussineau2d7d06d2016-02-25 13:02:11 +0100872 qemu_log_mask(LOG_GUEST_ERROR, "%s: use of unassigned channel %d\n",
873 __func__, ch->channel);
Mark Cave-Ayland2df77892016-07-10 19:08:58 +0100874 ch->io.processing = false;
Benjamin Herrenschmidt77453882017-09-17 18:15:45 +0100875
876 cmd = le16_to_cpu(current->command) & COMMAND_MASK;
877 if (cmd == OUTPUT_MORE || cmd == OUTPUT_LAST ||
878 cmd == INPUT_MORE || cmd == INPUT_LAST) {
879 current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
880 current->res_count = cpu_to_le16(io->len);
881 dbdma_cmdptr_save(ch);
882 }
Hervé Poussineau2d7d06d2016-02-25 13:02:11 +0100883}
884
885static void dbdma_unassigned_flush(DBDMA_io *io)
886{
887 DBDMA_channel *ch = io->channel;
888 qemu_log_mask(LOG_GUEST_ERROR, "%s: use of unassigned channel %d\n",
889 __func__, ch->channel);
890}
891
Mark Cave-Ayland1d27f352017-09-24 15:47:41 +0100892static void mac_dbdma_init(Object *obj)
893{
894 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
895 DBDMAState *s = MAC_DBDMA(obj);
896 int i;
aurel3228ce5ce2009-01-30 20:39:32 +0000897
Alexander Graf3e300fa2014-05-26 10:27:58 +0200898 for (i = 0; i < DBDMA_CHANNELS; i++) {
Hervé Poussineau2d7d06d2016-02-25 13:02:11 +0100899 DBDMA_channel *ch = &s->channels[i];
Hervé Poussineau2d7d06d2016-02-25 13:02:11 +0100900
901 ch->rw = dbdma_unassigned_rw;
902 ch->flush = dbdma_unassigned_flush;
903 ch->channel = i;
904 ch->io.channel = ch;
Alexander Graf3e300fa2014-05-26 10:27:58 +0200905 }
906
Mark Cave-Ayland1d27f352017-09-24 15:47:41 +0100907 memory_region_init_io(&s->mem, obj, &dbdma_ops, s, "dbdma", 0x1000);
908 sysbus_init_mmio(sbd, &s->mem);
909}
910
911static void mac_dbdma_realize(DeviceState *dev, Error **errp)
912{
913 DBDMAState *s = MAC_DBDMA(dev);
aurel3228ce5ce2009-01-30 20:39:32 +0000914
Alexander Grafd2f0ce22013-06-30 02:22:41 +0200915 s->bh = qemu_bh_new(DBDMA_run_bh, s);
j_mayer3cbee152007-10-28 23:42:18 +0000916}
Mark Cave-Ayland1d27f352017-09-24 15:47:41 +0100917
918static void mac_dbdma_class_init(ObjectClass *oc, void *data)
919{
920 DeviceClass *dc = DEVICE_CLASS(oc);
921
922 dc->realize = mac_dbdma_realize;
923 dc->reset = mac_dbdma_reset;
924 dc->vmsd = &vmstate_dbdma;
925}
926
927static const TypeInfo mac_dbdma_type_info = {
928 .name = TYPE_MAC_DBDMA,
929 .parent = TYPE_SYS_BUS_DEVICE,
930 .instance_size = sizeof(DBDMAState),
931 .instance_init = mac_dbdma_init,
932 .class_init = mac_dbdma_class_init
933};
934
935static void mac_dbdma_register_types(void)
936{
937 type_register_static(&mac_dbdma_type_info);
938}
939
940type_init(mac_dbdma_register_types)