sde_rotator_dev.c 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "%s:%d: " fmt, __func__, __LINE__
  6. #include <linux/vmalloc.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/pm_runtime.h>
  11. #include <linux/regulator/consumer.h>
  12. #include <linux/delay.h>
  13. #include <linux/wait.h>
  14. #include <linux/of.h>
  15. #include <media/v4l2-ioctl.h>
  16. #include <media/v4l2-event.h>
  17. #include <media/videobuf2-v4l2.h>
  18. #include <media/v4l2-mem2mem.h>
  19. #include "sde_rotator_inline.h"
  20. #include "sde_rotator_base.h"
  21. #include "sde_rotator_core.h"
  22. #include "sde_rotator_dev.h"
  23. #include "sde_rotator_debug.h"
  24. #include "sde_rotator_trace.h"
  25. /* Start v4l2 device number (default allocation) */
  26. #define SDE_ROTATOR_BASE_DEVICE_NUMBER -1
  27. /* Default value for early_submit flag */
  28. #define SDE_ROTATOR_EARLY_SUBMIT 1
  29. /* Timeout (msec) waiting for stream to turn off. */
  30. #define SDE_ROTATOR_STREAM_OFF_TIMEOUT 500
  31. /* acquire fence time out, following other driver fence time out practice */
  32. #define SDE_ROTATOR_FENCE_TIMEOUT MSEC_PER_SEC
  33. /* Timeout (msec) waiting for ctx open */
  34. #define SDE_ROTATOR_CTX_OPEN_TIMEOUT 500
  35. /* Rotator default fps */
  36. #define SDE_ROTATOR_DEFAULT_FPS 60
  37. /* Rotator rotation angles */
  38. #define SDE_ROTATOR_DEGREE_270 270
  39. #define SDE_ROTATOR_DEGREE_180 180
  40. #define SDE_ROTATOR_DEGREE_90 90
  41. /* Inline rotator qos request */
  42. #define SDE_ROTATOR_ADD_REQUEST 1
  43. #define SDE_ROTATOR_REMOVE_REQUEST 0
  44. static void sde_rotator_submit_handler(struct kthread_work *work);
  45. static void sde_rotator_retire_handler(struct kthread_work *work);
  46. #ifdef CONFIG_COMPAT
  47. static long sde_rotator_compat_ioctl32(struct file *file,
  48. unsigned int cmd, unsigned long arg);
  49. #endif
  50. /*
  51. * sde_rotator_ctx_from_fh - Get rotator context from v4l2 fh.
  52. * @fh: Pointer to v4l2 fh.
  53. */
  54. static inline struct sde_rotator_ctx *sde_rotator_ctx_from_fh(
  55. struct v4l2_fh *fh)
  56. {
  57. return container_of(fh, struct sde_rotator_ctx, fh);
  58. }
  59. /*
  60. * sde_rotator_get_flags_from_ctx - Get low-level command flag
  61. * @ctx: Pointer to rotator context.
  62. */
  63. static uint32_t sde_rotator_get_flags_from_ctx(struct sde_rotator_ctx *ctx)
  64. {
  65. uint32_t ret_flags = 0;
  66. if (ctx->rotate == SDE_ROTATOR_DEGREE_270)
  67. ret_flags |= SDE_ROTATION_270;
  68. else if (ctx->rotate == SDE_ROTATOR_DEGREE_180)
  69. ret_flags |= SDE_ROTATION_180;
  70. else if (ctx->rotate == SDE_ROTATOR_DEGREE_90)
  71. ret_flags |= SDE_ROTATION_90;
  72. if (ctx->hflip)
  73. ret_flags ^= SDE_ROTATION_FLIP_LR;
  74. if (ctx->vflip)
  75. ret_flags ^= SDE_ROTATION_FLIP_UD;
  76. if (ctx->secure)
  77. ret_flags |= SDE_ROTATION_SECURE;
  78. if (ctx->secure_camera)
  79. ret_flags |= SDE_ROTATION_SECURE_CAMERA;
  80. if (ctx->format_out.fmt.pix.field == V4L2_FIELD_INTERLACED &&
  81. ctx->format_cap.fmt.pix.field == V4L2_FIELD_NONE)
  82. ret_flags |= SDE_ROTATION_DEINTERLACE;
  83. return ret_flags;
  84. }
  85. /*
  86. * sde_rotator_get_config_from_ctx - Fill rotator configure structure.
  87. * @ctx: Pointer to rotator ctx.
  88. * @config: Pointer to config structure.
  89. */
  90. static void sde_rotator_get_config_from_ctx(struct sde_rotator_ctx *ctx,
  91. struct sde_rotation_config *config)
  92. {
  93. memset(config, 0, sizeof(struct sde_rotation_config));
  94. config->flags = sde_rotator_get_flags_from_ctx(ctx);
  95. config->frame_rate = (ctx->timeperframe.numerator) ?
  96. ctx->timeperframe.denominator
  97. / ctx->timeperframe.numerator : 0;
  98. config->session_id = ctx->session_id;
  99. config->input.width = ctx->crop_out.width;
  100. config->input.height = ctx->crop_out.height;
  101. config->input.format = ctx->format_out.fmt.pix.pixelformat;
  102. config->input.comp_ratio.numer = 1;
  103. config->input.comp_ratio.denom = 1;
  104. config->output.width = ctx->crop_cap.width;
  105. config->output.height = ctx->crop_cap.height;
  106. config->output.format = ctx->format_cap.fmt.pix.pixelformat;
  107. config->output.comp_ratio.numer = 1;
  108. config->output.comp_ratio.denom = 1;
  109. /*
  110. * Use compression ratio of the first buffer to estimate
  111. * performance requirement of the session. If core layer does
  112. * not support dynamic per buffer compression ratio recalculation,
  113. * this configuration will determine the overall static compression
  114. * ratio of the session.
  115. */
  116. if (ctx->vbinfo_out)
  117. config->input.comp_ratio = ctx->vbinfo_out[0].comp_ratio;
  118. if (ctx->vbinfo_cap)
  119. config->output.comp_ratio = ctx->vbinfo_cap[0].comp_ratio;
  120. SDEDEV_DBG(ctx->rot_dev->dev, "config s:%d out_cr:%u/%u cap_cr:%u/%u\n",
  121. ctx->session_id,
  122. config->input.comp_ratio.numer,
  123. config->input.comp_ratio.denom,
  124. config->output.comp_ratio.numer,
  125. config->output.comp_ratio.denom);
  126. }
  127. /*
  128. * sde_rotator_get_item_from_ctx - Fill rotator item structure.
  129. * @ctx: Pointer to rotator ctx.
  130. * @item: Pointer to item structure.
  131. */
  132. static void sde_rotator_get_item_from_ctx(struct sde_rotator_ctx *ctx,
  133. struct sde_rotation_item *item)
  134. {
  135. memset(item, 0, sizeof(struct sde_rotation_item));
  136. item->flags = sde_rotator_get_flags_from_ctx(ctx);
  137. item->session_id = ctx->session_id;
  138. item->sequence_id = 0;
  139. /* assign high/low priority */
  140. item->wb_idx = (ctx->fh.prio >= V4L2_PRIORITY_DEFAULT) ? 0 : 1;
  141. item->src_rect.x = ctx->crop_out.left;
  142. item->src_rect.y = ctx->crop_out.top;
  143. item->src_rect.w = ctx->crop_out.width;
  144. item->src_rect.h = ctx->crop_out.height;
  145. item->input.width = ctx->format_out.fmt.pix.width;
  146. item->input.height = ctx->format_out.fmt.pix.height;
  147. item->input.format = ctx->format_out.fmt.pix.pixelformat;
  148. item->input.planes[0].fd = -1;
  149. item->input.planes[0].offset = 0;
  150. item->input.planes[0].stride = ctx->format_out.fmt.pix.bytesperline;
  151. item->input.plane_count = 1;
  152. item->input.fence = NULL;
  153. item->input.comp_ratio.numer = 1;
  154. item->input.comp_ratio.denom = 1;
  155. item->dst_rect.x = ctx->crop_cap.left;
  156. item->dst_rect.y = ctx->crop_cap.top;
  157. item->dst_rect.w = ctx->crop_cap.width;
  158. item->dst_rect.h = ctx->crop_cap.height;
  159. item->output.width = ctx->format_cap.fmt.pix.width;
  160. item->output.height = ctx->format_cap.fmt.pix.height;
  161. item->output.format = ctx->format_cap.fmt.pix.pixelformat;
  162. item->output.planes[0].fd = -1;
  163. item->output.planes[0].offset = 0;
  164. item->output.planes[0].stride = ctx->format_cap.fmt.pix.bytesperline;
  165. item->output.plane_count = 1;
  166. item->output.fence = NULL;
  167. item->output.comp_ratio.numer = 1;
  168. item->output.comp_ratio.denom = 1;
  169. }
  170. /*
  171. * sde_rotator_format_recalc - Recalculate format parameters.
  172. * @f: v4l2 format.
  173. */
  174. static void sde_rotator_format_recalc(struct v4l2_format *f)
  175. {
  176. int ret;
  177. struct sde_mdp_format_params *fmt;
  178. struct sde_mdp_plane_sizes ps;
  179. fmt = sde_get_format_params(f->fmt.pix.pixelformat);
  180. if (!fmt) {
  181. SDEROT_ERR("invalid format\n");
  182. goto error_fmt;
  183. }
  184. ret = sde_mdp_get_plane_sizes(fmt,
  185. f->fmt.pix.width, f->fmt.pix.height, &ps, 0, 0);
  186. if (ret) {
  187. SDEROT_ERR("invalid plane size\n");
  188. goto error_fmt;
  189. }
  190. f->fmt.pix.bytesperline = ps.ystride[0];
  191. f->fmt.pix.sizeimage = ps.total_size;
  192. return;
  193. error_fmt:
  194. f->fmt.pix.bytesperline = 0;
  195. f->fmt.pix.sizeimage = 0;
  196. }
  197. /*
  198. * sde_rotator_validate_item - Check if rotator item is valid for processing.
  199. * @ctx: Pointer to rotator ctx.
  200. * @item: Pointer to item structure
  201. */
  202. static int sde_rotator_validate_item(struct sde_rotator_ctx *ctx,
  203. struct sde_rotation_item *item)
  204. {
  205. int ret;
  206. struct sde_rot_entry_container *req;
  207. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  208. sde_rot_mgr_lock(rot_dev->mgr);
  209. req = sde_rotator_req_init(rot_dev->mgr, ctx->private, item, 1, 0);
  210. if (IS_ERR_OR_NULL(req)) {
  211. SDEDEV_ERR(rot_dev->dev, "fail allocate item\n");
  212. return -ENOMEM;
  213. }
  214. ret = sde_rotator_validate_request(rot_dev->mgr, ctx->private, req);
  215. sde_rot_mgr_unlock(rot_dev->mgr);
  216. devm_kfree(rot_dev->dev, req);
  217. return ret;
  218. }
  219. /*
  220. * sde_rotator_queue_setup - vb2_ops queue_setup callback.
  221. * @q: Pointer to vb2 queue struct.
  222. * @num_buffers: Pointer of number of buffers requested.
  223. * @num_planes: Pointer to number of planes requested.
  224. * @sizes: Array containing sizes of planes.
  225. * @alloc_ctxs: Array of allocated contexts for each plane.
  226. */
  227. static int sde_rotator_queue_setup(struct vb2_queue *q,
  228. unsigned int *num_buffers, unsigned int *num_planes,
  229. unsigned int sizes[], struct device *alloc_devs[])
  230. {
  231. struct sde_rotator_ctx *ctx = vb2_get_drv_priv(q);
  232. int i;
  233. if (!num_buffers)
  234. return -EINVAL;
  235. switch (q->type) {
  236. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  237. sizes[0] = ctx->format_out.fmt.pix.sizeimage;
  238. break;
  239. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  240. sizes[0] = ctx->format_cap.fmt.pix.sizeimage;
  241. break;
  242. default:
  243. return -EINVAL;
  244. }
  245. *num_planes = 1;
  246. alloc_devs[0] = (struct device *)ctx;
  247. switch (q->type) {
  248. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  249. ctx->nbuf_out = *num_buffers;
  250. kfree(ctx->vbinfo_out);
  251. ctx->vbinfo_out = kcalloc(ctx->nbuf_out,
  252. sizeof(struct sde_rotator_vbinfo), GFP_KERNEL);
  253. if (!ctx->vbinfo_out)
  254. return -ENOMEM;
  255. for (i = 0; i < ctx->nbuf_out; i++) {
  256. ctx->vbinfo_out[i].fd = -1;
  257. ctx->vbinfo_out[i].comp_ratio.numer = 1;
  258. ctx->vbinfo_out[i].comp_ratio.denom = 1;
  259. }
  260. break;
  261. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  262. ctx->nbuf_cap = *num_buffers;
  263. kfree(ctx->vbinfo_cap);
  264. ctx->vbinfo_cap = kcalloc(ctx->nbuf_cap,
  265. sizeof(struct sde_rotator_vbinfo), GFP_KERNEL);
  266. if (!ctx->vbinfo_cap)
  267. return -ENOMEM;
  268. for (i = 0; i < ctx->nbuf_cap; i++) {
  269. ctx->vbinfo_cap[i].fd = -1;
  270. ctx->vbinfo_cap[i].comp_ratio.numer = 1;
  271. ctx->vbinfo_cap[i].comp_ratio.denom = 1;
  272. }
  273. break;
  274. default:
  275. return -EINVAL;
  276. }
  277. return 0;
  278. }
  279. /*
  280. * sde_rotator_buf_queue - vb2_ops buf_queue callback.
  281. * @vb: Pointer to vb2 buffer struct.
  282. */
  283. static void sde_rotator_buf_queue(struct vb2_buffer *vb)
  284. {
  285. struct sde_rotator_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  286. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  287. v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  288. }
  289. /*
  290. * sde_rotator_buf_finish - vb2_ops buf_finish to finalize buffer before going
  291. * back to user space
  292. * @vb: Pointer to vb2 buffer struct.
  293. */
  294. static void sde_rotator_buf_finish(struct vb2_buffer *vb)
  295. {
  296. struct sde_rotator_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  297. int i;
  298. SDEDEV_DBG(ctx->rot_dev->dev,
  299. "buf_finish t:%d i:%d s:%d m:%u np:%d up:%lu\n",
  300. vb->type, vb->index, vb->state,
  301. vb->vb2_queue->memory,
  302. vb->num_planes,
  303. vb->planes[0].m.userptr);
  304. if (vb->vb2_queue->memory != VB2_MEMORY_USERPTR)
  305. return;
  306. /*
  307. * We use userptr to tunnel fd, and fd can be the same across qbuf
  308. * even though the underlying buffer is different. Since vb2 layer
  309. * optimizes memory mapping for userptr by first checking if userptr
  310. * has changed, it will not trigger put_userptr if fd value does
  311. * not change. In order to force buffer release, we need to clear
  312. * userptr when the current buffer is done and ready to go back to
  313. * user mode. Since 0 is a valid fd, reset userptr to -1 instead.
  314. */
  315. for (i = 0; i < vb->num_planes; i++)
  316. vb->planes[i].m.userptr = ~0;
  317. }
  318. /*
  319. * sde_rotator_return_all_buffers - Return all buffers with the given status.
  320. * @q: Pointer to vb2 buffer queue struct.
  321. * @state: State of the buffer
  322. */
  323. static void sde_rotator_return_all_buffers(struct vb2_queue *q,
  324. enum vb2_buffer_state state)
  325. {
  326. struct sde_rotator_ctx *ctx = vb2_get_drv_priv(q);
  327. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  328. SDEDEV_DBG(rot_dev->dev,
  329. "return q t:%d c:%d dc:%d s:%d\n",
  330. q->type, q->queued_count,
  331. atomic_read(&q->owned_by_drv_count),
  332. state);
  333. /* return buffers according videobuffer2-core.h */
  334. if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  335. struct vb2_v4l2_buffer *buf;
  336. while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx))) {
  337. SDEDEV_DBG(rot_dev->dev,
  338. "return vb t:%d i:%d\n",
  339. buf->vb2_buf.type,
  340. buf->vb2_buf.index);
  341. v4l2_m2m_buf_done(buf, state);
  342. }
  343. } else if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  344. struct vb2_v4l2_buffer *buf;
  345. while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx))) {
  346. SDEDEV_DBG(rot_dev->dev,
  347. "return vb t:%d i:%d\n",
  348. buf->vb2_buf.type,
  349. buf->vb2_buf.index);
  350. v4l2_m2m_buf_done(buf, state);
  351. }
  352. } else {
  353. SDEDEV_ERR(rot_dev->dev, "unsupported vb t:%d\n", q->type);
  354. }
  355. }
  356. /*
  357. * sde_rotator_start_streaming - vb2_ops start_streaming callback.
  358. * @q: Pointer to vb2 queue struct.
  359. * @count: Number of buffer queued before stream on call.
  360. */
  361. static int sde_rotator_start_streaming(struct vb2_queue *q, unsigned int count)
  362. {
  363. struct sde_rotator_ctx *ctx = vb2_get_drv_priv(q);
  364. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  365. SDEDEV_DBG(rot_dev->dev, "start streaming s:%d t:%d\n",
  366. ctx->session_id, q->type);
  367. if (!list_empty(&ctx->pending_list)) {
  368. SDEDEV_ERR(rot_dev->dev,
  369. "command pending error s:%d t:%d p:%d\n",
  370. ctx->session_id, q->type,
  371. !list_empty(&ctx->pending_list));
  372. return -EINVAL;
  373. }
  374. ctx->abort_pending = 0;
  375. return 0;
  376. }
  377. /*
  378. * sde_rotator_stop_streaming - vb2_ops stop_streaming callback.
  379. * @q: Pointer to vb2 queue struct.
  380. *
  381. * This function will block waiting for stream to stop. Unlock queue
  382. * lock to avoid deadlock.
  383. */
  384. static void sde_rotator_stop_streaming(struct vb2_queue *q)
  385. {
  386. struct sde_rotator_ctx *ctx = vb2_get_drv_priv(q);
  387. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  388. struct sde_rotator_request *request;
  389. struct list_head *curr, *next;
  390. int i;
  391. int ret;
  392. SDEDEV_DBG(rot_dev->dev, "stop streaming s:%d t:%d p:%d\n",
  393. ctx->session_id, q->type,
  394. !list_empty(&ctx->pending_list));
  395. ctx->abort_pending = 1;
  396. mutex_unlock(q->lock);
  397. ret = wait_event_timeout(ctx->wait_queue,
  398. list_empty(&ctx->pending_list),
  399. msecs_to_jiffies(rot_dev->streamoff_timeout));
  400. mutex_lock(q->lock);
  401. if (!ret) {
  402. SDEDEV_ERR(rot_dev->dev,
  403. "timeout to stream off s:%d t:%d p:%d\n",
  404. ctx->session_id, q->type,
  405. !list_empty(&ctx->pending_list));
  406. SDEROT_EVTLOG(ctx->session_id, q->type,
  407. !list_empty(&ctx->pending_list),
  408. SDE_ROT_EVTLOG_ERROR);
  409. sde_rot_mgr_lock(rot_dev->mgr);
  410. sde_rotator_cancel_all_requests(rot_dev->mgr, ctx->private);
  411. sde_rot_mgr_unlock(rot_dev->mgr);
  412. list_for_each_safe(curr, next, &ctx->pending_list) {
  413. request = container_of(curr, struct sde_rotator_request,
  414. list);
  415. SDEDEV_DBG(rot_dev->dev, "cancel request s:%d\n",
  416. ctx->session_id);
  417. mutex_unlock(q->lock);
  418. kthread_cancel_work_sync(&request->submit_work);
  419. kthread_cancel_work_sync(&request->retire_work);
  420. mutex_lock(q->lock);
  421. spin_lock(&ctx->list_lock);
  422. list_del_init(&request->list);
  423. list_add_tail(&request->list, &ctx->retired_list);
  424. spin_unlock(&ctx->list_lock);
  425. }
  426. }
  427. sde_rotator_return_all_buffers(q, VB2_BUF_STATE_ERROR);
  428. /* clear fence for buffer */
  429. sde_rotator_resync_timeline(ctx->work_queue.timeline);
  430. if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  431. for (i = 0; i < ctx->nbuf_cap; i++) {
  432. struct sde_rotator_vbinfo *vbinfo =
  433. &ctx->vbinfo_cap[i];
  434. if (vbinfo->fence) {
  435. /* fence is not used */
  436. SDEDEV_DBG(rot_dev->dev,
  437. "put fence s:%d t:%d i:%d\n",
  438. ctx->session_id, q->type, i);
  439. sde_rotator_put_sync_fence(vbinfo->fence);
  440. }
  441. vbinfo->fence = NULL;
  442. vbinfo->fd = -1;
  443. }
  444. } else if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  445. for (i = 0; i < ctx->nbuf_out; i++) {
  446. struct sde_rotator_vbinfo *vbinfo =
  447. &ctx->vbinfo_out[i];
  448. if (vbinfo->fence) {
  449. SDEDEV_DBG(rot_dev->dev,
  450. "put fence s:%d t:%d i:%d\n",
  451. ctx->session_id, q->type, i);
  452. sde_rotator_put_sync_fence(vbinfo->fence);
  453. }
  454. vbinfo->fence = NULL;
  455. vbinfo->fd = -1;
  456. }
  457. }
  458. }
  459. /* Videobuf2 queue callbacks. */
  460. static const struct vb2_ops sde_rotator_vb2_q_ops = {
  461. .queue_setup = sde_rotator_queue_setup,
  462. .buf_queue = sde_rotator_buf_queue,
  463. .start_streaming = sde_rotator_start_streaming,
  464. .stop_streaming = sde_rotator_stop_streaming,
  465. .wait_prepare = vb2_ops_wait_prepare,
  466. .wait_finish = vb2_ops_wait_finish,
  467. .buf_finish = sde_rotator_buf_finish,
  468. };
  469. /*
  470. * sde_rotator_get_userptr - Map and get buffer handler for user pointer buffer.
  471. * @dev: device allocated in buf_setup.
  472. * @vaddr: Virtual addr passed from userpsace (in our case ion fd)
  473. * @size: Size of the buffer
  474. * @dma_dir: DMA data direction of the given buffer.
  475. */
  476. static void *sde_rotator_get_userptr(struct device *dev,
  477. unsigned long vaddr, unsigned long size,
  478. enum dma_data_direction dma_dir)
  479. {
  480. struct sde_rotator_ctx *ctx = (struct sde_rotator_ctx *)dev;
  481. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  482. struct sde_rotator_buf_handle *buf;
  483. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  484. if (!buf)
  485. return ERR_PTR(-ENOMEM);
  486. buf->fd = vaddr;
  487. buf->secure = ctx->secure || ctx->secure_camera;
  488. buf->ctx = ctx;
  489. buf->rot_dev = rot_dev;
  490. buf->size = size;
  491. buf->buffer = dma_buf_get(buf->fd);
  492. if (IS_ERR_OR_NULL(buf->buffer)) {
  493. SDEDEV_ERR(rot_dev->dev,
  494. "fail get dmabuf fd:%d r:%ld\n",
  495. buf->fd, PTR_ERR(buf->buffer));
  496. goto error_buf_get;
  497. }
  498. SDEDEV_DBG(rot_dev->dev,
  499. "get dmabuf s:%d fd:%d buf:%pad\n",
  500. buf->ctx->session_id,
  501. buf->fd, &buf->buffer);
  502. return buf;
  503. error_buf_get:
  504. kfree(buf);
  505. return ERR_PTR(-ENOMEM);
  506. }
  507. /*
  508. * sde_rotator_put_userptr - Unmap and free buffer handler.
  509. * @buf_priv: Buffer handler allocated get_userptr callback.
  510. */
  511. static void sde_rotator_put_userptr(void *buf_priv)
  512. {
  513. struct sde_rotator_buf_handle *buf = buf_priv;
  514. if (IS_ERR_OR_NULL(buf))
  515. return;
  516. if (!buf->rot_dev || !buf->ctx) {
  517. WARN_ON(!buf->rot_dev || !buf->ctx);
  518. SDEROT_ERR("null rotator device/context\n");
  519. return;
  520. }
  521. SDEDEV_DBG(buf->rot_dev->dev, "put dmabuf s:%d fd:%d buf:%pad\n",
  522. buf->ctx->session_id,
  523. buf->fd, &buf->buffer);
  524. if (buf->buffer) {
  525. dma_buf_put(buf->buffer);
  526. buf->buffer = NULL;
  527. }
  528. kfree(buf_priv);
  529. }
  530. /* Videobuf2 memory callbacks. */
  531. static struct vb2_mem_ops sde_rotator_vb2_mem_ops = {
  532. .get_userptr = sde_rotator_get_userptr,
  533. .put_userptr = sde_rotator_put_userptr,
  534. };
  535. /*
  536. * sde_rotator_s_ctx_ctrl - set context control variable to v4l2 control
  537. * @ctx: Pointer to rotator context.
  538. * @ctx_ctrl: Pointer to context control variable
  539. * @ctrl: Pointer to v4l2 control variable
  540. */
  541. static int sde_rotator_s_ctx_ctrl(struct sde_rotator_ctx *ctx,
  542. s32 *ctx_ctrl, struct v4l2_ctrl *ctrl)
  543. {
  544. *ctx_ctrl = ctrl->val;
  545. return 0;
  546. }
  547. /*
  548. * sde_rotator_s_ctrl - Set control.
  549. * @ctrl: Pointer to v4l2 control structure.
  550. */
  551. static int sde_rotator_s_ctrl(struct v4l2_ctrl *ctrl)
  552. {
  553. struct sde_rotator_ctx *ctx =
  554. container_of(ctrl->handler,
  555. struct sde_rotator_ctx, ctrl_handler);
  556. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  557. int ret;
  558. SDEDEV_DBG(rot_dev->dev, "set %s:%d s:%d\n", ctrl->name, ctrl->val,
  559. ctx->session_id);
  560. sde_rot_mgr_lock(rot_dev->mgr);
  561. switch (ctrl->id) {
  562. case V4L2_CID_HFLIP:
  563. ret = sde_rotator_s_ctx_ctrl(ctx, &ctx->hflip, ctrl);
  564. break;
  565. case V4L2_CID_VFLIP:
  566. ret = sde_rotator_s_ctx_ctrl(ctx, &ctx->vflip, ctrl);
  567. break;
  568. case V4L2_CID_ROTATE:
  569. ret = sde_rotator_s_ctx_ctrl(ctx, &ctx->rotate, ctrl);
  570. break;
  571. case V4L2_CID_SDE_ROTATOR_SECURE:
  572. ret = sde_rotator_s_ctx_ctrl(ctx, &ctx->secure, ctrl);
  573. break;
  574. case V4L2_CID_SDE_ROTATOR_SECURE_CAMERA:
  575. ret = sde_rotator_s_ctx_ctrl(ctx, &ctx->secure_camera, ctrl);
  576. break;
  577. default:
  578. v4l2_warn(&rot_dev->v4l2_dev, "invalid control %d\n", ctrl->id);
  579. ret = -EINVAL;
  580. }
  581. sde_rot_mgr_unlock(rot_dev->mgr);
  582. return ret;
  583. }
  584. /*
  585. * sde_rotator_ctrl_ops - Control operations.
  586. */
  587. static const struct v4l2_ctrl_ops sde_rotator_ctrl_ops = {
  588. .s_ctrl = sde_rotator_s_ctrl,
  589. };
  590. /*
  591. * sde_rotator_ctrl_secure - Non-secure/Secure.
  592. */
  593. static const struct v4l2_ctrl_config sde_rotator_ctrl_secure = {
  594. .ops = &sde_rotator_ctrl_ops,
  595. .id = V4L2_CID_SDE_ROTATOR_SECURE,
  596. .name = "Non-secure/Secure Domain",
  597. .type = V4L2_CTRL_TYPE_INTEGER,
  598. .def = 0,
  599. .min = 0,
  600. .max = 1,
  601. .step = 1,
  602. };
  603. static const struct v4l2_ctrl_config sde_rotator_ctrl_secure_camera = {
  604. .ops = &sde_rotator_ctrl_ops,
  605. .id = V4L2_CID_SDE_ROTATOR_SECURE_CAMERA,
  606. .name = "Secure Camera content",
  607. .type = V4L2_CTRL_TYPE_INTEGER,
  608. .def = 0,
  609. .min = 0,
  610. .max = 1,
  611. .step = 1,
  612. };
  613. /*
  614. * sde_rotator_ctx_show - show context state.
  615. */
  616. static ssize_t sde_rotator_ctx_show(struct kobject *kobj,
  617. struct kobj_attribute *attr, char *buf)
  618. {
  619. size_t len = PAGE_SIZE;
  620. int cnt = 0;
  621. struct sde_rotator_ctx *ctx =
  622. container_of(kobj, struct sde_rotator_ctx, kobj);
  623. if (!ctx)
  624. return cnt;
  625. #define SPRINT(fmt, ...) \
  626. (cnt += scnprintf(buf + cnt, len - cnt, fmt, ##__VA_ARGS__))
  627. SPRINT("rotate=%d\n", ctx->rotate);
  628. SPRINT("hflip=%d\n", ctx->hflip);
  629. SPRINT("vflip=%d\n", ctx->vflip);
  630. SPRINT("priority=%d\n", ctx->fh.prio);
  631. SPRINT("secure=%d\n", ctx->secure);
  632. SPRINT("timeperframe=%u %u\n", ctx->timeperframe.numerator,
  633. ctx->timeperframe.denominator);
  634. SPRINT("nbuf_out=%d\n", ctx->nbuf_out);
  635. SPRINT("nbuf_cap=%d\n", ctx->nbuf_cap);
  636. SPRINT("crop_out=%u %u %u %u\n",
  637. ctx->crop_out.left, ctx->crop_out.top,
  638. ctx->crop_out.width, ctx->crop_out.height);
  639. SPRINT("crop_cap=%u %u %u %u\n",
  640. ctx->crop_cap.left, ctx->crop_cap.top,
  641. ctx->crop_cap.width, ctx->crop_cap.height);
  642. SPRINT("fmt_out=%c%c%c%c %u %u %u %u\n",
  643. (ctx->format_out.fmt.pix.pixelformat>>0)&0xff,
  644. (ctx->format_out.fmt.pix.pixelformat>>8)&0xff,
  645. (ctx->format_out.fmt.pix.pixelformat>>16)&0xff,
  646. (ctx->format_out.fmt.pix.pixelformat>>24)&0xff,
  647. ctx->format_out.fmt.pix.width,
  648. ctx->format_out.fmt.pix.height,
  649. ctx->format_out.fmt.pix.bytesperline,
  650. ctx->format_out.fmt.pix.sizeimage);
  651. SPRINT("fmt_cap=%c%c%c%c %u %u %u %u\n",
  652. (ctx->format_cap.fmt.pix.pixelformat>>0)&0xff,
  653. (ctx->format_cap.fmt.pix.pixelformat>>8)&0xff,
  654. (ctx->format_cap.fmt.pix.pixelformat>>16)&0xff,
  655. (ctx->format_cap.fmt.pix.pixelformat>>24)&0xff,
  656. ctx->format_cap.fmt.pix.width,
  657. ctx->format_cap.fmt.pix.height,
  658. ctx->format_cap.fmt.pix.bytesperline,
  659. ctx->format_cap.fmt.pix.sizeimage);
  660. SPRINT("abort_pending=%d\n", ctx->abort_pending);
  661. SPRINT("command_pending=%d\n", !list_empty(&ctx->pending_list));
  662. SPRINT("sequence=%u\n",
  663. sde_rotator_get_timeline_commit_ts(ctx->work_queue.timeline));
  664. SPRINT("timestamp=%u\n",
  665. sde_rotator_get_timeline_retire_ts(ctx->work_queue.timeline));
  666. return cnt;
  667. }
  668. static struct kobj_attribute sde_rotator_ctx_attr =
  669. __ATTR(state, 0664, sde_rotator_ctx_show, NULL);
  670. static struct attribute *sde_rotator_fs_attrs[] = {
  671. &sde_rotator_ctx_attr.attr,
  672. NULL
  673. };
  674. static struct attribute_group sde_rotator_fs_attr_group = {
  675. .attrs = sde_rotator_fs_attrs
  676. };
  677. /*
  678. * sde_rotator_ctx_show - sysfs show callback.
  679. */
  680. static ssize_t sde_rotator_fs_show(struct kobject *kobj,
  681. struct attribute *attr, char *buf)
  682. {
  683. ssize_t ret = -EIO;
  684. struct kobj_attribute *kattr =
  685. container_of(attr, struct kobj_attribute, attr);
  686. if (kattr->show)
  687. ret = kattr->show(kobj, kattr, buf);
  688. return ret;
  689. }
  690. /*
  691. * sde_rotator_fs_store - sysfs store callback.
  692. */
  693. static ssize_t sde_rotator_fs_store(struct kobject *kobj,
  694. struct attribute *attr, const char *buf, size_t count)
  695. {
  696. ssize_t ret = -EIO;
  697. struct kobj_attribute *kattr =
  698. container_of(attr, struct kobj_attribute, attr);
  699. if (kattr->store)
  700. ret = kattr->store(kobj, kattr, buf, count);
  701. return ret;
  702. }
  703. static const struct sysfs_ops sde_rotator_fs_ops = {
  704. .show = sde_rotator_fs_show,
  705. .store = sde_rotator_fs_store,
  706. };
  707. static struct kobj_type sde_rotator_fs_ktype = {
  708. .sysfs_ops = &sde_rotator_fs_ops,
  709. };
  710. /*
  711. * sde_rotator_queue_init - m2m_ops queue_setup callback.
  712. * @priv: Pointer to rotator ctx.
  713. * @src_vq: vb2 source queue.
  714. * @dst_vq: vb2 destination queue.
  715. */
  716. static int sde_rotator_queue_init(void *priv, struct vb2_queue *src_vq,
  717. struct vb2_queue *dst_vq)
  718. {
  719. struct sde_rotator_ctx *ctx = priv;
  720. int ret;
  721. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  722. src_vq->io_modes = VB2_USERPTR;
  723. src_vq->drv_priv = ctx;
  724. src_vq->mem_ops = &sde_rotator_vb2_mem_ops;
  725. src_vq->ops = &sde_rotator_vb2_q_ops;
  726. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  727. src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  728. src_vq->lock = &ctx->rot_dev->lock;
  729. src_vq->min_buffers_needed = 1;
  730. src_vq->dev = ctx->rot_dev->dev;
  731. ret = vb2_queue_init(src_vq);
  732. if (ret) {
  733. SDEDEV_ERR(ctx->rot_dev->dev,
  734. "fail init src queue r:%d\n", ret);
  735. return ret;
  736. }
  737. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  738. dst_vq->io_modes = VB2_USERPTR;
  739. dst_vq->drv_priv = ctx;
  740. dst_vq->mem_ops = &sde_rotator_vb2_mem_ops;
  741. dst_vq->ops = &sde_rotator_vb2_q_ops;
  742. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  743. dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  744. dst_vq->lock = &ctx->rot_dev->lock;
  745. dst_vq->min_buffers_needed = 1;
  746. src_vq->dev = ctx->rot_dev->dev;
  747. ret = vb2_queue_init(dst_vq);
  748. if (ret) {
  749. SDEDEV_ERR(ctx->rot_dev->dev,
  750. "fail init dst queue r:%d\n", ret);
  751. return ret;
  752. }
  753. return 0;
  754. }
  755. /*
  756. * sde_rotator_ctx_open - Rotator device open method.
  757. * @rot_dev: Pointer to rotator device structure
  758. * @file: Pointer to file struct (optional)
  759. * return: Pointer rotator context if success; ptr error code, otherwise.
  760. */
  761. struct sde_rotator_ctx *sde_rotator_ctx_open(
  762. struct sde_rotator_device *rot_dev, struct file *file)
  763. {
  764. struct video_device *video = file ? video_devdata(file) : NULL;
  765. struct sde_rotator_ctx *ctx;
  766. struct v4l2_ctrl_handler *ctrl_handler;
  767. char name[32];
  768. int i, ret;
  769. if (atomic_read(&rot_dev->mgr->device_suspended))
  770. return ERR_PTR(-EPERM);
  771. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  772. if (!ctx)
  773. return ERR_PTR(-ENOMEM);
  774. if (mutex_lock_interruptible(&rot_dev->lock)) {
  775. ret = -ERESTARTSYS;
  776. goto error_lock;
  777. }
  778. /* wait until exclusive ctx, if exists, finishes or timeout */
  779. while (rot_dev->excl_ctx) {
  780. SDEROT_DBG("waiting to open %s session %d ...\n",
  781. file ? "v4l2" : "excl", rot_dev->session_id);
  782. mutex_unlock(&rot_dev->lock);
  783. ret = wait_event_interruptible_timeout(rot_dev->open_wq,
  784. !rot_dev->excl_ctx,
  785. msecs_to_jiffies(rot_dev->open_timeout));
  786. if (ret < 0) {
  787. goto error_lock;
  788. } else if (!ret) {
  789. SDEROT_WARN("timeout to open session %d\n",
  790. rot_dev->session_id);
  791. SDEROT_EVTLOG(rot_dev->session_id,
  792. SDE_ROT_EVTLOG_ERROR);
  793. ret = -EBUSY;
  794. goto error_lock;
  795. } else if (mutex_lock_interruptible(&rot_dev->lock)) {
  796. ret = -ERESTARTSYS;
  797. goto error_lock;
  798. }
  799. }
  800. ctx->rot_dev = rot_dev;
  801. ctx->file = file;
  802. /* Set context defaults */
  803. ctx->session_id = rot_dev->session_id++;
  804. SDEDEV_DBG(ctx->rot_dev->dev, "open %d\n", ctx->session_id);
  805. ctx->timeperframe.numerator = 1;
  806. ctx->timeperframe.denominator = SDE_ROTATOR_DEFAULT_FPS;
  807. ctx->hflip = 0;
  808. ctx->vflip = 0;
  809. ctx->rotate = 0;
  810. ctx->secure = 0;
  811. ctx->abort_pending = 0;
  812. ctx->kthread_id = -1;
  813. ctx->format_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  814. ctx->format_cap.fmt.pix.pixelformat = SDE_PIX_FMT_Y_CBCR_H2V2;
  815. ctx->format_cap.fmt.pix.width = 640;
  816. ctx->format_cap.fmt.pix.height = 480;
  817. ctx->crop_cap.width = 640;
  818. ctx->crop_cap.height = 480;
  819. ctx->format_out.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  820. ctx->format_out.fmt.pix.pixelformat = SDE_PIX_FMT_Y_CBCR_H2V2;
  821. ctx->format_out.fmt.pix.width = 640;
  822. ctx->format_out.fmt.pix.height = 480;
  823. ctx->crop_out.width = 640;
  824. ctx->crop_out.height = 480;
  825. init_waitqueue_head(&ctx->wait_queue);
  826. spin_lock_init(&ctx->list_lock);
  827. INIT_LIST_HEAD(&ctx->pending_list);
  828. INIT_LIST_HEAD(&ctx->retired_list);
  829. for (i = 0 ; i < ARRAY_SIZE(ctx->requests); i++) {
  830. struct sde_rotator_request *request = &ctx->requests[i];
  831. kthread_init_work(&request->submit_work,
  832. sde_rotator_submit_handler);
  833. kthread_init_work(&request->retire_work,
  834. sde_rotator_retire_handler);
  835. request->ctx = ctx;
  836. INIT_LIST_HEAD(&request->list);
  837. list_add_tail(&request->list, &ctx->retired_list);
  838. }
  839. if (ctx->file) {
  840. v4l2_fh_init(&ctx->fh, video);
  841. file->private_data = &ctx->fh;
  842. v4l2_fh_add(&ctx->fh);
  843. ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(rot_dev->m2m_dev,
  844. ctx, sde_rotator_queue_init);
  845. if (IS_ERR_OR_NULL(ctx->fh.m2m_ctx)) {
  846. ret = PTR_ERR(ctx->fh.m2m_ctx);
  847. ctx->fh.m2m_ctx = NULL;
  848. goto error_m2m_init;
  849. }
  850. }
  851. ret = kobject_init_and_add(&ctx->kobj, &sde_rotator_fs_ktype,
  852. &rot_dev->dev->kobj, "session_%d", ctx->session_id);
  853. if (ret) {
  854. SDEDEV_ERR(ctx->rot_dev->dev,
  855. "fail initialize context kobject\n");
  856. goto error_kobj_init;
  857. }
  858. ret = sysfs_create_group(&ctx->kobj, &sde_rotator_fs_attr_group);
  859. if (ret) {
  860. SDEDEV_ERR(ctx->rot_dev->dev,
  861. "fail register rotator sysfs nodes\n");
  862. goto error_create_sysfs;
  863. }
  864. for (i = 0; i < MAX_ROT_OPEN_SESSION; i++) {
  865. if (rot_dev->kthread_free[i]) {
  866. rot_dev->kthread_free[i] = false;
  867. ctx->kthread_id = i;
  868. ctx->work_queue.rot_kw = &rot_dev->rot_kw[i];
  869. ctx->work_queue.rot_thread = rot_dev->rot_thread[i];
  870. break;
  871. }
  872. }
  873. if (ctx->kthread_id < 0) {
  874. SDEDEV_ERR(ctx->rot_dev->dev,
  875. "fail to acquire the kthread\n");
  876. ret = -EINVAL;
  877. goto error_alloc_kthread;
  878. }
  879. snprintf(name, sizeof(name), "%d_%d", rot_dev->dev->id,
  880. ctx->session_id);
  881. ctx->work_queue.timeline = sde_rotator_create_timeline(name);
  882. if (!ctx->work_queue.timeline)
  883. SDEDEV_DBG(ctx->rot_dev->dev, "timeline is not available\n");
  884. sde_rot_mgr_lock(rot_dev->mgr);
  885. ret = sde_rotator_session_open(rot_dev->mgr, &ctx->private,
  886. ctx->session_id, &ctx->work_queue);
  887. if (ret < 0) {
  888. SDEDEV_ERR(ctx->rot_dev->dev, "fail open session\n");
  889. goto error_open_session;
  890. }
  891. sde_rot_mgr_unlock(rot_dev->mgr);
  892. if (ctx->file) {
  893. /* Create control */
  894. ctrl_handler = &ctx->ctrl_handler;
  895. v4l2_ctrl_handler_init(ctrl_handler, 4);
  896. v4l2_ctrl_new_std(ctrl_handler,
  897. &sde_rotator_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
  898. v4l2_ctrl_new_std(ctrl_handler,
  899. &sde_rotator_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
  900. v4l2_ctrl_new_std(ctrl_handler,
  901. &sde_rotator_ctrl_ops, V4L2_CID_ROTATE, 0, 270, 90, 0);
  902. v4l2_ctrl_new_custom(ctrl_handler,
  903. &sde_rotator_ctrl_secure, NULL);
  904. v4l2_ctrl_new_custom(ctrl_handler,
  905. &sde_rotator_ctrl_secure_camera, NULL);
  906. if (ctrl_handler->error) {
  907. ret = ctrl_handler->error;
  908. v4l2_ctrl_handler_free(ctrl_handler);
  909. goto error_ctrl_handler;
  910. }
  911. ctx->fh.ctrl_handler = ctrl_handler;
  912. v4l2_ctrl_handler_setup(ctrl_handler);
  913. } else {
  914. /* acquire exclusive context */
  915. SDEDEV_DBG(rot_dev->dev, "acquire exclusive session id:%u\n",
  916. ctx->session_id);
  917. SDEROT_EVTLOG(ctx->session_id);
  918. rot_dev->excl_ctx = ctx;
  919. }
  920. mutex_unlock(&rot_dev->lock);
  921. SDEDEV_DBG(ctx->rot_dev->dev, "SDE v4l2 rotator open success\n");
  922. ATRACE_BEGIN(ctx->kobj.name);
  923. return ctx;
  924. error_ctrl_handler:
  925. error_open_session:
  926. sde_rot_mgr_unlock(rot_dev->mgr);
  927. sde_rotator_destroy_timeline(ctx->work_queue.timeline);
  928. rot_dev->kthread_free[ctx->kthread_id] = true;
  929. ctx->kthread_id = -1;
  930. error_alloc_kthread:
  931. sysfs_remove_group(&ctx->kobj, &sde_rotator_fs_attr_group);
  932. error_create_sysfs:
  933. kobject_put(&ctx->kobj);
  934. error_kobj_init:
  935. if (ctx->file) {
  936. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  937. ctx->fh.m2m_ctx = NULL;
  938. }
  939. error_m2m_init:
  940. if (ctx->file) {
  941. v4l2_fh_del(&ctx->fh);
  942. v4l2_fh_exit(&ctx->fh);
  943. }
  944. mutex_unlock(&rot_dev->lock);
  945. error_lock:
  946. kfree(ctx);
  947. ctx = NULL;
  948. return ERR_PTR(ret);
  949. }
  950. /*
  951. * sde_rotator_ctx_release - Rotator device release method.
  952. * @ctx: Pointer rotator context.
  953. * @file: Pointer to file struct (optional)
  954. * return: 0 if success; error code, otherwise
  955. */
  956. static int sde_rotator_ctx_release(struct sde_rotator_ctx *ctx,
  957. struct file *file)
  958. {
  959. struct sde_rotator_device *rot_dev;
  960. u32 session_id;
  961. struct list_head *curr, *next;
  962. if (!ctx) {
  963. SDEROT_DBG("ctx is NULL\n");
  964. return -EINVAL;
  965. }
  966. rot_dev = ctx->rot_dev;
  967. session_id = ctx->session_id;
  968. ATRACE_END(ctx->kobj.name);
  969. SDEDEV_DBG(rot_dev->dev, "release s:%d\n", session_id);
  970. mutex_lock(&rot_dev->lock);
  971. if (rot_dev->excl_ctx == ctx) {
  972. SDEDEV_DBG(rot_dev->dev, "release exclusive session id:%u\n",
  973. session_id);
  974. SDEROT_EVTLOG(session_id);
  975. rot_dev->excl_ctx = NULL;
  976. }
  977. if (ctx->file) {
  978. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  979. SDEDEV_DBG(rot_dev->dev, "release streams s:%d\n", session_id);
  980. if (ctx->fh.m2m_ctx) {
  981. v4l2_m2m_streamoff(file, ctx->fh.m2m_ctx,
  982. V4L2_BUF_TYPE_VIDEO_OUTPUT);
  983. v4l2_m2m_streamoff(file, ctx->fh.m2m_ctx,
  984. V4L2_BUF_TYPE_VIDEO_CAPTURE);
  985. }
  986. }
  987. mutex_unlock(&rot_dev->lock);
  988. SDEDEV_DBG(rot_dev->dev, "release submit work s:%d\n", session_id);
  989. list_for_each_safe(curr, next, &ctx->pending_list) {
  990. struct sde_rotator_request *request =
  991. container_of(curr, struct sde_rotator_request, list);
  992. SDEDEV_DBG(rot_dev->dev, "release submit work s:%d\n",
  993. session_id);
  994. kthread_cancel_work_sync(&request->submit_work);
  995. }
  996. SDEDEV_DBG(rot_dev->dev, "release session s:%d\n", session_id);
  997. sde_rot_mgr_lock(rot_dev->mgr);
  998. sde_rotator_session_close(rot_dev->mgr, ctx->private, session_id);
  999. sde_rot_mgr_unlock(rot_dev->mgr);
  1000. SDEDEV_DBG(rot_dev->dev, "release retire work s:%d\n", session_id);
  1001. list_for_each_safe(curr, next, &ctx->pending_list) {
  1002. struct sde_rotator_request *request =
  1003. container_of(curr, struct sde_rotator_request, list);
  1004. SDEDEV_DBG(rot_dev->dev, "release retire work s:%d\n",
  1005. session_id);
  1006. kthread_cancel_work_sync(&request->retire_work);
  1007. }
  1008. mutex_lock(&rot_dev->lock);
  1009. SDEDEV_DBG(rot_dev->dev, "release context s:%d\n", session_id);
  1010. sde_rotator_destroy_timeline(ctx->work_queue.timeline);
  1011. if (ctx->kthread_id >= 0 && ctx->work_queue.rot_kw) {
  1012. kthread_flush_worker(ctx->work_queue.rot_kw);
  1013. rot_dev->kthread_free[ctx->kthread_id] = true;
  1014. }
  1015. sysfs_remove_group(&ctx->kobj, &sde_rotator_fs_attr_group);
  1016. kobject_put(&ctx->kobj);
  1017. if (ctx->file) {
  1018. if (ctx->fh.m2m_ctx)
  1019. v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  1020. if (ctx->fh.vdev) {
  1021. v4l2_fh_del(&ctx->fh);
  1022. v4l2_fh_exit(&ctx->fh);
  1023. }
  1024. }
  1025. kfree(ctx->vbinfo_out);
  1026. kfree(ctx->vbinfo_cap);
  1027. kfree(ctx);
  1028. wake_up_interruptible(&rot_dev->open_wq);
  1029. mutex_unlock(&rot_dev->lock);
  1030. SDEDEV_DBG(rot_dev->dev, "release complete s:%d\n", session_id);
  1031. return 0;
  1032. }
  1033. /*
  1034. * sde_rotator_update_retire_sequence - update retired sequence of the context
  1035. * referenced in the request, and wake up any waiting for update event
  1036. * @request: Pointer to rotator request
  1037. */
  1038. static void sde_rotator_update_retire_sequence(
  1039. struct sde_rotator_request *request)
  1040. {
  1041. struct sde_rotator_ctx *ctx;
  1042. if (!request || !request->ctx) {
  1043. SDEROT_ERR("invalid parameters\n");
  1044. return;
  1045. }
  1046. ctx = request->ctx;
  1047. ctx->retired_sequence_id = request->sequence_id;
  1048. wake_up(&ctx->wait_queue);
  1049. SDEROT_DBG("update sequence s:%d.%d\n",
  1050. ctx->session_id, ctx->retired_sequence_id);
  1051. }
  1052. /*
  1053. * sde_rotator_retire_request - retire the given rotator request with
  1054. * device mutex locked
  1055. * @request: Pointer to rotator request
  1056. */
  1057. static void sde_rotator_retire_request(struct sde_rotator_request *request)
  1058. {
  1059. struct sde_rotator_ctx *ctx;
  1060. if (!request || !request->ctx) {
  1061. SDEROT_ERR("invalid parameters\n");
  1062. return;
  1063. }
  1064. ctx = request->ctx;
  1065. request->req = NULL;
  1066. request->sequence_id = 0;
  1067. request->committed = false;
  1068. spin_lock(&ctx->list_lock);
  1069. list_del_init(&request->list);
  1070. list_add_tail(&request->list, &ctx->retired_list);
  1071. spin_unlock(&ctx->list_lock);
  1072. wake_up(&ctx->wait_queue);
  1073. SDEROT_DBG("retire request s:%d.%d\n",
  1074. ctx->session_id, ctx->retired_sequence_id);
  1075. }
  1076. /*
  1077. * sde_rotator_is_request_retired - Return true if given request already expired
  1078. * @request: Pointer to rotator request
  1079. */
  1080. static bool sde_rotator_is_request_retired(struct sde_rotator_request *request)
  1081. {
  1082. struct sde_rotator_ctx *ctx;
  1083. u32 sequence_id;
  1084. s32 retire_delta;
  1085. if (!request || !request->ctx)
  1086. return true;
  1087. ctx = request->ctx;
  1088. sequence_id = request->sequence_id;
  1089. retire_delta = (s32) (ctx->retired_sequence_id - sequence_id);
  1090. SDEROT_DBG("sequence:%u/%u\n", sequence_id, ctx->retired_sequence_id);
  1091. return retire_delta >= 0;
  1092. }
  1093. /*
  1094. * sde_rotator_inline_open - open inline rotator session
  1095. * @pdev: Pointer to rotator platform device
  1096. * @video_mode: true if video mode is requested
  1097. * return: Pointer to new rotator session context
  1098. */
  1099. void *sde_rotator_inline_open(struct platform_device *pdev)
  1100. {
  1101. struct sde_rotator_device *rot_dev;
  1102. struct sde_rotator_ctx *ctx;
  1103. int rc;
  1104. if (!pdev) {
  1105. SDEROT_ERR("invalid platform device\n");
  1106. return ERR_PTR(-EINVAL);
  1107. }
  1108. rot_dev = (struct sde_rotator_device *) platform_get_drvdata(pdev);
  1109. if (!rot_dev) {
  1110. SDEROT_ERR("invalid rotator device\n");
  1111. return ERR_PTR(-EINVAL);
  1112. }
  1113. ctx = sde_rotator_ctx_open(rot_dev, NULL);
  1114. if (IS_ERR_OR_NULL(ctx)) {
  1115. rc = PTR_ERR(ctx);
  1116. SDEROT_ERR("failed to open rotator context %d\n", rc);
  1117. goto rotator_open_error;
  1118. }
  1119. ctx->slice = llcc_slice_getd(LLCC_ROTATOR);
  1120. if (IS_ERR(ctx->slice)) {
  1121. rc = PTR_ERR(ctx->slice);
  1122. SDEROT_ERR("failed to get system cache %d\n", rc);
  1123. goto slice_getd_error;
  1124. }
  1125. if (!rot_dev->disable_syscache) {
  1126. rc = llcc_slice_activate(ctx->slice);
  1127. if (rc) {
  1128. SDEROT_ERR("failed to activate slice %d\n", rc);
  1129. goto activate_error;
  1130. }
  1131. SDEROT_DBG("scid %d size %zukb\n",
  1132. llcc_get_slice_id(ctx->slice),
  1133. llcc_get_slice_size(ctx->slice));
  1134. } else {
  1135. SDEROT_DBG("syscache bypassed\n");
  1136. }
  1137. SDEROT_EVTLOG(ctx->session_id, llcc_get_slice_id(ctx->slice),
  1138. llcc_get_slice_size(ctx->slice),
  1139. rot_dev->disable_syscache);
  1140. return ctx;
  1141. activate_error:
  1142. llcc_slice_putd(ctx->slice);
  1143. ctx->slice = NULL;
  1144. slice_getd_error:
  1145. sde_rotator_ctx_release(ctx, NULL);
  1146. rotator_open_error:
  1147. return ERR_PTR(rc);
  1148. }
  1149. EXPORT_SYMBOL(sde_rotator_inline_open);
  1150. int sde_rotator_inline_release(void *handle)
  1151. {
  1152. struct sde_rotator_device *rot_dev;
  1153. struct sde_rotator_ctx *ctx;
  1154. if (!handle) {
  1155. SDEROT_ERR("invalid rotator ctx\n");
  1156. return -EINVAL;
  1157. }
  1158. ctx = handle;
  1159. rot_dev = ctx->rot_dev;
  1160. if (!rot_dev) {
  1161. SDEROT_ERR("invalid rotator device\n");
  1162. return -EINVAL;
  1163. }
  1164. if (ctx->slice) {
  1165. if (!rot_dev->disable_syscache)
  1166. llcc_slice_deactivate(ctx->slice);
  1167. llcc_slice_putd(ctx->slice);
  1168. ctx->slice = NULL;
  1169. }
  1170. SDEROT_EVTLOG(ctx->session_id);
  1171. return sde_rotator_ctx_release(ctx, NULL);
  1172. }
  1173. EXPORT_SYMBOL(sde_rotator_inline_release);
  1174. /*
  1175. * sde_rotator_inline_get_dst_pixfmt - determine output pixel format
  1176. * @pdev: Pointer to platform device
  1177. * @src_pixfmt: input pixel format
  1178. * @dst_pixfmt: Pointer to output pixel format (output)
  1179. * return: 0 if success; error code otherwise
  1180. */
  1181. int sde_rotator_inline_get_dst_pixfmt(struct platform_device *pdev,
  1182. u32 src_pixfmt, u32 *dst_pixfmt)
  1183. {
  1184. int rc;
  1185. if (!src_pixfmt || !dst_pixfmt)
  1186. return -EINVAL;
  1187. rc = sde_rot_get_base_tilea5x_pixfmt(src_pixfmt, dst_pixfmt);
  1188. if (rc)
  1189. return rc;
  1190. /*
  1191. * Currently, NV21 tile is not supported as output; hence,
  1192. * override with NV12 tile.
  1193. */
  1194. if (*dst_pixfmt == SDE_PIX_FMT_Y_CRCB_H2V2_TILE)
  1195. *dst_pixfmt = SDE_PIX_FMT_Y_CBCR_H2V2_TILE;
  1196. return 0;
  1197. }
  1198. EXPORT_SYMBOL(sde_rotator_inline_get_dst_pixfmt);
  1199. /*
  1200. * sde_rotator_inline_get_downscale_caps - get scaling capability
  1201. * @pdev: Pointer to platform device
  1202. * @caps: string buffer for capability
  1203. * @len: length of string buffer
  1204. * return: length of capability string
  1205. */
  1206. int sde_rotator_inline_get_downscale_caps(struct platform_device *pdev,
  1207. char *caps, int len)
  1208. {
  1209. struct sde_rotator_device *rot_dev;
  1210. int rc;
  1211. if (!pdev) {
  1212. SDEROT_ERR("invalid platform device\n");
  1213. return -EINVAL;
  1214. }
  1215. rot_dev = (struct sde_rotator_device *) platform_get_drvdata(pdev);
  1216. if (!rot_dev || !rot_dev->mgr) {
  1217. SDEROT_ERR("invalid rotator device\n");
  1218. return -EINVAL;
  1219. }
  1220. sde_rot_mgr_lock(rot_dev->mgr);
  1221. rc = sde_rotator_get_downscale_caps(rot_dev->mgr, caps, len);
  1222. sde_rot_mgr_unlock(rot_dev->mgr);
  1223. return rc;
  1224. }
  1225. EXPORT_SYMBOL(sde_rotator_inline_get_downscale_caps);
  1226. /*
  1227. * sde_rotator_inline_get_maxlinewidth - get maximum line width of rotator
  1228. * @pdev: Pointer to platform device
  1229. * return: maximum line width
  1230. */
  1231. int sde_rotator_inline_get_maxlinewidth(struct platform_device *pdev)
  1232. {
  1233. struct sde_rotator_device *rot_dev;
  1234. int maxlinewidth;
  1235. if (!pdev) {
  1236. SDEROT_ERR("invalid platform device\n");
  1237. return -EINVAL;
  1238. }
  1239. rot_dev = (struct sde_rotator_device *)platform_get_drvdata(pdev);
  1240. if (!rot_dev || !rot_dev->mgr) {
  1241. SDEROT_ERR("invalid rotator device\n");
  1242. return -EINVAL;
  1243. }
  1244. sde_rot_mgr_lock(rot_dev->mgr);
  1245. maxlinewidth = sde_rotator_get_maxlinewidth(rot_dev->mgr);
  1246. sde_rot_mgr_unlock(rot_dev->mgr);
  1247. return maxlinewidth;
  1248. }
  1249. EXPORT_SYMBOL(sde_rotator_inline_get_maxlinewidth);
  1250. /*
  1251. * sde_rotator_inline_get_pixfmt_caps - get pixel format capability
  1252. * @pdev: Pointer to platform device
  1253. * @pixfmt: array of pixel format buffer
  1254. * @len: length of pixel format buffer
  1255. * return: length of pixel format capability if success; error code otherwise
  1256. */
  1257. int sde_rotator_inline_get_pixfmt_caps(struct platform_device *pdev,
  1258. bool input, u32 *pixfmts, int len)
  1259. {
  1260. struct sde_rotator_device *rot_dev;
  1261. u32 i, pixfmt;
  1262. if (!pdev) {
  1263. SDEROT_ERR("invalid platform device\n");
  1264. return -EINVAL;
  1265. }
  1266. rot_dev = (struct sde_rotator_device *) platform_get_drvdata(pdev);
  1267. if (!rot_dev || !rot_dev->mgr) {
  1268. SDEROT_ERR("invalid rotator device\n");
  1269. return -EINVAL;
  1270. }
  1271. sde_rot_mgr_lock(rot_dev->mgr);
  1272. for (i = 0;; i++) {
  1273. pixfmt = sde_rotator_get_pixfmt(rot_dev->mgr, i, input,
  1274. SDE_ROTATOR_MODE_SBUF);
  1275. if (!pixfmt)
  1276. break;
  1277. if (pixfmts && i < len)
  1278. pixfmts[i] = pixfmt;
  1279. }
  1280. sde_rot_mgr_unlock(rot_dev->mgr);
  1281. return i;
  1282. }
  1283. EXPORT_SYMBOL(sde_rotator_inline_get_pixfmt_caps);
  1284. /*
  1285. * _sde_rotator_inline_cleanup - perform inline related request cleanup
  1286. * This function assumes rot_dev->mgr lock has been taken when called.
  1287. * @handle: Pointer to rotator context
  1288. * @request: Pointer to rotation request
  1289. * return: 0 if success; -EAGAIN if cleanup should be retried
  1290. */
  1291. static int _sde_rotator_inline_cleanup(void *handle,
  1292. struct sde_rotator_request *request)
  1293. {
  1294. struct sde_rotator_ctx *ctx;
  1295. struct sde_rotator_device *rot_dev;
  1296. int ret;
  1297. if (!handle || !request) {
  1298. SDEROT_ERR("invalid rotator handle/request\n");
  1299. return -EINVAL;
  1300. }
  1301. ctx = handle;
  1302. rot_dev = ctx->rot_dev;
  1303. if (!rot_dev || !rot_dev->mgr) {
  1304. SDEROT_ERR("invalid rotator device\n");
  1305. return -EINVAL;
  1306. }
  1307. if (request->committed) {
  1308. /* wait until request is finished */
  1309. sde_rot_mgr_unlock(rot_dev->mgr);
  1310. mutex_unlock(&rot_dev->lock);
  1311. ret = wait_event_timeout(ctx->wait_queue,
  1312. sde_rotator_is_request_retired(request),
  1313. msecs_to_jiffies(rot_dev->streamoff_timeout));
  1314. mutex_lock(&rot_dev->lock);
  1315. sde_rot_mgr_lock(rot_dev->mgr);
  1316. if (!ret) {
  1317. SDEROT_ERR("timeout w/o retire s:%d\n",
  1318. ctx->session_id);
  1319. SDEROT_EVTLOG(ctx->session_id, SDE_ROT_EVTLOG_ERROR);
  1320. sde_rotator_abort_inline_request(rot_dev->mgr,
  1321. ctx->private, request->req);
  1322. return -EAGAIN;
  1323. } else if (ret == 1) {
  1324. SDEROT_ERR("timeout w/ retire s:%d\n", ctx->session_id);
  1325. SDEROT_EVTLOG(ctx->session_id, SDE_ROT_EVTLOG_ERROR);
  1326. }
  1327. }
  1328. sde_rotator_req_finish(rot_dev->mgr, ctx->private, request->req);
  1329. sde_rotator_retire_request(request);
  1330. return 0;
  1331. }
  1332. /*
  1333. * sde_rotator_inline_commit - commit given rotator command
  1334. * @handle: Pointer to rotator context
  1335. * @cmd: Pointer to rotator command
  1336. * @cmd_type: command type - validate/prepare/commit/cleanup
  1337. * return: 0 if success; error code otherwise
  1338. */
  1339. int sde_rotator_inline_commit(void *handle, struct sde_rotator_inline_cmd *cmd,
  1340. enum sde_rotator_inline_cmd_type cmd_type)
  1341. {
  1342. struct sde_rotator_ctx *ctx;
  1343. struct sde_rotator_device *rot_dev;
  1344. struct sde_rotator_request *request = NULL;
  1345. struct sde_rot_entry_container *req = NULL;
  1346. struct sde_rotation_config rotcfg;
  1347. struct sde_rot_trace_entry rot_trace;
  1348. ktime_t *ts;
  1349. u32 flags = 0;
  1350. int i, ret = 0;
  1351. if (!handle || !cmd) {
  1352. SDEROT_ERR("invalid rotator handle/cmd\n");
  1353. return -EINVAL;
  1354. }
  1355. ctx = handle;
  1356. rot_dev = ctx->rot_dev;
  1357. if (!rot_dev || !rot_dev->mgr) {
  1358. SDEROT_ERR("invalid rotator device\n");
  1359. return -EINVAL;
  1360. }
  1361. SDEROT_DBG(
  1362. "s:%d.%u src:(%u,%u,%u,%u)/%ux%u/%c%c%c%c dst:(%u,%u,%u,%u)/%c%c%c%c r:%d f:%d/%d s:%d fps:%u clk:%llu bw:%llu prefill:%llu wb:%d vid:%d cmd:%d\n",
  1363. ctx->session_id, cmd->sequence_id,
  1364. cmd->src_rect_x, cmd->src_rect_y,
  1365. cmd->src_rect_w, cmd->src_rect_h,
  1366. cmd->src_width, cmd->src_height,
  1367. cmd->src_pixfmt >> 0, cmd->src_pixfmt >> 8,
  1368. cmd->src_pixfmt >> 16, cmd->src_pixfmt >> 24,
  1369. cmd->dst_rect_x, cmd->dst_rect_y,
  1370. cmd->dst_rect_w, cmd->dst_rect_h,
  1371. cmd->dst_pixfmt >> 0, cmd->dst_pixfmt >> 8,
  1372. cmd->dst_pixfmt >> 16, cmd->dst_pixfmt >> 24,
  1373. cmd->rot90, cmd->hflip, cmd->vflip, cmd->secure, cmd->fps,
  1374. cmd->clkrate, cmd->data_bw, cmd->prefill_bw,
  1375. cmd->dst_writeback, cmd->video_mode, cmd_type);
  1376. SDEROT_EVTLOG(ctx->session_id, cmd->sequence_id,
  1377. cmd->src_rect_x, cmd->src_rect_y,
  1378. cmd->src_rect_w, cmd->src_rect_h,
  1379. cmd->src_pixfmt,
  1380. cmd->dst_rect_w, cmd->dst_rect_h,
  1381. cmd->dst_pixfmt,
  1382. cmd->fps, cmd->clkrate, cmd->data_bw, cmd->prefill_bw,
  1383. (cmd->rot90 << 0) | (cmd->hflip << 1) | (cmd->vflip << 2) |
  1384. (cmd->secure << 3) | (cmd->dst_writeback << 4) |
  1385. (cmd->video_mode << 5) |
  1386. (cmd_type << 24));
  1387. mutex_lock(&rot_dev->lock);
  1388. sde_rot_mgr_lock(rot_dev->mgr);
  1389. if (cmd_type == SDE_ROTATOR_INLINE_CMD_VALIDATE ||
  1390. cmd_type == SDE_ROTATOR_INLINE_CMD_COMMIT) {
  1391. struct sde_rotation_item item;
  1392. struct sde_rotator_statistics *stats = &rot_dev->stats;
  1393. int scid = llcc_get_slice_id(ctx->slice);
  1394. /* allocate slot for timestamp */
  1395. ts = stats->ts[stats->count % SDE_ROTATOR_NUM_EVENTS];
  1396. if (cmd_type == SDE_ROTATOR_INLINE_CMD_COMMIT)
  1397. stats->count++;
  1398. if (cmd->rot90)
  1399. flags |= SDE_ROTATION_90;
  1400. if (cmd->hflip)
  1401. flags |= SDE_ROTATION_FLIP_LR;
  1402. if (cmd->vflip)
  1403. flags |= SDE_ROTATION_FLIP_UD;
  1404. if (cmd->secure)
  1405. flags |= SDE_ROTATION_SECURE;
  1406. flags |= SDE_ROTATION_EXT_PERF;
  1407. /* fill in item work structure */
  1408. memset(&item, 0, sizeof(struct sde_rotation_item));
  1409. item.flags = flags | SDE_ROTATION_EXT_IOVA;
  1410. item.trigger = cmd->video_mode ? SDE_ROTATOR_TRIGGER_VIDEO :
  1411. SDE_ROTATOR_TRIGGER_COMMAND;
  1412. item.prefill_bw = cmd->prefill_bw;
  1413. item.session_id = ctx->session_id;
  1414. item.sequence_id = cmd->sequence_id;
  1415. item.src_rect.x = cmd->src_rect_x;
  1416. item.src_rect.y = cmd->src_rect_y;
  1417. item.src_rect.w = cmd->src_rect_w;
  1418. item.src_rect.h = cmd->src_rect_h;
  1419. item.input.width = cmd->src_width;
  1420. item.input.height = cmd->src_height;
  1421. item.input.format = cmd->src_pixfmt;
  1422. for (i = 0; i < SDE_ROTATOR_INLINE_PLANE_MAX; i++) {
  1423. item.input.planes[i].addr = cmd->src_addr[i];
  1424. item.input.planes[i].len = cmd->src_len[i];
  1425. item.input.planes[i].fd = -1;
  1426. }
  1427. item.input.plane_count = cmd->src_planes;
  1428. item.input.comp_ratio.numer = 1;
  1429. item.input.comp_ratio.denom = 1;
  1430. item.output.width = cmd->dst_rect_x + cmd->dst_rect_w;
  1431. item.output.height = cmd->dst_rect_y + cmd->dst_rect_h;
  1432. item.dst_rect.x = cmd->dst_rect_x;
  1433. item.dst_rect.y = cmd->dst_rect_y;
  1434. item.dst_rect.w = cmd->dst_rect_w;
  1435. item.dst_rect.h = cmd->dst_rect_h;
  1436. item.output.sbuf = true;
  1437. item.output.scid = scid;
  1438. item.output.writeback = cmd->dst_writeback;
  1439. item.output.format = cmd->dst_pixfmt;
  1440. for (i = 0; i < SDE_ROTATOR_INLINE_PLANE_MAX; i++) {
  1441. item.output.planes[i].addr = cmd->dst_addr[i];
  1442. item.output.planes[i].len = cmd->dst_len[i];
  1443. item.output.planes[i].fd = -1;
  1444. }
  1445. item.output.plane_count = cmd->dst_planes;
  1446. item.output.comp_ratio.numer = 1;
  1447. item.output.comp_ratio.denom = 1;
  1448. item.sequence_id = ++(ctx->commit_sequence_id);
  1449. item.ts = ts;
  1450. req = sde_rotator_req_init(rot_dev->mgr, ctx->private,
  1451. &item, 1, 0);
  1452. if (IS_ERR_OR_NULL(req)) {
  1453. SDEROT_ERR("fail allocate request s:%d\n",
  1454. ctx->session_id);
  1455. ret = -ENOMEM;
  1456. goto error_init_request;
  1457. }
  1458. /* initialize session configuration */
  1459. memset(&rotcfg, 0, sizeof(struct sde_rotation_config));
  1460. rotcfg.flags = flags;
  1461. rotcfg.frame_rate = cmd->fps;
  1462. rotcfg.clk_rate = cmd->clkrate;
  1463. rotcfg.data_bw = cmd->data_bw;
  1464. rotcfg.session_id = ctx->session_id;
  1465. rotcfg.input.width = cmd->src_rect_w;
  1466. rotcfg.input.height = cmd->src_rect_h;
  1467. rotcfg.input.format = cmd->src_pixfmt;
  1468. rotcfg.input.comp_ratio.numer = 1;
  1469. rotcfg.input.comp_ratio.denom = 1;
  1470. rotcfg.output.width = cmd->dst_rect_w;
  1471. rotcfg.output.height = cmd->dst_rect_h;
  1472. rotcfg.output.format = cmd->dst_pixfmt;
  1473. rotcfg.output.comp_ratio.numer = 1;
  1474. rotcfg.output.comp_ratio.denom = 1;
  1475. rotcfg.output.sbuf = true;
  1476. }
  1477. if (cmd_type == SDE_ROTATOR_INLINE_CMD_VALIDATE) {
  1478. ret = sde_rotator_session_validate(rot_dev->mgr,
  1479. ctx->private, &rotcfg);
  1480. if (ret) {
  1481. SDEROT_WARN("fail session validation s:%d\n",
  1482. ctx->session_id);
  1483. goto error_session_validate;
  1484. }
  1485. devm_kfree(rot_dev->dev, req);
  1486. req = NULL;
  1487. } else if (cmd_type == SDE_ROTATOR_INLINE_CMD_COMMIT) {
  1488. if (memcmp(&rotcfg, &ctx->rotcfg, sizeof(rotcfg))) {
  1489. ret = sde_rotator_session_config(rot_dev->mgr,
  1490. ctx->private, &rotcfg);
  1491. if (ret) {
  1492. SDEROT_ERR("fail session config s:%d\n",
  1493. ctx->session_id);
  1494. goto error_session_config;
  1495. }
  1496. ctx->rotcfg = rotcfg;
  1497. }
  1498. request = list_first_entry_or_null(&ctx->retired_list,
  1499. struct sde_rotator_request, list);
  1500. if (!request) {
  1501. /* should not happen */
  1502. ret = -ENOMEM;
  1503. SDEROT_ERR("no free request s:%d\n", ctx->session_id);
  1504. goto error_retired_list;
  1505. }
  1506. request->req = req;
  1507. request->sequence_id = req->entries[0].item.sequence_id;
  1508. spin_lock(&ctx->list_lock);
  1509. list_del_init(&request->list);
  1510. list_add_tail(&request->list, &ctx->pending_list);
  1511. spin_unlock(&ctx->list_lock);
  1512. ts = req->entries[0].item.ts;
  1513. if (ts) {
  1514. ts[SDE_ROTATOR_TS_SRCQB] = ktime_get();
  1515. ts[SDE_ROTATOR_TS_DSTQB] = ktime_get();
  1516. ts[SDE_ROTATOR_TS_FENCE] = ktime_get();
  1517. } else {
  1518. SDEROT_ERR("invalid stats timestamp\n");
  1519. }
  1520. req->retire_kw = ctx->work_queue.rot_kw;
  1521. req->retire_work = &request->retire_work;
  1522. /* Set values to pass to trace */
  1523. rot_trace.wb_idx = req->entries[0].item.wb_idx;
  1524. rot_trace.flags = req->entries[0].item.flags;
  1525. rot_trace.input_format = req->entries[0].item.input.format;
  1526. rot_trace.input_width = req->entries[0].item.input.width;
  1527. rot_trace.input_height = req->entries[0].item.input.height;
  1528. rot_trace.src_x = req->entries[0].item.src_rect.x;
  1529. rot_trace.src_y = req->entries[0].item.src_rect.y;
  1530. rot_trace.src_w = req->entries[0].item.src_rect.w;
  1531. rot_trace.src_h = req->entries[0].item.src_rect.h;
  1532. rot_trace.output_format = req->entries[0].item.output.format;
  1533. rot_trace.output_width = req->entries[0].item.output.width;
  1534. rot_trace.output_height = req->entries[0].item.output.height;
  1535. rot_trace.dst_x = req->entries[0].item.dst_rect.x;
  1536. rot_trace.dst_y = req->entries[0].item.dst_rect.y;
  1537. rot_trace.dst_w = req->entries[0].item.dst_rect.w;
  1538. rot_trace.dst_h = req->entries[0].item.dst_rect.h;
  1539. trace_rot_entry_fence(
  1540. ctx->session_id, cmd->sequence_id, &rot_trace);
  1541. ret = sde_rotator_handle_request_common(
  1542. rot_dev->mgr, ctx->private, req);
  1543. if (ret) {
  1544. SDEROT_ERR("fail handle request s:%d\n",
  1545. ctx->session_id);
  1546. goto error_handle_request;
  1547. }
  1548. sde_rotator_req_reset_start(rot_dev->mgr, req);
  1549. sde_rotator_queue_request(rot_dev->mgr, ctx->private, req);
  1550. request->committed = true;
  1551. /* save request in private handle */
  1552. cmd->priv_handle = request;
  1553. } else if (cmd_type == SDE_ROTATOR_INLINE_CMD_START) {
  1554. if (!cmd->priv_handle) {
  1555. ret = -EINVAL;
  1556. SDEROT_ERR("invalid private handle\n");
  1557. goto error_invalid_handle;
  1558. }
  1559. request = cmd->priv_handle;
  1560. sde_rotator_req_set_start(rot_dev->mgr, request->req);
  1561. } else if (cmd_type == SDE_ROTATOR_INLINE_CMD_CLEANUP) {
  1562. if (!cmd->priv_handle) {
  1563. ret = -EINVAL;
  1564. SDEROT_ERR("invalid private handle\n");
  1565. goto error_invalid_handle;
  1566. }
  1567. request = cmd->priv_handle;
  1568. /* attempt single retry if first cleanup attempt failed */
  1569. if (_sde_rotator_inline_cleanup(handle, request) == -EAGAIN)
  1570. _sde_rotator_inline_cleanup(handle, request);
  1571. cmd->priv_handle = NULL;
  1572. } else if (cmd_type == SDE_ROTATOR_INLINE_CMD_ABORT) {
  1573. if (!cmd->priv_handle) {
  1574. ret = -EINVAL;
  1575. SDEROT_ERR("invalid private handle\n");
  1576. goto error_invalid_handle;
  1577. }
  1578. request = cmd->priv_handle;
  1579. if (!sde_rotator_is_request_retired(request))
  1580. sde_rotator_abort_inline_request(rot_dev->mgr,
  1581. ctx->private, request->req);
  1582. }
  1583. sde_rot_mgr_unlock(rot_dev->mgr);
  1584. mutex_unlock(&rot_dev->lock);
  1585. return 0;
  1586. error_handle_request:
  1587. sde_rotator_update_retire_sequence(request);
  1588. sde_rotator_retire_request(request);
  1589. error_retired_list:
  1590. error_session_validate:
  1591. error_session_config:
  1592. devm_kfree(rot_dev->dev, req);
  1593. error_invalid_handle:
  1594. error_init_request:
  1595. sde_rot_mgr_unlock(rot_dev->mgr);
  1596. mutex_unlock(&rot_dev->lock);
  1597. return ret;
  1598. }
  1599. EXPORT_SYMBOL(sde_rotator_inline_commit);
  1600. void sde_rotator_inline_reg_dump(struct platform_device *pdev)
  1601. {
  1602. struct sde_rotator_device *rot_dev;
  1603. if (!pdev) {
  1604. SDEROT_ERR("invalid platform device\n");
  1605. return;
  1606. }
  1607. rot_dev = (struct sde_rotator_device *) platform_get_drvdata(pdev);
  1608. if (!rot_dev || !rot_dev->mgr) {
  1609. SDEROT_ERR("invalid rotator device\n");
  1610. return;
  1611. }
  1612. sde_rot_mgr_lock(rot_dev->mgr);
  1613. sde_rotator_core_dump(rot_dev->mgr);
  1614. sde_rot_mgr_unlock(rot_dev->mgr);
  1615. }
  1616. EXPORT_SYMBOL(sde_rotator_inline_reg_dump);
  1617. /*
  1618. * sde_rotator_open - Rotator device open method.
  1619. * @file: Pointer to file struct.
  1620. */
  1621. static int sde_rotator_open(struct file *file)
  1622. {
  1623. struct sde_rotator_device *rot_dev = video_drvdata(file);
  1624. struct sde_rotator_ctx *ctx;
  1625. int ret = 0;
  1626. ctx = sde_rotator_ctx_open(rot_dev, file);
  1627. if (IS_ERR_OR_NULL(ctx)) {
  1628. SDEDEV_DBG(rot_dev->dev, "failed to open %d\n", ret);
  1629. ret = PTR_ERR(ctx);
  1630. }
  1631. return ret;
  1632. }
  1633. /*
  1634. * sde_rotator_release - Rotator device release method.
  1635. * @file: Pointer to file struct.
  1636. */
  1637. static int sde_rotator_release(struct file *file)
  1638. {
  1639. struct sde_rotator_ctx *ctx =
  1640. sde_rotator_ctx_from_fh(file->private_data);
  1641. return sde_rotator_ctx_release(ctx, file);
  1642. }
  1643. /*
  1644. * sde_rotator_poll - rotator device pool method.
  1645. * @file: Pointer to file struct.
  1646. * @wait: Pointer to poll table struct.
  1647. */
  1648. static unsigned int sde_rotator_poll(struct file *file,
  1649. struct poll_table_struct *wait)
  1650. {
  1651. struct sde_rotator_device *rot_dev = video_drvdata(file);
  1652. struct sde_rotator_ctx *ctx =
  1653. sde_rotator_ctx_from_fh(file->private_data);
  1654. int ret;
  1655. mutex_lock(&rot_dev->lock);
  1656. ret = v4l2_m2m_poll(file, ctx->fh.m2m_ctx, wait);
  1657. mutex_unlock(&rot_dev->lock);
  1658. return ret;
  1659. }
  1660. /* rotator device file operations callbacks */
  1661. static const struct v4l2_file_operations sde_rotator_fops = {
  1662. .owner = THIS_MODULE,
  1663. .open = sde_rotator_open,
  1664. .release = sde_rotator_release,
  1665. .poll = sde_rotator_poll,
  1666. .unlocked_ioctl = video_ioctl2,
  1667. #ifdef CONFIG_COMPAT
  1668. .compat_ioctl32 = sde_rotator_compat_ioctl32,
  1669. #endif
  1670. };
  1671. /*
  1672. * sde_rotator_querycap - V4l2 ioctl query capability handler.
  1673. * @file: Pointer to file struct.
  1674. * @fh: V4l2 File handle.
  1675. * @cap: Pointer to v4l2_capability struct need to be filled.
  1676. */
  1677. static int sde_rotator_querycap(struct file *file,
  1678. void *fh, struct v4l2_capability *cap)
  1679. {
  1680. cap->bus_info[0] = 0;
  1681. strlcpy(cap->driver, SDE_ROTATOR_DRV_NAME, sizeof(cap->driver));
  1682. strlcpy(cap->card, SDE_ROTATOR_DRV_NAME, sizeof(cap->card));
  1683. cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M |
  1684. V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_CAPTURE;
  1685. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  1686. return 0;
  1687. }
  1688. /*
  1689. * sde_rotator_enum_fmt_vid_cap - V4l2 ioctl enumerate output format handler.
  1690. * @file: Pointer to file struct.
  1691. * @fh: V4l2 File handle.
  1692. * @f: Pointer to v4l2_fmtdesc struct need to be filled.
  1693. */
  1694. static int sde_rotator_enum_fmt_vid_cap(struct file *file,
  1695. void *fh, struct v4l2_fmtdesc *f)
  1696. {
  1697. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  1698. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  1699. struct sde_mdp_format_params *fmt;
  1700. u32 i, index, pixfmt;
  1701. bool found = false;
  1702. for (i = 0, index = 0; index <= f->index; i++) {
  1703. pixfmt = sde_rotator_get_pixfmt(rot_dev->mgr, i, false,
  1704. SDE_ROTATOR_MODE_OFFLINE);
  1705. if (!pixfmt)
  1706. return -EINVAL;
  1707. fmt = sde_get_format_params(pixfmt);
  1708. if (!fmt)
  1709. return -EINVAL;
  1710. if (sde_mdp_is_private_format(fmt))
  1711. continue;
  1712. if (index == f->index) {
  1713. found = true;
  1714. break;
  1715. }
  1716. index++;
  1717. }
  1718. if (!found)
  1719. return -EINVAL;
  1720. f->pixelformat = pixfmt;
  1721. strlcpy(f->description, fmt->description, sizeof(f->description));
  1722. return 0;
  1723. }
  1724. /*
  1725. * sde_rotator_enum_fmt_vid_out - V4l2 ioctl enumerate capture format handler.
  1726. * @file: Pointer to file struct.
  1727. * @fh: V4l2 File handle.
  1728. * @f: Pointer to v4l2_fmtdesc struct need to be filled.
  1729. */
  1730. static int sde_rotator_enum_fmt_vid_out(struct file *file,
  1731. void *fh, struct v4l2_fmtdesc *f)
  1732. {
  1733. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  1734. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  1735. struct sde_mdp_format_params *fmt;
  1736. u32 i, index, pixfmt;
  1737. bool found = false;
  1738. for (i = 0, index = 0; index <= f->index; i++) {
  1739. pixfmt = sde_rotator_get_pixfmt(rot_dev->mgr, i, true,
  1740. SDE_ROTATOR_MODE_OFFLINE);
  1741. if (!pixfmt)
  1742. return -EINVAL;
  1743. fmt = sde_get_format_params(pixfmt);
  1744. if (!fmt)
  1745. return -EINVAL;
  1746. if (sde_mdp_is_private_format(fmt))
  1747. continue;
  1748. if (index == f->index) {
  1749. found = true;
  1750. break;
  1751. }
  1752. index++;
  1753. }
  1754. if (!found)
  1755. return -EINVAL;
  1756. f->pixelformat = pixfmt;
  1757. strlcpy(f->description, fmt->description, sizeof(f->description));
  1758. return 0;
  1759. }
  1760. /*
  1761. * sde_rotator_g_fmt_cap - V4l2 ioctl get capture format handler.
  1762. * @file: Pointer to file struct.
  1763. * @fh: V4l2 File handle.
  1764. * @f: Pointer to v4l2_format struct need to be filled.
  1765. */
  1766. static int sde_rotator_g_fmt_cap(struct file *file, void *fh,
  1767. struct v4l2_format *f)
  1768. {
  1769. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  1770. *f = ctx->format_cap;
  1771. return 0;
  1772. }
  1773. /*
  1774. * sde_rotator_g_fmt_out - V4l2 ioctl get output format handler.
  1775. * @file: Pointer to file struct.
  1776. * @fh: V4l2 File handle.
  1777. * @f: Pointer to v4l2_format struct need to be filled.
  1778. */
  1779. static int sde_rotator_g_fmt_out(struct file *file, void *fh,
  1780. struct v4l2_format *f)
  1781. {
  1782. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  1783. *f = ctx->format_out;
  1784. return 0;
  1785. }
  1786. /*
  1787. * sde_rotator_try_fmt_vid_cap - V4l2 ioctl try capture format handler.
  1788. * @file: Pointer to file struct.
  1789. * @fh: V4l2 File handle.
  1790. * @f: Pointer to v4l2_format struct.
  1791. */
  1792. static int sde_rotator_try_fmt_vid_cap(struct file *file,
  1793. void *fh, struct v4l2_format *f)
  1794. {
  1795. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  1796. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  1797. struct sde_rotation_config config;
  1798. int ret;
  1799. if ((f->fmt.pix.width == 0) || (f->fmt.pix.height == 0)) {
  1800. SDEDEV_WARN(ctx->rot_dev->dev,
  1801. "Not supporting 0 width/height: %dx%d\n",
  1802. f->fmt.pix.width, f->fmt.pix.height);
  1803. return -EINVAL;
  1804. }
  1805. sde_rot_mgr_lock(rot_dev->mgr);
  1806. sde_rotator_get_config_from_ctx(ctx, &config);
  1807. config.output.format = f->fmt.pix.pixelformat;
  1808. config.output.width = f->fmt.pix.width;
  1809. config.output.height = f->fmt.pix.height;
  1810. config.flags |= SDE_ROTATION_VERIFY_INPUT_ONLY;
  1811. ret = sde_rotator_verify_config_output(rot_dev->mgr, &config);
  1812. sde_rot_mgr_unlock(rot_dev->mgr);
  1813. if (ret) {
  1814. if ((config.output.width == f->fmt.pix.width) &&
  1815. (config.output.height == f->fmt.pix.height)) {
  1816. SDEDEV_WARN(ctx->rot_dev->dev,
  1817. "invalid capture format 0x%8.8x %dx%d\n",
  1818. f->fmt.pix.pixelformat,
  1819. f->fmt.pix.width,
  1820. f->fmt.pix.height);
  1821. return -EINVAL;
  1822. }
  1823. f->fmt.pix.width = config.output.width;
  1824. f->fmt.pix.height = config.output.height;
  1825. }
  1826. sde_rotator_format_recalc(f);
  1827. return ret;
  1828. }
  1829. /*
  1830. * sde_rotator_try_fmt_vid_out - V4l2 ioctl try output format handler.
  1831. * @file: Pointer to file struct.
  1832. * @fh: V4l2 File handle.
  1833. * @f: Pointer to v4l2_format struct.
  1834. */
  1835. static int sde_rotator_try_fmt_vid_out(struct file *file,
  1836. void *fh, struct v4l2_format *f)
  1837. {
  1838. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  1839. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  1840. struct sde_rotation_config config;
  1841. int ret;
  1842. if ((f->fmt.pix.width == 0) || (f->fmt.pix.height == 0)) {
  1843. SDEDEV_WARN(ctx->rot_dev->dev,
  1844. "Not supporting 0 width/height: %dx%d\n",
  1845. f->fmt.pix.width, f->fmt.pix.height);
  1846. return -EINVAL;
  1847. }
  1848. sde_rot_mgr_lock(rot_dev->mgr);
  1849. sde_rotator_get_config_from_ctx(ctx, &config);
  1850. config.input.format = f->fmt.pix.pixelformat;
  1851. config.input.width = f->fmt.pix.width;
  1852. config.input.height = f->fmt.pix.height;
  1853. config.flags |= SDE_ROTATION_VERIFY_INPUT_ONLY;
  1854. ret = sde_rotator_verify_config_input(rot_dev->mgr, &config);
  1855. sde_rot_mgr_unlock(rot_dev->mgr);
  1856. if (ret) {
  1857. if ((config.input.width == f->fmt.pix.width) &&
  1858. (config.input.height == f->fmt.pix.height)) {
  1859. SDEDEV_WARN(ctx->rot_dev->dev,
  1860. "invalid output format 0x%8.8x %dx%d\n",
  1861. f->fmt.pix.pixelformat,
  1862. f->fmt.pix.width,
  1863. f->fmt.pix.height);
  1864. return -EINVAL;
  1865. }
  1866. f->fmt.pix.width = config.input.width;
  1867. f->fmt.pix.height = config.input.height;
  1868. }
  1869. sde_rotator_format_recalc(f);
  1870. return ret;
  1871. }
  1872. /*
  1873. * sde_rotator_s_fmt_vid_cap - V4l2 ioctl set capture format handler.
  1874. * @file: Pointer to file struct.
  1875. * @fh: V4l2 File handle.
  1876. * @f: Pointer to v4l2_format struct.
  1877. */
  1878. static int sde_rotator_s_fmt_vid_cap(struct file *file,
  1879. void *fh, struct v4l2_format *f)
  1880. {
  1881. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  1882. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  1883. int ret;
  1884. ret = sde_rotator_try_fmt_vid_cap(file, fh, f);
  1885. if (ret)
  1886. return -EINVAL;
  1887. /* Initialize crop */
  1888. ctx->crop_cap.top = 0;
  1889. ctx->crop_cap.left = 0;
  1890. ctx->crop_cap.width = f->fmt.pix.width;
  1891. ctx->crop_cap.height = f->fmt.pix.height;
  1892. ctx->format_cap = *f;
  1893. SDEDEV_DBG(rot_dev->dev,
  1894. "s_fmt s:%d t:%d fmt:0x%8.8x field:%u (%u,%u)\n",
  1895. ctx->session_id, f->type,
  1896. f->fmt.pix.pixelformat,
  1897. f->fmt.pix.field,
  1898. f->fmt.pix.width, f->fmt.pix.height);
  1899. return 0;
  1900. }
  1901. /*
  1902. * sde_rotator_s_fmt_vid_out - V4l2 ioctl set output format handler.
  1903. * @file: Pointer to file struct.
  1904. * @fh: V4l2 File handle.
  1905. * @f: Pointer to v4l2_format struct.
  1906. */
  1907. static int sde_rotator_s_fmt_vid_out(struct file *file,
  1908. void *fh, struct v4l2_format *f)
  1909. {
  1910. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  1911. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  1912. int ret;
  1913. ret = sde_rotator_try_fmt_vid_out(file, fh, f);
  1914. if (ret)
  1915. return -EINVAL;
  1916. /* Initialize crop */
  1917. ctx->crop_out.top = 0;
  1918. ctx->crop_out.left = 0;
  1919. ctx->crop_out.width = f->fmt.pix.width;
  1920. ctx->crop_out.height = f->fmt.pix.height;
  1921. ctx->format_out = *f;
  1922. SDEDEV_DBG(rot_dev->dev,
  1923. "s_fmt s:%d t:%d fmt:0x%8.8x field:%u (%u,%u)\n",
  1924. ctx->session_id, f->type,
  1925. f->fmt.pix.pixelformat,
  1926. f->fmt.pix.field,
  1927. f->fmt.pix.width, f->fmt.pix.height);
  1928. return 0;
  1929. }
  1930. /*
  1931. * sde_rotator_reqbufs - V4l2 ioctl request buffers handler.
  1932. * @file: Pointer to file struct.
  1933. * @fh: V4l2 File handle.
  1934. * @req: Pointer to v4l2_requestbuffer struct.
  1935. */
  1936. static int sde_rotator_reqbufs(struct file *file,
  1937. void *fh, struct v4l2_requestbuffers *req)
  1938. {
  1939. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  1940. return v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, req);
  1941. }
  1942. /*
  1943. * sde_rotator_qbuf - V4l2 ioctl queue buffer handler.
  1944. * @file: Pointer to file struct.
  1945. * @fh: V4l2 File handle.
  1946. * @buf: Pointer to v4l2_buffer struct.
  1947. */
  1948. static int sde_rotator_qbuf(struct file *file, void *fh,
  1949. struct v4l2_buffer *buf)
  1950. {
  1951. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  1952. int ret;
  1953. /* create fence for capture buffer */
  1954. if ((buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1955. && (buf->index < ctx->nbuf_cap)) {
  1956. int idx = buf->index;
  1957. ctx->vbinfo_cap[idx].fd = -1;
  1958. ctx->vbinfo_cap[idx].fence = sde_rotator_get_sync_fence(
  1959. ctx->work_queue.timeline, NULL,
  1960. &ctx->vbinfo_cap[idx].fence_ts);
  1961. ctx->vbinfo_cap[idx].qbuf_ts = ktime_get();
  1962. ctx->vbinfo_cap[idx].dqbuf_ts = NULL;
  1963. SDEDEV_DBG(ctx->rot_dev->dev,
  1964. "create buffer fence s:%d.%u i:%d f:%pK\n",
  1965. ctx->session_id,
  1966. ctx->vbinfo_cap[idx].fence_ts,
  1967. idx,
  1968. ctx->vbinfo_cap[idx].fence);
  1969. } else if ((buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1970. && (buf->index < ctx->nbuf_out)) {
  1971. int idx = buf->index;
  1972. ctx->vbinfo_out[idx].qbuf_ts = ktime_get();
  1973. ctx->vbinfo_out[idx].dqbuf_ts = NULL;
  1974. }
  1975. ret = v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
  1976. if (ret < 0)
  1977. SDEDEV_ERR(ctx->rot_dev->dev, "fail qbuf s:%d t:%d r:%d\n",
  1978. ctx->session_id, buf->type, ret);
  1979. SDEROT_EVTLOG(buf->type, buf->bytesused, buf->length, buf->m.fd, ret);
  1980. return ret;
  1981. }
  1982. /*
  1983. * sde_rotator_dqbuf - V4l2 ioctl dequeue buffer handler.
  1984. * @file: Pointer to file struct.
  1985. * @fh: V4l2 File handle.
  1986. * @buf: Pointer to v4l2_buffer struct.
  1987. */
  1988. static int sde_rotator_dqbuf(struct file *file,
  1989. void *fh, struct v4l2_buffer *buf)
  1990. {
  1991. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  1992. int ret;
  1993. ret = v4l2_m2m_dqbuf(file, ctx->fh.m2m_ctx, buf);
  1994. if (ret) {
  1995. SDEDEV_ERR(ctx->rot_dev->dev,
  1996. "fail dqbuf s:%d t:%d i:%d r:%d\n",
  1997. ctx->session_id, buf->type, buf->index, ret);
  1998. return ret;
  1999. }
  2000. /* clear fence for buffer */
  2001. if ((buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2002. && (buf->index < ctx->nbuf_cap)) {
  2003. int idx = buf->index;
  2004. if (ctx->vbinfo_cap[idx].fence) {
  2005. /* fence is not used */
  2006. SDEDEV_DBG(ctx->rot_dev->dev, "put fence s:%d i:%d\n",
  2007. ctx->session_id, idx);
  2008. sde_rotator_put_sync_fence(ctx->vbinfo_cap[idx].fence);
  2009. }
  2010. ctx->vbinfo_cap[idx].fence = NULL;
  2011. ctx->vbinfo_cap[idx].fd = -1;
  2012. if (ctx->vbinfo_cap[idx].dqbuf_ts)
  2013. *(ctx->vbinfo_cap[idx].dqbuf_ts) = ktime_get();
  2014. } else if ((buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  2015. && (buf->index < ctx->nbuf_out)) {
  2016. int idx = buf->index;
  2017. ctx->vbinfo_out[idx].fence = NULL;
  2018. ctx->vbinfo_out[idx].fd = -1;
  2019. if (ctx->vbinfo_out[idx].dqbuf_ts)
  2020. *(ctx->vbinfo_out[idx].dqbuf_ts) = ktime_get();
  2021. } else {
  2022. SDEDEV_WARN(ctx->rot_dev->dev, "invalid dq s:%d t:%d i:%d\n",
  2023. ctx->session_id, buf->type, buf->index);
  2024. }
  2025. return 0;
  2026. }
  2027. /*
  2028. * sde_rotator_querybuf - V4l2 ioctl query buffer handler.
  2029. * @file: Pointer to file struct.
  2030. * @fh: V4l2 File handle.
  2031. * @buf: Pointer to v4l2_buffer struct.
  2032. */
  2033. static int sde_rotator_querybuf(struct file *file,
  2034. void *fh, struct v4l2_buffer *buf)
  2035. {
  2036. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  2037. return v4l2_m2m_querybuf(file, ctx->fh.m2m_ctx, buf);
  2038. }
  2039. /*
  2040. * sde_rotator_streamon - V4l2 ioctl stream on handler.
  2041. * @file: Pointer to file struct.
  2042. * @fh: V4l2 File handle.
  2043. * @buf_type: V4l2 buffer type.
  2044. */
  2045. static int sde_rotator_streamon(struct file *file,
  2046. void *fh, enum v4l2_buf_type buf_type)
  2047. {
  2048. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  2049. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  2050. struct sde_rotation_config config;
  2051. struct vb2_queue *vq;
  2052. int ret;
  2053. SDEDEV_DBG(ctx->rot_dev->dev, "stream on s:%d t:%d\n",
  2054. ctx->session_id, buf_type);
  2055. vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
  2056. buf_type == V4L2_BUF_TYPE_VIDEO_OUTPUT ?
  2057. V4L2_BUF_TYPE_VIDEO_CAPTURE :
  2058. V4L2_BUF_TYPE_VIDEO_OUTPUT);
  2059. if (!vq) {
  2060. SDEDEV_ERR(ctx->rot_dev->dev, "fail to get vq on s:%d t:%d\n",
  2061. ctx->session_id, buf_type);
  2062. return -EINVAL;
  2063. }
  2064. if (vb2_is_streaming(vq)) {
  2065. sde_rot_mgr_lock(rot_dev->mgr);
  2066. sde_rotator_get_config_from_ctx(ctx, &config);
  2067. config.flags &= ~SDE_ROTATION_VERIFY_INPUT_ONLY;
  2068. ret = sde_rotator_session_config(rot_dev->mgr, ctx->private,
  2069. &config);
  2070. sde_rot_mgr_unlock(rot_dev->mgr);
  2071. if (ret < 0) {
  2072. SDEDEV_ERR(rot_dev->dev,
  2073. "fail config in stream on s:%d t:%d r:%d\n",
  2074. ctx->session_id, buf_type, ret);
  2075. return ret;
  2076. }
  2077. ctx->rotcfg = config;
  2078. }
  2079. ret = v4l2_m2m_streamon(file, ctx->fh.m2m_ctx, buf_type);
  2080. if (ret < 0)
  2081. SDEDEV_ERR(ctx->rot_dev->dev, "fail stream on s:%d t:%d\n",
  2082. ctx->session_id, buf_type);
  2083. return ret;
  2084. }
  2085. /*
  2086. * sde_rotator_streamoff - V4l2 ioctl stream off handler.
  2087. * @file: Pointer to file struct.
  2088. * @fh: V4l2 File handle.
  2089. * @buf_type: V4l2 buffer type.
  2090. */
  2091. static int sde_rotator_streamoff(struct file *file,
  2092. void *fh, enum v4l2_buf_type buf_type)
  2093. {
  2094. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  2095. int ret;
  2096. SDEDEV_DBG(ctx->rot_dev->dev, "stream off s:%d t:%d\n",
  2097. ctx->session_id, buf_type);
  2098. ret = v4l2_m2m_streamoff(file, ctx->fh.m2m_ctx, buf_type);
  2099. if (ret < 0)
  2100. SDEDEV_ERR(ctx->rot_dev->dev, "fail stream off s:%d t:%d\n",
  2101. ctx->session_id, buf_type);
  2102. return ret;
  2103. }
  2104. /*
  2105. * sde_rotator_g_selection - V4l2 ioctl get crop.
  2106. * @file: Pointer to file struct.
  2107. * @fh: V4l2 File handle.
  2108. * @selection: Pointer to v4l2_selection struct need to be set.
  2109. */
  2110. static int sde_rotator_g_selection(struct file *file, void *fh,
  2111. struct v4l2_selection *selection)
  2112. {
  2113. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  2114. switch (selection->type) {
  2115. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  2116. selection->r = ctx->crop_out;
  2117. break;
  2118. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2119. selection->r = ctx->crop_cap;
  2120. break;
  2121. default:
  2122. return -EINVAL;
  2123. }
  2124. return 0;
  2125. }
  2126. /*
  2127. * sde_rotator_s_selection - V4l2 ioctl set crop.
  2128. * @file: Pointer to file struct.
  2129. * @fh: V4l2 File handle.
  2130. * @selection: Pointer to v4l2_selection struct need to be set.
  2131. */
  2132. static int sde_rotator_s_selection(struct file *file, void *fh,
  2133. struct v4l2_selection *selection)
  2134. {
  2135. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  2136. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  2137. struct sde_rotation_item item;
  2138. struct v4l2_rect rect;
  2139. sde_rotator_get_item_from_ctx(ctx, &item);
  2140. rect.left = max_t(__u32, selection->r.left, 0);
  2141. rect.top = max_t(__u32, selection->r.top, 0);
  2142. rect.height = max_t(__u32, selection->r.height, 0);
  2143. rect.width = max_t(__u32, selection->r.width, 0);
  2144. if (selection->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  2145. rect.left = min_t(__u32, rect.left,
  2146. ctx->format_out.fmt.pix.width - 1);
  2147. rect.top = min_t(__u32, rect.top,
  2148. ctx->format_out.fmt.pix.height - 1);
  2149. rect.width = min_t(__u32, rect.width,
  2150. (ctx->format_out.fmt.pix.width - rect.left));
  2151. rect.height = min_t(__u32, rect.height,
  2152. (ctx->format_out.fmt.pix.height - rect.top));
  2153. item.src_rect.x = rect.left;
  2154. item.src_rect.y = rect.top;
  2155. item.src_rect.w = rect.width;
  2156. item.src_rect.h = rect.height;
  2157. sde_rotator_validate_item(ctx, &item);
  2158. SDEDEV_DBG(rot_dev->dev,
  2159. "s_crop s:%d t:%d (%u,%u,%u,%u)->(%u,%u,%u,%u)\n",
  2160. ctx->session_id, selection->type,
  2161. selection->r.left, selection->r.top,
  2162. selection->r.width, selection->r.height,
  2163. item.src_rect.x, item.src_rect.y,
  2164. item.src_rect.w, item.src_rect.h);
  2165. ctx->crop_out.left = item.src_rect.x;
  2166. ctx->crop_out.top = item.src_rect.y;
  2167. ctx->crop_out.width = item.src_rect.w;
  2168. ctx->crop_out.height = item.src_rect.h;
  2169. } else if (selection->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2170. rect.left = min_t(__u32, rect.left,
  2171. ctx->format_cap.fmt.pix.width - 1);
  2172. rect.top = min_t(__u32, rect.top,
  2173. ctx->format_cap.fmt.pix.height - 1);
  2174. rect.width = min_t(__u32, rect.width,
  2175. (ctx->format_cap.fmt.pix.width - rect.left));
  2176. rect.height = min_t(__u32, rect.height,
  2177. (ctx->format_cap.fmt.pix.height - rect.top));
  2178. item.dst_rect.x = rect.left;
  2179. item.dst_rect.y = rect.top;
  2180. item.dst_rect.w = rect.width;
  2181. item.dst_rect.h = rect.height;
  2182. sde_rotator_validate_item(ctx, &item);
  2183. SDEDEV_DBG(rot_dev->dev,
  2184. "s_crop s:%d t:%d (%u,%u,%u,%u)->(%u,%u,%u,%u)\n",
  2185. ctx->session_id, selection->type,
  2186. selection->r.left, selection->r.top,
  2187. selection->r.width, selection->r.height,
  2188. item.dst_rect.x, item.dst_rect.y,
  2189. item.dst_rect.w, item.dst_rect.h);
  2190. ctx->crop_cap.left = item.dst_rect.x;
  2191. ctx->crop_cap.top = item.dst_rect.y;
  2192. ctx->crop_cap.width = item.dst_rect.w;
  2193. ctx->crop_cap.height = item.dst_rect.h;
  2194. } else {
  2195. return -EINVAL;
  2196. }
  2197. return 0;
  2198. }
  2199. /*
  2200. * sde_rotator_g_parm - V4l2 ioctl get parm.
  2201. * @file: Pointer to file struct.
  2202. * @fh: V4l2 File handle.
  2203. * @a: Pointer to v4l2_streamparm struct need to be filled.
  2204. */
  2205. static int sde_rotator_g_parm(struct file *file, void *fh,
  2206. struct v4l2_streamparm *a)
  2207. {
  2208. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  2209. /* Get param is supported only for input buffers */
  2210. if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  2211. return -EINVAL;
  2212. a->parm.output.capability = 0;
  2213. a->parm.output.extendedmode = 0;
  2214. a->parm.output.outputmode = 0;
  2215. a->parm.output.writebuffers = 0;
  2216. a->parm.output.timeperframe = ctx->timeperframe;
  2217. return 0;
  2218. }
  2219. /*
  2220. * sde_rotator_s_parm - V4l2 ioctl set parm.
  2221. * @file: Pointer to file struct.
  2222. * @fh: V4l2 File handle.
  2223. * @a: Pointer to v4l2_streamparm struct need to be set.
  2224. */
  2225. static int sde_rotator_s_parm(struct file *file, void *fh,
  2226. struct v4l2_streamparm *a)
  2227. {
  2228. struct sde_rotator_ctx *ctx = sde_rotator_ctx_from_fh(fh);
  2229. /* Set param is supported only for input buffers */
  2230. if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  2231. return -EINVAL;
  2232. if (!a->parm.output.timeperframe.numerator ||
  2233. !a->parm.output.timeperframe.denominator)
  2234. return -EINVAL;
  2235. ctx->timeperframe = a->parm.output.timeperframe;
  2236. return 0;
  2237. }
  2238. /*
  2239. * sde_rotator_private_ioctl - V4l2 private ioctl handler.
  2240. * @file: Pointer to file struct.
  2241. * @fd: V4l2 device file handle.
  2242. * @valid_prio: Priority ioctl valid flag.
  2243. * @cmd: Ioctl command.
  2244. * @arg: Ioctl argument.
  2245. */
  2246. static long sde_rotator_private_ioctl(struct file *file, void *fh,
  2247. bool valid_prio, unsigned int cmd, void *arg)
  2248. {
  2249. struct sde_rotator_ctx *ctx =
  2250. sde_rotator_ctx_from_fh(file->private_data);
  2251. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  2252. struct msm_sde_rotator_fence *fence = arg;
  2253. struct msm_sde_rotator_comp_ratio *comp_ratio = arg;
  2254. struct sde_rotator_vbinfo *vbinfo;
  2255. int ret;
  2256. switch (cmd) {
  2257. case VIDIOC_S_SDE_ROTATOR_FENCE:
  2258. if (!fence)
  2259. return -EINVAL;
  2260. if (fence->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  2261. return -EINVAL;
  2262. if (fence->index >= ctx->nbuf_out)
  2263. return -EINVAL;
  2264. SDEDEV_DBG(rot_dev->dev,
  2265. "VIDIOC_S_SDE_ROTATOR_FENCE s:%d i:%d fd:%d\n",
  2266. ctx->session_id, fence->index,
  2267. fence->fd);
  2268. vbinfo = &ctx->vbinfo_out[fence->index];
  2269. if (vbinfo->fd >= 0) {
  2270. if (vbinfo->fence) {
  2271. SDEDEV_DBG(rot_dev->dev,
  2272. "put fence s:%d t:%d i:%d\n",
  2273. ctx->session_id,
  2274. fence->type, fence->index);
  2275. sde_rotator_put_sync_fence(vbinfo->fence);
  2276. }
  2277. vbinfo->fence = NULL;
  2278. vbinfo->fd = -1;
  2279. }
  2280. vbinfo->fd = fence->fd;
  2281. if (vbinfo->fd >= 0) {
  2282. vbinfo->fence =
  2283. sde_rotator_get_fd_sync_fence(vbinfo->fd);
  2284. if (!vbinfo->fence) {
  2285. SDEDEV_WARN(rot_dev->dev,
  2286. "invalid input fence fd s:%d fd:%d\n",
  2287. ctx->session_id, vbinfo->fd);
  2288. vbinfo->fd = -1;
  2289. return -EINVAL;
  2290. }
  2291. } else {
  2292. vbinfo->fence = NULL;
  2293. }
  2294. break;
  2295. case VIDIOC_G_SDE_ROTATOR_FENCE:
  2296. if (!fence)
  2297. return -EINVAL;
  2298. if (fence->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2299. return -EINVAL;
  2300. if (fence->index >= ctx->nbuf_cap)
  2301. return -EINVAL;
  2302. vbinfo = &ctx->vbinfo_cap[fence->index];
  2303. if (!vbinfo)
  2304. return -EINVAL;
  2305. if (vbinfo->fence) {
  2306. ret = sde_rotator_get_sync_fence_fd(vbinfo->fence);
  2307. if (ret < 0) {
  2308. SDEDEV_ERR(rot_dev->dev,
  2309. "fail get fence fd s:%d\n",
  2310. ctx->session_id);
  2311. return ret;
  2312. }
  2313. /**
  2314. * Cache fence descriptor in case user calls this
  2315. * ioctl multiple times. Cached value would be stale
  2316. * if user duplicated and closed old descriptor.
  2317. */
  2318. vbinfo->fd = ret;
  2319. } else if (!sde_rotator_get_fd_sync_fence(vbinfo->fd)) {
  2320. /**
  2321. * User has closed cached fence descriptor.
  2322. * Invalidate descriptor cache.
  2323. */
  2324. vbinfo->fd = -1;
  2325. }
  2326. fence->fd = vbinfo->fd;
  2327. SDEDEV_DBG(rot_dev->dev,
  2328. "VIDIOC_G_SDE_ROTATOR_FENCE s:%d i:%d fd:%d\n",
  2329. ctx->session_id, fence->index,
  2330. fence->fd);
  2331. break;
  2332. case VIDIOC_S_SDE_ROTATOR_COMP_RATIO:
  2333. if (!comp_ratio)
  2334. return -EINVAL;
  2335. else if (!comp_ratio->numer || !comp_ratio->denom)
  2336. return -EINVAL;
  2337. else if (comp_ratio->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  2338. comp_ratio->index < ctx->nbuf_out)
  2339. vbinfo = &ctx->vbinfo_out[comp_ratio->index];
  2340. else if (comp_ratio->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  2341. comp_ratio->index < ctx->nbuf_cap)
  2342. vbinfo = &ctx->vbinfo_cap[comp_ratio->index];
  2343. else
  2344. return -EINVAL;
  2345. vbinfo->comp_ratio.numer = comp_ratio->numer;
  2346. vbinfo->comp_ratio.denom = comp_ratio->denom;
  2347. SDEDEV_DBG(rot_dev->dev,
  2348. "VIDIOC_S_SDE_ROTATOR_COMP_RATIO s:%d i:%d t:%d cr:%u/%u\n",
  2349. ctx->session_id, comp_ratio->index,
  2350. comp_ratio->type,
  2351. vbinfo->comp_ratio.numer,
  2352. vbinfo->comp_ratio.denom);
  2353. break;
  2354. case VIDIOC_G_SDE_ROTATOR_COMP_RATIO:
  2355. if (!comp_ratio)
  2356. return -EINVAL;
  2357. else if (comp_ratio->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  2358. comp_ratio->index < ctx->nbuf_out)
  2359. vbinfo = &ctx->vbinfo_out[comp_ratio->index];
  2360. else if (comp_ratio->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  2361. comp_ratio->index < ctx->nbuf_cap)
  2362. vbinfo = &ctx->vbinfo_cap[comp_ratio->index];
  2363. else
  2364. return -EINVAL;
  2365. comp_ratio->numer = vbinfo->comp_ratio.numer;
  2366. comp_ratio->denom = vbinfo->comp_ratio.denom;
  2367. SDEDEV_DBG(rot_dev->dev,
  2368. "VIDIOC_G_SDE_ROTATOR_COMP_RATIO s:%d i:%d t:%d cr:%u/%u\n",
  2369. ctx->session_id, comp_ratio->index,
  2370. comp_ratio->type,
  2371. comp_ratio->numer,
  2372. comp_ratio->denom);
  2373. break;
  2374. default:
  2375. SDEDEV_WARN(rot_dev->dev, "invalid ioctl type %x\n", cmd);
  2376. return -ENOTTY;
  2377. }
  2378. return 0;
  2379. }
  2380. #ifdef CONFIG_COMPAT
  2381. /*
  2382. * sde_rotator_compat_ioctl32 - Compat ioctl handler function.
  2383. * @file: Pointer to file struct.
  2384. * @cmd: Ioctl command.
  2385. * @arg: Ioctl argument.
  2386. */
  2387. static long sde_rotator_compat_ioctl32(struct file *file,
  2388. unsigned int cmd, unsigned long arg)
  2389. {
  2390. struct video_device *vdev = video_devdata(file);
  2391. struct sde_rotator_ctx *ctx =
  2392. sde_rotator_ctx_from_fh(file->private_data);
  2393. long ret;
  2394. mutex_lock(vdev->lock);
  2395. switch (cmd) {
  2396. case VIDIOC_S_SDE_ROTATOR_FENCE:
  2397. case VIDIOC_G_SDE_ROTATOR_FENCE:
  2398. {
  2399. struct msm_sde_rotator_fence fence;
  2400. if (copy_from_user(&fence, (void __user *)arg,
  2401. sizeof(struct msm_sde_rotator_fence)))
  2402. goto ioctl32_error;
  2403. ret = sde_rotator_private_ioctl(file, file->private_data,
  2404. 0, cmd, (void *)&fence);
  2405. if (copy_to_user((void __user *)arg, &fence,
  2406. sizeof(struct msm_sde_rotator_fence)))
  2407. goto ioctl32_error;
  2408. break;
  2409. }
  2410. case VIDIOC_S_SDE_ROTATOR_COMP_RATIO:
  2411. case VIDIOC_G_SDE_ROTATOR_COMP_RATIO:
  2412. {
  2413. struct msm_sde_rotator_comp_ratio comp_ratio;
  2414. if (copy_from_user(&comp_ratio, (void __user *)arg,
  2415. sizeof(struct msm_sde_rotator_comp_ratio)))
  2416. goto ioctl32_error;
  2417. ret = sde_rotator_private_ioctl(file, file->private_data,
  2418. 0, cmd, (void *)&comp_ratio);
  2419. if (copy_to_user((void __user *)arg, &comp_ratio,
  2420. sizeof(struct msm_sde_rotator_comp_ratio)))
  2421. goto ioctl32_error;
  2422. break;
  2423. }
  2424. default:
  2425. SDEDEV_ERR(ctx->rot_dev->dev, "invalid ioctl32 type:%x\n", cmd);
  2426. ret = -ENOIOCTLCMD;
  2427. break;
  2428. }
  2429. mutex_unlock(vdev->lock);
  2430. return ret;
  2431. ioctl32_error:
  2432. mutex_unlock(vdev->lock);
  2433. SDEDEV_ERR(ctx->rot_dev->dev, "error handling ioctl32 cmd:%x\n", cmd);
  2434. return -EFAULT;
  2435. }
  2436. #endif
  2437. static int sde_rotator_ctrl_subscribe_event(struct v4l2_fh *fh,
  2438. const struct v4l2_event_subscription *sub)
  2439. {
  2440. return -EINVAL;
  2441. }
  2442. static int sde_rotator_event_unsubscribe(struct v4l2_fh *fh,
  2443. const struct v4l2_event_subscription *sub)
  2444. {
  2445. return -EINVAL;
  2446. }
  2447. /* V4l2 ioctl handlers */
  2448. static const struct v4l2_ioctl_ops sde_rotator_ioctl_ops = {
  2449. .vidioc_querycap = sde_rotator_querycap,
  2450. .vidioc_enum_fmt_vid_out = sde_rotator_enum_fmt_vid_out,
  2451. .vidioc_enum_fmt_vid_cap = sde_rotator_enum_fmt_vid_cap,
  2452. .vidioc_g_fmt_vid_out = sde_rotator_g_fmt_out,
  2453. .vidioc_g_fmt_vid_cap = sde_rotator_g_fmt_cap,
  2454. .vidioc_try_fmt_vid_out = sde_rotator_try_fmt_vid_out,
  2455. .vidioc_try_fmt_vid_cap = sde_rotator_try_fmt_vid_cap,
  2456. .vidioc_s_fmt_vid_out = sde_rotator_s_fmt_vid_out,
  2457. .vidioc_s_fmt_vid_cap = sde_rotator_s_fmt_vid_cap,
  2458. .vidioc_reqbufs = sde_rotator_reqbufs,
  2459. .vidioc_qbuf = sde_rotator_qbuf,
  2460. .vidioc_dqbuf = sde_rotator_dqbuf,
  2461. .vidioc_querybuf = sde_rotator_querybuf,
  2462. .vidioc_streamon = sde_rotator_streamon,
  2463. .vidioc_streamoff = sde_rotator_streamoff,
  2464. .vidioc_g_selection = sde_rotator_g_selection,
  2465. .vidioc_s_selection = sde_rotator_s_selection,
  2466. .vidioc_g_parm = sde_rotator_g_parm,
  2467. .vidioc_s_parm = sde_rotator_s_parm,
  2468. .vidioc_default = sde_rotator_private_ioctl,
  2469. .vidioc_log_status = v4l2_ctrl_log_status,
  2470. .vidioc_subscribe_event = sde_rotator_ctrl_subscribe_event,
  2471. .vidioc_unsubscribe_event = sde_rotator_event_unsubscribe,
  2472. };
  2473. /*
  2474. * sde_rotator_retire_handler - Invoked by hal when processing is done.
  2475. * @work: Pointer to work structure.
  2476. *
  2477. * This function is scheduled in work queue context.
  2478. */
  2479. static void sde_rotator_retire_handler(struct kthread_work *work)
  2480. {
  2481. struct vb2_v4l2_buffer *src_buf;
  2482. struct vb2_v4l2_buffer *dst_buf;
  2483. struct sde_rotator_ctx *ctx;
  2484. struct sde_rotator_device *rot_dev;
  2485. struct sde_rotator_request *request;
  2486. request = container_of(work, struct sde_rotator_request, retire_work);
  2487. ctx = request->ctx;
  2488. if (!ctx || !ctx->rot_dev) {
  2489. SDEROT_ERR("null context/device\n");
  2490. return;
  2491. }
  2492. rot_dev = ctx->rot_dev;
  2493. SDEDEV_DBG(rot_dev->dev, "retire handler s:%d\n", ctx->session_id);
  2494. mutex_lock(&rot_dev->lock);
  2495. if (ctx->abort_pending) {
  2496. SDEDEV_DBG(rot_dev->dev, "abort command in retire s:%d\n",
  2497. ctx->session_id);
  2498. sde_rotator_update_retire_sequence(request);
  2499. sde_rotator_retire_request(request);
  2500. mutex_unlock(&rot_dev->lock);
  2501. return;
  2502. }
  2503. if (!ctx->file) {
  2504. sde_rotator_update_retire_sequence(request);
  2505. } else if (rot_dev->early_submit) {
  2506. if (IS_ERR_OR_NULL(request->req)) {
  2507. /* fail pending request or something wrong */
  2508. SDEDEV_ERR(rot_dev->dev,
  2509. "pending request fail in retire s:%d\n",
  2510. ctx->session_id);
  2511. }
  2512. /* pending request. reschedule this context. */
  2513. v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
  2514. } else {
  2515. /* no pending request. acknowledge the usual way. */
  2516. src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  2517. dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  2518. if (!src_buf || !dst_buf) {
  2519. SDEDEV_ERR(rot_dev->dev,
  2520. "null buffer in retire s:%d sb:%pK db:%pK\n",
  2521. ctx->session_id,
  2522. src_buf, dst_buf);
  2523. }
  2524. sde_rotator_update_retire_sequence(request);
  2525. sde_rotator_retire_request(request);
  2526. v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
  2527. v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
  2528. v4l2_m2m_job_finish(rot_dev->m2m_dev, ctx->fh.m2m_ctx);
  2529. }
  2530. mutex_unlock(&rot_dev->lock);
  2531. }
  2532. /*
  2533. * sde_rotator_process_buffers - Start rotator processing.
  2534. * @ctx: Pointer rotator context.
  2535. * @src_buf: Pointer to Vb2 source buffer.
  2536. * @dst_buf: Pointer to Vb2 destination buffer.
  2537. * @request: Pointer to rotator request
  2538. */
  2539. static int sde_rotator_process_buffers(struct sde_rotator_ctx *ctx,
  2540. struct vb2_buffer *src_buf, struct vb2_buffer *dst_buf,
  2541. struct sde_rotator_request *request)
  2542. {
  2543. struct sde_rotator_device *rot_dev = ctx->rot_dev;
  2544. struct sde_rotation_item item;
  2545. struct sde_rot_entry_container *req = NULL;
  2546. struct sde_rotator_buf_handle *src_handle;
  2547. struct sde_rotator_buf_handle *dst_handle;
  2548. struct sde_rotator_statistics *stats = &rot_dev->stats;
  2549. struct sde_rotator_vbinfo *vbinfo_out;
  2550. struct sde_rotator_vbinfo *vbinfo_cap;
  2551. struct sde_rot_trace_entry rot_trace;
  2552. ktime_t *ts;
  2553. int ret;
  2554. if (!src_buf || !dst_buf) {
  2555. SDEDEV_ERR(rot_dev->dev, "null vb2 buffers\n");
  2556. ret = -EINVAL;
  2557. goto error_null_buffer;
  2558. }
  2559. src_handle = src_buf->planes[0].mem_priv;
  2560. dst_handle = dst_buf->planes[0].mem_priv;
  2561. if (!src_handle || !dst_handle) {
  2562. SDEDEV_ERR(rot_dev->dev, "null buffer handle\n");
  2563. ret = -EINVAL;
  2564. goto error_null_buffer;
  2565. }
  2566. vbinfo_out = &ctx->vbinfo_out[src_buf->index];
  2567. vbinfo_cap = &ctx->vbinfo_cap[dst_buf->index];
  2568. SDEDEV_DBG(rot_dev->dev,
  2569. "process buffer s:%d.%u src:(%u,%u,%u,%u) dst:(%u,%u,%u,%u) rot:%d flip:%d/%d sec:%d src_cr:%u/%u dst_cr:%u/%u\n",
  2570. ctx->session_id, vbinfo_cap->fence_ts,
  2571. ctx->crop_out.left, ctx->crop_out.top,
  2572. ctx->crop_out.width, ctx->crop_out.height,
  2573. ctx->crop_cap.left, ctx->crop_cap.top,
  2574. ctx->crop_cap.width, ctx->crop_cap.height,
  2575. ctx->rotate, ctx->hflip, ctx->vflip, ctx->secure,
  2576. vbinfo_out->comp_ratio.numer, vbinfo_out->comp_ratio.denom,
  2577. vbinfo_cap->comp_ratio.numer, vbinfo_cap->comp_ratio.denom);
  2578. /* allocate slot for timestamp */
  2579. ts = stats->ts[stats->count++ % SDE_ROTATOR_NUM_EVENTS];
  2580. ts[SDE_ROTATOR_TS_SRCQB] = vbinfo_out->qbuf_ts;
  2581. ts[SDE_ROTATOR_TS_DSTQB] = vbinfo_cap->qbuf_ts;
  2582. vbinfo_out->dqbuf_ts = &ts[SDE_ROTATOR_TS_SRCDQB];
  2583. vbinfo_cap->dqbuf_ts = &ts[SDE_ROTATOR_TS_DSTDQB];
  2584. ts[SDE_ROTATOR_TS_FENCE] = ktime_get();
  2585. /* Set values to pass to trace */
  2586. rot_trace.wb_idx = ctx->fh.prio;
  2587. rot_trace.flags = (ctx->rotate << 0) | (ctx->hflip << 8) |
  2588. (ctx->hflip << 9) | (ctx->secure << 10);
  2589. rot_trace.input_format = ctx->format_out.fmt.pix.pixelformat;
  2590. rot_trace.input_width = ctx->format_out.fmt.pix.width;
  2591. rot_trace.input_height = ctx->format_out.fmt.pix.height;
  2592. rot_trace.src_x = ctx->crop_out.left;
  2593. rot_trace.src_y = ctx->crop_out.top;
  2594. rot_trace.src_w = ctx->crop_out.width;
  2595. rot_trace.src_h = ctx->crop_out.height;
  2596. rot_trace.output_format = ctx->format_cap.fmt.pix.pixelformat;
  2597. rot_trace.output_width = ctx->format_cap.fmt.pix.width;
  2598. rot_trace.output_height = ctx->format_cap.fmt.pix.height;
  2599. rot_trace.dst_x = ctx->crop_cap.left;
  2600. rot_trace.dst_y = ctx->crop_cap.top;
  2601. rot_trace.dst_w = ctx->crop_cap.width;
  2602. rot_trace.dst_h = ctx->crop_cap.height;
  2603. trace_rot_entry_fence(
  2604. ctx->session_id, vbinfo_cap->fence_ts, &rot_trace);
  2605. if (vbinfo_out->fence) {
  2606. sde_rot_mgr_unlock(rot_dev->mgr);
  2607. mutex_unlock(&rot_dev->lock);
  2608. SDEDEV_DBG(rot_dev->dev, "fence enter s:%d.%d fd:%d\n",
  2609. ctx->session_id, vbinfo_cap->fence_ts, vbinfo_out->fd);
  2610. ret = sde_rotator_wait_sync_fence(vbinfo_out->fence,
  2611. rot_dev->fence_timeout);
  2612. mutex_lock(&rot_dev->lock);
  2613. sde_rot_mgr_lock(rot_dev->mgr);
  2614. sde_rotator_put_sync_fence(vbinfo_out->fence);
  2615. vbinfo_out->fence = NULL;
  2616. if (ret) {
  2617. SDEDEV_ERR(rot_dev->dev,
  2618. "error waiting for fence s:%d.%d fd:%d r:%d\n",
  2619. ctx->session_id,
  2620. vbinfo_cap->fence_ts, vbinfo_out->fd, ret);
  2621. SDEROT_EVTLOG(ctx->session_id, vbinfo_cap->fence_ts,
  2622. vbinfo_out->fd, ret,
  2623. SDE_ROT_EVTLOG_ERROR);
  2624. goto error_fence_wait;
  2625. } else {
  2626. SDEDEV_DBG(rot_dev->dev, "fence exit s:%d.%d fd:%d\n",
  2627. ctx->session_id,
  2628. vbinfo_cap->fence_ts, vbinfo_out->fd);
  2629. }
  2630. }
  2631. /* fill in item work structure */
  2632. sde_rotator_get_item_from_ctx(ctx, &item);
  2633. item.flags |= SDE_ROTATION_EXT_DMA_BUF;
  2634. item.input.planes[0].fd = src_handle->fd;
  2635. item.input.planes[0].buffer = src_handle->buffer;
  2636. item.input.planes[0].offset = src_handle->addr;
  2637. item.input.planes[0].stride = ctx->format_out.fmt.pix.bytesperline;
  2638. item.input.plane_count = 1;
  2639. item.input.fence = NULL;
  2640. item.input.comp_ratio = vbinfo_out->comp_ratio;
  2641. item.output.planes[0].fd = dst_handle->fd;
  2642. item.output.planes[0].buffer = dst_handle->buffer;
  2643. item.output.planes[0].offset = dst_handle->addr;
  2644. item.output.planes[0].stride = ctx->format_cap.fmt.pix.bytesperline;
  2645. item.output.plane_count = 1;
  2646. item.output.fence = NULL;
  2647. item.output.comp_ratio = vbinfo_cap->comp_ratio;
  2648. item.sequence_id = vbinfo_cap->fence_ts;
  2649. item.ts = ts;
  2650. req = sde_rotator_req_init(rot_dev->mgr, ctx->private, &item, 1, 0);
  2651. if (IS_ERR_OR_NULL(req)) {
  2652. SDEDEV_ERR(rot_dev->dev, "fail allocate rotation request\n");
  2653. ret = -ENOMEM;
  2654. goto error_init_request;
  2655. }
  2656. req->retire_kw = ctx->work_queue.rot_kw;
  2657. req->retire_work = &request->retire_work;
  2658. ret = sde_rotator_handle_request_common(
  2659. rot_dev->mgr, ctx->private, req);
  2660. if (ret) {
  2661. SDEDEV_ERR(rot_dev->dev, "fail handle request\n");
  2662. goto error_handle_request;
  2663. }
  2664. sde_rotator_queue_request(rot_dev->mgr, ctx->private, req);
  2665. request->req = req;
  2666. request->sequence_id = item.sequence_id;
  2667. request->committed = true;
  2668. return 0;
  2669. error_handle_request:
  2670. devm_kfree(rot_dev->dev, req);
  2671. error_init_request:
  2672. error_fence_wait:
  2673. error_null_buffer:
  2674. request->req = NULL;
  2675. request->sequence_id = 0;
  2676. request->committed = false;
  2677. return ret;
  2678. }
  2679. /*
  2680. * sde_rotator_submit_handler - Invoked by m2m to submit job.
  2681. * @work: Pointer to work structure.
  2682. *
  2683. * This function is scheduled in work queue context.
  2684. */
  2685. static void sde_rotator_submit_handler(struct kthread_work *work)
  2686. {
  2687. struct sde_rotator_ctx *ctx;
  2688. struct sde_rotator_device *rot_dev;
  2689. struct vb2_v4l2_buffer *src_buf;
  2690. struct vb2_v4l2_buffer *dst_buf;
  2691. struct sde_rotator_request *request;
  2692. int ret;
  2693. request = container_of(work, struct sde_rotator_request, submit_work);
  2694. ctx = request->ctx;
  2695. if (!ctx || !ctx->rot_dev) {
  2696. SDEROT_ERR("null device\n");
  2697. return;
  2698. }
  2699. rot_dev = ctx->rot_dev;
  2700. SDEDEV_DBG(rot_dev->dev, "submit handler s:%d\n", ctx->session_id);
  2701. mutex_lock(&rot_dev->lock);
  2702. if (ctx->abort_pending) {
  2703. SDEDEV_DBG(rot_dev->dev, "abort command in submit s:%d\n",
  2704. ctx->session_id);
  2705. sde_rotator_update_retire_sequence(request);
  2706. sde_rotator_retire_request(request);
  2707. mutex_unlock(&rot_dev->lock);
  2708. return;
  2709. }
  2710. /* submit new request */
  2711. dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  2712. src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  2713. sde_rot_mgr_lock(rot_dev->mgr);
  2714. ret = sde_rotator_process_buffers(ctx, &src_buf->vb2_buf,
  2715. &dst_buf->vb2_buf, request);
  2716. sde_rot_mgr_unlock(rot_dev->mgr);
  2717. if (ret) {
  2718. SDEDEV_ERR(rot_dev->dev,
  2719. "fail process buffer in submit s:%d\n",
  2720. ctx->session_id);
  2721. /* advance to device run to clean up buffers */
  2722. v4l2_m2m_try_schedule(ctx->fh.m2m_ctx);
  2723. }
  2724. mutex_unlock(&rot_dev->lock);
  2725. }
  2726. /*
  2727. * sde_rotator_device_run - rotator m2m device run callback
  2728. * @priv: Pointer rotator context.
  2729. */
  2730. static void sde_rotator_device_run(void *priv)
  2731. {
  2732. struct sde_rotator_ctx *ctx = priv;
  2733. struct sde_rotator_device *rot_dev;
  2734. struct vb2_v4l2_buffer *src_buf;
  2735. struct vb2_v4l2_buffer *dst_buf;
  2736. struct sde_rotator_request *request;
  2737. int ret;
  2738. if (!ctx || !ctx->rot_dev) {
  2739. SDEROT_ERR("null context/device\n");
  2740. return;
  2741. }
  2742. rot_dev = ctx->rot_dev;
  2743. SDEDEV_DBG(rot_dev->dev, "device run s:%d\n", ctx->session_id);
  2744. if (rot_dev->early_submit) {
  2745. request = list_first_entry_or_null(&ctx->pending_list,
  2746. struct sde_rotator_request, list);
  2747. /* pending request mode, check for completion */
  2748. if (!request || IS_ERR_OR_NULL(request->req)) {
  2749. /* pending request fails or something wrong. */
  2750. SDEDEV_ERR(rot_dev->dev,
  2751. "pending request fail in device run s:%d\n",
  2752. ctx->session_id);
  2753. rot_dev->stats.fail_count++;
  2754. ATRACE_INT("fail_count", rot_dev->stats.fail_count);
  2755. goto error_process_buffers;
  2756. } else if (!atomic_read(&request->req->pending_count)) {
  2757. /* pending request completed. signal done. */
  2758. int failed_count =
  2759. atomic_read(&request->req->failed_count);
  2760. SDEDEV_DBG(rot_dev->dev,
  2761. "pending request completed in device run s:%d\n",
  2762. ctx->session_id);
  2763. /* disconnect request (will be freed by core layer) */
  2764. sde_rot_mgr_lock(rot_dev->mgr);
  2765. sde_rotator_req_finish(rot_dev->mgr, ctx->private,
  2766. request->req);
  2767. sde_rot_mgr_unlock(rot_dev->mgr);
  2768. if (failed_count) {
  2769. SDEDEV_ERR(rot_dev->dev,
  2770. "pending request failed in device run s:%d f:%d\n",
  2771. ctx->session_id,
  2772. failed_count);
  2773. rot_dev->stats.fail_count++;
  2774. ATRACE_INT("fail_count",
  2775. rot_dev->stats.fail_count);
  2776. goto error_process_buffers;
  2777. }
  2778. src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  2779. dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  2780. if (!src_buf || !dst_buf) {
  2781. SDEDEV_ERR(rot_dev->dev,
  2782. "null buffer in device run s:%d sb:%pK db:%pK\n",
  2783. ctx->session_id,
  2784. src_buf, dst_buf);
  2785. goto error_process_buffers;
  2786. }
  2787. sde_rotator_update_retire_sequence(request);
  2788. sde_rotator_retire_request(request);
  2789. v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
  2790. v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
  2791. v4l2_m2m_job_finish(rot_dev->m2m_dev, ctx->fh.m2m_ctx);
  2792. } else {
  2793. /* pending request not complete. something wrong. */
  2794. SDEDEV_ERR(rot_dev->dev,
  2795. "Incomplete pending request in device run s:%d\n",
  2796. ctx->session_id);
  2797. /* disconnect request (will be freed by core layer) */
  2798. sde_rot_mgr_lock(rot_dev->mgr);
  2799. sde_rotator_req_finish(rot_dev->mgr, ctx->private,
  2800. request->req);
  2801. sde_rot_mgr_unlock(rot_dev->mgr);
  2802. goto error_process_buffers;
  2803. }
  2804. } else {
  2805. request = list_first_entry_or_null(&ctx->retired_list,
  2806. struct sde_rotator_request, list);
  2807. if (!request) {
  2808. SDEDEV_ERR(rot_dev->dev,
  2809. "no free request in device run s:%d\n",
  2810. ctx->session_id);
  2811. goto error_retired_list;
  2812. }
  2813. spin_lock(&ctx->list_lock);
  2814. list_del_init(&request->list);
  2815. list_add_tail(&request->list, &ctx->pending_list);
  2816. spin_unlock(&ctx->list_lock);
  2817. /* no pending request. submit buffer the usual way. */
  2818. dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  2819. src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  2820. if (!src_buf || !dst_buf) {
  2821. SDEDEV_ERR(rot_dev->dev,
  2822. "null buffer in device run s:%d sb:%pK db:%pK\n",
  2823. ctx->session_id,
  2824. src_buf, dst_buf);
  2825. goto error_empty_buffer;
  2826. }
  2827. sde_rot_mgr_lock(rot_dev->mgr);
  2828. ret = sde_rotator_process_buffers(ctx, &src_buf->vb2_buf,
  2829. &dst_buf->vb2_buf, request);
  2830. sde_rot_mgr_unlock(rot_dev->mgr);
  2831. if (ret) {
  2832. SDEDEV_ERR(rot_dev->dev,
  2833. "fail process buffer in device run s:%d\n",
  2834. ctx->session_id);
  2835. rot_dev->stats.fail_count++;
  2836. ATRACE_INT("fail_count", rot_dev->stats.fail_count);
  2837. goto error_process_buffers;
  2838. }
  2839. }
  2840. return;
  2841. error_process_buffers:
  2842. error_empty_buffer:
  2843. error_retired_list:
  2844. sde_rotator_update_retire_sequence(request);
  2845. sde_rotator_retire_request(request);
  2846. src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  2847. dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  2848. if (src_buf)
  2849. v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
  2850. if (dst_buf)
  2851. v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
  2852. sde_rotator_resync_timeline(ctx->work_queue.timeline);
  2853. v4l2_m2m_job_finish(rot_dev->m2m_dev, ctx->fh.m2m_ctx);
  2854. }
  2855. /*
  2856. * sde_rotator_job_abort - rotator m2m job abort callback
  2857. * @priv: Pointer rotator context.
  2858. */
  2859. static void sde_rotator_job_abort(void *priv)
  2860. {
  2861. struct sde_rotator_ctx *ctx = priv;
  2862. struct sde_rotator_device *rot_dev;
  2863. if (!ctx || !ctx->rot_dev) {
  2864. SDEROT_ERR("null context/device\n");
  2865. return;
  2866. }
  2867. rot_dev = ctx->rot_dev;
  2868. SDEDEV_DBG(rot_dev->dev, "job abort s:%d\n", ctx->session_id);
  2869. v4l2_m2m_job_finish(rot_dev->m2m_dev, ctx->fh.m2m_ctx);
  2870. }
  2871. /*
  2872. * sde_rotator_job_ready - rotator m2m job ready callback
  2873. * @priv: Pointer rotator context.
  2874. */
  2875. static int sde_rotator_job_ready(void *priv)
  2876. {
  2877. struct sde_rotator_ctx *ctx = priv;
  2878. struct sde_rotator_device *rot_dev;
  2879. struct sde_rotator_request *request;
  2880. int ret = 0;
  2881. if (!ctx || !ctx->rot_dev) {
  2882. SDEROT_ERR("null context/device\n");
  2883. return 0;
  2884. }
  2885. rot_dev = ctx->rot_dev;
  2886. SDEDEV_DBG(rot_dev->dev, "job ready s:%d\n", ctx->session_id);
  2887. request = list_first_entry_or_null(&ctx->pending_list,
  2888. struct sde_rotator_request, list);
  2889. if (!rot_dev->early_submit) {
  2890. /* always ready in normal mode. */
  2891. ret = 1;
  2892. } else if (request && IS_ERR_OR_NULL(request->req)) {
  2893. /* if pending request fails, forward to device run state. */
  2894. SDEDEV_DBG(rot_dev->dev,
  2895. "pending request fail in job ready s:%d\n",
  2896. ctx->session_id);
  2897. ret = 1;
  2898. } else if (list_empty(&ctx->pending_list)) {
  2899. /* if no pending request, submit a new request. */
  2900. SDEDEV_DBG(rot_dev->dev,
  2901. "submit job s:%d sc:%d dc:%d p:%d\n",
  2902. ctx->session_id,
  2903. v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx),
  2904. v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx),
  2905. !list_empty(&ctx->pending_list));
  2906. request = list_first_entry_or_null(&ctx->retired_list,
  2907. struct sde_rotator_request, list);
  2908. if (!request) {
  2909. /* should not happen */
  2910. SDEDEV_ERR(rot_dev->dev,
  2911. "no free request in job ready s:%d\n",
  2912. ctx->session_id);
  2913. } else {
  2914. spin_lock(&ctx->list_lock);
  2915. list_del_init(&request->list);
  2916. list_add_tail(&request->list, &ctx->pending_list);
  2917. spin_unlock(&ctx->list_lock);
  2918. kthread_queue_work(ctx->work_queue.rot_kw,
  2919. &request->submit_work);
  2920. }
  2921. } else if (request && !atomic_read(&request->req->pending_count)) {
  2922. /* if pending request completed, forward to device run state */
  2923. SDEDEV_DBG(rot_dev->dev,
  2924. "pending request completed in job ready s:%d\n",
  2925. ctx->session_id);
  2926. ret = 1;
  2927. }
  2928. return ret;
  2929. }
  2930. /* V4l2 mem2mem handlers */
  2931. static struct v4l2_m2m_ops sde_rotator_m2m_ops = {
  2932. .device_run = sde_rotator_device_run,
  2933. .job_abort = sde_rotator_job_abort,
  2934. .job_ready = sde_rotator_job_ready,
  2935. };
  2936. /* Device tree match struct */
  2937. static const struct of_device_id sde_rotator_dt_match[] = {
  2938. {
  2939. .compatible = "qcom,sde_rotator",
  2940. .data = NULL,
  2941. },
  2942. {}
  2943. };
  2944. /*
  2945. * sde_rotator_get_drv_data - rotator device driver data.
  2946. * @dev: Pointer to device.
  2947. */
  2948. static const void *sde_rotator_get_drv_data(struct device *dev)
  2949. {
  2950. const struct of_device_id *match;
  2951. match = of_match_node(sde_rotator_dt_match, dev->of_node);
  2952. if (match)
  2953. return match->data;
  2954. return NULL;
  2955. }
  2956. /*
  2957. * sde_rotator_probe - rotator device probe method.
  2958. * @pdev: Pointer to rotator platform device.
  2959. */
  2960. static int sde_rotator_probe(struct platform_device *pdev)
  2961. {
  2962. struct sde_rotator_device *rot_dev;
  2963. struct video_device *vdev;
  2964. int ret, i;
  2965. char name[32];
  2966. SDEDEV_DBG(&pdev->dev, "SDE v4l2 rotator probed\n");
  2967. /* sde rotator device struct */
  2968. rot_dev = kzalloc(sizeof(struct sde_rotator_device), GFP_KERNEL);
  2969. if (!rot_dev)
  2970. return -ENOMEM;
  2971. mutex_init(&rot_dev->lock);
  2972. rot_dev->early_submit = SDE_ROTATOR_EARLY_SUBMIT;
  2973. rot_dev->fence_timeout = SDE_ROTATOR_FENCE_TIMEOUT;
  2974. rot_dev->streamoff_timeout = SDE_ROTATOR_STREAM_OFF_TIMEOUT;
  2975. rot_dev->min_rot_clk = 0;
  2976. rot_dev->min_bw = 0;
  2977. rot_dev->min_overhead_us = 0;
  2978. rot_dev->drvdata = sde_rotator_get_drv_data(&pdev->dev);
  2979. rot_dev->open_timeout = SDE_ROTATOR_CTX_OPEN_TIMEOUT;
  2980. init_waitqueue_head(&rot_dev->open_wq);
  2981. rot_dev->pdev = pdev;
  2982. rot_dev->dev = &pdev->dev;
  2983. platform_set_drvdata(pdev, rot_dev);
  2984. ret = sde_rotator_base_init(&rot_dev->mdata, pdev, rot_dev->drvdata);
  2985. if (ret < 0) {
  2986. SDEDEV_ERR(&pdev->dev, "fail init base data %d\n", ret);
  2987. goto error_rotator_base_init;
  2988. }
  2989. ret = sde_rotator_core_init(&rot_dev->mgr, pdev);
  2990. if (ret < 0) {
  2991. if (ret == -EPROBE_DEFER)
  2992. SDEDEV_INFO(&pdev->dev, "probe defer for core init\n");
  2993. else
  2994. SDEDEV_ERR(&pdev->dev, "fail init core %d\n", ret);
  2995. goto error_rotator_core_init;
  2996. }
  2997. /* mem2mem device */
  2998. rot_dev->m2m_dev = v4l2_m2m_init(&sde_rotator_m2m_ops);
  2999. if (IS_ERR(rot_dev->m2m_dev)) {
  3000. ret = PTR_ERR(rot_dev->m2m_dev);
  3001. SDEDEV_ERR(&pdev->dev, "fail init mem2mem device %d\n", ret);
  3002. goto error_m2m_init;
  3003. }
  3004. /* v4l2 device */
  3005. ret = v4l2_device_register(&pdev->dev, &rot_dev->v4l2_dev);
  3006. if (ret < 0) {
  3007. SDEDEV_ERR(&pdev->dev, "fail register v4l2 device %d\n", ret);
  3008. goto error_v4l2_register;
  3009. }
  3010. vdev = video_device_alloc();
  3011. if (!vdev) {
  3012. SDEDEV_ERR(&pdev->dev, "fail allocate video device\n");
  3013. goto error_alloc_video_device;
  3014. }
  3015. vdev->fops = &sde_rotator_fops;
  3016. vdev->ioctl_ops = &sde_rotator_ioctl_ops;
  3017. vdev->lock = &rot_dev->lock;
  3018. vdev->minor = -1;
  3019. vdev->release = video_device_release;
  3020. vdev->v4l2_dev = &rot_dev->v4l2_dev;
  3021. vdev->vfl_dir = VFL_DIR_M2M;
  3022. vdev->vfl_type = VFL_TYPE_GRABBER;
  3023. strlcpy(vdev->name, SDE_ROTATOR_DRV_NAME, sizeof(vdev->name));
  3024. ret = video_register_device(vdev, VFL_TYPE_GRABBER,
  3025. SDE_ROTATOR_BASE_DEVICE_NUMBER);
  3026. if (ret < 0) {
  3027. SDEDEV_ERR(&pdev->dev, "fail register video device %d\n",
  3028. ret);
  3029. goto error_video_register;
  3030. }
  3031. rot_dev->vdev = vdev;
  3032. video_set_drvdata(rot_dev->vdev, rot_dev);
  3033. rot_dev->debugfs_root = sde_rotator_create_debugfs(rot_dev);
  3034. for (i = 0; i < MAX_ROT_OPEN_SESSION; i++) {
  3035. snprintf(name, sizeof(name), "rot_fenceq_%d_%d",
  3036. rot_dev->dev->id, i);
  3037. kthread_init_worker(&rot_dev->rot_kw[i]);
  3038. rot_dev->rot_thread[i] = kthread_run(kthread_worker_fn,
  3039. &rot_dev->rot_kw[i], name);
  3040. if (IS_ERR(rot_dev->rot_thread[i])) {
  3041. SDEDEV_ERR(rot_dev->dev,
  3042. "fail allocate kthread i:%d\n", i);
  3043. ret = -EPERM;
  3044. goto error_kthread_create;
  3045. }
  3046. rot_dev->kthread_free[i] = true;
  3047. }
  3048. SDEDEV_INFO(&pdev->dev, "SDE v4l2 rotator probe success\n");
  3049. return 0;
  3050. error_kthread_create:
  3051. for (i--; i >= 0; i--)
  3052. kthread_stop(rot_dev->rot_thread[i]);
  3053. sde_rotator_destroy_debugfs(rot_dev->debugfs_root);
  3054. video_unregister_device(rot_dev->vdev);
  3055. error_video_register:
  3056. video_device_release(vdev);
  3057. error_alloc_video_device:
  3058. v4l2_device_unregister(&rot_dev->v4l2_dev);
  3059. error_v4l2_register:
  3060. v4l2_m2m_release(rot_dev->m2m_dev);
  3061. error_m2m_init:
  3062. sde_rotator_core_destroy(rot_dev->mgr);
  3063. error_rotator_core_init:
  3064. sde_rotator_base_destroy(rot_dev->mdata);
  3065. error_rotator_base_init:
  3066. kfree(rot_dev);
  3067. return ret;
  3068. }
  3069. /*
  3070. * sde_rotator_remove - rotator device remove method.
  3071. * @pdev: Pointer rotator platform device.
  3072. */
  3073. static int sde_rotator_remove(struct platform_device *pdev)
  3074. {
  3075. struct sde_rotator_device *rot_dev;
  3076. int i;
  3077. rot_dev = platform_get_drvdata(pdev);
  3078. if (rot_dev == NULL) {
  3079. SDEDEV_ERR(&pdev->dev, "fail get rotator drvdata\n");
  3080. return 0;
  3081. }
  3082. for (i = MAX_ROT_OPEN_SESSION - 1; i >= 0; i--)
  3083. kthread_stop(rot_dev->rot_thread[i]);
  3084. sde_rotator_destroy_debugfs(rot_dev->debugfs_root);
  3085. video_unregister_device(rot_dev->vdev);
  3086. video_device_release(rot_dev->vdev);
  3087. v4l2_device_unregister(&rot_dev->v4l2_dev);
  3088. v4l2_m2m_release(rot_dev->m2m_dev);
  3089. sde_rotator_core_destroy(rot_dev->mgr);
  3090. sde_rotator_base_destroy(rot_dev->mdata);
  3091. kfree(rot_dev);
  3092. return 0;
  3093. }
  3094. static const struct dev_pm_ops sde_rotator_pm_ops = {
  3095. SET_SYSTEM_SLEEP_PM_OPS(sde_rotator_pm_suspend, sde_rotator_pm_resume)
  3096. SET_RUNTIME_PM_OPS(sde_rotator_runtime_suspend,
  3097. sde_rotator_runtime_resume,
  3098. sde_rotator_runtime_idle)
  3099. };
  3100. /* SDE Rotator platform driver definition */
  3101. static struct platform_driver rotator_driver = {
  3102. .probe = sde_rotator_probe,
  3103. .remove = sde_rotator_remove,
  3104. .suspend = sde_rotator_suspend,
  3105. .resume = sde_rotator_resume,
  3106. .driver = {
  3107. .name = SDE_ROTATOR_DRV_NAME,
  3108. .of_match_table = sde_rotator_dt_match,
  3109. .pm = &sde_rotator_pm_ops,
  3110. },
  3111. };
  3112. void __init sde_rotator_register(void)
  3113. {
  3114. platform_driver_register(&rotator_driver);
  3115. }
  3116. void __exit sde_rotator_unregister(void)
  3117. {
  3118. platform_driver_unregister(&rotator_driver);
  3119. }