intel_display.c 182 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/module.h>
  27. #include <linux/input.h>
  28. #include <linux/i2c.h>
  29. #include <linux/kernel.h>
  30. #include <linux/slab.h>
  31. #include <linux/vgaarb.h>
  32. #include "drmP.h"
  33. #include "intel_drv.h"
  34. #include "i915_drm.h"
  35. #include "i915_drv.h"
  36. #include "i915_trace.h"
  37. #include "drm_dp_helper.h"
  38. #include "drm_crtc_helper.h"
  39. #define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  40. bool intel_pipe_has_type (struct drm_crtc *crtc, int type);
  41. static void intel_update_watermarks(struct drm_device *dev);
  42. static void intel_increase_pllclock(struct drm_crtc *crtc);
  43. static void intel_crtc_update_cursor(struct drm_crtc *crtc);
  44. typedef struct {
  45. /* given values */
  46. int n;
  47. int m1, m2;
  48. int p1, p2;
  49. /* derived values */
  50. int dot;
  51. int vco;
  52. int m;
  53. int p;
  54. } intel_clock_t;
  55. typedef struct {
  56. int min, max;
  57. } intel_range_t;
  58. typedef struct {
  59. int dot_limit;
  60. int p2_slow, p2_fast;
  61. } intel_p2_t;
  62. #define INTEL_P2_NUM 2
  63. typedef struct intel_limit intel_limit_t;
  64. struct intel_limit {
  65. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  66. intel_p2_t p2;
  67. bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
  68. int, int, intel_clock_t *);
  69. };
  70. #define I8XX_DOT_MIN 25000
  71. #define I8XX_DOT_MAX 350000
  72. #define I8XX_VCO_MIN 930000
  73. #define I8XX_VCO_MAX 1400000
  74. #define I8XX_N_MIN 3
  75. #define I8XX_N_MAX 16
  76. #define I8XX_M_MIN 96
  77. #define I8XX_M_MAX 140
  78. #define I8XX_M1_MIN 18
  79. #define I8XX_M1_MAX 26
  80. #define I8XX_M2_MIN 6
  81. #define I8XX_M2_MAX 16
  82. #define I8XX_P_MIN 4
  83. #define I8XX_P_MAX 128
  84. #define I8XX_P1_MIN 2
  85. #define I8XX_P1_MAX 33
  86. #define I8XX_P1_LVDS_MIN 1
  87. #define I8XX_P1_LVDS_MAX 6
  88. #define I8XX_P2_SLOW 4
  89. #define I8XX_P2_FAST 2
  90. #define I8XX_P2_LVDS_SLOW 14
  91. #define I8XX_P2_LVDS_FAST 7
  92. #define I8XX_P2_SLOW_LIMIT 165000
  93. #define I9XX_DOT_MIN 20000
  94. #define I9XX_DOT_MAX 400000
  95. #define I9XX_VCO_MIN 1400000
  96. #define I9XX_VCO_MAX 2800000
  97. #define PINEVIEW_VCO_MIN 1700000
  98. #define PINEVIEW_VCO_MAX 3500000
  99. #define I9XX_N_MIN 1
  100. #define I9XX_N_MAX 6
  101. /* Pineview's Ncounter is a ring counter */
  102. #define PINEVIEW_N_MIN 3
  103. #define PINEVIEW_N_MAX 6
  104. #define I9XX_M_MIN 70
  105. #define I9XX_M_MAX 120
  106. #define PINEVIEW_M_MIN 2
  107. #define PINEVIEW_M_MAX 256
  108. #define I9XX_M1_MIN 10
  109. #define I9XX_M1_MAX 22
  110. #define I9XX_M2_MIN 5
  111. #define I9XX_M2_MAX 9
  112. /* Pineview M1 is reserved, and must be 0 */
  113. #define PINEVIEW_M1_MIN 0
  114. #define PINEVIEW_M1_MAX 0
  115. #define PINEVIEW_M2_MIN 0
  116. #define PINEVIEW_M2_MAX 254
  117. #define I9XX_P_SDVO_DAC_MIN 5
  118. #define I9XX_P_SDVO_DAC_MAX 80
  119. #define I9XX_P_LVDS_MIN 7
  120. #define I9XX_P_LVDS_MAX 98
  121. #define PINEVIEW_P_LVDS_MIN 7
  122. #define PINEVIEW_P_LVDS_MAX 112
  123. #define I9XX_P1_MIN 1
  124. #define I9XX_P1_MAX 8
  125. #define I9XX_P2_SDVO_DAC_SLOW 10
  126. #define I9XX_P2_SDVO_DAC_FAST 5
  127. #define I9XX_P2_SDVO_DAC_SLOW_LIMIT 200000
  128. #define I9XX_P2_LVDS_SLOW 14
  129. #define I9XX_P2_LVDS_FAST 7
  130. #define I9XX_P2_LVDS_SLOW_LIMIT 112000
  131. /*The parameter is for SDVO on G4x platform*/
  132. #define G4X_DOT_SDVO_MIN 25000
  133. #define G4X_DOT_SDVO_MAX 270000
  134. #define G4X_VCO_MIN 1750000
  135. #define G4X_VCO_MAX 3500000
  136. #define G4X_N_SDVO_MIN 1
  137. #define G4X_N_SDVO_MAX 4
  138. #define G4X_M_SDVO_MIN 104
  139. #define G4X_M_SDVO_MAX 138
  140. #define G4X_M1_SDVO_MIN 17
  141. #define G4X_M1_SDVO_MAX 23
  142. #define G4X_M2_SDVO_MIN 5
  143. #define G4X_M2_SDVO_MAX 11
  144. #define G4X_P_SDVO_MIN 10
  145. #define G4X_P_SDVO_MAX 30
  146. #define G4X_P1_SDVO_MIN 1
  147. #define G4X_P1_SDVO_MAX 3
  148. #define G4X_P2_SDVO_SLOW 10
  149. #define G4X_P2_SDVO_FAST 10
  150. #define G4X_P2_SDVO_LIMIT 270000
  151. /*The parameter is for HDMI_DAC on G4x platform*/
  152. #define G4X_DOT_HDMI_DAC_MIN 22000
  153. #define G4X_DOT_HDMI_DAC_MAX 400000
  154. #define G4X_N_HDMI_DAC_MIN 1
  155. #define G4X_N_HDMI_DAC_MAX 4
  156. #define G4X_M_HDMI_DAC_MIN 104
  157. #define G4X_M_HDMI_DAC_MAX 138
  158. #define G4X_M1_HDMI_DAC_MIN 16
  159. #define G4X_M1_HDMI_DAC_MAX 23
  160. #define G4X_M2_HDMI_DAC_MIN 5
  161. #define G4X_M2_HDMI_DAC_MAX 11
  162. #define G4X_P_HDMI_DAC_MIN 5
  163. #define G4X_P_HDMI_DAC_MAX 80
  164. #define G4X_P1_HDMI_DAC_MIN 1
  165. #define G4X_P1_HDMI_DAC_MAX 8
  166. #define G4X_P2_HDMI_DAC_SLOW 10
  167. #define G4X_P2_HDMI_DAC_FAST 5
  168. #define G4X_P2_HDMI_DAC_LIMIT 165000
  169. /*The parameter is for SINGLE_CHANNEL_LVDS on G4x platform*/
  170. #define G4X_DOT_SINGLE_CHANNEL_LVDS_MIN 20000
  171. #define G4X_DOT_SINGLE_CHANNEL_LVDS_MAX 115000
  172. #define G4X_N_SINGLE_CHANNEL_LVDS_MIN 1
  173. #define G4X_N_SINGLE_CHANNEL_LVDS_MAX 3
  174. #define G4X_M_SINGLE_CHANNEL_LVDS_MIN 104
  175. #define G4X_M_SINGLE_CHANNEL_LVDS_MAX 138
  176. #define G4X_M1_SINGLE_CHANNEL_LVDS_MIN 17
  177. #define G4X_M1_SINGLE_CHANNEL_LVDS_MAX 23
  178. #define G4X_M2_SINGLE_CHANNEL_LVDS_MIN 5
  179. #define G4X_M2_SINGLE_CHANNEL_LVDS_MAX 11
  180. #define G4X_P_SINGLE_CHANNEL_LVDS_MIN 28
  181. #define G4X_P_SINGLE_CHANNEL_LVDS_MAX 112
  182. #define G4X_P1_SINGLE_CHANNEL_LVDS_MIN 2
  183. #define G4X_P1_SINGLE_CHANNEL_LVDS_MAX 8
  184. #define G4X_P2_SINGLE_CHANNEL_LVDS_SLOW 14
  185. #define G4X_P2_SINGLE_CHANNEL_LVDS_FAST 14
  186. #define G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT 0
  187. /*The parameter is for DUAL_CHANNEL_LVDS on G4x platform*/
  188. #define G4X_DOT_DUAL_CHANNEL_LVDS_MIN 80000
  189. #define G4X_DOT_DUAL_CHANNEL_LVDS_MAX 224000
  190. #define G4X_N_DUAL_CHANNEL_LVDS_MIN 1
  191. #define G4X_N_DUAL_CHANNEL_LVDS_MAX 3
  192. #define G4X_M_DUAL_CHANNEL_LVDS_MIN 104
  193. #define G4X_M_DUAL_CHANNEL_LVDS_MAX 138
  194. #define G4X_M1_DUAL_CHANNEL_LVDS_MIN 17
  195. #define G4X_M1_DUAL_CHANNEL_LVDS_MAX 23
  196. #define G4X_M2_DUAL_CHANNEL_LVDS_MIN 5
  197. #define G4X_M2_DUAL_CHANNEL_LVDS_MAX 11
  198. #define G4X_P_DUAL_CHANNEL_LVDS_MIN 14
  199. #define G4X_P_DUAL_CHANNEL_LVDS_MAX 42
  200. #define G4X_P1_DUAL_CHANNEL_LVDS_MIN 2
  201. #define G4X_P1_DUAL_CHANNEL_LVDS_MAX 6
  202. #define G4X_P2_DUAL_CHANNEL_LVDS_SLOW 7
  203. #define G4X_P2_DUAL_CHANNEL_LVDS_FAST 7
  204. #define G4X_P2_DUAL_CHANNEL_LVDS_LIMIT 0
  205. /*The parameter is for DISPLAY PORT on G4x platform*/
  206. #define G4X_DOT_DISPLAY_PORT_MIN 161670
  207. #define G4X_DOT_DISPLAY_PORT_MAX 227000
  208. #define G4X_N_DISPLAY_PORT_MIN 1
  209. #define G4X_N_DISPLAY_PORT_MAX 2
  210. #define G4X_M_DISPLAY_PORT_MIN 97
  211. #define G4X_M_DISPLAY_PORT_MAX 108
  212. #define G4X_M1_DISPLAY_PORT_MIN 0x10
  213. #define G4X_M1_DISPLAY_PORT_MAX 0x12
  214. #define G4X_M2_DISPLAY_PORT_MIN 0x05
  215. #define G4X_M2_DISPLAY_PORT_MAX 0x06
  216. #define G4X_P_DISPLAY_PORT_MIN 10
  217. #define G4X_P_DISPLAY_PORT_MAX 20
  218. #define G4X_P1_DISPLAY_PORT_MIN 1
  219. #define G4X_P1_DISPLAY_PORT_MAX 2
  220. #define G4X_P2_DISPLAY_PORT_SLOW 10
  221. #define G4X_P2_DISPLAY_PORT_FAST 10
  222. #define G4X_P2_DISPLAY_PORT_LIMIT 0
  223. /* Ironlake / Sandybridge */
  224. /* as we calculate clock using (register_value + 2) for
  225. N/M1/M2, so here the range value for them is (actual_value-2).
  226. */
  227. #define IRONLAKE_DOT_MIN 25000
  228. #define IRONLAKE_DOT_MAX 350000
  229. #define IRONLAKE_VCO_MIN 1760000
  230. #define IRONLAKE_VCO_MAX 3510000
  231. #define IRONLAKE_M1_MIN 12
  232. #define IRONLAKE_M1_MAX 22
  233. #define IRONLAKE_M2_MIN 5
  234. #define IRONLAKE_M2_MAX 9
  235. #define IRONLAKE_P2_DOT_LIMIT 225000 /* 225Mhz */
  236. /* We have parameter ranges for different type of outputs. */
  237. /* DAC & HDMI Refclk 120Mhz */
  238. #define IRONLAKE_DAC_N_MIN 1
  239. #define IRONLAKE_DAC_N_MAX 5
  240. #define IRONLAKE_DAC_M_MIN 79
  241. #define IRONLAKE_DAC_M_MAX 127
  242. #define IRONLAKE_DAC_P_MIN 5
  243. #define IRONLAKE_DAC_P_MAX 80
  244. #define IRONLAKE_DAC_P1_MIN 1
  245. #define IRONLAKE_DAC_P1_MAX 8
  246. #define IRONLAKE_DAC_P2_SLOW 10
  247. #define IRONLAKE_DAC_P2_FAST 5
  248. /* LVDS single-channel 120Mhz refclk */
  249. #define IRONLAKE_LVDS_S_N_MIN 1
  250. #define IRONLAKE_LVDS_S_N_MAX 3
  251. #define IRONLAKE_LVDS_S_M_MIN 79
  252. #define IRONLAKE_LVDS_S_M_MAX 118
  253. #define IRONLAKE_LVDS_S_P_MIN 28
  254. #define IRONLAKE_LVDS_S_P_MAX 112
  255. #define IRONLAKE_LVDS_S_P1_MIN 2
  256. #define IRONLAKE_LVDS_S_P1_MAX 8
  257. #define IRONLAKE_LVDS_S_P2_SLOW 14
  258. #define IRONLAKE_LVDS_S_P2_FAST 14
  259. /* LVDS dual-channel 120Mhz refclk */
  260. #define IRONLAKE_LVDS_D_N_MIN 1
  261. #define IRONLAKE_LVDS_D_N_MAX 3
  262. #define IRONLAKE_LVDS_D_M_MIN 79
  263. #define IRONLAKE_LVDS_D_M_MAX 127
  264. #define IRONLAKE_LVDS_D_P_MIN 14
  265. #define IRONLAKE_LVDS_D_P_MAX 56
  266. #define IRONLAKE_LVDS_D_P1_MIN 2
  267. #define IRONLAKE_LVDS_D_P1_MAX 8
  268. #define IRONLAKE_LVDS_D_P2_SLOW 7
  269. #define IRONLAKE_LVDS_D_P2_FAST 7
  270. /* LVDS single-channel 100Mhz refclk */
  271. #define IRONLAKE_LVDS_S_SSC_N_MIN 1
  272. #define IRONLAKE_LVDS_S_SSC_N_MAX 2
  273. #define IRONLAKE_LVDS_S_SSC_M_MIN 79
  274. #define IRONLAKE_LVDS_S_SSC_M_MAX 126
  275. #define IRONLAKE_LVDS_S_SSC_P_MIN 28
  276. #define IRONLAKE_LVDS_S_SSC_P_MAX 112
  277. #define IRONLAKE_LVDS_S_SSC_P1_MIN 2
  278. #define IRONLAKE_LVDS_S_SSC_P1_MAX 8
  279. #define IRONLAKE_LVDS_S_SSC_P2_SLOW 14
  280. #define IRONLAKE_LVDS_S_SSC_P2_FAST 14
  281. /* LVDS dual-channel 100Mhz refclk */
  282. #define IRONLAKE_LVDS_D_SSC_N_MIN 1
  283. #define IRONLAKE_LVDS_D_SSC_N_MAX 3
  284. #define IRONLAKE_LVDS_D_SSC_M_MIN 79
  285. #define IRONLAKE_LVDS_D_SSC_M_MAX 126
  286. #define IRONLAKE_LVDS_D_SSC_P_MIN 14
  287. #define IRONLAKE_LVDS_D_SSC_P_MAX 42
  288. #define IRONLAKE_LVDS_D_SSC_P1_MIN 2
  289. #define IRONLAKE_LVDS_D_SSC_P1_MAX 6
  290. #define IRONLAKE_LVDS_D_SSC_P2_SLOW 7
  291. #define IRONLAKE_LVDS_D_SSC_P2_FAST 7
  292. /* DisplayPort */
  293. #define IRONLAKE_DP_N_MIN 1
  294. #define IRONLAKE_DP_N_MAX 2
  295. #define IRONLAKE_DP_M_MIN 81
  296. #define IRONLAKE_DP_M_MAX 90
  297. #define IRONLAKE_DP_P_MIN 10
  298. #define IRONLAKE_DP_P_MAX 20
  299. #define IRONLAKE_DP_P2_FAST 10
  300. #define IRONLAKE_DP_P2_SLOW 10
  301. #define IRONLAKE_DP_P2_LIMIT 0
  302. #define IRONLAKE_DP_P1_MIN 1
  303. #define IRONLAKE_DP_P1_MAX 2
  304. /* FDI */
  305. #define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
  306. static bool
  307. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  308. int target, int refclk, intel_clock_t *best_clock);
  309. static bool
  310. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  311. int target, int refclk, intel_clock_t *best_clock);
  312. static bool
  313. intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
  314. int target, int refclk, intel_clock_t *best_clock);
  315. static bool
  316. intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
  317. int target, int refclk, intel_clock_t *best_clock);
  318. static inline u32 /* units of 100MHz */
  319. intel_fdi_link_freq(struct drm_device *dev)
  320. {
  321. struct drm_i915_private *dev_priv = dev->dev_private;
  322. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  323. }
  324. static const intel_limit_t intel_limits_i8xx_dvo = {
  325. .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
  326. .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
  327. .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
  328. .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
  329. .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
  330. .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
  331. .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
  332. .p1 = { .min = I8XX_P1_MIN, .max = I8XX_P1_MAX },
  333. .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
  334. .p2_slow = I8XX_P2_SLOW, .p2_fast = I8XX_P2_FAST },
  335. .find_pll = intel_find_best_PLL,
  336. };
  337. static const intel_limit_t intel_limits_i8xx_lvds = {
  338. .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
  339. .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
  340. .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
  341. .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
  342. .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
  343. .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
  344. .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
  345. .p1 = { .min = I8XX_P1_LVDS_MIN, .max = I8XX_P1_LVDS_MAX },
  346. .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
  347. .p2_slow = I8XX_P2_LVDS_SLOW, .p2_fast = I8XX_P2_LVDS_FAST },
  348. .find_pll = intel_find_best_PLL,
  349. };
  350. static const intel_limit_t intel_limits_i9xx_sdvo = {
  351. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  352. .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
  353. .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
  354. .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
  355. .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
  356. .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
  357. .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
  358. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  359. .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
  360. .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
  361. .find_pll = intel_find_best_PLL,
  362. };
  363. static const intel_limit_t intel_limits_i9xx_lvds = {
  364. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  365. .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
  366. .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
  367. .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
  368. .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
  369. .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
  370. .p = { .min = I9XX_P_LVDS_MIN, .max = I9XX_P_LVDS_MAX },
  371. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  372. /* The single-channel range is 25-112Mhz, and dual-channel
  373. * is 80-224Mhz. Prefer single channel as much as possible.
  374. */
  375. .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
  376. .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_FAST },
  377. .find_pll = intel_find_best_PLL,
  378. };
  379. /* below parameter and function is for G4X Chipset Family*/
  380. static const intel_limit_t intel_limits_g4x_sdvo = {
  381. .dot = { .min = G4X_DOT_SDVO_MIN, .max = G4X_DOT_SDVO_MAX },
  382. .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
  383. .n = { .min = G4X_N_SDVO_MIN, .max = G4X_N_SDVO_MAX },
  384. .m = { .min = G4X_M_SDVO_MIN, .max = G4X_M_SDVO_MAX },
  385. .m1 = { .min = G4X_M1_SDVO_MIN, .max = G4X_M1_SDVO_MAX },
  386. .m2 = { .min = G4X_M2_SDVO_MIN, .max = G4X_M2_SDVO_MAX },
  387. .p = { .min = G4X_P_SDVO_MIN, .max = G4X_P_SDVO_MAX },
  388. .p1 = { .min = G4X_P1_SDVO_MIN, .max = G4X_P1_SDVO_MAX},
  389. .p2 = { .dot_limit = G4X_P2_SDVO_LIMIT,
  390. .p2_slow = G4X_P2_SDVO_SLOW,
  391. .p2_fast = G4X_P2_SDVO_FAST
  392. },
  393. .find_pll = intel_g4x_find_best_PLL,
  394. };
  395. static const intel_limit_t intel_limits_g4x_hdmi = {
  396. .dot = { .min = G4X_DOT_HDMI_DAC_MIN, .max = G4X_DOT_HDMI_DAC_MAX },
  397. .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
  398. .n = { .min = G4X_N_HDMI_DAC_MIN, .max = G4X_N_HDMI_DAC_MAX },
  399. .m = { .min = G4X_M_HDMI_DAC_MIN, .max = G4X_M_HDMI_DAC_MAX },
  400. .m1 = { .min = G4X_M1_HDMI_DAC_MIN, .max = G4X_M1_HDMI_DAC_MAX },
  401. .m2 = { .min = G4X_M2_HDMI_DAC_MIN, .max = G4X_M2_HDMI_DAC_MAX },
  402. .p = { .min = G4X_P_HDMI_DAC_MIN, .max = G4X_P_HDMI_DAC_MAX },
  403. .p1 = { .min = G4X_P1_HDMI_DAC_MIN, .max = G4X_P1_HDMI_DAC_MAX},
  404. .p2 = { .dot_limit = G4X_P2_HDMI_DAC_LIMIT,
  405. .p2_slow = G4X_P2_HDMI_DAC_SLOW,
  406. .p2_fast = G4X_P2_HDMI_DAC_FAST
  407. },
  408. .find_pll = intel_g4x_find_best_PLL,
  409. };
  410. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  411. .dot = { .min = G4X_DOT_SINGLE_CHANNEL_LVDS_MIN,
  412. .max = G4X_DOT_SINGLE_CHANNEL_LVDS_MAX },
  413. .vco = { .min = G4X_VCO_MIN,
  414. .max = G4X_VCO_MAX },
  415. .n = { .min = G4X_N_SINGLE_CHANNEL_LVDS_MIN,
  416. .max = G4X_N_SINGLE_CHANNEL_LVDS_MAX },
  417. .m = { .min = G4X_M_SINGLE_CHANNEL_LVDS_MIN,
  418. .max = G4X_M_SINGLE_CHANNEL_LVDS_MAX },
  419. .m1 = { .min = G4X_M1_SINGLE_CHANNEL_LVDS_MIN,
  420. .max = G4X_M1_SINGLE_CHANNEL_LVDS_MAX },
  421. .m2 = { .min = G4X_M2_SINGLE_CHANNEL_LVDS_MIN,
  422. .max = G4X_M2_SINGLE_CHANNEL_LVDS_MAX },
  423. .p = { .min = G4X_P_SINGLE_CHANNEL_LVDS_MIN,
  424. .max = G4X_P_SINGLE_CHANNEL_LVDS_MAX },
  425. .p1 = { .min = G4X_P1_SINGLE_CHANNEL_LVDS_MIN,
  426. .max = G4X_P1_SINGLE_CHANNEL_LVDS_MAX },
  427. .p2 = { .dot_limit = G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT,
  428. .p2_slow = G4X_P2_SINGLE_CHANNEL_LVDS_SLOW,
  429. .p2_fast = G4X_P2_SINGLE_CHANNEL_LVDS_FAST
  430. },
  431. .find_pll = intel_g4x_find_best_PLL,
  432. };
  433. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  434. .dot = { .min = G4X_DOT_DUAL_CHANNEL_LVDS_MIN,
  435. .max = G4X_DOT_DUAL_CHANNEL_LVDS_MAX },
  436. .vco = { .min = G4X_VCO_MIN,
  437. .max = G4X_VCO_MAX },
  438. .n = { .min = G4X_N_DUAL_CHANNEL_LVDS_MIN,
  439. .max = G4X_N_DUAL_CHANNEL_LVDS_MAX },
  440. .m = { .min = G4X_M_DUAL_CHANNEL_LVDS_MIN,
  441. .max = G4X_M_DUAL_CHANNEL_LVDS_MAX },
  442. .m1 = { .min = G4X_M1_DUAL_CHANNEL_LVDS_MIN,
  443. .max = G4X_M1_DUAL_CHANNEL_LVDS_MAX },
  444. .m2 = { .min = G4X_M2_DUAL_CHANNEL_LVDS_MIN,
  445. .max = G4X_M2_DUAL_CHANNEL_LVDS_MAX },
  446. .p = { .min = G4X_P_DUAL_CHANNEL_LVDS_MIN,
  447. .max = G4X_P_DUAL_CHANNEL_LVDS_MAX },
  448. .p1 = { .min = G4X_P1_DUAL_CHANNEL_LVDS_MIN,
  449. .max = G4X_P1_DUAL_CHANNEL_LVDS_MAX },
  450. .p2 = { .dot_limit = G4X_P2_DUAL_CHANNEL_LVDS_LIMIT,
  451. .p2_slow = G4X_P2_DUAL_CHANNEL_LVDS_SLOW,
  452. .p2_fast = G4X_P2_DUAL_CHANNEL_LVDS_FAST
  453. },
  454. .find_pll = intel_g4x_find_best_PLL,
  455. };
  456. static const intel_limit_t intel_limits_g4x_display_port = {
  457. .dot = { .min = G4X_DOT_DISPLAY_PORT_MIN,
  458. .max = G4X_DOT_DISPLAY_PORT_MAX },
  459. .vco = { .min = G4X_VCO_MIN,
  460. .max = G4X_VCO_MAX},
  461. .n = { .min = G4X_N_DISPLAY_PORT_MIN,
  462. .max = G4X_N_DISPLAY_PORT_MAX },
  463. .m = { .min = G4X_M_DISPLAY_PORT_MIN,
  464. .max = G4X_M_DISPLAY_PORT_MAX },
  465. .m1 = { .min = G4X_M1_DISPLAY_PORT_MIN,
  466. .max = G4X_M1_DISPLAY_PORT_MAX },
  467. .m2 = { .min = G4X_M2_DISPLAY_PORT_MIN,
  468. .max = G4X_M2_DISPLAY_PORT_MAX },
  469. .p = { .min = G4X_P_DISPLAY_PORT_MIN,
  470. .max = G4X_P_DISPLAY_PORT_MAX },
  471. .p1 = { .min = G4X_P1_DISPLAY_PORT_MIN,
  472. .max = G4X_P1_DISPLAY_PORT_MAX},
  473. .p2 = { .dot_limit = G4X_P2_DISPLAY_PORT_LIMIT,
  474. .p2_slow = G4X_P2_DISPLAY_PORT_SLOW,
  475. .p2_fast = G4X_P2_DISPLAY_PORT_FAST },
  476. .find_pll = intel_find_pll_g4x_dp,
  477. };
  478. static const intel_limit_t intel_limits_pineview_sdvo = {
  479. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX},
  480. .vco = { .min = PINEVIEW_VCO_MIN, .max = PINEVIEW_VCO_MAX },
  481. .n = { .min = PINEVIEW_N_MIN, .max = PINEVIEW_N_MAX },
  482. .m = { .min = PINEVIEW_M_MIN, .max = PINEVIEW_M_MAX },
  483. .m1 = { .min = PINEVIEW_M1_MIN, .max = PINEVIEW_M1_MAX },
  484. .m2 = { .min = PINEVIEW_M2_MIN, .max = PINEVIEW_M2_MAX },
  485. .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
  486. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  487. .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
  488. .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
  489. .find_pll = intel_find_best_PLL,
  490. };
  491. static const intel_limit_t intel_limits_pineview_lvds = {
  492. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  493. .vco = { .min = PINEVIEW_VCO_MIN, .max = PINEVIEW_VCO_MAX },
  494. .n = { .min = PINEVIEW_N_MIN, .max = PINEVIEW_N_MAX },
  495. .m = { .min = PINEVIEW_M_MIN, .max = PINEVIEW_M_MAX },
  496. .m1 = { .min = PINEVIEW_M1_MIN, .max = PINEVIEW_M1_MAX },
  497. .m2 = { .min = PINEVIEW_M2_MIN, .max = PINEVIEW_M2_MAX },
  498. .p = { .min = PINEVIEW_P_LVDS_MIN, .max = PINEVIEW_P_LVDS_MAX },
  499. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  500. /* Pineview only supports single-channel mode. */
  501. .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
  502. .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_SLOW },
  503. .find_pll = intel_find_best_PLL,
  504. };
  505. static const intel_limit_t intel_limits_ironlake_dac = {
  506. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  507. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  508. .n = { .min = IRONLAKE_DAC_N_MIN, .max = IRONLAKE_DAC_N_MAX },
  509. .m = { .min = IRONLAKE_DAC_M_MIN, .max = IRONLAKE_DAC_M_MAX },
  510. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  511. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  512. .p = { .min = IRONLAKE_DAC_P_MIN, .max = IRONLAKE_DAC_P_MAX },
  513. .p1 = { .min = IRONLAKE_DAC_P1_MIN, .max = IRONLAKE_DAC_P1_MAX },
  514. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  515. .p2_slow = IRONLAKE_DAC_P2_SLOW,
  516. .p2_fast = IRONLAKE_DAC_P2_FAST },
  517. .find_pll = intel_g4x_find_best_PLL,
  518. };
  519. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  520. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  521. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  522. .n = { .min = IRONLAKE_LVDS_S_N_MIN, .max = IRONLAKE_LVDS_S_N_MAX },
  523. .m = { .min = IRONLAKE_LVDS_S_M_MIN, .max = IRONLAKE_LVDS_S_M_MAX },
  524. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  525. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  526. .p = { .min = IRONLAKE_LVDS_S_P_MIN, .max = IRONLAKE_LVDS_S_P_MAX },
  527. .p1 = { .min = IRONLAKE_LVDS_S_P1_MIN, .max = IRONLAKE_LVDS_S_P1_MAX },
  528. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  529. .p2_slow = IRONLAKE_LVDS_S_P2_SLOW,
  530. .p2_fast = IRONLAKE_LVDS_S_P2_FAST },
  531. .find_pll = intel_g4x_find_best_PLL,
  532. };
  533. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  534. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  535. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  536. .n = { .min = IRONLAKE_LVDS_D_N_MIN, .max = IRONLAKE_LVDS_D_N_MAX },
  537. .m = { .min = IRONLAKE_LVDS_D_M_MIN, .max = IRONLAKE_LVDS_D_M_MAX },
  538. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  539. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  540. .p = { .min = IRONLAKE_LVDS_D_P_MIN, .max = IRONLAKE_LVDS_D_P_MAX },
  541. .p1 = { .min = IRONLAKE_LVDS_D_P1_MIN, .max = IRONLAKE_LVDS_D_P1_MAX },
  542. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  543. .p2_slow = IRONLAKE_LVDS_D_P2_SLOW,
  544. .p2_fast = IRONLAKE_LVDS_D_P2_FAST },
  545. .find_pll = intel_g4x_find_best_PLL,
  546. };
  547. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  548. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  549. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  550. .n = { .min = IRONLAKE_LVDS_S_SSC_N_MIN, .max = IRONLAKE_LVDS_S_SSC_N_MAX },
  551. .m = { .min = IRONLAKE_LVDS_S_SSC_M_MIN, .max = IRONLAKE_LVDS_S_SSC_M_MAX },
  552. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  553. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  554. .p = { .min = IRONLAKE_LVDS_S_SSC_P_MIN, .max = IRONLAKE_LVDS_S_SSC_P_MAX },
  555. .p1 = { .min = IRONLAKE_LVDS_S_SSC_P1_MIN,.max = IRONLAKE_LVDS_S_SSC_P1_MAX },
  556. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  557. .p2_slow = IRONLAKE_LVDS_S_SSC_P2_SLOW,
  558. .p2_fast = IRONLAKE_LVDS_S_SSC_P2_FAST },
  559. .find_pll = intel_g4x_find_best_PLL,
  560. };
  561. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  562. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  563. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  564. .n = { .min = IRONLAKE_LVDS_D_SSC_N_MIN, .max = IRONLAKE_LVDS_D_SSC_N_MAX },
  565. .m = { .min = IRONLAKE_LVDS_D_SSC_M_MIN, .max = IRONLAKE_LVDS_D_SSC_M_MAX },
  566. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  567. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  568. .p = { .min = IRONLAKE_LVDS_D_SSC_P_MIN, .max = IRONLAKE_LVDS_D_SSC_P_MAX },
  569. .p1 = { .min = IRONLAKE_LVDS_D_SSC_P1_MIN,.max = IRONLAKE_LVDS_D_SSC_P1_MAX },
  570. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  571. .p2_slow = IRONLAKE_LVDS_D_SSC_P2_SLOW,
  572. .p2_fast = IRONLAKE_LVDS_D_SSC_P2_FAST },
  573. .find_pll = intel_g4x_find_best_PLL,
  574. };
  575. static const intel_limit_t intel_limits_ironlake_display_port = {
  576. .dot = { .min = IRONLAKE_DOT_MIN,
  577. .max = IRONLAKE_DOT_MAX },
  578. .vco = { .min = IRONLAKE_VCO_MIN,
  579. .max = IRONLAKE_VCO_MAX},
  580. .n = { .min = IRONLAKE_DP_N_MIN,
  581. .max = IRONLAKE_DP_N_MAX },
  582. .m = { .min = IRONLAKE_DP_M_MIN,
  583. .max = IRONLAKE_DP_M_MAX },
  584. .m1 = { .min = IRONLAKE_M1_MIN,
  585. .max = IRONLAKE_M1_MAX },
  586. .m2 = { .min = IRONLAKE_M2_MIN,
  587. .max = IRONLAKE_M2_MAX },
  588. .p = { .min = IRONLAKE_DP_P_MIN,
  589. .max = IRONLAKE_DP_P_MAX },
  590. .p1 = { .min = IRONLAKE_DP_P1_MIN,
  591. .max = IRONLAKE_DP_P1_MAX},
  592. .p2 = { .dot_limit = IRONLAKE_DP_P2_LIMIT,
  593. .p2_slow = IRONLAKE_DP_P2_SLOW,
  594. .p2_fast = IRONLAKE_DP_P2_FAST },
  595. .find_pll = intel_find_pll_ironlake_dp,
  596. };
  597. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc)
  598. {
  599. struct drm_device *dev = crtc->dev;
  600. struct drm_i915_private *dev_priv = dev->dev_private;
  601. const intel_limit_t *limit;
  602. int refclk = 120;
  603. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  604. if (dev_priv->lvds_use_ssc && dev_priv->lvds_ssc_freq == 100)
  605. refclk = 100;
  606. if ((I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) ==
  607. LVDS_CLKB_POWER_UP) {
  608. /* LVDS dual channel */
  609. if (refclk == 100)
  610. limit = &intel_limits_ironlake_dual_lvds_100m;
  611. else
  612. limit = &intel_limits_ironlake_dual_lvds;
  613. } else {
  614. if (refclk == 100)
  615. limit = &intel_limits_ironlake_single_lvds_100m;
  616. else
  617. limit = &intel_limits_ironlake_single_lvds;
  618. }
  619. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  620. HAS_eDP)
  621. limit = &intel_limits_ironlake_display_port;
  622. else
  623. limit = &intel_limits_ironlake_dac;
  624. return limit;
  625. }
  626. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  627. {
  628. struct drm_device *dev = crtc->dev;
  629. struct drm_i915_private *dev_priv = dev->dev_private;
  630. const intel_limit_t *limit;
  631. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  632. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  633. LVDS_CLKB_POWER_UP)
  634. /* LVDS with dual channel */
  635. limit = &intel_limits_g4x_dual_channel_lvds;
  636. else
  637. /* LVDS with dual channel */
  638. limit = &intel_limits_g4x_single_channel_lvds;
  639. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  640. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  641. limit = &intel_limits_g4x_hdmi;
  642. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  643. limit = &intel_limits_g4x_sdvo;
  644. } else if (intel_pipe_has_type (crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  645. limit = &intel_limits_g4x_display_port;
  646. } else /* The option is for other outputs */
  647. limit = &intel_limits_i9xx_sdvo;
  648. return limit;
  649. }
  650. static const intel_limit_t *intel_limit(struct drm_crtc *crtc)
  651. {
  652. struct drm_device *dev = crtc->dev;
  653. const intel_limit_t *limit;
  654. if (HAS_PCH_SPLIT(dev))
  655. limit = intel_ironlake_limit(crtc);
  656. else if (IS_G4X(dev)) {
  657. limit = intel_g4x_limit(crtc);
  658. } else if (IS_I9XX(dev) && !IS_PINEVIEW(dev)) {
  659. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  660. limit = &intel_limits_i9xx_lvds;
  661. else
  662. limit = &intel_limits_i9xx_sdvo;
  663. } else if (IS_PINEVIEW(dev)) {
  664. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  665. limit = &intel_limits_pineview_lvds;
  666. else
  667. limit = &intel_limits_pineview_sdvo;
  668. } else {
  669. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  670. limit = &intel_limits_i8xx_lvds;
  671. else
  672. limit = &intel_limits_i8xx_dvo;
  673. }
  674. return limit;
  675. }
  676. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  677. static void pineview_clock(int refclk, intel_clock_t *clock)
  678. {
  679. clock->m = clock->m2 + 2;
  680. clock->p = clock->p1 * clock->p2;
  681. clock->vco = refclk * clock->m / clock->n;
  682. clock->dot = clock->vco / clock->p;
  683. }
  684. static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
  685. {
  686. if (IS_PINEVIEW(dev)) {
  687. pineview_clock(refclk, clock);
  688. return;
  689. }
  690. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  691. clock->p = clock->p1 * clock->p2;
  692. clock->vco = refclk * clock->m / (clock->n + 2);
  693. clock->dot = clock->vco / clock->p;
  694. }
  695. /**
  696. * Returns whether any output on the specified pipe is of the specified type
  697. */
  698. bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  699. {
  700. struct drm_device *dev = crtc->dev;
  701. struct drm_mode_config *mode_config = &dev->mode_config;
  702. struct intel_encoder *encoder;
  703. list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
  704. if (encoder->base.crtc == crtc && encoder->type == type)
  705. return true;
  706. return false;
  707. }
  708. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  709. /**
  710. * Returns whether the given set of divisors are valid for a given refclk with
  711. * the given connectors.
  712. */
  713. static bool intel_PLL_is_valid(struct drm_crtc *crtc, intel_clock_t *clock)
  714. {
  715. const intel_limit_t *limit = intel_limit (crtc);
  716. struct drm_device *dev = crtc->dev;
  717. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  718. INTELPllInvalid ("p1 out of range\n");
  719. if (clock->p < limit->p.min || limit->p.max < clock->p)
  720. INTELPllInvalid ("p out of range\n");
  721. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  722. INTELPllInvalid ("m2 out of range\n");
  723. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  724. INTELPllInvalid ("m1 out of range\n");
  725. if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
  726. INTELPllInvalid ("m1 <= m2\n");
  727. if (clock->m < limit->m.min || limit->m.max < clock->m)
  728. INTELPllInvalid ("m out of range\n");
  729. if (clock->n < limit->n.min || limit->n.max < clock->n)
  730. INTELPllInvalid ("n out of range\n");
  731. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  732. INTELPllInvalid ("vco out of range\n");
  733. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  734. * connector, etc., rather than just a single range.
  735. */
  736. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  737. INTELPllInvalid ("dot out of range\n");
  738. return true;
  739. }
  740. static bool
  741. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  742. int target, int refclk, intel_clock_t *best_clock)
  743. {
  744. struct drm_device *dev = crtc->dev;
  745. struct drm_i915_private *dev_priv = dev->dev_private;
  746. intel_clock_t clock;
  747. int err = target;
  748. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  749. (I915_READ(LVDS)) != 0) {
  750. /*
  751. * For LVDS, if the panel is on, just rely on its current
  752. * settings for dual-channel. We haven't figured out how to
  753. * reliably set up different single/dual channel state, if we
  754. * even can.
  755. */
  756. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  757. LVDS_CLKB_POWER_UP)
  758. clock.p2 = limit->p2.p2_fast;
  759. else
  760. clock.p2 = limit->p2.p2_slow;
  761. } else {
  762. if (target < limit->p2.dot_limit)
  763. clock.p2 = limit->p2.p2_slow;
  764. else
  765. clock.p2 = limit->p2.p2_fast;
  766. }
  767. memset (best_clock, 0, sizeof (*best_clock));
  768. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  769. clock.m1++) {
  770. for (clock.m2 = limit->m2.min;
  771. clock.m2 <= limit->m2.max; clock.m2++) {
  772. /* m1 is always 0 in Pineview */
  773. if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
  774. break;
  775. for (clock.n = limit->n.min;
  776. clock.n <= limit->n.max; clock.n++) {
  777. for (clock.p1 = limit->p1.min;
  778. clock.p1 <= limit->p1.max; clock.p1++) {
  779. int this_err;
  780. intel_clock(dev, refclk, &clock);
  781. if (!intel_PLL_is_valid(crtc, &clock))
  782. continue;
  783. this_err = abs(clock.dot - target);
  784. if (this_err < err) {
  785. *best_clock = clock;
  786. err = this_err;
  787. }
  788. }
  789. }
  790. }
  791. }
  792. return (err != target);
  793. }
  794. static bool
  795. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  796. int target, int refclk, intel_clock_t *best_clock)
  797. {
  798. struct drm_device *dev = crtc->dev;
  799. struct drm_i915_private *dev_priv = dev->dev_private;
  800. intel_clock_t clock;
  801. int max_n;
  802. bool found;
  803. /* approximately equals target * 0.00585 */
  804. int err_most = (target >> 8) + (target >> 9);
  805. found = false;
  806. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  807. int lvds_reg;
  808. if (HAS_PCH_SPLIT(dev))
  809. lvds_reg = PCH_LVDS;
  810. else
  811. lvds_reg = LVDS;
  812. if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
  813. LVDS_CLKB_POWER_UP)
  814. clock.p2 = limit->p2.p2_fast;
  815. else
  816. clock.p2 = limit->p2.p2_slow;
  817. } else {
  818. if (target < limit->p2.dot_limit)
  819. clock.p2 = limit->p2.p2_slow;
  820. else
  821. clock.p2 = limit->p2.p2_fast;
  822. }
  823. memset(best_clock, 0, sizeof(*best_clock));
  824. max_n = limit->n.max;
  825. /* based on hardware requirement, prefer smaller n to precision */
  826. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  827. /* based on hardware requirement, prefere larger m1,m2 */
  828. for (clock.m1 = limit->m1.max;
  829. clock.m1 >= limit->m1.min; clock.m1--) {
  830. for (clock.m2 = limit->m2.max;
  831. clock.m2 >= limit->m2.min; clock.m2--) {
  832. for (clock.p1 = limit->p1.max;
  833. clock.p1 >= limit->p1.min; clock.p1--) {
  834. int this_err;
  835. intel_clock(dev, refclk, &clock);
  836. if (!intel_PLL_is_valid(crtc, &clock))
  837. continue;
  838. this_err = abs(clock.dot - target) ;
  839. if (this_err < err_most) {
  840. *best_clock = clock;
  841. err_most = this_err;
  842. max_n = clock.n;
  843. found = true;
  844. }
  845. }
  846. }
  847. }
  848. }
  849. return found;
  850. }
  851. static bool
  852. intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  853. int target, int refclk, intel_clock_t *best_clock)
  854. {
  855. struct drm_device *dev = crtc->dev;
  856. intel_clock_t clock;
  857. /* return directly when it is eDP */
  858. if (HAS_eDP)
  859. return true;
  860. if (target < 200000) {
  861. clock.n = 1;
  862. clock.p1 = 2;
  863. clock.p2 = 10;
  864. clock.m1 = 12;
  865. clock.m2 = 9;
  866. } else {
  867. clock.n = 2;
  868. clock.p1 = 1;
  869. clock.p2 = 10;
  870. clock.m1 = 14;
  871. clock.m2 = 8;
  872. }
  873. intel_clock(dev, refclk, &clock);
  874. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  875. return true;
  876. }
  877. /* DisplayPort has only two frequencies, 162MHz and 270MHz */
  878. static bool
  879. intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  880. int target, int refclk, intel_clock_t *best_clock)
  881. {
  882. intel_clock_t clock;
  883. if (target < 200000) {
  884. clock.p1 = 2;
  885. clock.p2 = 10;
  886. clock.n = 2;
  887. clock.m1 = 23;
  888. clock.m2 = 8;
  889. } else {
  890. clock.p1 = 1;
  891. clock.p2 = 10;
  892. clock.n = 1;
  893. clock.m1 = 14;
  894. clock.m2 = 2;
  895. }
  896. clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
  897. clock.p = (clock.p1 * clock.p2);
  898. clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
  899. clock.vco = 0;
  900. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  901. return true;
  902. }
  903. /**
  904. * intel_wait_for_vblank - wait for vblank on a given pipe
  905. * @dev: drm device
  906. * @pipe: pipe to wait for
  907. *
  908. * Wait for vblank to occur on a given pipe. Needed for various bits of
  909. * mode setting code.
  910. */
  911. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  912. {
  913. struct drm_i915_private *dev_priv = dev->dev_private;
  914. int pipestat_reg = (pipe == 0 ? PIPEASTAT : PIPEBSTAT);
  915. /* Clear existing vblank status. Note this will clear any other
  916. * sticky status fields as well.
  917. *
  918. * This races with i915_driver_irq_handler() with the result
  919. * that either function could miss a vblank event. Here it is not
  920. * fatal, as we will either wait upon the next vblank interrupt or
  921. * timeout. Generally speaking intel_wait_for_vblank() is only
  922. * called during modeset at which time the GPU should be idle and
  923. * should *not* be performing page flips and thus not waiting on
  924. * vblanks...
  925. * Currently, the result of us stealing a vblank from the irq
  926. * handler is that a single frame will be skipped during swapbuffers.
  927. */
  928. I915_WRITE(pipestat_reg,
  929. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  930. /* Wait for vblank interrupt bit to set */
  931. if (wait_for(I915_READ(pipestat_reg) &
  932. PIPE_VBLANK_INTERRUPT_STATUS,
  933. 50))
  934. DRM_DEBUG_KMS("vblank wait timed out\n");
  935. }
  936. /**
  937. * intel_wait_for_vblank_off - wait for vblank after disabling a pipe
  938. * @dev: drm device
  939. * @pipe: pipe to wait for
  940. *
  941. * After disabling a pipe, we can't wait for vblank in the usual way,
  942. * spinning on the vblank interrupt status bit, since we won't actually
  943. * see an interrupt when the pipe is disabled.
  944. *
  945. * So this function waits for the display line value to settle (it
  946. * usually ends up stopping at the start of the next frame).
  947. */
  948. void intel_wait_for_vblank_off(struct drm_device *dev, int pipe)
  949. {
  950. struct drm_i915_private *dev_priv = dev->dev_private;
  951. int pipedsl_reg = (pipe == 0 ? PIPEADSL : PIPEBDSL);
  952. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  953. u32 last_line;
  954. /* Wait for the display line to settle */
  955. do {
  956. last_line = I915_READ(pipedsl_reg) & DSL_LINEMASK;
  957. mdelay(5);
  958. } while (((I915_READ(pipedsl_reg) & DSL_LINEMASK) != last_line) &&
  959. time_after(timeout, jiffies));
  960. if (time_after(jiffies, timeout))
  961. DRM_DEBUG_KMS("vblank wait timed out\n");
  962. }
  963. static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  964. {
  965. struct drm_device *dev = crtc->dev;
  966. struct drm_i915_private *dev_priv = dev->dev_private;
  967. struct drm_framebuffer *fb = crtc->fb;
  968. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  969. struct drm_i915_gem_object *obj_priv = to_intel_bo(intel_fb->obj);
  970. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  971. int plane, i;
  972. u32 fbc_ctl, fbc_ctl2;
  973. if (fb->pitch == dev_priv->cfb_pitch &&
  974. obj_priv->fence_reg == dev_priv->cfb_fence &&
  975. intel_crtc->plane == dev_priv->cfb_plane &&
  976. I915_READ(FBC_CONTROL) & FBC_CTL_EN)
  977. return;
  978. i8xx_disable_fbc(dev);
  979. dev_priv->cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
  980. if (fb->pitch < dev_priv->cfb_pitch)
  981. dev_priv->cfb_pitch = fb->pitch;
  982. /* FBC_CTL wants 64B units */
  983. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  984. dev_priv->cfb_fence = obj_priv->fence_reg;
  985. dev_priv->cfb_plane = intel_crtc->plane;
  986. plane = dev_priv->cfb_plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
  987. /* Clear old tags */
  988. for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
  989. I915_WRITE(FBC_TAG + (i * 4), 0);
  990. /* Set it up... */
  991. fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | plane;
  992. if (obj_priv->tiling_mode != I915_TILING_NONE)
  993. fbc_ctl2 |= FBC_CTL_CPU_FENCE;
  994. I915_WRITE(FBC_CONTROL2, fbc_ctl2);
  995. I915_WRITE(FBC_FENCE_OFF, crtc->y);
  996. /* enable it... */
  997. fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
  998. if (IS_I945GM(dev))
  999. fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
  1000. fbc_ctl |= (dev_priv->cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
  1001. fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
  1002. if (obj_priv->tiling_mode != I915_TILING_NONE)
  1003. fbc_ctl |= dev_priv->cfb_fence;
  1004. I915_WRITE(FBC_CONTROL, fbc_ctl);
  1005. DRM_DEBUG_KMS("enabled FBC, pitch %ld, yoff %d, plane %d, ",
  1006. dev_priv->cfb_pitch, crtc->y, dev_priv->cfb_plane);
  1007. }
  1008. void i8xx_disable_fbc(struct drm_device *dev)
  1009. {
  1010. struct drm_i915_private *dev_priv = dev->dev_private;
  1011. u32 fbc_ctl;
  1012. /* Disable compression */
  1013. fbc_ctl = I915_READ(FBC_CONTROL);
  1014. fbc_ctl &= ~FBC_CTL_EN;
  1015. I915_WRITE(FBC_CONTROL, fbc_ctl);
  1016. /* Wait for compressing bit to clear */
  1017. if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
  1018. DRM_DEBUG_KMS("FBC idle timed out\n");
  1019. return;
  1020. }
  1021. DRM_DEBUG_KMS("disabled FBC\n");
  1022. }
  1023. static bool i8xx_fbc_enabled(struct drm_device *dev)
  1024. {
  1025. struct drm_i915_private *dev_priv = dev->dev_private;
  1026. return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
  1027. }
  1028. static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1029. {
  1030. struct drm_device *dev = crtc->dev;
  1031. struct drm_i915_private *dev_priv = dev->dev_private;
  1032. struct drm_framebuffer *fb = crtc->fb;
  1033. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1034. struct drm_i915_gem_object *obj_priv = to_intel_bo(intel_fb->obj);
  1035. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1036. int plane = (intel_crtc->plane == 0 ? DPFC_CTL_PLANEA :
  1037. DPFC_CTL_PLANEB);
  1038. unsigned long stall_watermark = 200;
  1039. u32 dpfc_ctl;
  1040. dpfc_ctl = I915_READ(DPFC_CONTROL);
  1041. if (dpfc_ctl & DPFC_CTL_EN) {
  1042. if (dev_priv->cfb_pitch == dev_priv->cfb_pitch / 64 - 1 &&
  1043. dev_priv->cfb_fence == obj_priv->fence_reg &&
  1044. dev_priv->cfb_plane == intel_crtc->plane &&
  1045. dev_priv->cfb_y == crtc->y)
  1046. return;
  1047. I915_WRITE(DPFC_CONTROL, dpfc_ctl & ~DPFC_CTL_EN);
  1048. POSTING_READ(DPFC_CONTROL);
  1049. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1050. }
  1051. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  1052. dev_priv->cfb_fence = obj_priv->fence_reg;
  1053. dev_priv->cfb_plane = intel_crtc->plane;
  1054. dev_priv->cfb_y = crtc->y;
  1055. dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
  1056. if (obj_priv->tiling_mode != I915_TILING_NONE) {
  1057. dpfc_ctl |= DPFC_CTL_FENCE_EN | dev_priv->cfb_fence;
  1058. I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
  1059. } else {
  1060. I915_WRITE(DPFC_CHICKEN, ~DPFC_HT_MODIFY);
  1061. }
  1062. I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  1063. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  1064. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1065. I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
  1066. /* enable it... */
  1067. I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
  1068. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1069. }
  1070. void g4x_disable_fbc(struct drm_device *dev)
  1071. {
  1072. struct drm_i915_private *dev_priv = dev->dev_private;
  1073. u32 dpfc_ctl;
  1074. /* Disable compression */
  1075. dpfc_ctl = I915_READ(DPFC_CONTROL);
  1076. if (dpfc_ctl & DPFC_CTL_EN) {
  1077. dpfc_ctl &= ~DPFC_CTL_EN;
  1078. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  1079. DRM_DEBUG_KMS("disabled FBC\n");
  1080. }
  1081. }
  1082. static bool g4x_fbc_enabled(struct drm_device *dev)
  1083. {
  1084. struct drm_i915_private *dev_priv = dev->dev_private;
  1085. return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
  1086. }
  1087. static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1088. {
  1089. struct drm_device *dev = crtc->dev;
  1090. struct drm_i915_private *dev_priv = dev->dev_private;
  1091. struct drm_framebuffer *fb = crtc->fb;
  1092. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1093. struct drm_i915_gem_object *obj_priv = to_intel_bo(intel_fb->obj);
  1094. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1095. int plane = (intel_crtc->plane == 0) ? DPFC_CTL_PLANEA :
  1096. DPFC_CTL_PLANEB;
  1097. unsigned long stall_watermark = 200;
  1098. u32 dpfc_ctl;
  1099. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1100. if (dpfc_ctl & DPFC_CTL_EN) {
  1101. if (dev_priv->cfb_pitch == dev_priv->cfb_pitch / 64 - 1 &&
  1102. dev_priv->cfb_fence == obj_priv->fence_reg &&
  1103. dev_priv->cfb_plane == intel_crtc->plane &&
  1104. dev_priv->cfb_offset == obj_priv->gtt_offset &&
  1105. dev_priv->cfb_y == crtc->y)
  1106. return;
  1107. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl & ~DPFC_CTL_EN);
  1108. POSTING_READ(ILK_DPFC_CONTROL);
  1109. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1110. }
  1111. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  1112. dev_priv->cfb_fence = obj_priv->fence_reg;
  1113. dev_priv->cfb_plane = intel_crtc->plane;
  1114. dev_priv->cfb_offset = obj_priv->gtt_offset;
  1115. dev_priv->cfb_y = crtc->y;
  1116. dpfc_ctl &= DPFC_RESERVED;
  1117. dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
  1118. if (obj_priv->tiling_mode != I915_TILING_NONE) {
  1119. dpfc_ctl |= (DPFC_CTL_FENCE_EN | dev_priv->cfb_fence);
  1120. I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
  1121. } else {
  1122. I915_WRITE(ILK_DPFC_CHICKEN, ~DPFC_HT_MODIFY);
  1123. }
  1124. I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  1125. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  1126. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1127. I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
  1128. I915_WRITE(ILK_FBC_RT_BASE, obj_priv->gtt_offset | ILK_FBC_RT_VALID);
  1129. /* enable it... */
  1130. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  1131. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1132. }
  1133. void ironlake_disable_fbc(struct drm_device *dev)
  1134. {
  1135. struct drm_i915_private *dev_priv = dev->dev_private;
  1136. u32 dpfc_ctl;
  1137. /* Disable compression */
  1138. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1139. if (dpfc_ctl & DPFC_CTL_EN) {
  1140. dpfc_ctl &= ~DPFC_CTL_EN;
  1141. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
  1142. DRM_DEBUG_KMS("disabled FBC\n");
  1143. }
  1144. }
  1145. static bool ironlake_fbc_enabled(struct drm_device *dev)
  1146. {
  1147. struct drm_i915_private *dev_priv = dev->dev_private;
  1148. return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
  1149. }
  1150. bool intel_fbc_enabled(struct drm_device *dev)
  1151. {
  1152. struct drm_i915_private *dev_priv = dev->dev_private;
  1153. if (!dev_priv->display.fbc_enabled)
  1154. return false;
  1155. return dev_priv->display.fbc_enabled(dev);
  1156. }
  1157. void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1158. {
  1159. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  1160. if (!dev_priv->display.enable_fbc)
  1161. return;
  1162. dev_priv->display.enable_fbc(crtc, interval);
  1163. }
  1164. void intel_disable_fbc(struct drm_device *dev)
  1165. {
  1166. struct drm_i915_private *dev_priv = dev->dev_private;
  1167. if (!dev_priv->display.disable_fbc)
  1168. return;
  1169. dev_priv->display.disable_fbc(dev);
  1170. }
  1171. /**
  1172. * intel_update_fbc - enable/disable FBC as needed
  1173. * @dev: the drm_device
  1174. *
  1175. * Set up the framebuffer compression hardware at mode set time. We
  1176. * enable it if possible:
  1177. * - plane A only (on pre-965)
  1178. * - no pixel mulitply/line duplication
  1179. * - no alpha buffer discard
  1180. * - no dual wide
  1181. * - framebuffer <= 2048 in width, 1536 in height
  1182. *
  1183. * We can't assume that any compression will take place (worst case),
  1184. * so the compressed buffer has to be the same size as the uncompressed
  1185. * one. It also must reside (along with the line length buffer) in
  1186. * stolen memory.
  1187. *
  1188. * We need to enable/disable FBC on a global basis.
  1189. */
  1190. static void intel_update_fbc(struct drm_device *dev)
  1191. {
  1192. struct drm_i915_private *dev_priv = dev->dev_private;
  1193. struct drm_crtc *crtc = NULL, *tmp_crtc;
  1194. struct intel_crtc *intel_crtc;
  1195. struct drm_framebuffer *fb;
  1196. struct intel_framebuffer *intel_fb;
  1197. struct drm_i915_gem_object *obj_priv;
  1198. DRM_DEBUG_KMS("\n");
  1199. if (!i915_powersave)
  1200. return;
  1201. if (!I915_HAS_FBC(dev))
  1202. return;
  1203. /*
  1204. * If FBC is already on, we just have to verify that we can
  1205. * keep it that way...
  1206. * Need to disable if:
  1207. * - more than one pipe is active
  1208. * - changing FBC params (stride, fence, mode)
  1209. * - new fb is too large to fit in compressed buffer
  1210. * - going to an unsupported config (interlace, pixel multiply, etc.)
  1211. */
  1212. list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
  1213. if (tmp_crtc->enabled) {
  1214. if (crtc) {
  1215. DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
  1216. dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
  1217. goto out_disable;
  1218. }
  1219. crtc = tmp_crtc;
  1220. }
  1221. }
  1222. if (!crtc || crtc->fb == NULL) {
  1223. DRM_DEBUG_KMS("no output, disabling\n");
  1224. dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
  1225. goto out_disable;
  1226. }
  1227. intel_crtc = to_intel_crtc(crtc);
  1228. fb = crtc->fb;
  1229. intel_fb = to_intel_framebuffer(fb);
  1230. obj_priv = to_intel_bo(intel_fb->obj);
  1231. if (intel_fb->obj->size > dev_priv->cfb_size) {
  1232. DRM_DEBUG_KMS("framebuffer too large, disabling "
  1233. "compression\n");
  1234. dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
  1235. goto out_disable;
  1236. }
  1237. if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
  1238. (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
  1239. DRM_DEBUG_KMS("mode incompatible with compression, "
  1240. "disabling\n");
  1241. dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
  1242. goto out_disable;
  1243. }
  1244. if ((crtc->mode.hdisplay > 2048) ||
  1245. (crtc->mode.vdisplay > 1536)) {
  1246. DRM_DEBUG_KMS("mode too large for compression, disabling\n");
  1247. dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
  1248. goto out_disable;
  1249. }
  1250. if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
  1251. DRM_DEBUG_KMS("plane not 0, disabling compression\n");
  1252. dev_priv->no_fbc_reason = FBC_BAD_PLANE;
  1253. goto out_disable;
  1254. }
  1255. if (obj_priv->tiling_mode != I915_TILING_X) {
  1256. DRM_DEBUG_KMS("framebuffer not tiled, disabling compression\n");
  1257. dev_priv->no_fbc_reason = FBC_NOT_TILED;
  1258. goto out_disable;
  1259. }
  1260. /* If the kernel debugger is active, always disable compression */
  1261. if (in_dbg_master())
  1262. goto out_disable;
  1263. intel_enable_fbc(crtc, 500);
  1264. return;
  1265. out_disable:
  1266. /* Multiple disables should be harmless */
  1267. if (intel_fbc_enabled(dev)) {
  1268. DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
  1269. intel_disable_fbc(dev);
  1270. }
  1271. }
  1272. int
  1273. intel_pin_and_fence_fb_obj(struct drm_device *dev, struct drm_gem_object *obj)
  1274. {
  1275. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1276. u32 alignment;
  1277. int ret;
  1278. switch (obj_priv->tiling_mode) {
  1279. case I915_TILING_NONE:
  1280. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1281. alignment = 128 * 1024;
  1282. else if (IS_I965G(dev))
  1283. alignment = 4 * 1024;
  1284. else
  1285. alignment = 64 * 1024;
  1286. break;
  1287. case I915_TILING_X:
  1288. /* pin() will align the object as required by fence */
  1289. alignment = 0;
  1290. break;
  1291. case I915_TILING_Y:
  1292. /* FIXME: Is this true? */
  1293. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  1294. return -EINVAL;
  1295. default:
  1296. BUG();
  1297. }
  1298. ret = i915_gem_object_pin(obj, alignment);
  1299. if (ret != 0)
  1300. return ret;
  1301. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1302. * fence, whereas 965+ only requires a fence if using
  1303. * framebuffer compression. For simplicity, we always install
  1304. * a fence as the cost is not that onerous.
  1305. */
  1306. if (obj_priv->fence_reg == I915_FENCE_REG_NONE &&
  1307. obj_priv->tiling_mode != I915_TILING_NONE) {
  1308. ret = i915_gem_object_get_fence_reg(obj);
  1309. if (ret != 0) {
  1310. i915_gem_object_unpin(obj);
  1311. return ret;
  1312. }
  1313. }
  1314. return 0;
  1315. }
  1316. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  1317. static int
  1318. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1319. int x, int y)
  1320. {
  1321. struct drm_device *dev = crtc->dev;
  1322. struct drm_i915_private *dev_priv = dev->dev_private;
  1323. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1324. struct intel_framebuffer *intel_fb;
  1325. struct drm_i915_gem_object *obj_priv;
  1326. struct drm_gem_object *obj;
  1327. int plane = intel_crtc->plane;
  1328. unsigned long Start, Offset;
  1329. int dspbase = (plane == 0 ? DSPAADDR : DSPBADDR);
  1330. int dspsurf = (plane == 0 ? DSPASURF : DSPBSURF);
  1331. int dspstride = (plane == 0) ? DSPASTRIDE : DSPBSTRIDE;
  1332. int dsptileoff = (plane == 0 ? DSPATILEOFF : DSPBTILEOFF);
  1333. int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
  1334. u32 dspcntr;
  1335. switch (plane) {
  1336. case 0:
  1337. case 1:
  1338. break;
  1339. default:
  1340. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1341. return -EINVAL;
  1342. }
  1343. intel_fb = to_intel_framebuffer(fb);
  1344. obj = intel_fb->obj;
  1345. obj_priv = to_intel_bo(obj);
  1346. dspcntr = I915_READ(dspcntr_reg);
  1347. /* Mask out pixel format bits in case we change it */
  1348. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1349. switch (fb->bits_per_pixel) {
  1350. case 8:
  1351. dspcntr |= DISPPLANE_8BPP;
  1352. break;
  1353. case 16:
  1354. if (fb->depth == 15)
  1355. dspcntr |= DISPPLANE_15_16BPP;
  1356. else
  1357. dspcntr |= DISPPLANE_16BPP;
  1358. break;
  1359. case 24:
  1360. case 32:
  1361. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1362. break;
  1363. default:
  1364. DRM_ERROR("Unknown color depth\n");
  1365. return -EINVAL;
  1366. }
  1367. if (IS_I965G(dev)) {
  1368. if (obj_priv->tiling_mode != I915_TILING_NONE)
  1369. dspcntr |= DISPPLANE_TILED;
  1370. else
  1371. dspcntr &= ~DISPPLANE_TILED;
  1372. }
  1373. if (HAS_PCH_SPLIT(dev))
  1374. /* must disable */
  1375. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1376. I915_WRITE(dspcntr_reg, dspcntr);
  1377. Start = obj_priv->gtt_offset;
  1378. Offset = y * fb->pitch + x * (fb->bits_per_pixel / 8);
  1379. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1380. Start, Offset, x, y, fb->pitch);
  1381. I915_WRITE(dspstride, fb->pitch);
  1382. if (IS_I965G(dev)) {
  1383. I915_WRITE(dspsurf, Start);
  1384. I915_WRITE(dsptileoff, (y << 16) | x);
  1385. I915_WRITE(dspbase, Offset);
  1386. } else {
  1387. I915_WRITE(dspbase, Start + Offset);
  1388. }
  1389. POSTING_READ(dspbase);
  1390. intel_update_fbc(dev);
  1391. intel_increase_pllclock(crtc);
  1392. return 0;
  1393. }
  1394. static int
  1395. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  1396. struct drm_framebuffer *old_fb)
  1397. {
  1398. struct drm_device *dev = crtc->dev;
  1399. struct drm_i915_master_private *master_priv;
  1400. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1401. struct intel_framebuffer *intel_fb;
  1402. struct drm_i915_gem_object *obj_priv;
  1403. struct drm_gem_object *obj;
  1404. int pipe = intel_crtc->pipe;
  1405. int plane = intel_crtc->plane;
  1406. int ret;
  1407. /* no fb bound */
  1408. if (!crtc->fb) {
  1409. DRM_DEBUG_KMS("No FB bound\n");
  1410. return 0;
  1411. }
  1412. switch (plane) {
  1413. case 0:
  1414. case 1:
  1415. break;
  1416. default:
  1417. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1418. return -EINVAL;
  1419. }
  1420. intel_fb = to_intel_framebuffer(crtc->fb);
  1421. obj = intel_fb->obj;
  1422. obj_priv = to_intel_bo(obj);
  1423. mutex_lock(&dev->struct_mutex);
  1424. ret = intel_pin_and_fence_fb_obj(dev, obj);
  1425. if (ret != 0) {
  1426. mutex_unlock(&dev->struct_mutex);
  1427. return ret;
  1428. }
  1429. ret = i915_gem_object_set_to_display_plane(obj);
  1430. if (ret != 0) {
  1431. i915_gem_object_unpin(obj);
  1432. mutex_unlock(&dev->struct_mutex);
  1433. return ret;
  1434. }
  1435. ret = intel_pipe_set_base_atomic(crtc, crtc->fb, x, y);
  1436. if (ret) {
  1437. i915_gem_object_unpin(obj);
  1438. mutex_unlock(&dev->struct_mutex);
  1439. return ret;
  1440. }
  1441. if (old_fb) {
  1442. intel_fb = to_intel_framebuffer(old_fb);
  1443. obj_priv = to_intel_bo(intel_fb->obj);
  1444. i915_gem_object_unpin(intel_fb->obj);
  1445. }
  1446. mutex_unlock(&dev->struct_mutex);
  1447. if (!dev->primary->master)
  1448. return 0;
  1449. master_priv = dev->primary->master->driver_priv;
  1450. if (!master_priv->sarea_priv)
  1451. return 0;
  1452. if (pipe) {
  1453. master_priv->sarea_priv->pipeB_x = x;
  1454. master_priv->sarea_priv->pipeB_y = y;
  1455. } else {
  1456. master_priv->sarea_priv->pipeA_x = x;
  1457. master_priv->sarea_priv->pipeA_y = y;
  1458. }
  1459. return 0;
  1460. }
  1461. static void ironlake_set_pll_edp (struct drm_crtc *crtc, int clock)
  1462. {
  1463. struct drm_device *dev = crtc->dev;
  1464. struct drm_i915_private *dev_priv = dev->dev_private;
  1465. u32 dpa_ctl;
  1466. DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
  1467. dpa_ctl = I915_READ(DP_A);
  1468. dpa_ctl &= ~DP_PLL_FREQ_MASK;
  1469. if (clock < 200000) {
  1470. u32 temp;
  1471. dpa_ctl |= DP_PLL_FREQ_160MHZ;
  1472. /* workaround for 160Mhz:
  1473. 1) program 0x4600c bits 15:0 = 0x8124
  1474. 2) program 0x46010 bit 0 = 1
  1475. 3) program 0x46034 bit 24 = 1
  1476. 4) program 0x64000 bit 14 = 1
  1477. */
  1478. temp = I915_READ(0x4600c);
  1479. temp &= 0xffff0000;
  1480. I915_WRITE(0x4600c, temp | 0x8124);
  1481. temp = I915_READ(0x46010);
  1482. I915_WRITE(0x46010, temp | 1);
  1483. temp = I915_READ(0x46034);
  1484. I915_WRITE(0x46034, temp | (1 << 24));
  1485. } else {
  1486. dpa_ctl |= DP_PLL_FREQ_270MHZ;
  1487. }
  1488. I915_WRITE(DP_A, dpa_ctl);
  1489. POSTING_READ(DP_A);
  1490. udelay(500);
  1491. }
  1492. /* The FDI link training functions for ILK/Ibexpeak. */
  1493. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  1494. {
  1495. struct drm_device *dev = crtc->dev;
  1496. struct drm_i915_private *dev_priv = dev->dev_private;
  1497. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1498. int pipe = intel_crtc->pipe;
  1499. int fdi_tx_reg = (pipe == 0) ? FDI_TXA_CTL : FDI_TXB_CTL;
  1500. int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
  1501. int fdi_rx_iir_reg = (pipe == 0) ? FDI_RXA_IIR : FDI_RXB_IIR;
  1502. int fdi_rx_imr_reg = (pipe == 0) ? FDI_RXA_IMR : FDI_RXB_IMR;
  1503. u32 temp, tries = 0;
  1504. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1505. for train result */
  1506. temp = I915_READ(fdi_rx_imr_reg);
  1507. temp &= ~FDI_RX_SYMBOL_LOCK;
  1508. temp &= ~FDI_RX_BIT_LOCK;
  1509. I915_WRITE(fdi_rx_imr_reg, temp);
  1510. I915_READ(fdi_rx_imr_reg);
  1511. udelay(150);
  1512. /* enable CPU FDI TX and PCH FDI RX */
  1513. temp = I915_READ(fdi_tx_reg);
  1514. temp |= FDI_TX_ENABLE;
  1515. temp &= ~(7 << 19);
  1516. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1517. temp &= ~FDI_LINK_TRAIN_NONE;
  1518. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1519. I915_WRITE(fdi_tx_reg, temp);
  1520. I915_READ(fdi_tx_reg);
  1521. temp = I915_READ(fdi_rx_reg);
  1522. temp &= ~FDI_LINK_TRAIN_NONE;
  1523. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1524. I915_WRITE(fdi_rx_reg, temp | FDI_RX_ENABLE);
  1525. I915_READ(fdi_rx_reg);
  1526. udelay(150);
  1527. for (tries = 0; tries < 5; tries++) {
  1528. temp = I915_READ(fdi_rx_iir_reg);
  1529. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1530. if ((temp & FDI_RX_BIT_LOCK)) {
  1531. DRM_DEBUG_KMS("FDI train 1 done.\n");
  1532. I915_WRITE(fdi_rx_iir_reg,
  1533. temp | FDI_RX_BIT_LOCK);
  1534. break;
  1535. }
  1536. }
  1537. if (tries == 5)
  1538. DRM_DEBUG_KMS("FDI train 1 fail!\n");
  1539. /* Train 2 */
  1540. temp = I915_READ(fdi_tx_reg);
  1541. temp &= ~FDI_LINK_TRAIN_NONE;
  1542. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1543. I915_WRITE(fdi_tx_reg, temp);
  1544. temp = I915_READ(fdi_rx_reg);
  1545. temp &= ~FDI_LINK_TRAIN_NONE;
  1546. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1547. I915_WRITE(fdi_rx_reg, temp);
  1548. POSTING_READ(fdi_rx_reg);
  1549. udelay(150);
  1550. tries = 0;
  1551. for (tries = 0; tries < 5; tries++) {
  1552. temp = I915_READ(fdi_rx_iir_reg);
  1553. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1554. if (temp & FDI_RX_SYMBOL_LOCK) {
  1555. I915_WRITE(fdi_rx_iir_reg,
  1556. temp | FDI_RX_SYMBOL_LOCK);
  1557. DRM_DEBUG_KMS("FDI train 2 done.\n");
  1558. break;
  1559. }
  1560. }
  1561. if (tries == 5)
  1562. DRM_DEBUG_KMS("FDI train 2 fail!\n");
  1563. DRM_DEBUG_KMS("FDI train done\n");
  1564. }
  1565. static int snb_b_fdi_train_param [] = {
  1566. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  1567. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  1568. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  1569. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  1570. };
  1571. /* The FDI link training functions for SNB/Cougarpoint. */
  1572. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  1573. {
  1574. struct drm_device *dev = crtc->dev;
  1575. struct drm_i915_private *dev_priv = dev->dev_private;
  1576. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1577. int pipe = intel_crtc->pipe;
  1578. int fdi_tx_reg = (pipe == 0) ? FDI_TXA_CTL : FDI_TXB_CTL;
  1579. int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
  1580. int fdi_rx_iir_reg = (pipe == 0) ? FDI_RXA_IIR : FDI_RXB_IIR;
  1581. int fdi_rx_imr_reg = (pipe == 0) ? FDI_RXA_IMR : FDI_RXB_IMR;
  1582. u32 temp, i;
  1583. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1584. for train result */
  1585. temp = I915_READ(fdi_rx_imr_reg);
  1586. temp &= ~FDI_RX_SYMBOL_LOCK;
  1587. temp &= ~FDI_RX_BIT_LOCK;
  1588. I915_WRITE(fdi_rx_imr_reg, temp);
  1589. I915_READ(fdi_rx_imr_reg);
  1590. udelay(150);
  1591. /* enable CPU FDI TX and PCH FDI RX */
  1592. temp = I915_READ(fdi_tx_reg);
  1593. temp |= FDI_TX_ENABLE;
  1594. temp &= ~(7 << 19);
  1595. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1596. temp &= ~FDI_LINK_TRAIN_NONE;
  1597. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1598. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1599. /* SNB-B */
  1600. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  1601. I915_WRITE(fdi_tx_reg, temp);
  1602. I915_READ(fdi_tx_reg);
  1603. temp = I915_READ(fdi_rx_reg);
  1604. if (HAS_PCH_CPT(dev)) {
  1605. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1606. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  1607. } else {
  1608. temp &= ~FDI_LINK_TRAIN_NONE;
  1609. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1610. }
  1611. I915_WRITE(fdi_rx_reg, temp | FDI_RX_ENABLE);
  1612. I915_READ(fdi_rx_reg);
  1613. udelay(150);
  1614. for (i = 0; i < 4; i++ ) {
  1615. temp = I915_READ(fdi_tx_reg);
  1616. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1617. temp |= snb_b_fdi_train_param[i];
  1618. I915_WRITE(fdi_tx_reg, temp);
  1619. POSTING_READ(fdi_tx_reg);
  1620. udelay(500);
  1621. temp = I915_READ(fdi_rx_iir_reg);
  1622. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1623. if (temp & FDI_RX_BIT_LOCK) {
  1624. I915_WRITE(fdi_rx_iir_reg,
  1625. temp | FDI_RX_BIT_LOCK);
  1626. DRM_DEBUG_KMS("FDI train 1 done.\n");
  1627. break;
  1628. }
  1629. }
  1630. if (i == 4)
  1631. DRM_DEBUG_KMS("FDI train 1 fail!\n");
  1632. /* Train 2 */
  1633. temp = I915_READ(fdi_tx_reg);
  1634. temp &= ~FDI_LINK_TRAIN_NONE;
  1635. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1636. if (IS_GEN6(dev)) {
  1637. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1638. /* SNB-B */
  1639. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  1640. }
  1641. I915_WRITE(fdi_tx_reg, temp);
  1642. temp = I915_READ(fdi_rx_reg);
  1643. if (HAS_PCH_CPT(dev)) {
  1644. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1645. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  1646. } else {
  1647. temp &= ~FDI_LINK_TRAIN_NONE;
  1648. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1649. }
  1650. I915_WRITE(fdi_rx_reg, temp);
  1651. POSTING_READ(fdi_rx_reg);
  1652. udelay(150);
  1653. for (i = 0; i < 4; i++ ) {
  1654. temp = I915_READ(fdi_tx_reg);
  1655. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1656. temp |= snb_b_fdi_train_param[i];
  1657. I915_WRITE(fdi_tx_reg, temp);
  1658. POSTING_READ(fdi_tx_reg);
  1659. udelay(500);
  1660. temp = I915_READ(fdi_rx_iir_reg);
  1661. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1662. if (temp & FDI_RX_SYMBOL_LOCK) {
  1663. I915_WRITE(fdi_rx_iir_reg,
  1664. temp | FDI_RX_SYMBOL_LOCK);
  1665. DRM_DEBUG_KMS("FDI train 2 done.\n");
  1666. break;
  1667. }
  1668. }
  1669. if (i == 4)
  1670. DRM_DEBUG_KMS("FDI train 2 fail!\n");
  1671. DRM_DEBUG_KMS("FDI train done.\n");
  1672. }
  1673. static void ironlake_fdi_enable(struct drm_crtc *crtc)
  1674. {
  1675. struct drm_device *dev = crtc->dev;
  1676. struct drm_i915_private *dev_priv = dev->dev_private;
  1677. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1678. int pipe = intel_crtc->pipe;
  1679. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  1680. int fdi_tx_reg = (pipe == 0) ? FDI_TXA_CTL : FDI_TXB_CTL;
  1681. int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
  1682. int data_m1_reg = (pipe == 0) ? PIPEA_DATA_M1 : PIPEB_DATA_M1;
  1683. u32 temp;
  1684. u32 pipe_bpc;
  1685. u32 tx_size;
  1686. temp = I915_READ(pipeconf_reg);
  1687. pipe_bpc = temp & PIPE_BPC_MASK;
  1688. /* Write the TU size bits so error detection works */
  1689. tx_size = I915_READ(data_m1_reg) & TU_SIZE_MASK;
  1690. I915_WRITE(FDI_RXA_TUSIZE1, tx_size);
  1691. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  1692. temp = I915_READ(fdi_rx_reg);
  1693. /*
  1694. * make the BPC in FDI Rx be consistent with that in
  1695. * pipeconf reg.
  1696. */
  1697. temp &= ~(0x7 << 16);
  1698. temp |= (pipe_bpc << 11);
  1699. temp &= ~(7 << 19);
  1700. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1701. I915_WRITE(fdi_rx_reg, temp | FDI_RX_PLL_ENABLE);
  1702. I915_READ(fdi_rx_reg);
  1703. udelay(200);
  1704. /* Switch from Rawclk to PCDclk */
  1705. temp = I915_READ(fdi_rx_reg);
  1706. I915_WRITE(fdi_rx_reg, temp | FDI_SEL_PCDCLK);
  1707. I915_READ(fdi_rx_reg);
  1708. udelay(200);
  1709. /* Enable CPU FDI TX PLL, always on for Ironlake */
  1710. temp = I915_READ(fdi_tx_reg);
  1711. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  1712. I915_WRITE(fdi_tx_reg, temp | FDI_TX_PLL_ENABLE);
  1713. I915_READ(fdi_tx_reg);
  1714. udelay(100);
  1715. }
  1716. }
  1717. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  1718. {
  1719. struct drm_device *dev = crtc->dev;
  1720. struct drm_i915_private *dev_priv = dev->dev_private;
  1721. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1722. int pipe = intel_crtc->pipe;
  1723. int plane = intel_crtc->plane;
  1724. int pch_dpll_reg = (pipe == 0) ? PCH_DPLL_A : PCH_DPLL_B;
  1725. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  1726. int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
  1727. int dspbase_reg = (plane == 0) ? DSPAADDR : DSPBADDR;
  1728. int fdi_tx_reg = (pipe == 0) ? FDI_TXA_CTL : FDI_TXB_CTL;
  1729. int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
  1730. int transconf_reg = (pipe == 0) ? TRANSACONF : TRANSBCONF;
  1731. int cpu_htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
  1732. int cpu_hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
  1733. int cpu_hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
  1734. int cpu_vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
  1735. int cpu_vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
  1736. int cpu_vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
  1737. int trans_htot_reg = (pipe == 0) ? TRANS_HTOTAL_A : TRANS_HTOTAL_B;
  1738. int trans_hblank_reg = (pipe == 0) ? TRANS_HBLANK_A : TRANS_HBLANK_B;
  1739. int trans_hsync_reg = (pipe == 0) ? TRANS_HSYNC_A : TRANS_HSYNC_B;
  1740. int trans_vtot_reg = (pipe == 0) ? TRANS_VTOTAL_A : TRANS_VTOTAL_B;
  1741. int trans_vblank_reg = (pipe == 0) ? TRANS_VBLANK_A : TRANS_VBLANK_B;
  1742. int trans_vsync_reg = (pipe == 0) ? TRANS_VSYNC_A : TRANS_VSYNC_B;
  1743. int trans_dpll_sel = (pipe == 0) ? 0 : 1;
  1744. u32 temp;
  1745. u32 pipe_bpc;
  1746. temp = I915_READ(pipeconf_reg);
  1747. pipe_bpc = temp & PIPE_BPC_MASK;
  1748. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  1749. temp = I915_READ(PCH_LVDS);
  1750. if ((temp & LVDS_PORT_EN) == 0) {
  1751. I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
  1752. POSTING_READ(PCH_LVDS);
  1753. }
  1754. }
  1755. ironlake_fdi_enable(crtc);
  1756. /* Enable panel fitting for LVDS */
  1757. if (dev_priv->pch_pf_size &&
  1758. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)
  1759. || HAS_eDP || intel_pch_has_edp(crtc))) {
  1760. /* Force use of hard-coded filter coefficients
  1761. * as some pre-programmed values are broken,
  1762. * e.g. x201.
  1763. */
  1764. I915_WRITE(pipe ? PFB_CTL_1 : PFA_CTL_1,
  1765. PF_ENABLE | PF_FILTER_MED_3x3);
  1766. I915_WRITE(pipe ? PFB_WIN_POS : PFA_WIN_POS,
  1767. dev_priv->pch_pf_pos);
  1768. I915_WRITE(pipe ? PFB_WIN_SZ : PFA_WIN_SZ,
  1769. dev_priv->pch_pf_size);
  1770. }
  1771. /* Enable CPU pipe */
  1772. temp = I915_READ(pipeconf_reg);
  1773. if ((temp & PIPEACONF_ENABLE) == 0) {
  1774. I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
  1775. I915_READ(pipeconf_reg);
  1776. udelay(100);
  1777. }
  1778. /* configure and enable CPU plane */
  1779. temp = I915_READ(dspcntr_reg);
  1780. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  1781. I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
  1782. /* Flush the plane changes */
  1783. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  1784. }
  1785. /* For PCH output, training FDI link */
  1786. if (IS_GEN6(dev))
  1787. gen6_fdi_link_train(crtc);
  1788. else
  1789. ironlake_fdi_link_train(crtc);
  1790. /* enable PCH DPLL */
  1791. temp = I915_READ(pch_dpll_reg);
  1792. if ((temp & DPLL_VCO_ENABLE) == 0) {
  1793. I915_WRITE(pch_dpll_reg, temp | DPLL_VCO_ENABLE);
  1794. I915_READ(pch_dpll_reg);
  1795. udelay(200);
  1796. }
  1797. if (HAS_PCH_CPT(dev)) {
  1798. /* Be sure PCH DPLL SEL is set */
  1799. temp = I915_READ(PCH_DPLL_SEL);
  1800. if (trans_dpll_sel == 0 &&
  1801. (temp & TRANSA_DPLL_ENABLE) == 0)
  1802. temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
  1803. else if (trans_dpll_sel == 1 &&
  1804. (temp & TRANSB_DPLL_ENABLE) == 0)
  1805. temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  1806. I915_WRITE(PCH_DPLL_SEL, temp);
  1807. I915_READ(PCH_DPLL_SEL);
  1808. }
  1809. /* set transcoder timing */
  1810. I915_WRITE(trans_htot_reg, I915_READ(cpu_htot_reg));
  1811. I915_WRITE(trans_hblank_reg, I915_READ(cpu_hblank_reg));
  1812. I915_WRITE(trans_hsync_reg, I915_READ(cpu_hsync_reg));
  1813. I915_WRITE(trans_vtot_reg, I915_READ(cpu_vtot_reg));
  1814. I915_WRITE(trans_vblank_reg, I915_READ(cpu_vblank_reg));
  1815. I915_WRITE(trans_vsync_reg, I915_READ(cpu_vsync_reg));
  1816. /* enable normal train */
  1817. temp = I915_READ(fdi_tx_reg);
  1818. temp &= ~FDI_LINK_TRAIN_NONE;
  1819. I915_WRITE(fdi_tx_reg, temp | FDI_LINK_TRAIN_NONE |
  1820. FDI_TX_ENHANCE_FRAME_ENABLE);
  1821. I915_READ(fdi_tx_reg);
  1822. temp = I915_READ(fdi_rx_reg);
  1823. if (HAS_PCH_CPT(dev)) {
  1824. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1825. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  1826. } else {
  1827. temp &= ~FDI_LINK_TRAIN_NONE;
  1828. temp |= FDI_LINK_TRAIN_NONE;
  1829. }
  1830. I915_WRITE(fdi_rx_reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  1831. I915_READ(fdi_rx_reg);
  1832. /* wait one idle pattern time */
  1833. udelay(100);
  1834. /* For PCH DP, enable TRANS_DP_CTL */
  1835. if (HAS_PCH_CPT(dev) &&
  1836. intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  1837. int trans_dp_ctl = (pipe == 0) ? TRANS_DP_CTL_A : TRANS_DP_CTL_B;
  1838. int reg;
  1839. reg = I915_READ(trans_dp_ctl);
  1840. reg &= ~(TRANS_DP_PORT_SEL_MASK |
  1841. TRANS_DP_SYNC_MASK);
  1842. reg |= (TRANS_DP_OUTPUT_ENABLE |
  1843. TRANS_DP_ENH_FRAMING);
  1844. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  1845. reg |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  1846. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  1847. reg |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  1848. switch (intel_trans_dp_port_sel(crtc)) {
  1849. case PCH_DP_B:
  1850. reg |= TRANS_DP_PORT_SEL_B;
  1851. break;
  1852. case PCH_DP_C:
  1853. reg |= TRANS_DP_PORT_SEL_C;
  1854. break;
  1855. case PCH_DP_D:
  1856. reg |= TRANS_DP_PORT_SEL_D;
  1857. break;
  1858. default:
  1859. DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
  1860. reg |= TRANS_DP_PORT_SEL_B;
  1861. break;
  1862. }
  1863. I915_WRITE(trans_dp_ctl, reg);
  1864. POSTING_READ(trans_dp_ctl);
  1865. }
  1866. /* enable PCH transcoder */
  1867. temp = I915_READ(transconf_reg);
  1868. /*
  1869. * make the BPC in transcoder be consistent with
  1870. * that in pipeconf reg.
  1871. */
  1872. temp &= ~PIPE_BPC_MASK;
  1873. temp |= pipe_bpc;
  1874. I915_WRITE(transconf_reg, temp | TRANS_ENABLE);
  1875. I915_READ(transconf_reg);
  1876. if (wait_for(I915_READ(transconf_reg) & TRANS_STATE_ENABLE, 100))
  1877. DRM_ERROR("failed to enable transcoder\n");
  1878. intel_crtc_load_lut(crtc);
  1879. intel_update_fbc(dev);
  1880. }
  1881. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  1882. {
  1883. struct drm_device *dev = crtc->dev;
  1884. struct drm_i915_private *dev_priv = dev->dev_private;
  1885. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1886. int pipe = intel_crtc->pipe;
  1887. int plane = intel_crtc->plane;
  1888. int pch_dpll_reg = (pipe == 0) ? PCH_DPLL_A : PCH_DPLL_B;
  1889. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  1890. int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
  1891. int dspbase_reg = (plane == 0) ? DSPAADDR : DSPBADDR;
  1892. int fdi_tx_reg = (pipe == 0) ? FDI_TXA_CTL : FDI_TXB_CTL;
  1893. int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
  1894. int transconf_reg = (pipe == 0) ? TRANSACONF : TRANSBCONF;
  1895. int trans_dpll_sel = (pipe == 0) ? 0 : 1;
  1896. u32 temp;
  1897. u32 pipe_bpc;
  1898. temp = I915_READ(pipeconf_reg);
  1899. pipe_bpc = temp & PIPE_BPC_MASK;
  1900. drm_vblank_off(dev, pipe);
  1901. /* Disable display plane */
  1902. temp = I915_READ(dspcntr_reg);
  1903. if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
  1904. I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
  1905. /* Flush the plane changes */
  1906. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  1907. I915_READ(dspbase_reg);
  1908. }
  1909. if (dev_priv->cfb_plane == plane &&
  1910. dev_priv->display.disable_fbc)
  1911. dev_priv->display.disable_fbc(dev);
  1912. /* disable cpu pipe, disable after all planes disabled */
  1913. temp = I915_READ(pipeconf_reg);
  1914. if ((temp & PIPEACONF_ENABLE) != 0) {
  1915. I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
  1916. /* wait for cpu pipe off, pipe state */
  1917. if (wait_for((I915_READ(pipeconf_reg) & I965_PIPECONF_ACTIVE) == 0, 50))
  1918. DRM_ERROR("failed to turn off cpu pipe\n");
  1919. } else
  1920. DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
  1921. /* Disable PF */
  1922. I915_WRITE(pipe ? PFB_CTL_1 : PFA_CTL_1, 0);
  1923. I915_WRITE(pipe ? PFB_WIN_SZ : PFA_WIN_SZ, 0);
  1924. /* disable CPU FDI tx and PCH FDI rx */
  1925. temp = I915_READ(fdi_tx_reg);
  1926. I915_WRITE(fdi_tx_reg, temp & ~FDI_TX_ENABLE);
  1927. I915_READ(fdi_tx_reg);
  1928. temp = I915_READ(fdi_rx_reg);
  1929. /* BPC in FDI rx is consistent with that in pipeconf */
  1930. temp &= ~(0x07 << 16);
  1931. temp |= (pipe_bpc << 11);
  1932. I915_WRITE(fdi_rx_reg, temp & ~FDI_RX_ENABLE);
  1933. I915_READ(fdi_rx_reg);
  1934. udelay(100);
  1935. /* still set train pattern 1 */
  1936. temp = I915_READ(fdi_tx_reg);
  1937. temp &= ~FDI_LINK_TRAIN_NONE;
  1938. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1939. I915_WRITE(fdi_tx_reg, temp);
  1940. POSTING_READ(fdi_tx_reg);
  1941. temp = I915_READ(fdi_rx_reg);
  1942. if (HAS_PCH_CPT(dev)) {
  1943. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1944. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  1945. } else {
  1946. temp &= ~FDI_LINK_TRAIN_NONE;
  1947. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1948. }
  1949. I915_WRITE(fdi_rx_reg, temp);
  1950. POSTING_READ(fdi_rx_reg);
  1951. udelay(100);
  1952. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  1953. temp = I915_READ(PCH_LVDS);
  1954. I915_WRITE(PCH_LVDS, temp & ~LVDS_PORT_EN);
  1955. I915_READ(PCH_LVDS);
  1956. udelay(100);
  1957. }
  1958. /* disable PCH transcoder */
  1959. temp = I915_READ(transconf_reg);
  1960. if ((temp & TRANS_ENABLE) != 0) {
  1961. I915_WRITE(transconf_reg, temp & ~TRANS_ENABLE);
  1962. /* wait for PCH transcoder off, transcoder state */
  1963. if (wait_for((I915_READ(transconf_reg) & TRANS_STATE_ENABLE) == 0, 50))
  1964. DRM_ERROR("failed to disable transcoder\n");
  1965. }
  1966. temp = I915_READ(transconf_reg);
  1967. /* BPC in transcoder is consistent with that in pipeconf */
  1968. temp &= ~PIPE_BPC_MASK;
  1969. temp |= pipe_bpc;
  1970. I915_WRITE(transconf_reg, temp);
  1971. I915_READ(transconf_reg);
  1972. udelay(100);
  1973. if (HAS_PCH_CPT(dev)) {
  1974. /* disable TRANS_DP_CTL */
  1975. int trans_dp_ctl = (pipe == 0) ? TRANS_DP_CTL_A : TRANS_DP_CTL_B;
  1976. int reg;
  1977. reg = I915_READ(trans_dp_ctl);
  1978. reg &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
  1979. I915_WRITE(trans_dp_ctl, reg);
  1980. POSTING_READ(trans_dp_ctl);
  1981. /* disable DPLL_SEL */
  1982. temp = I915_READ(PCH_DPLL_SEL);
  1983. if (trans_dpll_sel == 0)
  1984. temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
  1985. else
  1986. temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  1987. I915_WRITE(PCH_DPLL_SEL, temp);
  1988. I915_READ(PCH_DPLL_SEL);
  1989. }
  1990. /* disable PCH DPLL */
  1991. temp = I915_READ(pch_dpll_reg);
  1992. I915_WRITE(pch_dpll_reg, temp & ~DPLL_VCO_ENABLE);
  1993. I915_READ(pch_dpll_reg);
  1994. /* Switch from PCDclk to Rawclk */
  1995. temp = I915_READ(fdi_rx_reg);
  1996. temp &= ~FDI_SEL_PCDCLK;
  1997. I915_WRITE(fdi_rx_reg, temp);
  1998. I915_READ(fdi_rx_reg);
  1999. /* Disable CPU FDI TX PLL */
  2000. temp = I915_READ(fdi_tx_reg);
  2001. I915_WRITE(fdi_tx_reg, temp & ~FDI_TX_PLL_ENABLE);
  2002. I915_READ(fdi_tx_reg);
  2003. udelay(100);
  2004. temp = I915_READ(fdi_rx_reg);
  2005. temp &= ~FDI_RX_PLL_ENABLE;
  2006. I915_WRITE(fdi_rx_reg, temp);
  2007. I915_READ(fdi_rx_reg);
  2008. /* Wait for the clocks to turn off. */
  2009. udelay(100);
  2010. }
  2011. static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
  2012. {
  2013. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2014. int pipe = intel_crtc->pipe;
  2015. int plane = intel_crtc->plane;
  2016. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2017. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2018. */
  2019. switch (mode) {
  2020. case DRM_MODE_DPMS_ON:
  2021. case DRM_MODE_DPMS_STANDBY:
  2022. case DRM_MODE_DPMS_SUSPEND:
  2023. DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
  2024. ironlake_crtc_enable(crtc);
  2025. break;
  2026. case DRM_MODE_DPMS_OFF:
  2027. DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
  2028. ironlake_crtc_disable(crtc);
  2029. break;
  2030. }
  2031. }
  2032. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  2033. {
  2034. if (!enable && intel_crtc->overlay) {
  2035. struct drm_device *dev = intel_crtc->base.dev;
  2036. mutex_lock(&dev->struct_mutex);
  2037. (void) intel_overlay_switch_off(intel_crtc->overlay, false);
  2038. mutex_unlock(&dev->struct_mutex);
  2039. }
  2040. /* Let userspace switch the overlay on again. In most cases userspace
  2041. * has to recompute where to put it anyway.
  2042. */
  2043. }
  2044. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  2045. {
  2046. struct drm_device *dev = crtc->dev;
  2047. struct drm_i915_private *dev_priv = dev->dev_private;
  2048. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2049. int pipe = intel_crtc->pipe;
  2050. int plane = intel_crtc->plane;
  2051. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  2052. int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
  2053. int dspbase_reg = (plane == 0) ? DSPAADDR : DSPBADDR;
  2054. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  2055. u32 temp;
  2056. /* Enable the DPLL */
  2057. temp = I915_READ(dpll_reg);
  2058. if ((temp & DPLL_VCO_ENABLE) == 0) {
  2059. I915_WRITE(dpll_reg, temp);
  2060. I915_READ(dpll_reg);
  2061. /* Wait for the clocks to stabilize. */
  2062. udelay(150);
  2063. I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
  2064. I915_READ(dpll_reg);
  2065. /* Wait for the clocks to stabilize. */
  2066. udelay(150);
  2067. I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
  2068. I915_READ(dpll_reg);
  2069. /* Wait for the clocks to stabilize. */
  2070. udelay(150);
  2071. }
  2072. /* Enable the pipe */
  2073. temp = I915_READ(pipeconf_reg);
  2074. if ((temp & PIPEACONF_ENABLE) == 0)
  2075. I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
  2076. /* Enable the plane */
  2077. temp = I915_READ(dspcntr_reg);
  2078. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  2079. I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
  2080. /* Flush the plane changes */
  2081. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  2082. }
  2083. intel_crtc_load_lut(crtc);
  2084. intel_update_fbc(dev);
  2085. /* Give the overlay scaler a chance to enable if it's on this pipe */
  2086. intel_crtc_dpms_overlay(intel_crtc, true);
  2087. }
  2088. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  2089. {
  2090. struct drm_device *dev = crtc->dev;
  2091. struct drm_i915_private *dev_priv = dev->dev_private;
  2092. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2093. int pipe = intel_crtc->pipe;
  2094. int plane = intel_crtc->plane;
  2095. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  2096. int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
  2097. int dspbase_reg = (plane == 0) ? DSPAADDR : DSPBADDR;
  2098. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  2099. u32 temp;
  2100. /* Give the overlay scaler a chance to disable if it's on this pipe */
  2101. intel_crtc_dpms_overlay(intel_crtc, false);
  2102. drm_vblank_off(dev, pipe);
  2103. if (dev_priv->cfb_plane == plane &&
  2104. dev_priv->display.disable_fbc)
  2105. dev_priv->display.disable_fbc(dev);
  2106. /* Disable display plane */
  2107. temp = I915_READ(dspcntr_reg);
  2108. if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
  2109. I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
  2110. /* Flush the plane changes */
  2111. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  2112. I915_READ(dspbase_reg);
  2113. }
  2114. if (!IS_I9XX(dev)) {
  2115. /* Wait for vblank for the disable to take effect */
  2116. intel_wait_for_vblank_off(dev, pipe);
  2117. }
  2118. /* Don't disable pipe A or pipe A PLLs if needed */
  2119. if (pipeconf_reg == PIPEACONF &&
  2120. (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  2121. goto skip_pipe_off;
  2122. /* Next, disable display pipes */
  2123. temp = I915_READ(pipeconf_reg);
  2124. if ((temp & PIPEACONF_ENABLE) != 0) {
  2125. I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
  2126. I915_READ(pipeconf_reg);
  2127. }
  2128. /* Wait for vblank for the disable to take effect. */
  2129. intel_wait_for_vblank_off(dev, pipe);
  2130. temp = I915_READ(dpll_reg);
  2131. if ((temp & DPLL_VCO_ENABLE) != 0) {
  2132. I915_WRITE(dpll_reg, temp & ~DPLL_VCO_ENABLE);
  2133. I915_READ(dpll_reg);
  2134. }
  2135. skip_pipe_off:
  2136. /* Wait for the clocks to turn off. */
  2137. udelay(150);
  2138. }
  2139. static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
  2140. {
  2141. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2142. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2143. */
  2144. switch (mode) {
  2145. case DRM_MODE_DPMS_ON:
  2146. case DRM_MODE_DPMS_STANDBY:
  2147. case DRM_MODE_DPMS_SUSPEND:
  2148. i9xx_crtc_enable(crtc);
  2149. break;
  2150. case DRM_MODE_DPMS_OFF:
  2151. i9xx_crtc_disable(crtc);
  2152. break;
  2153. }
  2154. }
  2155. /*
  2156. * When we disable a pipe, we need to clear any pending scanline wait events
  2157. * to avoid hanging the ring, which we assume we are waiting on.
  2158. */
  2159. static void intel_clear_scanline_wait(struct drm_device *dev)
  2160. {
  2161. struct drm_i915_private *dev_priv = dev->dev_private;
  2162. u32 tmp;
  2163. if (IS_GEN2(dev))
  2164. /* Can't break the hang on i8xx */
  2165. return;
  2166. tmp = I915_READ(PRB0_CTL);
  2167. if (tmp & RING_WAIT) {
  2168. I915_WRITE(PRB0_CTL, tmp);
  2169. POSTING_READ(PRB0_CTL);
  2170. }
  2171. }
  2172. /**
  2173. * Sets the power management mode of the pipe and plane.
  2174. */
  2175. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  2176. {
  2177. struct drm_device *dev = crtc->dev;
  2178. struct drm_i915_private *dev_priv = dev->dev_private;
  2179. struct drm_i915_master_private *master_priv;
  2180. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2181. int pipe = intel_crtc->pipe;
  2182. bool enabled;
  2183. if (intel_crtc->dpms_mode == mode)
  2184. return;
  2185. intel_crtc->dpms_mode = mode;
  2186. intel_crtc->cursor_on = mode == DRM_MODE_DPMS_ON;
  2187. /* When switching on the display, ensure that SR is disabled
  2188. * with multiple pipes prior to enabling to new pipe.
  2189. *
  2190. * When switching off the display, make sure the cursor is
  2191. * properly hidden and there are no pending waits prior to
  2192. * disabling the pipe.
  2193. */
  2194. if (mode == DRM_MODE_DPMS_ON)
  2195. intel_update_watermarks(dev);
  2196. else
  2197. intel_crtc_update_cursor(crtc);
  2198. dev_priv->display.dpms(crtc, mode);
  2199. if (mode == DRM_MODE_DPMS_ON) {
  2200. intel_crtc_update_cursor(crtc);
  2201. } else {
  2202. /* XXX Note that this is not a complete solution, but a hack
  2203. * to avoid the most frequently hit hang.
  2204. */
  2205. intel_clear_scanline_wait(dev);
  2206. intel_update_watermarks(dev);
  2207. }
  2208. intel_update_fbc(dev);
  2209. if (!dev->primary->master)
  2210. return;
  2211. master_priv = dev->primary->master->driver_priv;
  2212. if (!master_priv->sarea_priv)
  2213. return;
  2214. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  2215. switch (pipe) {
  2216. case 0:
  2217. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  2218. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  2219. break;
  2220. case 1:
  2221. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  2222. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  2223. break;
  2224. default:
  2225. DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
  2226. break;
  2227. }
  2228. }
  2229. /* Prepare for a mode set.
  2230. *
  2231. * Note we could be a lot smarter here. We need to figure out which outputs
  2232. * will be enabled, which disabled (in short, how the config will changes)
  2233. * and perform the minimum necessary steps to accomplish that, e.g. updating
  2234. * watermarks, FBC configuration, making sure PLLs are programmed correctly,
  2235. * panel fitting is in the proper state, etc.
  2236. */
  2237. static void i9xx_crtc_prepare(struct drm_crtc *crtc)
  2238. {
  2239. struct drm_device *dev = crtc->dev;
  2240. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2241. intel_crtc->cursor_on = false;
  2242. intel_crtc_update_cursor(crtc);
  2243. i9xx_crtc_disable(crtc);
  2244. intel_clear_scanline_wait(dev);
  2245. }
  2246. static void i9xx_crtc_commit(struct drm_crtc *crtc)
  2247. {
  2248. struct drm_device *dev = crtc->dev;
  2249. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2250. intel_update_watermarks(dev);
  2251. i9xx_crtc_enable(crtc);
  2252. intel_crtc->cursor_on = true;
  2253. intel_crtc_update_cursor(crtc);
  2254. }
  2255. static void ironlake_crtc_prepare(struct drm_crtc *crtc)
  2256. {
  2257. struct drm_device *dev = crtc->dev;
  2258. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2259. intel_crtc->cursor_on = false;
  2260. intel_crtc_update_cursor(crtc);
  2261. ironlake_crtc_disable(crtc);
  2262. intel_clear_scanline_wait(dev);
  2263. }
  2264. static void ironlake_crtc_commit(struct drm_crtc *crtc)
  2265. {
  2266. struct drm_device *dev = crtc->dev;
  2267. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2268. intel_update_watermarks(dev);
  2269. ironlake_crtc_enable(crtc);
  2270. intel_crtc->cursor_on = true;
  2271. intel_crtc_update_cursor(crtc);
  2272. }
  2273. void intel_encoder_prepare (struct drm_encoder *encoder)
  2274. {
  2275. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2276. /* lvds has its own version of prepare see intel_lvds_prepare */
  2277. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  2278. }
  2279. void intel_encoder_commit (struct drm_encoder *encoder)
  2280. {
  2281. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2282. /* lvds has its own version of commit see intel_lvds_commit */
  2283. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  2284. }
  2285. void intel_encoder_destroy(struct drm_encoder *encoder)
  2286. {
  2287. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  2288. if (intel_encoder->ddc_bus)
  2289. intel_i2c_destroy(intel_encoder->ddc_bus);
  2290. if (intel_encoder->i2c_bus)
  2291. intel_i2c_destroy(intel_encoder->i2c_bus);
  2292. drm_encoder_cleanup(encoder);
  2293. kfree(intel_encoder);
  2294. }
  2295. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  2296. struct drm_display_mode *mode,
  2297. struct drm_display_mode *adjusted_mode)
  2298. {
  2299. struct drm_device *dev = crtc->dev;
  2300. if (HAS_PCH_SPLIT(dev)) {
  2301. /* FDI link clock is fixed at 2.7G */
  2302. if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
  2303. return false;
  2304. }
  2305. return true;
  2306. }
  2307. static int i945_get_display_clock_speed(struct drm_device *dev)
  2308. {
  2309. return 400000;
  2310. }
  2311. static int i915_get_display_clock_speed(struct drm_device *dev)
  2312. {
  2313. return 333000;
  2314. }
  2315. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  2316. {
  2317. return 200000;
  2318. }
  2319. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  2320. {
  2321. u16 gcfgc = 0;
  2322. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  2323. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  2324. return 133000;
  2325. else {
  2326. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  2327. case GC_DISPLAY_CLOCK_333_MHZ:
  2328. return 333000;
  2329. default:
  2330. case GC_DISPLAY_CLOCK_190_200_MHZ:
  2331. return 190000;
  2332. }
  2333. }
  2334. }
  2335. static int i865_get_display_clock_speed(struct drm_device *dev)
  2336. {
  2337. return 266000;
  2338. }
  2339. static int i855_get_display_clock_speed(struct drm_device *dev)
  2340. {
  2341. u16 hpllcc = 0;
  2342. /* Assume that the hardware is in the high speed state. This
  2343. * should be the default.
  2344. */
  2345. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  2346. case GC_CLOCK_133_200:
  2347. case GC_CLOCK_100_200:
  2348. return 200000;
  2349. case GC_CLOCK_166_250:
  2350. return 250000;
  2351. case GC_CLOCK_100_133:
  2352. return 133000;
  2353. }
  2354. /* Shouldn't happen */
  2355. return 0;
  2356. }
  2357. static int i830_get_display_clock_speed(struct drm_device *dev)
  2358. {
  2359. return 133000;
  2360. }
  2361. /**
  2362. * Return the pipe currently connected to the panel fitter,
  2363. * or -1 if the panel fitter is not present or not in use
  2364. */
  2365. int intel_panel_fitter_pipe (struct drm_device *dev)
  2366. {
  2367. struct drm_i915_private *dev_priv = dev->dev_private;
  2368. u32 pfit_control;
  2369. /* i830 doesn't have a panel fitter */
  2370. if (IS_I830(dev))
  2371. return -1;
  2372. pfit_control = I915_READ(PFIT_CONTROL);
  2373. /* See if the panel fitter is in use */
  2374. if ((pfit_control & PFIT_ENABLE) == 0)
  2375. return -1;
  2376. /* 965 can place panel fitter on either pipe */
  2377. if (IS_I965G(dev))
  2378. return (pfit_control >> 29) & 0x3;
  2379. /* older chips can only use pipe 1 */
  2380. return 1;
  2381. }
  2382. struct fdi_m_n {
  2383. u32 tu;
  2384. u32 gmch_m;
  2385. u32 gmch_n;
  2386. u32 link_m;
  2387. u32 link_n;
  2388. };
  2389. static void
  2390. fdi_reduce_ratio(u32 *num, u32 *den)
  2391. {
  2392. while (*num > 0xffffff || *den > 0xffffff) {
  2393. *num >>= 1;
  2394. *den >>= 1;
  2395. }
  2396. }
  2397. #define DATA_N 0x800000
  2398. #define LINK_N 0x80000
  2399. static void
  2400. ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
  2401. int link_clock, struct fdi_m_n *m_n)
  2402. {
  2403. u64 temp;
  2404. m_n->tu = 64; /* default size */
  2405. temp = (u64) DATA_N * pixel_clock;
  2406. temp = div_u64(temp, link_clock);
  2407. m_n->gmch_m = div_u64(temp * bits_per_pixel, nlanes);
  2408. m_n->gmch_m >>= 3; /* convert to bytes_per_pixel */
  2409. m_n->gmch_n = DATA_N;
  2410. fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  2411. temp = (u64) LINK_N * pixel_clock;
  2412. m_n->link_m = div_u64(temp, link_clock);
  2413. m_n->link_n = LINK_N;
  2414. fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
  2415. }
  2416. struct intel_watermark_params {
  2417. unsigned long fifo_size;
  2418. unsigned long max_wm;
  2419. unsigned long default_wm;
  2420. unsigned long guard_size;
  2421. unsigned long cacheline_size;
  2422. };
  2423. /* Pineview has different values for various configs */
  2424. static struct intel_watermark_params pineview_display_wm = {
  2425. PINEVIEW_DISPLAY_FIFO,
  2426. PINEVIEW_MAX_WM,
  2427. PINEVIEW_DFT_WM,
  2428. PINEVIEW_GUARD_WM,
  2429. PINEVIEW_FIFO_LINE_SIZE
  2430. };
  2431. static struct intel_watermark_params pineview_display_hplloff_wm = {
  2432. PINEVIEW_DISPLAY_FIFO,
  2433. PINEVIEW_MAX_WM,
  2434. PINEVIEW_DFT_HPLLOFF_WM,
  2435. PINEVIEW_GUARD_WM,
  2436. PINEVIEW_FIFO_LINE_SIZE
  2437. };
  2438. static struct intel_watermark_params pineview_cursor_wm = {
  2439. PINEVIEW_CURSOR_FIFO,
  2440. PINEVIEW_CURSOR_MAX_WM,
  2441. PINEVIEW_CURSOR_DFT_WM,
  2442. PINEVIEW_CURSOR_GUARD_WM,
  2443. PINEVIEW_FIFO_LINE_SIZE,
  2444. };
  2445. static struct intel_watermark_params pineview_cursor_hplloff_wm = {
  2446. PINEVIEW_CURSOR_FIFO,
  2447. PINEVIEW_CURSOR_MAX_WM,
  2448. PINEVIEW_CURSOR_DFT_WM,
  2449. PINEVIEW_CURSOR_GUARD_WM,
  2450. PINEVIEW_FIFO_LINE_SIZE
  2451. };
  2452. static struct intel_watermark_params g4x_wm_info = {
  2453. G4X_FIFO_SIZE,
  2454. G4X_MAX_WM,
  2455. G4X_MAX_WM,
  2456. 2,
  2457. G4X_FIFO_LINE_SIZE,
  2458. };
  2459. static struct intel_watermark_params g4x_cursor_wm_info = {
  2460. I965_CURSOR_FIFO,
  2461. I965_CURSOR_MAX_WM,
  2462. I965_CURSOR_DFT_WM,
  2463. 2,
  2464. G4X_FIFO_LINE_SIZE,
  2465. };
  2466. static struct intel_watermark_params i965_cursor_wm_info = {
  2467. I965_CURSOR_FIFO,
  2468. I965_CURSOR_MAX_WM,
  2469. I965_CURSOR_DFT_WM,
  2470. 2,
  2471. I915_FIFO_LINE_SIZE,
  2472. };
  2473. static struct intel_watermark_params i945_wm_info = {
  2474. I945_FIFO_SIZE,
  2475. I915_MAX_WM,
  2476. 1,
  2477. 2,
  2478. I915_FIFO_LINE_SIZE
  2479. };
  2480. static struct intel_watermark_params i915_wm_info = {
  2481. I915_FIFO_SIZE,
  2482. I915_MAX_WM,
  2483. 1,
  2484. 2,
  2485. I915_FIFO_LINE_SIZE
  2486. };
  2487. static struct intel_watermark_params i855_wm_info = {
  2488. I855GM_FIFO_SIZE,
  2489. I915_MAX_WM,
  2490. 1,
  2491. 2,
  2492. I830_FIFO_LINE_SIZE
  2493. };
  2494. static struct intel_watermark_params i830_wm_info = {
  2495. I830_FIFO_SIZE,
  2496. I915_MAX_WM,
  2497. 1,
  2498. 2,
  2499. I830_FIFO_LINE_SIZE
  2500. };
  2501. static struct intel_watermark_params ironlake_display_wm_info = {
  2502. ILK_DISPLAY_FIFO,
  2503. ILK_DISPLAY_MAXWM,
  2504. ILK_DISPLAY_DFTWM,
  2505. 2,
  2506. ILK_FIFO_LINE_SIZE
  2507. };
  2508. static struct intel_watermark_params ironlake_cursor_wm_info = {
  2509. ILK_CURSOR_FIFO,
  2510. ILK_CURSOR_MAXWM,
  2511. ILK_CURSOR_DFTWM,
  2512. 2,
  2513. ILK_FIFO_LINE_SIZE
  2514. };
  2515. static struct intel_watermark_params ironlake_display_srwm_info = {
  2516. ILK_DISPLAY_SR_FIFO,
  2517. ILK_DISPLAY_MAX_SRWM,
  2518. ILK_DISPLAY_DFT_SRWM,
  2519. 2,
  2520. ILK_FIFO_LINE_SIZE
  2521. };
  2522. static struct intel_watermark_params ironlake_cursor_srwm_info = {
  2523. ILK_CURSOR_SR_FIFO,
  2524. ILK_CURSOR_MAX_SRWM,
  2525. ILK_CURSOR_DFT_SRWM,
  2526. 2,
  2527. ILK_FIFO_LINE_SIZE
  2528. };
  2529. /**
  2530. * intel_calculate_wm - calculate watermark level
  2531. * @clock_in_khz: pixel clock
  2532. * @wm: chip FIFO params
  2533. * @pixel_size: display pixel size
  2534. * @latency_ns: memory latency for the platform
  2535. *
  2536. * Calculate the watermark level (the level at which the display plane will
  2537. * start fetching from memory again). Each chip has a different display
  2538. * FIFO size and allocation, so the caller needs to figure that out and pass
  2539. * in the correct intel_watermark_params structure.
  2540. *
  2541. * As the pixel clock runs, the FIFO will be drained at a rate that depends
  2542. * on the pixel size. When it reaches the watermark level, it'll start
  2543. * fetching FIFO line sized based chunks from memory until the FIFO fills
  2544. * past the watermark point. If the FIFO drains completely, a FIFO underrun
  2545. * will occur, and a display engine hang could result.
  2546. */
  2547. static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
  2548. struct intel_watermark_params *wm,
  2549. int pixel_size,
  2550. unsigned long latency_ns)
  2551. {
  2552. long entries_required, wm_size;
  2553. /*
  2554. * Note: we need to make sure we don't overflow for various clock &
  2555. * latency values.
  2556. * clocks go from a few thousand to several hundred thousand.
  2557. * latency is usually a few thousand
  2558. */
  2559. entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
  2560. 1000;
  2561. entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
  2562. DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries_required);
  2563. wm_size = wm->fifo_size - (entries_required + wm->guard_size);
  2564. DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
  2565. /* Don't promote wm_size to unsigned... */
  2566. if (wm_size > (long)wm->max_wm)
  2567. wm_size = wm->max_wm;
  2568. if (wm_size <= 0)
  2569. wm_size = wm->default_wm;
  2570. return wm_size;
  2571. }
  2572. struct cxsr_latency {
  2573. int is_desktop;
  2574. int is_ddr3;
  2575. unsigned long fsb_freq;
  2576. unsigned long mem_freq;
  2577. unsigned long display_sr;
  2578. unsigned long display_hpll_disable;
  2579. unsigned long cursor_sr;
  2580. unsigned long cursor_hpll_disable;
  2581. };
  2582. static const struct cxsr_latency cxsr_latency_table[] = {
  2583. {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  2584. {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  2585. {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  2586. {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
  2587. {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
  2588. {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  2589. {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  2590. {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  2591. {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
  2592. {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
  2593. {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  2594. {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  2595. {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  2596. {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
  2597. {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
  2598. {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  2599. {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  2600. {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  2601. {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
  2602. {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
  2603. {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  2604. {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  2605. {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  2606. {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
  2607. {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
  2608. {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  2609. {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  2610. {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  2611. {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
  2612. {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
  2613. };
  2614. static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
  2615. int is_ddr3,
  2616. int fsb,
  2617. int mem)
  2618. {
  2619. const struct cxsr_latency *latency;
  2620. int i;
  2621. if (fsb == 0 || mem == 0)
  2622. return NULL;
  2623. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  2624. latency = &cxsr_latency_table[i];
  2625. if (is_desktop == latency->is_desktop &&
  2626. is_ddr3 == latency->is_ddr3 &&
  2627. fsb == latency->fsb_freq && mem == latency->mem_freq)
  2628. return latency;
  2629. }
  2630. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  2631. return NULL;
  2632. }
  2633. static void pineview_disable_cxsr(struct drm_device *dev)
  2634. {
  2635. struct drm_i915_private *dev_priv = dev->dev_private;
  2636. /* deactivate cxsr */
  2637. I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
  2638. }
  2639. /*
  2640. * Latency for FIFO fetches is dependent on several factors:
  2641. * - memory configuration (speed, channels)
  2642. * - chipset
  2643. * - current MCH state
  2644. * It can be fairly high in some situations, so here we assume a fairly
  2645. * pessimal value. It's a tradeoff between extra memory fetches (if we
  2646. * set this value too high, the FIFO will fetch frequently to stay full)
  2647. * and power consumption (set it too low to save power and we might see
  2648. * FIFO underruns and display "flicker").
  2649. *
  2650. * A value of 5us seems to be a good balance; safe for very low end
  2651. * platforms but not overly aggressive on lower latency configs.
  2652. */
  2653. static const int latency_ns = 5000;
  2654. static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
  2655. {
  2656. struct drm_i915_private *dev_priv = dev->dev_private;
  2657. uint32_t dsparb = I915_READ(DSPARB);
  2658. int size;
  2659. size = dsparb & 0x7f;
  2660. if (plane)
  2661. size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
  2662. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2663. plane ? "B" : "A", size);
  2664. return size;
  2665. }
  2666. static int i85x_get_fifo_size(struct drm_device *dev, int plane)
  2667. {
  2668. struct drm_i915_private *dev_priv = dev->dev_private;
  2669. uint32_t dsparb = I915_READ(DSPARB);
  2670. int size;
  2671. size = dsparb & 0x1ff;
  2672. if (plane)
  2673. size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
  2674. size >>= 1; /* Convert to cachelines */
  2675. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2676. plane ? "B" : "A", size);
  2677. return size;
  2678. }
  2679. static int i845_get_fifo_size(struct drm_device *dev, int plane)
  2680. {
  2681. struct drm_i915_private *dev_priv = dev->dev_private;
  2682. uint32_t dsparb = I915_READ(DSPARB);
  2683. int size;
  2684. size = dsparb & 0x7f;
  2685. size >>= 2; /* Convert to cachelines */
  2686. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2687. plane ? "B" : "A",
  2688. size);
  2689. return size;
  2690. }
  2691. static int i830_get_fifo_size(struct drm_device *dev, int plane)
  2692. {
  2693. struct drm_i915_private *dev_priv = dev->dev_private;
  2694. uint32_t dsparb = I915_READ(DSPARB);
  2695. int size;
  2696. size = dsparb & 0x7f;
  2697. size >>= 1; /* Convert to cachelines */
  2698. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2699. plane ? "B" : "A", size);
  2700. return size;
  2701. }
  2702. static void pineview_update_wm(struct drm_device *dev, int planea_clock,
  2703. int planeb_clock, int sr_hdisplay, int unused,
  2704. int pixel_size)
  2705. {
  2706. struct drm_i915_private *dev_priv = dev->dev_private;
  2707. const struct cxsr_latency *latency;
  2708. u32 reg;
  2709. unsigned long wm;
  2710. int sr_clock;
  2711. latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
  2712. dev_priv->fsb_freq, dev_priv->mem_freq);
  2713. if (!latency) {
  2714. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  2715. pineview_disable_cxsr(dev);
  2716. return;
  2717. }
  2718. if (!planea_clock || !planeb_clock) {
  2719. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2720. /* Display SR */
  2721. wm = intel_calculate_wm(sr_clock, &pineview_display_wm,
  2722. pixel_size, latency->display_sr);
  2723. reg = I915_READ(DSPFW1);
  2724. reg &= ~DSPFW_SR_MASK;
  2725. reg |= wm << DSPFW_SR_SHIFT;
  2726. I915_WRITE(DSPFW1, reg);
  2727. DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
  2728. /* cursor SR */
  2729. wm = intel_calculate_wm(sr_clock, &pineview_cursor_wm,
  2730. pixel_size, latency->cursor_sr);
  2731. reg = I915_READ(DSPFW3);
  2732. reg &= ~DSPFW_CURSOR_SR_MASK;
  2733. reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
  2734. I915_WRITE(DSPFW3, reg);
  2735. /* Display HPLL off SR */
  2736. wm = intel_calculate_wm(sr_clock, &pineview_display_hplloff_wm,
  2737. pixel_size, latency->display_hpll_disable);
  2738. reg = I915_READ(DSPFW3);
  2739. reg &= ~DSPFW_HPLL_SR_MASK;
  2740. reg |= wm & DSPFW_HPLL_SR_MASK;
  2741. I915_WRITE(DSPFW3, reg);
  2742. /* cursor HPLL off SR */
  2743. wm = intel_calculate_wm(sr_clock, &pineview_cursor_hplloff_wm,
  2744. pixel_size, latency->cursor_hpll_disable);
  2745. reg = I915_READ(DSPFW3);
  2746. reg &= ~DSPFW_HPLL_CURSOR_MASK;
  2747. reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
  2748. I915_WRITE(DSPFW3, reg);
  2749. DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
  2750. /* activate cxsr */
  2751. I915_WRITE(DSPFW3,
  2752. I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
  2753. DRM_DEBUG_KMS("Self-refresh is enabled\n");
  2754. } else {
  2755. pineview_disable_cxsr(dev);
  2756. DRM_DEBUG_KMS("Self-refresh is disabled\n");
  2757. }
  2758. }
  2759. static void g4x_update_wm(struct drm_device *dev, int planea_clock,
  2760. int planeb_clock, int sr_hdisplay, int sr_htotal,
  2761. int pixel_size)
  2762. {
  2763. struct drm_i915_private *dev_priv = dev->dev_private;
  2764. int total_size, cacheline_size;
  2765. int planea_wm, planeb_wm, cursora_wm, cursorb_wm, cursor_sr;
  2766. struct intel_watermark_params planea_params, planeb_params;
  2767. unsigned long line_time_us;
  2768. int sr_clock, sr_entries = 0, entries_required;
  2769. /* Create copies of the base settings for each pipe */
  2770. planea_params = planeb_params = g4x_wm_info;
  2771. /* Grab a couple of global values before we overwrite them */
  2772. total_size = planea_params.fifo_size;
  2773. cacheline_size = planea_params.cacheline_size;
  2774. /*
  2775. * Note: we need to make sure we don't overflow for various clock &
  2776. * latency values.
  2777. * clocks go from a few thousand to several hundred thousand.
  2778. * latency is usually a few thousand
  2779. */
  2780. entries_required = ((planea_clock / 1000) * pixel_size * latency_ns) /
  2781. 1000;
  2782. entries_required = DIV_ROUND_UP(entries_required, G4X_FIFO_LINE_SIZE);
  2783. planea_wm = entries_required + planea_params.guard_size;
  2784. entries_required = ((planeb_clock / 1000) * pixel_size * latency_ns) /
  2785. 1000;
  2786. entries_required = DIV_ROUND_UP(entries_required, G4X_FIFO_LINE_SIZE);
  2787. planeb_wm = entries_required + planeb_params.guard_size;
  2788. cursora_wm = cursorb_wm = 16;
  2789. cursor_sr = 32;
  2790. DRM_DEBUG("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  2791. /* Calc sr entries for one plane configs */
  2792. if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
  2793. /* self-refresh has much higher latency */
  2794. static const int sr_latency_ns = 12000;
  2795. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2796. line_time_us = ((sr_htotal * 1000) / sr_clock);
  2797. /* Use ns/us then divide to preserve precision */
  2798. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  2799. pixel_size * sr_hdisplay;
  2800. sr_entries = DIV_ROUND_UP(sr_entries, cacheline_size);
  2801. entries_required = (((sr_latency_ns / line_time_us) +
  2802. 1000) / 1000) * pixel_size * 64;
  2803. entries_required = DIV_ROUND_UP(entries_required,
  2804. g4x_cursor_wm_info.cacheline_size);
  2805. cursor_sr = entries_required + g4x_cursor_wm_info.guard_size;
  2806. if (cursor_sr > g4x_cursor_wm_info.max_wm)
  2807. cursor_sr = g4x_cursor_wm_info.max_wm;
  2808. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  2809. "cursor %d\n", sr_entries, cursor_sr);
  2810. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  2811. } else {
  2812. /* Turn off self refresh if both pipes are enabled */
  2813. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  2814. & ~FW_BLC_SELF_EN);
  2815. }
  2816. DRM_DEBUG("Setting FIFO watermarks - A: %d, B: %d, SR %d\n",
  2817. planea_wm, planeb_wm, sr_entries);
  2818. planea_wm &= 0x3f;
  2819. planeb_wm &= 0x3f;
  2820. I915_WRITE(DSPFW1, (sr_entries << DSPFW_SR_SHIFT) |
  2821. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  2822. (planeb_wm << DSPFW_PLANEB_SHIFT) | planea_wm);
  2823. I915_WRITE(DSPFW2, (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
  2824. (cursora_wm << DSPFW_CURSORA_SHIFT));
  2825. /* HPLL off in SR has some issues on G4x... disable it */
  2826. I915_WRITE(DSPFW3, (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
  2827. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  2828. }
  2829. static void i965_update_wm(struct drm_device *dev, int planea_clock,
  2830. int planeb_clock, int sr_hdisplay, int sr_htotal,
  2831. int pixel_size)
  2832. {
  2833. struct drm_i915_private *dev_priv = dev->dev_private;
  2834. unsigned long line_time_us;
  2835. int sr_clock, sr_entries, srwm = 1;
  2836. int cursor_sr = 16;
  2837. /* Calc sr entries for one plane configs */
  2838. if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
  2839. /* self-refresh has much higher latency */
  2840. static const int sr_latency_ns = 12000;
  2841. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2842. line_time_us = ((sr_htotal * 1000) / sr_clock);
  2843. /* Use ns/us then divide to preserve precision */
  2844. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  2845. pixel_size * sr_hdisplay;
  2846. sr_entries = DIV_ROUND_UP(sr_entries, I915_FIFO_LINE_SIZE);
  2847. DRM_DEBUG("self-refresh entries: %d\n", sr_entries);
  2848. srwm = I965_FIFO_SIZE - sr_entries;
  2849. if (srwm < 0)
  2850. srwm = 1;
  2851. srwm &= 0x1ff;
  2852. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  2853. pixel_size * 64;
  2854. sr_entries = DIV_ROUND_UP(sr_entries,
  2855. i965_cursor_wm_info.cacheline_size);
  2856. cursor_sr = i965_cursor_wm_info.fifo_size -
  2857. (sr_entries + i965_cursor_wm_info.guard_size);
  2858. if (cursor_sr > i965_cursor_wm_info.max_wm)
  2859. cursor_sr = i965_cursor_wm_info.max_wm;
  2860. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  2861. "cursor %d\n", srwm, cursor_sr);
  2862. if (IS_I965GM(dev))
  2863. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  2864. } else {
  2865. /* Turn off self refresh if both pipes are enabled */
  2866. if (IS_I965GM(dev))
  2867. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  2868. & ~FW_BLC_SELF_EN);
  2869. }
  2870. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  2871. srwm);
  2872. /* 965 has limitations... */
  2873. I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) | (8 << 16) | (8 << 8) |
  2874. (8 << 0));
  2875. I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
  2876. /* update cursor SR watermark */
  2877. I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  2878. }
  2879. static void i9xx_update_wm(struct drm_device *dev, int planea_clock,
  2880. int planeb_clock, int sr_hdisplay, int sr_htotal,
  2881. int pixel_size)
  2882. {
  2883. struct drm_i915_private *dev_priv = dev->dev_private;
  2884. uint32_t fwater_lo;
  2885. uint32_t fwater_hi;
  2886. int total_size, cacheline_size, cwm, srwm = 1;
  2887. int planea_wm, planeb_wm;
  2888. struct intel_watermark_params planea_params, planeb_params;
  2889. unsigned long line_time_us;
  2890. int sr_clock, sr_entries = 0;
  2891. /* Create copies of the base settings for each pipe */
  2892. if (IS_I965GM(dev) || IS_I945GM(dev))
  2893. planea_params = planeb_params = i945_wm_info;
  2894. else if (IS_I9XX(dev))
  2895. planea_params = planeb_params = i915_wm_info;
  2896. else
  2897. planea_params = planeb_params = i855_wm_info;
  2898. /* Grab a couple of global values before we overwrite them */
  2899. total_size = planea_params.fifo_size;
  2900. cacheline_size = planea_params.cacheline_size;
  2901. /* Update per-plane FIFO sizes */
  2902. planea_params.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  2903. planeb_params.fifo_size = dev_priv->display.get_fifo_size(dev, 1);
  2904. planea_wm = intel_calculate_wm(planea_clock, &planea_params,
  2905. pixel_size, latency_ns);
  2906. planeb_wm = intel_calculate_wm(planeb_clock, &planeb_params,
  2907. pixel_size, latency_ns);
  2908. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  2909. /*
  2910. * Overlay gets an aggressive default since video jitter is bad.
  2911. */
  2912. cwm = 2;
  2913. /* Calc sr entries for one plane configs */
  2914. if (HAS_FW_BLC(dev) && sr_hdisplay &&
  2915. (!planea_clock || !planeb_clock)) {
  2916. /* self-refresh has much higher latency */
  2917. static const int sr_latency_ns = 6000;
  2918. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2919. line_time_us = ((sr_htotal * 1000) / sr_clock);
  2920. /* Use ns/us then divide to preserve precision */
  2921. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  2922. pixel_size * sr_hdisplay;
  2923. sr_entries = DIV_ROUND_UP(sr_entries, cacheline_size);
  2924. DRM_DEBUG_KMS("self-refresh entries: %d\n", sr_entries);
  2925. srwm = total_size - sr_entries;
  2926. if (srwm < 0)
  2927. srwm = 1;
  2928. if (IS_I945G(dev) || IS_I945GM(dev))
  2929. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  2930. else if (IS_I915GM(dev)) {
  2931. /* 915M has a smaller SRWM field */
  2932. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  2933. I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
  2934. }
  2935. } else {
  2936. /* Turn off self refresh if both pipes are enabled */
  2937. if (IS_I945G(dev) || IS_I945GM(dev)) {
  2938. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  2939. & ~FW_BLC_SELF_EN);
  2940. } else if (IS_I915GM(dev)) {
  2941. I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
  2942. }
  2943. }
  2944. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  2945. planea_wm, planeb_wm, cwm, srwm);
  2946. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  2947. fwater_hi = (cwm & 0x1f);
  2948. /* Set request length to 8 cachelines per fetch */
  2949. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  2950. fwater_hi = fwater_hi | (1 << 8);
  2951. I915_WRITE(FW_BLC, fwater_lo);
  2952. I915_WRITE(FW_BLC2, fwater_hi);
  2953. }
  2954. static void i830_update_wm(struct drm_device *dev, int planea_clock, int unused,
  2955. int unused2, int unused3, int pixel_size)
  2956. {
  2957. struct drm_i915_private *dev_priv = dev->dev_private;
  2958. uint32_t fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  2959. int planea_wm;
  2960. i830_wm_info.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  2961. planea_wm = intel_calculate_wm(planea_clock, &i830_wm_info,
  2962. pixel_size, latency_ns);
  2963. fwater_lo |= (3<<8) | planea_wm;
  2964. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  2965. I915_WRITE(FW_BLC, fwater_lo);
  2966. }
  2967. #define ILK_LP0_PLANE_LATENCY 700
  2968. #define ILK_LP0_CURSOR_LATENCY 1300
  2969. static bool ironlake_compute_wm0(struct drm_device *dev,
  2970. int pipe,
  2971. int *plane_wm,
  2972. int *cursor_wm)
  2973. {
  2974. struct drm_crtc *crtc;
  2975. int htotal, hdisplay, clock, pixel_size = 0;
  2976. int line_time_us, line_count, entries;
  2977. crtc = intel_get_crtc_for_pipe(dev, pipe);
  2978. if (crtc->fb == NULL || !crtc->enabled)
  2979. return false;
  2980. htotal = crtc->mode.htotal;
  2981. hdisplay = crtc->mode.hdisplay;
  2982. clock = crtc->mode.clock;
  2983. pixel_size = crtc->fb->bits_per_pixel / 8;
  2984. /* Use the small buffer method to calculate plane watermark */
  2985. entries = ((clock * pixel_size / 1000) * ILK_LP0_PLANE_LATENCY) / 1000;
  2986. entries = DIV_ROUND_UP(entries,
  2987. ironlake_display_wm_info.cacheline_size);
  2988. *plane_wm = entries + ironlake_display_wm_info.guard_size;
  2989. if (*plane_wm > (int)ironlake_display_wm_info.max_wm)
  2990. *plane_wm = ironlake_display_wm_info.max_wm;
  2991. /* Use the large buffer method to calculate cursor watermark */
  2992. line_time_us = ((htotal * 1000) / clock);
  2993. line_count = (ILK_LP0_CURSOR_LATENCY / line_time_us + 1000) / 1000;
  2994. entries = line_count * 64 * pixel_size;
  2995. entries = DIV_ROUND_UP(entries,
  2996. ironlake_cursor_wm_info.cacheline_size);
  2997. *cursor_wm = entries + ironlake_cursor_wm_info.guard_size;
  2998. if (*cursor_wm > ironlake_cursor_wm_info.max_wm)
  2999. *cursor_wm = ironlake_cursor_wm_info.max_wm;
  3000. return true;
  3001. }
  3002. static void ironlake_update_wm(struct drm_device *dev,
  3003. int planea_clock, int planeb_clock,
  3004. int sr_hdisplay, int sr_htotal,
  3005. int pixel_size)
  3006. {
  3007. struct drm_i915_private *dev_priv = dev->dev_private;
  3008. int plane_wm, cursor_wm, enabled;
  3009. int tmp;
  3010. enabled = 0;
  3011. if (ironlake_compute_wm0(dev, 0, &plane_wm, &cursor_wm)) {
  3012. I915_WRITE(WM0_PIPEA_ILK,
  3013. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3014. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  3015. " plane %d, " "cursor: %d\n",
  3016. plane_wm, cursor_wm);
  3017. enabled++;
  3018. }
  3019. if (ironlake_compute_wm0(dev, 1, &plane_wm, &cursor_wm)) {
  3020. I915_WRITE(WM0_PIPEB_ILK,
  3021. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3022. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  3023. " plane %d, cursor: %d\n",
  3024. plane_wm, cursor_wm);
  3025. enabled++;
  3026. }
  3027. /*
  3028. * Calculate and update the self-refresh watermark only when one
  3029. * display plane is used.
  3030. */
  3031. tmp = 0;
  3032. if (enabled == 1 && /* XXX disabled due to buggy implmentation? */ 0) {
  3033. unsigned long line_time_us;
  3034. int small, large, plane_fbc;
  3035. int sr_clock, entries;
  3036. int line_count, line_size;
  3037. /* Read the self-refresh latency. The unit is 0.5us */
  3038. int ilk_sr_latency = I915_READ(MLTR_ILK) & ILK_SRLT_MASK;
  3039. sr_clock = planea_clock ? planea_clock : planeb_clock;
  3040. line_time_us = (sr_htotal * 1000) / sr_clock;
  3041. /* Use ns/us then divide to preserve precision */
  3042. line_count = ((ilk_sr_latency * 500) / line_time_us + 1000)
  3043. / 1000;
  3044. line_size = sr_hdisplay * pixel_size;
  3045. /* Use the minimum of the small and large buffer method for primary */
  3046. small = ((sr_clock * pixel_size / 1000) * (ilk_sr_latency * 500)) / 1000;
  3047. large = line_count * line_size;
  3048. entries = DIV_ROUND_UP(min(small, large),
  3049. ironlake_display_srwm_info.cacheline_size);
  3050. plane_fbc = entries * 64;
  3051. plane_fbc = DIV_ROUND_UP(plane_fbc, line_size);
  3052. plane_wm = entries + ironlake_display_srwm_info.guard_size;
  3053. if (plane_wm > (int)ironlake_display_srwm_info.max_wm)
  3054. plane_wm = ironlake_display_srwm_info.max_wm;
  3055. /* calculate the self-refresh watermark for display cursor */
  3056. entries = line_count * pixel_size * 64;
  3057. entries = DIV_ROUND_UP(entries,
  3058. ironlake_cursor_srwm_info.cacheline_size);
  3059. cursor_wm = entries + ironlake_cursor_srwm_info.guard_size;
  3060. if (cursor_wm > (int)ironlake_cursor_srwm_info.max_wm)
  3061. cursor_wm = ironlake_cursor_srwm_info.max_wm;
  3062. /* configure watermark and enable self-refresh */
  3063. tmp = (WM1_LP_SR_EN |
  3064. (ilk_sr_latency << WM1_LP_LATENCY_SHIFT) |
  3065. (plane_fbc << WM1_LP_FBC_SHIFT) |
  3066. (plane_wm << WM1_LP_SR_SHIFT) |
  3067. cursor_wm);
  3068. DRM_DEBUG_KMS("self-refresh watermark: display plane %d, fbc lines %d,"
  3069. " cursor %d\n", plane_wm, plane_fbc, cursor_wm);
  3070. }
  3071. I915_WRITE(WM1_LP_ILK, tmp);
  3072. /* XXX setup WM2 and WM3 */
  3073. }
  3074. /**
  3075. * intel_update_watermarks - update FIFO watermark values based on current modes
  3076. *
  3077. * Calculate watermark values for the various WM regs based on current mode
  3078. * and plane configuration.
  3079. *
  3080. * There are several cases to deal with here:
  3081. * - normal (i.e. non-self-refresh)
  3082. * - self-refresh (SR) mode
  3083. * - lines are large relative to FIFO size (buffer can hold up to 2)
  3084. * - lines are small relative to FIFO size (buffer can hold more than 2
  3085. * lines), so need to account for TLB latency
  3086. *
  3087. * The normal calculation is:
  3088. * watermark = dotclock * bytes per pixel * latency
  3089. * where latency is platform & configuration dependent (we assume pessimal
  3090. * values here).
  3091. *
  3092. * The SR calculation is:
  3093. * watermark = (trunc(latency/line time)+1) * surface width *
  3094. * bytes per pixel
  3095. * where
  3096. * line time = htotal / dotclock
  3097. * surface width = hdisplay for normal plane and 64 for cursor
  3098. * and latency is assumed to be high, as above.
  3099. *
  3100. * The final value programmed to the register should always be rounded up,
  3101. * and include an extra 2 entries to account for clock crossings.
  3102. *
  3103. * We don't use the sprite, so we can ignore that. And on Crestline we have
  3104. * to set the non-SR watermarks to 8.
  3105. */
  3106. static void intel_update_watermarks(struct drm_device *dev)
  3107. {
  3108. struct drm_i915_private *dev_priv = dev->dev_private;
  3109. struct drm_crtc *crtc;
  3110. int sr_hdisplay = 0;
  3111. unsigned long planea_clock = 0, planeb_clock = 0, sr_clock = 0;
  3112. int enabled = 0, pixel_size = 0;
  3113. int sr_htotal = 0;
  3114. if (!dev_priv->display.update_wm)
  3115. return;
  3116. /* Get the clock config from both planes */
  3117. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  3118. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3119. if (intel_crtc->dpms_mode == DRM_MODE_DPMS_ON) {
  3120. enabled++;
  3121. if (intel_crtc->plane == 0) {
  3122. DRM_DEBUG_KMS("plane A (pipe %d) clock: %d\n",
  3123. intel_crtc->pipe, crtc->mode.clock);
  3124. planea_clock = crtc->mode.clock;
  3125. } else {
  3126. DRM_DEBUG_KMS("plane B (pipe %d) clock: %d\n",
  3127. intel_crtc->pipe, crtc->mode.clock);
  3128. planeb_clock = crtc->mode.clock;
  3129. }
  3130. sr_hdisplay = crtc->mode.hdisplay;
  3131. sr_clock = crtc->mode.clock;
  3132. sr_htotal = crtc->mode.htotal;
  3133. if (crtc->fb)
  3134. pixel_size = crtc->fb->bits_per_pixel / 8;
  3135. else
  3136. pixel_size = 4; /* by default */
  3137. }
  3138. }
  3139. if (enabled <= 0)
  3140. return;
  3141. dev_priv->display.update_wm(dev, planea_clock, planeb_clock,
  3142. sr_hdisplay, sr_htotal, pixel_size);
  3143. }
  3144. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  3145. struct drm_display_mode *mode,
  3146. struct drm_display_mode *adjusted_mode,
  3147. int x, int y,
  3148. struct drm_framebuffer *old_fb)
  3149. {
  3150. struct drm_device *dev = crtc->dev;
  3151. struct drm_i915_private *dev_priv = dev->dev_private;
  3152. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3153. int pipe = intel_crtc->pipe;
  3154. int plane = intel_crtc->plane;
  3155. int fp_reg = (pipe == 0) ? FPA0 : FPB0;
  3156. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  3157. int dpll_md_reg = (intel_crtc->pipe == 0) ? DPLL_A_MD : DPLL_B_MD;
  3158. int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
  3159. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  3160. int htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
  3161. int hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
  3162. int hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
  3163. int vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
  3164. int vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
  3165. int vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
  3166. int dspsize_reg = (plane == 0) ? DSPASIZE : DSPBSIZE;
  3167. int dsppos_reg = (plane == 0) ? DSPAPOS : DSPBPOS;
  3168. int pipesrc_reg = (pipe == 0) ? PIPEASRC : PIPEBSRC;
  3169. int refclk, num_connectors = 0;
  3170. intel_clock_t clock, reduced_clock;
  3171. u32 dpll = 0, fp = 0, fp2 = 0, dspcntr, pipeconf;
  3172. bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
  3173. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  3174. struct intel_encoder *has_edp_encoder = NULL;
  3175. struct drm_mode_config *mode_config = &dev->mode_config;
  3176. struct drm_encoder *encoder;
  3177. const intel_limit_t *limit;
  3178. int ret;
  3179. struct fdi_m_n m_n = {0};
  3180. int data_m1_reg = (pipe == 0) ? PIPEA_DATA_M1 : PIPEB_DATA_M1;
  3181. int data_n1_reg = (pipe == 0) ? PIPEA_DATA_N1 : PIPEB_DATA_N1;
  3182. int link_m1_reg = (pipe == 0) ? PIPEA_LINK_M1 : PIPEB_LINK_M1;
  3183. int link_n1_reg = (pipe == 0) ? PIPEA_LINK_N1 : PIPEB_LINK_N1;
  3184. int pch_fp_reg = (pipe == 0) ? PCH_FPA0 : PCH_FPB0;
  3185. int pch_dpll_reg = (pipe == 0) ? PCH_DPLL_A : PCH_DPLL_B;
  3186. int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
  3187. int fdi_tx_reg = (pipe == 0) ? FDI_TXA_CTL : FDI_TXB_CTL;
  3188. int trans_dpll_sel = (pipe == 0) ? 0 : 1;
  3189. int lvds_reg = LVDS;
  3190. u32 temp;
  3191. int target_clock;
  3192. drm_vblank_pre_modeset(dev, pipe);
  3193. list_for_each_entry(encoder, &mode_config->encoder_list, head) {
  3194. struct intel_encoder *intel_encoder;
  3195. if (encoder->crtc != crtc)
  3196. continue;
  3197. intel_encoder = to_intel_encoder(encoder);
  3198. switch (intel_encoder->type) {
  3199. case INTEL_OUTPUT_LVDS:
  3200. is_lvds = true;
  3201. break;
  3202. case INTEL_OUTPUT_SDVO:
  3203. case INTEL_OUTPUT_HDMI:
  3204. is_sdvo = true;
  3205. if (intel_encoder->needs_tv_clock)
  3206. is_tv = true;
  3207. break;
  3208. case INTEL_OUTPUT_DVO:
  3209. is_dvo = true;
  3210. break;
  3211. case INTEL_OUTPUT_TVOUT:
  3212. is_tv = true;
  3213. break;
  3214. case INTEL_OUTPUT_ANALOG:
  3215. is_crt = true;
  3216. break;
  3217. case INTEL_OUTPUT_DISPLAYPORT:
  3218. is_dp = true;
  3219. break;
  3220. case INTEL_OUTPUT_EDP:
  3221. has_edp_encoder = intel_encoder;
  3222. break;
  3223. }
  3224. num_connectors++;
  3225. }
  3226. if (is_lvds && dev_priv->lvds_use_ssc && num_connectors < 2) {
  3227. refclk = dev_priv->lvds_ssc_freq * 1000;
  3228. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  3229. refclk / 1000);
  3230. } else if (IS_I9XX(dev)) {
  3231. refclk = 96000;
  3232. if (HAS_PCH_SPLIT(dev))
  3233. refclk = 120000; /* 120Mhz refclk */
  3234. } else {
  3235. refclk = 48000;
  3236. }
  3237. /*
  3238. * Returns a set of divisors for the desired target clock with the given
  3239. * refclk, or FALSE. The returned values represent the clock equation:
  3240. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  3241. */
  3242. limit = intel_limit(crtc);
  3243. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
  3244. if (!ok) {
  3245. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  3246. drm_vblank_post_modeset(dev, pipe);
  3247. return -EINVAL;
  3248. }
  3249. /* Ensure that the cursor is valid for the new mode before changing... */
  3250. intel_crtc_update_cursor(crtc);
  3251. if (is_lvds && dev_priv->lvds_downclock_avail) {
  3252. has_reduced_clock = limit->find_pll(limit, crtc,
  3253. dev_priv->lvds_downclock,
  3254. refclk,
  3255. &reduced_clock);
  3256. if (has_reduced_clock && (clock.p != reduced_clock.p)) {
  3257. /*
  3258. * If the different P is found, it means that we can't
  3259. * switch the display clock by using the FP0/FP1.
  3260. * In such case we will disable the LVDS downclock
  3261. * feature.
  3262. */
  3263. DRM_DEBUG_KMS("Different P is found for "
  3264. "LVDS clock/downclock\n");
  3265. has_reduced_clock = 0;
  3266. }
  3267. }
  3268. /* SDVO TV has fixed PLL values depend on its clock range,
  3269. this mirrors vbios setting. */
  3270. if (is_sdvo && is_tv) {
  3271. if (adjusted_mode->clock >= 100000
  3272. && adjusted_mode->clock < 140500) {
  3273. clock.p1 = 2;
  3274. clock.p2 = 10;
  3275. clock.n = 3;
  3276. clock.m1 = 16;
  3277. clock.m2 = 8;
  3278. } else if (adjusted_mode->clock >= 140500
  3279. && adjusted_mode->clock <= 200000) {
  3280. clock.p1 = 1;
  3281. clock.p2 = 10;
  3282. clock.n = 6;
  3283. clock.m1 = 12;
  3284. clock.m2 = 8;
  3285. }
  3286. }
  3287. /* FDI link */
  3288. if (HAS_PCH_SPLIT(dev)) {
  3289. int lane = 0, link_bw, bpp;
  3290. /* eDP doesn't require FDI link, so just set DP M/N
  3291. according to current link config */
  3292. if (has_edp_encoder) {
  3293. target_clock = mode->clock;
  3294. intel_edp_link_config(has_edp_encoder,
  3295. &lane, &link_bw);
  3296. } else {
  3297. /* DP over FDI requires target mode clock
  3298. instead of link clock */
  3299. if (is_dp)
  3300. target_clock = mode->clock;
  3301. else
  3302. target_clock = adjusted_mode->clock;
  3303. /* FDI is a binary signal running at ~2.7GHz, encoding
  3304. * each output octet as 10 bits. The actual frequency
  3305. * is stored as a divider into a 100MHz clock, and the
  3306. * mode pixel clock is stored in units of 1KHz.
  3307. * Hence the bw of each lane in terms of the mode signal
  3308. * is:
  3309. */
  3310. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  3311. }
  3312. /* determine panel color depth */
  3313. temp = I915_READ(pipeconf_reg);
  3314. temp &= ~PIPE_BPC_MASK;
  3315. if (is_lvds) {
  3316. int lvds_reg = I915_READ(PCH_LVDS);
  3317. /* the BPC will be 6 if it is 18-bit LVDS panel */
  3318. if ((lvds_reg & LVDS_A3_POWER_MASK) == LVDS_A3_POWER_UP)
  3319. temp |= PIPE_8BPC;
  3320. else
  3321. temp |= PIPE_6BPC;
  3322. } else if (has_edp_encoder || (is_dp && intel_pch_has_edp(crtc))) {
  3323. switch (dev_priv->edp_bpp/3) {
  3324. case 8:
  3325. temp |= PIPE_8BPC;
  3326. break;
  3327. case 10:
  3328. temp |= PIPE_10BPC;
  3329. break;
  3330. case 6:
  3331. temp |= PIPE_6BPC;
  3332. break;
  3333. case 12:
  3334. temp |= PIPE_12BPC;
  3335. break;
  3336. }
  3337. } else
  3338. temp |= PIPE_8BPC;
  3339. I915_WRITE(pipeconf_reg, temp);
  3340. I915_READ(pipeconf_reg);
  3341. switch (temp & PIPE_BPC_MASK) {
  3342. case PIPE_8BPC:
  3343. bpp = 24;
  3344. break;
  3345. case PIPE_10BPC:
  3346. bpp = 30;
  3347. break;
  3348. case PIPE_6BPC:
  3349. bpp = 18;
  3350. break;
  3351. case PIPE_12BPC:
  3352. bpp = 36;
  3353. break;
  3354. default:
  3355. DRM_ERROR("unknown pipe bpc value\n");
  3356. bpp = 24;
  3357. }
  3358. if (!lane) {
  3359. /*
  3360. * Account for spread spectrum to avoid
  3361. * oversubscribing the link. Max center spread
  3362. * is 2.5%; use 5% for safety's sake.
  3363. */
  3364. u32 bps = target_clock * bpp * 21 / 20;
  3365. lane = bps / (link_bw * 8) + 1;
  3366. }
  3367. intel_crtc->fdi_lanes = lane;
  3368. ironlake_compute_m_n(bpp, lane, target_clock, link_bw, &m_n);
  3369. }
  3370. /* Ironlake: try to setup display ref clock before DPLL
  3371. * enabling. This is only under driver's control after
  3372. * PCH B stepping, previous chipset stepping should be
  3373. * ignoring this setting.
  3374. */
  3375. if (HAS_PCH_SPLIT(dev)) {
  3376. temp = I915_READ(PCH_DREF_CONTROL);
  3377. /* Always enable nonspread source */
  3378. temp &= ~DREF_NONSPREAD_SOURCE_MASK;
  3379. temp |= DREF_NONSPREAD_SOURCE_ENABLE;
  3380. I915_WRITE(PCH_DREF_CONTROL, temp);
  3381. POSTING_READ(PCH_DREF_CONTROL);
  3382. temp &= ~DREF_SSC_SOURCE_MASK;
  3383. temp |= DREF_SSC_SOURCE_ENABLE;
  3384. I915_WRITE(PCH_DREF_CONTROL, temp);
  3385. POSTING_READ(PCH_DREF_CONTROL);
  3386. udelay(200);
  3387. if (has_edp_encoder) {
  3388. if (dev_priv->lvds_use_ssc) {
  3389. temp |= DREF_SSC1_ENABLE;
  3390. I915_WRITE(PCH_DREF_CONTROL, temp);
  3391. POSTING_READ(PCH_DREF_CONTROL);
  3392. udelay(200);
  3393. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  3394. temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  3395. I915_WRITE(PCH_DREF_CONTROL, temp);
  3396. POSTING_READ(PCH_DREF_CONTROL);
  3397. } else {
  3398. temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  3399. I915_WRITE(PCH_DREF_CONTROL, temp);
  3400. POSTING_READ(PCH_DREF_CONTROL);
  3401. }
  3402. }
  3403. }
  3404. if (IS_PINEVIEW(dev)) {
  3405. fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
  3406. if (has_reduced_clock)
  3407. fp2 = (1 << reduced_clock.n) << 16 |
  3408. reduced_clock.m1 << 8 | reduced_clock.m2;
  3409. } else {
  3410. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  3411. if (has_reduced_clock)
  3412. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  3413. reduced_clock.m2;
  3414. }
  3415. if (!HAS_PCH_SPLIT(dev))
  3416. dpll = DPLL_VGA_MODE_DIS;
  3417. if (IS_I9XX(dev)) {
  3418. if (is_lvds)
  3419. dpll |= DPLLB_MODE_LVDS;
  3420. else
  3421. dpll |= DPLLB_MODE_DAC_SERIAL;
  3422. if (is_sdvo) {
  3423. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  3424. if (pixel_multiplier > 1) {
  3425. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3426. dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  3427. else if (HAS_PCH_SPLIT(dev))
  3428. dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  3429. }
  3430. dpll |= DPLL_DVO_HIGH_SPEED;
  3431. }
  3432. if (is_dp)
  3433. dpll |= DPLL_DVO_HIGH_SPEED;
  3434. /* compute bitmask from p1 value */
  3435. if (IS_PINEVIEW(dev))
  3436. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  3437. else {
  3438. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3439. /* also FPA1 */
  3440. if (HAS_PCH_SPLIT(dev))
  3441. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3442. if (IS_G4X(dev) && has_reduced_clock)
  3443. dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3444. }
  3445. switch (clock.p2) {
  3446. case 5:
  3447. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  3448. break;
  3449. case 7:
  3450. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  3451. break;
  3452. case 10:
  3453. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  3454. break;
  3455. case 14:
  3456. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  3457. break;
  3458. }
  3459. if (IS_I965G(dev) && !HAS_PCH_SPLIT(dev))
  3460. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  3461. } else {
  3462. if (is_lvds) {
  3463. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3464. } else {
  3465. if (clock.p1 == 2)
  3466. dpll |= PLL_P1_DIVIDE_BY_TWO;
  3467. else
  3468. dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3469. if (clock.p2 == 4)
  3470. dpll |= PLL_P2_DIVIDE_BY_4;
  3471. }
  3472. }
  3473. if (is_sdvo && is_tv)
  3474. dpll |= PLL_REF_INPUT_TVCLKINBC;
  3475. else if (is_tv)
  3476. /* XXX: just matching BIOS for now */
  3477. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  3478. dpll |= 3;
  3479. else if (is_lvds && dev_priv->lvds_use_ssc && num_connectors < 2)
  3480. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3481. else
  3482. dpll |= PLL_REF_INPUT_DREFCLK;
  3483. /* setup pipeconf */
  3484. pipeconf = I915_READ(pipeconf_reg);
  3485. /* Set up the display plane register */
  3486. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3487. /* Ironlake's plane is forced to pipe, bit 24 is to
  3488. enable color space conversion */
  3489. if (!HAS_PCH_SPLIT(dev)) {
  3490. if (pipe == 0)
  3491. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  3492. else
  3493. dspcntr |= DISPPLANE_SEL_PIPE_B;
  3494. }
  3495. if (pipe == 0 && !IS_I965G(dev)) {
  3496. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  3497. * core speed.
  3498. *
  3499. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  3500. * pipe == 0 check?
  3501. */
  3502. if (mode->clock >
  3503. dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
  3504. pipeconf |= PIPEACONF_DOUBLE_WIDE;
  3505. else
  3506. pipeconf &= ~PIPEACONF_DOUBLE_WIDE;
  3507. }
  3508. dspcntr |= DISPLAY_PLANE_ENABLE;
  3509. pipeconf |= PIPEACONF_ENABLE;
  3510. dpll |= DPLL_VCO_ENABLE;
  3511. /* Disable the panel fitter if it was on our pipe */
  3512. if (!HAS_PCH_SPLIT(dev) && intel_panel_fitter_pipe(dev) == pipe)
  3513. I915_WRITE(PFIT_CONTROL, 0);
  3514. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  3515. drm_mode_debug_printmodeline(mode);
  3516. /* assign to Ironlake registers */
  3517. if (HAS_PCH_SPLIT(dev)) {
  3518. fp_reg = pch_fp_reg;
  3519. dpll_reg = pch_dpll_reg;
  3520. }
  3521. if (!has_edp_encoder) {
  3522. I915_WRITE(fp_reg, fp);
  3523. I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
  3524. I915_READ(dpll_reg);
  3525. udelay(150);
  3526. }
  3527. /* enable transcoder DPLL */
  3528. if (HAS_PCH_CPT(dev)) {
  3529. temp = I915_READ(PCH_DPLL_SEL);
  3530. if (trans_dpll_sel == 0)
  3531. temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
  3532. else
  3533. temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  3534. I915_WRITE(PCH_DPLL_SEL, temp);
  3535. I915_READ(PCH_DPLL_SEL);
  3536. udelay(150);
  3537. }
  3538. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  3539. * This is an exception to the general rule that mode_set doesn't turn
  3540. * things on.
  3541. */
  3542. if (is_lvds) {
  3543. u32 lvds;
  3544. if (HAS_PCH_SPLIT(dev))
  3545. lvds_reg = PCH_LVDS;
  3546. lvds = I915_READ(lvds_reg);
  3547. lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  3548. if (pipe == 1) {
  3549. if (HAS_PCH_CPT(dev))
  3550. lvds |= PORT_TRANS_B_SEL_CPT;
  3551. else
  3552. lvds |= LVDS_PIPEB_SELECT;
  3553. } else {
  3554. if (HAS_PCH_CPT(dev))
  3555. lvds &= ~PORT_TRANS_SEL_MASK;
  3556. else
  3557. lvds &= ~LVDS_PIPEB_SELECT;
  3558. }
  3559. /* set the corresponsding LVDS_BORDER bit */
  3560. lvds |= dev_priv->lvds_border_bits;
  3561. /* Set the B0-B3 data pairs corresponding to whether we're going to
  3562. * set the DPLLs for dual-channel mode or not.
  3563. */
  3564. if (clock.p2 == 7)
  3565. lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  3566. else
  3567. lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  3568. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  3569. * appropriately here, but we need to look more thoroughly into how
  3570. * panels behave in the two modes.
  3571. */
  3572. /* set the dithering flag on non-PCH LVDS as needed */
  3573. if (IS_I965G(dev) && !HAS_PCH_SPLIT(dev)) {
  3574. if (dev_priv->lvds_dither)
  3575. lvds |= LVDS_ENABLE_DITHER;
  3576. else
  3577. lvds &= ~LVDS_ENABLE_DITHER;
  3578. }
  3579. I915_WRITE(lvds_reg, lvds);
  3580. I915_READ(lvds_reg);
  3581. }
  3582. /* set the dithering flag and clear for anything other than a panel. */
  3583. if (HAS_PCH_SPLIT(dev)) {
  3584. pipeconf &= ~PIPECONF_DITHER_EN;
  3585. pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
  3586. if (dev_priv->lvds_dither && (is_lvds || has_edp_encoder)) {
  3587. pipeconf |= PIPECONF_DITHER_EN;
  3588. pipeconf |= PIPECONF_DITHER_TYPE_ST1;
  3589. }
  3590. }
  3591. if (is_dp)
  3592. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  3593. else if (HAS_PCH_SPLIT(dev)) {
  3594. /* For non-DP output, clear any trans DP clock recovery setting.*/
  3595. if (pipe == 0) {
  3596. I915_WRITE(TRANSA_DATA_M1, 0);
  3597. I915_WRITE(TRANSA_DATA_N1, 0);
  3598. I915_WRITE(TRANSA_DP_LINK_M1, 0);
  3599. I915_WRITE(TRANSA_DP_LINK_N1, 0);
  3600. } else {
  3601. I915_WRITE(TRANSB_DATA_M1, 0);
  3602. I915_WRITE(TRANSB_DATA_N1, 0);
  3603. I915_WRITE(TRANSB_DP_LINK_M1, 0);
  3604. I915_WRITE(TRANSB_DP_LINK_N1, 0);
  3605. }
  3606. }
  3607. if (!has_edp_encoder) {
  3608. I915_WRITE(fp_reg, fp);
  3609. I915_WRITE(dpll_reg, dpll);
  3610. I915_READ(dpll_reg);
  3611. /* Wait for the clocks to stabilize. */
  3612. udelay(150);
  3613. if (IS_I965G(dev) && !HAS_PCH_SPLIT(dev)) {
  3614. if (is_sdvo) {
  3615. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  3616. if (pixel_multiplier > 1)
  3617. pixel_multiplier = (pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  3618. else
  3619. pixel_multiplier = 0;
  3620. I915_WRITE(dpll_md_reg,
  3621. (0 << DPLL_MD_UDI_DIVIDER_SHIFT) |
  3622. pixel_multiplier);
  3623. } else
  3624. I915_WRITE(dpll_md_reg, 0);
  3625. } else {
  3626. /* write it again -- the BIOS does, after all */
  3627. I915_WRITE(dpll_reg, dpll);
  3628. }
  3629. I915_READ(dpll_reg);
  3630. /* Wait for the clocks to stabilize. */
  3631. udelay(150);
  3632. }
  3633. if (is_lvds && has_reduced_clock && i915_powersave) {
  3634. I915_WRITE(fp_reg + 4, fp2);
  3635. intel_crtc->lowfreq_avail = true;
  3636. if (HAS_PIPE_CXSR(dev)) {
  3637. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  3638. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  3639. }
  3640. } else {
  3641. I915_WRITE(fp_reg + 4, fp);
  3642. intel_crtc->lowfreq_avail = false;
  3643. if (HAS_PIPE_CXSR(dev)) {
  3644. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  3645. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  3646. }
  3647. }
  3648. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  3649. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  3650. /* the chip adds 2 halflines automatically */
  3651. adjusted_mode->crtc_vdisplay -= 1;
  3652. adjusted_mode->crtc_vtotal -= 1;
  3653. adjusted_mode->crtc_vblank_start -= 1;
  3654. adjusted_mode->crtc_vblank_end -= 1;
  3655. adjusted_mode->crtc_vsync_end -= 1;
  3656. adjusted_mode->crtc_vsync_start -= 1;
  3657. } else
  3658. pipeconf &= ~PIPECONF_INTERLACE_W_FIELD_INDICATION; /* progressive */
  3659. I915_WRITE(htot_reg, (adjusted_mode->crtc_hdisplay - 1) |
  3660. ((adjusted_mode->crtc_htotal - 1) << 16));
  3661. I915_WRITE(hblank_reg, (adjusted_mode->crtc_hblank_start - 1) |
  3662. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  3663. I915_WRITE(hsync_reg, (adjusted_mode->crtc_hsync_start - 1) |
  3664. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  3665. I915_WRITE(vtot_reg, (adjusted_mode->crtc_vdisplay - 1) |
  3666. ((adjusted_mode->crtc_vtotal - 1) << 16));
  3667. I915_WRITE(vblank_reg, (adjusted_mode->crtc_vblank_start - 1) |
  3668. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  3669. I915_WRITE(vsync_reg, (adjusted_mode->crtc_vsync_start - 1) |
  3670. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  3671. /* pipesrc and dspsize control the size that is scaled from, which should
  3672. * always be the user's requested size.
  3673. */
  3674. if (!HAS_PCH_SPLIT(dev)) {
  3675. I915_WRITE(dspsize_reg, ((mode->vdisplay - 1) << 16) |
  3676. (mode->hdisplay - 1));
  3677. I915_WRITE(dsppos_reg, 0);
  3678. }
  3679. I915_WRITE(pipesrc_reg, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  3680. if (HAS_PCH_SPLIT(dev)) {
  3681. I915_WRITE(data_m1_reg, TU_SIZE(m_n.tu) | m_n.gmch_m);
  3682. I915_WRITE(data_n1_reg, m_n.gmch_n);
  3683. I915_WRITE(link_m1_reg, m_n.link_m);
  3684. I915_WRITE(link_n1_reg, m_n.link_n);
  3685. if (has_edp_encoder) {
  3686. ironlake_set_pll_edp(crtc, adjusted_mode->clock);
  3687. } else {
  3688. /* enable FDI RX PLL too */
  3689. temp = I915_READ(fdi_rx_reg);
  3690. I915_WRITE(fdi_rx_reg, temp | FDI_RX_PLL_ENABLE);
  3691. I915_READ(fdi_rx_reg);
  3692. udelay(200);
  3693. /* enable FDI TX PLL too */
  3694. temp = I915_READ(fdi_tx_reg);
  3695. I915_WRITE(fdi_tx_reg, temp | FDI_TX_PLL_ENABLE);
  3696. I915_READ(fdi_tx_reg);
  3697. /* enable FDI RX PCDCLK */
  3698. temp = I915_READ(fdi_rx_reg);
  3699. I915_WRITE(fdi_rx_reg, temp | FDI_SEL_PCDCLK);
  3700. I915_READ(fdi_rx_reg);
  3701. udelay(200);
  3702. }
  3703. }
  3704. I915_WRITE(pipeconf_reg, pipeconf);
  3705. I915_READ(pipeconf_reg);
  3706. intel_wait_for_vblank(dev, pipe);
  3707. if (IS_IRONLAKE(dev)) {
  3708. /* enable address swizzle for tiling buffer */
  3709. temp = I915_READ(DISP_ARB_CTL);
  3710. I915_WRITE(DISP_ARB_CTL, temp | DISP_TILE_SURFACE_SWIZZLING);
  3711. }
  3712. I915_WRITE(dspcntr_reg, dspcntr);
  3713. /* Flush the plane changes */
  3714. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  3715. intel_update_watermarks(dev);
  3716. drm_vblank_post_modeset(dev, pipe);
  3717. return ret;
  3718. }
  3719. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3720. void intel_crtc_load_lut(struct drm_crtc *crtc)
  3721. {
  3722. struct drm_device *dev = crtc->dev;
  3723. struct drm_i915_private *dev_priv = dev->dev_private;
  3724. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3725. int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
  3726. int i;
  3727. /* The clocks have to be on to load the palette. */
  3728. if (!crtc->enabled)
  3729. return;
  3730. /* use legacy palette for Ironlake */
  3731. if (HAS_PCH_SPLIT(dev))
  3732. palreg = (intel_crtc->pipe == 0) ? LGC_PALETTE_A :
  3733. LGC_PALETTE_B;
  3734. for (i = 0; i < 256; i++) {
  3735. I915_WRITE(palreg + 4 * i,
  3736. (intel_crtc->lut_r[i] << 16) |
  3737. (intel_crtc->lut_g[i] << 8) |
  3738. intel_crtc->lut_b[i]);
  3739. }
  3740. }
  3741. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  3742. {
  3743. struct drm_device *dev = crtc->dev;
  3744. struct drm_i915_private *dev_priv = dev->dev_private;
  3745. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3746. bool visible = base != 0;
  3747. u32 cntl;
  3748. if (intel_crtc->cursor_visible == visible)
  3749. return;
  3750. cntl = I915_READ(CURACNTR);
  3751. if (visible) {
  3752. /* On these chipsets we can only modify the base whilst
  3753. * the cursor is disabled.
  3754. */
  3755. I915_WRITE(CURABASE, base);
  3756. cntl &= ~(CURSOR_FORMAT_MASK);
  3757. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  3758. cntl |= CURSOR_ENABLE |
  3759. CURSOR_GAMMA_ENABLE |
  3760. CURSOR_FORMAT_ARGB;
  3761. } else
  3762. cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  3763. I915_WRITE(CURACNTR, cntl);
  3764. intel_crtc->cursor_visible = visible;
  3765. }
  3766. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  3767. {
  3768. struct drm_device *dev = crtc->dev;
  3769. struct drm_i915_private *dev_priv = dev->dev_private;
  3770. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3771. int pipe = intel_crtc->pipe;
  3772. bool visible = base != 0;
  3773. if (intel_crtc->cursor_visible != visible) {
  3774. uint32_t cntl = I915_READ(pipe == 0 ? CURACNTR : CURBCNTR);
  3775. if (base) {
  3776. cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  3777. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  3778. cntl |= pipe << 28; /* Connect to correct pipe */
  3779. } else {
  3780. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  3781. cntl |= CURSOR_MODE_DISABLE;
  3782. }
  3783. I915_WRITE(pipe == 0 ? CURACNTR : CURBCNTR, cntl);
  3784. intel_crtc->cursor_visible = visible;
  3785. }
  3786. /* and commit changes on next vblank */
  3787. I915_WRITE(pipe == 0 ? CURABASE : CURBBASE, base);
  3788. }
  3789. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  3790. static void intel_crtc_update_cursor(struct drm_crtc *crtc)
  3791. {
  3792. struct drm_device *dev = crtc->dev;
  3793. struct drm_i915_private *dev_priv = dev->dev_private;
  3794. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3795. int pipe = intel_crtc->pipe;
  3796. int x = intel_crtc->cursor_x;
  3797. int y = intel_crtc->cursor_y;
  3798. u32 base, pos;
  3799. bool visible;
  3800. pos = 0;
  3801. if (intel_crtc->cursor_on && crtc->fb) {
  3802. base = intel_crtc->cursor_addr;
  3803. if (x > (int) crtc->fb->width)
  3804. base = 0;
  3805. if (y > (int) crtc->fb->height)
  3806. base = 0;
  3807. } else
  3808. base = 0;
  3809. if (x < 0) {
  3810. if (x + intel_crtc->cursor_width < 0)
  3811. base = 0;
  3812. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  3813. x = -x;
  3814. }
  3815. pos |= x << CURSOR_X_SHIFT;
  3816. if (y < 0) {
  3817. if (y + intel_crtc->cursor_height < 0)
  3818. base = 0;
  3819. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  3820. y = -y;
  3821. }
  3822. pos |= y << CURSOR_Y_SHIFT;
  3823. visible = base != 0;
  3824. if (!visible && !intel_crtc->cursor_visible)
  3825. return;
  3826. I915_WRITE(pipe == 0 ? CURAPOS : CURBPOS, pos);
  3827. if (IS_845G(dev) || IS_I865G(dev))
  3828. i845_update_cursor(crtc, base);
  3829. else
  3830. i9xx_update_cursor(crtc, base);
  3831. if (visible)
  3832. intel_mark_busy(dev, to_intel_framebuffer(crtc->fb)->obj);
  3833. }
  3834. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  3835. struct drm_file *file_priv,
  3836. uint32_t handle,
  3837. uint32_t width, uint32_t height)
  3838. {
  3839. struct drm_device *dev = crtc->dev;
  3840. struct drm_i915_private *dev_priv = dev->dev_private;
  3841. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3842. struct drm_gem_object *bo;
  3843. struct drm_i915_gem_object *obj_priv;
  3844. uint32_t addr;
  3845. int ret;
  3846. DRM_DEBUG_KMS("\n");
  3847. /* if we want to turn off the cursor ignore width and height */
  3848. if (!handle) {
  3849. DRM_DEBUG_KMS("cursor off\n");
  3850. addr = 0;
  3851. bo = NULL;
  3852. mutex_lock(&dev->struct_mutex);
  3853. goto finish;
  3854. }
  3855. /* Currently we only support 64x64 cursors */
  3856. if (width != 64 || height != 64) {
  3857. DRM_ERROR("we currently only support 64x64 cursors\n");
  3858. return -EINVAL;
  3859. }
  3860. bo = drm_gem_object_lookup(dev, file_priv, handle);
  3861. if (!bo)
  3862. return -ENOENT;
  3863. obj_priv = to_intel_bo(bo);
  3864. if (bo->size < width * height * 4) {
  3865. DRM_ERROR("buffer is to small\n");
  3866. ret = -ENOMEM;
  3867. goto fail;
  3868. }
  3869. /* we only need to pin inside GTT if cursor is non-phy */
  3870. mutex_lock(&dev->struct_mutex);
  3871. if (!dev_priv->info->cursor_needs_physical) {
  3872. ret = i915_gem_object_pin(bo, PAGE_SIZE);
  3873. if (ret) {
  3874. DRM_ERROR("failed to pin cursor bo\n");
  3875. goto fail_locked;
  3876. }
  3877. ret = i915_gem_object_set_to_gtt_domain(bo, 0);
  3878. if (ret) {
  3879. DRM_ERROR("failed to move cursor bo into the GTT\n");
  3880. goto fail_unpin;
  3881. }
  3882. addr = obj_priv->gtt_offset;
  3883. } else {
  3884. int align = IS_I830(dev) ? 16 * 1024 : 256;
  3885. ret = i915_gem_attach_phys_object(dev, bo,
  3886. (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
  3887. align);
  3888. if (ret) {
  3889. DRM_ERROR("failed to attach phys object\n");
  3890. goto fail_locked;
  3891. }
  3892. addr = obj_priv->phys_obj->handle->busaddr;
  3893. }
  3894. if (!IS_I9XX(dev))
  3895. I915_WRITE(CURSIZE, (height << 12) | width);
  3896. finish:
  3897. if (intel_crtc->cursor_bo) {
  3898. if (dev_priv->info->cursor_needs_physical) {
  3899. if (intel_crtc->cursor_bo != bo)
  3900. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  3901. } else
  3902. i915_gem_object_unpin(intel_crtc->cursor_bo);
  3903. drm_gem_object_unreference(intel_crtc->cursor_bo);
  3904. }
  3905. mutex_unlock(&dev->struct_mutex);
  3906. intel_crtc->cursor_addr = addr;
  3907. intel_crtc->cursor_bo = bo;
  3908. intel_crtc->cursor_width = width;
  3909. intel_crtc->cursor_height = height;
  3910. intel_crtc_update_cursor(crtc);
  3911. return 0;
  3912. fail_unpin:
  3913. i915_gem_object_unpin(bo);
  3914. fail_locked:
  3915. mutex_unlock(&dev->struct_mutex);
  3916. fail:
  3917. drm_gem_object_unreference_unlocked(bo);
  3918. return ret;
  3919. }
  3920. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  3921. {
  3922. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3923. intel_crtc->cursor_x = x;
  3924. intel_crtc->cursor_y = y;
  3925. intel_crtc_update_cursor(crtc);
  3926. return 0;
  3927. }
  3928. /** Sets the color ramps on behalf of RandR */
  3929. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  3930. u16 blue, int regno)
  3931. {
  3932. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3933. intel_crtc->lut_r[regno] = red >> 8;
  3934. intel_crtc->lut_g[regno] = green >> 8;
  3935. intel_crtc->lut_b[regno] = blue >> 8;
  3936. }
  3937. void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  3938. u16 *blue, int regno)
  3939. {
  3940. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3941. *red = intel_crtc->lut_r[regno] << 8;
  3942. *green = intel_crtc->lut_g[regno] << 8;
  3943. *blue = intel_crtc->lut_b[regno] << 8;
  3944. }
  3945. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  3946. u16 *blue, uint32_t start, uint32_t size)
  3947. {
  3948. int end = (start + size > 256) ? 256 : start + size, i;
  3949. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3950. for (i = start; i < end; i++) {
  3951. intel_crtc->lut_r[i] = red[i] >> 8;
  3952. intel_crtc->lut_g[i] = green[i] >> 8;
  3953. intel_crtc->lut_b[i] = blue[i] >> 8;
  3954. }
  3955. intel_crtc_load_lut(crtc);
  3956. }
  3957. /**
  3958. * Get a pipe with a simple mode set on it for doing load-based monitor
  3959. * detection.
  3960. *
  3961. * It will be up to the load-detect code to adjust the pipe as appropriate for
  3962. * its requirements. The pipe will be connected to no other encoders.
  3963. *
  3964. * Currently this code will only succeed if there is a pipe with no encoders
  3965. * configured for it. In the future, it could choose to temporarily disable
  3966. * some outputs to free up a pipe for its use.
  3967. *
  3968. * \return crtc, or NULL if no pipes are available.
  3969. */
  3970. /* VESA 640x480x72Hz mode to set on the pipe */
  3971. static struct drm_display_mode load_detect_mode = {
  3972. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  3973. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  3974. };
  3975. struct drm_crtc *intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
  3976. struct drm_connector *connector,
  3977. struct drm_display_mode *mode,
  3978. int *dpms_mode)
  3979. {
  3980. struct intel_crtc *intel_crtc;
  3981. struct drm_crtc *possible_crtc;
  3982. struct drm_crtc *supported_crtc =NULL;
  3983. struct drm_encoder *encoder = &intel_encoder->base;
  3984. struct drm_crtc *crtc = NULL;
  3985. struct drm_device *dev = encoder->dev;
  3986. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  3987. struct drm_crtc_helper_funcs *crtc_funcs;
  3988. int i = -1;
  3989. /*
  3990. * Algorithm gets a little messy:
  3991. * - if the connector already has an assigned crtc, use it (but make
  3992. * sure it's on first)
  3993. * - try to find the first unused crtc that can drive this connector,
  3994. * and use that if we find one
  3995. * - if there are no unused crtcs available, try to use the first
  3996. * one we found that supports the connector
  3997. */
  3998. /* See if we already have a CRTC for this connector */
  3999. if (encoder->crtc) {
  4000. crtc = encoder->crtc;
  4001. /* Make sure the crtc and connector are running */
  4002. intel_crtc = to_intel_crtc(crtc);
  4003. *dpms_mode = intel_crtc->dpms_mode;
  4004. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  4005. crtc_funcs = crtc->helper_private;
  4006. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  4007. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  4008. }
  4009. return crtc;
  4010. }
  4011. /* Find an unused one (if possible) */
  4012. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  4013. i++;
  4014. if (!(encoder->possible_crtcs & (1 << i)))
  4015. continue;
  4016. if (!possible_crtc->enabled) {
  4017. crtc = possible_crtc;
  4018. break;
  4019. }
  4020. if (!supported_crtc)
  4021. supported_crtc = possible_crtc;
  4022. }
  4023. /*
  4024. * If we didn't find an unused CRTC, don't use any.
  4025. */
  4026. if (!crtc) {
  4027. return NULL;
  4028. }
  4029. encoder->crtc = crtc;
  4030. connector->encoder = encoder;
  4031. intel_encoder->load_detect_temp = true;
  4032. intel_crtc = to_intel_crtc(crtc);
  4033. *dpms_mode = intel_crtc->dpms_mode;
  4034. if (!crtc->enabled) {
  4035. if (!mode)
  4036. mode = &load_detect_mode;
  4037. drm_crtc_helper_set_mode(crtc, mode, 0, 0, crtc->fb);
  4038. } else {
  4039. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  4040. crtc_funcs = crtc->helper_private;
  4041. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  4042. }
  4043. /* Add this connector to the crtc */
  4044. encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
  4045. encoder_funcs->commit(encoder);
  4046. }
  4047. /* let the connector get through one full cycle before testing */
  4048. intel_wait_for_vblank(dev, intel_crtc->pipe);
  4049. return crtc;
  4050. }
  4051. void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
  4052. struct drm_connector *connector, int dpms_mode)
  4053. {
  4054. struct drm_encoder *encoder = &intel_encoder->base;
  4055. struct drm_device *dev = encoder->dev;
  4056. struct drm_crtc *crtc = encoder->crtc;
  4057. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  4058. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  4059. if (intel_encoder->load_detect_temp) {
  4060. encoder->crtc = NULL;
  4061. connector->encoder = NULL;
  4062. intel_encoder->load_detect_temp = false;
  4063. crtc->enabled = drm_helper_crtc_in_use(crtc);
  4064. drm_helper_disable_unused_functions(dev);
  4065. }
  4066. /* Switch crtc and encoder back off if necessary */
  4067. if (crtc->enabled && dpms_mode != DRM_MODE_DPMS_ON) {
  4068. if (encoder->crtc == crtc)
  4069. encoder_funcs->dpms(encoder, dpms_mode);
  4070. crtc_funcs->dpms(crtc, dpms_mode);
  4071. }
  4072. }
  4073. /* Returns the clock of the currently programmed mode of the given pipe. */
  4074. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  4075. {
  4076. struct drm_i915_private *dev_priv = dev->dev_private;
  4077. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4078. int pipe = intel_crtc->pipe;
  4079. u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
  4080. u32 fp;
  4081. intel_clock_t clock;
  4082. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  4083. fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
  4084. else
  4085. fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
  4086. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  4087. if (IS_PINEVIEW(dev)) {
  4088. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  4089. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  4090. } else {
  4091. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  4092. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  4093. }
  4094. if (IS_I9XX(dev)) {
  4095. if (IS_PINEVIEW(dev))
  4096. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  4097. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  4098. else
  4099. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  4100. DPLL_FPA01_P1_POST_DIV_SHIFT);
  4101. switch (dpll & DPLL_MODE_MASK) {
  4102. case DPLLB_MODE_DAC_SERIAL:
  4103. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  4104. 5 : 10;
  4105. break;
  4106. case DPLLB_MODE_LVDS:
  4107. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  4108. 7 : 14;
  4109. break;
  4110. default:
  4111. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  4112. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  4113. return 0;
  4114. }
  4115. /* XXX: Handle the 100Mhz refclk */
  4116. intel_clock(dev, 96000, &clock);
  4117. } else {
  4118. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  4119. if (is_lvds) {
  4120. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  4121. DPLL_FPA01_P1_POST_DIV_SHIFT);
  4122. clock.p2 = 14;
  4123. if ((dpll & PLL_REF_INPUT_MASK) ==
  4124. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  4125. /* XXX: might not be 66MHz */
  4126. intel_clock(dev, 66000, &clock);
  4127. } else
  4128. intel_clock(dev, 48000, &clock);
  4129. } else {
  4130. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  4131. clock.p1 = 2;
  4132. else {
  4133. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  4134. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  4135. }
  4136. if (dpll & PLL_P2_DIVIDE_BY_4)
  4137. clock.p2 = 4;
  4138. else
  4139. clock.p2 = 2;
  4140. intel_clock(dev, 48000, &clock);
  4141. }
  4142. }
  4143. /* XXX: It would be nice to validate the clocks, but we can't reuse
  4144. * i830PllIsValid() because it relies on the xf86_config connector
  4145. * configuration being accurate, which it isn't necessarily.
  4146. */
  4147. return clock.dot;
  4148. }
  4149. /** Returns the currently programmed mode of the given pipe. */
  4150. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  4151. struct drm_crtc *crtc)
  4152. {
  4153. struct drm_i915_private *dev_priv = dev->dev_private;
  4154. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4155. int pipe = intel_crtc->pipe;
  4156. struct drm_display_mode *mode;
  4157. int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
  4158. int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
  4159. int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
  4160. int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
  4161. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  4162. if (!mode)
  4163. return NULL;
  4164. mode->clock = intel_crtc_clock_get(dev, crtc);
  4165. mode->hdisplay = (htot & 0xffff) + 1;
  4166. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  4167. mode->hsync_start = (hsync & 0xffff) + 1;
  4168. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  4169. mode->vdisplay = (vtot & 0xffff) + 1;
  4170. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  4171. mode->vsync_start = (vsync & 0xffff) + 1;
  4172. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  4173. drm_mode_set_name(mode);
  4174. drm_mode_set_crtcinfo(mode, 0);
  4175. return mode;
  4176. }
  4177. #define GPU_IDLE_TIMEOUT 500 /* ms */
  4178. /* When this timer fires, we've been idle for awhile */
  4179. static void intel_gpu_idle_timer(unsigned long arg)
  4180. {
  4181. struct drm_device *dev = (struct drm_device *)arg;
  4182. drm_i915_private_t *dev_priv = dev->dev_private;
  4183. DRM_DEBUG_DRIVER("idle timer fired, downclocking\n");
  4184. dev_priv->busy = false;
  4185. queue_work(dev_priv->wq, &dev_priv->idle_work);
  4186. }
  4187. #define CRTC_IDLE_TIMEOUT 1000 /* ms */
  4188. static void intel_crtc_idle_timer(unsigned long arg)
  4189. {
  4190. struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
  4191. struct drm_crtc *crtc = &intel_crtc->base;
  4192. drm_i915_private_t *dev_priv = crtc->dev->dev_private;
  4193. DRM_DEBUG_DRIVER("idle timer fired, downclocking\n");
  4194. intel_crtc->busy = false;
  4195. queue_work(dev_priv->wq, &dev_priv->idle_work);
  4196. }
  4197. static void intel_increase_pllclock(struct drm_crtc *crtc)
  4198. {
  4199. struct drm_device *dev = crtc->dev;
  4200. drm_i915_private_t *dev_priv = dev->dev_private;
  4201. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4202. int pipe = intel_crtc->pipe;
  4203. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  4204. int dpll = I915_READ(dpll_reg);
  4205. if (HAS_PCH_SPLIT(dev))
  4206. return;
  4207. if (!dev_priv->lvds_downclock_avail)
  4208. return;
  4209. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  4210. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  4211. /* Unlock panel regs */
  4212. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
  4213. PANEL_UNLOCK_REGS);
  4214. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  4215. I915_WRITE(dpll_reg, dpll);
  4216. dpll = I915_READ(dpll_reg);
  4217. intel_wait_for_vblank(dev, pipe);
  4218. dpll = I915_READ(dpll_reg);
  4219. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  4220. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  4221. /* ...and lock them again */
  4222. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  4223. }
  4224. /* Schedule downclock */
  4225. mod_timer(&intel_crtc->idle_timer, jiffies +
  4226. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4227. }
  4228. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  4229. {
  4230. struct drm_device *dev = crtc->dev;
  4231. drm_i915_private_t *dev_priv = dev->dev_private;
  4232. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4233. int pipe = intel_crtc->pipe;
  4234. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  4235. int dpll = I915_READ(dpll_reg);
  4236. if (HAS_PCH_SPLIT(dev))
  4237. return;
  4238. if (!dev_priv->lvds_downclock_avail)
  4239. return;
  4240. /*
  4241. * Since this is called by a timer, we should never get here in
  4242. * the manual case.
  4243. */
  4244. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  4245. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  4246. /* Unlock panel regs */
  4247. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
  4248. PANEL_UNLOCK_REGS);
  4249. dpll |= DISPLAY_RATE_SELECT_FPA1;
  4250. I915_WRITE(dpll_reg, dpll);
  4251. dpll = I915_READ(dpll_reg);
  4252. intel_wait_for_vblank(dev, pipe);
  4253. dpll = I915_READ(dpll_reg);
  4254. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  4255. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  4256. /* ...and lock them again */
  4257. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  4258. }
  4259. }
  4260. /**
  4261. * intel_idle_update - adjust clocks for idleness
  4262. * @work: work struct
  4263. *
  4264. * Either the GPU or display (or both) went idle. Check the busy status
  4265. * here and adjust the CRTC and GPU clocks as necessary.
  4266. */
  4267. static void intel_idle_update(struct work_struct *work)
  4268. {
  4269. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  4270. idle_work);
  4271. struct drm_device *dev = dev_priv->dev;
  4272. struct drm_crtc *crtc;
  4273. struct intel_crtc *intel_crtc;
  4274. int enabled = 0;
  4275. if (!i915_powersave)
  4276. return;
  4277. mutex_lock(&dev->struct_mutex);
  4278. i915_update_gfx_val(dev_priv);
  4279. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4280. /* Skip inactive CRTCs */
  4281. if (!crtc->fb)
  4282. continue;
  4283. enabled++;
  4284. intel_crtc = to_intel_crtc(crtc);
  4285. if (!intel_crtc->busy)
  4286. intel_decrease_pllclock(crtc);
  4287. }
  4288. if ((enabled == 1) && (IS_I945G(dev) || IS_I945GM(dev))) {
  4289. DRM_DEBUG_DRIVER("enable memory self refresh on 945\n");
  4290. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
  4291. }
  4292. mutex_unlock(&dev->struct_mutex);
  4293. }
  4294. /**
  4295. * intel_mark_busy - mark the GPU and possibly the display busy
  4296. * @dev: drm device
  4297. * @obj: object we're operating on
  4298. *
  4299. * Callers can use this function to indicate that the GPU is busy processing
  4300. * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
  4301. * buffer), we'll also mark the display as busy, so we know to increase its
  4302. * clock frequency.
  4303. */
  4304. void intel_mark_busy(struct drm_device *dev, struct drm_gem_object *obj)
  4305. {
  4306. drm_i915_private_t *dev_priv = dev->dev_private;
  4307. struct drm_crtc *crtc = NULL;
  4308. struct intel_framebuffer *intel_fb;
  4309. struct intel_crtc *intel_crtc;
  4310. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4311. return;
  4312. if (!dev_priv->busy) {
  4313. if (IS_I945G(dev) || IS_I945GM(dev)) {
  4314. u32 fw_blc_self;
  4315. DRM_DEBUG_DRIVER("disable memory self refresh on 945\n");
  4316. fw_blc_self = I915_READ(FW_BLC_SELF);
  4317. fw_blc_self &= ~FW_BLC_SELF_EN;
  4318. I915_WRITE(FW_BLC_SELF, fw_blc_self | FW_BLC_SELF_EN_MASK);
  4319. }
  4320. dev_priv->busy = true;
  4321. } else
  4322. mod_timer(&dev_priv->idle_timer, jiffies +
  4323. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  4324. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4325. if (!crtc->fb)
  4326. continue;
  4327. intel_crtc = to_intel_crtc(crtc);
  4328. intel_fb = to_intel_framebuffer(crtc->fb);
  4329. if (intel_fb->obj == obj) {
  4330. if (!intel_crtc->busy) {
  4331. if (IS_I945G(dev) || IS_I945GM(dev)) {
  4332. u32 fw_blc_self;
  4333. DRM_DEBUG_DRIVER("disable memory self refresh on 945\n");
  4334. fw_blc_self = I915_READ(FW_BLC_SELF);
  4335. fw_blc_self &= ~FW_BLC_SELF_EN;
  4336. I915_WRITE(FW_BLC_SELF, fw_blc_self | FW_BLC_SELF_EN_MASK);
  4337. }
  4338. /* Non-busy -> busy, upclock */
  4339. intel_increase_pllclock(crtc);
  4340. intel_crtc->busy = true;
  4341. } else {
  4342. /* Busy -> busy, put off timer */
  4343. mod_timer(&intel_crtc->idle_timer, jiffies +
  4344. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4345. }
  4346. }
  4347. }
  4348. }
  4349. static void intel_crtc_destroy(struct drm_crtc *crtc)
  4350. {
  4351. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4352. struct drm_device *dev = crtc->dev;
  4353. struct intel_unpin_work *work;
  4354. unsigned long flags;
  4355. spin_lock_irqsave(&dev->event_lock, flags);
  4356. work = intel_crtc->unpin_work;
  4357. intel_crtc->unpin_work = NULL;
  4358. spin_unlock_irqrestore(&dev->event_lock, flags);
  4359. if (work) {
  4360. cancel_work_sync(&work->work);
  4361. kfree(work);
  4362. }
  4363. drm_crtc_cleanup(crtc);
  4364. kfree(intel_crtc);
  4365. }
  4366. static void intel_unpin_work_fn(struct work_struct *__work)
  4367. {
  4368. struct intel_unpin_work *work =
  4369. container_of(__work, struct intel_unpin_work, work);
  4370. mutex_lock(&work->dev->struct_mutex);
  4371. i915_gem_object_unpin(work->old_fb_obj);
  4372. drm_gem_object_unreference(work->pending_flip_obj);
  4373. drm_gem_object_unreference(work->old_fb_obj);
  4374. mutex_unlock(&work->dev->struct_mutex);
  4375. kfree(work);
  4376. }
  4377. static void do_intel_finish_page_flip(struct drm_device *dev,
  4378. struct drm_crtc *crtc)
  4379. {
  4380. drm_i915_private_t *dev_priv = dev->dev_private;
  4381. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4382. struct intel_unpin_work *work;
  4383. struct drm_i915_gem_object *obj_priv;
  4384. struct drm_pending_vblank_event *e;
  4385. struct timeval now;
  4386. unsigned long flags;
  4387. /* Ignore early vblank irqs */
  4388. if (intel_crtc == NULL)
  4389. return;
  4390. spin_lock_irqsave(&dev->event_lock, flags);
  4391. work = intel_crtc->unpin_work;
  4392. if (work == NULL || !work->pending) {
  4393. spin_unlock_irqrestore(&dev->event_lock, flags);
  4394. return;
  4395. }
  4396. intel_crtc->unpin_work = NULL;
  4397. drm_vblank_put(dev, intel_crtc->pipe);
  4398. if (work->event) {
  4399. e = work->event;
  4400. do_gettimeofday(&now);
  4401. e->event.sequence = drm_vblank_count(dev, intel_crtc->pipe);
  4402. e->event.tv_sec = now.tv_sec;
  4403. e->event.tv_usec = now.tv_usec;
  4404. list_add_tail(&e->base.link,
  4405. &e->base.file_priv->event_list);
  4406. wake_up_interruptible(&e->base.file_priv->event_wait);
  4407. }
  4408. spin_unlock_irqrestore(&dev->event_lock, flags);
  4409. obj_priv = to_intel_bo(work->pending_flip_obj);
  4410. /* Initial scanout buffer will have a 0 pending flip count */
  4411. if ((atomic_read(&obj_priv->pending_flip) == 0) ||
  4412. atomic_dec_and_test(&obj_priv->pending_flip))
  4413. DRM_WAKEUP(&dev_priv->pending_flip_queue);
  4414. schedule_work(&work->work);
  4415. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  4416. }
  4417. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  4418. {
  4419. drm_i915_private_t *dev_priv = dev->dev_private;
  4420. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  4421. do_intel_finish_page_flip(dev, crtc);
  4422. }
  4423. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  4424. {
  4425. drm_i915_private_t *dev_priv = dev->dev_private;
  4426. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  4427. do_intel_finish_page_flip(dev, crtc);
  4428. }
  4429. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  4430. {
  4431. drm_i915_private_t *dev_priv = dev->dev_private;
  4432. struct intel_crtc *intel_crtc =
  4433. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  4434. unsigned long flags;
  4435. spin_lock_irqsave(&dev->event_lock, flags);
  4436. if (intel_crtc->unpin_work) {
  4437. if ((++intel_crtc->unpin_work->pending) > 1)
  4438. DRM_ERROR("Prepared flip multiple times\n");
  4439. } else {
  4440. DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
  4441. }
  4442. spin_unlock_irqrestore(&dev->event_lock, flags);
  4443. }
  4444. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  4445. struct drm_framebuffer *fb,
  4446. struct drm_pending_vblank_event *event)
  4447. {
  4448. struct drm_device *dev = crtc->dev;
  4449. struct drm_i915_private *dev_priv = dev->dev_private;
  4450. struct intel_framebuffer *intel_fb;
  4451. struct drm_i915_gem_object *obj_priv;
  4452. struct drm_gem_object *obj;
  4453. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4454. struct intel_unpin_work *work;
  4455. unsigned long flags, offset;
  4456. int pipe = intel_crtc->pipe;
  4457. u32 pf, pipesrc;
  4458. int ret;
  4459. work = kzalloc(sizeof *work, GFP_KERNEL);
  4460. if (work == NULL)
  4461. return -ENOMEM;
  4462. work->event = event;
  4463. work->dev = crtc->dev;
  4464. intel_fb = to_intel_framebuffer(crtc->fb);
  4465. work->old_fb_obj = intel_fb->obj;
  4466. INIT_WORK(&work->work, intel_unpin_work_fn);
  4467. /* We borrow the event spin lock for protecting unpin_work */
  4468. spin_lock_irqsave(&dev->event_lock, flags);
  4469. if (intel_crtc->unpin_work) {
  4470. spin_unlock_irqrestore(&dev->event_lock, flags);
  4471. kfree(work);
  4472. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  4473. return -EBUSY;
  4474. }
  4475. intel_crtc->unpin_work = work;
  4476. spin_unlock_irqrestore(&dev->event_lock, flags);
  4477. intel_fb = to_intel_framebuffer(fb);
  4478. obj = intel_fb->obj;
  4479. mutex_lock(&dev->struct_mutex);
  4480. ret = intel_pin_and_fence_fb_obj(dev, obj);
  4481. if (ret)
  4482. goto cleanup_work;
  4483. /* Reference the objects for the scheduled work. */
  4484. drm_gem_object_reference(work->old_fb_obj);
  4485. drm_gem_object_reference(obj);
  4486. crtc->fb = fb;
  4487. ret = i915_gem_object_flush_write_domain(obj);
  4488. if (ret)
  4489. goto cleanup_objs;
  4490. ret = drm_vblank_get(dev, intel_crtc->pipe);
  4491. if (ret)
  4492. goto cleanup_objs;
  4493. obj_priv = to_intel_bo(obj);
  4494. atomic_inc(&obj_priv->pending_flip);
  4495. work->pending_flip_obj = obj;
  4496. if (IS_GEN3(dev) || IS_GEN2(dev)) {
  4497. u32 flip_mask;
  4498. if (intel_crtc->plane)
  4499. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  4500. else
  4501. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  4502. BEGIN_LP_RING(2);
  4503. OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
  4504. OUT_RING(0);
  4505. ADVANCE_LP_RING();
  4506. }
  4507. work->enable_stall_check = true;
  4508. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  4509. offset = crtc->y * fb->pitch + crtc->x * fb->bits_per_pixel/8;
  4510. BEGIN_LP_RING(4);
  4511. switch(INTEL_INFO(dev)->gen) {
  4512. case 2:
  4513. OUT_RING(MI_DISPLAY_FLIP |
  4514. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4515. OUT_RING(fb->pitch);
  4516. OUT_RING(obj_priv->gtt_offset + offset);
  4517. OUT_RING(MI_NOOP);
  4518. break;
  4519. case 3:
  4520. OUT_RING(MI_DISPLAY_FLIP_I915 |
  4521. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4522. OUT_RING(fb->pitch);
  4523. OUT_RING(obj_priv->gtt_offset + offset);
  4524. OUT_RING(MI_NOOP);
  4525. break;
  4526. case 4:
  4527. case 5:
  4528. /* i965+ uses the linear or tiled offsets from the
  4529. * Display Registers (which do not change across a page-flip)
  4530. * so we need only reprogram the base address.
  4531. */
  4532. OUT_RING(MI_DISPLAY_FLIP |
  4533. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4534. OUT_RING(fb->pitch);
  4535. OUT_RING(obj_priv->gtt_offset | obj_priv->tiling_mode);
  4536. /* XXX Enabling the panel-fitter across page-flip is so far
  4537. * untested on non-native modes, so ignore it for now.
  4538. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  4539. */
  4540. pf = 0;
  4541. pipesrc = I915_READ(pipe == 0 ? PIPEASRC : PIPEBSRC) & 0x0fff0fff;
  4542. OUT_RING(pf | pipesrc);
  4543. break;
  4544. case 6:
  4545. OUT_RING(MI_DISPLAY_FLIP |
  4546. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4547. OUT_RING(fb->pitch | obj_priv->tiling_mode);
  4548. OUT_RING(obj_priv->gtt_offset);
  4549. pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  4550. pipesrc = I915_READ(pipe == 0 ? PIPEASRC : PIPEBSRC) & 0x0fff0fff;
  4551. OUT_RING(pf | pipesrc);
  4552. break;
  4553. }
  4554. ADVANCE_LP_RING();
  4555. mutex_unlock(&dev->struct_mutex);
  4556. trace_i915_flip_request(intel_crtc->plane, obj);
  4557. return 0;
  4558. cleanup_objs:
  4559. drm_gem_object_unreference(work->old_fb_obj);
  4560. drm_gem_object_unreference(obj);
  4561. cleanup_work:
  4562. mutex_unlock(&dev->struct_mutex);
  4563. spin_lock_irqsave(&dev->event_lock, flags);
  4564. intel_crtc->unpin_work = NULL;
  4565. spin_unlock_irqrestore(&dev->event_lock, flags);
  4566. kfree(work);
  4567. return ret;
  4568. }
  4569. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  4570. .dpms = intel_crtc_dpms,
  4571. .mode_fixup = intel_crtc_mode_fixup,
  4572. .mode_set = intel_crtc_mode_set,
  4573. .mode_set_base = intel_pipe_set_base,
  4574. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  4575. .load_lut = intel_crtc_load_lut,
  4576. };
  4577. static const struct drm_crtc_funcs intel_crtc_funcs = {
  4578. .cursor_set = intel_crtc_cursor_set,
  4579. .cursor_move = intel_crtc_cursor_move,
  4580. .gamma_set = intel_crtc_gamma_set,
  4581. .set_config = drm_crtc_helper_set_config,
  4582. .destroy = intel_crtc_destroy,
  4583. .page_flip = intel_crtc_page_flip,
  4584. };
  4585. static void intel_crtc_init(struct drm_device *dev, int pipe)
  4586. {
  4587. drm_i915_private_t *dev_priv = dev->dev_private;
  4588. struct intel_crtc *intel_crtc;
  4589. int i;
  4590. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  4591. if (intel_crtc == NULL)
  4592. return;
  4593. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  4594. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  4595. intel_crtc->pipe = pipe;
  4596. intel_crtc->plane = pipe;
  4597. for (i = 0; i < 256; i++) {
  4598. intel_crtc->lut_r[i] = i;
  4599. intel_crtc->lut_g[i] = i;
  4600. intel_crtc->lut_b[i] = i;
  4601. }
  4602. /* Swap pipes & planes for FBC on pre-965 */
  4603. intel_crtc->pipe = pipe;
  4604. intel_crtc->plane = pipe;
  4605. if (IS_MOBILE(dev) && (IS_I9XX(dev) && !IS_I965G(dev))) {
  4606. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  4607. intel_crtc->plane = ((pipe == 0) ? 1 : 0);
  4608. }
  4609. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  4610. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  4611. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  4612. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  4613. intel_crtc->cursor_addr = 0;
  4614. intel_crtc->dpms_mode = -1;
  4615. if (HAS_PCH_SPLIT(dev)) {
  4616. intel_helper_funcs.prepare = ironlake_crtc_prepare;
  4617. intel_helper_funcs.commit = ironlake_crtc_commit;
  4618. } else {
  4619. intel_helper_funcs.prepare = i9xx_crtc_prepare;
  4620. intel_helper_funcs.commit = i9xx_crtc_commit;
  4621. }
  4622. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  4623. intel_crtc->busy = false;
  4624. setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
  4625. (unsigned long)intel_crtc);
  4626. }
  4627. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  4628. struct drm_file *file_priv)
  4629. {
  4630. drm_i915_private_t *dev_priv = dev->dev_private;
  4631. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  4632. struct drm_mode_object *drmmode_obj;
  4633. struct intel_crtc *crtc;
  4634. if (!dev_priv) {
  4635. DRM_ERROR("called with no initialization\n");
  4636. return -EINVAL;
  4637. }
  4638. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  4639. DRM_MODE_OBJECT_CRTC);
  4640. if (!drmmode_obj) {
  4641. DRM_ERROR("no such CRTC id\n");
  4642. return -EINVAL;
  4643. }
  4644. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  4645. pipe_from_crtc_id->pipe = crtc->pipe;
  4646. return 0;
  4647. }
  4648. static int intel_encoder_clones(struct drm_device *dev, int type_mask)
  4649. {
  4650. struct intel_encoder *encoder;
  4651. int index_mask = 0;
  4652. int entry = 0;
  4653. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  4654. if (type_mask & encoder->clone_mask)
  4655. index_mask |= (1 << entry);
  4656. entry++;
  4657. }
  4658. return index_mask;
  4659. }
  4660. static void intel_setup_outputs(struct drm_device *dev)
  4661. {
  4662. struct drm_i915_private *dev_priv = dev->dev_private;
  4663. struct intel_encoder *encoder;
  4664. bool dpd_is_edp = false;
  4665. if (IS_MOBILE(dev) && !IS_I830(dev))
  4666. intel_lvds_init(dev);
  4667. if (HAS_PCH_SPLIT(dev)) {
  4668. dpd_is_edp = intel_dpd_is_edp(dev);
  4669. if (IS_MOBILE(dev) && (I915_READ(DP_A) & DP_DETECTED))
  4670. intel_dp_init(dev, DP_A);
  4671. if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  4672. intel_dp_init(dev, PCH_DP_D);
  4673. }
  4674. intel_crt_init(dev);
  4675. if (HAS_PCH_SPLIT(dev)) {
  4676. int found;
  4677. if (I915_READ(HDMIB) & PORT_DETECTED) {
  4678. /* PCH SDVOB multiplex with HDMIB */
  4679. found = intel_sdvo_init(dev, PCH_SDVOB);
  4680. if (!found)
  4681. intel_hdmi_init(dev, HDMIB);
  4682. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  4683. intel_dp_init(dev, PCH_DP_B);
  4684. }
  4685. if (I915_READ(HDMIC) & PORT_DETECTED)
  4686. intel_hdmi_init(dev, HDMIC);
  4687. if (I915_READ(HDMID) & PORT_DETECTED)
  4688. intel_hdmi_init(dev, HDMID);
  4689. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  4690. intel_dp_init(dev, PCH_DP_C);
  4691. if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  4692. intel_dp_init(dev, PCH_DP_D);
  4693. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  4694. bool found = false;
  4695. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  4696. DRM_DEBUG_KMS("probing SDVOB\n");
  4697. found = intel_sdvo_init(dev, SDVOB);
  4698. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  4699. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  4700. intel_hdmi_init(dev, SDVOB);
  4701. }
  4702. if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
  4703. DRM_DEBUG_KMS("probing DP_B\n");
  4704. intel_dp_init(dev, DP_B);
  4705. }
  4706. }
  4707. /* Before G4X SDVOC doesn't have its own detect register */
  4708. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  4709. DRM_DEBUG_KMS("probing SDVOC\n");
  4710. found = intel_sdvo_init(dev, SDVOC);
  4711. }
  4712. if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
  4713. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  4714. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  4715. intel_hdmi_init(dev, SDVOC);
  4716. }
  4717. if (SUPPORTS_INTEGRATED_DP(dev)) {
  4718. DRM_DEBUG_KMS("probing DP_C\n");
  4719. intel_dp_init(dev, DP_C);
  4720. }
  4721. }
  4722. if (SUPPORTS_INTEGRATED_DP(dev) &&
  4723. (I915_READ(DP_D) & DP_DETECTED)) {
  4724. DRM_DEBUG_KMS("probing DP_D\n");
  4725. intel_dp_init(dev, DP_D);
  4726. }
  4727. } else if (IS_GEN2(dev))
  4728. intel_dvo_init(dev);
  4729. if (SUPPORTS_TV(dev))
  4730. intel_tv_init(dev);
  4731. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  4732. encoder->base.possible_crtcs = encoder->crtc_mask;
  4733. encoder->base.possible_clones =
  4734. intel_encoder_clones(dev, encoder->clone_mask);
  4735. }
  4736. }
  4737. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  4738. {
  4739. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  4740. drm_framebuffer_cleanup(fb);
  4741. drm_gem_object_unreference_unlocked(intel_fb->obj);
  4742. kfree(intel_fb);
  4743. }
  4744. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  4745. struct drm_file *file_priv,
  4746. unsigned int *handle)
  4747. {
  4748. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  4749. struct drm_gem_object *object = intel_fb->obj;
  4750. return drm_gem_handle_create(file_priv, object, handle);
  4751. }
  4752. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  4753. .destroy = intel_user_framebuffer_destroy,
  4754. .create_handle = intel_user_framebuffer_create_handle,
  4755. };
  4756. int intel_framebuffer_init(struct drm_device *dev,
  4757. struct intel_framebuffer *intel_fb,
  4758. struct drm_mode_fb_cmd *mode_cmd,
  4759. struct drm_gem_object *obj)
  4760. {
  4761. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  4762. int ret;
  4763. if (obj_priv->tiling_mode == I915_TILING_Y)
  4764. return -EINVAL;
  4765. if (mode_cmd->pitch & 63)
  4766. return -EINVAL;
  4767. switch (mode_cmd->bpp) {
  4768. case 8:
  4769. case 16:
  4770. case 24:
  4771. case 32:
  4772. break;
  4773. default:
  4774. return -EINVAL;
  4775. }
  4776. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  4777. if (ret) {
  4778. DRM_ERROR("framebuffer init failed %d\n", ret);
  4779. return ret;
  4780. }
  4781. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  4782. intel_fb->obj = obj;
  4783. return 0;
  4784. }
  4785. static struct drm_framebuffer *
  4786. intel_user_framebuffer_create(struct drm_device *dev,
  4787. struct drm_file *filp,
  4788. struct drm_mode_fb_cmd *mode_cmd)
  4789. {
  4790. struct drm_gem_object *obj;
  4791. struct intel_framebuffer *intel_fb;
  4792. int ret;
  4793. obj = drm_gem_object_lookup(dev, filp, mode_cmd->handle);
  4794. if (!obj)
  4795. return ERR_PTR(-ENOENT);
  4796. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  4797. if (!intel_fb)
  4798. return ERR_PTR(-ENOMEM);
  4799. ret = intel_framebuffer_init(dev, intel_fb,
  4800. mode_cmd, obj);
  4801. if (ret) {
  4802. drm_gem_object_unreference_unlocked(obj);
  4803. kfree(intel_fb);
  4804. return ERR_PTR(ret);
  4805. }
  4806. return &intel_fb->base;
  4807. }
  4808. static const struct drm_mode_config_funcs intel_mode_funcs = {
  4809. .fb_create = intel_user_framebuffer_create,
  4810. .output_poll_changed = intel_fb_output_poll_changed,
  4811. };
  4812. static struct drm_gem_object *
  4813. intel_alloc_context_page(struct drm_device *dev)
  4814. {
  4815. struct drm_gem_object *ctx;
  4816. int ret;
  4817. ctx = i915_gem_alloc_object(dev, 4096);
  4818. if (!ctx) {
  4819. DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
  4820. return NULL;
  4821. }
  4822. mutex_lock(&dev->struct_mutex);
  4823. ret = i915_gem_object_pin(ctx, 4096);
  4824. if (ret) {
  4825. DRM_ERROR("failed to pin power context: %d\n", ret);
  4826. goto err_unref;
  4827. }
  4828. ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
  4829. if (ret) {
  4830. DRM_ERROR("failed to set-domain on power context: %d\n", ret);
  4831. goto err_unpin;
  4832. }
  4833. mutex_unlock(&dev->struct_mutex);
  4834. return ctx;
  4835. err_unpin:
  4836. i915_gem_object_unpin(ctx);
  4837. err_unref:
  4838. drm_gem_object_unreference(ctx);
  4839. mutex_unlock(&dev->struct_mutex);
  4840. return NULL;
  4841. }
  4842. bool ironlake_set_drps(struct drm_device *dev, u8 val)
  4843. {
  4844. struct drm_i915_private *dev_priv = dev->dev_private;
  4845. u16 rgvswctl;
  4846. rgvswctl = I915_READ16(MEMSWCTL);
  4847. if (rgvswctl & MEMCTL_CMD_STS) {
  4848. DRM_DEBUG("gpu busy, RCS change rejected\n");
  4849. return false; /* still busy with another command */
  4850. }
  4851. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  4852. (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  4853. I915_WRITE16(MEMSWCTL, rgvswctl);
  4854. POSTING_READ16(MEMSWCTL);
  4855. rgvswctl |= MEMCTL_CMD_STS;
  4856. I915_WRITE16(MEMSWCTL, rgvswctl);
  4857. return true;
  4858. }
  4859. void ironlake_enable_drps(struct drm_device *dev)
  4860. {
  4861. struct drm_i915_private *dev_priv = dev->dev_private;
  4862. u32 rgvmodectl = I915_READ(MEMMODECTL);
  4863. u8 fmax, fmin, fstart, vstart;
  4864. /* Enable temp reporting */
  4865. I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
  4866. I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
  4867. /* 100ms RC evaluation intervals */
  4868. I915_WRITE(RCUPEI, 100000);
  4869. I915_WRITE(RCDNEI, 100000);
  4870. /* Set max/min thresholds to 90ms and 80ms respectively */
  4871. I915_WRITE(RCBMAXAVG, 90000);
  4872. I915_WRITE(RCBMINAVG, 80000);
  4873. I915_WRITE(MEMIHYST, 1);
  4874. /* Set up min, max, and cur for interrupt handling */
  4875. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  4876. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  4877. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  4878. MEMMODE_FSTART_SHIFT;
  4879. fstart = fmax;
  4880. vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
  4881. PXVFREQ_PX_SHIFT;
  4882. dev_priv->fmax = fstart; /* IPS callback will increase this */
  4883. dev_priv->fstart = fstart;
  4884. dev_priv->max_delay = fmax;
  4885. dev_priv->min_delay = fmin;
  4886. dev_priv->cur_delay = fstart;
  4887. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n", fmax, fmin,
  4888. fstart);
  4889. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  4890. /*
  4891. * Interrupts will be enabled in ironlake_irq_postinstall
  4892. */
  4893. I915_WRITE(VIDSTART, vstart);
  4894. POSTING_READ(VIDSTART);
  4895. rgvmodectl |= MEMMODE_SWMODE_EN;
  4896. I915_WRITE(MEMMODECTL, rgvmodectl);
  4897. if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
  4898. DRM_ERROR("stuck trying to change perf mode\n");
  4899. msleep(1);
  4900. ironlake_set_drps(dev, fstart);
  4901. dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
  4902. I915_READ(0x112e0);
  4903. dev_priv->last_time1 = jiffies_to_msecs(jiffies);
  4904. dev_priv->last_count2 = I915_READ(0x112f4);
  4905. getrawmonotonic(&dev_priv->last_time2);
  4906. }
  4907. void ironlake_disable_drps(struct drm_device *dev)
  4908. {
  4909. struct drm_i915_private *dev_priv = dev->dev_private;
  4910. u16 rgvswctl = I915_READ16(MEMSWCTL);
  4911. /* Ack interrupts, disable EFC interrupt */
  4912. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  4913. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  4914. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  4915. I915_WRITE(DEIIR, DE_PCU_EVENT);
  4916. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  4917. /* Go back to the starting frequency */
  4918. ironlake_set_drps(dev, dev_priv->fstart);
  4919. msleep(1);
  4920. rgvswctl |= MEMCTL_CMD_STS;
  4921. I915_WRITE(MEMSWCTL, rgvswctl);
  4922. msleep(1);
  4923. }
  4924. static unsigned long intel_pxfreq(u32 vidfreq)
  4925. {
  4926. unsigned long freq;
  4927. int div = (vidfreq & 0x3f0000) >> 16;
  4928. int post = (vidfreq & 0x3000) >> 12;
  4929. int pre = (vidfreq & 0x7);
  4930. if (!pre)
  4931. return 0;
  4932. freq = ((div * 133333) / ((1<<post) * pre));
  4933. return freq;
  4934. }
  4935. void intel_init_emon(struct drm_device *dev)
  4936. {
  4937. struct drm_i915_private *dev_priv = dev->dev_private;
  4938. u32 lcfuse;
  4939. u8 pxw[16];
  4940. int i;
  4941. /* Disable to program */
  4942. I915_WRITE(ECR, 0);
  4943. POSTING_READ(ECR);
  4944. /* Program energy weights for various events */
  4945. I915_WRITE(SDEW, 0x15040d00);
  4946. I915_WRITE(CSIEW0, 0x007f0000);
  4947. I915_WRITE(CSIEW1, 0x1e220004);
  4948. I915_WRITE(CSIEW2, 0x04000004);
  4949. for (i = 0; i < 5; i++)
  4950. I915_WRITE(PEW + (i * 4), 0);
  4951. for (i = 0; i < 3; i++)
  4952. I915_WRITE(DEW + (i * 4), 0);
  4953. /* Program P-state weights to account for frequency power adjustment */
  4954. for (i = 0; i < 16; i++) {
  4955. u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
  4956. unsigned long freq = intel_pxfreq(pxvidfreq);
  4957. unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
  4958. PXVFREQ_PX_SHIFT;
  4959. unsigned long val;
  4960. val = vid * vid;
  4961. val *= (freq / 1000);
  4962. val *= 255;
  4963. val /= (127*127*900);
  4964. if (val > 0xff)
  4965. DRM_ERROR("bad pxval: %ld\n", val);
  4966. pxw[i] = val;
  4967. }
  4968. /* Render standby states get 0 weight */
  4969. pxw[14] = 0;
  4970. pxw[15] = 0;
  4971. for (i = 0; i < 4; i++) {
  4972. u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
  4973. (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
  4974. I915_WRITE(PXW + (i * 4), val);
  4975. }
  4976. /* Adjust magic regs to magic values (more experimental results) */
  4977. I915_WRITE(OGW0, 0);
  4978. I915_WRITE(OGW1, 0);
  4979. I915_WRITE(EG0, 0x00007f00);
  4980. I915_WRITE(EG1, 0x0000000e);
  4981. I915_WRITE(EG2, 0x000e0000);
  4982. I915_WRITE(EG3, 0x68000300);
  4983. I915_WRITE(EG4, 0x42000000);
  4984. I915_WRITE(EG5, 0x00140031);
  4985. I915_WRITE(EG6, 0);
  4986. I915_WRITE(EG7, 0);
  4987. for (i = 0; i < 8; i++)
  4988. I915_WRITE(PXWL + (i * 4), 0);
  4989. /* Enable PMON + select events */
  4990. I915_WRITE(ECR, 0x80000019);
  4991. lcfuse = I915_READ(LCFUSE02);
  4992. dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);
  4993. }
  4994. void intel_init_clock_gating(struct drm_device *dev)
  4995. {
  4996. struct drm_i915_private *dev_priv = dev->dev_private;
  4997. /*
  4998. * Disable clock gating reported to work incorrectly according to the
  4999. * specs, but enable as much else as we can.
  5000. */
  5001. if (HAS_PCH_SPLIT(dev)) {
  5002. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  5003. if (IS_IRONLAKE(dev)) {
  5004. /* Required for FBC */
  5005. dspclk_gate |= DPFDUNIT_CLOCK_GATE_DISABLE;
  5006. /* Required for CxSR */
  5007. dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
  5008. I915_WRITE(PCH_3DCGDIS0,
  5009. MARIUNIT_CLOCK_GATE_DISABLE |
  5010. SVSMUNIT_CLOCK_GATE_DISABLE);
  5011. }
  5012. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  5013. /*
  5014. * According to the spec the following bits should be set in
  5015. * order to enable memory self-refresh
  5016. * The bit 22/21 of 0x42004
  5017. * The bit 5 of 0x42020
  5018. * The bit 15 of 0x45000
  5019. */
  5020. if (IS_IRONLAKE(dev)) {
  5021. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5022. (I915_READ(ILK_DISPLAY_CHICKEN2) |
  5023. ILK_DPARB_GATE | ILK_VSDPFD_FULL));
  5024. I915_WRITE(ILK_DSPCLK_GATE,
  5025. (I915_READ(ILK_DSPCLK_GATE) |
  5026. ILK_DPARB_CLK_GATE));
  5027. I915_WRITE(DISP_ARB_CTL,
  5028. (I915_READ(DISP_ARB_CTL) |
  5029. DISP_FBC_WM_DIS));
  5030. I915_WRITE(WM3_LP_ILK, 0);
  5031. I915_WRITE(WM2_LP_ILK, 0);
  5032. I915_WRITE(WM1_LP_ILK, 0);
  5033. }
  5034. /*
  5035. * Based on the document from hardware guys the following bits
  5036. * should be set unconditionally in order to enable FBC.
  5037. * The bit 22 of 0x42000
  5038. * The bit 22 of 0x42004
  5039. * The bit 7,8,9 of 0x42020.
  5040. */
  5041. if (IS_IRONLAKE_M(dev)) {
  5042. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  5043. I915_READ(ILK_DISPLAY_CHICKEN1) |
  5044. ILK_FBCQ_DIS);
  5045. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5046. I915_READ(ILK_DISPLAY_CHICKEN2) |
  5047. ILK_DPARB_GATE);
  5048. I915_WRITE(ILK_DSPCLK_GATE,
  5049. I915_READ(ILK_DSPCLK_GATE) |
  5050. ILK_DPFC_DIS1 |
  5051. ILK_DPFC_DIS2 |
  5052. ILK_CLK_FBC);
  5053. }
  5054. return;
  5055. } else if (IS_G4X(dev)) {
  5056. uint32_t dspclk_gate;
  5057. I915_WRITE(RENCLK_GATE_D1, 0);
  5058. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  5059. GS_UNIT_CLOCK_GATE_DISABLE |
  5060. CL_UNIT_CLOCK_GATE_DISABLE);
  5061. I915_WRITE(RAMCLK_GATE_D, 0);
  5062. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  5063. OVRUNIT_CLOCK_GATE_DISABLE |
  5064. OVCUNIT_CLOCK_GATE_DISABLE;
  5065. if (IS_GM45(dev))
  5066. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  5067. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  5068. } else if (IS_I965GM(dev)) {
  5069. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  5070. I915_WRITE(RENCLK_GATE_D2, 0);
  5071. I915_WRITE(DSPCLK_GATE_D, 0);
  5072. I915_WRITE(RAMCLK_GATE_D, 0);
  5073. I915_WRITE16(DEUC, 0);
  5074. } else if (IS_I965G(dev)) {
  5075. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  5076. I965_RCC_CLOCK_GATE_DISABLE |
  5077. I965_RCPB_CLOCK_GATE_DISABLE |
  5078. I965_ISC_CLOCK_GATE_DISABLE |
  5079. I965_FBC_CLOCK_GATE_DISABLE);
  5080. I915_WRITE(RENCLK_GATE_D2, 0);
  5081. } else if (IS_I9XX(dev)) {
  5082. u32 dstate = I915_READ(D_STATE);
  5083. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  5084. DSTATE_DOT_CLOCK_GATING;
  5085. I915_WRITE(D_STATE, dstate);
  5086. } else if (IS_I85X(dev) || IS_I865G(dev)) {
  5087. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  5088. } else if (IS_I830(dev)) {
  5089. I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
  5090. }
  5091. /*
  5092. * GPU can automatically power down the render unit if given a page
  5093. * to save state.
  5094. */
  5095. if (IS_IRONLAKE_M(dev)) {
  5096. if (dev_priv->renderctx == NULL)
  5097. dev_priv->renderctx = intel_alloc_context_page(dev);
  5098. if (dev_priv->renderctx) {
  5099. struct drm_i915_gem_object *obj_priv;
  5100. obj_priv = to_intel_bo(dev_priv->renderctx);
  5101. if (obj_priv) {
  5102. BEGIN_LP_RING(4);
  5103. OUT_RING(MI_SET_CONTEXT);
  5104. OUT_RING(obj_priv->gtt_offset |
  5105. MI_MM_SPACE_GTT |
  5106. MI_SAVE_EXT_STATE_EN |
  5107. MI_RESTORE_EXT_STATE_EN |
  5108. MI_RESTORE_INHIBIT);
  5109. OUT_RING(MI_NOOP);
  5110. OUT_RING(MI_FLUSH);
  5111. ADVANCE_LP_RING();
  5112. }
  5113. } else
  5114. DRM_DEBUG_KMS("Failed to allocate render context."
  5115. "Disable RC6\n");
  5116. }
  5117. if (I915_HAS_RC6(dev) && drm_core_check_feature(dev, DRIVER_MODESET)) {
  5118. struct drm_i915_gem_object *obj_priv = NULL;
  5119. if (dev_priv->pwrctx) {
  5120. obj_priv = to_intel_bo(dev_priv->pwrctx);
  5121. } else {
  5122. struct drm_gem_object *pwrctx;
  5123. pwrctx = intel_alloc_context_page(dev);
  5124. if (pwrctx) {
  5125. dev_priv->pwrctx = pwrctx;
  5126. obj_priv = to_intel_bo(pwrctx);
  5127. }
  5128. }
  5129. if (obj_priv) {
  5130. I915_WRITE(PWRCTXA, obj_priv->gtt_offset | PWRCTX_EN);
  5131. I915_WRITE(MCHBAR_RENDER_STANDBY,
  5132. I915_READ(MCHBAR_RENDER_STANDBY) & ~RCX_SW_EXIT);
  5133. }
  5134. }
  5135. }
  5136. /* Set up chip specific display functions */
  5137. static void intel_init_display(struct drm_device *dev)
  5138. {
  5139. struct drm_i915_private *dev_priv = dev->dev_private;
  5140. /* We always want a DPMS function */
  5141. if (HAS_PCH_SPLIT(dev))
  5142. dev_priv->display.dpms = ironlake_crtc_dpms;
  5143. else
  5144. dev_priv->display.dpms = i9xx_crtc_dpms;
  5145. if (I915_HAS_FBC(dev)) {
  5146. if (IS_IRONLAKE_M(dev)) {
  5147. dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
  5148. dev_priv->display.enable_fbc = ironlake_enable_fbc;
  5149. dev_priv->display.disable_fbc = ironlake_disable_fbc;
  5150. } else if (IS_GM45(dev)) {
  5151. dev_priv->display.fbc_enabled = g4x_fbc_enabled;
  5152. dev_priv->display.enable_fbc = g4x_enable_fbc;
  5153. dev_priv->display.disable_fbc = g4x_disable_fbc;
  5154. } else if (IS_I965GM(dev)) {
  5155. dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
  5156. dev_priv->display.enable_fbc = i8xx_enable_fbc;
  5157. dev_priv->display.disable_fbc = i8xx_disable_fbc;
  5158. }
  5159. /* 855GM needs testing */
  5160. }
  5161. /* Returns the core display clock speed */
  5162. if (IS_I945G(dev) || (IS_G33(dev) && ! IS_PINEVIEW_M(dev)))
  5163. dev_priv->display.get_display_clock_speed =
  5164. i945_get_display_clock_speed;
  5165. else if (IS_I915G(dev))
  5166. dev_priv->display.get_display_clock_speed =
  5167. i915_get_display_clock_speed;
  5168. else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
  5169. dev_priv->display.get_display_clock_speed =
  5170. i9xx_misc_get_display_clock_speed;
  5171. else if (IS_I915GM(dev))
  5172. dev_priv->display.get_display_clock_speed =
  5173. i915gm_get_display_clock_speed;
  5174. else if (IS_I865G(dev))
  5175. dev_priv->display.get_display_clock_speed =
  5176. i865_get_display_clock_speed;
  5177. else if (IS_I85X(dev))
  5178. dev_priv->display.get_display_clock_speed =
  5179. i855_get_display_clock_speed;
  5180. else /* 852, 830 */
  5181. dev_priv->display.get_display_clock_speed =
  5182. i830_get_display_clock_speed;
  5183. /* For FIFO watermark updates */
  5184. if (HAS_PCH_SPLIT(dev)) {
  5185. if (IS_IRONLAKE(dev)) {
  5186. if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
  5187. dev_priv->display.update_wm = ironlake_update_wm;
  5188. else {
  5189. DRM_DEBUG_KMS("Failed to get proper latency. "
  5190. "Disable CxSR\n");
  5191. dev_priv->display.update_wm = NULL;
  5192. }
  5193. } else
  5194. dev_priv->display.update_wm = NULL;
  5195. } else if (IS_PINEVIEW(dev)) {
  5196. if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
  5197. dev_priv->is_ddr3,
  5198. dev_priv->fsb_freq,
  5199. dev_priv->mem_freq)) {
  5200. DRM_INFO("failed to find known CxSR latency "
  5201. "(found ddr%s fsb freq %d, mem freq %d), "
  5202. "disabling CxSR\n",
  5203. (dev_priv->is_ddr3 == 1) ? "3": "2",
  5204. dev_priv->fsb_freq, dev_priv->mem_freq);
  5205. /* Disable CxSR and never update its watermark again */
  5206. pineview_disable_cxsr(dev);
  5207. dev_priv->display.update_wm = NULL;
  5208. } else
  5209. dev_priv->display.update_wm = pineview_update_wm;
  5210. } else if (IS_G4X(dev))
  5211. dev_priv->display.update_wm = g4x_update_wm;
  5212. else if (IS_I965G(dev))
  5213. dev_priv->display.update_wm = i965_update_wm;
  5214. else if (IS_I9XX(dev)) {
  5215. dev_priv->display.update_wm = i9xx_update_wm;
  5216. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  5217. } else if (IS_I85X(dev)) {
  5218. dev_priv->display.update_wm = i9xx_update_wm;
  5219. dev_priv->display.get_fifo_size = i85x_get_fifo_size;
  5220. } else {
  5221. dev_priv->display.update_wm = i830_update_wm;
  5222. if (IS_845G(dev))
  5223. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  5224. else
  5225. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  5226. }
  5227. }
  5228. /*
  5229. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  5230. * resume, or other times. This quirk makes sure that's the case for
  5231. * affected systems.
  5232. */
  5233. static void quirk_pipea_force (struct drm_device *dev)
  5234. {
  5235. struct drm_i915_private *dev_priv = dev->dev_private;
  5236. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  5237. DRM_DEBUG_DRIVER("applying pipe a force quirk\n");
  5238. }
  5239. struct intel_quirk {
  5240. int device;
  5241. int subsystem_vendor;
  5242. int subsystem_device;
  5243. void (*hook)(struct drm_device *dev);
  5244. };
  5245. struct intel_quirk intel_quirks[] = {
  5246. /* HP Compaq 2730p needs pipe A force quirk (LP: #291555) */
  5247. { 0x2a42, 0x103c, 0x30eb, quirk_pipea_force },
  5248. /* HP Mini needs pipe A force quirk (LP: #322104) */
  5249. { 0x27ae,0x103c, 0x361a, quirk_pipea_force },
  5250. /* Thinkpad R31 needs pipe A force quirk */
  5251. { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
  5252. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  5253. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  5254. /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
  5255. { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
  5256. /* ThinkPad X40 needs pipe A force quirk */
  5257. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  5258. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  5259. /* 855 & before need to leave pipe A & dpll A up */
  5260. { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  5261. { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  5262. };
  5263. static void intel_init_quirks(struct drm_device *dev)
  5264. {
  5265. struct pci_dev *d = dev->pdev;
  5266. int i;
  5267. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  5268. struct intel_quirk *q = &intel_quirks[i];
  5269. if (d->device == q->device &&
  5270. (d->subsystem_vendor == q->subsystem_vendor ||
  5271. q->subsystem_vendor == PCI_ANY_ID) &&
  5272. (d->subsystem_device == q->subsystem_device ||
  5273. q->subsystem_device == PCI_ANY_ID))
  5274. q->hook(dev);
  5275. }
  5276. }
  5277. /* Disable the VGA plane that we never use */
  5278. static void i915_disable_vga(struct drm_device *dev)
  5279. {
  5280. struct drm_i915_private *dev_priv = dev->dev_private;
  5281. u8 sr1;
  5282. u32 vga_reg;
  5283. if (HAS_PCH_SPLIT(dev))
  5284. vga_reg = CPU_VGACNTRL;
  5285. else
  5286. vga_reg = VGACNTRL;
  5287. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  5288. outb(1, VGA_SR_INDEX);
  5289. sr1 = inb(VGA_SR_DATA);
  5290. outb(sr1 | 1<<5, VGA_SR_DATA);
  5291. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  5292. udelay(300);
  5293. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  5294. POSTING_READ(vga_reg);
  5295. }
  5296. void intel_modeset_init(struct drm_device *dev)
  5297. {
  5298. struct drm_i915_private *dev_priv = dev->dev_private;
  5299. int i;
  5300. drm_mode_config_init(dev);
  5301. dev->mode_config.min_width = 0;
  5302. dev->mode_config.min_height = 0;
  5303. dev->mode_config.funcs = (void *)&intel_mode_funcs;
  5304. intel_init_quirks(dev);
  5305. intel_init_display(dev);
  5306. if (IS_I965G(dev)) {
  5307. dev->mode_config.max_width = 8192;
  5308. dev->mode_config.max_height = 8192;
  5309. } else if (IS_I9XX(dev)) {
  5310. dev->mode_config.max_width = 4096;
  5311. dev->mode_config.max_height = 4096;
  5312. } else {
  5313. dev->mode_config.max_width = 2048;
  5314. dev->mode_config.max_height = 2048;
  5315. }
  5316. /* set memory base */
  5317. if (IS_I9XX(dev))
  5318. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 2);
  5319. else
  5320. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 0);
  5321. if (IS_MOBILE(dev) || IS_I9XX(dev))
  5322. dev_priv->num_pipe = 2;
  5323. else
  5324. dev_priv->num_pipe = 1;
  5325. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  5326. dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
  5327. for (i = 0; i < dev_priv->num_pipe; i++) {
  5328. intel_crtc_init(dev, i);
  5329. }
  5330. intel_setup_outputs(dev);
  5331. intel_init_clock_gating(dev);
  5332. /* Just disable it once at startup */
  5333. i915_disable_vga(dev);
  5334. if (IS_IRONLAKE_M(dev)) {
  5335. ironlake_enable_drps(dev);
  5336. intel_init_emon(dev);
  5337. }
  5338. INIT_WORK(&dev_priv->idle_work, intel_idle_update);
  5339. setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
  5340. (unsigned long)dev);
  5341. intel_setup_overlay(dev);
  5342. }
  5343. void intel_modeset_cleanup(struct drm_device *dev)
  5344. {
  5345. struct drm_i915_private *dev_priv = dev->dev_private;
  5346. struct drm_crtc *crtc;
  5347. struct intel_crtc *intel_crtc;
  5348. mutex_lock(&dev->struct_mutex);
  5349. drm_kms_helper_poll_fini(dev);
  5350. intel_fbdev_fini(dev);
  5351. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5352. /* Skip inactive CRTCs */
  5353. if (!crtc->fb)
  5354. continue;
  5355. intel_crtc = to_intel_crtc(crtc);
  5356. intel_increase_pllclock(crtc);
  5357. }
  5358. if (dev_priv->display.disable_fbc)
  5359. dev_priv->display.disable_fbc(dev);
  5360. if (dev_priv->renderctx) {
  5361. struct drm_i915_gem_object *obj_priv;
  5362. obj_priv = to_intel_bo(dev_priv->renderctx);
  5363. I915_WRITE(CCID, obj_priv->gtt_offset &~ CCID_EN);
  5364. I915_READ(CCID);
  5365. i915_gem_object_unpin(dev_priv->renderctx);
  5366. drm_gem_object_unreference(dev_priv->renderctx);
  5367. }
  5368. if (dev_priv->pwrctx) {
  5369. struct drm_i915_gem_object *obj_priv;
  5370. obj_priv = to_intel_bo(dev_priv->pwrctx);
  5371. I915_WRITE(PWRCTXA, obj_priv->gtt_offset &~ PWRCTX_EN);
  5372. I915_READ(PWRCTXA);
  5373. i915_gem_object_unpin(dev_priv->pwrctx);
  5374. drm_gem_object_unreference(dev_priv->pwrctx);
  5375. }
  5376. if (IS_IRONLAKE_M(dev))
  5377. ironlake_disable_drps(dev);
  5378. mutex_unlock(&dev->struct_mutex);
  5379. /* Disable the irq before mode object teardown, for the irq might
  5380. * enqueue unpin/hotplug work. */
  5381. drm_irq_uninstall(dev);
  5382. cancel_work_sync(&dev_priv->hotplug_work);
  5383. /* Shut off idle work before the crtcs get freed. */
  5384. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5385. intel_crtc = to_intel_crtc(crtc);
  5386. del_timer_sync(&intel_crtc->idle_timer);
  5387. }
  5388. del_timer_sync(&dev_priv->idle_timer);
  5389. cancel_work_sync(&dev_priv->idle_work);
  5390. drm_mode_config_cleanup(dev);
  5391. }
  5392. /*
  5393. * Return which encoder is currently attached for connector.
  5394. */
  5395. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  5396. {
  5397. return &intel_attached_encoder(connector)->base;
  5398. }
  5399. void intel_connector_attach_encoder(struct intel_connector *connector,
  5400. struct intel_encoder *encoder)
  5401. {
  5402. connector->encoder = encoder;
  5403. drm_mode_connector_attach_encoder(&connector->base,
  5404. &encoder->base);
  5405. }
  5406. /*
  5407. * set vga decode state - true == enable VGA decode
  5408. */
  5409. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  5410. {
  5411. struct drm_i915_private *dev_priv = dev->dev_private;
  5412. u16 gmch_ctrl;
  5413. pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
  5414. if (state)
  5415. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  5416. else
  5417. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  5418. pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
  5419. return 0;
  5420. }