cam_isp_context.c 292 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2024, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/debugfs.h>
  7. #include <linux/videodev2.h>
  8. #include <linux/slab.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/ratelimit.h>
  11. #include "cam_mem_mgr.h"
  12. #include "cam_sync_api.h"
  13. #include "cam_req_mgr_dev.h"
  14. #include "cam_trace.h"
  15. #include "cam_debug_util.h"
  16. #include "cam_packet_util.h"
  17. #include "cam_context_utils.h"
  18. #include "cam_cdm_util.h"
  19. #include "cam_isp_context.h"
  20. #include "cam_common_util.h"
  21. #include "cam_req_mgr_debug.h"
  22. #include "cam_cpas_api.h"
  23. #include "cam_ife_hw_mgr.h"
  24. #include "cam_subdev.h"
  25. static const char isp_dev_name[] = "cam-isp";
  26. static struct cam_isp_ctx_debug isp_ctx_debug;
  27. #define INC_HEAD(head, max_entries, ret) \
  28. div_u64_rem(atomic64_add_return(1, head),\
  29. max_entries, (ret))
  30. static int cam_isp_context_dump_requests(void *data,
  31. void *pf_args);
  32. static int cam_isp_context_hw_recovery(void *priv, void *data);
  33. static int cam_isp_context_handle_message(void *context,
  34. uint32_t msg_type, void *data);
  35. static int __cam_isp_ctx_start_dev_in_ready(struct cam_context *ctx,
  36. struct cam_start_stop_dev_cmd *cmd);
  37. static void __cam_isp_ctx_dump_state_monitor_array(
  38. struct cam_isp_context *ctx_isp);
  39. static const char *__cam_isp_hw_evt_val_to_type(
  40. uint32_t evt_id);
  41. static const char *__cam_isp_ctx_substate_val_to_type(
  42. enum cam_isp_ctx_activated_substate type);
  43. static int __cam_isp_ctx_check_deferred_buf_done(
  44. struct cam_isp_context *ctx_isp,
  45. struct cam_isp_hw_done_event_data *done,
  46. uint32_t bubble_state);
  47. static const char *__cam_isp_evt_val_to_type(
  48. uint32_t evt_id)
  49. {
  50. switch (evt_id) {
  51. case CAM_ISP_CTX_EVENT_SUBMIT:
  52. return "SUBMIT";
  53. case CAM_ISP_CTX_EVENT_APPLY:
  54. return "APPLY";
  55. case CAM_ISP_CTX_EVENT_EPOCH:
  56. return "EPOCH";
  57. case CAM_ISP_CTX_EVENT_RUP:
  58. return "RUP";
  59. case CAM_ISP_CTX_EVENT_BUFDONE:
  60. return "BUFDONE";
  61. case CAM_ISP_CTX_EVENT_SHUTTER:
  62. return "SHUTTER_NOTIFICATION";
  63. default:
  64. return "CAM_ISP_EVENT_INVALID";
  65. }
  66. }
  67. static void __cam_isp_ctx_update_event_record(
  68. struct cam_isp_context *ctx_isp,
  69. enum cam_isp_ctx_event event,
  70. struct cam_ctx_request *req,
  71. void *event_data)
  72. {
  73. int iterator = 0;
  74. ktime_t cur_time;
  75. struct cam_isp_ctx_req *req_isp;
  76. if (!ctx_isp) {
  77. CAM_ERR(CAM_ISP, "Invalid Args");
  78. return;
  79. }
  80. switch (event) {
  81. case CAM_ISP_CTX_EVENT_EPOCH:
  82. case CAM_ISP_CTX_EVENT_RUP:
  83. case CAM_ISP_CTX_EVENT_BUFDONE:
  84. case CAM_ISP_CTX_EVENT_SHUTTER:
  85. break;
  86. case CAM_ISP_CTX_EVENT_SUBMIT:
  87. case CAM_ISP_CTX_EVENT_APPLY:
  88. if (!req) {
  89. CAM_ERR(CAM_ISP, "Invalid arg for event %d", event);
  90. return;
  91. }
  92. break;
  93. default:
  94. break;
  95. }
  96. INC_HEAD(&ctx_isp->dbg_monitors.event_record_head[event],
  97. CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES, &iterator);
  98. cur_time = ktime_get();
  99. if (req) {
  100. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  101. ctx_isp->dbg_monitors.event_record[event][iterator].req_id =
  102. req->request_id;
  103. req_isp->event_timestamp[event] = cur_time;
  104. } else {
  105. ctx_isp->dbg_monitors.event_record[event][iterator].req_id = 0;
  106. }
  107. ctx_isp->dbg_monitors.event_record[event][iterator].timestamp = cur_time;
  108. if (event_data == NULL)
  109. return;
  110. /* Update event specific data */
  111. ctx_isp->dbg_monitors.event_record[event][iterator].event_type = event;
  112. if (event == CAM_ISP_CTX_EVENT_SHUTTER) {
  113. ctx_isp->dbg_monitors.event_record[event][iterator].req_id =
  114. ((struct shutter_event *)event_data)->req_id;
  115. ctx_isp->dbg_monitors.event_record[event][iterator].event.shutter_event.req_id =
  116. ((struct shutter_event *)event_data)->req_id;
  117. ctx_isp->dbg_monitors.event_record[event][iterator].event.shutter_event.status =
  118. ((struct shutter_event *)event_data)->status;
  119. ctx_isp->dbg_monitors.event_record[event][iterator].event.shutter_event.frame_id =
  120. ((struct shutter_event *)event_data)->frame_id;
  121. ctx_isp->dbg_monitors.event_record[event][iterator].event.shutter_event.boot_ts =
  122. ((struct shutter_event *)event_data)->boot_ts;
  123. ctx_isp->dbg_monitors.event_record[event][iterator].event.shutter_event.sof_ts =
  124. ((struct shutter_event *)event_data)->sof_ts;
  125. }
  126. }
  127. static int __cam_isp_ctx_handle_sof_freeze_evt(
  128. struct cam_context *ctx)
  129. {
  130. int rc = 0;
  131. struct cam_isp_context *ctx_isp;
  132. struct cam_hw_cmd_args hw_cmd_args;
  133. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  134. ctx_isp = (struct cam_isp_context *)ctx->ctx_priv;
  135. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  136. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  137. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_SOF_DEBUG;
  138. isp_hw_cmd_args.u.sof_irq_enable = 1;
  139. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  140. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  141. &hw_cmd_args);
  142. ctx_isp->sof_dbg_irq_en = true;
  143. return rc;
  144. }
  145. static void *cam_isp_ctx_user_dump_events(
  146. void *dump_struct, uint8_t *addr_ptr)
  147. {
  148. uint64_t *addr;
  149. struct cam_isp_context_event_record *record;
  150. struct timespec64 ts;
  151. record = (struct cam_isp_context_event_record *)dump_struct;
  152. addr = (uint64_t *)addr_ptr;
  153. ts = ktime_to_timespec64(record->timestamp);
  154. *addr++ = record->req_id;
  155. *addr++ = ts.tv_sec;
  156. *addr++ = ts.tv_nsec / NSEC_PER_USEC;
  157. return addr;
  158. }
  159. static int __cam_isp_ctx_print_event_record(struct cam_isp_context *ctx_isp)
  160. {
  161. int i, j, rc = 0;
  162. int index;
  163. uint32_t oldest_entry, num_entries;
  164. uint64_t state_head;
  165. struct cam_isp_context_event_record *record;
  166. uint32_t len;
  167. uint8_t buf[CAM_ISP_CONTEXT_DBG_BUF_LEN];
  168. struct timespec64 ts;
  169. struct cam_context *ctx = ctx_isp->base;
  170. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++) {
  171. state_head = atomic64_read(&ctx_isp->dbg_monitors.event_record_head[i]);
  172. if (state_head == -1) {
  173. return 0;
  174. } else if (state_head < CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES) {
  175. num_entries = state_head + 1;
  176. oldest_entry = 0;
  177. } else {
  178. num_entries = CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES;
  179. div_u64_rem(state_head + 1,
  180. CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES,
  181. &oldest_entry);
  182. }
  183. index = oldest_entry;
  184. len = 0;
  185. memset(buf, 0, CAM_ISP_CONTEXT_DBG_BUF_LEN);
  186. for (j = 0; j < num_entries; j++) {
  187. record = &ctx_isp->dbg_monitors.event_record[i][index];
  188. ts = ktime_to_timespec64(record->timestamp);
  189. if (len >= CAM_ISP_CONTEXT_DBG_BUF_LEN) {
  190. CAM_WARN(CAM_ISP, "Overshooting buffer length %llu", len);
  191. break;
  192. }
  193. if (record->event_type != CAM_ISP_CTX_EVENT_SHUTTER)
  194. len += scnprintf(buf + len, CAM_ISP_CONTEXT_DBG_BUF_LEN - len,
  195. "%llu[%lld:%06lld] ", record->req_id, ts.tv_sec,
  196. ts.tv_nsec / NSEC_PER_USEC);
  197. else
  198. /*
  199. * Output format
  200. * req Id[timestamp] status frmId softs bootts
  201. */
  202. len += scnprintf(buf + len, (CAM_ISP_CONTEXT_DBG_BUF_LEN) - len,
  203. "%llu[%lld:%06lld] [%d %lld %llu %llu] | ",
  204. record->req_id, ts.tv_sec,
  205. ts.tv_nsec / NSEC_PER_USEC,
  206. record->event.shutter_event.status,
  207. record->event.shutter_event.frame_id,
  208. record->event.shutter_event.sof_ts,
  209. record->event.shutter_event.boot_ts);
  210. index = (index + 1) %
  211. CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES;
  212. }
  213. if (len)
  214. CAM_INFO(CAM_ISP, "Ctx:%d %s: %s",
  215. ctx->ctx_id, __cam_isp_evt_val_to_type(i), buf);
  216. }
  217. return rc;
  218. }
  219. static int __cam_isp_ctx_dump_event_record(
  220. struct cam_isp_context *ctx_isp,
  221. struct cam_common_hw_dump_args *dump_args)
  222. {
  223. int i, j, rc = 0;
  224. int index;
  225. size_t remain_len;
  226. uint32_t oldest_entry, num_entries;
  227. uint32_t min_len;
  228. uint64_t state_head;
  229. struct cam_isp_context_event_record *record;
  230. if (!dump_args || !ctx_isp) {
  231. CAM_ERR(CAM_ISP, "Invalid args %pK %pK",
  232. dump_args, ctx_isp);
  233. return -EINVAL;
  234. }
  235. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++) {
  236. state_head = atomic64_read(&ctx_isp->dbg_monitors.event_record_head[i]);
  237. if (state_head == -1)
  238. return 0;
  239. else if (state_head < CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES) {
  240. num_entries = state_head + 1;
  241. oldest_entry = 0;
  242. } else {
  243. num_entries = CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES;
  244. div_u64_rem(state_head + 1, CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES,
  245. &oldest_entry);
  246. }
  247. index = oldest_entry;
  248. if (dump_args->buf_len <= dump_args->offset) {
  249. CAM_WARN(CAM_ISP, "Dump buffer overshoot len %zu offset %zu",
  250. dump_args->buf_len, dump_args->offset);
  251. return -ENOSPC;
  252. }
  253. min_len = (sizeof(struct cam_isp_context_dump_header) +
  254. (CAM_ISP_CTX_DUMP_EVENT_NUM_WORDS * sizeof(uint64_t))) * num_entries;
  255. remain_len = dump_args->buf_len - dump_args->offset;
  256. if (remain_len < min_len) {
  257. CAM_WARN(CAM_ISP,
  258. "Dump buffer exhaust remain %zu min %u",
  259. remain_len, min_len);
  260. return -ENOSPC;
  261. }
  262. for (j = 0; j < num_entries; j++) {
  263. record = &ctx_isp->dbg_monitors.event_record[i][index];
  264. rc = cam_common_user_dump_helper(dump_args, cam_isp_ctx_user_dump_events,
  265. record, sizeof(uint64_t), "ISP_EVT_%s:",
  266. __cam_isp_evt_val_to_type(i));
  267. if (rc) {
  268. CAM_ERR(CAM_ISP,
  269. "CAM_ISP_CONTEXT DUMP_EVENT_RECORD: Dump failed, rc: %d",
  270. rc);
  271. return rc;
  272. }
  273. index = (index + 1) % CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES;
  274. }
  275. }
  276. return rc;
  277. }
  278. static void __cam_isp_ctx_req_mini_dump(struct cam_ctx_request *req,
  279. uint8_t *start_addr, uint8_t *end_addr,
  280. unsigned long *bytes_updated)
  281. {
  282. struct cam_isp_ctx_req_mini_dump *req_md;
  283. struct cam_buf_io_cfg *io_cfg;
  284. struct cam_isp_ctx_req *req_isp;
  285. struct cam_packet *packet = NULL;
  286. unsigned long bytes_required = 0;
  287. bytes_required = sizeof(*req_md);
  288. *bytes_updated = 0;
  289. if (start_addr + bytes_required > end_addr)
  290. return;
  291. req_md = (struct cam_isp_ctx_req_mini_dump *)start_addr;
  292. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  293. req_md->num_acked = req_isp->num_acked;
  294. req_md->num_deferred_acks = req_isp->num_deferred_acks;
  295. req_md->bubble_report = req_isp->bubble_report;
  296. req_md->bubble_detected = req_isp->bubble_detected;
  297. req_md->reapply_type = req_isp->reapply_type;
  298. req_md->request_id = req->request_id;
  299. *bytes_updated += bytes_required;
  300. if (req_isp->num_fence_map_out) {
  301. bytes_required = sizeof(struct cam_hw_fence_map_entry) *
  302. req_isp->num_fence_map_out;
  303. if (start_addr + *bytes_updated + bytes_required > end_addr)
  304. return;
  305. req_md->map_out = (struct cam_hw_fence_map_entry *)
  306. ((uint8_t *)start_addr + *bytes_updated);
  307. memcpy(req_md->map_out, req_isp->fence_map_out, bytes_required);
  308. req_md->num_fence_map_out = req_isp->num_fence_map_out;
  309. *bytes_updated += bytes_required;
  310. }
  311. if (req_isp->num_fence_map_in) {
  312. bytes_required = sizeof(struct cam_hw_fence_map_entry) *
  313. req_isp->num_fence_map_in;
  314. if (start_addr + *bytes_updated + bytes_required > end_addr)
  315. return;
  316. req_md->map_in = (struct cam_hw_fence_map_entry *)
  317. ((uint8_t *)start_addr + *bytes_updated);
  318. memcpy(req_md->map_in, req_isp->fence_map_in, bytes_required);
  319. req_md->num_fence_map_in = req_isp->num_fence_map_in;
  320. *bytes_updated += bytes_required;
  321. }
  322. packet = req_isp->hw_update_data.packet;
  323. if (packet && packet->num_io_configs) {
  324. bytes_required = packet->num_io_configs * sizeof(struct cam_buf_io_cfg);
  325. if (start_addr + *bytes_updated + bytes_required > end_addr)
  326. return;
  327. io_cfg = (struct cam_buf_io_cfg *)((uint32_t *)&packet->payload +
  328. packet->io_configs_offset / 4);
  329. req_md->io_cfg = (struct cam_buf_io_cfg *)((uint8_t *)start_addr + *bytes_updated);
  330. memcpy(req_md->io_cfg, io_cfg, bytes_required);
  331. *bytes_updated += bytes_required;
  332. req_md->num_io_cfg = packet->num_io_configs;
  333. }
  334. }
  335. static int __cam_isp_ctx_minidump_cb(void *priv, void *args)
  336. {
  337. struct cam_isp_ctx_mini_dump_info *md;
  338. struct cam_isp_context *ctx_isp;
  339. struct cam_context *ctx;
  340. struct cam_ctx_request *req, *req_temp;
  341. struct cam_hw_mini_dump_args *dump_args;
  342. uint8_t *start_addr;
  343. uint8_t *end_addr;
  344. unsigned long total_bytes = 0;
  345. unsigned long bytes_updated = 0;
  346. uint32_t i;
  347. if (!priv || !args) {
  348. CAM_ERR(CAM_ISP, "invalid params");
  349. return 0;
  350. }
  351. dump_args = (struct cam_hw_mini_dump_args *)args;
  352. if (dump_args->len < sizeof(*md)) {
  353. CAM_ERR(CAM_ISP,
  354. "In sufficient size received %lu required size: %zu",
  355. dump_args->len, sizeof(*md));
  356. return 0;
  357. }
  358. ctx = (struct cam_context *)priv;
  359. ctx_isp = (struct cam_isp_context *)ctx->ctx_priv;
  360. start_addr = (uint8_t *)dump_args->start_addr;
  361. end_addr = start_addr + dump_args->len;
  362. md = (struct cam_isp_ctx_mini_dump_info *)dump_args->start_addr;
  363. md->sof_timestamp_val = ctx_isp->sof_timestamp_val;
  364. md->boot_timestamp = ctx_isp->boot_timestamp;
  365. md->last_sof_timestamp = ctx_isp->last_sof_timestamp;
  366. md->init_timestamp = ctx_isp->init_timestamp;
  367. md->frame_id = ctx_isp->frame_id;
  368. md->reported_req_id = ctx_isp->reported_req_id;
  369. md->last_applied_req_id = ctx_isp->last_applied_req_id;
  370. md->last_bufdone_err_apply_req_id =
  371. ctx_isp->last_bufdone_err_apply_req_id;
  372. md->frame_id_meta = ctx_isp->frame_id_meta;
  373. md->substate_activated = ctx_isp->substate_activated;
  374. md->ctx_id = ctx->ctx_id;
  375. md->subscribe_event = ctx_isp->subscribe_event;
  376. md->bubble_frame_cnt = ctx_isp->bubble_frame_cnt;
  377. md->isp_device_type = ctx_isp->isp_device_type;
  378. md->active_req_cnt = ctx_isp->active_req_cnt;
  379. md->trigger_id = ctx_isp->trigger_id;
  380. md->rdi_only_context = ctx_isp->rdi_only_context;
  381. md->offline_context = ctx_isp->offline_context;
  382. md->hw_acquired = ctx_isp->hw_acquired;
  383. md->init_received = ctx_isp->init_received;
  384. md->split_acquire = ctx_isp->split_acquire;
  385. md->use_frame_header_ts = ctx_isp->use_frame_header_ts;
  386. md->support_consumed_addr = ctx_isp->support_consumed_addr;
  387. md->use_default_apply = ctx_isp->use_default_apply;
  388. md->apply_in_progress = atomic_read(&ctx_isp->apply_in_progress);
  389. md->process_bubble = atomic_read(&ctx_isp->process_bubble);
  390. md->rxd_epoch = atomic_read(&ctx_isp->rxd_epoch);
  391. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++) {
  392. memcpy(md->event_record[i], ctx_isp->dbg_monitors.event_record[i],
  393. sizeof(struct cam_isp_context_event_record) *
  394. CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES);
  395. }
  396. total_bytes += sizeof(*md);
  397. if (start_addr + total_bytes >= end_addr)
  398. goto end;
  399. if (!list_empty(&ctx->active_req_list)) {
  400. md->active_list = (struct cam_isp_ctx_req_mini_dump *)
  401. (start_addr + total_bytes);
  402. list_for_each_entry_safe(req, req_temp, &ctx->active_req_list, list) {
  403. bytes_updated = 0;
  404. __cam_isp_ctx_req_mini_dump(req,
  405. (uint8_t *)&md->active_list[md->active_cnt++],
  406. end_addr, &bytes_updated);
  407. total_bytes += bytes_updated;
  408. if ((start_addr + total_bytes >= end_addr))
  409. goto end;
  410. }
  411. }
  412. if (!list_empty(&ctx->wait_req_list)) {
  413. md->wait_list = (struct cam_isp_ctx_req_mini_dump *)
  414. (start_addr + total_bytes);
  415. list_for_each_entry_safe(req, req_temp, &ctx->wait_req_list, list) {
  416. bytes_updated = 0;
  417. __cam_isp_ctx_req_mini_dump(req,
  418. (uint8_t *)&md->wait_list[md->wait_cnt++],
  419. end_addr, &bytes_updated);
  420. total_bytes += bytes_updated;
  421. if ((start_addr + total_bytes >= end_addr))
  422. goto end;
  423. }
  424. }
  425. if (!list_empty(&ctx->pending_req_list)) {
  426. md->pending_list = (struct cam_isp_ctx_req_mini_dump *)
  427. (start_addr + total_bytes);
  428. list_for_each_entry_safe(req, req_temp, &ctx->pending_req_list, list) {
  429. bytes_updated = 0;
  430. __cam_isp_ctx_req_mini_dump(req,
  431. (uint8_t *)&md->pending_list[md->pending_cnt++],
  432. end_addr, &bytes_updated);
  433. total_bytes += bytes_updated;
  434. if ((start_addr + total_bytes >= end_addr))
  435. goto end;
  436. }
  437. }
  438. end:
  439. dump_args->bytes_written = total_bytes;
  440. return 0;
  441. }
  442. static void __cam_isp_ctx_update_state_monitor_array(
  443. struct cam_isp_context *ctx_isp,
  444. enum cam_isp_state_change_trigger trigger_type,
  445. uint64_t req_id)
  446. {
  447. int iterator;
  448. struct cam_context *ctx = ctx_isp->base;
  449. INC_HEAD(&ctx_isp->dbg_monitors.state_monitor_head,
  450. CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES, &iterator);
  451. ctx_isp->dbg_monitors.state_monitor[iterator].curr_state =
  452. ctx_isp->substate_activated;
  453. ctx_isp->dbg_monitors.state_monitor[iterator].frame_id =
  454. ctx_isp->frame_id;
  455. ctx_isp->dbg_monitors.state_monitor[iterator].trigger =
  456. trigger_type;
  457. ctx_isp->dbg_monitors.state_monitor[iterator].req_id =
  458. req_id;
  459. if (trigger_type == CAM_ISP_STATE_CHANGE_TRIGGER_CDM_DONE)
  460. ctx_isp->dbg_monitors.state_monitor[iterator].evt_time_stamp =
  461. ctx->cdm_done_ts;
  462. else
  463. ktime_get_clocktai_ts64(
  464. &ctx_isp->dbg_monitors.state_monitor[iterator].evt_time_stamp);
  465. }
  466. static int __cam_isp_ctx_update_frame_timing_record(
  467. enum cam_isp_hw_event_type hw_evt, struct cam_isp_context *ctx_isp)
  468. {
  469. uint32_t index = 0;
  470. /* Update event index on IFE SOF - primary event */
  471. if (hw_evt == CAM_ISP_HW_EVENT_SOF)
  472. INC_HEAD(&ctx_isp->dbg_monitors.frame_monitor_head,
  473. CAM_ISP_CTX_MAX_FRAME_RECORDS, &index);
  474. else
  475. div_u64_rem(atomic64_read(&ctx_isp->dbg_monitors.frame_monitor_head),
  476. CAM_ISP_CTX_MAX_FRAME_RECORDS, &index);
  477. switch (hw_evt) {
  478. case CAM_ISP_HW_EVENT_SOF:
  479. CAM_GET_BOOT_TIMESTAMP(ctx_isp->dbg_monitors.frame_monitor[index].sof_ts);
  480. break;
  481. case CAM_ISP_HW_EVENT_EOF:
  482. CAM_GET_BOOT_TIMESTAMP(ctx_isp->dbg_monitors.frame_monitor[index].eof_ts);
  483. break;
  484. case CAM_ISP_HW_EVENT_EPOCH:
  485. CAM_GET_BOOT_TIMESTAMP(ctx_isp->dbg_monitors.frame_monitor[index].epoch_ts);
  486. break;
  487. case CAM_ISP_HW_SECONDARY_EVENT:
  488. CAM_GET_BOOT_TIMESTAMP(ctx_isp->dbg_monitors.frame_monitor[index].secondary_sof_ts);
  489. break;
  490. default:
  491. break;
  492. }
  493. return 0;
  494. }
  495. static void __cam_isp_ctx_dump_frame_timing_record(
  496. struct cam_isp_context *ctx_isp)
  497. {
  498. int i = 0;
  499. int64_t state_head = 0;
  500. uint32_t index, num_entries, oldest_entry;
  501. state_head = atomic64_read(&ctx_isp->dbg_monitors.frame_monitor_head);
  502. if (state_head == -1)
  503. return;
  504. if (state_head < CAM_ISP_CTX_MAX_FRAME_RECORDS) {
  505. num_entries = state_head + 1;
  506. oldest_entry = 0;
  507. } else {
  508. num_entries = CAM_ISP_CTX_MAX_FRAME_RECORDS;
  509. div_u64_rem(state_head + 1, CAM_ISP_CTX_MAX_FRAME_RECORDS,
  510. &oldest_entry);
  511. }
  512. index = oldest_entry;
  513. for (i = 0; i < num_entries; i++) {
  514. CAM_INFO(CAM_ISP,
  515. "Index: %u SOF_TS: %llu.%llu EPOCH_TS: %llu.%llu EOF_TS: %llu.%llu SEC_SOF: %llu.%llu",
  516. index,
  517. ctx_isp->dbg_monitors.frame_monitor[index].sof_ts.tv_sec,
  518. ctx_isp->dbg_monitors.frame_monitor[index].sof_ts.tv_nsec / NSEC_PER_USEC,
  519. ctx_isp->dbg_monitors.frame_monitor[index].epoch_ts.tv_sec,
  520. ctx_isp->dbg_monitors.frame_monitor[index].epoch_ts.tv_nsec / NSEC_PER_USEC,
  521. ctx_isp->dbg_monitors.frame_monitor[index].eof_ts.tv_sec,
  522. ctx_isp->dbg_monitors.frame_monitor[index].eof_ts.tv_nsec / NSEC_PER_USEC,
  523. ctx_isp->dbg_monitors.frame_monitor[index].secondary_sof_ts.tv_sec,
  524. ctx_isp->dbg_monitors.frame_monitor[index].secondary_sof_ts.tv_nsec /
  525. NSEC_PER_USEC);
  526. index = (index + 1) % CAM_ISP_CTX_MAX_FRAME_RECORDS;
  527. }
  528. }
  529. static const char *__cam_isp_ctx_substate_val_to_type(
  530. enum cam_isp_ctx_activated_substate type)
  531. {
  532. switch (type) {
  533. case CAM_ISP_CTX_ACTIVATED_SOF:
  534. return "SOF";
  535. case CAM_ISP_CTX_ACTIVATED_APPLIED:
  536. return "APPLIED";
  537. case CAM_ISP_CTX_ACTIVATED_EPOCH:
  538. return "EPOCH";
  539. case CAM_ISP_CTX_ACTIVATED_BUBBLE:
  540. return "BUBBLE";
  541. case CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED:
  542. return "BUBBLE_APPLIED";
  543. case CAM_ISP_CTX_ACTIVATED_HW_ERROR:
  544. return "HW_ERROR";
  545. case CAM_ISP_CTX_ACTIVATED_HALT:
  546. return "HALT";
  547. default:
  548. return "INVALID";
  549. }
  550. }
  551. static const char *__cam_isp_hw_evt_val_to_type(
  552. uint32_t evt_id)
  553. {
  554. switch (evt_id) {
  555. case CAM_ISP_STATE_CHANGE_TRIGGER_ERROR:
  556. return "ERROR";
  557. case CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED:
  558. return "APPLIED";
  559. case CAM_ISP_STATE_CHANGE_TRIGGER_SOF:
  560. return "SOF";
  561. case CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE:
  562. return "REG_UPDATE";
  563. case CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH:
  564. return "EPOCH";
  565. case CAM_ISP_STATE_CHANGE_TRIGGER_EOF:
  566. return "EOF";
  567. case CAM_ISP_STATE_CHANGE_TRIGGER_CDM_DONE:
  568. return "CDM_DONE";
  569. case CAM_ISP_STATE_CHANGE_TRIGGER_DONE:
  570. return "DONE";
  571. case CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH:
  572. return "FLUSH";
  573. case CAM_ISP_STATE_CHANGE_TRIGGER_SEC_EVT_SOF:
  574. return "SEC_EVT_SOF";
  575. case CAM_ISP_STATE_CHANGE_TRIGGER_SEC_EVT_EPOCH:
  576. return "SEC_EVT_EPOCH";
  577. case CAM_ISP_STATE_CHANGE_TRIGGER_FRAME_DROP:
  578. return "OUT_OF_SYNC_FRAME_DROP";
  579. default:
  580. return "CAM_ISP_EVENT_INVALID";
  581. }
  582. }
  583. static void __cam_isp_ctx_dump_state_monitor_array(
  584. struct cam_isp_context *ctx_isp)
  585. {
  586. int i = 0;
  587. int64_t state_head = 0;
  588. uint32_t index, num_entries, oldest_entry;
  589. struct tm ts;
  590. state_head = atomic64_read(&ctx_isp->dbg_monitors.state_monitor_head);
  591. if (state_head == -1)
  592. return;
  593. if (state_head < CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES) {
  594. num_entries = state_head + 1;
  595. oldest_entry = 0;
  596. } else {
  597. num_entries = CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES;
  598. div_u64_rem(state_head + 1, CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES,
  599. &oldest_entry);
  600. }
  601. CAM_ERR(CAM_ISP,
  602. "Dumping state information for preceding requests");
  603. index = oldest_entry;
  604. for (i = 0; i < num_entries; i++) {
  605. time64_to_tm(ctx_isp->dbg_monitors.state_monitor[index].evt_time_stamp.tv_sec,
  606. 0, &ts);
  607. CAM_ERR(CAM_ISP,
  608. "Idx[%d] time[%d-%d %d:%d:%d.%lld]:Substate[%s] Frame[%lld] Req[%llu] evt[%s]",
  609. index, ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min, ts.tm_sec,
  610. ctx_isp->dbg_monitors.state_monitor[index].evt_time_stamp.tv_nsec / 1000000,
  611. __cam_isp_ctx_substate_val_to_type(
  612. ctx_isp->dbg_monitors.state_monitor[index].curr_state),
  613. ctx_isp->dbg_monitors.state_monitor[index].frame_id,
  614. ctx_isp->dbg_monitors.state_monitor[index].req_id,
  615. __cam_isp_hw_evt_val_to_type(
  616. ctx_isp->dbg_monitors.state_monitor[index].trigger));
  617. index = (index + 1) % CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES;
  618. }
  619. }
  620. static void *cam_isp_ctx_user_dump_state_monitor_array_info(
  621. void *dump_struct, uint8_t *addr_ptr)
  622. {
  623. struct cam_isp_context_state_monitor *evt = NULL;
  624. uint64_t *addr;
  625. evt = (struct cam_isp_context_state_monitor *)dump_struct;
  626. addr = (uint64_t *)addr_ptr;
  627. *addr++ = evt->evt_time_stamp.tv_sec;
  628. *addr++ = evt->evt_time_stamp.tv_nsec / NSEC_PER_USEC;
  629. *addr++ = evt->frame_id;
  630. *addr++ = evt->req_id;
  631. return addr;
  632. }
  633. static int __cam_isp_ctx_user_dump_state_monitor_array(
  634. struct cam_isp_context *ctx_isp,
  635. struct cam_common_hw_dump_args *dump_args)
  636. {
  637. int i, rc = 0;
  638. int index;
  639. uint32_t oldest_entry;
  640. uint32_t num_entries;
  641. uint64_t state_head = 0;
  642. if (!dump_args || !ctx_isp) {
  643. CAM_ERR(CAM_ISP, "Invalid args %pK %pK",
  644. dump_args, ctx_isp);
  645. return -EINVAL;
  646. }
  647. state_head = atomic64_read(&ctx_isp->dbg_monitors.state_monitor_head);
  648. if (state_head == -1) {
  649. return 0;
  650. } else if (state_head < CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES) {
  651. num_entries = state_head;
  652. oldest_entry = 0;
  653. } else {
  654. num_entries = CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES;
  655. div_u64_rem(state_head + 1,
  656. CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES, &oldest_entry);
  657. }
  658. CAM_ERR(CAM_ISP,
  659. "Dumping state information for preceding requests");
  660. index = oldest_entry;
  661. for (i = 0; i < num_entries; i++) {
  662. rc = cam_common_user_dump_helper(dump_args,
  663. cam_isp_ctx_user_dump_state_monitor_array_info,
  664. &ctx_isp->dbg_monitors.state_monitor[index],
  665. sizeof(uint64_t), "ISP_STATE_MONITOR.%s.%s:",
  666. __cam_isp_ctx_substate_val_to_type(
  667. ctx_isp->dbg_monitors.state_monitor[index].curr_state),
  668. __cam_isp_hw_evt_val_to_type(
  669. ctx_isp->dbg_monitors.state_monitor[index].trigger));
  670. if (rc) {
  671. CAM_ERR(CAM_ISP, "CAM ISP CONTEXT: Event record dump failed, rc: %d", rc);
  672. return rc;
  673. }
  674. index = (index + 1) % CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES;
  675. }
  676. return rc;
  677. }
  678. static int cam_isp_context_info_dump(void *context,
  679. enum cam_context_dump_id id)
  680. {
  681. struct cam_context *ctx = (struct cam_context *)context;
  682. switch (id) {
  683. case CAM_CTX_DUMP_ACQ_INFO: {
  684. cam_context_dump_hw_acq_info(ctx);
  685. break;
  686. }
  687. default:
  688. CAM_DBG(CAM_ISP, "DUMP id not valid %u, ctx_idx: %u, link: 0x%x",
  689. id, ctx->ctx_id, ctx->link_hdl);
  690. break;
  691. }
  692. return 0;
  693. }
  694. static const char *__cam_isp_ctx_crm_trigger_point_to_string(
  695. int trigger_point)
  696. {
  697. switch (trigger_point) {
  698. case CAM_TRIGGER_POINT_SOF:
  699. return "SOF";
  700. case CAM_TRIGGER_POINT_EOF:
  701. return "EOF";
  702. default:
  703. return "Invalid";
  704. }
  705. }
  706. static int __cam_isp_ctx_notify_trigger_util(
  707. int trigger_type, struct cam_isp_context *ctx_isp)
  708. {
  709. int rc = -EINVAL;
  710. struct cam_context *ctx = ctx_isp->base;
  711. struct cam_req_mgr_trigger_notify notify;
  712. /* Trigger type not supported, return */
  713. if (!(ctx_isp->subscribe_event & trigger_type)) {
  714. CAM_DBG(CAM_ISP,
  715. "%s trigger point not subscribed for in mask: %u in ctx: %u on link: 0x%x last_bufdone: %lld",
  716. __cam_isp_ctx_crm_trigger_point_to_string(trigger_type),
  717. ctx_isp->subscribe_event, ctx->ctx_id, ctx->link_hdl,
  718. ctx_isp->req_info.last_bufdone_req_id);
  719. return 0;
  720. }
  721. /* Skip CRM notify when recovery is in progress */
  722. if (atomic_read(&ctx_isp->internal_recovery_set)) {
  723. CAM_DBG(CAM_ISP,
  724. "Internal recovery in progress skip notifying %s trigger point in ctx: %u on link: 0x%x",
  725. __cam_isp_ctx_crm_trigger_point_to_string(trigger_type),
  726. ctx->ctx_id, ctx->link_hdl);
  727. return 0;
  728. }
  729. notify.link_hdl = ctx->link_hdl;
  730. notify.dev_hdl = ctx->dev_hdl;
  731. notify.frame_id = ctx_isp->frame_id;
  732. notify.trigger = trigger_type;
  733. notify.req_id = ctx_isp->req_info.last_bufdone_req_id;
  734. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  735. notify.trigger_id = ctx_isp->trigger_id;
  736. CAM_DBG(CAM_ISP,
  737. "Notify CRM %s on frame: %llu ctx: %u link: 0x%x last_buf_done_req: %lld",
  738. __cam_isp_ctx_crm_trigger_point_to_string(trigger_type),
  739. ctx_isp->frame_id, ctx->ctx_id, ctx->link_hdl,
  740. ctx_isp->req_info.last_bufdone_req_id);
  741. rc = ctx->ctx_crm_intf->notify_trigger(&notify);
  742. if (rc)
  743. CAM_ERR_RATE_LIMIT(CAM_ISP,
  744. "Failed to notify CRM %s on frame: %llu ctx: %u link: 0x%x last_buf_done_req: %lld rc: %d",
  745. __cam_isp_ctx_crm_trigger_point_to_string(trigger_type),
  746. ctx_isp->frame_id, ctx->ctx_id, ctx->link_hdl,
  747. ctx_isp->req_info.last_bufdone_req_id, rc);
  748. return rc;
  749. }
  750. static int __cam_isp_ctx_notify_v4l2_error_event(
  751. uint32_t error_type, uint32_t error_code,
  752. uint64_t error_request_id, struct cam_context *ctx)
  753. {
  754. int rc = 0;
  755. struct cam_req_mgr_message req_msg;
  756. req_msg.session_hdl = ctx->session_hdl;
  757. req_msg.u.err_msg.device_hdl = ctx->dev_hdl;
  758. req_msg.u.err_msg.error_type = error_type;
  759. req_msg.u.err_msg.link_hdl = ctx->link_hdl;
  760. req_msg.u.err_msg.request_id = error_request_id;
  761. req_msg.u.err_msg.resource_size = 0x0;
  762. req_msg.u.err_msg.error_code = error_code;
  763. CAM_DBG(CAM_ISP,
  764. "v4l2 error event [type: %u code: %u] for req: %llu in ctx: %u on link: 0x%x notified successfully",
  765. error_type, error_code, error_request_id, ctx->ctx_id, ctx->link_hdl);
  766. rc = cam_req_mgr_notify_message(&req_msg,
  767. V4L_EVENT_CAM_REQ_MGR_ERROR,
  768. V4L_EVENT_CAM_REQ_MGR_EVENT);
  769. if (rc)
  770. CAM_ERR(CAM_ISP,
  771. "Notifying v4l2 error [type: %u code: %u] failed for req id:%llu in ctx %u on link: 0x%x",
  772. error_type, error_code, error_request_id, ctx->ctx_id, ctx->link_hdl);
  773. return rc;
  774. }
  775. static int __cam_isp_ctx_notify_error_util(
  776. uint32_t trigger_type, enum cam_req_mgr_device_error error,
  777. uint64_t req_id, struct cam_isp_context *ctx_isp)
  778. {
  779. int rc = -EINVAL;
  780. struct cam_context *ctx = ctx_isp->base;
  781. struct cam_req_mgr_error_notify notify;
  782. notify.link_hdl = ctx->link_hdl;
  783. notify.dev_hdl = ctx->dev_hdl;
  784. notify.req_id = req_id;
  785. notify.error = error;
  786. notify.trigger = trigger_type;
  787. notify.frame_id = ctx_isp->frame_id;
  788. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  789. if ((error == CRM_KMD_ERR_BUBBLE) || (error == CRM_KMD_WARN_INTERNAL_RECOVERY))
  790. CAM_WARN(CAM_ISP,
  791. "Notify CRM about bubble req: %llu frame: %llu in ctx: %u on link: 0x%x",
  792. req_id, ctx_isp->frame_id, ctx->ctx_id, ctx->link_hdl);
  793. else
  794. CAM_ERR(CAM_ISP,
  795. "Notify CRM about fatal error: %u req: %llu frame: %llu in ctx: %u on link: 0x%x",
  796. error, req_id, ctx_isp->frame_id, ctx->ctx_id, ctx->link_hdl);
  797. rc = ctx->ctx_crm_intf->notify_err(&notify);
  798. if (rc)
  799. CAM_ERR(CAM_ISP,
  800. "Failed to notify error: %u for req: %lu on ctx: %u in link: 0x%x",
  801. error, req_id, ctx->ctx_id, ctx->link_hdl);
  802. return rc;
  803. }
  804. static int __cam_isp_ctx_trigger_reg_dump(
  805. enum cam_hw_mgr_command cmd,
  806. struct cam_context *ctx)
  807. {
  808. int rc = 0;
  809. struct cam_hw_cmd_args hw_cmd_args;
  810. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  811. hw_cmd_args.cmd_type = cmd;
  812. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  813. &hw_cmd_args);
  814. if (rc) {
  815. CAM_ERR(CAM_ISP, "Reg dump on error failed ctx: %u link: 0x%x rc: %d",
  816. ctx->ctx_id, ctx->link_hdl, rc);
  817. goto end;
  818. }
  819. CAM_DBG(CAM_ISP,
  820. "Reg dump type: %u successful in ctx: %u on link: 0x%x",
  821. cmd, ctx->ctx_id, ctx->link_hdl);
  822. end:
  823. return rc;
  824. }
  825. static int __cam_isp_ctx_pause_crm_timer(
  826. struct cam_context *ctx)
  827. {
  828. int rc = -EINVAL;
  829. struct cam_req_mgr_timer_notify timer;
  830. if (!ctx || !ctx->ctx_crm_intf)
  831. goto end;
  832. timer.link_hdl = ctx->link_hdl;
  833. timer.dev_hdl = ctx->dev_hdl;
  834. timer.state = false;
  835. rc = ctx->ctx_crm_intf->notify_timer(&timer);
  836. if (rc) {
  837. CAM_ERR(CAM_ISP, "Failed to pause sof timer in ctx: %u on link: 0x%x",
  838. ctx->ctx_id, ctx->link_hdl);
  839. goto end;
  840. }
  841. CAM_DBG(CAM_ISP, "Notify CRM to pause timer for ctx: %u link: 0x%x success",
  842. ctx->ctx_id, ctx->link_hdl);
  843. end:
  844. return rc;
  845. }
  846. static inline void __cam_isp_ctx_update_sof_ts_util(
  847. struct cam_isp_hw_sof_event_data *sof_event_data,
  848. struct cam_isp_context *ctx_isp)
  849. {
  850. /* Delayed update, skip if ts is already updated */
  851. if (ctx_isp->sof_timestamp_val == sof_event_data->timestamp)
  852. return;
  853. ctx_isp->frame_id++;
  854. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  855. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  856. }
  857. static int cam_isp_ctx_dump_req(
  858. struct cam_isp_ctx_req *req_isp,
  859. uintptr_t cpu_addr,
  860. size_t buf_len,
  861. size_t *offset,
  862. bool dump_to_buff)
  863. {
  864. int i, rc = 0;
  865. size_t len = 0;
  866. uint32_t *buf_addr;
  867. uint32_t *buf_start, *buf_end;
  868. size_t remain_len = 0;
  869. struct cam_cdm_cmd_buf_dump_info dump_info;
  870. for (i = 0; i < req_isp->num_cfg; i++) {
  871. rc = cam_packet_util_get_cmd_mem_addr(
  872. req_isp->cfg[i].handle, &buf_addr, &len);
  873. if (rc) {
  874. CAM_ERR_RATE_LIMIT(CAM_ISP,
  875. "Failed to get_cmd_mem_addr, rc=%d",
  876. rc);
  877. } else {
  878. if (req_isp->cfg[i].offset >= ((uint32_t)len)) {
  879. CAM_ERR(CAM_ISP,
  880. "Invalid offset exp %u actual %u",
  881. req_isp->cfg[i].offset, (uint32_t)len);
  882. cam_mem_put_cpu_buf(req_isp->cfg[i].handle);
  883. return -EINVAL;
  884. }
  885. remain_len = len - req_isp->cfg[i].offset;
  886. if (req_isp->cfg[i].len >
  887. ((uint32_t)remain_len)) {
  888. CAM_ERR(CAM_ISP,
  889. "Invalid len exp %u remain_len %u",
  890. req_isp->cfg[i].len,
  891. (uint32_t)remain_len);
  892. cam_mem_put_cpu_buf(req_isp->cfg[i].handle);
  893. return -EINVAL;
  894. }
  895. buf_start = (uint32_t *)((uint8_t *) buf_addr +
  896. req_isp->cfg[i].offset);
  897. buf_end = (uint32_t *)((uint8_t *) buf_start +
  898. req_isp->cfg[i].len - 1);
  899. if (dump_to_buff) {
  900. if (!cpu_addr || !offset || !buf_len) {
  901. CAM_ERR(CAM_ISP, "Invalid args");
  902. cam_mem_put_cpu_buf(req_isp->cfg[i].handle);
  903. break;
  904. }
  905. dump_info.src_start = buf_start;
  906. dump_info.src_end = buf_end;
  907. dump_info.dst_start = cpu_addr;
  908. dump_info.dst_offset = *offset;
  909. dump_info.dst_max_size = buf_len;
  910. rc = cam_cdm_util_dump_cmd_bufs_v2(
  911. &dump_info);
  912. *offset = dump_info.dst_offset;
  913. if (rc) {
  914. cam_mem_put_cpu_buf(req_isp->cfg[i].handle);
  915. return rc;
  916. }
  917. } else
  918. cam_cdm_util_dump_cmd_buf(buf_start, buf_end);
  919. cam_mem_put_cpu_buf(req_isp->cfg[i].handle);
  920. }
  921. }
  922. return rc;
  923. }
  924. static int __cam_isp_ctx_enqueue_request_in_order(
  925. struct cam_context *ctx, struct cam_ctx_request *req, bool lock)
  926. {
  927. struct cam_ctx_request *req_current;
  928. struct cam_ctx_request *req_prev;
  929. struct list_head temp_list;
  930. struct cam_isp_context *ctx_isp;
  931. INIT_LIST_HEAD(&temp_list);
  932. if (lock)
  933. spin_lock_bh(&ctx->lock);
  934. if (list_empty(&ctx->pending_req_list)) {
  935. list_add_tail(&req->list, &ctx->pending_req_list);
  936. } else {
  937. list_for_each_entry_safe_reverse(
  938. req_current, req_prev, &ctx->pending_req_list, list) {
  939. if (req->request_id < req_current->request_id) {
  940. list_del_init(&req_current->list);
  941. list_add(&req_current->list, &temp_list);
  942. continue;
  943. } else if (req->request_id == req_current->request_id) {
  944. CAM_WARN(CAM_ISP,
  945. "Received duplicated request %lld, ctx_idx: %u link: 0x%x",
  946. req->request_id, ctx->ctx_id, ctx->link_hdl);
  947. }
  948. break;
  949. }
  950. list_add_tail(&req->list, &ctx->pending_req_list);
  951. if (!list_empty(&temp_list)) {
  952. list_for_each_entry_safe(
  953. req_current, req_prev, &temp_list, list) {
  954. list_del_init(&req_current->list);
  955. list_add_tail(&req_current->list,
  956. &ctx->pending_req_list);
  957. }
  958. }
  959. }
  960. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  961. __cam_isp_ctx_update_event_record(ctx_isp,
  962. CAM_ISP_CTX_EVENT_SUBMIT, req, NULL);
  963. if (lock)
  964. spin_unlock_bh(&ctx->lock);
  965. return 0;
  966. }
  967. static int __cam_isp_ctx_enqueue_init_request(
  968. struct cam_context *ctx, struct cam_ctx_request *req)
  969. {
  970. int rc = 0;
  971. struct cam_ctx_request *req_old;
  972. struct cam_isp_ctx_req *req_isp_old;
  973. struct cam_isp_ctx_req *req_isp_new;
  974. struct cam_isp_prepare_hw_update_data *req_update_old;
  975. struct cam_isp_prepare_hw_update_data *req_update_new;
  976. struct cam_isp_prepare_hw_update_data *hw_update_data;
  977. struct cam_isp_fcg_config_info *fcg_info_old;
  978. struct cam_isp_fcg_config_info *fcg_info_new;
  979. spin_lock_bh(&ctx->lock);
  980. if (list_empty(&ctx->pending_req_list)) {
  981. list_add_tail(&req->list, &ctx->pending_req_list);
  982. CAM_DBG(CAM_ISP, "INIT packet added req id= %d, ctx_idx: %u, link: 0x%x",
  983. req->request_id, ctx->ctx_id, ctx->link_hdl);
  984. goto end;
  985. }
  986. req_old = list_first_entry(&ctx->pending_req_list,
  987. struct cam_ctx_request, list);
  988. req_isp_old = (struct cam_isp_ctx_req *) req_old->req_priv;
  989. req_isp_new = (struct cam_isp_ctx_req *) req->req_priv;
  990. if (req_isp_old->hw_update_data.packet_opcode_type ==
  991. CAM_ISP_PACKET_INIT_DEV) {
  992. if ((req_isp_old->num_cfg + req_isp_new->num_cfg) >=
  993. ctx->max_hw_update_entries) {
  994. CAM_WARN(CAM_ISP,
  995. "Can not merge INIT pkt num_cfgs = %d, ctx_idx: %u, link: 0x%x",
  996. (req_isp_old->num_cfg +
  997. req_isp_new->num_cfg), ctx->ctx_id, ctx->link_hdl);
  998. rc = -ENOMEM;
  999. }
  1000. if (req_isp_old->num_fence_map_out != 0 ||
  1001. req_isp_old->num_fence_map_in != 0) {
  1002. CAM_WARN(CAM_ISP, "Invalid INIT pkt sequence, ctx_idx: %u, link: 0x%x",
  1003. ctx->ctx_id, ctx->link_hdl);
  1004. rc = -EINVAL;
  1005. }
  1006. if (!rc) {
  1007. memcpy(req_isp_old->fence_map_out,
  1008. req_isp_new->fence_map_out,
  1009. sizeof(req_isp_new->fence_map_out[0])*
  1010. req_isp_new->num_fence_map_out);
  1011. req_isp_old->num_fence_map_out =
  1012. req_isp_new->num_fence_map_out;
  1013. memcpy(req_isp_old->fence_map_in,
  1014. req_isp_new->fence_map_in,
  1015. sizeof(req_isp_new->fence_map_in[0])*
  1016. req_isp_new->num_fence_map_in);
  1017. req_isp_old->num_fence_map_in =
  1018. req_isp_new->num_fence_map_in;
  1019. /* Copy hw update entries, num_cfg is updated later */
  1020. memcpy(&req_isp_old->cfg[req_isp_old->num_cfg],
  1021. req_isp_new->cfg,
  1022. sizeof(req_isp_new->cfg[0]) *
  1023. req_isp_new->num_cfg);
  1024. memcpy(&req_old->pf_data, &req->pf_data,
  1025. sizeof(struct cam_hw_mgr_pf_request_info));
  1026. if (req_isp_new->hw_update_data.num_reg_dump_buf) {
  1027. req_update_new = &req_isp_new->hw_update_data;
  1028. req_update_old = &req_isp_old->hw_update_data;
  1029. memcpy(&req_update_old->reg_dump_buf_desc,
  1030. &req_update_new->reg_dump_buf_desc,
  1031. sizeof(struct cam_cmd_buf_desc) *
  1032. req_update_new->num_reg_dump_buf);
  1033. req_update_old->num_reg_dump_buf =
  1034. req_update_new->num_reg_dump_buf;
  1035. }
  1036. /* Update HW update params for ePCR */
  1037. hw_update_data = &req_isp_new->hw_update_data;
  1038. req_isp_old->hw_update_data.frame_header_res_id =
  1039. req_isp_new->hw_update_data.frame_header_res_id;
  1040. req_isp_old->hw_update_data.frame_header_cpu_addr =
  1041. hw_update_data->frame_header_cpu_addr;
  1042. if (req_isp_new->hw_update_data.mup_en) {
  1043. req_isp_old->hw_update_data.mup_en =
  1044. req_isp_new->hw_update_data.mup_en;
  1045. req_isp_old->hw_update_data.mup_val =
  1046. req_isp_new->hw_update_data.mup_val;
  1047. req_isp_old->hw_update_data.num_exp =
  1048. req_isp_new->hw_update_data.num_exp;
  1049. }
  1050. /* Copy FCG HW update params */
  1051. fcg_info_new = &hw_update_data->fcg_info;
  1052. fcg_info_old = &req_isp_old->hw_update_data.fcg_info;
  1053. fcg_info_old->use_current_cfg = true;
  1054. if (fcg_info_new->ife_fcg_online) {
  1055. fcg_info_old->ife_fcg_online = true;
  1056. fcg_info_old->ife_fcg_entry_idx =
  1057. req_isp_old->num_cfg +
  1058. fcg_info_new->ife_fcg_entry_idx;
  1059. memcpy(&fcg_info_old->ife_fcg_config,
  1060. &fcg_info_new->ife_fcg_config,
  1061. sizeof(struct cam_isp_fcg_config_internal));
  1062. }
  1063. if (fcg_info_new->sfe_fcg_online) {
  1064. fcg_info_old->sfe_fcg_online = true;
  1065. fcg_info_old->sfe_fcg_entry_idx =
  1066. req_isp_old->num_cfg +
  1067. fcg_info_new->sfe_fcg_entry_idx;
  1068. memcpy(&fcg_info_old->sfe_fcg_config,
  1069. &fcg_info_new->sfe_fcg_config,
  1070. sizeof(struct cam_isp_fcg_config_internal));
  1071. }
  1072. req_isp_old->num_cfg += req_isp_new->num_cfg;
  1073. req_old->request_id = req->request_id;
  1074. list_splice_init(&req->buf_tracker, &req_old->buf_tracker);
  1075. list_add_tail(&req->list, &ctx->free_req_list);
  1076. }
  1077. } else {
  1078. CAM_WARN(CAM_ISP,
  1079. "Received Update pkt before INIT pkt. req_id= %lld, ctx_idx: %u, link: 0x%x",
  1080. req->request_id, ctx->ctx_id, ctx->link_hdl);
  1081. rc = -EINVAL;
  1082. }
  1083. end:
  1084. spin_unlock_bh(&ctx->lock);
  1085. return rc;
  1086. }
  1087. static char *__cam_isp_ife_sfe_resource_handle_id_to_type(
  1088. uint32_t resource_handle)
  1089. {
  1090. switch (resource_handle) {
  1091. /* IFE output ports */
  1092. case CAM_ISP_IFE_OUT_RES_FULL: return "IFE_FULL";
  1093. case CAM_ISP_IFE_OUT_RES_DS4: return "IFE_DS4";
  1094. case CAM_ISP_IFE_OUT_RES_DS16: return "IFE_DS16";
  1095. case CAM_ISP_IFE_OUT_RES_RAW_DUMP: return "IFE_RAW_DUMP";
  1096. case CAM_ISP_IFE_OUT_RES_FD: return "IFE_FD";
  1097. case CAM_ISP_IFE_OUT_RES_PDAF: return "IFE_PDAF";
  1098. case CAM_ISP_IFE_OUT_RES_RDI_0: return "IFE_RDI_0";
  1099. case CAM_ISP_IFE_OUT_RES_RDI_1: return "IFE_RDI_1";
  1100. case CAM_ISP_IFE_OUT_RES_RDI_2: return "IFE_RDI_2";
  1101. case CAM_ISP_IFE_OUT_RES_RDI_3: return "IFE_RDI_3";
  1102. case CAM_ISP_IFE_OUT_RES_STATS_HDR_BE: return "IFE_STATS_HDR_BE";
  1103. case CAM_ISP_IFE_OUT_RES_STATS_HDR_BHIST: return "IFE_STATS_HDR_BHIST";
  1104. case CAM_ISP_IFE_OUT_RES_STATS_TL_BG: return "IFE_STATS_TL_BG";
  1105. case CAM_ISP_IFE_OUT_RES_STATS_BF: return "IFE_STATS_BF";
  1106. case CAM_ISP_IFE_OUT_RES_STATS_AWB_BG: return "IFE_STATS_AWB_BG";
  1107. case CAM_ISP_IFE_OUT_RES_STATS_BHIST: return "IFE_STATS_BHIST";
  1108. case CAM_ISP_IFE_OUT_RES_STATS_RS: return "IFE_STATS_RS";
  1109. case CAM_ISP_IFE_OUT_RES_STATS_CS: return "IFE_STATS_CS";
  1110. case CAM_ISP_IFE_OUT_RES_STATS_IHIST: return "IFE_STATS_IHIST";
  1111. case CAM_ISP_IFE_OUT_RES_FULL_DISP: return "IFE_FULL_DISP";
  1112. case CAM_ISP_IFE_OUT_RES_DS4_DISP: return "IFE_DS4_DISP";
  1113. case CAM_ISP_IFE_OUT_RES_DS16_DISP: return "IFE_DS16_DISP";
  1114. case CAM_ISP_IFE_OUT_RES_2PD: return "IFE_2PD";
  1115. case CAM_ISP_IFE_OUT_RES_LCR: return "IFE_LCR";
  1116. case CAM_ISP_IFE_OUT_RES_AWB_BFW: return "IFE_AWB_BFW";
  1117. case CAM_ISP_IFE_OUT_RES_PREPROCESS_2PD: return "IFE_PREPROCESS_2PD";
  1118. case CAM_ISP_IFE_OUT_RES_STATS_AEC_BE: return "IFE_STATS_AEC_BE";
  1119. case CAM_ISP_IFE_OUT_RES_LTM_STATS: return "IFE_LTM_STATS";
  1120. case CAM_ISP_IFE_OUT_RES_STATS_GTM_BHIST: return "IFE_STATS_GTM_BHIST";
  1121. case CAM_ISP_IFE_LITE_OUT_RES_STATS_BG: return "IFE_STATS_BG";
  1122. case CAM_ISP_IFE_LITE_OUT_RES_PREPROCESS_RAW: return "IFE_PREPROCESS_RAW";
  1123. case CAM_ISP_IFE_OUT_RES_SPARSE_PD: return "IFE_SPARSE_PD";
  1124. case CAM_ISP_IFE_OUT_RES_STATS_CAF: return "IFE_STATS_CAF";
  1125. case CAM_ISP_IFE_OUT_RES_STATS_BAYER_RS: return "IFE_STATS_BAYER_RS";
  1126. case CAM_ISP_IFE_OUT_RES_PDAF_PARSED_DATA: return "IFE_PDAF_PARSED_DATA";
  1127. case CAM_ISP_IFE_OUT_RES_STATS_ALSC: return "IFE_STATS_ALSC";
  1128. /* SFE output ports */
  1129. case CAM_ISP_SFE_OUT_RES_RDI_0: return "SFE_RDI_0";
  1130. case CAM_ISP_SFE_OUT_RES_RDI_1: return "SFE_RDI_1";
  1131. case CAM_ISP_SFE_OUT_RES_RDI_2: return "SFE_RDI_2";
  1132. case CAM_ISP_SFE_OUT_RES_RDI_3: return "SFE_RDI_3";
  1133. case CAM_ISP_SFE_OUT_RES_RDI_4: return "SFE_RDI_4";
  1134. case CAM_ISP_SFE_OUT_BE_STATS_0: return "SFE_BE_STATS_0";
  1135. case CAM_ISP_SFE_OUT_BE_STATS_1: return "SFE_BE_STATS_1";
  1136. case CAM_ISP_SFE_OUT_BE_STATS_2: return "SFE_BE_STATS_2";
  1137. case CAM_ISP_SFE_OUT_BHIST_STATS_0: return "SFE_BHIST_STATS_0";
  1138. case CAM_ISP_SFE_OUT_BHIST_STATS_1: return "SFE_BHIST_STATS_1";
  1139. case CAM_ISP_SFE_OUT_BHIST_STATS_2: return "SFE_BHIST_STATS_2";
  1140. case CAM_ISP_SFE_OUT_RES_LCR: return "SFE_LCR";
  1141. case CAM_ISP_SFE_OUT_RES_RAW_DUMP: return "SFE_PROCESSED_RAW";
  1142. case CAM_ISP_SFE_OUT_RES_IR: return "SFE_IR";
  1143. case CAM_ISP_SFE_OUT_BAYER_RS_STATS_0: return "SFE_RS_STATS_0";
  1144. case CAM_ISP_SFE_OUT_BAYER_RS_STATS_1: return "SFE_RS_STATS_1";
  1145. case CAM_ISP_SFE_OUT_BAYER_RS_STATS_2: return "SFE_RS_STATS_2";
  1146. case CAM_ISP_SFE_OUT_HDR_STATS: return "HDR_STATS";
  1147. /* SFE input ports */
  1148. case CAM_ISP_SFE_IN_RD_0: return "SFE_RD_0";
  1149. case CAM_ISP_SFE_IN_RD_1: return "SFE_RD_1";
  1150. case CAM_ISP_SFE_IN_RD_2: return "SFE_RD_2";
  1151. /* Handle invalid type */
  1152. default: return "Invalid_Resource_Type";
  1153. }
  1154. }
  1155. static const char *__cam_isp_tfe_resource_handle_id_to_type(
  1156. uint32_t resource_handle)
  1157. {
  1158. switch (resource_handle) {
  1159. /* TFE output ports */
  1160. case CAM_ISP_TFE_OUT_RES_FULL: return "TFE_FULL";
  1161. case CAM_ISP_TFE_OUT_RES_RAW_DUMP: return "TFE_RAW_DUMP";
  1162. case CAM_ISP_TFE_OUT_RES_PDAF: return "TFE_PDAF";
  1163. case CAM_ISP_TFE_OUT_RES_RDI_0: return "TFE_RDI_0";
  1164. case CAM_ISP_TFE_OUT_RES_RDI_1: return "TFE_RDI_1";
  1165. case CAM_ISP_TFE_OUT_RES_RDI_2: return "TFE_RDI_2";
  1166. case CAM_ISP_TFE_OUT_RES_STATS_HDR_BE: return "TFE_STATS_HDR_BE";
  1167. case CAM_ISP_TFE_OUT_RES_STATS_HDR_BHIST: return "TFE_STATS_HDR_BHIST";
  1168. case CAM_ISP_TFE_OUT_RES_STATS_TL_BG: return "TFE_STATS_TL_BG";
  1169. case CAM_ISP_TFE_OUT_RES_STATS_BF: return "TFE_STATS_BF";
  1170. case CAM_ISP_TFE_OUT_RES_STATS_AWB_BG: return "TFE_STATS_AWB_BG";
  1171. case CAM_ISP_TFE_OUT_RES_STATS_RS: return "TFE_STATS_RS";
  1172. case CAM_ISP_TFE_OUT_RES_DS4: return "TFE_DS_4";
  1173. case CAM_ISP_TFE_OUT_RES_DS16: return "TFE_DS_16";
  1174. case CAM_ISP_TFE_OUT_RES_AI: return "TFE_AI";
  1175. case CAM_ISP_TFE_OUT_RES_PD_LCR_STATS: return "TFE_LCR_STATS";
  1176. case CAM_ISP_TFE_OUT_RES_PD_PREPROCESSED: return "TFE_PD_PREPROCESSED";
  1177. case CAM_ISP_TFE_OUT_RES_PD_PARSED: return "TFE_PD_PARSED";
  1178. /* Handle invalid type */
  1179. default: return "Invalid_Resource_Type";
  1180. }
  1181. }
  1182. static const char *__cam_isp_resource_handle_id_to_type(
  1183. uint32_t device_type, uint32_t resource_handle)
  1184. {
  1185. switch (device_type) {
  1186. case CAM_IFE_DEVICE_TYPE:
  1187. case CAM_TFE_MC_DEVICE_TYPE:
  1188. return __cam_isp_ife_sfe_resource_handle_id_to_type(resource_handle);
  1189. case CAM_TFE_DEVICE_TYPE:
  1190. return __cam_isp_tfe_resource_handle_id_to_type(resource_handle);
  1191. default:
  1192. return "INVALID_DEV_TYPE";
  1193. }
  1194. }
  1195. static uint64_t __cam_isp_ctx_get_event_ts(uint32_t evt_id, void *evt_data)
  1196. {
  1197. uint64_t ts = 0;
  1198. if (!evt_data)
  1199. return 0;
  1200. switch (evt_id) {
  1201. case CAM_ISP_HW_EVENT_ERROR:
  1202. ts = ((struct cam_isp_hw_error_event_data *)evt_data)->
  1203. timestamp;
  1204. break;
  1205. case CAM_ISP_HW_EVENT_SOF:
  1206. ts = ((struct cam_isp_hw_sof_event_data *)evt_data)->
  1207. timestamp;
  1208. break;
  1209. case CAM_ISP_HW_EVENT_REG_UPDATE:
  1210. ts = ((struct cam_isp_hw_reg_update_event_data *)evt_data)->
  1211. timestamp;
  1212. break;
  1213. case CAM_ISP_HW_EVENT_EPOCH:
  1214. ts = ((struct cam_isp_hw_epoch_event_data *)evt_data)->
  1215. timestamp;
  1216. break;
  1217. case CAM_ISP_HW_EVENT_EOF:
  1218. ts = ((struct cam_isp_hw_eof_event_data *)evt_data)->
  1219. timestamp;
  1220. break;
  1221. case CAM_ISP_HW_EVENT_DONE:
  1222. case CAM_ISP_HW_SECONDARY_EVENT:
  1223. break;
  1224. default:
  1225. CAM_DBG(CAM_ISP, "Invalid Event Type %d", evt_id);
  1226. }
  1227. return ts;
  1228. }
  1229. static int __cam_isp_ctx_get_hw_timestamp(struct cam_context *ctx, uint64_t *prev_ts,
  1230. uint64_t *curr_ts, uint64_t *boot_ts)
  1231. {
  1232. struct cam_hw_cmd_args hw_cmd_args;
  1233. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  1234. int rc;
  1235. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  1236. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  1237. hw_cmd_args.u.internal_args = &isp_hw_cmd_args;
  1238. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_GET_SOF_TS;
  1239. rc = ctx->hw_mgr_intf->hw_cmd(ctx->ctxt_to_hw_map, &hw_cmd_args);
  1240. if (rc)
  1241. return rc;
  1242. if (isp_hw_cmd_args.u.sof_ts.prev >= isp_hw_cmd_args.u.sof_ts.curr) {
  1243. CAM_ERR(CAM_ISP, "ctx:%u link:0x%x prev timestamp greater than curr timestamp",
  1244. ctx->ctx_id, ctx->link_hdl);
  1245. return -EINVAL;
  1246. }
  1247. *prev_ts = isp_hw_cmd_args.u.sof_ts.prev;
  1248. *curr_ts = isp_hw_cmd_args.u.sof_ts.curr;
  1249. *boot_ts = isp_hw_cmd_args.u.sof_ts.boot;
  1250. return 0;
  1251. }
  1252. static int __cam_isp_ctx_get_cdm_done_timestamp(struct cam_context *ctx,
  1253. uint64_t *last_cdm_done_req)
  1254. {
  1255. struct cam_hw_cmd_args hw_cmd_args;
  1256. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  1257. struct tm ts;
  1258. int rc;
  1259. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  1260. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  1261. hw_cmd_args.u.internal_args = &isp_hw_cmd_args;
  1262. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_GET_LAST_CDM_DONE;
  1263. rc = ctx->hw_mgr_intf->hw_cmd(ctx->ctxt_to_hw_map, &hw_cmd_args);
  1264. if (rc)
  1265. return rc;
  1266. *last_cdm_done_req = isp_hw_cmd_args.u.last_cdm_done;
  1267. ctx->cdm_done_ts = isp_hw_cmd_args.cdm_done_ts;
  1268. time64_to_tm(isp_hw_cmd_args.cdm_done_ts.tv_sec, 0, &ts);
  1269. CAM_DBG(CAM_ISP,
  1270. "last_cdm_done req: %llu ctx: %u link: 0x%x time[%d-%d %d:%d:%d.%lld]",
  1271. last_cdm_done_req, ctx->ctx_id, ctx->link_hdl,
  1272. ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min, ts.tm_sec,
  1273. isp_hw_cmd_args.cdm_done_ts.tv_nsec / 1000000);
  1274. return 0;
  1275. }
  1276. static int __cam_isp_ctx_recover_sof_timestamp(struct cam_context *ctx, uint64_t request_id)
  1277. {
  1278. struct cam_isp_context *ctx_isp = ctx->ctx_priv;
  1279. uint64_t prev_ts, curr_ts, boot_ts;
  1280. uint64_t a, b, c;
  1281. int rc;
  1282. rc = __cam_isp_ctx_get_hw_timestamp(ctx, &prev_ts, &curr_ts, &boot_ts);
  1283. if (rc) {
  1284. CAM_ERR(CAM_ISP, "ctx:%u link: 0x%x Failed to get timestamp from HW",
  1285. ctx->ctx_id, ctx->link_hdl);
  1286. return rc;
  1287. }
  1288. /**
  1289. * If the last received SOF was for frame A and we have missed the SOF for frame B,
  1290. * then we need to find out if the hardware is at frame B or C.
  1291. * +-----+-----+-----+
  1292. * | A | B | C |
  1293. * +-----+-----+-----+
  1294. */
  1295. a = ctx_isp->sof_timestamp_val;
  1296. if (a == prev_ts) {
  1297. /* Hardware is at frame B */
  1298. b = curr_ts;
  1299. CAM_DBG(CAM_ISP, "ctx:%u link:0x%x recover time(last:0x%llx,curr:0x%llx)req:%llu",
  1300. ctx->ctx_id, ctx->link_hdl, a, b, request_id);
  1301. } else if (a < prev_ts) {
  1302. /* Hardware is at frame C */
  1303. b = prev_ts;
  1304. c = curr_ts;
  1305. CAM_DBG(CAM_ISP,
  1306. "ctx:%u link:0x%x recover time(last:0x%llx,prev:0x%llx,curr:0x%llx)req:%llu",
  1307. ctx->ctx_id, ctx->link_hdl, a, b, c, request_id);
  1308. } else {
  1309. /* Hardware is at frame A (which we supposedly missed) */
  1310. CAM_ERR_RATE_LIMIT(CAM_ISP,
  1311. "ctx:%u link: 0x%x erroneous call to SOF recovery(last:0x%llx, prev:0x%llx, curr:0x%llx) req: %llu",
  1312. ctx->ctx_id, ctx->link_hdl, a, prev_ts, curr_ts, request_id);
  1313. return 0;
  1314. }
  1315. ctx_isp->boot_timestamp = boot_ts + (b - curr_ts);
  1316. ctx_isp->sof_timestamp_val = b;
  1317. ctx_isp->frame_id++;
  1318. return 0;
  1319. }
  1320. static void __cam_isp_ctx_send_sof_boot_timestamp(
  1321. struct cam_isp_context *ctx_isp, uint64_t request_id,
  1322. uint32_t sof_event_status, struct shutter_event *shutter_event)
  1323. {
  1324. struct cam_req_mgr_message req_msg;
  1325. req_msg.session_hdl = ctx_isp->base->session_hdl;
  1326. req_msg.u.frame_msg.frame_id = ctx_isp->frame_id;
  1327. req_msg.u.frame_msg.request_id = request_id;
  1328. req_msg.u.frame_msg.timestamp = ctx_isp->boot_timestamp;
  1329. req_msg.u.frame_msg.link_hdl = ctx_isp->base->link_hdl;
  1330. req_msg.u.frame_msg.sof_status = sof_event_status;
  1331. req_msg.u.frame_msg.frame_id_meta = ctx_isp->frame_id_meta;
  1332. CAM_DBG(CAM_ISP,
  1333. "request id:%lld frame number:%lld boot time stamp:0x%llx status:%u",
  1334. request_id, ctx_isp->frame_id,
  1335. ctx_isp->boot_timestamp, sof_event_status);
  1336. shutter_event->frame_id = ctx_isp->frame_id;
  1337. shutter_event->req_id = request_id;
  1338. shutter_event->boot_ts = ctx_isp->boot_timestamp;
  1339. shutter_event->sof_ts = ctx_isp->sof_timestamp_val;
  1340. if (cam_req_mgr_notify_message(&req_msg,
  1341. V4L_EVENT_CAM_REQ_MGR_SOF_BOOT_TS,
  1342. V4L_EVENT_CAM_REQ_MGR_EVENT))
  1343. CAM_ERR(CAM_ISP,
  1344. "Error in notifying the boot time for req id:%lld",
  1345. request_id);
  1346. __cam_isp_ctx_update_event_record(ctx_isp,
  1347. CAM_ISP_CTX_EVENT_SHUTTER, NULL, shutter_event);
  1348. }
  1349. static void __cam_isp_ctx_send_unified_timestamp(
  1350. struct cam_isp_context *ctx_isp, uint64_t request_id,
  1351. struct shutter_event *shutter_event)
  1352. {
  1353. struct cam_req_mgr_message req_msg;
  1354. req_msg.session_hdl = ctx_isp->base->session_hdl;
  1355. req_msg.u.frame_msg_v2.frame_id = ctx_isp->frame_id;
  1356. req_msg.u.frame_msg_v2.request_id = request_id;
  1357. req_msg.u.frame_msg_v2.timestamps[CAM_REQ_SOF_QTIMER_TIMESTAMP] =
  1358. (request_id == 0) ? 0 : ctx_isp->sof_timestamp_val;
  1359. req_msg.u.frame_msg_v2.timestamps[CAM_REQ_BOOT_TIMESTAMP] = ctx_isp->boot_timestamp;
  1360. req_msg.u.frame_msg_v2.link_hdl = ctx_isp->base->link_hdl;
  1361. req_msg.u.frame_msg_v2.frame_id_meta = ctx_isp->frame_id_meta;
  1362. CAM_DBG(CAM_ISP,
  1363. "link hdl 0x%x request id:%lld frame number:%lld SOF time stamp:0x%llx ctx %d\
  1364. boot time stamp:0x%llx", ctx_isp->base->link_hdl, request_id,
  1365. ctx_isp->frame_id, ctx_isp->sof_timestamp_val,ctx_isp->base->ctx_id,
  1366. ctx_isp->boot_timestamp);
  1367. shutter_event->frame_id = ctx_isp->frame_id;
  1368. shutter_event->req_id = request_id;
  1369. shutter_event->boot_ts = ctx_isp->boot_timestamp;
  1370. shutter_event->sof_ts = ctx_isp->sof_timestamp_val;
  1371. if (cam_req_mgr_notify_message(&req_msg,
  1372. V4L_EVENT_CAM_REQ_MGR_SOF_UNIFIED_TS, V4L_EVENT_CAM_REQ_MGR_EVENT))
  1373. CAM_ERR(CAM_ISP,
  1374. "Error in notifying the sof and boot time for req id:%lld",
  1375. request_id);
  1376. __cam_isp_ctx_update_event_record(ctx_isp,
  1377. CAM_ISP_CTX_EVENT_SHUTTER, NULL, shutter_event);
  1378. }
  1379. static void __cam_isp_ctx_send_sof_timestamp_frame_header(
  1380. struct cam_isp_context *ctx_isp, uint32_t *frame_header_cpu_addr,
  1381. uint64_t request_id, uint32_t sof_event_status)
  1382. {
  1383. uint32_t *time32 = NULL;
  1384. uint64_t timestamp = 0;
  1385. struct cam_req_mgr_message req_msg;
  1386. time32 = frame_header_cpu_addr;
  1387. timestamp = (uint64_t) time32[1];
  1388. timestamp = timestamp << 24;
  1389. timestamp |= (uint64_t)(time32[0] >> 8);
  1390. timestamp = mul_u64_u32_div(timestamp,
  1391. CAM_IFE_QTIMER_MUL_FACTOR,
  1392. CAM_IFE_QTIMER_DIV_FACTOR);
  1393. ctx_isp->sof_timestamp_val = timestamp;
  1394. req_msg.session_hdl = ctx_isp->base->session_hdl;
  1395. req_msg.u.frame_msg.frame_id = ctx_isp->frame_id;
  1396. req_msg.u.frame_msg.request_id = request_id;
  1397. req_msg.u.frame_msg.timestamp = ctx_isp->sof_timestamp_val;
  1398. req_msg.u.frame_msg.link_hdl = ctx_isp->base->link_hdl;
  1399. req_msg.u.frame_msg.sof_status = sof_event_status;
  1400. CAM_DBG(CAM_ISP,
  1401. "request id:%lld frame number:%lld SOF time stamp:0x%llx status:%u",
  1402. request_id, ctx_isp->frame_id,
  1403. ctx_isp->sof_timestamp_val, sof_event_status);
  1404. if (cam_req_mgr_notify_message(&req_msg,
  1405. V4L_EVENT_CAM_REQ_MGR_SOF, V4L_EVENT_CAM_REQ_MGR_EVENT))
  1406. CAM_ERR(CAM_ISP,
  1407. "Error in notifying the sof time for req id:%lld",
  1408. request_id);
  1409. }
  1410. static void __cam_isp_ctx_send_sof_timestamp(
  1411. struct cam_isp_context *ctx_isp, uint64_t request_id,
  1412. uint32_t sof_event_status)
  1413. {
  1414. struct cam_req_mgr_message req_msg;
  1415. struct cam_context *ctx = ctx_isp->base;
  1416. struct shutter_event shutter_event = {0};
  1417. if (ctx_isp->reported_frame_id == ctx_isp->frame_id) {
  1418. if (__cam_isp_ctx_recover_sof_timestamp(ctx_isp->base, request_id))
  1419. CAM_WARN(CAM_ISP, "Missed SOF.No SOF timestamp recovery,ctx:%u,link:0x%x",
  1420. ctx->ctx_id, ctx->link_hdl);
  1421. }
  1422. if (request_id == 0 && (ctx_isp->reported_frame_id == ctx_isp->frame_id)) {
  1423. CAM_WARN_RATE_LIMIT(CAM_ISP,
  1424. "Missed SOF Recovery for invalid req, Skip notificaiton to userspace Ctx: %u link: 0x%x frame_id %u",
  1425. ctx->ctx_id, ctx->link_hdl, ctx_isp->frame_id);
  1426. return;
  1427. }
  1428. ctx_isp->reported_frame_id = ctx_isp->frame_id;
  1429. shutter_event.status = sof_event_status;
  1430. if ((ctx_isp->v4l2_event_sub_ids & (1 << V4L_EVENT_CAM_REQ_MGR_SOF_UNIFIED_TS))
  1431. && !ctx_isp->use_frame_header_ts) {
  1432. __cam_isp_ctx_send_unified_timestamp(ctx_isp, request_id, &shutter_event);
  1433. return;
  1434. }
  1435. if ((ctx_isp->use_frame_header_ts) || (request_id == 0))
  1436. goto end;
  1437. req_msg.session_hdl = ctx_isp->base->session_hdl;
  1438. req_msg.u.frame_msg.frame_id = ctx_isp->frame_id;
  1439. req_msg.u.frame_msg.request_id = request_id;
  1440. req_msg.u.frame_msg.timestamp = ctx_isp->sof_timestamp_val;
  1441. req_msg.u.frame_msg.link_hdl = ctx_isp->base->link_hdl;
  1442. req_msg.u.frame_msg.sof_status = sof_event_status;
  1443. req_msg.u.frame_msg.frame_id_meta = ctx_isp->frame_id_meta;
  1444. CAM_DBG(CAM_ISP,
  1445. "request id:%lld frame number:%lld SOF time stamp:0x%llx status:%u ctx_idx: %u, link: 0x%x",
  1446. request_id, ctx_isp->frame_id,
  1447. ctx_isp->sof_timestamp_val, sof_event_status, ctx->ctx_id, ctx->link_hdl);
  1448. if (cam_req_mgr_notify_message(&req_msg,
  1449. V4L_EVENT_CAM_REQ_MGR_SOF, V4L_EVENT_CAM_REQ_MGR_EVENT))
  1450. CAM_ERR(CAM_ISP,
  1451. "Error in notifying the sof time for req id:%lld, ctx_idx: %u, link: 0x%x",
  1452. request_id, ctx->ctx_id, ctx->link_hdl);
  1453. end:
  1454. __cam_isp_ctx_send_sof_boot_timestamp(ctx_isp,
  1455. request_id, sof_event_status, &shutter_event);
  1456. }
  1457. static void __cam_isp_ctx_handle_buf_done_fail_log(
  1458. struct cam_isp_context *ctx_isp, uint64_t request_id,
  1459. struct cam_isp_ctx_req *req_isp)
  1460. {
  1461. int i;
  1462. struct cam_context *ctx = ctx_isp->base;
  1463. const char *handle_type;
  1464. if (req_isp->num_fence_map_out >= CAM_ISP_CTX_RES_MAX) {
  1465. CAM_ERR(CAM_ISP,
  1466. "Num Resources exceed mMAX %d >= %d ",
  1467. req_isp->num_fence_map_out, CAM_ISP_CTX_RES_MAX);
  1468. return;
  1469. }
  1470. CAM_WARN_RATE_LIMIT(CAM_ISP,
  1471. "Prev Req[%lld] : num_out=%d, num_acked=%d, bubble : report=%d, detected=%d",
  1472. request_id, req_isp->num_fence_map_out, req_isp->num_acked,
  1473. req_isp->bubble_report, req_isp->bubble_detected);
  1474. CAM_WARN_RATE_LIMIT(CAM_ISP,
  1475. "Resource Handles that fail to generate buf_done in prev frame");
  1476. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  1477. if (req_isp->fence_map_out[i].sync_id != -1) {
  1478. handle_type = __cam_isp_resource_handle_id_to_type(
  1479. ctx_isp->isp_device_type,
  1480. req_isp->fence_map_out[i].resource_handle);
  1481. trace_cam_log_event("Buf_done Congestion",
  1482. handle_type, request_id, req_isp->fence_map_out[i].sync_id);
  1483. CAM_WARN_RATE_LIMIT(CAM_ISP,
  1484. "Resource_Handle: [%s][0x%x] Sync_ID: [0x%x]",
  1485. handle_type,
  1486. req_isp->fence_map_out[i].resource_handle,
  1487. req_isp->fence_map_out[i].sync_id);
  1488. }
  1489. }
  1490. ctx_isp->congestion_cnt++;
  1491. /* Trigger SOF freeze debug dump on 3 or greater instances of congestion */
  1492. if ((ctx_isp->congestion_cnt >= CAM_ISP_CONTEXT_CONGESTION_CNT_MAX) &&
  1493. (!ctx_isp->sof_dbg_irq_en))
  1494. __cam_isp_ctx_handle_sof_freeze_evt(ctx);
  1495. }
  1496. static void __cam_isp_context_reset_internal_recovery_params(
  1497. struct cam_isp_context *ctx_isp)
  1498. {
  1499. atomic_set(&ctx_isp->internal_recovery_set, 0);
  1500. atomic_set(&ctx_isp->process_bubble, 0);
  1501. ctx_isp->aeb_error_cnt = 0;
  1502. ctx_isp->bubble_frame_cnt = 0;
  1503. ctx_isp->congestion_cnt = 0;
  1504. ctx_isp->sof_dbg_irq_en = false;
  1505. }
  1506. static int __cam_isp_context_try_internal_recovery(
  1507. struct cam_isp_context *ctx_isp)
  1508. {
  1509. int rc = 0;
  1510. struct cam_context *ctx = ctx_isp->base;
  1511. struct cam_ctx_request *req;
  1512. struct cam_isp_ctx_req *req_isp;
  1513. /*
  1514. * Start with wait list, if recovery is stil set
  1515. * errored request has not been moved to pending yet.
  1516. * Buf done for errored request has not occurred recover
  1517. * from here
  1518. */
  1519. if (!list_empty(&ctx->wait_req_list)) {
  1520. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request, list);
  1521. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  1522. if (req->request_id == ctx_isp->recovery_req_id) {
  1523. rc = __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF,
  1524. CRM_KMD_WARN_INTERNAL_RECOVERY, ctx_isp->recovery_req_id, ctx_isp);
  1525. if (rc) {
  1526. /* Unable to do bubble recovery reset back to normal */
  1527. CAM_WARN(CAM_ISP,
  1528. "Unable to perform internal recovery [bubble reporting failed] for req: %llu in ctx: %u on link: 0x%x",
  1529. req->request_id, ctx->ctx_id, ctx->link_hdl);
  1530. __cam_isp_context_reset_internal_recovery_params(ctx_isp);
  1531. req_isp->bubble_detected = false;
  1532. goto end;
  1533. }
  1534. list_del_init(&req->list);
  1535. list_add(&req->list, &ctx->pending_req_list);
  1536. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  1537. CAM_INFO(CAM_ISP,
  1538. "Internal recovery for req: %llu in ctx: %u on link: 0x%x triggered",
  1539. ctx_isp->recovery_req_id, ctx->ctx_id, ctx->link_hdl);
  1540. goto end;
  1541. }
  1542. }
  1543. /*
  1544. * If not in wait list only other possibility is request is in pending list
  1545. * on error detection, bubble detect is set assuming new frame after detection
  1546. * comes in, there is an rup it's moved to active list and it finishes with
  1547. * it's buf done's
  1548. */
  1549. if (!list_empty(&ctx->pending_req_list)) {
  1550. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request, list);
  1551. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  1552. if (req->request_id == ctx_isp->recovery_req_id) {
  1553. rc = __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF,
  1554. CRM_KMD_WARN_INTERNAL_RECOVERY, ctx_isp->recovery_req_id, ctx_isp);
  1555. if (rc) {
  1556. /* Unable to do bubble recovery reset back to normal */
  1557. CAM_WARN(CAM_ISP,
  1558. "Unable to perform internal recovery [bubble reporting failed] for req: %llu in ctx: %u on link: 0x%x",
  1559. req->request_id, ctx->ctx_id, ctx->link_hdl);
  1560. __cam_isp_context_reset_internal_recovery_params(ctx_isp);
  1561. req_isp->bubble_detected = false;
  1562. goto end;
  1563. }
  1564. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  1565. CAM_INFO(CAM_ISP,
  1566. "Internal recovery for req: %llu in ctx: %u on link: 0x%x triggered",
  1567. ctx_isp->recovery_req_id, ctx->ctx_id, ctx->link_hdl);
  1568. goto end;
  1569. }
  1570. }
  1571. /* If request is not found in either of the lists skip recovery */
  1572. __cam_isp_context_reset_internal_recovery_params(ctx_isp);
  1573. end:
  1574. return rc;
  1575. }
  1576. static int __cam_isp_ctx_handle_buf_done_for_req_list(
  1577. struct cam_isp_context *ctx_isp,
  1578. struct cam_ctx_request *req)
  1579. {
  1580. int rc = 0, i;
  1581. uint64_t buf_done_req_id;
  1582. struct cam_isp_ctx_req *req_isp;
  1583. struct cam_context *ctx = ctx_isp->base;
  1584. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1585. ctx_isp->active_req_cnt--;
  1586. buf_done_req_id = req->request_id;
  1587. if (req_isp->bubble_detected && req_isp->bubble_report) {
  1588. req_isp->num_acked = 0;
  1589. req_isp->num_deferred_acks = 0;
  1590. req_isp->bubble_detected = false;
  1591. list_del_init(&req->list);
  1592. atomic_set(&ctx_isp->process_bubble, 0);
  1593. req_isp->cdm_reset_before_apply = false;
  1594. ctx_isp->bubble_frame_cnt = 0;
  1595. if (buf_done_req_id <= ctx->last_flush_req) {
  1596. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  1597. for (i = 0; i < req_isp->num_fence_map_out; i++)
  1598. rc = cam_sync_signal(
  1599. req_isp->fence_map_out[i].sync_id,
  1600. CAM_SYNC_STATE_SIGNALED_ERROR,
  1601. CAM_SYNC_ISP_EVENT_BUBBLE);
  1602. list_add_tail(&req->list, &ctx->free_req_list);
  1603. CAM_DBG(CAM_REQ,
  1604. "Move active request %lld to free list(cnt = %d) [flushed], ctx %u, link: 0x%x",
  1605. buf_done_req_id, ctx_isp->active_req_cnt,
  1606. ctx->ctx_id, ctx->link_hdl);
  1607. ctx_isp->last_bufdone_err_apply_req_id = 0;
  1608. } else {
  1609. list_add(&req->list, &ctx->pending_req_list);
  1610. CAM_DBG(CAM_REQ,
  1611. "Move active request %lld to pending list(cnt = %d) [bubble recovery], ctx %u, link: 0x%x",
  1612. req->request_id, ctx_isp->active_req_cnt,
  1613. ctx->ctx_id, ctx->link_hdl);
  1614. }
  1615. } else {
  1616. if (!ctx_isp->use_frame_header_ts) {
  1617. if (ctx_isp->reported_req_id < buf_done_req_id) {
  1618. ctx_isp->reported_req_id = buf_done_req_id;
  1619. __cam_isp_ctx_send_sof_timestamp(ctx_isp,
  1620. buf_done_req_id,
  1621. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1622. }
  1623. }
  1624. list_del_init(&req->list);
  1625. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  1626. list_add_tail(&req->list, &ctx->free_req_list);
  1627. req_isp->reapply_type = CAM_CONFIG_REAPPLY_NONE;
  1628. req_isp->cdm_reset_before_apply = false;
  1629. req_isp->num_acked = 0;
  1630. req_isp->num_deferred_acks = 0;
  1631. /*
  1632. * Only update the process_bubble and bubble_frame_cnt
  1633. * when bubble is detected on this req, in case the other
  1634. * request is processing bubble.
  1635. */
  1636. if (req_isp->bubble_detected) {
  1637. atomic_set(&ctx_isp->process_bubble, 0);
  1638. ctx_isp->bubble_frame_cnt = 0;
  1639. req_isp->bubble_detected = false;
  1640. }
  1641. CAM_DBG(CAM_REQ,
  1642. "Move active request %lld to free list(cnt = %d) [all fences done], ctx %u link: 0x%x",
  1643. buf_done_req_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  1644. ctx_isp->req_info.last_bufdone_req_id = req->request_id;
  1645. ctx_isp->last_bufdone_err_apply_req_id = 0;
  1646. }
  1647. if (atomic_read(&ctx_isp->internal_recovery_set) && !ctx_isp->active_req_cnt)
  1648. __cam_isp_context_try_internal_recovery(ctx_isp);
  1649. cam_cpas_notify_event("IFE BufDone", buf_done_req_id);
  1650. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1651. CAM_ISP_STATE_CHANGE_TRIGGER_DONE, buf_done_req_id);
  1652. __cam_isp_ctx_update_event_record(ctx_isp,
  1653. CAM_ISP_CTX_EVENT_BUFDONE, req, NULL);
  1654. return rc;
  1655. }
  1656. static int __cam_isp_ctx_handle_buf_done_for_request(
  1657. struct cam_isp_context *ctx_isp,
  1658. struct cam_ctx_request *req,
  1659. struct cam_isp_hw_done_event_data *done,
  1660. uint32_t bubble_state,
  1661. struct cam_isp_hw_done_event_data *done_next_req)
  1662. {
  1663. int rc = 0;
  1664. int i, j, k;
  1665. bool not_found = false;
  1666. struct cam_isp_ctx_req *req_isp;
  1667. struct cam_context *ctx = ctx_isp->base;
  1668. const char *handle_type;
  1669. struct cam_isp_context_comp_record *comp_grp = NULL;
  1670. trace_cam_buf_done("ISP", ctx, req);
  1671. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1672. CAM_DBG(CAM_ISP, "Enter with bubble_state %d, req_bubble_detected %d, ctx %u link: 0x%x",
  1673. bubble_state, req_isp->bubble_detected, ctx->ctx_id, ctx->link_hdl);
  1674. done_next_req->resource_handle = 0;
  1675. done_next_req->timestamp = done->timestamp;
  1676. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  1677. if (done->resource_handle ==
  1678. req_isp->fence_map_out[i].resource_handle)
  1679. break;
  1680. }
  1681. if (done->hw_type == CAM_ISP_HW_TYPE_SFE)
  1682. comp_grp = &ctx_isp->sfe_bus_comp_grp[done->comp_group_id];
  1683. else
  1684. comp_grp = &ctx_isp->vfe_bus_comp_grp[done->comp_group_id];
  1685. if (!comp_grp) {
  1686. CAM_ERR(CAM_ISP, "comp_grp is NULL");
  1687. rc = -EINVAL;
  1688. return rc;
  1689. }
  1690. if (i == req_isp->num_fence_map_out) {
  1691. for (j = 0; j < comp_grp->num_res; j++) {
  1692. not_found = false;
  1693. if (comp_grp->res_id[j] == done->resource_handle)
  1694. continue;
  1695. for (k = 0; k < req_isp->num_fence_map_out; k++)
  1696. if (comp_grp->res_id[j] ==
  1697. req_isp->fence_map_out[k].resource_handle)
  1698. break;
  1699. if ((k == req_isp->num_fence_map_out) && (j != comp_grp->num_res - 1))
  1700. continue;
  1701. else if (k != req_isp->num_fence_map_out)
  1702. break;
  1703. else
  1704. not_found = true;
  1705. }
  1706. }
  1707. if (not_found) {
  1708. /*
  1709. * If not found in current request, it could be
  1710. * belonging to next request, this can happen if
  1711. * IRQ delay happens. It is only valid when the
  1712. * platform doesn't have last consumed address.
  1713. */
  1714. CAM_WARN(CAM_ISP,
  1715. "BUF_DONE for res %s not found in Req %lld ",
  1716. __cam_isp_resource_handle_id_to_type(
  1717. ctx_isp->isp_device_type,
  1718. done->resource_handle),
  1719. req->request_id);
  1720. done_next_req->hw_type = done->hw_type;
  1721. done_next_req->resource_handle = done->resource_handle;
  1722. done_next_req->comp_group_id = done->comp_group_id;
  1723. goto check_deferred;
  1724. }
  1725. for (i = 0; i < comp_grp->num_res; i++) {
  1726. for (j = 0; j < req_isp->num_fence_map_out; j++) {
  1727. if (comp_grp->res_id[i] ==
  1728. req_isp->fence_map_out[j].resource_handle)
  1729. break;
  1730. }
  1731. if (j == req_isp->num_fence_map_out) {
  1732. /*
  1733. * If not found in current request, it could be
  1734. * belonging to an active port with no valid fence
  1735. * bound to it, we needn't process it.
  1736. */
  1737. CAM_DBG(CAM_ISP,
  1738. "BUF_DONE for res %s not active in Req %lld ctx %u link: 0x%x",
  1739. __cam_isp_resource_handle_id_to_type(
  1740. ctx_isp->isp_device_type,
  1741. comp_grp->res_id[i]),
  1742. req->request_id, ctx->ctx_id, ctx->link_hdl);
  1743. continue;
  1744. }
  1745. if (req_isp->fence_map_out[j].sync_id == -1) {
  1746. handle_type =
  1747. __cam_isp_resource_handle_id_to_type(
  1748. ctx_isp->isp_device_type,
  1749. req_isp->fence_map_out[j].resource_handle);
  1750. CAM_WARN(CAM_ISP,
  1751. "Duplicate BUF_DONE for req %lld : i=%d, j=%d, res=%s, ctx %u link: 0x%x",
  1752. req->request_id, i, j, handle_type, ctx->ctx_id, ctx->link_hdl);
  1753. trace_cam_log_event("Duplicate BufDone",
  1754. handle_type, req->request_id, ctx->ctx_id);
  1755. continue;
  1756. }
  1757. /* Get buf handles from packet and retrieve them from presil framework */
  1758. if (cam_presil_mode_enabled()) {
  1759. rc = cam_presil_retrieve_buffers_from_packet(req_isp->hw_update_data.packet,
  1760. ctx->img_iommu_hdl, req_isp->fence_map_out[j].resource_handle);
  1761. if (rc) {
  1762. CAM_ERR(CAM_ISP,
  1763. "Failed to retrieve image buffers req_id:%d ctx_id:%u link: 0x%x bubble detected:%d rc:%d",
  1764. req->request_id, ctx->ctx_id, ctx->link_hdl,
  1765. req_isp->bubble_detected, rc);
  1766. return rc;
  1767. }
  1768. }
  1769. if (!req_isp->bubble_detected) {
  1770. handle_type = __cam_isp_resource_handle_id_to_type(
  1771. ctx_isp->isp_device_type,
  1772. req_isp->fence_map_out[j].resource_handle);
  1773. CAM_DBG(CAM_ISP,
  1774. "Sync with success: req %lld res 0x%x fd 0x%x, ctx %u link: 0x%x port %s",
  1775. req->request_id,
  1776. req_isp->fence_map_out[j].resource_handle,
  1777. req_isp->fence_map_out[j].sync_id,
  1778. ctx->ctx_id, ctx->link_hdl, handle_type);
  1779. cam_smmu_buffer_tracker_buffer_putref(
  1780. req_isp->fence_map_out[j].buffer_tracker);
  1781. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  1782. CAM_SYNC_STATE_SIGNALED_SUCCESS,
  1783. CAM_SYNC_COMMON_EVENT_SUCCESS);
  1784. if (rc)
  1785. CAM_DBG(CAM_ISP, "Sync failed with rc = %d, ctx %u link: 0x%x",
  1786. rc, ctx->ctx_id, ctx->link_hdl);
  1787. } else if (!req_isp->bubble_report) {
  1788. CAM_DBG(CAM_ISP,
  1789. "Sync with failure: req %lld res 0x%x fd 0x%x, ctx %u link: 0x%x",
  1790. req->request_id,
  1791. req_isp->fence_map_out[j].resource_handle,
  1792. req_isp->fence_map_out[j].sync_id,
  1793. ctx->ctx_id, ctx->link_hdl);
  1794. cam_smmu_buffer_tracker_buffer_putref(
  1795. req_isp->fence_map_out[j].buffer_tracker);
  1796. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  1797. CAM_SYNC_STATE_SIGNALED_ERROR,
  1798. CAM_SYNC_ISP_EVENT_BUBBLE);
  1799. if (rc)
  1800. CAM_ERR(CAM_ISP, "Sync failed with rc = %d, ctx %u link: 0x%x",
  1801. rc, ctx->ctx_id, ctx->link_hdl);
  1802. } else {
  1803. /*
  1804. * Ignore the buffer done if bubble detect is on
  1805. * Increment the ack number here, and queue the
  1806. * request back to pending list whenever all the
  1807. * buffers are done.
  1808. */
  1809. req_isp->num_acked++;
  1810. CAM_DBG(CAM_ISP,
  1811. "buf done with bubble state %d recovery %d for req %lld, ctx %u link: 0x%x",
  1812. bubble_state,
  1813. req_isp->bubble_report,
  1814. req->request_id,
  1815. ctx->ctx_id, ctx->link_hdl);
  1816. continue;
  1817. }
  1818. CAM_DBG(CAM_ISP, "req %lld, reset sync id 0x%x ctx %u link: 0x%x",
  1819. req->request_id,
  1820. req_isp->fence_map_out[j].sync_id, ctx->ctx_id, ctx->link_hdl);
  1821. if (!rc) {
  1822. req_isp->num_acked++;
  1823. req_isp->fence_map_out[j].sync_id = -1;
  1824. }
  1825. if ((ctx_isp->use_frame_header_ts) &&
  1826. (req_isp->hw_update_data.frame_header_res_id ==
  1827. req_isp->fence_map_out[j].resource_handle))
  1828. __cam_isp_ctx_send_sof_timestamp_frame_header(
  1829. ctx_isp,
  1830. req_isp->hw_update_data.frame_header_cpu_addr,
  1831. req->request_id, CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1832. }
  1833. check_deferred:
  1834. if (req_isp->num_acked > req_isp->num_fence_map_out) {
  1835. /* Should not happen */
  1836. CAM_ERR(CAM_ISP,
  1837. "WARNING: req_id %lld num_acked %d > map_out %d, ctx %u link: 0x%x",
  1838. req->request_id, req_isp->num_acked,
  1839. req_isp->num_fence_map_out, ctx->ctx_id, ctx->link_hdl);
  1840. WARN_ON(req_isp->num_acked > req_isp->num_fence_map_out);
  1841. }
  1842. if (req_isp->num_acked != req_isp->num_fence_map_out)
  1843. return rc;
  1844. rc = __cam_isp_ctx_handle_buf_done_for_req_list(ctx_isp, req);
  1845. return rc;
  1846. }
  1847. static int __cam_isp_handle_deferred_buf_done(
  1848. struct cam_isp_context *ctx_isp,
  1849. struct cam_ctx_request *req,
  1850. bool bubble_handling,
  1851. uint32_t status, uint32_t event_cause)
  1852. {
  1853. int i, j;
  1854. int rc = 0;
  1855. struct cam_isp_ctx_req *req_isp =
  1856. (struct cam_isp_ctx_req *) req->req_priv;
  1857. struct cam_context *ctx = ctx_isp->base;
  1858. CAM_DBG(CAM_ISP,
  1859. "ctx[%u] link[0x%x] : Req %llu : Handling %d deferred buf_dones num_acked=%d, bubble_handling=%d",
  1860. ctx->ctx_id, ctx->link_hdl, req->request_id, req_isp->num_deferred_acks,
  1861. req_isp->num_acked, bubble_handling);
  1862. for (i = 0; i < req_isp->num_deferred_acks; i++) {
  1863. j = req_isp->deferred_fence_map_index[i];
  1864. CAM_DBG(CAM_ISP,
  1865. "ctx[%u] link[0x%x] : Sync with status=%d, event_cause=%d: req %lld res 0x%x sync_id 0x%x",
  1866. ctx->ctx_id, ctx->link_hdl, status, event_cause,
  1867. req->request_id,
  1868. req_isp->fence_map_out[j].resource_handle,
  1869. req_isp->fence_map_out[j].sync_id);
  1870. if (req_isp->fence_map_out[j].sync_id == -1) {
  1871. CAM_WARN(CAM_ISP,
  1872. "ctx[%u] link[0x%x] : Deferred buf_done already signalled, req_id=%llu, j=%d, res=0x%x",
  1873. ctx->ctx_id, ctx->link_hdl, req->request_id, j,
  1874. req_isp->fence_map_out[j].resource_handle);
  1875. continue;
  1876. }
  1877. if (!bubble_handling) {
  1878. CAM_WARN(CAM_ISP,
  1879. "Unexpected Buf done for res=0x%x on ctx[%u] link[0x%x] for Req %llu, status=%d, possible bh delays",
  1880. req_isp->fence_map_out[j].resource_handle, ctx->ctx_id,
  1881. ctx->link_hdl, req->request_id, status);
  1882. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  1883. status, event_cause);
  1884. if (rc) {
  1885. CAM_ERR(CAM_ISP,
  1886. "ctx[%u] link[0x%x] : Sync signal for Req %llu, sync_id %d status=%d failed with rc = %d",
  1887. ctx->ctx_id, ctx->link_hdl, req->request_id,
  1888. req_isp->fence_map_out[j].sync_id,
  1889. status, rc);
  1890. } else {
  1891. req_isp->num_acked++;
  1892. req_isp->fence_map_out[j].sync_id = -1;
  1893. }
  1894. } else {
  1895. req_isp->num_acked++;
  1896. }
  1897. }
  1898. CAM_DBG(CAM_ISP,
  1899. "ctx[%u] link[0x%x] : Req %llu : Handled %d deferred buf_dones num_acked=%d, num_fence_map_out=%d",
  1900. ctx->ctx_id, ctx->link_hdl, req->request_id, req_isp->num_deferred_acks,
  1901. req_isp->num_acked, req_isp->num_fence_map_out);
  1902. req_isp->num_deferred_acks = 0;
  1903. return rc;
  1904. }
  1905. static int __cam_isp_ctx_handle_deferred_buf_done_in_bubble(
  1906. struct cam_isp_context *ctx_isp,
  1907. struct cam_ctx_request *req)
  1908. {
  1909. int rc = 0;
  1910. struct cam_context *ctx = ctx_isp->base;
  1911. struct cam_isp_ctx_req *req_isp;
  1912. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  1913. if (req_isp->num_deferred_acks)
  1914. rc = __cam_isp_handle_deferred_buf_done(ctx_isp, req,
  1915. req_isp->bubble_report,
  1916. CAM_SYNC_STATE_SIGNALED_ERROR,
  1917. CAM_SYNC_ISP_EVENT_BUBBLE);
  1918. if (req_isp->num_acked > req_isp->num_fence_map_out) {
  1919. /* Should not happen */
  1920. CAM_ERR(CAM_ISP,
  1921. "WARNING: req_id %lld num_acked %d > map_out %d, ctx %u, link[0x%x]",
  1922. req->request_id, req_isp->num_acked,
  1923. req_isp->num_fence_map_out, ctx->ctx_id, ctx->link_hdl);
  1924. WARN_ON(req_isp->num_acked > req_isp->num_fence_map_out);
  1925. }
  1926. if (req_isp->num_acked == req_isp->num_fence_map_out)
  1927. rc = __cam_isp_ctx_handle_buf_done_for_req_list(ctx_isp, req);
  1928. return rc;
  1929. }
  1930. static int __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  1931. struct cam_isp_context *ctx_isp,
  1932. struct cam_ctx_request *req,
  1933. struct cam_isp_hw_done_event_data *done,
  1934. uint32_t bubble_state,
  1935. bool verify_consumed_addr,
  1936. bool defer_buf_done)
  1937. {
  1938. int rc = 0;
  1939. int i, j, k, def_idx;
  1940. bool not_found = false;
  1941. bool duplicate_defer_buf_done = false;
  1942. struct cam_isp_ctx_req *req_isp;
  1943. struct cam_context *ctx = ctx_isp->base;
  1944. const char *handle_type;
  1945. uint32_t cmp_addr = 0;
  1946. struct cam_isp_hw_done_event_data unhandled_done = {0};
  1947. struct cam_isp_context_comp_record *comp_grp = NULL;
  1948. struct cam_hw_cmd_args hw_cmd_args;
  1949. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  1950. trace_cam_buf_done("ISP", ctx, req);
  1951. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1952. CAM_DBG(CAM_ISP, "Enter with bubble_state %d, req_bubble_detected %d, ctx %u, link[0x%x]",
  1953. bubble_state, req_isp->bubble_detected, ctx->ctx_id, ctx->link_hdl);
  1954. unhandled_done.timestamp = done->timestamp;
  1955. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  1956. if (done->resource_handle ==
  1957. req_isp->fence_map_out[i].resource_handle) {
  1958. cmp_addr = cam_smmu_is_expanded_memory() ? CAM_36BIT_INTF_GET_IOVA_BASE(
  1959. req_isp->fence_map_out[i].image_buf_addr[0]) :
  1960. req_isp->fence_map_out[i].image_buf_addr[0];
  1961. if (!verify_consumed_addr ||
  1962. (verify_consumed_addr && (done->last_consumed_addr == cmp_addr))) {
  1963. break;
  1964. }
  1965. }
  1966. }
  1967. if (done->hw_type == CAM_ISP_HW_TYPE_SFE)
  1968. comp_grp = &ctx_isp->sfe_bus_comp_grp[done->comp_group_id];
  1969. else
  1970. comp_grp = &ctx_isp->vfe_bus_comp_grp[done->comp_group_id];
  1971. if (!comp_grp) {
  1972. CAM_ERR(CAM_ISP, "comp_grp is NULL for hw_type: %d", done->hw_type);
  1973. rc = -EINVAL;
  1974. return rc;
  1975. }
  1976. if (i == req_isp->num_fence_map_out) {
  1977. not_found = true;
  1978. for (j = 0; j < comp_grp->num_res; j++) {
  1979. /* If the res is same with original res, we don't need to read again */
  1980. if (comp_grp->res_id[j] == done->resource_handle)
  1981. continue;
  1982. /* Check if the res in the requested list */
  1983. for (k = 0; k < req_isp->num_fence_map_out; k++)
  1984. if (comp_grp->res_id[j] ==
  1985. req_isp->fence_map_out[k].resource_handle)
  1986. break;
  1987. /* If res_id[j] isn't in requested list, then try next res in the group */
  1988. if (k == req_isp->num_fence_map_out) {
  1989. if (j != comp_grp->num_res - 1)
  1990. continue;
  1991. else
  1992. break;
  1993. }
  1994. if (!verify_consumed_addr) {
  1995. not_found = false;
  1996. break;
  1997. }
  1998. /*
  1999. * Find out the res from the requested list,
  2000. * then we can get last consumed address from this port.
  2001. */
  2002. done->resource_handle = comp_grp->res_id[j];
  2003. done->last_consumed_addr = 0;
  2004. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2005. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  2006. isp_hw_cmd_args.cmd_type =
  2007. CAM_ISP_HW_MGR_GET_LAST_CONSUMED_ADDR;
  2008. isp_hw_cmd_args.cmd_data = done;
  2009. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  2010. rc = ctx->hw_mgr_intf->hw_cmd(
  2011. ctx->hw_mgr_intf->hw_mgr_priv,
  2012. &hw_cmd_args);
  2013. if (rc) {
  2014. CAM_ERR(CAM_ISP, "HW command failed, ctx %u, link: 0x%x",
  2015. ctx->ctx_id, ctx->link_hdl);
  2016. return rc;
  2017. }
  2018. cmp_addr = cam_smmu_is_expanded_memory() ?
  2019. CAM_36BIT_INTF_GET_IOVA_BASE(
  2020. req_isp->fence_map_out[k].image_buf_addr[0]) :
  2021. req_isp->fence_map_out[k].image_buf_addr[0];
  2022. CAM_DBG(CAM_ISP,
  2023. "Get res %s comp_grp_rec_idx:%d fence_map_idx:%d last_consumed_addr:0x%x cmp_addr:0x%x",
  2024. __cam_isp_resource_handle_id_to_type(
  2025. ctx_isp->isp_device_type, done->resource_handle), j, k,
  2026. done->last_consumed_addr, cmp_addr);
  2027. if (done->last_consumed_addr == cmp_addr) {
  2028. CAM_DBG(CAM_ISP, "Consumed addr compare success for res:%s ",
  2029. __cam_isp_resource_handle_id_to_type(
  2030. ctx_isp->isp_device_type, done->resource_handle));
  2031. not_found = false;
  2032. break;
  2033. }
  2034. }
  2035. }
  2036. if (not_found) {
  2037. /*
  2038. * If not found in current request, it could be
  2039. * belonging to next request, this can happen if
  2040. * IRQ delay happens. It is only valid when the
  2041. * platform doesn't have last consumed address.
  2042. */
  2043. CAM_WARN(CAM_ISP,
  2044. "BUF_DONE for res %s last_consumed_addr:0x%x not found in Req %lld ",
  2045. __cam_isp_resource_handle_id_to_type(
  2046. ctx_isp->isp_device_type, done->resource_handle),
  2047. done->last_consumed_addr,
  2048. req->request_id);
  2049. unhandled_done.hw_type = done->hw_type;
  2050. unhandled_done.resource_handle = done->resource_handle;
  2051. unhandled_done.comp_group_id = done->comp_group_id;
  2052. unhandled_done.last_consumed_addr = done->last_consumed_addr;
  2053. goto check_deferred;
  2054. }
  2055. for (i = 0; i < comp_grp->num_res; i++) {
  2056. for (j = 0; j < req_isp->num_fence_map_out; j++) {
  2057. if (comp_grp->res_id[i] ==
  2058. req_isp->fence_map_out[j].resource_handle)
  2059. break;
  2060. }
  2061. if (j == req_isp->num_fence_map_out) {
  2062. /*
  2063. * If not found in current request, it could be
  2064. * belonging to an active port with no valid fence
  2065. * bound to it, we needn't process it.
  2066. */
  2067. CAM_DBG(CAM_ISP,
  2068. "BUF_DONE for res %s not active in Req %lld ctx %u, link[0x%x]",
  2069. __cam_isp_resource_handle_id_to_type(
  2070. ctx_isp->isp_device_type, comp_grp->res_id[i]),
  2071. req->request_id, ctx->ctx_id, ctx->link_hdl);
  2072. continue;
  2073. }
  2074. if (req_isp->fence_map_out[j].sync_id == -1) {
  2075. handle_type = __cam_isp_resource_handle_id_to_type(
  2076. ctx_isp->isp_device_type,
  2077. req_isp->fence_map_out[j].resource_handle);
  2078. CAM_WARN(CAM_ISP,
  2079. "Duplicate BUF_DONE for req %lld : i=%d, j=%d, res=%s, ctx %u, link[0x%x]",
  2080. req->request_id, i, j, handle_type, ctx->ctx_id, ctx->link_hdl);
  2081. trace_cam_log_event("Duplicate BufDone",
  2082. handle_type, req->request_id, ctx->ctx_id);
  2083. continue;
  2084. }
  2085. /* Get buf handles from packet and retrieve them from presil framework */
  2086. if (cam_presil_mode_enabled()) {
  2087. rc = cam_presil_retrieve_buffers_from_packet(req_isp->hw_update_data.packet,
  2088. ctx->img_iommu_hdl, req_isp->fence_map_out[j].resource_handle);
  2089. if (rc) {
  2090. CAM_ERR(CAM_ISP,
  2091. "Failed to retrieve image buffers req_id:%d ctx_id:%u link[0x%x] bubble detected:%d rc:%d",
  2092. req->request_id, ctx->ctx_id, ctx->link_hdl,
  2093. req_isp->bubble_detected, rc);
  2094. return rc;
  2095. }
  2096. }
  2097. if (defer_buf_done) {
  2098. uint32_t deferred_indx = req_isp->num_deferred_acks;
  2099. duplicate_defer_buf_done = false;
  2100. for (k = 0; k < req_isp->num_deferred_acks; k++) {
  2101. def_idx = req_isp->deferred_fence_map_index[k];
  2102. if (def_idx == j) {
  2103. CAM_WARN(CAM_ISP,
  2104. "duplicate deferred ack for ctx[%u] link[0x%x] req %lld res 0x%x sync_id 0x%x",
  2105. ctx->ctx_id, ctx->link_hdl,
  2106. req->request_id,
  2107. req_isp->fence_map_out[j].resource_handle,
  2108. req_isp->fence_map_out[j].sync_id);
  2109. duplicate_defer_buf_done = true;
  2110. break;
  2111. }
  2112. }
  2113. if (duplicate_defer_buf_done)
  2114. continue;
  2115. if (req_isp->num_deferred_acks == req_isp->num_fence_map_out) {
  2116. CAM_WARN(CAM_ISP,
  2117. "WARNING: req_id %lld num_deferred_acks %d > map_out %d, ctx_idx:%u link[0x%x]",
  2118. req->request_id, req_isp->num_deferred_acks,
  2119. req_isp->num_fence_map_out, ctx->ctx_id, ctx->link_hdl);
  2120. continue;
  2121. }
  2122. /*
  2123. * If we are handling this BUF_DONE event for a request
  2124. * that is still in wait_list, do not signal now,
  2125. * instead mark it as done and handle it later -
  2126. * if this request is going into BUBBLE state later
  2127. * it will automatically be re-applied. If this is not
  2128. * going into BUBBLE, signal fences later.
  2129. * Note - we will come here only if the last consumed
  2130. * address matches with this ports buffer.
  2131. */
  2132. req_isp->deferred_fence_map_index[deferred_indx] = j;
  2133. req_isp->num_deferred_acks++;
  2134. CAM_DBG(CAM_ISP,
  2135. "ctx[%u] link[0x%x]:Deferred buf done for %llu with bubble state %d recovery %d",
  2136. ctx->ctx_id, ctx->link_hdl, req->request_id, bubble_state,
  2137. req_isp->bubble_report);
  2138. CAM_DBG(CAM_ISP,
  2139. "ctx[%u] link[0x%x]:Deferred info:num_acks=%d,fence_map_index=%d,resource_handle=0x%x,sync_id=%d",
  2140. ctx->ctx_id, ctx->link_hdl, req_isp->num_deferred_acks, j,
  2141. req_isp->fence_map_out[j].resource_handle,
  2142. req_isp->fence_map_out[j].sync_id);
  2143. continue;
  2144. } else if (!req_isp->bubble_detected) {
  2145. CAM_DBG(CAM_ISP,
  2146. "Sync with success: req %lld res 0x%x fd 0x%x, ctx %u res %s",
  2147. req->request_id,
  2148. req_isp->fence_map_out[j].resource_handle,
  2149. req_isp->fence_map_out[j].sync_id,
  2150. ctx->ctx_id,
  2151. __cam_isp_resource_handle_id_to_type(ctx_isp->isp_device_type,
  2152. req_isp->fence_map_out[j].resource_handle));
  2153. cam_smmu_buffer_tracker_buffer_putref(
  2154. req_isp->fence_map_out[j].buffer_tracker);
  2155. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  2156. CAM_SYNC_STATE_SIGNALED_SUCCESS,
  2157. CAM_SYNC_COMMON_EVENT_SUCCESS);
  2158. if (rc) {
  2159. CAM_ERR(CAM_ISP,
  2160. "Sync=%u for req=%llu failed with rc=%d ctx:%u link[0x%x]",
  2161. req_isp->fence_map_out[j].sync_id, req->request_id,
  2162. rc, ctx->ctx_id, ctx->link_hdl);
  2163. } else if (req_isp->num_deferred_acks) {
  2164. /* Process deferred buf_done acks */
  2165. __cam_isp_handle_deferred_buf_done(ctx_isp,
  2166. req, false,
  2167. CAM_SYNC_STATE_SIGNALED_SUCCESS,
  2168. CAM_SYNC_COMMON_EVENT_SUCCESS);
  2169. }
  2170. /* Reset fence */
  2171. req_isp->fence_map_out[j].sync_id = -1;
  2172. } else if (!req_isp->bubble_report) {
  2173. CAM_DBG(CAM_ISP,
  2174. "Sync with failure: req %lld res 0x%x fd 0x%x, ctx:%u link[0x%x]",
  2175. req->request_id,
  2176. req_isp->fence_map_out[j].resource_handle,
  2177. req_isp->fence_map_out[j].sync_id,
  2178. ctx->ctx_id, ctx->link_hdl);
  2179. cam_smmu_buffer_tracker_buffer_putref(
  2180. req_isp->fence_map_out[j].buffer_tracker);
  2181. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  2182. CAM_SYNC_STATE_SIGNALED_ERROR,
  2183. CAM_SYNC_ISP_EVENT_BUBBLE);
  2184. if (rc) {
  2185. CAM_ERR(CAM_ISP,
  2186. "Sync:%u for req:%llu failed with rc:%d,ctx:%u,link[0x%x]",
  2187. req_isp->fence_map_out[j].sync_id, req->request_id,
  2188. rc, ctx->ctx_id, ctx->link_hdl);
  2189. } else if (req_isp->num_deferred_acks) {
  2190. /* Process deferred buf_done acks */
  2191. __cam_isp_handle_deferred_buf_done(ctx_isp, req,
  2192. false,
  2193. CAM_SYNC_STATE_SIGNALED_ERROR,
  2194. CAM_SYNC_ISP_EVENT_BUBBLE);
  2195. }
  2196. /* Reset fence */
  2197. req_isp->fence_map_out[j].sync_id = -1;
  2198. } else {
  2199. /*
  2200. * Ignore the buffer done if bubble detect is on
  2201. * Increment the ack number here, and queue the
  2202. * request back to pending list whenever all the
  2203. * buffers are done.
  2204. */
  2205. req_isp->num_acked++;
  2206. CAM_DBG(CAM_ISP,
  2207. "buf done with bubble state %d recovery %d for req %lld, ctx_idx:%u link[0x%x]",
  2208. bubble_state,
  2209. req_isp->bubble_report,
  2210. req->request_id,
  2211. ctx->ctx_id, ctx->link_hdl);
  2212. /* Process deferred buf_done acks */
  2213. if (req_isp->num_deferred_acks)
  2214. __cam_isp_handle_deferred_buf_done(ctx_isp, req,
  2215. true,
  2216. CAM_SYNC_STATE_SIGNALED_ERROR,
  2217. CAM_SYNC_ISP_EVENT_BUBBLE);
  2218. if (req_isp->num_acked == req_isp->num_fence_map_out) {
  2219. rc = __cam_isp_ctx_handle_buf_done_for_req_list(ctx_isp, req);
  2220. if (rc)
  2221. CAM_ERR(CAM_ISP,
  2222. "Error in buf done for req = %llu with rc = %d, ctx_idx:%u link[0x%x]",
  2223. req->request_id, rc, ctx->ctx_id, ctx->link_hdl);
  2224. return rc;
  2225. }
  2226. continue;
  2227. }
  2228. CAM_DBG(CAM_ISP, "req %lld, reset sync id 0x%x ctx_idx:%u link[0x%x]",
  2229. req->request_id,
  2230. req_isp->fence_map_out[j].sync_id, ctx->ctx_id, ctx->link_hdl);
  2231. if (!rc) {
  2232. req_isp->num_acked++;
  2233. }
  2234. if ((ctx_isp->use_frame_header_ts) &&
  2235. (req_isp->hw_update_data.frame_header_res_id ==
  2236. req_isp->fence_map_out[j].resource_handle))
  2237. __cam_isp_ctx_send_sof_timestamp_frame_header(
  2238. ctx_isp,
  2239. req_isp->hw_update_data.frame_header_cpu_addr,
  2240. req->request_id, CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2241. }
  2242. check_deferred:
  2243. if ((unhandled_done.resource_handle > 0) && (!defer_buf_done))
  2244. __cam_isp_ctx_check_deferred_buf_done(
  2245. ctx_isp, &unhandled_done, bubble_state);
  2246. if (req_isp->num_acked > req_isp->num_fence_map_out) {
  2247. /* Should not happen */
  2248. CAM_ERR(CAM_ISP,
  2249. "WARNING: req_id %lld num_acked %d > map_out %d, ctx_idx:%u link[0x%x]",
  2250. req->request_id, req_isp->num_acked,
  2251. req_isp->num_fence_map_out, ctx->ctx_id, ctx->link_hdl);
  2252. }
  2253. if (req_isp->num_acked != req_isp->num_fence_map_out)
  2254. return rc;
  2255. rc = __cam_isp_ctx_handle_buf_done_for_req_list(ctx_isp, req);
  2256. return rc;
  2257. }
  2258. static int __cam_isp_ctx_handle_buf_done(
  2259. struct cam_isp_context *ctx_isp,
  2260. struct cam_isp_hw_done_event_data *done,
  2261. uint32_t bubble_state)
  2262. {
  2263. int rc = 0;
  2264. struct cam_ctx_request *req;
  2265. struct cam_context *ctx = ctx_isp->base;
  2266. struct cam_isp_hw_done_event_data done_next_req = {0};
  2267. if (list_empty(&ctx->active_req_list)) {
  2268. CAM_WARN(CAM_ISP, "Buf done with no active request, ctx_idx:%u link[0x%x]",
  2269. ctx->ctx_id, ctx->link_hdl);
  2270. return 0;
  2271. }
  2272. req = list_first_entry(&ctx->active_req_list,
  2273. struct cam_ctx_request, list);
  2274. rc = __cam_isp_ctx_handle_buf_done_for_request(ctx_isp, req, done,
  2275. bubble_state, &done_next_req);
  2276. if (done_next_req.resource_handle) {
  2277. struct cam_isp_hw_done_event_data unhandled_res = {0};
  2278. struct cam_ctx_request *next_req = list_last_entry(
  2279. &ctx->active_req_list, struct cam_ctx_request, list);
  2280. if (next_req->request_id != req->request_id) {
  2281. /*
  2282. * Few resource handles are already signalled in the
  2283. * current request, lets check if there is another
  2284. * request waiting for these resources. This can
  2285. * happen if handling some of next request's buf done
  2286. * events are happening first before handling current
  2287. * request's remaining buf dones due to IRQ scheduling.
  2288. * Lets check only one more request as we will have
  2289. * maximum of 2 requests in active_list at any time.
  2290. */
  2291. CAM_WARN(CAM_ISP,
  2292. "Unhandled bufdone resources for req %lld,trying next request %lld,ctx:%u link[0x%x]",
  2293. req->request_id, next_req->request_id, ctx->ctx_id, ctx->link_hdl);
  2294. __cam_isp_ctx_handle_buf_done_for_request(ctx_isp,
  2295. next_req, &done_next_req,
  2296. bubble_state, &unhandled_res);
  2297. if (unhandled_res.resource_handle == 0)
  2298. CAM_INFO(CAM_ISP,
  2299. "BUF Done event handed for next request %lld, ctx_idx:%u link[0x%x]",
  2300. next_req->request_id, ctx->ctx_id, ctx->link_hdl);
  2301. else
  2302. CAM_ERR(CAM_ISP,
  2303. "BUF Done not handled for next request %lld, ctx_idx:%u link[0x%x]",
  2304. next_req->request_id, ctx->ctx_id, ctx->link_hdl);
  2305. } else {
  2306. CAM_WARN(CAM_ISP,
  2307. "Req %lld only active request, spurious buf_done rxd, ctx_idx:%u link[0x%x]",
  2308. req->request_id, ctx->ctx_id, ctx->link_hdl);
  2309. }
  2310. }
  2311. return rc;
  2312. }
  2313. static void __cam_isp_ctx_buf_done_match_req(
  2314. struct cam_ctx_request *req,
  2315. struct cam_isp_hw_done_event_data *done,
  2316. bool *irq_delay_detected)
  2317. {
  2318. int i;
  2319. uint32_t match_count = 0;
  2320. struct cam_isp_ctx_req *req_isp;
  2321. uint32_t cmp_addr = 0;
  2322. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2323. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  2324. cmp_addr = cam_smmu_is_expanded_memory() ? CAM_36BIT_INTF_GET_IOVA_BASE(
  2325. req_isp->fence_map_out[i].image_buf_addr[0]) :
  2326. req_isp->fence_map_out[i].image_buf_addr[0];
  2327. if ((done->resource_handle ==
  2328. req_isp->fence_map_out[i].resource_handle) &&
  2329. (done->last_consumed_addr == cmp_addr)) {
  2330. match_count++;
  2331. break;
  2332. }
  2333. }
  2334. if (match_count > 0)
  2335. *irq_delay_detected = true;
  2336. else
  2337. *irq_delay_detected = false;
  2338. CAM_DBG(CAM_ISP,
  2339. "buf done num handles %d match count %d for next req:%lld",
  2340. done->resource_handle, match_count, req->request_id);
  2341. CAM_DBG(CAM_ISP,
  2342. "irq_delay_detected %d", *irq_delay_detected);
  2343. }
  2344. static void __cam_isp_ctx_try_buf_done_process_for_active_request(
  2345. uint32_t deferred_ack_start_idx, struct cam_isp_context *ctx_isp,
  2346. struct cam_ctx_request *deferred_req)
  2347. {
  2348. int i, j, deferred_map_idx, rc;
  2349. struct cam_context *ctx = ctx_isp->base;
  2350. struct cam_ctx_request *curr_active_req;
  2351. struct cam_isp_ctx_req *curr_active_isp_req;
  2352. struct cam_isp_ctx_req *deferred_isp_req;
  2353. if (list_empty(&ctx->active_req_list))
  2354. return;
  2355. curr_active_req = list_first_entry(&ctx->active_req_list,
  2356. struct cam_ctx_request, list);
  2357. curr_active_isp_req = (struct cam_isp_ctx_req *)curr_active_req->req_priv;
  2358. deferred_isp_req = (struct cam_isp_ctx_req *)deferred_req->req_priv;
  2359. /* Check from newly updated deferred acks */
  2360. for (i = deferred_ack_start_idx; i < deferred_isp_req->num_deferred_acks; i++) {
  2361. deferred_map_idx = deferred_isp_req->deferred_fence_map_index[i];
  2362. for (j = 0; j < curr_active_isp_req->num_fence_map_out; j++) {
  2363. /* resource needs to match */
  2364. if (curr_active_isp_req->fence_map_out[j].resource_handle !=
  2365. deferred_isp_req->fence_map_out[deferred_map_idx].resource_handle)
  2366. continue;
  2367. /* Check if fence is valid */
  2368. if (curr_active_isp_req->fence_map_out[j].sync_id == -1)
  2369. break;
  2370. CAM_WARN(CAM_ISP,
  2371. "Processing delayed buf done req: %llu bubble_detected: %s res: 0x%x fd: 0x%x, ctx: %u link: 0x%x [deferred req: %llu last applied: %llu]",
  2372. curr_active_req->request_id,
  2373. CAM_BOOL_TO_YESNO(curr_active_isp_req->bubble_detected),
  2374. curr_active_isp_req->fence_map_out[j].resource_handle,
  2375. curr_active_isp_req->fence_map_out[j].sync_id,
  2376. ctx->ctx_id, ctx->link_hdl,
  2377. deferred_req->request_id, ctx_isp->last_applied_req_id);
  2378. /* Signal only if bubble is not detected for this request */
  2379. if (!curr_active_isp_req->bubble_detected) {
  2380. rc = cam_sync_signal(curr_active_isp_req->fence_map_out[j].sync_id,
  2381. CAM_SYNC_STATE_SIGNALED_SUCCESS,
  2382. CAM_SYNC_COMMON_EVENT_SUCCESS);
  2383. if (rc)
  2384. CAM_ERR(CAM_ISP,
  2385. "Sync: %d for req: %llu failed with rc: %d, ctx: %u link: 0x%x",
  2386. curr_active_isp_req->fence_map_out[j].sync_id,
  2387. curr_active_req->request_id, rc,
  2388. ctx->ctx_id, ctx->link_hdl);
  2389. curr_active_isp_req->fence_map_out[j].sync_id = -1;
  2390. }
  2391. curr_active_isp_req->num_acked++;
  2392. break;
  2393. }
  2394. }
  2395. }
  2396. static int __cam_isp_ctx_check_deferred_buf_done(
  2397. struct cam_isp_context *ctx_isp,
  2398. struct cam_isp_hw_done_event_data *done,
  2399. uint32_t bubble_state)
  2400. {
  2401. int rc = 0;
  2402. uint32_t curr_num_deferred = 0;
  2403. struct cam_ctx_request *req;
  2404. struct cam_context *ctx = ctx_isp->base;
  2405. struct cam_isp_ctx_req *req_isp;
  2406. bool req_in_pending_wait_list = false;
  2407. if (!list_empty(&ctx->wait_req_list)) {
  2408. req = list_first_entry(&ctx->wait_req_list,
  2409. struct cam_ctx_request, list);
  2410. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2411. curr_num_deferred = req_isp->num_deferred_acks;
  2412. req_in_pending_wait_list = true;
  2413. if (ctx_isp->last_applied_req_id !=
  2414. ctx_isp->last_bufdone_err_apply_req_id) {
  2415. CAM_DBG(CAM_ISP,
  2416. "Trying to find buf done with req in wait list, req %llu last apply id:%lld last err id:%lld curr_num_deferred: %u, ctx: %u link: 0x%x",
  2417. req->request_id, ctx_isp->last_applied_req_id,
  2418. ctx_isp->last_bufdone_err_apply_req_id, curr_num_deferred,
  2419. ctx->ctx_id, ctx->link_hdl);
  2420. ctx_isp->last_bufdone_err_apply_req_id =
  2421. ctx_isp->last_applied_req_id;
  2422. }
  2423. /*
  2424. * Verify consumed address for this request to make sure
  2425. * we are handling the buf_done for the correct
  2426. * buffer. Also defer actual buf_done handling, i.e
  2427. * do not signal the fence as this request may go into
  2428. * Bubble state eventully.
  2429. */
  2430. rc = __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  2431. ctx_isp, req, done, bubble_state, true, true);
  2432. /* Check for active req if any deferred is processed */
  2433. if (req_isp->num_deferred_acks > curr_num_deferred)
  2434. __cam_isp_ctx_try_buf_done_process_for_active_request(
  2435. curr_num_deferred, ctx_isp, req);
  2436. } else if (!list_empty(&ctx->pending_req_list)) {
  2437. /*
  2438. * We saw the case that the hw config is blocked due to
  2439. * some reason, the we get the reg upd and buf done before
  2440. * the req is added to wait req list.
  2441. */
  2442. req = list_first_entry(&ctx->pending_req_list,
  2443. struct cam_ctx_request, list);
  2444. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2445. curr_num_deferred = req_isp->num_deferred_acks;
  2446. req_in_pending_wait_list = true;
  2447. if (ctx_isp->last_applied_req_id !=
  2448. ctx_isp->last_bufdone_err_apply_req_id) {
  2449. CAM_DBG(CAM_ISP,
  2450. "Trying to find buf done with req in pending list, req %llu last apply id:%lld last err id:%lld curr_num_deferred: %u, ctx: %u link: 0x%x",
  2451. req->request_id, ctx_isp->last_applied_req_id,
  2452. ctx_isp->last_bufdone_err_apply_req_id, curr_num_deferred,
  2453. ctx->ctx_id, ctx->link_hdl);
  2454. ctx_isp->last_bufdone_err_apply_req_id =
  2455. ctx_isp->last_applied_req_id;
  2456. }
  2457. /*
  2458. * Verify consumed address for this request to make sure
  2459. * we are handling the buf_done for the correct
  2460. * buffer. Also defer actual buf_done handling, i.e
  2461. * do not signal the fence as this request may go into
  2462. * Bubble state eventully.
  2463. */
  2464. rc = __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  2465. ctx_isp, req, done, bubble_state, true, true);
  2466. /* Check for active req if any deferred is processed */
  2467. if (req_isp->num_deferred_acks > curr_num_deferred)
  2468. __cam_isp_ctx_try_buf_done_process_for_active_request(
  2469. curr_num_deferred, ctx_isp, req);
  2470. }
  2471. if (!req_in_pending_wait_list && (ctx_isp->last_applied_req_id !=
  2472. ctx_isp->last_bufdone_err_apply_req_id)) {
  2473. CAM_DBG(CAM_ISP,
  2474. "Bufdone without active request bubble_state=%d last_applied_req_id:%lld,ctx:%u link:0x%x",
  2475. bubble_state, ctx_isp->last_applied_req_id, ctx->ctx_id, ctx->link_hdl);
  2476. ctx_isp->last_bufdone_err_apply_req_id =
  2477. ctx_isp->last_applied_req_id;
  2478. }
  2479. return rc;
  2480. }
  2481. static int __cam_isp_ctx_handle_buf_done_verify_addr(
  2482. struct cam_isp_context *ctx_isp,
  2483. struct cam_isp_hw_done_event_data *done,
  2484. uint32_t bubble_state)
  2485. {
  2486. int rc = 0;
  2487. bool irq_delay_detected = false;
  2488. struct cam_ctx_request *req;
  2489. struct cam_ctx_request *next_req = NULL;
  2490. struct cam_context *ctx = ctx_isp->base;
  2491. if (list_empty(&ctx->active_req_list)) {
  2492. return __cam_isp_ctx_check_deferred_buf_done(
  2493. ctx_isp, done, bubble_state);
  2494. }
  2495. req = list_first_entry(&ctx->active_req_list,
  2496. struct cam_ctx_request, list);
  2497. if (ctx_isp->active_req_cnt > 1) {
  2498. next_req = list_last_entry(
  2499. &ctx->active_req_list,
  2500. struct cam_ctx_request, list);
  2501. if (next_req->request_id != req->request_id)
  2502. __cam_isp_ctx_buf_done_match_req(next_req, done,
  2503. &irq_delay_detected);
  2504. else
  2505. CAM_WARN(CAM_ISP,
  2506. "Req %lld only active request, spurious buf_done rxd, ctx: %u link: 0x%x",
  2507. req->request_id, ctx->ctx_id, ctx->link_hdl);
  2508. }
  2509. /*
  2510. * If irq delay isn't detected, then we need to verify
  2511. * the consumed address for current req, otherwise, we
  2512. * can't verify the consumed address.
  2513. */
  2514. rc = __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  2515. ctx_isp, req, done, bubble_state,
  2516. !irq_delay_detected, false);
  2517. /*
  2518. * Verify the consumed address for next req all the time,
  2519. * since the reported buf done event may belong to current
  2520. * req, then we can't signal this event for next req.
  2521. */
  2522. if (!rc && irq_delay_detected)
  2523. rc = __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  2524. ctx_isp, next_req, done,
  2525. bubble_state, true, false);
  2526. return rc;
  2527. }
  2528. static int __cam_isp_ctx_handle_buf_done_in_activated_state(
  2529. struct cam_isp_context *ctx_isp,
  2530. struct cam_isp_hw_done_event_data *done,
  2531. uint32_t bubble_state)
  2532. {
  2533. int rc = 0;
  2534. if (ctx_isp->support_consumed_addr)
  2535. rc = __cam_isp_ctx_handle_buf_done_verify_addr(
  2536. ctx_isp, done, bubble_state);
  2537. else
  2538. rc = __cam_isp_ctx_handle_buf_done(
  2539. ctx_isp, done, bubble_state);
  2540. return rc;
  2541. }
  2542. static int __cam_isp_ctx_apply_pending_req(
  2543. void *priv, void *data)
  2544. {
  2545. int rc = 0;
  2546. int64_t prev_applied_req;
  2547. struct cam_context *ctx = NULL;
  2548. struct cam_isp_context *ctx_isp = priv;
  2549. struct cam_ctx_request *req;
  2550. struct cam_isp_ctx_req *req_isp;
  2551. struct cam_hw_config_args cfg = {0};
  2552. if (!ctx_isp) {
  2553. CAM_ERR(CAM_ISP, "Invalid ctx_isp:%pK", ctx);
  2554. rc = -EINVAL;
  2555. goto end;
  2556. }
  2557. ctx = ctx_isp->base;
  2558. if (list_empty(&ctx->pending_req_list)) {
  2559. CAM_DBG(CAM_ISP,
  2560. "No pending requests to apply, ctx_idx: %u, link: 0x%x",
  2561. ctx->ctx_id, ctx->link_hdl);
  2562. rc = -EFAULT;
  2563. goto end;
  2564. }
  2565. if (ctx_isp->vfps_aux_context) {
  2566. if (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_APPLIED)
  2567. goto end;
  2568. if (ctx_isp->active_req_cnt >= 1)
  2569. goto end;
  2570. } else {
  2571. if ((ctx->state != CAM_CTX_ACTIVATED) ||
  2572. (!atomic_read(&ctx_isp->rxd_epoch)) ||
  2573. (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_APPLIED))
  2574. goto end;
  2575. if (ctx_isp->active_req_cnt >= 2)
  2576. goto end;
  2577. }
  2578. spin_lock_bh(&ctx->lock);
  2579. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request,
  2580. list);
  2581. spin_unlock_bh(&ctx->lock);
  2582. CAM_DBG(CAM_REQ, "Apply request %lld in substate %d ctx_idx: %u, link: 0x%x",
  2583. req->request_id, ctx_isp->substate_activated, ctx->ctx_id, ctx->link_hdl);
  2584. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2585. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2586. cfg.request_id = req->request_id;
  2587. cfg.hw_update_entries = req_isp->cfg;
  2588. cfg.num_hw_update_entries = req_isp->num_cfg;
  2589. cfg.priv = &req_isp->hw_update_data;
  2590. /*
  2591. * Offline mode may receive the SOF and REG_UPD earlier than
  2592. * CDM processing return back, so we set the substate before
  2593. * apply setting.
  2594. */
  2595. spin_lock_bh(&ctx->lock);
  2596. atomic_set(&ctx_isp->rxd_epoch, 0);
  2597. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_APPLIED;
  2598. prev_applied_req = ctx_isp->last_applied_req_id;
  2599. ctx_isp->last_applied_req_id = req->request_id;
  2600. atomic_set(&ctx_isp->apply_in_progress, 1);
  2601. list_del_init(&req->list);
  2602. list_add_tail(&req->list, &ctx->wait_req_list);
  2603. spin_unlock_bh(&ctx->lock);
  2604. rc = ctx->hw_mgr_intf->hw_config(ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  2605. if (rc) {
  2606. CAM_ERR_RATE_LIMIT(CAM_ISP,
  2607. "Can not apply the configuration,ctx: %u,link: 0x%x",
  2608. ctx->ctx_id, ctx->link_hdl);
  2609. spin_lock_bh(&ctx->lock);
  2610. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  2611. ctx_isp->last_applied_req_id = prev_applied_req;
  2612. atomic_set(&ctx_isp->apply_in_progress, 0);
  2613. list_del_init(&req->list);
  2614. list_add(&req->list, &ctx->pending_req_list);
  2615. spin_unlock_bh(&ctx->lock);
  2616. } else {
  2617. atomic_set(&ctx_isp->apply_in_progress, 0);
  2618. CAM_DBG(CAM_ISP, "New substate state %d, applied req %lld, ctx: %u, link: 0x%x",
  2619. CAM_ISP_CTX_ACTIVATED_APPLIED,
  2620. ctx_isp->last_applied_req_id, ctx->ctx_id, ctx->link_hdl);
  2621. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2622. CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED,
  2623. req->request_id);
  2624. }
  2625. end:
  2626. return rc;
  2627. }
  2628. static int __cam_isp_ctx_schedule_apply_req(
  2629. struct cam_isp_context *ctx_isp)
  2630. {
  2631. int rc = 0;
  2632. struct crm_workq_task *task;
  2633. task = cam_req_mgr_workq_get_task(ctx_isp->workq);
  2634. if (!task) {
  2635. CAM_ERR(CAM_ISP, "No task for worker");
  2636. return -ENOMEM;
  2637. }
  2638. task->process_cb = __cam_isp_ctx_apply_pending_req;
  2639. rc = cam_req_mgr_workq_enqueue_task(task, ctx_isp, CRM_TASK_PRIORITY_0);
  2640. if (rc)
  2641. CAM_ERR(CAM_ISP, "Failed to schedule task rc:%d", rc);
  2642. return rc;
  2643. }
  2644. static int __cam_isp_ctx_offline_epoch_in_activated_state(
  2645. struct cam_isp_context *ctx_isp, void *evt_data)
  2646. {
  2647. struct cam_context *ctx = ctx_isp->base;
  2648. struct cam_ctx_request *req, *req_temp;
  2649. uint64_t request_id = 0;
  2650. atomic_set(&ctx_isp->rxd_epoch, 1);
  2651. CAM_DBG(CAM_ISP, "SOF frame %lld ctx %u link: 0x%x", ctx_isp->frame_id,
  2652. ctx->ctx_id, ctx->link_hdl);
  2653. /*
  2654. * For offline it is not possible for epoch to be generated without
  2655. * RUP done. IRQ scheduling delays can possibly cause this.
  2656. */
  2657. if (list_empty(&ctx->active_req_list)) {
  2658. CAM_WARN(CAM_ISP,
  2659. "Active list empty on ctx:%u link:0x%x - EPOCH serviced before RUP",
  2660. ctx->ctx_id, ctx->link_hdl);
  2661. } else {
  2662. list_for_each_entry_safe(req, req_temp, &ctx->active_req_list, list) {
  2663. if (req->request_id > ctx_isp->reported_req_id) {
  2664. request_id = req->request_id;
  2665. ctx_isp->reported_req_id = request_id;
  2666. break;
  2667. }
  2668. }
  2669. }
  2670. __cam_isp_ctx_schedule_apply_req(ctx_isp);
  2671. /*
  2672. * If no valid request, wait for RUP shutter posted after buf done
  2673. */
  2674. if (request_id)
  2675. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2676. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2677. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2678. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH,
  2679. request_id);
  2680. return 0;
  2681. }
  2682. static int __cam_isp_ctx_reg_upd_in_epoch_bubble_state(
  2683. struct cam_isp_context *ctx_isp, void *evt_data)
  2684. {
  2685. if (ctx_isp->frame_id == 1)
  2686. CAM_DBG(CAM_ISP, "Reg update in Substate[%s] for early PCR",
  2687. __cam_isp_ctx_substate_val_to_type(
  2688. ctx_isp->substate_activated));
  2689. else
  2690. CAM_WARN_RATE_LIMIT(CAM_ISP,
  2691. "ctx:%u Unexpected regupdate in activated Substate[%s] for frame_id:%lld",
  2692. ctx_isp->base->ctx_id,
  2693. __cam_isp_ctx_substate_val_to_type(
  2694. ctx_isp->substate_activated),
  2695. ctx_isp->frame_id);
  2696. return 0;
  2697. }
  2698. static int __cam_isp_ctx_reg_upd_in_applied_state(
  2699. struct cam_isp_context *ctx_isp, void *evt_data)
  2700. {
  2701. int rc = 0;
  2702. struct cam_ctx_request *req;
  2703. struct cam_context *ctx = ctx_isp->base;
  2704. struct cam_isp_ctx_req *req_isp;
  2705. uint64_t request_id = 0;
  2706. if (list_empty(&ctx->wait_req_list)) {
  2707. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request, ctx_idx: %u, link: 0x%x",
  2708. ctx->ctx_id, ctx->link_hdl);
  2709. goto end;
  2710. }
  2711. req = list_first_entry(&ctx->wait_req_list,
  2712. struct cam_ctx_request, list);
  2713. list_del_init(&req->list);
  2714. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2715. if (req_isp->num_fence_map_out != 0) {
  2716. list_add_tail(&req->list, &ctx->active_req_list);
  2717. ctx_isp->active_req_cnt++;
  2718. request_id = req->request_id;
  2719. CAM_DBG(CAM_REQ,
  2720. "move request %lld to active list(cnt = %d), ctx %u, link: 0x%x",
  2721. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  2722. __cam_isp_ctx_update_event_record(ctx_isp,
  2723. CAM_ISP_CTX_EVENT_RUP, req, NULL);
  2724. } else {
  2725. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  2726. /* no io config, so the request is completed. */
  2727. list_add_tail(&req->list, &ctx->free_req_list);
  2728. CAM_DBG(CAM_ISP,
  2729. "move active request %lld to free list(cnt = %d), ctx %u, link: 0x%x",
  2730. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  2731. }
  2732. /*
  2733. * This function only called directly from applied and bubble applied
  2734. * state so change substate here.
  2735. */
  2736. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  2737. CAM_DBG(CAM_ISP, "next Substate[%s], ctx %u, link: 0x%x",
  2738. __cam_isp_ctx_substate_val_to_type(
  2739. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  2740. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2741. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE, request_id);
  2742. end:
  2743. return rc;
  2744. }
  2745. static int __cam_isp_ctx_notify_sof_in_activated_state(
  2746. struct cam_isp_context *ctx_isp, void *evt_data)
  2747. {
  2748. int rc = 0;
  2749. uint64_t request_id = 0;
  2750. struct cam_context *ctx = ctx_isp->base;
  2751. struct cam_ctx_request *req;
  2752. struct cam_isp_ctx_req *req_isp;
  2753. struct cam_hw_cmd_args hw_cmd_args;
  2754. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  2755. uint64_t last_cdm_done_req = 0;
  2756. struct cam_isp_hw_epoch_event_data *epoch_done_event_data =
  2757. (struct cam_isp_hw_epoch_event_data *)evt_data;
  2758. if (!evt_data) {
  2759. CAM_ERR(CAM_ISP, "invalid event data");
  2760. return -EINVAL;
  2761. }
  2762. ctx_isp->frame_id_meta = epoch_done_event_data->frame_id_meta;
  2763. if (atomic_read(&ctx_isp->process_bubble)) {
  2764. if (list_empty(&ctx->active_req_list)) {
  2765. CAM_ERR(CAM_ISP,
  2766. "No available active req in bubble, ctx %u, link: 0x%x",
  2767. ctx->ctx_id, ctx->link_hdl);
  2768. atomic_set(&ctx_isp->process_bubble, 0);
  2769. ctx_isp->bubble_frame_cnt = 0;
  2770. rc = -EINVAL;
  2771. return rc;
  2772. }
  2773. if (ctx_isp->last_sof_timestamp ==
  2774. ctx_isp->sof_timestamp_val) {
  2775. CAM_DBG(CAM_ISP,
  2776. "Tasklet delay detected! Bubble frame check skipped, sof_timestamp: %lld, ctx %u, link: 0x%x",
  2777. ctx_isp->sof_timestamp_val, ctx->ctx_id, ctx->link_hdl);
  2778. goto notify_only;
  2779. }
  2780. req = list_first_entry(&ctx->active_req_list,
  2781. struct cam_ctx_request, list);
  2782. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2783. if (ctx_isp->bubble_frame_cnt >= 1 &&
  2784. req_isp->bubble_detected) {
  2785. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2786. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  2787. isp_hw_cmd_args.cmd_type =
  2788. CAM_ISP_HW_MGR_GET_LAST_CDM_DONE;
  2789. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  2790. rc = ctx->hw_mgr_intf->hw_cmd(
  2791. ctx->hw_mgr_intf->hw_mgr_priv,
  2792. &hw_cmd_args);
  2793. if (rc) {
  2794. CAM_ERR(CAM_ISP, "HW command failed, ctx %u, link: 0x%x",
  2795. ctx->ctx_id, ctx->link_hdl);
  2796. return rc;
  2797. }
  2798. last_cdm_done_req = isp_hw_cmd_args.u.last_cdm_done;
  2799. CAM_DBG(CAM_ISP, "last_cdm_done req: %d, ctx %u, link: 0x%x",
  2800. last_cdm_done_req, ctx->ctx_id, ctx->link_hdl);
  2801. if (last_cdm_done_req >= req->request_id) {
  2802. CAM_DBG(CAM_ISP,
  2803. "invalid sof event data CDM cb detected for req: %lld, possible buf_done delay, waiting for buf_done, ctx %u, link: 0x%x",
  2804. req->request_id, ctx->ctx_id, ctx->link_hdl);
  2805. ctx_isp->bubble_frame_cnt = 0;
  2806. } else {
  2807. CAM_DBG(CAM_ISP,
  2808. "CDM callback not happened for req: %lld, possible CDM stuck or workqueue delay, ctx %u, link: 0x%x",
  2809. req->request_id, ctx->ctx_id, ctx->link_hdl);
  2810. req_isp->num_acked = 0;
  2811. req_isp->num_deferred_acks = 0;
  2812. ctx_isp->bubble_frame_cnt = 0;
  2813. req_isp->bubble_detected = false;
  2814. req_isp->cdm_reset_before_apply = true;
  2815. list_del_init(&req->list);
  2816. list_add(&req->list, &ctx->pending_req_list);
  2817. atomic_set(&ctx_isp->process_bubble, 0);
  2818. ctx_isp->active_req_cnt--;
  2819. CAM_DBG(CAM_REQ,
  2820. "Move active req: %lld to pending list(cnt = %d) [bubble re-apply], ctx %u link: 0x%x",
  2821. req->request_id,
  2822. ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  2823. }
  2824. } else if (req_isp->bubble_detected) {
  2825. ctx_isp->bubble_frame_cnt++;
  2826. CAM_DBG(CAM_ISP,
  2827. "Waiting on bufdone for bubble req: %lld, since frame_cnt = %lld, ctx %u link: 0x%x",
  2828. req->request_id,
  2829. ctx_isp->bubble_frame_cnt, ctx->ctx_id, ctx->link_hdl);
  2830. } else {
  2831. CAM_DBG(CAM_ISP, "Delayed bufdone for req: %lld, ctx %u link: 0x%x",
  2832. req->request_id, ctx->ctx_id, ctx->link_hdl);
  2833. }
  2834. }
  2835. notify_only:
  2836. /*
  2837. * notify reqmgr with sof signal. Note, due to scheduling delay
  2838. * we can run into situation that two active requests has already
  2839. * be in the active queue while we try to do the notification.
  2840. * In this case, we need to skip the current notification. This
  2841. * helps the state machine to catch up the delay.
  2842. */
  2843. if (ctx_isp->active_req_cnt <= 2) {
  2844. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  2845. list_for_each_entry(req, &ctx->active_req_list, list) {
  2846. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2847. if ((!req_isp->bubble_detected) &&
  2848. (req->request_id > ctx_isp->reported_req_id)) {
  2849. request_id = req->request_id;
  2850. __cam_isp_ctx_update_event_record(ctx_isp,
  2851. CAM_ISP_CTX_EVENT_EPOCH, req, NULL);
  2852. break;
  2853. }
  2854. }
  2855. if (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_BUBBLE)
  2856. request_id = 0;
  2857. if (request_id != 0)
  2858. ctx_isp->reported_req_id = request_id;
  2859. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2860. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2861. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2862. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH,
  2863. request_id);
  2864. }
  2865. ctx_isp->last_sof_timestamp = ctx_isp->sof_timestamp_val;
  2866. return 0;
  2867. }
  2868. static int __cam_isp_ctx_notify_eof_in_activated_state(
  2869. struct cam_isp_context *ctx_isp, void *evt_data)
  2870. {
  2871. int rc = 0;
  2872. struct cam_context *ctx = ctx_isp->base;
  2873. uint64_t last_cdm_done_req = 0;
  2874. /* update last cdm done timestamp */
  2875. rc = __cam_isp_ctx_get_cdm_done_timestamp(ctx, &last_cdm_done_req);
  2876. if (rc)
  2877. CAM_ERR(CAM_ISP, "ctx:%u link: 0x%x Failed to get timestamp from HW",
  2878. ctx->ctx_id, ctx->link_hdl);
  2879. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2880. CAM_ISP_STATE_CHANGE_TRIGGER_CDM_DONE, last_cdm_done_req);
  2881. /* notify reqmgr with eof signal */
  2882. rc = __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_EOF, ctx_isp);
  2883. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2884. CAM_ISP_STATE_CHANGE_TRIGGER_EOF, 0);
  2885. return rc;
  2886. }
  2887. static int __cam_isp_ctx_reg_upd_in_hw_error(
  2888. struct cam_isp_context *ctx_isp, void *evt_data)
  2889. {
  2890. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  2891. return 0;
  2892. }
  2893. static int __cam_isp_ctx_sof_in_activated_state(
  2894. struct cam_isp_context *ctx_isp, void *evt_data)
  2895. {
  2896. int rc = 0;
  2897. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  2898. struct cam_ctx_request *req = NULL;
  2899. struct cam_context *ctx = ctx_isp->base;
  2900. uint64_t request_id = 0;
  2901. ctx_isp->last_sof_jiffies = jiffies;
  2902. /* First check if there is a valid request in active list */
  2903. list_for_each_entry(req, &ctx->active_req_list, list) {
  2904. if (req->request_id > ctx_isp->reported_req_id) {
  2905. request_id = req->request_id;
  2906. break;
  2907. }
  2908. }
  2909. /*
  2910. * If nothing in active list, current request might have not moved
  2911. * from wait to active list. This could happen if REG_UPDATE to sw
  2912. * is coming immediately after SOF
  2913. */
  2914. if (request_id == 0) {
  2915. req = list_first_entry(&ctx->wait_req_list,
  2916. struct cam_ctx_request, list);
  2917. if (req)
  2918. request_id = req->request_id;
  2919. }
  2920. if (!evt_data) {
  2921. CAM_ERR(CAM_ISP, "in valid sof event data, ctx_idx: %u, link: 0x%x",
  2922. ctx->ctx_id, ctx->link_hdl);
  2923. return -EINVAL;
  2924. }
  2925. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  2926. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2927. CAM_ISP_STATE_CHANGE_TRIGGER_SOF, request_id);
  2928. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx %u request %llu, link: 0x%x",
  2929. ctx_isp->frame_id, ctx_isp->sof_timestamp_val, ctx->ctx_id, request_id,
  2930. ctx->link_hdl);
  2931. return rc;
  2932. }
  2933. static int __cam_isp_ctx_reg_upd_in_sof(struct cam_isp_context *ctx_isp,
  2934. void *evt_data)
  2935. {
  2936. int rc = 0;
  2937. struct cam_ctx_request *req = NULL;
  2938. struct cam_isp_ctx_req *req_isp;
  2939. struct cam_context *ctx = ctx_isp->base;
  2940. if (ctx->state != CAM_CTX_ACTIVATED && ctx_isp->frame_id > 1) {
  2941. CAM_DBG(CAM_ISP, "invalid RUP");
  2942. goto end;
  2943. }
  2944. /*
  2945. * This is for the first update. The initial setting will
  2946. * cause the reg_upd in the first frame.
  2947. */
  2948. if (!list_empty(&ctx->wait_req_list)) {
  2949. req = list_first_entry(&ctx->wait_req_list,
  2950. struct cam_ctx_request, list);
  2951. list_del_init(&req->list);
  2952. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  2953. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2954. if (req_isp->num_fence_map_out == req_isp->num_acked)
  2955. list_add_tail(&req->list, &ctx->free_req_list);
  2956. else
  2957. CAM_ERR(CAM_ISP,
  2958. "receive rup in unexpected state, ctx_idx: %u, link: 0x%x",
  2959. ctx->ctx_id, ctx->link_hdl);
  2960. }
  2961. if (req != NULL) {
  2962. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2963. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  2964. req->request_id);
  2965. }
  2966. end:
  2967. return rc;
  2968. }
  2969. static int __cam_isp_ctx_epoch_in_applied(struct cam_isp_context *ctx_isp,
  2970. void *evt_data)
  2971. {
  2972. uint64_t request_id = 0;
  2973. uint32_t wait_req_cnt = 0;
  2974. uint32_t sof_event_status = CAM_REQ_MGR_SOF_EVENT_SUCCESS;
  2975. struct cam_ctx_request *req;
  2976. struct cam_isp_ctx_req *req_isp;
  2977. struct cam_context *ctx = ctx_isp->base;
  2978. struct cam_isp_hw_epoch_event_data *epoch_done_event_data =
  2979. (struct cam_isp_hw_epoch_event_data *)evt_data;
  2980. if (!evt_data) {
  2981. CAM_ERR(CAM_ISP, "invalid event data");
  2982. return -EINVAL;
  2983. }
  2984. ctx_isp->frame_id_meta = epoch_done_event_data->frame_id_meta;
  2985. if (list_empty(&ctx->wait_req_list)) {
  2986. /*
  2987. * If no wait req in epoch, this is an error case.
  2988. * The recovery is to go back to sof state
  2989. */
  2990. CAM_ERR(CAM_ISP, "Ctx:%u link: 0x%x No wait request", ctx->ctx_id, ctx->link_hdl);
  2991. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  2992. /* Send SOF event as empty frame*/
  2993. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2994. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2995. __cam_isp_ctx_update_event_record(ctx_isp,
  2996. CAM_ISP_CTX_EVENT_EPOCH, NULL, NULL);
  2997. goto end;
  2998. }
  2999. if (ctx_isp->last_applied_jiffies >= ctx_isp->last_sof_jiffies) {
  3000. list_for_each_entry(req, &ctx->wait_req_list, list) {
  3001. wait_req_cnt++;
  3002. }
  3003. /*
  3004. * The previous req is applied after SOF and there is only
  3005. * one applied req, we don't need to report bubble for this case.
  3006. */
  3007. if (wait_req_cnt == 1 && !ctx_isp->is_tfe_shdr) {
  3008. req = list_first_entry(&ctx->wait_req_list,
  3009. struct cam_ctx_request, list);
  3010. request_id = req->request_id;
  3011. CAM_INFO(CAM_ISP,
  3012. "ctx:%d Don't report the bubble for req:%lld",
  3013. ctx->ctx_id, request_id);
  3014. goto end;
  3015. }
  3016. }
  3017. /* Update state prior to notifying CRM */
  3018. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  3019. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  3020. list);
  3021. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  3022. req_isp->bubble_detected = true;
  3023. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  3024. req_isp->cdm_reset_before_apply = false;
  3025. atomic_set(&ctx_isp->process_bubble, 1);
  3026. CAM_INFO_RATE_LIMIT(CAM_ISP, "ctx:%u link: 0x%x Report Bubble flag %d req id:%lld",
  3027. ctx->ctx_id, ctx->link_hdl, req_isp->bubble_report, req->request_id);
  3028. if (req_isp->bubble_report) {
  3029. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_BUBBLE,
  3030. req->request_id, ctx_isp);
  3031. trace_cam_log_event("Bubble", "Rcvd epoch in applied state",
  3032. req->request_id, ctx->ctx_id);
  3033. } else {
  3034. req_isp->bubble_report = 0;
  3035. CAM_DBG(CAM_ISP, "Skip bubble recovery for req %lld ctx %u, link: 0x%x",
  3036. req->request_id, ctx->ctx_id, ctx->link_hdl);
  3037. if (ctx_isp->active_req_cnt <= 1)
  3038. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  3039. }
  3040. /*
  3041. * Always move the request to active list. Let buf done
  3042. * function handles the rest.
  3043. */
  3044. list_del_init(&req->list);
  3045. list_add_tail(&req->list, &ctx->active_req_list);
  3046. ctx_isp->active_req_cnt++;
  3047. CAM_DBG(CAM_REQ, "move request %lld to active list(cnt = %d), ctx %u, link: 0x%x",
  3048. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  3049. /*
  3050. * Handle the deferred buf done after moving
  3051. * the bubble req to active req list.
  3052. */
  3053. __cam_isp_ctx_handle_deferred_buf_done_in_bubble(
  3054. ctx_isp, req);
  3055. /*
  3056. * Update the record before req pointer to
  3057. * other invalid req.
  3058. */
  3059. __cam_isp_ctx_update_event_record(ctx_isp,
  3060. CAM_ISP_CTX_EVENT_EPOCH, req, NULL);
  3061. /*
  3062. * Get the req again from active_req_list in case
  3063. * the active req cnt is 2.
  3064. */
  3065. list_for_each_entry(req, &ctx->active_req_list, list) {
  3066. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3067. if ((!req_isp->bubble_report) &&
  3068. (req->request_id > ctx_isp->reported_req_id)) {
  3069. request_id = req->request_id;
  3070. ctx_isp->reported_req_id = request_id;
  3071. CAM_DBG(CAM_ISP,
  3072. "ctx %u link: 0x%x reported_req_id update to %lld",
  3073. ctx->ctx_id, ctx->link_hdl, ctx_isp->reported_req_id);
  3074. break;
  3075. }
  3076. }
  3077. if ((request_id != 0) && req_isp->bubble_detected)
  3078. sof_event_status = CAM_REQ_MGR_SOF_EVENT_ERROR;
  3079. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3080. sof_event_status);
  3081. cam_req_mgr_debug_delay_detect();
  3082. trace_cam_delay_detect("ISP",
  3083. "bubble epoch_in_applied", req->request_id,
  3084. ctx->ctx_id, ctx->link_hdl, ctx->session_hdl,
  3085. CAM_DEFAULT_VALUE);
  3086. end:
  3087. if (request_id == 0) {
  3088. req = list_last_entry(&ctx->active_req_list,
  3089. struct cam_ctx_request, list);
  3090. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3091. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, req->request_id);
  3092. } else {
  3093. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3094. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, request_id);
  3095. }
  3096. CAM_DBG(CAM_ISP, "next Substate[%s], ctx_idx: %u link: 0x%x",
  3097. __cam_isp_ctx_substate_val_to_type(
  3098. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  3099. return 0;
  3100. }
  3101. static int __cam_isp_ctx_buf_done_in_sof(struct cam_isp_context *ctx_isp,
  3102. void *evt_data)
  3103. {
  3104. int rc = 0;
  3105. struct cam_isp_hw_done_event_data *done =
  3106. (struct cam_isp_hw_done_event_data *) evt_data;
  3107. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  3108. return rc;
  3109. }
  3110. static int __cam_isp_ctx_buf_done_in_applied(struct cam_isp_context *ctx_isp,
  3111. void *evt_data)
  3112. {
  3113. int rc = 0;
  3114. struct cam_isp_hw_done_event_data *done =
  3115. (struct cam_isp_hw_done_event_data *) evt_data;
  3116. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  3117. return rc;
  3118. }
  3119. static int __cam_isp_ctx_sof_in_epoch(struct cam_isp_context *ctx_isp,
  3120. void *evt_data)
  3121. {
  3122. int rc = 0;
  3123. struct cam_context *ctx = ctx_isp->base;
  3124. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3125. struct cam_ctx_request *req;
  3126. if (!evt_data) {
  3127. CAM_ERR(CAM_ISP, "in valid sof event data");
  3128. return -EINVAL;
  3129. }
  3130. ctx_isp->last_sof_jiffies = jiffies;
  3131. if (atomic_read(&ctx_isp->apply_in_progress))
  3132. CAM_INFO(CAM_ISP, "Apply is in progress at the time of SOF, ctx: %u, link: 0x%x",
  3133. ctx->ctx_id, ctx->link_hdl);
  3134. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  3135. if (list_empty(&ctx->active_req_list))
  3136. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3137. else
  3138. CAM_DBG(CAM_ISP, "Still need to wait for the buf done, ctx: %u, link: 0x%x",
  3139. ctx->ctx_id, ctx->link_hdl);
  3140. req = list_last_entry(&ctx->active_req_list,
  3141. struct cam_ctx_request, list);
  3142. if (req)
  3143. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3144. CAM_ISP_STATE_CHANGE_TRIGGER_SOF,
  3145. req->request_id);
  3146. if (ctx_isp->frame_id == 1)
  3147. CAM_INFO(CAM_ISP,
  3148. "First SOF in EPCR ctx:%u link: 0x%x frame_id:%lld next substate %s",
  3149. ctx->ctx_id, ctx->link_hdl, ctx_isp->frame_id,
  3150. __cam_isp_ctx_substate_val_to_type(
  3151. ctx_isp->substate_activated));
  3152. CAM_DBG(CAM_ISP, "SOF in epoch ctx:%u link: 0x%x frame_id:%lld next substate:%s",
  3153. ctx->ctx_id, ctx->link_hdl, ctx_isp->frame_id,
  3154. __cam_isp_ctx_substate_val_to_type(
  3155. ctx_isp->substate_activated));
  3156. return rc;
  3157. }
  3158. static int __cam_isp_ctx_buf_done_in_epoch(struct cam_isp_context *ctx_isp,
  3159. void *evt_data)
  3160. {
  3161. int rc = 0;
  3162. struct cam_isp_hw_done_event_data *done =
  3163. (struct cam_isp_hw_done_event_data *) evt_data;
  3164. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  3165. return rc;
  3166. }
  3167. static int __cam_isp_ctx_buf_done_in_bubble(
  3168. struct cam_isp_context *ctx_isp, void *evt_data)
  3169. {
  3170. int rc = 0;
  3171. struct cam_isp_hw_done_event_data *done =
  3172. (struct cam_isp_hw_done_event_data *) evt_data;
  3173. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 1);
  3174. return rc;
  3175. }
  3176. static int __cam_isp_ctx_epoch_in_bubble_applied(
  3177. struct cam_isp_context *ctx_isp, void *evt_data)
  3178. {
  3179. uint64_t request_id = 0;
  3180. struct cam_ctx_request *req;
  3181. struct cam_isp_ctx_req *req_isp;
  3182. struct cam_context *ctx = ctx_isp->base;
  3183. struct cam_isp_hw_epoch_event_data *epoch_done_event_data =
  3184. (struct cam_isp_hw_epoch_event_data *)evt_data;
  3185. if (!evt_data) {
  3186. CAM_ERR(CAM_ISP, "invalid event data, ctx_idx: %u, link: 0x%x",
  3187. ctx->ctx_id, ctx->link_hdl);
  3188. return -EINVAL;
  3189. }
  3190. ctx_isp->frame_id_meta = epoch_done_event_data->frame_id_meta;
  3191. /*
  3192. * This means we missed the reg upd ack. So we need to
  3193. * transition to BUBBLE state again.
  3194. */
  3195. if (list_empty(&ctx->wait_req_list)) {
  3196. /*
  3197. * If no pending req in epoch, this is an error case.
  3198. * Just go back to the bubble state.
  3199. */
  3200. CAM_ERR(CAM_ISP, "ctx:%u link: 0x%x No pending request.",
  3201. ctx->ctx_id, ctx->link_hdl);
  3202. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3203. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3204. __cam_isp_ctx_update_event_record(ctx_isp,
  3205. CAM_ISP_CTX_EVENT_EPOCH, NULL, NULL);
  3206. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  3207. goto end;
  3208. }
  3209. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  3210. list);
  3211. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  3212. req_isp->bubble_detected = true;
  3213. CAM_INFO_RATE_LIMIT(CAM_ISP, "Ctx:%u link: 0x%x Report Bubble flag %d req id:%lld",
  3214. ctx->ctx_id, ctx->link_hdl, req_isp->bubble_report, req->request_id);
  3215. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  3216. req_isp->cdm_reset_before_apply = false;
  3217. if (req_isp->bubble_report) {
  3218. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_BUBBLE,
  3219. req->request_id, ctx_isp);
  3220. atomic_set(&ctx_isp->process_bubble, 1);
  3221. } else {
  3222. req_isp->bubble_report = 0;
  3223. CAM_DBG(CAM_ISP, "Skip bubble recovery for req %lld ctx %u link: 0x%x",
  3224. req->request_id, ctx->ctx_id, ctx->link_hdl);
  3225. if (ctx_isp->active_req_cnt <= 1)
  3226. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  3227. atomic_set(&ctx_isp->process_bubble, 1);
  3228. }
  3229. /*
  3230. * Always move the request to active list. Let buf done
  3231. * function handles the rest.
  3232. */
  3233. list_del_init(&req->list);
  3234. list_add_tail(&req->list, &ctx->active_req_list);
  3235. ctx_isp->active_req_cnt++;
  3236. CAM_DBG(CAM_ISP, "move request %lld to active list(cnt = %d) ctx %u, link: 0x%x",
  3237. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  3238. /*
  3239. * Handle the deferred buf done after moving
  3240. * the bubble req to active req list.
  3241. */
  3242. __cam_isp_ctx_handle_deferred_buf_done_in_bubble(
  3243. ctx_isp, req);
  3244. if (!req_isp->bubble_detected) {
  3245. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request,
  3246. list);
  3247. req_isp->bubble_detected = true;
  3248. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  3249. req_isp->cdm_reset_before_apply = false;
  3250. atomic_set(&ctx_isp->process_bubble, 1);
  3251. list_del_init(&req->list);
  3252. list_add_tail(&req->list, &ctx->active_req_list);
  3253. ctx_isp->active_req_cnt++;
  3254. }
  3255. if (!req_isp->bubble_report) {
  3256. if (req->request_id > ctx_isp->reported_req_id) {
  3257. request_id = req->request_id;
  3258. ctx_isp->reported_req_id = request_id;
  3259. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3260. CAM_REQ_MGR_SOF_EVENT_ERROR);
  3261. __cam_isp_ctx_update_event_record(ctx_isp,
  3262. CAM_ISP_CTX_EVENT_EPOCH, req, NULL);
  3263. } else {
  3264. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3265. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3266. __cam_isp_ctx_update_event_record(ctx_isp,
  3267. CAM_ISP_CTX_EVENT_EPOCH, NULL, NULL);
  3268. }
  3269. } else {
  3270. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3271. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3272. __cam_isp_ctx_update_event_record(ctx_isp,
  3273. CAM_ISP_CTX_EVENT_EPOCH, NULL, NULL);
  3274. }
  3275. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  3276. CAM_DBG(CAM_ISP, "next Substate[%s], ctx_idx: %u, link: 0x%x",
  3277. __cam_isp_ctx_substate_val_to_type(
  3278. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  3279. cam_req_mgr_debug_delay_detect();
  3280. trace_cam_delay_detect("ISP",
  3281. "bubble epoch_in_bubble_applied",
  3282. req->request_id, ctx->ctx_id, ctx->link_hdl,
  3283. ctx->session_hdl,
  3284. CAM_DEFAULT_VALUE);
  3285. end:
  3286. req = list_last_entry(&ctx->active_req_list, struct cam_ctx_request,
  3287. list);
  3288. if (req)
  3289. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3290. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, req->request_id);
  3291. return 0;
  3292. }
  3293. static int __cam_isp_ctx_buf_done_in_bubble_applied(
  3294. struct cam_isp_context *ctx_isp, void *evt_data)
  3295. {
  3296. int rc = 0;
  3297. struct cam_isp_hw_done_event_data *done =
  3298. (struct cam_isp_hw_done_event_data *) evt_data;
  3299. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 1);
  3300. return rc;
  3301. }
  3302. static void __cam_isp_get_notification_evt_params(
  3303. uint32_t hw_error, uint32_t *fence_evt_cause,
  3304. uint32_t *req_mgr_err_code, uint32_t *recovery_type)
  3305. {
  3306. uint32_t err_type, err_code = 0, recovery_type_temp;
  3307. err_type = CAM_SYNC_ISP_EVENT_UNKNOWN;
  3308. recovery_type_temp = CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3309. if (hw_error & CAM_ISP_HW_ERROR_OVERFLOW) {
  3310. err_code |= CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  3311. err_type = CAM_SYNC_ISP_EVENT_OVERFLOW;
  3312. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3313. }
  3314. if (hw_error & CAM_ISP_HW_ERROR_CSID_OUTPUT_FIFO_OVERFLOW) {
  3315. err_code |= CAM_REQ_MGR_CSID_FIFO_OVERFLOW_ERROR;
  3316. err_type = CAM_SYNC_ISP_EVENT_CSID_OUTPUT_FIFO_OVERFLOW;
  3317. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3318. }
  3319. if (hw_error & CAM_ISP_HW_ERROR_RECOVERY_OVERFLOW) {
  3320. err_code |= CAM_REQ_MGR_CSID_RECOVERY_OVERFLOW_ERROR;
  3321. err_type = CAM_SYNC_ISP_EVENT_RECOVERY_OVERFLOW;
  3322. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3323. }
  3324. if (hw_error & CAM_ISP_HW_ERROR_P2I_ERROR) {
  3325. err_code |= CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  3326. err_type = CAM_SYNC_ISP_EVENT_P2I_ERROR;
  3327. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3328. }
  3329. if (hw_error & CAM_ISP_HW_ERROR_VIOLATION) {
  3330. err_code |= CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  3331. err_type = CAM_SYNC_ISP_EVENT_VIOLATION;
  3332. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3333. }
  3334. if (hw_error & CAM_ISP_HW_ERROR_HWPD_VIOLATION) {
  3335. err_code |= CAM_REQ_MGR_ISP_ERR_HWPD_VIOLATION;
  3336. err_type = CAM_SYNC_ISP_EVENT_VIOLATION;
  3337. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3338. }
  3339. if (hw_error & CAM_ISP_HW_ERROR_BUSIF_OVERFLOW) {
  3340. err_code |= CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  3341. err_type = CAM_SYNC_ISP_EVENT_BUSIF_OVERFLOW;
  3342. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3343. }
  3344. if (hw_error & CAM_ISP_HW_ERROR_CSID_SENSOR_SWITCH_ERROR) {
  3345. err_code |= CAM_REQ_MGR_CSID_ERR_ON_SENSOR_SWITCHING;
  3346. err_type = CAM_SYNC_ISP_EVENT_CSID_SENSOR_SWITCH_ERROR;
  3347. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  3348. }
  3349. if (hw_error & CAM_ISP_HW_ERROR_CSID_LANE_FIFO_OVERFLOW) {
  3350. err_code |= CAM_REQ_MGR_CSID_LANE_FIFO_OVERFLOW_ERROR;
  3351. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  3352. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  3353. }
  3354. if (hw_error & CAM_ISP_HW_ERROR_CSID_PKT_HDR_CORRUPTED) {
  3355. err_code |= CAM_REQ_MGR_CSID_RX_PKT_HDR_CORRUPTION;
  3356. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  3357. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  3358. }
  3359. if (hw_error & CAM_ISP_HW_ERROR_CSID_MISSING_PKT_HDR_DATA) {
  3360. err_code |= CAM_REQ_MGR_CSID_MISSING_PKT_HDR_DATA;
  3361. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  3362. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  3363. }
  3364. if (hw_error & CAM_ISP_HW_ERROR_CSID_UNBOUNDED_FRAME) {
  3365. err_code |= CAM_REQ_MGR_CSID_UNBOUNDED_FRAME;
  3366. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  3367. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  3368. }
  3369. if (hw_error & CAM_ISP_HW_ERROR_CSID_FRAME_SIZE) {
  3370. err_code |= CAM_REQ_MGR_CSID_PIXEL_COUNT_MISMATCH;
  3371. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  3372. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3373. }
  3374. if (hw_error & CAM_ISP_HW_ERROR_CSID_MISSING_EOT) {
  3375. err_code |= CAM_REQ_MGR_CSID_MISSING_EOT;
  3376. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  3377. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  3378. }
  3379. if (hw_error & CAM_ISP_HW_ERROR_CSID_PKT_PAYLOAD_CORRUPTED) {
  3380. err_code |= CAM_REQ_MGR_CSID_RX_PKT_PAYLOAD_CORRUPTION;
  3381. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  3382. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  3383. }
  3384. if (recovery_type_temp == (CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY |
  3385. CAM_REQ_MGR_ERROR_TYPE_RECOVERY))
  3386. recovery_type_temp = CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  3387. if (!err_code)
  3388. err_code = CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  3389. *req_mgr_err_code = err_code;
  3390. *fence_evt_cause = err_type;
  3391. *recovery_type = recovery_type_temp;
  3392. }
  3393. static bool __cam_isp_ctx_request_can_reapply(
  3394. struct cam_isp_ctx_req *req_isp)
  3395. {
  3396. int i;
  3397. for (i = 0; i < req_isp->num_fence_map_out; i++)
  3398. if (req_isp->fence_map_out[i].sync_id == -1)
  3399. return false;
  3400. return true;
  3401. }
  3402. static int __cam_isp_ctx_validate_for_req_reapply_util(
  3403. struct cam_isp_context *ctx_isp)
  3404. {
  3405. int rc = 0;
  3406. struct cam_ctx_request *req_temp;
  3407. struct cam_ctx_request *req = NULL;
  3408. struct cam_isp_ctx_req *req_isp = NULL;
  3409. struct cam_context *ctx = ctx_isp->base;
  3410. if (in_task())
  3411. spin_lock_bh(&ctx->lock);
  3412. /* Check for req in active/wait lists */
  3413. if (list_empty(&ctx->active_req_list)) {
  3414. CAM_DBG(CAM_ISP,
  3415. "Active request list empty for ctx: %u on link: 0x%x",
  3416. ctx->ctx_id, ctx->link_hdl);
  3417. if (list_empty(&ctx->wait_req_list)) {
  3418. CAM_WARN(CAM_ISP,
  3419. "No active/wait req for ctx: %u on link: 0x%x start from pending",
  3420. ctx->ctx_id, ctx->link_hdl);
  3421. rc = 0;
  3422. goto end;
  3423. }
  3424. }
  3425. /* Validate if all fences for active requests are not signaled */
  3426. if (!list_empty(&ctx->active_req_list)) {
  3427. list_for_each_entry_safe_reverse(req, req_temp,
  3428. &ctx->active_req_list, list) {
  3429. /*
  3430. * If some fences of the active request are already
  3431. * signaled, we should not do recovery for the buffer
  3432. * and timestamp consistency.
  3433. */
  3434. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  3435. if (!__cam_isp_ctx_request_can_reapply(req_isp)) {
  3436. CAM_WARN(CAM_ISP,
  3437. "Req: %llu in ctx:%u on link: 0x%x fence has partially signaled, cannot do recovery",
  3438. req->request_id, ctx->ctx_id, ctx->link_hdl);
  3439. rc = -EINVAL;
  3440. goto end;
  3441. }
  3442. }
  3443. }
  3444. /* Move active requests to pending list */
  3445. if (!list_empty(&ctx->active_req_list)) {
  3446. list_for_each_entry_safe_reverse(req, req_temp,
  3447. &ctx->active_req_list, list) {
  3448. list_del_init(&req->list);
  3449. __cam_isp_ctx_enqueue_request_in_order(ctx, req, false);
  3450. ctx_isp->active_req_cnt--;
  3451. CAM_DBG(CAM_ISP, "ctx:%u link:0x%x move active req %llu to pending",
  3452. ctx->ctx_id, ctx->link_hdl, req->request_id);
  3453. }
  3454. }
  3455. /* Move wait requests to pending list */
  3456. if (!list_empty(&ctx->wait_req_list)) {
  3457. list_for_each_entry_safe_reverse(req, req_temp, &ctx->wait_req_list, list) {
  3458. list_del_init(&req->list);
  3459. __cam_isp_ctx_enqueue_request_in_order(ctx, req, false);
  3460. CAM_DBG(CAM_ISP, "ctx:%u link:0x%x move wait req %llu to pending",
  3461. ctx->ctx_id, ctx->link_hdl, req->request_id);
  3462. }
  3463. }
  3464. end:
  3465. if (in_task())
  3466. spin_unlock_bh(&ctx->lock);
  3467. return rc;
  3468. }
  3469. static int __cam_isp_ctx_handle_recovery_req_util(
  3470. struct cam_isp_context *ctx_isp)
  3471. {
  3472. int rc = 0;
  3473. struct cam_context *ctx = ctx_isp->base;
  3474. struct cam_ctx_request *req_to_reapply = NULL;
  3475. if (list_empty(&ctx->pending_req_list)) {
  3476. CAM_WARN(CAM_ISP,
  3477. "No pending request to recover from on ctx: %u", ctx->ctx_id);
  3478. return -EINVAL;
  3479. }
  3480. req_to_reapply = list_first_entry(&ctx->pending_req_list,
  3481. struct cam_ctx_request, list);
  3482. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  3483. ctx_isp->recovery_req_id = req_to_reapply->request_id;
  3484. atomic_set(&ctx_isp->internal_recovery_set, 1);
  3485. CAM_INFO(CAM_ISP, "Notify CRM to reapply req:%llu for ctx:%u link:0x%x",
  3486. req_to_reapply->request_id, ctx->ctx_id, ctx->link_hdl);
  3487. rc = __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF,
  3488. CRM_KMD_WARN_INTERNAL_RECOVERY, req_to_reapply->request_id,
  3489. ctx_isp);
  3490. if (rc) {
  3491. /* Unable to notify CRM to do reapply back to normal */
  3492. CAM_WARN(CAM_ISP,
  3493. "ctx:%u link:0x%x unable to notify CRM for req %llu",
  3494. ctx->ctx_id, ctx->link_hdl, ctx_isp->recovery_req_id);
  3495. ctx_isp->recovery_req_id = 0;
  3496. atomic_set(&ctx_isp->internal_recovery_set, 0);
  3497. }
  3498. return rc;
  3499. }
  3500. static int __cam_isp_ctx_trigger_error_req_reapply(
  3501. uint32_t err_type, struct cam_isp_context *ctx_isp)
  3502. {
  3503. int rc = 0;
  3504. struct cam_context *ctx = ctx_isp->base;
  3505. if ((err_type & CAM_ISP_HW_ERROR_RECOVERY_OVERFLOW) &&
  3506. (isp_ctx_debug.disable_internal_recovery_mask &
  3507. CAM_ISP_CTX_DISABLE_RECOVERY_BUS_OVERFLOW))
  3508. return -EINVAL;
  3509. /*
  3510. * For errors that can be recoverable within kmd, we
  3511. * try to do internal hw stop, restart and notify CRM
  3512. * to do reapply with the help of bubble control flow.
  3513. */
  3514. rc = __cam_isp_ctx_validate_for_req_reapply_util(ctx_isp);
  3515. if (rc)
  3516. goto end;
  3517. rc = __cam_isp_ctx_handle_recovery_req_util(ctx_isp);
  3518. if (rc)
  3519. goto end;
  3520. CAM_DBG(CAM_ISP, "Triggered internal recovery for req:%llu ctx:%u on link 0x%x",
  3521. ctx_isp->recovery_req_id, ctx->ctx_id, ctx->link_hdl);
  3522. end:
  3523. return rc;
  3524. }
  3525. static int __cam_isp_ctx_handle_error(struct cam_isp_context *ctx_isp,
  3526. void *evt_data)
  3527. {
  3528. int rc = 0;
  3529. enum cam_req_mgr_device_error error;
  3530. uint32_t i = 0;
  3531. bool found = 0;
  3532. struct cam_ctx_request *req = NULL;
  3533. struct cam_ctx_request *req_to_report = NULL;
  3534. struct cam_ctx_request *req_to_dump = NULL;
  3535. struct cam_ctx_request *req_temp;
  3536. struct cam_isp_ctx_req *req_isp = NULL;
  3537. struct cam_isp_ctx_req *req_isp_to_report = NULL;
  3538. uint64_t error_request_id;
  3539. struct cam_hw_fence_map_entry *fence_map_out = NULL;
  3540. uint32_t recovery_type, fence_evt_cause;
  3541. uint32_t req_mgr_err_code;
  3542. struct cam_context *ctx = ctx_isp->base;
  3543. struct cam_isp_hw_error_event_data *error_event_data =
  3544. (struct cam_isp_hw_error_event_data *)evt_data;
  3545. CAM_DBG(CAM_ISP, "Enter HW error_type = %d, ctx:%u on link 0x%x",
  3546. error_event_data->error_type, ctx->ctx_id, ctx->link_hdl);
  3547. if (error_event_data->try_internal_recovery) {
  3548. rc = __cam_isp_ctx_trigger_error_req_reapply(error_event_data->error_type, ctx_isp);
  3549. if (!rc)
  3550. goto exit;
  3551. }
  3552. if (!ctx_isp->offline_context)
  3553. __cam_isp_ctx_pause_crm_timer(ctx);
  3554. __cam_isp_ctx_dump_frame_timing_record(ctx_isp);
  3555. __cam_isp_ctx_trigger_reg_dump(CAM_HW_MGR_CMD_REG_DUMP_ON_ERROR, ctx);
  3556. __cam_isp_get_notification_evt_params(error_event_data->error_type,
  3557. &fence_evt_cause, &req_mgr_err_code, &recovery_type);
  3558. /*
  3559. * The error is likely caused by first request on the active list.
  3560. * If active list is empty check wait list (maybe error hit as soon
  3561. * as RUP and we handle error before RUP.
  3562. */
  3563. if (list_empty(&ctx->active_req_list)) {
  3564. CAM_DBG(CAM_ISP,
  3565. "handling error with no active request, ctx:%u on link 0x%x",
  3566. ctx->ctx_id, ctx->link_hdl);
  3567. if (list_empty(&ctx->wait_req_list)) {
  3568. CAM_ERR_RATE_LIMIT(CAM_ISP,
  3569. "Error with no active/wait request, ctx:%u on link 0x%x",
  3570. ctx->ctx_id, ctx->link_hdl);
  3571. goto end;
  3572. } else {
  3573. req_to_dump = list_first_entry(&ctx->wait_req_list,
  3574. struct cam_ctx_request, list);
  3575. }
  3576. } else {
  3577. req_to_dump = list_first_entry(&ctx->active_req_list,
  3578. struct cam_ctx_request, list);
  3579. }
  3580. req_isp = (struct cam_isp_ctx_req *) req_to_dump->req_priv;
  3581. if (error_event_data->enable_req_dump)
  3582. rc = cam_isp_ctx_dump_req(req_isp, 0, 0, NULL, false);
  3583. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3584. CAM_ISP_STATE_CHANGE_TRIGGER_ERROR, req_to_dump->request_id);
  3585. list_for_each_entry_safe(req, req_temp,
  3586. &ctx->active_req_list, list) {
  3587. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  3588. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3589. if (!req_isp->bubble_report) {
  3590. CAM_ERR(CAM_ISP, "signalled error for req %llu, ctx:%u on link 0x%x",
  3591. req->request_id, ctx->ctx_id, ctx->link_hdl);
  3592. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  3593. fence_map_out =
  3594. &req_isp->fence_map_out[i];
  3595. if (req_isp->fence_map_out[i].sync_id != -1) {
  3596. CAM_DBG(CAM_ISP,
  3597. "req %llu, Sync fd 0x%x ctx %u, link 0x%x",
  3598. req->request_id,
  3599. req_isp->fence_map_out[i].sync_id,
  3600. ctx->ctx_id, ctx->link_hdl);
  3601. rc = cam_sync_signal(
  3602. fence_map_out->sync_id,
  3603. CAM_SYNC_STATE_SIGNALED_ERROR,
  3604. fence_evt_cause);
  3605. fence_map_out->sync_id = -1;
  3606. }
  3607. }
  3608. list_del_init(&req->list);
  3609. list_add_tail(&req->list, &ctx->free_req_list);
  3610. ctx_isp->active_req_cnt--;
  3611. } else {
  3612. found = 1;
  3613. break;
  3614. }
  3615. }
  3616. if (found)
  3617. goto move_to_pending;
  3618. list_for_each_entry_safe(req, req_temp,
  3619. &ctx->wait_req_list, list) {
  3620. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  3621. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3622. if (!req_isp->bubble_report) {
  3623. CAM_ERR(CAM_ISP, "signalled error for req %llu, ctx %u, link 0x%x",
  3624. req->request_id, ctx->ctx_id, ctx->link_hdl);
  3625. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  3626. fence_map_out =
  3627. &req_isp->fence_map_out[i];
  3628. if (req_isp->fence_map_out[i].sync_id != -1) {
  3629. CAM_DBG(CAM_ISP,
  3630. "req %llu, Sync fd 0x%x ctx %u link 0x%x",
  3631. req->request_id,
  3632. req_isp->fence_map_out[i].sync_id,
  3633. ctx->ctx_id, ctx->link_hdl);
  3634. rc = cam_sync_signal(
  3635. fence_map_out->sync_id,
  3636. CAM_SYNC_STATE_SIGNALED_ERROR,
  3637. fence_evt_cause);
  3638. fence_map_out->sync_id = -1;
  3639. }
  3640. }
  3641. list_del_init(&req->list);
  3642. list_add_tail(&req->list, &ctx->free_req_list);
  3643. } else {
  3644. found = 1;
  3645. break;
  3646. }
  3647. }
  3648. move_to_pending:
  3649. /*
  3650. * If bubble recovery is enabled on any request we need to move that
  3651. * request and all the subsequent requests to the pending list.
  3652. * Note:
  3653. * We need to traverse the active list in reverse order and add
  3654. * to head of pending list.
  3655. * e.g. pending current state: 10, 11 | active current state: 8, 9
  3656. * intermittent for loop iteration- pending: 9, 10, 11 | active: 8
  3657. * final state - pending: 8, 9, 10, 11 | active: NULL
  3658. */
  3659. if (found) {
  3660. list_for_each_entry_safe_reverse(req, req_temp,
  3661. &ctx->active_req_list, list) {
  3662. list_del_init(&req->list);
  3663. list_add(&req->list, &ctx->pending_req_list);
  3664. ctx_isp->active_req_cnt--;
  3665. }
  3666. list_for_each_entry_safe_reverse(req, req_temp,
  3667. &ctx->wait_req_list, list) {
  3668. list_del_init(&req->list);
  3669. list_add(&req->list, &ctx->pending_req_list);
  3670. }
  3671. }
  3672. end:
  3673. do {
  3674. if (list_empty(&ctx->pending_req_list)) {
  3675. error_request_id = ctx_isp->last_applied_req_id;
  3676. break;
  3677. }
  3678. req = list_first_entry(&ctx->pending_req_list,
  3679. struct cam_ctx_request, list);
  3680. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  3681. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3682. error_request_id = ctx_isp->last_applied_req_id;
  3683. if (req_isp->bubble_report) {
  3684. req_to_report = req;
  3685. req_isp_to_report = req_to_report->req_priv;
  3686. break;
  3687. }
  3688. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  3689. if (req_isp->fence_map_out[i].sync_id != -1)
  3690. rc = cam_sync_signal(
  3691. req_isp->fence_map_out[i].sync_id,
  3692. CAM_SYNC_STATE_SIGNALED_ERROR,
  3693. fence_evt_cause);
  3694. req_isp->fence_map_out[i].sync_id = -1;
  3695. }
  3696. list_del_init(&req->list);
  3697. list_add_tail(&req->list, &ctx->free_req_list);
  3698. } while (req->request_id < ctx_isp->last_applied_req_id);
  3699. if (ctx_isp->offline_context)
  3700. goto exit;
  3701. error = CRM_KMD_ERR_FATAL;
  3702. if (req_isp_to_report && req_isp_to_report->bubble_report)
  3703. if (error_event_data->recovery_enabled)
  3704. error = CRM_KMD_ERR_BUBBLE;
  3705. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, error,
  3706. error_request_id, ctx_isp);
  3707. /*
  3708. * Need to send error occurred in KMD
  3709. * This will help UMD to take necessary action
  3710. * and to dump relevant info
  3711. */
  3712. if (error == CRM_KMD_ERR_FATAL)
  3713. __cam_isp_ctx_notify_v4l2_error_event(recovery_type,
  3714. req_mgr_err_code, error_request_id, ctx);
  3715. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HW_ERROR;
  3716. CAM_DBG(CAM_ISP, "Handling error done on ctx: %u, link: 0x%x", ctx->ctx_id, ctx->link_hdl);
  3717. exit:
  3718. return rc;
  3719. }
  3720. static int __cam_isp_ctx_fs2_sof_in_sof_state(
  3721. struct cam_isp_context *ctx_isp, void *evt_data)
  3722. {
  3723. int rc = 0;
  3724. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3725. struct cam_ctx_request *req;
  3726. struct cam_context *ctx = ctx_isp->base;
  3727. uint64_t request_id = 0;
  3728. if (!evt_data) {
  3729. CAM_ERR(CAM_ISP, "in valid sof event data, ctx: %u, link: 0x%x",
  3730. ctx->ctx_id, ctx->link_hdl);
  3731. return -EINVAL;
  3732. }
  3733. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  3734. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx: %u, link: 0x%x",
  3735. ctx_isp->frame_id, ctx_isp->sof_timestamp_val, ctx->ctx_id, ctx->link_hdl);
  3736. if (!(list_empty(&ctx->wait_req_list)))
  3737. goto end;
  3738. if (ctx_isp->active_req_cnt <= 2) {
  3739. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  3740. list_for_each_entry(req, &ctx->active_req_list, list) {
  3741. if (req->request_id > ctx_isp->reported_req_id) {
  3742. request_id = req->request_id;
  3743. ctx_isp->reported_req_id = request_id;
  3744. break;
  3745. }
  3746. }
  3747. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3748. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3749. }
  3750. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3751. CAM_ISP_STATE_CHANGE_TRIGGER_SOF, request_id);
  3752. end:
  3753. return rc;
  3754. }
  3755. static int __cam_isp_ctx_fs2_buf_done(struct cam_isp_context *ctx_isp,
  3756. void *evt_data)
  3757. {
  3758. int rc = 0;
  3759. struct cam_isp_hw_done_event_data *done =
  3760. (struct cam_isp_hw_done_event_data *) evt_data;
  3761. struct cam_context *ctx = ctx_isp->base;
  3762. int prev_active_req_cnt = 0;
  3763. int curr_req_id = 0;
  3764. struct cam_ctx_request *req;
  3765. prev_active_req_cnt = ctx_isp->active_req_cnt;
  3766. req = list_first_entry(&ctx->active_req_list,
  3767. struct cam_ctx_request, list);
  3768. if (req)
  3769. curr_req_id = req->request_id;
  3770. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  3771. if (prev_active_req_cnt == ctx_isp->active_req_cnt + 1) {
  3772. if (list_empty(&ctx->wait_req_list) &&
  3773. list_empty(&ctx->active_req_list)) {
  3774. CAM_DBG(CAM_ISP, "No request, move to SOF, ctx_idx: %u, link: 0x%x",
  3775. ctx->ctx_id, ctx->link_hdl);
  3776. ctx_isp->substate_activated =
  3777. CAM_ISP_CTX_ACTIVATED_SOF;
  3778. if (ctx_isp->reported_req_id < curr_req_id) {
  3779. ctx_isp->reported_req_id = curr_req_id;
  3780. __cam_isp_ctx_send_sof_timestamp(ctx_isp,
  3781. curr_req_id,
  3782. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3783. }
  3784. }
  3785. }
  3786. return rc;
  3787. }
  3788. static int __cam_isp_ctx_fs2_buf_done_in_epoch(struct cam_isp_context *ctx_isp,
  3789. void *evt_data)
  3790. {
  3791. int rc = 0;
  3792. rc = __cam_isp_ctx_fs2_buf_done(ctx_isp, evt_data);
  3793. return rc;
  3794. }
  3795. static int __cam_isp_ctx_fs2_buf_done_in_applied(
  3796. struct cam_isp_context *ctx_isp,
  3797. void *evt_data)
  3798. {
  3799. int rc = 0;
  3800. rc = __cam_isp_ctx_fs2_buf_done(ctx_isp, evt_data);
  3801. return rc;
  3802. }
  3803. static int __cam_isp_ctx_fs2_reg_upd_in_sof(struct cam_isp_context *ctx_isp,
  3804. void *evt_data)
  3805. {
  3806. int rc = 0;
  3807. struct cam_ctx_request *req = NULL;
  3808. struct cam_isp_ctx_req *req_isp;
  3809. struct cam_context *ctx = ctx_isp->base;
  3810. if (ctx->state != CAM_CTX_ACTIVATED && ctx_isp->frame_id > 1) {
  3811. CAM_DBG(CAM_ISP, "invalid RUP");
  3812. goto end;
  3813. }
  3814. /*
  3815. * This is for the first update. The initial setting will
  3816. * cause the reg_upd in the first frame.
  3817. */
  3818. if (!list_empty(&ctx->wait_req_list)) {
  3819. req = list_first_entry(&ctx->wait_req_list,
  3820. struct cam_ctx_request, list);
  3821. list_del_init(&req->list);
  3822. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  3823. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3824. if (req_isp->num_fence_map_out == req_isp->num_acked)
  3825. list_add_tail(&req->list, &ctx->free_req_list);
  3826. else
  3827. CAM_ERR(CAM_ISP,
  3828. "receive rup in unexpected state, ctx_idx: %u, link: 0x%x",
  3829. ctx->ctx_id, ctx->link_hdl);
  3830. }
  3831. if (req != NULL) {
  3832. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3833. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  3834. req->request_id);
  3835. }
  3836. end:
  3837. return rc;
  3838. }
  3839. static int __cam_isp_ctx_fs2_reg_upd_in_applied_state(
  3840. struct cam_isp_context *ctx_isp, void *evt_data)
  3841. {
  3842. int rc = 0;
  3843. struct cam_ctx_request *req = NULL;
  3844. struct cam_context *ctx = ctx_isp->base;
  3845. struct cam_isp_ctx_req *req_isp;
  3846. uint64_t request_id = 0;
  3847. if (list_empty(&ctx->wait_req_list)) {
  3848. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request, ctx_idx: %u, link: 0x%x",
  3849. ctx->ctx_id, ctx->link_hdl);
  3850. goto end;
  3851. }
  3852. req = list_first_entry(&ctx->wait_req_list,
  3853. struct cam_ctx_request, list);
  3854. list_del_init(&req->list);
  3855. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3856. if (req_isp->num_fence_map_out != 0) {
  3857. list_add_tail(&req->list, &ctx->active_req_list);
  3858. ctx_isp->active_req_cnt++;
  3859. CAM_DBG(CAM_REQ, "move request %lld to active list(cnt = %d), ctx:%u,link:0x%x",
  3860. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  3861. } else {
  3862. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  3863. /* no io config, so the request is completed. */
  3864. list_add_tail(&req->list, &ctx->free_req_list);
  3865. }
  3866. /*
  3867. * This function only called directly from applied and bubble applied
  3868. * state so change substate here.
  3869. */
  3870. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  3871. if (req_isp->num_fence_map_out != 1)
  3872. goto end;
  3873. if (ctx_isp->active_req_cnt <= 2) {
  3874. list_for_each_entry(req, &ctx->active_req_list, list) {
  3875. if (req->request_id > ctx_isp->reported_req_id) {
  3876. request_id = req->request_id;
  3877. ctx_isp->reported_req_id = request_id;
  3878. break;
  3879. }
  3880. }
  3881. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3882. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3883. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  3884. }
  3885. CAM_DBG(CAM_ISP, "next Substate[%s], ctx_idx: %u, link: 0x%x",
  3886. __cam_isp_ctx_substate_val_to_type(ctx_isp->substate_activated),
  3887. ctx->ctx_id, ctx->link_hdl);
  3888. end:
  3889. if (req != NULL && !rc) {
  3890. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3891. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  3892. req->request_id);
  3893. }
  3894. return rc;
  3895. }
  3896. static void __cam_isp_ctx_notify_aeb_error_for_sec_event(
  3897. struct cam_isp_context *ctx_isp)
  3898. {
  3899. struct cam_context *ctx = ctx_isp->base;
  3900. if ((++ctx_isp->aeb_error_cnt) <= CAM_ISP_CONTEXT_AEB_ERROR_CNT_MAX) {
  3901. CAM_WARN(CAM_ISP,
  3902. "AEB slave RDI's current request's SOF seen after next req is applied for ctx: %u on link: 0x%x last_applied_req: %llu err_cnt: %u",
  3903. ctx->ctx_id, ctx->link_hdl, ctx_isp->last_applied_req_id, ctx_isp->aeb_error_cnt);
  3904. return;
  3905. }
  3906. CAM_ERR(CAM_ISP,
  3907. "Fatal - AEB slave RDI's current request's SOF seen after next req is applied, EPOCH height need to be re-configured for ctx: %u on link: 0x%x err_cnt: %u",
  3908. ctx->ctx_id, ctx->link_hdl, ctx_isp->aeb_error_cnt);
  3909. /* Pause CRM timer */
  3910. if (!ctx_isp->offline_context)
  3911. __cam_isp_ctx_pause_crm_timer(ctx);
  3912. /* Trigger reg dump */
  3913. __cam_isp_ctx_trigger_reg_dump(CAM_HW_MGR_CMD_REG_DUMP_ON_ERROR, ctx);
  3914. /* Notify CRM on fatal error */
  3915. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_FATAL,
  3916. ctx_isp->last_applied_req_id, ctx_isp);
  3917. /* Notify userland on error */
  3918. __cam_isp_ctx_notify_v4l2_error_event(CAM_REQ_MGR_ERROR_TYPE_RECOVERY,
  3919. CAM_REQ_MGR_CSID_ERR_ON_SENSOR_SWITCHING, ctx_isp->last_applied_req_id, ctx);
  3920. /* Change state to HALT, stop further processing of HW events */
  3921. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  3922. /* Dump AEB debug info */
  3923. __cam_isp_ctx_dump_frame_timing_record(ctx_isp);
  3924. }
  3925. static int __cam_isp_ctx_trigger_internal_recovery(
  3926. bool sync_frame_drop, struct cam_isp_context *ctx_isp)
  3927. {
  3928. int rc = 0;
  3929. bool do_recovery = true;
  3930. struct cam_context *ctx = ctx_isp->base;
  3931. struct cam_ctx_request *req = NULL;
  3932. struct cam_isp_ctx_req *req_isp = NULL;
  3933. if (list_empty(&ctx->wait_req_list)) {
  3934. /*
  3935. * If the wait list is empty, and we encounter a "silent" frame drop
  3936. * then the settings applied on the previous frame, did not reflect
  3937. * at the next frame boundary, it's expected to latch a frame after.
  3938. * No need to recover. If it's an out of sync drop use pending req
  3939. */
  3940. if (sync_frame_drop && !list_empty(&ctx->pending_req_list))
  3941. req = list_first_entry(&ctx->pending_req_list,
  3942. struct cam_ctx_request, list);
  3943. else
  3944. do_recovery = false;
  3945. }
  3946. /* If both wait and pending list have no request to recover on */
  3947. if (!do_recovery) {
  3948. CAM_WARN(CAM_ISP,
  3949. "No request to perform recovery - ctx: %u on link: 0x%x last_applied: %lld last_buf_done: %lld",
  3950. ctx->ctx_id, ctx->link_hdl, ctx_isp->last_applied_req_id,
  3951. ctx_isp->req_info.last_bufdone_req_id);
  3952. goto end;
  3953. }
  3954. if (!req) {
  3955. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request, list);
  3956. if (req->request_id != ctx_isp->last_applied_req_id)
  3957. CAM_WARN(CAM_ISP,
  3958. "Top of wait list req: %llu does not match with last applied: %llu in ctx: %u on link: 0x%x",
  3959. req->request_id, ctx_isp->last_applied_req_id,
  3960. ctx->ctx_id, ctx->link_hdl);
  3961. }
  3962. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  3963. /*
  3964. * Treat this as bubble, after recovery re-start from appropriate sub-state
  3965. * This will block servicing any further apply calls from CRM
  3966. */
  3967. atomic_set(&ctx_isp->internal_recovery_set, 1);
  3968. atomic_set(&ctx_isp->process_bubble, 1);
  3969. ctx_isp->recovery_req_id = req->request_id;
  3970. /* Wait for active request's to finish before issuing recovery */
  3971. if (ctx_isp->active_req_cnt) {
  3972. req_isp->bubble_detected = true;
  3973. CAM_WARN(CAM_ISP,
  3974. "Active req cnt: %u wait for all buf dones before kicking in recovery on req: %lld ctx: %u on link: 0x%x",
  3975. ctx_isp->active_req_cnt, ctx_isp->recovery_req_id,
  3976. ctx->ctx_id, ctx->link_hdl);
  3977. } else {
  3978. rc = __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF,
  3979. CRM_KMD_WARN_INTERNAL_RECOVERY, ctx_isp->recovery_req_id, ctx_isp);
  3980. if (rc) {
  3981. /* Unable to do bubble recovery reset back to normal */
  3982. CAM_WARN(CAM_ISP,
  3983. "Unable to perform internal recovery [bubble reporting failed] for req: %llu in ctx: %u on link: 0x%x",
  3984. ctx_isp->recovery_req_id, ctx->ctx_id, ctx->link_hdl);
  3985. __cam_isp_context_reset_internal_recovery_params(ctx_isp);
  3986. goto end;
  3987. }
  3988. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  3989. list_del_init(&req->list);
  3990. list_add(&req->list, &ctx->pending_req_list);
  3991. }
  3992. end:
  3993. return rc;
  3994. }
  3995. static int __cam_isp_ctx_handle_secondary_events(
  3996. struct cam_isp_context *ctx_isp, void *evt_data)
  3997. {
  3998. int rc = 0;
  3999. bool recover = false, sync_frame_drop = false;
  4000. struct cam_context *ctx = ctx_isp->base;
  4001. struct cam_isp_hw_secondary_event_data *sec_evt_data =
  4002. (struct cam_isp_hw_secondary_event_data *)evt_data;
  4003. /* Current scheme to handle only for custom AEB */
  4004. if (!ctx_isp->aeb_enabled) {
  4005. CAM_WARN(CAM_ISP,
  4006. "Recovery not supported for non-AEB ctx: %u on link: 0x%x reject sec evt: %u",
  4007. ctx->ctx_id, ctx->link_hdl, sec_evt_data->evt_type);
  4008. goto end;
  4009. }
  4010. if (atomic_read(&ctx_isp->internal_recovery_set)) {
  4011. CAM_WARN(CAM_ISP,
  4012. "Internal recovery in progress in ctx: %u on link: 0x%x reject sec evt: %u",
  4013. ctx->ctx_id, ctx->link_hdl, sec_evt_data->evt_type);
  4014. goto end;
  4015. }
  4016. /*
  4017. * In case of custom AEB ensure first exposure frame has
  4018. * not moved forward with its settings without second/third
  4019. * expoure frame coming in. Also track for bubble, in case of system
  4020. * delays it's possible for the IFE settings to be not written to
  4021. * HW on a given frame. If these scenarios occurs flag as error,
  4022. * and recover.
  4023. */
  4024. switch (sec_evt_data->evt_type) {
  4025. case CAM_ISP_HW_SEC_EVENT_SOF:
  4026. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  4027. CAM_ISP_STATE_CHANGE_TRIGGER_SEC_EVT_SOF,
  4028. ctx_isp->last_applied_req_id);
  4029. __cam_isp_ctx_update_frame_timing_record(CAM_ISP_HW_SECONDARY_EVENT, ctx_isp);
  4030. /* Slave RDI's frame starting post IFE EPOCH - Fatal */
  4031. if ((ctx_isp->substate_activated ==
  4032. CAM_ISP_CTX_ACTIVATED_APPLIED) ||
  4033. (ctx_isp->substate_activated ==
  4034. CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED))
  4035. __cam_isp_ctx_notify_aeb_error_for_sec_event(ctx_isp);
  4036. else
  4037. /* Reset error count */
  4038. ctx_isp->aeb_error_cnt = 0;
  4039. break;
  4040. case CAM_ISP_HW_SEC_EVENT_EPOCH:
  4041. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  4042. CAM_ISP_STATE_CHANGE_TRIGGER_SEC_EVT_EPOCH,
  4043. ctx_isp->last_applied_req_id);
  4044. ctx_isp->out_of_sync_cnt = 0;
  4045. /*
  4046. * Master RDI frame dropped in CSID, due to programming delay no RUP/AUP
  4047. * On such occasions use CSID CAMIF EPOCH for bubble detection, flag
  4048. * on detection and perform necessary bubble recovery
  4049. */
  4050. if ((ctx_isp->substate_activated ==
  4051. CAM_ISP_CTX_ACTIVATED_APPLIED) ||
  4052. (ctx_isp->substate_activated ==
  4053. CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED)) {
  4054. recover = true;
  4055. CAM_WARN(CAM_ISP,
  4056. "Programming delay input frame dropped ctx: %u on link: 0x%x last_applied_req: %llu, kicking in internal recovery....",
  4057. ctx->ctx_id, ctx->link_hdl, ctx_isp->last_applied_req_id);
  4058. }
  4059. break;
  4060. case CAM_ISP_HW_SEC_EVENT_OUT_OF_SYNC_FRAME_DROP:
  4061. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  4062. CAM_ISP_STATE_CHANGE_TRIGGER_FRAME_DROP,
  4063. ctx_isp->last_applied_req_id);
  4064. /* Avoid recovery loop if frame is dropped at stream on */
  4065. if (!ctx_isp->frame_id) {
  4066. CAM_ERR(CAM_ISP,
  4067. "Sensor sync [vc mismatch] frame dropped at stream on ctx: %u link: 0x%x frame_id: %u last_applied_req: %lld",
  4068. ctx->ctx_id, ctx->link_hdl,
  4069. ctx_isp->frame_id, ctx_isp->last_applied_req_id);
  4070. rc = -EPERM;
  4071. break;
  4072. }
  4073. if (!(ctx_isp->out_of_sync_cnt++) &&
  4074. (ctx_isp->recovery_req_id == ctx_isp->last_applied_req_id)) {
  4075. CAM_WARN(CAM_ISP,
  4076. "Sensor sync [vc mismatch] frame dropped ctx: %u on link: 0x%x last_applied_req: %llu last_recovered_req: %llu out_of_sync_cnt: %u, recovery maybe in progress...",
  4077. ctx->ctx_id, ctx->link_hdl, ctx_isp->last_applied_req_id,
  4078. ctx_isp->recovery_req_id, ctx_isp->out_of_sync_cnt);
  4079. break;
  4080. }
  4081. recover = true;
  4082. sync_frame_drop = true;
  4083. ctx_isp->out_of_sync_cnt = 0;
  4084. CAM_WARN(CAM_ISP,
  4085. "Sensor sync [vc mismatch] frame dropped ctx: %u on link: 0x%x last_applied_req: %llu last_recovered_req: %llu out_of_sync_cnt: %u, kicking in internal recovery....",
  4086. ctx->ctx_id, ctx->link_hdl, ctx_isp->last_applied_req_id,
  4087. ctx_isp->recovery_req_id, ctx_isp->out_of_sync_cnt);
  4088. break;
  4089. default:
  4090. break;
  4091. }
  4092. if (recover &&
  4093. !(isp_ctx_debug.disable_internal_recovery_mask & CAM_ISP_CTX_DISABLE_RECOVERY_AEB))
  4094. rc = __cam_isp_ctx_trigger_internal_recovery(sync_frame_drop, ctx_isp);
  4095. end:
  4096. return rc;
  4097. }
  4098. static struct cam_isp_ctx_irq_ops
  4099. cam_isp_ctx_activated_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  4100. /* SOF */
  4101. {
  4102. .irq_ops = {
  4103. __cam_isp_ctx_handle_error,
  4104. __cam_isp_ctx_sof_in_activated_state,
  4105. __cam_isp_ctx_reg_upd_in_sof,
  4106. __cam_isp_ctx_notify_sof_in_activated_state,
  4107. __cam_isp_ctx_notify_eof_in_activated_state,
  4108. __cam_isp_ctx_buf_done_in_sof,
  4109. __cam_isp_ctx_handle_secondary_events,
  4110. },
  4111. },
  4112. /* APPLIED */
  4113. {
  4114. .irq_ops = {
  4115. __cam_isp_ctx_handle_error,
  4116. __cam_isp_ctx_sof_in_activated_state,
  4117. __cam_isp_ctx_reg_upd_in_applied_state,
  4118. __cam_isp_ctx_epoch_in_applied,
  4119. __cam_isp_ctx_notify_eof_in_activated_state,
  4120. __cam_isp_ctx_buf_done_in_applied,
  4121. __cam_isp_ctx_handle_secondary_events,
  4122. },
  4123. },
  4124. /* EPOCH */
  4125. {
  4126. .irq_ops = {
  4127. __cam_isp_ctx_handle_error,
  4128. __cam_isp_ctx_sof_in_epoch,
  4129. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  4130. __cam_isp_ctx_notify_sof_in_activated_state,
  4131. __cam_isp_ctx_notify_eof_in_activated_state,
  4132. __cam_isp_ctx_buf_done_in_epoch,
  4133. __cam_isp_ctx_handle_secondary_events,
  4134. },
  4135. },
  4136. /* BUBBLE */
  4137. {
  4138. .irq_ops = {
  4139. __cam_isp_ctx_handle_error,
  4140. __cam_isp_ctx_sof_in_activated_state,
  4141. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  4142. __cam_isp_ctx_notify_sof_in_activated_state,
  4143. __cam_isp_ctx_notify_eof_in_activated_state,
  4144. __cam_isp_ctx_buf_done_in_bubble,
  4145. __cam_isp_ctx_handle_secondary_events,
  4146. },
  4147. },
  4148. /* Bubble Applied */
  4149. {
  4150. .irq_ops = {
  4151. __cam_isp_ctx_handle_error,
  4152. __cam_isp_ctx_sof_in_activated_state,
  4153. __cam_isp_ctx_reg_upd_in_applied_state,
  4154. __cam_isp_ctx_epoch_in_bubble_applied,
  4155. NULL,
  4156. __cam_isp_ctx_buf_done_in_bubble_applied,
  4157. __cam_isp_ctx_handle_secondary_events,
  4158. },
  4159. },
  4160. /* HW ERROR */
  4161. {
  4162. .irq_ops = {
  4163. NULL,
  4164. __cam_isp_ctx_sof_in_activated_state,
  4165. __cam_isp_ctx_reg_upd_in_hw_error,
  4166. NULL,
  4167. NULL,
  4168. NULL,
  4169. },
  4170. },
  4171. /* HALT */
  4172. {
  4173. },
  4174. };
  4175. static struct cam_isp_ctx_irq_ops
  4176. cam_isp_ctx_fs2_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  4177. /* SOF */
  4178. {
  4179. .irq_ops = {
  4180. __cam_isp_ctx_handle_error,
  4181. __cam_isp_ctx_fs2_sof_in_sof_state,
  4182. __cam_isp_ctx_fs2_reg_upd_in_sof,
  4183. __cam_isp_ctx_fs2_sof_in_sof_state,
  4184. __cam_isp_ctx_notify_eof_in_activated_state,
  4185. NULL,
  4186. },
  4187. },
  4188. /* APPLIED */
  4189. {
  4190. .irq_ops = {
  4191. __cam_isp_ctx_handle_error,
  4192. __cam_isp_ctx_sof_in_activated_state,
  4193. __cam_isp_ctx_fs2_reg_upd_in_applied_state,
  4194. __cam_isp_ctx_epoch_in_applied,
  4195. __cam_isp_ctx_notify_eof_in_activated_state,
  4196. __cam_isp_ctx_fs2_buf_done_in_applied,
  4197. },
  4198. },
  4199. /* EPOCH */
  4200. {
  4201. .irq_ops = {
  4202. __cam_isp_ctx_handle_error,
  4203. __cam_isp_ctx_sof_in_epoch,
  4204. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  4205. __cam_isp_ctx_notify_sof_in_activated_state,
  4206. __cam_isp_ctx_notify_eof_in_activated_state,
  4207. __cam_isp_ctx_fs2_buf_done_in_epoch,
  4208. },
  4209. },
  4210. /* BUBBLE */
  4211. {
  4212. .irq_ops = {
  4213. __cam_isp_ctx_handle_error,
  4214. __cam_isp_ctx_sof_in_activated_state,
  4215. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  4216. __cam_isp_ctx_notify_sof_in_activated_state,
  4217. __cam_isp_ctx_notify_eof_in_activated_state,
  4218. __cam_isp_ctx_buf_done_in_bubble,
  4219. },
  4220. },
  4221. /* Bubble Applied */
  4222. {
  4223. .irq_ops = {
  4224. __cam_isp_ctx_handle_error,
  4225. __cam_isp_ctx_sof_in_activated_state,
  4226. __cam_isp_ctx_reg_upd_in_applied_state,
  4227. __cam_isp_ctx_epoch_in_bubble_applied,
  4228. NULL,
  4229. __cam_isp_ctx_buf_done_in_bubble_applied,
  4230. },
  4231. },
  4232. /* HW ERROR */
  4233. {
  4234. .irq_ops = {
  4235. NULL,
  4236. __cam_isp_ctx_sof_in_activated_state,
  4237. __cam_isp_ctx_reg_upd_in_hw_error,
  4238. NULL,
  4239. NULL,
  4240. NULL,
  4241. },
  4242. },
  4243. /* HALT */
  4244. {
  4245. },
  4246. };
  4247. static struct cam_isp_ctx_irq_ops
  4248. cam_isp_ctx_offline_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  4249. /* SOF */
  4250. {
  4251. .irq_ops = {
  4252. __cam_isp_ctx_handle_error,
  4253. NULL,
  4254. NULL,
  4255. NULL,
  4256. NULL,
  4257. NULL,
  4258. },
  4259. },
  4260. /* APPLIED */
  4261. {
  4262. .irq_ops = {
  4263. __cam_isp_ctx_handle_error,
  4264. __cam_isp_ctx_sof_in_activated_state,
  4265. __cam_isp_ctx_reg_upd_in_applied_state,
  4266. __cam_isp_ctx_offline_epoch_in_activated_state,
  4267. NULL,
  4268. __cam_isp_ctx_buf_done_in_applied,
  4269. },
  4270. },
  4271. /* EPOCH */
  4272. {
  4273. .irq_ops = {
  4274. __cam_isp_ctx_handle_error,
  4275. __cam_isp_ctx_sof_in_activated_state,
  4276. NULL,
  4277. __cam_isp_ctx_offline_epoch_in_activated_state,
  4278. NULL,
  4279. __cam_isp_ctx_buf_done_in_epoch,
  4280. },
  4281. },
  4282. /* BUBBLE */
  4283. {
  4284. },
  4285. /* Bubble Applied */
  4286. {
  4287. },
  4288. /* HW ERROR */
  4289. {
  4290. .irq_ops = {
  4291. NULL,
  4292. __cam_isp_ctx_sof_in_activated_state,
  4293. __cam_isp_ctx_reg_upd_in_hw_error,
  4294. NULL,
  4295. NULL,
  4296. NULL,
  4297. },
  4298. },
  4299. /* HALT */
  4300. {
  4301. },
  4302. };
  4303. static inline int cam_isp_context_apply_evt_injection(struct cam_context *ctx)
  4304. {
  4305. struct cam_isp_context *ctx_isp = ctx->ctx_priv;
  4306. struct cam_hw_inject_evt_param *evt_inject_params = &ctx_isp->evt_inject_params;
  4307. struct cam_common_evt_inject_data inject_evt = {0};
  4308. int rc;
  4309. inject_evt.evt_params = evt_inject_params;
  4310. rc = cam_context_apply_evt_injection(ctx, &inject_evt);
  4311. if (rc)
  4312. CAM_ERR(CAM_ISP, "Fail to apply event injection ctx_id: %u link: 0x%x req_id: %u",
  4313. ctx->ctx_id, ctx->link_hdl, evt_inject_params->req_id);
  4314. evt_inject_params->is_valid = false;
  4315. return rc;
  4316. }
  4317. static inline void __cam_isp_ctx_update_fcg_prediction_idx(
  4318. struct cam_context *ctx,
  4319. uint64_t request_id,
  4320. struct cam_isp_fcg_prediction_tracker *fcg_tracker,
  4321. struct cam_isp_fcg_config_info *fcg_info)
  4322. {
  4323. struct cam_isp_context *ctx_isp = ctx->ctx_priv;
  4324. if ((fcg_tracker->sum_skipped == 0) ||
  4325. (fcg_tracker->sum_skipped > CAM_ISP_MAX_FCG_PREDICTIONS)) {
  4326. fcg_info->use_current_cfg = true;
  4327. CAM_DBG(CAM_ISP,
  4328. "Apply req: %llu, Use current FCG value, frame_id: %llu, ctx_id: %u",
  4329. request_id, ctx_isp->frame_id, ctx->ctx_id);
  4330. } else {
  4331. fcg_info->prediction_idx = fcg_tracker->sum_skipped;
  4332. CAM_DBG(CAM_ISP,
  4333. "Apply req: %llu, FCG prediction: %u, frame_id: %llu, ctx_id: %u",
  4334. request_id, fcg_tracker->sum_skipped,
  4335. ctx_isp->frame_id, ctx->ctx_id);
  4336. }
  4337. }
  4338. static inline void __cam_isp_ctx_print_fcg_tracker(
  4339. struct cam_isp_fcg_prediction_tracker *fcg_tracker)
  4340. {
  4341. uint32_t skipped_list[CAM_ISP_AFD_PIPELINE_DELAY];
  4342. struct cam_isp_skip_frame_info *skip_info;
  4343. int i = 0;
  4344. list_for_each_entry(skip_info,
  4345. &fcg_tracker->skipped_list, list) {
  4346. skipped_list[i] = skip_info->num_frame_skipped;
  4347. i += 1;
  4348. }
  4349. CAM_DBG(CAM_ISP,
  4350. "FCG tracker num_skipped: %u, sum_skipped: %u, skipped list: [%u, %u, %u]",
  4351. fcg_tracker->num_skipped, fcg_tracker->sum_skipped,
  4352. skipped_list[0], skipped_list[1], skipped_list[2]);
  4353. }
  4354. static int __cam_isp_ctx_apply_req_in_activated_state(
  4355. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply,
  4356. enum cam_isp_ctx_activated_substate next_state)
  4357. {
  4358. int rc = 0;
  4359. struct cam_ctx_request *req;
  4360. struct cam_ctx_request *active_req = NULL;
  4361. struct cam_isp_ctx_req *req_isp;
  4362. struct cam_isp_ctx_req *active_req_isp;
  4363. struct cam_isp_context *ctx_isp = NULL;
  4364. struct cam_hw_config_args cfg = {0};
  4365. struct cam_isp_skip_frame_info *skip_info;
  4366. struct cam_isp_fcg_prediction_tracker *fcg_tracker;
  4367. struct cam_isp_fcg_config_info *fcg_info;
  4368. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  4369. /* Reset mswitch ref cnt */
  4370. atomic_set(&ctx_isp->mswitch_default_apply_delay_ref_cnt,
  4371. ctx_isp->mswitch_default_apply_delay_max_cnt);
  4372. if (apply->re_apply)
  4373. if (apply->request_id <= ctx_isp->last_applied_req_id) {
  4374. CAM_INFO_RATE_LIMIT(CAM_ISP,
  4375. "ctx_id:%u link: 0x%x Trying to reapply the same request %llu again",
  4376. ctx->ctx_id, ctx->link_hdl,
  4377. apply->request_id);
  4378. return 0;
  4379. }
  4380. if (list_empty(&ctx->pending_req_list)) {
  4381. CAM_ERR_RATE_LIMIT(CAM_ISP,
  4382. "ctx_id:%u link: 0x%x No available request for Apply id %lld",
  4383. ctx->ctx_id, ctx->link_hdl,
  4384. apply->request_id);
  4385. rc = -EFAULT;
  4386. goto end;
  4387. }
  4388. /*
  4389. * When the pipeline has issue, the requests can be queued up in the
  4390. * pipeline. In this case, we should reject the additional request.
  4391. * The maximum number of request allowed to be outstanding is 2.
  4392. *
  4393. */
  4394. if (atomic_read(&ctx_isp->process_bubble)) {
  4395. CAM_INFO_RATE_LIMIT(CAM_ISP,
  4396. "ctx_id:%u link: 0x%x Processing bubble cannot apply Request Id %llu",
  4397. ctx->ctx_id, ctx->link_hdl,
  4398. apply->request_id);
  4399. rc = -EFAULT;
  4400. goto end;
  4401. }
  4402. /*
  4403. * When isp processing internal recovery, the crm may still apply
  4404. * req to isp ctx. In this case, we should reject this req apply.
  4405. */
  4406. if (atomic_read(&ctx_isp->internal_recovery_set)) {
  4407. CAM_INFO_RATE_LIMIT(CAM_ISP,
  4408. "ctx_id:%u link: 0x%x Processing recovery cannot apply Request Id %lld",
  4409. ctx->ctx_id, ctx->link_hdl,
  4410. apply->request_id);
  4411. rc = -EAGAIN;
  4412. goto end;
  4413. }
  4414. spin_lock_bh(&ctx->lock);
  4415. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request,
  4416. list);
  4417. spin_unlock_bh(&ctx->lock);
  4418. /*
  4419. * Check whether the request id is matching the tip, if not, this means
  4420. * we are in the middle of the error handling. Need to reject this apply
  4421. */
  4422. if (req->request_id != apply->request_id) {
  4423. CAM_ERR_RATE_LIMIT(CAM_ISP,
  4424. "ctx_id:%u link: 0x%x Invalid Request Id asking %llu existing %llu",
  4425. ctx->ctx_id, ctx->link_hdl,
  4426. apply->request_id, req->request_id);
  4427. rc = -EFAULT;
  4428. goto end;
  4429. }
  4430. CAM_DBG(CAM_REQ, "Apply request %lld in Substate[%s] ctx %u, link: 0x%x",
  4431. req->request_id,
  4432. __cam_isp_ctx_substate_val_to_type(ctx_isp->substate_activated),
  4433. ctx->ctx_id, ctx->link_hdl);
  4434. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4435. if (ctx_isp->active_req_cnt >= 2) {
  4436. CAM_WARN_RATE_LIMIT(CAM_ISP,
  4437. "Reject apply request (id %lld) due to congestion(cnt = %d) ctx %u, link: 0x%x",
  4438. req->request_id,
  4439. ctx_isp->active_req_cnt,
  4440. ctx->ctx_id, ctx->link_hdl);
  4441. spin_lock_bh(&ctx->lock);
  4442. if (!list_empty(&ctx->active_req_list))
  4443. active_req = list_first_entry(&ctx->active_req_list,
  4444. struct cam_ctx_request, list);
  4445. else
  4446. CAM_ERR_RATE_LIMIT(CAM_ISP,
  4447. "WARNING: should not happen (cnt = %d) but active_list empty, ctx %u, link: 0x%x",
  4448. ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  4449. spin_unlock_bh(&ctx->lock);
  4450. if (active_req) {
  4451. active_req_isp =
  4452. (struct cam_isp_ctx_req *) active_req->req_priv;
  4453. __cam_isp_ctx_handle_buf_done_fail_log(ctx_isp,
  4454. active_req->request_id, active_req_isp);
  4455. }
  4456. rc = -EFAULT;
  4457. goto end;
  4458. }
  4459. /* Reset congestion counter */
  4460. ctx_isp->congestion_cnt = 0;
  4461. req_isp->bubble_report = apply->report_if_bubble;
  4462. /*
  4463. * Reset all buf done/bubble flags for the req being applied
  4464. * If internal recovery has led to re-apply of same
  4465. * request, clear all stale entities
  4466. */
  4467. req_isp->num_acked = 0;
  4468. req_isp->num_deferred_acks = 0;
  4469. req_isp->cdm_reset_before_apply = false;
  4470. req_isp->bubble_detected = false;
  4471. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4472. cfg.request_id = req->request_id;
  4473. cfg.hw_update_entries = req_isp->cfg;
  4474. cfg.num_hw_update_entries = req_isp->num_cfg;
  4475. cfg.priv = &req_isp->hw_update_data;
  4476. cfg.init_packet = 0;
  4477. cfg.reapply_type = req_isp->reapply_type;
  4478. cfg.cdm_reset_before_apply = req_isp->cdm_reset_before_apply;
  4479. if ((ctx_isp->evt_inject_params.is_valid) &&
  4480. (req->request_id == ctx_isp->evt_inject_params.req_id)) {
  4481. rc = cam_isp_context_apply_evt_injection(ctx_isp->base);
  4482. if (!rc)
  4483. goto end;
  4484. }
  4485. /* Decide the exact FCG prediction */
  4486. fcg_tracker = &ctx_isp->fcg_tracker;
  4487. fcg_info = &req_isp->hw_update_data.fcg_info;
  4488. if (!list_empty(&fcg_tracker->skipped_list)) {
  4489. __cam_isp_ctx_print_fcg_tracker(fcg_tracker);
  4490. skip_info = list_first_entry(&fcg_tracker->skipped_list,
  4491. struct cam_isp_skip_frame_info, list);
  4492. fcg_tracker->sum_skipped -= skip_info->num_frame_skipped;
  4493. if (unlikely((uint32_t)UINT_MAX - fcg_tracker->sum_skipped <
  4494. fcg_tracker->num_skipped))
  4495. fcg_tracker->num_skipped =
  4496. (uint32_t)UINT_MAX - fcg_tracker->sum_skipped;
  4497. fcg_tracker->sum_skipped += fcg_tracker->num_skipped;
  4498. skip_info->num_frame_skipped = fcg_tracker->num_skipped;
  4499. fcg_tracker->num_skipped = 0;
  4500. list_rotate_left(&fcg_tracker->skipped_list);
  4501. __cam_isp_ctx_print_fcg_tracker(fcg_tracker);
  4502. __cam_isp_ctx_update_fcg_prediction_idx(ctx,
  4503. apply->request_id, fcg_tracker, fcg_info);
  4504. }
  4505. atomic_set(&ctx_isp->apply_in_progress, 1);
  4506. rc = ctx->hw_mgr_intf->hw_config(ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  4507. if (!rc) {
  4508. spin_lock_bh(&ctx->lock);
  4509. ctx_isp->substate_activated = next_state;
  4510. ctx_isp->last_applied_req_id = apply->request_id;
  4511. ctx_isp->last_applied_jiffies = jiffies;
  4512. if (ctx_isp->is_tfe_shdr) {
  4513. if (ctx_isp->is_shdr_master && req_isp->hw_update_data.mup_en)
  4514. apply->dual_trigger_status = req_isp->hw_update_data.num_exp;
  4515. else
  4516. apply->dual_trigger_status = CAM_REQ_DUAL_TRIGGER_NONE;
  4517. } else {
  4518. apply->dual_trigger_status = CAM_REQ_DUAL_TRIGGER_NONE;
  4519. }
  4520. list_del_init(&req->list);
  4521. if (atomic_read(&ctx_isp->internal_recovery_set))
  4522. __cam_isp_ctx_enqueue_request_in_order(ctx, req, false);
  4523. else
  4524. list_add_tail(&req->list, &ctx->wait_req_list);
  4525. CAM_DBG(CAM_ISP, "new Substate[%s], applied req %lld, ctx_idx: %u, link: 0x%x",
  4526. __cam_isp_ctx_substate_val_to_type(next_state),
  4527. ctx_isp->last_applied_req_id, ctx->ctx_id, ctx->link_hdl);
  4528. spin_unlock_bh(&ctx->lock);
  4529. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  4530. CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED,
  4531. req->request_id);
  4532. __cam_isp_ctx_update_event_record(ctx_isp,
  4533. CAM_ISP_CTX_EVENT_APPLY, req, NULL);
  4534. } else if (rc == -EALREADY) {
  4535. spin_lock_bh(&ctx->lock);
  4536. req_isp->bubble_detected = true;
  4537. req_isp->cdm_reset_before_apply = false;
  4538. atomic_set(&ctx_isp->process_bubble, 1);
  4539. list_del_init(&req->list);
  4540. list_add(&req->list, &ctx->active_req_list);
  4541. ctx_isp->active_req_cnt++;
  4542. spin_unlock_bh(&ctx->lock);
  4543. CAM_DBG(CAM_REQ,
  4544. "move request %lld to active list(cnt = %d), ctx %u, link: 0x%x",
  4545. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  4546. } else {
  4547. CAM_ERR_RATE_LIMIT(CAM_ISP,
  4548. "ctx_id:%u link: 0x%x,Can not apply (req %lld) the configuration, rc %d",
  4549. ctx->ctx_id, ctx->link_hdl, apply->request_id, rc);
  4550. }
  4551. atomic_set(&ctx_isp->apply_in_progress, 0);
  4552. end:
  4553. return rc;
  4554. }
  4555. static int __cam_isp_ctx_apply_req_in_sof(
  4556. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  4557. {
  4558. int rc = 0;
  4559. struct cam_isp_context *ctx_isp =
  4560. (struct cam_isp_context *) ctx->ctx_priv;
  4561. CAM_DBG(CAM_ISP, "current Substate[%s], ctx %u, link: 0x%x",
  4562. __cam_isp_ctx_substate_val_to_type(
  4563. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  4564. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  4565. CAM_ISP_CTX_ACTIVATED_APPLIED);
  4566. CAM_DBG(CAM_ISP, "new Substate[%s], ctx %u, link: 0x%x",
  4567. __cam_isp_ctx_substate_val_to_type(
  4568. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  4569. if (rc)
  4570. CAM_DBG(CAM_ISP, "Apply failed in Substate[%s], rc %d, ctx %u, link: 0x%x",
  4571. __cam_isp_ctx_substate_val_to_type(
  4572. ctx_isp->substate_activated), rc, ctx->ctx_id, ctx->link_hdl);
  4573. return rc;
  4574. }
  4575. static int __cam_isp_ctx_apply_req_in_epoch(
  4576. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  4577. {
  4578. int rc = 0;
  4579. struct cam_isp_context *ctx_isp =
  4580. (struct cam_isp_context *) ctx->ctx_priv;
  4581. CAM_DBG(CAM_ISP, "current Substate[%s], ctx %u, link: 0x%x",
  4582. __cam_isp_ctx_substate_val_to_type(
  4583. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  4584. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  4585. CAM_ISP_CTX_ACTIVATED_APPLIED);
  4586. CAM_DBG(CAM_ISP, "new Substate[%s], ctx %u, link: 0x%x",
  4587. __cam_isp_ctx_substate_val_to_type(
  4588. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  4589. if (rc)
  4590. CAM_DBG(CAM_ISP, "Apply failed in Substate[%s], rc %d, ctx %u, link: 0x%x",
  4591. __cam_isp_ctx_substate_val_to_type(
  4592. ctx_isp->substate_activated), rc, ctx->ctx_id, ctx->link_hdl);
  4593. return rc;
  4594. }
  4595. static int __cam_isp_ctx_apply_req_in_bubble(
  4596. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  4597. {
  4598. int rc = 0;
  4599. struct cam_isp_context *ctx_isp =
  4600. (struct cam_isp_context *) ctx->ctx_priv;
  4601. CAM_DBG(CAM_ISP, "current Substate[%s], ctx %u, link: 0x%x",
  4602. __cam_isp_ctx_substate_val_to_type(
  4603. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  4604. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  4605. CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED);
  4606. CAM_DBG(CAM_ISP, "new Substate[%s], ctx %u, link: 0x%x",
  4607. __cam_isp_ctx_substate_val_to_type(
  4608. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  4609. if (rc)
  4610. CAM_DBG(CAM_ISP, "Apply failed in Substate[%s], rc %d, ctx %u, link: 0x%x",
  4611. __cam_isp_ctx_substate_val_to_type(
  4612. ctx_isp->substate_activated), rc, ctx->ctx_id, ctx->link_hdl);
  4613. return rc;
  4614. }
  4615. static void __cam_isp_ctx_find_mup_for_default_settings(
  4616. int64_t req_id, struct cam_context *ctx,
  4617. struct cam_ctx_request **switch_req)
  4618. {
  4619. struct cam_ctx_request *req, *temp_req;
  4620. if (list_empty(&ctx->pending_req_list)) {
  4621. CAM_DBG(CAM_ISP,
  4622. "Pending list empty, unable to find mup for req: %lld ctx: %u",
  4623. req_id, ctx->ctx_id);
  4624. return;
  4625. }
  4626. list_for_each_entry_safe(
  4627. req, temp_req, &ctx->pending_req_list, list) {
  4628. if (req->request_id == req_id) {
  4629. struct cam_isp_ctx_req *req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  4630. if (req_isp->hw_update_data.mup_en) {
  4631. *switch_req = req;
  4632. CAM_DBG(CAM_ISP,
  4633. "Found mup for last applied max pd req: %lld in ctx: %u",
  4634. req_id, ctx->ctx_id);
  4635. }
  4636. }
  4637. }
  4638. }
  4639. static int __cam_isp_ctx_apply_default_req_settings(
  4640. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  4641. {
  4642. int rc = 0;
  4643. bool skip_rup_aup = false;
  4644. struct cam_ctx_request *req = NULL;
  4645. struct cam_isp_ctx_req *req_isp = NULL;
  4646. struct cam_isp_context *isp_ctx =
  4647. (struct cam_isp_context *) ctx->ctx_priv;
  4648. struct cam_hw_cmd_args hw_cmd_args;
  4649. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4650. struct cam_hw_config_args cfg = {0};
  4651. if (isp_ctx->mode_switch_en && isp_ctx->handle_mswitch) {
  4652. if ((apply->last_applied_max_pd_req > 0) &&
  4653. (atomic_dec_and_test(&isp_ctx->mswitch_default_apply_delay_ref_cnt))) {
  4654. __cam_isp_ctx_find_mup_for_default_settings(
  4655. apply->last_applied_max_pd_req, ctx, &req);
  4656. }
  4657. if (req) {
  4658. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  4659. CAM_DBG(CAM_ISP,
  4660. "Applying IQ for mode switch req: %lld ctx: %u",
  4661. req->request_id, ctx->ctx_id);
  4662. cfg.ctxt_to_hw_map = isp_ctx->hw_ctx;
  4663. cfg.request_id = req->request_id;
  4664. cfg.hw_update_entries = req_isp->cfg;
  4665. cfg.num_hw_update_entries = req_isp->num_cfg;
  4666. cfg.priv = &req_isp->hw_update_data;
  4667. cfg.init_packet = 0;
  4668. cfg.reapply_type = CAM_CONFIG_REAPPLY_IQ;
  4669. cfg.cdm_reset_before_apply = req_isp->cdm_reset_before_apply;
  4670. rc = ctx->hw_mgr_intf->hw_config(ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  4671. if (rc) {
  4672. CAM_ERR(CAM_ISP,
  4673. "Failed to apply req: %lld IQ settings in ctx: %u",
  4674. req->request_id, ctx->ctx_id);
  4675. goto end;
  4676. }
  4677. skip_rup_aup = true;
  4678. }
  4679. }
  4680. if (isp_ctx->use_default_apply) {
  4681. hw_cmd_args.ctxt_to_hw_map = isp_ctx->hw_ctx;
  4682. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4683. isp_hw_cmd_args.cmd_type =
  4684. CAM_ISP_HW_MGR_CMD_PROG_DEFAULT_CFG;
  4685. isp_hw_cmd_args.cmd_data = (void *)&skip_rup_aup;
  4686. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4687. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4688. &hw_cmd_args);
  4689. if (rc)
  4690. CAM_ERR(CAM_ISP,
  4691. "Failed to apply default settings rc %d ctx %u, link: 0x%x",
  4692. rc, ctx->ctx_id, ctx->link_hdl);
  4693. else
  4694. CAM_DBG(CAM_ISP, "Applied default settings rc %d ctx: %u link: 0x%x",
  4695. rc, ctx->ctx_id, ctx->link_hdl);
  4696. }
  4697. end:
  4698. return rc;
  4699. }
  4700. static void *cam_isp_ctx_user_dump_req_list(
  4701. void *dump_struct, uint8_t *addr_ptr)
  4702. {
  4703. struct list_head *head = NULL;
  4704. uint64_t *addr;
  4705. struct cam_ctx_request *req, *req_temp;
  4706. head = (struct list_head *)dump_struct;
  4707. addr = (uint64_t *)addr_ptr;
  4708. if (!list_empty(head)) {
  4709. list_for_each_entry_safe(req, req_temp, head, list) {
  4710. *addr++ = req->request_id;
  4711. }
  4712. }
  4713. return addr;
  4714. }
  4715. static void *cam_isp_ctx_user_dump_active_requests(
  4716. void *dump_struct, uint8_t *addr_ptr)
  4717. {
  4718. uint64_t *addr;
  4719. struct cam_ctx_request *req;
  4720. req = (struct cam_ctx_request *)dump_struct;
  4721. addr = (uint64_t *)addr_ptr;
  4722. *addr++ = req->request_id;
  4723. return addr;
  4724. }
  4725. static int __cam_isp_ctx_dump_req_info(
  4726. struct cam_context *ctx,
  4727. struct cam_ctx_request *req,
  4728. struct cam_common_hw_dump_args *dump_args)
  4729. {
  4730. int i, rc = 0;
  4731. uint32_t min_len;
  4732. size_t remain_len;
  4733. struct cam_isp_ctx_req *req_isp;
  4734. struct cam_ctx_request *req_temp;
  4735. if (!req || !ctx || !dump_args) {
  4736. CAM_ERR(CAM_ISP, "Invalid parameters %pK %pK %pK",
  4737. req, ctx, dump_args);
  4738. return -EINVAL;
  4739. }
  4740. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  4741. if (dump_args->buf_len <= dump_args->offset) {
  4742. CAM_WARN(CAM_ISP,
  4743. "Dump buffer overshoot len %zu offset %zu, ctx_idx: %u, link: 0x%x",
  4744. dump_args->buf_len, dump_args->offset, ctx->ctx_id, ctx->link_hdl);
  4745. return -ENOSPC;
  4746. }
  4747. remain_len = dump_args->buf_len - dump_args->offset;
  4748. min_len = sizeof(struct cam_isp_context_dump_header) +
  4749. (CAM_ISP_CTX_DUMP_REQUEST_NUM_WORDS *
  4750. req_isp->num_fence_map_out *
  4751. sizeof(uint64_t));
  4752. if (remain_len < min_len) {
  4753. CAM_WARN(CAM_ISP, "Dump buffer exhaust remain %zu min %u, ctx_idx: %u, link: 0x%x",
  4754. remain_len, min_len, ctx->ctx_id, ctx->link_hdl);
  4755. return -ENOSPC;
  4756. }
  4757. /* Dump pending request list */
  4758. rc = cam_common_user_dump_helper(dump_args, cam_isp_ctx_user_dump_req_list,
  4759. &ctx->pending_req_list, sizeof(uint64_t), "ISP_OUT_FENCE_PENDING_REQUESTS:");
  4760. if (rc) {
  4761. CAM_ERR(CAM_ISP,
  4762. "CAM_ISP_CONTEXT:Pending request dump failed, rc:%d, ctx:%u, link:0x%x",
  4763. rc, ctx->ctx_id, ctx->link_hdl);
  4764. return rc;
  4765. }
  4766. /* Dump applied request list */
  4767. rc = cam_common_user_dump_helper(dump_args, cam_isp_ctx_user_dump_req_list,
  4768. &ctx->wait_req_list, sizeof(uint64_t), "ISP_OUT_FENCE_APPLIED_REQUESTS:");
  4769. if (rc) {
  4770. CAM_ERR(CAM_ISP,
  4771. "CAM_ISP_CONTEXT: Applied request dump failed, rc:%d, ctx:%u, link:0x%x",
  4772. rc, ctx->ctx_id, ctx->link_hdl);
  4773. return rc;
  4774. }
  4775. /* Dump active request list */
  4776. rc = cam_common_user_dump_helper(dump_args, cam_isp_ctx_user_dump_req_list,
  4777. &ctx->active_req_list, sizeof(uint64_t), "ISP_OUT_FENCE_ACTIVE_REQUESTS:");
  4778. if (rc) {
  4779. CAM_ERR(CAM_ISP,
  4780. "CAM_ISP_CONTEXT: Active request dump failed, rc:%d, ctx:%u, link:0x%x",
  4781. rc, ctx->ctx_id, ctx->link_hdl);
  4782. return rc;
  4783. }
  4784. /* Dump active request fences */
  4785. if (!list_empty(&ctx->active_req_list)) {
  4786. list_for_each_entry_safe(req, req_temp, &ctx->active_req_list, list) {
  4787. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  4788. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  4789. rc = cam_common_user_dump_helper(dump_args,
  4790. cam_isp_ctx_user_dump_active_requests,
  4791. req, sizeof(uint64_t),
  4792. "ISP_OUT_FENCE_REQUEST_ACTIVE.%s.%u.%d:",
  4793. __cam_isp_ife_sfe_resource_handle_id_to_type(
  4794. req_isp->fence_map_out[i].resource_handle),
  4795. req_isp->fence_map_out[i].image_buf_addr[0],
  4796. req_isp->fence_map_out[i].sync_id);
  4797. if (rc) {
  4798. CAM_ERR(CAM_ISP,
  4799. "CAM_ISP_CONTEXT DUMP_REQ_INFO: Dump failed, rc: %d, ctx_idx: %u, link: 0x%x",
  4800. rc, ctx->ctx_id, ctx->link_hdl);
  4801. return rc;
  4802. }
  4803. }
  4804. }
  4805. }
  4806. return rc;
  4807. }
  4808. static void *cam_isp_ctx_user_dump_timer(
  4809. void *dump_struct, uint8_t *addr_ptr)
  4810. {
  4811. struct cam_ctx_request *req = NULL;
  4812. struct cam_isp_ctx_req *req_isp = NULL;
  4813. uint64_t *addr;
  4814. ktime_t cur_time;
  4815. req = (struct cam_ctx_request *)dump_struct;
  4816. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  4817. cur_time = ktime_get();
  4818. addr = (uint64_t *)addr_ptr;
  4819. *addr++ = req->request_id;
  4820. *addr++ = ktime_to_timespec64(
  4821. req_isp->event_timestamp[CAM_ISP_CTX_EVENT_APPLY]).tv_sec;
  4822. *addr++ = ktime_to_timespec64(
  4823. req_isp->event_timestamp[CAM_ISP_CTX_EVENT_APPLY]).tv_nsec / NSEC_PER_USEC;
  4824. *addr++ = ktime_to_timespec64(cur_time).tv_sec;
  4825. *addr++ = ktime_to_timespec64(cur_time).tv_nsec / NSEC_PER_USEC;
  4826. return addr;
  4827. }
  4828. static void *cam_isp_ctx_user_dump_stream_info(
  4829. void *dump_struct, uint8_t *addr_ptr)
  4830. {
  4831. struct cam_context *ctx = NULL;
  4832. int32_t *addr;
  4833. ctx = (struct cam_context *)dump_struct;
  4834. addr = (int32_t *)addr_ptr;
  4835. *addr++ = ctx->ctx_id;
  4836. *addr++ = ctx->dev_hdl;
  4837. *addr++ = ctx->link_hdl;
  4838. return addr;
  4839. }
  4840. static int __cam_isp_ctx_dump_in_top_state(
  4841. struct cam_context *ctx,
  4842. struct cam_req_mgr_dump_info *dump_info)
  4843. {
  4844. int rc = 0;
  4845. bool dump_only_event_record = false;
  4846. size_t buf_len;
  4847. size_t remain_len;
  4848. ktime_t cur_time;
  4849. uint32_t min_len;
  4850. uint64_t diff;
  4851. uintptr_t cpu_addr;
  4852. uint8_t req_type;
  4853. struct cam_isp_context *ctx_isp;
  4854. struct cam_ctx_request *req = NULL;
  4855. struct cam_isp_ctx_req *req_isp;
  4856. struct cam_ctx_request *req_temp;
  4857. struct cam_hw_dump_args ife_dump_args;
  4858. struct cam_common_hw_dump_args dump_args;
  4859. struct cam_hw_cmd_args hw_cmd_args;
  4860. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4861. spin_lock_bh(&ctx->lock);
  4862. list_for_each_entry_safe(req, req_temp,
  4863. &ctx->active_req_list, list) {
  4864. if (req->request_id == dump_info->req_id) {
  4865. CAM_INFO(CAM_ISP, "isp dump active list req: %lld, ctx_idx: %u, link: 0x%x",
  4866. dump_info->req_id, ctx->ctx_id, ctx->link_hdl);
  4867. req_type = 'a';
  4868. goto hw_dump;
  4869. }
  4870. }
  4871. list_for_each_entry_safe(req, req_temp,
  4872. &ctx->wait_req_list, list) {
  4873. if (req->request_id == dump_info->req_id) {
  4874. CAM_INFO(CAM_ISP, "isp dump wait list req: %lld, ctx_idx: %u, link: 0x%x",
  4875. dump_info->req_id, ctx->ctx_id, ctx->link_hdl);
  4876. req_type = 'w';
  4877. goto hw_dump;
  4878. }
  4879. }
  4880. list_for_each_entry_safe(req, req_temp,
  4881. &ctx->pending_req_list, list) {
  4882. if (req->request_id == dump_info->req_id) {
  4883. CAM_INFO(CAM_ISP,
  4884. "isp dump pending list req: %lld, ctx_idx: %u, link: 0x%x",
  4885. dump_info->req_id, ctx->ctx_id, ctx->link_hdl);
  4886. req_type = 'p';
  4887. goto hw_dump;
  4888. }
  4889. }
  4890. goto end;
  4891. hw_dump:
  4892. rc = cam_mem_get_cpu_buf(dump_info->buf_handle,
  4893. &cpu_addr, &buf_len);
  4894. if (rc) {
  4895. CAM_ERR(CAM_ISP, "Invalid handle %u rc %d, ctx_idx: %u, link: 0x%x",
  4896. dump_info->buf_handle, rc, ctx->ctx_id, ctx->link_hdl);
  4897. spin_unlock_bh(&ctx->lock);
  4898. return rc;
  4899. }
  4900. if (buf_len <= dump_info->offset) {
  4901. spin_unlock_bh(&ctx->lock);
  4902. CAM_WARN(CAM_ISP,
  4903. "Dump buffer overshoot len %zu offset %zu, ctx_idx: %u, link: 0x%x",
  4904. buf_len, dump_info->offset, ctx->ctx_id, ctx->link_hdl);
  4905. cam_mem_put_cpu_buf(dump_info->buf_handle);
  4906. return -ENOSPC;
  4907. }
  4908. remain_len = buf_len - dump_info->offset;
  4909. min_len = sizeof(struct cam_isp_context_dump_header) +
  4910. (CAM_ISP_CTX_DUMP_NUM_WORDS * sizeof(uint64_t));
  4911. if (remain_len < min_len) {
  4912. CAM_WARN(CAM_ISP,
  4913. "Dump buffer exhaust remain %zu min %u, ctx_idx: %u, link: 0x%x",
  4914. remain_len, min_len, ctx->ctx_id, ctx->link_hdl);
  4915. spin_unlock_bh(&ctx->lock);
  4916. cam_mem_put_cpu_buf(dump_info->buf_handle);
  4917. return -ENOSPC;
  4918. }
  4919. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  4920. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4921. cur_time = ktime_get();
  4922. diff = ktime_us_delta(
  4923. req_isp->event_timestamp[CAM_ISP_CTX_EVENT_APPLY],
  4924. cur_time);
  4925. __cam_isp_ctx_print_event_record(ctx_isp);
  4926. if (diff < CAM_ISP_CTX_RESPONSE_TIME_THRESHOLD) {
  4927. CAM_INFO(CAM_ISP, "req %lld found no error, ctx_idx: %u, link: 0x%x",
  4928. req->request_id, ctx->ctx_id, ctx->link_hdl);
  4929. dump_only_event_record = true;
  4930. }
  4931. dump_args.req_id = dump_info->req_id;
  4932. dump_args.cpu_addr = cpu_addr;
  4933. dump_args.buf_len = buf_len;
  4934. dump_args.offset = dump_info->offset;
  4935. dump_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4936. /* Dump time info */
  4937. rc = cam_common_user_dump_helper(&dump_args, cam_isp_ctx_user_dump_timer,
  4938. req, sizeof(uint64_t), "ISP_CTX_DUMP.%c:", req_type);
  4939. if (rc) {
  4940. CAM_ERR(CAM_ISP, "Time dump fail %lld, rc: %d, ctx_idx: %u, link: 0x%x",
  4941. req->request_id, rc, ctx->ctx_id, ctx->link_hdl);
  4942. goto end;
  4943. }
  4944. dump_info->offset = dump_args.offset;
  4945. min_len = sizeof(struct cam_isp_context_dump_header) +
  4946. (CAM_ISP_CTX_DUMP_NUM_WORDS * sizeof(int32_t));
  4947. remain_len = buf_len - dump_info->offset;
  4948. if (remain_len < min_len) {
  4949. CAM_WARN(CAM_ISP,
  4950. "Dump buffer exhaust remain %zu min %u, ctx_idx: %u, link: 0x%x",
  4951. remain_len, min_len, ctx->ctx_id, ctx->link_hdl);
  4952. spin_unlock_bh(&ctx->lock);
  4953. cam_mem_put_cpu_buf(dump_info->buf_handle);
  4954. return -ENOSPC;
  4955. }
  4956. /* Dump stream info */
  4957. ctx->ctxt_to_hw_map = ctx_isp->hw_ctx;
  4958. if (ctx->hw_mgr_intf->hw_dump) {
  4959. /* Dump first part of stream info from isp context */
  4960. rc = cam_common_user_dump_helper(&dump_args,
  4961. cam_isp_ctx_user_dump_stream_info, ctx,
  4962. sizeof(int32_t), "ISP_STREAM_INFO_FROM_CTX:");
  4963. if (rc) {
  4964. CAM_ERR(CAM_ISP,
  4965. "ISP CTX stream info dump fail %lld, rc: %d, ctx: %u, link: 0x%x",
  4966. req->request_id, rc, ctx->ctx_id, ctx->link_hdl);
  4967. goto end;
  4968. }
  4969. dump_info->offset = dump_args.offset;
  4970. remain_len = buf_len - dump_info->offset;
  4971. if (remain_len < min_len) {
  4972. CAM_WARN(CAM_ISP,
  4973. "Dump buffer exhaust remain %zu min %u, ctx_idx: %u, link: 0x%x",
  4974. remain_len, min_len, ctx->ctx_id, ctx->link_hdl);
  4975. spin_unlock_bh(&ctx->lock);
  4976. cam_mem_put_cpu_buf(dump_info->buf_handle);
  4977. return -ENOSPC;
  4978. }
  4979. /* Dump second part of stream info from ife hw manager */
  4980. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  4981. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4982. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_DUMP_STREAM_INFO;
  4983. isp_hw_cmd_args.cmd_data = &dump_args;
  4984. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4985. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv, &hw_cmd_args);
  4986. if (rc) {
  4987. CAM_ERR(CAM_ISP,
  4988. "IFE HW MGR stream info dump fail %lld, rc: %d, ctx: %u, link: 0x%x",
  4989. req->request_id, rc, ctx->ctx_id, ctx->link_hdl);
  4990. goto end;
  4991. }
  4992. dump_info->offset = dump_args.offset;
  4993. }
  4994. /* Dump event record */
  4995. rc = __cam_isp_ctx_dump_event_record(ctx_isp, &dump_args);
  4996. if (rc) {
  4997. CAM_ERR(CAM_ISP, "Event record dump fail %lld, rc: %d, ctx_idx: %u, link: 0x%x",
  4998. req->request_id, rc, ctx->ctx_id, ctx->link_hdl);
  4999. goto end;
  5000. }
  5001. dump_info->offset = dump_args.offset;
  5002. if (dump_only_event_record) {
  5003. goto end;
  5004. }
  5005. /* Dump state monitor array */
  5006. rc = __cam_isp_ctx_user_dump_state_monitor_array(ctx_isp, &dump_args);
  5007. if (rc) {
  5008. CAM_ERR(CAM_ISP, "Dump event fail %lld, rc: %d, ctx_idx: %u, link: 0x%x",
  5009. req->request_id, rc, ctx->ctx_id, ctx->link_hdl);
  5010. goto end;
  5011. }
  5012. /* Dump request info */
  5013. rc = __cam_isp_ctx_dump_req_info(ctx, req, &dump_args);
  5014. if (rc) {
  5015. CAM_ERR(CAM_ISP, "Dump Req info fail %lld, rc: %d, ctx_idx: %u, link: 0x%x",
  5016. req->request_id, rc, ctx->ctx_id, ctx->link_hdl);
  5017. goto end;
  5018. }
  5019. spin_unlock_bh(&ctx->lock);
  5020. /* Dump CSID, VFE, and SFE info */
  5021. dump_info->offset = dump_args.offset;
  5022. if (ctx->hw_mgr_intf->hw_dump) {
  5023. ife_dump_args.offset = dump_args.offset;
  5024. ife_dump_args.request_id = dump_info->req_id;
  5025. ife_dump_args.buf_handle = dump_info->buf_handle;
  5026. ife_dump_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5027. rc = ctx->hw_mgr_intf->hw_dump(
  5028. ctx->hw_mgr_intf->hw_mgr_priv,
  5029. &ife_dump_args);
  5030. dump_info->offset = ife_dump_args.offset;
  5031. }
  5032. cam_mem_put_cpu_buf(dump_info->buf_handle);
  5033. return rc;
  5034. end:
  5035. spin_unlock_bh(&ctx->lock);
  5036. cam_mem_put_cpu_buf(dump_info->buf_handle);
  5037. return rc;
  5038. }
  5039. static int __cam_isp_ctx_flush_req_in_flushed_state(
  5040. struct cam_context *ctx,
  5041. struct cam_req_mgr_flush_request *flush_req)
  5042. {
  5043. CAM_INFO(CAM_ISP, "Flush (type %d) in flushed state req id %lld ctx_id:%u link: 0x%x",
  5044. flush_req->type, flush_req->req_id, ctx->ctx_id, ctx->link_hdl);
  5045. if (flush_req->req_id > ctx->last_flush_req)
  5046. ctx->last_flush_req = flush_req->req_id;
  5047. return 0;
  5048. }
  5049. static int __cam_isp_ctx_flush_req(struct cam_context *ctx,
  5050. struct list_head *req_list, struct cam_req_mgr_flush_request *flush_req)
  5051. {
  5052. int i, rc, tmp = 0;
  5053. struct cam_ctx_request *req;
  5054. struct cam_ctx_request *req_temp;
  5055. struct cam_isp_ctx_req *req_isp;
  5056. struct list_head flush_list;
  5057. struct cam_isp_context *ctx_isp = NULL;
  5058. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  5059. INIT_LIST_HEAD(&flush_list);
  5060. if (list_empty(req_list)) {
  5061. CAM_DBG(CAM_ISP, "request list is empty, ctx_id:%u link: 0x%x",
  5062. ctx->ctx_id, ctx->link_hdl);
  5063. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  5064. CAM_INFO(CAM_ISP,
  5065. "no request to cancel(lastapplied:%lld cancel:%lld),ctx:%u link:0x%x",
  5066. ctx_isp->last_applied_req_id, flush_req->req_id,
  5067. ctx->ctx_id, ctx->link_hdl);
  5068. return -EINVAL;
  5069. } else
  5070. return 0;
  5071. }
  5072. CAM_DBG(CAM_REQ, "Flush [%u] in progress for req_id %llu, ctx_id:%u link: 0x%x",
  5073. flush_req->type, flush_req->req_id, ctx->ctx_id, ctx->link_hdl);
  5074. list_for_each_entry_safe(req, req_temp, req_list, list) {
  5075. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  5076. if (req->request_id != flush_req->req_id) {
  5077. continue;
  5078. } else {
  5079. list_del_init(&req->list);
  5080. list_add_tail(&req->list, &flush_list);
  5081. __cam_isp_ctx_update_state_monitor_array(
  5082. ctx_isp,
  5083. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH,
  5084. req->request_id);
  5085. break;
  5086. }
  5087. }
  5088. list_del_init(&req->list);
  5089. list_add_tail(&req->list, &flush_list);
  5090. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  5091. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH, req->request_id);
  5092. }
  5093. if (list_empty(&flush_list)) {
  5094. /*
  5095. * Maybe the req isn't sent to KMD since UMD already skip
  5096. * req in CSL layer.
  5097. */
  5098. CAM_INFO(CAM_ISP,
  5099. "flush list is empty, flush type %d for req %llu, ctx_id:%u link: 0x%x",
  5100. flush_req->type, flush_req->req_id, ctx->ctx_id, ctx->link_hdl);
  5101. return 0;
  5102. }
  5103. list_for_each_entry_safe(req, req_temp, &flush_list, list) {
  5104. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  5105. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5106. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  5107. if (req_isp->fence_map_out[i].sync_id != -1) {
  5108. CAM_DBG(CAM_ISP,
  5109. "Flush req 0x%llx, fence %d, ctx_id:%u link: 0x%x",
  5110. req->request_id,
  5111. req_isp->fence_map_out[i].sync_id,
  5112. ctx->ctx_id, ctx->link_hdl);
  5113. rc = cam_sync_signal(
  5114. req_isp->fence_map_out[i].sync_id,
  5115. CAM_SYNC_STATE_SIGNALED_CANCEL,
  5116. CAM_SYNC_ISP_EVENT_FLUSH);
  5117. if (rc) {
  5118. tmp = req_isp->fence_map_out[i].sync_id;
  5119. CAM_ERR_RATE_LIMIT(CAM_ISP,
  5120. "signal fence %d failed, ctx_id:%u link: 0x%x",
  5121. tmp, ctx->ctx_id, ctx->link_hdl);
  5122. }
  5123. req_isp->fence_map_out[i].sync_id = -1;
  5124. }
  5125. }
  5126. req_isp->reapply_type = CAM_CONFIG_REAPPLY_NONE;
  5127. req_isp->cdm_reset_before_apply = false;
  5128. list_del_init(&req->list);
  5129. list_add_tail(&req->list, &ctx->free_req_list);
  5130. }
  5131. return 0;
  5132. }
  5133. static inline void __cam_isp_ctx_reset_fcg_tracker(
  5134. struct cam_context *ctx)
  5135. {
  5136. struct cam_isp_context *ctx_isp;
  5137. struct cam_isp_skip_frame_info *skip_info;
  5138. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  5139. /* Reset skipped_list for FCG config */
  5140. ctx_isp->fcg_tracker.sum_skipped = 0;
  5141. ctx_isp->fcg_tracker.num_skipped = 0;
  5142. list_for_each_entry(skip_info, &ctx_isp->fcg_tracker.skipped_list, list)
  5143. skip_info->num_frame_skipped = 0;
  5144. CAM_DBG(CAM_ISP, "Reset FCG skip info on ctx %u link: %x",
  5145. ctx->ctx_id, ctx->link_hdl);
  5146. }
  5147. static int __cam_isp_ctx_flush_req_in_top_state(
  5148. struct cam_context *ctx,
  5149. struct cam_req_mgr_flush_request *flush_req)
  5150. {
  5151. int rc = 0;
  5152. struct cam_isp_context *ctx_isp;
  5153. struct cam_isp_stop_args stop_isp;
  5154. struct cam_hw_stop_args stop_args;
  5155. struct cam_hw_reset_args reset_args;
  5156. struct cam_req_mgr_timer_notify timer;
  5157. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  5158. /* Reset skipped_list for FCG config */
  5159. __cam_isp_ctx_reset_fcg_tracker(ctx);
  5160. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_ALL) {
  5161. if (ctx->state <= CAM_CTX_READY) {
  5162. ctx->state = CAM_CTX_ACQUIRED;
  5163. goto end;
  5164. }
  5165. spin_lock_bh(&ctx->lock);
  5166. ctx->state = CAM_CTX_FLUSHED;
  5167. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  5168. spin_unlock_bh(&ctx->lock);
  5169. CAM_INFO(CAM_ISP, "Last request id to flush is %lld, ctx_id:%u link: 0x%x",
  5170. flush_req->req_id, ctx->ctx_id, ctx->link_hdl);
  5171. ctx->last_flush_req = flush_req->req_id;
  5172. __cam_isp_ctx_trigger_reg_dump(CAM_HW_MGR_CMD_REG_DUMP_ON_FLUSH, ctx);
  5173. stop_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5174. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  5175. stop_isp.stop_only = true;
  5176. stop_isp.is_internal_stop = false;
  5177. stop_args.args = (void *)&stop_isp;
  5178. rc = ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  5179. &stop_args);
  5180. if (rc)
  5181. CAM_ERR(CAM_ISP, "Failed to stop HW in Flush rc: %d, ctx_id:%u link: 0x%x",
  5182. rc, ctx->ctx_id, ctx->link_hdl);
  5183. CAM_INFO(CAM_ISP, "Stop HW complete. Reset HW next.Ctx_id:%u link: 0x%x",
  5184. ctx->ctx_id, ctx->link_hdl);
  5185. CAM_DBG(CAM_ISP, "Flush wait and active lists, ctx_id:%u link: 0x%x",
  5186. ctx->ctx_id, ctx->link_hdl);
  5187. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_timer) {
  5188. timer.link_hdl = ctx->link_hdl;
  5189. timer.dev_hdl = ctx->dev_hdl;
  5190. timer.state = false;
  5191. ctx->ctx_crm_intf->notify_timer(&timer);
  5192. }
  5193. spin_lock_bh(&ctx->lock);
  5194. if (!list_empty(&ctx->wait_req_list))
  5195. __cam_isp_ctx_flush_req(ctx, &ctx->wait_req_list,
  5196. flush_req);
  5197. if (!list_empty(&ctx->active_req_list))
  5198. __cam_isp_ctx_flush_req(ctx, &ctx->active_req_list,
  5199. flush_req);
  5200. ctx_isp->active_req_cnt = 0;
  5201. spin_unlock_bh(&ctx->lock);
  5202. reset_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5203. rc = ctx->hw_mgr_intf->hw_reset(ctx->hw_mgr_intf->hw_mgr_priv,
  5204. &reset_args);
  5205. if (rc)
  5206. CAM_ERR(CAM_ISP, "Failed to reset HW rc: %d, ctx_id:%u link: 0x%x",
  5207. rc, ctx->ctx_id, ctx->link_hdl);
  5208. ctx_isp->init_received = false;
  5209. }
  5210. CAM_DBG(CAM_ISP, "Flush pending list, ctx_idx: %u, link: 0x%x", ctx->ctx_id, ctx->link_hdl);
  5211. /*
  5212. * On occasions when we are doing a flush all, HW would get reset
  5213. * shutting down any th/bh in the pipeline. If internal recovery
  5214. * is triggered prior to flush, by clearing the pending list post
  5215. * HW reset will ensure no stale request entities are left behind
  5216. */
  5217. spin_lock_bh(&ctx->lock);
  5218. __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  5219. spin_unlock_bh(&ctx->lock);
  5220. end:
  5221. ctx_isp->bubble_frame_cnt = 0;
  5222. ctx_isp->congestion_cnt = 0;
  5223. ctx_isp->sof_dbg_irq_en = false;
  5224. atomic_set(&ctx_isp->process_bubble, 0);
  5225. atomic_set(&ctx_isp->rxd_epoch, 0);
  5226. atomic_set(&ctx_isp->internal_recovery_set, 0);
  5227. return rc;
  5228. }
  5229. static int __cam_isp_ctx_flush_req_in_ready(
  5230. struct cam_context *ctx,
  5231. struct cam_req_mgr_flush_request *flush_req)
  5232. {
  5233. int rc = 0;
  5234. CAM_DBG(CAM_ISP, "try to flush pending list, ctx_id:%u link: 0x%x",
  5235. ctx->ctx_id, ctx->link_hdl);
  5236. spin_lock_bh(&ctx->lock);
  5237. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  5238. /* if nothing is in pending req list, change state to acquire */
  5239. if (list_empty(&ctx->pending_req_list))
  5240. ctx->state = CAM_CTX_ACQUIRED;
  5241. spin_unlock_bh(&ctx->lock);
  5242. trace_cam_context_state("ISP", ctx);
  5243. CAM_DBG(CAM_ISP, "Flush request in ready state. next state %d, ctx_id:%u link: 0x%x",
  5244. ctx->state, ctx->ctx_id, ctx->link_hdl);
  5245. return rc;
  5246. }
  5247. static struct cam_ctx_ops
  5248. cam_isp_ctx_activated_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  5249. /* SOF */
  5250. {
  5251. .ioctl_ops = {},
  5252. .crm_ops = {
  5253. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  5254. .notify_frame_skip =
  5255. __cam_isp_ctx_apply_default_req_settings,
  5256. },
  5257. .irq_ops = NULL,
  5258. },
  5259. /* APPLIED */
  5260. {
  5261. .ioctl_ops = {},
  5262. .crm_ops = {},
  5263. .irq_ops = NULL,
  5264. },
  5265. /* EPOCH */
  5266. {
  5267. .ioctl_ops = {},
  5268. .crm_ops = {
  5269. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  5270. .notify_frame_skip =
  5271. __cam_isp_ctx_apply_default_req_settings,
  5272. },
  5273. .irq_ops = NULL,
  5274. },
  5275. /* BUBBLE */
  5276. {
  5277. .ioctl_ops = {},
  5278. .crm_ops = {
  5279. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  5280. .notify_frame_skip =
  5281. __cam_isp_ctx_apply_default_req_settings,
  5282. },
  5283. .irq_ops = NULL,
  5284. },
  5285. /* Bubble Applied */
  5286. {
  5287. .ioctl_ops = {},
  5288. .crm_ops = {
  5289. .notify_frame_skip =
  5290. __cam_isp_ctx_apply_default_req_settings,
  5291. },
  5292. .irq_ops = NULL,
  5293. },
  5294. /* HW ERROR */
  5295. {
  5296. .ioctl_ops = {},
  5297. .crm_ops = {},
  5298. .irq_ops = NULL,
  5299. },
  5300. /* HALT */
  5301. {
  5302. .ioctl_ops = {},
  5303. .crm_ops = {},
  5304. .irq_ops = NULL,
  5305. },
  5306. };
  5307. static struct cam_ctx_ops
  5308. cam_isp_ctx_fs2_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  5309. /* SOF */
  5310. {
  5311. .ioctl_ops = {},
  5312. .crm_ops = {
  5313. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  5314. },
  5315. .irq_ops = NULL,
  5316. },
  5317. /* APPLIED */
  5318. {
  5319. .ioctl_ops = {},
  5320. .crm_ops = {},
  5321. .irq_ops = NULL,
  5322. },
  5323. /* EPOCH */
  5324. {
  5325. .ioctl_ops = {},
  5326. .crm_ops = {
  5327. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  5328. },
  5329. .irq_ops = NULL,
  5330. },
  5331. /* BUBBLE */
  5332. {
  5333. .ioctl_ops = {},
  5334. .crm_ops = {
  5335. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  5336. },
  5337. .irq_ops = NULL,
  5338. },
  5339. /* Bubble Applied */
  5340. {
  5341. .ioctl_ops = {},
  5342. .crm_ops = {},
  5343. .irq_ops = NULL,
  5344. },
  5345. /* HW ERROR */
  5346. {
  5347. .ioctl_ops = {},
  5348. .crm_ops = {},
  5349. .irq_ops = NULL,
  5350. },
  5351. /* HALT */
  5352. {
  5353. .ioctl_ops = {},
  5354. .crm_ops = {},
  5355. .irq_ops = NULL,
  5356. },
  5357. };
  5358. static int __cam_isp_ctx_rdi_only_sof_in_top_state(
  5359. struct cam_isp_context *ctx_isp, void *evt_data)
  5360. {
  5361. int rc = 0;
  5362. struct cam_context *ctx = ctx_isp->base;
  5363. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  5364. uint64_t request_id = 0;
  5365. if (!evt_data) {
  5366. CAM_ERR(CAM_ISP, "in valid sof event data, ctx_idx: %u, link: 0x%x",
  5367. ctx->ctx_id, ctx->link_hdl);
  5368. return -EINVAL;
  5369. }
  5370. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  5371. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx_idx: %u, link: 0x%x",
  5372. ctx_isp->frame_id, ctx_isp->sof_timestamp_val, ctx->ctx_id, ctx->link_hdl);
  5373. /*
  5374. * notify reqmgr with sof signal. Note, due to scheduling delay
  5375. * we can run into situation that two active requests has already
  5376. * be in the active queue while we try to do the notification.
  5377. * In this case, we need to skip the current notification. This
  5378. * helps the state machine to catch up the delay.
  5379. */
  5380. if (ctx_isp->active_req_cnt <= 2) {
  5381. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  5382. /*
  5383. * It's possible for rup done to be processed before
  5384. * SOF, check for first active request shutter here
  5385. */
  5386. if (!list_empty(&ctx->active_req_list)) {
  5387. struct cam_ctx_request *req = NULL;
  5388. req = list_first_entry(&ctx->active_req_list,
  5389. struct cam_ctx_request, list);
  5390. if (req->request_id > ctx_isp->reported_req_id) {
  5391. request_id = req->request_id;
  5392. ctx_isp->reported_req_id = request_id;
  5393. }
  5394. }
  5395. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5396. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  5397. } else {
  5398. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not notify SOF to CRM, ctx_idx: %u, link: 0x%x",
  5399. ctx->ctx_id, ctx->link_hdl);
  5400. }
  5401. if (list_empty(&ctx->active_req_list))
  5402. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  5403. else
  5404. CAM_DBG(CAM_ISP, "Still need to wait for the buf done, ctx_idx: %u, link: 0x%x",
  5405. ctx->ctx_id, ctx->link_hdl);
  5406. CAM_DBG(CAM_ISP, "next Substate[%s], ctx_idx: %u, link: 0x%x",
  5407. __cam_isp_ctx_substate_val_to_type(
  5408. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  5409. return rc;
  5410. }
  5411. static int __cam_isp_ctx_rdi_only_sof_in_applied_state(
  5412. struct cam_isp_context *ctx_isp, void *evt_data)
  5413. {
  5414. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  5415. if (!evt_data) {
  5416. CAM_ERR(CAM_ISP, "in valid sof event data");
  5417. return -EINVAL;
  5418. }
  5419. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  5420. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  5421. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  5422. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED;
  5423. CAM_DBG(CAM_ISP, "next Substate[%s]",
  5424. __cam_isp_ctx_substate_val_to_type(
  5425. ctx_isp->substate_activated));
  5426. return 0;
  5427. }
  5428. static int __cam_isp_ctx_rdi_only_sof_in_bubble_applied(
  5429. struct cam_isp_context *ctx_isp, void *evt_data)
  5430. {
  5431. struct cam_ctx_request *req;
  5432. struct cam_isp_ctx_req *req_isp;
  5433. struct cam_context *ctx = ctx_isp->base;
  5434. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  5435. uint64_t request_id = 0;
  5436. /*
  5437. * Sof in bubble applied state means, reg update not received.
  5438. * before increment frame id and override time stamp value, send
  5439. * the previous sof time stamp that got captured in the
  5440. * sof in applied state.
  5441. */
  5442. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx_idx: %u, link: 0x%x",
  5443. ctx_isp->frame_id, ctx_isp->sof_timestamp_val, ctx->ctx_id, ctx->link_hdl);
  5444. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5445. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  5446. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  5447. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx_idx: %u, link: 0x%x",
  5448. ctx_isp->frame_id, ctx_isp->sof_timestamp_val, ctx->ctx_id, ctx->link_hdl);
  5449. if (list_empty(&ctx->wait_req_list)) {
  5450. /*
  5451. * If no pending req in epoch, this is an error case.
  5452. * The recovery is to go back to sof state
  5453. */
  5454. CAM_ERR(CAM_ISP, "No wait request, ctx_idx: %u, link: 0x%x",
  5455. ctx->ctx_id, ctx->link_hdl);
  5456. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  5457. /* Send SOF event as empty frame*/
  5458. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5459. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  5460. goto end;
  5461. }
  5462. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  5463. list);
  5464. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  5465. req_isp->bubble_detected = true;
  5466. CAM_INFO_RATE_LIMIT(CAM_ISP, "Ctx:%u link: 0x%x Report Bubble flag %d req id:%lld",
  5467. ctx->ctx_id, ctx->link_hdl, req_isp->bubble_report, req->request_id);
  5468. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  5469. req_isp->cdm_reset_before_apply = false;
  5470. if (req_isp->bubble_report) {
  5471. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_BUBBLE,
  5472. req->request_id, ctx_isp);
  5473. atomic_set(&ctx_isp->process_bubble, 1);
  5474. } else {
  5475. req_isp->bubble_report = 0;
  5476. }
  5477. /*
  5478. * Always move the request to active list. Let buf done
  5479. * function handles the rest.
  5480. */
  5481. list_del_init(&req->list);
  5482. list_add_tail(&req->list, &ctx->active_req_list);
  5483. ctx_isp->active_req_cnt++;
  5484. CAM_DBG(CAM_ISP, "move request %lld to active list(cnt = %d), ctx_idx: %u, link: 0x%x",
  5485. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  5486. if (!req_isp->bubble_report) {
  5487. if (req->request_id > ctx_isp->reported_req_id) {
  5488. request_id = req->request_id;
  5489. ctx_isp->reported_req_id = request_id;
  5490. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5491. CAM_REQ_MGR_SOF_EVENT_ERROR);
  5492. } else
  5493. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5494. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  5495. } else
  5496. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5497. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  5498. /* change the state to bubble, as reg update has not come */
  5499. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  5500. CAM_DBG(CAM_ISP, "next Substate[%s], ctx_idx: %u, link: 0x%x",
  5501. __cam_isp_ctx_substate_val_to_type(
  5502. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  5503. end:
  5504. return 0;
  5505. }
  5506. static int __cam_isp_ctx_rdi_only_sof_in_bubble_state(
  5507. struct cam_isp_context *ctx_isp, void *evt_data)
  5508. {
  5509. uint32_t i;
  5510. struct cam_ctx_request *req;
  5511. struct cam_context *ctx = ctx_isp->base;
  5512. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  5513. struct cam_isp_ctx_req *req_isp;
  5514. struct cam_hw_cmd_args hw_cmd_args;
  5515. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5516. uint64_t request_id = 0;
  5517. uint64_t last_cdm_done_req = 0;
  5518. int rc = 0;
  5519. if (!evt_data) {
  5520. CAM_ERR(CAM_ISP, "in valid sof event data, ctx_idx: %u, link: 0x%x",
  5521. ctx->ctx_id, ctx->link_hdl);
  5522. return -EINVAL;
  5523. }
  5524. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  5525. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx_idx: %u, link: 0x%x",
  5526. ctx_isp->frame_id, ctx_isp->sof_timestamp_val, ctx->ctx_id, ctx->link_hdl);
  5527. if (atomic_read(&ctx_isp->process_bubble)) {
  5528. if (list_empty(&ctx->active_req_list)) {
  5529. CAM_ERR(CAM_ISP, "No available active req in bubble, ctx: %u, link: 0x%x",
  5530. ctx->ctx_id, ctx->link_hdl);
  5531. atomic_set(&ctx_isp->process_bubble, 0);
  5532. return -EINVAL;
  5533. }
  5534. if (ctx_isp->last_sof_timestamp ==
  5535. ctx_isp->sof_timestamp_val) {
  5536. CAM_DBG(CAM_ISP,
  5537. "Tasklet delay detected! Bubble frame: %lld check skipped, sof_timestamp: %lld, ctx_id: %u, link: 0x%x",
  5538. ctx_isp->frame_id,
  5539. ctx_isp->sof_timestamp_val,
  5540. ctx->ctx_id, ctx->link_hdl);
  5541. goto end;
  5542. }
  5543. req = list_first_entry(&ctx->active_req_list,
  5544. struct cam_ctx_request, list);
  5545. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5546. if (req_isp->bubble_detected) {
  5547. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5548. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5549. isp_hw_cmd_args.cmd_type =
  5550. CAM_ISP_HW_MGR_GET_LAST_CDM_DONE;
  5551. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5552. rc = ctx->hw_mgr_intf->hw_cmd(
  5553. ctx->hw_mgr_intf->hw_mgr_priv,
  5554. &hw_cmd_args);
  5555. if (rc) {
  5556. CAM_ERR(CAM_ISP, "HW command failed, ctx_id: %u, link: 0x%x",
  5557. ctx->ctx_id, ctx->link_hdl);
  5558. return rc;
  5559. }
  5560. last_cdm_done_req = isp_hw_cmd_args.u.last_cdm_done;
  5561. CAM_DBG(CAM_ISP, "last_cdm_done req: %d ctx_id: %u, link: 0x%x",
  5562. last_cdm_done_req, ctx->ctx_id, ctx->link_hdl);
  5563. if (last_cdm_done_req >= req->request_id) {
  5564. CAM_DBG(CAM_ISP,
  5565. "CDM callback detected for req: %lld, possible buf_done delay, waiting for buf_done, ctx_id: %u, link: 0x%x",
  5566. req->request_id, ctx->ctx_id, ctx->link_hdl);
  5567. if (req_isp->num_fence_map_out ==
  5568. req_isp->num_deferred_acks) {
  5569. __cam_isp_handle_deferred_buf_done(ctx_isp, req,
  5570. true,
  5571. CAM_SYNC_STATE_SIGNALED_ERROR,
  5572. CAM_SYNC_ISP_EVENT_BUBBLE);
  5573. __cam_isp_ctx_handle_buf_done_for_req_list(
  5574. ctx_isp, req);
  5575. }
  5576. goto end;
  5577. } else {
  5578. CAM_WARN(CAM_ISP,
  5579. "CDM callback not happened for req: %lld, possible CDM stuck or workqueue delay, ctx_id: %u, link: 0x%x",
  5580. req->request_id, ctx->ctx_id, ctx->link_hdl);
  5581. req_isp->num_acked = 0;
  5582. req_isp->num_deferred_acks = 0;
  5583. req_isp->bubble_detected = false;
  5584. req_isp->cdm_reset_before_apply = true;
  5585. list_del_init(&req->list);
  5586. list_add(&req->list, &ctx->pending_req_list);
  5587. atomic_set(&ctx_isp->process_bubble, 0);
  5588. ctx_isp->active_req_cnt--;
  5589. CAM_DBG(CAM_REQ,
  5590. "Move active req: %lld to pending list(cnt = %d) [bubble re-apply],ctx %u link: 0x%x",
  5591. req->request_id,
  5592. ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  5593. }
  5594. goto end;
  5595. }
  5596. }
  5597. /*
  5598. * Signal all active requests with error and move the all the active
  5599. * requests to free list
  5600. */
  5601. while (!list_empty(&ctx->active_req_list)) {
  5602. req = list_first_entry(&ctx->active_req_list,
  5603. struct cam_ctx_request, list);
  5604. list_del_init(&req->list);
  5605. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  5606. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5607. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d, ctx %u link: 0x%x",
  5608. req_isp->num_fence_map_out, ctx->ctx_id, ctx->link_hdl);
  5609. for (i = 0; i < req_isp->num_fence_map_out; i++)
  5610. if (req_isp->fence_map_out[i].sync_id != -1) {
  5611. cam_sync_signal(
  5612. req_isp->fence_map_out[i].sync_id,
  5613. CAM_SYNC_STATE_SIGNALED_ERROR,
  5614. CAM_SYNC_ISP_EVENT_BUBBLE);
  5615. }
  5616. list_add_tail(&req->list, &ctx->free_req_list);
  5617. ctx_isp->active_req_cnt--;
  5618. }
  5619. end:
  5620. /* notify reqmgr with sof signal */
  5621. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  5622. /*
  5623. * It is idle frame with out any applied request id, send
  5624. * request id as zero
  5625. */
  5626. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5627. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  5628. /*
  5629. * Can't move the substate to SOF if we are processing bubble,
  5630. * since the SOF substate can't receive REG_UPD and buf done,
  5631. * then the processing of bubble req can't be finished
  5632. */
  5633. if (!atomic_read(&ctx_isp->process_bubble))
  5634. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  5635. CAM_DBG(CAM_ISP, "next Substate[%s], ctx %u link: 0x%x",
  5636. __cam_isp_ctx_substate_val_to_type(
  5637. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  5638. ctx_isp->last_sof_timestamp = ctx_isp->sof_timestamp_val;
  5639. return 0;
  5640. }
  5641. static int __cam_isp_ctx_rdi_only_reg_upd_in_bubble_state(
  5642. struct cam_isp_context *ctx_isp, void *evt_data)
  5643. {
  5644. struct cam_ctx_request *req = NULL;
  5645. struct cam_context *ctx = ctx_isp->base;
  5646. req = list_first_entry(&ctx->active_req_list,
  5647. struct cam_ctx_request, list);
  5648. CAM_INFO(CAM_ISP, "Received RUP for Bubble Request, ctx %u link: 0x%x",
  5649. req->request_id, ctx->ctx_id, ctx->link_hdl);
  5650. return 0;
  5651. }
  5652. static int __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state(
  5653. struct cam_isp_context *ctx_isp, void *evt_data)
  5654. {
  5655. struct cam_ctx_request *req = NULL;
  5656. struct cam_context *ctx = ctx_isp->base;
  5657. struct cam_isp_ctx_req *req_isp;
  5658. uint64_t request_id = 0;
  5659. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  5660. /* notify reqmgr with sof signal*/
  5661. if (list_empty(&ctx->wait_req_list)) {
  5662. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request, ctx %u link: 0x%x",
  5663. ctx->ctx_id, ctx->link_hdl);
  5664. goto error;
  5665. }
  5666. req = list_first_entry(&ctx->wait_req_list,
  5667. struct cam_ctx_request, list);
  5668. list_del_init(&req->list);
  5669. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5670. request_id =
  5671. (req_isp->hw_update_data.packet_opcode_type ==
  5672. CAM_ISP_PACKET_INIT_DEV) ? 0 : req->request_id;
  5673. if (req_isp->num_fence_map_out != 0) {
  5674. list_add_tail(&req->list, &ctx->active_req_list);
  5675. ctx_isp->active_req_cnt++;
  5676. CAM_DBG(CAM_ISP,
  5677. "move request %lld to active list(cnt = %d), ctx %u link: 0x%x",
  5678. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  5679. /* if packet has buffers, set correct request id */
  5680. request_id = req->request_id;
  5681. } else {
  5682. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  5683. /* no io config, so the request is completed. */
  5684. list_add_tail(&req->list, &ctx->free_req_list);
  5685. CAM_DBG(CAM_ISP,
  5686. "move active req %lld to free list(cnt=%d), ctx %u link: 0x%x",
  5687. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  5688. }
  5689. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  5690. if (request_id)
  5691. ctx_isp->reported_req_id = request_id;
  5692. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5693. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  5694. CAM_DBG(CAM_ISP, "next Substate[%s], ctx %u link: 0x%x",
  5695. __cam_isp_ctx_substate_val_to_type(
  5696. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  5697. __cam_isp_ctx_update_event_record(ctx_isp,
  5698. CAM_ISP_CTX_EVENT_RUP, req, NULL);
  5699. return 0;
  5700. error:
  5701. /* Send SOF event as idle frame*/
  5702. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5703. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  5704. __cam_isp_ctx_update_event_record(ctx_isp,
  5705. CAM_ISP_CTX_EVENT_RUP, NULL, NULL);
  5706. /*
  5707. * There is no request in the pending list, move the sub state machine
  5708. * to SOF sub state
  5709. */
  5710. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  5711. return 0;
  5712. }
  5713. static struct cam_isp_ctx_irq_ops
  5714. cam_isp_ctx_rdi_only_activated_state_machine_irq
  5715. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  5716. /* SOF */
  5717. {
  5718. .irq_ops = {
  5719. NULL,
  5720. __cam_isp_ctx_rdi_only_sof_in_top_state,
  5721. __cam_isp_ctx_reg_upd_in_sof,
  5722. NULL,
  5723. __cam_isp_ctx_notify_eof_in_activated_state,
  5724. NULL,
  5725. },
  5726. },
  5727. /* APPLIED */
  5728. {
  5729. .irq_ops = {
  5730. __cam_isp_ctx_handle_error,
  5731. __cam_isp_ctx_rdi_only_sof_in_applied_state,
  5732. __cam_isp_ctx_reg_upd_in_applied_state,
  5733. NULL,
  5734. __cam_isp_ctx_notify_eof_in_activated_state,
  5735. __cam_isp_ctx_buf_done_in_applied,
  5736. },
  5737. },
  5738. /* EPOCH */
  5739. {
  5740. .irq_ops = {
  5741. __cam_isp_ctx_handle_error,
  5742. __cam_isp_ctx_rdi_only_sof_in_top_state,
  5743. NULL,
  5744. NULL,
  5745. __cam_isp_ctx_notify_eof_in_activated_state,
  5746. __cam_isp_ctx_buf_done_in_epoch,
  5747. },
  5748. },
  5749. /* BUBBLE*/
  5750. {
  5751. .irq_ops = {
  5752. __cam_isp_ctx_handle_error,
  5753. __cam_isp_ctx_rdi_only_sof_in_bubble_state,
  5754. __cam_isp_ctx_rdi_only_reg_upd_in_bubble_state,
  5755. NULL,
  5756. __cam_isp_ctx_notify_eof_in_activated_state,
  5757. __cam_isp_ctx_buf_done_in_bubble,
  5758. },
  5759. },
  5760. /* BUBBLE APPLIED ie PRE_BUBBLE */
  5761. {
  5762. .irq_ops = {
  5763. __cam_isp_ctx_handle_error,
  5764. __cam_isp_ctx_rdi_only_sof_in_bubble_applied,
  5765. __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state,
  5766. NULL,
  5767. __cam_isp_ctx_notify_eof_in_activated_state,
  5768. __cam_isp_ctx_buf_done_in_bubble_applied,
  5769. },
  5770. },
  5771. /* HW ERROR */
  5772. {
  5773. },
  5774. /* HALT */
  5775. {
  5776. },
  5777. };
  5778. static int __cam_isp_ctx_rdi_only_apply_req_top_state(
  5779. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  5780. {
  5781. int rc = 0;
  5782. struct cam_isp_context *ctx_isp =
  5783. (struct cam_isp_context *) ctx->ctx_priv;
  5784. CAM_DBG(CAM_ISP, "current Substate[%s], ctx_idx: %u, link: 0x%x",
  5785. __cam_isp_ctx_substate_val_to_type(
  5786. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  5787. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  5788. CAM_ISP_CTX_ACTIVATED_APPLIED);
  5789. CAM_DBG(CAM_ISP, "new Substate[%s], ctx_idx: %u, link: 0x%x",
  5790. __cam_isp_ctx_substate_val_to_type(
  5791. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  5792. if (rc)
  5793. CAM_ERR_RATE_LIMIT(CAM_ISP,
  5794. "ctx_id:%u link: 0x%x Apply failed in Substate[%s], rc %d",
  5795. ctx->ctx_id, ctx->link_hdl,
  5796. __cam_isp_ctx_substate_val_to_type(
  5797. ctx_isp->substate_activated), rc);
  5798. return rc;
  5799. }
  5800. static struct cam_ctx_ops
  5801. cam_isp_ctx_rdi_only_activated_state_machine
  5802. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  5803. /* SOF */
  5804. {
  5805. .ioctl_ops = {},
  5806. .crm_ops = {
  5807. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  5808. },
  5809. .irq_ops = NULL,
  5810. },
  5811. /* APPLIED */
  5812. {
  5813. .ioctl_ops = {},
  5814. .crm_ops = {},
  5815. .irq_ops = NULL,
  5816. },
  5817. /* EPOCH */
  5818. {
  5819. .ioctl_ops = {},
  5820. .crm_ops = {
  5821. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  5822. },
  5823. .irq_ops = NULL,
  5824. },
  5825. /* PRE BUBBLE */
  5826. {
  5827. .ioctl_ops = {},
  5828. .crm_ops = {},
  5829. .irq_ops = NULL,
  5830. },
  5831. /* BUBBLE */
  5832. {
  5833. .ioctl_ops = {},
  5834. .crm_ops = {},
  5835. .irq_ops = NULL,
  5836. },
  5837. /* HW ERROR */
  5838. {
  5839. .ioctl_ops = {},
  5840. .crm_ops = {},
  5841. .irq_ops = NULL,
  5842. },
  5843. /* HALT */
  5844. {
  5845. .ioctl_ops = {},
  5846. .crm_ops = {},
  5847. .irq_ops = NULL,
  5848. },
  5849. };
  5850. static int __cam_isp_ctx_flush_dev_in_top_state(struct cam_context *ctx,
  5851. struct cam_flush_dev_cmd *cmd)
  5852. {
  5853. struct cam_isp_context *ctx_isp = ctx->ctx_priv;
  5854. struct cam_req_mgr_flush_request flush_req;
  5855. if (!ctx_isp->offline_context) {
  5856. CAM_ERR(CAM_ISP, "flush dev only supported in offline context,ctx: %u, link:0x%x",
  5857. ctx->ctx_id, ctx->link_hdl);
  5858. return -EINVAL;
  5859. }
  5860. flush_req.type = (cmd->flush_type == CAM_FLUSH_TYPE_ALL) ? CAM_REQ_MGR_FLUSH_TYPE_ALL :
  5861. CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ;
  5862. flush_req.req_id = cmd->req_id;
  5863. CAM_DBG(CAM_ISP, "offline flush (type:%u, req:%lu), ctx_idx: %u, link: 0x%x",
  5864. flush_req.type, flush_req.req_id, ctx->ctx_id, ctx->link_hdl);
  5865. switch (ctx->state) {
  5866. case CAM_CTX_ACQUIRED:
  5867. case CAM_CTX_ACTIVATED:
  5868. return __cam_isp_ctx_flush_req_in_top_state(ctx, &flush_req);
  5869. case CAM_CTX_READY:
  5870. return __cam_isp_ctx_flush_req_in_ready(ctx, &flush_req);
  5871. default:
  5872. CAM_ERR(CAM_ISP, "flush dev in wrong state: %d, ctx_idx: %u, link: 0x%x",
  5873. ctx->state, ctx->ctx_id, ctx->link_hdl);
  5874. return -EINVAL;
  5875. }
  5876. if (cmd->flush_type == CAM_FLUSH_TYPE_ALL)
  5877. cam_req_mgr_workq_flush(ctx_isp->workq);
  5878. }
  5879. static void __cam_isp_ctx_free_mem_hw_entries(struct cam_context *ctx)
  5880. {
  5881. int i;
  5882. struct cam_isp_context *ctx_isp =
  5883. (struct cam_isp_context *) ctx->ctx_priv;
  5884. if (ctx->out_map_entries) {
  5885. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  5886. kfree(ctx->out_map_entries[i]);
  5887. ctx->out_map_entries[i] = NULL;
  5888. }
  5889. kfree(ctx->out_map_entries);
  5890. ctx->out_map_entries = NULL;
  5891. }
  5892. if (ctx->in_map_entries) {
  5893. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  5894. kfree(ctx->in_map_entries[i]);
  5895. ctx->in_map_entries[i] = NULL;
  5896. }
  5897. kfree(ctx->in_map_entries);
  5898. ctx->in_map_entries = NULL;
  5899. }
  5900. if (ctx->hw_update_entry) {
  5901. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  5902. kfree(ctx->hw_update_entry[i]);
  5903. ctx->hw_update_entry[i] = NULL;
  5904. }
  5905. kfree(ctx->hw_update_entry);
  5906. ctx->hw_update_entry = NULL;
  5907. }
  5908. if (ctx_isp) {
  5909. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  5910. kfree(ctx_isp->req_isp[i].deferred_fence_map_index);
  5911. ctx_isp->req_isp[i].deferred_fence_map_index = NULL;
  5912. }
  5913. }
  5914. ctx->max_out_map_entries = 0;
  5915. ctx->max_in_map_entries = 0;
  5916. ctx->max_hw_update_entries = 0;
  5917. }
  5918. static int __cam_isp_ctx_release_hw_in_top_state(struct cam_context *ctx,
  5919. void *cmd)
  5920. {
  5921. int rc = 0;
  5922. struct cam_hw_release_args rel_arg;
  5923. struct cam_isp_context *ctx_isp =
  5924. (struct cam_isp_context *) ctx->ctx_priv;
  5925. struct cam_req_mgr_flush_request flush_req;
  5926. int i;
  5927. if (ctx_isp->hw_ctx) {
  5928. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5929. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  5930. &rel_arg);
  5931. ctx_isp->hw_ctx = NULL;
  5932. } else {
  5933. CAM_ERR(CAM_ISP, "No hw resources acquired for ctx[%u], link: 0x%x",
  5934. ctx->ctx_id, ctx->link_hdl);
  5935. }
  5936. ctx->last_flush_req = 0;
  5937. ctx_isp->custom_enabled = false;
  5938. ctx_isp->use_frame_header_ts = false;
  5939. ctx_isp->use_default_apply = false;
  5940. ctx_isp->frame_id = 0;
  5941. ctx_isp->active_req_cnt = 0;
  5942. ctx_isp->reported_req_id = 0;
  5943. ctx_isp->reported_frame_id = 0;
  5944. ctx_isp->hw_acquired = false;
  5945. ctx_isp->init_received = false;
  5946. ctx_isp->support_consumed_addr = false;
  5947. ctx_isp->aeb_enabled = false;
  5948. ctx_isp->req_info.last_bufdone_req_id = 0;
  5949. kfree(ctx_isp->vfe_bus_comp_grp);
  5950. kfree(ctx_isp->sfe_bus_comp_grp);
  5951. ctx_isp->vfe_bus_comp_grp = NULL;
  5952. ctx_isp->sfe_bus_comp_grp = NULL;
  5953. atomic64_set(&ctx_isp->dbg_monitors.state_monitor_head, -1);
  5954. atomic64_set(&ctx_isp->dbg_monitors.frame_monitor_head, -1);
  5955. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  5956. atomic64_set(&ctx_isp->dbg_monitors.event_record_head[i], -1);
  5957. /*
  5958. * Ideally, we should never have any active request here.
  5959. * But we still add some sanity check code here to help the debug
  5960. */
  5961. if (!list_empty(&ctx->active_req_list))
  5962. CAM_WARN(CAM_ISP, "Active list is not empty, ctx_idx: %u, link: 0x%x",
  5963. ctx->ctx_id, ctx->link_hdl);
  5964. /* Flush all the pending request list */
  5965. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  5966. flush_req.link_hdl = ctx->link_hdl;
  5967. flush_req.dev_hdl = ctx->dev_hdl;
  5968. flush_req.req_id = 0;
  5969. CAM_DBG(CAM_ISP, "try to flush pending list, ctx_idx: %u, link: 0x%x",
  5970. ctx->ctx_id, ctx->link_hdl);
  5971. spin_lock_bh(&ctx->lock);
  5972. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  5973. spin_unlock_bh(&ctx->lock);
  5974. __cam_isp_ctx_free_mem_hw_entries(ctx);
  5975. cam_req_mgr_workq_destroy(&ctx_isp->workq);
  5976. ctx->state = CAM_CTX_ACQUIRED;
  5977. trace_cam_context_state("ISP", ctx);
  5978. CAM_DBG(CAM_ISP, "Release device success[%u] link: 0x%x next state %d",
  5979. ctx->ctx_id, ctx->link_hdl, ctx->state);
  5980. return rc;
  5981. }
  5982. /* top level state machine */
  5983. static int __cam_isp_ctx_release_dev_in_top_state(struct cam_context *ctx,
  5984. struct cam_release_dev_cmd *cmd)
  5985. {
  5986. int rc = 0;
  5987. int i;
  5988. struct cam_hw_release_args rel_arg;
  5989. struct cam_isp_context *ctx_isp =
  5990. (struct cam_isp_context *) ctx->ctx_priv;
  5991. struct cam_req_mgr_flush_request flush_req;
  5992. if (cmd && ctx_isp->hw_ctx) {
  5993. CAM_ERR(CAM_ISP, "releasing hw, ctx_idx: %u, link: 0x%x",
  5994. ctx->ctx_id, ctx->link_hdl);
  5995. __cam_isp_ctx_release_hw_in_top_state(ctx, NULL);
  5996. }
  5997. if (ctx_isp->hw_ctx) {
  5998. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5999. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  6000. &rel_arg);
  6001. ctx_isp->hw_ctx = NULL;
  6002. }
  6003. cam_common_release_evt_params(ctx->dev_hdl);
  6004. memset(&ctx_isp->evt_inject_params, 0, sizeof(struct cam_hw_inject_evt_param));
  6005. ctx->session_hdl = -1;
  6006. ctx->dev_hdl = -1;
  6007. ctx->link_hdl = -1;
  6008. ctx->ctx_crm_intf = NULL;
  6009. ctx->last_flush_req = 0;
  6010. ctx_isp->frame_id = 0;
  6011. ctx_isp->active_req_cnt = 0;
  6012. ctx_isp->reported_req_id = 0;
  6013. ctx_isp->reported_frame_id = 0;
  6014. ctx_isp->hw_acquired = false;
  6015. ctx_isp->init_received = false;
  6016. ctx_isp->offline_context = false;
  6017. ctx_isp->vfps_aux_context = false;
  6018. ctx_isp->rdi_only_context = false;
  6019. ctx_isp->req_info.last_bufdone_req_id = 0;
  6020. ctx_isp->v4l2_event_sub_ids = 0;
  6021. ctx_isp->resume_hw_in_flushed = false;
  6022. atomic64_set(&ctx_isp->dbg_monitors.state_monitor_head, -1);
  6023. atomic64_set(&ctx_isp->dbg_monitors.frame_monitor_head, -1);
  6024. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  6025. atomic64_set(&ctx_isp->dbg_monitors.event_record_head[i], -1);
  6026. /*
  6027. * Ideally, we should never have any active request here.
  6028. * But we still add some sanity check code here to help the debug
  6029. */
  6030. if (!list_empty(&ctx->active_req_list))
  6031. CAM_ERR(CAM_ISP, "Active list is not empty, ctx_idx: %u, link: 0x%x",
  6032. ctx->ctx_id, ctx->link_hdl);
  6033. /* Flush all the pending request list */
  6034. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  6035. flush_req.link_hdl = ctx->link_hdl;
  6036. flush_req.dev_hdl = ctx->dev_hdl;
  6037. flush_req.req_id = 0;
  6038. CAM_DBG(CAM_ISP, "try to flush pending list, ctx_idx: %u, link: 0x%x",
  6039. ctx->ctx_id, ctx->link_hdl);
  6040. spin_lock_bh(&ctx->lock);
  6041. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  6042. spin_unlock_bh(&ctx->lock);
  6043. __cam_isp_ctx_free_mem_hw_entries(ctx);
  6044. ctx->state = CAM_CTX_AVAILABLE;
  6045. trace_cam_context_state("ISP", ctx);
  6046. CAM_DBG(CAM_ISP, "Release device success[%u] link: 0x%x next state %d",
  6047. ctx->ctx_id, ctx->link_hdl, ctx->state);
  6048. return rc;
  6049. }
  6050. static int __cam_isp_ctx_config_dev_in_top_state(
  6051. struct cam_context *ctx, struct cam_config_dev_cmd *cmd)
  6052. {
  6053. int rc = 0, i;
  6054. struct cam_ctx_request *req = NULL;
  6055. struct cam_isp_ctx_req *req_isp;
  6056. struct cam_packet *packet;
  6057. size_t remain_len = 0;
  6058. struct cam_hw_prepare_update_args cfg = {0};
  6059. struct cam_isp_prepare_hw_update_data *hw_update_data;
  6060. struct cam_req_mgr_add_request add_req;
  6061. struct cam_isp_context *ctx_isp =
  6062. (struct cam_isp_context *) ctx->ctx_priv;
  6063. struct cam_hw_cmd_args hw_cmd_args;
  6064. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  6065. uint32_t packet_opcode = 0;
  6066. CAM_DBG(CAM_ISP, "get free request object......ctx_idx: %u, link: 0x%x",
  6067. ctx->ctx_id, ctx->link_hdl);
  6068. /* get free request */
  6069. spin_lock_bh(&ctx->lock);
  6070. if (!list_empty(&ctx->free_req_list)) {
  6071. req = list_first_entry(&ctx->free_req_list,
  6072. struct cam_ctx_request, list);
  6073. list_del_init(&req->list);
  6074. }
  6075. spin_unlock_bh(&ctx->lock);
  6076. if (!req) {
  6077. CAM_ERR(CAM_ISP, "No more request obj free, ctx_idx: %u, link: 0x%x",
  6078. ctx->ctx_id, ctx->link_hdl);
  6079. return -ENOMEM;
  6080. }
  6081. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  6082. remain_len = cam_context_parse_config_cmd(ctx, cmd, &packet);
  6083. if (IS_ERR(packet)) {
  6084. rc = PTR_ERR(packet);
  6085. goto free_req;
  6086. }
  6087. /* Query the packet opcode */
  6088. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  6089. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  6090. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_GET_PACKET_OPCODE;
  6091. isp_hw_cmd_args.cmd_data = (void *)packet;
  6092. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  6093. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  6094. &hw_cmd_args);
  6095. if (rc) {
  6096. CAM_ERR(CAM_ISP, "HW command failed, ctx_idx: %u, link: 0x%x",
  6097. ctx->ctx_id, ctx->link_hdl);
  6098. goto free_req;
  6099. }
  6100. packet_opcode = isp_hw_cmd_args.u.packet_op_code;
  6101. if ((packet_opcode == CAM_ISP_PACKET_UPDATE_DEV)
  6102. && (packet->header.request_id <= ctx->last_flush_req)) {
  6103. CAM_INFO(CAM_ISP,
  6104. "request %lld has been flushed, reject packet, ctx_idx: %u, link: 0x%x",
  6105. packet->header.request_id, ctx->ctx_id, ctx->link_hdl);
  6106. rc = -EBADR;
  6107. goto free_req;
  6108. } else if ((packet_opcode == CAM_ISP_PACKET_INIT_DEV)
  6109. && (packet->header.request_id <= ctx->last_flush_req)
  6110. && ctx->last_flush_req && packet->header.request_id) {
  6111. CAM_WARN(CAM_ISP,
  6112. "last flushed req is %lld, config dev(init) for req %lld, ctx_idx: %u, link: 0x%x",
  6113. ctx->last_flush_req, packet->header.request_id, ctx->ctx_id, ctx->link_hdl);
  6114. rc = -EBADR;
  6115. goto free_req;
  6116. }
  6117. cfg.packet = packet;
  6118. cfg.remain_len = remain_len;
  6119. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  6120. cfg.max_hw_update_entries = ctx->max_hw_update_entries;
  6121. cfg.hw_update_entries = req_isp->cfg;
  6122. cfg.max_out_map_entries = ctx->max_out_map_entries;
  6123. cfg.max_in_map_entries = ctx->max_in_map_entries;
  6124. cfg.out_map_entries = req_isp->fence_map_out;
  6125. cfg.in_map_entries = req_isp->fence_map_in;
  6126. cfg.priv = &req_isp->hw_update_data;
  6127. cfg.pf_data = &(req->pf_data);
  6128. cfg.num_out_map_entries = 0;
  6129. cfg.num_in_map_entries = 0;
  6130. cfg.buf_tracker = &req->buf_tracker;
  6131. memset(&req_isp->hw_update_data, 0, sizeof(req_isp->hw_update_data));
  6132. memset(req_isp->fence_map_out, 0, sizeof(struct cam_hw_fence_map_entry)
  6133. * ctx->max_out_map_entries);
  6134. INIT_LIST_HEAD(cfg.buf_tracker);
  6135. rc = ctx->hw_mgr_intf->hw_prepare_update(
  6136. ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  6137. if (rc != 0) {
  6138. CAM_ERR(CAM_ISP, "Prepare config packet failed in HW layer, ctx: %u, link: 0x%x",
  6139. ctx->ctx_id, ctx->link_hdl);
  6140. rc = -EFAULT;
  6141. goto free_req_and_buf_tracker_list;
  6142. }
  6143. hw_update_data = cfg.priv;
  6144. req_isp->num_cfg = cfg.num_hw_update_entries;
  6145. req_isp->num_fence_map_out = cfg.num_out_map_entries;
  6146. req_isp->num_fence_map_in = cfg.num_in_map_entries;
  6147. req_isp->num_acked = 0;
  6148. req_isp->num_deferred_acks = 0;
  6149. req_isp->bubble_detected = false;
  6150. req_isp->cdm_reset_before_apply = false;
  6151. req_isp->hw_update_data.packet = packet;
  6152. req_isp->hw_update_data.num_exp = hw_update_data->num_exp;
  6153. req_isp->hw_update_data.mup_en = hw_update_data->mup_en;
  6154. req->pf_data.packet_handle = cmd->packet_handle;
  6155. req->pf_data.packet_offset = cmd->offset;
  6156. req->pf_data.req = req;
  6157. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  6158. rc = cam_sync_get_obj_ref(req_isp->fence_map_out[i].sync_id);
  6159. if (rc) {
  6160. CAM_ERR(CAM_ISP, "Can't get ref for fence %d, ctx_idx: %u, link: 0x%x",
  6161. req_isp->fence_map_out[i].sync_id, ctx->ctx_id, ctx->link_hdl);
  6162. goto put_ref;
  6163. }
  6164. }
  6165. CAM_DBG(CAM_ISP,
  6166. "packet req-id:%lld, opcode:%d, num_entry:%d, num_fence_out: %d, num_fence_in: %d, ctx: %u, link: 0x%x",
  6167. packet->header.request_id, req_isp->hw_update_data.packet_opcode_type,
  6168. req_isp->num_cfg, req_isp->num_fence_map_out, req_isp->num_fence_map_in,
  6169. ctx->ctx_id, ctx->link_hdl);
  6170. req->request_id = packet->header.request_id;
  6171. req->status = 1;
  6172. if (req_isp->hw_update_data.packet_opcode_type ==
  6173. CAM_ISP_PACKET_INIT_DEV) {
  6174. if (ctx->state < CAM_CTX_ACTIVATED) {
  6175. rc = __cam_isp_ctx_enqueue_init_request(ctx, req);
  6176. if (rc)
  6177. CAM_ERR(CAM_ISP, "Enqueue INIT pkt failed, ctx: %u, link: 0x%x",
  6178. ctx->ctx_id, ctx->link_hdl);
  6179. ctx_isp->init_received = true;
  6180. if ((ctx_isp->vfps_aux_context) && (req->request_id > 0))
  6181. ctx_isp->resume_hw_in_flushed = true;
  6182. else
  6183. ctx_isp->resume_hw_in_flushed = false;
  6184. } else {
  6185. rc = -EINVAL;
  6186. CAM_ERR(CAM_ISP, "Received INIT pkt in wrong state:%d, ctx:%u, link:0x%x",
  6187. ctx->state, ctx->ctx_id, ctx->link_hdl);
  6188. }
  6189. } else {
  6190. if ((ctx->state == CAM_CTX_FLUSHED) || (ctx->state < CAM_CTX_READY)) {
  6191. rc = -EINVAL;
  6192. CAM_ERR(CAM_ISP,
  6193. "Received update req %lld in wrong state:%d, ctx_idx: %u, link: 0x%x",
  6194. req->request_id, ctx->state, ctx->ctx_id, ctx->link_hdl);
  6195. goto put_ref;
  6196. }
  6197. if ((ctx_isp->offline_context) || (ctx_isp->vfps_aux_context)) {
  6198. __cam_isp_ctx_enqueue_request_in_order(ctx, req, true);
  6199. } else if (ctx->ctx_crm_intf->add_req) {
  6200. memset(&add_req, 0, sizeof(add_req));
  6201. add_req.link_hdl = ctx->link_hdl;
  6202. add_req.dev_hdl = ctx->dev_hdl;
  6203. add_req.req_id = req->request_id;
  6204. add_req.num_exp = ctx_isp->last_num_exp;
  6205. if (req_isp->hw_update_data.mup_en) {
  6206. add_req.num_exp = req_isp->hw_update_data.num_exp;
  6207. ctx_isp->last_num_exp = add_req.num_exp;
  6208. }
  6209. rc = ctx->ctx_crm_intf->add_req(&add_req);
  6210. if (rc) {
  6211. if (rc == -EBADR)
  6212. CAM_INFO(CAM_ISP,
  6213. "Add req failed: req id=%llu, it has been flushed on link 0x%x ctx %u",
  6214. req->request_id, ctx->link_hdl, ctx->ctx_id);
  6215. else
  6216. CAM_ERR(CAM_ISP,
  6217. "Add req failed: req id=%llu on link 0x%x ctx %u",
  6218. req->request_id, ctx->link_hdl, ctx->ctx_id);
  6219. } else {
  6220. __cam_isp_ctx_enqueue_request_in_order(ctx, req, true);
  6221. }
  6222. } else {
  6223. CAM_ERR(CAM_ISP, "Unable to add request: req id=%llu,ctx: %u,link: 0x%x",
  6224. req->request_id, ctx->ctx_id, ctx->link_hdl);
  6225. rc = -ENODEV;
  6226. }
  6227. }
  6228. if (rc)
  6229. goto put_ref;
  6230. CAM_DBG(CAM_REQ,
  6231. "Preprocessing Config req_id %lld successful on ctx %u, link: 0x%x",
  6232. req->request_id, ctx->ctx_id, ctx->link_hdl);
  6233. if (ctx_isp->offline_context && atomic_read(&ctx_isp->rxd_epoch))
  6234. __cam_isp_ctx_schedule_apply_req(ctx_isp);
  6235. else if (ctx_isp->vfps_aux_context &&
  6236. (req_isp->hw_update_data.packet_opcode_type != CAM_ISP_PACKET_INIT_DEV))
  6237. __cam_isp_ctx_schedule_apply_req(ctx_isp);
  6238. return rc;
  6239. put_ref:
  6240. for (--i; i >= 0; i--) {
  6241. if (cam_sync_put_obj_ref(req_isp->fence_map_out[i].sync_id))
  6242. CAM_ERR(CAM_CTXT, "Failed to put ref of fence %d, ctx_idx: %u, link: 0x%x",
  6243. req_isp->fence_map_out[i].sync_id, ctx->ctx_id, ctx->link_hdl);
  6244. }
  6245. free_req_and_buf_tracker_list:
  6246. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  6247. free_req:
  6248. spin_lock_bh(&ctx->lock);
  6249. list_add_tail(&req->list, &ctx->free_req_list);
  6250. spin_unlock_bh(&ctx->lock);
  6251. return rc;
  6252. }
  6253. static int __cam_isp_ctx_allocate_mem_hw_entries(
  6254. struct cam_context *ctx,
  6255. struct cam_hw_acquire_args *param)
  6256. {
  6257. int rc = 0, i;
  6258. uint32_t max_res = 0;
  6259. uint32_t max_hw_upd_entries = CAM_ISP_CTX_CFG_MAX;
  6260. struct cam_ctx_request *req;
  6261. struct cam_ctx_request *temp_req;
  6262. struct cam_isp_ctx_req *req_isp;
  6263. struct cam_isp_context *ctx_isp =
  6264. (struct cam_isp_context *) ctx->ctx_priv;
  6265. if (!param->op_params.param_list[0])
  6266. max_res = CAM_ISP_CTX_RES_MAX;
  6267. else {
  6268. max_res = param->op_params.param_list[0];
  6269. if (param->op_flags & CAM_IFE_CTX_SFE_EN) {
  6270. max_res += param->op_params.param_list[1];
  6271. max_hw_upd_entries = CAM_ISP_SFE_CTX_CFG_MAX;
  6272. }
  6273. }
  6274. ctx->max_in_map_entries = max_res;
  6275. ctx->max_out_map_entries = max_res;
  6276. ctx->max_hw_update_entries = max_hw_upd_entries;
  6277. CAM_DBG(CAM_ISP,
  6278. "Allocate max_entries: 0x%x max_res: 0x%x is_sfe_en: %d, ctx: %u, link: 0x%x",
  6279. max_hw_upd_entries, max_res, (param->op_flags & CAM_IFE_CTX_SFE_EN),
  6280. ctx->ctx_id, ctx->link_hdl);
  6281. ctx->hw_update_entry = kcalloc(CAM_ISP_CTX_REQ_MAX, sizeof(struct cam_hw_update_entry *),
  6282. GFP_KERNEL);
  6283. if (!ctx->hw_update_entry) {
  6284. CAM_ERR(CAM_CTXT, "%s[%u] no memory, link: 0x%x",
  6285. ctx->dev_name, ctx->ctx_id, ctx->link_hdl);
  6286. return -ENOMEM;
  6287. }
  6288. ctx->in_map_entries = kcalloc(CAM_ISP_CTX_REQ_MAX, sizeof(struct cam_hw_fence_map_entry *),
  6289. GFP_KERNEL);
  6290. if (!ctx->in_map_entries) {
  6291. CAM_ERR(CAM_CTXT, "%s[%u] no memory for in_map_entries, link: 0x%x",
  6292. ctx->dev_name, ctx->ctx_id, ctx->link_hdl);
  6293. rc = -ENOMEM;
  6294. goto end;
  6295. }
  6296. ctx->out_map_entries = kcalloc(CAM_ISP_CTX_REQ_MAX, sizeof(struct cam_hw_fence_map_entry *),
  6297. GFP_KERNEL);
  6298. if (!ctx->out_map_entries) {
  6299. CAM_ERR(CAM_CTXT, "%s[%u] no memory for out_map_entries, link: 0x%x",
  6300. ctx->dev_name, ctx->ctx_id, ctx->link_hdl);
  6301. rc = -ENOMEM;
  6302. goto end;
  6303. }
  6304. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  6305. ctx->hw_update_entry[i] = kcalloc(ctx->max_hw_update_entries,
  6306. sizeof(struct cam_hw_update_entry), GFP_KERNEL);
  6307. if (!ctx->hw_update_entry[i]) {
  6308. CAM_ERR(CAM_CTXT, "%s[%u] no memory for hw_update_entry: %u, link: 0x%x",
  6309. ctx->dev_name, ctx->ctx_id, i, ctx->link_hdl);
  6310. rc = -ENOMEM;
  6311. goto end;
  6312. }
  6313. ctx->in_map_entries[i] = kcalloc(ctx->max_in_map_entries,
  6314. sizeof(struct cam_hw_fence_map_entry),
  6315. GFP_KERNEL);
  6316. if (!ctx->in_map_entries[i]) {
  6317. CAM_ERR(CAM_CTXT, "%s[%u] no memory for in_map_entries: %u, link: 0x%x",
  6318. ctx->dev_name, ctx->ctx_id, i, ctx->link_hdl);
  6319. rc = -ENOMEM;
  6320. goto end;
  6321. }
  6322. ctx->out_map_entries[i] = kcalloc(ctx->max_out_map_entries,
  6323. sizeof(struct cam_hw_fence_map_entry),
  6324. GFP_KERNEL);
  6325. if (!ctx->out_map_entries[i]) {
  6326. CAM_ERR(CAM_CTXT, "%s[%u] no memory for out_map_entries: %u, link: 0x%x",
  6327. ctx->dev_name, ctx->ctx_id, i, ctx->link_hdl);
  6328. rc = -ENOMEM;
  6329. goto end;
  6330. }
  6331. ctx_isp->req_isp[i].deferred_fence_map_index = kcalloc(param->total_ports_acq,
  6332. sizeof(uint32_t), GFP_KERNEL);
  6333. if (!ctx_isp->req_isp[i].deferred_fence_map_index) {
  6334. CAM_ERR(CAM_ISP, "%s[%d] no memory for defer fence map idx arr, ports:%u",
  6335. ctx->dev_name, ctx->ctx_id, param->total_ports_acq);
  6336. rc = -ENOMEM;
  6337. goto end;
  6338. }
  6339. }
  6340. list_for_each_entry_safe(req, temp_req,
  6341. &ctx->free_req_list, list) {
  6342. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  6343. req_isp->cfg = ctx->hw_update_entry[req->index];
  6344. req_isp->fence_map_in = ctx->in_map_entries[req->index];
  6345. req_isp->fence_map_out = ctx->out_map_entries[req->index];
  6346. }
  6347. return rc;
  6348. end:
  6349. __cam_isp_ctx_free_mem_hw_entries(ctx);
  6350. return rc;
  6351. }
  6352. static int __cam_isp_ctx_acquire_dev_in_available(struct cam_context *ctx,
  6353. struct cam_acquire_dev_cmd *cmd)
  6354. {
  6355. int rc = 0;
  6356. int i;
  6357. struct cam_hw_acquire_args param;
  6358. struct cam_isp_resource *isp_res = NULL;
  6359. struct cam_create_dev_hdl req_hdl_param;
  6360. struct cam_hw_release_args release;
  6361. struct cam_isp_context *ctx_isp =
  6362. (struct cam_isp_context *) ctx->ctx_priv;
  6363. struct cam_hw_cmd_args hw_cmd_args;
  6364. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  6365. if (!ctx->hw_mgr_intf) {
  6366. CAM_ERR(CAM_ISP, "HW interface is not ready, ctx_idx: %u, link: 0x%x",
  6367. ctx->ctx_id, ctx->link_hdl);
  6368. rc = -EFAULT;
  6369. goto end;
  6370. }
  6371. CAM_DBG(CAM_ISP,
  6372. "session_hdl 0x%x, num_resources %d, hdl type %d, res %lld, ctx_idx: %u, link: 0x%x",
  6373. cmd->session_handle, cmd->num_resources,
  6374. cmd->handle_type, cmd->resource_hdl, ctx->ctx_id, ctx->link_hdl);
  6375. ctx_isp->v4l2_event_sub_ids = cam_req_mgr_get_id_subscribed();
  6376. if (cmd->num_resources == CAM_API_COMPAT_CONSTANT) {
  6377. ctx_isp->split_acquire = true;
  6378. CAM_DBG(CAM_ISP, "Acquire dev handle, ctx_idx: %u, link: 0x%x",
  6379. ctx->ctx_id, ctx->link_hdl);
  6380. goto get_dev_handle;
  6381. }
  6382. if (cmd->num_resources > CAM_ISP_CTX_RES_MAX) {
  6383. CAM_ERR(CAM_ISP, "Too much resources in the acquire, ctx_idx: %u, link: 0x%x",
  6384. ctx->ctx_id, ctx->link_hdl);
  6385. rc = -ENOMEM;
  6386. goto end;
  6387. }
  6388. /* for now we only support user pointer */
  6389. if (cmd->handle_type != 1) {
  6390. CAM_ERR(CAM_ISP, "Only user pointer is supported, ctx_idx: %u, link: 0x%x",
  6391. ctx->ctx_id, ctx->link_hdl);
  6392. rc = -EINVAL;
  6393. goto end;
  6394. }
  6395. isp_res = kzalloc(
  6396. sizeof(*isp_res)*cmd->num_resources, GFP_KERNEL);
  6397. if (!isp_res) {
  6398. rc = -ENOMEM;
  6399. goto end;
  6400. }
  6401. CAM_DBG(CAM_ISP, "start copy %d resources from user, ctx_idx: %u, link: 0x%x",
  6402. cmd->num_resources, ctx->ctx_id, ctx->link_hdl);
  6403. if (copy_from_user(isp_res, u64_to_user_ptr(cmd->resource_hdl),
  6404. sizeof(*isp_res)*cmd->num_resources)) {
  6405. rc = -EFAULT;
  6406. goto free_res;
  6407. }
  6408. memset(&param, 0, sizeof(param));
  6409. param.context_data = ctx;
  6410. param.event_cb = ctx->irq_cb_intf;
  6411. param.sec_pf_evt_cb = cam_context_dump_pf_info;
  6412. param.num_acq = cmd->num_resources;
  6413. param.acquire_info = (uintptr_t) isp_res;
  6414. /* call HW manager to reserve the resource */
  6415. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  6416. &param);
  6417. if (rc != 0) {
  6418. CAM_ERR(CAM_ISP, "Acquire device failed, ctx_idx: %u, link: 0x%x",
  6419. ctx->ctx_id, ctx->link_hdl);
  6420. goto free_res;
  6421. }
  6422. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx, &param);
  6423. if (rc) {
  6424. CAM_ERR(CAM_ISP, "Ctx[%u] link: 0x%x allocate hw entry fail",
  6425. ctx->ctx_id, ctx->link_hdl);
  6426. goto free_res;
  6427. }
  6428. /* Query the context has rdi only resource */
  6429. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  6430. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  6431. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  6432. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  6433. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  6434. &hw_cmd_args);
  6435. if (rc) {
  6436. CAM_ERR(CAM_ISP, "HW command failed, ctx_idx: %u, link: 0x%x",
  6437. ctx->ctx_id, ctx->link_hdl);
  6438. goto free_hw;
  6439. }
  6440. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  6441. /*
  6442. * this context has rdi only resource assign rdi only
  6443. * state machine
  6444. */
  6445. CAM_DBG(CAM_ISP, "RDI only session Context, ctx_idx: %u, link: 0x%x",
  6446. ctx->ctx_id, ctx->link_hdl);
  6447. ctx_isp->substate_machine_irq =
  6448. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  6449. ctx_isp->substate_machine =
  6450. cam_isp_ctx_rdi_only_activated_state_machine;
  6451. ctx_isp->rdi_only_context = true;
  6452. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  6453. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI, ctx_idx: %u, link: 0x%x",
  6454. ctx->ctx_id, ctx->link_hdl);
  6455. ctx_isp->substate_machine_irq =
  6456. cam_isp_ctx_fs2_state_machine_irq;
  6457. ctx_isp->substate_machine =
  6458. cam_isp_ctx_fs2_state_machine;
  6459. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  6460. CAM_DBG(CAM_ISP,
  6461. "offline Session has PIX and RD resources, ctx_idx: %u, link: 0x%x",
  6462. ctx->ctx_id, ctx->link_hdl);
  6463. ctx_isp->substate_machine_irq =
  6464. cam_isp_ctx_offline_state_machine_irq;
  6465. } else {
  6466. CAM_DBG(CAM_ISP,
  6467. "Session has PIX or PIX and RDI resources, ctx_idx: %u, link: 0x%x",
  6468. ctx->ctx_id, ctx->link_hdl);
  6469. ctx_isp->substate_machine_irq =
  6470. cam_isp_ctx_activated_state_machine_irq;
  6471. ctx_isp->substate_machine =
  6472. cam_isp_ctx_activated_state_machine;
  6473. }
  6474. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  6475. ctx_isp->hw_acquired = true;
  6476. ctx_isp->split_acquire = false;
  6477. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  6478. atomic64_set(&ctx_isp->dbg_monitors.state_monitor_head, -1);
  6479. atomic64_set(&ctx_isp->dbg_monitors.frame_monitor_head, -1);
  6480. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  6481. atomic64_set(&ctx_isp->dbg_monitors.event_record_head[i], -1);
  6482. CAM_INFO(CAM_ISP, "Ctx_type: %u, ctx_id: %u, hw_mgr_ctx: %u", isp_hw_cmd_args.u.ctx_type,
  6483. ctx->ctx_id, param.hw_mgr_ctx_id);
  6484. kfree(isp_res);
  6485. isp_res = NULL;
  6486. get_dev_handle:
  6487. req_hdl_param.session_hdl = cmd->session_handle;
  6488. /* bridge is not ready for these flags. so false for now */
  6489. req_hdl_param.v4l2_sub_dev_flag = 0;
  6490. req_hdl_param.media_entity_flag = 0;
  6491. req_hdl_param.ops = ctx->crm_ctx_intf;
  6492. req_hdl_param.priv = ctx;
  6493. req_hdl_param.dev_id = CAM_ISP;
  6494. CAM_DBG(CAM_ISP, "get device handle form bridge, ctx_idx: %u, link: 0x%x",
  6495. ctx->ctx_id, ctx->link_hdl);
  6496. ctx->dev_hdl = cam_create_device_hdl(&req_hdl_param);
  6497. if (ctx->dev_hdl <= 0) {
  6498. rc = -EFAULT;
  6499. CAM_ERR(CAM_ISP, "Can not create device handle, ctx_idx: %u, link: 0x%x",
  6500. ctx->ctx_id, ctx->link_hdl);
  6501. goto free_hw;
  6502. }
  6503. cmd->dev_handle = ctx->dev_hdl;
  6504. /* store session information */
  6505. ctx->session_hdl = cmd->session_handle;
  6506. ctx->state = CAM_CTX_ACQUIRED;
  6507. trace_cam_context_state("ISP", ctx);
  6508. CAM_INFO(CAM_ISP,
  6509. "Acquire success: session_hdl 0x%x num_rsrces %d ctx %u link: 0x%x",
  6510. cmd->session_handle, cmd->num_resources, ctx->ctx_id, ctx->link_hdl);
  6511. return rc;
  6512. free_hw:
  6513. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  6514. if (ctx_isp->hw_acquired)
  6515. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  6516. &release);
  6517. ctx_isp->hw_ctx = NULL;
  6518. ctx_isp->hw_acquired = false;
  6519. free_res:
  6520. kfree(isp_res);
  6521. end:
  6522. return rc;
  6523. }
  6524. static int __cam_isp_ctx_acquire_hw_v1(struct cam_context *ctx,
  6525. void *args)
  6526. {
  6527. int rc = 0;
  6528. int i;
  6529. struct cam_acquire_hw_cmd_v1 *cmd =
  6530. (struct cam_acquire_hw_cmd_v1 *)args;
  6531. struct cam_hw_acquire_args param;
  6532. struct cam_hw_release_args release;
  6533. struct cam_isp_context *ctx_isp =
  6534. (struct cam_isp_context *) ctx->ctx_priv;
  6535. struct cam_hw_cmd_args hw_cmd_args;
  6536. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  6537. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  6538. struct cam_isp_comp_record_query query_cmd;
  6539. if (!ctx->hw_mgr_intf) {
  6540. CAM_ERR(CAM_ISP, "HW interface is not ready, ctx %u link: 0x%x",
  6541. ctx->ctx_id, ctx->link_hdl);
  6542. rc = -EFAULT;
  6543. goto end;
  6544. }
  6545. CAM_DBG(CAM_ISP,
  6546. "session_hdl 0x%x, hdl type %d, res %lld ctx %u link: 0x%x",
  6547. cmd->session_handle, cmd->handle_type, cmd->resource_hdl,
  6548. ctx->ctx_id, ctx->link_hdl);
  6549. /* for now we only support user pointer */
  6550. if (cmd->handle_type != 1) {
  6551. CAM_ERR(CAM_ISP, "Only user pointer is supported, ctx %u link: 0x%x",
  6552. ctx->ctx_id, ctx->link_hdl);
  6553. rc = -EINVAL;
  6554. goto end;
  6555. }
  6556. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  6557. CAM_ERR(CAM_ISP, "data_size is not a valid value, ctx %u link: 0x%x",
  6558. ctx->ctx_id, ctx->link_hdl);
  6559. goto end;
  6560. }
  6561. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  6562. if (!acquire_hw_info) {
  6563. rc = -ENOMEM;
  6564. goto end;
  6565. }
  6566. CAM_DBG(CAM_ISP, "start copy resources from user, ctx_idx: %u, link: 0x%x",
  6567. ctx->ctx_id, ctx->link_hdl);
  6568. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  6569. cmd->data_size)) {
  6570. rc = -EFAULT;
  6571. goto free_res;
  6572. }
  6573. memset(&param, 0, sizeof(param));
  6574. param.context_data = ctx;
  6575. param.event_cb = ctx->irq_cb_intf;
  6576. param.sec_pf_evt_cb = cam_context_dump_pf_info;
  6577. param.num_acq = CAM_API_COMPAT_CONSTANT;
  6578. param.acquire_info_size = cmd->data_size;
  6579. param.acquire_info = (uint64_t) acquire_hw_info;
  6580. param.mini_dump_cb = __cam_isp_ctx_minidump_cb;
  6581. param.link_hdl = ctx->link_hdl;
  6582. /* call HW manager to reserve the resource */
  6583. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  6584. &param);
  6585. if (rc != 0) {
  6586. CAM_ERR(CAM_ISP, "Acquire device failed, ctx_idx: %u, link: 0x%x",
  6587. ctx->ctx_id, ctx->link_hdl);
  6588. goto free_res;
  6589. }
  6590. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx,
  6591. &param);
  6592. if (rc) {
  6593. CAM_ERR(CAM_ISP, "Ctx[%u] link: 0x%x allocate hw entry fail",
  6594. ctx->ctx_id, ctx->link_hdl);
  6595. goto free_res;
  6596. }
  6597. ctx_isp->last_num_exp = 0;
  6598. ctx_isp->support_consumed_addr =
  6599. (param.op_flags & CAM_IFE_CTX_CONSUME_ADDR_EN);
  6600. ctx_isp->is_tfe_shdr = (param.op_flags & CAM_IFE_CTX_SHDR_EN);
  6601. ctx_isp->is_shdr_master = (param.op_flags & CAM_IFE_CTX_SHDR_IS_MASTER);
  6602. /* Query the context bus comp group information */
  6603. ctx_isp->vfe_bus_comp_grp = kcalloc(CAM_IFE_BUS_COMP_NUM_MAX,
  6604. sizeof(struct cam_isp_context_comp_record), GFP_KERNEL);
  6605. if (!ctx_isp->vfe_bus_comp_grp) {
  6606. CAM_ERR(CAM_CTXT, "%s[%d] no memory for vfe_bus_comp_grp",
  6607. ctx->dev_name, ctx->ctx_id);
  6608. rc = -ENOMEM;
  6609. goto free_hw;
  6610. }
  6611. query_cmd.vfe_bus_comp_grp = ctx_isp->vfe_bus_comp_grp;
  6612. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  6613. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  6614. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_GET_BUS_COMP_GROUP;
  6615. isp_hw_cmd_args.cmd_data = &query_cmd;
  6616. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  6617. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  6618. &hw_cmd_args);
  6619. if (rc) {
  6620. CAM_ERR(CAM_ISP, "Bus Comp HW command failed, ctx_idx: %u, link: 0x%x",
  6621. ctx->ctx_id, ctx->link_hdl);
  6622. goto free_hw;
  6623. }
  6624. /* Query the context has rdi only resource */
  6625. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  6626. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  6627. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  6628. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  6629. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  6630. &hw_cmd_args);
  6631. if (rc) {
  6632. CAM_ERR(CAM_ISP, "HW command failed, ctx_idx: %u, link: 0x%x",
  6633. ctx->ctx_id, ctx->link_hdl);
  6634. goto free_hw;
  6635. }
  6636. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  6637. /*
  6638. * this context has rdi only resource assign rdi only
  6639. * state machine
  6640. */
  6641. CAM_DBG(CAM_ISP, "RDI only session Context, ctx_idx: %u, link: 0x%x",
  6642. ctx->ctx_id, ctx->link_hdl);
  6643. ctx_isp->substate_machine_irq =
  6644. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  6645. ctx_isp->substate_machine =
  6646. cam_isp_ctx_rdi_only_activated_state_machine;
  6647. ctx_isp->rdi_only_context = true;
  6648. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  6649. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI, ctx_idx: %u, link: 0x%x",
  6650. ctx->ctx_id, ctx->link_hdl);
  6651. ctx_isp->substate_machine_irq =
  6652. cam_isp_ctx_fs2_state_machine_irq;
  6653. ctx_isp->substate_machine =
  6654. cam_isp_ctx_fs2_state_machine;
  6655. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  6656. CAM_DBG(CAM_ISP, "Offline session has PIX and RD resources, ctx: %u, link: 0x%x",
  6657. ctx->ctx_id, ctx->link_hdl);
  6658. ctx_isp->substate_machine_irq =
  6659. cam_isp_ctx_offline_state_machine_irq;
  6660. ctx_isp->substate_machine = NULL;
  6661. } else {
  6662. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources, ctx: %u, link: 0x%x",
  6663. ctx->ctx_id, ctx->link_hdl);
  6664. ctx_isp->substate_machine_irq =
  6665. cam_isp_ctx_activated_state_machine_irq;
  6666. ctx_isp->substate_machine =
  6667. cam_isp_ctx_activated_state_machine;
  6668. }
  6669. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  6670. ctx_isp->hw_acquired = true;
  6671. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  6672. atomic64_set(&ctx_isp->dbg_monitors.state_monitor_head, -1);
  6673. atomic64_set(&ctx_isp->dbg_monitors.frame_monitor_head, -1);
  6674. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  6675. atomic64_set(&ctx_isp->dbg_monitors.event_record_head[i], -1);
  6676. trace_cam_context_state("ISP", ctx);
  6677. CAM_INFO(CAM_ISP,
  6678. "Acquire success:session_hdl 0x%xs ctx_type %d ctx %u link: 0x%x hw_mgr_ctx: %u is_shdr %d is_shdr_master %d",
  6679. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id, ctx->link_hdl,
  6680. param.hw_mgr_ctx_id, ctx_isp->is_tfe_shdr, ctx_isp->is_shdr_master);
  6681. kfree(acquire_hw_info);
  6682. return rc;
  6683. free_hw:
  6684. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  6685. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  6686. ctx_isp->hw_ctx = NULL;
  6687. ctx_isp->hw_acquired = false;
  6688. free_res:
  6689. kfree(acquire_hw_info);
  6690. end:
  6691. return rc;
  6692. }
  6693. static void cam_req_mgr_process_workq_apply_req_worker(struct work_struct *w)
  6694. {
  6695. cam_req_mgr_process_workq(w);
  6696. }
  6697. static int __cam_isp_ctx_acquire_hw_v2(struct cam_context *ctx,
  6698. void *args)
  6699. {
  6700. int rc = 0, i, j;
  6701. struct cam_acquire_hw_cmd_v2 *cmd =
  6702. (struct cam_acquire_hw_cmd_v2 *)args;
  6703. struct cam_hw_acquire_args param;
  6704. struct cam_hw_release_args release;
  6705. struct cam_isp_context *ctx_isp =
  6706. (struct cam_isp_context *) ctx->ctx_priv;
  6707. struct cam_hw_cmd_args hw_cmd_args;
  6708. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  6709. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  6710. struct cam_isp_comp_record_query query_cmd;
  6711. if (!ctx->hw_mgr_intf) {
  6712. CAM_ERR(CAM_ISP, "HW interface is not ready, ctx_id %u link: 0x%x",
  6713. ctx->ctx_id, ctx->link_hdl);
  6714. rc = -EFAULT;
  6715. goto end;
  6716. }
  6717. CAM_DBG(CAM_ISP,
  6718. "session_hdl 0x%x, hdl type %d, res %lld, ctx_id %u link: 0x%x",
  6719. cmd->session_handle, cmd->handle_type, cmd->resource_hdl,
  6720. ctx->ctx_id, ctx->link_hdl);
  6721. /* for now we only support user pointer */
  6722. if (cmd->handle_type != 1) {
  6723. CAM_ERR(CAM_ISP, "Only user pointer is supported, ctx_id %u link: 0x%x",
  6724. ctx->ctx_id, ctx->link_hdl);
  6725. rc = -EINVAL;
  6726. goto end;
  6727. }
  6728. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  6729. CAM_ERR(CAM_ISP, "data_size is not a valid value, ctx_id %u link: 0x%x",
  6730. ctx->ctx_id, ctx->link_hdl);
  6731. goto end;
  6732. }
  6733. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  6734. if (!acquire_hw_info) {
  6735. rc = -ENOMEM;
  6736. goto end;
  6737. }
  6738. CAM_DBG(CAM_ISP, "start copy resources from user, ctx_id %u link: 0x%x",
  6739. ctx->ctx_id, ctx->link_hdl);
  6740. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  6741. cmd->data_size)) {
  6742. rc = -EFAULT;
  6743. goto free_res;
  6744. }
  6745. memset(&param, 0, sizeof(param));
  6746. param.context_data = ctx;
  6747. param.event_cb = ctx->irq_cb_intf;
  6748. param.sec_pf_evt_cb = cam_context_dump_pf_info;
  6749. param.num_acq = CAM_API_COMPAT_CONSTANT;
  6750. param.acquire_info_size = cmd->data_size;
  6751. param.acquire_info = (uint64_t) acquire_hw_info;
  6752. param.mini_dump_cb = __cam_isp_ctx_minidump_cb;
  6753. param.link_hdl = ctx->link_hdl;
  6754. /* call HW manager to reserve the resource */
  6755. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  6756. &param);
  6757. if (rc != 0) {
  6758. CAM_ERR(CAM_ISP, "Acquire device failed, ctx_id %u link: 0x%x",
  6759. ctx->ctx_id, ctx->link_hdl);
  6760. goto free_res;
  6761. }
  6762. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx, &param);
  6763. if (rc) {
  6764. CAM_ERR(CAM_ISP, "Ctx[%u] link: 0x%x allocate hw entry fail",
  6765. ctx->ctx_id, ctx->link_hdl);
  6766. goto free_hw;
  6767. }
  6768. /*
  6769. * Set feature flag if applicable
  6770. * custom hw is supported only on v2
  6771. */
  6772. ctx_isp->last_num_exp = 0;
  6773. ctx_isp->custom_enabled =
  6774. (param.op_flags & CAM_IFE_CTX_CUSTOM_EN);
  6775. ctx_isp->use_frame_header_ts =
  6776. (param.op_flags & CAM_IFE_CTX_FRAME_HEADER_EN);
  6777. ctx_isp->use_default_apply =
  6778. (param.op_flags & CAM_IFE_CTX_APPLY_DEFAULT_CFG);
  6779. ctx_isp->support_consumed_addr =
  6780. (param.op_flags & CAM_IFE_CTX_CONSUME_ADDR_EN);
  6781. ctx_isp->aeb_enabled =
  6782. (param.op_flags & CAM_IFE_CTX_AEB_EN);
  6783. ctx_isp->mode_switch_en =
  6784. (param.op_flags & CAM_IFE_CTX_DYNAMIC_SWITCH_EN);
  6785. ctx_isp->is_tfe_shdr = (param.op_flags & CAM_IFE_CTX_SHDR_EN);
  6786. ctx_isp->is_shdr_master = (param.op_flags & CAM_IFE_CTX_SHDR_IS_MASTER);
  6787. /* Query the context bus comp group information */
  6788. ctx_isp->vfe_bus_comp_grp = kcalloc(CAM_IFE_BUS_COMP_NUM_MAX,
  6789. sizeof(struct cam_isp_context_comp_record), GFP_KERNEL);
  6790. if (!ctx_isp->vfe_bus_comp_grp) {
  6791. CAM_ERR(CAM_CTXT, "%s[%d] no memory for vfe_bus_comp_grp",
  6792. ctx->dev_name, ctx->ctx_id);
  6793. rc = -ENOMEM;
  6794. goto end;
  6795. }
  6796. if (param.op_flags & CAM_IFE_CTX_SFE_EN) {
  6797. ctx_isp->sfe_bus_comp_grp = kcalloc(CAM_SFE_BUS_COMP_NUM_MAX,
  6798. sizeof(struct cam_isp_context_comp_record), GFP_KERNEL);
  6799. if (!ctx_isp->sfe_bus_comp_grp) {
  6800. CAM_ERR(CAM_CTXT, "%s[%d] no memory for sfe_bus_comp_grp",
  6801. ctx->dev_name, ctx->ctx_id);
  6802. rc = -ENOMEM;
  6803. goto end;
  6804. }
  6805. }
  6806. query_cmd.vfe_bus_comp_grp = ctx_isp->vfe_bus_comp_grp;
  6807. if (ctx_isp->sfe_bus_comp_grp)
  6808. query_cmd.sfe_bus_comp_grp = ctx_isp->sfe_bus_comp_grp;
  6809. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  6810. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  6811. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_GET_BUS_COMP_GROUP;
  6812. isp_hw_cmd_args.cmd_data = &query_cmd;
  6813. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  6814. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  6815. &hw_cmd_args);
  6816. if (rc) {
  6817. CAM_ERR(CAM_ISP, "HW command failed");
  6818. goto free_hw;
  6819. }
  6820. /* Query the context has rdi only resource */
  6821. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  6822. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  6823. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  6824. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  6825. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  6826. &hw_cmd_args);
  6827. if (rc) {
  6828. CAM_ERR(CAM_ISP, "HW command failed, ctx_id %u link: 0x%x",
  6829. ctx->ctx_id, ctx->link_hdl);
  6830. goto free_hw;
  6831. }
  6832. if (param.valid_acquired_hw) {
  6833. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  6834. cmd->hw_info.acquired_hw_id[i] =
  6835. param.acquired_hw_id[i];
  6836. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  6837. for (j = 0; j < CAM_MAX_HW_SPLIT; j++)
  6838. cmd->hw_info.acquired_hw_path[i][j] =
  6839. param.acquired_hw_path[i][j];
  6840. ctx_isp->hw_idx = param.acquired_hw_id[0];
  6841. }
  6842. cmd->hw_info.valid_acquired_hw =
  6843. param.valid_acquired_hw;
  6844. cmd->hw_info.valid_acquired_hw = param.valid_acquired_hw;
  6845. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  6846. /*
  6847. * this context has rdi only resource assign rdi only
  6848. * state machine
  6849. */
  6850. CAM_DBG(CAM_ISP, "RDI only session Context, ctx_id %u link: 0x%x",
  6851. ctx->ctx_id, ctx->link_hdl);
  6852. ctx_isp->substate_machine_irq =
  6853. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  6854. ctx_isp->substate_machine =
  6855. cam_isp_ctx_rdi_only_activated_state_machine;
  6856. ctx_isp->rdi_only_context = true;
  6857. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  6858. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI, ctx_id %u link: 0x%x",
  6859. ctx->ctx_id, ctx->link_hdl);
  6860. ctx_isp->substate_machine_irq =
  6861. cam_isp_ctx_fs2_state_machine_irq;
  6862. ctx_isp->substate_machine =
  6863. cam_isp_ctx_fs2_state_machine;
  6864. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  6865. CAM_DBG(CAM_ISP, "Offline Session has PIX and RD resources, ctx_id %u link: 0x%x",
  6866. ctx->ctx_id, ctx->link_hdl);
  6867. ctx_isp->substate_machine_irq =
  6868. cam_isp_ctx_offline_state_machine_irq;
  6869. ctx_isp->substate_machine = NULL;
  6870. ctx_isp->offline_context = true;
  6871. } else {
  6872. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources, ctx_id %u link: 0x%x",
  6873. ctx->ctx_id, ctx->link_hdl);
  6874. ctx_isp->substate_machine_irq =
  6875. cam_isp_ctx_activated_state_machine_irq;
  6876. ctx_isp->substate_machine =
  6877. cam_isp_ctx_activated_state_machine;
  6878. }
  6879. if (ctx_isp->offline_context || ctx_isp->vfps_aux_context) {
  6880. rc = cam_req_mgr_workq_create("ife_apply_req", 20,
  6881. &ctx_isp->workq, CRM_WORKQ_USAGE_IRQ, 0,
  6882. cam_req_mgr_process_workq_apply_req_worker);
  6883. if (rc)
  6884. CAM_ERR(CAM_ISP,
  6885. "Failed to create workq for IFE rc:%d offline: %s vfps: %s ctx_id %u link: 0x%x",
  6886. rc, CAM_BOOL_TO_YESNO(ctx_isp->offline_context),
  6887. CAM_BOOL_TO_YESNO(ctx_isp->vfps_aux_context),
  6888. ctx->ctx_id, ctx->link_hdl);
  6889. }
  6890. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  6891. ctx_isp->hw_acquired = true;
  6892. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  6893. ctx->hw_mgr_ctx_id = param.hw_mgr_ctx_id;
  6894. snprintf(ctx->ctx_id_string, sizeof(ctx->ctx_id_string),
  6895. "%s_ctx[%d]_hwmgrctx[%d]_hwidx[0x%x]",
  6896. ctx->dev_name,
  6897. ctx->ctx_id,
  6898. ctx->hw_mgr_ctx_id,
  6899. ctx_isp->hw_idx);
  6900. trace_cam_context_state("ISP", ctx);
  6901. CAM_INFO(CAM_ISP,
  6902. "Acquire success: session_hdl 0x%xs ctx_type %d ctx %u link 0x%x hw_mgr_ctx %u is_shdr %d is_shdr_master %d",
  6903. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id, ctx->link_hdl,
  6904. param.hw_mgr_ctx_id, ctx_isp->is_tfe_shdr, ctx_isp->is_shdr_master);
  6905. kfree(acquire_hw_info);
  6906. return rc;
  6907. free_hw:
  6908. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  6909. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  6910. ctx_isp->hw_ctx = NULL;
  6911. ctx_isp->hw_acquired = false;
  6912. free_res:
  6913. kfree(acquire_hw_info);
  6914. end:
  6915. return rc;
  6916. }
  6917. static int __cam_isp_ctx_acquire_hw_in_acquired(struct cam_context *ctx,
  6918. void *args)
  6919. {
  6920. int rc = -EINVAL;
  6921. uint32_t api_version;
  6922. if (!ctx || !args) {
  6923. CAM_ERR(CAM_ISP, "Invalid input pointer");
  6924. return rc;
  6925. }
  6926. api_version = *((uint32_t *)args);
  6927. if (api_version == 1)
  6928. rc = __cam_isp_ctx_acquire_hw_v1(ctx, args);
  6929. else if (api_version == 2)
  6930. rc = __cam_isp_ctx_acquire_hw_v2(ctx, args);
  6931. else
  6932. CAM_ERR(CAM_ISP, "Unsupported api version %d, ctx_id %u link: 0x%x",
  6933. api_version, ctx->ctx_id, ctx->link_hdl);
  6934. return rc;
  6935. }
  6936. static int __cam_isp_ctx_config_dev_in_acquired(struct cam_context *ctx,
  6937. struct cam_config_dev_cmd *cmd)
  6938. {
  6939. int rc = 0;
  6940. struct cam_isp_context *ctx_isp =
  6941. (struct cam_isp_context *) ctx->ctx_priv;
  6942. if (!ctx_isp->hw_acquired) {
  6943. CAM_ERR(CAM_ISP, "HW is not acquired, reject packet, ctx_id %u link: 0x%x",
  6944. ctx->ctx_id, ctx->link_hdl);
  6945. return -EINVAL;
  6946. }
  6947. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  6948. if (!rc && ((ctx->link_hdl >= 0) || ctx_isp->offline_context)) {
  6949. ctx->state = CAM_CTX_READY;
  6950. trace_cam_context_state("ISP", ctx);
  6951. }
  6952. CAM_DBG(CAM_ISP, "next state %d, ctx %u link: 0x%x",
  6953. ctx->state, ctx->ctx_id, ctx->link_hdl);
  6954. return rc;
  6955. }
  6956. static int __cam_isp_ctx_config_dev_in_flushed(struct cam_context *ctx,
  6957. struct cam_config_dev_cmd *cmd)
  6958. {
  6959. int rc = 0;
  6960. struct cam_start_stop_dev_cmd start_cmd;
  6961. struct cam_hw_cmd_args hw_cmd_args;
  6962. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  6963. struct cam_isp_context *ctx_isp =
  6964. (struct cam_isp_context *) ctx->ctx_priv;
  6965. if (!ctx_isp->hw_acquired) {
  6966. CAM_ERR(CAM_ISP, "HW is not acquired, reject packet, ctx_id %u link: 0x%x",
  6967. ctx->ctx_id, ctx->link_hdl);
  6968. rc = -EINVAL;
  6969. goto end;
  6970. }
  6971. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  6972. if (rc)
  6973. goto end;
  6974. if (!ctx_isp->init_received) {
  6975. CAM_WARN(CAM_ISP,
  6976. "Received update pckt in flushed state, skip start, ctx %u link: 0x%x",
  6977. ctx->ctx_id, ctx->link_hdl);
  6978. goto end;
  6979. }
  6980. CAM_DBG(CAM_ISP, "vfps_ctx:%s resume_hw_in_flushed:%d ctx:%u link: 0x%x",
  6981. CAM_BOOL_TO_YESNO(ctx_isp->vfps_aux_context),
  6982. ctx_isp->resume_hw_in_flushed,
  6983. ctx->ctx_id, ctx->link_hdl);
  6984. if (ctx_isp->vfps_aux_context) {
  6985. /* Resume the HW only when we get first valid req */
  6986. if (!ctx_isp->resume_hw_in_flushed)
  6987. goto end;
  6988. else
  6989. ctx_isp->resume_hw_in_flushed = false;
  6990. }
  6991. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  6992. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  6993. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  6994. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  6995. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  6996. &hw_cmd_args);
  6997. if (rc) {
  6998. CAM_ERR(CAM_ISP, "Failed to resume HW rc: %d, ctx_id %u link: 0x%x",
  6999. rc, ctx->ctx_id, ctx->link_hdl);
  7000. goto end;
  7001. }
  7002. start_cmd.dev_handle = cmd->dev_handle;
  7003. start_cmd.session_handle = cmd->session_handle;
  7004. rc = __cam_isp_ctx_start_dev_in_ready(ctx, &start_cmd);
  7005. if (rc)
  7006. CAM_ERR(CAM_ISP,
  7007. "Failed to re-start HW after flush rc: %d, ctx_id %u link: 0x%x",
  7008. rc, ctx->ctx_id, ctx->link_hdl);
  7009. else
  7010. CAM_INFO(CAM_ISP,
  7011. "Received init after flush. Re-start HW complete in ctx:%d, link: 0x%x",
  7012. ctx->ctx_id, ctx->link_hdl);
  7013. end:
  7014. CAM_DBG(CAM_ISP, "next state %d sub_state:%d ctx_id %u link: 0x%x", ctx->state,
  7015. ctx_isp->substate_activated, ctx->ctx_id, ctx->link_hdl);
  7016. return rc;
  7017. }
  7018. static int __cam_isp_ctx_link_in_acquired(struct cam_context *ctx,
  7019. struct cam_req_mgr_core_dev_link_setup *link)
  7020. {
  7021. int rc = 0;
  7022. struct cam_isp_context *ctx_isp =
  7023. (struct cam_isp_context *) ctx->ctx_priv;
  7024. if (!link) {
  7025. CAM_ERR(CAM_ISP, "setup link info is null: %pK ctx: %u link: 0x%x",
  7026. link, ctx->ctx_id, ctx->link_hdl);
  7027. return -EINVAL;
  7028. }
  7029. if (!link->crm_cb) {
  7030. CAM_ERR(CAM_ISP, "crm cb is null: %pK ctx: %u, link: 0x%x",
  7031. link->crm_cb, ctx->ctx_id, ctx->link_hdl);
  7032. return -EINVAL;
  7033. }
  7034. CAM_DBG(CAM_ISP, "Enter.........ctx: %u, link: 0x%x", ctx->ctx_id, ctx->link_hdl);
  7035. ctx->link_hdl = link->link_hdl;
  7036. ctx->ctx_crm_intf = link->crm_cb;
  7037. ctx_isp->subscribe_event =
  7038. CAM_TRIGGER_POINT_SOF | CAM_TRIGGER_POINT_EOF;
  7039. ctx_isp->trigger_id = link->trigger_id;
  7040. ctx_isp->mswitch_default_apply_delay_max_cnt = 0;
  7041. atomic_set(&ctx_isp->mswitch_default_apply_delay_ref_cnt, 0);
  7042. if ((link->mode_switch_max_delay - CAM_MODESWITCH_DELAY_1) > 0) {
  7043. ctx_isp->handle_mswitch = true;
  7044. ctx_isp->mswitch_default_apply_delay_max_cnt =
  7045. link->mode_switch_max_delay - CAM_MODESWITCH_DELAY_1;
  7046. CAM_DBG(CAM_ISP,
  7047. "Enabled mode switch handling on ctx: %u max delay cnt: %u",
  7048. ctx->ctx_id, ctx_isp->mswitch_default_apply_delay_max_cnt);
  7049. atomic_set(&ctx_isp->mswitch_default_apply_delay_ref_cnt,
  7050. ctx_isp->mswitch_default_apply_delay_max_cnt);
  7051. }
  7052. /* change state only if we had the init config */
  7053. if (ctx_isp->init_received) {
  7054. ctx->state = CAM_CTX_READY;
  7055. trace_cam_context_state("ISP", ctx);
  7056. }
  7057. CAM_DBG(CAM_ISP, "next state %d, ctx: %u, link: 0x%x",
  7058. ctx->state, ctx->ctx_id, ctx->link_hdl);
  7059. return rc;
  7060. }
  7061. static int __cam_isp_ctx_unlink_in_acquired(struct cam_context *ctx,
  7062. struct cam_req_mgr_core_dev_link_setup *unlink)
  7063. {
  7064. int rc = 0;
  7065. struct cam_isp_context *ctx_isp =
  7066. (struct cam_isp_context *) ctx->ctx_priv;
  7067. ctx->link_hdl = -1;
  7068. ctx->ctx_crm_intf = NULL;
  7069. ctx_isp->trigger_id = -1;
  7070. ctx_isp->mswitch_default_apply_delay_max_cnt = 0;
  7071. atomic_set(&ctx_isp->mswitch_default_apply_delay_ref_cnt, 0);
  7072. return rc;
  7073. }
  7074. static int __cam_isp_ctx_get_dev_info(struct cam_context *ctx,
  7075. struct cam_req_mgr_device_info *dev_info)
  7076. {
  7077. int rc = 0;
  7078. struct cam_isp_context *ctx_isp =
  7079. (struct cam_isp_context *) ctx->ctx_priv;
  7080. dev_info->dev_hdl = ctx->dev_hdl;
  7081. strlcpy(dev_info->name, CAM_ISP_DEV_NAME, sizeof(dev_info->name));
  7082. dev_info->dev_id = CAM_REQ_MGR_DEVICE_IFE;
  7083. dev_info->p_delay = CAM_PIPELINE_DELAY_1;
  7084. dev_info->m_delay = CAM_MODESWITCH_DELAY_1;
  7085. dev_info->trigger = CAM_TRIGGER_POINT_SOF;
  7086. dev_info->trigger_on = true;
  7087. dev_info->is_shdr = ctx_isp->is_tfe_shdr;
  7088. dev_info->is_shdr_master = ctx_isp->is_shdr_master;
  7089. return rc;
  7090. }
  7091. static inline void __cam_isp_context_reset_ctx_params(
  7092. struct cam_isp_context *ctx_isp)
  7093. {
  7094. atomic_set(&ctx_isp->process_bubble, 0);
  7095. atomic_set(&ctx_isp->rxd_epoch, 0);
  7096. atomic_set(&ctx_isp->internal_recovery_set, 0);
  7097. ctx_isp->frame_id = 0;
  7098. ctx_isp->sof_timestamp_val = 0;
  7099. ctx_isp->boot_timestamp = 0;
  7100. ctx_isp->active_req_cnt = 0;
  7101. ctx_isp->reported_req_id = 0;
  7102. ctx_isp->reported_frame_id = 0;
  7103. ctx_isp->bubble_frame_cnt = 0;
  7104. ctx_isp->congestion_cnt = 0;
  7105. ctx_isp->recovery_req_id = 0;
  7106. ctx_isp->aeb_error_cnt = 0;
  7107. ctx_isp->out_of_sync_cnt = 0;
  7108. ctx_isp->sof_dbg_irq_en = false;
  7109. ctx_isp->last_sof_jiffies = 0;
  7110. ctx_isp->last_applied_jiffies = 0;
  7111. }
  7112. static int __cam_isp_ctx_start_dev_in_ready(struct cam_context *ctx,
  7113. struct cam_start_stop_dev_cmd *cmd)
  7114. {
  7115. int rc = 0;
  7116. int i;
  7117. struct cam_isp_start_args start_isp;
  7118. struct cam_ctx_request *req;
  7119. struct cam_isp_ctx_req *req_isp;
  7120. struct cam_isp_context *ctx_isp =
  7121. (struct cam_isp_context *) ctx->ctx_priv;
  7122. if (cmd->session_handle != ctx->session_hdl ||
  7123. cmd->dev_handle != ctx->dev_hdl) {
  7124. rc = -EPERM;
  7125. goto end;
  7126. }
  7127. if (list_empty(&ctx->pending_req_list)) {
  7128. /* should never happen */
  7129. CAM_ERR(CAM_ISP, "Start device with empty configuration, ctx: %u, link: 0x%x",
  7130. ctx->ctx_id, ctx->link_hdl);
  7131. rc = -EFAULT;
  7132. goto end;
  7133. } else {
  7134. req = list_first_entry(&ctx->pending_req_list,
  7135. struct cam_ctx_request, list);
  7136. }
  7137. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  7138. if (!ctx_isp->hw_ctx) {
  7139. CAM_ERR(CAM_ISP, "Wrong hw context pointer.ctx_idx: %u, link: 0x%x",
  7140. ctx->ctx_id, ctx->link_hdl);
  7141. rc = -EFAULT;
  7142. goto end;
  7143. }
  7144. start_isp.hw_config.ctxt_to_hw_map = ctx_isp->hw_ctx;
  7145. start_isp.hw_config.request_id = req->request_id;
  7146. start_isp.hw_config.hw_update_entries = req_isp->cfg;
  7147. start_isp.hw_config.num_hw_update_entries = req_isp->num_cfg;
  7148. start_isp.hw_config.priv = &req_isp->hw_update_data;
  7149. start_isp.hw_config.init_packet = 1;
  7150. start_isp.hw_config.reapply_type = CAM_CONFIG_REAPPLY_NONE;
  7151. start_isp.hw_config.cdm_reset_before_apply = false;
  7152. start_isp.is_internal_start = false;
  7153. ctx_isp->last_applied_req_id = req->request_id;
  7154. if (ctx->state == CAM_CTX_FLUSHED)
  7155. start_isp.start_only = true;
  7156. else
  7157. start_isp.start_only = false;
  7158. __cam_isp_context_reset_ctx_params(ctx_isp);
  7159. ctx_isp->substate_activated = ctx_isp->rdi_only_context ?
  7160. CAM_ISP_CTX_ACTIVATED_APPLIED :
  7161. (req_isp->num_fence_map_out) ? CAM_ISP_CTX_ACTIVATED_EPOCH :
  7162. CAM_ISP_CTX_ACTIVATED_SOF;
  7163. atomic64_set(&ctx_isp->dbg_monitors.state_monitor_head, -1);
  7164. atomic64_set(&ctx_isp->dbg_monitors.frame_monitor_head, -1);
  7165. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  7166. atomic64_set(&ctx_isp->dbg_monitors.event_record_head[i], -1);
  7167. /*
  7168. * In case of CSID TPG we might receive SOF and RUP IRQs
  7169. * before hw_mgr_intf->hw_start has returned. So move
  7170. * req out of pending list before hw_start and add it
  7171. * back to pending list if hw_start fails.
  7172. */
  7173. list_del_init(&req->list);
  7174. if (ctx_isp->offline_context && !req_isp->num_fence_map_out) {
  7175. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  7176. list_add_tail(&req->list, &ctx->free_req_list);
  7177. atomic_set(&ctx_isp->rxd_epoch, 1);
  7178. CAM_DBG(CAM_REQ,
  7179. "Move pending req: %lld to free list(cnt: %d) offline ctx %u link: 0x%x",
  7180. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  7181. } else if (ctx_isp->rdi_only_context || !req_isp->num_fence_map_out) {
  7182. list_add_tail(&req->list, &ctx->wait_req_list);
  7183. CAM_DBG(CAM_REQ,
  7184. "Move pending req: %lld to wait list(cnt: %d) ctx %u link: 0x%x",
  7185. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  7186. } else {
  7187. list_add_tail(&req->list, &ctx->active_req_list);
  7188. ctx_isp->active_req_cnt++;
  7189. CAM_DBG(CAM_REQ,
  7190. "Move pending req: %lld to active list(cnt: %d) ctx %u link: 0x%x offline %d",
  7191. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl,
  7192. ctx_isp->offline_context);
  7193. }
  7194. /*
  7195. * Only place to change state before calling the hw due to
  7196. * hardware tasklet has higher priority that can cause the
  7197. * irq handling comes early
  7198. */
  7199. ctx->state = CAM_CTX_ACTIVATED;
  7200. trace_cam_context_state("ISP", ctx);
  7201. rc = ctx->hw_mgr_intf->hw_start(ctx->hw_mgr_intf->hw_mgr_priv,
  7202. &start_isp);
  7203. if (rc) {
  7204. /* HW failure. user need to clean up the resource */
  7205. CAM_ERR(CAM_ISP, "Start HW failed, ctx %u link: 0x%x", ctx->ctx_id, ctx->link_hdl);
  7206. ctx->state = CAM_CTX_READY;
  7207. if ((rc == -ETIMEDOUT) &&
  7208. (isp_ctx_debug.enable_cdm_cmd_buff_dump))
  7209. rc = cam_isp_ctx_dump_req(req_isp, 0, 0, NULL, false);
  7210. trace_cam_context_state("ISP", ctx);
  7211. list_del_init(&req->list);
  7212. list_add(&req->list, &ctx->pending_req_list);
  7213. goto end;
  7214. }
  7215. CAM_DBG(CAM_ISP, "start device success ctx %u link: 0x%x", ctx->ctx_id, ctx->link_hdl);
  7216. end:
  7217. return rc;
  7218. }
  7219. static int __cam_isp_ctx_unlink_in_ready(struct cam_context *ctx,
  7220. struct cam_req_mgr_core_dev_link_setup *unlink)
  7221. {
  7222. int rc = 0;
  7223. ctx->link_hdl = -1;
  7224. ctx->ctx_crm_intf = NULL;
  7225. ctx->state = CAM_CTX_ACQUIRED;
  7226. trace_cam_context_state("ISP", ctx);
  7227. return rc;
  7228. }
  7229. static int __cam_isp_ctx_stop_dev_in_activated_unlock(
  7230. struct cam_context *ctx, struct cam_start_stop_dev_cmd *stop_cmd)
  7231. {
  7232. int rc = 0;
  7233. uint32_t i;
  7234. struct cam_hw_stop_args stop;
  7235. struct cam_ctx_request *req;
  7236. struct cam_isp_ctx_req *req_isp;
  7237. struct cam_isp_context *ctx_isp =
  7238. (struct cam_isp_context *) ctx->ctx_priv;
  7239. struct cam_isp_stop_args stop_isp;
  7240. /* Mask off all the incoming hardware events */
  7241. spin_lock_bh(&ctx->lock);
  7242. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  7243. spin_unlock_bh(&ctx->lock);
  7244. /* stop hw first */
  7245. if (ctx_isp->hw_ctx) {
  7246. stop.ctxt_to_hw_map = ctx_isp->hw_ctx;
  7247. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  7248. stop_isp.stop_only = false;
  7249. stop_isp.is_internal_stop = false;
  7250. stop.args = (void *) &stop_isp;
  7251. ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  7252. &stop);
  7253. }
  7254. CAM_DBG(CAM_ISP, "next Substate[%s], ctx_idx: %u, link: 0x%x",
  7255. __cam_isp_ctx_substate_val_to_type(
  7256. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  7257. if (ctx->ctx_crm_intf &&
  7258. ctx->ctx_crm_intf->notify_stop) {
  7259. struct cam_req_mgr_notify_stop notify;
  7260. notify.link_hdl = ctx->link_hdl;
  7261. CAM_DBG(CAM_ISP,
  7262. "Notify CRM about device stop ctx %u link 0x%x",
  7263. ctx->ctx_id, ctx->link_hdl);
  7264. ctx->ctx_crm_intf->notify_stop(&notify);
  7265. } else if (!ctx_isp->offline_context)
  7266. CAM_ERR(CAM_ISP, "cb not present, ctx_idx: %u, link: 0x%x",
  7267. ctx->ctx_id, ctx->link_hdl);
  7268. while (!list_empty(&ctx->pending_req_list)) {
  7269. req = list_first_entry(&ctx->pending_req_list,
  7270. struct cam_ctx_request, list);
  7271. list_del_init(&req->list);
  7272. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  7273. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  7274. CAM_DBG(CAM_ISP, "signal fence in pending list. fence num %d ctx:%u, link: 0x%x",
  7275. req_isp->num_fence_map_out, ctx->ctx_id, ctx->link_hdl);
  7276. for (i = 0; i < req_isp->num_fence_map_out; i++)
  7277. if (req_isp->fence_map_out[i].sync_id != -1) {
  7278. cam_sync_signal(
  7279. req_isp->fence_map_out[i].sync_id,
  7280. CAM_SYNC_STATE_SIGNALED_CANCEL,
  7281. CAM_SYNC_ISP_EVENT_HW_STOP);
  7282. }
  7283. list_add_tail(&req->list, &ctx->free_req_list);
  7284. }
  7285. while (!list_empty(&ctx->wait_req_list)) {
  7286. req = list_first_entry(&ctx->wait_req_list,
  7287. struct cam_ctx_request, list);
  7288. list_del_init(&req->list);
  7289. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  7290. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  7291. CAM_DBG(CAM_ISP, "signal fence in wait list. fence num %d ctx: %u, link: 0x%x",
  7292. req_isp->num_fence_map_out, ctx->ctx_id, ctx->link_hdl);
  7293. for (i = 0; i < req_isp->num_fence_map_out; i++)
  7294. if (req_isp->fence_map_out[i].sync_id != -1) {
  7295. cam_sync_signal(
  7296. req_isp->fence_map_out[i].sync_id,
  7297. CAM_SYNC_STATE_SIGNALED_CANCEL,
  7298. CAM_SYNC_ISP_EVENT_HW_STOP);
  7299. }
  7300. list_add_tail(&req->list, &ctx->free_req_list);
  7301. }
  7302. while (!list_empty(&ctx->active_req_list)) {
  7303. req = list_first_entry(&ctx->active_req_list,
  7304. struct cam_ctx_request, list);
  7305. list_del_init(&req->list);
  7306. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  7307. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  7308. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d ctx: %u, link: 0x%x",
  7309. req_isp->num_fence_map_out, ctx->ctx_id, ctx->link_hdl);
  7310. for (i = 0; i < req_isp->num_fence_map_out; i++)
  7311. if (req_isp->fence_map_out[i].sync_id != -1) {
  7312. cam_sync_signal(
  7313. req_isp->fence_map_out[i].sync_id,
  7314. CAM_SYNC_STATE_SIGNALED_CANCEL,
  7315. CAM_SYNC_ISP_EVENT_HW_STOP);
  7316. }
  7317. list_add_tail(&req->list, &ctx->free_req_list);
  7318. }
  7319. ctx_isp->frame_id = 0;
  7320. ctx_isp->active_req_cnt = 0;
  7321. ctx_isp->reported_req_id = 0;
  7322. ctx_isp->reported_frame_id = 0;
  7323. ctx_isp->last_applied_req_id = 0;
  7324. ctx_isp->req_info.last_bufdone_req_id = 0;
  7325. ctx_isp->bubble_frame_cnt = 0;
  7326. ctx_isp->congestion_cnt = 0;
  7327. ctx_isp->sof_dbg_irq_en = false;
  7328. atomic_set(&ctx_isp->process_bubble, 0);
  7329. atomic_set(&ctx_isp->internal_recovery_set, 0);
  7330. atomic_set(&ctx_isp->rxd_epoch, 0);
  7331. atomic64_set(&ctx_isp->dbg_monitors.state_monitor_head, -1);
  7332. atomic64_set(&ctx_isp->dbg_monitors.frame_monitor_head, -1);
  7333. /* Reset skipped_list for FCG config */
  7334. __cam_isp_ctx_reset_fcg_tracker(ctx);
  7335. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  7336. atomic64_set(&ctx_isp->dbg_monitors.event_record_head[i], -1);
  7337. CAM_DBG(CAM_ISP, "Stop device success next state %d on ctx %u link: 0x%x",
  7338. ctx->state, ctx->ctx_id, ctx->link_hdl);
  7339. if (!stop_cmd) {
  7340. rc = __cam_isp_ctx_unlink_in_ready(ctx, NULL);
  7341. if (rc)
  7342. CAM_ERR(CAM_ISP, "Unlink failed rc=%d, ctx %u link: 0x%x",
  7343. rc, ctx->ctx_id, ctx->link_hdl);
  7344. }
  7345. return rc;
  7346. }
  7347. static int __cam_isp_ctx_stop_dev_in_activated(struct cam_context *ctx,
  7348. struct cam_start_stop_dev_cmd *cmd)
  7349. {
  7350. int rc = 0;
  7351. struct cam_isp_context *ctx_isp =
  7352. (struct cam_isp_context *)ctx->ctx_priv;
  7353. __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, cmd);
  7354. ctx_isp->init_received = false;
  7355. ctx->state = CAM_CTX_ACQUIRED;
  7356. trace_cam_context_state("ISP", ctx);
  7357. return rc;
  7358. }
  7359. static int __cam_isp_ctx_release_dev_in_activated(struct cam_context *ctx,
  7360. struct cam_release_dev_cmd *cmd)
  7361. {
  7362. int rc = 0;
  7363. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  7364. if (rc)
  7365. CAM_ERR(CAM_ISP, "Stop device failed rc=%d, ctx %u link: 0x%x",
  7366. rc, ctx->ctx_id, ctx->link_hdl);
  7367. rc = __cam_isp_ctx_release_dev_in_top_state(ctx, cmd);
  7368. if (rc)
  7369. CAM_ERR(CAM_ISP, "Release device failed rc=%d ctx %u link: 0x%x",
  7370. rc, ctx->ctx_id, ctx->link_hdl);
  7371. return rc;
  7372. }
  7373. static int __cam_isp_ctx_release_hw_in_activated(struct cam_context *ctx,
  7374. void *cmd)
  7375. {
  7376. int rc = 0;
  7377. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  7378. if (rc)
  7379. CAM_ERR(CAM_ISP, "Stop device failed rc=%d, ctx %u link: 0x%x",
  7380. rc, ctx->ctx_id, ctx->link_hdl);
  7381. rc = __cam_isp_ctx_release_hw_in_top_state(ctx, cmd);
  7382. if (rc)
  7383. CAM_ERR(CAM_ISP, "Release hw failed rc=%d, ctx %u link: 0x%x",
  7384. rc, ctx->ctx_id, ctx->link_hdl);
  7385. return rc;
  7386. }
  7387. static int __cam_isp_ctx_link_pause(struct cam_context *ctx)
  7388. {
  7389. int rc = 0;
  7390. struct cam_hw_cmd_args hw_cmd_args;
  7391. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  7392. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  7393. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  7394. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_PAUSE_HW;
  7395. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  7396. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  7397. &hw_cmd_args);
  7398. return rc;
  7399. }
  7400. static int __cam_isp_ctx_link_resume(struct cam_context *ctx)
  7401. {
  7402. int rc = 0;
  7403. struct cam_hw_cmd_args hw_cmd_args;
  7404. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  7405. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  7406. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  7407. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  7408. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  7409. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  7410. &hw_cmd_args);
  7411. return rc;
  7412. }
  7413. static int __cam_isp_ctx_reset_and_recover(
  7414. bool skip_resume, struct cam_context *ctx)
  7415. {
  7416. int rc = 0;
  7417. struct cam_isp_context *ctx_isp =
  7418. (struct cam_isp_context *)ctx->ctx_priv;
  7419. struct cam_isp_stop_args stop_isp;
  7420. struct cam_hw_stop_args stop_args;
  7421. struct cam_isp_start_args start_isp;
  7422. struct cam_hw_cmd_args hw_cmd_args;
  7423. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  7424. struct cam_ctx_request *req;
  7425. struct cam_isp_ctx_req *req_isp;
  7426. spin_lock_bh(&ctx->lock);
  7427. if (ctx_isp->active_req_cnt) {
  7428. spin_unlock_bh(&ctx->lock);
  7429. CAM_WARN(CAM_ISP,
  7430. "Active list not empty: %u in ctx: %u on link: 0x%x, retry recovery for req: %lld after buf_done",
  7431. ctx_isp->active_req_cnt, ctx->ctx_id,
  7432. ctx->link_hdl, ctx_isp->recovery_req_id);
  7433. goto end;
  7434. }
  7435. if (ctx->state != CAM_CTX_ACTIVATED) {
  7436. spin_unlock_bh(&ctx->lock);
  7437. CAM_ERR(CAM_ISP,
  7438. "In wrong state %d, for recovery ctx: %u in link: 0x%x recovery req: %lld",
  7439. ctx->state, ctx->ctx_id,
  7440. ctx->link_hdl, ctx_isp->recovery_req_id);
  7441. rc = -EINVAL;
  7442. goto end;
  7443. }
  7444. if (list_empty(&ctx->pending_req_list)) {
  7445. /* Cannot start with no request */
  7446. spin_unlock_bh(&ctx->lock);
  7447. CAM_ERR(CAM_ISP,
  7448. "Failed to reset and recover last_applied_req: %llu in ctx: %u on link: 0x%x",
  7449. ctx_isp->last_applied_req_id, ctx->ctx_id, ctx->link_hdl);
  7450. rc = -EFAULT;
  7451. goto end;
  7452. }
  7453. if (!ctx_isp->hw_ctx) {
  7454. spin_unlock_bh(&ctx->lock);
  7455. CAM_ERR(CAM_ISP,
  7456. "Invalid hw context pointer ctx: %u on link: 0x%x",
  7457. ctx->ctx_id, ctx->link_hdl);
  7458. rc = -EFAULT;
  7459. goto end;
  7460. }
  7461. /* Block all events till HW is resumed */
  7462. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  7463. req = list_first_entry(&ctx->pending_req_list,
  7464. struct cam_ctx_request, list);
  7465. spin_unlock_bh(&ctx->lock);
  7466. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  7467. CAM_INFO(CAM_ISP,
  7468. "Trigger Halt, Reset & Resume for req: %llu ctx: %u in state: %d link: 0x%x",
  7469. req->request_id, ctx->ctx_id, ctx->state, ctx->link_hdl);
  7470. stop_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  7471. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  7472. stop_isp.stop_only = true;
  7473. stop_isp.is_internal_stop = true;
  7474. stop_args.args = (void *)&stop_isp;
  7475. rc = ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  7476. &stop_args);
  7477. if (rc) {
  7478. CAM_ERR(CAM_ISP, "Failed to stop HW rc: %d ctx: %u link: 0x%x",
  7479. rc, ctx->ctx_id, ctx->link_hdl);
  7480. goto end;
  7481. }
  7482. CAM_DBG(CAM_ISP, "Stop HW success ctx: %u link: 0x%x",
  7483. ctx->ctx_id, ctx->link_hdl);
  7484. /* API provides provision to stream off and not resume as well in case of fatal errors */
  7485. if (skip_resume) {
  7486. atomic_set(&ctx_isp->internal_recovery_set, 0);
  7487. CAM_INFO(CAM_ISP,
  7488. "Halting streaming off IFE/SFE ctx: %u last_applied_req: %lld [recovery_req: %lld] on link: 0x%x",
  7489. ctx->ctx_id, ctx_isp->last_applied_req_id,
  7490. ctx_isp->recovery_req_id, ctx->link_hdl);
  7491. goto end;
  7492. }
  7493. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  7494. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  7495. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  7496. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  7497. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  7498. &hw_cmd_args);
  7499. if (rc) {
  7500. CAM_ERR(CAM_ISP, "Failed to resume HW rc: %d ctx: %u link: 0x%x",
  7501. rc, ctx->ctx_id, ctx->link_hdl);
  7502. goto end;
  7503. }
  7504. CAM_DBG(CAM_ISP, "Resume call success ctx: %u on link: 0x%x",
  7505. ctx->ctx_id, ctx->link_hdl);
  7506. start_isp.hw_config.ctxt_to_hw_map = ctx_isp->hw_ctx;
  7507. start_isp.hw_config.request_id = req->request_id;
  7508. start_isp.hw_config.hw_update_entries = req_isp->cfg;
  7509. start_isp.hw_config.num_hw_update_entries = req_isp->num_cfg;
  7510. start_isp.hw_config.priv = &req_isp->hw_update_data;
  7511. start_isp.hw_config.init_packet = 1;
  7512. start_isp.hw_config.reapply_type = CAM_CONFIG_REAPPLY_IQ;
  7513. start_isp.hw_config.cdm_reset_before_apply = false;
  7514. start_isp.start_only = true;
  7515. start_isp.is_internal_start = true;
  7516. __cam_isp_context_reset_internal_recovery_params(ctx_isp);
  7517. ctx_isp->substate_activated = ctx_isp->rdi_only_context ?
  7518. CAM_ISP_CTX_ACTIVATED_APPLIED : CAM_ISP_CTX_ACTIVATED_SOF;
  7519. rc = ctx->hw_mgr_intf->hw_start(ctx->hw_mgr_intf->hw_mgr_priv,
  7520. &start_isp);
  7521. if (rc) {
  7522. CAM_ERR(CAM_ISP, "Start HW failed, ctx: %u link: 0x%x", ctx->ctx_id, ctx->link_hdl);
  7523. ctx->state = CAM_CTX_READY;
  7524. goto end;
  7525. }
  7526. /* IQ applied for this request, on next trigger skip IQ cfg */
  7527. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  7528. /* Notify userland that KMD has done internal recovery */
  7529. __cam_isp_ctx_notify_v4l2_error_event(CAM_REQ_MGR_WARN_TYPE_KMD_RECOVERY,
  7530. 0, req->request_id, ctx);
  7531. CAM_INFO(CAM_ISP, "Internal Start HW success ctx %u on link: 0x%x for req: %llu",
  7532. ctx->ctx_id, ctx->link_hdl, req->request_id);
  7533. end:
  7534. return rc;
  7535. }
  7536. static bool __cam_isp_ctx_try_internal_recovery_for_bubble(
  7537. int64_t error_req_id, struct cam_context *ctx)
  7538. {
  7539. int rc;
  7540. struct cam_isp_context *ctx_isp =
  7541. (struct cam_isp_context *)ctx->ctx_priv;
  7542. if (isp_ctx_debug.disable_internal_recovery_mask &
  7543. CAM_ISP_CTX_DISABLE_RECOVERY_BUBBLE)
  7544. return false;
  7545. /* Perform recovery if bubble recovery is stalled */
  7546. if (!atomic_read(&ctx_isp->process_bubble))
  7547. return false;
  7548. /* Validate if errored request has been applied */
  7549. if (ctx_isp->last_applied_req_id < error_req_id) {
  7550. CAM_WARN(CAM_ISP,
  7551. "Skip trying for internal recovery last applied: %lld error_req: %lld for ctx: %u on link: 0x%x",
  7552. ctx_isp->last_applied_req_id, error_req_id,
  7553. ctx->ctx_id, ctx->link_hdl);
  7554. return false;
  7555. }
  7556. if (__cam_isp_ctx_validate_for_req_reapply_util(ctx_isp)) {
  7557. CAM_WARN(CAM_ISP,
  7558. "Internal recovery not possible for ctx: %u on link: 0x%x req: %lld [last_applied: %lld]",
  7559. ctx->ctx_id, ctx->link_hdl, error_req_id, ctx_isp->last_applied_req_id);
  7560. return false;
  7561. }
  7562. /* Trigger reset and recover */
  7563. atomic_set(&ctx_isp->internal_recovery_set, 1);
  7564. rc = __cam_isp_ctx_reset_and_recover(false, ctx);
  7565. if (rc) {
  7566. CAM_WARN(CAM_ISP,
  7567. "Internal recovery failed in ctx: %u on link: 0x%x req: %lld [last_applied: %lld]",
  7568. ctx->ctx_id, ctx->link_hdl, error_req_id, ctx_isp->last_applied_req_id);
  7569. atomic_set(&ctx_isp->internal_recovery_set, 0);
  7570. goto error;
  7571. }
  7572. CAM_DBG(CAM_ISP,
  7573. "Internal recovery done in ctx: %u on link: 0x%x req: %lld [last_applied: %lld]",
  7574. ctx->ctx_id, ctx->link_hdl, error_req_id, ctx_isp->last_applied_req_id);
  7575. return true;
  7576. error:
  7577. return false;
  7578. }
  7579. static int __cam_isp_ctx_process_evt(struct cam_context *ctx,
  7580. struct cam_req_mgr_link_evt_data *link_evt_data)
  7581. {
  7582. int rc = 0;
  7583. struct cam_isp_context *ctx_isp =
  7584. (struct cam_isp_context *) ctx->ctx_priv;
  7585. if ((ctx->state == CAM_CTX_ACQUIRED) &&
  7586. (link_evt_data->evt_type != CAM_REQ_MGR_LINK_EVT_UPDATE_PROPERTIES)) {
  7587. CAM_WARN(CAM_ISP,
  7588. "Get unexpect evt:%d in acquired state, ctx: %u on link: 0x%x",
  7589. link_evt_data->evt_type, ctx->ctx_id, ctx->link_hdl);
  7590. return -EINVAL;
  7591. }
  7592. switch (link_evt_data->evt_type) {
  7593. case CAM_REQ_MGR_LINK_EVT_ERR:
  7594. case CAM_REQ_MGR_LINK_EVT_EOF:
  7595. /* No handling */
  7596. break;
  7597. case CAM_REQ_MGR_LINK_EVT_PAUSE:
  7598. rc = __cam_isp_ctx_link_pause(ctx);
  7599. break;
  7600. case CAM_REQ_MGR_LINK_EVT_RESUME:
  7601. rc = __cam_isp_ctx_link_resume(ctx);
  7602. break;
  7603. case CAM_REQ_MGR_LINK_EVT_SOF_FREEZE:
  7604. rc = __cam_isp_ctx_handle_sof_freeze_evt(ctx);
  7605. break;
  7606. case CAM_REQ_MGR_LINK_EVT_STALLED: {
  7607. bool internal_recovery_skipped = false;
  7608. if (ctx->state == CAM_CTX_ACTIVATED) {
  7609. if (link_evt_data->try_for_recovery)
  7610. internal_recovery_skipped =
  7611. __cam_isp_ctx_try_internal_recovery_for_bubble(
  7612. link_evt_data->req_id, ctx);
  7613. if (!internal_recovery_skipped)
  7614. rc = __cam_isp_ctx_trigger_reg_dump(
  7615. CAM_HW_MGR_CMD_REG_DUMP_ON_ERROR, ctx);
  7616. }
  7617. link_evt_data->try_for_recovery = internal_recovery_skipped;
  7618. }
  7619. break;
  7620. case CAM_REQ_MGR_LINK_EVT_UPDATE_PROPERTIES:
  7621. if (link_evt_data->u.properties_mask &
  7622. CAM_LINK_PROPERTY_SENSOR_STANDBY_AFTER_EOF)
  7623. ctx_isp->vfps_aux_context = true;
  7624. else
  7625. ctx_isp->vfps_aux_context = false;
  7626. CAM_DBG(CAM_ISP, "vfps_aux_context:%s on ctx: %u link: 0x%x",
  7627. CAM_BOOL_TO_YESNO(ctx_isp->vfps_aux_context), ctx->ctx_id, ctx->link_hdl);
  7628. break;
  7629. default:
  7630. CAM_WARN(CAM_ISP,
  7631. "Unsupported event type: 0x%x on ctx: %u link: 0x%x",
  7632. link_evt_data->evt_type, ctx->ctx_id, ctx->link_hdl);
  7633. rc = -EINVAL;
  7634. break;
  7635. }
  7636. return rc;
  7637. }
  7638. static int __cam_isp_ctx_unlink_in_activated(struct cam_context *ctx,
  7639. struct cam_req_mgr_core_dev_link_setup *unlink)
  7640. {
  7641. int rc = 0;
  7642. CAM_WARN(CAM_ISP,
  7643. "Received unlink in activated state. It's unexpected, ctx: %u link: 0x%x",
  7644. ctx->ctx_id, ctx->link_hdl);
  7645. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  7646. if (rc)
  7647. CAM_WARN(CAM_ISP, "Stop device failed rc=%d, ctx: %u link: 0x%x",
  7648. rc, ctx->ctx_id, ctx->link_hdl);
  7649. rc = __cam_isp_ctx_unlink_in_ready(ctx, unlink);
  7650. if (rc)
  7651. CAM_ERR(CAM_ISP, "Unlink failed rc=%d, ctx: %u link: 0x%x",
  7652. rc, ctx->ctx_id, ctx->link_hdl);
  7653. return rc;
  7654. }
  7655. static int __cam_isp_ctx_apply_req(struct cam_context *ctx,
  7656. struct cam_req_mgr_apply_request *apply)
  7657. {
  7658. int rc = 0;
  7659. struct cam_ctx_ops *ctx_ops = NULL;
  7660. struct cam_isp_context *ctx_isp =
  7661. (struct cam_isp_context *) ctx->ctx_priv;
  7662. trace_cam_apply_req("ISP", ctx->ctx_id, apply->request_id, apply->link_hdl);
  7663. CAM_DBG(CAM_ISP, "Enter: apply req in Substate[%s] request_id:%lld, ctx: %u link: 0x%x",
  7664. __cam_isp_ctx_substate_val_to_type(
  7665. ctx_isp->substate_activated), apply->request_id, ctx->ctx_id, ctx->link_hdl);
  7666. ctx_ops = &ctx_isp->substate_machine[ctx_isp->substate_activated];
  7667. if (ctx_ops->crm_ops.apply_req) {
  7668. rc = ctx_ops->crm_ops.apply_req(ctx, apply);
  7669. } else {
  7670. CAM_WARN_RATE_LIMIT(CAM_ISP,
  7671. "No handle function in activated Substate[%s], ctx: %u link: 0x%x",
  7672. __cam_isp_ctx_substate_val_to_type(
  7673. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  7674. rc = -EFAULT;
  7675. }
  7676. if (rc)
  7677. CAM_WARN_RATE_LIMIT(CAM_ISP,
  7678. "Apply failed in active Substate[%s] rc %d, ctx: %u link: 0x%x",
  7679. __cam_isp_ctx_substate_val_to_type(
  7680. ctx_isp->substate_activated), rc, ctx->ctx_id, ctx->link_hdl);
  7681. return rc;
  7682. }
  7683. static int __cam_isp_ctx_apply_default_settings(
  7684. struct cam_context *ctx,
  7685. struct cam_req_mgr_apply_request *apply)
  7686. {
  7687. int rc = 0;
  7688. struct cam_ctx_ops *ctx_ops = NULL;
  7689. struct cam_isp_context *ctx_isp =
  7690. (struct cam_isp_context *) ctx->ctx_priv;
  7691. struct cam_isp_fcg_prediction_tracker *fcg_tracker;
  7692. if (!(apply->trigger_point & ctx_isp->subscribe_event)) {
  7693. CAM_WARN(CAM_ISP,
  7694. "Trigger: %u not subscribed for: %u, ctx: %u link: 0x%x",
  7695. apply->trigger_point, ctx_isp->subscribe_event, ctx->ctx_id,
  7696. ctx->link_hdl);
  7697. return 0;
  7698. }
  7699. /* Allow apply default settings for IFE only at SOF */
  7700. if (apply->trigger_point != CAM_TRIGGER_POINT_SOF)
  7701. return 0;
  7702. if (atomic_read(&ctx_isp->internal_recovery_set))
  7703. return __cam_isp_ctx_reset_and_recover(false, ctx);
  7704. /* FCG handling */
  7705. fcg_tracker = &ctx_isp->fcg_tracker;
  7706. if (ctx_isp->frame_id != 1)
  7707. fcg_tracker->num_skipped += 1;
  7708. CAM_DBG(CAM_ISP,
  7709. "Apply default settings, number of previous continuous skipped frames: %d, ctx_id: %d",
  7710. fcg_tracker->num_skipped, ctx->ctx_id);
  7711. /*
  7712. * Call notify frame skip for static offline cases or
  7713. * mode switch cases where IFE mode switch delay differs
  7714. * from other devices on the link
  7715. */
  7716. if ((ctx_isp->use_default_apply) ||
  7717. (ctx_isp->mode_switch_en && ctx_isp->handle_mswitch)) {
  7718. CAM_DBG(CAM_ISP,
  7719. "Enter: apply req in Substate:%d request _id:%lld ctx:%u on link:0x%x",
  7720. ctx_isp->substate_activated, apply->request_id,
  7721. ctx->ctx_id, ctx->link_hdl);
  7722. ctx_ops = &ctx_isp->substate_machine[
  7723. ctx_isp->substate_activated];
  7724. if (ctx_ops->crm_ops.notify_frame_skip) {
  7725. rc = ctx_ops->crm_ops.notify_frame_skip(ctx, apply);
  7726. } else {
  7727. CAM_WARN_RATE_LIMIT(CAM_ISP,
  7728. "No handle function in activated substate %d, ctx:%u on link:0x%x",
  7729. ctx_isp->substate_activated, ctx->ctx_id, ctx->link_hdl);
  7730. rc = -EFAULT;
  7731. }
  7732. if (rc)
  7733. CAM_WARN_RATE_LIMIT(CAM_ISP,
  7734. "Apply default failed in active substate %d rc %d ctx: %u link: 0x%x",
  7735. ctx_isp->substate_activated, rc, ctx->ctx_id, ctx->link_hdl);
  7736. }
  7737. return rc;
  7738. }
  7739. void __cam_isp_ctx_notify_cpas(struct cam_context *ctx, uint32_t evt_id)
  7740. {
  7741. uint64_t request_id = 0;
  7742. struct cam_isp_context *ctx_isp =
  7743. (struct cam_isp_context *)ctx->ctx_priv;
  7744. struct cam_ctx_request *req = NULL;
  7745. char ctx_evt_id_string[128];
  7746. switch (evt_id) {
  7747. case CAM_ISP_HW_EVENT_SOF:
  7748. if (!list_empty(&ctx->wait_req_list)) {
  7749. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request, list);
  7750. request_id = req->request_id;
  7751. } else
  7752. request_id = 0;
  7753. if (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_EPOCH &&
  7754. !list_empty(&ctx->active_req_list)) {
  7755. req = list_last_entry(&ctx->active_req_list, struct cam_ctx_request, list);
  7756. request_id = req->request_id;
  7757. CAM_DBG(CAM_ISP, "EPCR notify cpas");
  7758. }
  7759. break;
  7760. case CAM_ISP_HW_EVENT_EOF:
  7761. if (!list_empty(&ctx->active_req_list)) {
  7762. req = list_first_entry(&ctx->active_req_list, struct cam_ctx_request, list);
  7763. request_id = req->request_id;
  7764. } else
  7765. request_id = 0;
  7766. break;
  7767. case CAM_ISP_HW_EVENT_EPOCH:
  7768. if (list_empty(&ctx->wait_req_list)) {
  7769. if (!list_empty(&ctx->active_req_list)) {
  7770. req = list_last_entry(&ctx->active_req_list,
  7771. struct cam_ctx_request, list);
  7772. request_id = req->request_id;
  7773. } else
  7774. request_id = 0;
  7775. } else {
  7776. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request, list);
  7777. request_id = req->request_id;
  7778. }
  7779. break;
  7780. default:
  7781. return;
  7782. }
  7783. snprintf(ctx_evt_id_string, sizeof(ctx_evt_id_string),
  7784. "%s_frame[%llu]_%s",
  7785. ctx->ctx_id_string,
  7786. ctx_isp->frame_id,
  7787. cam_isp_hw_evt_type_to_string(evt_id));
  7788. CAM_DBG(CAM_ISP, "Substate[%s] ctx: %s frame: %llu evt: %s req: %llu",
  7789. __cam_isp_ctx_substate_val_to_type(ctx_isp->substate_activated),
  7790. ctx->ctx_id_string,
  7791. ctx_isp->frame_id,
  7792. cam_isp_hw_evt_type_to_string(evt_id),
  7793. request_id);
  7794. cam_cpas_notify_event(ctx_evt_id_string, request_id);
  7795. }
  7796. static int __cam_isp_ctx_handle_irq_in_activated(void *context,
  7797. uint32_t evt_id, void *evt_data)
  7798. {
  7799. int rc = 0;
  7800. struct cam_isp_ctx_irq_ops *irq_ops = NULL;
  7801. struct cam_context *ctx = (struct cam_context *)context;
  7802. struct cam_isp_context *ctx_isp =
  7803. (struct cam_isp_context *)ctx->ctx_priv;
  7804. spin_lock(&ctx->lock);
  7805. trace_cam_isp_activated_irq(ctx, ctx_isp->substate_activated, evt_id,
  7806. __cam_isp_ctx_get_event_ts(evt_id, evt_data));
  7807. CAM_DBG(CAM_ISP, "Enter: State %d, Substate[%s], evt id %d, ctx:%u link: 0x%x",
  7808. ctx->state, __cam_isp_ctx_substate_val_to_type(
  7809. ctx_isp->substate_activated), evt_id,
  7810. ctx->ctx_id, ctx->link_hdl);
  7811. irq_ops = &ctx_isp->substate_machine_irq[ctx_isp->substate_activated];
  7812. if (irq_ops->irq_ops[evt_id]) {
  7813. rc = irq_ops->irq_ops[evt_id](ctx_isp, evt_data);
  7814. } else {
  7815. CAM_DBG(CAM_ISP,
  7816. "No handle function for Substate[%s], evt id %d, ctx:%u link: 0x%x",
  7817. __cam_isp_ctx_substate_val_to_type(
  7818. ctx_isp->substate_activated), evt_id,
  7819. ctx->ctx_id, ctx->link_hdl);
  7820. if (isp_ctx_debug.enable_state_monitor_dump)
  7821. __cam_isp_ctx_dump_state_monitor_array(ctx_isp);
  7822. }
  7823. if ((evt_id == CAM_ISP_HW_EVENT_SOF) ||
  7824. (evt_id == CAM_ISP_HW_EVENT_EOF) ||
  7825. (evt_id == CAM_ISP_HW_EVENT_EPOCH))
  7826. __cam_isp_ctx_update_frame_timing_record(evt_id, ctx_isp);
  7827. __cam_isp_ctx_notify_cpas(ctx, evt_id);
  7828. CAM_DBG(CAM_ISP, "Exit: State %d Substate[%s], ctx: %u link: 0x%x",
  7829. ctx->state, __cam_isp_ctx_substate_val_to_type(
  7830. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  7831. spin_unlock(&ctx->lock);
  7832. return rc;
  7833. }
  7834. static int cam_isp_context_validate_event_notify_injection(struct cam_context *ctx,
  7835. struct cam_hw_inject_evt_param *evt_params)
  7836. {
  7837. int rc = 0;
  7838. uint32_t evt_type;
  7839. uint64_t req_id;
  7840. req_id = evt_params->req_id;
  7841. evt_type = evt_params->u.evt_notify.evt_notify_type;
  7842. switch (evt_type) {
  7843. case V4L_EVENT_CAM_REQ_MGR_ERROR: {
  7844. struct cam_hw_inject_err_evt_param *err_evt_params =
  7845. &evt_params->u.evt_notify.u.err_evt_params;
  7846. switch (err_evt_params->err_type) {
  7847. case CAM_REQ_MGR_ERROR_TYPE_RECOVERY:
  7848. case CAM_REQ_MGR_ERROR_TYPE_SOF_FREEZE:
  7849. case CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY:
  7850. case CAM_REQ_MGR_WARN_TYPE_KMD_RECOVERY:
  7851. break;
  7852. default:
  7853. CAM_ERR(CAM_ISP,
  7854. "Invalid error type: %u for error event injection err type: %u req id: %llu ctx id: %u link: 0x%x dev hdl: %d",
  7855. err_evt_params->err_type, err_evt_params->err_code,
  7856. req_id, ctx->ctx_id, ctx->link_hdl, ctx->dev_hdl);
  7857. return -EINVAL;
  7858. }
  7859. CAM_INFO(CAM_ISP,
  7860. "Inject ERR evt: err code: %u err type: %u req id: %llu ctx id: %u link: 0x%x dev hdl: %d",
  7861. err_evt_params->err_code, err_evt_params->err_type,
  7862. req_id, ctx->ctx_id, ctx->link_hdl, ctx->dev_hdl);
  7863. break;
  7864. }
  7865. case V4L_EVENT_CAM_REQ_MGR_PF_ERROR: {
  7866. struct cam_hw_inject_pf_evt_param *pf_evt_params =
  7867. &evt_params->u.evt_notify.u.pf_evt_params;
  7868. bool non_fatal_en;
  7869. rc = cam_smmu_is_cb_non_fatal_fault_en(ctx->img_iommu_hdl, &non_fatal_en);
  7870. if (rc) {
  7871. CAM_ERR(CAM_ISP,
  7872. "Fail to query whether device's cb has non-fatal enabled rc:%d, ctx id: %u link: 0x%x",
  7873. rc, ctx->ctx_id, ctx->link_hdl);
  7874. return rc;
  7875. }
  7876. if (!non_fatal_en) {
  7877. CAM_ERR(CAM_ISP,
  7878. "Fail to inject pagefault event notif. Pagefault fatal for ISP,ctx:%u link:0x%x",
  7879. ctx->ctx_id, ctx->link_hdl);
  7880. return -EINVAL;
  7881. }
  7882. CAM_INFO(CAM_ISP,
  7883. "Inject PF evt: req_id:%llu ctx:%u link:0x%x dev hdl:%d ctx found:%hhu",
  7884. req_id, ctx->ctx_id, ctx->link_hdl, ctx->dev_hdl,
  7885. pf_evt_params->ctx_found);
  7886. break;
  7887. }
  7888. default:
  7889. CAM_ERR(CAM_ISP, "Event notification type not supported: %u, ctx: %u link: 0x%x",
  7890. evt_type, ctx->ctx_id, ctx->link_hdl);
  7891. rc = -EINVAL;
  7892. }
  7893. return rc;
  7894. }
  7895. static int cam_isp_context_inject_evt(void *context, void *evt_args)
  7896. {
  7897. struct cam_context *ctx = context;
  7898. struct cam_isp_context *ctx_isp = NULL;
  7899. struct cam_hw_inject_evt_param *evt_params = evt_args;
  7900. int rc = 0;
  7901. if (!ctx || !evt_args) {
  7902. CAM_ERR(CAM_ISP,
  7903. "Invalid params ctx %s event args %s",
  7904. CAM_IS_NULL_TO_STR(ctx), CAM_IS_NULL_TO_STR(evt_args));
  7905. return -EINVAL;
  7906. }
  7907. ctx_isp = ctx->ctx_priv;
  7908. if (evt_params->inject_id == CAM_COMMON_EVT_INJECT_NOTIFY_EVENT_TYPE) {
  7909. rc = cam_isp_context_validate_event_notify_injection(ctx, evt_params);
  7910. if (rc) {
  7911. CAM_ERR(CAM_ISP,
  7912. "Event notif injection failed validation rc:%d, ctx:%u link:0x%x",
  7913. rc, ctx->ctx_id, ctx->link_hdl);
  7914. return rc;
  7915. }
  7916. } else {
  7917. CAM_ERR(CAM_ISP, "Bufdone err injection %u not supported by ISP,ctx:%u link:0x%x",
  7918. evt_params->inject_id, ctx->ctx_id, ctx->link_hdl);
  7919. return -EINVAL;
  7920. }
  7921. memcpy(&ctx_isp->evt_inject_params, evt_params,
  7922. sizeof(struct cam_hw_inject_evt_param));
  7923. ctx_isp->evt_inject_params.is_valid = true;
  7924. return rc;
  7925. }
  7926. /* top state machine */
  7927. static struct cam_ctx_ops
  7928. cam_isp_ctx_top_state_machine[CAM_CTX_STATE_MAX] = {
  7929. /* Uninit */
  7930. {
  7931. .ioctl_ops = {},
  7932. .crm_ops = {},
  7933. .irq_ops = NULL,
  7934. },
  7935. /* Available */
  7936. {
  7937. .ioctl_ops = {
  7938. .acquire_dev = __cam_isp_ctx_acquire_dev_in_available,
  7939. },
  7940. .crm_ops = {},
  7941. .irq_ops = NULL,
  7942. },
  7943. /* Acquired */
  7944. {
  7945. .ioctl_ops = {
  7946. .acquire_hw = __cam_isp_ctx_acquire_hw_in_acquired,
  7947. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  7948. .config_dev = __cam_isp_ctx_config_dev_in_acquired,
  7949. .flush_dev = __cam_isp_ctx_flush_dev_in_top_state,
  7950. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  7951. },
  7952. .crm_ops = {
  7953. .link = __cam_isp_ctx_link_in_acquired,
  7954. .unlink = __cam_isp_ctx_unlink_in_acquired,
  7955. .get_dev_info = __cam_isp_ctx_get_dev_info,
  7956. .process_evt = __cam_isp_ctx_process_evt,
  7957. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  7958. .dump_req = __cam_isp_ctx_dump_in_top_state,
  7959. },
  7960. .irq_ops = NULL,
  7961. .pagefault_ops = cam_isp_context_dump_requests,
  7962. .dumpinfo_ops = cam_isp_context_info_dump,
  7963. .evt_inject_ops = cam_isp_context_inject_evt,
  7964. },
  7965. /* Ready */
  7966. {
  7967. .ioctl_ops = {
  7968. .start_dev = __cam_isp_ctx_start_dev_in_ready,
  7969. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  7970. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  7971. .flush_dev = __cam_isp_ctx_flush_dev_in_top_state,
  7972. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  7973. },
  7974. .crm_ops = {
  7975. .unlink = __cam_isp_ctx_unlink_in_ready,
  7976. .get_dev_info = __cam_isp_ctx_get_dev_info,
  7977. .flush_req = __cam_isp_ctx_flush_req_in_ready,
  7978. .dump_req = __cam_isp_ctx_dump_in_top_state,
  7979. },
  7980. .irq_ops = NULL,
  7981. .pagefault_ops = cam_isp_context_dump_requests,
  7982. .dumpinfo_ops = cam_isp_context_info_dump,
  7983. .evt_inject_ops = cam_isp_context_inject_evt,
  7984. },
  7985. /* Flushed */
  7986. {
  7987. .ioctl_ops = {
  7988. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  7989. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  7990. .config_dev = __cam_isp_ctx_config_dev_in_flushed,
  7991. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  7992. },
  7993. .crm_ops = {
  7994. .unlink = __cam_isp_ctx_unlink_in_ready,
  7995. .process_evt = __cam_isp_ctx_process_evt,
  7996. .flush_req = __cam_isp_ctx_flush_req_in_flushed_state,
  7997. },
  7998. .irq_ops = NULL,
  7999. .pagefault_ops = cam_isp_context_dump_requests,
  8000. .dumpinfo_ops = cam_isp_context_info_dump,
  8001. .evt_inject_ops = cam_isp_context_inject_evt,
  8002. .msg_cb_ops = cam_isp_context_handle_message,
  8003. },
  8004. /* Activated */
  8005. {
  8006. .ioctl_ops = {
  8007. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  8008. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  8009. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  8010. .flush_dev = __cam_isp_ctx_flush_dev_in_top_state,
  8011. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  8012. },
  8013. .crm_ops = {
  8014. .unlink = __cam_isp_ctx_unlink_in_activated,
  8015. .apply_req = __cam_isp_ctx_apply_req,
  8016. .notify_frame_skip =
  8017. __cam_isp_ctx_apply_default_settings,
  8018. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  8019. .process_evt = __cam_isp_ctx_process_evt,
  8020. .dump_req = __cam_isp_ctx_dump_in_top_state,
  8021. },
  8022. .irq_ops = __cam_isp_ctx_handle_irq_in_activated,
  8023. .pagefault_ops = cam_isp_context_dump_requests,
  8024. .dumpinfo_ops = cam_isp_context_info_dump,
  8025. .recovery_ops = cam_isp_context_hw_recovery,
  8026. .evt_inject_ops = cam_isp_context_inject_evt,
  8027. .msg_cb_ops = cam_isp_context_handle_message,
  8028. },
  8029. };
  8030. static int cam_isp_context_hw_recovery(void *priv, void *data)
  8031. {
  8032. struct cam_context *ctx = priv;
  8033. int rc = -EPERM;
  8034. if (ctx->hw_mgr_intf->hw_recovery)
  8035. rc = ctx->hw_mgr_intf->hw_recovery(ctx->hw_mgr_intf->hw_mgr_priv, data);
  8036. else
  8037. CAM_ERR(CAM_ISP, "hw mgr doesn't support recovery, ctx_idx: %u, link: 0x%x",
  8038. ctx->ctx_id, ctx->link_hdl);
  8039. return rc;
  8040. }
  8041. static void cam_isp_context_find_faulted_context(struct cam_context *ctx,
  8042. struct list_head *req_list, struct cam_hw_dump_pf_args *pf_args, bool *found)
  8043. {
  8044. struct cam_ctx_request *req = NULL;
  8045. struct cam_ctx_request *req_temp = NULL;
  8046. int rc;
  8047. *found = false;
  8048. list_for_each_entry_safe(req, req_temp, req_list, list) {
  8049. CAM_INFO(CAM_ISP, "List req_id: %llu ctx id: %u link: 0x%x",
  8050. req->request_id, ctx->ctx_id, ctx->link_hdl);
  8051. rc = cam_context_dump_pf_info_to_hw(ctx, pf_args, &req->pf_data);
  8052. if (rc)
  8053. CAM_ERR(CAM_ISP, "Failed to dump pf info, ctx_idx: %u, link: 0x%x",
  8054. ctx->ctx_id, ctx->link_hdl);
  8055. /*
  8056. * Found faulted buffer. Even if faulted ctx is found, but
  8057. * continue to search for faulted buffer
  8058. */
  8059. if (pf_args->pf_context_info.mem_type != CAM_FAULT_BUF_NOT_FOUND) {
  8060. *found = true;
  8061. break;
  8062. }
  8063. }
  8064. }
  8065. static int cam_isp_context_dump_requests(void *data, void *args)
  8066. {
  8067. struct cam_context *ctx = (struct cam_context *)data;
  8068. struct cam_isp_context *ctx_isp;
  8069. struct cam_hw_dump_pf_args *pf_args = (struct cam_hw_dump_pf_args *)args;
  8070. int rc = 0;
  8071. bool found;
  8072. if (!ctx || !pf_args) {
  8073. CAM_ERR(CAM_ISP, "Invalid ctx %pK or pf args %pK",
  8074. ctx, pf_args);
  8075. return -EINVAL;
  8076. }
  8077. ctx_isp = (struct cam_isp_context *)ctx->ctx_priv;
  8078. if (!ctx_isp) {
  8079. CAM_ERR(CAM_ISP, "Invalid isp ctx");
  8080. return -EINVAL;
  8081. }
  8082. if (pf_args->handle_sec_pf)
  8083. goto end;
  8084. CAM_INFO(CAM_ISP,
  8085. "Iterating over active list for isp ctx %u link: 0x%x state %d",
  8086. ctx->ctx_id, ctx->link_hdl, ctx->state);
  8087. cam_isp_context_find_faulted_context(ctx, &ctx->active_req_list,
  8088. pf_args, &found);
  8089. if (found)
  8090. goto end;
  8091. CAM_INFO(CAM_ISP,
  8092. "Iterating over waiting list of isp ctx %u link: 0x%x state %d",
  8093. ctx->ctx_id, ctx->link_hdl, ctx->state);
  8094. cam_isp_context_find_faulted_context(ctx, &ctx->wait_req_list,
  8095. pf_args, &found);
  8096. if (found)
  8097. goto end;
  8098. /*
  8099. * In certain scenarios we observe both overflow and SMMU pagefault
  8100. * for a particular request. If overflow is handled before page fault
  8101. * we need to traverse through pending request list because if
  8102. * bubble recovery is enabled on any request we move that request
  8103. * and all the subsequent requests to the pending list while handling
  8104. * overflow error.
  8105. */
  8106. CAM_INFO(CAM_ISP,
  8107. "Iterating over pending req list of isp ctx %u link: 0x%x state %d",
  8108. ctx->ctx_id, ctx->link_hdl, ctx->state);
  8109. cam_isp_context_find_faulted_context(ctx, &ctx->pending_req_list,
  8110. pf_args, &found);
  8111. if (found)
  8112. goto end;
  8113. end:
  8114. if (pf_args->pf_context_info.resource_type) {
  8115. ctx_isp = (struct cam_isp_context *)ctx->ctx_priv;
  8116. CAM_INFO(CAM_ISP,
  8117. "Page fault on resource:%s (0x%x) ctx id:%u link: 0x%x frame id:%d reported id:%lld applied id:%lld",
  8118. __cam_isp_resource_handle_id_to_type(ctx_isp->isp_device_type,
  8119. pf_args->pf_context_info.resource_type),
  8120. pf_args->pf_context_info.resource_type,
  8121. ctx->ctx_id, ctx->link_hdl, ctx_isp->frame_id,
  8122. ctx_isp->reported_req_id, ctx_isp->last_applied_req_id);
  8123. }
  8124. /*
  8125. * Send PF notification to UMD if PF found on current CTX
  8126. * or it is forced to send PF notification to UMD even if no
  8127. * faulted context found
  8128. */
  8129. if (pf_args->pf_context_info.ctx_found ||
  8130. pf_args->pf_context_info.force_send_pf_evt)
  8131. rc = cam_context_send_pf_evt(ctx, pf_args);
  8132. if (rc)
  8133. CAM_ERR(CAM_ISP,
  8134. "Failed to notify PF event to userspace rc: %d, ctx id:%u link: 0x%x",
  8135. rc, ctx->ctx_id, ctx->link_hdl);
  8136. return rc;
  8137. }
  8138. static int cam_isp_context_handle_message(void *context,
  8139. uint32_t msg_type, void *data)
  8140. {
  8141. int rc = -EINVAL;
  8142. struct cam_hw_cmd_args hw_cmd_args = {0};
  8143. struct cam_isp_hw_cmd_args isp_hw_cmd_args = {0};
  8144. struct cam_context *ctx = (struct cam_context *)context;
  8145. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  8146. switch (msg_type) {
  8147. case CAM_SUBDEV_MESSAGE_CLOCK_UPDATE:
  8148. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  8149. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_UPDATE_CLOCK;
  8150. isp_hw_cmd_args.cmd_data = data;
  8151. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  8152. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  8153. &hw_cmd_args);
  8154. if (rc)
  8155. CAM_ERR(CAM_ISP, "Update clock rate failed rc: %d", rc);
  8156. break;
  8157. default:
  8158. CAM_ERR(CAM_ISP, "Invalid message type %d", msg_type);
  8159. }
  8160. return rc;
  8161. }
  8162. static int cam_isp_context_debug_register(void)
  8163. {
  8164. int rc = 0;
  8165. struct dentry *dbgfileptr = NULL;
  8166. if (!cam_debugfs_available())
  8167. return 0;
  8168. rc = cam_debugfs_create_subdir("isp_ctx", &dbgfileptr);
  8169. if (rc) {
  8170. CAM_ERR(CAM_ISP, "DebugFS could not create directory!");
  8171. return rc;
  8172. }
  8173. /* Store parent inode for cleanup in caller */
  8174. isp_ctx_debug.dentry = dbgfileptr;
  8175. debugfs_create_u32("enable_state_monitor_dump", 0644,
  8176. isp_ctx_debug.dentry, &isp_ctx_debug.enable_state_monitor_dump);
  8177. debugfs_create_u8("enable_cdm_cmd_buffer_dump", 0644,
  8178. isp_ctx_debug.dentry, &isp_ctx_debug.enable_cdm_cmd_buff_dump);
  8179. debugfs_create_u32("disable_internal_recovery_mask", 0644,
  8180. isp_ctx_debug.dentry, &isp_ctx_debug.disable_internal_recovery_mask);
  8181. return 0;
  8182. }
  8183. int cam_isp_context_init(struct cam_isp_context *ctx,
  8184. struct cam_context *ctx_base,
  8185. struct cam_req_mgr_kmd_ops *crm_node_intf,
  8186. struct cam_hw_mgr_intf *hw_intf,
  8187. uint32_t ctx_id,
  8188. uint32_t isp_device_type,
  8189. int img_iommu_hdl)
  8190. {
  8191. int rc = -1;
  8192. int i;
  8193. struct cam_isp_skip_frame_info *skip_info, *temp;
  8194. if (!ctx || !ctx_base) {
  8195. CAM_ERR(CAM_ISP, "Invalid Context");
  8196. goto err;
  8197. }
  8198. /* ISP context setup */
  8199. memset(ctx, 0, sizeof(*ctx));
  8200. ctx->base = ctx_base;
  8201. ctx->frame_id = 0;
  8202. ctx->custom_enabled = false;
  8203. ctx->use_frame_header_ts = false;
  8204. ctx->use_default_apply = false;
  8205. ctx->active_req_cnt = 0;
  8206. ctx->reported_req_id = 0;
  8207. ctx->bubble_frame_cnt = 0;
  8208. ctx->congestion_cnt = 0;
  8209. ctx->req_info.last_bufdone_req_id = 0;
  8210. ctx->v4l2_event_sub_ids = 0;
  8211. ctx->hw_ctx = NULL;
  8212. ctx->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  8213. ctx->substate_machine = cam_isp_ctx_activated_state_machine;
  8214. ctx->substate_machine_irq = cam_isp_ctx_activated_state_machine_irq;
  8215. ctx->init_timestamp = jiffies_to_msecs(jiffies);
  8216. ctx->isp_device_type = isp_device_type;
  8217. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  8218. ctx->req_base[i].req_priv = &ctx->req_isp[i];
  8219. ctx->req_isp[i].base = &ctx->req_base[i];
  8220. }
  8221. /* camera context setup */
  8222. rc = cam_context_init(ctx_base, isp_dev_name, CAM_ISP, ctx_id,
  8223. crm_node_intf, hw_intf, ctx->req_base, CAM_ISP_CTX_REQ_MAX, img_iommu_hdl);
  8224. if (rc) {
  8225. CAM_ERR(CAM_ISP, "Camera Context Base init failed, ctx_idx: %u, link: 0x%x",
  8226. ctx_base->ctx_id, ctx_base->link_hdl);
  8227. goto free_mem;
  8228. }
  8229. /* FCG related struct setup */
  8230. INIT_LIST_HEAD(&ctx->fcg_tracker.skipped_list);
  8231. for (i = 0; i < CAM_ISP_AFD_PIPELINE_DELAY; i++) {
  8232. skip_info = kzalloc(sizeof(struct cam_isp_skip_frame_info), GFP_KERNEL);
  8233. if (!skip_info) {
  8234. CAM_ERR(CAM_ISP,
  8235. "Failed to allocate memory for FCG struct, ctx_idx: %u, link: %x",
  8236. ctx_base->ctx_id, ctx_base->link_hdl);
  8237. rc = -ENOMEM;
  8238. goto free_mem;
  8239. }
  8240. list_add_tail(&skip_info->list, &ctx->fcg_tracker.skipped_list);
  8241. }
  8242. /* link camera context with isp context */
  8243. ctx_base->state_machine = cam_isp_ctx_top_state_machine;
  8244. ctx_base->ctx_priv = ctx;
  8245. /* initializing current state for error logging */
  8246. for (i = 0; i < CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES; i++) {
  8247. ctx->dbg_monitors.state_monitor[i].curr_state =
  8248. CAM_ISP_CTX_ACTIVATED_MAX;
  8249. }
  8250. atomic64_set(&ctx->dbg_monitors.state_monitor_head, -1);
  8251. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  8252. atomic64_set(&ctx->dbg_monitors.event_record_head[i], -1);
  8253. atomic64_set(&ctx->dbg_monitors.frame_monitor_head, -1);
  8254. if (!isp_ctx_debug.dentry)
  8255. cam_isp_context_debug_register();
  8256. return rc;
  8257. free_mem:
  8258. list_for_each_entry_safe(skip_info, temp,
  8259. &ctx->fcg_tracker.skipped_list, list) {
  8260. list_del(&skip_info->list);
  8261. kfree(skip_info);
  8262. skip_info = NULL;
  8263. }
  8264. err:
  8265. return rc;
  8266. }
  8267. int cam_isp_context_deinit(struct cam_isp_context *ctx)
  8268. {
  8269. struct cam_isp_skip_frame_info *skip_info, *temp;
  8270. list_for_each_entry_safe(skip_info, temp,
  8271. &ctx->fcg_tracker.skipped_list, list) {
  8272. list_del(&skip_info->list);
  8273. kfree(skip_info);
  8274. skip_info = NULL;
  8275. }
  8276. if (ctx->base)
  8277. cam_context_deinit(ctx->base);
  8278. if (ctx->substate_activated != CAM_ISP_CTX_ACTIVATED_SOF)
  8279. CAM_ERR(CAM_ISP, "ISP context Substate[%s] is invalid",
  8280. __cam_isp_ctx_substate_val_to_type(
  8281. ctx->substate_activated));
  8282. isp_ctx_debug.dentry = NULL;
  8283. memset(ctx, 0, sizeof(*ctx));
  8284. return 0;
  8285. }