adsp-loader.c 13 KB

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