q35.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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 "qemu/log.h"
  32. #include "hw/i386/pc.h"
  33. #include "hw/pci-host/q35.h"
  34. #include "hw/qdev-properties.h"
  35. #include "migration/vmstate.h"
  36. #include "qapi/error.h"
  37. #include "qapi/visitor.h"
  38. #include "qemu/module.h"
  39. /****************************************************************************
  40. * Q35 host
  41. */
  42. #define Q35_PCI_HOST_HOLE64_SIZE_DEFAULT (1ULL << 35)
  43. static void q35_host_realize(DeviceState *dev, Error **errp)
  44. {
  45. PCIHostState *pci = PCI_HOST_BRIDGE(dev);
  46. Q35PCIHost *s = Q35_HOST_DEVICE(dev);
  47. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  48. sysbus_add_io(sbd, MCH_HOST_BRIDGE_CONFIG_ADDR, &pci->conf_mem);
  49. sysbus_init_ioports(sbd, MCH_HOST_BRIDGE_CONFIG_ADDR, 4);
  50. sysbus_add_io(sbd, MCH_HOST_BRIDGE_CONFIG_DATA, &pci->data_mem);
  51. sysbus_init_ioports(sbd, MCH_HOST_BRIDGE_CONFIG_DATA, 4);
  52. /* register q35 0xcf8 port as coalesced pio */
  53. memory_region_set_flush_coalesced(&pci->data_mem);
  54. memory_region_add_coalescing(&pci->conf_mem, 0, 4);
  55. pci->bus = pci_root_bus_new(DEVICE(s), "pcie.0",
  56. s->mch.pci_address_space,
  57. s->mch.address_space_io,
  58. 0, TYPE_PCIE_BUS);
  59. PC_MACHINE(qdev_get_machine())->bus = pci->bus;
  60. pci->bypass_iommu =
  61. PC_MACHINE(qdev_get_machine())->default_bus_bypass_iommu;
  62. qdev_realize(DEVICE(&s->mch), BUS(pci->bus), &error_fatal);
  63. }
  64. static const char *q35_host_root_bus_path(PCIHostState *host_bridge,
  65. PCIBus *rootbus)
  66. {
  67. Q35PCIHost *s = Q35_HOST_DEVICE(host_bridge);
  68. /* For backwards compat with old device paths */
  69. if (s->mch.short_root_bus) {
  70. return "0000";
  71. }
  72. return "0000:00";
  73. }
  74. static void q35_host_get_pci_hole_start(Object *obj, Visitor *v,
  75. const char *name, void *opaque,
  76. Error **errp)
  77. {
  78. Q35PCIHost *s = Q35_HOST_DEVICE(obj);
  79. uint64_t val64;
  80. uint32_t value;
  81. val64 = range_is_empty(&s->mch.pci_hole)
  82. ? 0 : range_lob(&s->mch.pci_hole);
  83. value = val64;
  84. assert(value == val64);
  85. visit_type_uint32(v, name, &value, errp);
  86. }
  87. static void q35_host_get_pci_hole_end(Object *obj, Visitor *v,
  88. const char *name, void *opaque,
  89. Error **errp)
  90. {
  91. Q35PCIHost *s = Q35_HOST_DEVICE(obj);
  92. uint64_t val64;
  93. uint32_t value;
  94. val64 = range_is_empty(&s->mch.pci_hole)
  95. ? 0 : range_upb(&s->mch.pci_hole) + 1;
  96. value = val64;
  97. assert(value == val64);
  98. visit_type_uint32(v, name, &value, errp);
  99. }
  100. /*
  101. * The 64bit PCI hole start is set by the Guest firmware
  102. * as the address of the first 64bit PCI MEM resource.
  103. * If no PCI device has resources on the 64bit area,
  104. * the 64bit PCI hole will start after "over 4G RAM" and the
  105. * reserved space for memory hotplug if any.
  106. */
  107. static uint64_t q35_host_get_pci_hole64_start_value(Object *obj)
  108. {
  109. PCIHostState *h = PCI_HOST_BRIDGE(obj);
  110. Q35PCIHost *s = Q35_HOST_DEVICE(obj);
  111. Range w64;
  112. uint64_t value;
  113. pci_bus_get_w64_range(h->bus, &w64);
  114. value = range_is_empty(&w64) ? 0 : range_lob(&w64);
  115. if (!value && s->pci_hole64_fix) {
  116. value = pc_pci_hole64_start();
  117. }
  118. return value;
  119. }
  120. static void q35_host_get_pci_hole64_start(Object *obj, Visitor *v,
  121. const char *name, void *opaque,
  122. Error **errp)
  123. {
  124. uint64_t hole64_start = q35_host_get_pci_hole64_start_value(obj);
  125. visit_type_uint64(v, name, &hole64_start, errp);
  126. }
  127. /*
  128. * The 64bit PCI hole end is set by the Guest firmware
  129. * as the address of the last 64bit PCI MEM resource.
  130. * Then it is expanded to the PCI_HOST_PROP_PCI_HOLE64_SIZE
  131. * that can be configured by the user.
  132. */
  133. static void q35_host_get_pci_hole64_end(Object *obj, Visitor *v,
  134. const char *name, void *opaque,
  135. Error **errp)
  136. {
  137. PCIHostState *h = PCI_HOST_BRIDGE(obj);
  138. Q35PCIHost *s = Q35_HOST_DEVICE(obj);
  139. uint64_t hole64_start = q35_host_get_pci_hole64_start_value(obj);
  140. Range w64;
  141. uint64_t value, hole64_end;
  142. pci_bus_get_w64_range(h->bus, &w64);
  143. value = range_is_empty(&w64) ? 0 : range_upb(&w64) + 1;
  144. hole64_end = ROUND_UP(hole64_start + s->mch.pci_hole64_size, 1ULL << 30);
  145. if (s->pci_hole64_fix && value < hole64_end) {
  146. value = hole64_end;
  147. }
  148. visit_type_uint64(v, name, &value, errp);
  149. }
  150. /*
  151. * NOTE: setting defaults for the mch.* fields in this table
  152. * doesn't work, because mch is a separate QOM object that is
  153. * zeroed by the object_initialize(&s->mch, ...) call inside
  154. * q35_host_initfn(). The default values for those
  155. * properties need to be initialized manually by
  156. * q35_host_initfn() after the object_initialize() call.
  157. */
  158. static Property q35_host_props[] = {
  159. DEFINE_PROP_UINT64(PCIE_HOST_MCFG_BASE, Q35PCIHost, parent_obj.base_addr,
  160. MCH_HOST_BRIDGE_PCIEXBAR_DEFAULT),
  161. DEFINE_PROP_SIZE(PCI_HOST_PROP_PCI_HOLE64_SIZE, Q35PCIHost,
  162. mch.pci_hole64_size, Q35_PCI_HOST_HOLE64_SIZE_DEFAULT),
  163. DEFINE_PROP_UINT32("short_root_bus", Q35PCIHost, mch.short_root_bus, 0),
  164. DEFINE_PROP_SIZE(PCI_HOST_BELOW_4G_MEM_SIZE, Q35PCIHost,
  165. mch.below_4g_mem_size, 0),
  166. DEFINE_PROP_SIZE(PCI_HOST_ABOVE_4G_MEM_SIZE, Q35PCIHost,
  167. mch.above_4g_mem_size, 0),
  168. DEFINE_PROP_BOOL("x-pci-hole64-fix", Q35PCIHost, pci_hole64_fix, true),
  169. DEFINE_PROP_END_OF_LIST(),
  170. };
  171. static void q35_host_class_init(ObjectClass *klass, void *data)
  172. {
  173. DeviceClass *dc = DEVICE_CLASS(klass);
  174. PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(klass);
  175. hc->root_bus_path = q35_host_root_bus_path;
  176. dc->realize = q35_host_realize;
  177. device_class_set_props(dc, q35_host_props);
  178. /* Reason: needs to be wired up by pc_q35_init */
  179. dc->user_creatable = false;
  180. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  181. dc->fw_name = "pci";
  182. }
  183. static void q35_host_initfn(Object *obj)
  184. {
  185. Q35PCIHost *s = Q35_HOST_DEVICE(obj);
  186. PCIHostState *phb = PCI_HOST_BRIDGE(obj);
  187. PCIExpressHost *pehb = PCIE_HOST_BRIDGE(obj);
  188. memory_region_init_io(&phb->conf_mem, obj, &pci_host_conf_le_ops, phb,
  189. "pci-conf-idx", 4);
  190. memory_region_init_io(&phb->data_mem, obj, &pci_host_data_le_ops, phb,
  191. "pci-conf-data", 4);
  192. object_initialize_child(OBJECT(s), "mch", &s->mch, TYPE_MCH_PCI_DEVICE);
  193. qdev_prop_set_int32(DEVICE(&s->mch), "addr", PCI_DEVFN(0, 0));
  194. qdev_prop_set_bit(DEVICE(&s->mch), "multifunction", false);
  195. /* mch's object_initialize resets the default value, set it again */
  196. qdev_prop_set_uint64(DEVICE(s), PCI_HOST_PROP_PCI_HOLE64_SIZE,
  197. Q35_PCI_HOST_HOLE64_SIZE_DEFAULT);
  198. object_property_add(obj, PCI_HOST_PROP_PCI_HOLE_START, "uint32",
  199. q35_host_get_pci_hole_start,
  200. NULL, NULL, NULL);
  201. object_property_add(obj, PCI_HOST_PROP_PCI_HOLE_END, "uint32",
  202. q35_host_get_pci_hole_end,
  203. NULL, NULL, NULL);
  204. object_property_add(obj, PCI_HOST_PROP_PCI_HOLE64_START, "uint64",
  205. q35_host_get_pci_hole64_start,
  206. NULL, NULL, NULL);
  207. object_property_add(obj, PCI_HOST_PROP_PCI_HOLE64_END, "uint64",
  208. q35_host_get_pci_hole64_end,
  209. NULL, NULL, NULL);
  210. object_property_add_uint64_ptr(obj, PCIE_HOST_MCFG_SIZE,
  211. &pehb->size, OBJ_PROP_FLAG_READ);
  212. object_property_add_link(obj, MCH_HOST_PROP_RAM_MEM, TYPE_MEMORY_REGION,
  213. (Object **) &s->mch.ram_memory,
  214. qdev_prop_allow_set_link_before_realize, 0);
  215. object_property_add_link(obj, MCH_HOST_PROP_PCI_MEM, TYPE_MEMORY_REGION,
  216. (Object **) &s->mch.pci_address_space,
  217. qdev_prop_allow_set_link_before_realize, 0);
  218. object_property_add_link(obj, MCH_HOST_PROP_SYSTEM_MEM, TYPE_MEMORY_REGION,
  219. (Object **) &s->mch.system_memory,
  220. qdev_prop_allow_set_link_before_realize, 0);
  221. object_property_add_link(obj, MCH_HOST_PROP_IO_MEM, TYPE_MEMORY_REGION,
  222. (Object **) &s->mch.address_space_io,
  223. qdev_prop_allow_set_link_before_realize, 0);
  224. }
  225. static const TypeInfo q35_host_info = {
  226. .name = TYPE_Q35_HOST_DEVICE,
  227. .parent = TYPE_PCIE_HOST_BRIDGE,
  228. .instance_size = sizeof(Q35PCIHost),
  229. .instance_init = q35_host_initfn,
  230. .class_init = q35_host_class_init,
  231. };
  232. /****************************************************************************
  233. * MCH D0:F0
  234. */
  235. static uint64_t blackhole_read(void *ptr, hwaddr reg, unsigned size)
  236. {
  237. return 0xffffffff;
  238. }
  239. static void blackhole_write(void *opaque, hwaddr addr, uint64_t val,
  240. unsigned width)
  241. {
  242. /* nothing */
  243. }
  244. static const MemoryRegionOps blackhole_ops = {
  245. .read = blackhole_read,
  246. .write = blackhole_write,
  247. .endianness = DEVICE_NATIVE_ENDIAN,
  248. .valid.min_access_size = 1,
  249. .valid.max_access_size = 4,
  250. .impl.min_access_size = 4,
  251. .impl.max_access_size = 4,
  252. .endianness = DEVICE_LITTLE_ENDIAN,
  253. };
  254. /* PCIe MMCFG */
  255. static void mch_update_pciexbar(MCHPCIState *mch)
  256. {
  257. PCIDevice *pci_dev = PCI_DEVICE(mch);
  258. BusState *bus = qdev_get_parent_bus(DEVICE(mch));
  259. PCIExpressHost *pehb = PCIE_HOST_BRIDGE(bus->parent);
  260. uint64_t pciexbar;
  261. int enable;
  262. uint64_t addr;
  263. uint64_t addr_mask;
  264. uint32_t length;
  265. pciexbar = pci_get_quad(pci_dev->config + MCH_HOST_BRIDGE_PCIEXBAR);
  266. enable = pciexbar & MCH_HOST_BRIDGE_PCIEXBAREN;
  267. addr_mask = MCH_HOST_BRIDGE_PCIEXBAR_ADMSK;
  268. switch (pciexbar & MCH_HOST_BRIDGE_PCIEXBAR_LENGTH_MASK) {
  269. case MCH_HOST_BRIDGE_PCIEXBAR_LENGTH_256M:
  270. length = 256 * 1024 * 1024;
  271. break;
  272. case MCH_HOST_BRIDGE_PCIEXBAR_LENGTH_128M:
  273. length = 128 * 1024 * 1024;
  274. addr_mask |= MCH_HOST_BRIDGE_PCIEXBAR_128ADMSK |
  275. MCH_HOST_BRIDGE_PCIEXBAR_64ADMSK;
  276. break;
  277. case MCH_HOST_BRIDGE_PCIEXBAR_LENGTH_64M:
  278. length = 64 * 1024 * 1024;
  279. addr_mask |= MCH_HOST_BRIDGE_PCIEXBAR_64ADMSK;
  280. break;
  281. case MCH_HOST_BRIDGE_PCIEXBAR_LENGTH_RVD:
  282. qemu_log_mask(LOG_GUEST_ERROR, "Q35: Reserved PCIEXBAR LENGTH\n");
  283. return;
  284. default:
  285. abort();
  286. }
  287. addr = pciexbar & addr_mask;
  288. pcie_host_mmcfg_update(pehb, enable, addr, length);
  289. }
  290. /* PAM */
  291. static void mch_update_pam(MCHPCIState *mch)
  292. {
  293. PCIDevice *pd = PCI_DEVICE(mch);
  294. int i;
  295. memory_region_transaction_begin();
  296. for (i = 0; i < 13; i++) {
  297. pam_update(&mch->pam_regions[i], i,
  298. pd->config[MCH_HOST_BRIDGE_PAM0 + DIV_ROUND_UP(i, 2)]);
  299. }
  300. memory_region_transaction_commit();
  301. }
  302. /* SMRAM */
  303. static void mch_update_smram(MCHPCIState *mch)
  304. {
  305. PCIDevice *pd = PCI_DEVICE(mch);
  306. bool h_smrame = (pd->config[MCH_HOST_BRIDGE_ESMRAMC] & MCH_HOST_BRIDGE_ESMRAMC_H_SMRAME);
  307. uint32_t tseg_size;
  308. /* implement SMRAM.D_LCK */
  309. if (pd->config[MCH_HOST_BRIDGE_SMRAM] & MCH_HOST_BRIDGE_SMRAM_D_LCK) {
  310. pd->config[MCH_HOST_BRIDGE_SMRAM] &= ~MCH_HOST_BRIDGE_SMRAM_D_OPEN;
  311. pd->wmask[MCH_HOST_BRIDGE_SMRAM] = MCH_HOST_BRIDGE_SMRAM_WMASK_LCK;
  312. pd->wmask[MCH_HOST_BRIDGE_ESMRAMC] = MCH_HOST_BRIDGE_ESMRAMC_WMASK_LCK;
  313. }
  314. memory_region_transaction_begin();
  315. if (pd->config[MCH_HOST_BRIDGE_SMRAM] & SMRAM_D_OPEN) {
  316. /* Hide (!) low SMRAM if H_SMRAME = 1 */
  317. memory_region_set_enabled(&mch->smram_region, h_smrame);
  318. /* Show high SMRAM if H_SMRAME = 1 */
  319. memory_region_set_enabled(&mch->open_high_smram, h_smrame);
  320. } else {
  321. /* Hide high SMRAM and low SMRAM */
  322. memory_region_set_enabled(&mch->smram_region, true);
  323. memory_region_set_enabled(&mch->open_high_smram, false);
  324. }
  325. if (pd->config[MCH_HOST_BRIDGE_SMRAM] & SMRAM_G_SMRAME) {
  326. memory_region_set_enabled(&mch->low_smram, !h_smrame);
  327. memory_region_set_enabled(&mch->high_smram, h_smrame);
  328. } else {
  329. memory_region_set_enabled(&mch->low_smram, false);
  330. memory_region_set_enabled(&mch->high_smram, false);
  331. }
  332. if ((pd->config[MCH_HOST_BRIDGE_ESMRAMC] & MCH_HOST_BRIDGE_ESMRAMC_T_EN) &&
  333. (pd->config[MCH_HOST_BRIDGE_SMRAM] & SMRAM_G_SMRAME)) {
  334. switch (pd->config[MCH_HOST_BRIDGE_ESMRAMC] &
  335. MCH_HOST_BRIDGE_ESMRAMC_TSEG_SZ_MASK) {
  336. case MCH_HOST_BRIDGE_ESMRAMC_TSEG_SZ_1MB:
  337. tseg_size = 1024 * 1024;
  338. break;
  339. case MCH_HOST_BRIDGE_ESMRAMC_TSEG_SZ_2MB:
  340. tseg_size = 1024 * 1024 * 2;
  341. break;
  342. case MCH_HOST_BRIDGE_ESMRAMC_TSEG_SZ_8MB:
  343. tseg_size = 1024 * 1024 * 8;
  344. break;
  345. default:
  346. tseg_size = 1024 * 1024 * (uint32_t)mch->ext_tseg_mbytes;
  347. break;
  348. }
  349. } else {
  350. tseg_size = 0;
  351. }
  352. memory_region_del_subregion(mch->system_memory, &mch->tseg_blackhole);
  353. memory_region_set_enabled(&mch->tseg_blackhole, tseg_size);
  354. memory_region_set_size(&mch->tseg_blackhole, tseg_size);
  355. memory_region_add_subregion_overlap(mch->system_memory,
  356. mch->below_4g_mem_size - tseg_size,
  357. &mch->tseg_blackhole, 1);
  358. memory_region_set_enabled(&mch->tseg_window, tseg_size);
  359. memory_region_set_size(&mch->tseg_window, tseg_size);
  360. memory_region_set_address(&mch->tseg_window,
  361. mch->below_4g_mem_size - tseg_size);
  362. memory_region_set_alias_offset(&mch->tseg_window,
  363. mch->below_4g_mem_size - tseg_size);
  364. memory_region_transaction_commit();
  365. }
  366. static void mch_update_ext_tseg_mbytes(MCHPCIState *mch)
  367. {
  368. PCIDevice *pd = PCI_DEVICE(mch);
  369. uint8_t *reg = pd->config + MCH_HOST_BRIDGE_EXT_TSEG_MBYTES;
  370. if (mch->ext_tseg_mbytes > 0 &&
  371. pci_get_word(reg) == MCH_HOST_BRIDGE_EXT_TSEG_MBYTES_QUERY) {
  372. pci_set_word(reg, mch->ext_tseg_mbytes);
  373. }
  374. }
  375. static void mch_update_smbase_smram(MCHPCIState *mch)
  376. {
  377. PCIDevice *pd = PCI_DEVICE(mch);
  378. uint8_t *reg = pd->config + MCH_HOST_BRIDGE_F_SMBASE;
  379. bool lck;
  380. if (!mch->has_smram_at_smbase) {
  381. return;
  382. }
  383. if (*reg == MCH_HOST_BRIDGE_F_SMBASE_QUERY) {
  384. pd->wmask[MCH_HOST_BRIDGE_F_SMBASE] =
  385. MCH_HOST_BRIDGE_F_SMBASE_LCK;
  386. *reg = MCH_HOST_BRIDGE_F_SMBASE_IN_RAM;
  387. return;
  388. }
  389. /*
  390. * default/reset state, discard written value
  391. * which will disable SMRAM balackhole at SMBASE
  392. */
  393. if (pd->wmask[MCH_HOST_BRIDGE_F_SMBASE] == 0xff) {
  394. *reg = 0x00;
  395. }
  396. memory_region_transaction_begin();
  397. if (*reg & MCH_HOST_BRIDGE_F_SMBASE_LCK) {
  398. /* disable all writes */
  399. pd->wmask[MCH_HOST_BRIDGE_F_SMBASE] &=
  400. ~MCH_HOST_BRIDGE_F_SMBASE_LCK;
  401. *reg = MCH_HOST_BRIDGE_F_SMBASE_LCK;
  402. lck = true;
  403. } else {
  404. lck = false;
  405. }
  406. memory_region_set_enabled(&mch->smbase_blackhole, lck);
  407. memory_region_set_enabled(&mch->smbase_window, lck);
  408. memory_region_transaction_commit();
  409. }
  410. static void mch_write_config(PCIDevice *d,
  411. uint32_t address, uint32_t val, int len)
  412. {
  413. MCHPCIState *mch = MCH_PCI_DEVICE(d);
  414. pci_default_write_config(d, address, val, len);
  415. if (ranges_overlap(address, len, MCH_HOST_BRIDGE_PAM0,
  416. MCH_HOST_BRIDGE_PAM_SIZE)) {
  417. mch_update_pam(mch);
  418. }
  419. if (ranges_overlap(address, len, MCH_HOST_BRIDGE_PCIEXBAR,
  420. MCH_HOST_BRIDGE_PCIEXBAR_SIZE)) {
  421. mch_update_pciexbar(mch);
  422. }
  423. if (ranges_overlap(address, len, MCH_HOST_BRIDGE_SMRAM,
  424. MCH_HOST_BRIDGE_SMRAM_SIZE)) {
  425. mch_update_smram(mch);
  426. }
  427. if (ranges_overlap(address, len, MCH_HOST_BRIDGE_EXT_TSEG_MBYTES,
  428. MCH_HOST_BRIDGE_EXT_TSEG_MBYTES_SIZE)) {
  429. mch_update_ext_tseg_mbytes(mch);
  430. }
  431. if (ranges_overlap(address, len, MCH_HOST_BRIDGE_F_SMBASE, 1)) {
  432. mch_update_smbase_smram(mch);
  433. }
  434. }
  435. static void mch_update(MCHPCIState *mch)
  436. {
  437. mch_update_pciexbar(mch);
  438. mch_update_pam(mch);
  439. mch_update_smram(mch);
  440. mch_update_ext_tseg_mbytes(mch);
  441. mch_update_smbase_smram(mch);
  442. /*
  443. * pci hole goes from end-of-low-ram to io-apic.
  444. * mmconfig will be excluded by the dsdt builder.
  445. */
  446. range_set_bounds(&mch->pci_hole,
  447. mch->below_4g_mem_size,
  448. IO_APIC_DEFAULT_ADDRESS - 1);
  449. }
  450. static int mch_post_load(void *opaque, int version_id)
  451. {
  452. MCHPCIState *mch = opaque;
  453. mch_update(mch);
  454. return 0;
  455. }
  456. static const VMStateDescription vmstate_mch = {
  457. .name = "mch",
  458. .version_id = 1,
  459. .minimum_version_id = 1,
  460. .post_load = mch_post_load,
  461. .fields = (VMStateField[]) {
  462. VMSTATE_PCI_DEVICE(parent_obj, MCHPCIState),
  463. /* Used to be smm_enabled, which was basically always zero because
  464. * SeaBIOS hardly uses SMM. SMRAM is now handled by CPU code.
  465. */
  466. VMSTATE_UNUSED(1),
  467. VMSTATE_END_OF_LIST()
  468. }
  469. };
  470. static void mch_reset(DeviceState *qdev)
  471. {
  472. PCIDevice *d = PCI_DEVICE(qdev);
  473. MCHPCIState *mch = MCH_PCI_DEVICE(d);
  474. pci_set_quad(d->config + MCH_HOST_BRIDGE_PCIEXBAR,
  475. MCH_HOST_BRIDGE_PCIEXBAR_DEFAULT);
  476. d->config[MCH_HOST_BRIDGE_SMRAM] = MCH_HOST_BRIDGE_SMRAM_DEFAULT;
  477. d->config[MCH_HOST_BRIDGE_ESMRAMC] = MCH_HOST_BRIDGE_ESMRAMC_DEFAULT;
  478. d->wmask[MCH_HOST_BRIDGE_SMRAM] = MCH_HOST_BRIDGE_SMRAM_WMASK;
  479. d->wmask[MCH_HOST_BRIDGE_ESMRAMC] = MCH_HOST_BRIDGE_ESMRAMC_WMASK;
  480. if (mch->ext_tseg_mbytes > 0) {
  481. pci_set_word(d->config + MCH_HOST_BRIDGE_EXT_TSEG_MBYTES,
  482. MCH_HOST_BRIDGE_EXT_TSEG_MBYTES_QUERY);
  483. }
  484. d->config[MCH_HOST_BRIDGE_F_SMBASE] = 0;
  485. d->wmask[MCH_HOST_BRIDGE_F_SMBASE] = 0xff;
  486. mch_update(mch);
  487. }
  488. static void mch_realize(PCIDevice *d, Error **errp)
  489. {
  490. int i;
  491. MCHPCIState *mch = MCH_PCI_DEVICE(d);
  492. if (mch->ext_tseg_mbytes > MCH_HOST_BRIDGE_EXT_TSEG_MBYTES_MAX) {
  493. error_setg(errp, "invalid extended-tseg-mbytes value: %" PRIu16,
  494. mch->ext_tseg_mbytes);
  495. return;
  496. }
  497. /* setup pci memory mapping */
  498. pc_pci_as_mapping_init(mch->system_memory, mch->pci_address_space);
  499. /* if *disabled* show SMRAM to all CPUs */
  500. memory_region_init_alias(&mch->smram_region, OBJECT(mch), "smram-region",
  501. mch->pci_address_space, MCH_HOST_BRIDGE_SMRAM_C_BASE,
  502. MCH_HOST_BRIDGE_SMRAM_C_SIZE);
  503. memory_region_add_subregion_overlap(mch->system_memory, MCH_HOST_BRIDGE_SMRAM_C_BASE,
  504. &mch->smram_region, 1);
  505. memory_region_set_enabled(&mch->smram_region, true);
  506. memory_region_init_alias(&mch->open_high_smram, OBJECT(mch), "smram-open-high",
  507. mch->ram_memory, MCH_HOST_BRIDGE_SMRAM_C_BASE,
  508. MCH_HOST_BRIDGE_SMRAM_C_SIZE);
  509. memory_region_add_subregion_overlap(mch->system_memory, 0xfeda0000,
  510. &mch->open_high_smram, 1);
  511. memory_region_set_enabled(&mch->open_high_smram, false);
  512. /* smram, as seen by SMM CPUs */
  513. memory_region_init(&mch->smram, OBJECT(mch), "smram", 4 * GiB);
  514. memory_region_set_enabled(&mch->smram, true);
  515. memory_region_init_alias(&mch->low_smram, OBJECT(mch), "smram-low",
  516. mch->ram_memory, MCH_HOST_BRIDGE_SMRAM_C_BASE,
  517. MCH_HOST_BRIDGE_SMRAM_C_SIZE);
  518. memory_region_set_enabled(&mch->low_smram, true);
  519. memory_region_add_subregion(&mch->smram, MCH_HOST_BRIDGE_SMRAM_C_BASE,
  520. &mch->low_smram);
  521. memory_region_init_alias(&mch->high_smram, OBJECT(mch), "smram-high",
  522. mch->ram_memory, MCH_HOST_BRIDGE_SMRAM_C_BASE,
  523. MCH_HOST_BRIDGE_SMRAM_C_SIZE);
  524. memory_region_set_enabled(&mch->high_smram, true);
  525. memory_region_add_subregion(&mch->smram, 0xfeda0000, &mch->high_smram);
  526. memory_region_init_io(&mch->tseg_blackhole, OBJECT(mch),
  527. &blackhole_ops, NULL,
  528. "tseg-blackhole", 0);
  529. memory_region_set_enabled(&mch->tseg_blackhole, false);
  530. memory_region_add_subregion_overlap(mch->system_memory,
  531. mch->below_4g_mem_size,
  532. &mch->tseg_blackhole, 1);
  533. memory_region_init_alias(&mch->tseg_window, OBJECT(mch), "tseg-window",
  534. mch->ram_memory, mch->below_4g_mem_size, 0);
  535. memory_region_set_enabled(&mch->tseg_window, false);
  536. memory_region_add_subregion(&mch->smram, mch->below_4g_mem_size,
  537. &mch->tseg_window);
  538. /*
  539. * This is not what hardware does, so it's QEMU specific hack.
  540. * See commit message for details.
  541. */
  542. memory_region_init_io(&mch->smbase_blackhole, OBJECT(mch), &blackhole_ops,
  543. NULL, "smbase-blackhole",
  544. MCH_HOST_BRIDGE_SMBASE_SIZE);
  545. memory_region_set_enabled(&mch->smbase_blackhole, false);
  546. memory_region_add_subregion_overlap(mch->system_memory,
  547. MCH_HOST_BRIDGE_SMBASE_ADDR,
  548. &mch->smbase_blackhole, 1);
  549. memory_region_init_alias(&mch->smbase_window, OBJECT(mch),
  550. "smbase-window", mch->ram_memory,
  551. MCH_HOST_BRIDGE_SMBASE_ADDR,
  552. MCH_HOST_BRIDGE_SMBASE_SIZE);
  553. memory_region_set_enabled(&mch->smbase_window, false);
  554. memory_region_add_subregion(&mch->smram, MCH_HOST_BRIDGE_SMBASE_ADDR,
  555. &mch->smbase_window);
  556. object_property_add_const_link(qdev_get_machine(), "smram",
  557. OBJECT(&mch->smram));
  558. init_pam(DEVICE(mch), mch->ram_memory, mch->system_memory,
  559. mch->pci_address_space, &mch->pam_regions[0],
  560. PAM_BIOS_BASE, PAM_BIOS_SIZE);
  561. for (i = 0; i < ARRAY_SIZE(mch->pam_regions) - 1; ++i) {
  562. init_pam(DEVICE(mch), mch->ram_memory, mch->system_memory,
  563. mch->pci_address_space, &mch->pam_regions[i+1],
  564. PAM_EXPAN_BASE + i * PAM_EXPAN_SIZE, PAM_EXPAN_SIZE);
  565. }
  566. }
  567. uint64_t mch_mcfg_base(void)
  568. {
  569. bool ambiguous;
  570. Object *o = object_resolve_path_type("", TYPE_MCH_PCI_DEVICE, &ambiguous);
  571. if (!o) {
  572. return 0;
  573. }
  574. return MCH_HOST_BRIDGE_PCIEXBAR_DEFAULT;
  575. }
  576. static Property mch_props[] = {
  577. DEFINE_PROP_UINT16("extended-tseg-mbytes", MCHPCIState, ext_tseg_mbytes,
  578. 16),
  579. DEFINE_PROP_BOOL("smbase-smram", MCHPCIState, has_smram_at_smbase, true),
  580. DEFINE_PROP_END_OF_LIST(),
  581. };
  582. static void mch_class_init(ObjectClass *klass, void *data)
  583. {
  584. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  585. DeviceClass *dc = DEVICE_CLASS(klass);
  586. k->realize = mch_realize;
  587. k->config_write = mch_write_config;
  588. dc->reset = mch_reset;
  589. device_class_set_props(dc, mch_props);
  590. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  591. dc->desc = "Host bridge";
  592. dc->vmsd = &vmstate_mch;
  593. k->vendor_id = PCI_VENDOR_ID_INTEL;
  594. /*
  595. * The 'q35' machine type implements an Intel Series 3 chipset,
  596. * of which there are several variants. The key difference between
  597. * the 82P35 MCH ('p35') and 82Q35 GMCH ('q35') variants is that
  598. * the latter has an integrated graphics adapter. QEMU does not
  599. * implement integrated graphics, so uses the PCI ID for the 82P35
  600. * chipset.
  601. */
  602. k->device_id = PCI_DEVICE_ID_INTEL_P35_MCH;
  603. k->revision = MCH_HOST_BRIDGE_REVISION_DEFAULT;
  604. k->class_id = PCI_CLASS_BRIDGE_HOST;
  605. /*
  606. * PCI-facing part of the host bridge, not usable without the
  607. * host-facing part, which can't be device_add'ed, yet.
  608. */
  609. dc->user_creatable = false;
  610. }
  611. static const TypeInfo mch_info = {
  612. .name = TYPE_MCH_PCI_DEVICE,
  613. .parent = TYPE_PCI_DEVICE,
  614. .instance_size = sizeof(MCHPCIState),
  615. .class_init = mch_class_init,
  616. .interfaces = (InterfaceInfo[]) {
  617. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  618. { },
  619. },
  620. };
  621. static void q35_register(void)
  622. {
  623. type_register_static(&mch_info);
  624. type_register_static(&q35_host_info);
  625. }
  626. type_init(q35_register);