cam_isp_context.c 175 KB

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