cam_isp_context.c 190 KB

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