msm_rng.c 12 KB

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