synx.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946
  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 <linux/atomic.h>
  7. #include <linux/file.h>
  8. #include <linux/fs.h>
  9. #include <linux/module.h>
  10. #include <linux/poll.h>
  11. #include <linux/random.h>
  12. #include <linux/remoteproc/qcom_rproc.h>
  13. #include <linux/slab.h>
  14. #include <linux/sync_file.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/vmalloc.h>
  17. #include "synx_debugfs.h"
  18. #include "synx_private.h"
  19. #include "synx_util.h"
  20. struct synx_device *synx_dev;
  21. static atomic64_t synx_counter = ATOMIC64_INIT(1);
  22. void synx_external_callback(s32 sync_obj, int status, void *data)
  23. {
  24. struct synx_signal_cb *signal_cb = data;
  25. if (IS_ERR_OR_NULL(signal_cb)) {
  26. dprintk(SYNX_ERR,
  27. "invalid payload from external obj %d [%d]\n",
  28. sync_obj, status);
  29. return;
  30. }
  31. signal_cb->status = status;
  32. signal_cb->ext_sync_id = sync_obj;
  33. signal_cb->flag = SYNX_SIGNAL_FROM_CALLBACK;
  34. dprintk(SYNX_DBG,
  35. "external callback from %d on handle %u\n",
  36. sync_obj, signal_cb->handle);
  37. /*
  38. * invoke the handler directly as external callback
  39. * is invoked from separate task.
  40. * avoids creation of separate task again.
  41. */
  42. synx_signal_handler(&signal_cb->cb_dispatch);
  43. }
  44. EXPORT_SYMBOL(synx_external_callback);
  45. bool synx_fence_enable_signaling(struct dma_fence *fence)
  46. {
  47. return true;
  48. }
  49. const char *synx_fence_driver_name(struct dma_fence *fence)
  50. {
  51. return "Global Synx driver";
  52. }
  53. void synx_fence_release(struct dma_fence *fence)
  54. {
  55. /* release the memory allocated during create */
  56. kfree(fence->lock);
  57. kfree(fence);
  58. dprintk(SYNX_MEM, "released backing fence %pK\n", fence);
  59. }
  60. EXPORT_SYMBOL(synx_fence_release);
  61. static struct dma_fence_ops synx_fence_ops = {
  62. .wait = dma_fence_default_wait,
  63. .enable_signaling = synx_fence_enable_signaling,
  64. .get_driver_name = synx_fence_driver_name,
  65. .get_timeline_name = synx_fence_driver_name,
  66. .release = synx_fence_release,
  67. };
  68. static int synx_create_sync_fd(struct dma_fence *fence)
  69. {
  70. int fd;
  71. struct sync_file *sync_file;
  72. if (IS_ERR_OR_NULL(fence))
  73. return -SYNX_INVALID;
  74. fd = get_unused_fd_flags(O_CLOEXEC);
  75. if (fd < 0)
  76. return fd;
  77. sync_file = sync_file_create(fence);
  78. if (IS_ERR_OR_NULL(sync_file)) {
  79. dprintk(SYNX_ERR, "error creating sync file\n");
  80. goto err;
  81. }
  82. fd_install(fd, sync_file->file);
  83. return fd;
  84. err:
  85. put_unused_fd(fd);
  86. return -SYNX_INVALID;
  87. }
  88. void *synx_get_fence(struct synx_session *session,
  89. u32 h_synx)
  90. {
  91. struct synx_client *client;
  92. struct synx_handle_coredata *synx_data;
  93. struct synx_coredata *synx_obj;
  94. struct dma_fence *fence = NULL;
  95. client = synx_get_client(session);
  96. if (IS_ERR_OR_NULL(client))
  97. return NULL;
  98. synx_data = synx_util_acquire_handle(client, h_synx);
  99. synx_obj = synx_util_obtain_object(synx_data);
  100. if (IS_ERR_OR_NULL(synx_obj) ||
  101. IS_ERR_OR_NULL(synx_obj->fence)) {
  102. dprintk(SYNX_ERR,
  103. "[sess :%llu] invalid handle access %u\n",
  104. client->id, h_synx);
  105. goto fail;
  106. }
  107. mutex_lock(&synx_obj->obj_lock);
  108. fence = synx_obj->fence;
  109. /* obtain an additional reference to the fence */
  110. dma_fence_get(fence);
  111. mutex_unlock(&synx_obj->obj_lock);
  112. fail:
  113. synx_util_release_handle(synx_data);
  114. synx_put_client(client);
  115. return fence;
  116. }
  117. EXPORT_SYMBOL(synx_get_fence);
  118. static int synx_native_check_bind(struct synx_client *client,
  119. struct synx_create_params *params)
  120. {
  121. int rc;
  122. u32 h_synx;
  123. struct synx_entry_64 *ext_entry;
  124. struct synx_map_entry *entry;
  125. if (IS_ERR_OR_NULL(params->fence))
  126. return -SYNX_INVALID;
  127. ext_entry = synx_util_retrieve_data(params->fence,
  128. synx_util_map_params_to_type(params->flags));
  129. if (IS_ERR_OR_NULL(ext_entry))
  130. return -SYNX_NOENT;
  131. h_synx = ext_entry->data[0];
  132. synx_util_remove_data(params->fence,
  133. synx_util_map_params_to_type(params->flags));
  134. entry = synx_util_get_map_entry(h_synx);
  135. if (IS_ERR_OR_NULL(entry))
  136. /* possible cleanup, retry to alloc new handle */
  137. return -SYNX_NOENT;
  138. rc = synx_util_init_handle(client, entry->synx_obj,
  139. &h_synx, entry);
  140. if (rc != SYNX_SUCCESS) {
  141. dprintk(SYNX_ERR,
  142. "[sess :%llu] new handle init failed\n",
  143. client->id);
  144. goto fail;
  145. }
  146. *params->h_synx = h_synx;
  147. return SYNX_SUCCESS;
  148. fail:
  149. synx_util_release_map_entry(entry);
  150. return rc;
  151. }
  152. static int synx_native_create_core(struct synx_client *client,
  153. struct synx_create_params *params)
  154. {
  155. int rc;
  156. struct synx_coredata *synx_obj;
  157. struct synx_map_entry *map_entry;
  158. if (IS_ERR_OR_NULL(client) || IS_ERR_OR_NULL(params) ||
  159. IS_ERR_OR_NULL(params->h_synx))
  160. return -SYNX_INVALID;
  161. synx_obj = kzalloc(sizeof(*synx_obj), GFP_KERNEL);
  162. if (IS_ERR_OR_NULL(synx_obj))
  163. return -SYNX_NOMEM;
  164. rc = synx_util_init_coredata(synx_obj, params,
  165. &synx_fence_ops, client->dma_context);
  166. if (rc) {
  167. dprintk(SYNX_ERR,
  168. "[sess :%llu] handle allocation failed\n",
  169. client->id);
  170. kfree(synx_obj);
  171. goto fail;
  172. }
  173. map_entry = synx_util_insert_to_map(synx_obj,
  174. *params->h_synx, 0);
  175. if (IS_ERR_OR_NULL(map_entry)) {
  176. rc = PTR_ERR(map_entry);
  177. synx_util_put_object(synx_obj);
  178. goto fail;
  179. }
  180. rc = synx_util_add_callback(synx_obj, *params->h_synx);
  181. if (rc != SYNX_SUCCESS) {
  182. synx_util_release_map_entry(map_entry);
  183. goto fail;
  184. }
  185. rc = synx_util_init_handle(client, synx_obj,
  186. params->h_synx, map_entry);
  187. if (rc < 0) {
  188. dprintk(SYNX_ERR,
  189. "[sess :%llu] unable to init new handle\n",
  190. client->id);
  191. synx_util_release_map_entry(map_entry);
  192. goto fail;
  193. }
  194. dprintk(SYNX_MEM,
  195. "[sess :%llu] allocated %u, core %pK, fence %pK\n",
  196. client->id, *params->h_synx, synx_obj, synx_obj->fence);
  197. return SYNX_SUCCESS;
  198. fail:
  199. return rc;
  200. }
  201. int synx_create(struct synx_session *session,
  202. struct synx_create_params *params)
  203. {
  204. int rc = -SYNX_NOENT;
  205. struct synx_client *client;
  206. struct synx_external_desc_v2 ext_desc = {0};
  207. if (IS_ERR_OR_NULL(params) || IS_ERR_OR_NULL(params->h_synx) ||
  208. params->flags > SYNX_CREATE_MAX_FLAGS) {
  209. dprintk(SYNX_ERR, "invalid create arguments\n");
  210. return -SYNX_INVALID;
  211. }
  212. if (params->flags & SYNX_CREATE_DMA_FENCE) {
  213. dprintk(SYNX_ERR,
  214. "handle create with native fence not supported\n");
  215. return -SYNX_NOSUPPORT;
  216. }
  217. client = synx_get_client(session);
  218. if (IS_ERR_OR_NULL(client))
  219. return -SYNX_INVALID;
  220. *params->h_synx = 0;
  221. do {
  222. /* create with external fence */
  223. if (!IS_ERR_OR_NULL(params->fence))
  224. rc = synx_native_check_bind(client, params);
  225. if (rc == -SYNX_NOENT) {
  226. rc = synx_native_create_core(client, params);
  227. if (rc == SYNX_SUCCESS &&
  228. !IS_ERR_OR_NULL(params->fence)) {
  229. /* save external fence details */
  230. rc = synx_util_save_data(params->fence,
  231. synx_util_map_params_to_type(params->flags),
  232. *params->h_synx);
  233. if (rc == -SYNX_ALREADY) {
  234. /*
  235. * raced with create on same fence from
  236. * another client. clear the allocated
  237. * handle and retry.
  238. */
  239. synx_native_release_core(client, *params->h_synx);
  240. *params->h_synx = 0;
  241. rc = -SYNX_NOENT;
  242. continue;
  243. } else if (rc != SYNX_SUCCESS) {
  244. dprintk(SYNX_ERR,
  245. "allocating handle failed=%d", rc);
  246. synx_native_release_core(client, *params->h_synx);
  247. break;
  248. }
  249. /* bind with external fence */
  250. ext_desc.id = *((u32 *)params->fence);
  251. ext_desc.type = synx_util_map_params_to_type(params->flags);
  252. rc = synx_bind(session, *params->h_synx, ext_desc);
  253. if (rc != SYNX_SUCCESS) {
  254. dprintk(SYNX_ERR,
  255. "[sess :%llu] bind external fence failed\n",
  256. client->id);
  257. synx_native_release_core(client, *params->h_synx);
  258. goto fail;
  259. }
  260. }
  261. }
  262. if (rc == SYNX_SUCCESS)
  263. dprintk(SYNX_VERB,
  264. "[sess :%llu] handle allocated %u\n",
  265. client->id, *params->h_synx);
  266. break;
  267. } while (true);
  268. fail:
  269. synx_put_client(client);
  270. return rc;
  271. }
  272. EXPORT_SYMBOL(synx_create);
  273. int synx_native_signal_core(struct synx_coredata *synx_obj,
  274. u32 status,
  275. bool cb_signal,
  276. u64 ext_sync_id)
  277. {
  278. int rc = 0;
  279. int ret;
  280. u32 i = 0;
  281. u32 idx = 0;
  282. s32 sync_id;
  283. u32 type;
  284. void *data = NULL;
  285. struct synx_bind_desc bind_descs[SYNX_MAX_NUM_BINDINGS];
  286. struct bind_operations *bind_ops = NULL;
  287. if (IS_ERR_OR_NULL(synx_obj))
  288. return -SYNX_INVALID;
  289. synx_util_callback_dispatch(synx_obj, status);
  290. /*
  291. * signal the external bound sync obj/s even if fence signal fails,
  292. * w/ error signal state (set above) to prevent deadlock
  293. */
  294. if (synx_obj->num_bound_synxs > 0) {
  295. memset(bind_descs, 0,
  296. sizeof(struct synx_bind_desc) * SYNX_MAX_NUM_BINDINGS);
  297. for (i = 0; i < synx_obj->num_bound_synxs; i++) {
  298. /* signal invoked by external sync obj */
  299. if (cb_signal &&
  300. (ext_sync_id ==
  301. synx_obj->bound_synxs[i].external_desc.id)) {
  302. dprintk(SYNX_VERB,
  303. "skipping signaling inbound sync: %llu\n",
  304. ext_sync_id);
  305. type = synx_obj->bound_synxs[i].external_desc.type;
  306. memset(&synx_obj->bound_synxs[i], 0,
  307. sizeof(struct synx_bind_desc));
  308. /* clear the hash table entry */
  309. synx_util_remove_data(&ext_sync_id, type);
  310. continue;
  311. }
  312. memcpy(&bind_descs[idx++],
  313. &synx_obj->bound_synxs[i],
  314. sizeof(struct synx_bind_desc));
  315. /* clear the memory, its been backed up above */
  316. memset(&synx_obj->bound_synxs[i], 0,
  317. sizeof(struct synx_bind_desc));
  318. }
  319. synx_obj->num_bound_synxs = 0;
  320. }
  321. for (i = 0; i < idx; i++) {
  322. sync_id = bind_descs[i].external_desc.id;
  323. data = bind_descs[i].external_data;
  324. type = bind_descs[i].external_desc.type;
  325. bind_ops = synx_util_get_bind_ops(type);
  326. if (IS_ERR_OR_NULL(bind_ops)) {
  327. dprintk(SYNX_ERR,
  328. "invalid bind ops for type: %u\n", type);
  329. kfree(data);
  330. continue;
  331. }
  332. /* clear the hash table entry */
  333. synx_util_remove_data(&sync_id, type);
  334. /*
  335. * we are already signaled, so don't want to
  336. * recursively be signaled
  337. */
  338. ret = bind_ops->deregister_callback(
  339. synx_external_callback, data, sync_id);
  340. if (ret < 0) {
  341. dprintk(SYNX_ERR,
  342. "deregistration fail on %d, type: %u, err=%d\n",
  343. sync_id, type, ret);
  344. continue;
  345. }
  346. dprintk(SYNX_VERB,
  347. "signal external sync: %d, type: %u, status: %u\n",
  348. sync_id, type, status);
  349. /* optional function to enable external signaling */
  350. if (bind_ops->enable_signaling) {
  351. ret = bind_ops->enable_signaling(sync_id);
  352. if (ret < 0)
  353. dprintk(SYNX_ERR,
  354. "enabling fail on %d, type: %u, err=%d\n",
  355. sync_id, type, ret);
  356. }
  357. ret = bind_ops->signal(sync_id, status);
  358. if (ret < 0)
  359. dprintk(SYNX_ERR,
  360. "signaling fail on %d, type: %u, err=%d\n",
  361. sync_id, type, ret);
  362. /*
  363. * release the memory allocated for external data.
  364. * It is safe to release this memory as external cb
  365. * has been already deregistered before this.
  366. */
  367. kfree(data);
  368. }
  369. return rc;
  370. }
  371. int synx_native_signal_fence(struct synx_coredata *synx_obj,
  372. u32 status)
  373. {
  374. int rc = 0;
  375. unsigned long flags;
  376. if (IS_ERR_OR_NULL(synx_obj) || IS_ERR_OR_NULL(synx_obj->fence))
  377. return -SYNX_INVALID;
  378. if (status <= SYNX_STATE_ACTIVE) {
  379. dprintk(SYNX_ERR, "signaling with wrong status: %u\n",
  380. status);
  381. return -SYNX_INVALID;
  382. }
  383. if (synx_util_is_merged_object(synx_obj)) {
  384. dprintk(SYNX_ERR, "signaling a composite handle\n");
  385. return -SYNX_INVALID;
  386. }
  387. if (synx_util_get_object_status(synx_obj) !=
  388. SYNX_STATE_ACTIVE)
  389. return -SYNX_ALREADY;
  390. if (IS_ERR_OR_NULL(synx_obj->signal_cb)) {
  391. dprintk(SYNX_ERR, "signal cb in bad state\n");
  392. return -SYNX_INVALID;
  393. }
  394. /*
  395. * remove registered callback for the fence
  396. * so it does not invoke the signal through callback again
  397. */
  398. if (!dma_fence_remove_callback(synx_obj->fence,
  399. &synx_obj->signal_cb->fence_cb)) {
  400. dprintk(SYNX_ERR, "callback could not be removed\n");
  401. return -SYNX_INVALID;
  402. }
  403. dprintk(SYNX_MEM, "signal cb destroyed %pK\n",
  404. synx_obj->signal_cb);
  405. kfree(synx_obj->signal_cb);
  406. synx_obj->signal_cb = NULL;
  407. /* releasing reference held by signal cb */
  408. synx_util_put_object(synx_obj);
  409. spin_lock_irqsave(synx_obj->fence->lock, flags);
  410. /* check the status again acquiring lock to avoid errors */
  411. if (synx_util_get_object_status_locked(synx_obj) !=
  412. SYNX_STATE_ACTIVE) {
  413. spin_unlock_irqrestore(synx_obj->fence->lock, flags);
  414. return -SYNX_ALREADY;
  415. }
  416. synx_obj->status = status;
  417. if (status >= SYNX_DMA_FENCE_STATE_MAX)
  418. status = SYNX_DMA_FENCE_STATE_MAX - 1;
  419. /* set fence error to model {signal w/ error} */
  420. if (status != SYNX_STATE_SIGNALED_SUCCESS)
  421. dma_fence_set_error(synx_obj->fence, -status);
  422. rc = dma_fence_signal_locked(synx_obj->fence);
  423. if (rc)
  424. dprintk(SYNX_ERR,
  425. "signaling fence %pK failed=%d\n",
  426. synx_obj->fence, rc);
  427. spin_unlock_irqrestore(synx_obj->fence->lock, flags);
  428. return rc;
  429. }
  430. int synx_native_signal_merged_fence(struct synx_coredata *synx_obj, u32 status)
  431. {
  432. int rc = SYNX_SUCCESS;
  433. unsigned long flags;
  434. int i = 0, num_fences = 0;
  435. struct synx_coredata **synx_child_obj = NULL;
  436. rc = synx_get_child_coredata(synx_obj, &synx_child_obj, &num_fences);
  437. if (rc != SYNX_SUCCESS)
  438. return rc;
  439. for(i = 0; i < num_fences; i++)
  440. {
  441. if (IS_ERR_OR_NULL(synx_child_obj[i]) || IS_ERR_OR_NULL(synx_child_obj[i]->fence)) {
  442. dprintk(SYNX_ERR, "Invalid child coredata %d\n", i);
  443. rc = -SYNX_NOENT;
  444. goto fail;
  445. }
  446. mutex_lock(&synx_child_obj[i]->obj_lock);
  447. spin_lock_irqsave(synx_child_obj[i]->fence->lock, flags);
  448. if (synx_util_get_object_status_locked(synx_child_obj[i]) != SYNX_STATE_ACTIVE ||
  449. !synx_util_is_global_object(synx_child_obj[i]))
  450. {
  451. spin_unlock_irqrestore(synx_child_obj[i]->fence->lock, flags);
  452. mutex_unlock(&synx_child_obj[i]->obj_lock);
  453. continue;
  454. }
  455. spin_unlock_irqrestore(synx_child_obj[i]->fence->lock, flags);
  456. status = synx_global_get_status(synx_child_obj[i]->global_idx);
  457. rc = synx_native_signal_fence(synx_child_obj[i], status);
  458. mutex_unlock(&synx_child_obj[i]->obj_lock);
  459. }
  460. fail:
  461. kfree(synx_child_obj);
  462. return rc;
  463. }
  464. u32 synx_get_child_status(struct synx_coredata *synx_obj)
  465. {
  466. u32 h_child = 0, i = 0;
  467. u32 status = SYNX_DMA_FENCE_STATE_MAX - 1, child_status = SYNX_STATE_ACTIVE;
  468. struct dma_fence_array *array = NULL;
  469. struct synx_map_entry *fence_entry = NULL;
  470. struct synx_coredata *synx_child_obj = NULL;
  471. if (!dma_fence_is_array(synx_obj->fence))
  472. return status;
  473. array = to_dma_fence_array(synx_obj->fence);
  474. if (IS_ERR_OR_NULL(array))
  475. goto bail;
  476. for (i = 0; i < array->num_fences; i++) {
  477. h_child = synx_util_get_fence_entry((u64)array->fences[i], 1);
  478. if (h_child == 0)
  479. h_child = synx_util_get_fence_entry((u64)array->fences[i], 0);
  480. if (h_child == 0)
  481. continue;
  482. fence_entry = synx_util_get_map_entry(h_child);
  483. if (IS_ERR_OR_NULL(fence_entry) || IS_ERR_OR_NULL(fence_entry->synx_obj)) {
  484. dprintk(SYNX_ERR, "Invalid handle access %u", h_child);
  485. goto bail;
  486. }
  487. synx_child_obj = fence_entry->synx_obj;
  488. mutex_lock(&synx_child_obj->obj_lock);
  489. if (synx_util_is_global_object(synx_child_obj))
  490. child_status = synx_global_get_status(synx_child_obj->global_idx);
  491. else
  492. child_status = synx_child_obj->status;
  493. mutex_unlock(&synx_child_obj->obj_lock);
  494. synx_util_release_map_entry(fence_entry);
  495. dprintk(SYNX_VERB, "Child handle %u status %d", h_child, child_status);
  496. if (child_status != SYNX_STATE_ACTIVE &&
  497. (status == SYNX_DMA_FENCE_STATE_MAX - 1 ||
  498. (child_status > SYNX_STATE_SIGNALED_SUCCESS &&
  499. child_status <= SYNX_STATE_SIGNALED_MAX)))
  500. status = child_status;
  501. }
  502. bail:
  503. return status;
  504. }
  505. u32 synx_custom_get_status(struct synx_coredata *synx_obj, u32 status)
  506. {
  507. u32 custom_status = status;
  508. u32 parent_global_status =
  509. synx_util_is_global_object(synx_obj) ?
  510. synx_global_get_status(synx_obj->global_idx) : SYNX_STATE_ACTIVE;
  511. if (IS_ERR_OR_NULL(synx_obj))
  512. goto bail;
  513. mutex_lock(&synx_obj->obj_lock);
  514. if (synx_util_is_merged_object(synx_obj)) {
  515. if (parent_global_status == SYNX_STATE_ACTIVE)
  516. synx_obj->status = synx_get_child_status(synx_obj);
  517. else
  518. synx_obj->status = parent_global_status;
  519. custom_status = synx_obj->status;
  520. }
  521. mutex_unlock(&synx_obj->obj_lock);
  522. bail:
  523. return custom_status;
  524. }
  525. void synx_signal_handler(struct work_struct *cb_dispatch)
  526. {
  527. int rc = SYNX_SUCCESS;
  528. u32 idx;
  529. struct synx_signal_cb *signal_cb =
  530. container_of(cb_dispatch, struct synx_signal_cb, cb_dispatch);
  531. struct synx_coredata *synx_obj = signal_cb->synx_obj;
  532. u32 h_synx = signal_cb->handle;
  533. u32 status = signal_cb->status;
  534. if (signal_cb->flag & SYNX_SIGNAL_FROM_FENCE) {
  535. status = synx_custom_get_status(synx_obj, status);
  536. dprintk(SYNX_VERB,
  537. "handle %d will be updated with status %d\n",
  538. h_synx, status);
  539. }
  540. if ((signal_cb->flag & SYNX_SIGNAL_FROM_FENCE) &&
  541. (synx_util_is_global_handle(h_synx) ||
  542. synx_util_is_global_object(synx_obj))) {
  543. idx = (IS_ERR_OR_NULL(synx_obj)) ?
  544. synx_util_global_idx(h_synx) :
  545. synx_obj->global_idx;
  546. if (synx_global_get_status(idx) == SYNX_STATE_ACTIVE) {
  547. rc = synx_global_update_status(idx, status);
  548. if (rc != SYNX_SUCCESS)
  549. dprintk(SYNX_ERR,
  550. "global status update of %u failed=%d\n",
  551. h_synx, rc);
  552. }
  553. /*
  554. * We are decrementing the reference here assuming this code will be
  555. * executed after handle is released. But in case if clients signal
  556. * dma fence in middle of execution sequence, then we will put
  557. * one reference thus deleting the global idx. As of now clients cannot
  558. * signal dma fence.
  559. */
  560. if (IS_ERR_OR_NULL(synx_obj))
  561. synx_global_put_ref(idx);
  562. }
  563. /*
  564. * when invoked from external callback, possible for
  565. * all local clients to have released the handle coredata.
  566. */
  567. if (IS_ERR_OR_NULL(synx_obj)) {
  568. dprintk(SYNX_WARN,
  569. "handle %d has no local clients\n",
  570. h_synx);
  571. dprintk(SYNX_MEM, "signal cb destroyed %pK\n",
  572. signal_cb);
  573. kfree(signal_cb);
  574. return;
  575. }
  576. if (rc != SYNX_SUCCESS) {
  577. dprintk(SYNX_ERR,
  578. "global status update for %u failed=%d\n",
  579. h_synx, rc);
  580. goto fail;
  581. }
  582. mutex_lock(&synx_obj->obj_lock);
  583. if (signal_cb->flag & SYNX_SIGNAL_FROM_IPC &&
  584. synx_util_get_object_status(synx_obj) == SYNX_STATE_ACTIVE) {
  585. if (synx_util_is_merged_object(synx_obj))
  586. rc = synx_native_signal_merged_fence(synx_obj, status);
  587. else
  588. rc = synx_native_signal_fence(synx_obj, status);
  589. }
  590. if (rc != SYNX_SUCCESS) {
  591. mutex_unlock(&synx_obj->obj_lock);
  592. dprintk(SYNX_ERR,
  593. "failed to signal fence %u with err=%d\n",
  594. h_synx, rc);
  595. goto fail;
  596. }
  597. if (rc == SYNX_SUCCESS && synx_util_get_object_status(synx_obj)
  598. != SYNX_STATE_ACTIVE)
  599. rc = synx_native_signal_core(synx_obj, status,
  600. (signal_cb->flag & SYNX_SIGNAL_FROM_CALLBACK) ?
  601. true : false, signal_cb->ext_sync_id);
  602. mutex_unlock(&synx_obj->obj_lock);
  603. if (rc != SYNX_SUCCESS)
  604. dprintk(SYNX_ERR,
  605. "internal signaling %u failed=%d",
  606. h_synx, rc);
  607. fail:
  608. /* release reference held by signal cb */
  609. synx_util_put_object(synx_obj);
  610. dprintk(SYNX_MEM, "signal cb destroyed %pK\n", signal_cb);
  611. kfree(signal_cb);
  612. dprintk(SYNX_VERB, "signal handle %u dispatch complete=%d",
  613. h_synx, rc);
  614. }
  615. /* function would be called from atomic context */
  616. void synx_fence_callback(struct dma_fence *fence,
  617. struct dma_fence_cb *cb)
  618. {
  619. s32 status;
  620. struct synx_signal_cb *signal_cb =
  621. container_of(cb, struct synx_signal_cb, fence_cb);
  622. dprintk(SYNX_DBG,
  623. "callback from external fence %pK for handle %u\n",
  624. fence, signal_cb->handle);
  625. /* other signal_cb members would be set during cb registration */
  626. status = dma_fence_get_status_locked(fence);
  627. /*
  628. * dma_fence_get_status_locked API returns 1 if signaled,
  629. * 0 if ACTIVE,
  630. * and negative error code in case of any failure
  631. */
  632. if (status == 1)
  633. status = SYNX_STATE_SIGNALED_SUCCESS;
  634. else if (status == -SYNX_STATE_SIGNALED_CANCEL)
  635. status = SYNX_STATE_SIGNALED_CANCEL;
  636. else if (status < 0 && status >= -SYNX_STATE_SIGNALED_MAX)
  637. status = SYNX_STATE_SIGNALED_EXTERNAL;
  638. else
  639. status = (u32)-status;
  640. signal_cb->status = status;
  641. INIT_WORK(&signal_cb->cb_dispatch, synx_signal_handler);
  642. queue_work(synx_dev->wq_cb, &signal_cb->cb_dispatch);
  643. }
  644. EXPORT_SYMBOL(synx_fence_callback);
  645. static int synx_signal_offload_job(
  646. struct synx_client *client,
  647. struct synx_coredata *synx_obj,
  648. u32 h_synx, u32 status)
  649. {
  650. int rc = SYNX_SUCCESS;
  651. struct synx_signal_cb *signal_cb;
  652. signal_cb = kzalloc(sizeof(*signal_cb), GFP_ATOMIC);
  653. if (IS_ERR_OR_NULL(signal_cb)) {
  654. rc = -SYNX_NOMEM;
  655. goto fail;
  656. }
  657. /*
  658. * since the signal will be queued to separate thread,
  659. * to ensure the synx coredata pointer remain valid, get
  660. * additional reference, thus avoiding any potential
  661. * use-after-free.
  662. */
  663. synx_util_get_object(synx_obj);
  664. signal_cb->handle = h_synx;
  665. signal_cb->status = status;
  666. signal_cb->synx_obj = synx_obj;
  667. signal_cb->flag = SYNX_SIGNAL_FROM_CLIENT;
  668. dprintk(SYNX_VERB,
  669. "[sess :%llu] signal work queued for %u\n",
  670. client->id, h_synx);
  671. INIT_WORK(&signal_cb->cb_dispatch, synx_signal_handler);
  672. queue_work(synx_dev->wq_cb, &signal_cb->cb_dispatch);
  673. fail:
  674. return rc;
  675. }
  676. int synx_signal(struct synx_session *session, u32 h_synx, u32 status)
  677. {
  678. int rc = SYNX_SUCCESS;
  679. struct synx_client *client;
  680. struct synx_handle_coredata *synx_data = NULL;
  681. struct synx_coredata *synx_obj;
  682. client = synx_get_client(session);
  683. if (IS_ERR_OR_NULL(client))
  684. return -SYNX_INVALID;
  685. if (status <= SYNX_STATE_ACTIVE ||
  686. !(status == SYNX_STATE_SIGNALED_SUCCESS ||
  687. status == SYNX_STATE_SIGNALED_CANCEL ||
  688. status > SYNX_STATE_SIGNALED_MAX)) {
  689. dprintk(SYNX_ERR,
  690. "[sess :%llu] signaling with wrong status: %u\n",
  691. client->id, status);
  692. rc = -SYNX_INVALID;
  693. goto fail;
  694. }
  695. synx_data = synx_util_acquire_handle(client, h_synx);
  696. synx_obj = synx_util_obtain_object(synx_data);
  697. if (IS_ERR_OR_NULL(synx_obj) ||
  698. IS_ERR_OR_NULL(synx_obj->fence)) {
  699. dprintk(SYNX_ERR,
  700. "[sess :%llu] invalid handle access %u\n",
  701. client->id, h_synx);
  702. rc = -SYNX_INVALID;
  703. goto fail;
  704. }
  705. mutex_lock(&synx_obj->obj_lock);
  706. if (synx_util_is_global_handle(h_synx) ||
  707. synx_util_is_global_object(synx_obj))
  708. rc = synx_global_update_status(
  709. synx_obj->global_idx, status);
  710. if (rc != SYNX_SUCCESS) {
  711. mutex_unlock(&synx_obj->obj_lock);
  712. dprintk(SYNX_ERR,
  713. "[sess :%llu] status update %d failed=%d\n",
  714. client->id, h_synx, rc);
  715. goto fail;
  716. }
  717. /*
  718. * offload callback dispatch and external fence
  719. * notification to separate worker thread, if any.
  720. */
  721. if (synx_obj->num_bound_synxs ||
  722. !list_empty(&synx_obj->reg_cbs_list))
  723. rc = synx_signal_offload_job(client, synx_obj,
  724. h_synx, status);
  725. rc = synx_native_signal_fence(synx_obj, status);
  726. if (rc != SYNX_SUCCESS)
  727. dprintk(SYNX_ERR,
  728. "[sess :%llu] signaling %u failed=%d\n",
  729. client->id, h_synx, rc);
  730. mutex_unlock(&synx_obj->obj_lock);
  731. fail:
  732. synx_util_release_handle(synx_data);
  733. synx_put_client(client);
  734. return rc;
  735. }
  736. EXPORT_SYMBOL(synx_signal);
  737. static int synx_match_payload(struct synx_kernel_payload *cb_payload,
  738. struct synx_kernel_payload *payload)
  739. {
  740. int rc = 0;
  741. if (IS_ERR_OR_NULL(cb_payload) || IS_ERR_OR_NULL(payload))
  742. return -SYNX_INVALID;
  743. if ((cb_payload->cb_func == payload->cb_func) &&
  744. (cb_payload->data == payload->data)) {
  745. if (payload->cancel_cb_func) {
  746. cb_payload->cb_func =
  747. payload->cancel_cb_func;
  748. rc = 1;
  749. } else {
  750. rc = 2;
  751. dprintk(SYNX_VERB,
  752. "kernel cb de-registration success\n");
  753. }
  754. }
  755. return rc;
  756. }
  757. /* Timer Callback function. This will be called when timer expires */
  758. void synx_timer_cb(struct timer_list *data)
  759. {
  760. struct synx_client *client;
  761. struct synx_handle_coredata *synx_data;
  762. struct synx_coredata *synx_obj;
  763. struct synx_cb_data *synx_cb = container_of(data, struct synx_cb_data, synx_timer);
  764. client = synx_get_client(synx_cb->session);
  765. if (IS_ERR_OR_NULL(client)) {
  766. dprintk(SYNX_ERR,
  767. "invalid session data 0x%x in cb payload\n",
  768. synx_cb->session);
  769. return;
  770. }
  771. synx_data = synx_util_acquire_handle(client, synx_cb->h_synx);
  772. synx_obj = synx_util_obtain_object(synx_data);
  773. if (IS_ERR_OR_NULL(synx_obj)) {
  774. dprintk(SYNX_ERR,
  775. "[sess :0x%llx] invalid handle access 0x%x\n",
  776. synx_cb->session, synx_cb->h_synx);
  777. return;
  778. }
  779. dprintk(SYNX_VERB,
  780. "Timer expired for synx_cb 0x%x timeout 0x%llx. Deleting the timer.\n",
  781. synx_cb, synx_cb->timeout);
  782. synx_cb->status = SYNX_STATE_TIMEOUT;
  783. del_timer(&synx_cb->synx_timer);
  784. list_del_init(&synx_cb->node);
  785. queue_work(synx_dev->wq_cb, &synx_cb->cb_dispatch);
  786. }
  787. static int synx_start_timer(struct synx_cb_data *synx_cb)
  788. {
  789. int rc = 0;
  790. timer_setup(&synx_cb->synx_timer, synx_timer_cb, 0);
  791. rc = mod_timer(&synx_cb->synx_timer, jiffies + msecs_to_jiffies(synx_cb->timeout));
  792. dprintk(SYNX_VERB,
  793. "Timer started for synx_cb 0x%x timeout 0x%llx\n",
  794. synx_cb, synx_cb->timeout);
  795. return rc;
  796. }
  797. int synx_async_wait(struct synx_session *session,
  798. struct synx_callback_params *params)
  799. {
  800. int rc = 0;
  801. u32 idx;
  802. u32 status;
  803. struct synx_client *client;
  804. struct synx_handle_coredata *synx_data;
  805. struct synx_coredata *synx_obj;
  806. struct synx_cb_data *synx_cb;
  807. struct synx_kernel_payload payload;
  808. if (IS_ERR_OR_NULL(session) || IS_ERR_OR_NULL(params))
  809. return -SYNX_INVALID;
  810. client = synx_get_client(session);
  811. if (IS_ERR_OR_NULL(client))
  812. return -SYNX_INVALID;
  813. synx_data = synx_util_acquire_handle(client, params->h_synx);
  814. synx_obj = synx_util_obtain_object(synx_data);
  815. if (IS_ERR_OR_NULL(synx_obj)) {
  816. dprintk(SYNX_ERR,
  817. "[sess :%llu] invalid handle access %u\n",
  818. client->id, params->h_synx);
  819. rc = -SYNX_INVALID;
  820. goto fail;
  821. }
  822. mutex_lock(&synx_obj->obj_lock);
  823. synx_cb = kzalloc(sizeof(*synx_cb), GFP_ATOMIC);
  824. if (IS_ERR_OR_NULL(synx_cb)) {
  825. rc = -SYNX_NOMEM;
  826. goto release;
  827. }
  828. payload.h_synx = params->h_synx;
  829. payload.cb_func = params->cb_func;
  830. payload.data = params->userdata;
  831. /* allocate a free index from client cb table */
  832. rc = synx_util_alloc_cb_entry(client, &payload, &idx);
  833. if (rc) {
  834. dprintk(SYNX_ERR,
  835. "[sess :%llu] error allocating cb entry\n",
  836. client->id);
  837. kfree(synx_cb);
  838. goto release;
  839. }
  840. if (synx_util_is_global_handle(params->h_synx) ||
  841. synx_util_is_global_object(synx_obj)) {
  842. status = synx_global_test_status_set_wait(
  843. synx_util_global_idx(params->h_synx),
  844. SYNX_CORE_APSS);
  845. if (status != SYNX_STATE_ACTIVE) {
  846. if (synx_util_is_merged_object(synx_obj))
  847. synx_native_signal_merged_fence(synx_obj, status);
  848. else
  849. synx_native_signal_fence(synx_obj, status);
  850. }
  851. }
  852. status = synx_util_get_object_status(synx_obj);
  853. synx_cb->session = session;
  854. synx_cb->idx = idx;
  855. synx_cb->h_synx = params->h_synx;
  856. INIT_WORK(&synx_cb->cb_dispatch, synx_util_cb_dispatch);
  857. /* add callback if object still ACTIVE, dispatch if SIGNALED */
  858. if (status == SYNX_STATE_ACTIVE) {
  859. dprintk(SYNX_VERB,
  860. "[sess :%llu] callback added for handle %u\n",
  861. client->id, params->h_synx);
  862. synx_cb->timeout = params->timeout_ms;
  863. if (params->timeout_ms != SYNX_NO_TIMEOUT) {
  864. rc = synx_start_timer(synx_cb);
  865. if (rc != SYNX_SUCCESS) {
  866. dprintk(SYNX_ERR,
  867. "[sess :%llu] timer start failed - synx_cb: 0x%x, params->timeout_ms: 0x%llx, handle: 0x%x, ret : %d\n",
  868. client->id, synx_cb, params->timeout_ms,
  869. params->h_synx, rc);
  870. goto release;
  871. }
  872. }
  873. list_add(&synx_cb->node, &synx_obj->reg_cbs_list);
  874. } else {
  875. synx_cb->status = status;
  876. dprintk(SYNX_VERB,
  877. "[sess :%llu] callback queued for handle %u\n",
  878. client->id, params->h_synx);
  879. queue_work(synx_dev->wq_cb,
  880. &synx_cb->cb_dispatch);
  881. }
  882. release:
  883. mutex_unlock(&synx_obj->obj_lock);
  884. fail:
  885. synx_util_release_handle(synx_data);
  886. synx_put_client(client);
  887. return rc;
  888. }
  889. EXPORT_SYMBOL(synx_async_wait);
  890. int synx_cancel_async_wait(
  891. struct synx_session *session,
  892. struct synx_callback_params *params)
  893. {
  894. int rc = 0, ret = 0;
  895. u32 status;
  896. bool match_found = false;
  897. struct synx_client *client;
  898. struct synx_handle_coredata *synx_data;
  899. struct synx_coredata *synx_obj;
  900. struct synx_kernel_payload payload;
  901. struct synx_cb_data *synx_cb, *synx_cb_temp;
  902. struct synx_client_cb *cb_payload;
  903. if (IS_ERR_OR_NULL(session) || IS_ERR_OR_NULL(params))
  904. return -SYNX_INVALID;
  905. client = synx_get_client(session);
  906. if (IS_ERR_OR_NULL(client))
  907. return -SYNX_INVALID;
  908. synx_data = synx_util_acquire_handle(client, params->h_synx);
  909. synx_obj = synx_util_obtain_object(synx_data);
  910. if (IS_ERR_OR_NULL(synx_obj)) {
  911. dprintk(SYNX_ERR,
  912. "[sess :%llu] invalid handle access %u\n",
  913. client->id, params->h_synx);
  914. rc = -SYNX_INVALID;
  915. goto fail;
  916. }
  917. mutex_lock(&synx_obj->obj_lock);
  918. if (synx_util_is_external_object(synx_obj)) {
  919. dprintk(SYNX_ERR,
  920. "cannot cancel wait on external fence\n");
  921. goto release;
  922. }
  923. payload.h_synx = params->h_synx;
  924. payload.cb_func = params->cb_func;
  925. payload.data = params->userdata;
  926. payload.cancel_cb_func = params->cancel_cb_func;
  927. status = synx_util_get_object_status(synx_obj);
  928. if (status != SYNX_STATE_ACTIVE) {
  929. dprintk(SYNX_ERR,
  930. "handle %u already signaled or timed out, cannot cancel\n",
  931. params->h_synx);
  932. rc = -SYNX_INVALID;
  933. goto release;
  934. }
  935. status = SYNX_CALLBACK_RESULT_CANCELED;
  936. /* remove all cb payloads mayching the deregister call */
  937. list_for_each_entry_safe(synx_cb, synx_cb_temp,
  938. &synx_obj->reg_cbs_list, node) {
  939. if (synx_cb->session != session) {
  940. continue;
  941. } else if (synx_cb->idx == 0 ||
  942. synx_cb->idx >= SYNX_MAX_OBJS) {
  943. /*
  944. * this should not happen. Even if it does,
  945. * the allocated memory will be cleaned up
  946. * when object is destroyed, preventing any
  947. * memory leaks.
  948. */
  949. dprintk(SYNX_ERR,
  950. "[sess :%llu] invalid callback data\n",
  951. client->id);
  952. continue;
  953. }
  954. cb_payload = &client->cb_table[synx_cb->idx];
  955. ret = synx_match_payload(&cb_payload->kernel_cb, &payload);
  956. if (synx_cb->timeout != SYNX_NO_TIMEOUT) {
  957. dprintk(SYNX_VERB,
  958. "Deleting timer synx_cb 0x%x, timeout 0x%llx\n",
  959. synx_cb, synx_cb->timeout);
  960. del_timer(&synx_cb->synx_timer);
  961. }
  962. switch (ret) {
  963. case 1:
  964. /* queue the cancel cb work */
  965. list_del_init(&synx_cb->node);
  966. synx_cb->status = status;
  967. queue_work(synx_dev->wq_cb,
  968. &synx_cb->cb_dispatch);
  969. match_found = true;
  970. break;
  971. case 2:
  972. /* no cancellation cb */
  973. if (synx_util_clear_cb_entry(client, cb_payload))
  974. dprintk(SYNX_ERR,
  975. "[sess :%llu] error clearing cb %u\n",
  976. client->id, params->h_synx);
  977. list_del_init(&synx_cb->node);
  978. kfree(synx_cb);
  979. match_found = true;
  980. break;
  981. default:
  982. break;
  983. }
  984. }
  985. if (!match_found)
  986. rc = -SYNX_INVALID;
  987. release:
  988. mutex_unlock(&synx_obj->obj_lock);
  989. fail:
  990. synx_util_release_handle(synx_data);
  991. synx_put_client(client);
  992. return rc;
  993. }
  994. EXPORT_SYMBOL(synx_cancel_async_wait);
  995. int synx_merge(struct synx_session *session,
  996. struct synx_merge_params *params)
  997. {
  998. int rc = SYNX_SUCCESS, i, num_signaled = 0;
  999. u32 count = 0, h_child, status = SYNX_STATE_ACTIVE;
  1000. u32 *h_child_list = NULL, *h_child_idx_list = NULL;
  1001. struct synx_client *client;
  1002. struct dma_fence **fences = NULL;
  1003. struct synx_map_entry *map_entry;
  1004. struct synx_coredata *synx_obj, *synx_obj_child;
  1005. struct synx_handle_coredata *synx_data_child;
  1006. if (IS_ERR_OR_NULL(session) || IS_ERR_OR_NULL(params))
  1007. return -SYNX_INVALID;
  1008. if (IS_ERR_OR_NULL(params->h_synxs) ||
  1009. IS_ERR_OR_NULL(params->h_merged_obj)) {
  1010. dprintk(SYNX_ERR, "invalid arguments\n");
  1011. return -SYNX_INVALID;
  1012. }
  1013. client = synx_get_client(session);
  1014. if (IS_ERR_OR_NULL(client))
  1015. return -SYNX_INVALID;
  1016. synx_obj = kzalloc(sizeof(*synx_obj), GFP_KERNEL);
  1017. if (IS_ERR_OR_NULL(synx_obj)) {
  1018. rc = -SYNX_NOMEM;
  1019. goto fail;
  1020. }
  1021. rc = synx_util_validate_merge(client, params->h_synxs,
  1022. params->num_objs, &fences, &count);
  1023. if (rc < 0) {
  1024. dprintk(SYNX_ERR,
  1025. "[sess :%llu] merge validation failed\n",
  1026. client->id);
  1027. rc = -SYNX_INVALID;
  1028. kfree(synx_obj);
  1029. goto fail;
  1030. }
  1031. rc = synx_util_init_group_coredata(synx_obj, fences,
  1032. params, count, client->dma_context);
  1033. if (rc) {
  1034. dprintk(SYNX_ERR,
  1035. "[sess :%llu] error initializing merge handle\n",
  1036. client->id);
  1037. goto clean_up;
  1038. }
  1039. map_entry = synx_util_insert_to_map(synx_obj,
  1040. *params->h_merged_obj, 0);
  1041. if (IS_ERR_OR_NULL(map_entry)) {
  1042. rc = PTR_ERR(map_entry);
  1043. /*
  1044. * dma fence put will take care of removing the references taken
  1045. * on child fences
  1046. */
  1047. dma_fence_put(synx_obj->fence);
  1048. kfree(synx_obj);
  1049. goto fail;
  1050. }
  1051. rc = synx_util_add_callback(synx_obj, *params->h_merged_obj);
  1052. if (rc != SYNX_SUCCESS)
  1053. goto clean_up;
  1054. rc = synx_util_init_handle(client, synx_obj,
  1055. params->h_merged_obj, map_entry);
  1056. if (rc) {
  1057. dprintk(SYNX_ERR,
  1058. "[sess :%llu] unable to init merge handle %u\n",
  1059. client->id, *params->h_merged_obj);
  1060. goto clean_up;
  1061. }
  1062. h_child_list = kzalloc(count*4, GFP_KERNEL);
  1063. if (IS_ERR_OR_NULL(h_child_list)) {
  1064. rc = -SYNX_NOMEM;
  1065. goto clear;
  1066. }
  1067. h_child_idx_list = kzalloc(count*4, GFP_KERNEL);
  1068. if (IS_ERR_OR_NULL(h_child_idx_list)) {
  1069. kfree(h_child_list);
  1070. rc = -SYNX_NOMEM;
  1071. goto clear;
  1072. }
  1073. for (i = 0; i < count; i++) {
  1074. h_child = synx_util_get_fence_entry((u64)fences[i], 1);
  1075. if (!synx_util_is_global_handle(h_child))
  1076. continue;
  1077. h_child_list[num_signaled] = h_child;
  1078. h_child_idx_list[num_signaled++] = synx_util_global_idx(h_child);
  1079. }
  1080. if (params->flags & SYNX_MERGE_GLOBAL_FENCE) {
  1081. rc = synx_global_merge(h_child_idx_list, num_signaled,
  1082. synx_util_global_idx(*params->h_merged_obj));
  1083. if (rc != SYNX_SUCCESS) {
  1084. dprintk(SYNX_ERR, "global merge failed\n");
  1085. kfree(h_child_list);
  1086. kfree(h_child_idx_list);
  1087. goto clear;
  1088. }
  1089. }
  1090. else {
  1091. for(i = 0; i < num_signaled; i++) {
  1092. status = synx_global_test_status_set_wait(synx_util_global_idx(h_child_list[i]), SYNX_CORE_APSS);
  1093. if (status != SYNX_STATE_ACTIVE) {
  1094. synx_data_child = synx_util_acquire_handle(client, h_child_list[i]);
  1095. synx_obj_child = synx_util_obtain_object(synx_data_child);
  1096. if (IS_ERR_OR_NULL(synx_obj_child)) {
  1097. dprintk(SYNX_ERR,
  1098. "[sess :%llu] invalid child handle %u\n",
  1099. client->id, h_child_list[i]);
  1100. continue;
  1101. }
  1102. mutex_lock(&synx_obj_child->obj_lock);
  1103. if (synx_obj->status == SYNX_STATE_ACTIVE)
  1104. rc = synx_native_signal_fence(synx_obj_child, status);
  1105. mutex_unlock(&synx_obj_child->obj_lock);
  1106. if (rc != SYNX_SUCCESS)
  1107. dprintk(SYNX_ERR, "h_synx %u failed with status %d\n", h_child_list[i], rc);
  1108. synx_util_release_handle(synx_data_child);
  1109. }
  1110. }
  1111. }
  1112. dprintk(SYNX_MEM,
  1113. "[sess :%llu] merge allocated %u, core %pK, fence %pK\n",
  1114. client->id, *params->h_merged_obj, synx_obj,
  1115. synx_obj->fence);
  1116. kfree(h_child_list);
  1117. kfree(h_child_idx_list);
  1118. synx_put_client(client);
  1119. return SYNX_SUCCESS;
  1120. clear:
  1121. synx_native_release_core(client, (*params->h_merged_obj));
  1122. synx_put_client(client);
  1123. return rc;
  1124. clean_up:
  1125. /*
  1126. * if map_entry is not created the cleanup of child fences have to be
  1127. * handled manually
  1128. */
  1129. if (IS_ERR_OR_NULL(map_entry)) {
  1130. kfree(synx_obj);
  1131. synx_util_merge_error(client, params->h_synxs, count);
  1132. if (params->num_objs && params->num_objs <= count)
  1133. kfree(fences);
  1134. } else {
  1135. synx_util_release_map_entry(map_entry);
  1136. }
  1137. fail:
  1138. synx_put_client(client);
  1139. return rc;
  1140. }
  1141. EXPORT_SYMBOL(synx_merge);
  1142. int synx_native_release_core(struct synx_client *client,
  1143. u32 h_synx)
  1144. {
  1145. int rc = -SYNX_INVALID;
  1146. struct synx_handle_coredata *curr, *synx_handle = NULL;
  1147. spin_lock_bh(&client->handle_map_lock);
  1148. hash_for_each_possible(client->handle_map,
  1149. curr, node, h_synx) {
  1150. if (curr->key == h_synx &&
  1151. curr->rel_count != 0) {
  1152. curr->rel_count--;
  1153. synx_handle = curr;
  1154. rc = SYNX_SUCCESS;
  1155. break;
  1156. }
  1157. }
  1158. spin_unlock_bh(&client->handle_map_lock);
  1159. /* release the reference obtained at synx creation */
  1160. synx_util_release_handle(synx_handle);
  1161. return rc;
  1162. }
  1163. int synx_release(struct synx_session *session, u32 h_synx)
  1164. {
  1165. int rc = 0;
  1166. struct synx_client *client;
  1167. client = synx_get_client(session);
  1168. if (IS_ERR_OR_NULL(client))
  1169. return -SYNX_INVALID;
  1170. rc = synx_native_release_core(client, h_synx);
  1171. synx_put_client(client);
  1172. return rc;
  1173. }
  1174. EXPORT_SYMBOL(synx_release);
  1175. int synx_wait(struct synx_session *session,
  1176. u32 h_synx, u64 timeout_ms)
  1177. {
  1178. int rc = 0;
  1179. unsigned long timeleft;
  1180. struct synx_client *client;
  1181. struct synx_handle_coredata *synx_data;
  1182. struct synx_coredata *synx_obj;
  1183. client = synx_get_client(session);
  1184. if (IS_ERR_OR_NULL(client))
  1185. return -SYNX_INVALID;
  1186. synx_data = synx_util_acquire_handle(client, h_synx);
  1187. synx_obj = synx_util_obtain_object(synx_data);
  1188. if (IS_ERR_OR_NULL(synx_obj) || IS_ERR_OR_NULL(synx_obj->fence)) {
  1189. dprintk(SYNX_ERR,
  1190. "[sess :%llu] invalid handle access %u\n",
  1191. client->id, h_synx);
  1192. rc = -SYNX_INVALID;
  1193. goto fail;
  1194. }
  1195. if (synx_util_is_global_handle(h_synx)) {
  1196. rc = synx_global_test_status_set_wait(
  1197. synx_util_global_idx(h_synx), SYNX_CORE_APSS);
  1198. if (rc != SYNX_STATE_ACTIVE) {
  1199. mutex_lock(&synx_obj->obj_lock);
  1200. if (synx_util_is_merged_object(synx_obj))
  1201. synx_native_signal_merged_fence(synx_obj, rc);
  1202. else
  1203. synx_native_signal_fence(synx_obj, rc);
  1204. mutex_unlock(&synx_obj->obj_lock);
  1205. }
  1206. }
  1207. timeleft = dma_fence_wait_timeout(synx_obj->fence, (bool) 0,
  1208. msecs_to_jiffies(timeout_ms));
  1209. if (timeleft <= 0) {
  1210. dprintk(SYNX_ERR,
  1211. "[sess :%llu] wait timeout for handle %u\n",
  1212. client->id, h_synx);
  1213. rc = -ETIMEDOUT;
  1214. goto fail;
  1215. }
  1216. mutex_lock(&synx_obj->obj_lock);
  1217. rc = synx_util_get_object_status(synx_obj);
  1218. mutex_unlock(&synx_obj->obj_lock);
  1219. fail:
  1220. synx_util_release_handle(synx_data);
  1221. synx_put_client(client);
  1222. return rc;
  1223. }
  1224. EXPORT_SYMBOL(synx_wait);
  1225. int synx_bind(struct synx_session *session,
  1226. u32 h_synx,
  1227. struct synx_external_desc_v2 external_sync)
  1228. {
  1229. int rc = 0;
  1230. u32 i;
  1231. u32 bound_idx;
  1232. struct synx_client *client;
  1233. struct synx_handle_coredata *synx_data = NULL;
  1234. struct synx_coredata *synx_obj;
  1235. struct synx_signal_cb *data = NULL;
  1236. struct bind_operations *bind_ops = NULL;
  1237. client = synx_get_client(session);
  1238. if (IS_ERR_OR_NULL(client))
  1239. return -SYNX_INVALID;
  1240. synx_data = synx_util_acquire_handle(client, h_synx);
  1241. synx_obj = synx_util_obtain_object(synx_data);
  1242. if (IS_ERR_OR_NULL(synx_obj)) {
  1243. if (rc || synx_data)
  1244. dprintk(SYNX_ERR,
  1245. "[sess :%llu] invalid handle access %u\n",
  1246. client->id, h_synx);
  1247. goto fail;
  1248. }
  1249. bind_ops = synx_util_get_bind_ops(external_sync.type);
  1250. if (IS_ERR_OR_NULL(bind_ops)) {
  1251. dprintk(SYNX_ERR,
  1252. "[sess :%llu] invalid bind ops for %u\n",
  1253. client->id, external_sync.type);
  1254. rc = -SYNX_INVALID;
  1255. goto fail;
  1256. }
  1257. mutex_lock(&synx_obj->obj_lock);
  1258. if (synx_util_is_merged_object(synx_obj)) {
  1259. dprintk(SYNX_ERR,
  1260. "[sess :%llu] cannot bind to composite handle %u\n",
  1261. client->id, h_synx);
  1262. rc = -SYNX_INVALID;
  1263. goto release;
  1264. }
  1265. if (synx_obj->num_bound_synxs >= SYNX_MAX_NUM_BINDINGS) {
  1266. dprintk(SYNX_ERR,
  1267. "[sess :%llu] max bindings reached for handle %u\n",
  1268. client->id, h_synx);
  1269. rc = -SYNX_NOMEM;
  1270. goto release;
  1271. }
  1272. /* don't bind external sync obj if already done */
  1273. for (i = 0; i < synx_obj->num_bound_synxs; i++) {
  1274. if ((external_sync.id ==
  1275. synx_obj->bound_synxs[i].external_desc.id) &&
  1276. (external_sync.type ==
  1277. synx_obj->bound_synxs[i].external_desc.type)){
  1278. dprintk(SYNX_ERR,
  1279. "[sess :%llu] duplicate bind for sync %llu\n",
  1280. client->id, external_sync.id);
  1281. rc = -SYNX_ALREADY;
  1282. goto release;
  1283. }
  1284. }
  1285. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1286. if (IS_ERR_OR_NULL(data)) {
  1287. rc = -SYNX_NOMEM;
  1288. goto release;
  1289. }
  1290. /* get additional reference since passing pointer to cb */
  1291. synx_util_get_object(synx_obj);
  1292. /* data passed to external callback */
  1293. data->handle = h_synx;
  1294. data->synx_obj = synx_obj;
  1295. bound_idx = synx_obj->num_bound_synxs;
  1296. memcpy(&synx_obj->bound_synxs[bound_idx],
  1297. &external_sync, sizeof(struct synx_external_desc_v2));
  1298. synx_obj->bound_synxs[bound_idx].external_data = data;
  1299. synx_obj->num_bound_synxs++;
  1300. mutex_unlock(&synx_obj->obj_lock);
  1301. rc = bind_ops->register_callback(synx_external_callback,
  1302. data, external_sync.id);
  1303. if (rc) {
  1304. dprintk(SYNX_ERR,
  1305. "[sess :%llu] callback reg failed for %llu\n",
  1306. client->id, external_sync.id);
  1307. mutex_lock(&synx_obj->obj_lock);
  1308. memset(&synx_obj->bound_synxs[bound_idx], 0,
  1309. sizeof(struct synx_external_desc_v2));
  1310. synx_obj->num_bound_synxs--;
  1311. mutex_unlock(&synx_obj->obj_lock);
  1312. synx_util_put_object(synx_obj);
  1313. kfree(data);
  1314. goto fail;
  1315. }
  1316. synx_util_release_handle(synx_data);
  1317. dprintk(SYNX_DBG,
  1318. "[sess :%llu] ext sync %llu bound to handle %u\n",
  1319. client->id, external_sync.id, h_synx);
  1320. synx_put_client(client);
  1321. return SYNX_SUCCESS;
  1322. release:
  1323. mutex_unlock(&synx_obj->obj_lock);
  1324. fail:
  1325. synx_util_release_handle(synx_data);
  1326. synx_put_client(client);
  1327. return rc;
  1328. }
  1329. EXPORT_SYMBOL(synx_bind);
  1330. int synx_get_status(struct synx_session *session,
  1331. u32 h_synx)
  1332. {
  1333. int rc = 0, status = 0;
  1334. struct synx_client *client;
  1335. struct synx_handle_coredata *synx_data;
  1336. struct synx_coredata *synx_obj;
  1337. client = synx_get_client(session);
  1338. if (IS_ERR_OR_NULL(client))
  1339. return -SYNX_INVALID;
  1340. synx_data = synx_util_acquire_handle(client, h_synx);
  1341. synx_obj = synx_util_obtain_object(synx_data);
  1342. if (IS_ERR_OR_NULL(synx_obj) ||
  1343. IS_ERR_OR_NULL(synx_obj->fence)) {
  1344. dprintk(SYNX_ERR,
  1345. "[sess :%llu] invalid handle access %u\n",
  1346. client->id, h_synx);
  1347. rc = -SYNX_INVALID;
  1348. goto fail;
  1349. }
  1350. mutex_lock(&synx_obj->obj_lock);
  1351. status = synx_util_get_object_status(synx_obj);
  1352. rc = synx_obj->status;
  1353. mutex_unlock(&synx_obj->obj_lock);
  1354. dprintk(SYNX_VERB,
  1355. "[sess :%llu] handle %u synx coredata status %d and dma fence status %d\n",
  1356. client->id, h_synx, rc, status);
  1357. fail:
  1358. synx_util_release_handle(synx_data);
  1359. synx_put_client(client);
  1360. return rc;
  1361. }
  1362. EXPORT_SYMBOL(synx_get_status);
  1363. static struct synx_map_entry *synx_handle_conversion(
  1364. struct synx_client *client,
  1365. u32 *h_synx, struct synx_map_entry *old_entry)
  1366. {
  1367. int rc;
  1368. struct synx_map_entry *map_entry = NULL;
  1369. struct synx_coredata *synx_obj;
  1370. if (IS_ERR_OR_NULL(old_entry)) {
  1371. old_entry = synx_util_get_map_entry(*h_synx);
  1372. if (IS_ERR_OR_NULL(old_entry)) {
  1373. rc = PTR_ERR(old_entry);
  1374. dprintk(SYNX_ERR,
  1375. "invalid import handle %u err=%d",
  1376. *h_synx, rc);
  1377. return old_entry;
  1378. }
  1379. }
  1380. synx_obj = old_entry->synx_obj;
  1381. BUG_ON(synx_obj == NULL);
  1382. mutex_lock(&synx_obj->obj_lock);
  1383. synx_util_get_object(synx_obj);
  1384. if (synx_obj->global_idx != 0) {
  1385. *h_synx = synx_encode_handle(
  1386. synx_obj->global_idx, SYNX_CORE_APSS, true);
  1387. map_entry = synx_util_get_map_entry(*h_synx);
  1388. if (IS_ERR_OR_NULL(map_entry)) {
  1389. /* raced with release from last global client */
  1390. map_entry = synx_util_insert_to_map(synx_obj,
  1391. *h_synx, 0);
  1392. if (IS_ERR_OR_NULL(map_entry)) {
  1393. rc = PTR_ERR(map_entry);
  1394. dprintk(SYNX_ERR,
  1395. "addition of %u to map failed=%d",
  1396. *h_synx, rc);
  1397. }
  1398. }
  1399. } else {
  1400. synx_obj->map_count++;
  1401. rc = synx_alloc_global_handle(h_synx);
  1402. if (rc == SYNX_SUCCESS) {
  1403. synx_obj->global_idx =
  1404. synx_util_global_idx(*h_synx);
  1405. synx_obj->type |= SYNX_CREATE_GLOBAL_FENCE;
  1406. map_entry = synx_util_insert_to_map(synx_obj,
  1407. *h_synx, 0);
  1408. if (IS_ERR_OR_NULL(map_entry)) {
  1409. rc = PTR_ERR(map_entry);
  1410. synx_global_put_ref(
  1411. synx_util_global_idx(*h_synx));
  1412. dprintk(SYNX_ERR,
  1413. "insertion of %u to map failed=%d",
  1414. *h_synx, rc);
  1415. }
  1416. }
  1417. }
  1418. mutex_unlock(&synx_obj->obj_lock);
  1419. if (IS_ERR_OR_NULL(map_entry))
  1420. synx_util_put_object(synx_obj);
  1421. synx_util_release_map_entry(old_entry);
  1422. return map_entry;
  1423. }
  1424. static int synx_native_import_handle(struct synx_client *client,
  1425. struct synx_import_indv_params *params)
  1426. {
  1427. int rc = SYNX_SUCCESS;
  1428. u32 h_synx, core_id;
  1429. struct synx_map_entry *map_entry, *old_entry;
  1430. struct synx_coredata *synx_obj;
  1431. struct synx_handle_coredata *synx_data = NULL, *curr;
  1432. char name[SYNX_OBJ_NAME_LEN] = {0};
  1433. struct synx_create_params c_params = {0};
  1434. if (IS_ERR_OR_NULL(client) || IS_ERR_OR_NULL(params) ||
  1435. IS_ERR_OR_NULL(params->fence) ||
  1436. IS_ERR_OR_NULL(params->new_h_synx))
  1437. return -SYNX_INVALID;
  1438. h_synx = *((u32 *)params->fence);
  1439. /* check if already mapped to client */
  1440. spin_lock_bh(&client->handle_map_lock);
  1441. hash_for_each_possible(client->handle_map,
  1442. curr, node, h_synx) {
  1443. if (curr->key == h_synx &&
  1444. curr->rel_count != 0 &&
  1445. (synx_util_is_global_handle(h_synx) ||
  1446. params->flags & SYNX_IMPORT_LOCAL_FENCE)) {
  1447. curr->rel_count++;
  1448. kref_get(&curr->refcount);
  1449. synx_data = curr;
  1450. break;
  1451. }
  1452. }
  1453. spin_unlock_bh(&client->handle_map_lock);
  1454. if (synx_data) {
  1455. *params->new_h_synx = h_synx;
  1456. return SYNX_SUCCESS;
  1457. }
  1458. map_entry = synx_util_get_map_entry(h_synx);
  1459. if (IS_ERR_OR_NULL(map_entry)) {
  1460. core_id = (h_synx & SYNX_OBJ_CORE_ID_MASK)
  1461. >> SYNX_HANDLE_INDEX_BITS;
  1462. if (core_id == SYNX_CORE_APSS) {
  1463. dprintk(SYNX_ERR,
  1464. "[sess :%llu] invalid import handle %u\n",
  1465. client->id, h_synx);
  1466. return -SYNX_INVALID;
  1467. } else if (synx_util_is_global_handle(h_synx)) {
  1468. /* import global handle created in another core */
  1469. synx_util_map_import_params_to_create(params, &c_params);
  1470. scnprintf(name, SYNX_OBJ_NAME_LEN, "import-client-%d",
  1471. current->pid);
  1472. c_params.name = name;
  1473. c_params.h_synx = &h_synx;
  1474. rc = synx_native_create_core(client, &c_params);
  1475. if (rc != SYNX_SUCCESS)
  1476. return rc;
  1477. *params->new_h_synx = h_synx;
  1478. return SYNX_SUCCESS;
  1479. }
  1480. dprintk(SYNX_ERR,
  1481. "[sess :%llu] invalid handle %u\n",
  1482. client->id, h_synx);
  1483. return -SYNX_INVALID;
  1484. }
  1485. synx_obj = map_entry->synx_obj;
  1486. BUG_ON(synx_obj == NULL);
  1487. if ((params->flags & SYNX_IMPORT_GLOBAL_FENCE) &&
  1488. !synx_util_is_global_handle(h_synx)) {
  1489. old_entry = map_entry;
  1490. map_entry = synx_handle_conversion(client, &h_synx,
  1491. old_entry);
  1492. }
  1493. if (IS_ERR_OR_NULL(map_entry))
  1494. return -SYNX_INVALID;
  1495. *params->new_h_synx = h_synx;
  1496. rc = synx_util_init_handle(client, map_entry->synx_obj,
  1497. params->new_h_synx, map_entry);
  1498. if (rc != SYNX_SUCCESS) {
  1499. dprintk(SYNX_ERR,
  1500. "[sess :%llu] init of imported handle %u failed=%d\n",
  1501. client->id, h_synx, rc);
  1502. synx_util_release_map_entry(map_entry);
  1503. }
  1504. return rc;
  1505. }
  1506. static int synx_native_import_fence(struct synx_client *client,
  1507. struct synx_import_indv_params *params)
  1508. {
  1509. int rc = SYNX_SUCCESS;
  1510. u32 curr_h_synx;
  1511. u32 global;
  1512. struct synx_create_params c_params = {0};
  1513. char name[SYNX_OBJ_NAME_LEN] = {0};
  1514. struct synx_fence_entry *entry;
  1515. struct synx_map_entry *map_entry = NULL;
  1516. struct synx_handle_coredata *synx_data = NULL, *curr;
  1517. if (IS_ERR_OR_NULL(client) || IS_ERR_OR_NULL(params) ||
  1518. IS_ERR_OR_NULL(params->fence) ||
  1519. IS_ERR_OR_NULL(params->new_h_synx))
  1520. return -SYNX_INVALID;
  1521. global = SYNX_IMPORT_GLOBAL_FENCE & params->flags;
  1522. retry:
  1523. *params->new_h_synx =
  1524. synx_util_get_fence_entry((u64)params->fence, global);
  1525. if (*params->new_h_synx == 0) {
  1526. /* create a new synx obj and add to fence map */
  1527. synx_util_map_import_params_to_create(params, &c_params);
  1528. scnprintf(name, SYNX_OBJ_NAME_LEN, "import-client-%d",
  1529. current->pid);
  1530. c_params.name = name;
  1531. c_params.h_synx = params->new_h_synx;
  1532. c_params.fence = params->fence;
  1533. rc = synx_native_create_core(client, &c_params);
  1534. if (rc != SYNX_SUCCESS)
  1535. return rc;
  1536. curr_h_synx = *params->new_h_synx;
  1537. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  1538. if (IS_ERR_OR_NULL(entry)) {
  1539. rc = -SYNX_NOMEM;
  1540. curr_h_synx = *c_params.h_synx;
  1541. goto fail;
  1542. }
  1543. do {
  1544. entry->key = (u64)params->fence;
  1545. if (global)
  1546. entry->g_handle = *params->new_h_synx;
  1547. else
  1548. entry->l_handle = *params->new_h_synx;
  1549. rc = synx_util_insert_fence_entry(entry,
  1550. params->new_h_synx, global);
  1551. if (rc == SYNX_SUCCESS) {
  1552. dprintk(SYNX_DBG,
  1553. "mapped fence %pK to new handle %u\n",
  1554. params->fence, *params->new_h_synx);
  1555. break;
  1556. } else if (rc == -SYNX_ALREADY) {
  1557. /*
  1558. * release the new handle allocated
  1559. * and use the available handle
  1560. * already mapped instead.
  1561. */
  1562. map_entry = synx_util_get_map_entry(
  1563. *params->new_h_synx);
  1564. if (IS_ERR_OR_NULL(map_entry)) {
  1565. /* race with fence release, need to retry */
  1566. dprintk(SYNX_DBG,
  1567. "re-attempting handle import\n");
  1568. *params->new_h_synx = curr_h_synx;
  1569. continue;
  1570. }
  1571. rc = synx_util_init_handle(client,
  1572. map_entry->synx_obj,
  1573. params->new_h_synx, map_entry);
  1574. dprintk(SYNX_DBG, "mapped fence %pK to handle %u\n",
  1575. params->fence, *params->new_h_synx);
  1576. goto release;
  1577. } else {
  1578. dprintk(SYNX_ERR,
  1579. "importing fence %pK failed, err=%d\n",
  1580. params->fence, rc);
  1581. goto release;
  1582. }
  1583. } while (true);
  1584. } else {
  1585. /* check if already mapped to client */
  1586. spin_lock_bh(&client->handle_map_lock);
  1587. hash_for_each_possible(client->handle_map,
  1588. curr, node, *params->new_h_synx) {
  1589. if (curr->key == *params->new_h_synx &&
  1590. curr->rel_count != 0) {
  1591. curr->rel_count++;
  1592. kref_get(&curr->refcount);
  1593. synx_data = curr;
  1594. break;
  1595. }
  1596. }
  1597. spin_unlock_bh(&client->handle_map_lock);
  1598. if (synx_data) {
  1599. dprintk(SYNX_DBG, "mapped fence %pK to handle %u\n",
  1600. params->fence, *params->new_h_synx);
  1601. return SYNX_SUCCESS;
  1602. }
  1603. if (global && !synx_util_is_global_handle(
  1604. *params->new_h_synx))
  1605. map_entry = synx_handle_conversion(client,
  1606. params->new_h_synx, NULL);
  1607. else
  1608. map_entry = synx_util_get_map_entry(
  1609. *params->new_h_synx);
  1610. if (IS_ERR_OR_NULL(map_entry)) {
  1611. /* race with fence release, need to retry */
  1612. dprintk(SYNX_DBG, "re-attempting handle import\n");
  1613. goto retry;
  1614. }
  1615. rc = synx_util_init_handle(client, map_entry->synx_obj,
  1616. params->new_h_synx, map_entry);
  1617. dprintk(SYNX_DBG, "mapped fence %pK to existing handle %u\n",
  1618. params->fence, *params->new_h_synx);
  1619. }
  1620. return rc;
  1621. release:
  1622. kfree(entry);
  1623. fail:
  1624. synx_native_release_core(client, curr_h_synx);
  1625. return rc;
  1626. }
  1627. static int synx_native_import_indv(struct synx_client *client,
  1628. struct synx_import_indv_params *params)
  1629. {
  1630. int rc = -SYNX_INVALID;
  1631. if (IS_ERR_OR_NULL(params) ||
  1632. IS_ERR_OR_NULL(params->new_h_synx) ||
  1633. IS_ERR_OR_NULL(params->fence)) {
  1634. dprintk(SYNX_ERR, "invalid import arguments\n");
  1635. return -SYNX_INVALID;
  1636. }
  1637. if (likely(params->flags & SYNX_IMPORT_DMA_FENCE))
  1638. rc = synx_native_import_fence(client, params);
  1639. else if (params->flags & SYNX_IMPORT_SYNX_FENCE)
  1640. rc = synx_native_import_handle(client, params);
  1641. dprintk(SYNX_DBG,
  1642. "[sess :%llu] import of fence %pK %s, handle %u\n",
  1643. client->id, params->fence,
  1644. rc ? "failed" : "successful",
  1645. rc ? 0 : *params->new_h_synx);
  1646. return rc;
  1647. }
  1648. static int synx_native_import_arr(struct synx_client *client,
  1649. struct synx_import_arr_params *params)
  1650. {
  1651. u32 i;
  1652. int rc = SYNX_SUCCESS;
  1653. if (IS_ERR_OR_NULL(params) || params->num_fences == 0) {
  1654. dprintk(SYNX_ERR, "invalid import arr arguments\n");
  1655. return -SYNX_INVALID;
  1656. }
  1657. for (i = 0; i < params->num_fences; i++) {
  1658. rc = synx_native_import_indv(client, &params->list[i]);
  1659. if (rc != SYNX_SUCCESS) {
  1660. dprintk(SYNX_ERR,
  1661. "importing fence[%u] %pK failed=%d\n",
  1662. i, params->list[i].fence, rc);
  1663. break;
  1664. }
  1665. }
  1666. if (rc != SYNX_SUCCESS)
  1667. while (i--) {
  1668. /* release the imported handles and cleanup */
  1669. if (synx_native_release_core(client,
  1670. *params->list[i].new_h_synx) != SYNX_SUCCESS)
  1671. dprintk(SYNX_ERR,
  1672. "error cleaning up imported handle[%u] %u\n",
  1673. i, *params->list[i].new_h_synx);
  1674. }
  1675. return rc;
  1676. }
  1677. int synx_import(struct synx_session *session,
  1678. struct synx_import_params *params)
  1679. {
  1680. int rc = 0;
  1681. struct synx_client *client;
  1682. if (IS_ERR_OR_NULL(params)) {
  1683. dprintk(SYNX_ERR, "invalid import arguments\n");
  1684. return -SYNX_INVALID;
  1685. }
  1686. client = synx_get_client(session);
  1687. if (IS_ERR_OR_NULL(client))
  1688. return -SYNX_INVALID;
  1689. /* import fence based on its type */
  1690. if (params->type == SYNX_IMPORT_ARR_PARAMS)
  1691. rc = synx_native_import_arr(client, &params->arr);
  1692. else
  1693. rc = synx_native_import_indv(client, &params->indv);
  1694. synx_put_client(client);
  1695. return rc;
  1696. }
  1697. EXPORT_SYMBOL(synx_import);
  1698. static int synx_handle_create(struct synx_private_ioctl_arg *k_ioctl,
  1699. struct synx_session *session)
  1700. {
  1701. int result;
  1702. int csl_fence;
  1703. struct synx_create_v2 create_info;
  1704. struct synx_create_params params = {0};
  1705. if (k_ioctl->size != sizeof(create_info))
  1706. return -SYNX_INVALID;
  1707. if (copy_from_user(&create_info,
  1708. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1709. k_ioctl->size))
  1710. return -EFAULT;
  1711. params.h_synx = &create_info.synx_obj;
  1712. params.name = create_info.name;
  1713. params.flags = create_info.flags;
  1714. if (create_info.flags & SYNX_CREATE_CSL_FENCE) {
  1715. csl_fence = create_info.desc.id[0];
  1716. params.fence = &csl_fence;
  1717. }
  1718. result = synx_create(session, &params);
  1719. if (!result)
  1720. if (copy_to_user(u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1721. &create_info,
  1722. k_ioctl->size))
  1723. return -EFAULT;
  1724. return result;
  1725. }
  1726. static int synx_handle_getstatus(struct synx_private_ioctl_arg *k_ioctl,
  1727. struct synx_session *session)
  1728. {
  1729. struct synx_signal_v2 signal_info;
  1730. if (k_ioctl->size != sizeof(signal_info))
  1731. return -SYNX_INVALID;
  1732. if (copy_from_user(&signal_info,
  1733. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1734. k_ioctl->size))
  1735. return -EFAULT;
  1736. signal_info.synx_state =
  1737. synx_get_status(session, signal_info.synx_obj);
  1738. if (copy_to_user(u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1739. &signal_info,
  1740. k_ioctl->size))
  1741. return -EFAULT;
  1742. return SYNX_SUCCESS;
  1743. }
  1744. static int synx_handle_import(struct synx_private_ioctl_arg *k_ioctl,
  1745. struct synx_session *session)
  1746. {
  1747. struct synx_import_info import_info;
  1748. struct synx_import_params params = {0};
  1749. int result = SYNX_SUCCESS;
  1750. if (k_ioctl->size != sizeof(import_info))
  1751. return -SYNX_INVALID;
  1752. if (copy_from_user(&import_info,
  1753. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1754. k_ioctl->size))
  1755. return -EFAULT;
  1756. if (import_info.flags & SYNX_IMPORT_DMA_FENCE)
  1757. params.indv.fence =
  1758. sync_file_get_fence(import_info.desc.id[0]);
  1759. else if (import_info.flags & SYNX_IMPORT_SYNX_FENCE)
  1760. params.indv.fence = &import_info.synx_obj;
  1761. params.type = SYNX_IMPORT_INDV_PARAMS;
  1762. params.indv.flags = import_info.flags;
  1763. params.indv.new_h_synx = &import_info.new_synx_obj;
  1764. if (synx_import(session, &params))
  1765. result = -SYNX_INVALID;
  1766. // Fence needs to be put irresepctive of import status
  1767. if (import_info.flags & SYNX_IMPORT_DMA_FENCE)
  1768. dma_fence_put(params.indv.fence);
  1769. if (result != SYNX_SUCCESS)
  1770. return result;
  1771. if (copy_to_user(u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1772. &import_info,
  1773. k_ioctl->size))
  1774. return -EFAULT;
  1775. return result;
  1776. }
  1777. static int synx_handle_import_arr(
  1778. struct synx_private_ioctl_arg *k_ioctl,
  1779. struct synx_session *session)
  1780. {
  1781. int rc = -SYNX_INVALID;
  1782. u32 idx = 0;
  1783. struct synx_client *client;
  1784. struct synx_import_arr_info arr_info;
  1785. struct synx_import_info *arr;
  1786. struct synx_import_indv_params params = {0};
  1787. if (k_ioctl->size != sizeof(arr_info))
  1788. return -SYNX_INVALID;
  1789. if (copy_from_user(&arr_info,
  1790. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1791. k_ioctl->size))
  1792. return -EFAULT;
  1793. arr = kcalloc(arr_info.num_objs,
  1794. sizeof(*arr), GFP_KERNEL);
  1795. if (IS_ERR_OR_NULL(arr))
  1796. return -ENOMEM;
  1797. client = synx_get_client(session);
  1798. if (IS_ERR_OR_NULL(client)) {
  1799. rc = PTR_ERR(client);
  1800. goto clean;
  1801. }
  1802. if (copy_from_user(arr,
  1803. u64_to_user_ptr(arr_info.list),
  1804. sizeof(*arr) * arr_info.num_objs)) {
  1805. rc = -EFAULT;
  1806. goto fail;
  1807. }
  1808. while (idx < arr_info.num_objs) {
  1809. params.new_h_synx = &arr[idx].new_synx_obj;
  1810. params.flags = arr[idx].flags;
  1811. if (arr[idx].flags & SYNX_IMPORT_DMA_FENCE)
  1812. params.fence =
  1813. sync_file_get_fence(arr[idx].desc.id[0]);
  1814. else if (arr[idx].flags & SYNX_IMPORT_SYNX_FENCE)
  1815. params.fence = &arr[idx].synx_obj;
  1816. rc = synx_native_import_indv(client, &params);
  1817. // Fence needs to be put irresepctive of import status
  1818. if (arr[idx].flags & SYNX_IMPORT_DMA_FENCE)
  1819. dma_fence_put(params.fence);
  1820. if (rc != SYNX_SUCCESS)
  1821. break;
  1822. idx++;
  1823. }
  1824. /* release allocated handles in case of failure */
  1825. if (rc != SYNX_SUCCESS) {
  1826. while (idx > 0)
  1827. synx_native_release_core(client,
  1828. arr[--idx].new_synx_obj);
  1829. } else {
  1830. if (copy_to_user(u64_to_user_ptr(arr_info.list),
  1831. arr,
  1832. sizeof(*arr) * arr_info.num_objs)) {
  1833. rc = -EFAULT;
  1834. goto fail;
  1835. }
  1836. }
  1837. fail:
  1838. synx_put_client(client);
  1839. clean:
  1840. kfree(arr);
  1841. return rc;
  1842. }
  1843. static int synx_handle_export(struct synx_private_ioctl_arg *k_ioctl,
  1844. struct synx_session *session)
  1845. {
  1846. return -SYNX_INVALID;
  1847. }
  1848. static int synx_handle_signal(struct synx_private_ioctl_arg *k_ioctl,
  1849. struct synx_session *session)
  1850. {
  1851. struct synx_signal_v2 signal_info;
  1852. if (k_ioctl->size != sizeof(signal_info))
  1853. return -SYNX_INVALID;
  1854. if (copy_from_user(&signal_info,
  1855. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1856. k_ioctl->size))
  1857. return -EFAULT;
  1858. return synx_signal(session, signal_info.synx_obj,
  1859. signal_info.synx_state);
  1860. }
  1861. static int synx_handle_merge(struct synx_private_ioctl_arg *k_ioctl,
  1862. struct synx_session *session)
  1863. {
  1864. u32 *h_synxs;
  1865. int result;
  1866. struct synx_merge_v2 merge_info;
  1867. struct synx_merge_params params = {0};
  1868. if (k_ioctl->size != sizeof(merge_info))
  1869. return -SYNX_INVALID;
  1870. if (copy_from_user(&merge_info,
  1871. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1872. k_ioctl->size))
  1873. return -EFAULT;
  1874. if (merge_info.num_objs >= SYNX_MAX_OBJS)
  1875. return -SYNX_INVALID;
  1876. h_synxs = kcalloc(merge_info.num_objs,
  1877. sizeof(*h_synxs), GFP_KERNEL);
  1878. if (IS_ERR_OR_NULL(h_synxs))
  1879. return -ENOMEM;
  1880. if (copy_from_user(h_synxs,
  1881. u64_to_user_ptr(merge_info.synx_objs),
  1882. sizeof(u32) * merge_info.num_objs)) {
  1883. kfree(h_synxs);
  1884. return -EFAULT;
  1885. }
  1886. params.num_objs = merge_info.num_objs;
  1887. params.h_synxs = h_synxs;
  1888. params.flags = merge_info.flags;
  1889. params.h_merged_obj = &merge_info.merged;
  1890. result = synx_merge(session, &params);
  1891. if (!result)
  1892. if (copy_to_user(u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1893. &merge_info,
  1894. k_ioctl->size)) {
  1895. kfree(h_synxs);
  1896. return -EFAULT;
  1897. }
  1898. kfree(h_synxs);
  1899. return result;
  1900. }
  1901. static int synx_handle_wait(struct synx_private_ioctl_arg *k_ioctl,
  1902. struct synx_session *session)
  1903. {
  1904. struct synx_wait_v2 wait_info;
  1905. if (k_ioctl->size != sizeof(wait_info))
  1906. return -SYNX_INVALID;
  1907. if (copy_from_user(&wait_info,
  1908. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1909. k_ioctl->size))
  1910. return -EFAULT;
  1911. k_ioctl->result = synx_wait(session,
  1912. wait_info.synx_obj, wait_info.timeout_ms);
  1913. return SYNX_SUCCESS;
  1914. }
  1915. static int synx_handle_async_wait(
  1916. struct synx_private_ioctl_arg *k_ioctl,
  1917. struct synx_session *session)
  1918. {
  1919. int rc = 0;
  1920. struct synx_userpayload_info_v2 user_data;
  1921. struct synx_callback_params params = {0};
  1922. if (k_ioctl->size != sizeof(user_data))
  1923. return -SYNX_INVALID;
  1924. if (copy_from_user(&user_data,
  1925. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1926. k_ioctl->size))
  1927. return -EFAULT;
  1928. params.h_synx = user_data.synx_obj;
  1929. params.cb_func = synx_util_default_user_callback;
  1930. params.userdata = (void *)user_data.payload[0];
  1931. params.timeout_ms = user_data.payload[2];
  1932. rc = synx_async_wait(session, &params);
  1933. if (rc)
  1934. dprintk(SYNX_ERR,
  1935. "user cb registration failed for handle %d\n",
  1936. user_data.synx_obj);
  1937. return rc;
  1938. }
  1939. static int synx_handle_cancel_async_wait(
  1940. struct synx_private_ioctl_arg *k_ioctl,
  1941. struct synx_session *session)
  1942. {
  1943. int rc = 0;
  1944. struct synx_userpayload_info_v2 user_data;
  1945. struct synx_callback_params params = {0};
  1946. if (k_ioctl->size != sizeof(user_data))
  1947. return -SYNX_INVALID;
  1948. if (copy_from_user(&user_data,
  1949. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1950. k_ioctl->size))
  1951. return -EFAULT;
  1952. params.h_synx = user_data.synx_obj;
  1953. params.cb_func = synx_util_default_user_callback;
  1954. params.userdata = (void *)user_data.payload[0];
  1955. rc = synx_cancel_async_wait(session, &params);
  1956. if (rc)
  1957. dprintk(SYNX_ERR,
  1958. "user cb deregistration failed for handle %d\n",
  1959. user_data.synx_obj);
  1960. return rc;
  1961. }
  1962. static int synx_handle_bind(struct synx_private_ioctl_arg *k_ioctl,
  1963. struct synx_session *session)
  1964. {
  1965. struct synx_bind_v2 synx_bind_info;
  1966. if (k_ioctl->size != sizeof(synx_bind_info))
  1967. return -SYNX_INVALID;
  1968. if (copy_from_user(&synx_bind_info,
  1969. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1970. k_ioctl->size))
  1971. return -EFAULT;
  1972. k_ioctl->result = synx_bind(session,
  1973. synx_bind_info.synx_obj,
  1974. synx_bind_info.ext_sync_desc);
  1975. return k_ioctl->result;
  1976. }
  1977. static int synx_handle_release(struct synx_private_ioctl_arg *k_ioctl,
  1978. struct synx_session *session)
  1979. {
  1980. struct synx_info release_info;
  1981. if (k_ioctl->size != sizeof(release_info))
  1982. return -SYNX_INVALID;
  1983. if (copy_from_user(&release_info,
  1984. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1985. k_ioctl->size))
  1986. return -EFAULT;
  1987. return synx_release(session, release_info.synx_obj);
  1988. }
  1989. static int synx_handle_get_fence(struct synx_private_ioctl_arg *k_ioctl,
  1990. struct synx_session *session)
  1991. {
  1992. struct synx_fence_fd fence_fd;
  1993. struct dma_fence *fence;
  1994. if (k_ioctl->size != sizeof(fence_fd))
  1995. return -SYNX_INVALID;
  1996. if (copy_from_user(&fence_fd,
  1997. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1998. k_ioctl->size))
  1999. return -EFAULT;
  2000. fence = synx_get_fence(session, fence_fd.synx_obj);
  2001. fence_fd.fd = synx_create_sync_fd(fence);
  2002. /*
  2003. * release additional reference taken in synx_get_fence.
  2004. * additional reference ensures the fence is valid and
  2005. * does not race with handle/fence release.
  2006. */
  2007. dma_fence_put(fence);
  2008. if (copy_to_user(u64_to_user_ptr(k_ioctl->ioctl_ptr),
  2009. &fence_fd, k_ioctl->size))
  2010. return -EFAULT;
  2011. return SYNX_SUCCESS;
  2012. }
  2013. static long synx_ioctl(struct file *filep,
  2014. unsigned int cmd,
  2015. unsigned long arg)
  2016. {
  2017. s32 rc = 0;
  2018. struct synx_private_ioctl_arg k_ioctl;
  2019. struct synx_session *session = filep->private_data;
  2020. if (cmd != SYNX_PRIVATE_IOCTL_CMD) {
  2021. dprintk(SYNX_ERR, "invalid ioctl cmd\n");
  2022. return -ENOIOCTLCMD;
  2023. }
  2024. if (copy_from_user(&k_ioctl,
  2025. (struct synx_private_ioctl_arg *)arg,
  2026. sizeof(k_ioctl))) {
  2027. dprintk(SYNX_ERR, "invalid ioctl args\n");
  2028. return -EFAULT;
  2029. }
  2030. if (!k_ioctl.ioctl_ptr)
  2031. return -SYNX_INVALID;
  2032. dprintk(SYNX_VERB, "[sess :%llu] Enter cmd %u from pid %d\n",
  2033. ((struct synx_client *)session)->id,
  2034. k_ioctl.id, current->pid);
  2035. switch (k_ioctl.id) {
  2036. case SYNX_CREATE:
  2037. rc = synx_handle_create(&k_ioctl, session);
  2038. break;
  2039. case SYNX_RELEASE:
  2040. rc = synx_handle_release(&k_ioctl, session);
  2041. break;
  2042. case SYNX_REGISTER_PAYLOAD:
  2043. rc = synx_handle_async_wait(&k_ioctl,
  2044. session);
  2045. break;
  2046. case SYNX_DEREGISTER_PAYLOAD:
  2047. rc = synx_handle_cancel_async_wait(&k_ioctl,
  2048. session);
  2049. break;
  2050. case SYNX_SIGNAL:
  2051. rc = synx_handle_signal(&k_ioctl, session);
  2052. break;
  2053. case SYNX_MERGE:
  2054. rc = synx_handle_merge(&k_ioctl, session);
  2055. break;
  2056. case SYNX_WAIT:
  2057. rc = synx_handle_wait(&k_ioctl, session);
  2058. if (copy_to_user((void *)arg,
  2059. &k_ioctl,
  2060. sizeof(k_ioctl))) {
  2061. dprintk(SYNX_ERR, "invalid ioctl args\n");
  2062. rc = -EFAULT;
  2063. }
  2064. break;
  2065. case SYNX_BIND:
  2066. rc = synx_handle_bind(&k_ioctl, session);
  2067. break;
  2068. case SYNX_GETSTATUS:
  2069. rc = synx_handle_getstatus(&k_ioctl, session);
  2070. break;
  2071. case SYNX_IMPORT:
  2072. rc = synx_handle_import(&k_ioctl, session);
  2073. break;
  2074. case SYNX_IMPORT_ARR:
  2075. rc = synx_handle_import_arr(&k_ioctl, session);
  2076. break;
  2077. case SYNX_EXPORT:
  2078. rc = synx_handle_export(&k_ioctl, session);
  2079. break;
  2080. case SYNX_GETFENCE_FD:
  2081. rc = synx_handle_get_fence(&k_ioctl, session);
  2082. break;
  2083. default:
  2084. rc = -SYNX_INVALID;
  2085. }
  2086. dprintk(SYNX_VERB, "[sess :%llu] exit with status %d\n",
  2087. ((struct synx_client *)session)->id, rc);
  2088. return rc;
  2089. }
  2090. static ssize_t synx_read(struct file *filep,
  2091. char __user *buf, size_t size, loff_t *f_pos)
  2092. {
  2093. ssize_t rc = 0;
  2094. struct synx_client *client = NULL;
  2095. struct synx_client_cb *cb;
  2096. struct synx_session *session = filep->private_data;
  2097. struct synx_userpayload_info_v2 data;
  2098. if (size != sizeof(struct synx_userpayload_info_v2)) {
  2099. dprintk(SYNX_ERR, "invalid read size\n");
  2100. return -SYNX_INVALID;
  2101. }
  2102. client = synx_get_client(session);
  2103. if (IS_ERR_OR_NULL(client))
  2104. return -SYNX_INVALID;
  2105. mutex_lock(&client->event_q_lock);
  2106. cb = list_first_entry_or_null(&client->event_q,
  2107. struct synx_client_cb, node);
  2108. if (IS_ERR_OR_NULL(cb)) {
  2109. mutex_unlock(&client->event_q_lock);
  2110. rc = 0;
  2111. goto fail;
  2112. }
  2113. if (cb->idx == 0 || cb->idx >= SYNX_MAX_OBJS) {
  2114. dprintk(SYNX_ERR, "invalid index\n");
  2115. mutex_unlock(&client->event_q_lock);
  2116. rc = -SYNX_INVALID;
  2117. goto fail;
  2118. }
  2119. list_del_init(&cb->node);
  2120. mutex_unlock(&client->event_q_lock);
  2121. memset(&data, 0, sizeof(struct synx_userpayload_info_v2));
  2122. rc = size;
  2123. data.synx_obj = cb->kernel_cb.h_synx;
  2124. data.reserved = cb->kernel_cb.status;
  2125. data.payload[0] = (u64)cb->kernel_cb.data;
  2126. if (copy_to_user(buf,
  2127. &data,
  2128. sizeof(struct synx_userpayload_info_v2))) {
  2129. dprintk(SYNX_ERR, "couldn't copy user callback data\n");
  2130. rc = -EFAULT;
  2131. }
  2132. if (synx_util_clear_cb_entry(client, cb))
  2133. dprintk(SYNX_ERR,
  2134. "[sess :%llu] error clearing cb for handle %u\n",
  2135. client->id, data.synx_obj);
  2136. fail:
  2137. synx_put_client(client);
  2138. return rc;
  2139. }
  2140. static unsigned int synx_poll(struct file *filep,
  2141. struct poll_table_struct *poll_table)
  2142. {
  2143. int rc = 0;
  2144. struct synx_client *client;
  2145. struct synx_session *session = filep->private_data;
  2146. client = synx_get_client(session);
  2147. if (IS_ERR_OR_NULL(client)) {
  2148. dprintk(SYNX_ERR, "invalid session in poll\n");
  2149. return SYNX_SUCCESS;
  2150. }
  2151. poll_wait(filep, &client->event_wq, poll_table);
  2152. mutex_lock(&client->event_q_lock);
  2153. if (!list_empty(&client->event_q))
  2154. rc = POLLPRI;
  2155. mutex_unlock(&client->event_q_lock);
  2156. synx_put_client(client);
  2157. return rc;
  2158. }
  2159. struct synx_session *synx_initialize(
  2160. struct synx_initialization_params *params)
  2161. {
  2162. struct synx_client *client;
  2163. if (IS_ERR_OR_NULL(params))
  2164. return ERR_PTR(-SYNX_INVALID);
  2165. client = vzalloc(sizeof(*client));
  2166. if (IS_ERR_OR_NULL(client))
  2167. return ERR_PTR(-SYNX_NOMEM);
  2168. if (params->name)
  2169. strlcpy(client->name, params->name, sizeof(client->name));
  2170. client->active = true;
  2171. client->dma_context = dma_fence_context_alloc(1);
  2172. client->id = atomic64_inc_return(&synx_counter);
  2173. kref_init(&client->refcount);
  2174. spin_lock_init(&client->handle_map_lock);
  2175. mutex_init(&client->event_q_lock);
  2176. INIT_LIST_HEAD(&client->event_q);
  2177. init_waitqueue_head(&client->event_wq);
  2178. /* zero idx not allowed */
  2179. set_bit(0, client->cb_bitmap);
  2180. spin_lock_bh(&synx_dev->native->metadata_map_lock);
  2181. hash_add(synx_dev->native->client_metadata_map,
  2182. &client->node, (u64)client);
  2183. spin_unlock_bh(&synx_dev->native->metadata_map_lock);
  2184. dprintk(SYNX_INFO, "[sess :%llu] session created %s\n",
  2185. client->id, params->name);
  2186. return (struct synx_session *)client;
  2187. }
  2188. EXPORT_SYMBOL(synx_initialize);
  2189. int synx_uninitialize(struct synx_session *session)
  2190. {
  2191. struct synx_client *client = NULL, *curr;
  2192. spin_lock_bh(&synx_dev->native->metadata_map_lock);
  2193. hash_for_each_possible(synx_dev->native->client_metadata_map,
  2194. curr, node, (u64)session) {
  2195. if (curr == (struct synx_client *)session) {
  2196. if (curr->active) {
  2197. curr->active = false;
  2198. client = curr;
  2199. }
  2200. break;
  2201. }
  2202. }
  2203. spin_unlock_bh(&synx_dev->native->metadata_map_lock);
  2204. /* release the reference obtained at synx init */
  2205. synx_put_client(client);
  2206. return SYNX_SUCCESS;
  2207. }
  2208. EXPORT_SYMBOL(synx_uninitialize);
  2209. static int synx_open(struct inode *inode, struct file *filep)
  2210. {
  2211. int rc = 0;
  2212. char name[SYNX_OBJ_NAME_LEN];
  2213. struct synx_initialization_params params = {0};
  2214. dprintk(SYNX_VERB, "Enter pid: %d\n", current->pid);
  2215. scnprintf(name, SYNX_OBJ_NAME_LEN, "umd-client-%d", current->pid);
  2216. params.name = name;
  2217. params.id = SYNX_CLIENT_NATIVE;
  2218. filep->private_data = synx_initialize(&params);
  2219. if (IS_ERR_OR_NULL(filep->private_data)) {
  2220. dprintk(SYNX_ERR, "session allocation failed for pid: %d\n",
  2221. current->pid);
  2222. rc = PTR_ERR(filep->private_data);
  2223. } else {
  2224. dprintk(SYNX_VERB, "allocated new session for pid: %d\n",
  2225. current->pid);
  2226. }
  2227. return rc;
  2228. }
  2229. static int synx_close(struct inode *inode, struct file *filep)
  2230. {
  2231. struct synx_session *session = filep->private_data;
  2232. return synx_uninitialize(session);
  2233. }
  2234. static const struct file_operations synx_fops = {
  2235. .owner = THIS_MODULE,
  2236. .open = synx_open,
  2237. .read = synx_read,
  2238. .release = synx_close,
  2239. .poll = synx_poll,
  2240. .unlocked_ioctl = synx_ioctl,
  2241. #ifdef CONFIG_COMPAT
  2242. .compat_ioctl = synx_ioctl,
  2243. #endif
  2244. };
  2245. int synx_register_ops(
  2246. const struct synx_register_params *params)
  2247. {
  2248. s32 rc = 0;
  2249. struct synx_registered_ops *client_ops;
  2250. if (!synx_dev || !params || !params->name ||
  2251. !synx_util_is_valid_bind_type(params->type) ||
  2252. !params->ops.register_callback ||
  2253. !params->ops.deregister_callback ||
  2254. !params->ops.signal) {
  2255. dprintk(SYNX_ERR, "invalid register params\n");
  2256. return -SYNX_INVALID;
  2257. }
  2258. mutex_lock(&synx_dev->vtbl_lock);
  2259. client_ops = &synx_dev->bind_vtbl[params->type];
  2260. if (!client_ops->valid) {
  2261. client_ops->valid = true;
  2262. memcpy(&client_ops->ops, &params->ops,
  2263. sizeof(client_ops->ops));
  2264. strlcpy(client_ops->name, params->name,
  2265. sizeof(client_ops->name));
  2266. client_ops->type = params->type;
  2267. dprintk(SYNX_INFO,
  2268. "registered bind ops type %u for %s\n",
  2269. params->type, params->name);
  2270. } else {
  2271. dprintk(SYNX_WARN,
  2272. "client already registered for type %u by %s\n",
  2273. client_ops->type, client_ops->name);
  2274. rc = -SYNX_ALREADY;
  2275. }
  2276. mutex_unlock(&synx_dev->vtbl_lock);
  2277. return rc;
  2278. }
  2279. EXPORT_SYMBOL(synx_register_ops);
  2280. int synx_deregister_ops(
  2281. const struct synx_register_params *params)
  2282. {
  2283. struct synx_registered_ops *client_ops;
  2284. if (IS_ERR_OR_NULL(params) || params->name ||
  2285. !synx_util_is_valid_bind_type(params->type)) {
  2286. dprintk(SYNX_ERR, "invalid params\n");
  2287. return -SYNX_INVALID;
  2288. }
  2289. mutex_lock(&synx_dev->vtbl_lock);
  2290. client_ops = &synx_dev->bind_vtbl[params->type];
  2291. memset(client_ops, 0, sizeof(*client_ops));
  2292. dprintk(SYNX_INFO, "deregistered bind ops for %s\n",
  2293. params->name);
  2294. mutex_unlock(&synx_dev->vtbl_lock);
  2295. return SYNX_SUCCESS;
  2296. }
  2297. EXPORT_SYMBOL(synx_deregister_ops);
  2298. void synx_ipc_handler(struct work_struct *cb_dispatch)
  2299. {
  2300. struct synx_signal_cb *signal_cb =
  2301. container_of(cb_dispatch, struct synx_signal_cb, cb_dispatch);
  2302. struct synx_map_entry *map_entry;
  2303. map_entry = synx_util_get_map_entry(signal_cb->handle);
  2304. if (IS_ERR_OR_NULL(map_entry)) {
  2305. dprintk(SYNX_WARN,
  2306. "no clients to notify for %u\n",
  2307. signal_cb->handle);
  2308. dprintk(SYNX_MEM, "signal cb destroyed %pK\n", signal_cb);
  2309. kfree(signal_cb);
  2310. return;
  2311. }
  2312. /* get reference on synx coredata for signal cb */
  2313. synx_util_get_object(map_entry->synx_obj);
  2314. signal_cb->synx_obj = map_entry->synx_obj;
  2315. synx_util_release_map_entry(map_entry);
  2316. synx_signal_handler(&signal_cb->cb_dispatch);
  2317. }
  2318. int synx_ipc_callback(u32 client_id,
  2319. s64 data, void *priv)
  2320. {
  2321. struct synx_signal_cb *signal_cb;
  2322. u32 status = (u32)data;
  2323. u32 handle = (u32)(data >> 32);
  2324. signal_cb = kzalloc(sizeof(*signal_cb), GFP_ATOMIC);
  2325. if (IS_ERR_OR_NULL(signal_cb))
  2326. return -SYNX_NOMEM;
  2327. dprintk(SYNX_DBG,
  2328. "signal notification for %u received with status %u\n",
  2329. handle, status);
  2330. signal_cb->status = status;
  2331. signal_cb->handle = handle;
  2332. signal_cb->flag = SYNX_SIGNAL_FROM_IPC;
  2333. INIT_WORK(&signal_cb->cb_dispatch, synx_ipc_handler);
  2334. queue_work(synx_dev->wq_cb, &signal_cb->cb_dispatch);
  2335. return SYNX_SUCCESS;
  2336. }
  2337. EXPORT_SYMBOL(synx_ipc_callback);
  2338. int synx_recover(enum synx_client_id id)
  2339. {
  2340. u32 core_id;
  2341. core_id = synx_util_map_client_id_to_core(id);
  2342. if (core_id >= SYNX_CORE_MAX) {
  2343. dprintk(SYNX_ERR, "invalid client id %u\n", id);
  2344. return -SYNX_INVALID;
  2345. }
  2346. switch (core_id) {
  2347. case SYNX_CORE_EVA:
  2348. case SYNX_CORE_IRIS:
  2349. case SYNX_CORE_ICP:
  2350. break;
  2351. default:
  2352. dprintk(SYNX_ERR, "recovery not supported on %u\n", id);
  2353. return -SYNX_NOSUPPORT;
  2354. }
  2355. return synx_global_recover(core_id);
  2356. }
  2357. EXPORT_SYMBOL(synx_recover);
  2358. static int synx_local_mem_init(void)
  2359. {
  2360. if (!synx_dev->native)
  2361. return -SYNX_INVALID;
  2362. hash_init(synx_dev->native->client_metadata_map);
  2363. hash_init(synx_dev->native->fence_map);
  2364. hash_init(synx_dev->native->global_map);
  2365. hash_init(synx_dev->native->local_map);
  2366. hash_init(synx_dev->native->csl_fence_map);
  2367. spin_lock_init(&synx_dev->native->metadata_map_lock);
  2368. spin_lock_init(&synx_dev->native->fence_map_lock);
  2369. spin_lock_init(&synx_dev->native->global_map_lock);
  2370. spin_lock_init(&synx_dev->native->local_map_lock);
  2371. spin_lock_init(&synx_dev->native->csl_map_lock);
  2372. /* zero idx not allowed */
  2373. set_bit(0, synx_dev->native->bitmap);
  2374. return 0;
  2375. }
  2376. static int synx_cdsp_restart_notifier(struct notifier_block *nb,
  2377. unsigned long code, void *data)
  2378. {
  2379. struct synx_cdsp_ssr *cdsp_ssr = &synx_dev->cdsp_ssr;
  2380. if (&cdsp_ssr->nb != nb) {
  2381. dprintk(SYNX_ERR, "Invalid SSR Notifier block\n");
  2382. return NOTIFY_BAD;
  2383. }
  2384. switch (code) {
  2385. case QCOM_SSR_BEFORE_SHUTDOWN:
  2386. break;
  2387. case QCOM_SSR_AFTER_SHUTDOWN:
  2388. if (cdsp_ssr->ssrcnt != 0) {
  2389. dprintk(SYNX_INFO, "Cleaning up global memory\n");
  2390. synx_global_recover(SYNX_CORE_NSP);
  2391. }
  2392. break;
  2393. case QCOM_SSR_BEFORE_POWERUP:
  2394. break;
  2395. case QCOM_SSR_AFTER_POWERUP:
  2396. dprintk(SYNX_DBG, "CDSP is up");
  2397. if (cdsp_ssr->ssrcnt == 0)
  2398. cdsp_ssr->ssrcnt++;
  2399. break;
  2400. default:
  2401. dprintk(SYNX_ERR, "Unknown status code for CDSP SSR\n");
  2402. break;
  2403. }
  2404. return NOTIFY_DONE;
  2405. }
  2406. static int __init synx_init(void)
  2407. {
  2408. int rc;
  2409. dprintk(SYNX_INFO, "device initialization start\n");
  2410. synx_dev = kzalloc(sizeof(*synx_dev), GFP_KERNEL);
  2411. if (IS_ERR_OR_NULL(synx_dev))
  2412. return -SYNX_NOMEM;
  2413. rc = alloc_chrdev_region(&synx_dev->dev, 0, 1, SYNX_DEVICE_NAME);
  2414. if (rc < 0) {
  2415. dprintk(SYNX_ERR, "region allocation failed\n");
  2416. goto alloc_fail;
  2417. }
  2418. cdev_init(&synx_dev->cdev, &synx_fops);
  2419. synx_dev->cdev.owner = THIS_MODULE;
  2420. rc = cdev_add(&synx_dev->cdev, synx_dev->dev, 1);
  2421. if (rc < 0) {
  2422. dprintk(SYNX_ERR, "device registation failed\n");
  2423. goto reg_fail;
  2424. }
  2425. synx_dev->class = class_create(THIS_MODULE, SYNX_DEVICE_NAME);
  2426. if (IS_ERR(synx_dev->class)) {
  2427. rc = PTR_ERR(synx_dev->class);
  2428. goto err_class_create;
  2429. }
  2430. device_create(synx_dev->class, NULL, synx_dev->dev,
  2431. NULL, SYNX_DEVICE_NAME);
  2432. synx_dev->wq_cb = alloc_workqueue(SYNX_WQ_CB_NAME,
  2433. WQ_HIGHPRI | WQ_UNBOUND, SYNX_WQ_CB_THREADS);
  2434. synx_dev->wq_cleanup = alloc_workqueue(SYNX_WQ_CLEANUP_NAME,
  2435. WQ_HIGHPRI | WQ_UNBOUND, SYNX_WQ_CLEANUP_THREADS);
  2436. if (!synx_dev->wq_cb || !synx_dev->wq_cleanup) {
  2437. dprintk(SYNX_ERR,
  2438. "high priority work queue creation failed\n");
  2439. rc = -SYNX_INVALID;
  2440. goto fail;
  2441. }
  2442. synx_dev->native = vzalloc(sizeof(*synx_dev->native));
  2443. if (IS_ERR_OR_NULL(synx_dev->native))
  2444. goto fail;
  2445. mutex_init(&synx_dev->vtbl_lock);
  2446. mutex_init(&synx_dev->error_lock);
  2447. INIT_LIST_HEAD(&synx_dev->error_list);
  2448. synx_dev->debugfs_root = synx_init_debugfs_dir(synx_dev);
  2449. rc = synx_global_mem_init();
  2450. if (rc) {
  2451. dprintk(SYNX_ERR, "shared mem init failed, err=%d\n", rc);
  2452. goto err;
  2453. }
  2454. synx_dev->cdsp_ssr.ssrcnt = 0;
  2455. synx_dev->cdsp_ssr.nb.notifier_call = synx_cdsp_restart_notifier;
  2456. synx_dev->cdsp_ssr.handle =
  2457. qcom_register_ssr_notifier("cdsp", &synx_dev->cdsp_ssr.nb);
  2458. if (synx_dev->cdsp_ssr.handle == NULL) {
  2459. dprintk(SYNX_ERR, "SSR registration failed\n");
  2460. goto err;
  2461. }
  2462. ipclite_register_client(synx_ipc_callback, NULL);
  2463. synx_local_mem_init();
  2464. dprintk(SYNX_INFO, "device initialization success\n");
  2465. return 0;
  2466. err:
  2467. vfree(synx_dev->native);
  2468. fail:
  2469. device_destroy(synx_dev->class, synx_dev->dev);
  2470. class_destroy(synx_dev->class);
  2471. err_class_create:
  2472. cdev_del(&synx_dev->cdev);
  2473. reg_fail:
  2474. unregister_chrdev_region(synx_dev->dev, 1);
  2475. alloc_fail:
  2476. kfree(synx_dev);
  2477. synx_dev = NULL;
  2478. return rc;
  2479. }
  2480. static void __exit synx_exit(void)
  2481. {
  2482. struct error_node *err_node, *err_node_tmp;
  2483. flush_workqueue(synx_dev->wq_cb);
  2484. flush_workqueue(synx_dev->wq_cleanup);
  2485. device_destroy(synx_dev->class, synx_dev->dev);
  2486. class_destroy(synx_dev->class);
  2487. cdev_del(&synx_dev->cdev);
  2488. unregister_chrdev_region(synx_dev->dev, 1);
  2489. synx_remove_debugfs_dir(synx_dev);
  2490. /* release uncleared error nodes */
  2491. list_for_each_entry_safe(
  2492. err_node, err_node_tmp,
  2493. &synx_dev->error_list,
  2494. node) {
  2495. list_del(&err_node->node);
  2496. kfree(err_node);
  2497. }
  2498. mutex_destroy(&synx_dev->vtbl_lock);
  2499. mutex_destroy(&synx_dev->error_lock);
  2500. vfree(synx_dev->native);
  2501. kfree(synx_dev);
  2502. }
  2503. module_init(synx_init);
  2504. module_exit(synx_exit);
  2505. MODULE_DESCRIPTION("Global Synx Driver");
  2506. MODULE_LICENSE("GPL v2");