trusty.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /*
  2. * Copyright (C) 2013 Google, Inc.
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <asm/compiler.h>
  15. #include <linux/delay.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/platform_device.h>
  20. #ifdef CONFIG_MT_TRUSTY_DEBUGFS
  21. #include <linux/random.h>
  22. #endif
  23. #include <linux/slab.h>
  24. #include <linux/stat.h>
  25. #include <linux/string.h>
  26. #include <linux/trusty/smcall.h>
  27. #include <linux/trusty/sm_err.h>
  28. #include <linux/trusty/trusty.h>
  29. #if 0
  30. #ifdef CONFIG_MTK_ENABLE_GENIEZONE
  31. #ifdef CONFIG_MTK_RAM_CONSOLE
  32. #include "trusty-ramconsole.h"
  33. #endif
  34. #endif
  35. #endif
  36. struct trusty_state;
  37. struct trusty_work {
  38. struct trusty_state *ts;
  39. struct work_struct work;
  40. };
  41. struct trusty_state {
  42. struct mutex smc_lock;
  43. struct atomic_notifier_head notifier;
  44. struct completion cpu_idle_completion;
  45. char *version_str;
  46. u32 api_version;
  47. struct device *dev;
  48. struct workqueue_struct *nop_wq;
  49. struct trusty_work __percpu *nop_works;
  50. struct list_head nop_queue;
  51. spinlock_t nop_lock; /* protects nop_queue */
  52. };
  53. #ifdef CONFIG_ARM64
  54. #define SMC_ARG0 "x0"
  55. #define SMC_ARG1 "x1"
  56. #define SMC_ARG2 "x2"
  57. #define SMC_ARG3 "x3"
  58. #define SMC_ARCH_EXTENSION ""
  59. #define SMC_REGISTERS_TRASHED "x4","x5","x6","x7","x8","x9","x10","x11", \
  60. "x12","x13","x14","x15","x16","x17"
  61. #else
  62. #define SMC_ARG0 "r0"
  63. #define SMC_ARG1 "r1"
  64. #define SMC_ARG2 "r2"
  65. #define SMC_ARG3 "r3"
  66. #define SMC_ARCH_EXTENSION ".arch_extension sec\n"
  67. #define SMC_REGISTERS_TRASHED "ip"
  68. #endif
  69. static inline ulong smc(ulong r0, ulong r1, ulong r2, ulong r3)
  70. {
  71. register ulong _r0 asm(SMC_ARG0) = r0;
  72. register ulong _r1 asm(SMC_ARG1) = r1;
  73. register ulong _r2 asm(SMC_ARG2) = r2;
  74. register ulong _r3 asm(SMC_ARG3) = r3;
  75. asm volatile(
  76. __asmeq("%0", SMC_ARG0)
  77. __asmeq("%1", SMC_ARG1)
  78. __asmeq("%2", SMC_ARG2)
  79. __asmeq("%3", SMC_ARG3)
  80. __asmeq("%4", SMC_ARG0)
  81. __asmeq("%5", SMC_ARG1)
  82. __asmeq("%6", SMC_ARG2)
  83. __asmeq("%7", SMC_ARG3)
  84. SMC_ARCH_EXTENSION
  85. "smc #0" /* switch to secure world */
  86. : "=r" (_r0), "=r" (_r1), "=r" (_r2), "=r" (_r3)
  87. : "r" (_r0), "r" (_r1), "r" (_r2), "r" (_r3)
  88. : SMC_REGISTERS_TRASHED);
  89. return _r0;
  90. }
  91. #ifdef CONFIG_TRUSTY_WDT_FIQ_ARMV7_SUPPORT
  92. s32 trusty_fast_call32_nodev(u32 smcnr, u32 a0, u32 a1, u32 a2)
  93. {
  94. BUG_ON(!SMC_IS_FASTCALL(smcnr));
  95. BUG_ON(SMC_IS_SMC64(smcnr));
  96. return smc(smcnr, a0, a1, a2);
  97. }
  98. #endif
  99. s32 trusty_fast_call32(struct device *dev, u32 smcnr, u32 a0, u32 a1, u32 a2)
  100. {
  101. struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
  102. BUG_ON(!s);
  103. BUG_ON(!SMC_IS_FASTCALL(smcnr));
  104. BUG_ON(SMC_IS_SMC64(smcnr));
  105. return smc(smcnr, a0, a1, a2);
  106. }
  107. EXPORT_SYMBOL(trusty_fast_call32);
  108. #ifdef CONFIG_64BIT
  109. s64 trusty_fast_call64(struct device *dev, u64 smcnr, u64 a0, u64 a1, u64 a2)
  110. {
  111. struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
  112. BUG_ON(!s);
  113. BUG_ON(!SMC_IS_FASTCALL(smcnr));
  114. BUG_ON(!SMC_IS_SMC64(smcnr));
  115. return smc(smcnr, a0, a1, a2);
  116. }
  117. #endif
  118. static ulong trusty_std_call_inner(struct device *dev, ulong smcnr,
  119. ulong a0, ulong a1, ulong a2)
  120. {
  121. ulong ret;
  122. int retry = 5;
  123. dev_dbg(dev, "%s(0x%lx 0x%lx 0x%lx 0x%lx)\n",
  124. __func__, smcnr, a0, a1, a2);
  125. while (true) {
  126. ret = smc(smcnr, a0, a1, a2);
  127. while ((s32)ret == SM_ERR_FIQ_INTERRUPTED)
  128. ret = smc(SMC_SC_RESTART_FIQ, 0, 0, 0);
  129. if ((int)ret != SM_ERR_BUSY || !retry)
  130. break;
  131. dev_dbg(dev, "%s(0x%lx 0x%lx 0x%lx 0x%lx) returned busy, retry\n",
  132. __func__, smcnr, a0, a1, a2);
  133. retry--;
  134. }
  135. return ret;
  136. }
  137. static ulong trusty_std_call_helper(struct device *dev, ulong smcnr,
  138. ulong a0, ulong a1, ulong a2)
  139. {
  140. ulong ret;
  141. int sleep_time = 1;
  142. struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
  143. while (true) {
  144. local_irq_disable();
  145. atomic_notifier_call_chain(&s->notifier, TRUSTY_CALL_PREPARE,
  146. NULL);
  147. ret = trusty_std_call_inner(dev, smcnr, a0, a1, a2);
  148. atomic_notifier_call_chain(&s->notifier, TRUSTY_CALL_RETURNED,
  149. NULL);
  150. local_irq_enable();
  151. if ((int)ret != SM_ERR_BUSY)
  152. break;
  153. if (sleep_time == 256)
  154. dev_warn(dev, "%s(0x%lx 0x%lx 0x%lx 0x%lx) returned busy\n",
  155. __func__, smcnr, a0, a1, a2);
  156. dev_dbg(dev, "%s(0x%lx 0x%lx 0x%lx 0x%lx) returned busy, wait %d ms\n",
  157. __func__, smcnr, a0, a1, a2, sleep_time);
  158. msleep(sleep_time);
  159. if (sleep_time < 1000)
  160. sleep_time <<= 1;
  161. dev_dbg(dev, "%s(0x%lx 0x%lx 0x%lx 0x%lx) retry\n",
  162. __func__, smcnr, a0, a1, a2);
  163. }
  164. if (sleep_time > 256)
  165. dev_warn(dev, "%s(0x%lx 0x%lx 0x%lx 0x%lx) busy cleared\n",
  166. __func__, smcnr, a0, a1, a2);
  167. return ret;
  168. }
  169. static void trusty_std_call_cpu_idle(struct trusty_state *s)
  170. {
  171. int ret;
  172. unsigned long timeout = HZ * 10;
  173. #ifdef CONFIG_TRUSTY_INTERRUPT_FIQ_ONLY
  174. timeout = HZ / 5; /* 200 ms */
  175. #endif
  176. ret = wait_for_completion_timeout(&s->cpu_idle_completion, timeout);
  177. if (!ret) {
  178. pr_warn("%s: timed out waiting for cpu idle to clear, retry anyway\n",
  179. __func__);
  180. }
  181. }
  182. s32 trusty_std_call32(struct device *dev, u32 smcnr, u32 a0, u32 a1, u32 a2)
  183. {
  184. int ret;
  185. struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
  186. BUG_ON(SMC_IS_FASTCALL(smcnr));
  187. BUG_ON(SMC_IS_SMC64(smcnr));
  188. if (smcnr != SMC_SC_NOP) {
  189. mutex_lock(&s->smc_lock);
  190. reinit_completion(&s->cpu_idle_completion);
  191. }
  192. dev_dbg(dev, "%s(0x%x 0x%x 0x%x 0x%x) started\n",
  193. __func__, smcnr, a0, a1, a2);
  194. ret = trusty_std_call_helper(dev, smcnr, a0, a1, a2);
  195. while (ret == SM_ERR_INTERRUPTED || ret == SM_ERR_CPU_IDLE) {
  196. dev_dbg(dev, "%s(0x%x 0x%x 0x%x 0x%x) interrupted\n",
  197. __func__, smcnr, a0, a1, a2);
  198. if (ret == SM_ERR_CPU_IDLE)
  199. trusty_std_call_cpu_idle(s);
  200. ret = trusty_std_call_helper(dev, SMC_SC_RESTART_LAST, 0, 0, 0);
  201. }
  202. dev_dbg(dev, "%s(0x%x 0x%x 0x%x 0x%x) returned 0x%x\n",
  203. __func__, smcnr, a0, a1, a2, ret);
  204. WARN_ONCE(ret == SM_ERR_PANIC, "trusty crashed");
  205. if (smcnr == SMC_SC_NOP)
  206. complete(&s->cpu_idle_completion);
  207. else
  208. mutex_unlock(&s->smc_lock);
  209. return ret;
  210. }
  211. EXPORT_SYMBOL(trusty_std_call32);
  212. int trusty_call_notifier_register(struct device *dev, struct notifier_block *n)
  213. {
  214. struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
  215. return atomic_notifier_chain_register(&s->notifier, n);
  216. }
  217. EXPORT_SYMBOL(trusty_call_notifier_register);
  218. int trusty_call_notifier_unregister(struct device *dev,
  219. struct notifier_block *n)
  220. {
  221. struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
  222. return atomic_notifier_chain_unregister(&s->notifier, n);
  223. }
  224. EXPORT_SYMBOL(trusty_call_notifier_unregister);
  225. static int trusty_remove_child(struct device *dev, void *data)
  226. {
  227. platform_device_unregister(to_platform_device(dev));
  228. return 0;
  229. }
  230. ssize_t trusty_version_show(struct device *dev, struct device_attribute *attr,
  231. char *buf)
  232. {
  233. struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
  234. return scnprintf(buf, PAGE_SIZE, "%s\n", s->version_str);
  235. }
  236. DEVICE_ATTR(trusty_version, S_IRUSR, trusty_version_show, NULL);
  237. #if 0
  238. #ifdef CONFIG_MTK_ENABLE_GENIEZONE
  239. #ifdef CONFIG_MTK_RAM_CONSOLE
  240. static void init_gz_ramconsole(struct device *dev)
  241. {
  242. u32 low, high;
  243. #ifdef SRAM_TEST
  244. low = 0x0011E000;
  245. high = 0x00000000;
  246. #else
  247. unsigned long *gz_irq_pa;
  248. gz_irq_pa = aee_rr_rec_gz_irq_pa();
  249. dev_info(dev, "ram console: get PA %p\n", gz_irq_pa);
  250. low = (u32)(((u64)gz_irq_pa << 32) >> 32);
  251. high = (u32)((u64)gz_irq_pa >> 32);
  252. #endif
  253. dev_info(dev, "ram console: send ram console PA from kernel to GZ\n");
  254. trusty_std_call32(dev, MT_SMC_SC_SET_RAMCONSOLE, low, high, 0);
  255. }
  256. #endif
  257. #endif
  258. #endif
  259. #ifdef CONFIG_MT_TRUSTY_DEBUGFS
  260. ssize_t trusty_add(struct device *dev, struct device_attribute *attr,
  261. char *buf)
  262. {
  263. s32 a, b, ret;
  264. get_random_bytes(&a, sizeof(s32));
  265. a &= 0xFF;
  266. get_random_bytes(&b, sizeof(s32));
  267. b &= 0xFF;
  268. ret = trusty_std_call32(dev, MT_SMC_SC_ADD, a, b, 0);
  269. return scnprintf(buf, PAGE_SIZE, "%d + %d = %d, %s\n", a, b, ret,
  270. (a + b) == ret ? "PASS" : "FAIL");
  271. }
  272. DEVICE_ATTR(trusty_add, S_IRUSR, trusty_add, NULL);
  273. ssize_t trusty_threads(struct device *dev,
  274. struct device_attribute *attr, char *buf)
  275. {
  276. /* Dump Trusty threads info to memlog */
  277. trusty_fast_call32(dev, MT_SMC_FC_THREADS, 0, 0, 0);
  278. /* Dump threads info from memlog to kmsg*/
  279. trusty_std_call32(dev, SMC_SC_NOP, 0, 0, 0);
  280. return 0;
  281. }
  282. DEVICE_ATTR(trusty_threads, S_IRUSR, trusty_threads, NULL);
  283. ssize_t trusty_threadstats(struct device *dev,
  284. struct device_attribute *attr, char *buf)
  285. {
  286. /* Dump Trusty threads info to memlog */
  287. trusty_fast_call32(dev, MT_SMC_FC_THREADSTATS, 0, 0, 0);
  288. /* Dump threads info from memlog to kmsg*/
  289. trusty_std_call32(dev, SMC_SC_NOP, 0, 0, 0);
  290. return 0;
  291. }
  292. DEVICE_ATTR(trusty_threadstats, S_IRUSR, trusty_threadstats, NULL);
  293. ssize_t trusty_threadload(struct device *dev,
  294. struct device_attribute *attr, char *buf)
  295. {
  296. /* Dump Trusty threads info to memlog */
  297. trusty_fast_call32(dev, MT_SMC_FC_THREADLOAD, 0, 0, 0);
  298. /* Dump threads info from memlog to kmsg*/
  299. trusty_std_call32(dev, SMC_SC_NOP, 0, 0, 0);
  300. return 0;
  301. }
  302. DEVICE_ATTR(trusty_threadload, S_IRUSR, trusty_threadload, NULL);
  303. ssize_t trusty_heap_dump(struct device *dev,
  304. struct device_attribute *attr, char *buf)
  305. {
  306. /* Dump Trusty threads info to memlog */
  307. trusty_fast_call32(dev, MT_SMC_FC_HEAP_DUMP, 0, 0, 0);
  308. /* Dump threads info from memlog to kmsg*/
  309. trusty_std_call32(dev, SMC_SC_NOP, 0, 0, 0);
  310. return 0;
  311. }
  312. DEVICE_ATTR(trusty_heap_dump, S_IRUSR, trusty_heap_dump, NULL);
  313. ssize_t trusty_apps(struct device *dev,
  314. struct device_attribute *attr, char *buf)
  315. {
  316. /* Dump Trusty threads info to memlog */
  317. trusty_fast_call32(dev, MT_SMC_FC_APPS, 0, 0, 0);
  318. /* Dump threads info from memlog to kmsg*/
  319. trusty_std_call32(dev, SMC_SC_NOP, 0, 0, 0);
  320. return 0;
  321. }
  322. DEVICE_ATTR(trusty_apps, S_IRUSR, trusty_apps, NULL);
  323. ssize_t trusty_vdev_reset(struct device *dev,
  324. struct device_attribute *attr, char *buf)
  325. {
  326. trusty_std_call32(dev, SMC_SC_VDEV_RESET, 0, 0, 0);
  327. return 0;
  328. }
  329. DEVICE_ATTR(trusty_vdev_reset, S_IRUSR, trusty_vdev_reset, NULL);
  330. static void trusty_create_debugfs(struct trusty_state *s, struct device *pdev)
  331. {
  332. int ret;
  333. ret = device_create_file(pdev, &dev_attr_trusty_add);
  334. if (ret)
  335. goto err_create_trusty_add;
  336. ret = device_create_file(pdev, &dev_attr_trusty_threads);
  337. if (ret)
  338. goto err_create_trusty_threads;
  339. ret = device_create_file(pdev, &dev_attr_trusty_threadstats);
  340. if (ret)
  341. goto err_create_trusty_threadstats;
  342. ret = device_create_file(pdev, &dev_attr_trusty_threadload);
  343. if (ret)
  344. goto err_create_trusty_threadload;
  345. ret = device_create_file(pdev, &dev_attr_trusty_heap_dump);
  346. if (ret)
  347. goto err_create_trusty_heap_dump;
  348. ret = device_create_file(pdev, &dev_attr_trusty_apps);
  349. if (ret)
  350. goto err_create_trusty_apps;
  351. ret = device_create_file(pdev, &dev_attr_trusty_vdev_reset);
  352. if (ret)
  353. goto err_create_trusty_vdev_reset;
  354. return;
  355. err_create_trusty_vdev_reset:
  356. device_remove_file(pdev, &dev_attr_trusty_vdev_reset);
  357. err_create_trusty_apps:
  358. device_remove_file(pdev, &dev_attr_trusty_apps);
  359. err_create_trusty_heap_dump:
  360. device_remove_file(pdev, &dev_attr_trusty_heap_dump);
  361. err_create_trusty_threadload:
  362. device_remove_file(pdev, &dev_attr_trusty_threadload);
  363. err_create_trusty_threadstats:
  364. device_remove_file(pdev, &dev_attr_trusty_threadstats);
  365. err_create_trusty_threads:
  366. device_remove_file(pdev, &dev_attr_trusty_threads);
  367. err_create_trusty_add:
  368. device_remove_file(pdev, &dev_attr_trusty_add);
  369. }
  370. #endif /* CONFIG_MT_TRUSTY_DEBUGFS */
  371. const char *trusty_version_str_get(struct device *dev)
  372. {
  373. struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
  374. return s->version_str;
  375. }
  376. EXPORT_SYMBOL(trusty_version_str_get);
  377. static void trusty_init_version(struct trusty_state *s, struct device *dev)
  378. {
  379. int ret;
  380. int i;
  381. int version_str_len;
  382. ret = trusty_fast_call32(dev, SMC_FC_GET_VERSION_STR, -1, 0, 0);
  383. if (ret <= 0)
  384. goto err_get_size;
  385. version_str_len = ret;
  386. s->version_str = kmalloc(version_str_len + 1, GFP_KERNEL);
  387. for (i = 0; i < version_str_len; i++) {
  388. ret = trusty_fast_call32(dev, SMC_FC_GET_VERSION_STR, i, 0, 0);
  389. if (ret < 0)
  390. goto err_get_char;
  391. s->version_str[i] = ret;
  392. }
  393. s->version_str[i] = '\0';
  394. dev_info(dev, "trusty version: %s\n", s->version_str);
  395. ret = device_create_file(dev, &dev_attr_trusty_version);
  396. if (ret)
  397. goto err_create_file;
  398. return;
  399. err_create_file:
  400. err_get_char:
  401. kfree(s->version_str);
  402. s->version_str = NULL;
  403. err_get_size:
  404. dev_err(dev, "failed to get version: %d\n", ret);
  405. }
  406. u32 trusty_get_api_version(struct device *dev)
  407. {
  408. struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
  409. return s->api_version;
  410. }
  411. EXPORT_SYMBOL(trusty_get_api_version);
  412. static int trusty_init_api_version(struct trusty_state *s, struct device *dev)
  413. {
  414. u32 api_version;
  415. api_version = trusty_fast_call32(dev, SMC_FC_API_VERSION,
  416. TRUSTY_API_VERSION_CURRENT, 0, 0);
  417. if (api_version == SM_ERR_UNDEFINED_SMC)
  418. api_version = 0;
  419. if (api_version > TRUSTY_API_VERSION_CURRENT) {
  420. dev_err(dev, "unsupported api version %u > %u\n",
  421. api_version, TRUSTY_API_VERSION_CURRENT);
  422. return -EINVAL;
  423. }
  424. dev_info(dev, "selected api version: %u (requested %u)\n",
  425. api_version, TRUSTY_API_VERSION_CURRENT);
  426. s->api_version = api_version;
  427. return 0;
  428. }
  429. static bool dequeue_nop(struct trusty_state *s, u32 *args)
  430. {
  431. unsigned long flags;
  432. struct trusty_nop *nop = NULL;
  433. spin_lock_irqsave(&s->nop_lock, flags);
  434. if (!list_empty(&s->nop_queue)) {
  435. nop = list_first_entry(&s->nop_queue,
  436. struct trusty_nop, node);
  437. list_del_init(&nop->node);
  438. args[0] = nop->args[0];
  439. args[1] = nop->args[1];
  440. args[2] = nop->args[2];
  441. } else {
  442. args[0] = 0;
  443. args[1] = 0;
  444. args[2] = 0;
  445. }
  446. spin_unlock_irqrestore(&s->nop_lock, flags);
  447. return nop;
  448. }
  449. static void locked_nop_work_func(struct work_struct *work)
  450. {
  451. int ret;
  452. struct trusty_work *tw = container_of(work, struct trusty_work, work);
  453. struct trusty_state *s = tw->ts;
  454. dev_dbg(s->dev, "%s\n", __func__);
  455. ret = trusty_std_call32(s->dev, SMC_SC_LOCKED_NOP, 0, 0, 0);
  456. if (ret != 0)
  457. dev_info(s->dev, "%s: SMC_SC_LOCKED_NOP failed %d",
  458. __func__, ret);
  459. dev_dbg(s->dev, "%s: done\n", __func__);
  460. }
  461. static void nop_work_func(struct work_struct *work)
  462. {
  463. int ret;
  464. bool next;
  465. u32 args[3];
  466. struct trusty_work *tw = container_of(work, struct trusty_work, work);
  467. struct trusty_state *s = tw->ts;
  468. dev_dbg(s->dev, "%s:\n", __func__);
  469. dequeue_nop(s, args);
  470. do {
  471. dev_dbg(s->dev, "%s: %x %x %x\n",
  472. __func__, args[0], args[1], args[2]);
  473. ret = trusty_std_call32(s->dev, SMC_SC_NOP,
  474. args[0], args[1], args[2]);
  475. next = dequeue_nop(s, args);
  476. if (ret == SM_ERR_NOP_INTERRUPTED)
  477. next = true;
  478. else if (ret != SM_ERR_NOP_DONE)
  479. dev_info(s->dev, "%s: SMC_SC_NOP failed %d",
  480. __func__, ret);
  481. } while (next);
  482. dev_dbg(s->dev, "%s: done\n", __func__);
  483. }
  484. void trusty_enqueue_nop(struct device *dev, struct trusty_nop *nop)
  485. {
  486. unsigned long flags;
  487. struct trusty_work *tw;
  488. struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
  489. preempt_disable();
  490. tw = this_cpu_ptr(s->nop_works);
  491. if (nop) {
  492. WARN_ON(s->api_version < TRUSTY_API_VERSION_SMP_NOP);
  493. spin_lock_irqsave(&s->nop_lock, flags);
  494. if (list_empty(&nop->node))
  495. list_add_tail(&nop->node, &s->nop_queue);
  496. spin_unlock_irqrestore(&s->nop_lock, flags);
  497. }
  498. queue_work(s->nop_wq, &tw->work);
  499. preempt_enable();
  500. }
  501. EXPORT_SYMBOL(trusty_enqueue_nop);
  502. void trusty_dequeue_nop(struct device *dev, struct trusty_nop *nop)
  503. {
  504. unsigned long flags;
  505. struct trusty_state *s = platform_get_drvdata(to_platform_device(dev));
  506. if (WARN_ON(!nop))
  507. return;
  508. spin_lock_irqsave(&s->nop_lock, flags);
  509. if (!list_empty(&nop->node))
  510. list_del_init(&nop->node);
  511. spin_unlock_irqrestore(&s->nop_lock, flags);
  512. }
  513. EXPORT_SYMBOL(trusty_dequeue_nop);
  514. static int trusty_probe(struct platform_device *pdev)
  515. {
  516. int ret;
  517. unsigned int cpu;
  518. work_func_t work_func;
  519. struct trusty_state *s;
  520. struct device_node *node = pdev->dev.of_node;
  521. if (!node) {
  522. dev_err(&pdev->dev, "of_node required\n");
  523. return -EINVAL;
  524. }
  525. s = kzalloc(sizeof(*s), GFP_KERNEL);
  526. if (!s) {
  527. ret = -ENOMEM;
  528. goto err_allocate_state;
  529. }
  530. s->dev = &pdev->dev;
  531. spin_lock_init(&s->nop_lock);
  532. INIT_LIST_HEAD(&s->nop_queue);
  533. mutex_init(&s->smc_lock);
  534. ATOMIC_INIT_NOTIFIER_HEAD(&s->notifier);
  535. init_completion(&s->cpu_idle_completion);
  536. platform_set_drvdata(pdev, s);
  537. trusty_init_version(s, &pdev->dev);
  538. ret = trusty_init_api_version(s, &pdev->dev);
  539. if (ret < 0)
  540. goto err_api_version;
  541. s->nop_wq = alloc_workqueue("trusty-nop-wq", WQ_CPU_INTENSIVE, 0);
  542. if (!s->nop_wq) {
  543. ret = -ENODEV;
  544. dev_info(&pdev->dev, "Failed create trusty-nop-wq\n");
  545. goto err_create_nop_wq;
  546. }
  547. s->nop_works = alloc_percpu(struct trusty_work);
  548. if (!s->nop_works) {
  549. ret = -ENOMEM;
  550. dev_info(&pdev->dev, "Failed to allocate works\n");
  551. goto err_alloc_works;
  552. }
  553. if (s->api_version < TRUSTY_API_VERSION_SMP)
  554. work_func = locked_nop_work_func;
  555. else
  556. work_func = nop_work_func;
  557. for_each_possible_cpu(cpu) {
  558. struct trusty_work *tw = per_cpu_ptr(s->nop_works, cpu);
  559. tw->ts = s;
  560. INIT_WORK(&tw->work, work_func);
  561. }
  562. ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
  563. if (ret < 0) {
  564. dev_err(&pdev->dev, "Failed to add children: %d\n", ret);
  565. goto err_add_children;
  566. }
  567. #ifdef CONFIG_MT_TRUSTY_DEBUGFS
  568. trusty_create_debugfs(s, &pdev->dev);
  569. #endif
  570. #if 0
  571. #ifdef CONFIG_MTK_ENABLE_GENIEZONE
  572. #ifdef CONFIG_MTK_RAM_CONSOLE
  573. init_gz_ramconsole(&pdev->dev);
  574. #endif
  575. #endif
  576. #endif
  577. return 0;
  578. err_add_children:
  579. for_each_possible_cpu(cpu) {
  580. struct trusty_work *tw = per_cpu_ptr(s->nop_works, cpu);
  581. flush_work(&tw->work);
  582. }
  583. free_percpu(s->nop_works);
  584. err_alloc_works:
  585. destroy_workqueue(s->nop_wq);
  586. err_create_nop_wq:
  587. err_api_version:
  588. if (s->version_str) {
  589. device_remove_file(&pdev->dev, &dev_attr_trusty_version);
  590. kfree(s->version_str);
  591. }
  592. device_for_each_child(&pdev->dev, NULL, trusty_remove_child);
  593. mutex_destroy(&s->smc_lock);
  594. kfree(s);
  595. err_allocate_state:
  596. return ret;
  597. }
  598. static int trusty_remove(struct platform_device *pdev)
  599. {
  600. unsigned int cpu;
  601. struct trusty_state *s = platform_get_drvdata(pdev);
  602. device_for_each_child(&pdev->dev, NULL, trusty_remove_child);
  603. for_each_possible_cpu(cpu) {
  604. struct trusty_work *tw = per_cpu_ptr(s->nop_works, cpu);
  605. flush_work(&tw->work);
  606. }
  607. free_percpu(s->nop_works);
  608. destroy_workqueue(s->nop_wq);
  609. mutex_destroy(&s->smc_lock);
  610. if (s->version_str) {
  611. device_remove_file(&pdev->dev, &dev_attr_trusty_version);
  612. kfree(s->version_str);
  613. }
  614. kfree(s);
  615. return 0;
  616. }
  617. static const struct of_device_id trusty_of_match[] = {
  618. { .compatible = "android,trusty-smc-v1", },
  619. {},
  620. };
  621. static struct platform_driver trusty_driver = {
  622. .probe = trusty_probe,
  623. .remove = trusty_remove,
  624. .driver = {
  625. .name = "trusty",
  626. .owner = THIS_MODULE,
  627. .of_match_table = trusty_of_match,
  628. },
  629. };
  630. static int __init trusty_driver_init(void)
  631. {
  632. return platform_driver_register(&trusty_driver);
  633. }
  634. static void __exit trusty_driver_exit(void)
  635. {
  636. platform_driver_unregister(&trusty_driver);
  637. }
  638. subsys_initcall(trusty_driver_init);
  639. module_exit(trusty_driver_exit);