msm_rng.c 11 KB

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