cam_isp_context.c 179 KB

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