2 * Copyright (c) 2006 Fabrice Bellard
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * Copyright (c) 2009, 2010, 2011
26 * Isaku Yamahata <yamahata at valinux co jp>
27 * VA Linux Systems Japan K.K.
28 * Copyright (C) 2012 Jason Baron <jbaron@redhat.com>
30 * This is based on piix_pci.c, but heavily modified.
32 * This library is free software; you can redistribute it and/or
33 * modify it under the terms of the GNU Lesser General Public
34 * License as published by the Free Software Foundation; either
35 * version 2 of the License, or (at your option) any later version.
37 * This library is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
40 * Lesser General Public License for more details.
42 * You should have received a copy of the GNU Lesser General Public
43 * License along with this library; if not, see <http://www.gnu.org/licenses/>
46 #include "qemu-common.h"
55 #include "pcie_host.h"
56 #include "pci_bridge.h"
59 #include "acpi_ich9.h"
61 #include "pci_internals.h"
62 #include "exec-memory.h"
65 static int ich9_lpc_sci_irq(ICH9LPCState
*lpc
);
67 /*****************************************************************************/
68 /* ICH9 LPC PCI to ISA bridge */
70 static void ich9_lpc_reset(DeviceState
*qdev
);
72 /* chipset configuration register
73 * to access chipset configuration registers, pci_[sg]et_{byte, word, long}
75 * Although it's not pci configuration space, it's little endian as Intel.
78 static void ich9_cc_update_ir(uint8_t irr
[PCI_NUM_PINS
], uint16_t ir
)
81 for (intx
= 0; intx
< PCI_NUM_PINS
; intx
++) {
82 irr
[intx
] = (ir
>> (intx
* ICH9_CC_DIR_SHIFT
)) & ICH9_CC_DIR_MASK
;
86 static void ich9_cc_update(ICH9LPCState
*lpc
)
91 const int reg_offsets
[] = {
102 /* D{25 - 31}IR, but D30IR is read only to 0. */
103 for (slot
= 25, offset
= reg_offsets
; slot
< 32; slot
++, offset
++) {
107 ich9_cc_update_ir(lpc
->irr
[slot
],
108 pci_get_word(lpc
->chip_config
+ *offset
));
112 * D30: DMI2PCI bridge
113 * It is arbitrarily decided how INTx lines of PCI devicesbehind the bridge
114 * are connected to pirq lines. Our choice is PIRQ[E-H].
115 * INT[A-D] are connected to PIRQ[E-H]
117 for (pci_intx
= 0; pci_intx
< PCI_NUM_PINS
; pci_intx
++) {
118 lpc
->irr
[30][pci_intx
] = pci_intx
+ 4;
122 static void ich9_cc_init(ICH9LPCState
*lpc
)
127 /* the default irq routing is arbitrary as long as it matches with
128 * acpi irq routing table.
129 * The one that is incompatible with piix_pci(= bochs) one is
130 * intentionally chosen to let the users know that the different
133 * int[A-D] -> pirq[E-F]
134 * avoid pirq A-D because they are used for pci express port
136 for (slot
= 0; slot
< PCI_SLOT_MAX
; slot
++) {
137 for (intx
= 0; intx
< PCI_NUM_PINS
; intx
++) {
138 lpc
->irr
[slot
][intx
] = (slot
+ intx
) % 4 + 4;
144 static void ich9_cc_reset(ICH9LPCState
*lpc
)
146 uint8_t *c
= lpc
->chip_config
;
148 memset(lpc
->chip_config
, 0, sizeof(lpc
->chip_config
));
150 pci_set_long(c
+ ICH9_CC_D31IR
, ICH9_CC_DIR_DEFAULT
);
151 pci_set_long(c
+ ICH9_CC_D30IR
, ICH9_CC_D30IR_DEFAULT
);
152 pci_set_long(c
+ ICH9_CC_D29IR
, ICH9_CC_DIR_DEFAULT
);
153 pci_set_long(c
+ ICH9_CC_D28IR
, ICH9_CC_DIR_DEFAULT
);
154 pci_set_long(c
+ ICH9_CC_D27IR
, ICH9_CC_DIR_DEFAULT
);
155 pci_set_long(c
+ ICH9_CC_D26IR
, ICH9_CC_DIR_DEFAULT
);
156 pci_set_long(c
+ ICH9_CC_D25IR
, ICH9_CC_DIR_DEFAULT
);
161 static void ich9_cc_addr_len(uint64_t *addr
, unsigned *len
)
163 *addr
&= ICH9_CC_ADDR_MASK
;
164 if (*addr
+ *len
>= ICH9_CC_SIZE
) {
165 *len
= ICH9_CC_SIZE
- *addr
;
169 /* val: little endian */
170 static void ich9_cc_write(void *opaque
, hwaddr addr
,
171 uint64_t val
, unsigned len
)
173 ICH9LPCState
*lpc
= (ICH9LPCState
*)opaque
;
175 ich9_cc_addr_len(&addr
, &len
);
176 memcpy(lpc
->chip_config
+ addr
, &val
, len
);
180 /* return value: little endian */
181 static uint64_t ich9_cc_read(void *opaque
, hwaddr addr
,
184 ICH9LPCState
*lpc
= (ICH9LPCState
*)opaque
;
187 ich9_cc_addr_len(&addr
, &len
);
188 memcpy(&val
, lpc
->chip_config
+ addr
, len
);
194 static void ich9_lpc_rout(uint8_t pirq_rout
, int *pic_irq
, int *pic_dis
)
196 *pic_irq
= pirq_rout
& ICH9_LPC_PIRQ_ROUT_MASK
;
197 *pic_dis
= pirq_rout
& ICH9_LPC_PIRQ_ROUT_IRQEN
;
200 static void ich9_lpc_pic_irq(ICH9LPCState
*lpc
, int pirq_num
,
201 int *pic_irq
, int *pic_dis
)
204 case 0 ... 3: /* A-D */
205 ich9_lpc_rout(lpc
->d
.config
[ICH9_LPC_PIRQA_ROUT
+ pirq_num
],
208 case 4 ... 7: /* E-H */
209 ich9_lpc_rout(lpc
->d
.config
[ICH9_LPC_PIRQE_ROUT
+ (pirq_num
- 4)],
218 /* pic_irq: i8254 irq 0-15 */
219 static void ich9_lpc_update_pic(ICH9LPCState
*lpc
, int pic_irq
)
223 /* The pic level is the logical OR of all the PCI irqs mapped to it */
225 for (i
= 0; i
< ICH9_LPC_NB_PIRQS
; i
++) {
228 ich9_lpc_pic_irq(lpc
, i
, &tmp_irq
, &tmp_dis
);
229 if (!tmp_dis
&& pic_irq
== tmp_irq
) {
230 pic_level
|= pci_bus_get_irq_level(lpc
->d
.bus
, i
);
233 if (pic_irq
== ich9_lpc_sci_irq(lpc
)) {
234 pic_level
|= lpc
->sci_level
;
237 qemu_set_irq(lpc
->pic
[pic_irq
], pic_level
);
240 /* pirq: pirq[A-H] 0-7*/
241 static void ich9_lpc_update_by_pirq(ICH9LPCState
*lpc
, int pirq
)
246 ich9_lpc_pic_irq(lpc
, pirq
, &pic_irq
, &pic_dis
);
247 assert(pic_irq
< ICH9_LPC_PIC_NUM_PINS
);
252 ich9_lpc_update_pic(lpc
, pic_irq
);
255 /* APIC mode: GSIx: PIRQ[A-H] -> GSI 16, ... no pirq shares same APIC pins. */
256 static int ich9_pirq_to_gsi(int pirq
)
258 return pirq
+ ICH9_LPC_PIC_NUM_PINS
;
261 static int ich9_gsi_to_pirq(int gsi
)
263 return gsi
- ICH9_LPC_PIC_NUM_PINS
;
266 static void ich9_lpc_update_apic(ICH9LPCState
*lpc
, int gsi
)
270 if (gsi
>= ICH9_LPC_PIC_NUM_PINS
) {
271 level
|= pci_bus_get_irq_level(lpc
->d
.bus
, ich9_gsi_to_pirq(gsi
));
273 if (gsi
== ich9_lpc_sci_irq(lpc
)) {
274 level
|= lpc
->sci_level
;
277 qemu_set_irq(lpc
->ioapic
[gsi
], level
);
280 void ich9_lpc_set_irq(void *opaque
, int pirq
, int level
)
282 ICH9LPCState
*lpc
= opaque
;
285 assert(pirq
< ICH9_LPC_NB_PIRQS
);
287 ich9_lpc_update_apic(lpc
, ich9_pirq_to_gsi(pirq
));
288 ich9_lpc_update_by_pirq(lpc
, pirq
);
291 /* return the pirq number (PIRQ[A-H]:0-7) corresponding to
292 * a given device irq pin.
294 int ich9_lpc_map_irq(PCIDevice
*pci_dev
, int intx
)
296 BusState
*bus
= qdev_get_parent_bus(&pci_dev
->qdev
);
297 PCIBus
*pci_bus
= PCI_BUS(bus
);
298 PCIDevice
*lpc_pdev
=
299 pci_bus
->devices
[PCI_DEVFN(ICH9_LPC_DEV
, ICH9_LPC_FUNC
)];
300 ICH9LPCState
*lpc
= ICH9_LPC_DEVICE(lpc_pdev
);
302 return lpc
->irr
[PCI_SLOT(pci_dev
->devfn
)][intx
];
305 static int ich9_lpc_sci_irq(ICH9LPCState
*lpc
)
307 switch (lpc
->d
.config
[ICH9_LPC_ACPI_CTRL
] &
308 ICH9_LPC_ACPI_CTRL_SCI_IRQ_SEL_MASK
) {
309 case ICH9_LPC_ACPI_CTRL_9
:
311 case ICH9_LPC_ACPI_CTRL_10
:
313 case ICH9_LPC_ACPI_CTRL_11
:
315 case ICH9_LPC_ACPI_CTRL_20
:
317 case ICH9_LPC_ACPI_CTRL_21
:
326 static void ich9_set_sci(void *opaque
, int irq_num
, int level
)
328 ICH9LPCState
*lpc
= opaque
;
331 assert(irq_num
== 0);
333 if (level
== lpc
->sci_level
) {
336 lpc
->sci_level
= level
;
338 irq
= ich9_lpc_sci_irq(lpc
);
343 ich9_lpc_update_apic(lpc
, irq
);
344 if (irq
< ICH9_LPC_PIC_NUM_PINS
) {
345 ich9_lpc_update_pic(lpc
, irq
);
349 void ich9_lpc_pm_init(PCIDevice
*lpc_pci
, qemu_irq cmos_s3
)
351 ICH9LPCState
*lpc
= ICH9_LPC_DEVICE(lpc_pci
);
354 sci_irq
= qemu_allocate_irqs(ich9_set_sci
, lpc
, 1);
355 ich9_pm_init(&lpc
->pm
, sci_irq
[0], cmos_s3
);
357 ich9_lpc_reset(&lpc
->d
.qdev
);
362 static void ich9_apm_ctrl_changed(uint32_t val
, void *arg
)
364 ICH9LPCState
*lpc
= arg
;
366 /* ACPI specs 3.0, 4.7.2.5 */
367 acpi_pm1_cnt_update(&lpc
->pm
.acpi_regs
,
368 val
== ICH9_APM_ACPI_ENABLE
,
369 val
== ICH9_APM_ACPI_DISABLE
);
371 /* SMI_EN = PMBASE + 30. SMI control and enable register */
372 if (lpc
->pm
.smi_en
& ICH9_PMIO_SMI_EN_APMC_EN
) {
373 cpu_interrupt(first_cpu
, CPU_INTERRUPT_SMI
);
379 ich9_lpc_pmbase_update(ICH9LPCState
*lpc
)
381 uint32_t pm_io_base
= pci_get_long(lpc
->d
.config
+ ICH9_LPC_PMBASE
);
382 pm_io_base
&= ICH9_LPC_PMBASE_BASE_ADDRESS_MASK
;
384 ich9_pm_iospace_update(&lpc
->pm
, pm_io_base
);
388 static void ich9_lpc_rcba_update(ICH9LPCState
*lpc
, uint32_t rbca_old
)
390 uint32_t rbca
= pci_get_long(lpc
->d
.config
+ ICH9_LPC_RCBA
);
392 if (rbca_old
& ICH9_LPC_RCBA_EN
) {
393 memory_region_del_subregion(get_system_memory(), &lpc
->rbca_mem
);
395 if (rbca
& ICH9_LPC_RCBA_EN
) {
396 memory_region_add_subregion_overlap(get_system_memory(),
397 rbca
& ICH9_LPC_RCBA_BA_MASK
,
402 static int ich9_lpc_post_load(void *opaque
, int version_id
)
404 ICH9LPCState
*lpc
= opaque
;
406 ich9_lpc_pmbase_update(lpc
);
407 ich9_lpc_rcba_update(lpc
, 0 /* disabled ICH9_LPC_RBCA_EN */);
411 static void ich9_lpc_config_write(PCIDevice
*d
,
412 uint32_t addr
, uint32_t val
, int len
)
414 ICH9LPCState
*lpc
= ICH9_LPC_DEVICE(d
);
415 uint32_t rbca_old
= pci_get_long(d
->config
+ ICH9_LPC_RCBA
);
417 pci_default_write_config(d
, addr
, val
, len
);
418 if (ranges_overlap(addr
, len
, ICH9_LPC_PMBASE
, 4)) {
419 ich9_lpc_pmbase_update(lpc
);
421 if (ranges_overlap(addr
, len
, ICH9_LPC_RCBA
, 4)) {
422 ich9_lpc_rcba_update(lpc
, rbca_old
);
426 static void ich9_lpc_reset(DeviceState
*qdev
)
428 PCIDevice
*d
= PCI_DEVICE(qdev
);
429 ICH9LPCState
*lpc
= ICH9_LPC_DEVICE(d
);
430 uint32_t rbca_old
= pci_get_long(d
->config
+ ICH9_LPC_RCBA
);
433 for (i
= 0; i
< 4; i
++) {
434 pci_set_byte(d
->config
+ ICH9_LPC_PIRQA_ROUT
+ i
,
435 ICH9_LPC_PIRQ_ROUT_DEFAULT
);
437 for (i
= 0; i
< 4; i
++) {
438 pci_set_byte(d
->config
+ ICH9_LPC_PIRQE_ROUT
+ i
,
439 ICH9_LPC_PIRQ_ROUT_DEFAULT
);
441 pci_set_byte(d
->config
+ ICH9_LPC_ACPI_CTRL
, ICH9_LPC_ACPI_CTRL_DEFAULT
);
443 pci_set_long(d
->config
+ ICH9_LPC_PMBASE
, ICH9_LPC_PMBASE_DEFAULT
);
444 pci_set_long(d
->config
+ ICH9_LPC_RCBA
, ICH9_LPC_RCBA_DEFAULT
);
448 ich9_lpc_pmbase_update(lpc
);
449 ich9_lpc_rcba_update(lpc
, rbca_old
);
454 static const MemoryRegionOps rbca_mmio_ops
= {
455 .read
= ich9_cc_read
,
456 .write
= ich9_cc_write
,
457 .endianness
= DEVICE_LITTLE_ENDIAN
,
460 static void ich9_lpc_machine_ready(Notifier
*n
, void *opaque
)
462 ICH9LPCState
*s
= container_of(n
, ICH9LPCState
, machine_ready
);
465 pci_conf
= s
->d
.config
;
466 if (isa_is_ioport_assigned(0x3f8)) {
468 pci_conf
[0x82] |= 0x01;
470 if (isa_is_ioport_assigned(0x2f8)) {
472 pci_conf
[0x82] |= 0x02;
474 if (isa_is_ioport_assigned(0x378)) {
476 pci_conf
[0x82] |= 0x04;
478 if (isa_is_ioport_assigned(0x3f0)) {
480 pci_conf
[0x82] |= 0x08;
484 static int ich9_lpc_initfn(PCIDevice
*d
)
486 ICH9LPCState
*lpc
= ICH9_LPC_DEVICE(d
);
489 isa_bus
= isa_bus_new(&d
->qdev
, get_system_io());
491 pci_set_long(d
->wmask
+ ICH9_LPC_PMBASE
,
492 ICH9_LPC_PMBASE_BASE_ADDRESS_MASK
);
494 memory_region_init_io(&lpc
->rbca_mem
, &rbca_mmio_ops
, lpc
,
495 "lpc-rbca-mmio", ICH9_CC_SIZE
);
497 lpc
->isa_bus
= isa_bus
;
500 apm_init(d
, &lpc
->apm
, ich9_apm_ctrl_changed
, lpc
);
502 lpc
->machine_ready
.notify
= ich9_lpc_machine_ready
;
503 qemu_add_machine_init_done_notifier(&lpc
->machine_ready
);
508 static const VMStateDescription vmstate_ich9_lpc
= {
511 .minimum_version_id
= 1,
512 .minimum_version_id_old
= 1,
513 .post_load
= ich9_lpc_post_load
,
514 .fields
= (VMStateField
[]) {
515 VMSTATE_PCI_DEVICE(d
, ICH9LPCState
),
516 VMSTATE_STRUCT(apm
, ICH9LPCState
, 0, vmstate_apm
, APMState
),
517 VMSTATE_STRUCT(pm
, ICH9LPCState
, 0, vmstate_ich9_pm
, ICH9LPCPMRegs
),
518 VMSTATE_UINT8_ARRAY(chip_config
, ICH9LPCState
, ICH9_CC_SIZE
),
519 VMSTATE_UINT32(sci_level
, ICH9LPCState
),
520 VMSTATE_END_OF_LIST()
524 static void ich9_lpc_class_init(ObjectClass
*klass
, void *data
)
526 DeviceClass
*dc
= DEVICE_CLASS(klass
);
527 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
529 dc
->reset
= ich9_lpc_reset
;
530 k
->init
= ich9_lpc_initfn
;
531 dc
->vmsd
= &vmstate_ich9_lpc
;
533 k
->config_write
= ich9_lpc_config_write
;
534 dc
->desc
= "ICH9 LPC bridge";
535 k
->vendor_id
= PCI_VENDOR_ID_INTEL
;
536 k
->device_id
= PCI_DEVICE_ID_INTEL_ICH9_8
;
537 k
->revision
= ICH9_A2_LPC_REVISION
;
538 k
->class_id
= PCI_CLASS_BRIDGE_ISA
;
542 static const TypeInfo ich9_lpc_info
= {
543 .name
= TYPE_ICH9_LPC_DEVICE
,
544 .parent
= TYPE_PCI_DEVICE
,
545 .instance_size
= sizeof(struct ICH9LPCState
),
546 .class_init
= ich9_lpc_class_init
,
549 static void ich9_lpc_register(void)
551 type_register_static(&ich9_lpc_info
);
554 type_init(ich9_lpc_register
);