intel_display.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102
  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 "drmP.h"
  28. #include "intel_drv.h"
  29. #include "i915_drm.h"
  30. #include "i915_drv.h"
  31. #include "drm_crtc_helper.h"
  32. bool intel_pipe_has_type (struct drm_crtc *crtc, int type);
  33. typedef struct {
  34. /* given values */
  35. int n;
  36. int m1, m2;
  37. int p1, p2;
  38. /* derived values */
  39. int dot;
  40. int vco;
  41. int m;
  42. int p;
  43. } intel_clock_t;
  44. typedef struct {
  45. int min, max;
  46. } intel_range_t;
  47. typedef struct {
  48. int dot_limit;
  49. int p2_slow, p2_fast;
  50. } intel_p2_t;
  51. #define INTEL_P2_NUM 2
  52. typedef struct intel_limit intel_limit_t;
  53. struct intel_limit {
  54. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  55. intel_p2_t p2;
  56. bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
  57. int, int, intel_clock_t *);
  58. };
  59. #define I8XX_DOT_MIN 25000
  60. #define I8XX_DOT_MAX 350000
  61. #define I8XX_VCO_MIN 930000
  62. #define I8XX_VCO_MAX 1400000
  63. #define I8XX_N_MIN 3
  64. #define I8XX_N_MAX 16
  65. #define I8XX_M_MIN 96
  66. #define I8XX_M_MAX 140
  67. #define I8XX_M1_MIN 18
  68. #define I8XX_M1_MAX 26
  69. #define I8XX_M2_MIN 6
  70. #define I8XX_M2_MAX 16
  71. #define I8XX_P_MIN 4
  72. #define I8XX_P_MAX 128
  73. #define I8XX_P1_MIN 2
  74. #define I8XX_P1_MAX 33
  75. #define I8XX_P1_LVDS_MIN 1
  76. #define I8XX_P1_LVDS_MAX 6
  77. #define I8XX_P2_SLOW 4
  78. #define I8XX_P2_FAST 2
  79. #define I8XX_P2_LVDS_SLOW 14
  80. #define I8XX_P2_LVDS_FAST 14 /* No fast option */
  81. #define I8XX_P2_SLOW_LIMIT 165000
  82. #define I9XX_DOT_MIN 20000
  83. #define I9XX_DOT_MAX 400000
  84. #define I9XX_VCO_MIN 1400000
  85. #define I9XX_VCO_MAX 2800000
  86. #define IGD_VCO_MIN 1700000
  87. #define IGD_VCO_MAX 3500000
  88. #define I9XX_N_MIN 1
  89. #define I9XX_N_MAX 6
  90. /* IGD's Ncounter is a ring counter */
  91. #define IGD_N_MIN 3
  92. #define IGD_N_MAX 6
  93. #define I9XX_M_MIN 70
  94. #define I9XX_M_MAX 120
  95. #define IGD_M_MIN 2
  96. #define IGD_M_MAX 256
  97. #define I9XX_M1_MIN 10
  98. #define I9XX_M1_MAX 22
  99. #define I9XX_M2_MIN 5
  100. #define I9XX_M2_MAX 9
  101. /* IGD M1 is reserved, and must be 0 */
  102. #define IGD_M1_MIN 0
  103. #define IGD_M1_MAX 0
  104. #define IGD_M2_MIN 0
  105. #define IGD_M2_MAX 254
  106. #define I9XX_P_SDVO_DAC_MIN 5
  107. #define I9XX_P_SDVO_DAC_MAX 80
  108. #define I9XX_P_LVDS_MIN 7
  109. #define I9XX_P_LVDS_MAX 98
  110. #define IGD_P_LVDS_MIN 7
  111. #define IGD_P_LVDS_MAX 112
  112. #define I9XX_P1_MIN 1
  113. #define I9XX_P1_MAX 8
  114. #define I9XX_P2_SDVO_DAC_SLOW 10
  115. #define I9XX_P2_SDVO_DAC_FAST 5
  116. #define I9XX_P2_SDVO_DAC_SLOW_LIMIT 200000
  117. #define I9XX_P2_LVDS_SLOW 14
  118. #define I9XX_P2_LVDS_FAST 7
  119. #define I9XX_P2_LVDS_SLOW_LIMIT 112000
  120. #define INTEL_LIMIT_I8XX_DVO_DAC 0
  121. #define INTEL_LIMIT_I8XX_LVDS 1
  122. #define INTEL_LIMIT_I9XX_SDVO_DAC 2
  123. #define INTEL_LIMIT_I9XX_LVDS 3
  124. #define INTEL_LIMIT_G4X_SDVO 4
  125. #define INTEL_LIMIT_G4X_HDMI_DAC 5
  126. #define INTEL_LIMIT_G4X_SINGLE_CHANNEL_LVDS 6
  127. #define INTEL_LIMIT_G4X_DUAL_CHANNEL_LVDS 7
  128. #define INTEL_LIMIT_IGD_SDVO_DAC 8
  129. #define INTEL_LIMIT_IGD_LVDS 9
  130. /*The parameter is for SDVO on G4x platform*/
  131. #define G4X_DOT_SDVO_MIN 25000
  132. #define G4X_DOT_SDVO_MAX 270000
  133. #define G4X_VCO_MIN 1750000
  134. #define G4X_VCO_MAX 3500000
  135. #define G4X_N_SDVO_MIN 1
  136. #define G4X_N_SDVO_MAX 4
  137. #define G4X_M_SDVO_MIN 104
  138. #define G4X_M_SDVO_MAX 138
  139. #define G4X_M1_SDVO_MIN 17
  140. #define G4X_M1_SDVO_MAX 23
  141. #define G4X_M2_SDVO_MIN 5
  142. #define G4X_M2_SDVO_MAX 11
  143. #define G4X_P_SDVO_MIN 10
  144. #define G4X_P_SDVO_MAX 30
  145. #define G4X_P1_SDVO_MIN 1
  146. #define G4X_P1_SDVO_MAX 3
  147. #define G4X_P2_SDVO_SLOW 10
  148. #define G4X_P2_SDVO_FAST 10
  149. #define G4X_P2_SDVO_LIMIT 270000
  150. /*The parameter is for HDMI_DAC on G4x platform*/
  151. #define G4X_DOT_HDMI_DAC_MIN 22000
  152. #define G4X_DOT_HDMI_DAC_MAX 400000
  153. #define G4X_N_HDMI_DAC_MIN 1
  154. #define G4X_N_HDMI_DAC_MAX 4
  155. #define G4X_M_HDMI_DAC_MIN 104
  156. #define G4X_M_HDMI_DAC_MAX 138
  157. #define G4X_M1_HDMI_DAC_MIN 16
  158. #define G4X_M1_HDMI_DAC_MAX 23
  159. #define G4X_M2_HDMI_DAC_MIN 5
  160. #define G4X_M2_HDMI_DAC_MAX 11
  161. #define G4X_P_HDMI_DAC_MIN 5
  162. #define G4X_P_HDMI_DAC_MAX 80
  163. #define G4X_P1_HDMI_DAC_MIN 1
  164. #define G4X_P1_HDMI_DAC_MAX 8
  165. #define G4X_P2_HDMI_DAC_SLOW 10
  166. #define G4X_P2_HDMI_DAC_FAST 5
  167. #define G4X_P2_HDMI_DAC_LIMIT 165000
  168. /*The parameter is for SINGLE_CHANNEL_LVDS on G4x platform*/
  169. #define G4X_DOT_SINGLE_CHANNEL_LVDS_MIN 20000
  170. #define G4X_DOT_SINGLE_CHANNEL_LVDS_MAX 115000
  171. #define G4X_N_SINGLE_CHANNEL_LVDS_MIN 1
  172. #define G4X_N_SINGLE_CHANNEL_LVDS_MAX 3
  173. #define G4X_M_SINGLE_CHANNEL_LVDS_MIN 104
  174. #define G4X_M_SINGLE_CHANNEL_LVDS_MAX 138
  175. #define G4X_M1_SINGLE_CHANNEL_LVDS_MIN 17
  176. #define G4X_M1_SINGLE_CHANNEL_LVDS_MAX 23
  177. #define G4X_M2_SINGLE_CHANNEL_LVDS_MIN 5
  178. #define G4X_M2_SINGLE_CHANNEL_LVDS_MAX 11
  179. #define G4X_P_SINGLE_CHANNEL_LVDS_MIN 28
  180. #define G4X_P_SINGLE_CHANNEL_LVDS_MAX 112
  181. #define G4X_P1_SINGLE_CHANNEL_LVDS_MIN 2
  182. #define G4X_P1_SINGLE_CHANNEL_LVDS_MAX 8
  183. #define G4X_P2_SINGLE_CHANNEL_LVDS_SLOW 14
  184. #define G4X_P2_SINGLE_CHANNEL_LVDS_FAST 14
  185. #define G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT 0
  186. /*The parameter is for DUAL_CHANNEL_LVDS on G4x platform*/
  187. #define G4X_DOT_DUAL_CHANNEL_LVDS_MIN 80000
  188. #define G4X_DOT_DUAL_CHANNEL_LVDS_MAX 224000
  189. #define G4X_N_DUAL_CHANNEL_LVDS_MIN 1
  190. #define G4X_N_DUAL_CHANNEL_LVDS_MAX 3
  191. #define G4X_M_DUAL_CHANNEL_LVDS_MIN 104
  192. #define G4X_M_DUAL_CHANNEL_LVDS_MAX 138
  193. #define G4X_M1_DUAL_CHANNEL_LVDS_MIN 17
  194. #define G4X_M1_DUAL_CHANNEL_LVDS_MAX 23
  195. #define G4X_M2_DUAL_CHANNEL_LVDS_MIN 5
  196. #define G4X_M2_DUAL_CHANNEL_LVDS_MAX 11
  197. #define G4X_P_DUAL_CHANNEL_LVDS_MIN 14
  198. #define G4X_P_DUAL_CHANNEL_LVDS_MAX 42
  199. #define G4X_P1_DUAL_CHANNEL_LVDS_MIN 2
  200. #define G4X_P1_DUAL_CHANNEL_LVDS_MAX 6
  201. #define G4X_P2_DUAL_CHANNEL_LVDS_SLOW 7
  202. #define G4X_P2_DUAL_CHANNEL_LVDS_FAST 7
  203. #define G4X_P2_DUAL_CHANNEL_LVDS_LIMIT 0
  204. static bool
  205. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  206. int target, int refclk, intel_clock_t *best_clock);
  207. static bool
  208. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  209. int target, int refclk, intel_clock_t *best_clock);
  210. static const intel_limit_t intel_limits[] = {
  211. { /* INTEL_LIMIT_I8XX_DVO_DAC */
  212. .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
  213. .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
  214. .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
  215. .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
  216. .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
  217. .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
  218. .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
  219. .p1 = { .min = I8XX_P1_MIN, .max = I8XX_P1_MAX },
  220. .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
  221. .p2_slow = I8XX_P2_SLOW, .p2_fast = I8XX_P2_FAST },
  222. .find_pll = intel_find_best_PLL,
  223. },
  224. { /* INTEL_LIMIT_I8XX_LVDS */
  225. .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
  226. .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
  227. .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
  228. .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
  229. .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
  230. .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
  231. .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
  232. .p1 = { .min = I8XX_P1_LVDS_MIN, .max = I8XX_P1_LVDS_MAX },
  233. .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
  234. .p2_slow = I8XX_P2_LVDS_SLOW, .p2_fast = I8XX_P2_LVDS_FAST },
  235. .find_pll = intel_find_best_PLL,
  236. },
  237. { /* INTEL_LIMIT_I9XX_SDVO_DAC */
  238. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  239. .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
  240. .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
  241. .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
  242. .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
  243. .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
  244. .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
  245. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  246. .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
  247. .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
  248. .find_pll = intel_find_best_PLL,
  249. },
  250. { /* INTEL_LIMIT_I9XX_LVDS */
  251. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  252. .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
  253. .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
  254. .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
  255. .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
  256. .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
  257. .p = { .min = I9XX_P_LVDS_MIN, .max = I9XX_P_LVDS_MAX },
  258. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  259. /* The single-channel range is 25-112Mhz, and dual-channel
  260. * is 80-224Mhz. Prefer single channel as much as possible.
  261. */
  262. .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
  263. .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_FAST },
  264. .find_pll = intel_find_best_PLL,
  265. },
  266. /* below parameter and function is for G4X Chipset Family*/
  267. { /* INTEL_LIMIT_G4X_SDVO */
  268. .dot = { .min = G4X_DOT_SDVO_MIN, .max = G4X_DOT_SDVO_MAX },
  269. .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
  270. .n = { .min = G4X_N_SDVO_MIN, .max = G4X_N_SDVO_MAX },
  271. .m = { .min = G4X_M_SDVO_MIN, .max = G4X_M_SDVO_MAX },
  272. .m1 = { .min = G4X_M1_SDVO_MIN, .max = G4X_M1_SDVO_MAX },
  273. .m2 = { .min = G4X_M2_SDVO_MIN, .max = G4X_M2_SDVO_MAX },
  274. .p = { .min = G4X_P_SDVO_MIN, .max = G4X_P_SDVO_MAX },
  275. .p1 = { .min = G4X_P1_SDVO_MIN, .max = G4X_P1_SDVO_MAX},
  276. .p2 = { .dot_limit = G4X_P2_SDVO_LIMIT,
  277. .p2_slow = G4X_P2_SDVO_SLOW,
  278. .p2_fast = G4X_P2_SDVO_FAST
  279. },
  280. .find_pll = intel_g4x_find_best_PLL,
  281. },
  282. { /* INTEL_LIMIT_G4X_HDMI_DAC */
  283. .dot = { .min = G4X_DOT_HDMI_DAC_MIN, .max = G4X_DOT_HDMI_DAC_MAX },
  284. .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
  285. .n = { .min = G4X_N_HDMI_DAC_MIN, .max = G4X_N_HDMI_DAC_MAX },
  286. .m = { .min = G4X_M_HDMI_DAC_MIN, .max = G4X_M_HDMI_DAC_MAX },
  287. .m1 = { .min = G4X_M1_HDMI_DAC_MIN, .max = G4X_M1_HDMI_DAC_MAX },
  288. .m2 = { .min = G4X_M2_HDMI_DAC_MIN, .max = G4X_M2_HDMI_DAC_MAX },
  289. .p = { .min = G4X_P_HDMI_DAC_MIN, .max = G4X_P_HDMI_DAC_MAX },
  290. .p1 = { .min = G4X_P1_HDMI_DAC_MIN, .max = G4X_P1_HDMI_DAC_MAX},
  291. .p2 = { .dot_limit = G4X_P2_HDMI_DAC_LIMIT,
  292. .p2_slow = G4X_P2_HDMI_DAC_SLOW,
  293. .p2_fast = G4X_P2_HDMI_DAC_FAST
  294. },
  295. .find_pll = intel_g4x_find_best_PLL,
  296. },
  297. { /* INTEL_LIMIT_G4X_SINGLE_CHANNEL_LVDS */
  298. .dot = { .min = G4X_DOT_SINGLE_CHANNEL_LVDS_MIN,
  299. .max = G4X_DOT_SINGLE_CHANNEL_LVDS_MAX },
  300. .vco = { .min = G4X_VCO_MIN,
  301. .max = G4X_VCO_MAX },
  302. .n = { .min = G4X_N_SINGLE_CHANNEL_LVDS_MIN,
  303. .max = G4X_N_SINGLE_CHANNEL_LVDS_MAX },
  304. .m = { .min = G4X_M_SINGLE_CHANNEL_LVDS_MIN,
  305. .max = G4X_M_SINGLE_CHANNEL_LVDS_MAX },
  306. .m1 = { .min = G4X_M1_SINGLE_CHANNEL_LVDS_MIN,
  307. .max = G4X_M1_SINGLE_CHANNEL_LVDS_MAX },
  308. .m2 = { .min = G4X_M2_SINGLE_CHANNEL_LVDS_MIN,
  309. .max = G4X_M2_SINGLE_CHANNEL_LVDS_MAX },
  310. .p = { .min = G4X_P_SINGLE_CHANNEL_LVDS_MIN,
  311. .max = G4X_P_SINGLE_CHANNEL_LVDS_MAX },
  312. .p1 = { .min = G4X_P1_SINGLE_CHANNEL_LVDS_MIN,
  313. .max = G4X_P1_SINGLE_CHANNEL_LVDS_MAX },
  314. .p2 = { .dot_limit = G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT,
  315. .p2_slow = G4X_P2_SINGLE_CHANNEL_LVDS_SLOW,
  316. .p2_fast = G4X_P2_SINGLE_CHANNEL_LVDS_FAST
  317. },
  318. .find_pll = intel_g4x_find_best_PLL,
  319. },
  320. { /* INTEL_LIMIT_G4X_DUAL_CHANNEL_LVDS */
  321. .dot = { .min = G4X_DOT_DUAL_CHANNEL_LVDS_MIN,
  322. .max = G4X_DOT_DUAL_CHANNEL_LVDS_MAX },
  323. .vco = { .min = G4X_VCO_MIN,
  324. .max = G4X_VCO_MAX },
  325. .n = { .min = G4X_N_DUAL_CHANNEL_LVDS_MIN,
  326. .max = G4X_N_DUAL_CHANNEL_LVDS_MAX },
  327. .m = { .min = G4X_M_DUAL_CHANNEL_LVDS_MIN,
  328. .max = G4X_M_DUAL_CHANNEL_LVDS_MAX },
  329. .m1 = { .min = G4X_M1_DUAL_CHANNEL_LVDS_MIN,
  330. .max = G4X_M1_DUAL_CHANNEL_LVDS_MAX },
  331. .m2 = { .min = G4X_M2_DUAL_CHANNEL_LVDS_MIN,
  332. .max = G4X_M2_DUAL_CHANNEL_LVDS_MAX },
  333. .p = { .min = G4X_P_DUAL_CHANNEL_LVDS_MIN,
  334. .max = G4X_P_DUAL_CHANNEL_LVDS_MAX },
  335. .p1 = { .min = G4X_P1_DUAL_CHANNEL_LVDS_MIN,
  336. .max = G4X_P1_DUAL_CHANNEL_LVDS_MAX },
  337. .p2 = { .dot_limit = G4X_P2_DUAL_CHANNEL_LVDS_LIMIT,
  338. .p2_slow = G4X_P2_DUAL_CHANNEL_LVDS_SLOW,
  339. .p2_fast = G4X_P2_DUAL_CHANNEL_LVDS_FAST
  340. },
  341. .find_pll = intel_g4x_find_best_PLL,
  342. },
  343. { /* INTEL_LIMIT_IGD_SDVO */
  344. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX},
  345. .vco = { .min = IGD_VCO_MIN, .max = IGD_VCO_MAX },
  346. .n = { .min = IGD_N_MIN, .max = IGD_N_MAX },
  347. .m = { .min = IGD_M_MIN, .max = IGD_M_MAX },
  348. .m1 = { .min = IGD_M1_MIN, .max = IGD_M1_MAX },
  349. .m2 = { .min = IGD_M2_MIN, .max = IGD_M2_MAX },
  350. .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
  351. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  352. .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
  353. .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
  354. .find_pll = intel_find_best_PLL,
  355. },
  356. { /* INTEL_LIMIT_IGD_LVDS */
  357. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  358. .vco = { .min = IGD_VCO_MIN, .max = IGD_VCO_MAX },
  359. .n = { .min = IGD_N_MIN, .max = IGD_N_MAX },
  360. .m = { .min = IGD_M_MIN, .max = IGD_M_MAX },
  361. .m1 = { .min = IGD_M1_MIN, .max = IGD_M1_MAX },
  362. .m2 = { .min = IGD_M2_MIN, .max = IGD_M2_MAX },
  363. .p = { .min = IGD_P_LVDS_MIN, .max = IGD_P_LVDS_MAX },
  364. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  365. /* IGD only supports single-channel mode. */
  366. .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
  367. .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_SLOW },
  368. .find_pll = intel_find_best_PLL,
  369. },
  370. };
  371. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  372. {
  373. struct drm_device *dev = crtc->dev;
  374. struct drm_i915_private *dev_priv = dev->dev_private;
  375. const intel_limit_t *limit;
  376. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  377. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  378. LVDS_CLKB_POWER_UP)
  379. /* LVDS with dual channel */
  380. limit = &intel_limits
  381. [INTEL_LIMIT_G4X_DUAL_CHANNEL_LVDS];
  382. else
  383. /* LVDS with dual channel */
  384. limit = &intel_limits
  385. [INTEL_LIMIT_G4X_SINGLE_CHANNEL_LVDS];
  386. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  387. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  388. limit = &intel_limits[INTEL_LIMIT_G4X_HDMI_DAC];
  389. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  390. limit = &intel_limits[INTEL_LIMIT_G4X_SDVO];
  391. } else /* The option is for other outputs */
  392. limit = &intel_limits[INTEL_LIMIT_I9XX_SDVO_DAC];
  393. return limit;
  394. }
  395. static const intel_limit_t *intel_limit(struct drm_crtc *crtc)
  396. {
  397. struct drm_device *dev = crtc->dev;
  398. const intel_limit_t *limit;
  399. if (IS_G4X(dev)) {
  400. limit = intel_g4x_limit(crtc);
  401. } else if (IS_I9XX(dev) && !IS_IGD(dev)) {
  402. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  403. limit = &intel_limits[INTEL_LIMIT_I9XX_LVDS];
  404. else
  405. limit = &intel_limits[INTEL_LIMIT_I9XX_SDVO_DAC];
  406. } else if (IS_IGD(dev)) {
  407. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  408. limit = &intel_limits[INTEL_LIMIT_IGD_LVDS];
  409. else
  410. limit = &intel_limits[INTEL_LIMIT_IGD_SDVO_DAC];
  411. } else {
  412. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  413. limit = &intel_limits[INTEL_LIMIT_I8XX_LVDS];
  414. else
  415. limit = &intel_limits[INTEL_LIMIT_I8XX_DVO_DAC];
  416. }
  417. return limit;
  418. }
  419. /* m1 is reserved as 0 in IGD, n is a ring counter */
  420. static void igd_clock(int refclk, intel_clock_t *clock)
  421. {
  422. clock->m = clock->m2 + 2;
  423. clock->p = clock->p1 * clock->p2;
  424. clock->vco = refclk * clock->m / clock->n;
  425. clock->dot = clock->vco / clock->p;
  426. }
  427. static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
  428. {
  429. if (IS_IGD(dev)) {
  430. igd_clock(refclk, clock);
  431. return;
  432. }
  433. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  434. clock->p = clock->p1 * clock->p2;
  435. clock->vco = refclk * clock->m / (clock->n + 2);
  436. clock->dot = clock->vco / clock->p;
  437. }
  438. /**
  439. * Returns whether any output on the specified pipe is of the specified type
  440. */
  441. bool intel_pipe_has_type (struct drm_crtc *crtc, int type)
  442. {
  443. struct drm_device *dev = crtc->dev;
  444. struct drm_mode_config *mode_config = &dev->mode_config;
  445. struct drm_connector *l_entry;
  446. list_for_each_entry(l_entry, &mode_config->connector_list, head) {
  447. if (l_entry->encoder &&
  448. l_entry->encoder->crtc == crtc) {
  449. struct intel_output *intel_output = to_intel_output(l_entry);
  450. if (intel_output->type == type)
  451. return true;
  452. }
  453. }
  454. return false;
  455. }
  456. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  457. /**
  458. * Returns whether the given set of divisors are valid for a given refclk with
  459. * the given connectors.
  460. */
  461. static bool intel_PLL_is_valid(struct drm_crtc *crtc, intel_clock_t *clock)
  462. {
  463. const intel_limit_t *limit = intel_limit (crtc);
  464. struct drm_device *dev = crtc->dev;
  465. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  466. INTELPllInvalid ("p1 out of range\n");
  467. if (clock->p < limit->p.min || limit->p.max < clock->p)
  468. INTELPllInvalid ("p out of range\n");
  469. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  470. INTELPllInvalid ("m2 out of range\n");
  471. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  472. INTELPllInvalid ("m1 out of range\n");
  473. if (clock->m1 <= clock->m2 && !IS_IGD(dev))
  474. INTELPllInvalid ("m1 <= m2\n");
  475. if (clock->m < limit->m.min || limit->m.max < clock->m)
  476. INTELPllInvalid ("m out of range\n");
  477. if (clock->n < limit->n.min || limit->n.max < clock->n)
  478. INTELPllInvalid ("n out of range\n");
  479. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  480. INTELPllInvalid ("vco out of range\n");
  481. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  482. * connector, etc., rather than just a single range.
  483. */
  484. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  485. INTELPllInvalid ("dot out of range\n");
  486. return true;
  487. }
  488. static bool
  489. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  490. int target, int refclk, intel_clock_t *best_clock)
  491. {
  492. struct drm_device *dev = crtc->dev;
  493. struct drm_i915_private *dev_priv = dev->dev_private;
  494. intel_clock_t clock;
  495. int err = target;
  496. if (IS_I9XX(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  497. (I915_READ(LVDS) & LVDS_PORT_EN) != 0) {
  498. /*
  499. * For LVDS, if the panel is on, just rely on its current
  500. * settings for dual-channel. We haven't figured out how to
  501. * reliably set up different single/dual channel state, if we
  502. * even can.
  503. */
  504. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  505. LVDS_CLKB_POWER_UP)
  506. clock.p2 = limit->p2.p2_fast;
  507. else
  508. clock.p2 = limit->p2.p2_slow;
  509. } else {
  510. if (target < limit->p2.dot_limit)
  511. clock.p2 = limit->p2.p2_slow;
  512. else
  513. clock.p2 = limit->p2.p2_fast;
  514. }
  515. memset (best_clock, 0, sizeof (*best_clock));
  516. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  517. for (clock.m2 = limit->m2.min; clock.m2 <= limit->m2.max; clock.m2++) {
  518. /* m1 is always 0 in IGD */
  519. if (clock.m2 >= clock.m1 && !IS_IGD(dev))
  520. break;
  521. for (clock.n = limit->n.min; clock.n <= limit->n.max;
  522. clock.n++) {
  523. for (clock.p1 = limit->p1.min;
  524. clock.p1 <= limit->p1.max; clock.p1++) {
  525. int this_err;
  526. intel_clock(dev, refclk, &clock);
  527. if (!intel_PLL_is_valid(crtc, &clock))
  528. continue;
  529. this_err = abs(clock.dot - target);
  530. if (this_err < err) {
  531. *best_clock = clock;
  532. err = this_err;
  533. }
  534. }
  535. }
  536. }
  537. }
  538. return (err != target);
  539. }
  540. static bool
  541. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  542. int target, int refclk, intel_clock_t *best_clock)
  543. {
  544. struct drm_device *dev = crtc->dev;
  545. struct drm_i915_private *dev_priv = dev->dev_private;
  546. intel_clock_t clock;
  547. int max_n;
  548. bool found;
  549. /* approximately equals target * 0.00488 */
  550. int err_most = (target >> 8) + (target >> 10);
  551. found = false;
  552. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  553. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  554. LVDS_CLKB_POWER_UP)
  555. clock.p2 = limit->p2.p2_fast;
  556. else
  557. clock.p2 = limit->p2.p2_slow;
  558. } else {
  559. if (target < limit->p2.dot_limit)
  560. clock.p2 = limit->p2.p2_slow;
  561. else
  562. clock.p2 = limit->p2.p2_fast;
  563. }
  564. memset(best_clock, 0, sizeof(*best_clock));
  565. max_n = limit->n.max;
  566. /* based on hardware requriment prefer smaller n to precision */
  567. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  568. /* based on hardware requirment prefere larger m1,m2, p1 */
  569. for (clock.m1 = limit->m1.max;
  570. clock.m1 >= limit->m1.min; clock.m1--) {
  571. for (clock.m2 = limit->m2.max;
  572. clock.m2 >= limit->m2.min; clock.m2--) {
  573. for (clock.p1 = limit->p1.max;
  574. clock.p1 >= limit->p1.min; clock.p1--) {
  575. int this_err;
  576. intel_clock(dev, refclk, &clock);
  577. if (!intel_PLL_is_valid(crtc, &clock))
  578. continue;
  579. this_err = abs(clock.dot - target) ;
  580. if (this_err < err_most) {
  581. *best_clock = clock;
  582. err_most = this_err;
  583. max_n = clock.n;
  584. found = true;
  585. }
  586. }
  587. }
  588. }
  589. }
  590. return found;
  591. }
  592. void
  593. intel_wait_for_vblank(struct drm_device *dev)
  594. {
  595. /* Wait for 20ms, i.e. one cycle at 50hz. */
  596. mdelay(20);
  597. }
  598. static int
  599. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  600. struct drm_framebuffer *old_fb)
  601. {
  602. struct drm_device *dev = crtc->dev;
  603. struct drm_i915_private *dev_priv = dev->dev_private;
  604. struct drm_i915_master_private *master_priv;
  605. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  606. struct intel_framebuffer *intel_fb;
  607. struct drm_i915_gem_object *obj_priv;
  608. struct drm_gem_object *obj;
  609. int pipe = intel_crtc->pipe;
  610. unsigned long Start, Offset;
  611. int dspbase = (pipe == 0 ? DSPAADDR : DSPBADDR);
  612. int dspsurf = (pipe == 0 ? DSPASURF : DSPBSURF);
  613. int dspstride = (pipe == 0) ? DSPASTRIDE : DSPBSTRIDE;
  614. int dsptileoff = (pipe == 0 ? DSPATILEOFF : DSPBTILEOFF);
  615. int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
  616. u32 dspcntr, alignment;
  617. int ret;
  618. /* no fb bound */
  619. if (!crtc->fb) {
  620. DRM_DEBUG("No FB bound\n");
  621. return 0;
  622. }
  623. switch (pipe) {
  624. case 0:
  625. case 1:
  626. break;
  627. default:
  628. DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
  629. return -EINVAL;
  630. }
  631. intel_fb = to_intel_framebuffer(crtc->fb);
  632. obj = intel_fb->obj;
  633. obj_priv = obj->driver_private;
  634. switch (obj_priv->tiling_mode) {
  635. case I915_TILING_NONE:
  636. alignment = 64 * 1024;
  637. break;
  638. case I915_TILING_X:
  639. /* pin() will align the object as required by fence */
  640. alignment = 0;
  641. break;
  642. case I915_TILING_Y:
  643. /* FIXME: Is this true? */
  644. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  645. return -EINVAL;
  646. default:
  647. BUG();
  648. }
  649. mutex_lock(&dev->struct_mutex);
  650. ret = i915_gem_object_pin(intel_fb->obj, alignment);
  651. if (ret != 0) {
  652. mutex_unlock(&dev->struct_mutex);
  653. return ret;
  654. }
  655. ret = i915_gem_object_set_to_gtt_domain(intel_fb->obj, 1);
  656. if (ret != 0) {
  657. i915_gem_object_unpin(intel_fb->obj);
  658. mutex_unlock(&dev->struct_mutex);
  659. return ret;
  660. }
  661. dspcntr = I915_READ(dspcntr_reg);
  662. /* Mask out pixel format bits in case we change it */
  663. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  664. switch (crtc->fb->bits_per_pixel) {
  665. case 8:
  666. dspcntr |= DISPPLANE_8BPP;
  667. break;
  668. case 16:
  669. if (crtc->fb->depth == 15)
  670. dspcntr |= DISPPLANE_15_16BPP;
  671. else
  672. dspcntr |= DISPPLANE_16BPP;
  673. break;
  674. case 24:
  675. case 32:
  676. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  677. break;
  678. default:
  679. DRM_ERROR("Unknown color depth\n");
  680. i915_gem_object_unpin(intel_fb->obj);
  681. mutex_unlock(&dev->struct_mutex);
  682. return -EINVAL;
  683. }
  684. if (IS_I965G(dev)) {
  685. if (obj_priv->tiling_mode != I915_TILING_NONE)
  686. dspcntr |= DISPPLANE_TILED;
  687. else
  688. dspcntr &= ~DISPPLANE_TILED;
  689. }
  690. I915_WRITE(dspcntr_reg, dspcntr);
  691. Start = obj_priv->gtt_offset;
  692. Offset = y * crtc->fb->pitch + x * (crtc->fb->bits_per_pixel / 8);
  693. DRM_DEBUG("Writing base %08lX %08lX %d %d\n", Start, Offset, x, y);
  694. I915_WRITE(dspstride, crtc->fb->pitch);
  695. if (IS_I965G(dev)) {
  696. I915_WRITE(dspbase, Offset);
  697. I915_READ(dspbase);
  698. I915_WRITE(dspsurf, Start);
  699. I915_READ(dspsurf);
  700. I915_WRITE(dsptileoff, (y << 16) | x);
  701. } else {
  702. I915_WRITE(dspbase, Start + Offset);
  703. I915_READ(dspbase);
  704. }
  705. intel_wait_for_vblank(dev);
  706. if (old_fb) {
  707. intel_fb = to_intel_framebuffer(old_fb);
  708. i915_gem_object_unpin(intel_fb->obj);
  709. }
  710. mutex_unlock(&dev->struct_mutex);
  711. if (!dev->primary->master)
  712. return 0;
  713. master_priv = dev->primary->master->driver_priv;
  714. if (!master_priv->sarea_priv)
  715. return 0;
  716. if (pipe) {
  717. master_priv->sarea_priv->pipeB_x = x;
  718. master_priv->sarea_priv->pipeB_y = y;
  719. } else {
  720. master_priv->sarea_priv->pipeA_x = x;
  721. master_priv->sarea_priv->pipeA_y = y;
  722. }
  723. return 0;
  724. }
  725. /**
  726. * Sets the power management mode of the pipe and plane.
  727. *
  728. * This code should probably grow support for turning the cursor off and back
  729. * on appropriately at the same time as we're turning the pipe off/on.
  730. */
  731. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  732. {
  733. struct drm_device *dev = crtc->dev;
  734. struct drm_i915_master_private *master_priv;
  735. struct drm_i915_private *dev_priv = dev->dev_private;
  736. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  737. int pipe = intel_crtc->pipe;
  738. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  739. int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
  740. int dspbase_reg = (pipe == 0) ? DSPAADDR : DSPBADDR;
  741. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  742. u32 temp;
  743. bool enabled;
  744. /* XXX: When our outputs are all unaware of DPMS modes other than off
  745. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  746. */
  747. switch (mode) {
  748. case DRM_MODE_DPMS_ON:
  749. case DRM_MODE_DPMS_STANDBY:
  750. case DRM_MODE_DPMS_SUSPEND:
  751. /* Enable the DPLL */
  752. temp = I915_READ(dpll_reg);
  753. if ((temp & DPLL_VCO_ENABLE) == 0) {
  754. I915_WRITE(dpll_reg, temp);
  755. I915_READ(dpll_reg);
  756. /* Wait for the clocks to stabilize. */
  757. udelay(150);
  758. I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
  759. I915_READ(dpll_reg);
  760. /* Wait for the clocks to stabilize. */
  761. udelay(150);
  762. I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
  763. I915_READ(dpll_reg);
  764. /* Wait for the clocks to stabilize. */
  765. udelay(150);
  766. }
  767. /* Enable the pipe */
  768. temp = I915_READ(pipeconf_reg);
  769. if ((temp & PIPEACONF_ENABLE) == 0)
  770. I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
  771. /* Enable the plane */
  772. temp = I915_READ(dspcntr_reg);
  773. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  774. I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
  775. /* Flush the plane changes */
  776. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  777. }
  778. intel_crtc_load_lut(crtc);
  779. /* Give the overlay scaler a chance to enable if it's on this pipe */
  780. //intel_crtc_dpms_video(crtc, true); TODO
  781. break;
  782. case DRM_MODE_DPMS_OFF:
  783. /* Give the overlay scaler a chance to disable if it's on this pipe */
  784. //intel_crtc_dpms_video(crtc, FALSE); TODO
  785. /* Disable the VGA plane that we never use */
  786. I915_WRITE(VGACNTRL, VGA_DISP_DISABLE);
  787. /* Disable display plane */
  788. temp = I915_READ(dspcntr_reg);
  789. if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
  790. I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
  791. /* Flush the plane changes */
  792. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  793. I915_READ(dspbase_reg);
  794. }
  795. if (!IS_I9XX(dev)) {
  796. /* Wait for vblank for the disable to take effect */
  797. intel_wait_for_vblank(dev);
  798. }
  799. /* Next, disable display pipes */
  800. temp = I915_READ(pipeconf_reg);
  801. if ((temp & PIPEACONF_ENABLE) != 0) {
  802. I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
  803. I915_READ(pipeconf_reg);
  804. }
  805. /* Wait for vblank for the disable to take effect. */
  806. intel_wait_for_vblank(dev);
  807. temp = I915_READ(dpll_reg);
  808. if ((temp & DPLL_VCO_ENABLE) != 0) {
  809. I915_WRITE(dpll_reg, temp & ~DPLL_VCO_ENABLE);
  810. I915_READ(dpll_reg);
  811. }
  812. /* Wait for the clocks to turn off. */
  813. udelay(150);
  814. break;
  815. }
  816. if (!dev->primary->master)
  817. return;
  818. master_priv = dev->primary->master->driver_priv;
  819. if (!master_priv->sarea_priv)
  820. return;
  821. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  822. switch (pipe) {
  823. case 0:
  824. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  825. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  826. break;
  827. case 1:
  828. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  829. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  830. break;
  831. default:
  832. DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
  833. break;
  834. }
  835. intel_crtc->dpms_mode = mode;
  836. }
  837. static void intel_crtc_prepare (struct drm_crtc *crtc)
  838. {
  839. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  840. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  841. }
  842. static void intel_crtc_commit (struct drm_crtc *crtc)
  843. {
  844. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  845. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  846. }
  847. void intel_encoder_prepare (struct drm_encoder *encoder)
  848. {
  849. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  850. /* lvds has its own version of prepare see intel_lvds_prepare */
  851. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  852. }
  853. void intel_encoder_commit (struct drm_encoder *encoder)
  854. {
  855. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  856. /* lvds has its own version of commit see intel_lvds_commit */
  857. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  858. }
  859. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  860. struct drm_display_mode *mode,
  861. struct drm_display_mode *adjusted_mode)
  862. {
  863. return true;
  864. }
  865. /** Returns the core display clock speed for i830 - i945 */
  866. static int intel_get_core_clock_speed(struct drm_device *dev)
  867. {
  868. /* Core clock values taken from the published datasheets.
  869. * The 830 may go up to 166 Mhz, which we should check.
  870. */
  871. if (IS_I945G(dev))
  872. return 400000;
  873. else if (IS_I915G(dev))
  874. return 333000;
  875. else if (IS_I945GM(dev) || IS_845G(dev) || IS_IGDGM(dev))
  876. return 200000;
  877. else if (IS_I915GM(dev)) {
  878. u16 gcfgc = 0;
  879. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  880. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  881. return 133000;
  882. else {
  883. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  884. case GC_DISPLAY_CLOCK_333_MHZ:
  885. return 333000;
  886. default:
  887. case GC_DISPLAY_CLOCK_190_200_MHZ:
  888. return 190000;
  889. }
  890. }
  891. } else if (IS_I865G(dev))
  892. return 266000;
  893. else if (IS_I855(dev)) {
  894. u16 hpllcc = 0;
  895. /* Assume that the hardware is in the high speed state. This
  896. * should be the default.
  897. */
  898. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  899. case GC_CLOCK_133_200:
  900. case GC_CLOCK_100_200:
  901. return 200000;
  902. case GC_CLOCK_166_250:
  903. return 250000;
  904. case GC_CLOCK_100_133:
  905. return 133000;
  906. }
  907. } else /* 852, 830 */
  908. return 133000;
  909. return 0; /* Silence gcc warning */
  910. }
  911. /**
  912. * Return the pipe currently connected to the panel fitter,
  913. * or -1 if the panel fitter is not present or not in use
  914. */
  915. static int intel_panel_fitter_pipe (struct drm_device *dev)
  916. {
  917. struct drm_i915_private *dev_priv = dev->dev_private;
  918. u32 pfit_control;
  919. /* i830 doesn't have a panel fitter */
  920. if (IS_I830(dev))
  921. return -1;
  922. pfit_control = I915_READ(PFIT_CONTROL);
  923. /* See if the panel fitter is in use */
  924. if ((pfit_control & PFIT_ENABLE) == 0)
  925. return -1;
  926. /* 965 can place panel fitter on either pipe */
  927. if (IS_I965G(dev))
  928. return (pfit_control >> 29) & 0x3;
  929. /* older chips can only use pipe 1 */
  930. return 1;
  931. }
  932. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  933. struct drm_display_mode *mode,
  934. struct drm_display_mode *adjusted_mode,
  935. int x, int y,
  936. struct drm_framebuffer *old_fb)
  937. {
  938. struct drm_device *dev = crtc->dev;
  939. struct drm_i915_private *dev_priv = dev->dev_private;
  940. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  941. int pipe = intel_crtc->pipe;
  942. int fp_reg = (pipe == 0) ? FPA0 : FPB0;
  943. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  944. int dpll_md_reg = (intel_crtc->pipe == 0) ? DPLL_A_MD : DPLL_B_MD;
  945. int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
  946. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  947. int htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
  948. int hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
  949. int hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
  950. int vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
  951. int vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
  952. int vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
  953. int dspsize_reg = (pipe == 0) ? DSPASIZE : DSPBSIZE;
  954. int dsppos_reg = (pipe == 0) ? DSPAPOS : DSPBPOS;
  955. int pipesrc_reg = (pipe == 0) ? PIPEASRC : PIPEBSRC;
  956. int refclk, num_outputs = 0;
  957. intel_clock_t clock;
  958. u32 dpll = 0, fp = 0, dspcntr, pipeconf;
  959. bool ok, is_sdvo = false, is_dvo = false;
  960. bool is_crt = false, is_lvds = false, is_tv = false;
  961. struct drm_mode_config *mode_config = &dev->mode_config;
  962. struct drm_connector *connector;
  963. const intel_limit_t *limit;
  964. int ret;
  965. drm_vblank_pre_modeset(dev, pipe);
  966. list_for_each_entry(connector, &mode_config->connector_list, head) {
  967. struct intel_output *intel_output = to_intel_output(connector);
  968. if (!connector->encoder || connector->encoder->crtc != crtc)
  969. continue;
  970. switch (intel_output->type) {
  971. case INTEL_OUTPUT_LVDS:
  972. is_lvds = true;
  973. break;
  974. case INTEL_OUTPUT_SDVO:
  975. case INTEL_OUTPUT_HDMI:
  976. is_sdvo = true;
  977. if (intel_output->needs_tv_clock)
  978. is_tv = true;
  979. break;
  980. case INTEL_OUTPUT_DVO:
  981. is_dvo = true;
  982. break;
  983. case INTEL_OUTPUT_TVOUT:
  984. is_tv = true;
  985. break;
  986. case INTEL_OUTPUT_ANALOG:
  987. is_crt = true;
  988. break;
  989. }
  990. num_outputs++;
  991. }
  992. if (is_lvds && dev_priv->lvds_use_ssc && num_outputs < 2) {
  993. refclk = dev_priv->lvds_ssc_freq * 1000;
  994. DRM_DEBUG("using SSC reference clock of %d MHz\n", refclk / 1000);
  995. } else if (IS_I9XX(dev)) {
  996. refclk = 96000;
  997. } else {
  998. refclk = 48000;
  999. }
  1000. /*
  1001. * Returns a set of divisors for the desired target clock with the given
  1002. * refclk, or FALSE. The returned values represent the clock equation:
  1003. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  1004. */
  1005. limit = intel_limit(crtc);
  1006. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
  1007. if (!ok) {
  1008. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  1009. return -EINVAL;
  1010. }
  1011. /* SDVO TV has fixed PLL values depend on its clock range,
  1012. this mirrors vbios setting. */
  1013. if (is_sdvo && is_tv) {
  1014. if (adjusted_mode->clock >= 100000
  1015. && adjusted_mode->clock < 140500) {
  1016. clock.p1 = 2;
  1017. clock.p2 = 10;
  1018. clock.n = 3;
  1019. clock.m1 = 16;
  1020. clock.m2 = 8;
  1021. } else if (adjusted_mode->clock >= 140500
  1022. && adjusted_mode->clock <= 200000) {
  1023. clock.p1 = 1;
  1024. clock.p2 = 10;
  1025. clock.n = 6;
  1026. clock.m1 = 12;
  1027. clock.m2 = 8;
  1028. }
  1029. }
  1030. if (IS_IGD(dev))
  1031. fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
  1032. else
  1033. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  1034. dpll = DPLL_VGA_MODE_DIS;
  1035. if (IS_I9XX(dev)) {
  1036. if (is_lvds)
  1037. dpll |= DPLLB_MODE_LVDS;
  1038. else
  1039. dpll |= DPLLB_MODE_DAC_SERIAL;
  1040. if (is_sdvo) {
  1041. dpll |= DPLL_DVO_HIGH_SPEED;
  1042. if (IS_I945G(dev) || IS_I945GM(dev)) {
  1043. int sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
  1044. dpll |= (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  1045. }
  1046. }
  1047. /* compute bitmask from p1 value */
  1048. if (IS_IGD(dev))
  1049. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_IGD;
  1050. else
  1051. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  1052. switch (clock.p2) {
  1053. case 5:
  1054. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  1055. break;
  1056. case 7:
  1057. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  1058. break;
  1059. case 10:
  1060. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  1061. break;
  1062. case 14:
  1063. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  1064. break;
  1065. }
  1066. if (IS_I965G(dev))
  1067. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  1068. } else {
  1069. if (is_lvds) {
  1070. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  1071. } else {
  1072. if (clock.p1 == 2)
  1073. dpll |= PLL_P1_DIVIDE_BY_TWO;
  1074. else
  1075. dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  1076. if (clock.p2 == 4)
  1077. dpll |= PLL_P2_DIVIDE_BY_4;
  1078. }
  1079. }
  1080. if (is_sdvo && is_tv)
  1081. dpll |= PLL_REF_INPUT_TVCLKINBC;
  1082. else if (is_tv)
  1083. /* XXX: just matching BIOS for now */
  1084. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  1085. dpll |= 3;
  1086. else if (is_lvds && dev_priv->lvds_use_ssc && num_outputs < 2)
  1087. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  1088. else
  1089. dpll |= PLL_REF_INPUT_DREFCLK;
  1090. /* setup pipeconf */
  1091. pipeconf = I915_READ(pipeconf_reg);
  1092. /* Set up the display plane register */
  1093. dspcntr = DISPPLANE_GAMMA_ENABLE;
  1094. if (pipe == 0)
  1095. dspcntr |= DISPPLANE_SEL_PIPE_A;
  1096. else
  1097. dspcntr |= DISPPLANE_SEL_PIPE_B;
  1098. if (pipe == 0 && !IS_I965G(dev)) {
  1099. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  1100. * core speed.
  1101. *
  1102. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  1103. * pipe == 0 check?
  1104. */
  1105. if (mode->clock > intel_get_core_clock_speed(dev) * 9 / 10)
  1106. pipeconf |= PIPEACONF_DOUBLE_WIDE;
  1107. else
  1108. pipeconf &= ~PIPEACONF_DOUBLE_WIDE;
  1109. }
  1110. dspcntr |= DISPLAY_PLANE_ENABLE;
  1111. pipeconf |= PIPEACONF_ENABLE;
  1112. dpll |= DPLL_VCO_ENABLE;
  1113. /* Disable the panel fitter if it was on our pipe */
  1114. if (intel_panel_fitter_pipe(dev) == pipe)
  1115. I915_WRITE(PFIT_CONTROL, 0);
  1116. DRM_DEBUG("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  1117. drm_mode_debug_printmodeline(mode);
  1118. if (dpll & DPLL_VCO_ENABLE) {
  1119. I915_WRITE(fp_reg, fp);
  1120. I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
  1121. I915_READ(dpll_reg);
  1122. udelay(150);
  1123. }
  1124. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  1125. * This is an exception to the general rule that mode_set doesn't turn
  1126. * things on.
  1127. */
  1128. if (is_lvds) {
  1129. u32 lvds = I915_READ(LVDS);
  1130. lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP | LVDS_PIPEB_SELECT;
  1131. /* Set the B0-B3 data pairs corresponding to whether we're going to
  1132. * set the DPLLs for dual-channel mode or not.
  1133. */
  1134. if (clock.p2 == 7)
  1135. lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  1136. else
  1137. lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  1138. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  1139. * appropriately here, but we need to look more thoroughly into how
  1140. * panels behave in the two modes.
  1141. */
  1142. I915_WRITE(LVDS, lvds);
  1143. I915_READ(LVDS);
  1144. }
  1145. I915_WRITE(fp_reg, fp);
  1146. I915_WRITE(dpll_reg, dpll);
  1147. I915_READ(dpll_reg);
  1148. /* Wait for the clocks to stabilize. */
  1149. udelay(150);
  1150. if (IS_I965G(dev)) {
  1151. int sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
  1152. I915_WRITE(dpll_md_reg, (0 << DPLL_MD_UDI_DIVIDER_SHIFT) |
  1153. ((sdvo_pixel_multiply - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT));
  1154. } else {
  1155. /* write it again -- the BIOS does, after all */
  1156. I915_WRITE(dpll_reg, dpll);
  1157. }
  1158. I915_READ(dpll_reg);
  1159. /* Wait for the clocks to stabilize. */
  1160. udelay(150);
  1161. I915_WRITE(htot_reg, (adjusted_mode->crtc_hdisplay - 1) |
  1162. ((adjusted_mode->crtc_htotal - 1) << 16));
  1163. I915_WRITE(hblank_reg, (adjusted_mode->crtc_hblank_start - 1) |
  1164. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  1165. I915_WRITE(hsync_reg, (adjusted_mode->crtc_hsync_start - 1) |
  1166. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  1167. I915_WRITE(vtot_reg, (adjusted_mode->crtc_vdisplay - 1) |
  1168. ((adjusted_mode->crtc_vtotal - 1) << 16));
  1169. I915_WRITE(vblank_reg, (adjusted_mode->crtc_vblank_start - 1) |
  1170. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  1171. I915_WRITE(vsync_reg, (adjusted_mode->crtc_vsync_start - 1) |
  1172. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  1173. /* pipesrc and dspsize control the size that is scaled from, which should
  1174. * always be the user's requested size.
  1175. */
  1176. I915_WRITE(dspsize_reg, ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
  1177. I915_WRITE(dsppos_reg, 0);
  1178. I915_WRITE(pipesrc_reg, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  1179. I915_WRITE(pipeconf_reg, pipeconf);
  1180. I915_READ(pipeconf_reg);
  1181. intel_wait_for_vblank(dev);
  1182. I915_WRITE(dspcntr_reg, dspcntr);
  1183. /* Flush the plane changes */
  1184. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  1185. if (ret != 0)
  1186. return ret;
  1187. drm_vblank_post_modeset(dev, pipe);
  1188. return 0;
  1189. }
  1190. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  1191. void intel_crtc_load_lut(struct drm_crtc *crtc)
  1192. {
  1193. struct drm_device *dev = crtc->dev;
  1194. struct drm_i915_private *dev_priv = dev->dev_private;
  1195. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1196. int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
  1197. int i;
  1198. /* The clocks have to be on to load the palette. */
  1199. if (!crtc->enabled)
  1200. return;
  1201. for (i = 0; i < 256; i++) {
  1202. I915_WRITE(palreg + 4 * i,
  1203. (intel_crtc->lut_r[i] << 16) |
  1204. (intel_crtc->lut_g[i] << 8) |
  1205. intel_crtc->lut_b[i]);
  1206. }
  1207. }
  1208. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  1209. struct drm_file *file_priv,
  1210. uint32_t handle,
  1211. uint32_t width, uint32_t height)
  1212. {
  1213. struct drm_device *dev = crtc->dev;
  1214. struct drm_i915_private *dev_priv = dev->dev_private;
  1215. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1216. struct drm_gem_object *bo;
  1217. struct drm_i915_gem_object *obj_priv;
  1218. int pipe = intel_crtc->pipe;
  1219. uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
  1220. uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
  1221. uint32_t temp = I915_READ(control);
  1222. size_t addr;
  1223. int ret;
  1224. DRM_DEBUG("\n");
  1225. /* if we want to turn off the cursor ignore width and height */
  1226. if (!handle) {
  1227. DRM_DEBUG("cursor off\n");
  1228. if (IS_MOBILE(dev) || IS_I9XX(dev)) {
  1229. temp &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  1230. temp |= CURSOR_MODE_DISABLE;
  1231. } else {
  1232. temp &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  1233. }
  1234. addr = 0;
  1235. bo = NULL;
  1236. mutex_lock(&dev->struct_mutex);
  1237. goto finish;
  1238. }
  1239. /* Currently we only support 64x64 cursors */
  1240. if (width != 64 || height != 64) {
  1241. DRM_ERROR("we currently only support 64x64 cursors\n");
  1242. return -EINVAL;
  1243. }
  1244. bo = drm_gem_object_lookup(dev, file_priv, handle);
  1245. if (!bo)
  1246. return -ENOENT;
  1247. obj_priv = bo->driver_private;
  1248. if (bo->size < width * height * 4) {
  1249. DRM_ERROR("buffer is to small\n");
  1250. ret = -ENOMEM;
  1251. goto fail;
  1252. }
  1253. /* we only need to pin inside GTT if cursor is non-phy */
  1254. mutex_lock(&dev->struct_mutex);
  1255. if (!dev_priv->cursor_needs_physical) {
  1256. ret = i915_gem_object_pin(bo, PAGE_SIZE);
  1257. if (ret) {
  1258. DRM_ERROR("failed to pin cursor bo\n");
  1259. goto fail_locked;
  1260. }
  1261. addr = obj_priv->gtt_offset;
  1262. } else {
  1263. ret = i915_gem_attach_phys_object(dev, bo, (pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1);
  1264. if (ret) {
  1265. DRM_ERROR("failed to attach phys object\n");
  1266. goto fail_locked;
  1267. }
  1268. addr = obj_priv->phys_obj->handle->busaddr;
  1269. }
  1270. if (!IS_I9XX(dev))
  1271. I915_WRITE(CURSIZE, (height << 12) | width);
  1272. /* Hooray for CUR*CNTR differences */
  1273. if (IS_MOBILE(dev) || IS_I9XX(dev)) {
  1274. temp &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  1275. temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  1276. temp |= (pipe << 28); /* Connect to correct pipe */
  1277. } else {
  1278. temp &= ~(CURSOR_FORMAT_MASK);
  1279. temp |= CURSOR_ENABLE;
  1280. temp |= CURSOR_FORMAT_ARGB | CURSOR_GAMMA_ENABLE;
  1281. }
  1282. finish:
  1283. I915_WRITE(control, temp);
  1284. I915_WRITE(base, addr);
  1285. if (intel_crtc->cursor_bo) {
  1286. if (dev_priv->cursor_needs_physical) {
  1287. if (intel_crtc->cursor_bo != bo)
  1288. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  1289. } else
  1290. i915_gem_object_unpin(intel_crtc->cursor_bo);
  1291. drm_gem_object_unreference(intel_crtc->cursor_bo);
  1292. }
  1293. mutex_unlock(&dev->struct_mutex);
  1294. intel_crtc->cursor_addr = addr;
  1295. intel_crtc->cursor_bo = bo;
  1296. return 0;
  1297. fail:
  1298. mutex_lock(&dev->struct_mutex);
  1299. fail_locked:
  1300. drm_gem_object_unreference(bo);
  1301. mutex_unlock(&dev->struct_mutex);
  1302. return ret;
  1303. }
  1304. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  1305. {
  1306. struct drm_device *dev = crtc->dev;
  1307. struct drm_i915_private *dev_priv = dev->dev_private;
  1308. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1309. int pipe = intel_crtc->pipe;
  1310. uint32_t temp = 0;
  1311. uint32_t adder;
  1312. if (x < 0) {
  1313. temp |= (CURSOR_POS_SIGN << CURSOR_X_SHIFT);
  1314. x = -x;
  1315. }
  1316. if (y < 0) {
  1317. temp |= (CURSOR_POS_SIGN << CURSOR_Y_SHIFT);
  1318. y = -y;
  1319. }
  1320. temp |= ((x & CURSOR_POS_MASK) << CURSOR_X_SHIFT);
  1321. temp |= ((y & CURSOR_POS_MASK) << CURSOR_Y_SHIFT);
  1322. adder = intel_crtc->cursor_addr;
  1323. I915_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
  1324. I915_WRITE((pipe == 0) ? CURABASE : CURBBASE, adder);
  1325. return 0;
  1326. }
  1327. /** Sets the color ramps on behalf of RandR */
  1328. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  1329. u16 blue, int regno)
  1330. {
  1331. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1332. intel_crtc->lut_r[regno] = red >> 8;
  1333. intel_crtc->lut_g[regno] = green >> 8;
  1334. intel_crtc->lut_b[regno] = blue >> 8;
  1335. }
  1336. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  1337. u16 *blue, uint32_t size)
  1338. {
  1339. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1340. int i;
  1341. if (size != 256)
  1342. return;
  1343. for (i = 0; i < 256; i++) {
  1344. intel_crtc->lut_r[i] = red[i] >> 8;
  1345. intel_crtc->lut_g[i] = green[i] >> 8;
  1346. intel_crtc->lut_b[i] = blue[i] >> 8;
  1347. }
  1348. intel_crtc_load_lut(crtc);
  1349. }
  1350. /**
  1351. * Get a pipe with a simple mode set on it for doing load-based monitor
  1352. * detection.
  1353. *
  1354. * It will be up to the load-detect code to adjust the pipe as appropriate for
  1355. * its requirements. The pipe will be connected to no other outputs.
  1356. *
  1357. * Currently this code will only succeed if there is a pipe with no outputs
  1358. * configured for it. In the future, it could choose to temporarily disable
  1359. * some outputs to free up a pipe for its use.
  1360. *
  1361. * \return crtc, or NULL if no pipes are available.
  1362. */
  1363. /* VESA 640x480x72Hz mode to set on the pipe */
  1364. static struct drm_display_mode load_detect_mode = {
  1365. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  1366. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  1367. };
  1368. struct drm_crtc *intel_get_load_detect_pipe(struct intel_output *intel_output,
  1369. struct drm_display_mode *mode,
  1370. int *dpms_mode)
  1371. {
  1372. struct intel_crtc *intel_crtc;
  1373. struct drm_crtc *possible_crtc;
  1374. struct drm_crtc *supported_crtc =NULL;
  1375. struct drm_encoder *encoder = &intel_output->enc;
  1376. struct drm_crtc *crtc = NULL;
  1377. struct drm_device *dev = encoder->dev;
  1378. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  1379. struct drm_crtc_helper_funcs *crtc_funcs;
  1380. int i = -1;
  1381. /*
  1382. * Algorithm gets a little messy:
  1383. * - if the connector already has an assigned crtc, use it (but make
  1384. * sure it's on first)
  1385. * - try to find the first unused crtc that can drive this connector,
  1386. * and use that if we find one
  1387. * - if there are no unused crtcs available, try to use the first
  1388. * one we found that supports the connector
  1389. */
  1390. /* See if we already have a CRTC for this connector */
  1391. if (encoder->crtc) {
  1392. crtc = encoder->crtc;
  1393. /* Make sure the crtc and connector are running */
  1394. intel_crtc = to_intel_crtc(crtc);
  1395. *dpms_mode = intel_crtc->dpms_mode;
  1396. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  1397. crtc_funcs = crtc->helper_private;
  1398. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  1399. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  1400. }
  1401. return crtc;
  1402. }
  1403. /* Find an unused one (if possible) */
  1404. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  1405. i++;
  1406. if (!(encoder->possible_crtcs & (1 << i)))
  1407. continue;
  1408. if (!possible_crtc->enabled) {
  1409. crtc = possible_crtc;
  1410. break;
  1411. }
  1412. if (!supported_crtc)
  1413. supported_crtc = possible_crtc;
  1414. }
  1415. /*
  1416. * If we didn't find an unused CRTC, don't use any.
  1417. */
  1418. if (!crtc) {
  1419. return NULL;
  1420. }
  1421. encoder->crtc = crtc;
  1422. intel_output->load_detect_temp = true;
  1423. intel_crtc = to_intel_crtc(crtc);
  1424. *dpms_mode = intel_crtc->dpms_mode;
  1425. if (!crtc->enabled) {
  1426. if (!mode)
  1427. mode = &load_detect_mode;
  1428. drm_crtc_helper_set_mode(crtc, mode, 0, 0, crtc->fb);
  1429. } else {
  1430. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  1431. crtc_funcs = crtc->helper_private;
  1432. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  1433. }
  1434. /* Add this connector to the crtc */
  1435. encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
  1436. encoder_funcs->commit(encoder);
  1437. }
  1438. /* let the connector get through one full cycle before testing */
  1439. intel_wait_for_vblank(dev);
  1440. return crtc;
  1441. }
  1442. void intel_release_load_detect_pipe(struct intel_output *intel_output, int dpms_mode)
  1443. {
  1444. struct drm_encoder *encoder = &intel_output->enc;
  1445. struct drm_device *dev = encoder->dev;
  1446. struct drm_crtc *crtc = encoder->crtc;
  1447. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  1448. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  1449. if (intel_output->load_detect_temp) {
  1450. encoder->crtc = NULL;
  1451. intel_output->load_detect_temp = false;
  1452. crtc->enabled = drm_helper_crtc_in_use(crtc);
  1453. drm_helper_disable_unused_functions(dev);
  1454. }
  1455. /* Switch crtc and output back off if necessary */
  1456. if (crtc->enabled && dpms_mode != DRM_MODE_DPMS_ON) {
  1457. if (encoder->crtc == crtc)
  1458. encoder_funcs->dpms(encoder, dpms_mode);
  1459. crtc_funcs->dpms(crtc, dpms_mode);
  1460. }
  1461. }
  1462. /* Returns the clock of the currently programmed mode of the given pipe. */
  1463. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  1464. {
  1465. struct drm_i915_private *dev_priv = dev->dev_private;
  1466. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1467. int pipe = intel_crtc->pipe;
  1468. u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
  1469. u32 fp;
  1470. intel_clock_t clock;
  1471. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  1472. fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
  1473. else
  1474. fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
  1475. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  1476. if (IS_IGD(dev)) {
  1477. clock.n = ffs((fp & FP_N_IGD_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  1478. clock.m2 = (fp & FP_M2_IGD_DIV_MASK) >> FP_M2_DIV_SHIFT;
  1479. } else {
  1480. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  1481. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  1482. }
  1483. if (IS_I9XX(dev)) {
  1484. if (IS_IGD(dev))
  1485. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_IGD) >>
  1486. DPLL_FPA01_P1_POST_DIV_SHIFT_IGD);
  1487. else
  1488. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  1489. DPLL_FPA01_P1_POST_DIV_SHIFT);
  1490. switch (dpll & DPLL_MODE_MASK) {
  1491. case DPLLB_MODE_DAC_SERIAL:
  1492. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  1493. 5 : 10;
  1494. break;
  1495. case DPLLB_MODE_LVDS:
  1496. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  1497. 7 : 14;
  1498. break;
  1499. default:
  1500. DRM_DEBUG("Unknown DPLL mode %08x in programmed "
  1501. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  1502. return 0;
  1503. }
  1504. /* XXX: Handle the 100Mhz refclk */
  1505. intel_clock(dev, 96000, &clock);
  1506. } else {
  1507. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  1508. if (is_lvds) {
  1509. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  1510. DPLL_FPA01_P1_POST_DIV_SHIFT);
  1511. clock.p2 = 14;
  1512. if ((dpll & PLL_REF_INPUT_MASK) ==
  1513. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  1514. /* XXX: might not be 66MHz */
  1515. intel_clock(dev, 66000, &clock);
  1516. } else
  1517. intel_clock(dev, 48000, &clock);
  1518. } else {
  1519. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  1520. clock.p1 = 2;
  1521. else {
  1522. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  1523. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  1524. }
  1525. if (dpll & PLL_P2_DIVIDE_BY_4)
  1526. clock.p2 = 4;
  1527. else
  1528. clock.p2 = 2;
  1529. intel_clock(dev, 48000, &clock);
  1530. }
  1531. }
  1532. /* XXX: It would be nice to validate the clocks, but we can't reuse
  1533. * i830PllIsValid() because it relies on the xf86_config connector
  1534. * configuration being accurate, which it isn't necessarily.
  1535. */
  1536. return clock.dot;
  1537. }
  1538. /** Returns the currently programmed mode of the given pipe. */
  1539. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  1540. struct drm_crtc *crtc)
  1541. {
  1542. struct drm_i915_private *dev_priv = dev->dev_private;
  1543. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1544. int pipe = intel_crtc->pipe;
  1545. struct drm_display_mode *mode;
  1546. int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
  1547. int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
  1548. int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
  1549. int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
  1550. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  1551. if (!mode)
  1552. return NULL;
  1553. mode->clock = intel_crtc_clock_get(dev, crtc);
  1554. mode->hdisplay = (htot & 0xffff) + 1;
  1555. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  1556. mode->hsync_start = (hsync & 0xffff) + 1;
  1557. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  1558. mode->vdisplay = (vtot & 0xffff) + 1;
  1559. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  1560. mode->vsync_start = (vsync & 0xffff) + 1;
  1561. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  1562. drm_mode_set_name(mode);
  1563. drm_mode_set_crtcinfo(mode, 0);
  1564. return mode;
  1565. }
  1566. static void intel_crtc_destroy(struct drm_crtc *crtc)
  1567. {
  1568. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1569. drm_crtc_cleanup(crtc);
  1570. kfree(intel_crtc);
  1571. }
  1572. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  1573. .dpms = intel_crtc_dpms,
  1574. .mode_fixup = intel_crtc_mode_fixup,
  1575. .mode_set = intel_crtc_mode_set,
  1576. .mode_set_base = intel_pipe_set_base,
  1577. .prepare = intel_crtc_prepare,
  1578. .commit = intel_crtc_commit,
  1579. };
  1580. static const struct drm_crtc_funcs intel_crtc_funcs = {
  1581. .cursor_set = intel_crtc_cursor_set,
  1582. .cursor_move = intel_crtc_cursor_move,
  1583. .gamma_set = intel_crtc_gamma_set,
  1584. .set_config = drm_crtc_helper_set_config,
  1585. .destroy = intel_crtc_destroy,
  1586. };
  1587. static void intel_crtc_init(struct drm_device *dev, int pipe)
  1588. {
  1589. struct intel_crtc *intel_crtc;
  1590. int i;
  1591. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  1592. if (intel_crtc == NULL)
  1593. return;
  1594. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  1595. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  1596. intel_crtc->pipe = pipe;
  1597. for (i = 0; i < 256; i++) {
  1598. intel_crtc->lut_r[i] = i;
  1599. intel_crtc->lut_g[i] = i;
  1600. intel_crtc->lut_b[i] = i;
  1601. }
  1602. intel_crtc->cursor_addr = 0;
  1603. intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
  1604. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  1605. intel_crtc->mode_set.crtc = &intel_crtc->base;
  1606. intel_crtc->mode_set.connectors = (struct drm_connector **)(intel_crtc + 1);
  1607. intel_crtc->mode_set.num_connectors = 0;
  1608. if (i915_fbpercrtc) {
  1609. }
  1610. }
  1611. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  1612. struct drm_file *file_priv)
  1613. {
  1614. drm_i915_private_t *dev_priv = dev->dev_private;
  1615. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  1616. struct drm_crtc *crtc = NULL;
  1617. int pipe = -1;
  1618. if (!dev_priv) {
  1619. DRM_ERROR("called with no initialization\n");
  1620. return -EINVAL;
  1621. }
  1622. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1623. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1624. if (crtc->base.id == pipe_from_crtc_id->crtc_id) {
  1625. pipe = intel_crtc->pipe;
  1626. break;
  1627. }
  1628. }
  1629. if (pipe == -1) {
  1630. DRM_ERROR("no such CRTC id\n");
  1631. return -EINVAL;
  1632. }
  1633. pipe_from_crtc_id->pipe = pipe;
  1634. return 0;
  1635. }
  1636. struct drm_crtc *intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
  1637. {
  1638. struct drm_crtc *crtc = NULL;
  1639. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1640. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1641. if (intel_crtc->pipe == pipe)
  1642. break;
  1643. }
  1644. return crtc;
  1645. }
  1646. static int intel_connector_clones(struct drm_device *dev, int type_mask)
  1647. {
  1648. int index_mask = 0;
  1649. struct drm_connector *connector;
  1650. int entry = 0;
  1651. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1652. struct intel_output *intel_output = to_intel_output(connector);
  1653. if (type_mask & (1 << intel_output->type))
  1654. index_mask |= (1 << entry);
  1655. entry++;
  1656. }
  1657. return index_mask;
  1658. }
  1659. static void intel_setup_outputs(struct drm_device *dev)
  1660. {
  1661. struct drm_i915_private *dev_priv = dev->dev_private;
  1662. struct drm_connector *connector;
  1663. intel_crt_init(dev);
  1664. /* Set up integrated LVDS */
  1665. if (IS_MOBILE(dev) && !IS_I830(dev))
  1666. intel_lvds_init(dev);
  1667. if (IS_I9XX(dev)) {
  1668. int found;
  1669. u32 reg;
  1670. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  1671. found = intel_sdvo_init(dev, SDVOB);
  1672. if (!found && SUPPORTS_INTEGRATED_HDMI(dev))
  1673. intel_hdmi_init(dev, SDVOB);
  1674. }
  1675. /* Before G4X SDVOC doesn't have its own detect register */
  1676. if (IS_G4X(dev))
  1677. reg = SDVOC;
  1678. else
  1679. reg = SDVOB;
  1680. if (I915_READ(reg) & SDVO_DETECTED) {
  1681. found = intel_sdvo_init(dev, SDVOC);
  1682. if (!found && SUPPORTS_INTEGRATED_HDMI(dev))
  1683. intel_hdmi_init(dev, SDVOC);
  1684. }
  1685. } else
  1686. intel_dvo_init(dev);
  1687. if (IS_I9XX(dev) && IS_MOBILE(dev))
  1688. intel_tv_init(dev);
  1689. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1690. struct intel_output *intel_output = to_intel_output(connector);
  1691. struct drm_encoder *encoder = &intel_output->enc;
  1692. int crtc_mask = 0, clone_mask = 0;
  1693. /* valid crtcs */
  1694. switch(intel_output->type) {
  1695. case INTEL_OUTPUT_HDMI:
  1696. crtc_mask = ((1 << 0)|
  1697. (1 << 1));
  1698. clone_mask = ((1 << INTEL_OUTPUT_HDMI));
  1699. break;
  1700. case INTEL_OUTPUT_DVO:
  1701. case INTEL_OUTPUT_SDVO:
  1702. crtc_mask = ((1 << 0)|
  1703. (1 << 1));
  1704. clone_mask = ((1 << INTEL_OUTPUT_ANALOG) |
  1705. (1 << INTEL_OUTPUT_DVO) |
  1706. (1 << INTEL_OUTPUT_SDVO));
  1707. break;
  1708. case INTEL_OUTPUT_ANALOG:
  1709. crtc_mask = ((1 << 0)|
  1710. (1 << 1));
  1711. clone_mask = ((1 << INTEL_OUTPUT_ANALOG) |
  1712. (1 << INTEL_OUTPUT_DVO) |
  1713. (1 << INTEL_OUTPUT_SDVO));
  1714. break;
  1715. case INTEL_OUTPUT_LVDS:
  1716. crtc_mask = (1 << 1);
  1717. clone_mask = (1 << INTEL_OUTPUT_LVDS);
  1718. break;
  1719. case INTEL_OUTPUT_TVOUT:
  1720. crtc_mask = ((1 << 0) |
  1721. (1 << 1));
  1722. clone_mask = (1 << INTEL_OUTPUT_TVOUT);
  1723. break;
  1724. }
  1725. encoder->possible_crtcs = crtc_mask;
  1726. encoder->possible_clones = intel_connector_clones(dev, clone_mask);
  1727. }
  1728. }
  1729. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  1730. {
  1731. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1732. struct drm_device *dev = fb->dev;
  1733. if (fb->fbdev)
  1734. intelfb_remove(dev, fb);
  1735. drm_framebuffer_cleanup(fb);
  1736. mutex_lock(&dev->struct_mutex);
  1737. drm_gem_object_unreference(intel_fb->obj);
  1738. mutex_unlock(&dev->struct_mutex);
  1739. kfree(intel_fb);
  1740. }
  1741. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  1742. struct drm_file *file_priv,
  1743. unsigned int *handle)
  1744. {
  1745. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1746. struct drm_gem_object *object = intel_fb->obj;
  1747. return drm_gem_handle_create(file_priv, object, handle);
  1748. }
  1749. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  1750. .destroy = intel_user_framebuffer_destroy,
  1751. .create_handle = intel_user_framebuffer_create_handle,
  1752. };
  1753. int intel_framebuffer_create(struct drm_device *dev,
  1754. struct drm_mode_fb_cmd *mode_cmd,
  1755. struct drm_framebuffer **fb,
  1756. struct drm_gem_object *obj)
  1757. {
  1758. struct intel_framebuffer *intel_fb;
  1759. int ret;
  1760. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  1761. if (!intel_fb)
  1762. return -ENOMEM;
  1763. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  1764. if (ret) {
  1765. DRM_ERROR("framebuffer init failed %d\n", ret);
  1766. return ret;
  1767. }
  1768. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  1769. intel_fb->obj = obj;
  1770. *fb = &intel_fb->base;
  1771. return 0;
  1772. }
  1773. static struct drm_framebuffer *
  1774. intel_user_framebuffer_create(struct drm_device *dev,
  1775. struct drm_file *filp,
  1776. struct drm_mode_fb_cmd *mode_cmd)
  1777. {
  1778. struct drm_gem_object *obj;
  1779. struct drm_framebuffer *fb;
  1780. int ret;
  1781. obj = drm_gem_object_lookup(dev, filp, mode_cmd->handle);
  1782. if (!obj)
  1783. return NULL;
  1784. ret = intel_framebuffer_create(dev, mode_cmd, &fb, obj);
  1785. if (ret) {
  1786. mutex_lock(&dev->struct_mutex);
  1787. drm_gem_object_unreference(obj);
  1788. mutex_unlock(&dev->struct_mutex);
  1789. return NULL;
  1790. }
  1791. return fb;
  1792. }
  1793. static const struct drm_mode_config_funcs intel_mode_funcs = {
  1794. .fb_create = intel_user_framebuffer_create,
  1795. .fb_changed = intelfb_probe,
  1796. };
  1797. void intel_modeset_init(struct drm_device *dev)
  1798. {
  1799. int num_pipe;
  1800. int i;
  1801. drm_mode_config_init(dev);
  1802. dev->mode_config.min_width = 0;
  1803. dev->mode_config.min_height = 0;
  1804. dev->mode_config.funcs = (void *)&intel_mode_funcs;
  1805. if (IS_I965G(dev)) {
  1806. dev->mode_config.max_width = 8192;
  1807. dev->mode_config.max_height = 8192;
  1808. } else {
  1809. dev->mode_config.max_width = 2048;
  1810. dev->mode_config.max_height = 2048;
  1811. }
  1812. /* set memory base */
  1813. if (IS_I9XX(dev))
  1814. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 2);
  1815. else
  1816. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 0);
  1817. if (IS_MOBILE(dev) || IS_I9XX(dev))
  1818. num_pipe = 2;
  1819. else
  1820. num_pipe = 1;
  1821. DRM_DEBUG("%d display pipe%s available.\n",
  1822. num_pipe, num_pipe > 1 ? "s" : "");
  1823. for (i = 0; i < num_pipe; i++) {
  1824. intel_crtc_init(dev, i);
  1825. }
  1826. intel_setup_outputs(dev);
  1827. }
  1828. void intel_modeset_cleanup(struct drm_device *dev)
  1829. {
  1830. drm_mode_config_cleanup(dev);
  1831. }
  1832. /* current intel driver doesn't take advantage of encoders
  1833. always give back the encoder for the connector
  1834. */
  1835. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  1836. {
  1837. struct intel_output *intel_output = to_intel_output(connector);
  1838. return &intel_output->enc;
  1839. }