sde_rotator_dev.c 100 KB

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