intel_display.c 176 KB

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