cam_isp_context.c 190 KB

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