cam_isp_context.c 265 KB

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