lpc_ich9.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /*
  2. * QEMU ICH9 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 "cpu.h"
  32. #include "qapi/visitor.h"
  33. #include "qemu/range.h"
  34. #include "hw/isa/isa.h"
  35. #include "hw/sysbus.h"
  36. #include "migration/vmstate.h"
  37. #include "hw/irq.h"
  38. #include "hw/isa/apm.h"
  39. #include "hw/pci/pci.h"
  40. #include "hw/pci/pci_bridge.h"
  41. #include "hw/i386/ich9.h"
  42. #include "hw/acpi/acpi.h"
  43. #include "hw/acpi/ich9.h"
  44. #include "hw/pci/pci_bus.h"
  45. #include "hw/qdev-properties.h"
  46. #include "exec/address-spaces.h"
  47. #include "sysemu/runstate.h"
  48. #include "sysemu/sysemu.h"
  49. #include "hw/core/cpu.h"
  50. #include "hw/nvram/fw_cfg.h"
  51. #include "qemu/cutils.h"
  52. /*****************************************************************************/
  53. /* ICH9 LPC PCI to ISA bridge */
  54. static void ich9_lpc_reset(DeviceState *qdev);
  55. /* chipset configuration register
  56. * to access chipset configuration registers, pci_[sg]et_{byte, word, long}
  57. * are used.
  58. * Although it's not pci configuration space, it's little endian as Intel.
  59. */
  60. static void ich9_cc_update_ir(uint8_t irr[PCI_NUM_PINS], uint16_t ir)
  61. {
  62. int intx;
  63. for (intx = 0; intx < PCI_NUM_PINS; intx++) {
  64. irr[intx] = (ir >> (intx * ICH9_CC_DIR_SHIFT)) & ICH9_CC_DIR_MASK;
  65. }
  66. }
  67. static void ich9_cc_update(ICH9LPCState *lpc)
  68. {
  69. int slot;
  70. int pci_intx;
  71. const int reg_offsets[] = {
  72. ICH9_CC_D25IR,
  73. ICH9_CC_D26IR,
  74. ICH9_CC_D27IR,
  75. ICH9_CC_D28IR,
  76. ICH9_CC_D29IR,
  77. ICH9_CC_D30IR,
  78. ICH9_CC_D31IR,
  79. };
  80. const int *offset;
  81. /* D{25 - 31}IR, but D30IR is read only to 0. */
  82. for (slot = 25, offset = reg_offsets; slot < 32; slot++, offset++) {
  83. if (slot == 30) {
  84. continue;
  85. }
  86. ich9_cc_update_ir(lpc->irr[slot],
  87. pci_get_word(lpc->chip_config + *offset));
  88. }
  89. /*
  90. * D30: DMI2PCI bridge
  91. * It is arbitrarily decided how INTx lines of PCI devices behind
  92. * the bridge are connected to pirq lines. Our choice is PIRQ[E-H].
  93. * INT[A-D] are connected to PIRQ[E-H]
  94. */
  95. for (pci_intx = 0; pci_intx < PCI_NUM_PINS; pci_intx++) {
  96. lpc->irr[30][pci_intx] = pci_intx + 4;
  97. }
  98. }
  99. static void ich9_cc_init(ICH9LPCState *lpc)
  100. {
  101. int slot;
  102. int intx;
  103. /* the default irq routing is arbitrary as long as it matches with
  104. * acpi irq routing table.
  105. * The one that is incompatible with piix_pci(= bochs) one is
  106. * intentionally chosen to let the users know that the different
  107. * board is used.
  108. *
  109. * int[A-D] -> pirq[E-F]
  110. * avoid pirq A-D because they are used for pci express port
  111. */
  112. for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
  113. for (intx = 0; intx < PCI_NUM_PINS; intx++) {
  114. lpc->irr[slot][intx] = (slot + intx) % 4 + 4;
  115. }
  116. }
  117. ich9_cc_update(lpc);
  118. }
  119. static void ich9_cc_reset(ICH9LPCState *lpc)
  120. {
  121. uint8_t *c = lpc->chip_config;
  122. memset(lpc->chip_config, 0, sizeof(lpc->chip_config));
  123. pci_set_long(c + ICH9_CC_D31IR, ICH9_CC_DIR_DEFAULT);
  124. pci_set_long(c + ICH9_CC_D30IR, ICH9_CC_D30IR_DEFAULT);
  125. pci_set_long(c + ICH9_CC_D29IR, ICH9_CC_DIR_DEFAULT);
  126. pci_set_long(c + ICH9_CC_D28IR, ICH9_CC_DIR_DEFAULT);
  127. pci_set_long(c + ICH9_CC_D27IR, ICH9_CC_DIR_DEFAULT);
  128. pci_set_long(c + ICH9_CC_D26IR, ICH9_CC_DIR_DEFAULT);
  129. pci_set_long(c + ICH9_CC_D25IR, ICH9_CC_DIR_DEFAULT);
  130. pci_set_long(c + ICH9_CC_GCS, ICH9_CC_GCS_DEFAULT);
  131. ich9_cc_update(lpc);
  132. }
  133. static void ich9_cc_addr_len(uint64_t *addr, unsigned *len)
  134. {
  135. *addr &= ICH9_CC_ADDR_MASK;
  136. if (*addr + *len >= ICH9_CC_SIZE) {
  137. *len = ICH9_CC_SIZE - *addr;
  138. }
  139. }
  140. /* val: little endian */
  141. static void ich9_cc_write(void *opaque, hwaddr addr,
  142. uint64_t val, unsigned len)
  143. {
  144. ICH9LPCState *lpc = (ICH9LPCState *)opaque;
  145. ich9_cc_addr_len(&addr, &len);
  146. memcpy(lpc->chip_config + addr, &val, len);
  147. pci_bus_fire_intx_routing_notifier(pci_get_bus(&lpc->d));
  148. ich9_cc_update(lpc);
  149. }
  150. /* return value: little endian */
  151. static uint64_t ich9_cc_read(void *opaque, hwaddr addr,
  152. unsigned len)
  153. {
  154. ICH9LPCState *lpc = (ICH9LPCState *)opaque;
  155. uint32_t val = 0;
  156. ich9_cc_addr_len(&addr, &len);
  157. memcpy(&val, lpc->chip_config + addr, len);
  158. return val;
  159. }
  160. /* IRQ routing */
  161. /* */
  162. static void ich9_lpc_rout(uint8_t pirq_rout, int *pic_irq, int *pic_dis)
  163. {
  164. *pic_irq = pirq_rout & ICH9_LPC_PIRQ_ROUT_MASK;
  165. *pic_dis = pirq_rout & ICH9_LPC_PIRQ_ROUT_IRQEN;
  166. }
  167. static void ich9_lpc_pic_irq(ICH9LPCState *lpc, int pirq_num,
  168. int *pic_irq, int *pic_dis)
  169. {
  170. switch (pirq_num) {
  171. case 0 ... 3: /* A-D */
  172. ich9_lpc_rout(lpc->d.config[ICH9_LPC_PIRQA_ROUT + pirq_num],
  173. pic_irq, pic_dis);
  174. return;
  175. case 4 ... 7: /* E-H */
  176. ich9_lpc_rout(lpc->d.config[ICH9_LPC_PIRQE_ROUT + (pirq_num - 4)],
  177. pic_irq, pic_dis);
  178. return;
  179. default:
  180. break;
  181. }
  182. abort();
  183. }
  184. /* gsi: i8259+ioapic irq 0-15, otherwise assert */
  185. static void ich9_lpc_update_pic(ICH9LPCState *lpc, int gsi)
  186. {
  187. int i, pic_level;
  188. assert(gsi < ICH9_LPC_PIC_NUM_PINS);
  189. /* The pic level is the logical OR of all the PCI irqs mapped to it */
  190. pic_level = 0;
  191. for (i = 0; i < ICH9_LPC_NB_PIRQS; i++) {
  192. int tmp_irq;
  193. int tmp_dis;
  194. ich9_lpc_pic_irq(lpc, i, &tmp_irq, &tmp_dis);
  195. if (!tmp_dis && tmp_irq == gsi) {
  196. pic_level |= pci_bus_get_irq_level(pci_get_bus(&lpc->d), i);
  197. }
  198. }
  199. if (gsi == lpc->sci_gsi) {
  200. pic_level |= lpc->sci_level;
  201. }
  202. qemu_set_irq(lpc->gsi[gsi], pic_level);
  203. }
  204. /* APIC mode: GSIx: PIRQ[A-H] -> GSI 16, ... no pirq shares same APIC pins. */
  205. static int ich9_pirq_to_gsi(int pirq)
  206. {
  207. return pirq + ICH9_LPC_PIC_NUM_PINS;
  208. }
  209. static int ich9_gsi_to_pirq(int gsi)
  210. {
  211. return gsi - ICH9_LPC_PIC_NUM_PINS;
  212. }
  213. /* gsi: ioapic irq 16-23, otherwise assert */
  214. static void ich9_lpc_update_apic(ICH9LPCState *lpc, int gsi)
  215. {
  216. int level = 0;
  217. assert(gsi >= ICH9_LPC_PIC_NUM_PINS);
  218. level |= pci_bus_get_irq_level(pci_get_bus(&lpc->d), ich9_gsi_to_pirq(gsi));
  219. if (gsi == lpc->sci_gsi) {
  220. level |= lpc->sci_level;
  221. }
  222. qemu_set_irq(lpc->gsi[gsi], level);
  223. }
  224. void ich9_lpc_set_irq(void *opaque, int pirq, int level)
  225. {
  226. ICH9LPCState *lpc = opaque;
  227. int pic_irq, pic_dis;
  228. assert(0 <= pirq);
  229. assert(pirq < ICH9_LPC_NB_PIRQS);
  230. ich9_lpc_update_apic(lpc, ich9_pirq_to_gsi(pirq));
  231. ich9_lpc_pic_irq(lpc, pirq, &pic_irq, &pic_dis);
  232. ich9_lpc_update_pic(lpc, pic_irq);
  233. }
  234. /* return the pirq number (PIRQ[A-H]:0-7) corresponding to
  235. * a given device irq pin.
  236. */
  237. int ich9_lpc_map_irq(PCIDevice *pci_dev, int intx)
  238. {
  239. BusState *bus = qdev_get_parent_bus(&pci_dev->qdev);
  240. PCIBus *pci_bus = PCI_BUS(bus);
  241. PCIDevice *lpc_pdev =
  242. pci_bus->devices[PCI_DEVFN(ICH9_LPC_DEV, ICH9_LPC_FUNC)];
  243. ICH9LPCState *lpc = ICH9_LPC_DEVICE(lpc_pdev);
  244. return lpc->irr[PCI_SLOT(pci_dev->devfn)][intx];
  245. }
  246. PCIINTxRoute ich9_route_intx_pin_to_irq(void *opaque, int pirq_pin)
  247. {
  248. ICH9LPCState *lpc = opaque;
  249. PCIINTxRoute route;
  250. int pic_irq;
  251. int pic_dis;
  252. assert(0 <= pirq_pin);
  253. assert(pirq_pin < ICH9_LPC_NB_PIRQS);
  254. route.mode = PCI_INTX_ENABLED;
  255. ich9_lpc_pic_irq(lpc, pirq_pin, &pic_irq, &pic_dis);
  256. if (!pic_dis) {
  257. if (pic_irq < ICH9_LPC_PIC_NUM_PINS) {
  258. route.irq = pic_irq;
  259. } else {
  260. route.mode = PCI_INTX_DISABLED;
  261. route.irq = -1;
  262. }
  263. } else {
  264. route.irq = ich9_pirq_to_gsi(pirq_pin);
  265. }
  266. return route;
  267. }
  268. void ich9_generate_smi(void)
  269. {
  270. cpu_interrupt(first_cpu, CPU_INTERRUPT_SMI);
  271. }
  272. static int ich9_lpc_sci_irq(ICH9LPCState *lpc)
  273. {
  274. switch (lpc->d.config[ICH9_LPC_ACPI_CTRL] &
  275. ICH9_LPC_ACPI_CTRL_SCI_IRQ_SEL_MASK) {
  276. case ICH9_LPC_ACPI_CTRL_9:
  277. return 9;
  278. case ICH9_LPC_ACPI_CTRL_10:
  279. return 10;
  280. case ICH9_LPC_ACPI_CTRL_11:
  281. return 11;
  282. case ICH9_LPC_ACPI_CTRL_20:
  283. return 20;
  284. case ICH9_LPC_ACPI_CTRL_21:
  285. return 21;
  286. default:
  287. /* reserved */
  288. break;
  289. }
  290. return -1;
  291. }
  292. static void ich9_set_sci(void *opaque, int irq_num, int level)
  293. {
  294. ICH9LPCState *lpc = opaque;
  295. int irq;
  296. assert(irq_num == 0);
  297. level = !!level;
  298. if (level == lpc->sci_level) {
  299. return;
  300. }
  301. lpc->sci_level = level;
  302. irq = lpc->sci_gsi;
  303. if (irq < 0) {
  304. return;
  305. }
  306. if (irq >= ICH9_LPC_PIC_NUM_PINS) {
  307. ich9_lpc_update_apic(lpc, irq);
  308. } else {
  309. ich9_lpc_update_pic(lpc, irq);
  310. }
  311. }
  312. static void smi_features_ok_callback(void *opaque)
  313. {
  314. ICH9LPCState *lpc = opaque;
  315. uint64_t guest_features;
  316. if (lpc->smi_features_ok) {
  317. /* negotiation already complete, features locked */
  318. return;
  319. }
  320. memcpy(&guest_features, lpc->smi_guest_features_le, sizeof guest_features);
  321. le64_to_cpus(&guest_features);
  322. if (guest_features & ~lpc->smi_host_features) {
  323. /* guest requests invalid features, leave @features_ok at zero */
  324. return;
  325. }
  326. /* valid feature subset requested, lock it down, report success */
  327. lpc->smi_negotiated_features = guest_features;
  328. lpc->smi_features_ok = 1;
  329. }
  330. void ich9_lpc_pm_init(PCIDevice *lpc_pci, bool smm_enabled)
  331. {
  332. ICH9LPCState *lpc = ICH9_LPC_DEVICE(lpc_pci);
  333. qemu_irq sci_irq;
  334. FWCfgState *fw_cfg = fw_cfg_find();
  335. sci_irq = qemu_allocate_irq(ich9_set_sci, lpc, 0);
  336. ich9_pm_init(lpc_pci, &lpc->pm, smm_enabled, sci_irq);
  337. if (lpc->smi_host_features && fw_cfg) {
  338. uint64_t host_features_le;
  339. host_features_le = cpu_to_le64(lpc->smi_host_features);
  340. memcpy(lpc->smi_host_features_le, &host_features_le,
  341. sizeof host_features_le);
  342. fw_cfg_add_file(fw_cfg, "etc/smi/supported-features",
  343. lpc->smi_host_features_le,
  344. sizeof lpc->smi_host_features_le);
  345. /* The other two guest-visible fields are cleared on device reset, we
  346. * just link them into fw_cfg here.
  347. */
  348. fw_cfg_add_file_callback(fw_cfg, "etc/smi/requested-features",
  349. NULL, NULL, NULL,
  350. lpc->smi_guest_features_le,
  351. sizeof lpc->smi_guest_features_le,
  352. false);
  353. fw_cfg_add_file_callback(fw_cfg, "etc/smi/features-ok",
  354. smi_features_ok_callback, NULL, lpc,
  355. &lpc->smi_features_ok,
  356. sizeof lpc->smi_features_ok,
  357. true);
  358. }
  359. ich9_lpc_reset(DEVICE(lpc));
  360. }
  361. /* APM */
  362. static void ich9_apm_ctrl_changed(uint32_t val, void *arg)
  363. {
  364. ICH9LPCState *lpc = arg;
  365. /* ACPI specs 3.0, 4.7.2.5 */
  366. acpi_pm1_cnt_update(&lpc->pm.acpi_regs,
  367. val == ICH9_APM_ACPI_ENABLE,
  368. val == ICH9_APM_ACPI_DISABLE);
  369. if (val == ICH9_APM_ACPI_ENABLE || val == ICH9_APM_ACPI_DISABLE) {
  370. return;
  371. }
  372. /* SMI_EN = PMBASE + 30. SMI control and enable register */
  373. if (lpc->pm.smi_en & ICH9_PMIO_SMI_EN_APMC_EN) {
  374. if (lpc->smi_negotiated_features &
  375. (UINT64_C(1) << ICH9_LPC_SMI_F_BROADCAST_BIT)) {
  376. CPUState *cs;
  377. CPU_FOREACH(cs) {
  378. cpu_interrupt(cs, CPU_INTERRUPT_SMI);
  379. }
  380. } else {
  381. cpu_interrupt(current_cpu, CPU_INTERRUPT_SMI);
  382. }
  383. }
  384. }
  385. /* config:PMBASE */
  386. static void
  387. ich9_lpc_pmbase_sci_update(ICH9LPCState *lpc)
  388. {
  389. uint32_t pm_io_base = pci_get_long(lpc->d.config + ICH9_LPC_PMBASE);
  390. uint8_t acpi_cntl = pci_get_long(lpc->d.config + ICH9_LPC_ACPI_CTRL);
  391. uint8_t new_gsi;
  392. if (acpi_cntl & ICH9_LPC_ACPI_CTRL_ACPI_EN) {
  393. pm_io_base &= ICH9_LPC_PMBASE_BASE_ADDRESS_MASK;
  394. } else {
  395. pm_io_base = 0;
  396. }
  397. ich9_pm_iospace_update(&lpc->pm, pm_io_base);
  398. new_gsi = ich9_lpc_sci_irq(lpc);
  399. if (lpc->sci_level && new_gsi != lpc->sci_gsi) {
  400. qemu_set_irq(lpc->pm.irq, 0);
  401. lpc->sci_gsi = new_gsi;
  402. qemu_set_irq(lpc->pm.irq, 1);
  403. }
  404. lpc->sci_gsi = new_gsi;
  405. }
  406. /* config:RCBA */
  407. static void ich9_lpc_rcba_update(ICH9LPCState *lpc, uint32_t rcba_old)
  408. {
  409. uint32_t rcba = pci_get_long(lpc->d.config + ICH9_LPC_RCBA);
  410. if (rcba_old & ICH9_LPC_RCBA_EN) {
  411. memory_region_del_subregion(get_system_memory(), &lpc->rcrb_mem);
  412. }
  413. if (rcba & ICH9_LPC_RCBA_EN) {
  414. memory_region_add_subregion_overlap(get_system_memory(),
  415. rcba & ICH9_LPC_RCBA_BA_MASK,
  416. &lpc->rcrb_mem, 1);
  417. }
  418. }
  419. /* config:GEN_PMCON* */
  420. static void
  421. ich9_lpc_pmcon_update(ICH9LPCState *lpc)
  422. {
  423. uint16_t gen_pmcon_1 = pci_get_word(lpc->d.config + ICH9_LPC_GEN_PMCON_1);
  424. uint16_t wmask;
  425. if (gen_pmcon_1 & ICH9_LPC_GEN_PMCON_1_SMI_LOCK) {
  426. wmask = pci_get_word(lpc->d.wmask + ICH9_LPC_GEN_PMCON_1);
  427. wmask &= ~ICH9_LPC_GEN_PMCON_1_SMI_LOCK;
  428. pci_set_word(lpc->d.wmask + ICH9_LPC_GEN_PMCON_1, wmask);
  429. lpc->pm.smi_en_wmask &= ~1;
  430. }
  431. }
  432. static int ich9_lpc_post_load(void *opaque, int version_id)
  433. {
  434. ICH9LPCState *lpc = opaque;
  435. ich9_lpc_pmbase_sci_update(lpc);
  436. ich9_lpc_rcba_update(lpc, 0 /* disabled ICH9_LPC_RCBA_EN */);
  437. ich9_lpc_pmcon_update(lpc);
  438. return 0;
  439. }
  440. static void ich9_lpc_config_write(PCIDevice *d,
  441. uint32_t addr, uint32_t val, int len)
  442. {
  443. ICH9LPCState *lpc = ICH9_LPC_DEVICE(d);
  444. uint32_t rcba_old = pci_get_long(d->config + ICH9_LPC_RCBA);
  445. pci_default_write_config(d, addr, val, len);
  446. if (ranges_overlap(addr, len, ICH9_LPC_PMBASE, 4) ||
  447. ranges_overlap(addr, len, ICH9_LPC_ACPI_CTRL, 1)) {
  448. ich9_lpc_pmbase_sci_update(lpc);
  449. }
  450. if (ranges_overlap(addr, len, ICH9_LPC_RCBA, 4)) {
  451. ich9_lpc_rcba_update(lpc, rcba_old);
  452. }
  453. if (ranges_overlap(addr, len, ICH9_LPC_PIRQA_ROUT, 4)) {
  454. pci_bus_fire_intx_routing_notifier(pci_get_bus(&lpc->d));
  455. }
  456. if (ranges_overlap(addr, len, ICH9_LPC_PIRQE_ROUT, 4)) {
  457. pci_bus_fire_intx_routing_notifier(pci_get_bus(&lpc->d));
  458. }
  459. if (ranges_overlap(addr, len, ICH9_LPC_GEN_PMCON_1, 8)) {
  460. ich9_lpc_pmcon_update(lpc);
  461. }
  462. }
  463. static void ich9_lpc_reset(DeviceState *qdev)
  464. {
  465. PCIDevice *d = PCI_DEVICE(qdev);
  466. ICH9LPCState *lpc = ICH9_LPC_DEVICE(d);
  467. uint32_t rcba_old = pci_get_long(d->config + ICH9_LPC_RCBA);
  468. int i;
  469. for (i = 0; i < 4; i++) {
  470. pci_set_byte(d->config + ICH9_LPC_PIRQA_ROUT + i,
  471. ICH9_LPC_PIRQ_ROUT_DEFAULT);
  472. }
  473. for (i = 0; i < 4; i++) {
  474. pci_set_byte(d->config + ICH9_LPC_PIRQE_ROUT + i,
  475. ICH9_LPC_PIRQ_ROUT_DEFAULT);
  476. }
  477. pci_set_byte(d->config + ICH9_LPC_ACPI_CTRL, ICH9_LPC_ACPI_CTRL_DEFAULT);
  478. pci_set_long(d->config + ICH9_LPC_PMBASE, ICH9_LPC_PMBASE_DEFAULT);
  479. pci_set_long(d->config + ICH9_LPC_RCBA, ICH9_LPC_RCBA_DEFAULT);
  480. ich9_cc_reset(lpc);
  481. ich9_lpc_pmbase_sci_update(lpc);
  482. ich9_lpc_rcba_update(lpc, rcba_old);
  483. lpc->sci_level = 0;
  484. lpc->rst_cnt = 0;
  485. memset(lpc->smi_guest_features_le, 0, sizeof lpc->smi_guest_features_le);
  486. lpc->smi_features_ok = 0;
  487. lpc->smi_negotiated_features = 0;
  488. }
  489. /* root complex register block is mapped into memory space */
  490. static const MemoryRegionOps rcrb_mmio_ops = {
  491. .read = ich9_cc_read,
  492. .write = ich9_cc_write,
  493. .endianness = DEVICE_LITTLE_ENDIAN,
  494. };
  495. static void ich9_lpc_machine_ready(Notifier *n, void *opaque)
  496. {
  497. ICH9LPCState *s = container_of(n, ICH9LPCState, machine_ready);
  498. MemoryRegion *io_as = pci_address_space_io(&s->d);
  499. uint8_t *pci_conf;
  500. pci_conf = s->d.config;
  501. if (memory_region_present(io_as, 0x3f8)) {
  502. /* com1 */
  503. pci_conf[0x82] |= 0x01;
  504. }
  505. if (memory_region_present(io_as, 0x2f8)) {
  506. /* com2 */
  507. pci_conf[0x82] |= 0x02;
  508. }
  509. if (memory_region_present(io_as, 0x378)) {
  510. /* lpt */
  511. pci_conf[0x82] |= 0x04;
  512. }
  513. if (memory_region_present(io_as, 0x3f2)) {
  514. /* floppy */
  515. pci_conf[0x82] |= 0x08;
  516. }
  517. }
  518. /* reset control */
  519. static void ich9_rst_cnt_write(void *opaque, hwaddr addr, uint64_t val,
  520. unsigned len)
  521. {
  522. ICH9LPCState *lpc = opaque;
  523. if (val & 4) {
  524. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  525. return;
  526. }
  527. lpc->rst_cnt = val & 0xA; /* keep FULL_RST (bit 3) and SYS_RST (bit 1) */
  528. }
  529. static uint64_t ich9_rst_cnt_read(void *opaque, hwaddr addr, unsigned len)
  530. {
  531. ICH9LPCState *lpc = opaque;
  532. return lpc->rst_cnt;
  533. }
  534. static const MemoryRegionOps ich9_rst_cnt_ops = {
  535. .read = ich9_rst_cnt_read,
  536. .write = ich9_rst_cnt_write,
  537. .endianness = DEVICE_LITTLE_ENDIAN
  538. };
  539. static void ich9_lpc_initfn(Object *obj)
  540. {
  541. ICH9LPCState *lpc = ICH9_LPC_DEVICE(obj);
  542. static const uint8_t acpi_enable_cmd = ICH9_APM_ACPI_ENABLE;
  543. static const uint8_t acpi_disable_cmd = ICH9_APM_ACPI_DISABLE;
  544. object_property_add_uint8_ptr(obj, ACPI_PM_PROP_SCI_INT,
  545. &lpc->sci_gsi, OBJ_PROP_FLAG_READ);
  546. object_property_add_uint8_ptr(OBJECT(lpc), ACPI_PM_PROP_ACPI_ENABLE_CMD,
  547. &acpi_enable_cmd, OBJ_PROP_FLAG_READ);
  548. object_property_add_uint8_ptr(OBJECT(lpc), ACPI_PM_PROP_ACPI_DISABLE_CMD,
  549. &acpi_disable_cmd, OBJ_PROP_FLAG_READ);
  550. ich9_pm_add_properties(obj, &lpc->pm);
  551. }
  552. static void ich9_lpc_realize(PCIDevice *d, Error **errp)
  553. {
  554. ICH9LPCState *lpc = ICH9_LPC_DEVICE(d);
  555. DeviceState *dev = DEVICE(d);
  556. ISABus *isa_bus;
  557. isa_bus = isa_bus_new(DEVICE(d), get_system_memory(), get_system_io(),
  558. errp);
  559. if (!isa_bus) {
  560. return;
  561. }
  562. pci_set_long(d->wmask + ICH9_LPC_PMBASE,
  563. ICH9_LPC_PMBASE_BASE_ADDRESS_MASK);
  564. pci_set_byte(d->wmask + ICH9_LPC_PMBASE,
  565. ICH9_LPC_ACPI_CTRL_ACPI_EN |
  566. ICH9_LPC_ACPI_CTRL_SCI_IRQ_SEL_MASK);
  567. memory_region_init_io(&lpc->rcrb_mem, OBJECT(d), &rcrb_mmio_ops, lpc,
  568. "lpc-rcrb-mmio", ICH9_CC_SIZE);
  569. lpc->isa_bus = isa_bus;
  570. ich9_cc_init(lpc);
  571. apm_init(d, &lpc->apm, ich9_apm_ctrl_changed, lpc);
  572. lpc->machine_ready.notify = ich9_lpc_machine_ready;
  573. qemu_add_machine_init_done_notifier(&lpc->machine_ready);
  574. memory_region_init_io(&lpc->rst_cnt_mem, OBJECT(d), &ich9_rst_cnt_ops, lpc,
  575. "lpc-reset-control", 1);
  576. memory_region_add_subregion_overlap(pci_address_space_io(d),
  577. ICH9_RST_CNT_IOPORT, &lpc->rst_cnt_mem,
  578. 1);
  579. qdev_init_gpio_out_named(dev, lpc->gsi, ICH9_GPIO_GSI, GSI_NUM_PINS);
  580. isa_bus_irqs(isa_bus, lpc->gsi);
  581. }
  582. static bool ich9_rst_cnt_needed(void *opaque)
  583. {
  584. ICH9LPCState *lpc = opaque;
  585. return (lpc->rst_cnt != 0);
  586. }
  587. static const VMStateDescription vmstate_ich9_rst_cnt = {
  588. .name = "ICH9LPC/rst_cnt",
  589. .version_id = 1,
  590. .minimum_version_id = 1,
  591. .needed = ich9_rst_cnt_needed,
  592. .fields = (VMStateField[]) {
  593. VMSTATE_UINT8(rst_cnt, ICH9LPCState),
  594. VMSTATE_END_OF_LIST()
  595. }
  596. };
  597. static bool ich9_smi_feat_needed(void *opaque)
  598. {
  599. ICH9LPCState *lpc = opaque;
  600. return !buffer_is_zero(lpc->smi_guest_features_le,
  601. sizeof lpc->smi_guest_features_le) ||
  602. lpc->smi_features_ok;
  603. }
  604. static const VMStateDescription vmstate_ich9_smi_feat = {
  605. .name = "ICH9LPC/smi_feat",
  606. .version_id = 1,
  607. .minimum_version_id = 1,
  608. .needed = ich9_smi_feat_needed,
  609. .fields = (VMStateField[]) {
  610. VMSTATE_UINT8_ARRAY(smi_guest_features_le, ICH9LPCState,
  611. sizeof(uint64_t)),
  612. VMSTATE_UINT8(smi_features_ok, ICH9LPCState),
  613. VMSTATE_UINT64(smi_negotiated_features, ICH9LPCState),
  614. VMSTATE_END_OF_LIST()
  615. }
  616. };
  617. static const VMStateDescription vmstate_ich9_lpc = {
  618. .name = "ICH9LPC",
  619. .version_id = 1,
  620. .minimum_version_id = 1,
  621. .post_load = ich9_lpc_post_load,
  622. .fields = (VMStateField[]) {
  623. VMSTATE_PCI_DEVICE(d, ICH9LPCState),
  624. VMSTATE_STRUCT(apm, ICH9LPCState, 0, vmstate_apm, APMState),
  625. VMSTATE_STRUCT(pm, ICH9LPCState, 0, vmstate_ich9_pm, ICH9LPCPMRegs),
  626. VMSTATE_UINT8_ARRAY(chip_config, ICH9LPCState, ICH9_CC_SIZE),
  627. VMSTATE_UINT32(sci_level, ICH9LPCState),
  628. VMSTATE_END_OF_LIST()
  629. },
  630. .subsections = (const VMStateDescription*[]) {
  631. &vmstate_ich9_rst_cnt,
  632. &vmstate_ich9_smi_feat,
  633. NULL
  634. }
  635. };
  636. static Property ich9_lpc_properties[] = {
  637. DEFINE_PROP_BOOL("noreboot", ICH9LPCState, pin_strap.spkr_hi, true),
  638. DEFINE_PROP_BIT64("x-smi-broadcast", ICH9LPCState, smi_host_features,
  639. ICH9_LPC_SMI_F_BROADCAST_BIT, true),
  640. DEFINE_PROP_END_OF_LIST(),
  641. };
  642. static void ich9_send_gpe(AcpiDeviceIf *adev, AcpiEventStatusBits ev)
  643. {
  644. ICH9LPCState *s = ICH9_LPC_DEVICE(adev);
  645. acpi_send_gpe_event(&s->pm.acpi_regs, s->pm.irq, ev);
  646. }
  647. static void ich9_lpc_class_init(ObjectClass *klass, void *data)
  648. {
  649. DeviceClass *dc = DEVICE_CLASS(klass);
  650. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  651. HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
  652. AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_CLASS(klass);
  653. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  654. dc->reset = ich9_lpc_reset;
  655. k->realize = ich9_lpc_realize;
  656. dc->vmsd = &vmstate_ich9_lpc;
  657. device_class_set_props(dc, ich9_lpc_properties);
  658. k->config_write = ich9_lpc_config_write;
  659. dc->desc = "ICH9 LPC bridge";
  660. k->vendor_id = PCI_VENDOR_ID_INTEL;
  661. k->device_id = PCI_DEVICE_ID_INTEL_ICH9_8;
  662. k->revision = ICH9_A2_LPC_REVISION;
  663. k->class_id = PCI_CLASS_BRIDGE_ISA;
  664. /*
  665. * Reason: part of ICH9 southbridge, needs to be wired up by
  666. * pc_q35_init()
  667. */
  668. dc->user_creatable = false;
  669. hc->pre_plug = ich9_pm_device_pre_plug_cb;
  670. hc->plug = ich9_pm_device_plug_cb;
  671. hc->unplug_request = ich9_pm_device_unplug_request_cb;
  672. hc->unplug = ich9_pm_device_unplug_cb;
  673. adevc->ospm_status = ich9_pm_ospm_status;
  674. adevc->send_event = ich9_send_gpe;
  675. adevc->madt_cpu = pc_madt_cpu_entry;
  676. }
  677. static const TypeInfo ich9_lpc_info = {
  678. .name = TYPE_ICH9_LPC_DEVICE,
  679. .parent = TYPE_PCI_DEVICE,
  680. .instance_size = sizeof(struct ICH9LPCState),
  681. .instance_init = ich9_lpc_initfn,
  682. .class_init = ich9_lpc_class_init,
  683. .interfaces = (InterfaceInfo[]) {
  684. { TYPE_HOTPLUG_HANDLER },
  685. { TYPE_ACPI_DEVICE_IF },
  686. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  687. { }
  688. }
  689. };
  690. static void ich9_lpc_register(void)
  691. {
  692. type_register_static(&ich9_lpc_info);
  693. }
  694. type_init(ich9_lpc_register);