]>
Commit | Line | Data |
---|---|---|
4040ab72 DG |
1 | /* |
2 | * QEMU sPAPR VIO code | |
3 | * | |
4 | * Copyright (c) 2010 David Gibson, IBM Corporation <[email protected]> | |
5 | * Based on the s390 virtio bus code: | |
6 | * Copyright (c) 2009 Alexander Graf <[email protected]> | |
7 | * | |
8 | * This library is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License as published by the Free Software Foundation; either | |
11 | * version 2 of the License, or (at your option) any later version. | |
12 | * | |
13 | * This library is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Lesser General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public | |
19 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
22 | #include "hw.h" | |
23 | #include "sysemu.h" | |
24 | #include "boards.h" | |
25 | #include "monitor.h" | |
26 | #include "loader.h" | |
27 | #include "elf.h" | |
28 | #include "hw/sysbus.h" | |
29 | #include "kvm.h" | |
30 | #include "device_tree.h" | |
b45d63b6 | 31 | #include "kvm_ppc.h" |
4040ab72 DG |
32 | |
33 | #include "hw/spapr.h" | |
34 | #include "hw/spapr_vio.h" | |
277f9acf | 35 | #include "hw/xics.h" |
4040ab72 DG |
36 | |
37 | #ifdef CONFIG_FDT | |
38 | #include <libfdt.h> | |
39 | #endif /* CONFIG_FDT */ | |
40 | ||
41 | /* #define DEBUG_SPAPR */ | |
ee86dfee | 42 | /* #define DEBUG_TCE */ |
4040ab72 DG |
43 | |
44 | #ifdef DEBUG_SPAPR | |
45 | #define dprintf(fmt, ...) \ | |
46 | do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0) | |
47 | #else | |
48 | #define dprintf(fmt, ...) \ | |
49 | do { } while (0) | |
50 | #endif | |
51 | ||
52 | static struct BusInfo spapr_vio_bus_info = { | |
53 | .name = "spapr-vio", | |
54 | .size = sizeof(VIOsPAPRBus), | |
416343b1 PB |
55 | .props = (Property[]) { |
56 | DEFINE_PROP_UINT32("irq", VIOsPAPRDevice, vio_irq_num, 0), \ | |
57 | DEFINE_PROP_END_OF_LIST(), | |
58 | }, | |
4040ab72 DG |
59 | }; |
60 | ||
61 | VIOsPAPRDevice *spapr_vio_find_by_reg(VIOsPAPRBus *bus, uint32_t reg) | |
62 | { | |
63 | DeviceState *qdev; | |
64 | VIOsPAPRDevice *dev = NULL; | |
65 | ||
d8bb00d6 | 66 | QTAILQ_FOREACH(qdev, &bus->bus.children, sibling) { |
4040ab72 DG |
67 | dev = (VIOsPAPRDevice *)qdev; |
68 | if (dev->reg == reg) { | |
5435352c | 69 | return dev; |
4040ab72 DG |
70 | } |
71 | } | |
72 | ||
5435352c | 73 | return NULL; |
4040ab72 DG |
74 | } |
75 | ||
1e34d859 ME |
76 | static char *vio_format_dev_name(VIOsPAPRDevice *dev) |
77 | { | |
3954d33a | 78 | VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev); |
1e34d859 ME |
79 | char *name; |
80 | ||
81 | /* Device tree style name device@reg */ | |
3954d33a | 82 | if (asprintf(&name, "%s@%x", pc->dt_name, dev->reg) < 0) { |
1e34d859 ME |
83 | return NULL; |
84 | } | |
85 | ||
86 | return name; | |
87 | } | |
88 | ||
4040ab72 DG |
89 | #ifdef CONFIG_FDT |
90 | static int vio_make_devnode(VIOsPAPRDevice *dev, | |
91 | void *fdt) | |
92 | { | |
3954d33a | 93 | VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev); |
1e34d859 ME |
94 | int vdevice_off, node_off, ret; |
95 | char *dt_name; | |
4040ab72 DG |
96 | |
97 | vdevice_off = fdt_path_offset(fdt, "/vdevice"); | |
98 | if (vdevice_off < 0) { | |
99 | return vdevice_off; | |
100 | } | |
101 | ||
1e34d859 ME |
102 | dt_name = vio_format_dev_name(dev); |
103 | if (!dt_name) { | |
104 | return -ENOMEM; | |
105 | } | |
106 | ||
107 | node_off = fdt_add_subnode(fdt, vdevice_off, dt_name); | |
108 | free(dt_name); | |
4040ab72 DG |
109 | if (node_off < 0) { |
110 | return node_off; | |
111 | } | |
112 | ||
113 | ret = fdt_setprop_cell(fdt, node_off, "reg", dev->reg); | |
114 | if (ret < 0) { | |
115 | return ret; | |
116 | } | |
117 | ||
3954d33a | 118 | if (pc->dt_type) { |
4040ab72 | 119 | ret = fdt_setprop_string(fdt, node_off, "device_type", |
3954d33a | 120 | pc->dt_type); |
4040ab72 DG |
121 | if (ret < 0) { |
122 | return ret; | |
123 | } | |
124 | } | |
125 | ||
3954d33a | 126 | if (pc->dt_compatible) { |
4040ab72 | 127 | ret = fdt_setprop_string(fdt, node_off, "compatible", |
3954d33a | 128 | pc->dt_compatible); |
4040ab72 DG |
129 | if (ret < 0) { |
130 | return ret; | |
131 | } | |
132 | } | |
133 | ||
00dc738d DG |
134 | if (dev->qirq) { |
135 | uint32_t ints_prop[] = {cpu_to_be32(dev->vio_irq_num), 0}; | |
136 | ||
137 | ret = fdt_setprop(fdt, node_off, "interrupts", ints_prop, | |
138 | sizeof(ints_prop)); | |
139 | if (ret < 0) { | |
140 | return ret; | |
141 | } | |
142 | } | |
143 | ||
ee86dfee DG |
144 | if (dev->rtce_window_size) { |
145 | uint32_t dma_prop[] = {cpu_to_be32(dev->reg), | |
146 | 0, 0, | |
147 | 0, cpu_to_be32(dev->rtce_window_size)}; | |
148 | ||
149 | ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-address-cells", 2); | |
150 | if (ret < 0) { | |
151 | return ret; | |
152 | } | |
153 | ||
154 | ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-size-cells", 2); | |
155 | if (ret < 0) { | |
156 | return ret; | |
157 | } | |
158 | ||
159 | ret = fdt_setprop(fdt, node_off, "ibm,my-dma-window", dma_prop, | |
160 | sizeof(dma_prop)); | |
161 | if (ret < 0) { | |
162 | return ret; | |
163 | } | |
164 | } | |
165 | ||
3954d33a AL |
166 | if (pc->devnode) { |
167 | ret = (pc->devnode)(dev, fdt, node_off); | |
4040ab72 DG |
168 | if (ret < 0) { |
169 | return ret; | |
170 | } | |
171 | } | |
172 | ||
173 | return node_off; | |
174 | } | |
175 | #endif /* CONFIG_FDT */ | |
176 | ||
ee86dfee DG |
177 | /* |
178 | * RTCE handling | |
179 | */ | |
180 | ||
181 | static void rtce_init(VIOsPAPRDevice *dev) | |
182 | { | |
183 | size_t size = (dev->rtce_window_size >> SPAPR_VIO_TCE_PAGE_SHIFT) | |
184 | * sizeof(VIOsPAPR_RTCE); | |
185 | ||
186 | if (size) { | |
0f5cb298 DG |
187 | dev->rtce_table = kvmppc_create_spapr_tce(dev->reg, |
188 | dev->rtce_window_size, | |
189 | &dev->kvmtce_fd); | |
190 | ||
191 | if (!dev->rtce_table) { | |
192 | dev->rtce_table = g_malloc0(size); | |
193 | } | |
ee86dfee DG |
194 | } |
195 | } | |
196 | ||
197 | static target_ulong h_put_tce(CPUState *env, sPAPREnvironment *spapr, | |
198 | target_ulong opcode, target_ulong *args) | |
199 | { | |
200 | target_ulong liobn = args[0]; | |
201 | target_ulong ioba = args[1]; | |
202 | target_ulong tce = args[2]; | |
203 | VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, liobn); | |
204 | VIOsPAPR_RTCE *rtce; | |
205 | ||
206 | if (!dev) { | |
207 | hcall_dprintf("spapr_vio_put_tce on non-existent LIOBN " | |
208 | TARGET_FMT_lx "\n", liobn); | |
209 | return H_PARAMETER; | |
210 | } | |
211 | ||
212 | ioba &= ~(SPAPR_VIO_TCE_PAGE_SIZE - 1); | |
213 | ||
214 | #ifdef DEBUG_TCE | |
215 | fprintf(stderr, "spapr_vio_put_tce on %s ioba 0x" TARGET_FMT_lx | |
216 | " TCE 0x" TARGET_FMT_lx "\n", dev->qdev.id, ioba, tce); | |
217 | #endif | |
218 | ||
219 | if (ioba >= dev->rtce_window_size) { | |
220 | hcall_dprintf("spapr_vio_put_tce on out-of-boards IOBA 0x" | |
221 | TARGET_FMT_lx "\n", ioba); | |
222 | return H_PARAMETER; | |
223 | } | |
224 | ||
225 | rtce = dev->rtce_table + (ioba >> SPAPR_VIO_TCE_PAGE_SHIFT); | |
226 | rtce->tce = tce; | |
227 | ||
228 | return H_SUCCESS; | |
229 | } | |
230 | ||
231 | int spapr_vio_check_tces(VIOsPAPRDevice *dev, target_ulong ioba, | |
232 | target_ulong len, enum VIOsPAPR_TCEAccess access) | |
233 | { | |
234 | int start, end, i; | |
235 | ||
236 | start = ioba >> SPAPR_VIO_TCE_PAGE_SHIFT; | |
237 | end = (ioba + len - 1) >> SPAPR_VIO_TCE_PAGE_SHIFT; | |
238 | ||
239 | for (i = start; i <= end; i++) { | |
240 | if ((dev->rtce_table[i].tce & access) != access) { | |
241 | #ifdef DEBUG_TCE | |
242 | fprintf(stderr, "FAIL on %d\n", i); | |
243 | #endif | |
244 | return -1; | |
245 | } | |
246 | } | |
247 | ||
248 | return 0; | |
249 | } | |
250 | ||
251 | int spapr_tce_dma_write(VIOsPAPRDevice *dev, uint64_t taddr, const void *buf, | |
252 | uint32_t size) | |
253 | { | |
254 | #ifdef DEBUG_TCE | |
255 | fprintf(stderr, "spapr_tce_dma_write taddr=0x%llx size=0x%x\n", | |
256 | (unsigned long long)taddr, size); | |
257 | #endif | |
258 | ||
08942ac1 BH |
259 | /* Check for bypass */ |
260 | if (dev->flags & VIO_PAPR_FLAG_DMA_BYPASS) { | |
261 | cpu_physical_memory_write(taddr, buf, size); | |
262 | return 0; | |
263 | } | |
264 | ||
ee86dfee DG |
265 | while (size) { |
266 | uint64_t tce; | |
267 | uint32_t lsize; | |
268 | uint64_t txaddr; | |
269 | ||
270 | /* Check if we are in bound */ | |
271 | if (taddr >= dev->rtce_window_size) { | |
272 | #ifdef DEBUG_TCE | |
273 | fprintf(stderr, "spapr_tce_dma_write out of bounds\n"); | |
274 | #endif | |
275 | return H_DEST_PARM; | |
276 | } | |
277 | tce = dev->rtce_table[taddr >> SPAPR_VIO_TCE_PAGE_SHIFT].tce; | |
278 | ||
279 | /* How much til end of page ? */ | |
280 | lsize = MIN(size, ((~taddr) & SPAPR_VIO_TCE_PAGE_MASK) + 1); | |
281 | ||
282 | /* Check TCE */ | |
283 | if (!(tce & 2)) { | |
284 | return H_DEST_PARM; | |
285 | } | |
286 | ||
287 | /* Translate */ | |
288 | txaddr = (tce & ~SPAPR_VIO_TCE_PAGE_MASK) | | |
289 | (taddr & SPAPR_VIO_TCE_PAGE_MASK); | |
290 | ||
291 | #ifdef DEBUG_TCE | |
292 | fprintf(stderr, " -> write to txaddr=0x%llx, size=0x%x\n", | |
293 | (unsigned long long)txaddr, lsize); | |
294 | #endif | |
295 | ||
296 | /* Do it */ | |
297 | cpu_physical_memory_write(txaddr, buf, lsize); | |
298 | buf += lsize; | |
299 | taddr += lsize; | |
300 | size -= lsize; | |
301 | } | |
302 | return 0; | |
303 | } | |
304 | ||
305 | int spapr_tce_dma_zero(VIOsPAPRDevice *dev, uint64_t taddr, uint32_t size) | |
306 | { | |
307 | /* FIXME: allocating a temp buffer is nasty, but just stepping | |
308 | * through writing zeroes is awkward. This will do for now. */ | |
309 | uint8_t zeroes[size]; | |
310 | ||
311 | #ifdef DEBUG_TCE | |
312 | fprintf(stderr, "spapr_tce_dma_zero taddr=0x%llx size=0x%x\n", | |
313 | (unsigned long long)taddr, size); | |
314 | #endif | |
315 | ||
316 | memset(zeroes, 0, size); | |
317 | return spapr_tce_dma_write(dev, taddr, zeroes, size); | |
318 | } | |
319 | ||
320 | void stb_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint8_t val) | |
321 | { | |
322 | spapr_tce_dma_write(dev, taddr, &val, sizeof(val)); | |
323 | } | |
324 | ||
325 | void sth_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint16_t val) | |
326 | { | |
327 | val = tswap16(val); | |
328 | spapr_tce_dma_write(dev, taddr, &val, sizeof(val)); | |
329 | } | |
330 | ||
331 | ||
332 | void stw_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint32_t val) | |
333 | { | |
334 | val = tswap32(val); | |
335 | spapr_tce_dma_write(dev, taddr, &val, sizeof(val)); | |
336 | } | |
337 | ||
338 | void stq_tce(VIOsPAPRDevice *dev, uint64_t taddr, uint64_t val) | |
339 | { | |
340 | val = tswap64(val); | |
341 | spapr_tce_dma_write(dev, taddr, &val, sizeof(val)); | |
342 | } | |
343 | ||
344 | int spapr_tce_dma_read(VIOsPAPRDevice *dev, uint64_t taddr, void *buf, | |
345 | uint32_t size) | |
346 | { | |
347 | #ifdef DEBUG_TCE | |
348 | fprintf(stderr, "spapr_tce_dma_write taddr=0x%llx size=0x%x\n", | |
349 | (unsigned long long)taddr, size); | |
350 | #endif | |
351 | ||
08942ac1 BH |
352 | /* Check for bypass */ |
353 | if (dev->flags & VIO_PAPR_FLAG_DMA_BYPASS) { | |
354 | cpu_physical_memory_read(taddr, buf, size); | |
355 | return 0; | |
356 | } | |
357 | ||
ee86dfee DG |
358 | while (size) { |
359 | uint64_t tce; | |
360 | uint32_t lsize; | |
361 | uint64_t txaddr; | |
362 | ||
363 | /* Check if we are in bound */ | |
364 | if (taddr >= dev->rtce_window_size) { | |
365 | #ifdef DEBUG_TCE | |
366 | fprintf(stderr, "spapr_tce_dma_read out of bounds\n"); | |
367 | #endif | |
368 | return H_DEST_PARM; | |
369 | } | |
370 | tce = dev->rtce_table[taddr >> SPAPR_VIO_TCE_PAGE_SHIFT].tce; | |
371 | ||
372 | /* How much til end of page ? */ | |
373 | lsize = MIN(size, ((~taddr) & SPAPR_VIO_TCE_PAGE_MASK) + 1); | |
374 | ||
375 | /* Check TCE */ | |
376 | if (!(tce & 1)) { | |
377 | return H_DEST_PARM; | |
378 | } | |
379 | ||
380 | /* Translate */ | |
381 | txaddr = (tce & ~SPAPR_VIO_TCE_PAGE_MASK) | | |
382 | (taddr & SPAPR_VIO_TCE_PAGE_MASK); | |
383 | ||
384 | #ifdef DEBUG_TCE | |
385 | fprintf(stderr, " -> write to txaddr=0x%llx, size=0x%x\n", | |
386 | (unsigned long long)txaddr, lsize); | |
387 | #endif | |
388 | /* Do it */ | |
389 | cpu_physical_memory_read(txaddr, buf, lsize); | |
390 | buf += lsize; | |
391 | taddr += lsize; | |
392 | size -= lsize; | |
393 | } | |
394 | return H_SUCCESS; | |
395 | } | |
396 | ||
397 | uint64_t ldq_tce(VIOsPAPRDevice *dev, uint64_t taddr) | |
398 | { | |
399 | uint64_t val; | |
400 | ||
401 | spapr_tce_dma_read(dev, taddr, &val, sizeof(val)); | |
402 | return tswap64(val); | |
403 | } | |
404 | ||
b45d63b6 BH |
405 | /* |
406 | * CRQ handling | |
407 | */ | |
408 | static target_ulong h_reg_crq(CPUState *env, sPAPREnvironment *spapr, | |
409 | target_ulong opcode, target_ulong *args) | |
410 | { | |
411 | target_ulong reg = args[0]; | |
412 | target_ulong queue_addr = args[1]; | |
413 | target_ulong queue_len = args[2]; | |
414 | VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg); | |
415 | ||
416 | if (!dev) { | |
417 | hcall_dprintf("h_reg_crq on non-existent unit 0x" | |
418 | TARGET_FMT_lx "\n", reg); | |
419 | return H_PARAMETER; | |
420 | } | |
421 | ||
422 | /* We can't grok a queue size bigger than 256M for now */ | |
423 | if (queue_len < 0x1000 || queue_len > 0x10000000) { | |
424 | hcall_dprintf("h_reg_crq, queue size too small or too big (0x%llx)\n", | |
425 | (unsigned long long)queue_len); | |
426 | return H_PARAMETER; | |
427 | } | |
428 | ||
429 | /* Check queue alignment */ | |
430 | if (queue_addr & 0xfff) { | |
431 | hcall_dprintf("h_reg_crq, queue not aligned (0x%llx)\n", | |
432 | (unsigned long long)queue_addr); | |
433 | return H_PARAMETER; | |
434 | } | |
435 | ||
436 | /* Check if device supports CRQs */ | |
437 | if (!dev->crq.SendFunc) { | |
438 | return H_NOT_FOUND; | |
439 | } | |
440 | ||
441 | ||
442 | /* Already a queue ? */ | |
443 | if (dev->crq.qsize) { | |
444 | return H_RESOURCE; | |
445 | } | |
446 | dev->crq.qladdr = queue_addr; | |
447 | dev->crq.qsize = queue_len; | |
448 | dev->crq.qnext = 0; | |
449 | ||
450 | dprintf("CRQ for dev 0x" TARGET_FMT_lx " registered at 0x" | |
451 | TARGET_FMT_lx "/0x" TARGET_FMT_lx "\n", | |
452 | reg, queue_addr, queue_len); | |
453 | return H_SUCCESS; | |
454 | } | |
455 | ||
456 | static target_ulong h_free_crq(CPUState *env, sPAPREnvironment *spapr, | |
457 | target_ulong opcode, target_ulong *args) | |
458 | { | |
459 | target_ulong reg = args[0]; | |
460 | VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg); | |
461 | ||
462 | if (!dev) { | |
463 | hcall_dprintf("h_free_crq on non-existent unit 0x" | |
464 | TARGET_FMT_lx "\n", reg); | |
465 | return H_PARAMETER; | |
466 | } | |
467 | ||
468 | dev->crq.qladdr = 0; | |
469 | dev->crq.qsize = 0; | |
470 | dev->crq.qnext = 0; | |
471 | ||
472 | dprintf("CRQ for dev 0x" TARGET_FMT_lx " freed\n", reg); | |
473 | ||
474 | return H_SUCCESS; | |
475 | } | |
476 | ||
477 | static target_ulong h_send_crq(CPUState *env, sPAPREnvironment *spapr, | |
478 | target_ulong opcode, target_ulong *args) | |
479 | { | |
480 | target_ulong reg = args[0]; | |
481 | target_ulong msg_hi = args[1]; | |
482 | target_ulong msg_lo = args[2]; | |
483 | VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg); | |
484 | uint64_t crq_mangle[2]; | |
485 | ||
486 | if (!dev) { | |
487 | hcall_dprintf("h_send_crq on non-existent unit 0x" | |
488 | TARGET_FMT_lx "\n", reg); | |
489 | return H_PARAMETER; | |
490 | } | |
491 | crq_mangle[0] = cpu_to_be64(msg_hi); | |
492 | crq_mangle[1] = cpu_to_be64(msg_lo); | |
493 | ||
494 | if (dev->crq.SendFunc) { | |
495 | return dev->crq.SendFunc(dev, (uint8_t *)crq_mangle); | |
496 | } | |
497 | ||
498 | return H_HARDWARE; | |
499 | } | |
500 | ||
501 | static target_ulong h_enable_crq(CPUState *env, sPAPREnvironment *spapr, | |
502 | target_ulong opcode, target_ulong *args) | |
503 | { | |
504 | target_ulong reg = args[0]; | |
505 | VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg); | |
506 | ||
507 | if (!dev) { | |
508 | hcall_dprintf("h_enable_crq on non-existent unit 0x" | |
509 | TARGET_FMT_lx "\n", reg); | |
510 | return H_PARAMETER; | |
511 | } | |
512 | ||
513 | return 0; | |
514 | } | |
515 | ||
516 | /* Returns negative error, 0 success, or positive: queue full */ | |
517 | int spapr_vio_send_crq(VIOsPAPRDevice *dev, uint8_t *crq) | |
518 | { | |
519 | int rc; | |
520 | uint8_t byte; | |
521 | ||
522 | if (!dev->crq.qsize) { | |
523 | fprintf(stderr, "spapr_vio_send_creq on uninitialized queue\n"); | |
524 | return -1; | |
525 | } | |
526 | ||
527 | /* Maybe do a fast path for KVM just writing to the pages */ | |
528 | rc = spapr_tce_dma_read(dev, dev->crq.qladdr + dev->crq.qnext, &byte, 1); | |
529 | if (rc) { | |
530 | return rc; | |
531 | } | |
532 | if (byte != 0) { | |
533 | return 1; | |
534 | } | |
535 | ||
536 | rc = spapr_tce_dma_write(dev, dev->crq.qladdr + dev->crq.qnext + 8, | |
537 | &crq[8], 8); | |
538 | if (rc) { | |
539 | return rc; | |
540 | } | |
541 | ||
542 | kvmppc_eieio(); | |
543 | ||
544 | rc = spapr_tce_dma_write(dev, dev->crq.qladdr + dev->crq.qnext, crq, 8); | |
545 | if (rc) { | |
546 | return rc; | |
547 | } | |
548 | ||
549 | dev->crq.qnext = (dev->crq.qnext + 16) % dev->crq.qsize; | |
550 | ||
551 | if (dev->signal_state & 1) { | |
552 | qemu_irq_pulse(dev->qirq); | |
553 | } | |
554 | ||
555 | return 0; | |
556 | } | |
557 | ||
08942ac1 BH |
558 | /* "quiesce" handling */ |
559 | ||
560 | static void spapr_vio_quiesce_one(VIOsPAPRDevice *dev) | |
561 | { | |
562 | dev->flags &= ~VIO_PAPR_FLAG_DMA_BYPASS; | |
563 | ||
564 | if (dev->rtce_table) { | |
565 | size_t size = (dev->rtce_window_size >> SPAPR_VIO_TCE_PAGE_SHIFT) | |
566 | * sizeof(VIOsPAPR_RTCE); | |
567 | memset(dev->rtce_table, 0, size); | |
568 | } | |
569 | ||
570 | dev->crq.qladdr = 0; | |
571 | dev->crq.qsize = 0; | |
572 | dev->crq.qnext = 0; | |
573 | } | |
574 | ||
575 | static void rtas_set_tce_bypass(sPAPREnvironment *spapr, uint32_t token, | |
576 | uint32_t nargs, target_ulong args, | |
577 | uint32_t nret, target_ulong rets) | |
578 | { | |
579 | VIOsPAPRBus *bus = spapr->vio_bus; | |
580 | VIOsPAPRDevice *dev; | |
581 | uint32_t unit, enable; | |
582 | ||
583 | if (nargs != 2) { | |
584 | rtas_st(rets, 0, -3); | |
585 | return; | |
586 | } | |
587 | unit = rtas_ld(args, 0); | |
588 | enable = rtas_ld(args, 1); | |
589 | dev = spapr_vio_find_by_reg(bus, unit); | |
590 | if (!dev) { | |
591 | rtas_st(rets, 0, -3); | |
592 | return; | |
593 | } | |
594 | if (enable) { | |
595 | dev->flags |= VIO_PAPR_FLAG_DMA_BYPASS; | |
596 | } else { | |
597 | dev->flags &= ~VIO_PAPR_FLAG_DMA_BYPASS; | |
598 | } | |
599 | ||
600 | rtas_st(rets, 0, 0); | |
601 | } | |
602 | ||
603 | static void rtas_quiesce(sPAPREnvironment *spapr, uint32_t token, | |
604 | uint32_t nargs, target_ulong args, | |
605 | uint32_t nret, target_ulong rets) | |
606 | { | |
607 | VIOsPAPRBus *bus = spapr->vio_bus; | |
608 | DeviceState *qdev; | |
609 | VIOsPAPRDevice *dev = NULL; | |
610 | ||
611 | if (nargs != 0) { | |
612 | rtas_st(rets, 0, -3); | |
613 | return; | |
614 | } | |
615 | ||
d8bb00d6 | 616 | QTAILQ_FOREACH(qdev, &bus->bus.children, sibling) { |
08942ac1 BH |
617 | dev = (VIOsPAPRDevice *)qdev; |
618 | spapr_vio_quiesce_one(dev); | |
619 | } | |
620 | ||
621 | rtas_st(rets, 0, 0); | |
622 | } | |
623 | ||
3954d33a | 624 | static int spapr_vio_check_reg(VIOsPAPRDevice *sdev) |
9fc380d3 ME |
625 | { |
626 | VIOsPAPRDevice *other_sdev; | |
627 | DeviceState *qdev; | |
628 | VIOsPAPRBus *sbus; | |
629 | ||
630 | sbus = DO_UPCAST(VIOsPAPRBus, bus, sdev->qdev.parent_bus); | |
631 | ||
632 | /* | |
633 | * Check two device aren't given clashing addresses by the user (or some | |
634 | * other mechanism). We have to open code this because we have to check | |
635 | * for matches with devices other than us. | |
636 | */ | |
637 | QTAILQ_FOREACH(qdev, &sbus->bus.children, sibling) { | |
638 | other_sdev = DO_UPCAST(VIOsPAPRDevice, qdev, qdev); | |
639 | ||
640 | if (other_sdev != sdev && other_sdev->reg == sdev->reg) { | |
641 | fprintf(stderr, "vio: %s and %s devices conflict at address %#x\n", | |
3954d33a AL |
642 | object_get_typename(OBJECT(sdev)), |
643 | object_get_typename(OBJECT(qdev)), | |
644 | sdev->reg); | |
9fc380d3 ME |
645 | return -EEXIST; |
646 | } | |
647 | } | |
648 | ||
649 | return 0; | |
650 | } | |
651 | ||
d307af79 | 652 | static int spapr_vio_busdev_init(DeviceState *qdev) |
4040ab72 | 653 | { |
4040ab72 | 654 | VIOsPAPRDevice *dev = (VIOsPAPRDevice *)qdev; |
3954d33a | 655 | VIOsPAPRDeviceClass *pc = VIO_SPAPR_DEVICE_GET_CLASS(dev); |
4040ab72 | 656 | char *id; |
9fc380d3 ME |
657 | int ret; |
658 | ||
3954d33a | 659 | ret = spapr_vio_check_reg(dev); |
9fc380d3 ME |
660 | if (ret) { |
661 | return ret; | |
662 | } | |
4040ab72 | 663 | |
1e34d859 ME |
664 | /* Don't overwrite ids assigned on the command line */ |
665 | if (!dev->qdev.id) { | |
666 | id = vio_format_dev_name(dev); | |
667 | if (!id) { | |
668 | return -1; | |
669 | } | |
670 | dev->qdev.id = id; | |
4040ab72 DG |
671 | } |
672 | ||
e6c866d4 DG |
673 | dev->qirq = spapr_allocate_irq(dev->vio_irq_num, &dev->vio_irq_num); |
674 | if (!dev->qirq) { | |
675 | return -1; | |
416343b1 | 676 | } |
4040ab72 | 677 | |
ee86dfee DG |
678 | rtce_init(dev); |
679 | ||
3954d33a | 680 | return pc->init(dev); |
4040ab72 DG |
681 | } |
682 | ||
00dc738d DG |
683 | static target_ulong h_vio_signal(CPUState *env, sPAPREnvironment *spapr, |
684 | target_ulong opcode, | |
685 | target_ulong *args) | |
686 | { | |
687 | target_ulong reg = args[0]; | |
688 | target_ulong mode = args[1]; | |
689 | VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg); | |
3954d33a | 690 | VIOsPAPRDeviceClass *pc; |
00dc738d DG |
691 | |
692 | if (!dev) { | |
693 | return H_PARAMETER; | |
694 | } | |
695 | ||
3954d33a | 696 | pc = VIO_SPAPR_DEVICE_GET_CLASS(dev); |
00dc738d | 697 | |
3954d33a | 698 | if (mode & ~pc->signal_mask) { |
00dc738d DG |
699 | return H_PARAMETER; |
700 | } | |
701 | ||
702 | dev->signal_state = mode; | |
703 | ||
704 | return H_SUCCESS; | |
705 | } | |
706 | ||
4040ab72 DG |
707 | VIOsPAPRBus *spapr_vio_bus_init(void) |
708 | { | |
709 | VIOsPAPRBus *bus; | |
710 | BusState *qbus; | |
711 | DeviceState *dev; | |
4040ab72 DG |
712 | |
713 | /* Create bridge device */ | |
714 | dev = qdev_create(NULL, "spapr-vio-bridge"); | |
715 | qdev_init_nofail(dev); | |
716 | ||
717 | /* Create bus on bridge device */ | |
718 | ||
719 | qbus = qbus_create(&spapr_vio_bus_info, dev, "spapr-vio"); | |
720 | bus = DO_UPCAST(VIOsPAPRBus, bus, qbus); | |
721 | ||
00dc738d DG |
722 | /* hcall-vio */ |
723 | spapr_register_hypercall(H_VIO_SIGNAL, h_vio_signal); | |
724 | ||
ee86dfee DG |
725 | /* hcall-tce */ |
726 | spapr_register_hypercall(H_PUT_TCE, h_put_tce); | |
727 | ||
b45d63b6 BH |
728 | /* hcall-crq */ |
729 | spapr_register_hypercall(H_REG_CRQ, h_reg_crq); | |
730 | spapr_register_hypercall(H_FREE_CRQ, h_free_crq); | |
731 | spapr_register_hypercall(H_SEND_CRQ, h_send_crq); | |
732 | spapr_register_hypercall(H_ENABLE_CRQ, h_enable_crq); | |
733 | ||
08942ac1 BH |
734 | /* RTAS calls */ |
735 | spapr_rtas_register("ibm,set-tce-bypass", rtas_set_tce_bypass); | |
736 | spapr_rtas_register("quiesce", rtas_quiesce); | |
737 | ||
4040ab72 DG |
738 | return bus; |
739 | } | |
740 | ||
741 | /* Represents sPAPR hcall VIO devices */ | |
742 | ||
743 | static int spapr_vio_bridge_init(SysBusDevice *dev) | |
744 | { | |
745 | /* nothing */ | |
746 | return 0; | |
747 | } | |
748 | ||
999e12bb AL |
749 | static void spapr_vio_bridge_class_init(ObjectClass *klass, void *data) |
750 | { | |
39bffca2 AL |
751 | DeviceClass *dc = DEVICE_CLASS(klass); |
752 | SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); | |
999e12bb | 753 | |
39bffca2 AL |
754 | k->init = spapr_vio_bridge_init; |
755 | dc->no_user = 1; | |
999e12bb AL |
756 | } |
757 | ||
39bffca2 AL |
758 | static TypeInfo spapr_vio_bridge_info = { |
759 | .name = "spapr-vio-bridge", | |
760 | .parent = TYPE_SYS_BUS_DEVICE, | |
761 | .instance_size = sizeof(SysBusDevice), | |
762 | .class_init = spapr_vio_bridge_class_init, | |
4040ab72 DG |
763 | }; |
764 | ||
39bffca2 AL |
765 | static void vio_spapr_device_class_init(ObjectClass *klass, void *data) |
766 | { | |
767 | DeviceClass *k = DEVICE_CLASS(klass); | |
768 | k->init = spapr_vio_busdev_init; | |
769 | k->bus_info = &spapr_vio_bus_info; | |
770 | } | |
771 | ||
3954d33a AL |
772 | static TypeInfo spapr_vio_type_info = { |
773 | .name = TYPE_VIO_SPAPR_DEVICE, | |
774 | .parent = TYPE_DEVICE, | |
775 | .instance_size = sizeof(VIOsPAPRDevice), | |
776 | .abstract = true, | |
777 | .class_size = sizeof(VIOsPAPRDeviceClass), | |
39bffca2 | 778 | .class_init = vio_spapr_device_class_init, |
3954d33a AL |
779 | }; |
780 | ||
4040ab72 DG |
781 | static void spapr_vio_register_devices(void) |
782 | { | |
39bffca2 | 783 | type_register_static(&spapr_vio_bridge_info); |
3954d33a | 784 | type_register_static(&spapr_vio_type_info); |
4040ab72 DG |
785 | } |
786 | ||
787 | device_init(spapr_vio_register_devices) | |
788 | ||
789 | #ifdef CONFIG_FDT | |
05c19438 DG |
790 | static int compare_reg(const void *p1, const void *p2) |
791 | { | |
792 | VIOsPAPRDevice const *dev1, *dev2; | |
793 | ||
794 | dev1 = (VIOsPAPRDevice *)*(DeviceState **)p1; | |
795 | dev2 = (VIOsPAPRDevice *)*(DeviceState **)p2; | |
796 | ||
797 | if (dev1->reg < dev2->reg) { | |
798 | return -1; | |
799 | } | |
800 | if (dev1->reg == dev2->reg) { | |
801 | return 0; | |
802 | } | |
803 | ||
804 | /* dev1->reg > dev2->reg */ | |
805 | return 1; | |
806 | } | |
807 | ||
4040ab72 DG |
808 | int spapr_populate_vdevice(VIOsPAPRBus *bus, void *fdt) |
809 | { | |
05c19438 DG |
810 | DeviceState *qdev, **qdevs; |
811 | int i, num, ret = 0; | |
4040ab72 | 812 | |
05c19438 DG |
813 | /* Count qdevs on the bus list */ |
814 | num = 0; | |
d8bb00d6 | 815 | QTAILQ_FOREACH(qdev, &bus->bus.children, sibling) { |
05c19438 DG |
816 | num++; |
817 | } | |
818 | ||
819 | /* Copy out into an array of pointers */ | |
820 | qdevs = g_malloc(sizeof(qdev) * num); | |
821 | num = 0; | |
822 | QTAILQ_FOREACH(qdev, &bus->bus.children, sibling) { | |
823 | qdevs[num++] = qdev; | |
824 | } | |
825 | ||
826 | /* Sort the array */ | |
827 | qsort(qdevs, num, sizeof(qdev), compare_reg); | |
828 | ||
829 | /* Hack alert. Give the devices to libfdt in reverse order, we happen | |
830 | * to know that will mean they are in forward order in the tree. */ | |
831 | for (i = num - 1; i >= 0; i--) { | |
832 | VIOsPAPRDevice *dev = (VIOsPAPRDevice *)(qdevs[i]); | |
4040ab72 DG |
833 | |
834 | ret = vio_make_devnode(dev, fdt); | |
835 | ||
836 | if (ret < 0) { | |
05c19438 | 837 | goto out; |
4040ab72 DG |
838 | } |
839 | } | |
840 | ||
05c19438 DG |
841 | ret = 0; |
842 | out: | |
843 | free(qdevs); | |
844 | ||
845 | return ret; | |
4040ab72 | 846 | } |
68f3a94c DG |
847 | |
848 | int spapr_populate_chosen_stdout(void *fdt, VIOsPAPRBus *bus) | |
849 | { | |
850 | VIOsPAPRDevice *dev; | |
851 | char *name, *path; | |
852 | int ret, offset; | |
853 | ||
854 | dev = spapr_vty_get_default(bus); | |
855 | if (!dev) | |
856 | return 0; | |
857 | ||
858 | offset = fdt_path_offset(fdt, "/chosen"); | |
859 | if (offset < 0) { | |
860 | return offset; | |
861 | } | |
862 | ||
863 | name = vio_format_dev_name(dev); | |
864 | if (!name) { | |
865 | return -ENOMEM; | |
866 | } | |
867 | ||
868 | if (asprintf(&path, "/vdevice/%s", name) < 0) { | |
869 | path = NULL; | |
870 | ret = -ENOMEM; | |
871 | goto out; | |
872 | } | |
873 | ||
874 | ret = fdt_setprop_string(fdt, offset, "linux,stdout-path", path); | |
875 | out: | |
876 | free(name); | |
877 | free(path); | |
878 | ||
879 | return ret; | |
880 | } | |
4040ab72 | 881 | #endif /* CONFIG_FDT */ |