blob: 06601dd0b1b470ea486d09a27ef6b9f8f5e1f58f [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;
41
42 data.out_buf = (uint8_t *) virt_to_phys(random_buffer);
43 desc.args[0] = (unsigned long) data.out_buf;
44 desc.args[1] = data.out_buf_sz = SZ_512;
45 desc.arginfo = SCM_ARGS(2, SCM_RW, SCM_VAL);
46
47 dmac_flush_range(random_buffer, random_buffer + RANDOM_BUFFER_SIZE);
48
49 if (!is_scm_armv8())
50 ret = scm_call_noalloc(TZ_SVC_CRYPTO, PRNG_CMD_ID, &data,
51 sizeof(data), &resp, sizeof(resp),
52 common_scm_buf,
53 SCM_BUFFER_SIZE(common_scm_buf));
54 else
55 ret = scm_call2(SCM_SIP_FNID(TZ_SVC_CRYPTO, PRNG_CMD_ID),
56 &desc);
57
58 if (!ret) {
Olav Haugand67723c2017-06-30 11:45:19 -070059 u64 bytes_received = desc.ret[0];
60
61 if (bytes_received != SZ_512)
62 pr_warn("Did not receive the expected number of bytes from PRNG: %llu\n",
63 bytes_received);
64
Laura Abbott130e1d02013-08-09 18:17:06 -070065 dmac_inv_range(random_buffer, random_buffer +
66 RANDOM_BUFFER_SIZE);
Olav Haugand67723c2017-06-30 11:45:19 -070067 bytes_received = (bytes_received <= RANDOM_BUFFER_SIZE) ?
68 bytes_received : RANDOM_BUFFER_SIZE;
69 add_hwgenerator_randomness(random_buffer, bytes_received,
70 bytes_received << 3);
Laura Abbott130e1d02013-08-09 18:17:06 -070071 }
72}
73