Olav Haugan | fbaeb88 | 2017-05-23 16:37:49 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2013-2014, 2016-2017, The Linux Foundation. All rights |
| 2 | * reserved. |
Laura Abbott | 130e1d0 | 2013-08-09 18:17:06 -0700 | [diff] [blame] | 3 | * |
| 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 Haugan | fbaeb88 | 2017-05-23 16:37:49 -0700 | [diff] [blame] | 16 | #include <linux/hw_random.h> |
Laura Abbott | 130e1d0 | 2013-08-09 18:17:06 -0700 | [diff] [blame] | 17 | #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 | |
| 26 | struct tz_prng_data { |
| 27 | uint8_t *out_buf; |
| 28 | uint32_t out_buf_sz; |
| 29 | } __packed; |
| 30 | |
| 31 | DEFINE_SCM_BUFFER(common_scm_buf) |
| 32 | #define RANDOM_BUFFER_SIZE PAGE_SIZE |
| 33 | char random_buffer[RANDOM_BUFFER_SIZE] __aligned(PAGE_SIZE); |
| 34 | |
| 35 | void __init init_random_pool(void) |
| 36 | { |
| 37 | struct tz_prng_data data; |
| 38 | int ret; |
| 39 | u32 resp; |
| 40 | struct scm_desc desc; |
Olav Haugan | bf5a41f | 2017-08-03 11:43:51 -0700 | [diff] [blame] | 41 | u64 bytes_received; |
Laura Abbott | 130e1d0 | 2013-08-09 18:17:06 -0700 | [diff] [blame] | 42 | |
| 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 Haugan | bf5a41f | 2017-08-03 11:43:51 -0700 | [diff] [blame] | 50 | if (!is_scm_armv8()) { |
Laura Abbott | 130e1d0 | 2013-08-09 18:17:06 -0700 | [diff] [blame] | 51 | 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 Haugan | bf5a41f | 2017-08-03 11:43:51 -0700 | [diff] [blame] | 55 | bytes_received = resp; |
| 56 | } else { |
Laura Abbott | 130e1d0 | 2013-08-09 18:17:06 -0700 | [diff] [blame] | 57 | ret = scm_call2(SCM_SIP_FNID(TZ_SVC_CRYPTO, PRNG_CMD_ID), |
| 58 | &desc); |
Olav Haugan | bf5a41f | 2017-08-03 11:43:51 -0700 | [diff] [blame] | 59 | bytes_received = desc.ret[0]; |
| 60 | } |
Laura Abbott | 130e1d0 | 2013-08-09 18:17:06 -0700 | [diff] [blame] | 61 | if (!ret) { |
Olav Haugan | d67723c | 2017-06-30 11:45:19 -0700 | [diff] [blame] | 62 | 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 Abbott | 130e1d0 | 2013-08-09 18:17:06 -0700 | [diff] [blame] | 66 | dmac_inv_range(random_buffer, random_buffer + |
| 67 | RANDOM_BUFFER_SIZE); |
Olav Haugan | d67723c | 2017-06-30 11:45:19 -0700 | [diff] [blame] | 68 | 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 Abbott | 130e1d0 | 2013-08-09 18:17:06 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |