nouveau_bios.c 181 KB

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