msm_vidc_driver.c 134 KB

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