msm_vidc_driver.c 143 KB

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