adsp-loader.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2014, 2017-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/init.h>
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/err.h>
  9. #include <linux/string.h>
  10. #include <linux/delay.h>
  11. #include <linux/platform_device.h>
  12. #include <ipc/apr.h>
  13. #include <linux/of_device.h>
  14. #include <linux/sysfs.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/nvmem-consumer.h>
  17. #include <linux/slab.h>
  18. #include <soc/qcom/subsystem_restart.h>
  19. #define Q6_PIL_GET_DELAY_MS 100
  20. #define BOOT_CMD 1
  21. #define SSR_RESET_CMD 1
  22. #define IMAGE_UNLOAD_CMD 0
  23. #define MAX_FW_IMAGES 4
  24. static ssize_t adsp_boot_store(struct kobject *kobj,
  25. struct kobj_attribute *attr,
  26. const char *buf, size_t count);
  27. static ssize_t adsp_ssr_store(struct kobject *kobj,
  28. struct kobj_attribute *attr,
  29. const char *buf, size_t count);
  30. struct adsp_loader_private {
  31. void *pil_h;
  32. struct kobject *boot_adsp_obj;
  33. struct attribute_group *attr_group;
  34. char *adsp_fw_name;
  35. };
  36. static struct kobj_attribute adsp_boot_attribute =
  37. __ATTR(boot, 0220, NULL, adsp_boot_store);
  38. static struct kobj_attribute adsp_ssr_attribute =
  39. __ATTR(ssr, 0220, NULL, adsp_ssr_store);
  40. static struct attribute *attrs[] = {
  41. &adsp_boot_attribute.attr,
  42. &adsp_ssr_attribute.attr,
  43. NULL,
  44. };
  45. static struct work_struct adsp_ldr_work;
  46. static struct platform_device *adsp_private;
  47. static void adsp_loader_unload(struct platform_device *pdev);
  48. static void adsp_load_fw(struct work_struct *adsp_ldr_work)
  49. {
  50. struct platform_device *pdev = adsp_private;
  51. struct adsp_loader_private *priv = NULL;
  52. const char *adsp_dt = "qcom,adsp-state";
  53. int rc = 0;
  54. u32 adsp_state;
  55. const char *img_name;
  56. if (!pdev) {
  57. dev_err(&pdev->dev, "%s: Platform device null\n", __func__);
  58. goto fail;
  59. }
  60. if (!pdev->dev.of_node) {
  61. dev_err(&pdev->dev,
  62. "%s: Device tree information missing\n", __func__);
  63. goto fail;
  64. }
  65. rc = of_property_read_u32(pdev->dev.of_node, adsp_dt, &adsp_state);
  66. if (rc) {
  67. dev_err(&pdev->dev,
  68. "%s: ADSP state = %x\n", __func__, adsp_state);
  69. goto fail;
  70. }
  71. rc = of_property_read_string(pdev->dev.of_node,
  72. "qcom,proc-img-to-load",
  73. &img_name);
  74. if (rc) {
  75. dev_dbg(&pdev->dev,
  76. "%s: loading default image ADSP\n", __func__);
  77. goto load_adsp;
  78. }
  79. if (!strcmp(img_name, "modem")) {
  80. /* adsp_state always returns "0". So load modem image based on
  81. * apr_modem_state to prevent loading of image twice
  82. */
  83. adsp_state = apr_get_modem_state();
  84. if (adsp_state == APR_SUBSYS_DOWN) {
  85. priv = platform_get_drvdata(pdev);
  86. if (!priv) {
  87. dev_err(&pdev->dev,
  88. " %s: Private data get failed\n", __func__);
  89. goto fail;
  90. }
  91. priv->pil_h = subsystem_get("modem");
  92. if (IS_ERR(priv->pil_h)) {
  93. dev_err(&pdev->dev, "%s: pil get failed,\n",
  94. __func__);
  95. goto fail;
  96. }
  97. /* Set the state of the ADSP in APR driver */
  98. apr_set_modem_state(APR_SUBSYS_LOADED);
  99. } else if (adsp_state == APR_SUBSYS_LOADED) {
  100. dev_dbg(&pdev->dev,
  101. "%s: MDSP state = %x\n", __func__, adsp_state);
  102. }
  103. dev_dbg(&pdev->dev, "%s: Q6/MDSP image is loaded\n", __func__);
  104. return;
  105. }
  106. load_adsp:
  107. {
  108. adsp_state = apr_get_q6_state();
  109. if (adsp_state == APR_SUBSYS_DOWN) {
  110. priv = platform_get_drvdata(pdev);
  111. if (!priv) {
  112. dev_err(&pdev->dev,
  113. " %s: Private data get failed\n", __func__);
  114. goto fail;
  115. }
  116. if (!priv->adsp_fw_name) {
  117. dev_dbg(&pdev->dev, "%s: Load default ADSP\n",
  118. __func__);
  119. priv->pil_h = subsystem_get("adsp");
  120. } else {
  121. dev_dbg(&pdev->dev, "%s: Load ADSP with fw name %s\n",
  122. __func__, priv->adsp_fw_name);
  123. priv->pil_h = subsystem_get_with_fwname("adsp", priv->adsp_fw_name);
  124. }
  125. if (IS_ERR(priv->pil_h)) {
  126. dev_err(&pdev->dev, "%s: pil get failed,\n",
  127. __func__);
  128. goto fail;
  129. }
  130. } else if (adsp_state == APR_SUBSYS_LOADED) {
  131. dev_dbg(&pdev->dev,
  132. "%s: ADSP state = %x\n", __func__, adsp_state);
  133. }
  134. dev_dbg(&pdev->dev, "%s: Q6/ADSP image is loaded\n", __func__);
  135. return;
  136. }
  137. fail:
  138. dev_err(&pdev->dev, "%s: Q6 image loading failed\n", __func__);
  139. }
  140. static void adsp_loader_do(struct platform_device *pdev)
  141. {
  142. schedule_work(&adsp_ldr_work);
  143. }
  144. static ssize_t adsp_ssr_store(struct kobject *kobj,
  145. struct kobj_attribute *attr,
  146. const char *buf,
  147. size_t count)
  148. {
  149. int ssr_command = 0;
  150. struct subsys_device *adsp_dev = NULL;
  151. struct platform_device *pdev = adsp_private;
  152. struct adsp_loader_private *priv = NULL;
  153. int rc;
  154. dev_dbg(&pdev->dev, "%s: going to call adsp ssr\n ", __func__);
  155. if (kstrtoint(buf, 10, &ssr_command) < 0)
  156. return -EINVAL;
  157. if (ssr_command != SSR_RESET_CMD)
  158. return -EINVAL;
  159. priv = platform_get_drvdata(pdev);
  160. if (!priv)
  161. return -EINVAL;
  162. adsp_dev = (struct subsys_device *)priv->pil_h;
  163. if (!adsp_dev)
  164. return -EINVAL;
  165. dev_err(&pdev->dev, "requesting for ADSP restart\n");
  166. /* subsystem_restart_dev has worker queue to handle */
  167. rc = subsystem_restart_dev(adsp_dev);
  168. if (rc) {
  169. dev_err(&pdev->dev, "subsystem_restart_dev failed\n");
  170. return rc;
  171. }
  172. dev_dbg(&pdev->dev, "ADSP restarted\n");
  173. return count;
  174. }
  175. static ssize_t adsp_boot_store(struct kobject *kobj,
  176. struct kobj_attribute *attr,
  177. const char *buf,
  178. size_t count)
  179. {
  180. int boot = 0;
  181. if (sscanf(buf, "%du", &boot) != 1) {
  182. pr_err("%s: failed to read boot info from string\n", __func__);
  183. return -EINVAL;
  184. }
  185. if (boot == BOOT_CMD) {
  186. pr_debug("%s: going to call adsp_loader_do\n", __func__);
  187. adsp_loader_do(adsp_private);
  188. } else if (boot == IMAGE_UNLOAD_CMD) {
  189. pr_debug("%s: going to call adsp_unloader\n", __func__);
  190. adsp_loader_unload(adsp_private);
  191. }
  192. return count;
  193. }
  194. static void adsp_loader_unload(struct platform_device *pdev)
  195. {
  196. struct adsp_loader_private *priv = NULL;
  197. priv = platform_get_drvdata(pdev);
  198. if (!priv)
  199. return;
  200. if (priv->pil_h) {
  201. dev_dbg(&pdev->dev, "%s: calling subsystem put\n", __func__);
  202. subsystem_put(priv->pil_h);
  203. priv->pil_h = NULL;
  204. }
  205. }
  206. static int adsp_loader_init_sysfs(struct platform_device *pdev)
  207. {
  208. int ret = -EINVAL;
  209. struct adsp_loader_private *priv = NULL;
  210. adsp_private = NULL;
  211. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  212. if (!priv) {
  213. ret = -ENOMEM;
  214. return ret;
  215. }
  216. platform_set_drvdata(pdev, priv);
  217. priv->pil_h = NULL;
  218. priv->boot_adsp_obj = NULL;
  219. priv->attr_group = devm_kzalloc(&pdev->dev,
  220. sizeof(*(priv->attr_group)),
  221. GFP_KERNEL);
  222. if (!priv->attr_group) {
  223. ret = -ENOMEM;
  224. goto error_return;
  225. }
  226. priv->attr_group->attrs = attrs;
  227. priv->boot_adsp_obj = kobject_create_and_add("boot_adsp", kernel_kobj);
  228. if (!priv->boot_adsp_obj) {
  229. dev_err(&pdev->dev, "%s: sysfs create and add failed\n",
  230. __func__);
  231. ret = -ENOMEM;
  232. goto error_return;
  233. }
  234. ret = sysfs_create_group(priv->boot_adsp_obj, priv->attr_group);
  235. if (ret) {
  236. dev_err(&pdev->dev, "%s: sysfs create group failed %d\n",
  237. __func__, ret);
  238. goto error_return;
  239. }
  240. adsp_private = pdev;
  241. return 0;
  242. error_return:
  243. if (priv->boot_adsp_obj) {
  244. kobject_del(priv->boot_adsp_obj);
  245. priv->boot_adsp_obj = NULL;
  246. }
  247. return ret;
  248. }
  249. static int adsp_loader_remove(struct platform_device *pdev)
  250. {
  251. struct adsp_loader_private *priv = NULL;
  252. priv = platform_get_drvdata(pdev);
  253. if (!priv)
  254. return 0;
  255. if (priv->pil_h) {
  256. subsystem_put(priv->pil_h);
  257. priv->pil_h = NULL;
  258. }
  259. if (priv->boot_adsp_obj) {
  260. sysfs_remove_group(priv->boot_adsp_obj, priv->attr_group);
  261. kobject_del(priv->boot_adsp_obj);
  262. priv->boot_adsp_obj = NULL;
  263. }
  264. return 0;
  265. }
  266. static int adsp_loader_probe(struct platform_device *pdev)
  267. {
  268. struct adsp_loader_private *priv = NULL;
  269. struct nvmem_cell *cell;
  270. size_t len;
  271. u32 *buf;
  272. const char **adsp_fw_name_array = NULL;
  273. int adsp_fw_cnt;
  274. u32* adsp_fw_bit_values = NULL;
  275. int i;
  276. int fw_name_size;
  277. u32 adsp_var_idx = 0;
  278. int ret = 0;
  279. u32 adsp_fuse_not_supported = 0;
  280. const char *adsp_fw_name;
  281. ret = adsp_loader_init_sysfs(pdev);
  282. if (ret != 0) {
  283. dev_err(&pdev->dev, "%s: Error in initing sysfs\n", __func__);
  284. return ret;
  285. }
  286. priv = platform_get_drvdata(pdev);
  287. /* get adsp variant idx */
  288. cell = nvmem_cell_get(&pdev->dev, "adsp_variant");
  289. if (IS_ERR_OR_NULL(cell)) {
  290. dev_dbg(&pdev->dev, "%s: FAILED to get nvmem cell \n",
  291. __func__);
  292. /*
  293. * When ADSP variant read from fuse register is not
  294. * supported, check if image with different fw image
  295. * name needs to be loaded
  296. */
  297. ret = of_property_read_u32(pdev->dev.of_node,
  298. "adsp-fuse-not-supported",
  299. &adsp_fuse_not_supported);
  300. if (ret) {
  301. dev_dbg(&pdev->dev,
  302. "%s: adsp_fuse_not_supported prop not found",
  303. __func__, ret);
  304. goto wqueue;
  305. }
  306. if (adsp_fuse_not_supported) {
  307. /* Read ADSP firmware image name */
  308. ret = of_property_read_string(pdev->dev.of_node,
  309. "adsp-fw-name",
  310. &adsp_fw_name);
  311. if (ret < 0) {
  312. dev_dbg(&pdev->dev, "%s: unable to read fw-name\n",
  313. __func__);
  314. goto wqueue;
  315. }
  316. fw_name_size = strlen(adsp_fw_name) + 1;
  317. priv->adsp_fw_name = devm_kzalloc(&pdev->dev,
  318. fw_name_size,
  319. GFP_KERNEL);
  320. if (!priv->adsp_fw_name)
  321. goto wqueue;
  322. strlcpy(priv->adsp_fw_name, adsp_fw_name,
  323. fw_name_size);
  324. }
  325. goto wqueue;
  326. }
  327. buf = nvmem_cell_read(cell, &len);
  328. nvmem_cell_put(cell);
  329. if (IS_ERR_OR_NULL(buf) || len <= 0 || len > sizeof(u32)) {
  330. dev_dbg(&pdev->dev, "%s: FAILED to read nvmem cell \n", __func__);
  331. goto wqueue;
  332. }
  333. memcpy(&adsp_var_idx, buf, len);
  334. kfree(buf);
  335. /* Get count of fw images */
  336. adsp_fw_cnt = of_property_count_strings(pdev->dev.of_node,
  337. "adsp-fw-names");
  338. if (adsp_fw_cnt <= 0 || adsp_fw_cnt > MAX_FW_IMAGES) {
  339. dev_dbg(&pdev->dev, "%s: Invalid number of fw images %d",
  340. __func__, adsp_fw_cnt);
  341. goto wqueue;
  342. }
  343. adsp_fw_bit_values = devm_kzalloc(&pdev->dev,
  344. adsp_fw_cnt * sizeof(u32), GFP_KERNEL);
  345. if (!adsp_fw_bit_values)
  346. goto wqueue;
  347. /* Read bit values corresponding to each firmware image entry */
  348. ret = of_property_read_u32_array(pdev->dev.of_node,
  349. "adsp-fw-bit-values",
  350. adsp_fw_bit_values,
  351. adsp_fw_cnt);
  352. if (ret) {
  353. dev_dbg(&pdev->dev, "%s: unable to read fw-bit-values\n",
  354. __func__);
  355. goto wqueue;
  356. }
  357. adsp_fw_name_array = devm_kzalloc(&pdev->dev,
  358. adsp_fw_cnt * sizeof(char *), GFP_KERNEL);
  359. if (!adsp_fw_name_array)
  360. goto wqueue;
  361. /* Read ADSP firmware image names */
  362. ret = of_property_read_string_array(pdev->dev.of_node,
  363. "adsp-fw-names",
  364. adsp_fw_name_array,
  365. adsp_fw_cnt);
  366. if (ret < 0) {
  367. dev_dbg(&pdev->dev, "%s: unable to read fw-names\n",
  368. __func__);
  369. goto wqueue;
  370. }
  371. for (i = 0; i < adsp_fw_cnt; i++) {
  372. if (adsp_fw_bit_values[i] == adsp_var_idx) {
  373. fw_name_size = strlen(adsp_fw_name_array[i]) + 1;
  374. priv->adsp_fw_name = devm_kzalloc(&pdev->dev,
  375. fw_name_size,
  376. GFP_KERNEL);
  377. if (!priv->adsp_fw_name)
  378. goto wqueue;
  379. strlcpy(priv->adsp_fw_name, adsp_fw_name_array[i],
  380. fw_name_size);
  381. break;
  382. }
  383. }
  384. wqueue:
  385. INIT_WORK(&adsp_ldr_work, adsp_load_fw);
  386. if (adsp_fw_bit_values)
  387. devm_kfree(&pdev->dev, adsp_fw_bit_values);
  388. if (adsp_fw_name_array)
  389. devm_kfree(&pdev->dev, adsp_fw_name_array);
  390. return 0;
  391. }
  392. static const struct of_device_id adsp_loader_dt_match[] = {
  393. { .compatible = "qcom,adsp-loader" },
  394. { }
  395. };
  396. MODULE_DEVICE_TABLE(of, adsp_loader_dt_match);
  397. static struct platform_driver adsp_loader_driver = {
  398. .driver = {
  399. .name = "adsp-loader",
  400. .owner = THIS_MODULE,
  401. .of_match_table = adsp_loader_dt_match,
  402. .suppress_bind_attrs = true,
  403. },
  404. .probe = adsp_loader_probe,
  405. .remove = adsp_loader_remove,
  406. };
  407. static int __init adsp_loader_init(void)
  408. {
  409. return platform_driver_register(&adsp_loader_driver);
  410. }
  411. module_init(adsp_loader_init);
  412. static void __exit adsp_loader_exit(void)
  413. {
  414. platform_driver_unregister(&adsp_loader_driver);
  415. }
  416. module_exit(adsp_loader_exit);
  417. MODULE_DESCRIPTION("ADSP Loader module");
  418. MODULE_LICENSE("GPL v2");