cam_isp_context.c 224 KB

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