nouveau_bios.c 174 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393
  1. /*
  2. * Copyright 2005-2006 Erik Waling
  3. * Copyright 2006 Stephane Marchesin
  4. * Copyright 2007-2009 Stuart Bennett
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
  21. * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #include "drmP.h"
  25. #define NV_DEBUG_NOTRACE
  26. #include "nouveau_drv.h"
  27. #include "nouveau_hw.h"
  28. #include "nouveau_encoder.h"
  29. /* these defines are made up */
  30. #define NV_CIO_CRE_44_HEADA 0x0
  31. #define NV_CIO_CRE_44_HEADB 0x3
  32. #define FEATURE_MOBILE 0x10 /* also FEATURE_QUADRO for BMP */
  33. #define LEGACY_I2C_CRT 0x80
  34. #define LEGACY_I2C_PANEL 0x81
  35. #define LEGACY_I2C_TV 0x82
  36. #define EDID1_LEN 128
  37. #define BIOSLOG(sip, fmt, arg...) NV_DEBUG(sip->dev, fmt, ##arg)
  38. #define LOG_OLD_VALUE(x)
  39. #define ROM16(x) le16_to_cpu(*(uint16_t *)&(x))
  40. #define ROM32(x) le32_to_cpu(*(uint32_t *)&(x))
  41. struct init_exec {
  42. bool execute;
  43. bool repeat;
  44. };
  45. static bool nv_cksum(const uint8_t *data, unsigned int length)
  46. {
  47. /*
  48. * There's a few checksums in the BIOS, so here's a generic checking
  49. * function.
  50. */
  51. int i;
  52. uint8_t sum = 0;
  53. for (i = 0; i < length; i++)
  54. sum += data[i];
  55. if (sum)
  56. return true;
  57. return false;
  58. }
  59. static int
  60. score_vbios(struct drm_device *dev, const uint8_t *data, const bool writeable)
  61. {
  62. if (!(data[0] == 0x55 && data[1] == 0xAA)) {
  63. NV_TRACEWARN(dev, "... BIOS signature not found\n");
  64. return 0;
  65. }
  66. if (nv_cksum(data, data[2] * 512)) {
  67. NV_TRACEWARN(dev, "... BIOS checksum invalid\n");
  68. /* if a ro image is somewhat bad, it's probably all rubbish */
  69. return writeable ? 2 : 1;
  70. } else
  71. NV_TRACE(dev, "... appears to be valid\n");
  72. return 3;
  73. }
  74. static void load_vbios_prom(struct drm_device *dev, uint8_t *data)
  75. {
  76. struct drm_nouveau_private *dev_priv = dev->dev_private;
  77. uint32_t pci_nv_20, save_pci_nv_20;
  78. int pcir_ptr;
  79. int i;
  80. if (dev_priv->card_type >= NV_50)
  81. pci_nv_20 = 0x88050;
  82. else
  83. pci_nv_20 = NV_PBUS_PCI_NV_20;
  84. /* enable ROM access */
  85. save_pci_nv_20 = nvReadMC(dev, pci_nv_20);
  86. nvWriteMC(dev, pci_nv_20,
  87. save_pci_nv_20 & ~NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED);
  88. /* bail if no rom signature */
  89. if (nv_rd08(dev, NV_PROM_OFFSET) != 0x55 ||
  90. nv_rd08(dev, NV_PROM_OFFSET + 1) != 0xaa)
  91. goto out;
  92. /* additional check (see note below) - read PCI record header */
  93. pcir_ptr = nv_rd08(dev, NV_PROM_OFFSET + 0x18) |
  94. nv_rd08(dev, NV_PROM_OFFSET + 0x19) << 8;
  95. if (nv_rd08(dev, NV_PROM_OFFSET + pcir_ptr) != 'P' ||
  96. nv_rd08(dev, NV_PROM_OFFSET + pcir_ptr + 1) != 'C' ||
  97. nv_rd08(dev, NV_PROM_OFFSET + pcir_ptr + 2) != 'I' ||
  98. nv_rd08(dev, NV_PROM_OFFSET + pcir_ptr + 3) != 'R')
  99. goto out;
  100. /* on some 6600GT/6800LE prom reads are messed up. nvclock alleges a
  101. * a good read may be obtained by waiting or re-reading (cargocult: 5x)
  102. * each byte. we'll hope pramin has something usable instead
  103. */
  104. for (i = 0; i < NV_PROM_SIZE; i++)
  105. data[i] = nv_rd08(dev, NV_PROM_OFFSET + i);
  106. out:
  107. /* disable ROM access */
  108. nvWriteMC(dev, pci_nv_20,
  109. save_pci_nv_20 | NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED);
  110. }
  111. static void load_vbios_pramin(struct drm_device *dev, uint8_t *data)
  112. {
  113. struct drm_nouveau_private *dev_priv = dev->dev_private;
  114. uint32_t old_bar0_pramin = 0;
  115. int i;
  116. if (dev_priv->card_type >= NV_50) {
  117. uint32_t vbios_vram = (nv_rd32(dev, 0x619f04) & ~0xff) << 8;
  118. if (!vbios_vram)
  119. vbios_vram = (nv_rd32(dev, 0x1700) << 16) + 0xf0000;
  120. old_bar0_pramin = nv_rd32(dev, 0x1700);
  121. nv_wr32(dev, 0x1700, vbios_vram >> 16);
  122. }
  123. /* bail if no rom signature */
  124. if (nv_rd08(dev, NV_PRAMIN_OFFSET) != 0x55 ||
  125. nv_rd08(dev, NV_PRAMIN_OFFSET + 1) != 0xaa)
  126. goto out;
  127. for (i = 0; i < NV_PROM_SIZE; i++)
  128. data[i] = nv_rd08(dev, NV_PRAMIN_OFFSET + i);
  129. out:
  130. if (dev_priv->card_type >= NV_50)
  131. nv_wr32(dev, 0x1700, old_bar0_pramin);
  132. }
  133. static void load_vbios_pci(struct drm_device *dev, uint8_t *data)
  134. {
  135. void __iomem *rom = NULL;
  136. size_t rom_len;
  137. int ret;
  138. ret = pci_enable_rom(dev->pdev);
  139. if (ret)
  140. return;
  141. rom = pci_map_rom(dev->pdev, &rom_len);
  142. if (!rom)
  143. goto out;
  144. memcpy_fromio(data, rom, rom_len);
  145. pci_unmap_rom(dev->pdev, rom);
  146. out:
  147. pci_disable_rom(dev->pdev);
  148. }
  149. static void load_vbios_acpi(struct drm_device *dev, uint8_t *data)
  150. {
  151. int i;
  152. int ret;
  153. int size = 64 * 1024;
  154. if (!nouveau_acpi_rom_supported(dev->pdev))
  155. return;
  156. for (i = 0; i < (size / ROM_BIOS_PAGE); i++) {
  157. ret = nouveau_acpi_get_bios_chunk(data,
  158. (i * ROM_BIOS_PAGE),
  159. ROM_BIOS_PAGE);
  160. if (ret <= 0)
  161. break;
  162. }
  163. return;
  164. }
  165. struct methods {
  166. const char desc[8];
  167. void (*loadbios)(struct drm_device *, uint8_t *);
  168. const bool rw;
  169. };
  170. static struct methods shadow_methods[] = {
  171. { "PRAMIN", load_vbios_pramin, true },
  172. { "PROM", load_vbios_prom, false },
  173. { "PCIROM", load_vbios_pci, true },
  174. { "ACPI", load_vbios_acpi, true },
  175. };
  176. static bool NVShadowVBIOS(struct drm_device *dev, uint8_t *data)
  177. {
  178. const int nr_methods = ARRAY_SIZE(shadow_methods);
  179. struct methods *methods = shadow_methods;
  180. int testscore = 3;
  181. int scores[nr_methods], i;
  182. if (nouveau_vbios) {
  183. for (i = 0; i < nr_methods; i++)
  184. if (!strcasecmp(nouveau_vbios, methods[i].desc))
  185. break;
  186. if (i < nr_methods) {
  187. NV_INFO(dev, "Attempting to use BIOS image from %s\n",
  188. methods[i].desc);
  189. methods[i].loadbios(dev, data);
  190. if (score_vbios(dev, data, methods[i].rw))
  191. return true;
  192. }
  193. NV_ERROR(dev, "VBIOS source \'%s\' invalid\n", nouveau_vbios);
  194. }
  195. for (i = 0; i < nr_methods; i++) {
  196. NV_TRACE(dev, "Attempting to load BIOS image from %s\n",
  197. methods[i].desc);
  198. data[0] = data[1] = 0; /* avoid reuse of previous image */
  199. methods[i].loadbios(dev, data);
  200. scores[i] = score_vbios(dev, data, methods[i].rw);
  201. if (scores[i] == testscore)
  202. return true;
  203. }
  204. while (--testscore > 0) {
  205. for (i = 0; i < nr_methods; i++) {
  206. if (scores[i] == testscore) {
  207. NV_TRACE(dev, "Using BIOS image from %s\n",
  208. methods[i].desc);
  209. methods[i].loadbios(dev, data);
  210. return true;
  211. }
  212. }
  213. }
  214. NV_ERROR(dev, "No valid BIOS image found\n");
  215. return false;
  216. }
  217. struct init_tbl_entry {
  218. char *name;
  219. uint8_t id;
  220. /* Return:
  221. * > 0: success, length of opcode
  222. * 0: success, but abort further parsing of table (INIT_DONE etc)
  223. * < 0: failure, table parsing will be aborted
  224. */
  225. int (*handler)(struct nvbios *, uint16_t, struct init_exec *);
  226. };
  227. struct bit_entry {
  228. uint8_t id[2];
  229. uint16_t length;
  230. uint16_t offset;
  231. };
  232. static int parse_init_table(struct nvbios *, unsigned int, struct init_exec *);
  233. #define MACRO_INDEX_SIZE 2
  234. #define MACRO_SIZE 8
  235. #define CONDITION_SIZE 12
  236. #define IO_FLAG_CONDITION_SIZE 9
  237. #define IO_CONDITION_SIZE 5
  238. #define MEM_INIT_SIZE 66
  239. static void still_alive(void)
  240. {
  241. #if 0
  242. sync();
  243. msleep(2);
  244. #endif
  245. }
  246. static uint32_t
  247. munge_reg(struct nvbios *bios, uint32_t reg)
  248. {
  249. struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
  250. struct dcb_entry *dcbent = bios->display.output;
  251. if (dev_priv->card_type < NV_50)
  252. return reg;
  253. if (reg & 0x40000000) {
  254. BUG_ON(!dcbent);
  255. reg += (ffs(dcbent->or) - 1) * 0x800;
  256. if ((reg & 0x20000000) && !(dcbent->sorconf.link & 1))
  257. reg += 0x00000080;
  258. }
  259. reg &= ~0x60000000;
  260. return reg;
  261. }
  262. static int
  263. valid_reg(struct nvbios *bios, uint32_t reg)
  264. {
  265. struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
  266. struct drm_device *dev = bios->dev;
  267. /* C51 has misaligned regs on purpose. Marvellous */
  268. if (reg & 0x2 ||
  269. (reg & 0x1 && dev_priv->vbios.chip_version != 0x51))
  270. NV_ERROR(dev, "======= misaligned reg 0x%08X =======\n", reg);
  271. /* warn on C51 regs that haven't been verified accessible in tracing */
  272. if (reg & 0x1 && dev_priv->vbios.chip_version == 0x51 &&
  273. reg != 0x130d && reg != 0x1311 && reg != 0x60081d)
  274. NV_WARN(dev, "=== C51 misaligned reg 0x%08X not verified ===\n",
  275. reg);
  276. if (reg >= (8*1024*1024)) {
  277. NV_ERROR(dev, "=== reg 0x%08x out of mapped bounds ===\n", reg);
  278. return 0;
  279. }
  280. return 1;
  281. }
  282. static bool
  283. valid_idx_port(struct nvbios *bios, uint16_t port)
  284. {
  285. struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
  286. struct drm_device *dev = bios->dev;
  287. /*
  288. * If adding more ports here, the read/write functions below will need
  289. * updating so that the correct mmio range (PRMCIO, PRMDIO, PRMVIO) is
  290. * used for the port in question
  291. */
  292. if (dev_priv->card_type < NV_50) {
  293. if (port == NV_CIO_CRX__COLOR)
  294. return true;
  295. if (port == NV_VIO_SRX)
  296. return true;
  297. } else {
  298. if (port == NV_CIO_CRX__COLOR)
  299. return true;
  300. }
  301. NV_ERROR(dev, "========== unknown indexed io port 0x%04X ==========\n",
  302. port);
  303. return false;
  304. }
  305. static bool
  306. valid_port(struct nvbios *bios, uint16_t port)
  307. {
  308. struct drm_device *dev = bios->dev;
  309. /*
  310. * If adding more ports here, the read/write functions below will need
  311. * updating so that the correct mmio range (PRMCIO, PRMDIO, PRMVIO) is
  312. * used for the port in question
  313. */
  314. if (port == NV_VIO_VSE2)
  315. return true;
  316. NV_ERROR(dev, "========== unknown io port 0x%04X ==========\n", port);
  317. return false;
  318. }
  319. static uint32_t
  320. bios_rd32(struct nvbios *bios, uint32_t reg)
  321. {
  322. uint32_t data;
  323. reg = munge_reg(bios, reg);
  324. if (!valid_reg(bios, reg))
  325. return 0;
  326. /*
  327. * C51 sometimes uses regs with bit0 set in the address. For these
  328. * cases there should exist a translation in a BIOS table to an IO
  329. * port address which the BIOS uses for accessing the reg
  330. *
  331. * These only seem to appear for the power control regs to a flat panel,
  332. * and the GPIO regs at 0x60081*. In C51 mmio traces the normal regs
  333. * for 0x1308 and 0x1310 are used - hence the mask below. An S3
  334. * suspend-resume mmio trace from a C51 will be required to see if this
  335. * is true for the power microcode in 0x14.., or whether the direct IO
  336. * port access method is needed
  337. */
  338. if (reg & 0x1)
  339. reg &= ~0x1;
  340. data = nv_rd32(bios->dev, reg);
  341. BIOSLOG(bios, " Read: Reg: 0x%08X, Data: 0x%08X\n", reg, data);
  342. return data;
  343. }
  344. static void
  345. bios_wr32(struct nvbios *bios, uint32_t reg, uint32_t data)
  346. {
  347. struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
  348. reg = munge_reg(bios, reg);
  349. if (!valid_reg(bios, reg))
  350. return;
  351. /* see note in bios_rd32 */
  352. if (reg & 0x1)
  353. reg &= 0xfffffffe;
  354. LOG_OLD_VALUE(bios_rd32(bios, reg));
  355. BIOSLOG(bios, " Write: Reg: 0x%08X, Data: 0x%08X\n", reg, data);
  356. if (dev_priv->vbios.execute) {
  357. still_alive();
  358. nv_wr32(bios->dev, reg, data);
  359. }
  360. }
  361. static uint8_t
  362. bios_idxprt_rd(struct nvbios *bios, uint16_t port, uint8_t index)
  363. {
  364. struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
  365. struct drm_device *dev = bios->dev;
  366. uint8_t data;
  367. if (!valid_idx_port(bios, port))
  368. return 0;
  369. if (dev_priv->card_type < NV_50) {
  370. if (port == NV_VIO_SRX)
  371. data = NVReadVgaSeq(dev, bios->state.crtchead, index);
  372. else /* assume NV_CIO_CRX__COLOR */
  373. data = NVReadVgaCrtc(dev, bios->state.crtchead, index);
  374. } else {
  375. uint32_t data32;
  376. data32 = bios_rd32(bios, NV50_PDISPLAY_VGACRTC(index & ~3));
  377. data = (data32 >> ((index & 3) << 3)) & 0xff;
  378. }
  379. BIOSLOG(bios, " Indexed IO read: Port: 0x%04X, Index: 0x%02X, "
  380. "Head: 0x%02X, Data: 0x%02X\n",
  381. port, index, bios->state.crtchead, data);
  382. return data;
  383. }
  384. static void
  385. bios_idxprt_wr(struct nvbios *bios, uint16_t port, uint8_t index, uint8_t data)
  386. {
  387. struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
  388. struct drm_device *dev = bios->dev;
  389. if (!valid_idx_port(bios, port))
  390. return;
  391. /*
  392. * The current head is maintained in the nvbios member state.crtchead.
  393. * We trap changes to CR44 and update the head variable and hence the
  394. * register set written.
  395. * As CR44 only exists on CRTC0, we update crtchead to head0 in advance
  396. * of the write, and to head1 after the write
  397. */
  398. if (port == NV_CIO_CRX__COLOR && index == NV_CIO_CRE_44 &&
  399. data != NV_CIO_CRE_44_HEADB)
  400. bios->state.crtchead = 0;
  401. LOG_OLD_VALUE(bios_idxprt_rd(bios, port, index));
  402. BIOSLOG(bios, " Indexed IO write: Port: 0x%04X, Index: 0x%02X, "
  403. "Head: 0x%02X, Data: 0x%02X\n",
  404. port, index, bios->state.crtchead, data);
  405. if (bios->execute && dev_priv->card_type < NV_50) {
  406. still_alive();
  407. if (port == NV_VIO_SRX)
  408. NVWriteVgaSeq(dev, bios->state.crtchead, index, data);
  409. else /* assume NV_CIO_CRX__COLOR */
  410. NVWriteVgaCrtc(dev, bios->state.crtchead, index, data);
  411. } else
  412. if (bios->execute) {
  413. uint32_t data32, shift = (index & 3) << 3;
  414. still_alive();
  415. data32 = bios_rd32(bios, NV50_PDISPLAY_VGACRTC(index & ~3));
  416. data32 &= ~(0xff << shift);
  417. data32 |= (data << shift);
  418. bios_wr32(bios, NV50_PDISPLAY_VGACRTC(index & ~3), data32);
  419. }
  420. if (port == NV_CIO_CRX__COLOR &&
  421. index == NV_CIO_CRE_44 && data == NV_CIO_CRE_44_HEADB)
  422. bios->state.crtchead = 1;
  423. }
  424. static uint8_t
  425. bios_port_rd(struct nvbios *bios, uint16_t port)
  426. {
  427. uint8_t data, head = bios->state.crtchead;
  428. if (!valid_port(bios, port))
  429. return 0;
  430. data = NVReadPRMVIO(bios->dev, head, NV_PRMVIO0_OFFSET + port);
  431. BIOSLOG(bios, " IO read: Port: 0x%04X, Head: 0x%02X, Data: 0x%02X\n",
  432. port, head, data);
  433. return data;
  434. }
  435. static void
  436. bios_port_wr(struct nvbios *bios, uint16_t port, uint8_t data)
  437. {
  438. int head = bios->state.crtchead;
  439. if (!valid_port(bios, port))
  440. return;
  441. LOG_OLD_VALUE(bios_port_rd(bios, port));
  442. BIOSLOG(bios, " IO write: Port: 0x%04X, Head: 0x%02X, Data: 0x%02X\n",
  443. port, head, data);
  444. if (!bios->execute)
  445. return;
  446. still_alive();
  447. NVWritePRMVIO(bios->dev, head, NV_PRMVIO0_OFFSET + port, data);
  448. }
  449. static bool
  450. io_flag_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)
  451. {
  452. /*
  453. * The IO flag condition entry has 2 bytes for the CRTC port; 1 byte
  454. * for the CRTC index; 1 byte for the mask to apply to the value
  455. * retrieved from the CRTC; 1 byte for the shift right to apply to the
  456. * masked CRTC value; 2 bytes for the offset to the flag array, to
  457. * which the shifted value is added; 1 byte for the mask applied to the
  458. * value read from the flag array; and 1 byte for the value to compare
  459. * against the masked byte from the flag table.
  460. */
  461. uint16_t condptr = bios->io_flag_condition_tbl_ptr + cond * IO_FLAG_CONDITION_SIZE;
  462. uint16_t crtcport = ROM16(bios->data[condptr]);
  463. uint8_t crtcindex = bios->data[condptr + 2];
  464. uint8_t mask = bios->data[condptr + 3];
  465. uint8_t shift = bios->data[condptr + 4];
  466. uint16_t flagarray = ROM16(bios->data[condptr + 5]);
  467. uint8_t flagarraymask = bios->data[condptr + 7];
  468. uint8_t cmpval = bios->data[condptr + 8];
  469. uint8_t data;
  470. BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
  471. "Shift: 0x%02X, FlagArray: 0x%04X, FAMask: 0x%02X, "
  472. "Cmpval: 0x%02X\n",
  473. offset, crtcport, crtcindex, mask, shift, flagarray, flagarraymask, cmpval);
  474. data = bios_idxprt_rd(bios, crtcport, crtcindex);
  475. data = bios->data[flagarray + ((data & mask) >> shift)];
  476. data &= flagarraymask;
  477. BIOSLOG(bios, "0x%04X: Checking if 0x%02X equals 0x%02X\n",
  478. offset, data, cmpval);
  479. return (data == cmpval);
  480. }
  481. static bool
  482. bios_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)
  483. {
  484. /*
  485. * The condition table entry has 4 bytes for the address of the
  486. * register to check, 4 bytes for a mask to apply to the register and
  487. * 4 for a test comparison value
  488. */
  489. uint16_t condptr = bios->condition_tbl_ptr + cond * CONDITION_SIZE;
  490. uint32_t reg = ROM32(bios->data[condptr]);
  491. uint32_t mask = ROM32(bios->data[condptr + 4]);
  492. uint32_t cmpval = ROM32(bios->data[condptr + 8]);
  493. uint32_t data;
  494. BIOSLOG(bios, "0x%04X: Cond: 0x%02X, Reg: 0x%08X, Mask: 0x%08X\n",
  495. offset, cond, reg, mask);
  496. data = bios_rd32(bios, reg) & mask;
  497. BIOSLOG(bios, "0x%04X: Checking if 0x%08X equals 0x%08X\n",
  498. offset, data, cmpval);
  499. return (data == cmpval);
  500. }
  501. static bool
  502. io_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)
  503. {
  504. /*
  505. * The IO condition entry has 2 bytes for the IO port address; 1 byte
  506. * for the index to write to io_port; 1 byte for the mask to apply to
  507. * the byte read from io_port+1; and 1 byte for the value to compare
  508. * against the masked byte.
  509. */
  510. uint16_t condptr = bios->io_condition_tbl_ptr + cond * IO_CONDITION_SIZE;
  511. uint16_t io_port = ROM16(bios->data[condptr]);
  512. uint8_t port_index = bios->data[condptr + 2];
  513. uint8_t mask = bios->data[condptr + 3];
  514. uint8_t cmpval = bios->data[condptr + 4];
  515. uint8_t data = bios_idxprt_rd(bios, io_port, port_index) & mask;
  516. BIOSLOG(bios, "0x%04X: Checking if 0x%02X equals 0x%02X\n",
  517. offset, data, cmpval);
  518. return (data == cmpval);
  519. }
  520. static int
  521. nv50_pll_set(struct drm_device *dev, uint32_t reg, uint32_t clk)
  522. {
  523. struct drm_nouveau_private *dev_priv = dev->dev_private;
  524. uint32_t reg0 = nv_rd32(dev, reg + 0);
  525. uint32_t reg1 = nv_rd32(dev, reg + 4);
  526. struct nouveau_pll_vals pll;
  527. struct pll_lims pll_limits;
  528. int ret;
  529. ret = get_pll_limits(dev, reg, &pll_limits);
  530. if (ret)
  531. return ret;
  532. clk = nouveau_calc_pll_mnp(dev, &pll_limits, clk, &pll);
  533. if (!clk)
  534. return -ERANGE;
  535. reg0 = (reg0 & 0xfff8ffff) | (pll.log2P << 16);
  536. reg1 = (reg1 & 0xffff0000) | (pll.N1 << 8) | pll.M1;
  537. if (dev_priv->vbios.execute) {
  538. still_alive();
  539. nv_wr32(dev, reg + 4, reg1);
  540. nv_wr32(dev, reg + 0, reg0);
  541. }
  542. return 0;
  543. }
  544. static int
  545. setPLL(struct nvbios *bios, uint32_t reg, uint32_t clk)
  546. {
  547. struct drm_device *dev = bios->dev;
  548. struct drm_nouveau_private *dev_priv = dev->dev_private;
  549. /* clk in kHz */
  550. struct pll_lims pll_lim;
  551. struct nouveau_pll_vals pllvals;
  552. int ret;
  553. if (dev_priv->card_type >= NV_50)
  554. return nv50_pll_set(dev, reg, clk);
  555. /* high regs (such as in the mac g5 table) are not -= 4 */
  556. ret = get_pll_limits(dev, reg > 0x405c ? reg : reg - 4, &pll_lim);
  557. if (ret)
  558. return ret;
  559. clk = nouveau_calc_pll_mnp(dev, &pll_lim, clk, &pllvals);
  560. if (!clk)
  561. return -ERANGE;
  562. if (bios->execute) {
  563. still_alive();
  564. nouveau_hw_setpll(dev, reg, &pllvals);
  565. }
  566. return 0;
  567. }
  568. static int dcb_entry_idx_from_crtchead(struct drm_device *dev)
  569. {
  570. struct drm_nouveau_private *dev_priv = dev->dev_private;
  571. struct nvbios *bios = &dev_priv->vbios;
  572. /*
  573. * For the results of this function to be correct, CR44 must have been
  574. * set (using bios_idxprt_wr to set crtchead), CR58 set for CR57 = 0,
  575. * and the DCB table parsed, before the script calling the function is
  576. * run. run_digital_op_script is example of how to do such setup
  577. */
  578. uint8_t dcb_entry = NVReadVgaCrtc5758(dev, bios->state.crtchead, 0);
  579. if (dcb_entry > bios->dcb.entries) {
  580. NV_ERROR(dev, "CR58 doesn't have a valid DCB entry currently "
  581. "(%02X)\n", dcb_entry);
  582. dcb_entry = 0x7f; /* unused / invalid marker */
  583. }
  584. return dcb_entry;
  585. }
  586. static int
  587. read_dcb_i2c_entry(struct drm_device *dev, int dcb_version, uint8_t *i2ctable, int index, struct dcb_i2c_entry *i2c)
  588. {
  589. uint8_t dcb_i2c_ver = dcb_version, headerlen = 0, entry_len = 4;
  590. int i2c_entries = DCB_MAX_NUM_I2C_ENTRIES;
  591. int recordoffset = 0, rdofs = 1, wrofs = 0;
  592. uint8_t port_type = 0;
  593. if (!i2ctable)
  594. return -EINVAL;
  595. if (dcb_version >= 0x30) {
  596. if (i2ctable[0] != dcb_version) /* necessary? */
  597. NV_WARN(dev,
  598. "DCB I2C table version mismatch (%02X vs %02X)\n",
  599. i2ctable[0], dcb_version);
  600. dcb_i2c_ver = i2ctable[0];
  601. headerlen = i2ctable[1];
  602. if (i2ctable[2] <= DCB_MAX_NUM_I2C_ENTRIES)
  603. i2c_entries = i2ctable[2];
  604. else
  605. NV_WARN(dev,
  606. "DCB I2C table has more entries than indexable "
  607. "(%d entries, max %d)\n", i2ctable[2],
  608. DCB_MAX_NUM_I2C_ENTRIES);
  609. entry_len = i2ctable[3];
  610. /* [4] is i2c_default_indices, read in parse_dcb_table() */
  611. }
  612. /*
  613. * It's your own fault if you call this function on a DCB 1.1 BIOS --
  614. * the test below is for DCB 1.2
  615. */
  616. if (dcb_version < 0x14) {
  617. recordoffset = 2;
  618. rdofs = 0;
  619. wrofs = 1;
  620. }
  621. if (index == 0xf)
  622. return 0;
  623. if (index >= i2c_entries) {
  624. NV_ERROR(dev, "DCB I2C index too big (%d >= %d)\n",
  625. index, i2ctable[2]);
  626. return -ENOENT;
  627. }
  628. if (i2ctable[headerlen + entry_len * index + 3] == 0xff) {
  629. NV_ERROR(dev, "DCB I2C entry invalid\n");
  630. return -EINVAL;
  631. }
  632. if (dcb_i2c_ver >= 0x30) {
  633. port_type = i2ctable[headerlen + recordoffset + 3 + entry_len * index];
  634. /*
  635. * Fixup for chips using same address offset for read and
  636. * write.
  637. */
  638. if (port_type == 4) /* seen on C51 */
  639. rdofs = wrofs = 1;
  640. if (port_type >= 5) /* G80+ */
  641. rdofs = wrofs = 0;
  642. }
  643. if (dcb_i2c_ver >= 0x40) {
  644. if (port_type != 5 && port_type != 6)
  645. NV_WARN(dev, "DCB I2C table has port type %d\n", port_type);
  646. i2c->entry = ROM32(i2ctable[headerlen + recordoffset + entry_len * index]);
  647. }
  648. i2c->port_type = port_type;
  649. i2c->read = i2ctable[headerlen + recordoffset + rdofs + entry_len * index];
  650. i2c->write = i2ctable[headerlen + recordoffset + wrofs + entry_len * index];
  651. return 0;
  652. }
  653. static struct nouveau_i2c_chan *
  654. init_i2c_device_find(struct drm_device *dev, int i2c_index)
  655. {
  656. struct drm_nouveau_private *dev_priv = dev->dev_private;
  657. struct dcb_table *dcb = &dev_priv->vbios.dcb;
  658. if (i2c_index == 0xff) {
  659. /* note: dcb_entry_idx_from_crtchead needs pre-script set-up */
  660. int idx = dcb_entry_idx_from_crtchead(dev), shift = 0;
  661. int default_indices = dcb->i2c_default_indices;
  662. if (idx != 0x7f && dcb->entry[idx].i2c_upper_default)
  663. shift = 4;
  664. i2c_index = (default_indices >> shift) & 0xf;
  665. }
  666. if (i2c_index == 0x80) /* g80+ */
  667. i2c_index = dcb->i2c_default_indices & 0xf;
  668. else
  669. if (i2c_index == 0x81)
  670. i2c_index = (dcb->i2c_default_indices & 0xf0) >> 4;
  671. if (i2c_index >= DCB_MAX_NUM_I2C_ENTRIES) {
  672. NV_ERROR(dev, "invalid i2c_index 0x%x\n", i2c_index);
  673. return NULL;
  674. }
  675. /* Make sure i2c table entry has been parsed, it may not
  676. * have been if this is a bus not referenced by a DCB encoder
  677. */
  678. read_dcb_i2c_entry(dev, dcb->version, dcb->i2c_table,
  679. i2c_index, &dcb->i2c[i2c_index]);
  680. return nouveau_i2c_find(dev, i2c_index);
  681. }
  682. static uint32_t
  683. get_tmds_index_reg(struct drm_device *dev, uint8_t mlv)
  684. {
  685. /*
  686. * For mlv < 0x80, it is an index into a table of TMDS base addresses.
  687. * For mlv == 0x80 use the "or" value of the dcb_entry indexed by
  688. * CR58 for CR57 = 0 to index a table of offsets to the basic
  689. * 0x6808b0 address.
  690. * For mlv == 0x81 use the "or" value of the dcb_entry indexed by
  691. * CR58 for CR57 = 0 to index a table of offsets to the basic
  692. * 0x6808b0 address, and then flip the offset by 8.
  693. */
  694. struct drm_nouveau_private *dev_priv = dev->dev_private;
  695. struct nvbios *bios = &dev_priv->vbios;
  696. const int pramdac_offset[13] = {
  697. 0, 0, 0x8, 0, 0x2000, 0, 0, 0, 0x2008, 0, 0, 0, 0x2000 };
  698. const uint32_t pramdac_table[4] = {
  699. 0x6808b0, 0x6808b8, 0x6828b0, 0x6828b8 };
  700. if (mlv >= 0x80) {
  701. int dcb_entry, dacoffset;
  702. /* note: dcb_entry_idx_from_crtchead needs pre-script set-up */
  703. dcb_entry = dcb_entry_idx_from_crtchead(dev);
  704. if (dcb_entry == 0x7f)
  705. return 0;
  706. dacoffset = pramdac_offset[bios->dcb.entry[dcb_entry].or];
  707. if (mlv == 0x81)
  708. dacoffset ^= 8;
  709. return 0x6808b0 + dacoffset;
  710. } else {
  711. if (mlv >= ARRAY_SIZE(pramdac_table)) {
  712. NV_ERROR(dev, "Magic Lookup Value too big (%02X)\n",
  713. mlv);
  714. return 0;
  715. }
  716. return pramdac_table[mlv];
  717. }
  718. }
  719. static int
  720. init_io_restrict_prog(struct nvbios *bios, uint16_t offset,
  721. struct init_exec *iexec)
  722. {
  723. /*
  724. * INIT_IO_RESTRICT_PROG opcode: 0x32 ('2')
  725. *
  726. * offset (8 bit): opcode
  727. * offset + 1 (16 bit): CRTC port
  728. * offset + 3 (8 bit): CRTC index
  729. * offset + 4 (8 bit): mask
  730. * offset + 5 (8 bit): shift
  731. * offset + 6 (8 bit): count
  732. * offset + 7 (32 bit): register
  733. * offset + 11 (32 bit): configuration 1
  734. * ...
  735. *
  736. * Starting at offset + 11 there are "count" 32 bit values.
  737. * To find out which value to use read index "CRTC index" on "CRTC
  738. * port", AND this value with "mask" and then bit shift right "shift"
  739. * bits. Read the appropriate value using this index and write to
  740. * "register"
  741. */
  742. uint16_t crtcport = ROM16(bios->data[offset + 1]);
  743. uint8_t crtcindex = bios->data[offset + 3];
  744. uint8_t mask = bios->data[offset + 4];
  745. uint8_t shift = bios->data[offset + 5];
  746. uint8_t count = bios->data[offset + 6];
  747. uint32_t reg = ROM32(bios->data[offset + 7]);
  748. uint8_t config;
  749. uint32_t configval;
  750. int len = 11 + count * 4;
  751. if (!iexec->execute)
  752. return len;
  753. BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
  754. "Shift: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",
  755. offset, crtcport, crtcindex, mask, shift, count, reg);
  756. config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;
  757. if (config > count) {
  758. NV_ERROR(bios->dev,
  759. "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
  760. offset, config, count);
  761. return len;
  762. }
  763. configval = ROM32(bios->data[offset + 11 + config * 4]);
  764. BIOSLOG(bios, "0x%04X: Writing config %02X\n", offset, config);
  765. bios_wr32(bios, reg, configval);
  766. return len;
  767. }
  768. static int
  769. init_repeat(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  770. {
  771. /*
  772. * INIT_REPEAT opcode: 0x33 ('3')
  773. *
  774. * offset (8 bit): opcode
  775. * offset + 1 (8 bit): count
  776. *
  777. * Execute script following this opcode up to INIT_REPEAT_END
  778. * "count" times
  779. */
  780. uint8_t count = bios->data[offset + 1];
  781. uint8_t i;
  782. /* no iexec->execute check by design */
  783. BIOSLOG(bios, "0x%04X: Repeating following segment %d times\n",
  784. offset, count);
  785. iexec->repeat = true;
  786. /*
  787. * count - 1, as the script block will execute once when we leave this
  788. * opcode -- this is compatible with bios behaviour as:
  789. * a) the block is always executed at least once, even if count == 0
  790. * b) the bios interpreter skips to the op following INIT_END_REPEAT,
  791. * while we don't
  792. */
  793. for (i = 0; i < count - 1; i++)
  794. parse_init_table(bios, offset + 2, iexec);
  795. iexec->repeat = false;
  796. return 2;
  797. }
  798. static int
  799. init_io_restrict_pll(struct nvbios *bios, uint16_t offset,
  800. struct init_exec *iexec)
  801. {
  802. /*
  803. * INIT_IO_RESTRICT_PLL opcode: 0x34 ('4')
  804. *
  805. * offset (8 bit): opcode
  806. * offset + 1 (16 bit): CRTC port
  807. * offset + 3 (8 bit): CRTC index
  808. * offset + 4 (8 bit): mask
  809. * offset + 5 (8 bit): shift
  810. * offset + 6 (8 bit): IO flag condition index
  811. * offset + 7 (8 bit): count
  812. * offset + 8 (32 bit): register
  813. * offset + 12 (16 bit): frequency 1
  814. * ...
  815. *
  816. * Starting at offset + 12 there are "count" 16 bit frequencies (10kHz).
  817. * Set PLL register "register" to coefficients for frequency n,
  818. * selected by reading index "CRTC index" of "CRTC port" ANDed with
  819. * "mask" and shifted right by "shift".
  820. *
  821. * If "IO flag condition index" > 0, and condition met, double
  822. * frequency before setting it.
  823. */
  824. uint16_t crtcport = ROM16(bios->data[offset + 1]);
  825. uint8_t crtcindex = bios->data[offset + 3];
  826. uint8_t mask = bios->data[offset + 4];
  827. uint8_t shift = bios->data[offset + 5];
  828. int8_t io_flag_condition_idx = bios->data[offset + 6];
  829. uint8_t count = bios->data[offset + 7];
  830. uint32_t reg = ROM32(bios->data[offset + 8]);
  831. uint8_t config;
  832. uint16_t freq;
  833. int len = 12 + count * 2;
  834. if (!iexec->execute)
  835. return len;
  836. BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
  837. "Shift: 0x%02X, IO Flag Condition: 0x%02X, "
  838. "Count: 0x%02X, Reg: 0x%08X\n",
  839. offset, crtcport, crtcindex, mask, shift,
  840. io_flag_condition_idx, count, reg);
  841. config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;
  842. if (config > count) {
  843. NV_ERROR(bios->dev,
  844. "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
  845. offset, config, count);
  846. return len;
  847. }
  848. freq = ROM16(bios->data[offset + 12 + config * 2]);
  849. if (io_flag_condition_idx > 0) {
  850. if (io_flag_condition_met(bios, offset, io_flag_condition_idx)) {
  851. BIOSLOG(bios, "0x%04X: Condition fulfilled -- "
  852. "frequency doubled\n", offset);
  853. freq *= 2;
  854. } else
  855. BIOSLOG(bios, "0x%04X: Condition not fulfilled -- "
  856. "frequency unchanged\n", offset);
  857. }
  858. BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Config: 0x%02X, Freq: %d0kHz\n",
  859. offset, reg, config, freq);
  860. setPLL(bios, reg, freq * 10);
  861. return len;
  862. }
  863. static int
  864. init_end_repeat(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  865. {
  866. /*
  867. * INIT_END_REPEAT opcode: 0x36 ('6')
  868. *
  869. * offset (8 bit): opcode
  870. *
  871. * Marks the end of the block for INIT_REPEAT to repeat
  872. */
  873. /* no iexec->execute check by design */
  874. /*
  875. * iexec->repeat flag necessary to go past INIT_END_REPEAT opcode when
  876. * we're not in repeat mode
  877. */
  878. if (iexec->repeat)
  879. return 0;
  880. return 1;
  881. }
  882. static int
  883. init_copy(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  884. {
  885. /*
  886. * INIT_COPY opcode: 0x37 ('7')
  887. *
  888. * offset (8 bit): opcode
  889. * offset + 1 (32 bit): register
  890. * offset + 5 (8 bit): shift
  891. * offset + 6 (8 bit): srcmask
  892. * offset + 7 (16 bit): CRTC port
  893. * offset + 9 (8 bit): CRTC index
  894. * offset + 10 (8 bit): mask
  895. *
  896. * Read index "CRTC index" on "CRTC port", AND with "mask", OR with
  897. * (REGVAL("register") >> "shift" & "srcmask") and write-back to CRTC
  898. * port
  899. */
  900. uint32_t reg = ROM32(bios->data[offset + 1]);
  901. uint8_t shift = bios->data[offset + 5];
  902. uint8_t srcmask = bios->data[offset + 6];
  903. uint16_t crtcport = ROM16(bios->data[offset + 7]);
  904. uint8_t crtcindex = bios->data[offset + 9];
  905. uint8_t mask = bios->data[offset + 10];
  906. uint32_t data;
  907. uint8_t crtcdata;
  908. if (!iexec->execute)
  909. return 11;
  910. BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Shift: 0x%02X, SrcMask: 0x%02X, "
  911. "Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X\n",
  912. offset, reg, shift, srcmask, crtcport, crtcindex, mask);
  913. data = bios_rd32(bios, reg);
  914. if (shift < 0x80)
  915. data >>= shift;
  916. else
  917. data <<= (0x100 - shift);
  918. data &= srcmask;
  919. crtcdata = bios_idxprt_rd(bios, crtcport, crtcindex) & mask;
  920. crtcdata |= (uint8_t)data;
  921. bios_idxprt_wr(bios, crtcport, crtcindex, crtcdata);
  922. return 11;
  923. }
  924. static int
  925. init_not(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  926. {
  927. /*
  928. * INIT_NOT opcode: 0x38 ('8')
  929. *
  930. * offset (8 bit): opcode
  931. *
  932. * Invert the current execute / no-execute condition (i.e. "else")
  933. */
  934. if (iexec->execute)
  935. BIOSLOG(bios, "0x%04X: ------ Skipping following commands ------\n", offset);
  936. else
  937. BIOSLOG(bios, "0x%04X: ------ Executing following commands ------\n", offset);
  938. iexec->execute = !iexec->execute;
  939. return 1;
  940. }
  941. static int
  942. init_io_flag_condition(struct nvbios *bios, uint16_t offset,
  943. struct init_exec *iexec)
  944. {
  945. /*
  946. * INIT_IO_FLAG_CONDITION opcode: 0x39 ('9')
  947. *
  948. * offset (8 bit): opcode
  949. * offset + 1 (8 bit): condition number
  950. *
  951. * Check condition "condition number" in the IO flag condition table.
  952. * If condition not met skip subsequent opcodes until condition is
  953. * inverted (INIT_NOT), or we hit INIT_RESUME
  954. */
  955. uint8_t cond = bios->data[offset + 1];
  956. if (!iexec->execute)
  957. return 2;
  958. if (io_flag_condition_met(bios, offset, cond))
  959. BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
  960. else {
  961. BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
  962. iexec->execute = false;
  963. }
  964. return 2;
  965. }
  966. static int
  967. init_dp_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  968. {
  969. /*
  970. * INIT_DP_CONDITION opcode: 0x3A ('')
  971. *
  972. * offset (8 bit): opcode
  973. * offset + 1 (8 bit): "sub" opcode
  974. * offset + 2 (8 bit): unknown
  975. *
  976. */
  977. struct bit_displayport_encoder_table *dpe = NULL;
  978. struct dcb_entry *dcb = bios->display.output;
  979. struct drm_device *dev = bios->dev;
  980. uint8_t cond = bios->data[offset + 1];
  981. int dummy;
  982. BIOSLOG(bios, "0x%04X: subop 0x%02X\n", offset, cond);
  983. if (!iexec->execute)
  984. return 3;
  985. dpe = nouveau_bios_dp_table(dev, dcb, &dummy);
  986. if (!dpe) {
  987. NV_ERROR(dev, "0x%04X: INIT_3A: no encoder table!!\n", offset);
  988. return 3;
  989. }
  990. switch (cond) {
  991. case 0:
  992. {
  993. struct dcb_connector_table_entry *ent =
  994. &bios->dcb.connector.entry[dcb->connector];
  995. if (ent->type != DCB_CONNECTOR_eDP)
  996. iexec->execute = false;
  997. }
  998. break;
  999. case 1:
  1000. case 2:
  1001. if (!(dpe->unknown & cond))
  1002. iexec->execute = false;
  1003. break;
  1004. case 5:
  1005. {
  1006. struct nouveau_i2c_chan *auxch;
  1007. int ret;
  1008. auxch = nouveau_i2c_find(dev, bios->display.output->i2c_index);
  1009. if (!auxch) {
  1010. NV_ERROR(dev, "0x%04X: couldn't get auxch\n", offset);
  1011. return 3;
  1012. }
  1013. ret = nouveau_dp_auxch(auxch, 9, 0xd, &cond, 1);
  1014. if (ret) {
  1015. NV_ERROR(dev, "0x%04X: auxch rd fail: %d\n", offset, ret);
  1016. return 3;
  1017. }
  1018. if (cond & 1)
  1019. iexec->execute = false;
  1020. }
  1021. break;
  1022. default:
  1023. NV_WARN(dev, "0x%04X: unknown INIT_3A op: %d\n", offset, cond);
  1024. break;
  1025. }
  1026. if (iexec->execute)
  1027. BIOSLOG(bios, "0x%04X: continuing to execute\n", offset);
  1028. else
  1029. BIOSLOG(bios, "0x%04X: skipping following commands\n", offset);
  1030. return 3;
  1031. }
  1032. static int
  1033. init_op_3b(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1034. {
  1035. /*
  1036. * INIT_3B opcode: 0x3B ('')
  1037. *
  1038. * offset (8 bit): opcode
  1039. * offset + 1 (8 bit): crtc index
  1040. *
  1041. */
  1042. uint8_t or = ffs(bios->display.output->or) - 1;
  1043. uint8_t index = bios->data[offset + 1];
  1044. uint8_t data;
  1045. if (!iexec->execute)
  1046. return 2;
  1047. data = bios_idxprt_rd(bios, 0x3d4, index);
  1048. bios_idxprt_wr(bios, 0x3d4, index, data & ~(1 << or));
  1049. return 2;
  1050. }
  1051. static int
  1052. init_op_3c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1053. {
  1054. /*
  1055. * INIT_3C opcode: 0x3C ('')
  1056. *
  1057. * offset (8 bit): opcode
  1058. * offset + 1 (8 bit): crtc index
  1059. *
  1060. */
  1061. uint8_t or = ffs(bios->display.output->or) - 1;
  1062. uint8_t index = bios->data[offset + 1];
  1063. uint8_t data;
  1064. if (!iexec->execute)
  1065. return 2;
  1066. data = bios_idxprt_rd(bios, 0x3d4, index);
  1067. bios_idxprt_wr(bios, 0x3d4, index, data | (1 << or));
  1068. return 2;
  1069. }
  1070. static int
  1071. init_idx_addr_latched(struct nvbios *bios, uint16_t offset,
  1072. struct init_exec *iexec)
  1073. {
  1074. /*
  1075. * INIT_INDEX_ADDRESS_LATCHED opcode: 0x49 ('I')
  1076. *
  1077. * offset (8 bit): opcode
  1078. * offset + 1 (32 bit): control register
  1079. * offset + 5 (32 bit): data register
  1080. * offset + 9 (32 bit): mask
  1081. * offset + 13 (32 bit): data
  1082. * offset + 17 (8 bit): count
  1083. * offset + 18 (8 bit): address 1
  1084. * offset + 19 (8 bit): data 1
  1085. * ...
  1086. *
  1087. * For each of "count" address and data pairs, write "data n" to
  1088. * "data register", read the current value of "control register",
  1089. * and write it back once ANDed with "mask", ORed with "data",
  1090. * and ORed with "address n"
  1091. */
  1092. uint32_t controlreg = ROM32(bios->data[offset + 1]);
  1093. uint32_t datareg = ROM32(bios->data[offset + 5]);
  1094. uint32_t mask = ROM32(bios->data[offset + 9]);
  1095. uint32_t data = ROM32(bios->data[offset + 13]);
  1096. uint8_t count = bios->data[offset + 17];
  1097. int len = 18 + count * 2;
  1098. uint32_t value;
  1099. int i;
  1100. if (!iexec->execute)
  1101. return len;
  1102. BIOSLOG(bios, "0x%04X: ControlReg: 0x%08X, DataReg: 0x%08X, "
  1103. "Mask: 0x%08X, Data: 0x%08X, Count: 0x%02X\n",
  1104. offset, controlreg, datareg, mask, data, count);
  1105. for (i = 0; i < count; i++) {
  1106. uint8_t instaddress = bios->data[offset + 18 + i * 2];
  1107. uint8_t instdata = bios->data[offset + 19 + i * 2];
  1108. BIOSLOG(bios, "0x%04X: Address: 0x%02X, Data: 0x%02X\n",
  1109. offset, instaddress, instdata);
  1110. bios_wr32(bios, datareg, instdata);
  1111. value = bios_rd32(bios, controlreg) & mask;
  1112. value |= data;
  1113. value |= instaddress;
  1114. bios_wr32(bios, controlreg, value);
  1115. }
  1116. return len;
  1117. }
  1118. static int
  1119. init_io_restrict_pll2(struct nvbios *bios, uint16_t offset,
  1120. struct init_exec *iexec)
  1121. {
  1122. /*
  1123. * INIT_IO_RESTRICT_PLL2 opcode: 0x4A ('J')
  1124. *
  1125. * offset (8 bit): opcode
  1126. * offset + 1 (16 bit): CRTC port
  1127. * offset + 3 (8 bit): CRTC index
  1128. * offset + 4 (8 bit): mask
  1129. * offset + 5 (8 bit): shift
  1130. * offset + 6 (8 bit): count
  1131. * offset + 7 (32 bit): register
  1132. * offset + 11 (32 bit): frequency 1
  1133. * ...
  1134. *
  1135. * Starting at offset + 11 there are "count" 32 bit frequencies (kHz).
  1136. * Set PLL register "register" to coefficients for frequency n,
  1137. * selected by reading index "CRTC index" of "CRTC port" ANDed with
  1138. * "mask" and shifted right by "shift".
  1139. */
  1140. uint16_t crtcport = ROM16(bios->data[offset + 1]);
  1141. uint8_t crtcindex = bios->data[offset + 3];
  1142. uint8_t mask = bios->data[offset + 4];
  1143. uint8_t shift = bios->data[offset + 5];
  1144. uint8_t count = bios->data[offset + 6];
  1145. uint32_t reg = ROM32(bios->data[offset + 7]);
  1146. int len = 11 + count * 4;
  1147. uint8_t config;
  1148. uint32_t freq;
  1149. if (!iexec->execute)
  1150. return len;
  1151. BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
  1152. "Shift: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",
  1153. offset, crtcport, crtcindex, mask, shift, count, reg);
  1154. if (!reg)
  1155. return len;
  1156. config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;
  1157. if (config > count) {
  1158. NV_ERROR(bios->dev,
  1159. "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
  1160. offset, config, count);
  1161. return len;
  1162. }
  1163. freq = ROM32(bios->data[offset + 11 + config * 4]);
  1164. BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Config: 0x%02X, Freq: %dkHz\n",
  1165. offset, reg, config, freq);
  1166. setPLL(bios, reg, freq);
  1167. return len;
  1168. }
  1169. static int
  1170. init_pll2(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1171. {
  1172. /*
  1173. * INIT_PLL2 opcode: 0x4B ('K')
  1174. *
  1175. * offset (8 bit): opcode
  1176. * offset + 1 (32 bit): register
  1177. * offset + 5 (32 bit): freq
  1178. *
  1179. * Set PLL register "register" to coefficients for frequency "freq"
  1180. */
  1181. uint32_t reg = ROM32(bios->data[offset + 1]);
  1182. uint32_t freq = ROM32(bios->data[offset + 5]);
  1183. if (!iexec->execute)
  1184. return 9;
  1185. BIOSLOG(bios, "0x%04X: Reg: 0x%04X, Freq: %dkHz\n",
  1186. offset, reg, freq);
  1187. setPLL(bios, reg, freq);
  1188. return 9;
  1189. }
  1190. static int
  1191. init_i2c_byte(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1192. {
  1193. /*
  1194. * INIT_I2C_BYTE opcode: 0x4C ('L')
  1195. *
  1196. * offset (8 bit): opcode
  1197. * offset + 1 (8 bit): DCB I2C table entry index
  1198. * offset + 2 (8 bit): I2C slave address
  1199. * offset + 3 (8 bit): count
  1200. * offset + 4 (8 bit): I2C register 1
  1201. * offset + 5 (8 bit): mask 1
  1202. * offset + 6 (8 bit): data 1
  1203. * ...
  1204. *
  1205. * For each of "count" registers given by "I2C register n" on the device
  1206. * addressed by "I2C slave address" on the I2C bus given by
  1207. * "DCB I2C table entry index", read the register, AND the result with
  1208. * "mask n" and OR it with "data n" before writing it back to the device
  1209. */
  1210. struct drm_device *dev = bios->dev;
  1211. uint8_t i2c_index = bios->data[offset + 1];
  1212. uint8_t i2c_address = bios->data[offset + 2] >> 1;
  1213. uint8_t count = bios->data[offset + 3];
  1214. struct nouveau_i2c_chan *chan;
  1215. int len = 4 + count * 3;
  1216. int ret, i;
  1217. if (!iexec->execute)
  1218. return len;
  1219. BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "
  1220. "Count: 0x%02X\n",
  1221. offset, i2c_index, i2c_address, count);
  1222. chan = init_i2c_device_find(dev, i2c_index);
  1223. if (!chan) {
  1224. NV_ERROR(dev, "0x%04X: i2c bus not found\n", offset);
  1225. return len;
  1226. }
  1227. for (i = 0; i < count; i++) {
  1228. uint8_t reg = bios->data[offset + 4 + i * 3];
  1229. uint8_t mask = bios->data[offset + 5 + i * 3];
  1230. uint8_t data = bios->data[offset + 6 + i * 3];
  1231. union i2c_smbus_data val;
  1232. ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,
  1233. I2C_SMBUS_READ, reg,
  1234. I2C_SMBUS_BYTE_DATA, &val);
  1235. if (ret < 0) {
  1236. NV_ERROR(dev, "0x%04X: i2c rd fail: %d\n", offset, ret);
  1237. return len;
  1238. }
  1239. BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Value: 0x%02X, "
  1240. "Mask: 0x%02X, Data: 0x%02X\n",
  1241. offset, reg, val.byte, mask, data);
  1242. if (!bios->execute)
  1243. continue;
  1244. val.byte &= mask;
  1245. val.byte |= data;
  1246. ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,
  1247. I2C_SMBUS_WRITE, reg,
  1248. I2C_SMBUS_BYTE_DATA, &val);
  1249. if (ret < 0) {
  1250. NV_ERROR(dev, "0x%04X: i2c wr fail: %d\n", offset, ret);
  1251. return len;
  1252. }
  1253. }
  1254. return len;
  1255. }
  1256. static int
  1257. init_zm_i2c_byte(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1258. {
  1259. /*
  1260. * INIT_ZM_I2C_BYTE opcode: 0x4D ('M')
  1261. *
  1262. * offset (8 bit): opcode
  1263. * offset + 1 (8 bit): DCB I2C table entry index
  1264. * offset + 2 (8 bit): I2C slave address
  1265. * offset + 3 (8 bit): count
  1266. * offset + 4 (8 bit): I2C register 1
  1267. * offset + 5 (8 bit): data 1
  1268. * ...
  1269. *
  1270. * For each of "count" registers given by "I2C register n" on the device
  1271. * addressed by "I2C slave address" on the I2C bus given by
  1272. * "DCB I2C table entry index", set the register to "data n"
  1273. */
  1274. struct drm_device *dev = bios->dev;
  1275. uint8_t i2c_index = bios->data[offset + 1];
  1276. uint8_t i2c_address = bios->data[offset + 2] >> 1;
  1277. uint8_t count = bios->data[offset + 3];
  1278. struct nouveau_i2c_chan *chan;
  1279. int len = 4 + count * 2;
  1280. int ret, i;
  1281. if (!iexec->execute)
  1282. return len;
  1283. BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "
  1284. "Count: 0x%02X\n",
  1285. offset, i2c_index, i2c_address, count);
  1286. chan = init_i2c_device_find(dev, i2c_index);
  1287. if (!chan) {
  1288. NV_ERROR(dev, "0x%04X: i2c bus not found\n", offset);
  1289. return len;
  1290. }
  1291. for (i = 0; i < count; i++) {
  1292. uint8_t reg = bios->data[offset + 4 + i * 2];
  1293. union i2c_smbus_data val;
  1294. val.byte = bios->data[offset + 5 + i * 2];
  1295. BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Data: 0x%02X\n",
  1296. offset, reg, val.byte);
  1297. if (!bios->execute)
  1298. continue;
  1299. ret = i2c_smbus_xfer(&chan->adapter, i2c_address, 0,
  1300. I2C_SMBUS_WRITE, reg,
  1301. I2C_SMBUS_BYTE_DATA, &val);
  1302. if (ret < 0) {
  1303. NV_ERROR(dev, "0x%04X: i2c wr fail: %d\n", offset, ret);
  1304. return len;
  1305. }
  1306. }
  1307. return len;
  1308. }
  1309. static int
  1310. init_zm_i2c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1311. {
  1312. /*
  1313. * INIT_ZM_I2C opcode: 0x4E ('N')
  1314. *
  1315. * offset (8 bit): opcode
  1316. * offset + 1 (8 bit): DCB I2C table entry index
  1317. * offset + 2 (8 bit): I2C slave address
  1318. * offset + 3 (8 bit): count
  1319. * offset + 4 (8 bit): data 1
  1320. * ...
  1321. *
  1322. * Send "count" bytes ("data n") to the device addressed by "I2C slave
  1323. * address" on the I2C bus given by "DCB I2C table entry index"
  1324. */
  1325. struct drm_device *dev = bios->dev;
  1326. uint8_t i2c_index = bios->data[offset + 1];
  1327. uint8_t i2c_address = bios->data[offset + 2] >> 1;
  1328. uint8_t count = bios->data[offset + 3];
  1329. int len = 4 + count;
  1330. struct nouveau_i2c_chan *chan;
  1331. struct i2c_msg msg;
  1332. uint8_t data[256];
  1333. int ret, i;
  1334. if (!iexec->execute)
  1335. return len;
  1336. BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "
  1337. "Count: 0x%02X\n",
  1338. offset, i2c_index, i2c_address, count);
  1339. chan = init_i2c_device_find(dev, i2c_index);
  1340. if (!chan) {
  1341. NV_ERROR(dev, "0x%04X: i2c bus not found\n", offset);
  1342. return len;
  1343. }
  1344. for (i = 0; i < count; i++) {
  1345. data[i] = bios->data[offset + 4 + i];
  1346. BIOSLOG(bios, "0x%04X: Data: 0x%02X\n", offset, data[i]);
  1347. }
  1348. if (bios->execute) {
  1349. msg.addr = i2c_address;
  1350. msg.flags = 0;
  1351. msg.len = count;
  1352. msg.buf = data;
  1353. ret = i2c_transfer(&chan->adapter, &msg, 1);
  1354. if (ret != 1) {
  1355. NV_ERROR(dev, "0x%04X: i2c wr fail: %d\n", offset, ret);
  1356. return len;
  1357. }
  1358. }
  1359. return len;
  1360. }
  1361. static int
  1362. init_tmds(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1363. {
  1364. /*
  1365. * INIT_TMDS opcode: 0x4F ('O') (non-canon name)
  1366. *
  1367. * offset (8 bit): opcode
  1368. * offset + 1 (8 bit): magic lookup value
  1369. * offset + 2 (8 bit): TMDS address
  1370. * offset + 3 (8 bit): mask
  1371. * offset + 4 (8 bit): data
  1372. *
  1373. * Read the data reg for TMDS address "TMDS address", AND it with mask
  1374. * and OR it with data, then write it back
  1375. * "magic lookup value" determines which TMDS base address register is
  1376. * used -- see get_tmds_index_reg()
  1377. */
  1378. struct drm_device *dev = bios->dev;
  1379. uint8_t mlv = bios->data[offset + 1];
  1380. uint32_t tmdsaddr = bios->data[offset + 2];
  1381. uint8_t mask = bios->data[offset + 3];
  1382. uint8_t data = bios->data[offset + 4];
  1383. uint32_t reg, value;
  1384. if (!iexec->execute)
  1385. return 5;
  1386. BIOSLOG(bios, "0x%04X: MagicLookupValue: 0x%02X, TMDSAddr: 0x%02X, "
  1387. "Mask: 0x%02X, Data: 0x%02X\n",
  1388. offset, mlv, tmdsaddr, mask, data);
  1389. reg = get_tmds_index_reg(bios->dev, mlv);
  1390. if (!reg) {
  1391. NV_ERROR(dev, "0x%04X: no tmds_index_reg\n", offset);
  1392. return 5;
  1393. }
  1394. bios_wr32(bios, reg,
  1395. tmdsaddr | NV_PRAMDAC_FP_TMDS_CONTROL_WRITE_DISABLE);
  1396. value = (bios_rd32(bios, reg + 4) & mask) | data;
  1397. bios_wr32(bios, reg + 4, value);
  1398. bios_wr32(bios, reg, tmdsaddr);
  1399. return 5;
  1400. }
  1401. static int
  1402. init_zm_tmds_group(struct nvbios *bios, uint16_t offset,
  1403. struct init_exec *iexec)
  1404. {
  1405. /*
  1406. * INIT_ZM_TMDS_GROUP opcode: 0x50 ('P') (non-canon name)
  1407. *
  1408. * offset (8 bit): opcode
  1409. * offset + 1 (8 bit): magic lookup value
  1410. * offset + 2 (8 bit): count
  1411. * offset + 3 (8 bit): addr 1
  1412. * offset + 4 (8 bit): data 1
  1413. * ...
  1414. *
  1415. * For each of "count" TMDS address and data pairs write "data n" to
  1416. * "addr n". "magic lookup value" determines which TMDS base address
  1417. * register is used -- see get_tmds_index_reg()
  1418. */
  1419. struct drm_device *dev = bios->dev;
  1420. uint8_t mlv = bios->data[offset + 1];
  1421. uint8_t count = bios->data[offset + 2];
  1422. int len = 3 + count * 2;
  1423. uint32_t reg;
  1424. int i;
  1425. if (!iexec->execute)
  1426. return len;
  1427. BIOSLOG(bios, "0x%04X: MagicLookupValue: 0x%02X, Count: 0x%02X\n",
  1428. offset, mlv, count);
  1429. reg = get_tmds_index_reg(bios->dev, mlv);
  1430. if (!reg) {
  1431. NV_ERROR(dev, "0x%04X: no tmds_index_reg\n", offset);
  1432. return len;
  1433. }
  1434. for (i = 0; i < count; i++) {
  1435. uint8_t tmdsaddr = bios->data[offset + 3 + i * 2];
  1436. uint8_t tmdsdata = bios->data[offset + 4 + i * 2];
  1437. bios_wr32(bios, reg + 4, tmdsdata);
  1438. bios_wr32(bios, reg, tmdsaddr);
  1439. }
  1440. return len;
  1441. }
  1442. static int
  1443. init_cr_idx_adr_latch(struct nvbios *bios, uint16_t offset,
  1444. struct init_exec *iexec)
  1445. {
  1446. /*
  1447. * INIT_CR_INDEX_ADDRESS_LATCHED opcode: 0x51 ('Q')
  1448. *
  1449. * offset (8 bit): opcode
  1450. * offset + 1 (8 bit): CRTC index1
  1451. * offset + 2 (8 bit): CRTC index2
  1452. * offset + 3 (8 bit): baseaddr
  1453. * offset + 4 (8 bit): count
  1454. * offset + 5 (8 bit): data 1
  1455. * ...
  1456. *
  1457. * For each of "count" address and data pairs, write "baseaddr + n" to
  1458. * "CRTC index1" and "data n" to "CRTC index2"
  1459. * Once complete, restore initial value read from "CRTC index1"
  1460. */
  1461. uint8_t crtcindex1 = bios->data[offset + 1];
  1462. uint8_t crtcindex2 = bios->data[offset + 2];
  1463. uint8_t baseaddr = bios->data[offset + 3];
  1464. uint8_t count = bios->data[offset + 4];
  1465. int len = 5 + count;
  1466. uint8_t oldaddr, data;
  1467. int i;
  1468. if (!iexec->execute)
  1469. return len;
  1470. BIOSLOG(bios, "0x%04X: Index1: 0x%02X, Index2: 0x%02X, "
  1471. "BaseAddr: 0x%02X, Count: 0x%02X\n",
  1472. offset, crtcindex1, crtcindex2, baseaddr, count);
  1473. oldaddr = bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, crtcindex1);
  1474. for (i = 0; i < count; i++) {
  1475. bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex1,
  1476. baseaddr + i);
  1477. data = bios->data[offset + 5 + i];
  1478. bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex2, data);
  1479. }
  1480. bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex1, oldaddr);
  1481. return len;
  1482. }
  1483. static int
  1484. init_cr(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1485. {
  1486. /*
  1487. * INIT_CR opcode: 0x52 ('R')
  1488. *
  1489. * offset (8 bit): opcode
  1490. * offset + 1 (8 bit): CRTC index
  1491. * offset + 2 (8 bit): mask
  1492. * offset + 3 (8 bit): data
  1493. *
  1494. * Assign the value of at "CRTC index" ANDed with mask and ORed with
  1495. * data back to "CRTC index"
  1496. */
  1497. uint8_t crtcindex = bios->data[offset + 1];
  1498. uint8_t mask = bios->data[offset + 2];
  1499. uint8_t data = bios->data[offset + 3];
  1500. uint8_t value;
  1501. if (!iexec->execute)
  1502. return 4;
  1503. BIOSLOG(bios, "0x%04X: Index: 0x%02X, Mask: 0x%02X, Data: 0x%02X\n",
  1504. offset, crtcindex, mask, data);
  1505. value = bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, crtcindex) & mask;
  1506. value |= data;
  1507. bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex, value);
  1508. return 4;
  1509. }
  1510. static int
  1511. init_zm_cr(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1512. {
  1513. /*
  1514. * INIT_ZM_CR opcode: 0x53 ('S')
  1515. *
  1516. * offset (8 bit): opcode
  1517. * offset + 1 (8 bit): CRTC index
  1518. * offset + 2 (8 bit): value
  1519. *
  1520. * Assign "value" to CRTC register with index "CRTC index".
  1521. */
  1522. uint8_t crtcindex = ROM32(bios->data[offset + 1]);
  1523. uint8_t data = bios->data[offset + 2];
  1524. if (!iexec->execute)
  1525. return 3;
  1526. bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex, data);
  1527. return 3;
  1528. }
  1529. static int
  1530. init_zm_cr_group(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1531. {
  1532. /*
  1533. * INIT_ZM_CR_GROUP opcode: 0x54 ('T')
  1534. *
  1535. * offset (8 bit): opcode
  1536. * offset + 1 (8 bit): count
  1537. * offset + 2 (8 bit): CRTC index 1
  1538. * offset + 3 (8 bit): value 1
  1539. * ...
  1540. *
  1541. * For "count", assign "value n" to CRTC register with index
  1542. * "CRTC index n".
  1543. */
  1544. uint8_t count = bios->data[offset + 1];
  1545. int len = 2 + count * 2;
  1546. int i;
  1547. if (!iexec->execute)
  1548. return len;
  1549. for (i = 0; i < count; i++)
  1550. init_zm_cr(bios, offset + 2 + 2 * i - 1, iexec);
  1551. return len;
  1552. }
  1553. static int
  1554. init_condition_time(struct nvbios *bios, uint16_t offset,
  1555. struct init_exec *iexec)
  1556. {
  1557. /*
  1558. * INIT_CONDITION_TIME opcode: 0x56 ('V')
  1559. *
  1560. * offset (8 bit): opcode
  1561. * offset + 1 (8 bit): condition number
  1562. * offset + 2 (8 bit): retries / 50
  1563. *
  1564. * Check condition "condition number" in the condition table.
  1565. * Bios code then sleeps for 2ms if the condition is not met, and
  1566. * repeats up to "retries" times, but on one C51 this has proved
  1567. * insufficient. In mmiotraces the driver sleeps for 20ms, so we do
  1568. * this, and bail after "retries" times, or 2s, whichever is less.
  1569. * If still not met after retries, clear execution flag for this table.
  1570. */
  1571. uint8_t cond = bios->data[offset + 1];
  1572. uint16_t retries = bios->data[offset + 2] * 50;
  1573. unsigned cnt;
  1574. if (!iexec->execute)
  1575. return 3;
  1576. if (retries > 100)
  1577. retries = 100;
  1578. BIOSLOG(bios, "0x%04X: Condition: 0x%02X, Retries: 0x%02X\n",
  1579. offset, cond, retries);
  1580. if (!bios->execute) /* avoid 2s delays when "faking" execution */
  1581. retries = 1;
  1582. for (cnt = 0; cnt < retries; cnt++) {
  1583. if (bios_condition_met(bios, offset, cond)) {
  1584. BIOSLOG(bios, "0x%04X: Condition met, continuing\n",
  1585. offset);
  1586. break;
  1587. } else {
  1588. BIOSLOG(bios, "0x%04X: "
  1589. "Condition not met, sleeping for 20ms\n",
  1590. offset);
  1591. msleep(20);
  1592. }
  1593. }
  1594. if (!bios_condition_met(bios, offset, cond)) {
  1595. NV_WARN(bios->dev,
  1596. "0x%04X: Condition still not met after %dms, "
  1597. "skipping following opcodes\n", offset, 20 * retries);
  1598. iexec->execute = false;
  1599. }
  1600. return 3;
  1601. }
  1602. static int
  1603. init_zm_reg_sequence(struct nvbios *bios, uint16_t offset,
  1604. struct init_exec *iexec)
  1605. {
  1606. /*
  1607. * INIT_ZM_REG_SEQUENCE opcode: 0x58 ('X')
  1608. *
  1609. * offset (8 bit): opcode
  1610. * offset + 1 (32 bit): base register
  1611. * offset + 5 (8 bit): count
  1612. * offset + 6 (32 bit): value 1
  1613. * ...
  1614. *
  1615. * Starting at offset + 6 there are "count" 32 bit values.
  1616. * For "count" iterations set "base register" + 4 * current_iteration
  1617. * to "value current_iteration"
  1618. */
  1619. uint32_t basereg = ROM32(bios->data[offset + 1]);
  1620. uint32_t count = bios->data[offset + 5];
  1621. int len = 6 + count * 4;
  1622. int i;
  1623. if (!iexec->execute)
  1624. return len;
  1625. BIOSLOG(bios, "0x%04X: BaseReg: 0x%08X, Count: 0x%02X\n",
  1626. offset, basereg, count);
  1627. for (i = 0; i < count; i++) {
  1628. uint32_t reg = basereg + i * 4;
  1629. uint32_t data = ROM32(bios->data[offset + 6 + i * 4]);
  1630. bios_wr32(bios, reg, data);
  1631. }
  1632. return len;
  1633. }
  1634. static int
  1635. init_sub_direct(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1636. {
  1637. /*
  1638. * INIT_SUB_DIRECT opcode: 0x5B ('[')
  1639. *
  1640. * offset (8 bit): opcode
  1641. * offset + 1 (16 bit): subroutine offset (in bios)
  1642. *
  1643. * Calls a subroutine that will execute commands until INIT_DONE
  1644. * is found.
  1645. */
  1646. uint16_t sub_offset = ROM16(bios->data[offset + 1]);
  1647. if (!iexec->execute)
  1648. return 3;
  1649. BIOSLOG(bios, "0x%04X: Executing subroutine at 0x%04X\n",
  1650. offset, sub_offset);
  1651. parse_init_table(bios, sub_offset, iexec);
  1652. BIOSLOG(bios, "0x%04X: End of 0x%04X subroutine\n", offset, sub_offset);
  1653. return 3;
  1654. }
  1655. static int
  1656. init_copy_nv_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1657. {
  1658. /*
  1659. * INIT_COPY_NV_REG opcode: 0x5F ('_')
  1660. *
  1661. * offset (8 bit): opcode
  1662. * offset + 1 (32 bit): src reg
  1663. * offset + 5 (8 bit): shift
  1664. * offset + 6 (32 bit): src mask
  1665. * offset + 10 (32 bit): xor
  1666. * offset + 14 (32 bit): dst reg
  1667. * offset + 18 (32 bit): dst mask
  1668. *
  1669. * Shift REGVAL("src reg") right by (signed) "shift", AND result with
  1670. * "src mask", then XOR with "xor". Write this OR'd with
  1671. * (REGVAL("dst reg") AND'd with "dst mask") to "dst reg"
  1672. */
  1673. uint32_t srcreg = *((uint32_t *)(&bios->data[offset + 1]));
  1674. uint8_t shift = bios->data[offset + 5];
  1675. uint32_t srcmask = *((uint32_t *)(&bios->data[offset + 6]));
  1676. uint32_t xor = *((uint32_t *)(&bios->data[offset + 10]));
  1677. uint32_t dstreg = *((uint32_t *)(&bios->data[offset + 14]));
  1678. uint32_t dstmask = *((uint32_t *)(&bios->data[offset + 18]));
  1679. uint32_t srcvalue, dstvalue;
  1680. if (!iexec->execute)
  1681. return 22;
  1682. BIOSLOG(bios, "0x%04X: SrcReg: 0x%08X, Shift: 0x%02X, SrcMask: 0x%08X, "
  1683. "Xor: 0x%08X, DstReg: 0x%08X, DstMask: 0x%08X\n",
  1684. offset, srcreg, shift, srcmask, xor, dstreg, dstmask);
  1685. srcvalue = bios_rd32(bios, srcreg);
  1686. if (shift < 0x80)
  1687. srcvalue >>= shift;
  1688. else
  1689. srcvalue <<= (0x100 - shift);
  1690. srcvalue = (srcvalue & srcmask) ^ xor;
  1691. dstvalue = bios_rd32(bios, dstreg) & dstmask;
  1692. bios_wr32(bios, dstreg, dstvalue | srcvalue);
  1693. return 22;
  1694. }
  1695. static int
  1696. init_zm_index_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1697. {
  1698. /*
  1699. * INIT_ZM_INDEX_IO opcode: 0x62 ('b')
  1700. *
  1701. * offset (8 bit): opcode
  1702. * offset + 1 (16 bit): CRTC port
  1703. * offset + 3 (8 bit): CRTC index
  1704. * offset + 4 (8 bit): data
  1705. *
  1706. * Write "data" to index "CRTC index" of "CRTC port"
  1707. */
  1708. uint16_t crtcport = ROM16(bios->data[offset + 1]);
  1709. uint8_t crtcindex = bios->data[offset + 3];
  1710. uint8_t data = bios->data[offset + 4];
  1711. if (!iexec->execute)
  1712. return 5;
  1713. bios_idxprt_wr(bios, crtcport, crtcindex, data);
  1714. return 5;
  1715. }
  1716. static int
  1717. init_compute_mem(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1718. {
  1719. /*
  1720. * INIT_COMPUTE_MEM opcode: 0x63 ('c')
  1721. *
  1722. * offset (8 bit): opcode
  1723. *
  1724. * This opcode is meant to set NV_PFB_CFG0 (0x100200) appropriately so
  1725. * that the hardware can correctly calculate how much VRAM it has
  1726. * (and subsequently report that value in NV_PFB_CSTATUS (0x10020C))
  1727. *
  1728. * The implementation of this opcode in general consists of two parts:
  1729. * 1) determination of the memory bus width
  1730. * 2) determination of how many of the card's RAM pads have ICs attached
  1731. *
  1732. * 1) is done by a cunning combination of writes to offsets 0x1c and
  1733. * 0x3c in the framebuffer, and seeing whether the written values are
  1734. * read back correctly. This then affects bits 4-7 of NV_PFB_CFG0
  1735. *
  1736. * 2) is done by a cunning combination of writes to an offset slightly
  1737. * less than the maximum memory reported by NV_PFB_CSTATUS, then seeing
  1738. * if the test pattern can be read back. This then affects bits 12-15 of
  1739. * NV_PFB_CFG0
  1740. *
  1741. * In this context a "cunning combination" may include multiple reads
  1742. * and writes to varying locations, often alternating the test pattern
  1743. * and 0, doubtless to make sure buffers are filled, residual charges
  1744. * on tracks are removed etc.
  1745. *
  1746. * Unfortunately, the "cunning combination"s mentioned above, and the
  1747. * changes to the bits in NV_PFB_CFG0 differ with nearly every bios
  1748. * trace I have.
  1749. *
  1750. * Therefore, we cheat and assume the value of NV_PFB_CFG0 with which
  1751. * we started was correct, and use that instead
  1752. */
  1753. /* no iexec->execute check by design */
  1754. /*
  1755. * This appears to be a NOP on G8x chipsets, both io logs of the VBIOS
  1756. * and kmmio traces of the binary driver POSTing the card show nothing
  1757. * being done for this opcode. why is it still listed in the table?!
  1758. */
  1759. struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
  1760. if (dev_priv->card_type >= NV_40)
  1761. return 1;
  1762. /*
  1763. * On every card I've seen, this step gets done for us earlier in
  1764. * the init scripts
  1765. uint8_t crdata = bios_idxprt_rd(dev, NV_VIO_SRX, 0x01);
  1766. bios_idxprt_wr(dev, NV_VIO_SRX, 0x01, crdata | 0x20);
  1767. */
  1768. /*
  1769. * This also has probably been done in the scripts, but an mmio trace of
  1770. * s3 resume shows nvidia doing it anyway (unlike the NV_VIO_SRX write)
  1771. */
  1772. bios_wr32(bios, NV_PFB_REFCTRL, NV_PFB_REFCTRL_VALID_1);
  1773. /* write back the saved configuration value */
  1774. bios_wr32(bios, NV_PFB_CFG0, bios->state.saved_nv_pfb_cfg0);
  1775. return 1;
  1776. }
  1777. static int
  1778. init_reset(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1779. {
  1780. /*
  1781. * INIT_RESET opcode: 0x65 ('e')
  1782. *
  1783. * offset (8 bit): opcode
  1784. * offset + 1 (32 bit): register
  1785. * offset + 5 (32 bit): value1
  1786. * offset + 9 (32 bit): value2
  1787. *
  1788. * Assign "value1" to "register", then assign "value2" to "register"
  1789. */
  1790. uint32_t reg = ROM32(bios->data[offset + 1]);
  1791. uint32_t value1 = ROM32(bios->data[offset + 5]);
  1792. uint32_t value2 = ROM32(bios->data[offset + 9]);
  1793. uint32_t pci_nv_19, pci_nv_20;
  1794. /* no iexec->execute check by design */
  1795. pci_nv_19 = bios_rd32(bios, NV_PBUS_PCI_NV_19);
  1796. bios_wr32(bios, NV_PBUS_PCI_NV_19, pci_nv_19 & ~0xf00);
  1797. bios_wr32(bios, reg, value1);
  1798. udelay(10);
  1799. bios_wr32(bios, reg, value2);
  1800. bios_wr32(bios, NV_PBUS_PCI_NV_19, pci_nv_19);
  1801. pci_nv_20 = bios_rd32(bios, NV_PBUS_PCI_NV_20);
  1802. pci_nv_20 &= ~NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED; /* 0xfffffffe */
  1803. bios_wr32(bios, NV_PBUS_PCI_NV_20, pci_nv_20);
  1804. return 13;
  1805. }
  1806. static int
  1807. init_configure_mem(struct nvbios *bios, uint16_t offset,
  1808. struct init_exec *iexec)
  1809. {
  1810. /*
  1811. * INIT_CONFIGURE_MEM opcode: 0x66 ('f')
  1812. *
  1813. * offset (8 bit): opcode
  1814. *
  1815. * Equivalent to INIT_DONE on bios version 3 or greater.
  1816. * For early bios versions, sets up the memory registers, using values
  1817. * taken from the memory init table
  1818. */
  1819. /* no iexec->execute check by design */
  1820. uint16_t meminitoffs = bios->legacy.mem_init_tbl_ptr + MEM_INIT_SIZE * (bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_SCRATCH4__INDEX) >> 4);
  1821. uint16_t seqtbloffs = bios->legacy.sdr_seq_tbl_ptr, meminitdata = meminitoffs + 6;
  1822. uint32_t reg, data;
  1823. if (bios->major_version > 2)
  1824. return 0;
  1825. bios_idxprt_wr(bios, NV_VIO_SRX, NV_VIO_SR_CLOCK_INDEX, bios_idxprt_rd(
  1826. bios, NV_VIO_SRX, NV_VIO_SR_CLOCK_INDEX) | 0x20);
  1827. if (bios->data[meminitoffs] & 1)
  1828. seqtbloffs = bios->legacy.ddr_seq_tbl_ptr;
  1829. for (reg = ROM32(bios->data[seqtbloffs]);
  1830. reg != 0xffffffff;
  1831. reg = ROM32(bios->data[seqtbloffs += 4])) {
  1832. switch (reg) {
  1833. case NV_PFB_PRE:
  1834. data = NV_PFB_PRE_CMD_PRECHARGE;
  1835. break;
  1836. case NV_PFB_PAD:
  1837. data = NV_PFB_PAD_CKE_NORMAL;
  1838. break;
  1839. case NV_PFB_REF:
  1840. data = NV_PFB_REF_CMD_REFRESH;
  1841. break;
  1842. default:
  1843. data = ROM32(bios->data[meminitdata]);
  1844. meminitdata += 4;
  1845. if (data == 0xffffffff)
  1846. continue;
  1847. }
  1848. bios_wr32(bios, reg, data);
  1849. }
  1850. return 1;
  1851. }
  1852. static int
  1853. init_configure_clk(struct nvbios *bios, uint16_t offset,
  1854. struct init_exec *iexec)
  1855. {
  1856. /*
  1857. * INIT_CONFIGURE_CLK opcode: 0x67 ('g')
  1858. *
  1859. * offset (8 bit): opcode
  1860. *
  1861. * Equivalent to INIT_DONE on bios version 3 or greater.
  1862. * For early bios versions, sets up the NVClk and MClk PLLs, using
  1863. * values taken from the memory init table
  1864. */
  1865. /* no iexec->execute check by design */
  1866. uint16_t meminitoffs = bios->legacy.mem_init_tbl_ptr + MEM_INIT_SIZE * (bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_SCRATCH4__INDEX) >> 4);
  1867. int clock;
  1868. if (bios->major_version > 2)
  1869. return 0;
  1870. clock = ROM16(bios->data[meminitoffs + 4]) * 10;
  1871. setPLL(bios, NV_PRAMDAC_NVPLL_COEFF, clock);
  1872. clock = ROM16(bios->data[meminitoffs + 2]) * 10;
  1873. if (bios->data[meminitoffs] & 1) /* DDR */
  1874. clock *= 2;
  1875. setPLL(bios, NV_PRAMDAC_MPLL_COEFF, clock);
  1876. return 1;
  1877. }
  1878. static int
  1879. init_configure_preinit(struct nvbios *bios, uint16_t offset,
  1880. struct init_exec *iexec)
  1881. {
  1882. /*
  1883. * INIT_CONFIGURE_PREINIT opcode: 0x68 ('h')
  1884. *
  1885. * offset (8 bit): opcode
  1886. *
  1887. * Equivalent to INIT_DONE on bios version 3 or greater.
  1888. * For early bios versions, does early init, loading ram and crystal
  1889. * configuration from straps into CR3C
  1890. */
  1891. /* no iexec->execute check by design */
  1892. uint32_t straps = bios_rd32(bios, NV_PEXTDEV_BOOT_0);
  1893. uint8_t cr3c = ((straps << 2) & 0xf0) | (straps & (1 << 6));
  1894. if (bios->major_version > 2)
  1895. return 0;
  1896. bios_idxprt_wr(bios, NV_CIO_CRX__COLOR,
  1897. NV_CIO_CRE_SCRATCH4__INDEX, cr3c);
  1898. return 1;
  1899. }
  1900. static int
  1901. init_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1902. {
  1903. /*
  1904. * INIT_IO opcode: 0x69 ('i')
  1905. *
  1906. * offset (8 bit): opcode
  1907. * offset + 1 (16 bit): CRTC port
  1908. * offset + 3 (8 bit): mask
  1909. * offset + 4 (8 bit): data
  1910. *
  1911. * Assign ((IOVAL("crtc port") & "mask") | "data") to "crtc port"
  1912. */
  1913. struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
  1914. uint16_t crtcport = ROM16(bios->data[offset + 1]);
  1915. uint8_t mask = bios->data[offset + 3];
  1916. uint8_t data = bios->data[offset + 4];
  1917. if (!iexec->execute)
  1918. return 5;
  1919. BIOSLOG(bios, "0x%04X: Port: 0x%04X, Mask: 0x%02X, Data: 0x%02X\n",
  1920. offset, crtcport, mask, data);
  1921. /*
  1922. * I have no idea what this does, but NVIDIA do this magic sequence
  1923. * in the places where this INIT_IO happens..
  1924. */
  1925. if (dev_priv->card_type >= NV_50 && crtcport == 0x3c3 && data == 1) {
  1926. int i;
  1927. bios_wr32(bios, 0x614100, (bios_rd32(
  1928. bios, 0x614100) & 0x0fffffff) | 0x00800000);
  1929. bios_wr32(bios, 0x00e18c, bios_rd32(
  1930. bios, 0x00e18c) | 0x00020000);
  1931. bios_wr32(bios, 0x614900, (bios_rd32(
  1932. bios, 0x614900) & 0x0fffffff) | 0x00800000);
  1933. bios_wr32(bios, 0x000200, bios_rd32(
  1934. bios, 0x000200) & ~0x40000000);
  1935. mdelay(10);
  1936. bios_wr32(bios, 0x00e18c, bios_rd32(
  1937. bios, 0x00e18c) & ~0x00020000);
  1938. bios_wr32(bios, 0x000200, bios_rd32(
  1939. bios, 0x000200) | 0x40000000);
  1940. bios_wr32(bios, 0x614100, 0x00800018);
  1941. bios_wr32(bios, 0x614900, 0x00800018);
  1942. mdelay(10);
  1943. bios_wr32(bios, 0x614100, 0x10000018);
  1944. bios_wr32(bios, 0x614900, 0x10000018);
  1945. for (i = 0; i < 3; i++)
  1946. bios_wr32(bios, 0x614280 + (i*0x800), bios_rd32(
  1947. bios, 0x614280 + (i*0x800)) & 0xf0f0f0f0);
  1948. for (i = 0; i < 2; i++)
  1949. bios_wr32(bios, 0x614300 + (i*0x800), bios_rd32(
  1950. bios, 0x614300 + (i*0x800)) & 0xfffff0f0);
  1951. for (i = 0; i < 3; i++)
  1952. bios_wr32(bios, 0x614380 + (i*0x800), bios_rd32(
  1953. bios, 0x614380 + (i*0x800)) & 0xfffff0f0);
  1954. for (i = 0; i < 2; i++)
  1955. bios_wr32(bios, 0x614200 + (i*0x800), bios_rd32(
  1956. bios, 0x614200 + (i*0x800)) & 0xfffffff0);
  1957. for (i = 0; i < 2; i++)
  1958. bios_wr32(bios, 0x614108 + (i*0x800), bios_rd32(
  1959. bios, 0x614108 + (i*0x800)) & 0x0fffffff);
  1960. return 5;
  1961. }
  1962. bios_port_wr(bios, crtcport, (bios_port_rd(bios, crtcport) & mask) |
  1963. data);
  1964. return 5;
  1965. }
  1966. static int
  1967. init_sub(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  1968. {
  1969. /*
  1970. * INIT_SUB opcode: 0x6B ('k')
  1971. *
  1972. * offset (8 bit): opcode
  1973. * offset + 1 (8 bit): script number
  1974. *
  1975. * Execute script number "script number", as a subroutine
  1976. */
  1977. uint8_t sub = bios->data[offset + 1];
  1978. if (!iexec->execute)
  1979. return 2;
  1980. BIOSLOG(bios, "0x%04X: Calling script %d\n", offset, sub);
  1981. parse_init_table(bios,
  1982. ROM16(bios->data[bios->init_script_tbls_ptr + sub * 2]),
  1983. iexec);
  1984. BIOSLOG(bios, "0x%04X: End of script %d\n", offset, sub);
  1985. return 2;
  1986. }
  1987. static int
  1988. init_ram_condition(struct nvbios *bios, uint16_t offset,
  1989. struct init_exec *iexec)
  1990. {
  1991. /*
  1992. * INIT_RAM_CONDITION opcode: 0x6D ('m')
  1993. *
  1994. * offset (8 bit): opcode
  1995. * offset + 1 (8 bit): mask
  1996. * offset + 2 (8 bit): cmpval
  1997. *
  1998. * Test if (NV_PFB_BOOT_0 & "mask") equals "cmpval".
  1999. * If condition not met skip subsequent opcodes until condition is
  2000. * inverted (INIT_NOT), or we hit INIT_RESUME
  2001. */
  2002. uint8_t mask = bios->data[offset + 1];
  2003. uint8_t cmpval = bios->data[offset + 2];
  2004. uint8_t data;
  2005. if (!iexec->execute)
  2006. return 3;
  2007. data = bios_rd32(bios, NV_PFB_BOOT_0) & mask;
  2008. BIOSLOG(bios, "0x%04X: Checking if 0x%08X equals 0x%08X\n",
  2009. offset, data, cmpval);
  2010. if (data == cmpval)
  2011. BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
  2012. else {
  2013. BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
  2014. iexec->execute = false;
  2015. }
  2016. return 3;
  2017. }
  2018. static int
  2019. init_nv_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2020. {
  2021. /*
  2022. * INIT_NV_REG opcode: 0x6E ('n')
  2023. *
  2024. * offset (8 bit): opcode
  2025. * offset + 1 (32 bit): register
  2026. * offset + 5 (32 bit): mask
  2027. * offset + 9 (32 bit): data
  2028. *
  2029. * Assign ((REGVAL("register") & "mask") | "data") to "register"
  2030. */
  2031. uint32_t reg = ROM32(bios->data[offset + 1]);
  2032. uint32_t mask = ROM32(bios->data[offset + 5]);
  2033. uint32_t data = ROM32(bios->data[offset + 9]);
  2034. if (!iexec->execute)
  2035. return 13;
  2036. BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Mask: 0x%08X, Data: 0x%08X\n",
  2037. offset, reg, mask, data);
  2038. bios_wr32(bios, reg, (bios_rd32(bios, reg) & mask) | data);
  2039. return 13;
  2040. }
  2041. static int
  2042. init_macro(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2043. {
  2044. /*
  2045. * INIT_MACRO opcode: 0x6F ('o')
  2046. *
  2047. * offset (8 bit): opcode
  2048. * offset + 1 (8 bit): macro number
  2049. *
  2050. * Look up macro index "macro number" in the macro index table.
  2051. * The macro index table entry has 1 byte for the index in the macro
  2052. * table, and 1 byte for the number of times to repeat the macro.
  2053. * The macro table entry has 4 bytes for the register address and
  2054. * 4 bytes for the value to write to that register
  2055. */
  2056. uint8_t macro_index_tbl_idx = bios->data[offset + 1];
  2057. uint16_t tmp = bios->macro_index_tbl_ptr + (macro_index_tbl_idx * MACRO_INDEX_SIZE);
  2058. uint8_t macro_tbl_idx = bios->data[tmp];
  2059. uint8_t count = bios->data[tmp + 1];
  2060. uint32_t reg, data;
  2061. int i;
  2062. if (!iexec->execute)
  2063. return 2;
  2064. BIOSLOG(bios, "0x%04X: Macro: 0x%02X, MacroTableIndex: 0x%02X, "
  2065. "Count: 0x%02X\n",
  2066. offset, macro_index_tbl_idx, macro_tbl_idx, count);
  2067. for (i = 0; i < count; i++) {
  2068. uint16_t macroentryptr = bios->macro_tbl_ptr + (macro_tbl_idx + i) * MACRO_SIZE;
  2069. reg = ROM32(bios->data[macroentryptr]);
  2070. data = ROM32(bios->data[macroentryptr + 4]);
  2071. bios_wr32(bios, reg, data);
  2072. }
  2073. return 2;
  2074. }
  2075. static int
  2076. init_done(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2077. {
  2078. /*
  2079. * INIT_DONE opcode: 0x71 ('q')
  2080. *
  2081. * offset (8 bit): opcode
  2082. *
  2083. * End the current script
  2084. */
  2085. /* mild retval abuse to stop parsing this table */
  2086. return 0;
  2087. }
  2088. static int
  2089. init_resume(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2090. {
  2091. /*
  2092. * INIT_RESUME opcode: 0x72 ('r')
  2093. *
  2094. * offset (8 bit): opcode
  2095. *
  2096. * End the current execute / no-execute condition
  2097. */
  2098. if (iexec->execute)
  2099. return 1;
  2100. iexec->execute = true;
  2101. BIOSLOG(bios, "0x%04X: ---- Executing following commands ----\n", offset);
  2102. return 1;
  2103. }
  2104. static int
  2105. init_time(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2106. {
  2107. /*
  2108. * INIT_TIME opcode: 0x74 ('t')
  2109. *
  2110. * offset (8 bit): opcode
  2111. * offset + 1 (16 bit): time
  2112. *
  2113. * Sleep for "time" microseconds.
  2114. */
  2115. unsigned time = ROM16(bios->data[offset + 1]);
  2116. if (!iexec->execute)
  2117. return 3;
  2118. BIOSLOG(bios, "0x%04X: Sleeping for 0x%04X microseconds\n",
  2119. offset, time);
  2120. if (time < 1000)
  2121. udelay(time);
  2122. else
  2123. msleep((time + 900) / 1000);
  2124. return 3;
  2125. }
  2126. static int
  2127. init_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2128. {
  2129. /*
  2130. * INIT_CONDITION opcode: 0x75 ('u')
  2131. *
  2132. * offset (8 bit): opcode
  2133. * offset + 1 (8 bit): condition number
  2134. *
  2135. * Check condition "condition number" in the condition table.
  2136. * If condition not met skip subsequent opcodes until condition is
  2137. * inverted (INIT_NOT), or we hit INIT_RESUME
  2138. */
  2139. uint8_t cond = bios->data[offset + 1];
  2140. if (!iexec->execute)
  2141. return 2;
  2142. BIOSLOG(bios, "0x%04X: Condition: 0x%02X\n", offset, cond);
  2143. if (bios_condition_met(bios, offset, cond))
  2144. BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
  2145. else {
  2146. BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
  2147. iexec->execute = false;
  2148. }
  2149. return 2;
  2150. }
  2151. static int
  2152. init_io_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2153. {
  2154. /*
  2155. * INIT_IO_CONDITION opcode: 0x76
  2156. *
  2157. * offset (8 bit): opcode
  2158. * offset + 1 (8 bit): condition number
  2159. *
  2160. * Check condition "condition number" in the io condition table.
  2161. * If condition not met skip subsequent opcodes until condition is
  2162. * inverted (INIT_NOT), or we hit INIT_RESUME
  2163. */
  2164. uint8_t cond = bios->data[offset + 1];
  2165. if (!iexec->execute)
  2166. return 2;
  2167. BIOSLOG(bios, "0x%04X: IO condition: 0x%02X\n", offset, cond);
  2168. if (io_condition_met(bios, offset, cond))
  2169. BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
  2170. else {
  2171. BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
  2172. iexec->execute = false;
  2173. }
  2174. return 2;
  2175. }
  2176. static int
  2177. init_index_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2178. {
  2179. /*
  2180. * INIT_INDEX_IO opcode: 0x78 ('x')
  2181. *
  2182. * offset (8 bit): opcode
  2183. * offset + 1 (16 bit): CRTC port
  2184. * offset + 3 (8 bit): CRTC index
  2185. * offset + 4 (8 bit): mask
  2186. * offset + 5 (8 bit): data
  2187. *
  2188. * Read value at index "CRTC index" on "CRTC port", AND with "mask",
  2189. * OR with "data", write-back
  2190. */
  2191. uint16_t crtcport = ROM16(bios->data[offset + 1]);
  2192. uint8_t crtcindex = bios->data[offset + 3];
  2193. uint8_t mask = bios->data[offset + 4];
  2194. uint8_t data = bios->data[offset + 5];
  2195. uint8_t value;
  2196. if (!iexec->execute)
  2197. return 6;
  2198. BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
  2199. "Data: 0x%02X\n",
  2200. offset, crtcport, crtcindex, mask, data);
  2201. value = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) | data;
  2202. bios_idxprt_wr(bios, crtcport, crtcindex, value);
  2203. return 6;
  2204. }
  2205. static int
  2206. init_pll(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2207. {
  2208. /*
  2209. * INIT_PLL opcode: 0x79 ('y')
  2210. *
  2211. * offset (8 bit): opcode
  2212. * offset + 1 (32 bit): register
  2213. * offset + 5 (16 bit): freq
  2214. *
  2215. * Set PLL register "register" to coefficients for frequency (10kHz)
  2216. * "freq"
  2217. */
  2218. uint32_t reg = ROM32(bios->data[offset + 1]);
  2219. uint16_t freq = ROM16(bios->data[offset + 5]);
  2220. if (!iexec->execute)
  2221. return 7;
  2222. BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Freq: %d0kHz\n", offset, reg, freq);
  2223. setPLL(bios, reg, freq * 10);
  2224. return 7;
  2225. }
  2226. static int
  2227. init_zm_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2228. {
  2229. /*
  2230. * INIT_ZM_REG opcode: 0x7A ('z')
  2231. *
  2232. * offset (8 bit): opcode
  2233. * offset + 1 (32 bit): register
  2234. * offset + 5 (32 bit): value
  2235. *
  2236. * Assign "value" to "register"
  2237. */
  2238. uint32_t reg = ROM32(bios->data[offset + 1]);
  2239. uint32_t value = ROM32(bios->data[offset + 5]);
  2240. if (!iexec->execute)
  2241. return 9;
  2242. if (reg == 0x000200)
  2243. value |= 1;
  2244. bios_wr32(bios, reg, value);
  2245. return 9;
  2246. }
  2247. static int
  2248. init_ram_restrict_pll(struct nvbios *bios, uint16_t offset,
  2249. struct init_exec *iexec)
  2250. {
  2251. /*
  2252. * INIT_RAM_RESTRICT_PLL opcode: 0x87 ('')
  2253. *
  2254. * offset (8 bit): opcode
  2255. * offset + 1 (8 bit): PLL type
  2256. * offset + 2 (32 bit): frequency 0
  2257. *
  2258. * Uses the RAMCFG strap of PEXTDEV_BOOT as an index into the table at
  2259. * ram_restrict_table_ptr. The value read from there is used to select
  2260. * a frequency from the table starting at 'frequency 0' to be
  2261. * programmed into the PLL corresponding to 'type'.
  2262. *
  2263. * The PLL limits table on cards using this opcode has a mapping of
  2264. * 'type' to the relevant registers.
  2265. */
  2266. struct drm_device *dev = bios->dev;
  2267. uint32_t strap = (bios_rd32(bios, NV_PEXTDEV_BOOT_0) & 0x0000003c) >> 2;
  2268. uint8_t index = bios->data[bios->ram_restrict_tbl_ptr + strap];
  2269. uint8_t type = bios->data[offset + 1];
  2270. uint32_t freq = ROM32(bios->data[offset + 2 + (index * 4)]);
  2271. uint8_t *pll_limits = &bios->data[bios->pll_limit_tbl_ptr], *entry;
  2272. int len = 2 + bios->ram_restrict_group_count * 4;
  2273. int i;
  2274. if (!iexec->execute)
  2275. return len;
  2276. if (!bios->pll_limit_tbl_ptr || (pll_limits[0] & 0xf0) != 0x30) {
  2277. NV_ERROR(dev, "PLL limits table not version 3.x\n");
  2278. return len; /* deliberate, allow default clocks to remain */
  2279. }
  2280. entry = pll_limits + pll_limits[1];
  2281. for (i = 0; i < pll_limits[3]; i++, entry += pll_limits[2]) {
  2282. if (entry[0] == type) {
  2283. uint32_t reg = ROM32(entry[3]);
  2284. BIOSLOG(bios, "0x%04X: "
  2285. "Type %02x Reg 0x%08x Freq %dKHz\n",
  2286. offset, type, reg, freq);
  2287. setPLL(bios, reg, freq);
  2288. return len;
  2289. }
  2290. }
  2291. NV_ERROR(dev, "PLL type 0x%02x not found in PLL limits table", type);
  2292. return len;
  2293. }
  2294. static int
  2295. init_8c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2296. {
  2297. /*
  2298. * INIT_8C opcode: 0x8C ('')
  2299. *
  2300. * NOP so far....
  2301. *
  2302. */
  2303. return 1;
  2304. }
  2305. static int
  2306. init_8d(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2307. {
  2308. /*
  2309. * INIT_8D opcode: 0x8D ('')
  2310. *
  2311. * NOP so far....
  2312. *
  2313. */
  2314. return 1;
  2315. }
  2316. static int
  2317. init_gpio(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2318. {
  2319. /*
  2320. * INIT_GPIO opcode: 0x8E ('')
  2321. *
  2322. * offset (8 bit): opcode
  2323. *
  2324. * Loop over all entries in the DCB GPIO table, and initialise
  2325. * each GPIO according to various values listed in each entry
  2326. */
  2327. struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
  2328. const uint32_t nv50_gpio_ctl[2] = { 0xe100, 0xe28c };
  2329. int i;
  2330. if (dev_priv->card_type != NV_50) {
  2331. NV_ERROR(bios->dev, "INIT_GPIO on unsupported chipset\n");
  2332. return 1;
  2333. }
  2334. if (!iexec->execute)
  2335. return 1;
  2336. for (i = 0; i < bios->dcb.gpio.entries; i++) {
  2337. struct dcb_gpio_entry *gpio = &bios->dcb.gpio.entry[i];
  2338. uint32_t r, s, v;
  2339. BIOSLOG(bios, "0x%04X: Entry: 0x%08X\n", offset, gpio->entry);
  2340. BIOSLOG(bios, "0x%04X: set gpio 0x%02x, state %d\n",
  2341. offset, gpio->tag, gpio->state_default);
  2342. if (bios->execute)
  2343. nv50_gpio_set(bios->dev, gpio->tag, gpio->state_default);
  2344. /* The NVIDIA binary driver doesn't appear to actually do
  2345. * any of this, my VBIOS does however.
  2346. */
  2347. /* Not a clue, needs de-magicing */
  2348. r = nv50_gpio_ctl[gpio->line >> 4];
  2349. s = (gpio->line & 0x0f);
  2350. v = bios_rd32(bios, r) & ~(0x00010001 << s);
  2351. switch ((gpio->entry & 0x06000000) >> 25) {
  2352. case 1:
  2353. v |= (0x00000001 << s);
  2354. break;
  2355. case 2:
  2356. v |= (0x00010000 << s);
  2357. break;
  2358. default:
  2359. break;
  2360. }
  2361. bios_wr32(bios, r, v);
  2362. }
  2363. return 1;
  2364. }
  2365. static int
  2366. init_ram_restrict_zm_reg_group(struct nvbios *bios, uint16_t offset,
  2367. struct init_exec *iexec)
  2368. {
  2369. /*
  2370. * INIT_RAM_RESTRICT_ZM_REG_GROUP opcode: 0x8F ('')
  2371. *
  2372. * offset (8 bit): opcode
  2373. * offset + 1 (32 bit): reg
  2374. * offset + 5 (8 bit): regincrement
  2375. * offset + 6 (8 bit): count
  2376. * offset + 7 (32 bit): value 1,1
  2377. * ...
  2378. *
  2379. * Use the RAMCFG strap of PEXTDEV_BOOT as an index into the table at
  2380. * ram_restrict_table_ptr. The value read from here is 'n', and
  2381. * "value 1,n" gets written to "reg". This repeats "count" times and on
  2382. * each iteration 'm', "reg" increases by "regincrement" and
  2383. * "value m,n" is used. The extent of n is limited by a number read
  2384. * from the 'M' BIT table, herein called "blocklen"
  2385. */
  2386. uint32_t reg = ROM32(bios->data[offset + 1]);
  2387. uint8_t regincrement = bios->data[offset + 5];
  2388. uint8_t count = bios->data[offset + 6];
  2389. uint32_t strap_ramcfg, data;
  2390. /* previously set by 'M' BIT table */
  2391. uint16_t blocklen = bios->ram_restrict_group_count * 4;
  2392. int len = 7 + count * blocklen;
  2393. uint8_t index;
  2394. int i;
  2395. /* critical! to know the length of the opcode */;
  2396. if (!blocklen) {
  2397. NV_ERROR(bios->dev,
  2398. "0x%04X: Zero block length - has the M table "
  2399. "been parsed?\n", offset);
  2400. return -EINVAL;
  2401. }
  2402. if (!iexec->execute)
  2403. return len;
  2404. strap_ramcfg = (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 2) & 0xf;
  2405. index = bios->data[bios->ram_restrict_tbl_ptr + strap_ramcfg];
  2406. BIOSLOG(bios, "0x%04X: Reg: 0x%08X, RegIncrement: 0x%02X, "
  2407. "Count: 0x%02X, StrapRamCfg: 0x%02X, Index: 0x%02X\n",
  2408. offset, reg, regincrement, count, strap_ramcfg, index);
  2409. for (i = 0; i < count; i++) {
  2410. data = ROM32(bios->data[offset + 7 + index * 4 + blocklen * i]);
  2411. bios_wr32(bios, reg, data);
  2412. reg += regincrement;
  2413. }
  2414. return len;
  2415. }
  2416. static int
  2417. init_copy_zm_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2418. {
  2419. /*
  2420. * INIT_COPY_ZM_REG opcode: 0x90 ('')
  2421. *
  2422. * offset (8 bit): opcode
  2423. * offset + 1 (32 bit): src reg
  2424. * offset + 5 (32 bit): dst reg
  2425. *
  2426. * Put contents of "src reg" into "dst reg"
  2427. */
  2428. uint32_t srcreg = ROM32(bios->data[offset + 1]);
  2429. uint32_t dstreg = ROM32(bios->data[offset + 5]);
  2430. if (!iexec->execute)
  2431. return 9;
  2432. bios_wr32(bios, dstreg, bios_rd32(bios, srcreg));
  2433. return 9;
  2434. }
  2435. static int
  2436. init_zm_reg_group_addr_latched(struct nvbios *bios, uint16_t offset,
  2437. struct init_exec *iexec)
  2438. {
  2439. /*
  2440. * INIT_ZM_REG_GROUP_ADDRESS_LATCHED opcode: 0x91 ('')
  2441. *
  2442. * offset (8 bit): opcode
  2443. * offset + 1 (32 bit): dst reg
  2444. * offset + 5 (8 bit): count
  2445. * offset + 6 (32 bit): data 1
  2446. * ...
  2447. *
  2448. * For each of "count" values write "data n" to "dst reg"
  2449. */
  2450. uint32_t reg = ROM32(bios->data[offset + 1]);
  2451. uint8_t count = bios->data[offset + 5];
  2452. int len = 6 + count * 4;
  2453. int i;
  2454. if (!iexec->execute)
  2455. return len;
  2456. for (i = 0; i < count; i++) {
  2457. uint32_t data = ROM32(bios->data[offset + 6 + 4 * i]);
  2458. bios_wr32(bios, reg, data);
  2459. }
  2460. return len;
  2461. }
  2462. static int
  2463. init_reserved(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2464. {
  2465. /*
  2466. * INIT_RESERVED opcode: 0x92 ('')
  2467. *
  2468. * offset (8 bit): opcode
  2469. *
  2470. * Seemingly does nothing
  2471. */
  2472. return 1;
  2473. }
  2474. static int
  2475. init_96(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2476. {
  2477. /*
  2478. * INIT_96 opcode: 0x96 ('')
  2479. *
  2480. * offset (8 bit): opcode
  2481. * offset + 1 (32 bit): sreg
  2482. * offset + 5 (8 bit): sshift
  2483. * offset + 6 (8 bit): smask
  2484. * offset + 7 (8 bit): index
  2485. * offset + 8 (32 bit): reg
  2486. * offset + 12 (32 bit): mask
  2487. * offset + 16 (8 bit): shift
  2488. *
  2489. */
  2490. uint16_t xlatptr = bios->init96_tbl_ptr + (bios->data[offset + 7] * 2);
  2491. uint32_t reg = ROM32(bios->data[offset + 8]);
  2492. uint32_t mask = ROM32(bios->data[offset + 12]);
  2493. uint32_t val;
  2494. val = bios_rd32(bios, ROM32(bios->data[offset + 1]));
  2495. if (bios->data[offset + 5] < 0x80)
  2496. val >>= bios->data[offset + 5];
  2497. else
  2498. val <<= (0x100 - bios->data[offset + 5]);
  2499. val &= bios->data[offset + 6];
  2500. val = bios->data[ROM16(bios->data[xlatptr]) + val];
  2501. val <<= bios->data[offset + 16];
  2502. if (!iexec->execute)
  2503. return 17;
  2504. bios_wr32(bios, reg, (bios_rd32(bios, reg) & mask) | val);
  2505. return 17;
  2506. }
  2507. static int
  2508. init_97(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2509. {
  2510. /*
  2511. * INIT_97 opcode: 0x97 ('')
  2512. *
  2513. * offset (8 bit): opcode
  2514. * offset + 1 (32 bit): register
  2515. * offset + 5 (32 bit): mask
  2516. * offset + 9 (32 bit): value
  2517. *
  2518. * Adds "value" to "register" preserving the fields specified
  2519. * by "mask"
  2520. */
  2521. uint32_t reg = ROM32(bios->data[offset + 1]);
  2522. uint32_t mask = ROM32(bios->data[offset + 5]);
  2523. uint32_t add = ROM32(bios->data[offset + 9]);
  2524. uint32_t val;
  2525. val = bios_rd32(bios, reg);
  2526. val = (val & mask) | ((val + add) & ~mask);
  2527. if (!iexec->execute)
  2528. return 13;
  2529. bios_wr32(bios, reg, val);
  2530. return 13;
  2531. }
  2532. static int
  2533. init_auxch(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2534. {
  2535. /*
  2536. * INIT_AUXCH opcode: 0x98 ('')
  2537. *
  2538. * offset (8 bit): opcode
  2539. * offset + 1 (32 bit): address
  2540. * offset + 5 (8 bit): count
  2541. * offset + 6 (8 bit): mask 0
  2542. * offset + 7 (8 bit): data 0
  2543. * ...
  2544. *
  2545. */
  2546. struct drm_device *dev = bios->dev;
  2547. struct nouveau_i2c_chan *auxch;
  2548. uint32_t addr = ROM32(bios->data[offset + 1]);
  2549. uint8_t count = bios->data[offset + 5];
  2550. int len = 6 + count * 2;
  2551. int ret, i;
  2552. if (!bios->display.output) {
  2553. NV_ERROR(dev, "INIT_AUXCH: no active output\n");
  2554. return len;
  2555. }
  2556. auxch = init_i2c_device_find(dev, bios->display.output->i2c_index);
  2557. if (!auxch) {
  2558. NV_ERROR(dev, "INIT_AUXCH: couldn't get auxch %d\n",
  2559. bios->display.output->i2c_index);
  2560. return len;
  2561. }
  2562. if (!iexec->execute)
  2563. return len;
  2564. offset += 6;
  2565. for (i = 0; i < count; i++, offset += 2) {
  2566. uint8_t data;
  2567. ret = nouveau_dp_auxch(auxch, 9, addr, &data, 1);
  2568. if (ret) {
  2569. NV_ERROR(dev, "INIT_AUXCH: rd auxch fail %d\n", ret);
  2570. return len;
  2571. }
  2572. data &= bios->data[offset + 0];
  2573. data |= bios->data[offset + 1];
  2574. ret = nouveau_dp_auxch(auxch, 8, addr, &data, 1);
  2575. if (ret) {
  2576. NV_ERROR(dev, "INIT_AUXCH: wr auxch fail %d\n", ret);
  2577. return len;
  2578. }
  2579. }
  2580. return len;
  2581. }
  2582. static int
  2583. init_zm_auxch(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
  2584. {
  2585. /*
  2586. * INIT_ZM_AUXCH opcode: 0x99 ('')
  2587. *
  2588. * offset (8 bit): opcode
  2589. * offset + 1 (32 bit): address
  2590. * offset + 5 (8 bit): count
  2591. * offset + 6 (8 bit): data 0
  2592. * ...
  2593. *
  2594. */
  2595. struct drm_device *dev = bios->dev;
  2596. struct nouveau_i2c_chan *auxch;
  2597. uint32_t addr = ROM32(bios->data[offset + 1]);
  2598. uint8_t count = bios->data[offset + 5];
  2599. int len = 6 + count;
  2600. int ret, i;
  2601. if (!bios->display.output) {
  2602. NV_ERROR(dev, "INIT_ZM_AUXCH: no active output\n");
  2603. return len;
  2604. }
  2605. auxch = init_i2c_device_find(dev, bios->display.output->i2c_index);
  2606. if (!auxch) {
  2607. NV_ERROR(dev, "INIT_ZM_AUXCH: couldn't get auxch %d\n",
  2608. bios->display.output->i2c_index);
  2609. return len;
  2610. }
  2611. if (!iexec->execute)
  2612. return len;
  2613. offset += 6;
  2614. for (i = 0; i < count; i++, offset++) {
  2615. ret = nouveau_dp_auxch(auxch, 8, addr, &bios->data[offset], 1);
  2616. if (ret) {
  2617. NV_ERROR(dev, "INIT_ZM_AUXCH: wr auxch fail %d\n", ret);
  2618. return len;
  2619. }
  2620. }
  2621. return len;
  2622. }
  2623. static struct init_tbl_entry itbl_entry[] = {
  2624. /* command name , id , length , offset , mult , command handler */
  2625. /* INIT_PROG (0x31, 15, 10, 4) removed due to no example of use */
  2626. { "INIT_IO_RESTRICT_PROG" , 0x32, init_io_restrict_prog },
  2627. { "INIT_REPEAT" , 0x33, init_repeat },
  2628. { "INIT_IO_RESTRICT_PLL" , 0x34, init_io_restrict_pll },
  2629. { "INIT_END_REPEAT" , 0x36, init_end_repeat },
  2630. { "INIT_COPY" , 0x37, init_copy },
  2631. { "INIT_NOT" , 0x38, init_not },
  2632. { "INIT_IO_FLAG_CONDITION" , 0x39, init_io_flag_condition },
  2633. { "INIT_DP_CONDITION" , 0x3A, init_dp_condition },
  2634. { "INIT_OP_3B" , 0x3B, init_op_3b },
  2635. { "INIT_OP_3C" , 0x3C, init_op_3c },
  2636. { "INIT_INDEX_ADDRESS_LATCHED" , 0x49, init_idx_addr_latched },
  2637. { "INIT_IO_RESTRICT_PLL2" , 0x4A, init_io_restrict_pll2 },
  2638. { "INIT_PLL2" , 0x4B, init_pll2 },
  2639. { "INIT_I2C_BYTE" , 0x4C, init_i2c_byte },
  2640. { "INIT_ZM_I2C_BYTE" , 0x4D, init_zm_i2c_byte },
  2641. { "INIT_ZM_I2C" , 0x4E, init_zm_i2c },
  2642. { "INIT_TMDS" , 0x4F, init_tmds },
  2643. { "INIT_ZM_TMDS_GROUP" , 0x50, init_zm_tmds_group },
  2644. { "INIT_CR_INDEX_ADDRESS_LATCHED" , 0x51, init_cr_idx_adr_latch },
  2645. { "INIT_CR" , 0x52, init_cr },
  2646. { "INIT_ZM_CR" , 0x53, init_zm_cr },
  2647. { "INIT_ZM_CR_GROUP" , 0x54, init_zm_cr_group },
  2648. { "INIT_CONDITION_TIME" , 0x56, init_condition_time },
  2649. { "INIT_ZM_REG_SEQUENCE" , 0x58, init_zm_reg_sequence },
  2650. /* INIT_INDIRECT_REG (0x5A, 7, 0, 0) removed due to no example of use */
  2651. { "INIT_SUB_DIRECT" , 0x5B, init_sub_direct },
  2652. { "INIT_COPY_NV_REG" , 0x5F, init_copy_nv_reg },
  2653. { "INIT_ZM_INDEX_IO" , 0x62, init_zm_index_io },
  2654. { "INIT_COMPUTE_MEM" , 0x63, init_compute_mem },
  2655. { "INIT_RESET" , 0x65, init_reset },
  2656. { "INIT_CONFIGURE_MEM" , 0x66, init_configure_mem },
  2657. { "INIT_CONFIGURE_CLK" , 0x67, init_configure_clk },
  2658. { "INIT_CONFIGURE_PREINIT" , 0x68, init_configure_preinit },
  2659. { "INIT_IO" , 0x69, init_io },
  2660. { "INIT_SUB" , 0x6B, init_sub },
  2661. { "INIT_RAM_CONDITION" , 0x6D, init_ram_condition },
  2662. { "INIT_NV_REG" , 0x6E, init_nv_reg },
  2663. { "INIT_MACRO" , 0x6F, init_macro },
  2664. { "INIT_DONE" , 0x71, init_done },
  2665. { "INIT_RESUME" , 0x72, init_resume },
  2666. /* INIT_RAM_CONDITION2 (0x73, 9, 0, 0) removed due to no example of use */
  2667. { "INIT_TIME" , 0x74, init_time },
  2668. { "INIT_CONDITION" , 0x75, init_condition },
  2669. { "INIT_IO_CONDITION" , 0x76, init_io_condition },
  2670. { "INIT_INDEX_IO" , 0x78, init_index_io },
  2671. { "INIT_PLL" , 0x79, init_pll },
  2672. { "INIT_ZM_REG" , 0x7A, init_zm_reg },
  2673. { "INIT_RAM_RESTRICT_PLL" , 0x87, init_ram_restrict_pll },
  2674. { "INIT_8C" , 0x8C, init_8c },
  2675. { "INIT_8D" , 0x8D, init_8d },
  2676. { "INIT_GPIO" , 0x8E, init_gpio },
  2677. { "INIT_RAM_RESTRICT_ZM_REG_GROUP" , 0x8F, init_ram_restrict_zm_reg_group },
  2678. { "INIT_COPY_ZM_REG" , 0x90, init_copy_zm_reg },
  2679. { "INIT_ZM_REG_GROUP_ADDRESS_LATCHED" , 0x91, init_zm_reg_group_addr_latched },
  2680. { "INIT_RESERVED" , 0x92, init_reserved },
  2681. { "INIT_96" , 0x96, init_96 },
  2682. { "INIT_97" , 0x97, init_97 },
  2683. { "INIT_AUXCH" , 0x98, init_auxch },
  2684. { "INIT_ZM_AUXCH" , 0x99, init_zm_auxch },
  2685. { NULL , 0 , NULL }
  2686. };
  2687. #define MAX_TABLE_OPS 1000
  2688. static int
  2689. parse_init_table(struct nvbios *bios, unsigned int offset,
  2690. struct init_exec *iexec)
  2691. {
  2692. /*
  2693. * Parses all commands in an init table.
  2694. *
  2695. * We start out executing all commands found in the init table. Some
  2696. * opcodes may change the status of iexec->execute to SKIP, which will
  2697. * cause the following opcodes to perform no operation until the value
  2698. * is changed back to EXECUTE.
  2699. */
  2700. int count = 0, i, ret;
  2701. uint8_t id;
  2702. /*
  2703. * Loop until INIT_DONE causes us to break out of the loop
  2704. * (or until offset > bios length just in case... )
  2705. * (and no more than MAX_TABLE_OPS iterations, just in case... )
  2706. */
  2707. while ((offset < bios->length) && (count++ < MAX_TABLE_OPS)) {
  2708. id = bios->data[offset];
  2709. /* Find matching id in itbl_entry */
  2710. for (i = 0; itbl_entry[i].name && (itbl_entry[i].id != id); i++)
  2711. ;
  2712. if (!itbl_entry[i].name) {
  2713. NV_ERROR(bios->dev,
  2714. "0x%04X: Init table command not found: "
  2715. "0x%02X\n", offset, id);
  2716. return -ENOENT;
  2717. }
  2718. BIOSLOG(bios, "0x%04X: [ (0x%02X) - %s ]\n", offset,
  2719. itbl_entry[i].id, itbl_entry[i].name);
  2720. /* execute eventual command handler */
  2721. ret = (*itbl_entry[i].handler)(bios, offset, iexec);
  2722. if (ret < 0) {
  2723. NV_ERROR(bios->dev, "0x%04X: Failed parsing init "
  2724. "table opcode: %s %d\n", offset,
  2725. itbl_entry[i].name, ret);
  2726. }
  2727. if (ret <= 0)
  2728. break;
  2729. /*
  2730. * Add the offset of the current command including all data
  2731. * of that command. The offset will then be pointing on the
  2732. * next op code.
  2733. */
  2734. offset += ret;
  2735. }
  2736. if (offset >= bios->length)
  2737. NV_WARN(bios->dev,
  2738. "Offset 0x%04X greater than known bios image length. "
  2739. "Corrupt image?\n", offset);
  2740. if (count >= MAX_TABLE_OPS)
  2741. NV_WARN(bios->dev,
  2742. "More than %d opcodes to a table is unlikely, "
  2743. "is the bios image corrupt?\n", MAX_TABLE_OPS);
  2744. return 0;
  2745. }
  2746. static void
  2747. parse_init_tables(struct nvbios *bios)
  2748. {
  2749. /* Loops and calls parse_init_table() for each present table. */
  2750. int i = 0;
  2751. uint16_t table;
  2752. struct init_exec iexec = {true, false};
  2753. if (bios->old_style_init) {
  2754. if (bios->init_script_tbls_ptr)
  2755. parse_init_table(bios, bios->init_script_tbls_ptr, &iexec);
  2756. if (bios->extra_init_script_tbl_ptr)
  2757. parse_init_table(bios, bios->extra_init_script_tbl_ptr, &iexec);
  2758. return;
  2759. }
  2760. while ((table = ROM16(bios->data[bios->init_script_tbls_ptr + i]))) {
  2761. NV_INFO(bios->dev,
  2762. "Parsing VBIOS init table %d at offset 0x%04X\n",
  2763. i / 2, table);
  2764. BIOSLOG(bios, "0x%04X: ------ Executing following commands ------\n", table);
  2765. parse_init_table(bios, table, &iexec);
  2766. i += 2;
  2767. }
  2768. }
  2769. static uint16_t clkcmptable(struct nvbios *bios, uint16_t clktable, int pxclk)
  2770. {
  2771. int compare_record_len, i = 0;
  2772. uint16_t compareclk, scriptptr = 0;
  2773. if (bios->major_version < 5) /* pre BIT */
  2774. compare_record_len = 3;
  2775. else
  2776. compare_record_len = 4;
  2777. do {
  2778. compareclk = ROM16(bios->data[clktable + compare_record_len * i]);
  2779. if (pxclk >= compareclk * 10) {
  2780. if (bios->major_version < 5) {
  2781. uint8_t tmdssub = bios->data[clktable + 2 + compare_record_len * i];
  2782. scriptptr = ROM16(bios->data[bios->init_script_tbls_ptr + tmdssub * 2]);
  2783. } else
  2784. scriptptr = ROM16(bios->data[clktable + 2 + compare_record_len * i]);
  2785. break;
  2786. }
  2787. i++;
  2788. } while (compareclk);
  2789. return scriptptr;
  2790. }
  2791. static void
  2792. run_digital_op_script(struct drm_device *dev, uint16_t scriptptr,
  2793. struct dcb_entry *dcbent, int head, bool dl)
  2794. {
  2795. struct drm_nouveau_private *dev_priv = dev->dev_private;
  2796. struct nvbios *bios = &dev_priv->vbios;
  2797. struct init_exec iexec = {true, false};
  2798. NV_TRACE(dev, "0x%04X: Parsing digital output script table\n",
  2799. scriptptr);
  2800. bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_44,
  2801. head ? NV_CIO_CRE_44_HEADB : NV_CIO_CRE_44_HEADA);
  2802. /* note: if dcb entries have been merged, index may be misleading */
  2803. NVWriteVgaCrtc5758(dev, head, 0, dcbent->index);
  2804. parse_init_table(bios, scriptptr, &iexec);
  2805. nv04_dfp_bind_head(dev, dcbent, head, dl);
  2806. }
  2807. static int call_lvds_manufacturer_script(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script)
  2808. {
  2809. struct drm_nouveau_private *dev_priv = dev->dev_private;
  2810. struct nvbios *bios = &dev_priv->vbios;
  2811. uint8_t sub = bios->data[bios->fp.xlated_entry + script] + (bios->fp.link_c_increment && dcbent->or & OUTPUT_C ? 1 : 0);
  2812. uint16_t scriptofs = ROM16(bios->data[bios->init_script_tbls_ptr + sub * 2]);
  2813. if (!bios->fp.xlated_entry || !sub || !scriptofs)
  2814. return -EINVAL;
  2815. run_digital_op_script(dev, scriptofs, dcbent, head, bios->fp.dual_link);
  2816. if (script == LVDS_PANEL_OFF) {
  2817. /* off-on delay in ms */
  2818. msleep(ROM16(bios->data[bios->fp.xlated_entry + 7]));
  2819. }
  2820. #ifdef __powerpc__
  2821. /* Powerbook specific quirks */
  2822. if ((dev->pci_device & 0xffff) == 0x0179 ||
  2823. (dev->pci_device & 0xffff) == 0x0189 ||
  2824. (dev->pci_device & 0xffff) == 0x0329) {
  2825. if (script == LVDS_RESET) {
  2826. nv_write_tmds(dev, dcbent->or, 0, 0x02, 0x72);
  2827. } else if (script == LVDS_PANEL_ON) {
  2828. bios_wr32(bios, NV_PBUS_DEBUG_DUALHEAD_CTL,
  2829. bios_rd32(bios, NV_PBUS_DEBUG_DUALHEAD_CTL)
  2830. | (1 << 31));
  2831. bios_wr32(bios, NV_PCRTC_GPIO_EXT,
  2832. bios_rd32(bios, NV_PCRTC_GPIO_EXT) | 1);
  2833. } else if (script == LVDS_PANEL_OFF) {
  2834. bios_wr32(bios, NV_PBUS_DEBUG_DUALHEAD_CTL,
  2835. bios_rd32(bios, NV_PBUS_DEBUG_DUALHEAD_CTL)
  2836. & ~(1 << 31));
  2837. bios_wr32(bios, NV_PCRTC_GPIO_EXT,
  2838. bios_rd32(bios, NV_PCRTC_GPIO_EXT) & ~3);
  2839. }
  2840. }
  2841. #endif
  2842. return 0;
  2843. }
  2844. static int run_lvds_table(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script, int pxclk)
  2845. {
  2846. /*
  2847. * The BIT LVDS table's header has the information to setup the
  2848. * necessary registers. Following the standard 4 byte header are:
  2849. * A bitmask byte and a dual-link transition pxclk value for use in
  2850. * selecting the init script when not using straps; 4 script pointers
  2851. * for panel power, selected by output and on/off; and 8 table pointers
  2852. * for panel init, the needed one determined by output, and bits in the
  2853. * conf byte. These tables are similar to the TMDS tables, consisting
  2854. * of a list of pxclks and script pointers.
  2855. */
  2856. struct drm_nouveau_private *dev_priv = dev->dev_private;
  2857. struct nvbios *bios = &dev_priv->vbios;
  2858. unsigned int outputset = (dcbent->or == 4) ? 1 : 0;
  2859. uint16_t scriptptr = 0, clktable;
  2860. /*
  2861. * For now we assume version 3.0 table - g80 support will need some
  2862. * changes
  2863. */
  2864. switch (script) {
  2865. case LVDS_INIT:
  2866. return -ENOSYS;
  2867. case LVDS_BACKLIGHT_ON:
  2868. case LVDS_PANEL_ON:
  2869. scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 7 + outputset * 2]);
  2870. break;
  2871. case LVDS_BACKLIGHT_OFF:
  2872. case LVDS_PANEL_OFF:
  2873. scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 11 + outputset * 2]);
  2874. break;
  2875. case LVDS_RESET:
  2876. clktable = bios->fp.lvdsmanufacturerpointer + 15;
  2877. if (dcbent->or == 4)
  2878. clktable += 8;
  2879. if (dcbent->lvdsconf.use_straps_for_mode) {
  2880. if (bios->fp.dual_link)
  2881. clktable += 4;
  2882. if (bios->fp.if_is_24bit)
  2883. clktable += 2;
  2884. } else {
  2885. /* using EDID */
  2886. int cmpval_24bit = (dcbent->or == 4) ? 4 : 1;
  2887. if (bios->fp.dual_link) {
  2888. clktable += 4;
  2889. cmpval_24bit <<= 1;
  2890. }
  2891. if (bios->fp.strapless_is_24bit & cmpval_24bit)
  2892. clktable += 2;
  2893. }
  2894. clktable = ROM16(bios->data[clktable]);
  2895. if (!clktable) {
  2896. NV_ERROR(dev, "Pixel clock comparison table not found\n");
  2897. return -ENOENT;
  2898. }
  2899. scriptptr = clkcmptable(bios, clktable, pxclk);
  2900. }
  2901. if (!scriptptr) {
  2902. NV_ERROR(dev, "LVDS output init script not found\n");
  2903. return -ENOENT;
  2904. }
  2905. run_digital_op_script(dev, scriptptr, dcbent, head, bios->fp.dual_link);
  2906. return 0;
  2907. }
  2908. int call_lvds_script(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script, int pxclk)
  2909. {
  2910. /*
  2911. * LVDS operations are multiplexed in an effort to present a single API
  2912. * which works with two vastly differing underlying structures.
  2913. * This acts as the demux
  2914. */
  2915. struct drm_nouveau_private *dev_priv = dev->dev_private;
  2916. struct nvbios *bios = &dev_priv->vbios;
  2917. uint8_t lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
  2918. uint32_t sel_clk_binding, sel_clk;
  2919. int ret;
  2920. if (bios->fp.last_script_invoc == (script << 1 | head) || !lvds_ver ||
  2921. (lvds_ver >= 0x30 && script == LVDS_INIT))
  2922. return 0;
  2923. if (!bios->fp.lvds_init_run) {
  2924. bios->fp.lvds_init_run = true;
  2925. call_lvds_script(dev, dcbent, head, LVDS_INIT, pxclk);
  2926. }
  2927. if (script == LVDS_PANEL_ON && bios->fp.reset_after_pclk_change)
  2928. call_lvds_script(dev, dcbent, head, LVDS_RESET, pxclk);
  2929. if (script == LVDS_RESET && bios->fp.power_off_for_reset)
  2930. call_lvds_script(dev, dcbent, head, LVDS_PANEL_OFF, pxclk);
  2931. NV_TRACE(dev, "Calling LVDS script %d:\n", script);
  2932. /* don't let script change pll->head binding */
  2933. sel_clk_binding = bios_rd32(bios, NV_PRAMDAC_SEL_CLK) & 0x50000;
  2934. if (lvds_ver < 0x30)
  2935. ret = call_lvds_manufacturer_script(dev, dcbent, head, script);
  2936. else
  2937. ret = run_lvds_table(dev, dcbent, head, script, pxclk);
  2938. bios->fp.last_script_invoc = (script << 1 | head);
  2939. sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
  2940. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
  2941. /* some scripts set a value in NV_PBUS_POWERCTRL_2 and break video overlay */
  2942. nvWriteMC(dev, NV_PBUS_POWERCTRL_2, 0);
  2943. return ret;
  2944. }
  2945. struct lvdstableheader {
  2946. uint8_t lvds_ver, headerlen, recordlen;
  2947. };
  2948. static int parse_lvds_manufacturer_table_header(struct drm_device *dev, struct nvbios *bios, struct lvdstableheader *lth)
  2949. {
  2950. /*
  2951. * BMP version (0xa) LVDS table has a simple header of version and
  2952. * record length. The BIT LVDS table has the typical BIT table header:
  2953. * version byte, header length byte, record length byte, and a byte for
  2954. * the maximum number of records that can be held in the table.
  2955. */
  2956. uint8_t lvds_ver, headerlen, recordlen;
  2957. memset(lth, 0, sizeof(struct lvdstableheader));
  2958. if (bios->fp.lvdsmanufacturerpointer == 0x0) {
  2959. NV_ERROR(dev, "Pointer to LVDS manufacturer table invalid\n");
  2960. return -EINVAL;
  2961. }
  2962. lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
  2963. switch (lvds_ver) {
  2964. case 0x0a: /* pre NV40 */
  2965. headerlen = 2;
  2966. recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
  2967. break;
  2968. case 0x30: /* NV4x */
  2969. headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
  2970. if (headerlen < 0x1f) {
  2971. NV_ERROR(dev, "LVDS table header not understood\n");
  2972. return -EINVAL;
  2973. }
  2974. recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
  2975. break;
  2976. case 0x40: /* G80/G90 */
  2977. headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
  2978. if (headerlen < 0x7) {
  2979. NV_ERROR(dev, "LVDS table header not understood\n");
  2980. return -EINVAL;
  2981. }
  2982. recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
  2983. break;
  2984. default:
  2985. NV_ERROR(dev,
  2986. "LVDS table revision %d.%d not currently supported\n",
  2987. lvds_ver >> 4, lvds_ver & 0xf);
  2988. return -ENOSYS;
  2989. }
  2990. lth->lvds_ver = lvds_ver;
  2991. lth->headerlen = headerlen;
  2992. lth->recordlen = recordlen;
  2993. return 0;
  2994. }
  2995. static int
  2996. get_fp_strap(struct drm_device *dev, struct nvbios *bios)
  2997. {
  2998. struct drm_nouveau_private *dev_priv = dev->dev_private;
  2999. /*
  3000. * The fp strap is normally dictated by the "User Strap" in
  3001. * PEXTDEV_BOOT_0[20:16], but on BMP cards when bit 2 of the
  3002. * Internal_Flags struct at 0x48 is set, the user strap gets overriden
  3003. * by the PCI subsystem ID during POST, but not before the previous user
  3004. * strap has been committed to CR58 for CR57=0xf on head A, which may be
  3005. * read and used instead
  3006. */
  3007. if (bios->major_version < 5 && bios->data[0x48] & 0x4)
  3008. return NVReadVgaCrtc5758(dev, 0, 0xf) & 0xf;
  3009. if (dev_priv->card_type >= NV_50)
  3010. return (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 24) & 0xf;
  3011. else
  3012. return (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 16) & 0xf;
  3013. }
  3014. static int parse_fp_mode_table(struct drm_device *dev, struct nvbios *bios)
  3015. {
  3016. uint8_t *fptable;
  3017. uint8_t fptable_ver, headerlen = 0, recordlen, fpentries = 0xf, fpindex;
  3018. int ret, ofs, fpstrapping;
  3019. struct lvdstableheader lth;
  3020. if (bios->fp.fptablepointer == 0x0) {
  3021. /* Apple cards don't have the fp table; the laptops use DDC */
  3022. /* The table is also missing on some x86 IGPs */
  3023. #ifndef __powerpc__
  3024. NV_ERROR(dev, "Pointer to flat panel table invalid\n");
  3025. #endif
  3026. bios->digital_min_front_porch = 0x4b;
  3027. return 0;
  3028. }
  3029. fptable = &bios->data[bios->fp.fptablepointer];
  3030. fptable_ver = fptable[0];
  3031. switch (fptable_ver) {
  3032. /*
  3033. * BMP version 0x5.0x11 BIOSen have version 1 like tables, but no
  3034. * version field, and miss one of the spread spectrum/PWM bytes.
  3035. * This could affect early GF2Go parts (not seen any appropriate ROMs
  3036. * though). Here we assume that a version of 0x05 matches this case
  3037. * (combining with a BMP version check would be better), as the
  3038. * common case for the panel type field is 0x0005, and that is in
  3039. * fact what we are reading the first byte of.
  3040. */
  3041. case 0x05: /* some NV10, 11, 15, 16 */
  3042. recordlen = 42;
  3043. ofs = -1;
  3044. break;
  3045. case 0x10: /* some NV15/16, and NV11+ */
  3046. recordlen = 44;
  3047. ofs = 0;
  3048. break;
  3049. case 0x20: /* NV40+ */
  3050. headerlen = fptable[1];
  3051. recordlen = fptable[2];
  3052. fpentries = fptable[3];
  3053. /*
  3054. * fptable[4] is the minimum
  3055. * RAMDAC_FP_HCRTC -> RAMDAC_FP_HSYNC_START gap
  3056. */
  3057. bios->digital_min_front_porch = fptable[4];
  3058. ofs = -7;
  3059. break;
  3060. default:
  3061. NV_ERROR(dev,
  3062. "FP table revision %d.%d not currently supported\n",
  3063. fptable_ver >> 4, fptable_ver & 0xf);
  3064. return -ENOSYS;
  3065. }
  3066. if (!bios->is_mobile) /* !mobile only needs digital_min_front_porch */
  3067. return 0;
  3068. ret = parse_lvds_manufacturer_table_header(dev, bios, &lth);
  3069. if (ret)
  3070. return ret;
  3071. if (lth.lvds_ver == 0x30 || lth.lvds_ver == 0x40) {
  3072. bios->fp.fpxlatetableptr = bios->fp.lvdsmanufacturerpointer +
  3073. lth.headerlen + 1;
  3074. bios->fp.xlatwidth = lth.recordlen;
  3075. }
  3076. if (bios->fp.fpxlatetableptr == 0x0) {
  3077. NV_ERROR(dev, "Pointer to flat panel xlat table invalid\n");
  3078. return -EINVAL;
  3079. }
  3080. fpstrapping = get_fp_strap(dev, bios);
  3081. fpindex = bios->data[bios->fp.fpxlatetableptr +
  3082. fpstrapping * bios->fp.xlatwidth];
  3083. if (fpindex > fpentries) {
  3084. NV_ERROR(dev, "Bad flat panel table index\n");
  3085. return -ENOENT;
  3086. }
  3087. /* nv4x cards need both a strap value and fpindex of 0xf to use DDC */
  3088. if (lth.lvds_ver > 0x10)
  3089. bios->fp_no_ddc = fpstrapping != 0xf || fpindex != 0xf;
  3090. /*
  3091. * If either the strap or xlated fpindex value are 0xf there is no
  3092. * panel using a strap-derived bios mode present. this condition
  3093. * includes, but is different from, the DDC panel indicator above
  3094. */
  3095. if (fpstrapping == 0xf || fpindex == 0xf)
  3096. return 0;
  3097. bios->fp.mode_ptr = bios->fp.fptablepointer + headerlen +
  3098. recordlen * fpindex + ofs;
  3099. NV_TRACE(dev, "BIOS FP mode: %dx%d (%dkHz pixel clock)\n",
  3100. ROM16(bios->data[bios->fp.mode_ptr + 11]) + 1,
  3101. ROM16(bios->data[bios->fp.mode_ptr + 25]) + 1,
  3102. ROM16(bios->data[bios->fp.mode_ptr + 7]) * 10);
  3103. return 0;
  3104. }
  3105. bool nouveau_bios_fp_mode(struct drm_device *dev, struct drm_display_mode *mode)
  3106. {
  3107. struct drm_nouveau_private *dev_priv = dev->dev_private;
  3108. struct nvbios *bios = &dev_priv->vbios;
  3109. uint8_t *mode_entry = &bios->data[bios->fp.mode_ptr];
  3110. if (!mode) /* just checking whether we can produce a mode */
  3111. return bios->fp.mode_ptr;
  3112. memset(mode, 0, sizeof(struct drm_display_mode));
  3113. /*
  3114. * For version 1.0 (version in byte 0):
  3115. * bytes 1-2 are "panel type", including bits on whether Colour/mono,
  3116. * single/dual link, and type (TFT etc.)
  3117. * bytes 3-6 are bits per colour in RGBX
  3118. */
  3119. mode->clock = ROM16(mode_entry[7]) * 10;
  3120. /* bytes 9-10 is HActive */
  3121. mode->hdisplay = ROM16(mode_entry[11]) + 1;
  3122. /*
  3123. * bytes 13-14 is HValid Start
  3124. * bytes 15-16 is HValid End
  3125. */
  3126. mode->hsync_start = ROM16(mode_entry[17]) + 1;
  3127. mode->hsync_end = ROM16(mode_entry[19]) + 1;
  3128. mode->htotal = ROM16(mode_entry[21]) + 1;
  3129. /* bytes 23-24, 27-30 similarly, but vertical */
  3130. mode->vdisplay = ROM16(mode_entry[25]) + 1;
  3131. mode->vsync_start = ROM16(mode_entry[31]) + 1;
  3132. mode->vsync_end = ROM16(mode_entry[33]) + 1;
  3133. mode->vtotal = ROM16(mode_entry[35]) + 1;
  3134. mode->flags |= (mode_entry[37] & 0x10) ?
  3135. DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
  3136. mode->flags |= (mode_entry[37] & 0x1) ?
  3137. DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
  3138. /*
  3139. * bytes 38-39 relate to spread spectrum settings
  3140. * bytes 40-43 are something to do with PWM
  3141. */
  3142. mode->status = MODE_OK;
  3143. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  3144. drm_mode_set_name(mode);
  3145. return bios->fp.mode_ptr;
  3146. }
  3147. int nouveau_bios_parse_lvds_table(struct drm_device *dev, int pxclk, bool *dl, bool *if_is_24bit)
  3148. {
  3149. /*
  3150. * The LVDS table header is (mostly) described in
  3151. * parse_lvds_manufacturer_table_header(): the BIT header additionally
  3152. * contains the dual-link transition pxclk (in 10s kHz), at byte 5 - if
  3153. * straps are not being used for the panel, this specifies the frequency
  3154. * at which modes should be set up in the dual link style.
  3155. *
  3156. * Following the header, the BMP (ver 0xa) table has several records,
  3157. * indexed by a separate xlat table, indexed in turn by the fp strap in
  3158. * EXTDEV_BOOT. Each record had a config byte, followed by 6 script
  3159. * numbers for use by INIT_SUB which controlled panel init and power,
  3160. * and finally a dword of ms to sleep between power off and on
  3161. * operations.
  3162. *
  3163. * In the BIT versions, the table following the header serves as an
  3164. * integrated config and xlat table: the records in the table are
  3165. * indexed by the FP strap nibble in EXTDEV_BOOT, and each record has
  3166. * two bytes - the first as a config byte, the second for indexing the
  3167. * fp mode table pointed to by the BIT 'D' table
  3168. *
  3169. * DDC is not used until after card init, so selecting the correct table
  3170. * entry and setting the dual link flag for EDID equipped panels,
  3171. * requiring tests against the native-mode pixel clock, cannot be done
  3172. * until later, when this function should be called with non-zero pxclk
  3173. */
  3174. struct drm_nouveau_private *dev_priv = dev->dev_private;
  3175. struct nvbios *bios = &dev_priv->vbios;
  3176. int fpstrapping = get_fp_strap(dev, bios), lvdsmanufacturerindex = 0;
  3177. struct lvdstableheader lth;
  3178. uint16_t lvdsofs;
  3179. int ret, chip_version = bios->chip_version;
  3180. ret = parse_lvds_manufacturer_table_header(dev, bios, &lth);
  3181. if (ret)
  3182. return ret;
  3183. switch (lth.lvds_ver) {
  3184. case 0x0a: /* pre NV40 */
  3185. lvdsmanufacturerindex = bios->data[
  3186. bios->fp.fpxlatemanufacturertableptr +
  3187. fpstrapping];
  3188. /* we're done if this isn't the EDID panel case */
  3189. if (!pxclk)
  3190. break;
  3191. if (chip_version < 0x25) {
  3192. /* nv17 behaviour
  3193. *
  3194. * It seems the old style lvds script pointer is reused
  3195. * to select 18/24 bit colour depth for EDID panels.
  3196. */
  3197. lvdsmanufacturerindex =
  3198. (bios->legacy.lvds_single_a_script_ptr & 1) ?
  3199. 2 : 0;
  3200. if (pxclk >= bios->fp.duallink_transition_clk)
  3201. lvdsmanufacturerindex++;
  3202. } else if (chip_version < 0x30) {
  3203. /* nv28 behaviour (off-chip encoder)
  3204. *
  3205. * nv28 does a complex dance of first using byte 121 of
  3206. * the EDID to choose the lvdsmanufacturerindex, then
  3207. * later attempting to match the EDID manufacturer and
  3208. * product IDs in a table (signature 'pidt' (panel id
  3209. * table?)), setting an lvdsmanufacturerindex of 0 and
  3210. * an fp strap of the match index (or 0xf if none)
  3211. */
  3212. lvdsmanufacturerindex = 0;
  3213. } else {
  3214. /* nv31, nv34 behaviour */
  3215. lvdsmanufacturerindex = 0;
  3216. if (pxclk >= bios->fp.duallink_transition_clk)
  3217. lvdsmanufacturerindex = 2;
  3218. if (pxclk >= 140000)
  3219. lvdsmanufacturerindex = 3;
  3220. }
  3221. /*
  3222. * nvidia set the high nibble of (cr57=f, cr58) to
  3223. * lvdsmanufacturerindex in this case; we don't
  3224. */
  3225. break;
  3226. case 0x30: /* NV4x */
  3227. case 0x40: /* G80/G90 */
  3228. lvdsmanufacturerindex = fpstrapping;
  3229. break;
  3230. default:
  3231. NV_ERROR(dev, "LVDS table revision not currently supported\n");
  3232. return -ENOSYS;
  3233. }
  3234. lvdsofs = bios->fp.xlated_entry = bios->fp.lvdsmanufacturerpointer + lth.headerlen + lth.recordlen * lvdsmanufacturerindex;
  3235. switch (lth.lvds_ver) {
  3236. case 0x0a:
  3237. bios->fp.power_off_for_reset = bios->data[lvdsofs] & 1;
  3238. bios->fp.reset_after_pclk_change = bios->data[lvdsofs] & 2;
  3239. bios->fp.dual_link = bios->data[lvdsofs] & 4;
  3240. bios->fp.link_c_increment = bios->data[lvdsofs] & 8;
  3241. *if_is_24bit = bios->data[lvdsofs] & 16;
  3242. break;
  3243. case 0x30:
  3244. case 0x40:
  3245. /*
  3246. * No sign of the "power off for reset" or "reset for panel
  3247. * on" bits, but it's safer to assume we should
  3248. */
  3249. bios->fp.power_off_for_reset = true;
  3250. bios->fp.reset_after_pclk_change = true;
  3251. /*
  3252. * It's ok lvdsofs is wrong for nv4x edid case; dual_link is
  3253. * over-written, and if_is_24bit isn't used
  3254. */
  3255. bios->fp.dual_link = bios->data[lvdsofs] & 1;
  3256. bios->fp.if_is_24bit = bios->data[lvdsofs] & 2;
  3257. bios->fp.strapless_is_24bit = bios->data[bios->fp.lvdsmanufacturerpointer + 4];
  3258. bios->fp.duallink_transition_clk = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 5]) * 10;
  3259. break;
  3260. }
  3261. /* Dell Latitude D620 reports a too-high value for the dual-link
  3262. * transition freq, causing us to program the panel incorrectly.
  3263. *
  3264. * It doesn't appear the VBIOS actually uses its transition freq
  3265. * (90000kHz), instead it uses the "Number of LVDS channels" field
  3266. * out of the panel ID structure (http://www.spwg.org/).
  3267. *
  3268. * For the moment, a quirk will do :)
  3269. */
  3270. if ((dev->pdev->device == 0x01d7) &&
  3271. (dev->pdev->subsystem_vendor == 0x1028) &&
  3272. (dev->pdev->subsystem_device == 0x01c2)) {
  3273. bios->fp.duallink_transition_clk = 80000;
  3274. }
  3275. /* set dual_link flag for EDID case */
  3276. if (pxclk && (chip_version < 0x25 || chip_version > 0x28))
  3277. bios->fp.dual_link = (pxclk >= bios->fp.duallink_transition_clk);
  3278. *dl = bios->fp.dual_link;
  3279. return 0;
  3280. }
  3281. static uint8_t *
  3282. bios_output_config_match(struct drm_device *dev, struct dcb_entry *dcbent,
  3283. uint16_t record, int record_len, int record_nr,
  3284. bool match_link)
  3285. {
  3286. struct drm_nouveau_private *dev_priv = dev->dev_private;
  3287. struct nvbios *bios = &dev_priv->vbios;
  3288. uint32_t entry;
  3289. uint16_t table;
  3290. int i, v;
  3291. switch (dcbent->type) {
  3292. case OUTPUT_TMDS:
  3293. case OUTPUT_LVDS:
  3294. case OUTPUT_DP:
  3295. break;
  3296. default:
  3297. match_link = false;
  3298. break;
  3299. }
  3300. for (i = 0; i < record_nr; i++, record += record_len) {
  3301. table = ROM16(bios->data[record]);
  3302. if (!table)
  3303. continue;
  3304. entry = ROM32(bios->data[table]);
  3305. if (match_link) {
  3306. v = (entry & 0x00c00000) >> 22;
  3307. if (!(v & dcbent->sorconf.link))
  3308. continue;
  3309. }
  3310. v = (entry & 0x000f0000) >> 16;
  3311. if (!(v & dcbent->or))
  3312. continue;
  3313. v = (entry & 0x000000f0) >> 4;
  3314. if (v != dcbent->location)
  3315. continue;
  3316. v = (entry & 0x0000000f);
  3317. if (v != dcbent->type)
  3318. continue;
  3319. return &bios->data[table];
  3320. }
  3321. return NULL;
  3322. }
  3323. void *
  3324. nouveau_bios_dp_table(struct drm_device *dev, struct dcb_entry *dcbent,
  3325. int *length)
  3326. {
  3327. struct drm_nouveau_private *dev_priv = dev->dev_private;
  3328. struct nvbios *bios = &dev_priv->vbios;
  3329. uint8_t *table;
  3330. if (!bios->display.dp_table_ptr) {
  3331. NV_ERROR(dev, "No pointer to DisplayPort table\n");
  3332. return NULL;
  3333. }
  3334. table = &bios->data[bios->display.dp_table_ptr];
  3335. if (table[0] != 0x20 && table[0] != 0x21) {
  3336. NV_ERROR(dev, "DisplayPort table version 0x%02x unknown\n",
  3337. table[0]);
  3338. return NULL;
  3339. }
  3340. *length = table[4];
  3341. return bios_output_config_match(dev, dcbent,
  3342. bios->display.dp_table_ptr + table[1],
  3343. table[2], table[3], table[0] >= 0x21);
  3344. }
  3345. int
  3346. nouveau_bios_run_display_table(struct drm_device *dev, struct dcb_entry *dcbent,
  3347. uint32_t sub, int pxclk)
  3348. {
  3349. /*
  3350. * The display script table is located by the BIT 'U' table.
  3351. *
  3352. * It contains an array of pointers to various tables describing
  3353. * a particular output type. The first 32-bits of the output
  3354. * tables contains similar information to a DCB entry, and is
  3355. * used to decide whether that particular table is suitable for
  3356. * the output you want to access.
  3357. *
  3358. * The "record header length" field here seems to indicate the
  3359. * offset of the first configuration entry in the output tables.
  3360. * This is 10 on most cards I've seen, but 12 has been witnessed
  3361. * on DP cards, and there's another script pointer within the
  3362. * header.
  3363. *
  3364. * offset + 0 ( 8 bits): version
  3365. * offset + 1 ( 8 bits): header length
  3366. * offset + 2 ( 8 bits): record length
  3367. * offset + 3 ( 8 bits): number of records
  3368. * offset + 4 ( 8 bits): record header length
  3369. * offset + 5 (16 bits): pointer to first output script table
  3370. */
  3371. struct drm_nouveau_private *dev_priv = dev->dev_private;
  3372. struct nvbios *bios = &dev_priv->vbios;
  3373. uint8_t *table = &bios->data[bios->display.script_table_ptr];
  3374. uint8_t *otable = NULL;
  3375. uint16_t script;
  3376. int i = 0;
  3377. if (!bios->display.script_table_ptr) {
  3378. NV_ERROR(dev, "No pointer to output script table\n");
  3379. return 1;
  3380. }
  3381. /*
  3382. * Nothing useful has been in any of the pre-2.0 tables I've seen,
  3383. * so until they are, we really don't need to care.
  3384. */
  3385. if (table[0] < 0x20)
  3386. return 1;
  3387. if (table[0] != 0x20 && table[0] != 0x21) {
  3388. NV_ERROR(dev, "Output script table version 0x%02x unknown\n",
  3389. table[0]);
  3390. return 1;
  3391. }
  3392. /*
  3393. * The output script tables describing a particular output type
  3394. * look as follows:
  3395. *
  3396. * offset + 0 (32 bits): output this table matches (hash of DCB)
  3397. * offset + 4 ( 8 bits): unknown
  3398. * offset + 5 ( 8 bits): number of configurations
  3399. * offset + 6 (16 bits): pointer to some script
  3400. * offset + 8 (16 bits): pointer to some script
  3401. *
  3402. * headerlen == 10
  3403. * offset + 10 : configuration 0
  3404. *
  3405. * headerlen == 12
  3406. * offset + 10 : pointer to some script
  3407. * offset + 12 : configuration 0
  3408. *
  3409. * Each config entry is as follows:
  3410. *
  3411. * offset + 0 (16 bits): unknown, assumed to be a match value
  3412. * offset + 2 (16 bits): pointer to script table (clock set?)
  3413. * offset + 4 (16 bits): pointer to script table (reset?)
  3414. *
  3415. * There doesn't appear to be a count value to say how many
  3416. * entries exist in each script table, instead, a 0 value in
  3417. * the first 16-bit word seems to indicate both the end of the
  3418. * list and the default entry. The second 16-bit word in the
  3419. * script tables is a pointer to the script to execute.
  3420. */
  3421. NV_DEBUG_KMS(dev, "Searching for output entry for %d %d %d\n",
  3422. dcbent->type, dcbent->location, dcbent->or);
  3423. otable = bios_output_config_match(dev, dcbent, table[1] +
  3424. bios->display.script_table_ptr,
  3425. table[2], table[3], table[0] >= 0x21);
  3426. if (!otable) {
  3427. NV_ERROR(dev, "Couldn't find matching output script table\n");
  3428. return 1;
  3429. }
  3430. if (pxclk < -2 || pxclk > 0) {
  3431. /* Try to find matching script table entry */
  3432. for (i = 0; i < otable[5]; i++) {
  3433. if (ROM16(otable[table[4] + i*6]) == sub)
  3434. break;
  3435. }
  3436. if (i == otable[5]) {
  3437. NV_ERROR(dev, "Table 0x%04x not found for %d/%d, "
  3438. "using first\n",
  3439. sub, dcbent->type, dcbent->or);
  3440. i = 0;
  3441. }
  3442. }
  3443. if (pxclk == 0) {
  3444. script = ROM16(otable[6]);
  3445. if (!script) {
  3446. NV_DEBUG_KMS(dev, "output script 0 not found\n");
  3447. return 1;
  3448. }
  3449. NV_TRACE(dev, "0x%04X: parsing output script 0\n", script);
  3450. nouveau_bios_run_init_table(dev, script, dcbent);
  3451. } else
  3452. if (pxclk == -1) {
  3453. script = ROM16(otable[8]);
  3454. if (!script) {
  3455. NV_DEBUG_KMS(dev, "output script 1 not found\n");
  3456. return 1;
  3457. }
  3458. NV_TRACE(dev, "0x%04X: parsing output script 1\n", script);
  3459. nouveau_bios_run_init_table(dev, script, dcbent);
  3460. } else
  3461. if (pxclk == -2) {
  3462. if (table[4] >= 12)
  3463. script = ROM16(otable[10]);
  3464. else
  3465. script = 0;
  3466. if (!script) {
  3467. NV_DEBUG_KMS(dev, "output script 2 not found\n");
  3468. return 1;
  3469. }
  3470. NV_TRACE(dev, "0x%04X: parsing output script 2\n", script);
  3471. nouveau_bios_run_init_table(dev, script, dcbent);
  3472. } else
  3473. if (pxclk > 0) {
  3474. script = ROM16(otable[table[4] + i*6 + 2]);
  3475. if (script)
  3476. script = clkcmptable(bios, script, pxclk);
  3477. if (!script) {
  3478. NV_ERROR(dev, "clock script 0 not found\n");
  3479. return 1;
  3480. }
  3481. NV_TRACE(dev, "0x%04X: parsing clock script 0\n", script);
  3482. nouveau_bios_run_init_table(dev, script, dcbent);
  3483. } else
  3484. if (pxclk < 0) {
  3485. script = ROM16(otable[table[4] + i*6 + 4]);
  3486. if (script)
  3487. script = clkcmptable(bios, script, -pxclk);
  3488. if (!script) {
  3489. NV_DEBUG_KMS(dev, "clock script 1 not found\n");
  3490. return 1;
  3491. }
  3492. NV_TRACE(dev, "0x%04X: parsing clock script 1\n", script);
  3493. nouveau_bios_run_init_table(dev, script, dcbent);
  3494. }
  3495. return 0;
  3496. }
  3497. int run_tmds_table(struct drm_device *dev, struct dcb_entry *dcbent, int head, int pxclk)
  3498. {
  3499. /*
  3500. * the pxclk parameter is in kHz
  3501. *
  3502. * This runs the TMDS regs setting code found on BIT bios cards
  3503. *
  3504. * For ffs(or) == 1 use the first table, for ffs(or) == 2 and
  3505. * ffs(or) == 3, use the second.
  3506. */
  3507. struct drm_nouveau_private *dev_priv = dev->dev_private;
  3508. struct nvbios *bios = &dev_priv->vbios;
  3509. int cv = bios->chip_version;
  3510. uint16_t clktable = 0, scriptptr;
  3511. uint32_t sel_clk_binding, sel_clk;
  3512. /* pre-nv17 off-chip tmds uses scripts, post nv17 doesn't */
  3513. if (cv >= 0x17 && cv != 0x1a && cv != 0x20 &&
  3514. dcbent->location != DCB_LOC_ON_CHIP)
  3515. return 0;
  3516. switch (ffs(dcbent->or)) {
  3517. case 1:
  3518. clktable = bios->tmds.output0_script_ptr;
  3519. break;
  3520. case 2:
  3521. case 3:
  3522. clktable = bios->tmds.output1_script_ptr;
  3523. break;
  3524. }
  3525. if (!clktable) {
  3526. NV_ERROR(dev, "Pixel clock comparison table not found\n");
  3527. return -EINVAL;
  3528. }
  3529. scriptptr = clkcmptable(bios, clktable, pxclk);
  3530. if (!scriptptr) {
  3531. NV_ERROR(dev, "TMDS output init script not found\n");
  3532. return -ENOENT;
  3533. }
  3534. /* don't let script change pll->head binding */
  3535. sel_clk_binding = bios_rd32(bios, NV_PRAMDAC_SEL_CLK) & 0x50000;
  3536. run_digital_op_script(dev, scriptptr, dcbent, head, pxclk >= 165000);
  3537. sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
  3538. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
  3539. return 0;
  3540. }
  3541. int get_pll_limits(struct drm_device *dev, uint32_t limit_match, struct pll_lims *pll_lim)
  3542. {
  3543. /*
  3544. * PLL limits table
  3545. *
  3546. * Version 0x10: NV30, NV31
  3547. * One byte header (version), one record of 24 bytes
  3548. * Version 0x11: NV36 - Not implemented
  3549. * Seems to have same record style as 0x10, but 3 records rather than 1
  3550. * Version 0x20: Found on Geforce 6 cards
  3551. * Trivial 4 byte BIT header. 31 (0x1f) byte record length
  3552. * Version 0x21: Found on Geforce 7, 8 and some Geforce 6 cards
  3553. * 5 byte header, fifth byte of unknown purpose. 35 (0x23) byte record
  3554. * length in general, some (integrated) have an extra configuration byte
  3555. * Version 0x30: Found on Geforce 8, separates the register mapping
  3556. * from the limits tables.
  3557. */
  3558. struct drm_nouveau_private *dev_priv = dev->dev_private;
  3559. struct nvbios *bios = &dev_priv->vbios;
  3560. int cv = bios->chip_version, pllindex = 0;
  3561. uint8_t pll_lim_ver = 0, headerlen = 0, recordlen = 0, entries = 0;
  3562. uint32_t crystal_strap_mask, crystal_straps;
  3563. if (!bios->pll_limit_tbl_ptr) {
  3564. if (cv == 0x30 || cv == 0x31 || cv == 0x35 || cv == 0x36 ||
  3565. cv >= 0x40) {
  3566. NV_ERROR(dev, "Pointer to PLL limits table invalid\n");
  3567. return -EINVAL;
  3568. }
  3569. } else
  3570. pll_lim_ver = bios->data[bios->pll_limit_tbl_ptr];
  3571. crystal_strap_mask = 1 << 6;
  3572. /* open coded dev->twoHeads test */
  3573. if (cv > 0x10 && cv != 0x15 && cv != 0x1a && cv != 0x20)
  3574. crystal_strap_mask |= 1 << 22;
  3575. crystal_straps = nvReadEXTDEV(dev, NV_PEXTDEV_BOOT_0) &
  3576. crystal_strap_mask;
  3577. switch (pll_lim_ver) {
  3578. /*
  3579. * We use version 0 to indicate a pre limit table bios (single stage
  3580. * pll) and load the hard coded limits instead.
  3581. */
  3582. case 0:
  3583. break;
  3584. case 0x10:
  3585. case 0x11:
  3586. /*
  3587. * Strictly v0x11 has 3 entries, but the last two don't seem
  3588. * to get used.
  3589. */
  3590. headerlen = 1;
  3591. recordlen = 0x18;
  3592. entries = 1;
  3593. pllindex = 0;
  3594. break;
  3595. case 0x20:
  3596. case 0x21:
  3597. case 0x30:
  3598. case 0x40:
  3599. headerlen = bios->data[bios->pll_limit_tbl_ptr + 1];
  3600. recordlen = bios->data[bios->pll_limit_tbl_ptr + 2];
  3601. entries = bios->data[bios->pll_limit_tbl_ptr + 3];
  3602. break;
  3603. default:
  3604. NV_ERROR(dev, "PLL limits table revision 0x%X not currently "
  3605. "supported\n", pll_lim_ver);
  3606. return -ENOSYS;
  3607. }
  3608. /* initialize all members to zero */
  3609. memset(pll_lim, 0, sizeof(struct pll_lims));
  3610. if (pll_lim_ver == 0x10 || pll_lim_ver == 0x11) {
  3611. uint8_t *pll_rec = &bios->data[bios->pll_limit_tbl_ptr + headerlen + recordlen * pllindex];
  3612. pll_lim->vco1.minfreq = ROM32(pll_rec[0]);
  3613. pll_lim->vco1.maxfreq = ROM32(pll_rec[4]);
  3614. pll_lim->vco2.minfreq = ROM32(pll_rec[8]);
  3615. pll_lim->vco2.maxfreq = ROM32(pll_rec[12]);
  3616. pll_lim->vco1.min_inputfreq = ROM32(pll_rec[16]);
  3617. pll_lim->vco2.min_inputfreq = ROM32(pll_rec[20]);
  3618. pll_lim->vco1.max_inputfreq = pll_lim->vco2.max_inputfreq = INT_MAX;
  3619. /* these values taken from nv30/31/36 */
  3620. pll_lim->vco1.min_n = 0x1;
  3621. if (cv == 0x36)
  3622. pll_lim->vco1.min_n = 0x5;
  3623. pll_lim->vco1.max_n = 0xff;
  3624. pll_lim->vco1.min_m = 0x1;
  3625. pll_lim->vco1.max_m = 0xd;
  3626. pll_lim->vco2.min_n = 0x4;
  3627. /*
  3628. * On nv30, 31, 36 (i.e. all cards with two stage PLLs with this
  3629. * table version (apart from nv35)), N2 is compared to
  3630. * maxN2 (0x46) and 10 * maxM2 (0x4), so set maxN2 to 0x28 and
  3631. * save a comparison
  3632. */
  3633. pll_lim->vco2.max_n = 0x28;
  3634. if (cv == 0x30 || cv == 0x35)
  3635. /* only 5 bits available for N2 on nv30/35 */
  3636. pll_lim->vco2.max_n = 0x1f;
  3637. pll_lim->vco2.min_m = 0x1;
  3638. pll_lim->vco2.max_m = 0x4;
  3639. pll_lim->max_log2p = 0x7;
  3640. pll_lim->max_usable_log2p = 0x6;
  3641. } else if (pll_lim_ver == 0x20 || pll_lim_ver == 0x21) {
  3642. uint16_t plloffs = bios->pll_limit_tbl_ptr + headerlen;
  3643. uint32_t reg = 0; /* default match */
  3644. uint8_t *pll_rec;
  3645. int i;
  3646. /*
  3647. * First entry is default match, if nothing better. warn if
  3648. * reg field nonzero
  3649. */
  3650. if (ROM32(bios->data[plloffs]))
  3651. NV_WARN(dev, "Default PLL limit entry has non-zero "
  3652. "register field\n");
  3653. if (limit_match > MAX_PLL_TYPES)
  3654. /* we've been passed a reg as the match */
  3655. reg = limit_match;
  3656. else /* limit match is a pll type */
  3657. for (i = 1; i < entries && !reg; i++) {
  3658. uint32_t cmpreg = ROM32(bios->data[plloffs + recordlen * i]);
  3659. if (limit_match == NVPLL &&
  3660. (cmpreg == NV_PRAMDAC_NVPLL_COEFF || cmpreg == 0x4000))
  3661. reg = cmpreg;
  3662. if (limit_match == MPLL &&
  3663. (cmpreg == NV_PRAMDAC_MPLL_COEFF || cmpreg == 0x4020))
  3664. reg = cmpreg;
  3665. if (limit_match == VPLL1 &&
  3666. (cmpreg == NV_PRAMDAC_VPLL_COEFF || cmpreg == 0x4010))
  3667. reg = cmpreg;
  3668. if (limit_match == VPLL2 &&
  3669. (cmpreg == NV_RAMDAC_VPLL2 || cmpreg == 0x4018))
  3670. reg = cmpreg;
  3671. }
  3672. for (i = 1; i < entries; i++)
  3673. if (ROM32(bios->data[plloffs + recordlen * i]) == reg) {
  3674. pllindex = i;
  3675. break;
  3676. }
  3677. pll_rec = &bios->data[plloffs + recordlen * pllindex];
  3678. BIOSLOG(bios, "Loading PLL limits for reg 0x%08x\n",
  3679. pllindex ? reg : 0);
  3680. /*
  3681. * Frequencies are stored in tables in MHz, kHz are more
  3682. * useful, so we convert.
  3683. */
  3684. /* What output frequencies can each VCO generate? */
  3685. pll_lim->vco1.minfreq = ROM16(pll_rec[4]) * 1000;
  3686. pll_lim->vco1.maxfreq = ROM16(pll_rec[6]) * 1000;
  3687. pll_lim->vco2.minfreq = ROM16(pll_rec[8]) * 1000;
  3688. pll_lim->vco2.maxfreq = ROM16(pll_rec[10]) * 1000;
  3689. /* What input frequencies they accept (past the m-divider)? */
  3690. pll_lim->vco1.min_inputfreq = ROM16(pll_rec[12]) * 1000;
  3691. pll_lim->vco2.min_inputfreq = ROM16(pll_rec[14]) * 1000;
  3692. pll_lim->vco1.max_inputfreq = ROM16(pll_rec[16]) * 1000;
  3693. pll_lim->vco2.max_inputfreq = ROM16(pll_rec[18]) * 1000;
  3694. /* What values are accepted as multiplier and divider? */
  3695. pll_lim->vco1.min_n = pll_rec[20];
  3696. pll_lim->vco1.max_n = pll_rec[21];
  3697. pll_lim->vco1.min_m = pll_rec[22];
  3698. pll_lim->vco1.max_m = pll_rec[23];
  3699. pll_lim->vco2.min_n = pll_rec[24];
  3700. pll_lim->vco2.max_n = pll_rec[25];
  3701. pll_lim->vco2.min_m = pll_rec[26];
  3702. pll_lim->vco2.max_m = pll_rec[27];
  3703. pll_lim->max_usable_log2p = pll_lim->max_log2p = pll_rec[29];
  3704. if (pll_lim->max_log2p > 0x7)
  3705. /* pll decoding in nv_hw.c assumes never > 7 */
  3706. NV_WARN(dev, "Max log2 P value greater than 7 (%d)\n",
  3707. pll_lim->max_log2p);
  3708. if (cv < 0x60)
  3709. pll_lim->max_usable_log2p = 0x6;
  3710. pll_lim->log2p_bias = pll_rec[30];
  3711. if (recordlen > 0x22)
  3712. pll_lim->refclk = ROM32(pll_rec[31]);
  3713. if (recordlen > 0x23 && pll_rec[35])
  3714. NV_WARN(dev,
  3715. "Bits set in PLL configuration byte (%x)\n",
  3716. pll_rec[35]);
  3717. /* C51 special not seen elsewhere */
  3718. if (cv == 0x51 && !pll_lim->refclk) {
  3719. uint32_t sel_clk = bios_rd32(bios, NV_PRAMDAC_SEL_CLK);
  3720. if (((limit_match == NV_PRAMDAC_VPLL_COEFF || limit_match == VPLL1) && sel_clk & 0x20) ||
  3721. ((limit_match == NV_RAMDAC_VPLL2 || limit_match == VPLL2) && sel_clk & 0x80)) {
  3722. if (bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_CHIP_ID_INDEX) < 0xa3)
  3723. pll_lim->refclk = 200000;
  3724. else
  3725. pll_lim->refclk = 25000;
  3726. }
  3727. }
  3728. } else if (pll_lim_ver == 0x30) { /* ver 0x30 */
  3729. uint8_t *entry = &bios->data[bios->pll_limit_tbl_ptr + headerlen];
  3730. uint8_t *record = NULL;
  3731. int i;
  3732. BIOSLOG(bios, "Loading PLL limits for register 0x%08x\n",
  3733. limit_match);
  3734. for (i = 0; i < entries; i++, entry += recordlen) {
  3735. if (ROM32(entry[3]) == limit_match) {
  3736. record = &bios->data[ROM16(entry[1])];
  3737. break;
  3738. }
  3739. }
  3740. if (!record) {
  3741. NV_ERROR(dev, "Register 0x%08x not found in PLL "
  3742. "limits table", limit_match);
  3743. return -ENOENT;
  3744. }
  3745. pll_lim->vco1.minfreq = ROM16(record[0]) * 1000;
  3746. pll_lim->vco1.maxfreq = ROM16(record[2]) * 1000;
  3747. pll_lim->vco2.minfreq = ROM16(record[4]) * 1000;
  3748. pll_lim->vco2.maxfreq = ROM16(record[6]) * 1000;
  3749. pll_lim->vco1.min_inputfreq = ROM16(record[8]) * 1000;
  3750. pll_lim->vco2.min_inputfreq = ROM16(record[10]) * 1000;
  3751. pll_lim->vco1.max_inputfreq = ROM16(record[12]) * 1000;
  3752. pll_lim->vco2.max_inputfreq = ROM16(record[14]) * 1000;
  3753. pll_lim->vco1.min_n = record[16];
  3754. pll_lim->vco1.max_n = record[17];
  3755. pll_lim->vco1.min_m = record[18];
  3756. pll_lim->vco1.max_m = record[19];
  3757. pll_lim->vco2.min_n = record[20];
  3758. pll_lim->vco2.max_n = record[21];
  3759. pll_lim->vco2.min_m = record[22];
  3760. pll_lim->vco2.max_m = record[23];
  3761. pll_lim->max_usable_log2p = pll_lim->max_log2p = record[25];
  3762. pll_lim->log2p_bias = record[27];
  3763. pll_lim->refclk = ROM32(record[28]);
  3764. } else if (pll_lim_ver) { /* ver 0x40 */
  3765. uint8_t *entry = &bios->data[bios->pll_limit_tbl_ptr + headerlen];
  3766. uint8_t *record = NULL;
  3767. int i;
  3768. BIOSLOG(bios, "Loading PLL limits for register 0x%08x\n",
  3769. limit_match);
  3770. for (i = 0; i < entries; i++, entry += recordlen) {
  3771. if (ROM32(entry[3]) == limit_match) {
  3772. record = &bios->data[ROM16(entry[1])];
  3773. break;
  3774. }
  3775. }
  3776. if (!record) {
  3777. NV_ERROR(dev, "Register 0x%08x not found in PLL "
  3778. "limits table", limit_match);
  3779. return -ENOENT;
  3780. }
  3781. pll_lim->vco1.minfreq = ROM16(record[0]) * 1000;
  3782. pll_lim->vco1.maxfreq = ROM16(record[2]) * 1000;
  3783. pll_lim->vco1.min_inputfreq = ROM16(record[4]) * 1000;
  3784. pll_lim->vco1.max_inputfreq = ROM16(record[6]) * 1000;
  3785. pll_lim->vco1.min_m = record[8];
  3786. pll_lim->vco1.max_m = record[9];
  3787. pll_lim->vco1.min_n = record[10];
  3788. pll_lim->vco1.max_n = record[11];
  3789. pll_lim->min_p = record[12];
  3790. pll_lim->max_p = record[13];
  3791. /* where did this go to?? */
  3792. if (limit_match == 0x00614100 || limit_match == 0x00614900)
  3793. pll_lim->refclk = 27000;
  3794. else
  3795. pll_lim->refclk = 100000;
  3796. }
  3797. /*
  3798. * By now any valid limit table ought to have set a max frequency for
  3799. * vco1, so if it's zero it's either a pre limit table bios, or one
  3800. * with an empty limit table (seen on nv18)
  3801. */
  3802. if (!pll_lim->vco1.maxfreq) {
  3803. pll_lim->vco1.minfreq = bios->fminvco;
  3804. pll_lim->vco1.maxfreq = bios->fmaxvco;
  3805. pll_lim->vco1.min_inputfreq = 0;
  3806. pll_lim->vco1.max_inputfreq = INT_MAX;
  3807. pll_lim->vco1.min_n = 0x1;
  3808. pll_lim->vco1.max_n = 0xff;
  3809. pll_lim->vco1.min_m = 0x1;
  3810. if (crystal_straps == 0) {
  3811. /* nv05 does this, nv11 doesn't, nv10 unknown */
  3812. if (cv < 0x11)
  3813. pll_lim->vco1.min_m = 0x7;
  3814. pll_lim->vco1.max_m = 0xd;
  3815. } else {
  3816. if (cv < 0x11)
  3817. pll_lim->vco1.min_m = 0x8;
  3818. pll_lim->vco1.max_m = 0xe;
  3819. }
  3820. if (cv < 0x17 || cv == 0x1a || cv == 0x20)
  3821. pll_lim->max_log2p = 4;
  3822. else
  3823. pll_lim->max_log2p = 5;
  3824. pll_lim->max_usable_log2p = pll_lim->max_log2p;
  3825. }
  3826. if (!pll_lim->refclk)
  3827. switch (crystal_straps) {
  3828. case 0:
  3829. pll_lim->refclk = 13500;
  3830. break;
  3831. case (1 << 6):
  3832. pll_lim->refclk = 14318;
  3833. break;
  3834. case (1 << 22):
  3835. pll_lim->refclk = 27000;
  3836. break;
  3837. case (1 << 22 | 1 << 6):
  3838. pll_lim->refclk = 25000;
  3839. break;
  3840. }
  3841. NV_DEBUG(dev, "pll.vco1.minfreq: %d\n", pll_lim->vco1.minfreq);
  3842. NV_DEBUG(dev, "pll.vco1.maxfreq: %d\n", pll_lim->vco1.maxfreq);
  3843. NV_DEBUG(dev, "pll.vco1.min_inputfreq: %d\n", pll_lim->vco1.min_inputfreq);
  3844. NV_DEBUG(dev, "pll.vco1.max_inputfreq: %d\n", pll_lim->vco1.max_inputfreq);
  3845. NV_DEBUG(dev, "pll.vco1.min_n: %d\n", pll_lim->vco1.min_n);
  3846. NV_DEBUG(dev, "pll.vco1.max_n: %d\n", pll_lim->vco1.max_n);
  3847. NV_DEBUG(dev, "pll.vco1.min_m: %d\n", pll_lim->vco1.min_m);
  3848. NV_DEBUG(dev, "pll.vco1.max_m: %d\n", pll_lim->vco1.max_m);
  3849. if (pll_lim->vco2.maxfreq) {
  3850. NV_DEBUG(dev, "pll.vco2.minfreq: %d\n", pll_lim->vco2.minfreq);
  3851. NV_DEBUG(dev, "pll.vco2.maxfreq: %d\n", pll_lim->vco2.maxfreq);
  3852. NV_DEBUG(dev, "pll.vco2.min_inputfreq: %d\n", pll_lim->vco2.min_inputfreq);
  3853. NV_DEBUG(dev, "pll.vco2.max_inputfreq: %d\n", pll_lim->vco2.max_inputfreq);
  3854. NV_DEBUG(dev, "pll.vco2.min_n: %d\n", pll_lim->vco2.min_n);
  3855. NV_DEBUG(dev, "pll.vco2.max_n: %d\n", pll_lim->vco2.max_n);
  3856. NV_DEBUG(dev, "pll.vco2.min_m: %d\n", pll_lim->vco2.min_m);
  3857. NV_DEBUG(dev, "pll.vco2.max_m: %d\n", pll_lim->vco2.max_m);
  3858. }
  3859. if (!pll_lim->max_p) {
  3860. NV_DEBUG(dev, "pll.max_log2p: %d\n", pll_lim->max_log2p);
  3861. NV_DEBUG(dev, "pll.log2p_bias: %d\n", pll_lim->log2p_bias);
  3862. } else {
  3863. NV_DEBUG(dev, "pll.min_p: %d\n", pll_lim->min_p);
  3864. NV_DEBUG(dev, "pll.max_p: %d\n", pll_lim->max_p);
  3865. }
  3866. NV_DEBUG(dev, "pll.refclk: %d\n", pll_lim->refclk);
  3867. return 0;
  3868. }
  3869. static void parse_bios_version(struct drm_device *dev, struct nvbios *bios, uint16_t offset)
  3870. {
  3871. /*
  3872. * offset + 0 (8 bits): Micro version
  3873. * offset + 1 (8 bits): Minor version
  3874. * offset + 2 (8 bits): Chip version
  3875. * offset + 3 (8 bits): Major version
  3876. */
  3877. bios->major_version = bios->data[offset + 3];
  3878. bios->chip_version = bios->data[offset + 2];
  3879. NV_TRACE(dev, "Bios version %02x.%02x.%02x.%02x\n",
  3880. bios->data[offset + 3], bios->data[offset + 2],
  3881. bios->data[offset + 1], bios->data[offset]);
  3882. }
  3883. static void parse_script_table_pointers(struct nvbios *bios, uint16_t offset)
  3884. {
  3885. /*
  3886. * Parses the init table segment for pointers used in script execution.
  3887. *
  3888. * offset + 0 (16 bits): init script tables pointer
  3889. * offset + 2 (16 bits): macro index table pointer
  3890. * offset + 4 (16 bits): macro table pointer
  3891. * offset + 6 (16 bits): condition table pointer
  3892. * offset + 8 (16 bits): io condition table pointer
  3893. * offset + 10 (16 bits): io flag condition table pointer
  3894. * offset + 12 (16 bits): init function table pointer
  3895. */
  3896. bios->init_script_tbls_ptr = ROM16(bios->data[offset]);
  3897. bios->macro_index_tbl_ptr = ROM16(bios->data[offset + 2]);
  3898. bios->macro_tbl_ptr = ROM16(bios->data[offset + 4]);
  3899. bios->condition_tbl_ptr = ROM16(bios->data[offset + 6]);
  3900. bios->io_condition_tbl_ptr = ROM16(bios->data[offset + 8]);
  3901. bios->io_flag_condition_tbl_ptr = ROM16(bios->data[offset + 10]);
  3902. bios->init_function_tbl_ptr = ROM16(bios->data[offset + 12]);
  3903. }
  3904. static int parse_bit_A_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  3905. {
  3906. /*
  3907. * Parses the load detect values for g80 cards.
  3908. *
  3909. * offset + 0 (16 bits): loadval table pointer
  3910. */
  3911. uint16_t load_table_ptr;
  3912. uint8_t version, headerlen, entrylen, num_entries;
  3913. if (bitentry->length != 3) {
  3914. NV_ERROR(dev, "Do not understand BIT A table\n");
  3915. return -EINVAL;
  3916. }
  3917. load_table_ptr = ROM16(bios->data[bitentry->offset]);
  3918. if (load_table_ptr == 0x0) {
  3919. NV_ERROR(dev, "Pointer to BIT loadval table invalid\n");
  3920. return -EINVAL;
  3921. }
  3922. version = bios->data[load_table_ptr];
  3923. if (version != 0x10) {
  3924. NV_ERROR(dev, "BIT loadval table version %d.%d not supported\n",
  3925. version >> 4, version & 0xF);
  3926. return -ENOSYS;
  3927. }
  3928. headerlen = bios->data[load_table_ptr + 1];
  3929. entrylen = bios->data[load_table_ptr + 2];
  3930. num_entries = bios->data[load_table_ptr + 3];
  3931. if (headerlen != 4 || entrylen != 4 || num_entries != 2) {
  3932. NV_ERROR(dev, "Do not understand BIT loadval table\n");
  3933. return -EINVAL;
  3934. }
  3935. /* First entry is normal dac, 2nd tv-out perhaps? */
  3936. bios->dactestval = ROM32(bios->data[load_table_ptr + headerlen]) & 0x3ff;
  3937. return 0;
  3938. }
  3939. static int parse_bit_C_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  3940. {
  3941. /*
  3942. * offset + 8 (16 bits): PLL limits table pointer
  3943. *
  3944. * There's more in here, but that's unknown.
  3945. */
  3946. if (bitentry->length < 10) {
  3947. NV_ERROR(dev, "Do not understand BIT C table\n");
  3948. return -EINVAL;
  3949. }
  3950. bios->pll_limit_tbl_ptr = ROM16(bios->data[bitentry->offset + 8]);
  3951. return 0;
  3952. }
  3953. static int parse_bit_display_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  3954. {
  3955. /*
  3956. * Parses the flat panel table segment that the bit entry points to.
  3957. * Starting at bitentry->offset:
  3958. *
  3959. * offset + 0 (16 bits): ??? table pointer - seems to have 18 byte
  3960. * records beginning with a freq.
  3961. * offset + 2 (16 bits): mode table pointer
  3962. */
  3963. if (bitentry->length != 4) {
  3964. NV_ERROR(dev, "Do not understand BIT display table\n");
  3965. return -EINVAL;
  3966. }
  3967. bios->fp.fptablepointer = ROM16(bios->data[bitentry->offset + 2]);
  3968. return 0;
  3969. }
  3970. static int parse_bit_init_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  3971. {
  3972. /*
  3973. * Parses the init table segment that the bit entry points to.
  3974. *
  3975. * See parse_script_table_pointers for layout
  3976. */
  3977. if (bitentry->length < 14) {
  3978. NV_ERROR(dev, "Do not understand init table\n");
  3979. return -EINVAL;
  3980. }
  3981. parse_script_table_pointers(bios, bitentry->offset);
  3982. if (bitentry->length >= 16)
  3983. bios->some_script_ptr = ROM16(bios->data[bitentry->offset + 14]);
  3984. if (bitentry->length >= 18)
  3985. bios->init96_tbl_ptr = ROM16(bios->data[bitentry->offset + 16]);
  3986. return 0;
  3987. }
  3988. static int parse_bit_i_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  3989. {
  3990. /*
  3991. * BIT 'i' (info?) table
  3992. *
  3993. * offset + 0 (32 bits): BIOS version dword (as in B table)
  3994. * offset + 5 (8 bits): BIOS feature byte (same as for BMP?)
  3995. * offset + 13 (16 bits): pointer to table containing DAC load
  3996. * detection comparison values
  3997. *
  3998. * There's other things in the table, purpose unknown
  3999. */
  4000. uint16_t daccmpoffset;
  4001. uint8_t dacver, dacheaderlen;
  4002. if (bitentry->length < 6) {
  4003. NV_ERROR(dev, "BIT i table too short for needed information\n");
  4004. return -EINVAL;
  4005. }
  4006. parse_bios_version(dev, bios, bitentry->offset);
  4007. /*
  4008. * bit 4 seems to indicate a mobile bios (doesn't suffer from BMP's
  4009. * Quadro identity crisis), other bits possibly as for BMP feature byte
  4010. */
  4011. bios->feature_byte = bios->data[bitentry->offset + 5];
  4012. bios->is_mobile = bios->feature_byte & FEATURE_MOBILE;
  4013. if (bitentry->length < 15) {
  4014. NV_WARN(dev, "BIT i table not long enough for DAC load "
  4015. "detection comparison table\n");
  4016. return -EINVAL;
  4017. }
  4018. daccmpoffset = ROM16(bios->data[bitentry->offset + 13]);
  4019. /* doesn't exist on g80 */
  4020. if (!daccmpoffset)
  4021. return 0;
  4022. /*
  4023. * The first value in the table, following the header, is the
  4024. * comparison value, the second entry is a comparison value for
  4025. * TV load detection.
  4026. */
  4027. dacver = bios->data[daccmpoffset];
  4028. dacheaderlen = bios->data[daccmpoffset + 1];
  4029. if (dacver != 0x00 && dacver != 0x10) {
  4030. NV_WARN(dev, "DAC load detection comparison table version "
  4031. "%d.%d not known\n", dacver >> 4, dacver & 0xf);
  4032. return -ENOSYS;
  4033. }
  4034. bios->dactestval = ROM32(bios->data[daccmpoffset + dacheaderlen]);
  4035. bios->tvdactestval = ROM32(bios->data[daccmpoffset + dacheaderlen + 4]);
  4036. return 0;
  4037. }
  4038. static int parse_bit_lvds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  4039. {
  4040. /*
  4041. * Parses the LVDS table segment that the bit entry points to.
  4042. * Starting at bitentry->offset:
  4043. *
  4044. * offset + 0 (16 bits): LVDS strap xlate table pointer
  4045. */
  4046. if (bitentry->length != 2) {
  4047. NV_ERROR(dev, "Do not understand BIT LVDS table\n");
  4048. return -EINVAL;
  4049. }
  4050. /*
  4051. * No idea if it's still called the LVDS manufacturer table, but
  4052. * the concept's close enough.
  4053. */
  4054. bios->fp.lvdsmanufacturerpointer = ROM16(bios->data[bitentry->offset]);
  4055. return 0;
  4056. }
  4057. static int
  4058. parse_bit_M_tbl_entry(struct drm_device *dev, struct nvbios *bios,
  4059. struct bit_entry *bitentry)
  4060. {
  4061. /*
  4062. * offset + 2 (8 bits): number of options in an
  4063. * INIT_RAM_RESTRICT_ZM_REG_GROUP opcode option set
  4064. * offset + 3 (16 bits): pointer to strap xlate table for RAM
  4065. * restrict option selection
  4066. *
  4067. * There's a bunch of bits in this table other than the RAM restrict
  4068. * stuff that we don't use - their use currently unknown
  4069. */
  4070. /*
  4071. * Older bios versions don't have a sufficiently long table for
  4072. * what we want
  4073. */
  4074. if (bitentry->length < 0x5)
  4075. return 0;
  4076. if (bitentry->id[1] < 2) {
  4077. bios->ram_restrict_group_count = bios->data[bitentry->offset + 2];
  4078. bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 3]);
  4079. } else {
  4080. bios->ram_restrict_group_count = bios->data[bitentry->offset + 0];
  4081. bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 1]);
  4082. }
  4083. return 0;
  4084. }
  4085. static int parse_bit_tmds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
  4086. {
  4087. /*
  4088. * Parses the pointer to the TMDS table
  4089. *
  4090. * Starting at bitentry->offset:
  4091. *
  4092. * offset + 0 (16 bits): TMDS table pointer
  4093. *
  4094. * The TMDS table is typically found just before the DCB table, with a
  4095. * characteristic signature of 0x11,0x13 (1.1 being version, 0x13 being
  4096. * length?)
  4097. *
  4098. * At offset +7 is a pointer to a script, which I don't know how to
  4099. * run yet.
  4100. * At offset +9 is a pointer to another script, likewise
  4101. * Offset +11 has a pointer to a table where the first word is a pxclk
  4102. * frequency and the second word a pointer to a script, which should be
  4103. * run if the comparison pxclk frequency is less than the pxclk desired.
  4104. * This repeats for decreasing comparison frequencies
  4105. * Offset +13 has a pointer to a similar table
  4106. * The selection of table (and possibly +7/+9 script) is dictated by
  4107. * "or" from the DCB.
  4108. */
  4109. uint16_t tmdstableptr, script1, script2;
  4110. if (bitentry->length != 2) {
  4111. NV_ERROR(dev, "Do not understand BIT TMDS table\n");
  4112. return -EINVAL;
  4113. }
  4114. tmdstableptr = ROM16(bios->data[bitentry->offset]);
  4115. if (tmdstableptr == 0x0) {
  4116. NV_ERROR(dev, "Pointer to TMDS table invalid\n");
  4117. return -EINVAL;
  4118. }
  4119. /* nv50+ has v2.0, but we don't parse it atm */
  4120. if (bios->data[tmdstableptr] != 0x11) {
  4121. NV_WARN(dev,
  4122. "TMDS table revision %d.%d not currently supported\n",
  4123. bios->data[tmdstableptr] >> 4, bios->data[tmdstableptr] & 0xf);
  4124. return -ENOSYS;
  4125. }
  4126. /*
  4127. * These two scripts are odd: they don't seem to get run even when
  4128. * they are not stubbed.
  4129. */
  4130. script1 = ROM16(bios->data[tmdstableptr + 7]);
  4131. script2 = ROM16(bios->data[tmdstableptr + 9]);
  4132. if (bios->data[script1] != 'q' || bios->data[script2] != 'q')
  4133. NV_WARN(dev, "TMDS table script pointers not stubbed\n");
  4134. bios->tmds.output0_script_ptr = ROM16(bios->data[tmdstableptr + 11]);
  4135. bios->tmds.output1_script_ptr = ROM16(bios->data[tmdstableptr + 13]);
  4136. return 0;
  4137. }
  4138. static int
  4139. parse_bit_U_tbl_entry(struct drm_device *dev, struct nvbios *bios,
  4140. struct bit_entry *bitentry)
  4141. {
  4142. /*
  4143. * Parses the pointer to the G80 output script tables
  4144. *
  4145. * Starting at bitentry->offset:
  4146. *
  4147. * offset + 0 (16 bits): output script table pointer
  4148. */
  4149. uint16_t outputscripttableptr;
  4150. if (bitentry->length != 3) {
  4151. NV_ERROR(dev, "Do not understand BIT U table\n");
  4152. return -EINVAL;
  4153. }
  4154. outputscripttableptr = ROM16(bios->data[bitentry->offset]);
  4155. bios->display.script_table_ptr = outputscripttableptr;
  4156. return 0;
  4157. }
  4158. static int
  4159. parse_bit_displayport_tbl_entry(struct drm_device *dev, struct nvbios *bios,
  4160. struct bit_entry *bitentry)
  4161. {
  4162. bios->display.dp_table_ptr = ROM16(bios->data[bitentry->offset]);
  4163. return 0;
  4164. }
  4165. struct bit_table {
  4166. const char id;
  4167. int (* const parse_fn)(struct drm_device *, struct nvbios *, struct bit_entry *);
  4168. };
  4169. #define BIT_TABLE(id, funcid) ((struct bit_table){ id, parse_bit_##funcid##_tbl_entry })
  4170. static int
  4171. parse_bit_table(struct nvbios *bios, const uint16_t bitoffset,
  4172. struct bit_table *table)
  4173. {
  4174. struct drm_device *dev = bios->dev;
  4175. uint8_t maxentries = bios->data[bitoffset + 4];
  4176. int i, offset;
  4177. struct bit_entry bitentry;
  4178. for (i = 0, offset = bitoffset + 6; i < maxentries; i++, offset += 6) {
  4179. bitentry.id[0] = bios->data[offset];
  4180. if (bitentry.id[0] != table->id)
  4181. continue;
  4182. bitentry.id[1] = bios->data[offset + 1];
  4183. bitentry.length = ROM16(bios->data[offset + 2]);
  4184. bitentry.offset = ROM16(bios->data[offset + 4]);
  4185. return table->parse_fn(dev, bios, &bitentry);
  4186. }
  4187. NV_INFO(dev, "BIT table '%c' not found\n", table->id);
  4188. return -ENOSYS;
  4189. }
  4190. static int
  4191. parse_bit_structure(struct nvbios *bios, const uint16_t bitoffset)
  4192. {
  4193. int ret;
  4194. /*
  4195. * The only restriction on parsing order currently is having 'i' first
  4196. * for use of bios->*_version or bios->feature_byte while parsing;
  4197. * functions shouldn't be actually *doing* anything apart from pulling
  4198. * data from the image into the bios struct, thus no interdependencies
  4199. */
  4200. ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('i', i));
  4201. if (ret) /* info? */
  4202. return ret;
  4203. if (bios->major_version >= 0x60) /* g80+ */
  4204. parse_bit_table(bios, bitoffset, &BIT_TABLE('A', A));
  4205. ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('C', C));
  4206. if (ret)
  4207. return ret;
  4208. parse_bit_table(bios, bitoffset, &BIT_TABLE('D', display));
  4209. ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('I', init));
  4210. if (ret)
  4211. return ret;
  4212. parse_bit_table(bios, bitoffset, &BIT_TABLE('M', M)); /* memory? */
  4213. parse_bit_table(bios, bitoffset, &BIT_TABLE('L', lvds));
  4214. parse_bit_table(bios, bitoffset, &BIT_TABLE('T', tmds));
  4215. parse_bit_table(bios, bitoffset, &BIT_TABLE('U', U));
  4216. parse_bit_table(bios, bitoffset, &BIT_TABLE('d', displayport));
  4217. return 0;
  4218. }
  4219. static int parse_bmp_structure(struct drm_device *dev, struct nvbios *bios, unsigned int offset)
  4220. {
  4221. /*
  4222. * Parses the BMP structure for useful things, but does not act on them
  4223. *
  4224. * offset + 5: BMP major version
  4225. * offset + 6: BMP minor version
  4226. * offset + 9: BMP feature byte
  4227. * offset + 10: BCD encoded BIOS version
  4228. *
  4229. * offset + 18: init script table pointer (for bios versions < 5.10h)
  4230. * offset + 20: extra init script table pointer (for bios
  4231. * versions < 5.10h)
  4232. *
  4233. * offset + 24: memory init table pointer (used on early bios versions)
  4234. * offset + 26: SDR memory sequencing setup data table
  4235. * offset + 28: DDR memory sequencing setup data table
  4236. *
  4237. * offset + 54: index of I2C CRTC pair to use for CRT output
  4238. * offset + 55: index of I2C CRTC pair to use for TV output
  4239. * offset + 56: index of I2C CRTC pair to use for flat panel output
  4240. * offset + 58: write CRTC index for I2C pair 0
  4241. * offset + 59: read CRTC index for I2C pair 0
  4242. * offset + 60: write CRTC index for I2C pair 1
  4243. * offset + 61: read CRTC index for I2C pair 1
  4244. *
  4245. * offset + 67: maximum internal PLL frequency (single stage PLL)
  4246. * offset + 71: minimum internal PLL frequency (single stage PLL)
  4247. *
  4248. * offset + 75: script table pointers, as described in
  4249. * parse_script_table_pointers
  4250. *
  4251. * offset + 89: TMDS single link output A table pointer
  4252. * offset + 91: TMDS single link output B table pointer
  4253. * offset + 95: LVDS single link output A table pointer
  4254. * offset + 105: flat panel timings table pointer
  4255. * offset + 107: flat panel strapping translation table pointer
  4256. * offset + 117: LVDS manufacturer panel config table pointer
  4257. * offset + 119: LVDS manufacturer strapping translation table pointer
  4258. *
  4259. * offset + 142: PLL limits table pointer
  4260. *
  4261. * offset + 156: minimum pixel clock for LVDS dual link
  4262. */
  4263. uint8_t *bmp = &bios->data[offset], bmp_version_major, bmp_version_minor;
  4264. uint16_t bmplength;
  4265. uint16_t legacy_scripts_offset, legacy_i2c_offset;
  4266. /* load needed defaults in case we can't parse this info */
  4267. bios->dcb.i2c[0].write = NV_CIO_CRE_DDC_WR__INDEX;
  4268. bios->dcb.i2c[0].read = NV_CIO_CRE_DDC_STATUS__INDEX;
  4269. bios->dcb.i2c[1].write = NV_CIO_CRE_DDC0_WR__INDEX;
  4270. bios->dcb.i2c[1].read = NV_CIO_CRE_DDC0_STATUS__INDEX;
  4271. bios->digital_min_front_porch = 0x4b;
  4272. bios->fmaxvco = 256000;
  4273. bios->fminvco = 128000;
  4274. bios->fp.duallink_transition_clk = 90000;
  4275. bmp_version_major = bmp[5];
  4276. bmp_version_minor = bmp[6];
  4277. NV_TRACE(dev, "BMP version %d.%d\n",
  4278. bmp_version_major, bmp_version_minor);
  4279. /*
  4280. * Make sure that 0x36 is blank and can't be mistaken for a DCB
  4281. * pointer on early versions
  4282. */
  4283. if (bmp_version_major < 5)
  4284. *(uint16_t *)&bios->data[0x36] = 0;
  4285. /*
  4286. * Seems that the minor version was 1 for all major versions prior
  4287. * to 5. Version 6 could theoretically exist, but I suspect BIT
  4288. * happened instead.
  4289. */
  4290. if ((bmp_version_major < 5 && bmp_version_minor != 1) || bmp_version_major > 5) {
  4291. NV_ERROR(dev, "You have an unsupported BMP version. "
  4292. "Please send in your bios\n");
  4293. return -ENOSYS;
  4294. }
  4295. if (bmp_version_major == 0)
  4296. /* nothing that's currently useful in this version */
  4297. return 0;
  4298. else if (bmp_version_major == 1)
  4299. bmplength = 44; /* exact for 1.01 */
  4300. else if (bmp_version_major == 2)
  4301. bmplength = 48; /* exact for 2.01 */
  4302. else if (bmp_version_major == 3)
  4303. bmplength = 54;
  4304. /* guessed - mem init tables added in this version */
  4305. else if (bmp_version_major == 4 || bmp_version_minor < 0x1)
  4306. /* don't know if 5.0 exists... */
  4307. bmplength = 62;
  4308. /* guessed - BMP I2C indices added in version 4*/
  4309. else if (bmp_version_minor < 0x6)
  4310. bmplength = 67; /* exact for 5.01 */
  4311. else if (bmp_version_minor < 0x10)
  4312. bmplength = 75; /* exact for 5.06 */
  4313. else if (bmp_version_minor == 0x10)
  4314. bmplength = 89; /* exact for 5.10h */
  4315. else if (bmp_version_minor < 0x14)
  4316. bmplength = 118; /* exact for 5.11h */
  4317. else if (bmp_version_minor < 0x24)
  4318. /*
  4319. * Not sure of version where pll limits came in;
  4320. * certainly exist by 0x24 though.
  4321. */
  4322. /* length not exact: this is long enough to get lvds members */
  4323. bmplength = 123;
  4324. else if (bmp_version_minor < 0x27)
  4325. /*
  4326. * Length not exact: this is long enough to get pll limit
  4327. * member
  4328. */
  4329. bmplength = 144;
  4330. else
  4331. /*
  4332. * Length not exact: this is long enough to get dual link
  4333. * transition clock.
  4334. */
  4335. bmplength = 158;
  4336. /* checksum */
  4337. if (nv_cksum(bmp, 8)) {
  4338. NV_ERROR(dev, "Bad BMP checksum\n");
  4339. return -EINVAL;
  4340. }
  4341. /*
  4342. * Bit 4 seems to indicate either a mobile bios or a quadro card --
  4343. * mobile behaviour consistent (nv11+), quadro only seen nv18gl-nv36gl
  4344. * (not nv10gl), bit 5 that the flat panel tables are present, and
  4345. * bit 6 a tv bios.
  4346. */
  4347. bios->feature_byte = bmp[9];
  4348. parse_bios_version(dev, bios, offset + 10);
  4349. if (bmp_version_major < 5 || bmp_version_minor < 0x10)
  4350. bios->old_style_init = true;
  4351. legacy_scripts_offset = 18;
  4352. if (bmp_version_major < 2)
  4353. legacy_scripts_offset -= 4;
  4354. bios->init_script_tbls_ptr = ROM16(bmp[legacy_scripts_offset]);
  4355. bios->extra_init_script_tbl_ptr = ROM16(bmp[legacy_scripts_offset + 2]);
  4356. if (bmp_version_major > 2) { /* appears in BMP 3 */
  4357. bios->legacy.mem_init_tbl_ptr = ROM16(bmp[24]);
  4358. bios->legacy.sdr_seq_tbl_ptr = ROM16(bmp[26]);
  4359. bios->legacy.ddr_seq_tbl_ptr = ROM16(bmp[28]);
  4360. }
  4361. legacy_i2c_offset = 0x48; /* BMP version 2 & 3 */
  4362. if (bmplength > 61)
  4363. legacy_i2c_offset = offset + 54;
  4364. bios->legacy.i2c_indices.crt = bios->data[legacy_i2c_offset];
  4365. bios->legacy.i2c_indices.tv = bios->data[legacy_i2c_offset + 1];
  4366. bios->legacy.i2c_indices.panel = bios->data[legacy_i2c_offset + 2];
  4367. if (bios->data[legacy_i2c_offset + 4])
  4368. bios->dcb.i2c[0].write = bios->data[legacy_i2c_offset + 4];
  4369. if (bios->data[legacy_i2c_offset + 5])
  4370. bios->dcb.i2c[0].read = bios->data[legacy_i2c_offset + 5];
  4371. if (bios->data[legacy_i2c_offset + 6])
  4372. bios->dcb.i2c[1].write = bios->data[legacy_i2c_offset + 6];
  4373. if (bios->data[legacy_i2c_offset + 7])
  4374. bios->dcb.i2c[1].read = bios->data[legacy_i2c_offset + 7];
  4375. if (bmplength > 74) {
  4376. bios->fmaxvco = ROM32(bmp[67]);
  4377. bios->fminvco = ROM32(bmp[71]);
  4378. }
  4379. if (bmplength > 88)
  4380. parse_script_table_pointers(bios, offset + 75);
  4381. if (bmplength > 94) {
  4382. bios->tmds.output0_script_ptr = ROM16(bmp[89]);
  4383. bios->tmds.output1_script_ptr = ROM16(bmp[91]);
  4384. /*
  4385. * Never observed in use with lvds scripts, but is reused for
  4386. * 18/24 bit panel interface default for EDID equipped panels
  4387. * (if_is_24bit not set directly to avoid any oscillation).
  4388. */
  4389. bios->legacy.lvds_single_a_script_ptr = ROM16(bmp[95]);
  4390. }
  4391. if (bmplength > 108) {
  4392. bios->fp.fptablepointer = ROM16(bmp[105]);
  4393. bios->fp.fpxlatetableptr = ROM16(bmp[107]);
  4394. bios->fp.xlatwidth = 1;
  4395. }
  4396. if (bmplength > 120) {
  4397. bios->fp.lvdsmanufacturerpointer = ROM16(bmp[117]);
  4398. bios->fp.fpxlatemanufacturertableptr = ROM16(bmp[119]);
  4399. }
  4400. if (bmplength > 143)
  4401. bios->pll_limit_tbl_ptr = ROM16(bmp[142]);
  4402. if (bmplength > 157)
  4403. bios->fp.duallink_transition_clk = ROM16(bmp[156]) * 10;
  4404. return 0;
  4405. }
  4406. static uint16_t findstr(uint8_t *data, int n, const uint8_t *str, int len)
  4407. {
  4408. int i, j;
  4409. for (i = 0; i <= (n - len); i++) {
  4410. for (j = 0; j < len; j++)
  4411. if (data[i + j] != str[j])
  4412. break;
  4413. if (j == len)
  4414. return i;
  4415. }
  4416. return 0;
  4417. }
  4418. static struct dcb_gpio_entry *
  4419. new_gpio_entry(struct nvbios *bios)
  4420. {
  4421. struct dcb_gpio_table *gpio = &bios->dcb.gpio;
  4422. return &gpio->entry[gpio->entries++];
  4423. }
  4424. struct dcb_gpio_entry *
  4425. nouveau_bios_gpio_entry(struct drm_device *dev, enum dcb_gpio_tag tag)
  4426. {
  4427. struct drm_nouveau_private *dev_priv = dev->dev_private;
  4428. struct nvbios *bios = &dev_priv->vbios;
  4429. int i;
  4430. for (i = 0; i < bios->dcb.gpio.entries; i++) {
  4431. if (bios->dcb.gpio.entry[i].tag != tag)
  4432. continue;
  4433. return &bios->dcb.gpio.entry[i];
  4434. }
  4435. return NULL;
  4436. }
  4437. static void
  4438. parse_dcb30_gpio_entry(struct nvbios *bios, uint16_t offset)
  4439. {
  4440. struct dcb_gpio_entry *gpio;
  4441. uint16_t ent = ROM16(bios->data[offset]);
  4442. uint8_t line = ent & 0x1f,
  4443. tag = ent >> 5 & 0x3f,
  4444. flags = ent >> 11 & 0x1f;
  4445. if (tag == 0x3f)
  4446. return;
  4447. gpio = new_gpio_entry(bios);
  4448. gpio->tag = tag;
  4449. gpio->line = line;
  4450. gpio->invert = flags != 4;
  4451. gpio->entry = ent;
  4452. }
  4453. static void
  4454. parse_dcb40_gpio_entry(struct nvbios *bios, uint16_t offset)
  4455. {
  4456. uint32_t entry = ROM32(bios->data[offset]);
  4457. struct dcb_gpio_entry *gpio;
  4458. if ((entry & 0x0000ff00) == 0x0000ff00)
  4459. return;
  4460. gpio = new_gpio_entry(bios);
  4461. gpio->tag = (entry & 0x0000ff00) >> 8;
  4462. gpio->line = (entry & 0x0000001f) >> 0;
  4463. gpio->state_default = (entry & 0x01000000) >> 24;
  4464. gpio->state[0] = (entry & 0x18000000) >> 27;
  4465. gpio->state[1] = (entry & 0x60000000) >> 29;
  4466. gpio->entry = entry;
  4467. }
  4468. static void
  4469. parse_dcb_gpio_table(struct nvbios *bios)
  4470. {
  4471. struct drm_device *dev = bios->dev;
  4472. uint16_t gpio_table_ptr = bios->dcb.gpio_table_ptr;
  4473. uint8_t *gpio_table = &bios->data[gpio_table_ptr];
  4474. int header_len = gpio_table[1],
  4475. entries = gpio_table[2],
  4476. entry_len = gpio_table[3];
  4477. void (*parse_entry)(struct nvbios *, uint16_t) = NULL;
  4478. int i;
  4479. if (bios->dcb.version >= 0x40) {
  4480. if (gpio_table_ptr && entry_len != 4) {
  4481. NV_WARN(dev, "Invalid DCB GPIO table entry length.\n");
  4482. return;
  4483. }
  4484. parse_entry = parse_dcb40_gpio_entry;
  4485. } else if (bios->dcb.version >= 0x30) {
  4486. if (gpio_table_ptr && entry_len != 2) {
  4487. NV_WARN(dev, "Invalid DCB GPIO table entry length.\n");
  4488. return;
  4489. }
  4490. parse_entry = parse_dcb30_gpio_entry;
  4491. } else if (bios->dcb.version >= 0x22) {
  4492. /*
  4493. * DCBs older than v3.0 don't really have a GPIO
  4494. * table, instead they keep some GPIO info at fixed
  4495. * locations.
  4496. */
  4497. uint16_t dcbptr = ROM16(bios->data[0x36]);
  4498. uint8_t *tvdac_gpio = &bios->data[dcbptr - 5];
  4499. if (tvdac_gpio[0] & 1) {
  4500. struct dcb_gpio_entry *gpio = new_gpio_entry(bios);
  4501. gpio->tag = DCB_GPIO_TVDAC0;
  4502. gpio->line = tvdac_gpio[1] >> 4;
  4503. gpio->invert = tvdac_gpio[0] & 2;
  4504. }
  4505. }
  4506. if (!gpio_table_ptr)
  4507. return;
  4508. if (entries > DCB_MAX_NUM_GPIO_ENTRIES) {
  4509. NV_WARN(dev, "Too many entries in the DCB GPIO table.\n");
  4510. entries = DCB_MAX_NUM_GPIO_ENTRIES;
  4511. }
  4512. for (i = 0; i < entries; i++)
  4513. parse_entry(bios, gpio_table_ptr + header_len + entry_len * i);
  4514. }
  4515. struct dcb_connector_table_entry *
  4516. nouveau_bios_connector_entry(struct drm_device *dev, int index)
  4517. {
  4518. struct drm_nouveau_private *dev_priv = dev->dev_private;
  4519. struct nvbios *bios = &dev_priv->vbios;
  4520. struct dcb_connector_table_entry *cte;
  4521. if (index >= bios->dcb.connector.entries)
  4522. return NULL;
  4523. cte = &bios->dcb.connector.entry[index];
  4524. if (cte->type == 0xff)
  4525. return NULL;
  4526. return cte;
  4527. }
  4528. static enum dcb_connector_type
  4529. divine_connector_type(struct nvbios *bios, int index)
  4530. {
  4531. struct dcb_table *dcb = &bios->dcb;
  4532. unsigned encoders = 0, type = DCB_CONNECTOR_NONE;
  4533. int i;
  4534. for (i = 0; i < dcb->entries; i++) {
  4535. if (dcb->entry[i].connector == index)
  4536. encoders |= (1 << dcb->entry[i].type);
  4537. }
  4538. if (encoders & (1 << OUTPUT_DP)) {
  4539. if (encoders & (1 << OUTPUT_TMDS))
  4540. type = DCB_CONNECTOR_DP;
  4541. else
  4542. type = DCB_CONNECTOR_eDP;
  4543. } else
  4544. if (encoders & (1 << OUTPUT_TMDS)) {
  4545. if (encoders & (1 << OUTPUT_ANALOG))
  4546. type = DCB_CONNECTOR_DVI_I;
  4547. else
  4548. type = DCB_CONNECTOR_DVI_D;
  4549. } else
  4550. if (encoders & (1 << OUTPUT_ANALOG)) {
  4551. type = DCB_CONNECTOR_VGA;
  4552. } else
  4553. if (encoders & (1 << OUTPUT_LVDS)) {
  4554. type = DCB_CONNECTOR_LVDS;
  4555. } else
  4556. if (encoders & (1 << OUTPUT_TV)) {
  4557. type = DCB_CONNECTOR_TV_0;
  4558. }
  4559. return type;
  4560. }
  4561. static void
  4562. apply_dcb_connector_quirks(struct nvbios *bios, int idx)
  4563. {
  4564. struct dcb_connector_table_entry *cte = &bios->dcb.connector.entry[idx];
  4565. struct drm_device *dev = bios->dev;
  4566. /* Gigabyte NX85T */
  4567. if ((dev->pdev->device == 0x0421) &&
  4568. (dev->pdev->subsystem_vendor == 0x1458) &&
  4569. (dev->pdev->subsystem_device == 0x344c)) {
  4570. if (cte->type == DCB_CONNECTOR_HDMI_1)
  4571. cte->type = DCB_CONNECTOR_DVI_I;
  4572. }
  4573. }
  4574. static void
  4575. parse_dcb_connector_table(struct nvbios *bios)
  4576. {
  4577. struct drm_device *dev = bios->dev;
  4578. struct dcb_connector_table *ct = &bios->dcb.connector;
  4579. struct dcb_connector_table_entry *cte;
  4580. uint8_t *conntab = &bios->data[bios->dcb.connector_table_ptr];
  4581. uint8_t *entry;
  4582. int i;
  4583. if (!bios->dcb.connector_table_ptr) {
  4584. NV_DEBUG_KMS(dev, "No DCB connector table present\n");
  4585. return;
  4586. }
  4587. NV_INFO(dev, "DCB connector table: VHER 0x%02x %d %d %d\n",
  4588. conntab[0], conntab[1], conntab[2], conntab[3]);
  4589. if ((conntab[0] != 0x30 && conntab[0] != 0x40) ||
  4590. (conntab[3] != 2 && conntab[3] != 4)) {
  4591. NV_ERROR(dev, " Unknown! Please report.\n");
  4592. return;
  4593. }
  4594. ct->entries = conntab[2];
  4595. entry = conntab + conntab[1];
  4596. cte = &ct->entry[0];
  4597. for (i = 0; i < conntab[2]; i++, entry += conntab[3], cte++) {
  4598. cte->index = i;
  4599. if (conntab[3] == 2)
  4600. cte->entry = ROM16(entry[0]);
  4601. else
  4602. cte->entry = ROM32(entry[0]);
  4603. cte->type = (cte->entry & 0x000000ff) >> 0;
  4604. cte->index2 = (cte->entry & 0x00000f00) >> 8;
  4605. switch (cte->entry & 0x00033000) {
  4606. case 0x00001000:
  4607. cte->gpio_tag = 0x07;
  4608. break;
  4609. case 0x00002000:
  4610. cte->gpio_tag = 0x08;
  4611. break;
  4612. case 0x00010000:
  4613. cte->gpio_tag = 0x51;
  4614. break;
  4615. case 0x00020000:
  4616. cte->gpio_tag = 0x52;
  4617. break;
  4618. default:
  4619. cte->gpio_tag = 0xff;
  4620. break;
  4621. }
  4622. if (cte->type == 0xff)
  4623. continue;
  4624. apply_dcb_connector_quirks(bios, i);
  4625. NV_INFO(dev, " %d: 0x%08x: type 0x%02x idx %d tag 0x%02x\n",
  4626. i, cte->entry, cte->type, cte->index, cte->gpio_tag);
  4627. /* check for known types, fallback to guessing the type
  4628. * from attached encoders if we hit an unknown.
  4629. */
  4630. switch (cte->type) {
  4631. case DCB_CONNECTOR_VGA:
  4632. case DCB_CONNECTOR_TV_0:
  4633. case DCB_CONNECTOR_TV_1:
  4634. case DCB_CONNECTOR_TV_3:
  4635. case DCB_CONNECTOR_DVI_I:
  4636. case DCB_CONNECTOR_DVI_D:
  4637. case DCB_CONNECTOR_LVDS:
  4638. case DCB_CONNECTOR_DP:
  4639. case DCB_CONNECTOR_eDP:
  4640. case DCB_CONNECTOR_HDMI_0:
  4641. case DCB_CONNECTOR_HDMI_1:
  4642. break;
  4643. default:
  4644. cte->type = divine_connector_type(bios, cte->index);
  4645. NV_WARN(dev, "unknown type, using 0x%02x\n", cte->type);
  4646. break;
  4647. }
  4648. if (nouveau_override_conntype) {
  4649. int type = divine_connector_type(bios, cte->index);
  4650. if (type != cte->type)
  4651. NV_WARN(dev, " -> type 0x%02x\n", cte->type);
  4652. }
  4653. }
  4654. }
  4655. static struct dcb_entry *new_dcb_entry(struct dcb_table *dcb)
  4656. {
  4657. struct dcb_entry *entry = &dcb->entry[dcb->entries];
  4658. memset(entry, 0, sizeof(struct dcb_entry));
  4659. entry->index = dcb->entries++;
  4660. return entry;
  4661. }
  4662. static void fabricate_vga_output(struct dcb_table *dcb, int i2c, int heads)
  4663. {
  4664. struct dcb_entry *entry = new_dcb_entry(dcb);
  4665. entry->type = 0;
  4666. entry->i2c_index = i2c;
  4667. entry->heads = heads;
  4668. entry->location = DCB_LOC_ON_CHIP;
  4669. /* "or" mostly unused in early gen crt modesetting, 0 is fine */
  4670. }
  4671. static void fabricate_dvi_i_output(struct dcb_table *dcb, bool twoHeads)
  4672. {
  4673. struct dcb_entry *entry = new_dcb_entry(dcb);
  4674. entry->type = 2;
  4675. entry->i2c_index = LEGACY_I2C_PANEL;
  4676. entry->heads = twoHeads ? 3 : 1;
  4677. entry->location = !DCB_LOC_ON_CHIP; /* ie OFF CHIP */
  4678. entry->or = 1; /* means |0x10 gets set on CRE_LCD__INDEX */
  4679. entry->duallink_possible = false; /* SiI164 and co. are single link */
  4680. #if 0
  4681. /*
  4682. * For dvi-a either crtc probably works, but my card appears to only
  4683. * support dvi-d. "nvidia" still attempts to program it for dvi-a,
  4684. * doing the full fp output setup (program 0x6808.. fp dimension regs,
  4685. * setting 0x680848 to 0x10000111 to enable, maybe setting 0x680880);
  4686. * the monitor picks up the mode res ok and lights up, but no pixel
  4687. * data appears, so the board manufacturer probably connected up the
  4688. * sync lines, but missed the video traces / components
  4689. *
  4690. * with this introduction, dvi-a left as an exercise for the reader.
  4691. */
  4692. fabricate_vga_output(dcb, LEGACY_I2C_PANEL, entry->heads);
  4693. #endif
  4694. }
  4695. static void fabricate_tv_output(struct dcb_table *dcb, bool twoHeads)
  4696. {
  4697. struct dcb_entry *entry = new_dcb_entry(dcb);
  4698. entry->type = 1;
  4699. entry->i2c_index = LEGACY_I2C_TV;
  4700. entry->heads = twoHeads ? 3 : 1;
  4701. entry->location = !DCB_LOC_ON_CHIP; /* ie OFF CHIP */
  4702. }
  4703. static bool
  4704. parse_dcb20_entry(struct drm_device *dev, struct dcb_table *dcb,
  4705. uint32_t conn, uint32_t conf, struct dcb_entry *entry)
  4706. {
  4707. entry->type = conn & 0xf;
  4708. entry->i2c_index = (conn >> 4) & 0xf;
  4709. entry->heads = (conn >> 8) & 0xf;
  4710. if (dcb->version >= 0x40)
  4711. entry->connector = (conn >> 12) & 0xf;
  4712. entry->bus = (conn >> 16) & 0xf;
  4713. entry->location = (conn >> 20) & 0x3;
  4714. entry->or = (conn >> 24) & 0xf;
  4715. switch (entry->type) {
  4716. case OUTPUT_ANALOG:
  4717. /*
  4718. * Although the rest of a CRT conf dword is usually
  4719. * zeros, mac biosen have stuff there so we must mask
  4720. */
  4721. entry->crtconf.maxfreq = (dcb->version < 0x30) ?
  4722. (conf & 0xffff) * 10 :
  4723. (conf & 0xff) * 10000;
  4724. break;
  4725. case OUTPUT_LVDS:
  4726. {
  4727. uint32_t mask;
  4728. if (conf & 0x1)
  4729. entry->lvdsconf.use_straps_for_mode = true;
  4730. if (dcb->version < 0x22) {
  4731. mask = ~0xd;
  4732. /*
  4733. * The laptop in bug 14567 lies and claims to not use
  4734. * straps when it does, so assume all DCB 2.0 laptops
  4735. * use straps, until a broken EDID using one is produced
  4736. */
  4737. entry->lvdsconf.use_straps_for_mode = true;
  4738. /*
  4739. * Both 0x4 and 0x8 show up in v2.0 tables; assume they
  4740. * mean the same thing (probably wrong, but might work)
  4741. */
  4742. if (conf & 0x4 || conf & 0x8)
  4743. entry->lvdsconf.use_power_scripts = true;
  4744. } else {
  4745. mask = ~0x5;
  4746. if (conf & 0x4)
  4747. entry->lvdsconf.use_power_scripts = true;
  4748. }
  4749. if (conf & mask) {
  4750. /*
  4751. * Until we even try to use these on G8x, it's
  4752. * useless reporting unknown bits. They all are.
  4753. */
  4754. if (dcb->version >= 0x40)
  4755. break;
  4756. NV_ERROR(dev, "Unknown LVDS configuration bits, "
  4757. "please report\n");
  4758. }
  4759. break;
  4760. }
  4761. case OUTPUT_TV:
  4762. {
  4763. if (dcb->version >= 0x30)
  4764. entry->tvconf.has_component_output = conf & (0x8 << 4);
  4765. else
  4766. entry->tvconf.has_component_output = false;
  4767. break;
  4768. }
  4769. case OUTPUT_DP:
  4770. entry->dpconf.sor.link = (conf & 0x00000030) >> 4;
  4771. entry->dpconf.link_bw = (conf & 0x00e00000) >> 21;
  4772. switch ((conf & 0x0f000000) >> 24) {
  4773. case 0xf:
  4774. entry->dpconf.link_nr = 4;
  4775. break;
  4776. case 0x3:
  4777. entry->dpconf.link_nr = 2;
  4778. break;
  4779. default:
  4780. entry->dpconf.link_nr = 1;
  4781. break;
  4782. }
  4783. break;
  4784. case OUTPUT_TMDS:
  4785. entry->tmdsconf.sor.link = (conf & 0x00000030) >> 4;
  4786. break;
  4787. case 0xe:
  4788. /* weird g80 mobile type that "nv" treats as a terminator */
  4789. dcb->entries--;
  4790. return false;
  4791. default:
  4792. break;
  4793. }
  4794. if (dcb->version < 0x40) {
  4795. /* Normal entries consist of a single bit, but dual link has
  4796. * the next most significant bit set too
  4797. */
  4798. entry->duallink_possible =
  4799. ((1 << (ffs(entry->or) - 1)) * 3 == entry->or);
  4800. } else {
  4801. entry->duallink_possible = (entry->sorconf.link == 3);
  4802. }
  4803. /* unsure what DCB version introduces this, 3.0? */
  4804. if (conf & 0x100000)
  4805. entry->i2c_upper_default = true;
  4806. return true;
  4807. }
  4808. static bool
  4809. parse_dcb15_entry(struct drm_device *dev, struct dcb_table *dcb,
  4810. uint32_t conn, uint32_t conf, struct dcb_entry *entry)
  4811. {
  4812. switch (conn & 0x0000000f) {
  4813. case 0:
  4814. entry->type = OUTPUT_ANALOG;
  4815. break;
  4816. case 1:
  4817. entry->type = OUTPUT_TV;
  4818. break;
  4819. case 2:
  4820. case 3:
  4821. entry->type = OUTPUT_LVDS;
  4822. break;
  4823. case 4:
  4824. switch ((conn & 0x000000f0) >> 4) {
  4825. case 0:
  4826. entry->type = OUTPUT_TMDS;
  4827. break;
  4828. case 1:
  4829. entry->type = OUTPUT_LVDS;
  4830. break;
  4831. default:
  4832. NV_ERROR(dev, "Unknown DCB subtype 4/%d\n",
  4833. (conn & 0x000000f0) >> 4);
  4834. return false;
  4835. }
  4836. break;
  4837. default:
  4838. NV_ERROR(dev, "Unknown DCB type %d\n", conn & 0x0000000f);
  4839. return false;
  4840. }
  4841. entry->i2c_index = (conn & 0x0003c000) >> 14;
  4842. entry->heads = ((conn & 0x001c0000) >> 18) + 1;
  4843. entry->or = entry->heads; /* same as heads, hopefully safe enough */
  4844. entry->location = (conn & 0x01e00000) >> 21;
  4845. entry->bus = (conn & 0x0e000000) >> 25;
  4846. entry->duallink_possible = false;
  4847. switch (entry->type) {
  4848. case OUTPUT_ANALOG:
  4849. entry->crtconf.maxfreq = (conf & 0xffff) * 10;
  4850. break;
  4851. case OUTPUT_TV:
  4852. entry->tvconf.has_component_output = false;
  4853. break;
  4854. case OUTPUT_TMDS:
  4855. /*
  4856. * Invent a DVI-A output, by copying the fields of the DVI-D
  4857. * output; reported to work by math_b on an NV20(!).
  4858. */
  4859. fabricate_vga_output(dcb, entry->i2c_index, entry->heads);
  4860. break;
  4861. case OUTPUT_LVDS:
  4862. if ((conn & 0x00003f00) != 0x10)
  4863. entry->lvdsconf.use_straps_for_mode = true;
  4864. entry->lvdsconf.use_power_scripts = true;
  4865. break;
  4866. default:
  4867. break;
  4868. }
  4869. return true;
  4870. }
  4871. static bool parse_dcb_entry(struct drm_device *dev, struct dcb_table *dcb,
  4872. uint32_t conn, uint32_t conf)
  4873. {
  4874. struct dcb_entry *entry = new_dcb_entry(dcb);
  4875. bool ret;
  4876. if (dcb->version >= 0x20)
  4877. ret = parse_dcb20_entry(dev, dcb, conn, conf, entry);
  4878. else
  4879. ret = parse_dcb15_entry(dev, dcb, conn, conf, entry);
  4880. if (!ret)
  4881. return ret;
  4882. read_dcb_i2c_entry(dev, dcb->version, dcb->i2c_table,
  4883. entry->i2c_index, &dcb->i2c[entry->i2c_index]);
  4884. return true;
  4885. }
  4886. static
  4887. void merge_like_dcb_entries(struct drm_device *dev, struct dcb_table *dcb)
  4888. {
  4889. /*
  4890. * DCB v2.0 lists each output combination separately.
  4891. * Here we merge compatible entries to have fewer outputs, with
  4892. * more options
  4893. */
  4894. int i, newentries = 0;
  4895. for (i = 0; i < dcb->entries; i++) {
  4896. struct dcb_entry *ient = &dcb->entry[i];
  4897. int j;
  4898. for (j = i + 1; j < dcb->entries; j++) {
  4899. struct dcb_entry *jent = &dcb->entry[j];
  4900. if (jent->type == 100) /* already merged entry */
  4901. continue;
  4902. /* merge heads field when all other fields the same */
  4903. if (jent->i2c_index == ient->i2c_index &&
  4904. jent->type == ient->type &&
  4905. jent->location == ient->location &&
  4906. jent->or == ient->or) {
  4907. NV_TRACE(dev, "Merging DCB entries %d and %d\n",
  4908. i, j);
  4909. ient->heads |= jent->heads;
  4910. jent->type = 100; /* dummy value */
  4911. }
  4912. }
  4913. }
  4914. /* Compact entries merged into others out of dcb */
  4915. for (i = 0; i < dcb->entries; i++) {
  4916. if (dcb->entry[i].type == 100)
  4917. continue;
  4918. if (newentries != i) {
  4919. dcb->entry[newentries] = dcb->entry[i];
  4920. dcb->entry[newentries].index = newentries;
  4921. }
  4922. newentries++;
  4923. }
  4924. dcb->entries = newentries;
  4925. }
  4926. static bool
  4927. apply_dcb_encoder_quirks(struct drm_device *dev, int idx, u32 *conn, u32 *conf)
  4928. {
  4929. /* Dell Precision M6300
  4930. * DCB entry 2: 02025312 00000010
  4931. * DCB entry 3: 02026312 00000020
  4932. *
  4933. * Identical, except apparently a different connector on a
  4934. * different SOR link. Not a clue how we're supposed to know
  4935. * which one is in use if it even shares an i2c line...
  4936. *
  4937. * Ignore the connector on the second SOR link to prevent
  4938. * nasty problems until this is sorted (assuming it's not a
  4939. * VBIOS bug).
  4940. */
  4941. if ((dev->pdev->device == 0x040d) &&
  4942. (dev->pdev->subsystem_vendor == 0x1028) &&
  4943. (dev->pdev->subsystem_device == 0x019b)) {
  4944. if (*conn == 0x02026312 && *conf == 0x00000020)
  4945. return false;
  4946. }
  4947. return true;
  4948. }
  4949. static int
  4950. parse_dcb_table(struct drm_device *dev, struct nvbios *bios, bool twoHeads)
  4951. {
  4952. struct drm_nouveau_private *dev_priv = dev->dev_private;
  4953. struct dcb_table *dcb = &bios->dcb;
  4954. uint16_t dcbptr = 0, i2ctabptr = 0;
  4955. uint8_t *dcbtable;
  4956. uint8_t headerlen = 0x4, entries = DCB_MAX_NUM_ENTRIES;
  4957. bool configblock = true;
  4958. int recordlength = 8, confofs = 4;
  4959. int i;
  4960. /* get the offset from 0x36 */
  4961. if (dev_priv->card_type > NV_04) {
  4962. dcbptr = ROM16(bios->data[0x36]);
  4963. if (dcbptr == 0x0000)
  4964. NV_WARN(dev, "No output data (DCB) found in BIOS\n");
  4965. }
  4966. /* this situation likely means a really old card, pre DCB */
  4967. if (dcbptr == 0x0) {
  4968. NV_INFO(dev, "Assuming a CRT output exists\n");
  4969. fabricate_vga_output(dcb, LEGACY_I2C_CRT, 1);
  4970. if (nv04_tv_identify(dev, bios->legacy.i2c_indices.tv) >= 0)
  4971. fabricate_tv_output(dcb, twoHeads);
  4972. return 0;
  4973. }
  4974. dcbtable = &bios->data[dcbptr];
  4975. /* get DCB version */
  4976. dcb->version = dcbtable[0];
  4977. NV_TRACE(dev, "Found Display Configuration Block version %d.%d\n",
  4978. dcb->version >> 4, dcb->version & 0xf);
  4979. if (dcb->version >= 0x20) { /* NV17+ */
  4980. uint32_t sig;
  4981. if (dcb->version >= 0x30) { /* NV40+ */
  4982. headerlen = dcbtable[1];
  4983. entries = dcbtable[2];
  4984. recordlength = dcbtable[3];
  4985. i2ctabptr = ROM16(dcbtable[4]);
  4986. sig = ROM32(dcbtable[6]);
  4987. dcb->gpio_table_ptr = ROM16(dcbtable[10]);
  4988. dcb->connector_table_ptr = ROM16(dcbtable[20]);
  4989. } else {
  4990. i2ctabptr = ROM16(dcbtable[2]);
  4991. sig = ROM32(dcbtable[4]);
  4992. headerlen = 8;
  4993. }
  4994. if (sig != 0x4edcbdcb) {
  4995. NV_ERROR(dev, "Bad Display Configuration Block "
  4996. "signature (%08X)\n", sig);
  4997. return -EINVAL;
  4998. }
  4999. } else if (dcb->version >= 0x15) { /* some NV11 and NV20 */
  5000. char sig[8] = { 0 };
  5001. strncpy(sig, (char *)&dcbtable[-7], 7);
  5002. i2ctabptr = ROM16(dcbtable[2]);
  5003. recordlength = 10;
  5004. confofs = 6;
  5005. if (strcmp(sig, "DEV_REC")) {
  5006. NV_ERROR(dev, "Bad Display Configuration Block "
  5007. "signature (%s)\n", sig);
  5008. return -EINVAL;
  5009. }
  5010. } else {
  5011. /*
  5012. * v1.4 (some NV15/16, NV11+) seems the same as v1.5, but always
  5013. * has the same single (crt) entry, even when tv-out present, so
  5014. * the conclusion is this version cannot really be used.
  5015. * v1.2 tables (some NV6/10, and NV15+) normally have the same
  5016. * 5 entries, which are not specific to the card and so no use.
  5017. * v1.2 does have an I2C table that read_dcb_i2c_table can
  5018. * handle, but cards exist (nv11 in #14821) with a bad i2c table
  5019. * pointer, so use the indices parsed in parse_bmp_structure.
  5020. * v1.1 (NV5+, maybe some NV4) is entirely unhelpful
  5021. */
  5022. NV_TRACEWARN(dev, "No useful information in BIOS output table; "
  5023. "adding all possible outputs\n");
  5024. fabricate_vga_output(dcb, LEGACY_I2C_CRT, 1);
  5025. /*
  5026. * Attempt to detect TV before DVI because the test
  5027. * for the former is more accurate and it rules the
  5028. * latter out.
  5029. */
  5030. if (nv04_tv_identify(dev,
  5031. bios->legacy.i2c_indices.tv) >= 0)
  5032. fabricate_tv_output(dcb, twoHeads);
  5033. else if (bios->tmds.output0_script_ptr ||
  5034. bios->tmds.output1_script_ptr)
  5035. fabricate_dvi_i_output(dcb, twoHeads);
  5036. return 0;
  5037. }
  5038. if (!i2ctabptr)
  5039. NV_WARN(dev, "No pointer to DCB I2C port table\n");
  5040. else {
  5041. dcb->i2c_table = &bios->data[i2ctabptr];
  5042. if (dcb->version >= 0x30)
  5043. dcb->i2c_default_indices = dcb->i2c_table[4];
  5044. }
  5045. if (entries > DCB_MAX_NUM_ENTRIES)
  5046. entries = DCB_MAX_NUM_ENTRIES;
  5047. for (i = 0; i < entries; i++) {
  5048. uint32_t connection, config = 0;
  5049. connection = ROM32(dcbtable[headerlen + recordlength * i]);
  5050. if (configblock)
  5051. config = ROM32(dcbtable[headerlen + confofs + recordlength * i]);
  5052. /* seen on an NV11 with DCB v1.5 */
  5053. if (connection == 0x00000000)
  5054. break;
  5055. /* seen on an NV17 with DCB v2.0 */
  5056. if (connection == 0xffffffff)
  5057. break;
  5058. if ((connection & 0x0000000f) == 0x0000000f)
  5059. continue;
  5060. if (!apply_dcb_encoder_quirks(dev, i, &connection, &config))
  5061. continue;
  5062. NV_TRACEWARN(dev, "Raw DCB entry %d: %08x %08x\n",
  5063. dcb->entries, connection, config);
  5064. if (!parse_dcb_entry(dev, dcb, connection, config))
  5065. break;
  5066. }
  5067. /*
  5068. * apart for v2.1+ not being known for requiring merging, this
  5069. * guarantees dcbent->index is the index of the entry in the rom image
  5070. */
  5071. if (dcb->version < 0x21)
  5072. merge_like_dcb_entries(dev, dcb);
  5073. if (!dcb->entries)
  5074. return -ENXIO;
  5075. parse_dcb_gpio_table(bios);
  5076. parse_dcb_connector_table(bios);
  5077. return 0;
  5078. }
  5079. static void
  5080. fixup_legacy_connector(struct nvbios *bios)
  5081. {
  5082. struct dcb_table *dcb = &bios->dcb;
  5083. int i, i2c, i2c_conn[DCB_MAX_NUM_I2C_ENTRIES] = { };
  5084. /*
  5085. * DCB 3.0 also has the table in most cases, but there are some cards
  5086. * where the table is filled with stub entries, and the DCB entriy
  5087. * indices are all 0. We don't need the connector indices on pre-G80
  5088. * chips (yet?) so limit the use to DCB 4.0 and above.
  5089. */
  5090. if (dcb->version >= 0x40)
  5091. return;
  5092. dcb->connector.entries = 0;
  5093. /*
  5094. * No known connector info before v3.0, so make it up. the rule here
  5095. * is: anything on the same i2c bus is considered to be on the same
  5096. * connector. any output without an associated i2c bus is assigned
  5097. * its own unique connector index.
  5098. */
  5099. for (i = 0; i < dcb->entries; i++) {
  5100. /*
  5101. * Ignore the I2C index for on-chip TV-out, as there
  5102. * are cards with bogus values (nv31m in bug 23212),
  5103. * and it's otherwise useless.
  5104. */
  5105. if (dcb->entry[i].type == OUTPUT_TV &&
  5106. dcb->entry[i].location == DCB_LOC_ON_CHIP)
  5107. dcb->entry[i].i2c_index = 0xf;
  5108. i2c = dcb->entry[i].i2c_index;
  5109. if (i2c_conn[i2c]) {
  5110. dcb->entry[i].connector = i2c_conn[i2c] - 1;
  5111. continue;
  5112. }
  5113. dcb->entry[i].connector = dcb->connector.entries++;
  5114. if (i2c != 0xf)
  5115. i2c_conn[i2c] = dcb->connector.entries;
  5116. }
  5117. /* Fake the connector table as well as just connector indices */
  5118. for (i = 0; i < dcb->connector.entries; i++) {
  5119. dcb->connector.entry[i].index = i;
  5120. dcb->connector.entry[i].type = divine_connector_type(bios, i);
  5121. dcb->connector.entry[i].gpio_tag = 0xff;
  5122. }
  5123. }
  5124. static void
  5125. fixup_legacy_i2c(struct nvbios *bios)
  5126. {
  5127. struct dcb_table *dcb = &bios->dcb;
  5128. int i;
  5129. for (i = 0; i < dcb->entries; i++) {
  5130. if (dcb->entry[i].i2c_index == LEGACY_I2C_CRT)
  5131. dcb->entry[i].i2c_index = bios->legacy.i2c_indices.crt;
  5132. if (dcb->entry[i].i2c_index == LEGACY_I2C_PANEL)
  5133. dcb->entry[i].i2c_index = bios->legacy.i2c_indices.panel;
  5134. if (dcb->entry[i].i2c_index == LEGACY_I2C_TV)
  5135. dcb->entry[i].i2c_index = bios->legacy.i2c_indices.tv;
  5136. }
  5137. }
  5138. static int load_nv17_hwsq_ucode_entry(struct drm_device *dev, struct nvbios *bios, uint16_t hwsq_offset, int entry)
  5139. {
  5140. /*
  5141. * The header following the "HWSQ" signature has the number of entries,
  5142. * and the entry size
  5143. *
  5144. * An entry consists of a dword to write to the sequencer control reg
  5145. * (0x00001304), followed by the ucode bytes, written sequentially,
  5146. * starting at reg 0x00001400
  5147. */
  5148. uint8_t bytes_to_write;
  5149. uint16_t hwsq_entry_offset;
  5150. int i;
  5151. if (bios->data[hwsq_offset] <= entry) {
  5152. NV_ERROR(dev, "Too few entries in HW sequencer table for "
  5153. "requested entry\n");
  5154. return -ENOENT;
  5155. }
  5156. bytes_to_write = bios->data[hwsq_offset + 1];
  5157. if (bytes_to_write != 36) {
  5158. NV_ERROR(dev, "Unknown HW sequencer entry size\n");
  5159. return -EINVAL;
  5160. }
  5161. NV_TRACE(dev, "Loading NV17 power sequencing microcode\n");
  5162. hwsq_entry_offset = hwsq_offset + 2 + entry * bytes_to_write;
  5163. /* set sequencer control */
  5164. bios_wr32(bios, 0x00001304, ROM32(bios->data[hwsq_entry_offset]));
  5165. bytes_to_write -= 4;
  5166. /* write ucode */
  5167. for (i = 0; i < bytes_to_write; i += 4)
  5168. bios_wr32(bios, 0x00001400 + i, ROM32(bios->data[hwsq_entry_offset + i + 4]));
  5169. /* twiddle NV_PBUS_DEBUG_4 */
  5170. bios_wr32(bios, NV_PBUS_DEBUG_4, bios_rd32(bios, NV_PBUS_DEBUG_4) | 0x18);
  5171. return 0;
  5172. }
  5173. static int load_nv17_hw_sequencer_ucode(struct drm_device *dev,
  5174. struct nvbios *bios)
  5175. {
  5176. /*
  5177. * BMP based cards, from NV17, need a microcode loading to correctly
  5178. * control the GPIO etc for LVDS panels
  5179. *
  5180. * BIT based cards seem to do this directly in the init scripts
  5181. *
  5182. * The microcode entries are found by the "HWSQ" signature.
  5183. */
  5184. const uint8_t hwsq_signature[] = { 'H', 'W', 'S', 'Q' };
  5185. const int sz = sizeof(hwsq_signature);
  5186. int hwsq_offset;
  5187. hwsq_offset = findstr(bios->data, bios->length, hwsq_signature, sz);
  5188. if (!hwsq_offset)
  5189. return 0;
  5190. /* always use entry 0? */
  5191. return load_nv17_hwsq_ucode_entry(dev, bios, hwsq_offset + sz, 0);
  5192. }
  5193. uint8_t *nouveau_bios_embedded_edid(struct drm_device *dev)
  5194. {
  5195. struct drm_nouveau_private *dev_priv = dev->dev_private;
  5196. struct nvbios *bios = &dev_priv->vbios;
  5197. const uint8_t edid_sig[] = {
  5198. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
  5199. uint16_t offset = 0;
  5200. uint16_t newoffset;
  5201. int searchlen = NV_PROM_SIZE;
  5202. if (bios->fp.edid)
  5203. return bios->fp.edid;
  5204. while (searchlen) {
  5205. newoffset = findstr(&bios->data[offset], searchlen,
  5206. edid_sig, 8);
  5207. if (!newoffset)
  5208. return NULL;
  5209. offset += newoffset;
  5210. if (!nv_cksum(&bios->data[offset], EDID1_LEN))
  5211. break;
  5212. searchlen -= offset;
  5213. offset++;
  5214. }
  5215. NV_TRACE(dev, "Found EDID in BIOS\n");
  5216. return bios->fp.edid = &bios->data[offset];
  5217. }
  5218. void
  5219. nouveau_bios_run_init_table(struct drm_device *dev, uint16_t table,
  5220. struct dcb_entry *dcbent)
  5221. {
  5222. struct drm_nouveau_private *dev_priv = dev->dev_private;
  5223. struct nvbios *bios = &dev_priv->vbios;
  5224. struct init_exec iexec = { true, false };
  5225. mutex_lock(&bios->lock);
  5226. bios->display.output = dcbent;
  5227. parse_init_table(bios, table, &iexec);
  5228. bios->display.output = NULL;
  5229. mutex_unlock(&bios->lock);
  5230. }
  5231. static bool NVInitVBIOS(struct drm_device *dev)
  5232. {
  5233. struct drm_nouveau_private *dev_priv = dev->dev_private;
  5234. struct nvbios *bios = &dev_priv->vbios;
  5235. memset(bios, 0, sizeof(struct nvbios));
  5236. mutex_init(&bios->lock);
  5237. bios->dev = dev;
  5238. if (!NVShadowVBIOS(dev, bios->data))
  5239. return false;
  5240. bios->length = NV_PROM_SIZE;
  5241. return true;
  5242. }
  5243. static int nouveau_parse_vbios_struct(struct drm_device *dev)
  5244. {
  5245. struct drm_nouveau_private *dev_priv = dev->dev_private;
  5246. struct nvbios *bios = &dev_priv->vbios;
  5247. const uint8_t bit_signature[] = { 0xff, 0xb8, 'B', 'I', 'T' };
  5248. const uint8_t bmp_signature[] = { 0xff, 0x7f, 'N', 'V', 0x0 };
  5249. int offset;
  5250. offset = findstr(bios->data, bios->length,
  5251. bit_signature, sizeof(bit_signature));
  5252. if (offset) {
  5253. NV_TRACE(dev, "BIT BIOS found\n");
  5254. return parse_bit_structure(bios, offset + 6);
  5255. }
  5256. offset = findstr(bios->data, bios->length,
  5257. bmp_signature, sizeof(bmp_signature));
  5258. if (offset) {
  5259. NV_TRACE(dev, "BMP BIOS found\n");
  5260. return parse_bmp_structure(dev, bios, offset);
  5261. }
  5262. NV_ERROR(dev, "No known BIOS signature found\n");
  5263. return -ENODEV;
  5264. }
  5265. int
  5266. nouveau_run_vbios_init(struct drm_device *dev)
  5267. {
  5268. struct drm_nouveau_private *dev_priv = dev->dev_private;
  5269. struct nvbios *bios = &dev_priv->vbios;
  5270. int i, ret = 0;
  5271. NVLockVgaCrtcs(dev, false);
  5272. if (nv_two_heads(dev))
  5273. NVSetOwner(dev, bios->state.crtchead);
  5274. if (bios->major_version < 5) /* BMP only */
  5275. load_nv17_hw_sequencer_ucode(dev, bios);
  5276. if (bios->execute) {
  5277. bios->fp.last_script_invoc = 0;
  5278. bios->fp.lvds_init_run = false;
  5279. }
  5280. parse_init_tables(bios);
  5281. /*
  5282. * Runs some additional script seen on G8x VBIOSen. The VBIOS'
  5283. * parser will run this right after the init tables, the binary
  5284. * driver appears to run it at some point later.
  5285. */
  5286. if (bios->some_script_ptr) {
  5287. struct init_exec iexec = {true, false};
  5288. NV_INFO(dev, "Parsing VBIOS init table at offset 0x%04X\n",
  5289. bios->some_script_ptr);
  5290. parse_init_table(bios, bios->some_script_ptr, &iexec);
  5291. }
  5292. if (dev_priv->card_type >= NV_50) {
  5293. for (i = 0; i < bios->dcb.entries; i++) {
  5294. nouveau_bios_run_display_table(dev,
  5295. &bios->dcb.entry[i],
  5296. 0, 0);
  5297. }
  5298. }
  5299. NVLockVgaCrtcs(dev, true);
  5300. return ret;
  5301. }
  5302. static void
  5303. nouveau_bios_i2c_devices_takedown(struct drm_device *dev)
  5304. {
  5305. struct drm_nouveau_private *dev_priv = dev->dev_private;
  5306. struct nvbios *bios = &dev_priv->vbios;
  5307. struct dcb_i2c_entry *entry;
  5308. int i;
  5309. entry = &bios->dcb.i2c[0];
  5310. for (i = 0; i < DCB_MAX_NUM_I2C_ENTRIES; i++, entry++)
  5311. nouveau_i2c_fini(dev, entry);
  5312. }
  5313. static bool
  5314. nouveau_bios_posted(struct drm_device *dev)
  5315. {
  5316. struct drm_nouveau_private *dev_priv = dev->dev_private;
  5317. bool was_locked;
  5318. unsigned htotal;
  5319. if (dev_priv->chipset >= NV_50) {
  5320. if (NVReadVgaCrtc(dev, 0, 0x00) == 0 &&
  5321. NVReadVgaCrtc(dev, 0, 0x1a) == 0)
  5322. return false;
  5323. return true;
  5324. }
  5325. was_locked = NVLockVgaCrtcs(dev, false);
  5326. htotal = NVReadVgaCrtc(dev, 0, 0x06);
  5327. htotal |= (NVReadVgaCrtc(dev, 0, 0x07) & 0x01) << 8;
  5328. htotal |= (NVReadVgaCrtc(dev, 0, 0x07) & 0x20) << 4;
  5329. htotal |= (NVReadVgaCrtc(dev, 0, 0x25) & 0x01) << 10;
  5330. htotal |= (NVReadVgaCrtc(dev, 0, 0x41) & 0x01) << 11;
  5331. NVLockVgaCrtcs(dev, was_locked);
  5332. return (htotal != 0);
  5333. }
  5334. int
  5335. nouveau_bios_init(struct drm_device *dev)
  5336. {
  5337. struct drm_nouveau_private *dev_priv = dev->dev_private;
  5338. struct nvbios *bios = &dev_priv->vbios;
  5339. uint32_t saved_nv_pextdev_boot_0;
  5340. bool was_locked;
  5341. int ret;
  5342. if (!NVInitVBIOS(dev))
  5343. return -ENODEV;
  5344. ret = nouveau_parse_vbios_struct(dev);
  5345. if (ret)
  5346. return ret;
  5347. ret = parse_dcb_table(dev, bios, nv_two_heads(dev));
  5348. if (ret)
  5349. return ret;
  5350. fixup_legacy_i2c(bios);
  5351. fixup_legacy_connector(bios);
  5352. if (!bios->major_version) /* we don't run version 0 bios */
  5353. return 0;
  5354. /* these will need remembering across a suspend */
  5355. saved_nv_pextdev_boot_0 = bios_rd32(bios, NV_PEXTDEV_BOOT_0);
  5356. bios->state.saved_nv_pfb_cfg0 = bios_rd32(bios, NV_PFB_CFG0);
  5357. /* init script execution disabled */
  5358. bios->execute = false;
  5359. /* ... unless card isn't POSTed already */
  5360. if (!nouveau_bios_posted(dev)) {
  5361. NV_INFO(dev, "Adaptor not initialised\n");
  5362. if (dev_priv->card_type < NV_40) {
  5363. NV_ERROR(dev, "Unable to POST this chipset\n");
  5364. return -ENODEV;
  5365. }
  5366. NV_INFO(dev, "Running VBIOS init tables\n");
  5367. bios->execute = true;
  5368. }
  5369. bios_wr32(bios, NV_PEXTDEV_BOOT_0, saved_nv_pextdev_boot_0);
  5370. ret = nouveau_run_vbios_init(dev);
  5371. if (ret)
  5372. return ret;
  5373. /* feature_byte on BMP is poor, but init always sets CR4B */
  5374. was_locked = NVLockVgaCrtcs(dev, false);
  5375. if (bios->major_version < 5)
  5376. bios->is_mobile = NVReadVgaCrtc(dev, 0, NV_CIO_CRE_4B) & 0x40;
  5377. /* all BIT systems need p_f_m_t for digital_min_front_porch */
  5378. if (bios->is_mobile || bios->major_version >= 5)
  5379. ret = parse_fp_mode_table(dev, bios);
  5380. NVLockVgaCrtcs(dev, was_locked);
  5381. /* allow subsequent scripts to execute */
  5382. bios->execute = true;
  5383. return 0;
  5384. }
  5385. void
  5386. nouveau_bios_takedown(struct drm_device *dev)
  5387. {
  5388. nouveau_bios_i2c_devices_takedown(dev);
  5389. }