cam_isp_context.c 110 KB

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