msm_vidc_driver.c 144 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2022, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/iommu.h>
  7. #include <linux/workqueue.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_state.h"
  15. #include "msm_vidc_power.h"
  16. #include "msm_vidc_debug.h"
  17. #include "msm_vidc.h"
  18. #include "msm_vdec.h"
  19. #include "msm_venc.h"
  20. #include "msm_vidc_fence.h"
  21. #include "venus_hfi.h"
  22. #include "venus_hfi_response.h"
  23. #include "hfi_packet.h"
  24. #include "msm_vidc_events.h"
  25. extern struct msm_vidc_core *g_core;
  26. #define is_odd(val) ((val) % 2 == 1)
  27. #define in_range(val, min, max) (((min) <= (val)) && ((val) <= (max)))
  28. #define COUNT_BITS(a, out) { \
  29. while ((a) >= 1) { \
  30. (out) += (a) & (1); \
  31. (a) >>= (1); \
  32. } \
  33. }
  34. #define SSR_TYPE 0x0000000F
  35. #define SSR_TYPE_SHIFT 0
  36. #define SSR_SUB_CLIENT_ID 0x000000F0
  37. #define SSR_SUB_CLIENT_ID_SHIFT 4
  38. #define SSR_ADDR_ID 0xFFFFFFFF00000000
  39. #define SSR_ADDR_SHIFT 32
  40. #define STABILITY_TYPE 0x0000000F
  41. #define STABILITY_TYPE_SHIFT 0
  42. #define STABILITY_SUB_CLIENT_ID 0x000000F0
  43. #define STABILITY_SUB_CLIENT_ID_SHIFT 4
  44. #define STABILITY_PAYLOAD_ID 0xFFFFFFFF00000000
  45. #define STABILITY_PAYLOAD_SHIFT 32
  46. /* do not modify the cap names as it is used in test scripts */
  47. static const char * const cap_name_arr[] =
  48. FOREACH_CAP(GENERATE_STRING);
  49. const char *cap_name(enum msm_vidc_inst_capability_type cap_id)
  50. {
  51. const char *name = "UNKNOWN CAP";
  52. if (cap_id >= ARRAY_SIZE(cap_name_arr))
  53. goto exit;
  54. name = cap_name_arr[cap_id];
  55. exit:
  56. return name;
  57. }
  58. static const char * const buf_type_name_arr[] =
  59. FOREACH_BUF_TYPE(GENERATE_STRING);
  60. const char *buf_name(enum msm_vidc_buffer_type type)
  61. {
  62. const char *name = "UNKNOWN BUF";
  63. if (type >= ARRAY_SIZE(buf_type_name_arr))
  64. goto exit;
  65. name = buf_type_name_arr[type];
  66. exit:
  67. return name;
  68. }
  69. static const char * const inst_allow_name_arr[] =
  70. FOREACH_ALLOW(GENERATE_STRING);
  71. const char *allow_name(enum msm_vidc_allow allow)
  72. {
  73. const char *name = "UNKNOWN";
  74. if (allow >= ARRAY_SIZE(inst_allow_name_arr))
  75. goto exit;
  76. name = inst_allow_name_arr[allow];
  77. exit:
  78. return name;
  79. }
  80. const char *v4l2_type_name(u32 port)
  81. {
  82. switch (port) {
  83. case INPUT_MPLANE: return "INPUT";
  84. case OUTPUT_MPLANE: return "OUTPUT";
  85. case INPUT_META_PLANE: return "INPUT_META";
  86. case OUTPUT_META_PLANE: return "OUTPUT_META";
  87. }
  88. return "UNKNOWN";
  89. }
  90. const char *v4l2_pixelfmt_name(struct msm_vidc_inst *inst, u32 pixfmt)
  91. {
  92. struct msm_vidc_core *core;
  93. const struct codec_info *codec_info;
  94. const struct color_format_info *color_format_info;
  95. u32 i, size;
  96. core = inst->core;
  97. if (!core->platform || !core->platform->data.format_data) {
  98. d_vpr_e("%s: invalid core platform\n", __func__);
  99. goto exit;
  100. }
  101. codec_info = core->platform->data.format_data->codec_info;
  102. size = core->platform->data.format_data->codec_info_size;
  103. for (i = 0; i < size; i++) {
  104. if (codec_info[i].v4l2_codec == pixfmt)
  105. return codec_info[i].pixfmt_name;
  106. }
  107. color_format_info = core->platform->data.format_data->color_format_info;
  108. size = core->platform->data.format_data->color_format_info_size;
  109. for (i = 0; i < size; i++) {
  110. if (color_format_info[i].v4l2_color_format == pixfmt)
  111. return color_format_info[i].pixfmt_name;
  112. }
  113. exit:
  114. return "UNKNOWN";
  115. }
  116. void print_vidc_buffer(u32 tag, const char *tag_str, const char *str, struct msm_vidc_inst *inst,
  117. struct msm_vidc_buffer *vbuf)
  118. {
  119. struct dma_buf *dbuf;
  120. struct inode *f_inode;
  121. unsigned long inode_num = 0;
  122. long ref_count = -1;
  123. if (!vbuf || !tag_str || !str)
  124. return;
  125. dbuf = (struct dma_buf *)vbuf->dmabuf;
  126. if (dbuf && dbuf->file) {
  127. f_inode = file_inode(dbuf->file);
  128. if (f_inode) {
  129. inode_num = f_inode->i_ino;
  130. ref_count = file_count(dbuf->file);
  131. }
  132. }
  133. dprintk_inst(tag, tag_str, inst,
  134. "%s: %s: idx %2d fd %3d off %d daddr %#llx inode %8lu ref %2ld size %8d filled %8d flags %#x ts %8lld attr %#x dbuf_get %d attach %d map %d counts(etb ebd ftb fbd) %4llu %4llu %4llu %4llu\n",
  135. str, buf_name(vbuf->type),
  136. vbuf->index, vbuf->fd, vbuf->data_offset,
  137. vbuf->device_addr, inode_num, ref_count, vbuf->buffer_size,
  138. vbuf->data_size, vbuf->flags, vbuf->timestamp, vbuf->attr,
  139. vbuf->dbuf_get, vbuf->attach ? 1 : 0, vbuf->sg_table ? 1 : 0,
  140. inst->debug_count.etb, inst->debug_count.ebd,
  141. inst->debug_count.ftb, inst->debug_count.fbd);
  142. trace_msm_v4l2_vidc_buffer_event_log(inst, str, buf_name(vbuf->type), vbuf,
  143. inode_num, ref_count);
  144. }
  145. void print_vb2_buffer(const char *str, struct msm_vidc_inst *inst,
  146. struct vb2_buffer *vb2)
  147. {
  148. if (vb2->type == INPUT_MPLANE || vb2->type == OUTPUT_MPLANE) {
  149. i_vpr_e(inst,
  150. "%s: %s: idx %2d fd %d off %d size %d filled %d\n",
  151. str, vb2->type == INPUT_MPLANE ? "INPUT" : "OUTPUT",
  152. vb2->index, vb2->planes[0].m.fd,
  153. vb2->planes[0].data_offset, vb2->planes[0].length,
  154. vb2->planes[0].bytesused);
  155. } else if (vb2->type == INPUT_META_PLANE || vb2->type == OUTPUT_META_PLANE) {
  156. i_vpr_e(inst,
  157. "%s: %s: idx %2d fd %d off %d size %d filled %d\n",
  158. str, vb2->type == INPUT_MPLANE ? "INPUT_META" : "OUTPUT_META",
  159. vb2->index, vb2->planes[0].m.fd,
  160. vb2->planes[0].data_offset, vb2->planes[0].length,
  161. vb2->planes[0].bytesused);
  162. }
  163. }
  164. static void print_buffer_stats(u32 tag, const char *tag_str, struct msm_vidc_inst *inst,
  165. struct msm_vidc_buffer_stats *stats)
  166. {
  167. if (!tag_str || !stats)
  168. return;
  169. /* skip flushed buffer stats */
  170. if (!stats->etb_time_ms || !stats->ebd_time_ms ||
  171. !stats->ftb_time_ms || !stats->fbd_time_ms)
  172. return;
  173. dprintk_inst(tag, tag_str, inst,
  174. "f.no %4u ts %16llu (etb ebd ftb fbd)ms %6u %6u %6u %6u (ebd-etb fbd-etb etb-ftb)ms %4d %4d %4d size %8u attr %#x\n",
  175. stats->frame_num, stats->timestamp, stats->etb_time_ms, stats->ebd_time_ms,
  176. stats->ftb_time_ms, stats->fbd_time_ms, stats->ebd_time_ms - stats->etb_time_ms,
  177. stats->fbd_time_ms - stats->etb_time_ms, stats->etb_time_ms - stats->ftb_time_ms,
  178. stats->data_size, stats->flags);
  179. }
  180. static u32 msm_vidc_get_buffer_stats_flag(struct msm_vidc_inst *inst)
  181. {
  182. u32 flags = 0;
  183. if (inst->hfi_frame_info.data_corrupt)
  184. flags |= MSM_VIDC_STATS_FLAG_CORRUPT;
  185. if (inst->hfi_frame_info.overflow)
  186. flags |= MSM_VIDC_STATS_FLAG_OVERFLOW;
  187. if (inst->hfi_frame_info.no_output)
  188. flags |= MSM_VIDC_STATS_FLAG_NO_OUTPUT;
  189. if (inst->hfi_frame_info.subframe_input)
  190. flags |= MSM_VIDC_STATS_FLAG_SUBFRAME_INPUT;
  191. return flags;
  192. }
  193. int msm_vidc_suspend(struct msm_vidc_core *core)
  194. {
  195. int rc = 0;
  196. if (!core) {
  197. d_vpr_e("%s: invalid params\n", __func__);
  198. return -EINVAL;
  199. }
  200. rc = venus_hfi_suspend(core);
  201. if (rc)
  202. return rc;
  203. return rc;
  204. }
  205. int msm_vidc_add_buffer_stats(struct msm_vidc_inst *inst,
  206. struct msm_vidc_buffer *buf, u64 timestamp)
  207. {
  208. struct msm_vidc_buffer_stats *stats = NULL;
  209. if (!(msm_vidc_debug & VIDC_LOW))
  210. return 0;
  211. /* stats applicable only to input & output buffers */
  212. if (buf->type != MSM_VIDC_BUF_INPUT && buf->type != MSM_VIDC_BUF_OUTPUT)
  213. return -EINVAL;
  214. /* update start timestamp */
  215. buf->start_time_ms = (ktime_get_ns() / 1000 - inst->initial_time_us) / 1000;
  216. /* add buffer stats only in ETB path */
  217. if (buf->type != MSM_VIDC_BUF_INPUT)
  218. return 0;
  219. stats = msm_vidc_pool_alloc(inst, MSM_MEM_POOL_BUF_STATS);
  220. if (!stats)
  221. return -ENOMEM;
  222. INIT_LIST_HEAD(&stats->list);
  223. list_add_tail(&stats->list, &inst->buffer_stats_list);
  224. stats->frame_num = inst->debug_count.etb;
  225. stats->timestamp = timestamp;
  226. stats->ts_offset = 0;
  227. stats->etb_time_ms = buf->start_time_ms;
  228. if (is_decode_session(inst))
  229. stats->data_size = buf->data_size;
  230. return 0;
  231. }
  232. int msm_vidc_remove_buffer_stats(struct msm_vidc_inst *inst,
  233. struct msm_vidc_buffer *buf, u64 timestamp)
  234. {
  235. struct msm_vidc_buffer_stats *stats = NULL, *dummy_stats = NULL;
  236. struct msm_vidc_buffer_stats *prev_stats = NULL;
  237. bool remove_stat = false, is_first_stat = false;;
  238. if (!(msm_vidc_debug & VIDC_LOW))
  239. return 0;
  240. /* stats applicable only to input & output buffers */
  241. if (buf->type != MSM_VIDC_BUF_INPUT && buf->type != MSM_VIDC_BUF_OUTPUT)
  242. return -EINVAL;
  243. /* update end timestamp */
  244. buf->end_time_ms = (ktime_get_ns() / 1000 - inst->initial_time_us) / 1000;
  245. list_for_each_entry_safe(stats, dummy_stats, &inst->buffer_stats_list, list) {
  246. if (stats->timestamp - stats->ts_offset != timestamp)
  247. continue;
  248. remove_stat = false;
  249. if (buf->type == MSM_VIDC_BUF_INPUT) {
  250. /* skip - ebd already updated(multiple input - single output case) */
  251. if (stats->ebd_time_ms)
  252. continue;
  253. /* ebd: update end ts and return */
  254. stats->ebd_time_ms = buf->end_time_ms;
  255. stats->flags |= msm_vidc_get_buffer_stats_flag(inst);
  256. /* multi in - single out (interlace/slice decoding case) */
  257. is_first_stat = list_is_first(&stats->list, &inst->buffer_stats_list);
  258. if (!is_first_stat) {
  259. prev_stats = list_prev_entry(stats, list);
  260. /* add offset if FW requires more etb's to process output */
  261. if (prev_stats->flags & MSM_VIDC_STATS_FLAG_SUBFRAME_INPUT)
  262. stats->ts_offset = stats->timestamp - prev_stats->timestamp;
  263. }
  264. /* remove entry - no output attached */
  265. remove_stat = !!(stats->flags & MSM_VIDC_STATS_FLAG_NO_OUTPUT);
  266. remove_stat |= stats->ebd_time_ms && stats->fbd_time_ms;
  267. } else if (buf->type == MSM_VIDC_BUF_OUTPUT) {
  268. /* skip - ebd already updated(encoder superframe case) */
  269. if (stats->fbd_time_ms)
  270. continue;
  271. /* fbd: update end ts */
  272. stats->ftb_time_ms = buf->start_time_ms;
  273. stats->fbd_time_ms = buf->end_time_ms;
  274. stats->flags |= msm_vidc_get_buffer_stats_flag(inst);
  275. if (is_encode_session(inst))
  276. stats->data_size = buf->data_size;
  277. remove_stat |= stats->ebd_time_ms && stats->fbd_time_ms;
  278. }
  279. /* remove stats node */
  280. if (remove_stat) {
  281. list_del_init(&stats->list);
  282. print_buffer_stats(VIDC_LOW, "low ", inst, stats);
  283. msm_vidc_pool_free(inst, stats);
  284. }
  285. }
  286. return 0;
  287. }
  288. int msm_vidc_flush_buffer_stats(struct msm_vidc_inst *inst)
  289. {
  290. struct msm_vidc_buffer_stats *stats, *dummy_stats;
  291. i_vpr_l(inst, "%s: flush buffer_stats list\n", __func__);
  292. list_for_each_entry_safe(stats, dummy_stats, &inst->buffer_stats_list, list) {
  293. list_del_init(&stats->list);
  294. msm_vidc_pool_free(inst, stats);
  295. }
  296. /* reset initial ts as well to avoid huge delta */
  297. inst->initial_time_us = ktime_get_ns() / 1000;
  298. return 0;
  299. }
  300. enum msm_vidc_buffer_type v4l2_type_to_driver(u32 type, const char *func)
  301. {
  302. enum msm_vidc_buffer_type buffer_type = 0;
  303. switch (type) {
  304. case INPUT_MPLANE:
  305. buffer_type = MSM_VIDC_BUF_INPUT;
  306. break;
  307. case OUTPUT_MPLANE:
  308. buffer_type = MSM_VIDC_BUF_OUTPUT;
  309. break;
  310. case INPUT_META_PLANE:
  311. buffer_type = MSM_VIDC_BUF_INPUT_META;
  312. break;
  313. case OUTPUT_META_PLANE:
  314. buffer_type = MSM_VIDC_BUF_OUTPUT_META;
  315. break;
  316. default:
  317. d_vpr_e("%s: invalid v4l2 buffer type %#x\n", func, type);
  318. break;
  319. }
  320. return buffer_type;
  321. }
  322. u32 v4l2_type_from_driver(enum msm_vidc_buffer_type buffer_type,
  323. const char *func)
  324. {
  325. u32 type = 0;
  326. switch (buffer_type) {
  327. case MSM_VIDC_BUF_INPUT:
  328. type = INPUT_MPLANE;
  329. break;
  330. case MSM_VIDC_BUF_OUTPUT:
  331. type = OUTPUT_MPLANE;
  332. break;
  333. case MSM_VIDC_BUF_INPUT_META:
  334. type = INPUT_META_PLANE;
  335. break;
  336. case MSM_VIDC_BUF_OUTPUT_META:
  337. type = OUTPUT_META_PLANE;
  338. break;
  339. default:
  340. d_vpr_e("%s: invalid driver buffer type %d\n",
  341. func, buffer_type);
  342. break;
  343. }
  344. return type;
  345. }
  346. enum msm_vidc_codec_type v4l2_codec_to_driver(struct msm_vidc_inst *inst,
  347. u32 v4l2_codec, const char *func)
  348. {
  349. struct msm_vidc_core *core;
  350. const struct codec_info *codec_info;
  351. u32 i, size;
  352. enum msm_vidc_codec_type codec = 0;
  353. core = inst->core;
  354. if (!core->platform || !core->platform->data.format_data) {
  355. d_vpr_e("%s: invalid core platform\n", __func__);
  356. return -EINVAL;
  357. }
  358. codec_info = core->platform->data.format_data->codec_info;
  359. size = core->platform->data.format_data->codec_info_size;
  360. for (i = 0; i < size; i++) {
  361. if (codec_info[i].v4l2_codec == v4l2_codec)
  362. return codec_info[i].vidc_codec;
  363. }
  364. d_vpr_h("%s: invalid v4l2 codec %#x\n", func, v4l2_codec);
  365. return codec;
  366. }
  367. u32 v4l2_codec_from_driver(struct msm_vidc_inst *inst,
  368. enum msm_vidc_codec_type codec, const char *func)
  369. {
  370. struct msm_vidc_core *core;
  371. const struct codec_info *codec_info;
  372. u32 i, size;
  373. u32 v4l2_codec = 0;
  374. core = inst->core;
  375. if (!core->platform || !core->platform->data.format_data) {
  376. d_vpr_e("%s: invalid core platform\n", __func__);
  377. return -EINVAL;
  378. }
  379. codec_info = core->platform->data.format_data->codec_info;
  380. size = core->platform->data.format_data->codec_info_size;
  381. for (i = 0; i < size; i++) {
  382. if (codec_info[i].vidc_codec == codec)
  383. return codec_info[i].v4l2_codec;
  384. }
  385. d_vpr_e("%s: invalid driver codec %#x\n", func, codec);
  386. return v4l2_codec;
  387. }
  388. enum msm_vidc_colorformat_type v4l2_colorformat_to_driver(
  389. struct msm_vidc_inst *inst,
  390. u32 v4l2_colorformat, const char *func)
  391. {
  392. struct msm_vidc_core *core;
  393. const struct color_format_info *color_format_info;
  394. u32 i, size;
  395. enum msm_vidc_colorformat_type colorformat = 0;
  396. core = inst->core;
  397. if (!core->platform || !core->platform->data.format_data) {
  398. d_vpr_e("%s: invalid core platform\n", __func__);
  399. return -EINVAL;
  400. }
  401. color_format_info = core->platform->data.format_data->color_format_info;
  402. size = core->platform->data.format_data->color_format_info_size;
  403. for (i = 0; i < size; i++) {
  404. if (color_format_info[i].v4l2_color_format == v4l2_colorformat)
  405. return color_format_info[i].vidc_color_format;
  406. }
  407. d_vpr_e("%s: invalid v4l2 color format %#x\n", func, v4l2_colorformat);
  408. return colorformat;
  409. }
  410. u32 v4l2_colorformat_from_driver(struct msm_vidc_inst *inst,
  411. enum msm_vidc_colorformat_type colorformat,
  412. const char *func)
  413. {
  414. struct msm_vidc_core *core;
  415. const struct color_format_info *color_format_info;
  416. u32 i, size;
  417. u32 v4l2_colorformat = 0;
  418. core = inst->core;
  419. if (!core->platform || !core->platform->data.format_data) {
  420. d_vpr_e("%s: invalid core platform\n", __func__);
  421. return -EINVAL;
  422. }
  423. color_format_info = core->platform->data.format_data->color_format_info;
  424. size = core->platform->data.format_data->color_format_info_size;
  425. for (i = 0; i < size; i++) {
  426. if (color_format_info[i].vidc_color_format == colorformat)
  427. return color_format_info[i].v4l2_color_format;
  428. }
  429. d_vpr_e("%s: invalid driver color format %#x\n", func, colorformat);
  430. return v4l2_colorformat;
  431. }
  432. u32 v4l2_color_primaries_to_driver(struct msm_vidc_inst *inst,
  433. u32 v4l2_primaries, const char *func)
  434. {
  435. struct msm_vidc_core *core;
  436. const struct color_primaries_info *color_prim_info;
  437. u32 i, size;
  438. u32 vidc_color_primaries = MSM_VIDC_PRIMARIES_RESERVED;
  439. core = inst->core;
  440. if (!core->platform || !core->platform->data.format_data) {
  441. d_vpr_e("%s: invalid core platform\n", __func__);
  442. return -EINVAL;
  443. }
  444. color_prim_info = core->platform->data.format_data->color_prim_info;
  445. size = core->platform->data.format_data->color_prim_info_size;
  446. for (i = 0; i < size; i++) {
  447. if (color_prim_info[i].v4l2_color_primaries == v4l2_primaries)
  448. return color_prim_info[i].vidc_color_primaries;
  449. }
  450. i_vpr_e(inst, "%s: invalid v4l2 color primaries %d\n",
  451. func, v4l2_primaries);
  452. return vidc_color_primaries;
  453. }
  454. u32 v4l2_color_primaries_from_driver(struct msm_vidc_inst *inst,
  455. u32 vidc_color_primaries, const char *func)
  456. {
  457. struct msm_vidc_core *core;
  458. const struct color_primaries_info *color_prim_info;
  459. u32 i, size;
  460. u32 v4l2_primaries = V4L2_COLORSPACE_DEFAULT;
  461. core = inst->core;
  462. if (!core->platform || !core->platform->data.format_data) {
  463. d_vpr_e("%s: invalid core platform\n", __func__);
  464. return -EINVAL;
  465. }
  466. color_prim_info = core->platform->data.format_data->color_prim_info;
  467. size = core->platform->data.format_data->color_prim_info_size;
  468. for (i = 0; i < size; i++) {
  469. if (color_prim_info[i].vidc_color_primaries == vidc_color_primaries)
  470. return color_prim_info[i].v4l2_color_primaries;
  471. }
  472. i_vpr_e(inst, "%s: invalid hfi color primaries %d\n",
  473. func, vidc_color_primaries);
  474. return v4l2_primaries;
  475. }
  476. u32 v4l2_transfer_char_to_driver(struct msm_vidc_inst *inst,
  477. u32 v4l2_transfer_char, const char *func)
  478. {
  479. struct msm_vidc_core *core;
  480. const struct transfer_char_info *transfer_char_info;
  481. u32 i, size;
  482. u32 vidc_transfer_char = MSM_VIDC_TRANSFER_RESERVED;
  483. core = inst->core;
  484. if (!core->platform || !core->platform->data.format_data) {
  485. d_vpr_e("%s: invalid core platform\n", __func__);
  486. return -EINVAL;
  487. }
  488. transfer_char_info = core->platform->data.format_data->transfer_char_info;
  489. size = core->platform->data.format_data->transfer_char_info_size;
  490. for (i = 0; i < size; i++) {
  491. if (transfer_char_info[i].v4l2_transfer_char == v4l2_transfer_char)
  492. return transfer_char_info[i].vidc_transfer_char;
  493. }
  494. i_vpr_e(inst, "%s: invalid v4l2 transfer char %d\n",
  495. func, v4l2_transfer_char);
  496. return vidc_transfer_char;
  497. }
  498. u32 v4l2_transfer_char_from_driver(struct msm_vidc_inst *inst,
  499. u32 vidc_transfer_char, const char *func)
  500. {
  501. struct msm_vidc_core *core;
  502. const struct transfer_char_info *transfer_char_info;
  503. u32 i, size;
  504. u32 v4l2_transfer_char = V4L2_XFER_FUNC_DEFAULT;
  505. core = inst->core;
  506. if (!core->platform || !core->platform->data.format_data) {
  507. d_vpr_e("%s: invalid core platform\n", __func__);
  508. return -EINVAL;
  509. }
  510. transfer_char_info = core->platform->data.format_data->transfer_char_info;
  511. size = core->platform->data.format_data->transfer_char_info_size;
  512. for (i = 0; i < size; i++) {
  513. if (transfer_char_info[i].vidc_transfer_char == vidc_transfer_char)
  514. return transfer_char_info[i].v4l2_transfer_char;
  515. }
  516. i_vpr_e(inst, "%s: invalid hfi transfer char %d\n",
  517. func, vidc_transfer_char);
  518. return v4l2_transfer_char;
  519. }
  520. u32 v4l2_matrix_coeff_to_driver(struct msm_vidc_inst *inst,
  521. u32 v4l2_matrix_coeff, const char *func)
  522. {
  523. struct msm_vidc_core *core;
  524. const struct matrix_coeff_info *matrix_coeff_info;
  525. u32 i, size;
  526. u32 vidc_matrix_coeff = MSM_VIDC_MATRIX_COEFF_RESERVED;
  527. core = inst->core;
  528. if (!core->platform || !core->platform->data.format_data) {
  529. d_vpr_e("%s: invalid core platform\n", __func__);
  530. return -EINVAL;
  531. }
  532. matrix_coeff_info = core->platform->data.format_data->matrix_coeff_info;
  533. size = core->platform->data.format_data->matrix_coeff_info_size;
  534. for (i = 0; i < size; i++) {
  535. if (matrix_coeff_info[i].v4l2_matrix_coeff == v4l2_matrix_coeff)
  536. return matrix_coeff_info[i].vidc_matrix_coeff;
  537. }
  538. i_vpr_e(inst, "%s: invalid v4l2 matrix coeff %d\n",
  539. func, v4l2_matrix_coeff);
  540. return vidc_matrix_coeff;
  541. }
  542. u32 v4l2_matrix_coeff_from_driver(struct msm_vidc_inst *inst,
  543. u32 vidc_matrix_coeff, const char *func)
  544. {
  545. struct msm_vidc_core *core;
  546. const struct matrix_coeff_info *matrix_coeff_info;
  547. u32 i, size;
  548. u32 v4l2_matrix_coeff = V4L2_YCBCR_ENC_DEFAULT;
  549. core = inst->core;
  550. if (!core->platform || !core->platform->data.format_data) {
  551. d_vpr_e("%s: invalid core platform\n", __func__);
  552. return -EINVAL;
  553. }
  554. matrix_coeff_info = core->platform->data.format_data->matrix_coeff_info;
  555. size = core->platform->data.format_data->matrix_coeff_info_size;
  556. for (i = 0; i < size; i++) {
  557. if (matrix_coeff_info[i].vidc_matrix_coeff == vidc_matrix_coeff)
  558. return matrix_coeff_info[i].v4l2_matrix_coeff;
  559. }
  560. i_vpr_e(inst, "%s: invalid hfi matrix coeff %d\n",
  561. func, vidc_matrix_coeff);
  562. return v4l2_matrix_coeff;
  563. }
  564. int v4l2_type_to_driver_port(struct msm_vidc_inst *inst, u32 type,
  565. const char *func)
  566. {
  567. int port;
  568. if (type == INPUT_MPLANE) {
  569. port = INPUT_PORT;
  570. } else if (type == INPUT_META_PLANE) {
  571. port = INPUT_META_PORT;
  572. } else if (type == OUTPUT_MPLANE) {
  573. port = OUTPUT_PORT;
  574. } else if (type == OUTPUT_META_PLANE) {
  575. port = OUTPUT_META_PORT;
  576. } else {
  577. i_vpr_e(inst, "%s: port not found for v4l2 type %d\n",
  578. func, type);
  579. port = -EINVAL;
  580. }
  581. return port;
  582. }
  583. struct msm_vidc_buffers *msm_vidc_get_buffers(
  584. struct msm_vidc_inst *inst, enum msm_vidc_buffer_type buffer_type,
  585. const char *func)
  586. {
  587. switch (buffer_type) {
  588. case MSM_VIDC_BUF_INPUT:
  589. return &inst->buffers.input;
  590. case MSM_VIDC_BUF_INPUT_META:
  591. return &inst->buffers.input_meta;
  592. case MSM_VIDC_BUF_OUTPUT:
  593. return &inst->buffers.output;
  594. case MSM_VIDC_BUF_OUTPUT_META:
  595. return &inst->buffers.output_meta;
  596. case MSM_VIDC_BUF_READ_ONLY:
  597. return &inst->buffers.read_only;
  598. case MSM_VIDC_BUF_BIN:
  599. return &inst->buffers.bin;
  600. case MSM_VIDC_BUF_ARP:
  601. return &inst->buffers.arp;
  602. case MSM_VIDC_BUF_COMV:
  603. return &inst->buffers.comv;
  604. case MSM_VIDC_BUF_NON_COMV:
  605. return &inst->buffers.non_comv;
  606. case MSM_VIDC_BUF_LINE:
  607. return &inst->buffers.line;
  608. case MSM_VIDC_BUF_DPB:
  609. return &inst->buffers.dpb;
  610. case MSM_VIDC_BUF_PERSIST:
  611. return &inst->buffers.persist;
  612. case MSM_VIDC_BUF_VPSS:
  613. return &inst->buffers.vpss;
  614. case MSM_VIDC_BUF_PARTIAL_DATA:
  615. return &inst->buffers.partial_data;
  616. case MSM_VIDC_BUF_INTERFACE_QUEUE:
  617. return NULL;
  618. default:
  619. i_vpr_e(inst, "%s: invalid driver buffer type %d\n",
  620. func, buffer_type);
  621. return NULL;
  622. }
  623. }
  624. struct msm_vidc_mem_list *msm_vidc_get_mem_info(
  625. struct msm_vidc_inst *inst, enum msm_vidc_buffer_type buffer_type,
  626. const char *func)
  627. {
  628. switch (buffer_type) {
  629. case MSM_VIDC_BUF_BIN:
  630. return &inst->mem_info.bin;
  631. case MSM_VIDC_BUF_ARP:
  632. return &inst->mem_info.arp;
  633. case MSM_VIDC_BUF_COMV:
  634. return &inst->mem_info.comv;
  635. case MSM_VIDC_BUF_NON_COMV:
  636. return &inst->mem_info.non_comv;
  637. case MSM_VIDC_BUF_LINE:
  638. return &inst->mem_info.line;
  639. case MSM_VIDC_BUF_DPB:
  640. return &inst->mem_info.dpb;
  641. case MSM_VIDC_BUF_PERSIST:
  642. return &inst->mem_info.persist;
  643. case MSM_VIDC_BUF_VPSS:
  644. return &inst->mem_info.vpss;
  645. case MSM_VIDC_BUF_PARTIAL_DATA:
  646. return &inst->mem_info.partial_data;
  647. default:
  648. i_vpr_e(inst, "%s: invalid driver buffer type %d\n",
  649. func, buffer_type);
  650. return NULL;
  651. }
  652. }
  653. bool res_is_greater_than(u32 width, u32 height,
  654. u32 ref_width, u32 ref_height)
  655. {
  656. u32 num_mbs = NUM_MBS_PER_FRAME(height, width);
  657. u32 max_side = max(ref_width, ref_height);
  658. if (num_mbs > NUM_MBS_PER_FRAME(ref_height, ref_width) ||
  659. width > max_side ||
  660. height > max_side)
  661. return true;
  662. else
  663. return false;
  664. }
  665. bool res_is_greater_than_or_equal_to(u32 width, u32 height,
  666. u32 ref_width, u32 ref_height)
  667. {
  668. u32 num_mbs = NUM_MBS_PER_FRAME(height, width);
  669. u32 max_side = max(ref_width, ref_height);
  670. if (num_mbs >= NUM_MBS_PER_FRAME(ref_height, ref_width) ||
  671. width >= max_side ||
  672. height >= max_side)
  673. return true;
  674. else
  675. return false;
  676. }
  677. bool res_is_less_than(u32 width, u32 height,
  678. u32 ref_width, u32 ref_height)
  679. {
  680. u32 num_mbs = NUM_MBS_PER_FRAME(height, width);
  681. u32 max_side = max(ref_width, ref_height);
  682. if (num_mbs < NUM_MBS_PER_FRAME(ref_height, ref_width) &&
  683. width < max_side &&
  684. height < max_side)
  685. return true;
  686. else
  687. return false;
  688. }
  689. bool res_is_less_than_or_equal_to(u32 width, u32 height,
  690. u32 ref_width, u32 ref_height)
  691. {
  692. u32 num_mbs = NUM_MBS_PER_FRAME(height, width);
  693. u32 max_side = max(ref_width, ref_height);
  694. if (num_mbs <= NUM_MBS_PER_FRAME(ref_height, ref_width) &&
  695. width <= max_side &&
  696. height <= max_side)
  697. return true;
  698. else
  699. return false;
  700. }
  701. int signal_session_msg_receipt(struct msm_vidc_inst *inst,
  702. enum signal_session_response cmd)
  703. {
  704. if (cmd < MAX_SIGNAL)
  705. complete(&inst->completions[cmd]);
  706. return 0;
  707. }
  708. bool msm_vidc_allow_metadata_delivery(struct msm_vidc_inst *inst, u32 cap_id,
  709. u32 port)
  710. {
  711. return true;
  712. }
  713. bool msm_vidc_allow_metadata_subscription(struct msm_vidc_inst *inst, u32 cap_id,
  714. u32 port)
  715. {
  716. bool is_allowed = true;
  717. if (port == INPUT_PORT) {
  718. switch (cap_id) {
  719. case META_BUF_TAG:
  720. case META_BITSTREAM_RESOLUTION:
  721. case META_CROP_OFFSETS:
  722. case META_SEI_MASTERING_DISP:
  723. case META_SEI_CLL:
  724. case META_HDR10PLUS:
  725. if (!is_meta_rx_inp_enabled(inst, META_OUTBUF_FENCE)) {
  726. i_vpr_h(inst,
  727. "%s: cap: %24s not allowed as output buffer fence is disabled\n",
  728. __func__, cap_name(cap_id));
  729. is_allowed = false;
  730. }
  731. break;
  732. case META_DPB_TAG_LIST:
  733. if (!is_ubwc_colorformat(inst->capabilities[PIX_FMTS].value)) {
  734. i_vpr_h(inst,
  735. "%s: cap: %24s not allowed for split mode\n",
  736. __func__, cap_name(cap_id));
  737. is_allowed = false;
  738. }
  739. break;
  740. default:
  741. is_allowed = true;
  742. break;
  743. }
  744. } else if (port == OUTPUT_PORT) {
  745. is_allowed = true;
  746. } else {
  747. i_vpr_e(inst, "%s: invalid port %d\n", __func__, port);
  748. is_allowed = false;
  749. }
  750. return is_allowed;
  751. }
  752. bool msm_vidc_allow_property(struct msm_vidc_inst *inst, u32 hfi_id)
  753. {
  754. bool is_allowed = true;
  755. switch (hfi_id) {
  756. case HFI_PROP_AV1_TILE_ROWS_COLUMNS:
  757. case HFI_PROP_AV1_UNIFORM_TILE_SPACING:
  758. if (inst->codec == MSM_VIDC_AV1)
  759. is_allowed = true;
  760. else
  761. is_allowed = false;
  762. break;
  763. case HFI_PROP_FENCE:
  764. if (!is_meta_rx_inp_enabled(inst, META_OUTBUF_FENCE)) {
  765. i_vpr_h(inst,
  766. "%s: cap: %24s not enabled, hence not allowed to subscribe\n",
  767. __func__, cap_name(META_OUTBUF_FENCE));
  768. is_allowed = false;
  769. }
  770. break;
  771. default:
  772. is_allowed = true;
  773. break;
  774. }
  775. return is_allowed;
  776. }
  777. enum msm_vidc_allow msm_vidc_allow_input_psc(struct msm_vidc_inst *inst)
  778. {
  779. enum msm_vidc_allow allow = MSM_VIDC_ALLOW;
  780. /*
  781. * if drc sequence is not completed by client, fw is not
  782. * expected to raise another ipsc
  783. */
  784. if (is_sub_state(inst, MSM_VIDC_DRC)) {
  785. i_vpr_e(inst, "%s: not allowed in sub state %s\n",
  786. __func__, inst->sub_state_name);
  787. return MSM_VIDC_DISALLOW;
  788. }
  789. return allow;
  790. }
  791. bool msm_vidc_allow_drain_last_flag(struct msm_vidc_inst *inst)
  792. {
  793. /*
  794. * drain last flag is expected only when DRAIN, INPUT_PAUSE
  795. * is set and DRAIN_LAST_BUFFER is not set
  796. */
  797. if (is_sub_state(inst, MSM_VIDC_DRAIN) &&
  798. is_sub_state(inst, MSM_VIDC_INPUT_PAUSE) &&
  799. !is_sub_state(inst, MSM_VIDC_DRAIN_LAST_BUFFER))
  800. return true;
  801. i_vpr_e(inst, "%s: not allowed in sub state %s\n",
  802. __func__, inst->sub_state_name);
  803. return false;
  804. }
  805. bool msm_vidc_allow_psc_last_flag(struct msm_vidc_inst *inst)
  806. {
  807. /*
  808. * drc last flag is expected only when DRC, INPUT_PAUSE
  809. * is set and DRC_LAST_BUFFER is not set
  810. */
  811. if (is_sub_state(inst, MSM_VIDC_DRC) &&
  812. is_sub_state(inst, MSM_VIDC_INPUT_PAUSE) &&
  813. !is_sub_state(inst, MSM_VIDC_DRC_LAST_BUFFER))
  814. return true;
  815. i_vpr_e(inst, "%s: not allowed in sub state %s\n",
  816. __func__, inst->sub_state_name);
  817. return false;
  818. }
  819. enum msm_vidc_allow msm_vidc_allow_pm_suspend(struct msm_vidc_core *core)
  820. {
  821. if (!core) {
  822. d_vpr_e("%s: invalid param\n", __func__);
  823. return MSM_VIDC_DISALLOW;
  824. }
  825. /* core must be in valid state to do pm_suspend */
  826. if (!core_in_valid_state(core)) {
  827. d_vpr_e("%s: invalid core state %s\n",
  828. __func__, core_state_name(core->state));
  829. return MSM_VIDC_DISALLOW;
  830. }
  831. /* check if power is enabled */
  832. if (!is_core_sub_state(core, CORE_SUBSTATE_POWER_ENABLE)) {
  833. d_vpr_h("%s: Power already disabled\n", __func__);
  834. return MSM_VIDC_IGNORE;
  835. }
  836. return MSM_VIDC_ALLOW;
  837. }
  838. bool is_hevc_10bit_decode_session(struct msm_vidc_inst *inst)
  839. {
  840. bool is10bit = false;
  841. enum msm_vidc_colorformat_type colorformat;
  842. /* in case of decoder session return false */
  843. if (!is_decode_session(inst))
  844. return false;
  845. colorformat = v4l2_colorformat_to_driver(inst,
  846. inst->fmts[OUTPUT_PORT].fmt.pix_mp.pixelformat, __func__);
  847. if (colorformat == MSM_VIDC_FMT_TP10C || colorformat == MSM_VIDC_FMT_P010)
  848. is10bit = true;
  849. return inst->domain == MSM_VIDC_DECODER &&
  850. inst->codec == MSM_VIDC_HEVC &&
  851. is10bit;
  852. }
  853. int msm_vidc_state_change_streamon(struct msm_vidc_inst *inst,
  854. enum msm_vidc_port_type port)
  855. {
  856. int rc = 0;
  857. enum msm_vidc_state new_state = MSM_VIDC_ERROR;
  858. if (port == INPUT_META_PORT || port == OUTPUT_META_PORT)
  859. return 0;
  860. if (port == INPUT_PORT) {
  861. if (is_state(inst, MSM_VIDC_OPEN))
  862. new_state = MSM_VIDC_INPUT_STREAMING;
  863. else if (is_state(inst, MSM_VIDC_OUTPUT_STREAMING))
  864. new_state = MSM_VIDC_STREAMING;
  865. } else if (port == OUTPUT_PORT) {
  866. if (is_state(inst, MSM_VIDC_OPEN))
  867. new_state = MSM_VIDC_OUTPUT_STREAMING;
  868. else if (is_state(inst, MSM_VIDC_INPUT_STREAMING))
  869. new_state = MSM_VIDC_STREAMING;
  870. }
  871. rc = msm_vidc_change_state(inst, new_state, __func__);
  872. if (rc)
  873. return rc;
  874. return rc;
  875. }
  876. int msm_vidc_state_change_streamoff(struct msm_vidc_inst *inst,
  877. enum msm_vidc_port_type port)
  878. {
  879. int rc = 0;
  880. enum msm_vidc_state new_state = MSM_VIDC_ERROR;
  881. if (port == INPUT_META_PORT || port == OUTPUT_META_PORT)
  882. return 0;
  883. if (port == INPUT_PORT) {
  884. if (is_state(inst, MSM_VIDC_INPUT_STREAMING)) {
  885. new_state = MSM_VIDC_OPEN;
  886. } else if (is_state(inst, MSM_VIDC_STREAMING)) {
  887. new_state = MSM_VIDC_OUTPUT_STREAMING;
  888. }
  889. } else if (port == OUTPUT_PORT) {
  890. if (is_state(inst, MSM_VIDC_OUTPUT_STREAMING)) {
  891. new_state = MSM_VIDC_OPEN;
  892. } else if (is_state(inst, MSM_VIDC_STREAMING)) {
  893. new_state = MSM_VIDC_INPUT_STREAMING;
  894. }
  895. }
  896. rc = msm_vidc_change_state(inst, new_state, __func__);
  897. if (rc)
  898. goto exit;
  899. exit:
  900. return rc;
  901. }
  902. int msm_vidc_process_drain(struct msm_vidc_inst *inst)
  903. {
  904. int rc = 0;
  905. rc = venus_hfi_session_drain(inst, INPUT_PORT);
  906. if (rc)
  907. return rc;
  908. rc = msm_vidc_change_sub_state(inst, 0, MSM_VIDC_DRAIN, __func__);
  909. if (rc)
  910. return rc;
  911. msm_vidc_scale_power(inst, true);
  912. return rc;
  913. }
  914. int msm_vidc_process_resume(struct msm_vidc_inst *inst)
  915. {
  916. int rc = 0;
  917. enum msm_vidc_sub_state clear_sub_state = MSM_VIDC_SUB_STATE_NONE;
  918. bool drain_pending = false;
  919. msm_vidc_scale_power(inst, true);
  920. /* first check DRC pending else check drain pending */
  921. if (is_sub_state(inst, MSM_VIDC_DRC) &&
  922. is_sub_state(inst, MSM_VIDC_DRC_LAST_BUFFER)) {
  923. clear_sub_state = MSM_VIDC_DRC | MSM_VIDC_DRC_LAST_BUFFER;
  924. /*
  925. * if drain sequence is not completed then do not resume here.
  926. * client will eventually complete drain sequence in which ports
  927. * will be resumed.
  928. */
  929. drain_pending = is_sub_state(inst, MSM_VIDC_DRAIN) &&
  930. is_sub_state(inst, MSM_VIDC_DRAIN_LAST_BUFFER);
  931. if (!drain_pending) {
  932. if (is_sub_state(inst, MSM_VIDC_INPUT_PAUSE)) {
  933. rc = venus_hfi_session_resume(inst, INPUT_PORT,
  934. HFI_CMD_SETTINGS_CHANGE);
  935. if (rc)
  936. return rc;
  937. clear_sub_state |= MSM_VIDC_INPUT_PAUSE;
  938. }
  939. if (is_sub_state(inst, MSM_VIDC_OUTPUT_PAUSE)) {
  940. rc = venus_hfi_session_resume(inst, OUTPUT_PORT,
  941. HFI_CMD_SETTINGS_CHANGE);
  942. if (rc)
  943. return rc;
  944. clear_sub_state |= MSM_VIDC_OUTPUT_PAUSE;
  945. }
  946. }
  947. } else if (is_sub_state(inst, MSM_VIDC_DRAIN) &&
  948. is_sub_state(inst, MSM_VIDC_DRAIN_LAST_BUFFER)) {
  949. clear_sub_state = MSM_VIDC_DRAIN | MSM_VIDC_DRAIN_LAST_BUFFER;
  950. if (is_sub_state(inst, MSM_VIDC_INPUT_PAUSE)) {
  951. rc = venus_hfi_session_resume(inst, INPUT_PORT, HFI_CMD_DRAIN);
  952. if (rc)
  953. return rc;
  954. clear_sub_state |= MSM_VIDC_INPUT_PAUSE;
  955. }
  956. if (is_sub_state(inst, MSM_VIDC_OUTPUT_PAUSE)) {
  957. rc = venus_hfi_session_resume(inst, OUTPUT_PORT, HFI_CMD_DRAIN);
  958. if (rc)
  959. return rc;
  960. clear_sub_state |= MSM_VIDC_OUTPUT_PAUSE;
  961. }
  962. }
  963. rc = msm_vidc_change_sub_state(inst, clear_sub_state, 0, __func__);
  964. if (rc)
  965. return rc;
  966. return rc;
  967. }
  968. int msm_vidc_process_streamon_input(struct msm_vidc_inst *inst)
  969. {
  970. int rc = 0;
  971. enum msm_vidc_sub_state clear_sub_state = MSM_VIDC_SUB_STATE_NONE;
  972. enum msm_vidc_sub_state set_sub_state = MSM_VIDC_SUB_STATE_NONE;
  973. msm_vidc_scale_power(inst, true);
  974. rc = venus_hfi_start(inst, INPUT_PORT);
  975. if (rc)
  976. return rc;
  977. /* clear input pause substate immediately */
  978. if (is_sub_state(inst, MSM_VIDC_INPUT_PAUSE)) {
  979. rc = msm_vidc_change_sub_state(inst, MSM_VIDC_INPUT_PAUSE, 0, __func__);
  980. if (rc)
  981. return rc;
  982. }
  983. /*
  984. * if DRC sequence is not completed by the client then PAUSE
  985. * firmware input port to avoid firmware raising IPSC again.
  986. * When client completes DRC or DRAIN sequences, firmware
  987. * input port will be resumed.
  988. */
  989. if (is_sub_state(inst, MSM_VIDC_DRC) ||
  990. is_sub_state(inst, MSM_VIDC_DRAIN)) {
  991. if (!is_sub_state(inst, MSM_VIDC_INPUT_PAUSE)) {
  992. rc = venus_hfi_session_pause(inst, INPUT_PORT);
  993. if (rc)
  994. return rc;
  995. set_sub_state = MSM_VIDC_INPUT_PAUSE;
  996. }
  997. }
  998. rc = msm_vidc_state_change_streamon(inst, INPUT_PORT);
  999. if (rc)
  1000. return rc;
  1001. rc = msm_vidc_change_sub_state(inst, clear_sub_state, set_sub_state, __func__);
  1002. if (rc)
  1003. return rc;
  1004. return rc;
  1005. }
  1006. int msm_vidc_process_streamon_output(struct msm_vidc_inst *inst)
  1007. {
  1008. int rc = 0;
  1009. enum msm_vidc_sub_state clear_sub_state = MSM_VIDC_SUB_STATE_NONE;
  1010. enum msm_vidc_sub_state set_sub_state = MSM_VIDC_SUB_STATE_NONE;
  1011. bool drain_pending = false;
  1012. msm_vidc_scale_power(inst, true);
  1013. /*
  1014. * client completed drc sequence, reset DRC and
  1015. * MSM_VIDC_DRC_LAST_BUFFER substates
  1016. */
  1017. if (is_sub_state(inst, MSM_VIDC_DRC) &&
  1018. is_sub_state(inst, MSM_VIDC_DRC_LAST_BUFFER)) {
  1019. clear_sub_state = MSM_VIDC_DRC | MSM_VIDC_DRC_LAST_BUFFER;
  1020. }
  1021. /*
  1022. * Client is completing port reconfiguration, hence reallocate
  1023. * input internal buffers before input port is resumed.
  1024. * Drc sub-state cannot be checked because DRC sub-state will
  1025. * not be set during initial port reconfiguration.
  1026. */
  1027. if (is_decode_session(inst) &&
  1028. is_sub_state(inst, MSM_VIDC_INPUT_PAUSE)) {
  1029. rc = msm_vidc_alloc_and_queue_input_internal_buffers(inst);
  1030. if (rc)
  1031. return rc;
  1032. rc = msm_vidc_set_stage(inst, STAGE);
  1033. if (rc)
  1034. return rc;
  1035. rc = msm_vidc_set_pipe(inst, PIPE);
  1036. if (rc)
  1037. return rc;
  1038. /*
  1039. * Input port subscription for metadata may be changed.
  1040. * For eg: due to IPSC, driver may have disabled tx
  1041. * type output fence, hence fence related metadatas
  1042. * to recieve on input port will be disabled by HAL.
  1043. * Hence, update metadata subscription properties
  1044. * on INPUT port before sending RESUME command to FW.
  1045. */
  1046. i_vpr_l(inst, "%s: reset input port subscribe metadata\n",
  1047. __func__);
  1048. rc = msm_vdec_subscribe_metadata(inst, INPUT_PORT);
  1049. if (rc)
  1050. return rc;
  1051. }
  1052. /*
  1053. * fw input port is paused due to ipsc. now that client
  1054. * completed drc sequence, resume fw input port provided
  1055. * drain is not pending and input port is streaming.
  1056. */
  1057. drain_pending = is_sub_state(inst, MSM_VIDC_DRAIN) &&
  1058. is_sub_state(inst, MSM_VIDC_DRAIN_LAST_BUFFER);
  1059. if (!drain_pending && is_state(inst, MSM_VIDC_INPUT_STREAMING)) {
  1060. if (is_sub_state(inst, MSM_VIDC_INPUT_PAUSE)) {
  1061. i_vpr_h(inst, "%s: resume input port\n", __func__);
  1062. rc = venus_hfi_session_resume(inst, INPUT_PORT,
  1063. HFI_CMD_SETTINGS_CHANGE);
  1064. if (rc)
  1065. return rc;
  1066. clear_sub_state |= MSM_VIDC_INPUT_PAUSE;
  1067. }
  1068. }
  1069. rc = venus_hfi_start(inst, OUTPUT_PORT);
  1070. if (rc)
  1071. return rc;
  1072. /* clear output pause substate immediately */
  1073. if (is_sub_state(inst, MSM_VIDC_OUTPUT_PAUSE)) {
  1074. rc = msm_vidc_change_sub_state(inst, MSM_VIDC_OUTPUT_PAUSE, 0, __func__);
  1075. if (rc)
  1076. return rc;
  1077. }
  1078. rc = msm_vidc_state_change_streamon(inst, OUTPUT_PORT);
  1079. if (rc)
  1080. return rc;
  1081. rc = msm_vidc_change_sub_state(inst, clear_sub_state, set_sub_state, __func__);
  1082. if (rc)
  1083. return rc;
  1084. return rc;
  1085. }
  1086. int msm_vidc_process_stop_done(struct msm_vidc_inst *inst,
  1087. enum signal_session_response signal_type)
  1088. {
  1089. int rc = 0;
  1090. enum msm_vidc_sub_state set_sub_state = MSM_VIDC_SUB_STATE_NONE;
  1091. if (signal_type == SIGNAL_CMD_STOP_INPUT) {
  1092. set_sub_state = MSM_VIDC_INPUT_PAUSE;
  1093. /*
  1094. * FW is expected to return DRC LAST flag before input
  1095. * stop done if DRC sequence is pending
  1096. */
  1097. if (is_sub_state(inst, MSM_VIDC_DRC) &&
  1098. !is_sub_state(inst, MSM_VIDC_DRC_LAST_BUFFER)) {
  1099. i_vpr_e(inst, "%s: drc last flag pkt not received\n", __func__);
  1100. msm_vidc_change_state(inst, MSM_VIDC_ERROR, __func__);
  1101. }
  1102. /*
  1103. * for a decode session, FW is expected to return
  1104. * DRAIN LAST flag before input stop done if
  1105. * DRAIN sequence is pending
  1106. */
  1107. if (is_decode_session(inst) &&
  1108. is_sub_state(inst, MSM_VIDC_DRAIN) &&
  1109. !is_sub_state(inst, MSM_VIDC_DRAIN_LAST_BUFFER)) {
  1110. i_vpr_e(inst, "%s: drain last flag pkt not received\n", __func__);
  1111. msm_vidc_change_state(inst, MSM_VIDC_ERROR, __func__);
  1112. }
  1113. } else if (signal_type == SIGNAL_CMD_STOP_OUTPUT) {
  1114. set_sub_state = MSM_VIDC_OUTPUT_PAUSE;
  1115. }
  1116. rc = msm_vidc_change_sub_state(inst, 0, set_sub_state, __func__);
  1117. if (rc)
  1118. return rc;
  1119. signal_session_msg_receipt(inst, signal_type);
  1120. return rc;
  1121. }
  1122. int msm_vidc_process_drain_done(struct msm_vidc_inst *inst)
  1123. {
  1124. int rc = 0;
  1125. if (is_sub_state(inst, MSM_VIDC_DRAIN)) {
  1126. rc = msm_vidc_change_sub_state(inst, 0, MSM_VIDC_INPUT_PAUSE, __func__);
  1127. if (rc)
  1128. return rc;
  1129. } else {
  1130. i_vpr_e(inst, "%s: unexpected drain done\n", __func__);
  1131. }
  1132. return rc;
  1133. }
  1134. int msm_vidc_process_drain_last_flag(struct msm_vidc_inst *inst)
  1135. {
  1136. int rc = 0;
  1137. struct v4l2_event event = {0};
  1138. rc = msm_vidc_state_change_drain_last_flag(inst);
  1139. if (rc)
  1140. return rc;
  1141. if (!inst->capabilities[LAST_FLAG_EVENT_ENABLE].value) {
  1142. i_vpr_h(inst, "%s: last flag event not enabled\n", __func__);
  1143. return 0;
  1144. }
  1145. event.type = V4L2_EVENT_EOS;
  1146. v4l2_event_queue_fh(&inst->fh, &event);
  1147. return rc;
  1148. }
  1149. int msm_vidc_process_psc_last_flag(struct msm_vidc_inst *inst)
  1150. {
  1151. int rc = 0;
  1152. struct v4l2_event event = {0};
  1153. rc = msm_vidc_state_change_psc_last_flag(inst);
  1154. if (rc)
  1155. return rc;
  1156. if (!inst->capabilities[LAST_FLAG_EVENT_ENABLE].value) {
  1157. i_vpr_h(inst, "%s: last flag event not enabled\n", __func__);
  1158. return 0;
  1159. }
  1160. event.type = V4L2_EVENT_EOS;
  1161. v4l2_event_queue_fh(&inst->fh, &event);
  1162. return rc;
  1163. }
  1164. int msm_vidc_state_change_input_psc(struct msm_vidc_inst *inst)
  1165. {
  1166. int rc = 0;
  1167. enum msm_vidc_sub_state set_sub_state = MSM_VIDC_SUB_STATE_NONE;
  1168. /*
  1169. * if output port is not streaming, then do not set DRC substate
  1170. * because DRC_LAST_FLAG is not going to be received. Update
  1171. * INPUT_PAUSE substate only
  1172. */
  1173. if (is_state(inst, MSM_VIDC_INPUT_STREAMING) ||
  1174. is_state(inst, MSM_VIDC_OPEN))
  1175. set_sub_state = MSM_VIDC_INPUT_PAUSE;
  1176. else
  1177. set_sub_state = MSM_VIDC_DRC | MSM_VIDC_INPUT_PAUSE;
  1178. rc = msm_vidc_change_sub_state(inst, 0, set_sub_state, __func__);
  1179. if (rc)
  1180. return rc;
  1181. return rc;
  1182. }
  1183. int msm_vidc_state_change_drain_last_flag(struct msm_vidc_inst *inst)
  1184. {
  1185. int rc = 0;
  1186. enum msm_vidc_sub_state set_sub_state = MSM_VIDC_SUB_STATE_NONE;
  1187. set_sub_state = MSM_VIDC_DRAIN_LAST_BUFFER | MSM_VIDC_OUTPUT_PAUSE;
  1188. rc = msm_vidc_change_sub_state(inst, 0, set_sub_state, __func__);
  1189. if (rc)
  1190. return rc;
  1191. return rc;
  1192. }
  1193. int msm_vidc_state_change_psc_last_flag(struct msm_vidc_inst *inst)
  1194. {
  1195. int rc = 0;
  1196. enum msm_vidc_sub_state set_sub_state = MSM_VIDC_SUB_STATE_NONE;
  1197. set_sub_state = MSM_VIDC_DRC_LAST_BUFFER | MSM_VIDC_OUTPUT_PAUSE;
  1198. rc = msm_vidc_change_sub_state(inst, 0, set_sub_state, __func__);
  1199. if (rc)
  1200. return rc;
  1201. return rc;
  1202. }
  1203. int msm_vidc_get_fence_fd(struct msm_vidc_inst *inst, int *fence_fd)
  1204. {
  1205. int rc = 0;
  1206. struct msm_vidc_fence *fence, *dummy_fence;
  1207. struct msm_vidc_core *core;
  1208. bool found = false;
  1209. *fence_fd = INVALID_FD;
  1210. core = inst->core;
  1211. list_for_each_entry_safe(fence, dummy_fence, &inst->fence_list, list) {
  1212. if (fence->fence_id ==
  1213. (u64)inst->capabilities[FENCE_ID].value) {
  1214. found = true;
  1215. break;
  1216. }
  1217. }
  1218. if (!found) {
  1219. i_vpr_h(inst, "%s: could not find matching fence for fence id: %d\n",
  1220. __func__, inst->capabilities[FENCE_ID].value);
  1221. goto exit;
  1222. }
  1223. if (fence->fd == INVALID_FD) {
  1224. rc = call_fence_op(core, fence_create_fd, inst, fence);
  1225. if (rc)
  1226. goto exit;
  1227. }
  1228. *fence_fd = fence->fd;
  1229. exit:
  1230. return rc;
  1231. }
  1232. int msm_vidc_get_control(struct msm_vidc_inst *inst, struct v4l2_ctrl *ctrl)
  1233. {
  1234. int rc = 0;
  1235. enum msm_vidc_inst_capability_type cap_id;
  1236. cap_id = msm_vidc_get_cap_id(inst, ctrl->id);
  1237. if (!is_valid_cap_id(cap_id)) {
  1238. i_vpr_e(inst, "%s: could not find cap_id for ctrl %s\n",
  1239. __func__, ctrl->name);
  1240. return -EINVAL;
  1241. }
  1242. switch (cap_id) {
  1243. case MIN_BUFFERS_OUTPUT:
  1244. ctrl->val = inst->buffers.output.min_count +
  1245. inst->buffers.output.extra_count;
  1246. i_vpr_h(inst, "g_min: output buffers %d\n", ctrl->val);
  1247. break;
  1248. case MIN_BUFFERS_INPUT:
  1249. ctrl->val = inst->buffers.input.min_count +
  1250. inst->buffers.input.extra_count;
  1251. i_vpr_h(inst, "g_min: input buffers %d\n", ctrl->val);
  1252. break;
  1253. case FILM_GRAIN:
  1254. ctrl->val = inst->capabilities[FILM_GRAIN].value;
  1255. i_vpr_h(inst, "%s: film grain present: %d\n",
  1256. __func__, ctrl->val);
  1257. break;
  1258. case FENCE_FD:
  1259. rc = msm_vidc_get_fence_fd(inst, &ctrl->val);
  1260. if (!rc)
  1261. i_vpr_l(inst, "%s: fence fd: %d\n",
  1262. __func__, ctrl->val);
  1263. break;
  1264. case MAX_NUM_REORDER_FRAMES:
  1265. ctrl->val = inst->capabilities[MAX_NUM_REORDER_FRAMES].value;
  1266. i_vpr_h(inst, "%s: max num reorder frames: %d\n",
  1267. __func__, ctrl->val);
  1268. break;
  1269. case CODED_FRAMES:
  1270. ctrl->val = inst->capabilities[CODED_FRAMES].value;
  1271. i_vpr_h(inst, "%s: coded frames: %d\n", __func__, ctrl->val);
  1272. break;
  1273. default:
  1274. i_vpr_e(inst, "invalid ctrl %s id %d\n",
  1275. ctrl->name, ctrl->id);
  1276. return -EINVAL;
  1277. }
  1278. return rc;
  1279. }
  1280. int msm_vidc_get_mbs_per_frame(struct msm_vidc_inst *inst)
  1281. {
  1282. int height = 0, width = 0;
  1283. struct v4l2_format *inp_f;
  1284. if (is_decode_session(inst)) {
  1285. inp_f = &inst->fmts[INPUT_PORT];
  1286. width = max(inp_f->fmt.pix_mp.width, inst->crop.width);
  1287. height = max(inp_f->fmt.pix_mp.height, inst->crop.height);
  1288. } else if (is_encode_session(inst)) {
  1289. width = inst->crop.width;
  1290. height = inst->crop.height;
  1291. }
  1292. return NUM_MBS_PER_FRAME(height, width);
  1293. }
  1294. int msm_vidc_get_fps(struct msm_vidc_inst *inst)
  1295. {
  1296. int fps;
  1297. u32 frame_rate, operating_rate;
  1298. frame_rate = msm_vidc_get_frame_rate(inst);
  1299. operating_rate = msm_vidc_get_operating_rate(inst);
  1300. if (operating_rate > frame_rate)
  1301. fps = operating_rate ? operating_rate : 1;
  1302. else
  1303. fps = frame_rate;
  1304. return fps;
  1305. }
  1306. int msm_vidc_num_buffers(struct msm_vidc_inst *inst,
  1307. enum msm_vidc_buffer_type type, enum msm_vidc_buffer_attributes attr)
  1308. {
  1309. int count = 0;
  1310. struct msm_vidc_buffer *vbuf;
  1311. struct msm_vidc_buffers *buffers;
  1312. if (type == MSM_VIDC_BUF_OUTPUT) {
  1313. buffers = &inst->buffers.output;
  1314. } else if (type == MSM_VIDC_BUF_INPUT) {
  1315. buffers = &inst->buffers.input;
  1316. } else {
  1317. i_vpr_e(inst, "%s: invalid buffer type %#x\n",
  1318. __func__, type);
  1319. return count;
  1320. }
  1321. list_for_each_entry(vbuf, &buffers->list, list) {
  1322. if (vbuf->type != type)
  1323. continue;
  1324. if (!(vbuf->attr & attr))
  1325. continue;
  1326. count++;
  1327. }
  1328. return count;
  1329. }
  1330. int vb2_buffer_to_driver(struct vb2_buffer *vb2,
  1331. struct msm_vidc_buffer *buf)
  1332. {
  1333. int rc = 0;
  1334. struct vb2_v4l2_buffer *vbuf;
  1335. if (!vb2 || !buf) {
  1336. d_vpr_e("%s: invalid params\n", __func__);
  1337. return -EINVAL;
  1338. }
  1339. vbuf = to_vb2_v4l2_buffer(vb2);
  1340. buf->fd = vb2->planes[0].m.fd;
  1341. buf->data_offset = vb2->planes[0].data_offset;
  1342. buf->data_size = vb2->planes[0].bytesused - vb2->planes[0].data_offset;
  1343. buf->buffer_size = vb2->planes[0].length;
  1344. buf->timestamp = vb2->timestamp;
  1345. buf->flags = vbuf->flags;
  1346. buf->attr = 0;
  1347. buf->fence_id = 0;
  1348. return rc;
  1349. }
  1350. int msm_vidc_process_readonly_buffers(struct msm_vidc_inst *inst,
  1351. struct msm_vidc_buffer *buf)
  1352. {
  1353. int rc = 0;
  1354. struct msm_vidc_buffer *ro_buf, *dummy;
  1355. struct msm_vidc_core *core;
  1356. core = inst->core;
  1357. if (!is_decode_session(inst) || !is_output_buffer(buf->type))
  1358. return 0;
  1359. /*
  1360. * check if read_only buffer is present in read_only list
  1361. * if present: add ro flag to buf provided buffer is not
  1362. * pending release
  1363. */
  1364. list_for_each_entry_safe(ro_buf, dummy, &inst->buffers.read_only.list, list) {
  1365. if (ro_buf->device_addr != buf->device_addr)
  1366. continue;
  1367. if (ro_buf->attr & MSM_VIDC_ATTR_READ_ONLY &&
  1368. !(ro_buf->attr & MSM_VIDC_ATTR_PENDING_RELEASE)) {
  1369. /* add READ_ONLY to the buffer going to the firmware */
  1370. buf->attr |= MSM_VIDC_ATTR_READ_ONLY;
  1371. /*
  1372. * remove READ_ONLY on the read_only list buffer so that
  1373. * it will get removed from the read_only list below
  1374. */
  1375. ro_buf->attr &= ~MSM_VIDC_ATTR_READ_ONLY;
  1376. break;
  1377. }
  1378. }
  1379. /* remove ro buffers if not required anymore */
  1380. list_for_each_entry_safe(ro_buf, dummy, &inst->buffers.read_only.list, list) {
  1381. /* if read only buffer do not remove */
  1382. if (ro_buf->attr & MSM_VIDC_ATTR_READ_ONLY)
  1383. continue;
  1384. print_vidc_buffer(VIDC_LOW, "low ", "ro buf removed", inst, ro_buf);
  1385. /* unmap the buffer if driver holds mapping */
  1386. if (ro_buf->sg_table && ro_buf->attach) {
  1387. call_mem_op(core, dma_buf_unmap_attachment, core,
  1388. ro_buf->attach, ro_buf->sg_table);
  1389. call_mem_op(core, dma_buf_detach, core,
  1390. ro_buf->dmabuf, ro_buf->attach);
  1391. ro_buf->sg_table = NULL;
  1392. ro_buf->attach = NULL;
  1393. }
  1394. if (ro_buf->dbuf_get) {
  1395. call_mem_op(core, dma_buf_put, inst, ro_buf->dmabuf);
  1396. ro_buf->dmabuf = NULL;
  1397. ro_buf->dbuf_get = 0;
  1398. }
  1399. list_del_init(&ro_buf->list);
  1400. msm_vidc_pool_free(inst, ro_buf);
  1401. }
  1402. return rc;
  1403. }
  1404. int msm_vidc_set_auto_framerate(struct msm_vidc_inst *inst, u64 timestamp)
  1405. {
  1406. struct msm_vidc_core *core;
  1407. struct msm_vidc_timestamp *ts;
  1408. struct msm_vidc_timestamp *prev = NULL;
  1409. u32 counter = 0, prev_fr = 0, curr_fr = 0;
  1410. u64 time_us = 0;
  1411. int rc = 0;
  1412. core = inst->core;
  1413. if (!core->capabilities[ENC_AUTO_FRAMERATE].value ||
  1414. is_image_session(inst) || msm_vidc_is_super_buffer(inst) ||
  1415. !inst->capabilities[TIME_DELTA_BASED_RC].value)
  1416. goto exit;
  1417. rc = msm_vidc_update_timestamp_rate(inst, timestamp);
  1418. if (rc)
  1419. goto exit;
  1420. list_for_each_entry(ts, &inst->timestamps.list, sort.list) {
  1421. if (prev) {
  1422. time_us = ts->sort.val - prev->sort.val;
  1423. prev_fr = curr_fr;
  1424. curr_fr = time_us ? DIV64_U64_ROUND_CLOSEST(USEC_PER_SEC, time_us) << 16 :
  1425. inst->auto_framerate;
  1426. if (curr_fr > inst->capabilities[FRAME_RATE].max)
  1427. curr_fr = inst->capabilities[FRAME_RATE].max;
  1428. }
  1429. prev = ts;
  1430. counter++;
  1431. }
  1432. if (counter < ENC_FPS_WINDOW)
  1433. goto exit;
  1434. /* if framerate changed and stable for 2 frames, set to firmware */
  1435. if (curr_fr == prev_fr && curr_fr != inst->auto_framerate) {
  1436. i_vpr_l(inst, "%s: updated fps: %u -> %u\n", __func__,
  1437. inst->auto_framerate >> 16, curr_fr >> 16);
  1438. rc = venus_hfi_session_property(inst,
  1439. HFI_PROP_FRAME_RATE,
  1440. HFI_HOST_FLAGS_NONE,
  1441. HFI_PORT_BITSTREAM,
  1442. HFI_PAYLOAD_Q16,
  1443. &curr_fr,
  1444. sizeof(u32));
  1445. if (rc) {
  1446. i_vpr_e(inst, "%s: set auto frame rate failed\n",
  1447. __func__);
  1448. goto exit;
  1449. }
  1450. inst->auto_framerate = curr_fr;
  1451. }
  1452. exit:
  1453. return rc;
  1454. }
  1455. int msm_vidc_update_input_rate(struct msm_vidc_inst *inst, u64 time_us)
  1456. {
  1457. struct msm_vidc_input_timer *input_timer;
  1458. struct msm_vidc_input_timer *prev_timer = NULL;
  1459. struct msm_vidc_core *core;
  1460. u64 counter = 0;
  1461. u64 input_timer_sum_us = 0;
  1462. core = inst->core;
  1463. input_timer = msm_vidc_pool_alloc(inst, MSM_MEM_POOL_BUF_TIMER);
  1464. if (!input_timer)
  1465. return -ENOMEM;
  1466. input_timer->time_us = time_us;
  1467. INIT_LIST_HEAD(&input_timer->list);
  1468. list_add_tail(&input_timer->list, &inst->input_timer_list);
  1469. list_for_each_entry(input_timer, &inst->input_timer_list, list) {
  1470. if (prev_timer) {
  1471. input_timer_sum_us += input_timer->time_us - prev_timer->time_us;
  1472. counter++;
  1473. }
  1474. prev_timer = input_timer;
  1475. }
  1476. if (input_timer_sum_us && counter >= INPUT_TIMER_LIST_SIZE)
  1477. inst->capabilities[INPUT_RATE].value =
  1478. (s32)(DIV64_U64_ROUND_CLOSEST(counter * 1000000,
  1479. input_timer_sum_us) << 16);
  1480. /* delete the first entry once counter >= INPUT_TIMER_LIST_SIZE */
  1481. if (counter >= INPUT_TIMER_LIST_SIZE) {
  1482. input_timer = list_first_entry(&inst->input_timer_list,
  1483. struct msm_vidc_input_timer, list);
  1484. list_del_init(&input_timer->list);
  1485. msm_vidc_pool_free(inst, input_timer);
  1486. }
  1487. return 0;
  1488. }
  1489. int msm_vidc_flush_input_timer(struct msm_vidc_inst *inst)
  1490. {
  1491. struct msm_vidc_input_timer *input_timer, *dummy_timer;
  1492. struct msm_vidc_core *core;
  1493. core = inst->core;
  1494. i_vpr_l(inst, "%s: flush input_timer list\n", __func__);
  1495. list_for_each_entry_safe(input_timer, dummy_timer, &inst->input_timer_list, list) {
  1496. list_del_init(&input_timer->list);
  1497. msm_vidc_pool_free(inst, input_timer);
  1498. }
  1499. return 0;
  1500. }
  1501. int msm_vidc_get_input_rate(struct msm_vidc_inst *inst)
  1502. {
  1503. return inst->capabilities[INPUT_RATE].value >> 16;
  1504. }
  1505. int msm_vidc_get_timestamp_rate(struct msm_vidc_inst *inst)
  1506. {
  1507. return inst->capabilities[TIMESTAMP_RATE].value >> 16;
  1508. }
  1509. int msm_vidc_get_frame_rate(struct msm_vidc_inst *inst)
  1510. {
  1511. return inst->capabilities[FRAME_RATE].value >> 16;
  1512. }
  1513. int msm_vidc_get_operating_rate(struct msm_vidc_inst *inst)
  1514. {
  1515. return inst->capabilities[OPERATING_RATE].value >> 16;
  1516. }
  1517. static int msm_vidc_insert_sort(struct list_head *head,
  1518. struct msm_vidc_sort *entry)
  1519. {
  1520. struct msm_vidc_sort *first, *node;
  1521. struct msm_vidc_sort *prev = NULL;
  1522. bool is_inserted = false;
  1523. if (!head || !entry) {
  1524. d_vpr_e("%s: invalid params\n", __func__);
  1525. return -EINVAL;
  1526. }
  1527. if (list_empty(head)) {
  1528. list_add(&entry->list, head);
  1529. return 0;
  1530. }
  1531. first = list_first_entry(head, struct msm_vidc_sort, list);
  1532. if (entry->val < first->val) {
  1533. list_add(&entry->list, head);
  1534. return 0;
  1535. }
  1536. list_for_each_entry(node, head, list) {
  1537. if (prev &&
  1538. entry->val >= prev->val && entry->val <= node->val) {
  1539. list_add(&entry->list, &prev->list);
  1540. is_inserted = true;
  1541. break;
  1542. }
  1543. prev = node;
  1544. }
  1545. if (!is_inserted && prev)
  1546. list_add(&entry->list, &prev->list);
  1547. return 0;
  1548. }
  1549. static struct msm_vidc_timestamp *msm_vidc_get_least_rank_ts(struct msm_vidc_inst *inst)
  1550. {
  1551. struct msm_vidc_timestamp *ts, *final = NULL;
  1552. u64 least_rank = INT_MAX;
  1553. list_for_each_entry(ts, &inst->timestamps.list, sort.list) {
  1554. if (ts->rank < least_rank) {
  1555. least_rank = ts->rank;
  1556. final = ts;
  1557. }
  1558. }
  1559. return final;
  1560. }
  1561. int msm_vidc_flush_ts(struct msm_vidc_inst *inst)
  1562. {
  1563. struct msm_vidc_timestamp *temp, *ts = NULL;
  1564. struct msm_vidc_core *core;
  1565. core = inst->core;
  1566. list_for_each_entry_safe(ts, temp, &inst->timestamps.list, sort.list) {
  1567. i_vpr_l(inst, "%s: flushing ts: val %llu, rank %llu\n",
  1568. __func__, ts->sort.val, ts->rank);
  1569. list_del(&ts->sort.list);
  1570. msm_vidc_pool_free(inst, ts);
  1571. }
  1572. inst->timestamps.count = 0;
  1573. inst->timestamps.rank = 0;
  1574. return 0;
  1575. }
  1576. int msm_vidc_update_timestamp_rate(struct msm_vidc_inst *inst, u64 timestamp)
  1577. {
  1578. struct msm_vidc_timestamp *ts, *prev = NULL;
  1579. struct msm_vidc_core *core;
  1580. int rc = 0;
  1581. u32 window_size = 0;
  1582. u32 timestamp_rate = 0;
  1583. u64 ts_ms = 0;
  1584. u32 counter = 0;
  1585. core = inst->core;
  1586. ts = msm_vidc_pool_alloc(inst, MSM_MEM_POOL_TIMESTAMP);
  1587. if (!ts) {
  1588. i_vpr_e(inst, "%s: ts alloc failed\n", __func__);
  1589. return -ENOMEM;
  1590. }
  1591. INIT_LIST_HEAD(&ts->sort.list);
  1592. ts->sort.val = timestamp;
  1593. ts->rank = inst->timestamps.rank++;
  1594. rc = msm_vidc_insert_sort(&inst->timestamps.list, &ts->sort);
  1595. if (rc)
  1596. return rc;
  1597. inst->timestamps.count++;
  1598. if (is_encode_session(inst))
  1599. window_size = ENC_FPS_WINDOW;
  1600. else
  1601. window_size = DEC_FPS_WINDOW;
  1602. /* keep sliding window */
  1603. if (inst->timestamps.count > window_size) {
  1604. ts = msm_vidc_get_least_rank_ts(inst);
  1605. if (!ts) {
  1606. i_vpr_e(inst, "%s: least rank ts is NULL\n", __func__);
  1607. return -EINVAL;
  1608. }
  1609. inst->timestamps.count--;
  1610. list_del(&ts->sort.list);
  1611. msm_vidc_pool_free(inst, ts);
  1612. }
  1613. /* Calculate timestamp rate */
  1614. list_for_each_entry(ts, &inst->timestamps.list, sort.list) {
  1615. if (prev) {
  1616. if (ts->sort.val == prev->sort.val)
  1617. continue;
  1618. ts_ms += div_u64(ts->sort.val - prev->sort.val, 1000000);
  1619. counter++;
  1620. }
  1621. prev = ts;
  1622. }
  1623. if (ts_ms)
  1624. timestamp_rate = (u32)div_u64((u64)counter * 1000, ts_ms);
  1625. msm_vidc_update_cap_value(inst, TIMESTAMP_RATE, timestamp_rate << 16, __func__);
  1626. return 0;
  1627. }
  1628. int msm_vidc_ts_reorder_insert_timestamp(struct msm_vidc_inst *inst, u64 timestamp)
  1629. {
  1630. struct msm_vidc_timestamp *ts;
  1631. struct msm_vidc_core *core;
  1632. int rc = 0;
  1633. core = inst->core;
  1634. /* allocate ts from pool */
  1635. ts = msm_vidc_pool_alloc(inst, MSM_MEM_POOL_TIMESTAMP);
  1636. if (!ts) {
  1637. i_vpr_e(inst, "%s: ts alloc failed\n", __func__);
  1638. return -ENOMEM;
  1639. }
  1640. /* initialize ts node */
  1641. INIT_LIST_HEAD(&ts->sort.list);
  1642. ts->sort.val = timestamp;
  1643. rc = msm_vidc_insert_sort(&inst->ts_reorder.list, &ts->sort);
  1644. if (rc)
  1645. return rc;
  1646. inst->ts_reorder.count++;
  1647. return 0;
  1648. }
  1649. int msm_vidc_ts_reorder_remove_timestamp(struct msm_vidc_inst *inst, u64 timestamp)
  1650. {
  1651. struct msm_vidc_timestamp *ts, *temp;
  1652. struct msm_vidc_core *core;
  1653. core = inst->core;
  1654. /* remove matching node */
  1655. list_for_each_entry_safe(ts, temp, &inst->ts_reorder.list, sort.list) {
  1656. if (ts->sort.val == timestamp) {
  1657. list_del_init(&ts->sort.list);
  1658. inst->ts_reorder.count--;
  1659. msm_vidc_pool_free(inst, ts);
  1660. break;
  1661. }
  1662. }
  1663. return 0;
  1664. }
  1665. int msm_vidc_ts_reorder_get_first_timestamp(struct msm_vidc_inst *inst, u64 *timestamp)
  1666. {
  1667. struct msm_vidc_timestamp *ts;
  1668. struct msm_vidc_core *core;
  1669. core = inst->core;
  1670. /* check if list empty */
  1671. if (list_empty(&inst->ts_reorder.list)) {
  1672. i_vpr_e(inst, "%s: list empty. ts %lld\n", __func__, *timestamp);
  1673. return -EINVAL;
  1674. }
  1675. /* get 1st node from reorder list */
  1676. ts = list_first_entry(&inst->ts_reorder.list,
  1677. struct msm_vidc_timestamp, sort.list);
  1678. list_del_init(&ts->sort.list);
  1679. /* copy timestamp */
  1680. *timestamp = ts->sort.val;
  1681. inst->ts_reorder.count--;
  1682. msm_vidc_pool_free(inst, ts);
  1683. return 0;
  1684. }
  1685. int msm_vidc_ts_reorder_flush(struct msm_vidc_inst *inst)
  1686. {
  1687. struct msm_vidc_timestamp *temp, *ts = NULL;
  1688. struct msm_vidc_core *core;
  1689. core = inst->core;
  1690. /* flush all entries */
  1691. list_for_each_entry_safe(ts, temp, &inst->ts_reorder.list, sort.list) {
  1692. i_vpr_l(inst, "%s: flushing ts: val %lld\n", __func__, ts->sort.val);
  1693. list_del(&ts->sort.list);
  1694. msm_vidc_pool_free(inst, ts);
  1695. }
  1696. inst->ts_reorder.count = 0;
  1697. return 0;
  1698. }
  1699. struct msm_vidc_buffer *msm_vidc_get_driver_buf(struct msm_vidc_inst *inst,
  1700. struct vb2_buffer *vb2)
  1701. {
  1702. int rc = 0;
  1703. struct msm_vidc_buffer *buf;
  1704. struct msm_vidc_core *core;
  1705. core = inst->core;
  1706. buf = msm_vidc_fetch_buffer(inst, vb2);
  1707. if (!buf) {
  1708. i_vpr_e(inst, "%s: failed to fetch buffer\n", __func__);
  1709. return NULL;
  1710. }
  1711. rc = vb2_buffer_to_driver(vb2, buf);
  1712. if (rc)
  1713. return NULL;
  1714. /* treat every buffer as deferred buffer initially */
  1715. buf->attr |= MSM_VIDC_ATTR_DEFERRED;
  1716. if (is_decode_session(inst) && is_output_buffer(buf->type)) {
  1717. /* get a reference */
  1718. if (!buf->dbuf_get) {
  1719. buf->dmabuf = call_mem_op(core, dma_buf_get, inst, buf->fd);
  1720. if (!buf->dmabuf)
  1721. return NULL;
  1722. buf->dbuf_get = 1;
  1723. }
  1724. }
  1725. return buf;
  1726. }
  1727. int msm_vidc_allocate_buffers(struct msm_vidc_inst *inst,
  1728. enum msm_vidc_buffer_type buf_type, u32 num_buffers)
  1729. {
  1730. int rc = 0;
  1731. int idx = 0;
  1732. struct msm_vidc_buffer *buf = NULL;
  1733. struct msm_vidc_buffers *buffers;
  1734. struct msm_vidc_core *core;
  1735. core = inst->core;
  1736. buffers = msm_vidc_get_buffers(inst, buf_type, __func__);
  1737. if (!buffers)
  1738. return -EINVAL;
  1739. for (idx = 0; idx < num_buffers; idx++) {
  1740. buf = msm_vidc_pool_alloc(inst, MSM_MEM_POOL_BUFFER);
  1741. if (!buf) {
  1742. i_vpr_e(inst, "%s: alloc failed\n", __func__);
  1743. return -EINVAL;
  1744. }
  1745. INIT_LIST_HEAD(&buf->list);
  1746. list_add_tail(&buf->list, &buffers->list);
  1747. buf->type = buf_type;
  1748. buf->index = idx;
  1749. buf->region = call_mem_op(core, buffer_region, inst, buf_type);
  1750. }
  1751. i_vpr_h(inst, "%s: allocated %d buffers for type %s\n",
  1752. __func__, num_buffers, buf_name(buf_type));
  1753. return rc;
  1754. }
  1755. int msm_vidc_free_buffers(struct msm_vidc_inst *inst,
  1756. enum msm_vidc_buffer_type buf_type)
  1757. {
  1758. int rc = 0;
  1759. int buf_count = 0;
  1760. struct msm_vidc_buffer *buf, *dummy;
  1761. struct msm_vidc_buffers *buffers;
  1762. struct msm_vidc_core *core;
  1763. core = inst->core;
  1764. buffers = msm_vidc_get_buffers(inst, buf_type, __func__);
  1765. if (!buffers)
  1766. return -EINVAL;
  1767. list_for_each_entry_safe(buf, dummy, &buffers->list, list) {
  1768. buf_count++;
  1769. print_vidc_buffer(VIDC_LOW, "low ", "free buffer", inst, buf);
  1770. list_del_init(&buf->list);
  1771. msm_vidc_pool_free(inst, buf);
  1772. }
  1773. i_vpr_h(inst, "%s: freed %d buffers for type %s\n",
  1774. __func__, buf_count, buf_name(buf_type));
  1775. return rc;
  1776. }
  1777. struct msm_vidc_buffer *msm_vidc_fetch_buffer(struct msm_vidc_inst *inst,
  1778. struct vb2_buffer *vb2)
  1779. {
  1780. struct msm_vidc_buffer *buf = NULL;
  1781. struct msm_vidc_buffers *buffers;
  1782. enum msm_vidc_buffer_type buf_type;
  1783. bool found = false;
  1784. buf_type = v4l2_type_to_driver(vb2->type, __func__);
  1785. if (!buf_type)
  1786. return NULL;
  1787. buffers = msm_vidc_get_buffers(inst, buf_type, __func__);
  1788. if (!buffers)
  1789. return NULL;
  1790. list_for_each_entry(buf, &buffers->list, list) {
  1791. if (buf->index == vb2->index) {
  1792. found = true;
  1793. break;
  1794. }
  1795. }
  1796. if (!found) {
  1797. i_vpr_e(inst, "%s: buffer not found for index %d for vb2 buffer type %s\n",
  1798. __func__, vb2->index, v4l2_type_name(vb2->type));
  1799. return NULL;
  1800. }
  1801. return buf;
  1802. }
  1803. struct msm_vidc_buffer *get_meta_buffer(struct msm_vidc_inst *inst,
  1804. struct msm_vidc_buffer *buf)
  1805. {
  1806. struct msm_vidc_buffer *mbuf;
  1807. struct msm_vidc_buffers *buffers;
  1808. bool found = false;
  1809. if (buf->type == MSM_VIDC_BUF_INPUT) {
  1810. buffers = &inst->buffers.input_meta;
  1811. } else if (buf->type == MSM_VIDC_BUF_OUTPUT) {
  1812. buffers = &inst->buffers.output_meta;
  1813. } else {
  1814. i_vpr_e(inst, "%s: invalid buffer type %d\n",
  1815. __func__, buf->type);
  1816. return NULL;
  1817. }
  1818. list_for_each_entry(mbuf, &buffers->list, list) {
  1819. if (mbuf->index == buf->index) {
  1820. found = true;
  1821. break;
  1822. }
  1823. }
  1824. if (!found)
  1825. return NULL;
  1826. return mbuf;
  1827. }
  1828. bool msm_vidc_is_super_buffer(struct msm_vidc_inst *inst)
  1829. {
  1830. return !!inst->capabilities[SUPER_FRAME].value;
  1831. }
  1832. static bool is_single_session(struct msm_vidc_inst *inst)
  1833. {
  1834. struct msm_vidc_core *core;
  1835. u32 count = 0;
  1836. core = inst->core;
  1837. core_lock(core, __func__);
  1838. list_for_each_entry(inst, &core->instances, list)
  1839. count++;
  1840. core_unlock(core, __func__);
  1841. return count == 1;
  1842. }
  1843. void msm_vidc_allow_dcvs(struct msm_vidc_inst *inst)
  1844. {
  1845. bool allow = false;
  1846. struct msm_vidc_core *core;
  1847. u32 fps;
  1848. core = inst->core;
  1849. allow = !msm_vidc_clock_voting;
  1850. if (!allow) {
  1851. i_vpr_h(inst, "%s: core_clock_voting is set\n", __func__);
  1852. goto exit;
  1853. }
  1854. allow = core->capabilities[DCVS].value;
  1855. if (!allow) {
  1856. i_vpr_h(inst, "%s: core doesn't support dcvs\n", __func__);
  1857. goto exit;
  1858. }
  1859. allow = !inst->decode_batch.enable;
  1860. if (!allow) {
  1861. i_vpr_h(inst, "%s: decode_batching enabled\n", __func__);
  1862. goto exit;
  1863. }
  1864. allow = !msm_vidc_is_super_buffer(inst);
  1865. if (!allow) {
  1866. i_vpr_h(inst, "%s: encode_batching(super_buffer) enabled\n", __func__);
  1867. goto exit;
  1868. }
  1869. allow = !is_thumbnail_session(inst);
  1870. if (!allow) {
  1871. i_vpr_h(inst, "%s: thumbnail session\n", __func__);
  1872. goto exit;
  1873. }
  1874. allow = !is_critical_priority_session(inst);
  1875. if (!allow) {
  1876. i_vpr_h(inst, "%s: critical priority session\n", __func__);
  1877. goto exit;
  1878. }
  1879. allow = !is_image_session(inst);
  1880. if (!allow) {
  1881. i_vpr_h(inst, "%s: image session\n", __func__);
  1882. goto exit;
  1883. }
  1884. allow = !is_lowlatency_session(inst);
  1885. if (!allow) {
  1886. i_vpr_h(inst, "%s: lowlatency session\n", __func__);
  1887. goto exit;
  1888. }
  1889. fps = msm_vidc_get_fps(inst);
  1890. if (is_decode_session(inst) &&
  1891. fps >= inst->capabilities[FRAME_RATE].max) {
  1892. allow = false;
  1893. i_vpr_h(inst, "%s: unsupported fps %d\n", __func__, fps);
  1894. goto exit;
  1895. }
  1896. exit:
  1897. i_vpr_hp(inst, "%s: dcvs: %s\n", __func__, allow ? "enabled" : "disabled");
  1898. inst->power.dcvs_flags = 0;
  1899. inst->power.dcvs_mode = allow;
  1900. }
  1901. bool msm_vidc_allow_decode_batch(struct msm_vidc_inst *inst)
  1902. {
  1903. struct msm_vidc_inst_cap *cap;
  1904. struct msm_vidc_core *core;
  1905. bool allow = false;
  1906. u32 value = 0;
  1907. core = inst->core;
  1908. cap = &inst->capabilities[0];
  1909. allow = inst->decode_batch.enable;
  1910. if (!allow) {
  1911. i_vpr_h(inst, "%s: batching already disabled\n", __func__);
  1912. goto exit;
  1913. }
  1914. allow = core->capabilities[DECODE_BATCH].value;
  1915. if (!allow) {
  1916. i_vpr_h(inst, "%s: core doesn't support batching\n", __func__);
  1917. goto exit;
  1918. }
  1919. allow = is_single_session(inst);
  1920. if (!allow) {
  1921. i_vpr_h(inst, "%s: multiple sessions running\n", __func__);
  1922. goto exit;
  1923. }
  1924. allow = is_decode_session(inst);
  1925. if (!allow) {
  1926. i_vpr_h(inst, "%s: not a decoder session\n", __func__);
  1927. goto exit;
  1928. }
  1929. allow = !is_thumbnail_session(inst);
  1930. if (!allow) {
  1931. i_vpr_h(inst, "%s: thumbnail session\n", __func__);
  1932. goto exit;
  1933. }
  1934. allow = !is_image_session(inst);
  1935. if (!allow) {
  1936. i_vpr_h(inst, "%s: image session\n", __func__);
  1937. goto exit;
  1938. }
  1939. allow = is_realtime_session(inst);
  1940. if (!allow) {
  1941. i_vpr_h(inst, "%s: non-realtime session\n", __func__);
  1942. goto exit;
  1943. }
  1944. allow = !is_lowlatency_session(inst);
  1945. if (!allow) {
  1946. i_vpr_h(inst, "%s: lowlatency session\n", __func__);
  1947. goto exit;
  1948. }
  1949. value = msm_vidc_get_fps(inst);
  1950. allow = value < cap[BATCH_FPS].value;
  1951. if (!allow) {
  1952. i_vpr_h(inst, "%s: unsupported fps %u, max %u\n", __func__,
  1953. value, cap[BATCH_FPS].value);
  1954. goto exit;
  1955. }
  1956. value = msm_vidc_get_mbs_per_frame(inst);
  1957. allow = value < cap[BATCH_MBPF].value;
  1958. if (!allow) {
  1959. i_vpr_h(inst, "%s: unsupported mbpf %u, max %u\n", __func__,
  1960. value, cap[BATCH_MBPF].value);
  1961. goto exit;
  1962. }
  1963. exit:
  1964. i_vpr_hp(inst, "%s: batching: %s\n", __func__, allow ? "enabled" : "disabled");
  1965. return allow;
  1966. }
  1967. static void msm_vidc_update_input_cr(struct msm_vidc_inst *inst, u32 idx, u32 cr)
  1968. {
  1969. struct msm_vidc_input_cr_data *temp = NULL, *next = NULL;
  1970. bool found = false;
  1971. list_for_each_entry_safe(temp, next, &inst->enc_input_crs, list) {
  1972. if (temp->index == idx) {
  1973. temp->input_cr = cr;
  1974. found = true;
  1975. break;
  1976. }
  1977. }
  1978. if (!found) {
  1979. temp = NULL;
  1980. if (msm_vidc_vmem_alloc(sizeof(*temp), (void **)&temp, __func__))
  1981. return;
  1982. temp->index = idx;
  1983. temp->input_cr = cr;
  1984. list_add_tail(&temp->list, &inst->enc_input_crs);
  1985. }
  1986. }
  1987. void msm_vidc_update_stats(struct msm_vidc_inst *inst,
  1988. struct msm_vidc_buffer *buf, enum msm_vidc_debugfs_event etype)
  1989. {
  1990. if ((is_decode_session(inst) && etype == MSM_VIDC_DEBUGFS_EVENT_ETB) ||
  1991. (is_encode_session(inst) && etype == MSM_VIDC_DEBUGFS_EVENT_FBD))
  1992. inst->stats.data_size += buf->data_size;
  1993. msm_vidc_debugfs_update(inst, etype);
  1994. }
  1995. void msm_vidc_print_stats(struct msm_vidc_inst *inst)
  1996. {
  1997. u32 frame_rate, operating_rate, achieved_fps, priority, etb, ebd, ftb, fbd, dt_ms;
  1998. u64 bitrate_kbps = 0, time_ms = ktime_get_ns() / 1000 / 1000;
  1999. etb = inst->debug_count.etb - inst->stats.count.etb;
  2000. ebd = inst->debug_count.ebd - inst->stats.count.ebd;
  2001. ftb = inst->debug_count.ftb - inst->stats.count.ftb;
  2002. fbd = inst->debug_count.fbd - inst->stats.count.fbd;
  2003. frame_rate = inst->capabilities[FRAME_RATE].value >> 16;
  2004. operating_rate = inst->capabilities[OPERATING_RATE].value >> 16;
  2005. priority = inst->capabilities[PRIORITY].value;
  2006. dt_ms = time_ms - inst->stats.time_ms;
  2007. achieved_fps = (fbd * 1000) / dt_ms;
  2008. bitrate_kbps = (inst->stats.data_size * 8 * 1000) / (dt_ms * 1024);
  2009. i_vpr_hs(inst,
  2010. "counts (etb,ebd,ftb,fbd): %u %u %u %u (total %llu %llu %llu %llu), achieved bitrate %lldKbps fps %u/s, frame rate %u, operating rate %u, priority %u, avg bw llcc %ukhz, avb bw ddr %ukhz, dt %ums\n",
  2011. etb, ebd, ftb, fbd, inst->debug_count.etb, inst->debug_count.ebd,
  2012. inst->debug_count.ftb, inst->debug_count.fbd, bitrate_kbps,
  2013. achieved_fps, frame_rate, operating_rate, priority,
  2014. inst->stats.avg_bw_llcc, inst->stats.avg_bw_ddr, dt_ms);
  2015. inst->stats.count = inst->debug_count;
  2016. inst->stats.data_size = 0;
  2017. inst->stats.avg_bw_llcc = 0;
  2018. inst->stats.avg_bw_ddr = 0;
  2019. inst->stats.time_ms = time_ms;
  2020. }
  2021. void msm_vidc_print_memory_stats(struct msm_vidc_inst *inst)
  2022. {
  2023. static enum msm_vidc_buffer_type buf_type_arr[9] = {
  2024. MSM_VIDC_BUF_BIN,
  2025. MSM_VIDC_BUF_ARP,
  2026. MSM_VIDC_BUF_COMV,
  2027. MSM_VIDC_BUF_NON_COMV,
  2028. MSM_VIDC_BUF_LINE,
  2029. MSM_VIDC_BUF_DPB,
  2030. MSM_VIDC_BUF_PERSIST,
  2031. MSM_VIDC_BUF_VPSS,
  2032. MSM_VIDC_BUF_PARTIAL_DATA,
  2033. };
  2034. u32 count_arr[9];
  2035. u32 size_arr[9];
  2036. u32 size_kb_arr[9];
  2037. u64 total_size = 0;
  2038. struct msm_vidc_buffers *buffers;
  2039. int cnt;
  2040. /* reset array values */
  2041. memset(&count_arr, 0, sizeof(count_arr));
  2042. memset(&size_arr, 0, sizeof(size_arr));
  2043. memset(&size_kb_arr, 0, sizeof(size_kb_arr));
  2044. /* populate buffer details */
  2045. for (cnt = 0; cnt < 9; cnt++) {
  2046. buffers = msm_vidc_get_buffers(inst, buf_type_arr[cnt], __func__);
  2047. if (!buffers)
  2048. continue;
  2049. size_arr[cnt] = buffers->size;
  2050. count_arr[cnt] = buffers->min_count;
  2051. size_kb_arr[cnt] = (size_arr[cnt] * count_arr[cnt]) / 1024;
  2052. total_size += size_arr[cnt] * count_arr[cnt];
  2053. }
  2054. /* print internal memory stats */
  2055. i_vpr_hs(inst,
  2056. "%s %u kb(%ux%d) %s %u kb(%ux%d) %s %u kb(%ux%d) %s %u kb(%ux%d) %s %u kb(%ux%d) %s %u kb(%ux%d) %s %u kb(%ux%d) %s %u kb(%ux%d) %s %u kb(%ux%d) total %llu kb\n",
  2057. buf_name(buf_type_arr[0]), size_kb_arr[0], size_arr[0], count_arr[0],
  2058. buf_name(buf_type_arr[1]), size_kb_arr[1], size_arr[1], count_arr[1],
  2059. buf_name(buf_type_arr[2]), size_kb_arr[2], size_arr[2], count_arr[2],
  2060. buf_name(buf_type_arr[3]), size_kb_arr[3], size_arr[3], count_arr[3],
  2061. buf_name(buf_type_arr[4]), size_kb_arr[4], size_arr[4], count_arr[4],
  2062. buf_name(buf_type_arr[5]), size_kb_arr[5], size_arr[5], count_arr[5],
  2063. buf_name(buf_type_arr[6]), size_kb_arr[6], size_arr[6], count_arr[6],
  2064. buf_name(buf_type_arr[7]), size_kb_arr[7], size_arr[7], count_arr[7],
  2065. buf_name(buf_type_arr[8]), size_kb_arr[8], size_arr[8], count_arr[8],
  2066. (total_size / 1024));
  2067. }
  2068. int schedule_stats_work(struct msm_vidc_inst *inst)
  2069. {
  2070. struct msm_vidc_core *core;
  2071. if (!inst || !inst->core) {
  2072. d_vpr_e("%s: invalid params\n", __func__);
  2073. return -EINVAL;
  2074. }
  2075. if (!is_stats_enabled()) {
  2076. i_vpr_h(inst, "%s: stats not enabled. Skip scheduling\n", __func__);
  2077. return 0;
  2078. }
  2079. /**
  2080. * Hfi session is already closed and inst also going to be
  2081. * closed soon. So skip scheduling new stats_work to avoid
  2082. * use-after-free issues with close sequence.
  2083. */
  2084. if (!inst->packet) {
  2085. i_vpr_e(inst, "skip scheduling stats_work\n");
  2086. return 0;
  2087. }
  2088. core = inst->core;
  2089. mod_delayed_work(inst->workq, &inst->stats_work,
  2090. msecs_to_jiffies(core->capabilities[STATS_TIMEOUT_MS].value));
  2091. return 0;
  2092. }
  2093. int cancel_stats_work_sync(struct msm_vidc_inst *inst)
  2094. {
  2095. cancel_delayed_work_sync(&inst->stats_work);
  2096. return 0;
  2097. }
  2098. void msm_vidc_stats_handler(struct work_struct *work)
  2099. {
  2100. struct msm_vidc_inst *inst;
  2101. inst = container_of(work, struct msm_vidc_inst, stats_work.work);
  2102. inst = get_inst_ref(g_core, inst);
  2103. if (!inst || !inst->packet) {
  2104. d_vpr_e("%s: invalid params\n", __func__);
  2105. return;
  2106. }
  2107. inst_lock(inst, __func__);
  2108. msm_vidc_print_stats(inst);
  2109. schedule_stats_work(inst);
  2110. inst_unlock(inst, __func__);
  2111. put_inst(inst);
  2112. }
  2113. static int msm_vidc_queue_buffer(struct msm_vidc_inst *inst, struct msm_vidc_buffer *buf)
  2114. {
  2115. struct msm_vidc_buffer *meta;
  2116. enum msm_vidc_debugfs_event etype;
  2117. int rc = 0;
  2118. u32 cr = 0;
  2119. if (is_encode_session(inst) && is_input_buffer(buf->type)) {
  2120. cr = inst->capabilities[ENC_IP_CR].value;
  2121. msm_vidc_update_input_cr(inst, buf->index, cr);
  2122. msm_vidc_update_cap_value(inst, ENC_IP_CR, 0, __func__);
  2123. }
  2124. if (is_decode_session(inst) && is_input_buffer(buf->type) &&
  2125. inst->capabilities[CODEC_CONFIG].value) {
  2126. buf->flags |= MSM_VIDC_BUF_FLAG_CODECCONFIG;
  2127. msm_vidc_update_cap_value(inst, CODEC_CONFIG, 0, __func__);
  2128. }
  2129. if (is_decode_session(inst) && is_output_buffer(buf->type)) {
  2130. rc = msm_vidc_process_readonly_buffers(inst, buf);
  2131. if (rc)
  2132. return rc;
  2133. }
  2134. print_vidc_buffer(VIDC_HIGH, "high", "qbuf", inst, buf);
  2135. meta = get_meta_buffer(inst, buf);
  2136. if (meta && meta->attr & MSM_VIDC_ATTR_DEFERRED)
  2137. print_vidc_buffer(VIDC_LOW, "low ", "qbuf", inst, meta);
  2138. else
  2139. meta = NULL;
  2140. if (!meta && is_meta_enabled(inst, buf->type)) {
  2141. print_vidc_buffer(VIDC_ERR, "err ", "missing meta for", inst, buf);
  2142. return -EINVAL;
  2143. }
  2144. if (msm_vidc_is_super_buffer(inst) && is_input_buffer(buf->type))
  2145. rc = venus_hfi_queue_super_buffer(inst, buf, meta);
  2146. else
  2147. rc = venus_hfi_queue_buffer(inst, buf, meta);
  2148. if (rc)
  2149. return rc;
  2150. buf->attr &= ~MSM_VIDC_ATTR_DEFERRED;
  2151. buf->attr |= MSM_VIDC_ATTR_QUEUED;
  2152. if (meta) {
  2153. meta->attr &= ~MSM_VIDC_ATTR_DEFERRED;
  2154. meta->attr |= MSM_VIDC_ATTR_QUEUED;
  2155. }
  2156. /* insert timestamp for ts_reorder enable case */
  2157. if (is_ts_reorder_allowed(inst) && is_input_buffer(buf->type)) {
  2158. rc = msm_vidc_ts_reorder_insert_timestamp(inst, buf->timestamp);
  2159. if (rc)
  2160. i_vpr_e(inst, "%s: insert timestamp failed\n", __func__);
  2161. }
  2162. if (is_input_buffer(buf->type))
  2163. inst->power.buffer_counter++;
  2164. if (is_input_buffer(buf->type))
  2165. etype = MSM_VIDC_DEBUGFS_EVENT_ETB;
  2166. else
  2167. etype = MSM_VIDC_DEBUGFS_EVENT_FTB;
  2168. msm_vidc_update_stats(inst, buf, etype);
  2169. return 0;
  2170. }
  2171. int msm_vidc_alloc_and_queue_input_internal_buffers(struct msm_vidc_inst *inst)
  2172. {
  2173. int rc = 0;
  2174. rc = msm_vdec_get_input_internal_buffers(inst);
  2175. if (rc)
  2176. return rc;
  2177. rc = msm_vdec_release_input_internal_buffers(inst);
  2178. if (rc)
  2179. return rc;
  2180. rc = msm_vdec_create_input_internal_buffers(inst);
  2181. if (rc)
  2182. return rc;
  2183. rc = msm_vdec_queue_input_internal_buffers(inst);
  2184. if (rc)
  2185. return rc;
  2186. return rc;
  2187. }
  2188. int msm_vidc_queue_deferred_buffers(struct msm_vidc_inst *inst, enum msm_vidc_buffer_type buf_type)
  2189. {
  2190. struct msm_vidc_buffers *buffers;
  2191. struct msm_vidc_buffer *buf;
  2192. int rc = 0;
  2193. buffers = msm_vidc_get_buffers(inst, buf_type, __func__);
  2194. if (!buffers)
  2195. return -EINVAL;
  2196. msm_vidc_scale_power(inst, true);
  2197. list_for_each_entry(buf, &buffers->list, list) {
  2198. if (!(buf->attr & MSM_VIDC_ATTR_DEFERRED))
  2199. continue;
  2200. rc = msm_vidc_queue_buffer(inst, buf);
  2201. if (rc)
  2202. return rc;
  2203. }
  2204. return 0;
  2205. }
  2206. int msm_vidc_buf_queue(struct msm_vidc_inst *inst, struct msm_vidc_buffer *buf)
  2207. {
  2208. int rc = 0;
  2209. msm_vidc_scale_power(inst, is_input_buffer(buf->type));
  2210. rc = msm_vidc_queue_buffer(inst, buf);
  2211. if (rc)
  2212. return rc;
  2213. return rc;
  2214. }
  2215. int msm_vidc_queue_buffer_single(struct msm_vidc_inst *inst, struct vb2_buffer *vb2)
  2216. {
  2217. int rc = 0;
  2218. struct msm_vidc_buffer *buf = NULL;
  2219. struct msm_vidc_fence *fence = NULL;
  2220. struct msm_vidc_core *core = NULL;
  2221. core = inst->core;
  2222. buf = msm_vidc_get_driver_buf(inst, vb2);
  2223. if (!buf)
  2224. return -EINVAL;
  2225. if (is_meta_rx_inp_enabled(inst, META_OUTBUF_FENCE) &&
  2226. is_output_buffer(buf->type)) {
  2227. fence = call_fence_op(core, fence_create, inst);
  2228. if (!fence)
  2229. return -EINVAL;
  2230. buf->fence_id = fence->fence_id;
  2231. }
  2232. rc = inst->event_handle(inst, MSM_VIDC_BUF_QUEUE, buf);
  2233. if (rc)
  2234. goto exit;
  2235. exit:
  2236. if (rc) {
  2237. i_vpr_e(inst, "%s: qbuf failed\n", __func__);
  2238. if (fence)
  2239. call_fence_op(core, fence_destroy, inst, fence->fence_id);
  2240. }
  2241. return rc;
  2242. }
  2243. int msm_vidc_destroy_internal_buffer(struct msm_vidc_inst *inst,
  2244. struct msm_vidc_buffer *buffer)
  2245. {
  2246. struct msm_vidc_buffers *buffers;
  2247. struct msm_vidc_mem_list *mem_list;
  2248. struct msm_vidc_mem *mem, *mem_dummy;
  2249. struct msm_vidc_buffer *buf, *dummy;
  2250. struct msm_vidc_core *core;
  2251. core = inst->core;
  2252. if (!is_internal_buffer(buffer->type)) {
  2253. i_vpr_e(inst, "%s: type: %s is not internal\n",
  2254. __func__, buf_name(buffer->type));
  2255. return 0;
  2256. }
  2257. i_vpr_h(inst, "%s: destroy: type: %8s, size: %9u, device_addr %#llx\n", __func__,
  2258. buf_name(buffer->type), buffer->buffer_size, buffer->device_addr);
  2259. buffers = msm_vidc_get_buffers(inst, buffer->type, __func__);
  2260. if (!buffers)
  2261. return -EINVAL;
  2262. mem_list = msm_vidc_get_mem_info(inst, buffer->type, __func__);
  2263. if (!mem_list)
  2264. return -EINVAL;
  2265. list_for_each_entry_safe(mem, mem_dummy, &mem_list->list, list) {
  2266. if (mem->dmabuf == buffer->dmabuf) {
  2267. call_mem_op(core, memory_unmap_free, core, mem);
  2268. list_del(&mem->list);
  2269. msm_vidc_pool_free(inst, mem);
  2270. break;
  2271. }
  2272. }
  2273. list_for_each_entry_safe(buf, dummy, &buffers->list, list) {
  2274. if (buf->dmabuf == buffer->dmabuf) {
  2275. list_del(&buf->list);
  2276. msm_vidc_pool_free(inst, buf);
  2277. break;
  2278. }
  2279. }
  2280. return 0;
  2281. }
  2282. int msm_vidc_get_internal_buffers(struct msm_vidc_inst *inst,
  2283. enum msm_vidc_buffer_type buffer_type)
  2284. {
  2285. u32 buf_size;
  2286. u32 buf_count;
  2287. struct msm_vidc_core *core;
  2288. struct msm_vidc_buffers *buffers;
  2289. core = inst->core;
  2290. buf_size = call_session_op(core, buffer_size,
  2291. inst, buffer_type);
  2292. buf_count = call_session_op(core, min_count,
  2293. inst, buffer_type);
  2294. buffers = msm_vidc_get_buffers(inst, buffer_type, __func__);
  2295. if (!buffers)
  2296. return -EINVAL;
  2297. /*
  2298. * In a usecase when film grain is initially present, dpb buffers
  2299. * are allocated and in the middle of the session, if film grain
  2300. * is disabled, then dpb internal buffers should be destroyed.
  2301. * When film grain is disabled, buffer_size op call returns 0.
  2302. * To ensure buffers->reuse is set to false, add check to detect
  2303. * if buf_size has become zero. Do the same for buf_count as well.
  2304. */
  2305. if (buf_size && buf_size <= buffers->size &&
  2306. buf_count && buf_count <= buffers->min_count) {
  2307. buffers->reuse = true;
  2308. } else {
  2309. buffers->reuse = false;
  2310. buffers->size = buf_size;
  2311. buffers->min_count = buf_count;
  2312. }
  2313. return 0;
  2314. }
  2315. int msm_vidc_create_internal_buffer(struct msm_vidc_inst *inst,
  2316. enum msm_vidc_buffer_type buffer_type, u32 index)
  2317. {
  2318. int rc = 0;
  2319. struct msm_vidc_buffers *buffers;
  2320. struct msm_vidc_mem_list *mem_list;
  2321. struct msm_vidc_buffer *buffer;
  2322. struct msm_vidc_mem *mem;
  2323. struct msm_vidc_core *core;
  2324. core = inst->core;
  2325. if (!is_internal_buffer(buffer_type)) {
  2326. i_vpr_e(inst, "%s: type %s is not internal\n",
  2327. __func__, buf_name(buffer_type));
  2328. return 0;
  2329. }
  2330. buffers = msm_vidc_get_buffers(inst, buffer_type, __func__);
  2331. if (!buffers)
  2332. return -EINVAL;
  2333. mem_list = msm_vidc_get_mem_info(inst, buffer_type, __func__);
  2334. if (!mem_list)
  2335. return -EINVAL;
  2336. if (!buffers->size)
  2337. return 0;
  2338. buffer = msm_vidc_pool_alloc(inst, MSM_MEM_POOL_BUFFER);
  2339. if (!buffer) {
  2340. i_vpr_e(inst, "%s: buf alloc failed\n", __func__);
  2341. return -ENOMEM;
  2342. }
  2343. INIT_LIST_HEAD(&buffer->list);
  2344. buffer->type = buffer_type;
  2345. buffer->index = index;
  2346. buffer->buffer_size = buffers->size;
  2347. list_add_tail(&buffer->list, &buffers->list);
  2348. mem = msm_vidc_pool_alloc(inst, MSM_MEM_POOL_ALLOC_MAP);
  2349. if (!mem) {
  2350. i_vpr_e(inst, "%s: mem poo alloc failed\n", __func__);
  2351. return -ENOMEM;
  2352. }
  2353. INIT_LIST_HEAD(&mem->list);
  2354. mem->type = buffer_type;
  2355. mem->region = call_mem_op(core, buffer_region, inst, buffer_type);
  2356. mem->size = buffer->buffer_size;
  2357. mem->secure = is_secure_region(mem->region);
  2358. rc = call_mem_op(core, memory_alloc_map, core, mem);
  2359. if (rc)
  2360. return -ENOMEM;
  2361. list_add_tail(&mem->list, &mem_list->list);
  2362. buffer->dmabuf = mem->dmabuf;
  2363. buffer->device_addr = mem->device_addr;
  2364. buffer->region = mem->region;
  2365. i_vpr_h(inst, "%s: create: type: %8s, size: %9u, device_addr %#llx\n", __func__,
  2366. buf_name(buffer_type), buffers->size, buffer->device_addr);
  2367. return 0;
  2368. }
  2369. int msm_vidc_create_internal_buffers(struct msm_vidc_inst *inst,
  2370. enum msm_vidc_buffer_type buffer_type)
  2371. {
  2372. int rc = 0;
  2373. struct msm_vidc_buffers *buffers;
  2374. int i;
  2375. buffers = msm_vidc_get_buffers(inst, buffer_type, __func__);
  2376. if (!buffers)
  2377. return -EINVAL;
  2378. if (buffers->reuse) {
  2379. i_vpr_l(inst, "%s: reuse enabled for %s\n", __func__, buf_name(buffer_type));
  2380. return 0;
  2381. }
  2382. for (i = 0; i < buffers->min_count; i++) {
  2383. rc = msm_vidc_create_internal_buffer(inst, buffer_type, i);
  2384. if (rc)
  2385. return rc;
  2386. }
  2387. return rc;
  2388. }
  2389. int msm_vidc_queue_internal_buffers(struct msm_vidc_inst *inst,
  2390. enum msm_vidc_buffer_type buffer_type)
  2391. {
  2392. int rc = 0;
  2393. struct msm_vidc_buffers *buffers;
  2394. struct msm_vidc_buffer *buffer, *dummy;
  2395. if (!is_internal_buffer(buffer_type)) {
  2396. i_vpr_e(inst, "%s: %s is not internal\n", __func__, buf_name(buffer_type));
  2397. return 0;
  2398. }
  2399. /*
  2400. * Set HFI_PROP_COMV_BUFFER_COUNT to firmware even if COMV buffer
  2401. * is reused.
  2402. */
  2403. if (is_decode_session(inst) && buffer_type == MSM_VIDC_BUF_COMV) {
  2404. rc = msm_vdec_set_num_comv(inst);
  2405. if (rc)
  2406. return rc;
  2407. }
  2408. buffers = msm_vidc_get_buffers(inst, buffer_type, __func__);
  2409. if (!buffers)
  2410. return -EINVAL;
  2411. list_for_each_entry_safe(buffer, dummy, &buffers->list, list) {
  2412. /* do not queue pending release buffers */
  2413. if (buffer->attr & MSM_VIDC_ATTR_PENDING_RELEASE)
  2414. continue;
  2415. /* do not queue already queued buffers */
  2416. if (buffer->attr & MSM_VIDC_ATTR_QUEUED)
  2417. continue;
  2418. rc = venus_hfi_queue_buffer(inst, buffer, NULL);
  2419. if (rc)
  2420. return rc;
  2421. /* mark queued */
  2422. buffer->attr |= MSM_VIDC_ATTR_QUEUED;
  2423. i_vpr_h(inst, "%s: queue: type: %8s, size: %9u, device_addr %#llx\n", __func__,
  2424. buf_name(buffer->type), buffer->buffer_size, buffer->device_addr);
  2425. }
  2426. return 0;
  2427. }
  2428. int msm_vidc_alloc_and_queue_session_internal_buffers(struct msm_vidc_inst *inst,
  2429. enum msm_vidc_buffer_type buffer_type)
  2430. {
  2431. int rc = 0;
  2432. if (buffer_type != MSM_VIDC_BUF_ARP &&
  2433. buffer_type != MSM_VIDC_BUF_PERSIST) {
  2434. i_vpr_e(inst, "%s: invalid buffer type: %s\n",
  2435. __func__, buf_name(buffer_type));
  2436. rc = -EINVAL;
  2437. goto exit;
  2438. }
  2439. rc = msm_vidc_get_internal_buffers(inst, buffer_type);
  2440. if (rc)
  2441. goto exit;
  2442. rc = msm_vidc_create_internal_buffers(inst, buffer_type);
  2443. if (rc)
  2444. goto exit;
  2445. rc = msm_vidc_queue_internal_buffers(inst, buffer_type);
  2446. if (rc)
  2447. goto exit;
  2448. exit:
  2449. return rc;
  2450. }
  2451. int msm_vidc_release_internal_buffers(struct msm_vidc_inst *inst,
  2452. enum msm_vidc_buffer_type buffer_type)
  2453. {
  2454. int rc = 0;
  2455. struct msm_vidc_buffers *buffers;
  2456. struct msm_vidc_buffer *buffer, *dummy;
  2457. if (!is_internal_buffer(buffer_type)) {
  2458. i_vpr_e(inst, "%s: %s is not internal\n",
  2459. __func__, buf_name(buffer_type));
  2460. return 0;
  2461. }
  2462. buffers = msm_vidc_get_buffers(inst, buffer_type, __func__);
  2463. if (!buffers)
  2464. return -EINVAL;
  2465. if (buffers->reuse) {
  2466. i_vpr_l(inst, "%s: reuse enabled for %s buf\n",
  2467. __func__, buf_name(buffer_type));
  2468. return 0;
  2469. }
  2470. list_for_each_entry_safe(buffer, dummy, &buffers->list, list) {
  2471. /* do not release already pending release buffers */
  2472. if (buffer->attr & MSM_VIDC_ATTR_PENDING_RELEASE)
  2473. continue;
  2474. /* release only queued buffers */
  2475. if (!(buffer->attr & MSM_VIDC_ATTR_QUEUED))
  2476. continue;
  2477. rc = venus_hfi_release_buffer(inst, buffer);
  2478. if (rc)
  2479. return rc;
  2480. /* mark pending release */
  2481. buffer->attr |= MSM_VIDC_ATTR_PENDING_RELEASE;
  2482. i_vpr_h(inst, "%s: release: type: %8s, size: %9u, device_addr %#llx\n", __func__,
  2483. buf_name(buffer->type), buffer->buffer_size, buffer->device_addr);
  2484. }
  2485. return 0;
  2486. }
  2487. int msm_vidc_vb2_buffer_done(struct msm_vidc_inst *inst,
  2488. struct msm_vidc_buffer *buf)
  2489. {
  2490. int type, port, state;
  2491. struct vb2_queue *q;
  2492. struct vb2_buffer *vb2;
  2493. struct vb2_v4l2_buffer *vbuf;
  2494. bool found;
  2495. type = v4l2_type_from_driver(buf->type, __func__);
  2496. if (!type)
  2497. return -EINVAL;
  2498. port = v4l2_type_to_driver_port(inst, type, __func__);
  2499. if (port < 0)
  2500. return -EINVAL;
  2501. q = inst->bufq[port].vb2q;
  2502. if (!q->streaming) {
  2503. i_vpr_e(inst, "%s: port %d is not streaming\n",
  2504. __func__, port);
  2505. return -EINVAL;
  2506. }
  2507. found = false;
  2508. list_for_each_entry(vb2, &q->queued_list, queued_entry) {
  2509. if (vb2->state != VB2_BUF_STATE_ACTIVE)
  2510. continue;
  2511. if (vb2->index == buf->index) {
  2512. found = true;
  2513. break;
  2514. }
  2515. }
  2516. if (!found) {
  2517. print_vidc_buffer(VIDC_ERR, "err ", "vb2 not found for", inst, buf);
  2518. return -EINVAL;
  2519. }
  2520. /**
  2521. * v4l2 clears buffer state related flags. For driver errors
  2522. * send state as error to avoid skipping V4L2_BUF_FLAG_ERROR
  2523. * flag at v4l2 side.
  2524. */
  2525. if (buf->flags & MSM_VIDC_BUF_FLAG_ERROR)
  2526. state = VB2_BUF_STATE_ERROR;
  2527. else
  2528. state = VB2_BUF_STATE_DONE;
  2529. vbuf = to_vb2_v4l2_buffer(vb2);
  2530. vbuf->flags = buf->flags;
  2531. vb2->timestamp = buf->timestamp;
  2532. vb2->planes[0].bytesused = buf->data_size + vb2->planes[0].data_offset;
  2533. vb2_buffer_done(vb2, state);
  2534. return 0;
  2535. }
  2536. int msm_vidc_v4l2_fh_init(struct msm_vidc_inst *inst)
  2537. {
  2538. int rc = 0;
  2539. int index;
  2540. struct msm_vidc_core *core;
  2541. core = inst->core;
  2542. /* do not init, if already inited */
  2543. if (inst->fh.vdev) {
  2544. i_vpr_e(inst, "%s: already inited\n", __func__);
  2545. return -EINVAL;
  2546. }
  2547. if (is_decode_session(inst))
  2548. index = 0;
  2549. else if (is_encode_session(inst))
  2550. index = 1;
  2551. else
  2552. return -EINVAL;
  2553. v4l2_fh_init(&inst->fh, &core->vdev[index].vdev);
  2554. inst->fh.ctrl_handler = &inst->ctrl_handler;
  2555. v4l2_fh_add(&inst->fh);
  2556. return rc;
  2557. }
  2558. int msm_vidc_v4l2_fh_deinit(struct msm_vidc_inst *inst)
  2559. {
  2560. int rc = 0;
  2561. /* do not deinit, if not already inited */
  2562. if (!inst->fh.vdev) {
  2563. i_vpr_h(inst, "%s: already not inited\n", __func__);
  2564. return 0;
  2565. }
  2566. v4l2_fh_del(&inst->fh);
  2567. inst->fh.ctrl_handler = NULL;
  2568. v4l2_fh_exit(&inst->fh);
  2569. return rc;
  2570. }
  2571. static int vb2q_init(struct msm_vidc_inst *inst,
  2572. struct vb2_queue *q, enum v4l2_buf_type type)
  2573. {
  2574. int rc = 0;
  2575. struct msm_vidc_core *core;
  2576. core = inst->core;
  2577. q->type = type;
  2578. q->io_modes = VB2_MMAP | VB2_DMABUF;
  2579. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  2580. q->ops = core->vb2_ops;
  2581. q->mem_ops = core->vb2_mem_ops;
  2582. q->drv_priv = inst;
  2583. q->allow_zero_bytesused = 1;
  2584. q->copy_timestamp = 1;
  2585. rc = vb2_queue_init(q);
  2586. if (rc)
  2587. i_vpr_e(inst, "%s: vb2_queue_init failed for type %d\n",
  2588. __func__, type);
  2589. return rc;
  2590. }
  2591. static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
  2592. struct vb2_queue *dst_vq)
  2593. {
  2594. int rc = 0;
  2595. struct msm_vidc_inst *inst = priv;
  2596. struct msm_vidc_core *core;
  2597. if (!inst || !inst->core || !src_vq || !dst_vq) {
  2598. d_vpr_e("%s: invalid params\n", __func__);
  2599. return -EINVAL;
  2600. }
  2601. core = inst->core;
  2602. src_vq->supports_requests = core->capabilities[SUPPORTS_REQUESTS].value;
  2603. src_vq->lock = &inst->request_lock;
  2604. src_vq->dev = &core->pdev->dev;
  2605. rc = vb2q_init(inst, src_vq, INPUT_MPLANE);
  2606. if (rc)
  2607. goto fail_input_vb2q_init;
  2608. inst->bufq[INPUT_PORT].vb2q = src_vq;
  2609. dst_vq->lock = src_vq->lock;
  2610. dst_vq->dev = &core->pdev->dev;
  2611. rc = vb2q_init(inst, dst_vq, OUTPUT_MPLANE);
  2612. if (rc)
  2613. goto fail_out_vb2q_init;
  2614. inst->bufq[OUTPUT_PORT].vb2q = dst_vq;
  2615. return rc;
  2616. fail_out_vb2q_init:
  2617. vb2_queue_release(inst->bufq[INPUT_PORT].vb2q);
  2618. fail_input_vb2q_init:
  2619. return rc;
  2620. }
  2621. int msm_vidc_vb2_queue_init(struct msm_vidc_inst *inst)
  2622. {
  2623. int rc = 0;
  2624. struct msm_vidc_core *core;
  2625. core = inst->core;
  2626. if (inst->m2m_dev) {
  2627. i_vpr_e(inst, "%s: vb2q already inited\n", __func__);
  2628. return -EINVAL;
  2629. }
  2630. inst->m2m_dev = v4l2_m2m_init(core->v4l2_m2m_ops);
  2631. if (IS_ERR(inst->m2m_dev)) {
  2632. i_vpr_e(inst, "%s: failed to initialize v4l2 m2m device\n", __func__);
  2633. rc = PTR_ERR(inst->m2m_dev);
  2634. goto fail_m2m_init;
  2635. }
  2636. /* v4l2_m2m_ctx_init will do input & output queues initialization */
  2637. inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
  2638. if (!inst->m2m_ctx) {
  2639. rc = -EINVAL;
  2640. i_vpr_e(inst, "%s: v4l2_m2m_ctx_init failed\n", __func__);
  2641. goto fail_m2m_ctx_init;
  2642. }
  2643. inst->fh.m2m_ctx = inst->m2m_ctx;
  2644. rc = msm_vidc_vmem_alloc(sizeof(struct vb2_queue),
  2645. (void **)&inst->bufq[INPUT_META_PORT].vb2q, "input meta port");
  2646. if (rc)
  2647. goto fail_in_meta_alloc;
  2648. /* do input meta port queues initialization */
  2649. rc = vb2q_init(inst, inst->bufq[INPUT_META_PORT].vb2q, INPUT_META_PLANE);
  2650. if (rc)
  2651. goto fail_in_meta_vb2q_init;
  2652. rc = msm_vidc_vmem_alloc(sizeof(struct vb2_queue),
  2653. (void **)&inst->bufq[OUTPUT_META_PORT].vb2q, "output meta port");
  2654. if (rc)
  2655. goto fail_out_meta_alloc;
  2656. /* do output meta port queues initialization */
  2657. rc = vb2q_init(inst, inst->bufq[OUTPUT_META_PORT].vb2q, OUTPUT_META_PLANE);
  2658. if (rc)
  2659. goto fail_out_meta_vb2q_init;
  2660. return 0;
  2661. fail_out_meta_vb2q_init:
  2662. msm_vidc_vmem_free((void **)&inst->bufq[OUTPUT_META_PORT].vb2q);
  2663. inst->bufq[OUTPUT_META_PORT].vb2q = NULL;
  2664. fail_out_meta_alloc:
  2665. vb2_queue_release(inst->bufq[INPUT_META_PORT].vb2q);
  2666. fail_in_meta_vb2q_init:
  2667. msm_vidc_vmem_free((void **)&inst->bufq[INPUT_META_PORT].vb2q);
  2668. inst->bufq[INPUT_META_PORT].vb2q = NULL;
  2669. fail_in_meta_alloc:
  2670. v4l2_m2m_ctx_release(inst->m2m_ctx);
  2671. inst->m2m_ctx = NULL;
  2672. inst->fh.m2m_ctx = NULL;
  2673. inst->bufq[OUTPUT_PORT].vb2q = NULL;
  2674. inst->bufq[INPUT_PORT].vb2q = NULL;
  2675. fail_m2m_ctx_init:
  2676. v4l2_m2m_release(inst->m2m_dev);
  2677. inst->m2m_dev = NULL;
  2678. fail_m2m_init:
  2679. return rc;
  2680. }
  2681. int msm_vidc_vb2_queue_deinit(struct msm_vidc_inst *inst)
  2682. {
  2683. int rc = 0;
  2684. if (!inst->m2m_dev) {
  2685. i_vpr_h(inst, "%s: vb2q already deinited\n", __func__);
  2686. return 0;
  2687. }
  2688. /*
  2689. * vb2_queue_release() for input and output queues
  2690. * is called from v4l2_m2m_ctx_release()
  2691. */
  2692. v4l2_m2m_ctx_release(inst->m2m_ctx);
  2693. inst->m2m_ctx = NULL;
  2694. inst->bufq[OUTPUT_PORT].vb2q = NULL;
  2695. inst->bufq[INPUT_PORT].vb2q = NULL;
  2696. v4l2_m2m_release(inst->m2m_dev);
  2697. inst->m2m_dev = NULL;
  2698. vb2_queue_release(inst->bufq[OUTPUT_META_PORT].vb2q);
  2699. msm_vidc_vmem_free((void **)&inst->bufq[OUTPUT_META_PORT].vb2q);
  2700. inst->bufq[OUTPUT_META_PORT].vb2q = NULL;
  2701. vb2_queue_release(inst->bufq[INPUT_META_PORT].vb2q);
  2702. msm_vidc_vmem_free((void **)&inst->bufq[INPUT_META_PORT].vb2q);
  2703. inst->bufq[INPUT_META_PORT].vb2q = NULL;
  2704. return rc;
  2705. }
  2706. int msm_vidc_add_session(struct msm_vidc_inst *inst)
  2707. {
  2708. int rc = 0;
  2709. struct msm_vidc_inst *i;
  2710. struct msm_vidc_core *core;
  2711. u32 count = 0;
  2712. core = inst->core;
  2713. core_lock(core, __func__);
  2714. if (core->state != MSM_VIDC_CORE_INIT) {
  2715. i_vpr_e(inst, "%s: invalid state %s\n",
  2716. __func__, core_state_name(core->state));
  2717. rc = -EINVAL;
  2718. goto unlock;
  2719. }
  2720. list_for_each_entry(i, &core->instances, list)
  2721. count++;
  2722. if (count < core->capabilities[MAX_SESSION_COUNT].value) {
  2723. list_add_tail(&inst->list, &core->instances);
  2724. } else {
  2725. i_vpr_e(inst, "%s: max limit %d already running %d sessions\n",
  2726. __func__, core->capabilities[MAX_SESSION_COUNT].value, count);
  2727. rc = -EINVAL;
  2728. }
  2729. unlock:
  2730. core_unlock(core, __func__);
  2731. return rc;
  2732. }
  2733. int msm_vidc_remove_session(struct msm_vidc_inst *inst)
  2734. {
  2735. struct msm_vidc_inst *i, *temp;
  2736. struct msm_vidc_core *core;
  2737. u32 count = 0;
  2738. core = inst->core;
  2739. core_lock(core, __func__);
  2740. list_for_each_entry_safe(i, temp, &core->instances, list) {
  2741. if (i->session_id == inst->session_id) {
  2742. list_move_tail(&i->list, &core->dangling_instances);
  2743. i_vpr_h(inst, "%s: removed session %#x\n",
  2744. __func__, i->session_id);
  2745. }
  2746. }
  2747. list_for_each_entry(i, &core->instances, list)
  2748. count++;
  2749. i_vpr_h(inst, "%s: remaining sessions %d\n", __func__, count);
  2750. core_unlock(core, __func__);
  2751. return 0;
  2752. }
  2753. int msm_vidc_remove_dangling_session(struct msm_vidc_inst *inst)
  2754. {
  2755. struct msm_vidc_inst *i, *temp;
  2756. struct msm_vidc_core *core;
  2757. u32 count = 0, dcount = 0;
  2758. core = inst->core;
  2759. core_lock(core, __func__);
  2760. list_for_each_entry_safe(i, temp, &core->dangling_instances, list) {
  2761. if (i->session_id == inst->session_id) {
  2762. list_del_init(&i->list);
  2763. i_vpr_h(inst, "%s: removed dangling session %#x\n",
  2764. __func__, i->session_id);
  2765. break;
  2766. }
  2767. }
  2768. list_for_each_entry(i, &core->instances, list)
  2769. count++;
  2770. list_for_each_entry(i, &core->dangling_instances, list)
  2771. dcount++;
  2772. i_vpr_h(inst, "%s: remaining sessions. active %d, dangling %d\n",
  2773. __func__, count, dcount);
  2774. core_unlock(core, __func__);
  2775. return 0;
  2776. }
  2777. int msm_vidc_session_open(struct msm_vidc_inst *inst)
  2778. {
  2779. int rc = 0;
  2780. inst->packet_size = 4096;
  2781. rc = msm_vidc_vmem_alloc(inst->packet_size, (void **)&inst->packet, __func__);
  2782. if (rc)
  2783. return rc;
  2784. rc = venus_hfi_session_open(inst);
  2785. if (rc)
  2786. goto error;
  2787. return 0;
  2788. error:
  2789. i_vpr_e(inst, "%s(): session open failed\n", __func__);
  2790. msm_vidc_vmem_free((void **)&inst->packet);
  2791. inst->packet = NULL;
  2792. return rc;
  2793. }
  2794. int msm_vidc_session_set_codec(struct msm_vidc_inst *inst)
  2795. {
  2796. int rc = 0;
  2797. rc = venus_hfi_session_set_codec(inst);
  2798. if (rc)
  2799. return rc;
  2800. return 0;
  2801. }
  2802. int msm_vidc_session_set_secure_mode(struct msm_vidc_inst *inst)
  2803. {
  2804. int rc = 0;
  2805. rc = venus_hfi_session_set_secure_mode(inst);
  2806. if (rc)
  2807. return rc;
  2808. return 0;
  2809. }
  2810. int msm_vidc_session_set_default_header(struct msm_vidc_inst *inst)
  2811. {
  2812. int rc = 0;
  2813. u32 default_header = false;
  2814. default_header = inst->capabilities[DEFAULT_HEADER].value;
  2815. i_vpr_h(inst, "%s: default header: %d", __func__, default_header);
  2816. rc = venus_hfi_session_property(inst,
  2817. HFI_PROP_DEC_DEFAULT_HEADER,
  2818. HFI_HOST_FLAGS_NONE,
  2819. get_hfi_port(inst, INPUT_PORT),
  2820. HFI_PAYLOAD_U32,
  2821. &default_header,
  2822. sizeof(u32));
  2823. if (rc)
  2824. i_vpr_e(inst, "%s: set property failed\n", __func__);
  2825. return rc;
  2826. }
  2827. int msm_vidc_session_streamoff(struct msm_vidc_inst *inst,
  2828. enum msm_vidc_port_type port)
  2829. {
  2830. int rc = 0;
  2831. int count = 0;
  2832. struct msm_vidc_core *core;
  2833. enum signal_session_response signal_type;
  2834. enum msm_vidc_buffer_type buffer_type;
  2835. if (port == INPUT_PORT) {
  2836. signal_type = SIGNAL_CMD_STOP_INPUT;
  2837. buffer_type = MSM_VIDC_BUF_INPUT;
  2838. } else if (port == OUTPUT_PORT) {
  2839. signal_type = SIGNAL_CMD_STOP_OUTPUT;
  2840. buffer_type = MSM_VIDC_BUF_OUTPUT;
  2841. } else {
  2842. i_vpr_e(inst, "%s: invalid port: %d\n", __func__, port);
  2843. return -EINVAL;
  2844. }
  2845. rc = venus_hfi_stop(inst, port);
  2846. if (rc)
  2847. goto error;
  2848. core = inst->core;
  2849. i_vpr_h(inst, "%s: wait on port: %d for time: %d ms\n",
  2850. __func__, port, core->capabilities[HW_RESPONSE_TIMEOUT].value);
  2851. inst_unlock(inst, __func__);
  2852. rc = wait_for_completion_timeout(
  2853. &inst->completions[signal_type],
  2854. msecs_to_jiffies(
  2855. core->capabilities[HW_RESPONSE_TIMEOUT].value));
  2856. if (!rc) {
  2857. i_vpr_e(inst, "%s: session stop timed out for port: %d\n",
  2858. __func__, port);
  2859. rc = -ETIMEDOUT;
  2860. msm_vidc_inst_timeout(inst);
  2861. } else {
  2862. rc = 0;
  2863. }
  2864. inst_lock(inst, __func__);
  2865. if (rc)
  2866. goto error;
  2867. if (port == INPUT_PORT) {
  2868. /* flush input timer list */
  2869. msm_vidc_flush_input_timer(inst);
  2870. }
  2871. /* no more queued buffers after streamoff */
  2872. count = msm_vidc_num_buffers(inst, buffer_type, MSM_VIDC_ATTR_QUEUED);
  2873. if (!count) {
  2874. i_vpr_h(inst, "%s: stop successful on port: %d\n",
  2875. __func__, port);
  2876. } else {
  2877. i_vpr_e(inst,
  2878. "%s: %d buffers pending with firmware on port: %d\n",
  2879. __func__, count, port);
  2880. rc = -EINVAL;
  2881. goto error;
  2882. }
  2883. rc = msm_vidc_state_change_streamoff(inst, port);
  2884. if (rc)
  2885. goto error;
  2886. /* flush deferred buffers */
  2887. msm_vidc_flush_buffers(inst, buffer_type);
  2888. msm_vidc_flush_read_only_buffers(inst, buffer_type);
  2889. return 0;
  2890. error:
  2891. msm_vidc_kill_session(inst);
  2892. msm_vidc_flush_buffers(inst, buffer_type);
  2893. msm_vidc_flush_read_only_buffers(inst, buffer_type);
  2894. return rc;
  2895. }
  2896. int msm_vidc_session_close(struct msm_vidc_inst *inst)
  2897. {
  2898. int rc = 0;
  2899. struct msm_vidc_core *core;
  2900. bool wait_for_response;
  2901. core = inst->core;
  2902. wait_for_response = true;
  2903. rc = venus_hfi_session_close(inst);
  2904. if (rc) {
  2905. i_vpr_e(inst, "%s: session close cmd failed\n", __func__);
  2906. wait_for_response = false;
  2907. }
  2908. /* we are not supposed to send any more commands after close */
  2909. i_vpr_h(inst, "%s: free session packet data\n", __func__);
  2910. msm_vidc_vmem_free((void **)&inst->packet);
  2911. inst->packet = NULL;
  2912. if (wait_for_response) {
  2913. i_vpr_h(inst, "%s: wait on close for time: %d ms\n",
  2914. __func__, core->capabilities[HW_RESPONSE_TIMEOUT].value);
  2915. inst_unlock(inst, __func__);
  2916. rc = wait_for_completion_timeout(
  2917. &inst->completions[SIGNAL_CMD_CLOSE],
  2918. msecs_to_jiffies(
  2919. core->capabilities[HW_RESPONSE_TIMEOUT].value));
  2920. if (!rc) {
  2921. i_vpr_e(inst, "%s: session close timed out\n", __func__);
  2922. rc = -ETIMEDOUT;
  2923. msm_vidc_inst_timeout(inst);
  2924. } else {
  2925. rc = 0;
  2926. i_vpr_h(inst, "%s: close successful\n", __func__);
  2927. }
  2928. inst_lock(inst, __func__);
  2929. }
  2930. return rc;
  2931. }
  2932. int msm_vidc_kill_session(struct msm_vidc_inst *inst)
  2933. {
  2934. if (!inst->session_id) {
  2935. i_vpr_e(inst, "%s: already killed\n", __func__);
  2936. return 0;
  2937. }
  2938. i_vpr_e(inst, "%s: killing session\n", __func__);
  2939. msm_vidc_session_close(inst);
  2940. msm_vidc_change_state(inst, MSM_VIDC_ERROR, __func__);
  2941. return 0;
  2942. }
  2943. int msm_vidc_get_inst_capability(struct msm_vidc_inst *inst)
  2944. {
  2945. int rc = 0;
  2946. int i;
  2947. u32 codecs_count = 0;
  2948. struct msm_vidc_core *core;
  2949. core = inst->core;
  2950. codecs_count = core->enc_codecs_count + core->dec_codecs_count;
  2951. for (i = 0; i < codecs_count; i++) {
  2952. if (core->inst_caps[i].domain == inst->domain &&
  2953. core->inst_caps[i].codec == inst->codec) {
  2954. i_vpr_h(inst,
  2955. "%s: copied capabilities with %#x codec, %#x domain\n",
  2956. __func__, inst->codec, inst->domain);
  2957. memcpy(&inst->capabilities[0], &core->inst_caps[i].cap[0],
  2958. (INST_CAP_MAX + 1) * sizeof(struct msm_vidc_inst_cap));
  2959. }
  2960. }
  2961. return rc;
  2962. }
  2963. int msm_vidc_init_core_caps(struct msm_vidc_core *core)
  2964. {
  2965. int rc = 0;
  2966. int i, num_platform_caps;
  2967. struct msm_platform_core_capability *platform_data;
  2968. if (!core || !core->platform) {
  2969. d_vpr_e("%s: invalid params\n", __func__);
  2970. rc = -EINVAL;
  2971. goto exit;
  2972. }
  2973. platform_data = core->platform->data.core_data;
  2974. if (!platform_data) {
  2975. d_vpr_e("%s: platform core data is NULL\n",
  2976. __func__);
  2977. rc = -EINVAL;
  2978. goto exit;
  2979. }
  2980. num_platform_caps = core->platform->data.core_data_size;
  2981. /* loop over platform caps */
  2982. for (i = 0; i < num_platform_caps && i < CORE_CAP_MAX; i++) {
  2983. core->capabilities[platform_data[i].type].type = platform_data[i].type;
  2984. core->capabilities[platform_data[i].type].value = platform_data[i].value;
  2985. }
  2986. exit:
  2987. return rc;
  2988. }
  2989. static int update_inst_capability(struct msm_platform_inst_capability *in,
  2990. struct msm_vidc_inst_capability *capability)
  2991. {
  2992. if (!in || !capability) {
  2993. d_vpr_e("%s: invalid params %pK %pK\n",
  2994. __func__, in, capability);
  2995. return -EINVAL;
  2996. }
  2997. if (in->cap_id >= INST_CAP_MAX) {
  2998. d_vpr_e("%s: invalid cap id %d\n", __func__, in->cap_id);
  2999. return -EINVAL;
  3000. }
  3001. capability->cap[in->cap_id].cap_id = in->cap_id;
  3002. capability->cap[in->cap_id].min = in->min;
  3003. capability->cap[in->cap_id].max = in->max;
  3004. capability->cap[in->cap_id].step_or_mask = in->step_or_mask;
  3005. capability->cap[in->cap_id].value = in->value;
  3006. capability->cap[in->cap_id].flags = in->flags;
  3007. capability->cap[in->cap_id].v4l2_id = in->v4l2_id;
  3008. capability->cap[in->cap_id].hfi_id = in->hfi_id;
  3009. return 0;
  3010. }
  3011. static int update_inst_cap_dependency(
  3012. struct msm_platform_inst_cap_dependency *in,
  3013. struct msm_vidc_inst_capability *capability)
  3014. {
  3015. if (!in || !capability) {
  3016. d_vpr_e("%s: invalid params %pK %pK\n",
  3017. __func__, in, capability);
  3018. return -EINVAL;
  3019. }
  3020. if (in->cap_id >= INST_CAP_MAX) {
  3021. d_vpr_e("%s: invalid cap id %d\n", __func__, in->cap_id);
  3022. return -EINVAL;
  3023. }
  3024. if (capability->cap[in->cap_id].cap_id != in->cap_id) {
  3025. d_vpr_e("%s: invalid cap id %d\n", __func__, in->cap_id);
  3026. return -EINVAL;
  3027. }
  3028. memcpy(capability->cap[in->cap_id].children, in->children,
  3029. sizeof(capability->cap[in->cap_id].children));
  3030. capability->cap[in->cap_id].adjust = in->adjust;
  3031. capability->cap[in->cap_id].set = in->set;
  3032. return 0;
  3033. }
  3034. int msm_vidc_init_instance_caps(struct msm_vidc_core *core)
  3035. {
  3036. int rc = 0;
  3037. u8 enc_valid_codecs, dec_valid_codecs;
  3038. u8 count_bits, codecs_count = 0;
  3039. u8 enc_codecs_count = 0, dec_codecs_count = 0;
  3040. int i, j, check_bit;
  3041. int num_platform_cap_data, num_platform_cap_dependency_data;
  3042. struct msm_platform_inst_capability *platform_cap_data = NULL;
  3043. struct msm_platform_inst_cap_dependency *platform_cap_dependency_data = NULL;
  3044. if (!core || !core->platform) {
  3045. d_vpr_e("%s: invalid params\n", __func__);
  3046. rc = -EINVAL;
  3047. goto error;
  3048. }
  3049. platform_cap_data = core->platform->data.inst_cap_data;
  3050. if (!platform_cap_data) {
  3051. d_vpr_e("%s: platform instance cap data is NULL\n",
  3052. __func__);
  3053. rc = -EINVAL;
  3054. goto error;
  3055. }
  3056. platform_cap_dependency_data = core->platform->data.inst_cap_dependency_data;
  3057. if (!platform_cap_dependency_data) {
  3058. d_vpr_e("%s: platform instance cap dependency data is NULL\n",
  3059. __func__);
  3060. rc = -EINVAL;
  3061. goto error;
  3062. }
  3063. enc_valid_codecs = core->capabilities[ENC_CODECS].value;
  3064. count_bits = enc_valid_codecs;
  3065. COUNT_BITS(count_bits, enc_codecs_count);
  3066. core->enc_codecs_count = enc_codecs_count;
  3067. dec_valid_codecs = core->capabilities[DEC_CODECS].value;
  3068. count_bits = dec_valid_codecs;
  3069. COUNT_BITS(count_bits, dec_codecs_count);
  3070. core->dec_codecs_count = dec_codecs_count;
  3071. codecs_count = enc_codecs_count + dec_codecs_count;
  3072. core->inst_caps = devm_kzalloc(&core->pdev->dev,
  3073. codecs_count * sizeof(struct msm_vidc_inst_capability), GFP_KERNEL);
  3074. if (!core->inst_caps) {
  3075. d_vpr_e("%s: failed to alloc memory for instance caps\n", __func__);
  3076. rc = -ENOMEM;
  3077. goto error;
  3078. }
  3079. check_bit = 0;
  3080. /* determine codecs for enc domain */
  3081. for (i = 0; i < enc_codecs_count; i++) {
  3082. while (check_bit < (sizeof(enc_valid_codecs) * 8)) {
  3083. if (enc_valid_codecs & BIT(check_bit)) {
  3084. core->inst_caps[i].domain = MSM_VIDC_ENCODER;
  3085. core->inst_caps[i].codec = enc_valid_codecs &
  3086. BIT(check_bit);
  3087. check_bit++;
  3088. break;
  3089. }
  3090. check_bit++;
  3091. }
  3092. }
  3093. /* reset checkbit to check from 0th bit of decoder codecs set bits*/
  3094. check_bit = 0;
  3095. /* determine codecs for dec domain */
  3096. for (; i < codecs_count; i++) {
  3097. while (check_bit < (sizeof(dec_valid_codecs) * 8)) {
  3098. if (dec_valid_codecs & BIT(check_bit)) {
  3099. core->inst_caps[i].domain = MSM_VIDC_DECODER;
  3100. core->inst_caps[i].codec = dec_valid_codecs &
  3101. BIT(check_bit);
  3102. check_bit++;
  3103. break;
  3104. }
  3105. check_bit++;
  3106. }
  3107. }
  3108. num_platform_cap_data = core->platform->data.inst_cap_data_size;
  3109. num_platform_cap_dependency_data = core->platform->data.inst_cap_dependency_data_size;
  3110. d_vpr_h("%s: num caps %d, dependency %d\n", __func__,
  3111. num_platform_cap_data, num_platform_cap_dependency_data);
  3112. /* loop over each platform capability */
  3113. for (i = 0; i < num_platform_cap_data; i++) {
  3114. /* select matching core codec and update it */
  3115. for (j = 0; j < codecs_count; j++) {
  3116. if ((platform_cap_data[i].domain &
  3117. core->inst_caps[j].domain) &&
  3118. (platform_cap_data[i].codec &
  3119. core->inst_caps[j].codec)) {
  3120. /* update core capability */
  3121. rc = update_inst_capability(&platform_cap_data[i],
  3122. &core->inst_caps[j]);
  3123. if (rc)
  3124. return rc;
  3125. }
  3126. }
  3127. }
  3128. /* loop over each platform dependency capability */
  3129. for (i = 0; i < num_platform_cap_dependency_data; i++) {
  3130. /* select matching core codec and update it */
  3131. for (j = 0; j < codecs_count; j++) {
  3132. if ((platform_cap_dependency_data[i].domain &
  3133. core->inst_caps[j].domain) &&
  3134. (platform_cap_dependency_data[i].codec &
  3135. core->inst_caps[j].codec)) {
  3136. /* update core dependency capability */
  3137. rc = update_inst_cap_dependency(
  3138. &platform_cap_dependency_data[i],
  3139. &core->inst_caps[j]);
  3140. if (rc)
  3141. return rc;
  3142. }
  3143. }
  3144. }
  3145. error:
  3146. return rc;
  3147. }
  3148. int msm_vidc_core_deinit_locked(struct msm_vidc_core *core, bool force)
  3149. {
  3150. int rc = 0;
  3151. struct msm_vidc_inst *inst, *dummy;
  3152. enum msm_vidc_allow allow;
  3153. if (!core) {
  3154. d_vpr_e("%s: invalid params\n", __func__);
  3155. return -EINVAL;
  3156. }
  3157. rc = __strict_check(core, __func__);
  3158. if (rc) {
  3159. d_vpr_e("%s(): core was not locked\n", __func__);
  3160. return rc;
  3161. }
  3162. if (is_core_state(core, MSM_VIDC_CORE_DEINIT))
  3163. return 0;
  3164. /* print error for state change not allowed case */
  3165. allow = msm_vidc_allow_core_state_change(core, MSM_VIDC_CORE_DEINIT);
  3166. if (allow != MSM_VIDC_ALLOW)
  3167. d_vpr_e("%s: %s core state change %s -> %s\n", __func__,
  3168. allow_name(allow), core_state_name(core->state),
  3169. core_state_name(MSM_VIDC_CORE_DEINIT));
  3170. if (force) {
  3171. d_vpr_e("%s(): force deinit core\n", __func__);
  3172. } else {
  3173. /* in normal case, deinit core only if no session present */
  3174. if (!list_empty(&core->instances)) {
  3175. d_vpr_h("%s(): skip deinit\n", __func__);
  3176. return 0;
  3177. } else {
  3178. d_vpr_h("%s(): deinit core\n", __func__);
  3179. }
  3180. }
  3181. venus_hfi_core_deinit(core, force);
  3182. /* unlink all sessions from core, if any */
  3183. list_for_each_entry_safe(inst, dummy, &core->instances, list) {
  3184. msm_vidc_change_state(inst, MSM_VIDC_ERROR, __func__);
  3185. list_move_tail(&inst->list, &core->dangling_instances);
  3186. }
  3187. msm_vidc_change_core_state(core, MSM_VIDC_CORE_DEINIT, __func__);
  3188. return rc;
  3189. }
  3190. int msm_vidc_core_deinit(struct msm_vidc_core *core, bool force)
  3191. {
  3192. int rc = 0;
  3193. if (!core) {
  3194. d_vpr_e("%s: invalid params\n", __func__);
  3195. return -EINVAL;
  3196. }
  3197. core_lock(core, __func__);
  3198. rc = msm_vidc_core_deinit_locked(core, force);
  3199. core_unlock(core, __func__);
  3200. return rc;
  3201. }
  3202. int msm_vidc_core_init_wait(struct msm_vidc_core *core)
  3203. {
  3204. const int interval = 10;
  3205. int max_tries, count = 0, rc = 0;
  3206. if (!core) {
  3207. d_vpr_e("%s: invalid params\n", __func__);
  3208. return -EINVAL;
  3209. }
  3210. core_lock(core, __func__);
  3211. if (is_core_state(core, MSM_VIDC_CORE_INIT)) {
  3212. rc = 0;
  3213. goto unlock;
  3214. } else if (is_core_state(core, MSM_VIDC_CORE_DEINIT) ||
  3215. is_core_state(core, MSM_VIDC_CORE_ERROR)) {
  3216. d_vpr_e("%s: invalid core state %s\n",
  3217. __func__, core_state_name(core->state));
  3218. rc = -EINVAL;
  3219. goto unlock;
  3220. }
  3221. d_vpr_h("%s(): waiting for state change\n", __func__);
  3222. max_tries = core->capabilities[HW_RESPONSE_TIMEOUT].value / interval;
  3223. while (count < max_tries) {
  3224. if (core->state != MSM_VIDC_CORE_INIT_WAIT)
  3225. break;
  3226. core_unlock(core, __func__);
  3227. msleep_interruptible(interval);
  3228. core_lock(core, __func__);
  3229. count++;
  3230. }
  3231. d_vpr_h("%s: state %s, interval %u, count %u, max_tries %u\n", __func__,
  3232. core_state_name(core->state), interval, count, max_tries);
  3233. if (is_core_state(core, MSM_VIDC_CORE_INIT)) {
  3234. d_vpr_h("%s: sys init successful\n", __func__);
  3235. rc = 0;
  3236. goto unlock;
  3237. } else if (is_core_state(core, MSM_VIDC_CORE_INIT_WAIT)) {
  3238. d_vpr_h("%s: sys init wait timedout. state %s\n",
  3239. __func__, core_state_name(core->state));
  3240. msm_vidc_change_core_state(core, MSM_VIDC_CORE_ERROR, __func__);
  3241. /* mark video hw unresponsive */
  3242. msm_vidc_change_core_sub_state(core,
  3243. 0, CORE_SUBSTATE_VIDEO_UNRESPONSIVE, __func__);
  3244. /* core deinit to handle error */
  3245. msm_vidc_core_deinit_locked(core, true);
  3246. rc = -EINVAL;
  3247. goto unlock;
  3248. } else {
  3249. d_vpr_e("%s: invalid core state %s\n",
  3250. __func__, core_state_name(core->state));
  3251. rc = -EINVAL;
  3252. goto unlock;
  3253. }
  3254. unlock:
  3255. core_unlock(core, __func__);
  3256. return rc;
  3257. }
  3258. int msm_vidc_core_init(struct msm_vidc_core *core)
  3259. {
  3260. enum msm_vidc_allow allow;
  3261. int rc = 0;
  3262. if (!core) {
  3263. d_vpr_e("%s: invalid params\n", __func__);
  3264. return -EINVAL;
  3265. }
  3266. core_lock(core, __func__);
  3267. if (core_in_valid_state(core)) {
  3268. goto unlock;
  3269. } else if (is_core_state(core, MSM_VIDC_CORE_ERROR)) {
  3270. d_vpr_e("%s: invalid core state %s\n",
  3271. __func__, core_state_name(core->state));
  3272. rc = -EINVAL;
  3273. goto unlock;
  3274. }
  3275. /* print error for state change not allowed case */
  3276. allow = msm_vidc_allow_core_state_change(core, MSM_VIDC_CORE_INIT_WAIT);
  3277. if (allow != MSM_VIDC_ALLOW)
  3278. d_vpr_e("%s: %s core state change %s -> %s\n", __func__,
  3279. allow_name(allow), core_state_name(core->state),
  3280. core_state_name(MSM_VIDC_CORE_INIT_WAIT));
  3281. msm_vidc_change_core_state(core, MSM_VIDC_CORE_INIT_WAIT, __func__);
  3282. /* clear PM suspend from core sub_state */
  3283. msm_vidc_change_core_sub_state(core, CORE_SUBSTATE_PM_SUSPEND, 0, __func__);
  3284. msm_vidc_change_core_sub_state(core, CORE_SUBSTATE_PAGE_FAULT, 0, __func__);
  3285. rc = venus_hfi_core_init(core);
  3286. if (rc) {
  3287. msm_vidc_change_core_state(core, MSM_VIDC_CORE_ERROR, __func__);
  3288. d_vpr_e("%s: core init failed\n", __func__);
  3289. /* do core deinit to handle error */
  3290. msm_vidc_core_deinit_locked(core, true);
  3291. goto unlock;
  3292. }
  3293. unlock:
  3294. core_unlock(core, __func__);
  3295. return rc;
  3296. }
  3297. int msm_vidc_print_residency_stats(struct msm_vidc_core *core)
  3298. {
  3299. int rc = 0;
  3300. core_lock(core, __func__);
  3301. rc = call_res_op(core, clk_print_residency_stats, core);
  3302. if (rc)
  3303. goto unlock;
  3304. unlock:
  3305. core_unlock(core, __func__);
  3306. return rc;
  3307. }
  3308. int msm_vidc_reset_residency_stats(struct msm_vidc_core *core)
  3309. {
  3310. int rc = 0;
  3311. core_lock(core, __func__);
  3312. rc = call_res_op(core, clk_reset_residency_stats, core);
  3313. if (rc)
  3314. goto unlock;
  3315. unlock:
  3316. core_unlock(core, __func__);
  3317. return rc;
  3318. }
  3319. int msm_vidc_inst_timeout(struct msm_vidc_inst *inst)
  3320. {
  3321. int rc = 0;
  3322. struct msm_vidc_core *core;
  3323. struct msm_vidc_inst *instance;
  3324. bool found;
  3325. core = inst->core;
  3326. core_lock(core, __func__);
  3327. /*
  3328. * All sessions will be removed from core list in core deinit,
  3329. * do not deinit core from a session which is not present in
  3330. * core list.
  3331. */
  3332. found = false;
  3333. list_for_each_entry(instance, &core->instances, list) {
  3334. if (instance == inst) {
  3335. found = true;
  3336. break;
  3337. }
  3338. }
  3339. if (!found) {
  3340. i_vpr_e(inst,
  3341. "%s: session not available in core list\n", __func__);
  3342. rc = -EINVAL;
  3343. goto unlock;
  3344. }
  3345. /* mark video hw unresponsive */
  3346. msm_vidc_change_core_state(core, MSM_VIDC_CORE_ERROR, __func__);
  3347. msm_vidc_change_core_sub_state(core,
  3348. 0, CORE_SUBSTATE_VIDEO_UNRESPONSIVE, __func__);
  3349. /* call core deinit for a valid instance timeout case */
  3350. msm_vidc_core_deinit_locked(core, true);
  3351. unlock:
  3352. core_unlock(core, __func__);
  3353. return rc;
  3354. }
  3355. int msm_vidc_print_buffer_info(struct msm_vidc_inst *inst)
  3356. {
  3357. struct msm_vidc_buffers *buffers;
  3358. int i;
  3359. /* Print buffer details */
  3360. for (i = 1; i < ARRAY_SIZE(buf_type_name_arr); i++) {
  3361. buffers = msm_vidc_get_buffers(inst, i, __func__);
  3362. if (!buffers)
  3363. continue;
  3364. i_vpr_h(inst, "buf: type: %15s, min %2d, extra %2d, actual %2d, size %9u, reuse %d\n",
  3365. buf_name(i), buffers->min_count,
  3366. buffers->extra_count, buffers->actual_count,
  3367. buffers->size, buffers->reuse);
  3368. }
  3369. return 0;
  3370. }
  3371. int msm_vidc_print_inst_info(struct msm_vidc_inst *inst)
  3372. {
  3373. struct msm_vidc_buffers *buffers;
  3374. struct msm_vidc_buffer *buf;
  3375. enum msm_vidc_port_type port;
  3376. bool is_secure, is_decode;
  3377. u32 bit_depth, bit_rate, frame_rate, width, height;
  3378. struct dma_buf *dbuf;
  3379. struct inode *f_inode;
  3380. unsigned long inode_num = 0;
  3381. long ref_count = -1;
  3382. int i = 0;
  3383. is_secure = is_secure_session(inst);
  3384. is_decode = inst->domain == MSM_VIDC_DECODER;
  3385. port = is_decode ? INPUT_PORT : OUTPUT_PORT;
  3386. width = inst->fmts[port].fmt.pix_mp.width;
  3387. height = inst->fmts[port].fmt.pix_mp.height;
  3388. bit_depth = inst->capabilities[BIT_DEPTH].value & 0xFFFF;
  3389. bit_rate = inst->capabilities[BIT_RATE].value;
  3390. frame_rate = inst->capabilities[FRAME_RATE].value >> 16;
  3391. i_vpr_e(inst, "%s %s session, HxW: %d x %d, fps: %d, bitrate: %d, bit-depth: %d\n",
  3392. is_secure ? "Secure" : "Non-Secure",
  3393. is_decode ? "Decode" : "Encode",
  3394. height, width,
  3395. frame_rate, bit_rate, bit_depth);
  3396. /* Print buffer details */
  3397. for (i = 1; i < ARRAY_SIZE(buf_type_name_arr); i++) {
  3398. buffers = msm_vidc_get_buffers(inst, i, __func__);
  3399. if (!buffers)
  3400. continue;
  3401. i_vpr_e(inst, "count: type: %11s, min: %2d, extra: %2d, actual: %2d\n",
  3402. buf_name(i), buffers->min_count,
  3403. buffers->extra_count, buffers->actual_count);
  3404. list_for_each_entry(buf, &buffers->list, list) {
  3405. if (!buf->dmabuf)
  3406. continue;
  3407. dbuf = (struct dma_buf *)buf->dmabuf;
  3408. if (dbuf && dbuf->file) {
  3409. f_inode = file_inode(dbuf->file);
  3410. if (f_inode) {
  3411. inode_num = f_inode->i_ino;
  3412. ref_count = file_count(dbuf->file);
  3413. }
  3414. }
  3415. i_vpr_e(inst,
  3416. "buf: type: %11s, index: %2d, fd: %4d, size: %9u, off: %8u, filled: %9u, daddr: %#llx, inode: %8lu, ref: %2ld, flags: %8x, ts: %16lld, attr: %8x\n",
  3417. buf_name(i), buf->index, buf->fd, buf->buffer_size,
  3418. buf->data_offset, buf->data_size, buf->device_addr,
  3419. inode_num, ref_count, buf->flags, buf->timestamp, buf->attr);
  3420. }
  3421. }
  3422. return 0;
  3423. }
  3424. void msm_vidc_print_core_info(struct msm_vidc_core *core)
  3425. {
  3426. struct msm_vidc_inst *inst = NULL;
  3427. struct msm_vidc_inst *instances[MAX_SUPPORTED_INSTANCES];
  3428. s32 num_instances = 0;
  3429. if (!core) {
  3430. d_vpr_e("%s: invalid params\n", __func__);
  3431. return;
  3432. }
  3433. core_lock(core, __func__);
  3434. list_for_each_entry(inst, &core->instances, list)
  3435. instances[num_instances++] = inst;
  3436. core_unlock(core, __func__);
  3437. while (num_instances--) {
  3438. inst = instances[num_instances];
  3439. inst = get_inst_ref(core, inst);
  3440. if (!inst)
  3441. continue;
  3442. inst_lock(inst, __func__);
  3443. msm_vidc_print_inst_info(inst);
  3444. inst_unlock(inst, __func__);
  3445. put_inst(inst);
  3446. }
  3447. }
  3448. int msm_vidc_smmu_fault_handler(struct iommu_domain *domain,
  3449. struct device *dev, unsigned long iova, int flags, void *data)
  3450. {
  3451. struct msm_vidc_core *core = data;
  3452. if (!domain || !core) {
  3453. d_vpr_e("%s: invalid params %pK %pK\n",
  3454. __func__, domain, core);
  3455. return -EINVAL;
  3456. }
  3457. if (is_core_sub_state(core, CORE_SUBSTATE_PAGE_FAULT)) {
  3458. if (core->capabilities[NON_FATAL_FAULTS].value) {
  3459. dprintk_ratelimit(VIDC_ERR, "err ",
  3460. "%s: non-fatal pagefault address: %lx\n",
  3461. __func__, iova);
  3462. return 0;
  3463. }
  3464. }
  3465. d_vpr_e(FMT_STRING_FAULT_HANDLER, __func__, iova);
  3466. /* mark smmu fault as handled */
  3467. core_lock(core, __func__);
  3468. msm_vidc_change_core_sub_state(core, 0, CORE_SUBSTATE_PAGE_FAULT, __func__);
  3469. core_unlock(core, __func__);
  3470. msm_vidc_print_core_info(core);
  3471. /*
  3472. * Return -ENOSYS to elicit the default behaviour of smmu driver.
  3473. * If we return -ENOSYS, then smmu driver assumes page fault handler
  3474. * is not installed and prints a list of useful debug information like
  3475. * FAR, SID etc. This information is not printed if we return 0.
  3476. */
  3477. return -ENOSYS;
  3478. }
  3479. int msm_vidc_trigger_ssr(struct msm_vidc_core *core,
  3480. u64 trigger_ssr_val)
  3481. {
  3482. struct msm_vidc_ssr *ssr;
  3483. if (!core) {
  3484. d_vpr_e("%s: Invalid parameters\n", __func__);
  3485. return -EINVAL;
  3486. }
  3487. ssr = &core->ssr;
  3488. /*
  3489. * <test_addr><sub_client_id><ssr_type>
  3490. * ssr_type: 0-3 bits
  3491. * sub_client_id: 4-7 bits
  3492. * reserved: 8-31 bits
  3493. * test_addr: 32-63 bits
  3494. */
  3495. d_vpr_e("%s: trigger ssr is called. trigger ssr val: %#llx\n",
  3496. __func__, trigger_ssr_val);
  3497. ssr->ssr_type = (trigger_ssr_val &
  3498. (unsigned long)SSR_TYPE) >> SSR_TYPE_SHIFT;
  3499. ssr->sub_client_id = (trigger_ssr_val &
  3500. (unsigned long)SSR_SUB_CLIENT_ID) >> SSR_SUB_CLIENT_ID_SHIFT;
  3501. ssr->test_addr = (trigger_ssr_val &
  3502. (unsigned long)SSR_ADDR_ID) >> SSR_ADDR_SHIFT;
  3503. schedule_work(&core->ssr_work);
  3504. return 0;
  3505. }
  3506. void msm_vidc_ssr_handler(struct work_struct *work)
  3507. {
  3508. int rc;
  3509. struct msm_vidc_core *core;
  3510. struct msm_vidc_ssr *ssr;
  3511. core = container_of(work, struct msm_vidc_core, ssr_work);
  3512. if (!core) {
  3513. d_vpr_e("%s: invalid params %pK\n", __func__, core);
  3514. return;
  3515. }
  3516. ssr = &core->ssr;
  3517. d_vpr_e("%s: ssr handler is called, core state: %s\n",
  3518. __func__, core_state_name(core->state));
  3519. core_lock(core, __func__);
  3520. if (is_core_state(core, MSM_VIDC_CORE_INIT)) {
  3521. /*
  3522. * In current implementation, user-initiated SSR triggers
  3523. * a fatal error from hardware. However, there is no way
  3524. * to know if fatal error is due to SSR or not. Handle
  3525. * user SSR as non-fatal.
  3526. */
  3527. rc = venus_hfi_trigger_ssr(core, ssr->ssr_type,
  3528. ssr->sub_client_id, ssr->test_addr);
  3529. if (rc)
  3530. d_vpr_e("%s: trigger_ssr failed\n", __func__);
  3531. } else {
  3532. d_vpr_e("%s: video core not initialized\n", __func__);
  3533. }
  3534. core_unlock(core, __func__);
  3535. }
  3536. int msm_vidc_trigger_stability(struct msm_vidc_core *core,
  3537. u64 trigger_stability_val)
  3538. {
  3539. struct msm_vidc_inst *inst = NULL;
  3540. struct msm_vidc_stability stability;
  3541. if (!core) {
  3542. d_vpr_e("%s: invalid params\n", __func__);
  3543. return -EINVAL;
  3544. }
  3545. /*
  3546. * <payload><sub_client_id><stability_type>
  3547. * stability_type: 0-3 bits
  3548. * sub_client_id: 4-7 bits
  3549. * reserved: 8-31 bits
  3550. * payload: 32-63 bits
  3551. */
  3552. memset(&stability, 0, sizeof(struct msm_vidc_stability));
  3553. stability.stability_type = (trigger_stability_val &
  3554. (unsigned long)STABILITY_TYPE) >> STABILITY_TYPE_SHIFT;
  3555. stability.sub_client_id = (trigger_stability_val &
  3556. (unsigned long)STABILITY_SUB_CLIENT_ID) >> STABILITY_SUB_CLIENT_ID_SHIFT;
  3557. stability.value = (trigger_stability_val &
  3558. (unsigned long)STABILITY_PAYLOAD_ID) >> STABILITY_PAYLOAD_SHIFT;
  3559. core_lock(core, __func__);
  3560. list_for_each_entry(inst, &core->instances, list) {
  3561. memcpy(&inst->stability, &stability, sizeof(struct msm_vidc_stability));
  3562. schedule_work(&inst->stability_work);
  3563. }
  3564. core_unlock(core, __func__);
  3565. return 0;
  3566. }
  3567. void msm_vidc_stability_handler(struct work_struct *work)
  3568. {
  3569. int rc;
  3570. struct msm_vidc_inst *inst;
  3571. struct msm_vidc_stability *stability;
  3572. inst = container_of(work, struct msm_vidc_inst, stability_work);
  3573. inst = get_inst_ref(g_core, inst);
  3574. if (!inst) {
  3575. d_vpr_e("%s: invalid params\n", __func__);
  3576. return;
  3577. }
  3578. inst_lock(inst, __func__);
  3579. stability = &inst->stability;
  3580. rc = venus_hfi_trigger_stability(inst, stability->stability_type,
  3581. stability->sub_client_id, stability->value);
  3582. if (rc)
  3583. i_vpr_e(inst, "%s: trigger_stability failed\n", __func__);
  3584. inst_unlock(inst, __func__);
  3585. put_inst(inst);
  3586. }
  3587. int cancel_stability_work_sync(struct msm_vidc_inst *inst)
  3588. {
  3589. if (!inst) {
  3590. d_vpr_e("%s: Invalid arguments\n", __func__);
  3591. return -EINVAL;
  3592. }
  3593. cancel_work_sync(&inst->stability_work);
  3594. return 0;
  3595. }
  3596. void msm_vidc_fw_unload_handler(struct work_struct *work)
  3597. {
  3598. struct msm_vidc_core *core = NULL;
  3599. int rc = 0;
  3600. core = container_of(work, struct msm_vidc_core, fw_unload_work.work);
  3601. if (!core) {
  3602. d_vpr_e("%s: invalid work or core handle\n", __func__);
  3603. return;
  3604. }
  3605. d_vpr_h("%s: deinitializing video core\n", __func__);
  3606. rc = msm_vidc_core_deinit(core, false);
  3607. if (rc)
  3608. d_vpr_e("%s: Failed to deinit core\n", __func__);
  3609. }
  3610. void msm_vidc_batch_handler(struct work_struct *work)
  3611. {
  3612. struct msm_vidc_inst *inst;
  3613. struct msm_vidc_core *core;
  3614. int rc = 0;
  3615. inst = container_of(work, struct msm_vidc_inst, decode_batch.work.work);
  3616. inst = get_inst_ref(g_core, inst);
  3617. if (!inst || !inst->core) {
  3618. d_vpr_e("%s: invalid params\n", __func__);
  3619. return;
  3620. }
  3621. core = inst->core;
  3622. inst_lock(inst, __func__);
  3623. if (is_session_error(inst)) {
  3624. i_vpr_e(inst, "%s: failled. Session error\n", __func__);
  3625. goto exit;
  3626. }
  3627. if (is_core_sub_state(core, CORE_SUBSTATE_PM_SUSPEND)) {
  3628. i_vpr_h(inst, "%s: device in pm suspend state\n", __func__);
  3629. goto exit;
  3630. }
  3631. if (is_state(inst, MSM_VIDC_OPEN) ||
  3632. is_state(inst, MSM_VIDC_INPUT_STREAMING)) {
  3633. i_vpr_e(inst, "%s: not allowed in state: %s\n", __func__,
  3634. state_name(inst->state));
  3635. goto exit;
  3636. }
  3637. i_vpr_h(inst, "%s: queue pending batch buffers\n", __func__);
  3638. rc = msm_vidc_queue_deferred_buffers(inst, MSM_VIDC_BUF_OUTPUT);
  3639. if (rc) {
  3640. i_vpr_e(inst, "%s: batch qbufs failed\n", __func__);
  3641. msm_vidc_change_state(inst, MSM_VIDC_ERROR, __func__);
  3642. }
  3643. exit:
  3644. inst_unlock(inst, __func__);
  3645. put_inst(inst);
  3646. }
  3647. int msm_vidc_flush_buffers(struct msm_vidc_inst *inst,
  3648. enum msm_vidc_buffer_type type)
  3649. {
  3650. int rc = 0;
  3651. struct msm_vidc_core *core;
  3652. struct msm_vidc_buffers *buffers;
  3653. struct msm_vidc_buffer *buf, *dummy;
  3654. enum msm_vidc_buffer_type buffer_type[2];
  3655. int i;
  3656. core = inst->core;
  3657. if (type == MSM_VIDC_BUF_INPUT) {
  3658. buffer_type[0] = MSM_VIDC_BUF_INPUT_META;
  3659. buffer_type[1] = MSM_VIDC_BUF_INPUT;
  3660. } else if (type == MSM_VIDC_BUF_OUTPUT) {
  3661. buffer_type[0] = MSM_VIDC_BUF_OUTPUT_META;
  3662. buffer_type[1] = MSM_VIDC_BUF_OUTPUT;
  3663. } else {
  3664. i_vpr_h(inst, "%s: invalid buffer type %d\n",
  3665. __func__, type);
  3666. return -EINVAL;
  3667. }
  3668. for (i = 0; i < ARRAY_SIZE(buffer_type); i++) {
  3669. buffers = msm_vidc_get_buffers(inst, buffer_type[i], __func__);
  3670. if (!buffers)
  3671. return -EINVAL;
  3672. list_for_each_entry_safe(buf, dummy, &buffers->list, list) {
  3673. if (buf->attr & MSM_VIDC_ATTR_QUEUED ||
  3674. buf->attr & MSM_VIDC_ATTR_DEFERRED) {
  3675. print_vidc_buffer(VIDC_HIGH, "high", "flushing buffer", inst, buf);
  3676. if (!(buf->attr & MSM_VIDC_ATTR_BUFFER_DONE)) {
  3677. if (is_decode_session(inst) && is_output_buffer(buf->type)) {
  3678. if (buf->dbuf_get) {
  3679. call_mem_op(core, dma_buf_put, inst, buf->dmabuf);
  3680. buf->dbuf_get = 0;
  3681. }
  3682. }
  3683. buf->data_size = 0;
  3684. msm_vidc_vb2_buffer_done(inst, buf);
  3685. }
  3686. }
  3687. }
  3688. }
  3689. return rc;
  3690. }
  3691. int msm_vidc_flush_read_only_buffers(struct msm_vidc_inst *inst,
  3692. enum msm_vidc_buffer_type type)
  3693. {
  3694. int rc = 0;
  3695. struct msm_vidc_buffer *ro_buf, *dummy;
  3696. struct msm_vidc_core *core;
  3697. core = inst->core;
  3698. if (!is_decode_session(inst) || !is_output_buffer(type))
  3699. return 0;
  3700. list_for_each_entry_safe(ro_buf, dummy, &inst->buffers.read_only.list, list) {
  3701. if (ro_buf->attr & MSM_VIDC_ATTR_READ_ONLY)
  3702. continue;
  3703. print_vidc_buffer(VIDC_ERR, "high", "flush ro buf", inst, ro_buf);
  3704. if (ro_buf->attach && ro_buf->sg_table)
  3705. call_mem_op(core, dma_buf_unmap_attachment, core,
  3706. ro_buf->attach, ro_buf->sg_table);
  3707. if (ro_buf->attach && ro_buf->dmabuf)
  3708. call_mem_op(core, dma_buf_detach, core,
  3709. ro_buf->dmabuf, ro_buf->attach);
  3710. if (ro_buf->dbuf_get)
  3711. call_mem_op(core, dma_buf_put, inst, ro_buf->dmabuf);
  3712. ro_buf->attach = NULL;
  3713. ro_buf->sg_table = NULL;
  3714. ro_buf->dmabuf = NULL;
  3715. ro_buf->dbuf_get = 0;
  3716. ro_buf->device_addr = 0x0;
  3717. list_del_init(&ro_buf->list);
  3718. msm_vidc_pool_free(inst, ro_buf);
  3719. }
  3720. return rc;
  3721. }
  3722. void msm_vidc_destroy_buffers(struct msm_vidc_inst *inst)
  3723. {
  3724. struct msm_vidc_buffers *buffers;
  3725. struct msm_vidc_buffer *buf, *dummy;
  3726. struct msm_vidc_timestamp *ts, *dummy_ts;
  3727. struct msm_memory_dmabuf *dbuf, *dummy_dbuf;
  3728. struct msm_vidc_input_timer *timer, *dummy_timer;
  3729. struct msm_vidc_buffer_stats *stats, *dummy_stats;
  3730. struct msm_vidc_inst_cap_entry *entry, *dummy_entry;
  3731. struct msm_vidc_input_cr_data *cr, *dummy_cr;
  3732. struct msm_vidc_fence *fence, *dummy_fence;
  3733. struct msm_vidc_core *core;
  3734. static const enum msm_vidc_buffer_type ext_buf_types[] = {
  3735. MSM_VIDC_BUF_INPUT,
  3736. MSM_VIDC_BUF_OUTPUT,
  3737. MSM_VIDC_BUF_INPUT_META,
  3738. MSM_VIDC_BUF_OUTPUT_META,
  3739. };
  3740. static const enum msm_vidc_buffer_type internal_buf_types[] = {
  3741. MSM_VIDC_BUF_BIN,
  3742. MSM_VIDC_BUF_ARP,
  3743. MSM_VIDC_BUF_COMV,
  3744. MSM_VIDC_BUF_NON_COMV,
  3745. MSM_VIDC_BUF_LINE,
  3746. MSM_VIDC_BUF_DPB,
  3747. MSM_VIDC_BUF_PERSIST,
  3748. MSM_VIDC_BUF_VPSS,
  3749. MSM_VIDC_BUF_PARTIAL_DATA,
  3750. };
  3751. int i;
  3752. core = inst->core;
  3753. for (i = 0; i < ARRAY_SIZE(internal_buf_types); i++) {
  3754. buffers = msm_vidc_get_buffers(inst, internal_buf_types[i], __func__);
  3755. if (!buffers)
  3756. continue;
  3757. list_for_each_entry_safe(buf, dummy, &buffers->list, list) {
  3758. i_vpr_h(inst,
  3759. "destroying internal buffer: type %d idx %d fd %d addr %#llx size %d\n",
  3760. buf->type, buf->index, buf->fd, buf->device_addr, buf->buffer_size);
  3761. msm_vidc_destroy_internal_buffer(inst, buf);
  3762. }
  3763. }
  3764. /*
  3765. * read_only list does not take dma ref_count using dma_buf_get().
  3766. * dma_buf ptr will be obselete when its ref_count reaches zero.
  3767. * Hence printthe dma_buf info before releasing the ref count.
  3768. */
  3769. list_for_each_entry_safe(buf, dummy, &inst->buffers.read_only.list, list) {
  3770. print_vidc_buffer(VIDC_ERR, "err ", "destroying ro buf", inst, buf);
  3771. if (buf->attach && buf->sg_table)
  3772. call_mem_op(core, dma_buf_unmap_attachment, core,
  3773. buf->attach, buf->sg_table);
  3774. if (buf->attach && buf->dmabuf)
  3775. call_mem_op(core, dma_buf_detach, core, buf->dmabuf, buf->attach);
  3776. if (buf->dbuf_get)
  3777. call_mem_op(core, dma_buf_put, inst, buf->dmabuf);
  3778. list_del_init(&buf->list);
  3779. msm_vidc_pool_free(inst, buf);
  3780. }
  3781. for (i = 0; i < ARRAY_SIZE(ext_buf_types); i++) {
  3782. buffers = msm_vidc_get_buffers(inst, ext_buf_types[i], __func__);
  3783. if (!buffers)
  3784. continue;
  3785. list_for_each_entry_safe(buf, dummy, &buffers->list, list) {
  3786. if (buf->attach && buf->sg_table)
  3787. call_mem_op(core, dma_buf_unmap_attachment, core,
  3788. buf->attach, buf->sg_table);
  3789. if (buf->attach && buf->dmabuf)
  3790. call_mem_op(core, dma_buf_detach, core, buf->dmabuf, buf->attach);
  3791. if (buf->dbuf_get) {
  3792. print_vidc_buffer(VIDC_ERR, "err ", "destroying: put dmabuf", inst, buf);
  3793. call_mem_op(core, dma_buf_put, inst, buf->dmabuf);
  3794. }
  3795. list_del_init(&buf->list);
  3796. msm_vidc_pool_free(inst, buf);
  3797. }
  3798. }
  3799. list_for_each_entry_safe(ts, dummy_ts, &inst->timestamps.list, sort.list) {
  3800. i_vpr_e(inst, "%s: removing ts: val %lld, rank %lld\n",
  3801. __func__, ts->sort.val, ts->rank);
  3802. list_del(&ts->sort.list);
  3803. msm_vidc_pool_free(inst, ts);
  3804. }
  3805. list_for_each_entry_safe(ts, dummy_ts, &inst->ts_reorder.list, sort.list) {
  3806. i_vpr_e(inst, "%s: removing reorder ts: val %lld\n",
  3807. __func__, ts->sort.val);
  3808. list_del(&ts->sort.list);
  3809. msm_vidc_pool_free(inst, ts);
  3810. }
  3811. list_for_each_entry_safe(timer, dummy_timer, &inst->input_timer_list, list) {
  3812. i_vpr_e(inst, "%s: removing input_timer %lld\n",
  3813. __func__, timer->time_us);
  3814. list_del(&timer->list);
  3815. msm_vidc_pool_free(inst, timer);
  3816. }
  3817. list_for_each_entry_safe(stats, dummy_stats, &inst->buffer_stats_list, list) {
  3818. print_buffer_stats(VIDC_ERR, "err ", inst, stats);
  3819. list_del(&stats->list);
  3820. msm_vidc_pool_free(inst, stats);
  3821. }
  3822. list_for_each_entry_safe(dbuf, dummy_dbuf, &inst->dmabuf_tracker, list) {
  3823. struct dma_buf *dmabuf;
  3824. struct inode *f_inode;
  3825. unsigned long inode_num = 0;
  3826. dmabuf = dbuf->dmabuf;
  3827. if (dmabuf && dmabuf->file) {
  3828. f_inode = file_inode(dmabuf->file);
  3829. if (f_inode) {
  3830. inode_num = f_inode->i_ino;
  3831. }
  3832. }
  3833. i_vpr_e(inst, "%s: removing dma_buf %p, inode %lu, refcount %u\n",
  3834. __func__, dbuf->dmabuf, inode_num, dbuf->refcount);
  3835. call_mem_op(core, dma_buf_put_completely, inst, dbuf);
  3836. }
  3837. list_for_each_entry_safe(entry, dummy_entry, &inst->firmware_list, list) {
  3838. i_vpr_e(inst, "%s: fw list: %s\n", __func__, cap_name(entry->cap_id));
  3839. list_del(&entry->list);
  3840. msm_vidc_vmem_free((void **)&entry);
  3841. }
  3842. list_for_each_entry_safe(entry, dummy_entry, &inst->children_list, list) {
  3843. i_vpr_e(inst, "%s: child list: %s\n", __func__, cap_name(entry->cap_id));
  3844. list_del(&entry->list);
  3845. msm_vidc_vmem_free((void **)&entry);
  3846. }
  3847. list_for_each_entry_safe(entry, dummy_entry, &inst->caps_list, list) {
  3848. list_del(&entry->list);
  3849. msm_vidc_vmem_free((void **)&entry);
  3850. }
  3851. list_for_each_entry_safe(cr, dummy_cr, &inst->enc_input_crs, list) {
  3852. list_del(&cr->list);
  3853. msm_vidc_vmem_free((void **)&cr);
  3854. }
  3855. list_for_each_entry_safe(fence, dummy_fence, &inst->fence_list, list) {
  3856. i_vpr_e(inst, "%s: destroying fence %s\n", __func__, fence->name);
  3857. call_fence_op(core, fence_destroy, inst, fence->fence_id);
  3858. }
  3859. /* destroy buffers from pool */
  3860. msm_vidc_pools_deinit(inst);
  3861. }
  3862. static void msm_vidc_close_helper(struct kref *kref)
  3863. {
  3864. struct msm_vidc_inst *inst = container_of(kref,
  3865. struct msm_vidc_inst, kref);
  3866. struct msm_vidc_core *core;
  3867. core = inst->core;
  3868. i_vpr_h(inst, "%s()\n", __func__);
  3869. msm_vidc_debugfs_deinit_inst(inst);
  3870. msm_vidc_fence_deinit(inst);
  3871. if (is_decode_session(inst))
  3872. msm_vdec_inst_deinit(inst);
  3873. else if (is_encode_session(inst))
  3874. msm_venc_inst_deinit(inst);
  3875. /**
  3876. * Lock is not necessay here, but in force close case,
  3877. * vb2q_deinit() will attempt to call stop_streaming()
  3878. * vb2 callback and i.e expecting inst lock to be taken.
  3879. * So acquire lock before calling vb2q_deinit.
  3880. */
  3881. inst_lock(inst, __func__);
  3882. msm_vidc_vb2_queue_deinit(inst);
  3883. msm_vidc_v4l2_fh_deinit(inst);
  3884. inst_unlock(inst, __func__);
  3885. destroy_workqueue(inst->workq);
  3886. msm_vidc_destroy_buffers(inst);
  3887. msm_vidc_remove_session(inst);
  3888. msm_vidc_remove_dangling_session(inst);
  3889. mutex_destroy(&inst->client_lock);
  3890. mutex_destroy(&inst->request_lock);
  3891. mutex_destroy(&inst->lock);
  3892. msm_vidc_vmem_free((void **)&inst);
  3893. }
  3894. struct msm_vidc_inst *get_inst_ref(struct msm_vidc_core *core,
  3895. struct msm_vidc_inst *instance)
  3896. {
  3897. struct msm_vidc_inst *inst = NULL;
  3898. bool matches = false;
  3899. if (!core) {
  3900. d_vpr_e("%s: invalid params\n", __func__);
  3901. return NULL;
  3902. }
  3903. mutex_lock(&core->lock);
  3904. list_for_each_entry(inst, &core->instances, list) {
  3905. if (inst == instance) {
  3906. matches = true;
  3907. break;
  3908. }
  3909. }
  3910. inst = (matches && kref_get_unless_zero(&inst->kref)) ? inst : NULL;
  3911. mutex_unlock(&core->lock);
  3912. return inst;
  3913. }
  3914. struct msm_vidc_inst *get_inst(struct msm_vidc_core *core,
  3915. u32 session_id)
  3916. {
  3917. struct msm_vidc_inst *inst = NULL;
  3918. bool matches = false;
  3919. if (!core) {
  3920. d_vpr_e("%s: invalid params\n", __func__);
  3921. return NULL;
  3922. }
  3923. mutex_lock(&core->lock);
  3924. list_for_each_entry(inst, &core->instances, list) {
  3925. if (inst->session_id == session_id) {
  3926. matches = true;
  3927. break;
  3928. }
  3929. }
  3930. inst = (matches && kref_get_unless_zero(&inst->kref)) ? inst : NULL;
  3931. mutex_unlock(&core->lock);
  3932. return inst;
  3933. }
  3934. void put_inst(struct msm_vidc_inst *inst)
  3935. {
  3936. kref_put(&inst->kref, msm_vidc_close_helper);
  3937. }
  3938. bool core_lock_check(struct msm_vidc_core *core, const char *func)
  3939. {
  3940. return mutex_is_locked(&core->lock);
  3941. }
  3942. void core_lock(struct msm_vidc_core *core, const char *function)
  3943. {
  3944. mutex_lock(&core->lock);
  3945. }
  3946. void core_unlock(struct msm_vidc_core *core, const char *function)
  3947. {
  3948. mutex_unlock(&core->lock);
  3949. }
  3950. bool inst_lock_check(struct msm_vidc_inst *inst, const char *func)
  3951. {
  3952. return mutex_is_locked(&inst->lock);
  3953. }
  3954. void inst_lock(struct msm_vidc_inst *inst, const char *function)
  3955. {
  3956. mutex_lock(&inst->lock);
  3957. }
  3958. void inst_unlock(struct msm_vidc_inst *inst, const char *function)
  3959. {
  3960. mutex_unlock(&inst->lock);
  3961. }
  3962. bool client_lock_check(struct msm_vidc_inst *inst, const char *func)
  3963. {
  3964. return mutex_is_locked(&inst->client_lock);
  3965. }
  3966. void client_lock(struct msm_vidc_inst *inst, const char *function)
  3967. {
  3968. mutex_lock(&inst->client_lock);
  3969. }
  3970. void client_unlock(struct msm_vidc_inst *inst, const char *function)
  3971. {
  3972. mutex_unlock(&inst->client_lock);
  3973. }
  3974. int msm_vidc_update_bitstream_buffer_size(struct msm_vidc_inst *inst)
  3975. {
  3976. struct msm_vidc_core *core;
  3977. struct v4l2_format *fmt;
  3978. core = inst->core;
  3979. if (is_decode_session(inst)) {
  3980. fmt = &inst->fmts[INPUT_PORT];
  3981. fmt->fmt.pix_mp.plane_fmt[0].sizeimage = call_session_op(core,
  3982. buffer_size, inst, MSM_VIDC_BUF_INPUT);
  3983. }
  3984. return 0;
  3985. }
  3986. int msm_vidc_update_meta_port_settings(struct msm_vidc_inst *inst)
  3987. {
  3988. struct msm_vidc_core *core;
  3989. struct v4l2_format *fmt;
  3990. core = inst->core;
  3991. fmt = &inst->fmts[INPUT_META_PORT];
  3992. fmt->fmt.meta.buffersize = call_session_op(core,
  3993. buffer_size, inst, MSM_VIDC_BUF_INPUT_META);
  3994. inst->buffers.input_meta.min_count =
  3995. inst->buffers.input.min_count;
  3996. inst->buffers.input_meta.extra_count =
  3997. inst->buffers.input.extra_count;
  3998. inst->buffers.input_meta.actual_count =
  3999. inst->buffers.input.actual_count;
  4000. inst->buffers.input_meta.size = fmt->fmt.meta.buffersize;
  4001. fmt = &inst->fmts[OUTPUT_META_PORT];
  4002. fmt->fmt.meta.buffersize = call_session_op(core,
  4003. buffer_size, inst, MSM_VIDC_BUF_OUTPUT_META);
  4004. inst->buffers.output_meta.min_count =
  4005. inst->buffers.output.min_count;
  4006. inst->buffers.output_meta.extra_count =
  4007. inst->buffers.output.extra_count;
  4008. inst->buffers.output_meta.actual_count =
  4009. inst->buffers.output.actual_count;
  4010. inst->buffers.output_meta.size = fmt->fmt.meta.buffersize;
  4011. return 0;
  4012. }
  4013. int msm_vidc_update_buffer_count(struct msm_vidc_inst *inst, u32 port)
  4014. {
  4015. struct msm_vidc_core *core;
  4016. core = inst->core;
  4017. switch (port) {
  4018. case INPUT_PORT:
  4019. inst->buffers.input.min_count = call_session_op(core,
  4020. min_count, inst, MSM_VIDC_BUF_INPUT);
  4021. inst->buffers.input.extra_count = call_session_op(core,
  4022. extra_count, inst, MSM_VIDC_BUF_INPUT);
  4023. if (inst->buffers.input.actual_count <
  4024. inst->buffers.input.min_count +
  4025. inst->buffers.input.extra_count) {
  4026. inst->buffers.input.actual_count =
  4027. inst->buffers.input.min_count +
  4028. inst->buffers.input.extra_count;
  4029. }
  4030. if (is_input_meta_enabled(inst)) {
  4031. inst->buffers.input_meta.min_count =
  4032. inst->buffers.input.min_count;
  4033. inst->buffers.input_meta.extra_count =
  4034. inst->buffers.input.extra_count;
  4035. inst->buffers.input_meta.actual_count =
  4036. inst->buffers.input.actual_count;
  4037. } else {
  4038. inst->buffers.input_meta.min_count = 0;
  4039. inst->buffers.input_meta.extra_count = 0;
  4040. inst->buffers.input_meta.actual_count = 0;
  4041. }
  4042. i_vpr_h(inst, "%s: type: INPUT, count: min %u, extra %u, actual %u\n", __func__,
  4043. inst->buffers.input.min_count,
  4044. inst->buffers.input.extra_count,
  4045. inst->buffers.input.actual_count);
  4046. break;
  4047. case OUTPUT_PORT:
  4048. if (!inst->bufq[INPUT_PORT].vb2q->streaming)
  4049. inst->buffers.output.min_count = call_session_op(core,
  4050. min_count, inst, MSM_VIDC_BUF_OUTPUT);
  4051. inst->buffers.output.extra_count = call_session_op(core,
  4052. extra_count, inst, MSM_VIDC_BUF_OUTPUT);
  4053. if (inst->buffers.output.actual_count <
  4054. inst->buffers.output.min_count +
  4055. inst->buffers.output.extra_count) {
  4056. inst->buffers.output.actual_count =
  4057. inst->buffers.output.min_count +
  4058. inst->buffers.output.extra_count;
  4059. }
  4060. if (is_output_meta_enabled(inst)) {
  4061. inst->buffers.output_meta.min_count =
  4062. inst->buffers.output.min_count;
  4063. inst->buffers.output_meta.extra_count =
  4064. inst->buffers.output.extra_count;
  4065. inst->buffers.output_meta.actual_count =
  4066. inst->buffers.output.actual_count;
  4067. } else {
  4068. inst->buffers.output_meta.min_count = 0;
  4069. inst->buffers.output_meta.extra_count = 0;
  4070. inst->buffers.output_meta.actual_count = 0;
  4071. }
  4072. i_vpr_h(inst, "%s: type: OUTPUT, count: min %u, extra %u, actual %u\n", __func__,
  4073. inst->buffers.output.min_count,
  4074. inst->buffers.output.extra_count,
  4075. inst->buffers.output.actual_count);
  4076. break;
  4077. default:
  4078. d_vpr_e("%s unknown port %d\n", __func__, port);
  4079. return -EINVAL;
  4080. }
  4081. return 0;
  4082. }
  4083. void msm_vidc_schedule_core_deinit(struct msm_vidc_core *core)
  4084. {
  4085. if (!core)
  4086. return;
  4087. if (!core->capabilities[FW_UNLOAD].value)
  4088. return;
  4089. cancel_delayed_work(&core->fw_unload_work);
  4090. schedule_delayed_work(&core->fw_unload_work,
  4091. msecs_to_jiffies(core->capabilities[FW_UNLOAD_DELAY].value));
  4092. d_vpr_h("firmware unload delayed by %u ms\n",
  4093. core->capabilities[FW_UNLOAD_DELAY].value);
  4094. return;
  4095. }
  4096. static const char *get_codec_str(enum msm_vidc_codec_type type)
  4097. {
  4098. switch (type) {
  4099. case MSM_VIDC_H264: return " avc";
  4100. case MSM_VIDC_HEVC: return "hevc";
  4101. case MSM_VIDC_VP9: return " vp9";
  4102. case MSM_VIDC_AV1: return " av1";
  4103. case MSM_VIDC_HEIC: return "heic";
  4104. }
  4105. return "....";
  4106. }
  4107. static const char *get_domain_str(enum msm_vidc_domain_type type)
  4108. {
  4109. switch (type) {
  4110. case MSM_VIDC_ENCODER: return "E";
  4111. case MSM_VIDC_DECODER: return "D";
  4112. }
  4113. return ".";
  4114. }
  4115. int msm_vidc_update_debug_str(struct msm_vidc_inst *inst)
  4116. {
  4117. u32 sid;
  4118. int client_id = INVALID_CLIENT_ID;
  4119. const char *codec;
  4120. const char *domain;
  4121. if (!inst) {
  4122. d_vpr_e("%s: Invalid params\n", __func__);
  4123. return -EINVAL;
  4124. }
  4125. client_id = inst->capabilities[CLIENT_ID].value;
  4126. sid = inst->session_id;
  4127. codec = get_codec_str(inst->codec);
  4128. domain = get_domain_str(inst->domain);
  4129. if (client_id != INVALID_CLIENT_ID) {
  4130. snprintf(inst->debug_str, sizeof(inst->debug_str), "%08x: %s%s_%d",
  4131. sid, codec, domain, client_id);
  4132. } else {
  4133. snprintf(inst->debug_str, sizeof(inst->debug_str), "%08x: %s%s",
  4134. sid, codec, domain);
  4135. }
  4136. d_vpr_h("%s: sid: %08x, codec: %s, domain: %s, final: %s\n",
  4137. __func__, sid, codec, domain, inst->debug_str);
  4138. return 0;
  4139. }
  4140. static int msm_vidc_print_insts_info(struct msm_vidc_core *core)
  4141. {
  4142. struct msm_vidc_inst *inst;
  4143. u32 height, width, fps, orate;
  4144. struct msm_vidc_inst_cap *cap;
  4145. struct v4l2_format *out_f;
  4146. struct v4l2_format *inp_f;
  4147. char prop[64];
  4148. d_vpr_e("Print all running instances\n");
  4149. d_vpr_e("%6s | %6s | %5s | %5s | %5s\n", "width", "height", "fps", "orate", "prop");
  4150. core_lock(core, __func__);
  4151. list_for_each_entry(inst, &core->instances, list) {
  4152. out_f = &inst->fmts[OUTPUT_PORT];
  4153. inp_f = &inst->fmts[INPUT_PORT];
  4154. cap = &inst->capabilities[0];
  4155. memset(&prop, 0, sizeof(prop));
  4156. width = max(out_f->fmt.pix_mp.width, inp_f->fmt.pix_mp.width);
  4157. height = max(out_f->fmt.pix_mp.height, inp_f->fmt.pix_mp.height);
  4158. fps = cap[FRAME_RATE].value >> 16;
  4159. orate = cap[OPERATING_RATE].value >> 16;
  4160. if (is_realtime_session(inst))
  4161. strlcat(prop, "RT ", sizeof(prop));
  4162. else
  4163. strlcat(prop, "NRT", sizeof(prop));
  4164. if (is_thumbnail_session(inst))
  4165. strlcat(prop, "+THUMB", sizeof(prop));
  4166. if (is_image_session(inst))
  4167. strlcat(prop, "+IMAGE", sizeof(prop));
  4168. i_vpr_e(inst, "%6u | %6u | %5u | %5u | %5s\n", width, height, fps, orate, prop);
  4169. }
  4170. core_unlock(core, __func__);
  4171. return 0;
  4172. }
  4173. static int msm_vidc_get_inst_load(struct msm_vidc_inst *inst)
  4174. {
  4175. u32 mbpf, fps;
  4176. u32 input_rate, timestamp_rate, operating_rate;
  4177. if (!inst) {
  4178. d_vpr_e("%s: invalid params\n", __func__);
  4179. return -EINVAL;
  4180. }
  4181. /*
  4182. * Encoder: consider frame rate
  4183. * Decoder: consider max(frame rate, operating rate,
  4184. * timestamp rate, input queue rate)
  4185. */
  4186. mbpf = msm_vidc_get_mbs_per_frame(inst);
  4187. fps = msm_vidc_get_frame_rate(inst);
  4188. if (is_decode_session(inst)) {
  4189. input_rate = msm_vidc_get_input_rate(inst);
  4190. timestamp_rate = msm_vidc_get_timestamp_rate(inst);
  4191. operating_rate = msm_vidc_get_operating_rate(inst);
  4192. fps = max(fps, operating_rate);
  4193. fps = max(fps, input_rate);
  4194. fps = max(fps, timestamp_rate);
  4195. }
  4196. return mbpf * fps;
  4197. }
  4198. static bool msm_vidc_ignore_session_load(struct msm_vidc_inst *inst)
  4199. {
  4200. if (!is_realtime_session(inst) || is_thumbnail_session(inst) ||
  4201. is_image_session(inst) || is_session_error(inst))
  4202. return true;
  4203. return false;
  4204. }
  4205. int msm_vidc_check_core_mbps(struct msm_vidc_inst *inst)
  4206. {
  4207. u64 mbps = 0, total_mbps = 0, enc_mbps = 0, critical_mbps = 0;
  4208. struct msm_vidc_core *core;
  4209. struct msm_vidc_inst *instance;
  4210. core = inst->core;
  4211. /* skip mbps check for non-realtime, thumnail, image sessions */
  4212. if (msm_vidc_ignore_session_load(inst)) {
  4213. i_vpr_h(inst,
  4214. "%s: skip mbps check due to NRT %d, TH %d, IMG %d, error session %d\n",
  4215. __func__, !is_realtime_session(inst), is_thumbnail_session(inst),
  4216. is_image_session(inst), is_session_error(inst));
  4217. return 0;
  4218. }
  4219. core_lock(core, __func__);
  4220. list_for_each_entry(instance, &core->instances, list) {
  4221. if (is_critical_priority_session(instance))
  4222. critical_mbps += msm_vidc_get_inst_load(instance);
  4223. }
  4224. core_unlock(core, __func__);
  4225. if (critical_mbps > core->capabilities[MAX_MBPS].value) {
  4226. i_vpr_e(inst, "%s: Hardware overloaded with critical sessions. needed %u, max %u",
  4227. __func__, critical_mbps, core->capabilities[MAX_MBPS].value);
  4228. return -ENOMEM;
  4229. }
  4230. core_lock(core, __func__);
  4231. list_for_each_entry(instance, &core->instances, list) {
  4232. /* ignore thumbnail, image, non realtime, error sessions */
  4233. if (msm_vidc_ignore_session_load(instance))
  4234. continue;
  4235. mbps = msm_vidc_get_inst_load(instance);
  4236. total_mbps += mbps;
  4237. if (is_encode_session(instance))
  4238. enc_mbps += mbps;
  4239. }
  4240. core_unlock(core, __func__);
  4241. if (is_encode_session(inst)) {
  4242. /* reject encoder if all encoders mbps is greater than MAX_MBPS */
  4243. if (enc_mbps > core->capabilities[MAX_MBPS].value) {
  4244. i_vpr_e(inst, "%s: Hardware overloaded. needed %u, max %u", __func__,
  4245. mbps, core->capabilities[MAX_MBPS].value);
  4246. return -ENOMEM;
  4247. }
  4248. /*
  4249. * if total_mbps is greater than max_mbps then reduce all decoders
  4250. * priority by 1 to allow this encoder
  4251. */
  4252. if (total_mbps > core->capabilities[MAX_MBPS].value) {
  4253. core_lock(core, __func__);
  4254. list_for_each_entry(instance, &core->instances, list) {
  4255. /* reduce realtime decode sessions priority */
  4256. if (is_decode_session(instance) && is_realtime_session(instance)) {
  4257. instance->adjust_priority = RT_DEC_DOWN_PRORITY_OFFSET;
  4258. i_vpr_h(inst, "%s: pending adjust priority by %d\n",
  4259. __func__, instance->adjust_priority);
  4260. }
  4261. }
  4262. core_unlock(core, __func__);
  4263. }
  4264. } else if (is_decode_session(inst)) {
  4265. /*
  4266. * if total_mbps is greater than max_mbps then allow this
  4267. * decoder by reducing its piority (moving it to NRT)
  4268. */
  4269. if (total_mbps > core->capabilities[MAX_MBPS].value) {
  4270. inst->adjust_priority = RT_DEC_DOWN_PRORITY_OFFSET;
  4271. i_vpr_h(inst, "%s: pending adjust priority by %d\n",
  4272. __func__, inst->adjust_priority);
  4273. }
  4274. }
  4275. i_vpr_h(inst, "%s: HW load needed %u is within max %u", __func__,
  4276. total_mbps, core->capabilities[MAX_MBPS].value);
  4277. return 0;
  4278. }
  4279. int msm_vidc_check_core_mbpf(struct msm_vidc_inst *inst)
  4280. {
  4281. u32 video_mbpf = 0, image_mbpf = 0, video_rt_mbpf = 0;
  4282. u32 critical_mbpf = 0;
  4283. struct msm_vidc_core *core;
  4284. struct msm_vidc_inst *instance;
  4285. core = inst->core;
  4286. core_lock(core, __func__);
  4287. list_for_each_entry(instance, &core->instances, list) {
  4288. if (is_critical_priority_session(instance))
  4289. critical_mbpf += msm_vidc_get_mbs_per_frame(instance);
  4290. }
  4291. core_unlock(core, __func__);
  4292. if (critical_mbpf > core->capabilities[MAX_MBPF].value) {
  4293. i_vpr_e(inst, "%s: Hardware overloaded with critical sessions. needed %u, max %u",
  4294. __func__, critical_mbpf, core->capabilities[MAX_MBPF].value);
  4295. return -ENOMEM;
  4296. }
  4297. core_lock(core, __func__);
  4298. list_for_each_entry(instance, &core->instances, list) {
  4299. /* ignore thumbnail session */
  4300. if (is_thumbnail_session(instance))
  4301. continue;
  4302. if (is_image_session(instance))
  4303. image_mbpf += msm_vidc_get_mbs_per_frame(instance);
  4304. else
  4305. video_mbpf += msm_vidc_get_mbs_per_frame(instance);
  4306. }
  4307. core_unlock(core, __func__);
  4308. if (video_mbpf > core->capabilities[MAX_MBPF].value) {
  4309. i_vpr_e(inst, "%s: video overloaded. needed %u, max %u", __func__,
  4310. video_mbpf, core->capabilities[MAX_MBPF].value);
  4311. return -ENOMEM;
  4312. }
  4313. if (image_mbpf > core->capabilities[MAX_IMAGE_MBPF].value) {
  4314. i_vpr_e(inst, "%s: image overloaded. needed %u, max %u", __func__,
  4315. image_mbpf, core->capabilities[MAX_IMAGE_MBPF].value);
  4316. return -ENOMEM;
  4317. }
  4318. core_lock(core, __func__);
  4319. /* check real-time video sessions max limit */
  4320. list_for_each_entry(instance, &core->instances, list) {
  4321. if (msm_vidc_ignore_session_load(instance))
  4322. continue;
  4323. video_rt_mbpf += msm_vidc_get_mbs_per_frame(instance);
  4324. }
  4325. core_unlock(core, __func__);
  4326. if (video_rt_mbpf > core->capabilities[MAX_RT_MBPF].value) {
  4327. i_vpr_e(inst, "%s: real-time video overloaded. needed %u, max %u",
  4328. __func__, video_rt_mbpf, core->capabilities[MAX_RT_MBPF].value);
  4329. return -ENOMEM;
  4330. }
  4331. return 0;
  4332. }
  4333. static int msm_vidc_check_inst_mbpf(struct msm_vidc_inst *inst)
  4334. {
  4335. u32 mbpf = 0, max_mbpf = 0;
  4336. struct msm_vidc_inst_cap *cap;
  4337. cap = &inst->capabilities[0];
  4338. if (is_secure_session(inst))
  4339. max_mbpf = cap[SECURE_MBPF].max;
  4340. else if (is_encode_session(inst) && cap[LOSSLESS].value)
  4341. max_mbpf = cap[LOSSLESS_MBPF].max;
  4342. else
  4343. max_mbpf = cap[MBPF].max;
  4344. /* check current session mbpf */
  4345. mbpf = msm_vidc_get_mbs_per_frame(inst);
  4346. if (mbpf > max_mbpf) {
  4347. i_vpr_e(inst, "%s: session overloaded. needed %u, max %u", __func__,
  4348. mbpf, max_mbpf);
  4349. return -ENOMEM;
  4350. }
  4351. return 0;
  4352. }
  4353. u32 msm_vidc_get_max_bitrate(struct msm_vidc_inst *inst)
  4354. {
  4355. u32 max_bitrate = 0x7fffffff;
  4356. if (inst->capabilities[LOWLATENCY_MODE].value)
  4357. max_bitrate = min(max_bitrate,
  4358. (u32)inst->capabilities[LOWLATENCY_MAX_BITRATE].max);
  4359. if (inst->capabilities[ALL_INTRA].value)
  4360. max_bitrate = min(max_bitrate,
  4361. (u32)inst->capabilities[ALLINTRA_MAX_BITRATE].max);
  4362. if (inst->codec == MSM_VIDC_HEVC) {
  4363. max_bitrate = min(max_bitrate,
  4364. (u32)inst->capabilities[CABAC_MAX_BITRATE].max);
  4365. } else if (inst->codec == MSM_VIDC_H264) {
  4366. if (inst->capabilities[ENTROPY_MODE].value ==
  4367. V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC)
  4368. max_bitrate = min(max_bitrate,
  4369. (u32)inst->capabilities[CAVLC_MAX_BITRATE].max);
  4370. else
  4371. max_bitrate = min(max_bitrate,
  4372. (u32)inst->capabilities[CABAC_MAX_BITRATE].max);
  4373. }
  4374. if (max_bitrate == 0x7fffffff || !max_bitrate)
  4375. max_bitrate = min(max_bitrate, (u32)inst->capabilities[BIT_RATE].max);
  4376. return max_bitrate;
  4377. }
  4378. static bool msm_vidc_allow_image_encode_session(struct msm_vidc_inst *inst)
  4379. {
  4380. struct msm_vidc_inst_cap *cap;
  4381. struct v4l2_format *fmt;
  4382. u32 min_width, min_height, max_width, max_height, pix_fmt, profile;
  4383. bool allow = false;
  4384. cap = &inst->capabilities[0];
  4385. if (!is_image_encode_session(inst)) {
  4386. i_vpr_e(inst, "%s: not an image encode session\n", __func__);
  4387. return false;
  4388. }
  4389. pix_fmt = cap[PIX_FMTS].value;
  4390. profile = cap[PROFILE].value;
  4391. /* is input with & height is in allowed range */
  4392. min_width = cap[FRAME_WIDTH].min;
  4393. max_width = cap[FRAME_WIDTH].max;
  4394. min_height = cap[FRAME_HEIGHT].min;
  4395. max_height = cap[FRAME_HEIGHT].max;
  4396. fmt = &inst->fmts[INPUT_PORT];
  4397. if (!in_range(fmt->fmt.pix_mp.width, min_width, max_width) ||
  4398. !in_range(fmt->fmt.pix_mp.height, min_height, max_height)) {
  4399. i_vpr_e(inst, "unsupported wxh [%u x %u], allowed [%u x %u] to [%u x %u]\n",
  4400. fmt->fmt.pix_mp.width, fmt->fmt.pix_mp.height,
  4401. min_width, min_height, max_width, max_height);
  4402. allow = false;
  4403. goto exit;
  4404. }
  4405. /* is linear yuv color fmt */
  4406. allow = is_linear_yuv_colorformat(pix_fmt);
  4407. if (!allow) {
  4408. i_vpr_e(inst, "%s: compressed fmt: %#x\n", __func__, pix_fmt);
  4409. goto exit;
  4410. }
  4411. /* is output grid dimension */
  4412. fmt = &inst->fmts[OUTPUT_PORT];
  4413. allow = fmt->fmt.pix_mp.width == cap[GRID_SIZE].value;
  4414. allow &= fmt->fmt.pix_mp.height == cap[GRID_SIZE].value;
  4415. if (!allow) {
  4416. i_vpr_e(inst, "%s: output is not a grid dimension: %u x %u\n", __func__,
  4417. fmt->fmt.pix_mp.width, fmt->fmt.pix_mp.height);
  4418. goto exit;
  4419. }
  4420. /* is bitrate mode CQ */
  4421. allow = cap[BITRATE_MODE].value == V4L2_MPEG_VIDEO_BITRATE_MODE_CQ;
  4422. if (!allow) {
  4423. i_vpr_e(inst, "%s: bitrate mode is not CQ: %#x\n", __func__,
  4424. cap[BITRATE_MODE].value);
  4425. goto exit;
  4426. }
  4427. /* is all intra */
  4428. allow = !cap[GOP_SIZE].value;
  4429. allow &= !cap[B_FRAME].value;
  4430. if (!allow) {
  4431. i_vpr_e(inst, "%s: not all intra: gop: %u, bframe: %u\n", __func__,
  4432. cap[GOP_SIZE].value, cap[B_FRAME].value);
  4433. goto exit;
  4434. }
  4435. /* is time delta based rc disabled */
  4436. allow = !cap[TIME_DELTA_BASED_RC].value;
  4437. if (!allow) {
  4438. i_vpr_e(inst, "%s: time delta based rc not disabled: %#x\n", __func__,
  4439. cap[TIME_DELTA_BASED_RC].value);
  4440. goto exit;
  4441. }
  4442. /* is frame skip mode disabled */
  4443. allow = !cap[FRAME_SKIP_MODE].value;
  4444. if (!allow) {
  4445. i_vpr_e(inst, "%s: frame skip mode not disabled: %#x\n", __func__,
  4446. cap[FRAME_SKIP_MODE].value);
  4447. goto exit;
  4448. }
  4449. exit:
  4450. if (!allow)
  4451. i_vpr_e(inst, "%s: current session not allowed\n", __func__);
  4452. return allow;
  4453. }
  4454. static int msm_vidc_check_resolution_supported(struct msm_vidc_inst *inst)
  4455. {
  4456. struct msm_vidc_inst_cap *cap;
  4457. u32 width = 0, height = 0, min_width, min_height,
  4458. max_width, max_height;
  4459. bool is_interlaced = false;
  4460. cap = &inst->capabilities[0];
  4461. if (is_decode_session(inst)) {
  4462. width = inst->fmts[INPUT_PORT].fmt.pix_mp.width;
  4463. height = inst->fmts[INPUT_PORT].fmt.pix_mp.height;
  4464. } else if (is_encode_session(inst)) {
  4465. width = inst->crop.width;
  4466. height = inst->crop.height;
  4467. }
  4468. if (is_secure_session(inst)) {
  4469. min_width = cap[SECURE_FRAME_WIDTH].min;
  4470. max_width = cap[SECURE_FRAME_WIDTH].max;
  4471. min_height = cap[SECURE_FRAME_HEIGHT].min;
  4472. max_height = cap[SECURE_FRAME_HEIGHT].max;
  4473. } else if (is_encode_session(inst) && cap[LOSSLESS].value) {
  4474. min_width = cap[LOSSLESS_FRAME_WIDTH].min;
  4475. max_width = cap[LOSSLESS_FRAME_WIDTH].max;
  4476. min_height = cap[LOSSLESS_FRAME_HEIGHT].min;
  4477. max_height = cap[LOSSLESS_FRAME_HEIGHT].max;
  4478. } else {
  4479. min_width = cap[FRAME_WIDTH].min;
  4480. max_width = cap[FRAME_WIDTH].max;
  4481. min_height = cap[FRAME_HEIGHT].min;
  4482. max_height = cap[FRAME_HEIGHT].max;
  4483. }
  4484. /* check if input width and height is in supported range */
  4485. if (is_decode_session(inst) || is_encode_session(inst)) {
  4486. if (!in_range(width, min_width, max_width) ||
  4487. !in_range(height, min_height, max_height)) {
  4488. i_vpr_e(inst,
  4489. "%s: unsupported input wxh [%u x %u], allowed range: [%u x %u] to [%u x %u]\n",
  4490. __func__, width, height, min_width,
  4491. min_height, max_width, max_height);
  4492. return -EINVAL;
  4493. }
  4494. }
  4495. /* check interlace supported resolution */
  4496. is_interlaced = cap[CODED_FRAMES].value == CODED_FRAMES_INTERLACE;
  4497. if (is_interlaced && (width > INTERLACE_WIDTH_MAX || height > INTERLACE_HEIGHT_MAX ||
  4498. NUM_MBS_PER_FRAME(width, height) > INTERLACE_MB_PER_FRAME_MAX)) {
  4499. i_vpr_e(inst, "%s: unsupported interlace wxh [%u x %u], max [%u x %u]\n",
  4500. __func__, width, height, INTERLACE_WIDTH_MAX, INTERLACE_HEIGHT_MAX);
  4501. return -EINVAL;
  4502. }
  4503. return 0;
  4504. }
  4505. static int msm_vidc_check_max_sessions(struct msm_vidc_inst *inst)
  4506. {
  4507. u32 width = 0, height = 0;
  4508. u32 num_1080p_sessions = 0, num_4k_sessions = 0, num_8k_sessions = 0;
  4509. struct msm_vidc_inst *i;
  4510. struct msm_vidc_core *core;
  4511. core = inst->core;
  4512. core_lock(core, __func__);
  4513. list_for_each_entry(i, &core->instances, list) {
  4514. /* skip image sessions count */
  4515. if (is_image_session(i))
  4516. continue;
  4517. if (is_decode_session(i)) {
  4518. width = i->fmts[INPUT_PORT].fmt.pix_mp.width;
  4519. height = i->fmts[INPUT_PORT].fmt.pix_mp.height;
  4520. } else if (is_encode_session(i)) {
  4521. width = i->crop.width;
  4522. height = i->crop.height;
  4523. }
  4524. /*
  4525. * one 8k session equals to 64 720p sessions in reality.
  4526. * So for one 8k session the number of 720p sessions will
  4527. * exceed max supported session count(16), hence one 8k session
  4528. * will be rejected as well.
  4529. * Therefore, treat one 8k session equal to two 4k sessions and
  4530. * one 4k session equal to two 1080p sessions and
  4531. * one 1080p session equal to two 720p sessions. This equation
  4532. * will make one 8k session equal to eight 720p sessions
  4533. * which looks good.
  4534. *
  4535. * Do not treat resolutions above 4k as 8k session instead
  4536. * treat (4K + half 4k) above as 8k session
  4537. */
  4538. if (res_is_greater_than(width, height, 4096 + (4096 >> 1), 2176 + (2176 >> 1))) {
  4539. num_8k_sessions += 1;
  4540. num_4k_sessions += 2;
  4541. num_1080p_sessions += 4;
  4542. } else if (res_is_greater_than(width, height, 1920 + (1920 >> 1), 1088 + (1088 >> 1))) {
  4543. num_4k_sessions += 1;
  4544. num_1080p_sessions += 2;
  4545. } else if (res_is_greater_than(width, height, 1280 + (1280 >> 1), 736 + (736 >> 1))) {
  4546. num_1080p_sessions += 1;
  4547. }
  4548. }
  4549. core_unlock(core, __func__);
  4550. if (num_8k_sessions > core->capabilities[MAX_NUM_8K_SESSIONS].value) {
  4551. i_vpr_e(inst, "%s: total 8k sessions %d, exceeded max limit %d\n",
  4552. __func__, num_8k_sessions,
  4553. core->capabilities[MAX_NUM_8K_SESSIONS].value);
  4554. return -ENOMEM;
  4555. }
  4556. if (num_4k_sessions > core->capabilities[MAX_NUM_4K_SESSIONS].value) {
  4557. i_vpr_e(inst, "%s: total 4K sessions %d, exceeded max limit %d\n",
  4558. __func__, num_4k_sessions,
  4559. core->capabilities[MAX_NUM_4K_SESSIONS].value);
  4560. return -ENOMEM;
  4561. }
  4562. if (num_1080p_sessions > core->capabilities[MAX_NUM_1080P_SESSIONS].value) {
  4563. i_vpr_e(inst, "%s: total 1080p sessions %d, exceeded max limit %d\n",
  4564. __func__, num_1080p_sessions,
  4565. core->capabilities[MAX_NUM_1080P_SESSIONS].value);
  4566. return -ENOMEM;
  4567. }
  4568. return 0;
  4569. }
  4570. int msm_vidc_check_session_supported(struct msm_vidc_inst *inst)
  4571. {
  4572. bool allow = false;
  4573. int rc = 0;
  4574. if (is_image_session(inst) && is_secure_session(inst)) {
  4575. i_vpr_e(inst, "%s: secure image session not supported\n", __func__);
  4576. rc = -EINVAL;
  4577. goto exit;
  4578. }
  4579. rc = msm_vidc_check_core_mbps(inst);
  4580. if (rc)
  4581. goto exit;
  4582. rc = msm_vidc_check_core_mbpf(inst);
  4583. if (rc)
  4584. goto exit;
  4585. rc = msm_vidc_check_inst_mbpf(inst);
  4586. if (rc)
  4587. goto exit;
  4588. rc = msm_vidc_check_resolution_supported(inst);
  4589. if (rc)
  4590. goto exit;
  4591. /* check image capabilities */
  4592. if (is_image_encode_session(inst)) {
  4593. allow = msm_vidc_allow_image_encode_session(inst);
  4594. if (!allow) {
  4595. rc = -EINVAL;
  4596. goto exit;
  4597. }
  4598. }
  4599. rc = msm_vidc_check_max_sessions(inst);
  4600. if (rc)
  4601. goto exit;
  4602. exit:
  4603. if (rc) {
  4604. i_vpr_e(inst, "%s: current session not supported\n", __func__);
  4605. msm_vidc_print_insts_info(inst->core);
  4606. }
  4607. return rc;
  4608. }
  4609. int msm_vidc_check_scaling_supported(struct msm_vidc_inst *inst)
  4610. {
  4611. u32 iwidth, owidth, iheight, oheight, ds_factor;
  4612. if (is_image_session(inst) || is_decode_session(inst)) {
  4613. i_vpr_h(inst, "%s: Scaling is supported for encode session only\n", __func__);
  4614. return 0;
  4615. }
  4616. if (!is_scaling_enabled(inst)) {
  4617. i_vpr_h(inst, "%s: Scaling not enabled. skip scaling check\n", __func__);
  4618. return 0;
  4619. }
  4620. iwidth = inst->crop.width;
  4621. iheight = inst->crop.height;
  4622. owidth = inst->compose.width;
  4623. oheight = inst->compose.height;
  4624. ds_factor = inst->capabilities[SCALE_FACTOR].value;
  4625. /* upscaling: encoder doesnot support upscaling */
  4626. if (owidth > iwidth || oheight > iheight) {
  4627. i_vpr_e(inst, "%s: upscale not supported: input [%u x %u], output [%u x %u]\n",
  4628. __func__, iwidth, iheight, owidth, oheight);
  4629. return -EINVAL;
  4630. }
  4631. /* downscaling: only supported upto 1/8 of width & 1/8 of height */
  4632. if (iwidth > owidth * ds_factor || iheight > oheight * ds_factor) {
  4633. i_vpr_e(inst,
  4634. "%s: unsupported ratio: input [%u x %u], output [%u x %u], ratio %u\n",
  4635. __func__, iwidth, iheight, owidth, oheight, ds_factor);
  4636. return -EINVAL;
  4637. }
  4638. return 0;
  4639. }
  4640. struct msm_vidc_fw_query_params {
  4641. u32 hfi_prop_name;
  4642. u32 port;
  4643. };
  4644. int msm_vidc_get_properties(struct msm_vidc_inst *inst)
  4645. {
  4646. int rc = 0;
  4647. int i;
  4648. static const struct msm_vidc_fw_query_params fw_query_params[] = {
  4649. {HFI_PROP_STAGE, HFI_PORT_NONE},
  4650. {HFI_PROP_PIPE, HFI_PORT_NONE},
  4651. {HFI_PROP_QUALITY_MODE, HFI_PORT_BITSTREAM}
  4652. };
  4653. for (i = 0; i < ARRAY_SIZE(fw_query_params); i++) {
  4654. if (is_decode_session(inst)) {
  4655. if (fw_query_params[i].hfi_prop_name == HFI_PROP_QUALITY_MODE)
  4656. continue;
  4657. }
  4658. i_vpr_l(inst, "%s: querying fw for property %#x\n", __func__,
  4659. fw_query_params[i].hfi_prop_name);
  4660. rc = venus_hfi_session_property(inst,
  4661. fw_query_params[i].hfi_prop_name,
  4662. (HFI_HOST_FLAGS_RESPONSE_REQUIRED |
  4663. HFI_HOST_FLAGS_INTR_REQUIRED |
  4664. HFI_HOST_FLAGS_GET_PROPERTY),
  4665. fw_query_params[i].port,
  4666. HFI_PAYLOAD_NONE,
  4667. NULL,
  4668. 0);
  4669. if (rc)
  4670. return rc;
  4671. }
  4672. return 0;
  4673. }
  4674. struct context_bank_info *msm_vidc_get_context_bank_for_region(
  4675. struct msm_vidc_core *core, enum msm_vidc_buffer_region region)
  4676. {
  4677. struct context_bank_info *cb = NULL, *match = NULL;
  4678. if (!region || region >= MSM_VIDC_REGION_MAX) {
  4679. d_vpr_e("Invalid region %#x\n", region);
  4680. return NULL;
  4681. }
  4682. venus_hfi_for_each_context_bank(core, cb) {
  4683. if (cb->region == region) {
  4684. match = cb;
  4685. break;
  4686. }
  4687. }
  4688. if (!match)
  4689. d_vpr_e("cb not found for region %#x\n", region);
  4690. return match;
  4691. }
  4692. struct context_bank_info *msm_vidc_get_context_bank_for_device(
  4693. struct msm_vidc_core *core, struct device *dev)
  4694. {
  4695. struct context_bank_info *cb = NULL, *match = NULL;
  4696. if (!core || !dev) {
  4697. d_vpr_e("%s: invalid params\n", __func__);
  4698. return NULL;
  4699. }
  4700. venus_hfi_for_each_context_bank(core, cb) {
  4701. if (of_device_is_compatible(dev->of_node, cb->name)) {
  4702. match = cb;
  4703. break;
  4704. }
  4705. }
  4706. if (!match)
  4707. d_vpr_e("cb not found for dev %s\n", dev_name(dev));
  4708. return match;
  4709. }