msm_rng.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2011-2013, 2015, 2017-2021 The Linux Foundation. All rights
  4. * reserved.
  5. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/device.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/hw_random.h>
  13. #include <linux/clk.h>
  14. #include <linux/slab.h>
  15. #include <linux/io.h>
  16. #include <linux/err.h>
  17. #include <linux/types.h>
  18. #include <linux/of.h>
  19. #include <linux/qrng.h>
  20. #include <linux/fs.h>
  21. #include <linux/cdev.h>
  22. #include <linux/delay.h>
  23. #include <linux/crypto.h>
  24. #include <crypto/internal/rng.h>
  25. #include <linux/interconnect.h>
  26. #include <linux/sched/signal.h>
  27. #define DRIVER_NAME "msm_rng"
  28. /* Device specific register offsets */
  29. #define PRNG_DATA_OUT_OFFSET 0x0000
  30. #define PRNG_STATUS_OFFSET 0x0004
  31. #define PRNG_LFSR_CFG_OFFSET 0x0100
  32. #define PRNG_CONFIG_OFFSET 0x0104
  33. /* Device specific register masks and config values */
  34. #define PRNG_LFSR_CFG_MASK 0xFFFF0000
  35. #define PRNG_LFSR_CFG_CLOCKS 0x0000DDDD
  36. #define PRNG_CONFIG_MASK 0xFFFFFFFD
  37. #define PRNG_HW_ENABLE 0x00000002
  38. #define MAX_HW_FIFO_DEPTH 16 /* FIFO is 16 words deep */
  39. #define MAX_HW_FIFO_SIZE (MAX_HW_FIFO_DEPTH * 4) /* FIFO is 32 bits wide */
  40. #define RETRY_MAX_CNT 5 /* max retry times to read register */
  41. #define RETRY_DELAY_INTERVAL 440 /* retry delay interval in us */
  42. struct msm_rng_device {
  43. struct platform_device *pdev;
  44. void __iomem *base;
  45. struct clk *prng_clk;
  46. struct mutex rng_lock;
  47. struct icc_path *icc_path;
  48. };
  49. static struct msm_rng_device msm_rng_device_info;
  50. static struct msm_rng_device *msm_rng_dev_cached;
  51. static struct mutex cached_rng_lock;
  52. static long msm_rng_ioctl(struct file *filp, unsigned int cmd,
  53. unsigned long arg)
  54. {
  55. long ret = 0;
  56. switch (cmd) {
  57. case QRNG_IOCTL_RESET_BUS_BANDWIDTH:
  58. pr_debug("calling msm_rng_bus_scale(LOW)\n");
  59. ret = icc_set_bw(msm_rng_device_info.icc_path, 0, 0);
  60. if (ret)
  61. pr_err("failed qrng_reset_bus_bw, ret = %ld\n", ret);
  62. break;
  63. default:
  64. pr_err("Unsupported IOCTL call\n");
  65. break;
  66. }
  67. return ret;
  68. }
  69. /*
  70. *
  71. * This function calls hardware random bit generator directory and retuns it
  72. * back to caller
  73. *
  74. */
  75. static int msm_rng_direct_read(struct msm_rng_device *msm_rng_dev,
  76. void *data, size_t max)
  77. {
  78. struct platform_device *pdev;
  79. void __iomem *base;
  80. size_t currsize = 0;
  81. u32 val = 0;
  82. u32 *retdata = data;
  83. int ret;
  84. int failed = 0;
  85. pdev = msm_rng_dev->pdev;
  86. base = msm_rng_dev->base;
  87. /* no room for word data */
  88. if (max < 4)
  89. return 0;
  90. mutex_lock(&msm_rng_dev->rng_lock);
  91. if (msm_rng_dev->icc_path) {
  92. ret = icc_set_bw(msm_rng_dev->icc_path, 0, 300000);
  93. if (ret) {
  94. pr_err("bus_scale_client_update_req failed\n");
  95. goto bus_err;
  96. }
  97. }
  98. /* enable PRNG clock */
  99. if (msm_rng_dev->prng_clk) {
  100. ret = clk_prepare_enable(msm_rng_dev->prng_clk);
  101. if (ret) {
  102. pr_err("failed to enable prng clock\n");
  103. goto err;
  104. }
  105. }
  106. /* read random data from h/w */
  107. do {
  108. /* check status bit if data is available */
  109. if (!(readl_relaxed(base + PRNG_STATUS_OFFSET)
  110. & 0x00000001)) {
  111. if (failed++ == RETRY_MAX_CNT) {
  112. if (currsize == 0)
  113. pr_err("Data not available\n");
  114. break;
  115. }
  116. udelay(RETRY_DELAY_INTERVAL);
  117. } else {
  118. /* read FIFO */
  119. val = readl_relaxed(base + PRNG_DATA_OUT_OFFSET);
  120. /* write data back to callers pointer */
  121. *(retdata++) = val;
  122. currsize += 4;
  123. /* make sure we stay on 32bit boundary */
  124. if ((max - currsize) < 4)
  125. break;
  126. }
  127. } while (currsize < max);
  128. /* vote to turn off clock */
  129. if (msm_rng_dev->prng_clk)
  130. clk_disable_unprepare(msm_rng_dev->prng_clk);
  131. err:
  132. if (msm_rng_dev->icc_path) {
  133. ret = icc_set_bw(msm_rng_dev->icc_path, 0, 0);
  134. if (ret)
  135. pr_err("bus_scale_client_update_req failed\n");
  136. }
  137. bus_err:
  138. mutex_unlock(&msm_rng_dev->rng_lock);
  139. val = 0L;
  140. return currsize;
  141. }
  142. static int msm_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
  143. {
  144. struct msm_rng_device *msm_rng_dev;
  145. int rv = 0;
  146. msm_rng_dev = (struct msm_rng_device *)rng->priv;
  147. rv = msm_rng_direct_read(msm_rng_dev, data, max);
  148. return rv;
  149. }
  150. static struct hwrng msm_rng = {
  151. .name = DRIVER_NAME,
  152. .read = msm_rng_read,
  153. .quality = 1024,
  154. };
  155. static int msm_rng_enable_hw(struct msm_rng_device *msm_rng_dev)
  156. {
  157. unsigned long val = 0;
  158. unsigned long reg_val = 0;
  159. int ret = 0;
  160. if (msm_rng_dev->icc_path) {
  161. ret = icc_set_bw(msm_rng_dev->icc_path, 0, 30000);
  162. if (ret)
  163. pr_err("bus_scale_client_update_req failed\n");
  164. }
  165. /* Enable the PRNG CLK */
  166. if (msm_rng_dev->prng_clk) {
  167. ret = clk_prepare_enable(msm_rng_dev->prng_clk);
  168. if (ret) {
  169. dev_err(&(msm_rng_dev->pdev)->dev,
  170. "failed to enable clock in probe\n");
  171. return -EPERM;
  172. }
  173. }
  174. /* Enable PRNG h/w only if it is NOT ON */
  175. val = readl_relaxed(msm_rng_dev->base + PRNG_CONFIG_OFFSET) &
  176. PRNG_HW_ENABLE;
  177. /* PRNG H/W is not ON */
  178. if (val != PRNG_HW_ENABLE) {
  179. val = readl_relaxed(msm_rng_dev->base + PRNG_LFSR_CFG_OFFSET);
  180. val &= PRNG_LFSR_CFG_MASK;
  181. val |= PRNG_LFSR_CFG_CLOCKS;
  182. writel_relaxed(val, msm_rng_dev->base + PRNG_LFSR_CFG_OFFSET);
  183. /* The PRNG CONFIG register should be first written */
  184. mb();
  185. reg_val = readl_relaxed(msm_rng_dev->base + PRNG_CONFIG_OFFSET)
  186. & PRNG_CONFIG_MASK;
  187. reg_val |= PRNG_HW_ENABLE;
  188. writel_relaxed(reg_val, msm_rng_dev->base + PRNG_CONFIG_OFFSET);
  189. /* The PRNG clk should be disabled only after we enable the
  190. * PRNG h/w by writing to the PRNG CONFIG register.
  191. */
  192. mb();
  193. }
  194. if (msm_rng_dev->prng_clk)
  195. clk_disable_unprepare(msm_rng_dev->prng_clk);
  196. if (msm_rng_dev->icc_path) {
  197. ret = icc_set_bw(msm_rng_dev->icc_path, 0, 0);
  198. if (ret)
  199. pr_err("bus_scale_client_update_req failed\n");
  200. }
  201. return 0;
  202. }
  203. static const struct file_operations msm_rng_fops = {
  204. .unlocked_ioctl = msm_rng_ioctl,
  205. };
  206. static struct class *msm_rng_class;
  207. static struct cdev msm_rng_cdev;
  208. static int msm_rng_probe(struct platform_device *pdev)
  209. {
  210. struct resource *res;
  211. struct msm_rng_device *msm_rng_dev = NULL;
  212. void __iomem *base = NULL;
  213. bool configure_qrng = true;
  214. int error = 0;
  215. struct device *dev;
  216. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  217. if (res == NULL) {
  218. dev_err(&pdev->dev, "invalid address\n");
  219. error = -EFAULT;
  220. goto err_exit;
  221. }
  222. msm_rng_dev = kzalloc(sizeof(struct msm_rng_device), GFP_KERNEL);
  223. if (!msm_rng_dev) {
  224. error = -ENOMEM;
  225. goto err_exit;
  226. }
  227. base = ioremap(res->start, resource_size(res));
  228. if (!base) {
  229. dev_err(&pdev->dev, "ioremap failed\n");
  230. error = -ENOMEM;
  231. goto err_iomap;
  232. }
  233. msm_rng_dev->base = base;
  234. /* create a handle for clock control */
  235. if (pdev->dev.of_node) {
  236. if (of_property_read_bool(pdev->dev.of_node,
  237. "qcom,no-clock-support"))
  238. msm_rng_dev->prng_clk = NULL;
  239. else
  240. msm_rng_dev->prng_clk = clk_get(&pdev->dev,
  241. "km_clk_src");
  242. }
  243. if (IS_ERR(msm_rng_dev->prng_clk)) {
  244. dev_err(&pdev->dev, "failed to register clock source\n");
  245. error = -ENODEV;
  246. goto err_clk_get;
  247. }
  248. /* save away pdev and register driver data */
  249. msm_rng_dev->pdev = pdev;
  250. platform_set_drvdata(pdev, msm_rng_dev);
  251. if (pdev->dev.of_node) {
  252. msm_rng_dev->icc_path = of_icc_get(&pdev->dev, "data_path");
  253. msm_rng_device_info.icc_path = msm_rng_dev->icc_path;
  254. if (IS_ERR(msm_rng_dev->icc_path)) {
  255. error = PTR_ERR(msm_rng_dev->icc_path);
  256. dev_err(&pdev->dev, "get icc path err %d\n", error);
  257. goto err_icc_get;
  258. }
  259. }
  260. /* Enable rng h/w for the targets which can access the entire
  261. * address space of PRNG.
  262. */
  263. if ((pdev->dev.of_node) && (of_property_read_bool(pdev->dev.of_node,
  264. "qcom,no-qrng-config")))
  265. configure_qrng = false;
  266. if (configure_qrng) {
  267. error = msm_rng_enable_hw(msm_rng_dev);
  268. if (error)
  269. goto err_icc_get;
  270. }
  271. mutex_init(&msm_rng_dev->rng_lock);
  272. mutex_init(&cached_rng_lock);
  273. /* register with hwrng framework */
  274. msm_rng.priv = (unsigned long) msm_rng_dev;
  275. error = hwrng_register(&msm_rng);
  276. if (error) {
  277. dev_err(&pdev->dev, "failed to register hwrng\n");
  278. goto err_reg_hwrng;
  279. }
  280. error = register_chrdev(QRNG_IOC_MAGIC, DRIVER_NAME, &msm_rng_fops);
  281. if (error) {
  282. dev_err(&pdev->dev, "failed to register chrdev\n");
  283. goto err_reg_chrdev;
  284. }
  285. msm_rng_class = class_create(THIS_MODULE, "msm-rng");
  286. if (IS_ERR(msm_rng_class)) {
  287. pr_err("class_create failed\n");
  288. error = PTR_ERR(msm_rng_class);
  289. goto err_create_cls;
  290. }
  291. dev = device_create(msm_rng_class, NULL, MKDEV(QRNG_IOC_MAGIC, 0),
  292. NULL, "msm-rng");
  293. if (IS_ERR(dev)) {
  294. pr_err("Device create failed\n");
  295. error = PTR_ERR(dev);
  296. goto err_create_dev;
  297. }
  298. cdev_init(&msm_rng_cdev, &msm_rng_fops);
  299. msm_rng_dev_cached = msm_rng_dev;
  300. return error;
  301. err_create_dev:
  302. class_destroy(msm_rng_class);
  303. err_create_cls:
  304. unregister_chrdev(QRNG_IOC_MAGIC, DRIVER_NAME);
  305. err_reg_chrdev:
  306. hwrng_unregister(&msm_rng);
  307. err_reg_hwrng:
  308. if (msm_rng_dev->icc_path)
  309. icc_put(msm_rng_dev->icc_path);
  310. err_icc_get:
  311. if (msm_rng_dev->prng_clk)
  312. clk_put(msm_rng_dev->prng_clk);
  313. err_clk_get:
  314. iounmap(msm_rng_dev->base);
  315. err_iomap:
  316. kfree_sensitive(msm_rng_dev);
  317. err_exit:
  318. return error;
  319. }
  320. static int msm_rng_remove(struct platform_device *pdev)
  321. {
  322. struct msm_rng_device *msm_rng_dev = platform_get_drvdata(pdev);
  323. unregister_chrdev(QRNG_IOC_MAGIC, DRIVER_NAME);
  324. hwrng_unregister(&msm_rng);
  325. if (msm_rng_dev->prng_clk)
  326. clk_put(msm_rng_dev->prng_clk);
  327. iounmap(msm_rng_dev->base);
  328. platform_set_drvdata(pdev, NULL);
  329. if (msm_rng_dev->icc_path)
  330. icc_put(msm_rng_dev->icc_path);
  331. kfree_sensitive(msm_rng_dev);
  332. msm_rng_dev_cached = NULL;
  333. return 0;
  334. }
  335. static int qrng_get_random(struct crypto_rng *tfm, const u8 *src,
  336. unsigned int slen, u8 *rdata,
  337. unsigned int dlen)
  338. {
  339. int sizeread = 0;
  340. int rv = -EFAULT;
  341. if (!msm_rng_dev_cached) {
  342. pr_err("%s: msm_rng_dev is not initialized\n", __func__);
  343. rv = -ENODEV;
  344. goto err_exit;
  345. }
  346. if (!rdata) {
  347. pr_err("%s: data buffer is null\n", __func__);
  348. rv = -EINVAL;
  349. goto err_exit;
  350. }
  351. if (signal_pending(current) ||
  352. mutex_lock_interruptible(&cached_rng_lock)) {
  353. pr_err("%s: mutex lock interrupted\n", __func__);
  354. rv = -ERESTARTSYS;
  355. goto err_exit;
  356. }
  357. sizeread = msm_rng_direct_read(msm_rng_dev_cached, rdata, dlen);
  358. if (sizeread == dlen)
  359. rv = 0;
  360. mutex_unlock(&cached_rng_lock);
  361. err_exit:
  362. return rv;
  363. }
  364. static int qrng_reset(struct crypto_rng *tfm, const u8 *seed, unsigned int slen)
  365. {
  366. return 0;
  367. }
  368. static struct rng_alg rng_algs[] = { {
  369. .generate = qrng_get_random,
  370. .seed = qrng_reset,
  371. .seedsize = 0,
  372. .base = {
  373. .cra_name = "qrng",
  374. .cra_driver_name = "fips_hw_qrng",
  375. .cra_priority = 300,
  376. .cra_ctxsize = 0,
  377. .cra_module = THIS_MODULE,
  378. }
  379. } };
  380. static const struct of_device_id qrng_match[] = {
  381. {.compatible = "qcom,msm-rng"},
  382. {},
  383. };
  384. static struct platform_driver rng_driver = {
  385. .probe = msm_rng_probe,
  386. .remove = msm_rng_remove,
  387. .driver = {
  388. .name = DRIVER_NAME,
  389. .of_match_table = qrng_match,
  390. },
  391. };
  392. static int __init msm_rng_init(void)
  393. {
  394. int ret;
  395. msm_rng_dev_cached = NULL;
  396. ret = platform_driver_register(&rng_driver);
  397. if (ret) {
  398. pr_err("%s: platform_driver_register error:%d\n",
  399. __func__, ret);
  400. goto err_exit;
  401. }
  402. ret = crypto_register_rngs(rng_algs, ARRAY_SIZE(rng_algs));
  403. if (ret) {
  404. pr_err("%s: crypto_register_algs error:%d\n",
  405. __func__, ret);
  406. goto err_exit;
  407. }
  408. err_exit:
  409. return ret;
  410. }
  411. module_init(msm_rng_init);
  412. static void __exit msm_rng_exit(void)
  413. {
  414. crypto_unregister_rngs(rng_algs, ARRAY_SIZE(rng_algs));
  415. platform_driver_unregister(&rng_driver);
  416. }
  417. module_exit(msm_rng_exit);
  418. MODULE_DESCRIPTION("QTI MSM Random Number Driver");
  419. MODULE_LICENSE("GPL v2");