cam_isp_context.c 213 KB

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