adsp-loader.c 13 KB

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