cam_isp_context.c 288 KB

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