cam_isp_context.c 155 KB

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