cam_isp_context.c 275 KB

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