msm_vidc_driver.c 126 KB

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