cam_isp_context.c 272 KB

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