radeon_display.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  1. /*
  2. * Copyright 2007-8 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the 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 COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors: Dave Airlie
  24. * Alex Deucher
  25. */
  26. #include "drmP.h"
  27. #include "radeon_drm.h"
  28. #include "radeon.h"
  29. #include "atom.h"
  30. #include <asm/div64.h>
  31. #include "drm_crtc_helper.h"
  32. #include "drm_edid.h"
  33. static int radeon_ddc_dump(struct drm_connector *connector);
  34. static void avivo_crtc_load_lut(struct drm_crtc *crtc)
  35. {
  36. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  37. struct drm_device *dev = crtc->dev;
  38. struct radeon_device *rdev = dev->dev_private;
  39. int i;
  40. DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
  41. WREG32(AVIVO_DC_LUTA_CONTROL + radeon_crtc->crtc_offset, 0);
  42. WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
  43. WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
  44. WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
  45. WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
  46. WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
  47. WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
  48. WREG32(AVIVO_DC_LUT_RW_SELECT, radeon_crtc->crtc_id);
  49. WREG32(AVIVO_DC_LUT_RW_MODE, 0);
  50. WREG32(AVIVO_DC_LUT_WRITE_EN_MASK, 0x0000003f);
  51. WREG8(AVIVO_DC_LUT_RW_INDEX, 0);
  52. for (i = 0; i < 256; i++) {
  53. WREG32(AVIVO_DC_LUT_30_COLOR,
  54. (radeon_crtc->lut_r[i] << 20) |
  55. (radeon_crtc->lut_g[i] << 10) |
  56. (radeon_crtc->lut_b[i] << 0));
  57. }
  58. WREG32(AVIVO_D1GRPH_LUT_SEL + radeon_crtc->crtc_offset, radeon_crtc->crtc_id);
  59. }
  60. static void evergreen_crtc_load_lut(struct drm_crtc *crtc)
  61. {
  62. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  63. struct drm_device *dev = crtc->dev;
  64. struct radeon_device *rdev = dev->dev_private;
  65. int i;
  66. DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
  67. WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
  68. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
  69. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
  70. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
  71. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
  72. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
  73. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
  74. WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
  75. WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
  76. WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
  77. for (i = 0; i < 256; i++) {
  78. WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
  79. (radeon_crtc->lut_r[i] << 20) |
  80. (radeon_crtc->lut_g[i] << 10) |
  81. (radeon_crtc->lut_b[i] << 0));
  82. }
  83. }
  84. static void legacy_crtc_load_lut(struct drm_crtc *crtc)
  85. {
  86. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  87. struct drm_device *dev = crtc->dev;
  88. struct radeon_device *rdev = dev->dev_private;
  89. int i;
  90. uint32_t dac2_cntl;
  91. dac2_cntl = RREG32(RADEON_DAC_CNTL2);
  92. if (radeon_crtc->crtc_id == 0)
  93. dac2_cntl &= (uint32_t)~RADEON_DAC2_PALETTE_ACC_CTL;
  94. else
  95. dac2_cntl |= RADEON_DAC2_PALETTE_ACC_CTL;
  96. WREG32(RADEON_DAC_CNTL2, dac2_cntl);
  97. WREG8(RADEON_PALETTE_INDEX, 0);
  98. for (i = 0; i < 256; i++) {
  99. WREG32(RADEON_PALETTE_30_DATA,
  100. (radeon_crtc->lut_r[i] << 20) |
  101. (radeon_crtc->lut_g[i] << 10) |
  102. (radeon_crtc->lut_b[i] << 0));
  103. }
  104. }
  105. void radeon_crtc_load_lut(struct drm_crtc *crtc)
  106. {
  107. struct drm_device *dev = crtc->dev;
  108. struct radeon_device *rdev = dev->dev_private;
  109. if (!crtc->enabled)
  110. return;
  111. if (ASIC_IS_DCE4(rdev))
  112. evergreen_crtc_load_lut(crtc);
  113. else if (ASIC_IS_AVIVO(rdev))
  114. avivo_crtc_load_lut(crtc);
  115. else
  116. legacy_crtc_load_lut(crtc);
  117. }
  118. void radeon_crtc_save_lut(struct drm_crtc *crtc)
  119. {
  120. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  121. int i;
  122. if (!crtc->enabled)
  123. return;
  124. for (i = 0; i < 256; i++) {
  125. radeon_crtc->lut_r_copy[i] = radeon_crtc->lut_r[i];
  126. radeon_crtc->lut_g_copy[i] = radeon_crtc->lut_g[i];
  127. radeon_crtc->lut_b_copy[i] = radeon_crtc->lut_b[i];
  128. }
  129. }
  130. void radeon_crtc_restore_lut(struct drm_crtc *crtc)
  131. {
  132. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  133. int i;
  134. if (!crtc->enabled)
  135. return;
  136. for (i = 0; i < 256; i++) {
  137. radeon_crtc->lut_r[i] = radeon_crtc->lut_r_copy[i];
  138. radeon_crtc->lut_g[i] = radeon_crtc->lut_g_copy[i];
  139. radeon_crtc->lut_b[i] = radeon_crtc->lut_b_copy[i];
  140. }
  141. radeon_crtc_load_lut(crtc);
  142. }
  143. /** Sets the color ramps on behalf of fbcon */
  144. void radeon_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  145. u16 blue, int regno)
  146. {
  147. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  148. radeon_crtc->lut_r[regno] = red >> 6;
  149. radeon_crtc->lut_g[regno] = green >> 6;
  150. radeon_crtc->lut_b[regno] = blue >> 6;
  151. }
  152. /** Gets the color ramps on behalf of fbcon */
  153. void radeon_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  154. u16 *blue, int regno)
  155. {
  156. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  157. *red = radeon_crtc->lut_r[regno] << 6;
  158. *green = radeon_crtc->lut_g[regno] << 6;
  159. *blue = radeon_crtc->lut_b[regno] << 6;
  160. }
  161. static void radeon_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  162. u16 *blue, uint32_t start, uint32_t size)
  163. {
  164. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  165. int end = (start + size > 256) ? 256 : start + size, i;
  166. /* userspace palettes are always correct as is */
  167. for (i = start; i < end; i++) {
  168. radeon_crtc->lut_r[i] = red[i] >> 6;
  169. radeon_crtc->lut_g[i] = green[i] >> 6;
  170. radeon_crtc->lut_b[i] = blue[i] >> 6;
  171. }
  172. radeon_crtc_load_lut(crtc);
  173. }
  174. static void radeon_crtc_destroy(struct drm_crtc *crtc)
  175. {
  176. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  177. drm_crtc_cleanup(crtc);
  178. kfree(radeon_crtc);
  179. }
  180. static const struct drm_crtc_funcs radeon_crtc_funcs = {
  181. .cursor_set = radeon_crtc_cursor_set,
  182. .cursor_move = radeon_crtc_cursor_move,
  183. .gamma_set = radeon_crtc_gamma_set,
  184. .set_config = drm_crtc_helper_set_config,
  185. .destroy = radeon_crtc_destroy,
  186. };
  187. static void radeon_crtc_init(struct drm_device *dev, int index)
  188. {
  189. struct radeon_device *rdev = dev->dev_private;
  190. struct radeon_crtc *radeon_crtc;
  191. int i;
  192. radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  193. if (radeon_crtc == NULL)
  194. return;
  195. drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
  196. drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
  197. radeon_crtc->crtc_id = index;
  198. rdev->mode_info.crtcs[index] = radeon_crtc;
  199. #if 0
  200. radeon_crtc->mode_set.crtc = &radeon_crtc->base;
  201. radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
  202. radeon_crtc->mode_set.num_connectors = 0;
  203. #endif
  204. for (i = 0; i < 256; i++) {
  205. radeon_crtc->lut_r[i] = i << 2;
  206. radeon_crtc->lut_g[i] = i << 2;
  207. radeon_crtc->lut_b[i] = i << 2;
  208. }
  209. if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
  210. radeon_atombios_init_crtc(dev, radeon_crtc);
  211. else
  212. radeon_legacy_init_crtc(dev, radeon_crtc);
  213. }
  214. static const char *encoder_names[34] = {
  215. "NONE",
  216. "INTERNAL_LVDS",
  217. "INTERNAL_TMDS1",
  218. "INTERNAL_TMDS2",
  219. "INTERNAL_DAC1",
  220. "INTERNAL_DAC2",
  221. "INTERNAL_SDVOA",
  222. "INTERNAL_SDVOB",
  223. "SI170B",
  224. "CH7303",
  225. "CH7301",
  226. "INTERNAL_DVO1",
  227. "EXTERNAL_SDVOA",
  228. "EXTERNAL_SDVOB",
  229. "TITFP513",
  230. "INTERNAL_LVTM1",
  231. "VT1623",
  232. "HDMI_SI1930",
  233. "HDMI_INTERNAL",
  234. "INTERNAL_KLDSCP_TMDS1",
  235. "INTERNAL_KLDSCP_DVO1",
  236. "INTERNAL_KLDSCP_DAC1",
  237. "INTERNAL_KLDSCP_DAC2",
  238. "SI178",
  239. "MVPU_FPGA",
  240. "INTERNAL_DDI",
  241. "VT1625",
  242. "HDMI_SI1932",
  243. "DP_AN9801",
  244. "DP_DP501",
  245. "INTERNAL_UNIPHY",
  246. "INTERNAL_KLDSCP_LVTMA",
  247. "INTERNAL_UNIPHY1",
  248. "INTERNAL_UNIPHY2",
  249. };
  250. static const char *connector_names[15] = {
  251. "Unknown",
  252. "VGA",
  253. "DVI-I",
  254. "DVI-D",
  255. "DVI-A",
  256. "Composite",
  257. "S-video",
  258. "LVDS",
  259. "Component",
  260. "DIN",
  261. "DisplayPort",
  262. "HDMI-A",
  263. "HDMI-B",
  264. "TV",
  265. "eDP",
  266. };
  267. static const char *hpd_names[6] = {
  268. "HPD1",
  269. "HPD2",
  270. "HPD3",
  271. "HPD4",
  272. "HPD5",
  273. "HPD6",
  274. };
  275. static void radeon_print_display_setup(struct drm_device *dev)
  276. {
  277. struct drm_connector *connector;
  278. struct radeon_connector *radeon_connector;
  279. struct drm_encoder *encoder;
  280. struct radeon_encoder *radeon_encoder;
  281. uint32_t devices;
  282. int i = 0;
  283. DRM_INFO("Radeon Display Connectors\n");
  284. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  285. radeon_connector = to_radeon_connector(connector);
  286. DRM_INFO("Connector %d:\n", i);
  287. DRM_INFO(" %s\n", connector_names[connector->connector_type]);
  288. if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
  289. DRM_INFO(" %s\n", hpd_names[radeon_connector->hpd.hpd]);
  290. if (radeon_connector->ddc_bus) {
  291. DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
  292. radeon_connector->ddc_bus->rec.mask_clk_reg,
  293. radeon_connector->ddc_bus->rec.mask_data_reg,
  294. radeon_connector->ddc_bus->rec.a_clk_reg,
  295. radeon_connector->ddc_bus->rec.a_data_reg,
  296. radeon_connector->ddc_bus->rec.en_clk_reg,
  297. radeon_connector->ddc_bus->rec.en_data_reg,
  298. radeon_connector->ddc_bus->rec.y_clk_reg,
  299. radeon_connector->ddc_bus->rec.y_data_reg);
  300. if (radeon_connector->router_bus)
  301. DRM_INFO(" DDC Router 0x%x/0x%x\n",
  302. radeon_connector->router.mux_control_pin,
  303. radeon_connector->router.mux_state);
  304. } else {
  305. if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
  306. connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
  307. connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
  308. connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
  309. connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
  310. connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
  311. DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
  312. }
  313. DRM_INFO(" Encoders:\n");
  314. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  315. radeon_encoder = to_radeon_encoder(encoder);
  316. devices = radeon_encoder->devices & radeon_connector->devices;
  317. if (devices) {
  318. if (devices & ATOM_DEVICE_CRT1_SUPPORT)
  319. DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  320. if (devices & ATOM_DEVICE_CRT2_SUPPORT)
  321. DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  322. if (devices & ATOM_DEVICE_LCD1_SUPPORT)
  323. DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  324. if (devices & ATOM_DEVICE_DFP1_SUPPORT)
  325. DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  326. if (devices & ATOM_DEVICE_DFP2_SUPPORT)
  327. DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  328. if (devices & ATOM_DEVICE_DFP3_SUPPORT)
  329. DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
  330. if (devices & ATOM_DEVICE_DFP4_SUPPORT)
  331. DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
  332. if (devices & ATOM_DEVICE_DFP5_SUPPORT)
  333. DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
  334. if (devices & ATOM_DEVICE_DFP6_SUPPORT)
  335. DRM_INFO(" DFP6: %s\n", encoder_names[radeon_encoder->encoder_id]);
  336. if (devices & ATOM_DEVICE_TV1_SUPPORT)
  337. DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  338. if (devices & ATOM_DEVICE_CV_SUPPORT)
  339. DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
  340. }
  341. }
  342. i++;
  343. }
  344. }
  345. static bool radeon_setup_enc_conn(struct drm_device *dev)
  346. {
  347. struct radeon_device *rdev = dev->dev_private;
  348. struct drm_connector *drm_connector;
  349. bool ret = false;
  350. if (rdev->bios) {
  351. if (rdev->is_atom_bios) {
  352. ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
  353. if (ret == false)
  354. ret = radeon_get_atom_connector_info_from_object_table(dev);
  355. } else {
  356. ret = radeon_get_legacy_connector_info_from_bios(dev);
  357. if (ret == false)
  358. ret = radeon_get_legacy_connector_info_from_table(dev);
  359. }
  360. } else {
  361. if (!ASIC_IS_AVIVO(rdev))
  362. ret = radeon_get_legacy_connector_info_from_table(dev);
  363. }
  364. if (ret) {
  365. radeon_setup_encoder_clones(dev);
  366. radeon_print_display_setup(dev);
  367. list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head)
  368. radeon_ddc_dump(drm_connector);
  369. }
  370. return ret;
  371. }
  372. int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
  373. {
  374. struct drm_device *dev = radeon_connector->base.dev;
  375. struct radeon_device *rdev = dev->dev_private;
  376. int ret = 0;
  377. /* on hw with routers, select right port */
  378. if (radeon_connector->router.valid)
  379. radeon_router_select_port(radeon_connector);
  380. if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
  381. (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)) {
  382. struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
  383. if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
  384. dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) && dig->dp_i2c_bus)
  385. radeon_connector->edid = drm_get_edid(&radeon_connector->base, &dig->dp_i2c_bus->adapter);
  386. }
  387. if (!radeon_connector->ddc_bus)
  388. return -1;
  389. if (!radeon_connector->edid) {
  390. radeon_connector->edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
  391. }
  392. /* some servers provide a hardcoded edid in rom for KVMs */
  393. if (!radeon_connector->edid)
  394. radeon_connector->edid = radeon_combios_get_hardcoded_edid(rdev);
  395. if (radeon_connector->edid) {
  396. drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
  397. ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
  398. return ret;
  399. }
  400. drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
  401. return 0;
  402. }
  403. static int radeon_ddc_dump(struct drm_connector *connector)
  404. {
  405. struct edid *edid;
  406. struct radeon_connector *radeon_connector = to_radeon_connector(connector);
  407. int ret = 0;
  408. /* on hw with routers, select right port */
  409. if (radeon_connector->router.valid)
  410. radeon_router_select_port(radeon_connector);
  411. if (!radeon_connector->ddc_bus)
  412. return -1;
  413. edid = drm_get_edid(connector, &radeon_connector->ddc_bus->adapter);
  414. if (edid) {
  415. kfree(edid);
  416. }
  417. return ret;
  418. }
  419. static inline uint32_t radeon_div(uint64_t n, uint32_t d)
  420. {
  421. uint64_t mod;
  422. n += d / 2;
  423. mod = do_div(n, d);
  424. return n;
  425. }
  426. static void radeon_compute_pll_legacy(struct radeon_pll *pll,
  427. uint64_t freq,
  428. uint32_t *dot_clock_p,
  429. uint32_t *fb_div_p,
  430. uint32_t *frac_fb_div_p,
  431. uint32_t *ref_div_p,
  432. uint32_t *post_div_p)
  433. {
  434. uint32_t min_ref_div = pll->min_ref_div;
  435. uint32_t max_ref_div = pll->max_ref_div;
  436. uint32_t min_post_div = pll->min_post_div;
  437. uint32_t max_post_div = pll->max_post_div;
  438. uint32_t min_fractional_feed_div = 0;
  439. uint32_t max_fractional_feed_div = 0;
  440. uint32_t best_vco = pll->best_vco;
  441. uint32_t best_post_div = 1;
  442. uint32_t best_ref_div = 1;
  443. uint32_t best_feedback_div = 1;
  444. uint32_t best_frac_feedback_div = 0;
  445. uint32_t best_freq = -1;
  446. uint32_t best_error = 0xffffffff;
  447. uint32_t best_vco_diff = 1;
  448. uint32_t post_div;
  449. u32 pll_out_min, pll_out_max;
  450. DRM_DEBUG_KMS("PLL freq %llu %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
  451. freq = freq * 1000;
  452. if (pll->flags & RADEON_PLL_IS_LCD) {
  453. pll_out_min = pll->lcd_pll_out_min;
  454. pll_out_max = pll->lcd_pll_out_max;
  455. } else {
  456. pll_out_min = pll->pll_out_min;
  457. pll_out_max = pll->pll_out_max;
  458. }
  459. if (pll->flags & RADEON_PLL_USE_REF_DIV)
  460. min_ref_div = max_ref_div = pll->reference_div;
  461. else {
  462. while (min_ref_div < max_ref_div-1) {
  463. uint32_t mid = (min_ref_div + max_ref_div) / 2;
  464. uint32_t pll_in = pll->reference_freq / mid;
  465. if (pll_in < pll->pll_in_min)
  466. max_ref_div = mid;
  467. else if (pll_in > pll->pll_in_max)
  468. min_ref_div = mid;
  469. else
  470. break;
  471. }
  472. }
  473. if (pll->flags & RADEON_PLL_USE_POST_DIV)
  474. min_post_div = max_post_div = pll->post_div;
  475. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  476. min_fractional_feed_div = pll->min_frac_feedback_div;
  477. max_fractional_feed_div = pll->max_frac_feedback_div;
  478. }
  479. for (post_div = min_post_div; post_div <= max_post_div; ++post_div) {
  480. uint32_t ref_div;
  481. if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
  482. continue;
  483. /* legacy radeons only have a few post_divs */
  484. if (pll->flags & RADEON_PLL_LEGACY) {
  485. if ((post_div == 5) ||
  486. (post_div == 7) ||
  487. (post_div == 9) ||
  488. (post_div == 10) ||
  489. (post_div == 11) ||
  490. (post_div == 13) ||
  491. (post_div == 14) ||
  492. (post_div == 15))
  493. continue;
  494. }
  495. for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
  496. uint32_t feedback_div, current_freq = 0, error, vco_diff;
  497. uint32_t pll_in = pll->reference_freq / ref_div;
  498. uint32_t min_feed_div = pll->min_feedback_div;
  499. uint32_t max_feed_div = pll->max_feedback_div + 1;
  500. if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
  501. continue;
  502. while (min_feed_div < max_feed_div) {
  503. uint32_t vco;
  504. uint32_t min_frac_feed_div = min_fractional_feed_div;
  505. uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
  506. uint32_t frac_feedback_div;
  507. uint64_t tmp;
  508. feedback_div = (min_feed_div + max_feed_div) / 2;
  509. tmp = (uint64_t)pll->reference_freq * feedback_div;
  510. vco = radeon_div(tmp, ref_div);
  511. if (vco < pll_out_min) {
  512. min_feed_div = feedback_div + 1;
  513. continue;
  514. } else if (vco > pll_out_max) {
  515. max_feed_div = feedback_div;
  516. continue;
  517. }
  518. while (min_frac_feed_div < max_frac_feed_div) {
  519. frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
  520. tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
  521. tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
  522. current_freq = radeon_div(tmp, ref_div * post_div);
  523. if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
  524. if (freq < current_freq)
  525. error = 0xffffffff;
  526. else
  527. error = freq - current_freq;
  528. } else
  529. error = abs(current_freq - freq);
  530. vco_diff = abs(vco - best_vco);
  531. if ((best_vco == 0 && error < best_error) ||
  532. (best_vco != 0 &&
  533. ((best_error > 100 && error < best_error - 100) ||
  534. (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
  535. best_post_div = post_div;
  536. best_ref_div = ref_div;
  537. best_feedback_div = feedback_div;
  538. best_frac_feedback_div = frac_feedback_div;
  539. best_freq = current_freq;
  540. best_error = error;
  541. best_vco_diff = vco_diff;
  542. } else if (current_freq == freq) {
  543. if (best_freq == -1) {
  544. best_post_div = post_div;
  545. best_ref_div = ref_div;
  546. best_feedback_div = feedback_div;
  547. best_frac_feedback_div = frac_feedback_div;
  548. best_freq = current_freq;
  549. best_error = error;
  550. best_vco_diff = vco_diff;
  551. } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
  552. ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
  553. ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
  554. ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
  555. ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
  556. ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
  557. best_post_div = post_div;
  558. best_ref_div = ref_div;
  559. best_feedback_div = feedback_div;
  560. best_frac_feedback_div = frac_feedback_div;
  561. best_freq = current_freq;
  562. best_error = error;
  563. best_vco_diff = vco_diff;
  564. }
  565. }
  566. if (current_freq < freq)
  567. min_frac_feed_div = frac_feedback_div + 1;
  568. else
  569. max_frac_feed_div = frac_feedback_div;
  570. }
  571. if (current_freq < freq)
  572. min_feed_div = feedback_div + 1;
  573. else
  574. max_feed_div = feedback_div;
  575. }
  576. }
  577. }
  578. *dot_clock_p = best_freq / 10000;
  579. *fb_div_p = best_feedback_div;
  580. *frac_fb_div_p = best_frac_feedback_div;
  581. *ref_div_p = best_ref_div;
  582. *post_div_p = best_post_div;
  583. }
  584. static bool
  585. calc_fb_div(struct radeon_pll *pll,
  586. uint32_t freq,
  587. uint32_t post_div,
  588. uint32_t ref_div,
  589. uint32_t *fb_div,
  590. uint32_t *fb_div_frac)
  591. {
  592. fixed20_12 feedback_divider, a, b;
  593. u32 vco_freq;
  594. vco_freq = freq * post_div;
  595. /* feedback_divider = vco_freq * ref_div / pll->reference_freq; */
  596. a.full = dfixed_const(pll->reference_freq);
  597. feedback_divider.full = dfixed_const(vco_freq);
  598. feedback_divider.full = dfixed_div(feedback_divider, a);
  599. a.full = dfixed_const(ref_div);
  600. feedback_divider.full = dfixed_mul(feedback_divider, a);
  601. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  602. /* feedback_divider = floor((feedback_divider * 10.0) + 0.5) * 0.1; */
  603. a.full = dfixed_const(10);
  604. feedback_divider.full = dfixed_mul(feedback_divider, a);
  605. feedback_divider.full += dfixed_const_half(0);
  606. feedback_divider.full = dfixed_floor(feedback_divider);
  607. feedback_divider.full = dfixed_div(feedback_divider, a);
  608. /* *fb_div = floor(feedback_divider); */
  609. a.full = dfixed_floor(feedback_divider);
  610. *fb_div = dfixed_trunc(a);
  611. /* *fb_div_frac = fmod(feedback_divider, 1.0) * 10.0; */
  612. a.full = dfixed_const(10);
  613. b.full = dfixed_mul(feedback_divider, a);
  614. feedback_divider.full = dfixed_floor(feedback_divider);
  615. feedback_divider.full = dfixed_mul(feedback_divider, a);
  616. feedback_divider.full = b.full - feedback_divider.full;
  617. *fb_div_frac = dfixed_trunc(feedback_divider);
  618. } else {
  619. /* *fb_div = floor(feedback_divider + 0.5); */
  620. feedback_divider.full += dfixed_const_half(0);
  621. feedback_divider.full = dfixed_floor(feedback_divider);
  622. *fb_div = dfixed_trunc(feedback_divider);
  623. *fb_div_frac = 0;
  624. }
  625. if (((*fb_div) < pll->min_feedback_div) || ((*fb_div) > pll->max_feedback_div))
  626. return false;
  627. else
  628. return true;
  629. }
  630. static bool
  631. calc_fb_ref_div(struct radeon_pll *pll,
  632. uint32_t freq,
  633. uint32_t post_div,
  634. uint32_t *fb_div,
  635. uint32_t *fb_div_frac,
  636. uint32_t *ref_div)
  637. {
  638. fixed20_12 ffreq, max_error, error, pll_out, a;
  639. u32 vco;
  640. u32 pll_out_min, pll_out_max;
  641. if (pll->flags & RADEON_PLL_IS_LCD) {
  642. pll_out_min = pll->lcd_pll_out_min;
  643. pll_out_max = pll->lcd_pll_out_max;
  644. } else {
  645. pll_out_min = pll->pll_out_min;
  646. pll_out_max = pll->pll_out_max;
  647. }
  648. ffreq.full = dfixed_const(freq);
  649. /* max_error = ffreq * 0.0025; */
  650. a.full = dfixed_const(400);
  651. max_error.full = dfixed_div(ffreq, a);
  652. for ((*ref_div) = pll->min_ref_div; (*ref_div) < pll->max_ref_div; ++(*ref_div)) {
  653. if (calc_fb_div(pll, freq, post_div, (*ref_div), fb_div, fb_div_frac)) {
  654. vco = pll->reference_freq * (((*fb_div) * 10) + (*fb_div_frac));
  655. vco = vco / ((*ref_div) * 10);
  656. if ((vco < pll_out_min) || (vco > pll_out_max))
  657. continue;
  658. /* pll_out = vco / post_div; */
  659. a.full = dfixed_const(post_div);
  660. pll_out.full = dfixed_const(vco);
  661. pll_out.full = dfixed_div(pll_out, a);
  662. if (pll_out.full >= ffreq.full) {
  663. error.full = pll_out.full - ffreq.full;
  664. if (error.full <= max_error.full)
  665. return true;
  666. }
  667. }
  668. }
  669. return false;
  670. }
  671. static void radeon_compute_pll_new(struct radeon_pll *pll,
  672. uint64_t freq,
  673. uint32_t *dot_clock_p,
  674. uint32_t *fb_div_p,
  675. uint32_t *frac_fb_div_p,
  676. uint32_t *ref_div_p,
  677. uint32_t *post_div_p)
  678. {
  679. u32 fb_div = 0, fb_div_frac = 0, post_div = 0, ref_div = 0;
  680. u32 best_freq = 0, vco_frequency;
  681. u32 pll_out_min, pll_out_max;
  682. if (pll->flags & RADEON_PLL_IS_LCD) {
  683. pll_out_min = pll->lcd_pll_out_min;
  684. pll_out_max = pll->lcd_pll_out_max;
  685. } else {
  686. pll_out_min = pll->pll_out_min;
  687. pll_out_max = pll->pll_out_max;
  688. }
  689. /* freq = freq / 10; */
  690. do_div(freq, 10);
  691. if (pll->flags & RADEON_PLL_USE_POST_DIV) {
  692. post_div = pll->post_div;
  693. if ((post_div < pll->min_post_div) || (post_div > pll->max_post_div))
  694. goto done;
  695. vco_frequency = freq * post_div;
  696. if ((vco_frequency < pll_out_min) || (vco_frequency > pll_out_max))
  697. goto done;
  698. if (pll->flags & RADEON_PLL_USE_REF_DIV) {
  699. ref_div = pll->reference_div;
  700. if ((ref_div < pll->min_ref_div) || (ref_div > pll->max_ref_div))
  701. goto done;
  702. if (!calc_fb_div(pll, freq, post_div, ref_div, &fb_div, &fb_div_frac))
  703. goto done;
  704. }
  705. } else {
  706. for (post_div = pll->max_post_div; post_div >= pll->min_post_div; --post_div) {
  707. if (pll->flags & RADEON_PLL_LEGACY) {
  708. if ((post_div == 5) ||
  709. (post_div == 7) ||
  710. (post_div == 9) ||
  711. (post_div == 10) ||
  712. (post_div == 11))
  713. continue;
  714. }
  715. if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
  716. continue;
  717. vco_frequency = freq * post_div;
  718. if ((vco_frequency < pll_out_min) || (vco_frequency > pll_out_max))
  719. continue;
  720. if (pll->flags & RADEON_PLL_USE_REF_DIV) {
  721. ref_div = pll->reference_div;
  722. if ((ref_div < pll->min_ref_div) || (ref_div > pll->max_ref_div))
  723. goto done;
  724. if (calc_fb_div(pll, freq, post_div, ref_div, &fb_div, &fb_div_frac))
  725. break;
  726. } else {
  727. if (calc_fb_ref_div(pll, freq, post_div, &fb_div, &fb_div_frac, &ref_div))
  728. break;
  729. }
  730. }
  731. }
  732. best_freq = pll->reference_freq * 10 * fb_div;
  733. best_freq += pll->reference_freq * fb_div_frac;
  734. best_freq = best_freq / (ref_div * post_div);
  735. done:
  736. if (best_freq == 0)
  737. DRM_ERROR("Couldn't find valid PLL dividers\n");
  738. *dot_clock_p = best_freq / 10;
  739. *fb_div_p = fb_div;
  740. *frac_fb_div_p = fb_div_frac;
  741. *ref_div_p = ref_div;
  742. *post_div_p = post_div;
  743. DRM_DEBUG_KMS("%u %d.%d, %d, %d\n", *dot_clock_p, *fb_div_p, *frac_fb_div_p, *ref_div_p, *post_div_p);
  744. }
  745. void radeon_compute_pll(struct radeon_pll *pll,
  746. uint64_t freq,
  747. uint32_t *dot_clock_p,
  748. uint32_t *fb_div_p,
  749. uint32_t *frac_fb_div_p,
  750. uint32_t *ref_div_p,
  751. uint32_t *post_div_p)
  752. {
  753. switch (pll->algo) {
  754. case PLL_ALGO_NEW:
  755. radeon_compute_pll_new(pll, freq, dot_clock_p, fb_div_p,
  756. frac_fb_div_p, ref_div_p, post_div_p);
  757. break;
  758. case PLL_ALGO_LEGACY:
  759. default:
  760. radeon_compute_pll_legacy(pll, freq, dot_clock_p, fb_div_p,
  761. frac_fb_div_p, ref_div_p, post_div_p);
  762. break;
  763. }
  764. }
  765. static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
  766. {
  767. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  768. if (radeon_fb->obj) {
  769. drm_gem_object_unreference_unlocked(radeon_fb->obj);
  770. }
  771. drm_framebuffer_cleanup(fb);
  772. kfree(radeon_fb);
  773. }
  774. static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  775. struct drm_file *file_priv,
  776. unsigned int *handle)
  777. {
  778. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  779. return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
  780. }
  781. static const struct drm_framebuffer_funcs radeon_fb_funcs = {
  782. .destroy = radeon_user_framebuffer_destroy,
  783. .create_handle = radeon_user_framebuffer_create_handle,
  784. };
  785. void
  786. radeon_framebuffer_init(struct drm_device *dev,
  787. struct radeon_framebuffer *rfb,
  788. struct drm_mode_fb_cmd *mode_cmd,
  789. struct drm_gem_object *obj)
  790. {
  791. rfb->obj = obj;
  792. drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
  793. drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
  794. }
  795. static struct drm_framebuffer *
  796. radeon_user_framebuffer_create(struct drm_device *dev,
  797. struct drm_file *file_priv,
  798. struct drm_mode_fb_cmd *mode_cmd)
  799. {
  800. struct drm_gem_object *obj;
  801. struct radeon_framebuffer *radeon_fb;
  802. obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
  803. if (obj == NULL) {
  804. dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
  805. "can't create framebuffer\n", mode_cmd->handle);
  806. return ERR_PTR(-ENOENT);
  807. }
  808. radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
  809. if (radeon_fb == NULL)
  810. return ERR_PTR(-ENOMEM);
  811. radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
  812. return &radeon_fb->base;
  813. }
  814. static void radeon_output_poll_changed(struct drm_device *dev)
  815. {
  816. struct radeon_device *rdev = dev->dev_private;
  817. radeon_fb_output_poll_changed(rdev);
  818. }
  819. static const struct drm_mode_config_funcs radeon_mode_funcs = {
  820. .fb_create = radeon_user_framebuffer_create,
  821. .output_poll_changed = radeon_output_poll_changed
  822. };
  823. struct drm_prop_enum_list {
  824. int type;
  825. char *name;
  826. };
  827. static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
  828. { { 0, "driver" },
  829. { 1, "bios" },
  830. };
  831. static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
  832. { { TV_STD_NTSC, "ntsc" },
  833. { TV_STD_PAL, "pal" },
  834. { TV_STD_PAL_M, "pal-m" },
  835. { TV_STD_PAL_60, "pal-60" },
  836. { TV_STD_NTSC_J, "ntsc-j" },
  837. { TV_STD_SCART_PAL, "scart-pal" },
  838. { TV_STD_PAL_CN, "pal-cn" },
  839. { TV_STD_SECAM, "secam" },
  840. };
  841. static struct drm_prop_enum_list radeon_underscan_enum_list[] =
  842. { { UNDERSCAN_OFF, "off" },
  843. { UNDERSCAN_ON, "on" },
  844. { UNDERSCAN_AUTO, "auto" },
  845. };
  846. static int radeon_modeset_create_props(struct radeon_device *rdev)
  847. {
  848. int i, sz;
  849. if (rdev->is_atom_bios) {
  850. rdev->mode_info.coherent_mode_property =
  851. drm_property_create(rdev->ddev,
  852. DRM_MODE_PROP_RANGE,
  853. "coherent", 2);
  854. if (!rdev->mode_info.coherent_mode_property)
  855. return -ENOMEM;
  856. rdev->mode_info.coherent_mode_property->values[0] = 0;
  857. rdev->mode_info.coherent_mode_property->values[1] = 1;
  858. }
  859. if (!ASIC_IS_AVIVO(rdev)) {
  860. sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
  861. rdev->mode_info.tmds_pll_property =
  862. drm_property_create(rdev->ddev,
  863. DRM_MODE_PROP_ENUM,
  864. "tmds_pll", sz);
  865. for (i = 0; i < sz; i++) {
  866. drm_property_add_enum(rdev->mode_info.tmds_pll_property,
  867. i,
  868. radeon_tmds_pll_enum_list[i].type,
  869. radeon_tmds_pll_enum_list[i].name);
  870. }
  871. }
  872. rdev->mode_info.load_detect_property =
  873. drm_property_create(rdev->ddev,
  874. DRM_MODE_PROP_RANGE,
  875. "load detection", 2);
  876. if (!rdev->mode_info.load_detect_property)
  877. return -ENOMEM;
  878. rdev->mode_info.load_detect_property->values[0] = 0;
  879. rdev->mode_info.load_detect_property->values[1] = 1;
  880. drm_mode_create_scaling_mode_property(rdev->ddev);
  881. sz = ARRAY_SIZE(radeon_tv_std_enum_list);
  882. rdev->mode_info.tv_std_property =
  883. drm_property_create(rdev->ddev,
  884. DRM_MODE_PROP_ENUM,
  885. "tv standard", sz);
  886. for (i = 0; i < sz; i++) {
  887. drm_property_add_enum(rdev->mode_info.tv_std_property,
  888. i,
  889. radeon_tv_std_enum_list[i].type,
  890. radeon_tv_std_enum_list[i].name);
  891. }
  892. sz = ARRAY_SIZE(radeon_underscan_enum_list);
  893. rdev->mode_info.underscan_property =
  894. drm_property_create(rdev->ddev,
  895. DRM_MODE_PROP_ENUM,
  896. "underscan", sz);
  897. for (i = 0; i < sz; i++) {
  898. drm_property_add_enum(rdev->mode_info.underscan_property,
  899. i,
  900. radeon_underscan_enum_list[i].type,
  901. radeon_underscan_enum_list[i].name);
  902. }
  903. return 0;
  904. }
  905. void radeon_update_display_priority(struct radeon_device *rdev)
  906. {
  907. /* adjustment options for the display watermarks */
  908. if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
  909. /* set display priority to high for r3xx, rv515 chips
  910. * this avoids flickering due to underflow to the
  911. * display controllers during heavy acceleration.
  912. * Don't force high on rs4xx igp chips as it seems to
  913. * affect the sound card. See kernel bug 15982.
  914. */
  915. if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
  916. !(rdev->flags & RADEON_IS_IGP))
  917. rdev->disp_priority = 2;
  918. else
  919. rdev->disp_priority = 0;
  920. } else
  921. rdev->disp_priority = radeon_disp_priority;
  922. }
  923. int radeon_modeset_init(struct radeon_device *rdev)
  924. {
  925. int i;
  926. int ret;
  927. drm_mode_config_init(rdev->ddev);
  928. rdev->mode_info.mode_config_initialized = true;
  929. rdev->ddev->mode_config.funcs = (void *)&radeon_mode_funcs;
  930. if (ASIC_IS_AVIVO(rdev)) {
  931. rdev->ddev->mode_config.max_width = 8192;
  932. rdev->ddev->mode_config.max_height = 8192;
  933. } else {
  934. rdev->ddev->mode_config.max_width = 4096;
  935. rdev->ddev->mode_config.max_height = 4096;
  936. }
  937. rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
  938. ret = radeon_modeset_create_props(rdev);
  939. if (ret) {
  940. return ret;
  941. }
  942. /* init i2c buses */
  943. radeon_i2c_init(rdev);
  944. /* check combios for a valid hardcoded EDID - Sun servers */
  945. if (!rdev->is_atom_bios) {
  946. /* check for hardcoded EDID in BIOS */
  947. radeon_combios_check_hardcoded_edid(rdev);
  948. }
  949. /* allocate crtcs */
  950. for (i = 0; i < rdev->num_crtc; i++) {
  951. radeon_crtc_init(rdev->ddev, i);
  952. }
  953. /* okay we should have all the bios connectors */
  954. ret = radeon_setup_enc_conn(rdev->ddev);
  955. if (!ret) {
  956. return ret;
  957. }
  958. /* initialize hpd */
  959. radeon_hpd_init(rdev);
  960. /* Initialize power management */
  961. radeon_pm_init(rdev);
  962. radeon_fbdev_init(rdev);
  963. drm_kms_helper_poll_init(rdev->ddev);
  964. return 0;
  965. }
  966. void radeon_modeset_fini(struct radeon_device *rdev)
  967. {
  968. radeon_fbdev_fini(rdev);
  969. kfree(rdev->mode_info.bios_hardcoded_edid);
  970. radeon_pm_fini(rdev);
  971. if (rdev->mode_info.mode_config_initialized) {
  972. drm_kms_helper_poll_fini(rdev->ddev);
  973. radeon_hpd_fini(rdev);
  974. drm_mode_config_cleanup(rdev->ddev);
  975. rdev->mode_info.mode_config_initialized = false;
  976. }
  977. /* free i2c buses */
  978. radeon_i2c_fini(rdev);
  979. }
  980. static bool is_hdtv_mode(struct drm_display_mode *mode)
  981. {
  982. /* try and guess if this is a tv or a monitor */
  983. if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
  984. (mode->vdisplay == 576) || /* 576p */
  985. (mode->vdisplay == 720) || /* 720p */
  986. (mode->vdisplay == 1080)) /* 1080p */
  987. return true;
  988. else
  989. return false;
  990. }
  991. bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
  992. struct drm_display_mode *mode,
  993. struct drm_display_mode *adjusted_mode)
  994. {
  995. struct drm_device *dev = crtc->dev;
  996. struct radeon_device *rdev = dev->dev_private;
  997. struct drm_encoder *encoder;
  998. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  999. struct radeon_encoder *radeon_encoder;
  1000. struct drm_connector *connector;
  1001. struct radeon_connector *radeon_connector;
  1002. bool first = true;
  1003. u32 src_v = 1, dst_v = 1;
  1004. u32 src_h = 1, dst_h = 1;
  1005. radeon_crtc->h_border = 0;
  1006. radeon_crtc->v_border = 0;
  1007. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  1008. if (encoder->crtc != crtc)
  1009. continue;
  1010. radeon_encoder = to_radeon_encoder(encoder);
  1011. connector = radeon_get_connector_for_encoder(encoder);
  1012. radeon_connector = to_radeon_connector(connector);
  1013. if (first) {
  1014. /* set scaling */
  1015. if (radeon_encoder->rmx_type == RMX_OFF)
  1016. radeon_crtc->rmx_type = RMX_OFF;
  1017. else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
  1018. mode->vdisplay < radeon_encoder->native_mode.vdisplay)
  1019. radeon_crtc->rmx_type = radeon_encoder->rmx_type;
  1020. else
  1021. radeon_crtc->rmx_type = RMX_OFF;
  1022. /* copy native mode */
  1023. memcpy(&radeon_crtc->native_mode,
  1024. &radeon_encoder->native_mode,
  1025. sizeof(struct drm_display_mode));
  1026. src_v = crtc->mode.vdisplay;
  1027. dst_v = radeon_crtc->native_mode.vdisplay;
  1028. src_h = crtc->mode.hdisplay;
  1029. dst_h = radeon_crtc->native_mode.hdisplay;
  1030. /* fix up for overscan on hdmi */
  1031. if (ASIC_IS_AVIVO(rdev) &&
  1032. (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
  1033. ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
  1034. ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
  1035. drm_detect_hdmi_monitor(radeon_connector->edid) &&
  1036. is_hdtv_mode(mode)))) {
  1037. radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
  1038. radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
  1039. radeon_crtc->rmx_type = RMX_FULL;
  1040. src_v = crtc->mode.vdisplay;
  1041. dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
  1042. src_h = crtc->mode.hdisplay;
  1043. dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
  1044. }
  1045. first = false;
  1046. } else {
  1047. if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
  1048. /* WARNING: Right now this can't happen but
  1049. * in the future we need to check that scaling
  1050. * are consistent across different encoder
  1051. * (ie all encoder can work with the same
  1052. * scaling).
  1053. */
  1054. DRM_ERROR("Scaling not consistent across encoder.\n");
  1055. return false;
  1056. }
  1057. }
  1058. }
  1059. if (radeon_crtc->rmx_type != RMX_OFF) {
  1060. fixed20_12 a, b;
  1061. a.full = dfixed_const(src_v);
  1062. b.full = dfixed_const(dst_v);
  1063. radeon_crtc->vsc.full = dfixed_div(a, b);
  1064. a.full = dfixed_const(src_h);
  1065. b.full = dfixed_const(dst_h);
  1066. radeon_crtc->hsc.full = dfixed_div(a, b);
  1067. } else {
  1068. radeon_crtc->vsc.full = dfixed_const(1);
  1069. radeon_crtc->hsc.full = dfixed_const(1);
  1070. }
  1071. return true;
  1072. }