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