piix.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * QEMU IDE Emulation: PCI PIIX3/4 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. * References:
  26. * [1] 82371FB (PIIX) AND 82371SB (PIIX3) PCI ISA IDE XCELERATOR,
  27. * 290550-002, Intel Corporation, April 1997.
  28. */
  29. #include "qemu/osdep.h"
  30. #include "qapi/error.h"
  31. #include "hw/pci/pci.h"
  32. #include "hw/ide/piix.h"
  33. #include "hw/ide/pci.h"
  34. #include "ide-internal.h"
  35. #include "trace.h"
  36. static uint64_t bmdma_read(void *opaque, hwaddr addr, unsigned size)
  37. {
  38. BMDMAState *bm = opaque;
  39. uint32_t val;
  40. if (size != 1) {
  41. return ((uint64_t)1 << (size * 8)) - 1;
  42. }
  43. switch(addr & 3) {
  44. case 0:
  45. val = bm->cmd;
  46. break;
  47. case 2:
  48. val = bm->status;
  49. break;
  50. default:
  51. val = 0xff;
  52. break;
  53. }
  54. trace_bmdma_read(addr, val);
  55. return val;
  56. }
  57. static void bmdma_write(void *opaque, hwaddr addr,
  58. uint64_t val, unsigned size)
  59. {
  60. BMDMAState *bm = opaque;
  61. if (size != 1) {
  62. return;
  63. }
  64. trace_bmdma_write(addr, val);
  65. switch(addr & 3) {
  66. case 0:
  67. bmdma_cmd_writeb(bm, val);
  68. break;
  69. case 2:
  70. bmdma_status_writeb(bm, val);
  71. break;
  72. }
  73. }
  74. static const MemoryRegionOps piix_bmdma_ops = {
  75. .read = bmdma_read,
  76. .write = bmdma_write,
  77. };
  78. static void bmdma_setup_bar(PCIIDEState *d)
  79. {
  80. int i;
  81. memory_region_init(&d->bmdma_bar, OBJECT(d), "piix-bmdma-container", 16);
  82. for(i = 0;i < 2; i++) {
  83. BMDMAState *bm = &d->bmdma[i];
  84. memory_region_init_io(&bm->extra_io, OBJECT(d), &piix_bmdma_ops, bm,
  85. "piix-bmdma", 4);
  86. memory_region_add_subregion(&d->bmdma_bar, i * 8, &bm->extra_io);
  87. memory_region_init_io(&bm->addr_ioport, OBJECT(d),
  88. &bmdma_addr_ioport_ops, bm, "bmdma", 4);
  89. memory_region_add_subregion(&d->bmdma_bar, i * 8 + 4, &bm->addr_ioport);
  90. }
  91. }
  92. static void piix_ide_reset(DeviceState *dev)
  93. {
  94. PCIIDEState *d = PCI_IDE(dev);
  95. PCIDevice *pd = PCI_DEVICE(d);
  96. uint8_t *pci_conf = pd->config;
  97. int i;
  98. for (i = 0; i < 2; i++) {
  99. ide_bus_reset(&d->bus[i]);
  100. }
  101. /* PCI command register default value (0000h) per [1, p.48]. */
  102. pci_set_word(pci_conf + PCI_COMMAND, 0x0000);
  103. pci_set_word(pci_conf + PCI_STATUS,
  104. PCI_STATUS_DEVSEL_MEDIUM | PCI_STATUS_FAST_BACK);
  105. pci_set_long(pci_conf + 0x20, 0x1); /* BMIBA: 20-23h */
  106. }
  107. static bool pci_piix_init_bus(PCIIDEState *d, unsigned i, Error **errp)
  108. {
  109. static const struct {
  110. int iobase;
  111. int iobase2;
  112. int isairq;
  113. } port_info[] = {
  114. {0x1f0, 0x3f6, 14},
  115. {0x170, 0x376, 15},
  116. };
  117. int ret;
  118. ide_bus_init(&d->bus[i], sizeof(d->bus[i]), DEVICE(d), i, 2);
  119. ret = ide_init_ioport(&d->bus[i], NULL, port_info[i].iobase,
  120. port_info[i].iobase2);
  121. if (ret) {
  122. error_setg_errno(errp, -ret, "Failed to realize %s port %u",
  123. object_get_typename(OBJECT(d)), i);
  124. return false;
  125. }
  126. ide_bus_init_output_irq(&d->bus[i], isa_get_irq(NULL, port_info[i].isairq));
  127. bmdma_init(&d->bus[i], &d->bmdma[i], d);
  128. ide_bus_register_restart_cb(&d->bus[i]);
  129. return true;
  130. }
  131. static void pci_piix_ide_realize(PCIDevice *dev, Error **errp)
  132. {
  133. PCIIDEState *d = PCI_IDE(dev);
  134. uint8_t *pci_conf = dev->config;
  135. pci_conf[PCI_CLASS_PROG] = 0x80; // legacy ATA mode
  136. bmdma_setup_bar(d);
  137. pci_register_bar(dev, 4, PCI_BASE_ADDRESS_SPACE_IO, &d->bmdma_bar);
  138. for (unsigned i = 0; i < 2; i++) {
  139. if (!pci_piix_init_bus(d, i, errp)) {
  140. return;
  141. }
  142. }
  143. }
  144. static void pci_piix_ide_exitfn(PCIDevice *dev)
  145. {
  146. PCIIDEState *d = PCI_IDE(dev);
  147. unsigned i;
  148. for (i = 0; i < 2; ++i) {
  149. memory_region_del_subregion(&d->bmdma_bar, &d->bmdma[i].extra_io);
  150. memory_region_del_subregion(&d->bmdma_bar, &d->bmdma[i].addr_ioport);
  151. }
  152. }
  153. /* NOTE: for the PIIX3, the IRQs and IOports are hardcoded */
  154. static void piix3_ide_class_init(ObjectClass *klass, void *data)
  155. {
  156. DeviceClass *dc = DEVICE_CLASS(klass);
  157. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  158. device_class_set_legacy_reset(dc, piix_ide_reset);
  159. dc->vmsd = &vmstate_ide_pci;
  160. k->realize = pci_piix_ide_realize;
  161. k->exit = pci_piix_ide_exitfn;
  162. k->vendor_id = PCI_VENDOR_ID_INTEL;
  163. k->device_id = PCI_DEVICE_ID_INTEL_82371SB_1;
  164. k->class_id = PCI_CLASS_STORAGE_IDE;
  165. set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
  166. dc->hotpluggable = false;
  167. }
  168. static const TypeInfo piix3_ide_info = {
  169. .name = TYPE_PIIX3_IDE,
  170. .parent = TYPE_PCI_IDE,
  171. .class_init = piix3_ide_class_init,
  172. };
  173. /* NOTE: for the PIIX4, the IRQs and IOports are hardcoded */
  174. static void piix4_ide_class_init(ObjectClass *klass, void *data)
  175. {
  176. DeviceClass *dc = DEVICE_CLASS(klass);
  177. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  178. device_class_set_legacy_reset(dc, piix_ide_reset);
  179. dc->vmsd = &vmstate_ide_pci;
  180. k->realize = pci_piix_ide_realize;
  181. k->exit = pci_piix_ide_exitfn;
  182. k->vendor_id = PCI_VENDOR_ID_INTEL;
  183. k->device_id = PCI_DEVICE_ID_INTEL_82371AB;
  184. k->class_id = PCI_CLASS_STORAGE_IDE;
  185. set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
  186. dc->hotpluggable = false;
  187. }
  188. static const TypeInfo piix4_ide_info = {
  189. .name = TYPE_PIIX4_IDE,
  190. .parent = TYPE_PCI_IDE,
  191. .class_init = piix4_ide_class_init,
  192. };
  193. static void piix_ide_register_types(void)
  194. {
  195. type_register_static(&piix3_ide_info);
  196. type_register_static(&piix4_ide_info);
  197. }
  198. type_init(piix_ide_register_types)