lpc_ich9.c 27 KB

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