hgsl_hyp.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2019-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include "hgsl.h"
  7. #include "hgsl_hyp.h"
  8. #include "hgsl_utils.h"
  9. #include <linux/delay.h>
  10. #include <linux/dma-buf.h>
  11. #include <linux/habmm.h>
  12. #include <linux/sched.h>
  13. // #define DO_RPC_TRACE
  14. #ifdef DO_RPC_TRACE
  15. #define RPC_TRACE()
  16. #define RPC_TRACE_DONE()
  17. #else
  18. #define RPC_TRACE()
  19. #define RPC_TRACE_DONE()
  20. #endif
  21. static const enum gsl_rpc_client_type_t g_client_type = GSL_RPC_CLIENT_TYPE_2;
  22. #define gsl_rpc_send(data, size, channel) \
  23. gsl_rpc_send_(__func__, __LINE__, data, size, channel)
  24. #define gsl_rpc_recv(data, size, channel, interruptible) \
  25. gsl_rpc_recv_(__func__, __LINE__, data, size, channel, interruptible)
  26. static const uint32_t g_client_version = 1;
  27. static const char * const gsl_rpc_func_names[] = {
  28. "RPC_LIBRARY_OPEN",
  29. "RPC_LIBRARY_CLOSE",
  30. "RPC_LIBRARY_VERSION",
  31. "RPC_LIBRARY_SET_MEMNOTIFY_TYPE",
  32. "RPC_DEVICE_OPEN",
  33. "RPC_DEVICE_CLOSE",
  34. "RPC_DEVICE_GETINFO",
  35. "RPC_DEVICE_GETINFO_EXT",
  36. "RPC_DEVICE_SETPOWERSTATE",
  37. "RPC_DEVICE_WAITIRQ",
  38. "RPC_DEVICE_GETIRQCNTRBASE",
  39. "RPC_DEVICE_DUMPSTATE",
  40. "RPC_COMMAND_ISSUEIB",
  41. "RPC_COMMAND_INSERTFENCE",
  42. "RPC_COMMAND_READTIMESTAMP",
  43. "RPC_COMMAND_ISSUEIB_SYNC",
  44. "RPC_COMMAND_ISSUEIB_WITH_ALLOC_LIST",
  45. "RPC_COMMAND_CHECKTIMESTAMP",
  46. "RPC_COMMAND_WAITTIMESTAMP",
  47. "RPC_COMMAND_FREEMEMONTIMESTAMP",
  48. "RPC_COMMAND_RESETSTATUS_INTERNAL",
  49. "RPC_CONTEXT_CREATE",
  50. "RPC_CONTEXT_DESTROY",
  51. "RPC_CONTEXT_BINDGMEMSHADOW",
  52. "RPC_CONTEXT_SETBINBASEOFFSET",
  53. "RPC_MEMORY_READ",
  54. "RPC_MEMORY_WRITE",
  55. "RPC_MEMORY_COPY",
  56. "RPC_MEMORY_SET",
  57. "RPC_MEMORY_QUERYSTATS",
  58. "RPC_MEMORY_ALLOC_PURE",
  59. "RPC_MEMORY_PHYS_ALLOC_PURE",
  60. "RPC_MEMORY_VIRT_ALLOC_PURE",
  61. "RPC_MEMORY_FREE_PURE",
  62. "RPC_MEMORY_CACHEOPERATION",
  63. "RPC_MEMORY_NOTIFY",
  64. "RPC_MEMORY_BIND",
  65. "RPC_MEMORY_BIND_SYNC",
  66. "RPC_MEMORY_MMAP",
  67. "RPC_MEMORY_MUNMAP",
  68. "RPC_MEMORY_CREATE_PAGETABLE",
  69. "RPC_MEMORY_DESTROY_PAGETABLE",
  70. "RPC_MEMORY_SET_PAGETABLE",
  71. "RPC_COMMAND_FREEMEMONTIMESTAMP_PURE",
  72. "RPC_PERFCOUNTER_SELECT",
  73. "RPC_PERFCOUNTER_DESELECT",
  74. "RPC_PERFCOUNTER_QUERYSELECTIONS",
  75. "RPC_PERFCOUNTER_READ",
  76. "RPC_SYNCOBJ_CREATE",
  77. "RPC_SYNCOBJ_CREATE_FROM_BIND",
  78. "RPC_SYNCOBJ_DESTROY",
  79. "RPC_SYNCOBJ_WAIT",
  80. "RPC_TIMESTAMP_CMP",
  81. "RPC_SYNCOBJ_CLONE",
  82. "RPC_SYNCOBJ_MERGE",
  83. "RPC_SYNCOBJ_MERGE_MULTIPLE",
  84. "RPC_SYNCSOURCE_CREATE",
  85. "RPC_SYNCSOURCE_DESTROY",
  86. "RPC_SYNCOBJ_CREATE_FROM_SOURCE",
  87. "RPC_SYNCOBJ_SIGNAL",
  88. "RPC_SYNCOBJ_WAIT_MULTIPLE",
  89. "RPC_DEVICE_DEBUG",
  90. "RPC_CFFDUMP_WAITIRQ",
  91. "RPC_CFFDUMP_WRITEVERIFYFILE",
  92. "RPC_MEMORY_MAP_EXT_FD_PURE", /* Linux extension. */
  93. "RPC_MEMORY_UNMAP_EXT_FD_PURE", /* Linux extension */
  94. "RPC_GET_SHADOWMEM",
  95. "RPC_PUT_SHADOWMEM",
  96. "RPC_BLIT",
  97. "RPC_HANDSHAKE",
  98. "RPC_SUB_HANDSHAKE",
  99. "RPC_DISCONNECT",
  100. "RPC_MEMORY_SET_METAINFO",
  101. "RPC_GET_SYSTEM_TIME",
  102. "RPC_GET_DBQ_INFO",
  103. "RPC_DBQ_CREATE",
  104. "RPC_PERFCOUNTERS_READ",
  105. "RPC_NOTIFY_CLEANUP",
  106. "RPC_COMMAND_RESETSTATUS",
  107. "RPC_CONTEXT_QUERY_DBCQ",
  108. "RPC_CONTEXT_REGISTER_DBCQ",
  109. "RPC_FUNC_LAST" // insert new func BEFORE this line!
  110. };
  111. static inline const char *hgsl_get_rpc_fname(unsigned int opcode)
  112. {
  113. const char *fname = "Invalid opcode";
  114. if (opcode < RPC_FUNC_LAST)
  115. fname = gsl_rpc_func_names[opcode];
  116. return fname;
  117. }
  118. static int hgsl_rpc_connect(struct hgsl_hyp_priv_t *priv, int *socket)
  119. {
  120. int err = 0;
  121. int tmp_socket = priv->conn_id;
  122. LOGI("connecting using conn_id %d", tmp_socket);
  123. err = gsl_hab_open(&tmp_socket);
  124. LOGI("socket_open err %d, socket %d", err, tmp_socket);
  125. *socket = tmp_socket;
  126. return err;
  127. }
  128. static int hgsl_rpc_parcel_init(struct hgsl_hab_channel_t *hab_channel)
  129. {
  130. int ret = gsl_rpc_parcel_init(&hab_channel->send_buf);
  131. if (!ret)
  132. ret = gsl_rpc_parcel_init(&hab_channel->recv_buf);
  133. return ret;
  134. }
  135. static int hgsl_rpc_parcel_reset(struct hgsl_hab_channel_t *hab_channel)
  136. {
  137. int ret = gsl_rpc_parcel_reset(&hab_channel->send_buf);
  138. if (!ret)
  139. ret = gsl_rpc_parcel_reset(&hab_channel->recv_buf);
  140. return ret;
  141. }
  142. /*---------------------------------------------------------------------------*/
  143. static int gsl_rpc_send_(const char *fname, int line_num, void *data,
  144. size_t size, struct hgsl_hab_channel_t *hab_channel)
  145. {
  146. int ret = gsl_hab_send(hab_channel->socket,
  147. (unsigned char *)data, size);
  148. if (ret)
  149. LOGE("failed to send @ %s:%d", fname, line_num);
  150. return ret;
  151. }
  152. /*---------------------------------------------------------------------------*/
  153. static int gsl_rpc_recv_(const char *fname, int line_num, void *data,
  154. size_t size, struct hgsl_hab_channel_t *hab_channel, int interruptible)
  155. {
  156. int ret = gsl_hab_recv(hab_channel->socket,
  157. (unsigned char *)data, size, interruptible);
  158. return ret;
  159. }
  160. static int gsl_rpc_transact_ext(uint32_t opcode, uint32_t version,
  161. struct hgsl_hab_channel_t *hab_channel, bool interruptible)
  162. {
  163. int ret = -EINVAL;
  164. struct gsl_hab_payload *data = &hab_channel->send_buf;
  165. struct gsl_hab_payload *reply = &hab_channel->recv_buf;
  166. if (data && reply) {
  167. void *p_data;
  168. uint32_t data_size, max_size;
  169. uint32_t recv_opcode;
  170. if (hab_channel->wait_retry && interruptible) {
  171. ret = 0;
  172. } else if (hab_channel->wait_retry) {
  173. LOGE("channel is waiting for retry for uninterruptible RPC call");
  174. ret = -EINVAL;
  175. goto out;
  176. } else {
  177. gsl_rpc_set_call_params(data, opcode, version);
  178. ret = gsl_rpc_finalize(data);
  179. if (!ret) {
  180. ret = gsl_rpc_get_data_params(data,
  181. &p_data, &data_size, &max_size);
  182. } else {
  183. LOGE("failed to set footer, err %d", ret);
  184. goto out;
  185. }
  186. if (!ret) {
  187. ret = gsl_rpc_send(p_data,
  188. data->data_pos, hab_channel);
  189. } else {
  190. LOGE("failed to get data params, err %d", ret);
  191. goto out;
  192. }
  193. }
  194. if (!ret) {
  195. ret = gsl_rpc_get_data_params(reply,
  196. &p_data, &data_size, &max_size);
  197. } else {
  198. LOGE("failed to send data, err %d", ret);
  199. goto out;
  200. }
  201. if (!ret) {
  202. ret = gsl_rpc_recv(p_data, max_size, hab_channel, interruptible);
  203. } else {
  204. LOGE("failed to get data params, err %d", ret);
  205. goto out;
  206. }
  207. if (ret == -EINTR) {
  208. goto out;
  209. } else if (!ret) {
  210. ret = gsl_rpc_get_call_params(reply,
  211. &recv_opcode, NULL);
  212. } else {
  213. LOGE("failed to recv data, err %d", ret);
  214. goto out;
  215. }
  216. if (!ret) {
  217. if (recv_opcode != opcode) {
  218. if (opcode != RPC_DISCONNECT)
  219. LOGE("recv opcode %d (%s), expected %d (%s)",
  220. recv_opcode,
  221. hgsl_get_rpc_fname(recv_opcode),
  222. opcode,
  223. hgsl_get_rpc_fname(opcode));
  224. ret = -EINVAL;
  225. }
  226. } else {
  227. LOGE("failed to parse data, err %d", ret);
  228. }
  229. }
  230. out:
  231. return ret;
  232. }
  233. static int gsl_rpc_transact(uint32_t opcode,
  234. struct hgsl_hab_channel_t *hab_channel)
  235. {
  236. int ret = gsl_rpc_transact_ext(opcode, 0, hab_channel, false);
  237. if (ret == -EINTR) {
  238. LOGE("noninterruptible transaction was interrupted");
  239. ret = -EINVAL;
  240. }
  241. return ret;
  242. }
  243. static int gsl_rpc_transact_interrruptible(uint32_t opcode,
  244. struct hgsl_hab_channel_t *hab_channel)
  245. {
  246. int ret = gsl_rpc_transact_ext(opcode, 0, hab_channel, true);
  247. if (ret == -EINTR) {
  248. hab_channel->busy = false;
  249. hab_channel->wait_retry = true;
  250. } else {
  251. hab_channel->wait_retry = false;
  252. }
  253. return ret;
  254. }
  255. static int rpc_handshake(struct hgsl_hyp_priv_t *priv,
  256. struct hgsl_hab_channel_t *hab_channel)
  257. {
  258. int ret = 0;
  259. int rval = GSL_FAILURE;
  260. struct gsl_hab_payload *send_buf = NULL;
  261. struct gsl_hab_payload *recv_buf = NULL;
  262. union {
  263. struct handshake_params_t v1;
  264. struct handshake_params_v2_t v2;
  265. } params;
  266. size_t params_size;
  267. int handshake_version;
  268. int tmp = 0;
  269. enum gsl_rpc_server_type_t server_type = GSL_RPC_SERVER_TYPE_LAST;
  270. enum gsl_rpc_server_mode_t server_mode = GSL_RPC_SERVER_MODE_LAST;
  271. RPC_TRACE();
  272. for (handshake_version = 2; handshake_version >= 1; handshake_version--) {
  273. ret = hgsl_rpc_parcel_reset(hab_channel);
  274. if (ret) {
  275. LOGE("hgsl_rpc_parcel_reset failed %d", ret);
  276. goto out;
  277. }
  278. send_buf = &hab_channel->send_buf;
  279. recv_buf = &hab_channel->recv_buf;
  280. switch (handshake_version) {
  281. case 1:
  282. params_size = sizeof(params.v1);
  283. params.v1.client_type = g_client_type;
  284. params.v1.client_version = g_client_version;
  285. params.v1.pid = priv->client_pid;
  286. params.v1.size = sizeof(params.v1);
  287. strscpy(params.v1.name, priv->client_name, sizeof(params.v1.name));
  288. LOGD("client process name is (%s), handshake version %d",
  289. params.v1.name, handshake_version);
  290. break;
  291. case 2:
  292. params_size = sizeof(params.v2);
  293. params.v2.client_type = g_client_type;
  294. params.v2.client_version = g_client_version;
  295. params.v2.pid = priv->client_pid;
  296. params.v2.size = sizeof(params.v2);
  297. strscpy(params.v2.name, priv->client_name, sizeof(params.v2.name));
  298. params.v2.uid = from_kuid(current_user_ns(), current_uid());
  299. LOGD("client process name is (%s), uid %u, handshake version %d",
  300. params.v2.name, params.v2.uid, handshake_version);
  301. break;
  302. default:
  303. LOGE("Unknown handshake version %d", handshake_version);
  304. ret = -EINVAL;
  305. goto out;
  306. }
  307. ret = gsl_rpc_write(send_buf, &params, params_size);
  308. if (ret) {
  309. LOGE("gsl_rpc_write failed %d", ret);
  310. goto out;
  311. }
  312. ret = gsl_rpc_transact_ext(RPC_HANDSHAKE, handshake_version, hab_channel, 0);
  313. if (ret) {
  314. LOGE("gsl_rpc_transact_ext failed %d", ret);
  315. goto out;
  316. }
  317. ret = gsl_rpc_read_int32_l(recv_buf, &rval);
  318. if (ret) {
  319. LOGE("gsl_rpc_read_int32_l failed %d", ret);
  320. goto out;
  321. }
  322. if (rval != GSL_SUCCESS) {
  323. LOGE("Handshake failed %d, BE sent error %d, try smaller version",
  324. handshake_version, rval);
  325. } else {
  326. LOGD("Handshake success %d", handshake_version);
  327. break;
  328. }
  329. }
  330. if (ret == 0 && rval == GSL_SUCCESS) {
  331. ret = gsl_rpc_read_int32_l(recv_buf, &priv->conn_id);
  332. if (ret) {
  333. LOGE("Failed to read conn_id %d", ret);
  334. goto out;
  335. }
  336. ret = gsl_rpc_read_int32_l(recv_buf, &tmp);
  337. if (ret) {
  338. LOGE("Failed to read server_type %d", ret);
  339. goto out;
  340. }
  341. server_type = (enum gsl_rpc_server_type_t)tmp;
  342. ret = gsl_rpc_read_int32_l(recv_buf, &tmp);
  343. if (ret) {
  344. LOGE("Failed to read server_mode %d", ret);
  345. goto out;
  346. }
  347. server_mode = (enum gsl_rpc_server_mode_t)tmp;
  348. LOGI("Successfully connected to server, got connection id %d",
  349. priv->conn_id);
  350. } else {
  351. LOGE("handshake failed, %d", ret);
  352. }
  353. out:
  354. RPC_TRACE_DONE();
  355. return ret;
  356. }
  357. /*---------------------------------------------------------------------------*/
  358. static int rpc_sub_handshake(struct hgsl_hyp_priv_t *priv,
  359. struct hgsl_hab_channel_t *hab_channel)
  360. {
  361. int ret = 0;
  362. int rval = GSL_SUCCESS;
  363. struct gsl_hab_payload *send_buf = NULL;
  364. struct gsl_hab_payload *recv_buf = NULL;
  365. struct sub_handshake_params_t params = { 0 };
  366. int server_allow_direct_writes = 0;
  367. RPC_TRACE();
  368. ret = hgsl_rpc_parcel_reset(hab_channel);
  369. if (ret) {
  370. LOGE("hgsl_rpc_parcel_reset failed %d", ret);
  371. goto out;
  372. }
  373. send_buf = &hab_channel->send_buf;
  374. recv_buf = &hab_channel->recv_buf;
  375. params.size = sizeof(params);
  376. params.pid = priv->client_pid;
  377. params.memdesc_size = sizeof(struct gsl_memdesc_t);
  378. ret = gsl_rpc_write(send_buf, &params, sizeof(params));
  379. if (ret) {
  380. LOGE("gsl_rpc_write failed %d", ret);
  381. goto out;
  382. }
  383. ret = gsl_rpc_transact_ext(RPC_SUB_HANDSHAKE, 1, hab_channel, 0);
  384. if (ret) {
  385. LOGE("gsl_rpc_transact_ext failed %d", ret);
  386. goto out;
  387. }
  388. ret = gsl_rpc_read_int32_l(recv_buf, &rval);
  389. if ((!ret) && (rval != GSL_SUCCESS)) {
  390. LOGE("BE sent error %d", rval);
  391. ret = -EINVAL;
  392. }
  393. if (!ret) {
  394. ret = gsl_rpc_read_int32_l(recv_buf, &server_allow_direct_writes);
  395. if (ret) {
  396. LOGE("Failed to read server_allow_direct_writes %d", ret);
  397. goto out;
  398. }
  399. } else {
  400. LOGE("sub handshake failed, %d", ret);
  401. }
  402. out:
  403. RPC_TRACE_DONE();
  404. return ret;
  405. }
  406. static void hgsl_hyp_close_channel(struct hgsl_hab_channel_t *hab_channel)
  407. {
  408. struct gsl_hab_payload *send_buf = NULL;
  409. struct gsl_hab_payload *recv_buf = NULL;
  410. int ret = 0;
  411. int rval = GSL_SUCCESS;
  412. if (hab_channel == NULL) {
  413. LOGE("hab_channel is NULL");
  414. goto out;
  415. }
  416. send_buf = &hab_channel->send_buf;
  417. recv_buf = &hab_channel->recv_buf;
  418. if (hab_channel->socket != HAB_INVALID_HANDLE) {
  419. ret = hgsl_rpc_parcel_reset(hab_channel);
  420. if (ret)
  421. LOGE("hgsl_rpc_parcel_reset failed, %d", ret);
  422. else
  423. ret = gsl_rpc_transact(RPC_DISCONNECT, hab_channel);
  424. if (!ret) {
  425. ret = gsl_rpc_read_int32_l(recv_buf, &rval);
  426. if (ret || rval)
  427. LOGE("RPC_DISCONNECT failed %d, %d", ret, rval);
  428. }
  429. if (hab_channel->socket != HAB_INVALID_HANDLE) {
  430. gsl_hab_close(hab_channel->socket);
  431. hab_channel->socket = HAB_INVALID_HANDLE;
  432. }
  433. }
  434. gsl_rpc_parcel_free(send_buf);
  435. gsl_rpc_parcel_free(recv_buf);
  436. if (hab_channel->id >= 0)
  437. idr_remove(&hab_channel->priv->channel_idr, hab_channel->id);
  438. hgsl_free(hab_channel);
  439. out:
  440. return;
  441. }
  442. static int hgsl_rpc_create_channel(
  443. struct hgsl_hyp_priv_t *priv,
  444. struct hgsl_hab_channel_t **channel)
  445. {
  446. int socket = HAB_INVALID_HANDLE;
  447. int ret = -ENOMEM;
  448. struct hgsl_hab_channel_t *hab_channel
  449. = (struct hgsl_hab_channel_t *)hgsl_zalloc(
  450. sizeof(struct hgsl_hab_channel_t));
  451. if (hab_channel == NULL) {
  452. LOGE("Failed to allocate hab_channel");
  453. goto out;
  454. }
  455. hab_channel->socket = HAB_INVALID_HANDLE;
  456. hab_channel->priv = priv;
  457. hab_channel->busy = false;
  458. hab_channel->wait_retry = false;
  459. hab_channel->id = idr_alloc(&priv->channel_idr, hab_channel,
  460. 1, 0, GFP_NOWAIT);
  461. if (hab_channel->id < 0) {
  462. LOGE("Failed to allocate id for hab channel");
  463. ret = hab_channel->id;
  464. goto out;
  465. }
  466. ret = hgsl_rpc_parcel_init(hab_channel);
  467. if (ret) {
  468. LOGE("Failed to init parcel");
  469. goto out;
  470. }
  471. if (priv->conn_id == 0) {
  472. ret = hgsl_rpc_connect(priv, &socket);
  473. if (ret) {
  474. LOGE("Failed to open socket %d", ret);
  475. goto out;
  476. }
  477. hab_channel->socket = socket;
  478. ret = rpc_handshake(priv, hab_channel);
  479. if (ret)
  480. LOGE("rpc_handshake failed %d", ret);
  481. gsl_hab_close(socket);
  482. hab_channel->socket = HAB_INVALID_HANDLE;
  483. }
  484. ret = hgsl_rpc_connect(priv, &socket);
  485. if (ret) {
  486. LOGE("Failed to open socket %d", ret);
  487. goto out;
  488. }
  489. hab_channel->socket = socket;
  490. ret = rpc_sub_handshake(priv, hab_channel);
  491. if (ret) {
  492. LOGE("sub handshake failed %d", ret);
  493. gsl_hab_close(socket);
  494. hab_channel->socket = HAB_INVALID_HANDLE;
  495. }
  496. out:
  497. if (ret) {
  498. LOGE("Failed to create channel %d exiting", ret);
  499. if (hab_channel != NULL) {
  500. hgsl_hyp_close_channel(hab_channel);
  501. hab_channel = NULL;
  502. }
  503. } else {
  504. *channel = hab_channel;
  505. }
  506. return ret;
  507. }
  508. void hgsl_hyp_channel_pool_put_unsafe(struct hgsl_hab_channel_t *hab_channel)
  509. {
  510. if (hab_channel != NULL) {
  511. struct hgsl_hyp_priv_t *priv = hab_channel->priv;
  512. if (hab_channel->wait_retry)
  513. LOGE("put channel waiting for retry");
  514. hab_channel->busy = false;
  515. list_del(&hab_channel->node);
  516. list_add_tail(&hab_channel->node, &priv->free_channels);
  517. LOGD("put %p back to free pool", hab_channel);
  518. }
  519. }
  520. void hgsl_hyp_channel_pool_put(struct hgsl_hab_channel_t *hab_channel)
  521. {
  522. if (hab_channel != NULL) {
  523. struct hgsl_hyp_priv_t *priv = hab_channel->priv;
  524. mutex_lock(&priv->lock);
  525. hgsl_hyp_channel_pool_put_unsafe(hab_channel);
  526. mutex_unlock(&priv->lock);
  527. }
  528. }
  529. static int hgsl_hyp_channel_pool_get_by_id(
  530. struct hgsl_hyp_priv_t *priv, int id, struct hgsl_hab_channel_t **channel)
  531. {
  532. struct hgsl_hab_channel_t *hab_channel = NULL;
  533. int ret = -EINVAL;
  534. if (id > 0)
  535. hab_channel = idr_find(&priv->channel_idr, id);
  536. if (hab_channel) {
  537. if (!hab_channel->wait_retry) {
  538. hab_channel = NULL;
  539. LOGE("channel %d isn't waiting for retry", id);
  540. }
  541. } else {
  542. LOGE("can't find id %d", id);
  543. }
  544. if (hab_channel) {
  545. ret = 0;
  546. *channel = hab_channel;
  547. }
  548. return ret;
  549. }
  550. int hgsl_hyp_channel_pool_get(
  551. struct hgsl_hyp_priv_t *priv, int id, struct hgsl_hab_channel_t **channel)
  552. {
  553. struct hgsl_hab_channel_t *hab_channel = NULL;
  554. int ret = 0;
  555. if (!channel)
  556. return -EINVAL;
  557. mutex_lock(&priv->lock);
  558. if (id) {
  559. ret = hgsl_hyp_channel_pool_get_by_id(priv, id, &hab_channel);
  560. if (ret)
  561. LOGE("Failed to find channel %d, ret %d", id, ret);
  562. } else {
  563. if (list_empty(&priv->free_channels)) {
  564. ret = hgsl_rpc_create_channel(priv, &hab_channel);
  565. LOGD("hgsl_rpc_create_channel returned, ret %d hab_channel %p",
  566. ret, hab_channel);
  567. } else {
  568. hab_channel = container_of(priv->free_channels.next,
  569. struct hgsl_hab_channel_t, node);
  570. if (hab_channel != NULL) {
  571. list_del(&hab_channel->node);
  572. LOGD("get %p from free pool", hab_channel);
  573. } else {
  574. ret = -EINVAL;
  575. LOGE("invalid hab_channel in the list");
  576. }
  577. }
  578. if (!ret)
  579. list_add_tail(&hab_channel->node, &priv->busy_channels);
  580. }
  581. if (!ret) {
  582. *channel = hab_channel;
  583. hab_channel->busy = true;
  584. }
  585. mutex_unlock(&priv->lock);
  586. if ((!ret) && (!id)) {
  587. ret = hgsl_rpc_parcel_reset(hab_channel);
  588. if (ret) {
  589. LOGE("hgsl_rpc_parcel_reset failed %d", ret);
  590. hgsl_hyp_channel_pool_put(hab_channel);
  591. hab_channel = NULL;
  592. }
  593. }
  594. return ret;
  595. }
  596. static int hgsl_hyp_channel_pool_init(struct hgsl_hyp_priv_t *priv,
  597. int client_pid, const char * const client_name)
  598. {
  599. INIT_LIST_HEAD(&priv->free_channels);
  600. INIT_LIST_HEAD(&priv->busy_channels);
  601. mutex_init(&priv->lock);
  602. priv->conn_id = 0;
  603. strscpy(priv->client_name, client_name, sizeof(priv->client_name));
  604. priv->client_pid = client_pid;
  605. idr_init(&priv->channel_idr);
  606. LOGD("pid %d, task name %s"
  607. , (int) priv->client_pid, priv->client_name);
  608. return 0;
  609. }
  610. static void hgsl_hyp_put_waiting_channel(struct hgsl_hyp_priv_t *priv)
  611. {
  612. struct hgsl_hab_channel_t *channel = NULL;
  613. struct hgsl_hab_channel_t *tmp = NULL;
  614. list_for_each_entry_safe(channel, tmp, &priv->busy_channels, node) {
  615. if (!channel->busy) {
  616. channel->wait_retry = false;
  617. hgsl_hyp_channel_pool_put_unsafe(channel);
  618. }
  619. }
  620. }
  621. static void hgsl_hyp_channel_pool_close(struct hgsl_hyp_priv_t *priv)
  622. {
  623. struct hgsl_hab_channel_t *channel = NULL;
  624. struct hgsl_hab_channel_t *tmp = NULL;
  625. mutex_lock(&priv->lock);
  626. hgsl_hyp_put_waiting_channel(priv);
  627. while (!list_empty(&priv->busy_channels)) {
  628. mutex_unlock(&priv->lock);
  629. usleep_range(100, 1000);
  630. mutex_lock(&priv->lock);
  631. hgsl_hyp_put_waiting_channel(priv);
  632. }
  633. list_for_each_entry_safe(channel, tmp, &priv->free_channels, node) {
  634. list_del(&channel->node);
  635. hgsl_hyp_close_channel(channel);
  636. }
  637. mutex_unlock(&priv->lock);
  638. mutex_destroy(&priv->lock);
  639. memset(priv, 0, sizeof(*priv));
  640. }
  641. int hgsl_hyp_init(struct hgsl_hyp_priv_t *priv, struct device *dev,
  642. int client_pid, const char * const client_name)
  643. {
  644. priv->dev = dev;
  645. return hgsl_hyp_channel_pool_init(priv, client_pid, client_name);
  646. }
  647. void hgsl_hyp_close(struct hgsl_hyp_priv_t *priv)
  648. {
  649. hgsl_hyp_channel_pool_close(priv);
  650. }
  651. int hgsl_hyp_generic_transaction(struct hgsl_hyp_priv_t *priv,
  652. struct hgsl_ioctl_hyp_generic_transaction_params *params,
  653. void **pSend, void **pReply, void *pRval)
  654. {
  655. struct hgsl_hab_channel_t *hab_channel = NULL;
  656. struct gsl_hab_payload *send_buf = NULL;
  657. struct gsl_hab_payload *recv_buf = NULL;
  658. int ret = 0;
  659. unsigned int i = 0;
  660. RPC_TRACE();
  661. ret = hgsl_hyp_channel_pool_get(priv, 0, &hab_channel);
  662. if (ret) {
  663. LOGE("Failed to get hab channel %d", ret);
  664. goto out;
  665. }
  666. send_buf = &hab_channel->send_buf;
  667. recv_buf = &hab_channel->recv_buf;
  668. for (i = 0; i < params->send_num; i++) {
  669. ret = gsl_rpc_write(send_buf, pSend[i], params->send_size[i]);
  670. if (ret) {
  671. LOGE("gsl_rpc_write failed, %d", ret);
  672. goto out;
  673. }
  674. }
  675. ret = gsl_rpc_transact(params->cmd_id, hab_channel);
  676. if (ret) {
  677. LOGE("gsl_rpc_transact failed, %d", ret);
  678. goto out;
  679. }
  680. for (i = 0; i < params->reply_num; i++) {
  681. ret = gsl_rpc_read(recv_buf, pReply[i], params->reply_size[i]);
  682. if (ret) {
  683. LOGE("gsl_rpc_read failed, %d", ret);
  684. goto out;
  685. }
  686. }
  687. if (pRval != NULL) {
  688. ret = gsl_rpc_read_int32_l(recv_buf, pRval);
  689. if (ret) {
  690. LOGE("gsl_rpc_read_int32_l failed, %d", ret);
  691. goto out;
  692. }
  693. }
  694. out:
  695. hgsl_hyp_channel_pool_put(hab_channel);
  696. RPC_TRACE_DONE();
  697. if (params->cmd_id < RPC_FUNC_LAST) {
  698. LOGD("cmd %d %s, ret %d, rval %p %d", params->cmd_id,
  699. hgsl_get_rpc_fname(params->cmd_id),
  700. ret, pRval, pRval ? *((int *)pRval) : 0);
  701. } else {
  702. LOGE("unknown cmd id %d", params->cmd_id);
  703. }
  704. return ret;
  705. }
  706. int hgsl_hyp_gsl_lib_open(struct hgsl_hyp_priv_t *priv,
  707. uint32_t flags, int32_t *rval)
  708. {
  709. struct library_open_params_t rpc_params = { 0 };
  710. struct hgsl_hab_channel_t *hab_channel = NULL;
  711. struct gsl_hab_payload *send_buf = NULL;
  712. struct gsl_hab_payload *recv_buf = NULL;
  713. int ret = 0;
  714. RPC_TRACE();
  715. ret = hgsl_hyp_channel_pool_get(priv, 0, &hab_channel);
  716. if (ret) {
  717. LOGE("Failed to get hab channel %d", ret);
  718. goto out;
  719. }
  720. send_buf = &hab_channel->send_buf;
  721. recv_buf = &hab_channel->recv_buf;
  722. rpc_params.size = sizeof(rpc_params);
  723. rpc_params.flags = flags;
  724. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  725. if (ret) {
  726. LOGE("gsl_rpc_write failed, %d", ret);
  727. goto out;
  728. }
  729. ret = gsl_rpc_transact(RPC_LIBRARY_OPEN, hab_channel);
  730. if (ret) {
  731. LOGE("gsl_rpc_transact failed, %d", ret);
  732. goto out;
  733. }
  734. ret = gsl_rpc_read_int32_l(recv_buf, rval);
  735. if (ret) {
  736. LOGE("gsl_rpc_read_int32_l failed, %d", ret);
  737. goto out;
  738. }
  739. out:
  740. LOGD("%d, %d", ret, *rval);
  741. hgsl_hyp_channel_pool_put(hab_channel);
  742. RPC_TRACE_DONE();
  743. return ret;
  744. }
  745. int hgsl_hyp_ctxt_create(struct hgsl_hab_channel_t *hab_channel,
  746. struct hgsl_ioctl_ctxt_create_params *hgsl_params)
  747. {
  748. struct context_create_params_t rpc_params = { 0 };
  749. struct gsl_hab_payload *send_buf = NULL;
  750. struct gsl_hab_payload *recv_buf = NULL;
  751. int ret = 0;
  752. RPC_TRACE();
  753. if (!hab_channel) {
  754. LOGE("invalid hab_channel");
  755. ret = -EINVAL;
  756. goto out;
  757. }
  758. ret = hgsl_rpc_parcel_reset(hab_channel);
  759. if (ret) {
  760. LOGE("hgsl_rpc_parcel_reset failed %d", ret);
  761. goto out;
  762. }
  763. send_buf = &hab_channel->send_buf;
  764. recv_buf = &hab_channel->recv_buf;
  765. rpc_params.size = sizeof(rpc_params);
  766. rpc_params.devhandle = hgsl_params->devhandle;
  767. rpc_params.type = hgsl_params->type;
  768. rpc_params.flags = hgsl_params->flags;
  769. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  770. if (ret) {
  771. LOGE("gsl_rpc_write failed, %d", ret);
  772. goto out;
  773. }
  774. ret = gsl_rpc_transact(RPC_CONTEXT_CREATE, hab_channel);
  775. if (ret) {
  776. LOGE("gsl_rpc_transact failed, %d", ret);
  777. goto out;
  778. }
  779. ret = gsl_rpc_read_uint32_l(recv_buf, &hgsl_params->ctxthandle);
  780. if (ret) {
  781. LOGE("gsl_rpc_read_uint32_l failed, %d", ret);
  782. goto out;
  783. }
  784. out:
  785. RPC_TRACE_DONE();
  786. return ret;
  787. }
  788. int hgsl_hyp_ctxt_destroy(struct hgsl_hab_channel_t *hab_channel,
  789. uint32_t devhandle, uint32_t context_id, uint32_t *rval, uint32_t dbcq_export_id)
  790. {
  791. struct context_destroy_params_t rpc_params = { 0 };
  792. struct gsl_hab_payload *send_buf = NULL;
  793. struct gsl_hab_payload *recv_buf = NULL;
  794. int ret = 0;
  795. RPC_TRACE();
  796. if (!hab_channel) {
  797. LOGE("invalid hab_channel");
  798. ret = -EINVAL;
  799. goto out;
  800. }
  801. ret = hgsl_rpc_parcel_reset(hab_channel);
  802. if (ret) {
  803. LOGE("hgsl_rpc_parcel_reset failed %d", ret);
  804. goto out;
  805. }
  806. send_buf = &hab_channel->send_buf;
  807. recv_buf = &hab_channel->recv_buf;
  808. rpc_params.size = sizeof(rpc_params);
  809. rpc_params.devhandle = devhandle;
  810. rpc_params.ctxthandle = context_id;
  811. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  812. if (ret) {
  813. LOGE("gsl_rpc_write failed, %d", ret);
  814. goto out;
  815. }
  816. ret = gsl_rpc_transact(RPC_CONTEXT_DESTROY, hab_channel);
  817. if (ret) {
  818. LOGE("gsl_rpc_transact failed, %d", ret);
  819. goto out;
  820. }
  821. if (dbcq_export_id) {
  822. ret = habmm_unexport(hab_channel->socket, dbcq_export_id, 0);
  823. if (ret)
  824. LOGE("Failed to unexport context queue, %d, export_id %d",
  825. context_id, dbcq_export_id);
  826. }
  827. if (rval) {
  828. ret = gsl_rpc_read_uint32_l(recv_buf, rval);
  829. if (ret) {
  830. LOGE("gsl_rpc_read_uint32_l failed, %d", ret);
  831. goto out;
  832. }
  833. }
  834. out:
  835. LOGD("%d %d", context_id, ret);
  836. RPC_TRACE_DONE();
  837. return ret;
  838. }
  839. int hgsl_hyp_get_shadowts_mem(struct hgsl_hab_channel_t *hab_channel,
  840. uint32_t context_id, uint32_t *shadow_ts_flags,
  841. struct hgsl_mem_node *mem_node)
  842. {
  843. struct get_shadowmem_params_v1_t rpc_params = { 0 };
  844. struct gsl_hab_payload *send_buf = NULL;
  845. struct gsl_hab_payload *recv_buf = NULL;
  846. struct shadowprop_t rpc_shadow = { 0 };
  847. int ret = 0;
  848. size_t aligned_size = 0;
  849. struct dma_buf *dma_buf = NULL;
  850. uint32_t export_id;
  851. RPC_TRACE();
  852. if (!hab_channel) {
  853. LOGE("invalid hab_channel");
  854. ret = -EINVAL;
  855. goto out;
  856. }
  857. ret = hgsl_rpc_parcel_reset(hab_channel);
  858. if (ret) {
  859. LOGE("hgsl_rpc_parcel_reset failed %d", ret);
  860. goto out;
  861. }
  862. send_buf = &hab_channel->send_buf;
  863. recv_buf = &hab_channel->recv_buf;
  864. rpc_params.size = sizeof(rpc_params);
  865. rpc_params.device_id = GSL_DEVICE_3D;
  866. rpc_params.ctxthandle = context_id;
  867. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  868. if (ret) {
  869. LOGE("gsl_rpc_write failed, %d", ret);
  870. goto out;
  871. }
  872. ret = gsl_rpc_transact_ext(RPC_GET_SHADOWMEM, 1, hab_channel, 0);
  873. if (ret) {
  874. LOGE("gsl_rpc_transact failed, %d", ret);
  875. goto out;
  876. }
  877. ret = gsl_rpc_read(recv_buf, &rpc_shadow, sizeof(rpc_shadow));
  878. if (ret) {
  879. LOGE("gsl_rpc_read failed, %d", ret);
  880. goto out;
  881. }
  882. ret = gsl_rpc_read_uint32_l(recv_buf, &export_id);
  883. if ((ret) || (export_id == 0)) {
  884. LOGE("gsl_rpc_read_uint32_l failed, %d", ret);
  885. goto out;
  886. }
  887. *shadow_ts_flags = rpc_shadow.flags;
  888. mem_node->memdesc.size64 = (uint64_t)rpc_shadow.sizebytes;
  889. mem_node->fd = -1;
  890. if (rpc_shadow.flags & GSL_FLAGS_INITIALIZED) {
  891. aligned_size = (rpc_shadow.sizebytes + (0x1000-1)) & ~(0x1000-1);
  892. ret = habmm_import(hab_channel->socket,
  893. (void **)&dma_buf, aligned_size,
  894. export_id, 0);
  895. if (ret) {
  896. LOGE("habmm_import failed, ret = %d", ret);
  897. goto out;
  898. }
  899. mem_node->export_id = export_id;
  900. mem_node->dma_buf = dma_buf;
  901. }
  902. out:
  903. LOGD("%d, 0x%x", ret, rpc_shadow.flags);
  904. RPC_TRACE_DONE();
  905. return ret;
  906. }
  907. int hgsl_hyp_put_shadowts_mem(struct hgsl_hab_channel_t *hab_channel,
  908. struct hgsl_mem_node *mem_node)
  909. {
  910. struct put_shadowmem_params_t rpc_params = { 0 };
  911. struct gsl_hab_payload *send_buf = NULL;
  912. struct gsl_hab_payload *recv_buf = NULL;
  913. int ret = 0;
  914. int rval = GSL_SUCCESS;
  915. RPC_TRACE();
  916. if (!hab_channel) {
  917. LOGE("hab_channel is NULL");
  918. goto out;
  919. }
  920. ret = hgsl_rpc_parcel_reset(hab_channel);
  921. if (ret) {
  922. LOGE("hgsl_rpc_parcel_reset failed %d", ret);
  923. goto out;
  924. }
  925. send_buf = &hab_channel->send_buf;
  926. recv_buf = &hab_channel->recv_buf;
  927. ret = habmm_unimport(hab_channel->socket,
  928. mem_node->export_id, mem_node->dma_buf, 0);
  929. if (ret != 0) {
  930. LOGE("habmm_unimport failed, ret = %d, export_id = %u",
  931. ret, mem_node->export_id);
  932. }
  933. rpc_params.size = sizeof(rpc_params);
  934. rpc_params.export_id = mem_node->export_id;
  935. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  936. if (ret) {
  937. LOGE("gsl_rpc_write failed, %d", ret);
  938. goto out;
  939. }
  940. ret = gsl_rpc_transact(RPC_PUT_SHADOWMEM, hab_channel);
  941. if (ret) {
  942. LOGE("gsl_rpc_transact failed, %d", ret);
  943. goto out;
  944. }
  945. ret = gsl_rpc_read_int32_l(recv_buf, &rval);
  946. if (ret) {
  947. LOGE("gsl_rpc_read_int32_l failed, %d", ret);
  948. goto out;
  949. }
  950. if (rval) {
  951. LOGE("RPC_PUT_SHADOWMEM failed, %d", rval);
  952. ret = -EINVAL;
  953. goto out;
  954. }
  955. out:
  956. LOGD("ShadowTS %d, %u", ret, mem_node->export_id);
  957. RPC_TRACE_DONE();
  958. return ret;
  959. }
  960. int hgsl_hyp_mem_map_smmu(struct hgsl_hab_channel_t *hab_channel,
  961. uint64_t size, uint64_t offset,
  962. struct hgsl_mem_node *mem_node)
  963. {
  964. struct memory_map_ext_fd_params_t rpc_params = { 0 };
  965. struct gsl_hab_payload *send_buf = NULL;
  966. struct gsl_hab_payload *recv_buf = NULL;
  967. int ret = 0;
  968. int rval = 0;
  969. int hab_exp_flags = 0;
  970. void *hab_exp_handle = NULL;
  971. uint32_t export_id = 0;
  972. RPC_TRACE();
  973. if (!hab_channel) {
  974. LOGE("invalid hab_channel");
  975. ret = -EINVAL;
  976. goto out;
  977. }
  978. ret = hgsl_rpc_parcel_reset(hab_channel);
  979. if (ret) {
  980. LOGE("hgsl_rpc_parcel_reset failed %d", ret);
  981. goto out;
  982. }
  983. send_buf = &hab_channel->send_buf;
  984. recv_buf = &hab_channel->recv_buf;
  985. if (mem_node->fd >= 0) {
  986. hab_exp_flags = HABMM_EXPIMP_FLAGS_FD;
  987. hab_exp_handle = (void *)((uintptr_t)mem_node->fd);
  988. } else if (mem_node->dma_buf) {
  989. hab_exp_flags = HABMM_EXPIMP_FLAGS_DMABUF;
  990. hab_exp_handle = (void *)mem_node->dma_buf;
  991. } else {
  992. //todo, add support for uva
  993. ret = -EINVAL;
  994. goto out;
  995. }
  996. ret = habmm_export(hab_channel->socket, hab_exp_handle,
  997. size, &export_id, hab_exp_flags);
  998. if (ret) {
  999. LOGE("export failed, fd(%d), dma_buf(%p), offset(%d)",
  1000. mem_node->fd, mem_node->dma_buf,
  1001. offset);
  1002. LOGE("size(%d), hab flags(0x%X) ret(%d)",
  1003. size, hab_exp_flags, ret);
  1004. goto out;
  1005. }
  1006. rpc_params.size = sizeof(rpc_params);
  1007. rpc_params.fd = mem_node->fd;
  1008. rpc_params.hostptr = (uintptr_t) 0;
  1009. rpc_params.len = size;
  1010. rpc_params.offset = offset;
  1011. rpc_params.memtype = mem_node->memtype;
  1012. rpc_params.flags = mem_node->flags;
  1013. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  1014. if (ret) {
  1015. LOGE("gsl_rpc_write failed, %d", ret);
  1016. goto out;
  1017. }
  1018. ret = gsl_rpc_write_uint32(send_buf, export_id);
  1019. if (ret) {
  1020. LOGE("gsl_rpc_write failed, %d", ret);
  1021. goto out;
  1022. }
  1023. ret = gsl_rpc_transact(RPC_MEMORY_MAP_EXT_FD_PURE, hab_channel);
  1024. if (ret) {
  1025. LOGE("gsl_rpc_transact failed, %d", ret);
  1026. goto out;
  1027. }
  1028. ret = gsl_rpc_read(recv_buf, &mem_node->memdesc,
  1029. sizeof(mem_node->memdesc));
  1030. if (ret) {
  1031. LOGE("gsl_rpc_read failed, %d", ret);
  1032. goto out;
  1033. }
  1034. ret = gsl_rpc_read_int32_l(recv_buf, &rval);
  1035. if (ret) {
  1036. LOGE("gsl_rpc_read_int32_l failed, %d", ret);
  1037. goto out;
  1038. }
  1039. if (rval != GSL_SUCCESS) {
  1040. LOGE("RPC_MEMORY_MAP_EXT_FD_PURE failed, %d, %d, %d, %d",
  1041. rval, mem_node->memtype, mem_node->fd, export_id);
  1042. ret = -EINVAL;
  1043. goto out;
  1044. }
  1045. out:
  1046. mem_node->export_id = export_id;
  1047. LOGD("mem_map_smmu: export_id(%d), size(%d), flags(0x%x), priv(0x%lx), fd(%d), ret(%d)",
  1048. export_id, rpc_params.len, rpc_params.flags, mem_node->memdesc.priv64,
  1049. mem_node->fd, ret);
  1050. RPC_TRACE_DONE();
  1051. return ret;
  1052. }
  1053. int hgsl_hyp_mem_unmap_smmu(struct hgsl_hab_channel_t *hab_channel,
  1054. struct hgsl_mem_node *mem_node)
  1055. {
  1056. struct memory_unmap_ext_fd_params rpc_params = { 0 };
  1057. struct gsl_hab_payload *send_buf = NULL;
  1058. struct gsl_hab_payload *recv_buf = NULL;
  1059. int ret = 0;
  1060. int rval = 0;
  1061. RPC_TRACE();
  1062. if (mem_node == NULL) {
  1063. LOGE("invalid mem node");
  1064. ret = -EINVAL;
  1065. goto out;
  1066. }
  1067. if (!hab_channel) {
  1068. LOGE("invalid hab_channel");
  1069. ret = -EINVAL;
  1070. goto out;
  1071. }
  1072. ret = hgsl_rpc_parcel_reset(hab_channel);
  1073. if (ret) {
  1074. LOGE("hgsl_rpc_parcel_reset failed %d", ret);
  1075. goto out;
  1076. }
  1077. send_buf = &hab_channel->send_buf;
  1078. recv_buf = &hab_channel->recv_buf;
  1079. if (mem_node->memdesc.gpuaddr) {
  1080. rpc_params.size = sizeof(rpc_params);
  1081. rpc_params.gpuaddr = mem_node->memdesc.gpuaddr;
  1082. rpc_params.hostptr = (uintptr_t) mem_node->memdesc.hostptr64;
  1083. rpc_params.len = mem_node->memdesc.size;
  1084. rpc_params.memtype = mem_node->memtype;
  1085. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  1086. if (ret) {
  1087. LOGE("gsl_rpc_write failed, %d", ret);
  1088. goto out;
  1089. }
  1090. ret = gsl_rpc_write_uint32(send_buf, mem_node->export_id);
  1091. if (ret) {
  1092. LOGE("gsl_rpc_write failed, %d", ret);
  1093. goto out;
  1094. }
  1095. ret = gsl_rpc_transact(RPC_MEMORY_UNMAP_EXT_FD_PURE, hab_channel);
  1096. if (ret) {
  1097. LOGE("gsl_rpc_transact failed, %d", ret);
  1098. goto out;
  1099. }
  1100. ret = gsl_rpc_read_int32_l(recv_buf, &rval);
  1101. if (ret) {
  1102. LOGE("gsl_rpc_read_int32_l failed, %d", ret);
  1103. goto out;
  1104. }
  1105. if (rval != GSL_SUCCESS) {
  1106. LOGE("RPC_MEMORY_UNMAP_EXT_FD_PURE failed, %d", ret);
  1107. ret = -EINVAL;
  1108. goto out;
  1109. }
  1110. }
  1111. if (mem_node->export_id) {
  1112. ret = habmm_unexport(hab_channel->socket,
  1113. mem_node->export_id, 0);
  1114. if (ret) {
  1115. LOGE("habmm_unexport failed, socket %d export_id %d",
  1116. hab_channel->socket,
  1117. mem_node->export_id);
  1118. goto out;
  1119. }
  1120. }
  1121. out:
  1122. RPC_TRACE_DONE();
  1123. return ret;
  1124. }
  1125. int hgsl_hyp_set_metainfo(struct hgsl_hyp_priv_t *priv,
  1126. struct hgsl_ioctl_set_metainfo_params *hgsl_param,
  1127. const char *metainfo)
  1128. {
  1129. struct memory_set_metainfo_params_t rpc_params = { 0 };
  1130. struct hgsl_hab_channel_t *hab_channel = NULL;
  1131. struct gsl_hab_payload *send_buf = NULL;
  1132. struct gsl_hab_payload *recv_buf = NULL;
  1133. int ret = 0;
  1134. int rval = 0;
  1135. RPC_TRACE();
  1136. ret = hgsl_hyp_channel_pool_get(priv, 0, &hab_channel);
  1137. if (ret) {
  1138. LOGE("Failed to get hab channel %d", ret);
  1139. goto out;
  1140. }
  1141. send_buf = &hab_channel->send_buf;
  1142. recv_buf = &hab_channel->recv_buf;
  1143. rpc_params.memdesc_priv = hgsl_param->memdesc_priv;
  1144. rpc_params.flags = hgsl_param->flags;
  1145. rpc_params.metainfo_len = hgsl_param->metainfo_len;
  1146. strscpy(rpc_params.metainfo, metainfo, sizeof(rpc_params.metainfo));
  1147. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  1148. if (ret) {
  1149. LOGE("gsl_rpc_write failed, %d", ret);
  1150. goto out;
  1151. }
  1152. ret = gsl_rpc_transact(RPC_MEMORY_SET_METAINFO, hab_channel);
  1153. if (ret) {
  1154. LOGE("gsl_rpc_transact failed, %d", ret);
  1155. goto out;
  1156. }
  1157. ret = gsl_rpc_read_int32_l(recv_buf, &rval);
  1158. if (ret) {
  1159. LOGE("gsl_rpc_read failed, %d", ret);
  1160. goto out;
  1161. }
  1162. if (rval != GSL_SUCCESS) {
  1163. LOGE("RPC_MEMORY_SET_METAINFO failed, %d", rval);
  1164. ret = -EINVAL;
  1165. goto out;
  1166. }
  1167. out:
  1168. if (ret)
  1169. LOGE("ret %d", ret);
  1170. hgsl_hyp_channel_pool_put(hab_channel);
  1171. RPC_TRACE_DONE();
  1172. return ret;
  1173. }
  1174. int hgsl_hyp_issueib(struct hgsl_hyp_priv_t *priv,
  1175. struct hgsl_ioctl_issueib_params *hgsl_param,
  1176. struct hgsl_ibdesc *ib)
  1177. {
  1178. struct command_issueib_params_t rpc_params = { 0 };
  1179. struct hgsl_hab_channel_t *hab_channel = NULL;
  1180. struct gsl_hab_payload *send_buf = NULL;
  1181. struct gsl_hab_payload *recv_buf = NULL;
  1182. struct hyp_ibdesc_t hyp_ibdesc = {0};
  1183. int ret = 0;
  1184. unsigned int i = 0;
  1185. RPC_TRACE();
  1186. ret = hgsl_hyp_channel_pool_get(priv, hgsl_param->channel_id, &hab_channel);
  1187. if (ret) {
  1188. LOGE("Failed to get hab channel %d", ret);
  1189. goto out;
  1190. }
  1191. send_buf = &hab_channel->send_buf;
  1192. recv_buf = &hab_channel->recv_buf;
  1193. if (!hab_channel->wait_retry) {
  1194. if (ib == NULL) {
  1195. ret = -EINVAL;
  1196. goto out;
  1197. }
  1198. rpc_params.size = sizeof(rpc_params);
  1199. rpc_params.devhandle = hgsl_param->devhandle;
  1200. rpc_params.ctxthandle = hgsl_param->ctxthandle;
  1201. rpc_params.timestamp = hgsl_param->timestamp;
  1202. rpc_params.flags = hgsl_param->flags;
  1203. rpc_params.numibs = hgsl_param->num_ibs;
  1204. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  1205. if (ret) {
  1206. LOGE("gsl_rpc_write failed, %d", ret);
  1207. goto out;
  1208. }
  1209. for (i = 0 ; i < rpc_params.numibs; i++) {
  1210. hyp_ibdesc.gpuaddr = ib[i].gpuaddr;
  1211. hyp_ibdesc.sizedwords = (uint32_t)ib[i].sizedwords;
  1212. ret = gsl_rpc_write(send_buf, (void *)&hyp_ibdesc,
  1213. sizeof(hyp_ibdesc));
  1214. if (ret) {
  1215. LOGE("gsl_rpc_write failed, %d", ret);
  1216. goto out;
  1217. }
  1218. }
  1219. }
  1220. ret = gsl_rpc_transact_interrruptible(RPC_COMMAND_ISSUEIB, hab_channel);
  1221. if (ret == -EINTR) {
  1222. hgsl_param->channel_id = hab_channel->id;
  1223. return ret;
  1224. } else if (ret) {
  1225. LOGE("gsl_rpc_transact_interrruptible failed, %d", ret);
  1226. goto out;
  1227. }
  1228. ret = gsl_rpc_read(recv_buf,
  1229. (void *)&hgsl_param->timestamp, sizeof(hgsl_param->timestamp));
  1230. if (ret) {
  1231. LOGE("gsl_rpc_read failed, %d", ret);
  1232. goto out;
  1233. }
  1234. ret = gsl_rpc_read_int32_l(recv_buf, &hgsl_param->rval);
  1235. if (ret) {
  1236. LOGE("gsl_rpc_read failed, %d", ret);
  1237. goto out;
  1238. }
  1239. LOGD("%d, %d, %d", hgsl_param->ctxthandle, hgsl_param->timestamp, ret);
  1240. out:
  1241. hgsl_hyp_channel_pool_put(hab_channel);
  1242. RPC_TRACE_DONE();
  1243. return ret;
  1244. }
  1245. int hgsl_hyp_issueib_with_alloc_list(struct hgsl_hyp_priv_t *priv,
  1246. struct hgsl_ioctl_issueib_with_alloc_list_params *hgsl_param,
  1247. struct gsl_command_buffer_object_t *ib,
  1248. struct gsl_memory_object_t *allocations,
  1249. struct gsl_memdesc_t *be_descs,
  1250. uint64_t *be_offsets)
  1251. {
  1252. struct command_issueib_with_alloc_list_params rpc_params = { 0 };
  1253. struct hgsl_hab_channel_t *hab_channel = NULL;
  1254. struct gsl_hab_payload *send_buf = NULL;
  1255. struct gsl_hab_payload *recv_buf = NULL;
  1256. int ret = 0;
  1257. unsigned int i = 0;
  1258. size_t size = 0;
  1259. RPC_TRACE();
  1260. ret = hgsl_hyp_channel_pool_get(priv, hgsl_param->channel_id, &hab_channel);
  1261. if (ret) {
  1262. LOGE("Failed to get hab channel %d", ret);
  1263. goto out;
  1264. }
  1265. send_buf = &hab_channel->send_buf;
  1266. recv_buf = &hab_channel->recv_buf;
  1267. if (!hab_channel->wait_retry) {
  1268. if ((ib == NULL) ||
  1269. ((allocations == NULL) && (hgsl_param->num_allocations)) ||
  1270. (be_descs == NULL) || (be_offsets == NULL)) {
  1271. LOGE("Invalid input");
  1272. ret = -EINVAL;
  1273. goto out;
  1274. }
  1275. rpc_params.size = sizeof(rpc_params);
  1276. rpc_params.devhandle = hgsl_param->devhandle;
  1277. rpc_params.ctxthandle = hgsl_param->ctxthandle;
  1278. rpc_params.timestamp = hgsl_param->timestamp;
  1279. rpc_params.flags = hgsl_param->flags;
  1280. rpc_params.numibs = hgsl_param->num_ibs;
  1281. rpc_params.numallocations = hgsl_param->num_allocations;
  1282. rpc_params.syncobj = (uint64_t) hgsl_param->rpc_syncobj;
  1283. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  1284. if (ret) {
  1285. LOGE("gsl_rpc_write failed, %d", ret);
  1286. goto out;
  1287. }
  1288. size = sizeof(struct gsl_command_buffer_object_t) * rpc_params.numibs;
  1289. ret = gsl_rpc_write(send_buf, (void *)ib, size);
  1290. if (ret) {
  1291. LOGE("gsl_rpc_write failed, %d", ret);
  1292. goto out;
  1293. }
  1294. /* Send all ib[].memdesc together after the entire ib[] array */
  1295. for (i = 0; i < rpc_params.numibs; i++) {
  1296. ret = gsl_rpc_write(send_buf, &be_descs[i],
  1297. sizeof(struct gsl_memdesc_t));
  1298. if (ret) {
  1299. LOGE("gsl_rpc_write failed, %d", ret);
  1300. goto out;
  1301. }
  1302. ret = gsl_rpc_write_uint64(send_buf, be_offsets[i]);
  1303. if (ret) {
  1304. LOGE("gsl_rpc_write failed, %d", ret);
  1305. goto out;
  1306. }
  1307. }
  1308. if (rpc_params.numallocations) {
  1309. size = sizeof(struct gsl_memory_object_t) *
  1310. rpc_params.numallocations;
  1311. ret = gsl_rpc_write(send_buf, (void *)allocations, size);
  1312. if (ret) {
  1313. LOGE("gsl_rpc_write failed, %d", ret);
  1314. goto out;
  1315. }
  1316. }
  1317. for (i = 0; i < rpc_params.numallocations; i++) {
  1318. ret = gsl_rpc_write(send_buf,
  1319. &be_descs[i + rpc_params.numibs],
  1320. sizeof(struct gsl_memdesc_t));
  1321. if (ret) {
  1322. LOGE("gsl_rpc_write failed, %d", ret);
  1323. goto out;
  1324. }
  1325. ret = gsl_rpc_write_uint64(send_buf,
  1326. be_offsets[i + rpc_params.numibs]);
  1327. if (ret) {
  1328. LOGE("gsl_rpc_write failed, %d", ret);
  1329. goto out;
  1330. }
  1331. }
  1332. }
  1333. ret = gsl_rpc_transact_interrruptible(RPC_COMMAND_ISSUEIB_WITH_ALLOC_LIST,
  1334. hab_channel);
  1335. if (ret == -EINTR) {
  1336. hgsl_param->channel_id = hab_channel->id;
  1337. return ret;
  1338. } else if (ret) {
  1339. LOGE("gsl_rpc_transact_interrruptible failed, %d", ret);
  1340. goto out;
  1341. }
  1342. ret = gsl_rpc_read(recv_buf,
  1343. (void *)&hgsl_param->timestamp, sizeof(hgsl_param->timestamp));
  1344. if (ret) {
  1345. LOGE("gsl_rpc_read failed, %d", ret);
  1346. goto out;
  1347. }
  1348. ret = gsl_rpc_read_int32_l(recv_buf, &hgsl_param->rval);
  1349. if (ret) {
  1350. LOGE("gsl_rpc_read failed, %d", ret);
  1351. goto out;
  1352. }
  1353. LOGD("%d, %d, %d", hgsl_param->ctxthandle, hgsl_param->timestamp, ret);
  1354. out:
  1355. hgsl_hyp_channel_pool_put(hab_channel);
  1356. RPC_TRACE_DONE();
  1357. return ret;
  1358. }
  1359. int hgsl_hyp_wait_timestamp(struct hgsl_hyp_priv_t *priv,
  1360. struct hgsl_wait_ts_info *hgsl_param)
  1361. {
  1362. struct command_waittimestamp_params_t rpc_params = { 0 };
  1363. struct hgsl_hab_channel_t *hab_channel = NULL;
  1364. struct gsl_hab_payload *send_buf = NULL;
  1365. struct gsl_hab_payload *recv_buf = NULL;
  1366. uint32_t timeout = hgsl_param->timeout;
  1367. int ret = 0;
  1368. int rval = 0;
  1369. RPC_TRACE();
  1370. ret = hgsl_hyp_channel_pool_get(priv, hgsl_param->channel_id, &hab_channel);
  1371. if (ret) {
  1372. LOGE("Failed to get hab channel %d", ret);
  1373. goto out;
  1374. }
  1375. send_buf = &hab_channel->send_buf;
  1376. recv_buf = &hab_channel->recv_buf;
  1377. if (!hab_channel->wait_retry) {
  1378. hgsl_rpc_parcel_reset(hab_channel);
  1379. rpc_params.size = sizeof(rpc_params);
  1380. rpc_params.devhandle = hgsl_param->devhandle;
  1381. rpc_params.ctxthandle = hgsl_param->context_id;
  1382. rpc_params.timestamp = hgsl_param->timestamp;
  1383. if ((timeout == GSL_TIMEOUT_INFINITE) ||
  1384. (timeout >= GSL_RPC_WAITTIMESTAMP_SLICE))
  1385. rpc_params.timeout = GSL_RPC_WAITTIMESTAMP_SLICE;
  1386. else
  1387. rpc_params.timeout = hgsl_param->timeout;
  1388. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  1389. if (ret) {
  1390. LOGE("gsl_rpc_write failed, %d", ret);
  1391. goto out;
  1392. }
  1393. }
  1394. ret = gsl_rpc_transact_interrruptible(RPC_COMMAND_WAITTIMESTAMP, hab_channel);
  1395. if (ret == -EINTR) {
  1396. hgsl_param->channel_id = hab_channel->id;
  1397. return ret;
  1398. } else if (ret) {
  1399. LOGE("gsl_rpc_transact_interrruptible failed, %d", ret);
  1400. goto out;
  1401. }
  1402. ret = gsl_rpc_read_int32_l(recv_buf, &rval);
  1403. if (ret) {
  1404. LOGE("gsl_rpc_read_int32_l failed, %d", ret);
  1405. goto out;
  1406. }
  1407. if (rval != GSL_SUCCESS) {
  1408. ret = (rval == GSL_FAILURE_TIMEOUT) ? -ETIMEDOUT : -EINVAL;
  1409. goto out;
  1410. }
  1411. LOGD("%d, %d, %d", hgsl_param->context_id, hgsl_param->timestamp, ret);
  1412. out:
  1413. hgsl_hyp_channel_pool_put(hab_channel);
  1414. RPC_TRACE_DONE();
  1415. return ret;
  1416. }
  1417. int hgsl_hyp_read_timestamp(struct hgsl_hyp_priv_t *priv,
  1418. struct hgsl_ioctl_read_ts_params *hgsl_param)
  1419. {
  1420. struct command_readtimestamp_params_t rpc_params = { 0 };
  1421. struct hgsl_hab_channel_t *hab_channel = NULL;
  1422. struct gsl_hab_payload *send_buf = NULL;
  1423. struct gsl_hab_payload *recv_buf = NULL;
  1424. int ret = 0;
  1425. int rval = 0;
  1426. RPC_TRACE();
  1427. ret = hgsl_hyp_channel_pool_get(priv, 0, &hab_channel);
  1428. if (ret) {
  1429. LOGE("Failed to get hab channel %d", ret);
  1430. goto out;
  1431. }
  1432. send_buf = &hab_channel->send_buf;
  1433. recv_buf = &hab_channel->recv_buf;
  1434. rpc_params.size = sizeof(rpc_params);
  1435. rpc_params.devhandle = hgsl_param->devhandle;
  1436. rpc_params.ctxthandle = hgsl_param->ctxthandle;
  1437. rpc_params.type = hgsl_param->type;
  1438. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  1439. if (ret) {
  1440. LOGE("gsl_rpc_write failed, %d", ret);
  1441. goto out;
  1442. }
  1443. ret = gsl_rpc_transact(RPC_COMMAND_READTIMESTAMP, hab_channel);
  1444. if (ret) {
  1445. LOGE("gsl_rpc_transact failed, %d", ret);
  1446. goto out;
  1447. }
  1448. ret = gsl_rpc_read_uint32_l(recv_buf, &hgsl_param->timestamp);
  1449. if (ret) {
  1450. LOGE("gsl_rpc_read_uint32_l failed, %d", ret);
  1451. goto out;
  1452. }
  1453. ret = gsl_rpc_read_int32_l(recv_buf, &rval);
  1454. if (ret) {
  1455. LOGE("gsl_rpc_read_int32_l failed, %d", ret);
  1456. goto out;
  1457. }
  1458. if (rval != GSL_SUCCESS) {
  1459. LOGE("RPC_COMMAND_READTIMESTAMP failed, %d", rval);
  1460. ret = -EINVAL;
  1461. goto out;
  1462. }
  1463. LOGD("%d, %d, %d, %d", hgsl_param->ctxthandle,
  1464. hgsl_param->type, hgsl_param->timestamp, ret);
  1465. out:
  1466. hgsl_hyp_channel_pool_put(hab_channel);
  1467. RPC_TRACE_DONE();
  1468. return ret;
  1469. }
  1470. int hgsl_hyp_check_timestamp(struct hgsl_hyp_priv_t *priv,
  1471. struct hgsl_ioctl_check_ts_params *hgsl_param)
  1472. {
  1473. struct command_checktimestamp_params_t rpc_params = { 0 };
  1474. struct hgsl_hab_channel_t *hab_channel = NULL;
  1475. struct gsl_hab_payload *send_buf = NULL;
  1476. struct gsl_hab_payload *recv_buf = NULL;
  1477. int ret = 0;
  1478. RPC_TRACE();
  1479. ret = hgsl_hyp_channel_pool_get(priv, 0, &hab_channel);
  1480. if (ret) {
  1481. LOGE("Failed to get hab channel %d", ret);
  1482. goto out;
  1483. }
  1484. send_buf = &hab_channel->send_buf;
  1485. recv_buf = &hab_channel->recv_buf;
  1486. rpc_params.size = sizeof(rpc_params);
  1487. rpc_params.devhandle = hgsl_param->devhandle;
  1488. rpc_params.ctxthandle = hgsl_param->ctxthandle;
  1489. rpc_params.timestamp = hgsl_param->timestamp;
  1490. rpc_params.type = hgsl_param->type;
  1491. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  1492. if (ret) {
  1493. LOGE("gsl_rpc_write failed, %d", ret);
  1494. goto out;
  1495. }
  1496. ret = gsl_rpc_transact(RPC_COMMAND_CHECKTIMESTAMP, hab_channel);
  1497. if (ret) {
  1498. LOGE("gsl_rpc_transact failed, %d", ret);
  1499. goto out;
  1500. }
  1501. ret = gsl_rpc_read_int32_l(recv_buf, &hgsl_param->rval);
  1502. if (ret) {
  1503. LOGE("gsl_rpc_read_int32_l failed, %d", ret);
  1504. goto out;
  1505. }
  1506. LOGD("%d, %d, %d, %d", hgsl_param->ctxthandle,
  1507. hgsl_param->type, hgsl_param->timestamp, hgsl_param->rval);
  1508. out:
  1509. hgsl_hyp_channel_pool_put(hab_channel);
  1510. RPC_TRACE_DONE();
  1511. return ret;
  1512. }
  1513. int hgsl_hyp_get_system_time(struct hgsl_hyp_priv_t *priv,
  1514. uint64_t *hgsl_param)
  1515. {
  1516. struct get_system_time_params_t rpc_params = {0};
  1517. struct hgsl_hab_channel_t *hab_channel = NULL;
  1518. struct gsl_hab_payload *send_buf = NULL;
  1519. struct gsl_hab_payload *recv_buf = NULL;
  1520. int ret = 0;
  1521. int rval = 0;
  1522. uint64_t time_us = 0;
  1523. enum gsl_systemtime_usage_t usage
  1524. = (enum gsl_systemtime_usage_t)*hgsl_param;
  1525. RPC_TRACE();
  1526. ret = hgsl_hyp_channel_pool_get(priv, 0, &hab_channel);
  1527. if (ret) {
  1528. LOGE("Failed to get hab channel %d", ret);
  1529. goto out;
  1530. }
  1531. send_buf = &hab_channel->send_buf;
  1532. recv_buf = &hab_channel->recv_buf;
  1533. rpc_params.size = sizeof(rpc_params);
  1534. rpc_params.usage = usage;
  1535. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  1536. if (ret) {
  1537. LOGE("gsl_rpc_write failed, %d", ret);
  1538. goto out;
  1539. }
  1540. ret = gsl_rpc_transact(RPC_GET_SYSTEM_TIME, hab_channel);
  1541. if (ret) {
  1542. LOGE("gsl_rpc_transact failed, %d", ret);
  1543. goto out;
  1544. }
  1545. ret = gsl_rpc_read_int32_l(recv_buf, &rval);
  1546. if (ret) {
  1547. LOGE("gsl_rpc_read_int32_l failed, %d", ret);
  1548. goto out;
  1549. }
  1550. if (rval != GSL_SUCCESS) {
  1551. LOGE("RPC_GET_SYSTEM_TIME failed, rval %d", rval);
  1552. ret = -EINVAL;
  1553. goto out;
  1554. }
  1555. ret = gsl_rpc_read_uint64_l(recv_buf, &time_us);
  1556. if (ret) {
  1557. LOGE("gsl_rpc_read_uint32_l failed, %d", ret);
  1558. goto out;
  1559. }
  1560. *hgsl_param = time_us;
  1561. ret = rval;
  1562. out:
  1563. hgsl_hyp_channel_pool_put(hab_channel);
  1564. RPC_TRACE_DONE();
  1565. return ret;
  1566. }
  1567. int hgsl_hyp_syncobj_wait_multiple(struct hgsl_hyp_priv_t *priv,
  1568. uint64_t *rpc_syncobj, uint64_t num_syncobjs,
  1569. uint32_t timeout_ms, int32_t *status, int32_t *result)
  1570. {
  1571. struct syncobj_wait_multiple_params_t rpc_params = { };
  1572. struct hgsl_hab_channel_t *hab_channel = NULL;
  1573. struct gsl_hab_payload *send_buf = NULL;
  1574. struct gsl_hab_payload *recv_buf = NULL;
  1575. int ret = 0;
  1576. uint64_t i = 0;
  1577. rpc_params.size = sizeof(rpc_params);
  1578. rpc_params.num_syncobjs = num_syncobjs;
  1579. rpc_params.timeout_ms = timeout_ms;
  1580. RPC_TRACE();
  1581. ret = hgsl_hyp_channel_pool_get(priv, 0, &hab_channel);
  1582. if (ret) {
  1583. LOGE("Failed to get hab channel %d", ret);
  1584. goto out;
  1585. }
  1586. send_buf = &hab_channel->send_buf;
  1587. recv_buf = &hab_channel->recv_buf;
  1588. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  1589. if (ret) {
  1590. LOGE("gsl_rpc_write failed, %d", ret);
  1591. goto out;
  1592. }
  1593. for (i = 0; i < num_syncobjs; ++i) {
  1594. ret = gsl_rpc_write(send_buf, &rpc_syncobj[i],
  1595. sizeof(uint64_t));
  1596. if (ret) {
  1597. LOGE("gsl_rpc_write failed, %d", ret);
  1598. goto out;
  1599. }
  1600. }
  1601. ret = gsl_rpc_transact(RPC_SYNCOBJ_WAIT_MULTIPLE, hab_channel);
  1602. if (ret) {
  1603. LOGE("gsl_rpc_transact failed, %d", ret);
  1604. goto out;
  1605. }
  1606. ret = gsl_rpc_read(recv_buf, status, sizeof(int32_t) * num_syncobjs);
  1607. if (ret) {
  1608. LOGE("gsl_rpc_read failed, %d", ret);
  1609. goto out;
  1610. }
  1611. ret = gsl_rpc_read_int32_l(recv_buf, result);
  1612. if (ret) {
  1613. LOGE("gsl_rpc_read_int32_l failed, %d", ret);
  1614. goto out;
  1615. }
  1616. out:
  1617. hgsl_hyp_channel_pool_put(hab_channel);
  1618. RPC_TRACE_DONE();
  1619. return ret;
  1620. }
  1621. int hgsl_hyp_perfcounter_select(struct hgsl_hyp_priv_t *priv,
  1622. struct hgsl_ioctl_perfcounter_select_params *hgsl_param,
  1623. uint32_t *groups, uint32_t *counter_ids,
  1624. uint32_t *counter_val_regs, uint32_t *counter_val_hi_regs)
  1625. {
  1626. struct perfcounter_select_params_t rpc_params = { };
  1627. struct hgsl_hab_channel_t *hab_channel = NULL;
  1628. struct gsl_hab_payload *send_buf = NULL;
  1629. struct gsl_hab_payload *recv_buf = NULL;
  1630. int ret = 0;
  1631. rpc_params.size = sizeof(rpc_params);
  1632. rpc_params.devhandle = hgsl_param->devhandle;
  1633. rpc_params.ctxthandle = hgsl_param->ctxthandle;
  1634. rpc_params.num_counters = hgsl_param->num_counters;
  1635. RPC_TRACE();
  1636. ret = hgsl_hyp_channel_pool_get(priv, 0, &hab_channel);
  1637. if (ret) {
  1638. LOGE("Failed to get hab channel %d", ret);
  1639. goto out;
  1640. }
  1641. send_buf = &hab_channel->send_buf;
  1642. recv_buf = &hab_channel->recv_buf;
  1643. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  1644. if (ret) {
  1645. LOGE("gsl_rpc_write failed, %d", ret);
  1646. goto out;
  1647. }
  1648. ret = gsl_rpc_write(send_buf, groups,
  1649. sizeof(uint32_t) * hgsl_param->num_counters);
  1650. if (ret) {
  1651. LOGE("gsl_rpc_write failed, %d", ret);
  1652. goto out;
  1653. }
  1654. ret = gsl_rpc_write(send_buf, counter_ids,
  1655. sizeof(uint32_t) * hgsl_param->num_counters);
  1656. if (ret) {
  1657. LOGE("gsl_rpc_write failed, %d", ret);
  1658. goto out;
  1659. }
  1660. ret = gsl_rpc_transact(RPC_PERFCOUNTER_SELECT, hab_channel);
  1661. if (ret) {
  1662. LOGE("gsl_rpc_transact failed, %d", ret);
  1663. goto out;
  1664. }
  1665. ret = gsl_rpc_read_int32_l(recv_buf, &hgsl_param->rval);
  1666. if (ret) {
  1667. LOGE("gsl_rpc_read_int32_l failed, %d", ret);
  1668. goto out;
  1669. }
  1670. if (hgsl_param->rval == GSL_SUCCESS) {
  1671. ret = gsl_rpc_read(recv_buf, counter_val_regs,
  1672. sizeof(uint32_t) * hgsl_param->num_counters);
  1673. if (ret) {
  1674. LOGE("gsl_rpc_read failed, %d", ret);
  1675. goto out;
  1676. }
  1677. ret = gsl_rpc_read(recv_buf, counter_val_hi_regs,
  1678. sizeof(uint32_t) * hgsl_param->num_counters);
  1679. if (ret) {
  1680. LOGE("gsl_rpc_read failed, %d", ret);
  1681. goto out;
  1682. }
  1683. }
  1684. out:
  1685. hgsl_hyp_channel_pool_put(hab_channel);
  1686. RPC_TRACE_DONE();
  1687. return ret;
  1688. }
  1689. int hgsl_hyp_perfcounter_deselect(struct hgsl_hyp_priv_t *priv,
  1690. struct hgsl_ioctl_perfcounter_deselect_params *hgsl_param,
  1691. uint32_t *groups, uint32_t *counter_ids)
  1692. {
  1693. struct perfcounter_deselect_params_t rpc_params = { };
  1694. struct hgsl_hab_channel_t *hab_channel = NULL;
  1695. struct gsl_hab_payload *send_buf = NULL;
  1696. struct gsl_hab_payload *recv_buf = NULL;
  1697. int ret = 0;
  1698. rpc_params.size = sizeof(rpc_params);
  1699. rpc_params.devhandle = hgsl_param->devhandle;
  1700. rpc_params.ctxthandle = hgsl_param->ctxthandle;
  1701. rpc_params.timestamp = hgsl_param->timestamp;
  1702. rpc_params.num_counters = hgsl_param->num_counters;
  1703. RPC_TRACE();
  1704. ret = hgsl_hyp_channel_pool_get(priv, 0, &hab_channel);
  1705. if (ret) {
  1706. LOGE("Failed to get hab channel %d", ret);
  1707. goto out;
  1708. }
  1709. send_buf = &hab_channel->send_buf;
  1710. recv_buf = &hab_channel->recv_buf;
  1711. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  1712. if (ret) {
  1713. LOGE("gsl_rpc_write failed, %d", ret);
  1714. goto out;
  1715. }
  1716. ret = gsl_rpc_write(send_buf, groups,
  1717. sizeof(uint32_t) * hgsl_param->num_counters);
  1718. if (ret) {
  1719. LOGE("gsl_rpc_write failed, %d", ret);
  1720. goto out;
  1721. }
  1722. ret = gsl_rpc_write(send_buf, counter_ids,
  1723. sizeof(uint32_t) * hgsl_param->num_counters);
  1724. if (ret) {
  1725. LOGE("gsl_rpc_write failed, %d", ret);
  1726. goto out;
  1727. }
  1728. ret = gsl_rpc_transact(RPC_PERFCOUNTER_DESELECT, hab_channel);
  1729. if (ret) {
  1730. LOGE("gsl_rpc_transact failed, %d", ret);
  1731. goto out;
  1732. }
  1733. out:
  1734. hgsl_hyp_channel_pool_put(hab_channel);
  1735. RPC_TRACE_DONE();
  1736. return ret;
  1737. }
  1738. int hgsl_hyp_perfcounter_query_selections(struct hgsl_hyp_priv_t *priv,
  1739. struct hgsl_ioctl_perfcounter_query_selections_params *hgsl_param,
  1740. int32_t *selections)
  1741. {
  1742. struct perfcounter_query_selections_params_t rpc_params = { };
  1743. struct hgsl_hab_channel_t *hab_channel = NULL;
  1744. struct gsl_hab_payload *send_buf = NULL;
  1745. struct gsl_hab_payload *recv_buf = NULL;
  1746. int ret = 0;
  1747. rpc_params.size = sizeof(rpc_params);
  1748. rpc_params.devhandle = hgsl_param->devhandle;
  1749. rpc_params.ctxthandle = hgsl_param->ctxthandle;
  1750. rpc_params.num_counters = hgsl_param->num_counters;
  1751. rpc_params.group = hgsl_param->group;
  1752. RPC_TRACE();
  1753. ret = hgsl_hyp_channel_pool_get(priv, 0, &hab_channel);
  1754. if (ret) {
  1755. LOGE("Failed to get hab channel %d", ret);
  1756. goto out;
  1757. }
  1758. send_buf = &hab_channel->send_buf;
  1759. recv_buf = &hab_channel->recv_buf;
  1760. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  1761. if (ret) {
  1762. LOGE("gsl_rpc_write failed, %d", ret);
  1763. goto out;
  1764. }
  1765. ret = gsl_rpc_transact(RPC_PERFCOUNTER_QUERYSELECTIONS, hab_channel);
  1766. if (ret) {
  1767. LOGE("gsl_rpc_transact failed, %d", ret);
  1768. goto out;
  1769. }
  1770. ret = gsl_rpc_read_int32_l(recv_buf, &hgsl_param->max_counters);
  1771. if (ret) {
  1772. LOGE("gsl_rpc_read_int32_l failed, %d", ret);
  1773. goto out;
  1774. }
  1775. /* -1 max_counters indicates query_selections is not supported */
  1776. if (-1 != hgsl_param->max_counters) {
  1777. /* num_counters == 0 means user only wants the max_counters */
  1778. if ((hgsl_param->num_counters != 0) && (selections != NULL)) {
  1779. ret = gsl_rpc_read(recv_buf, selections,
  1780. sizeof(int32_t) * hgsl_param->num_counters);
  1781. if (ret) {
  1782. LOGE("gsl_rpc_read failed, %d", ret);
  1783. goto out;
  1784. }
  1785. }
  1786. } else {
  1787. LOGE("not supported, group=%d, num_counters=%d",
  1788. hgsl_param->group, hgsl_param->num_counters);
  1789. }
  1790. out:
  1791. hgsl_hyp_channel_pool_put(hab_channel);
  1792. RPC_TRACE_DONE();
  1793. return ret;
  1794. }
  1795. int hgsl_hyp_perfcounter_read(struct hgsl_hyp_priv_t *priv,
  1796. struct hgsl_ioctl_perfcounter_read_params *hgsl_param)
  1797. {
  1798. struct perfcounter_read_params_t rpc_params = { };
  1799. struct hgsl_hab_channel_t *hab_channel = NULL;
  1800. struct gsl_hab_payload *send_buf = NULL;
  1801. struct gsl_hab_payload *recv_buf = NULL;
  1802. int ret = 0;
  1803. rpc_params.size = sizeof(rpc_params);
  1804. rpc_params.devhandle = hgsl_param->devhandle;
  1805. rpc_params.group = hgsl_param->group;
  1806. rpc_params.counter = hgsl_param->counter;
  1807. RPC_TRACE();
  1808. ret = hgsl_hyp_channel_pool_get(priv, 0, &hab_channel);
  1809. if (ret) {
  1810. LOGE("Failed to get hab channel %d", ret);
  1811. goto out;
  1812. }
  1813. send_buf = &hab_channel->send_buf;
  1814. recv_buf = &hab_channel->recv_buf;
  1815. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  1816. if (ret) {
  1817. LOGE("gsl_rpc_write failed, %d", ret);
  1818. goto out;
  1819. }
  1820. ret = gsl_rpc_transact(RPC_PERFCOUNTER_READ, hab_channel);
  1821. if (ret) {
  1822. LOGE("gsl_rpc_transact failed, %d", ret);
  1823. goto out;
  1824. }
  1825. ret = gsl_rpc_read_int32_l(recv_buf, &(hgsl_param->rval));
  1826. if (ret) {
  1827. LOGE("gsl_rpc_read_int32_l failed, %d", ret);
  1828. goto out;
  1829. }
  1830. if (hgsl_param->rval == GSL_SUCCESS) {
  1831. ret = gsl_rpc_read_uint64_l(recv_buf, &(hgsl_param->value));
  1832. if (ret) {
  1833. LOGE("gsl_rpc_read_uint64_l failed, %d", ret);
  1834. goto out;
  1835. }
  1836. }
  1837. out:
  1838. hgsl_hyp_channel_pool_put(hab_channel);
  1839. RPC_TRACE_DONE();
  1840. return ret;
  1841. }
  1842. int hgsl_hyp_dbq_create(struct hgsl_hab_channel_t *hab_channel,
  1843. uint32_t ctxthandle, uint32_t *dbq_info)
  1844. {
  1845. struct dbq_create_params_t rpc_params = { 0 };
  1846. struct gsl_hab_payload *send_buf = NULL;
  1847. struct gsl_hab_payload *recv_buf = NULL;
  1848. int ret = 0;
  1849. int rval = 0;
  1850. RPC_TRACE();
  1851. if (!hab_channel) {
  1852. LOGE("hab_channel is invalid");
  1853. ret = -EINVAL;
  1854. goto out;
  1855. }
  1856. ret = hgsl_rpc_parcel_reset(hab_channel);
  1857. if (ret) {
  1858. LOGE("hgsl_rpc_parcel_reset failed %d", ret);
  1859. goto out;
  1860. }
  1861. send_buf = &hab_channel->send_buf;
  1862. recv_buf = &hab_channel->recv_buf;
  1863. rpc_params.size = sizeof(rpc_params);
  1864. rpc_params.ctxthandle = ctxthandle;
  1865. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  1866. if (ret) {
  1867. LOGE("gsl_rpc_write failed, %d", ret);
  1868. goto out;
  1869. }
  1870. ret = gsl_rpc_transact(RPC_DBQ_CREATE, hab_channel);
  1871. if (ret) {
  1872. LOGE("gsl_rpc_transact failed, %d", ret);
  1873. goto out;
  1874. }
  1875. ret = gsl_rpc_read_int32_l(recv_buf, &rval);
  1876. if (ret) {
  1877. LOGE("gsl_rpc_read_int32_l failed, %d", ret);
  1878. goto out;
  1879. }
  1880. if (rval != GSL_SUCCESS) {
  1881. LOGI("RPC_DBQ_CREATE failed, %d", rval);
  1882. ret = -EINVAL;
  1883. goto out;
  1884. }
  1885. ret = gsl_rpc_read_uint32_l(recv_buf, dbq_info);
  1886. if (ret) {
  1887. LOGE("gsl_rpc_read_uint32_l failed, %d", ret);
  1888. goto out;
  1889. }
  1890. out:
  1891. LOGD("%d, 0x%x", ret, *dbq_info);
  1892. RPC_TRACE_DONE();
  1893. return ret;
  1894. }
  1895. int hgsl_hyp_get_dbq_info(struct hgsl_hyp_priv_t *priv, uint32_t dbq_idx,
  1896. struct hgsl_dbq_info *dbq_info)
  1897. {
  1898. struct get_dbq_info_params_t rpc_params = { 0 };
  1899. struct hgsl_hab_channel_t *hab_channel = NULL;
  1900. struct gsl_hab_payload *send_buf = NULL;
  1901. struct gsl_hab_payload *recv_buf = NULL;
  1902. int ret = 0;
  1903. struct dma_buf *dma_buf = NULL;
  1904. int rval = 0;
  1905. RPC_TRACE();
  1906. ret = hgsl_hyp_channel_pool_get(priv, 0, &hab_channel);
  1907. if (ret) {
  1908. LOGE("Failed to get hab channel %d", ret);
  1909. goto out;
  1910. }
  1911. send_buf = &hab_channel->send_buf;
  1912. recv_buf = &hab_channel->recv_buf;
  1913. rpc_params.size = sizeof(rpc_params);
  1914. rpc_params.q_idx = dbq_idx;
  1915. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  1916. if (ret) {
  1917. LOGE("gsl_rpc_write failed, %d", ret);
  1918. goto out;
  1919. }
  1920. ret = gsl_rpc_transact(RPC_GET_DBQ_INFO, hab_channel);
  1921. if (ret) {
  1922. LOGE("gsl_rpc_transact failed, %d", ret);
  1923. goto out;
  1924. }
  1925. ret = gsl_rpc_read_int32_l(recv_buf, &rval);
  1926. if (ret) {
  1927. LOGE("gsl_rpc_read_int32_l failed, %d", ret);
  1928. goto out;
  1929. }
  1930. if (rval != GSL_SUCCESS) {
  1931. LOGE("RPC_GET_DBQ_INFO failed, %d", rval);
  1932. ret = -EINVAL;
  1933. goto out;
  1934. }
  1935. ret = gsl_rpc_read_uint32_l(recv_buf, &dbq_info->export_id);
  1936. if (ret) {
  1937. LOGE("gsl_rpc_read_uint32_l failed, %d", ret);
  1938. goto out;
  1939. }
  1940. ret = gsl_rpc_read_uint32_l(recv_buf, &dbq_info->size);
  1941. if (ret) {
  1942. LOGE("gsl_rpc_read_uint32_l failed, %d", ret);
  1943. goto out;
  1944. }
  1945. LOGD("RPC_GET_DBQ_INFO: export_id(%d), size(%d)",
  1946. dbq_info->export_id, dbq_info->size);
  1947. ret = gsl_rpc_read_int32_l(recv_buf, &dbq_info->queue_off_dwords);
  1948. if (ret) {
  1949. LOGE("gsl_rpc_read_int32_l failed, %d, %d", ret, rval);
  1950. goto out;
  1951. }
  1952. ret = gsl_rpc_read_uint32_l(recv_buf, &dbq_info->queue_dwords);
  1953. if (ret) {
  1954. LOGE("gsl_rpc_read_uint32_l failed, %d", ret);
  1955. goto out;
  1956. }
  1957. ret = gsl_rpc_read_int32_l(recv_buf, &dbq_info->head_off_dwords);
  1958. if (ret) {
  1959. LOGE("gsl_rpc_read_int32_l failed, %d, %d", ret, rval);
  1960. goto out;
  1961. }
  1962. ret = gsl_rpc_read_uint32_l(recv_buf, &dbq_info->head_dwords);
  1963. if (ret) {
  1964. LOGE("gsl_rpc_read_uint32_l failed, %d", ret);
  1965. goto out;
  1966. }
  1967. ret = gsl_rpc_read_uint64_l(recv_buf, &dbq_info->gmuaddr);
  1968. if (ret)
  1969. LOGW("gsl_rpc_read_uint64_l failed, %d", ret);
  1970. else {
  1971. ret = gsl_rpc_read_uint32_l(recv_buf,
  1972. &dbq_info->ibdesc_max_size);
  1973. if (ret)
  1974. LOGW("gsl_rpc_read_uint32_l failed, %d", ret);
  1975. }
  1976. if (ret) {
  1977. dbq_info->ibdesc_max_size = 0;
  1978. ret = 0;
  1979. }
  1980. dbq_info->size = (dbq_info->size + (0x1000 - 1)) & (~(0x1000 - 1));
  1981. ret = habmm_import(hab_channel->socket,
  1982. (void **)&dma_buf, dbq_info->size,
  1983. dbq_info->export_id, 0);
  1984. if (ret) {
  1985. LOGE("habmm_import failed, ret = %d, export_id = %d",
  1986. ret, dbq_info->export_id);
  1987. goto out;
  1988. }
  1989. dbq_info->dma_buf = dma_buf;
  1990. /*hab requires to use same socket for unexport */
  1991. dbq_info->hab_channel = hab_channel;
  1992. out:
  1993. hgsl_hyp_channel_pool_put(hab_channel);
  1994. LOGD("%d, 0x%x", ret, dbq_idx);
  1995. RPC_TRACE_DONE();
  1996. return ret;
  1997. }
  1998. int hgsl_hyp_notify_cleanup(struct hgsl_hab_channel_t *hab_channel, uint32_t timeout)
  1999. {
  2000. struct notify_cleanup_params_t rpc_params = { 0 };
  2001. struct gsl_hab_payload *send_buf = NULL;
  2002. struct gsl_hab_payload *recv_buf = NULL;
  2003. int ret;
  2004. int rval;
  2005. if (!hab_channel) {
  2006. LOGE("hab_channel is invalid");
  2007. return -EINVAL;
  2008. }
  2009. ret = hgsl_rpc_parcel_reset(hab_channel);
  2010. if (ret) {
  2011. LOGE("hgsl_rpc_parcel_reset failed %d", ret);
  2012. goto out;
  2013. }
  2014. send_buf = &hab_channel->send_buf;
  2015. recv_buf = &hab_channel->recv_buf;
  2016. rpc_params.size = sizeof(rpc_params);
  2017. rpc_params.timeout = timeout;
  2018. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  2019. if (ret) {
  2020. LOGE("gsl_rpc_write failed, %d", ret);
  2021. goto out;
  2022. }
  2023. ret = gsl_rpc_transact(RPC_NOTIFY_CLEANUP, hab_channel);
  2024. if (ret) {
  2025. LOGE("gsl_rpc_transact failed, %d", ret);
  2026. goto out;
  2027. }
  2028. ret = gsl_rpc_read_int32_l(recv_buf, &rval);
  2029. if (ret) {
  2030. LOGE("gsl_rpc_read_int32_l failed, %d", ret);
  2031. goto out;
  2032. }
  2033. if (rval != GSL_SUCCESS) {
  2034. ret = (rval == GSL_FAILURE_TIMEOUT) ? -ETIMEDOUT : -EINVAL;
  2035. goto out;
  2036. }
  2037. out:
  2038. return ret;
  2039. }
  2040. int hgsl_hyp_query_dbcq(struct hgsl_hab_channel_t *hab_channel, uint32_t devhandle,
  2041. uint32_t ctxthandle, uint32_t length, uint32_t *db_signal, uint32_t *queue_gmuaddr,
  2042. uint32_t *irq_idx)
  2043. {
  2044. struct query_dbcq_params_t rpc_params = { 0 };
  2045. struct gsl_hab_payload *send_buf = NULL;
  2046. struct gsl_hab_payload *recv_buf = NULL;
  2047. int ret = 0;
  2048. int rval = 0;
  2049. RPC_TRACE();
  2050. if (!hab_channel) {
  2051. LOGE("invalid hab_channel");
  2052. ret = -EINVAL;
  2053. goto out;
  2054. }
  2055. ret = hgsl_rpc_parcel_reset(hab_channel);
  2056. if (ret) {
  2057. LOGE("hgsl_rpc_parcel_reset failed %d", ret);
  2058. goto out;
  2059. }
  2060. send_buf = &hab_channel->send_buf;
  2061. recv_buf = &hab_channel->recv_buf;
  2062. rpc_params.size = sizeof(rpc_params);
  2063. rpc_params.devhandle = devhandle;
  2064. rpc_params.ctxthandle = ctxthandle;
  2065. rpc_params.length = length;
  2066. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  2067. if (ret) {
  2068. LOGE("gsl_rpc_write failed, %d", ret);
  2069. goto out;
  2070. }
  2071. ret = gsl_rpc_transact(RPC_CONTEXT_QUERY_DBCQ, hab_channel);
  2072. if (ret) {
  2073. LOGE("gsl_rpc_transact failed, %d", ret);
  2074. goto out;
  2075. }
  2076. ret = gsl_rpc_read_int32_l(recv_buf, &rval);
  2077. if (ret) {
  2078. LOGE("gsl_rpc_read_int32_l failed, %d", ret);
  2079. goto out;
  2080. }
  2081. if (rval != GSL_SUCCESS) {
  2082. LOGI("RPC_CONTEXT_QUERY_DBCQ failed, rval %d", rval);
  2083. ret = -EINVAL;
  2084. goto out;
  2085. }
  2086. ret = gsl_rpc_read_uint32_l(recv_buf, db_signal);
  2087. if (ret) {
  2088. LOGE("failed to read db_signal, %d", ret);
  2089. ret = -EINVAL;
  2090. goto out;
  2091. }
  2092. ret = gsl_rpc_read_uint32_l(recv_buf, queue_gmuaddr);
  2093. if (ret) {
  2094. LOGE("failed to read queue_gmuaddr, %d", ret);
  2095. ret = -EINVAL;
  2096. goto out;
  2097. }
  2098. ret = gsl_rpc_read_uint32_l(recv_buf, irq_idx);
  2099. if (ret) {
  2100. LOGE("failed to read irq_idx, %d", ret);
  2101. ret = -EINVAL;
  2102. goto out;
  2103. }
  2104. out:
  2105. RPC_TRACE_DONE();
  2106. return ret;
  2107. }
  2108. int hgsl_hyp_context_register_dbcq(struct hgsl_hab_channel_t *hab_channel,
  2109. uint32_t devhandle, uint32_t ctxthandle, struct dma_buf *dma_buf, uint32_t size,
  2110. uint32_t queue_body_offset, uint32_t *export_id)
  2111. {
  2112. struct register_dbcq_params_t rpc_params = { 0 };
  2113. struct gsl_hab_payload *send_buf = NULL;
  2114. struct gsl_hab_payload *recv_buf = NULL;
  2115. int ret = 0;
  2116. int rval = 0;
  2117. int hab_exp_flags = 0;
  2118. void *hab_exp_handle = NULL;
  2119. RPC_TRACE();
  2120. if (!hab_channel || !export_id) {
  2121. LOGE("invalid hab_channel");
  2122. ret = -EINVAL;
  2123. goto out;
  2124. }
  2125. ret = hgsl_rpc_parcel_reset(hab_channel);
  2126. if (ret) {
  2127. LOGE("hgsl_rpc_parcel_reset failed %d", ret);
  2128. goto out;
  2129. }
  2130. send_buf = &hab_channel->send_buf;
  2131. recv_buf = &hab_channel->recv_buf;
  2132. hab_exp_flags = HABMM_EXPIMP_FLAGS_DMABUF;
  2133. hab_exp_handle = (void *)dma_buf;
  2134. ret = habmm_export(hab_channel->socket, hab_exp_handle,
  2135. size, export_id, hab_exp_flags);
  2136. if (ret) {
  2137. LOGE("export failed, size(%d), hab flags(0x%X) ret(%d)", size, ret);
  2138. goto out;
  2139. }
  2140. rpc_params.size = sizeof(rpc_params);
  2141. rpc_params.devhandle = devhandle;
  2142. rpc_params.ctxthandle = ctxthandle;
  2143. rpc_params.len = size;
  2144. rpc_params.queue_body_offset = queue_body_offset;
  2145. rpc_params.export_id = *export_id;
  2146. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  2147. if (ret) {
  2148. LOGE("gsl_rpc_write failed, %d", ret);
  2149. goto out;
  2150. }
  2151. ret = gsl_rpc_transact(RPC_CONTEXT_REGISTER_DBCQ, hab_channel);
  2152. if (ret) {
  2153. LOGE("gsl_rpc_transact failed, %d", ret);
  2154. goto out;
  2155. }
  2156. ret = gsl_rpc_read_int32_l(recv_buf, &rval);
  2157. if (ret) {
  2158. LOGE("gsl_rpc_read_int32_l failed, %d", ret);
  2159. goto out;
  2160. }
  2161. if (rval != GSL_SUCCESS) {
  2162. LOGE("RPC_REGISTER_DB_CONTEXT_QUEUE failed, %d, %d", rval, *export_id);
  2163. ret = -EINVAL;
  2164. goto out;
  2165. }
  2166. LOGD("ctxt id(%d) export_id(%d), size(%d)", ctxthandle, *export_id, rpc_params.len);
  2167. out:
  2168. RPC_TRACE_DONE();
  2169. return ret;
  2170. }
  2171. static int write_ctxt_shadowts_mem_fe(struct hgsl_hab_channel_t *hab_channel,
  2172. struct hgsl_context *ctxt,
  2173. struct hgsl_ioctl_ctxt_create_params *hgsl_params,
  2174. uint32_t dbq_off,
  2175. struct hgsl_mem_node *mem_node)
  2176. {
  2177. struct context_create_params_v1_t rpc_params = { 0 };
  2178. struct context_create_params_t *ctxt_create_p =
  2179. &rpc_params.ctxt_create_param;
  2180. struct memory_map_ext_fd_params_t *shadow_map_p =
  2181. &rpc_params.shadow_map_param;
  2182. struct gsl_hab_payload *send_buf = &hab_channel->send_buf;
  2183. void *hab_exp_handle = NULL;
  2184. uint32_t export_id = 0;
  2185. int ret = 0;
  2186. if (!mem_node->dma_buf) {
  2187. LOGE("dma_buf is invalid");
  2188. ret = -EINVAL;
  2189. goto out;
  2190. }
  2191. rpc_params.size = sizeof(rpc_params);
  2192. ctxt_create_p->size = sizeof(*ctxt_create_p);
  2193. ctxt_create_p->devhandle = hgsl_params->devhandle;
  2194. ctxt_create_p->type = hgsl_params->type;
  2195. ctxt_create_p->flags = hgsl_params->flags;
  2196. hab_exp_handle = (void *)(mem_node->dma_buf);
  2197. ret = habmm_export(hab_channel->socket, hab_exp_handle, PAGE_SIZE,
  2198. &export_id, HABMM_EXPIMP_FLAGS_DMABUF);
  2199. if (ret) {
  2200. LOGE("habmm_export failed, %d", ret);
  2201. goto out;
  2202. }
  2203. mem_node->export_id = export_id;
  2204. shadow_map_p->size = sizeof(*shadow_map_p);
  2205. shadow_map_p->fd = mem_node->fd;
  2206. shadow_map_p->hostptr = 0;
  2207. shadow_map_p->len = PAGE_SIZE;
  2208. shadow_map_p->offset = 0;
  2209. shadow_map_p->memtype = mem_node->memtype;
  2210. shadow_map_p->flags = 0;
  2211. rpc_params.dbq_off = dbq_off;
  2212. ret = gsl_rpc_write(send_buf, &rpc_params, sizeof(rpc_params));
  2213. if (ret) {
  2214. LOGE("gsl_rpc_write failed, %d", ret);
  2215. goto out;
  2216. }
  2217. ret = gsl_rpc_write_uint32(send_buf, export_id);
  2218. if (ret) {
  2219. LOGE("gsl_rpc_write failed, %d", ret);
  2220. goto out;
  2221. }
  2222. out:
  2223. return ret;
  2224. }
  2225. static int read_shadowts_mem_fe(struct hgsl_hab_channel_t *hab_channel,
  2226. struct hgsl_context *ctxt,
  2227. struct hgsl_mem_node *mem_node)
  2228. {
  2229. struct gsl_hab_payload *recv_buf = &hab_channel->recv_buf;
  2230. int ret = 0;
  2231. int rval = GSL_SUCCESS;
  2232. ret = gsl_rpc_read_int32_l(recv_buf, &rval);
  2233. if (ret) {
  2234. LOGE("gsl_rpc_read_int32_l failed, %d", ret);
  2235. goto out;
  2236. }
  2237. if (rval == GSL_SUCCESS) {
  2238. ret = gsl_rpc_read(recv_buf, &mem_node->memdesc,
  2239. sizeof(mem_node->memdesc));
  2240. if (ret) {
  2241. LOGE("gsl_rpc_read failed, %d", ret);
  2242. goto out;
  2243. }
  2244. if (!mem_node->dma_buf) {
  2245. ret = -EINVAL;
  2246. goto out;
  2247. }
  2248. dma_buf_begin_cpu_access(mem_node->dma_buf, DMA_FROM_DEVICE);
  2249. ret = dma_buf_vmap(mem_node->dma_buf, &ctxt->map);
  2250. if (ret) {
  2251. dma_buf_end_cpu_access(mem_node->dma_buf, DMA_FROM_DEVICE);
  2252. ret = -EFAULT;
  2253. } else {
  2254. ctxt->shadow_ts = (struct shadow_ts *)ctxt->map.vaddr;
  2255. ctxt->shadow_ts_node = mem_node;
  2256. ctxt->is_fe_shadow = true;
  2257. ctxt->shadow_ts_flags = GSL_FLAGS_INITIALIZED;
  2258. }
  2259. } else {
  2260. ret = -EINVAL;
  2261. }
  2262. out:
  2263. return ret;
  2264. }
  2265. static int read_shadowts_mem_be(struct hgsl_hab_channel_t *hab_channel,
  2266. struct hgsl_context *ctxt)
  2267. {
  2268. struct qcom_hgsl *hgsl = ctxt->priv->dev;
  2269. struct gsl_hab_payload *recv_buf = &hab_channel->recv_buf;
  2270. uint32_t export_id = 0;
  2271. struct hgsl_mem_node *mem_node = NULL;
  2272. struct shadowprop_t rpc_shadow = { 0 };
  2273. int ret = 0;
  2274. ret = gsl_rpc_read(recv_buf, &rpc_shadow, sizeof(rpc_shadow));
  2275. if (ret) {
  2276. LOGE("gsl_rpc_read failed, %d", ret);
  2277. goto out;
  2278. }
  2279. ret = gsl_rpc_read_uint32_l(recv_buf, &export_id);
  2280. if (ret) {
  2281. LOGE("gsl_rpc_read_uint32_l failed, %d", ret);
  2282. goto out;
  2283. }
  2284. if (rpc_shadow.flags & GSL_FLAGS_INITIALIZED) {
  2285. mem_node = hgsl_mem_node_zalloc(hgsl->default_iocoherency);
  2286. if (mem_node == NULL) {
  2287. ret = -ENOMEM;
  2288. goto out;
  2289. }
  2290. mem_node->memdesc.size64 = (uint64_t)rpc_shadow.sizebytes;
  2291. mem_node->fd = -1;
  2292. ret = habmm_import(hab_channel->socket,
  2293. (void **)&mem_node->dma_buf,
  2294. PAGE_ALIGN(rpc_shadow.sizebytes), export_id, 0);
  2295. if (ret) {
  2296. LOGE("habmm_import failed, ret = %d", ret);
  2297. goto out;
  2298. }
  2299. mem_node->export_id = export_id;
  2300. dma_buf_begin_cpu_access(mem_node->dma_buf, DMA_FROM_DEVICE);
  2301. ret = dma_buf_vmap(mem_node->dma_buf, &ctxt->map);
  2302. if (ret) {
  2303. dma_buf_end_cpu_access(mem_node->dma_buf, DMA_FROM_DEVICE);
  2304. hgsl_hyp_put_shadowts_mem(hab_channel, mem_node);
  2305. habmm_unimport(hab_channel->socket,
  2306. export_id, mem_node->dma_buf, 0);
  2307. ret = -EFAULT;
  2308. } else {
  2309. ctxt->shadow_ts = (struct shadow_ts *)ctxt->map.vaddr;
  2310. ctxt->shadow_ts_node = mem_node;
  2311. ctxt->shadow_ts_flags = rpc_shadow.flags;
  2312. }
  2313. }
  2314. out:
  2315. if (ret)
  2316. kfree(mem_node);
  2317. return ret;
  2318. }
  2319. int hgsl_hyp_ctxt_create_v1(struct device *dev,
  2320. struct hgsl_priv *priv,
  2321. struct hgsl_hab_channel_t *hab_channel,
  2322. struct hgsl_context *ctxt,
  2323. struct hgsl_ioctl_ctxt_create_params *hgsl_params,
  2324. int dbq_off, uint32_t *dbq_info)
  2325. {
  2326. struct hgsl_mem_node *mem_node = NULL;
  2327. struct gsl_hab_payload *recv_buf = NULL;
  2328. struct qcom_hgsl *hgsl = priv->dev;
  2329. int ret = 0;
  2330. int rval = GSL_SUCCESS;
  2331. RPC_TRACE();
  2332. hgsl_params->ctxthandle = -1;
  2333. if (!hab_channel) {
  2334. LOGE("invalid hab_channel");
  2335. ret = -EINVAL;
  2336. goto out;
  2337. }
  2338. ret = hgsl_rpc_parcel_reset(hab_channel);
  2339. if (ret) {
  2340. LOGE("hgsl_rpc_parcel_reset failed %d", ret);
  2341. goto out;
  2342. }
  2343. recv_buf = &hab_channel->recv_buf;
  2344. mem_node = hgsl_mem_node_zalloc(hgsl->default_iocoherency);
  2345. if (mem_node == NULL) {
  2346. ret = -ENOMEM;
  2347. goto out;
  2348. }
  2349. mem_node->fd = -1;
  2350. ret = hgsl_sharedmem_alloc(dev, PAGE_SIZE, 0, mem_node);
  2351. if (ret)
  2352. goto out;
  2353. ret = write_ctxt_shadowts_mem_fe(hab_channel, ctxt, hgsl_params,
  2354. dbq_off, mem_node);
  2355. if (ret)
  2356. goto out;
  2357. ret = gsl_rpc_transact_ext(RPC_CONTEXT_CREATE, 1, hab_channel, false);
  2358. if (ret) {
  2359. LOGE("gsl_rpc_transact_ext failed, %d", ret);
  2360. goto out;
  2361. }
  2362. ret = gsl_rpc_read_uint32_l(recv_buf, &hgsl_params->ctxthandle);
  2363. if (ret) {
  2364. LOGE("gsl_rpc_read_uint32_l failed, %d", ret);
  2365. goto out;
  2366. }
  2367. if (hgsl_params->ctxthandle >= HGSL_CONTEXT_NUM) {
  2368. ret = -EINVAL;
  2369. goto out;
  2370. }
  2371. ctxt->context_id = hgsl_params->ctxthandle;
  2372. ctxt->devhandle = hgsl_params->devhandle;
  2373. ctxt->pid = priv->pid;
  2374. ctxt->priv = priv;
  2375. ctxt->flags = hgsl_params->flags;
  2376. ret = read_shadowts_mem_fe(hab_channel, ctxt, mem_node);
  2377. if (ret != 0) {
  2378. ret = read_shadowts_mem_be(hab_channel, ctxt);
  2379. if (ret) {
  2380. LOGE("failed to get shadow memory");
  2381. goto out;
  2382. }
  2383. }
  2384. if (dbq_off)
  2385. goto out;
  2386. ret = gsl_rpc_read_int32_l(recv_buf, &rval);
  2387. if (ret) {
  2388. LOGE("gsl_rpc_read_int32_l failed, %d", ret);
  2389. goto out;
  2390. }
  2391. if (rval == GSL_SUCCESS) {
  2392. ret = gsl_rpc_read_uint32_l(recv_buf, dbq_info);
  2393. if (ret) {
  2394. LOGE("gsl_rpc_read_uint32_l failed, %d", ret);
  2395. goto out;
  2396. }
  2397. }
  2398. out:
  2399. if (ret) {
  2400. if (ctxt->shadow_ts) {
  2401. dma_buf_vunmap(ctxt->shadow_ts_node->dma_buf,
  2402. &ctxt->map);
  2403. dma_buf_end_cpu_access(ctxt->shadow_ts_node->dma_buf,
  2404. DMA_FROM_DEVICE);
  2405. ctxt->shadow_ts = NULL;
  2406. }
  2407. if (ctxt->shadow_ts_node && !ctxt->is_fe_shadow) {
  2408. hgsl_hyp_put_shadowts_mem(hab_channel,
  2409. ctxt->shadow_ts_node);
  2410. kfree(ctxt->shadow_ts_node);
  2411. ctxt->shadow_ts_node = NULL;
  2412. }
  2413. if (hgsl_params->ctxthandle < HGSL_CONTEXT_NUM) {
  2414. hgsl_hyp_ctxt_destroy(hab_channel,
  2415. hgsl_params->devhandle,
  2416. hgsl_params->ctxthandle, NULL, 0);
  2417. hgsl_params->ctxthandle = -1;
  2418. }
  2419. }
  2420. if (ret || !ctxt->is_fe_shadow) {
  2421. hgsl_hyp_mem_unmap_smmu(hab_channel, mem_node);
  2422. hgsl_sharedmem_free(mem_node);
  2423. }
  2424. RPC_TRACE_DONE();
  2425. return ret;
  2426. }