2
0

macio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * QEMU IDE Emulation: MacIO support.
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. * Copyright (c) 2006 Openedhand Ltd.
  6. *
  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:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  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
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "hw/ppc/mac.h"
  27. #include "hw/ppc/mac_dbdma.h"
  28. #include "hw/qdev-properties.h"
  29. #include "migration/vmstate.h"
  30. #include "qemu/module.h"
  31. #include "hw/misc/macio/macio.h"
  32. #include "sysemu/block-backend.h"
  33. #include "sysemu/dma.h"
  34. #include "hw/ide/internal.h"
  35. /* debug MACIO */
  36. // #define DEBUG_MACIO
  37. #ifdef DEBUG_MACIO
  38. static const int debug_macio = 1;
  39. #else
  40. static const int debug_macio = 0;
  41. #endif
  42. #define MACIO_DPRINTF(fmt, ...) do { \
  43. if (debug_macio) { \
  44. printf(fmt , ## __VA_ARGS__); \
  45. } \
  46. } while (0)
  47. /***********************************************************/
  48. /* MacIO based PowerPC IDE */
  49. #define MACIO_PAGE_SIZE 4096
  50. static void pmac_ide_atapi_transfer_cb(void *opaque, int ret)
  51. {
  52. DBDMA_io *io = opaque;
  53. MACIOIDEState *m = io->opaque;
  54. IDEState *s = idebus_active_if(&m->bus);
  55. int64_t offset;
  56. MACIO_DPRINTF("pmac_ide_atapi_transfer_cb\n");
  57. if (ret < 0) {
  58. MACIO_DPRINTF("DMA error: %d\n", ret);
  59. qemu_sglist_destroy(&s->sg);
  60. ide_atapi_io_error(s, ret);
  61. goto done;
  62. }
  63. if (!m->dma_active) {
  64. MACIO_DPRINTF("waiting for data (%#x - %#x - %x)\n",
  65. s->nsector, io->len, s->status);
  66. /* data not ready yet, wait for the channel to get restarted */
  67. io->processing = false;
  68. return;
  69. }
  70. if (s->io_buffer_size <= 0) {
  71. MACIO_DPRINTF("End of IDE transfer\n");
  72. qemu_sglist_destroy(&s->sg);
  73. ide_atapi_cmd_ok(s);
  74. m->dma_active = false;
  75. goto done;
  76. }
  77. if (io->len == 0) {
  78. MACIO_DPRINTF("End of DMA transfer\n");
  79. goto done;
  80. }
  81. if (s->lba == -1) {
  82. /* Non-block ATAPI transfer - just copy to RAM */
  83. s->io_buffer_size = MIN(s->io_buffer_size, io->len);
  84. dma_memory_write(&address_space_memory, io->addr, s->io_buffer,
  85. s->io_buffer_size);
  86. io->len = 0;
  87. ide_atapi_cmd_ok(s);
  88. m->dma_active = false;
  89. goto done;
  90. }
  91. /* Calculate current offset */
  92. offset = ((int64_t)s->lba << 11) + s->io_buffer_index;
  93. qemu_sglist_init(&s->sg, DEVICE(m), io->len / MACIO_PAGE_SIZE + 1,
  94. &address_space_memory);
  95. qemu_sglist_add(&s->sg, io->addr, io->len);
  96. s->io_buffer_size -= io->len;
  97. s->io_buffer_index += io->len;
  98. io->len = 0;
  99. s->bus->dma->aiocb = dma_blk_read(s->blk, &s->sg, offset, 0x1,
  100. pmac_ide_atapi_transfer_cb, io);
  101. return;
  102. done:
  103. dma_memory_unmap(&address_space_memory, io->dma_mem, io->dma_len,
  104. io->dir, io->dma_len);
  105. if (ret < 0) {
  106. block_acct_failed(blk_get_stats(s->blk), &s->acct);
  107. } else {
  108. block_acct_done(blk_get_stats(s->blk), &s->acct);
  109. }
  110. ide_set_inactive(s, false);
  111. io->dma_end(opaque);
  112. }
  113. static void pmac_ide_transfer_cb(void *opaque, int ret)
  114. {
  115. DBDMA_io *io = opaque;
  116. MACIOIDEState *m = io->opaque;
  117. IDEState *s = idebus_active_if(&m->bus);
  118. int64_t offset;
  119. MACIO_DPRINTF("pmac_ide_transfer_cb\n");
  120. if (ret < 0) {
  121. MACIO_DPRINTF("DMA error: %d\n", ret);
  122. qemu_sglist_destroy(&s->sg);
  123. ide_dma_error(s);
  124. goto done;
  125. }
  126. if (!m->dma_active) {
  127. MACIO_DPRINTF("waiting for data (%#x - %#x - %x)\n",
  128. s->nsector, io->len, s->status);
  129. /* data not ready yet, wait for the channel to get restarted */
  130. io->processing = false;
  131. return;
  132. }
  133. if (s->io_buffer_size <= 0) {
  134. MACIO_DPRINTF("End of IDE transfer\n");
  135. qemu_sglist_destroy(&s->sg);
  136. s->status = READY_STAT | SEEK_STAT;
  137. ide_set_irq(s->bus);
  138. m->dma_active = false;
  139. goto done;
  140. }
  141. if (io->len == 0) {
  142. MACIO_DPRINTF("End of DMA transfer\n");
  143. goto done;
  144. }
  145. /* Calculate number of sectors */
  146. offset = (ide_get_sector(s) << 9) + s->io_buffer_index;
  147. qemu_sglist_init(&s->sg, DEVICE(m), io->len / MACIO_PAGE_SIZE + 1,
  148. &address_space_memory);
  149. qemu_sglist_add(&s->sg, io->addr, io->len);
  150. s->io_buffer_size -= io->len;
  151. s->io_buffer_index += io->len;
  152. io->len = 0;
  153. switch (s->dma_cmd) {
  154. case IDE_DMA_READ:
  155. s->bus->dma->aiocb = dma_blk_read(s->blk, &s->sg, offset, 0x1,
  156. pmac_ide_atapi_transfer_cb, io);
  157. break;
  158. case IDE_DMA_WRITE:
  159. s->bus->dma->aiocb = dma_blk_write(s->blk, &s->sg, offset, 0x1,
  160. pmac_ide_transfer_cb, io);
  161. break;
  162. case IDE_DMA_TRIM:
  163. s->bus->dma->aiocb = dma_blk_io(blk_get_aio_context(s->blk), &s->sg,
  164. offset, 0x1, ide_issue_trim, s,
  165. pmac_ide_transfer_cb, io,
  166. DMA_DIRECTION_TO_DEVICE);
  167. break;
  168. default:
  169. abort();
  170. }
  171. return;
  172. done:
  173. dma_memory_unmap(&address_space_memory, io->dma_mem, io->dma_len,
  174. io->dir, io->dma_len);
  175. if (s->dma_cmd == IDE_DMA_READ || s->dma_cmd == IDE_DMA_WRITE) {
  176. if (ret < 0) {
  177. block_acct_failed(blk_get_stats(s->blk), &s->acct);
  178. } else {
  179. block_acct_done(blk_get_stats(s->blk), &s->acct);
  180. }
  181. }
  182. ide_set_inactive(s, false);
  183. io->dma_end(opaque);
  184. }
  185. static void pmac_ide_transfer(DBDMA_io *io)
  186. {
  187. MACIOIDEState *m = io->opaque;
  188. IDEState *s = idebus_active_if(&m->bus);
  189. MACIO_DPRINTF("\n");
  190. if (s->drive_kind == IDE_CD) {
  191. block_acct_start(blk_get_stats(s->blk), &s->acct, io->len,
  192. BLOCK_ACCT_READ);
  193. pmac_ide_atapi_transfer_cb(io, 0);
  194. return;
  195. }
  196. switch (s->dma_cmd) {
  197. case IDE_DMA_READ:
  198. block_acct_start(blk_get_stats(s->blk), &s->acct, io->len,
  199. BLOCK_ACCT_READ);
  200. break;
  201. case IDE_DMA_WRITE:
  202. block_acct_start(blk_get_stats(s->blk), &s->acct, io->len,
  203. BLOCK_ACCT_WRITE);
  204. break;
  205. default:
  206. break;
  207. }
  208. pmac_ide_transfer_cb(io, 0);
  209. }
  210. static void pmac_ide_flush(DBDMA_io *io)
  211. {
  212. MACIOIDEState *m = io->opaque;
  213. IDEState *s = idebus_active_if(&m->bus);
  214. if (s->bus->dma->aiocb) {
  215. blk_drain(s->blk);
  216. }
  217. }
  218. /* PowerMac IDE memory IO */
  219. static uint64_t pmac_ide_read(void *opaque, hwaddr addr, unsigned size)
  220. {
  221. MACIOIDEState *d = opaque;
  222. uint64_t retval = 0xffffffff;
  223. int reg = addr >> 4;
  224. switch (reg) {
  225. case 0x0:
  226. if (size == 2) {
  227. retval = ide_data_readw(&d->bus, 0);
  228. } else if (size == 4) {
  229. retval = ide_data_readl(&d->bus, 0);
  230. }
  231. break;
  232. case 0x1 ... 0x7:
  233. if (size == 1) {
  234. retval = ide_ioport_read(&d->bus, reg);
  235. }
  236. break;
  237. case 0x8:
  238. case 0x16:
  239. if (size == 1) {
  240. retval = ide_status_read(&d->bus, 0);
  241. }
  242. break;
  243. case 0x20:
  244. if (size == 4) {
  245. retval = d->timing_reg;
  246. }
  247. break;
  248. case 0x30:
  249. /* This is an interrupt state register that only exists
  250. * in the KeyLargo and later variants. Bit 0x8000_0000
  251. * latches the DMA interrupt and has to be written to
  252. * clear. Bit 0x4000_0000 is an image of the disk
  253. * interrupt. MacOS X relies on this and will hang if
  254. * we don't provide at least the disk interrupt
  255. */
  256. if (size == 4) {
  257. retval = d->irq_reg;
  258. }
  259. break;
  260. }
  261. return retval;
  262. }
  263. static void pmac_ide_write(void *opaque, hwaddr addr, uint64_t val,
  264. unsigned size)
  265. {
  266. MACIOIDEState *d = opaque;
  267. int reg = addr >> 4;
  268. switch (reg) {
  269. case 0x0:
  270. if (size == 2) {
  271. ide_data_writew(&d->bus, 0, val);
  272. } else if (size == 4) {
  273. ide_data_writel(&d->bus, 0, val);
  274. }
  275. break;
  276. case 0x1 ... 0x7:
  277. if (size == 1) {
  278. ide_ioport_write(&d->bus, reg, val);
  279. }
  280. break;
  281. case 0x8:
  282. case 0x16:
  283. if (size == 1) {
  284. ide_ctrl_write(&d->bus, 0, val);
  285. }
  286. break;
  287. case 0x20:
  288. if (size == 4) {
  289. d->timing_reg = val;
  290. }
  291. break;
  292. case 0x30:
  293. if (size == 4) {
  294. if (val & 0x80000000u) {
  295. d->irq_reg &= 0x7fffffff;
  296. }
  297. }
  298. break;
  299. }
  300. }
  301. static const MemoryRegionOps pmac_ide_ops = {
  302. .read = pmac_ide_read,
  303. .write = pmac_ide_write,
  304. .valid.min_access_size = 1,
  305. .valid.max_access_size = 4,
  306. .endianness = DEVICE_LITTLE_ENDIAN,
  307. };
  308. static const VMStateDescription vmstate_pmac = {
  309. .name = "ide",
  310. .version_id = 5,
  311. .minimum_version_id = 0,
  312. .fields = (VMStateField[]) {
  313. VMSTATE_IDE_BUS(bus, MACIOIDEState),
  314. VMSTATE_IDE_DRIVES(bus.ifs, MACIOIDEState),
  315. VMSTATE_BOOL(dma_active, MACIOIDEState),
  316. VMSTATE_UINT32(timing_reg, MACIOIDEState),
  317. VMSTATE_UINT32(irq_reg, MACIOIDEState),
  318. VMSTATE_END_OF_LIST()
  319. }
  320. };
  321. static void macio_ide_reset(DeviceState *dev)
  322. {
  323. MACIOIDEState *d = MACIO_IDE(dev);
  324. ide_bus_reset(&d->bus);
  325. }
  326. static int ide_nop_int(const IDEDMA *dma, bool is_write)
  327. {
  328. return 0;
  329. }
  330. static int32_t ide_nop_int32(const IDEDMA *dma, int32_t l)
  331. {
  332. return 0;
  333. }
  334. static void ide_dbdma_start(const IDEDMA *dma, IDEState *s,
  335. BlockCompletionFunc *cb)
  336. {
  337. MACIOIDEState *m = container_of(dma, MACIOIDEState, dma);
  338. s->io_buffer_index = 0;
  339. if (s->drive_kind == IDE_CD) {
  340. s->io_buffer_size = s->packet_transfer_size;
  341. } else {
  342. s->io_buffer_size = s->nsector * BDRV_SECTOR_SIZE;
  343. }
  344. MACIO_DPRINTF("\n\n------------ IDE transfer\n");
  345. MACIO_DPRINTF("buffer_size: %x buffer_index: %x\n",
  346. s->io_buffer_size, s->io_buffer_index);
  347. MACIO_DPRINTF("lba: %x size: %x\n", s->lba, s->io_buffer_size);
  348. MACIO_DPRINTF("-------------------------\n");
  349. m->dma_active = true;
  350. DBDMA_kick(m->dbdma);
  351. }
  352. static const IDEDMAOps dbdma_ops = {
  353. .start_dma = ide_dbdma_start,
  354. .prepare_buf = ide_nop_int32,
  355. .rw_buf = ide_nop_int,
  356. };
  357. static void macio_ide_realizefn(DeviceState *dev, Error **errp)
  358. {
  359. MACIOIDEState *s = MACIO_IDE(dev);
  360. ide_init2(&s->bus, s->ide_irq);
  361. /* Register DMA callbacks */
  362. s->dma.ops = &dbdma_ops;
  363. s->bus.dma = &s->dma;
  364. }
  365. static void pmac_ide_irq(void *opaque, int n, int level)
  366. {
  367. MACIOIDEState *s = opaque;
  368. uint32_t mask = 0x80000000u >> n;
  369. /* We need to reflect the IRQ state in the irq register */
  370. if (level) {
  371. s->irq_reg |= mask;
  372. } else {
  373. s->irq_reg &= ~mask;
  374. }
  375. if (n) {
  376. qemu_set_irq(s->real_ide_irq, level);
  377. } else {
  378. qemu_set_irq(s->real_dma_irq, level);
  379. }
  380. }
  381. static void macio_ide_initfn(Object *obj)
  382. {
  383. SysBusDevice *d = SYS_BUS_DEVICE(obj);
  384. MACIOIDEState *s = MACIO_IDE(obj);
  385. ide_bus_new(&s->bus, sizeof(s->bus), DEVICE(obj), 0, 2);
  386. memory_region_init_io(&s->mem, obj, &pmac_ide_ops, s, "pmac-ide", 0x1000);
  387. sysbus_init_mmio(d, &s->mem);
  388. sysbus_init_irq(d, &s->real_ide_irq);
  389. sysbus_init_irq(d, &s->real_dma_irq);
  390. s->dma_irq = qemu_allocate_irq(pmac_ide_irq, s, 0);
  391. s->ide_irq = qemu_allocate_irq(pmac_ide_irq, s, 1);
  392. object_property_add_link(obj, "dbdma", TYPE_MAC_DBDMA,
  393. (Object **) &s->dbdma,
  394. qdev_prop_allow_set_link_before_realize, 0);
  395. }
  396. static Property macio_ide_properties[] = {
  397. DEFINE_PROP_UINT32("channel", MACIOIDEState, channel, 0),
  398. DEFINE_PROP_UINT32("addr", MACIOIDEState, addr, -1),
  399. DEFINE_PROP_END_OF_LIST(),
  400. };
  401. static void macio_ide_class_init(ObjectClass *oc, void *data)
  402. {
  403. DeviceClass *dc = DEVICE_CLASS(oc);
  404. dc->realize = macio_ide_realizefn;
  405. dc->reset = macio_ide_reset;
  406. device_class_set_props(dc, macio_ide_properties);
  407. dc->vmsd = &vmstate_pmac;
  408. set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
  409. }
  410. static const TypeInfo macio_ide_type_info = {
  411. .name = TYPE_MACIO_IDE,
  412. .parent = TYPE_SYS_BUS_DEVICE,
  413. .instance_size = sizeof(MACIOIDEState),
  414. .instance_init = macio_ide_initfn,
  415. .class_init = macio_ide_class_init,
  416. };
  417. static void macio_ide_register_types(void)
  418. {
  419. type_register_static(&macio_ide_type_info);
  420. }
  421. /* hd_table must contain 2 block drivers */
  422. void macio_ide_init_drives(MACIOIDEState *s, DriveInfo **hd_table)
  423. {
  424. int i;
  425. for (i = 0; i < 2; i++) {
  426. if (hd_table[i]) {
  427. ide_create_drive(&s->bus, i, hd_table[i]);
  428. }
  429. }
  430. }
  431. void macio_ide_register_dma(MACIOIDEState *s)
  432. {
  433. DBDMA_register_channel(s->dbdma, s->channel, s->dma_irq,
  434. pmac_ide_transfer, pmac_ide_flush, s);
  435. }
  436. type_init(macio_ide_register_types)