Statistics
| Branch: | Revision:

root / hw / pcie.c @ 5afb9869

History | View | Annotate | Download (18.8 kB)

1 0428527c Isaku Yamahata
/*
2 0428527c Isaku Yamahata
 * pcie.c
3 0428527c Isaku Yamahata
 *
4 0428527c Isaku Yamahata
 * Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp>
5 0428527c Isaku Yamahata
 *                    VA Linux Systems Japan K.K.
6 0428527c Isaku Yamahata
 *
7 0428527c Isaku Yamahata
 * This program is free software; you can redistribute it and/or modify
8 0428527c Isaku Yamahata
 * it under the terms of the GNU General Public License as published by
9 0428527c Isaku Yamahata
 * the Free Software Foundation; either version 2 of the License, or
10 0428527c Isaku Yamahata
 * (at your option) any later version.
11 0428527c Isaku Yamahata
 *
12 0428527c Isaku Yamahata
 * This program is distributed in the hope that it will be useful,
13 0428527c Isaku Yamahata
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 0428527c Isaku Yamahata
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 0428527c Isaku Yamahata
 * GNU General Public License for more details.
16 0428527c Isaku Yamahata
 *
17 0428527c Isaku Yamahata
 * You should have received a copy of the GNU General Public License along
18 0428527c Isaku Yamahata
 * with this program; if not, see <http://www.gnu.org/licenses/>.
19 0428527c Isaku Yamahata
 */
20 0428527c Isaku Yamahata
21 0428527c Isaku Yamahata
#include "sysemu.h"
22 0428527c Isaku Yamahata
#include "pci_bridge.h"
23 0428527c Isaku Yamahata
#include "pcie.h"
24 0428527c Isaku Yamahata
#include "msix.h"
25 0428527c Isaku Yamahata
#include "msi.h"
26 0428527c Isaku Yamahata
#include "pci_internals.h"
27 0428527c Isaku Yamahata
#include "pcie_regs.h"
28 5afb9869 Blue Swirl
#include "range.h"
29 0428527c Isaku Yamahata
30 0428527c Isaku Yamahata
//#define DEBUG_PCIE
31 0428527c Isaku Yamahata
#ifdef DEBUG_PCIE
32 0428527c Isaku Yamahata
# define PCIE_DPRINTF(fmt, ...)                                         \
33 0428527c Isaku Yamahata
    fprintf(stderr, "%s:%d " fmt, __func__, __LINE__, ## __VA_ARGS__)
34 0428527c Isaku Yamahata
#else
35 0428527c Isaku Yamahata
# define PCIE_DPRINTF(fmt, ...) do {} while (0)
36 0428527c Isaku Yamahata
#endif
37 0428527c Isaku Yamahata
#define PCIE_DEV_PRINTF(dev, fmt, ...)                                  \
38 0428527c Isaku Yamahata
    PCIE_DPRINTF("%s:%x "fmt, (dev)->name, (dev)->devfn, ## __VA_ARGS__)
39 0428527c Isaku Yamahata
40 0428527c Isaku Yamahata
41 0428527c Isaku Yamahata
/***************************************************************************
42 0428527c Isaku Yamahata
 * pci express capability helper functions
43 0428527c Isaku Yamahata
 */
44 0428527c Isaku Yamahata
int pcie_cap_init(PCIDevice *dev, uint8_t offset, uint8_t type, uint8_t port)
45 0428527c Isaku Yamahata
{
46 0428527c Isaku Yamahata
    int pos;
47 0428527c Isaku Yamahata
    uint8_t *exp_cap;
48 0428527c Isaku Yamahata
49 0428527c Isaku Yamahata
    assert(pci_is_express(dev));
50 0428527c Isaku Yamahata
51 0428527c Isaku Yamahata
    pos = pci_add_capability(dev, PCI_CAP_ID_EXP, offset,
52 0428527c Isaku Yamahata
                                 PCI_EXP_VER2_SIZEOF);
53 0428527c Isaku Yamahata
    if (pos < 0) {
54 0428527c Isaku Yamahata
        return pos;
55 0428527c Isaku Yamahata
    }
56 0428527c Isaku Yamahata
    dev->exp.exp_cap = pos;
57 0428527c Isaku Yamahata
    exp_cap = dev->config + pos;
58 0428527c Isaku Yamahata
59 0428527c Isaku Yamahata
    /* capability register
60 0428527c Isaku Yamahata
       interrupt message number defaults to 0 */
61 0428527c Isaku Yamahata
    pci_set_word(exp_cap + PCI_EXP_FLAGS,
62 0428527c Isaku Yamahata
                 ((type << PCI_EXP_FLAGS_TYPE_SHIFT) & PCI_EXP_FLAGS_TYPE) |
63 0428527c Isaku Yamahata
                 PCI_EXP_FLAGS_VER2);
64 0428527c Isaku Yamahata
65 0428527c Isaku Yamahata
    /* device capability register
66 0428527c Isaku Yamahata
     * table 7-12:
67 0428527c Isaku Yamahata
     * roll based error reporting bit must be set by all
68 0428527c Isaku Yamahata
     * Functions conforming to the ECN, PCI Express Base
69 0428527c Isaku Yamahata
     * Specification, Revision 1.1., or subsequent PCI Express Base
70 0428527c Isaku Yamahata
     * Specification revisions.
71 0428527c Isaku Yamahata
     */
72 0428527c Isaku Yamahata
    pci_set_long(exp_cap + PCI_EXP_DEVCAP, PCI_EXP_DEVCAP_RBER);
73 0428527c Isaku Yamahata
74 0428527c Isaku Yamahata
    pci_set_long(exp_cap + PCI_EXP_LNKCAP,
75 0428527c Isaku Yamahata
                 (port << PCI_EXP_LNKCAP_PN_SHIFT) |
76 0428527c Isaku Yamahata
                 PCI_EXP_LNKCAP_ASPMS_0S |
77 0428527c Isaku Yamahata
                 PCI_EXP_LNK_MLW_1 |
78 0428527c Isaku Yamahata
                 PCI_EXP_LNK_LS_25);
79 0428527c Isaku Yamahata
80 0428527c Isaku Yamahata
    pci_set_word(exp_cap + PCI_EXP_LNKSTA,
81 0428527c Isaku Yamahata
                 PCI_EXP_LNK_MLW_1 | PCI_EXP_LNK_LS_25);
82 0428527c Isaku Yamahata
83 0428527c Isaku Yamahata
    pci_set_long(exp_cap + PCI_EXP_DEVCAP2,
84 0428527c Isaku Yamahata
                 PCI_EXP_DEVCAP2_EFF | PCI_EXP_DEVCAP2_EETLPP);
85 0428527c Isaku Yamahata
86 0428527c Isaku Yamahata
    pci_set_word(dev->wmask + pos, PCI_EXP_DEVCTL2_EETLPPB);
87 0428527c Isaku Yamahata
    return pos;
88 0428527c Isaku Yamahata
}
89 0428527c Isaku Yamahata
90 0428527c Isaku Yamahata
void pcie_cap_exit(PCIDevice *dev)
91 0428527c Isaku Yamahata
{
92 0428527c Isaku Yamahata
    pci_del_capability(dev, PCI_CAP_ID_EXP, PCI_EXP_VER2_SIZEOF);
93 0428527c Isaku Yamahata
}
94 0428527c Isaku Yamahata
95 0428527c Isaku Yamahata
uint8_t pcie_cap_get_type(const PCIDevice *dev)
96 0428527c Isaku Yamahata
{
97 0428527c Isaku Yamahata
    uint32_t pos = dev->exp.exp_cap;
98 0428527c Isaku Yamahata
    assert(pos > 0);
99 0428527c Isaku Yamahata
    return (pci_get_word(dev->config + pos + PCI_EXP_FLAGS) &
100 0428527c Isaku Yamahata
            PCI_EXP_FLAGS_TYPE) >> PCI_EXP_FLAGS_TYPE_SHIFT;
101 0428527c Isaku Yamahata
}
102 0428527c Isaku Yamahata
103 0428527c Isaku Yamahata
/* MSI/MSI-X */
104 0428527c Isaku Yamahata
/* pci express interrupt message number */
105 0428527c Isaku Yamahata
/* 7.8.2 PCI Express Capabilities Register: Interrupt Message Number */
106 0428527c Isaku Yamahata
void pcie_cap_flags_set_vector(PCIDevice *dev, uint8_t vector)
107 0428527c Isaku Yamahata
{
108 0428527c Isaku Yamahata
    uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
109 0428527c Isaku Yamahata
    assert(vector < 32);
110 0428527c Isaku Yamahata
    pci_word_test_and_clear_mask(exp_cap + PCI_EXP_FLAGS, PCI_EXP_FLAGS_IRQ);
111 0428527c Isaku Yamahata
    pci_word_test_and_set_mask(exp_cap + PCI_EXP_FLAGS,
112 0428527c Isaku Yamahata
                               vector << PCI_EXP_FLAGS_IRQ_SHIFT);
113 0428527c Isaku Yamahata
}
114 0428527c Isaku Yamahata
115 0428527c Isaku Yamahata
uint8_t pcie_cap_flags_get_vector(PCIDevice *dev)
116 0428527c Isaku Yamahata
{
117 0428527c Isaku Yamahata
    return (pci_get_word(dev->config + dev->exp.exp_cap + PCI_EXP_FLAGS) &
118 0428527c Isaku Yamahata
            PCI_EXP_FLAGS_IRQ) >> PCI_EXP_FLAGS_IRQ_SHIFT;
119 0428527c Isaku Yamahata
}
120 0428527c Isaku Yamahata
121 0428527c Isaku Yamahata
void pcie_cap_deverr_init(PCIDevice *dev)
122 0428527c Isaku Yamahata
{
123 0428527c Isaku Yamahata
    uint32_t pos = dev->exp.exp_cap;
124 0428527c Isaku Yamahata
    pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_DEVCAP,
125 0428527c Isaku Yamahata
                               PCI_EXP_DEVCAP_RBER);
126 0428527c Isaku Yamahata
    pci_long_test_and_set_mask(dev->wmask + pos + PCI_EXP_DEVCTL,
127 0428527c Isaku Yamahata
                               PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE |
128 0428527c Isaku Yamahata
                               PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE);
129 0428527c Isaku Yamahata
    pci_long_test_and_set_mask(dev->w1cmask + pos + PCI_EXP_DEVSTA,
130 0428527c Isaku Yamahata
                               PCI_EXP_DEVSTA_CED | PCI_EXP_DEVSTA_NFED |
131 0428527c Isaku Yamahata
                               PCI_EXP_DEVSTA_URD | PCI_EXP_DEVSTA_URD);
132 0428527c Isaku Yamahata
}
133 0428527c Isaku Yamahata
134 0428527c Isaku Yamahata
void pcie_cap_deverr_reset(PCIDevice *dev)
135 0428527c Isaku Yamahata
{
136 0428527c Isaku Yamahata
    uint8_t *devctl = dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL;
137 0428527c Isaku Yamahata
    pci_long_test_and_clear_mask(devctl,
138 0428527c Isaku Yamahata
                                 PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE |
139 0428527c Isaku Yamahata
                                 PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE);
140 0428527c Isaku Yamahata
}
141 0428527c Isaku Yamahata
142 0428527c Isaku Yamahata
/*
143 0428527c Isaku Yamahata
 * A PCI Express Hot-Plug Event has occured, so update slot status register
144 0428527c Isaku Yamahata
 * and notify OS of the event if necessary.
145 0428527c Isaku Yamahata
 *
146 0428527c Isaku Yamahata
 * 6.7.3 PCI Express Hot-Plug Events
147 0428527c Isaku Yamahata
 * 6.7.3.4 Software Notification of Hot-Plug Events
148 0428527c Isaku Yamahata
 */
149 0428527c Isaku Yamahata
static void pcie_cap_slot_event(PCIDevice *dev, PCIExpressHotPlugEvent event)
150 0428527c Isaku Yamahata
{
151 0428527c Isaku Yamahata
    uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
152 0428527c Isaku Yamahata
    uint16_t sltctl = pci_get_word(exp_cap + PCI_EXP_SLTCTL);
153 0428527c Isaku Yamahata
    uint16_t sltsta = pci_get_word(exp_cap + PCI_EXP_SLTSTA);
154 0428527c Isaku Yamahata
155 0428527c Isaku Yamahata
    PCIE_DEV_PRINTF(dev,
156 0428527c Isaku Yamahata
                    "sltctl: 0x%02"PRIx16" sltsta: 0x%02"PRIx16" event: %x\n",
157 0428527c Isaku Yamahata
                    sltctl, sltsta, event);
158 0428527c Isaku Yamahata
159 0428527c Isaku Yamahata
    if (pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA, event)) {
160 0428527c Isaku Yamahata
        return;
161 0428527c Isaku Yamahata
    }
162 0428527c Isaku Yamahata
    sltsta = pci_get_word(exp_cap + PCI_EXP_SLTSTA);
163 0428527c Isaku Yamahata
    PCIE_DEV_PRINTF(dev, "sltsta -> %02"PRIx16"\n", sltsta);
164 0428527c Isaku Yamahata
165 0428527c Isaku Yamahata
    if ((sltctl & PCI_EXP_SLTCTL_HPIE) &&
166 0428527c Isaku Yamahata
        (sltctl & event & PCI_EXP_HP_EV_SUPPORTED)) {
167 0428527c Isaku Yamahata
        if (pci_msi_enabled(dev)) {
168 0428527c Isaku Yamahata
            pci_msi_notify(dev, pcie_cap_flags_get_vector(dev));
169 0428527c Isaku Yamahata
        } else {
170 0428527c Isaku Yamahata
            qemu_set_irq(dev->irq[dev->exp.hpev_intx], 1);
171 0428527c Isaku Yamahata
        }
172 0428527c Isaku Yamahata
    }
173 0428527c Isaku Yamahata
}
174 0428527c Isaku Yamahata
175 0428527c Isaku Yamahata
static int pcie_cap_slot_hotplug(DeviceState *qdev,
176 0428527c Isaku Yamahata
                                 PCIDevice *pci_dev, int state)
177 0428527c Isaku Yamahata
{
178 0428527c Isaku Yamahata
    PCIDevice *d = DO_UPCAST(PCIDevice, qdev, qdev);
179 0428527c Isaku Yamahata
    uint8_t *exp_cap = d->config + d->exp.exp_cap;
180 0428527c Isaku Yamahata
    uint16_t sltsta = pci_get_word(exp_cap + PCI_EXP_SLTSTA);
181 0428527c Isaku Yamahata
182 0428527c Isaku Yamahata
    if (!pci_dev->qdev.hotplugged) {
183 0428527c Isaku Yamahata
        assert(state); /* this case only happens at machine creation. */
184 0428527c Isaku Yamahata
        pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
185 0428527c Isaku Yamahata
                                   PCI_EXP_SLTSTA_PDS);
186 0428527c Isaku Yamahata
        return 0;
187 0428527c Isaku Yamahata
    }
188 0428527c Isaku Yamahata
189 0428527c Isaku Yamahata
    PCIE_DEV_PRINTF(pci_dev, "hotplug state: %d\n", state);
190 0428527c Isaku Yamahata
    if (sltsta & PCI_EXP_SLTSTA_EIS) {
191 0428527c Isaku Yamahata
        /* the slot is electromechanically locked.
192 0428527c Isaku Yamahata
         * This error is propagated up to qdev and then to HMP/QMP.
193 0428527c Isaku Yamahata
         */
194 0428527c Isaku Yamahata
        return -EBUSY;
195 0428527c Isaku Yamahata
    }
196 0428527c Isaku Yamahata
197 0428527c Isaku Yamahata
    /* TODO: multifunction hot-plug.
198 0428527c Isaku Yamahata
     * Right now, only a device of function = 0 is allowed to be
199 0428527c Isaku Yamahata
     * hot plugged/unplugged.
200 0428527c Isaku Yamahata
     */
201 0428527c Isaku Yamahata
    assert(PCI_FUNC(pci_dev->devfn) == 0);
202 0428527c Isaku Yamahata
203 0428527c Isaku Yamahata
    if (state) {
204 0428527c Isaku Yamahata
        pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
205 0428527c Isaku Yamahata
                                   PCI_EXP_SLTSTA_PDS);
206 0428527c Isaku Yamahata
        pcie_cap_slot_event(d, PCI_EXP_HP_EV_PDC);
207 0428527c Isaku Yamahata
    } else {
208 0428527c Isaku Yamahata
        qdev_free(&pci_dev->qdev);
209 0428527c Isaku Yamahata
        pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
210 0428527c Isaku Yamahata
                                     PCI_EXP_SLTSTA_PDS);
211 0428527c Isaku Yamahata
        pcie_cap_slot_event(d, PCI_EXP_HP_EV_PDC);
212 0428527c Isaku Yamahata
    }
213 0428527c Isaku Yamahata
    return 0;
214 0428527c Isaku Yamahata
}
215 0428527c Isaku Yamahata
216 0428527c Isaku Yamahata
/* pci express slot for pci express root/downstream port
217 0428527c Isaku Yamahata
   PCI express capability slot registers */
218 0428527c Isaku Yamahata
void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot)
219 0428527c Isaku Yamahata
{
220 0428527c Isaku Yamahata
    uint32_t pos = dev->exp.exp_cap;
221 0428527c Isaku Yamahata
222 0428527c Isaku Yamahata
    pci_word_test_and_set_mask(dev->config + pos + PCI_EXP_FLAGS,
223 0428527c Isaku Yamahata
                               PCI_EXP_FLAGS_SLOT);
224 0428527c Isaku Yamahata
225 0428527c Isaku Yamahata
    pci_long_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCAP,
226 0428527c Isaku Yamahata
                                 ~PCI_EXP_SLTCAP_PSN);
227 0428527c Isaku Yamahata
    pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCAP,
228 0428527c Isaku Yamahata
                               (slot << PCI_EXP_SLTCAP_PSN_SHIFT) |
229 0428527c Isaku Yamahata
                               PCI_EXP_SLTCAP_EIP |
230 0428527c Isaku Yamahata
                               PCI_EXP_SLTCAP_HPS |
231 0428527c Isaku Yamahata
                               PCI_EXP_SLTCAP_HPC |
232 0428527c Isaku Yamahata
                               PCI_EXP_SLTCAP_PIP |
233 0428527c Isaku Yamahata
                               PCI_EXP_SLTCAP_AIP |
234 0428527c Isaku Yamahata
                               PCI_EXP_SLTCAP_ABP);
235 0428527c Isaku Yamahata
236 0428527c Isaku Yamahata
    pci_word_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCTL,
237 0428527c Isaku Yamahata
                                 PCI_EXP_SLTCTL_PIC |
238 0428527c Isaku Yamahata
                                 PCI_EXP_SLTCTL_AIC);
239 0428527c Isaku Yamahata
    pci_word_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCTL,
240 0428527c Isaku Yamahata
                               PCI_EXP_SLTCTL_PIC_OFF |
241 0428527c Isaku Yamahata
                               PCI_EXP_SLTCTL_AIC_OFF);
242 0428527c Isaku Yamahata
    pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
243 0428527c Isaku Yamahata
                               PCI_EXP_SLTCTL_PIC |
244 0428527c Isaku Yamahata
                               PCI_EXP_SLTCTL_AIC |
245 0428527c Isaku Yamahata
                               PCI_EXP_SLTCTL_HPIE |
246 0428527c Isaku Yamahata
                               PCI_EXP_SLTCTL_CCIE |
247 0428527c Isaku Yamahata
                               PCI_EXP_SLTCTL_PDCE |
248 0428527c Isaku Yamahata
                               PCI_EXP_SLTCTL_ABPE);
249 0428527c Isaku Yamahata
    /* Although reading PCI_EXP_SLTCTL_EIC returns always 0,
250 0428527c Isaku Yamahata
     * make the bit writable here in order to detect 1b is written.
251 0428527c Isaku Yamahata
     * pcie_cap_slot_write_config() test-and-clear the bit, so
252 0428527c Isaku Yamahata
     * this bit always returns 0 to the guest.
253 0428527c Isaku Yamahata
     */
254 0428527c Isaku Yamahata
    pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
255 0428527c Isaku Yamahata
                               PCI_EXP_SLTCTL_EIC);
256 0428527c Isaku Yamahata
257 0428527c Isaku Yamahata
    pci_word_test_and_set_mask(dev->w1cmask + pos + PCI_EXP_SLTSTA,
258 0428527c Isaku Yamahata
                               PCI_EXP_HP_EV_SUPPORTED);
259 0428527c Isaku Yamahata
260 0428527c Isaku Yamahata
    pci_bus_hotplug(pci_bridge_get_sec_bus(DO_UPCAST(PCIBridge, dev, dev)),
261 0428527c Isaku Yamahata
                    pcie_cap_slot_hotplug, &dev->qdev);
262 0428527c Isaku Yamahata
}
263 0428527c Isaku Yamahata
264 0428527c Isaku Yamahata
void pcie_cap_slot_reset(PCIDevice *dev)
265 0428527c Isaku Yamahata
{
266 0428527c Isaku Yamahata
    uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
267 0428527c Isaku Yamahata
268 0428527c Isaku Yamahata
    PCIE_DEV_PRINTF(dev, "reset\n");
269 0428527c Isaku Yamahata
270 0428527c Isaku Yamahata
    pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
271 0428527c Isaku Yamahata
                                 PCI_EXP_SLTCTL_EIC |
272 0428527c Isaku Yamahata
                                 PCI_EXP_SLTCTL_PIC |
273 0428527c Isaku Yamahata
                                 PCI_EXP_SLTCTL_AIC |
274 0428527c Isaku Yamahata
                                 PCI_EXP_SLTCTL_HPIE |
275 0428527c Isaku Yamahata
                                 PCI_EXP_SLTCTL_CCIE |
276 0428527c Isaku Yamahata
                                 PCI_EXP_SLTCTL_PDCE |
277 0428527c Isaku Yamahata
                                 PCI_EXP_SLTCTL_ABPE);
278 0428527c Isaku Yamahata
    pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL,
279 0428527c Isaku Yamahata
                               PCI_EXP_SLTCTL_PIC_OFF |
280 0428527c Isaku Yamahata
                               PCI_EXP_SLTCTL_AIC_OFF);
281 0428527c Isaku Yamahata
282 0428527c Isaku Yamahata
    pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
283 0428527c Isaku Yamahata
                                 PCI_EXP_SLTSTA_EIS |/* on reset,
284 0428527c Isaku Yamahata
                                                        the lock is released */
285 0428527c Isaku Yamahata
                                 PCI_EXP_SLTSTA_CC |
286 0428527c Isaku Yamahata
                                 PCI_EXP_SLTSTA_PDC |
287 0428527c Isaku Yamahata
                                 PCI_EXP_SLTSTA_ABP);
288 0428527c Isaku Yamahata
}
289 0428527c Isaku Yamahata
290 0428527c Isaku Yamahata
void pcie_cap_slot_write_config(PCIDevice *dev,
291 0428527c Isaku Yamahata
                                uint32_t addr, uint32_t val, int len,
292 0428527c Isaku Yamahata
                                uint16_t sltctl_prev)
293 0428527c Isaku Yamahata
{
294 0428527c Isaku Yamahata
    uint32_t pos = dev->exp.exp_cap;
295 0428527c Isaku Yamahata
    uint8_t *exp_cap = dev->config + pos;
296 0428527c Isaku Yamahata
    uint16_t sltctl = pci_get_word(exp_cap + PCI_EXP_SLTCTL);
297 0428527c Isaku Yamahata
    uint16_t sltsta = pci_get_word(exp_cap + PCI_EXP_SLTSTA);
298 0428527c Isaku Yamahata
299 0428527c Isaku Yamahata
    PCIE_DEV_PRINTF(dev,
300 0428527c Isaku Yamahata
                    "addr: 0x%"PRIx32" val: 0x%"PRIx32" len: %d\n"
301 0428527c Isaku Yamahata
                    "\tsltctl_prev: 0x%02"PRIx16" sltctl: 0x%02"PRIx16
302 0428527c Isaku Yamahata
                    " sltsta: 0x%02"PRIx16"\n",
303 0428527c Isaku Yamahata
                    addr, val, len, sltctl_prev, sltctl, sltsta);
304 0428527c Isaku Yamahata
305 0428527c Isaku Yamahata
    /* SLTCTL */
306 0428527c Isaku Yamahata
    if (ranges_overlap(addr, len, pos + PCI_EXP_SLTCTL, 2)) {
307 0428527c Isaku Yamahata
        PCIE_DEV_PRINTF(dev, "sltctl: 0x%02"PRIx16" -> 0x%02"PRIx16"\n",
308 0428527c Isaku Yamahata
                        sltctl_prev, sltctl);
309 0428527c Isaku Yamahata
        if (pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
310 0428527c Isaku Yamahata
                                         PCI_EXP_SLTCTL_EIC)) {
311 0428527c Isaku Yamahata
            sltsta ^= PCI_EXP_SLTSTA_EIS; /* toggle PCI_EXP_SLTSTA_EIS bit */
312 0428527c Isaku Yamahata
            pci_set_word(exp_cap + PCI_EXP_SLTSTA, sltsta);
313 0428527c Isaku Yamahata
            PCIE_DEV_PRINTF(dev, "PCI_EXP_SLTCTL_EIC: "
314 0428527c Isaku Yamahata
                            "sltsta -> 0x%02"PRIx16"\n",
315 0428527c Isaku Yamahata
                            sltsta);
316 0428527c Isaku Yamahata
        }
317 0428527c Isaku Yamahata
318 0428527c Isaku Yamahata
        /*
319 0428527c Isaku Yamahata
         * The events control bits might be enabled or disabled,
320 0428527c Isaku Yamahata
         * Check if the software notificastion condition is satisfied
321 0428527c Isaku Yamahata
         * or disatisfied.
322 0428527c Isaku Yamahata
         *
323 0428527c Isaku Yamahata
         * 6.7.3.4 Software Notification of Hot-plug events
324 0428527c Isaku Yamahata
         */
325 0428527c Isaku Yamahata
        if (pci_msi_enabled(dev)) {
326 0428527c Isaku Yamahata
            bool msi_trigger =
327 0428527c Isaku Yamahata
                (sltctl & PCI_EXP_SLTCTL_HPIE) &&
328 0428527c Isaku Yamahata
                ((sltctl_prev ^ sltctl) & sltctl & /* stlctl: 0 -> 1 */
329 0428527c Isaku Yamahata
                 sltsta & PCI_EXP_HP_EV_SUPPORTED);
330 0428527c Isaku Yamahata
            if (msi_trigger) {
331 0428527c Isaku Yamahata
                pci_msi_notify(dev, pcie_cap_flags_get_vector(dev));
332 0428527c Isaku Yamahata
            }
333 0428527c Isaku Yamahata
        } else {
334 0428527c Isaku Yamahata
            int int_level =
335 0428527c Isaku Yamahata
                (sltctl & PCI_EXP_SLTCTL_HPIE) &&
336 0428527c Isaku Yamahata
                (sltctl & sltsta & PCI_EXP_HP_EV_SUPPORTED);
337 0428527c Isaku Yamahata
            qemu_set_irq(dev->irq[dev->exp.hpev_intx], int_level);
338 0428527c Isaku Yamahata
        }
339 0428527c Isaku Yamahata
340 0428527c Isaku Yamahata
        if (!((sltctl_prev ^ sltctl) & PCI_EXP_SLTCTL_SUPPORTED)) {
341 0428527c Isaku Yamahata
            PCIE_DEV_PRINTF(dev,
342 0428527c Isaku Yamahata
                            "sprious command completion slctl "
343 0428527c Isaku Yamahata
                            "0x%"PRIx16" -> 0x%"PRIx16"\n",
344 0428527c Isaku Yamahata
                            sltctl_prev, sltctl);
345 0428527c Isaku Yamahata
        }
346 0428527c Isaku Yamahata
347 0428527c Isaku Yamahata
        /* command completion.
348 0428527c Isaku Yamahata
         * Real hardware might take a while to complete
349 0428527c Isaku Yamahata
         * requested command because physical movement would be involved
350 0428527c Isaku Yamahata
         * like locking the electromechanical lock.
351 0428527c Isaku Yamahata
         * However in our case, command is completed instantaneously above,
352 0428527c Isaku Yamahata
         * so send a command completion event right now.
353 0428527c Isaku Yamahata
         *
354 0428527c Isaku Yamahata
         * 6.7.3.2 Command Completed Events
355 0428527c Isaku Yamahata
         */
356 0428527c Isaku Yamahata
        /* set command completed bit */
357 0428527c Isaku Yamahata
        pcie_cap_slot_event(dev, PCI_EXP_HP_EV_CCI);
358 0428527c Isaku Yamahata
    }
359 0428527c Isaku Yamahata
}
360 0428527c Isaku Yamahata
361 0428527c Isaku Yamahata
void pcie_cap_slot_push_attention_button(PCIDevice *dev)
362 0428527c Isaku Yamahata
{
363 0428527c Isaku Yamahata
    pcie_cap_slot_event(dev, PCI_EXP_HP_EV_ABP);
364 0428527c Isaku Yamahata
}
365 0428527c Isaku Yamahata
366 0428527c Isaku Yamahata
/* root control/capabilities/status. PME isn't emulated for now */
367 0428527c Isaku Yamahata
void pcie_cap_root_init(PCIDevice *dev)
368 0428527c Isaku Yamahata
{
369 0428527c Isaku Yamahata
    pci_set_word(dev->wmask + dev->exp.exp_cap + PCI_EXP_RTCTL,
370 0428527c Isaku Yamahata
                 PCI_EXP_RTCTL_SECEE | PCI_EXP_RTCTL_SENFEE |
371 0428527c Isaku Yamahata
                 PCI_EXP_RTCTL_SEFEE);
372 0428527c Isaku Yamahata
}
373 0428527c Isaku Yamahata
374 0428527c Isaku Yamahata
void pcie_cap_root_reset(PCIDevice *dev)
375 0428527c Isaku Yamahata
{
376 0428527c Isaku Yamahata
    pci_set_word(dev->config + dev->exp.exp_cap + PCI_EXP_RTCTL, 0);
377 0428527c Isaku Yamahata
}
378 0428527c Isaku Yamahata
379 0428527c Isaku Yamahata
/*
380 0428527c Isaku Yamahata
 * TODO: implement FLR:
381 0428527c Isaku Yamahata
 * Right now sets the bit which indicates FLR is supported.
382 0428527c Isaku Yamahata
 */
383 0428527c Isaku Yamahata
/* function level reset(FLR) */
384 0428527c Isaku Yamahata
void pcie_cap_flr_init(PCIDevice *dev)
385 0428527c Isaku Yamahata
{
386 0428527c Isaku Yamahata
    pci_long_test_and_set_mask(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCAP,
387 0428527c Isaku Yamahata
                               PCI_EXP_DEVCAP_FLR);
388 0428527c Isaku Yamahata
389 0428527c Isaku Yamahata
    /* Although reading BCR_FLR returns always 0,
390 0428527c Isaku Yamahata
     * the bit is made writable here in order to detect the 1b is written
391 0428527c Isaku Yamahata
     * pcie_cap_flr_write_config() test-and-clear the bit, so
392 0428527c Isaku Yamahata
     * this bit always returns 0 to the guest.
393 0428527c Isaku Yamahata
     */
394 0428527c Isaku Yamahata
    pci_word_test_and_set_mask(dev->wmask + dev->exp.exp_cap + PCI_EXP_DEVCTL,
395 0428527c Isaku Yamahata
                               PCI_EXP_DEVCTL_BCR_FLR);
396 0428527c Isaku Yamahata
}
397 0428527c Isaku Yamahata
398 0428527c Isaku Yamahata
void pcie_cap_flr_write_config(PCIDevice *dev,
399 0428527c Isaku Yamahata
                               uint32_t addr, uint32_t val, int len)
400 0428527c Isaku Yamahata
{
401 0428527c Isaku Yamahata
    uint8_t *devctl = dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL;
402 0428527c Isaku Yamahata
    if (pci_word_test_and_clear_mask(devctl, PCI_EXP_DEVCTL_BCR_FLR)) {
403 0428527c Isaku Yamahata
        /* TODO: implement FLR */
404 0428527c Isaku Yamahata
    }
405 0428527c Isaku Yamahata
}
406 0428527c Isaku Yamahata
407 0428527c Isaku Yamahata
/* Alternative Routing-ID Interpretation (ARI) */
408 0428527c Isaku Yamahata
/* ari forwarding support for down stream port */
409 0428527c Isaku Yamahata
void pcie_cap_ari_init(PCIDevice *dev)
410 0428527c Isaku Yamahata
{
411 0428527c Isaku Yamahata
    uint32_t pos = dev->exp.exp_cap;
412 0428527c Isaku Yamahata
    pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_DEVCAP2,
413 0428527c Isaku Yamahata
                               PCI_EXP_DEVCAP2_ARI);
414 0428527c Isaku Yamahata
    pci_long_test_and_set_mask(dev->wmask + pos + PCI_EXP_DEVCTL2,
415 0428527c Isaku Yamahata
                               PCI_EXP_DEVCTL2_ARI);
416 0428527c Isaku Yamahata
}
417 0428527c Isaku Yamahata
418 0428527c Isaku Yamahata
void pcie_cap_ari_reset(PCIDevice *dev)
419 0428527c Isaku Yamahata
{
420 0428527c Isaku Yamahata
    uint8_t *devctl2 = dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL2;
421 0428527c Isaku Yamahata
    pci_long_test_and_clear_mask(devctl2, PCI_EXP_DEVCTL2_ARI);
422 0428527c Isaku Yamahata
}
423 0428527c Isaku Yamahata
424 0428527c Isaku Yamahata
bool pcie_cap_is_ari_enabled(const PCIDevice *dev)
425 0428527c Isaku Yamahata
{
426 0428527c Isaku Yamahata
    if (!pci_is_express(dev)) {
427 0428527c Isaku Yamahata
        return false;
428 0428527c Isaku Yamahata
    }
429 0428527c Isaku Yamahata
    if (!dev->exp.exp_cap) {
430 0428527c Isaku Yamahata
        return false;
431 0428527c Isaku Yamahata
    }
432 0428527c Isaku Yamahata
433 0428527c Isaku Yamahata
    return pci_get_long(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL2) &
434 0428527c Isaku Yamahata
        PCI_EXP_DEVCTL2_ARI;
435 0428527c Isaku Yamahata
}
436 0428527c Isaku Yamahata
437 0428527c Isaku Yamahata
/**************************************************************************
438 0428527c Isaku Yamahata
 * pci express extended capability allocation functions
439 0428527c Isaku Yamahata
 * uint16_t ext_cap_id (16 bit)
440 0428527c Isaku Yamahata
 * uint8_t cap_ver (4 bit)
441 0428527c Isaku Yamahata
 * uint16_t cap_offset (12 bit)
442 0428527c Isaku Yamahata
 * uint16_t ext_cap_size
443 0428527c Isaku Yamahata
 */
444 0428527c Isaku Yamahata
445 0428527c Isaku Yamahata
static uint16_t pcie_find_capability_list(PCIDevice *dev, uint16_t cap_id,
446 0428527c Isaku Yamahata
                                          uint16_t *prev_p)
447 0428527c Isaku Yamahata
{
448 0428527c Isaku Yamahata
    uint16_t prev = 0;
449 0428527c Isaku Yamahata
    uint16_t next;
450 0428527c Isaku Yamahata
    uint32_t header = pci_get_long(dev->config + PCI_CONFIG_SPACE_SIZE);
451 0428527c Isaku Yamahata
452 0428527c Isaku Yamahata
    if (!header) {
453 0428527c Isaku Yamahata
        /* no extended capability */
454 0428527c Isaku Yamahata
        next = 0;
455 0428527c Isaku Yamahata
        goto out;
456 0428527c Isaku Yamahata
    }
457 0428527c Isaku Yamahata
    for (next = PCI_CONFIG_SPACE_SIZE; next;
458 0428527c Isaku Yamahata
         prev = next, next = PCI_EXT_CAP_NEXT(header)) {
459 0428527c Isaku Yamahata
460 0428527c Isaku Yamahata
        assert(next >= PCI_CONFIG_SPACE_SIZE);
461 0428527c Isaku Yamahata
        assert(next <= PCIE_CONFIG_SPACE_SIZE - 8);
462 0428527c Isaku Yamahata
463 0428527c Isaku Yamahata
        header = pci_get_long(dev->config + next);
464 0428527c Isaku Yamahata
        if (PCI_EXT_CAP_ID(header) == cap_id) {
465 0428527c Isaku Yamahata
            break;
466 0428527c Isaku Yamahata
        }
467 0428527c Isaku Yamahata
    }
468 0428527c Isaku Yamahata
469 0428527c Isaku Yamahata
out:
470 0428527c Isaku Yamahata
    if (prev_p) {
471 0428527c Isaku Yamahata
        *prev_p = prev;
472 0428527c Isaku Yamahata
    }
473 0428527c Isaku Yamahata
    return next;
474 0428527c Isaku Yamahata
}
475 0428527c Isaku Yamahata
476 0428527c Isaku Yamahata
uint16_t pcie_find_capability(PCIDevice *dev, uint16_t cap_id)
477 0428527c Isaku Yamahata
{
478 0428527c Isaku Yamahata
    return pcie_find_capability_list(dev, cap_id, NULL);
479 0428527c Isaku Yamahata
}
480 0428527c Isaku Yamahata
481 0428527c Isaku Yamahata
static void pcie_ext_cap_set_next(PCIDevice *dev, uint16_t pos, uint16_t next)
482 0428527c Isaku Yamahata
{
483 0428527c Isaku Yamahata
    uint16_t header = pci_get_long(dev->config + pos);
484 0428527c Isaku Yamahata
    assert(!(next & (PCI_EXT_CAP_ALIGN - 1)));
485 0428527c Isaku Yamahata
    header = (header & ~PCI_EXT_CAP_NEXT_MASK) |
486 0428527c Isaku Yamahata
        ((next << PCI_EXT_CAP_NEXT_SHIFT) & PCI_EXT_CAP_NEXT_MASK);
487 0428527c Isaku Yamahata
    pci_set_long(dev->config + pos, header);
488 0428527c Isaku Yamahata
}
489 0428527c Isaku Yamahata
490 0428527c Isaku Yamahata
/*
491 0428527c Isaku Yamahata
 * caller must supply valid (offset, size) * such that the range shouldn't
492 0428527c Isaku Yamahata
 * overlap with other capability or other registers.
493 0428527c Isaku Yamahata
 * This function doesn't check it.
494 0428527c Isaku Yamahata
 */
495 0428527c Isaku Yamahata
void pcie_add_capability(PCIDevice *dev,
496 0428527c Isaku Yamahata
                         uint16_t cap_id, uint8_t cap_ver,
497 0428527c Isaku Yamahata
                         uint16_t offset, uint16_t size)
498 0428527c Isaku Yamahata
{
499 0428527c Isaku Yamahata
    uint32_t header;
500 0428527c Isaku Yamahata
    uint16_t next;
501 0428527c Isaku Yamahata
502 0428527c Isaku Yamahata
    assert(offset >= PCI_CONFIG_SPACE_SIZE);
503 0428527c Isaku Yamahata
    assert(offset < offset + size);
504 0428527c Isaku Yamahata
    assert(offset + size < PCIE_CONFIG_SPACE_SIZE);
505 0428527c Isaku Yamahata
    assert(size >= 8);
506 0428527c Isaku Yamahata
    assert(pci_is_express(dev));
507 0428527c Isaku Yamahata
508 0428527c Isaku Yamahata
    if (offset == PCI_CONFIG_SPACE_SIZE) {
509 0428527c Isaku Yamahata
        header = pci_get_long(dev->config + offset);
510 0428527c Isaku Yamahata
        next = PCI_EXT_CAP_NEXT(header);
511 0428527c Isaku Yamahata
    } else {
512 0428527c Isaku Yamahata
        uint16_t prev;
513 0428527c Isaku Yamahata
514 0428527c Isaku Yamahata
        /* 0 is reserved cap id. use internally to find the last capability
515 0428527c Isaku Yamahata
           in the linked list */
516 0428527c Isaku Yamahata
        next = pcie_find_capability_list(dev, 0, &prev);
517 0428527c Isaku Yamahata
518 0428527c Isaku Yamahata
        assert(prev >= PCI_CONFIG_SPACE_SIZE);
519 0428527c Isaku Yamahata
        assert(next == 0);
520 0428527c Isaku Yamahata
        pcie_ext_cap_set_next(dev, prev, offset);
521 0428527c Isaku Yamahata
    }
522 0428527c Isaku Yamahata
    pci_set_long(dev->config + offset, PCI_EXT_CAP(cap_id, cap_ver, next));
523 0428527c Isaku Yamahata
524 0428527c Isaku Yamahata
    /* Make capability read-only by default */
525 0428527c Isaku Yamahata
    memset(dev->wmask + offset, 0, size);
526 0428527c Isaku Yamahata
    memset(dev->w1cmask + offset, 0, size);
527 0428527c Isaku Yamahata
    /* Check capability by default */
528 0428527c Isaku Yamahata
    memset(dev->cmask + offset, 0xFF, size);
529 0428527c Isaku Yamahata
}
530 0428527c Isaku Yamahata
531 0428527c Isaku Yamahata
/**************************************************************************
532 0428527c Isaku Yamahata
 * pci express extended capability helper functions
533 0428527c Isaku Yamahata
 */
534 0428527c Isaku Yamahata
535 0428527c Isaku Yamahata
/* ARI */
536 0428527c Isaku Yamahata
void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn)
537 0428527c Isaku Yamahata
{
538 0428527c Isaku Yamahata
    pcie_add_capability(dev, PCI_EXT_CAP_ID_ARI, PCI_ARI_VER,
539 0428527c Isaku Yamahata
                        offset, PCI_ARI_SIZEOF);
540 0428527c Isaku Yamahata
    pci_set_long(dev->config + offset + PCI_ARI_CAP, PCI_ARI_CAP_NFN(nextfn));
541 0428527c Isaku Yamahata
}