intel_display.c 171 KB

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