msm_vidc_driver.c 143 KB

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