blob: 641f70ee9f1f5e2377f0484b2dbbbe2eb2c3a821 [file] [log] [blame]
Olav Hauganfbaeb882017-05-23 16:37:49 -07001/* Copyright (c) 2013-2014, 2016-2017, The Linux Foundation. All rights
2 * reserved.
Laura Abbott130e1d02013-08-09 18:17:06 -07003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
14
15#include <linux/kernel.h>
Olav Hauganfbaeb882017-05-23 16:37:49 -070016#include <linux/hw_random.h>
Laura Abbott130e1d02013-08-09 18:17:06 -070017#include <linux/io.h>
18
19#include <soc/qcom/scm.h>
20
21#include <asm/cacheflush.h>
22
23#define TZ_SVC_CRYPTO 10
24#define PRNG_CMD_ID 0x01
25
26struct tz_prng_data {
27 uint8_t *out_buf;
28 uint32_t out_buf_sz;
29} __packed;
30
31DEFINE_SCM_BUFFER(common_scm_buf)
32#define RANDOM_BUFFER_SIZE PAGE_SIZE
33char random_buffer[RANDOM_BUFFER_SIZE] __aligned(PAGE_SIZE);
34
35void __init init_random_pool(void)
36{
37 struct tz_prng_data data;
38 int ret;
39 u32 resp;
40 struct scm_desc desc;
Olav Hauganbf5a41f2017-08-03 11:43:51 -070041 u64 bytes_received;
Laura Abbott130e1d02013-08-09 18:17:06 -070042
43 data.out_buf = (uint8_t *) virt_to_phys(random_buffer);
44 desc.args[0] = (unsigned long) data.out_buf;
45 desc.args[1] = data.out_buf_sz = SZ_512;
46 desc.arginfo = SCM_ARGS(2, SCM_RW, SCM_VAL);
47
48 dmac_flush_range(random_buffer, random_buffer + RANDOM_BUFFER_SIZE);
49
Olav Hauganbf5a41f2017-08-03 11:43:51 -070050 if (!is_scm_armv8()) {
Laura Abbott130e1d02013-08-09 18:17:06 -070051 ret = scm_call_noalloc(TZ_SVC_CRYPTO, PRNG_CMD_ID, &data,
52 sizeof(data), &resp, sizeof(resp),
53 common_scm_buf,
54 SCM_BUFFER_SIZE(common_scm_buf));
Olav Hauganbf5a41f2017-08-03 11:43:51 -070055 bytes_received = resp;
56 } else {
Laura Abbott130e1d02013-08-09 18:17:06 -070057 ret = scm_call2(SCM_SIP_FNID(TZ_SVC_CRYPTO, PRNG_CMD_ID),
58 &desc);
Olav Hauganbf5a41f2017-08-03 11:43:51 -070059 bytes_received = desc.ret[0];
60 }
Laura Abbott130e1d02013-08-09 18:17:06 -070061 if (!ret) {
Olav Haugand67723c2017-06-30 11:45:19 -070062 if (bytes_received != SZ_512)
63 pr_warn("Did not receive the expected number of bytes from PRNG: %llu\n",
64 bytes_received);
65
Laura Abbott130e1d02013-08-09 18:17:06 -070066 dmac_inv_range(random_buffer, random_buffer +
67 RANDOM_BUFFER_SIZE);
Olav Haugand67723c2017-06-30 11:45:19 -070068 bytes_received = (bytes_received <= RANDOM_BUFFER_SIZE) ?
69 bytes_received : RANDOM_BUFFER_SIZE;
70 add_hwgenerator_randomness(random_buffer, bytes_received,
71 bytes_received << 3);
Laura Abbott130e1d02013-08-09 18:17:06 -070072 }
73}
74