2
0

piix_pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * QEMU i440FX/PIIX3 PCI Bridge Emulation
  3. *
  4. * Copyright (c) 2006 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "hw.h"
  25. #include "pc.h"
  26. #include "pci.h"
  27. typedef uint32_t pci_addr_t;
  28. #include "pci_host.h"
  29. typedef PCIHostState I440FXState;
  30. static void i440fx_addr_writel(void* opaque, uint32_t addr, uint32_t val)
  31. {
  32. I440FXState *s = opaque;
  33. s->config_reg = val;
  34. }
  35. static uint32_t i440fx_addr_readl(void* opaque, uint32_t addr)
  36. {
  37. I440FXState *s = opaque;
  38. return s->config_reg;
  39. }
  40. static void piix3_set_irq(qemu_irq *pic, int irq_num, int level);
  41. /* return the global irq number corresponding to a given device irq
  42. pin. We could also use the bus number to have a more precise
  43. mapping. */
  44. static int pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
  45. {
  46. int slot_addend;
  47. slot_addend = (pci_dev->devfn >> 3) - 1;
  48. return (irq_num + slot_addend) & 3;
  49. }
  50. static target_phys_addr_t isa_page_descs[384 / 4];
  51. static uint8_t smm_enabled;
  52. static int pci_irq_levels[4];
  53. static void update_pam(PCIDevice *d, uint32_t start, uint32_t end, int r)
  54. {
  55. uint32_t addr;
  56. // printf("ISA mapping %08x-0x%08x: %d\n", start, end, r);
  57. switch(r) {
  58. case 3:
  59. /* RAM */
  60. cpu_register_physical_memory(start, end - start,
  61. start);
  62. break;
  63. case 1:
  64. /* ROM (XXX: not quite correct) */
  65. cpu_register_physical_memory(start, end - start,
  66. start | IO_MEM_ROM);
  67. break;
  68. case 2:
  69. case 0:
  70. /* XXX: should distinguish read/write cases */
  71. for(addr = start; addr < end; addr += 4096) {
  72. cpu_register_physical_memory(addr, 4096,
  73. isa_page_descs[(addr - 0xa0000) >> 12]);
  74. }
  75. break;
  76. }
  77. }
  78. static void i440fx_update_memory_mappings(PCIDevice *d)
  79. {
  80. int i, r;
  81. uint32_t smram, addr;
  82. update_pam(d, 0xf0000, 0x100000, (d->config[0x59] >> 4) & 3);
  83. for(i = 0; i < 12; i++) {
  84. r = (d->config[(i >> 1) + 0x5a] >> ((i & 1) * 4)) & 3;
  85. update_pam(d, 0xc0000 + 0x4000 * i, 0xc0000 + 0x4000 * (i + 1), r);
  86. }
  87. smram = d->config[0x72];
  88. if ((smm_enabled && (smram & 0x08)) || (smram & 0x40)) {
  89. cpu_register_physical_memory(0xa0000, 0x20000, 0xa0000);
  90. } else {
  91. for(addr = 0xa0000; addr < 0xc0000; addr += 4096) {
  92. cpu_register_physical_memory(addr, 4096,
  93. isa_page_descs[(addr - 0xa0000) >> 12]);
  94. }
  95. }
  96. }
  97. void i440fx_set_smm(PCIDevice *d, int val)
  98. {
  99. val = (val != 0);
  100. if (smm_enabled != val) {
  101. smm_enabled = val;
  102. i440fx_update_memory_mappings(d);
  103. }
  104. }
  105. /* XXX: suppress when better memory API. We make the assumption that
  106. no device (in particular the VGA) changes the memory mappings in
  107. the 0xa0000-0x100000 range */
  108. void i440fx_init_memory_mappings(PCIDevice *d)
  109. {
  110. int i;
  111. for(i = 0; i < 96; i++) {
  112. isa_page_descs[i] = cpu_get_physical_page_desc(0xa0000 + i * 0x1000);
  113. }
  114. }
  115. static void i440fx_write_config(PCIDevice *d,
  116. uint32_t address, uint32_t val, int len)
  117. {
  118. /* XXX: implement SMRAM.D_LOCK */
  119. pci_default_write_config(d, address, val, len);
  120. if ((address >= 0x59 && address <= 0x5f) || address == 0x72)
  121. i440fx_update_memory_mappings(d);
  122. }
  123. static void i440fx_save(QEMUFile* f, void *opaque)
  124. {
  125. PCIDevice *d = opaque;
  126. int i;
  127. pci_device_save(d, f);
  128. qemu_put_8s(f, &smm_enabled);
  129. for (i = 0; i < 4; i++)
  130. qemu_put_be32(f, pci_irq_levels[i]);
  131. }
  132. static int i440fx_load(QEMUFile* f, void *opaque, int version_id)
  133. {
  134. PCIDevice *d = opaque;
  135. int ret, i;
  136. if (version_id > 2)
  137. return -EINVAL;
  138. ret = pci_device_load(d, f);
  139. if (ret < 0)
  140. return ret;
  141. i440fx_update_memory_mappings(d);
  142. qemu_get_8s(f, &smm_enabled);
  143. if (version_id >= 2)
  144. for (i = 0; i < 4; i++)
  145. pci_irq_levels[i] = qemu_get_be32(f);
  146. return 0;
  147. }
  148. PCIBus *i440fx_init(PCIDevice **pi440fx_state, qemu_irq *pic)
  149. {
  150. PCIBus *b;
  151. PCIDevice *d;
  152. I440FXState *s;
  153. s = qemu_mallocz(sizeof(I440FXState));
  154. b = pci_register_bus(piix3_set_irq, pci_slot_get_pirq, pic, 0, 4);
  155. s->bus = b;
  156. register_ioport_write(0xcf8, 4, 4, i440fx_addr_writel, s);
  157. register_ioport_read(0xcf8, 4, 4, i440fx_addr_readl, s);
  158. register_ioport_write(0xcfc, 4, 1, pci_host_data_writeb, s);
  159. register_ioport_write(0xcfc, 4, 2, pci_host_data_writew, s);
  160. register_ioport_write(0xcfc, 4, 4, pci_host_data_writel, s);
  161. register_ioport_read(0xcfc, 4, 1, pci_host_data_readb, s);
  162. register_ioport_read(0xcfc, 4, 2, pci_host_data_readw, s);
  163. register_ioport_read(0xcfc, 4, 4, pci_host_data_readl, s);
  164. d = pci_register_device(b, "i440FX", sizeof(PCIDevice), 0,
  165. NULL, i440fx_write_config);
  166. pci_config_set_vendor_id(d->config, PCI_VENDOR_ID_INTEL);
  167. pci_config_set_device_id(d->config, PCI_DEVICE_ID_INTEL_82441);
  168. d->config[0x08] = 0x02; // revision
  169. pci_config_set_class(d->config, PCI_CLASS_BRIDGE_HOST);
  170. d->config[0x0e] = 0x00; // header_type
  171. d->config[0x72] = 0x02; /* SMRAM */
  172. register_savevm("I440FX", 0, 2, i440fx_save, i440fx_load, d);
  173. *pi440fx_state = d;
  174. return b;
  175. }
  176. /* PIIX3 PCI to ISA bridge */
  177. static PCIDevice *piix3_dev;
  178. PCIDevice *piix4_dev;
  179. /* just used for simpler irq handling. */
  180. #define PCI_IRQ_WORDS ((PCI_DEVICES_MAX + 31) / 32)
  181. static void piix3_set_irq(qemu_irq *pic, int irq_num, int level)
  182. {
  183. int i, pic_irq, pic_level;
  184. pci_irq_levels[irq_num] = level;
  185. /* now we change the pic irq level according to the piix irq mappings */
  186. /* XXX: optimize */
  187. pic_irq = piix3_dev->config[0x60 + irq_num];
  188. if (pic_irq < 16) {
  189. /* The pic level is the logical OR of all the PCI irqs mapped
  190. to it */
  191. pic_level = 0;
  192. for (i = 0; i < 4; i++) {
  193. if (pic_irq == piix3_dev->config[0x60 + i])
  194. pic_level |= pci_irq_levels[i];
  195. }
  196. qemu_set_irq(pic[pic_irq], pic_level);
  197. }
  198. }
  199. static void piix3_reset(PCIDevice *d)
  200. {
  201. uint8_t *pci_conf = d->config;
  202. pci_conf[0x04] = 0x07; // master, memory and I/O
  203. pci_conf[0x05] = 0x00;
  204. pci_conf[0x06] = 0x00;
  205. pci_conf[0x07] = 0x02; // PCI_status_devsel_medium
  206. pci_conf[0x4c] = 0x4d;
  207. pci_conf[0x4e] = 0x03;
  208. pci_conf[0x4f] = 0x00;
  209. pci_conf[0x60] = 0x80;
  210. pci_conf[0x61] = 0x80;
  211. pci_conf[0x62] = 0x80;
  212. pci_conf[0x63] = 0x80;
  213. pci_conf[0x69] = 0x02;
  214. pci_conf[0x70] = 0x80;
  215. pci_conf[0x76] = 0x0c;
  216. pci_conf[0x77] = 0x0c;
  217. pci_conf[0x78] = 0x02;
  218. pci_conf[0x79] = 0x00;
  219. pci_conf[0x80] = 0x00;
  220. pci_conf[0x82] = 0x00;
  221. pci_conf[0xa0] = 0x08;
  222. pci_conf[0xa2] = 0x00;
  223. pci_conf[0xa3] = 0x00;
  224. pci_conf[0xa4] = 0x00;
  225. pci_conf[0xa5] = 0x00;
  226. pci_conf[0xa6] = 0x00;
  227. pci_conf[0xa7] = 0x00;
  228. pci_conf[0xa8] = 0x0f;
  229. pci_conf[0xaa] = 0x00;
  230. pci_conf[0xab] = 0x00;
  231. pci_conf[0xac] = 0x00;
  232. pci_conf[0xae] = 0x00;
  233. }
  234. static void piix4_reset(PCIDevice *d)
  235. {
  236. uint8_t *pci_conf = d->config;
  237. pci_conf[0x04] = 0x07; // master, memory and I/O
  238. pci_conf[0x05] = 0x00;
  239. pci_conf[0x06] = 0x00;
  240. pci_conf[0x07] = 0x02; // PCI_status_devsel_medium
  241. pci_conf[0x4c] = 0x4d;
  242. pci_conf[0x4e] = 0x03;
  243. pci_conf[0x4f] = 0x00;
  244. pci_conf[0x60] = 0x0a; // PCI A -> IRQ 10
  245. pci_conf[0x61] = 0x0a; // PCI B -> IRQ 10
  246. pci_conf[0x62] = 0x0b; // PCI C -> IRQ 11
  247. pci_conf[0x63] = 0x0b; // PCI D -> IRQ 11
  248. pci_conf[0x69] = 0x02;
  249. pci_conf[0x70] = 0x80;
  250. pci_conf[0x76] = 0x0c;
  251. pci_conf[0x77] = 0x0c;
  252. pci_conf[0x78] = 0x02;
  253. pci_conf[0x79] = 0x00;
  254. pci_conf[0x80] = 0x00;
  255. pci_conf[0x82] = 0x00;
  256. pci_conf[0xa0] = 0x08;
  257. pci_conf[0xa2] = 0x00;
  258. pci_conf[0xa3] = 0x00;
  259. pci_conf[0xa4] = 0x00;
  260. pci_conf[0xa5] = 0x00;
  261. pci_conf[0xa6] = 0x00;
  262. pci_conf[0xa7] = 0x00;
  263. pci_conf[0xa8] = 0x0f;
  264. pci_conf[0xaa] = 0x00;
  265. pci_conf[0xab] = 0x00;
  266. pci_conf[0xac] = 0x00;
  267. pci_conf[0xae] = 0x00;
  268. }
  269. static void piix_save(QEMUFile* f, void *opaque)
  270. {
  271. PCIDevice *d = opaque;
  272. pci_device_save(d, f);
  273. }
  274. static int piix_load(QEMUFile* f, void *opaque, int version_id)
  275. {
  276. PCIDevice *d = opaque;
  277. if (version_id != 2)
  278. return -EINVAL;
  279. return pci_device_load(d, f);
  280. }
  281. int piix3_init(PCIBus *bus, int devfn)
  282. {
  283. PCIDevice *d;
  284. uint8_t *pci_conf;
  285. d = pci_register_device(bus, "PIIX3", sizeof(PCIDevice),
  286. devfn, NULL, NULL);
  287. register_savevm("PIIX3", 0, 2, piix_save, piix_load, d);
  288. piix3_dev = d;
  289. pci_conf = d->config;
  290. pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
  291. pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82371SB_0); // 82371SB PIIX3 PCI-to-ISA bridge (Step A1)
  292. pci_config_set_class(pci_conf, PCI_CLASS_BRIDGE_ISA);
  293. pci_conf[0x0e] = 0x80; // header_type = PCI_multifunction, generic
  294. piix3_reset(d);
  295. return d->devfn;
  296. }
  297. int piix4_init(PCIBus *bus, int devfn)
  298. {
  299. PCIDevice *d;
  300. uint8_t *pci_conf;
  301. d = pci_register_device(bus, "PIIX4", sizeof(PCIDevice),
  302. devfn, NULL, NULL);
  303. register_savevm("PIIX4", 0, 2, piix_save, piix_load, d);
  304. piix4_dev = d;
  305. pci_conf = d->config;
  306. pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
  307. pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82371AB_0); // 82371AB/EB/MB PIIX4 PCI-to-ISA bridge
  308. pci_config_set_class(pci_conf, PCI_CLASS_BRIDGE_ISA);
  309. pci_conf[0x0e] = 0x80; // header_type = PCI_multifunction, generic
  310. piix4_reset(d);
  311. return d->devfn;
  312. }