intel_display.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048
  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 dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
  615. u32 dspcntr, alignment;
  616. int ret;
  617. /* no fb bound */
  618. if (!crtc->fb) {
  619. DRM_DEBUG("No FB bound\n");
  620. return 0;
  621. }
  622. switch (pipe) {
  623. case 0:
  624. case 1:
  625. break;
  626. default:
  627. DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
  628. return -EINVAL;
  629. }
  630. intel_fb = to_intel_framebuffer(crtc->fb);
  631. obj = intel_fb->obj;
  632. obj_priv = obj->driver_private;
  633. switch (obj_priv->tiling_mode) {
  634. case I915_TILING_NONE:
  635. alignment = 64 * 1024;
  636. break;
  637. case I915_TILING_X:
  638. /* pin() will align the object as required by fence */
  639. alignment = 0;
  640. break;
  641. case I915_TILING_Y:
  642. /* FIXME: Is this true? */
  643. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  644. return -EINVAL;
  645. default:
  646. BUG();
  647. }
  648. mutex_lock(&dev->struct_mutex);
  649. ret = i915_gem_object_pin(intel_fb->obj, alignment);
  650. if (ret != 0) {
  651. mutex_unlock(&dev->struct_mutex);
  652. return ret;
  653. }
  654. ret = i915_gem_object_set_to_gtt_domain(intel_fb->obj, 1);
  655. if (ret != 0) {
  656. i915_gem_object_unpin(intel_fb->obj);
  657. mutex_unlock(&dev->struct_mutex);
  658. return ret;
  659. }
  660. dspcntr = I915_READ(dspcntr_reg);
  661. /* Mask out pixel format bits in case we change it */
  662. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  663. switch (crtc->fb->bits_per_pixel) {
  664. case 8:
  665. dspcntr |= DISPPLANE_8BPP;
  666. break;
  667. case 16:
  668. if (crtc->fb->depth == 15)
  669. dspcntr |= DISPPLANE_15_16BPP;
  670. else
  671. dspcntr |= DISPPLANE_16BPP;
  672. break;
  673. case 24:
  674. case 32:
  675. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  676. break;
  677. default:
  678. DRM_ERROR("Unknown color depth\n");
  679. i915_gem_object_unpin(intel_fb->obj);
  680. mutex_unlock(&dev->struct_mutex);
  681. return -EINVAL;
  682. }
  683. I915_WRITE(dspcntr_reg, dspcntr);
  684. Start = obj_priv->gtt_offset;
  685. Offset = y * crtc->fb->pitch + x * (crtc->fb->bits_per_pixel / 8);
  686. DRM_DEBUG("Writing base %08lX %08lX %d %d\n", Start, Offset, x, y);
  687. I915_WRITE(dspstride, crtc->fb->pitch);
  688. if (IS_I965G(dev)) {
  689. I915_WRITE(dspbase, Offset);
  690. I915_READ(dspbase);
  691. I915_WRITE(dspsurf, Start);
  692. I915_READ(dspsurf);
  693. } else {
  694. I915_WRITE(dspbase, Start + Offset);
  695. I915_READ(dspbase);
  696. }
  697. intel_wait_for_vblank(dev);
  698. if (old_fb) {
  699. intel_fb = to_intel_framebuffer(old_fb);
  700. i915_gem_object_unpin(intel_fb->obj);
  701. }
  702. mutex_unlock(&dev->struct_mutex);
  703. if (!dev->primary->master)
  704. return 0;
  705. master_priv = dev->primary->master->driver_priv;
  706. if (!master_priv->sarea_priv)
  707. return 0;
  708. if (pipe) {
  709. master_priv->sarea_priv->pipeB_x = x;
  710. master_priv->sarea_priv->pipeB_y = y;
  711. } else {
  712. master_priv->sarea_priv->pipeA_x = x;
  713. master_priv->sarea_priv->pipeA_y = y;
  714. }
  715. return 0;
  716. }
  717. /**
  718. * Sets the power management mode of the pipe and plane.
  719. *
  720. * This code should probably grow support for turning the cursor off and back
  721. * on appropriately at the same time as we're turning the pipe off/on.
  722. */
  723. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  724. {
  725. struct drm_device *dev = crtc->dev;
  726. struct drm_i915_master_private *master_priv;
  727. struct drm_i915_private *dev_priv = dev->dev_private;
  728. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  729. int pipe = intel_crtc->pipe;
  730. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  731. int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
  732. int dspbase_reg = (pipe == 0) ? DSPAADDR : DSPBADDR;
  733. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  734. u32 temp;
  735. bool enabled;
  736. /* XXX: When our outputs are all unaware of DPMS modes other than off
  737. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  738. */
  739. switch (mode) {
  740. case DRM_MODE_DPMS_ON:
  741. case DRM_MODE_DPMS_STANDBY:
  742. case DRM_MODE_DPMS_SUSPEND:
  743. /* Enable the DPLL */
  744. temp = I915_READ(dpll_reg);
  745. if ((temp & DPLL_VCO_ENABLE) == 0) {
  746. I915_WRITE(dpll_reg, temp);
  747. I915_READ(dpll_reg);
  748. /* Wait for the clocks to stabilize. */
  749. udelay(150);
  750. I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
  751. I915_READ(dpll_reg);
  752. /* Wait for the clocks to stabilize. */
  753. udelay(150);
  754. I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
  755. I915_READ(dpll_reg);
  756. /* Wait for the clocks to stabilize. */
  757. udelay(150);
  758. }
  759. /* Enable the pipe */
  760. temp = I915_READ(pipeconf_reg);
  761. if ((temp & PIPEACONF_ENABLE) == 0)
  762. I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
  763. /* Enable the plane */
  764. temp = I915_READ(dspcntr_reg);
  765. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  766. I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
  767. /* Flush the plane changes */
  768. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  769. }
  770. intel_crtc_load_lut(crtc);
  771. /* Give the overlay scaler a chance to enable if it's on this pipe */
  772. //intel_crtc_dpms_video(crtc, true); TODO
  773. break;
  774. case DRM_MODE_DPMS_OFF:
  775. /* Give the overlay scaler a chance to disable if it's on this pipe */
  776. //intel_crtc_dpms_video(crtc, FALSE); TODO
  777. /* Disable the VGA plane that we never use */
  778. I915_WRITE(VGACNTRL, VGA_DISP_DISABLE);
  779. /* Disable display plane */
  780. temp = I915_READ(dspcntr_reg);
  781. if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
  782. I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
  783. /* Flush the plane changes */
  784. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  785. I915_READ(dspbase_reg);
  786. }
  787. if (!IS_I9XX(dev)) {
  788. /* Wait for vblank for the disable to take effect */
  789. intel_wait_for_vblank(dev);
  790. }
  791. /* Next, disable display pipes */
  792. temp = I915_READ(pipeconf_reg);
  793. if ((temp & PIPEACONF_ENABLE) != 0) {
  794. I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
  795. I915_READ(pipeconf_reg);
  796. }
  797. /* Wait for vblank for the disable to take effect. */
  798. intel_wait_for_vblank(dev);
  799. temp = I915_READ(dpll_reg);
  800. if ((temp & DPLL_VCO_ENABLE) != 0) {
  801. I915_WRITE(dpll_reg, temp & ~DPLL_VCO_ENABLE);
  802. I915_READ(dpll_reg);
  803. }
  804. /* Wait for the clocks to turn off. */
  805. udelay(150);
  806. break;
  807. }
  808. if (!dev->primary->master)
  809. return;
  810. master_priv = dev->primary->master->driver_priv;
  811. if (!master_priv->sarea_priv)
  812. return;
  813. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  814. switch (pipe) {
  815. case 0:
  816. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  817. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  818. break;
  819. case 1:
  820. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  821. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  822. break;
  823. default:
  824. DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
  825. break;
  826. }
  827. intel_crtc->dpms_mode = mode;
  828. }
  829. static void intel_crtc_prepare (struct drm_crtc *crtc)
  830. {
  831. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  832. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  833. }
  834. static void intel_crtc_commit (struct drm_crtc *crtc)
  835. {
  836. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  837. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  838. }
  839. void intel_encoder_prepare (struct drm_encoder *encoder)
  840. {
  841. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  842. /* lvds has its own version of prepare see intel_lvds_prepare */
  843. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  844. }
  845. void intel_encoder_commit (struct drm_encoder *encoder)
  846. {
  847. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  848. /* lvds has its own version of commit see intel_lvds_commit */
  849. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  850. }
  851. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  852. struct drm_display_mode *mode,
  853. struct drm_display_mode *adjusted_mode)
  854. {
  855. return true;
  856. }
  857. /** Returns the core display clock speed for i830 - i945 */
  858. static int intel_get_core_clock_speed(struct drm_device *dev)
  859. {
  860. /* Core clock values taken from the published datasheets.
  861. * The 830 may go up to 166 Mhz, which we should check.
  862. */
  863. if (IS_I945G(dev))
  864. return 400000;
  865. else if (IS_I915G(dev))
  866. return 333000;
  867. else if (IS_I945GM(dev) || IS_845G(dev) || IS_IGDGM(dev))
  868. return 200000;
  869. else if (IS_I915GM(dev)) {
  870. u16 gcfgc = 0;
  871. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  872. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  873. return 133000;
  874. else {
  875. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  876. case GC_DISPLAY_CLOCK_333_MHZ:
  877. return 333000;
  878. default:
  879. case GC_DISPLAY_CLOCK_190_200_MHZ:
  880. return 190000;
  881. }
  882. }
  883. } else if (IS_I865G(dev))
  884. return 266000;
  885. else if (IS_I855(dev)) {
  886. u16 hpllcc = 0;
  887. /* Assume that the hardware is in the high speed state. This
  888. * should be the default.
  889. */
  890. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  891. case GC_CLOCK_133_200:
  892. case GC_CLOCK_100_200:
  893. return 200000;
  894. case GC_CLOCK_166_250:
  895. return 250000;
  896. case GC_CLOCK_100_133:
  897. return 133000;
  898. }
  899. } else /* 852, 830 */
  900. return 133000;
  901. return 0; /* Silence gcc warning */
  902. }
  903. /**
  904. * Return the pipe currently connected to the panel fitter,
  905. * or -1 if the panel fitter is not present or not in use
  906. */
  907. static int intel_panel_fitter_pipe (struct drm_device *dev)
  908. {
  909. struct drm_i915_private *dev_priv = dev->dev_private;
  910. u32 pfit_control;
  911. /* i830 doesn't have a panel fitter */
  912. if (IS_I830(dev))
  913. return -1;
  914. pfit_control = I915_READ(PFIT_CONTROL);
  915. /* See if the panel fitter is in use */
  916. if ((pfit_control & PFIT_ENABLE) == 0)
  917. return -1;
  918. /* 965 can place panel fitter on either pipe */
  919. if (IS_I965G(dev))
  920. return (pfit_control >> 29) & 0x3;
  921. /* older chips can only use pipe 1 */
  922. return 1;
  923. }
  924. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  925. struct drm_display_mode *mode,
  926. struct drm_display_mode *adjusted_mode,
  927. int x, int y,
  928. struct drm_framebuffer *old_fb)
  929. {
  930. struct drm_device *dev = crtc->dev;
  931. struct drm_i915_private *dev_priv = dev->dev_private;
  932. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  933. int pipe = intel_crtc->pipe;
  934. int fp_reg = (pipe == 0) ? FPA0 : FPB0;
  935. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  936. int dpll_md_reg = (intel_crtc->pipe == 0) ? DPLL_A_MD : DPLL_B_MD;
  937. int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
  938. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  939. int htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
  940. int hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
  941. int hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
  942. int vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
  943. int vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
  944. int vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
  945. int dspsize_reg = (pipe == 0) ? DSPASIZE : DSPBSIZE;
  946. int dsppos_reg = (pipe == 0) ? DSPAPOS : DSPBPOS;
  947. int pipesrc_reg = (pipe == 0) ? PIPEASRC : PIPEBSRC;
  948. int refclk, num_outputs = 0;
  949. intel_clock_t clock;
  950. u32 dpll = 0, fp = 0, dspcntr, pipeconf;
  951. bool ok, is_sdvo = false, is_dvo = false;
  952. bool is_crt = false, is_lvds = false, is_tv = false;
  953. struct drm_mode_config *mode_config = &dev->mode_config;
  954. struct drm_connector *connector;
  955. const intel_limit_t *limit;
  956. int ret;
  957. drm_vblank_pre_modeset(dev, pipe);
  958. list_for_each_entry(connector, &mode_config->connector_list, head) {
  959. struct intel_output *intel_output = to_intel_output(connector);
  960. if (!connector->encoder || connector->encoder->crtc != crtc)
  961. continue;
  962. switch (intel_output->type) {
  963. case INTEL_OUTPUT_LVDS:
  964. is_lvds = true;
  965. break;
  966. case INTEL_OUTPUT_SDVO:
  967. case INTEL_OUTPUT_HDMI:
  968. is_sdvo = true;
  969. if (intel_output->needs_tv_clock)
  970. is_tv = true;
  971. break;
  972. case INTEL_OUTPUT_DVO:
  973. is_dvo = true;
  974. break;
  975. case INTEL_OUTPUT_TVOUT:
  976. is_tv = true;
  977. break;
  978. case INTEL_OUTPUT_ANALOG:
  979. is_crt = true;
  980. break;
  981. }
  982. num_outputs++;
  983. }
  984. if (is_lvds && dev_priv->lvds_use_ssc && num_outputs < 2) {
  985. refclk = dev_priv->lvds_ssc_freq * 1000;
  986. DRM_DEBUG("using SSC reference clock of %d MHz\n", refclk / 1000);
  987. } else if (IS_I9XX(dev)) {
  988. refclk = 96000;
  989. } else {
  990. refclk = 48000;
  991. }
  992. /*
  993. * Returns a set of divisors for the desired target clock with the given
  994. * refclk, or FALSE. The returned values represent the clock equation:
  995. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  996. */
  997. limit = intel_limit(crtc);
  998. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
  999. if (!ok) {
  1000. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  1001. return -EINVAL;
  1002. }
  1003. /* SDVO TV has fixed PLL values depend on its clock range,
  1004. this mirrors vbios setting. */
  1005. if (is_sdvo && is_tv) {
  1006. if (adjusted_mode->clock >= 100000
  1007. && adjusted_mode->clock < 140500) {
  1008. clock.p1 = 2;
  1009. clock.p2 = 10;
  1010. clock.n = 3;
  1011. clock.m1 = 16;
  1012. clock.m2 = 8;
  1013. } else if (adjusted_mode->clock >= 140500
  1014. && adjusted_mode->clock <= 200000) {
  1015. clock.p1 = 1;
  1016. clock.p2 = 10;
  1017. clock.n = 6;
  1018. clock.m1 = 12;
  1019. clock.m2 = 8;
  1020. }
  1021. }
  1022. if (IS_IGD(dev))
  1023. fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
  1024. else
  1025. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  1026. dpll = DPLL_VGA_MODE_DIS;
  1027. if (IS_I9XX(dev)) {
  1028. if (is_lvds)
  1029. dpll |= DPLLB_MODE_LVDS;
  1030. else
  1031. dpll |= DPLLB_MODE_DAC_SERIAL;
  1032. if (is_sdvo) {
  1033. dpll |= DPLL_DVO_HIGH_SPEED;
  1034. if (IS_I945G(dev) || IS_I945GM(dev)) {
  1035. int sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
  1036. dpll |= (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  1037. }
  1038. }
  1039. /* compute bitmask from p1 value */
  1040. if (IS_IGD(dev))
  1041. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_IGD;
  1042. else
  1043. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  1044. switch (clock.p2) {
  1045. case 5:
  1046. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  1047. break;
  1048. case 7:
  1049. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  1050. break;
  1051. case 10:
  1052. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  1053. break;
  1054. case 14:
  1055. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  1056. break;
  1057. }
  1058. if (IS_I965G(dev))
  1059. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  1060. } else {
  1061. if (is_lvds) {
  1062. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  1063. } else {
  1064. if (clock.p1 == 2)
  1065. dpll |= PLL_P1_DIVIDE_BY_TWO;
  1066. else
  1067. dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  1068. if (clock.p2 == 4)
  1069. dpll |= PLL_P2_DIVIDE_BY_4;
  1070. }
  1071. }
  1072. if (is_sdvo && is_tv)
  1073. dpll |= PLL_REF_INPUT_TVCLKINBC;
  1074. else if (is_tv)
  1075. /* XXX: just matching BIOS for now */
  1076. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  1077. dpll |= 3;
  1078. else if (is_lvds && dev_priv->lvds_use_ssc && num_outputs < 2)
  1079. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  1080. else
  1081. dpll |= PLL_REF_INPUT_DREFCLK;
  1082. /* setup pipeconf */
  1083. pipeconf = I915_READ(pipeconf_reg);
  1084. /* Set up the display plane register */
  1085. dspcntr = DISPPLANE_GAMMA_ENABLE;
  1086. if (pipe == 0)
  1087. dspcntr |= DISPPLANE_SEL_PIPE_A;
  1088. else
  1089. dspcntr |= DISPPLANE_SEL_PIPE_B;
  1090. if (pipe == 0 && !IS_I965G(dev)) {
  1091. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  1092. * core speed.
  1093. *
  1094. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  1095. * pipe == 0 check?
  1096. */
  1097. if (mode->clock > intel_get_core_clock_speed(dev) * 9 / 10)
  1098. pipeconf |= PIPEACONF_DOUBLE_WIDE;
  1099. else
  1100. pipeconf &= ~PIPEACONF_DOUBLE_WIDE;
  1101. }
  1102. dspcntr |= DISPLAY_PLANE_ENABLE;
  1103. pipeconf |= PIPEACONF_ENABLE;
  1104. dpll |= DPLL_VCO_ENABLE;
  1105. /* Disable the panel fitter if it was on our pipe */
  1106. if (intel_panel_fitter_pipe(dev) == pipe)
  1107. I915_WRITE(PFIT_CONTROL, 0);
  1108. DRM_DEBUG("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  1109. drm_mode_debug_printmodeline(mode);
  1110. if (dpll & DPLL_VCO_ENABLE) {
  1111. I915_WRITE(fp_reg, fp);
  1112. I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
  1113. I915_READ(dpll_reg);
  1114. udelay(150);
  1115. }
  1116. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  1117. * This is an exception to the general rule that mode_set doesn't turn
  1118. * things on.
  1119. */
  1120. if (is_lvds) {
  1121. u32 lvds = I915_READ(LVDS);
  1122. lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP | LVDS_PIPEB_SELECT;
  1123. /* Set the B0-B3 data pairs corresponding to whether we're going to
  1124. * set the DPLLs for dual-channel mode or not.
  1125. */
  1126. if (clock.p2 == 7)
  1127. lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  1128. else
  1129. lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  1130. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  1131. * appropriately here, but we need to look more thoroughly into how
  1132. * panels behave in the two modes.
  1133. */
  1134. I915_WRITE(LVDS, lvds);
  1135. I915_READ(LVDS);
  1136. }
  1137. I915_WRITE(fp_reg, fp);
  1138. I915_WRITE(dpll_reg, dpll);
  1139. I915_READ(dpll_reg);
  1140. /* Wait for the clocks to stabilize. */
  1141. udelay(150);
  1142. if (IS_I965G(dev)) {
  1143. int sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
  1144. I915_WRITE(dpll_md_reg, (0 << DPLL_MD_UDI_DIVIDER_SHIFT) |
  1145. ((sdvo_pixel_multiply - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT));
  1146. } else {
  1147. /* write it again -- the BIOS does, after all */
  1148. I915_WRITE(dpll_reg, dpll);
  1149. }
  1150. I915_READ(dpll_reg);
  1151. /* Wait for the clocks to stabilize. */
  1152. udelay(150);
  1153. I915_WRITE(htot_reg, (adjusted_mode->crtc_hdisplay - 1) |
  1154. ((adjusted_mode->crtc_htotal - 1) << 16));
  1155. I915_WRITE(hblank_reg, (adjusted_mode->crtc_hblank_start - 1) |
  1156. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  1157. I915_WRITE(hsync_reg, (adjusted_mode->crtc_hsync_start - 1) |
  1158. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  1159. I915_WRITE(vtot_reg, (adjusted_mode->crtc_vdisplay - 1) |
  1160. ((adjusted_mode->crtc_vtotal - 1) << 16));
  1161. I915_WRITE(vblank_reg, (adjusted_mode->crtc_vblank_start - 1) |
  1162. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  1163. I915_WRITE(vsync_reg, (adjusted_mode->crtc_vsync_start - 1) |
  1164. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  1165. /* pipesrc and dspsize control the size that is scaled from, which should
  1166. * always be the user's requested size.
  1167. */
  1168. I915_WRITE(dspsize_reg, ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
  1169. I915_WRITE(dsppos_reg, 0);
  1170. I915_WRITE(pipesrc_reg, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  1171. I915_WRITE(pipeconf_reg, pipeconf);
  1172. I915_READ(pipeconf_reg);
  1173. intel_wait_for_vblank(dev);
  1174. I915_WRITE(dspcntr_reg, dspcntr);
  1175. /* Flush the plane changes */
  1176. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  1177. if (ret != 0)
  1178. return ret;
  1179. drm_vblank_post_modeset(dev, pipe);
  1180. return 0;
  1181. }
  1182. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  1183. void intel_crtc_load_lut(struct drm_crtc *crtc)
  1184. {
  1185. struct drm_device *dev = crtc->dev;
  1186. struct drm_i915_private *dev_priv = dev->dev_private;
  1187. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1188. int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
  1189. int i;
  1190. /* The clocks have to be on to load the palette. */
  1191. if (!crtc->enabled)
  1192. return;
  1193. for (i = 0; i < 256; i++) {
  1194. I915_WRITE(palreg + 4 * i,
  1195. (intel_crtc->lut_r[i] << 16) |
  1196. (intel_crtc->lut_g[i] << 8) |
  1197. intel_crtc->lut_b[i]);
  1198. }
  1199. }
  1200. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  1201. struct drm_file *file_priv,
  1202. uint32_t handle,
  1203. uint32_t width, uint32_t height)
  1204. {
  1205. struct drm_device *dev = crtc->dev;
  1206. struct drm_i915_private *dev_priv = dev->dev_private;
  1207. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1208. struct drm_gem_object *bo;
  1209. struct drm_i915_gem_object *obj_priv;
  1210. int pipe = intel_crtc->pipe;
  1211. uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
  1212. uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
  1213. uint32_t temp;
  1214. size_t addr;
  1215. int ret;
  1216. DRM_DEBUG("\n");
  1217. /* if we want to turn off the cursor ignore width and height */
  1218. if (!handle) {
  1219. DRM_DEBUG("cursor off\n");
  1220. temp = CURSOR_MODE_DISABLE;
  1221. addr = 0;
  1222. bo = NULL;
  1223. mutex_lock(&dev->struct_mutex);
  1224. goto finish;
  1225. }
  1226. /* Currently we only support 64x64 cursors */
  1227. if (width != 64 || height != 64) {
  1228. DRM_ERROR("we currently only support 64x64 cursors\n");
  1229. return -EINVAL;
  1230. }
  1231. bo = drm_gem_object_lookup(dev, file_priv, handle);
  1232. if (!bo)
  1233. return -ENOENT;
  1234. obj_priv = bo->driver_private;
  1235. if (bo->size < width * height * 4) {
  1236. DRM_ERROR("buffer is to small\n");
  1237. ret = -ENOMEM;
  1238. goto fail;
  1239. }
  1240. /* we only need to pin inside GTT if cursor is non-phy */
  1241. mutex_lock(&dev->struct_mutex);
  1242. if (!dev_priv->cursor_needs_physical) {
  1243. ret = i915_gem_object_pin(bo, PAGE_SIZE);
  1244. if (ret) {
  1245. DRM_ERROR("failed to pin cursor bo\n");
  1246. goto fail_locked;
  1247. }
  1248. addr = obj_priv->gtt_offset;
  1249. } else {
  1250. ret = i915_gem_attach_phys_object(dev, bo, (pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1);
  1251. if (ret) {
  1252. DRM_ERROR("failed to attach phys object\n");
  1253. goto fail_locked;
  1254. }
  1255. addr = obj_priv->phys_obj->handle->busaddr;
  1256. }
  1257. temp = 0;
  1258. /* set the pipe for the cursor */
  1259. temp |= (pipe << 28);
  1260. temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  1261. finish:
  1262. I915_WRITE(control, temp);
  1263. I915_WRITE(base, addr);
  1264. if (intel_crtc->cursor_bo) {
  1265. if (dev_priv->cursor_needs_physical) {
  1266. if (intel_crtc->cursor_bo != bo)
  1267. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  1268. } else
  1269. i915_gem_object_unpin(intel_crtc->cursor_bo);
  1270. drm_gem_object_unreference(intel_crtc->cursor_bo);
  1271. }
  1272. mutex_unlock(&dev->struct_mutex);
  1273. intel_crtc->cursor_addr = addr;
  1274. intel_crtc->cursor_bo = bo;
  1275. return 0;
  1276. fail:
  1277. mutex_lock(&dev->struct_mutex);
  1278. fail_locked:
  1279. drm_gem_object_unreference(bo);
  1280. mutex_unlock(&dev->struct_mutex);
  1281. return ret;
  1282. }
  1283. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  1284. {
  1285. struct drm_device *dev = crtc->dev;
  1286. struct drm_i915_private *dev_priv = dev->dev_private;
  1287. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1288. int pipe = intel_crtc->pipe;
  1289. uint32_t temp = 0;
  1290. uint32_t adder;
  1291. if (x < 0) {
  1292. temp |= (CURSOR_POS_SIGN << CURSOR_X_SHIFT);
  1293. x = -x;
  1294. }
  1295. if (y < 0) {
  1296. temp |= (CURSOR_POS_SIGN << CURSOR_Y_SHIFT);
  1297. y = -y;
  1298. }
  1299. temp |= ((x & CURSOR_POS_MASK) << CURSOR_X_SHIFT);
  1300. temp |= ((y & CURSOR_POS_MASK) << CURSOR_Y_SHIFT);
  1301. adder = intel_crtc->cursor_addr;
  1302. I915_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
  1303. I915_WRITE((pipe == 0) ? CURABASE : CURBBASE, adder);
  1304. return 0;
  1305. }
  1306. /** Sets the color ramps on behalf of RandR */
  1307. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  1308. u16 blue, int regno)
  1309. {
  1310. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1311. intel_crtc->lut_r[regno] = red >> 8;
  1312. intel_crtc->lut_g[regno] = green >> 8;
  1313. intel_crtc->lut_b[regno] = blue >> 8;
  1314. }
  1315. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  1316. u16 *blue, uint32_t size)
  1317. {
  1318. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1319. int i;
  1320. if (size != 256)
  1321. return;
  1322. for (i = 0; i < 256; i++) {
  1323. intel_crtc->lut_r[i] = red[i] >> 8;
  1324. intel_crtc->lut_g[i] = green[i] >> 8;
  1325. intel_crtc->lut_b[i] = blue[i] >> 8;
  1326. }
  1327. intel_crtc_load_lut(crtc);
  1328. }
  1329. /**
  1330. * Get a pipe with a simple mode set on it for doing load-based monitor
  1331. * detection.
  1332. *
  1333. * It will be up to the load-detect code to adjust the pipe as appropriate for
  1334. * its requirements. The pipe will be connected to no other outputs.
  1335. *
  1336. * Currently this code will only succeed if there is a pipe with no outputs
  1337. * configured for it. In the future, it could choose to temporarily disable
  1338. * some outputs to free up a pipe for its use.
  1339. *
  1340. * \return crtc, or NULL if no pipes are available.
  1341. */
  1342. /* VESA 640x480x72Hz mode to set on the pipe */
  1343. static struct drm_display_mode load_detect_mode = {
  1344. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  1345. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  1346. };
  1347. struct drm_crtc *intel_get_load_detect_pipe(struct intel_output *intel_output,
  1348. struct drm_display_mode *mode,
  1349. int *dpms_mode)
  1350. {
  1351. struct intel_crtc *intel_crtc;
  1352. struct drm_crtc *possible_crtc;
  1353. struct drm_crtc *supported_crtc =NULL;
  1354. struct drm_encoder *encoder = &intel_output->enc;
  1355. struct drm_crtc *crtc = NULL;
  1356. struct drm_device *dev = encoder->dev;
  1357. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  1358. struct drm_crtc_helper_funcs *crtc_funcs;
  1359. int i = -1;
  1360. /*
  1361. * Algorithm gets a little messy:
  1362. * - if the connector already has an assigned crtc, use it (but make
  1363. * sure it's on first)
  1364. * - try to find the first unused crtc that can drive this connector,
  1365. * and use that if we find one
  1366. * - if there are no unused crtcs available, try to use the first
  1367. * one we found that supports the connector
  1368. */
  1369. /* See if we already have a CRTC for this connector */
  1370. if (encoder->crtc) {
  1371. crtc = encoder->crtc;
  1372. /* Make sure the crtc and connector are running */
  1373. intel_crtc = to_intel_crtc(crtc);
  1374. *dpms_mode = intel_crtc->dpms_mode;
  1375. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  1376. crtc_funcs = crtc->helper_private;
  1377. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  1378. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  1379. }
  1380. return crtc;
  1381. }
  1382. /* Find an unused one (if possible) */
  1383. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  1384. i++;
  1385. if (!(encoder->possible_crtcs & (1 << i)))
  1386. continue;
  1387. if (!possible_crtc->enabled) {
  1388. crtc = possible_crtc;
  1389. break;
  1390. }
  1391. if (!supported_crtc)
  1392. supported_crtc = possible_crtc;
  1393. }
  1394. /*
  1395. * If we didn't find an unused CRTC, don't use any.
  1396. */
  1397. if (!crtc) {
  1398. return NULL;
  1399. }
  1400. encoder->crtc = crtc;
  1401. intel_output->load_detect_temp = true;
  1402. intel_crtc = to_intel_crtc(crtc);
  1403. *dpms_mode = intel_crtc->dpms_mode;
  1404. if (!crtc->enabled) {
  1405. if (!mode)
  1406. mode = &load_detect_mode;
  1407. drm_crtc_helper_set_mode(crtc, mode, 0, 0, crtc->fb);
  1408. } else {
  1409. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  1410. crtc_funcs = crtc->helper_private;
  1411. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  1412. }
  1413. /* Add this connector to the crtc */
  1414. encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
  1415. encoder_funcs->commit(encoder);
  1416. }
  1417. /* let the connector get through one full cycle before testing */
  1418. intel_wait_for_vblank(dev);
  1419. return crtc;
  1420. }
  1421. void intel_release_load_detect_pipe(struct intel_output *intel_output, int dpms_mode)
  1422. {
  1423. struct drm_encoder *encoder = &intel_output->enc;
  1424. struct drm_device *dev = encoder->dev;
  1425. struct drm_crtc *crtc = encoder->crtc;
  1426. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  1427. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  1428. if (intel_output->load_detect_temp) {
  1429. encoder->crtc = NULL;
  1430. intel_output->load_detect_temp = false;
  1431. crtc->enabled = drm_helper_crtc_in_use(crtc);
  1432. drm_helper_disable_unused_functions(dev);
  1433. }
  1434. /* Switch crtc and output back off if necessary */
  1435. if (crtc->enabled && dpms_mode != DRM_MODE_DPMS_ON) {
  1436. if (encoder->crtc == crtc)
  1437. encoder_funcs->dpms(encoder, dpms_mode);
  1438. crtc_funcs->dpms(crtc, dpms_mode);
  1439. }
  1440. }
  1441. /* Returns the clock of the currently programmed mode of the given pipe. */
  1442. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  1443. {
  1444. struct drm_i915_private *dev_priv = dev->dev_private;
  1445. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1446. int pipe = intel_crtc->pipe;
  1447. u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
  1448. u32 fp;
  1449. intel_clock_t clock;
  1450. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  1451. fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
  1452. else
  1453. fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
  1454. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  1455. if (IS_IGD(dev)) {
  1456. clock.n = ffs((fp & FP_N_IGD_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  1457. clock.m2 = (fp & FP_M2_IGD_DIV_MASK) >> FP_M2_DIV_SHIFT;
  1458. } else {
  1459. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  1460. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  1461. }
  1462. if (IS_I9XX(dev)) {
  1463. if (IS_IGD(dev))
  1464. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_IGD) >>
  1465. DPLL_FPA01_P1_POST_DIV_SHIFT_IGD);
  1466. else
  1467. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  1468. DPLL_FPA01_P1_POST_DIV_SHIFT);
  1469. switch (dpll & DPLL_MODE_MASK) {
  1470. case DPLLB_MODE_DAC_SERIAL:
  1471. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  1472. 5 : 10;
  1473. break;
  1474. case DPLLB_MODE_LVDS:
  1475. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  1476. 7 : 14;
  1477. break;
  1478. default:
  1479. DRM_DEBUG("Unknown DPLL mode %08x in programmed "
  1480. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  1481. return 0;
  1482. }
  1483. /* XXX: Handle the 100Mhz refclk */
  1484. intel_clock(dev, 96000, &clock);
  1485. } else {
  1486. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  1487. if (is_lvds) {
  1488. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  1489. DPLL_FPA01_P1_POST_DIV_SHIFT);
  1490. clock.p2 = 14;
  1491. if ((dpll & PLL_REF_INPUT_MASK) ==
  1492. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  1493. /* XXX: might not be 66MHz */
  1494. intel_clock(dev, 66000, &clock);
  1495. } else
  1496. intel_clock(dev, 48000, &clock);
  1497. } else {
  1498. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  1499. clock.p1 = 2;
  1500. else {
  1501. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  1502. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  1503. }
  1504. if (dpll & PLL_P2_DIVIDE_BY_4)
  1505. clock.p2 = 4;
  1506. else
  1507. clock.p2 = 2;
  1508. intel_clock(dev, 48000, &clock);
  1509. }
  1510. }
  1511. /* XXX: It would be nice to validate the clocks, but we can't reuse
  1512. * i830PllIsValid() because it relies on the xf86_config connector
  1513. * configuration being accurate, which it isn't necessarily.
  1514. */
  1515. return clock.dot;
  1516. }
  1517. /** Returns the currently programmed mode of the given pipe. */
  1518. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  1519. struct drm_crtc *crtc)
  1520. {
  1521. struct drm_i915_private *dev_priv = dev->dev_private;
  1522. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1523. int pipe = intel_crtc->pipe;
  1524. struct drm_display_mode *mode;
  1525. int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
  1526. int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
  1527. int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
  1528. int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
  1529. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  1530. if (!mode)
  1531. return NULL;
  1532. mode->clock = intel_crtc_clock_get(dev, crtc);
  1533. mode->hdisplay = (htot & 0xffff) + 1;
  1534. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  1535. mode->hsync_start = (hsync & 0xffff) + 1;
  1536. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  1537. mode->vdisplay = (vtot & 0xffff) + 1;
  1538. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  1539. mode->vsync_start = (vsync & 0xffff) + 1;
  1540. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  1541. drm_mode_set_name(mode);
  1542. drm_mode_set_crtcinfo(mode, 0);
  1543. return mode;
  1544. }
  1545. static void intel_crtc_destroy(struct drm_crtc *crtc)
  1546. {
  1547. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1548. drm_crtc_cleanup(crtc);
  1549. kfree(intel_crtc);
  1550. }
  1551. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  1552. .dpms = intel_crtc_dpms,
  1553. .mode_fixup = intel_crtc_mode_fixup,
  1554. .mode_set = intel_crtc_mode_set,
  1555. .mode_set_base = intel_pipe_set_base,
  1556. .prepare = intel_crtc_prepare,
  1557. .commit = intel_crtc_commit,
  1558. };
  1559. static const struct drm_crtc_funcs intel_crtc_funcs = {
  1560. .cursor_set = intel_crtc_cursor_set,
  1561. .cursor_move = intel_crtc_cursor_move,
  1562. .gamma_set = intel_crtc_gamma_set,
  1563. .set_config = drm_crtc_helper_set_config,
  1564. .destroy = intel_crtc_destroy,
  1565. };
  1566. static void intel_crtc_init(struct drm_device *dev, int pipe)
  1567. {
  1568. struct intel_crtc *intel_crtc;
  1569. int i;
  1570. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  1571. if (intel_crtc == NULL)
  1572. return;
  1573. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  1574. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  1575. intel_crtc->pipe = pipe;
  1576. for (i = 0; i < 256; i++) {
  1577. intel_crtc->lut_r[i] = i;
  1578. intel_crtc->lut_g[i] = i;
  1579. intel_crtc->lut_b[i] = i;
  1580. }
  1581. intel_crtc->cursor_addr = 0;
  1582. intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
  1583. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  1584. intel_crtc->mode_set.crtc = &intel_crtc->base;
  1585. intel_crtc->mode_set.connectors = (struct drm_connector **)(intel_crtc + 1);
  1586. intel_crtc->mode_set.num_connectors = 0;
  1587. if (i915_fbpercrtc) {
  1588. }
  1589. }
  1590. struct drm_crtc *intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
  1591. {
  1592. struct drm_crtc *crtc = NULL;
  1593. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1594. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1595. if (intel_crtc->pipe == pipe)
  1596. break;
  1597. }
  1598. return crtc;
  1599. }
  1600. static int intel_connector_clones(struct drm_device *dev, int type_mask)
  1601. {
  1602. int index_mask = 0;
  1603. struct drm_connector *connector;
  1604. int entry = 0;
  1605. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1606. struct intel_output *intel_output = to_intel_output(connector);
  1607. if (type_mask & (1 << intel_output->type))
  1608. index_mask |= (1 << entry);
  1609. entry++;
  1610. }
  1611. return index_mask;
  1612. }
  1613. static void intel_setup_outputs(struct drm_device *dev)
  1614. {
  1615. struct drm_i915_private *dev_priv = dev->dev_private;
  1616. struct drm_connector *connector;
  1617. intel_crt_init(dev);
  1618. /* Set up integrated LVDS */
  1619. if (IS_MOBILE(dev) && !IS_I830(dev))
  1620. intel_lvds_init(dev);
  1621. if (IS_I9XX(dev)) {
  1622. int found;
  1623. u32 reg;
  1624. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  1625. found = intel_sdvo_init(dev, SDVOB);
  1626. if (!found && SUPPORTS_INTEGRATED_HDMI(dev))
  1627. intel_hdmi_init(dev, SDVOB);
  1628. }
  1629. /* Before G4X SDVOC doesn't have its own detect register */
  1630. if (IS_G4X(dev))
  1631. reg = SDVOC;
  1632. else
  1633. reg = SDVOB;
  1634. if (I915_READ(reg) & SDVO_DETECTED) {
  1635. found = intel_sdvo_init(dev, SDVOC);
  1636. if (!found && SUPPORTS_INTEGRATED_HDMI(dev))
  1637. intel_hdmi_init(dev, SDVOC);
  1638. }
  1639. } else
  1640. intel_dvo_init(dev);
  1641. if (IS_I9XX(dev) && IS_MOBILE(dev))
  1642. intel_tv_init(dev);
  1643. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1644. struct intel_output *intel_output = to_intel_output(connector);
  1645. struct drm_encoder *encoder = &intel_output->enc;
  1646. int crtc_mask = 0, clone_mask = 0;
  1647. /* valid crtcs */
  1648. switch(intel_output->type) {
  1649. case INTEL_OUTPUT_HDMI:
  1650. crtc_mask = ((1 << 0)|
  1651. (1 << 1));
  1652. clone_mask = ((1 << INTEL_OUTPUT_HDMI));
  1653. break;
  1654. case INTEL_OUTPUT_DVO:
  1655. case INTEL_OUTPUT_SDVO:
  1656. crtc_mask = ((1 << 0)|
  1657. (1 << 1));
  1658. clone_mask = ((1 << INTEL_OUTPUT_ANALOG) |
  1659. (1 << INTEL_OUTPUT_DVO) |
  1660. (1 << INTEL_OUTPUT_SDVO));
  1661. break;
  1662. case INTEL_OUTPUT_ANALOG:
  1663. crtc_mask = ((1 << 0)|
  1664. (1 << 1));
  1665. clone_mask = ((1 << INTEL_OUTPUT_ANALOG) |
  1666. (1 << INTEL_OUTPUT_DVO) |
  1667. (1 << INTEL_OUTPUT_SDVO));
  1668. break;
  1669. case INTEL_OUTPUT_LVDS:
  1670. crtc_mask = (1 << 1);
  1671. clone_mask = (1 << INTEL_OUTPUT_LVDS);
  1672. break;
  1673. case INTEL_OUTPUT_TVOUT:
  1674. crtc_mask = ((1 << 0) |
  1675. (1 << 1));
  1676. clone_mask = (1 << INTEL_OUTPUT_TVOUT);
  1677. break;
  1678. }
  1679. encoder->possible_crtcs = crtc_mask;
  1680. encoder->possible_clones = intel_connector_clones(dev, clone_mask);
  1681. }
  1682. }
  1683. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  1684. {
  1685. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1686. struct drm_device *dev = fb->dev;
  1687. if (fb->fbdev)
  1688. intelfb_remove(dev, fb);
  1689. drm_framebuffer_cleanup(fb);
  1690. mutex_lock(&dev->struct_mutex);
  1691. drm_gem_object_unreference(intel_fb->obj);
  1692. mutex_unlock(&dev->struct_mutex);
  1693. kfree(intel_fb);
  1694. }
  1695. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  1696. struct drm_file *file_priv,
  1697. unsigned int *handle)
  1698. {
  1699. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1700. struct drm_gem_object *object = intel_fb->obj;
  1701. return drm_gem_handle_create(file_priv, object, handle);
  1702. }
  1703. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  1704. .destroy = intel_user_framebuffer_destroy,
  1705. .create_handle = intel_user_framebuffer_create_handle,
  1706. };
  1707. int intel_framebuffer_create(struct drm_device *dev,
  1708. struct drm_mode_fb_cmd *mode_cmd,
  1709. struct drm_framebuffer **fb,
  1710. struct drm_gem_object *obj)
  1711. {
  1712. struct intel_framebuffer *intel_fb;
  1713. int ret;
  1714. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  1715. if (!intel_fb)
  1716. return -ENOMEM;
  1717. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  1718. if (ret) {
  1719. DRM_ERROR("framebuffer init failed %d\n", ret);
  1720. return ret;
  1721. }
  1722. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  1723. intel_fb->obj = obj;
  1724. *fb = &intel_fb->base;
  1725. return 0;
  1726. }
  1727. static struct drm_framebuffer *
  1728. intel_user_framebuffer_create(struct drm_device *dev,
  1729. struct drm_file *filp,
  1730. struct drm_mode_fb_cmd *mode_cmd)
  1731. {
  1732. struct drm_gem_object *obj;
  1733. struct drm_framebuffer *fb;
  1734. int ret;
  1735. obj = drm_gem_object_lookup(dev, filp, mode_cmd->handle);
  1736. if (!obj)
  1737. return NULL;
  1738. ret = intel_framebuffer_create(dev, mode_cmd, &fb, obj);
  1739. if (ret) {
  1740. mutex_lock(&dev->struct_mutex);
  1741. drm_gem_object_unreference(obj);
  1742. mutex_unlock(&dev->struct_mutex);
  1743. return NULL;
  1744. }
  1745. return fb;
  1746. }
  1747. static const struct drm_mode_config_funcs intel_mode_funcs = {
  1748. .fb_create = intel_user_framebuffer_create,
  1749. .fb_changed = intelfb_probe,
  1750. };
  1751. void intel_modeset_init(struct drm_device *dev)
  1752. {
  1753. int num_pipe;
  1754. int i;
  1755. drm_mode_config_init(dev);
  1756. dev->mode_config.min_width = 0;
  1757. dev->mode_config.min_height = 0;
  1758. dev->mode_config.funcs = (void *)&intel_mode_funcs;
  1759. if (IS_I965G(dev)) {
  1760. dev->mode_config.max_width = 8192;
  1761. dev->mode_config.max_height = 8192;
  1762. } else {
  1763. dev->mode_config.max_width = 2048;
  1764. dev->mode_config.max_height = 2048;
  1765. }
  1766. /* set memory base */
  1767. if (IS_I9XX(dev))
  1768. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 2);
  1769. else
  1770. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 0);
  1771. if (IS_MOBILE(dev) || IS_I9XX(dev))
  1772. num_pipe = 2;
  1773. else
  1774. num_pipe = 1;
  1775. DRM_DEBUG("%d display pipe%s available.\n",
  1776. num_pipe, num_pipe > 1 ? "s" : "");
  1777. for (i = 0; i < num_pipe; i++) {
  1778. intel_crtc_init(dev, i);
  1779. }
  1780. intel_setup_outputs(dev);
  1781. }
  1782. void intel_modeset_cleanup(struct drm_device *dev)
  1783. {
  1784. drm_mode_config_cleanup(dev);
  1785. }
  1786. /* current intel driver doesn't take advantage of encoders
  1787. always give back the encoder for the connector
  1788. */
  1789. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  1790. {
  1791. struct intel_output *intel_output = to_intel_output(connector);
  1792. return &intel_output->enc;
  1793. }