adsp-loader.c 13 KB

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