adsp-loader.c 13 KB

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