msm_vidc_driver.c 139 KB

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