cam_isp_context.c 148 KB

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