macio-ide: add to storage category
[qemu/ar7.git] / hw / ppc / spapr_rng.c
blobed43d5e042213f66b1fd3abed155f35cdc152865
1 /*
2 * QEMU sPAPR random number generator "device" for H_RANDOM hypercall
4 * Copyright 2015 Thomas Huth, Red Hat Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License,
9 * or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/error-report.h"
21 #include "sysemu/sysemu.h"
22 #include "sysemu/device_tree.h"
23 #include "sysemu/rng.h"
24 #include "hw/ppc/spapr.h"
25 #include "kvm_ppc.h"
27 #define SPAPR_RNG(obj) \
28 OBJECT_CHECK(sPAPRRngState, (obj), TYPE_SPAPR_RNG)
30 struct sPAPRRngState {
31 /*< private >*/
32 DeviceState ds;
33 RngBackend *backend;
34 bool use_kvm;
36 typedef struct sPAPRRngState sPAPRRngState;
38 struct HRandomData {
39 QemuSemaphore sem;
40 union {
41 uint64_t v64;
42 uint8_t v8[8];
43 } val;
44 int received;
46 typedef struct HRandomData HRandomData;
48 /* Callback function for the RngBackend */
49 static void random_recv(void *dest, const void *src, size_t size)
51 HRandomData *hrdp = dest;
53 if (src && size > 0) {
54 assert(size + hrdp->received <= sizeof(hrdp->val.v8));
55 memcpy(&hrdp->val.v8[hrdp->received], src, size);
56 hrdp->received += size;
59 qemu_sem_post(&hrdp->sem);
62 /* Handler for the H_RANDOM hypercall */
63 static target_ulong h_random(PowerPCCPU *cpu, sPAPRMachineState *spapr,
64 target_ulong opcode, target_ulong *args)
66 sPAPRRngState *rngstate;
67 HRandomData hrdata;
69 rngstate = SPAPR_RNG(object_resolve_path_type("", TYPE_SPAPR_RNG, NULL));
71 if (!rngstate || !rngstate->backend) {
72 return H_HARDWARE;
75 qemu_sem_init(&hrdata.sem, 0);
76 hrdata.val.v64 = 0;
77 hrdata.received = 0;
79 qemu_mutex_unlock_iothread();
80 while (hrdata.received < 8) {
81 rng_backend_request_entropy(rngstate->backend, 8 - hrdata.received,
82 random_recv, &hrdata);
83 qemu_sem_wait(&hrdata.sem);
85 qemu_mutex_lock_iothread();
87 qemu_sem_destroy(&hrdata.sem);
88 args[0] = hrdata.val.v64;
90 return H_SUCCESS;
93 static void spapr_rng_instance_init(Object *obj)
95 sPAPRRngState *rngstate = SPAPR_RNG(obj);
97 if (object_resolve_path_type("", TYPE_SPAPR_RNG, NULL) != NULL) {
98 error_report("spapr-rng can not be instantiated twice!");
99 return;
102 object_property_add_link(obj, "rng", TYPE_RNG_BACKEND,
103 (Object **)&rngstate->backend,
104 object_property_allow_set_link,
105 OBJ_PROP_LINK_UNREF_ON_RELEASE, NULL);
106 object_property_set_description(obj, "rng",
107 "ID of the random number generator backend",
108 NULL);
111 static void spapr_rng_realize(DeviceState *dev, Error **errp)
114 sPAPRRngState *rngstate = SPAPR_RNG(dev);
116 if (rngstate->use_kvm) {
117 if (kvmppc_enable_hwrng() == 0) {
118 return;
121 * If user specified both, use-kvm and a backend, we fall back to
122 * the backend now. If not, provide an appropriate error message.
124 if (!rngstate->backend) {
125 error_setg(errp, "Could not initialize in-kernel H_RANDOM call!");
126 return;
130 if (rngstate->backend) {
131 spapr_register_hypercall(H_RANDOM, h_random);
132 } else {
133 error_setg(errp, "spapr-rng needs an RNG backend!");
137 int spapr_rng_populate_dt(void *fdt)
139 int node;
140 int ret;
142 node = qemu_fdt_add_subnode(fdt, "/ibm,platform-facilities");
143 if (node <= 0) {
144 return -1;
146 ret = fdt_setprop_string(fdt, node, "device_type",
147 "ibm,platform-facilities");
148 ret |= fdt_setprop_cell(fdt, node, "#address-cells", 0x1);
149 ret |= fdt_setprop_cell(fdt, node, "#size-cells", 0x0);
151 node = fdt_add_subnode(fdt, node, "ibm,random-v1");
152 if (node <= 0) {
153 return -1;
155 ret |= fdt_setprop_string(fdt, node, "compatible", "ibm,random");
157 return ret ? -1 : 0;
160 static Property spapr_rng_properties[] = {
161 DEFINE_PROP_BOOL("use-kvm", sPAPRRngState, use_kvm, false),
162 DEFINE_PROP_END_OF_LIST(),
165 static void spapr_rng_class_init(ObjectClass *oc, void *data)
167 DeviceClass *dc = DEVICE_CLASS(oc);
169 dc->realize = spapr_rng_realize;
170 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
171 dc->props = spapr_rng_properties;
174 static const TypeInfo spapr_rng_info = {
175 .name = TYPE_SPAPR_RNG,
176 .parent = TYPE_DEVICE,
177 .instance_size = sizeof(sPAPRRngState),
178 .instance_init = spapr_rng_instance_init,
179 .class_init = spapr_rng_class_init,
182 static void spapr_rng_register_type(void)
184 type_register_static(&spapr_rng_info);
186 type_init(spapr_rng_register_type)