cam_isp_context.c 266 KB

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