cam_isp_context.c 147 KB

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