cam_isp_context.c 240 KB

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