nouveau_bios.c 174 KB

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