adsp-loader.c 12 KB

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