msm_vidc_driver.c 113 KB

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