Statistics
| Branch: | Revision:

root / hw / pci / pcie.c @ ddf5636d

History | View | Annotate | Download (20.2 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 d8dfad9c Blue Swirl
#include "qemu-common.h"
22 c759b24f Michael S. Tsirkin
#include "hw/pci/pci_bridge.h"
23 c759b24f Michael S. Tsirkin
#include "hw/pci/pcie.h"
24 c759b24f Michael S. Tsirkin
#include "hw/pci/msix.h"
25 c759b24f Michael S. Tsirkin
#include "hw/pci/msi.h"
26 06aac7bd Michael S. Tsirkin
#include "hw/pci/pci_bus.h"
27 c759b24f Michael S. Tsirkin
#include "hw/pci/pcie_regs.h"
28 1de7afc9 Paolo Bonzini
#include "qemu/range.h"
29 a66e657e Igor Mammedov
#include "qapi/qmp/qerror.h"
30 0428527c Isaku Yamahata
31 0428527c Isaku Yamahata
//#define DEBUG_PCIE
32 0428527c Isaku Yamahata
#ifdef DEBUG_PCIE
33 0428527c Isaku Yamahata
# define PCIE_DPRINTF(fmt, ...)                                         \
34 0428527c Isaku Yamahata
    fprintf(stderr, "%s:%d " fmt, __func__, __LINE__, ## __VA_ARGS__)
35 0428527c Isaku Yamahata
#else
36 0428527c Isaku Yamahata
# define PCIE_DPRINTF(fmt, ...) do {} while (0)
37 0428527c Isaku Yamahata
#endif
38 0428527c Isaku Yamahata
#define PCIE_DEV_PRINTF(dev, fmt, ...)                                  \
39 0428527c Isaku Yamahata
    PCIE_DPRINTF("%s:%x "fmt, (dev)->name, (dev)->devfn, ## __VA_ARGS__)
40 0428527c Isaku Yamahata
41 0428527c Isaku Yamahata
42 0428527c Isaku Yamahata
/***************************************************************************
43 0428527c Isaku Yamahata
 * pci express capability helper functions
44 0428527c Isaku Yamahata
 */
45 0428527c Isaku Yamahata
int pcie_cap_init(PCIDevice *dev, uint8_t offset, uint8_t type, uint8_t port)
46 0428527c Isaku Yamahata
{
47 0428527c Isaku Yamahata
    int pos;
48 0428527c Isaku Yamahata
    uint8_t *exp_cap;
49 0428527c Isaku Yamahata
50 0428527c Isaku Yamahata
    assert(pci_is_express(dev));
51 0428527c Isaku Yamahata
52 0428527c Isaku Yamahata
    pos = pci_add_capability(dev, PCI_CAP_ID_EXP, offset,
53 0428527c Isaku Yamahata
                                 PCI_EXP_VER2_SIZEOF);
54 0428527c Isaku Yamahata
    if (pos < 0) {
55 0428527c Isaku Yamahata
        return pos;
56 0428527c Isaku Yamahata
    }
57 0428527c Isaku Yamahata
    dev->exp.exp_cap = pos;
58 0428527c Isaku Yamahata
    exp_cap = dev->config + pos;
59 0428527c Isaku Yamahata
60 0428527c Isaku Yamahata
    /* capability register
61 0428527c Isaku Yamahata
       interrupt message number defaults to 0 */
62 0428527c Isaku Yamahata
    pci_set_word(exp_cap + PCI_EXP_FLAGS,
63 0428527c Isaku Yamahata
                 ((type << PCI_EXP_FLAGS_TYPE_SHIFT) & PCI_EXP_FLAGS_TYPE) |
64 0428527c Isaku Yamahata
                 PCI_EXP_FLAGS_VER2);
65 0428527c Isaku Yamahata
66 0428527c Isaku Yamahata
    /* device capability register
67 0428527c Isaku Yamahata
     * table 7-12:
68 0428527c Isaku Yamahata
     * roll based error reporting bit must be set by all
69 0428527c Isaku Yamahata
     * Functions conforming to the ECN, PCI Express Base
70 0428527c Isaku Yamahata
     * Specification, Revision 1.1., or subsequent PCI Express Base
71 0428527c Isaku Yamahata
     * Specification revisions.
72 0428527c Isaku Yamahata
     */
73 0428527c Isaku Yamahata
    pci_set_long(exp_cap + PCI_EXP_DEVCAP, PCI_EXP_DEVCAP_RBER);
74 0428527c Isaku Yamahata
75 0428527c Isaku Yamahata
    pci_set_long(exp_cap + PCI_EXP_LNKCAP,
76 0428527c Isaku Yamahata
                 (port << PCI_EXP_LNKCAP_PN_SHIFT) |
77 0428527c Isaku Yamahata
                 PCI_EXP_LNKCAP_ASPMS_0S |
78 0428527c Isaku Yamahata
                 PCI_EXP_LNK_MLW_1 |
79 0428527c Isaku Yamahata
                 PCI_EXP_LNK_LS_25);
80 0428527c Isaku Yamahata
81 0428527c Isaku Yamahata
    pci_set_word(exp_cap + PCI_EXP_LNKSTA,
82 0428527c Isaku Yamahata
                 PCI_EXP_LNK_MLW_1 | PCI_EXP_LNK_LS_25);
83 0428527c Isaku Yamahata
84 0428527c Isaku Yamahata
    pci_set_long(exp_cap + PCI_EXP_DEVCAP2,
85 0428527c Isaku Yamahata
                 PCI_EXP_DEVCAP2_EFF | PCI_EXP_DEVCAP2_EETLPP);
86 0428527c Isaku Yamahata
87 0428527c Isaku Yamahata
    pci_set_word(dev->wmask + pos, PCI_EXP_DEVCTL2_EETLPPB);
88 0428527c Isaku Yamahata
    return pos;
89 0428527c Isaku Yamahata
}
90 0428527c Isaku Yamahata
91 6214e73c Alex Williamson
int pcie_endpoint_cap_init(PCIDevice *dev, uint8_t offset)
92 6214e73c Alex Williamson
{
93 6214e73c Alex Williamson
    uint8_t type = PCI_EXP_TYPE_ENDPOINT;
94 6214e73c Alex Williamson
95 6214e73c Alex Williamson
    /*
96 6214e73c Alex Williamson
     * Windows guests will report Code 10, device cannot start, if
97 6214e73c Alex Williamson
     * a regular Endpoint type is exposed on a root complex.  These
98 6214e73c Alex Williamson
     * should instead be Root Complex Integrated Endpoints.
99 6214e73c Alex Williamson
     */
100 6214e73c Alex Williamson
    if (pci_bus_is_express(dev->bus) && pci_bus_is_root(dev->bus)) {
101 6214e73c Alex Williamson
        type = PCI_EXP_TYPE_RC_END;
102 6214e73c Alex Williamson
    }
103 6214e73c Alex Williamson
104 6214e73c Alex Williamson
    return pcie_cap_init(dev, offset, type, 0);
105 6214e73c Alex Williamson
}
106 6214e73c Alex Williamson
107 0428527c Isaku Yamahata
void pcie_cap_exit(PCIDevice *dev)
108 0428527c Isaku Yamahata
{
109 0428527c Isaku Yamahata
    pci_del_capability(dev, PCI_CAP_ID_EXP, PCI_EXP_VER2_SIZEOF);
110 0428527c Isaku Yamahata
}
111 0428527c Isaku Yamahata
112 0428527c Isaku Yamahata
uint8_t pcie_cap_get_type(const PCIDevice *dev)
113 0428527c Isaku Yamahata
{
114 0428527c Isaku Yamahata
    uint32_t pos = dev->exp.exp_cap;
115 0428527c Isaku Yamahata
    assert(pos > 0);
116 0428527c Isaku Yamahata
    return (pci_get_word(dev->config + pos + PCI_EXP_FLAGS) &
117 0428527c Isaku Yamahata
            PCI_EXP_FLAGS_TYPE) >> PCI_EXP_FLAGS_TYPE_SHIFT;
118 0428527c Isaku Yamahata
}
119 0428527c Isaku Yamahata
120 0428527c Isaku Yamahata
/* MSI/MSI-X */
121 0428527c Isaku Yamahata
/* pci express interrupt message number */
122 0428527c Isaku Yamahata
/* 7.8.2 PCI Express Capabilities Register: Interrupt Message Number */
123 0428527c Isaku Yamahata
void pcie_cap_flags_set_vector(PCIDevice *dev, uint8_t vector)
124 0428527c Isaku Yamahata
{
125 0428527c Isaku Yamahata
    uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
126 0428527c Isaku Yamahata
    assert(vector < 32);
127 0428527c Isaku Yamahata
    pci_word_test_and_clear_mask(exp_cap + PCI_EXP_FLAGS, PCI_EXP_FLAGS_IRQ);
128 0428527c Isaku Yamahata
    pci_word_test_and_set_mask(exp_cap + PCI_EXP_FLAGS,
129 0428527c Isaku Yamahata
                               vector << PCI_EXP_FLAGS_IRQ_SHIFT);
130 0428527c Isaku Yamahata
}
131 0428527c Isaku Yamahata
132 0428527c Isaku Yamahata
uint8_t pcie_cap_flags_get_vector(PCIDevice *dev)
133 0428527c Isaku Yamahata
{
134 0428527c Isaku Yamahata
    return (pci_get_word(dev->config + dev->exp.exp_cap + PCI_EXP_FLAGS) &
135 0428527c Isaku Yamahata
            PCI_EXP_FLAGS_IRQ) >> PCI_EXP_FLAGS_IRQ_SHIFT;
136 0428527c Isaku Yamahata
}
137 0428527c Isaku Yamahata
138 0428527c Isaku Yamahata
void pcie_cap_deverr_init(PCIDevice *dev)
139 0428527c Isaku Yamahata
{
140 0428527c Isaku Yamahata
    uint32_t pos = dev->exp.exp_cap;
141 0428527c Isaku Yamahata
    pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_DEVCAP,
142 0428527c Isaku Yamahata
                               PCI_EXP_DEVCAP_RBER);
143 0428527c Isaku Yamahata
    pci_long_test_and_set_mask(dev->wmask + pos + PCI_EXP_DEVCTL,
144 0428527c Isaku Yamahata
                               PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE |
145 0428527c Isaku Yamahata
                               PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE);
146 0428527c Isaku Yamahata
    pci_long_test_and_set_mask(dev->w1cmask + pos + PCI_EXP_DEVSTA,
147 0428527c Isaku Yamahata
                               PCI_EXP_DEVSTA_CED | PCI_EXP_DEVSTA_NFED |
148 0428527c Isaku Yamahata
                               PCI_EXP_DEVSTA_URD | PCI_EXP_DEVSTA_URD);
149 0428527c Isaku Yamahata
}
150 0428527c Isaku Yamahata
151 0428527c Isaku Yamahata
void pcie_cap_deverr_reset(PCIDevice *dev)
152 0428527c Isaku Yamahata
{
153 0428527c Isaku Yamahata
    uint8_t *devctl = dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL;
154 0428527c Isaku Yamahata
    pci_long_test_and_clear_mask(devctl,
155 0428527c Isaku Yamahata
                                 PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE |
156 0428527c Isaku Yamahata
                                 PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE);
157 0428527c Isaku Yamahata
}
158 0428527c Isaku Yamahata
159 6bde6aaa Michael S. Tsirkin
static void hotplug_event_update_event_status(PCIDevice *dev)
160 6bde6aaa Michael S. Tsirkin
{
161 6bde6aaa Michael S. Tsirkin
    uint32_t pos = dev->exp.exp_cap;
162 6bde6aaa Michael S. Tsirkin
    uint8_t *exp_cap = dev->config + pos;
163 6bde6aaa Michael S. Tsirkin
    uint16_t sltctl = pci_get_word(exp_cap + PCI_EXP_SLTCTL);
164 6bde6aaa Michael S. Tsirkin
    uint16_t sltsta = pci_get_word(exp_cap + PCI_EXP_SLTSTA);
165 6bde6aaa Michael S. Tsirkin
166 6bde6aaa Michael S. Tsirkin
    dev->exp.hpev_notified = (sltctl & PCI_EXP_SLTCTL_HPIE) &&
167 6bde6aaa Michael S. Tsirkin
        (sltsta & sltctl & PCI_EXP_HP_EV_SUPPORTED);
168 6bde6aaa Michael S. Tsirkin
}
169 6bde6aaa Michael S. Tsirkin
170 6bde6aaa Michael S. Tsirkin
static void hotplug_event_notify(PCIDevice *dev)
171 6bde6aaa Michael S. Tsirkin
{
172 6bde6aaa Michael S. Tsirkin
    bool prev = dev->exp.hpev_notified;
173 6bde6aaa Michael S. Tsirkin
174 6bde6aaa Michael S. Tsirkin
    hotplug_event_update_event_status(dev);
175 6bde6aaa Michael S. Tsirkin
176 6bde6aaa Michael S. Tsirkin
    if (prev == dev->exp.hpev_notified) {
177 6bde6aaa Michael S. Tsirkin
        return;
178 6bde6aaa Michael S. Tsirkin
    }
179 6bde6aaa Michael S. Tsirkin
180 6bde6aaa Michael S. Tsirkin
    /* Note: the logic above does not take into account whether interrupts
181 6bde6aaa Michael S. Tsirkin
     * are masked. The result is that interrupt will be sent when it is
182 6bde6aaa Michael S. Tsirkin
     * subsequently unmasked. This appears to be legal: Section 6.7.3.4:
183 6bde6aaa Michael S. Tsirkin
     * The Port may optionally send an MSI when there are hot-plug events that
184 6bde6aaa Michael S. Tsirkin
     * occur while interrupt generation is disabled, and interrupt generation is
185 6bde6aaa Michael S. Tsirkin
     * subsequently enabled. */
186 4a9dd665 Michael S. Tsirkin
    if (msix_enabled(dev)) {
187 4a9dd665 Michael S. Tsirkin
        msix_notify(dev, pcie_cap_flags_get_vector(dev));
188 4a9dd665 Michael S. Tsirkin
    } else if (msi_enabled(dev)) {
189 4a9dd665 Michael S. Tsirkin
        msi_notify(dev, pcie_cap_flags_get_vector(dev));
190 4a9dd665 Michael S. Tsirkin
    } else {
191 5a03e708 Marcel Apfelbaum
        pci_set_irq(dev, dev->exp.hpev_notified);
192 6bde6aaa Michael S. Tsirkin
    }
193 6bde6aaa Michael S. Tsirkin
}
194 6bde6aaa Michael S. Tsirkin
195 1553d4f1 Isaku Yamahata
static void hotplug_event_clear(PCIDevice *dev)
196 1553d4f1 Isaku Yamahata
{
197 1553d4f1 Isaku Yamahata
    hotplug_event_update_event_status(dev);
198 1553d4f1 Isaku Yamahata
    if (!msix_enabled(dev) && !msi_enabled(dev) && !dev->exp.hpev_notified) {
199 5a03e708 Marcel Apfelbaum
        pci_irq_deassert(dev);
200 1553d4f1 Isaku Yamahata
    }
201 1553d4f1 Isaku Yamahata
}
202 1553d4f1 Isaku Yamahata
203 0428527c Isaku Yamahata
/*
204 a1c7273b Stefan Weil
 * A PCI Express Hot-Plug Event has occurred, so update slot status register
205 0428527c Isaku Yamahata
 * and notify OS of the event if necessary.
206 0428527c Isaku Yamahata
 *
207 0428527c Isaku Yamahata
 * 6.7.3 PCI Express Hot-Plug Events
208 0428527c Isaku Yamahata
 * 6.7.3.4 Software Notification of Hot-Plug Events
209 0428527c Isaku Yamahata
 */
210 0428527c Isaku Yamahata
static void pcie_cap_slot_event(PCIDevice *dev, PCIExpressHotPlugEvent event)
211 0428527c Isaku Yamahata
{
212 6bde6aaa Michael S. Tsirkin
    /* Minor optimization: if nothing changed - no event is needed. */
213 6bde6aaa Michael S. Tsirkin
    if (pci_word_test_and_set_mask(dev->config + dev->exp.exp_cap +
214 6bde6aaa Michael S. Tsirkin
                                   PCI_EXP_SLTSTA, event)) {
215 0428527c Isaku Yamahata
        return;
216 0428527c Isaku Yamahata
    }
217 6bde6aaa Michael S. Tsirkin
    hotplug_event_notify(dev);
218 0428527c Isaku Yamahata
}
219 0428527c Isaku Yamahata
220 a66e657e Igor Mammedov
static void pcie_cap_slot_hotplug_common(PCIDevice *hotplug_dev,
221 a66e657e Igor Mammedov
                                         DeviceState *dev,
222 a66e657e Igor Mammedov
                                         uint8_t **exp_cap, Error **errp)
223 0428527c Isaku Yamahata
{
224 a66e657e Igor Mammedov
    PCIDevice *pci_dev = PCI_DEVICE(dev);
225 a66e657e Igor Mammedov
    *exp_cap = hotplug_dev->config + hotplug_dev->exp.exp_cap;
226 a66e657e Igor Mammedov
    uint16_t sltsta = pci_get_word(*exp_cap + PCI_EXP_SLTSTA);
227 0428527c Isaku Yamahata
228 0428527c Isaku Yamahata
    PCIE_DEV_PRINTF(pci_dev, "hotplug state: %d\n", state);
229 0428527c Isaku Yamahata
    if (sltsta & PCI_EXP_SLTSTA_EIS) {
230 0428527c Isaku Yamahata
        /* the slot is electromechanically locked.
231 0428527c Isaku Yamahata
         * This error is propagated up to qdev and then to HMP/QMP.
232 0428527c Isaku Yamahata
         */
233 a66e657e Igor Mammedov
        error_setg_errno(errp, -EBUSY, "slot is electromechanically locked");
234 0428527c Isaku Yamahata
    }
235 0428527c Isaku Yamahata
236 0428527c Isaku Yamahata
    /* TODO: multifunction hot-plug.
237 0428527c Isaku Yamahata
     * Right now, only a device of function = 0 is allowed to be
238 0428527c Isaku Yamahata
     * hot plugged/unplugged.
239 0428527c Isaku Yamahata
     */
240 0428527c Isaku Yamahata
    assert(PCI_FUNC(pci_dev->devfn) == 0);
241 a66e657e Igor Mammedov
}
242 a66e657e Igor Mammedov
243 a66e657e Igor Mammedov
void pcie_cap_slot_hotplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
244 a66e657e Igor Mammedov
                              Error **errp)
245 a66e657e Igor Mammedov
{
246 a66e657e Igor Mammedov
    uint8_t *exp_cap;
247 a66e657e Igor Mammedov
248 a66e657e Igor Mammedov
    pcie_cap_slot_hotplug_common(PCI_DEVICE(hotplug_dev), dev, &exp_cap, errp);
249 0428527c Isaku Yamahata
250 a66e657e Igor Mammedov
    /* Don't send event when device is enabled during qemu machine creation:
251 a66e657e Igor Mammedov
     * it is present on boot, no hotplug event is necessary. We do send an
252 a66e657e Igor Mammedov
     * event when the device is disabled later. */
253 a66e657e Igor Mammedov
    if (!dev->hotplugged) {
254 0428527c Isaku Yamahata
        pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
255 0428527c Isaku Yamahata
                                   PCI_EXP_SLTSTA_PDS);
256 a66e657e Igor Mammedov
        return;
257 0428527c Isaku Yamahata
    }
258 a66e657e Igor Mammedov
259 a66e657e Igor Mammedov
    pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
260 a66e657e Igor Mammedov
                               PCI_EXP_SLTSTA_PDS);
261 a66e657e Igor Mammedov
    pcie_cap_slot_event(PCI_DEVICE(hotplug_dev), PCI_EXP_HP_EV_PDC);
262 a66e657e Igor Mammedov
}
263 a66e657e Igor Mammedov
264 a66e657e Igor Mammedov
void pcie_cap_slot_hot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
265 a66e657e Igor Mammedov
                                 Error **errp)
266 a66e657e Igor Mammedov
{
267 a66e657e Igor Mammedov
    uint8_t *exp_cap;
268 a66e657e Igor Mammedov
269 a66e657e Igor Mammedov
    pcie_cap_slot_hotplug_common(PCI_DEVICE(hotplug_dev), dev, &exp_cap, errp);
270 a66e657e Igor Mammedov
271 a66e657e Igor Mammedov
    object_unparent(OBJECT(dev));
272 a66e657e Igor Mammedov
    pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
273 a66e657e Igor Mammedov
                                 PCI_EXP_SLTSTA_PDS);
274 a66e657e Igor Mammedov
    pcie_cap_slot_event(PCI_DEVICE(hotplug_dev), PCI_EXP_HP_EV_PDC);
275 0428527c Isaku Yamahata
}
276 0428527c Isaku Yamahata
277 0428527c Isaku Yamahata
/* pci express slot for pci express root/downstream port
278 0428527c Isaku Yamahata
   PCI express capability slot registers */
279 0428527c Isaku Yamahata
void pcie_cap_slot_init(PCIDevice *dev, uint16_t slot)
280 0428527c Isaku Yamahata
{
281 0428527c Isaku Yamahata
    uint32_t pos = dev->exp.exp_cap;
282 0428527c Isaku Yamahata
283 0428527c Isaku Yamahata
    pci_word_test_and_set_mask(dev->config + pos + PCI_EXP_FLAGS,
284 0428527c Isaku Yamahata
                               PCI_EXP_FLAGS_SLOT);
285 0428527c Isaku Yamahata
286 0428527c Isaku Yamahata
    pci_long_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCAP,
287 0428527c Isaku Yamahata
                                 ~PCI_EXP_SLTCAP_PSN);
288 0428527c Isaku Yamahata
    pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCAP,
289 0428527c Isaku Yamahata
                               (slot << PCI_EXP_SLTCAP_PSN_SHIFT) |
290 0428527c Isaku Yamahata
                               PCI_EXP_SLTCAP_EIP |
291 0428527c Isaku Yamahata
                               PCI_EXP_SLTCAP_HPS |
292 0428527c Isaku Yamahata
                               PCI_EXP_SLTCAP_HPC |
293 0428527c Isaku Yamahata
                               PCI_EXP_SLTCAP_PIP |
294 0428527c Isaku Yamahata
                               PCI_EXP_SLTCAP_AIP |
295 0428527c Isaku Yamahata
                               PCI_EXP_SLTCAP_ABP);
296 0428527c Isaku Yamahata
297 0428527c Isaku Yamahata
    pci_word_test_and_clear_mask(dev->config + pos + PCI_EXP_SLTCTL,
298 0428527c Isaku Yamahata
                                 PCI_EXP_SLTCTL_PIC |
299 0428527c Isaku Yamahata
                                 PCI_EXP_SLTCTL_AIC);
300 0428527c Isaku Yamahata
    pci_word_test_and_set_mask(dev->config + pos + PCI_EXP_SLTCTL,
301 0428527c Isaku Yamahata
                               PCI_EXP_SLTCTL_PIC_OFF |
302 0428527c Isaku Yamahata
                               PCI_EXP_SLTCTL_AIC_OFF);
303 0428527c Isaku Yamahata
    pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
304 0428527c Isaku Yamahata
                               PCI_EXP_SLTCTL_PIC |
305 0428527c Isaku Yamahata
                               PCI_EXP_SLTCTL_AIC |
306 0428527c Isaku Yamahata
                               PCI_EXP_SLTCTL_HPIE |
307 0428527c Isaku Yamahata
                               PCI_EXP_SLTCTL_CCIE |
308 0428527c Isaku Yamahata
                               PCI_EXP_SLTCTL_PDCE |
309 0428527c Isaku Yamahata
                               PCI_EXP_SLTCTL_ABPE);
310 0428527c Isaku Yamahata
    /* Although reading PCI_EXP_SLTCTL_EIC returns always 0,
311 0428527c Isaku Yamahata
     * make the bit writable here in order to detect 1b is written.
312 0428527c Isaku Yamahata
     * pcie_cap_slot_write_config() test-and-clear the bit, so
313 0428527c Isaku Yamahata
     * this bit always returns 0 to the guest.
314 0428527c Isaku Yamahata
     */
315 0428527c Isaku Yamahata
    pci_word_test_and_set_mask(dev->wmask + pos + PCI_EXP_SLTCTL,
316 0428527c Isaku Yamahata
                               PCI_EXP_SLTCTL_EIC);
317 0428527c Isaku Yamahata
318 0428527c Isaku Yamahata
    pci_word_test_and_set_mask(dev->w1cmask + pos + PCI_EXP_SLTSTA,
319 0428527c Isaku Yamahata
                               PCI_EXP_HP_EV_SUPPORTED);
320 0428527c Isaku Yamahata
321 6bde6aaa Michael S. Tsirkin
    dev->exp.hpev_notified = false;
322 6bde6aaa Michael S. Tsirkin
323 a66e657e Igor Mammedov
    qbus_set_hotplug_handler(BUS(pci_bridge_get_sec_bus(PCI_BRIDGE(dev))),
324 a66e657e Igor Mammedov
                             DEVICE(dev), NULL);
325 0428527c Isaku Yamahata
}
326 0428527c Isaku Yamahata
327 0428527c Isaku Yamahata
void pcie_cap_slot_reset(PCIDevice *dev)
328 0428527c Isaku Yamahata
{
329 0428527c Isaku Yamahata
    uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
330 0428527c Isaku Yamahata
331 0428527c Isaku Yamahata
    PCIE_DEV_PRINTF(dev, "reset\n");
332 0428527c Isaku Yamahata
333 0428527c Isaku Yamahata
    pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
334 0428527c Isaku Yamahata
                                 PCI_EXP_SLTCTL_EIC |
335 0428527c Isaku Yamahata
                                 PCI_EXP_SLTCTL_PIC |
336 0428527c Isaku Yamahata
                                 PCI_EXP_SLTCTL_AIC |
337 0428527c Isaku Yamahata
                                 PCI_EXP_SLTCTL_HPIE |
338 0428527c Isaku Yamahata
                                 PCI_EXP_SLTCTL_CCIE |
339 0428527c Isaku Yamahata
                                 PCI_EXP_SLTCTL_PDCE |
340 0428527c Isaku Yamahata
                                 PCI_EXP_SLTCTL_ABPE);
341 0428527c Isaku Yamahata
    pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTCTL,
342 0428527c Isaku Yamahata
                               PCI_EXP_SLTCTL_PIC_OFF |
343 0428527c Isaku Yamahata
                               PCI_EXP_SLTCTL_AIC_OFF);
344 0428527c Isaku Yamahata
345 0428527c Isaku Yamahata
    pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTSTA,
346 0428527c Isaku Yamahata
                                 PCI_EXP_SLTSTA_EIS |/* on reset,
347 0428527c Isaku Yamahata
                                                        the lock is released */
348 0428527c Isaku Yamahata
                                 PCI_EXP_SLTSTA_CC |
349 0428527c Isaku Yamahata
                                 PCI_EXP_SLTSTA_PDC |
350 0428527c Isaku Yamahata
                                 PCI_EXP_SLTSTA_ABP);
351 6bde6aaa Michael S. Tsirkin
352 804b2071 Michael S. Tsirkin
    hotplug_event_update_event_status(dev);
353 0428527c Isaku Yamahata
}
354 0428527c Isaku Yamahata
355 0428527c Isaku Yamahata
void pcie_cap_slot_write_config(PCIDevice *dev,
356 6bde6aaa Michael S. Tsirkin
                                uint32_t addr, uint32_t val, int len)
357 0428527c Isaku Yamahata
{
358 0428527c Isaku Yamahata
    uint32_t pos = dev->exp.exp_cap;
359 0428527c Isaku Yamahata
    uint8_t *exp_cap = dev->config + pos;
360 0428527c Isaku Yamahata
    uint16_t sltsta = pci_get_word(exp_cap + PCI_EXP_SLTSTA);
361 0428527c Isaku Yamahata
362 1553d4f1 Isaku Yamahata
    if (ranges_overlap(addr, len, pos + PCI_EXP_SLTSTA, 2)) {
363 1553d4f1 Isaku Yamahata
        hotplug_event_clear(dev);
364 1553d4f1 Isaku Yamahata
    }
365 1553d4f1 Isaku Yamahata
366 ac0cdda3 Michael S. Tsirkin
    if (!ranges_overlap(addr, len, pos + PCI_EXP_SLTCTL, 2)) {
367 ac0cdda3 Michael S. Tsirkin
        return;
368 ac0cdda3 Michael S. Tsirkin
    }
369 ac0cdda3 Michael S. Tsirkin
370 ac0cdda3 Michael S. Tsirkin
    if (pci_word_test_and_clear_mask(exp_cap + PCI_EXP_SLTCTL,
371 ac0cdda3 Michael S. Tsirkin
                                     PCI_EXP_SLTCTL_EIC)) {
372 ac0cdda3 Michael S. Tsirkin
        sltsta ^= PCI_EXP_SLTSTA_EIS; /* toggle PCI_EXP_SLTSTA_EIS bit */
373 ac0cdda3 Michael S. Tsirkin
        pci_set_word(exp_cap + PCI_EXP_SLTSTA, sltsta);
374 ac0cdda3 Michael S. Tsirkin
        PCIE_DEV_PRINTF(dev, "PCI_EXP_SLTCTL_EIC: "
375 ac0cdda3 Michael S. Tsirkin
                        "sltsta -> 0x%02"PRIx16"\n",
376 ac0cdda3 Michael S. Tsirkin
                        sltsta);
377 ac0cdda3 Michael S. Tsirkin
    }
378 0428527c Isaku Yamahata
379 6bde6aaa Michael S. Tsirkin
    hotplug_event_notify(dev);
380 ac0cdda3 Michael S. Tsirkin
381 ac0cdda3 Michael S. Tsirkin
    /* 
382 ac0cdda3 Michael S. Tsirkin
     * 6.7.3.2 Command Completed Events
383 ac0cdda3 Michael S. Tsirkin
     *
384 ac0cdda3 Michael S. Tsirkin
     * Software issues a command to a hot-plug capable Downstream Port by
385 ac0cdda3 Michael S. Tsirkin
     * issuing a write transaction that targets any portion of the Portโ€™s Slot
386 ac0cdda3 Michael S. Tsirkin
     * Control register. A single write to the Slot Control register is
387 ac0cdda3 Michael S. Tsirkin
     * considered to be a single command, even if the write affects more than
388 ac0cdda3 Michael S. Tsirkin
     * one field in the Slot Control register. In response to this transaction,
389 ac0cdda3 Michael S. Tsirkin
     * the Port must carry out the requested actions and then set the
390 ac0cdda3 Michael S. Tsirkin
     * associated status field for the command completed event. */
391 ac0cdda3 Michael S. Tsirkin
392 ac0cdda3 Michael S. Tsirkin
    /* Real hardware might take a while to complete requested command because
393 ac0cdda3 Michael S. Tsirkin
     * physical movement would be involved like locking the electromechanical
394 ac0cdda3 Michael S. Tsirkin
     * lock.  However in our case, command is completed instantaneously above,
395 ac0cdda3 Michael S. Tsirkin
     * so send a command completion event right now.
396 ac0cdda3 Michael S. Tsirkin
     */
397 ac0cdda3 Michael S. Tsirkin
    pcie_cap_slot_event(dev, PCI_EXP_HP_EV_CCI);
398 0428527c Isaku Yamahata
}
399 0428527c Isaku Yamahata
400 6bde6aaa Michael S. Tsirkin
int pcie_cap_slot_post_load(void *opaque, int version_id)
401 6bde6aaa Michael S. Tsirkin
{
402 6bde6aaa Michael S. Tsirkin
    PCIDevice *dev = opaque;
403 6bde6aaa Michael S. Tsirkin
    hotplug_event_update_event_status(dev);
404 6bde6aaa Michael S. Tsirkin
    return 0;
405 6bde6aaa Michael S. Tsirkin
}
406 6bde6aaa Michael S. Tsirkin
407 0428527c Isaku Yamahata
void pcie_cap_slot_push_attention_button(PCIDevice *dev)
408 0428527c Isaku Yamahata
{
409 0428527c Isaku Yamahata
    pcie_cap_slot_event(dev, PCI_EXP_HP_EV_ABP);
410 0428527c Isaku Yamahata
}
411 0428527c Isaku Yamahata
412 0428527c Isaku Yamahata
/* root control/capabilities/status. PME isn't emulated for now */
413 0428527c Isaku Yamahata
void pcie_cap_root_init(PCIDevice *dev)
414 0428527c Isaku Yamahata
{
415 0428527c Isaku Yamahata
    pci_set_word(dev->wmask + dev->exp.exp_cap + PCI_EXP_RTCTL,
416 0428527c Isaku Yamahata
                 PCI_EXP_RTCTL_SECEE | PCI_EXP_RTCTL_SENFEE |
417 0428527c Isaku Yamahata
                 PCI_EXP_RTCTL_SEFEE);
418 0428527c Isaku Yamahata
}
419 0428527c Isaku Yamahata
420 0428527c Isaku Yamahata
void pcie_cap_root_reset(PCIDevice *dev)
421 0428527c Isaku Yamahata
{
422 0428527c Isaku Yamahata
    pci_set_word(dev->config + dev->exp.exp_cap + PCI_EXP_RTCTL, 0);
423 0428527c Isaku Yamahata
}
424 0428527c Isaku Yamahata
425 0428527c Isaku Yamahata
/* function level reset(FLR) */
426 0428527c Isaku Yamahata
void pcie_cap_flr_init(PCIDevice *dev)
427 0428527c Isaku Yamahata
{
428 0428527c Isaku Yamahata
    pci_long_test_and_set_mask(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCAP,
429 0428527c Isaku Yamahata
                               PCI_EXP_DEVCAP_FLR);
430 0428527c Isaku Yamahata
431 0428527c Isaku Yamahata
    /* Although reading BCR_FLR returns always 0,
432 0428527c Isaku Yamahata
     * the bit is made writable here in order to detect the 1b is written
433 0428527c Isaku Yamahata
     * pcie_cap_flr_write_config() test-and-clear the bit, so
434 0428527c Isaku Yamahata
     * this bit always returns 0 to the guest.
435 0428527c Isaku Yamahata
     */
436 0428527c Isaku Yamahata
    pci_word_test_and_set_mask(dev->wmask + dev->exp.exp_cap + PCI_EXP_DEVCTL,
437 0428527c Isaku Yamahata
                               PCI_EXP_DEVCTL_BCR_FLR);
438 0428527c Isaku Yamahata
}
439 0428527c Isaku Yamahata
440 0428527c Isaku Yamahata
void pcie_cap_flr_write_config(PCIDevice *dev,
441 0428527c Isaku Yamahata
                               uint32_t addr, uint32_t val, int len)
442 0428527c Isaku Yamahata
{
443 0428527c Isaku Yamahata
    uint8_t *devctl = dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL;
444 0ead87c8 Isaku Yamahata
    if (pci_get_word(devctl) & PCI_EXP_DEVCTL_BCR_FLR) {
445 0ead87c8 Isaku Yamahata
        /* Clear PCI_EXP_DEVCTL_BCR_FLR after invoking the reset handler
446 0ead87c8 Isaku Yamahata
           so the handler can detect FLR by looking at this bit. */
447 0ead87c8 Isaku Yamahata
        pci_device_reset(dev);
448 0ead87c8 Isaku Yamahata
        pci_word_test_and_clear_mask(devctl, PCI_EXP_DEVCTL_BCR_FLR);
449 0428527c Isaku Yamahata
    }
450 0428527c Isaku Yamahata
}
451 0428527c Isaku Yamahata
452 0428527c Isaku Yamahata
/* Alternative Routing-ID Interpretation (ARI) */
453 0428527c Isaku Yamahata
/* ari forwarding support for down stream port */
454 0428527c Isaku Yamahata
void pcie_cap_ari_init(PCIDevice *dev)
455 0428527c Isaku Yamahata
{
456 0428527c Isaku Yamahata
    uint32_t pos = dev->exp.exp_cap;
457 0428527c Isaku Yamahata
    pci_long_test_and_set_mask(dev->config + pos + PCI_EXP_DEVCAP2,
458 0428527c Isaku Yamahata
                               PCI_EXP_DEVCAP2_ARI);
459 0428527c Isaku Yamahata
    pci_long_test_and_set_mask(dev->wmask + pos + PCI_EXP_DEVCTL2,
460 0428527c Isaku Yamahata
                               PCI_EXP_DEVCTL2_ARI);
461 0428527c Isaku Yamahata
}
462 0428527c Isaku Yamahata
463 0428527c Isaku Yamahata
void pcie_cap_ari_reset(PCIDevice *dev)
464 0428527c Isaku Yamahata
{
465 0428527c Isaku Yamahata
    uint8_t *devctl2 = dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL2;
466 0428527c Isaku Yamahata
    pci_long_test_and_clear_mask(devctl2, PCI_EXP_DEVCTL2_ARI);
467 0428527c Isaku Yamahata
}
468 0428527c Isaku Yamahata
469 0428527c Isaku Yamahata
bool pcie_cap_is_ari_enabled(const PCIDevice *dev)
470 0428527c Isaku Yamahata
{
471 0428527c Isaku Yamahata
    if (!pci_is_express(dev)) {
472 0428527c Isaku Yamahata
        return false;
473 0428527c Isaku Yamahata
    }
474 0428527c Isaku Yamahata
    if (!dev->exp.exp_cap) {
475 0428527c Isaku Yamahata
        return false;
476 0428527c Isaku Yamahata
    }
477 0428527c Isaku Yamahata
478 0428527c Isaku Yamahata
    return pci_get_long(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL2) &
479 0428527c Isaku Yamahata
        PCI_EXP_DEVCTL2_ARI;
480 0428527c Isaku Yamahata
}
481 0428527c Isaku Yamahata
482 0428527c Isaku Yamahata
/**************************************************************************
483 0428527c Isaku Yamahata
 * pci express extended capability allocation functions
484 0428527c Isaku Yamahata
 * uint16_t ext_cap_id (16 bit)
485 0428527c Isaku Yamahata
 * uint8_t cap_ver (4 bit)
486 0428527c Isaku Yamahata
 * uint16_t cap_offset (12 bit)
487 0428527c Isaku Yamahata
 * uint16_t ext_cap_size
488 0428527c Isaku Yamahata
 */
489 0428527c Isaku Yamahata
490 0428527c Isaku Yamahata
static uint16_t pcie_find_capability_list(PCIDevice *dev, uint16_t cap_id,
491 0428527c Isaku Yamahata
                                          uint16_t *prev_p)
492 0428527c Isaku Yamahata
{
493 0428527c Isaku Yamahata
    uint16_t prev = 0;
494 0428527c Isaku Yamahata
    uint16_t next;
495 0428527c Isaku Yamahata
    uint32_t header = pci_get_long(dev->config + PCI_CONFIG_SPACE_SIZE);
496 0428527c Isaku Yamahata
497 0428527c Isaku Yamahata
    if (!header) {
498 0428527c Isaku Yamahata
        /* no extended capability */
499 0428527c Isaku Yamahata
        next = 0;
500 0428527c Isaku Yamahata
        goto out;
501 0428527c Isaku Yamahata
    }
502 0428527c Isaku Yamahata
    for (next = PCI_CONFIG_SPACE_SIZE; next;
503 0428527c Isaku Yamahata
         prev = next, next = PCI_EXT_CAP_NEXT(header)) {
504 0428527c Isaku Yamahata
505 0428527c Isaku Yamahata
        assert(next >= PCI_CONFIG_SPACE_SIZE);
506 0428527c Isaku Yamahata
        assert(next <= PCIE_CONFIG_SPACE_SIZE - 8);
507 0428527c Isaku Yamahata
508 0428527c Isaku Yamahata
        header = pci_get_long(dev->config + next);
509 0428527c Isaku Yamahata
        if (PCI_EXT_CAP_ID(header) == cap_id) {
510 0428527c Isaku Yamahata
            break;
511 0428527c Isaku Yamahata
        }
512 0428527c Isaku Yamahata
    }
513 0428527c Isaku Yamahata
514 0428527c Isaku Yamahata
out:
515 0428527c Isaku Yamahata
    if (prev_p) {
516 0428527c Isaku Yamahata
        *prev_p = prev;
517 0428527c Isaku Yamahata
    }
518 0428527c Isaku Yamahata
    return next;
519 0428527c Isaku Yamahata
}
520 0428527c Isaku Yamahata
521 0428527c Isaku Yamahata
uint16_t pcie_find_capability(PCIDevice *dev, uint16_t cap_id)
522 0428527c Isaku Yamahata
{
523 0428527c Isaku Yamahata
    return pcie_find_capability_list(dev, cap_id, NULL);
524 0428527c Isaku Yamahata
}
525 0428527c Isaku Yamahata
526 0428527c Isaku Yamahata
static void pcie_ext_cap_set_next(PCIDevice *dev, uint16_t pos, uint16_t next)
527 0428527c Isaku Yamahata
{
528 812d2594 Knut Omang
    uint32_t header = pci_get_long(dev->config + pos);
529 0428527c Isaku Yamahata
    assert(!(next & (PCI_EXT_CAP_ALIGN - 1)));
530 0428527c Isaku Yamahata
    header = (header & ~PCI_EXT_CAP_NEXT_MASK) |
531 0428527c Isaku Yamahata
        ((next << PCI_EXT_CAP_NEXT_SHIFT) & PCI_EXT_CAP_NEXT_MASK);
532 0428527c Isaku Yamahata
    pci_set_long(dev->config + pos, header);
533 0428527c Isaku Yamahata
}
534 0428527c Isaku Yamahata
535 0428527c Isaku Yamahata
/*
536 0428527c Isaku Yamahata
 * caller must supply valid (offset, size) * such that the range shouldn't
537 0428527c Isaku Yamahata
 * overlap with other capability or other registers.
538 0428527c Isaku Yamahata
 * This function doesn't check it.
539 0428527c Isaku Yamahata
 */
540 0428527c Isaku Yamahata
void pcie_add_capability(PCIDevice *dev,
541 0428527c Isaku Yamahata
                         uint16_t cap_id, uint8_t cap_ver,
542 0428527c Isaku Yamahata
                         uint16_t offset, uint16_t size)
543 0428527c Isaku Yamahata
{
544 0428527c Isaku Yamahata
    uint32_t header;
545 0428527c Isaku Yamahata
    uint16_t next;
546 0428527c Isaku Yamahata
547 0428527c Isaku Yamahata
    assert(offset >= PCI_CONFIG_SPACE_SIZE);
548 0428527c Isaku Yamahata
    assert(offset < offset + size);
549 0428527c Isaku Yamahata
    assert(offset + size < PCIE_CONFIG_SPACE_SIZE);
550 0428527c Isaku Yamahata
    assert(size >= 8);
551 0428527c Isaku Yamahata
    assert(pci_is_express(dev));
552 0428527c Isaku Yamahata
553 0428527c Isaku Yamahata
    if (offset == PCI_CONFIG_SPACE_SIZE) {
554 0428527c Isaku Yamahata
        header = pci_get_long(dev->config + offset);
555 0428527c Isaku Yamahata
        next = PCI_EXT_CAP_NEXT(header);
556 0428527c Isaku Yamahata
    } else {
557 0428527c Isaku Yamahata
        uint16_t prev;
558 0428527c Isaku Yamahata
559 0428527c Isaku Yamahata
        /* 0 is reserved cap id. use internally to find the last capability
560 0428527c Isaku Yamahata
           in the linked list */
561 0428527c Isaku Yamahata
        next = pcie_find_capability_list(dev, 0, &prev);
562 0428527c Isaku Yamahata
563 0428527c Isaku Yamahata
        assert(prev >= PCI_CONFIG_SPACE_SIZE);
564 0428527c Isaku Yamahata
        assert(next == 0);
565 0428527c Isaku Yamahata
        pcie_ext_cap_set_next(dev, prev, offset);
566 0428527c Isaku Yamahata
    }
567 0428527c Isaku Yamahata
    pci_set_long(dev->config + offset, PCI_EXT_CAP(cap_id, cap_ver, next));
568 0428527c Isaku Yamahata
569 0428527c Isaku Yamahata
    /* Make capability read-only by default */
570 0428527c Isaku Yamahata
    memset(dev->wmask + offset, 0, size);
571 0428527c Isaku Yamahata
    memset(dev->w1cmask + offset, 0, size);
572 0428527c Isaku Yamahata
    /* Check capability by default */
573 0428527c Isaku Yamahata
    memset(dev->cmask + offset, 0xFF, size);
574 0428527c Isaku Yamahata
}
575 0428527c Isaku Yamahata
576 0428527c Isaku Yamahata
/**************************************************************************
577 0428527c Isaku Yamahata
 * pci express extended capability helper functions
578 0428527c Isaku Yamahata
 */
579 0428527c Isaku Yamahata
580 0428527c Isaku Yamahata
/* ARI */
581 0428527c Isaku Yamahata
void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn)
582 0428527c Isaku Yamahata
{
583 0428527c Isaku Yamahata
    pcie_add_capability(dev, PCI_EXT_CAP_ID_ARI, PCI_ARI_VER,
584 0428527c Isaku Yamahata
                        offset, PCI_ARI_SIZEOF);
585 0428527c Isaku Yamahata
    pci_set_long(dev->config + offset + PCI_ARI_CAP, PCI_ARI_CAP_NFN(nextfn));
586 0428527c Isaku Yamahata
}