synx.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2019-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023, 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. }
  520. custom_status = synx_obj->status;
  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. else
  853. status = synx_util_get_object_status(synx_obj);
  854. synx_cb->session = session;
  855. synx_cb->idx = idx;
  856. synx_cb->h_synx = params->h_synx;
  857. INIT_WORK(&synx_cb->cb_dispatch, synx_util_cb_dispatch);
  858. /* add callback if object still ACTIVE, dispatch if SIGNALED */
  859. if (status == SYNX_STATE_ACTIVE) {
  860. dprintk(SYNX_VERB,
  861. "[sess :%llu] callback added for handle %u\n",
  862. client->id, params->h_synx);
  863. synx_cb->timeout = params->timeout_ms;
  864. if (params->timeout_ms != SYNX_NO_TIMEOUT) {
  865. rc = synx_start_timer(synx_cb);
  866. if (rc != SYNX_SUCCESS) {
  867. dprintk(SYNX_ERR,
  868. "[sess :%llu] timer start failed - synx_cb: 0x%x, params->timeout_ms: 0x%llx, handle: 0x%x, ret : %d\n",
  869. client->id, synx_cb, params->timeout_ms,
  870. params->h_synx, rc);
  871. goto release;
  872. }
  873. }
  874. list_add(&synx_cb->node, &synx_obj->reg_cbs_list);
  875. } else {
  876. synx_cb->status = status;
  877. dprintk(SYNX_VERB,
  878. "[sess :%llu] callback queued for handle %u\n",
  879. client->id, params->h_synx);
  880. queue_work(synx_dev->wq_cb,
  881. &synx_cb->cb_dispatch);
  882. }
  883. release:
  884. mutex_unlock(&synx_obj->obj_lock);
  885. fail:
  886. synx_util_release_handle(synx_data);
  887. synx_put_client(client);
  888. return rc;
  889. }
  890. EXPORT_SYMBOL(synx_async_wait);
  891. int synx_cancel_async_wait(
  892. struct synx_session *session,
  893. struct synx_callback_params *params)
  894. {
  895. int rc = 0, ret = 0;
  896. u32 status;
  897. bool match_found = false;
  898. struct synx_client *client;
  899. struct synx_handle_coredata *synx_data;
  900. struct synx_coredata *synx_obj;
  901. struct synx_kernel_payload payload;
  902. struct synx_cb_data *synx_cb, *synx_cb_temp;
  903. struct synx_client_cb *cb_payload;
  904. if (IS_ERR_OR_NULL(session) || IS_ERR_OR_NULL(params))
  905. return -SYNX_INVALID;
  906. client = synx_get_client(session);
  907. if (IS_ERR_OR_NULL(client))
  908. return -SYNX_INVALID;
  909. synx_data = synx_util_acquire_handle(client, params->h_synx);
  910. synx_obj = synx_util_obtain_object(synx_data);
  911. if (IS_ERR_OR_NULL(synx_obj)) {
  912. dprintk(SYNX_ERR,
  913. "[sess :%llu] invalid handle access %u\n",
  914. client->id, params->h_synx);
  915. rc = -SYNX_INVALID;
  916. goto fail;
  917. }
  918. mutex_lock(&synx_obj->obj_lock);
  919. if (synx_util_is_external_object(synx_obj)) {
  920. dprintk(SYNX_ERR,
  921. "cannot cancel wait on external fence\n");
  922. goto release;
  923. }
  924. payload.h_synx = params->h_synx;
  925. payload.cb_func = params->cb_func;
  926. payload.data = params->userdata;
  927. payload.cancel_cb_func = params->cancel_cb_func;
  928. status = synx_util_get_object_status(synx_obj);
  929. if (status != SYNX_STATE_ACTIVE) {
  930. dprintk(SYNX_ERR,
  931. "handle %u already signaled or timed out, cannot cancel\n",
  932. params->h_synx);
  933. rc = -SYNX_INVALID;
  934. goto release;
  935. }
  936. status = SYNX_CALLBACK_RESULT_CANCELED;
  937. /* remove all cb payloads mayching the deregister call */
  938. list_for_each_entry_safe(synx_cb, synx_cb_temp,
  939. &synx_obj->reg_cbs_list, node) {
  940. if (synx_cb->session != session) {
  941. continue;
  942. } else if (synx_cb->idx == 0 ||
  943. synx_cb->idx >= SYNX_MAX_OBJS) {
  944. /*
  945. * this should not happen. Even if it does,
  946. * the allocated memory will be cleaned up
  947. * when object is destroyed, preventing any
  948. * memory leaks.
  949. */
  950. dprintk(SYNX_ERR,
  951. "[sess :%llu] invalid callback data\n",
  952. client->id);
  953. continue;
  954. }
  955. cb_payload = &client->cb_table[synx_cb->idx];
  956. ret = synx_match_payload(&cb_payload->kernel_cb, &payload);
  957. if (synx_cb->timeout != SYNX_NO_TIMEOUT) {
  958. dprintk(SYNX_VERB,
  959. "Deleting timer synx_cb 0x%x, timeout 0x%llx\n",
  960. synx_cb, synx_cb->timeout);
  961. del_timer(&synx_cb->synx_timer);
  962. }
  963. switch (ret) {
  964. case 1:
  965. /* queue the cancel cb work */
  966. list_del_init(&synx_cb->node);
  967. synx_cb->status = status;
  968. queue_work(synx_dev->wq_cb,
  969. &synx_cb->cb_dispatch);
  970. match_found = true;
  971. break;
  972. case 2:
  973. /* no cancellation cb */
  974. if (synx_util_clear_cb_entry(client, cb_payload))
  975. dprintk(SYNX_ERR,
  976. "[sess :%llu] error clearing cb %u\n",
  977. client->id, params->h_synx);
  978. list_del_init(&synx_cb->node);
  979. kfree(synx_cb);
  980. match_found = true;
  981. break;
  982. default:
  983. break;
  984. }
  985. }
  986. if (!match_found)
  987. rc = -SYNX_INVALID;
  988. release:
  989. mutex_unlock(&synx_obj->obj_lock);
  990. fail:
  991. synx_util_release_handle(synx_data);
  992. synx_put_client(client);
  993. return rc;
  994. }
  995. EXPORT_SYMBOL(synx_cancel_async_wait);
  996. int synx_merge(struct synx_session *session,
  997. struct synx_merge_params *params)
  998. {
  999. int rc = SYNX_SUCCESS, i, num_signaled = 0;
  1000. u32 count = 0, h_child, status = SYNX_STATE_ACTIVE;
  1001. u32 *h_child_list = NULL, *h_child_idx_list = NULL;
  1002. struct synx_client *client;
  1003. struct dma_fence **fences = NULL;
  1004. struct synx_map_entry *map_entry;
  1005. struct synx_coredata *synx_obj, *synx_obj_child;
  1006. struct synx_handle_coredata *synx_data_child;
  1007. if (IS_ERR_OR_NULL(session) || IS_ERR_OR_NULL(params))
  1008. return -SYNX_INVALID;
  1009. if (IS_ERR_OR_NULL(params->h_synxs) ||
  1010. IS_ERR_OR_NULL(params->h_merged_obj)) {
  1011. dprintk(SYNX_ERR, "invalid arguments\n");
  1012. return -SYNX_INVALID;
  1013. }
  1014. client = synx_get_client(session);
  1015. if (IS_ERR_OR_NULL(client))
  1016. return -SYNX_INVALID;
  1017. rc = synx_util_validate_merge(client, params->h_synxs,
  1018. params->num_objs, &fences, &count);
  1019. if (rc < 0) {
  1020. dprintk(SYNX_ERR,
  1021. "[sess :%llu] merge validation failed\n",
  1022. client->id);
  1023. rc = -SYNX_INVALID;
  1024. goto fail;
  1025. }
  1026. synx_obj = kzalloc(sizeof(*synx_obj), GFP_KERNEL);
  1027. if (IS_ERR_OR_NULL(synx_obj)) {
  1028. rc = -SYNX_NOMEM;
  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. goto clean_up;
  1044. }
  1045. rc = synx_util_add_callback(synx_obj, *params->h_merged_obj);
  1046. if (rc != SYNX_SUCCESS)
  1047. goto clear;
  1048. rc = synx_util_init_handle(client, synx_obj,
  1049. params->h_merged_obj, map_entry);
  1050. if (rc) {
  1051. dprintk(SYNX_ERR,
  1052. "[sess :%llu] unable to init merge handle %u\n",
  1053. client->id, *params->h_merged_obj);
  1054. dma_fence_put(synx_obj->fence);
  1055. goto clear;
  1056. }
  1057. h_child_list = kzalloc(count*4, GFP_KERNEL);
  1058. if (IS_ERR_OR_NULL(h_child_list)) {
  1059. rc = -SYNX_NOMEM;
  1060. goto clear;
  1061. }
  1062. h_child_idx_list = kzalloc(count*4, GFP_KERNEL);
  1063. if (IS_ERR_OR_NULL(h_child_idx_list)) {
  1064. kfree(h_child_list);
  1065. rc = -SYNX_NOMEM;
  1066. goto clear;
  1067. }
  1068. for (i = 0; i < count; i++) {
  1069. h_child = synx_util_get_fence_entry((u64)fences[i], 1);
  1070. if (!synx_util_is_global_handle(h_child))
  1071. continue;
  1072. h_child_list[num_signaled] = h_child;
  1073. h_child_idx_list[num_signaled++] = synx_util_global_idx(h_child);
  1074. }
  1075. if (params->flags & SYNX_MERGE_GLOBAL_FENCE) {
  1076. rc = synx_global_merge(h_child_idx_list, num_signaled,
  1077. synx_util_global_idx(*params->h_merged_obj));
  1078. if (rc != SYNX_SUCCESS) {
  1079. dprintk(SYNX_ERR, "global merge failed\n");
  1080. kfree(h_child_list);
  1081. kfree(h_child_idx_list);
  1082. goto clear;
  1083. }
  1084. }
  1085. else {
  1086. for(i = 0; i < num_signaled; i++) {
  1087. status = synx_global_test_status_set_wait(synx_util_global_idx(h_child_list[i]), SYNX_CORE_APSS);
  1088. if (status != SYNX_STATE_ACTIVE) {
  1089. synx_data_child = synx_util_acquire_handle(client, h_child_list[i]);
  1090. synx_obj_child = synx_util_obtain_object(synx_data_child);
  1091. if (IS_ERR_OR_NULL(synx_obj_child)) {
  1092. dprintk(SYNX_ERR,
  1093. "[sess :%llu] invalid child handle %u\n",
  1094. client->id, h_child_list[i]);
  1095. continue;
  1096. }
  1097. mutex_lock(&synx_obj_child->obj_lock);
  1098. if (synx_obj->status == SYNX_STATE_ACTIVE)
  1099. rc = synx_native_signal_fence(synx_obj_child, status);
  1100. mutex_unlock(&synx_obj_child->obj_lock);
  1101. if (rc != SYNX_SUCCESS)
  1102. dprintk(SYNX_ERR, "h_synx %u failed with status %d\n", h_child_list[i], rc);
  1103. synx_util_release_handle(synx_data_child);
  1104. }
  1105. }
  1106. }
  1107. dprintk(SYNX_MEM,
  1108. "[sess :%llu] merge allocated %u, core %pK, fence %pK\n",
  1109. client->id, *params->h_merged_obj, synx_obj,
  1110. synx_obj->fence);
  1111. kfree(h_child_list);
  1112. kfree(h_child_idx_list);
  1113. synx_put_client(client);
  1114. return SYNX_SUCCESS;
  1115. clear:
  1116. synx_util_release_map_entry(map_entry);
  1117. clean_up:
  1118. kfree(synx_obj);
  1119. fail:
  1120. synx_util_merge_error(client, params->h_synxs, count);
  1121. if (params->num_objs && params->num_objs <= count)
  1122. kfree(fences);
  1123. synx_put_client(client);
  1124. return rc;
  1125. }
  1126. EXPORT_SYMBOL(synx_merge);
  1127. int synx_native_release_core(struct synx_client *client,
  1128. u32 h_synx)
  1129. {
  1130. int rc = -SYNX_INVALID;
  1131. struct synx_handle_coredata *curr, *synx_handle = NULL;
  1132. spin_lock_bh(&client->handle_map_lock);
  1133. hash_for_each_possible(client->handle_map,
  1134. curr, node, h_synx) {
  1135. if (curr->key == h_synx &&
  1136. curr->rel_count != 0) {
  1137. curr->rel_count--;
  1138. synx_handle = curr;
  1139. rc = SYNX_SUCCESS;
  1140. break;
  1141. }
  1142. }
  1143. spin_unlock_bh(&client->handle_map_lock);
  1144. /* release the reference obtained at synx creation */
  1145. synx_util_release_handle(synx_handle);
  1146. return rc;
  1147. }
  1148. int synx_release(struct synx_session *session, u32 h_synx)
  1149. {
  1150. int rc = 0;
  1151. struct synx_client *client;
  1152. client = synx_get_client(session);
  1153. if (IS_ERR_OR_NULL(client))
  1154. return -SYNX_INVALID;
  1155. rc = synx_native_release_core(client, h_synx);
  1156. synx_put_client(client);
  1157. return rc;
  1158. }
  1159. EXPORT_SYMBOL(synx_release);
  1160. int synx_wait(struct synx_session *session,
  1161. u32 h_synx, u64 timeout_ms)
  1162. {
  1163. int rc = 0;
  1164. unsigned long timeleft;
  1165. struct synx_client *client;
  1166. struct synx_handle_coredata *synx_data;
  1167. struct synx_coredata *synx_obj;
  1168. client = synx_get_client(session);
  1169. if (IS_ERR_OR_NULL(client))
  1170. return -SYNX_INVALID;
  1171. synx_data = synx_util_acquire_handle(client, h_synx);
  1172. synx_obj = synx_util_obtain_object(synx_data);
  1173. if (IS_ERR_OR_NULL(synx_obj) || IS_ERR_OR_NULL(synx_obj->fence)) {
  1174. dprintk(SYNX_ERR,
  1175. "[sess :%llu] invalid handle access %u\n",
  1176. client->id, h_synx);
  1177. rc = -SYNX_INVALID;
  1178. goto fail;
  1179. }
  1180. if (synx_util_is_global_handle(h_synx)) {
  1181. rc = synx_global_test_status_set_wait(
  1182. synx_util_global_idx(h_synx), SYNX_CORE_APSS);
  1183. if (rc != SYNX_STATE_ACTIVE) {
  1184. mutex_lock(&synx_obj->obj_lock);
  1185. if (synx_util_is_merged_object(synx_obj))
  1186. synx_native_signal_merged_fence(synx_obj, rc);
  1187. else
  1188. synx_native_signal_fence(synx_obj, rc);
  1189. mutex_unlock(&synx_obj->obj_lock);
  1190. goto status;
  1191. }
  1192. }
  1193. timeleft = dma_fence_wait_timeout(synx_obj->fence, (bool) 0,
  1194. msecs_to_jiffies(timeout_ms));
  1195. if (timeleft <= 0) {
  1196. dprintk(SYNX_ERR,
  1197. "[sess :%llu] wait timeout for handle %u\n",
  1198. client->id, h_synx);
  1199. rc = -ETIMEDOUT;
  1200. goto fail;
  1201. }
  1202. status:
  1203. mutex_lock(&synx_obj->obj_lock);
  1204. rc = synx_util_get_object_status(synx_obj);
  1205. mutex_unlock(&synx_obj->obj_lock);
  1206. fail:
  1207. synx_util_release_handle(synx_data);
  1208. synx_put_client(client);
  1209. return rc;
  1210. }
  1211. EXPORT_SYMBOL(synx_wait);
  1212. int synx_bind(struct synx_session *session,
  1213. u32 h_synx,
  1214. struct synx_external_desc_v2 external_sync)
  1215. {
  1216. int rc = 0;
  1217. u32 i;
  1218. u32 bound_idx;
  1219. struct synx_client *client;
  1220. struct synx_handle_coredata *synx_data = NULL;
  1221. struct synx_coredata *synx_obj;
  1222. struct synx_signal_cb *data = NULL;
  1223. struct bind_operations *bind_ops = NULL;
  1224. client = synx_get_client(session);
  1225. if (IS_ERR_OR_NULL(client))
  1226. return -SYNX_INVALID;
  1227. synx_data = synx_util_acquire_handle(client, h_synx);
  1228. synx_obj = synx_util_obtain_object(synx_data);
  1229. if (IS_ERR_OR_NULL(synx_obj)) {
  1230. if (rc || synx_data)
  1231. dprintk(SYNX_ERR,
  1232. "[sess :%llu] invalid handle access %u\n",
  1233. client->id, h_synx);
  1234. goto fail;
  1235. }
  1236. bind_ops = synx_util_get_bind_ops(external_sync.type);
  1237. if (IS_ERR_OR_NULL(bind_ops)) {
  1238. dprintk(SYNX_ERR,
  1239. "[sess :%llu] invalid bind ops for %u\n",
  1240. client->id, external_sync.type);
  1241. rc = -SYNX_INVALID;
  1242. goto fail;
  1243. }
  1244. mutex_lock(&synx_obj->obj_lock);
  1245. if (synx_util_is_merged_object(synx_obj)) {
  1246. dprintk(SYNX_ERR,
  1247. "[sess :%llu] cannot bind to composite handle %u\n",
  1248. client->id, h_synx);
  1249. rc = -SYNX_INVALID;
  1250. goto release;
  1251. }
  1252. if (synx_obj->num_bound_synxs >= SYNX_MAX_NUM_BINDINGS) {
  1253. dprintk(SYNX_ERR,
  1254. "[sess :%llu] max bindings reached for handle %u\n",
  1255. client->id, h_synx);
  1256. rc = -SYNX_NOMEM;
  1257. goto release;
  1258. }
  1259. /* don't bind external sync obj if already done */
  1260. for (i = 0; i < synx_obj->num_bound_synxs; i++) {
  1261. if ((external_sync.id ==
  1262. synx_obj->bound_synxs[i].external_desc.id) &&
  1263. (external_sync.type ==
  1264. synx_obj->bound_synxs[i].external_desc.type)){
  1265. dprintk(SYNX_ERR,
  1266. "[sess :%llu] duplicate bind for sync %llu\n",
  1267. client->id, external_sync.id);
  1268. rc = -SYNX_ALREADY;
  1269. goto release;
  1270. }
  1271. }
  1272. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1273. if (IS_ERR_OR_NULL(data)) {
  1274. rc = -SYNX_NOMEM;
  1275. goto release;
  1276. }
  1277. /* get additional reference since passing pointer to cb */
  1278. synx_util_get_object(synx_obj);
  1279. /* data passed to external callback */
  1280. data->handle = h_synx;
  1281. data->synx_obj = synx_obj;
  1282. bound_idx = synx_obj->num_bound_synxs;
  1283. memcpy(&synx_obj->bound_synxs[bound_idx],
  1284. &external_sync, sizeof(struct synx_external_desc_v2));
  1285. synx_obj->bound_synxs[bound_idx].external_data = data;
  1286. synx_obj->num_bound_synxs++;
  1287. mutex_unlock(&synx_obj->obj_lock);
  1288. rc = bind_ops->register_callback(synx_external_callback,
  1289. data, external_sync.id);
  1290. if (rc) {
  1291. dprintk(SYNX_ERR,
  1292. "[sess :%llu] callback reg failed for %llu\n",
  1293. client->id, external_sync.id);
  1294. mutex_lock(&synx_obj->obj_lock);
  1295. memset(&synx_obj->bound_synxs[bound_idx], 0,
  1296. sizeof(struct synx_external_desc_v2));
  1297. synx_obj->num_bound_synxs--;
  1298. mutex_unlock(&synx_obj->obj_lock);
  1299. synx_util_put_object(synx_obj);
  1300. kfree(data);
  1301. goto fail;
  1302. }
  1303. synx_util_release_handle(synx_data);
  1304. dprintk(SYNX_DBG,
  1305. "[sess :%llu] ext sync %llu bound to handle %u\n",
  1306. client->id, external_sync.id, h_synx);
  1307. synx_put_client(client);
  1308. return SYNX_SUCCESS;
  1309. release:
  1310. mutex_unlock(&synx_obj->obj_lock);
  1311. fail:
  1312. synx_util_release_handle(synx_data);
  1313. synx_put_client(client);
  1314. return rc;
  1315. }
  1316. EXPORT_SYMBOL(synx_bind);
  1317. int synx_get_status(struct synx_session *session,
  1318. u32 h_synx)
  1319. {
  1320. int rc = 0, status = 0;
  1321. struct synx_client *client;
  1322. struct synx_handle_coredata *synx_data;
  1323. struct synx_coredata *synx_obj;
  1324. client = synx_get_client(session);
  1325. if (IS_ERR_OR_NULL(client))
  1326. return -SYNX_INVALID;
  1327. synx_data = synx_util_acquire_handle(client, h_synx);
  1328. synx_obj = synx_util_obtain_object(synx_data);
  1329. if (IS_ERR_OR_NULL(synx_obj) ||
  1330. IS_ERR_OR_NULL(synx_obj->fence)) {
  1331. dprintk(SYNX_ERR,
  1332. "[sess :%llu] invalid handle access %u\n",
  1333. client->id, h_synx);
  1334. rc = -SYNX_INVALID;
  1335. goto fail;
  1336. }
  1337. mutex_lock(&synx_obj->obj_lock);
  1338. status = synx_util_get_object_status(synx_obj);
  1339. rc = synx_obj->status;
  1340. mutex_unlock(&synx_obj->obj_lock);
  1341. dprintk(SYNX_VERB,
  1342. "[sess :%llu] handle %u synx coredata status %d and dma fence status %d\n",
  1343. client->id, h_synx, rc, status);
  1344. fail:
  1345. synx_util_release_handle(synx_data);
  1346. synx_put_client(client);
  1347. return rc;
  1348. }
  1349. EXPORT_SYMBOL(synx_get_status);
  1350. static struct synx_map_entry *synx_handle_conversion(
  1351. struct synx_client *client,
  1352. u32 *h_synx, struct synx_map_entry *old_entry)
  1353. {
  1354. int rc;
  1355. struct synx_map_entry *map_entry = NULL;
  1356. struct synx_coredata *synx_obj;
  1357. if (IS_ERR_OR_NULL(old_entry)) {
  1358. old_entry = synx_util_get_map_entry(*h_synx);
  1359. if (IS_ERR_OR_NULL(old_entry)) {
  1360. rc = PTR_ERR(old_entry);
  1361. dprintk(SYNX_ERR,
  1362. "invalid import handle %u err=%d",
  1363. *h_synx, rc);
  1364. return old_entry;
  1365. }
  1366. }
  1367. synx_obj = old_entry->synx_obj;
  1368. BUG_ON(synx_obj == NULL);
  1369. mutex_lock(&synx_obj->obj_lock);
  1370. synx_util_get_object(synx_obj);
  1371. if (synx_obj->global_idx != 0) {
  1372. *h_synx = synx_encode_handle(
  1373. synx_obj->global_idx, SYNX_CORE_APSS, true);
  1374. map_entry = synx_util_get_map_entry(*h_synx);
  1375. if (IS_ERR_OR_NULL(map_entry)) {
  1376. /* raced with release from last global client */
  1377. map_entry = synx_util_insert_to_map(synx_obj,
  1378. *h_synx, 0);
  1379. if (IS_ERR_OR_NULL(map_entry)) {
  1380. rc = PTR_ERR(map_entry);
  1381. dprintk(SYNX_ERR,
  1382. "addition of %u to map failed=%d",
  1383. *h_synx, rc);
  1384. }
  1385. }
  1386. } else {
  1387. synx_obj->map_count++;
  1388. rc = synx_alloc_global_handle(h_synx);
  1389. if (rc == SYNX_SUCCESS) {
  1390. synx_obj->global_idx =
  1391. synx_util_global_idx(*h_synx);
  1392. synx_obj->type |= SYNX_CREATE_GLOBAL_FENCE;
  1393. map_entry = synx_util_insert_to_map(synx_obj,
  1394. *h_synx, 0);
  1395. if (IS_ERR_OR_NULL(map_entry)) {
  1396. rc = PTR_ERR(map_entry);
  1397. synx_global_put_ref(
  1398. synx_util_global_idx(*h_synx));
  1399. dprintk(SYNX_ERR,
  1400. "insertion of %u to map failed=%d",
  1401. *h_synx, rc);
  1402. }
  1403. }
  1404. }
  1405. mutex_unlock(&synx_obj->obj_lock);
  1406. if (IS_ERR_OR_NULL(map_entry))
  1407. synx_util_put_object(synx_obj);
  1408. synx_util_release_map_entry(old_entry);
  1409. return map_entry;
  1410. }
  1411. static int synx_native_import_handle(struct synx_client *client,
  1412. struct synx_import_indv_params *params)
  1413. {
  1414. int rc = SYNX_SUCCESS;
  1415. u32 h_synx, core_id;
  1416. struct synx_map_entry *map_entry, *old_entry;
  1417. struct synx_coredata *synx_obj;
  1418. struct synx_handle_coredata *synx_data = NULL, *curr;
  1419. char name[SYNX_OBJ_NAME_LEN] = {0};
  1420. struct synx_create_params c_params = {0};
  1421. if (IS_ERR_OR_NULL(client) || IS_ERR_OR_NULL(params) ||
  1422. IS_ERR_OR_NULL(params->fence) ||
  1423. IS_ERR_OR_NULL(params->new_h_synx))
  1424. return -SYNX_INVALID;
  1425. h_synx = *((u32 *)params->fence);
  1426. /* check if already mapped to client */
  1427. spin_lock_bh(&client->handle_map_lock);
  1428. hash_for_each_possible(client->handle_map,
  1429. curr, node, h_synx) {
  1430. if (curr->key == h_synx &&
  1431. curr->rel_count != 0 &&
  1432. (synx_util_is_global_handle(h_synx) ||
  1433. params->flags & SYNX_IMPORT_LOCAL_FENCE)) {
  1434. curr->rel_count++;
  1435. kref_get(&curr->refcount);
  1436. synx_data = curr;
  1437. break;
  1438. }
  1439. }
  1440. spin_unlock_bh(&client->handle_map_lock);
  1441. if (synx_data) {
  1442. *params->new_h_synx = h_synx;
  1443. return SYNX_SUCCESS;
  1444. }
  1445. map_entry = synx_util_get_map_entry(h_synx);
  1446. if (IS_ERR_OR_NULL(map_entry)) {
  1447. core_id = (h_synx & SYNX_OBJ_CORE_ID_MASK)
  1448. >> SYNX_HANDLE_INDEX_BITS;
  1449. if (core_id == SYNX_CORE_APSS) {
  1450. dprintk(SYNX_ERR,
  1451. "[sess :%llu] invalid import handle %u\n",
  1452. client->id, h_synx);
  1453. return -SYNX_INVALID;
  1454. } else if (synx_util_is_global_handle(h_synx)) {
  1455. /* import global handle created in another core */
  1456. synx_util_map_import_params_to_create(params, &c_params);
  1457. scnprintf(name, SYNX_OBJ_NAME_LEN, "import-client-%d",
  1458. current->pid);
  1459. c_params.name = name;
  1460. c_params.h_synx = &h_synx;
  1461. rc = synx_native_create_core(client, &c_params);
  1462. if (rc != SYNX_SUCCESS)
  1463. return rc;
  1464. *params->new_h_synx = h_synx;
  1465. return SYNX_SUCCESS;
  1466. }
  1467. dprintk(SYNX_ERR,
  1468. "[sess :%llu] invalid handle %u\n",
  1469. client->id, h_synx);
  1470. return -SYNX_INVALID;
  1471. }
  1472. synx_obj = map_entry->synx_obj;
  1473. BUG_ON(synx_obj == NULL);
  1474. if ((params->flags & SYNX_IMPORT_GLOBAL_FENCE) &&
  1475. !synx_util_is_global_handle(h_synx)) {
  1476. old_entry = map_entry;
  1477. map_entry = synx_handle_conversion(client, &h_synx,
  1478. old_entry);
  1479. }
  1480. if (rc != SYNX_SUCCESS)
  1481. return rc;
  1482. *params->new_h_synx = h_synx;
  1483. rc = synx_util_init_handle(client, map_entry->synx_obj,
  1484. params->new_h_synx, map_entry);
  1485. if (rc != SYNX_SUCCESS) {
  1486. dprintk(SYNX_ERR,
  1487. "[sess :%llu] init of imported handle %u failed=%d\n",
  1488. client->id, h_synx, rc);
  1489. synx_util_release_map_entry(map_entry);
  1490. }
  1491. return rc;
  1492. }
  1493. static int synx_native_import_fence(struct synx_client *client,
  1494. struct synx_import_indv_params *params)
  1495. {
  1496. int rc = SYNX_SUCCESS;
  1497. u32 curr_h_synx;
  1498. u32 global;
  1499. struct synx_create_params c_params = {0};
  1500. char name[SYNX_OBJ_NAME_LEN] = {0};
  1501. struct synx_fence_entry *entry;
  1502. struct synx_map_entry *map_entry = NULL;
  1503. struct synx_handle_coredata *synx_data = NULL, *curr;
  1504. if (IS_ERR_OR_NULL(client) || IS_ERR_OR_NULL(params) ||
  1505. IS_ERR_OR_NULL(params->fence) ||
  1506. IS_ERR_OR_NULL(params->new_h_synx))
  1507. return -SYNX_INVALID;
  1508. global = SYNX_IMPORT_GLOBAL_FENCE & params->flags;
  1509. retry:
  1510. *params->new_h_synx =
  1511. synx_util_get_fence_entry((u64)params->fence, global);
  1512. if (*params->new_h_synx == 0) {
  1513. /* create a new synx obj and add to fence map */
  1514. synx_util_map_import_params_to_create(params, &c_params);
  1515. scnprintf(name, SYNX_OBJ_NAME_LEN, "import-client-%d",
  1516. current->pid);
  1517. c_params.name = name;
  1518. c_params.h_synx = params->new_h_synx;
  1519. c_params.fence = params->fence;
  1520. rc = synx_native_create_core(client, &c_params);
  1521. if (rc != SYNX_SUCCESS)
  1522. return rc;
  1523. curr_h_synx = *params->new_h_synx;
  1524. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  1525. if (IS_ERR_OR_NULL(entry)) {
  1526. rc = -SYNX_NOMEM;
  1527. curr_h_synx = *c_params.h_synx;
  1528. goto fail;
  1529. }
  1530. do {
  1531. entry->key = (u64)params->fence;
  1532. if (global)
  1533. entry->g_handle = *params->new_h_synx;
  1534. else
  1535. entry->l_handle = *params->new_h_synx;
  1536. rc = synx_util_insert_fence_entry(entry,
  1537. params->new_h_synx, global);
  1538. if (rc == SYNX_SUCCESS) {
  1539. dprintk(SYNX_DBG,
  1540. "mapped fence %pK to new handle %u\n",
  1541. params->fence, *params->new_h_synx);
  1542. break;
  1543. } else if (rc == -SYNX_ALREADY) {
  1544. /*
  1545. * release the new handle allocated
  1546. * and use the available handle
  1547. * already mapped instead.
  1548. */
  1549. map_entry = synx_util_get_map_entry(
  1550. *params->new_h_synx);
  1551. if (IS_ERR_OR_NULL(map_entry)) {
  1552. /* race with fence release, need to retry */
  1553. dprintk(SYNX_DBG,
  1554. "re-attempting handle import\n");
  1555. *params->new_h_synx = curr_h_synx;
  1556. continue;
  1557. }
  1558. rc = synx_util_init_handle(client,
  1559. map_entry->synx_obj,
  1560. params->new_h_synx, map_entry);
  1561. dprintk(SYNX_DBG, "mapped fence %pK to handle %u\n",
  1562. params->fence, *params->new_h_synx);
  1563. goto release;
  1564. } else {
  1565. dprintk(SYNX_ERR,
  1566. "importing fence %pK failed, err=%d\n",
  1567. params->fence, rc);
  1568. goto release;
  1569. }
  1570. } while (true);
  1571. } else {
  1572. /* check if already mapped to client */
  1573. spin_lock_bh(&client->handle_map_lock);
  1574. hash_for_each_possible(client->handle_map,
  1575. curr, node, *params->new_h_synx) {
  1576. if (curr->key == *params->new_h_synx &&
  1577. curr->rel_count != 0) {
  1578. curr->rel_count++;
  1579. kref_get(&curr->refcount);
  1580. synx_data = curr;
  1581. break;
  1582. }
  1583. }
  1584. spin_unlock_bh(&client->handle_map_lock);
  1585. if (synx_data) {
  1586. dprintk(SYNX_DBG, "mapped fence %pK to handle %u\n",
  1587. params->fence, *params->new_h_synx);
  1588. return SYNX_SUCCESS;
  1589. }
  1590. if (global && !synx_util_is_global_handle(
  1591. *params->new_h_synx))
  1592. map_entry = synx_handle_conversion(client,
  1593. params->new_h_synx, NULL);
  1594. else
  1595. map_entry = synx_util_get_map_entry(
  1596. *params->new_h_synx);
  1597. if (IS_ERR_OR_NULL(map_entry)) {
  1598. /* race with fence release, need to retry */
  1599. dprintk(SYNX_DBG, "re-attempting handle import\n");
  1600. goto retry;
  1601. }
  1602. rc = synx_util_init_handle(client, map_entry->synx_obj,
  1603. params->new_h_synx, map_entry);
  1604. dprintk(SYNX_DBG, "mapped fence %pK to existing handle %u\n",
  1605. params->fence, *params->new_h_synx);
  1606. }
  1607. return rc;
  1608. release:
  1609. kfree(entry);
  1610. fail:
  1611. synx_native_release_core(client, curr_h_synx);
  1612. return rc;
  1613. }
  1614. static int synx_native_import_indv(struct synx_client *client,
  1615. struct synx_import_indv_params *params)
  1616. {
  1617. int rc = -SYNX_INVALID;
  1618. if (IS_ERR_OR_NULL(params) ||
  1619. IS_ERR_OR_NULL(params->new_h_synx) ||
  1620. IS_ERR_OR_NULL(params->fence)) {
  1621. dprintk(SYNX_ERR, "invalid import arguments\n");
  1622. return -SYNX_INVALID;
  1623. }
  1624. if (likely(params->flags & SYNX_IMPORT_DMA_FENCE))
  1625. rc = synx_native_import_fence(client, params);
  1626. else if (params->flags & SYNX_IMPORT_SYNX_FENCE)
  1627. rc = synx_native_import_handle(client, params);
  1628. dprintk(SYNX_DBG,
  1629. "[sess :%llu] import of fence %pK %s, handle %u\n",
  1630. client->id, params->fence,
  1631. rc ? "failed" : "successful",
  1632. rc ? 0 : *params->new_h_synx);
  1633. return rc;
  1634. }
  1635. static int synx_native_import_arr(struct synx_client *client,
  1636. struct synx_import_arr_params *params)
  1637. {
  1638. u32 i;
  1639. int rc = SYNX_SUCCESS;
  1640. if (IS_ERR_OR_NULL(params) || params->num_fences == 0) {
  1641. dprintk(SYNX_ERR, "invalid import arr arguments\n");
  1642. return -SYNX_INVALID;
  1643. }
  1644. for (i = 0; i < params->num_fences; i++) {
  1645. rc = synx_native_import_indv(client, &params->list[i]);
  1646. if (rc != SYNX_SUCCESS) {
  1647. dprintk(SYNX_ERR,
  1648. "importing fence[%u] %pK failed=%d\n",
  1649. i, params->list[i].fence, rc);
  1650. break;
  1651. }
  1652. }
  1653. if (rc != SYNX_SUCCESS)
  1654. while (i--) {
  1655. /* release the imported handles and cleanup */
  1656. if (synx_native_release_core(client,
  1657. *params->list[i].new_h_synx) != SYNX_SUCCESS)
  1658. dprintk(SYNX_ERR,
  1659. "error cleaning up imported handle[%u] %u\n",
  1660. i, *params->list[i].new_h_synx);
  1661. }
  1662. return rc;
  1663. }
  1664. int synx_import(struct synx_session *session,
  1665. struct synx_import_params *params)
  1666. {
  1667. int rc = 0;
  1668. struct synx_client *client;
  1669. if (IS_ERR_OR_NULL(params)) {
  1670. dprintk(SYNX_ERR, "invalid import arguments\n");
  1671. return -SYNX_INVALID;
  1672. }
  1673. client = synx_get_client(session);
  1674. if (IS_ERR_OR_NULL(client))
  1675. return -SYNX_INVALID;
  1676. /* import fence based on its type */
  1677. if (params->type == SYNX_IMPORT_ARR_PARAMS)
  1678. rc = synx_native_import_arr(client, &params->arr);
  1679. else
  1680. rc = synx_native_import_indv(client, &params->indv);
  1681. synx_put_client(client);
  1682. return rc;
  1683. }
  1684. EXPORT_SYMBOL(synx_import);
  1685. static int synx_handle_create(struct synx_private_ioctl_arg *k_ioctl,
  1686. struct synx_session *session)
  1687. {
  1688. int result;
  1689. int csl_fence;
  1690. struct synx_create_v2 create_info;
  1691. struct synx_create_params params = {0};
  1692. if (k_ioctl->size != sizeof(create_info))
  1693. return -SYNX_INVALID;
  1694. if (copy_from_user(&create_info,
  1695. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1696. k_ioctl->size))
  1697. return -EFAULT;
  1698. params.h_synx = &create_info.synx_obj;
  1699. params.name = create_info.name;
  1700. params.flags = create_info.flags;
  1701. if (create_info.flags & SYNX_CREATE_CSL_FENCE) {
  1702. csl_fence = create_info.desc.id[0];
  1703. params.fence = &csl_fence;
  1704. }
  1705. result = synx_create(session, &params);
  1706. if (!result)
  1707. if (copy_to_user(u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1708. &create_info,
  1709. k_ioctl->size))
  1710. return -EFAULT;
  1711. return result;
  1712. }
  1713. static int synx_handle_getstatus(struct synx_private_ioctl_arg *k_ioctl,
  1714. struct synx_session *session)
  1715. {
  1716. struct synx_signal_v2 signal_info;
  1717. if (k_ioctl->size != sizeof(signal_info))
  1718. return -SYNX_INVALID;
  1719. if (copy_from_user(&signal_info,
  1720. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1721. k_ioctl->size))
  1722. return -EFAULT;
  1723. signal_info.synx_state =
  1724. synx_get_status(session, signal_info.synx_obj);
  1725. if (copy_to_user(u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1726. &signal_info,
  1727. k_ioctl->size))
  1728. return -EFAULT;
  1729. return SYNX_SUCCESS;
  1730. }
  1731. static int synx_handle_import(struct synx_private_ioctl_arg *k_ioctl,
  1732. struct synx_session *session)
  1733. {
  1734. struct synx_import_info import_info;
  1735. struct synx_import_params params = {0};
  1736. if (k_ioctl->size != sizeof(import_info))
  1737. return -SYNX_INVALID;
  1738. if (copy_from_user(&import_info,
  1739. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1740. k_ioctl->size))
  1741. return -EFAULT;
  1742. if (import_info.flags & SYNX_IMPORT_SYNX_FENCE)
  1743. params.indv.fence = &import_info.synx_obj;
  1744. else if (import_info.flags & SYNX_IMPORT_DMA_FENCE)
  1745. params.indv.fence =
  1746. sync_file_get_fence(import_info.desc.id[0]);
  1747. params.type = SYNX_IMPORT_INDV_PARAMS;
  1748. params.indv.flags = import_info.flags;
  1749. params.indv.new_h_synx = &import_info.new_synx_obj;
  1750. if (synx_import(session, &params))
  1751. return -SYNX_INVALID;
  1752. if (import_info.flags & SYNX_IMPORT_DMA_FENCE)
  1753. dma_fence_put(params.indv.fence);
  1754. if (copy_to_user(u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1755. &import_info,
  1756. k_ioctl->size))
  1757. return -EFAULT;
  1758. return SYNX_SUCCESS;
  1759. }
  1760. static int synx_handle_import_arr(
  1761. struct synx_private_ioctl_arg *k_ioctl,
  1762. struct synx_session *session)
  1763. {
  1764. int rc = -SYNX_INVALID;
  1765. u32 idx = 0;
  1766. struct synx_client *client;
  1767. struct synx_import_arr_info arr_info;
  1768. struct synx_import_info *arr;
  1769. struct synx_import_indv_params params = {0};
  1770. if (k_ioctl->size != sizeof(arr_info))
  1771. return -SYNX_INVALID;
  1772. if (copy_from_user(&arr_info,
  1773. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1774. k_ioctl->size))
  1775. return -EFAULT;
  1776. arr = kcalloc(arr_info.num_objs,
  1777. sizeof(*arr), GFP_KERNEL);
  1778. if (IS_ERR_OR_NULL(arr))
  1779. return -ENOMEM;
  1780. client = synx_get_client(session);
  1781. if (IS_ERR_OR_NULL(client)) {
  1782. rc = PTR_ERR(client);
  1783. goto clean;
  1784. }
  1785. if (copy_from_user(arr,
  1786. u64_to_user_ptr(arr_info.list),
  1787. sizeof(*arr) * arr_info.num_objs)) {
  1788. rc = -EFAULT;
  1789. goto fail;
  1790. }
  1791. while (idx < arr_info.num_objs) {
  1792. params.new_h_synx = &arr[idx].new_synx_obj;
  1793. params.flags = arr[idx].flags;
  1794. if (arr[idx].flags & SYNX_IMPORT_SYNX_FENCE)
  1795. params.fence = &arr[idx].synx_obj;
  1796. if (arr[idx].flags & SYNX_IMPORT_DMA_FENCE)
  1797. params.fence =
  1798. sync_file_get_fence(arr[idx].desc.id[0]);
  1799. rc = synx_native_import_indv(client, &params);
  1800. if (rc != SYNX_SUCCESS)
  1801. break;
  1802. idx++;
  1803. }
  1804. /* release allocated handles in case of failure */
  1805. if (rc != SYNX_SUCCESS) {
  1806. while (idx > 0)
  1807. synx_native_release_core(client,
  1808. arr[--idx].new_synx_obj);
  1809. } else {
  1810. if (copy_to_user(u64_to_user_ptr(arr_info.list),
  1811. arr,
  1812. sizeof(*arr) * arr_info.num_objs)) {
  1813. rc = -EFAULT;
  1814. goto fail;
  1815. }
  1816. }
  1817. fail:
  1818. synx_put_client(client);
  1819. clean:
  1820. kfree(arr);
  1821. return rc;
  1822. }
  1823. static int synx_handle_export(struct synx_private_ioctl_arg *k_ioctl,
  1824. struct synx_session *session)
  1825. {
  1826. return -SYNX_INVALID;
  1827. }
  1828. static int synx_handle_signal(struct synx_private_ioctl_arg *k_ioctl,
  1829. struct synx_session *session)
  1830. {
  1831. struct synx_signal_v2 signal_info;
  1832. if (k_ioctl->size != sizeof(signal_info))
  1833. return -SYNX_INVALID;
  1834. if (copy_from_user(&signal_info,
  1835. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1836. k_ioctl->size))
  1837. return -EFAULT;
  1838. return synx_signal(session, signal_info.synx_obj,
  1839. signal_info.synx_state);
  1840. }
  1841. static int synx_handle_merge(struct synx_private_ioctl_arg *k_ioctl,
  1842. struct synx_session *session)
  1843. {
  1844. u32 *h_synxs;
  1845. int result;
  1846. struct synx_merge_v2 merge_info;
  1847. struct synx_merge_params params = {0};
  1848. if (k_ioctl->size != sizeof(merge_info))
  1849. return -SYNX_INVALID;
  1850. if (copy_from_user(&merge_info,
  1851. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1852. k_ioctl->size))
  1853. return -EFAULT;
  1854. if (merge_info.num_objs >= SYNX_MAX_OBJS)
  1855. return -SYNX_INVALID;
  1856. h_synxs = kcalloc(merge_info.num_objs,
  1857. sizeof(*h_synxs), GFP_KERNEL);
  1858. if (IS_ERR_OR_NULL(h_synxs))
  1859. return -ENOMEM;
  1860. if (copy_from_user(h_synxs,
  1861. u64_to_user_ptr(merge_info.synx_objs),
  1862. sizeof(u32) * merge_info.num_objs)) {
  1863. kfree(h_synxs);
  1864. return -EFAULT;
  1865. }
  1866. params.num_objs = merge_info.num_objs;
  1867. params.h_synxs = h_synxs;
  1868. params.flags = merge_info.flags;
  1869. params.h_merged_obj = &merge_info.merged;
  1870. result = synx_merge(session, &params);
  1871. if (!result)
  1872. if (copy_to_user(u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1873. &merge_info,
  1874. k_ioctl->size)) {
  1875. kfree(h_synxs);
  1876. return -EFAULT;
  1877. }
  1878. kfree(h_synxs);
  1879. return result;
  1880. }
  1881. static int synx_handle_wait(struct synx_private_ioctl_arg *k_ioctl,
  1882. struct synx_session *session)
  1883. {
  1884. struct synx_wait_v2 wait_info;
  1885. if (k_ioctl->size != sizeof(wait_info))
  1886. return -SYNX_INVALID;
  1887. if (copy_from_user(&wait_info,
  1888. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1889. k_ioctl->size))
  1890. return -EFAULT;
  1891. k_ioctl->result = synx_wait(session,
  1892. wait_info.synx_obj, wait_info.timeout_ms);
  1893. return SYNX_SUCCESS;
  1894. }
  1895. static int synx_handle_async_wait(
  1896. struct synx_private_ioctl_arg *k_ioctl,
  1897. struct synx_session *session)
  1898. {
  1899. int rc = 0;
  1900. struct synx_userpayload_info_v2 user_data;
  1901. struct synx_callback_params params = {0};
  1902. if (k_ioctl->size != sizeof(user_data))
  1903. return -SYNX_INVALID;
  1904. if (copy_from_user(&user_data,
  1905. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1906. k_ioctl->size))
  1907. return -EFAULT;
  1908. params.h_synx = user_data.synx_obj;
  1909. params.cb_func = synx_util_default_user_callback;
  1910. params.userdata = (void *)user_data.payload[0];
  1911. params.timeout_ms = user_data.payload[2];
  1912. rc = synx_async_wait(session, &params);
  1913. if (rc)
  1914. dprintk(SYNX_ERR,
  1915. "user cb registration failed for handle %d\n",
  1916. user_data.synx_obj);
  1917. return rc;
  1918. }
  1919. static int synx_handle_cancel_async_wait(
  1920. struct synx_private_ioctl_arg *k_ioctl,
  1921. struct synx_session *session)
  1922. {
  1923. int rc = 0;
  1924. struct synx_userpayload_info_v2 user_data;
  1925. struct synx_callback_params params = {0};
  1926. if (k_ioctl->size != sizeof(user_data))
  1927. return -SYNX_INVALID;
  1928. if (copy_from_user(&user_data,
  1929. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1930. k_ioctl->size))
  1931. return -EFAULT;
  1932. params.h_synx = user_data.synx_obj;
  1933. params.cb_func = synx_util_default_user_callback;
  1934. params.userdata = (void *)user_data.payload[0];
  1935. rc = synx_cancel_async_wait(session, &params);
  1936. if (rc)
  1937. dprintk(SYNX_ERR,
  1938. "user cb deregistration failed for handle %d\n",
  1939. user_data.synx_obj);
  1940. return rc;
  1941. }
  1942. static int synx_handle_bind(struct synx_private_ioctl_arg *k_ioctl,
  1943. struct synx_session *session)
  1944. {
  1945. struct synx_bind_v2 synx_bind_info;
  1946. if (k_ioctl->size != sizeof(synx_bind_info))
  1947. return -SYNX_INVALID;
  1948. if (copy_from_user(&synx_bind_info,
  1949. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1950. k_ioctl->size))
  1951. return -EFAULT;
  1952. k_ioctl->result = synx_bind(session,
  1953. synx_bind_info.synx_obj,
  1954. synx_bind_info.ext_sync_desc);
  1955. return k_ioctl->result;
  1956. }
  1957. static int synx_handle_release(struct synx_private_ioctl_arg *k_ioctl,
  1958. struct synx_session *session)
  1959. {
  1960. struct synx_info release_info;
  1961. if (k_ioctl->size != sizeof(release_info))
  1962. return -SYNX_INVALID;
  1963. if (copy_from_user(&release_info,
  1964. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1965. k_ioctl->size))
  1966. return -EFAULT;
  1967. return synx_release(session, release_info.synx_obj);
  1968. }
  1969. static int synx_handle_get_fence(struct synx_private_ioctl_arg *k_ioctl,
  1970. struct synx_session *session)
  1971. {
  1972. struct synx_fence_fd fence_fd;
  1973. struct dma_fence *fence;
  1974. if (k_ioctl->size != sizeof(fence_fd))
  1975. return -SYNX_INVALID;
  1976. if (copy_from_user(&fence_fd,
  1977. u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1978. k_ioctl->size))
  1979. return -EFAULT;
  1980. fence = synx_get_fence(session, fence_fd.synx_obj);
  1981. fence_fd.fd = synx_create_sync_fd(fence);
  1982. /*
  1983. * release additional reference taken in synx_get_fence.
  1984. * additional reference ensures the fence is valid and
  1985. * does not race with handle/fence release.
  1986. */
  1987. dma_fence_put(fence);
  1988. if (copy_to_user(u64_to_user_ptr(k_ioctl->ioctl_ptr),
  1989. &fence_fd, k_ioctl->size))
  1990. return -EFAULT;
  1991. return SYNX_SUCCESS;
  1992. }
  1993. static long synx_ioctl(struct file *filep,
  1994. unsigned int cmd,
  1995. unsigned long arg)
  1996. {
  1997. s32 rc = 0;
  1998. struct synx_private_ioctl_arg k_ioctl;
  1999. struct synx_session *session = filep->private_data;
  2000. if (cmd != SYNX_PRIVATE_IOCTL_CMD) {
  2001. dprintk(SYNX_ERR, "invalid ioctl cmd\n");
  2002. return -ENOIOCTLCMD;
  2003. }
  2004. if (copy_from_user(&k_ioctl,
  2005. (struct synx_private_ioctl_arg *)arg,
  2006. sizeof(k_ioctl))) {
  2007. dprintk(SYNX_ERR, "invalid ioctl args\n");
  2008. return -EFAULT;
  2009. }
  2010. if (!k_ioctl.ioctl_ptr)
  2011. return -SYNX_INVALID;
  2012. dprintk(SYNX_VERB, "[sess :%llu] Enter cmd %u from pid %d\n",
  2013. ((struct synx_client *)session)->id,
  2014. k_ioctl.id, current->pid);
  2015. switch (k_ioctl.id) {
  2016. case SYNX_CREATE:
  2017. rc = synx_handle_create(&k_ioctl, session);
  2018. break;
  2019. case SYNX_RELEASE:
  2020. rc = synx_handle_release(&k_ioctl, session);
  2021. break;
  2022. case SYNX_REGISTER_PAYLOAD:
  2023. rc = synx_handle_async_wait(&k_ioctl,
  2024. session);
  2025. break;
  2026. case SYNX_DEREGISTER_PAYLOAD:
  2027. rc = synx_handle_cancel_async_wait(&k_ioctl,
  2028. session);
  2029. break;
  2030. case SYNX_SIGNAL:
  2031. rc = synx_handle_signal(&k_ioctl, session);
  2032. break;
  2033. case SYNX_MERGE:
  2034. rc = synx_handle_merge(&k_ioctl, session);
  2035. break;
  2036. case SYNX_WAIT:
  2037. rc = synx_handle_wait(&k_ioctl, session);
  2038. if (copy_to_user((void *)arg,
  2039. &k_ioctl,
  2040. sizeof(k_ioctl))) {
  2041. dprintk(SYNX_ERR, "invalid ioctl args\n");
  2042. rc = -EFAULT;
  2043. }
  2044. break;
  2045. case SYNX_BIND:
  2046. rc = synx_handle_bind(&k_ioctl, session);
  2047. break;
  2048. case SYNX_GETSTATUS:
  2049. rc = synx_handle_getstatus(&k_ioctl, session);
  2050. break;
  2051. case SYNX_IMPORT:
  2052. rc = synx_handle_import(&k_ioctl, session);
  2053. break;
  2054. case SYNX_IMPORT_ARR:
  2055. rc = synx_handle_import_arr(&k_ioctl, session);
  2056. break;
  2057. case SYNX_EXPORT:
  2058. rc = synx_handle_export(&k_ioctl, session);
  2059. break;
  2060. case SYNX_GETFENCE_FD:
  2061. rc = synx_handle_get_fence(&k_ioctl, session);
  2062. break;
  2063. default:
  2064. rc = -SYNX_INVALID;
  2065. }
  2066. dprintk(SYNX_VERB, "[sess :%llu] exit with status %d\n",
  2067. ((struct synx_client *)session)->id, rc);
  2068. return rc;
  2069. }
  2070. static ssize_t synx_read(struct file *filep,
  2071. char __user *buf, size_t size, loff_t *f_pos)
  2072. {
  2073. ssize_t rc = 0;
  2074. struct synx_client *client = NULL;
  2075. struct synx_client_cb *cb;
  2076. struct synx_session *session = filep->private_data;
  2077. struct synx_userpayload_info_v2 data;
  2078. if (size != sizeof(struct synx_userpayload_info_v2)) {
  2079. dprintk(SYNX_ERR, "invalid read size\n");
  2080. return -SYNX_INVALID;
  2081. }
  2082. client = synx_get_client(session);
  2083. if (IS_ERR_OR_NULL(client))
  2084. return -SYNX_INVALID;
  2085. mutex_lock(&client->event_q_lock);
  2086. cb = list_first_entry_or_null(&client->event_q,
  2087. struct synx_client_cb, node);
  2088. if (IS_ERR_OR_NULL(cb)) {
  2089. mutex_unlock(&client->event_q_lock);
  2090. rc = 0;
  2091. goto fail;
  2092. }
  2093. if (cb->idx == 0 || cb->idx >= SYNX_MAX_OBJS) {
  2094. dprintk(SYNX_ERR, "invalid index\n");
  2095. mutex_unlock(&client->event_q_lock);
  2096. rc = -SYNX_INVALID;
  2097. goto fail;
  2098. }
  2099. list_del_init(&cb->node);
  2100. mutex_unlock(&client->event_q_lock);
  2101. rc = size;
  2102. data.synx_obj = cb->kernel_cb.h_synx;
  2103. data.reserved = cb->kernel_cb.status;
  2104. data.payload[0] = (u64)cb->kernel_cb.data;
  2105. if (copy_to_user(buf,
  2106. &data,
  2107. sizeof(struct synx_userpayload_info_v2))) {
  2108. dprintk(SYNX_ERR, "couldn't copy user callback data\n");
  2109. rc = -EFAULT;
  2110. }
  2111. if (synx_util_clear_cb_entry(client, cb))
  2112. dprintk(SYNX_ERR,
  2113. "[sess :%llu] error clearing cb for handle %u\n",
  2114. client->id, data.synx_obj);
  2115. fail:
  2116. synx_put_client(client);
  2117. return rc;
  2118. }
  2119. static unsigned int synx_poll(struct file *filep,
  2120. struct poll_table_struct *poll_table)
  2121. {
  2122. int rc = 0;
  2123. struct synx_client *client;
  2124. struct synx_session *session = filep->private_data;
  2125. client = synx_get_client(session);
  2126. if (IS_ERR_OR_NULL(client)) {
  2127. dprintk(SYNX_ERR, "invalid session in poll\n");
  2128. return SYNX_SUCCESS;
  2129. }
  2130. poll_wait(filep, &client->event_wq, poll_table);
  2131. mutex_lock(&client->event_q_lock);
  2132. if (!list_empty(&client->event_q))
  2133. rc = POLLPRI;
  2134. mutex_unlock(&client->event_q_lock);
  2135. synx_put_client(client);
  2136. return rc;
  2137. }
  2138. struct synx_session *synx_initialize(
  2139. struct synx_initialization_params *params)
  2140. {
  2141. struct synx_client *client;
  2142. if (IS_ERR_OR_NULL(params))
  2143. return ERR_PTR(-SYNX_INVALID);
  2144. client = vzalloc(sizeof(*client));
  2145. if (IS_ERR_OR_NULL(client))
  2146. return ERR_PTR(-SYNX_NOMEM);
  2147. if (params->name)
  2148. strlcpy(client->name, params->name, sizeof(client->name));
  2149. client->active = true;
  2150. client->dma_context = dma_fence_context_alloc(1);
  2151. client->id = atomic64_inc_return(&synx_counter);
  2152. kref_init(&client->refcount);
  2153. spin_lock_init(&client->handle_map_lock);
  2154. mutex_init(&client->event_q_lock);
  2155. INIT_LIST_HEAD(&client->event_q);
  2156. init_waitqueue_head(&client->event_wq);
  2157. /* zero idx not allowed */
  2158. set_bit(0, client->cb_bitmap);
  2159. spin_lock_bh(&synx_dev->native->metadata_map_lock);
  2160. hash_add(synx_dev->native->client_metadata_map,
  2161. &client->node, (u64)client);
  2162. spin_unlock_bh(&synx_dev->native->metadata_map_lock);
  2163. dprintk(SYNX_INFO, "[sess :%llu] session created %s\n",
  2164. client->id, params->name);
  2165. return (struct synx_session *)client;
  2166. }
  2167. EXPORT_SYMBOL(synx_initialize);
  2168. int synx_uninitialize(struct synx_session *session)
  2169. {
  2170. struct synx_client *client = NULL, *curr;
  2171. spin_lock_bh(&synx_dev->native->metadata_map_lock);
  2172. hash_for_each_possible(synx_dev->native->client_metadata_map,
  2173. curr, node, (u64)session) {
  2174. if (curr == (struct synx_client *)session) {
  2175. if (curr->active) {
  2176. curr->active = false;
  2177. client = curr;
  2178. }
  2179. break;
  2180. }
  2181. }
  2182. spin_unlock_bh(&synx_dev->native->metadata_map_lock);
  2183. /* release the reference obtained at synx init */
  2184. synx_put_client(client);
  2185. return SYNX_SUCCESS;
  2186. }
  2187. EXPORT_SYMBOL(synx_uninitialize);
  2188. static int synx_open(struct inode *inode, struct file *filep)
  2189. {
  2190. int rc = 0;
  2191. char name[SYNX_OBJ_NAME_LEN];
  2192. struct synx_initialization_params params = {0};
  2193. dprintk(SYNX_VERB, "Enter pid: %d\n", current->pid);
  2194. scnprintf(name, SYNX_OBJ_NAME_LEN, "umd-client-%d", current->pid);
  2195. params.name = name;
  2196. params.id = SYNX_CLIENT_NATIVE;
  2197. filep->private_data = synx_initialize(&params);
  2198. if (IS_ERR_OR_NULL(filep->private_data)) {
  2199. dprintk(SYNX_ERR, "session allocation failed for pid: %d\n",
  2200. current->pid);
  2201. rc = PTR_ERR(filep->private_data);
  2202. } else {
  2203. dprintk(SYNX_VERB, "allocated new session for pid: %d\n",
  2204. current->pid);
  2205. }
  2206. return rc;
  2207. }
  2208. static int synx_close(struct inode *inode, struct file *filep)
  2209. {
  2210. struct synx_session *session = filep->private_data;
  2211. return synx_uninitialize(session);
  2212. }
  2213. static const struct file_operations synx_fops = {
  2214. .owner = THIS_MODULE,
  2215. .open = synx_open,
  2216. .read = synx_read,
  2217. .release = synx_close,
  2218. .poll = synx_poll,
  2219. .unlocked_ioctl = synx_ioctl,
  2220. #ifdef CONFIG_COMPAT
  2221. .compat_ioctl = synx_ioctl,
  2222. #endif
  2223. };
  2224. int synx_register_ops(
  2225. const struct synx_register_params *params)
  2226. {
  2227. s32 rc = 0;
  2228. struct synx_registered_ops *client_ops;
  2229. if (!synx_dev || !params || !params->name ||
  2230. !synx_util_is_valid_bind_type(params->type) ||
  2231. !params->ops.register_callback ||
  2232. !params->ops.deregister_callback ||
  2233. !params->ops.signal) {
  2234. dprintk(SYNX_ERR, "invalid register params\n");
  2235. return -SYNX_INVALID;
  2236. }
  2237. mutex_lock(&synx_dev->vtbl_lock);
  2238. client_ops = &synx_dev->bind_vtbl[params->type];
  2239. if (!client_ops->valid) {
  2240. client_ops->valid = true;
  2241. memcpy(&client_ops->ops, &params->ops,
  2242. sizeof(client_ops->ops));
  2243. strlcpy(client_ops->name, params->name,
  2244. sizeof(client_ops->name));
  2245. client_ops->type = params->type;
  2246. dprintk(SYNX_INFO,
  2247. "registered bind ops type %u for %s\n",
  2248. params->type, params->name);
  2249. } else {
  2250. dprintk(SYNX_WARN,
  2251. "client already registered for type %u by %s\n",
  2252. client_ops->type, client_ops->name);
  2253. rc = -SYNX_ALREADY;
  2254. }
  2255. mutex_unlock(&synx_dev->vtbl_lock);
  2256. return rc;
  2257. }
  2258. EXPORT_SYMBOL(synx_register_ops);
  2259. int synx_deregister_ops(
  2260. const struct synx_register_params *params)
  2261. {
  2262. struct synx_registered_ops *client_ops;
  2263. if (IS_ERR_OR_NULL(params) || params->name ||
  2264. !synx_util_is_valid_bind_type(params->type)) {
  2265. dprintk(SYNX_ERR, "invalid params\n");
  2266. return -SYNX_INVALID;
  2267. }
  2268. mutex_lock(&synx_dev->vtbl_lock);
  2269. client_ops = &synx_dev->bind_vtbl[params->type];
  2270. memset(client_ops, 0, sizeof(*client_ops));
  2271. dprintk(SYNX_INFO, "deregistered bind ops for %s\n",
  2272. params->name);
  2273. mutex_unlock(&synx_dev->vtbl_lock);
  2274. return SYNX_SUCCESS;
  2275. }
  2276. EXPORT_SYMBOL(synx_deregister_ops);
  2277. void synx_ipc_handler(struct work_struct *cb_dispatch)
  2278. {
  2279. struct synx_signal_cb *signal_cb =
  2280. container_of(cb_dispatch, struct synx_signal_cb, cb_dispatch);
  2281. struct synx_map_entry *map_entry;
  2282. map_entry = synx_util_get_map_entry(signal_cb->handle);
  2283. if (IS_ERR_OR_NULL(map_entry)) {
  2284. dprintk(SYNX_WARN,
  2285. "no clients to notify for %u\n",
  2286. signal_cb->handle);
  2287. dprintk(SYNX_MEM, "signal cb destroyed %pK\n", signal_cb);
  2288. kfree(signal_cb);
  2289. return;
  2290. }
  2291. /* get reference on synx coredata for signal cb */
  2292. synx_util_get_object(map_entry->synx_obj);
  2293. signal_cb->synx_obj = map_entry->synx_obj;
  2294. synx_util_release_map_entry(map_entry);
  2295. synx_signal_handler(&signal_cb->cb_dispatch);
  2296. }
  2297. int synx_ipc_callback(u32 client_id,
  2298. s64 data, void *priv)
  2299. {
  2300. struct synx_signal_cb *signal_cb;
  2301. u32 status = (u32)data;
  2302. u32 handle = (u32)(data >> 32);
  2303. signal_cb = kzalloc(sizeof(*signal_cb), GFP_ATOMIC);
  2304. if (IS_ERR_OR_NULL(signal_cb))
  2305. return -SYNX_NOMEM;
  2306. dprintk(SYNX_DBG,
  2307. "signal notification for %u received with status %u\n",
  2308. handle, status);
  2309. signal_cb->status = status;
  2310. signal_cb->handle = handle;
  2311. signal_cb->flag = SYNX_SIGNAL_FROM_IPC;
  2312. INIT_WORK(&signal_cb->cb_dispatch, synx_ipc_handler);
  2313. queue_work(synx_dev->wq_cb, &signal_cb->cb_dispatch);
  2314. return SYNX_SUCCESS;
  2315. }
  2316. EXPORT_SYMBOL(synx_ipc_callback);
  2317. int synx_recover(enum synx_client_id id)
  2318. {
  2319. u32 core_id;
  2320. core_id = synx_util_map_client_id_to_core(id);
  2321. if (core_id >= SYNX_CORE_MAX) {
  2322. dprintk(SYNX_ERR, "invalid client id %u\n", id);
  2323. return -SYNX_INVALID;
  2324. }
  2325. switch (core_id) {
  2326. case SYNX_CORE_EVA:
  2327. case SYNX_CORE_IRIS:
  2328. case SYNX_CORE_ICP:
  2329. break;
  2330. default:
  2331. dprintk(SYNX_ERR, "recovery not supported on %u\n", id);
  2332. return -SYNX_NOSUPPORT;
  2333. }
  2334. return synx_global_recover(core_id);
  2335. }
  2336. EXPORT_SYMBOL(synx_recover);
  2337. static int synx_local_mem_init(void)
  2338. {
  2339. if (!synx_dev->native)
  2340. return -SYNX_INVALID;
  2341. hash_init(synx_dev->native->client_metadata_map);
  2342. hash_init(synx_dev->native->fence_map);
  2343. hash_init(synx_dev->native->global_map);
  2344. hash_init(synx_dev->native->local_map);
  2345. hash_init(synx_dev->native->csl_fence_map);
  2346. spin_lock_init(&synx_dev->native->metadata_map_lock);
  2347. spin_lock_init(&synx_dev->native->fence_map_lock);
  2348. spin_lock_init(&synx_dev->native->global_map_lock);
  2349. spin_lock_init(&synx_dev->native->local_map_lock);
  2350. spin_lock_init(&synx_dev->native->csl_map_lock);
  2351. /* zero idx not allowed */
  2352. set_bit(0, synx_dev->native->bitmap);
  2353. return 0;
  2354. }
  2355. static int synx_cdsp_restart_notifier(struct notifier_block *nb,
  2356. unsigned long code, void *data)
  2357. {
  2358. struct synx_cdsp_ssr *cdsp_ssr = &synx_dev->cdsp_ssr;
  2359. if (&cdsp_ssr->nb != nb) {
  2360. dprintk(SYNX_ERR, "Invalid SSR Notifier block\n");
  2361. return NOTIFY_BAD;
  2362. }
  2363. switch (code) {
  2364. case QCOM_SSR_BEFORE_SHUTDOWN:
  2365. break;
  2366. case QCOM_SSR_AFTER_SHUTDOWN:
  2367. if (cdsp_ssr->ssrcnt != 0) {
  2368. dprintk(SYNX_INFO, "Cleaning up global memory\n");
  2369. synx_global_recover(SYNX_CORE_NSP);
  2370. }
  2371. break;
  2372. case QCOM_SSR_BEFORE_POWERUP:
  2373. break;
  2374. case QCOM_SSR_AFTER_POWERUP:
  2375. dprintk(SYNX_DBG, "CDSP is up");
  2376. if (cdsp_ssr->ssrcnt == 0)
  2377. cdsp_ssr->ssrcnt++;
  2378. break;
  2379. default:
  2380. dprintk(SYNX_ERR, "Unknown status code for CDSP SSR\n");
  2381. break;
  2382. }
  2383. return NOTIFY_DONE;
  2384. }
  2385. static int __init synx_init(void)
  2386. {
  2387. int rc;
  2388. dprintk(SYNX_INFO, "device initialization start\n");
  2389. synx_dev = kzalloc(sizeof(*synx_dev), GFP_KERNEL);
  2390. if (IS_ERR_OR_NULL(synx_dev))
  2391. return -SYNX_NOMEM;
  2392. rc = alloc_chrdev_region(&synx_dev->dev, 0, 1, SYNX_DEVICE_NAME);
  2393. if (rc < 0) {
  2394. dprintk(SYNX_ERR, "region allocation failed\n");
  2395. goto alloc_fail;
  2396. }
  2397. cdev_init(&synx_dev->cdev, &synx_fops);
  2398. synx_dev->cdev.owner = THIS_MODULE;
  2399. rc = cdev_add(&synx_dev->cdev, synx_dev->dev, 1);
  2400. if (rc < 0) {
  2401. dprintk(SYNX_ERR, "device registation failed\n");
  2402. goto reg_fail;
  2403. }
  2404. synx_dev->class = class_create(THIS_MODULE, SYNX_DEVICE_NAME);
  2405. device_create(synx_dev->class, NULL, synx_dev->dev,
  2406. NULL, SYNX_DEVICE_NAME);
  2407. synx_dev->wq_cb = alloc_workqueue(SYNX_WQ_CB_NAME,
  2408. WQ_HIGHPRI | WQ_UNBOUND, SYNX_WQ_CB_THREADS);
  2409. synx_dev->wq_cleanup = alloc_workqueue(SYNX_WQ_CLEANUP_NAME,
  2410. WQ_HIGHPRI | WQ_UNBOUND, SYNX_WQ_CLEANUP_THREADS);
  2411. if (!synx_dev->wq_cb || !synx_dev->wq_cleanup) {
  2412. dprintk(SYNX_ERR,
  2413. "high priority work queue creation failed\n");
  2414. rc = -SYNX_INVALID;
  2415. goto fail;
  2416. }
  2417. synx_dev->native = vzalloc(sizeof(*synx_dev->native));
  2418. if (IS_ERR_OR_NULL(synx_dev->native))
  2419. goto fail;
  2420. mutex_init(&synx_dev->vtbl_lock);
  2421. mutex_init(&synx_dev->error_lock);
  2422. INIT_LIST_HEAD(&synx_dev->error_list);
  2423. synx_dev->debugfs_root = synx_init_debugfs_dir(synx_dev);
  2424. rc = synx_global_mem_init();
  2425. if (rc) {
  2426. dprintk(SYNX_ERR, "shared mem init failed, err=%d\n", rc);
  2427. goto err;
  2428. }
  2429. synx_dev->cdsp_ssr.ssrcnt = 0;
  2430. synx_dev->cdsp_ssr.nb.notifier_call = synx_cdsp_restart_notifier;
  2431. synx_dev->cdsp_ssr.handle =
  2432. qcom_register_ssr_notifier("cdsp", &synx_dev->cdsp_ssr.nb);
  2433. if (synx_dev->cdsp_ssr.handle == NULL) {
  2434. dprintk(SYNX_ERR, "SSR registration failed\n");
  2435. goto err;
  2436. }
  2437. ipclite_register_client(synx_ipc_callback, NULL);
  2438. synx_local_mem_init();
  2439. dprintk(SYNX_INFO, "device initialization success\n");
  2440. return 0;
  2441. err:
  2442. vfree(synx_dev->native);
  2443. fail:
  2444. device_destroy(synx_dev->class, synx_dev->dev);
  2445. class_destroy(synx_dev->class);
  2446. reg_fail:
  2447. unregister_chrdev_region(synx_dev->dev, 1);
  2448. alloc_fail:
  2449. kfree(synx_dev);
  2450. synx_dev = NULL;
  2451. return rc;
  2452. }
  2453. static void __exit synx_exit(void)
  2454. {
  2455. struct error_node *err_node, *err_node_tmp;
  2456. flush_workqueue(synx_dev->wq_cb);
  2457. flush_workqueue(synx_dev->wq_cleanup);
  2458. device_destroy(synx_dev->class, synx_dev->dev);
  2459. class_destroy(synx_dev->class);
  2460. cdev_del(&synx_dev->cdev);
  2461. unregister_chrdev_region(synx_dev->dev, 1);
  2462. synx_remove_debugfs_dir(synx_dev);
  2463. /* release uncleared error nodes */
  2464. list_for_each_entry_safe(
  2465. err_node, err_node_tmp,
  2466. &synx_dev->error_list,
  2467. node) {
  2468. list_del(&err_node->node);
  2469. kfree(err_node);
  2470. }
  2471. mutex_destroy(&synx_dev->vtbl_lock);
  2472. mutex_destroy(&synx_dev->error_lock);
  2473. vfree(synx_dev->native);
  2474. kfree(synx_dev);
  2475. }
  2476. module_init(synx_init);
  2477. module_exit(synx_exit);
  2478. MODULE_DESCRIPTION("Global Synx Driver");
  2479. MODULE_LICENSE("GPL v2");