blob: d31fbfceb85f0851040967003c65d85861808ee8 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * linux/drivers/char/hw_random/asr-rng.c - Random Number Generator driver
4 *
5 * Copyright (C) 2023 ASR Micro Limited
6 *
7 */
8
9#include <linux/module.h>
10#include <linux/kernel.h>
11#include <linux/platform_device.h>
12#include <linux/of.h>
13#include <linux/clk.h>
14#include <linux/hw_random.h>
15#include <linux/io.h>
16#include <linux/slab.h>
17#include <linux/sched.h>
18#include <linux/fs.h>
19#ifdef CONFIG_TEE
20#include <linux/tee_drv.h>
21#endif
22
23#include "asr-rng-optee.h"
24
25static struct teec_uuid pta_rng_uuid = ASR_RNG_ACCESS_UUID;
26
27static void asrrng_uuid_to_octets(uint8_t d[TEE_IOCTL_UUID_LEN], struct teec_uuid *s)
28{
29 d[0] = s->timeLow >> 24;
30 d[1] = s->timeLow >> 16;
31 d[2] = s->timeLow >> 8;
32 d[3] = s->timeLow;
33 d[4] = s->timeMid >> 8;
34 d[5] = s->timeMid;
35 d[6] = s->timeHiAndVersion >> 8;
36 d[7] = s->timeHiAndVersion;
37 memcpy(d + 8, s->clockSeqAndNode, sizeof(s->clockSeqAndNode));
38}
39
40static int asrrng_tee_match_cb(struct tee_ioctl_version_data *ver, const void *data)
41{
42 return 1;
43}
44
45static int asrrng_optee_open_ta(struct asrrng_tee_context *ctx, struct teec_uuid *uuid)
46{
47 struct tee_ioctl_open_session_arg open_session_arg;
48 int ret;
49
50 if (ctx == NULL)
51 return -EINVAL;
52
53 ctx->session = 0;
54 ctx->tee_ctx = tee_client_open_context(NULL, asrrng_tee_match_cb, NULL, NULL);
55 if (IS_ERR(ctx->tee_ctx)) {
56 ret = PTR_ERR(ctx->tee_ctx);
57 ctx->tee_ctx = NULL;
58 return ret;
59 }
60
61 memset(&open_session_arg, 0x0, sizeof(struct tee_ioctl_open_session_arg));
62 asrrng_uuid_to_octets(open_session_arg.uuid, uuid);
63 open_session_arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC;
64 open_session_arg.num_params = 0;
65 ret = tee_client_open_session(ctx->tee_ctx, &open_session_arg, NULL);
66 if (ret != 0) {
67 goto err_exit;
68 } else if (open_session_arg.ret != 0) {
69 ret = -EIO;
70 goto err_exit;
71 }
72
73 ctx->session = open_session_arg.session;
74
75 return ret;
76err_exit:
77 tee_client_close_context(ctx->tee_ctx);
78 ctx->tee_ctx = NULL;
79 return ret;
80}
81
82static int asrrng_optee_close_ta(struct asrrng_tee_context *ctx)
83{
84 int ret;
85
86 if (ctx == NULL)
87 return -EINVAL;
88
89 ret = tee_client_close_session(ctx->tee_ctx, ctx->session);
90
91 tee_client_close_context(ctx->tee_ctx);
92
93 return ret;
94}
95
96static int asrrng_optee_acquire_ta(struct teec_uuid *uuid, u32 cmd, void *buff, size_t len, size_t *outlen)
97{
98 struct tee_ioctl_invoke_arg invoke_arg;
99 struct tee_param params;
100 struct asrrng_tee_context asrrng_tee_ctx;
101 struct tee_shm *shm;
102 int ret = 0;
103 size_t size;
104 char *ma = NULL;
105
106 ret = asrrng_optee_open_ta(&asrrng_tee_ctx, uuid);
107 if (ret != 0) {
108 return ret;
109 }
110
111 memset(&invoke_arg, 0x0, sizeof(struct tee_ioctl_invoke_arg));
112 invoke_arg.func = cmd;
113 invoke_arg.session = asrrng_tee_ctx.session;
114 invoke_arg.num_params = 1;
115
116 shm = tee_shm_alloc(asrrng_tee_ctx.tee_ctx, len, TEE_SHM_MAPPED | TEE_SHM_DMA_BUF);
117 if (!shm) {
118 ret = -EINVAL;
119 goto exit;
120 }
121
122 params.attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
123 params.u.memref.shm_offs = 0;
124
125 params.u.memref.size = len;
126 params.u.memref.shm = shm;
127
128 ret = tee_client_invoke_func(asrrng_tee_ctx.tee_ctx, &invoke_arg, &params);
129 if (ret != 0) {
130 goto free_shm;
131 } else if (invoke_arg.ret != 0) {
132 ret = -EIO;
133 goto free_shm;
134 }
135
136 size = (params.u.memref.size > len) ? (int)len: (int)params.u.memref.size;
137 ma = tee_shm_get_va(shm, 0);
138 memcpy(buff, ma, size);
139
140 if (outlen)
141 *outlen = size;
142
143free_shm:
144 tee_shm_free(shm);
145exit:
146 asrrng_optee_close_ta(&asrrng_tee_ctx);
147 return ret;
148}
149
150static int asr_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
151{
152 int ret = 0;
153 size_t readsize;
154 size_t size = max < 4096 ? max : 4096;
155
156 (void)wait;
157
158 ret = asrrng_optee_acquire_ta(&pta_rng_uuid, ASR_RNG_GET_DATA,
159 data, size, &readsize);
160
161 if (!ret)
162 return readsize;
163
164 return 0;
165}
166
167static int asr_rng_probe(struct platform_device *pdev)
168{
169 struct asr_rng *prng;
170 struct device *dev = &pdev->dev;
171 int err = 0;
172
173 prng = devm_kzalloc(&pdev->dev, sizeof(*prng), GFP_KERNEL);
174 if (prng == NULL)
175 return -ENOMEM;
176
177 prng->dev = dev;
178 platform_set_drvdata(pdev, prng);
179
180 prng->rng.name = "asr";
181 prng->rng.read = asr_rng_read;
182 prng->rng.quality = 1000;
183
184 err = hwrng_register(&prng->rng);
185 if (err) {
186 dev_err(dev, "failed to register asr_rng!\n");
187 goto res_err;
188 }
189
190 dev_info(dev, "H/W RNG is initialized\n");
191 return 0;
192
193res_err:
194 devm_kfree(dev, prng);
195 dev_err(dev, "initialization failed.\n");
196
197 return err;
198}
199
200static int asr_rng_remove(struct platform_device *pdev)
201{
202 struct asr_rng *prng;
203
204 prng = platform_get_drvdata(pdev);
205 if (!prng) {
206 return -ENODEV;
207 }
208 hwrng_unregister(&prng->rng);
209
210 devm_kfree(prng->dev, prng);
211
212 return 0;
213}
214
215#if defined(CONFIG_OF)
216static const struct of_device_id asr_rng_dt_ids[] = {
217 { .compatible = "asr,asr-hwrng" },
218 { /* sentinel */ }
219};
220MODULE_DEVICE_TABLE(of, asr_rng_dt_ids);
221#endif
222
223static struct platform_driver asr_rng_driver = {
224 .probe = asr_rng_probe,
225 .remove = asr_rng_remove,
226 .driver = {
227 .name = "asr_rng",
228 .of_match_table = of_match_ptr(asr_rng_dt_ids),
229 },
230};
231
232static int __init asr_random_init(void)
233{
234 int ret;
235
236 ret = platform_driver_register(&asr_rng_driver);
237
238 return ret;
239}
240
241device_initcall_sync(asr_random_init);
242
243
244MODULE_LICENSE("GPL v2");
245MODULE_AUTHOR("Yu Zhang <yuzhang@asrmicro.com>");
246MODULE_AUTHOR("Wang Yonggan <wangyonggan@asrmicro.com>");
247MODULE_DESCRIPTION("ASR H/W RNG driver with optee-os");