msm_vidc_driver.c 139 KB

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