2
0

exynos4210_rng.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Exynos4210 Pseudo Random Nubmer Generator Emulation
  3. *
  4. * Copyright (c) 2017 Krzysztof Kozlowski <krzk@kernel.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "hw/sysbus.h"
  21. #include "migration/vmstate.h"
  22. #include "qapi/error.h"
  23. #include "qemu/log.h"
  24. #include "qemu/guest-random.h"
  25. #include "qemu/module.h"
  26. #include "qom/object.h"
  27. #define DEBUG_EXYNOS_RNG 0
  28. #define DPRINTF(fmt, ...) \
  29. do { \
  30. if (DEBUG_EXYNOS_RNG) { \
  31. printf("exynos4210_rng: " fmt, ## __VA_ARGS__); \
  32. } \
  33. } while (0)
  34. #define TYPE_EXYNOS4210_RNG "exynos4210.rng"
  35. OBJECT_DECLARE_SIMPLE_TYPE(Exynos4210RngState, EXYNOS4210_RNG)
  36. /*
  37. * Exynos4220, PRNG, only polling mode is supported.
  38. */
  39. /* RNG_CONTROL_1 register bitfields, reset value: 0x0 */
  40. #define EXYNOS4210_RNG_CONTROL_1_PRNG 0x8
  41. #define EXYNOS4210_RNG_CONTROL_1_START_INIT BIT(4)
  42. /* RNG_STATUS register bitfields, reset value: 0x1 */
  43. #define EXYNOS4210_RNG_STATUS_PRNG_ERROR BIT(7)
  44. #define EXYNOS4210_RNG_STATUS_PRNG_DONE BIT(5)
  45. #define EXYNOS4210_RNG_STATUS_MSG_DONE BIT(4)
  46. #define EXYNOS4210_RNG_STATUS_PARTIAL_DONE BIT(3)
  47. #define EXYNOS4210_RNG_STATUS_PRNG_BUSY BIT(2)
  48. #define EXYNOS4210_RNG_STATUS_SEED_SETTING_DONE BIT(1)
  49. #define EXYNOS4210_RNG_STATUS_BUFFER_READY BIT(0)
  50. #define EXYNOS4210_RNG_STATUS_WRITE_MASK (EXYNOS4210_RNG_STATUS_PRNG_DONE \
  51. | EXYNOS4210_RNG_STATUS_MSG_DONE \
  52. | EXYNOS4210_RNG_STATUS_PARTIAL_DONE)
  53. #define EXYNOS4210_RNG_CONTROL_1 0x0
  54. #define EXYNOS4210_RNG_STATUS 0x10
  55. #define EXYNOS4210_RNG_SEED_IN 0x140
  56. #define EXYNOS4210_RNG_SEED_IN_OFFSET(n) (EXYNOS4210_RNG_SEED_IN + (n * 0x4))
  57. #define EXYNOS4210_RNG_PRNG 0x160
  58. #define EXYNOS4210_RNG_PRNG_OFFSET(n) (EXYNOS4210_RNG_PRNG + (n * 0x4))
  59. #define EXYNOS4210_RNG_PRNG_NUM 5
  60. #define EXYNOS4210_RNG_REGS_MEM_SIZE 0x200
  61. struct Exynos4210RngState {
  62. SysBusDevice parent_obj;
  63. MemoryRegion iomem;
  64. int32_t randr_value[EXYNOS4210_RNG_PRNG_NUM];
  65. /* bits from 0 to EXYNOS4210_RNG_PRNG_NUM if given seed register was set */
  66. uint32_t seed_set;
  67. /* Register values */
  68. uint32_t reg_control;
  69. uint32_t reg_status;
  70. };
  71. static bool exynos4210_rng_seed_ready(const Exynos4210RngState *s)
  72. {
  73. uint32_t mask = MAKE_64BIT_MASK(0, EXYNOS4210_RNG_PRNG_NUM);
  74. /* Return true if all the seed-set bits are set. */
  75. return (s->seed_set & mask) == mask;
  76. }
  77. static void exynos4210_rng_set_seed(Exynos4210RngState *s, unsigned int i,
  78. uint64_t val)
  79. {
  80. /*
  81. * We actually ignore the seed and always generate true random numbers.
  82. * Theoretically this should not match the device as Exynos has
  83. * a Pseudo Random Number Generator but testing shown that it always
  84. * generates random numbers regardless of the seed value.
  85. */
  86. s->seed_set |= BIT(i);
  87. /* If all seeds were written, update the status to reflect it */
  88. if (exynos4210_rng_seed_ready(s)) {
  89. s->reg_status |= EXYNOS4210_RNG_STATUS_SEED_SETTING_DONE;
  90. } else {
  91. s->reg_status &= ~EXYNOS4210_RNG_STATUS_SEED_SETTING_DONE;
  92. }
  93. }
  94. static void exynos4210_rng_run_engine(Exynos4210RngState *s)
  95. {
  96. Error *err = NULL;
  97. /* Seed set? */
  98. if ((s->reg_status & EXYNOS4210_RNG_STATUS_SEED_SETTING_DONE) == 0) {
  99. goto out;
  100. }
  101. /* PRNG engine chosen? */
  102. if ((s->reg_control & EXYNOS4210_RNG_CONTROL_1_PRNG) == 0) {
  103. goto out;
  104. }
  105. /* PRNG engine started? */
  106. if ((s->reg_control & EXYNOS4210_RNG_CONTROL_1_START_INIT) == 0) {
  107. goto out;
  108. }
  109. /* Get randoms */
  110. if (qemu_guest_getrandom(s->randr_value, sizeof(s->randr_value), &err)) {
  111. error_report_err(err);
  112. } else {
  113. /* Notify that PRNG is ready */
  114. s->reg_status |= EXYNOS4210_RNG_STATUS_PRNG_DONE;
  115. }
  116. out:
  117. /* Always clear start engine bit */
  118. s->reg_control &= ~EXYNOS4210_RNG_CONTROL_1_START_INIT;
  119. }
  120. static uint64_t exynos4210_rng_read(void *opaque, hwaddr offset,
  121. unsigned size)
  122. {
  123. Exynos4210RngState *s = (Exynos4210RngState *)opaque;
  124. uint32_t val = 0;
  125. assert(size == 4);
  126. switch (offset) {
  127. case EXYNOS4210_RNG_CONTROL_1:
  128. val = s->reg_control;
  129. break;
  130. case EXYNOS4210_RNG_STATUS:
  131. val = s->reg_status;
  132. break;
  133. case EXYNOS4210_RNG_PRNG_OFFSET(0):
  134. case EXYNOS4210_RNG_PRNG_OFFSET(1):
  135. case EXYNOS4210_RNG_PRNG_OFFSET(2):
  136. case EXYNOS4210_RNG_PRNG_OFFSET(3):
  137. case EXYNOS4210_RNG_PRNG_OFFSET(4):
  138. val = s->randr_value[(offset - EXYNOS4210_RNG_PRNG_OFFSET(0)) / 4];
  139. DPRINTF("returning random @0x%" HWADDR_PRIx ": 0x%" PRIx32 "\n",
  140. offset, val);
  141. break;
  142. default:
  143. qemu_log_mask(LOG_GUEST_ERROR,
  144. "%s: bad read offset 0x%" HWADDR_PRIx "\n",
  145. __func__, offset);
  146. }
  147. return val;
  148. }
  149. static void exynos4210_rng_write(void *opaque, hwaddr offset,
  150. uint64_t val, unsigned size)
  151. {
  152. Exynos4210RngState *s = (Exynos4210RngState *)opaque;
  153. assert(size == 4);
  154. switch (offset) {
  155. case EXYNOS4210_RNG_CONTROL_1:
  156. DPRINTF("RNG_CONTROL_1 = 0x%" PRIx64 "\n", val);
  157. s->reg_control = val;
  158. exynos4210_rng_run_engine(s);
  159. break;
  160. case EXYNOS4210_RNG_STATUS:
  161. /* For clearing status fields */
  162. s->reg_status &= ~EXYNOS4210_RNG_STATUS_WRITE_MASK;
  163. s->reg_status |= val & EXYNOS4210_RNG_STATUS_WRITE_MASK;
  164. break;
  165. case EXYNOS4210_RNG_SEED_IN_OFFSET(0):
  166. case EXYNOS4210_RNG_SEED_IN_OFFSET(1):
  167. case EXYNOS4210_RNG_SEED_IN_OFFSET(2):
  168. case EXYNOS4210_RNG_SEED_IN_OFFSET(3):
  169. case EXYNOS4210_RNG_SEED_IN_OFFSET(4):
  170. exynos4210_rng_set_seed(s,
  171. (offset - EXYNOS4210_RNG_SEED_IN_OFFSET(0)) / 4,
  172. val);
  173. break;
  174. default:
  175. qemu_log_mask(LOG_GUEST_ERROR,
  176. "%s: bad write offset 0x%" HWADDR_PRIx "\n",
  177. __func__, offset);
  178. }
  179. }
  180. static const MemoryRegionOps exynos4210_rng_ops = {
  181. .read = exynos4210_rng_read,
  182. .write = exynos4210_rng_write,
  183. .endianness = DEVICE_NATIVE_ENDIAN,
  184. };
  185. static void exynos4210_rng_reset(DeviceState *dev)
  186. {
  187. Exynos4210RngState *s = EXYNOS4210_RNG(dev);
  188. s->reg_control = 0;
  189. s->reg_status = EXYNOS4210_RNG_STATUS_BUFFER_READY;
  190. memset(s->randr_value, 0, sizeof(s->randr_value));
  191. s->seed_set = 0;
  192. }
  193. static void exynos4210_rng_init(Object *obj)
  194. {
  195. Exynos4210RngState *s = EXYNOS4210_RNG(obj);
  196. SysBusDevice *dev = SYS_BUS_DEVICE(obj);
  197. memory_region_init_io(&s->iomem, obj, &exynos4210_rng_ops, s,
  198. TYPE_EXYNOS4210_RNG, EXYNOS4210_RNG_REGS_MEM_SIZE);
  199. sysbus_init_mmio(dev, &s->iomem);
  200. }
  201. static const VMStateDescription exynos4210_rng_vmstate = {
  202. .name = TYPE_EXYNOS4210_RNG,
  203. .version_id = 1,
  204. .minimum_version_id = 1,
  205. .fields = (VMStateField[]) {
  206. VMSTATE_INT32_ARRAY(randr_value, Exynos4210RngState,
  207. EXYNOS4210_RNG_PRNG_NUM),
  208. VMSTATE_UINT32(seed_set, Exynos4210RngState),
  209. VMSTATE_UINT32(reg_status, Exynos4210RngState),
  210. VMSTATE_UINT32(reg_control, Exynos4210RngState),
  211. VMSTATE_END_OF_LIST()
  212. }
  213. };
  214. static void exynos4210_rng_class_init(ObjectClass *klass, void *data)
  215. {
  216. DeviceClass *dc = DEVICE_CLASS(klass);
  217. dc->reset = exynos4210_rng_reset;
  218. dc->vmsd = &exynos4210_rng_vmstate;
  219. }
  220. static const TypeInfo exynos4210_rng_info = {
  221. .name = TYPE_EXYNOS4210_RNG,
  222. .parent = TYPE_SYS_BUS_DEVICE,
  223. .instance_size = sizeof(Exynos4210RngState),
  224. .instance_init = exynos4210_rng_init,
  225. .class_init = exynos4210_rng_class_init,
  226. };
  227. static void exynos4210_rng_register(void)
  228. {
  229. type_register_static(&exynos4210_rng_info);
  230. }
  231. type_init(exynos4210_rng_register)