gh_rm_iface.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. *
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/limits.h>
  9. #include <linux/module.h>
  10. #include <linux/vmalloc.h>
  11. #include <linux/gunyah/gh_msgq.h>
  12. #include <linux/gunyah/gh_common.h>
  13. #include <linux/mm.h>
  14. #define CREATE_TRACE_POINTS
  15. #include "gh_rm_drv_private.h"
  16. #include <trace/events/gunyah.h>
  17. #define GH_RM_MEM_RELEASE_VALID_FLAGS GH_RM_MEM_RELEASE_CLEAR
  18. #define GH_RM_MEM_RECLAIM_VALID_FLAGS GH_RM_MEM_RECLAIM_CLEAR
  19. #define GH_RM_MEM_ACCEPT_VALID_FLAGS\
  20. (GH_RM_MEM_ACCEPT_VALIDATE_SANITIZED |\
  21. GH_RM_MEM_ACCEPT_VALIDATE_ACL_ATTRS |\
  22. GH_RM_MEM_ACCEPT_VALIDATE_LABEL |\
  23. GH_RM_MEM_ACCEPT_MAP_IPA_CONTIGUOUS |\
  24. GH_RM_MEM_ACCEPT_DONE)
  25. #define GH_RM_MEM_SHARE_VALID_FLAGS GH_RM_MEM_SHARE_SANITIZE
  26. #define GH_RM_MEM_LEND_VALID_FLAGS GH_RM_MEM_LEND_SANITIZE
  27. #define GH_RM_MEM_DONATE_VALID_FLAGS GH_RM_MEM_DONATE_SANITIZE
  28. #define GH_RM_MEM_NOTIFY_VALID_FLAGS\
  29. (GH_RM_MEM_NOTIFY_RECIPIENT_SHARED |\
  30. GH_RM_MEM_NOTIFY_OWNER_RELEASED | GH_RM_MEM_NOTIFY_OWNER_ACCEPTED)
  31. #define GH_RM_MEM_APPEND_VALID_FLAGS GH_RM_MEM_APPEND_END
  32. /* Maximum number of sgl entries supported by lend/share/donate/append/notify calls */
  33. #define GH_RM_MEM_MAX_SGL_ENTRIES 512
  34. static DEFINE_SPINLOCK(gh_vm_table_lock);
  35. static struct gh_vm_property gh_vm_table[GH_VM_MAX];
  36. void gh_init_vm_prop_table(void)
  37. {
  38. size_t vm_name;
  39. spin_lock(&gh_vm_table_lock);
  40. gh_vm_table[GH_SELF_VM].vmid = 0;
  41. for (vm_name = GH_SELF_VM + 1; vm_name < GH_VM_MAX; vm_name++) {
  42. gh_vm_table[vm_name].vmid = GH_VMID_INVAL;
  43. gh_vm_table[vm_name].guid = NULL;
  44. gh_vm_table[vm_name].uri = NULL;
  45. gh_vm_table[vm_name].name = NULL;
  46. gh_vm_table[vm_name].sign_auth = NULL;
  47. }
  48. spin_unlock(&gh_vm_table_lock);
  49. }
  50. int gh_update_vm_prop_table(enum gh_vm_names vm_name,
  51. struct gh_vm_property *vm_prop)
  52. {
  53. if (!vm_prop)
  54. return -EINVAL;
  55. if (vm_prop->vmid < 0 || vm_name < GH_SELF_VM || vm_name > GH_VM_MAX)
  56. return -EINVAL;
  57. spin_lock(&gh_vm_table_lock);
  58. if (gh_vm_table[vm_name].guid || gh_vm_table[vm_name].uri ||
  59. gh_vm_table[vm_name].name || gh_vm_table[vm_name].sign_auth) {
  60. spin_unlock(&gh_vm_table_lock);
  61. return -EEXIST;
  62. }
  63. if (vm_prop->vmid)
  64. gh_vm_table[vm_name].vmid = vm_prop->vmid;
  65. if (vm_prop->guid)
  66. gh_vm_table[vm_name].guid = vm_prop->guid;
  67. if (vm_prop->uri)
  68. gh_vm_table[vm_name].uri = vm_prop->uri;
  69. if (vm_prop->name)
  70. gh_vm_table[vm_name].name = vm_prop->name;
  71. if (vm_prop->sign_auth)
  72. gh_vm_table[vm_name].sign_auth = vm_prop->sign_auth;
  73. spin_unlock(&gh_vm_table_lock);
  74. return 0;
  75. }
  76. void gh_reset_vm_prop_table_entry(gh_vmid_t vmid)
  77. {
  78. size_t vm_name;
  79. spin_lock(&gh_vm_table_lock);
  80. for (vm_name = GH_SELF_VM + 1; vm_name < GH_VM_MAX; vm_name++) {
  81. if (vmid == gh_vm_table[vm_name].vmid) {
  82. gh_vm_table[vm_name].vmid = GH_VMID_INVAL;
  83. gh_vm_table[vm_name].uri = NULL;
  84. gh_vm_table[vm_name].guid = NULL;
  85. gh_vm_table[vm_name].name = NULL;
  86. gh_vm_table[vm_name].sign_auth = NULL;
  87. break;
  88. }
  89. }
  90. spin_unlock(&gh_vm_table_lock);
  91. }
  92. /**
  93. * ghd_rm_get_vmid: Translate VM name to vmid
  94. * @vm_name: VM name to lookup
  95. * @vmid: out pointer to store found vmid if VM is ofund
  96. *
  97. * If gh_rm_core has not yet probed, returns -EPROBE_DEFER.
  98. * If no VM is known to RM with the supplied name, returns -EINVAL.
  99. * Returns 0 on success.
  100. */
  101. int ghd_rm_get_vmid(enum gh_vm_names vm_name, gh_vmid_t *vmid)
  102. {
  103. gh_vmid_t _vmid;
  104. int ret = 0;
  105. if (vm_name < GH_SELF_VM || vm_name > GH_VM_MAX)
  106. return -EINVAL;
  107. spin_lock(&gh_vm_table_lock);
  108. _vmid = gh_vm_table[vm_name].vmid;
  109. if (!gh_rm_core_initialized) {
  110. ret = -EPROBE_DEFER;
  111. goto out;
  112. }
  113. if (!_vmid && vm_name != GH_SELF_VM) {
  114. ret = -EINVAL;
  115. goto out;
  116. }
  117. if (vmid)
  118. *vmid = _vmid;
  119. out:
  120. spin_unlock(&gh_vm_table_lock);
  121. return ret;
  122. }
  123. EXPORT_SYMBOL(ghd_rm_get_vmid);
  124. /**
  125. * gh_rm_get_vm_name: Translate vmid to vm name
  126. * @vmid: vmid to lookup
  127. * @vm_name: out pointer to store found VM name if vmid is found
  128. *
  129. * If no VM is known to RM with the supplied VMID, -EINVAL is returned.
  130. * 0 otherwise.
  131. */
  132. int gh_rm_get_vm_name(gh_vmid_t vmid, enum gh_vm_names *vm_name)
  133. {
  134. enum gh_vm_names i;
  135. spin_lock(&gh_vm_table_lock);
  136. for (i = 0; i < GH_VM_MAX; i++)
  137. if (gh_vm_table[i].vmid == vmid) {
  138. if (vm_name)
  139. *vm_name = i;
  140. spin_unlock(&gh_vm_table_lock);
  141. return 0;
  142. }
  143. spin_unlock(&gh_vm_table_lock);
  144. return -EINVAL;
  145. }
  146. EXPORT_SYMBOL(gh_rm_get_vm_name);
  147. /**
  148. * gh_rm_get_vminfo: Obtain Vm related info with vm name
  149. * @vm_name: VM name to lookup
  150. * @vm: out pointer to store id information about VM
  151. *
  152. * If no VM is known to RM with the supplied name, -EINVAL is returned.
  153. * 0 otherwise.
  154. */
  155. int gh_rm_get_vminfo(enum gh_vm_names vm_name, struct gh_vminfo *vm)
  156. {
  157. if (!vm)
  158. return -EINVAL;
  159. spin_lock(&gh_vm_table_lock);
  160. if (vm_name < GH_SELF_VM || vm_name > GH_VM_MAX) {
  161. spin_unlock(&gh_vm_table_lock);
  162. return -EINVAL;
  163. }
  164. vm->guid = gh_vm_table[vm_name].guid;
  165. vm->uri = gh_vm_table[vm_name].uri;
  166. vm->name = gh_vm_table[vm_name].name;
  167. vm->sign_auth = gh_vm_table[vm_name].sign_auth;
  168. spin_unlock(&gh_vm_table_lock);
  169. return 0;
  170. }
  171. EXPORT_SYMBOL(gh_rm_get_vminfo);
  172. /**
  173. * gh_rm_vm_get_id: Get identification info about a VM
  174. * @vmid: vmid whose info is needed. Pass 0 for self
  175. * @n_entries: The number of the resource entries that's returned to the caller
  176. *
  177. * The function returns an array of type 'struct gh_vm_get_id_resp_entry',
  178. * in which each entry specifies identification info about the vm. The number
  179. * of entries in the array is returned by 'n_entries'. The caller must kfree
  180. * the returned pointer when done.
  181. *
  182. * The function encodes the error codes via ERR_PTR. Hence, the caller is
  183. * responsible to check it with IS_ERR_OR_NULL().
  184. */
  185. struct gh_vm_get_id_resp_entry *
  186. gh_rm_vm_get_id(gh_vmid_t vmid, u32 *n_entries)
  187. {
  188. struct gh_vm_get_id_resp_payload *resp_payload;
  189. struct gh_vm_get_id_req_payload req_payload = {
  190. .vmid = vmid
  191. };
  192. struct gh_vm_get_id_resp_entry *resp_entries, *temp_entry;
  193. size_t resp_payload_size, resp_entries_size = 0;
  194. int ret, i;
  195. if (!n_entries)
  196. return ERR_PTR(-EINVAL);
  197. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_GET_ID,
  198. &req_payload, sizeof(req_payload),
  199. (void **)&resp_payload, &resp_payload_size);
  200. if (ret) {
  201. pr_err("%s: GET_ID failed with err: %d\n",
  202. __func__, ret);
  203. return ERR_PTR(ret);
  204. }
  205. if (resp_payload_size < sizeof(*resp_payload)) {
  206. if (resp_payload_size)
  207. kfree(resp_payload);
  208. return ERR_PTR(-EBADMSG);
  209. }
  210. /* The response payload should contain all the resource entries */
  211. temp_entry = resp_payload->resp_entries;
  212. for (i = 0; i < resp_payload->n_id_entries; i++) {
  213. resp_entries_size +=
  214. sizeof(*temp_entry) + round_up(temp_entry->id_size, 4);
  215. temp_entry = (void *)temp_entry + sizeof(*temp_entry) +
  216. round_up(temp_entry->id_size, 4);
  217. }
  218. if (resp_entries_size != resp_payload_size - sizeof(*n_entries)) {
  219. pr_err("%s: Invalid size received for GET_ID: %u expect %u\n",
  220. __func__, resp_payload_size, resp_entries_size);
  221. resp_entries = ERR_PTR(-EINVAL);
  222. goto out;
  223. }
  224. resp_entries = kmemdup(resp_payload->resp_entries, resp_entries_size,
  225. GFP_KERNEL);
  226. if (!resp_entries) {
  227. resp_entries = ERR_PTR(-ENOMEM);
  228. goto out;
  229. }
  230. *n_entries = resp_payload->n_id_entries;
  231. out:
  232. kfree(resp_payload);
  233. return resp_entries;
  234. }
  235. static int gh_rm_vm_lookup_name_uri(gh_rm_msgid_t msg_id, const char *data,
  236. size_t size, gh_vmid_t *vmid)
  237. {
  238. struct gh_vm_lookup_resp_payload *resp_payload;
  239. struct gh_vm_lookup_char_req_payload *req_payload;
  240. size_t resp_payload_size, req_payload_size;
  241. int ret = 0;
  242. if (!data || !vmid)
  243. return -EINVAL;
  244. req_payload_size = sizeof(*req_payload) + round_up(size, 4);
  245. req_payload = kzalloc(req_payload_size, GFP_KERNEL);
  246. if (!req_payload)
  247. return -ENOMEM;
  248. req_payload->size = size;
  249. memcpy(req_payload->data, data, size);
  250. ret = gh_rm_call(rm, msg_id, req_payload, req_payload_size,
  251. (void **)&resp_payload, &resp_payload_size);
  252. if (ret) {
  253. pr_err("%s: lookup name/uri failed with err: %d\n", __func__, ret);
  254. goto out;
  255. }
  256. if (resp_payload_size < sizeof(*resp_payload) ||
  257. resp_payload_size != struct_size(resp_payload,
  258. resp_entries, resp_payload->n_id_entries)) {
  259. if (resp_payload_size)
  260. kfree(resp_payload);
  261. ret = -EBADMSG;
  262. goto out;
  263. }
  264. if (resp_payload->n_id_entries == 1) {
  265. *vmid = resp_payload->resp_entries->vmid;
  266. } else if (resp_payload->n_id_entries == 0) {
  267. pr_err("%s: No VMID found from lookup %s\n", __func__, data);
  268. ret = -EINVAL;
  269. } else {
  270. pr_err("%s: More than one VMID received from lookup %s\n",
  271. __func__, data);
  272. ret = -EINVAL;
  273. }
  274. kfree(resp_payload);
  275. out:
  276. kfree(req_payload);
  277. return ret;
  278. }
  279. static int gh_rm_vm_lookup_guid(const u8 *data, gh_vmid_t *vmid)
  280. {
  281. struct gh_vm_lookup_resp_payload *resp_payload;
  282. size_t resp_payload_size;
  283. int ret = 0;
  284. if (!data || !vmid)
  285. return -EINVAL;
  286. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_LOOKUP_GUID, (void *)data,
  287. 16, (void **)&resp_payload, &resp_payload_size);
  288. if (ret) {
  289. pr_err("%s: lookup guid failed with err: %d\n", __func__, ret);
  290. return ret;
  291. }
  292. if (resp_payload_size < sizeof(*resp_payload) ||
  293. resp_payload_size != struct_size(resp_payload,
  294. resp_entries, resp_payload->n_id_entries)) {
  295. if (resp_payload_size)
  296. kfree(resp_payload);
  297. return -EBADMSG;
  298. }
  299. if (resp_payload->n_id_entries == 1) {
  300. *vmid = resp_payload->resp_entries->vmid;
  301. } else if (resp_payload->n_id_entries == 0) {
  302. pr_err("%s: No VMID found from lookup %pUB\n", __func__, data);
  303. ret = -EINVAL;
  304. } else {
  305. pr_err("%s: More than one VMID received from lookup: %pUB\n",
  306. __func__, data);
  307. ret = -EINVAL;
  308. }
  309. kfree(resp_payload);
  310. return ret;
  311. }
  312. /**
  313. * gh_rm_vm_lookup: Get vmid from name
  314. * @type: which type of property need to lookup
  315. * @data: name/uri/guid whose vmid is needed
  316. * @size: data size
  317. * @vmid: vmid return to caller
  318. *
  319. */
  320. int gh_rm_vm_lookup(enum gh_vm_lookup_type type, const void *data, size_t size,
  321. gh_vmid_t *vmid)
  322. {
  323. int ret = 0;
  324. switch (type) {
  325. case GH_VM_LOOKUP_NAME:
  326. ret = gh_rm_vm_lookup_name_uri(
  327. GH_RM_RPC_MSG_ID_CALL_VM_LOOKUP_NAME,
  328. (const char *)data, size, vmid);
  329. break;
  330. case GH_VM_LOOKUP_URI:
  331. ret = gh_rm_vm_lookup_name_uri(
  332. GH_RM_RPC_MSG_ID_CALL_VM_LOOKUP_URI, (const char *)data,
  333. size, vmid);
  334. break;
  335. case GH_VM_LOOKUP_GUID:
  336. if (size != 16) {
  337. pr_err("Invalid GUID size=%d\n", size);
  338. ret = -EINVAL;
  339. } else
  340. ret = gh_rm_vm_lookup_guid((const u8 *)data, vmid);
  341. break;
  342. default:
  343. pr_err("Invalid lookup type=%d\n", type);
  344. break;
  345. }
  346. return ret;
  347. }
  348. /**
  349. * gh_rm_get_this_vmid() - Retrieve VMID of this virtual machine
  350. * @vmid: Filled with the VMID of this VM
  351. */
  352. int gh_rm_get_this_vmid(gh_vmid_t *vmid)
  353. {
  354. static gh_vmid_t cached_vmid = GH_VMID_INVAL;
  355. size_t resp_size;
  356. __le32 *resp;
  357. int ret;
  358. if (cached_vmid != GH_VMID_INVAL) {
  359. *vmid = cached_vmid;
  360. return 0;
  361. }
  362. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_GET_VMID, NULL, 0,
  363. (void **)&resp, &resp_size);
  364. if (ret) {
  365. pr_err("%s: failed with err: %d\n", __func__, ret);
  366. return ret;
  367. }
  368. if (resp_size != sizeof(*resp)) {
  369. if (resp_size)
  370. kfree(resp);
  371. return -EBADMSG;
  372. }
  373. *vmid = cached_vmid = lower_16_bits(le32_to_cpu(*resp));
  374. kfree(resp);
  375. return 0;
  376. }
  377. EXPORT_SYMBOL(gh_rm_get_this_vmid);
  378. /**
  379. * gh_rm_vm_get_status: Get the status of a particular VM
  380. * @vmid: The vmid of tehe VM. Pass 0 for self.
  381. *
  382. * The function returns a pointer to gh_vm_status containing
  383. * the status of the VM for the requested vmid. The caller
  384. * must kfree the memory when done reading the contents.
  385. *
  386. * The function encodes the error codes via ERR_PTR. Hence, the
  387. * caller is responsible to check it with IS_ERR_OR_NULL().
  388. */
  389. struct gh_vm_status *gh_rm_vm_get_status(gh_vmid_t vmid)
  390. {
  391. struct gh_vm_get_state_req_payload req_payload = {
  392. .vmid = vmid,
  393. };
  394. struct gh_vm_get_state_resp_payload *resp_payload;
  395. struct gh_vm_status *gh_vm_status;
  396. int err = 0;
  397. size_t resp_payload_size;
  398. err = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_GET_STATE,
  399. &req_payload, sizeof(req_payload),
  400. (void **)&resp_payload, &resp_payload_size);
  401. if (err) {
  402. pr_err("%s: Failed to call VM_GET_STATE: %d\n",
  403. __func__, err);
  404. if (resp_payload) {
  405. gh_vm_status = ERR_PTR(err);
  406. goto out;
  407. }
  408. return ERR_PTR(err);
  409. }
  410. if (resp_payload_size != sizeof(*resp_payload)) {
  411. pr_err("%s: Invalid size received for VM_GET_STATE: %u\n",
  412. __func__, resp_payload_size);
  413. gh_vm_status = ERR_PTR(-EINVAL);
  414. goto out;
  415. }
  416. gh_vm_status = kmemdup(resp_payload, resp_payload_size, GFP_KERNEL);
  417. if (!gh_vm_status)
  418. gh_vm_status = ERR_PTR(-ENOMEM);
  419. out:
  420. if (resp_payload_size)
  421. kfree(resp_payload);
  422. return gh_vm_status;
  423. }
  424. EXPORT_SYMBOL(gh_rm_vm_get_status);
  425. /**
  426. * gh_rm_vm_set_status: Set the status of this VM
  427. * @gh_vm_status: The status to set
  428. *
  429. * The function sets this VM's status as per gh_vm_status.
  430. * It returns 0 upon success and a negative error code
  431. * upon failure.
  432. */
  433. int gh_rm_vm_set_status(struct gh_vm_status gh_vm_status)
  434. {
  435. struct gh_vm_set_status_req_payload req_payload = {
  436. .vm_status = gh_vm_status.vm_status,
  437. .os_status = gh_vm_status.os_status,
  438. .app_status = gh_vm_status.app_status,
  439. };
  440. size_t resp_payload_size;
  441. int ret = 0;
  442. void *resp;
  443. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_SET_STATUS,
  444. &req_payload, sizeof(req_payload),
  445. &resp, &resp_payload_size);
  446. if (ret) {
  447. pr_err("%s: VM_SET_STATUS returned error: %d\n",
  448. __func__, ret);
  449. return ret;
  450. }
  451. if (resp_payload_size) {
  452. pr_err("%s: Invalid size received for VM_SET_STATUS: %u\n",
  453. __func__, resp_payload_size);
  454. kfree(resp);
  455. return -EINVAL;
  456. }
  457. return 0;
  458. }
  459. EXPORT_SYMBOL(gh_rm_vm_set_status);
  460. /**
  461. * gh_rm_vm_set_vm_status: Set the vm status
  462. * @vm_status: The vm_status to set
  463. *
  464. * The function returns 0 on success and a negative
  465. * error code upon failure.
  466. */
  467. int gh_rm_vm_set_vm_status(u8 vm_status)
  468. {
  469. int ret = 0;
  470. struct gh_vm_status *gh_vm_status;
  471. gh_vm_status = gh_rm_vm_get_status(0);
  472. if (IS_ERR_OR_NULL(gh_vm_status))
  473. return PTR_ERR(gh_vm_status);
  474. gh_vm_status->vm_status = vm_status;
  475. ret = gh_rm_vm_set_status(*gh_vm_status);
  476. kfree(gh_vm_status);
  477. return ret;
  478. }
  479. EXPORT_SYMBOL(gh_rm_vm_set_vm_status);
  480. /**
  481. * gh_rm_vm_set_os_status: Set the OS status. Once the VM starts booting,
  482. * there are various stages during the boot up that status of the
  483. * Operating System can be set like early_boot, boot, init, run etc.
  484. * @os_status: The os_status to set
  485. *
  486. * The function returns 0 on success and a negative
  487. * error code upon failure.
  488. */
  489. int gh_rm_vm_set_os_status(u8 os_status)
  490. {
  491. int ret = 0;
  492. struct gh_vm_status *gh_vm_status;
  493. gh_vm_status = gh_rm_vm_get_status(0);
  494. if (IS_ERR_OR_NULL(gh_vm_status))
  495. return PTR_ERR(gh_vm_status);
  496. gh_vm_status->os_status = os_status;
  497. ret = gh_rm_vm_set_status(*gh_vm_status);
  498. kfree(gh_vm_status);
  499. return ret;
  500. }
  501. EXPORT_SYMBOL(gh_rm_vm_set_os_status);
  502. /**
  503. * gh_rm_vm_set_app_status: Set the app status
  504. * @app_status: The app_status to set
  505. *
  506. * The function returns 0 on success and a negative
  507. * error code upon failure.
  508. */
  509. int gh_rm_vm_set_app_status(u16 app_status)
  510. {
  511. int ret = 0;
  512. struct gh_vm_status *gh_vm_status;
  513. gh_vm_status = gh_rm_vm_get_status(0);
  514. if (IS_ERR_OR_NULL(gh_vm_status))
  515. return PTR_ERR(gh_vm_status);
  516. gh_vm_status->app_status = app_status;
  517. ret = gh_rm_vm_set_status(*gh_vm_status);
  518. kfree(gh_vm_status);
  519. return ret;
  520. }
  521. EXPORT_SYMBOL(gh_rm_vm_set_app_status);
  522. /**
  523. * gh_rm_vm_get_hyp_res: Get info about a series of resources for this VM
  524. * @vmid: vmid whose info is needed. Pass 0 for self
  525. * @n_entries: The number of the resource entries that's returned to the caller
  526. *
  527. * The function returns an array of type 'struct gh_vm_get_hyp_res_resp_entry',
  528. * in which each entry specifies info about a particular resource. The number
  529. * of entries in the array is returned by 'n_entries'. The caller must kfree
  530. * the returned pointer when done.
  531. *
  532. * The function encodes the error codes via ERR_PTR. Hence, the caller is
  533. * responsible to check it with IS_ERR_OR_NULL().
  534. */
  535. struct gh_vm_get_hyp_res_resp_entry *
  536. gh_rm_vm_get_hyp_res(gh_vmid_t vmid, u32 *n_entries)
  537. {
  538. struct gh_vm_get_hyp_res_resp_payload *resp_payload;
  539. struct gh_vm_get_hyp_res_req_payload req_payload = {
  540. .vmid = vmid
  541. };
  542. struct gh_vm_get_hyp_res_resp_entry *resp_entries;
  543. size_t resp_payload_size, resp_entries_size;
  544. int err;
  545. if (!n_entries)
  546. return ERR_PTR(-EINVAL);
  547. err = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_GET_HYP_RESOURCES,
  548. &req_payload, sizeof(req_payload),
  549. (void **)&resp_payload, &resp_payload_size);
  550. if (err) {
  551. pr_err("%s: GET_HYP_RESOURCES failed with err: %d\n",
  552. __func__, err);
  553. return ERR_PTR(err);
  554. }
  555. /* The response payload should contain all the resource entries */
  556. if (resp_payload_size < sizeof(*n_entries) ||
  557. (sizeof(*resp_entries) &&
  558. (resp_payload->n_resource_entries > U32_MAX / sizeof(*resp_entries))) ||
  559. (sizeof(*n_entries) > (U32_MAX -
  560. (resp_payload->n_resource_entries * sizeof(*resp_entries)))) ||
  561. resp_payload_size != sizeof(*n_entries) +
  562. (resp_payload->n_resource_entries * sizeof(*resp_entries))) {
  563. pr_err("%s: Invalid size received for GET_HYP_RESOURCES: %u\n",
  564. __func__, resp_payload_size);
  565. resp_entries = ERR_PTR(-EINVAL);
  566. goto out;
  567. }
  568. resp_entries_size = sizeof(*resp_entries) *
  569. resp_payload->n_resource_entries;
  570. resp_entries = kmemdup(resp_payload->resp_entries, resp_entries_size,
  571. GFP_KERNEL);
  572. if (!resp_entries) {
  573. resp_entries = ERR_PTR(-ENOMEM);
  574. goto out;
  575. }
  576. *n_entries = resp_payload->n_resource_entries;
  577. out:
  578. kfree(resp_payload);
  579. return resp_entries;
  580. }
  581. EXPORT_SYMBOL(gh_rm_vm_get_hyp_res);
  582. /**
  583. * gh_rm_vm_irq_notify: Notify an IRQ to another VM
  584. * @vmids: VMs to notify the handle about
  585. * @num_vmids: number of VMs to notify the handle about
  586. * @flags: notification reason
  587. * @virq_handle: Response handle which RM will accept from the other VM to take
  588. * the lent interrupt
  589. */
  590. static int gh_rm_vm_irq_notify(const gh_vmid_t *vmids, unsigned int num_vmids,
  591. u16 flags, gh_virq_handle_t virq_handle)
  592. {
  593. void *resp;
  594. struct gh_vm_irq_notify_req_payload *req_payload;
  595. size_t resp_payload_size, req_payload_size;
  596. int ret = 0;
  597. unsigned int i;
  598. if (!(flags & GH_VM_IRQ_NOTIFY_FLAGS_LENT) && num_vmids)
  599. return -EINVAL;
  600. if (num_vmids > U16_MAX)
  601. return -EINVAL;
  602. req_payload_size = sizeof(*req_payload);
  603. if (flags & GH_VM_IRQ_NOTIFY_FLAGS_LENT)
  604. req_payload_size += sizeof(*(req_payload->optional)) +
  605. (sizeof(req_payload->optional->vmids[0]) * num_vmids);
  606. req_payload = kzalloc(req_payload_size, GFP_KERNEL);
  607. if (!req_payload)
  608. return -ENOMEM;
  609. req_payload->virq = virq_handle;
  610. req_payload->flags = flags;
  611. if (flags & GH_VM_IRQ_NOTIFY_FLAGS_LENT) {
  612. req_payload->optional[0].num_vmids = num_vmids;
  613. for (i = 0; i < num_vmids; i++)
  614. req_payload->optional[0].vmids[i].vmid = vmids[i];
  615. }
  616. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_IRQ_NOTIFY,
  617. req_payload, req_payload_size,
  618. &resp, &resp_payload_size);
  619. kfree(req_payload);
  620. if (ret) {
  621. pr_err("%s: IRQ_NOTIFY returned error: %d\n", __func__,
  622. ret);
  623. return ret;
  624. }
  625. if (resp_payload_size) {
  626. pr_err("%s: Invalid size received for IRQ_NOTIFY: %u\n",
  627. __func__, resp_payload_size);
  628. ret = -EINVAL;
  629. kfree(resp);
  630. }
  631. return ret;
  632. }
  633. /**
  634. * gh_rm_vm_irq_lend: Lend an IRQ to another VM
  635. * @vmid: VM to lend the interrupt to
  636. * @virq: Virtual IRQ number to lend
  637. * @label: Label to give to VM so it may know how to associate the interrupt
  638. * @virq_handle: Response handle which RM will accept from the other VM to take
  639. * the lent interrupt
  640. */
  641. int gh_rm_vm_irq_lend(gh_vmid_t vmid, int virq, int label,
  642. gh_virq_handle_t *virq_handle)
  643. {
  644. struct gh_vm_irq_lend_resp_payload *resp_payload;
  645. struct gh_vm_irq_lend_req_payload req_payload = {0};
  646. size_t resp_payload_size;
  647. int ret = 0;
  648. req_payload.vmid = vmid;
  649. req_payload.virq = virq;
  650. req_payload.label = label;
  651. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_IRQ_LEND,
  652. &req_payload, sizeof(req_payload),
  653. (void **)&resp_payload, &resp_payload_size);
  654. if (ret) {
  655. pr_err("%s: VM_IRQ_LEND failed with err: %d\n",
  656. __func__, ret);
  657. return ret;
  658. }
  659. if (resp_payload_size != sizeof(*resp_payload)) {
  660. pr_err("%s: Invalid size received for VM_IRQ_LEND: %u\n",
  661. __func__, resp_payload_size);
  662. if (resp_payload_size)
  663. kfree(resp_payload);
  664. return -EINVAL;
  665. }
  666. if (virq_handle)
  667. *virq_handle = resp_payload->virq;
  668. kfree(resp_payload);
  669. return ret;
  670. }
  671. EXPORT_SYMBOL(gh_rm_vm_irq_lend);
  672. /**
  673. * gh_rm_vm_irq_lend_notify: Lend an IRQ to a VM and notify the VM about it
  674. * @vmid: VM to lend interrupt to
  675. * @virq: Virtual IRQ number to lend
  676. * @label: Label to give to VM so it may know how to associate the interrupt
  677. * @virq_handle: vIRQ handle generated by hypervisor to reperesent the interrupt
  678. * which can be used later to know when the interrupt has been
  679. * released
  680. *
  681. * This function performs interrupt sharing flow for "HLOS" described in
  682. * Resource Manager High Level Design Sec. 3.3.3.
  683. */
  684. int gh_rm_vm_irq_lend_notify(gh_vmid_t vmid, gh_virq_handle_t virq_handle)
  685. {
  686. return gh_rm_vm_irq_notify(&vmid, 1, GH_VM_IRQ_NOTIFY_FLAGS_LENT,
  687. virq_handle);
  688. }
  689. EXPORT_SYMBOL(gh_rm_vm_irq_lend_notify);
  690. /**
  691. * gh_rm_vm_irq_release: Return a lent IRQ
  692. * @virq_handle: IRQ handle to be released
  693. */
  694. int gh_rm_vm_irq_release(gh_virq_handle_t virq_handle)
  695. {
  696. struct gh_vm_irq_release_req_payload req_payload = {0};
  697. void *resp;
  698. int ret = 0;
  699. size_t resp_payload_size;
  700. req_payload.virq_handle = virq_handle;
  701. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_IRQ_RELEASE,
  702. &req_payload, sizeof(req_payload),
  703. &resp, &resp_payload_size);
  704. if (ret) {
  705. pr_err("%s: IRQ_RELEASE returned error: %d\n", __func__,
  706. ret);
  707. return ret;
  708. }
  709. if (resp_payload_size) {
  710. pr_err("%s: Invalid size received for IRQ_RELEASE: %u\n",
  711. __func__, resp_payload_size);
  712. kfree(resp);
  713. ret = -EINVAL;
  714. }
  715. return ret;
  716. }
  717. EXPORT_SYMBOL(gh_rm_vm_irq_release);
  718. /**
  719. * gh_rm_vm_irq_release_notify: Release IRQ back to a VM and notify that it has
  720. * been released.
  721. * @vmid: VM to release interrupt to
  722. * @virq_handle: Virtual IRQ handle to release
  723. */
  724. int gh_rm_vm_irq_release_notify(gh_vmid_t vmid, gh_virq_handle_t virq_handle)
  725. {
  726. return gh_rm_vm_irq_notify(NULL, 0, GH_VM_IRQ_NOTIFY_FLAGS_RELEASED,
  727. virq_handle);
  728. }
  729. EXPORT_SYMBOL(gh_rm_vm_irq_release_notify);
  730. /**
  731. * gh_rm_vm_irq_accept: Bind the virq number to the supplied virq_handle
  732. * @virq_handle: The virtual IRQ handle (for example, obtained via
  733. * call to gh_rm_get_hyp_resources())
  734. * @virq: The virtual IRQ number to bind to. Note that this is the virtual
  735. * GIC IRQ number and not the linux IRQ number. Pass -1 here if the
  736. * caller wants the Resource Manager VM to allocate a number
  737. *
  738. * If provided -1 for virq, the function returns the new IRQ number, else
  739. * the one that was already provided.
  740. *
  741. * The function encodes the error codes via ERR_PTR. Hence, the caller is
  742. * responsible to check it with IS_ERR_OR_NULL().
  743. */
  744. int gh_rm_vm_irq_accept(gh_virq_handle_t virq_handle, int virq)
  745. {
  746. struct gh_vm_irq_accept_resp_payload *resp_payload;
  747. struct gh_vm_irq_accept_req_payload req_payload = {0};
  748. size_t resp_payload_size;
  749. int ret;
  750. /* -1 is valid for virq if requesting for a new number */
  751. if (virq < -1)
  752. return -EINVAL;
  753. req_payload.virq_handle = virq_handle;
  754. req_payload.virq = virq;
  755. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_IRQ_ACCEPT,
  756. &req_payload, sizeof(req_payload),
  757. (void **)&resp_payload, &resp_payload_size);
  758. if (ret) {
  759. pr_err("%s: VM_IRQ_ACCEPT failed with err: %d\n",
  760. __func__, ret);
  761. return ret;
  762. }
  763. if (virq == -1 && resp_payload_size != sizeof(*resp_payload)) {
  764. pr_err("%s: Invalid size received for VM_IRQ_ACCEPT: %u\n",
  765. __func__, resp_payload_size);
  766. if (resp_payload_size)
  767. kfree(resp_payload);
  768. return -EINVAL;
  769. }
  770. ret = virq == -1 ? resp_payload->virq : virq;
  771. kfree(resp_payload);
  772. return ret;
  773. }
  774. EXPORT_SYMBOL(gh_rm_vm_irq_accept);
  775. /**
  776. * gh_rm_vm_irq_release_notify: Release IRQ back to a VM and notify that it has
  777. * been released.
  778. * @vmid: VM to release interrupt to
  779. * @virq_handle: Virtual IRQ handle to release
  780. */
  781. int gh_rm_vm_irq_accept_notify(gh_vmid_t vmid, gh_virq_handle_t virq_handle)
  782. {
  783. return gh_rm_vm_irq_notify(NULL, 0, GH_VM_IRQ_NOTIFY_FLAGS_ACCEPTED,
  784. virq_handle);
  785. }
  786. EXPORT_SYMBOL(gh_rm_vm_irq_accept_notify);
  787. /**
  788. * gh_rm_vm_irq_reclaim: Return a lent IRQ
  789. * @virq_handle: IRQ handle to be reclaimed
  790. */
  791. int gh_rm_vm_irq_reclaim(gh_virq_handle_t virq_handle)
  792. {
  793. struct gh_vm_irq_reclaim_req_payload req_payload = {0};
  794. void *resp;
  795. int ret = 0;
  796. size_t resp_payload_size;
  797. req_payload.virq_handle = virq_handle;
  798. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_IRQ_RECLAIM,
  799. &req_payload, sizeof(req_payload),
  800. &resp, &resp_payload_size);
  801. if (ret) {
  802. pr_err("%s: IRQ_RELEASE returned error: %d\n", __func__,
  803. ret);
  804. return ret;
  805. }
  806. if (resp_payload_size) {
  807. pr_err("%s: Invalid size received for IRQ_RELEASE: %u\n",
  808. __func__, resp_payload_size);
  809. kfree(resp);
  810. ret = -EINVAL;
  811. }
  812. return ret;
  813. }
  814. EXPORT_SYMBOL(gh_rm_vm_irq_reclaim);
  815. /**
  816. * gh_rm_vm_alloc_vmid: Return a vmid associated with the vm loaded into
  817. * memory. This call should be called only during
  818. initialization.
  819. * @vm_name: The enum value of the vm that has been loaded.
  820. * @vmid: Value of vmid read from DT. If not present in DT using 0.
  821. *
  822. * The function encodes the error codes via ERR_PTR. Hence, the caller is
  823. * responsible to check it with IS_ERR_OR_NULL().
  824. */
  825. int gh_rm_vm_alloc_vmid(enum gh_vm_names vm_name, int *vmid)
  826. {
  827. struct gh_vm_allocate_resp_payload *resp_payload;
  828. struct gh_vm_allocate_req_payload req_payload = {0};
  829. size_t resp_payload_size;
  830. struct gh_vm_property vm_prop = {0};
  831. int err, ret;
  832. /* Look up for the vm_name<->vmid pair if already present.
  833. * If so, return.
  834. */
  835. if (vm_name < GH_SELF_VM || vm_name > GH_VM_MAX)
  836. return -EINVAL;
  837. spin_lock(&gh_vm_table_lock);
  838. if (gh_vm_table[vm_name].vmid != GH_VMID_INVAL ||
  839. vm_name == GH_SELF_VM) {
  840. pr_err("%s: VM_ALLOCATE already called for this VM\n",
  841. __func__);
  842. spin_unlock(&gh_vm_table_lock);
  843. return -EINVAL;
  844. }
  845. spin_unlock(&gh_vm_table_lock);
  846. req_payload.vmid = *vmid;
  847. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_ALLOCATE,
  848. &req_payload, sizeof(req_payload),
  849. (void **)&resp_payload, &resp_payload_size);
  850. if (ret) {
  851. pr_err("%s: VM_ALLOCATE failed with err: %d\n",
  852. __func__, ret);
  853. return ret;
  854. }
  855. if (resp_payload &&
  856. (resp_payload_size != sizeof(*resp_payload))) {
  857. pr_err("%s: Invalid size received for VM_ALLOCATE: %u\n",
  858. __func__, resp_payload_size);
  859. kfree(resp_payload);
  860. return -EINVAL;
  861. }
  862. if (resp_payload)
  863. *vmid = resp_payload->vmid;
  864. vm_prop.vmid = *vmid;
  865. err = gh_update_vm_prop_table(vm_name, &vm_prop);
  866. if (err) {
  867. pr_err("%s: Invalid vmid sent for updating table: %d\n",
  868. __func__, vm_prop.vmid);
  869. return -EINVAL;
  870. }
  871. kfree(resp_payload);
  872. return 0;
  873. }
  874. EXPORT_SYMBOL(gh_rm_vm_alloc_vmid);
  875. /**
  876. * gh_rm_vm_dealloc_vmid: Deallocate an already allocated vmid
  877. * @vmid: The vmid to deallocate.
  878. *
  879. * The function returns 0 on success and a negative error code
  880. * upon failure.
  881. */
  882. int gh_rm_vm_dealloc_vmid(gh_vmid_t vmid)
  883. {
  884. struct gh_vm_deallocate_req_payload req_payload = {
  885. .vmid = vmid,
  886. };
  887. size_t resp_payload_size;
  888. int ret;
  889. void *resp;
  890. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_DEALLOCATE,
  891. &req_payload, sizeof(req_payload),
  892. &resp, &resp_payload_size);
  893. if (ret) {
  894. pr_err("%s: VM_DEALLOCATE failed with err: %d\n",
  895. __func__, ret);
  896. return ret;
  897. }
  898. if (resp_payload_size) {
  899. pr_err("%s: Invalid size received for VM_DEALLOCATE: %u\n",
  900. __func__, resp_payload_size);
  901. kfree(resp);
  902. return -EINVAL;
  903. }
  904. gh_reset_vm_prop_table_entry(vmid);
  905. return 0;
  906. }
  907. EXPORT_SYMBOL(gh_rm_vm_dealloc_vmid);
  908. /**
  909. * gh_rm_vm_config_image: Configure the VM properties
  910. * @vmid: The vmid of VM configure.
  911. * @auth_mech: The kind of authentication mechanism based on VM image
  912. * @mem_handle: The handle to the memory lent/donated
  913. * @image_offset: Start addr of image relative to memparcel
  914. * @image_size: Size of image relative to start addr
  915. * @dtb_offset: Base addr of dtb image relative to memparcel
  916. * @dtb_size: Size of dtb relative to memparcel
  917. *
  918. * The function returns 0 on success and a negative error code
  919. * upon failure.
  920. */
  921. int gh_rm_vm_config_image(gh_vmid_t vmid, u16 auth_mech, u32 mem_handle,
  922. u64 image_offset, u64 image_size, u64 dtb_offset, u64 dtb_size)
  923. {
  924. struct gh_vm_config_image_req_payload req_payload = {
  925. .vmid = vmid,
  926. .auth_mech = auth_mech,
  927. .mem_handle = mem_handle,
  928. .image_offset_low = image_offset,
  929. .image_offset_high = image_offset >> 32,
  930. .image_size_low = image_size,
  931. .image_size_high = image_size >> 32,
  932. .dtb_offset_low = dtb_offset,
  933. .dtb_offset_high = dtb_offset >> 32,
  934. .dtb_size_low = dtb_size,
  935. .dtb_size_high = dtb_size >> 32,
  936. };
  937. size_t resp_payload_size;
  938. int ret = 0;
  939. void *resp;
  940. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_CONFIG_IMAGE,
  941. &req_payload, sizeof(req_payload),
  942. &resp, &resp_payload_size);
  943. if (ret) {
  944. pr_err("%s: VM_CONFIG_IMAGE failed with err: %d\n",
  945. __func__, ret);
  946. return ret;
  947. }
  948. if (resp_payload_size) {
  949. pr_err("%s: Invalid size received for VM_CONFIG_IMAGE: %u\n",
  950. __func__, resp_payload_size);
  951. kfree(resp);
  952. return -EINVAL;
  953. }
  954. return 0;
  955. }
  956. EXPORT_SYMBOL(gh_rm_vm_config_image);
  957. /**
  958. * gh_rm_vm_auth_image: Request to authenticate the VM
  959. * @vmid: The vmid of VM to authenticate.
  960. * @n_entries: NUmber of auth_param entries
  961. * @entry: Pointer to gh_vm_auth_param_entry structures
  962. *
  963. * The function returns 0 on success and a negative error code
  964. * upon failure.
  965. */
  966. int gh_rm_vm_auth_image(gh_vmid_t vmid, ssize_t n_entries,
  967. struct gh_vm_auth_param_entry *entry)
  968. {
  969. struct gh_vm_auth_image_req_payload_hdr *req_payload;
  970. struct gh_vm_auth_param_entry *dest_entry;
  971. size_t resp_payload_size;
  972. size_t req_payload_size;
  973. int ret = 0, n_entry;
  974. void *req_buf;
  975. void *resp;
  976. req_payload_size = sizeof(*req_payload) + n_entries*sizeof(*entry);
  977. req_buf = kzalloc(req_payload_size, GFP_KERNEL);
  978. if (!req_buf)
  979. return -ENOMEM;
  980. req_payload = req_buf;
  981. req_payload->vmid = vmid;
  982. req_payload->num_auth_params = n_entries;
  983. dest_entry = req_buf + sizeof(*req_payload);
  984. for (n_entry = 0; n_entry < n_entries; n_entry++) {
  985. dest_entry[n_entry].auth_param_type = entry[n_entry].auth_param_type;
  986. dest_entry[n_entry].auth_param = entry[n_entry].auth_param;
  987. }
  988. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_AUTH_IMAGE,
  989. req_buf, req_payload_size,
  990. &resp, &resp_payload_size);
  991. if (ret) {
  992. pr_err("%s: VM_AUTH_IMAGE failed with err: %d\n",
  993. __func__, ret);
  994. kfree(req_buf);
  995. return ret;
  996. }
  997. if (resp_payload_size) {
  998. pr_err("%s: Invalid size received for VM_AUTH_IMAGE: %u\n",
  999. __func__, resp_payload_size);
  1000. kfree(resp);
  1001. kfree(req_buf);
  1002. return -EINVAL;
  1003. }
  1004. kfree(req_buf);
  1005. return 0;
  1006. }
  1007. EXPORT_SYMBOL(gh_rm_vm_auth_image);
  1008. /**
  1009. * ghd_rm_vm_init: Request to allocate resources of the VM
  1010. * @vmid: The vmid of VM to initialize.
  1011. *
  1012. * The function returns 0 on success and a negative error code
  1013. * upon failure.
  1014. */
  1015. int ghd_rm_vm_init(gh_vmid_t vmid)
  1016. {
  1017. struct gh_vm_init_req_payload req_payload = {
  1018. .vmid = vmid,
  1019. };
  1020. size_t resp_payload_size;
  1021. int ret = 0;
  1022. void *resp;
  1023. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_INIT,
  1024. &req_payload, sizeof(req_payload),
  1025. &resp, &resp_payload_size);
  1026. if (ret) {
  1027. pr_err("%s: VM_INIT failed with err: %d\n",
  1028. __func__, ret);
  1029. return ret;
  1030. }
  1031. if (resp_payload_size) {
  1032. pr_err("%s: Invalid size received for VM_INIT: %u\n",
  1033. __func__, resp_payload_size);
  1034. kfree(resp);
  1035. return -EINVAL;
  1036. }
  1037. return 0;
  1038. }
  1039. EXPORT_SYMBOL(ghd_rm_vm_init);
  1040. /**
  1041. * ghd_rm_vm_start: Send a request to Resource Manager VM to start a VM.
  1042. * @vmid: The vmid of the vm to be started.
  1043. *
  1044. * The function encodes the error codes via ERR_PTR. Hence, the caller is
  1045. * responsible to check it with IS_ERR_OR_NULL().
  1046. */
  1047. int ghd_rm_vm_start(int vmid)
  1048. {
  1049. struct gh_vm_start_resp_payload *resp_payload;
  1050. struct gh_vm_start_req_payload req_payload = {0};
  1051. size_t resp_payload_size;
  1052. int ret = 0;
  1053. req_payload.vmid = (gh_vmid_t) vmid;
  1054. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_START,
  1055. &req_payload, sizeof(req_payload),
  1056. (void **)&resp_payload, &resp_payload_size);
  1057. if (ret) {
  1058. pr_err("%s: VM_START failed with err: %d\n",
  1059. __func__, ret);
  1060. return ret;
  1061. }
  1062. if (resp_payload_size) {
  1063. pr_err("%s: Invalid size received for VM_START: %u\n",
  1064. __func__, resp_payload_size);
  1065. kfree(resp_payload);
  1066. return -EINVAL;
  1067. }
  1068. return 0;
  1069. }
  1070. EXPORT_SYMBOL(ghd_rm_vm_start);
  1071. /**
  1072. * ghd_rm_vm_stop: Send a request to Resource Manager VM to stop a VM.
  1073. * @vmid: The vmid of the vm to be stopped.
  1074. *
  1075. * The function encodes the error codes via ERR_PTR. Hence, the caller is
  1076. * responsible to check it with IS_ERR_OR_NULL().
  1077. */
  1078. int ghd_rm_vm_stop(gh_vmid_t vmid, u32 stop_reason, u8 flags)
  1079. {
  1080. struct gh_vm_stop_req_payload req_payload = {0};
  1081. size_t resp_payload_size;
  1082. int ret = 0;
  1083. void *resp;
  1084. if (stop_reason >= GH_VM_STOP_MAX) {
  1085. pr_err("%s: Invalid stop reason provided for VM_STOP\n",
  1086. __func__);
  1087. return -EINVAL;
  1088. }
  1089. req_payload.vmid = vmid;
  1090. req_payload.stop_reason = stop_reason;
  1091. req_payload.flags = flags;
  1092. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_STOP,
  1093. &req_payload, sizeof(req_payload),
  1094. &resp, &resp_payload_size);
  1095. if (ret) {
  1096. pr_err("%s: VM_STOP failed with err: %d\n", __func__, ret);
  1097. return ret;
  1098. }
  1099. if (resp_payload_size) {
  1100. pr_err("%s: Invalid size received for VM_STOP: %u\n",
  1101. __func__, resp_payload_size);
  1102. kfree(resp);
  1103. return -EINVAL;
  1104. }
  1105. return 0;
  1106. }
  1107. EXPORT_SYMBOL(ghd_rm_vm_stop);
  1108. /**
  1109. * ghd_rm_vm_reset: Send a request to Resource Manager VM to free up all
  1110. * resources used by the VM.
  1111. * @vmid: The vmid of the vm to be cleaned up.
  1112. *
  1113. * The function returns 0 on success and a negative error code
  1114. * upon failure.
  1115. */
  1116. int ghd_rm_vm_reset(gh_vmid_t vmid)
  1117. {
  1118. struct gh_vm_reset_req_payload req_payload = {
  1119. .vmid = vmid,
  1120. };
  1121. size_t resp_payload_size;
  1122. int ret;
  1123. void *resp;
  1124. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_RESET,
  1125. &req_payload, sizeof(req_payload),
  1126. &resp, &resp_payload_size);
  1127. if (ret) {
  1128. pr_err("%s: VM_RESET failed with err: %d\n",
  1129. __func__, ret);
  1130. return ret;
  1131. }
  1132. if (resp_payload_size) {
  1133. pr_err("%s: Invalid size received for VM_RESET: %u\n",
  1134. __func__, resp_payload_size);
  1135. kfree(resp);
  1136. return -EINVAL;
  1137. }
  1138. return 0;
  1139. }
  1140. EXPORT_SYMBOL(ghd_rm_vm_reset);
  1141. /**
  1142. * gh_rm_console_open: Open a console with a VM
  1143. * @vmid: The vmid of the vm to be started.
  1144. */
  1145. int gh_rm_console_open(gh_vmid_t vmid)
  1146. {
  1147. void *resp;
  1148. struct gh_vm_console_common_req_payload req_payload = {0};
  1149. size_t resp_payload_size;
  1150. int ret = 0;
  1151. req_payload.vmid = vmid;
  1152. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_CONSOLE_OPEN,
  1153. &req_payload, sizeof(req_payload),
  1154. &resp, &resp_payload_size);
  1155. if (ret) {
  1156. pr_err("%s: CONSOLE_OPEN returned error: %d\n", __func__,
  1157. ret);
  1158. return ret;
  1159. }
  1160. if (resp_payload_size) {
  1161. pr_err("%s: Invalid size received for CONSOLE_OPEN: %u\n",
  1162. __func__, resp_payload_size);
  1163. kfree(resp);
  1164. return -EINVAL;
  1165. }
  1166. return 0;
  1167. }
  1168. EXPORT_SYMBOL(gh_rm_console_open);
  1169. /**
  1170. * gh_rm_console_close: Close a console with a VM
  1171. * @vmid: The vmid of the vm whose console to close.
  1172. */
  1173. int gh_rm_console_close(gh_vmid_t vmid)
  1174. {
  1175. void *resp;
  1176. struct gh_vm_console_common_req_payload req_payload = {0};
  1177. size_t resp_payload_size;
  1178. int ret = 0;
  1179. req_payload.vmid = vmid;
  1180. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_CONSOLE_CLOSE,
  1181. &req_payload, sizeof(req_payload),
  1182. &resp, &resp_payload_size);
  1183. if (ret) {
  1184. pr_err("%s: CONSOLE_CLOSE returned error: %d\n", __func__,
  1185. ret);
  1186. return ret;
  1187. }
  1188. if (resp_payload_size) {
  1189. pr_err("%s: Invalid size received for CONSOLE_CLOSE: %u\n",
  1190. __func__, resp_payload_size);
  1191. kfree(resp);
  1192. return -EINVAL;
  1193. }
  1194. return 0;
  1195. }
  1196. EXPORT_SYMBOL(gh_rm_console_close);
  1197. /**
  1198. * gh_rm_console_write: Write to a VM's console
  1199. * @vmid: The vmid of the vm whose console to write to.
  1200. * @buf: Buffer to write to the VM's console
  1201. * @size: Size of the buffer
  1202. */
  1203. int gh_rm_console_write(gh_vmid_t vmid, const char *buf, size_t size)
  1204. {
  1205. void *resp;
  1206. struct gh_vm_console_write_req_payload *req_payload;
  1207. size_t resp_payload_size;
  1208. int ret = 0;
  1209. size_t req_payload_size = sizeof(*req_payload) + size;
  1210. if (size < 1 || size > (U32_MAX - sizeof(*req_payload)))
  1211. return -EINVAL;
  1212. req_payload = kzalloc(req_payload_size, GFP_KERNEL);
  1213. if (!req_payload)
  1214. return -ENOMEM;
  1215. req_payload->vmid = vmid;
  1216. req_payload->num_bytes = size;
  1217. memcpy(req_payload->data, buf, size);
  1218. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_CONSOLE_WRITE,
  1219. req_payload, req_payload_size,
  1220. &resp, &resp_payload_size);
  1221. kfree(req_payload);
  1222. if (ret) {
  1223. pr_err("%s: CONSOLE_WRITE returned error: %d\n", __func__,
  1224. ret);
  1225. return ret;
  1226. }
  1227. if (resp_payload_size) {
  1228. pr_err("%s: Invalid size received for CONSOLE_WRITE: %u\n",
  1229. __func__, resp_payload_size);
  1230. kfree(resp);
  1231. return -EINVAL;
  1232. }
  1233. return 0;
  1234. }
  1235. EXPORT_SYMBOL(gh_rm_console_write);
  1236. /**
  1237. * gh_rm_console_flush: Flush a console with a VM
  1238. * @vmid: The vmid of the vm whose console to flush
  1239. */
  1240. int gh_rm_console_flush(gh_vmid_t vmid)
  1241. {
  1242. void *resp;
  1243. struct gh_vm_console_common_req_payload req_payload = {0};
  1244. size_t resp_payload_size;
  1245. int ret = 0;
  1246. req_payload.vmid = vmid;
  1247. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_CONSOLE_FLUSH,
  1248. &req_payload, sizeof(req_payload),
  1249. &resp, &resp_payload_size);
  1250. if (ret) {
  1251. pr_err("%s: CONSOLE_FLUSH returned error: %d\n", __func__,
  1252. ret);
  1253. return ret;
  1254. }
  1255. if (resp_payload_size) {
  1256. pr_err("%s: Invalid size received for CONSOLE_FLUSH: %u\n",
  1257. __func__, resp_payload_size);
  1258. kfree(resp);
  1259. return -EINVAL;
  1260. }
  1261. return 0;
  1262. }
  1263. EXPORT_SYMBOL(gh_rm_console_flush);
  1264. static void gh_rm_populate_acl_desc(struct gh_acl_desc *dst_desc,
  1265. struct gh_acl_desc *src_desc)
  1266. {
  1267. u32 n_acl_entries = src_desc ? src_desc->n_acl_entries : 0;
  1268. unsigned int i;
  1269. dst_desc->n_acl_entries = n_acl_entries;
  1270. for (i = 0; i < n_acl_entries; i++) {
  1271. dst_desc->acl_entries[i].vmid = src_desc->acl_entries[i].vmid;
  1272. dst_desc->acl_entries[i].perms = src_desc->acl_entries[i].perms;
  1273. }
  1274. }
  1275. static void gh_rm_populate_sgl_desc(struct gh_sgl_desc *dst_desc,
  1276. struct gh_sgl_desc *src_desc,
  1277. u16 reserved_param)
  1278. {
  1279. u32 n_sgl_entries;
  1280. n_sgl_entries = min_t(u32, GH_RM_MEM_MAX_SGL_ENTRIES,
  1281. src_desc ? src_desc->n_sgl_entries : 0);
  1282. dst_desc->n_sgl_entries = n_sgl_entries;
  1283. dst_desc->reserved = reserved_param;
  1284. if (n_sgl_entries)
  1285. memcpy(dst_desc->sgl_entries, src_desc->sgl_entries,
  1286. sizeof(*dst_desc->sgl_entries) * n_sgl_entries);
  1287. }
  1288. static void gh_rm_populate_mem_attr_desc(struct gh_mem_attr_desc *dst_desc,
  1289. struct gh_mem_attr_desc *src_desc)
  1290. {
  1291. u32 n_mem_attr_entries = src_desc ? src_desc->n_mem_attr_entries : 0;
  1292. dst_desc->n_mem_attr_entries = n_mem_attr_entries;
  1293. if (n_mem_attr_entries)
  1294. memcpy(dst_desc->attr_entries, src_desc->attr_entries,
  1295. sizeof(*dst_desc->attr_entries) * n_mem_attr_entries);
  1296. }
  1297. /*
  1298. * Only first GH_RM_MEM_MAX_SGL_ENTRIES are added to req_buf.
  1299. */
  1300. static void gh_rm_populate_mem_request(void *req_buf, u32 fn_id,
  1301. struct gh_acl_desc *src_acl_desc,
  1302. struct gh_sgl_desc *src_sgl_desc,
  1303. u16 reserved_param,
  1304. struct gh_mem_attr_desc *src_mem_attrs)
  1305. {
  1306. struct gh_acl_desc *dst_acl_desc;
  1307. struct gh_sgl_desc *dst_sgl_desc;
  1308. struct gh_mem_attr_desc *dst_mem_attrs;
  1309. size_t req_hdr_size, req_acl_size, req_sgl_size;
  1310. u32 n_acl_entries = src_acl_desc ? src_acl_desc->n_acl_entries : 0;
  1311. u32 n_sgl_entries;
  1312. n_sgl_entries = min_t(u32, GH_RM_MEM_MAX_SGL_ENTRIES,
  1313. src_sgl_desc ? src_sgl_desc->n_sgl_entries : 0);
  1314. switch (fn_id) {
  1315. case GH_RM_RPC_MSG_ID_CALL_MEM_LEND:
  1316. case GH_RM_RPC_MSG_ID_CALL_MEM_SHARE:
  1317. case GH_RM_RPC_MSG_ID_CALL_MEM_DONATE:
  1318. req_hdr_size = sizeof(struct gh_mem_share_req_payload_hdr);
  1319. break;
  1320. case GH_RM_RPC_MSG_ID_CALL_MEM_QCOM_LOOKUP_SGL:
  1321. req_hdr_size =
  1322. sizeof(struct gh_mem_qcom_lookup_sgl_req_payload_hdr);
  1323. break;
  1324. case GH_RM_RPC_MSG_ID_CALL_MEM_ACCEPT:
  1325. req_hdr_size =
  1326. sizeof(struct gh_mem_accept_req_payload_hdr);
  1327. break;
  1328. default:
  1329. return;
  1330. }
  1331. req_acl_size = offsetof(struct gh_acl_desc, acl_entries[n_acl_entries]);
  1332. req_sgl_size = offsetof(struct gh_sgl_desc, sgl_entries[n_sgl_entries]);
  1333. dst_acl_desc = req_buf + req_hdr_size;
  1334. dst_sgl_desc = req_buf + req_hdr_size + req_acl_size;
  1335. dst_mem_attrs = req_buf + req_hdr_size + req_acl_size + req_sgl_size;
  1336. gh_rm_populate_acl_desc(dst_acl_desc, src_acl_desc);
  1337. gh_rm_populate_sgl_desc(dst_sgl_desc, src_sgl_desc, reserved_param);
  1338. gh_rm_populate_mem_attr_desc(dst_mem_attrs, src_mem_attrs);
  1339. }
  1340. static void *gh_rm_alloc_mem_request_buf(u32 fn_id, size_t n_acl_entries,
  1341. size_t n_sgl_entries,
  1342. size_t n_mem_attr_entries,
  1343. size_t *req_payload_size_ptr)
  1344. {
  1345. size_t req_acl_size, req_sgl_size, req_mem_attr_size, req_payload_size;
  1346. void *req_buf;
  1347. switch (fn_id) {
  1348. case GH_RM_RPC_MSG_ID_CALL_MEM_LEND:
  1349. case GH_RM_RPC_MSG_ID_CALL_MEM_SHARE:
  1350. case GH_RM_RPC_MSG_ID_CALL_MEM_DONATE:
  1351. req_payload_size = sizeof(struct gh_mem_share_req_payload_hdr);
  1352. break;
  1353. case GH_RM_RPC_MSG_ID_CALL_MEM_QCOM_LOOKUP_SGL:
  1354. req_payload_size =
  1355. sizeof(struct gh_mem_qcom_lookup_sgl_req_payload_hdr);
  1356. break;
  1357. case GH_RM_RPC_MSG_ID_CALL_MEM_ACCEPT:
  1358. req_payload_size =
  1359. sizeof(struct gh_mem_accept_req_payload_hdr);
  1360. break;
  1361. default:
  1362. return ERR_PTR(-EINVAL);
  1363. }
  1364. req_acl_size = offsetof(struct gh_acl_desc, acl_entries[n_acl_entries]);
  1365. req_sgl_size = offsetof(struct gh_sgl_desc, sgl_entries[n_sgl_entries]);
  1366. req_mem_attr_size = offsetof(struct gh_mem_attr_desc,
  1367. attr_entries[n_mem_attr_entries]);
  1368. req_payload_size += req_acl_size + req_sgl_size + req_mem_attr_size;
  1369. req_buf = kzalloc(req_payload_size, GFP_KERNEL);
  1370. if (!req_buf)
  1371. return ERR_PTR(-ENOMEM);
  1372. *req_payload_size_ptr = req_payload_size;
  1373. return req_buf;
  1374. }
  1375. /**
  1376. * gh_rm_mem_qcom_lookup_sgl: Look up the handle for a memparcel by its sg-list
  1377. * @mem_type: The type of memory associated with the memparcel (i.e. normal or
  1378. * I/O)
  1379. * @label: The label to assign to the memparcel
  1380. * @acl_desc: Describes the number of ACL entries and VMID and permission pairs
  1381. * for the memparcel
  1382. * @sgl_desc: Describes the number of SG-List entries and the SG-List for the
  1383. * memory associated with the memparcel
  1384. * @mem_attr_desc: Describes the number of memory attribute entries and the
  1385. * memory attribute and VMID pairs for the memparcel. This
  1386. * parameter is currently optional, as this function is meant
  1387. * to be used in conjunction with hyp_assign_[phys/table], which
  1388. * does not provide memory attributes
  1389. * @handle: Pointer to where the memparcel handle should be stored
  1390. *
  1391. * On success, the function will return 0 and populate the memory referenced by
  1392. * @handle with the memparcel handle. Otherwise, a negative number will be
  1393. * returned.
  1394. */
  1395. int gh_rm_mem_qcom_lookup_sgl(u8 mem_type, gh_label_t label,
  1396. struct gh_acl_desc *acl_desc,
  1397. struct gh_sgl_desc *sgl_desc,
  1398. struct gh_mem_attr_desc *mem_attr_desc,
  1399. gh_memparcel_handle_t *handle)
  1400. {
  1401. struct gh_mem_qcom_lookup_sgl_req_payload_hdr *req_payload_hdr;
  1402. struct gh_mem_qcom_lookup_sgl_resp_payload *resp_payload;
  1403. size_t req_payload_size, resp_size;
  1404. void *req_buf;
  1405. unsigned int n_mem_attr_entries = 0;
  1406. u32 fn_id = GH_RM_RPC_MSG_ID_CALL_MEM_QCOM_LOOKUP_SGL;
  1407. int ret = 0;
  1408. if ((mem_type != GH_RM_MEM_TYPE_NORMAL &&
  1409. mem_type != GH_RM_MEM_TYPE_IO) || !acl_desc ||
  1410. !acl_desc->n_acl_entries || !sgl_desc ||
  1411. !sgl_desc->n_sgl_entries || !handle || (mem_attr_desc &&
  1412. !mem_attr_desc->n_mem_attr_entries))
  1413. return -EINVAL;
  1414. if (mem_attr_desc)
  1415. n_mem_attr_entries = mem_attr_desc->n_mem_attr_entries;
  1416. req_buf = gh_rm_alloc_mem_request_buf(fn_id, acl_desc->n_acl_entries,
  1417. sgl_desc->n_sgl_entries,
  1418. n_mem_attr_entries,
  1419. &req_payload_size);
  1420. if (IS_ERR(req_buf))
  1421. return PTR_ERR(req_buf);
  1422. req_payload_hdr = req_buf;
  1423. req_payload_hdr->mem_type = mem_type;
  1424. req_payload_hdr->label = label;
  1425. gh_rm_populate_mem_request(req_buf, fn_id, acl_desc, sgl_desc, 0,
  1426. mem_attr_desc);
  1427. ret = gh_rm_call(rm, fn_id, req_buf, req_payload_size,
  1428. (void **)&resp_payload, &resp_size);
  1429. if (ret) {
  1430. pr_err("%s failed with err: %d\n", __func__, ret);
  1431. goto err_rm_call;
  1432. }
  1433. if (resp_size != sizeof(*resp_payload)) {
  1434. ret = -EINVAL;
  1435. pr_err("%s invalid size received %u\n", __func__, resp_size);
  1436. if (resp_size)
  1437. kfree(resp_payload);
  1438. goto err_rm_call;
  1439. }
  1440. *handle = resp_payload->memparcel_handle;
  1441. err_rm_call:
  1442. kfree(req_buf);
  1443. return ret;
  1444. }
  1445. EXPORT_SYMBOL(gh_rm_mem_qcom_lookup_sgl);
  1446. static int gh_rm_mem_release_helper(u32 fn_id, gh_memparcel_handle_t handle,
  1447. u8 flags)
  1448. {
  1449. struct gh_mem_release_req_payload req_payload = {};
  1450. void *resp;
  1451. size_t resp_size;
  1452. int ret;
  1453. if ((fn_id == GH_RM_RPC_MSG_ID_CALL_MEM_RELEASE) &&
  1454. (flags & ~GH_RM_MEM_RELEASE_VALID_FLAGS))
  1455. return -EINVAL;
  1456. else if ((fn_id == GH_RM_RPC_MSG_ID_CALL_MEM_RECLAIM) &&
  1457. (flags & ~GH_RM_MEM_RECLAIM_VALID_FLAGS))
  1458. return -EINVAL;
  1459. req_payload.memparcel_handle = handle;
  1460. req_payload.flags = flags;
  1461. ret = gh_rm_call(rm, fn_id, &req_payload, sizeof(req_payload), &resp, &resp_size);
  1462. if (ret) {
  1463. pr_err("%s failed with err: %d\n", __func__, ret);
  1464. return ret;
  1465. }
  1466. if (resp_size) {
  1467. kfree(resp);
  1468. return -EBADMSG;
  1469. }
  1470. return 0;
  1471. }
  1472. /**
  1473. * gh_rm_mem_release: Release a handle representing memory. This results in
  1474. * the RM unmapping the associated memory from the stage-2
  1475. * page-tables of the current VM
  1476. * @handle: The memparcel handle associated with the memory
  1477. * @flags: Bitmask of values to influence the behavior of the RM when it unmaps
  1478. * the memory.
  1479. *
  1480. * On success, the function will return 0. Otherwise, a negative number will be
  1481. * returned.
  1482. */
  1483. int gh_rm_mem_release(gh_memparcel_handle_t handle, u8 flags)
  1484. {
  1485. int ret;
  1486. trace_gh_rm_mem_release(handle, flags);
  1487. ret = gh_rm_mem_release_helper(GH_RM_RPC_MSG_ID_CALL_MEM_RELEASE,
  1488. handle, flags);
  1489. trace_gh_rm_mem_call_return(handle, ret);
  1490. return ret;
  1491. }
  1492. EXPORT_SYMBOL(gh_rm_mem_release);
  1493. /**
  1494. * ghd_rm_mem_reclaim: Reclaim a memory represented by a handle. This results in
  1495. * the RM mapping the associated memory into the stage-2
  1496. * page-tables of the owner VM
  1497. * @handle: The memparcel handle associated with the memory
  1498. * @flags: Bitmask of values to influence the behavior of the RM when it unmaps
  1499. * the memory.
  1500. *
  1501. * On success, the function will return 0. Otherwise, a negative number will be
  1502. * returned.
  1503. */
  1504. int ghd_rm_mem_reclaim(gh_memparcel_handle_t handle, u8 flags)
  1505. {
  1506. int ret;
  1507. trace_gh_rm_mem_reclaim(handle, flags);
  1508. ret = gh_rm_mem_release_helper(GH_RM_RPC_MSG_ID_CALL_MEM_RECLAIM,
  1509. handle, flags);
  1510. trace_gh_rm_mem_call_return(handle, ret);
  1511. return ret;
  1512. }
  1513. EXPORT_SYMBOL(ghd_rm_mem_reclaim);
  1514. static void gh_sgl_fragment_release(struct gh_sgl_fragment *gather)
  1515. {
  1516. struct gh_sgl_frag_entry *entry, *tmp;
  1517. list_for_each_entry_safe(entry, tmp, &gather->list, list) {
  1518. list_del(&entry->list);
  1519. kfree(entry->sgl_desc);
  1520. kfree(entry);
  1521. }
  1522. kfree(gather);
  1523. }
  1524. static struct gh_sgl_fragment *gh_sgl_fragment_init(void)
  1525. {
  1526. struct gh_sgl_fragment *gather;
  1527. gather = kzalloc(sizeof(*gather), GFP_KERNEL);
  1528. if (!gather)
  1529. return NULL;
  1530. INIT_LIST_HEAD(&gather->list);
  1531. return gather;
  1532. }
  1533. static int gh_sgl_fragment_append(struct gh_sgl_fragment *gather,
  1534. struct gh_sgl_desc *sgl_desc)
  1535. {
  1536. struct gh_sgl_frag_entry *entry;
  1537. /* Check for overflow */
  1538. if (sgl_desc->n_sgl_entries > (U16_MAX - gather->n_sgl_entries)) {
  1539. pr_err("%s: Too many sgl_entries\n", __func__);
  1540. return -EINVAL;
  1541. }
  1542. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  1543. if (!entry)
  1544. return -ENOMEM;
  1545. entry->sgl_desc = sgl_desc;
  1546. list_add(&entry->list, &gather->list);
  1547. gather->n_sgl_entries += sgl_desc->n_sgl_entries;
  1548. return 0;
  1549. }
  1550. static struct gh_sgl_desc *gh_sgl_fragment_combine(struct gh_sgl_fragment *gather)
  1551. {
  1552. size_t size;
  1553. struct gh_sgl_desc *sgl_desc;
  1554. struct gh_sgl_frag_entry *entry, *tmp;
  1555. struct gh_sgl_entry *p;
  1556. size = offsetof(struct gh_sgl_desc, sgl_entries[gather->n_sgl_entries]);
  1557. sgl_desc = kvmalloc(size, GFP_KERNEL);
  1558. if (!sgl_desc)
  1559. return ERR_PTR(-ENOMEM);
  1560. p = sgl_desc->sgl_entries;
  1561. list_for_each_entry_safe(entry, tmp, &gather->list, list) {
  1562. memcpy(p, entry->sgl_desc->sgl_entries,
  1563. entry->sgl_desc->n_sgl_entries * sizeof(*p));
  1564. p += entry->sgl_desc->n_sgl_entries;
  1565. list_del(&entry->list);
  1566. kfree(entry->sgl_desc);
  1567. kfree(entry);
  1568. }
  1569. sgl_desc->n_sgl_entries = gather->n_sgl_entries;
  1570. gather->n_sgl_entries = 0;
  1571. return sgl_desc;
  1572. }
  1573. static int gh_rm_mem_accept_check_resp(struct gh_mem_accept_resp_payload *resp,
  1574. size_t size, bool has_sgl)
  1575. {
  1576. size_t expected_size;
  1577. if (has_sgl)
  1578. expected_size = 0;
  1579. else if (size < sizeof(*resp))
  1580. expected_size = sizeof(*resp);
  1581. else
  1582. expected_size = sizeof(*resp) +
  1583. resp->n_sgl_entries * sizeof(struct gh_sgl_entry);
  1584. if (size == expected_size)
  1585. return 0;
  1586. pr_err("%s Invalid response size: 0x%zx, expected 0x%zx\n",
  1587. __func__, size, expected_size);
  1588. return -EINVAL;
  1589. }
  1590. static struct gh_mem_accept_req_payload_hdr *
  1591. gh_rm_mem_accept_prepare_request(gh_memparcel_handle_t handle, u8 mem_type,
  1592. u8 trans_type, u8 flags, gh_label_t label,
  1593. struct gh_acl_desc *acl_desc,
  1594. struct gh_sgl_desc *sgl_desc,
  1595. struct gh_mem_attr_desc *mem_attr_desc,
  1596. u16 map_vmid, size_t *req_payload_size)
  1597. {
  1598. void *req_buf;
  1599. struct gh_mem_accept_req_payload_hdr *req_payload_hdr;
  1600. u16 req_sgl_entries = 0, req_mem_attr_entries = 0;
  1601. u32 req_acl_entries = 0;
  1602. u32 fn_id = GH_RM_RPC_MSG_ID_CALL_MEM_ACCEPT;
  1603. if ((mem_type != GH_RM_MEM_TYPE_NORMAL &&
  1604. mem_type != GH_RM_MEM_TYPE_IO) ||
  1605. (trans_type != GH_RM_TRANS_TYPE_DONATE &&
  1606. trans_type != GH_RM_TRANS_TYPE_LEND &&
  1607. trans_type != GH_RM_TRANS_TYPE_SHARE) ||
  1608. (flags & ~GH_RM_MEM_ACCEPT_VALID_FLAGS) ||
  1609. (sgl_desc && sgl_desc->n_sgl_entries > GH_RM_MEM_MAX_SGL_ENTRIES))
  1610. return ERR_PTR(-EINVAL);
  1611. if (flags & GH_RM_MEM_ACCEPT_VALIDATE_ACL_ATTRS &&
  1612. (!acl_desc || !acl_desc->n_acl_entries) &&
  1613. (!mem_attr_desc || !mem_attr_desc->n_mem_attr_entries))
  1614. return ERR_PTR(-EINVAL);
  1615. if (flags & GH_RM_MEM_ACCEPT_VALIDATE_ACL_ATTRS) {
  1616. if (acl_desc)
  1617. req_acl_entries = acl_desc->n_acl_entries;
  1618. if (mem_attr_desc)
  1619. req_mem_attr_entries =
  1620. mem_attr_desc->n_mem_attr_entries;
  1621. }
  1622. if (sgl_desc)
  1623. req_sgl_entries = sgl_desc->n_sgl_entries;
  1624. req_buf = gh_rm_alloc_mem_request_buf(fn_id, req_acl_entries,
  1625. req_sgl_entries,
  1626. req_mem_attr_entries,
  1627. req_payload_size);
  1628. if (IS_ERR(req_buf))
  1629. return req_buf;
  1630. req_payload_hdr = req_buf;
  1631. req_payload_hdr->memparcel_handle = handle;
  1632. req_payload_hdr->mem_type = mem_type;
  1633. req_payload_hdr->trans_type = trans_type;
  1634. req_payload_hdr->flags = flags;
  1635. if (flags & GH_RM_MEM_ACCEPT_VALIDATE_LABEL)
  1636. req_payload_hdr->validate_label = label;
  1637. gh_rm_populate_mem_request(req_buf, fn_id, acl_desc, sgl_desc, map_vmid,
  1638. mem_attr_desc);
  1639. return req_payload_hdr;
  1640. }
  1641. /**
  1642. * gh_rm_mem_accept: Accept a handle representing memory. This results in
  1643. * the RM mapping the associated memory from the stage-2
  1644. * page-tables of a VM
  1645. * @handle: The memparcel handle associated with the memory
  1646. * @mem_type: The type of memory associated with the memparcel (i.e. normal or
  1647. * I/O)
  1648. * @trans_type: The type of memory transfer
  1649. * @flags: Bitmask of values to influence the behavior of the RM when it maps
  1650. * the memory
  1651. * @label: The label to validate against the label maintained by the RM
  1652. * @acl_desc: Describes the number of ACL entries and VMID and permission
  1653. * pairs that the resource manager should validate against for AC
  1654. * regarding the memparcel
  1655. * @sgl_desc: Describes the number of SG-List entries as well as
  1656. * where the memory should be mapped in the IPA space of the VM
  1657. * denoted by @map_vmid. If this parameter is left NULL, then the
  1658. * RM will map the memory at an arbitrary location
  1659. * @mem_attr_desc: Describes the number of memory attribute entries and the
  1660. * memory attribute and VMID pairs that the RM should validate
  1661. * against regarding the memparcel.
  1662. * @map_vmid: The VMID which RM will map the memory for. VMID 0 corresponds
  1663. * to mapping the memory for the current VM
  1664. *
  1665. *
  1666. * On success, the function will return a pointer to an sg-list to convey where
  1667. * the memory has been mapped. If the @sgl_desc parameter was not NULL, then the
  1668. * return value will be a pointer to the same SG-List. Otherwise, the return
  1669. * value will be a pointer to a newly allocated SG-List. After the SG-List is
  1670. * no longer needed, the caller must free the table. On a failure, a negative
  1671. * number will be returned.
  1672. *
  1673. * If a sgl_desc is to be returned, hypervisor may return it in fragments,
  1674. * and multiple calls are needed to obtain the full value.
  1675. */
  1676. struct gh_sgl_desc *gh_rm_mem_accept(gh_memparcel_handle_t handle, u8 mem_type,
  1677. u8 trans_type, u8 flags, gh_label_t label,
  1678. struct gh_acl_desc *acl_desc,
  1679. struct gh_sgl_desc *sgl_desc,
  1680. struct gh_mem_attr_desc *mem_attr_desc,
  1681. u16 map_vmid)
  1682. {
  1683. struct gh_mem_accept_req_payload_hdr *req_payload;
  1684. struct gh_mem_accept_resp_payload *resp_payload;
  1685. size_t req_payload_size, resp_payload_size;
  1686. u32 fn_id = GH_RM_RPC_MSG_ID_CALL_MEM_ACCEPT;
  1687. struct gh_sgl_fragment *gather;
  1688. bool accept_in_progress = false;
  1689. bool multi_call;
  1690. int ret;
  1691. trace_gh_rm_mem_accept(mem_type, flags, label, acl_desc, sgl_desc,
  1692. mem_attr_desc, &handle, map_vmid, trans_type);
  1693. req_payload = gh_rm_mem_accept_prepare_request(handle, mem_type, trans_type, flags,
  1694. label, acl_desc, sgl_desc, mem_attr_desc,
  1695. map_vmid, &req_payload_size);
  1696. if (IS_ERR(req_payload))
  1697. return ERR_CAST(req_payload);
  1698. /* Send DONE flag only after all sgl_desc fragments are received */
  1699. if (flags & GH_RM_MEM_ACCEPT_MAP_IPA_CONTIGUOUS || sgl_desc) {
  1700. multi_call = false;
  1701. } else {
  1702. req_payload->flags &= ~GH_RM_MEM_ACCEPT_DONE;
  1703. multi_call = true;
  1704. }
  1705. gather = gh_sgl_fragment_init();
  1706. if (!gather) {
  1707. ret = -ENOMEM;
  1708. goto err_gather_init;
  1709. }
  1710. do {
  1711. ret = gh_rm_call(rm, fn_id, req_payload, req_payload_size,
  1712. (void **)&resp_payload, &resp_payload_size);
  1713. if (ret) {
  1714. pr_err("%s failed with error: %d\n", __func__, ret);
  1715. goto err_rm_call;
  1716. }
  1717. accept_in_progress = true;
  1718. if (gh_rm_mem_accept_check_resp(resp_payload, resp_payload_size, !!sgl_desc)) {
  1719. ret = -EINVAL;
  1720. if (resp_payload_size) {
  1721. kfree(resp_payload);
  1722. break;
  1723. }
  1724. goto err_rm_call;
  1725. }
  1726. /* Expected when !!sgl_desc */
  1727. if (!resp_payload_size)
  1728. break;
  1729. if (gh_sgl_fragment_append(gather, (struct gh_sgl_desc *)resp_payload)) {
  1730. ret = -ENOMEM;
  1731. kfree(resp_payload);
  1732. goto err_rm_call;
  1733. }
  1734. } while (resp_payload->flags & GH_MEM_ACCEPT_RESP_INCOMPLETE);
  1735. if (multi_call && flags & GH_RM_MEM_ACCEPT_DONE) {
  1736. req_payload->flags |= GH_RM_MEM_ACCEPT_DONE;
  1737. ret = gh_rm_call(rm, fn_id, req_payload, req_payload_size,
  1738. (void **)&resp_payload, &resp_payload_size);
  1739. if (ret) {
  1740. pr_err("%s failed with error: %d\n", __func__, ret);
  1741. goto err_rm_call;
  1742. }
  1743. if (resp_payload_size)
  1744. kfree(resp_payload);
  1745. }
  1746. if (!sgl_desc) {
  1747. sgl_desc = gh_sgl_fragment_combine(gather);
  1748. if (IS_ERR(sgl_desc)) {
  1749. ret = PTR_ERR(sgl_desc);
  1750. goto err_rm_call;
  1751. }
  1752. }
  1753. gh_sgl_fragment_release(gather);
  1754. kfree(req_payload);
  1755. trace_gh_rm_mem_accept_reply(sgl_desc);
  1756. return sgl_desc;
  1757. err_rm_call:
  1758. if (accept_in_progress)
  1759. gh_rm_mem_release(handle, 0);
  1760. gh_sgl_fragment_release(gather);
  1761. err_gather_init:
  1762. kfree(req_payload);
  1763. trace_gh_rm_mem_accept_reply(ERR_PTR(ret));
  1764. return ERR_PTR(ret);
  1765. }
  1766. EXPORT_SYMBOL(gh_rm_mem_accept);
  1767. /**
  1768. * gh_rm_mem_append: Append additional memory to an existing handle.
  1769. * @handle: Memparcel handle from a previous gh_rm_mem_lend/share/donate call, which
  1770. * had GH_RM_MEM_*_APPEND flag set.
  1771. * @flags:
  1772. * @sgl_entries: List of physical memory to append.
  1773. * @n_sgl_entries:
  1774. *
  1775. * flags must include GH_RM_MEM_APPEND_END on the last call to append.
  1776. * in case of error on a partially constructed handle, the caller should call
  1777. * gh_rm_mem_reclaim.
  1778. */
  1779. int gh_rm_mem_append(gh_memparcel_handle_t handle, u8 flags,
  1780. struct gh_sgl_entry *sgl_entries, u32 n_sgl_entries)
  1781. {
  1782. int ret = 0;
  1783. size_t req_payload_size, resp_payload_size;
  1784. void *req_buf, *resp_payload;
  1785. struct gh_mem_append_req_payload_hdr *req_hdr;
  1786. struct gh_sgl_desc *req_sgl_desc;
  1787. if ((flags & ~GH_RM_MEM_APPEND_VALID_FLAGS) ||
  1788. n_sgl_entries > GH_RM_MEM_MAX_SGL_ENTRIES)
  1789. return -EINVAL;
  1790. req_payload_size = sizeof(*req_hdr);
  1791. req_payload_size += offsetof(struct gh_sgl_desc, sgl_entries[n_sgl_entries]);
  1792. req_buf = kmalloc(req_payload_size, GFP_KERNEL);
  1793. if (!req_buf)
  1794. return -ENOMEM;
  1795. req_hdr = req_buf;
  1796. req_sgl_desc = req_buf + sizeof(*req_hdr);
  1797. req_hdr->memparcel_handle = handle;
  1798. req_hdr->flags = flags;
  1799. req_sgl_desc->n_sgl_entries = n_sgl_entries;
  1800. memcpy(req_sgl_desc->sgl_entries, sgl_entries,
  1801. sizeof(*sgl_entries) * n_sgl_entries);
  1802. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_MEM_APPEND,
  1803. req_buf, req_payload_size,
  1804. &resp_payload, &resp_payload_size);
  1805. if (ret) {
  1806. pr_err("%s failed with error: %d\n", __func__, ret);
  1807. goto free_req_buf;
  1808. }
  1809. if (resp_payload_size) {
  1810. ret = -EINVAL;
  1811. pr_err("%s: Invalid size received: %u\n",
  1812. __func__, resp_payload_size);
  1813. kfree(resp_payload);
  1814. }
  1815. free_req_buf:
  1816. kfree(req_buf);
  1817. return ret;
  1818. }
  1819. EXPORT_SYMBOL(gh_rm_mem_append);
  1820. static int gh_rm_mem_share_lend_helper(u32 fn_id, u8 mem_type, u8 flags,
  1821. gh_label_t label,
  1822. struct gh_acl_desc *acl_desc,
  1823. struct gh_sgl_desc *sgl_desc,
  1824. struct gh_mem_attr_desc *mem_attr_desc,
  1825. gh_memparcel_handle_t *handle)
  1826. {
  1827. struct gh_mem_share_req_payload_hdr *req_payload_hdr;
  1828. struct gh_mem_share_resp_payload *resp_payload;
  1829. void *req_buf;
  1830. size_t req_payload_size, resp_payload_size;
  1831. u16 req_sgl_entries, req_acl_entries, req_mem_attr_entries = 0;
  1832. int ret = 0;
  1833. int idx, next;
  1834. if ((mem_type != GH_RM_MEM_TYPE_NORMAL &&
  1835. mem_type != GH_RM_MEM_TYPE_IO) ||
  1836. ((fn_id == GH_RM_RPC_MSG_ID_CALL_MEM_SHARE) &&
  1837. (flags & ~GH_RM_MEM_SHARE_VALID_FLAGS)) ||
  1838. ((fn_id == GH_RM_RPC_MSG_ID_CALL_MEM_LEND) &&
  1839. (flags & ~GH_RM_MEM_LEND_VALID_FLAGS)) ||
  1840. ((fn_id == GH_RM_RPC_MSG_ID_CALL_MEM_DONATE) &&
  1841. (flags & ~GH_RM_MEM_DONATE_VALID_FLAGS)) || !acl_desc ||
  1842. (acl_desc && !acl_desc->n_acl_entries) || !sgl_desc ||
  1843. (sgl_desc && !sgl_desc->n_sgl_entries) ||
  1844. (mem_attr_desc && !mem_attr_desc->n_mem_attr_entries) || !handle)
  1845. return -EINVAL;
  1846. req_acl_entries = acl_desc->n_acl_entries;
  1847. req_sgl_entries = sgl_desc->n_sgl_entries;
  1848. if (req_sgl_entries > GH_RM_MEM_MAX_SGL_ENTRIES) {
  1849. flags |= GH_RM_MEM_LEND_APPEND;
  1850. req_sgl_entries = GH_RM_MEM_MAX_SGL_ENTRIES;
  1851. }
  1852. if (mem_attr_desc)
  1853. req_mem_attr_entries = mem_attr_desc->n_mem_attr_entries;
  1854. req_buf = gh_rm_alloc_mem_request_buf(fn_id, req_acl_entries,
  1855. req_sgl_entries,
  1856. req_mem_attr_entries,
  1857. &req_payload_size);
  1858. if (IS_ERR(req_buf))
  1859. return PTR_ERR(req_buf);
  1860. req_payload_hdr = req_buf;
  1861. req_payload_hdr->mem_type = mem_type;
  1862. req_payload_hdr->flags = flags;
  1863. req_payload_hdr->label = label;
  1864. gh_rm_populate_mem_request(req_buf, fn_id, acl_desc, sgl_desc, 0,
  1865. mem_attr_desc);
  1866. ret = gh_rm_call(rm, fn_id, req_buf, req_payload_size,
  1867. (void **)&resp_payload, &resp_payload_size);
  1868. if (ret) {
  1869. pr_err("%s failed with error: %d\n", __func__, ret);
  1870. goto err_rm_call;
  1871. }
  1872. if (resp_payload_size != sizeof(*resp_payload)) {
  1873. ret = -EINVAL;
  1874. if (resp_payload_size)
  1875. kfree(resp_payload);
  1876. goto err_rm_call;
  1877. }
  1878. for (idx = req_sgl_entries; idx < sgl_desc->n_sgl_entries; idx = next) {
  1879. u8 append_flags = 0;
  1880. next = min_t(u32, idx + GH_RM_MEM_MAX_SGL_ENTRIES, sgl_desc->n_sgl_entries);
  1881. if (next == sgl_desc->n_sgl_entries)
  1882. append_flags |= GH_RM_MEM_APPEND_END;
  1883. ret = gh_rm_mem_append(resp_payload->memparcel_handle, append_flags,
  1884. sgl_desc->sgl_entries + idx, next - idx);
  1885. if (ret)
  1886. goto err_mem_append;
  1887. }
  1888. *handle = resp_payload->memparcel_handle;
  1889. kfree(resp_payload);
  1890. kfree(req_buf);
  1891. return 0;
  1892. err_mem_append:
  1893. ghd_rm_mem_reclaim(resp_payload->memparcel_handle, 0);
  1894. kfree(resp_payload);
  1895. err_rm_call:
  1896. kfree(req_buf);
  1897. return ret;
  1898. }
  1899. /**
  1900. * ghd_rm_mem_share: Share memory with other VM(s) without excluding the owner
  1901. * @mem_type: The type of memory being shared (i.e. normal or I/O)
  1902. * @flags: Bitmask of values to influence the behavior of the RM when it shares
  1903. * the memory
  1904. * @label: The label to assign to the memparcel that the RM will create
  1905. * @acl_desc: Describes the number of ACL entries and VMID and permission
  1906. * pairs that the resource manager should consider when sharing the
  1907. * memory
  1908. * @sgl_desc: Describes the number of SG-List entries as well as
  1909. * the location of the memory in the IPA space of the owner
  1910. * @mem_attr_desc: Describes the number of memory attribute entries and the
  1911. * memory attribute and VMID pairs that the RM should consider
  1912. * when sharing the memory
  1913. * @handle: Pointer to where the memparcel handle should be stored
  1914. * On success, the function will return 0 and populate the memory referenced by
  1915. * @handle with the memparcel handle. Otherwise, a negative number will be
  1916. * returned.
  1917. */
  1918. int ghd_rm_mem_share(u8 mem_type, u8 flags, gh_label_t label,
  1919. struct gh_acl_desc *acl_desc, struct gh_sgl_desc *sgl_desc,
  1920. struct gh_mem_attr_desc *mem_attr_desc,
  1921. gh_memparcel_handle_t *handle)
  1922. {
  1923. int ret;
  1924. trace_gh_rm_mem_share(mem_type, flags, label, acl_desc, sgl_desc,
  1925. mem_attr_desc, handle, 0, SHARE);
  1926. ret = gh_rm_mem_share_lend_helper(GH_RM_RPC_MSG_ID_CALL_MEM_SHARE,
  1927. mem_type, flags, label, acl_desc,
  1928. sgl_desc, mem_attr_desc, handle);
  1929. trace_gh_rm_mem_call_return(*handle, ret);
  1930. return ret;
  1931. }
  1932. EXPORT_SYMBOL(ghd_rm_mem_share);
  1933. /**
  1934. * ghd_rm_mem_lend: Lend memory to other VM(s)--excluding the owner
  1935. * @mem_type: The type of memory being lent (i.e. normal or I/O)
  1936. * @flags: Bitmask of values to influence the behavior of the RM when it lends
  1937. * the memory
  1938. * @label: The label to assign to the memparcel that the RM will create
  1939. * @acl_desc: Describes the number of ACL entries and VMID and permission
  1940. * pairs that the resource manager should consider when lending the
  1941. * memory
  1942. * @sgl_desc: Describes the number of SG-List entries as well as
  1943. * the location of the memory in the IPA space of the owner
  1944. * @mem_attr_desc: Describes the number of memory attribute entries and the
  1945. * memory attribute and VMID pairs that the RM should consider
  1946. * when lending the memory
  1947. * @handle: Pointer to where the memparcel handle should be stored
  1948. * On success, the function will return 0 and populate the memory referenced by
  1949. * @handle with the memparcel handle. Otherwise, a negative number will be
  1950. * returned.
  1951. */
  1952. int ghd_rm_mem_lend(u8 mem_type, u8 flags, gh_label_t label,
  1953. struct gh_acl_desc *acl_desc, struct gh_sgl_desc *sgl_desc,
  1954. struct gh_mem_attr_desc *mem_attr_desc,
  1955. gh_memparcel_handle_t *handle)
  1956. {
  1957. int ret;
  1958. trace_gh_rm_mem_lend(mem_type, flags, label, acl_desc, sgl_desc,
  1959. mem_attr_desc, handle, 0, LEND);
  1960. ret = gh_rm_mem_share_lend_helper(GH_RM_RPC_MSG_ID_CALL_MEM_LEND,
  1961. mem_type, flags, label, acl_desc,
  1962. sgl_desc, mem_attr_desc, handle);
  1963. trace_gh_rm_mem_call_return(*handle, ret);
  1964. return ret;
  1965. }
  1966. EXPORT_SYMBOL(ghd_rm_mem_lend);
  1967. /**
  1968. * gh_rm_mem_donate: Donate memory to a single VM.
  1969. * @mem_type: The type of memory being lent (i.e. normal or I/O)
  1970. * @flags: Bitmask of values to influence the behavior of the RM when it lends
  1971. * the memory
  1972. * @label: The label to assign to the memparcel that the RM will create
  1973. * @acl_desc: Describes the number of ACL entries and VMID and permission
  1974. * pairs that the resource manager should consider when lending the
  1975. * memory
  1976. * @sgl_desc: Describes the number of SG-List entries as well as
  1977. * the location of the memory in the IPA space of the owner
  1978. * @mem_attr_desc: Describes the number of memory attribute entries and the
  1979. * memory attribute and VMID pairs that the RM should consider
  1980. * when lending the memory
  1981. * @handle: Pointer to where the memparcel handle should be stored
  1982. * On success, the function will return 0 and populate the memory referenced by
  1983. * @handle with the memparcel handle. Otherwise, a negative number will be
  1984. * returned.
  1985. *
  1986. * Restrictions:
  1987. * Only to or from HLOS.
  1988. * non-HLOS VM must only donate memory which was previously donated to them by
  1989. * HLOS.
  1990. * Physically contiguous.
  1991. * If Lend or Share operates on a sgl entry which contains memory which
  1992. * originated from donate, that sgl entry must be entirely contained within
  1993. * that donate operation.
  1994. */
  1995. int gh_rm_mem_donate(u8 mem_type, u8 flags, gh_label_t label,
  1996. struct gh_acl_desc *acl_desc, struct gh_sgl_desc *sgl_desc,
  1997. struct gh_mem_attr_desc *mem_attr_desc,
  1998. gh_memparcel_handle_t *handle)
  1999. {
  2000. int ret;
  2001. trace_gh_rm_mem_donate(mem_type, flags, label, acl_desc, sgl_desc,
  2002. mem_attr_desc, handle, 0, DONATE);
  2003. if (acl_desc->n_acl_entries != 1) {
  2004. pr_err("%s: Donate requires single destination VM\n", __func__);
  2005. return -EINVAL;
  2006. }
  2007. if (acl_desc->acl_entries[0].perms != (GH_RM_ACL_X | GH_RM_ACL_W | GH_RM_ACL_R)) {
  2008. pr_err("%s: Invalid permission argument\n", __func__);
  2009. return -EINVAL;
  2010. }
  2011. ret = gh_rm_mem_share_lend_helper(GH_RM_RPC_MSG_ID_CALL_MEM_DONATE,
  2012. mem_type, flags, label, acl_desc,
  2013. sgl_desc, mem_attr_desc, handle);
  2014. trace_gh_rm_mem_call_return(*handle, ret);
  2015. return ret;
  2016. }
  2017. EXPORT_SYMBOL(gh_rm_mem_donate);
  2018. /**
  2019. * gh_rm_mem_notify: Notify VMs about a change in state with respect to a
  2020. * memparcel
  2021. * @handle: The handle of the memparcel for which a notification should be sent
  2022. * out
  2023. * @flags: Flags to determine if the notification is for notifying that memory
  2024. * has been shared to another VM, or that a VM has released memory
  2025. * @mem_info_tag: A 32-bit value that is attached to the
  2026. * MEM_SHARED/MEM_RELEASED/MEM_ACCEPTED notifications to aid in
  2027. * distinguishing different resources from one another.
  2028. * @vmid_desc: A list of VMIDs to notify that memory has been shared with them.
  2029. * This parameter should only be non-NULL if other VMs are being
  2030. * notified (i.e. it is invalid to specify this parameter when the
  2031. * operation is a release notification)
  2032. *
  2033. * On success, the function will return 0. Otherwise, a negative number will be
  2034. * returned.
  2035. */
  2036. int gh_rm_mem_notify(gh_memparcel_handle_t handle, u8 flags,
  2037. gh_label_t mem_info_tag,
  2038. struct gh_notify_vmid_desc *vmid_desc)
  2039. {
  2040. struct gh_mem_notify_req_payload *req_payload_hdr;
  2041. struct gh_notify_vmid_desc *dst_vmid_desc;
  2042. void *req_buf, *resp_payload;
  2043. size_t n_vmid_entries = 0, req_vmid_desc_size = 0, req_payload_size;
  2044. size_t resp_size;
  2045. unsigned int i;
  2046. int ret = 0;
  2047. trace_gh_rm_mem_notify(handle, flags, mem_info_tag, vmid_desc);
  2048. if ((flags & ~GH_RM_MEM_NOTIFY_VALID_FLAGS) ||
  2049. ((flags & GH_RM_MEM_NOTIFY_RECIPIENT_SHARED) && (!vmid_desc ||
  2050. (vmid_desc &&
  2051. !vmid_desc->n_vmid_entries))) ||
  2052. ((flags & (GH_RM_MEM_NOTIFY_OWNER_RELEASED |
  2053. GH_RM_MEM_NOTIFY_OWNER_ACCEPTED)) && vmid_desc) ||
  2054. (hweight8(flags) != 1))
  2055. return -EINVAL;
  2056. if (flags & GH_RM_MEM_NOTIFY_RECIPIENT_SHARED) {
  2057. n_vmid_entries = vmid_desc->n_vmid_entries;
  2058. req_vmid_desc_size = offsetof(struct gh_notify_vmid_desc,
  2059. vmid_entries[n_vmid_entries]);
  2060. }
  2061. req_payload_size = sizeof(*req_payload_hdr) + req_vmid_desc_size;
  2062. req_buf = kzalloc(req_payload_size, GFP_KERNEL);
  2063. if (!req_buf)
  2064. return -ENOMEM;
  2065. req_payload_hdr = req_buf;
  2066. req_payload_hdr->memparcel_handle = handle;
  2067. req_payload_hdr->flags = flags;
  2068. req_payload_hdr->mem_info_tag = mem_info_tag;
  2069. if (flags & GH_RM_MEM_NOTIFY_RECIPIENT_SHARED) {
  2070. dst_vmid_desc = req_buf + sizeof(*req_payload_hdr);
  2071. dst_vmid_desc->n_vmid_entries = n_vmid_entries;
  2072. for (i = 0; i < n_vmid_entries; i++)
  2073. dst_vmid_desc->vmid_entries[i].vmid =
  2074. vmid_desc->vmid_entries[i].vmid;
  2075. }
  2076. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_MEM_NOTIFY, req_buf,
  2077. req_payload_size, &resp_payload, &resp_size);
  2078. if (ret)
  2079. pr_err("%s failed with err: %d\n", __func__, ret);
  2080. if (WARN_ON(resp_size))
  2081. kfree(resp_payload);
  2082. kfree(req_buf);
  2083. return ret;
  2084. }
  2085. EXPORT_SYMBOL(gh_rm_mem_notify);
  2086. /**
  2087. * gh_rm_vm_set_time_base: Send a request to Resource Manager VM to set time base.
  2088. * @vmid: The vmid of the vm to be started.
  2089. *
  2090. * The function encodes the error codes via ERR_PTR. Hence, the caller is
  2091. * responsible to check it with IS_ERR_OR_NULL().
  2092. */
  2093. int gh_rm_vm_set_time_base(gh_vmid_t vmid)
  2094. {
  2095. struct gh_vm_set_time_base_req_payload req_payload = {0};
  2096. size_t resp_payload_size;
  2097. struct timespec64 ts_ref;
  2098. u64 ts_ns_ref;
  2099. u64 qtime_ref;
  2100. int ret = 0;
  2101. void *resp;
  2102. req_payload.vmid = (gh_vmid_t) vmid;
  2103. local_irq_disable();
  2104. ktime_get_real_ts64(&ts_ref);
  2105. qtime_ref = arch_timer_read_counter();
  2106. local_irq_enable();
  2107. ts_ns_ref = timespec64_to_ns(&ts_ref);
  2108. req_payload.time_base_low = (u32) ts_ns_ref;
  2109. req_payload.time_base_high = (u32) (ts_ns_ref >> 32);
  2110. req_payload.arch_timer_ref_low = (u32) qtime_ref;
  2111. req_payload.arch_timer_ref_high = (u32) (qtime_ref >> 32);
  2112. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_SET_TIME_BASE,
  2113. &req_payload, sizeof(req_payload),
  2114. &resp, &resp_payload_size);
  2115. if (ret) {
  2116. pr_err("%s: VM_SET_TIME_BASE failed with err: %d\n",
  2117. __func__, ret);
  2118. return ret;
  2119. }
  2120. if (resp_payload_size) {
  2121. pr_err("%s: Invalid size received for VM_SET_TIME_BASE: %u\n",
  2122. __func__, resp_payload_size);
  2123. return -EINVAL;
  2124. }
  2125. return 0;
  2126. }
  2127. EXPORT_SYMBOL(gh_rm_vm_set_time_base);
  2128. /**
  2129. * gh_rm_minidump_get_info: Get available slot number of current VM
  2130. *
  2131. * On success, the function will return available slot number.
  2132. * Otherwise, a negative number will be returned.
  2133. */
  2134. int gh_rm_minidump_get_info(void)
  2135. {
  2136. struct gh_minidump_get_info_req_payload req_payload = {};
  2137. struct gh_minidump_get_info_resp_payload *resp_payload;
  2138. size_t resp_size;
  2139. int ret = 0;
  2140. if (!rm)
  2141. return -EPROBE_DEFER;
  2142. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_MINIDUMP_GET_INFO,
  2143. &req_payload, sizeof(req_payload),
  2144. (void **)&resp_payload, &resp_size);
  2145. if (ret) {
  2146. pr_err("%s failed with err: 0x%llx %d\n", __func__, resp_payload, ret);
  2147. goto err_rm_call;
  2148. }
  2149. if (resp_size != sizeof(*resp_payload)) {
  2150. ret = -EINVAL;
  2151. pr_err("%s: Invalid size received: %u\n", __func__, resp_size);
  2152. if (resp_size)
  2153. kfree(resp_payload);
  2154. goto err_rm_call;
  2155. }
  2156. pr_debug("%s: slot num: %d\n", __func__, resp_payload->slot_num);
  2157. ret = resp_payload->slot_num;
  2158. kfree(resp_payload);
  2159. err_rm_call:
  2160. return ret;
  2161. }
  2162. EXPORT_SYMBOL(gh_rm_minidump_get_info);
  2163. /**
  2164. * gh_rm_minidump_register_range: Register a minidump entry
  2165. *
  2166. * @base_ipa: The base ipa of mem region which need to register
  2167. * @region_size: The size of mem region
  2168. * @name: The name of minidump entry
  2169. * @name_size: The size of entry name
  2170. *
  2171. * On success, the function will return slot number. Otherwise, a negative number will be
  2172. * returned.
  2173. */
  2174. int gh_rm_minidump_register_range(phys_addr_t base_ipa, size_t region_size,
  2175. const char *name, size_t name_size)
  2176. {
  2177. struct gh_minidump_register_range_req_hdr *req_hdr;
  2178. struct gh_minidump_register_range_resp_payload *resp_payload;
  2179. void *req_buf;
  2180. size_t resp_size, req_size;
  2181. int ret = 0;
  2182. if (!name)
  2183. return -EINVAL;
  2184. req_size = sizeof(*req_hdr);
  2185. req_size += name_size;
  2186. req_buf = kmalloc(req_size, GFP_KERNEL);
  2187. if (!req_buf)
  2188. return -ENOMEM;
  2189. req_hdr = req_buf;
  2190. req_hdr->base_ipa = base_ipa;
  2191. req_hdr->region_size = region_size;
  2192. req_hdr->name_size = name_size;
  2193. req_hdr->name_offset = sizeof(*req_hdr) + 8;
  2194. memcpy(req_buf + sizeof(*req_hdr), name, name_size);
  2195. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_MINIDUMP_REGISTER_RANGE,
  2196. req_buf, req_size, (void **)&resp_payload, &resp_size);
  2197. if (ret) {
  2198. pr_err("%s failed with err: 0x%llx %d\n", __func__, resp_payload, ret);
  2199. goto err_rm_call;
  2200. }
  2201. if (resp_size != sizeof(*resp_payload)) {
  2202. ret = -EINVAL;
  2203. pr_err("%s: Invalid size received: %u\n", __func__, resp_size);
  2204. if (resp_size)
  2205. kfree(resp_payload);
  2206. goto err_rm_call;
  2207. }
  2208. ret = resp_payload->slot_num;
  2209. pr_debug("%s: slot num: %d\n", __func__, resp_payload->slot_num);
  2210. err_rm_call:
  2211. kfree(req_buf);
  2212. return ret;
  2213. }
  2214. EXPORT_SYMBOL(gh_rm_minidump_register_range);
  2215. /**
  2216. * gh_rm_minidump_deregister_slot: Deregister a minidump entry with slot number
  2217. *
  2218. * @slot_num: The number of slot which need to be deregistered
  2219. *
  2220. * On success, the function will return 0. Otherwise, a negative number will be
  2221. * returned.
  2222. */
  2223. int gh_rm_minidump_deregister_slot(uint16_t slot_num)
  2224. {
  2225. struct gh_minidump_deregister_slot_req_payload req_payload = {};
  2226. void *resp;
  2227. size_t resp_size;
  2228. int ret;
  2229. req_payload.slot_num = slot_num;
  2230. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_MINIDUMP_DEREGISTER_SLOT,
  2231. &req_payload, sizeof(req_payload), &resp, &resp_size);
  2232. if (ret) {
  2233. pr_err("%s failed with err: 0x%llx %d\n", __func__, resp, ret);
  2234. return ret;
  2235. }
  2236. if (resp_size) {
  2237. kfree(resp);
  2238. pr_err("%s: Invalid size received: %u\n", __func__, resp_size);
  2239. return -EINVAL;
  2240. }
  2241. return 0;
  2242. }
  2243. EXPORT_SYMBOL(gh_rm_minidump_deregister_slot);
  2244. /**
  2245. * gh_rm_minidump_get_slot_from_name: Get a slot number by entry name
  2246. *
  2247. * @starting_slot: The start slot number to find the entry
  2248. * @name: The name of a registered entry
  2249. * @name_size: Length of the entry name
  2250. *
  2251. * On success, the function will return slot number. Otherwise, a negative
  2252. * value will be returned.
  2253. */
  2254. int gh_rm_minidump_get_slot_from_name(uint16_t starting_slot, const char *name, size_t name_size)
  2255. {
  2256. int ret = -EINVAL;
  2257. struct gh_minidump_get_slot_req_payload *req_payload;
  2258. struct gh_minidump_get_slot_resp_payload *resp_payload;
  2259. size_t req_size, resp_size;
  2260. void *req_buf;
  2261. if (!name)
  2262. return -EINVAL;
  2263. req_size = sizeof(*req_payload) + name_size;
  2264. req_buf = kzalloc(req_size, GFP_KERNEL);
  2265. if (!req_buf)
  2266. return -ENOMEM;
  2267. req_payload = req_buf;
  2268. req_payload->name_len = name_size;
  2269. req_payload->starting_slot = starting_slot;
  2270. memcpy(req_buf + sizeof(*req_payload), name, name_size);
  2271. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_VM_MINIDUMP_GET_SLOT_NUMBER,
  2272. req_buf, req_size, (void **)&resp_payload, &resp_size);
  2273. if (ret) {
  2274. pr_err("%s failed with err: 0x%llx %d\n", __func__, resp_payload, ret);
  2275. goto err_rm_call;
  2276. }
  2277. if (resp_size != sizeof(*resp_payload)) {
  2278. ret = -EINVAL;
  2279. if (resp_size)
  2280. kfree(resp_payload);
  2281. pr_err("%s: Invalid size received: %u\n", __func__, resp_size);
  2282. goto err_rm_call;
  2283. }
  2284. ret = resp_payload->slot_number;
  2285. kfree(resp_payload);
  2286. err_rm_call:
  2287. kfree(req_buf);
  2288. return ret;
  2289. }
  2290. EXPORT_SYMBOL(gh_rm_minidump_get_slot_from_name);
  2291. /*
  2292. * Reserves an ipa region of the given size and alignment from within a
  2293. * limited range. The IPA range may have additional restrictions on its
  2294. * memory type or security as described by flags & platform_flags.
  2295. * No ipa_unreserve API is supported by hypervisor.
  2296. *
  2297. * @ipa - Return value.
  2298. *
  2299. * Returns zero on success, or negative on failure. May return -EPROBE_DEFER.
  2300. */
  2301. int gh_rm_ipa_reserve(u64 size, u64 align, struct range limits, u32 generic_constraints,
  2302. u32 platform_constraints, u64 *ipa)
  2303. {
  2304. int ret;
  2305. struct gh_ipa_reserve_payload req = {};
  2306. struct gh_ipa_reserve_resp_payload *resp;
  2307. size_t resp_payload_size;
  2308. u64 len;
  2309. if (!gh_rm_core_initialized)
  2310. return -EPROBE_DEFER;
  2311. /* Account for overflow */
  2312. len = limits.end - limits.start + 1;
  2313. if (!len)
  2314. len = ALIGN_DOWN(U64_MAX, PAGE_SIZE);
  2315. if (generic_constraints & ~GH_RM_IPA_RESERVE_VALID_FLAGS ||
  2316. platform_constraints & ~GH_RM_IPA_RESERVE_PLATFORM_VALID_FLAGS) {
  2317. pr_debug("%s: Invalid constraints\n", __func__);
  2318. return -EINVAL;
  2319. }
  2320. if (!IS_ALIGNED(size, PAGE_SIZE) ||
  2321. !IS_ALIGNED(align, PAGE_SIZE) ||
  2322. !IS_ALIGNED(limits.start, PAGE_SIZE) ||
  2323. !IS_ALIGNED(len, PAGE_SIZE)) {
  2324. pr_debug("%s: Parameters must be PAGE aligned\n", __func__);
  2325. return -EINVAL;
  2326. }
  2327. req.alloc_type = GH_RM_IPA_RESERVE_ALLOC_TYPE;
  2328. req.generic_constraints = generic_constraints;
  2329. req.platform_constraints = platform_constraints;
  2330. req.nr_ranges = 1;
  2331. req.region_base = limits.start;
  2332. req.region_size = len;
  2333. req.size = size;
  2334. req.align = align;
  2335. ret = gh_rm_call(rm, GH_RM_RPC_MSG_ID_CALL_IPA_RESERVE,
  2336. &req, sizeof(req),
  2337. (void **)&resp, &resp_payload_size);
  2338. if (ret) {
  2339. pr_err("%s failed with error: %d\n", __func__, ret);
  2340. return ret;
  2341. }
  2342. if (resp_payload_size != sizeof(*resp)) {
  2343. pr_err("%s: Invalid size received: %zu\n",
  2344. __func__, resp_payload_size);
  2345. if (resp_payload_size)
  2346. kfree(resp);
  2347. return -EINVAL;
  2348. }
  2349. *ipa = resp->ipa;
  2350. kfree(resp);
  2351. return 0;
  2352. }
  2353. EXPORT_SYMBOL(gh_rm_ipa_reserve);