i440fx.c 17 KB

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