cam_isp_context.c 113 KB

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