nouveau_bios.c 166 KB

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