xref: /qemu/hw/ide/ich.c (revision 465f1ab16169f5d71e52f11d4c650a50fd681030)
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
107fb6577bSAlexander Graf  * version 2 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 
6303c7a6a8SSebastian Herbszt #include <hw/hw.h>
6403c7a6a8SSebastian Herbszt #include <hw/msi.h>
6503c7a6a8SSebastian Herbszt #include <hw/pc.h>
6603c7a6a8SSebastian Herbszt #include <hw/pci.h>
6703c7a6a8SSebastian Herbszt #include <hw/isa.h>
6803c7a6a8SSebastian Herbszt #include "block.h"
6903c7a6a8SSebastian Herbszt #include "dma.h"
7003c7a6a8SSebastian Herbszt 
7103c7a6a8SSebastian Herbszt #include <hw/ide/pci.h>
7203c7a6a8SSebastian Herbszt #include <hw/ide/ahci.h>
7303c7a6a8SSebastian Herbszt 
74*465f1ab1SDaniel Verkamp #define ICH9_SATA_CAP_OFFSET    0xA8
75*465f1ab1SDaniel Verkamp 
76*465f1ab1SDaniel Verkamp #define ICH9_IDP_BAR            4
77*465f1ab1SDaniel Verkamp #define ICH9_MEM_BAR            5
78*465f1ab1SDaniel Verkamp 
79*465f1ab1SDaniel Verkamp #define ICH9_IDP_INDEX          0x10
80*465f1ab1SDaniel Verkamp #define ICH9_IDP_INDEX_LOG2     0x04
81*465f1ab1SDaniel Verkamp 
82b7ce1b27SGerd Hoffmann static const VMStateDescription vmstate_ahci = {
83b7ce1b27SGerd Hoffmann     .name = "ahci",
84b7ce1b27SGerd Hoffmann     .unmigratable = 1,
85b7ce1b27SGerd Hoffmann };
86b7ce1b27SGerd Hoffmann 
877fb6577bSAlexander Graf static int pci_ich9_ahci_init(PCIDevice *dev)
8803c7a6a8SSebastian Herbszt {
8903c7a6a8SSebastian Herbszt     struct AHCIPCIState *d;
90*465f1ab1SDaniel Verkamp     int sata_cap_offset;
91*465f1ab1SDaniel Verkamp     uint8_t *sata_cap;
9203c7a6a8SSebastian Herbszt     d = DO_UPCAST(struct AHCIPCIState, card, dev);
9303c7a6a8SSebastian Herbszt 
9469c8944fSMichael S. Tsirkin     ahci_init(&d->ahci, &dev->qdev, 6);
9569c8944fSMichael S. Tsirkin 
9603c7a6a8SSebastian Herbszt     pci_config_set_prog_interface(d->card.config, AHCI_PROGMODE_MAJOR_REV_1);
9703c7a6a8SSebastian Herbszt 
9803c7a6a8SSebastian Herbszt     d->card.config[PCI_CACHE_LINE_SIZE] = 0x08;  /* Cache line size */
9903c7a6a8SSebastian Herbszt     d->card.config[PCI_LATENCY_TIMER]   = 0x00;  /* Latency timer */
10003c7a6a8SSebastian Herbszt     pci_config_set_interrupt_pin(d->card.config, 1);
10103c7a6a8SSebastian Herbszt 
10203c7a6a8SSebastian Herbszt     /* XXX Software should program this register */
10303c7a6a8SSebastian Herbszt     d->card.config[0x90]   = 1 << 6; /* Address Map Register - AHCI mode */
10403c7a6a8SSebastian Herbszt 
10503c7a6a8SSebastian Herbszt     qemu_register_reset(ahci_reset, d);
10603c7a6a8SSebastian Herbszt 
10703c7a6a8SSebastian Herbszt     msi_init(dev, 0x50, 1, true, false);
10803c7a6a8SSebastian Herbszt     d->ahci.irq = d->card.irq[0];
10903c7a6a8SSebastian Herbszt 
110*465f1ab1SDaniel Verkamp     pci_register_bar(&d->card, ICH9_IDP_BAR, PCI_BASE_ADDRESS_SPACE_IO,
111*465f1ab1SDaniel Verkamp                      &d->ahci.idp);
112*465f1ab1SDaniel Verkamp     pci_register_bar(&d->card, ICH9_MEM_BAR, PCI_BASE_ADDRESS_SPACE_MEMORY,
113*465f1ab1SDaniel Verkamp                      &d->ahci.mem);
114*465f1ab1SDaniel Verkamp 
115*465f1ab1SDaniel Verkamp     sata_cap_offset = pci_add_capability(&d->card, PCI_CAP_ID_SATA,
116*465f1ab1SDaniel Verkamp                                          ICH9_SATA_CAP_OFFSET, SATA_CAP_SIZE);
117*465f1ab1SDaniel Verkamp     if (sata_cap_offset < 0) {
118*465f1ab1SDaniel Verkamp         return sata_cap_offset;
119*465f1ab1SDaniel Verkamp     }
120*465f1ab1SDaniel Verkamp 
121*465f1ab1SDaniel Verkamp     sata_cap = d->card.config + sata_cap_offset;
122*465f1ab1SDaniel Verkamp     pci_set_word(sata_cap + SATA_CAP_REV, 0x10);
123*465f1ab1SDaniel Verkamp     pci_set_long(sata_cap + SATA_CAP_BAR,
124*465f1ab1SDaniel Verkamp                  (ICH9_IDP_BAR + 0x4) | (ICH9_IDP_INDEX_LOG2 << 4));
125*465f1ab1SDaniel Verkamp     d->ahci.idp_offset = ICH9_IDP_INDEX;
12696d19bcbSJan Kiszka 
12703c7a6a8SSebastian Herbszt     return 0;
12803c7a6a8SSebastian Herbszt }
12903c7a6a8SSebastian Herbszt 
1307fb6577bSAlexander Graf static int pci_ich9_uninit(PCIDevice *dev)
1317fb6577bSAlexander Graf {
1327fb6577bSAlexander Graf     struct AHCIPCIState *d;
1337fb6577bSAlexander Graf     d = DO_UPCAST(struct AHCIPCIState, card, dev);
1347fb6577bSAlexander Graf 
1357fb6577bSAlexander Graf     msi_uninit(dev);
1367fb6577bSAlexander Graf     qemu_unregister_reset(ahci_reset, d);
1372c4b9d0eSAlexander Graf     ahci_uninit(&d->ahci);
1387fb6577bSAlexander Graf 
1397fb6577bSAlexander Graf     return 0;
1407fb6577bSAlexander Graf }
1417fb6577bSAlexander Graf 
1427fb6577bSAlexander Graf static void pci_ich9_write_config(PCIDevice *pci, uint32_t addr,
1437fb6577bSAlexander Graf                                   uint32_t val, int len)
1447fb6577bSAlexander Graf {
1457fb6577bSAlexander Graf     pci_default_write_config(pci, addr, val, len);
1467fb6577bSAlexander Graf     msi_write_config(pci, addr, val, len);
1477fb6577bSAlexander Graf }
1487fb6577bSAlexander Graf 
14903c7a6a8SSebastian Herbszt static PCIDeviceInfo ich_ahci_info[] = {
15003c7a6a8SSebastian Herbszt     {
15103c7a6a8SSebastian Herbszt         .qdev.name    = "ich9-ahci",
1527fb6577bSAlexander Graf         .qdev.alias   = "ahci",
15303c7a6a8SSebastian Herbszt         .qdev.size    = sizeof(AHCIPCIState),
154b7ce1b27SGerd Hoffmann         .qdev.vmsd    = &vmstate_ahci,
1557fb6577bSAlexander Graf         .init         = pci_ich9_ahci_init,
1567fb6577bSAlexander Graf         .exit         = pci_ich9_uninit,
1577fb6577bSAlexander Graf         .config_write = pci_ich9_write_config,
15803f1c143SIsaku Yamahata         .vendor_id    = PCI_VENDOR_ID_INTEL,
15903f1c143SIsaku Yamahata         .device_id    = PCI_DEVICE_ID_INTEL_82801IR,
16003f1c143SIsaku Yamahata         .revision     = 0x02,
16103f1c143SIsaku Yamahata         .class_id     = PCI_CLASS_STORAGE_SATA,
16203c7a6a8SSebastian Herbszt     },{
16303c7a6a8SSebastian Herbszt         /* end of list */
16403c7a6a8SSebastian Herbszt     }
16503c7a6a8SSebastian Herbszt };
16603c7a6a8SSebastian Herbszt 
16703c7a6a8SSebastian Herbszt static void ich_ahci_register(void)
16803c7a6a8SSebastian Herbszt {
16903c7a6a8SSebastian Herbszt     pci_qdev_register_many(ich_ahci_info);
17003c7a6a8SSebastian Herbszt }
17103c7a6a8SSebastian Herbszt device_init(ich_ahci_register);
172