sde_rotator_dev.c 100 KB

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