intel_display.c 178 KB

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