Statistics
| Branch: | Revision:

root / hw / pci / shpc.c @ 216db403

History | View | Annotate | Download (24.6 kB)

1 b4a42f81 Paolo Bonzini
#include "qemu-common.h"
2 1dc324d2 Michael S. Tsirkin
#include <strings.h>
3 1dc324d2 Michael S. Tsirkin
#include <stdint.h>
4 1de7afc9 Paolo Bonzini
#include "qemu/range.h"
5 b4a42f81 Paolo Bonzini
#include "qemu/error-report.h"
6 c759b24f Michael S. Tsirkin
#include "hw/pci/shpc.h"
7 c759b24f Michael S. Tsirkin
#include "hw/pci/pci.h"
8 06aac7bd Michael S. Tsirkin
#include "hw/pci/pci_bus.h"
9 c759b24f Michael S. Tsirkin
#include "hw/pci/msi.h"
10 5d268704 Igor Mammedov
#include "qapi/qmp/qerror.h"
11 1dc324d2 Michael S. Tsirkin
12 1dc324d2 Michael S. Tsirkin
/* TODO: model power only and disabled slot states. */
13 1dc324d2 Michael S. Tsirkin
/* TODO: handle SERR and wakeups */
14 1dc324d2 Michael S. Tsirkin
/* TODO: consider enabling 66MHz support */
15 1dc324d2 Michael S. Tsirkin
16 1dc324d2 Michael S. Tsirkin
/* TODO: remove fully only on state DISABLED and LED off.
17 1dc324d2 Michael S. Tsirkin
 * track state to properly record this. */
18 1dc324d2 Michael S. Tsirkin
19 1dc324d2 Michael S. Tsirkin
/* SHPC Working Register Set */
20 1dc324d2 Michael S. Tsirkin
#define SHPC_BASE_OFFSET  0x00 /* 4 bytes */
21 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOTS_33     0x04 /* 4 bytes. Also encodes PCI-X slots. */
22 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOTS_66     0x08 /* 4 bytes. */
23 1dc324d2 Michael S. Tsirkin
#define SHPC_NSLOTS       0x0C /* 1 byte */
24 1dc324d2 Michael S. Tsirkin
#define SHPC_FIRST_DEV    0x0D /* 1 byte */
25 1dc324d2 Michael S. Tsirkin
#define SHPC_PHYS_SLOT    0x0E /* 2 byte */
26 1dc324d2 Michael S. Tsirkin
#define SHPC_PHYS_NUM_MAX 0x7ff
27 1dc324d2 Michael S. Tsirkin
#define SHPC_PHYS_NUM_UP  0x2000
28 1dc324d2 Michael S. Tsirkin
#define SHPC_PHYS_MRL     0x4000
29 1dc324d2 Michael S. Tsirkin
#define SHPC_PHYS_BUTTON  0x8000
30 1dc324d2 Michael S. Tsirkin
#define SHPC_SEC_BUS      0x10 /* 2 bytes */
31 1dc324d2 Michael S. Tsirkin
#define SHPC_SEC_BUS_33   0x0
32 1dc324d2 Michael S. Tsirkin
#define SHPC_SEC_BUS_66   0x1 /* Unused */
33 1dc324d2 Michael S. Tsirkin
#define SHPC_SEC_BUS_MASK 0x7
34 1dc324d2 Michael S. Tsirkin
#define SHPC_MSI_CTL      0x12 /* 1 byte */
35 1dc324d2 Michael S. Tsirkin
#define SHPC_PROG_IFC     0x13 /* 1 byte */
36 1dc324d2 Michael S. Tsirkin
#define SHPC_PROG_IFC_1_0 0x1
37 1dc324d2 Michael S. Tsirkin
#define SHPC_CMD_CODE     0x14 /* 1 byte */
38 1dc324d2 Michael S. Tsirkin
#define SHPC_CMD_TRGT     0x15 /* 1 byte */
39 1dc324d2 Michael S. Tsirkin
#define SHPC_CMD_TRGT_MIN 0x1
40 1dc324d2 Michael S. Tsirkin
#define SHPC_CMD_TRGT_MAX 0x1f
41 1dc324d2 Michael S. Tsirkin
#define SHPC_CMD_STATUS   0x16 /* 2 bytes */
42 1dc324d2 Michael S. Tsirkin
#define SHPC_CMD_STATUS_BUSY          0x1
43 1dc324d2 Michael S. Tsirkin
#define SHPC_CMD_STATUS_MRL_OPEN      0x2
44 1dc324d2 Michael S. Tsirkin
#define SHPC_CMD_STATUS_INVALID_CMD   0x4
45 1dc324d2 Michael S. Tsirkin
#define SHPC_CMD_STATUS_INVALID_MODE  0x8
46 1dc324d2 Michael S. Tsirkin
#define SHPC_INT_LOCATOR  0x18 /* 4 bytes */
47 1dc324d2 Michael S. Tsirkin
#define SHPC_INT_COMMAND  0x1
48 1dc324d2 Michael S. Tsirkin
#define SHPC_SERR_LOCATOR 0x1C /* 4 bytes */
49 1dc324d2 Michael S. Tsirkin
#define SHPC_SERR_INT     0x20 /* 4 bytes */
50 1dc324d2 Michael S. Tsirkin
#define SHPC_INT_DIS      0x1
51 1dc324d2 Michael S. Tsirkin
#define SHPC_SERR_DIS     0x2
52 1dc324d2 Michael S. Tsirkin
#define SHPC_CMD_INT_DIS  0x4
53 1dc324d2 Michael S. Tsirkin
#define SHPC_ARB_SERR_DIS 0x8
54 1dc324d2 Michael S. Tsirkin
#define SHPC_CMD_DETECTED 0x10000
55 1dc324d2 Michael S. Tsirkin
#define SHPC_ARB_DETECTED 0x20000
56 1dc324d2 Michael S. Tsirkin
 /* 4 bytes * slot # (start from 0) */
57 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_REG(s)         (0x24 + (s) * 4)
58 1dc324d2 Michael S. Tsirkin
 /* 2 bytes */
59 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_STATUS(s)       (0x0 + SHPC_SLOT_REG(s))
60 1dc324d2 Michael S. Tsirkin
61 1dc324d2 Michael S. Tsirkin
/* Same slot state masks are used for command and status registers */
62 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_STATE_MASK     0x03
63 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_STATE_SHIFT \
64 1dc324d2 Michael S. Tsirkin
    (ffs(SHPC_SLOT_STATE_MASK) - 1)
65 1dc324d2 Michael S. Tsirkin
66 1dc324d2 Michael S. Tsirkin
#define SHPC_STATE_NO       0x0
67 1dc324d2 Michael S. Tsirkin
#define SHPC_STATE_PWRONLY  0x1
68 1dc324d2 Michael S. Tsirkin
#define SHPC_STATE_ENABLED  0x2
69 1dc324d2 Michael S. Tsirkin
#define SHPC_STATE_DISABLED 0x3
70 1dc324d2 Michael S. Tsirkin
71 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_PWR_LED_MASK   0xC
72 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_PWR_LED_SHIFT \
73 1dc324d2 Michael S. Tsirkin
    (ffs(SHPC_SLOT_PWR_LED_MASK) - 1)
74 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_ATTN_LED_MASK  0x30
75 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_ATTN_LED_SHIFT \
76 1dc324d2 Michael S. Tsirkin
    (ffs(SHPC_SLOT_ATTN_LED_MASK) - 1)
77 1dc324d2 Michael S. Tsirkin
78 1dc324d2 Michael S. Tsirkin
#define SHPC_LED_NO     0x0
79 1dc324d2 Michael S. Tsirkin
#define SHPC_LED_ON     0x1
80 1dc324d2 Michael S. Tsirkin
#define SHPC_LED_BLINK  0x2
81 1dc324d2 Michael S. Tsirkin
#define SHPC_LED_OFF    0x3
82 1dc324d2 Michael S. Tsirkin
83 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_STATUS_PWR_FAULT      0x40
84 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_STATUS_BUTTON         0x80
85 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_STATUS_MRL_OPEN       0x100
86 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_STATUS_66             0x200
87 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_STATUS_PRSNT_MASK     0xC00
88 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_STATUS_PRSNT_EMPTY    0x3
89 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_STATUS_PRSNT_25W      0x1
90 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_STATUS_PRSNT_15W      0x2
91 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_STATUS_PRSNT_7_5W     0x0
92 1dc324d2 Michael S. Tsirkin
93 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_STATUS_PRSNT_PCIX     0x3000
94 1dc324d2 Michael S. Tsirkin
95 1dc324d2 Michael S. Tsirkin
96 1dc324d2 Michael S. Tsirkin
 /* 1 byte */
97 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_EVENT_LATCH(s)        (0x2 + SHPC_SLOT_REG(s))
98 1dc324d2 Michael S. Tsirkin
 /* 1 byte */
99 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_EVENT_SERR_INT_DIS(d, s) (0x3 + SHPC_SLOT_REG(s))
100 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_EVENT_PRESENCE        0x01
101 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_EVENT_ISOLATED_FAULT  0x02
102 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_EVENT_BUTTON          0x04
103 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_EVENT_MRL             0x08
104 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_EVENT_CONNECTED_FAULT 0x10
105 1dc324d2 Michael S. Tsirkin
/* Bits below are used for Serr/Int disable only */
106 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_EVENT_MRL_SERR_DIS    0x20
107 1dc324d2 Michael S. Tsirkin
#define SHPC_SLOT_EVENT_CONNECTED_FAULT_SERR_DIS 0x40
108 1dc324d2 Michael S. Tsirkin
109 1dc324d2 Michael S. Tsirkin
#define SHPC_MIN_SLOTS        1
110 1dc324d2 Michael S. Tsirkin
#define SHPC_MAX_SLOTS        31
111 1dc324d2 Michael S. Tsirkin
#define SHPC_SIZEOF(d)    SHPC_SLOT_REG((d)->shpc->nslots)
112 1dc324d2 Michael S. Tsirkin
113 1dc324d2 Michael S. Tsirkin
/* SHPC Slot identifiers */
114 1dc324d2 Michael S. Tsirkin
115 1dc324d2 Michael S. Tsirkin
/* Hotplug supported at 31 slots out of the total 32.  We reserve slot 0,
116 1dc324d2 Michael S. Tsirkin
   and give the rest of them physical *and* pci numbers starting from 1, so
117 1dc324d2 Michael S. Tsirkin
   they match logical numbers.  Note: this means that multiple slots must have
118 1dc324d2 Michael S. Tsirkin
   different chassis number values, to make chassis+physical slot unique.
119 1dc324d2 Michael S. Tsirkin
   TODO: make this configurable? */
120 1dc324d2 Michael S. Tsirkin
#define SHPC_IDX_TO_LOGICAL(slot) ((slot) + 1)
121 1dc324d2 Michael S. Tsirkin
#define SHPC_LOGICAL_TO_IDX(target) ((target) - 1)
122 1dc324d2 Michael S. Tsirkin
#define SHPC_IDX_TO_PCI(slot) ((slot) + 1)
123 1dc324d2 Michael S. Tsirkin
#define SHPC_PCI_TO_IDX(pci_slot) ((pci_slot) - 1)
124 1dc324d2 Michael S. Tsirkin
#define SHPC_IDX_TO_PHYSICAL(slot) ((slot) + 1)
125 1dc324d2 Michael S. Tsirkin
126 1dc324d2 Michael S. Tsirkin
static int roundup_pow_of_two(int x)
127 1dc324d2 Michael S. Tsirkin
{
128 1dc324d2 Michael S. Tsirkin
    x |= (x >> 1);
129 1dc324d2 Michael S. Tsirkin
    x |= (x >> 2);
130 1dc324d2 Michael S. Tsirkin
    x |= (x >> 4);
131 1dc324d2 Michael S. Tsirkin
    x |= (x >> 8);
132 1dc324d2 Michael S. Tsirkin
    x |= (x >> 16);
133 1dc324d2 Michael S. Tsirkin
    return x + 1;
134 1dc324d2 Michael S. Tsirkin
}
135 1dc324d2 Michael S. Tsirkin
136 1dc324d2 Michael S. Tsirkin
static uint16_t shpc_get_status(SHPCDevice *shpc, int slot, uint16_t msk)
137 1dc324d2 Michael S. Tsirkin
{
138 1dc324d2 Michael S. Tsirkin
    uint8_t *status = shpc->config + SHPC_SLOT_STATUS(slot);
139 1dc324d2 Michael S. Tsirkin
    return (pci_get_word(status) & msk) >> (ffs(msk) - 1);
140 1dc324d2 Michael S. Tsirkin
}
141 1dc324d2 Michael S. Tsirkin
142 1dc324d2 Michael S. Tsirkin
static void shpc_set_status(SHPCDevice *shpc,
143 1dc324d2 Michael S. Tsirkin
                            int slot, uint8_t value, uint16_t msk)
144 1dc324d2 Michael S. Tsirkin
{
145 1dc324d2 Michael S. Tsirkin
    uint8_t *status = shpc->config + SHPC_SLOT_STATUS(slot);
146 1dc324d2 Michael S. Tsirkin
    pci_word_test_and_clear_mask(status, msk);
147 1dc324d2 Michael S. Tsirkin
    pci_word_test_and_set_mask(status, value << (ffs(msk) - 1));
148 1dc324d2 Michael S. Tsirkin
}
149 1dc324d2 Michael S. Tsirkin
150 1dc324d2 Michael S. Tsirkin
static void shpc_interrupt_update(PCIDevice *d)
151 1dc324d2 Michael S. Tsirkin
{
152 1dc324d2 Michael S. Tsirkin
    SHPCDevice *shpc = d->shpc;
153 1dc324d2 Michael S. Tsirkin
    int slot;
154 1dc324d2 Michael S. Tsirkin
    int level = 0;
155 1dc324d2 Michael S. Tsirkin
    uint32_t serr_int;
156 1dc324d2 Michael S. Tsirkin
    uint32_t int_locator = 0;
157 1dc324d2 Michael S. Tsirkin
158 1dc324d2 Michael S. Tsirkin
    /* Update interrupt locator register */
159 1dc324d2 Michael S. Tsirkin
    for (slot = 0; slot < shpc->nslots; ++slot) {
160 1dc324d2 Michael S. Tsirkin
        uint8_t event = shpc->config[SHPC_SLOT_EVENT_LATCH(slot)];
161 1dc324d2 Michael S. Tsirkin
        uint8_t disable = shpc->config[SHPC_SLOT_EVENT_SERR_INT_DIS(d, slot)];
162 1dc324d2 Michael S. Tsirkin
        uint32_t mask = 1 << SHPC_IDX_TO_LOGICAL(slot);
163 1dc324d2 Michael S. Tsirkin
        if (event & ~disable) {
164 1dc324d2 Michael S. Tsirkin
            int_locator |= mask;
165 1dc324d2 Michael S. Tsirkin
        }
166 1dc324d2 Michael S. Tsirkin
    }
167 1dc324d2 Michael S. Tsirkin
    serr_int = pci_get_long(shpc->config + SHPC_SERR_INT);
168 1dc324d2 Michael S. Tsirkin
    if ((serr_int & SHPC_CMD_DETECTED) && !(serr_int & SHPC_CMD_INT_DIS)) {
169 1dc324d2 Michael S. Tsirkin
        int_locator |= SHPC_INT_COMMAND;
170 1dc324d2 Michael S. Tsirkin
    }
171 1dc324d2 Michael S. Tsirkin
    pci_set_long(shpc->config + SHPC_INT_LOCATOR, int_locator);
172 1dc324d2 Michael S. Tsirkin
    level = (!(serr_int & SHPC_INT_DIS) && int_locator) ? 1 : 0;
173 1dc324d2 Michael S. Tsirkin
    if (msi_enabled(d) && shpc->msi_requested != level)
174 1dc324d2 Michael S. Tsirkin
        msi_notify(d, 0);
175 1dc324d2 Michael S. Tsirkin
    else
176 9e64f8a3 Marcel Apfelbaum
        pci_set_irq(d, level);
177 1dc324d2 Michael S. Tsirkin
    shpc->msi_requested = level;
178 1dc324d2 Michael S. Tsirkin
}
179 1dc324d2 Michael S. Tsirkin
180 1dc324d2 Michael S. Tsirkin
static void shpc_set_sec_bus_speed(SHPCDevice *shpc, uint8_t speed)
181 1dc324d2 Michael S. Tsirkin
{
182 1dc324d2 Michael S. Tsirkin
    switch (speed) {
183 1dc324d2 Michael S. Tsirkin
    case SHPC_SEC_BUS_33:
184 1dc324d2 Michael S. Tsirkin
        shpc->config[SHPC_SEC_BUS] &= ~SHPC_SEC_BUS_MASK;
185 1dc324d2 Michael S. Tsirkin
        shpc->config[SHPC_SEC_BUS] |= speed;
186 1dc324d2 Michael S. Tsirkin
        break;
187 1dc324d2 Michael S. Tsirkin
    default:
188 1dc324d2 Michael S. Tsirkin
        pci_word_test_and_set_mask(shpc->config + SHPC_CMD_STATUS,
189 1dc324d2 Michael S. Tsirkin
                                   SHPC_CMD_STATUS_INVALID_MODE);
190 1dc324d2 Michael S. Tsirkin
    }
191 1dc324d2 Michael S. Tsirkin
}
192 1dc324d2 Michael S. Tsirkin
193 1dc324d2 Michael S. Tsirkin
void shpc_reset(PCIDevice *d)
194 1dc324d2 Michael S. Tsirkin
{
195 1dc324d2 Michael S. Tsirkin
    SHPCDevice *shpc = d->shpc;
196 1dc324d2 Michael S. Tsirkin
    int nslots = shpc->nslots;
197 1dc324d2 Michael S. Tsirkin
    int i;
198 1dc324d2 Michael S. Tsirkin
    memset(shpc->config, 0, SHPC_SIZEOF(d));
199 1dc324d2 Michael S. Tsirkin
    pci_set_byte(shpc->config + SHPC_NSLOTS, nslots);
200 1dc324d2 Michael S. Tsirkin
    pci_set_long(shpc->config + SHPC_SLOTS_33, nslots);
201 1dc324d2 Michael S. Tsirkin
    pci_set_long(shpc->config + SHPC_SLOTS_66, 0);
202 1dc324d2 Michael S. Tsirkin
    pci_set_byte(shpc->config + SHPC_FIRST_DEV, SHPC_IDX_TO_PCI(0));
203 1dc324d2 Michael S. Tsirkin
    pci_set_word(shpc->config + SHPC_PHYS_SLOT,
204 1dc324d2 Michael S. Tsirkin
                 SHPC_IDX_TO_PHYSICAL(0) |
205 1dc324d2 Michael S. Tsirkin
                 SHPC_PHYS_NUM_UP |
206 1dc324d2 Michael S. Tsirkin
                 SHPC_PHYS_MRL |
207 1dc324d2 Michael S. Tsirkin
                 SHPC_PHYS_BUTTON);
208 1dc324d2 Michael S. Tsirkin
    pci_set_long(shpc->config + SHPC_SERR_INT, SHPC_INT_DIS |
209 1dc324d2 Michael S. Tsirkin
                 SHPC_SERR_DIS |
210 1dc324d2 Michael S. Tsirkin
                 SHPC_CMD_INT_DIS |
211 1dc324d2 Michael S. Tsirkin
                 SHPC_ARB_SERR_DIS);
212 1dc324d2 Michael S. Tsirkin
    pci_set_byte(shpc->config + SHPC_PROG_IFC, SHPC_PROG_IFC_1_0);
213 1dc324d2 Michael S. Tsirkin
    pci_set_word(shpc->config + SHPC_SEC_BUS, SHPC_SEC_BUS_33);
214 1dc324d2 Michael S. Tsirkin
    for (i = 0; i < shpc->nslots; ++i) {
215 1dc324d2 Michael S. Tsirkin
        pci_set_byte(shpc->config + SHPC_SLOT_EVENT_SERR_INT_DIS(d, i),
216 1dc324d2 Michael S. Tsirkin
                     SHPC_SLOT_EVENT_PRESENCE |
217 1dc324d2 Michael S. Tsirkin
                     SHPC_SLOT_EVENT_ISOLATED_FAULT |
218 1dc324d2 Michael S. Tsirkin
                     SHPC_SLOT_EVENT_BUTTON |
219 1dc324d2 Michael S. Tsirkin
                     SHPC_SLOT_EVENT_MRL |
220 1dc324d2 Michael S. Tsirkin
                     SHPC_SLOT_EVENT_CONNECTED_FAULT |
221 1dc324d2 Michael S. Tsirkin
                     SHPC_SLOT_EVENT_MRL_SERR_DIS |
222 1dc324d2 Michael S. Tsirkin
                     SHPC_SLOT_EVENT_CONNECTED_FAULT_SERR_DIS);
223 1dc324d2 Michael S. Tsirkin
        if (shpc->sec_bus->devices[PCI_DEVFN(SHPC_IDX_TO_PCI(i), 0)]) {
224 1dc324d2 Michael S. Tsirkin
            shpc_set_status(shpc, i, SHPC_STATE_ENABLED, SHPC_SLOT_STATE_MASK);
225 1dc324d2 Michael S. Tsirkin
            shpc_set_status(shpc, i, 0, SHPC_SLOT_STATUS_MRL_OPEN);
226 1dc324d2 Michael S. Tsirkin
            shpc_set_status(shpc, i, SHPC_SLOT_STATUS_PRSNT_7_5W,
227 1dc324d2 Michael S. Tsirkin
                            SHPC_SLOT_STATUS_PRSNT_MASK);
228 1dc324d2 Michael S. Tsirkin
            shpc_set_status(shpc, i, SHPC_LED_ON, SHPC_SLOT_PWR_LED_MASK);
229 1dc324d2 Michael S. Tsirkin
        } else {
230 1dc324d2 Michael S. Tsirkin
            shpc_set_status(shpc, i, SHPC_STATE_DISABLED, SHPC_SLOT_STATE_MASK);
231 1dc324d2 Michael S. Tsirkin
            shpc_set_status(shpc, i, 1, SHPC_SLOT_STATUS_MRL_OPEN);
232 1dc324d2 Michael S. Tsirkin
            shpc_set_status(shpc, i, SHPC_SLOT_STATUS_PRSNT_EMPTY,
233 1dc324d2 Michael S. Tsirkin
                            SHPC_SLOT_STATUS_PRSNT_MASK);
234 1dc324d2 Michael S. Tsirkin
            shpc_set_status(shpc, i, SHPC_LED_OFF, SHPC_SLOT_PWR_LED_MASK);
235 1dc324d2 Michael S. Tsirkin
        }
236 1dc324d2 Michael S. Tsirkin
        shpc_set_status(shpc, i, 0, SHPC_SLOT_STATUS_66);
237 1dc324d2 Michael S. Tsirkin
    }
238 1dc324d2 Michael S. Tsirkin
    shpc_set_sec_bus_speed(shpc, SHPC_SEC_BUS_33);
239 1dc324d2 Michael S. Tsirkin
    shpc->msi_requested = 0;
240 1dc324d2 Michael S. Tsirkin
    shpc_interrupt_update(d);
241 1dc324d2 Michael S. Tsirkin
}
242 1dc324d2 Michael S. Tsirkin
243 1dc324d2 Michael S. Tsirkin
static void shpc_invalid_command(SHPCDevice *shpc)
244 1dc324d2 Michael S. Tsirkin
{
245 1dc324d2 Michael S. Tsirkin
    pci_word_test_and_set_mask(shpc->config + SHPC_CMD_STATUS,
246 1dc324d2 Michael S. Tsirkin
                               SHPC_CMD_STATUS_INVALID_CMD);
247 1dc324d2 Michael S. Tsirkin
}
248 1dc324d2 Michael S. Tsirkin
249 1dc324d2 Michael S. Tsirkin
static void shpc_free_devices_in_slot(SHPCDevice *shpc, int slot)
250 1dc324d2 Michael S. Tsirkin
{
251 1dc324d2 Michael S. Tsirkin
    int devfn;
252 1dc324d2 Michael S. Tsirkin
    int pci_slot = SHPC_IDX_TO_PCI(slot);
253 1dc324d2 Michael S. Tsirkin
    for (devfn = PCI_DEVFN(pci_slot, 0);
254 1dc324d2 Michael S. Tsirkin
         devfn <= PCI_DEVFN(pci_slot, PCI_FUNC_MAX - 1);
255 1dc324d2 Michael S. Tsirkin
         ++devfn) {
256 1dc324d2 Michael S. Tsirkin
        PCIDevice *affected_dev = shpc->sec_bus->devices[devfn];
257 1dc324d2 Michael S. Tsirkin
        if (affected_dev) {
258 02a5c4c9 Stefan Hajnoczi
            object_unparent(OBJECT(affected_dev));
259 1dc324d2 Michael S. Tsirkin
        }
260 1dc324d2 Michael S. Tsirkin
    }
261 1dc324d2 Michael S. Tsirkin
}
262 1dc324d2 Michael S. Tsirkin
263 1dc324d2 Michael S. Tsirkin
static void shpc_slot_command(SHPCDevice *shpc, uint8_t target,
264 1dc324d2 Michael S. Tsirkin
                              uint8_t state, uint8_t power, uint8_t attn)
265 1dc324d2 Michael S. Tsirkin
{
266 1dc324d2 Michael S. Tsirkin
    uint8_t current_state;
267 1dc324d2 Michael S. Tsirkin
    int slot = SHPC_LOGICAL_TO_IDX(target);
268 1dc324d2 Michael S. Tsirkin
    if (target < SHPC_CMD_TRGT_MIN || slot >= shpc->nslots) {
269 1dc324d2 Michael S. Tsirkin
        shpc_invalid_command(shpc);
270 1dc324d2 Michael S. Tsirkin
        return;
271 1dc324d2 Michael S. Tsirkin
    }
272 1dc324d2 Michael S. Tsirkin
    current_state = shpc_get_status(shpc, slot, SHPC_SLOT_STATE_MASK);
273 1dc324d2 Michael S. Tsirkin
    if (current_state == SHPC_STATE_ENABLED && state == SHPC_STATE_PWRONLY) {
274 1dc324d2 Michael S. Tsirkin
        shpc_invalid_command(shpc);
275 1dc324d2 Michael S. Tsirkin
        return;
276 1dc324d2 Michael S. Tsirkin
    }
277 1dc324d2 Michael S. Tsirkin
278 1dc324d2 Michael S. Tsirkin
    switch (power) {
279 1dc324d2 Michael S. Tsirkin
    case SHPC_LED_NO:
280 1dc324d2 Michael S. Tsirkin
        break;
281 1dc324d2 Michael S. Tsirkin
    default:
282 1dc324d2 Michael S. Tsirkin
        /* TODO: send event to monitor */
283 1dc324d2 Michael S. Tsirkin
        shpc_set_status(shpc, slot, power, SHPC_SLOT_PWR_LED_MASK);
284 1dc324d2 Michael S. Tsirkin
    }
285 1dc324d2 Michael S. Tsirkin
    switch (attn) {
286 1dc324d2 Michael S. Tsirkin
    case SHPC_LED_NO:
287 1dc324d2 Michael S. Tsirkin
        break;
288 1dc324d2 Michael S. Tsirkin
    default:
289 1dc324d2 Michael S. Tsirkin
        /* TODO: send event to monitor */
290 1dc324d2 Michael S. Tsirkin
        shpc_set_status(shpc, slot, attn, SHPC_SLOT_ATTN_LED_MASK);
291 1dc324d2 Michael S. Tsirkin
    }
292 1dc324d2 Michael S. Tsirkin
293 1dc324d2 Michael S. Tsirkin
    if ((current_state == SHPC_STATE_DISABLED && state == SHPC_STATE_PWRONLY) ||
294 1dc324d2 Michael S. Tsirkin
        (current_state == SHPC_STATE_DISABLED && state == SHPC_STATE_ENABLED)) {
295 1dc324d2 Michael S. Tsirkin
        shpc_set_status(shpc, slot, state, SHPC_SLOT_STATE_MASK);
296 1dc324d2 Michael S. Tsirkin
    } else if ((current_state == SHPC_STATE_ENABLED ||
297 1dc324d2 Michael S. Tsirkin
                current_state == SHPC_STATE_PWRONLY) &&
298 1dc324d2 Michael S. Tsirkin
               state == SHPC_STATE_DISABLED) {
299 1dc324d2 Michael S. Tsirkin
        shpc_set_status(shpc, slot, state, SHPC_SLOT_STATE_MASK);
300 1dc324d2 Michael S. Tsirkin
        power = shpc_get_status(shpc, slot, SHPC_SLOT_PWR_LED_MASK);
301 1dc324d2 Michael S. Tsirkin
        /* TODO: track what monitor requested. */
302 1dc324d2 Michael S. Tsirkin
        /* Look at LED to figure out whether it's ok to remove the device. */
303 1dc324d2 Michael S. Tsirkin
        if (power == SHPC_LED_OFF) {
304 1dc324d2 Michael S. Tsirkin
            shpc_free_devices_in_slot(shpc, slot);
305 1dc324d2 Michael S. Tsirkin
            shpc_set_status(shpc, slot, 1, SHPC_SLOT_STATUS_MRL_OPEN);
306 1dc324d2 Michael S. Tsirkin
            shpc_set_status(shpc, slot, SHPC_SLOT_STATUS_PRSNT_EMPTY,
307 1dc324d2 Michael S. Tsirkin
                            SHPC_SLOT_STATUS_PRSNT_MASK);
308 1dc324d2 Michael S. Tsirkin
            shpc->config[SHPC_SLOT_EVENT_LATCH(slot)] |=
309 1dc324d2 Michael S. Tsirkin
                SHPC_SLOT_EVENT_BUTTON |
310 1dc324d2 Michael S. Tsirkin
                SHPC_SLOT_EVENT_MRL |
311 1dc324d2 Michael S. Tsirkin
                SHPC_SLOT_EVENT_PRESENCE;
312 1dc324d2 Michael S. Tsirkin
        }
313 1dc324d2 Michael S. Tsirkin
    }
314 1dc324d2 Michael S. Tsirkin
}
315 1dc324d2 Michael S. Tsirkin
316 1dc324d2 Michael S. Tsirkin
static void shpc_command(SHPCDevice *shpc)
317 1dc324d2 Michael S. Tsirkin
{
318 1dc324d2 Michael S. Tsirkin
    uint8_t code = pci_get_byte(shpc->config + SHPC_CMD_CODE);
319 1dc324d2 Michael S. Tsirkin
    uint8_t speed;
320 1dc324d2 Michael S. Tsirkin
    uint8_t target;
321 1dc324d2 Michael S. Tsirkin
    uint8_t attn;
322 1dc324d2 Michael S. Tsirkin
    uint8_t power;
323 1dc324d2 Michael S. Tsirkin
    uint8_t state;
324 1dc324d2 Michael S. Tsirkin
    int i;
325 1dc324d2 Michael S. Tsirkin
326 1dc324d2 Michael S. Tsirkin
    /* Clear status from the previous command. */
327 1dc324d2 Michael S. Tsirkin
    pci_word_test_and_clear_mask(shpc->config + SHPC_CMD_STATUS,
328 1dc324d2 Michael S. Tsirkin
                                 SHPC_CMD_STATUS_BUSY |
329 1dc324d2 Michael S. Tsirkin
                                 SHPC_CMD_STATUS_MRL_OPEN |
330 1dc324d2 Michael S. Tsirkin
                                 SHPC_CMD_STATUS_INVALID_CMD |
331 1dc324d2 Michael S. Tsirkin
                                 SHPC_CMD_STATUS_INVALID_MODE);
332 1dc324d2 Michael S. Tsirkin
    switch (code) {
333 1dc324d2 Michael S. Tsirkin
    case 0x00 ... 0x3f:
334 1dc324d2 Michael S. Tsirkin
        target = shpc->config[SHPC_CMD_TRGT] & SHPC_CMD_TRGT_MAX;
335 1dc324d2 Michael S. Tsirkin
        state = (code & SHPC_SLOT_STATE_MASK) >> SHPC_SLOT_STATE_SHIFT;
336 1dc324d2 Michael S. Tsirkin
        power = (code & SHPC_SLOT_PWR_LED_MASK) >> SHPC_SLOT_PWR_LED_SHIFT;
337 1dc324d2 Michael S. Tsirkin
        attn = (code & SHPC_SLOT_ATTN_LED_MASK) >> SHPC_SLOT_ATTN_LED_SHIFT;
338 1dc324d2 Michael S. Tsirkin
        shpc_slot_command(shpc, target, state, power, attn);
339 1dc324d2 Michael S. Tsirkin
        break;
340 1dc324d2 Michael S. Tsirkin
    case 0x40 ... 0x47:
341 1dc324d2 Michael S. Tsirkin
        speed = code & SHPC_SEC_BUS_MASK;
342 1dc324d2 Michael S. Tsirkin
        shpc_set_sec_bus_speed(shpc, speed);
343 1dc324d2 Michael S. Tsirkin
        break;
344 1dc324d2 Michael S. Tsirkin
    case 0x48:
345 1dc324d2 Michael S. Tsirkin
        /* Power only all slots */
346 1dc324d2 Michael S. Tsirkin
        /* first verify no slots are enabled */
347 1dc324d2 Michael S. Tsirkin
        for (i = 0; i < shpc->nslots; ++i) {
348 1dc324d2 Michael S. Tsirkin
            state = shpc_get_status(shpc, i, SHPC_SLOT_STATE_MASK);
349 1dc324d2 Michael S. Tsirkin
            if (state == SHPC_STATE_ENABLED) {
350 1dc324d2 Michael S. Tsirkin
                shpc_invalid_command(shpc);
351 1dc324d2 Michael S. Tsirkin
                goto done;
352 1dc324d2 Michael S. Tsirkin
            }
353 1dc324d2 Michael S. Tsirkin
        }
354 1dc324d2 Michael S. Tsirkin
        for (i = 0; i < shpc->nslots; ++i) {
355 1dc324d2 Michael S. Tsirkin
            if (!(shpc_get_status(shpc, i, SHPC_SLOT_STATUS_MRL_OPEN))) {
356 1dc324d2 Michael S. Tsirkin
                shpc_slot_command(shpc, i + SHPC_CMD_TRGT_MIN,
357 1dc324d2 Michael S. Tsirkin
                                  SHPC_STATE_PWRONLY, SHPC_LED_ON, SHPC_LED_NO);
358 1dc324d2 Michael S. Tsirkin
            } else {
359 1dc324d2 Michael S. Tsirkin
                shpc_slot_command(shpc, i + SHPC_CMD_TRGT_MIN,
360 1dc324d2 Michael S. Tsirkin
                                  SHPC_STATE_NO, SHPC_LED_OFF, SHPC_LED_NO);
361 1dc324d2 Michael S. Tsirkin
            }
362 1dc324d2 Michael S. Tsirkin
        }
363 1dc324d2 Michael S. Tsirkin
        break;
364 1dc324d2 Michael S. Tsirkin
    case 0x49:
365 1dc324d2 Michael S. Tsirkin
        /* Enable all slots */
366 1dc324d2 Michael S. Tsirkin
        /* TODO: Spec says this shall fail if some are already enabled.
367 1dc324d2 Michael S. Tsirkin
         * This doesn't make sense - why not? a spec bug? */
368 1dc324d2 Michael S. Tsirkin
        for (i = 0; i < shpc->nslots; ++i) {
369 1dc324d2 Michael S. Tsirkin
            state = shpc_get_status(shpc, i, SHPC_SLOT_STATE_MASK);
370 1dc324d2 Michael S. Tsirkin
            if (state == SHPC_STATE_ENABLED) {
371 1dc324d2 Michael S. Tsirkin
                shpc_invalid_command(shpc);
372 1dc324d2 Michael S. Tsirkin
                goto done;
373 1dc324d2 Michael S. Tsirkin
            }
374 1dc324d2 Michael S. Tsirkin
        }
375 1dc324d2 Michael S. Tsirkin
        for (i = 0; i < shpc->nslots; ++i) {
376 1dc324d2 Michael S. Tsirkin
            if (!(shpc_get_status(shpc, i, SHPC_SLOT_STATUS_MRL_OPEN))) {
377 1dc324d2 Michael S. Tsirkin
                shpc_slot_command(shpc, i + SHPC_CMD_TRGT_MIN,
378 1dc324d2 Michael S. Tsirkin
                                  SHPC_STATE_ENABLED, SHPC_LED_ON, SHPC_LED_NO);
379 1dc324d2 Michael S. Tsirkin
            } else {
380 1dc324d2 Michael S. Tsirkin
                shpc_slot_command(shpc, i + SHPC_CMD_TRGT_MIN,
381 1dc324d2 Michael S. Tsirkin
                                  SHPC_STATE_NO, SHPC_LED_OFF, SHPC_LED_NO);
382 1dc324d2 Michael S. Tsirkin
            }
383 1dc324d2 Michael S. Tsirkin
        }
384 1dc324d2 Michael S. Tsirkin
        break;
385 1dc324d2 Michael S. Tsirkin
    default:
386 1dc324d2 Michael S. Tsirkin
        shpc_invalid_command(shpc);
387 1dc324d2 Michael S. Tsirkin
        break;
388 1dc324d2 Michael S. Tsirkin
    }
389 1dc324d2 Michael S. Tsirkin
done:
390 1dc324d2 Michael S. Tsirkin
    pci_long_test_and_set_mask(shpc->config + SHPC_SERR_INT, SHPC_CMD_DETECTED);
391 1dc324d2 Michael S. Tsirkin
}
392 1dc324d2 Michael S. Tsirkin
393 1dc324d2 Michael S. Tsirkin
static void shpc_write(PCIDevice *d, unsigned addr, uint64_t val, int l)
394 1dc324d2 Michael S. Tsirkin
{
395 1dc324d2 Michael S. Tsirkin
    SHPCDevice *shpc = d->shpc;
396 1dc324d2 Michael S. Tsirkin
    int i;
397 1dc324d2 Michael S. Tsirkin
    if (addr >= SHPC_SIZEOF(d)) {
398 1dc324d2 Michael S. Tsirkin
        return;
399 1dc324d2 Michael S. Tsirkin
    }
400 1dc324d2 Michael S. Tsirkin
    l = MIN(l, SHPC_SIZEOF(d) - addr);
401 1dc324d2 Michael S. Tsirkin
402 1dc324d2 Michael S. Tsirkin
    /* TODO: code duplicated from pci.c */
403 1dc324d2 Michael S. Tsirkin
    for (i = 0; i < l; val >>= 8, ++i) {
404 1dc324d2 Michael S. Tsirkin
        unsigned a = addr + i;
405 1dc324d2 Michael S. Tsirkin
        uint8_t wmask = shpc->wmask[a];
406 1dc324d2 Michael S. Tsirkin
        uint8_t w1cmask = shpc->w1cmask[a];
407 1dc324d2 Michael S. Tsirkin
        assert(!(wmask & w1cmask));
408 1dc324d2 Michael S. Tsirkin
        shpc->config[a] = (shpc->config[a] & ~wmask) | (val & wmask);
409 1dc324d2 Michael S. Tsirkin
        shpc->config[a] &= ~(val & w1cmask); /* W1C: Write 1 to Clear */
410 1dc324d2 Michael S. Tsirkin
    }
411 1dc324d2 Michael S. Tsirkin
    if (ranges_overlap(addr, l, SHPC_CMD_CODE, 2)) {
412 1dc324d2 Michael S. Tsirkin
        shpc_command(shpc);
413 1dc324d2 Michael S. Tsirkin
    }
414 1dc324d2 Michael S. Tsirkin
    shpc_interrupt_update(d);
415 1dc324d2 Michael S. Tsirkin
}
416 1dc324d2 Michael S. Tsirkin
417 1dc324d2 Michael S. Tsirkin
static uint64_t shpc_read(PCIDevice *d, unsigned addr, int l)
418 1dc324d2 Michael S. Tsirkin
{
419 1dc324d2 Michael S. Tsirkin
    uint64_t val = 0x0;
420 1dc324d2 Michael S. Tsirkin
    if (addr >= SHPC_SIZEOF(d)) {
421 1dc324d2 Michael S. Tsirkin
        return val;
422 1dc324d2 Michael S. Tsirkin
    }
423 1dc324d2 Michael S. Tsirkin
    l = MIN(l, SHPC_SIZEOF(d) - addr);
424 1dc324d2 Michael S. Tsirkin
    memcpy(&val, d->shpc->config + addr, l);
425 1dc324d2 Michael S. Tsirkin
    return val;
426 1dc324d2 Michael S. Tsirkin
}
427 1dc324d2 Michael S. Tsirkin
428 1dc324d2 Michael S. Tsirkin
/* SHPC Bridge Capability */
429 1dc324d2 Michael S. Tsirkin
#define SHPC_CAP_LENGTH 0x08
430 1dc324d2 Michael S. Tsirkin
#define SHPC_CAP_DWORD_SELECT 0x2 /* 1 byte */
431 1dc324d2 Michael S. Tsirkin
#define SHPC_CAP_CxP 0x3 /* 1 byte: CSP, CIP */
432 1dc324d2 Michael S. Tsirkin
#define SHPC_CAP_DWORD_DATA 0x4 /* 4 bytes */
433 1dc324d2 Michael S. Tsirkin
#define SHPC_CAP_CSP_MASK 0x4
434 1dc324d2 Michael S. Tsirkin
#define SHPC_CAP_CIP_MASK 0x8
435 1dc324d2 Michael S. Tsirkin
436 1dc324d2 Michael S. Tsirkin
static uint8_t shpc_cap_dword(PCIDevice *d)
437 1dc324d2 Michael S. Tsirkin
{
438 1dc324d2 Michael S. Tsirkin
    return pci_get_byte(d->config + d->shpc->cap + SHPC_CAP_DWORD_SELECT);
439 1dc324d2 Michael S. Tsirkin
}
440 1dc324d2 Michael S. Tsirkin
441 1dc324d2 Michael S. Tsirkin
/* Update dword data capability register */
442 1dc324d2 Michael S. Tsirkin
static void shpc_cap_update_dword(PCIDevice *d)
443 1dc324d2 Michael S. Tsirkin
{
444 1dc324d2 Michael S. Tsirkin
    unsigned data;
445 1dc324d2 Michael S. Tsirkin
    data = shpc_read(d, shpc_cap_dword(d) * 4, 4);
446 1dc324d2 Michael S. Tsirkin
    pci_set_long(d->config  + d->shpc->cap + SHPC_CAP_DWORD_DATA, data);
447 1dc324d2 Michael S. Tsirkin
}
448 1dc324d2 Michael S. Tsirkin
449 1dc324d2 Michael S. Tsirkin
/* Add SHPC capability to the config space for the device. */
450 1dc324d2 Michael S. Tsirkin
static int shpc_cap_add_config(PCIDevice *d)
451 1dc324d2 Michael S. Tsirkin
{
452 1dc324d2 Michael S. Tsirkin
    uint8_t *config;
453 1dc324d2 Michael S. Tsirkin
    int config_offset;
454 1dc324d2 Michael S. Tsirkin
    config_offset = pci_add_capability(d, PCI_CAP_ID_SHPC,
455 1dc324d2 Michael S. Tsirkin
                                       0, SHPC_CAP_LENGTH);
456 1dc324d2 Michael S. Tsirkin
    if (config_offset < 0) {
457 1dc324d2 Michael S. Tsirkin
        return config_offset;
458 1dc324d2 Michael S. Tsirkin
    }
459 1dc324d2 Michael S. Tsirkin
    config = d->config + config_offset;
460 1dc324d2 Michael S. Tsirkin
461 1dc324d2 Michael S. Tsirkin
    pci_set_byte(config + SHPC_CAP_DWORD_SELECT, 0);
462 1dc324d2 Michael S. Tsirkin
    pci_set_byte(config + SHPC_CAP_CxP, 0);
463 1dc324d2 Michael S. Tsirkin
    pci_set_long(config + SHPC_CAP_DWORD_DATA, 0);
464 1dc324d2 Michael S. Tsirkin
    d->shpc->cap = config_offset;
465 1dc324d2 Michael S. Tsirkin
    /* Make dword select and data writeable. */
466 1dc324d2 Michael S. Tsirkin
    pci_set_byte(d->wmask + config_offset + SHPC_CAP_DWORD_SELECT, 0xff);
467 1dc324d2 Michael S. Tsirkin
    pci_set_long(d->wmask + config_offset + SHPC_CAP_DWORD_DATA, 0xffffffff);
468 1dc324d2 Michael S. Tsirkin
    return 0;
469 1dc324d2 Michael S. Tsirkin
}
470 1dc324d2 Michael S. Tsirkin
471 a8170e5e Avi Kivity
static uint64_t shpc_mmio_read(void *opaque, hwaddr addr,
472 1dc324d2 Michael S. Tsirkin
                               unsigned size)
473 1dc324d2 Michael S. Tsirkin
{
474 1dc324d2 Michael S. Tsirkin
    return shpc_read(opaque, addr, size);
475 1dc324d2 Michael S. Tsirkin
}
476 1dc324d2 Michael S. Tsirkin
477 a8170e5e Avi Kivity
static void shpc_mmio_write(void *opaque, hwaddr addr,
478 1dc324d2 Michael S. Tsirkin
                            uint64_t val, unsigned size)
479 1dc324d2 Michael S. Tsirkin
{
480 1dc324d2 Michael S. Tsirkin
    shpc_write(opaque, addr, val, size);
481 1dc324d2 Michael S. Tsirkin
}
482 1dc324d2 Michael S. Tsirkin
483 1dc324d2 Michael S. Tsirkin
static const MemoryRegionOps shpc_mmio_ops = {
484 1dc324d2 Michael S. Tsirkin
    .read = shpc_mmio_read,
485 1dc324d2 Michael S. Tsirkin
    .write = shpc_mmio_write,
486 1dc324d2 Michael S. Tsirkin
    .endianness = DEVICE_LITTLE_ENDIAN,
487 1dc324d2 Michael S. Tsirkin
    .valid = {
488 1dc324d2 Michael S. Tsirkin
        /* SHPC ECN requires dword accesses, but the original 1.0 spec doesn't.
489 1dc324d2 Michael S. Tsirkin
         * It's easier to suppport all sizes than worry about it. */
490 1dc324d2 Michael S. Tsirkin
        .min_access_size = 1,
491 1dc324d2 Michael S. Tsirkin
        .max_access_size = 4,
492 1dc324d2 Michael S. Tsirkin
    },
493 1dc324d2 Michael S. Tsirkin
};
494 5d268704 Igor Mammedov
static void shpc_device_hotplug_common(PCIDevice *affected_dev, int *slot,
495 5d268704 Igor Mammedov
                                       SHPCDevice *shpc, Error **errp)
496 1dc324d2 Michael S. Tsirkin
{
497 1dc324d2 Michael S. Tsirkin
    int pci_slot = PCI_SLOT(affected_dev->devfn);
498 5d268704 Igor Mammedov
    *slot = SHPC_PCI_TO_IDX(pci_slot);
499 5d268704 Igor Mammedov
500 5d268704 Igor Mammedov
    if (pci_slot < SHPC_IDX_TO_PCI(0) || *slot >= shpc->nslots) {
501 5d268704 Igor Mammedov
        error_setg(errp, "Unsupported PCI slot %d for standard hotplug "
502 5d268704 Igor Mammedov
                   "controller. Valid slots are between %d and %d.",
503 5d268704 Igor Mammedov
                   pci_slot, SHPC_IDX_TO_PCI(0),
504 5d268704 Igor Mammedov
                   SHPC_IDX_TO_PCI(shpc->nslots) - 1);
505 5d268704 Igor Mammedov
        return;
506 5d268704 Igor Mammedov
    }
507 5d268704 Igor Mammedov
}
508 5d268704 Igor Mammedov
509 5d268704 Igor Mammedov
void shpc_device_hotplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
510 5d268704 Igor Mammedov
                            Error **errp)
511 5d268704 Igor Mammedov
{
512 5d268704 Igor Mammedov
    Error *local_err = NULL;
513 5d268704 Igor Mammedov
    PCIDevice *pci_hotplug_dev = PCI_DEVICE(hotplug_dev);
514 5d268704 Igor Mammedov
    SHPCDevice *shpc = pci_hotplug_dev->shpc;
515 5d268704 Igor Mammedov
    int slot;
516 5d268704 Igor Mammedov
517 5d268704 Igor Mammedov
    shpc_device_hotplug_common(PCI_DEVICE(dev), &slot, shpc, &local_err);
518 5d268704 Igor Mammedov
    if (local_err) {
519 5d268704 Igor Mammedov
        error_propagate(errp, local_err);
520 5d268704 Igor Mammedov
        return;
521 1dc324d2 Michael S. Tsirkin
    }
522 5d268704 Igor Mammedov
523 1dc324d2 Michael S. Tsirkin
    /* Don't send event when device is enabled during qemu machine creation:
524 1dc324d2 Michael S. Tsirkin
     * it is present on boot, no hotplug event is necessary. We do send an
525 1dc324d2 Michael S. Tsirkin
     * event when the device is disabled later. */
526 5d268704 Igor Mammedov
    if (!dev->hotplugged) {
527 1dc324d2 Michael S. Tsirkin
        shpc_set_status(shpc, slot, 0, SHPC_SLOT_STATUS_MRL_OPEN);
528 1dc324d2 Michael S. Tsirkin
        shpc_set_status(shpc, slot, SHPC_SLOT_STATUS_PRSNT_7_5W,
529 1dc324d2 Michael S. Tsirkin
                        SHPC_SLOT_STATUS_PRSNT_MASK);
530 5d268704 Igor Mammedov
        return;
531 1dc324d2 Michael S. Tsirkin
    }
532 5d268704 Igor Mammedov
533 5d268704 Igor Mammedov
    /* This could be a cancellation of the previous removal.
534 5d268704 Igor Mammedov
     * We check MRL state to figure out. */
535 5d268704 Igor Mammedov
    if (shpc_get_status(shpc, slot, SHPC_SLOT_STATUS_MRL_OPEN)) {
536 5d268704 Igor Mammedov
        shpc_set_status(shpc, slot, 0, SHPC_SLOT_STATUS_MRL_OPEN);
537 5d268704 Igor Mammedov
        shpc_set_status(shpc, slot, SHPC_SLOT_STATUS_PRSNT_7_5W,
538 5d268704 Igor Mammedov
                        SHPC_SLOT_STATUS_PRSNT_MASK);
539 5d268704 Igor Mammedov
        shpc->config[SHPC_SLOT_EVENT_LATCH(slot)] |=
540 5d268704 Igor Mammedov
            SHPC_SLOT_EVENT_BUTTON |
541 5d268704 Igor Mammedov
            SHPC_SLOT_EVENT_MRL |
542 5d268704 Igor Mammedov
            SHPC_SLOT_EVENT_PRESENCE;
543 1dc324d2 Michael S. Tsirkin
    } else {
544 5d268704 Igor Mammedov
        /* Press attention button to cancel removal */
545 5d268704 Igor Mammedov
        shpc->config[SHPC_SLOT_EVENT_LATCH(slot)] |=
546 5d268704 Igor Mammedov
            SHPC_SLOT_EVENT_BUTTON;
547 1dc324d2 Michael S. Tsirkin
    }
548 1dc324d2 Michael S. Tsirkin
    shpc_set_status(shpc, slot, 0, SHPC_SLOT_STATUS_66);
549 5d268704 Igor Mammedov
    shpc_interrupt_update(pci_hotplug_dev);
550 5d268704 Igor Mammedov
}
551 5d268704 Igor Mammedov
552 5d268704 Igor Mammedov
void shpc_device_hot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
553 5d268704 Igor Mammedov
                               Error **errp)
554 5d268704 Igor Mammedov
{
555 5d268704 Igor Mammedov
    Error *local_err = NULL;
556 5d268704 Igor Mammedov
    PCIDevice *pci_hotplug_dev = PCI_DEVICE(hotplug_dev);
557 5d268704 Igor Mammedov
    SHPCDevice *shpc = pci_hotplug_dev->shpc;
558 5d268704 Igor Mammedov
    uint8_t state;
559 5d268704 Igor Mammedov
    uint8_t led;
560 5d268704 Igor Mammedov
    int slot;
561 5d268704 Igor Mammedov
562 5d268704 Igor Mammedov
    shpc_device_hotplug_common(PCI_DEVICE(dev), &slot, shpc, errp);
563 5d268704 Igor Mammedov
    if (local_err) {
564 5d268704 Igor Mammedov
        return;
565 5d268704 Igor Mammedov
    }
566 5d268704 Igor Mammedov
567 5d268704 Igor Mammedov
    shpc->config[SHPC_SLOT_EVENT_LATCH(slot)] |= SHPC_SLOT_EVENT_BUTTON;
568 5d268704 Igor Mammedov
    state = shpc_get_status(shpc, slot, SHPC_SLOT_STATE_MASK);
569 5d268704 Igor Mammedov
    led = shpc_get_status(shpc, slot, SHPC_SLOT_PWR_LED_MASK);
570 5d268704 Igor Mammedov
    if (state == SHPC_STATE_DISABLED && led == SHPC_LED_OFF) {
571 5d268704 Igor Mammedov
        shpc_free_devices_in_slot(shpc, slot);
572 5d268704 Igor Mammedov
        shpc_set_status(shpc, slot, 1, SHPC_SLOT_STATUS_MRL_OPEN);
573 5d268704 Igor Mammedov
        shpc_set_status(shpc, slot, SHPC_SLOT_STATUS_PRSNT_EMPTY,
574 5d268704 Igor Mammedov
                        SHPC_SLOT_STATUS_PRSNT_MASK);
575 5d268704 Igor Mammedov
        shpc->config[SHPC_SLOT_EVENT_LATCH(slot)] |=
576 5d268704 Igor Mammedov
            SHPC_SLOT_EVENT_MRL |
577 5d268704 Igor Mammedov
            SHPC_SLOT_EVENT_PRESENCE;
578 5d268704 Igor Mammedov
    }
579 5d268704 Igor Mammedov
    shpc_set_status(shpc, slot, 0, SHPC_SLOT_STATUS_66);
580 5d268704 Igor Mammedov
    shpc_interrupt_update(pci_hotplug_dev);
581 1dc324d2 Michael S. Tsirkin
}
582 1dc324d2 Michael S. Tsirkin
583 1dc324d2 Michael S. Tsirkin
/* Initialize the SHPC structure in bridge's BAR. */
584 1dc324d2 Michael S. Tsirkin
int shpc_init(PCIDevice *d, PCIBus *sec_bus, MemoryRegion *bar, unsigned offset)
585 1dc324d2 Michael S. Tsirkin
{
586 1dc324d2 Michael S. Tsirkin
    int i, ret;
587 1dc324d2 Michael S. Tsirkin
    int nslots = SHPC_MAX_SLOTS; /* TODO: qdev property? */
588 1dc324d2 Michael S. Tsirkin
    SHPCDevice *shpc = d->shpc = g_malloc0(sizeof(*d->shpc));
589 1dc324d2 Michael S. Tsirkin
    shpc->sec_bus = sec_bus;
590 1dc324d2 Michael S. Tsirkin
    ret = shpc_cap_add_config(d);
591 1dc324d2 Michael S. Tsirkin
    if (ret) {
592 1dc324d2 Michael S. Tsirkin
        g_free(d->shpc);
593 1dc324d2 Michael S. Tsirkin
        return ret;
594 1dc324d2 Michael S. Tsirkin
    }
595 1dc324d2 Michael S. Tsirkin
    if (nslots < SHPC_MIN_SLOTS) {
596 1dc324d2 Michael S. Tsirkin
        return 0;
597 1dc324d2 Michael S. Tsirkin
    }
598 1dc324d2 Michael S. Tsirkin
    if (nslots > SHPC_MAX_SLOTS ||
599 1dc324d2 Michael S. Tsirkin
        SHPC_IDX_TO_PCI(nslots) > PCI_SLOT_MAX) {
600 1dc324d2 Michael S. Tsirkin
        /* TODO: report an error mesage that makes sense. */
601 1dc324d2 Michael S. Tsirkin
        return -EINVAL;
602 1dc324d2 Michael S. Tsirkin
    }
603 1dc324d2 Michael S. Tsirkin
    shpc->nslots = nslots;
604 1dc324d2 Michael S. Tsirkin
    shpc->config = g_malloc0(SHPC_SIZEOF(d));
605 1dc324d2 Michael S. Tsirkin
    shpc->cmask = g_malloc0(SHPC_SIZEOF(d));
606 1dc324d2 Michael S. Tsirkin
    shpc->wmask = g_malloc0(SHPC_SIZEOF(d));
607 1dc324d2 Michael S. Tsirkin
    shpc->w1cmask = g_malloc0(SHPC_SIZEOF(d));
608 1dc324d2 Michael S. Tsirkin
609 1dc324d2 Michael S. Tsirkin
    shpc_reset(d);
610 1dc324d2 Michael S. Tsirkin
611 1dc324d2 Michael S. Tsirkin
    pci_set_long(shpc->config + SHPC_BASE_OFFSET, offset);
612 1dc324d2 Michael S. Tsirkin
613 1dc324d2 Michael S. Tsirkin
    pci_set_byte(shpc->wmask + SHPC_CMD_CODE, 0xff);
614 1dc324d2 Michael S. Tsirkin
    pci_set_byte(shpc->wmask + SHPC_CMD_TRGT, SHPC_CMD_TRGT_MAX);
615 1dc324d2 Michael S. Tsirkin
    pci_set_byte(shpc->wmask + SHPC_CMD_TRGT, SHPC_CMD_TRGT_MAX);
616 1dc324d2 Michael S. Tsirkin
    pci_set_long(shpc->wmask + SHPC_SERR_INT,
617 1dc324d2 Michael S. Tsirkin
                 SHPC_INT_DIS |
618 1dc324d2 Michael S. Tsirkin
                 SHPC_SERR_DIS |
619 1dc324d2 Michael S. Tsirkin
                 SHPC_CMD_INT_DIS |
620 1dc324d2 Michael S. Tsirkin
                 SHPC_ARB_SERR_DIS);
621 1dc324d2 Michael S. Tsirkin
    pci_set_long(shpc->w1cmask + SHPC_SERR_INT,
622 1dc324d2 Michael S. Tsirkin
                 SHPC_CMD_DETECTED |
623 1dc324d2 Michael S. Tsirkin
                 SHPC_ARB_DETECTED);
624 1dc324d2 Michael S. Tsirkin
    for (i = 0; i < nslots; ++i) {
625 1dc324d2 Michael S. Tsirkin
        pci_set_byte(shpc->wmask +
626 1dc324d2 Michael S. Tsirkin
                     SHPC_SLOT_EVENT_SERR_INT_DIS(d, i),
627 1dc324d2 Michael S. Tsirkin
                     SHPC_SLOT_EVENT_PRESENCE |
628 1dc324d2 Michael S. Tsirkin
                     SHPC_SLOT_EVENT_ISOLATED_FAULT |
629 1dc324d2 Michael S. Tsirkin
                     SHPC_SLOT_EVENT_BUTTON |
630 1dc324d2 Michael S. Tsirkin
                     SHPC_SLOT_EVENT_MRL |
631 1dc324d2 Michael S. Tsirkin
                     SHPC_SLOT_EVENT_CONNECTED_FAULT |
632 1dc324d2 Michael S. Tsirkin
                     SHPC_SLOT_EVENT_MRL_SERR_DIS |
633 1dc324d2 Michael S. Tsirkin
                     SHPC_SLOT_EVENT_CONNECTED_FAULT_SERR_DIS);
634 1dc324d2 Michael S. Tsirkin
        pci_set_byte(shpc->w1cmask +
635 1dc324d2 Michael S. Tsirkin
                     SHPC_SLOT_EVENT_LATCH(i),
636 1dc324d2 Michael S. Tsirkin
                     SHPC_SLOT_EVENT_PRESENCE |
637 1dc324d2 Michael S. Tsirkin
                     SHPC_SLOT_EVENT_ISOLATED_FAULT |
638 1dc324d2 Michael S. Tsirkin
                     SHPC_SLOT_EVENT_BUTTON |
639 1dc324d2 Michael S. Tsirkin
                     SHPC_SLOT_EVENT_MRL |
640 1dc324d2 Michael S. Tsirkin
                     SHPC_SLOT_EVENT_CONNECTED_FAULT);
641 1dc324d2 Michael S. Tsirkin
    }
642 1dc324d2 Michael S. Tsirkin
643 1dc324d2 Michael S. Tsirkin
    /* TODO: init cmask */
644 40c5dce9 Paolo Bonzini
    memory_region_init_io(&shpc->mmio, OBJECT(d), &shpc_mmio_ops,
645 40c5dce9 Paolo Bonzini
                          d, "shpc-mmio", SHPC_SIZEOF(d));
646 1dc324d2 Michael S. Tsirkin
    shpc_cap_update_dword(d);
647 1dc324d2 Michael S. Tsirkin
    memory_region_add_subregion(bar, offset, &shpc->mmio);
648 5d268704 Igor Mammedov
649 5d268704 Igor Mammedov
    qbus_set_hotplug_handler(BUS(sec_bus), DEVICE(d), NULL);
650 1dc324d2 Michael S. Tsirkin
651 1dc324d2 Michael S. Tsirkin
    d->cap_present |= QEMU_PCI_CAP_SHPC;
652 1dc324d2 Michael S. Tsirkin
    return 0;
653 1dc324d2 Michael S. Tsirkin
}
654 1dc324d2 Michael S. Tsirkin
655 1dc324d2 Michael S. Tsirkin
int shpc_bar_size(PCIDevice *d)
656 1dc324d2 Michael S. Tsirkin
{
657 1dc324d2 Michael S. Tsirkin
    return roundup_pow_of_two(SHPC_SLOT_REG(SHPC_MAX_SLOTS));
658 1dc324d2 Michael S. Tsirkin
}
659 1dc324d2 Michael S. Tsirkin
660 1dc324d2 Michael S. Tsirkin
void shpc_cleanup(PCIDevice *d, MemoryRegion *bar)
661 1dc324d2 Michael S. Tsirkin
{
662 1dc324d2 Michael S. Tsirkin
    SHPCDevice *shpc = d->shpc;
663 1dc324d2 Michael S. Tsirkin
    d->cap_present &= ~QEMU_PCI_CAP_SHPC;
664 1dc324d2 Michael S. Tsirkin
    memory_region_del_subregion(bar, &shpc->mmio);
665 1dc324d2 Michael S. Tsirkin
    /* TODO: cleanup config space changes? */
666 1dc324d2 Michael S. Tsirkin
    g_free(shpc->config);
667 1dc324d2 Michael S. Tsirkin
    g_free(shpc->cmask);
668 1dc324d2 Michael S. Tsirkin
    g_free(shpc->wmask);
669 1dc324d2 Michael S. Tsirkin
    g_free(shpc->w1cmask);
670 1dc324d2 Michael S. Tsirkin
    memory_region_destroy(&shpc->mmio);
671 1dc324d2 Michael S. Tsirkin
    g_free(shpc);
672 1dc324d2 Michael S. Tsirkin
}
673 1dc324d2 Michael S. Tsirkin
674 1dc324d2 Michael S. Tsirkin
void shpc_cap_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
675 1dc324d2 Michael S. Tsirkin
{
676 1dc324d2 Michael S. Tsirkin
    if (!ranges_overlap(addr, l, d->shpc->cap, SHPC_CAP_LENGTH)) {
677 1dc324d2 Michael S. Tsirkin
        return;
678 1dc324d2 Michael S. Tsirkin
    }
679 1dc324d2 Michael S. Tsirkin
    if (ranges_overlap(addr, l, d->shpc->cap + SHPC_CAP_DWORD_DATA, 4)) {
680 1dc324d2 Michael S. Tsirkin
        unsigned dword_data;
681 1dc324d2 Michael S. Tsirkin
        dword_data = pci_get_long(d->shpc->config + d->shpc->cap
682 1dc324d2 Michael S. Tsirkin
                                  + SHPC_CAP_DWORD_DATA);
683 1dc324d2 Michael S. Tsirkin
        shpc_write(d, shpc_cap_dword(d) * 4, dword_data, 4);
684 1dc324d2 Michael S. Tsirkin
    }
685 1dc324d2 Michael S. Tsirkin
    /* Update cap dword data in case guest is going to read it. */
686 1dc324d2 Michael S. Tsirkin
    shpc_cap_update_dword(d);
687 1dc324d2 Michael S. Tsirkin
}
688 1dc324d2 Michael S. Tsirkin
689 1dc324d2 Michael S. Tsirkin
static void shpc_save(QEMUFile *f, void *pv, size_t size)
690 1dc324d2 Michael S. Tsirkin
{
691 1dc324d2 Michael S. Tsirkin
    PCIDevice *d = container_of(pv, PCIDevice, shpc);
692 1dc324d2 Michael S. Tsirkin
    qemu_put_buffer(f, d->shpc->config, SHPC_SIZEOF(d));
693 1dc324d2 Michael S. Tsirkin
}
694 1dc324d2 Michael S. Tsirkin
695 1dc324d2 Michael S. Tsirkin
static int shpc_load(QEMUFile *f, void *pv, size_t size)
696 1dc324d2 Michael S. Tsirkin
{
697 1dc324d2 Michael S. Tsirkin
    PCIDevice *d = container_of(pv, PCIDevice, shpc);
698 1dc324d2 Michael S. Tsirkin
    int ret = qemu_get_buffer(f, d->shpc->config, SHPC_SIZEOF(d));
699 1dc324d2 Michael S. Tsirkin
    if (ret != SHPC_SIZEOF(d)) {
700 1dc324d2 Michael S. Tsirkin
        return -EINVAL;
701 1dc324d2 Michael S. Tsirkin
    }
702 1dc324d2 Michael S. Tsirkin
    /* Make sure we don't lose notifications. An extra interrupt is harmless. */
703 1dc324d2 Michael S. Tsirkin
    d->shpc->msi_requested = 0;
704 1dc324d2 Michael S. Tsirkin
    shpc_interrupt_update(d);
705 1dc324d2 Michael S. Tsirkin
    return 0;
706 1dc324d2 Michael S. Tsirkin
}
707 1dc324d2 Michael S. Tsirkin
708 1dc324d2 Michael S. Tsirkin
VMStateInfo shpc_vmstate_info = {
709 1dc324d2 Michael S. Tsirkin
    .name = "shpc",
710 1dc324d2 Michael S. Tsirkin
    .get  = shpc_load,
711 1dc324d2 Michael S. Tsirkin
    .put  = shpc_save,
712 1dc324d2 Michael S. Tsirkin
};