msm_vidc_driver.c 121 KB

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