cam_isp_context.c 146 KB

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