sde_rotator_dev.c 100 KB

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