cam_isp_context.c 150 KB

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