intel_display.c 136 KB

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