q35.c 25 KB

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