macio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 "system/block-backend.h"
  33. #include "system/dma.h"
  34. #include "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. if (ret < 0) {
  104. block_acct_failed(blk_get_stats(s->blk), &s->acct);
  105. } else {
  106. block_acct_done(blk_get_stats(s->blk), &s->acct);
  107. }
  108. ide_set_inactive(s, false);
  109. io->dma_end(opaque);
  110. }
  111. static void pmac_ide_transfer_cb(void *opaque, int ret)
  112. {
  113. DBDMA_io *io = opaque;
  114. MACIOIDEState *m = io->opaque;
  115. IDEState *s = ide_bus_active_if(&m->bus);
  116. int64_t offset;
  117. MACIO_DPRINTF("pmac_ide_transfer_cb\n");
  118. if (ret < 0) {
  119. MACIO_DPRINTF("DMA error: %d\n", ret);
  120. qemu_sglist_destroy(&s->sg);
  121. ide_dma_error(s);
  122. goto done;
  123. }
  124. if (!m->dma_active) {
  125. MACIO_DPRINTF("waiting for data (%#x - %#x - %x)\n",
  126. s->nsector, io->len, s->status);
  127. /* data not ready yet, wait for the channel to get restarted */
  128. io->processing = false;
  129. return;
  130. }
  131. if (s->io_buffer_size <= 0) {
  132. MACIO_DPRINTF("End of IDE transfer\n");
  133. qemu_sglist_destroy(&s->sg);
  134. s->status = READY_STAT | SEEK_STAT;
  135. ide_bus_set_irq(s->bus);
  136. m->dma_active = false;
  137. goto done;
  138. }
  139. if (io->len == 0) {
  140. MACIO_DPRINTF("End of DMA transfer\n");
  141. goto done;
  142. }
  143. /* Calculate number of sectors */
  144. offset = (ide_get_sector(s) << 9) + s->io_buffer_index;
  145. qemu_sglist_init(&s->sg, DEVICE(m), io->len / MACIO_PAGE_SIZE + 1,
  146. &address_space_memory);
  147. qemu_sglist_add(&s->sg, io->addr, io->len);
  148. s->io_buffer_size -= io->len;
  149. s->io_buffer_index += io->len;
  150. io->len = 0;
  151. switch (s->dma_cmd) {
  152. case IDE_DMA_READ:
  153. s->bus->dma->aiocb = dma_blk_read(s->blk, &s->sg, offset, 0x1,
  154. pmac_ide_atapi_transfer_cb, io);
  155. break;
  156. case IDE_DMA_WRITE:
  157. s->bus->dma->aiocb = dma_blk_write(s->blk, &s->sg, offset, 0x1,
  158. pmac_ide_transfer_cb, io);
  159. break;
  160. case IDE_DMA_TRIM:
  161. s->bus->dma->aiocb = dma_blk_io(&s->sg, offset, 0x1, ide_issue_trim, s,
  162. pmac_ide_transfer_cb, io,
  163. DMA_DIRECTION_TO_DEVICE);
  164. break;
  165. default:
  166. abort();
  167. }
  168. return;
  169. done:
  170. if (s->dma_cmd == IDE_DMA_READ || s->dma_cmd == IDE_DMA_WRITE) {
  171. if (ret < 0) {
  172. block_acct_failed(blk_get_stats(s->blk), &s->acct);
  173. } else {
  174. block_acct_done(blk_get_stats(s->blk), &s->acct);
  175. }
  176. }
  177. ide_set_inactive(s, false);
  178. io->dma_end(opaque);
  179. }
  180. static void pmac_ide_transfer(DBDMA_io *io)
  181. {
  182. MACIOIDEState *m = io->opaque;
  183. IDEState *s = ide_bus_active_if(&m->bus);
  184. MACIO_DPRINTF("\n");
  185. if (s->drive_kind == IDE_CD) {
  186. block_acct_start(blk_get_stats(s->blk), &s->acct, io->len,
  187. BLOCK_ACCT_READ);
  188. pmac_ide_atapi_transfer_cb(io, 0);
  189. return;
  190. }
  191. switch (s->dma_cmd) {
  192. case IDE_DMA_READ:
  193. block_acct_start(blk_get_stats(s->blk), &s->acct, io->len,
  194. BLOCK_ACCT_READ);
  195. break;
  196. case IDE_DMA_WRITE:
  197. block_acct_start(blk_get_stats(s->blk), &s->acct, io->len,
  198. BLOCK_ACCT_WRITE);
  199. break;
  200. default:
  201. break;
  202. }
  203. pmac_ide_transfer_cb(io, 0);
  204. }
  205. static void pmac_ide_flush(DBDMA_io *io)
  206. {
  207. MACIOIDEState *m = io->opaque;
  208. IDEState *s = ide_bus_active_if(&m->bus);
  209. if (s->bus->dma->aiocb) {
  210. blk_drain(s->blk);
  211. }
  212. }
  213. /* PowerMac IDE memory IO */
  214. static uint64_t pmac_ide_read(void *opaque, hwaddr addr, unsigned size)
  215. {
  216. MACIOIDEState *d = opaque;
  217. uint64_t retval = 0xffffffff;
  218. int reg = addr >> 4;
  219. switch (reg) {
  220. case 0x0:
  221. if (size == 1) {
  222. retval = ide_data_readw(&d->bus, 0) & 0xFF;
  223. } else if (size == 2) {
  224. retval = ide_data_readw(&d->bus, 0);
  225. } else if (size == 4) {
  226. retval = ide_data_readl(&d->bus, 0);
  227. }
  228. break;
  229. case 0x1 ... 0x7:
  230. if (size == 1) {
  231. retval = ide_ioport_read(&d->bus, reg);
  232. }
  233. break;
  234. case 0x8:
  235. case 0x16:
  236. if (size == 1) {
  237. retval = ide_status_read(&d->bus, 0);
  238. }
  239. break;
  240. case 0x20:
  241. if (size == 4) {
  242. retval = d->timing_reg;
  243. }
  244. break;
  245. case 0x30:
  246. /* This is an interrupt state register that only exists
  247. * in the KeyLargo and later variants. Bit 0x8000_0000
  248. * latches the DMA interrupt and has to be written to
  249. * clear. Bit 0x4000_0000 is an image of the disk
  250. * interrupt. MacOS X relies on this and will hang if
  251. * we don't provide at least the disk interrupt
  252. */
  253. if (size == 4) {
  254. retval = d->irq_reg;
  255. }
  256. break;
  257. }
  258. return retval;
  259. }
  260. static void pmac_ide_write(void *opaque, hwaddr addr, uint64_t val,
  261. unsigned size)
  262. {
  263. MACIOIDEState *d = opaque;
  264. int reg = addr >> 4;
  265. switch (reg) {
  266. case 0x0:
  267. if (size == 2) {
  268. ide_data_writew(&d->bus, 0, val);
  269. } else if (size == 4) {
  270. ide_data_writel(&d->bus, 0, val);
  271. }
  272. break;
  273. case 0x1 ... 0x7:
  274. if (size == 1) {
  275. ide_ioport_write(&d->bus, reg, val);
  276. }
  277. break;
  278. case 0x8:
  279. case 0x16:
  280. if (size == 1) {
  281. ide_ctrl_write(&d->bus, 0, val);
  282. }
  283. break;
  284. case 0x20:
  285. if (size == 4) {
  286. d->timing_reg = val;
  287. }
  288. break;
  289. case 0x30:
  290. if (size == 4) {
  291. if (val & 0x80000000u) {
  292. d->irq_reg &= 0x7fffffff;
  293. }
  294. }
  295. break;
  296. }
  297. }
  298. static const MemoryRegionOps pmac_ide_ops = {
  299. .read = pmac_ide_read,
  300. .write = pmac_ide_write,
  301. .valid.min_access_size = 1,
  302. .valid.max_access_size = 4,
  303. .endianness = DEVICE_LITTLE_ENDIAN,
  304. };
  305. static const VMStateDescription vmstate_pmac = {
  306. .name = "ide",
  307. .version_id = 5,
  308. .minimum_version_id = 0,
  309. .fields = (const VMStateField[]) {
  310. VMSTATE_IDE_BUS(bus, MACIOIDEState),
  311. VMSTATE_IDE_DRIVES(bus.ifs, MACIOIDEState),
  312. VMSTATE_BOOL(dma_active, MACIOIDEState),
  313. VMSTATE_UINT32(timing_reg, MACIOIDEState),
  314. VMSTATE_UINT32(irq_reg, MACIOIDEState),
  315. VMSTATE_END_OF_LIST()
  316. }
  317. };
  318. static void macio_ide_reset(DeviceState *dev)
  319. {
  320. MACIOIDEState *d = MACIO_IDE(dev);
  321. ide_bus_reset(&d->bus);
  322. }
  323. static int ide_nop_int(const IDEDMA *dma, bool is_write)
  324. {
  325. return 0;
  326. }
  327. static int32_t ide_nop_int32(const IDEDMA *dma, int32_t l)
  328. {
  329. return 0;
  330. }
  331. static void ide_dbdma_start(const IDEDMA *dma, IDEState *s,
  332. BlockCompletionFunc *cb)
  333. {
  334. MACIOIDEState *m = container_of(dma, MACIOIDEState, dma);
  335. s->io_buffer_index = 0;
  336. if (s->drive_kind == IDE_CD) {
  337. s->io_buffer_size = s->packet_transfer_size;
  338. } else {
  339. s->io_buffer_size = s->nsector * BDRV_SECTOR_SIZE;
  340. }
  341. MACIO_DPRINTF("\n\n------------ IDE transfer\n");
  342. MACIO_DPRINTF("buffer_size: %x buffer_index: %x\n",
  343. s->io_buffer_size, s->io_buffer_index);
  344. MACIO_DPRINTF("lba: %x size: %x\n", s->lba, s->io_buffer_size);
  345. MACIO_DPRINTF("-------------------------\n");
  346. m->dma_active = true;
  347. DBDMA_kick(m->dbdma);
  348. }
  349. static const IDEDMAOps dbdma_ops = {
  350. .start_dma = ide_dbdma_start,
  351. .prepare_buf = ide_nop_int32,
  352. .rw_buf = ide_nop_int,
  353. };
  354. static void macio_ide_realizefn(DeviceState *dev, Error **errp)
  355. {
  356. MACIOIDEState *s = MACIO_IDE(dev);
  357. ide_bus_init_output_irq(&s->bus,
  358. qdev_get_gpio_in(dev, MACIO_IDE_PMAC_IDE_IRQ));
  359. /* Register DMA callbacks */
  360. s->dma.ops = &dbdma_ops;
  361. s->bus.dma = &s->dma;
  362. }
  363. static void pmac_ide_irq(void *opaque, int n, int level)
  364. {
  365. MACIOIDEState *s = opaque;
  366. uint32_t mask = 0x80000000u >> n;
  367. /* We need to reflect the IRQ state in the irq register */
  368. if (level) {
  369. s->irq_reg |= mask;
  370. } else {
  371. s->irq_reg &= ~mask;
  372. }
  373. if (n) {
  374. qemu_set_irq(s->real_ide_irq, level);
  375. } else {
  376. qemu_set_irq(s->real_dma_irq, level);
  377. }
  378. }
  379. static void macio_ide_initfn(Object *obj)
  380. {
  381. SysBusDevice *d = SYS_BUS_DEVICE(obj);
  382. MACIOIDEState *s = MACIO_IDE(obj);
  383. ide_bus_init(&s->bus, sizeof(s->bus), DEVICE(obj), 0, 2);
  384. memory_region_init_io(&s->mem, obj, &pmac_ide_ops, s, "pmac-ide", 0x1000);
  385. sysbus_init_mmio(d, &s->mem);
  386. sysbus_init_irq(d, &s->real_ide_irq);
  387. sysbus_init_irq(d, &s->real_dma_irq);
  388. qdev_init_gpio_in(DEVICE(obj), pmac_ide_irq, MACIO_IDE_PMAC_NIRQS);
  389. object_property_add_link(obj, "dbdma", TYPE_MAC_DBDMA,
  390. (Object **) &s->dbdma,
  391. qdev_prop_allow_set_link_before_realize, 0);
  392. }
  393. static const Property macio_ide_properties[] = {
  394. DEFINE_PROP_UINT32("channel", MACIOIDEState, channel, 0),
  395. DEFINE_PROP_UINT32("addr", MACIOIDEState, addr, -1),
  396. };
  397. static void macio_ide_class_init(ObjectClass *oc, void *data)
  398. {
  399. DeviceClass *dc = DEVICE_CLASS(oc);
  400. dc->realize = macio_ide_realizefn;
  401. device_class_set_legacy_reset(dc, macio_ide_reset);
  402. device_class_set_props(dc, macio_ide_properties);
  403. dc->vmsd = &vmstate_pmac;
  404. set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
  405. }
  406. static const TypeInfo macio_ide_type_info = {
  407. .name = TYPE_MACIO_IDE,
  408. .parent = TYPE_SYS_BUS_DEVICE,
  409. .instance_size = sizeof(MACIOIDEState),
  410. .instance_init = macio_ide_initfn,
  411. .class_init = macio_ide_class_init,
  412. };
  413. static void macio_ide_register_types(void)
  414. {
  415. type_register_static(&macio_ide_type_info);
  416. }
  417. /* hd_table must contain 2 block drivers */
  418. void macio_ide_init_drives(MACIOIDEState *s, DriveInfo **hd_table)
  419. {
  420. int i;
  421. for (i = 0; i < 2; i++) {
  422. if (hd_table[i]) {
  423. ide_bus_create_drive(&s->bus, i, hd_table[i]);
  424. }
  425. }
  426. }
  427. void macio_ide_register_dma(MACIOIDEState *s)
  428. {
  429. DBDMA_register_channel(s->dbdma, s->channel,
  430. qdev_get_gpio_in(DEVICE(s), MACIO_IDE_PMAC_DMA_IRQ),
  431. pmac_ide_transfer, pmac_ide_flush, s);
  432. }
  433. type_init(macio_ide_register_types)