cam_isp_context.c 221 KB

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