intel_display.c 163 KB

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