cam_isp_context.c 161 KB

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