qcom_scm.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854
  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/remoteproc.h>
  30. #include <linux/sizes.h>
  31. #include <linux/types.h>
  32. #include <dt-bindings/interrupt-controller/arm-gic.h>
  33. #include "qcom_scm.h"
  34. #include "qcom_tzmem.h"
  35. static u32 download_mode;
  36. #define GIC_SPI_BASE 32
  37. #define GIC_MAX_SPI 1019 // SPIs in GICv3 spec range from 32..1019
  38. #define GIC_ESPI_BASE 4096
  39. #define GIC_MAX_ESPI 5119 // ESPIs in GICv3 spec range from 4096..5119
  40. struct qcom_scm {
  41. struct device *dev;
  42. struct clk *core_clk;
  43. struct clk *iface_clk;
  44. struct clk *bus_clk;
  45. struct icc_path *path;
  46. struct completion *waitq_comps;
  47. struct reset_controller_dev reset;
  48. /* control access to the interconnect path */
  49. struct mutex scm_bw_lock;
  50. int scm_vote_count;
  51. u64 dload_mode_addr;
  52. struct qcom_tzmem_pool *mempool;
  53. unsigned int wq_cnt;
  54. };
  55. struct qcom_scm_current_perm_info {
  56. __le32 vmid;
  57. __le32 perm;
  58. __le64 ctx;
  59. __le32 ctx_size;
  60. __le32 unused;
  61. };
  62. struct qcom_scm_mem_map_info {
  63. __le64 mem_addr;
  64. __le64 mem_size;
  65. };
  66. /**
  67. * struct qcom_scm_qseecom_resp - QSEECOM SCM call response.
  68. * @result: Result or status of the SCM call. See &enum qcom_scm_qseecom_result.
  69. * @resp_type: Type of the response. See &enum qcom_scm_qseecom_resp_type.
  70. * @data: Response data. The type of this data is given in @resp_type.
  71. */
  72. struct qcom_scm_qseecom_resp {
  73. u64 result;
  74. u64 resp_type;
  75. u64 data;
  76. };
  77. enum qcom_scm_qseecom_result {
  78. QSEECOM_RESULT_SUCCESS = 0,
  79. QSEECOM_RESULT_INCOMPLETE = 1,
  80. QSEECOM_RESULT_BLOCKED_ON_LISTENER = 2,
  81. QSEECOM_RESULT_FAILURE = 0xFFFFFFFF,
  82. };
  83. enum qcom_scm_qseecom_resp_type {
  84. QSEECOM_SCM_RES_APP_ID = 0xEE01,
  85. QSEECOM_SCM_RES_QSEOS_LISTENER_ID = 0xEE02,
  86. };
  87. enum qcom_scm_qseecom_tz_owner {
  88. QSEECOM_TZ_OWNER_SIP = 2,
  89. QSEECOM_TZ_OWNER_TZ_APPS = 48,
  90. QSEECOM_TZ_OWNER_QSEE_OS = 50
  91. };
  92. enum qcom_scm_qseecom_tz_svc {
  93. QSEECOM_TZ_SVC_APP_ID_PLACEHOLDER = 0,
  94. QSEECOM_TZ_SVC_APP_MGR = 1,
  95. QSEECOM_TZ_SVC_INFO = 6,
  96. };
  97. enum qcom_scm_qseecom_tz_cmd_app {
  98. QSEECOM_TZ_CMD_APP_SEND = 1,
  99. QSEECOM_TZ_CMD_APP_LOOKUP = 3,
  100. };
  101. enum qcom_scm_qseecom_tz_cmd_info {
  102. QSEECOM_TZ_CMD_INFO_VERSION = 3,
  103. };
  104. #define RSCTABLE_BUFFER_NOT_SUFFICIENT 20
  105. #define QSEECOM_MAX_APP_NAME_SIZE 64
  106. #define SHMBRIDGE_RESULT_NOTSUPP 4
  107. /* Each bit configures cold/warm boot address for one of the 4 CPUs */
  108. static const u8 qcom_scm_cpu_cold_bits[QCOM_SCM_BOOT_MAX_CPUS] = {
  109. 0, BIT(0), BIT(3), BIT(5)
  110. };
  111. static const u8 qcom_scm_cpu_warm_bits[QCOM_SCM_BOOT_MAX_CPUS] = {
  112. BIT(2), BIT(1), BIT(4), BIT(6)
  113. };
  114. #define QCOM_SMC_WAITQ_FLAG_WAKE_ONE BIT(0)
  115. #define QCOM_DLOAD_MASK GENMASK(5, 4)
  116. #define QCOM_DLOAD_NODUMP 0
  117. #define QCOM_DLOAD_FULLDUMP 1
  118. #define QCOM_DLOAD_MINIDUMP 2
  119. #define QCOM_DLOAD_BOTHDUMP 3
  120. #define QCOM_SCM_DEFAULT_WAITQ_COUNT 1
  121. static const char * const qcom_scm_convention_names[] = {
  122. [SMC_CONVENTION_UNKNOWN] = "unknown",
  123. [SMC_CONVENTION_ARM_32] = "smc arm 32",
  124. [SMC_CONVENTION_ARM_64] = "smc arm 64",
  125. [SMC_CONVENTION_LEGACY] = "smc legacy",
  126. };
  127. static const char * const download_mode_name[] = {
  128. [QCOM_DLOAD_NODUMP] = "off",
  129. [QCOM_DLOAD_FULLDUMP] = "full",
  130. [QCOM_DLOAD_MINIDUMP] = "mini",
  131. [QCOM_DLOAD_BOTHDUMP] = "full,mini",
  132. };
  133. static struct qcom_scm *__scm;
  134. static int qcom_scm_clk_enable(void)
  135. {
  136. int ret;
  137. ret = clk_prepare_enable(__scm->core_clk);
  138. if (ret)
  139. goto bail;
  140. ret = clk_prepare_enable(__scm->iface_clk);
  141. if (ret)
  142. goto disable_core;
  143. ret = clk_prepare_enable(__scm->bus_clk);
  144. if (ret)
  145. goto disable_iface;
  146. return 0;
  147. disable_iface:
  148. clk_disable_unprepare(__scm->iface_clk);
  149. disable_core:
  150. clk_disable_unprepare(__scm->core_clk);
  151. bail:
  152. return ret;
  153. }
  154. static void qcom_scm_clk_disable(void)
  155. {
  156. clk_disable_unprepare(__scm->core_clk);
  157. clk_disable_unprepare(__scm->iface_clk);
  158. clk_disable_unprepare(__scm->bus_clk);
  159. }
  160. static int qcom_scm_bw_enable(void)
  161. {
  162. int ret = 0;
  163. if (!__scm->path)
  164. return 0;
  165. mutex_lock(&__scm->scm_bw_lock);
  166. if (!__scm->scm_vote_count) {
  167. ret = icc_set_bw(__scm->path, 0, UINT_MAX);
  168. if (ret < 0) {
  169. dev_err(__scm->dev, "failed to set bandwidth request\n");
  170. goto err_bw;
  171. }
  172. }
  173. __scm->scm_vote_count++;
  174. err_bw:
  175. mutex_unlock(&__scm->scm_bw_lock);
  176. return ret;
  177. }
  178. static void qcom_scm_bw_disable(void)
  179. {
  180. if (!__scm->path)
  181. return;
  182. mutex_lock(&__scm->scm_bw_lock);
  183. if (__scm->scm_vote_count-- == 1)
  184. icc_set_bw(__scm->path, 0, 0);
  185. mutex_unlock(&__scm->scm_bw_lock);
  186. }
  187. enum qcom_scm_convention qcom_scm_convention = SMC_CONVENTION_UNKNOWN;
  188. static DEFINE_SPINLOCK(scm_query_lock);
  189. struct qcom_tzmem_pool *qcom_scm_get_tzmem_pool(void)
  190. {
  191. if (!qcom_scm_is_available())
  192. return NULL;
  193. return __scm->mempool;
  194. }
  195. static enum qcom_scm_convention __get_convention(void)
  196. {
  197. unsigned long flags;
  198. struct qcom_scm_desc desc = {
  199. .svc = QCOM_SCM_SVC_INFO,
  200. .cmd = QCOM_SCM_INFO_IS_CALL_AVAIL,
  201. .args[0] = SCM_SMC_FNID(QCOM_SCM_SVC_INFO,
  202. QCOM_SCM_INFO_IS_CALL_AVAIL) |
  203. (ARM_SMCCC_OWNER_SIP << ARM_SMCCC_OWNER_SHIFT),
  204. .arginfo = QCOM_SCM_ARGS(1),
  205. .owner = ARM_SMCCC_OWNER_SIP,
  206. };
  207. struct qcom_scm_res res;
  208. enum qcom_scm_convention probed_convention;
  209. int ret;
  210. bool forced = false;
  211. if (likely(qcom_scm_convention != SMC_CONVENTION_UNKNOWN))
  212. return qcom_scm_convention;
  213. /*
  214. * Per the "SMC calling convention specification", the 64-bit calling
  215. * convention can only be used when the client is 64-bit, otherwise
  216. * system will encounter the undefined behaviour.
  217. */
  218. #if IS_ENABLED(CONFIG_ARM64)
  219. /*
  220. * Device isn't required as there is only one argument - no device
  221. * needed to dma_map_single to secure world
  222. */
  223. probed_convention = SMC_CONVENTION_ARM_64;
  224. ret = __scm_smc_call(NULL, &desc, probed_convention, &res, true);
  225. if (!ret && res.result[0] == 1)
  226. goto found;
  227. /*
  228. * Some SC7180 firmwares didn't implement the
  229. * QCOM_SCM_INFO_IS_CALL_AVAIL call, so we fallback to forcing ARM_64
  230. * calling conventions on these firmwares. Luckily we don't make any
  231. * early calls into the firmware on these SoCs so the device pointer
  232. * will be valid here to check if the compatible matches.
  233. */
  234. if (of_device_is_compatible(__scm ? __scm->dev->of_node : NULL, "qcom,scm-sc7180")) {
  235. forced = true;
  236. goto found;
  237. }
  238. #endif
  239. probed_convention = SMC_CONVENTION_ARM_32;
  240. ret = __scm_smc_call(NULL, &desc, probed_convention, &res, true);
  241. if (!ret && res.result[0] == 1)
  242. goto found;
  243. probed_convention = SMC_CONVENTION_LEGACY;
  244. found:
  245. spin_lock_irqsave(&scm_query_lock, flags);
  246. if (probed_convention != qcom_scm_convention) {
  247. qcom_scm_convention = probed_convention;
  248. pr_info("qcom_scm: convention: %s%s\n",
  249. qcom_scm_convention_names[qcom_scm_convention],
  250. forced ? " (forced)" : "");
  251. }
  252. spin_unlock_irqrestore(&scm_query_lock, flags);
  253. return qcom_scm_convention;
  254. }
  255. /**
  256. * qcom_scm_call() - Invoke a syscall in the secure world
  257. * @dev: device
  258. * @desc: Descriptor structure containing arguments and return values
  259. * @res: Structure containing results from SMC/HVC call
  260. *
  261. * Sends a command to the SCM and waits for the command to finish processing.
  262. * This should *only* be called in pre-emptible context.
  263. */
  264. static int qcom_scm_call(struct device *dev, const struct qcom_scm_desc *desc,
  265. struct qcom_scm_res *res)
  266. {
  267. might_sleep();
  268. switch (__get_convention()) {
  269. case SMC_CONVENTION_ARM_32:
  270. case SMC_CONVENTION_ARM_64:
  271. return scm_smc_call(dev, desc, res, false);
  272. case SMC_CONVENTION_LEGACY:
  273. return scm_legacy_call(dev, desc, res);
  274. default:
  275. pr_err("Unknown current SCM calling convention.\n");
  276. return -EINVAL;
  277. }
  278. }
  279. /**
  280. * qcom_scm_call_atomic() - atomic variation of qcom_scm_call()
  281. * @dev: device
  282. * @desc: Descriptor structure containing arguments and return values
  283. * @res: Structure containing results from SMC/HVC call
  284. *
  285. * Sends a command to the SCM and waits for the command to finish processing.
  286. * This can be called in atomic context.
  287. */
  288. static int qcom_scm_call_atomic(struct device *dev,
  289. const struct qcom_scm_desc *desc,
  290. struct qcom_scm_res *res)
  291. {
  292. switch (__get_convention()) {
  293. case SMC_CONVENTION_ARM_32:
  294. case SMC_CONVENTION_ARM_64:
  295. return scm_smc_call(dev, desc, res, true);
  296. case SMC_CONVENTION_LEGACY:
  297. return scm_legacy_call_atomic(dev, desc, res);
  298. default:
  299. pr_err("Unknown current SCM calling convention.\n");
  300. return -EINVAL;
  301. }
  302. }
  303. static bool __qcom_scm_is_call_available(struct device *dev, u32 svc_id,
  304. u32 cmd_id)
  305. {
  306. int ret;
  307. struct qcom_scm_desc desc = {
  308. .svc = QCOM_SCM_SVC_INFO,
  309. .cmd = QCOM_SCM_INFO_IS_CALL_AVAIL,
  310. .owner = ARM_SMCCC_OWNER_SIP,
  311. };
  312. struct qcom_scm_res res;
  313. desc.arginfo = QCOM_SCM_ARGS(1);
  314. switch (__get_convention()) {
  315. case SMC_CONVENTION_ARM_32:
  316. case SMC_CONVENTION_ARM_64:
  317. desc.args[0] = SCM_SMC_FNID(svc_id, cmd_id) |
  318. (ARM_SMCCC_OWNER_SIP << ARM_SMCCC_OWNER_SHIFT);
  319. break;
  320. case SMC_CONVENTION_LEGACY:
  321. desc.args[0] = SCM_LEGACY_FNID(svc_id, cmd_id);
  322. break;
  323. default:
  324. pr_err("Unknown SMC convention being used\n");
  325. return false;
  326. }
  327. ret = qcom_scm_call(dev, &desc, &res);
  328. return ret ? false : !!res.result[0];
  329. }
  330. static int qcom_scm_set_boot_addr(void *entry, const u8 *cpu_bits)
  331. {
  332. int cpu;
  333. unsigned int flags = 0;
  334. struct qcom_scm_desc desc = {
  335. .svc = QCOM_SCM_SVC_BOOT,
  336. .cmd = QCOM_SCM_BOOT_SET_ADDR,
  337. .arginfo = QCOM_SCM_ARGS(2),
  338. .owner = ARM_SMCCC_OWNER_SIP,
  339. };
  340. for_each_present_cpu(cpu) {
  341. if (cpu >= QCOM_SCM_BOOT_MAX_CPUS)
  342. return -EINVAL;
  343. flags |= cpu_bits[cpu];
  344. }
  345. desc.args[0] = flags;
  346. desc.args[1] = virt_to_phys(entry);
  347. return qcom_scm_call_atomic(__scm ? __scm->dev : NULL, &desc, NULL);
  348. }
  349. static int qcom_scm_set_boot_addr_mc(void *entry, unsigned int flags)
  350. {
  351. struct qcom_scm_desc desc = {
  352. .svc = QCOM_SCM_SVC_BOOT,
  353. .cmd = QCOM_SCM_BOOT_SET_ADDR_MC,
  354. .owner = ARM_SMCCC_OWNER_SIP,
  355. .arginfo = QCOM_SCM_ARGS(6),
  356. .args = {
  357. virt_to_phys(entry),
  358. /* Apply to all CPUs in all affinity levels */
  359. ~0ULL, ~0ULL, ~0ULL, ~0ULL,
  360. flags,
  361. },
  362. };
  363. /* Need a device for DMA of the additional arguments */
  364. if (!__scm || __get_convention() == SMC_CONVENTION_LEGACY)
  365. return -EOPNOTSUPP;
  366. return qcom_scm_call(__scm->dev, &desc, NULL);
  367. }
  368. /**
  369. * qcom_scm_set_warm_boot_addr() - Set the warm boot address for all cpus
  370. * @entry: Entry point function for the cpus
  371. *
  372. * Set the Linux entry point for the SCM to transfer control to when coming
  373. * out of a power down. CPU power down may be executed on cpuidle or hotplug.
  374. */
  375. int qcom_scm_set_warm_boot_addr(void *entry)
  376. {
  377. if (qcom_scm_set_boot_addr_mc(entry, QCOM_SCM_BOOT_MC_FLAG_WARMBOOT))
  378. /* Fallback to old SCM call */
  379. return qcom_scm_set_boot_addr(entry, qcom_scm_cpu_warm_bits);
  380. return 0;
  381. }
  382. EXPORT_SYMBOL_GPL(qcom_scm_set_warm_boot_addr);
  383. /**
  384. * qcom_scm_set_cold_boot_addr() - Set the cold boot address for all cpus
  385. * @entry: Entry point function for the cpus
  386. */
  387. int qcom_scm_set_cold_boot_addr(void *entry)
  388. {
  389. if (qcom_scm_set_boot_addr_mc(entry, QCOM_SCM_BOOT_MC_FLAG_COLDBOOT))
  390. /* Fallback to old SCM call */
  391. return qcom_scm_set_boot_addr(entry, qcom_scm_cpu_cold_bits);
  392. return 0;
  393. }
  394. EXPORT_SYMBOL_GPL(qcom_scm_set_cold_boot_addr);
  395. /**
  396. * qcom_scm_cpu_power_down() - Power down the cpu
  397. * @flags: Flags to flush cache
  398. *
  399. * This is an end point to power down cpu. If there was a pending interrupt,
  400. * the control would return from this function, otherwise, the cpu jumps to the
  401. * warm boot entry point set for this cpu upon reset.
  402. */
  403. void qcom_scm_cpu_power_down(u32 flags)
  404. {
  405. struct qcom_scm_desc desc = {
  406. .svc = QCOM_SCM_SVC_BOOT,
  407. .cmd = QCOM_SCM_BOOT_TERMINATE_PC,
  408. .args[0] = flags & QCOM_SCM_FLUSH_FLAG_MASK,
  409. .arginfo = QCOM_SCM_ARGS(1),
  410. .owner = ARM_SMCCC_OWNER_SIP,
  411. };
  412. qcom_scm_call_atomic(__scm ? __scm->dev : NULL, &desc, NULL);
  413. }
  414. EXPORT_SYMBOL_GPL(qcom_scm_cpu_power_down);
  415. int qcom_scm_set_remote_state(u32 state, u32 id)
  416. {
  417. struct qcom_scm_desc desc = {
  418. .svc = QCOM_SCM_SVC_BOOT,
  419. .cmd = QCOM_SCM_BOOT_SET_REMOTE_STATE,
  420. .arginfo = QCOM_SCM_ARGS(2),
  421. .args[0] = state,
  422. .args[1] = id,
  423. .owner = ARM_SMCCC_OWNER_SIP,
  424. };
  425. struct qcom_scm_res res;
  426. int ret;
  427. ret = qcom_scm_call(__scm->dev, &desc, &res);
  428. return ret ? : res.result[0];
  429. }
  430. EXPORT_SYMBOL_GPL(qcom_scm_set_remote_state);
  431. static int qcom_scm_disable_sdi(void)
  432. {
  433. int ret;
  434. struct qcom_scm_desc desc = {
  435. .svc = QCOM_SCM_SVC_BOOT,
  436. .cmd = QCOM_SCM_BOOT_SDI_CONFIG,
  437. .args[0] = 1, /* Disable watchdog debug */
  438. .args[1] = 0, /* Disable SDI */
  439. .arginfo = QCOM_SCM_ARGS(2),
  440. .owner = ARM_SMCCC_OWNER_SIP,
  441. };
  442. struct qcom_scm_res res;
  443. ret = qcom_scm_clk_enable();
  444. if (ret)
  445. return ret;
  446. ret = qcom_scm_call(__scm->dev, &desc, &res);
  447. qcom_scm_clk_disable();
  448. return ret ? : res.result[0];
  449. }
  450. static int __qcom_scm_set_dload_mode(struct device *dev, bool enable)
  451. {
  452. struct qcom_scm_desc desc = {
  453. .svc = QCOM_SCM_SVC_BOOT,
  454. .cmd = QCOM_SCM_BOOT_SET_DLOAD_MODE,
  455. .arginfo = QCOM_SCM_ARGS(2),
  456. .args[0] = QCOM_SCM_BOOT_SET_DLOAD_MODE,
  457. .owner = ARM_SMCCC_OWNER_SIP,
  458. };
  459. desc.args[1] = enable ? QCOM_SCM_BOOT_SET_DLOAD_MODE : 0;
  460. return qcom_scm_call_atomic(__scm->dev, &desc, NULL);
  461. }
  462. static int qcom_scm_io_rmw(phys_addr_t addr, unsigned int mask, unsigned int val)
  463. {
  464. unsigned int old;
  465. unsigned int new;
  466. int ret;
  467. ret = qcom_scm_io_readl(addr, &old);
  468. if (ret)
  469. return ret;
  470. new = (old & ~mask) | (val & mask);
  471. return qcom_scm_io_writel(addr, new);
  472. }
  473. static void qcom_scm_set_download_mode(u32 dload_mode)
  474. {
  475. int ret = 0;
  476. if (__scm->dload_mode_addr) {
  477. ret = qcom_scm_io_rmw(__scm->dload_mode_addr, QCOM_DLOAD_MASK,
  478. FIELD_PREP(QCOM_DLOAD_MASK, dload_mode));
  479. } else if (__qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_BOOT,
  480. QCOM_SCM_BOOT_SET_DLOAD_MODE)) {
  481. ret = __qcom_scm_set_dload_mode(__scm->dev, !!dload_mode);
  482. } else if (dload_mode) {
  483. dev_err(__scm->dev,
  484. "No available mechanism for setting download mode\n");
  485. }
  486. if (ret)
  487. dev_err(__scm->dev, "failed to set download mode: %d\n", ret);
  488. }
  489. /**
  490. * devm_qcom_scm_pas_context_alloc() - Allocate peripheral authentication service
  491. * context for a given peripheral
  492. *
  493. * PAS context is device-resource managed, so the caller does not need
  494. * to worry about freeing the context memory.
  495. *
  496. * @dev: PAS firmware device
  497. * @pas_id: peripheral authentication service id
  498. * @mem_phys: Subsystem reserve memory start address
  499. * @mem_size: Subsystem reserve memory size
  500. *
  501. * Returns: The new PAS context, or ERR_PTR() on failure.
  502. */
  503. struct qcom_scm_pas_context *devm_qcom_scm_pas_context_alloc(struct device *dev,
  504. u32 pas_id,
  505. phys_addr_t mem_phys,
  506. size_t mem_size)
  507. {
  508. struct qcom_scm_pas_context *ctx;
  509. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  510. if (!ctx)
  511. return ERR_PTR(-ENOMEM);
  512. ctx->dev = dev;
  513. ctx->pas_id = pas_id;
  514. ctx->mem_phys = mem_phys;
  515. ctx->mem_size = mem_size;
  516. return ctx;
  517. }
  518. EXPORT_SYMBOL_GPL(devm_qcom_scm_pas_context_alloc);
  519. static int __qcom_scm_pas_init_image(u32 pas_id, dma_addr_t mdata_phys,
  520. struct qcom_scm_res *res)
  521. {
  522. struct qcom_scm_desc desc = {
  523. .svc = QCOM_SCM_SVC_PIL,
  524. .cmd = QCOM_SCM_PIL_PAS_INIT_IMAGE,
  525. .arginfo = QCOM_SCM_ARGS(2, QCOM_SCM_VAL, QCOM_SCM_RW),
  526. .args[0] = pas_id,
  527. .owner = ARM_SMCCC_OWNER_SIP,
  528. };
  529. int ret;
  530. ret = qcom_scm_clk_enable();
  531. if (ret)
  532. return ret;
  533. ret = qcom_scm_bw_enable();
  534. if (ret)
  535. goto disable_clk;
  536. desc.args[1] = mdata_phys;
  537. ret = qcom_scm_call(__scm->dev, &desc, res);
  538. qcom_scm_bw_disable();
  539. disable_clk:
  540. qcom_scm_clk_disable();
  541. return ret;
  542. }
  543. static int qcom_scm_pas_prep_and_init_image(struct qcom_scm_pas_context *ctx,
  544. const void *metadata, size_t size)
  545. {
  546. struct qcom_scm_res res;
  547. phys_addr_t mdata_phys;
  548. void *mdata_buf;
  549. int ret;
  550. mdata_buf = qcom_tzmem_alloc(__scm->mempool, size, GFP_KERNEL);
  551. if (!mdata_buf)
  552. return -ENOMEM;
  553. memcpy(mdata_buf, metadata, size);
  554. mdata_phys = qcom_tzmem_to_phys(mdata_buf);
  555. ret = __qcom_scm_pas_init_image(ctx->pas_id, mdata_phys, &res);
  556. if (ret < 0)
  557. qcom_tzmem_free(mdata_buf);
  558. else
  559. ctx->ptr = mdata_buf;
  560. return ret ? : res.result[0];
  561. }
  562. /**
  563. * qcom_scm_pas_init_image() - Initialize peripheral authentication service
  564. * state machine for a given peripheral, using the
  565. * metadata
  566. * @pas_id: peripheral authentication service id
  567. * @metadata: pointer to memory containing ELF header, program header table
  568. * and optional blob of data used for authenticating the metadata
  569. * and the rest of the firmware
  570. * @size: size of the metadata
  571. * @ctx: optional pas context
  572. *
  573. * Return: 0 on success.
  574. *
  575. * Upon successful return, the PAS metadata context (@ctx) will be used to
  576. * track the metadata allocation, this needs to be released by invoking
  577. * qcom_scm_pas_metadata_release() by the caller.
  578. */
  579. int qcom_scm_pas_init_image(u32 pas_id, const void *metadata, size_t size,
  580. struct qcom_scm_pas_context *ctx)
  581. {
  582. struct qcom_scm_res res;
  583. dma_addr_t mdata_phys;
  584. void *mdata_buf;
  585. int ret;
  586. if (ctx && ctx->use_tzmem)
  587. return qcom_scm_pas_prep_and_init_image(ctx, metadata, size);
  588. /*
  589. * During the scm call memory protection will be enabled for the meta
  590. * data blob, so make sure it's physically contiguous, 4K aligned and
  591. * non-cachable to avoid XPU violations.
  592. *
  593. * For PIL calls the hypervisor creates SHM Bridges for the blob
  594. * buffers on behalf of Linux so we must not do it ourselves hence
  595. * not using the TZMem allocator here.
  596. *
  597. * If we pass a buffer that is already part of an SHM Bridge to this
  598. * call, it will fail.
  599. */
  600. mdata_buf = dma_alloc_coherent(__scm->dev, size, &mdata_phys,
  601. GFP_KERNEL);
  602. if (!mdata_buf)
  603. return -ENOMEM;
  604. memcpy(mdata_buf, metadata, size);
  605. ret = __qcom_scm_pas_init_image(pas_id, mdata_phys, &res);
  606. if (ret < 0 || !ctx) {
  607. dma_free_coherent(__scm->dev, size, mdata_buf, mdata_phys);
  608. } else if (ctx) {
  609. ctx->ptr = mdata_buf;
  610. ctx->phys = mdata_phys;
  611. ctx->size = size;
  612. }
  613. return ret ? : res.result[0];
  614. }
  615. EXPORT_SYMBOL_GPL(qcom_scm_pas_init_image);
  616. /**
  617. * qcom_scm_pas_metadata_release() - release metadata context
  618. * @ctx: pas context
  619. */
  620. void qcom_scm_pas_metadata_release(struct qcom_scm_pas_context *ctx)
  621. {
  622. if (!ctx->ptr)
  623. return;
  624. if (ctx->use_tzmem)
  625. qcom_tzmem_free(ctx->ptr);
  626. else
  627. dma_free_coherent(__scm->dev, ctx->size, ctx->ptr, ctx->phys);
  628. ctx->ptr = NULL;
  629. }
  630. EXPORT_SYMBOL_GPL(qcom_scm_pas_metadata_release);
  631. /**
  632. * qcom_scm_pas_mem_setup() - Prepare the memory related to a given peripheral
  633. * for firmware loading
  634. * @pas_id: peripheral authentication service id
  635. * @addr: start address of memory area to prepare
  636. * @size: size of the memory area to prepare
  637. *
  638. * Returns 0 on success.
  639. */
  640. int qcom_scm_pas_mem_setup(u32 pas_id, phys_addr_t addr, phys_addr_t size)
  641. {
  642. int ret;
  643. struct qcom_scm_desc desc = {
  644. .svc = QCOM_SCM_SVC_PIL,
  645. .cmd = QCOM_SCM_PIL_PAS_MEM_SETUP,
  646. .arginfo = QCOM_SCM_ARGS(3),
  647. .args[0] = pas_id,
  648. .args[1] = addr,
  649. .args[2] = size,
  650. .owner = ARM_SMCCC_OWNER_SIP,
  651. };
  652. struct qcom_scm_res res;
  653. ret = qcom_scm_clk_enable();
  654. if (ret)
  655. return ret;
  656. ret = qcom_scm_bw_enable();
  657. if (ret)
  658. goto disable_clk;
  659. ret = qcom_scm_call(__scm->dev, &desc, &res);
  660. qcom_scm_bw_disable();
  661. disable_clk:
  662. qcom_scm_clk_disable();
  663. return ret ? : res.result[0];
  664. }
  665. EXPORT_SYMBOL_GPL(qcom_scm_pas_mem_setup);
  666. static void *__qcom_scm_pas_get_rsc_table(u32 pas_id, void *input_rt_tzm,
  667. size_t input_rt_size,
  668. size_t *output_rt_size)
  669. {
  670. struct qcom_scm_desc desc = {
  671. .svc = QCOM_SCM_SVC_PIL,
  672. .cmd = QCOM_SCM_PIL_PAS_GET_RSCTABLE,
  673. .arginfo = QCOM_SCM_ARGS(5, QCOM_SCM_VAL, QCOM_SCM_RO, QCOM_SCM_VAL,
  674. QCOM_SCM_RW, QCOM_SCM_VAL),
  675. .args[0] = pas_id,
  676. .owner = ARM_SMCCC_OWNER_SIP,
  677. };
  678. struct qcom_scm_res res;
  679. void *output_rt_tzm;
  680. int ret;
  681. output_rt_tzm = qcom_tzmem_alloc(__scm->mempool, *output_rt_size, GFP_KERNEL);
  682. if (!output_rt_tzm)
  683. return ERR_PTR(-ENOMEM);
  684. desc.args[1] = qcom_tzmem_to_phys(input_rt_tzm);
  685. desc.args[2] = input_rt_size;
  686. desc.args[3] = qcom_tzmem_to_phys(output_rt_tzm);
  687. desc.args[4] = *output_rt_size;
  688. /*
  689. * Whether SMC fail or pass, res.result[2] will hold actual resource table
  690. * size.
  691. *
  692. * If passed 'output_rt_size' buffer size is not sufficient to hold the
  693. * resource table TrustZone sends, response code in res.result[1] as
  694. * RSCTABLE_BUFFER_NOT_SUFFICIENT so that caller can retry this SMC call
  695. * with output_rt_tzm buffer with res.result[2] size however, It should not
  696. * be of unresonable size.
  697. */
  698. ret = qcom_scm_call(__scm->dev, &desc, &res);
  699. if (!ret && res.result[2] > SZ_1G) {
  700. ret = -E2BIG;
  701. goto free_output_rt;
  702. }
  703. *output_rt_size = res.result[2];
  704. if (ret && res.result[1] == RSCTABLE_BUFFER_NOT_SUFFICIENT)
  705. ret = -EOVERFLOW;
  706. free_output_rt:
  707. if (ret)
  708. qcom_tzmem_free(output_rt_tzm);
  709. return ret ? ERR_PTR(ret) : output_rt_tzm;
  710. }
  711. /**
  712. * qcom_scm_pas_get_rsc_table() - Retrieve the resource table in passed output buffer
  713. * for a given peripheral.
  714. *
  715. * Qualcomm remote processor may rely on both static and dynamic resources for
  716. * its functionality. Static resources typically refer to memory-mapped addresses
  717. * required by the subsystem and are often embedded within the firmware binary
  718. * and dynamic resources, such as shared memory in DDR etc., are determined at
  719. * runtime during the boot process.
  720. *
  721. * On Qualcomm Technologies devices, it's possible that static resources are not
  722. * embedded in the firmware binary and instead are provided by TrustZone However,
  723. * dynamic resources are always expected to come from TrustZone. This indicates
  724. * that for Qualcomm devices, all resources (static and dynamic) will be provided
  725. * by TrustZone via the SMC call.
  726. *
  727. * If the remote processor firmware binary does contain static resources, they
  728. * should be passed in input_rt. These will be forwarded to TrustZone for
  729. * authentication. TrustZone will then append the dynamic resources and return
  730. * the complete resource table in output_rt_tzm.
  731. *
  732. * If the remote processor firmware binary does not include a resource table,
  733. * the caller of this function should set input_rt as NULL and input_rt_size
  734. * as zero respectively.
  735. *
  736. * More about documentation on resource table data structures can be found in
  737. * include/linux/remoteproc.h
  738. *
  739. * @ctx: PAS context
  740. * @pas_id: peripheral authentication service id
  741. * @input_rt: resource table buffer which is present in firmware binary
  742. * @input_rt_size: size of the resource table present in firmware binary
  743. * @output_rt_size: TrustZone expects caller should pass worst case size for
  744. * the output_rt_tzm.
  745. *
  746. * Return:
  747. * On success, returns a pointer to the allocated buffer containing the final
  748. * resource table and output_rt_size will have actual resource table size from
  749. * TrustZone. The caller is responsible for freeing the buffer. On failure,
  750. * returns ERR_PTR(-errno).
  751. */
  752. struct resource_table *qcom_scm_pas_get_rsc_table(struct qcom_scm_pas_context *ctx,
  753. void *input_rt,
  754. size_t input_rt_size,
  755. size_t *output_rt_size)
  756. {
  757. struct resource_table empty_rsc = {};
  758. size_t size = SZ_16K;
  759. void *output_rt_tzm;
  760. void *input_rt_tzm;
  761. void *tbl_ptr;
  762. int ret;
  763. ret = qcom_scm_clk_enable();
  764. if (ret)
  765. return ERR_PTR(ret);
  766. ret = qcom_scm_bw_enable();
  767. if (ret)
  768. goto disable_clk;
  769. /*
  770. * TrustZone can not accept buffer as NULL value as argument hence,
  771. * we need to pass a input buffer indicating that subsystem firmware
  772. * does not have resource table by filling resource table structure.
  773. */
  774. if (!input_rt) {
  775. input_rt = &empty_rsc;
  776. input_rt_size = sizeof(empty_rsc);
  777. }
  778. input_rt_tzm = qcom_tzmem_alloc(__scm->mempool, input_rt_size, GFP_KERNEL);
  779. if (!input_rt_tzm) {
  780. ret = -ENOMEM;
  781. goto disable_scm_bw;
  782. }
  783. memcpy(input_rt_tzm, input_rt, input_rt_size);
  784. output_rt_tzm = __qcom_scm_pas_get_rsc_table(ctx->pas_id, input_rt_tzm,
  785. input_rt_size, &size);
  786. if (PTR_ERR(output_rt_tzm) == -EOVERFLOW)
  787. /* Try again with the size requested by the TZ */
  788. output_rt_tzm = __qcom_scm_pas_get_rsc_table(ctx->pas_id,
  789. input_rt_tzm,
  790. input_rt_size,
  791. &size);
  792. if (IS_ERR(output_rt_tzm)) {
  793. ret = PTR_ERR(output_rt_tzm);
  794. goto free_input_rt;
  795. }
  796. tbl_ptr = kzalloc(size, GFP_KERNEL);
  797. if (!tbl_ptr) {
  798. qcom_tzmem_free(output_rt_tzm);
  799. ret = -ENOMEM;
  800. goto free_input_rt;
  801. }
  802. memcpy(tbl_ptr, output_rt_tzm, size);
  803. *output_rt_size = size;
  804. qcom_tzmem_free(output_rt_tzm);
  805. free_input_rt:
  806. qcom_tzmem_free(input_rt_tzm);
  807. disable_scm_bw:
  808. qcom_scm_bw_disable();
  809. disable_clk:
  810. qcom_scm_clk_disable();
  811. return ret ? ERR_PTR(ret) : tbl_ptr;
  812. }
  813. EXPORT_SYMBOL_GPL(qcom_scm_pas_get_rsc_table);
  814. /**
  815. * qcom_scm_pas_auth_and_reset() - Authenticate the given peripheral firmware
  816. * and reset the remote processor
  817. * @pas_id: peripheral authentication service id
  818. *
  819. * Return 0 on success.
  820. */
  821. int qcom_scm_pas_auth_and_reset(u32 pas_id)
  822. {
  823. int ret;
  824. struct qcom_scm_desc desc = {
  825. .svc = QCOM_SCM_SVC_PIL,
  826. .cmd = QCOM_SCM_PIL_PAS_AUTH_AND_RESET,
  827. .arginfo = QCOM_SCM_ARGS(1),
  828. .args[0] = pas_id,
  829. .owner = ARM_SMCCC_OWNER_SIP,
  830. };
  831. struct qcom_scm_res res;
  832. ret = qcom_scm_clk_enable();
  833. if (ret)
  834. return ret;
  835. ret = qcom_scm_bw_enable();
  836. if (ret)
  837. goto disable_clk;
  838. ret = qcom_scm_call(__scm->dev, &desc, &res);
  839. qcom_scm_bw_disable();
  840. disable_clk:
  841. qcom_scm_clk_disable();
  842. return ret ? : res.result[0];
  843. }
  844. EXPORT_SYMBOL_GPL(qcom_scm_pas_auth_and_reset);
  845. /**
  846. * qcom_scm_pas_prepare_and_auth_reset() - Prepare, authenticate, and reset the
  847. * remote processor
  848. *
  849. * @ctx: Context saved during call to qcom_scm_pas_context_init()
  850. *
  851. * This function performs the necessary steps to prepare a PAS subsystem,
  852. * authenticate it using the provided metadata, and initiate a reset sequence.
  853. *
  854. * It should be used when Linux is in control setting up the IOMMU hardware
  855. * for remote subsystem during secure firmware loading processes. The preparation
  856. * step sets up a shmbridge over the firmware memory before TrustZone accesses the
  857. * firmware memory region for authentication. The authentication step verifies
  858. * the integrity and authenticity of the firmware or configuration using secure
  859. * metadata. Finally, the reset step ensures the subsystem starts in a clean and
  860. * sane state.
  861. *
  862. * Return: 0 on success, negative errno on failure.
  863. */
  864. int qcom_scm_pas_prepare_and_auth_reset(struct qcom_scm_pas_context *ctx)
  865. {
  866. u64 handle;
  867. int ret;
  868. /*
  869. * When Linux running @ EL1, Gunyah hypervisor running @ EL2 traps the
  870. * auth_and_reset call and create an shmbridge on the remote subsystem
  871. * memory region and then invokes a call to TrustZone to authenticate.
  872. */
  873. if (!ctx->use_tzmem)
  874. return qcom_scm_pas_auth_and_reset(ctx->pas_id);
  875. /*
  876. * When Linux runs @ EL2 Linux must create the shmbridge itself and then
  877. * subsequently call TrustZone for authenticate and reset.
  878. */
  879. ret = qcom_tzmem_shm_bridge_create(ctx->mem_phys, ctx->mem_size, &handle);
  880. if (ret)
  881. return ret;
  882. ret = qcom_scm_pas_auth_and_reset(ctx->pas_id);
  883. qcom_tzmem_shm_bridge_delete(handle);
  884. return ret;
  885. }
  886. EXPORT_SYMBOL_GPL(qcom_scm_pas_prepare_and_auth_reset);
  887. /**
  888. * qcom_scm_pas_shutdown() - Shut down the remote processor
  889. * @pas_id: peripheral authentication service id
  890. *
  891. * Returns 0 on success.
  892. */
  893. int qcom_scm_pas_shutdown(u32 pas_id)
  894. {
  895. int ret;
  896. struct qcom_scm_desc desc = {
  897. .svc = QCOM_SCM_SVC_PIL,
  898. .cmd = QCOM_SCM_PIL_PAS_SHUTDOWN,
  899. .arginfo = QCOM_SCM_ARGS(1),
  900. .args[0] = pas_id,
  901. .owner = ARM_SMCCC_OWNER_SIP,
  902. };
  903. struct qcom_scm_res res;
  904. ret = qcom_scm_clk_enable();
  905. if (ret)
  906. return ret;
  907. ret = qcom_scm_bw_enable();
  908. if (ret)
  909. goto disable_clk;
  910. ret = qcom_scm_call(__scm->dev, &desc, &res);
  911. qcom_scm_bw_disable();
  912. disable_clk:
  913. qcom_scm_clk_disable();
  914. return ret ? : res.result[0];
  915. }
  916. EXPORT_SYMBOL_GPL(qcom_scm_pas_shutdown);
  917. /**
  918. * qcom_scm_pas_supported() - Check if the peripheral authentication service is
  919. * available for the given peripherial
  920. * @pas_id: peripheral authentication service id
  921. *
  922. * Returns true if PAS is supported for this peripheral, otherwise false.
  923. */
  924. bool qcom_scm_pas_supported(u32 pas_id)
  925. {
  926. int ret;
  927. struct qcom_scm_desc desc = {
  928. .svc = QCOM_SCM_SVC_PIL,
  929. .cmd = QCOM_SCM_PIL_PAS_IS_SUPPORTED,
  930. .arginfo = QCOM_SCM_ARGS(1),
  931. .args[0] = pas_id,
  932. .owner = ARM_SMCCC_OWNER_SIP,
  933. };
  934. struct qcom_scm_res res;
  935. if (!__qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_PIL,
  936. QCOM_SCM_PIL_PAS_IS_SUPPORTED))
  937. return false;
  938. ret = qcom_scm_call(__scm->dev, &desc, &res);
  939. return ret ? false : !!res.result[0];
  940. }
  941. EXPORT_SYMBOL_GPL(qcom_scm_pas_supported);
  942. static int __qcom_scm_pas_mss_reset(struct device *dev, bool reset)
  943. {
  944. struct qcom_scm_desc desc = {
  945. .svc = QCOM_SCM_SVC_PIL,
  946. .cmd = QCOM_SCM_PIL_PAS_MSS_RESET,
  947. .arginfo = QCOM_SCM_ARGS(2),
  948. .args[0] = reset,
  949. .args[1] = 0,
  950. .owner = ARM_SMCCC_OWNER_SIP,
  951. };
  952. struct qcom_scm_res res;
  953. int ret;
  954. ret = qcom_scm_call(__scm->dev, &desc, &res);
  955. return ret ? : res.result[0];
  956. }
  957. static int qcom_scm_pas_reset_assert(struct reset_controller_dev *rcdev,
  958. unsigned long idx)
  959. {
  960. if (idx != 0)
  961. return -EINVAL;
  962. return __qcom_scm_pas_mss_reset(__scm->dev, 1);
  963. }
  964. static int qcom_scm_pas_reset_deassert(struct reset_controller_dev *rcdev,
  965. unsigned long idx)
  966. {
  967. if (idx != 0)
  968. return -EINVAL;
  969. return __qcom_scm_pas_mss_reset(__scm->dev, 0);
  970. }
  971. static const struct reset_control_ops qcom_scm_pas_reset_ops = {
  972. .assert = qcom_scm_pas_reset_assert,
  973. .deassert = qcom_scm_pas_reset_deassert,
  974. };
  975. int qcom_scm_io_readl(phys_addr_t addr, unsigned int *val)
  976. {
  977. struct qcom_scm_desc desc = {
  978. .svc = QCOM_SCM_SVC_IO,
  979. .cmd = QCOM_SCM_IO_READ,
  980. .arginfo = QCOM_SCM_ARGS(1),
  981. .args[0] = addr,
  982. .owner = ARM_SMCCC_OWNER_SIP,
  983. };
  984. struct qcom_scm_res res;
  985. int ret;
  986. ret = qcom_scm_call_atomic(__scm->dev, &desc, &res);
  987. if (ret >= 0)
  988. *val = res.result[0];
  989. return ret < 0 ? ret : 0;
  990. }
  991. EXPORT_SYMBOL_GPL(qcom_scm_io_readl);
  992. int qcom_scm_io_writel(phys_addr_t addr, unsigned int val)
  993. {
  994. struct qcom_scm_desc desc = {
  995. .svc = QCOM_SCM_SVC_IO,
  996. .cmd = QCOM_SCM_IO_WRITE,
  997. .arginfo = QCOM_SCM_ARGS(2),
  998. .args[0] = addr,
  999. .args[1] = val,
  1000. .owner = ARM_SMCCC_OWNER_SIP,
  1001. };
  1002. return qcom_scm_call_atomic(__scm->dev, &desc, NULL);
  1003. }
  1004. EXPORT_SYMBOL_GPL(qcom_scm_io_writel);
  1005. /**
  1006. * qcom_scm_restore_sec_cfg_available() - Check if secure environment
  1007. * supports restore security config interface.
  1008. *
  1009. * Return true if restore-cfg interface is supported, false if not.
  1010. */
  1011. bool qcom_scm_restore_sec_cfg_available(void)
  1012. {
  1013. return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_MP,
  1014. QCOM_SCM_MP_RESTORE_SEC_CFG);
  1015. }
  1016. EXPORT_SYMBOL_GPL(qcom_scm_restore_sec_cfg_available);
  1017. int qcom_scm_restore_sec_cfg(u32 device_id, u32 spare)
  1018. {
  1019. struct qcom_scm_desc desc = {
  1020. .svc = QCOM_SCM_SVC_MP,
  1021. .cmd = QCOM_SCM_MP_RESTORE_SEC_CFG,
  1022. .arginfo = QCOM_SCM_ARGS(2),
  1023. .args[0] = device_id,
  1024. .args[1] = spare,
  1025. .owner = ARM_SMCCC_OWNER_SIP,
  1026. };
  1027. struct qcom_scm_res res;
  1028. int ret;
  1029. ret = qcom_scm_call(__scm->dev, &desc, &res);
  1030. return ret ? : res.result[0];
  1031. }
  1032. EXPORT_SYMBOL_GPL(qcom_scm_restore_sec_cfg);
  1033. #define QCOM_SCM_CP_APERTURE_CONTEXT_MASK GENMASK(7, 0)
  1034. bool qcom_scm_set_gpu_smmu_aperture_is_available(void)
  1035. {
  1036. return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_MP,
  1037. QCOM_SCM_MP_CP_SMMU_APERTURE_ID);
  1038. }
  1039. EXPORT_SYMBOL_GPL(qcom_scm_set_gpu_smmu_aperture_is_available);
  1040. int qcom_scm_set_gpu_smmu_aperture(unsigned int context_bank)
  1041. {
  1042. struct qcom_scm_desc desc = {
  1043. .svc = QCOM_SCM_SVC_MP,
  1044. .cmd = QCOM_SCM_MP_CP_SMMU_APERTURE_ID,
  1045. .arginfo = QCOM_SCM_ARGS(4),
  1046. .args[0] = 0xffff0000 | FIELD_PREP(QCOM_SCM_CP_APERTURE_CONTEXT_MASK, context_bank),
  1047. .args[1] = 0xffffffff,
  1048. .args[2] = 0xffffffff,
  1049. .args[3] = 0xffffffff,
  1050. .owner = ARM_SMCCC_OWNER_SIP
  1051. };
  1052. return qcom_scm_call(__scm->dev, &desc, NULL);
  1053. }
  1054. EXPORT_SYMBOL_GPL(qcom_scm_set_gpu_smmu_aperture);
  1055. int qcom_scm_iommu_secure_ptbl_size(u32 spare, size_t *size)
  1056. {
  1057. struct qcom_scm_desc desc = {
  1058. .svc = QCOM_SCM_SVC_MP,
  1059. .cmd = QCOM_SCM_MP_IOMMU_SECURE_PTBL_SIZE,
  1060. .arginfo = QCOM_SCM_ARGS(1),
  1061. .args[0] = spare,
  1062. .owner = ARM_SMCCC_OWNER_SIP,
  1063. };
  1064. struct qcom_scm_res res;
  1065. int ret;
  1066. ret = qcom_scm_call(__scm->dev, &desc, &res);
  1067. if (size)
  1068. *size = res.result[0];
  1069. return ret ? : res.result[1];
  1070. }
  1071. EXPORT_SYMBOL_GPL(qcom_scm_iommu_secure_ptbl_size);
  1072. int qcom_scm_iommu_secure_ptbl_init(u64 addr, u32 size, u32 spare)
  1073. {
  1074. struct qcom_scm_desc desc = {
  1075. .svc = QCOM_SCM_SVC_MP,
  1076. .cmd = QCOM_SCM_MP_IOMMU_SECURE_PTBL_INIT,
  1077. .arginfo = QCOM_SCM_ARGS(3, QCOM_SCM_RW, QCOM_SCM_VAL,
  1078. QCOM_SCM_VAL),
  1079. .args[0] = addr,
  1080. .args[1] = size,
  1081. .args[2] = spare,
  1082. .owner = ARM_SMCCC_OWNER_SIP,
  1083. };
  1084. int ret;
  1085. ret = qcom_scm_call(__scm->dev, &desc, NULL);
  1086. /* the pg table has been initialized already, ignore the error */
  1087. if (ret == -EPERM)
  1088. ret = 0;
  1089. return ret;
  1090. }
  1091. EXPORT_SYMBOL_GPL(qcom_scm_iommu_secure_ptbl_init);
  1092. int qcom_scm_iommu_set_cp_pool_size(u32 spare, u32 size)
  1093. {
  1094. struct qcom_scm_desc desc = {
  1095. .svc = QCOM_SCM_SVC_MP,
  1096. .cmd = QCOM_SCM_MP_IOMMU_SET_CP_POOL_SIZE,
  1097. .arginfo = QCOM_SCM_ARGS(2),
  1098. .args[0] = size,
  1099. .args[1] = spare,
  1100. .owner = ARM_SMCCC_OWNER_SIP,
  1101. };
  1102. return qcom_scm_call(__scm->dev, &desc, NULL);
  1103. }
  1104. EXPORT_SYMBOL_GPL(qcom_scm_iommu_set_cp_pool_size);
  1105. int qcom_scm_mem_protect_video_var(u32 cp_start, u32 cp_size,
  1106. u32 cp_nonpixel_start,
  1107. u32 cp_nonpixel_size)
  1108. {
  1109. int ret;
  1110. struct qcom_scm_desc desc = {
  1111. .svc = QCOM_SCM_SVC_MP,
  1112. .cmd = QCOM_SCM_MP_VIDEO_VAR,
  1113. .arginfo = QCOM_SCM_ARGS(4, QCOM_SCM_VAL, QCOM_SCM_VAL,
  1114. QCOM_SCM_VAL, QCOM_SCM_VAL),
  1115. .args[0] = cp_start,
  1116. .args[1] = cp_size,
  1117. .args[2] = cp_nonpixel_start,
  1118. .args[3] = cp_nonpixel_size,
  1119. .owner = ARM_SMCCC_OWNER_SIP,
  1120. };
  1121. struct qcom_scm_res res;
  1122. ret = qcom_scm_call(__scm->dev, &desc, &res);
  1123. return ret ? : res.result[0];
  1124. }
  1125. EXPORT_SYMBOL_GPL(qcom_scm_mem_protect_video_var);
  1126. static int __qcom_scm_assign_mem(struct device *dev, phys_addr_t mem_region,
  1127. size_t mem_sz, phys_addr_t src, size_t src_sz,
  1128. phys_addr_t dest, size_t dest_sz)
  1129. {
  1130. int ret;
  1131. struct qcom_scm_desc desc = {
  1132. .svc = QCOM_SCM_SVC_MP,
  1133. .cmd = QCOM_SCM_MP_ASSIGN,
  1134. .arginfo = QCOM_SCM_ARGS(7, QCOM_SCM_RO, QCOM_SCM_VAL,
  1135. QCOM_SCM_RO, QCOM_SCM_VAL, QCOM_SCM_RO,
  1136. QCOM_SCM_VAL, QCOM_SCM_VAL),
  1137. .args[0] = mem_region,
  1138. .args[1] = mem_sz,
  1139. .args[2] = src,
  1140. .args[3] = src_sz,
  1141. .args[4] = dest,
  1142. .args[5] = dest_sz,
  1143. .args[6] = 0,
  1144. .owner = ARM_SMCCC_OWNER_SIP,
  1145. };
  1146. struct qcom_scm_res res;
  1147. ret = qcom_scm_call(dev, &desc, &res);
  1148. return ret ? : res.result[0];
  1149. }
  1150. /**
  1151. * qcom_scm_assign_mem() - Make a secure call to reassign memory ownership
  1152. * @mem_addr: mem region whose ownership need to be reassigned
  1153. * @mem_sz: size of the region.
  1154. * @srcvm: vmid for current set of owners, each set bit in
  1155. * flag indicate a unique owner
  1156. * @newvm: array having new owners and corresponding permission
  1157. * flags
  1158. * @dest_cnt: number of owners in next set.
  1159. *
  1160. * Return negative errno on failure or 0 on success with @srcvm updated.
  1161. */
  1162. int qcom_scm_assign_mem(phys_addr_t mem_addr, size_t mem_sz,
  1163. u64 *srcvm,
  1164. const struct qcom_scm_vmperm *newvm,
  1165. unsigned int dest_cnt)
  1166. {
  1167. struct qcom_scm_current_perm_info *destvm;
  1168. struct qcom_scm_mem_map_info *mem_to_map;
  1169. phys_addr_t mem_to_map_phys;
  1170. phys_addr_t dest_phys;
  1171. phys_addr_t ptr_phys;
  1172. size_t mem_to_map_sz;
  1173. size_t dest_sz;
  1174. size_t src_sz;
  1175. size_t ptr_sz;
  1176. int next_vm;
  1177. __le32 *src;
  1178. int ret, i, b;
  1179. u64 srcvm_bits = *srcvm;
  1180. src_sz = hweight64(srcvm_bits) * sizeof(*src);
  1181. mem_to_map_sz = sizeof(*mem_to_map);
  1182. dest_sz = dest_cnt * sizeof(*destvm);
  1183. ptr_sz = ALIGN(src_sz, SZ_64) + ALIGN(mem_to_map_sz, SZ_64) +
  1184. ALIGN(dest_sz, SZ_64);
  1185. void *ptr __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
  1186. ptr_sz, GFP_KERNEL);
  1187. if (!ptr)
  1188. return -ENOMEM;
  1189. ptr_phys = qcom_tzmem_to_phys(ptr);
  1190. /* Fill source vmid detail */
  1191. src = ptr;
  1192. i = 0;
  1193. for (b = 0; b < BITS_PER_TYPE(u64); b++) {
  1194. if (srcvm_bits & BIT(b))
  1195. src[i++] = cpu_to_le32(b);
  1196. }
  1197. /* Fill details of mem buff to map */
  1198. mem_to_map = ptr + ALIGN(src_sz, SZ_64);
  1199. mem_to_map_phys = ptr_phys + ALIGN(src_sz, SZ_64);
  1200. mem_to_map->mem_addr = cpu_to_le64(mem_addr);
  1201. mem_to_map->mem_size = cpu_to_le64(mem_sz);
  1202. next_vm = 0;
  1203. /* Fill details of next vmid detail */
  1204. destvm = ptr + ALIGN(mem_to_map_sz, SZ_64) + ALIGN(src_sz, SZ_64);
  1205. dest_phys = ptr_phys + ALIGN(mem_to_map_sz, SZ_64) + ALIGN(src_sz, SZ_64);
  1206. for (i = 0; i < dest_cnt; i++, destvm++, newvm++) {
  1207. destvm->vmid = cpu_to_le32(newvm->vmid);
  1208. destvm->perm = cpu_to_le32(newvm->perm);
  1209. destvm->ctx = 0;
  1210. destvm->ctx_size = 0;
  1211. next_vm |= BIT(newvm->vmid);
  1212. }
  1213. ret = __qcom_scm_assign_mem(__scm->dev, mem_to_map_phys, mem_to_map_sz,
  1214. ptr_phys, src_sz, dest_phys, dest_sz);
  1215. if (ret) {
  1216. dev_err(__scm->dev,
  1217. "Assign memory protection call failed %d\n", ret);
  1218. return ret;
  1219. }
  1220. *srcvm = next_vm;
  1221. return 0;
  1222. }
  1223. EXPORT_SYMBOL_GPL(qcom_scm_assign_mem);
  1224. /**
  1225. * qcom_scm_ocmem_lock_available() - is OCMEM lock/unlock interface available
  1226. */
  1227. bool qcom_scm_ocmem_lock_available(void)
  1228. {
  1229. return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_OCMEM,
  1230. QCOM_SCM_OCMEM_LOCK_CMD);
  1231. }
  1232. EXPORT_SYMBOL_GPL(qcom_scm_ocmem_lock_available);
  1233. /**
  1234. * qcom_scm_ocmem_lock() - call OCMEM lock interface to assign an OCMEM
  1235. * region to the specified initiator
  1236. *
  1237. * @id: tz initiator id
  1238. * @offset: OCMEM offset
  1239. * @size: OCMEM size
  1240. * @mode: access mode (WIDE/NARROW)
  1241. */
  1242. int qcom_scm_ocmem_lock(enum qcom_scm_ocmem_client id, u32 offset, u32 size,
  1243. u32 mode)
  1244. {
  1245. struct qcom_scm_desc desc = {
  1246. .svc = QCOM_SCM_SVC_OCMEM,
  1247. .cmd = QCOM_SCM_OCMEM_LOCK_CMD,
  1248. .args[0] = id,
  1249. .args[1] = offset,
  1250. .args[2] = size,
  1251. .args[3] = mode,
  1252. .arginfo = QCOM_SCM_ARGS(4),
  1253. };
  1254. return qcom_scm_call(__scm->dev, &desc, NULL);
  1255. }
  1256. EXPORT_SYMBOL_GPL(qcom_scm_ocmem_lock);
  1257. /**
  1258. * qcom_scm_ocmem_unlock() - call OCMEM unlock interface to release an OCMEM
  1259. * region from the specified initiator
  1260. *
  1261. * @id: tz initiator id
  1262. * @offset: OCMEM offset
  1263. * @size: OCMEM size
  1264. */
  1265. int qcom_scm_ocmem_unlock(enum qcom_scm_ocmem_client id, u32 offset, u32 size)
  1266. {
  1267. struct qcom_scm_desc desc = {
  1268. .svc = QCOM_SCM_SVC_OCMEM,
  1269. .cmd = QCOM_SCM_OCMEM_UNLOCK_CMD,
  1270. .args[0] = id,
  1271. .args[1] = offset,
  1272. .args[2] = size,
  1273. .arginfo = QCOM_SCM_ARGS(3),
  1274. };
  1275. return qcom_scm_call(__scm->dev, &desc, NULL);
  1276. }
  1277. EXPORT_SYMBOL_GPL(qcom_scm_ocmem_unlock);
  1278. /**
  1279. * qcom_scm_ice_available() - Is the ICE key programming interface available?
  1280. *
  1281. * Return: true iff the SCM calls wrapped by qcom_scm_ice_invalidate_key() and
  1282. * qcom_scm_ice_set_key() are available.
  1283. */
  1284. bool qcom_scm_ice_available(void)
  1285. {
  1286. return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_ES,
  1287. QCOM_SCM_ES_INVALIDATE_ICE_KEY) &&
  1288. __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_ES,
  1289. QCOM_SCM_ES_CONFIG_SET_ICE_KEY);
  1290. }
  1291. EXPORT_SYMBOL_GPL(qcom_scm_ice_available);
  1292. /**
  1293. * qcom_scm_ice_invalidate_key() - Invalidate an inline encryption key
  1294. * @index: the keyslot to invalidate
  1295. *
  1296. * The UFSHCI and eMMC standards define a standard way to do this, but it
  1297. * doesn't work on these SoCs; only this SCM call does.
  1298. *
  1299. * It is assumed that the SoC has only one ICE instance being used, as this SCM
  1300. * call doesn't specify which ICE instance the keyslot belongs to.
  1301. *
  1302. * Return: 0 on success; -errno on failure.
  1303. */
  1304. int qcom_scm_ice_invalidate_key(u32 index)
  1305. {
  1306. struct qcom_scm_desc desc = {
  1307. .svc = QCOM_SCM_SVC_ES,
  1308. .cmd = QCOM_SCM_ES_INVALIDATE_ICE_KEY,
  1309. .arginfo = QCOM_SCM_ARGS(1),
  1310. .args[0] = index,
  1311. .owner = ARM_SMCCC_OWNER_SIP,
  1312. };
  1313. return qcom_scm_call(__scm->dev, &desc, NULL);
  1314. }
  1315. EXPORT_SYMBOL_GPL(qcom_scm_ice_invalidate_key);
  1316. /**
  1317. * qcom_scm_ice_set_key() - Set an inline encryption key
  1318. * @index: the keyslot into which to set the key
  1319. * @key: the key to program
  1320. * @key_size: the size of the key in bytes
  1321. * @cipher: the encryption algorithm the key is for
  1322. * @data_unit_size: the encryption data unit size, i.e. the size of each
  1323. * individual plaintext and ciphertext. Given in 512-byte
  1324. * units, e.g. 1 = 512 bytes, 8 = 4096 bytes, etc.
  1325. *
  1326. * Program a key into a keyslot of Qualcomm ICE (Inline Crypto Engine), where it
  1327. * can then be used to encrypt/decrypt UFS or eMMC I/O requests inline.
  1328. *
  1329. * The UFSHCI and eMMC standards define a standard way to do this, but it
  1330. * doesn't work on these SoCs; only this SCM call does.
  1331. *
  1332. * It is assumed that the SoC has only one ICE instance being used, as this SCM
  1333. * call doesn't specify which ICE instance the keyslot belongs to.
  1334. *
  1335. * Return: 0 on success; -errno on failure.
  1336. */
  1337. int qcom_scm_ice_set_key(u32 index, const u8 *key, u32 key_size,
  1338. enum qcom_scm_ice_cipher cipher, u32 data_unit_size)
  1339. {
  1340. struct qcom_scm_desc desc = {
  1341. .svc = QCOM_SCM_SVC_ES,
  1342. .cmd = QCOM_SCM_ES_CONFIG_SET_ICE_KEY,
  1343. .arginfo = QCOM_SCM_ARGS(5, QCOM_SCM_VAL, QCOM_SCM_RW,
  1344. QCOM_SCM_VAL, QCOM_SCM_VAL,
  1345. QCOM_SCM_VAL),
  1346. .args[0] = index,
  1347. .args[2] = key_size,
  1348. .args[3] = cipher,
  1349. .args[4] = data_unit_size,
  1350. .owner = ARM_SMCCC_OWNER_SIP,
  1351. };
  1352. int ret;
  1353. void *keybuf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
  1354. key_size,
  1355. GFP_KERNEL);
  1356. if (!keybuf)
  1357. return -ENOMEM;
  1358. memcpy(keybuf, key, key_size);
  1359. desc.args[1] = qcom_tzmem_to_phys(keybuf);
  1360. ret = qcom_scm_call(__scm->dev, &desc, NULL);
  1361. memzero_explicit(keybuf, key_size);
  1362. return ret;
  1363. }
  1364. EXPORT_SYMBOL_GPL(qcom_scm_ice_set_key);
  1365. bool qcom_scm_has_wrapped_key_support(void)
  1366. {
  1367. return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_ES,
  1368. QCOM_SCM_ES_DERIVE_SW_SECRET) &&
  1369. __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_ES,
  1370. QCOM_SCM_ES_GENERATE_ICE_KEY) &&
  1371. __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_ES,
  1372. QCOM_SCM_ES_PREPARE_ICE_KEY) &&
  1373. __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_ES,
  1374. QCOM_SCM_ES_IMPORT_ICE_KEY);
  1375. }
  1376. EXPORT_SYMBOL_GPL(qcom_scm_has_wrapped_key_support);
  1377. /**
  1378. * qcom_scm_derive_sw_secret() - Derive software secret from wrapped key
  1379. * @eph_key: an ephemerally-wrapped key
  1380. * @eph_key_size: size of @eph_key in bytes
  1381. * @sw_secret: output buffer for the software secret
  1382. * @sw_secret_size: size of the software secret to derive in bytes
  1383. *
  1384. * Derive a software secret from an ephemerally-wrapped key for software crypto
  1385. * operations. This is done by calling into the secure execution environment,
  1386. * which then calls into the hardware to unwrap and derive the secret.
  1387. *
  1388. * For more information on sw_secret, see the "Hardware-wrapped keys" section of
  1389. * Documentation/block/inline-encryption.rst.
  1390. *
  1391. * Return: 0 on success; -errno on failure.
  1392. */
  1393. int qcom_scm_derive_sw_secret(const u8 *eph_key, size_t eph_key_size,
  1394. u8 *sw_secret, size_t sw_secret_size)
  1395. {
  1396. struct qcom_scm_desc desc = {
  1397. .svc = QCOM_SCM_SVC_ES,
  1398. .cmd = QCOM_SCM_ES_DERIVE_SW_SECRET,
  1399. .arginfo = QCOM_SCM_ARGS(4, QCOM_SCM_RW, QCOM_SCM_VAL,
  1400. QCOM_SCM_RW, QCOM_SCM_VAL),
  1401. .owner = ARM_SMCCC_OWNER_SIP,
  1402. };
  1403. int ret;
  1404. void *eph_key_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
  1405. eph_key_size,
  1406. GFP_KERNEL);
  1407. if (!eph_key_buf)
  1408. return -ENOMEM;
  1409. void *sw_secret_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
  1410. sw_secret_size,
  1411. GFP_KERNEL);
  1412. if (!sw_secret_buf)
  1413. return -ENOMEM;
  1414. memcpy(eph_key_buf, eph_key, eph_key_size);
  1415. desc.args[0] = qcom_tzmem_to_phys(eph_key_buf);
  1416. desc.args[1] = eph_key_size;
  1417. desc.args[2] = qcom_tzmem_to_phys(sw_secret_buf);
  1418. desc.args[3] = sw_secret_size;
  1419. ret = qcom_scm_call(__scm->dev, &desc, NULL);
  1420. if (!ret)
  1421. memcpy(sw_secret, sw_secret_buf, sw_secret_size);
  1422. memzero_explicit(eph_key_buf, eph_key_size);
  1423. memzero_explicit(sw_secret_buf, sw_secret_size);
  1424. return ret;
  1425. }
  1426. EXPORT_SYMBOL_GPL(qcom_scm_derive_sw_secret);
  1427. /**
  1428. * qcom_scm_generate_ice_key() - Generate a wrapped key for storage encryption
  1429. * @lt_key: output buffer for the long-term wrapped key
  1430. * @lt_key_size: size of @lt_key in bytes. Must be the exact wrapped key size
  1431. * used by the SoC.
  1432. *
  1433. * Generate a key using the built-in HW module in the SoC. The resulting key is
  1434. * returned wrapped with the platform-specific Key Encryption Key.
  1435. *
  1436. * Return: 0 on success; -errno on failure.
  1437. */
  1438. int qcom_scm_generate_ice_key(u8 *lt_key, size_t lt_key_size)
  1439. {
  1440. struct qcom_scm_desc desc = {
  1441. .svc = QCOM_SCM_SVC_ES,
  1442. .cmd = QCOM_SCM_ES_GENERATE_ICE_KEY,
  1443. .arginfo = QCOM_SCM_ARGS(2, QCOM_SCM_RW, QCOM_SCM_VAL),
  1444. .owner = ARM_SMCCC_OWNER_SIP,
  1445. };
  1446. int ret;
  1447. void *lt_key_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
  1448. lt_key_size,
  1449. GFP_KERNEL);
  1450. if (!lt_key_buf)
  1451. return -ENOMEM;
  1452. desc.args[0] = qcom_tzmem_to_phys(lt_key_buf);
  1453. desc.args[1] = lt_key_size;
  1454. ret = qcom_scm_call(__scm->dev, &desc, NULL);
  1455. if (!ret)
  1456. memcpy(lt_key, lt_key_buf, lt_key_size);
  1457. memzero_explicit(lt_key_buf, lt_key_size);
  1458. return ret;
  1459. }
  1460. EXPORT_SYMBOL_GPL(qcom_scm_generate_ice_key);
  1461. /**
  1462. * qcom_scm_prepare_ice_key() - Re-wrap a key with the per-boot ephemeral key
  1463. * @lt_key: a long-term wrapped key
  1464. * @lt_key_size: size of @lt_key in bytes
  1465. * @eph_key: output buffer for the ephemerally-wrapped key
  1466. * @eph_key_size: size of @eph_key in bytes. Must be the exact wrapped key size
  1467. * used by the SoC.
  1468. *
  1469. * Given a long-term wrapped key, re-wrap it with the per-boot ephemeral key for
  1470. * added protection. The resulting key will only be valid for the current boot.
  1471. *
  1472. * Return: 0 on success; -errno on failure.
  1473. */
  1474. int qcom_scm_prepare_ice_key(const u8 *lt_key, size_t lt_key_size,
  1475. u8 *eph_key, size_t eph_key_size)
  1476. {
  1477. struct qcom_scm_desc desc = {
  1478. .svc = QCOM_SCM_SVC_ES,
  1479. .cmd = QCOM_SCM_ES_PREPARE_ICE_KEY,
  1480. .arginfo = QCOM_SCM_ARGS(4, QCOM_SCM_RO, QCOM_SCM_VAL,
  1481. QCOM_SCM_RW, QCOM_SCM_VAL),
  1482. .owner = ARM_SMCCC_OWNER_SIP,
  1483. };
  1484. int ret;
  1485. void *lt_key_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
  1486. lt_key_size,
  1487. GFP_KERNEL);
  1488. if (!lt_key_buf)
  1489. return -ENOMEM;
  1490. void *eph_key_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
  1491. eph_key_size,
  1492. GFP_KERNEL);
  1493. if (!eph_key_buf)
  1494. return -ENOMEM;
  1495. memcpy(lt_key_buf, lt_key, lt_key_size);
  1496. desc.args[0] = qcom_tzmem_to_phys(lt_key_buf);
  1497. desc.args[1] = lt_key_size;
  1498. desc.args[2] = qcom_tzmem_to_phys(eph_key_buf);
  1499. desc.args[3] = eph_key_size;
  1500. ret = qcom_scm_call(__scm->dev, &desc, NULL);
  1501. if (!ret)
  1502. memcpy(eph_key, eph_key_buf, eph_key_size);
  1503. memzero_explicit(lt_key_buf, lt_key_size);
  1504. memzero_explicit(eph_key_buf, eph_key_size);
  1505. return ret;
  1506. }
  1507. EXPORT_SYMBOL_GPL(qcom_scm_prepare_ice_key);
  1508. /**
  1509. * qcom_scm_import_ice_key() - Import key for storage encryption
  1510. * @raw_key: the raw key to import
  1511. * @raw_key_size: size of @raw_key in bytes
  1512. * @lt_key: output buffer for the long-term wrapped key
  1513. * @lt_key_size: size of @lt_key in bytes. Must be the exact wrapped key size
  1514. * used by the SoC.
  1515. *
  1516. * Import a raw key and return a long-term wrapped key. Uses the SoC's HWKM to
  1517. * wrap the raw key using the platform-specific Key Encryption Key.
  1518. *
  1519. * Return: 0 on success; -errno on failure.
  1520. */
  1521. int qcom_scm_import_ice_key(const u8 *raw_key, size_t raw_key_size,
  1522. u8 *lt_key, size_t lt_key_size)
  1523. {
  1524. struct qcom_scm_desc desc = {
  1525. .svc = QCOM_SCM_SVC_ES,
  1526. .cmd = QCOM_SCM_ES_IMPORT_ICE_KEY,
  1527. .arginfo = QCOM_SCM_ARGS(4, QCOM_SCM_RO, QCOM_SCM_VAL,
  1528. QCOM_SCM_RW, QCOM_SCM_VAL),
  1529. .owner = ARM_SMCCC_OWNER_SIP,
  1530. };
  1531. int ret;
  1532. void *raw_key_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
  1533. raw_key_size,
  1534. GFP_KERNEL);
  1535. if (!raw_key_buf)
  1536. return -ENOMEM;
  1537. void *lt_key_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
  1538. lt_key_size,
  1539. GFP_KERNEL);
  1540. if (!lt_key_buf)
  1541. return -ENOMEM;
  1542. memcpy(raw_key_buf, raw_key, raw_key_size);
  1543. desc.args[0] = qcom_tzmem_to_phys(raw_key_buf);
  1544. desc.args[1] = raw_key_size;
  1545. desc.args[2] = qcom_tzmem_to_phys(lt_key_buf);
  1546. desc.args[3] = lt_key_size;
  1547. ret = qcom_scm_call(__scm->dev, &desc, NULL);
  1548. if (!ret)
  1549. memcpy(lt_key, lt_key_buf, lt_key_size);
  1550. memzero_explicit(raw_key_buf, raw_key_size);
  1551. memzero_explicit(lt_key_buf, lt_key_size);
  1552. return ret;
  1553. }
  1554. EXPORT_SYMBOL_GPL(qcom_scm_import_ice_key);
  1555. /**
  1556. * qcom_scm_hdcp_available() - Check if secure environment supports HDCP.
  1557. *
  1558. * Return true if HDCP is supported, false if not.
  1559. */
  1560. bool qcom_scm_hdcp_available(void)
  1561. {
  1562. bool avail;
  1563. int ret = qcom_scm_clk_enable();
  1564. if (ret)
  1565. return ret;
  1566. avail = __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_HDCP,
  1567. QCOM_SCM_HDCP_INVOKE);
  1568. qcom_scm_clk_disable();
  1569. return avail;
  1570. }
  1571. EXPORT_SYMBOL_GPL(qcom_scm_hdcp_available);
  1572. /**
  1573. * qcom_scm_hdcp_req() - Send HDCP request.
  1574. * @req: HDCP request array
  1575. * @req_cnt: HDCP request array count
  1576. * @resp: response buffer passed to SCM
  1577. *
  1578. * Write HDCP register(s) through SCM.
  1579. */
  1580. int qcom_scm_hdcp_req(struct qcom_scm_hdcp_req *req, u32 req_cnt, u32 *resp)
  1581. {
  1582. int ret;
  1583. struct qcom_scm_desc desc = {
  1584. .svc = QCOM_SCM_SVC_HDCP,
  1585. .cmd = QCOM_SCM_HDCP_INVOKE,
  1586. .arginfo = QCOM_SCM_ARGS(10),
  1587. .args = {
  1588. req[0].addr,
  1589. req[0].val,
  1590. req[1].addr,
  1591. req[1].val,
  1592. req[2].addr,
  1593. req[2].val,
  1594. req[3].addr,
  1595. req[3].val,
  1596. req[4].addr,
  1597. req[4].val
  1598. },
  1599. .owner = ARM_SMCCC_OWNER_SIP,
  1600. };
  1601. struct qcom_scm_res res;
  1602. if (req_cnt > QCOM_SCM_HDCP_MAX_REQ_CNT)
  1603. return -ERANGE;
  1604. ret = qcom_scm_clk_enable();
  1605. if (ret)
  1606. return ret;
  1607. ret = qcom_scm_call(__scm->dev, &desc, &res);
  1608. *resp = res.result[0];
  1609. qcom_scm_clk_disable();
  1610. return ret;
  1611. }
  1612. EXPORT_SYMBOL_GPL(qcom_scm_hdcp_req);
  1613. int qcom_scm_iommu_set_pt_format(u32 sec_id, u32 ctx_num, u32 pt_fmt)
  1614. {
  1615. struct qcom_scm_desc desc = {
  1616. .svc = QCOM_SCM_SVC_SMMU_PROGRAM,
  1617. .cmd = QCOM_SCM_SMMU_PT_FORMAT,
  1618. .arginfo = QCOM_SCM_ARGS(3),
  1619. .args[0] = sec_id,
  1620. .args[1] = ctx_num,
  1621. .args[2] = pt_fmt, /* 0: LPAE AArch32 - 1: AArch64 */
  1622. .owner = ARM_SMCCC_OWNER_SIP,
  1623. };
  1624. return qcom_scm_call(__scm->dev, &desc, NULL);
  1625. }
  1626. EXPORT_SYMBOL_GPL(qcom_scm_iommu_set_pt_format);
  1627. int qcom_scm_qsmmu500_wait_safe_toggle(bool en)
  1628. {
  1629. struct qcom_scm_desc desc = {
  1630. .svc = QCOM_SCM_SVC_SMMU_PROGRAM,
  1631. .cmd = QCOM_SCM_SMMU_CONFIG_ERRATA1,
  1632. .arginfo = QCOM_SCM_ARGS(2),
  1633. .args[0] = QCOM_SCM_SMMU_CONFIG_ERRATA1_CLIENT_ALL,
  1634. .args[1] = en,
  1635. .owner = ARM_SMCCC_OWNER_SIP,
  1636. };
  1637. return qcom_scm_call_atomic(__scm->dev, &desc, NULL);
  1638. }
  1639. EXPORT_SYMBOL_GPL(qcom_scm_qsmmu500_wait_safe_toggle);
  1640. bool qcom_scm_lmh_dcvsh_available(void)
  1641. {
  1642. return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_LMH, QCOM_SCM_LMH_LIMIT_DCVSH);
  1643. }
  1644. EXPORT_SYMBOL_GPL(qcom_scm_lmh_dcvsh_available);
  1645. /*
  1646. * This is only supposed to be called once by the TZMem module. It takes the
  1647. * SCM struct device as argument and uses it to pass the call as at the time
  1648. * the SHM Bridge is enabled, the SCM is not yet fully set up and doesn't
  1649. * accept global user calls. Don't try to use the __scm pointer here.
  1650. */
  1651. int qcom_scm_shm_bridge_enable(struct device *scm_dev)
  1652. {
  1653. int ret;
  1654. struct qcom_scm_desc desc = {
  1655. .svc = QCOM_SCM_SVC_MP,
  1656. .cmd = QCOM_SCM_MP_SHM_BRIDGE_ENABLE,
  1657. .owner = ARM_SMCCC_OWNER_SIP
  1658. };
  1659. struct qcom_scm_res res;
  1660. if (!__qcom_scm_is_call_available(scm_dev, QCOM_SCM_SVC_MP,
  1661. QCOM_SCM_MP_SHM_BRIDGE_ENABLE))
  1662. return -EOPNOTSUPP;
  1663. ret = qcom_scm_call(scm_dev, &desc, &res);
  1664. if (ret)
  1665. return ret;
  1666. if (res.result[0] == SHMBRIDGE_RESULT_NOTSUPP)
  1667. return -EOPNOTSUPP;
  1668. return res.result[0];
  1669. }
  1670. EXPORT_SYMBOL_GPL(qcom_scm_shm_bridge_enable);
  1671. int qcom_scm_shm_bridge_create(u64 pfn_and_ns_perm_flags,
  1672. u64 ipfn_and_s_perm_flags, u64 size_and_flags,
  1673. u64 ns_vmids, u64 *handle)
  1674. {
  1675. struct qcom_scm_desc desc = {
  1676. .svc = QCOM_SCM_SVC_MP,
  1677. .cmd = QCOM_SCM_MP_SHM_BRIDGE_CREATE,
  1678. .owner = ARM_SMCCC_OWNER_SIP,
  1679. .args[0] = pfn_and_ns_perm_flags,
  1680. .args[1] = ipfn_and_s_perm_flags,
  1681. .args[2] = size_and_flags,
  1682. .args[3] = ns_vmids,
  1683. .arginfo = QCOM_SCM_ARGS(4, QCOM_SCM_VAL, QCOM_SCM_VAL,
  1684. QCOM_SCM_VAL, QCOM_SCM_VAL),
  1685. };
  1686. struct qcom_scm_res res;
  1687. int ret;
  1688. ret = qcom_scm_call(__scm->dev, &desc, &res);
  1689. if (handle && !ret)
  1690. *handle = res.result[1];
  1691. return ret ?: res.result[0];
  1692. }
  1693. EXPORT_SYMBOL_GPL(qcom_scm_shm_bridge_create);
  1694. int qcom_scm_shm_bridge_delete(u64 handle)
  1695. {
  1696. struct qcom_scm_desc desc = {
  1697. .svc = QCOM_SCM_SVC_MP,
  1698. .cmd = QCOM_SCM_MP_SHM_BRIDGE_DELETE,
  1699. .owner = ARM_SMCCC_OWNER_SIP,
  1700. .args[0] = handle,
  1701. .arginfo = QCOM_SCM_ARGS(1, QCOM_SCM_VAL),
  1702. };
  1703. return qcom_scm_call(__scm->dev, &desc, NULL);
  1704. }
  1705. EXPORT_SYMBOL_GPL(qcom_scm_shm_bridge_delete);
  1706. int qcom_scm_lmh_profile_change(u32 profile_id)
  1707. {
  1708. struct qcom_scm_desc desc = {
  1709. .svc = QCOM_SCM_SVC_LMH,
  1710. .cmd = QCOM_SCM_LMH_LIMIT_PROFILE_CHANGE,
  1711. .arginfo = QCOM_SCM_ARGS(1, QCOM_SCM_VAL),
  1712. .args[0] = profile_id,
  1713. .owner = ARM_SMCCC_OWNER_SIP,
  1714. };
  1715. return qcom_scm_call(__scm->dev, &desc, NULL);
  1716. }
  1717. EXPORT_SYMBOL_GPL(qcom_scm_lmh_profile_change);
  1718. int qcom_scm_lmh_dcvsh(u32 payload_fn, u32 payload_reg, u32 payload_val,
  1719. u64 limit_node, u32 node_id, u64 version)
  1720. {
  1721. int ret, payload_size = 5 * sizeof(u32);
  1722. struct qcom_scm_desc desc = {
  1723. .svc = QCOM_SCM_SVC_LMH,
  1724. .cmd = QCOM_SCM_LMH_LIMIT_DCVSH,
  1725. .arginfo = QCOM_SCM_ARGS(5, QCOM_SCM_RO, QCOM_SCM_VAL, QCOM_SCM_VAL,
  1726. QCOM_SCM_VAL, QCOM_SCM_VAL),
  1727. .args[1] = payload_size,
  1728. .args[2] = limit_node,
  1729. .args[3] = node_id,
  1730. .args[4] = version,
  1731. .owner = ARM_SMCCC_OWNER_SIP,
  1732. };
  1733. u32 *payload_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
  1734. payload_size,
  1735. GFP_KERNEL);
  1736. if (!payload_buf)
  1737. return -ENOMEM;
  1738. payload_buf[0] = payload_fn;
  1739. payload_buf[1] = 0;
  1740. payload_buf[2] = payload_reg;
  1741. payload_buf[3] = 1;
  1742. payload_buf[4] = payload_val;
  1743. desc.args[0] = qcom_tzmem_to_phys(payload_buf);
  1744. ret = qcom_scm_call(__scm->dev, &desc, NULL);
  1745. return ret;
  1746. }
  1747. EXPORT_SYMBOL_GPL(qcom_scm_lmh_dcvsh);
  1748. int qcom_scm_gpu_init_regs(u32 gpu_req)
  1749. {
  1750. struct qcom_scm_desc desc = {
  1751. .svc = QCOM_SCM_SVC_GPU,
  1752. .cmd = QCOM_SCM_SVC_GPU_INIT_REGS,
  1753. .arginfo = QCOM_SCM_ARGS(1),
  1754. .args[0] = gpu_req,
  1755. .owner = ARM_SMCCC_OWNER_SIP,
  1756. };
  1757. return qcom_scm_call(__scm->dev, &desc, NULL);
  1758. }
  1759. EXPORT_SYMBOL_GPL(qcom_scm_gpu_init_regs);
  1760. static int qcom_scm_find_dload_address(struct device *dev, u64 *addr)
  1761. {
  1762. struct device_node *tcsr;
  1763. struct device_node *np = dev->of_node;
  1764. struct resource res;
  1765. u32 offset;
  1766. int ret;
  1767. tcsr = of_parse_phandle(np, "qcom,dload-mode", 0);
  1768. if (!tcsr)
  1769. return 0;
  1770. ret = of_address_to_resource(tcsr, 0, &res);
  1771. of_node_put(tcsr);
  1772. if (ret)
  1773. return ret;
  1774. ret = of_property_read_u32_index(np, "qcom,dload-mode", 1, &offset);
  1775. if (ret < 0)
  1776. return ret;
  1777. *addr = res.start + offset;
  1778. return 0;
  1779. }
  1780. #ifdef CONFIG_QCOM_QSEECOM
  1781. /* Lock for QSEECOM SCM call executions */
  1782. static DEFINE_MUTEX(qcom_scm_qseecom_call_lock);
  1783. static int __qcom_scm_qseecom_call(const struct qcom_scm_desc *desc,
  1784. struct qcom_scm_qseecom_resp *res)
  1785. {
  1786. struct qcom_scm_res scm_res = {};
  1787. int status;
  1788. /*
  1789. * QSEECOM SCM calls should not be executed concurrently. Therefore, we
  1790. * require the respective call lock to be held.
  1791. */
  1792. lockdep_assert_held(&qcom_scm_qseecom_call_lock);
  1793. status = qcom_scm_call(__scm->dev, desc, &scm_res);
  1794. res->result = scm_res.result[0];
  1795. res->resp_type = scm_res.result[1];
  1796. res->data = scm_res.result[2];
  1797. if (status)
  1798. return status;
  1799. return 0;
  1800. }
  1801. /**
  1802. * qcom_scm_qseecom_call() - Perform a QSEECOM SCM call.
  1803. * @desc: SCM call descriptor.
  1804. * @res: SCM call response (output).
  1805. *
  1806. * Performs the QSEECOM SCM call described by @desc, returning the response in
  1807. * @rsp.
  1808. *
  1809. * Return: Zero on success, nonzero on failure.
  1810. */
  1811. static int qcom_scm_qseecom_call(const struct qcom_scm_desc *desc,
  1812. struct qcom_scm_qseecom_resp *res)
  1813. {
  1814. int status;
  1815. /*
  1816. * Note: Multiple QSEECOM SCM calls should not be executed same time,
  1817. * so lock things here. This needs to be extended to callback/listener
  1818. * handling when support for that is implemented.
  1819. */
  1820. mutex_lock(&qcom_scm_qseecom_call_lock);
  1821. status = __qcom_scm_qseecom_call(desc, res);
  1822. mutex_unlock(&qcom_scm_qseecom_call_lock);
  1823. dev_dbg(__scm->dev, "%s: owner=%x, svc=%x, cmd=%x, result=%lld, type=%llx, data=%llx\n",
  1824. __func__, desc->owner, desc->svc, desc->cmd, res->result,
  1825. res->resp_type, res->data);
  1826. if (status) {
  1827. dev_err(__scm->dev, "qseecom: scm call failed with error %d\n", status);
  1828. return status;
  1829. }
  1830. /*
  1831. * TODO: Handle incomplete and blocked calls:
  1832. *
  1833. * Incomplete and blocked calls are not supported yet. Some devices
  1834. * and/or commands require those, some don't. Let's warn about them
  1835. * prominently in case someone attempts to try these commands with a
  1836. * device/command combination that isn't supported yet.
  1837. */
  1838. WARN_ON(res->result == QSEECOM_RESULT_INCOMPLETE);
  1839. WARN_ON(res->result == QSEECOM_RESULT_BLOCKED_ON_LISTENER);
  1840. return 0;
  1841. }
  1842. /**
  1843. * qcom_scm_qseecom_get_version() - Query the QSEECOM version.
  1844. * @version: Pointer where the QSEECOM version will be stored.
  1845. *
  1846. * Performs the QSEECOM SCM querying the QSEECOM version currently running in
  1847. * the TrustZone.
  1848. *
  1849. * Return: Zero on success, nonzero on failure.
  1850. */
  1851. static int qcom_scm_qseecom_get_version(u32 *version)
  1852. {
  1853. struct qcom_scm_desc desc = {};
  1854. struct qcom_scm_qseecom_resp res = {};
  1855. u32 feature = 10;
  1856. int ret;
  1857. desc.owner = QSEECOM_TZ_OWNER_SIP;
  1858. desc.svc = QSEECOM_TZ_SVC_INFO;
  1859. desc.cmd = QSEECOM_TZ_CMD_INFO_VERSION;
  1860. desc.arginfo = QCOM_SCM_ARGS(1, QCOM_SCM_VAL);
  1861. desc.args[0] = feature;
  1862. ret = qcom_scm_qseecom_call(&desc, &res);
  1863. if (ret)
  1864. return ret;
  1865. *version = res.result;
  1866. return 0;
  1867. }
  1868. /**
  1869. * qcom_scm_qseecom_app_get_id() - Query the app ID for a given QSEE app name.
  1870. * @app_name: The name of the app.
  1871. * @app_id: The returned app ID.
  1872. *
  1873. * Query and return the application ID of the SEE app identified by the given
  1874. * name. This returned ID is the unique identifier of the app required for
  1875. * subsequent communication.
  1876. *
  1877. * Return: Zero on success, nonzero on failure, -ENOENT if the app has not been
  1878. * loaded or could not be found.
  1879. */
  1880. int qcom_scm_qseecom_app_get_id(const char *app_name, u32 *app_id)
  1881. {
  1882. unsigned long name_buf_size = QSEECOM_MAX_APP_NAME_SIZE;
  1883. unsigned long app_name_len = strlen(app_name);
  1884. struct qcom_scm_desc desc = {};
  1885. struct qcom_scm_qseecom_resp res = {};
  1886. int status;
  1887. if (app_name_len >= name_buf_size)
  1888. return -EINVAL;
  1889. char *name_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
  1890. name_buf_size,
  1891. GFP_KERNEL);
  1892. if (!name_buf)
  1893. return -ENOMEM;
  1894. memcpy(name_buf, app_name, app_name_len);
  1895. desc.owner = QSEECOM_TZ_OWNER_QSEE_OS;
  1896. desc.svc = QSEECOM_TZ_SVC_APP_MGR;
  1897. desc.cmd = QSEECOM_TZ_CMD_APP_LOOKUP;
  1898. desc.arginfo = QCOM_SCM_ARGS(2, QCOM_SCM_RW, QCOM_SCM_VAL);
  1899. desc.args[0] = qcom_tzmem_to_phys(name_buf);
  1900. desc.args[1] = app_name_len;
  1901. status = qcom_scm_qseecom_call(&desc, &res);
  1902. if (status)
  1903. return status;
  1904. if (res.result == QSEECOM_RESULT_FAILURE)
  1905. return -ENOENT;
  1906. if (res.result != QSEECOM_RESULT_SUCCESS)
  1907. return -EINVAL;
  1908. if (res.resp_type != QSEECOM_SCM_RES_APP_ID)
  1909. return -EINVAL;
  1910. *app_id = res.data;
  1911. return 0;
  1912. }
  1913. EXPORT_SYMBOL_GPL(qcom_scm_qseecom_app_get_id);
  1914. /**
  1915. * qcom_scm_qseecom_app_send() - Send to and receive data from a given QSEE app.
  1916. * @app_id: The ID of the target app.
  1917. * @req: Request buffer sent to the app (must be TZ memory)
  1918. * @req_size: Size of the request buffer.
  1919. * @rsp: Response buffer, written to by the app (must be TZ memory)
  1920. * @rsp_size: Size of the response buffer.
  1921. *
  1922. * Sends a request to the QSEE app associated with the given ID and read back
  1923. * its response. The caller must provide two DMA memory regions, one for the
  1924. * request and one for the response, and fill out the @req region with the
  1925. * respective (app-specific) request data. The QSEE app reads this and returns
  1926. * its response in the @rsp region.
  1927. *
  1928. * Return: Zero on success, nonzero on failure.
  1929. */
  1930. int qcom_scm_qseecom_app_send(u32 app_id, void *req, size_t req_size,
  1931. void *rsp, size_t rsp_size)
  1932. {
  1933. struct qcom_scm_qseecom_resp res = {};
  1934. struct qcom_scm_desc desc = {};
  1935. phys_addr_t req_phys;
  1936. phys_addr_t rsp_phys;
  1937. int status;
  1938. req_phys = qcom_tzmem_to_phys(req);
  1939. rsp_phys = qcom_tzmem_to_phys(rsp);
  1940. desc.owner = QSEECOM_TZ_OWNER_TZ_APPS;
  1941. desc.svc = QSEECOM_TZ_SVC_APP_ID_PLACEHOLDER;
  1942. desc.cmd = QSEECOM_TZ_CMD_APP_SEND;
  1943. desc.arginfo = QCOM_SCM_ARGS(5, QCOM_SCM_VAL,
  1944. QCOM_SCM_RW, QCOM_SCM_VAL,
  1945. QCOM_SCM_RW, QCOM_SCM_VAL);
  1946. desc.args[0] = app_id;
  1947. desc.args[1] = req_phys;
  1948. desc.args[2] = req_size;
  1949. desc.args[3] = rsp_phys;
  1950. desc.args[4] = rsp_size;
  1951. status = qcom_scm_qseecom_call(&desc, &res);
  1952. if (status)
  1953. return status;
  1954. if (res.result != QSEECOM_RESULT_SUCCESS)
  1955. return -EIO;
  1956. return 0;
  1957. }
  1958. EXPORT_SYMBOL_GPL(qcom_scm_qseecom_app_send);
  1959. /*
  1960. * We do not yet support re-entrant calls via the qseecom interface. To prevent
  1961. + any potential issues with this, only allow validated machines for now.
  1962. */
  1963. static const struct of_device_id qcom_scm_qseecom_allowlist[] __maybe_unused = {
  1964. { .compatible = "asus,vivobook-s15" },
  1965. { .compatible = "asus,zenbook-a14-ux3407qa" },
  1966. { .compatible = "asus,zenbook-a14-ux3407ra" },
  1967. { .compatible = "dell,inspiron-14-plus-7441" },
  1968. { .compatible = "dell,latitude-7455" },
  1969. { .compatible = "dell,xps13-9345" },
  1970. { .compatible = "hp,elitebook-ultra-g1q" },
  1971. { .compatible = "hp,omnibook-x14" },
  1972. { .compatible = "huawei,gaokun3" },
  1973. { .compatible = "lenovo,flex-5g" },
  1974. { .compatible = "lenovo,thinkbook-16" },
  1975. { .compatible = "lenovo,thinkpad-t14s" },
  1976. { .compatible = "lenovo,thinkpad-x13s", },
  1977. { .compatible = "lenovo,yoga-slim7x" },
  1978. { .compatible = "medion,sprchrgd14s1" },
  1979. { .compatible = "microsoft,arcata", },
  1980. { .compatible = "microsoft,blackrock" },
  1981. { .compatible = "microsoft,denali", },
  1982. { .compatible = "microsoft,romulus13", },
  1983. { .compatible = "microsoft,romulus15", },
  1984. { .compatible = "qcom,hamoa-iot-evk" },
  1985. { .compatible = "qcom,sc8180x-primus" },
  1986. { .compatible = "qcom,x1e001de-devkit" },
  1987. { .compatible = "qcom,x1e80100-crd" },
  1988. { .compatible = "qcom,x1e80100-qcp" },
  1989. { .compatible = "qcom,x1p42100-crd" },
  1990. { }
  1991. };
  1992. static void qcom_scm_qseecom_free(void *data)
  1993. {
  1994. struct platform_device *qseecom_dev = data;
  1995. platform_device_del(qseecom_dev);
  1996. platform_device_put(qseecom_dev);
  1997. }
  1998. static int qcom_scm_qseecom_init(struct qcom_scm *scm)
  1999. {
  2000. struct platform_device *qseecom_dev;
  2001. u32 version;
  2002. int ret;
  2003. /*
  2004. * Note: We do two steps of validation here: First, we try to query the
  2005. * QSEECOM version as a check to see if the interface exists on this
  2006. * device. Second, we check against known good devices due to current
  2007. * driver limitations (see comment in qcom_scm_qseecom_allowlist).
  2008. *
  2009. * Note that we deliberately do the machine check after the version
  2010. * check so that we can log potentially supported devices. This should
  2011. * be safe as downstream sources indicate that the version query is
  2012. * neither blocking nor reentrant.
  2013. */
  2014. ret = qcom_scm_qseecom_get_version(&version);
  2015. if (ret)
  2016. return 0;
  2017. dev_info(scm->dev, "qseecom: found qseecom with version 0x%x\n", version);
  2018. if (!of_machine_device_match(qcom_scm_qseecom_allowlist)) {
  2019. dev_info(scm->dev, "qseecom: untested machine, skipping\n");
  2020. return 0;
  2021. }
  2022. /*
  2023. * Set up QSEECOM interface device. All application clients will be
  2024. * set up and managed by the corresponding driver for it.
  2025. */
  2026. qseecom_dev = platform_device_alloc("qcom_qseecom", -1);
  2027. if (!qseecom_dev)
  2028. return -ENOMEM;
  2029. qseecom_dev->dev.parent = scm->dev;
  2030. ret = platform_device_add(qseecom_dev);
  2031. if (ret) {
  2032. platform_device_put(qseecom_dev);
  2033. return ret;
  2034. }
  2035. return devm_add_action_or_reset(scm->dev, qcom_scm_qseecom_free, qseecom_dev);
  2036. }
  2037. #else /* CONFIG_QCOM_QSEECOM */
  2038. static int qcom_scm_qseecom_init(struct qcom_scm *scm)
  2039. {
  2040. return 0;
  2041. }
  2042. #endif /* CONFIG_QCOM_QSEECOM */
  2043. /**
  2044. * qcom_scm_qtee_invoke_smc() - Invoke a QTEE object.
  2045. * @inbuf: start address of memory area used for inbound buffer.
  2046. * @inbuf_size: size of the memory area used for inbound buffer.
  2047. * @outbuf: start address of memory area used for outbound buffer.
  2048. * @outbuf_size: size of the memory area used for outbound buffer.
  2049. * @result: result of QTEE object invocation.
  2050. * @response_type: response type returned by QTEE.
  2051. *
  2052. * @response_type determines how the contents of @inbuf and @outbuf
  2053. * should be processed.
  2054. *
  2055. * Return: On success, return 0 or <0 on failure.
  2056. */
  2057. int qcom_scm_qtee_invoke_smc(phys_addr_t inbuf, size_t inbuf_size,
  2058. phys_addr_t outbuf, size_t outbuf_size,
  2059. u64 *result, u64 *response_type)
  2060. {
  2061. struct qcom_scm_desc desc = {
  2062. .svc = QCOM_SCM_SVC_SMCINVOKE,
  2063. .cmd = QCOM_SCM_SMCINVOKE_INVOKE,
  2064. .owner = ARM_SMCCC_OWNER_TRUSTED_OS,
  2065. .args[0] = inbuf,
  2066. .args[1] = inbuf_size,
  2067. .args[2] = outbuf,
  2068. .args[3] = outbuf_size,
  2069. .arginfo = QCOM_SCM_ARGS(4, QCOM_SCM_RW, QCOM_SCM_VAL,
  2070. QCOM_SCM_RW, QCOM_SCM_VAL),
  2071. };
  2072. struct qcom_scm_res res;
  2073. int ret;
  2074. ret = qcom_scm_call(__scm->dev, &desc, &res);
  2075. if (ret)
  2076. return ret;
  2077. if (response_type)
  2078. *response_type = res.result[0];
  2079. if (result)
  2080. *result = res.result[1];
  2081. return 0;
  2082. }
  2083. EXPORT_SYMBOL(qcom_scm_qtee_invoke_smc);
  2084. /**
  2085. * qcom_scm_qtee_callback_response() - Submit response for callback request.
  2086. * @buf: start address of memory area used for outbound buffer.
  2087. * @buf_size: size of the memory area used for outbound buffer.
  2088. * @result: Result of QTEE object invocation.
  2089. * @response_type: Response type returned by QTEE.
  2090. *
  2091. * @response_type determines how the contents of @buf should be processed.
  2092. *
  2093. * Return: On success, return 0 or <0 on failure.
  2094. */
  2095. int qcom_scm_qtee_callback_response(phys_addr_t buf, size_t buf_size,
  2096. u64 *result, u64 *response_type)
  2097. {
  2098. struct qcom_scm_desc desc = {
  2099. .svc = QCOM_SCM_SVC_SMCINVOKE,
  2100. .cmd = QCOM_SCM_SMCINVOKE_CB_RSP,
  2101. .owner = ARM_SMCCC_OWNER_TRUSTED_OS,
  2102. .args[0] = buf,
  2103. .args[1] = buf_size,
  2104. .arginfo = QCOM_SCM_ARGS(2, QCOM_SCM_RW, QCOM_SCM_VAL),
  2105. };
  2106. struct qcom_scm_res res;
  2107. int ret;
  2108. ret = qcom_scm_call(__scm->dev, &desc, &res);
  2109. if (ret)
  2110. return ret;
  2111. if (response_type)
  2112. *response_type = res.result[0];
  2113. if (result)
  2114. *result = res.result[1];
  2115. return 0;
  2116. }
  2117. EXPORT_SYMBOL(qcom_scm_qtee_callback_response);
  2118. static void qcom_scm_qtee_free(void *data)
  2119. {
  2120. struct platform_device *qtee_dev = data;
  2121. platform_device_unregister(qtee_dev);
  2122. }
  2123. static void qcom_scm_qtee_init(struct qcom_scm *scm)
  2124. {
  2125. struct platform_device *qtee_dev;
  2126. u64 result, response_type;
  2127. int ret;
  2128. /*
  2129. * Probe for smcinvoke support. This will fail due to invalid buffers,
  2130. * but first, it checks whether the call is supported in QTEE syscall
  2131. * handler. If it is not supported, -EIO is returned.
  2132. */
  2133. ret = qcom_scm_qtee_invoke_smc(0, 0, 0, 0, &result, &response_type);
  2134. if (ret == -EIO)
  2135. return;
  2136. /* Setup QTEE interface device. */
  2137. qtee_dev = platform_device_register_data(scm->dev, "qcomtee",
  2138. PLATFORM_DEVID_NONE, NULL, 0);
  2139. if (IS_ERR(qtee_dev))
  2140. return;
  2141. devm_add_action_or_reset(scm->dev, qcom_scm_qtee_free, qtee_dev);
  2142. }
  2143. /**
  2144. * qcom_scm_is_available() - Checks if SCM is available
  2145. */
  2146. bool qcom_scm_is_available(void)
  2147. {
  2148. /* Paired with smp_store_release() in qcom_scm_probe */
  2149. return !!smp_load_acquire(&__scm);
  2150. }
  2151. EXPORT_SYMBOL_GPL(qcom_scm_is_available);
  2152. static int qcom_scm_fill_irq_fwspec_params(struct irq_fwspec *fwspec, u32 hwirq)
  2153. {
  2154. if (hwirq >= GIC_SPI_BASE && hwirq <= GIC_MAX_SPI) {
  2155. fwspec->param[0] = GIC_SPI;
  2156. fwspec->param[1] = hwirq - GIC_SPI_BASE;
  2157. } else if (hwirq >= GIC_ESPI_BASE && hwirq <= GIC_MAX_ESPI) {
  2158. fwspec->param[0] = GIC_ESPI;
  2159. fwspec->param[1] = hwirq - GIC_ESPI_BASE;
  2160. } else {
  2161. WARN(1, "Unexpected hwirq: %d\n", hwirq);
  2162. return -ENXIO;
  2163. }
  2164. fwspec->param[2] = IRQ_TYPE_EDGE_RISING;
  2165. fwspec->param_count = 3;
  2166. return 0;
  2167. }
  2168. static int qcom_scm_query_waitq_count(struct qcom_scm *scm)
  2169. {
  2170. struct qcom_scm_desc desc = {
  2171. .svc = QCOM_SCM_SVC_WAITQ,
  2172. .cmd = QCOM_SCM_WAITQ_GET_INFO,
  2173. .owner = ARM_SMCCC_OWNER_SIP
  2174. };
  2175. struct qcom_scm_res res;
  2176. int ret;
  2177. ret = qcom_scm_call_atomic(scm->dev, &desc, &res);
  2178. if (ret)
  2179. return ret;
  2180. return res.result[0] & GENMASK(7, 0);
  2181. }
  2182. static int qcom_scm_get_waitq_irq(struct qcom_scm *scm)
  2183. {
  2184. struct qcom_scm_desc desc = {
  2185. .svc = QCOM_SCM_SVC_WAITQ,
  2186. .cmd = QCOM_SCM_WAITQ_GET_INFO,
  2187. .owner = ARM_SMCCC_OWNER_SIP
  2188. };
  2189. struct device_node *parent_irq_node;
  2190. struct irq_fwspec fwspec;
  2191. struct qcom_scm_res res;
  2192. u32 hwirq;
  2193. int ret;
  2194. ret = qcom_scm_call_atomic(scm->dev, &desc, &res);
  2195. if (ret)
  2196. return ret;
  2197. hwirq = res.result[1] & GENMASK(15, 0);
  2198. ret = qcom_scm_fill_irq_fwspec_params(&fwspec, hwirq);
  2199. if (ret)
  2200. return ret;
  2201. parent_irq_node = of_irq_find_parent(scm->dev->of_node);
  2202. if (!parent_irq_node)
  2203. return -ENODEV;
  2204. fwspec.fwnode = of_fwnode_handle(parent_irq_node);
  2205. return irq_create_fwspec_mapping(&fwspec);
  2206. }
  2207. static struct completion *qcom_scm_get_completion(u32 wq_ctx)
  2208. {
  2209. struct completion *wq;
  2210. if (WARN_ON_ONCE(wq_ctx >= __scm->wq_cnt))
  2211. return ERR_PTR(-EINVAL);
  2212. wq = &__scm->waitq_comps[wq_ctx];
  2213. return wq;
  2214. }
  2215. int qcom_scm_wait_for_wq_completion(u32 wq_ctx)
  2216. {
  2217. struct completion *wq;
  2218. wq = qcom_scm_get_completion(wq_ctx);
  2219. if (IS_ERR(wq))
  2220. return PTR_ERR(wq);
  2221. wait_for_completion_state(wq, TASK_IDLE);
  2222. return 0;
  2223. }
  2224. static int qcom_scm_waitq_wakeup(unsigned int wq_ctx)
  2225. {
  2226. struct completion *wq;
  2227. wq = qcom_scm_get_completion(wq_ctx);
  2228. if (IS_ERR(wq))
  2229. return PTR_ERR(wq);
  2230. complete(wq);
  2231. return 0;
  2232. }
  2233. static irqreturn_t qcom_scm_irq_handler(int irq, void *data)
  2234. {
  2235. int ret;
  2236. struct qcom_scm *scm = data;
  2237. u32 wq_ctx, flags, more_pending = 0;
  2238. do {
  2239. ret = scm_get_wq_ctx(&wq_ctx, &flags, &more_pending);
  2240. if (ret) {
  2241. dev_err(scm->dev, "GET_WQ_CTX SMC call failed: %d\n", ret);
  2242. goto out;
  2243. }
  2244. if (flags != QCOM_SMC_WAITQ_FLAG_WAKE_ONE) {
  2245. dev_err(scm->dev, "Invalid flags received for wq_ctx: %u\n", flags);
  2246. goto out;
  2247. }
  2248. ret = qcom_scm_waitq_wakeup(wq_ctx);
  2249. if (ret)
  2250. goto out;
  2251. } while (more_pending);
  2252. out:
  2253. return IRQ_HANDLED;
  2254. }
  2255. static int get_download_mode(char *buffer, const struct kernel_param *kp)
  2256. {
  2257. if (download_mode >= ARRAY_SIZE(download_mode_name))
  2258. return sysfs_emit(buffer, "unknown mode\n");
  2259. return sysfs_emit(buffer, "%s\n", download_mode_name[download_mode]);
  2260. }
  2261. static int set_download_mode(const char *val, const struct kernel_param *kp)
  2262. {
  2263. bool tmp;
  2264. int ret;
  2265. ret = sysfs_match_string(download_mode_name, val);
  2266. if (ret < 0) {
  2267. ret = kstrtobool(val, &tmp);
  2268. if (ret < 0) {
  2269. pr_err("qcom_scm: err: %d\n", ret);
  2270. return ret;
  2271. }
  2272. ret = tmp ? 1 : 0;
  2273. }
  2274. download_mode = ret;
  2275. if (__scm)
  2276. qcom_scm_set_download_mode(download_mode);
  2277. return 0;
  2278. }
  2279. static const struct kernel_param_ops download_mode_param_ops = {
  2280. .get = get_download_mode,
  2281. .set = set_download_mode,
  2282. };
  2283. module_param_cb(download_mode, &download_mode_param_ops, NULL, 0644);
  2284. 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");
  2285. static int qcom_scm_probe(struct platform_device *pdev)
  2286. {
  2287. struct qcom_tzmem_pool_config pool_config;
  2288. struct qcom_scm *scm;
  2289. int irq, ret;
  2290. int i;
  2291. scm = devm_kzalloc(&pdev->dev, sizeof(*scm), GFP_KERNEL);
  2292. if (!scm)
  2293. return -ENOMEM;
  2294. scm->dev = &pdev->dev;
  2295. ret = qcom_scm_find_dload_address(&pdev->dev, &scm->dload_mode_addr);
  2296. if (ret < 0)
  2297. return ret;
  2298. mutex_init(&scm->scm_bw_lock);
  2299. scm->path = devm_of_icc_get(&pdev->dev, NULL);
  2300. if (IS_ERR(scm->path))
  2301. return dev_err_probe(&pdev->dev, PTR_ERR(scm->path),
  2302. "failed to acquire interconnect path\n");
  2303. scm->core_clk = devm_clk_get_optional(&pdev->dev, "core");
  2304. if (IS_ERR(scm->core_clk))
  2305. return PTR_ERR(scm->core_clk);
  2306. scm->iface_clk = devm_clk_get_optional(&pdev->dev, "iface");
  2307. if (IS_ERR(scm->iface_clk))
  2308. return PTR_ERR(scm->iface_clk);
  2309. scm->bus_clk = devm_clk_get_optional(&pdev->dev, "bus");
  2310. if (IS_ERR(scm->bus_clk))
  2311. return PTR_ERR(scm->bus_clk);
  2312. scm->reset.ops = &qcom_scm_pas_reset_ops;
  2313. scm->reset.nr_resets = 1;
  2314. scm->reset.of_node = pdev->dev.of_node;
  2315. ret = devm_reset_controller_register(&pdev->dev, &scm->reset);
  2316. if (ret)
  2317. return ret;
  2318. /* vote for max clk rate for highest performance */
  2319. ret = clk_set_rate(scm->core_clk, INT_MAX);
  2320. if (ret)
  2321. return ret;
  2322. ret = of_reserved_mem_device_init(scm->dev);
  2323. if (ret && ret != -ENODEV)
  2324. return dev_err_probe(scm->dev, ret,
  2325. "Failed to setup the reserved memory region for TZ mem\n");
  2326. ret = qcom_tzmem_enable(scm->dev);
  2327. if (ret)
  2328. return dev_err_probe(scm->dev, ret,
  2329. "Failed to enable the TrustZone memory allocator\n");
  2330. memset(&pool_config, 0, sizeof(pool_config));
  2331. pool_config.initial_size = 0;
  2332. pool_config.policy = QCOM_TZMEM_POLICY_ON_DEMAND;
  2333. pool_config.max_size = SZ_256K;
  2334. scm->mempool = devm_qcom_tzmem_pool_new(scm->dev, &pool_config);
  2335. if (IS_ERR(scm->mempool))
  2336. return dev_err_probe(scm->dev, PTR_ERR(scm->mempool),
  2337. "Failed to create the SCM memory pool\n");
  2338. ret = qcom_scm_query_waitq_count(scm);
  2339. scm->wq_cnt = ret < 0 ? QCOM_SCM_DEFAULT_WAITQ_COUNT : ret;
  2340. scm->waitq_comps = devm_kcalloc(&pdev->dev, scm->wq_cnt, sizeof(*scm->waitq_comps),
  2341. GFP_KERNEL);
  2342. if (!scm->waitq_comps)
  2343. return -ENOMEM;
  2344. for (i = 0; i < scm->wq_cnt; i++)
  2345. init_completion(&scm->waitq_comps[i]);
  2346. irq = qcom_scm_get_waitq_irq(scm);
  2347. if (irq < 0)
  2348. irq = platform_get_irq_optional(pdev, 0);
  2349. if (irq < 0) {
  2350. if (irq != -ENXIO)
  2351. return irq;
  2352. } else {
  2353. ret = devm_request_threaded_irq(scm->dev, irq, NULL, qcom_scm_irq_handler,
  2354. IRQF_ONESHOT, "qcom-scm", scm);
  2355. if (ret < 0)
  2356. return dev_err_probe(scm->dev, ret,
  2357. "Failed to request qcom-scm irq\n");
  2358. }
  2359. /*
  2360. * Paired with smp_load_acquire() in qcom_scm_is_available().
  2361. *
  2362. * This marks the SCM API as ready to accept user calls and can only
  2363. * be called after the TrustZone memory pool is initialized and the
  2364. * waitqueue interrupt requested.
  2365. */
  2366. smp_store_release(&__scm, scm);
  2367. __get_convention();
  2368. /*
  2369. * If "download mode" is requested, from this point on warmboot
  2370. * will cause the boot stages to enter download mode, unless
  2371. * disabled below by a clean shutdown/reboot.
  2372. */
  2373. qcom_scm_set_download_mode(download_mode);
  2374. /*
  2375. * Disable SDI if indicated by DT that it is enabled by default.
  2376. */
  2377. if (of_property_read_bool(pdev->dev.of_node, "qcom,sdi-enabled") || !download_mode)
  2378. qcom_scm_disable_sdi();
  2379. /*
  2380. * Initialize the QSEECOM interface.
  2381. *
  2382. * Note: QSEECOM is fairly self-contained and this only adds the
  2383. * interface device (the driver of which does most of the heavy
  2384. * lifting). So any errors returned here should be either -ENOMEM or
  2385. * -EINVAL (with the latter only in case there's a bug in our code).
  2386. * This means that there is no need to bring down the whole SCM driver.
  2387. * Just log the error instead and let SCM live.
  2388. */
  2389. ret = qcom_scm_qseecom_init(scm);
  2390. WARN(ret < 0, "failed to initialize qseecom: %d\n", ret);
  2391. /* Initialize the QTEE object interface. */
  2392. qcom_scm_qtee_init(scm);
  2393. return 0;
  2394. }
  2395. static void qcom_scm_shutdown(struct platform_device *pdev)
  2396. {
  2397. /* Clean shutdown, disable download mode to allow normal restart */
  2398. qcom_scm_set_download_mode(QCOM_DLOAD_NODUMP);
  2399. }
  2400. static const struct of_device_id qcom_scm_dt_match[] = {
  2401. { .compatible = "qcom,scm" },
  2402. /* Legacy entries kept for backwards compatibility */
  2403. { .compatible = "qcom,scm-apq8064" },
  2404. { .compatible = "qcom,scm-apq8084" },
  2405. { .compatible = "qcom,scm-ipq4019" },
  2406. { .compatible = "qcom,scm-msm8953" },
  2407. { .compatible = "qcom,scm-msm8974" },
  2408. { .compatible = "qcom,scm-msm8996" },
  2409. {}
  2410. };
  2411. MODULE_DEVICE_TABLE(of, qcom_scm_dt_match);
  2412. static struct platform_driver qcom_scm_driver = {
  2413. .driver = {
  2414. .name = "qcom_scm",
  2415. .of_match_table = qcom_scm_dt_match,
  2416. .suppress_bind_attrs = true,
  2417. },
  2418. .probe = qcom_scm_probe,
  2419. .shutdown = qcom_scm_shutdown,
  2420. };
  2421. static int __init qcom_scm_init(void)
  2422. {
  2423. return platform_driver_register(&qcom_scm_driver);
  2424. }
  2425. subsys_initcall(qcom_scm_init);
  2426. MODULE_DESCRIPTION("Qualcomm Technologies, Inc. SCM driver");
  2427. MODULE_LICENSE("GPL v2");