cam_isp_context.c 251 KB

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