lpc_ich9.c 27 KB

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