intel_display.c 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190
  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/i2c.h>
  27. #include <linux/kernel.h>
  28. #include "drmP.h"
  29. #include "intel_drv.h"
  30. #include "i915_drm.h"
  31. #include "i915_drv.h"
  32. #include "intel_dp.h"
  33. #include "drm_crtc_helper.h"
  34. bool intel_pipe_has_type (struct drm_crtc *crtc, int type);
  35. static void intel_update_watermarks(struct drm_device *dev);
  36. typedef struct {
  37. /* given values */
  38. int n;
  39. int m1, m2;
  40. int p1, p2;
  41. /* derived values */
  42. int dot;
  43. int vco;
  44. int m;
  45. int p;
  46. } intel_clock_t;
  47. typedef struct {
  48. int min, max;
  49. } intel_range_t;
  50. typedef struct {
  51. int dot_limit;
  52. int p2_slow, p2_fast;
  53. } intel_p2_t;
  54. #define INTEL_P2_NUM 2
  55. typedef struct intel_limit intel_limit_t;
  56. struct intel_limit {
  57. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  58. intel_p2_t p2;
  59. bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
  60. int, int, intel_clock_t *);
  61. };
  62. #define I8XX_DOT_MIN 25000
  63. #define I8XX_DOT_MAX 350000
  64. #define I8XX_VCO_MIN 930000
  65. #define I8XX_VCO_MAX 1400000
  66. #define I8XX_N_MIN 3
  67. #define I8XX_N_MAX 16
  68. #define I8XX_M_MIN 96
  69. #define I8XX_M_MAX 140
  70. #define I8XX_M1_MIN 18
  71. #define I8XX_M1_MAX 26
  72. #define I8XX_M2_MIN 6
  73. #define I8XX_M2_MAX 16
  74. #define I8XX_P_MIN 4
  75. #define I8XX_P_MAX 128
  76. #define I8XX_P1_MIN 2
  77. #define I8XX_P1_MAX 33
  78. #define I8XX_P1_LVDS_MIN 1
  79. #define I8XX_P1_LVDS_MAX 6
  80. #define I8XX_P2_SLOW 4
  81. #define I8XX_P2_FAST 2
  82. #define I8XX_P2_LVDS_SLOW 14
  83. #define I8XX_P2_LVDS_FAST 14 /* No fast option */
  84. #define I8XX_P2_SLOW_LIMIT 165000
  85. #define I9XX_DOT_MIN 20000
  86. #define I9XX_DOT_MAX 400000
  87. #define I9XX_VCO_MIN 1400000
  88. #define I9XX_VCO_MAX 2800000
  89. #define IGD_VCO_MIN 1700000
  90. #define IGD_VCO_MAX 3500000
  91. #define I9XX_N_MIN 1
  92. #define I9XX_N_MAX 6
  93. /* IGD's Ncounter is a ring counter */
  94. #define IGD_N_MIN 3
  95. #define IGD_N_MAX 6
  96. #define I9XX_M_MIN 70
  97. #define I9XX_M_MAX 120
  98. #define IGD_M_MIN 2
  99. #define IGD_M_MAX 256
  100. #define I9XX_M1_MIN 10
  101. #define I9XX_M1_MAX 22
  102. #define I9XX_M2_MIN 5
  103. #define I9XX_M2_MAX 9
  104. /* IGD M1 is reserved, and must be 0 */
  105. #define IGD_M1_MIN 0
  106. #define IGD_M1_MAX 0
  107. #define IGD_M2_MIN 0
  108. #define IGD_M2_MAX 254
  109. #define I9XX_P_SDVO_DAC_MIN 5
  110. #define I9XX_P_SDVO_DAC_MAX 80
  111. #define I9XX_P_LVDS_MIN 7
  112. #define I9XX_P_LVDS_MAX 98
  113. #define IGD_P_LVDS_MIN 7
  114. #define IGD_P_LVDS_MAX 112
  115. #define I9XX_P1_MIN 1
  116. #define I9XX_P1_MAX 8
  117. #define I9XX_P2_SDVO_DAC_SLOW 10
  118. #define I9XX_P2_SDVO_DAC_FAST 5
  119. #define I9XX_P2_SDVO_DAC_SLOW_LIMIT 200000
  120. #define I9XX_P2_LVDS_SLOW 14
  121. #define I9XX_P2_LVDS_FAST 7
  122. #define I9XX_P2_LVDS_SLOW_LIMIT 112000
  123. /*The parameter is for SDVO on G4x platform*/
  124. #define G4X_DOT_SDVO_MIN 25000
  125. #define G4X_DOT_SDVO_MAX 270000
  126. #define G4X_VCO_MIN 1750000
  127. #define G4X_VCO_MAX 3500000
  128. #define G4X_N_SDVO_MIN 1
  129. #define G4X_N_SDVO_MAX 4
  130. #define G4X_M_SDVO_MIN 104
  131. #define G4X_M_SDVO_MAX 138
  132. #define G4X_M1_SDVO_MIN 17
  133. #define G4X_M1_SDVO_MAX 23
  134. #define G4X_M2_SDVO_MIN 5
  135. #define G4X_M2_SDVO_MAX 11
  136. #define G4X_P_SDVO_MIN 10
  137. #define G4X_P_SDVO_MAX 30
  138. #define G4X_P1_SDVO_MIN 1
  139. #define G4X_P1_SDVO_MAX 3
  140. #define G4X_P2_SDVO_SLOW 10
  141. #define G4X_P2_SDVO_FAST 10
  142. #define G4X_P2_SDVO_LIMIT 270000
  143. /*The parameter is for HDMI_DAC on G4x platform*/
  144. #define G4X_DOT_HDMI_DAC_MIN 22000
  145. #define G4X_DOT_HDMI_DAC_MAX 400000
  146. #define G4X_N_HDMI_DAC_MIN 1
  147. #define G4X_N_HDMI_DAC_MAX 4
  148. #define G4X_M_HDMI_DAC_MIN 104
  149. #define G4X_M_HDMI_DAC_MAX 138
  150. #define G4X_M1_HDMI_DAC_MIN 16
  151. #define G4X_M1_HDMI_DAC_MAX 23
  152. #define G4X_M2_HDMI_DAC_MIN 5
  153. #define G4X_M2_HDMI_DAC_MAX 11
  154. #define G4X_P_HDMI_DAC_MIN 5
  155. #define G4X_P_HDMI_DAC_MAX 80
  156. #define G4X_P1_HDMI_DAC_MIN 1
  157. #define G4X_P1_HDMI_DAC_MAX 8
  158. #define G4X_P2_HDMI_DAC_SLOW 10
  159. #define G4X_P2_HDMI_DAC_FAST 5
  160. #define G4X_P2_HDMI_DAC_LIMIT 165000
  161. /*The parameter is for SINGLE_CHANNEL_LVDS on G4x platform*/
  162. #define G4X_DOT_SINGLE_CHANNEL_LVDS_MIN 20000
  163. #define G4X_DOT_SINGLE_CHANNEL_LVDS_MAX 115000
  164. #define G4X_N_SINGLE_CHANNEL_LVDS_MIN 1
  165. #define G4X_N_SINGLE_CHANNEL_LVDS_MAX 3
  166. #define G4X_M_SINGLE_CHANNEL_LVDS_MIN 104
  167. #define G4X_M_SINGLE_CHANNEL_LVDS_MAX 138
  168. #define G4X_M1_SINGLE_CHANNEL_LVDS_MIN 17
  169. #define G4X_M1_SINGLE_CHANNEL_LVDS_MAX 23
  170. #define G4X_M2_SINGLE_CHANNEL_LVDS_MIN 5
  171. #define G4X_M2_SINGLE_CHANNEL_LVDS_MAX 11
  172. #define G4X_P_SINGLE_CHANNEL_LVDS_MIN 28
  173. #define G4X_P_SINGLE_CHANNEL_LVDS_MAX 112
  174. #define G4X_P1_SINGLE_CHANNEL_LVDS_MIN 2
  175. #define G4X_P1_SINGLE_CHANNEL_LVDS_MAX 8
  176. #define G4X_P2_SINGLE_CHANNEL_LVDS_SLOW 14
  177. #define G4X_P2_SINGLE_CHANNEL_LVDS_FAST 14
  178. #define G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT 0
  179. /*The parameter is for DUAL_CHANNEL_LVDS on G4x platform*/
  180. #define G4X_DOT_DUAL_CHANNEL_LVDS_MIN 80000
  181. #define G4X_DOT_DUAL_CHANNEL_LVDS_MAX 224000
  182. #define G4X_N_DUAL_CHANNEL_LVDS_MIN 1
  183. #define G4X_N_DUAL_CHANNEL_LVDS_MAX 3
  184. #define G4X_M_DUAL_CHANNEL_LVDS_MIN 104
  185. #define G4X_M_DUAL_CHANNEL_LVDS_MAX 138
  186. #define G4X_M1_DUAL_CHANNEL_LVDS_MIN 17
  187. #define G4X_M1_DUAL_CHANNEL_LVDS_MAX 23
  188. #define G4X_M2_DUAL_CHANNEL_LVDS_MIN 5
  189. #define G4X_M2_DUAL_CHANNEL_LVDS_MAX 11
  190. #define G4X_P_DUAL_CHANNEL_LVDS_MIN 14
  191. #define G4X_P_DUAL_CHANNEL_LVDS_MAX 42
  192. #define G4X_P1_DUAL_CHANNEL_LVDS_MIN 2
  193. #define G4X_P1_DUAL_CHANNEL_LVDS_MAX 6
  194. #define G4X_P2_DUAL_CHANNEL_LVDS_SLOW 7
  195. #define G4X_P2_DUAL_CHANNEL_LVDS_FAST 7
  196. #define G4X_P2_DUAL_CHANNEL_LVDS_LIMIT 0
  197. /*The parameter is for DISPLAY PORT on G4x platform*/
  198. #define G4X_DOT_DISPLAY_PORT_MIN 161670
  199. #define G4X_DOT_DISPLAY_PORT_MAX 227000
  200. #define G4X_N_DISPLAY_PORT_MIN 1
  201. #define G4X_N_DISPLAY_PORT_MAX 2
  202. #define G4X_M_DISPLAY_PORT_MIN 97
  203. #define G4X_M_DISPLAY_PORT_MAX 108
  204. #define G4X_M1_DISPLAY_PORT_MIN 0x10
  205. #define G4X_M1_DISPLAY_PORT_MAX 0x12
  206. #define G4X_M2_DISPLAY_PORT_MIN 0x05
  207. #define G4X_M2_DISPLAY_PORT_MAX 0x06
  208. #define G4X_P_DISPLAY_PORT_MIN 10
  209. #define G4X_P_DISPLAY_PORT_MAX 20
  210. #define G4X_P1_DISPLAY_PORT_MIN 1
  211. #define G4X_P1_DISPLAY_PORT_MAX 2
  212. #define G4X_P2_DISPLAY_PORT_SLOW 10
  213. #define G4X_P2_DISPLAY_PORT_FAST 10
  214. #define G4X_P2_DISPLAY_PORT_LIMIT 0
  215. /* IGDNG */
  216. /* as we calculate clock using (register_value + 2) for
  217. N/M1/M2, so here the range value for them is (actual_value-2).
  218. */
  219. #define IGDNG_DOT_MIN 25000
  220. #define IGDNG_DOT_MAX 350000
  221. #define IGDNG_VCO_MIN 1760000
  222. #define IGDNG_VCO_MAX 3510000
  223. #define IGDNG_N_MIN 1
  224. #define IGDNG_N_MAX 5
  225. #define IGDNG_M_MIN 79
  226. #define IGDNG_M_MAX 118
  227. #define IGDNG_M1_MIN 12
  228. #define IGDNG_M1_MAX 23
  229. #define IGDNG_M2_MIN 5
  230. #define IGDNG_M2_MAX 9
  231. #define IGDNG_P_SDVO_DAC_MIN 5
  232. #define IGDNG_P_SDVO_DAC_MAX 80
  233. #define IGDNG_P_LVDS_MIN 28
  234. #define IGDNG_P_LVDS_MAX 112
  235. #define IGDNG_P1_MIN 1
  236. #define IGDNG_P1_MAX 8
  237. #define IGDNG_P2_SDVO_DAC_SLOW 10
  238. #define IGDNG_P2_SDVO_DAC_FAST 5
  239. #define IGDNG_P2_LVDS_SLOW 14 /* single channel */
  240. #define IGDNG_P2_LVDS_FAST 7 /* double channel */
  241. #define IGDNG_P2_DOT_LIMIT 225000 /* 225Mhz */
  242. static bool
  243. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  244. int target, int refclk, intel_clock_t *best_clock);
  245. static bool
  246. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  247. int target, int refclk, intel_clock_t *best_clock);
  248. static bool
  249. intel_igdng_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  250. int target, int refclk, intel_clock_t *best_clock);
  251. static bool
  252. intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
  253. int target, int refclk, intel_clock_t *best_clock);
  254. static const intel_limit_t intel_limits_i8xx_dvo = {
  255. .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
  256. .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
  257. .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
  258. .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
  259. .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
  260. .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
  261. .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
  262. .p1 = { .min = I8XX_P1_MIN, .max = I8XX_P1_MAX },
  263. .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
  264. .p2_slow = I8XX_P2_SLOW, .p2_fast = I8XX_P2_FAST },
  265. .find_pll = intel_find_best_PLL,
  266. };
  267. static const intel_limit_t intel_limits_i8xx_lvds = {
  268. .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
  269. .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
  270. .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
  271. .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
  272. .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
  273. .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
  274. .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
  275. .p1 = { .min = I8XX_P1_LVDS_MIN, .max = I8XX_P1_LVDS_MAX },
  276. .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
  277. .p2_slow = I8XX_P2_LVDS_SLOW, .p2_fast = I8XX_P2_LVDS_FAST },
  278. .find_pll = intel_find_best_PLL,
  279. };
  280. static const intel_limit_t intel_limits_i9xx_sdvo = {
  281. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  282. .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
  283. .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
  284. .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
  285. .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
  286. .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
  287. .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
  288. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  289. .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
  290. .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
  291. .find_pll = intel_find_best_PLL,
  292. };
  293. static const intel_limit_t intel_limits_i9xx_lvds = {
  294. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  295. .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
  296. .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
  297. .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
  298. .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
  299. .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
  300. .p = { .min = I9XX_P_LVDS_MIN, .max = I9XX_P_LVDS_MAX },
  301. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  302. /* The single-channel range is 25-112Mhz, and dual-channel
  303. * is 80-224Mhz. Prefer single channel as much as possible.
  304. */
  305. .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
  306. .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_FAST },
  307. .find_pll = intel_find_best_PLL,
  308. };
  309. /* below parameter and function is for G4X Chipset Family*/
  310. static const intel_limit_t intel_limits_g4x_sdvo = {
  311. .dot = { .min = G4X_DOT_SDVO_MIN, .max = G4X_DOT_SDVO_MAX },
  312. .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
  313. .n = { .min = G4X_N_SDVO_MIN, .max = G4X_N_SDVO_MAX },
  314. .m = { .min = G4X_M_SDVO_MIN, .max = G4X_M_SDVO_MAX },
  315. .m1 = { .min = G4X_M1_SDVO_MIN, .max = G4X_M1_SDVO_MAX },
  316. .m2 = { .min = G4X_M2_SDVO_MIN, .max = G4X_M2_SDVO_MAX },
  317. .p = { .min = G4X_P_SDVO_MIN, .max = G4X_P_SDVO_MAX },
  318. .p1 = { .min = G4X_P1_SDVO_MIN, .max = G4X_P1_SDVO_MAX},
  319. .p2 = { .dot_limit = G4X_P2_SDVO_LIMIT,
  320. .p2_slow = G4X_P2_SDVO_SLOW,
  321. .p2_fast = G4X_P2_SDVO_FAST
  322. },
  323. .find_pll = intel_g4x_find_best_PLL,
  324. };
  325. static const intel_limit_t intel_limits_g4x_hdmi = {
  326. .dot = { .min = G4X_DOT_HDMI_DAC_MIN, .max = G4X_DOT_HDMI_DAC_MAX },
  327. .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
  328. .n = { .min = G4X_N_HDMI_DAC_MIN, .max = G4X_N_HDMI_DAC_MAX },
  329. .m = { .min = G4X_M_HDMI_DAC_MIN, .max = G4X_M_HDMI_DAC_MAX },
  330. .m1 = { .min = G4X_M1_HDMI_DAC_MIN, .max = G4X_M1_HDMI_DAC_MAX },
  331. .m2 = { .min = G4X_M2_HDMI_DAC_MIN, .max = G4X_M2_HDMI_DAC_MAX },
  332. .p = { .min = G4X_P_HDMI_DAC_MIN, .max = G4X_P_HDMI_DAC_MAX },
  333. .p1 = { .min = G4X_P1_HDMI_DAC_MIN, .max = G4X_P1_HDMI_DAC_MAX},
  334. .p2 = { .dot_limit = G4X_P2_HDMI_DAC_LIMIT,
  335. .p2_slow = G4X_P2_HDMI_DAC_SLOW,
  336. .p2_fast = G4X_P2_HDMI_DAC_FAST
  337. },
  338. .find_pll = intel_g4x_find_best_PLL,
  339. };
  340. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  341. .dot = { .min = G4X_DOT_SINGLE_CHANNEL_LVDS_MIN,
  342. .max = G4X_DOT_SINGLE_CHANNEL_LVDS_MAX },
  343. .vco = { .min = G4X_VCO_MIN,
  344. .max = G4X_VCO_MAX },
  345. .n = { .min = G4X_N_SINGLE_CHANNEL_LVDS_MIN,
  346. .max = G4X_N_SINGLE_CHANNEL_LVDS_MAX },
  347. .m = { .min = G4X_M_SINGLE_CHANNEL_LVDS_MIN,
  348. .max = G4X_M_SINGLE_CHANNEL_LVDS_MAX },
  349. .m1 = { .min = G4X_M1_SINGLE_CHANNEL_LVDS_MIN,
  350. .max = G4X_M1_SINGLE_CHANNEL_LVDS_MAX },
  351. .m2 = { .min = G4X_M2_SINGLE_CHANNEL_LVDS_MIN,
  352. .max = G4X_M2_SINGLE_CHANNEL_LVDS_MAX },
  353. .p = { .min = G4X_P_SINGLE_CHANNEL_LVDS_MIN,
  354. .max = G4X_P_SINGLE_CHANNEL_LVDS_MAX },
  355. .p1 = { .min = G4X_P1_SINGLE_CHANNEL_LVDS_MIN,
  356. .max = G4X_P1_SINGLE_CHANNEL_LVDS_MAX },
  357. .p2 = { .dot_limit = G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT,
  358. .p2_slow = G4X_P2_SINGLE_CHANNEL_LVDS_SLOW,
  359. .p2_fast = G4X_P2_SINGLE_CHANNEL_LVDS_FAST
  360. },
  361. .find_pll = intel_g4x_find_best_PLL,
  362. };
  363. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  364. .dot = { .min = G4X_DOT_DUAL_CHANNEL_LVDS_MIN,
  365. .max = G4X_DOT_DUAL_CHANNEL_LVDS_MAX },
  366. .vco = { .min = G4X_VCO_MIN,
  367. .max = G4X_VCO_MAX },
  368. .n = { .min = G4X_N_DUAL_CHANNEL_LVDS_MIN,
  369. .max = G4X_N_DUAL_CHANNEL_LVDS_MAX },
  370. .m = { .min = G4X_M_DUAL_CHANNEL_LVDS_MIN,
  371. .max = G4X_M_DUAL_CHANNEL_LVDS_MAX },
  372. .m1 = { .min = G4X_M1_DUAL_CHANNEL_LVDS_MIN,
  373. .max = G4X_M1_DUAL_CHANNEL_LVDS_MAX },
  374. .m2 = { .min = G4X_M2_DUAL_CHANNEL_LVDS_MIN,
  375. .max = G4X_M2_DUAL_CHANNEL_LVDS_MAX },
  376. .p = { .min = G4X_P_DUAL_CHANNEL_LVDS_MIN,
  377. .max = G4X_P_DUAL_CHANNEL_LVDS_MAX },
  378. .p1 = { .min = G4X_P1_DUAL_CHANNEL_LVDS_MIN,
  379. .max = G4X_P1_DUAL_CHANNEL_LVDS_MAX },
  380. .p2 = { .dot_limit = G4X_P2_DUAL_CHANNEL_LVDS_LIMIT,
  381. .p2_slow = G4X_P2_DUAL_CHANNEL_LVDS_SLOW,
  382. .p2_fast = G4X_P2_DUAL_CHANNEL_LVDS_FAST
  383. },
  384. .find_pll = intel_g4x_find_best_PLL,
  385. };
  386. static const intel_limit_t intel_limits_g4x_display_port = {
  387. .dot = { .min = G4X_DOT_DISPLAY_PORT_MIN,
  388. .max = G4X_DOT_DISPLAY_PORT_MAX },
  389. .vco = { .min = G4X_VCO_MIN,
  390. .max = G4X_VCO_MAX},
  391. .n = { .min = G4X_N_DISPLAY_PORT_MIN,
  392. .max = G4X_N_DISPLAY_PORT_MAX },
  393. .m = { .min = G4X_M_DISPLAY_PORT_MIN,
  394. .max = G4X_M_DISPLAY_PORT_MAX },
  395. .m1 = { .min = G4X_M1_DISPLAY_PORT_MIN,
  396. .max = G4X_M1_DISPLAY_PORT_MAX },
  397. .m2 = { .min = G4X_M2_DISPLAY_PORT_MIN,
  398. .max = G4X_M2_DISPLAY_PORT_MAX },
  399. .p = { .min = G4X_P_DISPLAY_PORT_MIN,
  400. .max = G4X_P_DISPLAY_PORT_MAX },
  401. .p1 = { .min = G4X_P1_DISPLAY_PORT_MIN,
  402. .max = G4X_P1_DISPLAY_PORT_MAX},
  403. .p2 = { .dot_limit = G4X_P2_DISPLAY_PORT_LIMIT,
  404. .p2_slow = G4X_P2_DISPLAY_PORT_SLOW,
  405. .p2_fast = G4X_P2_DISPLAY_PORT_FAST },
  406. .find_pll = intel_find_pll_g4x_dp,
  407. };
  408. static const intel_limit_t intel_limits_igd_sdvo = {
  409. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX},
  410. .vco = { .min = IGD_VCO_MIN, .max = IGD_VCO_MAX },
  411. .n = { .min = IGD_N_MIN, .max = IGD_N_MAX },
  412. .m = { .min = IGD_M_MIN, .max = IGD_M_MAX },
  413. .m1 = { .min = IGD_M1_MIN, .max = IGD_M1_MAX },
  414. .m2 = { .min = IGD_M2_MIN, .max = IGD_M2_MAX },
  415. .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
  416. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  417. .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
  418. .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
  419. .find_pll = intel_find_best_PLL,
  420. };
  421. static const intel_limit_t intel_limits_igd_lvds = {
  422. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  423. .vco = { .min = IGD_VCO_MIN, .max = IGD_VCO_MAX },
  424. .n = { .min = IGD_N_MIN, .max = IGD_N_MAX },
  425. .m = { .min = IGD_M_MIN, .max = IGD_M_MAX },
  426. .m1 = { .min = IGD_M1_MIN, .max = IGD_M1_MAX },
  427. .m2 = { .min = IGD_M2_MIN, .max = IGD_M2_MAX },
  428. .p = { .min = IGD_P_LVDS_MIN, .max = IGD_P_LVDS_MAX },
  429. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  430. /* IGD only supports single-channel mode. */
  431. .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
  432. .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_SLOW },
  433. .find_pll = intel_find_best_PLL,
  434. };
  435. static const intel_limit_t intel_limits_igdng_sdvo = {
  436. .dot = { .min = IGDNG_DOT_MIN, .max = IGDNG_DOT_MAX },
  437. .vco = { .min = IGDNG_VCO_MIN, .max = IGDNG_VCO_MAX },
  438. .n = { .min = IGDNG_N_MIN, .max = IGDNG_N_MAX },
  439. .m = { .min = IGDNG_M_MIN, .max = IGDNG_M_MAX },
  440. .m1 = { .min = IGDNG_M1_MIN, .max = IGDNG_M1_MAX },
  441. .m2 = { .min = IGDNG_M2_MIN, .max = IGDNG_M2_MAX },
  442. .p = { .min = IGDNG_P_SDVO_DAC_MIN, .max = IGDNG_P_SDVO_DAC_MAX },
  443. .p1 = { .min = IGDNG_P1_MIN, .max = IGDNG_P1_MAX },
  444. .p2 = { .dot_limit = IGDNG_P2_DOT_LIMIT,
  445. .p2_slow = IGDNG_P2_SDVO_DAC_SLOW,
  446. .p2_fast = IGDNG_P2_SDVO_DAC_FAST },
  447. .find_pll = intel_igdng_find_best_PLL,
  448. };
  449. static const intel_limit_t intel_limits_igdng_lvds = {
  450. .dot = { .min = IGDNG_DOT_MIN, .max = IGDNG_DOT_MAX },
  451. .vco = { .min = IGDNG_VCO_MIN, .max = IGDNG_VCO_MAX },
  452. .n = { .min = IGDNG_N_MIN, .max = IGDNG_N_MAX },
  453. .m = { .min = IGDNG_M_MIN, .max = IGDNG_M_MAX },
  454. .m1 = { .min = IGDNG_M1_MIN, .max = IGDNG_M1_MAX },
  455. .m2 = { .min = IGDNG_M2_MIN, .max = IGDNG_M2_MAX },
  456. .p = { .min = IGDNG_P_LVDS_MIN, .max = IGDNG_P_LVDS_MAX },
  457. .p1 = { .min = IGDNG_P1_MIN, .max = IGDNG_P1_MAX },
  458. .p2 = { .dot_limit = IGDNG_P2_DOT_LIMIT,
  459. .p2_slow = IGDNG_P2_LVDS_SLOW,
  460. .p2_fast = IGDNG_P2_LVDS_FAST },
  461. .find_pll = intel_igdng_find_best_PLL,
  462. };
  463. static const intel_limit_t *intel_igdng_limit(struct drm_crtc *crtc)
  464. {
  465. const intel_limit_t *limit;
  466. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  467. limit = &intel_limits_igdng_lvds;
  468. else
  469. limit = &intel_limits_igdng_sdvo;
  470. return limit;
  471. }
  472. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  473. {
  474. struct drm_device *dev = crtc->dev;
  475. struct drm_i915_private *dev_priv = dev->dev_private;
  476. const intel_limit_t *limit;
  477. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  478. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  479. LVDS_CLKB_POWER_UP)
  480. /* LVDS with dual channel */
  481. limit = &intel_limits_g4x_dual_channel_lvds;
  482. else
  483. /* LVDS with dual channel */
  484. limit = &intel_limits_g4x_single_channel_lvds;
  485. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  486. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  487. limit = &intel_limits_g4x_hdmi;
  488. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  489. limit = &intel_limits_g4x_sdvo;
  490. } else if (intel_pipe_has_type (crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  491. limit = &intel_limits_g4x_display_port;
  492. } else /* The option is for other outputs */
  493. limit = &intel_limits_i9xx_sdvo;
  494. return limit;
  495. }
  496. static const intel_limit_t *intel_limit(struct drm_crtc *crtc)
  497. {
  498. struct drm_device *dev = crtc->dev;
  499. const intel_limit_t *limit;
  500. if (IS_IGDNG(dev))
  501. limit = intel_igdng_limit(crtc);
  502. else if (IS_G4X(dev)) {
  503. limit = intel_g4x_limit(crtc);
  504. } else if (IS_I9XX(dev) && !IS_IGD(dev)) {
  505. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  506. limit = &intel_limits_i9xx_lvds;
  507. else
  508. limit = &intel_limits_i9xx_sdvo;
  509. } else if (IS_IGD(dev)) {
  510. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  511. limit = &intel_limits_igd_lvds;
  512. else
  513. limit = &intel_limits_igd_sdvo;
  514. } else {
  515. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  516. limit = &intel_limits_i8xx_lvds;
  517. else
  518. limit = &intel_limits_i8xx_dvo;
  519. }
  520. return limit;
  521. }
  522. /* m1 is reserved as 0 in IGD, n is a ring counter */
  523. static void igd_clock(int refclk, intel_clock_t *clock)
  524. {
  525. clock->m = clock->m2 + 2;
  526. clock->p = clock->p1 * clock->p2;
  527. clock->vco = refclk * clock->m / clock->n;
  528. clock->dot = clock->vco / clock->p;
  529. }
  530. static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
  531. {
  532. if (IS_IGD(dev)) {
  533. igd_clock(refclk, clock);
  534. return;
  535. }
  536. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  537. clock->p = clock->p1 * clock->p2;
  538. clock->vco = refclk * clock->m / (clock->n + 2);
  539. clock->dot = clock->vco / clock->p;
  540. }
  541. /**
  542. * Returns whether any output on the specified pipe is of the specified type
  543. */
  544. bool intel_pipe_has_type (struct drm_crtc *crtc, int type)
  545. {
  546. struct drm_device *dev = crtc->dev;
  547. struct drm_mode_config *mode_config = &dev->mode_config;
  548. struct drm_connector *l_entry;
  549. list_for_each_entry(l_entry, &mode_config->connector_list, head) {
  550. if (l_entry->encoder &&
  551. l_entry->encoder->crtc == crtc) {
  552. struct intel_output *intel_output = to_intel_output(l_entry);
  553. if (intel_output->type == type)
  554. return true;
  555. }
  556. }
  557. return false;
  558. }
  559. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  560. /**
  561. * Returns whether the given set of divisors are valid for a given refclk with
  562. * the given connectors.
  563. */
  564. static bool intel_PLL_is_valid(struct drm_crtc *crtc, intel_clock_t *clock)
  565. {
  566. const intel_limit_t *limit = intel_limit (crtc);
  567. struct drm_device *dev = crtc->dev;
  568. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  569. INTELPllInvalid ("p1 out of range\n");
  570. if (clock->p < limit->p.min || limit->p.max < clock->p)
  571. INTELPllInvalid ("p out of range\n");
  572. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  573. INTELPllInvalid ("m2 out of range\n");
  574. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  575. INTELPllInvalid ("m1 out of range\n");
  576. if (clock->m1 <= clock->m2 && !IS_IGD(dev))
  577. INTELPllInvalid ("m1 <= m2\n");
  578. if (clock->m < limit->m.min || limit->m.max < clock->m)
  579. INTELPllInvalid ("m out of range\n");
  580. if (clock->n < limit->n.min || limit->n.max < clock->n)
  581. INTELPllInvalid ("n out of range\n");
  582. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  583. INTELPllInvalid ("vco out of range\n");
  584. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  585. * connector, etc., rather than just a single range.
  586. */
  587. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  588. INTELPllInvalid ("dot out of range\n");
  589. return true;
  590. }
  591. static bool
  592. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  593. int target, int refclk, intel_clock_t *best_clock)
  594. {
  595. struct drm_device *dev = crtc->dev;
  596. struct drm_i915_private *dev_priv = dev->dev_private;
  597. intel_clock_t clock;
  598. int err = target;
  599. if (IS_I9XX(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  600. (I915_READ(LVDS) & LVDS_PORT_EN) != 0) {
  601. /*
  602. * For LVDS, if the panel is on, just rely on its current
  603. * settings for dual-channel. We haven't figured out how to
  604. * reliably set up different single/dual channel state, if we
  605. * even can.
  606. */
  607. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  608. LVDS_CLKB_POWER_UP)
  609. clock.p2 = limit->p2.p2_fast;
  610. else
  611. clock.p2 = limit->p2.p2_slow;
  612. } else {
  613. if (target < limit->p2.dot_limit)
  614. clock.p2 = limit->p2.p2_slow;
  615. else
  616. clock.p2 = limit->p2.p2_fast;
  617. }
  618. memset (best_clock, 0, sizeof (*best_clock));
  619. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  620. for (clock.m2 = limit->m2.min; clock.m2 <= limit->m2.max; clock.m2++) {
  621. /* m1 is always 0 in IGD */
  622. if (clock.m2 >= clock.m1 && !IS_IGD(dev))
  623. break;
  624. for (clock.n = limit->n.min; clock.n <= limit->n.max;
  625. clock.n++) {
  626. for (clock.p1 = limit->p1.min;
  627. clock.p1 <= limit->p1.max; clock.p1++) {
  628. int this_err;
  629. intel_clock(dev, refclk, &clock);
  630. if (!intel_PLL_is_valid(crtc, &clock))
  631. continue;
  632. this_err = abs(clock.dot - target);
  633. if (this_err < err) {
  634. *best_clock = clock;
  635. err = this_err;
  636. }
  637. }
  638. }
  639. }
  640. }
  641. return (err != target);
  642. }
  643. static bool
  644. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  645. int target, int refclk, intel_clock_t *best_clock)
  646. {
  647. struct drm_device *dev = crtc->dev;
  648. struct drm_i915_private *dev_priv = dev->dev_private;
  649. intel_clock_t clock;
  650. int max_n;
  651. bool found;
  652. /* approximately equals target * 0.00488 */
  653. int err_most = (target >> 8) + (target >> 10);
  654. found = false;
  655. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  656. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  657. LVDS_CLKB_POWER_UP)
  658. clock.p2 = limit->p2.p2_fast;
  659. else
  660. clock.p2 = limit->p2.p2_slow;
  661. } else {
  662. if (target < limit->p2.dot_limit)
  663. clock.p2 = limit->p2.p2_slow;
  664. else
  665. clock.p2 = limit->p2.p2_fast;
  666. }
  667. memset(best_clock, 0, sizeof(*best_clock));
  668. max_n = limit->n.max;
  669. /* based on hardware requriment prefer smaller n to precision */
  670. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  671. /* based on hardware requirment prefere larger m1,m2, p1 */
  672. for (clock.m1 = limit->m1.max;
  673. clock.m1 >= limit->m1.min; clock.m1--) {
  674. for (clock.m2 = limit->m2.max;
  675. clock.m2 >= limit->m2.min; clock.m2--) {
  676. for (clock.p1 = limit->p1.max;
  677. clock.p1 >= limit->p1.min; clock.p1--) {
  678. int this_err;
  679. intel_clock(dev, refclk, &clock);
  680. if (!intel_PLL_is_valid(crtc, &clock))
  681. continue;
  682. this_err = abs(clock.dot - target) ;
  683. if (this_err < err_most) {
  684. *best_clock = clock;
  685. err_most = this_err;
  686. max_n = clock.n;
  687. found = true;
  688. }
  689. }
  690. }
  691. }
  692. }
  693. return found;
  694. }
  695. static bool
  696. intel_igdng_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  697. int target, int refclk, intel_clock_t *best_clock)
  698. {
  699. struct drm_device *dev = crtc->dev;
  700. struct drm_i915_private *dev_priv = dev->dev_private;
  701. intel_clock_t clock;
  702. int max_n;
  703. bool found;
  704. int err_most = 47;
  705. found = false;
  706. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  707. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  708. LVDS_CLKB_POWER_UP)
  709. clock.p2 = limit->p2.p2_fast;
  710. else
  711. clock.p2 = limit->p2.p2_slow;
  712. } else {
  713. if (target < limit->p2.dot_limit)
  714. clock.p2 = limit->p2.p2_slow;
  715. else
  716. clock.p2 = limit->p2.p2_fast;
  717. }
  718. memset(best_clock, 0, sizeof(*best_clock));
  719. max_n = limit->n.max;
  720. /* based on hardware requriment prefer smaller n to precision */
  721. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  722. /* based on hardware requirment prefere larger m1,m2, p1 */
  723. for (clock.m1 = limit->m1.max;
  724. clock.m1 >= limit->m1.min; clock.m1--) {
  725. for (clock.m2 = limit->m2.max;
  726. clock.m2 >= limit->m2.min; clock.m2--) {
  727. for (clock.p1 = limit->p1.max;
  728. clock.p1 >= limit->p1.min; clock.p1--) {
  729. int this_err;
  730. intel_clock(dev, refclk, &clock);
  731. if (!intel_PLL_is_valid(crtc, &clock))
  732. continue;
  733. this_err = abs((10000 - (target*10000/clock.dot)));
  734. if (this_err < err_most) {
  735. *best_clock = clock;
  736. err_most = this_err;
  737. max_n = clock.n;
  738. found = true;
  739. /* found on first matching */
  740. goto out;
  741. }
  742. }
  743. }
  744. }
  745. }
  746. out:
  747. return found;
  748. }
  749. /* DisplayPort has only two frequencies, 162MHz and 270MHz */
  750. static bool
  751. intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  752. int target, int refclk, intel_clock_t *best_clock)
  753. {
  754. intel_clock_t clock;
  755. if (target < 200000) {
  756. clock.p1 = 2;
  757. clock.p2 = 10;
  758. clock.n = 2;
  759. clock.m1 = 23;
  760. clock.m2 = 8;
  761. } else {
  762. clock.p1 = 1;
  763. clock.p2 = 10;
  764. clock.n = 1;
  765. clock.m1 = 14;
  766. clock.m2 = 2;
  767. }
  768. clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
  769. clock.p = (clock.p1 * clock.p2);
  770. clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
  771. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  772. return true;
  773. }
  774. void
  775. intel_wait_for_vblank(struct drm_device *dev)
  776. {
  777. /* Wait for 20ms, i.e. one cycle at 50hz. */
  778. mdelay(20);
  779. }
  780. static int
  781. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  782. struct drm_framebuffer *old_fb)
  783. {
  784. struct drm_device *dev = crtc->dev;
  785. struct drm_i915_private *dev_priv = dev->dev_private;
  786. struct drm_i915_master_private *master_priv;
  787. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  788. struct intel_framebuffer *intel_fb;
  789. struct drm_i915_gem_object *obj_priv;
  790. struct drm_gem_object *obj;
  791. int pipe = intel_crtc->pipe;
  792. unsigned long Start, Offset;
  793. int dspbase = (pipe == 0 ? DSPAADDR : DSPBADDR);
  794. int dspsurf = (pipe == 0 ? DSPASURF : DSPBSURF);
  795. int dspstride = (pipe == 0) ? DSPASTRIDE : DSPBSTRIDE;
  796. int dsptileoff = (pipe == 0 ? DSPATILEOFF : DSPBTILEOFF);
  797. int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
  798. u32 dspcntr, alignment;
  799. int ret;
  800. /* no fb bound */
  801. if (!crtc->fb) {
  802. DRM_DEBUG("No FB bound\n");
  803. return 0;
  804. }
  805. switch (pipe) {
  806. case 0:
  807. case 1:
  808. break;
  809. default:
  810. DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
  811. return -EINVAL;
  812. }
  813. intel_fb = to_intel_framebuffer(crtc->fb);
  814. obj = intel_fb->obj;
  815. obj_priv = obj->driver_private;
  816. switch (obj_priv->tiling_mode) {
  817. case I915_TILING_NONE:
  818. alignment = 64 * 1024;
  819. break;
  820. case I915_TILING_X:
  821. /* pin() will align the object as required by fence */
  822. alignment = 0;
  823. break;
  824. case I915_TILING_Y:
  825. /* FIXME: Is this true? */
  826. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  827. return -EINVAL;
  828. default:
  829. BUG();
  830. }
  831. mutex_lock(&dev->struct_mutex);
  832. ret = i915_gem_object_pin(obj, alignment);
  833. if (ret != 0) {
  834. mutex_unlock(&dev->struct_mutex);
  835. return ret;
  836. }
  837. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  838. if (ret != 0) {
  839. i915_gem_object_unpin(obj);
  840. mutex_unlock(&dev->struct_mutex);
  841. return ret;
  842. }
  843. /* Pre-i965 needs to install a fence for tiled scan-out */
  844. if (!IS_I965G(dev) &&
  845. obj_priv->fence_reg == I915_FENCE_REG_NONE &&
  846. obj_priv->tiling_mode != I915_TILING_NONE) {
  847. ret = i915_gem_object_get_fence_reg(obj);
  848. if (ret != 0) {
  849. i915_gem_object_unpin(obj);
  850. mutex_unlock(&dev->struct_mutex);
  851. return ret;
  852. }
  853. }
  854. dspcntr = I915_READ(dspcntr_reg);
  855. /* Mask out pixel format bits in case we change it */
  856. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  857. switch (crtc->fb->bits_per_pixel) {
  858. case 8:
  859. dspcntr |= DISPPLANE_8BPP;
  860. break;
  861. case 16:
  862. if (crtc->fb->depth == 15)
  863. dspcntr |= DISPPLANE_15_16BPP;
  864. else
  865. dspcntr |= DISPPLANE_16BPP;
  866. break;
  867. case 24:
  868. case 32:
  869. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  870. break;
  871. default:
  872. DRM_ERROR("Unknown color depth\n");
  873. i915_gem_object_unpin(obj);
  874. mutex_unlock(&dev->struct_mutex);
  875. return -EINVAL;
  876. }
  877. if (IS_I965G(dev)) {
  878. if (obj_priv->tiling_mode != I915_TILING_NONE)
  879. dspcntr |= DISPPLANE_TILED;
  880. else
  881. dspcntr &= ~DISPPLANE_TILED;
  882. }
  883. I915_WRITE(dspcntr_reg, dspcntr);
  884. Start = obj_priv->gtt_offset;
  885. Offset = y * crtc->fb->pitch + x * (crtc->fb->bits_per_pixel / 8);
  886. DRM_DEBUG("Writing base %08lX %08lX %d %d\n", Start, Offset, x, y);
  887. I915_WRITE(dspstride, crtc->fb->pitch);
  888. if (IS_I965G(dev)) {
  889. I915_WRITE(dspbase, Offset);
  890. I915_READ(dspbase);
  891. I915_WRITE(dspsurf, Start);
  892. I915_READ(dspsurf);
  893. I915_WRITE(dsptileoff, (y << 16) | x);
  894. } else {
  895. I915_WRITE(dspbase, Start + Offset);
  896. I915_READ(dspbase);
  897. }
  898. intel_wait_for_vblank(dev);
  899. if (old_fb) {
  900. intel_fb = to_intel_framebuffer(old_fb);
  901. i915_gem_object_unpin(intel_fb->obj);
  902. }
  903. mutex_unlock(&dev->struct_mutex);
  904. if (!dev->primary->master)
  905. return 0;
  906. master_priv = dev->primary->master->driver_priv;
  907. if (!master_priv->sarea_priv)
  908. return 0;
  909. if (pipe) {
  910. master_priv->sarea_priv->pipeB_x = x;
  911. master_priv->sarea_priv->pipeB_y = y;
  912. } else {
  913. master_priv->sarea_priv->pipeA_x = x;
  914. master_priv->sarea_priv->pipeA_y = y;
  915. }
  916. return 0;
  917. }
  918. static void igdng_crtc_dpms(struct drm_crtc *crtc, int mode)
  919. {
  920. struct drm_device *dev = crtc->dev;
  921. struct drm_i915_private *dev_priv = dev->dev_private;
  922. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  923. int pipe = intel_crtc->pipe;
  924. int plane = intel_crtc->plane;
  925. int pch_dpll_reg = (pipe == 0) ? PCH_DPLL_A : PCH_DPLL_B;
  926. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  927. int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
  928. int dspbase_reg = (plane == 0) ? DSPAADDR : DSPBADDR;
  929. int fdi_tx_reg = (pipe == 0) ? FDI_TXA_CTL : FDI_TXB_CTL;
  930. int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
  931. int fdi_rx_iir_reg = (pipe == 0) ? FDI_RXA_IIR : FDI_RXB_IIR;
  932. int fdi_rx_imr_reg = (pipe == 0) ? FDI_RXA_IMR : FDI_RXB_IMR;
  933. int transconf_reg = (pipe == 0) ? TRANSACONF : TRANSBCONF;
  934. int pf_ctl_reg = (pipe == 0) ? PFA_CTL_1 : PFB_CTL_1;
  935. int cpu_htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
  936. int cpu_hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
  937. int cpu_hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
  938. int cpu_vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
  939. int cpu_vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
  940. int cpu_vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
  941. int trans_htot_reg = (pipe == 0) ? TRANS_HTOTAL_A : TRANS_HTOTAL_B;
  942. int trans_hblank_reg = (pipe == 0) ? TRANS_HBLANK_A : TRANS_HBLANK_B;
  943. int trans_hsync_reg = (pipe == 0) ? TRANS_HSYNC_A : TRANS_HSYNC_B;
  944. int trans_vtot_reg = (pipe == 0) ? TRANS_VTOTAL_A : TRANS_VTOTAL_B;
  945. int trans_vblank_reg = (pipe == 0) ? TRANS_VBLANK_A : TRANS_VBLANK_B;
  946. int trans_vsync_reg = (pipe == 0) ? TRANS_VSYNC_A : TRANS_VSYNC_B;
  947. u32 temp;
  948. int tries = 5, j;
  949. /* XXX: When our outputs are all unaware of DPMS modes other than off
  950. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  951. */
  952. switch (mode) {
  953. case DRM_MODE_DPMS_ON:
  954. case DRM_MODE_DPMS_STANDBY:
  955. case DRM_MODE_DPMS_SUSPEND:
  956. DRM_DEBUG("crtc %d dpms on\n", pipe);
  957. /* enable PCH DPLL */
  958. temp = I915_READ(pch_dpll_reg);
  959. if ((temp & DPLL_VCO_ENABLE) == 0) {
  960. I915_WRITE(pch_dpll_reg, temp | DPLL_VCO_ENABLE);
  961. I915_READ(pch_dpll_reg);
  962. }
  963. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  964. temp = I915_READ(fdi_rx_reg);
  965. I915_WRITE(fdi_rx_reg, temp | FDI_RX_PLL_ENABLE |
  966. FDI_SEL_PCDCLK |
  967. FDI_DP_PORT_WIDTH_X4); /* default 4 lanes */
  968. I915_READ(fdi_rx_reg);
  969. udelay(200);
  970. /* Enable CPU FDI TX PLL, always on for IGDNG */
  971. temp = I915_READ(fdi_tx_reg);
  972. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  973. I915_WRITE(fdi_tx_reg, temp | FDI_TX_PLL_ENABLE);
  974. I915_READ(fdi_tx_reg);
  975. udelay(100);
  976. }
  977. /* Enable CPU pipe */
  978. temp = I915_READ(pipeconf_reg);
  979. if ((temp & PIPEACONF_ENABLE) == 0) {
  980. I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
  981. I915_READ(pipeconf_reg);
  982. udelay(100);
  983. }
  984. /* configure and enable CPU plane */
  985. temp = I915_READ(dspcntr_reg);
  986. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  987. I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
  988. /* Flush the plane changes */
  989. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  990. }
  991. /* enable CPU FDI TX and PCH FDI RX */
  992. temp = I915_READ(fdi_tx_reg);
  993. temp |= FDI_TX_ENABLE;
  994. temp |= FDI_DP_PORT_WIDTH_X4; /* default */
  995. temp &= ~FDI_LINK_TRAIN_NONE;
  996. temp |= FDI_LINK_TRAIN_PATTERN_1;
  997. I915_WRITE(fdi_tx_reg, temp);
  998. I915_READ(fdi_tx_reg);
  999. temp = I915_READ(fdi_rx_reg);
  1000. temp &= ~FDI_LINK_TRAIN_NONE;
  1001. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1002. I915_WRITE(fdi_rx_reg, temp | FDI_RX_ENABLE);
  1003. I915_READ(fdi_rx_reg);
  1004. udelay(150);
  1005. /* Train FDI. */
  1006. /* umask FDI RX Interrupt symbol_lock and bit_lock bit
  1007. for train result */
  1008. temp = I915_READ(fdi_rx_imr_reg);
  1009. temp &= ~FDI_RX_SYMBOL_LOCK;
  1010. temp &= ~FDI_RX_BIT_LOCK;
  1011. I915_WRITE(fdi_rx_imr_reg, temp);
  1012. I915_READ(fdi_rx_imr_reg);
  1013. udelay(150);
  1014. temp = I915_READ(fdi_rx_iir_reg);
  1015. DRM_DEBUG("FDI_RX_IIR 0x%x\n", temp);
  1016. if ((temp & FDI_RX_BIT_LOCK) == 0) {
  1017. for (j = 0; j < tries; j++) {
  1018. temp = I915_READ(fdi_rx_iir_reg);
  1019. DRM_DEBUG("FDI_RX_IIR 0x%x\n", temp);
  1020. if (temp & FDI_RX_BIT_LOCK)
  1021. break;
  1022. udelay(200);
  1023. }
  1024. if (j != tries)
  1025. I915_WRITE(fdi_rx_iir_reg,
  1026. temp | FDI_RX_BIT_LOCK);
  1027. else
  1028. DRM_DEBUG("train 1 fail\n");
  1029. } else {
  1030. I915_WRITE(fdi_rx_iir_reg,
  1031. temp | FDI_RX_BIT_LOCK);
  1032. DRM_DEBUG("train 1 ok 2!\n");
  1033. }
  1034. temp = I915_READ(fdi_tx_reg);
  1035. temp &= ~FDI_LINK_TRAIN_NONE;
  1036. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1037. I915_WRITE(fdi_tx_reg, temp);
  1038. temp = I915_READ(fdi_rx_reg);
  1039. temp &= ~FDI_LINK_TRAIN_NONE;
  1040. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1041. I915_WRITE(fdi_rx_reg, temp);
  1042. udelay(150);
  1043. temp = I915_READ(fdi_rx_iir_reg);
  1044. DRM_DEBUG("FDI_RX_IIR 0x%x\n", temp);
  1045. if ((temp & FDI_RX_SYMBOL_LOCK) == 0) {
  1046. for (j = 0; j < tries; j++) {
  1047. temp = I915_READ(fdi_rx_iir_reg);
  1048. DRM_DEBUG("FDI_RX_IIR 0x%x\n", temp);
  1049. if (temp & FDI_RX_SYMBOL_LOCK)
  1050. break;
  1051. udelay(200);
  1052. }
  1053. if (j != tries) {
  1054. I915_WRITE(fdi_rx_iir_reg,
  1055. temp | FDI_RX_SYMBOL_LOCK);
  1056. DRM_DEBUG("train 2 ok 1!\n");
  1057. } else
  1058. DRM_DEBUG("train 2 fail\n");
  1059. } else {
  1060. I915_WRITE(fdi_rx_iir_reg, temp | FDI_RX_SYMBOL_LOCK);
  1061. DRM_DEBUG("train 2 ok 2!\n");
  1062. }
  1063. DRM_DEBUG("train done\n");
  1064. /* set transcoder timing */
  1065. I915_WRITE(trans_htot_reg, I915_READ(cpu_htot_reg));
  1066. I915_WRITE(trans_hblank_reg, I915_READ(cpu_hblank_reg));
  1067. I915_WRITE(trans_hsync_reg, I915_READ(cpu_hsync_reg));
  1068. I915_WRITE(trans_vtot_reg, I915_READ(cpu_vtot_reg));
  1069. I915_WRITE(trans_vblank_reg, I915_READ(cpu_vblank_reg));
  1070. I915_WRITE(trans_vsync_reg, I915_READ(cpu_vsync_reg));
  1071. /* enable PCH transcoder */
  1072. temp = I915_READ(transconf_reg);
  1073. I915_WRITE(transconf_reg, temp | TRANS_ENABLE);
  1074. I915_READ(transconf_reg);
  1075. while ((I915_READ(transconf_reg) & TRANS_STATE_ENABLE) == 0)
  1076. ;
  1077. /* enable normal */
  1078. temp = I915_READ(fdi_tx_reg);
  1079. temp &= ~FDI_LINK_TRAIN_NONE;
  1080. I915_WRITE(fdi_tx_reg, temp | FDI_LINK_TRAIN_NONE |
  1081. FDI_TX_ENHANCE_FRAME_ENABLE);
  1082. I915_READ(fdi_tx_reg);
  1083. temp = I915_READ(fdi_rx_reg);
  1084. temp &= ~FDI_LINK_TRAIN_NONE;
  1085. I915_WRITE(fdi_rx_reg, temp | FDI_LINK_TRAIN_NONE |
  1086. FDI_RX_ENHANCE_FRAME_ENABLE);
  1087. I915_READ(fdi_rx_reg);
  1088. /* wait one idle pattern time */
  1089. udelay(100);
  1090. intel_crtc_load_lut(crtc);
  1091. break;
  1092. case DRM_MODE_DPMS_OFF:
  1093. DRM_DEBUG("crtc %d dpms off\n", pipe);
  1094. /* Disable the VGA plane that we never use */
  1095. I915_WRITE(CPU_VGACNTRL, VGA_DISP_DISABLE);
  1096. /* Disable display plane */
  1097. temp = I915_READ(dspcntr_reg);
  1098. if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
  1099. I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
  1100. /* Flush the plane changes */
  1101. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  1102. I915_READ(dspbase_reg);
  1103. }
  1104. /* disable cpu pipe, disable after all planes disabled */
  1105. temp = I915_READ(pipeconf_reg);
  1106. if ((temp & PIPEACONF_ENABLE) != 0) {
  1107. I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
  1108. I915_READ(pipeconf_reg);
  1109. /* wait for cpu pipe off, pipe state */
  1110. while ((I915_READ(pipeconf_reg) & I965_PIPECONF_ACTIVE) != 0)
  1111. ;
  1112. } else
  1113. DRM_DEBUG("crtc %d is disabled\n", pipe);
  1114. /* IGDNG-A : disable cpu panel fitter ? */
  1115. temp = I915_READ(pf_ctl_reg);
  1116. if ((temp & PF_ENABLE) != 0) {
  1117. I915_WRITE(pf_ctl_reg, temp & ~PF_ENABLE);
  1118. I915_READ(pf_ctl_reg);
  1119. }
  1120. /* disable CPU FDI tx and PCH FDI rx */
  1121. temp = I915_READ(fdi_tx_reg);
  1122. I915_WRITE(fdi_tx_reg, temp & ~FDI_TX_ENABLE);
  1123. I915_READ(fdi_tx_reg);
  1124. temp = I915_READ(fdi_rx_reg);
  1125. I915_WRITE(fdi_rx_reg, temp & ~FDI_RX_ENABLE);
  1126. I915_READ(fdi_rx_reg);
  1127. /* still set train pattern 1 */
  1128. temp = I915_READ(fdi_tx_reg);
  1129. temp &= ~FDI_LINK_TRAIN_NONE;
  1130. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1131. I915_WRITE(fdi_tx_reg, temp);
  1132. temp = I915_READ(fdi_rx_reg);
  1133. temp &= ~FDI_LINK_TRAIN_NONE;
  1134. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1135. I915_WRITE(fdi_rx_reg, temp);
  1136. /* disable PCH transcoder */
  1137. temp = I915_READ(transconf_reg);
  1138. if ((temp & TRANS_ENABLE) != 0) {
  1139. I915_WRITE(transconf_reg, temp & ~TRANS_ENABLE);
  1140. I915_READ(transconf_reg);
  1141. /* wait for PCH transcoder off, transcoder state */
  1142. while ((I915_READ(transconf_reg) & TRANS_STATE_ENABLE) != 0)
  1143. ;
  1144. }
  1145. /* disable PCH DPLL */
  1146. temp = I915_READ(pch_dpll_reg);
  1147. if ((temp & DPLL_VCO_ENABLE) != 0) {
  1148. I915_WRITE(pch_dpll_reg, temp & ~DPLL_VCO_ENABLE);
  1149. I915_READ(pch_dpll_reg);
  1150. }
  1151. temp = I915_READ(fdi_rx_reg);
  1152. if ((temp & FDI_RX_PLL_ENABLE) != 0) {
  1153. temp &= ~FDI_SEL_PCDCLK;
  1154. temp &= ~FDI_RX_PLL_ENABLE;
  1155. I915_WRITE(fdi_rx_reg, temp);
  1156. I915_READ(fdi_rx_reg);
  1157. }
  1158. /* Wait for the clocks to turn off. */
  1159. udelay(150);
  1160. break;
  1161. }
  1162. }
  1163. static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
  1164. {
  1165. struct drm_device *dev = crtc->dev;
  1166. struct drm_i915_private *dev_priv = dev->dev_private;
  1167. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1168. int pipe = intel_crtc->pipe;
  1169. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  1170. int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
  1171. int dspbase_reg = (pipe == 0) ? DSPAADDR : DSPBADDR;
  1172. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  1173. u32 temp;
  1174. /* XXX: When our outputs are all unaware of DPMS modes other than off
  1175. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  1176. */
  1177. switch (mode) {
  1178. case DRM_MODE_DPMS_ON:
  1179. case DRM_MODE_DPMS_STANDBY:
  1180. case DRM_MODE_DPMS_SUSPEND:
  1181. /* Enable the DPLL */
  1182. temp = I915_READ(dpll_reg);
  1183. if ((temp & DPLL_VCO_ENABLE) == 0) {
  1184. I915_WRITE(dpll_reg, temp);
  1185. I915_READ(dpll_reg);
  1186. /* Wait for the clocks to stabilize. */
  1187. udelay(150);
  1188. I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
  1189. I915_READ(dpll_reg);
  1190. /* Wait for the clocks to stabilize. */
  1191. udelay(150);
  1192. I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
  1193. I915_READ(dpll_reg);
  1194. /* Wait for the clocks to stabilize. */
  1195. udelay(150);
  1196. }
  1197. /* Enable the pipe */
  1198. temp = I915_READ(pipeconf_reg);
  1199. if ((temp & PIPEACONF_ENABLE) == 0)
  1200. I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
  1201. /* Enable the plane */
  1202. temp = I915_READ(dspcntr_reg);
  1203. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  1204. I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
  1205. /* Flush the plane changes */
  1206. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  1207. }
  1208. intel_crtc_load_lut(crtc);
  1209. /* Give the overlay scaler a chance to enable if it's on this pipe */
  1210. //intel_crtc_dpms_video(crtc, true); TODO
  1211. intel_update_watermarks(dev);
  1212. break;
  1213. case DRM_MODE_DPMS_OFF:
  1214. intel_update_watermarks(dev);
  1215. /* Give the overlay scaler a chance to disable if it's on this pipe */
  1216. //intel_crtc_dpms_video(crtc, FALSE); TODO
  1217. /* Disable the VGA plane that we never use */
  1218. I915_WRITE(VGACNTRL, VGA_DISP_DISABLE);
  1219. /* Disable display plane */
  1220. temp = I915_READ(dspcntr_reg);
  1221. if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
  1222. I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
  1223. /* Flush the plane changes */
  1224. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  1225. I915_READ(dspbase_reg);
  1226. }
  1227. if (!IS_I9XX(dev)) {
  1228. /* Wait for vblank for the disable to take effect */
  1229. intel_wait_for_vblank(dev);
  1230. }
  1231. /* Next, disable display pipes */
  1232. temp = I915_READ(pipeconf_reg);
  1233. if ((temp & PIPEACONF_ENABLE) != 0) {
  1234. I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
  1235. I915_READ(pipeconf_reg);
  1236. }
  1237. /* Wait for vblank for the disable to take effect. */
  1238. intel_wait_for_vblank(dev);
  1239. temp = I915_READ(dpll_reg);
  1240. if ((temp & DPLL_VCO_ENABLE) != 0) {
  1241. I915_WRITE(dpll_reg, temp & ~DPLL_VCO_ENABLE);
  1242. I915_READ(dpll_reg);
  1243. }
  1244. /* Wait for the clocks to turn off. */
  1245. udelay(150);
  1246. break;
  1247. }
  1248. }
  1249. /**
  1250. * Sets the power management mode of the pipe and plane.
  1251. *
  1252. * This code should probably grow support for turning the cursor off and back
  1253. * on appropriately at the same time as we're turning the pipe off/on.
  1254. */
  1255. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  1256. {
  1257. struct drm_device *dev = crtc->dev;
  1258. struct drm_i915_master_private *master_priv;
  1259. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1260. int pipe = intel_crtc->pipe;
  1261. bool enabled;
  1262. if (IS_IGDNG(dev))
  1263. igdng_crtc_dpms(crtc, mode);
  1264. else
  1265. i9xx_crtc_dpms(crtc, mode);
  1266. if (!dev->primary->master)
  1267. return;
  1268. master_priv = dev->primary->master->driver_priv;
  1269. if (!master_priv->sarea_priv)
  1270. return;
  1271. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  1272. switch (pipe) {
  1273. case 0:
  1274. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  1275. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  1276. break;
  1277. case 1:
  1278. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  1279. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  1280. break;
  1281. default:
  1282. DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
  1283. break;
  1284. }
  1285. intel_crtc->dpms_mode = mode;
  1286. }
  1287. static void intel_crtc_prepare (struct drm_crtc *crtc)
  1288. {
  1289. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  1290. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  1291. }
  1292. static void intel_crtc_commit (struct drm_crtc *crtc)
  1293. {
  1294. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  1295. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  1296. }
  1297. void intel_encoder_prepare (struct drm_encoder *encoder)
  1298. {
  1299. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  1300. /* lvds has its own version of prepare see intel_lvds_prepare */
  1301. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  1302. }
  1303. void intel_encoder_commit (struct drm_encoder *encoder)
  1304. {
  1305. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  1306. /* lvds has its own version of commit see intel_lvds_commit */
  1307. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  1308. }
  1309. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  1310. struct drm_display_mode *mode,
  1311. struct drm_display_mode *adjusted_mode)
  1312. {
  1313. struct drm_device *dev = crtc->dev;
  1314. if (IS_IGDNG(dev)) {
  1315. /* FDI link clock is fixed at 2.7G */
  1316. if (mode->clock * 3 > 27000 * 4)
  1317. return MODE_CLOCK_HIGH;
  1318. }
  1319. return true;
  1320. }
  1321. /** Returns the core display clock speed for i830 - i945 */
  1322. static int intel_get_core_clock_speed(struct drm_device *dev)
  1323. {
  1324. /* Core clock values taken from the published datasheets.
  1325. * The 830 may go up to 166 Mhz, which we should check.
  1326. */
  1327. if (IS_I945G(dev))
  1328. return 400000;
  1329. else if (IS_I915G(dev))
  1330. return 333000;
  1331. else if (IS_I945GM(dev) || IS_845G(dev) || IS_IGDGM(dev))
  1332. return 200000;
  1333. else if (IS_I915GM(dev)) {
  1334. u16 gcfgc = 0;
  1335. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  1336. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  1337. return 133000;
  1338. else {
  1339. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  1340. case GC_DISPLAY_CLOCK_333_MHZ:
  1341. return 333000;
  1342. default:
  1343. case GC_DISPLAY_CLOCK_190_200_MHZ:
  1344. return 190000;
  1345. }
  1346. }
  1347. } else if (IS_I865G(dev))
  1348. return 266000;
  1349. else if (IS_I855(dev)) {
  1350. u16 hpllcc = 0;
  1351. /* Assume that the hardware is in the high speed state. This
  1352. * should be the default.
  1353. */
  1354. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  1355. case GC_CLOCK_133_200:
  1356. case GC_CLOCK_100_200:
  1357. return 200000;
  1358. case GC_CLOCK_166_250:
  1359. return 250000;
  1360. case GC_CLOCK_100_133:
  1361. return 133000;
  1362. }
  1363. } else /* 852, 830 */
  1364. return 133000;
  1365. return 0; /* Silence gcc warning */
  1366. }
  1367. /**
  1368. * Return the pipe currently connected to the panel fitter,
  1369. * or -1 if the panel fitter is not present or not in use
  1370. */
  1371. static int intel_panel_fitter_pipe (struct drm_device *dev)
  1372. {
  1373. struct drm_i915_private *dev_priv = dev->dev_private;
  1374. u32 pfit_control;
  1375. /* i830 doesn't have a panel fitter */
  1376. if (IS_I830(dev))
  1377. return -1;
  1378. pfit_control = I915_READ(PFIT_CONTROL);
  1379. /* See if the panel fitter is in use */
  1380. if ((pfit_control & PFIT_ENABLE) == 0)
  1381. return -1;
  1382. /* 965 can place panel fitter on either pipe */
  1383. if (IS_I965G(dev))
  1384. return (pfit_control >> 29) & 0x3;
  1385. /* older chips can only use pipe 1 */
  1386. return 1;
  1387. }
  1388. struct fdi_m_n {
  1389. u32 tu;
  1390. u32 gmch_m;
  1391. u32 gmch_n;
  1392. u32 link_m;
  1393. u32 link_n;
  1394. };
  1395. static void
  1396. fdi_reduce_ratio(u32 *num, u32 *den)
  1397. {
  1398. while (*num > 0xffffff || *den > 0xffffff) {
  1399. *num >>= 1;
  1400. *den >>= 1;
  1401. }
  1402. }
  1403. #define DATA_N 0x800000
  1404. #define LINK_N 0x80000
  1405. static void
  1406. igdng_compute_m_n(int bytes_per_pixel, int nlanes,
  1407. int pixel_clock, int link_clock,
  1408. struct fdi_m_n *m_n)
  1409. {
  1410. u64 temp;
  1411. m_n->tu = 64; /* default size */
  1412. temp = (u64) DATA_N * pixel_clock;
  1413. temp = div_u64(temp, link_clock);
  1414. m_n->gmch_m = div_u64(temp * bytes_per_pixel, nlanes);
  1415. m_n->gmch_n = DATA_N;
  1416. fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  1417. temp = (u64) LINK_N * pixel_clock;
  1418. m_n->link_m = div_u64(temp, link_clock);
  1419. m_n->link_n = LINK_N;
  1420. fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
  1421. }
  1422. struct intel_watermark_params {
  1423. unsigned long fifo_size;
  1424. unsigned long max_wm;
  1425. unsigned long default_wm;
  1426. unsigned long guard_size;
  1427. unsigned long cacheline_size;
  1428. };
  1429. /* IGD has different values for various configs */
  1430. static struct intel_watermark_params igd_display_wm = {
  1431. IGD_DISPLAY_FIFO,
  1432. IGD_MAX_WM,
  1433. IGD_DFT_WM,
  1434. IGD_GUARD_WM,
  1435. IGD_FIFO_LINE_SIZE
  1436. };
  1437. static struct intel_watermark_params igd_display_hplloff_wm = {
  1438. IGD_DISPLAY_FIFO,
  1439. IGD_MAX_WM,
  1440. IGD_DFT_HPLLOFF_WM,
  1441. IGD_GUARD_WM,
  1442. IGD_FIFO_LINE_SIZE
  1443. };
  1444. static struct intel_watermark_params igd_cursor_wm = {
  1445. IGD_CURSOR_FIFO,
  1446. IGD_CURSOR_MAX_WM,
  1447. IGD_CURSOR_DFT_WM,
  1448. IGD_CURSOR_GUARD_WM,
  1449. IGD_FIFO_LINE_SIZE,
  1450. };
  1451. static struct intel_watermark_params igd_cursor_hplloff_wm = {
  1452. IGD_CURSOR_FIFO,
  1453. IGD_CURSOR_MAX_WM,
  1454. IGD_CURSOR_DFT_WM,
  1455. IGD_CURSOR_GUARD_WM,
  1456. IGD_FIFO_LINE_SIZE
  1457. };
  1458. static struct intel_watermark_params i945_wm_info = {
  1459. I915_FIFO_LINE_SIZE,
  1460. I915_MAX_WM,
  1461. 1,
  1462. 0,
  1463. IGD_FIFO_LINE_SIZE
  1464. };
  1465. static struct intel_watermark_params i915_wm_info = {
  1466. I945_FIFO_SIZE,
  1467. I915_MAX_WM,
  1468. 1,
  1469. 0,
  1470. I915_FIFO_LINE_SIZE
  1471. };
  1472. static struct intel_watermark_params i855_wm_info = {
  1473. I855GM_FIFO_SIZE,
  1474. I915_MAX_WM,
  1475. 1,
  1476. 0,
  1477. I830_FIFO_LINE_SIZE
  1478. };
  1479. static struct intel_watermark_params i830_wm_info = {
  1480. I830_FIFO_SIZE,
  1481. I915_MAX_WM,
  1482. 1,
  1483. 0,
  1484. I830_FIFO_LINE_SIZE
  1485. };
  1486. static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
  1487. struct intel_watermark_params *wm,
  1488. int pixel_size,
  1489. unsigned long latency_ns)
  1490. {
  1491. unsigned long bytes_required, wm_size;
  1492. bytes_required = (clock_in_khz * pixel_size * latency_ns) / 1000000;
  1493. bytes_required /= wm->cacheline_size;
  1494. wm_size = wm->fifo_size - bytes_required - wm->guard_size;
  1495. if (wm_size > wm->max_wm)
  1496. wm_size = wm->max_wm;
  1497. if (wm_size == 0)
  1498. wm_size = wm->default_wm;
  1499. return wm_size;
  1500. }
  1501. struct cxsr_latency {
  1502. int is_desktop;
  1503. unsigned long fsb_freq;
  1504. unsigned long mem_freq;
  1505. unsigned long display_sr;
  1506. unsigned long display_hpll_disable;
  1507. unsigned long cursor_sr;
  1508. unsigned long cursor_hpll_disable;
  1509. };
  1510. static struct cxsr_latency cxsr_latency_table[] = {
  1511. {1, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  1512. {1, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  1513. {1, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  1514. {1, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  1515. {1, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  1516. {1, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  1517. {1, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  1518. {1, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  1519. {1, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  1520. {0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  1521. {0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  1522. {0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  1523. {0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  1524. {0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  1525. {0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  1526. {0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  1527. {0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  1528. {0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  1529. };
  1530. static struct cxsr_latency *intel_get_cxsr_latency(int is_desktop, int fsb,
  1531. int mem)
  1532. {
  1533. int i;
  1534. struct cxsr_latency *latency;
  1535. if (fsb == 0 || mem == 0)
  1536. return NULL;
  1537. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  1538. latency = &cxsr_latency_table[i];
  1539. if (is_desktop == latency->is_desktop &&
  1540. fsb == latency->fsb_freq && mem == latency->mem_freq)
  1541. break;
  1542. }
  1543. if (i >= ARRAY_SIZE(cxsr_latency_table)) {
  1544. DRM_DEBUG("Unknown FSB/MEM found, disable CxSR\n");
  1545. return NULL;
  1546. }
  1547. return latency;
  1548. }
  1549. static void igd_disable_cxsr(struct drm_device *dev)
  1550. {
  1551. struct drm_i915_private *dev_priv = dev->dev_private;
  1552. u32 reg;
  1553. /* deactivate cxsr */
  1554. reg = I915_READ(DSPFW3);
  1555. reg &= ~(IGD_SELF_REFRESH_EN);
  1556. I915_WRITE(DSPFW3, reg);
  1557. DRM_INFO("Big FIFO is disabled\n");
  1558. }
  1559. static void igd_enable_cxsr(struct drm_device *dev, unsigned long clock,
  1560. int pixel_size)
  1561. {
  1562. struct drm_i915_private *dev_priv = dev->dev_private;
  1563. u32 reg;
  1564. unsigned long wm;
  1565. struct cxsr_latency *latency;
  1566. latency = intel_get_cxsr_latency(IS_IGDG(dev), dev_priv->fsb_freq,
  1567. dev_priv->mem_freq);
  1568. if (!latency) {
  1569. DRM_DEBUG("Unknown FSB/MEM found, disable CxSR\n");
  1570. igd_disable_cxsr(dev);
  1571. return;
  1572. }
  1573. /* Display SR */
  1574. wm = intel_calculate_wm(clock, &igd_display_wm, pixel_size,
  1575. latency->display_sr);
  1576. reg = I915_READ(DSPFW1);
  1577. reg &= 0x7fffff;
  1578. reg |= wm << 23;
  1579. I915_WRITE(DSPFW1, reg);
  1580. DRM_DEBUG("DSPFW1 register is %x\n", reg);
  1581. /* cursor SR */
  1582. wm = intel_calculate_wm(clock, &igd_cursor_wm, pixel_size,
  1583. latency->cursor_sr);
  1584. reg = I915_READ(DSPFW3);
  1585. reg &= ~(0x3f << 24);
  1586. reg |= (wm & 0x3f) << 24;
  1587. I915_WRITE(DSPFW3, reg);
  1588. /* Display HPLL off SR */
  1589. wm = intel_calculate_wm(clock, &igd_display_hplloff_wm,
  1590. latency->display_hpll_disable, I915_FIFO_LINE_SIZE);
  1591. reg = I915_READ(DSPFW3);
  1592. reg &= 0xfffffe00;
  1593. reg |= wm & 0x1ff;
  1594. I915_WRITE(DSPFW3, reg);
  1595. /* cursor HPLL off SR */
  1596. wm = intel_calculate_wm(clock, &igd_cursor_hplloff_wm, pixel_size,
  1597. latency->cursor_hpll_disable);
  1598. reg = I915_READ(DSPFW3);
  1599. reg &= ~(0x3f << 16);
  1600. reg |= (wm & 0x3f) << 16;
  1601. I915_WRITE(DSPFW3, reg);
  1602. DRM_DEBUG("DSPFW3 register is %x\n", reg);
  1603. /* activate cxsr */
  1604. reg = I915_READ(DSPFW3);
  1605. reg |= IGD_SELF_REFRESH_EN;
  1606. I915_WRITE(DSPFW3, reg);
  1607. DRM_INFO("Big FIFO is enabled\n");
  1608. return;
  1609. }
  1610. const static int latency_ns = 5000; /* default for non-igd platforms */
  1611. static void i965_update_wm(struct drm_device *dev)
  1612. {
  1613. struct drm_i915_private *dev_priv = dev->dev_private;
  1614. DRM_DEBUG("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR 8\n");
  1615. /* 965 has limitations... */
  1616. I915_WRITE(DSPFW1, (8 << 16) | (8 << 8) | (8 << 0));
  1617. I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
  1618. }
  1619. static void i9xx_update_wm(struct drm_device *dev, int planea_clock,
  1620. int planeb_clock, int sr_hdisplay, int pixel_size)
  1621. {
  1622. struct drm_i915_private *dev_priv = dev->dev_private;
  1623. uint32_t fwater_lo = I915_READ(FW_BLC) & MM_FIFO_WATERMARK;
  1624. uint32_t fwater_hi = I915_READ(FW_BLC2) & LM_FIFO_WATERMARK;
  1625. int bsize, asize, cwm, bwm = 1, awm = 1, srwm = 1;
  1626. uint32_t dsparb = I915_READ(DSPARB);
  1627. int planea_entries, planeb_entries;
  1628. struct intel_watermark_params *wm_params;
  1629. unsigned long line_time_us;
  1630. int sr_clock, sr_entries = 0;
  1631. if (IS_I965GM(dev) || IS_I945GM(dev))
  1632. wm_params = &i945_wm_info;
  1633. else if (IS_I9XX(dev))
  1634. wm_params = &i915_wm_info;
  1635. else
  1636. wm_params = &i855_wm_info;
  1637. planea_entries = intel_calculate_wm(planea_clock, wm_params,
  1638. pixel_size, latency_ns);
  1639. planeb_entries = intel_calculate_wm(planeb_clock, wm_params,
  1640. pixel_size, latency_ns);
  1641. DRM_DEBUG("FIFO entries - A: %d, B: %d\n", planea_entries,
  1642. planeb_entries);
  1643. if (IS_I9XX(dev)) {
  1644. asize = dsparb & 0x7f;
  1645. bsize = (dsparb >> DSPARB_CSTART_SHIFT) & 0x7f;
  1646. } else {
  1647. asize = dsparb & 0x1ff;
  1648. bsize = (dsparb >> DSPARB_BEND_SHIFT) & 0x1ff;
  1649. }
  1650. DRM_DEBUG("FIFO size - A: %d, B: %d\n", asize, bsize);
  1651. /* Two extra entries for padding */
  1652. awm = asize - (planea_entries + 2);
  1653. bwm = bsize - (planeb_entries + 2);
  1654. /* Sanity check against potentially bad FIFO allocations */
  1655. if (awm <= 0) {
  1656. /* pipe is on but has too few FIFO entries */
  1657. if (planea_entries != 0)
  1658. DRM_DEBUG("plane A needs more FIFO entries\n");
  1659. awm = 1;
  1660. }
  1661. if (bwm <= 0) {
  1662. if (planeb_entries != 0)
  1663. DRM_DEBUG("plane B needs more FIFO entries\n");
  1664. bwm = 1;
  1665. }
  1666. /*
  1667. * Overlay gets an aggressive default since video jitter is bad.
  1668. */
  1669. cwm = 2;
  1670. /* Calc sr entries for one pipe configs */
  1671. if (!planea_clock || !planeb_clock) {
  1672. sr_clock = planea_clock ? planea_clock : planeb_clock;
  1673. line_time_us = (sr_hdisplay * 1000) / sr_clock;
  1674. sr_entries = (((latency_ns / line_time_us) + 1) * pixel_size *
  1675. sr_hdisplay) / 1000;
  1676. sr_entries = roundup(sr_entries / wm_params->cacheline_size, 1);
  1677. if (sr_entries < wm_params->fifo_size)
  1678. srwm = wm_params->fifo_size - sr_entries;
  1679. }
  1680. DRM_DEBUG("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  1681. awm, bwm, cwm, srwm);
  1682. fwater_lo = fwater_lo | ((bwm & 0x3f) << 16) | (awm & 0x3f);
  1683. fwater_hi = fwater_hi | (cwm & 0x1f);
  1684. I915_WRITE(FW_BLC, fwater_lo);
  1685. I915_WRITE(FW_BLC2, fwater_hi);
  1686. if (IS_I9XX(dev))
  1687. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN | (srwm & 0x3f));
  1688. }
  1689. static void i830_update_wm(struct drm_device *dev, int planea_clock,
  1690. int pixel_size)
  1691. {
  1692. struct drm_i915_private *dev_priv = dev->dev_private;
  1693. uint32_t dsparb = I915_READ(DSPARB);
  1694. uint32_t fwater_lo = I915_READ(FW_BLC) & MM_FIFO_WATERMARK;
  1695. unsigned int asize, awm;
  1696. int planea_entries;
  1697. planea_entries = intel_calculate_wm(planea_clock, &i830_wm_info,
  1698. pixel_size, latency_ns);
  1699. asize = dsparb & 0x7f;
  1700. awm = asize - planea_entries;
  1701. fwater_lo = fwater_lo | awm;
  1702. I915_WRITE(FW_BLC, fwater_lo);
  1703. }
  1704. /**
  1705. * intel_update_watermarks - update FIFO watermark values based on current modes
  1706. *
  1707. * Calculate watermark values for the various WM regs based on current mode
  1708. * and plane configuration.
  1709. *
  1710. * There are several cases to deal with here:
  1711. * - normal (i.e. non-self-refresh)
  1712. * - self-refresh (SR) mode
  1713. * - lines are large relative to FIFO size (buffer can hold up to 2)
  1714. * - lines are small relative to FIFO size (buffer can hold more than 2
  1715. * lines), so need to account for TLB latency
  1716. *
  1717. * The normal calculation is:
  1718. * watermark = dotclock * bytes per pixel * latency
  1719. * where latency is platform & configuration dependent (we assume pessimal
  1720. * values here).
  1721. *
  1722. * The SR calculation is:
  1723. * watermark = (trunc(latency/line time)+1) * surface width *
  1724. * bytes per pixel
  1725. * where
  1726. * line time = htotal / dotclock
  1727. * and latency is assumed to be high, as above.
  1728. *
  1729. * The final value programmed to the register should always be rounded up,
  1730. * and include an extra 2 entries to account for clock crossings.
  1731. *
  1732. * We don't use the sprite, so we can ignore that. And on Crestline we have
  1733. * to set the non-SR watermarks to 8.
  1734. */
  1735. static void intel_update_watermarks(struct drm_device *dev)
  1736. {
  1737. struct drm_crtc *crtc;
  1738. struct intel_crtc *intel_crtc;
  1739. int sr_hdisplay = 0;
  1740. unsigned long planea_clock = 0, planeb_clock = 0, sr_clock = 0;
  1741. int enabled = 0, pixel_size = 0;
  1742. if (DSPARB_HWCONTROL(dev))
  1743. return;
  1744. /* Get the clock config from both planes */
  1745. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1746. intel_crtc = to_intel_crtc(crtc);
  1747. if (crtc->enabled) {
  1748. enabled++;
  1749. if (intel_crtc->plane == 0) {
  1750. DRM_DEBUG("plane A (pipe %d) clock: %d\n",
  1751. intel_crtc->pipe, crtc->mode.clock);
  1752. planea_clock = crtc->mode.clock;
  1753. } else {
  1754. DRM_DEBUG("plane B (pipe %d) clock: %d\n",
  1755. intel_crtc->pipe, crtc->mode.clock);
  1756. planeb_clock = crtc->mode.clock;
  1757. }
  1758. sr_hdisplay = crtc->mode.hdisplay;
  1759. sr_clock = crtc->mode.clock;
  1760. if (crtc->fb)
  1761. pixel_size = crtc->fb->bits_per_pixel / 8;
  1762. else
  1763. pixel_size = 4; /* by default */
  1764. }
  1765. }
  1766. if (enabled <= 0)
  1767. return;
  1768. /* Single pipe configs can enable self refresh */
  1769. if (enabled == 1 && IS_IGD(dev))
  1770. igd_enable_cxsr(dev, sr_clock, pixel_size);
  1771. else if (IS_IGD(dev))
  1772. igd_disable_cxsr(dev);
  1773. if (IS_I965G(dev))
  1774. i965_update_wm(dev);
  1775. else if (IS_I9XX(dev) || IS_MOBILE(dev))
  1776. i9xx_update_wm(dev, planea_clock, planeb_clock, sr_hdisplay,
  1777. pixel_size);
  1778. else
  1779. i830_update_wm(dev, planea_clock, pixel_size);
  1780. }
  1781. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  1782. struct drm_display_mode *mode,
  1783. struct drm_display_mode *adjusted_mode,
  1784. int x, int y,
  1785. struct drm_framebuffer *old_fb)
  1786. {
  1787. struct drm_device *dev = crtc->dev;
  1788. struct drm_i915_private *dev_priv = dev->dev_private;
  1789. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1790. int pipe = intel_crtc->pipe;
  1791. int fp_reg = (pipe == 0) ? FPA0 : FPB0;
  1792. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  1793. int dpll_md_reg = (intel_crtc->pipe == 0) ? DPLL_A_MD : DPLL_B_MD;
  1794. int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
  1795. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  1796. int htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
  1797. int hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
  1798. int hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
  1799. int vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
  1800. int vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
  1801. int vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
  1802. int dspsize_reg = (pipe == 0) ? DSPASIZE : DSPBSIZE;
  1803. int dsppos_reg = (pipe == 0) ? DSPAPOS : DSPBPOS;
  1804. int pipesrc_reg = (pipe == 0) ? PIPEASRC : PIPEBSRC;
  1805. int refclk, num_outputs = 0;
  1806. intel_clock_t clock;
  1807. u32 dpll = 0, fp = 0, dspcntr, pipeconf;
  1808. bool ok, is_sdvo = false, is_dvo = false;
  1809. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  1810. struct drm_mode_config *mode_config = &dev->mode_config;
  1811. struct drm_connector *connector;
  1812. const intel_limit_t *limit;
  1813. int ret;
  1814. struct fdi_m_n m_n = {0};
  1815. int data_m1_reg = (pipe == 0) ? PIPEA_DATA_M1 : PIPEB_DATA_M1;
  1816. int data_n1_reg = (pipe == 0) ? PIPEA_DATA_N1 : PIPEB_DATA_N1;
  1817. int link_m1_reg = (pipe == 0) ? PIPEA_LINK_M1 : PIPEB_LINK_M1;
  1818. int link_n1_reg = (pipe == 0) ? PIPEA_LINK_N1 : PIPEB_LINK_N1;
  1819. int pch_fp_reg = (pipe == 0) ? PCH_FPA0 : PCH_FPB0;
  1820. int pch_dpll_reg = (pipe == 0) ? PCH_DPLL_A : PCH_DPLL_B;
  1821. int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
  1822. int lvds_reg = LVDS;
  1823. u32 temp;
  1824. int sdvo_pixel_multiply;
  1825. drm_vblank_pre_modeset(dev, pipe);
  1826. list_for_each_entry(connector, &mode_config->connector_list, head) {
  1827. struct intel_output *intel_output = to_intel_output(connector);
  1828. if (!connector->encoder || connector->encoder->crtc != crtc)
  1829. continue;
  1830. switch (intel_output->type) {
  1831. case INTEL_OUTPUT_LVDS:
  1832. is_lvds = true;
  1833. break;
  1834. case INTEL_OUTPUT_SDVO:
  1835. case INTEL_OUTPUT_HDMI:
  1836. is_sdvo = true;
  1837. if (intel_output->needs_tv_clock)
  1838. is_tv = true;
  1839. break;
  1840. case INTEL_OUTPUT_DVO:
  1841. is_dvo = true;
  1842. break;
  1843. case INTEL_OUTPUT_TVOUT:
  1844. is_tv = true;
  1845. break;
  1846. case INTEL_OUTPUT_ANALOG:
  1847. is_crt = true;
  1848. break;
  1849. case INTEL_OUTPUT_DISPLAYPORT:
  1850. is_dp = true;
  1851. break;
  1852. }
  1853. num_outputs++;
  1854. }
  1855. if (is_lvds && dev_priv->lvds_use_ssc && num_outputs < 2) {
  1856. refclk = dev_priv->lvds_ssc_freq * 1000;
  1857. DRM_DEBUG("using SSC reference clock of %d MHz\n", refclk / 1000);
  1858. } else if (IS_I9XX(dev)) {
  1859. refclk = 96000;
  1860. if (IS_IGDNG(dev))
  1861. refclk = 120000; /* 120Mhz refclk */
  1862. } else {
  1863. refclk = 48000;
  1864. }
  1865. /*
  1866. * Returns a set of divisors for the desired target clock with the given
  1867. * refclk, or FALSE. The returned values represent the clock equation:
  1868. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  1869. */
  1870. limit = intel_limit(crtc);
  1871. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
  1872. if (!ok) {
  1873. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  1874. drm_vblank_post_modeset(dev, pipe);
  1875. return -EINVAL;
  1876. }
  1877. /* SDVO TV has fixed PLL values depend on its clock range,
  1878. this mirrors vbios setting. */
  1879. if (is_sdvo && is_tv) {
  1880. if (adjusted_mode->clock >= 100000
  1881. && adjusted_mode->clock < 140500) {
  1882. clock.p1 = 2;
  1883. clock.p2 = 10;
  1884. clock.n = 3;
  1885. clock.m1 = 16;
  1886. clock.m2 = 8;
  1887. } else if (adjusted_mode->clock >= 140500
  1888. && adjusted_mode->clock <= 200000) {
  1889. clock.p1 = 1;
  1890. clock.p2 = 10;
  1891. clock.n = 6;
  1892. clock.m1 = 12;
  1893. clock.m2 = 8;
  1894. }
  1895. }
  1896. /* FDI link */
  1897. if (IS_IGDNG(dev))
  1898. igdng_compute_m_n(3, 4, /* lane num 4 */
  1899. adjusted_mode->clock,
  1900. 270000, /* lane clock */
  1901. &m_n);
  1902. if (IS_IGD(dev))
  1903. fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
  1904. else
  1905. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  1906. if (!IS_IGDNG(dev))
  1907. dpll = DPLL_VGA_MODE_DIS;
  1908. if (IS_I9XX(dev)) {
  1909. if (is_lvds)
  1910. dpll |= DPLLB_MODE_LVDS;
  1911. else
  1912. dpll |= DPLLB_MODE_DAC_SERIAL;
  1913. if (is_sdvo) {
  1914. dpll |= DPLL_DVO_HIGH_SPEED;
  1915. sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
  1916. if (IS_I945G(dev) || IS_I945GM(dev))
  1917. dpll |= (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  1918. else if (IS_IGDNG(dev))
  1919. dpll |= (sdvo_pixel_multiply - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  1920. }
  1921. if (is_dp)
  1922. dpll |= DPLL_DVO_HIGH_SPEED;
  1923. /* compute bitmask from p1 value */
  1924. if (IS_IGD(dev))
  1925. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_IGD;
  1926. else {
  1927. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  1928. /* also FPA1 */
  1929. if (IS_IGDNG(dev))
  1930. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  1931. }
  1932. switch (clock.p2) {
  1933. case 5:
  1934. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  1935. break;
  1936. case 7:
  1937. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  1938. break;
  1939. case 10:
  1940. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  1941. break;
  1942. case 14:
  1943. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  1944. break;
  1945. }
  1946. if (IS_I965G(dev) && !IS_IGDNG(dev))
  1947. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  1948. } else {
  1949. if (is_lvds) {
  1950. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  1951. } else {
  1952. if (clock.p1 == 2)
  1953. dpll |= PLL_P1_DIVIDE_BY_TWO;
  1954. else
  1955. dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  1956. if (clock.p2 == 4)
  1957. dpll |= PLL_P2_DIVIDE_BY_4;
  1958. }
  1959. }
  1960. if (is_sdvo && is_tv)
  1961. dpll |= PLL_REF_INPUT_TVCLKINBC;
  1962. else if (is_tv)
  1963. /* XXX: just matching BIOS for now */
  1964. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  1965. dpll |= 3;
  1966. else if (is_lvds && dev_priv->lvds_use_ssc && num_outputs < 2)
  1967. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  1968. else
  1969. dpll |= PLL_REF_INPUT_DREFCLK;
  1970. /* setup pipeconf */
  1971. pipeconf = I915_READ(pipeconf_reg);
  1972. /* Set up the display plane register */
  1973. dspcntr = DISPPLANE_GAMMA_ENABLE;
  1974. /* IGDNG's plane is forced to pipe, bit 24 is to
  1975. enable color space conversion */
  1976. if (!IS_IGDNG(dev)) {
  1977. if (pipe == 0)
  1978. dspcntr |= DISPPLANE_SEL_PIPE_A;
  1979. else
  1980. dspcntr |= DISPPLANE_SEL_PIPE_B;
  1981. }
  1982. if (pipe == 0 && !IS_I965G(dev)) {
  1983. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  1984. * core speed.
  1985. *
  1986. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  1987. * pipe == 0 check?
  1988. */
  1989. if (mode->clock > intel_get_core_clock_speed(dev) * 9 / 10)
  1990. pipeconf |= PIPEACONF_DOUBLE_WIDE;
  1991. else
  1992. pipeconf &= ~PIPEACONF_DOUBLE_WIDE;
  1993. }
  1994. dspcntr |= DISPLAY_PLANE_ENABLE;
  1995. pipeconf |= PIPEACONF_ENABLE;
  1996. dpll |= DPLL_VCO_ENABLE;
  1997. /* Disable the panel fitter if it was on our pipe */
  1998. if (!IS_IGDNG(dev) && intel_panel_fitter_pipe(dev) == pipe)
  1999. I915_WRITE(PFIT_CONTROL, 0);
  2000. DRM_DEBUG("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  2001. drm_mode_debug_printmodeline(mode);
  2002. /* assign to IGDNG registers */
  2003. if (IS_IGDNG(dev)) {
  2004. fp_reg = pch_fp_reg;
  2005. dpll_reg = pch_dpll_reg;
  2006. }
  2007. if (dpll & DPLL_VCO_ENABLE) {
  2008. I915_WRITE(fp_reg, fp);
  2009. I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
  2010. I915_READ(dpll_reg);
  2011. udelay(150);
  2012. }
  2013. if (IS_IGDNG(dev)) {
  2014. /* enable PCH clock reference source */
  2015. /* XXX need to change the setting for other outputs */
  2016. u32 temp;
  2017. temp = I915_READ(PCH_DREF_CONTROL);
  2018. temp &= ~DREF_NONSPREAD_SOURCE_MASK;
  2019. temp |= DREF_NONSPREAD_CK505_ENABLE;
  2020. temp &= ~DREF_SSC_SOURCE_MASK;
  2021. temp |= DREF_SSC_SOURCE_ENABLE;
  2022. temp &= ~DREF_SSC1_ENABLE;
  2023. /* if no eDP, disable source output to CPU */
  2024. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  2025. temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  2026. I915_WRITE(PCH_DREF_CONTROL, temp);
  2027. }
  2028. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  2029. * This is an exception to the general rule that mode_set doesn't turn
  2030. * things on.
  2031. */
  2032. if (is_lvds) {
  2033. u32 lvds;
  2034. if (IS_IGDNG(dev))
  2035. lvds_reg = PCH_LVDS;
  2036. lvds = I915_READ(lvds_reg);
  2037. lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP | LVDS_PIPEB_SELECT;
  2038. /* Set the B0-B3 data pairs corresponding to whether we're going to
  2039. * set the DPLLs for dual-channel mode or not.
  2040. */
  2041. if (clock.p2 == 7)
  2042. lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  2043. else
  2044. lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  2045. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  2046. * appropriately here, but we need to look more thoroughly into how
  2047. * panels behave in the two modes.
  2048. */
  2049. I915_WRITE(lvds_reg, lvds);
  2050. I915_READ(lvds_reg);
  2051. }
  2052. if (is_dp)
  2053. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  2054. I915_WRITE(fp_reg, fp);
  2055. I915_WRITE(dpll_reg, dpll);
  2056. I915_READ(dpll_reg);
  2057. /* Wait for the clocks to stabilize. */
  2058. udelay(150);
  2059. if (IS_I965G(dev) && !IS_IGDNG(dev)) {
  2060. sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
  2061. I915_WRITE(dpll_md_reg, (0 << DPLL_MD_UDI_DIVIDER_SHIFT) |
  2062. ((sdvo_pixel_multiply - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT));
  2063. } else {
  2064. /* write it again -- the BIOS does, after all */
  2065. I915_WRITE(dpll_reg, dpll);
  2066. }
  2067. I915_READ(dpll_reg);
  2068. /* Wait for the clocks to stabilize. */
  2069. udelay(150);
  2070. I915_WRITE(htot_reg, (adjusted_mode->crtc_hdisplay - 1) |
  2071. ((adjusted_mode->crtc_htotal - 1) << 16));
  2072. I915_WRITE(hblank_reg, (adjusted_mode->crtc_hblank_start - 1) |
  2073. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  2074. I915_WRITE(hsync_reg, (adjusted_mode->crtc_hsync_start - 1) |
  2075. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  2076. I915_WRITE(vtot_reg, (adjusted_mode->crtc_vdisplay - 1) |
  2077. ((adjusted_mode->crtc_vtotal - 1) << 16));
  2078. I915_WRITE(vblank_reg, (adjusted_mode->crtc_vblank_start - 1) |
  2079. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  2080. I915_WRITE(vsync_reg, (adjusted_mode->crtc_vsync_start - 1) |
  2081. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  2082. /* pipesrc and dspsize control the size that is scaled from, which should
  2083. * always be the user's requested size.
  2084. */
  2085. if (!IS_IGDNG(dev)) {
  2086. I915_WRITE(dspsize_reg, ((mode->vdisplay - 1) << 16) |
  2087. (mode->hdisplay - 1));
  2088. I915_WRITE(dsppos_reg, 0);
  2089. }
  2090. I915_WRITE(pipesrc_reg, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  2091. if (IS_IGDNG(dev)) {
  2092. I915_WRITE(data_m1_reg, TU_SIZE(m_n.tu) | m_n.gmch_m);
  2093. I915_WRITE(data_n1_reg, TU_SIZE(m_n.tu) | m_n.gmch_n);
  2094. I915_WRITE(link_m1_reg, m_n.link_m);
  2095. I915_WRITE(link_n1_reg, m_n.link_n);
  2096. /* enable FDI RX PLL too */
  2097. temp = I915_READ(fdi_rx_reg);
  2098. I915_WRITE(fdi_rx_reg, temp | FDI_RX_PLL_ENABLE);
  2099. udelay(200);
  2100. }
  2101. I915_WRITE(pipeconf_reg, pipeconf);
  2102. I915_READ(pipeconf_reg);
  2103. intel_wait_for_vblank(dev);
  2104. I915_WRITE(dspcntr_reg, dspcntr);
  2105. /* Flush the plane changes */
  2106. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  2107. intel_update_watermarks(dev);
  2108. drm_vblank_post_modeset(dev, pipe);
  2109. return ret;
  2110. }
  2111. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  2112. void intel_crtc_load_lut(struct drm_crtc *crtc)
  2113. {
  2114. struct drm_device *dev = crtc->dev;
  2115. struct drm_i915_private *dev_priv = dev->dev_private;
  2116. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2117. int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
  2118. int i;
  2119. /* The clocks have to be on to load the palette. */
  2120. if (!crtc->enabled)
  2121. return;
  2122. /* use legacy palette for IGDNG */
  2123. if (IS_IGDNG(dev))
  2124. palreg = (intel_crtc->pipe == 0) ? LGC_PALETTE_A :
  2125. LGC_PALETTE_B;
  2126. for (i = 0; i < 256; i++) {
  2127. I915_WRITE(palreg + 4 * i,
  2128. (intel_crtc->lut_r[i] << 16) |
  2129. (intel_crtc->lut_g[i] << 8) |
  2130. intel_crtc->lut_b[i]);
  2131. }
  2132. }
  2133. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  2134. struct drm_file *file_priv,
  2135. uint32_t handle,
  2136. uint32_t width, uint32_t height)
  2137. {
  2138. struct drm_device *dev = crtc->dev;
  2139. struct drm_i915_private *dev_priv = dev->dev_private;
  2140. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2141. struct drm_gem_object *bo;
  2142. struct drm_i915_gem_object *obj_priv;
  2143. int pipe = intel_crtc->pipe;
  2144. uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
  2145. uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
  2146. uint32_t temp = I915_READ(control);
  2147. size_t addr;
  2148. int ret;
  2149. DRM_DEBUG("\n");
  2150. /* if we want to turn off the cursor ignore width and height */
  2151. if (!handle) {
  2152. DRM_DEBUG("cursor off\n");
  2153. if (IS_MOBILE(dev) || IS_I9XX(dev)) {
  2154. temp &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  2155. temp |= CURSOR_MODE_DISABLE;
  2156. } else {
  2157. temp &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  2158. }
  2159. addr = 0;
  2160. bo = NULL;
  2161. mutex_lock(&dev->struct_mutex);
  2162. goto finish;
  2163. }
  2164. /* Currently we only support 64x64 cursors */
  2165. if (width != 64 || height != 64) {
  2166. DRM_ERROR("we currently only support 64x64 cursors\n");
  2167. return -EINVAL;
  2168. }
  2169. bo = drm_gem_object_lookup(dev, file_priv, handle);
  2170. if (!bo)
  2171. return -ENOENT;
  2172. obj_priv = bo->driver_private;
  2173. if (bo->size < width * height * 4) {
  2174. DRM_ERROR("buffer is to small\n");
  2175. ret = -ENOMEM;
  2176. goto fail;
  2177. }
  2178. /* we only need to pin inside GTT if cursor is non-phy */
  2179. mutex_lock(&dev->struct_mutex);
  2180. if (!dev_priv->cursor_needs_physical) {
  2181. ret = i915_gem_object_pin(bo, PAGE_SIZE);
  2182. if (ret) {
  2183. DRM_ERROR("failed to pin cursor bo\n");
  2184. goto fail_locked;
  2185. }
  2186. addr = obj_priv->gtt_offset;
  2187. } else {
  2188. ret = i915_gem_attach_phys_object(dev, bo, (pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1);
  2189. if (ret) {
  2190. DRM_ERROR("failed to attach phys object\n");
  2191. goto fail_locked;
  2192. }
  2193. addr = obj_priv->phys_obj->handle->busaddr;
  2194. }
  2195. if (!IS_I9XX(dev))
  2196. I915_WRITE(CURSIZE, (height << 12) | width);
  2197. /* Hooray for CUR*CNTR differences */
  2198. if (IS_MOBILE(dev) || IS_I9XX(dev)) {
  2199. temp &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  2200. temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  2201. temp |= (pipe << 28); /* Connect to correct pipe */
  2202. } else {
  2203. temp &= ~(CURSOR_FORMAT_MASK);
  2204. temp |= CURSOR_ENABLE;
  2205. temp |= CURSOR_FORMAT_ARGB | CURSOR_GAMMA_ENABLE;
  2206. }
  2207. finish:
  2208. I915_WRITE(control, temp);
  2209. I915_WRITE(base, addr);
  2210. if (intel_crtc->cursor_bo) {
  2211. if (dev_priv->cursor_needs_physical) {
  2212. if (intel_crtc->cursor_bo != bo)
  2213. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  2214. } else
  2215. i915_gem_object_unpin(intel_crtc->cursor_bo);
  2216. drm_gem_object_unreference(intel_crtc->cursor_bo);
  2217. }
  2218. mutex_unlock(&dev->struct_mutex);
  2219. intel_crtc->cursor_addr = addr;
  2220. intel_crtc->cursor_bo = bo;
  2221. return 0;
  2222. fail:
  2223. mutex_lock(&dev->struct_mutex);
  2224. fail_locked:
  2225. drm_gem_object_unreference(bo);
  2226. mutex_unlock(&dev->struct_mutex);
  2227. return ret;
  2228. }
  2229. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  2230. {
  2231. struct drm_device *dev = crtc->dev;
  2232. struct drm_i915_private *dev_priv = dev->dev_private;
  2233. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2234. int pipe = intel_crtc->pipe;
  2235. uint32_t temp = 0;
  2236. uint32_t adder;
  2237. if (x < 0) {
  2238. temp |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  2239. x = -x;
  2240. }
  2241. if (y < 0) {
  2242. temp |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  2243. y = -y;
  2244. }
  2245. temp |= x << CURSOR_X_SHIFT;
  2246. temp |= y << CURSOR_Y_SHIFT;
  2247. adder = intel_crtc->cursor_addr;
  2248. I915_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
  2249. I915_WRITE((pipe == 0) ? CURABASE : CURBBASE, adder);
  2250. return 0;
  2251. }
  2252. /** Sets the color ramps on behalf of RandR */
  2253. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  2254. u16 blue, int regno)
  2255. {
  2256. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2257. intel_crtc->lut_r[regno] = red >> 8;
  2258. intel_crtc->lut_g[regno] = green >> 8;
  2259. intel_crtc->lut_b[regno] = blue >> 8;
  2260. }
  2261. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  2262. u16 *blue, uint32_t size)
  2263. {
  2264. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2265. int i;
  2266. if (size != 256)
  2267. return;
  2268. for (i = 0; i < 256; i++) {
  2269. intel_crtc->lut_r[i] = red[i] >> 8;
  2270. intel_crtc->lut_g[i] = green[i] >> 8;
  2271. intel_crtc->lut_b[i] = blue[i] >> 8;
  2272. }
  2273. intel_crtc_load_lut(crtc);
  2274. }
  2275. /**
  2276. * Get a pipe with a simple mode set on it for doing load-based monitor
  2277. * detection.
  2278. *
  2279. * It will be up to the load-detect code to adjust the pipe as appropriate for
  2280. * its requirements. The pipe will be connected to no other outputs.
  2281. *
  2282. * Currently this code will only succeed if there is a pipe with no outputs
  2283. * configured for it. In the future, it could choose to temporarily disable
  2284. * some outputs to free up a pipe for its use.
  2285. *
  2286. * \return crtc, or NULL if no pipes are available.
  2287. */
  2288. /* VESA 640x480x72Hz mode to set on the pipe */
  2289. static struct drm_display_mode load_detect_mode = {
  2290. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  2291. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  2292. };
  2293. struct drm_crtc *intel_get_load_detect_pipe(struct intel_output *intel_output,
  2294. struct drm_display_mode *mode,
  2295. int *dpms_mode)
  2296. {
  2297. struct intel_crtc *intel_crtc;
  2298. struct drm_crtc *possible_crtc;
  2299. struct drm_crtc *supported_crtc =NULL;
  2300. struct drm_encoder *encoder = &intel_output->enc;
  2301. struct drm_crtc *crtc = NULL;
  2302. struct drm_device *dev = encoder->dev;
  2303. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2304. struct drm_crtc_helper_funcs *crtc_funcs;
  2305. int i = -1;
  2306. /*
  2307. * Algorithm gets a little messy:
  2308. * - if the connector already has an assigned crtc, use it (but make
  2309. * sure it's on first)
  2310. * - try to find the first unused crtc that can drive this connector,
  2311. * and use that if we find one
  2312. * - if there are no unused crtcs available, try to use the first
  2313. * one we found that supports the connector
  2314. */
  2315. /* See if we already have a CRTC for this connector */
  2316. if (encoder->crtc) {
  2317. crtc = encoder->crtc;
  2318. /* Make sure the crtc and connector are running */
  2319. intel_crtc = to_intel_crtc(crtc);
  2320. *dpms_mode = intel_crtc->dpms_mode;
  2321. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  2322. crtc_funcs = crtc->helper_private;
  2323. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  2324. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  2325. }
  2326. return crtc;
  2327. }
  2328. /* Find an unused one (if possible) */
  2329. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  2330. i++;
  2331. if (!(encoder->possible_crtcs & (1 << i)))
  2332. continue;
  2333. if (!possible_crtc->enabled) {
  2334. crtc = possible_crtc;
  2335. break;
  2336. }
  2337. if (!supported_crtc)
  2338. supported_crtc = possible_crtc;
  2339. }
  2340. /*
  2341. * If we didn't find an unused CRTC, don't use any.
  2342. */
  2343. if (!crtc) {
  2344. return NULL;
  2345. }
  2346. encoder->crtc = crtc;
  2347. intel_output->base.encoder = encoder;
  2348. intel_output->load_detect_temp = true;
  2349. intel_crtc = to_intel_crtc(crtc);
  2350. *dpms_mode = intel_crtc->dpms_mode;
  2351. if (!crtc->enabled) {
  2352. if (!mode)
  2353. mode = &load_detect_mode;
  2354. drm_crtc_helper_set_mode(crtc, mode, 0, 0, crtc->fb);
  2355. } else {
  2356. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  2357. crtc_funcs = crtc->helper_private;
  2358. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  2359. }
  2360. /* Add this connector to the crtc */
  2361. encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
  2362. encoder_funcs->commit(encoder);
  2363. }
  2364. /* let the connector get through one full cycle before testing */
  2365. intel_wait_for_vblank(dev);
  2366. return crtc;
  2367. }
  2368. void intel_release_load_detect_pipe(struct intel_output *intel_output, int dpms_mode)
  2369. {
  2370. struct drm_encoder *encoder = &intel_output->enc;
  2371. struct drm_device *dev = encoder->dev;
  2372. struct drm_crtc *crtc = encoder->crtc;
  2373. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2374. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  2375. if (intel_output->load_detect_temp) {
  2376. encoder->crtc = NULL;
  2377. intel_output->base.encoder = NULL;
  2378. intel_output->load_detect_temp = false;
  2379. crtc->enabled = drm_helper_crtc_in_use(crtc);
  2380. drm_helper_disable_unused_functions(dev);
  2381. }
  2382. /* Switch crtc and output back off if necessary */
  2383. if (crtc->enabled && dpms_mode != DRM_MODE_DPMS_ON) {
  2384. if (encoder->crtc == crtc)
  2385. encoder_funcs->dpms(encoder, dpms_mode);
  2386. crtc_funcs->dpms(crtc, dpms_mode);
  2387. }
  2388. }
  2389. /* Returns the clock of the currently programmed mode of the given pipe. */
  2390. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  2391. {
  2392. struct drm_i915_private *dev_priv = dev->dev_private;
  2393. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2394. int pipe = intel_crtc->pipe;
  2395. u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
  2396. u32 fp;
  2397. intel_clock_t clock;
  2398. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  2399. fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
  2400. else
  2401. fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
  2402. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  2403. if (IS_IGD(dev)) {
  2404. clock.n = ffs((fp & FP_N_IGD_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  2405. clock.m2 = (fp & FP_M2_IGD_DIV_MASK) >> FP_M2_DIV_SHIFT;
  2406. } else {
  2407. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  2408. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  2409. }
  2410. if (IS_I9XX(dev)) {
  2411. if (IS_IGD(dev))
  2412. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_IGD) >>
  2413. DPLL_FPA01_P1_POST_DIV_SHIFT_IGD);
  2414. else
  2415. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  2416. DPLL_FPA01_P1_POST_DIV_SHIFT);
  2417. switch (dpll & DPLL_MODE_MASK) {
  2418. case DPLLB_MODE_DAC_SERIAL:
  2419. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  2420. 5 : 10;
  2421. break;
  2422. case DPLLB_MODE_LVDS:
  2423. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  2424. 7 : 14;
  2425. break;
  2426. default:
  2427. DRM_DEBUG("Unknown DPLL mode %08x in programmed "
  2428. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  2429. return 0;
  2430. }
  2431. /* XXX: Handle the 100Mhz refclk */
  2432. intel_clock(dev, 96000, &clock);
  2433. } else {
  2434. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  2435. if (is_lvds) {
  2436. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  2437. DPLL_FPA01_P1_POST_DIV_SHIFT);
  2438. clock.p2 = 14;
  2439. if ((dpll & PLL_REF_INPUT_MASK) ==
  2440. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  2441. /* XXX: might not be 66MHz */
  2442. intel_clock(dev, 66000, &clock);
  2443. } else
  2444. intel_clock(dev, 48000, &clock);
  2445. } else {
  2446. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  2447. clock.p1 = 2;
  2448. else {
  2449. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  2450. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  2451. }
  2452. if (dpll & PLL_P2_DIVIDE_BY_4)
  2453. clock.p2 = 4;
  2454. else
  2455. clock.p2 = 2;
  2456. intel_clock(dev, 48000, &clock);
  2457. }
  2458. }
  2459. /* XXX: It would be nice to validate the clocks, but we can't reuse
  2460. * i830PllIsValid() because it relies on the xf86_config connector
  2461. * configuration being accurate, which it isn't necessarily.
  2462. */
  2463. return clock.dot;
  2464. }
  2465. /** Returns the currently programmed mode of the given pipe. */
  2466. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  2467. struct drm_crtc *crtc)
  2468. {
  2469. struct drm_i915_private *dev_priv = dev->dev_private;
  2470. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2471. int pipe = intel_crtc->pipe;
  2472. struct drm_display_mode *mode;
  2473. int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
  2474. int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
  2475. int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
  2476. int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
  2477. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  2478. if (!mode)
  2479. return NULL;
  2480. mode->clock = intel_crtc_clock_get(dev, crtc);
  2481. mode->hdisplay = (htot & 0xffff) + 1;
  2482. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  2483. mode->hsync_start = (hsync & 0xffff) + 1;
  2484. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  2485. mode->vdisplay = (vtot & 0xffff) + 1;
  2486. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  2487. mode->vsync_start = (vsync & 0xffff) + 1;
  2488. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  2489. drm_mode_set_name(mode);
  2490. drm_mode_set_crtcinfo(mode, 0);
  2491. return mode;
  2492. }
  2493. static void intel_crtc_destroy(struct drm_crtc *crtc)
  2494. {
  2495. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2496. if (intel_crtc->mode_set.mode)
  2497. drm_mode_destroy(crtc->dev, intel_crtc->mode_set.mode);
  2498. drm_crtc_cleanup(crtc);
  2499. kfree(intel_crtc);
  2500. }
  2501. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  2502. .dpms = intel_crtc_dpms,
  2503. .mode_fixup = intel_crtc_mode_fixup,
  2504. .mode_set = intel_crtc_mode_set,
  2505. .mode_set_base = intel_pipe_set_base,
  2506. .prepare = intel_crtc_prepare,
  2507. .commit = intel_crtc_commit,
  2508. };
  2509. static const struct drm_crtc_funcs intel_crtc_funcs = {
  2510. .cursor_set = intel_crtc_cursor_set,
  2511. .cursor_move = intel_crtc_cursor_move,
  2512. .gamma_set = intel_crtc_gamma_set,
  2513. .set_config = drm_crtc_helper_set_config,
  2514. .destroy = intel_crtc_destroy,
  2515. };
  2516. static void intel_crtc_init(struct drm_device *dev, int pipe)
  2517. {
  2518. struct intel_crtc *intel_crtc;
  2519. int i;
  2520. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  2521. if (intel_crtc == NULL)
  2522. return;
  2523. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  2524. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  2525. intel_crtc->pipe = pipe;
  2526. intel_crtc->plane = pipe;
  2527. for (i = 0; i < 256; i++) {
  2528. intel_crtc->lut_r[i] = i;
  2529. intel_crtc->lut_g[i] = i;
  2530. intel_crtc->lut_b[i] = i;
  2531. }
  2532. intel_crtc->cursor_addr = 0;
  2533. intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
  2534. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  2535. intel_crtc->mode_set.crtc = &intel_crtc->base;
  2536. intel_crtc->mode_set.connectors = (struct drm_connector **)(intel_crtc + 1);
  2537. intel_crtc->mode_set.num_connectors = 0;
  2538. if (i915_fbpercrtc) {
  2539. }
  2540. }
  2541. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  2542. struct drm_file *file_priv)
  2543. {
  2544. drm_i915_private_t *dev_priv = dev->dev_private;
  2545. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  2546. struct drm_crtc *crtc = NULL;
  2547. int pipe = -1;
  2548. if (!dev_priv) {
  2549. DRM_ERROR("called with no initialization\n");
  2550. return -EINVAL;
  2551. }
  2552. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2553. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2554. if (crtc->base.id == pipe_from_crtc_id->crtc_id) {
  2555. pipe = intel_crtc->pipe;
  2556. break;
  2557. }
  2558. }
  2559. if (pipe == -1) {
  2560. DRM_ERROR("no such CRTC id\n");
  2561. return -EINVAL;
  2562. }
  2563. pipe_from_crtc_id->pipe = pipe;
  2564. return 0;
  2565. }
  2566. struct drm_crtc *intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
  2567. {
  2568. struct drm_crtc *crtc = NULL;
  2569. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2570. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2571. if (intel_crtc->pipe == pipe)
  2572. break;
  2573. }
  2574. return crtc;
  2575. }
  2576. static int intel_connector_clones(struct drm_device *dev, int type_mask)
  2577. {
  2578. int index_mask = 0;
  2579. struct drm_connector *connector;
  2580. int entry = 0;
  2581. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2582. struct intel_output *intel_output = to_intel_output(connector);
  2583. if (type_mask & (1 << intel_output->type))
  2584. index_mask |= (1 << entry);
  2585. entry++;
  2586. }
  2587. return index_mask;
  2588. }
  2589. static void intel_setup_outputs(struct drm_device *dev)
  2590. {
  2591. struct drm_i915_private *dev_priv = dev->dev_private;
  2592. struct drm_connector *connector;
  2593. intel_crt_init(dev);
  2594. /* Set up integrated LVDS */
  2595. if (IS_MOBILE(dev) && !IS_I830(dev))
  2596. intel_lvds_init(dev);
  2597. if (IS_IGDNG(dev)) {
  2598. int found;
  2599. if (I915_READ(HDMIB) & PORT_DETECTED) {
  2600. /* check SDVOB */
  2601. /* found = intel_sdvo_init(dev, HDMIB); */
  2602. found = 0;
  2603. if (!found)
  2604. intel_hdmi_init(dev, HDMIB);
  2605. }
  2606. if (I915_READ(HDMIC) & PORT_DETECTED)
  2607. intel_hdmi_init(dev, HDMIC);
  2608. if (I915_READ(HDMID) & PORT_DETECTED)
  2609. intel_hdmi_init(dev, HDMID);
  2610. } else if (IS_I9XX(dev)) {
  2611. int found;
  2612. u32 reg;
  2613. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  2614. found = intel_sdvo_init(dev, SDVOB);
  2615. if (!found && SUPPORTS_INTEGRATED_HDMI(dev))
  2616. intel_hdmi_init(dev, SDVOB);
  2617. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  2618. intel_dp_init(dev, DP_B);
  2619. }
  2620. /* Before G4X SDVOC doesn't have its own detect register */
  2621. if (IS_G4X(dev))
  2622. reg = SDVOC;
  2623. else
  2624. reg = SDVOB;
  2625. if (I915_READ(reg) & SDVO_DETECTED) {
  2626. found = intel_sdvo_init(dev, SDVOC);
  2627. if (!found && SUPPORTS_INTEGRATED_HDMI(dev))
  2628. intel_hdmi_init(dev, SDVOC);
  2629. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  2630. intel_dp_init(dev, DP_C);
  2631. }
  2632. if (SUPPORTS_INTEGRATED_DP(dev) && (I915_READ(DP_D) & DP_DETECTED))
  2633. intel_dp_init(dev, DP_D);
  2634. } else
  2635. intel_dvo_init(dev);
  2636. if (IS_I9XX(dev) && IS_MOBILE(dev) && !IS_IGDNG(dev))
  2637. intel_tv_init(dev);
  2638. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2639. struct intel_output *intel_output = to_intel_output(connector);
  2640. struct drm_encoder *encoder = &intel_output->enc;
  2641. int crtc_mask = 0, clone_mask = 0;
  2642. /* valid crtcs */
  2643. switch(intel_output->type) {
  2644. case INTEL_OUTPUT_HDMI:
  2645. crtc_mask = ((1 << 0)|
  2646. (1 << 1));
  2647. clone_mask = ((1 << INTEL_OUTPUT_HDMI));
  2648. break;
  2649. case INTEL_OUTPUT_DVO:
  2650. case INTEL_OUTPUT_SDVO:
  2651. crtc_mask = ((1 << 0)|
  2652. (1 << 1));
  2653. clone_mask = ((1 << INTEL_OUTPUT_ANALOG) |
  2654. (1 << INTEL_OUTPUT_DVO) |
  2655. (1 << INTEL_OUTPUT_SDVO));
  2656. break;
  2657. case INTEL_OUTPUT_ANALOG:
  2658. crtc_mask = ((1 << 0)|
  2659. (1 << 1));
  2660. clone_mask = ((1 << INTEL_OUTPUT_ANALOG) |
  2661. (1 << INTEL_OUTPUT_DVO) |
  2662. (1 << INTEL_OUTPUT_SDVO));
  2663. break;
  2664. case INTEL_OUTPUT_LVDS:
  2665. crtc_mask = (1 << 1);
  2666. clone_mask = (1 << INTEL_OUTPUT_LVDS);
  2667. break;
  2668. case INTEL_OUTPUT_TVOUT:
  2669. crtc_mask = ((1 << 0) |
  2670. (1 << 1));
  2671. clone_mask = (1 << INTEL_OUTPUT_TVOUT);
  2672. break;
  2673. case INTEL_OUTPUT_DISPLAYPORT:
  2674. crtc_mask = ((1 << 0) |
  2675. (1 << 1));
  2676. clone_mask = (1 << INTEL_OUTPUT_DISPLAYPORT);
  2677. break;
  2678. }
  2679. encoder->possible_crtcs = crtc_mask;
  2680. encoder->possible_clones = intel_connector_clones(dev, clone_mask);
  2681. }
  2682. }
  2683. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  2684. {
  2685. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2686. struct drm_device *dev = fb->dev;
  2687. if (fb->fbdev)
  2688. intelfb_remove(dev, fb);
  2689. drm_framebuffer_cleanup(fb);
  2690. mutex_lock(&dev->struct_mutex);
  2691. drm_gem_object_unreference(intel_fb->obj);
  2692. mutex_unlock(&dev->struct_mutex);
  2693. kfree(intel_fb);
  2694. }
  2695. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  2696. struct drm_file *file_priv,
  2697. unsigned int *handle)
  2698. {
  2699. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2700. struct drm_gem_object *object = intel_fb->obj;
  2701. return drm_gem_handle_create(file_priv, object, handle);
  2702. }
  2703. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  2704. .destroy = intel_user_framebuffer_destroy,
  2705. .create_handle = intel_user_framebuffer_create_handle,
  2706. };
  2707. int intel_framebuffer_create(struct drm_device *dev,
  2708. struct drm_mode_fb_cmd *mode_cmd,
  2709. struct drm_framebuffer **fb,
  2710. struct drm_gem_object *obj)
  2711. {
  2712. struct intel_framebuffer *intel_fb;
  2713. int ret;
  2714. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  2715. if (!intel_fb)
  2716. return -ENOMEM;
  2717. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  2718. if (ret) {
  2719. DRM_ERROR("framebuffer init failed %d\n", ret);
  2720. return ret;
  2721. }
  2722. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  2723. intel_fb->obj = obj;
  2724. *fb = &intel_fb->base;
  2725. return 0;
  2726. }
  2727. static struct drm_framebuffer *
  2728. intel_user_framebuffer_create(struct drm_device *dev,
  2729. struct drm_file *filp,
  2730. struct drm_mode_fb_cmd *mode_cmd)
  2731. {
  2732. struct drm_gem_object *obj;
  2733. struct drm_framebuffer *fb;
  2734. int ret;
  2735. obj = drm_gem_object_lookup(dev, filp, mode_cmd->handle);
  2736. if (!obj)
  2737. return NULL;
  2738. ret = intel_framebuffer_create(dev, mode_cmd, &fb, obj);
  2739. if (ret) {
  2740. mutex_lock(&dev->struct_mutex);
  2741. drm_gem_object_unreference(obj);
  2742. mutex_unlock(&dev->struct_mutex);
  2743. return NULL;
  2744. }
  2745. return fb;
  2746. }
  2747. static const struct drm_mode_config_funcs intel_mode_funcs = {
  2748. .fb_create = intel_user_framebuffer_create,
  2749. .fb_changed = intelfb_probe,
  2750. };
  2751. void intel_modeset_init(struct drm_device *dev)
  2752. {
  2753. int num_pipe;
  2754. int i;
  2755. drm_mode_config_init(dev);
  2756. dev->mode_config.min_width = 0;
  2757. dev->mode_config.min_height = 0;
  2758. dev->mode_config.funcs = (void *)&intel_mode_funcs;
  2759. if (IS_I965G(dev)) {
  2760. dev->mode_config.max_width = 8192;
  2761. dev->mode_config.max_height = 8192;
  2762. } else {
  2763. dev->mode_config.max_width = 2048;
  2764. dev->mode_config.max_height = 2048;
  2765. }
  2766. /* set memory base */
  2767. if (IS_I9XX(dev))
  2768. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 2);
  2769. else
  2770. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 0);
  2771. if (IS_MOBILE(dev) || IS_I9XX(dev))
  2772. num_pipe = 2;
  2773. else
  2774. num_pipe = 1;
  2775. DRM_DEBUG("%d display pipe%s available.\n",
  2776. num_pipe, num_pipe > 1 ? "s" : "");
  2777. for (i = 0; i < num_pipe; i++) {
  2778. intel_crtc_init(dev, i);
  2779. }
  2780. intel_setup_outputs(dev);
  2781. }
  2782. void intel_modeset_cleanup(struct drm_device *dev)
  2783. {
  2784. drm_mode_config_cleanup(dev);
  2785. }
  2786. /* current intel driver doesn't take advantage of encoders
  2787. always give back the encoder for the connector
  2788. */
  2789. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  2790. {
  2791. struct intel_output *intel_output = to_intel_output(connector);
  2792. return &intel_output->enc;
  2793. }