intel_display.c 152 KB

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