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; |
| 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 Haugan | d67723c | 2017-06-30 11:45:19 -0700 | [diff] [blame^] | 59 | 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 Abbott | 130e1d0 | 2013-08-09 18:17:06 -0700 | [diff] [blame] | 65 | dmac_inv_range(random_buffer, random_buffer + |
| 66 | RANDOM_BUFFER_SIZE); |
Olav Haugan | d67723c | 2017-06-30 11:45:19 -0700 | [diff] [blame^] | 67 | 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 Abbott | 130e1d0 | 2013-08-09 18:17:06 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |