q35.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /*
  2. * QEMU MCH/ICH9 PCI Bridge Emulation
  3. *
  4. * Copyright (c) 2006 Fabrice Bellard
  5. * Copyright (c) 2009, 2010, 2011
  6. * Isaku Yamahata <yamahata at valinux co jp>
  7. * VA Linux Systems Japan K.K.
  8. * Copyright (C) 2012 Jason Baron <jbaron@redhat.com>
  9. *
  10. * This is based on piix.c, but heavily modified.
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a copy
  13. * of this software and associated documentation files (the "Software"), to deal
  14. * in the Software without restriction, including without limitation the rights
  15. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. * copies of the Software, and to permit persons to whom the Software is
  17. * furnished to do so, subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included in
  20. * all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  25. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. * THE SOFTWARE.
  29. */
  30. #include "qemu/osdep.h"
  31. #include "hw/pci-host/q35.h"
  32. #include "hw/qdev-properties.h"
  33. #include "migration/vmstate.h"
  34. #include "qapi/error.h"
  35. #include "qapi/visitor.h"
  36. #include "qemu/module.h"
  37. /****************************************************************************
  38. * Q35 host
  39. */
  40. #define Q35_PCI_HOST_HOLE64_SIZE_DEFAULT (1ULL << 35)
  41. static void q35_host_realize(DeviceState *dev, Error **errp)
  42. {
  43. PCIHostState *pci = PCI_HOST_BRIDGE(dev);
  44. Q35PCIHost *s = Q35_HOST_DEVICE(dev);
  45. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  46. sysbus_add_io(sbd, MCH_HOST_BRIDGE_CONFIG_ADDR, &pci->conf_mem);
  47. sysbus_init_ioports(sbd, MCH_HOST_BRIDGE_CONFIG_ADDR, 4);
  48. sysbus_add_io(sbd, MCH_HOST_BRIDGE_CONFIG_DATA, &pci->data_mem);
  49. sysbus_init_ioports(sbd, MCH_HOST_BRIDGE_CONFIG_DATA, 4);
  50. /* register q35 0xcf8 port as coalesced pio */
  51. memory_region_set_flush_coalesced(&pci->data_mem);
  52. memory_region_add_coalescing(&pci->conf_mem, 0, 4);
  53. pci->bus = pci_root_bus_new(DEVICE(s), "pcie.0",
  54. s->mch.pci_address_space,
  55. s->mch.address_space_io,
  56. 0, TYPE_PCIE_BUS);
  57. PC_MACHINE(qdev_get_machine())->bus = pci->bus;
  58. qdev_set_parent_bus(DEVICE(&s->mch), BUS(pci->bus));
  59. qdev_init_nofail(DEVICE(&s->mch));
  60. }
  61. static const char *q35_host_root_bus_path(PCIHostState *host_bridge,
  62. PCIBus *rootbus)
  63. {
  64. Q35PCIHost *s = Q35_HOST_DEVICE(host_bridge);
  65. /* For backwards compat with old device paths */
  66. if (s->mch.short_root_bus) {
  67. return "0000";
  68. }
  69. return "0000:00";
  70. }
  71. static void q35_host_get_pci_hole_start(Object *obj, Visitor *v,
  72. const char *name, void *opaque,
  73. Error **errp)
  74. {
  75. Q35PCIHost *s = Q35_HOST_DEVICE(obj);
  76. uint64_t val64;
  77. uint32_t value;
  78. val64 = range_is_empty(&s->mch.pci_hole)
  79. ? 0 : range_lob(&s->mch.pci_hole);
  80. value = val64;
  81. assert(value == val64);
  82. visit_type_uint32(v, name, &value, errp);
  83. }
  84. static void q35_host_get_pci_hole_end(Object *obj, Visitor *v,
  85. const char *name, void *opaque,
  86. Error **errp)
  87. {
  88. Q35PCIHost *s = Q35_HOST_DEVICE(obj);
  89. uint64_t val64;
  90. uint32_t value;
  91. val64 = range_is_empty(&s->mch.pci_hole)
  92. ? 0 : range_upb(&s->mch.pci_hole) + 1;
  93. value = val64;
  94. assert(value == val64);
  95. visit_type_uint32(v, name, &value, errp);
  96. }
  97. /*
  98. * The 64bit PCI hole start is set by the Guest firmware
  99. * as the address of the first 64bit PCI MEM resource.
  100. * If no PCI device has resources on the 64bit area,
  101. * the 64bit PCI hole will start after "over 4G RAM" and the
  102. * reserved space for memory hotplug if any.
  103. */
  104. static uint64_t q35_host_get_pci_hole64_start_value(Object *obj)
  105. {
  106. PCIHostState *h = PCI_HOST_BRIDGE(obj);
  107. Q35PCIHost *s = Q35_HOST_DEVICE(obj);
  108. Range w64;
  109. uint64_t value;
  110. pci_bus_get_w64_range(h->bus, &w64);
  111. value = range_is_empty(&w64) ? 0 : range_lob(&w64);
  112. if (!value && s->pci_hole64_fix) {
  113. value = pc_pci_hole64_start();
  114. }
  115. return value;
  116. }
  117. static void q35_host_get_pci_hole64_start(Object *obj, Visitor *v,
  118. const char *name, void *opaque,
  119. Error **errp)
  120. {
  121. uint64_t hole64_start = q35_host_get_pci_hole64_start_value(obj);
  122. visit_type_uint64(v, name, &hole64_start, errp);
  123. }
  124. /*
  125. * The 64bit PCI hole end is set by the Guest firmware
  126. * as the address of the last 64bit PCI MEM resource.
  127. * Then it is expanded to the PCI_HOST_PROP_PCI_HOLE64_SIZE
  128. * that can be configured by the user.
  129. */
  130. static void q35_host_get_pci_hole64_end(Object *obj, Visitor *v,
  131. const char *name, void *opaque,
  132. Error **errp)
  133. {
  134. PCIHostState *h = PCI_HOST_BRIDGE(obj);
  135. Q35PCIHost *s = Q35_HOST_DEVICE(obj);
  136. uint64_t hole64_start = q35_host_get_pci_hole64_start_value(obj);
  137. Range w64;
  138. uint64_t value, hole64_end;
  139. pci_bus_get_w64_range(h->bus, &w64);
  140. value = range_is_empty(&w64) ? 0 : range_upb(&w64) + 1;
  141. hole64_end = ROUND_UP(hole64_start + s->mch.pci_hole64_size, 1ULL << 30);
  142. if (s->pci_hole64_fix && value < hole64_end) {
  143. value = hole64_end;
  144. }
  145. visit_type_uint64(v, name, &value, errp);
  146. }
  147. static void q35_host_get_mmcfg_size(Object *obj, Visitor *v, const char *name,
  148. void *opaque, Error **errp)
  149. {
  150. PCIExpressHost *e = PCIE_HOST_BRIDGE(obj);
  151. visit_type_uint64(v, name, &e->size, errp);
  152. }
  153. /*
  154. * NOTE: setting defaults for the mch.* fields in this table
  155. * doesn't work, because mch is a separate QOM object that is
  156. * zeroed by the object_initialize(&s->mch, ...) call inside
  157. * q35_host_initfn(). The default values for those
  158. * properties need to be initialized manually by
  159. * q35_host_initfn() after the object_initialize() call.
  160. */
  161. static Property q35_host_props[] = {
  162. DEFINE_PROP_UINT64(PCIE_HOST_MCFG_BASE, Q35PCIHost, parent_obj.base_addr,
  163. MCH_HOST_BRIDGE_PCIEXBAR_DEFAULT),
  164. DEFINE_PROP_SIZE(PCI_HOST_PROP_PCI_HOLE64_SIZE, Q35PCIHost,
  165. mch.pci_hole64_size, Q35_PCI_HOST_HOLE64_SIZE_DEFAULT),
  166. DEFINE_PROP_UINT32("short_root_bus", Q35PCIHost, mch.short_root_bus, 0),
  167. DEFINE_PROP_SIZE(PCI_HOST_BELOW_4G_MEM_SIZE, Q35PCIHost,
  168. mch.below_4g_mem_size, 0),
  169. DEFINE_PROP_SIZE(PCI_HOST_ABOVE_4G_MEM_SIZE, Q35PCIHost,
  170. mch.above_4g_mem_size, 0),
  171. DEFINE_PROP_BOOL("x-pci-hole64-fix", Q35PCIHost, pci_hole64_fix, true),
  172. DEFINE_PROP_END_OF_LIST(),
  173. };
  174. static void q35_host_class_init(ObjectClass *klass, void *data)
  175. {
  176. DeviceClass *dc = DEVICE_CLASS(klass);
  177. PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(klass);
  178. hc->root_bus_path = q35_host_root_bus_path;
  179. dc->realize = q35_host_realize;
  180. dc->props = q35_host_props;
  181. /* Reason: needs to be wired up by pc_q35_init */
  182. dc->user_creatable = false;
  183. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  184. dc->fw_name = "pci";
  185. }
  186. static void q35_host_initfn(Object *obj)
  187. {
  188. Q35PCIHost *s = Q35_HOST_DEVICE(obj);
  189. PCIHostState *phb = PCI_HOST_BRIDGE(obj);
  190. memory_region_init_io(&phb->conf_mem, obj, &pci_host_conf_le_ops, phb,
  191. "pci-conf-idx", 4);
  192. memory_region_init_io(&phb->data_mem, obj, &pci_host_data_le_ops, phb,
  193. "pci-conf-data", 4);
  194. object_initialize_child(OBJECT(s), "mch", &s->mch, sizeof(s->mch),
  195. TYPE_MCH_PCI_DEVICE, &error_abort, NULL);
  196. qdev_prop_set_int32(DEVICE(&s->mch), "addr", PCI_DEVFN(0, 0));
  197. qdev_prop_set_bit(DEVICE(&s->mch), "multifunction", false);
  198. /* mch's object_initialize resets the default value, set it again */
  199. qdev_prop_set_uint64(DEVICE(s), PCI_HOST_PROP_PCI_HOLE64_SIZE,
  200. Q35_PCI_HOST_HOLE64_SIZE_DEFAULT);
  201. object_property_add(obj, PCI_HOST_PROP_PCI_HOLE_START, "uint32",
  202. q35_host_get_pci_hole_start,
  203. NULL, NULL, NULL, NULL);
  204. object_property_add(obj, PCI_HOST_PROP_PCI_HOLE_END, "uint32",
  205. q35_host_get_pci_hole_end,
  206. NULL, NULL, NULL, NULL);
  207. object_property_add(obj, PCI_HOST_PROP_PCI_HOLE64_START, "uint64",
  208. q35_host_get_pci_hole64_start,
  209. NULL, NULL, NULL, NULL);
  210. object_property_add(obj, PCI_HOST_PROP_PCI_HOLE64_END, "uint64",
  211. q35_host_get_pci_hole64_end,
  212. NULL, NULL, NULL, NULL);
  213. object_property_add(obj, PCIE_HOST_MCFG_SIZE, "uint64",
  214. q35_host_get_mmcfg_size,
  215. NULL, NULL, NULL, NULL);
  216. object_property_add_link(obj, MCH_HOST_PROP_RAM_MEM, TYPE_MEMORY_REGION,
  217. (Object **) &s->mch.ram_memory,
  218. qdev_prop_allow_set_link_before_realize, 0, NULL);
  219. object_property_add_link(obj, MCH_HOST_PROP_PCI_MEM, TYPE_MEMORY_REGION,
  220. (Object **) &s->mch.pci_address_space,
  221. qdev_prop_allow_set_link_before_realize, 0, NULL);
  222. object_property_add_link(obj, MCH_HOST_PROP_SYSTEM_MEM, TYPE_MEMORY_REGION,
  223. (Object **) &s->mch.system_memory,
  224. qdev_prop_allow_set_link_before_realize, 0, NULL);
  225. object_property_add_link(obj, MCH_HOST_PROP_IO_MEM, TYPE_MEMORY_REGION,
  226. (Object **) &s->mch.address_space_io,
  227. qdev_prop_allow_set_link_before_realize, 0, NULL);
  228. }
  229. static const TypeInfo q35_host_info = {
  230. .name = TYPE_Q35_HOST_DEVICE,
  231. .parent = TYPE_PCIE_HOST_BRIDGE,
  232. .instance_size = sizeof(Q35PCIHost),
  233. .instance_init = q35_host_initfn,
  234. .class_init = q35_host_class_init,
  235. };
  236. /****************************************************************************
  237. * MCH D0:F0
  238. */
  239. static uint64_t tseg_blackhole_read(void *ptr, hwaddr reg, unsigned size)
  240. {
  241. return 0xffffffff;
  242. }
  243. static void tseg_blackhole_write(void *opaque, hwaddr addr, uint64_t val,
  244. unsigned width)
  245. {
  246. /* nothing */
  247. }
  248. static const MemoryRegionOps tseg_blackhole_ops = {
  249. .read = tseg_blackhole_read,
  250. .write = tseg_blackhole_write,
  251. .endianness = DEVICE_NATIVE_ENDIAN,
  252. .valid.min_access_size = 1,
  253. .valid.max_access_size = 4,
  254. .impl.min_access_size = 4,
  255. .impl.max_access_size = 4,
  256. .endianness = DEVICE_LITTLE_ENDIAN,
  257. };
  258. /* PCIe MMCFG */
  259. static void mch_update_pciexbar(MCHPCIState *mch)
  260. {
  261. PCIDevice *pci_dev = PCI_DEVICE(mch);
  262. BusState *bus = qdev_get_parent_bus(DEVICE(mch));
  263. PCIExpressHost *pehb = PCIE_HOST_BRIDGE(bus->parent);
  264. uint64_t pciexbar;
  265. int enable;
  266. uint64_t addr;
  267. uint64_t addr_mask;
  268. uint32_t length;
  269. pciexbar = pci_get_quad(pci_dev->config + MCH_HOST_BRIDGE_PCIEXBAR);
  270. enable = pciexbar & MCH_HOST_BRIDGE_PCIEXBAREN;
  271. addr_mask = MCH_HOST_BRIDGE_PCIEXBAR_ADMSK;
  272. switch (pciexbar & MCH_HOST_BRIDGE_PCIEXBAR_LENGTH_MASK) {
  273. case MCH_HOST_BRIDGE_PCIEXBAR_LENGTH_256M:
  274. length = 256 * 1024 * 1024;
  275. break;
  276. case MCH_HOST_BRIDGE_PCIEXBAR_LENGTH_128M:
  277. length = 128 * 1024 * 1024;
  278. addr_mask |= MCH_HOST_BRIDGE_PCIEXBAR_128ADMSK |
  279. MCH_HOST_BRIDGE_PCIEXBAR_64ADMSK;
  280. break;
  281. case MCH_HOST_BRIDGE_PCIEXBAR_LENGTH_64M:
  282. length = 64 * 1024 * 1024;
  283. addr_mask |= MCH_HOST_BRIDGE_PCIEXBAR_64ADMSK;
  284. break;
  285. case MCH_HOST_BRIDGE_PCIEXBAR_LENGTH_RVD:
  286. default:
  287. abort();
  288. }
  289. addr = pciexbar & addr_mask;
  290. pcie_host_mmcfg_update(pehb, enable, addr, length);
  291. }
  292. /* PAM */
  293. static void mch_update_pam(MCHPCIState *mch)
  294. {
  295. PCIDevice *pd = PCI_DEVICE(mch);
  296. int i;
  297. memory_region_transaction_begin();
  298. for (i = 0; i < 13; i++) {
  299. pam_update(&mch->pam_regions[i], i,
  300. pd->config[MCH_HOST_BRIDGE_PAM0 + DIV_ROUND_UP(i, 2)]);
  301. }
  302. memory_region_transaction_commit();
  303. }
  304. /* SMRAM */
  305. static void mch_update_smram(MCHPCIState *mch)
  306. {
  307. PCIDevice *pd = PCI_DEVICE(mch);
  308. bool h_smrame = (pd->config[MCH_HOST_BRIDGE_ESMRAMC] & MCH_HOST_BRIDGE_ESMRAMC_H_SMRAME);
  309. uint32_t tseg_size;
  310. /* implement SMRAM.D_LCK */
  311. if (pd->config[MCH_HOST_BRIDGE_SMRAM] & MCH_HOST_BRIDGE_SMRAM_D_LCK) {
  312. pd->config[MCH_HOST_BRIDGE_SMRAM] &= ~MCH_HOST_BRIDGE_SMRAM_D_OPEN;
  313. pd->wmask[MCH_HOST_BRIDGE_SMRAM] = MCH_HOST_BRIDGE_SMRAM_WMASK_LCK;
  314. pd->wmask[MCH_HOST_BRIDGE_ESMRAMC] = MCH_HOST_BRIDGE_ESMRAMC_WMASK_LCK;
  315. }
  316. memory_region_transaction_begin();
  317. if (pd->config[MCH_HOST_BRIDGE_SMRAM] & SMRAM_D_OPEN) {
  318. /* Hide (!) low SMRAM if H_SMRAME = 1 */
  319. memory_region_set_enabled(&mch->smram_region, h_smrame);
  320. /* Show high SMRAM if H_SMRAME = 1 */
  321. memory_region_set_enabled(&mch->open_high_smram, h_smrame);
  322. } else {
  323. /* Hide high SMRAM and low SMRAM */
  324. memory_region_set_enabled(&mch->smram_region, true);
  325. memory_region_set_enabled(&mch->open_high_smram, false);
  326. }
  327. if (pd->config[MCH_HOST_BRIDGE_SMRAM] & SMRAM_G_SMRAME) {
  328. memory_region_set_enabled(&mch->low_smram, !h_smrame);
  329. memory_region_set_enabled(&mch->high_smram, h_smrame);
  330. } else {
  331. memory_region_set_enabled(&mch->low_smram, false);
  332. memory_region_set_enabled(&mch->high_smram, false);
  333. }
  334. if (pd->config[MCH_HOST_BRIDGE_ESMRAMC] & MCH_HOST_BRIDGE_ESMRAMC_T_EN) {
  335. switch (pd->config[MCH_HOST_BRIDGE_ESMRAMC] &
  336. MCH_HOST_BRIDGE_ESMRAMC_TSEG_SZ_MASK) {
  337. case MCH_HOST_BRIDGE_ESMRAMC_TSEG_SZ_1MB:
  338. tseg_size = 1024 * 1024;
  339. break;
  340. case MCH_HOST_BRIDGE_ESMRAMC_TSEG_SZ_2MB:
  341. tseg_size = 1024 * 1024 * 2;
  342. break;
  343. case MCH_HOST_BRIDGE_ESMRAMC_TSEG_SZ_8MB:
  344. tseg_size = 1024 * 1024 * 8;
  345. break;
  346. default:
  347. tseg_size = 1024 * 1024 * (uint32_t)mch->ext_tseg_mbytes;
  348. break;
  349. }
  350. } else {
  351. tseg_size = 0;
  352. }
  353. memory_region_del_subregion(mch->system_memory, &mch->tseg_blackhole);
  354. memory_region_set_enabled(&mch->tseg_blackhole, tseg_size);
  355. memory_region_set_size(&mch->tseg_blackhole, tseg_size);
  356. memory_region_add_subregion_overlap(mch->system_memory,
  357. mch->below_4g_mem_size - tseg_size,
  358. &mch->tseg_blackhole, 1);
  359. memory_region_set_enabled(&mch->tseg_window, tseg_size);
  360. memory_region_set_size(&mch->tseg_window, tseg_size);
  361. memory_region_set_address(&mch->tseg_window,
  362. mch->below_4g_mem_size - tseg_size);
  363. memory_region_set_alias_offset(&mch->tseg_window,
  364. mch->below_4g_mem_size - tseg_size);
  365. memory_region_transaction_commit();
  366. }
  367. static void mch_update_ext_tseg_mbytes(MCHPCIState *mch)
  368. {
  369. PCIDevice *pd = PCI_DEVICE(mch);
  370. uint8_t *reg = pd->config + MCH_HOST_BRIDGE_EXT_TSEG_MBYTES;
  371. if (mch->ext_tseg_mbytes > 0 &&
  372. pci_get_word(reg) == MCH_HOST_BRIDGE_EXT_TSEG_MBYTES_QUERY) {
  373. pci_set_word(reg, mch->ext_tseg_mbytes);
  374. }
  375. }
  376. static void mch_write_config(PCIDevice *d,
  377. uint32_t address, uint32_t val, int len)
  378. {
  379. MCHPCIState *mch = MCH_PCI_DEVICE(d);
  380. pci_default_write_config(d, address, val, len);
  381. if (ranges_overlap(address, len, MCH_HOST_BRIDGE_PAM0,
  382. MCH_HOST_BRIDGE_PAM_SIZE)) {
  383. mch_update_pam(mch);
  384. }
  385. if (ranges_overlap(address, len, MCH_HOST_BRIDGE_PCIEXBAR,
  386. MCH_HOST_BRIDGE_PCIEXBAR_SIZE)) {
  387. mch_update_pciexbar(mch);
  388. }
  389. if (ranges_overlap(address, len, MCH_HOST_BRIDGE_SMRAM,
  390. MCH_HOST_BRIDGE_SMRAM_SIZE)) {
  391. mch_update_smram(mch);
  392. }
  393. if (ranges_overlap(address, len, MCH_HOST_BRIDGE_EXT_TSEG_MBYTES,
  394. MCH_HOST_BRIDGE_EXT_TSEG_MBYTES_SIZE)) {
  395. mch_update_ext_tseg_mbytes(mch);
  396. }
  397. }
  398. static void mch_update(MCHPCIState *mch)
  399. {
  400. mch_update_pciexbar(mch);
  401. mch_update_pam(mch);
  402. mch_update_smram(mch);
  403. mch_update_ext_tseg_mbytes(mch);
  404. /*
  405. * pci hole goes from end-of-low-ram to io-apic.
  406. * mmconfig will be excluded by the dsdt builder.
  407. */
  408. range_set_bounds(&mch->pci_hole,
  409. mch->below_4g_mem_size,
  410. IO_APIC_DEFAULT_ADDRESS - 1);
  411. }
  412. static int mch_post_load(void *opaque, int version_id)
  413. {
  414. MCHPCIState *mch = opaque;
  415. mch_update(mch);
  416. return 0;
  417. }
  418. static const VMStateDescription vmstate_mch = {
  419. .name = "mch",
  420. .version_id = 1,
  421. .minimum_version_id = 1,
  422. .post_load = mch_post_load,
  423. .fields = (VMStateField[]) {
  424. VMSTATE_PCI_DEVICE(parent_obj, MCHPCIState),
  425. /* Used to be smm_enabled, which was basically always zero because
  426. * SeaBIOS hardly uses SMM. SMRAM is now handled by CPU code.
  427. */
  428. VMSTATE_UNUSED(1),
  429. VMSTATE_END_OF_LIST()
  430. }
  431. };
  432. static void mch_reset(DeviceState *qdev)
  433. {
  434. PCIDevice *d = PCI_DEVICE(qdev);
  435. MCHPCIState *mch = MCH_PCI_DEVICE(d);
  436. pci_set_quad(d->config + MCH_HOST_BRIDGE_PCIEXBAR,
  437. MCH_HOST_BRIDGE_PCIEXBAR_DEFAULT);
  438. d->config[MCH_HOST_BRIDGE_SMRAM] = MCH_HOST_BRIDGE_SMRAM_DEFAULT;
  439. d->config[MCH_HOST_BRIDGE_ESMRAMC] = MCH_HOST_BRIDGE_ESMRAMC_DEFAULT;
  440. d->wmask[MCH_HOST_BRIDGE_SMRAM] = MCH_HOST_BRIDGE_SMRAM_WMASK;
  441. d->wmask[MCH_HOST_BRIDGE_ESMRAMC] = MCH_HOST_BRIDGE_ESMRAMC_WMASK;
  442. if (mch->ext_tseg_mbytes > 0) {
  443. pci_set_word(d->config + MCH_HOST_BRIDGE_EXT_TSEG_MBYTES,
  444. MCH_HOST_BRIDGE_EXT_TSEG_MBYTES_QUERY);
  445. }
  446. mch_update(mch);
  447. }
  448. static void mch_realize(PCIDevice *d, Error **errp)
  449. {
  450. int i;
  451. MCHPCIState *mch = MCH_PCI_DEVICE(d);
  452. if (mch->ext_tseg_mbytes > MCH_HOST_BRIDGE_EXT_TSEG_MBYTES_MAX) {
  453. error_setg(errp, "invalid extended-tseg-mbytes value: %" PRIu16,
  454. mch->ext_tseg_mbytes);
  455. return;
  456. }
  457. /* setup pci memory mapping */
  458. pc_pci_as_mapping_init(OBJECT(mch), mch->system_memory,
  459. mch->pci_address_space);
  460. /* if *disabled* show SMRAM to all CPUs */
  461. memory_region_init_alias(&mch->smram_region, OBJECT(mch), "smram-region",
  462. mch->pci_address_space, MCH_HOST_BRIDGE_SMRAM_C_BASE,
  463. MCH_HOST_BRIDGE_SMRAM_C_SIZE);
  464. memory_region_add_subregion_overlap(mch->system_memory, MCH_HOST_BRIDGE_SMRAM_C_BASE,
  465. &mch->smram_region, 1);
  466. memory_region_set_enabled(&mch->smram_region, true);
  467. memory_region_init_alias(&mch->open_high_smram, OBJECT(mch), "smram-open-high",
  468. mch->ram_memory, MCH_HOST_BRIDGE_SMRAM_C_BASE,
  469. MCH_HOST_BRIDGE_SMRAM_C_SIZE);
  470. memory_region_add_subregion_overlap(mch->system_memory, 0xfeda0000,
  471. &mch->open_high_smram, 1);
  472. memory_region_set_enabled(&mch->open_high_smram, false);
  473. /* smram, as seen by SMM CPUs */
  474. memory_region_init(&mch->smram, OBJECT(mch), "smram", 1ull << 32);
  475. memory_region_set_enabled(&mch->smram, true);
  476. memory_region_init_alias(&mch->low_smram, OBJECT(mch), "smram-low",
  477. mch->ram_memory, MCH_HOST_BRIDGE_SMRAM_C_BASE,
  478. MCH_HOST_BRIDGE_SMRAM_C_SIZE);
  479. memory_region_set_enabled(&mch->low_smram, true);
  480. memory_region_add_subregion(&mch->smram, MCH_HOST_BRIDGE_SMRAM_C_BASE,
  481. &mch->low_smram);
  482. memory_region_init_alias(&mch->high_smram, OBJECT(mch), "smram-high",
  483. mch->ram_memory, MCH_HOST_BRIDGE_SMRAM_C_BASE,
  484. MCH_HOST_BRIDGE_SMRAM_C_SIZE);
  485. memory_region_set_enabled(&mch->high_smram, true);
  486. memory_region_add_subregion(&mch->smram, 0xfeda0000, &mch->high_smram);
  487. memory_region_init_io(&mch->tseg_blackhole, OBJECT(mch),
  488. &tseg_blackhole_ops, NULL,
  489. "tseg-blackhole", 0);
  490. memory_region_set_enabled(&mch->tseg_blackhole, false);
  491. memory_region_add_subregion_overlap(mch->system_memory,
  492. mch->below_4g_mem_size,
  493. &mch->tseg_blackhole, 1);
  494. memory_region_init_alias(&mch->tseg_window, OBJECT(mch), "tseg-window",
  495. mch->ram_memory, mch->below_4g_mem_size, 0);
  496. memory_region_set_enabled(&mch->tseg_window, false);
  497. memory_region_add_subregion(&mch->smram, mch->below_4g_mem_size,
  498. &mch->tseg_window);
  499. object_property_add_const_link(qdev_get_machine(), "smram",
  500. OBJECT(&mch->smram), &error_abort);
  501. init_pam(DEVICE(mch), mch->ram_memory, mch->system_memory,
  502. mch->pci_address_space, &mch->pam_regions[0],
  503. PAM_BIOS_BASE, PAM_BIOS_SIZE);
  504. for (i = 0; i < 12; ++i) {
  505. init_pam(DEVICE(mch), mch->ram_memory, mch->system_memory,
  506. mch->pci_address_space, &mch->pam_regions[i+1],
  507. PAM_EXPAN_BASE + i * PAM_EXPAN_SIZE, PAM_EXPAN_SIZE);
  508. }
  509. }
  510. uint64_t mch_mcfg_base(void)
  511. {
  512. bool ambiguous;
  513. Object *o = object_resolve_path_type("", TYPE_MCH_PCI_DEVICE, &ambiguous);
  514. if (!o) {
  515. return 0;
  516. }
  517. return MCH_HOST_BRIDGE_PCIEXBAR_DEFAULT;
  518. }
  519. static Property mch_props[] = {
  520. DEFINE_PROP_UINT16("extended-tseg-mbytes", MCHPCIState, ext_tseg_mbytes,
  521. 16),
  522. DEFINE_PROP_END_OF_LIST(),
  523. };
  524. static void mch_class_init(ObjectClass *klass, void *data)
  525. {
  526. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  527. DeviceClass *dc = DEVICE_CLASS(klass);
  528. k->realize = mch_realize;
  529. k->config_write = mch_write_config;
  530. dc->reset = mch_reset;
  531. dc->props = mch_props;
  532. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  533. dc->desc = "Host bridge";
  534. dc->vmsd = &vmstate_mch;
  535. k->vendor_id = PCI_VENDOR_ID_INTEL;
  536. /*
  537. * The 'q35' machine type implements an Intel Series 3 chipset,
  538. * of which there are several variants. The key difference between
  539. * the 82P35 MCH ('p35') and 82Q35 GMCH ('q35') variants is that
  540. * the latter has an integrated graphics adapter. QEMU does not
  541. * implement integrated graphics, so uses the PCI ID for the 82P35
  542. * chipset.
  543. */
  544. k->device_id = PCI_DEVICE_ID_INTEL_P35_MCH;
  545. k->revision = MCH_HOST_BRIDGE_REVISION_DEFAULT;
  546. k->class_id = PCI_CLASS_BRIDGE_HOST;
  547. /*
  548. * PCI-facing part of the host bridge, not usable without the
  549. * host-facing part, which can't be device_add'ed, yet.
  550. */
  551. dc->user_creatable = false;
  552. }
  553. static const TypeInfo mch_info = {
  554. .name = TYPE_MCH_PCI_DEVICE,
  555. .parent = TYPE_PCI_DEVICE,
  556. .instance_size = sizeof(MCHPCIState),
  557. .class_init = mch_class_init,
  558. .interfaces = (InterfaceInfo[]) {
  559. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  560. { },
  561. },
  562. };
  563. static void q35_register(void)
  564. {
  565. type_register_static(&mch_info);
  566. type_register_static(&q35_host_info);
  567. }
  568. type_init(q35_register);