intel_display.c 135 KB

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