intel_display.c 173 KB

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