cam_isp_context.c 118 KB

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