cam_isp_context.c 242 KB

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