msm_vidc_driver.c 175 KB

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