sde_rotator_dev.c 100 KB

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