sde_rotator_dev.c 100 KB

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