i440fx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * QEMU i440FX 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 "qemu/osdep.h"
  25. #include "qemu/units.h"
  26. #include "qemu/range.h"
  27. #include "hw/i386/pc.h"
  28. #include "hw/pci/pci.h"
  29. #include "hw/pci/pci_host.h"
  30. #include "hw/pci-host/i440fx.h"
  31. #include "hw/qdev-properties.h"
  32. #include "hw/sysbus.h"
  33. #include "qapi/error.h"
  34. #include "migration/vmstate.h"
  35. #include "qapi/visitor.h"
  36. #include "qemu/error-report.h"
  37. /*
  38. * I440FX chipset data sheet.
  39. * https://wiki.qemu.org/File:29054901.pdf
  40. */
  41. #define I440FX_PCI_HOST_BRIDGE(obj) \
  42. OBJECT_CHECK(I440FXState, (obj), TYPE_I440FX_PCI_HOST_BRIDGE)
  43. typedef struct I440FXState {
  44. PCIHostState parent_obj;
  45. Range pci_hole;
  46. uint64_t pci_hole64_size;
  47. bool pci_hole64_fix;
  48. uint32_t short_root_bus;
  49. } I440FXState;
  50. #define I440FX_PAM 0x59
  51. #define I440FX_PAM_SIZE 7
  52. #define I440FX_SMRAM 0x72
  53. /* Keep it 2G to comply with older win32 guests */
  54. #define I440FX_PCI_HOST_HOLE64_SIZE_DEFAULT (1ULL << 31)
  55. /* Older coreboot versions (4.0 and older) read a config register that doesn't
  56. * exist in real hardware, to get the RAM size from QEMU.
  57. */
  58. #define I440FX_COREBOOT_RAM_SIZE 0x57
  59. static void i440fx_update_memory_mappings(PCII440FXState *d)
  60. {
  61. int i;
  62. PCIDevice *pd = PCI_DEVICE(d);
  63. memory_region_transaction_begin();
  64. for (i = 0; i < ARRAY_SIZE(d->pam_regions); i++) {
  65. pam_update(&d->pam_regions[i], i,
  66. pd->config[I440FX_PAM + DIV_ROUND_UP(i, 2)]);
  67. }
  68. memory_region_set_enabled(&d->smram_region,
  69. !(pd->config[I440FX_SMRAM] & SMRAM_D_OPEN));
  70. memory_region_set_enabled(&d->smram,
  71. pd->config[I440FX_SMRAM] & SMRAM_G_SMRAME);
  72. memory_region_transaction_commit();
  73. }
  74. static void i440fx_write_config(PCIDevice *dev,
  75. uint32_t address, uint32_t val, int len)
  76. {
  77. PCII440FXState *d = I440FX_PCI_DEVICE(dev);
  78. /* XXX: implement SMRAM.D_LOCK */
  79. pci_default_write_config(dev, address, val, len);
  80. if (ranges_overlap(address, len, I440FX_PAM, I440FX_PAM_SIZE) ||
  81. range_covers_byte(address, len, I440FX_SMRAM)) {
  82. i440fx_update_memory_mappings(d);
  83. }
  84. }
  85. static int i440fx_post_load(void *opaque, int version_id)
  86. {
  87. PCII440FXState *d = opaque;
  88. i440fx_update_memory_mappings(d);
  89. return 0;
  90. }
  91. static const VMStateDescription vmstate_i440fx = {
  92. .name = "I440FX",
  93. .version_id = 3,
  94. .minimum_version_id = 3,
  95. .post_load = i440fx_post_load,
  96. .fields = (VMStateField[]) {
  97. VMSTATE_PCI_DEVICE(parent_obj, PCII440FXState),
  98. /* Used to be smm_enabled, which was basically always zero because
  99. * SeaBIOS hardly uses SMM. SMRAM is now handled by CPU code.
  100. */
  101. VMSTATE_UNUSED(1),
  102. VMSTATE_END_OF_LIST()
  103. }
  104. };
  105. static void i440fx_pcihost_get_pci_hole_start(Object *obj, Visitor *v,
  106. const char *name, void *opaque,
  107. Error **errp)
  108. {
  109. I440FXState *s = I440FX_PCI_HOST_BRIDGE(obj);
  110. uint64_t val64;
  111. uint32_t value;
  112. val64 = range_is_empty(&s->pci_hole) ? 0 : range_lob(&s->pci_hole);
  113. value = val64;
  114. assert(value == val64);
  115. visit_type_uint32(v, name, &value, errp);
  116. }
  117. static void i440fx_pcihost_get_pci_hole_end(Object *obj, Visitor *v,
  118. const char *name, void *opaque,
  119. Error **errp)
  120. {
  121. I440FXState *s = I440FX_PCI_HOST_BRIDGE(obj);
  122. uint64_t val64;
  123. uint32_t value;
  124. val64 = range_is_empty(&s->pci_hole) ? 0 : range_upb(&s->pci_hole) + 1;
  125. value = val64;
  126. assert(value == val64);
  127. visit_type_uint32(v, name, &value, errp);
  128. }
  129. /*
  130. * The 64bit PCI hole start is set by the Guest firmware
  131. * as the address of the first 64bit PCI MEM resource.
  132. * If no PCI device has resources on the 64bit area,
  133. * the 64bit PCI hole will start after "over 4G RAM" and the
  134. * reserved space for memory hotplug if any.
  135. */
  136. static uint64_t i440fx_pcihost_get_pci_hole64_start_value(Object *obj)
  137. {
  138. PCIHostState *h = PCI_HOST_BRIDGE(obj);
  139. I440FXState *s = I440FX_PCI_HOST_BRIDGE(obj);
  140. Range w64;
  141. uint64_t value;
  142. pci_bus_get_w64_range(h->bus, &w64);
  143. value = range_is_empty(&w64) ? 0 : range_lob(&w64);
  144. if (!value && s->pci_hole64_fix) {
  145. value = pc_pci_hole64_start();
  146. }
  147. return value;
  148. }
  149. static void i440fx_pcihost_get_pci_hole64_start(Object *obj, Visitor *v,
  150. const char *name,
  151. void *opaque, Error **errp)
  152. {
  153. uint64_t hole64_start = i440fx_pcihost_get_pci_hole64_start_value(obj);
  154. visit_type_uint64(v, name, &hole64_start, errp);
  155. }
  156. /*
  157. * The 64bit PCI hole end is set by the Guest firmware
  158. * as the address of the last 64bit PCI MEM resource.
  159. * Then it is expanded to the PCI_HOST_PROP_PCI_HOLE64_SIZE
  160. * that can be configured by the user.
  161. */
  162. static void i440fx_pcihost_get_pci_hole64_end(Object *obj, Visitor *v,
  163. const char *name, void *opaque,
  164. Error **errp)
  165. {
  166. PCIHostState *h = PCI_HOST_BRIDGE(obj);
  167. I440FXState *s = I440FX_PCI_HOST_BRIDGE(obj);
  168. uint64_t hole64_start = i440fx_pcihost_get_pci_hole64_start_value(obj);
  169. Range w64;
  170. uint64_t value, hole64_end;
  171. pci_bus_get_w64_range(h->bus, &w64);
  172. value = range_is_empty(&w64) ? 0 : range_upb(&w64) + 1;
  173. hole64_end = ROUND_UP(hole64_start + s->pci_hole64_size, 1ULL << 30);
  174. if (s->pci_hole64_fix && value < hole64_end) {
  175. value = hole64_end;
  176. }
  177. visit_type_uint64(v, name, &value, errp);
  178. }
  179. static void i440fx_pcihost_initfn(Object *obj)
  180. {
  181. PCIHostState *s = PCI_HOST_BRIDGE(obj);
  182. memory_region_init_io(&s->conf_mem, obj, &pci_host_conf_le_ops, s,
  183. "pci-conf-idx", 4);
  184. memory_region_init_io(&s->data_mem, obj, &pci_host_data_le_ops, s,
  185. "pci-conf-data", 4);
  186. object_property_add(obj, PCI_HOST_PROP_PCI_HOLE_START, "uint32",
  187. i440fx_pcihost_get_pci_hole_start,
  188. NULL, NULL, NULL);
  189. object_property_add(obj, PCI_HOST_PROP_PCI_HOLE_END, "uint32",
  190. i440fx_pcihost_get_pci_hole_end,
  191. NULL, NULL, NULL);
  192. object_property_add(obj, PCI_HOST_PROP_PCI_HOLE64_START, "uint64",
  193. i440fx_pcihost_get_pci_hole64_start,
  194. NULL, NULL, NULL);
  195. object_property_add(obj, PCI_HOST_PROP_PCI_HOLE64_END, "uint64",
  196. i440fx_pcihost_get_pci_hole64_end,
  197. NULL, NULL, NULL);
  198. }
  199. static void i440fx_pcihost_realize(DeviceState *dev, Error **errp)
  200. {
  201. PCIHostState *s = PCI_HOST_BRIDGE(dev);
  202. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  203. sysbus_add_io(sbd, 0xcf8, &s->conf_mem);
  204. sysbus_init_ioports(sbd, 0xcf8, 4);
  205. sysbus_add_io(sbd, 0xcfc, &s->data_mem);
  206. sysbus_init_ioports(sbd, 0xcfc, 4);
  207. /* register i440fx 0xcf8 port as coalesced pio */
  208. memory_region_set_flush_coalesced(&s->data_mem);
  209. memory_region_add_coalescing(&s->conf_mem, 0, 4);
  210. }
  211. static void i440fx_realize(PCIDevice *dev, Error **errp)
  212. {
  213. dev->config[I440FX_SMRAM] = 0x02;
  214. if (object_property_get_bool(qdev_get_machine(), "iommu", NULL)) {
  215. warn_report("i440fx doesn't support emulated iommu");
  216. }
  217. }
  218. PCIBus *i440fx_init(const char *host_type, const char *pci_type,
  219. PCII440FXState **pi440fx_state,
  220. MemoryRegion *address_space_mem,
  221. MemoryRegion *address_space_io,
  222. ram_addr_t ram_size,
  223. ram_addr_t below_4g_mem_size,
  224. ram_addr_t above_4g_mem_size,
  225. MemoryRegion *pci_address_space,
  226. MemoryRegion *ram_memory)
  227. {
  228. DeviceState *dev;
  229. PCIBus *b;
  230. PCIDevice *d;
  231. PCIHostState *s;
  232. PCII440FXState *f;
  233. unsigned i;
  234. I440FXState *i440fx;
  235. dev = qdev_new(host_type);
  236. s = PCI_HOST_BRIDGE(dev);
  237. b = pci_root_bus_new(dev, NULL, pci_address_space,
  238. address_space_io, 0, TYPE_PCI_BUS);
  239. s->bus = b;
  240. object_property_add_child(qdev_get_machine(), "i440fx", OBJECT(dev));
  241. sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
  242. d = pci_create_simple(b, 0, pci_type);
  243. *pi440fx_state = I440FX_PCI_DEVICE(d);
  244. f = *pi440fx_state;
  245. f->system_memory = address_space_mem;
  246. f->pci_address_space = pci_address_space;
  247. f->ram_memory = ram_memory;
  248. i440fx = I440FX_PCI_HOST_BRIDGE(dev);
  249. range_set_bounds(&i440fx->pci_hole, below_4g_mem_size,
  250. IO_APIC_DEFAULT_ADDRESS - 1);
  251. /* setup pci memory mapping */
  252. pc_pci_as_mapping_init(OBJECT(f), f->system_memory,
  253. f->pci_address_space);
  254. /* if *disabled* show SMRAM to all CPUs */
  255. memory_region_init_alias(&f->smram_region, OBJECT(d), "smram-region",
  256. f->pci_address_space, 0xa0000, 0x20000);
  257. memory_region_add_subregion_overlap(f->system_memory, 0xa0000,
  258. &f->smram_region, 1);
  259. memory_region_set_enabled(&f->smram_region, true);
  260. /* smram, as seen by SMM CPUs */
  261. memory_region_init(&f->smram, OBJECT(d), "smram", 4 * GiB);
  262. memory_region_set_enabled(&f->smram, true);
  263. memory_region_init_alias(&f->low_smram, OBJECT(d), "smram-low",
  264. f->ram_memory, 0xa0000, 0x20000);
  265. memory_region_set_enabled(&f->low_smram, true);
  266. memory_region_add_subregion(&f->smram, 0xa0000, &f->low_smram);
  267. object_property_add_const_link(qdev_get_machine(), "smram",
  268. OBJECT(&f->smram));
  269. init_pam(dev, f->ram_memory, f->system_memory, f->pci_address_space,
  270. &f->pam_regions[0], PAM_BIOS_BASE, PAM_BIOS_SIZE);
  271. for (i = 0; i < ARRAY_SIZE(f->pam_regions) - 1; ++i) {
  272. init_pam(dev, f->ram_memory, f->system_memory, f->pci_address_space,
  273. &f->pam_regions[i+1], PAM_EXPAN_BASE + i * PAM_EXPAN_SIZE,
  274. PAM_EXPAN_SIZE);
  275. }
  276. ram_size = ram_size / 8 / 1024 / 1024;
  277. if (ram_size > 255) {
  278. ram_size = 255;
  279. }
  280. d->config[I440FX_COREBOOT_RAM_SIZE] = ram_size;
  281. i440fx_update_memory_mappings(f);
  282. return b;
  283. }
  284. PCIBus *find_i440fx(void)
  285. {
  286. PCIHostState *s = OBJECT_CHECK(PCIHostState,
  287. object_resolve_path("/machine/i440fx", NULL),
  288. TYPE_PCI_HOST_BRIDGE);
  289. return s ? s->bus : NULL;
  290. }
  291. static void i440fx_class_init(ObjectClass *klass, void *data)
  292. {
  293. DeviceClass *dc = DEVICE_CLASS(klass);
  294. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  295. k->realize = i440fx_realize;
  296. k->config_write = i440fx_write_config;
  297. k->vendor_id = PCI_VENDOR_ID_INTEL;
  298. k->device_id = PCI_DEVICE_ID_INTEL_82441;
  299. k->revision = 0x02;
  300. k->class_id = PCI_CLASS_BRIDGE_HOST;
  301. dc->desc = "Host bridge";
  302. dc->vmsd = &vmstate_i440fx;
  303. /*
  304. * PCI-facing part of the host bridge, not usable without the
  305. * host-facing part, which can't be device_add'ed, yet.
  306. */
  307. dc->user_creatable = false;
  308. dc->hotpluggable = false;
  309. }
  310. static const TypeInfo i440fx_info = {
  311. .name = TYPE_I440FX_PCI_DEVICE,
  312. .parent = TYPE_PCI_DEVICE,
  313. .instance_size = sizeof(PCII440FXState),
  314. .class_init = i440fx_class_init,
  315. .interfaces = (InterfaceInfo[]) {
  316. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  317. { },
  318. },
  319. };
  320. static const char *i440fx_pcihost_root_bus_path(PCIHostState *host_bridge,
  321. PCIBus *rootbus)
  322. {
  323. I440FXState *s = I440FX_PCI_HOST_BRIDGE(host_bridge);
  324. /* For backwards compat with old device paths */
  325. if (s->short_root_bus) {
  326. return "0000";
  327. }
  328. return "0000:00";
  329. }
  330. static Property i440fx_props[] = {
  331. DEFINE_PROP_SIZE(PCI_HOST_PROP_PCI_HOLE64_SIZE, I440FXState,
  332. pci_hole64_size, I440FX_PCI_HOST_HOLE64_SIZE_DEFAULT),
  333. DEFINE_PROP_UINT32("short_root_bus", I440FXState, short_root_bus, 0),
  334. DEFINE_PROP_BOOL("x-pci-hole64-fix", I440FXState, pci_hole64_fix, true),
  335. DEFINE_PROP_END_OF_LIST(),
  336. };
  337. static void i440fx_pcihost_class_init(ObjectClass *klass, void *data)
  338. {
  339. DeviceClass *dc = DEVICE_CLASS(klass);
  340. PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(klass);
  341. hc->root_bus_path = i440fx_pcihost_root_bus_path;
  342. dc->realize = i440fx_pcihost_realize;
  343. dc->fw_name = "pci";
  344. device_class_set_props(dc, i440fx_props);
  345. /* Reason: needs to be wired up by pc_init1 */
  346. dc->user_creatable = false;
  347. }
  348. static const TypeInfo i440fx_pcihost_info = {
  349. .name = TYPE_I440FX_PCI_HOST_BRIDGE,
  350. .parent = TYPE_PCI_HOST_BRIDGE,
  351. .instance_size = sizeof(I440FXState),
  352. .instance_init = i440fx_pcihost_initfn,
  353. .class_init = i440fx_pcihost_class_init,
  354. };
  355. static void i440fx_register_types(void)
  356. {
  357. type_register_static(&i440fx_info);
  358. type_register_static(&i440fx_pcihost_info);
  359. }
  360. type_init(i440fx_register_types)