cam_isp_context.c 206 KB

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