summaryrefstree
path: root/kernel/drivers/char/hw_random/exyswd-rng.c
blob: d1735baa5574f06776de915a2bec71472b0d0dbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 * exyswd-rng.c - Random Number Generator driver for the exynos
 *
 * Copyright (C) 2013 Samsung Electronics
 * Sehee Kim <sehi.kim@samsung.com>
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
 *
 */

#include <linux/hw_random.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/io.h>
#include <linux/delay.h>
#include <linux/spinlock.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/smc.h>

#define HWRNG_RET_OK			0
#define HWRNG_RET_INVALID_ERROR		1
#define HWRNG_RET_RETRY_ERROR		2
#define HWRNG_RET_INVALID_FLAG_ERROR	3
#define HWRNG_RET_TEST_ERROR		4
#define HWRNG_RET_START_UP_TEST_DONE	5

#define EXYRNG_MAX_FAILURES		25
#define EXYRNG_START_UP_SIZE		(4096 + 1)
#define EXYRNG_RETRY_MAX_COUNT		1000
#define EXYRNG_START_UP_TEST_MAX_RETRY	2

uint32_t hwrng_read_flag;
static struct hwrng rng;

spinlock_t hwrandom_lock;
#if defined(CONFIG_EXYRNG_FAIL_POLICY_DISABLE)
static int hwrng_disabled;
#endif
static int start_up_test;

#ifdef CONFIG_EXYRNG_DEBUG
#define exyrng_debug(args...)	printk(KERN_INFO args)
#else
#define exyrng_debug(args...)
#endif

void exynos_swd_test_fail(void)
{
#if defined(CONFIG_EXYRNG_FAIL_POLICY_DISABLE)
	hwrng_disabled = 1;
	printk("[ExyRNG] disabled for test failures\n");
#else /* defined(CONFIG_EXYRNG_POLICY_RESET) */
	panic("[ExyRNG] It failed to health tests. It means that it detects "
	"the malfunction of TRNG(HW) which generates random numbers. If it "
	"doesn't offer enough entropy, it should not be used. The system "
	"reset could be a way to solve it. The health tests are designed "
	"to have the false positive rate of approximately once per billion "
	"based on min-entropy of TRNG.\n");
#endif
}

static int exynos_swd_startup_test(void)
{
	uint32_t start_up_size;
	uint32_t retry_cnt;
	uint32_t test_cnt;
	int ret = HWRNG_RET_OK;

	start_up_size = EXYRNG_START_UP_SIZE;

	retry_cnt = 0;
	test_cnt = 1;
	while (start_up_size) {
		ret = exynos_smc(SMC_CMD_RANDOM, HWRNG_GET_DATA, 1, 0);
		if (ret == HWRNG_RET_RETRY_ERROR) {
			if (retry_cnt++ > EXYRNG_RETRY_MAX_COUNT) {
				printk("[ExyRNG] exceed retry in start-up test\n");
				break;
			}
			usleep_range(50, 100);
			continue;
		}

		if (ret == HWRNG_RET_TEST_ERROR) {
#ifndef CONFIG_EXYRNG_USE_CRYPTOMANAGER
			if (test_cnt < EXYRNG_START_UP_TEST_MAX_RETRY) {
				start_up_size = EXYRNG_START_UP_SIZE;
				test_cnt++;
				printk("[ExyRNG] It performs start-up test "
				"again to detect the malfunction of TRNG with "
				"accuracy\n");
				continue;
			}
#endif
			exynos_swd_test_fail();
			return -EFAULT;
		}

		/* start-up test is performed already */
		if (ret == HWRNG_RET_START_UP_TEST_DONE) {
			ret = HWRNG_RET_OK;
			exyrng_debug("[ExyRNG] start-up test is already done\n");
			break;
		}

		if (ret != HWRNG_RET_OK) {
			exyrng_debug("[ExyRNG] failed to get random\n");
			return -EFAULT;
		}

		if (start_up_size >= 32)
			start_up_size -= 32;
		else
			start_up_size = 0;

		retry_cnt = 0;
	}

	return ret;
}

static int exynos_swd_read(struct hwrng *rng, void *data, size_t max, bool wait)
{
	uint32_t *read_buf = data;
	uint32_t read_size = max;
	uint32_t r_data[2];
	unsigned long flag;
	uint32_t retry_cnt;
	int ret = HWRNG_RET_OK;

	register u64 reg0 __asm__("x0");
	register u64 reg1 __asm__("x1");
	register u64 reg2 __asm__("x2");
	register u64 reg3 __asm__("x3");

#if defined(CONFIG_EXYRNG_FAIL_POLICY_DISABLE)
	if (hwrng_disabled)
		return -EPERM;
#endif

	reg0 = 0;
	reg1 = 0;
	reg2 = 0;
	reg3 = 0;

	retry_cnt = 0;
	do {
		spin_lock_irqsave(&hwrandom_lock, flag);
		if (hwrng_read_flag == 0) {
			ret = exynos_smc(SMC_CMD_RANDOM, HWRNG_INIT, 0, 0);
			if (ret == HWRNG_RET_OK)
				hwrng_read_flag = 1;
			spin_unlock_irqrestore(&hwrandom_lock, flag);

			if (ret == HWRNG_RET_RETRY_ERROR) {
				if (retry_cnt++ > EXYRNG_RETRY_MAX_COUNT) {
					printk("[ExyRNG] exceed retry in init\n");
					break;
				}
				usleep_range(50, 100);
			}
		} else {
			spin_unlock_irqrestore(&hwrandom_lock, flag);
			break;
		}
	} while (ret == HWRNG_RET_RETRY_ERROR);
	if (ret != HWRNG_RET_OK) {
		msleep(1);
		return -EFAULT;
	}

	if (start_up_test) {
		ret = exynos_swd_startup_test();
		if (ret != HWRNG_RET_OK)
			goto out;

		start_up_test = 0;
		printk("[ExyRNG] passed the start-up test\n");
	}

	retry_cnt = 0;
	while (read_size) {
		spin_lock_irqsave(&hwrandom_lock, flag);
		ret = exynos_smc(SMC_CMD_RANDOM, HWRNG_GET_DATA, 0, 0);
		__asm__ volatile(
			"\t"
			: "+r"(reg0), "+r"(reg1), "+r"(reg2), "+r"(reg3)
		);
		r_data[0] = (uint32_t)reg2;
		r_data[1] = (uint32_t)reg3;
		spin_unlock_irqrestore(&hwrandom_lock, flag);

		if (ret == HWRNG_RET_RETRY_ERROR) {
			if (retry_cnt++ > EXYRNG_RETRY_MAX_COUNT) {
				ret = -EFAULT;
				printk("[ExyRNG] exceed retry in read\n");
				goto out;
			}
			usleep_range(50, 100);
			continue;
		}

		if (ret == HWRNG_RET_TEST_ERROR) {
			exyrng_debug("[ExyRNG] failed to continuous test\n");
			ret = -EAGAIN;
			goto out;
		}

		if (ret != HWRNG_RET_OK) {
			ret = -EFAULT;
			goto out;
		}

		*(uint32_t*)(read_buf++) = r_data[0];
		*(uint32_t*)(read_buf++) = r_data[1];

		read_size -= 8;
		retry_cnt = 0;
	}

	ret = max;

out:
	retry_cnt = 0;
	do {
		spin_lock_irqsave(&hwrandom_lock, flag);
		if (!exynos_smc(SMC_CMD_RANDOM, HWRNG_EXIT, 0, 0)) {
			hwrng_read_flag = 0;
			spin_unlock_irqrestore(&hwrandom_lock, flag);
			break;
		}
		spin_unlock_irqrestore(&hwrandom_lock, flag);

		if (retry_cnt++ > EXYRNG_RETRY_MAX_COUNT) {
			printk("[ExyRNG] exceed retry in exit\n");
			break;
		}
		usleep_range(50, 100);
	} while(1);

	return ret;
}

static int exyswd_rng_probe(struct platform_device *pdev)
{
	int ret;

	rng.name = "exyswd_rng";
	rng.read = exynos_swd_read;
	rng.quality = 500;

	spin_lock_init(&hwrandom_lock);
#if defined(CONFIG_EXYRNG_FIPS_COMPLIANCE)
	start_up_test = 1;
#endif

	ret = hwrng_register(&rng);
	if (ret)
		return ret;

	printk(KERN_INFO "ExyRNG: hwrng registered\n");

	return 0;
}

static int exyswd_rng_remove(struct platform_device *pdev)
{
	hwrng_unregister(&rng);

	return 0;
}

#if defined(CONFIG_PM_SLEEP) || defined(CONFIG_PM_RUNTIME)
static int exyswd_rng_suspend(struct device *dev)
{
	unsigned long flag;
	int ret = HWRNG_RET_OK;

	spin_lock_irqsave(&hwrandom_lock, flag);
	if (hwrng_read_flag) {
		ret = exynos_smc(SMC_CMD_RANDOM, HWRNG_EXIT, 0, 0);
		if (ret != HWRNG_RET_OK)
			printk("[ExyRNG] failed to enter suspend with %d\n", ret);
	}
	spin_unlock_irqrestore(&hwrandom_lock, flag);

	return ret;
}

static int exyswd_rng_resume(struct device *dev)
{
	unsigned long flag;
	int ret = HWRNG_RET_OK;

	spin_lock_irqsave(&hwrandom_lock, flag);
#if defined(CONFIG_EXYRNG_FIPS_COMPLIANCE)
	exynos_smc(SMC_CMD_RANDOM, HWRNG_RESUME, 0, 0);
#endif
	if (hwrng_read_flag) {
		ret = exynos_smc(SMC_CMD_RANDOM, HWRNG_INIT, 0, 0);
		if (ret != HWRNG_RET_OK)
			printk("[ExyRNG] failed to resume with %d\n", ret);
	}
	spin_unlock_irqrestore(&hwrandom_lock, flag);

	return ret;
}
#endif

static UNIVERSAL_DEV_PM_OPS(exyswd_rng_pm_ops, exyswd_rng_suspend, exyswd_rng_resume, NULL);

static struct platform_driver exyswd_rng_driver = {
	.probe		= exyswd_rng_probe,
	.remove		= exyswd_rng_remove,
	.driver		= {
		.name	= "exyswd_rng",
		.owner	= THIS_MODULE,
		.pm     = &exyswd_rng_pm_ops,
	},
};

static struct platform_device exyswd_rng_device = {
	.name = "exyswd_rng",
	.id = -1,
};

static int __init exyswd_rng_init(void)
{
	int ret;

	ret = platform_device_register(&exyswd_rng_device);
	if (ret)
		return ret;

	ret = platform_driver_register(&exyswd_rng_driver);
	if (ret) {
		platform_device_unregister(&exyswd_rng_device);
		return ret;
	}

	printk(KERN_INFO "ExyRNG driver, (c) 2014 Samsung Electronics\n");

	return 0;
}

static void __exit exyswd_rng_exit(void)
{
	platform_driver_unregister(&exyswd_rng_driver);
	platform_device_unregister(&exyswd_rng_device);
}

module_init(exyswd_rng_init);
module_exit(exyswd_rng_exit);

MODULE_DESCRIPTION("EXYNOS H/W Random Number Generator driver");
MODULE_AUTHOR("Sehee Kim <sehi.kim@samsung.com>");
MODULE_LICENSE("GPL");