cam_isp_context.c 118 KB

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