cam_isp_context.c 271 KB

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