cam_isp_context.c 276 KB

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