cam_isp_context.c 147 KB

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