radeon_display.c 35 KB

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