nouveau_bios.c 173 KB

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