cam_isp_context.c 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2019, 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_STATE_MONITOR_HEAD(head) \
  23. (atomic64_add_return(1, head) % \
  24. CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES)
  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 void __cam_isp_ctx_update_state_monitor_array(
  30. struct cam_isp_context *ctx_isp,
  31. enum cam_isp_state_change_trigger trigger_type,
  32. uint64_t req_id)
  33. {
  34. int iterator = INC_STATE_MONITOR_HEAD(&ctx_isp->state_monitor_head);
  35. ctx_isp->cam_isp_ctx_state_monitor[iterator].curr_state =
  36. ctx_isp->substate_activated;
  37. ctx_isp->cam_isp_ctx_state_monitor[iterator].frame_id =
  38. ctx_isp->frame_id;
  39. ctx_isp->cam_isp_ctx_state_monitor[iterator].trigger =
  40. trigger_type;
  41. ctx_isp->cam_isp_ctx_state_monitor[iterator].req_id =
  42. req_id;
  43. ctx_isp->cam_isp_ctx_state_monitor[iterator].evt_time_stamp =
  44. jiffies_to_msecs(jiffies) - ctx_isp->init_timestamp;
  45. }
  46. static const char *__cam_isp_ctx_substate_val_to_type(
  47. uint32_t type)
  48. {
  49. switch (type) {
  50. case CAM_ISP_CTX_ACTIVATED_SOF:
  51. return "SOF";
  52. case CAM_ISP_CTX_ACTIVATED_APPLIED:
  53. return "APPLIED";
  54. case CAM_ISP_CTX_ACTIVATED_EPOCH:
  55. return "EPOCH";
  56. case CAM_ISP_CTX_ACTIVATED_BUBBLE:
  57. return "BUBBLE";
  58. case CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED:
  59. return "BUBBLE_APPLIED";
  60. case CAM_ISP_CTX_ACTIVATED_HW_ERROR:
  61. return "HW_ERROR";
  62. case CAM_ISP_CTX_ACTIVATED_HALT:
  63. return "HALT";
  64. default:
  65. return "CAM_ISP_CTX_INVALID_STATE";
  66. }
  67. }
  68. static const char *__cam_isp_hw_evt_val_to_type(
  69. uint32_t evt_id)
  70. {
  71. switch (evt_id) {
  72. case CAM_ISP_STATE_CHANGE_TRIGGER_ERROR:
  73. return "ERROR";
  74. case CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED:
  75. return "APPLIED";
  76. case CAM_ISP_STATE_CHANGE_TRIGGER_SOF:
  77. return "SOF";
  78. case CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE:
  79. return "REG_UPDATE";
  80. case CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH:
  81. return "EPOCH";
  82. case CAM_ISP_STATE_CHANGE_TRIGGER_EOF:
  83. return "EOF";
  84. case CAM_ISP_STATE_CHANGE_TRIGGER_DONE:
  85. return "DONE";
  86. case CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH:
  87. return "FLUSH";
  88. default:
  89. return "CAM_ISP_EVENT_INVALID";
  90. }
  91. }
  92. static void __cam_isp_ctx_dump_state_monitor_array(
  93. struct cam_isp_context *ctx_isp)
  94. {
  95. int i = 0;
  96. int64_t state_head = 0;
  97. uint32_t index, num_entries, oldest_entry;
  98. state_head = atomic64_read(&ctx_isp->state_monitor_head);
  99. if (state_head == -1) {
  100. return;
  101. } else if (state_head < CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES) {
  102. num_entries = state_head;
  103. oldest_entry = 0;
  104. } else {
  105. num_entries = CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES;
  106. oldest_entry = (state_head + 1) %
  107. CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES;
  108. }
  109. CAM_ERR(CAM_ISP,
  110. "Dumping state information for preceding requests");
  111. index = oldest_entry;
  112. for (i = 0; i < num_entries; i++) {
  113. CAM_ERR(CAM_ISP,
  114. "Index[%d] time[%d] : State[%s] Frame[%lld] ReqId[%llu] evt_type[%s]",
  115. index,
  116. ctx_isp->cam_isp_ctx_state_monitor[index].evt_time_stamp,
  117. __cam_isp_ctx_substate_val_to_type(
  118. ctx_isp->cam_isp_ctx_state_monitor[index].curr_state),
  119. ctx_isp->cam_isp_ctx_state_monitor[index].frame_id,
  120. ctx_isp->cam_isp_ctx_state_monitor[index].req_id,
  121. __cam_isp_hw_evt_val_to_type(
  122. ctx_isp->cam_isp_ctx_state_monitor[index].trigger));
  123. index = (index + 1) % CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES;
  124. }
  125. }
  126. static int cam_isp_context_info_dump(void *context,
  127. enum cam_context_dump_id id)
  128. {
  129. struct cam_context *ctx = (struct cam_context *)context;
  130. switch (id) {
  131. case CAM_CTX_DUMP_ACQ_INFO: {
  132. cam_context_dump_hw_acq_info(ctx);
  133. break;
  134. }
  135. default:
  136. CAM_DBG(CAM_ISP, "DUMP id not valid %u", id);
  137. break;
  138. }
  139. return 0;
  140. }
  141. static void cam_isp_ctx_dump_req(struct cam_isp_ctx_req *req_isp)
  142. {
  143. int i = 0, rc = 0;
  144. size_t len = 0;
  145. uint32_t *buf_addr;
  146. uint32_t *buf_start, *buf_end;
  147. size_t remain_len = 0;
  148. for (i = 0; i < req_isp->num_cfg; i++) {
  149. rc = cam_packet_util_get_cmd_mem_addr(
  150. req_isp->cfg[i].handle, &buf_addr, &len);
  151. if (rc) {
  152. CAM_ERR_RATE_LIMIT(CAM_ISP,
  153. "Failed to get_cmd_mem_addr, rc=%d",
  154. rc);
  155. } else {
  156. if (req_isp->cfg[i].offset >= ((uint32_t)len)) {
  157. CAM_ERR(CAM_ISP,
  158. "Invalid offset exp %u actual %u",
  159. req_isp->cfg[i].offset, (uint32_t)len);
  160. return;
  161. }
  162. remain_len = len - req_isp->cfg[i].offset;
  163. if (req_isp->cfg[i].len >
  164. ((uint32_t)remain_len)) {
  165. CAM_ERR(CAM_ISP,
  166. "Invalid len exp %u remain_len %u",
  167. req_isp->cfg[i].len,
  168. (uint32_t)remain_len);
  169. return;
  170. }
  171. buf_start = (uint32_t *)((uint8_t *) buf_addr +
  172. req_isp->cfg[i].offset);
  173. buf_end = (uint32_t *)((uint8_t *) buf_start +
  174. req_isp->cfg[i].len - 1);
  175. cam_cdm_util_dump_cmd_buf(buf_start, buf_end);
  176. }
  177. }
  178. }
  179. static int __cam_isp_ctx_enqueue_request_in_order(
  180. struct cam_context *ctx, struct cam_ctx_request *req)
  181. {
  182. struct cam_ctx_request *req_current;
  183. struct cam_ctx_request *req_prev;
  184. struct list_head temp_list;
  185. INIT_LIST_HEAD(&temp_list);
  186. spin_lock_bh(&ctx->lock);
  187. if (list_empty(&ctx->pending_req_list)) {
  188. list_add_tail(&req->list, &ctx->pending_req_list);
  189. } else {
  190. list_for_each_entry_safe_reverse(
  191. req_current, req_prev, &ctx->pending_req_list, list) {
  192. if (req->request_id < req_current->request_id) {
  193. list_del_init(&req_current->list);
  194. list_add(&req_current->list, &temp_list);
  195. continue;
  196. } else if (req->request_id == req_current->request_id) {
  197. CAM_WARN(CAM_ISP,
  198. "Received duplicated request %lld",
  199. req->request_id);
  200. }
  201. break;
  202. }
  203. list_add_tail(&req->list, &ctx->pending_req_list);
  204. if (!list_empty(&temp_list)) {
  205. list_for_each_entry_safe(
  206. req_current, req_prev, &temp_list, list) {
  207. list_del_init(&req_current->list);
  208. list_add_tail(&req_current->list,
  209. &ctx->pending_req_list);
  210. }
  211. }
  212. }
  213. spin_unlock_bh(&ctx->lock);
  214. return 0;
  215. }
  216. static int __cam_isp_ctx_enqueue_init_request(
  217. struct cam_context *ctx, struct cam_ctx_request *req)
  218. {
  219. int rc = 0;
  220. struct cam_ctx_request *req_old;
  221. struct cam_isp_ctx_req *req_isp_old;
  222. struct cam_isp_ctx_req *req_isp_new;
  223. spin_lock_bh(&ctx->lock);
  224. if (list_empty(&ctx->pending_req_list)) {
  225. list_add_tail(&req->list, &ctx->pending_req_list);
  226. CAM_DBG(CAM_ISP, "INIT packet added req id= %d",
  227. req->request_id);
  228. goto end;
  229. }
  230. req_old = list_first_entry(&ctx->pending_req_list,
  231. struct cam_ctx_request, list);
  232. req_isp_old = (struct cam_isp_ctx_req *) req_old->req_priv;
  233. req_isp_new = (struct cam_isp_ctx_req *) req->req_priv;
  234. if (req_isp_old->hw_update_data.packet_opcode_type ==
  235. CAM_ISP_PACKET_INIT_DEV) {
  236. if ((req_isp_old->num_cfg + req_isp_new->num_cfg) >=
  237. CAM_ISP_CTX_CFG_MAX) {
  238. CAM_WARN(CAM_ISP, "Can not merge INIT pkt");
  239. rc = -ENOMEM;
  240. }
  241. if (req_isp_old->num_fence_map_out != 0 ||
  242. req_isp_old->num_fence_map_in != 0) {
  243. CAM_WARN(CAM_ISP, "Invalid INIT pkt sequence");
  244. rc = -EINVAL;
  245. }
  246. if (!rc) {
  247. memcpy(req_isp_old->fence_map_out,
  248. req_isp_new->fence_map_out,
  249. sizeof(req_isp_new->fence_map_out[0])*
  250. req_isp_new->num_fence_map_out);
  251. req_isp_old->num_fence_map_out =
  252. req_isp_new->num_fence_map_out;
  253. memcpy(req_isp_old->fence_map_in,
  254. req_isp_new->fence_map_in,
  255. sizeof(req_isp_new->fence_map_in[0])*
  256. req_isp_new->num_fence_map_in);
  257. req_isp_old->num_fence_map_in =
  258. req_isp_new->num_fence_map_in;
  259. memcpy(&req_isp_old->cfg[req_isp_old->num_cfg],
  260. req_isp_new->cfg,
  261. sizeof(req_isp_new->cfg[0])*
  262. req_isp_new->num_cfg);
  263. req_isp_old->num_cfg += req_isp_new->num_cfg;
  264. memcpy(&req_old->pf_data, &req->pf_data,
  265. sizeof(struct cam_hw_mgr_dump_pf_data));
  266. req_old->request_id = req->request_id;
  267. list_add_tail(&req->list, &ctx->free_req_list);
  268. }
  269. } else {
  270. CAM_WARN(CAM_ISP,
  271. "Received Update pkt before INIT pkt. req_id= %lld",
  272. req->request_id);
  273. rc = -EINVAL;
  274. }
  275. end:
  276. spin_unlock_bh(&ctx->lock);
  277. return rc;
  278. }
  279. static const char *__cam_isp_resource_handle_id_to_type(
  280. uint32_t resource_handle)
  281. {
  282. switch (resource_handle) {
  283. case CAM_ISP_IFE_OUT_RES_FULL:
  284. return "FULL";
  285. case CAM_ISP_IFE_OUT_RES_DS4:
  286. return "DS4";
  287. case CAM_ISP_IFE_OUT_RES_DS16:
  288. return "DS16";
  289. case CAM_ISP_IFE_OUT_RES_RAW_DUMP:
  290. return "RAW_DUMP";
  291. case CAM_ISP_IFE_OUT_RES_FD:
  292. return "FD";
  293. case CAM_ISP_IFE_OUT_RES_PDAF:
  294. return "PDAF";
  295. case CAM_ISP_IFE_OUT_RES_RDI_0:
  296. return "RDI_0";
  297. case CAM_ISP_IFE_OUT_RES_RDI_1:
  298. return "RDI_1";
  299. case CAM_ISP_IFE_OUT_RES_RDI_2:
  300. return "RDI_2";
  301. case CAM_ISP_IFE_OUT_RES_RDI_3:
  302. return "RDI_3";
  303. case CAM_ISP_IFE_OUT_RES_STATS_HDR_BE:
  304. return "STATS_HDR_BE";
  305. case CAM_ISP_IFE_OUT_RES_STATS_HDR_BHIST:
  306. return "STATS_HDR_BHIST";
  307. case CAM_ISP_IFE_OUT_RES_STATS_TL_BG:
  308. return "STATS_TL_BG";
  309. case CAM_ISP_IFE_OUT_RES_STATS_BF:
  310. return "STATS_BF";
  311. case CAM_ISP_IFE_OUT_RES_STATS_AWB_BG:
  312. return "STATS_AWB_BG";
  313. case CAM_ISP_IFE_OUT_RES_STATS_BHIST:
  314. return "STATS_BHIST";
  315. case CAM_ISP_IFE_OUT_RES_STATS_RS:
  316. return "STATS_RS";
  317. case CAM_ISP_IFE_OUT_RES_STATS_CS:
  318. return "STATS_CS";
  319. case CAM_ISP_IFE_OUT_RES_STATS_IHIST:
  320. return "STATS_IHIST";
  321. case CAM_ISP_IFE_OUT_RES_FULL_DISP:
  322. return "FULL_DISP";
  323. case CAM_ISP_IFE_OUT_RES_DS4_DISP:
  324. return "DS4_DISP";
  325. case CAM_ISP_IFE_OUT_RES_DS16_DISP:
  326. return "DS16_DISP";
  327. case CAM_ISP_IFE_OUT_RES_2PD:
  328. return "2PD";
  329. case CAM_ISP_IFE_OUT_RES_RDI_RD:
  330. return "RDI_RD";
  331. case CAM_ISP_IFE_OUT_RES_LCR:
  332. return "LCR";
  333. default:
  334. return "CAM_ISP_Invalid_Resource_Type";
  335. }
  336. }
  337. static uint64_t __cam_isp_ctx_get_event_ts(uint32_t evt_id, void *evt_data)
  338. {
  339. uint64_t ts = 0;
  340. if (!evt_data)
  341. return 0;
  342. switch (evt_id) {
  343. case CAM_ISP_HW_EVENT_ERROR:
  344. ts = ((struct cam_isp_hw_error_event_data *)evt_data)->
  345. timestamp;
  346. break;
  347. case CAM_ISP_HW_EVENT_SOF:
  348. ts = ((struct cam_isp_hw_sof_event_data *)evt_data)->
  349. timestamp;
  350. break;
  351. case CAM_ISP_HW_EVENT_REG_UPDATE:
  352. ts = ((struct cam_isp_hw_reg_update_event_data *)evt_data)->
  353. timestamp;
  354. break;
  355. case CAM_ISP_HW_EVENT_EPOCH:
  356. ts = ((struct cam_isp_hw_epoch_event_data *)evt_data)->
  357. timestamp;
  358. break;
  359. case CAM_ISP_HW_EVENT_EOF:
  360. ts = ((struct cam_isp_hw_eof_event_data *)evt_data)->
  361. timestamp;
  362. break;
  363. case CAM_ISP_HW_EVENT_DONE:
  364. break;
  365. default:
  366. CAM_DBG(CAM_ISP, "Invalid Event Type %d", evt_id);
  367. }
  368. return ts;
  369. }
  370. static void __cam_isp_ctx_send_sof_boot_timestamp(
  371. struct cam_isp_context *ctx_isp, uint64_t request_id,
  372. uint32_t sof_event_status)
  373. {
  374. struct cam_req_mgr_message req_msg;
  375. req_msg.session_hdl = ctx_isp->base->session_hdl;
  376. req_msg.u.frame_msg.frame_id = ctx_isp->frame_id;
  377. req_msg.u.frame_msg.request_id = request_id;
  378. req_msg.u.frame_msg.timestamp = ctx_isp->boot_timestamp;
  379. req_msg.u.frame_msg.link_hdl = ctx_isp->base->link_hdl;
  380. req_msg.u.frame_msg.sof_status = sof_event_status;
  381. CAM_DBG(CAM_ISP,
  382. "request id:%lld frame number:%lld boot time stamp:0x%llx",
  383. request_id, ctx_isp->frame_id,
  384. ctx_isp->boot_timestamp);
  385. if (cam_req_mgr_notify_message(&req_msg,
  386. V4L_EVENT_CAM_REQ_MGR_SOF_BOOT_TS,
  387. V4L_EVENT_CAM_REQ_MGR_EVENT))
  388. CAM_ERR(CAM_ISP,
  389. "Error in notifying the boot time for req id:%lld",
  390. request_id);
  391. }
  392. static void __cam_isp_ctx_send_sof_timestamp(
  393. struct cam_isp_context *ctx_isp, uint64_t request_id,
  394. uint32_t sof_event_status)
  395. {
  396. struct cam_req_mgr_message req_msg;
  397. req_msg.session_hdl = ctx_isp->base->session_hdl;
  398. req_msg.u.frame_msg.frame_id = ctx_isp->frame_id;
  399. req_msg.u.frame_msg.request_id = request_id;
  400. req_msg.u.frame_msg.timestamp = ctx_isp->sof_timestamp_val;
  401. req_msg.u.frame_msg.link_hdl = ctx_isp->base->link_hdl;
  402. req_msg.u.frame_msg.sof_status = sof_event_status;
  403. CAM_DBG(CAM_ISP,
  404. "request id:%lld frame number:%lld SOF time stamp:0x%llx",
  405. request_id, ctx_isp->frame_id,
  406. ctx_isp->sof_timestamp_val);
  407. CAM_DBG(CAM_ISP, "sof status:%d", sof_event_status);
  408. if (cam_req_mgr_notify_message(&req_msg,
  409. V4L_EVENT_CAM_REQ_MGR_SOF, V4L_EVENT_CAM_REQ_MGR_EVENT))
  410. CAM_ERR(CAM_ISP,
  411. "Error in notifying the sof time for req id:%lld",
  412. request_id);
  413. __cam_isp_ctx_send_sof_boot_timestamp(ctx_isp,
  414. request_id, sof_event_status);
  415. }
  416. static void __cam_isp_ctx_handle_buf_done_fail_log(
  417. uint64_t request_id, struct cam_isp_ctx_req *req_isp)
  418. {
  419. int i;
  420. if (req_isp->num_fence_map_out >= CAM_ISP_CTX_RES_MAX) {
  421. CAM_ERR(CAM_ISP,
  422. "Num Resources exceed mMAX %d >= %d ",
  423. req_isp->num_fence_map_out, CAM_ISP_CTX_RES_MAX);
  424. return;
  425. }
  426. CAM_WARN(CAM_ISP,
  427. "Prev Req[%lld] : num_out=%d, num_acked=%d, bubble : report=%d, detected=%d",
  428. request_id, req_isp->num_fence_map_out, req_isp->num_acked,
  429. req_isp->bubble_report, req_isp->bubble_detected);
  430. CAM_WARN(CAM_ISP,
  431. "Resource Handles that fail to generate buf_done in prev frame");
  432. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  433. if (req_isp->fence_map_out[i].sync_id != -1) {
  434. CAM_WARN(CAM_ISP,
  435. "Resource_Handle: [%s][0x%x] Sync_ID: [0x%x]",
  436. __cam_isp_resource_handle_id_to_type(
  437. req_isp->fence_map_out[i].resource_handle),
  438. req_isp->fence_map_out[i].resource_handle,
  439. req_isp->fence_map_out[i].sync_id);
  440. }
  441. }
  442. }
  443. static int __cam_isp_ctx_handle_buf_done_for_request(
  444. struct cam_isp_context *ctx_isp,
  445. struct cam_ctx_request *req,
  446. struct cam_isp_hw_done_event_data *done,
  447. uint32_t bubble_state,
  448. struct cam_isp_hw_done_event_data *done_next_req)
  449. {
  450. int rc = 0;
  451. int i, j;
  452. struct cam_isp_ctx_req *req_isp;
  453. struct cam_context *ctx = ctx_isp->base;
  454. uint64_t buf_done_req_id;
  455. trace_cam_buf_done("ISP", ctx, req);
  456. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  457. CAM_DBG(CAM_ISP, "Enter with bubble_state %d, req_bubble_detected %d",
  458. bubble_state, req_isp->bubble_detected);
  459. if (done_next_req) {
  460. done_next_req->num_handles = 0;
  461. done_next_req->timestamp = done->timestamp;
  462. }
  463. for (i = 0; i < done->num_handles; i++) {
  464. for (j = 0; j < req_isp->num_fence_map_out; j++) {
  465. if (done->resource_handle[i] ==
  466. req_isp->fence_map_out[j].resource_handle)
  467. break;
  468. }
  469. if (j == req_isp->num_fence_map_out) {
  470. CAM_ERR(CAM_ISP,
  471. "Can not find matching lane handle 0x%x!",
  472. done->resource_handle[i]);
  473. rc = -EINVAL;
  474. continue;
  475. }
  476. if (req_isp->fence_map_out[j].sync_id == -1) {
  477. CAM_WARN(CAM_ISP,
  478. "Duplicate BUF_DONE for req %lld : i=%d, j=%d, res=%s",
  479. req->request_id, i, j,
  480. __cam_isp_resource_handle_id_to_type(
  481. done->resource_handle[i]));
  482. if (done_next_req) {
  483. done_next_req->resource_handle
  484. [done_next_req->num_handles++] =
  485. done->resource_handle[i];
  486. }
  487. continue;
  488. }
  489. if (!req_isp->bubble_detected) {
  490. CAM_DBG(CAM_ISP,
  491. "Sync with success: req %lld res 0x%x fd 0x%x, ctx %u",
  492. req->request_id,
  493. req_isp->fence_map_out[j].resource_handle,
  494. req_isp->fence_map_out[j].sync_id,
  495. ctx->ctx_id);
  496. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  497. CAM_SYNC_STATE_SIGNALED_SUCCESS);
  498. if (rc)
  499. CAM_DBG(CAM_ISP, "Sync failed with rc = %d",
  500. rc);
  501. } else if (!req_isp->bubble_report) {
  502. CAM_ERR(CAM_ISP,
  503. "Sync with failure: req %lld res 0x%x fd 0x%x, ctx %u",
  504. req->request_id,
  505. req_isp->fence_map_out[j].resource_handle,
  506. req_isp->fence_map_out[j].sync_id,
  507. ctx->ctx_id);
  508. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  509. CAM_SYNC_STATE_SIGNALED_ERROR);
  510. if (rc)
  511. CAM_ERR(CAM_ISP, "Sync failed with rc = %d",
  512. rc);
  513. } else {
  514. /*
  515. * Ignore the buffer done if bubble detect is on
  516. * Increment the ack number here, and queue the
  517. * request back to pending list whenever all the
  518. * buffers are done.
  519. */
  520. req_isp->num_acked++;
  521. CAM_DBG(CAM_ISP,
  522. "buf done with bubble state %d recovery %d",
  523. bubble_state, req_isp->bubble_report);
  524. continue;
  525. }
  526. CAM_DBG(CAM_ISP, "req %lld, reset sync id 0x%x ctx %u",
  527. req->request_id,
  528. req_isp->fence_map_out[j].sync_id, ctx->ctx_id);
  529. if (!rc) {
  530. req_isp->num_acked++;
  531. req_isp->fence_map_out[j].sync_id = -1;
  532. }
  533. }
  534. if (req_isp->num_acked > req_isp->num_fence_map_out) {
  535. /* Should not happen */
  536. CAM_ERR(CAM_ISP,
  537. "WARNING: req_id %lld num_acked %d > map_out %d, ctx %u",
  538. req->request_id, req_isp->num_acked,
  539. req_isp->num_fence_map_out, ctx->ctx_id);
  540. WARN_ON(req_isp->num_acked > req_isp->num_fence_map_out);
  541. }
  542. if (req_isp->num_acked != req_isp->num_fence_map_out)
  543. return rc;
  544. ctx_isp->active_req_cnt--;
  545. buf_done_req_id = req->request_id;
  546. if (req_isp->bubble_detected && req_isp->bubble_report) {
  547. req_isp->num_acked = 0;
  548. req_isp->bubble_detected = false;
  549. list_del_init(&req->list);
  550. atomic_set(&ctx_isp->process_bubble, 0);
  551. if (buf_done_req_id <= ctx->last_flush_req) {
  552. for (i = 0; i < req_isp->num_fence_map_out; i++)
  553. cam_sync_signal(
  554. req_isp->fence_map_out[i].sync_id,
  555. CAM_SYNC_STATE_SIGNALED_ERROR);
  556. list_add_tail(&req->list, &ctx->free_req_list);
  557. CAM_DBG(CAM_REQ,
  558. "Move active request %lld to free list(cnt = %d) [flushed], ctx %u",
  559. buf_done_req_id, ctx_isp->active_req_cnt,
  560. ctx->ctx_id);
  561. } else {
  562. list_add(&req->list, &ctx->pending_req_list);
  563. ctx_isp->bubble_frame_cnt = 0;
  564. CAM_DBG(CAM_REQ,
  565. "Move active request %lld to pending list(cnt = %d) [bubble recovery], ctx %u",
  566. req->request_id, ctx_isp->active_req_cnt,
  567. ctx->ctx_id);
  568. }
  569. } else {
  570. if (ctx_isp->reported_req_id < buf_done_req_id) {
  571. ctx_isp->reported_req_id = buf_done_req_id;
  572. __cam_isp_ctx_send_sof_timestamp(ctx_isp,
  573. buf_done_req_id, CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  574. }
  575. list_del_init(&req->list);
  576. list_add_tail(&req->list, &ctx->free_req_list);
  577. req_isp->reapply = false;
  578. CAM_DBG(CAM_REQ,
  579. "Move active request %lld to free list(cnt = %d) [all fences done], ctx %u",
  580. buf_done_req_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  581. }
  582. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  583. CAM_ISP_STATE_CHANGE_TRIGGER_DONE, buf_done_req_id);
  584. return rc;
  585. }
  586. static int __cam_isp_ctx_handle_buf_done_in_activated_state(
  587. struct cam_isp_context *ctx_isp,
  588. struct cam_isp_hw_done_event_data *done,
  589. uint32_t bubble_state)
  590. {
  591. int rc = 0;
  592. struct cam_ctx_request *req;
  593. struct cam_context *ctx = ctx_isp->base;
  594. struct cam_isp_hw_done_event_data done_next_req;
  595. if (list_empty(&ctx->active_req_list)) {
  596. CAM_DBG(CAM_ISP, "Buf done with no active request");
  597. return 0;
  598. }
  599. req = list_first_entry(&ctx->active_req_list,
  600. struct cam_ctx_request, list);
  601. rc = __cam_isp_ctx_handle_buf_done_for_request(ctx_isp, req, done,
  602. bubble_state, &done_next_req);
  603. if (done_next_req.num_handles) {
  604. struct cam_isp_hw_done_event_data unhandled_res;
  605. struct cam_ctx_request *next_req = list_next_entry(req, list);
  606. if (next_req) {
  607. /*
  608. * Few resource handles are already signalled in the
  609. * current request, lets check if there is another
  610. * request waiting for these resources. This can
  611. * happen if handling some of next request's buf done
  612. * events are happening first before handling current
  613. * request's remaining buf dones due to IRQ scheduling.
  614. * Lets check only one more request as we will have
  615. * maximum of 2 requests in active_list at any time.
  616. */
  617. CAM_WARN(CAM_ISP,
  618. "Unhandled buf done resources for req %lld, trying next request %lld in active_list",
  619. req->request_id, next_req->request_id);
  620. __cam_isp_ctx_handle_buf_done_for_request(ctx_isp,
  621. next_req, &done_next_req,
  622. bubble_state, &unhandled_res);
  623. if (unhandled_res.num_handles == 0)
  624. CAM_INFO(CAM_ISP,
  625. "BUF Done event handed for next request %lld",
  626. next_req->request_id);
  627. else
  628. CAM_ERR(CAM_ISP,
  629. "BUF Done not handled for next request %lld",
  630. next_req->request_id);
  631. }
  632. }
  633. return rc;
  634. }
  635. static int __cam_isp_ctx_reg_upd_in_epoch_state(
  636. struct cam_isp_context *ctx_isp, void *evt_data)
  637. {
  638. if (ctx_isp->frame_id == 1)
  639. CAM_DBG(CAM_ISP, "Reg update for early PCR");
  640. else
  641. CAM_WARN(CAM_ISP,
  642. "Unexpected reg update in activated substate:%d for frame_id:%lld",
  643. ctx_isp->substate_activated, ctx_isp->frame_id);
  644. return 0;
  645. }
  646. static int __cam_isp_ctx_reg_upd_in_applied_state(
  647. struct cam_isp_context *ctx_isp, void *evt_data)
  648. {
  649. int rc = 0;
  650. struct cam_ctx_request *req;
  651. struct cam_context *ctx = ctx_isp->base;
  652. struct cam_isp_ctx_req *req_isp;
  653. uint64_t request_id = 0;
  654. if (list_empty(&ctx->wait_req_list)) {
  655. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request");
  656. goto end;
  657. }
  658. req = list_first_entry(&ctx->wait_req_list,
  659. struct cam_ctx_request, list);
  660. list_del_init(&req->list);
  661. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  662. if (req_isp->num_fence_map_out != 0) {
  663. list_add_tail(&req->list, &ctx->active_req_list);
  664. ctx_isp->active_req_cnt++;
  665. request_id = req->request_id;
  666. CAM_DBG(CAM_REQ,
  667. "move request %lld to active list(cnt = %d), ctx %u",
  668. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  669. } else {
  670. /* no io config, so the request is completed. */
  671. list_add_tail(&req->list, &ctx->free_req_list);
  672. CAM_DBG(CAM_ISP,
  673. "move active request %lld to free list(cnt = %d), ctx %u",
  674. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  675. }
  676. /*
  677. * This function only called directly from applied and bubble applied
  678. * state so change substate here.
  679. */
  680. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  681. CAM_DBG(CAM_ISP, "next substate %d", ctx_isp->substate_activated);
  682. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  683. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE, request_id);
  684. end:
  685. return rc;
  686. }
  687. static int __cam_isp_ctx_notify_sof_in_activated_state(
  688. struct cam_isp_context *ctx_isp, void *evt_data)
  689. {
  690. int rc = 0;
  691. struct cam_req_mgr_trigger_notify notify;
  692. struct cam_context *ctx = ctx_isp->base;
  693. struct cam_ctx_request *req;
  694. struct cam_isp_ctx_req *req_isp;
  695. uint64_t request_id = 0;
  696. /*
  697. * notify reqmgr with sof signal. Note, due to scheduling delay
  698. * we can run into situation that two active requests has already
  699. * be in the active queue while we try to do the notification.
  700. * In this case, we need to skip the current notification. This
  701. * helps the state machine to catch up the delay.
  702. */
  703. if (atomic_read(&ctx_isp->process_bubble)) {
  704. if (list_empty(&ctx->active_req_list)) {
  705. CAM_ERR(CAM_ISP,
  706. "No available active req in bubble");
  707. atomic_set(&ctx_isp->process_bubble, 0);
  708. rc = -EINVAL;
  709. return rc;
  710. }
  711. req = list_first_entry(&ctx->active_req_list,
  712. struct cam_ctx_request, list);
  713. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  714. if (ctx_isp->bubble_frame_cnt >= 1 &&
  715. req_isp->bubble_detected) {
  716. req_isp->num_acked = 0;
  717. ctx_isp->bubble_frame_cnt = 0;
  718. req_isp->bubble_detected = false;
  719. list_del_init(&req->list);
  720. list_add(&req->list, &ctx->pending_req_list);
  721. atomic_set(&ctx_isp->process_bubble, 0);
  722. ctx_isp->active_req_cnt--;
  723. CAM_DBG(CAM_REQ,
  724. "Move active req: %lld to pending list(cnt = %d) [bubble re-apply], ctx %u",
  725. req->request_id,
  726. ctx_isp->active_req_cnt, ctx->ctx_id);
  727. } else if (req_isp->bubble_detected) {
  728. ctx_isp->bubble_frame_cnt++;
  729. CAM_DBG(CAM_ISP,
  730. "Waiting on bufdone for bubble req: %lld, since frame_cnt = %lld",
  731. req->request_id, ctx_isp->bubble_frame_cnt);
  732. } else
  733. CAM_DBG(CAM_ISP, "Delayed bufdone for req: %lld",
  734. req->request_id);
  735. }
  736. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  737. ctx_isp->active_req_cnt <= 2) {
  738. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF) {
  739. notify.link_hdl = ctx->link_hdl;
  740. notify.dev_hdl = ctx->dev_hdl;
  741. notify.frame_id = ctx_isp->frame_id;
  742. notify.trigger = CAM_TRIGGER_POINT_SOF;
  743. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  744. ctx->ctx_crm_intf->notify_trigger(&notify);
  745. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld ctx %u",
  746. ctx_isp->frame_id, ctx->ctx_id);
  747. }
  748. list_for_each_entry(req, &ctx->active_req_list, list) {
  749. if (req->request_id > ctx_isp->reported_req_id) {
  750. request_id = req->request_id;
  751. ctx_isp->reported_req_id = request_id;
  752. break;
  753. }
  754. }
  755. if (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_BUBBLE)
  756. request_id = 0;
  757. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  758. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  759. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  760. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH,
  761. request_id);
  762. } else {
  763. CAM_ERR_RATE_LIMIT(CAM_ISP,
  764. "Can not notify SOF to CRM for ctx %u",
  765. ctx->ctx_id);
  766. rc = -EFAULT;
  767. }
  768. return 0;
  769. }
  770. static int __cam_isp_ctx_notify_eof_in_activated_state(
  771. struct cam_isp_context *ctx_isp, void *evt_data)
  772. {
  773. int rc = 0;
  774. struct cam_req_mgr_trigger_notify notify;
  775. struct cam_context *ctx = ctx_isp->base;
  776. if (!(ctx_isp->subscribe_event & CAM_TRIGGER_POINT_EOF))
  777. return rc;
  778. /* notify reqmgr with eof signal */
  779. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger) {
  780. notify.link_hdl = ctx->link_hdl;
  781. notify.dev_hdl = ctx->dev_hdl;
  782. notify.frame_id = ctx_isp->frame_id;
  783. notify.trigger = CAM_TRIGGER_POINT_EOF;
  784. ctx->ctx_crm_intf->notify_trigger(&notify);
  785. CAM_DBG(CAM_ISP, "Notify CRM EOF frame %lld ctx %u",
  786. ctx_isp->frame_id, ctx->ctx_id);
  787. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  788. CAM_ISP_STATE_CHANGE_TRIGGER_EOF, 0);
  789. } else {
  790. CAM_ERR(CAM_ISP, "Can not notify EOF to CRM for ctx %u",
  791. ctx->ctx_id);
  792. rc = -EFAULT;
  793. }
  794. return rc;
  795. }
  796. static int __cam_isp_ctx_reg_upd_in_hw_error(
  797. struct cam_isp_context *ctx_isp, void *evt_data)
  798. {
  799. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  800. return 0;
  801. }
  802. static int __cam_isp_ctx_sof_in_activated_state(
  803. struct cam_isp_context *ctx_isp, void *evt_data)
  804. {
  805. int rc = 0;
  806. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  807. struct cam_ctx_request *req = NULL;
  808. struct cam_context *ctx = ctx_isp->base;
  809. uint64_t request_id = 0;
  810. /* First check if there is a valid request in active list */
  811. list_for_each_entry(req, &ctx->active_req_list, list) {
  812. if (req->request_id > ctx_isp->reported_req_id) {
  813. request_id = req->request_id;
  814. break;
  815. }
  816. }
  817. /*
  818. * If nothing in active list, current request might have not moved
  819. * from wait to active list. This could happen if REG_UPDATE to sw
  820. * is coming immediately after SOF
  821. */
  822. if (request_id == 0) {
  823. req = list_first_entry(&ctx->wait_req_list,
  824. struct cam_ctx_request, list);
  825. if (req)
  826. request_id = req->request_id;
  827. }
  828. if (!evt_data) {
  829. CAM_ERR(CAM_ISP, "in valid sof event data");
  830. return -EINVAL;
  831. }
  832. ctx_isp->frame_id++;
  833. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  834. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  835. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  836. CAM_ISP_STATE_CHANGE_TRIGGER_SOF, request_id);
  837. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx %u",
  838. ctx_isp->frame_id, ctx_isp->sof_timestamp_val, ctx->ctx_id);
  839. return rc;
  840. }
  841. static int __cam_isp_ctx_reg_upd_in_sof(struct cam_isp_context *ctx_isp,
  842. void *evt_data)
  843. {
  844. int rc = 0;
  845. struct cam_ctx_request *req = NULL;
  846. struct cam_isp_ctx_req *req_isp;
  847. struct cam_context *ctx = ctx_isp->base;
  848. if (ctx->state != CAM_CTX_ACTIVATED && ctx_isp->frame_id > 1) {
  849. CAM_DBG(CAM_ISP, "invalid RUP");
  850. goto end;
  851. }
  852. /*
  853. * This is for the first update. The initial setting will
  854. * cause the reg_upd in the first frame.
  855. */
  856. if (!list_empty(&ctx->wait_req_list)) {
  857. req = list_first_entry(&ctx->wait_req_list,
  858. struct cam_ctx_request, list);
  859. list_del_init(&req->list);
  860. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  861. if (req_isp->num_fence_map_out == req_isp->num_acked)
  862. list_add_tail(&req->list, &ctx->free_req_list);
  863. else
  864. CAM_ERR(CAM_ISP,
  865. "receive rup in unexpected state");
  866. }
  867. if (req != NULL) {
  868. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  869. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  870. req->request_id);
  871. }
  872. end:
  873. return rc;
  874. }
  875. static int __cam_isp_ctx_epoch_in_applied(struct cam_isp_context *ctx_isp,
  876. void *evt_data)
  877. {
  878. struct cam_ctx_request *req;
  879. struct cam_isp_ctx_req *req_isp;
  880. struct cam_context *ctx = ctx_isp->base;
  881. uint64_t request_id = 0;
  882. if (list_empty(&ctx->wait_req_list)) {
  883. /*
  884. * If no wait req in epoch, this is an error case.
  885. * The recovery is to go back to sof state
  886. */
  887. CAM_ERR(CAM_ISP, "No wait request");
  888. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  889. /* Send SOF event as empty frame*/
  890. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  891. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  892. goto end;
  893. }
  894. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  895. list);
  896. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  897. req_isp->bubble_detected = true;
  898. req_isp->reapply = true;
  899. CAM_DBG(CAM_ISP, "Report Bubble flag %d", req_isp->bubble_report);
  900. if (req_isp->bubble_report && ctx->ctx_crm_intf &&
  901. ctx->ctx_crm_intf->notify_err) {
  902. struct cam_req_mgr_error_notify notify;
  903. notify.link_hdl = ctx->link_hdl;
  904. notify.dev_hdl = ctx->dev_hdl;
  905. notify.req_id = req->request_id;
  906. notify.error = CRM_KMD_ERR_BUBBLE;
  907. CAM_WARN(CAM_ISP,
  908. "Notify CRM about Bubble req %lld frame %lld, ctx %u",
  909. req->request_id, ctx_isp->frame_id, ctx->ctx_id);
  910. ctx->ctx_crm_intf->notify_err(&notify);
  911. atomic_set(&ctx_isp->process_bubble, 1);
  912. } else {
  913. req_isp->bubble_report = 0;
  914. }
  915. /*
  916. * Always move the request to active list. Let buf done
  917. * function handles the rest.
  918. */
  919. list_del_init(&req->list);
  920. list_add_tail(&req->list, &ctx->active_req_list);
  921. ctx_isp->active_req_cnt++;
  922. CAM_DBG(CAM_REQ, "move request %lld to active list(cnt = %d), ctx %u",
  923. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  924. if (req->request_id > ctx_isp->reported_req_id) {
  925. request_id = req->request_id;
  926. ctx_isp->reported_req_id = request_id;
  927. }
  928. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  929. CAM_REQ_MGR_SOF_EVENT_ERROR);
  930. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  931. CAM_DBG(CAM_ISP, "next substate %d",
  932. ctx_isp->substate_activated);
  933. end:
  934. if (request_id == 0) {
  935. req = list_last_entry(&ctx->active_req_list,
  936. struct cam_ctx_request, list);
  937. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  938. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, req->request_id);
  939. } else {
  940. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  941. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, request_id);
  942. }
  943. return 0;
  944. }
  945. static int __cam_isp_ctx_buf_done_in_applied(struct cam_isp_context *ctx_isp,
  946. void *evt_data)
  947. {
  948. int rc = 0;
  949. struct cam_isp_hw_done_event_data *done =
  950. (struct cam_isp_hw_done_event_data *) evt_data;
  951. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  952. return rc;
  953. }
  954. static int __cam_isp_ctx_sof_in_epoch(struct cam_isp_context *ctx_isp,
  955. void *evt_data)
  956. {
  957. int rc = 0;
  958. struct cam_context *ctx = ctx_isp->base;
  959. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  960. struct cam_ctx_request *req;
  961. if (!evt_data) {
  962. CAM_ERR(CAM_ISP, "in valid sof event data");
  963. return -EINVAL;
  964. }
  965. ctx_isp->frame_id++;
  966. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  967. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  968. if (list_empty(&ctx->active_req_list))
  969. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  970. else
  971. CAM_DBG(CAM_ISP, "Still need to wait for the buf done");
  972. req = list_last_entry(&ctx->active_req_list,
  973. struct cam_ctx_request, list);
  974. if (req)
  975. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  976. CAM_ISP_STATE_CHANGE_TRIGGER_SOF,
  977. req->request_id);
  978. CAM_DBG(CAM_ISP, "next substate %d",
  979. ctx_isp->substate_activated);
  980. return rc;
  981. }
  982. static int __cam_isp_ctx_buf_done_in_epoch(struct cam_isp_context *ctx_isp,
  983. void *evt_data)
  984. {
  985. int rc = 0;
  986. struct cam_isp_hw_done_event_data *done =
  987. (struct cam_isp_hw_done_event_data *) evt_data;
  988. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  989. return rc;
  990. }
  991. static int __cam_isp_ctx_buf_done_in_bubble(
  992. struct cam_isp_context *ctx_isp, void *evt_data)
  993. {
  994. int rc = 0;
  995. struct cam_isp_hw_done_event_data *done =
  996. (struct cam_isp_hw_done_event_data *) evt_data;
  997. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 1);
  998. return rc;
  999. }
  1000. static int __cam_isp_ctx_epoch_in_bubble_applied(
  1001. struct cam_isp_context *ctx_isp, void *evt_data)
  1002. {
  1003. struct cam_ctx_request *req;
  1004. struct cam_isp_ctx_req *req_isp;
  1005. struct cam_context *ctx = ctx_isp->base;
  1006. uint64_t request_id = 0;
  1007. /*
  1008. * This means we missed the reg upd ack. So we need to
  1009. * transition to BUBBLE state again.
  1010. */
  1011. if (list_empty(&ctx->wait_req_list)) {
  1012. /*
  1013. * If no pending req in epoch, this is an error case.
  1014. * Just go back to the bubble state.
  1015. */
  1016. CAM_ERR(CAM_ISP, "No pending request.");
  1017. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1018. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1019. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  1020. goto end;
  1021. }
  1022. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  1023. list);
  1024. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  1025. req_isp->bubble_detected = true;
  1026. req_isp->reapply = true;
  1027. if (req_isp->bubble_report && ctx->ctx_crm_intf &&
  1028. ctx->ctx_crm_intf->notify_err) {
  1029. struct cam_req_mgr_error_notify notify;
  1030. notify.link_hdl = ctx->link_hdl;
  1031. notify.dev_hdl = ctx->dev_hdl;
  1032. notify.req_id = req->request_id;
  1033. notify.error = CRM_KMD_ERR_BUBBLE;
  1034. CAM_WARN(CAM_REQ,
  1035. "Notify CRM about Bubble req_id %llu frame %lld, ctx %u",
  1036. req->request_id, ctx_isp->frame_id, ctx->ctx_id);
  1037. ctx->ctx_crm_intf->notify_err(&notify);
  1038. atomic_set(&ctx_isp->process_bubble, 1);
  1039. } else {
  1040. req_isp->bubble_report = 0;
  1041. }
  1042. /*
  1043. * Always move the request to active list. Let buf done
  1044. * function handles the rest.
  1045. */
  1046. list_del_init(&req->list);
  1047. list_add_tail(&req->list, &ctx->active_req_list);
  1048. ctx_isp->active_req_cnt++;
  1049. CAM_DBG(CAM_ISP, "move request %lld to active list(cnt = %d) ctx %u",
  1050. req->request_id, ctx_isp->active_req_cnt);
  1051. if (!req_isp->bubble_report) {
  1052. if (req->request_id > ctx_isp->reported_req_id) {
  1053. request_id = req->request_id;
  1054. ctx_isp->reported_req_id = request_id;
  1055. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1056. CAM_REQ_MGR_SOF_EVENT_ERROR);
  1057. } else
  1058. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1059. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1060. } else
  1061. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1062. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1063. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  1064. CAM_DBG(CAM_ISP, "next substate %d", ctx_isp->substate_activated);
  1065. end:
  1066. req = list_last_entry(&ctx->active_req_list, struct cam_ctx_request,
  1067. list);
  1068. if (req)
  1069. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1070. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, req->request_id);
  1071. return 0;
  1072. }
  1073. static int __cam_isp_ctx_buf_done_in_bubble_applied(
  1074. struct cam_isp_context *ctx_isp, void *evt_data)
  1075. {
  1076. int rc = 0;
  1077. struct cam_isp_hw_done_event_data *done =
  1078. (struct cam_isp_hw_done_event_data *) evt_data;
  1079. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 1);
  1080. return rc;
  1081. }
  1082. static int __cam_isp_ctx_handle_error(struct cam_isp_context *ctx_isp,
  1083. void *evt_data)
  1084. {
  1085. int rc = 0;
  1086. uint32_t i = 0;
  1087. bool found = 0;
  1088. struct cam_ctx_request *req = NULL;
  1089. struct cam_ctx_request *req_to_report = NULL;
  1090. struct cam_ctx_request *req_to_dump = NULL;
  1091. struct cam_ctx_request *req_temp;
  1092. struct cam_isp_ctx_req *req_isp = NULL;
  1093. struct cam_isp_ctx_req *req_isp_to_report = NULL;
  1094. struct cam_req_mgr_error_notify notify = {};
  1095. uint64_t error_request_id;
  1096. struct cam_hw_fence_map_entry *fence_map_out = NULL;
  1097. struct cam_req_mgr_message req_msg;
  1098. struct cam_context *ctx = ctx_isp->base;
  1099. struct cam_isp_hw_error_event_data *error_event_data =
  1100. (struct cam_isp_hw_error_event_data *)evt_data;
  1101. uint32_t error_type = error_event_data->error_type;
  1102. CAM_DBG(CAM_ISP, "Enter error_type = %d", error_type);
  1103. if ((error_type == CAM_ISP_HW_ERROR_OVERFLOW) ||
  1104. (error_type == CAM_ISP_HW_ERROR_BUSIF_OVERFLOW) ||
  1105. (error_type == CAM_ISP_HW_ERROR_VIOLATION)) {
  1106. struct cam_hw_cmd_args hw_cmd_args;
  1107. memset(&hw_cmd_args, 0, sizeof(hw_cmd_args));
  1108. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  1109. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_REG_DUMP_ON_ERROR;
  1110. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  1111. &hw_cmd_args);
  1112. if (rc) {
  1113. CAM_ERR(CAM_ISP, "Reg dump on error failed rc: %d", rc);
  1114. rc = 0;
  1115. }
  1116. }
  1117. /*
  1118. * The error is likely caused by first request on the active list.
  1119. * If active list is empty check wait list (maybe error hit as soon
  1120. * as RUP and we handle error before RUP.
  1121. */
  1122. if (list_empty(&ctx->active_req_list)) {
  1123. CAM_DBG(CAM_ISP,
  1124. "handling error with no active request");
  1125. if (list_empty(&ctx->wait_req_list)) {
  1126. CAM_ERR_RATE_LIMIT(CAM_ISP,
  1127. "Error with no active/wait request");
  1128. goto end;
  1129. } else {
  1130. req_to_dump = list_first_entry(&ctx->wait_req_list,
  1131. struct cam_ctx_request, list);
  1132. }
  1133. } else {
  1134. req_to_dump = list_first_entry(&ctx->active_req_list,
  1135. struct cam_ctx_request, list);
  1136. }
  1137. req_isp = (struct cam_isp_ctx_req *) req_to_dump->req_priv;
  1138. if (error_event_data->enable_req_dump)
  1139. cam_isp_ctx_dump_req(req_isp);
  1140. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1141. CAM_ISP_STATE_CHANGE_TRIGGER_ERROR, req_to_dump->request_id);
  1142. list_for_each_entry_safe(req, req_temp,
  1143. &ctx->active_req_list, list) {
  1144. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1145. if (!req_isp->bubble_report) {
  1146. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  1147. fence_map_out =
  1148. &req_isp->fence_map_out[i];
  1149. CAM_ERR(CAM_ISP,
  1150. "req %llu, Sync fd 0x%x ctx %u",
  1151. req->request_id,
  1152. req_isp->fence_map_out[i].sync_id,
  1153. ctx->ctx_id);
  1154. if (req_isp->fence_map_out[i].sync_id != -1) {
  1155. rc = cam_sync_signal(
  1156. fence_map_out->sync_id,
  1157. CAM_SYNC_STATE_SIGNALED_ERROR);
  1158. fence_map_out->sync_id = -1;
  1159. }
  1160. }
  1161. list_del_init(&req->list);
  1162. list_add_tail(&req->list, &ctx->free_req_list);
  1163. ctx_isp->active_req_cnt--;
  1164. } else {
  1165. found = 1;
  1166. break;
  1167. }
  1168. }
  1169. if (found)
  1170. goto move_to_pending;
  1171. list_for_each_entry_safe(req, req_temp,
  1172. &ctx->wait_req_list, list) {
  1173. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1174. if (!req_isp->bubble_report) {
  1175. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  1176. fence_map_out =
  1177. &req_isp->fence_map_out[i];
  1178. CAM_ERR(CAM_ISP,
  1179. "req %llu, Sync fd 0x%x ctx %u",
  1180. req->request_id,
  1181. req_isp->fence_map_out[i].sync_id,
  1182. ctx->ctx_id);
  1183. if (req_isp->fence_map_out[i].sync_id != -1) {
  1184. rc = cam_sync_signal(
  1185. fence_map_out->sync_id,
  1186. CAM_SYNC_STATE_SIGNALED_ERROR);
  1187. fence_map_out->sync_id = -1;
  1188. }
  1189. }
  1190. list_del_init(&req->list);
  1191. list_add_tail(&req->list, &ctx->free_req_list);
  1192. ctx_isp->active_req_cnt--;
  1193. } else {
  1194. found = 1;
  1195. break;
  1196. }
  1197. }
  1198. move_to_pending:
  1199. /*
  1200. * If bubble recovery is enabled on any request we need to move that
  1201. * request and all the subsequent requests to the pending list.
  1202. * Note:
  1203. * We need to traverse the active list in reverse order and add
  1204. * to head of pending list.
  1205. * e.g. pending current state: 10, 11 | active current state: 8, 9
  1206. * intermittent for loop iteration- pending: 9, 10, 11 | active: 8
  1207. * final state - pending: 8, 9, 10, 11 | active: NULL
  1208. */
  1209. if (found) {
  1210. list_for_each_entry_safe_reverse(req, req_temp,
  1211. &ctx->active_req_list, list) {
  1212. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1213. list_del_init(&req->list);
  1214. list_add(&req->list, &ctx->pending_req_list);
  1215. ctx_isp->active_req_cnt--;
  1216. }
  1217. list_for_each_entry_safe_reverse(req, req_temp,
  1218. &ctx->wait_req_list, list) {
  1219. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1220. list_del_init(&req->list);
  1221. list_add(&req->list, &ctx->pending_req_list);
  1222. ctx_isp->active_req_cnt--;
  1223. }
  1224. }
  1225. end:
  1226. do {
  1227. if (list_empty(&ctx->pending_req_list)) {
  1228. error_request_id = ctx_isp->last_applied_req_id + 1;
  1229. req_isp = NULL;
  1230. break;
  1231. }
  1232. req = list_first_entry(&ctx->pending_req_list,
  1233. struct cam_ctx_request, list);
  1234. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1235. error_request_id = ctx_isp->last_applied_req_id;
  1236. if (req_isp->bubble_report) {
  1237. req_to_report = req;
  1238. req_isp_to_report = req_to_report->req_priv;
  1239. break;
  1240. }
  1241. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  1242. if (req_isp->fence_map_out[i].sync_id != -1)
  1243. rc = cam_sync_signal(
  1244. req_isp->fence_map_out[i].sync_id,
  1245. CAM_SYNC_STATE_SIGNALED_ERROR);
  1246. req_isp->fence_map_out[i].sync_id = -1;
  1247. }
  1248. list_del_init(&req->list);
  1249. list_add_tail(&req->list, &ctx->free_req_list);
  1250. } while (req->request_id < ctx_isp->last_applied_req_id);
  1251. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_err) {
  1252. notify.link_hdl = ctx->link_hdl;
  1253. notify.dev_hdl = ctx->dev_hdl;
  1254. notify.req_id = error_request_id;
  1255. if (req_isp_to_report && req_isp_to_report->bubble_report) {
  1256. if (error_event_data->recovery_enabled)
  1257. notify.error = CRM_KMD_ERR_BUBBLE;
  1258. } else {
  1259. notify.error = CRM_KMD_ERR_FATAL;
  1260. }
  1261. CAM_WARN(CAM_ISP,
  1262. "Notify CRM: req %lld, frame %lld ctx %u, error %d",
  1263. error_request_id, ctx_isp->frame_id, ctx->ctx_id,
  1264. notify.error);
  1265. ctx->ctx_crm_intf->notify_err(&notify);
  1266. /*
  1267. * Need to send error occurred in KMD
  1268. * This will help UMD to take necessary action
  1269. * and to dump relevant info
  1270. */
  1271. if (notify.error == CRM_KMD_ERR_FATAL) {
  1272. req_msg.session_hdl = ctx_isp->base->session_hdl;
  1273. req_msg.u.err_msg.device_hdl = ctx_isp->base->dev_hdl;
  1274. req_msg.u.err_msg.error_type =
  1275. CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  1276. req_msg.u.err_msg.link_hdl = ctx_isp->base->link_hdl;
  1277. req_msg.u.err_msg.request_id = error_request_id;
  1278. req_msg.u.err_msg.resource_size = 0x0;
  1279. if (cam_req_mgr_notify_message(&req_msg,
  1280. V4L_EVENT_CAM_REQ_MGR_ERROR,
  1281. V4L_EVENT_CAM_REQ_MGR_EVENT))
  1282. CAM_ERR(CAM_ISP,
  1283. "Error in notifying the error time for req id:%lld ctx %u",
  1284. ctx_isp->last_applied_req_id,
  1285. ctx->ctx_id);
  1286. }
  1287. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HW_ERROR;
  1288. } else {
  1289. CAM_ERR_RATE_LIMIT(CAM_ISP,
  1290. "Can not notify ERRROR to CRM for ctx %u",
  1291. ctx->ctx_id);
  1292. rc = -EFAULT;
  1293. }
  1294. CAM_DBG(CAM_ISP, "Exit");
  1295. return rc;
  1296. }
  1297. static int __cam_isp_ctx_fs2_sof_in_sof_state(
  1298. struct cam_isp_context *ctx_isp, void *evt_data)
  1299. {
  1300. int rc = 0;
  1301. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  1302. struct cam_ctx_request *req;
  1303. struct cam_context *ctx = ctx_isp->base;
  1304. struct cam_req_mgr_trigger_notify notify;
  1305. uint64_t request_id = 0;
  1306. if (!evt_data) {
  1307. CAM_ERR(CAM_ISP, "in valid sof event data");
  1308. return -EINVAL;
  1309. }
  1310. ctx_isp->frame_id++;
  1311. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  1312. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  1313. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  1314. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  1315. if (!(list_empty(&ctx->wait_req_list)))
  1316. goto end;
  1317. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  1318. ctx_isp->active_req_cnt <= 2) {
  1319. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF) {
  1320. notify.link_hdl = ctx->link_hdl;
  1321. notify.dev_hdl = ctx->dev_hdl;
  1322. notify.frame_id = ctx_isp->frame_id;
  1323. notify.trigger = CAM_TRIGGER_POINT_SOF;
  1324. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  1325. ctx->ctx_crm_intf->notify_trigger(&notify);
  1326. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  1327. ctx_isp->frame_id);
  1328. }
  1329. list_for_each_entry(req, &ctx->active_req_list, list) {
  1330. if (req->request_id > ctx_isp->reported_req_id) {
  1331. request_id = req->request_id;
  1332. ctx_isp->reported_req_id = request_id;
  1333. break;
  1334. }
  1335. }
  1336. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1337. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1338. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1339. CAM_ISP_STATE_CHANGE_TRIGGER_SOF, request_id);
  1340. } else {
  1341. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not notify SOF to CRM");
  1342. rc = -EFAULT;
  1343. }
  1344. end:
  1345. return rc;
  1346. }
  1347. static int __cam_isp_ctx_fs2_buf_done(struct cam_isp_context *ctx_isp,
  1348. void *evt_data)
  1349. {
  1350. int rc = 0;
  1351. struct cam_isp_hw_done_event_data *done =
  1352. (struct cam_isp_hw_done_event_data *) evt_data;
  1353. struct cam_context *ctx = ctx_isp->base;
  1354. int prev_active_req_cnt = 0;
  1355. int curr_req_id = 0;
  1356. struct cam_ctx_request *req;
  1357. prev_active_req_cnt = ctx_isp->active_req_cnt;
  1358. req = list_first_entry(&ctx->active_req_list,
  1359. struct cam_ctx_request, list);
  1360. if (req)
  1361. curr_req_id = req->request_id;
  1362. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  1363. if (prev_active_req_cnt == ctx_isp->active_req_cnt + 1) {
  1364. if (list_empty(&ctx->wait_req_list) &&
  1365. list_empty(&ctx->active_req_list)) {
  1366. CAM_DBG(CAM_ISP, "No request, move to SOF");
  1367. ctx_isp->substate_activated =
  1368. CAM_ISP_CTX_ACTIVATED_SOF;
  1369. if (ctx_isp->reported_req_id < curr_req_id) {
  1370. ctx_isp->reported_req_id = curr_req_id;
  1371. __cam_isp_ctx_send_sof_timestamp(ctx_isp,
  1372. curr_req_id,
  1373. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1374. }
  1375. }
  1376. }
  1377. return rc;
  1378. }
  1379. static int __cam_isp_ctx_fs2_buf_done_in_epoch(struct cam_isp_context *ctx_isp,
  1380. void *evt_data)
  1381. {
  1382. int rc = 0;
  1383. rc = __cam_isp_ctx_fs2_buf_done(ctx_isp, evt_data);
  1384. return rc;
  1385. }
  1386. static int __cam_isp_ctx_fs2_buf_done_in_applied(
  1387. struct cam_isp_context *ctx_isp,
  1388. void *evt_data)
  1389. {
  1390. int rc = 0;
  1391. rc = __cam_isp_ctx_fs2_buf_done(ctx_isp, evt_data);
  1392. return rc;
  1393. }
  1394. static int __cam_isp_ctx_fs2_reg_upd_in_sof(struct cam_isp_context *ctx_isp,
  1395. void *evt_data)
  1396. {
  1397. int rc = 0;
  1398. struct cam_ctx_request *req = NULL;
  1399. struct cam_isp_ctx_req *req_isp;
  1400. struct cam_context *ctx = ctx_isp->base;
  1401. if (ctx->state != CAM_CTX_ACTIVATED && ctx_isp->frame_id > 1) {
  1402. CAM_DBG(CAM_ISP, "invalid RUP");
  1403. goto end;
  1404. }
  1405. /*
  1406. * This is for the first update. The initial setting will
  1407. * cause the reg_upd in the first frame.
  1408. */
  1409. if (!list_empty(&ctx->wait_req_list)) {
  1410. req = list_first_entry(&ctx->wait_req_list,
  1411. struct cam_ctx_request, list);
  1412. list_del_init(&req->list);
  1413. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1414. if (req_isp->num_fence_map_out == req_isp->num_acked)
  1415. list_add_tail(&req->list, &ctx->free_req_list);
  1416. else
  1417. CAM_ERR(CAM_ISP,
  1418. "receive rup in unexpected state");
  1419. }
  1420. if (req != NULL) {
  1421. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1422. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  1423. req->request_id);
  1424. }
  1425. end:
  1426. return rc;
  1427. }
  1428. static int __cam_isp_ctx_fs2_reg_upd_in_applied_state(
  1429. struct cam_isp_context *ctx_isp, void *evt_data)
  1430. {
  1431. int rc = 0;
  1432. struct cam_ctx_request *req = NULL;
  1433. struct cam_context *ctx = ctx_isp->base;
  1434. struct cam_isp_ctx_req *req_isp;
  1435. struct cam_req_mgr_trigger_notify notify;
  1436. uint64_t request_id = 0;
  1437. if (list_empty(&ctx->wait_req_list)) {
  1438. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request");
  1439. goto end;
  1440. }
  1441. req = list_first_entry(&ctx->wait_req_list,
  1442. struct cam_ctx_request, list);
  1443. list_del_init(&req->list);
  1444. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1445. if (req_isp->num_fence_map_out != 0) {
  1446. list_add_tail(&req->list, &ctx->active_req_list);
  1447. ctx_isp->active_req_cnt++;
  1448. CAM_DBG(CAM_REQ, "move request %lld to active list(cnt = %d)",
  1449. req->request_id, ctx_isp->active_req_cnt);
  1450. } else {
  1451. /* no io config, so the request is completed. */
  1452. list_add_tail(&req->list, &ctx->free_req_list);
  1453. }
  1454. /*
  1455. * This function only called directly from applied and bubble applied
  1456. * state so change substate here.
  1457. */
  1458. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  1459. if (req_isp->num_fence_map_out != 1)
  1460. goto end;
  1461. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  1462. ctx_isp->active_req_cnt <= 2) {
  1463. list_for_each_entry(req, &ctx->active_req_list, list) {
  1464. if (req->request_id > ctx_isp->reported_req_id) {
  1465. request_id = req->request_id;
  1466. ctx_isp->reported_req_id = request_id;
  1467. break;
  1468. }
  1469. }
  1470. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1471. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1472. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF) {
  1473. notify.link_hdl = ctx->link_hdl;
  1474. notify.dev_hdl = ctx->dev_hdl;
  1475. notify.frame_id = ctx_isp->frame_id;
  1476. notify.trigger = CAM_TRIGGER_POINT_SOF;
  1477. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  1478. ctx->ctx_crm_intf->notify_trigger(&notify);
  1479. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  1480. ctx_isp->frame_id);
  1481. }
  1482. } else {
  1483. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not notify SOF to CRM");
  1484. rc = -EFAULT;
  1485. }
  1486. CAM_DBG(CAM_ISP, "next substate %d", ctx_isp->substate_activated);
  1487. end:
  1488. if (req != NULL && !rc) {
  1489. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1490. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  1491. req->request_id);
  1492. }
  1493. return rc;
  1494. }
  1495. static struct cam_isp_ctx_irq_ops
  1496. cam_isp_ctx_activated_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  1497. /* SOF */
  1498. {
  1499. .irq_ops = {
  1500. __cam_isp_ctx_handle_error,
  1501. __cam_isp_ctx_sof_in_activated_state,
  1502. __cam_isp_ctx_reg_upd_in_sof,
  1503. __cam_isp_ctx_notify_sof_in_activated_state,
  1504. __cam_isp_ctx_notify_eof_in_activated_state,
  1505. NULL,
  1506. },
  1507. },
  1508. /* APPLIED */
  1509. {
  1510. .irq_ops = {
  1511. __cam_isp_ctx_handle_error,
  1512. __cam_isp_ctx_sof_in_activated_state,
  1513. __cam_isp_ctx_reg_upd_in_applied_state,
  1514. __cam_isp_ctx_epoch_in_applied,
  1515. __cam_isp_ctx_notify_eof_in_activated_state,
  1516. __cam_isp_ctx_buf_done_in_applied,
  1517. },
  1518. },
  1519. /* EPOCH */
  1520. {
  1521. .irq_ops = {
  1522. __cam_isp_ctx_handle_error,
  1523. __cam_isp_ctx_sof_in_epoch,
  1524. __cam_isp_ctx_reg_upd_in_epoch_state,
  1525. __cam_isp_ctx_notify_sof_in_activated_state,
  1526. __cam_isp_ctx_notify_eof_in_activated_state,
  1527. __cam_isp_ctx_buf_done_in_epoch,
  1528. },
  1529. },
  1530. /* BUBBLE */
  1531. {
  1532. .irq_ops = {
  1533. __cam_isp_ctx_handle_error,
  1534. __cam_isp_ctx_sof_in_activated_state,
  1535. NULL,
  1536. __cam_isp_ctx_notify_sof_in_activated_state,
  1537. __cam_isp_ctx_notify_eof_in_activated_state,
  1538. __cam_isp_ctx_buf_done_in_bubble,
  1539. },
  1540. },
  1541. /* Bubble Applied */
  1542. {
  1543. .irq_ops = {
  1544. __cam_isp_ctx_handle_error,
  1545. __cam_isp_ctx_sof_in_activated_state,
  1546. __cam_isp_ctx_reg_upd_in_applied_state,
  1547. __cam_isp_ctx_epoch_in_bubble_applied,
  1548. NULL,
  1549. __cam_isp_ctx_buf_done_in_bubble_applied,
  1550. },
  1551. },
  1552. /* HW ERROR */
  1553. {
  1554. .irq_ops = {
  1555. NULL,
  1556. __cam_isp_ctx_sof_in_activated_state,
  1557. __cam_isp_ctx_reg_upd_in_hw_error,
  1558. NULL,
  1559. NULL,
  1560. NULL,
  1561. },
  1562. },
  1563. /* HALT */
  1564. {
  1565. },
  1566. };
  1567. static struct cam_isp_ctx_irq_ops
  1568. cam_isp_ctx_fs2_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  1569. /* SOF */
  1570. {
  1571. .irq_ops = {
  1572. __cam_isp_ctx_handle_error,
  1573. __cam_isp_ctx_fs2_sof_in_sof_state,
  1574. __cam_isp_ctx_fs2_reg_upd_in_sof,
  1575. __cam_isp_ctx_fs2_sof_in_sof_state,
  1576. __cam_isp_ctx_notify_eof_in_activated_state,
  1577. NULL,
  1578. },
  1579. },
  1580. /* APPLIED */
  1581. {
  1582. .irq_ops = {
  1583. __cam_isp_ctx_handle_error,
  1584. __cam_isp_ctx_sof_in_activated_state,
  1585. __cam_isp_ctx_fs2_reg_upd_in_applied_state,
  1586. __cam_isp_ctx_epoch_in_applied,
  1587. __cam_isp_ctx_notify_eof_in_activated_state,
  1588. __cam_isp_ctx_fs2_buf_done_in_applied,
  1589. },
  1590. },
  1591. /* EPOCH */
  1592. {
  1593. .irq_ops = {
  1594. __cam_isp_ctx_handle_error,
  1595. __cam_isp_ctx_sof_in_epoch,
  1596. __cam_isp_ctx_reg_upd_in_epoch_state,
  1597. __cam_isp_ctx_notify_sof_in_activated_state,
  1598. __cam_isp_ctx_notify_eof_in_activated_state,
  1599. __cam_isp_ctx_fs2_buf_done_in_epoch,
  1600. },
  1601. },
  1602. /* BUBBLE */
  1603. {
  1604. .irq_ops = {
  1605. __cam_isp_ctx_handle_error,
  1606. __cam_isp_ctx_sof_in_activated_state,
  1607. NULL,
  1608. __cam_isp_ctx_notify_sof_in_activated_state,
  1609. __cam_isp_ctx_notify_eof_in_activated_state,
  1610. __cam_isp_ctx_buf_done_in_bubble,
  1611. },
  1612. },
  1613. /* Bubble Applied */
  1614. {
  1615. .irq_ops = {
  1616. __cam_isp_ctx_handle_error,
  1617. __cam_isp_ctx_sof_in_activated_state,
  1618. __cam_isp_ctx_reg_upd_in_applied_state,
  1619. __cam_isp_ctx_epoch_in_bubble_applied,
  1620. NULL,
  1621. __cam_isp_ctx_buf_done_in_bubble_applied,
  1622. },
  1623. },
  1624. /* HW ERROR */
  1625. {
  1626. .irq_ops = {
  1627. NULL,
  1628. __cam_isp_ctx_sof_in_activated_state,
  1629. __cam_isp_ctx_reg_upd_in_hw_error,
  1630. NULL,
  1631. NULL,
  1632. NULL,
  1633. },
  1634. },
  1635. /* HALT */
  1636. {
  1637. },
  1638. };
  1639. static int __cam_isp_ctx_apply_req_in_activated_state(
  1640. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply,
  1641. uint32_t next_state)
  1642. {
  1643. int rc = 0;
  1644. struct cam_ctx_request *req;
  1645. struct cam_ctx_request *active_req = NULL;
  1646. struct cam_isp_ctx_req *req_isp;
  1647. struct cam_isp_ctx_req *active_req_isp;
  1648. struct cam_isp_context *ctx_isp = NULL;
  1649. struct cam_hw_config_args cfg;
  1650. if (list_empty(&ctx->pending_req_list)) {
  1651. CAM_ERR(CAM_ISP, "No available request for Apply id %lld",
  1652. apply->request_id);
  1653. rc = -EFAULT;
  1654. goto end;
  1655. }
  1656. /*
  1657. * When the pipeline has issue, the requests can be queued up in the
  1658. * pipeline. In this case, we should reject the additional request.
  1659. * The maximum number of request allowed to be outstanding is 2.
  1660. *
  1661. */
  1662. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  1663. if (atomic_read(&ctx_isp->process_bubble)) {
  1664. CAM_INFO(CAM_ISP,
  1665. "Processing bubble cannot apply Request Id %llu",
  1666. apply->request_id);
  1667. rc = -EAGAIN;
  1668. goto end;
  1669. }
  1670. spin_lock_bh(&ctx->lock);
  1671. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request,
  1672. list);
  1673. spin_unlock_bh(&ctx->lock);
  1674. /*
  1675. * Check whether the request id is matching the tip, if not, this means
  1676. * we are in the middle of the error handling. Need to reject this apply
  1677. */
  1678. if (req->request_id != apply->request_id) {
  1679. CAM_ERR_RATE_LIMIT(CAM_ISP,
  1680. "Invalid Request Id asking %llu existing %llu",
  1681. apply->request_id, req->request_id);
  1682. rc = -EFAULT;
  1683. goto end;
  1684. }
  1685. CAM_DBG(CAM_REQ, "Apply request %lld in substate %d ctx %u",
  1686. req->request_id, ctx_isp->substate_activated, ctx->ctx_id);
  1687. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1688. if (ctx_isp->active_req_cnt >= 2) {
  1689. CAM_WARN(CAM_ISP,
  1690. "Reject apply request (id %lld) due to congestion(cnt = %d) ctx %u",
  1691. req->request_id,
  1692. ctx_isp->active_req_cnt,
  1693. ctx->ctx_id);
  1694. spin_lock_bh(&ctx->lock);
  1695. if (!list_empty(&ctx->active_req_list))
  1696. active_req = list_first_entry(&ctx->active_req_list,
  1697. struct cam_ctx_request, list);
  1698. else
  1699. CAM_ERR_RATE_LIMIT(CAM_ISP,
  1700. "WARNING: should not happen (cnt = %d) but active_list empty",
  1701. ctx_isp->active_req_cnt);
  1702. spin_unlock_bh(&ctx->lock);
  1703. if (active_req) {
  1704. active_req_isp =
  1705. (struct cam_isp_ctx_req *) active_req->req_priv;
  1706. __cam_isp_ctx_handle_buf_done_fail_log(
  1707. active_req->request_id, active_req_isp);
  1708. }
  1709. rc = -EFAULT;
  1710. goto end;
  1711. }
  1712. req_isp->bubble_report = apply->report_if_bubble;
  1713. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  1714. cfg.request_id = req->request_id;
  1715. cfg.hw_update_entries = req_isp->cfg;
  1716. cfg.num_hw_update_entries = req_isp->num_cfg;
  1717. cfg.priv = &req_isp->hw_update_data;
  1718. cfg.init_packet = 0;
  1719. cfg.reapply = req_isp->reapply;
  1720. rc = ctx->hw_mgr_intf->hw_config(ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  1721. if (rc) {
  1722. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not apply the configuration");
  1723. } else {
  1724. spin_lock_bh(&ctx->lock);
  1725. ctx_isp->substate_activated = next_state;
  1726. ctx_isp->last_applied_req_id = apply->request_id;
  1727. list_del_init(&req->list);
  1728. list_add_tail(&req->list, &ctx->wait_req_list);
  1729. CAM_DBG(CAM_ISP, "new substate state %d, applied req %lld",
  1730. next_state, ctx_isp->last_applied_req_id);
  1731. spin_unlock_bh(&ctx->lock);
  1732. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1733. CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED,
  1734. req->request_id);
  1735. }
  1736. end:
  1737. return rc;
  1738. }
  1739. static int __cam_isp_ctx_apply_req_in_sof(
  1740. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  1741. {
  1742. int rc = 0;
  1743. struct cam_isp_context *ctx_isp =
  1744. (struct cam_isp_context *) ctx->ctx_priv;
  1745. CAM_DBG(CAM_ISP, "current substate %d",
  1746. ctx_isp->substate_activated);
  1747. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  1748. CAM_ISP_CTX_ACTIVATED_APPLIED);
  1749. CAM_DBG(CAM_ISP, "new substate %d", ctx_isp->substate_activated);
  1750. if (rc)
  1751. CAM_DBG(CAM_ISP, "Apply failed in state %d, rc %d",
  1752. ctx_isp->substate_activated, rc);
  1753. return rc;
  1754. }
  1755. static int __cam_isp_ctx_apply_req_in_epoch(
  1756. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  1757. {
  1758. int rc = 0;
  1759. struct cam_isp_context *ctx_isp =
  1760. (struct cam_isp_context *) ctx->ctx_priv;
  1761. CAM_DBG(CAM_ISP, "current substate %d",
  1762. ctx_isp->substate_activated);
  1763. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  1764. CAM_ISP_CTX_ACTIVATED_APPLIED);
  1765. CAM_DBG(CAM_ISP, "new substate %d", ctx_isp->substate_activated);
  1766. if (rc)
  1767. CAM_DBG(CAM_ISP, "Apply failed in state %d, rc %d",
  1768. ctx_isp->substate_activated, rc);
  1769. return rc;
  1770. }
  1771. static int __cam_isp_ctx_apply_req_in_bubble(
  1772. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  1773. {
  1774. int rc = 0;
  1775. struct cam_isp_context *ctx_isp =
  1776. (struct cam_isp_context *) ctx->ctx_priv;
  1777. CAM_DBG(CAM_ISP, "current substate %d",
  1778. ctx_isp->substate_activated);
  1779. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  1780. CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED);
  1781. CAM_DBG(CAM_ISP, "new substate %d", ctx_isp->substate_activated);
  1782. if (rc)
  1783. CAM_DBG(CAM_ISP, "Apply failed in state %d, rc %d",
  1784. ctx_isp->substate_activated, rc);
  1785. return rc;
  1786. }
  1787. static int __cam_isp_ctx_flush_req(struct cam_context *ctx,
  1788. struct list_head *req_list, struct cam_req_mgr_flush_request *flush_req)
  1789. {
  1790. int i, rc, tmp = 0;
  1791. uint32_t cancel_req_id_found = 0;
  1792. struct cam_ctx_request *req;
  1793. struct cam_ctx_request *req_temp;
  1794. struct cam_isp_ctx_req *req_isp;
  1795. struct list_head flush_list;
  1796. struct cam_isp_context *ctx_isp = NULL;
  1797. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  1798. INIT_LIST_HEAD(&flush_list);
  1799. if (list_empty(req_list)) {
  1800. CAM_DBG(CAM_ISP, "request list is empty");
  1801. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  1802. CAM_ERR(CAM_ISP, "no request to cancel");
  1803. return -EINVAL;
  1804. } else
  1805. return 0;
  1806. }
  1807. CAM_DBG(CAM_REQ, "Flush [%u] in progress for req_id %llu",
  1808. flush_req->type, flush_req->req_id);
  1809. list_for_each_entry_safe(req, req_temp, req_list, list) {
  1810. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  1811. if (req->request_id != flush_req->req_id) {
  1812. continue;
  1813. } else {
  1814. list_del_init(&req->list);
  1815. list_add_tail(&req->list, &flush_list);
  1816. cancel_req_id_found = 1;
  1817. __cam_isp_ctx_update_state_monitor_array(
  1818. ctx_isp,
  1819. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH,
  1820. req->request_id);
  1821. break;
  1822. }
  1823. }
  1824. list_del_init(&req->list);
  1825. list_add_tail(&req->list, &flush_list);
  1826. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1827. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH, req->request_id);
  1828. }
  1829. list_for_each_entry_safe(req, req_temp, &flush_list, list) {
  1830. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1831. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  1832. if (req_isp->fence_map_out[i].sync_id != -1) {
  1833. CAM_DBG(CAM_ISP, "Flush req 0x%llx, fence %d",
  1834. req->request_id,
  1835. req_isp->fence_map_out[i].sync_id);
  1836. rc = cam_sync_signal(
  1837. req_isp->fence_map_out[i].sync_id,
  1838. CAM_SYNC_STATE_SIGNALED_ERROR);
  1839. if (rc) {
  1840. tmp = req_isp->fence_map_out[i].sync_id;
  1841. CAM_ERR_RATE_LIMIT(CAM_ISP,
  1842. "signal fence %d failed", tmp);
  1843. }
  1844. req_isp->fence_map_out[i].sync_id = -1;
  1845. }
  1846. }
  1847. req_isp->reapply = false;
  1848. list_add_tail(&req->list, &ctx->free_req_list);
  1849. }
  1850. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ &&
  1851. !cancel_req_id_found)
  1852. CAM_DBG(CAM_ISP,
  1853. "Flush request id:%lld is not found in the list",
  1854. flush_req->req_id);
  1855. return 0;
  1856. }
  1857. static int __cam_isp_ctx_flush_req_in_top_state(
  1858. struct cam_context *ctx,
  1859. struct cam_req_mgr_flush_request *flush_req)
  1860. {
  1861. int rc = 0;
  1862. struct cam_isp_context *ctx_isp;
  1863. struct cam_isp_stop_args stop_isp;
  1864. struct cam_hw_stop_args stop_args;
  1865. struct cam_hw_reset_args reset_args;
  1866. struct cam_hw_cmd_args hw_cmd_args;
  1867. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  1868. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_ALL) {
  1869. CAM_INFO(CAM_ISP, "Last request id to flush is %lld",
  1870. flush_req->req_id);
  1871. ctx->last_flush_req = flush_req->req_id;
  1872. }
  1873. CAM_DBG(CAM_ISP, "Flush pending list");
  1874. spin_lock_bh(&ctx->lock);
  1875. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  1876. spin_unlock_bh(&ctx->lock);
  1877. memset(&hw_cmd_args, 0, sizeof(hw_cmd_args));
  1878. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  1879. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_REG_DUMP_ON_FLUSH;
  1880. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  1881. &hw_cmd_args);
  1882. if (rc) {
  1883. CAM_ERR(CAM_ISP, "Reg dump on flush failed rc: %d", rc);
  1884. rc = 0;
  1885. }
  1886. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_ALL) {
  1887. if (ctx->state <= CAM_CTX_READY) {
  1888. ctx->state = CAM_CTX_ACQUIRED;
  1889. goto end;
  1890. }
  1891. stop_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  1892. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  1893. stop_isp.stop_only = true;
  1894. stop_args.args = (void *)&stop_isp;
  1895. rc = ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  1896. &stop_args);
  1897. if (rc)
  1898. CAM_ERR(CAM_ISP, "Failed to stop HW in Flush rc: %d",
  1899. rc);
  1900. CAM_INFO(CAM_ISP, "Stop HW complete. Reset HW next.");
  1901. CAM_DBG(CAM_ISP, "Flush wait and active lists");
  1902. spin_lock_bh(&ctx->lock);
  1903. if (!list_empty(&ctx->wait_req_list))
  1904. rc = __cam_isp_ctx_flush_req(ctx, &ctx->wait_req_list,
  1905. flush_req);
  1906. if (!list_empty(&ctx->active_req_list))
  1907. rc = __cam_isp_ctx_flush_req(ctx, &ctx->active_req_list,
  1908. flush_req);
  1909. ctx_isp->active_req_cnt = 0;
  1910. spin_unlock_bh(&ctx->lock);
  1911. reset_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  1912. rc = ctx->hw_mgr_intf->hw_reset(ctx->hw_mgr_intf->hw_mgr_priv,
  1913. &reset_args);
  1914. if (rc)
  1915. CAM_ERR(CAM_ISP, "Failed to reset HW rc: %d", rc);
  1916. ctx->state = CAM_CTX_FLUSHED;
  1917. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  1918. ctx_isp->init_received = false;
  1919. }
  1920. end:
  1921. ctx_isp->bubble_frame_cnt = 0;
  1922. atomic_set(&ctx_isp->process_bubble, 0);
  1923. return rc;
  1924. }
  1925. static int __cam_isp_ctx_flush_req_in_ready(
  1926. struct cam_context *ctx,
  1927. struct cam_req_mgr_flush_request *flush_req)
  1928. {
  1929. int rc = 0;
  1930. CAM_DBG(CAM_ISP, "try to flush pending list");
  1931. spin_lock_bh(&ctx->lock);
  1932. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  1933. /* if nothing is in pending req list, change state to acquire */
  1934. if (list_empty(&ctx->pending_req_list))
  1935. ctx->state = CAM_CTX_ACQUIRED;
  1936. spin_unlock_bh(&ctx->lock);
  1937. trace_cam_context_state("ISP", ctx);
  1938. CAM_DBG(CAM_ISP, "Flush request in ready state. next state %d",
  1939. ctx->state);
  1940. return rc;
  1941. }
  1942. static struct cam_ctx_ops
  1943. cam_isp_ctx_activated_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  1944. /* SOF */
  1945. {
  1946. .ioctl_ops = {},
  1947. .crm_ops = {
  1948. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  1949. },
  1950. .irq_ops = NULL,
  1951. },
  1952. /* APPLIED */
  1953. {
  1954. .ioctl_ops = {},
  1955. .crm_ops = {},
  1956. .irq_ops = NULL,
  1957. },
  1958. /* EPOCH */
  1959. {
  1960. .ioctl_ops = {},
  1961. .crm_ops = {
  1962. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  1963. },
  1964. .irq_ops = NULL,
  1965. },
  1966. /* BUBBLE */
  1967. {
  1968. .ioctl_ops = {},
  1969. .crm_ops = {
  1970. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  1971. },
  1972. .irq_ops = NULL,
  1973. },
  1974. /* Bubble Applied */
  1975. {
  1976. .ioctl_ops = {},
  1977. .crm_ops = {},
  1978. .irq_ops = NULL,
  1979. },
  1980. /* HW ERROR */
  1981. {
  1982. .ioctl_ops = {},
  1983. .crm_ops = {},
  1984. .irq_ops = NULL,
  1985. },
  1986. /* HALT */
  1987. {
  1988. .ioctl_ops = {},
  1989. .crm_ops = {},
  1990. .irq_ops = NULL,
  1991. },
  1992. };
  1993. static struct cam_ctx_ops
  1994. cam_isp_ctx_fs2_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  1995. /* SOF */
  1996. {
  1997. .ioctl_ops = {},
  1998. .crm_ops = {
  1999. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  2000. },
  2001. .irq_ops = NULL,
  2002. },
  2003. /* APPLIED */
  2004. {
  2005. .ioctl_ops = {},
  2006. .crm_ops = {},
  2007. .irq_ops = NULL,
  2008. },
  2009. /* EPOCH */
  2010. {
  2011. .ioctl_ops = {},
  2012. .crm_ops = {
  2013. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  2014. },
  2015. .irq_ops = NULL,
  2016. },
  2017. /* BUBBLE */
  2018. {
  2019. .ioctl_ops = {},
  2020. .crm_ops = {
  2021. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  2022. },
  2023. .irq_ops = NULL,
  2024. },
  2025. /* Bubble Applied */
  2026. {
  2027. .ioctl_ops = {},
  2028. .crm_ops = {},
  2029. .irq_ops = NULL,
  2030. },
  2031. /* HW ERROR */
  2032. {
  2033. .ioctl_ops = {},
  2034. .crm_ops = {},
  2035. .irq_ops = NULL,
  2036. },
  2037. /* HALT */
  2038. {
  2039. .ioctl_ops = {},
  2040. .crm_ops = {},
  2041. .irq_ops = NULL,
  2042. },
  2043. };
  2044. static int __cam_isp_ctx_rdi_only_sof_in_top_state(
  2045. struct cam_isp_context *ctx_isp, void *evt_data)
  2046. {
  2047. int rc = 0;
  2048. struct cam_context *ctx = ctx_isp->base;
  2049. struct cam_req_mgr_trigger_notify notify;
  2050. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  2051. uint64_t request_id = 0;
  2052. if (!evt_data) {
  2053. CAM_ERR(CAM_ISP, "in valid sof event data");
  2054. return -EINVAL;
  2055. }
  2056. ctx_isp->frame_id++;
  2057. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  2058. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  2059. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  2060. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  2061. /*
  2062. * notify reqmgr with sof signal. Note, due to scheduling delay
  2063. * we can run into situation that two active requests has already
  2064. * be in the active queue while we try to do the notification.
  2065. * In this case, we need to skip the current notification. This
  2066. * helps the state machine to catch up the delay.
  2067. */
  2068. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  2069. ctx_isp->active_req_cnt <= 2) {
  2070. notify.link_hdl = ctx->link_hdl;
  2071. notify.dev_hdl = ctx->dev_hdl;
  2072. notify.frame_id = ctx_isp->frame_id;
  2073. notify.trigger = CAM_TRIGGER_POINT_SOF;
  2074. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  2075. ctx->ctx_crm_intf->notify_trigger(&notify);
  2076. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  2077. ctx_isp->frame_id);
  2078. /*
  2079. * It is idle frame with out any applied request id, send
  2080. * request id as zero
  2081. */
  2082. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2083. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2084. } else {
  2085. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not notify SOF to CRM");
  2086. }
  2087. if (list_empty(&ctx->active_req_list))
  2088. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  2089. else
  2090. CAM_DBG(CAM_ISP, "Still need to wait for the buf done");
  2091. CAM_DBG(CAM_ISP, "next substate %d",
  2092. ctx_isp->substate_activated);
  2093. return rc;
  2094. }
  2095. static int __cam_isp_ctx_rdi_only_sof_in_applied_state(
  2096. struct cam_isp_context *ctx_isp, void *evt_data)
  2097. {
  2098. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  2099. if (!evt_data) {
  2100. CAM_ERR(CAM_ISP, "in valid sof event data");
  2101. return -EINVAL;
  2102. }
  2103. ctx_isp->frame_id++;
  2104. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  2105. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  2106. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  2107. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  2108. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED;
  2109. CAM_DBG(CAM_ISP, "next substate %d", ctx_isp->substate_activated);
  2110. return 0;
  2111. }
  2112. static int __cam_isp_ctx_rdi_only_sof_in_bubble_applied(
  2113. struct cam_isp_context *ctx_isp, void *evt_data)
  2114. {
  2115. struct cam_ctx_request *req;
  2116. struct cam_isp_ctx_req *req_isp;
  2117. struct cam_context *ctx = ctx_isp->base;
  2118. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  2119. uint64_t request_id = 0;
  2120. /*
  2121. * Sof in bubble applied state means, reg update not received.
  2122. * before increment frame id and override time stamp value, send
  2123. * the previous sof time stamp that got captured in the
  2124. * sof in applied state.
  2125. */
  2126. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  2127. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  2128. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2129. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2130. ctx_isp->frame_id++;
  2131. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  2132. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  2133. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  2134. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  2135. if (list_empty(&ctx->wait_req_list)) {
  2136. /*
  2137. * If no pending req in epoch, this is an error case.
  2138. * The recovery is to go back to sof state
  2139. */
  2140. CAM_ERR(CAM_ISP, "No wait request");
  2141. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  2142. /* Send SOF event as empty frame*/
  2143. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2144. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2145. goto end;
  2146. }
  2147. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  2148. list);
  2149. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  2150. req_isp->bubble_detected = true;
  2151. req_isp->reapply = true;
  2152. CAM_DBG(CAM_ISP, "Report Bubble flag %d", req_isp->bubble_report);
  2153. if (req_isp->bubble_report && ctx->ctx_crm_intf &&
  2154. ctx->ctx_crm_intf->notify_err) {
  2155. struct cam_req_mgr_error_notify notify;
  2156. notify.link_hdl = ctx->link_hdl;
  2157. notify.dev_hdl = ctx->dev_hdl;
  2158. notify.req_id = req->request_id;
  2159. notify.error = CRM_KMD_ERR_BUBBLE;
  2160. CAM_WARN(CAM_ISP,
  2161. "Notify CRM about Bubble req %lld frame %lld ctx %u",
  2162. req->request_id,
  2163. ctx_isp->frame_id,
  2164. ctx->ctx_id);
  2165. ctx->ctx_crm_intf->notify_err(&notify);
  2166. } else {
  2167. req_isp->bubble_report = 0;
  2168. }
  2169. /*
  2170. * Always move the request to active list. Let buf done
  2171. * function handles the rest.
  2172. */
  2173. list_del_init(&req->list);
  2174. list_add_tail(&req->list, &ctx->active_req_list);
  2175. ctx_isp->active_req_cnt++;
  2176. CAM_DBG(CAM_ISP, "move request %lld to active list(cnt = %d)",
  2177. req->request_id, ctx_isp->active_req_cnt);
  2178. if (!req_isp->bubble_report) {
  2179. if (req->request_id > ctx_isp->reported_req_id) {
  2180. request_id = req->request_id;
  2181. ctx_isp->reported_req_id = request_id;
  2182. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2183. CAM_REQ_MGR_SOF_EVENT_ERROR);
  2184. } else
  2185. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2186. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2187. } else
  2188. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2189. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2190. /* change the state to bubble, as reg update has not come */
  2191. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  2192. CAM_DBG(CAM_ISP, "next substate %d", ctx_isp->substate_activated);
  2193. end:
  2194. return 0;
  2195. }
  2196. static int __cam_isp_ctx_rdi_only_sof_in_bubble_state(
  2197. struct cam_isp_context *ctx_isp, void *evt_data)
  2198. {
  2199. uint32_t i;
  2200. struct cam_ctx_request *req;
  2201. struct cam_context *ctx = ctx_isp->base;
  2202. struct cam_req_mgr_trigger_notify notify;
  2203. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  2204. struct cam_isp_ctx_req *req_isp;
  2205. uint64_t request_id = 0;
  2206. if (!evt_data) {
  2207. CAM_ERR(CAM_ISP, "in valid sof event data");
  2208. return -EINVAL;
  2209. }
  2210. ctx_isp->frame_id++;
  2211. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  2212. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  2213. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  2214. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  2215. /*
  2216. * Signal all active requests with error and move the all the active
  2217. * requests to free list
  2218. */
  2219. while (!list_empty(&ctx->active_req_list)) {
  2220. req = list_first_entry(&ctx->active_req_list,
  2221. struct cam_ctx_request, list);
  2222. list_del_init(&req->list);
  2223. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2224. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d",
  2225. req_isp->num_fence_map_out);
  2226. for (i = 0; i < req_isp->num_fence_map_out; i++)
  2227. if (req_isp->fence_map_out[i].sync_id != -1) {
  2228. cam_sync_signal(
  2229. req_isp->fence_map_out[i].sync_id,
  2230. CAM_SYNC_STATE_SIGNALED_ERROR);
  2231. }
  2232. list_add_tail(&req->list, &ctx->free_req_list);
  2233. ctx_isp->active_req_cnt--;
  2234. }
  2235. /* notify reqmgr with sof signal */
  2236. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger) {
  2237. notify.link_hdl = ctx->link_hdl;
  2238. notify.dev_hdl = ctx->dev_hdl;
  2239. notify.frame_id = ctx_isp->frame_id;
  2240. notify.trigger = CAM_TRIGGER_POINT_SOF;
  2241. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  2242. ctx->ctx_crm_intf->notify_trigger(&notify);
  2243. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  2244. ctx_isp->frame_id);
  2245. } else {
  2246. CAM_ERR(CAM_ISP, "Can not notify SOF to CRM");
  2247. }
  2248. /*
  2249. * It is idle frame with out any applied request id, send
  2250. * request id as zero
  2251. */
  2252. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2253. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2254. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  2255. CAM_DBG(CAM_ISP, "next substate %d",
  2256. ctx_isp->substate_activated);
  2257. return 0;
  2258. }
  2259. static int __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state(
  2260. struct cam_isp_context *ctx_isp, void *evt_data)
  2261. {
  2262. struct cam_ctx_request *req;
  2263. struct cam_context *ctx = ctx_isp->base;
  2264. struct cam_isp_ctx_req *req_isp;
  2265. struct cam_req_mgr_trigger_notify notify;
  2266. uint64_t request_id = 0;
  2267. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  2268. /* notify reqmgr with sof signal*/
  2269. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger) {
  2270. if (list_empty(&ctx->wait_req_list)) {
  2271. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request");
  2272. goto error;
  2273. }
  2274. req = list_first_entry(&ctx->wait_req_list,
  2275. struct cam_ctx_request, list);
  2276. list_del_init(&req->list);
  2277. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2278. request_id =
  2279. (req_isp->hw_update_data.packet_opcode_type ==
  2280. CAM_ISP_PACKET_INIT_DEV) ?
  2281. 0 : req->request_id;
  2282. if (req_isp->num_fence_map_out != 0) {
  2283. list_add_tail(&req->list, &ctx->active_req_list);
  2284. ctx_isp->active_req_cnt++;
  2285. CAM_DBG(CAM_ISP,
  2286. "move request %lld to active list(cnt = %d)",
  2287. req->request_id, ctx_isp->active_req_cnt);
  2288. /* if packet has buffers, set correct request id */
  2289. request_id = req->request_id;
  2290. } else {
  2291. /* no io config, so the request is completed. */
  2292. list_add_tail(&req->list, &ctx->free_req_list);
  2293. CAM_DBG(CAM_ISP,
  2294. "move active req %lld to free list(cnt=%d)",
  2295. req->request_id, ctx_isp->active_req_cnt);
  2296. }
  2297. notify.link_hdl = ctx->link_hdl;
  2298. notify.dev_hdl = ctx->dev_hdl;
  2299. notify.frame_id = ctx_isp->frame_id;
  2300. notify.trigger = CAM_TRIGGER_POINT_SOF;
  2301. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  2302. ctx->ctx_crm_intf->notify_trigger(&notify);
  2303. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  2304. ctx_isp->frame_id);
  2305. } else {
  2306. CAM_ERR(CAM_ISP, "Can not notify SOF to CRM");
  2307. }
  2308. if (request_id)
  2309. ctx_isp->reported_req_id = request_id;
  2310. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2311. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2312. CAM_DBG(CAM_ISP, "next substate %d", ctx_isp->substate_activated);
  2313. return 0;
  2314. error:
  2315. /* Send SOF event as idle frame*/
  2316. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2317. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2318. /*
  2319. * There is no request in the pending list, move the sub state machine
  2320. * to SOF sub state
  2321. */
  2322. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  2323. return 0;
  2324. }
  2325. static struct cam_isp_ctx_irq_ops
  2326. cam_isp_ctx_rdi_only_activated_state_machine_irq
  2327. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  2328. /* SOF */
  2329. {
  2330. .irq_ops = {
  2331. NULL,
  2332. __cam_isp_ctx_rdi_only_sof_in_top_state,
  2333. __cam_isp_ctx_reg_upd_in_sof,
  2334. NULL,
  2335. NULL,
  2336. NULL,
  2337. },
  2338. },
  2339. /* APPLIED */
  2340. {
  2341. .irq_ops = {
  2342. __cam_isp_ctx_handle_error,
  2343. __cam_isp_ctx_rdi_only_sof_in_applied_state,
  2344. NULL,
  2345. NULL,
  2346. NULL,
  2347. __cam_isp_ctx_buf_done_in_applied,
  2348. },
  2349. },
  2350. /* EPOCH */
  2351. {
  2352. .irq_ops = {
  2353. __cam_isp_ctx_handle_error,
  2354. __cam_isp_ctx_rdi_only_sof_in_top_state,
  2355. NULL,
  2356. NULL,
  2357. NULL,
  2358. __cam_isp_ctx_buf_done_in_epoch,
  2359. },
  2360. },
  2361. /* BUBBLE*/
  2362. {
  2363. .irq_ops = {
  2364. __cam_isp_ctx_handle_error,
  2365. __cam_isp_ctx_rdi_only_sof_in_bubble_state,
  2366. NULL,
  2367. NULL,
  2368. NULL,
  2369. __cam_isp_ctx_buf_done_in_bubble,
  2370. },
  2371. },
  2372. /* BUBBLE APPLIED ie PRE_BUBBLE */
  2373. {
  2374. .irq_ops = {
  2375. __cam_isp_ctx_handle_error,
  2376. __cam_isp_ctx_rdi_only_sof_in_bubble_applied,
  2377. __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state,
  2378. NULL,
  2379. NULL,
  2380. __cam_isp_ctx_buf_done_in_bubble_applied,
  2381. },
  2382. },
  2383. /* HW ERROR */
  2384. {
  2385. },
  2386. /* HALT */
  2387. {
  2388. },
  2389. };
  2390. static int __cam_isp_ctx_rdi_only_apply_req_top_state(
  2391. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  2392. {
  2393. int rc = 0;
  2394. struct cam_isp_context *ctx_isp =
  2395. (struct cam_isp_context *) ctx->ctx_priv;
  2396. CAM_DBG(CAM_ISP, "current substate %d",
  2397. ctx_isp->substate_activated);
  2398. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  2399. CAM_ISP_CTX_ACTIVATED_APPLIED);
  2400. CAM_DBG(CAM_ISP, "new substate %d", ctx_isp->substate_activated);
  2401. if (rc)
  2402. CAM_ERR(CAM_ISP, "Apply failed in state %d, rc %d",
  2403. ctx_isp->substate_activated, rc);
  2404. return rc;
  2405. }
  2406. static struct cam_ctx_ops
  2407. cam_isp_ctx_rdi_only_activated_state_machine
  2408. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  2409. /* SOF */
  2410. {
  2411. .ioctl_ops = {},
  2412. .crm_ops = {
  2413. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  2414. },
  2415. .irq_ops = NULL,
  2416. },
  2417. /* APPLIED */
  2418. {
  2419. .ioctl_ops = {},
  2420. .crm_ops = {},
  2421. .irq_ops = NULL,
  2422. },
  2423. /* EPOCH */
  2424. {
  2425. .ioctl_ops = {},
  2426. .crm_ops = {
  2427. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  2428. },
  2429. .irq_ops = NULL,
  2430. },
  2431. /* PRE BUBBLE */
  2432. {
  2433. .ioctl_ops = {},
  2434. .crm_ops = {},
  2435. .irq_ops = NULL,
  2436. },
  2437. /* BUBBLE */
  2438. {
  2439. .ioctl_ops = {},
  2440. .crm_ops = {},
  2441. .irq_ops = NULL,
  2442. },
  2443. /* HW ERROR */
  2444. {
  2445. .ioctl_ops = {},
  2446. .crm_ops = {},
  2447. .irq_ops = NULL,
  2448. },
  2449. /* HALT */
  2450. {
  2451. .ioctl_ops = {},
  2452. .crm_ops = {},
  2453. .irq_ops = NULL,
  2454. },
  2455. };
  2456. static int __cam_isp_ctx_release_hw_in_top_state(struct cam_context *ctx,
  2457. void *cmd)
  2458. {
  2459. int rc = 0;
  2460. struct cam_hw_release_args rel_arg;
  2461. struct cam_isp_context *ctx_isp =
  2462. (struct cam_isp_context *) ctx->ctx_priv;
  2463. struct cam_req_mgr_flush_request flush_req;
  2464. if (ctx_isp->hw_ctx) {
  2465. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2466. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  2467. &rel_arg);
  2468. ctx_isp->hw_ctx = NULL;
  2469. } else {
  2470. CAM_ERR(CAM_ISP, "No hw resources acquired for this ctx");
  2471. }
  2472. ctx->last_flush_req = 0;
  2473. ctx_isp->frame_id = 0;
  2474. ctx_isp->active_req_cnt = 0;
  2475. ctx_isp->reported_req_id = 0;
  2476. ctx_isp->hw_acquired = false;
  2477. ctx_isp->init_received = false;
  2478. atomic64_set(&ctx_isp->state_monitor_head, -1);
  2479. /*
  2480. * Ideally, we should never have any active request here.
  2481. * But we still add some sanity check code here to help the debug
  2482. */
  2483. if (!list_empty(&ctx->active_req_list))
  2484. CAM_WARN(CAM_ISP, "Active list is not empty");
  2485. /* Flush all the pending request list */
  2486. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  2487. flush_req.link_hdl = ctx->link_hdl;
  2488. flush_req.dev_hdl = ctx->dev_hdl;
  2489. CAM_DBG(CAM_ISP, "try to flush pending list");
  2490. spin_lock_bh(&ctx->lock);
  2491. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  2492. spin_unlock_bh(&ctx->lock);
  2493. ctx->state = CAM_CTX_ACQUIRED;
  2494. trace_cam_context_state("ISP", ctx);
  2495. CAM_DBG(CAM_ISP, "Release device success[%u] next state %d",
  2496. ctx->ctx_id, ctx->state);
  2497. return rc;
  2498. }
  2499. /* top level state machine */
  2500. static int __cam_isp_ctx_release_dev_in_top_state(struct cam_context *ctx,
  2501. struct cam_release_dev_cmd *cmd)
  2502. {
  2503. int rc = 0;
  2504. struct cam_hw_release_args rel_arg;
  2505. struct cam_isp_context *ctx_isp =
  2506. (struct cam_isp_context *) ctx->ctx_priv;
  2507. struct cam_req_mgr_flush_request flush_req;
  2508. if (cmd && ctx_isp->hw_ctx) {
  2509. CAM_ERR(CAM_ISP, "releasing hw");
  2510. __cam_isp_ctx_release_hw_in_top_state(ctx, NULL);
  2511. }
  2512. if (ctx_isp->hw_ctx) {
  2513. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2514. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  2515. &rel_arg);
  2516. ctx_isp->hw_ctx = NULL;
  2517. }
  2518. ctx->session_hdl = -1;
  2519. ctx->dev_hdl = -1;
  2520. ctx->link_hdl = -1;
  2521. ctx->ctx_crm_intf = NULL;
  2522. ctx->last_flush_req = 0;
  2523. ctx_isp->frame_id = 0;
  2524. ctx_isp->active_req_cnt = 0;
  2525. ctx_isp->reported_req_id = 0;
  2526. ctx_isp->hw_acquired = false;
  2527. ctx_isp->init_received = false;
  2528. ctx_isp->rdi_only_context = false;
  2529. atomic64_set(&ctx_isp->state_monitor_head, -1);
  2530. /*
  2531. * Ideally, we should never have any active request here.
  2532. * But we still add some sanity check code here to help the debug
  2533. */
  2534. if (!list_empty(&ctx->active_req_list))
  2535. CAM_ERR(CAM_ISP, "Active list is not empty");
  2536. /* Flush all the pending request list */
  2537. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  2538. flush_req.link_hdl = ctx->link_hdl;
  2539. flush_req.dev_hdl = ctx->dev_hdl;
  2540. CAM_DBG(CAM_ISP, "try to flush pending list");
  2541. spin_lock_bh(&ctx->lock);
  2542. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  2543. spin_unlock_bh(&ctx->lock);
  2544. ctx->state = CAM_CTX_AVAILABLE;
  2545. trace_cam_context_state("ISP", ctx);
  2546. CAM_DBG(CAM_ISP, "Release device success[%u] next state %d",
  2547. ctx->ctx_id, ctx->state);
  2548. return rc;
  2549. }
  2550. static int __cam_isp_ctx_config_dev_in_top_state(
  2551. struct cam_context *ctx, struct cam_config_dev_cmd *cmd)
  2552. {
  2553. int rc = 0, i;
  2554. struct cam_ctx_request *req = NULL;
  2555. struct cam_isp_ctx_req *req_isp;
  2556. uintptr_t packet_addr;
  2557. struct cam_packet *packet;
  2558. size_t len = 0;
  2559. size_t remain_len = 0;
  2560. struct cam_hw_prepare_update_args cfg;
  2561. struct cam_req_mgr_add_request add_req;
  2562. struct cam_isp_context *ctx_isp =
  2563. (struct cam_isp_context *) ctx->ctx_priv;
  2564. CAM_DBG(CAM_ISP, "get free request object......");
  2565. /* get free request */
  2566. spin_lock_bh(&ctx->lock);
  2567. if (!list_empty(&ctx->free_req_list)) {
  2568. req = list_first_entry(&ctx->free_req_list,
  2569. struct cam_ctx_request, list);
  2570. list_del_init(&req->list);
  2571. }
  2572. spin_unlock_bh(&ctx->lock);
  2573. if (!req) {
  2574. CAM_ERR(CAM_ISP, "No more request obj free");
  2575. return -ENOMEM;
  2576. }
  2577. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2578. /* for config dev, only memory handle is supported */
  2579. /* map packet from the memhandle */
  2580. rc = cam_mem_get_cpu_buf((int32_t) cmd->packet_handle,
  2581. &packet_addr, &len);
  2582. if (rc != 0) {
  2583. CAM_ERR(CAM_ISP, "Can not get packet address");
  2584. rc = -EINVAL;
  2585. goto free_req;
  2586. }
  2587. remain_len = len;
  2588. if ((len < sizeof(struct cam_packet)) ||
  2589. ((size_t)cmd->offset >= len - sizeof(struct cam_packet))) {
  2590. CAM_ERR(CAM_ISP, "invalid buff length: %zu or offset", len);
  2591. rc = -EINVAL;
  2592. goto free_req;
  2593. }
  2594. remain_len -= (size_t)cmd->offset;
  2595. packet = (struct cam_packet *)(packet_addr + (uint32_t)cmd->offset);
  2596. CAM_DBG(CAM_ISP, "pack_handle %llx", cmd->packet_handle);
  2597. CAM_DBG(CAM_ISP, "packet address is 0x%zx", packet_addr);
  2598. CAM_DBG(CAM_ISP, "packet with length %zu, offset 0x%llx",
  2599. len, cmd->offset);
  2600. CAM_DBG(CAM_ISP, "Packet request id %lld",
  2601. packet->header.request_id);
  2602. CAM_DBG(CAM_ISP, "Packet size 0x%x", packet->header.size);
  2603. CAM_DBG(CAM_ISP, "packet op %d", packet->header.op_code);
  2604. if ((((packet->header.op_code + 1) & 0xF) == CAM_ISP_PACKET_UPDATE_DEV)
  2605. && (packet->header.request_id <= ctx->last_flush_req)) {
  2606. CAM_INFO(CAM_ISP,
  2607. "request %lld has been flushed, reject packet",
  2608. packet->header.request_id);
  2609. rc = -EBADR;
  2610. goto free_req;
  2611. }
  2612. /* preprocess the configuration */
  2613. memset(&cfg, 0, sizeof(cfg));
  2614. cfg.packet = packet;
  2615. cfg.remain_len = remain_len;
  2616. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2617. cfg.max_hw_update_entries = CAM_ISP_CTX_CFG_MAX;
  2618. cfg.hw_update_entries = req_isp->cfg;
  2619. cfg.max_out_map_entries = CAM_ISP_CTX_RES_MAX;
  2620. cfg.max_in_map_entries = CAM_ISP_CTX_RES_MAX;
  2621. cfg.out_map_entries = req_isp->fence_map_out;
  2622. cfg.in_map_entries = req_isp->fence_map_in;
  2623. cfg.priv = &req_isp->hw_update_data;
  2624. cfg.pf_data = &(req->pf_data);
  2625. CAM_DBG(CAM_ISP, "try to prepare config packet......");
  2626. rc = ctx->hw_mgr_intf->hw_prepare_update(
  2627. ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  2628. if (rc != 0) {
  2629. CAM_ERR(CAM_ISP, "Prepare config packet failed in HW layer");
  2630. rc = -EFAULT;
  2631. goto free_req;
  2632. }
  2633. req_isp->num_cfg = cfg.num_hw_update_entries;
  2634. req_isp->num_fence_map_out = cfg.num_out_map_entries;
  2635. req_isp->num_fence_map_in = cfg.num_in_map_entries;
  2636. req_isp->num_acked = 0;
  2637. req_isp->bubble_detected = false;
  2638. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  2639. rc = cam_sync_get_obj_ref(req_isp->fence_map_out[i].sync_id);
  2640. if (rc) {
  2641. CAM_ERR(CAM_ISP, "Can't get ref for fence %d",
  2642. req_isp->fence_map_out[i].sync_id);
  2643. goto put_ref;
  2644. }
  2645. }
  2646. CAM_DBG(CAM_ISP, "num_entry: %d, num fence out: %d, num fence in: %d",
  2647. req_isp->num_cfg, req_isp->num_fence_map_out,
  2648. req_isp->num_fence_map_in);
  2649. req->request_id = packet->header.request_id;
  2650. req->status = 1;
  2651. CAM_DBG(CAM_ISP, "Packet request id %lld packet opcode:%d",
  2652. packet->header.request_id,
  2653. req_isp->hw_update_data.packet_opcode_type);
  2654. if (req_isp->hw_update_data.packet_opcode_type ==
  2655. CAM_ISP_PACKET_INIT_DEV) {
  2656. if (ctx->state < CAM_CTX_ACTIVATED) {
  2657. rc = __cam_isp_ctx_enqueue_init_request(ctx, req);
  2658. if (rc)
  2659. CAM_ERR(CAM_ISP, "Enqueue INIT pkt failed");
  2660. ctx_isp->init_received = true;
  2661. } else {
  2662. rc = -EINVAL;
  2663. CAM_ERR(CAM_ISP, "Recevied INIT pkt in wrong state");
  2664. }
  2665. } else {
  2666. if (ctx->state >= CAM_CTX_READY && ctx->ctx_crm_intf->add_req) {
  2667. add_req.link_hdl = ctx->link_hdl;
  2668. add_req.dev_hdl = ctx->dev_hdl;
  2669. add_req.req_id = req->request_id;
  2670. add_req.skip_before_applying = 0;
  2671. rc = ctx->ctx_crm_intf->add_req(&add_req);
  2672. if (rc) {
  2673. CAM_ERR(CAM_ISP, "Add req failed: req id=%llu",
  2674. req->request_id);
  2675. } else {
  2676. __cam_isp_ctx_enqueue_request_in_order(
  2677. ctx, req);
  2678. }
  2679. } else {
  2680. rc = -EINVAL;
  2681. CAM_ERR(CAM_ISP, "Recevied Update in wrong state");
  2682. }
  2683. }
  2684. if (rc)
  2685. goto put_ref;
  2686. CAM_DBG(CAM_REQ,
  2687. "Preprocessing Config req_id %lld successful on ctx %u",
  2688. req->request_id, ctx->ctx_id);
  2689. return rc;
  2690. put_ref:
  2691. for (--i; i >= 0; i--) {
  2692. if (cam_sync_put_obj_ref(req_isp->fence_map_out[i].sync_id))
  2693. CAM_ERR(CAM_CTXT, "Failed to put ref of fence %d",
  2694. req_isp->fence_map_out[i].sync_id);
  2695. }
  2696. free_req:
  2697. spin_lock_bh(&ctx->lock);
  2698. list_add_tail(&req->list, &ctx->free_req_list);
  2699. spin_unlock_bh(&ctx->lock);
  2700. return rc;
  2701. }
  2702. static int __cam_isp_ctx_acquire_dev_in_available(struct cam_context *ctx,
  2703. struct cam_acquire_dev_cmd *cmd)
  2704. {
  2705. int rc = 0;
  2706. struct cam_hw_acquire_args param;
  2707. struct cam_isp_resource *isp_res = NULL;
  2708. struct cam_create_dev_hdl req_hdl_param;
  2709. struct cam_hw_release_args release;
  2710. struct cam_isp_context *ctx_isp =
  2711. (struct cam_isp_context *) ctx->ctx_priv;
  2712. struct cam_hw_cmd_args hw_cmd_args;
  2713. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  2714. if (!ctx->hw_mgr_intf) {
  2715. CAM_ERR(CAM_ISP, "HW interface is not ready");
  2716. rc = -EFAULT;
  2717. goto end;
  2718. }
  2719. CAM_DBG(CAM_ISP,
  2720. "session_hdl 0x%x, num_resources %d, hdl type %d, res %lld",
  2721. cmd->session_handle, cmd->num_resources,
  2722. cmd->handle_type, cmd->resource_hdl);
  2723. if (cmd->num_resources == CAM_API_COMPAT_CONSTANT) {
  2724. ctx_isp->split_acquire = true;
  2725. CAM_DBG(CAM_ISP, "Acquire dev handle");
  2726. goto get_dev_handle;
  2727. }
  2728. if (cmd->num_resources > CAM_ISP_CTX_RES_MAX) {
  2729. CAM_ERR(CAM_ISP, "Too much resources in the acquire");
  2730. rc = -ENOMEM;
  2731. goto end;
  2732. }
  2733. /* for now we only support user pointer */
  2734. if (cmd->handle_type != 1) {
  2735. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  2736. rc = -EINVAL;
  2737. goto end;
  2738. }
  2739. isp_res = kzalloc(
  2740. sizeof(*isp_res)*cmd->num_resources, GFP_KERNEL);
  2741. if (!isp_res) {
  2742. rc = -ENOMEM;
  2743. goto end;
  2744. }
  2745. CAM_DBG(CAM_ISP, "start copy %d resources from user",
  2746. cmd->num_resources);
  2747. if (copy_from_user(isp_res, u64_to_user_ptr(cmd->resource_hdl),
  2748. sizeof(*isp_res)*cmd->num_resources)) {
  2749. rc = -EFAULT;
  2750. goto free_res;
  2751. }
  2752. param.context_data = ctx;
  2753. param.event_cb = ctx->irq_cb_intf;
  2754. param.num_acq = cmd->num_resources;
  2755. param.acquire_info = (uintptr_t) isp_res;
  2756. /* call HW manager to reserve the resource */
  2757. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  2758. &param);
  2759. if (rc != 0) {
  2760. CAM_ERR(CAM_ISP, "Acquire device failed");
  2761. goto free_res;
  2762. }
  2763. /* Query the context has rdi only resource */
  2764. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  2765. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  2766. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  2767. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  2768. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  2769. &hw_cmd_args);
  2770. if (rc) {
  2771. CAM_ERR(CAM_ISP, "HW command failed");
  2772. goto free_hw;
  2773. }
  2774. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  2775. /*
  2776. * this context has rdi only resource assign rdi only
  2777. * state machine
  2778. */
  2779. CAM_DBG(CAM_ISP, "RDI only session Context");
  2780. ctx_isp->substate_machine_irq =
  2781. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  2782. ctx_isp->substate_machine =
  2783. cam_isp_ctx_rdi_only_activated_state_machine;
  2784. ctx_isp->rdi_only_context = true;
  2785. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  2786. CAM_DBG(CAM_ISP, "FS2 Session has PIX ,RD and RDI");
  2787. ctx_isp->substate_machine_irq =
  2788. cam_isp_ctx_fs2_state_machine_irq;
  2789. ctx_isp->substate_machine =
  2790. cam_isp_ctx_fs2_state_machine;
  2791. } else {
  2792. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  2793. ctx_isp->substate_machine_irq =
  2794. cam_isp_ctx_activated_state_machine_irq;
  2795. ctx_isp->substate_machine =
  2796. cam_isp_ctx_activated_state_machine;
  2797. }
  2798. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  2799. ctx_isp->hw_acquired = true;
  2800. ctx_isp->split_acquire = false;
  2801. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  2802. atomic64_set(&ctx_isp->state_monitor_head, -1);
  2803. kfree(isp_res);
  2804. isp_res = NULL;
  2805. get_dev_handle:
  2806. req_hdl_param.session_hdl = cmd->session_handle;
  2807. /* bridge is not ready for these flags. so false for now */
  2808. req_hdl_param.v4l2_sub_dev_flag = 0;
  2809. req_hdl_param.media_entity_flag = 0;
  2810. req_hdl_param.ops = ctx->crm_ctx_intf;
  2811. req_hdl_param.priv = ctx;
  2812. CAM_DBG(CAM_ISP, "get device handle form bridge");
  2813. ctx->dev_hdl = cam_create_device_hdl(&req_hdl_param);
  2814. if (ctx->dev_hdl <= 0) {
  2815. rc = -EFAULT;
  2816. CAM_ERR(CAM_ISP, "Can not create device handle");
  2817. goto free_hw;
  2818. }
  2819. cmd->dev_handle = ctx->dev_hdl;
  2820. /* store session information */
  2821. ctx->session_hdl = cmd->session_handle;
  2822. ctx->state = CAM_CTX_ACQUIRED;
  2823. trace_cam_context_state("ISP", ctx);
  2824. CAM_DBG(CAM_ISP,
  2825. "Acquire success on session_hdl 0x%x num_rsrces %d ctx %u",
  2826. cmd->session_handle, cmd->num_resources, ctx->ctx_id);
  2827. return rc;
  2828. free_hw:
  2829. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2830. if (ctx_isp->hw_acquired)
  2831. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  2832. &release);
  2833. ctx_isp->hw_ctx = NULL;
  2834. ctx_isp->hw_acquired = false;
  2835. free_res:
  2836. kfree(isp_res);
  2837. end:
  2838. return rc;
  2839. }
  2840. static int __cam_isp_ctx_acquire_hw_v1(struct cam_context *ctx,
  2841. void *args)
  2842. {
  2843. int rc = 0;
  2844. struct cam_acquire_hw_cmd_v1 *cmd =
  2845. (struct cam_acquire_hw_cmd_v1 *)args;
  2846. struct cam_hw_acquire_args param;
  2847. struct cam_hw_release_args release;
  2848. struct cam_isp_context *ctx_isp =
  2849. (struct cam_isp_context *) ctx->ctx_priv;
  2850. struct cam_hw_cmd_args hw_cmd_args;
  2851. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  2852. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  2853. if (!ctx->hw_mgr_intf) {
  2854. CAM_ERR(CAM_ISP, "HW interface is not ready");
  2855. rc = -EFAULT;
  2856. goto end;
  2857. }
  2858. CAM_DBG(CAM_ISP,
  2859. "session_hdl 0x%x, hdl type %d, res %lld",
  2860. cmd->session_handle, cmd->handle_type, cmd->resource_hdl);
  2861. /* for now we only support user pointer */
  2862. if (cmd->handle_type != 1) {
  2863. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  2864. rc = -EINVAL;
  2865. goto end;
  2866. }
  2867. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  2868. CAM_ERR(CAM_ISP, "data_size is not a valid value");
  2869. goto end;
  2870. }
  2871. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  2872. if (!acquire_hw_info) {
  2873. rc = -ENOMEM;
  2874. goto end;
  2875. }
  2876. CAM_DBG(CAM_ISP, "start copy resources from user");
  2877. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  2878. cmd->data_size)) {
  2879. rc = -EFAULT;
  2880. goto free_res;
  2881. }
  2882. memset(&param, 0, sizeof(param));
  2883. param.context_data = ctx;
  2884. param.event_cb = ctx->irq_cb_intf;
  2885. param.num_acq = CAM_API_COMPAT_CONSTANT;
  2886. param.acquire_info_size = cmd->data_size;
  2887. param.acquire_info = (uint64_t) acquire_hw_info;
  2888. /* call HW manager to reserve the resource */
  2889. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  2890. &param);
  2891. if (rc != 0) {
  2892. CAM_ERR(CAM_ISP, "Acquire device failed");
  2893. goto free_res;
  2894. }
  2895. /* Query the context has rdi only resource */
  2896. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  2897. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  2898. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  2899. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  2900. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  2901. &hw_cmd_args);
  2902. if (rc) {
  2903. CAM_ERR(CAM_ISP, "HW command failed");
  2904. goto free_hw;
  2905. }
  2906. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  2907. /*
  2908. * this context has rdi only resource assign rdi only
  2909. * state machine
  2910. */
  2911. CAM_DBG(CAM_ISP, "RDI only session Context");
  2912. ctx_isp->substate_machine_irq =
  2913. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  2914. ctx_isp->substate_machine =
  2915. cam_isp_ctx_rdi_only_activated_state_machine;
  2916. ctx_isp->rdi_only_context = true;
  2917. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  2918. CAM_DBG(CAM_ISP, "FS2 Session has PIX ,RD and RDI");
  2919. ctx_isp->substate_machine_irq =
  2920. cam_isp_ctx_fs2_state_machine_irq;
  2921. ctx_isp->substate_machine =
  2922. cam_isp_ctx_fs2_state_machine;
  2923. } else {
  2924. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  2925. ctx_isp->substate_machine_irq =
  2926. cam_isp_ctx_activated_state_machine_irq;
  2927. ctx_isp->substate_machine =
  2928. cam_isp_ctx_activated_state_machine;
  2929. }
  2930. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  2931. ctx_isp->hw_acquired = true;
  2932. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  2933. atomic64_set(&ctx_isp->state_monitor_head, -1);
  2934. trace_cam_context_state("ISP", ctx);
  2935. CAM_DBG(CAM_ISP,
  2936. "Acquire success on session_hdl 0x%xs ctx_type %d ctx_id %u",
  2937. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id);
  2938. kfree(acquire_hw_info);
  2939. return rc;
  2940. free_hw:
  2941. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2942. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  2943. ctx_isp->hw_ctx = NULL;
  2944. ctx_isp->hw_acquired = false;
  2945. free_res:
  2946. kfree(acquire_hw_info);
  2947. end:
  2948. return rc;
  2949. }
  2950. static int __cam_isp_ctx_acquire_hw_v2(struct cam_context *ctx,
  2951. void *args)
  2952. {
  2953. int rc = 0, i, j;
  2954. struct cam_acquire_hw_cmd_v2 *cmd =
  2955. (struct cam_acquire_hw_cmd_v2 *)args;
  2956. struct cam_hw_acquire_args param;
  2957. struct cam_hw_release_args release;
  2958. struct cam_isp_context *ctx_isp =
  2959. (struct cam_isp_context *) ctx->ctx_priv;
  2960. struct cam_hw_cmd_args hw_cmd_args;
  2961. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  2962. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  2963. if (!ctx->hw_mgr_intf) {
  2964. CAM_ERR(CAM_ISP, "HW interface is not ready");
  2965. rc = -EFAULT;
  2966. goto end;
  2967. }
  2968. CAM_DBG(CAM_ISP,
  2969. "session_hdl 0x%x, hdl type %d, res %lld",
  2970. cmd->session_handle, cmd->handle_type, cmd->resource_hdl);
  2971. /* for now we only support user pointer */
  2972. if (cmd->handle_type != 1) {
  2973. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  2974. rc = -EINVAL;
  2975. goto end;
  2976. }
  2977. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  2978. CAM_ERR(CAM_ISP, "data_size is not a valid value");
  2979. goto end;
  2980. }
  2981. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  2982. if (!acquire_hw_info) {
  2983. rc = -ENOMEM;
  2984. goto end;
  2985. }
  2986. CAM_DBG(CAM_ISP, "start copy resources from user");
  2987. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  2988. cmd->data_size)) {
  2989. rc = -EFAULT;
  2990. goto free_res;
  2991. }
  2992. memset(&param, 0, sizeof(param));
  2993. param.context_data = ctx;
  2994. param.event_cb = ctx->irq_cb_intf;
  2995. param.num_acq = CAM_API_COMPAT_CONSTANT;
  2996. param.acquire_info_size = cmd->data_size;
  2997. param.acquire_info = (uint64_t) acquire_hw_info;
  2998. /* call HW manager to reserve the resource */
  2999. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  3000. &param);
  3001. if (rc != 0) {
  3002. CAM_ERR(CAM_ISP, "Acquire device failed");
  3003. goto free_res;
  3004. }
  3005. /* Query the context has rdi only resource */
  3006. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  3007. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  3008. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  3009. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  3010. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  3011. &hw_cmd_args);
  3012. if (rc) {
  3013. CAM_ERR(CAM_ISP, "HW command failed");
  3014. goto free_hw;
  3015. }
  3016. if (param.valid_acquired_hw) {
  3017. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  3018. cmd->hw_info.acquired_hw_id[i] =
  3019. param.acquired_hw_id[i];
  3020. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  3021. for (j = 0; j < CAM_MAX_HW_SPLIT; j++)
  3022. cmd->hw_info.acquired_hw_path[i][j] =
  3023. param.acquired_hw_path[i][j];
  3024. }
  3025. cmd->hw_info.valid_acquired_hw =
  3026. param.valid_acquired_hw;
  3027. cmd->hw_info.valid_acquired_hw = param.valid_acquired_hw;
  3028. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  3029. /*
  3030. * this context has rdi only resource assign rdi only
  3031. * state machine
  3032. */
  3033. CAM_DBG(CAM_ISP, "RDI only session Context");
  3034. ctx_isp->substate_machine_irq =
  3035. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  3036. ctx_isp->substate_machine =
  3037. cam_isp_ctx_rdi_only_activated_state_machine;
  3038. ctx_isp->rdi_only_context = true;
  3039. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  3040. CAM_DBG(CAM_ISP, "FS2 Session has PIX ,RD and RDI");
  3041. ctx_isp->substate_machine_irq =
  3042. cam_isp_ctx_fs2_state_machine_irq;
  3043. ctx_isp->substate_machine =
  3044. cam_isp_ctx_fs2_state_machine;
  3045. } else {
  3046. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  3047. ctx_isp->substate_machine_irq =
  3048. cam_isp_ctx_activated_state_machine_irq;
  3049. ctx_isp->substate_machine =
  3050. cam_isp_ctx_activated_state_machine;
  3051. }
  3052. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  3053. ctx_isp->hw_acquired = true;
  3054. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  3055. trace_cam_context_state("ISP", ctx);
  3056. CAM_DBG(CAM_ISP,
  3057. "Acquire success on session_hdl 0x%xs ctx_type %d ctx_id %u",
  3058. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id);
  3059. kfree(acquire_hw_info);
  3060. return rc;
  3061. free_hw:
  3062. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3063. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  3064. ctx_isp->hw_ctx = NULL;
  3065. ctx_isp->hw_acquired = false;
  3066. free_res:
  3067. kfree(acquire_hw_info);
  3068. end:
  3069. return rc;
  3070. }
  3071. static int __cam_isp_ctx_acquire_hw_in_acquired(struct cam_context *ctx,
  3072. void *args)
  3073. {
  3074. int rc = -EINVAL;
  3075. uint32_t api_version;
  3076. if (!ctx || !args) {
  3077. CAM_ERR(CAM_ISP, "Invalid input pointer");
  3078. return rc;
  3079. }
  3080. api_version = *((uint32_t *)args);
  3081. if (api_version == 1)
  3082. rc = __cam_isp_ctx_acquire_hw_v1(ctx, args);
  3083. else if (api_version == 2)
  3084. rc = __cam_isp_ctx_acquire_hw_v2(ctx, args);
  3085. else
  3086. CAM_ERR(CAM_ISP, "Unsupported api version %d", api_version);
  3087. return rc;
  3088. }
  3089. static int __cam_isp_ctx_config_dev_in_acquired(struct cam_context *ctx,
  3090. struct cam_config_dev_cmd *cmd)
  3091. {
  3092. int rc = 0;
  3093. struct cam_isp_context *ctx_isp =
  3094. (struct cam_isp_context *) ctx->ctx_priv;
  3095. if (!ctx_isp->hw_acquired) {
  3096. CAM_ERR(CAM_ISP, "HW is not acquired, reject packet");
  3097. return -EINVAL;
  3098. }
  3099. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  3100. if (!rc && (ctx->link_hdl >= 0)) {
  3101. ctx->state = CAM_CTX_READY;
  3102. trace_cam_context_state("ISP", ctx);
  3103. }
  3104. CAM_DBG(CAM_ISP, "next state %d", ctx->state);
  3105. return rc;
  3106. }
  3107. static int __cam_isp_ctx_config_dev_in_flushed(struct cam_context *ctx,
  3108. struct cam_config_dev_cmd *cmd)
  3109. {
  3110. int rc = 0;
  3111. struct cam_start_stop_dev_cmd start_cmd;
  3112. struct cam_hw_cmd_args hw_cmd_args;
  3113. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  3114. struct cam_isp_context *ctx_isp =
  3115. (struct cam_isp_context *) ctx->ctx_priv;
  3116. if (!ctx_isp->hw_acquired) {
  3117. CAM_ERR(CAM_ISP, "HW is not acquired, reject packet");
  3118. rc = -EINVAL;
  3119. goto end;
  3120. }
  3121. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  3122. if (rc)
  3123. goto end;
  3124. if (!ctx_isp->init_received) {
  3125. CAM_WARN(CAM_ISP,
  3126. "Received update packet in flushed state, skip start");
  3127. goto end;
  3128. }
  3129. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3130. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  3131. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  3132. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  3133. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  3134. &hw_cmd_args);
  3135. if (rc) {
  3136. CAM_ERR(CAM_ISP, "Failed to resume HW rc: %d", rc);
  3137. goto end;
  3138. }
  3139. start_cmd.dev_handle = cmd->dev_handle;
  3140. start_cmd.session_handle = cmd->session_handle;
  3141. rc = __cam_isp_ctx_start_dev_in_ready(ctx, &start_cmd);
  3142. if (rc)
  3143. CAM_ERR(CAM_ISP,
  3144. "Failed to re-start HW after flush rc: %d", rc);
  3145. else
  3146. CAM_INFO(CAM_ISP,
  3147. "Received init after flush. Re-start HW complete.");
  3148. end:
  3149. CAM_DBG(CAM_ISP, "next state %d sub_state:%d", ctx->state,
  3150. ctx_isp->substate_activated);
  3151. return rc;
  3152. }
  3153. static int __cam_isp_ctx_link_in_acquired(struct cam_context *ctx,
  3154. struct cam_req_mgr_core_dev_link_setup *link)
  3155. {
  3156. int rc = 0;
  3157. struct cam_isp_context *ctx_isp =
  3158. (struct cam_isp_context *) ctx->ctx_priv;
  3159. CAM_DBG(CAM_ISP, "Enter.........");
  3160. ctx->link_hdl = link->link_hdl;
  3161. ctx->ctx_crm_intf = link->crm_cb;
  3162. ctx_isp->subscribe_event = link->subscribe_event;
  3163. /* change state only if we had the init config */
  3164. if (ctx_isp->init_received) {
  3165. ctx->state = CAM_CTX_READY;
  3166. trace_cam_context_state("ISP", ctx);
  3167. }
  3168. CAM_DBG(CAM_ISP, "next state %d", ctx->state);
  3169. return rc;
  3170. }
  3171. static int __cam_isp_ctx_unlink_in_acquired(struct cam_context *ctx,
  3172. struct cam_req_mgr_core_dev_link_setup *unlink)
  3173. {
  3174. int rc = 0;
  3175. ctx->link_hdl = -1;
  3176. ctx->ctx_crm_intf = NULL;
  3177. return rc;
  3178. }
  3179. static int __cam_isp_ctx_get_dev_info_in_acquired(struct cam_context *ctx,
  3180. struct cam_req_mgr_device_info *dev_info)
  3181. {
  3182. int rc = 0;
  3183. dev_info->dev_hdl = ctx->dev_hdl;
  3184. strlcpy(dev_info->name, CAM_ISP_DEV_NAME, sizeof(dev_info->name));
  3185. dev_info->dev_id = CAM_REQ_MGR_DEVICE_IFE;
  3186. dev_info->p_delay = 1;
  3187. dev_info->trigger = CAM_TRIGGER_POINT_SOF;
  3188. return rc;
  3189. }
  3190. static int __cam_isp_ctx_start_dev_in_ready(struct cam_context *ctx,
  3191. struct cam_start_stop_dev_cmd *cmd)
  3192. {
  3193. int rc = 0;
  3194. struct cam_isp_start_args start_isp;
  3195. struct cam_ctx_request *req;
  3196. struct cam_isp_ctx_req *req_isp;
  3197. struct cam_isp_context *ctx_isp =
  3198. (struct cam_isp_context *) ctx->ctx_priv;
  3199. if (cmd->session_handle != ctx->session_hdl ||
  3200. cmd->dev_handle != ctx->dev_hdl) {
  3201. rc = -EPERM;
  3202. goto end;
  3203. }
  3204. if (list_empty(&ctx->pending_req_list)) {
  3205. /* should never happen */
  3206. CAM_ERR(CAM_ISP, "Start device with empty configuration");
  3207. rc = -EFAULT;
  3208. goto end;
  3209. } else {
  3210. req = list_first_entry(&ctx->pending_req_list,
  3211. struct cam_ctx_request, list);
  3212. }
  3213. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3214. if (!ctx_isp->hw_ctx) {
  3215. CAM_ERR(CAM_ISP, "Wrong hw context pointer.");
  3216. rc = -EFAULT;
  3217. goto end;
  3218. }
  3219. start_isp.hw_config.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3220. start_isp.hw_config.request_id = req->request_id;
  3221. start_isp.hw_config.hw_update_entries = req_isp->cfg;
  3222. start_isp.hw_config.num_hw_update_entries = req_isp->num_cfg;
  3223. start_isp.hw_config.priv = &req_isp->hw_update_data;
  3224. start_isp.hw_config.init_packet = 1;
  3225. start_isp.hw_config.reapply = 0;
  3226. if (ctx->state == CAM_CTX_FLUSHED)
  3227. start_isp.start_only = true;
  3228. else
  3229. start_isp.start_only = false;
  3230. atomic_set(&ctx_isp->process_bubble, 0);
  3231. ctx_isp->frame_id = 0;
  3232. ctx_isp->active_req_cnt = 0;
  3233. ctx_isp->reported_req_id = 0;
  3234. ctx_isp->bubble_frame_cnt = 0;
  3235. ctx_isp->substate_activated = ctx_isp->rdi_only_context ?
  3236. CAM_ISP_CTX_ACTIVATED_APPLIED :
  3237. (req_isp->num_fence_map_out) ? CAM_ISP_CTX_ACTIVATED_EPOCH :
  3238. CAM_ISP_CTX_ACTIVATED_SOF;
  3239. atomic64_set(&ctx_isp->state_monitor_head, -1);
  3240. /*
  3241. * In case of CSID TPG we might receive SOF and RUP IRQs
  3242. * before hw_mgr_intf->hw_start has returned. So move
  3243. * req out of pending list before hw_start and add it
  3244. * back to pending list if hw_start fails.
  3245. */
  3246. list_del_init(&req->list);
  3247. if (ctx_isp->rdi_only_context || !req_isp->num_fence_map_out) {
  3248. list_add_tail(&req->list, &ctx->wait_req_list);
  3249. } else {
  3250. list_add_tail(&req->list, &ctx->active_req_list);
  3251. ctx_isp->active_req_cnt++;
  3252. }
  3253. /*
  3254. * Only place to change state before calling the hw due to
  3255. * hardware tasklet has higher priority that can cause the
  3256. * irq handling comes early
  3257. */
  3258. ctx->state = CAM_CTX_ACTIVATED;
  3259. trace_cam_context_state("ISP", ctx);
  3260. rc = ctx->hw_mgr_intf->hw_start(ctx->hw_mgr_intf->hw_mgr_priv,
  3261. &start_isp);
  3262. if (rc) {
  3263. /* HW failure. user need to clean up the resource */
  3264. CAM_ERR(CAM_ISP, "Start HW failed");
  3265. ctx->state = CAM_CTX_READY;
  3266. trace_cam_context_state("ISP", ctx);
  3267. list_del_init(&req->list);
  3268. list_add(&req->list, &ctx->pending_req_list);
  3269. goto end;
  3270. }
  3271. CAM_DBG(CAM_ISP, "start device success ctx %u", ctx->ctx_id);
  3272. end:
  3273. return rc;
  3274. }
  3275. static int __cam_isp_ctx_unlink_in_ready(struct cam_context *ctx,
  3276. struct cam_req_mgr_core_dev_link_setup *unlink)
  3277. {
  3278. int rc = 0;
  3279. ctx->link_hdl = -1;
  3280. ctx->ctx_crm_intf = NULL;
  3281. ctx->state = CAM_CTX_ACQUIRED;
  3282. trace_cam_context_state("ISP", ctx);
  3283. return rc;
  3284. }
  3285. static int __cam_isp_ctx_stop_dev_in_activated_unlock(
  3286. struct cam_context *ctx, struct cam_start_stop_dev_cmd *stop_cmd)
  3287. {
  3288. int rc = 0;
  3289. uint32_t i;
  3290. struct cam_hw_stop_args stop;
  3291. struct cam_ctx_request *req;
  3292. struct cam_isp_ctx_req *req_isp;
  3293. struct cam_isp_context *ctx_isp =
  3294. (struct cam_isp_context *) ctx->ctx_priv;
  3295. struct cam_isp_stop_args stop_isp;
  3296. /* stop hw first */
  3297. if (ctx_isp->hw_ctx) {
  3298. stop.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3299. if (stop_cmd)
  3300. stop_isp.hw_stop_cmd =
  3301. CAM_ISP_HW_STOP_AT_FRAME_BOUNDARY;
  3302. else
  3303. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  3304. stop_isp.stop_only = false;
  3305. stop.args = (void *) &stop_isp;
  3306. ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  3307. &stop);
  3308. }
  3309. /* Mask off all the incoming hardware events */
  3310. spin_lock_bh(&ctx->lock);
  3311. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  3312. spin_unlock_bh(&ctx->lock);
  3313. CAM_DBG(CAM_ISP, "next substate %d", ctx_isp->substate_activated);
  3314. while (!list_empty(&ctx->pending_req_list)) {
  3315. req = list_first_entry(&ctx->pending_req_list,
  3316. struct cam_ctx_request, list);
  3317. list_del_init(&req->list);
  3318. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3319. CAM_DBG(CAM_ISP, "signal fence in pending list. fence num %d",
  3320. req_isp->num_fence_map_out);
  3321. for (i = 0; i < req_isp->num_fence_map_out; i++)
  3322. if (req_isp->fence_map_out[i].sync_id != -1) {
  3323. cam_sync_signal(
  3324. req_isp->fence_map_out[i].sync_id,
  3325. CAM_SYNC_STATE_SIGNALED_ERROR);
  3326. }
  3327. list_add_tail(&req->list, &ctx->free_req_list);
  3328. }
  3329. while (!list_empty(&ctx->wait_req_list)) {
  3330. req = list_first_entry(&ctx->wait_req_list,
  3331. struct cam_ctx_request, list);
  3332. list_del_init(&req->list);
  3333. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3334. CAM_DBG(CAM_ISP, "signal fence in wait list. fence num %d",
  3335. req_isp->num_fence_map_out);
  3336. for (i = 0; i < req_isp->num_fence_map_out; i++)
  3337. if (req_isp->fence_map_out[i].sync_id != -1) {
  3338. cam_sync_signal(
  3339. req_isp->fence_map_out[i].sync_id,
  3340. CAM_SYNC_STATE_SIGNALED_ERROR);
  3341. }
  3342. list_add_tail(&req->list, &ctx->free_req_list);
  3343. }
  3344. while (!list_empty(&ctx->active_req_list)) {
  3345. req = list_first_entry(&ctx->active_req_list,
  3346. struct cam_ctx_request, list);
  3347. list_del_init(&req->list);
  3348. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3349. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d",
  3350. req_isp->num_fence_map_out);
  3351. for (i = 0; i < req_isp->num_fence_map_out; i++)
  3352. if (req_isp->fence_map_out[i].sync_id != -1) {
  3353. cam_sync_signal(
  3354. req_isp->fence_map_out[i].sync_id,
  3355. CAM_SYNC_STATE_SIGNALED_ERROR);
  3356. }
  3357. list_add_tail(&req->list, &ctx->free_req_list);
  3358. }
  3359. ctx_isp->frame_id = 0;
  3360. ctx_isp->active_req_cnt = 0;
  3361. ctx_isp->reported_req_id = 0;
  3362. ctx_isp->bubble_frame_cnt = 0;
  3363. atomic_set(&ctx_isp->process_bubble, 0);
  3364. atomic64_set(&ctx_isp->state_monitor_head, -1);
  3365. CAM_DBG(CAM_ISP, "Stop device success next state %d on ctx %u",
  3366. ctx->state, ctx->ctx_id);
  3367. if (!stop_cmd) {
  3368. rc = __cam_isp_ctx_unlink_in_ready(ctx, NULL);
  3369. if (rc)
  3370. CAM_ERR(CAM_ISP, "Unlink failed rc=%d", rc);
  3371. }
  3372. return rc;
  3373. }
  3374. static int __cam_isp_ctx_stop_dev_in_activated(struct cam_context *ctx,
  3375. struct cam_start_stop_dev_cmd *cmd)
  3376. {
  3377. int rc = 0;
  3378. struct cam_isp_context *ctx_isp =
  3379. (struct cam_isp_context *)ctx->ctx_priv;
  3380. __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, cmd);
  3381. ctx_isp->init_received = false;
  3382. ctx->state = CAM_CTX_ACQUIRED;
  3383. trace_cam_context_state("ISP", ctx);
  3384. return rc;
  3385. }
  3386. static int __cam_isp_ctx_release_dev_in_activated(struct cam_context *ctx,
  3387. struct cam_release_dev_cmd *cmd)
  3388. {
  3389. int rc = 0;
  3390. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  3391. if (rc)
  3392. CAM_ERR(CAM_ISP, "Stop device failed rc=%d", rc);
  3393. rc = __cam_isp_ctx_release_dev_in_top_state(ctx, cmd);
  3394. if (rc)
  3395. CAM_ERR(CAM_ISP, "Release device failed rc=%d", rc);
  3396. return rc;
  3397. }
  3398. static int __cam_isp_ctx_release_hw_in_activated(struct cam_context *ctx,
  3399. void *cmd)
  3400. {
  3401. int rc = 0;
  3402. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  3403. if (rc)
  3404. CAM_ERR(CAM_ISP, "Stop device failed rc=%d", rc);
  3405. rc = __cam_isp_ctx_release_hw_in_top_state(ctx, cmd);
  3406. if (rc)
  3407. CAM_ERR(CAM_ISP, "Release hw failed rc=%d", rc);
  3408. return rc;
  3409. }
  3410. static int __cam_isp_ctx_link_pause(struct cam_context *ctx)
  3411. {
  3412. int rc = 0;
  3413. struct cam_hw_cmd_args hw_cmd_args;
  3414. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  3415. struct cam_isp_context *ctx_isp =
  3416. (struct cam_isp_context *) ctx->ctx_priv;
  3417. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3418. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  3419. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_PAUSE_HW;
  3420. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  3421. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  3422. &hw_cmd_args);
  3423. return rc;
  3424. }
  3425. static int __cam_isp_ctx_link_resume(struct cam_context *ctx)
  3426. {
  3427. int rc = 0;
  3428. struct cam_hw_cmd_args hw_cmd_args;
  3429. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  3430. struct cam_isp_context *ctx_isp =
  3431. (struct cam_isp_context *) ctx->ctx_priv;
  3432. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3433. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  3434. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  3435. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  3436. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  3437. &hw_cmd_args);
  3438. return rc;
  3439. }
  3440. static int __cam_isp_ctx_handle_sof_freeze_evt(
  3441. struct cam_context *ctx)
  3442. {
  3443. int rc = 0;
  3444. struct cam_hw_cmd_args hw_cmd_args;
  3445. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  3446. struct cam_isp_context *ctx_isp =
  3447. (struct cam_isp_context *) ctx->ctx_priv;
  3448. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3449. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  3450. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_SOF_DEBUG;
  3451. isp_hw_cmd_args.u.sof_irq_enable = 1;
  3452. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  3453. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  3454. &hw_cmd_args);
  3455. return rc;
  3456. }
  3457. static int __cam_isp_ctx_process_evt(struct cam_context *ctx,
  3458. struct cam_req_mgr_link_evt_data *link_evt_data)
  3459. {
  3460. int rc = 0;
  3461. switch (link_evt_data->evt_type) {
  3462. case CAM_REQ_MGR_LINK_EVT_ERR:
  3463. /* No need to handle this message now */
  3464. break;
  3465. case CAM_REQ_MGR_LINK_EVT_PAUSE:
  3466. __cam_isp_ctx_link_pause(ctx);
  3467. break;
  3468. case CAM_REQ_MGR_LINK_EVT_RESUME:
  3469. __cam_isp_ctx_link_resume(ctx);
  3470. break;
  3471. case CAM_REQ_MGR_LINK_EVT_SOF_FREEZE:
  3472. __cam_isp_ctx_handle_sof_freeze_evt(ctx);
  3473. break;
  3474. default:
  3475. CAM_WARN(CAM_ISP, "Unknown event from CRM");
  3476. break;
  3477. }
  3478. return rc;
  3479. }
  3480. static int __cam_isp_ctx_unlink_in_activated(struct cam_context *ctx,
  3481. struct cam_req_mgr_core_dev_link_setup *unlink)
  3482. {
  3483. int rc = 0;
  3484. CAM_WARN(CAM_ISP,
  3485. "Received unlink in activated state. It's unexpected");
  3486. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  3487. if (rc)
  3488. CAM_WARN(CAM_ISP, "Stop device failed rc=%d", rc);
  3489. rc = __cam_isp_ctx_unlink_in_ready(ctx, unlink);
  3490. if (rc)
  3491. CAM_ERR(CAM_ISP, "Unlink failed rc=%d", rc);
  3492. return rc;
  3493. }
  3494. static int __cam_isp_ctx_apply_req(struct cam_context *ctx,
  3495. struct cam_req_mgr_apply_request *apply)
  3496. {
  3497. int rc = 0;
  3498. struct cam_ctx_ops *ctx_ops = NULL;
  3499. struct cam_isp_context *ctx_isp =
  3500. (struct cam_isp_context *) ctx->ctx_priv;
  3501. trace_cam_apply_req("ISP", apply->request_id);
  3502. CAM_DBG(CAM_ISP, "Enter: apply req in Substate %d request _id:%lld",
  3503. ctx_isp->substate_activated, apply->request_id);
  3504. ctx_ops = &ctx_isp->substate_machine[ctx_isp->substate_activated];
  3505. if (ctx_ops->crm_ops.apply_req) {
  3506. rc = ctx_ops->crm_ops.apply_req(ctx, apply);
  3507. } else {
  3508. CAM_WARN_RATE_LIMIT(CAM_ISP,
  3509. "No handle function in activated substate %d",
  3510. ctx_isp->substate_activated);
  3511. rc = -EFAULT;
  3512. }
  3513. if (rc)
  3514. CAM_WARN_RATE_LIMIT(CAM_ISP,
  3515. "Apply failed in active substate %d rc %d",
  3516. ctx_isp->substate_activated, rc);
  3517. return rc;
  3518. }
  3519. static int __cam_isp_ctx_handle_irq_in_activated(void *context,
  3520. uint32_t evt_id, void *evt_data)
  3521. {
  3522. int rc = 0;
  3523. struct cam_isp_ctx_irq_ops *irq_ops = NULL;
  3524. struct cam_context *ctx = (struct cam_context *)context;
  3525. struct cam_isp_context *ctx_isp =
  3526. (struct cam_isp_context *)ctx->ctx_priv;
  3527. spin_lock(&ctx->lock);
  3528. trace_cam_isp_activated_irq(ctx, ctx_isp->substate_activated, evt_id,
  3529. __cam_isp_ctx_get_event_ts(evt_id, evt_data));
  3530. CAM_DBG(CAM_ISP, "Enter: State %d, Substate %d, evt id %d",
  3531. ctx->state, ctx_isp->substate_activated, evt_id);
  3532. irq_ops = &ctx_isp->substate_machine_irq[ctx_isp->substate_activated];
  3533. if (irq_ops->irq_ops[evt_id]) {
  3534. rc = irq_ops->irq_ops[evt_id](ctx_isp, evt_data);
  3535. } else {
  3536. CAM_DBG(CAM_ISP, "No handle function for substate %d",
  3537. ctx_isp->substate_activated);
  3538. if (isp_ctx_debug.enable_state_monitor_dump)
  3539. __cam_isp_ctx_dump_state_monitor_array(ctx_isp);
  3540. }
  3541. CAM_DBG(CAM_ISP, "Exit: State %d Substate %d",
  3542. ctx->state, ctx_isp->substate_activated);
  3543. spin_unlock(&ctx->lock);
  3544. return rc;
  3545. }
  3546. /* top state machine */
  3547. static struct cam_ctx_ops
  3548. cam_isp_ctx_top_state_machine[CAM_CTX_STATE_MAX] = {
  3549. /* Uninit */
  3550. {
  3551. .ioctl_ops = {},
  3552. .crm_ops = {},
  3553. .irq_ops = NULL,
  3554. },
  3555. /* Available */
  3556. {
  3557. .ioctl_ops = {
  3558. .acquire_dev = __cam_isp_ctx_acquire_dev_in_available,
  3559. },
  3560. .crm_ops = {},
  3561. .irq_ops = NULL,
  3562. },
  3563. /* Acquired */
  3564. {
  3565. .ioctl_ops = {
  3566. .acquire_hw = __cam_isp_ctx_acquire_hw_in_acquired,
  3567. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  3568. .config_dev = __cam_isp_ctx_config_dev_in_acquired,
  3569. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  3570. },
  3571. .crm_ops = {
  3572. .link = __cam_isp_ctx_link_in_acquired,
  3573. .unlink = __cam_isp_ctx_unlink_in_acquired,
  3574. .get_dev_info = __cam_isp_ctx_get_dev_info_in_acquired,
  3575. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  3576. },
  3577. .irq_ops = NULL,
  3578. .pagefault_ops = cam_isp_context_dump_active_request,
  3579. .dumpinfo_ops = cam_isp_context_info_dump,
  3580. },
  3581. /* Ready */
  3582. {
  3583. .ioctl_ops = {
  3584. .start_dev = __cam_isp_ctx_start_dev_in_ready,
  3585. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  3586. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  3587. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  3588. },
  3589. .crm_ops = {
  3590. .unlink = __cam_isp_ctx_unlink_in_ready,
  3591. .flush_req = __cam_isp_ctx_flush_req_in_ready,
  3592. },
  3593. .irq_ops = NULL,
  3594. .pagefault_ops = cam_isp_context_dump_active_request,
  3595. .dumpinfo_ops = cam_isp_context_info_dump,
  3596. },
  3597. /* Flushed */
  3598. {
  3599. .ioctl_ops = {
  3600. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  3601. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  3602. .config_dev = __cam_isp_ctx_config_dev_in_flushed,
  3603. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  3604. },
  3605. .crm_ops = {
  3606. .unlink = __cam_isp_ctx_unlink_in_ready,
  3607. .process_evt = __cam_isp_ctx_process_evt,
  3608. },
  3609. .irq_ops = NULL,
  3610. .pagefault_ops = cam_isp_context_dump_active_request,
  3611. .dumpinfo_ops = cam_isp_context_info_dump,
  3612. },
  3613. /* Activated */
  3614. {
  3615. .ioctl_ops = {
  3616. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  3617. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  3618. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  3619. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  3620. },
  3621. .crm_ops = {
  3622. .unlink = __cam_isp_ctx_unlink_in_activated,
  3623. .apply_req = __cam_isp_ctx_apply_req,
  3624. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  3625. .process_evt = __cam_isp_ctx_process_evt,
  3626. },
  3627. .irq_ops = __cam_isp_ctx_handle_irq_in_activated,
  3628. .pagefault_ops = cam_isp_context_dump_active_request,
  3629. .dumpinfo_ops = cam_isp_context_info_dump,
  3630. },
  3631. };
  3632. static int cam_isp_context_dump_active_request(void *data, unsigned long iova,
  3633. uint32_t buf_info)
  3634. {
  3635. struct cam_context *ctx = (struct cam_context *)data;
  3636. struct cam_ctx_request *req = NULL;
  3637. struct cam_ctx_request *req_temp = NULL;
  3638. struct cam_isp_ctx_req *req_isp = NULL;
  3639. struct cam_isp_prepare_hw_update_data *hw_update_data = NULL;
  3640. struct cam_hw_mgr_dump_pf_data *pf_dbg_entry = NULL;
  3641. bool mem_found = false;
  3642. int rc = 0;
  3643. struct cam_isp_context *isp_ctx =
  3644. (struct cam_isp_context *)ctx->ctx_priv;
  3645. if (!isp_ctx) {
  3646. CAM_ERR(CAM_ISP, "Invalid isp ctx");
  3647. return -EINVAL;
  3648. }
  3649. CAM_INFO(CAM_ISP, "iommu fault handler for isp ctx %d state %d",
  3650. ctx->ctx_id, ctx->state);
  3651. list_for_each_entry_safe(req, req_temp,
  3652. &ctx->active_req_list, list) {
  3653. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3654. hw_update_data = &req_isp->hw_update_data;
  3655. pf_dbg_entry = &(req->pf_data);
  3656. CAM_INFO(CAM_ISP, "req_id : %lld ", req->request_id);
  3657. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry->packet,
  3658. iova, buf_info, &mem_found);
  3659. if (rc)
  3660. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  3661. if (mem_found)
  3662. CAM_ERR(CAM_ISP, "Found page fault in req %lld %d",
  3663. req->request_id, rc);
  3664. }
  3665. CAM_INFO(CAM_ISP, "Iterating over wait_list of isp ctx %d state %d",
  3666. ctx->ctx_id, ctx->state);
  3667. list_for_each_entry_safe(req, req_temp,
  3668. &ctx->wait_req_list, list) {
  3669. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3670. hw_update_data = &req_isp->hw_update_data;
  3671. pf_dbg_entry = &(req->pf_data);
  3672. CAM_INFO(CAM_ISP, "req_id : %lld ", req->request_id);
  3673. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry->packet,
  3674. iova, buf_info, &mem_found);
  3675. if (rc)
  3676. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  3677. if (mem_found)
  3678. CAM_ERR(CAM_ISP, "Found page fault in req %lld %d",
  3679. req->request_id, rc);
  3680. }
  3681. /*
  3682. * In certain scenarios we observe both overflow and SMMU pagefault
  3683. * for a particular request. If overflow is handled before page fault
  3684. * we need to traverse through pending request list because if
  3685. * bubble recovery is enabled on any request we move that request
  3686. * and all the subsequent requests to the pending list while handling
  3687. * overflow error.
  3688. */
  3689. CAM_INFO(CAM_ISP,
  3690. "Iterating over pending req list of isp ctx %d state %d",
  3691. ctx->ctx_id, ctx->state);
  3692. list_for_each_entry_safe(req, req_temp,
  3693. &ctx->pending_req_list, list) {
  3694. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3695. hw_update_data = &req_isp->hw_update_data;
  3696. pf_dbg_entry = &(req->pf_data);
  3697. CAM_INFO(CAM_ISP, "req_id : %lld ", req->request_id);
  3698. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry->packet,
  3699. iova, buf_info, &mem_found);
  3700. if (rc)
  3701. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  3702. if (mem_found)
  3703. CAM_ERR(CAM_ISP, "Found page fault in req %lld %d",
  3704. req->request_id, rc);
  3705. }
  3706. return rc;
  3707. }
  3708. static int cam_isp_context_debug_register(void)
  3709. {
  3710. isp_ctx_debug.dentry = debugfs_create_dir("camera_isp_ctx",
  3711. NULL);
  3712. if (!isp_ctx_debug.dentry) {
  3713. CAM_ERR(CAM_ISP, "failed to create dentry");
  3714. return -ENOMEM;
  3715. }
  3716. if (!debugfs_create_u32("enable_state_monitor_dump",
  3717. 0644,
  3718. isp_ctx_debug.dentry,
  3719. &isp_ctx_debug.enable_state_monitor_dump)) {
  3720. CAM_ERR(CAM_ISP, "failed to create enable_state_monitor_dump");
  3721. goto err;
  3722. }
  3723. return 0;
  3724. err:
  3725. debugfs_remove_recursive(isp_ctx_debug.dentry);
  3726. return -ENOMEM;
  3727. }
  3728. int cam_isp_context_init(struct cam_isp_context *ctx,
  3729. struct cam_context *ctx_base,
  3730. struct cam_req_mgr_kmd_ops *crm_node_intf,
  3731. struct cam_hw_mgr_intf *hw_intf,
  3732. uint32_t ctx_id)
  3733. {
  3734. int rc = -1;
  3735. int i;
  3736. if (!ctx || !ctx_base) {
  3737. CAM_ERR(CAM_ISP, "Invalid Context");
  3738. goto err;
  3739. }
  3740. /* ISP context setup */
  3741. memset(ctx, 0, sizeof(*ctx));
  3742. ctx->base = ctx_base;
  3743. ctx->frame_id = 0;
  3744. ctx->active_req_cnt = 0;
  3745. ctx->reported_req_id = 0;
  3746. ctx->bubble_frame_cnt = 0;
  3747. ctx->hw_ctx = NULL;
  3748. ctx->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3749. ctx->substate_machine = cam_isp_ctx_activated_state_machine;
  3750. ctx->substate_machine_irq = cam_isp_ctx_activated_state_machine_irq;
  3751. ctx->init_timestamp = jiffies_to_msecs(jiffies);
  3752. for (i = 0; i < CAM_CTX_REQ_MAX; i++) {
  3753. ctx->req_base[i].req_priv = &ctx->req_isp[i];
  3754. ctx->req_isp[i].base = &ctx->req_base[i];
  3755. }
  3756. /* camera context setup */
  3757. rc = cam_context_init(ctx_base, isp_dev_name, CAM_ISP, ctx_id,
  3758. crm_node_intf, hw_intf, ctx->req_base, CAM_CTX_REQ_MAX);
  3759. if (rc) {
  3760. CAM_ERR(CAM_ISP, "Camera Context Base init failed");
  3761. goto err;
  3762. }
  3763. /* link camera context with isp context */
  3764. ctx_base->state_machine = cam_isp_ctx_top_state_machine;
  3765. ctx_base->ctx_priv = ctx;
  3766. /* initializing current state for error logging */
  3767. for (i = 0; i < CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES; i++) {
  3768. ctx->cam_isp_ctx_state_monitor[i].curr_state =
  3769. CAM_ISP_CTX_ACTIVATED_MAX;
  3770. }
  3771. atomic64_set(&ctx->state_monitor_head, -1);
  3772. cam_isp_context_debug_register();
  3773. err:
  3774. return rc;
  3775. }
  3776. int cam_isp_context_deinit(struct cam_isp_context *ctx)
  3777. {
  3778. int rc = 0;
  3779. if (ctx->base)
  3780. cam_context_deinit(ctx->base);
  3781. if (ctx->substate_activated != CAM_ISP_CTX_ACTIVATED_SOF)
  3782. CAM_ERR(CAM_ISP, "ISP context substate is invalid");
  3783. memset(ctx, 0, sizeof(*ctx));
  3784. return rc;
  3785. }