2
0

pci.c 15 KB

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