cam_isp_context.c 235 KB

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