sde_rotator_dev.c 100 KB

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