17fb6577bSAlexander Graf /*
27fb6577bSAlexander Graf * QEMU ICH Emulation
37fb6577bSAlexander Graf *
47fb6577bSAlexander Graf * Copyright (c) 2010 Sebastian Herbszt <herbszt@gmx.de>
57fb6577bSAlexander Graf * Copyright (c) 2010 Alexander Graf <agraf@suse.de>
67fb6577bSAlexander Graf *
77fb6577bSAlexander Graf * This library is free software; you can redistribute it and/or
87fb6577bSAlexander Graf * modify it under the terms of the GNU Lesser General Public
97fb6577bSAlexander Graf * License as published by the Free Software Foundation; either
1061f3c91aSChetan Pant * version 2.1 of the License, or (at your option) any later version.
117fb6577bSAlexander Graf *
127fb6577bSAlexander Graf * This library is distributed in the hope that it will be useful,
137fb6577bSAlexander Graf * but WITHOUT ANY WARRANTY; without even the implied warranty of
147fb6577bSAlexander Graf * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
157fb6577bSAlexander Graf * Lesser General Public License for more details.
167fb6577bSAlexander Graf *
177fb6577bSAlexander Graf * You should have received a copy of the GNU Lesser General Public
187fb6577bSAlexander Graf * License along with this library; if not, see <http://www.gnu.org/licenses/>.
197fb6577bSAlexander Graf *
207fb6577bSAlexander Graf *
217fb6577bSAlexander Graf * lspci dump of a ICH-9 real device
227fb6577bSAlexander Graf *
237fb6577bSAlexander Graf * 00:1f.2 SATA controller [0106]: Intel Corporation 82801IR/IO/IH (ICH9R/DO/DH) 6 port SATA AHCI Controller [8086:2922] (rev 02) (prog-if 01 [AHCI 1.0])
247fb6577bSAlexander Graf * Subsystem: Intel Corporation 82801IR/IO/IH (ICH9R/DO/DH) 6 port SATA AHCI Controller [8086:2922]
257fb6577bSAlexander Graf * Control: I/O+ Mem+ BusMaster+ SpecCycle- MemWINV- VGASnoop- ParErr- Stepping- SERR- FastB2B- DisINTx+
267fb6577bSAlexander Graf * Status: Cap+ 66MHz+ UDF- FastB2B+ ParErr- DEVSEL=medium >TAbort- <TAbort- <MAbort- >SERR- <PERR- INTx-
277fb6577bSAlexander Graf * Latency: 0
287fb6577bSAlexander Graf * Interrupt: pin B routed to IRQ 222
297fb6577bSAlexander Graf * Region 0: I/O ports at d000 [size=8]
307fb6577bSAlexander Graf * Region 1: I/O ports at cc00 [size=4]
317fb6577bSAlexander Graf * Region 2: I/O ports at c880 [size=8]
327fb6577bSAlexander Graf * Region 3: I/O ports at c800 [size=4]
337fb6577bSAlexander Graf * Region 4: I/O ports at c480 [size=32]
347fb6577bSAlexander Graf * Region 5: Memory at febf9000 (32-bit, non-prefetchable) [size=2K]
357fb6577bSAlexander Graf * Capabilities: [80] Message Signalled Interrupts: Mask- 64bit- Count=1/16 Enable+
367fb6577bSAlexander Graf * Address: fee0f00c Data: 41d9
377fb6577bSAlexander Graf * Capabilities: [70] Power Management version 3
387fb6577bSAlexander Graf * Flags: PMEClk- DSI- D1- D2- AuxCurrent=0mA PME(D0-,D1-,D2-,D3hot+,D3cold-)
397fb6577bSAlexander Graf * Status: D0 PME-Enable- DSel=0 DScale=0 PME-
407fb6577bSAlexander Graf * Capabilities: [a8] SATA HBA <?>
417fb6577bSAlexander Graf * Capabilities: [b0] Vendor Specific Information <?>
427fb6577bSAlexander Graf * Kernel driver in use: ahci
437fb6577bSAlexander Graf * Kernel modules: ahci
447fb6577bSAlexander Graf * 00: 86 80 22 29 07 04 b0 02 02 01 06 01 00 00 00 00
457fb6577bSAlexander Graf * 10: 01 d0 00 00 01 cc 00 00 81 c8 00 00 01 c8 00 00
467fb6577bSAlexander Graf * 20: 81 c4 00 00 00 90 bf fe 00 00 00 00 86 80 22 29
477fb6577bSAlexander Graf * 30: 00 00 00 00 80 00 00 00 00 00 00 00 0f 02 00 00
487fb6577bSAlexander Graf * 40: 00 80 00 80 00 00 00 00 00 00 00 00 00 00 00 00
497fb6577bSAlexander Graf * 50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
507fb6577bSAlexander Graf * 60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
517fb6577bSAlexander Graf * 70: 01 a8 03 40 08 00 00 00 00 00 00 00 00 00 00 00
527fb6577bSAlexander Graf * 80: 05 70 09 00 0c f0 e0 fe d9 41 00 00 00 00 00 00
537fb6577bSAlexander Graf * 90: 40 00 0f 82 93 01 00 00 00 00 00 00 00 00 00 00
547fb6577bSAlexander Graf * a0: ac 00 00 00 0a 00 12 00 12 b0 10 00 48 00 00 00
557fb6577bSAlexander Graf * b0: 09 00 06 20 00 00 00 00 00 00 00 00 00 00 00 00
567fb6577bSAlexander Graf * c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
577fb6577bSAlexander Graf * d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
587fb6577bSAlexander Graf * e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
597fb6577bSAlexander Graf * f0: 00 00 00 00 00 00 00 00 86 0f 02 00 00 00 00 00
607fb6577bSAlexander Graf *
617fb6577bSAlexander Graf */
627fb6577bSAlexander Graf
6353239262SPeter Maydell #include "qemu/osdep.h"
64a9c94277SMarkus Armbruster #include "hw/pci/msi.h"
65a9c94277SMarkus Armbruster #include "hw/pci/pci.h"
66d6454270SMarkus Armbruster #include "migration/vmstate.h"
670b8fa32fSMarkus Armbruster #include "qemu/module.h"
68a9c94277SMarkus Armbruster #include "hw/isa/isa.h"
6932cad1ffSPhilippe Mathieu-Daudé #include "system/dma.h"
70a9c94277SMarkus Armbruster #include "hw/ide/pci.h"
71d407be08SPhilippe Mathieu-Daudé #include "hw/ide/ahci-pci.h"
722f73edacSBALATON Zoltan #include "ahci-internal.h"
7303c7a6a8SSebastian Herbszt
74c8b5b20fSJohn Snow #define ICH9_MSI_CAP_OFFSET 0x80
75465f1ab1SDaniel Verkamp #define ICH9_SATA_CAP_OFFSET 0xA8
76465f1ab1SDaniel Verkamp
77465f1ab1SDaniel Verkamp #define ICH9_IDP_BAR 4
78465f1ab1SDaniel Verkamp #define ICH9_MEM_BAR 5
79465f1ab1SDaniel Verkamp
80465f1ab1SDaniel Verkamp #define ICH9_IDP_INDEX 0x10
81465f1ab1SDaniel Verkamp #define ICH9_IDP_INDEX_LOG2 0x04
82465f1ab1SDaniel Verkamp
83a2623021SJason Baron static const VMStateDescription vmstate_ich9_ahci = {
84a2623021SJason Baron .name = "ich9_ahci",
85a2623021SJason Baron .version_id = 1,
868595c054SRichard Henderson .fields = (const VMStateField[]) {
870d3aea56SAndreas Färber VMSTATE_PCI_DEVICE(parent_obj, AHCIPCIState),
88a2623021SJason Baron VMSTATE_AHCI(ahci, AHCIPCIState),
89a2623021SJason Baron VMSTATE_END_OF_LIST()
90a2623021SJason Baron },
91b7ce1b27SGerd Hoffmann };
92b7ce1b27SGerd Hoffmann
pci_ich9_ahci_update_irq(void * opaque,int irq_num,int level)938a4989f5SBernhard Beschow static void pci_ich9_ahci_update_irq(void *opaque, int irq_num, int level)
948a4989f5SBernhard Beschow {
958a4989f5SBernhard Beschow PCIDevice *pci_dev = opaque;
968a4989f5SBernhard Beschow
978a4989f5SBernhard Beschow if (msi_enabled(pci_dev)) {
988a4989f5SBernhard Beschow if (level) {
998a4989f5SBernhard Beschow msi_notify(pci_dev, 0);
1008a4989f5SBernhard Beschow }
1018a4989f5SBernhard Beschow } else {
1028a4989f5SBernhard Beschow pci_set_irq(pci_dev, level);
1038a4989f5SBernhard Beschow }
1048a4989f5SBernhard Beschow }
1058a4989f5SBernhard Beschow
pci_ich9_reset(DeviceState * dev)1068ab60a07SJan Kiszka static void pci_ich9_reset(DeviceState *dev)
107868a1a52SJan Kiszka {
108aa3c41fbSEduardo Habkost AHCIPCIState *d = ICH9_AHCI(dev);
109868a1a52SJan Kiszka
1108ab60a07SJan Kiszka ahci_reset(&d->ahci);
111868a1a52SJan Kiszka }
112868a1a52SJan Kiszka
pci_ich9_ahci_init(Object * obj)1130487eea4SPeter Crosthwaite static void pci_ich9_ahci_init(Object *obj)
1140487eea4SPeter Crosthwaite {
1159a4b35f5SPhilippe Mathieu-Daudé AHCIPCIState *d = ICH9_AHCI(obj);
1160487eea4SPeter Crosthwaite
1178a4989f5SBernhard Beschow qemu_init_irq(&d->irq, pci_ich9_ahci_update_irq, d, 0);
1180487eea4SPeter Crosthwaite ahci_init(&d->ahci, DEVICE(obj));
1198a4989f5SBernhard Beschow d->ahci.irq = &d->irq;
1200487eea4SPeter Crosthwaite }
1210487eea4SPeter Crosthwaite
pci_ich9_ahci_realize(PCIDevice * dev,Error ** errp)122b8a2dac0SMarkus Armbruster static void pci_ich9_ahci_realize(PCIDevice *dev, Error **errp)
12303c7a6a8SSebastian Herbszt {
1249a4b35f5SPhilippe Mathieu-Daudé AHCIPCIState *d;
125465f1ab1SDaniel Verkamp int sata_cap_offset;
126465f1ab1SDaniel Verkamp uint8_t *sata_cap;
127aa3c41fbSEduardo Habkost d = ICH9_AHCI(dev);
1281108b2f8SCao jin int ret;
12903c7a6a8SSebastian Herbszt
130be021501SPhilippe Mathieu-Daudé d->ahci.ports = 6;
131be021501SPhilippe Mathieu-Daudé ahci_realize(&d->ahci, DEVICE(dev), pci_get_address_space(dev));
13269c8944fSMichael S. Tsirkin
1330d3aea56SAndreas Färber pci_config_set_prog_interface(dev->config, AHCI_PROGMODE_MAJOR_REV_1);
13403c7a6a8SSebastian Herbszt
1350d3aea56SAndreas Färber dev->config[PCI_CACHE_LINE_SIZE] = 0x08; /* Cache line size */
1360d3aea56SAndreas Färber dev->config[PCI_LATENCY_TIMER] = 0x00; /* Latency timer */
1370d3aea56SAndreas Färber pci_config_set_interrupt_pin(dev->config, 1);
13803c7a6a8SSebastian Herbszt
13903c7a6a8SSebastian Herbszt /* XXX Software should program this register */
1400d3aea56SAndreas Färber dev->config[0x90] = 1 << 6; /* Address Map Register - AHCI mode */
14103c7a6a8SSebastian Herbszt
1420d3aea56SAndreas Färber pci_register_bar(dev, ICH9_IDP_BAR, PCI_BASE_ADDRESS_SPACE_IO,
143465f1ab1SDaniel Verkamp &d->ahci.idp);
1440d3aea56SAndreas Färber pci_register_bar(dev, ICH9_MEM_BAR, PCI_BASE_ADDRESS_SPACE_MEMORY,
145465f1ab1SDaniel Verkamp &d->ahci.mem);
146465f1ab1SDaniel Verkamp
14727841278SMao Zhongyi sata_cap_offset = pci_add_capability(dev, PCI_CAP_ID_SATA,
148b8a2dac0SMarkus Armbruster ICH9_SATA_CAP_OFFSET, SATA_CAP_SIZE,
149b8a2dac0SMarkus Armbruster errp);
150465f1ab1SDaniel Verkamp if (sata_cap_offset < 0) {
151b8a2dac0SMarkus Armbruster return;
152465f1ab1SDaniel Verkamp }
153465f1ab1SDaniel Verkamp
1540d3aea56SAndreas Färber sata_cap = dev->config + sata_cap_offset;
155465f1ab1SDaniel Verkamp pci_set_word(sata_cap + SATA_CAP_REV, 0x10);
156465f1ab1SDaniel Verkamp pci_set_long(sata_cap + SATA_CAP_BAR,
157465f1ab1SDaniel Verkamp (ICH9_IDP_BAR + 0x4) | (ICH9_IDP_INDEX_LOG2 << 4));
158465f1ab1SDaniel Verkamp d->ahci.idp_offset = ICH9_IDP_INDEX;
15996d19bcbSJan Kiszka
160c8b5b20fSJohn Snow /* Although the AHCI 1.3 specification states that the first capability
161c8b5b20fSJohn Snow * should be PMCAP, the Intel ICH9 data sheet specifies that the ICH9
162c8b5b20fSJohn Snow * AHCI device puts the MSI capability first, pointing to 0x80. */
1631108b2f8SCao jin ret = msi_init(dev, ICH9_MSI_CAP_OFFSET, 1, true, false, NULL);
1641108b2f8SCao jin /* Any error other than -ENOTSUP(board's MSI support is broken)
1651108b2f8SCao jin * is a programming error. Fall back to INTx silently on -ENOTSUP */
1661108b2f8SCao jin assert(!ret || ret == -ENOTSUP);
16703c7a6a8SSebastian Herbszt }
16803c7a6a8SSebastian Herbszt
pci_ich9_uninit(PCIDevice * dev)169f90c2bcdSAlex Williamson static void pci_ich9_uninit(PCIDevice *dev)
1707fb6577bSAlexander Graf {
1719a4b35f5SPhilippe Mathieu-Daudé AHCIPCIState *d;
172aa3c41fbSEduardo Habkost d = ICH9_AHCI(dev);
1737fb6577bSAlexander Graf
1747fb6577bSAlexander Graf msi_uninit(dev);
1752c4b9d0eSAlexander Graf ahci_uninit(&d->ahci);
1767fb6577bSAlexander Graf }
1777fb6577bSAlexander Graf
ich_ahci_class_init(ObjectClass * klass,const void * data)17812d1a768SPhilippe Mathieu-Daudé static void ich_ahci_class_init(ObjectClass *klass, const void *data)
17940021f08SAnthony Liguori {
18039bffca2SAnthony Liguori DeviceClass *dc = DEVICE_CLASS(klass);
18140021f08SAnthony Liguori PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
18240021f08SAnthony Liguori
183b8a2dac0SMarkus Armbruster k->realize = pci_ich9_ahci_realize;
18440021f08SAnthony Liguori k->exit = pci_ich9_uninit;
18540021f08SAnthony Liguori k->vendor_id = PCI_VENDOR_ID_INTEL;
18640021f08SAnthony Liguori k->device_id = PCI_DEVICE_ID_INTEL_82801IR;
18740021f08SAnthony Liguori k->revision = 0x02;
18840021f08SAnthony Liguori k->class_id = PCI_CLASS_STORAGE_SATA;
189a2623021SJason Baron dc->vmsd = &vmstate_ich9_ahci;
190e3d08143SPeter Maydell device_class_set_legacy_reset(dc, pci_ich9_reset);
191125ee0edSMarcel Apfelbaum set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
19240021f08SAnthony Liguori }
19340021f08SAnthony Liguori
1948c43a6f0SAndreas Färber static const TypeInfo ich_ahci_info = {
195fd58922cSPeter Crosthwaite .name = TYPE_ICH9_AHCI,
19639bffca2SAnthony Liguori .parent = TYPE_PCI_DEVICE,
19739bffca2SAnthony Liguori .instance_size = sizeof(AHCIPCIState),
1980487eea4SPeter Crosthwaite .instance_init = pci_ich9_ahci_init,
19940021f08SAnthony Liguori .class_init = ich_ahci_class_init,
200*2cd09e47SPhilippe Mathieu-Daudé .interfaces = (const InterfaceInfo[]) {
201fd3b02c8SEduardo Habkost { INTERFACE_CONVENTIONAL_PCI_DEVICE },
202fd3b02c8SEduardo Habkost { },
203fd3b02c8SEduardo Habkost },
20403c7a6a8SSebastian Herbszt };
20503c7a6a8SSebastian Herbszt
ich_ahci_register_types(void)20683f7d43aSAndreas Färber static void ich_ahci_register_types(void)
20703c7a6a8SSebastian Herbszt {
20839bffca2SAnthony Liguori type_register_static(&ich_ahci_info);
20903c7a6a8SSebastian Herbszt }
21083f7d43aSAndreas Färber
21183f7d43aSAndreas Färber type_init(ich_ahci_register_types)
212