lpc_ich9.c 24 KB

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