cam_isp_context.c 227 KB

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