cam_isp_context.c 161 KB

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