msm_vidc_driver.c 133 KB

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