cam_isp_context.c 190 KB

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