macio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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/irq.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 = ide_bus_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, MEMTXATTRS_UNSPECIFIED);
  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 = ide_bus_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_bus_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 = ide_bus_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 = ide_bus_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 == 1) {
  227. retval = ide_data_readw(&d->bus, 0) & 0xFF;
  228. } else if (size == 2) {
  229. retval = ide_data_readw(&d->bus, 0);
  230. } else if (size == 4) {
  231. retval = ide_data_readl(&d->bus, 0);
  232. }
  233. break;
  234. case 0x1 ... 0x7:
  235. if (size == 1) {
  236. retval = ide_ioport_read(&d->bus, reg);
  237. }
  238. break;
  239. case 0x8:
  240. case 0x16:
  241. if (size == 1) {
  242. retval = ide_status_read(&d->bus, 0);
  243. }
  244. break;
  245. case 0x20:
  246. if (size == 4) {
  247. retval = d->timing_reg;
  248. }
  249. break;
  250. case 0x30:
  251. /* This is an interrupt state register that only exists
  252. * in the KeyLargo and later variants. Bit 0x8000_0000
  253. * latches the DMA interrupt and has to be written to
  254. * clear. Bit 0x4000_0000 is an image of the disk
  255. * interrupt. MacOS X relies on this and will hang if
  256. * we don't provide at least the disk interrupt
  257. */
  258. if (size == 4) {
  259. retval = d->irq_reg;
  260. }
  261. break;
  262. }
  263. return retval;
  264. }
  265. static void pmac_ide_write(void *opaque, hwaddr addr, uint64_t val,
  266. unsigned size)
  267. {
  268. MACIOIDEState *d = opaque;
  269. int reg = addr >> 4;
  270. switch (reg) {
  271. case 0x0:
  272. if (size == 2) {
  273. ide_data_writew(&d->bus, 0, val);
  274. } else if (size == 4) {
  275. ide_data_writel(&d->bus, 0, val);
  276. }
  277. break;
  278. case 0x1 ... 0x7:
  279. if (size == 1) {
  280. ide_ioport_write(&d->bus, reg, val);
  281. }
  282. break;
  283. case 0x8:
  284. case 0x16:
  285. if (size == 1) {
  286. ide_ctrl_write(&d->bus, 0, val);
  287. }
  288. break;
  289. case 0x20:
  290. if (size == 4) {
  291. d->timing_reg = val;
  292. }
  293. break;
  294. case 0x30:
  295. if (size == 4) {
  296. if (val & 0x80000000u) {
  297. d->irq_reg &= 0x7fffffff;
  298. }
  299. }
  300. break;
  301. }
  302. }
  303. static const MemoryRegionOps pmac_ide_ops = {
  304. .read = pmac_ide_read,
  305. .write = pmac_ide_write,
  306. .valid.min_access_size = 1,
  307. .valid.max_access_size = 4,
  308. .endianness = DEVICE_LITTLE_ENDIAN,
  309. };
  310. static const VMStateDescription vmstate_pmac = {
  311. .name = "ide",
  312. .version_id = 5,
  313. .minimum_version_id = 0,
  314. .fields = (VMStateField[]) {
  315. VMSTATE_IDE_BUS(bus, MACIOIDEState),
  316. VMSTATE_IDE_DRIVES(bus.ifs, MACIOIDEState),
  317. VMSTATE_BOOL(dma_active, MACIOIDEState),
  318. VMSTATE_UINT32(timing_reg, MACIOIDEState),
  319. VMSTATE_UINT32(irq_reg, MACIOIDEState),
  320. VMSTATE_END_OF_LIST()
  321. }
  322. };
  323. static void macio_ide_reset(DeviceState *dev)
  324. {
  325. MACIOIDEState *d = MACIO_IDE(dev);
  326. ide_bus_reset(&d->bus);
  327. }
  328. static int ide_nop_int(const IDEDMA *dma, bool is_write)
  329. {
  330. return 0;
  331. }
  332. static int32_t ide_nop_int32(const IDEDMA *dma, int32_t l)
  333. {
  334. return 0;
  335. }
  336. static void ide_dbdma_start(const IDEDMA *dma, IDEState *s,
  337. BlockCompletionFunc *cb)
  338. {
  339. MACIOIDEState *m = container_of(dma, MACIOIDEState, dma);
  340. s->io_buffer_index = 0;
  341. if (s->drive_kind == IDE_CD) {
  342. s->io_buffer_size = s->packet_transfer_size;
  343. } else {
  344. s->io_buffer_size = s->nsector * BDRV_SECTOR_SIZE;
  345. }
  346. MACIO_DPRINTF("\n\n------------ IDE transfer\n");
  347. MACIO_DPRINTF("buffer_size: %x buffer_index: %x\n",
  348. s->io_buffer_size, s->io_buffer_index);
  349. MACIO_DPRINTF("lba: %x size: %x\n", s->lba, s->io_buffer_size);
  350. MACIO_DPRINTF("-------------------------\n");
  351. m->dma_active = true;
  352. DBDMA_kick(m->dbdma);
  353. }
  354. static const IDEDMAOps dbdma_ops = {
  355. .start_dma = ide_dbdma_start,
  356. .prepare_buf = ide_nop_int32,
  357. .rw_buf = ide_nop_int,
  358. };
  359. static void macio_ide_realizefn(DeviceState *dev, Error **errp)
  360. {
  361. MACIOIDEState *s = MACIO_IDE(dev);
  362. ide_bus_init_output_irq(&s->bus, s->ide_irq);
  363. /* Register DMA callbacks */
  364. s->dma.ops = &dbdma_ops;
  365. s->bus.dma = &s->dma;
  366. }
  367. static void pmac_ide_irq(void *opaque, int n, int level)
  368. {
  369. MACIOIDEState *s = opaque;
  370. uint32_t mask = 0x80000000u >> n;
  371. /* We need to reflect the IRQ state in the irq register */
  372. if (level) {
  373. s->irq_reg |= mask;
  374. } else {
  375. s->irq_reg &= ~mask;
  376. }
  377. if (n) {
  378. qemu_set_irq(s->real_ide_irq, level);
  379. } else {
  380. qemu_set_irq(s->real_dma_irq, level);
  381. }
  382. }
  383. static void macio_ide_initfn(Object *obj)
  384. {
  385. SysBusDevice *d = SYS_BUS_DEVICE(obj);
  386. MACIOIDEState *s = MACIO_IDE(obj);
  387. ide_bus_init(&s->bus, sizeof(s->bus), DEVICE(obj), 0, 2);
  388. memory_region_init_io(&s->mem, obj, &pmac_ide_ops, s, "pmac-ide", 0x1000);
  389. sysbus_init_mmio(d, &s->mem);
  390. sysbus_init_irq(d, &s->real_ide_irq);
  391. sysbus_init_irq(d, &s->real_dma_irq);
  392. s->dma_irq = qemu_allocate_irq(pmac_ide_irq, s, 0);
  393. s->ide_irq = qemu_allocate_irq(pmac_ide_irq, s, 1);
  394. object_property_add_link(obj, "dbdma", TYPE_MAC_DBDMA,
  395. (Object **) &s->dbdma,
  396. qdev_prop_allow_set_link_before_realize, 0);
  397. }
  398. static Property macio_ide_properties[] = {
  399. DEFINE_PROP_UINT32("channel", MACIOIDEState, channel, 0),
  400. DEFINE_PROP_UINT32("addr", MACIOIDEState, addr, -1),
  401. DEFINE_PROP_END_OF_LIST(),
  402. };
  403. static void macio_ide_class_init(ObjectClass *oc, void *data)
  404. {
  405. DeviceClass *dc = DEVICE_CLASS(oc);
  406. dc->realize = macio_ide_realizefn;
  407. dc->reset = macio_ide_reset;
  408. device_class_set_props(dc, macio_ide_properties);
  409. dc->vmsd = &vmstate_pmac;
  410. set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
  411. }
  412. static const TypeInfo macio_ide_type_info = {
  413. .name = TYPE_MACIO_IDE,
  414. .parent = TYPE_SYS_BUS_DEVICE,
  415. .instance_size = sizeof(MACIOIDEState),
  416. .instance_init = macio_ide_initfn,
  417. .class_init = macio_ide_class_init,
  418. };
  419. static void macio_ide_register_types(void)
  420. {
  421. type_register_static(&macio_ide_type_info);
  422. }
  423. /* hd_table must contain 2 block drivers */
  424. void macio_ide_init_drives(MACIOIDEState *s, DriveInfo **hd_table)
  425. {
  426. int i;
  427. for (i = 0; i < 2; i++) {
  428. if (hd_table[i]) {
  429. ide_bus_create_drive(&s->bus, i, hd_table[i]);
  430. }
  431. }
  432. }
  433. void macio_ide_register_dma(MACIOIDEState *s)
  434. {
  435. DBDMA_register_channel(s->dbdma, s->channel, s->dma_irq,
  436. pmac_ide_transfer, pmac_ide_flush, s);
  437. }
  438. type_init(macio_ide_register_types)