nouveau_bios.c 187 KB

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