2 * QEMU IDE Emulation: MacIO support.
4 * Copyright (c) 2003 Fabrice Bellard
5 * Copyright (c) 2006 Openedhand Ltd.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "hw/ppc/mac.h"
27 #include "hw/ppc/mac_dbdma.h"
28 #include "block/block.h"
29 #include "sysemu/dma.h"
31 #include <hw/ide/internal.h>
34 // #define DEBUG_MACIO
37 static const int debug_macio
= 1;
39 static const int debug_macio
= 0;
42 #define MACIO_DPRINTF(fmt, ...) do { \
44 printf(fmt , ## __VA_ARGS__); \
49 /***********************************************************/
50 /* MacIO based PowerPC IDE */
52 #define MACIO_PAGE_SIZE 4096
54 static void pmac_ide_atapi_transfer_cb(void *opaque
, int ret
)
56 DBDMA_io
*io
= opaque
;
57 MACIOIDEState
*m
= io
->opaque
;
58 IDEState
*s
= idebus_active_if(&m
->bus
);
63 qemu_sglist_destroy(&s
->sg
);
64 ide_atapi_io_error(s
, ret
);
65 io
->remainder_len
= 0;
70 MACIO_DPRINTF("waiting for data (%#x - %#x - %x)\n",
71 s
->nsector
, io
->len
, s
->status
);
72 /* data not ready yet, wait for the channel to get restarted */
73 io
->processing
= false;
77 MACIO_DPRINTF("io_buffer_size = %#x\n", s
->io_buffer_size
);
79 if (s
->io_buffer_size
> 0) {
81 qemu_sglist_destroy(&s
->sg
);
83 s
->packet_transfer_size
-= s
->io_buffer_size
;
85 s
->io_buffer_index
+= s
->io_buffer_size
;
86 s
->lba
+= s
->io_buffer_index
>> 11;
87 s
->io_buffer_index
&= 0x7ff;
90 s
->io_buffer_size
= MIN(io
->len
, s
->packet_transfer_size
);
92 MACIO_DPRINTF("remainder: %d io->len: %d size: %d\n", io
->remainder_len
,
93 io
->len
, s
->packet_transfer_size
);
94 if (io
->remainder_len
&& io
->len
) {
95 /* guest wants the rest of its previous transfer */
96 int remainder_len
= MIN(io
->remainder_len
, io
->len
);
98 MACIO_DPRINTF("copying remainder %d bytes\n", remainder_len
);
100 cpu_physical_memory_write(io
->addr
, io
->remainder
+ 0x200 -
101 remainder_len
, remainder_len
);
103 io
->addr
+= remainder_len
;
104 io
->len
-= remainder_len
;
105 s
->io_buffer_size
= remainder_len
;
106 io
->remainder_len
-= remainder_len
;
107 /* treat remainder as individual transfer, start again */
108 qemu_sglist_init(&s
->sg
, DEVICE(m
), io
->len
/ MACIO_PAGE_SIZE
+ 1,
109 &address_space_memory
);
110 pmac_ide_atapi_transfer_cb(opaque
, 0);
114 if (!s
->packet_transfer_size
) {
115 MACIO_DPRINTF("end of transfer\n");
117 m
->dma_active
= false;
121 MACIO_DPRINTF("end of DMA\n");
125 /* launch next transfer */
127 /* handle unaligned accesses first, get them over with and only do the
128 remaining bulk transfer using our async DMA helpers */
129 unaligned
= io
->len
& 0x1ff;
131 int sector_num
= (s
->lba
<< 2) + (s
->io_buffer_index
>> 9);
132 int nsector
= io
->len
>> 9;
134 MACIO_DPRINTF("precopying unaligned %d bytes to %#" HWADDR_PRIx
"\n",
135 unaligned
, io
->addr
+ io
->len
- unaligned
);
137 bdrv_read(s
->bs
, sector_num
+ nsector
, io
->remainder
, 1);
138 cpu_physical_memory_write(io
->addr
+ io
->len
- unaligned
,
139 io
->remainder
, unaligned
);
141 io
->len
-= unaligned
;
144 MACIO_DPRINTF("io->len = %#x\n", io
->len
);
146 qemu_sglist_init(&s
->sg
, DEVICE(m
), io
->len
/ MACIO_PAGE_SIZE
+ 1,
147 &address_space_memory
);
148 qemu_sglist_add(&s
->sg
, io
->addr
, io
->len
);
149 io
->addr
+= s
->io_buffer_size
;
150 io
->remainder_len
= MIN(s
->packet_transfer_size
- s
->io_buffer_size
,
151 (0x200 - unaligned
) & 0x1ff);
152 MACIO_DPRINTF("set remainder to: %d\n", io
->remainder_len
);
154 /* We would read no data from the block layer, thus not get a callback.
155 Just fake completion manually. */
157 pmac_ide_atapi_transfer_cb(opaque
, 0);
163 MACIO_DPRINTF("sector_num=%d size=%d, cmd_cmd=%d\n",
164 (s
->lba
<< 2) + (s
->io_buffer_index
>> 9),
165 s
->packet_transfer_size
, s
->dma_cmd
);
167 m
->aiocb
= dma_bdrv_read(s
->bs
, &s
->sg
,
168 (int64_t)(s
->lba
<< 2) + (s
->io_buffer_index
>> 9),
169 pmac_ide_atapi_transfer_cb
, io
);
173 MACIO_DPRINTF("done DMA\n");
174 bdrv_acct_done(s
->bs
, &s
->acct
);
178 static void pmac_ide_transfer_cb(void *opaque
, int ret
)
180 DBDMA_io
*io
= opaque
;
181 MACIOIDEState
*m
= io
->opaque
;
182 IDEState
*s
= idebus_active_if(&m
->bus
);
188 MACIO_DPRINTF("DMA error\n");
190 qemu_sglist_destroy(&s
->sg
);
192 io
->remainder_len
= 0;
196 if (!m
->dma_active
) {
197 MACIO_DPRINTF("waiting for data (%#x - %#x - %x)\n",
198 s
->nsector
, io
->len
, s
->status
);
199 /* data not ready yet, wait for the channel to get restarted */
200 io
->processing
= false;
204 sector_num
= ide_get_sector(s
);
205 MACIO_DPRINTF("io_buffer_size = %#x\n", s
->io_buffer_size
);
206 if (s
->io_buffer_size
> 0) {
208 qemu_sglist_destroy(&s
->sg
);
209 n
= (s
->io_buffer_size
+ 0x1ff) >> 9;
211 ide_set_sector(s
, sector_num
);
215 MACIO_DPRINTF("remainder: %d io->len: %d nsector: %d "
216 "sector_num: %" PRId64
"\n",
217 io
->remainder_len
, io
->len
, s
->nsector
, sector_num
);
218 if (io
->remainder_len
&& io
->len
) {
219 /* guest wants the rest of its previous transfer */
220 int remainder_len
= MIN(io
->remainder_len
, io
->len
);
221 uint8_t *p
= &io
->remainder
[0x200 - remainder_len
];
223 MACIO_DPRINTF("copying remainder %d bytes at %#" HWADDR_PRIx
"\n",
224 remainder_len
, io
->addr
);
226 switch (s
->dma_cmd
) {
228 cpu_physical_memory_write(io
->addr
, p
, remainder_len
);
231 cpu_physical_memory_read(io
->addr
, p
, remainder_len
);
232 bdrv_write(s
->bs
, sector_num
- 1, io
->remainder
, 1);
237 io
->addr
+= remainder_len
;
238 io
->len
-= remainder_len
;
239 io
->remainder_len
-= remainder_len
;
242 if (s
->nsector
== 0 && !io
->remainder_len
) {
243 MACIO_DPRINTF("end of transfer\n");
244 s
->status
= READY_STAT
| SEEK_STAT
;
246 m
->dma_active
= false;
250 MACIO_DPRINTF("end of DMA\n");
254 /* launch next transfer */
256 s
->io_buffer_index
= 0;
257 s
->io_buffer_size
= MIN(io
->len
, s
->nsector
* 512);
259 /* handle unaligned accesses first, get them over with and only do the
260 remaining bulk transfer using our async DMA helpers */
261 unaligned
= io
->len
& 0x1ff;
263 int nsector
= io
->len
>> 9;
265 MACIO_DPRINTF("precopying unaligned %d bytes to %#" HWADDR_PRIx
"\n",
266 unaligned
, io
->addr
+ io
->len
- unaligned
);
268 switch (s
->dma_cmd
) {
270 bdrv_read(s
->bs
, sector_num
+ nsector
, io
->remainder
, 1);
271 cpu_physical_memory_write(io
->addr
+ io
->len
- unaligned
,
272 io
->remainder
, unaligned
);
275 /* cache the contents in our io struct */
276 cpu_physical_memory_read(io
->addr
+ io
->len
- unaligned
,
277 io
->remainder
, unaligned
);
283 io
->len
-= unaligned
;
286 MACIO_DPRINTF("io->len = %#x\n", io
->len
);
288 qemu_sglist_init(&s
->sg
, DEVICE(m
), io
->len
/ MACIO_PAGE_SIZE
+ 1,
289 &address_space_memory
);
290 qemu_sglist_add(&s
->sg
, io
->addr
, io
->len
);
291 io
->addr
+= io
->len
+ unaligned
;
292 io
->remainder_len
= (0x200 - unaligned
) & 0x1ff;
293 MACIO_DPRINTF("set remainder to: %d\n", io
->remainder_len
);
295 /* We would read no data from the block layer, thus not get a callback.
296 Just fake completion manually. */
298 pmac_ide_transfer_cb(opaque
, 0);
304 MACIO_DPRINTF("sector_num=%" PRId64
" n=%d, nsector=%d, cmd_cmd=%d\n",
305 sector_num
, n
, s
->nsector
, s
->dma_cmd
);
307 switch (s
->dma_cmd
) {
309 m
->aiocb
= dma_bdrv_read(s
->bs
, &s
->sg
, sector_num
,
310 pmac_ide_transfer_cb
, io
);
313 m
->aiocb
= dma_bdrv_write(s
->bs
, &s
->sg
, sector_num
,
314 pmac_ide_transfer_cb
, io
);
317 m
->aiocb
= dma_bdrv_io(s
->bs
, &s
->sg
, sector_num
,
318 ide_issue_trim
, pmac_ide_transfer_cb
, io
,
319 DMA_DIRECTION_TO_DEVICE
);
325 if (s
->dma_cmd
== IDE_DMA_READ
|| s
->dma_cmd
== IDE_DMA_WRITE
) {
326 bdrv_acct_done(s
->bs
, &s
->acct
);
331 static void pmac_ide_transfer(DBDMA_io
*io
)
333 MACIOIDEState
*m
= io
->opaque
;
334 IDEState
*s
= idebus_active_if(&m
->bus
);
338 s
->io_buffer_size
= 0;
339 if (s
->drive_kind
== IDE_CD
) {
340 bdrv_acct_start(s
->bs
, &s
->acct
, io
->len
, BDRV_ACCT_READ
);
341 pmac_ide_atapi_transfer_cb(io
, 0);
345 switch (s
->dma_cmd
) {
347 bdrv_acct_start(s
->bs
, &s
->acct
, io
->len
, BDRV_ACCT_READ
);
350 bdrv_acct_start(s
->bs
, &s
->acct
, io
->len
, BDRV_ACCT_WRITE
);
356 pmac_ide_transfer_cb(io
, 0);
359 static void pmac_ide_flush(DBDMA_io
*io
)
361 MACIOIDEState
*m
= io
->opaque
;
368 /* PowerMac IDE memory IO */
369 static void pmac_ide_writeb (void *opaque
,
370 hwaddr addr
, uint32_t val
)
372 MACIOIDEState
*d
= opaque
;
374 addr
= (addr
& 0xFFF) >> 4;
377 ide_ioport_write(&d
->bus
, addr
, val
);
381 ide_cmd_write(&d
->bus
, 0, val
);
388 static uint32_t pmac_ide_readb (void *opaque
,hwaddr addr
)
391 MACIOIDEState
*d
= opaque
;
393 addr
= (addr
& 0xFFF) >> 4;
396 retval
= ide_ioport_read(&d
->bus
, addr
);
400 retval
= ide_status_read(&d
->bus
, 0);
409 static void pmac_ide_writew (void *opaque
,
410 hwaddr addr
, uint32_t val
)
412 MACIOIDEState
*d
= opaque
;
414 addr
= (addr
& 0xFFF) >> 4;
417 ide_data_writew(&d
->bus
, 0, val
);
421 static uint32_t pmac_ide_readw (void *opaque
,hwaddr addr
)
424 MACIOIDEState
*d
= opaque
;
426 addr
= (addr
& 0xFFF) >> 4;
428 retval
= ide_data_readw(&d
->bus
, 0);
432 retval
= bswap16(retval
);
436 static void pmac_ide_writel (void *opaque
,
437 hwaddr addr
, uint32_t val
)
439 MACIOIDEState
*d
= opaque
;
441 addr
= (addr
& 0xFFF) >> 4;
444 ide_data_writel(&d
->bus
, 0, val
);
448 static uint32_t pmac_ide_readl (void *opaque
,hwaddr addr
)
451 MACIOIDEState
*d
= opaque
;
453 addr
= (addr
& 0xFFF) >> 4;
455 retval
= ide_data_readl(&d
->bus
, 0);
459 retval
= bswap32(retval
);
463 static const MemoryRegionOps pmac_ide_ops
= {
476 .endianness
= DEVICE_NATIVE_ENDIAN
,
479 static const VMStateDescription vmstate_pmac
= {
482 .minimum_version_id
= 0,
483 .minimum_version_id_old
= 0,
484 .fields
= (VMStateField
[]) {
485 VMSTATE_IDE_BUS(bus
, MACIOIDEState
),
486 VMSTATE_IDE_DRIVES(bus
.ifs
, MACIOIDEState
),
487 VMSTATE_END_OF_LIST()
491 static void macio_ide_reset(DeviceState
*dev
)
493 MACIOIDEState
*d
= MACIO_IDE(dev
);
495 ide_bus_reset(&d
->bus
);
498 static int ide_nop(IDEDMA
*dma
)
503 static int ide_nop_int(IDEDMA
*dma
, int x
)
508 static void ide_nop_restart(void *opaque
, int x
, RunState y
)
512 static void ide_dbdma_start(IDEDMA
*dma
, IDEState
*s
,
513 BlockDriverCompletionFunc
*cb
)
515 MACIOIDEState
*m
= container_of(dma
, MACIOIDEState
, dma
);
518 m
->dma_active
= true;
519 DBDMA_kick(m
->dbdma
);
522 static const IDEDMAOps dbdma_ops
= {
523 .start_dma
= ide_dbdma_start
,
524 .start_transfer
= ide_nop
,
525 .prepare_buf
= ide_nop_int
,
526 .rw_buf
= ide_nop_int
,
527 .set_unit
= ide_nop_int
,
528 .add_status
= ide_nop_int
,
529 .set_inactive
= ide_nop
,
530 .restart_cb
= ide_nop_restart
,
534 static void macio_ide_realizefn(DeviceState
*dev
, Error
**errp
)
536 MACIOIDEState
*s
= MACIO_IDE(dev
);
538 ide_init2(&s
->bus
, s
->irq
);
540 /* Register DMA callbacks */
541 s
->dma
.ops
= &dbdma_ops
;
542 s
->bus
.dma
= &s
->dma
;
545 static void macio_ide_initfn(Object
*obj
)
547 SysBusDevice
*d
= SYS_BUS_DEVICE(obj
);
548 MACIOIDEState
*s
= MACIO_IDE(obj
);
550 ide_bus_new(&s
->bus
, DEVICE(obj
), 0, 2);
551 memory_region_init_io(&s
->mem
, obj
, &pmac_ide_ops
, s
, "pmac-ide", 0x1000);
552 sysbus_init_mmio(d
, &s
->mem
);
553 sysbus_init_irq(d
, &s
->irq
);
554 sysbus_init_irq(d
, &s
->dma_irq
);
557 static void macio_ide_class_init(ObjectClass
*oc
, void *data
)
559 DeviceClass
*dc
= DEVICE_CLASS(oc
);
561 dc
->realize
= macio_ide_realizefn
;
562 dc
->reset
= macio_ide_reset
;
563 dc
->vmsd
= &vmstate_pmac
;
566 static const TypeInfo macio_ide_type_info
= {
567 .name
= TYPE_MACIO_IDE
,
568 .parent
= TYPE_SYS_BUS_DEVICE
,
569 .instance_size
= sizeof(MACIOIDEState
),
570 .instance_init
= macio_ide_initfn
,
571 .class_init
= macio_ide_class_init
,
574 static void macio_ide_register_types(void)
576 type_register_static(&macio_ide_type_info
);
579 /* hd_table must contain 2 block drivers */
580 void macio_ide_init_drives(MACIOIDEState
*s
, DriveInfo
**hd_table
)
584 for (i
= 0; i
< 2; i
++) {
586 ide_create_drive(&s
->bus
, i
, hd_table
[i
]);
591 void macio_ide_register_dma(MACIOIDEState
*s
, void *dbdma
, int channel
)
594 DBDMA_register_channel(dbdma
, channel
, s
->dma_irq
,
595 pmac_ide_transfer
, pmac_ide_flush
, s
);
598 type_init(macio_ide_register_types
)