msm_vidc_driver.c 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/iommu.h>
  6. #include <linux/workqueue.h>
  7. #include <media/v4l2_vidc_extensions.h>
  8. #include "msm_media_info.h"
  9. #include "msm_vidc_driver.h"
  10. #include "msm_vidc_platform.h"
  11. #include "msm_vidc_internal.h"
  12. #include "msm_vidc_control.h"
  13. #include "msm_vidc_memory.h"
  14. #include "msm_vidc_debug.h"
  15. #include "msm_vidc_power.h"
  16. #include "msm_vidc.h"
  17. #include "msm_vdec.h"
  18. #include "msm_venc.h"
  19. #include "venus_hfi.h"
  20. #include "venus_hfi_response.h"
  21. #include "hfi_packet.h"
  22. extern struct msm_vidc_core *g_core;
  23. #define COUNT_BITS(a, out) { \
  24. while ((a) >= 1) { \
  25. (out) += (a) & (1); \
  26. (a) >>= (1); \
  27. } \
  28. }
  29. struct msm_vidc_buf_type_name {
  30. enum msm_vidc_buffer_type type;
  31. char *name;
  32. };
  33. struct msm_vidc_cap_name {
  34. enum msm_vidc_inst_capability_type cap;
  35. char *name;
  36. };
  37. static const struct msm_vidc_cap_name cap_name_arr[] = {
  38. {INST_CAP_NONE, "INST_CAP_NONE" },
  39. {FRAME_WIDTH, "FRAME_WIDTH" },
  40. {LOSSLESS_FRAME_WIDTH, "LOSSLESS_FRAME_WIDTH" },
  41. {SECURE_FRAME_WIDTH, "SECURE_FRAME_WIDTH" },
  42. {FRAME_HEIGHT, "FRAME_HEIGHT" },
  43. {LOSSLESS_FRAME_HEIGHT, "LOSSLESS_FRAME_HEIGHT" },
  44. {SECURE_FRAME_HEIGHT, "SECURE_FRAME_HEIGHT" },
  45. {PIX_FMTS, "PIX_FMTS" },
  46. {MIN_BUFFERS_INPUT, "MIN_BUFFERS_INPUT" },
  47. {MIN_BUFFERS_OUTPUT, "MIN_BUFFERS_OUTPUT" },
  48. {MBPF, "MBPF" },
  49. {LOSSLESS_MBPF, "LOSSLESS_MBPF" },
  50. {BATCH_MBPF, "BATCH_MBPF" },
  51. {BATCH_FPS, "BATCH_FPS" },
  52. {SECURE_MBPF, "SECURE_MBPF" },
  53. {MBPS, "MBPS" },
  54. {POWER_SAVE_MBPS, "POWER_SAVE_MBPS" },
  55. {FRAME_RATE, "FRAME_RATE" },
  56. {OPERATING_RATE, "OPERATING_RATE" },
  57. {SCALE_X, "SCALE_X" },
  58. {SCALE_Y, "SCALE_Y" },
  59. {MB_CYCLES_VSP, "MB_CYCLES_VSP" },
  60. {MB_CYCLES_VPP, "MB_CYCLES_VPP" },
  61. {MB_CYCLES_LP, "MB_CYCLES_LP" },
  62. {MB_CYCLES_FW, "MB_CYCLES_FW" },
  63. {MB_CYCLES_FW_VPP, "MB_CYCLES_FW_VPP" },
  64. {SECURE_MODE, "SECURE_MODE" },
  65. {HFLIP, "HFLIP" },
  66. {VFLIP, "VFLIP" },
  67. {ROTATION, "ROTATION" },
  68. {SUPER_FRAME, "SUPER_FRAME" },
  69. {SLICE_INTERFACE, "SLICE_INTERFACE" },
  70. {HEADER_MODE, "HEADER_MODE" },
  71. {PREPEND_SPSPPS_TO_IDR, "PREPEND_SPSPPS_TO_IDR" },
  72. {META_SEQ_HDR_NAL, "META_SEQ_HDR_NAL" },
  73. {WITHOUT_STARTCODE, "WITHOUT_STARTCODE" },
  74. {NAL_LENGTH_FIELD, "NAL_LENGTH_FIELD" },
  75. {REQUEST_I_FRAME, "REQUEST_I_FRAME" },
  76. {BIT_RATE, "BIT_RATE" },
  77. {BITRATE_MODE, "BITRATE_MODE" },
  78. {LOSSLESS, "LOSSLESS" },
  79. {FRAME_SKIP_MODE, "FRAME_SKIP_MODE" },
  80. {FRAME_RC_ENABLE, "FRAME_RC_ENABLE" },
  81. {CONSTANT_QUALITY, "CONSTANT_QUALITY" },
  82. {GOP_SIZE, "GOP_SIZE" },
  83. {GOP_CLOSURE, "GOP_CLOSURE" },
  84. {B_FRAME, "B_FRAME" },
  85. {BLUR_TYPES, "BLUR_TYPES" },
  86. {BLUR_RESOLUTION, "BLUR_RESOLUTION" },
  87. {CSC, "CSC" },
  88. {CSC_CUSTOM_MATRIX, "CSC_CUSTOM_MATRIX" },
  89. {GRID, "GRID" },
  90. {LOWLATENCY_MODE, "LOWLATENCY_MODE" },
  91. {LTR_COUNT, "LTR_COUNT" },
  92. {USE_LTR, "USE_LTR" },
  93. {MARK_LTR, "MARK_LTR" },
  94. {BASELAYER_PRIORITY, "BASELAYER_PRIORITY" },
  95. {IR_RANDOM, "IR_RANDOM" },
  96. {AU_DELIMITER, "AU_DELIMITER" },
  97. {TIME_DELTA_BASED_RC, "TIME_DELTA_BASED_RC" },
  98. {CONTENT_ADAPTIVE_CODING, "CONTENT_ADAPTIVE_CODING" },
  99. {BITRATE_BOOST, "BITRATE_BOOST" },
  100. {VBV_DELAY, "VBV_DELAY" },
  101. {MIN_FRAME_QP, "MIN_FRAME_QP" },
  102. {I_FRAME_MIN_QP, "I_FRAME_MIN_QP" },
  103. {P_FRAME_MIN_QP, "P_FRAME_MIN_QP" },
  104. {B_FRAME_MIN_QP, "B_FRAME_MIN_QP" },
  105. {MAX_FRAME_QP, "MAX_FRAME_QP" },
  106. {I_FRAME_MAX_QP, "I_FRAME_MAX_QP" },
  107. {P_FRAME_MAX_QP, "P_FRAME_MAX_QP" },
  108. {B_FRAME_MAX_QP, "B_FRAME_MAX_QP" },
  109. {HEVC_HIER_QP, "HEVC_HIER_QP" },
  110. {I_FRAME_QP, "I_FRAME_QP" },
  111. {P_FRAME_QP, "P_FRAME_QP" },
  112. {B_FRAME_QP, "B_FRAME_QP" },
  113. {L0_QP, "L0_QP" },
  114. {L1_QP, "L1_QP" },
  115. {L2_QP, "L2_QP" },
  116. {L3_QP, "L3_QP" },
  117. {L4_QP, "L4_QP" },
  118. {L5_QP, "L5_QP" },
  119. {HIER_LAYER_QP, "HIER_LAYER_QP" },
  120. {LAYER_TYPE, "LAYER_TYPE" },
  121. {LAYER_ENABLE, "LAYER_ENABLE" },
  122. {ENH_LAYER_COUNT, "ENH_LAYER_COUNT" },
  123. {L0_BR, "L0_BR" },
  124. {L1_BR, "L1_BR" },
  125. {L2_BR, "L2_BR" },
  126. {L3_BR, "L3_BR" },
  127. {L4_BR, "L4_BR" },
  128. {L5_BR, "L5_BR" },
  129. {ENTROPY_MODE, "ENTROPY_MODE" },
  130. {PROFILE, "PROFILE" },
  131. {LEVEL, "LEVEL" },
  132. {HEVC_TIER, "HEVC_TIER" },
  133. {LF_MODE, "LF_MODE" },
  134. {LF_ALPHA, "LF_ALPHA" },
  135. {LF_BETA, "LF_BETA" },
  136. {SLICE_MODE, "SLICE_MODE" },
  137. {SLICE_MAX_BYTES, "SLICE_MAX_BYTES" },
  138. {SLICE_MAX_MB, "SLICE_MAX_MB" },
  139. {MB_RC, "MB_RC" },
  140. {TRANSFORM_8X8, "TRANSFORM_8X8" },
  141. {CHROMA_QP_INDEX_OFFSET, "CHROMA_QP_INDEX_OFFSET" },
  142. {DISPLAY_DELAY_ENABLE, "DISPLAY_DELAY_ENABLE" },
  143. {DISPLAY_DELAY, "DISPLAY_DELAY" },
  144. {CONCEAL_COLOR_8BIT, "CONCEAL_COLOR_8BIT" },
  145. {CONCEAL_COLOR_10BIT, "CONCEAL_COLOR_10BIT" },
  146. {STAGE, "STAGE" },
  147. {PIPE, "PIPE" },
  148. {POC, "POC" },
  149. {QUALITY_MODE, "QUALITY_MODE" },
  150. {CODED_FRAMES, "CODED_FRAMES" },
  151. {BIT_DEPTH, "BIT_DEPTH" },
  152. {CODEC_CONFIG, "CODEC_CONFIG" },
  153. {BITSTREAM_SIZE_OVERWRITE, "BITSTREAM_SIZE_OVERWRITE" },
  154. {THUMBNAIL_MODE, "THUMBNAIL_MODE" },
  155. {DEFAULT_HEADER, "DEFAULT_HEADER" },
  156. {RAP_FRAME, "RAP_FRAME" },
  157. {SEQ_CHANGE_AT_SYNC_FRAME, "SEQ_CHANGE_AT_SYNC_FRAME" },
  158. {PRIORITY, "PRIORITY" },
  159. {ENC_IP_CR, "ENC_IP_CR" },
  160. {META_LTR_MARK_USE, "META_LTR_MARK_USE" },
  161. {META_DPB_MISR, "META_DPB_MISR" },
  162. {META_OPB_MISR, "META_OPB_MISR" },
  163. {META_INTERLACE, "META_INTERLACE" },
  164. {META_TIMESTAMP, "META_TIMESTAMP" },
  165. {META_CONCEALED_MB_CNT, "META_CONCEALED_MB_CNT" },
  166. {META_HIST_INFO, "META_HIST_INFO" },
  167. {META_SEI_MASTERING_DISP, "META_SEI_MASTERING_DISP" },
  168. {META_SEI_CLL, "META_SEI_CLL" },
  169. {META_HDR10PLUS, "META_HDR10PLUS" },
  170. {META_EVA_STATS, "META_EVA_STATS" },
  171. {META_BUF_TAG, "META_BUF_TAG" },
  172. {META_DPB_TAG_LIST, "META_DPB_TAG_LIST" },
  173. {META_SUBFRAME_OUTPUT, "META_SUBFRAME_OUTPUT" },
  174. {META_ENC_QP_METADATA, "META_ENC_QP_METADATA" },
  175. {META_ROI_INFO, "META_ROI_INFO" },
  176. {INST_CAP_MAX, "INST_CAP_MAX" },
  177. };
  178. const char *cap_name(enum msm_vidc_inst_capability_type cap)
  179. {
  180. const char *name = "UNKNOWN CAP";
  181. if (cap > ARRAY_SIZE(cap_name_arr))
  182. goto exit;
  183. if (cap_name_arr[cap].cap != cap)
  184. goto exit;
  185. name = cap_name_arr[cap].name;
  186. exit:
  187. return name;
  188. }
  189. struct msm_vidc_inst_state_name {
  190. enum msm_vidc_inst_state state;
  191. char *name;
  192. };
  193. static const struct msm_vidc_inst_state_name inst_state_name_arr[] = {
  194. {MSM_VIDC_OPEN, "OPEN" },
  195. {MSM_VIDC_START_INPUT, "START_INPUT" },
  196. {MSM_VIDC_START_OUTPUT, "START_OUTPUT" },
  197. {MSM_VIDC_START, "START" },
  198. {MSM_VIDC_DRC, "DRC" },
  199. {MSM_VIDC_DRC_LAST_FLAG, "DRC_LAST_FLAG" },
  200. {MSM_VIDC_DRAIN, "DRAIN" },
  201. {MSM_VIDC_DRAIN_LAST_FLAG, "DRAIN_LAST_FLAG" },
  202. {MSM_VIDC_DRC_DRAIN, "DRC_DRAIN" },
  203. {MSM_VIDC_DRC_DRAIN_LAST_FLAG, "DRC_DRAIN_LAST_FLAG" },
  204. {MSM_VIDC_DRAIN_START_INPUT, "DRAIN_START_INPUT" },
  205. {MSM_VIDC_ERROR, "ERROR" },
  206. };
  207. const char *state_name(enum msm_vidc_inst_state state)
  208. {
  209. const char *name = "UNKNOWN STATE";
  210. if (!state || state > ARRAY_SIZE(inst_state_name_arr))
  211. goto exit;
  212. if (inst_state_name_arr[state - 1].state != state)
  213. goto exit;
  214. name = inst_state_name_arr[state - 1].name;
  215. exit:
  216. return name;
  217. }
  218. struct msm_vidc_core_state_name {
  219. enum msm_vidc_core_state state;
  220. char *name;
  221. };
  222. static const struct msm_vidc_core_state_name core_state_name_arr[] = {
  223. {MSM_VIDC_CORE_DEINIT, "CORE_DEINIT" },
  224. {MSM_VIDC_CORE_INIT, "CORE_INIT" },
  225. };
  226. const char *core_state_name(enum msm_vidc_core_state state)
  227. {
  228. const char *name = "UNKNOWN STATE";
  229. if (state >= ARRAY_SIZE(core_state_name_arr))
  230. goto exit;
  231. if (core_state_name_arr[state].state != state)
  232. goto exit;
  233. name = core_state_name_arr[state].name;
  234. exit:
  235. return name;
  236. }
  237. void print_vidc_buffer(u32 tag, const char *tag_str, const char *str, struct msm_vidc_inst *inst,
  238. struct msm_vidc_buffer *vbuf)
  239. {
  240. if (!(tag & msm_vidc_debug) || !inst || !vbuf || !tag_str || !str)
  241. return;
  242. if (vbuf->type == MSM_VIDC_BUF_INPUT || vbuf->type == MSM_VIDC_BUF_OUTPUT) {
  243. dprintk_inst(tag, tag_str, inst,
  244. "%s: %s: idx %2d fd %3d off %d daddr %#llx size %d filled %d flags %#x ts %lld attr %#x\n",
  245. str, vbuf->type == MSM_VIDC_BUF_INPUT ? "INPUT" : "OUTPUT",
  246. vbuf->index, vbuf->fd, vbuf->data_offset,
  247. vbuf->device_addr, vbuf->buffer_size, vbuf->data_size,
  248. vbuf->flags, vbuf->timestamp, vbuf->attr);
  249. } else if (vbuf->type == MSM_VIDC_BUF_INPUT_META ||
  250. vbuf->type == MSM_VIDC_BUF_OUTPUT_META) {
  251. dprintk_inst(tag, tag_str, inst,
  252. "%s: %s: idx %2d fd %3d off %d daddr %#llx size %d filled %d flags %#x ts %lld attr %#x\n",
  253. str, vbuf->type == MSM_VIDC_BUF_INPUT_META ? "INPUT_META" : "OUTPUT_META",
  254. vbuf->index, vbuf->fd, vbuf->data_offset,
  255. vbuf->device_addr, vbuf->buffer_size, vbuf->data_size,
  256. vbuf->flags, vbuf->timestamp, vbuf->attr);
  257. }
  258. }
  259. void print_vb2_buffer(const char *str, struct msm_vidc_inst *inst,
  260. struct vb2_buffer *vb2)
  261. {
  262. if (!inst || !vb2)
  263. return;
  264. if (vb2->type == INPUT_MPLANE || vb2->type == OUTPUT_MPLANE) {
  265. i_vpr_e(inst,
  266. "%s: %s: idx %2d fd %d off %d size %d filled %d\n",
  267. str, vb2->type == INPUT_MPLANE ? "INPUT" : "OUTPUT",
  268. vb2->index, vb2->planes[0].m.fd,
  269. vb2->planes[0].data_offset, vb2->planes[0].length,
  270. vb2->planes[0].bytesused);
  271. } else if (vb2->type == INPUT_META_PLANE || vb2->type == OUTPUT_META_PLANE) {
  272. i_vpr_e(inst,
  273. "%s: %s: idx %2d fd %d off %d size %d filled %d\n",
  274. str, vb2->type == INPUT_MPLANE ? "INPUT_META" : "OUTPUT_META",
  275. vb2->index, vb2->planes[0].m.fd,
  276. vb2->planes[0].data_offset, vb2->planes[0].length,
  277. vb2->planes[0].bytesused);
  278. }
  279. }
  280. enum msm_vidc_buffer_type v4l2_type_to_driver(u32 type, const char *func)
  281. {
  282. enum msm_vidc_buffer_type buffer_type = 0;
  283. switch (type) {
  284. case INPUT_MPLANE:
  285. buffer_type = MSM_VIDC_BUF_INPUT;
  286. break;
  287. case OUTPUT_MPLANE:
  288. buffer_type = MSM_VIDC_BUF_OUTPUT;
  289. break;
  290. case INPUT_META_PLANE:
  291. buffer_type = MSM_VIDC_BUF_INPUT_META;
  292. break;
  293. case OUTPUT_META_PLANE:
  294. buffer_type = MSM_VIDC_BUF_OUTPUT_META;
  295. break;
  296. default:
  297. d_vpr_e("%s: invalid v4l2 buffer type %#x\n", func, type);
  298. break;
  299. }
  300. return buffer_type;
  301. }
  302. u32 v4l2_type_from_driver(enum msm_vidc_buffer_type buffer_type,
  303. const char *func)
  304. {
  305. u32 type = 0;
  306. switch (buffer_type) {
  307. case MSM_VIDC_BUF_INPUT:
  308. type = INPUT_MPLANE;
  309. break;
  310. case MSM_VIDC_BUF_OUTPUT:
  311. type = OUTPUT_MPLANE;
  312. break;
  313. case MSM_VIDC_BUF_INPUT_META:
  314. type = INPUT_META_PLANE;
  315. break;
  316. case MSM_VIDC_BUF_OUTPUT_META:
  317. type = OUTPUT_META_PLANE;
  318. break;
  319. default:
  320. d_vpr_e("%s: invalid driver buffer type %d\n",
  321. func, buffer_type);
  322. break;
  323. }
  324. return type;
  325. }
  326. enum msm_vidc_codec_type v4l2_codec_to_driver(u32 v4l2_codec, const char *func)
  327. {
  328. enum msm_vidc_codec_type codec = 0;
  329. switch (v4l2_codec) {
  330. case V4L2_PIX_FMT_H264:
  331. codec = MSM_VIDC_H264;
  332. break;
  333. case V4L2_PIX_FMT_HEVC:
  334. codec = MSM_VIDC_HEVC;
  335. break;
  336. case V4L2_PIX_FMT_VP9:
  337. codec = MSM_VIDC_VP9;
  338. break;
  339. case V4L2_PIX_FMT_HEIC:
  340. codec = MSM_VIDC_HEIC;
  341. break;
  342. default:
  343. d_vpr_e("%s: invalid v4l2 codec %#x\n", func, v4l2_codec);
  344. break;
  345. }
  346. return codec;
  347. }
  348. u32 v4l2_codec_from_driver(enum msm_vidc_codec_type codec, const char *func)
  349. {
  350. u32 v4l2_codec = 0;
  351. switch (codec) {
  352. case MSM_VIDC_H264:
  353. v4l2_codec = V4L2_PIX_FMT_H264;
  354. break;
  355. case MSM_VIDC_HEVC:
  356. v4l2_codec = V4L2_PIX_FMT_HEVC;
  357. break;
  358. case MSM_VIDC_VP9:
  359. v4l2_codec = V4L2_PIX_FMT_VP9;
  360. break;
  361. case MSM_VIDC_HEIC:
  362. v4l2_codec = V4L2_PIX_FMT_HEIC;
  363. break;
  364. default:
  365. d_vpr_e("%s: invalid driver codec %#x\n", func, codec);
  366. break;
  367. }
  368. return v4l2_codec;
  369. }
  370. enum msm_vidc_colorformat_type v4l2_colorformat_to_driver(u32 v4l2_colorformat,
  371. const char *func)
  372. {
  373. enum msm_vidc_colorformat_type colorformat = 0;
  374. switch (v4l2_colorformat) {
  375. case V4L2_PIX_FMT_NV12:
  376. colorformat = MSM_VIDC_FMT_NV12;
  377. break;
  378. case V4L2_PIX_FMT_NV21:
  379. colorformat = MSM_VIDC_FMT_NV21;
  380. break;
  381. case V4L2_PIX_FMT_VIDC_NV12C:
  382. colorformat = MSM_VIDC_FMT_NV12C;
  383. break;
  384. case V4L2_PIX_FMT_VIDC_TP10C:
  385. colorformat = MSM_VIDC_FMT_TP10C;
  386. break;
  387. case V4L2_PIX_FMT_RGBA32:
  388. colorformat = MSM_VIDC_FMT_RGBA8888;
  389. break;
  390. case V4L2_PIX_FMT_VIDC_ARGB32C:
  391. colorformat = MSM_VIDC_FMT_RGBA8888C;
  392. break;
  393. case V4L2_PIX_FMT_VIDC_P010:
  394. colorformat = MSM_VIDC_FMT_P010;
  395. break;
  396. default:
  397. d_vpr_e("%s: invalid v4l2 color format %#x\n",
  398. func, v4l2_colorformat);
  399. break;
  400. }
  401. return colorformat;
  402. }
  403. u32 v4l2_colorformat_from_driver(enum msm_vidc_colorformat_type colorformat,
  404. const char *func)
  405. {
  406. u32 v4l2_colorformat = 0;
  407. switch (colorformat) {
  408. case MSM_VIDC_FMT_NV12:
  409. v4l2_colorformat = V4L2_PIX_FMT_NV12;
  410. break;
  411. case MSM_VIDC_FMT_NV21:
  412. v4l2_colorformat = V4L2_PIX_FMT_NV21;
  413. break;
  414. case MSM_VIDC_FMT_NV12C:
  415. v4l2_colorformat = V4L2_PIX_FMT_VIDC_NV12C;
  416. break;
  417. case MSM_VIDC_FMT_TP10C:
  418. v4l2_colorformat = V4L2_PIX_FMT_VIDC_TP10C;
  419. break;
  420. case MSM_VIDC_FMT_RGBA8888:
  421. v4l2_colorformat = V4L2_PIX_FMT_RGBA32;
  422. break;
  423. case MSM_VIDC_FMT_RGBA8888C:
  424. v4l2_colorformat = V4L2_PIX_FMT_VIDC_ARGB32C;
  425. break;
  426. case MSM_VIDC_FMT_P010:
  427. v4l2_colorformat = V4L2_PIX_FMT_VIDC_P010;
  428. break;
  429. default:
  430. d_vpr_e("%s: invalid driver color format %#x\n",
  431. func, colorformat);
  432. break;
  433. }
  434. return v4l2_colorformat;
  435. }
  436. u32 v4l2_color_primaries_to_driver(struct msm_vidc_inst *inst,
  437. u32 v4l2_primaries, const char *func)
  438. {
  439. u32 vidc_color_primaries = MSM_VIDC_PRIMARIES_RESERVED;
  440. switch(v4l2_primaries) {
  441. case V4L2_COLORSPACE_DEFAULT:
  442. vidc_color_primaries = MSM_VIDC_PRIMARIES_RESERVED;
  443. break;
  444. case V4L2_COLORSPACE_REC709:
  445. vidc_color_primaries = MSM_VIDC_PRIMARIES_BT709;
  446. break;
  447. case V4L2_COLORSPACE_470_SYSTEM_M:
  448. vidc_color_primaries = MSM_VIDC_PRIMARIES_BT470_SYSTEM_M;
  449. break;
  450. case V4L2_COLORSPACE_470_SYSTEM_BG:
  451. vidc_color_primaries = MSM_VIDC_PRIMARIES_BT470_SYSTEM_BG;
  452. break;
  453. case V4L2_COLORSPACE_SMPTE170M:
  454. vidc_color_primaries = MSM_VIDC_PRIMARIES_BT601_525;
  455. break;
  456. case V4L2_COLORSPACE_SMPTE240M:
  457. vidc_color_primaries = MSM_VIDC_PRIMARIES_SMPTE_ST240M;
  458. break;
  459. case V4L2_COLORSPACE_VIDC_GENERIC_FILM:
  460. vidc_color_primaries = MSM_VIDC_PRIMARIES_GENERIC_FILM;
  461. break;
  462. case V4L2_COLORSPACE_BT2020:
  463. vidc_color_primaries = MSM_VIDC_PRIMARIES_BT2020;
  464. break;
  465. case V4L2_COLORSPACE_DCI_P3:
  466. vidc_color_primaries = MSM_VIDC_PRIMARIES_SMPTE_RP431_2;
  467. break;
  468. case V4L2_COLORSPACE_VIDC_EG431:
  469. vidc_color_primaries = MSM_VIDC_PRIMARIES_SMPTE_EG431_1;
  470. break;
  471. case V4L2_COLORSPACE_VIDC_EBU_TECH:
  472. vidc_color_primaries = MSM_VIDC_PRIMARIES_SMPTE_EBU_TECH;
  473. break;
  474. default:
  475. i_vpr_e(inst, "%s: invalid v4l2 color primaries %d\n",
  476. func, v4l2_primaries);
  477. break;
  478. }
  479. return vidc_color_primaries;
  480. }
  481. u32 v4l2_color_primaries_from_driver(struct msm_vidc_inst *inst,
  482. u32 vidc_color_primaries, const char *func)
  483. {
  484. u32 v4l2_primaries = V4L2_COLORSPACE_DEFAULT;
  485. switch(vidc_color_primaries) {
  486. case MSM_VIDC_PRIMARIES_UNSPECIFIED:
  487. v4l2_primaries = V4L2_COLORSPACE_DEFAULT;
  488. break;
  489. case MSM_VIDC_PRIMARIES_BT709:
  490. v4l2_primaries = V4L2_COLORSPACE_REC709;
  491. break;
  492. case MSM_VIDC_PRIMARIES_BT470_SYSTEM_M:
  493. v4l2_primaries = V4L2_COLORSPACE_470_SYSTEM_M;
  494. break;
  495. case MSM_VIDC_PRIMARIES_BT470_SYSTEM_BG:
  496. v4l2_primaries = V4L2_COLORSPACE_470_SYSTEM_BG;
  497. break;
  498. case MSM_VIDC_PRIMARIES_BT601_525:
  499. v4l2_primaries = V4L2_COLORSPACE_SMPTE170M;
  500. break;
  501. case MSM_VIDC_PRIMARIES_SMPTE_ST240M:
  502. v4l2_primaries = V4L2_COLORSPACE_SMPTE240M;
  503. break;
  504. case MSM_VIDC_PRIMARIES_GENERIC_FILM:
  505. v4l2_primaries = V4L2_COLORSPACE_VIDC_GENERIC_FILM;
  506. break;
  507. case MSM_VIDC_PRIMARIES_BT2020:
  508. v4l2_primaries = V4L2_COLORSPACE_BT2020;
  509. break;
  510. case MSM_VIDC_PRIMARIES_SMPTE_RP431_2:
  511. v4l2_primaries = V4L2_COLORSPACE_DCI_P3;
  512. break;
  513. case MSM_VIDC_PRIMARIES_SMPTE_EG431_1:
  514. v4l2_primaries = V4L2_COLORSPACE_VIDC_EG431;
  515. break;
  516. case MSM_VIDC_PRIMARIES_SMPTE_EBU_TECH:
  517. v4l2_primaries = V4L2_COLORSPACE_VIDC_EBU_TECH;
  518. break;
  519. default:
  520. i_vpr_e(inst, "%s: invalid hfi color primaries %d\n",
  521. func, vidc_color_primaries);
  522. break;
  523. }
  524. return v4l2_primaries;
  525. }
  526. u32 v4l2_transfer_char_to_driver(struct msm_vidc_inst *inst,
  527. u32 v4l2_transfer_char, const char *func)
  528. {
  529. u32 vidc_transfer_char = MSM_VIDC_TRANSFER_RESERVED;
  530. switch(v4l2_transfer_char) {
  531. case V4L2_XFER_FUNC_DEFAULT:
  532. vidc_transfer_char = MSM_VIDC_TRANSFER_RESERVED;
  533. break;
  534. case V4L2_XFER_FUNC_709:
  535. vidc_transfer_char = MSM_VIDC_TRANSFER_BT709;
  536. break;
  537. case V4L2_XFER_FUNC_VIDC_BT470_SYSTEM_M:
  538. vidc_transfer_char = MSM_VIDC_TRANSFER_BT470_SYSTEM_M;
  539. break;
  540. case V4L2_XFER_FUNC_VIDC_BT470_SYSTEM_BG:
  541. vidc_transfer_char = MSM_VIDC_TRANSFER_BT470_SYSTEM_BG;
  542. break;
  543. case V4L2_XFER_FUNC_VIDC_BT601_525_OR_625:
  544. vidc_transfer_char = MSM_VIDC_TRANSFER_BT601_525_OR_625;
  545. break;
  546. case V4L2_XFER_FUNC_SMPTE240M:
  547. vidc_transfer_char = MSM_VIDC_TRANSFER_SMPTE_ST240M;
  548. break;
  549. case V4L2_XFER_FUNC_VIDC_LINEAR:
  550. vidc_transfer_char = MSM_VIDC_TRANSFER_LINEAR;
  551. break;
  552. case V4L2_XFER_FUNC_VIDC_XVYCC:
  553. vidc_transfer_char = MSM_VIDC_TRANSFER_XVYCC;
  554. break;
  555. case V4L2_XFER_FUNC_VIDC_BT1361:
  556. vidc_transfer_char = MSM_VIDC_TRANSFER_BT1361_0;
  557. break;
  558. case V4L2_XFER_FUNC_SRGB:
  559. vidc_transfer_char = MSM_VIDC_TRANSFER_SRGB_SYCC;
  560. break;
  561. case V4L2_XFER_FUNC_VIDC_BT2020:
  562. vidc_transfer_char = MSM_VIDC_TRANSFER_BT2020_14;
  563. break;
  564. case V4L2_XFER_FUNC_SMPTE2084:
  565. vidc_transfer_char = MSM_VIDC_TRANSFER_SMPTE_ST2084_PQ;
  566. break;
  567. case V4L2_XFER_FUNC_VIDC_ST428:
  568. vidc_transfer_char = MSM_VIDC_TRANSFER_SMPTE_ST428_1;
  569. break;
  570. case V4L2_XFER_FUNC_VIDC_HLG:
  571. vidc_transfer_char = MSM_VIDC_TRANSFER_BT2100_2_HLG;
  572. break;
  573. default:
  574. i_vpr_e(inst, "%s: invalid v4l2 transfer char %d\n",
  575. func, v4l2_transfer_char);
  576. break;
  577. }
  578. return vidc_transfer_char;
  579. }
  580. u32 v4l2_transfer_char_from_driver(struct msm_vidc_inst *inst,
  581. u32 vidc_transfer_char, const char *func)
  582. {
  583. u32 v4l2_transfer_char = V4L2_XFER_FUNC_DEFAULT;
  584. switch(vidc_transfer_char) {
  585. case MSM_VIDC_TRANSFER_UNSPECIFIED:
  586. v4l2_transfer_char = V4L2_XFER_FUNC_DEFAULT;
  587. break;
  588. case MSM_VIDC_TRANSFER_BT709:
  589. v4l2_transfer_char = V4L2_XFER_FUNC_709;
  590. break;
  591. case MSM_VIDC_TRANSFER_BT470_SYSTEM_M:
  592. v4l2_transfer_char = V4L2_XFER_FUNC_VIDC_BT470_SYSTEM_M;
  593. break;
  594. case MSM_VIDC_TRANSFER_BT470_SYSTEM_BG:
  595. v4l2_transfer_char = V4L2_XFER_FUNC_VIDC_BT470_SYSTEM_BG;
  596. break;
  597. case MSM_VIDC_TRANSFER_BT601_525_OR_625:
  598. v4l2_transfer_char = V4L2_XFER_FUNC_VIDC_BT601_525_OR_625;
  599. break;
  600. case MSM_VIDC_TRANSFER_SMPTE_ST240M:
  601. v4l2_transfer_char = V4L2_XFER_FUNC_SMPTE240M;
  602. break;
  603. case MSM_VIDC_TRANSFER_LINEAR:
  604. v4l2_transfer_char = V4L2_XFER_FUNC_VIDC_LINEAR;
  605. break;
  606. case MSM_VIDC_TRANSFER_XVYCC:
  607. v4l2_transfer_char = V4L2_XFER_FUNC_VIDC_XVYCC;
  608. break;
  609. case MSM_VIDC_TRANSFER_BT1361_0:
  610. v4l2_transfer_char = V4L2_XFER_FUNC_VIDC_BT1361;
  611. break;
  612. case MSM_VIDC_TRANSFER_SRGB_SYCC:
  613. v4l2_transfer_char = V4L2_XFER_FUNC_SRGB;
  614. break;
  615. case MSM_VIDC_TRANSFER_BT2020_14:
  616. case MSM_VIDC_TRANSFER_BT2020_15:
  617. v4l2_transfer_char = V4L2_XFER_FUNC_VIDC_BT2020;
  618. break;
  619. case MSM_VIDC_TRANSFER_SMPTE_ST2084_PQ:
  620. v4l2_transfer_char = V4L2_XFER_FUNC_SMPTE2084;
  621. break;
  622. case MSM_VIDC_TRANSFER_SMPTE_ST428_1:
  623. v4l2_transfer_char = V4L2_XFER_FUNC_VIDC_ST428;
  624. break;
  625. case MSM_VIDC_TRANSFER_BT2100_2_HLG:
  626. v4l2_transfer_char = V4L2_XFER_FUNC_VIDC_HLG;
  627. break;
  628. default:
  629. i_vpr_e(inst, "%s: invalid hfi transfer char %d\n",
  630. func, vidc_transfer_char);
  631. break;
  632. }
  633. return v4l2_transfer_char;
  634. }
  635. u32 v4l2_matrix_coeff_to_driver(struct msm_vidc_inst *inst,
  636. u32 v4l2_matrix_coeff, const char *func)
  637. {
  638. u32 vidc_matrix_coeff = MSM_VIDC_MATRIX_COEFF_RESERVED;
  639. switch(v4l2_matrix_coeff) {
  640. case V4L2_YCBCR_ENC_DEFAULT:
  641. vidc_matrix_coeff = MSM_VIDC_MATRIX_COEFF_RESERVED;
  642. break;
  643. case V4L2_YCBCR_VIDC_SRGB_OR_SMPTE_ST428:
  644. vidc_matrix_coeff = MSM_VIDC_MATRIX_COEFF_SRGB_SMPTE_ST428_1;
  645. break;
  646. case V4L2_YCBCR_ENC_709:
  647. case V4L2_YCBCR_ENC_XV709:
  648. vidc_matrix_coeff = MSM_VIDC_MATRIX_COEFF_BT709;
  649. break;
  650. case V4L2_YCBCR_VIDC_FCC47_73_682:
  651. vidc_matrix_coeff = MSM_VIDC_MATRIX_COEFF_FCC_TITLE_47;
  652. break;
  653. case V4L2_YCBCR_ENC_XV601:
  654. vidc_matrix_coeff = MSM_VIDC_MATRIX_COEFF_BT470_SYS_BG_OR_BT601_625;
  655. break;
  656. case V4L2_YCBCR_ENC_601:
  657. vidc_matrix_coeff = MSM_VIDC_MATRIX_COEFF_BT601_525_BT1358_525_OR_625;
  658. break;
  659. case V4L2_YCBCR_ENC_SMPTE240M:
  660. vidc_matrix_coeff = MSM_VIDC_MATRIX_COEFF_SMPTE_ST240;
  661. break;
  662. case V4L2_YCBCR_ENC_BT2020:
  663. vidc_matrix_coeff = MSM_VIDC_MATRIX_COEFF_BT2020_NON_CONSTANT;
  664. break;
  665. case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
  666. vidc_matrix_coeff = MSM_VIDC_MATRIX_COEFF_BT2020_CONSTANT;
  667. break;
  668. default:
  669. i_vpr_e(inst, "%s: invalid v4l2 matrix coeff %d\n",
  670. func, v4l2_matrix_coeff);
  671. break;
  672. }
  673. return vidc_matrix_coeff;
  674. }
  675. u32 v4l2_matrix_coeff_from_driver(struct msm_vidc_inst *inst,
  676. u32 vidc_matrix_coeff, const char *func)
  677. {
  678. u32 v4l2_matrix_coeff = V4L2_YCBCR_ENC_DEFAULT;
  679. switch(vidc_matrix_coeff) {
  680. case MSM_VIDC_MATRIX_COEFF_SRGB_SMPTE_ST428_1:
  681. v4l2_matrix_coeff = V4L2_YCBCR_VIDC_SRGB_OR_SMPTE_ST428;
  682. break;
  683. case MSM_VIDC_MATRIX_COEFF_BT709:
  684. v4l2_matrix_coeff = V4L2_YCBCR_ENC_709;
  685. break;
  686. case MSM_VIDC_MATRIX_COEFF_UNSPECIFIED:
  687. v4l2_matrix_coeff = V4L2_YCBCR_ENC_DEFAULT;
  688. break;
  689. case MSM_VIDC_MATRIX_COEFF_FCC_TITLE_47:
  690. v4l2_matrix_coeff = V4L2_YCBCR_VIDC_FCC47_73_682;
  691. break;
  692. case MSM_VIDC_MATRIX_COEFF_BT470_SYS_BG_OR_BT601_625:
  693. v4l2_matrix_coeff = V4L2_YCBCR_ENC_XV601;
  694. break;
  695. case MSM_VIDC_MATRIX_COEFF_BT601_525_BT1358_525_OR_625:
  696. v4l2_matrix_coeff = V4L2_YCBCR_ENC_601;
  697. break;
  698. case MSM_VIDC_MATRIX_COEFF_SMPTE_ST240:
  699. v4l2_matrix_coeff = V4L2_YCBCR_ENC_SMPTE240M;
  700. break;
  701. case MSM_VIDC_MATRIX_COEFF_BT2020_NON_CONSTANT:
  702. v4l2_matrix_coeff = V4L2_YCBCR_ENC_BT2020;
  703. break;
  704. case MSM_VIDC_MATRIX_COEFF_BT2020_CONSTANT:
  705. v4l2_matrix_coeff = V4L2_YCBCR_ENC_BT2020_CONST_LUM;
  706. break;
  707. default:
  708. i_vpr_e(inst, "%s: invalid hfi matrix coeff %d\n",
  709. func, vidc_matrix_coeff);
  710. break;
  711. }
  712. return v4l2_matrix_coeff;
  713. }
  714. int v4l2_type_to_driver_port(struct msm_vidc_inst *inst, u32 type,
  715. const char *func)
  716. {
  717. int port;
  718. if (type == INPUT_MPLANE) {
  719. port = INPUT_PORT;
  720. } else if (type == INPUT_META_PLANE) {
  721. port = INPUT_META_PORT;
  722. } else if (type == OUTPUT_MPLANE) {
  723. port = OUTPUT_PORT;
  724. } else if (type == OUTPUT_META_PLANE) {
  725. port = OUTPUT_META_PORT;
  726. } else {
  727. i_vpr_e(inst, "%s: port not found for v4l2 type %d\n",
  728. func, type);
  729. port = -EINVAL;
  730. }
  731. return port;
  732. }
  733. u32 msm_vidc_get_buffer_region(struct msm_vidc_inst *inst,
  734. enum msm_vidc_buffer_type buffer_type, const char *func)
  735. {
  736. u32 region = MSM_VIDC_NON_SECURE;
  737. if (!is_secure_session(inst)) {
  738. switch (buffer_type) {
  739. case MSM_VIDC_BUF_ARP:
  740. region = MSM_VIDC_SECURE_NONPIXEL;
  741. break;
  742. case MSM_VIDC_BUF_INPUT:
  743. if (is_encode_session(inst))
  744. region = MSM_VIDC_NON_SECURE_PIXEL;
  745. else
  746. region = MSM_VIDC_NON_SECURE;
  747. break;
  748. case MSM_VIDC_BUF_OUTPUT:
  749. if (is_decode_session(inst))
  750. region = MSM_VIDC_NON_SECURE_PIXEL;
  751. else
  752. region = MSM_VIDC_NON_SECURE;
  753. break;
  754. case MSM_VIDC_BUF_DPB:
  755. region = MSM_VIDC_NON_SECURE_PIXEL;
  756. break;
  757. default:
  758. region = MSM_VIDC_NON_SECURE;
  759. break;
  760. }
  761. } else {
  762. switch (buffer_type) {
  763. case MSM_VIDC_BUF_INPUT:
  764. if (is_encode_session(inst))
  765. region = MSM_VIDC_SECURE_PIXEL;
  766. else
  767. region = MSM_VIDC_SECURE_BITSTREAM;
  768. break;
  769. case MSM_VIDC_BUF_OUTPUT:
  770. if (is_encode_session(inst))
  771. region = MSM_VIDC_SECURE_BITSTREAM;
  772. else
  773. region = MSM_VIDC_SECURE_PIXEL;
  774. break;
  775. case MSM_VIDC_BUF_INPUT_META:
  776. case MSM_VIDC_BUF_OUTPUT_META:
  777. region = MSM_VIDC_NON_SECURE;
  778. break;
  779. case MSM_VIDC_BUF_BIN:
  780. region = MSM_VIDC_SECURE_BITSTREAM;
  781. break;
  782. case MSM_VIDC_BUF_COMV:
  783. case MSM_VIDC_BUF_NON_COMV:
  784. case MSM_VIDC_BUF_LINE:
  785. region = MSM_VIDC_SECURE_NONPIXEL;
  786. break;
  787. case MSM_VIDC_BUF_DPB:
  788. region = MSM_VIDC_SECURE_PIXEL;
  789. break;
  790. case MSM_VIDC_BUF_PERSIST:
  791. case MSM_VIDC_BUF_ARP:
  792. region = MSM_VIDC_SECURE_NONPIXEL;
  793. break;
  794. default:
  795. i_vpr_e(inst, "%s: invalid driver buffer type %d\n",
  796. func, buffer_type);
  797. }
  798. }
  799. return region;
  800. }
  801. struct msm_vidc_buffers *msm_vidc_get_buffers(
  802. struct msm_vidc_inst *inst, enum msm_vidc_buffer_type buffer_type,
  803. const char *func)
  804. {
  805. switch (buffer_type) {
  806. case MSM_VIDC_BUF_INPUT:
  807. return &inst->buffers.input;
  808. case MSM_VIDC_BUF_INPUT_META:
  809. return &inst->buffers.input_meta;
  810. case MSM_VIDC_BUF_OUTPUT:
  811. return &inst->buffers.output;
  812. case MSM_VIDC_BUF_OUTPUT_META:
  813. return &inst->buffers.output_meta;
  814. case MSM_VIDC_BUF_BIN:
  815. return &inst->buffers.bin;
  816. case MSM_VIDC_BUF_ARP:
  817. return &inst->buffers.arp;
  818. case MSM_VIDC_BUF_COMV:
  819. return &inst->buffers.comv;
  820. case MSM_VIDC_BUF_NON_COMV:
  821. return &inst->buffers.non_comv;
  822. case MSM_VIDC_BUF_LINE:
  823. return &inst->buffers.line;
  824. case MSM_VIDC_BUF_DPB:
  825. return &inst->buffers.dpb;
  826. case MSM_VIDC_BUF_PERSIST:
  827. return &inst->buffers.persist;
  828. case MSM_VIDC_BUF_VPSS:
  829. return &inst->buffers.vpss;
  830. default:
  831. i_vpr_e(inst, "%s: invalid driver buffer type %d\n",
  832. func, buffer_type);
  833. return NULL;
  834. }
  835. }
  836. struct msm_vidc_mappings *msm_vidc_get_mappings(
  837. struct msm_vidc_inst *inst, enum msm_vidc_buffer_type buffer_type,
  838. const char *func)
  839. {
  840. switch (buffer_type) {
  841. case MSM_VIDC_BUF_INPUT:
  842. return &inst->mappings.input;
  843. case MSM_VIDC_BUF_INPUT_META:
  844. return &inst->mappings.input_meta;
  845. case MSM_VIDC_BUF_OUTPUT:
  846. return &inst->mappings.output;
  847. case MSM_VIDC_BUF_OUTPUT_META:
  848. return &inst->mappings.output_meta;
  849. case MSM_VIDC_BUF_BIN:
  850. return &inst->mappings.bin;
  851. case MSM_VIDC_BUF_ARP:
  852. return &inst->mappings.arp;
  853. case MSM_VIDC_BUF_COMV:
  854. return &inst->mappings.comv;
  855. case MSM_VIDC_BUF_NON_COMV:
  856. return &inst->mappings.non_comv;
  857. case MSM_VIDC_BUF_LINE:
  858. return &inst->mappings.line;
  859. case MSM_VIDC_BUF_DPB:
  860. return &inst->mappings.dpb;
  861. case MSM_VIDC_BUF_PERSIST:
  862. return &inst->mappings.persist;
  863. case MSM_VIDC_BUF_VPSS:
  864. return &inst->mappings.vpss;
  865. default:
  866. i_vpr_e(inst, "%s: invalid driver buffer type %d\n",
  867. func, buffer_type);
  868. return NULL;
  869. }
  870. }
  871. struct msm_vidc_allocations *msm_vidc_get_allocations(
  872. struct msm_vidc_inst *inst, enum msm_vidc_buffer_type buffer_type,
  873. const char *func)
  874. {
  875. switch (buffer_type) {
  876. case MSM_VIDC_BUF_BIN:
  877. return &inst->allocations.bin;
  878. case MSM_VIDC_BUF_ARP:
  879. return &inst->allocations.arp;
  880. case MSM_VIDC_BUF_COMV:
  881. return &inst->allocations.comv;
  882. case MSM_VIDC_BUF_NON_COMV:
  883. return &inst->allocations.non_comv;
  884. case MSM_VIDC_BUF_LINE:
  885. return &inst->allocations.line;
  886. case MSM_VIDC_BUF_DPB:
  887. return &inst->allocations.dpb;
  888. case MSM_VIDC_BUF_PERSIST:
  889. return &inst->allocations.persist;
  890. case MSM_VIDC_BUF_VPSS:
  891. return &inst->allocations.vpss;
  892. default:
  893. i_vpr_e(inst, "%s: invalid driver buffer type %d\n",
  894. func, buffer_type);
  895. return NULL;
  896. }
  897. }
  898. int msm_vidc_change_core_state(struct msm_vidc_core *core,
  899. enum msm_vidc_core_state request_state, const char *func)
  900. {
  901. if (!core) {
  902. d_vpr_e("%s: invalid params\n", __func__);
  903. return -EINVAL;
  904. }
  905. d_vpr_h("%s: core state changed from %s to %s\n",
  906. func, core_state_name(core->state),
  907. core_state_name(request_state));
  908. core->state = request_state;
  909. return 0;
  910. }
  911. int msm_vidc_change_inst_state(struct msm_vidc_inst *inst,
  912. enum msm_vidc_inst_state request_state, const char *func)
  913. {
  914. if (!inst) {
  915. d_vpr_e("%s: invalid params\n", __func__);
  916. return -EINVAL;
  917. }
  918. if (!request_state) {
  919. i_vpr_e(inst, "%s: invalid request state\n", func);
  920. return -EINVAL;
  921. }
  922. if (inst->state == MSM_VIDC_ERROR) {
  923. i_vpr_h(inst,
  924. "%s: inst is in bad state, can not change state to %s\n",
  925. func, state_name(request_state));
  926. return 0;
  927. }
  928. i_vpr_h(inst, "%s: state changed from %s to %s\n",
  929. func, state_name(inst->state), state_name(request_state));
  930. inst->state = request_state;
  931. return 0;
  932. }
  933. bool msm_vidc_allow_s_fmt(struct msm_vidc_inst *inst, u32 type)
  934. {
  935. bool allow = false;
  936. if (!inst) {
  937. d_vpr_e("%s: invalid params\n", __func__);
  938. return false;
  939. }
  940. if (inst->state == MSM_VIDC_OPEN) {
  941. allow = true;
  942. goto exit;
  943. }
  944. if (type == OUTPUT_MPLANE || type == OUTPUT_META_PLANE) {
  945. if (inst->state == MSM_VIDC_START_INPUT ||
  946. inst->state == MSM_VIDC_DRAIN_START_INPUT) {
  947. allow = true;
  948. goto exit;
  949. }
  950. }
  951. if (type == INPUT_MPLANE || type == INPUT_META_PLANE) {
  952. if (inst->state == MSM_VIDC_START_OUTPUT) {
  953. allow = true;
  954. goto exit;
  955. }
  956. }
  957. exit:
  958. if (!allow)
  959. i_vpr_e(inst, "%s: type %d not allowed in state %s\n",
  960. __func__, type, state_name(inst->state));
  961. return allow;
  962. }
  963. bool msm_vidc_allow_s_ctrl(struct msm_vidc_inst *inst, u32 id)
  964. {
  965. bool allow = false;
  966. if (!inst) {
  967. d_vpr_e("%s: invalid params\n", __func__);
  968. return false;
  969. }
  970. if (inst->state == MSM_VIDC_OPEN) {
  971. allow = true;
  972. goto exit;
  973. }
  974. if (is_decode_session(inst)) {
  975. if (!inst->vb2q[INPUT_PORT].streaming) {
  976. allow = true;
  977. goto exit;
  978. }
  979. if (inst->vb2q[INPUT_PORT].streaming) {
  980. switch (id) {
  981. case V4L2_CID_MPEG_VIDC_CODEC_CONFIG:
  982. allow = true;
  983. break;
  984. default:
  985. allow = false;
  986. break;
  987. }
  988. }
  989. } else if (is_encode_session(inst)) {
  990. if (inst->state == MSM_VIDC_START || inst->state == MSM_VIDC_START_OUTPUT) {
  991. switch (id) {
  992. case V4L2_CID_MPEG_VIDEO_BITRATE:
  993. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  994. case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
  995. case V4L2_CID_HFLIP:
  996. case V4L2_CID_VFLIP:
  997. case V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP:
  998. case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_LAYER:
  999. case V4L2_CID_MPEG_VIDEO_H264_HIERARCHICAL_CODING_LAYER:
  1000. case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L0_BR:
  1001. case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L1_BR:
  1002. case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L2_BR:
  1003. case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L3_BR:
  1004. case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L4_BR:
  1005. case V4L2_CID_MPEG_VIDEO_HEVC_HIER_CODING_L5_BR:
  1006. case V4L2_CID_MPEG_VIDEO_H264_HIER_CODING_L0_BR:
  1007. case V4L2_CID_MPEG_VIDEO_H264_HIER_CODING_L1_BR:
  1008. case V4L2_CID_MPEG_VIDEO_H264_HIER_CODING_L2_BR:
  1009. case V4L2_CID_MPEG_VIDEO_H264_HIER_CODING_L3_BR:
  1010. case V4L2_CID_MPEG_VIDEO_H264_HIER_CODING_L4_BR:
  1011. case V4L2_CID_MPEG_VIDEO_H264_HIER_CODING_L5_BR:
  1012. case V4L2_CID_MPEG_VIDC_USELTRFRAME:
  1013. case V4L2_CID_MPEG_VIDC_MARKLTRFRAME:
  1014. case V4L2_CID_MPEG_VIDC_VIDEO_BLUR_TYPES:
  1015. case V4L2_CID_MPEG_VIDC_VIDEO_BLUR_RESOLUTION:
  1016. case V4L2_CID_MPEG_VIDEO_CONSTANT_QUALITY:
  1017. case V4L2_CID_MPEG_VIDC_ENC_INPUT_COMPRESSION_RATIO:
  1018. allow = true;
  1019. break;
  1020. default:
  1021. allow = false;
  1022. break;
  1023. }
  1024. }
  1025. }
  1026. exit:
  1027. if (!allow)
  1028. i_vpr_e(inst, "%s: id %d not allowed in state %s\n",
  1029. __func__, id, state_name(inst->state));
  1030. return allow;
  1031. }
  1032. bool msm_vidc_allow_reqbufs(struct msm_vidc_inst *inst, u32 type)
  1033. {
  1034. bool allow = false;
  1035. if (!inst) {
  1036. d_vpr_e("%s: invalid params\n", __func__);
  1037. return false;
  1038. }
  1039. if (inst->state == MSM_VIDC_OPEN) {
  1040. allow = true;
  1041. goto exit;
  1042. }
  1043. if (type == OUTPUT_MPLANE || type == OUTPUT_META_PLANE) {
  1044. if (inst->state == MSM_VIDC_START_INPUT ||
  1045. inst->state == MSM_VIDC_DRAIN_START_INPUT) {
  1046. allow = true;
  1047. goto exit;
  1048. }
  1049. }
  1050. if (type == INPUT_MPLANE || type == INPUT_META_PLANE) {
  1051. if (inst->state == MSM_VIDC_START_OUTPUT) {
  1052. allow = true;
  1053. goto exit;
  1054. }
  1055. }
  1056. exit:
  1057. if (!allow)
  1058. i_vpr_e(inst, "%s: type %d not allowed in state %s\n",
  1059. __func__, type, state_name(inst->state));
  1060. return allow;
  1061. }
  1062. enum msm_vidc_allow msm_vidc_allow_stop(struct msm_vidc_inst *inst)
  1063. {
  1064. enum msm_vidc_allow allow = MSM_VIDC_DISALLOW;
  1065. if (!inst) {
  1066. d_vpr_e("%s: invalid params\n", __func__);
  1067. return allow;
  1068. }
  1069. if (inst->state == MSM_VIDC_START ||
  1070. inst->state == MSM_VIDC_DRC ||
  1071. inst->state == MSM_VIDC_DRC_LAST_FLAG ||
  1072. inst->state == MSM_VIDC_DRC_DRAIN) {
  1073. allow = MSM_VIDC_ALLOW;
  1074. } else if (inst->state == MSM_VIDC_START_INPUT) {
  1075. allow = MSM_VIDC_IGNORE;
  1076. i_vpr_e(inst, "%s: stop ignored in state %s\n",
  1077. __func__, state_name(inst->state));
  1078. } else {
  1079. i_vpr_e(inst, "%s: stop not allowed in state %s\n",
  1080. __func__, state_name(inst->state));
  1081. }
  1082. return allow;
  1083. }
  1084. bool msm_vidc_allow_start(struct msm_vidc_inst *inst)
  1085. {
  1086. if (!inst) {
  1087. d_vpr_e("%s: invalid params\n", __func__);
  1088. return false;
  1089. }
  1090. if (inst->state == MSM_VIDC_DRAIN_LAST_FLAG ||
  1091. inst->state == MSM_VIDC_DRC_LAST_FLAG ||
  1092. inst->state == MSM_VIDC_DRC_DRAIN_LAST_FLAG)
  1093. return true;
  1094. i_vpr_e(inst, "%s: not allowed in state %s\n",
  1095. __func__, state_name(inst->state));
  1096. return false;
  1097. }
  1098. bool msm_vidc_allow_streamon(struct msm_vidc_inst *inst, u32 type)
  1099. {
  1100. if (!inst) {
  1101. d_vpr_e("%s: invalid params\n", __func__);
  1102. return false;
  1103. }
  1104. if (type == INPUT_MPLANE || type == INPUT_META_PLANE) {
  1105. if (inst->state == MSM_VIDC_OPEN ||
  1106. inst->state == MSM_VIDC_START_OUTPUT)
  1107. return true;
  1108. } else if (type == OUTPUT_MPLANE || type == OUTPUT_META_PLANE) {
  1109. if (inst->state == MSM_VIDC_OPEN ||
  1110. inst->state == MSM_VIDC_START_INPUT ||
  1111. inst->state == MSM_VIDC_DRAIN_START_INPUT)
  1112. return true;
  1113. }
  1114. i_vpr_e(inst, "%s: type %d not allowed in state %s\n",
  1115. __func__, type, state_name(inst->state));
  1116. return false;
  1117. }
  1118. bool msm_vidc_allow_streamoff(struct msm_vidc_inst *inst, u32 type)
  1119. {
  1120. bool allow = true;
  1121. if (!inst) {
  1122. d_vpr_e("%s: invalid params\n", __func__);
  1123. return false;
  1124. }
  1125. if (type == INPUT_MPLANE) {
  1126. if (inst->state == MSM_VIDC_OPEN ||
  1127. inst->state == MSM_VIDC_START_OUTPUT)
  1128. allow = false;
  1129. } else if (type == INPUT_META_PLANE) {
  1130. if (inst->state == MSM_VIDC_START_INPUT)
  1131. allow = false;
  1132. } else if (type == OUTPUT_MPLANE) {
  1133. if (inst->state == MSM_VIDC_OPEN ||
  1134. inst->state == MSM_VIDC_START_INPUT)
  1135. allow = false;
  1136. } else if (type == OUTPUT_META_PLANE) {
  1137. if (inst->state == MSM_VIDC_START_OUTPUT)
  1138. allow = false;
  1139. }
  1140. if (!allow)
  1141. i_vpr_e(inst, "%s: type %d not allowed in state %s\n",
  1142. __func__, type, state_name(inst->state));
  1143. return allow;
  1144. }
  1145. enum msm_vidc_allow msm_vidc_allow_qbuf(struct msm_vidc_inst *inst, u32 type)
  1146. {
  1147. int port = 0;
  1148. if (!inst) {
  1149. d_vpr_e("%s: invalid params\n", __func__);
  1150. return MSM_VIDC_DISALLOW;
  1151. }
  1152. if (inst->state == MSM_VIDC_ERROR) {
  1153. i_vpr_e(inst, "%s: inst in error state\n", __func__);
  1154. return MSM_VIDC_DISALLOW;
  1155. }
  1156. port = v4l2_type_to_driver_port(inst, type, __func__);
  1157. if (port < 0)
  1158. return MSM_VIDC_DISALLOW;
  1159. /* defer queuing if streamon not completed */
  1160. if (!inst->vb2q[port].streaming)
  1161. return MSM_VIDC_DEFER;
  1162. if (type == INPUT_META_PLANE || type == OUTPUT_META_PLANE)
  1163. return MSM_VIDC_DEFER;
  1164. if (type == INPUT_MPLANE) {
  1165. if (inst->state == MSM_VIDC_OPEN ||
  1166. inst->state == MSM_VIDC_START_OUTPUT)
  1167. return MSM_VIDC_DEFER;
  1168. else
  1169. return MSM_VIDC_ALLOW;
  1170. } else if (type == OUTPUT_MPLANE) {
  1171. if (inst->state == MSM_VIDC_OPEN ||
  1172. inst->state == MSM_VIDC_START_INPUT ||
  1173. inst->state == MSM_VIDC_DRAIN_START_INPUT)
  1174. return MSM_VIDC_DEFER;
  1175. else
  1176. return MSM_VIDC_ALLOW;
  1177. } else {
  1178. i_vpr_e(inst, "%s: unknown buffer type %d\n", __func__, type);
  1179. return MSM_VIDC_DISALLOW;
  1180. }
  1181. return MSM_VIDC_DISALLOW;
  1182. }
  1183. enum msm_vidc_allow msm_vidc_allow_input_psc(struct msm_vidc_inst *inst)
  1184. {
  1185. enum msm_vidc_allow allow = MSM_VIDC_DISALLOW;
  1186. if (!inst) {
  1187. d_vpr_e("%s: invalid params\n", __func__);
  1188. return MSM_VIDC_DISALLOW;
  1189. }
  1190. if (inst->state == MSM_VIDC_START ||
  1191. inst->state == MSM_VIDC_START_INPUT ||
  1192. inst->state == MSM_VIDC_DRAIN) {
  1193. allow = MSM_VIDC_ALLOW;
  1194. } else if (inst->state == MSM_VIDC_DRC ||
  1195. inst->state == MSM_VIDC_DRC_LAST_FLAG ||
  1196. inst->state == MSM_VIDC_DRC_DRAIN ||
  1197. inst->state == MSM_VIDC_DRC_DRAIN_LAST_FLAG ||
  1198. inst->state == MSM_VIDC_DRAIN_START_INPUT) {
  1199. i_vpr_h(inst, "%s: defer input psc, inst state %s\n",
  1200. __func__, state_name(inst->state));
  1201. allow = MSM_VIDC_DEFER;
  1202. } else {
  1203. i_vpr_e(inst, "%s: input psc in wrong state %s\n",
  1204. __func__, state_name(inst->state));
  1205. allow = MSM_VIDC_DISALLOW;
  1206. }
  1207. return allow;
  1208. }
  1209. bool msm_vidc_allow_last_flag(struct msm_vidc_inst *inst)
  1210. {
  1211. if (!inst) {
  1212. d_vpr_e("%s: invalid params\n", __func__);
  1213. return false;
  1214. }
  1215. if (inst->state == MSM_VIDC_DRC ||
  1216. inst->state == MSM_VIDC_DRAIN ||
  1217. inst->state == MSM_VIDC_DRC_DRAIN)
  1218. return true;
  1219. i_vpr_e(inst, "%s: not allowed in state %s\n",
  1220. __func__, state_name(inst->state));
  1221. return false;
  1222. }
  1223. static int msm_vidc_process_pending_ipsc(struct msm_vidc_inst *inst,
  1224. enum msm_vidc_inst_state *new_state)
  1225. {
  1226. struct response_work *resp_work, *dummy = NULL;
  1227. int rc = 0;
  1228. if (!inst || !new_state) {
  1229. d_vpr_e("%s: invalid params\n", __func__);
  1230. return -EINVAL;
  1231. }
  1232. if (list_empty(&inst->response_works))
  1233. return 0;
  1234. i_vpr_h(inst, "%s: state %s, ipsc pending\n", __func__, state_name(inst->state));
  1235. list_for_each_entry_safe(resp_work, dummy, &inst->response_works, list) {
  1236. if (resp_work->type == RESP_WORK_INPUT_PSC) {
  1237. rc = handle_session_response_work(inst, resp_work);
  1238. if (rc) {
  1239. i_vpr_e(inst, "%s: handle ipsc failed\n", __func__);
  1240. *new_state = MSM_VIDC_ERROR;
  1241. } else {
  1242. if (inst->state == MSM_VIDC_DRC_DRAIN_LAST_FLAG ||
  1243. inst->state == MSM_VIDC_DRAIN_START_INPUT) {
  1244. *new_state = MSM_VIDC_DRC_DRAIN;
  1245. } else if (inst->state == MSM_VIDC_DRC_LAST_FLAG) {
  1246. *new_state = MSM_VIDC_DRC;
  1247. }
  1248. }
  1249. list_del(&resp_work->list);
  1250. kfree(resp_work->data);
  1251. kfree(resp_work);
  1252. /* list contains max only one ipsc at anytime */
  1253. break;
  1254. }
  1255. }
  1256. return rc;
  1257. }
  1258. int msm_vidc_state_change_streamon(struct msm_vidc_inst *inst, u32 type)
  1259. {
  1260. int rc = 0;
  1261. enum msm_vidc_inst_state new_state = MSM_VIDC_ERROR;
  1262. if (!inst || !inst->core) {
  1263. d_vpr_e("%s: invalid params\n", __func__);
  1264. return -EINVAL;
  1265. }
  1266. if (type == INPUT_META_PLANE || type == OUTPUT_META_PLANE)
  1267. return 0;
  1268. if (type == INPUT_MPLANE) {
  1269. if (inst->state == MSM_VIDC_OPEN)
  1270. new_state = MSM_VIDC_START_INPUT;
  1271. else if (inst->state == MSM_VIDC_START_OUTPUT)
  1272. new_state = MSM_VIDC_START;
  1273. } else if (type == OUTPUT_MPLANE) {
  1274. if (inst->state == MSM_VIDC_OPEN) {
  1275. new_state = MSM_VIDC_START_OUTPUT;
  1276. } else if (inst->state == MSM_VIDC_START_INPUT) {
  1277. new_state = MSM_VIDC_START;
  1278. } else if (inst->state == MSM_VIDC_DRAIN_START_INPUT) {
  1279. i_vpr_h(inst, "%s: streamon(output) in %s state\n",
  1280. __func__, state_name(inst->state));
  1281. new_state = MSM_VIDC_DRAIN;
  1282. rc = msm_vidc_process_pending_ipsc(inst, &new_state);
  1283. if (rc) {
  1284. i_vpr_e(inst, "%s: process pending ipsc failed\n", __func__);
  1285. goto state_change;
  1286. }
  1287. }
  1288. }
  1289. state_change:
  1290. msm_vidc_change_inst_state(inst, new_state, __func__);
  1291. return rc;
  1292. }
  1293. int msm_vidc_state_change_streamoff(struct msm_vidc_inst *inst, u32 type)
  1294. {
  1295. int rc = 0;
  1296. enum msm_vidc_inst_state new_state = MSM_VIDC_ERROR;
  1297. struct response_work *resp_work, *dummy;
  1298. if (!inst || !inst->core) {
  1299. d_vpr_e("%s: invalid params\n", __func__);
  1300. return -EINVAL;
  1301. }
  1302. if (type == INPUT_META_PLANE || type == OUTPUT_META_PLANE)
  1303. return 0;
  1304. if (type == INPUT_MPLANE) {
  1305. if (inst->state == MSM_VIDC_START_INPUT) {
  1306. new_state = MSM_VIDC_OPEN;
  1307. } else if (inst->state == MSM_VIDC_START) {
  1308. new_state = MSM_VIDC_START_OUTPUT;
  1309. } else if (inst->state == MSM_VIDC_DRC ||
  1310. inst->state == MSM_VIDC_DRC_LAST_FLAG ||
  1311. inst->state == MSM_VIDC_DRAIN ||
  1312. inst->state == MSM_VIDC_DRAIN_LAST_FLAG ||
  1313. inst->state == MSM_VIDC_DRC_DRAIN ||
  1314. inst->state == MSM_VIDC_DRC_DRAIN_LAST_FLAG ||
  1315. inst->state == MSM_VIDC_DRAIN_START_INPUT) {
  1316. new_state = MSM_VIDC_START_OUTPUT;
  1317. /* discard pending port settings change if any */
  1318. list_for_each_entry_safe(resp_work, dummy,
  1319. &inst->response_works, list) {
  1320. if (resp_work->type == RESP_WORK_INPUT_PSC) {
  1321. i_vpr_h(inst,
  1322. "%s: discard pending input psc\n", __func__);
  1323. list_del(&resp_work->list);
  1324. kfree(resp_work->data);
  1325. kfree(resp_work);
  1326. }
  1327. }
  1328. }
  1329. } else if (type == OUTPUT_MPLANE) {
  1330. if (inst->state == MSM_VIDC_START_OUTPUT) {
  1331. new_state = MSM_VIDC_OPEN;
  1332. } else if (inst->state == MSM_VIDC_START ||
  1333. inst->state == MSM_VIDC_DRAIN ||
  1334. inst->state == MSM_VIDC_DRAIN_LAST_FLAG ||
  1335. inst->state == MSM_VIDC_DRC ||
  1336. inst->state == MSM_VIDC_DRC_LAST_FLAG ||
  1337. inst->state == MSM_VIDC_DRC_DRAIN) {
  1338. new_state = MSM_VIDC_START_INPUT;
  1339. } else if (inst->state == MSM_VIDC_DRC_DRAIN_LAST_FLAG) {
  1340. new_state = MSM_VIDC_DRAIN_START_INPUT;
  1341. }
  1342. }
  1343. rc = msm_vidc_change_inst_state(inst, new_state, __func__);
  1344. if (rc)
  1345. goto exit;
  1346. exit:
  1347. return rc;
  1348. }
  1349. int msm_vidc_state_change_stop(struct msm_vidc_inst *inst)
  1350. {
  1351. int rc = 0;
  1352. enum msm_vidc_inst_state new_state = MSM_VIDC_ERROR;
  1353. if (!inst || !inst->core) {
  1354. d_vpr_e("%s: invalid params\n", __func__);
  1355. return -EINVAL;
  1356. }
  1357. if (inst->state == MSM_VIDC_START) {
  1358. new_state = MSM_VIDC_DRAIN;
  1359. } else if (inst->state == MSM_VIDC_DRC) {
  1360. new_state = MSM_VIDC_DRC_DRAIN;
  1361. } else if (inst->state == MSM_VIDC_DRC_DRAIN ||
  1362. inst->state == MSM_VIDC_DRC_LAST_FLAG) {
  1363. new_state = MSM_VIDC_DRC_DRAIN_LAST_FLAG;
  1364. } else {
  1365. i_vpr_e(inst, "%s: wrong state %s\n",
  1366. __func__, state_name(inst->state));
  1367. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  1368. return -EINVAL;
  1369. }
  1370. rc = msm_vidc_change_inst_state(inst, new_state, __func__);
  1371. if (rc)
  1372. return rc;
  1373. return rc;
  1374. }
  1375. int msm_vidc_state_change_start(struct msm_vidc_inst *inst)
  1376. {
  1377. int rc = 0;
  1378. enum msm_vidc_inst_state new_state = MSM_VIDC_ERROR;
  1379. if (!inst || !inst->core) {
  1380. d_vpr_e("%s: invalid params\n", __func__);
  1381. return -EINVAL;
  1382. }
  1383. if (inst->state == MSM_VIDC_DRAIN_LAST_FLAG ||
  1384. inst->state == MSM_VIDC_DRC_LAST_FLAG) {
  1385. new_state = MSM_VIDC_START;
  1386. rc = msm_vidc_process_pending_ipsc(inst, &new_state);
  1387. if (rc) {
  1388. i_vpr_e(inst, "%s: process pending ipsc failed\n", __func__);
  1389. goto state_change;
  1390. }
  1391. } else if (inst->state == MSM_VIDC_DRC_DRAIN_LAST_FLAG) {
  1392. new_state = MSM_VIDC_DRAIN;
  1393. rc = msm_vidc_process_pending_ipsc(inst, &new_state);
  1394. if (rc) {
  1395. i_vpr_e(inst, "%s: process pending ipsc failed\n", __func__);
  1396. goto state_change;
  1397. }
  1398. } else {
  1399. i_vpr_e(inst, "%s: wrong state %s\n", __func__, state_name(inst->state));
  1400. new_state = MSM_VIDC_ERROR;
  1401. rc = -EINVAL;
  1402. goto state_change;
  1403. }
  1404. state_change:
  1405. msm_vidc_change_inst_state(inst, new_state, __func__);
  1406. return rc;
  1407. }
  1408. int msm_vidc_state_change_input_psc(struct msm_vidc_inst *inst)
  1409. {
  1410. int rc = 0;
  1411. enum msm_vidc_inst_state new_state = MSM_VIDC_ERROR;
  1412. if (!inst || !inst->core) {
  1413. d_vpr_e("%s: invalid params\n", __func__);
  1414. return -EINVAL;
  1415. }
  1416. /* don't change state as output port is not started yet */
  1417. if (inst->state == MSM_VIDC_START_INPUT)
  1418. return 0;
  1419. if (inst->state == MSM_VIDC_START) {
  1420. new_state = MSM_VIDC_DRC;
  1421. } else if (inst->state == MSM_VIDC_DRAIN) {
  1422. new_state = MSM_VIDC_DRC_DRAIN;
  1423. } else {
  1424. i_vpr_e(inst, "%s: wrong state %s\n",
  1425. __func__, state_name(inst->state));
  1426. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  1427. return -EINVAL;
  1428. }
  1429. rc = msm_vidc_change_inst_state(inst, new_state, __func__);
  1430. if (rc)
  1431. return rc;
  1432. return rc;
  1433. }
  1434. int msm_vidc_state_change_last_flag(struct msm_vidc_inst *inst)
  1435. {
  1436. int rc = 0;
  1437. enum msm_vidc_inst_state new_state = MSM_VIDC_ERROR;
  1438. if (!inst || !inst->core) {
  1439. d_vpr_e("%s: invalid params\n", __func__);
  1440. return -EINVAL;
  1441. }
  1442. if (inst->state == MSM_VIDC_DRC) {
  1443. new_state = MSM_VIDC_DRC_LAST_FLAG;
  1444. } else if (inst->state == MSM_VIDC_DRAIN) {
  1445. new_state = MSM_VIDC_DRAIN_LAST_FLAG;
  1446. } else if (inst->state == MSM_VIDC_DRC_DRAIN) {
  1447. new_state = MSM_VIDC_DRC_DRAIN_LAST_FLAG;
  1448. } else {
  1449. i_vpr_e(inst, "%s: wrong state %s\n",
  1450. __func__, state_name(inst->state));
  1451. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  1452. return -EINVAL;
  1453. }
  1454. rc = msm_vidc_change_inst_state(inst, new_state, __func__);
  1455. if (rc)
  1456. return rc;
  1457. return rc;
  1458. }
  1459. int msm_vidc_get_control(struct msm_vidc_inst *inst, struct v4l2_ctrl *ctrl)
  1460. {
  1461. int rc = 0;
  1462. if (!inst || !ctrl) {
  1463. d_vpr_e("%s: invalid params\n", __func__);
  1464. return -EINVAL;
  1465. }
  1466. switch (ctrl->id) {
  1467. case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
  1468. ctrl->val = inst->buffers.output.min_count +
  1469. inst->buffers.output.extra_count;
  1470. i_vpr_h(inst, "g_min: output buffers %d\n", ctrl->val);
  1471. break;
  1472. case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:
  1473. ctrl->val = inst->buffers.input.min_count +
  1474. inst->buffers.input.extra_count;
  1475. i_vpr_h(inst, "g_min: input buffers %d\n", ctrl->val);
  1476. break;
  1477. default:
  1478. break;
  1479. }
  1480. return rc;
  1481. }
  1482. int msm_vidc_get_mbs_per_frame(struct msm_vidc_inst *inst)
  1483. {
  1484. int height, width;
  1485. struct v4l2_format *out_f;
  1486. struct v4l2_format *inp_f;
  1487. out_f = &inst->fmts[OUTPUT_PORT];
  1488. inp_f = &inst->fmts[INPUT_PORT];
  1489. height = max(out_f->fmt.pix_mp.height,
  1490. inp_f->fmt.pix_mp.height);
  1491. width = max(out_f->fmt.pix_mp.width,
  1492. inp_f->fmt.pix_mp.width);
  1493. return NUM_MBS_PER_FRAME(height, width);
  1494. }
  1495. int msm_vidc_get_fps(struct msm_vidc_inst *inst)
  1496. {
  1497. int fps;
  1498. u32 frame_rate, operating_rate;
  1499. if (!inst || !inst->capabilities) {
  1500. d_vpr_e("%s: invalid params\n", __func__);
  1501. return -EINVAL;
  1502. }
  1503. frame_rate = inst->capabilities->cap[FRAME_RATE].value;
  1504. operating_rate = inst->capabilities->cap[OPERATING_RATE].value;
  1505. if (operating_rate > frame_rate)
  1506. fps = (operating_rate >> 16) ?
  1507. (operating_rate >> 16) : 1;
  1508. else
  1509. fps = frame_rate >> 16;
  1510. return fps;
  1511. }
  1512. int msm_vidc_num_buffers(struct msm_vidc_inst *inst,
  1513. enum msm_vidc_buffer_type type, enum msm_vidc_buffer_attributes attr)
  1514. {
  1515. int count = 0;
  1516. struct msm_vidc_buffer *vbuf;
  1517. struct msm_vidc_buffers *buffers;
  1518. if (!inst) {
  1519. d_vpr_e("%s: invalid params\n", __func__);
  1520. return count;
  1521. }
  1522. if (type == MSM_VIDC_BUF_OUTPUT) {
  1523. buffers = &inst->buffers.output;
  1524. } else if (type == MSM_VIDC_BUF_INPUT) {
  1525. buffers = &inst->buffers.input;
  1526. } else {
  1527. i_vpr_e(inst, "%s: invalid buffer type %#x\n",
  1528. __func__, type);
  1529. return count;
  1530. }
  1531. list_for_each_entry(vbuf, &buffers->list, list) {
  1532. if (vbuf->type != type)
  1533. continue;
  1534. if (!(vbuf->attr & attr))
  1535. continue;
  1536. count++;
  1537. }
  1538. return count;
  1539. }
  1540. static int vb2_buffer_to_driver(struct vb2_buffer *vb2,
  1541. struct msm_vidc_buffer *buf)
  1542. {
  1543. int rc = 0;
  1544. if (!vb2 || !buf) {
  1545. d_vpr_e("%s: invalid params\n", __func__);
  1546. return -EINVAL;
  1547. }
  1548. buf->valid = true;
  1549. buf->type = v4l2_type_to_driver(vb2->type, __func__);
  1550. if (!buf->type)
  1551. return -EINVAL;
  1552. buf->index = vb2->index;
  1553. buf->fd = vb2->planes[0].m.fd;
  1554. buf->data_offset = vb2->planes[0].data_offset;
  1555. buf->data_size = vb2->planes[0].bytesused;
  1556. buf->buffer_size = vb2->planes[0].length;
  1557. buf->timestamp = vb2->timestamp;
  1558. return rc;
  1559. }
  1560. int msm_vidc_unmap_driver_buf(struct msm_vidc_inst *inst,
  1561. struct msm_vidc_buffer *buf)
  1562. {
  1563. int rc = 0;
  1564. struct msm_vidc_mappings *mappings;
  1565. struct msm_vidc_map *map = NULL;
  1566. bool found = false;
  1567. if (!inst || !buf) {
  1568. d_vpr_e("%s: invalid params\n", __func__);
  1569. return -EINVAL;
  1570. }
  1571. mappings = msm_vidc_get_mappings(inst, buf->type, __func__);
  1572. if (!mappings)
  1573. return -EINVAL;
  1574. /* sanity check to see if it was not removed */
  1575. list_for_each_entry(map, &mappings->list, list) {
  1576. if (map->dmabuf == buf->dmabuf) {
  1577. found = true;
  1578. break;
  1579. }
  1580. }
  1581. if (!found) {
  1582. print_vidc_buffer(VIDC_ERR, "err ", "no buf in mappings", inst, buf);
  1583. return -EINVAL;
  1584. }
  1585. rc = msm_vidc_memory_unmap(inst->core, map);
  1586. if (rc) {
  1587. print_vidc_buffer(VIDC_ERR, "err ", "unmap failed", inst, buf);
  1588. return -EINVAL;
  1589. }
  1590. /* finally delete if refcount is zero */
  1591. if (!map->refcount) {
  1592. list_del(&map->list);
  1593. kfree(map);
  1594. }
  1595. return 0;
  1596. }
  1597. int msm_vidc_put_driver_buf(struct msm_vidc_inst *inst,
  1598. struct msm_vidc_buffer *buf)
  1599. {
  1600. int rc = 0;
  1601. if (!inst || !buf) {
  1602. d_vpr_e("%s: invalid params\n", __func__);
  1603. return -EINVAL;
  1604. }
  1605. rc = msm_vidc_unmap_driver_buf(inst, buf);
  1606. if (rc)
  1607. return rc;
  1608. msm_vidc_memory_put_dmabuf(buf->dmabuf);
  1609. /* delete the buffer from buffers->list */
  1610. list_del(&buf->list);
  1611. kfree(buf);
  1612. return 0;
  1613. }
  1614. int msm_vidc_map_driver_buf(struct msm_vidc_inst *inst,
  1615. struct msm_vidc_buffer *buf)
  1616. {
  1617. int rc = 0;
  1618. struct msm_vidc_mappings *mappings;
  1619. struct msm_vidc_map *map = NULL;
  1620. bool found = false;
  1621. if (!inst || !buf) {
  1622. d_vpr_e("%s: invalid params\n", __func__);
  1623. return -EINVAL;
  1624. }
  1625. mappings = msm_vidc_get_mappings(inst, buf->type, __func__);
  1626. if (!mappings)
  1627. return -EINVAL;
  1628. /* check if it is an existing one */
  1629. list_for_each_entry(map, &mappings->list, list) {
  1630. if (map->dmabuf == buf->dmabuf) {
  1631. found = true;
  1632. break;
  1633. }
  1634. }
  1635. if (found) {
  1636. /* skip mapping for RO buffer */
  1637. if (!(buf->attr & MSM_VIDC_ATTR_READ_ONLY)) {
  1638. rc = msm_vidc_memory_map(inst->core, map);
  1639. if (rc)
  1640. return -ENOMEM;
  1641. buf->device_addr = map->device_addr;
  1642. }
  1643. return 0;
  1644. }
  1645. map = kzalloc(sizeof(struct msm_vidc_map), GFP_KERNEL);
  1646. if (!map) {
  1647. i_vpr_e(inst, "%s: alloc failed\n", __func__);
  1648. return -ENOMEM;
  1649. }
  1650. INIT_LIST_HEAD(&map->list);
  1651. map->type = buf->type;
  1652. map->dmabuf = buf->dmabuf;
  1653. map->region = msm_vidc_get_buffer_region(inst, buf->type, __func__);
  1654. rc = msm_vidc_memory_map(inst->core, map);
  1655. if (rc) {
  1656. kfree(map);
  1657. return -ENOMEM;
  1658. }
  1659. buf->device_addr = map->device_addr;
  1660. list_add_tail(&map->list, &mappings->list);
  1661. return 0;
  1662. }
  1663. struct msm_vidc_buffer *msm_vidc_get_driver_buf(struct msm_vidc_inst *inst,
  1664. struct vb2_buffer *vb2)
  1665. {
  1666. int rc = 0;
  1667. struct msm_vidc_buffer *buf = NULL;
  1668. struct msm_vidc_buffers *buffers;
  1669. struct dma_buf *dmabuf;
  1670. enum msm_vidc_buffer_type buf_type;
  1671. bool found = false;
  1672. if (!inst || !vb2) {
  1673. d_vpr_e("%s: invalid params\n", __func__);
  1674. return NULL;
  1675. }
  1676. buf_type = v4l2_type_to_driver(vb2->type, __func__);
  1677. if (!buf_type)
  1678. return NULL;
  1679. buffers = msm_vidc_get_buffers(inst, buf_type, __func__);
  1680. if (!buffers)
  1681. return NULL;
  1682. dmabuf = msm_vidc_memory_get_dmabuf(vb2->planes[0].m.fd);
  1683. if (!dmabuf)
  1684. return NULL;
  1685. /* check if it is an existing buffer */
  1686. list_for_each_entry(buf, &buffers->list, list) {
  1687. if (buf->dmabuf == dmabuf &&
  1688. buf->data_offset == vb2->planes[0].data_offset) {
  1689. found = true;
  1690. break;
  1691. }
  1692. }
  1693. if (found) {
  1694. /* only YUV buffers are allowed to repeat */
  1695. if ((is_decode_session(inst) && vb2->type != OUTPUT_MPLANE) ||
  1696. (is_encode_session(inst) && vb2->type != INPUT_MPLANE)) {
  1697. print_vidc_buffer(VIDC_ERR, "err ",
  1698. "existing buffer", inst, buf);
  1699. goto error;
  1700. }
  1701. /* for decoder, YUV with RO flag are allowed to repeat */
  1702. if (is_decode_session(inst) &&
  1703. !(buf->attr & MSM_VIDC_ATTR_READ_ONLY)) {
  1704. print_vidc_buffer(VIDC_ERR, "err ",
  1705. "existing buffer without RO flag", inst, buf);
  1706. goto error;
  1707. }
  1708. /* for encoder, treat the repeated buffer as new buffer */
  1709. if (is_encode_session(inst) && vb2->type == INPUT_MPLANE)
  1710. found = false;
  1711. }
  1712. if (!found) {
  1713. buf = kzalloc(sizeof(struct msm_vidc_buffer), GFP_KERNEL);
  1714. if (!buf) {
  1715. i_vpr_e(inst, "%s: alloc failed\n", __func__);
  1716. goto error;
  1717. }
  1718. buf->dmabuf = dmabuf;
  1719. INIT_LIST_HEAD(&buf->list);
  1720. list_add_tail(&buf->list, &buffers->list);
  1721. } else {
  1722. /* reset all attributes except read only */
  1723. buf->attr &= MSM_VIDC_ATTR_READ_ONLY;
  1724. }
  1725. /* treat every buffer as deferred buffer initially */
  1726. buf->attr |= MSM_VIDC_ATTR_DEFERRED;
  1727. rc = vb2_buffer_to_driver(vb2, buf);
  1728. if (rc)
  1729. goto error;
  1730. rc = msm_vidc_map_driver_buf(inst, buf);
  1731. if (rc)
  1732. goto error;
  1733. return buf;
  1734. error:
  1735. msm_vidc_memory_put_dmabuf(dmabuf);
  1736. if (!found)
  1737. kfree(buf);
  1738. return NULL;
  1739. }
  1740. struct msm_vidc_buffer *get_meta_buffer(struct msm_vidc_inst *inst,
  1741. struct msm_vidc_buffer *buf)
  1742. {
  1743. struct msm_vidc_buffer *mbuf;
  1744. struct msm_vidc_buffers *buffers;
  1745. bool found = false;
  1746. if (!inst || !buf) {
  1747. d_vpr_e("%s: invalid params\n", __func__);
  1748. return NULL;
  1749. }
  1750. if (buf->type == MSM_VIDC_BUF_INPUT) {
  1751. buffers = &inst->buffers.input_meta;
  1752. } else if (buf->type == MSM_VIDC_BUF_OUTPUT) {
  1753. buffers = &inst->buffers.output_meta;
  1754. } else {
  1755. i_vpr_e(inst, "%s: invalid buffer type %d\n",
  1756. __func__, buf->type);
  1757. return NULL;
  1758. }
  1759. list_for_each_entry(mbuf, &buffers->list, list) {
  1760. if (!mbuf->valid)
  1761. continue;
  1762. if (mbuf->index == buf->index) {
  1763. found = true;
  1764. break;
  1765. }
  1766. }
  1767. if (!found)
  1768. return NULL;
  1769. return mbuf;
  1770. }
  1771. bool msm_vidc_is_super_buffer(struct msm_vidc_inst *inst)
  1772. {
  1773. struct msm_vidc_inst_capability *capability = NULL;
  1774. if (!inst || !inst->capabilities) {
  1775. d_vpr_e("%s: Invalid params\n", __func__);
  1776. return false;
  1777. }
  1778. capability = inst->capabilities;
  1779. return !!capability->cap[SUPER_FRAME].value;
  1780. }
  1781. static bool is_single_session(struct msm_vidc_inst *inst)
  1782. {
  1783. struct msm_vidc_core *core;
  1784. u32 count = 0;
  1785. if (!inst) {
  1786. d_vpr_e("%s: Invalid params\n", __func__);
  1787. return false;
  1788. }
  1789. core = inst->core;
  1790. core_lock(core, __func__);
  1791. list_for_each_entry(inst, &core->instances, list)
  1792. count++;
  1793. core_unlock(core, __func__);
  1794. return count == 1;
  1795. }
  1796. bool msm_vidc_allow_decode_batch(struct msm_vidc_inst *inst)
  1797. {
  1798. struct msm_vidc_core *core;
  1799. bool allow = false;
  1800. if (!inst || !inst->core) {
  1801. d_vpr_e("%s: invalid params\n", __func__);
  1802. return -EINVAL;
  1803. }
  1804. core = inst->core;
  1805. allow = inst->decode_batch.enable;
  1806. if (!allow) {
  1807. i_vpr_h(inst, "%s: batching already disabled\n", __func__);
  1808. goto exit;
  1809. }
  1810. allow = core->capabilities[DECODE_BATCH].value;
  1811. if (!allow) {
  1812. i_vpr_h(inst, "%s: core doesn't support batching\n", __func__);
  1813. goto exit;
  1814. }
  1815. allow = is_single_session(inst);
  1816. if (!allow) {
  1817. i_vpr_h(inst, "%s: multiple sessions running\n", __func__);
  1818. goto exit;
  1819. }
  1820. allow = is_decode_session(inst);
  1821. if (!allow) {
  1822. i_vpr_h(inst, "%s: not a decoder session\n", __func__);
  1823. goto exit;
  1824. }
  1825. allow = !is_thumbnail_session(inst);
  1826. if (!allow) {
  1827. i_vpr_h(inst, "%s: thumbnail session\n", __func__);
  1828. goto exit;
  1829. }
  1830. allow = !is_image_session(inst);
  1831. if (!allow) {
  1832. i_vpr_h(inst, "%s: image session\n", __func__);
  1833. goto exit;
  1834. }
  1835. allow = is_realtime_session(inst);
  1836. if (!allow) {
  1837. i_vpr_h(inst, "%s: non-realtime session\n", __func__);
  1838. goto exit;
  1839. }
  1840. allow = !is_lowlatency_session(inst);
  1841. if (!allow) {
  1842. i_vpr_h(inst, "%s: lowlatency session\n", __func__);
  1843. goto exit;
  1844. }
  1845. exit:
  1846. i_vpr_h(inst, "%s: batching %s\n", __func__, allow ? "enabled" : "disabled");
  1847. return allow;
  1848. }
  1849. static int msm_vidc_queue_buffer(struct msm_vidc_inst *inst, struct msm_vidc_buffer *buf)
  1850. {
  1851. struct msm_vidc_buffer *meta;
  1852. int rc = 0;
  1853. if (!inst || !buf || !inst->capabilities) {
  1854. d_vpr_e("%s: invalid params\n", __func__);
  1855. return -EINVAL;
  1856. }
  1857. if (is_decode_session(inst) && is_input_buffer(buf->type) &&
  1858. inst->capabilities->cap[CODEC_CONFIG].value) {
  1859. buf->flags |= MSM_VIDC_BUF_FLAG_CODECCONFIG;
  1860. msm_vidc_update_cap_value(inst, CODEC_CONFIG, 0, __func__);
  1861. }
  1862. print_vidc_buffer(VIDC_HIGH, "high", "qbuf", inst, buf);
  1863. meta = get_meta_buffer(inst, buf);
  1864. if (meta)
  1865. print_vidc_buffer(VIDC_HIGH, "high", "qbuf", inst, meta);
  1866. if (!meta && is_meta_enabled(inst, buf->type)) {
  1867. print_vidc_buffer(VIDC_ERR, "err ", "missing meta for", inst, buf);
  1868. return -EINVAL;
  1869. }
  1870. if (msm_vidc_is_super_buffer(inst) && is_input_buffer(buf->type))
  1871. rc = venus_hfi_queue_super_buffer(inst, buf, meta);
  1872. else
  1873. rc = venus_hfi_queue_buffer(inst, buf, meta);
  1874. if (rc)
  1875. return rc;
  1876. buf->attr &= ~MSM_VIDC_ATTR_DEFERRED;
  1877. buf->attr |= MSM_VIDC_ATTR_QUEUED;
  1878. if (meta) {
  1879. meta->attr &= ~MSM_VIDC_ATTR_DEFERRED;
  1880. meta->attr |= MSM_VIDC_ATTR_QUEUED;
  1881. }
  1882. if (buf->type == MSM_VIDC_BUF_INPUT)
  1883. msm_vidc_debugfs_update(inst, MSM_VIDC_DEBUGFS_EVENT_ETB);
  1884. else if (buf->type == MSM_VIDC_BUF_OUTPUT)
  1885. msm_vidc_debugfs_update(inst, MSM_VIDC_DEBUGFS_EVENT_FTB);
  1886. return 0;
  1887. }
  1888. int msm_vidc_queue_buffer_batch(struct msm_vidc_inst *inst)
  1889. {
  1890. struct msm_vidc_buffers *buffers;
  1891. struct msm_vidc_buffer *buf;
  1892. int rc = 0;
  1893. if (!inst) {
  1894. d_vpr_e("%s: invalid params\n", __func__);
  1895. return -EINVAL;
  1896. }
  1897. buffers = msm_vidc_get_buffers(inst, MSM_VIDC_BUF_OUTPUT, __func__);
  1898. if (!buffers)
  1899. return -EINVAL;
  1900. msm_vidc_scale_power(inst, true);
  1901. list_for_each_entry(buf, &buffers->list, list) {
  1902. if (!(buf->attr & MSM_VIDC_ATTR_DEFERRED))
  1903. continue;
  1904. rc = msm_vidc_queue_buffer(inst, buf);
  1905. if (rc)
  1906. return rc;
  1907. }
  1908. return 0;
  1909. }
  1910. void msm_vidc_update_input_cr(struct msm_vidc_inst *inst, u32 idx, u32 cr)
  1911. {
  1912. struct msm_vidc_input_cr_data *temp, *next;
  1913. bool found = false;
  1914. list_for_each_entry_safe(temp, next, &inst->enc_input_crs, list) {
  1915. if (temp->index == idx) {
  1916. temp->input_cr = cr;
  1917. found = true;
  1918. break;
  1919. }
  1920. }
  1921. if (!found) {
  1922. temp = kzalloc(sizeof(*temp), GFP_KERNEL);
  1923. if (!temp) {
  1924. i_vpr_e(inst, "%s: malloc failure.\n", __func__);
  1925. return;
  1926. }
  1927. temp->index = idx;
  1928. temp->input_cr = cr;
  1929. list_add_tail(&temp->list, &inst->enc_input_crs);
  1930. }
  1931. }
  1932. void msm_vidc_free_input_cr_list(struct msm_vidc_inst *inst)
  1933. {
  1934. struct msm_vidc_input_cr_data *temp, *next;
  1935. list_for_each_entry_safe(temp, next, &inst->enc_input_crs, list) {
  1936. list_del(&temp->list);
  1937. kfree(temp);
  1938. }
  1939. INIT_LIST_HEAD(&inst->enc_input_crs);
  1940. }
  1941. int msm_vidc_queue_buffer_single(struct msm_vidc_inst *inst, struct vb2_buffer *vb2)
  1942. {
  1943. int rc = 0;
  1944. struct msm_vidc_buffer *buf;
  1945. enum msm_vidc_allow allow;
  1946. u32 cr = 0;
  1947. if (!inst || !vb2) {
  1948. d_vpr_e("%s: invalid params\n", __func__);
  1949. return -EINVAL;
  1950. }
  1951. buf = msm_vidc_get_driver_buf(inst, vb2);
  1952. if (!buf)
  1953. return -EINVAL;
  1954. allow = msm_vidc_allow_qbuf(inst, vb2->type);
  1955. if (allow == MSM_VIDC_DISALLOW) {
  1956. i_vpr_e(inst, "%s: qbuf not allowed\n", __func__);
  1957. return -EINVAL;
  1958. } else if (allow == MSM_VIDC_DEFER) {
  1959. print_vidc_buffer(VIDC_LOW, "high", "qbuf deferred", inst, buf);
  1960. return 0;
  1961. }
  1962. if (buf->type == MSM_VIDC_BUF_INPUT) {
  1963. if (is_encode_session(inst)) {
  1964. cr = inst->capabilities->cap[ENC_IP_CR].value;
  1965. msm_vidc_update_input_cr(inst, vb2->index, cr);
  1966. inst->capabilities->cap[ENC_IP_CR].value = 0;
  1967. }
  1968. inst->power.buffer_counter++;
  1969. msm_vidc_scale_power(inst, true);
  1970. }
  1971. rc = msm_vidc_queue_buffer(inst, buf);
  1972. if (rc)
  1973. return rc;
  1974. return rc;
  1975. }
  1976. int msm_vidc_destroy_internal_buffer(struct msm_vidc_inst *inst,
  1977. struct msm_vidc_buffer *buffer)
  1978. {
  1979. struct msm_vidc_buffers *buffers;
  1980. struct msm_vidc_allocations *allocations;
  1981. struct msm_vidc_mappings *mappings;
  1982. struct msm_vidc_alloc *alloc, *alloc_dummy;
  1983. struct msm_vidc_map *map, *map_dummy;
  1984. struct msm_vidc_buffer *buf, *dummy;
  1985. if (!inst || !inst->core) {
  1986. d_vpr_e("%s: invalid params\n", __func__);
  1987. return -EINVAL;
  1988. }
  1989. if (!is_internal_buffer(buffer->type)) {
  1990. i_vpr_e(inst, "%s: buffer type %#x is not internal\n",
  1991. __func__, buffer->type);
  1992. return 0;
  1993. }
  1994. i_vpr_h(inst,
  1995. "%s: destroy buffer_type %#x, size %d device_addr %#x\n",
  1996. __func__, buffer->type, buffer->buffer_size,
  1997. buffer->device_addr);
  1998. buffers = msm_vidc_get_buffers(inst, buffer->type, __func__);
  1999. if (!buffers)
  2000. return -EINVAL;
  2001. allocations = msm_vidc_get_allocations(inst, buffer->type, __func__);
  2002. if (!allocations)
  2003. return -EINVAL;
  2004. mappings = msm_vidc_get_mappings(inst, buffer->type, __func__);
  2005. if (!mappings)
  2006. return -EINVAL;
  2007. list_for_each_entry_safe(map, map_dummy, &mappings->list, list) {
  2008. if (map->dmabuf == buffer->dmabuf) {
  2009. msm_vidc_memory_unmap(inst->core, map);
  2010. list_del(&map->list);
  2011. kfree(map);
  2012. break;
  2013. }
  2014. }
  2015. list_for_each_entry_safe(alloc, alloc_dummy, &allocations->list, list) {
  2016. if (alloc->dmabuf == buffer->dmabuf) {
  2017. msm_vidc_memory_free(inst->core, alloc);
  2018. list_del(&alloc->list);
  2019. kfree(alloc);
  2020. break;
  2021. }
  2022. }
  2023. list_for_each_entry_safe(buf, dummy, &buffers->list, list) {
  2024. if (buf->dmabuf == buffer->dmabuf) {
  2025. list_del(&buf->list);
  2026. kfree(buf);
  2027. break;
  2028. }
  2029. }
  2030. buffers->size = 0;
  2031. buffers->min_count = buffers->extra_count = buffers->actual_count = 0;
  2032. return 0;
  2033. }
  2034. int msm_vidc_get_internal_buffers(struct msm_vidc_inst *inst,
  2035. enum msm_vidc_buffer_type buffer_type)
  2036. {
  2037. u32 buf_size;
  2038. u32 buf_count;
  2039. struct msm_vidc_core *core;
  2040. struct msm_vidc_buffers *buffers;
  2041. if (!inst || !inst->core) {
  2042. d_vpr_e("%s: invalid params\n", __func__);
  2043. return -EINVAL;
  2044. }
  2045. core = inst->core;
  2046. buf_size = call_session_op(core, buffer_size,
  2047. inst, buffer_type);
  2048. buf_count = call_session_op(core, min_count,
  2049. inst, buffer_type);
  2050. buffers = msm_vidc_get_buffers(inst, buffer_type, __func__);
  2051. if (!buffers)
  2052. return -EINVAL;
  2053. if (buf_size <= buffers->size &&
  2054. buf_count <= buffers->min_count) {
  2055. buffers->reuse = true;
  2056. } else {
  2057. buffers->reuse = false;
  2058. buffers->size = buf_size;
  2059. buffers->min_count = buf_count;
  2060. }
  2061. return 0;
  2062. }
  2063. int msm_vidc_create_internal_buffer(struct msm_vidc_inst *inst,
  2064. enum msm_vidc_buffer_type buffer_type, u32 index)
  2065. {
  2066. int rc = 0;
  2067. struct msm_vidc_buffers *buffers;
  2068. struct msm_vidc_allocations *allocations;
  2069. struct msm_vidc_mappings *mappings;
  2070. struct msm_vidc_buffer *buffer;
  2071. struct msm_vidc_alloc *alloc;
  2072. struct msm_vidc_map *map;
  2073. if (!inst || !inst->core) {
  2074. d_vpr_e("%s: invalid params\n", __func__);
  2075. return -EINVAL;
  2076. }
  2077. if (!is_internal_buffer(buffer_type)) {
  2078. i_vpr_e(inst, "%s: buffer type %#x is not internal\n",
  2079. __func__, buffer_type);
  2080. return 0;
  2081. }
  2082. buffers = msm_vidc_get_buffers(inst, buffer_type, __func__);
  2083. if (!buffers)
  2084. return -EINVAL;
  2085. allocations = msm_vidc_get_allocations(inst, buffer_type, __func__);
  2086. if (!allocations)
  2087. return -EINVAL;
  2088. mappings = msm_vidc_get_mappings(inst, buffer_type, __func__);
  2089. if (!mappings)
  2090. return -EINVAL;
  2091. if (!buffers->size) {
  2092. i_vpr_e(inst, "%s: invalid buffer %#x\n",
  2093. __func__, buffer_type);
  2094. return -EINVAL;
  2095. }
  2096. buffer = kzalloc(sizeof(struct msm_vidc_buffer), GFP_KERNEL);
  2097. if (!buffer) {
  2098. i_vpr_e(inst, "%s: buf alloc failed\n", __func__);
  2099. return -ENOMEM;
  2100. }
  2101. INIT_LIST_HEAD(&buffer->list);
  2102. buffer->valid = true;
  2103. buffer->type = buffer_type;
  2104. buffer->index = index;
  2105. buffer->buffer_size = buffers->size;
  2106. list_add_tail(&buffer->list, &buffers->list);
  2107. alloc = kzalloc(sizeof(struct msm_vidc_alloc), GFP_KERNEL);
  2108. if (!alloc) {
  2109. i_vpr_e(inst, "%s: alloc failed\n", __func__);
  2110. return -ENOMEM;
  2111. }
  2112. INIT_LIST_HEAD(&alloc->list);
  2113. alloc->type = buffer_type;
  2114. alloc->region = msm_vidc_get_buffer_region(inst,
  2115. buffer_type, __func__);
  2116. alloc->size = buffer->buffer_size;
  2117. alloc->secure = is_secure_region(alloc->region);
  2118. rc = msm_vidc_memory_alloc(inst->core, alloc);
  2119. if (rc)
  2120. return -ENOMEM;
  2121. list_add_tail(&alloc->list, &allocations->list);
  2122. map = kzalloc(sizeof(struct msm_vidc_map), GFP_KERNEL);
  2123. if (!map) {
  2124. i_vpr_e(inst, "%s: map alloc failed\n", __func__);
  2125. return -ENOMEM;
  2126. }
  2127. INIT_LIST_HEAD(&map->list);
  2128. map->type = alloc->type;
  2129. map->region = alloc->region;
  2130. map->dmabuf = alloc->dmabuf;
  2131. rc = msm_vidc_memory_map(inst->core, map);
  2132. if (rc)
  2133. return -ENOMEM;
  2134. list_add_tail(&map->list, &mappings->list);
  2135. buffer->dmabuf = alloc->dmabuf;
  2136. buffer->device_addr = map->device_addr;
  2137. i_vpr_h(inst,
  2138. "%s: created buffer_type %#x, size %d device_addr %#x\n",
  2139. __func__, buffer_type, buffers->size,
  2140. buffer->device_addr);
  2141. return 0;
  2142. }
  2143. int msm_vidc_create_internal_buffers(struct msm_vidc_inst *inst,
  2144. enum msm_vidc_buffer_type buffer_type)
  2145. {
  2146. int rc = 0;
  2147. struct msm_vidc_buffers *buffers;
  2148. int i;
  2149. if (!inst || !inst->core) {
  2150. d_vpr_e("%s: invalid params\n", __func__);
  2151. return -EINVAL;
  2152. }
  2153. buffers = msm_vidc_get_buffers(inst, buffer_type, __func__);
  2154. if (!buffers)
  2155. return -EINVAL;
  2156. if (buffers->reuse) {
  2157. i_vpr_l(inst, "%s: reuse enabled for buffer type %#x\n",
  2158. __func__, buffer_type);
  2159. return 0;
  2160. }
  2161. for (i = 0; i < buffers->min_count; i++) {
  2162. rc = msm_vidc_create_internal_buffer(inst, buffer_type, i);
  2163. if (rc)
  2164. return rc;
  2165. }
  2166. return rc;
  2167. }
  2168. int msm_vidc_queue_internal_buffers(struct msm_vidc_inst *inst,
  2169. enum msm_vidc_buffer_type buffer_type)
  2170. {
  2171. int rc = 0;
  2172. struct msm_vidc_buffers *buffers;
  2173. struct msm_vidc_buffer *buffer, *dummy;
  2174. if (!inst || !inst->core) {
  2175. d_vpr_e("%s: invalid params\n", __func__);
  2176. return -EINVAL;
  2177. }
  2178. if (!is_internal_buffer(buffer_type)) {
  2179. i_vpr_e(inst, "%s: buffer type %#x is not internal\n",
  2180. __func__, buffer_type);
  2181. return 0;
  2182. }
  2183. buffers = msm_vidc_get_buffers(inst, buffer_type, __func__);
  2184. if (!buffers)
  2185. return -EINVAL;
  2186. if (buffers->reuse) {
  2187. i_vpr_l(inst, "%s: reuse enabled for buffer type %#x\n",
  2188. __func__, buffer_type);
  2189. return 0;
  2190. }
  2191. list_for_each_entry_safe(buffer, dummy, &buffers->list, list) {
  2192. /* do not queue pending release buffers */
  2193. if (buffer->flags & MSM_VIDC_ATTR_PENDING_RELEASE)
  2194. continue;
  2195. /* do not queue already queued buffers */
  2196. if (buffer->attr & MSM_VIDC_ATTR_QUEUED)
  2197. continue;
  2198. rc = venus_hfi_queue_buffer(inst, buffer, NULL);
  2199. if (rc)
  2200. return rc;
  2201. /* mark queued */
  2202. buffer->attr |= MSM_VIDC_ATTR_QUEUED;
  2203. i_vpr_h(inst, "%s: queued buffer_type %#x, size %d\n",
  2204. __func__, buffer_type, buffers->size);
  2205. }
  2206. return 0;
  2207. }
  2208. int msm_vidc_alloc_and_queue_session_internal_buffers(struct msm_vidc_inst *inst,
  2209. enum msm_vidc_buffer_type buffer_type)
  2210. {
  2211. int rc = 0;
  2212. if (!inst || !inst->core) {
  2213. d_vpr_e("%s: invalid params\n", __func__);
  2214. return -EINVAL;
  2215. }
  2216. if (buffer_type != MSM_VIDC_BUF_ARP &&
  2217. buffer_type != MSM_VIDC_BUF_PERSIST) {
  2218. i_vpr_e(inst, "%s: invalid buffer type: %d\n",
  2219. __func__, buffer_type);
  2220. rc = -EINVAL;
  2221. goto exit;
  2222. }
  2223. rc = msm_vidc_get_internal_buffers(inst, buffer_type);
  2224. if (rc)
  2225. goto exit;
  2226. rc = msm_vidc_create_internal_buffers(inst, buffer_type);
  2227. if (rc)
  2228. goto exit;
  2229. rc = msm_vidc_queue_internal_buffers(inst, buffer_type);
  2230. if (rc)
  2231. goto exit;
  2232. exit:
  2233. return rc;
  2234. }
  2235. int msm_vidc_release_internal_buffers(struct msm_vidc_inst *inst,
  2236. enum msm_vidc_buffer_type buffer_type)
  2237. {
  2238. int rc = 0;
  2239. struct msm_vidc_buffers *buffers;
  2240. struct msm_vidc_buffer *buffer, *dummy;
  2241. if (!inst || !inst->core) {
  2242. d_vpr_e("%s: invalid params\n", __func__);
  2243. return -EINVAL;
  2244. }
  2245. if (!is_internal_buffer(buffer_type)) {
  2246. i_vpr_e(inst, "%s: buffer type %#x is not internal\n",
  2247. __func__, buffer_type);
  2248. return 0;
  2249. }
  2250. buffers = msm_vidc_get_buffers(inst, buffer_type, __func__);
  2251. if (!buffers)
  2252. return -EINVAL;
  2253. if (buffers->reuse) {
  2254. i_vpr_l(inst, "%s: reuse enabled for buffer type %#x\n",
  2255. __func__, buffer_type);
  2256. return 0;
  2257. }
  2258. list_for_each_entry_safe(buffer, dummy, &buffers->list, list) {
  2259. /* do not release already pending release buffers */
  2260. if (buffer->attr & MSM_VIDC_ATTR_PENDING_RELEASE)
  2261. continue;
  2262. /* release only queued buffers */
  2263. if (!(buffer->attr & MSM_VIDC_ATTR_QUEUED))
  2264. continue;
  2265. rc = venus_hfi_release_buffer(inst, buffer);
  2266. if (rc)
  2267. return rc;
  2268. /* mark pending release */
  2269. buffer->attr |= MSM_VIDC_ATTR_PENDING_RELEASE;
  2270. i_vpr_h(inst, "%s: released buffer_type %#x, size %d\n",
  2271. __func__, buffer_type, buffers->size);
  2272. }
  2273. return 0;
  2274. }
  2275. int msm_vidc_vb2_buffer_done(struct msm_vidc_inst *inst,
  2276. struct msm_vidc_buffer *buf)
  2277. {
  2278. int type, port;
  2279. struct vb2_queue *q;
  2280. struct vb2_buffer *vb2;
  2281. struct vb2_v4l2_buffer *vbuf;
  2282. bool found;
  2283. if (!inst || !buf) {
  2284. d_vpr_e("%s: invalid params\n", __func__);
  2285. return -EINVAL;
  2286. }
  2287. type = v4l2_type_from_driver(buf->type, __func__);
  2288. if (!type)
  2289. return -EINVAL;
  2290. port = v4l2_type_to_driver_port(inst, type, __func__);
  2291. if (port < 0)
  2292. return -EINVAL;
  2293. q = &inst->vb2q[port];
  2294. if (!q->streaming) {
  2295. i_vpr_e(inst, "%s: port %d is not streaming\n",
  2296. __func__, port);
  2297. return -EINVAL;
  2298. }
  2299. found = false;
  2300. list_for_each_entry(vb2, &q->queued_list, queued_entry) {
  2301. if (vb2->state != VB2_BUF_STATE_ACTIVE)
  2302. continue;
  2303. if (vb2->index == buf->index) {
  2304. found = true;
  2305. break;
  2306. }
  2307. }
  2308. if (!found) {
  2309. print_vidc_buffer(VIDC_ERR, "err ", "vb2 not found for", inst, buf);
  2310. return -EINVAL;
  2311. }
  2312. vbuf = to_vb2_v4l2_buffer(vb2);
  2313. vbuf->flags = buf->flags;
  2314. vb2->timestamp = buf->timestamp;
  2315. vb2->planes[0].bytesused = buf->data_size;
  2316. vb2_buffer_done(vb2, VB2_BUF_STATE_DONE);
  2317. return 0;
  2318. }
  2319. int msm_vidc_event_queue_init(struct msm_vidc_inst *inst)
  2320. {
  2321. int rc = 0;
  2322. int index;
  2323. struct msm_vidc_core *core;
  2324. if (!inst || !inst->core) {
  2325. d_vpr_e("%s: invalid params\n", __func__);
  2326. return -EINVAL;
  2327. }
  2328. core = inst->core;
  2329. if (is_decode_session(inst))
  2330. index = 0;
  2331. else if (is_encode_session(inst))
  2332. index = 1;
  2333. else
  2334. return -EINVAL;
  2335. v4l2_fh_init(&inst->event_handler, &core->vdev[index].vdev);
  2336. v4l2_fh_add(&inst->event_handler);
  2337. return rc;
  2338. }
  2339. int msm_vidc_event_queue_deinit(struct msm_vidc_inst *inst)
  2340. {
  2341. int rc = 0;
  2342. if (!inst) {
  2343. d_vpr_e("%s: invalid params\n", __func__);
  2344. return -EINVAL;
  2345. }
  2346. v4l2_fh_del(&inst->event_handler);
  2347. v4l2_fh_exit(&inst->event_handler);
  2348. return rc;
  2349. }
  2350. static int vb2q_init(struct msm_vidc_inst *inst,
  2351. struct vb2_queue *q, enum v4l2_buf_type type)
  2352. {
  2353. int rc = 0;
  2354. struct msm_vidc_core *core;
  2355. if (!inst || !q || !inst->core) {
  2356. d_vpr_e("%s: invalid params\n", __func__);
  2357. return -EINVAL;
  2358. }
  2359. core = inst->core;
  2360. q->type = type;
  2361. q->io_modes = VB2_DMABUF;
  2362. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  2363. q->ops = core->vb2_ops;
  2364. q->mem_ops = core->vb2_mem_ops;
  2365. q->drv_priv = inst;
  2366. q->allow_zero_bytesused = 1;
  2367. q->copy_timestamp = 1;
  2368. rc = vb2_queue_init(q);
  2369. if (rc)
  2370. i_vpr_e(inst, "%s: vb2_queue_init failed for type %d\n",
  2371. __func__, type);
  2372. return rc;
  2373. }
  2374. int msm_vidc_vb2_queue_init(struct msm_vidc_inst *inst)
  2375. {
  2376. int rc = 0;
  2377. if (!inst) {
  2378. i_vpr_e(inst, "%s: invalid params\n", __func__);
  2379. return -EINVAL;
  2380. }
  2381. if (inst->vb2q_init) {
  2382. i_vpr_h(inst, "%s: vb2q already inited\n", __func__);
  2383. return 0;
  2384. }
  2385. rc = vb2q_init(inst, &inst->vb2q[INPUT_PORT], INPUT_MPLANE);
  2386. if (rc)
  2387. goto exit;
  2388. rc = vb2q_init(inst, &inst->vb2q[OUTPUT_PORT], OUTPUT_MPLANE);
  2389. if (rc)
  2390. goto fail_out_vb2q_init;
  2391. rc = vb2q_init(inst, &inst->vb2q[INPUT_META_PORT], INPUT_META_PLANE);
  2392. if (rc)
  2393. goto fail_in_meta_vb2q_init;
  2394. rc = vb2q_init(inst, &inst->vb2q[OUTPUT_META_PORT], OUTPUT_META_PLANE);
  2395. if (rc)
  2396. goto fail_out_meta_vb2q_init;
  2397. inst->vb2q_init = true;
  2398. return 0;
  2399. fail_out_meta_vb2q_init:
  2400. vb2_queue_release(&inst->vb2q[INPUT_META_PORT]);
  2401. fail_in_meta_vb2q_init:
  2402. vb2_queue_release(&inst->vb2q[OUTPUT_PORT]);
  2403. fail_out_vb2q_init:
  2404. vb2_queue_release(&inst->vb2q[INPUT_PORT]);
  2405. exit:
  2406. return rc;
  2407. }
  2408. int msm_vidc_vb2_queue_deinit(struct msm_vidc_inst *inst)
  2409. {
  2410. int rc = 0;
  2411. if (!inst) {
  2412. d_vpr_e("%s: invalid params\n", __func__);
  2413. return -EINVAL;
  2414. }
  2415. if (!inst->vb2q_init) {
  2416. i_vpr_h(inst, "%s: vb2q already deinited\n", __func__);
  2417. return 0;
  2418. }
  2419. vb2_queue_release(&inst->vb2q[OUTPUT_META_PORT]);
  2420. vb2_queue_release(&inst->vb2q[INPUT_META_PORT]);
  2421. vb2_queue_release(&inst->vb2q[OUTPUT_PORT]);
  2422. vb2_queue_release(&inst->vb2q[INPUT_PORT]);
  2423. inst->vb2q_init = false;
  2424. return rc;
  2425. }
  2426. int msm_vidc_add_session(struct msm_vidc_inst *inst)
  2427. {
  2428. int rc = 0;
  2429. struct msm_vidc_inst *i;
  2430. struct msm_vidc_core *core;
  2431. u32 count = 0;
  2432. if (!inst || !inst->core) {
  2433. d_vpr_e("%s: invalid params\n", __func__);
  2434. return -EINVAL;
  2435. }
  2436. core = inst->core;
  2437. if (!core->capabilities) {
  2438. i_vpr_e(inst, "%s: invalid params\n", __func__);
  2439. return -EINVAL;
  2440. }
  2441. core_lock(core, __func__);
  2442. list_for_each_entry(i, &core->instances, list)
  2443. count++;
  2444. if (count < core->capabilities[MAX_SESSION_COUNT].value) {
  2445. list_add_tail(&inst->list, &core->instances);
  2446. } else {
  2447. i_vpr_e(inst, "%s: total sessions %d exceeded max limit %d\n",
  2448. __func__, count, core->capabilities[MAX_SESSION_COUNT].value);
  2449. rc = -EINVAL;
  2450. }
  2451. core_unlock(core, __func__);
  2452. return rc;
  2453. }
  2454. int msm_vidc_remove_session(struct msm_vidc_inst *inst)
  2455. {
  2456. struct msm_vidc_inst *i, *temp;
  2457. struct msm_vidc_core *core;
  2458. u32 count = 0;
  2459. if (!inst || !inst->core) {
  2460. d_vpr_e("%s: invalid params\n", __func__);
  2461. return -EINVAL;
  2462. }
  2463. core = inst->core;
  2464. core_lock(core, __func__);
  2465. list_for_each_entry_safe(i, temp, &core->instances, list) {
  2466. if (i->session_id == inst->session_id) {
  2467. list_del_init(&i->list);
  2468. i_vpr_h(inst, "%s: removed session %#x\n",
  2469. __func__, i->session_id);
  2470. }
  2471. }
  2472. list_for_each_entry(i, &core->instances, list)
  2473. count++;
  2474. i_vpr_h(inst, "%s: remaining sessions %d\n", __func__, count);
  2475. core_unlock(core, __func__);
  2476. return 0;
  2477. }
  2478. int msm_vidc_session_open(struct msm_vidc_inst *inst)
  2479. {
  2480. int rc = 0;
  2481. if (!inst) {
  2482. d_vpr_e("%s: invalid params\n", __func__);
  2483. return -EINVAL;
  2484. }
  2485. inst->packet_size = 4096;
  2486. inst->packet = kzalloc(inst->packet_size, GFP_KERNEL);
  2487. if (!inst->packet) {
  2488. i_vpr_e(inst, "%s(): inst packet allocation failed\n", __func__);
  2489. return -ENOMEM;
  2490. }
  2491. rc = venus_hfi_session_open(inst);
  2492. if (rc)
  2493. goto error;
  2494. return 0;
  2495. error:
  2496. i_vpr_e(inst, "%s(): session open failed\n", __func__);
  2497. kfree(inst->packet);
  2498. inst->packet = NULL;
  2499. return rc;
  2500. }
  2501. int msm_vidc_session_set_codec(struct msm_vidc_inst *inst)
  2502. {
  2503. int rc = 0;
  2504. if (!inst) {
  2505. d_vpr_e("%s: invalid params\n", __func__);
  2506. return -EINVAL;
  2507. }
  2508. rc = venus_hfi_session_set_codec(inst);
  2509. if (rc)
  2510. return rc;
  2511. return 0;
  2512. }
  2513. int msm_vidc_session_set_default_header(struct msm_vidc_inst *inst)
  2514. {
  2515. int rc = 0;
  2516. u32 default_header = false;
  2517. if (!inst) {
  2518. d_vpr_e("%s: invalid params\n", __func__);
  2519. return -EINVAL;
  2520. }
  2521. default_header = inst->capabilities->cap[DEFAULT_HEADER].value;
  2522. i_vpr_h(inst, "%s: default header: %d", __func__, default_header);
  2523. rc = venus_hfi_session_property(inst,
  2524. HFI_PROP_DEC_DEFAULT_HEADER,
  2525. HFI_HOST_FLAGS_NONE,
  2526. get_hfi_port(inst, INPUT_PORT),
  2527. HFI_PAYLOAD_U32,
  2528. &default_header,
  2529. sizeof(u32));
  2530. if (rc)
  2531. i_vpr_e(inst, "%s: set property failed\n", __func__);
  2532. return rc;
  2533. }
  2534. int msm_vidc_session_streamon(struct msm_vidc_inst *inst,
  2535. enum msm_vidc_port_type port)
  2536. {
  2537. int rc = 0;
  2538. if (!inst || !inst->core) {
  2539. d_vpr_e("%s: invalid params\n", __func__);
  2540. return -EINVAL;
  2541. }
  2542. msm_vidc_scale_power(inst, true);
  2543. rc = venus_hfi_start(inst, port);
  2544. if (rc)
  2545. return rc;
  2546. return rc;
  2547. }
  2548. int msm_vidc_session_streamoff(struct msm_vidc_inst *inst,
  2549. enum msm_vidc_port_type port)
  2550. {
  2551. int rc = 0;
  2552. int count = 0;
  2553. struct msm_vidc_core *core;
  2554. enum signal_session_response signal_type;
  2555. enum msm_vidc_buffer_type buffer_type;
  2556. if (!inst || !inst->core) {
  2557. d_vpr_e("%s: invalid params\n", __func__);
  2558. return -EINVAL;
  2559. }
  2560. if (port == INPUT_PORT) {
  2561. signal_type = SIGNAL_CMD_STOP_INPUT;
  2562. buffer_type = MSM_VIDC_BUF_INPUT;
  2563. } else if (port == OUTPUT_PORT) {
  2564. signal_type = SIGNAL_CMD_STOP_OUTPUT;
  2565. buffer_type = MSM_VIDC_BUF_OUTPUT;
  2566. } else {
  2567. i_vpr_e(inst, "%s: invalid port: %d\n", __func__, port);
  2568. return -EINVAL;
  2569. }
  2570. rc = venus_hfi_stop(inst, port);
  2571. if (rc)
  2572. goto error;
  2573. core = inst->core;
  2574. i_vpr_h(inst, "%s: wait on port: %d for time: %d ms\n",
  2575. __func__, port, core->capabilities[HW_RESPONSE_TIMEOUT].value);
  2576. mutex_unlock(&inst->lock);
  2577. rc = wait_for_completion_timeout(
  2578. &inst->completions[signal_type],
  2579. msecs_to_jiffies(
  2580. core->capabilities[HW_RESPONSE_TIMEOUT].value));
  2581. if (!rc) {
  2582. i_vpr_e(inst, "%s: session stop timed out for port: %d\n",
  2583. __func__, port);
  2584. rc = -ETIMEDOUT;
  2585. msm_vidc_core_timeout(inst->core);
  2586. } else {
  2587. rc = 0;
  2588. }
  2589. mutex_lock(&inst->lock);
  2590. if(rc)
  2591. goto error;
  2592. /* no more queued buffers after streamoff */
  2593. count = msm_vidc_num_buffers(inst, buffer_type, MSM_VIDC_ATTR_QUEUED);
  2594. if (!count) {
  2595. i_vpr_h(inst, "%s: stop successful on port: %d\n",
  2596. __func__, port);
  2597. } else {
  2598. i_vpr_e(inst,
  2599. "%s: %d buffers pending with firmware on port: %d\n",
  2600. __func__, count, port);
  2601. rc = -EINVAL;
  2602. goto error;
  2603. }
  2604. /* flush deferred buffers */
  2605. msm_vidc_flush_buffers(inst, buffer_type);
  2606. return 0;
  2607. error:
  2608. msm_vidc_kill_session(inst);
  2609. msm_vidc_flush_buffers(inst, buffer_type);
  2610. return rc;
  2611. }
  2612. int msm_vidc_session_close(struct msm_vidc_inst *inst)
  2613. {
  2614. int rc = 0;
  2615. struct msm_vidc_core *core;
  2616. if (!inst || !inst->core) {
  2617. d_vpr_e("%s: invalid params\n", __func__);
  2618. return -EINVAL;
  2619. }
  2620. rc = venus_hfi_session_close(inst);
  2621. if (rc)
  2622. return rc;
  2623. core = inst->core;
  2624. i_vpr_h(inst, "%s: wait on close for time: %d ms\n",
  2625. __func__, core->capabilities[HW_RESPONSE_TIMEOUT].value);
  2626. mutex_unlock(&inst->lock);
  2627. rc = wait_for_completion_timeout(
  2628. &inst->completions[SIGNAL_CMD_CLOSE],
  2629. msecs_to_jiffies(
  2630. core->capabilities[HW_RESPONSE_TIMEOUT].value));
  2631. if (!rc) {
  2632. i_vpr_e(inst, "%s: session close timed out\n", __func__);
  2633. rc = -ETIMEDOUT;
  2634. msm_vidc_core_timeout(inst->core);
  2635. } else {
  2636. rc = 0;
  2637. i_vpr_h(inst, "%s: close successful\n", __func__);
  2638. }
  2639. mutex_lock(&inst->lock);
  2640. msm_vidc_remove_session(inst);
  2641. i_vpr_h(inst, "%s: free session packet data\n", __func__);
  2642. kfree(inst->packet);
  2643. inst->packet = NULL;
  2644. return rc;
  2645. }
  2646. int msm_vidc_kill_session(struct msm_vidc_inst *inst)
  2647. {
  2648. if (!inst) {
  2649. d_vpr_e("%s: invalid params\n", __func__);
  2650. return -EINVAL;
  2651. }
  2652. if (!inst->session_id) {
  2653. i_vpr_e(inst, "%s: already killed\n", __func__);
  2654. return 0;
  2655. }
  2656. i_vpr_e(inst, "%s: killing session\n", __func__);
  2657. msm_vidc_session_close(inst);
  2658. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  2659. return 0;
  2660. }
  2661. int msm_vidc_get_inst_capability(struct msm_vidc_inst *inst)
  2662. {
  2663. int rc = 0;
  2664. int i;
  2665. struct msm_vidc_core *core;
  2666. if (!inst || !inst->core || !inst->capabilities) {
  2667. d_vpr_e("%s: invalid params\n", __func__);
  2668. return -EINVAL;
  2669. }
  2670. core = inst->core;
  2671. for (i = 0; i < core->codecs_count; i++) {
  2672. if (core->inst_caps[i].domain == inst->domain &&
  2673. core->inst_caps[i].codec == inst->codec) {
  2674. i_vpr_h(inst,
  2675. "%s: copied capabilities with %#x codec, %#x domain\n",
  2676. __func__, inst->codec, inst->domain);
  2677. memcpy(inst->capabilities, &core->inst_caps[i],
  2678. sizeof(struct msm_vidc_inst_capability));
  2679. }
  2680. }
  2681. return rc;
  2682. }
  2683. int msm_vidc_deinit_core_caps(struct msm_vidc_core *core)
  2684. {
  2685. int rc = 0;
  2686. if (!core) {
  2687. d_vpr_e("%s: invalid params\n", __func__);
  2688. return -EINVAL;
  2689. }
  2690. kfree(core->capabilities);
  2691. core->capabilities = NULL;
  2692. d_vpr_h("%s: Core capabilities freed\n", __func__);
  2693. return rc;
  2694. }
  2695. int msm_vidc_init_core_caps(struct msm_vidc_core *core)
  2696. {
  2697. int rc = 0;
  2698. int i, num_platform_caps;
  2699. struct msm_platform_core_capability *platform_data;
  2700. if (!core || !core->platform) {
  2701. d_vpr_e("%s: invalid params\n", __func__);
  2702. rc = -EINVAL;
  2703. goto exit;
  2704. }
  2705. platform_data = core->platform->data.core_data;
  2706. if (!platform_data) {
  2707. d_vpr_e("%s: platform core data is NULL\n",
  2708. __func__);
  2709. rc = -EINVAL;
  2710. goto exit;
  2711. }
  2712. core->capabilities = kcalloc(1,
  2713. (sizeof(struct msm_vidc_core_capability) *
  2714. (CORE_CAP_MAX + 1)), GFP_KERNEL);
  2715. if (!core->capabilities) {
  2716. d_vpr_e("%s: failed to allocate core capabilities\n",
  2717. __func__);
  2718. rc = -ENOMEM;
  2719. goto exit;
  2720. }
  2721. num_platform_caps = core->platform->data.core_data_size;
  2722. /* loop over platform caps */
  2723. for (i = 0; i < num_platform_caps && i < CORE_CAP_MAX; i++) {
  2724. core->capabilities[platform_data[i].type].type = platform_data[i].type;
  2725. core->capabilities[platform_data[i].type].value = platform_data[i].value;
  2726. }
  2727. exit:
  2728. return rc;
  2729. }
  2730. static void update_inst_capability(struct msm_platform_inst_capability *in,
  2731. struct msm_vidc_inst_capability *capability)
  2732. {
  2733. if (!in || !capability) {
  2734. d_vpr_e("%s: invalid params %pK %pK\n",
  2735. __func__, in, capability);
  2736. return;
  2737. }
  2738. if (in->cap < INST_CAP_MAX) {
  2739. capability->cap[in->cap].cap = in->cap;
  2740. capability->cap[in->cap].min = in->min;
  2741. capability->cap[in->cap].max = in->max;
  2742. capability->cap[in->cap].step_or_mask = in->step_or_mask;
  2743. capability->cap[in->cap].value = in->value;
  2744. capability->cap[in->cap].flags = in->flags;
  2745. capability->cap[in->cap].v4l2_id = in->v4l2_id;
  2746. capability->cap[in->cap].hfi_id = in->hfi_id;
  2747. memcpy(capability->cap[in->cap].parents, in->parents,
  2748. sizeof(capability->cap[in->cap].parents));
  2749. memcpy(capability->cap[in->cap].children, in->children,
  2750. sizeof(capability->cap[in->cap].children));
  2751. capability->cap[in->cap].adjust = in->adjust;
  2752. capability->cap[in->cap].set = in->set;
  2753. } else {
  2754. d_vpr_e("%s: invalid cap %d\n",
  2755. __func__, in->cap);
  2756. }
  2757. }
  2758. int msm_vidc_deinit_instance_caps(struct msm_vidc_core *core)
  2759. {
  2760. int rc = 0;
  2761. if (!core) {
  2762. d_vpr_e("%s: invalid params\n", __func__);
  2763. return -EINVAL;
  2764. }
  2765. kfree(core->inst_caps);
  2766. core->inst_caps = NULL;
  2767. d_vpr_h("%s: core->inst_caps freed\n", __func__);
  2768. return rc;
  2769. }
  2770. int msm_vidc_init_instance_caps(struct msm_vidc_core *core)
  2771. {
  2772. int rc = 0;
  2773. u8 enc_valid_codecs, dec_valid_codecs;
  2774. u8 count_bits, enc_codec_count;
  2775. u8 codecs_count = 0;
  2776. int i, j, check_bit, num_platform_caps;
  2777. struct msm_platform_inst_capability *platform_data = NULL;
  2778. if (!core || !core->platform || !core->capabilities) {
  2779. d_vpr_e("%s: invalid params\n", __func__);
  2780. rc = -EINVAL;
  2781. goto error;
  2782. }
  2783. platform_data = core->platform->data.instance_data;
  2784. if (!platform_data) {
  2785. d_vpr_e("%s: platform instance data is NULL\n",
  2786. __func__);
  2787. rc = -EINVAL;
  2788. goto error;
  2789. }
  2790. enc_valid_codecs = core->capabilities[ENC_CODECS].value;
  2791. count_bits = enc_valid_codecs;
  2792. COUNT_BITS(count_bits, codecs_count);
  2793. enc_codec_count = codecs_count;
  2794. dec_valid_codecs = core->capabilities[DEC_CODECS].value;
  2795. count_bits = dec_valid_codecs;
  2796. COUNT_BITS(count_bits, codecs_count);
  2797. core->codecs_count = codecs_count;
  2798. core->inst_caps = kcalloc(codecs_count,
  2799. sizeof(struct msm_vidc_inst_capability),
  2800. GFP_KERNEL);
  2801. if (!core->inst_caps) {
  2802. d_vpr_e("%s: failed to allocate core capabilities\n",
  2803. __func__);
  2804. rc = -ENOMEM;
  2805. goto error;
  2806. }
  2807. check_bit = 0;
  2808. /* determine codecs for enc domain */
  2809. for (i = 0; i < enc_codec_count; i++) {
  2810. while (check_bit < (sizeof(enc_valid_codecs) * 8)) {
  2811. if (enc_valid_codecs & BIT(check_bit)) {
  2812. core->inst_caps[i].domain = MSM_VIDC_ENCODER;
  2813. core->inst_caps[i].codec = enc_valid_codecs &
  2814. BIT(check_bit);
  2815. check_bit++;
  2816. break;
  2817. }
  2818. check_bit++;
  2819. }
  2820. }
  2821. /* reset checkbit to check from 0th bit of decoder codecs set bits*/
  2822. check_bit = 0;
  2823. /* determine codecs for dec domain */
  2824. for (; i < codecs_count; i++) {
  2825. while (check_bit < (sizeof(dec_valid_codecs) * 8)) {
  2826. if (dec_valid_codecs & BIT(check_bit)) {
  2827. core->inst_caps[i].domain = MSM_VIDC_DECODER;
  2828. core->inst_caps[i].codec = dec_valid_codecs &
  2829. BIT(check_bit);
  2830. check_bit++;
  2831. break;
  2832. }
  2833. check_bit++;
  2834. }
  2835. }
  2836. num_platform_caps = core->platform->data.instance_data_size;
  2837. d_vpr_h("%s: num caps %d\n", __func__, num_platform_caps);
  2838. /* loop over each platform capability */
  2839. for (i = 0; i < num_platform_caps; i++) {
  2840. /* select matching core codec and update it */
  2841. for (j = 0; j < codecs_count; j++) {
  2842. if ((platform_data[i].domain &
  2843. core->inst_caps[j].domain) &&
  2844. (platform_data[i].codec &
  2845. core->inst_caps[j].codec)) {
  2846. /* update core capability */
  2847. update_inst_capability(&platform_data[i],
  2848. &core->inst_caps[j]);
  2849. }
  2850. }
  2851. }
  2852. error:
  2853. return rc;
  2854. }
  2855. int msm_vidc_core_deinit(struct msm_vidc_core *core, bool force)
  2856. {
  2857. int rc = 0;
  2858. struct msm_vidc_inst *inst, *dummy;
  2859. if (!core) {
  2860. d_vpr_e("%s: invalid params\n", __func__);
  2861. return -EINVAL;
  2862. }
  2863. core_lock(core, __func__);
  2864. d_vpr_h("%s(): force %u\n", __func__, force);
  2865. if (core->state == MSM_VIDC_CORE_DEINIT)
  2866. goto unlock;
  2867. if (!force)
  2868. if (!list_empty(&core->instances))
  2869. goto unlock;
  2870. venus_hfi_core_deinit(core);
  2871. /* unlink all sessions from core, if any */
  2872. list_for_each_entry_safe(inst, dummy, &core->instances, list) {
  2873. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  2874. list_del(&inst->list);
  2875. }
  2876. msm_vidc_change_core_state(core, MSM_VIDC_CORE_DEINIT, __func__);
  2877. unlock:
  2878. core_unlock(core, __func__);
  2879. return rc;
  2880. }
  2881. int msm_vidc_core_init(struct msm_vidc_core *core)
  2882. {
  2883. int rc = 0;
  2884. if (!core || !core->platform) {
  2885. d_vpr_e("%s: invalid params\n", __func__);
  2886. return -EINVAL;
  2887. }
  2888. core_lock(core, __func__);
  2889. if (core->state == MSM_VIDC_CORE_INIT) {
  2890. rc = 0;
  2891. goto unlock;
  2892. }
  2893. msm_vidc_change_core_state(core, MSM_VIDC_CORE_INIT, __func__);
  2894. init_completion(&core->init_done);
  2895. core->smmu_fault_handled = false;
  2896. core->ssr.trigger = false;
  2897. rc = venus_hfi_core_init(core);
  2898. if (rc) {
  2899. d_vpr_e("%s: core init failed\n", __func__);
  2900. goto unlock;
  2901. }
  2902. d_vpr_h("%s(): waiting for sys_init_done, %d ms\n", __func__,
  2903. core->capabilities[HW_RESPONSE_TIMEOUT].value);
  2904. core_unlock(core, __func__);
  2905. rc = wait_for_completion_timeout(&core->init_done, msecs_to_jiffies(
  2906. core->capabilities[HW_RESPONSE_TIMEOUT].value));
  2907. core_lock(core, __func__);
  2908. if (!rc) {
  2909. d_vpr_e("%s: core init timed out\n", __func__);
  2910. rc = -ETIMEDOUT;
  2911. } else {
  2912. d_vpr_h("%s: system init wait completed\n", __func__);
  2913. rc = 0;
  2914. }
  2915. unlock:
  2916. core_unlock(core, __func__);
  2917. if (rc)
  2918. msm_vidc_core_deinit(core, true);
  2919. return rc;
  2920. }
  2921. int msm_vidc_core_timeout(struct msm_vidc_core *core)
  2922. {
  2923. return msm_vidc_core_deinit(core, true);
  2924. }
  2925. int msm_vidc_print_inst_info(struct msm_vidc_inst *inst)
  2926. {
  2927. struct msm_vidc_buffers *buffers;
  2928. struct msm_vidc_buffer *buf;
  2929. enum msm_vidc_port_type port;
  2930. bool is_secure, is_decode;
  2931. u32 bit_depth, bit_rate, frame_rate, width, height;
  2932. struct dma_buf *dbuf;
  2933. int i = 0;
  2934. static const struct msm_vidc_buf_type_name buf_type_name[] = {
  2935. {MSM_VIDC_BUF_INPUT, "INPUT" },
  2936. {MSM_VIDC_BUF_OUTPUT, "OUTPUT" },
  2937. {MSM_VIDC_BUF_INPUT_META, "IN_META" },
  2938. {MSM_VIDC_BUF_OUTPUT_META, "OUT_META" },
  2939. {MSM_VIDC_BUF_BIN, "BIN" },
  2940. {MSM_VIDC_BUF_ARP, "ARP" },
  2941. {MSM_VIDC_BUF_COMV, "COMV" },
  2942. {MSM_VIDC_BUF_NON_COMV, "NON_COMV" },
  2943. {MSM_VIDC_BUF_LINE, "LINE" },
  2944. {MSM_VIDC_BUF_PERSIST, "PERSIST" },
  2945. {MSM_VIDC_BUF_VPSS, "VPSS" },
  2946. };
  2947. if (!inst || !inst->capabilities) {
  2948. i_vpr_e(inst, "%s: invalid params\n", __func__);
  2949. return -EINVAL;
  2950. }
  2951. is_secure = is_secure_session(inst);
  2952. is_decode = inst->domain == MSM_VIDC_DECODER;
  2953. port = is_decode ? INPUT_PORT : OUTPUT_PORT;
  2954. width = inst->fmts[port].fmt.pix_mp.width;
  2955. height = inst->fmts[port].fmt.pix_mp.height;
  2956. bit_depth = inst->capabilities->cap[BIT_DEPTH].value & 0xFFFF;
  2957. bit_rate = inst->capabilities->cap[BIT_RATE].value;
  2958. frame_rate = inst->capabilities->cap[FRAME_RATE].value >> 16;
  2959. i_vpr_e(inst, "%s %s session, HxW: %d x %d, fps: %d, bitrate: %d, bit-depth: %d\n",
  2960. is_secure ? "Secure" : "Non-Secure",
  2961. is_decode ? "Decode" : "Encode",
  2962. height, width,
  2963. frame_rate, bit_rate, bit_depth);
  2964. /* Print buffer details */
  2965. for (i = 0; i < ARRAY_SIZE(buf_type_name); i++) {
  2966. buffers = msm_vidc_get_buffers(inst, buf_type_name[i].type, __func__);
  2967. if (!buffers)
  2968. continue;
  2969. i_vpr_e(inst, "count: type: %8s, min: %2d, extra: %2d, actual: %2d\n",
  2970. buf_type_name[i].name, buffers->min_count,
  2971. buffers->extra_count, buffers->actual_count);
  2972. list_for_each_entry(buf, &buffers->list, list) {
  2973. if (!buf->valid || !buf->dmabuf)
  2974. continue;
  2975. dbuf = (struct dma_buf *)buf->dmabuf;
  2976. i_vpr_e(inst,
  2977. "buf: type: %8s, index: %2d, fd: %4d, size: %9u, off: %8u, filled: %9u, iova: %8x, inode: %9ld, flags: %8x, ts: %16lld, attr: %8x\n",
  2978. buf_type_name[i].name, buf->index, buf->fd, buf->buffer_size,
  2979. buf->data_offset, buf->data_size, buf->device_addr,
  2980. file_inode(dbuf->file)->i_ino,
  2981. buf->flags, buf->timestamp, buf->attr);
  2982. }
  2983. }
  2984. return 0;
  2985. }
  2986. void msm_vidc_smmu_fault_work_handler(struct work_struct *work)
  2987. {
  2988. struct msm_vidc_core *core;
  2989. struct msm_vidc_inst *inst = NULL;
  2990. struct msm_vidc_inst *instances[MAX_SUPPORTED_INSTANCES];
  2991. s32 num_instances = 0;
  2992. core = container_of(work, struct msm_vidc_core, smmu_fault_work);
  2993. if (!core) {
  2994. d_vpr_e("%s: invalid params\n", __func__);
  2995. return;
  2996. }
  2997. /* print noc error log registers */
  2998. venus_print_noc_error_info(core);
  2999. core_lock(core, __func__);
  3000. list_for_each_entry(inst, &core->instances, list)
  3001. instances[num_instances++] = inst;
  3002. core_unlock(core, __func__);
  3003. while (num_instances--) {
  3004. inst = instances[num_instances];
  3005. inst = get_inst_ref(core, inst);
  3006. if (!inst)
  3007. continue;
  3008. inst_lock(inst, __func__);
  3009. msm_vidc_print_inst_info(inst);
  3010. inst_unlock(inst, __func__);
  3011. put_inst(inst);
  3012. }
  3013. }
  3014. int msm_vidc_smmu_fault_handler(struct iommu_domain *domain,
  3015. struct device *dev, unsigned long iova, int flags, void *data)
  3016. {
  3017. struct msm_vidc_core *core = data;
  3018. if (!domain || !core || !core->capabilities) {
  3019. d_vpr_e("%s: invalid params %pK %pK\n",
  3020. __func__, domain, core);
  3021. return -EINVAL;
  3022. }
  3023. if (core->smmu_fault_handled) {
  3024. if (core->capabilities[NON_FATAL_FAULTS].value) {
  3025. dprintk_ratelimit(VIDC_ERR, "err ",
  3026. "%s: non-fatal pagefault address: %lx\n",
  3027. __func__, iova);
  3028. return 0;
  3029. }
  3030. }
  3031. d_vpr_e("%s: faulting address: %lx\n", __func__, iova);
  3032. core->smmu_fault_handled = true;
  3033. /**
  3034. * Fault handler shouldn't be blocked for longtime. So offload work
  3035. * to device_workq to print buffer and memory consumption details.
  3036. */
  3037. queue_work(core->device_workq, &core->smmu_fault_work);
  3038. /*
  3039. * Return -ENOSYS to elicit the default behaviour of smmu driver.
  3040. * If we return -ENOSYS, then smmu driver assumes page fault handler
  3041. * is not installed and prints a list of useful debug information like
  3042. * FAR, SID etc. This information is not printed if we return 0.
  3043. */
  3044. return -ENOSYS;
  3045. }
  3046. int msm_vidc_trigger_ssr(struct msm_vidc_core *core,
  3047. enum msm_vidc_ssr_trigger_type type)
  3048. {
  3049. return 0;
  3050. }
  3051. void msm_vidc_ssr_handler(struct work_struct *work)
  3052. {
  3053. }
  3054. void msm_vidc_pm_work_handler(struct work_struct *work)
  3055. {
  3056. }
  3057. void msm_vidc_fw_unload_handler(struct work_struct *work)
  3058. {
  3059. struct msm_vidc_core *core = NULL;
  3060. int rc = 0;
  3061. core = container_of(work, struct msm_vidc_core, fw_unload_work.work);
  3062. if (!core) {
  3063. d_vpr_e("%s: invalid work or core handle\n", __func__);
  3064. return;
  3065. }
  3066. d_vpr_h("%s: deinitializing video core\n",__func__);
  3067. rc = msm_vidc_core_deinit(core, false);
  3068. if (rc)
  3069. d_vpr_e("%s: Failed to deinit core\n", __func__);
  3070. }
  3071. void msm_vidc_batch_handler(struct work_struct *work)
  3072. {
  3073. struct msm_vidc_inst *inst;
  3074. enum msm_vidc_allow allow;
  3075. int rc = 0;
  3076. inst = container_of(work, struct msm_vidc_inst, decode_batch.work.work);
  3077. inst = get_inst_ref(g_core, inst);
  3078. if (!inst) {
  3079. d_vpr_e("%s: invalid params\n", __func__);
  3080. return;
  3081. }
  3082. inst_lock(inst, __func__);
  3083. if (is_session_error(inst)) {
  3084. i_vpr_e(inst, "%s: failled. Session error\n", __func__);
  3085. goto exit;
  3086. }
  3087. allow = msm_vidc_allow_qbuf(inst, OUTPUT_MPLANE);
  3088. if (allow != MSM_VIDC_ALLOW) {
  3089. i_vpr_e(inst, "%s: not allowed in state: %s\n", __func__,
  3090. state_name(inst->state));
  3091. goto exit;
  3092. }
  3093. i_vpr_h(inst, "%s: queue pending batch buffers\n", __func__);
  3094. rc = msm_vidc_queue_buffer_batch(inst);
  3095. if (rc) {
  3096. i_vpr_e(inst, "%s: batch qbufs failed\n", __func__);
  3097. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  3098. }
  3099. exit:
  3100. inst_unlock(inst, __func__);
  3101. put_inst(inst);
  3102. }
  3103. int msm_vidc_flush_buffers(struct msm_vidc_inst* inst,
  3104. enum msm_vidc_buffer_type type)
  3105. {
  3106. int rc = 0;
  3107. struct msm_vidc_buffers *buffers;
  3108. struct msm_vidc_buffer *buf, *dummy;
  3109. enum msm_vidc_buffer_type buffer_type[2];
  3110. int i;
  3111. if (!inst) {
  3112. d_vpr_e("%s: invalid params\n", __func__);
  3113. return -EINVAL;
  3114. }
  3115. if (type == MSM_VIDC_BUF_INPUT) {
  3116. buffer_type[0] = MSM_VIDC_BUF_INPUT_META;
  3117. buffer_type[1] = MSM_VIDC_BUF_INPUT;
  3118. } else if (type == MSM_VIDC_BUF_OUTPUT) {
  3119. buffer_type[0] = MSM_VIDC_BUF_OUTPUT_META;
  3120. buffer_type[1] = MSM_VIDC_BUF_OUTPUT;
  3121. } else {
  3122. i_vpr_h(inst, "%s: invalid buffer type %d\n",
  3123. __func__, type);
  3124. return -EINVAL;
  3125. }
  3126. for (i = 0; i < ARRAY_SIZE(buffer_type); i++) {
  3127. buffers = msm_vidc_get_buffers(inst, buffer_type[i], __func__);
  3128. if (!buffers)
  3129. return -EINVAL;
  3130. list_for_each_entry_safe(buf, dummy, &buffers->list, list) {
  3131. if (buf->attr & MSM_VIDC_ATTR_QUEUED ||
  3132. buf->attr & MSM_VIDC_ATTR_DEFERRED) {
  3133. print_vidc_buffer(VIDC_ERR, "err ", "flushing buffer", inst, buf);
  3134. msm_vidc_vb2_buffer_done(inst, buf);
  3135. msm_vidc_put_driver_buf(inst, buf);
  3136. }
  3137. }
  3138. }
  3139. return rc;
  3140. }
  3141. void msm_vidc_destroy_buffers(struct msm_vidc_inst *inst)
  3142. {
  3143. struct msm_vidc_buffers *buffers;
  3144. struct msm_vidc_buffer *buf, *dummy;
  3145. static const enum msm_vidc_buffer_type buf_types[] = {
  3146. MSM_VIDC_BUF_INPUT,
  3147. MSM_VIDC_BUF_OUTPUT,
  3148. MSM_VIDC_BUF_INPUT_META,
  3149. MSM_VIDC_BUF_OUTPUT_META,
  3150. MSM_VIDC_BUF_BIN,
  3151. MSM_VIDC_BUF_ARP,
  3152. MSM_VIDC_BUF_COMV,
  3153. MSM_VIDC_BUF_NON_COMV,
  3154. MSM_VIDC_BUF_LINE,
  3155. MSM_VIDC_BUF_DPB,
  3156. MSM_VIDC_BUF_PERSIST,
  3157. MSM_VIDC_BUF_VPSS,
  3158. };
  3159. int i;
  3160. if (!inst) {
  3161. d_vpr_e("%s: invalid params\n", __func__);
  3162. return;
  3163. }
  3164. for (i = 0; i < ARRAY_SIZE(buf_types); i++) {
  3165. buffers = msm_vidc_get_buffers(inst, buf_types[i], __func__);
  3166. if (!buffers)
  3167. continue;
  3168. list_for_each_entry_safe(buf, dummy, &buffers->list, list) {
  3169. i_vpr_h(inst,
  3170. "destroying buffer: type %d idx %d fd %d addr %#x size %d\n",
  3171. buf->type, buf->index, buf->fd, buf->device_addr, buf->buffer_size);
  3172. if (is_internal_buffer(buf->type))
  3173. msm_vidc_destroy_internal_buffer(inst, buf);
  3174. else
  3175. msm_vidc_put_driver_buf(inst, buf);
  3176. }
  3177. }
  3178. }
  3179. static void msm_vidc_close_helper(struct kref *kref)
  3180. {
  3181. struct msm_vidc_inst *inst = container_of(kref,
  3182. struct msm_vidc_inst, kref);
  3183. i_vpr_h(inst, "%s()\n", __func__);
  3184. msm_vidc_event_queue_deinit(inst);
  3185. msm_vidc_vb2_queue_deinit(inst);
  3186. msm_vidc_debugfs_deinit_inst(inst);
  3187. if (is_decode_session(inst))
  3188. msm_vdec_inst_deinit(inst);
  3189. else if (is_encode_session(inst))
  3190. msm_venc_inst_deinit(inst);
  3191. msm_vidc_free_input_cr_list(inst);
  3192. kfree(inst->capabilities);
  3193. if (inst->response_workq)
  3194. destroy_workqueue(inst->response_workq);
  3195. kfree(inst);
  3196. }
  3197. struct msm_vidc_inst *get_inst_ref(struct msm_vidc_core *core,
  3198. struct msm_vidc_inst *instance)
  3199. {
  3200. struct msm_vidc_inst *inst = NULL;
  3201. bool matches = false;
  3202. if (!core) {
  3203. d_vpr_e("%s: invalid params\n", __func__);
  3204. return NULL;
  3205. }
  3206. mutex_lock(&core->lock);
  3207. list_for_each_entry(inst, &core->instances, list) {
  3208. if (inst == instance) {
  3209. matches = true;
  3210. break;
  3211. }
  3212. }
  3213. inst = (matches && kref_get_unless_zero(&inst->kref)) ? inst : NULL;
  3214. mutex_unlock(&core->lock);
  3215. return inst;
  3216. }
  3217. struct msm_vidc_inst *get_inst(struct msm_vidc_core *core,
  3218. u32 session_id)
  3219. {
  3220. struct msm_vidc_inst *inst = NULL;
  3221. bool matches = false;
  3222. if (!core) {
  3223. d_vpr_e("%s: invalid params\n", __func__);
  3224. return NULL;
  3225. }
  3226. mutex_lock(&core->lock);
  3227. list_for_each_entry(inst, &core->instances, list) {
  3228. if (inst->session_id == session_id) {
  3229. matches = true;
  3230. break;
  3231. }
  3232. }
  3233. inst = (matches && kref_get_unless_zero(&inst->kref)) ? inst : NULL;
  3234. mutex_unlock(&core->lock);
  3235. return inst;
  3236. }
  3237. void put_inst(struct msm_vidc_inst *inst)
  3238. {
  3239. if (!inst) {
  3240. d_vpr_e("%s: invalid params\n", __func__);
  3241. return;
  3242. }
  3243. kref_put(&inst->kref, msm_vidc_close_helper);
  3244. }
  3245. bool core_lock_check(struct msm_vidc_core *core, const char* func)
  3246. {
  3247. return mutex_is_locked(&core->lock);
  3248. }
  3249. void core_lock(struct msm_vidc_core *core, const char *function)
  3250. {
  3251. mutex_lock(&core->lock);
  3252. }
  3253. void core_unlock(struct msm_vidc_core *core, const char *function)
  3254. {
  3255. mutex_unlock(&core->lock);
  3256. }
  3257. bool inst_lock_check(struct msm_vidc_inst *inst, const char* func)
  3258. {
  3259. return mutex_is_locked(&inst->lock);
  3260. }
  3261. void inst_lock(struct msm_vidc_inst *inst, const char *function)
  3262. {
  3263. mutex_lock(&inst->lock);
  3264. }
  3265. void inst_unlock(struct msm_vidc_inst *inst, const char *function)
  3266. {
  3267. mutex_unlock(&inst->lock);
  3268. }
  3269. int msm_vidc_update_bitstream_buffer_size(struct msm_vidc_inst *inst)
  3270. {
  3271. struct msm_vidc_core *core;
  3272. struct v4l2_format *fmt;
  3273. if (!inst || !inst->core) {
  3274. d_vpr_e("%s: invalid params\n", __func__);
  3275. return -EINVAL;
  3276. }
  3277. core = inst->core;
  3278. if (is_decode_session(inst)) {
  3279. fmt = &inst->fmts[INPUT_PORT];
  3280. fmt->fmt.pix_mp.plane_fmt[0].sizeimage = call_session_op(core,
  3281. buffer_size, inst, MSM_VIDC_BUF_INPUT);
  3282. }
  3283. return 0;
  3284. }
  3285. int msm_vidc_update_meta_port_settings(struct msm_vidc_inst *inst)
  3286. {
  3287. struct msm_vidc_core *core;
  3288. struct v4l2_format *fmt;
  3289. if (!inst || !inst->core) {
  3290. d_vpr_e("%s: invalid params\n", __func__);
  3291. return -EINVAL;
  3292. }
  3293. core = inst->core;
  3294. fmt = &inst->fmts[INPUT_META_PORT];
  3295. if (is_input_meta_enabled(inst)) {
  3296. fmt->fmt.meta.buffersize = call_session_op(core,
  3297. buffer_size, inst, MSM_VIDC_BUF_INPUT_META);
  3298. inst->buffers.input_meta.min_count =
  3299. inst->buffers.input.min_count;
  3300. inst->buffers.input_meta.extra_count =
  3301. inst->buffers.input.extra_count;
  3302. inst->buffers.input_meta.actual_count =
  3303. inst->buffers.input.actual_count;
  3304. inst->buffers.input_meta.size = fmt->fmt.meta.buffersize;
  3305. } else {
  3306. fmt->fmt.meta.buffersize = 0;
  3307. inst->buffers.input_meta.min_count = 0;
  3308. inst->buffers.input_meta.extra_count = 0;
  3309. inst->buffers.input_meta.actual_count = 0;
  3310. inst->buffers.input_meta.size = 0;
  3311. }
  3312. fmt = &inst->fmts[OUTPUT_META_PORT];
  3313. if (is_output_meta_enabled(inst)) {
  3314. fmt->fmt.meta.buffersize = call_session_op(core,
  3315. buffer_size, inst, MSM_VIDC_BUF_OUTPUT_META);
  3316. inst->buffers.output_meta.min_count =
  3317. inst->buffers.output.min_count;
  3318. inst->buffers.output_meta.extra_count =
  3319. inst->buffers.output.extra_count;
  3320. inst->buffers.output_meta.actual_count =
  3321. inst->buffers.output.actual_count;
  3322. inst->buffers.output_meta.size = fmt->fmt.meta.buffersize;
  3323. } else {
  3324. fmt->fmt.meta.buffersize = 0;
  3325. inst->buffers.output_meta.min_count = 0;
  3326. inst->buffers.output_meta.extra_count = 0;
  3327. inst->buffers.output_meta.actual_count = 0;
  3328. inst->buffers.output_meta.size = 0;
  3329. }
  3330. return 0;
  3331. }
  3332. void msm_vidc_schedule_core_deinit(struct msm_vidc_core *core)
  3333. {
  3334. if (!core)
  3335. return;
  3336. if (!core->capabilities[FW_UNLOAD].value)
  3337. return;
  3338. cancel_delayed_work(&core->fw_unload_work);
  3339. schedule_delayed_work(&core->fw_unload_work,
  3340. msecs_to_jiffies(core->capabilities[FW_UNLOAD_DELAY].value));
  3341. d_vpr_h("firmware unload delayed by %u ms\n",
  3342. core->capabilities[FW_UNLOAD_DELAY].value);
  3343. return;
  3344. }
  3345. static const char *get_codec_str(enum msm_vidc_codec_type type)
  3346. {
  3347. switch (type) {
  3348. case MSM_VIDC_H264: return "h264";
  3349. case MSM_VIDC_HEVC: return "h265";
  3350. case MSM_VIDC_VP9: return " vp9";
  3351. case MSM_VIDC_HEIC: return "heic";
  3352. }
  3353. return "....";
  3354. }
  3355. static const char *get_domain_str(enum msm_vidc_domain_type type)
  3356. {
  3357. switch (type) {
  3358. case MSM_VIDC_ENCODER: return "e";
  3359. case MSM_VIDC_DECODER: return "d";
  3360. }
  3361. return ".";
  3362. }
  3363. int msm_vidc_update_debug_str(struct msm_vidc_inst *inst)
  3364. {
  3365. u32 sid;
  3366. const char *codec;
  3367. const char *domain;
  3368. if (!inst) {
  3369. d_vpr_e("%s: Invalid params\n", __func__);
  3370. return -EINVAL;
  3371. }
  3372. sid = inst->session_id;
  3373. codec = get_codec_str(inst->codec);
  3374. domain = get_domain_str(inst->domain);
  3375. snprintf(inst->debug_str, sizeof(inst->debug_str), "%08x: %s%s", sid, codec, domain);
  3376. d_vpr_h("%s: sid: %08x, codec: %s, domain: %s, final: %s\n",
  3377. __func__, sid, codec, domain, inst->debug_str);
  3378. return 0;
  3379. }
  3380. static int msm_vidc_check_mbps_supported(struct msm_vidc_inst *inst)
  3381. {
  3382. u32 mbps = 0;
  3383. struct msm_vidc_core *core;
  3384. struct msm_vidc_inst *instance;
  3385. if (!inst || !inst->core) {
  3386. d_vpr_e("%s: invalid params\n", __func__);
  3387. return -EINVAL;
  3388. }
  3389. core = inst->core;
  3390. core_lock(core, __func__);
  3391. list_for_each_entry(instance, &core->instances, list) {
  3392. /* ignore invalid/error session */
  3393. if (instance->state == MSM_VIDC_ERROR)
  3394. continue;
  3395. /* ignore thumbnail, image, and non realtime sessions */
  3396. if (is_thumbnail_session(instance) ||
  3397. is_image_session(instance) ||
  3398. !is_realtime_session(instance))
  3399. continue;
  3400. mbps += msm_vidc_get_inst_load(instance, LOAD_ADMISSION_CONTROL);
  3401. }
  3402. core_unlock(core, __func__);
  3403. if (mbps > core->capabilities[MAX_MBPS].value) {
  3404. /* todo: print running instances */
  3405. //msm_vidc_print_running_insts(inst->core);
  3406. return -ENOMEM;
  3407. }
  3408. return 0;
  3409. }
  3410. static int msm_vidc_check_mbpf_supported(struct msm_vidc_inst *inst)
  3411. {
  3412. u32 mbpf = 0;
  3413. struct msm_vidc_core *core;
  3414. struct msm_vidc_inst *instance;
  3415. if (!inst || !inst->core) {
  3416. d_vpr_e("%s: invalid params\n", __func__);
  3417. return -EINVAL;
  3418. }
  3419. core = inst->core;
  3420. core_lock(core, __func__);
  3421. list_for_each_entry(instance, &core->instances, list) {
  3422. /* ignore invalid/error session */
  3423. if (instance->state == MSM_VIDC_ERROR)
  3424. continue;
  3425. /* ignore thumbnail, image, and non realtime sessions */
  3426. if (is_thumbnail_session(instance) ||
  3427. is_image_session(instance) ||
  3428. !is_realtime_session(instance))
  3429. continue;
  3430. mbpf += msm_vidc_get_mbs_per_frame(instance);
  3431. }
  3432. core_unlock(core, __func__);
  3433. if (mbpf > core->capabilities[MAX_MBPF].value) {
  3434. /* todo: print running instances */
  3435. //msm_vidc_print_running_insts(inst->core);
  3436. return -ENOMEM;
  3437. }
  3438. return 0;
  3439. }
  3440. int msm_vidc_check_session_supported(struct msm_vidc_inst *inst)
  3441. {
  3442. struct msm_vidc_inst_capability *capability;
  3443. struct v4l2_format *fmt;
  3444. u32 pix_fmt, profile;
  3445. bool allow = false;
  3446. int rc = 0;
  3447. if (!inst || !inst->capabilities) {
  3448. d_vpr_e("%s: invalid params\n", __func__);
  3449. return -EINVAL;
  3450. }
  3451. capability = inst->capabilities;
  3452. /* todo: enable checks for all session type */
  3453. if (!is_image_session(inst))
  3454. return 0;
  3455. pix_fmt = capability->cap[PIX_FMTS].value;
  3456. profile = capability->cap[PROFILE].value;
  3457. if (is_image_encode_session(inst)) {
  3458. /* is linear color fmt */
  3459. allow = is_linear_colorformat(pix_fmt);
  3460. if (!allow) {
  3461. i_vpr_e(inst, "%s: compressed fmt: %#x\n", __func__, pix_fmt);
  3462. goto exit;
  3463. }
  3464. /* is input grid aligned */
  3465. fmt = &inst->fmts[INPUT_PORT];
  3466. allow = IS_ALIGNED(fmt->fmt.pix_mp.width, HEIC_GRID_DIMENSION);
  3467. allow &= IS_ALIGNED(fmt->fmt.pix_mp.height, HEIC_GRID_DIMENSION);
  3468. if (!allow) {
  3469. i_vpr_e(inst, "%s: input is not grid aligned: %u x %u\n", __func__,
  3470. fmt->fmt.pix_mp.width, fmt->fmt.pix_mp.height);
  3471. goto exit;
  3472. }
  3473. /* is output grid dimension */
  3474. fmt = &inst->fmts[OUTPUT_PORT];
  3475. allow = fmt->fmt.pix_mp.width == HEIC_GRID_DIMENSION;
  3476. allow &= fmt->fmt.pix_mp.height == HEIC_GRID_DIMENSION;
  3477. if (!allow) {
  3478. i_vpr_e(inst, "%s: output is not a grid dimension: %u x %u\n", __func__,
  3479. fmt->fmt.pix_mp.width, fmt->fmt.pix_mp.height);
  3480. goto exit;
  3481. }
  3482. /* is bitrate mode CQ */
  3483. allow = capability->cap[BITRATE_MODE].value == HFI_RC_CQ;
  3484. if (!allow) {
  3485. i_vpr_e(inst, "%s: bitrate mode is not CQ: %#x\n", __func__,
  3486. capability->cap[BITRATE_MODE].value);
  3487. goto exit;
  3488. }
  3489. /* is all intra */
  3490. allow = !capability->cap[GOP_SIZE].value;
  3491. allow &= !capability->cap[B_FRAME].value;
  3492. if (!allow) {
  3493. i_vpr_e(inst, "%s: not all intra: gop: %u, bframe: %u\n", __func__,
  3494. capability->cap[GOP_SIZE].value, capability->cap[B_FRAME].value);
  3495. goto exit;
  3496. }
  3497. /* is time delta based rc disabled */
  3498. allow = !capability->cap[TIME_DELTA_BASED_RC].value;
  3499. if (!allow) {
  3500. i_vpr_e(inst, "%s: time delta based rc not disabled: %#x\n", __func__,
  3501. capability->cap[TIME_DELTA_BASED_RC].value);
  3502. goto exit;
  3503. }
  3504. /* is profile type Still Pic */
  3505. if (is_10bit_colorformat(pix_fmt))
  3506. allow = profile == V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10;
  3507. else
  3508. allow = profile == V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE;
  3509. if (!allow) {
  3510. i_vpr_e(inst, "%s: profile not valid: %#x\n", __func__,
  3511. capability->cap[PROFILE].value);
  3512. goto exit;
  3513. }
  3514. }
  3515. rc = msm_vidc_check_mbps_supported(inst);
  3516. if (rc)
  3517. goto exit;
  3518. rc = msm_vidc_check_mbpf_supported(inst);
  3519. if (rc)
  3520. goto exit;
  3521. /* todo: add additional checks related to capabilities */
  3522. return 0;
  3523. exit:
  3524. i_vpr_e(inst, "%s: current session not supported\n", __func__);
  3525. return -EINVAL;
  3526. }
  3527. int msm_vidc_check_scaling_supported(struct msm_vidc_inst *inst)
  3528. {
  3529. if (!inst) {
  3530. d_vpr_e("%s: invalid params\n", __func__);
  3531. return -EINVAL;
  3532. }
  3533. if (is_image_session(inst) || is_decode_session(inst)) {
  3534. i_vpr_h(inst, "%s: Scaling is supported for encode session only\n", __func__);
  3535. return 0;
  3536. }
  3537. /* todo: add scaling check for encode session */
  3538. return 0;
  3539. }