cam_isp_context.c 231 KB

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