pci.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. * QEMU IDE Emulation: PCI Bus 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/pci/pci.h"
  27. #include "migration/vmstate.h"
  28. #include "sysemu/dma.h"
  29. #include "qemu/error-report.h"
  30. #include "qemu/module.h"
  31. #include "hw/ide/pci.h"
  32. #include "trace.h"
  33. #define BMDMA_PAGE_SIZE 4096
  34. #define BM_MIGRATION_COMPAT_STATUS_BITS \
  35. (IDE_RETRY_DMA | IDE_RETRY_PIO | \
  36. IDE_RETRY_READ | IDE_RETRY_FLUSH)
  37. static uint64_t pci_ide_status_read(void *opaque, hwaddr addr, unsigned size)
  38. {
  39. IDEBus *bus = opaque;
  40. if (addr != 2 || size != 1) {
  41. return ((uint64_t)1 << (size * 8)) - 1;
  42. }
  43. return ide_status_read(bus, addr + 2);
  44. }
  45. static void pci_ide_ctrl_write(void *opaque, hwaddr addr,
  46. uint64_t data, unsigned size)
  47. {
  48. IDEBus *bus = opaque;
  49. if (addr != 2 || size != 1) {
  50. return;
  51. }
  52. ide_ctrl_write(bus, addr + 2, data);
  53. }
  54. const MemoryRegionOps pci_ide_cmd_le_ops = {
  55. .read = pci_ide_status_read,
  56. .write = pci_ide_ctrl_write,
  57. .endianness = DEVICE_LITTLE_ENDIAN,
  58. };
  59. static uint64_t pci_ide_data_read(void *opaque, hwaddr addr, unsigned size)
  60. {
  61. IDEBus *bus = opaque;
  62. if (size == 1) {
  63. return ide_ioport_read(bus, addr);
  64. } else if (addr == 0) {
  65. if (size == 2) {
  66. return ide_data_readw(bus, addr);
  67. } else {
  68. return ide_data_readl(bus, addr);
  69. }
  70. }
  71. return ((uint64_t)1 << (size * 8)) - 1;
  72. }
  73. static void pci_ide_data_write(void *opaque, hwaddr addr,
  74. uint64_t data, unsigned size)
  75. {
  76. IDEBus *bus = opaque;
  77. if (size == 1) {
  78. ide_ioport_write(bus, addr, data);
  79. } else if (addr == 0) {
  80. if (size == 2) {
  81. ide_data_writew(bus, addr, data);
  82. } else {
  83. ide_data_writel(bus, addr, data);
  84. }
  85. }
  86. }
  87. const MemoryRegionOps pci_ide_data_le_ops = {
  88. .read = pci_ide_data_read,
  89. .write = pci_ide_data_write,
  90. .endianness = DEVICE_LITTLE_ENDIAN,
  91. };
  92. static void bmdma_start_dma(const IDEDMA *dma, IDEState *s,
  93. BlockCompletionFunc *dma_cb)
  94. {
  95. BMDMAState *bm = DO_UPCAST(BMDMAState, dma, dma);
  96. bm->dma_cb = dma_cb;
  97. bm->cur_prd_last = 0;
  98. bm->cur_prd_addr = 0;
  99. bm->cur_prd_len = 0;
  100. if (bm->status & BM_STATUS_DMAING) {
  101. bm->dma_cb(bmdma_active_if(bm), 0);
  102. }
  103. }
  104. /**
  105. * Prepare an sglist based on available PRDs.
  106. * @limit: How many bytes to prepare total.
  107. *
  108. * Returns the number of bytes prepared, -1 on error.
  109. * IDEState.io_buffer_size will contain the number of bytes described
  110. * by the PRDs, whether or not we added them to the sglist.
  111. */
  112. static int32_t bmdma_prepare_buf(const IDEDMA *dma, int32_t limit)
  113. {
  114. BMDMAState *bm = DO_UPCAST(BMDMAState, dma, dma);
  115. IDEState *s = bmdma_active_if(bm);
  116. PCIDevice *pci_dev = PCI_DEVICE(bm->pci_dev);
  117. struct {
  118. uint32_t addr;
  119. uint32_t size;
  120. } prd;
  121. int l, len;
  122. pci_dma_sglist_init(&s->sg, pci_dev,
  123. s->nsector / (BMDMA_PAGE_SIZE / BDRV_SECTOR_SIZE) + 1);
  124. s->io_buffer_size = 0;
  125. for(;;) {
  126. if (bm->cur_prd_len == 0) {
  127. /* end of table (with a fail safe of one page) */
  128. if (bm->cur_prd_last ||
  129. (bm->cur_addr - bm->addr) >= BMDMA_PAGE_SIZE) {
  130. return s->sg.size;
  131. }
  132. pci_dma_read(pci_dev, bm->cur_addr, &prd, 8);
  133. bm->cur_addr += 8;
  134. prd.addr = le32_to_cpu(prd.addr);
  135. prd.size = le32_to_cpu(prd.size);
  136. len = prd.size & 0xfffe;
  137. if (len == 0)
  138. len = 0x10000;
  139. bm->cur_prd_len = len;
  140. bm->cur_prd_addr = prd.addr;
  141. bm->cur_prd_last = (prd.size & 0x80000000);
  142. }
  143. l = bm->cur_prd_len;
  144. if (l > 0) {
  145. uint64_t sg_len;
  146. /* Don't add extra bytes to the SGList; consume any remaining
  147. * PRDs from the guest, but ignore them. */
  148. sg_len = MIN(limit - s->sg.size, bm->cur_prd_len);
  149. if (sg_len) {
  150. qemu_sglist_add(&s->sg, bm->cur_prd_addr, sg_len);
  151. }
  152. bm->cur_prd_addr += l;
  153. bm->cur_prd_len -= l;
  154. s->io_buffer_size += l;
  155. }
  156. }
  157. qemu_sglist_destroy(&s->sg);
  158. s->io_buffer_size = 0;
  159. return -1;
  160. }
  161. /* return 0 if buffer completed */
  162. static int bmdma_rw_buf(const IDEDMA *dma, bool is_write)
  163. {
  164. BMDMAState *bm = DO_UPCAST(BMDMAState, dma, dma);
  165. IDEState *s = bmdma_active_if(bm);
  166. PCIDevice *pci_dev = PCI_DEVICE(bm->pci_dev);
  167. struct {
  168. uint32_t addr;
  169. uint32_t size;
  170. } prd;
  171. int l, len;
  172. for(;;) {
  173. l = s->io_buffer_size - s->io_buffer_index;
  174. if (l <= 0)
  175. break;
  176. if (bm->cur_prd_len == 0) {
  177. /* end of table (with a fail safe of one page) */
  178. if (bm->cur_prd_last ||
  179. (bm->cur_addr - bm->addr) >= BMDMA_PAGE_SIZE)
  180. return 0;
  181. pci_dma_read(pci_dev, bm->cur_addr, &prd, 8);
  182. bm->cur_addr += 8;
  183. prd.addr = le32_to_cpu(prd.addr);
  184. prd.size = le32_to_cpu(prd.size);
  185. len = prd.size & 0xfffe;
  186. if (len == 0)
  187. len = 0x10000;
  188. bm->cur_prd_len = len;
  189. bm->cur_prd_addr = prd.addr;
  190. bm->cur_prd_last = (prd.size & 0x80000000);
  191. }
  192. if (l > bm->cur_prd_len)
  193. l = bm->cur_prd_len;
  194. if (l > 0) {
  195. if (is_write) {
  196. pci_dma_write(pci_dev, bm->cur_prd_addr,
  197. s->io_buffer + s->io_buffer_index, l);
  198. } else {
  199. pci_dma_read(pci_dev, bm->cur_prd_addr,
  200. s->io_buffer + s->io_buffer_index, l);
  201. }
  202. bm->cur_prd_addr += l;
  203. bm->cur_prd_len -= l;
  204. s->io_buffer_index += l;
  205. }
  206. }
  207. return 1;
  208. }
  209. static void bmdma_set_inactive(const IDEDMA *dma, bool more)
  210. {
  211. BMDMAState *bm = DO_UPCAST(BMDMAState, dma, dma);
  212. bm->dma_cb = NULL;
  213. if (more) {
  214. bm->status |= BM_STATUS_DMAING;
  215. } else {
  216. bm->status &= ~BM_STATUS_DMAING;
  217. }
  218. }
  219. static void bmdma_restart_dma(const IDEDMA *dma)
  220. {
  221. BMDMAState *bm = DO_UPCAST(BMDMAState, dma, dma);
  222. bm->cur_addr = bm->addr;
  223. }
  224. static void bmdma_cancel(BMDMAState *bm)
  225. {
  226. if (bm->status & BM_STATUS_DMAING) {
  227. /* cancel DMA request */
  228. bmdma_set_inactive(&bm->dma, false);
  229. }
  230. }
  231. static void bmdma_reset(const IDEDMA *dma)
  232. {
  233. BMDMAState *bm = DO_UPCAST(BMDMAState, dma, dma);
  234. trace_bmdma_reset();
  235. bmdma_cancel(bm);
  236. bm->cmd = 0;
  237. bm->status = 0;
  238. bm->addr = 0;
  239. bm->cur_addr = 0;
  240. bm->cur_prd_last = 0;
  241. bm->cur_prd_addr = 0;
  242. bm->cur_prd_len = 0;
  243. }
  244. static void bmdma_irq(void *opaque, int n, int level)
  245. {
  246. BMDMAState *bm = opaque;
  247. if (!level) {
  248. /* pass through lower */
  249. qemu_set_irq(bm->irq, level);
  250. return;
  251. }
  252. bm->status |= BM_STATUS_INT;
  253. /* trigger the real irq */
  254. qemu_set_irq(bm->irq, level);
  255. }
  256. void bmdma_cmd_writeb(BMDMAState *bm, uint32_t val)
  257. {
  258. trace_bmdma_cmd_writeb(val);
  259. /* Ignore writes to SSBM if it keeps the old value */
  260. if ((val & BM_CMD_START) != (bm->cmd & BM_CMD_START)) {
  261. if (!(val & BM_CMD_START)) {
  262. ide_cancel_dma_sync(idebus_active_if(bm->bus));
  263. bm->status &= ~BM_STATUS_DMAING;
  264. } else {
  265. bm->cur_addr = bm->addr;
  266. if (!(bm->status & BM_STATUS_DMAING)) {
  267. bm->status |= BM_STATUS_DMAING;
  268. /* start dma transfer if possible */
  269. if (bm->dma_cb)
  270. bm->dma_cb(bmdma_active_if(bm), 0);
  271. }
  272. }
  273. }
  274. bm->cmd = val & 0x09;
  275. }
  276. static uint64_t bmdma_addr_read(void *opaque, hwaddr addr,
  277. unsigned width)
  278. {
  279. BMDMAState *bm = opaque;
  280. uint32_t mask = (1ULL << (width * 8)) - 1;
  281. uint64_t data;
  282. data = (bm->addr >> (addr * 8)) & mask;
  283. trace_bmdma_addr_read(data);
  284. return data;
  285. }
  286. static void bmdma_addr_write(void *opaque, hwaddr addr,
  287. uint64_t data, unsigned width)
  288. {
  289. BMDMAState *bm = opaque;
  290. int shift = addr * 8;
  291. uint32_t mask = (1ULL << (width * 8)) - 1;
  292. trace_bmdma_addr_write(data);
  293. bm->addr &= ~(mask << shift);
  294. bm->addr |= ((data & mask) << shift) & ~3;
  295. }
  296. MemoryRegionOps bmdma_addr_ioport_ops = {
  297. .read = bmdma_addr_read,
  298. .write = bmdma_addr_write,
  299. .endianness = DEVICE_LITTLE_ENDIAN,
  300. };
  301. static bool ide_bmdma_current_needed(void *opaque)
  302. {
  303. BMDMAState *bm = opaque;
  304. return (bm->cur_prd_len != 0);
  305. }
  306. static bool ide_bmdma_status_needed(void *opaque)
  307. {
  308. BMDMAState *bm = opaque;
  309. /* Older versions abused some bits in the status register for internal
  310. * error state. If any of these bits are set, we must add a subsection to
  311. * transfer the real status register */
  312. uint8_t abused_bits = BM_MIGRATION_COMPAT_STATUS_BITS;
  313. return ((bm->status & abused_bits) != 0);
  314. }
  315. static int ide_bmdma_pre_save(void *opaque)
  316. {
  317. BMDMAState *bm = opaque;
  318. uint8_t abused_bits = BM_MIGRATION_COMPAT_STATUS_BITS;
  319. if (!(bm->status & BM_STATUS_DMAING) && bm->dma_cb) {
  320. bm->bus->error_status =
  321. ide_dma_cmd_to_retry(bmdma_active_if(bm)->dma_cmd);
  322. }
  323. bm->migration_retry_unit = bm->bus->retry_unit;
  324. bm->migration_retry_sector_num = bm->bus->retry_sector_num;
  325. bm->migration_retry_nsector = bm->bus->retry_nsector;
  326. bm->migration_compat_status =
  327. (bm->status & ~abused_bits) | (bm->bus->error_status & abused_bits);
  328. return 0;
  329. }
  330. /* This function accesses bm->bus->error_status which is loaded only after
  331. * BMDMA itself. This is why the function is called from ide_pci_post_load
  332. * instead of being registered with VMState where it would run too early. */
  333. static int ide_bmdma_post_load(void *opaque, int version_id)
  334. {
  335. BMDMAState *bm = opaque;
  336. uint8_t abused_bits = BM_MIGRATION_COMPAT_STATUS_BITS;
  337. if (bm->status == 0) {
  338. bm->status = bm->migration_compat_status & ~abused_bits;
  339. bm->bus->error_status |= bm->migration_compat_status & abused_bits;
  340. }
  341. if (bm->bus->error_status) {
  342. bm->bus->retry_sector_num = bm->migration_retry_sector_num;
  343. bm->bus->retry_nsector = bm->migration_retry_nsector;
  344. bm->bus->retry_unit = bm->migration_retry_unit;
  345. }
  346. return 0;
  347. }
  348. static const VMStateDescription vmstate_bmdma_current = {
  349. .name = "ide bmdma_current",
  350. .version_id = 1,
  351. .minimum_version_id = 1,
  352. .needed = ide_bmdma_current_needed,
  353. .fields = (VMStateField[]) {
  354. VMSTATE_UINT32(cur_addr, BMDMAState),
  355. VMSTATE_UINT32(cur_prd_last, BMDMAState),
  356. VMSTATE_UINT32(cur_prd_addr, BMDMAState),
  357. VMSTATE_UINT32(cur_prd_len, BMDMAState),
  358. VMSTATE_END_OF_LIST()
  359. }
  360. };
  361. static const VMStateDescription vmstate_bmdma_status = {
  362. .name ="ide bmdma/status",
  363. .version_id = 1,
  364. .minimum_version_id = 1,
  365. .needed = ide_bmdma_status_needed,
  366. .fields = (VMStateField[]) {
  367. VMSTATE_UINT8(status, BMDMAState),
  368. VMSTATE_END_OF_LIST()
  369. }
  370. };
  371. static const VMStateDescription vmstate_bmdma = {
  372. .name = "ide bmdma",
  373. .version_id = 3,
  374. .minimum_version_id = 0,
  375. .pre_save = ide_bmdma_pre_save,
  376. .fields = (VMStateField[]) {
  377. VMSTATE_UINT8(cmd, BMDMAState),
  378. VMSTATE_UINT8(migration_compat_status, BMDMAState),
  379. VMSTATE_UINT32(addr, BMDMAState),
  380. VMSTATE_INT64(migration_retry_sector_num, BMDMAState),
  381. VMSTATE_UINT32(migration_retry_nsector, BMDMAState),
  382. VMSTATE_UINT8(migration_retry_unit, BMDMAState),
  383. VMSTATE_END_OF_LIST()
  384. },
  385. .subsections = (const VMStateDescription*[]) {
  386. &vmstate_bmdma_current,
  387. &vmstate_bmdma_status,
  388. NULL
  389. }
  390. };
  391. static int ide_pci_post_load(void *opaque, int version_id)
  392. {
  393. PCIIDEState *d = opaque;
  394. int i;
  395. for(i = 0; i < 2; i++) {
  396. /* current versions always store 0/1, but older version
  397. stored bigger values. We only need last bit */
  398. d->bmdma[i].migration_retry_unit &= 1;
  399. ide_bmdma_post_load(&d->bmdma[i], -1);
  400. }
  401. return 0;
  402. }
  403. const VMStateDescription vmstate_ide_pci = {
  404. .name = "ide",
  405. .version_id = 3,
  406. .minimum_version_id = 0,
  407. .post_load = ide_pci_post_load,
  408. .fields = (VMStateField[]) {
  409. VMSTATE_PCI_DEVICE(parent_obj, PCIIDEState),
  410. VMSTATE_STRUCT_ARRAY(bmdma, PCIIDEState, 2, 0,
  411. vmstate_bmdma, BMDMAState),
  412. VMSTATE_IDE_BUS_ARRAY(bus, PCIIDEState, 2),
  413. VMSTATE_IDE_DRIVES(bus[0].ifs, PCIIDEState),
  414. VMSTATE_IDE_DRIVES(bus[1].ifs, PCIIDEState),
  415. VMSTATE_END_OF_LIST()
  416. }
  417. };
  418. /* hd_table must contain 4 block drivers */
  419. void pci_ide_create_devs(PCIDevice *dev)
  420. {
  421. PCIIDEState *d = PCI_IDE(dev);
  422. DriveInfo *hd_table[2 * MAX_IDE_DEVS];
  423. static const int bus[4] = { 0, 0, 1, 1 };
  424. static const int unit[4] = { 0, 1, 0, 1 };
  425. int i;
  426. ide_drive_get(hd_table, ARRAY_SIZE(hd_table));
  427. for (i = 0; i < 4; i++) {
  428. if (hd_table[i]) {
  429. ide_create_drive(d->bus + bus[i], unit[i], hd_table[i]);
  430. }
  431. }
  432. }
  433. static const struct IDEDMAOps bmdma_ops = {
  434. .start_dma = bmdma_start_dma,
  435. .prepare_buf = bmdma_prepare_buf,
  436. .rw_buf = bmdma_rw_buf,
  437. .restart_dma = bmdma_restart_dma,
  438. .set_inactive = bmdma_set_inactive,
  439. .reset = bmdma_reset,
  440. };
  441. void bmdma_init(IDEBus *bus, BMDMAState *bm, PCIIDEState *d)
  442. {
  443. if (bus->dma == &bm->dma) {
  444. return;
  445. }
  446. bm->dma.ops = &bmdma_ops;
  447. bus->dma = &bm->dma;
  448. bm->irq = bus->irq;
  449. bus->irq = qemu_allocate_irq(bmdma_irq, bm, 0);
  450. bm->pci_dev = d;
  451. }
  452. static const TypeInfo pci_ide_type_info = {
  453. .name = TYPE_PCI_IDE,
  454. .parent = TYPE_PCI_DEVICE,
  455. .instance_size = sizeof(PCIIDEState),
  456. .abstract = true,
  457. .interfaces = (InterfaceInfo[]) {
  458. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  459. { },
  460. },
  461. };
  462. static void pci_ide_register_types(void)
  463. {
  464. type_register_static(&pci_ide_type_info);
  465. }
  466. type_init(pci_ide_register_types)