nouveau_bios.c 181 KB

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