cam_isp_context.c 214 KB

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