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