cam_isp_context.c 267 KB

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