cam_isp_context.c 151 KB

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