cam_isp_context.c 206 KB

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