qcom_scm.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2010,2015,2019 The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2015 Linaro Ltd.
  4. */
  5. #include <linux/arm-smccc.h>
  6. #include <linux/bitfield.h>
  7. #include <linux/bits.h>
  8. #include <linux/cleanup.h>
  9. #include <linux/clk.h>
  10. #include <linux/completion.h>
  11. #include <linux/cpumask.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/err.h>
  14. #include <linux/export.h>
  15. #include <linux/firmware/qcom/qcom_scm.h>
  16. #include <linux/firmware/qcom/qcom_tzmem.h>
  17. #include <linux/init.h>
  18. #include <linux/interconnect.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/kstrtox.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/of_platform.h>
  26. #include <linux/of_reserved_mem.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/reset-controller.h>
  29. #include <linux/sizes.h>
  30. #include <linux/types.h>
  31. #include "qcom_scm.h"
  32. #include "qcom_tzmem.h"
  33. static u32 download_mode;
  34. struct qcom_scm {
  35. struct device *dev;
  36. struct clk *core_clk;
  37. struct clk *iface_clk;
  38. struct clk *bus_clk;
  39. struct icc_path *path;
  40. struct completion waitq_comp;
  41. struct reset_controller_dev reset;
  42. /* control access to the interconnect path */
  43. struct mutex scm_bw_lock;
  44. int scm_vote_count;
  45. u64 dload_mode_addr;
  46. struct qcom_tzmem_pool *mempool;
  47. };
  48. struct qcom_scm_current_perm_info {
  49. __le32 vmid;
  50. __le32 perm;
  51. __le64 ctx;
  52. __le32 ctx_size;
  53. __le32 unused;
  54. };
  55. struct qcom_scm_mem_map_info {
  56. __le64 mem_addr;
  57. __le64 mem_size;
  58. };
  59. /**
  60. * struct qcom_scm_qseecom_resp - QSEECOM SCM call response.
  61. * @result: Result or status of the SCM call. See &enum qcom_scm_qseecom_result.
  62. * @resp_type: Type of the response. See &enum qcom_scm_qseecom_resp_type.
  63. * @data: Response data. The type of this data is given in @resp_type.
  64. */
  65. struct qcom_scm_qseecom_resp {
  66. u64 result;
  67. u64 resp_type;
  68. u64 data;
  69. };
  70. enum qcom_scm_qseecom_result {
  71. QSEECOM_RESULT_SUCCESS = 0,
  72. QSEECOM_RESULT_INCOMPLETE = 1,
  73. QSEECOM_RESULT_BLOCKED_ON_LISTENER = 2,
  74. QSEECOM_RESULT_FAILURE = 0xFFFFFFFF,
  75. };
  76. enum qcom_scm_qseecom_resp_type {
  77. QSEECOM_SCM_RES_APP_ID = 0xEE01,
  78. QSEECOM_SCM_RES_QSEOS_LISTENER_ID = 0xEE02,
  79. };
  80. enum qcom_scm_qseecom_tz_owner {
  81. QSEECOM_TZ_OWNER_SIP = 2,
  82. QSEECOM_TZ_OWNER_TZ_APPS = 48,
  83. QSEECOM_TZ_OWNER_QSEE_OS = 50
  84. };
  85. enum qcom_scm_qseecom_tz_svc {
  86. QSEECOM_TZ_SVC_APP_ID_PLACEHOLDER = 0,
  87. QSEECOM_TZ_SVC_APP_MGR = 1,
  88. QSEECOM_TZ_SVC_INFO = 6,
  89. };
  90. enum qcom_scm_qseecom_tz_cmd_app {
  91. QSEECOM_TZ_CMD_APP_SEND = 1,
  92. QSEECOM_TZ_CMD_APP_LOOKUP = 3,
  93. };
  94. enum qcom_scm_qseecom_tz_cmd_info {
  95. QSEECOM_TZ_CMD_INFO_VERSION = 3,
  96. };
  97. #define QSEECOM_MAX_APP_NAME_SIZE 64
  98. #define SHMBRIDGE_RESULT_NOTSUPP 4
  99. /* Each bit configures cold/warm boot address for one of the 4 CPUs */
  100. static const u8 qcom_scm_cpu_cold_bits[QCOM_SCM_BOOT_MAX_CPUS] = {
  101. 0, BIT(0), BIT(3), BIT(5)
  102. };
  103. static const u8 qcom_scm_cpu_warm_bits[QCOM_SCM_BOOT_MAX_CPUS] = {
  104. BIT(2), BIT(1), BIT(4), BIT(6)
  105. };
  106. #define QCOM_SMC_WAITQ_FLAG_WAKE_ONE BIT(0)
  107. #define QCOM_DLOAD_MASK GENMASK(5, 4)
  108. #define QCOM_DLOAD_NODUMP 0
  109. #define QCOM_DLOAD_FULLDUMP 1
  110. #define QCOM_DLOAD_MINIDUMP 2
  111. #define QCOM_DLOAD_BOTHDUMP 3
  112. static const char * const qcom_scm_convention_names[] = {
  113. [SMC_CONVENTION_UNKNOWN] = "unknown",
  114. [SMC_CONVENTION_ARM_32] = "smc arm 32",
  115. [SMC_CONVENTION_ARM_64] = "smc arm 64",
  116. [SMC_CONVENTION_LEGACY] = "smc legacy",
  117. };
  118. static const char * const download_mode_name[] = {
  119. [QCOM_DLOAD_NODUMP] = "off",
  120. [QCOM_DLOAD_FULLDUMP] = "full",
  121. [QCOM_DLOAD_MINIDUMP] = "mini",
  122. [QCOM_DLOAD_BOTHDUMP] = "full,mini",
  123. };
  124. static struct qcom_scm *__scm;
  125. static int qcom_scm_clk_enable(void)
  126. {
  127. int ret;
  128. ret = clk_prepare_enable(__scm->core_clk);
  129. if (ret)
  130. goto bail;
  131. ret = clk_prepare_enable(__scm->iface_clk);
  132. if (ret)
  133. goto disable_core;
  134. ret = clk_prepare_enable(__scm->bus_clk);
  135. if (ret)
  136. goto disable_iface;
  137. return 0;
  138. disable_iface:
  139. clk_disable_unprepare(__scm->iface_clk);
  140. disable_core:
  141. clk_disable_unprepare(__scm->core_clk);
  142. bail:
  143. return ret;
  144. }
  145. static void qcom_scm_clk_disable(void)
  146. {
  147. clk_disable_unprepare(__scm->core_clk);
  148. clk_disable_unprepare(__scm->iface_clk);
  149. clk_disable_unprepare(__scm->bus_clk);
  150. }
  151. static int qcom_scm_bw_enable(void)
  152. {
  153. int ret = 0;
  154. if (!__scm->path)
  155. return 0;
  156. mutex_lock(&__scm->scm_bw_lock);
  157. if (!__scm->scm_vote_count) {
  158. ret = icc_set_bw(__scm->path, 0, UINT_MAX);
  159. if (ret < 0) {
  160. dev_err(__scm->dev, "failed to set bandwidth request\n");
  161. goto err_bw;
  162. }
  163. }
  164. __scm->scm_vote_count++;
  165. err_bw:
  166. mutex_unlock(&__scm->scm_bw_lock);
  167. return ret;
  168. }
  169. static void qcom_scm_bw_disable(void)
  170. {
  171. if (!__scm->path)
  172. return;
  173. mutex_lock(&__scm->scm_bw_lock);
  174. if (__scm->scm_vote_count-- == 1)
  175. icc_set_bw(__scm->path, 0, 0);
  176. mutex_unlock(&__scm->scm_bw_lock);
  177. }
  178. enum qcom_scm_convention qcom_scm_convention = SMC_CONVENTION_UNKNOWN;
  179. static DEFINE_SPINLOCK(scm_query_lock);
  180. struct qcom_tzmem_pool *qcom_scm_get_tzmem_pool(void)
  181. {
  182. if (!qcom_scm_is_available())
  183. return NULL;
  184. return __scm->mempool;
  185. }
  186. static enum qcom_scm_convention __get_convention(void)
  187. {
  188. unsigned long flags;
  189. struct qcom_scm_desc desc = {
  190. .svc = QCOM_SCM_SVC_INFO,
  191. .cmd = QCOM_SCM_INFO_IS_CALL_AVAIL,
  192. .args[0] = SCM_SMC_FNID(QCOM_SCM_SVC_INFO,
  193. QCOM_SCM_INFO_IS_CALL_AVAIL) |
  194. (ARM_SMCCC_OWNER_SIP << ARM_SMCCC_OWNER_SHIFT),
  195. .arginfo = QCOM_SCM_ARGS(1),
  196. .owner = ARM_SMCCC_OWNER_SIP,
  197. };
  198. struct qcom_scm_res res;
  199. enum qcom_scm_convention probed_convention;
  200. int ret;
  201. bool forced = false;
  202. if (likely(qcom_scm_convention != SMC_CONVENTION_UNKNOWN))
  203. return qcom_scm_convention;
  204. /*
  205. * Per the "SMC calling convention specification", the 64-bit calling
  206. * convention can only be used when the client is 64-bit, otherwise
  207. * system will encounter the undefined behaviour.
  208. */
  209. #if IS_ENABLED(CONFIG_ARM64)
  210. /*
  211. * Device isn't required as there is only one argument - no device
  212. * needed to dma_map_single to secure world
  213. */
  214. probed_convention = SMC_CONVENTION_ARM_64;
  215. ret = __scm_smc_call(NULL, &desc, probed_convention, &res, true);
  216. if (!ret && res.result[0] == 1)
  217. goto found;
  218. /*
  219. * Some SC7180 firmwares didn't implement the
  220. * QCOM_SCM_INFO_IS_CALL_AVAIL call, so we fallback to forcing ARM_64
  221. * calling conventions on these firmwares. Luckily we don't make any
  222. * early calls into the firmware on these SoCs so the device pointer
  223. * will be valid here to check if the compatible matches.
  224. */
  225. if (of_device_is_compatible(__scm ? __scm->dev->of_node : NULL, "qcom,scm-sc7180")) {
  226. forced = true;
  227. goto found;
  228. }
  229. #endif
  230. probed_convention = SMC_CONVENTION_ARM_32;
  231. ret = __scm_smc_call(NULL, &desc, probed_convention, &res, true);
  232. if (!ret && res.result[0] == 1)
  233. goto found;
  234. probed_convention = SMC_CONVENTION_LEGACY;
  235. found:
  236. spin_lock_irqsave(&scm_query_lock, flags);
  237. if (probed_convention != qcom_scm_convention) {
  238. qcom_scm_convention = probed_convention;
  239. pr_info("qcom_scm: convention: %s%s\n",
  240. qcom_scm_convention_names[qcom_scm_convention],
  241. forced ? " (forced)" : "");
  242. }
  243. spin_unlock_irqrestore(&scm_query_lock, flags);
  244. return qcom_scm_convention;
  245. }
  246. /**
  247. * qcom_scm_call() - Invoke a syscall in the secure world
  248. * @dev: device
  249. * @desc: Descriptor structure containing arguments and return values
  250. * @res: Structure containing results from SMC/HVC call
  251. *
  252. * Sends a command to the SCM and waits for the command to finish processing.
  253. * This should *only* be called in pre-emptible context.
  254. */
  255. static int qcom_scm_call(struct device *dev, const struct qcom_scm_desc *desc,
  256. struct qcom_scm_res *res)
  257. {
  258. might_sleep();
  259. switch (__get_convention()) {
  260. case SMC_CONVENTION_ARM_32:
  261. case SMC_CONVENTION_ARM_64:
  262. return scm_smc_call(dev, desc, res, false);
  263. case SMC_CONVENTION_LEGACY:
  264. return scm_legacy_call(dev, desc, res);
  265. default:
  266. pr_err("Unknown current SCM calling convention.\n");
  267. return -EINVAL;
  268. }
  269. }
  270. /**
  271. * qcom_scm_call_atomic() - atomic variation of qcom_scm_call()
  272. * @dev: device
  273. * @desc: Descriptor structure containing arguments and return values
  274. * @res: Structure containing results from SMC/HVC call
  275. *
  276. * Sends a command to the SCM and waits for the command to finish processing.
  277. * This can be called in atomic context.
  278. */
  279. static int qcom_scm_call_atomic(struct device *dev,
  280. const struct qcom_scm_desc *desc,
  281. struct qcom_scm_res *res)
  282. {
  283. switch (__get_convention()) {
  284. case SMC_CONVENTION_ARM_32:
  285. case SMC_CONVENTION_ARM_64:
  286. return scm_smc_call(dev, desc, res, true);
  287. case SMC_CONVENTION_LEGACY:
  288. return scm_legacy_call_atomic(dev, desc, res);
  289. default:
  290. pr_err("Unknown current SCM calling convention.\n");
  291. return -EINVAL;
  292. }
  293. }
  294. static bool __qcom_scm_is_call_available(struct device *dev, u32 svc_id,
  295. u32 cmd_id)
  296. {
  297. int ret;
  298. struct qcom_scm_desc desc = {
  299. .svc = QCOM_SCM_SVC_INFO,
  300. .cmd = QCOM_SCM_INFO_IS_CALL_AVAIL,
  301. .owner = ARM_SMCCC_OWNER_SIP,
  302. };
  303. struct qcom_scm_res res;
  304. desc.arginfo = QCOM_SCM_ARGS(1);
  305. switch (__get_convention()) {
  306. case SMC_CONVENTION_ARM_32:
  307. case SMC_CONVENTION_ARM_64:
  308. desc.args[0] = SCM_SMC_FNID(svc_id, cmd_id) |
  309. (ARM_SMCCC_OWNER_SIP << ARM_SMCCC_OWNER_SHIFT);
  310. break;
  311. case SMC_CONVENTION_LEGACY:
  312. desc.args[0] = SCM_LEGACY_FNID(svc_id, cmd_id);
  313. break;
  314. default:
  315. pr_err("Unknown SMC convention being used\n");
  316. return false;
  317. }
  318. ret = qcom_scm_call(dev, &desc, &res);
  319. return ret ? false : !!res.result[0];
  320. }
  321. static int qcom_scm_set_boot_addr(void *entry, const u8 *cpu_bits)
  322. {
  323. int cpu;
  324. unsigned int flags = 0;
  325. struct qcom_scm_desc desc = {
  326. .svc = QCOM_SCM_SVC_BOOT,
  327. .cmd = QCOM_SCM_BOOT_SET_ADDR,
  328. .arginfo = QCOM_SCM_ARGS(2),
  329. .owner = ARM_SMCCC_OWNER_SIP,
  330. };
  331. for_each_present_cpu(cpu) {
  332. if (cpu >= QCOM_SCM_BOOT_MAX_CPUS)
  333. return -EINVAL;
  334. flags |= cpu_bits[cpu];
  335. }
  336. desc.args[0] = flags;
  337. desc.args[1] = virt_to_phys(entry);
  338. return qcom_scm_call_atomic(__scm ? __scm->dev : NULL, &desc, NULL);
  339. }
  340. static int qcom_scm_set_boot_addr_mc(void *entry, unsigned int flags)
  341. {
  342. struct qcom_scm_desc desc = {
  343. .svc = QCOM_SCM_SVC_BOOT,
  344. .cmd = QCOM_SCM_BOOT_SET_ADDR_MC,
  345. .owner = ARM_SMCCC_OWNER_SIP,
  346. .arginfo = QCOM_SCM_ARGS(6),
  347. .args = {
  348. virt_to_phys(entry),
  349. /* Apply to all CPUs in all affinity levels */
  350. ~0ULL, ~0ULL, ~0ULL, ~0ULL,
  351. flags,
  352. },
  353. };
  354. /* Need a device for DMA of the additional arguments */
  355. if (!__scm || __get_convention() == SMC_CONVENTION_LEGACY)
  356. return -EOPNOTSUPP;
  357. return qcom_scm_call(__scm->dev, &desc, NULL);
  358. }
  359. /**
  360. * qcom_scm_set_warm_boot_addr() - Set the warm boot address for all cpus
  361. * @entry: Entry point function for the cpus
  362. *
  363. * Set the Linux entry point for the SCM to transfer control to when coming
  364. * out of a power down. CPU power down may be executed on cpuidle or hotplug.
  365. */
  366. int qcom_scm_set_warm_boot_addr(void *entry)
  367. {
  368. if (qcom_scm_set_boot_addr_mc(entry, QCOM_SCM_BOOT_MC_FLAG_WARMBOOT))
  369. /* Fallback to old SCM call */
  370. return qcom_scm_set_boot_addr(entry, qcom_scm_cpu_warm_bits);
  371. return 0;
  372. }
  373. EXPORT_SYMBOL_GPL(qcom_scm_set_warm_boot_addr);
  374. /**
  375. * qcom_scm_set_cold_boot_addr() - Set the cold boot address for all cpus
  376. * @entry: Entry point function for the cpus
  377. */
  378. int qcom_scm_set_cold_boot_addr(void *entry)
  379. {
  380. if (qcom_scm_set_boot_addr_mc(entry, QCOM_SCM_BOOT_MC_FLAG_COLDBOOT))
  381. /* Fallback to old SCM call */
  382. return qcom_scm_set_boot_addr(entry, qcom_scm_cpu_cold_bits);
  383. return 0;
  384. }
  385. EXPORT_SYMBOL_GPL(qcom_scm_set_cold_boot_addr);
  386. /**
  387. * qcom_scm_cpu_power_down() - Power down the cpu
  388. * @flags: Flags to flush cache
  389. *
  390. * This is an end point to power down cpu. If there was a pending interrupt,
  391. * the control would return from this function, otherwise, the cpu jumps to the
  392. * warm boot entry point set for this cpu upon reset.
  393. */
  394. void qcom_scm_cpu_power_down(u32 flags)
  395. {
  396. struct qcom_scm_desc desc = {
  397. .svc = QCOM_SCM_SVC_BOOT,
  398. .cmd = QCOM_SCM_BOOT_TERMINATE_PC,
  399. .args[0] = flags & QCOM_SCM_FLUSH_FLAG_MASK,
  400. .arginfo = QCOM_SCM_ARGS(1),
  401. .owner = ARM_SMCCC_OWNER_SIP,
  402. };
  403. qcom_scm_call_atomic(__scm ? __scm->dev : NULL, &desc, NULL);
  404. }
  405. EXPORT_SYMBOL_GPL(qcom_scm_cpu_power_down);
  406. int qcom_scm_set_remote_state(u32 state, u32 id)
  407. {
  408. struct qcom_scm_desc desc = {
  409. .svc = QCOM_SCM_SVC_BOOT,
  410. .cmd = QCOM_SCM_BOOT_SET_REMOTE_STATE,
  411. .arginfo = QCOM_SCM_ARGS(2),
  412. .args[0] = state,
  413. .args[1] = id,
  414. .owner = ARM_SMCCC_OWNER_SIP,
  415. };
  416. struct qcom_scm_res res;
  417. int ret;
  418. ret = qcom_scm_call(__scm->dev, &desc, &res);
  419. return ret ? : res.result[0];
  420. }
  421. EXPORT_SYMBOL_GPL(qcom_scm_set_remote_state);
  422. static int qcom_scm_disable_sdi(void)
  423. {
  424. int ret;
  425. struct qcom_scm_desc desc = {
  426. .svc = QCOM_SCM_SVC_BOOT,
  427. .cmd = QCOM_SCM_BOOT_SDI_CONFIG,
  428. .args[0] = 1, /* Disable watchdog debug */
  429. .args[1] = 0, /* Disable SDI */
  430. .arginfo = QCOM_SCM_ARGS(2),
  431. .owner = ARM_SMCCC_OWNER_SIP,
  432. };
  433. struct qcom_scm_res res;
  434. ret = qcom_scm_clk_enable();
  435. if (ret)
  436. return ret;
  437. ret = qcom_scm_call(__scm->dev, &desc, &res);
  438. qcom_scm_clk_disable();
  439. return ret ? : res.result[0];
  440. }
  441. static int __qcom_scm_set_dload_mode(struct device *dev, bool enable)
  442. {
  443. struct qcom_scm_desc desc = {
  444. .svc = QCOM_SCM_SVC_BOOT,
  445. .cmd = QCOM_SCM_BOOT_SET_DLOAD_MODE,
  446. .arginfo = QCOM_SCM_ARGS(2),
  447. .args[0] = QCOM_SCM_BOOT_SET_DLOAD_MODE,
  448. .owner = ARM_SMCCC_OWNER_SIP,
  449. };
  450. desc.args[1] = enable ? QCOM_SCM_BOOT_SET_DLOAD_MODE : 0;
  451. return qcom_scm_call_atomic(__scm->dev, &desc, NULL);
  452. }
  453. static int qcom_scm_io_rmw(phys_addr_t addr, unsigned int mask, unsigned int val)
  454. {
  455. unsigned int old;
  456. unsigned int new;
  457. int ret;
  458. ret = qcom_scm_io_readl(addr, &old);
  459. if (ret)
  460. return ret;
  461. new = (old & ~mask) | (val & mask);
  462. return qcom_scm_io_writel(addr, new);
  463. }
  464. static void qcom_scm_set_download_mode(u32 dload_mode)
  465. {
  466. int ret = 0;
  467. if (__scm->dload_mode_addr) {
  468. ret = qcom_scm_io_rmw(__scm->dload_mode_addr, QCOM_DLOAD_MASK,
  469. FIELD_PREP(QCOM_DLOAD_MASK, dload_mode));
  470. } else if (__qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_BOOT,
  471. QCOM_SCM_BOOT_SET_DLOAD_MODE)) {
  472. ret = __qcom_scm_set_dload_mode(__scm->dev, !!dload_mode);
  473. } else if (dload_mode) {
  474. dev_err(__scm->dev,
  475. "No available mechanism for setting download mode\n");
  476. }
  477. if (ret)
  478. dev_err(__scm->dev, "failed to set download mode: %d\n", ret);
  479. }
  480. /**
  481. * qcom_scm_pas_init_image() - Initialize peripheral authentication service
  482. * state machine for a given peripheral, using the
  483. * metadata
  484. * @peripheral: peripheral id
  485. * @metadata: pointer to memory containing ELF header, program header table
  486. * and optional blob of data used for authenticating the metadata
  487. * and the rest of the firmware
  488. * @size: size of the metadata
  489. * @ctx: optional metadata context
  490. *
  491. * Return: 0 on success.
  492. *
  493. * Upon successful return, the PAS metadata context (@ctx) will be used to
  494. * track the metadata allocation, this needs to be released by invoking
  495. * qcom_scm_pas_metadata_release() by the caller.
  496. */
  497. int qcom_scm_pas_init_image(u32 peripheral, const void *metadata, size_t size,
  498. struct qcom_scm_pas_metadata *ctx)
  499. {
  500. dma_addr_t mdata_phys;
  501. void *mdata_buf;
  502. int ret;
  503. struct qcom_scm_desc desc = {
  504. .svc = QCOM_SCM_SVC_PIL,
  505. .cmd = QCOM_SCM_PIL_PAS_INIT_IMAGE,
  506. .arginfo = QCOM_SCM_ARGS(2, QCOM_SCM_VAL, QCOM_SCM_RW),
  507. .args[0] = peripheral,
  508. .owner = ARM_SMCCC_OWNER_SIP,
  509. };
  510. struct qcom_scm_res res;
  511. /*
  512. * During the scm call memory protection will be enabled for the meta
  513. * data blob, so make sure it's physically contiguous, 4K aligned and
  514. * non-cachable to avoid XPU violations.
  515. *
  516. * For PIL calls the hypervisor creates SHM Bridges for the blob
  517. * buffers on behalf of Linux so we must not do it ourselves hence
  518. * not using the TZMem allocator here.
  519. *
  520. * If we pass a buffer that is already part of an SHM Bridge to this
  521. * call, it will fail.
  522. */
  523. mdata_buf = dma_alloc_coherent(__scm->dev, size, &mdata_phys,
  524. GFP_KERNEL);
  525. if (!mdata_buf)
  526. return -ENOMEM;
  527. memcpy(mdata_buf, metadata, size);
  528. ret = qcom_scm_clk_enable();
  529. if (ret)
  530. goto out;
  531. ret = qcom_scm_bw_enable();
  532. if (ret)
  533. goto disable_clk;
  534. desc.args[1] = mdata_phys;
  535. ret = qcom_scm_call(__scm->dev, &desc, &res);
  536. qcom_scm_bw_disable();
  537. disable_clk:
  538. qcom_scm_clk_disable();
  539. out:
  540. if (ret < 0 || !ctx) {
  541. dma_free_coherent(__scm->dev, size, mdata_buf, mdata_phys);
  542. } else if (ctx) {
  543. ctx->ptr = mdata_buf;
  544. ctx->phys = mdata_phys;
  545. ctx->size = size;
  546. }
  547. return ret ? : res.result[0];
  548. }
  549. EXPORT_SYMBOL_GPL(qcom_scm_pas_init_image);
  550. /**
  551. * qcom_scm_pas_metadata_release() - release metadata context
  552. * @ctx: metadata context
  553. */
  554. void qcom_scm_pas_metadata_release(struct qcom_scm_pas_metadata *ctx)
  555. {
  556. if (!ctx->ptr)
  557. return;
  558. dma_free_coherent(__scm->dev, ctx->size, ctx->ptr, ctx->phys);
  559. ctx->ptr = NULL;
  560. ctx->phys = 0;
  561. ctx->size = 0;
  562. }
  563. EXPORT_SYMBOL_GPL(qcom_scm_pas_metadata_release);
  564. /**
  565. * qcom_scm_pas_mem_setup() - Prepare the memory related to a given peripheral
  566. * for firmware loading
  567. * @peripheral: peripheral id
  568. * @addr: start address of memory area to prepare
  569. * @size: size of the memory area to prepare
  570. *
  571. * Returns 0 on success.
  572. */
  573. int qcom_scm_pas_mem_setup(u32 peripheral, phys_addr_t addr, phys_addr_t size)
  574. {
  575. int ret;
  576. struct qcom_scm_desc desc = {
  577. .svc = QCOM_SCM_SVC_PIL,
  578. .cmd = QCOM_SCM_PIL_PAS_MEM_SETUP,
  579. .arginfo = QCOM_SCM_ARGS(3),
  580. .args[0] = peripheral,
  581. .args[1] = addr,
  582. .args[2] = size,
  583. .owner = ARM_SMCCC_OWNER_SIP,
  584. };
  585. struct qcom_scm_res res;
  586. ret = qcom_scm_clk_enable();
  587. if (ret)
  588. return ret;
  589. ret = qcom_scm_bw_enable();
  590. if (ret)
  591. goto disable_clk;
  592. ret = qcom_scm_call(__scm->dev, &desc, &res);
  593. qcom_scm_bw_disable();
  594. disable_clk:
  595. qcom_scm_clk_disable();
  596. return ret ? : res.result[0];
  597. }
  598. EXPORT_SYMBOL_GPL(qcom_scm_pas_mem_setup);
  599. /**
  600. * qcom_scm_pas_auth_and_reset() - Authenticate the given peripheral firmware
  601. * and reset the remote processor
  602. * @peripheral: peripheral id
  603. *
  604. * Return 0 on success.
  605. */
  606. int qcom_scm_pas_auth_and_reset(u32 peripheral)
  607. {
  608. int ret;
  609. struct qcom_scm_desc desc = {
  610. .svc = QCOM_SCM_SVC_PIL,
  611. .cmd = QCOM_SCM_PIL_PAS_AUTH_AND_RESET,
  612. .arginfo = QCOM_SCM_ARGS(1),
  613. .args[0] = peripheral,
  614. .owner = ARM_SMCCC_OWNER_SIP,
  615. };
  616. struct qcom_scm_res res;
  617. ret = qcom_scm_clk_enable();
  618. if (ret)
  619. return ret;
  620. ret = qcom_scm_bw_enable();
  621. if (ret)
  622. goto disable_clk;
  623. ret = qcom_scm_call(__scm->dev, &desc, &res);
  624. qcom_scm_bw_disable();
  625. disable_clk:
  626. qcom_scm_clk_disable();
  627. return ret ? : res.result[0];
  628. }
  629. EXPORT_SYMBOL_GPL(qcom_scm_pas_auth_and_reset);
  630. /**
  631. * qcom_scm_pas_shutdown() - Shut down the remote processor
  632. * @peripheral: peripheral id
  633. *
  634. * Returns 0 on success.
  635. */
  636. int qcom_scm_pas_shutdown(u32 peripheral)
  637. {
  638. int ret;
  639. struct qcom_scm_desc desc = {
  640. .svc = QCOM_SCM_SVC_PIL,
  641. .cmd = QCOM_SCM_PIL_PAS_SHUTDOWN,
  642. .arginfo = QCOM_SCM_ARGS(1),
  643. .args[0] = peripheral,
  644. .owner = ARM_SMCCC_OWNER_SIP,
  645. };
  646. struct qcom_scm_res res;
  647. ret = qcom_scm_clk_enable();
  648. if (ret)
  649. return ret;
  650. ret = qcom_scm_bw_enable();
  651. if (ret)
  652. goto disable_clk;
  653. ret = qcom_scm_call(__scm->dev, &desc, &res);
  654. qcom_scm_bw_disable();
  655. disable_clk:
  656. qcom_scm_clk_disable();
  657. return ret ? : res.result[0];
  658. }
  659. EXPORT_SYMBOL_GPL(qcom_scm_pas_shutdown);
  660. /**
  661. * qcom_scm_pas_supported() - Check if the peripheral authentication service is
  662. * available for the given peripherial
  663. * @peripheral: peripheral id
  664. *
  665. * Returns true if PAS is supported for this peripheral, otherwise false.
  666. */
  667. bool qcom_scm_pas_supported(u32 peripheral)
  668. {
  669. int ret;
  670. struct qcom_scm_desc desc = {
  671. .svc = QCOM_SCM_SVC_PIL,
  672. .cmd = QCOM_SCM_PIL_PAS_IS_SUPPORTED,
  673. .arginfo = QCOM_SCM_ARGS(1),
  674. .args[0] = peripheral,
  675. .owner = ARM_SMCCC_OWNER_SIP,
  676. };
  677. struct qcom_scm_res res;
  678. if (!__qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_PIL,
  679. QCOM_SCM_PIL_PAS_IS_SUPPORTED))
  680. return false;
  681. ret = qcom_scm_call(__scm->dev, &desc, &res);
  682. return ret ? false : !!res.result[0];
  683. }
  684. EXPORT_SYMBOL_GPL(qcom_scm_pas_supported);
  685. static int __qcom_scm_pas_mss_reset(struct device *dev, bool reset)
  686. {
  687. struct qcom_scm_desc desc = {
  688. .svc = QCOM_SCM_SVC_PIL,
  689. .cmd = QCOM_SCM_PIL_PAS_MSS_RESET,
  690. .arginfo = QCOM_SCM_ARGS(2),
  691. .args[0] = reset,
  692. .args[1] = 0,
  693. .owner = ARM_SMCCC_OWNER_SIP,
  694. };
  695. struct qcom_scm_res res;
  696. int ret;
  697. ret = qcom_scm_call(__scm->dev, &desc, &res);
  698. return ret ? : res.result[0];
  699. }
  700. static int qcom_scm_pas_reset_assert(struct reset_controller_dev *rcdev,
  701. unsigned long idx)
  702. {
  703. if (idx != 0)
  704. return -EINVAL;
  705. return __qcom_scm_pas_mss_reset(__scm->dev, 1);
  706. }
  707. static int qcom_scm_pas_reset_deassert(struct reset_controller_dev *rcdev,
  708. unsigned long idx)
  709. {
  710. if (idx != 0)
  711. return -EINVAL;
  712. return __qcom_scm_pas_mss_reset(__scm->dev, 0);
  713. }
  714. static const struct reset_control_ops qcom_scm_pas_reset_ops = {
  715. .assert = qcom_scm_pas_reset_assert,
  716. .deassert = qcom_scm_pas_reset_deassert,
  717. };
  718. int qcom_scm_io_readl(phys_addr_t addr, unsigned int *val)
  719. {
  720. struct qcom_scm_desc desc = {
  721. .svc = QCOM_SCM_SVC_IO,
  722. .cmd = QCOM_SCM_IO_READ,
  723. .arginfo = QCOM_SCM_ARGS(1),
  724. .args[0] = addr,
  725. .owner = ARM_SMCCC_OWNER_SIP,
  726. };
  727. struct qcom_scm_res res;
  728. int ret;
  729. ret = qcom_scm_call_atomic(__scm->dev, &desc, &res);
  730. if (ret >= 0)
  731. *val = res.result[0];
  732. return ret < 0 ? ret : 0;
  733. }
  734. EXPORT_SYMBOL_GPL(qcom_scm_io_readl);
  735. int qcom_scm_io_writel(phys_addr_t addr, unsigned int val)
  736. {
  737. struct qcom_scm_desc desc = {
  738. .svc = QCOM_SCM_SVC_IO,
  739. .cmd = QCOM_SCM_IO_WRITE,
  740. .arginfo = QCOM_SCM_ARGS(2),
  741. .args[0] = addr,
  742. .args[1] = val,
  743. .owner = ARM_SMCCC_OWNER_SIP,
  744. };
  745. return qcom_scm_call_atomic(__scm->dev, &desc, NULL);
  746. }
  747. EXPORT_SYMBOL_GPL(qcom_scm_io_writel);
  748. /**
  749. * qcom_scm_restore_sec_cfg_available() - Check if secure environment
  750. * supports restore security config interface.
  751. *
  752. * Return true if restore-cfg interface is supported, false if not.
  753. */
  754. bool qcom_scm_restore_sec_cfg_available(void)
  755. {
  756. return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_MP,
  757. QCOM_SCM_MP_RESTORE_SEC_CFG);
  758. }
  759. EXPORT_SYMBOL_GPL(qcom_scm_restore_sec_cfg_available);
  760. int qcom_scm_restore_sec_cfg(u32 device_id, u32 spare)
  761. {
  762. struct qcom_scm_desc desc = {
  763. .svc = QCOM_SCM_SVC_MP,
  764. .cmd = QCOM_SCM_MP_RESTORE_SEC_CFG,
  765. .arginfo = QCOM_SCM_ARGS(2),
  766. .args[0] = device_id,
  767. .args[1] = spare,
  768. .owner = ARM_SMCCC_OWNER_SIP,
  769. };
  770. struct qcom_scm_res res;
  771. int ret;
  772. ret = qcom_scm_call(__scm->dev, &desc, &res);
  773. return ret ? : res.result[0];
  774. }
  775. EXPORT_SYMBOL_GPL(qcom_scm_restore_sec_cfg);
  776. int qcom_scm_iommu_secure_ptbl_size(u32 spare, size_t *size)
  777. {
  778. struct qcom_scm_desc desc = {
  779. .svc = QCOM_SCM_SVC_MP,
  780. .cmd = QCOM_SCM_MP_IOMMU_SECURE_PTBL_SIZE,
  781. .arginfo = QCOM_SCM_ARGS(1),
  782. .args[0] = spare,
  783. .owner = ARM_SMCCC_OWNER_SIP,
  784. };
  785. struct qcom_scm_res res;
  786. int ret;
  787. ret = qcom_scm_call(__scm->dev, &desc, &res);
  788. if (size)
  789. *size = res.result[0];
  790. return ret ? : res.result[1];
  791. }
  792. EXPORT_SYMBOL_GPL(qcom_scm_iommu_secure_ptbl_size);
  793. int qcom_scm_iommu_secure_ptbl_init(u64 addr, u32 size, u32 spare)
  794. {
  795. struct qcom_scm_desc desc = {
  796. .svc = QCOM_SCM_SVC_MP,
  797. .cmd = QCOM_SCM_MP_IOMMU_SECURE_PTBL_INIT,
  798. .arginfo = QCOM_SCM_ARGS(3, QCOM_SCM_RW, QCOM_SCM_VAL,
  799. QCOM_SCM_VAL),
  800. .args[0] = addr,
  801. .args[1] = size,
  802. .args[2] = spare,
  803. .owner = ARM_SMCCC_OWNER_SIP,
  804. };
  805. int ret;
  806. ret = qcom_scm_call(__scm->dev, &desc, NULL);
  807. /* the pg table has been initialized already, ignore the error */
  808. if (ret == -EPERM)
  809. ret = 0;
  810. return ret;
  811. }
  812. EXPORT_SYMBOL_GPL(qcom_scm_iommu_secure_ptbl_init);
  813. int qcom_scm_iommu_set_cp_pool_size(u32 spare, u32 size)
  814. {
  815. struct qcom_scm_desc desc = {
  816. .svc = QCOM_SCM_SVC_MP,
  817. .cmd = QCOM_SCM_MP_IOMMU_SET_CP_POOL_SIZE,
  818. .arginfo = QCOM_SCM_ARGS(2),
  819. .args[0] = size,
  820. .args[1] = spare,
  821. .owner = ARM_SMCCC_OWNER_SIP,
  822. };
  823. return qcom_scm_call(__scm->dev, &desc, NULL);
  824. }
  825. EXPORT_SYMBOL_GPL(qcom_scm_iommu_set_cp_pool_size);
  826. int qcom_scm_mem_protect_video_var(u32 cp_start, u32 cp_size,
  827. u32 cp_nonpixel_start,
  828. u32 cp_nonpixel_size)
  829. {
  830. int ret;
  831. struct qcom_scm_desc desc = {
  832. .svc = QCOM_SCM_SVC_MP,
  833. .cmd = QCOM_SCM_MP_VIDEO_VAR,
  834. .arginfo = QCOM_SCM_ARGS(4, QCOM_SCM_VAL, QCOM_SCM_VAL,
  835. QCOM_SCM_VAL, QCOM_SCM_VAL),
  836. .args[0] = cp_start,
  837. .args[1] = cp_size,
  838. .args[2] = cp_nonpixel_start,
  839. .args[3] = cp_nonpixel_size,
  840. .owner = ARM_SMCCC_OWNER_SIP,
  841. };
  842. struct qcom_scm_res res;
  843. ret = qcom_scm_call(__scm->dev, &desc, &res);
  844. return ret ? : res.result[0];
  845. }
  846. EXPORT_SYMBOL_GPL(qcom_scm_mem_protect_video_var);
  847. static int __qcom_scm_assign_mem(struct device *dev, phys_addr_t mem_region,
  848. size_t mem_sz, phys_addr_t src, size_t src_sz,
  849. phys_addr_t dest, size_t dest_sz)
  850. {
  851. int ret;
  852. struct qcom_scm_desc desc = {
  853. .svc = QCOM_SCM_SVC_MP,
  854. .cmd = QCOM_SCM_MP_ASSIGN,
  855. .arginfo = QCOM_SCM_ARGS(7, QCOM_SCM_RO, QCOM_SCM_VAL,
  856. QCOM_SCM_RO, QCOM_SCM_VAL, QCOM_SCM_RO,
  857. QCOM_SCM_VAL, QCOM_SCM_VAL),
  858. .args[0] = mem_region,
  859. .args[1] = mem_sz,
  860. .args[2] = src,
  861. .args[3] = src_sz,
  862. .args[4] = dest,
  863. .args[5] = dest_sz,
  864. .args[6] = 0,
  865. .owner = ARM_SMCCC_OWNER_SIP,
  866. };
  867. struct qcom_scm_res res;
  868. ret = qcom_scm_call(dev, &desc, &res);
  869. return ret ? : res.result[0];
  870. }
  871. /**
  872. * qcom_scm_assign_mem() - Make a secure call to reassign memory ownership
  873. * @mem_addr: mem region whose ownership need to be reassigned
  874. * @mem_sz: size of the region.
  875. * @srcvm: vmid for current set of owners, each set bit in
  876. * flag indicate a unique owner
  877. * @newvm: array having new owners and corresponding permission
  878. * flags
  879. * @dest_cnt: number of owners in next set.
  880. *
  881. * Return negative errno on failure or 0 on success with @srcvm updated.
  882. */
  883. int qcom_scm_assign_mem(phys_addr_t mem_addr, size_t mem_sz,
  884. u64 *srcvm,
  885. const struct qcom_scm_vmperm *newvm,
  886. unsigned int dest_cnt)
  887. {
  888. struct qcom_scm_current_perm_info *destvm;
  889. struct qcom_scm_mem_map_info *mem_to_map;
  890. phys_addr_t mem_to_map_phys;
  891. phys_addr_t dest_phys;
  892. phys_addr_t ptr_phys;
  893. size_t mem_to_map_sz;
  894. size_t dest_sz;
  895. size_t src_sz;
  896. size_t ptr_sz;
  897. int next_vm;
  898. __le32 *src;
  899. int ret, i, b;
  900. u64 srcvm_bits = *srcvm;
  901. src_sz = hweight64(srcvm_bits) * sizeof(*src);
  902. mem_to_map_sz = sizeof(*mem_to_map);
  903. dest_sz = dest_cnt * sizeof(*destvm);
  904. ptr_sz = ALIGN(src_sz, SZ_64) + ALIGN(mem_to_map_sz, SZ_64) +
  905. ALIGN(dest_sz, SZ_64);
  906. void *ptr __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
  907. ptr_sz, GFP_KERNEL);
  908. if (!ptr)
  909. return -ENOMEM;
  910. ptr_phys = qcom_tzmem_to_phys(ptr);
  911. /* Fill source vmid detail */
  912. src = ptr;
  913. i = 0;
  914. for (b = 0; b < BITS_PER_TYPE(u64); b++) {
  915. if (srcvm_bits & BIT(b))
  916. src[i++] = cpu_to_le32(b);
  917. }
  918. /* Fill details of mem buff to map */
  919. mem_to_map = ptr + ALIGN(src_sz, SZ_64);
  920. mem_to_map_phys = ptr_phys + ALIGN(src_sz, SZ_64);
  921. mem_to_map->mem_addr = cpu_to_le64(mem_addr);
  922. mem_to_map->mem_size = cpu_to_le64(mem_sz);
  923. next_vm = 0;
  924. /* Fill details of next vmid detail */
  925. destvm = ptr + ALIGN(mem_to_map_sz, SZ_64) + ALIGN(src_sz, SZ_64);
  926. dest_phys = ptr_phys + ALIGN(mem_to_map_sz, SZ_64) + ALIGN(src_sz, SZ_64);
  927. for (i = 0; i < dest_cnt; i++, destvm++, newvm++) {
  928. destvm->vmid = cpu_to_le32(newvm->vmid);
  929. destvm->perm = cpu_to_le32(newvm->perm);
  930. destvm->ctx = 0;
  931. destvm->ctx_size = 0;
  932. next_vm |= BIT(newvm->vmid);
  933. }
  934. ret = __qcom_scm_assign_mem(__scm->dev, mem_to_map_phys, mem_to_map_sz,
  935. ptr_phys, src_sz, dest_phys, dest_sz);
  936. if (ret) {
  937. dev_err(__scm->dev,
  938. "Assign memory protection call failed %d\n", ret);
  939. return -EINVAL;
  940. }
  941. *srcvm = next_vm;
  942. return 0;
  943. }
  944. EXPORT_SYMBOL_GPL(qcom_scm_assign_mem);
  945. /**
  946. * qcom_scm_ocmem_lock_available() - is OCMEM lock/unlock interface available
  947. */
  948. bool qcom_scm_ocmem_lock_available(void)
  949. {
  950. return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_OCMEM,
  951. QCOM_SCM_OCMEM_LOCK_CMD);
  952. }
  953. EXPORT_SYMBOL_GPL(qcom_scm_ocmem_lock_available);
  954. /**
  955. * qcom_scm_ocmem_lock() - call OCMEM lock interface to assign an OCMEM
  956. * region to the specified initiator
  957. *
  958. * @id: tz initiator id
  959. * @offset: OCMEM offset
  960. * @size: OCMEM size
  961. * @mode: access mode (WIDE/NARROW)
  962. */
  963. int qcom_scm_ocmem_lock(enum qcom_scm_ocmem_client id, u32 offset, u32 size,
  964. u32 mode)
  965. {
  966. struct qcom_scm_desc desc = {
  967. .svc = QCOM_SCM_SVC_OCMEM,
  968. .cmd = QCOM_SCM_OCMEM_LOCK_CMD,
  969. .args[0] = id,
  970. .args[1] = offset,
  971. .args[2] = size,
  972. .args[3] = mode,
  973. .arginfo = QCOM_SCM_ARGS(4),
  974. };
  975. return qcom_scm_call(__scm->dev, &desc, NULL);
  976. }
  977. EXPORT_SYMBOL_GPL(qcom_scm_ocmem_lock);
  978. /**
  979. * qcom_scm_ocmem_unlock() - call OCMEM unlock interface to release an OCMEM
  980. * region from the specified initiator
  981. *
  982. * @id: tz initiator id
  983. * @offset: OCMEM offset
  984. * @size: OCMEM size
  985. */
  986. int qcom_scm_ocmem_unlock(enum qcom_scm_ocmem_client id, u32 offset, u32 size)
  987. {
  988. struct qcom_scm_desc desc = {
  989. .svc = QCOM_SCM_SVC_OCMEM,
  990. .cmd = QCOM_SCM_OCMEM_UNLOCK_CMD,
  991. .args[0] = id,
  992. .args[1] = offset,
  993. .args[2] = size,
  994. .arginfo = QCOM_SCM_ARGS(3),
  995. };
  996. return qcom_scm_call(__scm->dev, &desc, NULL);
  997. }
  998. EXPORT_SYMBOL_GPL(qcom_scm_ocmem_unlock);
  999. /**
  1000. * qcom_scm_ice_available() - Is the ICE key programming interface available?
  1001. *
  1002. * Return: true iff the SCM calls wrapped by qcom_scm_ice_invalidate_key() and
  1003. * qcom_scm_ice_set_key() are available.
  1004. */
  1005. bool qcom_scm_ice_available(void)
  1006. {
  1007. return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_ES,
  1008. QCOM_SCM_ES_INVALIDATE_ICE_KEY) &&
  1009. __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_ES,
  1010. QCOM_SCM_ES_CONFIG_SET_ICE_KEY);
  1011. }
  1012. EXPORT_SYMBOL_GPL(qcom_scm_ice_available);
  1013. /**
  1014. * qcom_scm_ice_invalidate_key() - Invalidate an inline encryption key
  1015. * @index: the keyslot to invalidate
  1016. *
  1017. * The UFSHCI and eMMC standards define a standard way to do this, but it
  1018. * doesn't work on these SoCs; only this SCM call does.
  1019. *
  1020. * It is assumed that the SoC has only one ICE instance being used, as this SCM
  1021. * call doesn't specify which ICE instance the keyslot belongs to.
  1022. *
  1023. * Return: 0 on success; -errno on failure.
  1024. */
  1025. int qcom_scm_ice_invalidate_key(u32 index)
  1026. {
  1027. struct qcom_scm_desc desc = {
  1028. .svc = QCOM_SCM_SVC_ES,
  1029. .cmd = QCOM_SCM_ES_INVALIDATE_ICE_KEY,
  1030. .arginfo = QCOM_SCM_ARGS(1),
  1031. .args[0] = index,
  1032. .owner = ARM_SMCCC_OWNER_SIP,
  1033. };
  1034. return qcom_scm_call(__scm->dev, &desc, NULL);
  1035. }
  1036. EXPORT_SYMBOL_GPL(qcom_scm_ice_invalidate_key);
  1037. /**
  1038. * qcom_scm_ice_set_key() - Set an inline encryption key
  1039. * @index: the keyslot into which to set the key
  1040. * @key: the key to program
  1041. * @key_size: the size of the key in bytes
  1042. * @cipher: the encryption algorithm the key is for
  1043. * @data_unit_size: the encryption data unit size, i.e. the size of each
  1044. * individual plaintext and ciphertext. Given in 512-byte
  1045. * units, e.g. 1 = 512 bytes, 8 = 4096 bytes, etc.
  1046. *
  1047. * Program a key into a keyslot of Qualcomm ICE (Inline Crypto Engine), where it
  1048. * can then be used to encrypt/decrypt UFS or eMMC I/O requests inline.
  1049. *
  1050. * The UFSHCI and eMMC standards define a standard way to do this, but it
  1051. * doesn't work on these SoCs; only this SCM call does.
  1052. *
  1053. * It is assumed that the SoC has only one ICE instance being used, as this SCM
  1054. * call doesn't specify which ICE instance the keyslot belongs to.
  1055. *
  1056. * Return: 0 on success; -errno on failure.
  1057. */
  1058. int qcom_scm_ice_set_key(u32 index, const u8 *key, u32 key_size,
  1059. enum qcom_scm_ice_cipher cipher, u32 data_unit_size)
  1060. {
  1061. struct qcom_scm_desc desc = {
  1062. .svc = QCOM_SCM_SVC_ES,
  1063. .cmd = QCOM_SCM_ES_CONFIG_SET_ICE_KEY,
  1064. .arginfo = QCOM_SCM_ARGS(5, QCOM_SCM_VAL, QCOM_SCM_RW,
  1065. QCOM_SCM_VAL, QCOM_SCM_VAL,
  1066. QCOM_SCM_VAL),
  1067. .args[0] = index,
  1068. .args[2] = key_size,
  1069. .args[3] = cipher,
  1070. .args[4] = data_unit_size,
  1071. .owner = ARM_SMCCC_OWNER_SIP,
  1072. };
  1073. int ret;
  1074. void *keybuf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
  1075. key_size,
  1076. GFP_KERNEL);
  1077. if (!keybuf)
  1078. return -ENOMEM;
  1079. memcpy(keybuf, key, key_size);
  1080. desc.args[1] = qcom_tzmem_to_phys(keybuf);
  1081. ret = qcom_scm_call(__scm->dev, &desc, NULL);
  1082. memzero_explicit(keybuf, key_size);
  1083. return ret;
  1084. }
  1085. EXPORT_SYMBOL_GPL(qcom_scm_ice_set_key);
  1086. /**
  1087. * qcom_scm_hdcp_available() - Check if secure environment supports HDCP.
  1088. *
  1089. * Return true if HDCP is supported, false if not.
  1090. */
  1091. bool qcom_scm_hdcp_available(void)
  1092. {
  1093. bool avail;
  1094. int ret = qcom_scm_clk_enable();
  1095. if (ret)
  1096. return ret;
  1097. avail = __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_HDCP,
  1098. QCOM_SCM_HDCP_INVOKE);
  1099. qcom_scm_clk_disable();
  1100. return avail;
  1101. }
  1102. EXPORT_SYMBOL_GPL(qcom_scm_hdcp_available);
  1103. /**
  1104. * qcom_scm_hdcp_req() - Send HDCP request.
  1105. * @req: HDCP request array
  1106. * @req_cnt: HDCP request array count
  1107. * @resp: response buffer passed to SCM
  1108. *
  1109. * Write HDCP register(s) through SCM.
  1110. */
  1111. int qcom_scm_hdcp_req(struct qcom_scm_hdcp_req *req, u32 req_cnt, u32 *resp)
  1112. {
  1113. int ret;
  1114. struct qcom_scm_desc desc = {
  1115. .svc = QCOM_SCM_SVC_HDCP,
  1116. .cmd = QCOM_SCM_HDCP_INVOKE,
  1117. .arginfo = QCOM_SCM_ARGS(10),
  1118. .args = {
  1119. req[0].addr,
  1120. req[0].val,
  1121. req[1].addr,
  1122. req[1].val,
  1123. req[2].addr,
  1124. req[2].val,
  1125. req[3].addr,
  1126. req[3].val,
  1127. req[4].addr,
  1128. req[4].val
  1129. },
  1130. .owner = ARM_SMCCC_OWNER_SIP,
  1131. };
  1132. struct qcom_scm_res res;
  1133. if (req_cnt > QCOM_SCM_HDCP_MAX_REQ_CNT)
  1134. return -ERANGE;
  1135. ret = qcom_scm_clk_enable();
  1136. if (ret)
  1137. return ret;
  1138. ret = qcom_scm_call(__scm->dev, &desc, &res);
  1139. *resp = res.result[0];
  1140. qcom_scm_clk_disable();
  1141. return ret;
  1142. }
  1143. EXPORT_SYMBOL_GPL(qcom_scm_hdcp_req);
  1144. int qcom_scm_iommu_set_pt_format(u32 sec_id, u32 ctx_num, u32 pt_fmt)
  1145. {
  1146. struct qcom_scm_desc desc = {
  1147. .svc = QCOM_SCM_SVC_SMMU_PROGRAM,
  1148. .cmd = QCOM_SCM_SMMU_PT_FORMAT,
  1149. .arginfo = QCOM_SCM_ARGS(3),
  1150. .args[0] = sec_id,
  1151. .args[1] = ctx_num,
  1152. .args[2] = pt_fmt, /* 0: LPAE AArch32 - 1: AArch64 */
  1153. .owner = ARM_SMCCC_OWNER_SIP,
  1154. };
  1155. return qcom_scm_call(__scm->dev, &desc, NULL);
  1156. }
  1157. EXPORT_SYMBOL_GPL(qcom_scm_iommu_set_pt_format);
  1158. int qcom_scm_qsmmu500_wait_safe_toggle(bool en)
  1159. {
  1160. struct qcom_scm_desc desc = {
  1161. .svc = QCOM_SCM_SVC_SMMU_PROGRAM,
  1162. .cmd = QCOM_SCM_SMMU_CONFIG_ERRATA1,
  1163. .arginfo = QCOM_SCM_ARGS(2),
  1164. .args[0] = QCOM_SCM_SMMU_CONFIG_ERRATA1_CLIENT_ALL,
  1165. .args[1] = en,
  1166. .owner = ARM_SMCCC_OWNER_SIP,
  1167. };
  1168. return qcom_scm_call_atomic(__scm->dev, &desc, NULL);
  1169. }
  1170. EXPORT_SYMBOL_GPL(qcom_scm_qsmmu500_wait_safe_toggle);
  1171. bool qcom_scm_lmh_dcvsh_available(void)
  1172. {
  1173. return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_LMH, QCOM_SCM_LMH_LIMIT_DCVSH);
  1174. }
  1175. EXPORT_SYMBOL_GPL(qcom_scm_lmh_dcvsh_available);
  1176. int qcom_scm_shm_bridge_enable(void)
  1177. {
  1178. int ret;
  1179. struct qcom_scm_desc desc = {
  1180. .svc = QCOM_SCM_SVC_MP,
  1181. .cmd = QCOM_SCM_MP_SHM_BRIDGE_ENABLE,
  1182. .owner = ARM_SMCCC_OWNER_SIP
  1183. };
  1184. struct qcom_scm_res res;
  1185. if (!__qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_MP,
  1186. QCOM_SCM_MP_SHM_BRIDGE_ENABLE))
  1187. return -EOPNOTSUPP;
  1188. ret = qcom_scm_call(__scm->dev, &desc, &res);
  1189. if (ret)
  1190. return ret;
  1191. if (res.result[0] == SHMBRIDGE_RESULT_NOTSUPP)
  1192. return -EOPNOTSUPP;
  1193. return res.result[0];
  1194. }
  1195. EXPORT_SYMBOL_GPL(qcom_scm_shm_bridge_enable);
  1196. int qcom_scm_shm_bridge_create(struct device *dev, u64 pfn_and_ns_perm_flags,
  1197. u64 ipfn_and_s_perm_flags, u64 size_and_flags,
  1198. u64 ns_vmids, u64 *handle)
  1199. {
  1200. struct qcom_scm_desc desc = {
  1201. .svc = QCOM_SCM_SVC_MP,
  1202. .cmd = QCOM_SCM_MP_SHM_BRIDGE_CREATE,
  1203. .owner = ARM_SMCCC_OWNER_SIP,
  1204. .args[0] = pfn_and_ns_perm_flags,
  1205. .args[1] = ipfn_and_s_perm_flags,
  1206. .args[2] = size_and_flags,
  1207. .args[3] = ns_vmids,
  1208. .arginfo = QCOM_SCM_ARGS(4, QCOM_SCM_VAL, QCOM_SCM_VAL,
  1209. QCOM_SCM_VAL, QCOM_SCM_VAL),
  1210. };
  1211. struct qcom_scm_res res;
  1212. int ret;
  1213. ret = qcom_scm_call(__scm->dev, &desc, &res);
  1214. if (handle && !ret)
  1215. *handle = res.result[1];
  1216. return ret ?: res.result[0];
  1217. }
  1218. EXPORT_SYMBOL_GPL(qcom_scm_shm_bridge_create);
  1219. int qcom_scm_shm_bridge_delete(struct device *dev, u64 handle)
  1220. {
  1221. struct qcom_scm_desc desc = {
  1222. .svc = QCOM_SCM_SVC_MP,
  1223. .cmd = QCOM_SCM_MP_SHM_BRIDGE_DELETE,
  1224. .owner = ARM_SMCCC_OWNER_SIP,
  1225. .args[0] = handle,
  1226. .arginfo = QCOM_SCM_ARGS(1, QCOM_SCM_VAL),
  1227. };
  1228. return qcom_scm_call(__scm->dev, &desc, NULL);
  1229. }
  1230. EXPORT_SYMBOL_GPL(qcom_scm_shm_bridge_delete);
  1231. int qcom_scm_lmh_profile_change(u32 profile_id)
  1232. {
  1233. struct qcom_scm_desc desc = {
  1234. .svc = QCOM_SCM_SVC_LMH,
  1235. .cmd = QCOM_SCM_LMH_LIMIT_PROFILE_CHANGE,
  1236. .arginfo = QCOM_SCM_ARGS(1, QCOM_SCM_VAL),
  1237. .args[0] = profile_id,
  1238. .owner = ARM_SMCCC_OWNER_SIP,
  1239. };
  1240. return qcom_scm_call(__scm->dev, &desc, NULL);
  1241. }
  1242. EXPORT_SYMBOL_GPL(qcom_scm_lmh_profile_change);
  1243. int qcom_scm_lmh_dcvsh(u32 payload_fn, u32 payload_reg, u32 payload_val,
  1244. u64 limit_node, u32 node_id, u64 version)
  1245. {
  1246. int ret, payload_size = 5 * sizeof(u32);
  1247. struct qcom_scm_desc desc = {
  1248. .svc = QCOM_SCM_SVC_LMH,
  1249. .cmd = QCOM_SCM_LMH_LIMIT_DCVSH,
  1250. .arginfo = QCOM_SCM_ARGS(5, QCOM_SCM_RO, QCOM_SCM_VAL, QCOM_SCM_VAL,
  1251. QCOM_SCM_VAL, QCOM_SCM_VAL),
  1252. .args[1] = payload_size,
  1253. .args[2] = limit_node,
  1254. .args[3] = node_id,
  1255. .args[4] = version,
  1256. .owner = ARM_SMCCC_OWNER_SIP,
  1257. };
  1258. u32 *payload_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
  1259. payload_size,
  1260. GFP_KERNEL);
  1261. if (!payload_buf)
  1262. return -ENOMEM;
  1263. payload_buf[0] = payload_fn;
  1264. payload_buf[1] = 0;
  1265. payload_buf[2] = payload_reg;
  1266. payload_buf[3] = 1;
  1267. payload_buf[4] = payload_val;
  1268. desc.args[0] = qcom_tzmem_to_phys(payload_buf);
  1269. ret = qcom_scm_call(__scm->dev, &desc, NULL);
  1270. return ret;
  1271. }
  1272. EXPORT_SYMBOL_GPL(qcom_scm_lmh_dcvsh);
  1273. int qcom_scm_gpu_init_regs(u32 gpu_req)
  1274. {
  1275. struct qcom_scm_desc desc = {
  1276. .svc = QCOM_SCM_SVC_GPU,
  1277. .cmd = QCOM_SCM_SVC_GPU_INIT_REGS,
  1278. .arginfo = QCOM_SCM_ARGS(1),
  1279. .args[0] = gpu_req,
  1280. .owner = ARM_SMCCC_OWNER_SIP,
  1281. };
  1282. return qcom_scm_call(__scm->dev, &desc, NULL);
  1283. }
  1284. EXPORT_SYMBOL_GPL(qcom_scm_gpu_init_regs);
  1285. static int qcom_scm_find_dload_address(struct device *dev, u64 *addr)
  1286. {
  1287. struct device_node *tcsr;
  1288. struct device_node *np = dev->of_node;
  1289. struct resource res;
  1290. u32 offset;
  1291. int ret;
  1292. tcsr = of_parse_phandle(np, "qcom,dload-mode", 0);
  1293. if (!tcsr)
  1294. return 0;
  1295. ret = of_address_to_resource(tcsr, 0, &res);
  1296. of_node_put(tcsr);
  1297. if (ret)
  1298. return ret;
  1299. ret = of_property_read_u32_index(np, "qcom,dload-mode", 1, &offset);
  1300. if (ret < 0)
  1301. return ret;
  1302. *addr = res.start + offset;
  1303. return 0;
  1304. }
  1305. #ifdef CONFIG_QCOM_QSEECOM
  1306. /* Lock for QSEECOM SCM call executions */
  1307. static DEFINE_MUTEX(qcom_scm_qseecom_call_lock);
  1308. static int __qcom_scm_qseecom_call(const struct qcom_scm_desc *desc,
  1309. struct qcom_scm_qseecom_resp *res)
  1310. {
  1311. struct qcom_scm_res scm_res = {};
  1312. int status;
  1313. /*
  1314. * QSEECOM SCM calls should not be executed concurrently. Therefore, we
  1315. * require the respective call lock to be held.
  1316. */
  1317. lockdep_assert_held(&qcom_scm_qseecom_call_lock);
  1318. status = qcom_scm_call(__scm->dev, desc, &scm_res);
  1319. res->result = scm_res.result[0];
  1320. res->resp_type = scm_res.result[1];
  1321. res->data = scm_res.result[2];
  1322. if (status)
  1323. return status;
  1324. return 0;
  1325. }
  1326. /**
  1327. * qcom_scm_qseecom_call() - Perform a QSEECOM SCM call.
  1328. * @desc: SCM call descriptor.
  1329. * @res: SCM call response (output).
  1330. *
  1331. * Performs the QSEECOM SCM call described by @desc, returning the response in
  1332. * @rsp.
  1333. *
  1334. * Return: Zero on success, nonzero on failure.
  1335. */
  1336. static int qcom_scm_qseecom_call(const struct qcom_scm_desc *desc,
  1337. struct qcom_scm_qseecom_resp *res)
  1338. {
  1339. int status;
  1340. /*
  1341. * Note: Multiple QSEECOM SCM calls should not be executed same time,
  1342. * so lock things here. This needs to be extended to callback/listener
  1343. * handling when support for that is implemented.
  1344. */
  1345. mutex_lock(&qcom_scm_qseecom_call_lock);
  1346. status = __qcom_scm_qseecom_call(desc, res);
  1347. mutex_unlock(&qcom_scm_qseecom_call_lock);
  1348. dev_dbg(__scm->dev, "%s: owner=%x, svc=%x, cmd=%x, result=%lld, type=%llx, data=%llx\n",
  1349. __func__, desc->owner, desc->svc, desc->cmd, res->result,
  1350. res->resp_type, res->data);
  1351. if (status) {
  1352. dev_err(__scm->dev, "qseecom: scm call failed with error %d\n", status);
  1353. return status;
  1354. }
  1355. /*
  1356. * TODO: Handle incomplete and blocked calls:
  1357. *
  1358. * Incomplete and blocked calls are not supported yet. Some devices
  1359. * and/or commands require those, some don't. Let's warn about them
  1360. * prominently in case someone attempts to try these commands with a
  1361. * device/command combination that isn't supported yet.
  1362. */
  1363. WARN_ON(res->result == QSEECOM_RESULT_INCOMPLETE);
  1364. WARN_ON(res->result == QSEECOM_RESULT_BLOCKED_ON_LISTENER);
  1365. return 0;
  1366. }
  1367. /**
  1368. * qcom_scm_qseecom_get_version() - Query the QSEECOM version.
  1369. * @version: Pointer where the QSEECOM version will be stored.
  1370. *
  1371. * Performs the QSEECOM SCM querying the QSEECOM version currently running in
  1372. * the TrustZone.
  1373. *
  1374. * Return: Zero on success, nonzero on failure.
  1375. */
  1376. static int qcom_scm_qseecom_get_version(u32 *version)
  1377. {
  1378. struct qcom_scm_desc desc = {};
  1379. struct qcom_scm_qseecom_resp res = {};
  1380. u32 feature = 10;
  1381. int ret;
  1382. desc.owner = QSEECOM_TZ_OWNER_SIP;
  1383. desc.svc = QSEECOM_TZ_SVC_INFO;
  1384. desc.cmd = QSEECOM_TZ_CMD_INFO_VERSION;
  1385. desc.arginfo = QCOM_SCM_ARGS(1, QCOM_SCM_VAL);
  1386. desc.args[0] = feature;
  1387. ret = qcom_scm_qseecom_call(&desc, &res);
  1388. if (ret)
  1389. return ret;
  1390. *version = res.result;
  1391. return 0;
  1392. }
  1393. /**
  1394. * qcom_scm_qseecom_app_get_id() - Query the app ID for a given QSEE app name.
  1395. * @app_name: The name of the app.
  1396. * @app_id: The returned app ID.
  1397. *
  1398. * Query and return the application ID of the SEE app identified by the given
  1399. * name. This returned ID is the unique identifier of the app required for
  1400. * subsequent communication.
  1401. *
  1402. * Return: Zero on success, nonzero on failure, -ENOENT if the app has not been
  1403. * loaded or could not be found.
  1404. */
  1405. int qcom_scm_qseecom_app_get_id(const char *app_name, u32 *app_id)
  1406. {
  1407. unsigned long name_buf_size = QSEECOM_MAX_APP_NAME_SIZE;
  1408. unsigned long app_name_len = strlen(app_name);
  1409. struct qcom_scm_desc desc = {};
  1410. struct qcom_scm_qseecom_resp res = {};
  1411. int status;
  1412. if (app_name_len >= name_buf_size)
  1413. return -EINVAL;
  1414. char *name_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
  1415. name_buf_size,
  1416. GFP_KERNEL);
  1417. if (!name_buf)
  1418. return -ENOMEM;
  1419. memcpy(name_buf, app_name, app_name_len);
  1420. desc.owner = QSEECOM_TZ_OWNER_QSEE_OS;
  1421. desc.svc = QSEECOM_TZ_SVC_APP_MGR;
  1422. desc.cmd = QSEECOM_TZ_CMD_APP_LOOKUP;
  1423. desc.arginfo = QCOM_SCM_ARGS(2, QCOM_SCM_RW, QCOM_SCM_VAL);
  1424. desc.args[0] = qcom_tzmem_to_phys(name_buf);
  1425. desc.args[1] = app_name_len;
  1426. status = qcom_scm_qseecom_call(&desc, &res);
  1427. if (status)
  1428. return status;
  1429. if (res.result == QSEECOM_RESULT_FAILURE)
  1430. return -ENOENT;
  1431. if (res.result != QSEECOM_RESULT_SUCCESS)
  1432. return -EINVAL;
  1433. if (res.resp_type != QSEECOM_SCM_RES_APP_ID)
  1434. return -EINVAL;
  1435. *app_id = res.data;
  1436. return 0;
  1437. }
  1438. EXPORT_SYMBOL_GPL(qcom_scm_qseecom_app_get_id);
  1439. /**
  1440. * qcom_scm_qseecom_app_send() - Send to and receive data from a given QSEE app.
  1441. * @app_id: The ID of the target app.
  1442. * @req: Request buffer sent to the app (must be TZ memory)
  1443. * @req_size: Size of the request buffer.
  1444. * @rsp: Response buffer, written to by the app (must be TZ memory)
  1445. * @rsp_size: Size of the response buffer.
  1446. *
  1447. * Sends a request to the QSEE app associated with the given ID and read back
  1448. * its response. The caller must provide two DMA memory regions, one for the
  1449. * request and one for the response, and fill out the @req region with the
  1450. * respective (app-specific) request data. The QSEE app reads this and returns
  1451. * its response in the @rsp region.
  1452. *
  1453. * Return: Zero on success, nonzero on failure.
  1454. */
  1455. int qcom_scm_qseecom_app_send(u32 app_id, void *req, size_t req_size,
  1456. void *rsp, size_t rsp_size)
  1457. {
  1458. struct qcom_scm_qseecom_resp res = {};
  1459. struct qcom_scm_desc desc = {};
  1460. phys_addr_t req_phys;
  1461. phys_addr_t rsp_phys;
  1462. int status;
  1463. req_phys = qcom_tzmem_to_phys(req);
  1464. rsp_phys = qcom_tzmem_to_phys(rsp);
  1465. desc.owner = QSEECOM_TZ_OWNER_TZ_APPS;
  1466. desc.svc = QSEECOM_TZ_SVC_APP_ID_PLACEHOLDER;
  1467. desc.cmd = QSEECOM_TZ_CMD_APP_SEND;
  1468. desc.arginfo = QCOM_SCM_ARGS(5, QCOM_SCM_VAL,
  1469. QCOM_SCM_RW, QCOM_SCM_VAL,
  1470. QCOM_SCM_RW, QCOM_SCM_VAL);
  1471. desc.args[0] = app_id;
  1472. desc.args[1] = req_phys;
  1473. desc.args[2] = req_size;
  1474. desc.args[3] = rsp_phys;
  1475. desc.args[4] = rsp_size;
  1476. status = qcom_scm_qseecom_call(&desc, &res);
  1477. if (status)
  1478. return status;
  1479. if (res.result != QSEECOM_RESULT_SUCCESS)
  1480. return -EIO;
  1481. return 0;
  1482. }
  1483. EXPORT_SYMBOL_GPL(qcom_scm_qseecom_app_send);
  1484. /*
  1485. * We do not yet support re-entrant calls via the qseecom interface. To prevent
  1486. + any potential issues with this, only allow validated machines for now.
  1487. */
  1488. static const struct of_device_id qcom_scm_qseecom_allowlist[] __maybe_unused = {
  1489. { .compatible = "dell,xps13-9345" },
  1490. { .compatible = "lenovo,flex-5g" },
  1491. { .compatible = "lenovo,thinkpad-t14s" },
  1492. { .compatible = "lenovo,thinkpad-x13s", },
  1493. { .compatible = "lenovo,yoga-slim7x" },
  1494. { .compatible = "microsoft,romulus13", },
  1495. { .compatible = "microsoft,romulus15", },
  1496. { .compatible = "qcom,sc8180x-primus" },
  1497. { .compatible = "qcom,x1e80100-crd" },
  1498. { .compatible = "qcom,x1e80100-qcp" },
  1499. { }
  1500. };
  1501. static bool qcom_scm_qseecom_machine_is_allowed(void)
  1502. {
  1503. struct device_node *np;
  1504. bool match;
  1505. np = of_find_node_by_path("/");
  1506. if (!np)
  1507. return false;
  1508. match = of_match_node(qcom_scm_qseecom_allowlist, np);
  1509. of_node_put(np);
  1510. return match;
  1511. }
  1512. static void qcom_scm_qseecom_free(void *data)
  1513. {
  1514. struct platform_device *qseecom_dev = data;
  1515. platform_device_del(qseecom_dev);
  1516. platform_device_put(qseecom_dev);
  1517. }
  1518. static int qcom_scm_qseecom_init(struct qcom_scm *scm)
  1519. {
  1520. struct platform_device *qseecom_dev;
  1521. u32 version;
  1522. int ret;
  1523. /*
  1524. * Note: We do two steps of validation here: First, we try to query the
  1525. * QSEECOM version as a check to see if the interface exists on this
  1526. * device. Second, we check against known good devices due to current
  1527. * driver limitations (see comment in qcom_scm_qseecom_allowlist).
  1528. *
  1529. * Note that we deliberately do the machine check after the version
  1530. * check so that we can log potentially supported devices. This should
  1531. * be safe as downstream sources indicate that the version query is
  1532. * neither blocking nor reentrant.
  1533. */
  1534. ret = qcom_scm_qseecom_get_version(&version);
  1535. if (ret)
  1536. return 0;
  1537. dev_info(scm->dev, "qseecom: found qseecom with version 0x%x\n", version);
  1538. if (!qcom_scm_qseecom_machine_is_allowed()) {
  1539. dev_info(scm->dev, "qseecom: untested machine, skipping\n");
  1540. return 0;
  1541. }
  1542. /*
  1543. * Set up QSEECOM interface device. All application clients will be
  1544. * set up and managed by the corresponding driver for it.
  1545. */
  1546. qseecom_dev = platform_device_alloc("qcom_qseecom", -1);
  1547. if (!qseecom_dev)
  1548. return -ENOMEM;
  1549. qseecom_dev->dev.parent = scm->dev;
  1550. ret = platform_device_add(qseecom_dev);
  1551. if (ret) {
  1552. platform_device_put(qseecom_dev);
  1553. return ret;
  1554. }
  1555. return devm_add_action_or_reset(scm->dev, qcom_scm_qseecom_free, qseecom_dev);
  1556. }
  1557. #else /* CONFIG_QCOM_QSEECOM */
  1558. static int qcom_scm_qseecom_init(struct qcom_scm *scm)
  1559. {
  1560. return 0;
  1561. }
  1562. #endif /* CONFIG_QCOM_QSEECOM */
  1563. /**
  1564. * qcom_scm_is_available() - Checks if SCM is available
  1565. */
  1566. bool qcom_scm_is_available(void)
  1567. {
  1568. /* Paired with smp_store_release() in qcom_scm_probe */
  1569. return !!smp_load_acquire(&__scm);
  1570. }
  1571. EXPORT_SYMBOL_GPL(qcom_scm_is_available);
  1572. static int qcom_scm_assert_valid_wq_ctx(u32 wq_ctx)
  1573. {
  1574. /* FW currently only supports a single wq_ctx (zero).
  1575. * TODO: Update this logic to include dynamic allocation and lookup of
  1576. * completion structs when FW supports more wq_ctx values.
  1577. */
  1578. if (wq_ctx != 0) {
  1579. dev_err(__scm->dev, "Firmware unexpectedly passed non-zero wq_ctx\n");
  1580. return -EINVAL;
  1581. }
  1582. return 0;
  1583. }
  1584. int qcom_scm_wait_for_wq_completion(u32 wq_ctx)
  1585. {
  1586. int ret;
  1587. ret = qcom_scm_assert_valid_wq_ctx(wq_ctx);
  1588. if (ret)
  1589. return ret;
  1590. wait_for_completion(&__scm->waitq_comp);
  1591. return 0;
  1592. }
  1593. static int qcom_scm_waitq_wakeup(unsigned int wq_ctx)
  1594. {
  1595. int ret;
  1596. ret = qcom_scm_assert_valid_wq_ctx(wq_ctx);
  1597. if (ret)
  1598. return ret;
  1599. complete(&__scm->waitq_comp);
  1600. return 0;
  1601. }
  1602. static irqreturn_t qcom_scm_irq_handler(int irq, void *data)
  1603. {
  1604. int ret;
  1605. struct qcom_scm *scm = data;
  1606. u32 wq_ctx, flags, more_pending = 0;
  1607. do {
  1608. ret = scm_get_wq_ctx(&wq_ctx, &flags, &more_pending);
  1609. if (ret) {
  1610. dev_err(scm->dev, "GET_WQ_CTX SMC call failed: %d\n", ret);
  1611. goto out;
  1612. }
  1613. if (flags != QCOM_SMC_WAITQ_FLAG_WAKE_ONE) {
  1614. dev_err(scm->dev, "Invalid flags received for wq_ctx: %u\n", flags);
  1615. goto out;
  1616. }
  1617. ret = qcom_scm_waitq_wakeup(wq_ctx);
  1618. if (ret)
  1619. goto out;
  1620. } while (more_pending);
  1621. out:
  1622. return IRQ_HANDLED;
  1623. }
  1624. static int get_download_mode(char *buffer, const struct kernel_param *kp)
  1625. {
  1626. if (download_mode >= ARRAY_SIZE(download_mode_name))
  1627. return sysfs_emit(buffer, "unknown mode\n");
  1628. return sysfs_emit(buffer, "%s\n", download_mode_name[download_mode]);
  1629. }
  1630. static int set_download_mode(const char *val, const struct kernel_param *kp)
  1631. {
  1632. bool tmp;
  1633. int ret;
  1634. ret = sysfs_match_string(download_mode_name, val);
  1635. if (ret < 0) {
  1636. ret = kstrtobool(val, &tmp);
  1637. if (ret < 0) {
  1638. pr_err("qcom_scm: err: %d\n", ret);
  1639. return ret;
  1640. }
  1641. ret = tmp ? 1 : 0;
  1642. }
  1643. download_mode = ret;
  1644. if (__scm)
  1645. qcom_scm_set_download_mode(download_mode);
  1646. return 0;
  1647. }
  1648. static const struct kernel_param_ops download_mode_param_ops = {
  1649. .get = get_download_mode,
  1650. .set = set_download_mode,
  1651. };
  1652. module_param_cb(download_mode, &download_mode_param_ops, NULL, 0644);
  1653. MODULE_PARM_DESC(download_mode, "download mode: off/0/N for no dump mode, full/on/1/Y for full dump mode, mini for minidump mode and full,mini for both full and minidump mode together are acceptable values");
  1654. static int qcom_scm_probe(struct platform_device *pdev)
  1655. {
  1656. struct qcom_tzmem_pool_config pool_config;
  1657. struct qcom_scm *scm;
  1658. int irq, ret;
  1659. scm = devm_kzalloc(&pdev->dev, sizeof(*scm), GFP_KERNEL);
  1660. if (!scm)
  1661. return -ENOMEM;
  1662. scm->dev = &pdev->dev;
  1663. ret = qcom_scm_find_dload_address(&pdev->dev, &scm->dload_mode_addr);
  1664. if (ret < 0)
  1665. return ret;
  1666. init_completion(&scm->waitq_comp);
  1667. mutex_init(&scm->scm_bw_lock);
  1668. scm->path = devm_of_icc_get(&pdev->dev, NULL);
  1669. if (IS_ERR(scm->path))
  1670. return dev_err_probe(&pdev->dev, PTR_ERR(scm->path),
  1671. "failed to acquire interconnect path\n");
  1672. scm->core_clk = devm_clk_get_optional(&pdev->dev, "core");
  1673. if (IS_ERR(scm->core_clk))
  1674. return PTR_ERR(scm->core_clk);
  1675. scm->iface_clk = devm_clk_get_optional(&pdev->dev, "iface");
  1676. if (IS_ERR(scm->iface_clk))
  1677. return PTR_ERR(scm->iface_clk);
  1678. scm->bus_clk = devm_clk_get_optional(&pdev->dev, "bus");
  1679. if (IS_ERR(scm->bus_clk))
  1680. return PTR_ERR(scm->bus_clk);
  1681. scm->reset.ops = &qcom_scm_pas_reset_ops;
  1682. scm->reset.nr_resets = 1;
  1683. scm->reset.of_node = pdev->dev.of_node;
  1684. ret = devm_reset_controller_register(&pdev->dev, &scm->reset);
  1685. if (ret)
  1686. return ret;
  1687. /* vote for max clk rate for highest performance */
  1688. ret = clk_set_rate(scm->core_clk, INT_MAX);
  1689. if (ret)
  1690. return ret;
  1691. /* Paired with smp_load_acquire() in qcom_scm_is_available(). */
  1692. smp_store_release(&__scm, scm);
  1693. irq = platform_get_irq_optional(pdev, 0);
  1694. if (irq < 0) {
  1695. if (irq != -ENXIO) {
  1696. ret = irq;
  1697. goto err;
  1698. }
  1699. } else {
  1700. ret = devm_request_threaded_irq(__scm->dev, irq, NULL, qcom_scm_irq_handler,
  1701. IRQF_ONESHOT, "qcom-scm", __scm);
  1702. if (ret < 0) {
  1703. dev_err_probe(scm->dev, ret, "Failed to request qcom-scm irq\n");
  1704. goto err;
  1705. }
  1706. }
  1707. __get_convention();
  1708. /*
  1709. * If "download mode" is requested, from this point on warmboot
  1710. * will cause the boot stages to enter download mode, unless
  1711. * disabled below by a clean shutdown/reboot.
  1712. */
  1713. qcom_scm_set_download_mode(download_mode);
  1714. /*
  1715. * Disable SDI if indicated by DT that it is enabled by default.
  1716. */
  1717. if (of_property_read_bool(pdev->dev.of_node, "qcom,sdi-enabled") || !download_mode)
  1718. qcom_scm_disable_sdi();
  1719. ret = of_reserved_mem_device_init(__scm->dev);
  1720. if (ret && ret != -ENODEV) {
  1721. dev_err_probe(__scm->dev, ret,
  1722. "Failed to setup the reserved memory region for TZ mem\n");
  1723. goto err;
  1724. }
  1725. ret = qcom_tzmem_enable(__scm->dev);
  1726. if (ret) {
  1727. dev_err_probe(__scm->dev, ret,
  1728. "Failed to enable the TrustZone memory allocator\n");
  1729. goto err;
  1730. }
  1731. memset(&pool_config, 0, sizeof(pool_config));
  1732. pool_config.initial_size = 0;
  1733. pool_config.policy = QCOM_TZMEM_POLICY_ON_DEMAND;
  1734. pool_config.max_size = SZ_256K;
  1735. __scm->mempool = devm_qcom_tzmem_pool_new(__scm->dev, &pool_config);
  1736. if (IS_ERR(__scm->mempool)) {
  1737. ret = dev_err_probe(__scm->dev, PTR_ERR(__scm->mempool),
  1738. "Failed to create the SCM memory pool\n");
  1739. goto err;
  1740. }
  1741. /*
  1742. * Initialize the QSEECOM interface.
  1743. *
  1744. * Note: QSEECOM is fairly self-contained and this only adds the
  1745. * interface device (the driver of which does most of the heavy
  1746. * lifting). So any errors returned here should be either -ENOMEM or
  1747. * -EINVAL (with the latter only in case there's a bug in our code).
  1748. * This means that there is no need to bring down the whole SCM driver.
  1749. * Just log the error instead and let SCM live.
  1750. */
  1751. ret = qcom_scm_qseecom_init(scm);
  1752. WARN(ret < 0, "failed to initialize qseecom: %d\n", ret);
  1753. return 0;
  1754. err:
  1755. /* Paired with smp_load_acquire() in qcom_scm_is_available(). */
  1756. smp_store_release(&__scm, NULL);
  1757. return ret;
  1758. }
  1759. static void qcom_scm_shutdown(struct platform_device *pdev)
  1760. {
  1761. /* Clean shutdown, disable download mode to allow normal restart */
  1762. qcom_scm_set_download_mode(QCOM_DLOAD_NODUMP);
  1763. }
  1764. static const struct of_device_id qcom_scm_dt_match[] = {
  1765. { .compatible = "qcom,scm" },
  1766. /* Legacy entries kept for backwards compatibility */
  1767. { .compatible = "qcom,scm-apq8064" },
  1768. { .compatible = "qcom,scm-apq8084" },
  1769. { .compatible = "qcom,scm-ipq4019" },
  1770. { .compatible = "qcom,scm-msm8953" },
  1771. { .compatible = "qcom,scm-msm8974" },
  1772. { .compatible = "qcom,scm-msm8996" },
  1773. {}
  1774. };
  1775. MODULE_DEVICE_TABLE(of, qcom_scm_dt_match);
  1776. static struct platform_driver qcom_scm_driver = {
  1777. .driver = {
  1778. .name = "qcom_scm",
  1779. .of_match_table = qcom_scm_dt_match,
  1780. .suppress_bind_attrs = true,
  1781. },
  1782. .probe = qcom_scm_probe,
  1783. .shutdown = qcom_scm_shutdown,
  1784. };
  1785. static int __init qcom_scm_init(void)
  1786. {
  1787. return platform_driver_register(&qcom_scm_driver);
  1788. }
  1789. subsys_initcall(qcom_scm_init);
  1790. MODULE_DESCRIPTION("Qualcomm Technologies, Inc. SCM driver");
  1791. MODULE_LICENSE("GPL v2");