cam_isp_context.c 291 KB

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