radeon_display.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  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. void radeon_compute_pll(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 = max_post_div; post_div >= min_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 void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
  585. {
  586. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  587. if (radeon_fb->obj) {
  588. drm_gem_object_unreference_unlocked(radeon_fb->obj);
  589. }
  590. drm_framebuffer_cleanup(fb);
  591. kfree(radeon_fb);
  592. }
  593. static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  594. struct drm_file *file_priv,
  595. unsigned int *handle)
  596. {
  597. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  598. return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
  599. }
  600. static const struct drm_framebuffer_funcs radeon_fb_funcs = {
  601. .destroy = radeon_user_framebuffer_destroy,
  602. .create_handle = radeon_user_framebuffer_create_handle,
  603. };
  604. void
  605. radeon_framebuffer_init(struct drm_device *dev,
  606. struct radeon_framebuffer *rfb,
  607. struct drm_mode_fb_cmd *mode_cmd,
  608. struct drm_gem_object *obj)
  609. {
  610. rfb->obj = obj;
  611. drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
  612. drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
  613. }
  614. static struct drm_framebuffer *
  615. radeon_user_framebuffer_create(struct drm_device *dev,
  616. struct drm_file *file_priv,
  617. struct drm_mode_fb_cmd *mode_cmd)
  618. {
  619. struct drm_gem_object *obj;
  620. struct radeon_framebuffer *radeon_fb;
  621. obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
  622. if (obj == NULL) {
  623. dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
  624. "can't create framebuffer\n", mode_cmd->handle);
  625. return ERR_PTR(-ENOENT);
  626. }
  627. radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
  628. if (radeon_fb == NULL)
  629. return ERR_PTR(-ENOMEM);
  630. radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
  631. return &radeon_fb->base;
  632. }
  633. static void radeon_output_poll_changed(struct drm_device *dev)
  634. {
  635. struct radeon_device *rdev = dev->dev_private;
  636. radeon_fb_output_poll_changed(rdev);
  637. }
  638. static const struct drm_mode_config_funcs radeon_mode_funcs = {
  639. .fb_create = radeon_user_framebuffer_create,
  640. .output_poll_changed = radeon_output_poll_changed
  641. };
  642. struct drm_prop_enum_list {
  643. int type;
  644. char *name;
  645. };
  646. static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
  647. { { 0, "driver" },
  648. { 1, "bios" },
  649. };
  650. static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
  651. { { TV_STD_NTSC, "ntsc" },
  652. { TV_STD_PAL, "pal" },
  653. { TV_STD_PAL_M, "pal-m" },
  654. { TV_STD_PAL_60, "pal-60" },
  655. { TV_STD_NTSC_J, "ntsc-j" },
  656. { TV_STD_SCART_PAL, "scart-pal" },
  657. { TV_STD_PAL_CN, "pal-cn" },
  658. { TV_STD_SECAM, "secam" },
  659. };
  660. static struct drm_prop_enum_list radeon_underscan_enum_list[] =
  661. { { UNDERSCAN_OFF, "off" },
  662. { UNDERSCAN_ON, "on" },
  663. { UNDERSCAN_AUTO, "auto" },
  664. };
  665. static int radeon_modeset_create_props(struct radeon_device *rdev)
  666. {
  667. int i, sz;
  668. if (rdev->is_atom_bios) {
  669. rdev->mode_info.coherent_mode_property =
  670. drm_property_create(rdev->ddev,
  671. DRM_MODE_PROP_RANGE,
  672. "coherent", 2);
  673. if (!rdev->mode_info.coherent_mode_property)
  674. return -ENOMEM;
  675. rdev->mode_info.coherent_mode_property->values[0] = 0;
  676. rdev->mode_info.coherent_mode_property->values[1] = 1;
  677. }
  678. if (!ASIC_IS_AVIVO(rdev)) {
  679. sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
  680. rdev->mode_info.tmds_pll_property =
  681. drm_property_create(rdev->ddev,
  682. DRM_MODE_PROP_ENUM,
  683. "tmds_pll", sz);
  684. for (i = 0; i < sz; i++) {
  685. drm_property_add_enum(rdev->mode_info.tmds_pll_property,
  686. i,
  687. radeon_tmds_pll_enum_list[i].type,
  688. radeon_tmds_pll_enum_list[i].name);
  689. }
  690. }
  691. rdev->mode_info.load_detect_property =
  692. drm_property_create(rdev->ddev,
  693. DRM_MODE_PROP_RANGE,
  694. "load detection", 2);
  695. if (!rdev->mode_info.load_detect_property)
  696. return -ENOMEM;
  697. rdev->mode_info.load_detect_property->values[0] = 0;
  698. rdev->mode_info.load_detect_property->values[1] = 1;
  699. drm_mode_create_scaling_mode_property(rdev->ddev);
  700. sz = ARRAY_SIZE(radeon_tv_std_enum_list);
  701. rdev->mode_info.tv_std_property =
  702. drm_property_create(rdev->ddev,
  703. DRM_MODE_PROP_ENUM,
  704. "tv standard", sz);
  705. for (i = 0; i < sz; i++) {
  706. drm_property_add_enum(rdev->mode_info.tv_std_property,
  707. i,
  708. radeon_tv_std_enum_list[i].type,
  709. radeon_tv_std_enum_list[i].name);
  710. }
  711. sz = ARRAY_SIZE(radeon_underscan_enum_list);
  712. rdev->mode_info.underscan_property =
  713. drm_property_create(rdev->ddev,
  714. DRM_MODE_PROP_ENUM,
  715. "underscan", sz);
  716. for (i = 0; i < sz; i++) {
  717. drm_property_add_enum(rdev->mode_info.underscan_property,
  718. i,
  719. radeon_underscan_enum_list[i].type,
  720. radeon_underscan_enum_list[i].name);
  721. }
  722. rdev->mode_info.underscan_hborder_property =
  723. drm_property_create(rdev->ddev,
  724. DRM_MODE_PROP_RANGE,
  725. "underscan hborder", 2);
  726. if (!rdev->mode_info.underscan_hborder_property)
  727. return -ENOMEM;
  728. rdev->mode_info.underscan_hborder_property->values[0] = 0;
  729. rdev->mode_info.underscan_hborder_property->values[1] = 128;
  730. rdev->mode_info.underscan_vborder_property =
  731. drm_property_create(rdev->ddev,
  732. DRM_MODE_PROP_RANGE,
  733. "underscan vborder", 2);
  734. if (!rdev->mode_info.underscan_vborder_property)
  735. return -ENOMEM;
  736. rdev->mode_info.underscan_vborder_property->values[0] = 0;
  737. rdev->mode_info.underscan_vborder_property->values[1] = 128;
  738. return 0;
  739. }
  740. void radeon_update_display_priority(struct radeon_device *rdev)
  741. {
  742. /* adjustment options for the display watermarks */
  743. if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
  744. /* set display priority to high for r3xx, rv515 chips
  745. * this avoids flickering due to underflow to the
  746. * display controllers during heavy acceleration.
  747. * Don't force high on rs4xx igp chips as it seems to
  748. * affect the sound card. See kernel bug 15982.
  749. */
  750. if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
  751. !(rdev->flags & RADEON_IS_IGP))
  752. rdev->disp_priority = 2;
  753. else
  754. rdev->disp_priority = 0;
  755. } else
  756. rdev->disp_priority = radeon_disp_priority;
  757. }
  758. int radeon_modeset_init(struct radeon_device *rdev)
  759. {
  760. int i;
  761. int ret;
  762. drm_mode_config_init(rdev->ddev);
  763. rdev->mode_info.mode_config_initialized = true;
  764. rdev->ddev->mode_config.funcs = (void *)&radeon_mode_funcs;
  765. if (ASIC_IS_AVIVO(rdev)) {
  766. rdev->ddev->mode_config.max_width = 8192;
  767. rdev->ddev->mode_config.max_height = 8192;
  768. } else {
  769. rdev->ddev->mode_config.max_width = 4096;
  770. rdev->ddev->mode_config.max_height = 4096;
  771. }
  772. rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
  773. ret = radeon_modeset_create_props(rdev);
  774. if (ret) {
  775. return ret;
  776. }
  777. /* init i2c buses */
  778. radeon_i2c_init(rdev);
  779. /* check combios for a valid hardcoded EDID - Sun servers */
  780. if (!rdev->is_atom_bios) {
  781. /* check for hardcoded EDID in BIOS */
  782. radeon_combios_check_hardcoded_edid(rdev);
  783. }
  784. /* allocate crtcs */
  785. for (i = 0; i < rdev->num_crtc; i++) {
  786. radeon_crtc_init(rdev->ddev, i);
  787. }
  788. /* okay we should have all the bios connectors */
  789. ret = radeon_setup_enc_conn(rdev->ddev);
  790. if (!ret) {
  791. return ret;
  792. }
  793. /* initialize hpd */
  794. radeon_hpd_init(rdev);
  795. /* Initialize power management */
  796. radeon_pm_init(rdev);
  797. radeon_fbdev_init(rdev);
  798. drm_kms_helper_poll_init(rdev->ddev);
  799. return 0;
  800. }
  801. void radeon_modeset_fini(struct radeon_device *rdev)
  802. {
  803. radeon_fbdev_fini(rdev);
  804. kfree(rdev->mode_info.bios_hardcoded_edid);
  805. radeon_pm_fini(rdev);
  806. if (rdev->mode_info.mode_config_initialized) {
  807. drm_kms_helper_poll_fini(rdev->ddev);
  808. radeon_hpd_fini(rdev);
  809. drm_mode_config_cleanup(rdev->ddev);
  810. rdev->mode_info.mode_config_initialized = false;
  811. }
  812. /* free i2c buses */
  813. radeon_i2c_fini(rdev);
  814. }
  815. static bool is_hdtv_mode(struct drm_display_mode *mode)
  816. {
  817. /* try and guess if this is a tv or a monitor */
  818. if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
  819. (mode->vdisplay == 576) || /* 576p */
  820. (mode->vdisplay == 720) || /* 720p */
  821. (mode->vdisplay == 1080)) /* 1080p */
  822. return true;
  823. else
  824. return false;
  825. }
  826. bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
  827. struct drm_display_mode *mode,
  828. struct drm_display_mode *adjusted_mode)
  829. {
  830. struct drm_device *dev = crtc->dev;
  831. struct radeon_device *rdev = dev->dev_private;
  832. struct drm_encoder *encoder;
  833. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  834. struct radeon_encoder *radeon_encoder;
  835. struct drm_connector *connector;
  836. struct radeon_connector *radeon_connector;
  837. bool first = true;
  838. u32 src_v = 1, dst_v = 1;
  839. u32 src_h = 1, dst_h = 1;
  840. radeon_crtc->h_border = 0;
  841. radeon_crtc->v_border = 0;
  842. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  843. if (encoder->crtc != crtc)
  844. continue;
  845. radeon_encoder = to_radeon_encoder(encoder);
  846. connector = radeon_get_connector_for_encoder(encoder);
  847. radeon_connector = to_radeon_connector(connector);
  848. if (first) {
  849. /* set scaling */
  850. if (radeon_encoder->rmx_type == RMX_OFF)
  851. radeon_crtc->rmx_type = RMX_OFF;
  852. else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
  853. mode->vdisplay < radeon_encoder->native_mode.vdisplay)
  854. radeon_crtc->rmx_type = radeon_encoder->rmx_type;
  855. else
  856. radeon_crtc->rmx_type = RMX_OFF;
  857. /* copy native mode */
  858. memcpy(&radeon_crtc->native_mode,
  859. &radeon_encoder->native_mode,
  860. sizeof(struct drm_display_mode));
  861. src_v = crtc->mode.vdisplay;
  862. dst_v = radeon_crtc->native_mode.vdisplay;
  863. src_h = crtc->mode.hdisplay;
  864. dst_h = radeon_crtc->native_mode.hdisplay;
  865. /* fix up for overscan on hdmi */
  866. if (ASIC_IS_AVIVO(rdev) &&
  867. (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
  868. ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
  869. ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
  870. drm_detect_hdmi_monitor(radeon_connector->edid) &&
  871. is_hdtv_mode(mode)))) {
  872. if (radeon_encoder->underscan_hborder != 0)
  873. radeon_crtc->h_border = radeon_encoder->underscan_hborder;
  874. else
  875. radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
  876. if (radeon_encoder->underscan_vborder != 0)
  877. radeon_crtc->v_border = radeon_encoder->underscan_vborder;
  878. else
  879. radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
  880. radeon_crtc->rmx_type = RMX_FULL;
  881. src_v = crtc->mode.vdisplay;
  882. dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
  883. src_h = crtc->mode.hdisplay;
  884. dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
  885. }
  886. first = false;
  887. } else {
  888. if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
  889. /* WARNING: Right now this can't happen but
  890. * in the future we need to check that scaling
  891. * are consistent across different encoder
  892. * (ie all encoder can work with the same
  893. * scaling).
  894. */
  895. DRM_ERROR("Scaling not consistent across encoder.\n");
  896. return false;
  897. }
  898. }
  899. }
  900. if (radeon_crtc->rmx_type != RMX_OFF) {
  901. fixed20_12 a, b;
  902. a.full = dfixed_const(src_v);
  903. b.full = dfixed_const(dst_v);
  904. radeon_crtc->vsc.full = dfixed_div(a, b);
  905. a.full = dfixed_const(src_h);
  906. b.full = dfixed_const(dst_h);
  907. radeon_crtc->hsc.full = dfixed_div(a, b);
  908. } else {
  909. radeon_crtc->vsc.full = dfixed_const(1);
  910. radeon_crtc->hsc.full = dfixed_const(1);
  911. }
  912. return true;
  913. }
  914. /*
  915. * Retrieve current video scanout position of crtc on a given gpu.
  916. *
  917. * \param rdev Device to query.
  918. * \param crtc Crtc to query.
  919. * \param *vpos Location where vertical scanout position should be stored.
  920. * \param *hpos Location where horizontal scanout position should go.
  921. *
  922. * Returns vpos as a positive number while in active scanout area.
  923. * Returns vpos as a negative number inside vblank, counting the number
  924. * of scanlines to go until end of vblank, e.g., -1 means "one scanline
  925. * until start of active scanout / end of vblank."
  926. *
  927. * \return Flags, or'ed together as follows:
  928. *
  929. * RADEON_SCANOUTPOS_VALID = Query successfull.
  930. * RADEON_SCANOUTPOS_INVBL = Inside vblank.
  931. * RADEON_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
  932. * this flag means that returned position may be offset by a constant but
  933. * unknown small number of scanlines wrt. real scanout position.
  934. *
  935. */
  936. int radeon_get_crtc_scanoutpos(struct radeon_device *rdev, int crtc, int *vpos, int *hpos)
  937. {
  938. u32 stat_crtc = 0, vbl = 0, position = 0;
  939. int vbl_start, vbl_end, vtotal, ret = 0;
  940. bool in_vbl = true;
  941. if (ASIC_IS_DCE4(rdev)) {
  942. if (crtc == 0) {
  943. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  944. EVERGREEN_CRTC0_REGISTER_OFFSET);
  945. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  946. EVERGREEN_CRTC0_REGISTER_OFFSET);
  947. ret |= RADEON_SCANOUTPOS_VALID;
  948. }
  949. if (crtc == 1) {
  950. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  951. EVERGREEN_CRTC1_REGISTER_OFFSET);
  952. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  953. EVERGREEN_CRTC1_REGISTER_OFFSET);
  954. ret |= RADEON_SCANOUTPOS_VALID;
  955. }
  956. if (crtc == 2) {
  957. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  958. EVERGREEN_CRTC2_REGISTER_OFFSET);
  959. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  960. EVERGREEN_CRTC2_REGISTER_OFFSET);
  961. ret |= RADEON_SCANOUTPOS_VALID;
  962. }
  963. if (crtc == 3) {
  964. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  965. EVERGREEN_CRTC3_REGISTER_OFFSET);
  966. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  967. EVERGREEN_CRTC3_REGISTER_OFFSET);
  968. ret |= RADEON_SCANOUTPOS_VALID;
  969. }
  970. if (crtc == 4) {
  971. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  972. EVERGREEN_CRTC4_REGISTER_OFFSET);
  973. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  974. EVERGREEN_CRTC4_REGISTER_OFFSET);
  975. ret |= RADEON_SCANOUTPOS_VALID;
  976. }
  977. if (crtc == 5) {
  978. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  979. EVERGREEN_CRTC5_REGISTER_OFFSET);
  980. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  981. EVERGREEN_CRTC5_REGISTER_OFFSET);
  982. ret |= RADEON_SCANOUTPOS_VALID;
  983. }
  984. } else if (ASIC_IS_AVIVO(rdev)) {
  985. if (crtc == 0) {
  986. vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END);
  987. position = RREG32(AVIVO_D1CRTC_STATUS_POSITION);
  988. ret |= RADEON_SCANOUTPOS_VALID;
  989. }
  990. if (crtc == 1) {
  991. vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END);
  992. position = RREG32(AVIVO_D2CRTC_STATUS_POSITION);
  993. ret |= RADEON_SCANOUTPOS_VALID;
  994. }
  995. } else {
  996. /* Pre-AVIVO: Different encoding of scanout pos and vblank interval. */
  997. if (crtc == 0) {
  998. /* Assume vbl_end == 0, get vbl_start from
  999. * upper 16 bits.
  1000. */
  1001. vbl = (RREG32(RADEON_CRTC_V_TOTAL_DISP) &
  1002. RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
  1003. /* Only retrieve vpos from upper 16 bits, set hpos == 0. */
  1004. position = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
  1005. stat_crtc = RREG32(RADEON_CRTC_STATUS);
  1006. if (!(stat_crtc & 1))
  1007. in_vbl = false;
  1008. ret |= RADEON_SCANOUTPOS_VALID;
  1009. }
  1010. if (crtc == 1) {
  1011. vbl = (RREG32(RADEON_CRTC2_V_TOTAL_DISP) &
  1012. RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
  1013. position = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
  1014. stat_crtc = RREG32(RADEON_CRTC2_STATUS);
  1015. if (!(stat_crtc & 1))
  1016. in_vbl = false;
  1017. ret |= RADEON_SCANOUTPOS_VALID;
  1018. }
  1019. }
  1020. /* Decode into vertical and horizontal scanout position. */
  1021. *vpos = position & 0x1fff;
  1022. *hpos = (position >> 16) & 0x1fff;
  1023. /* Valid vblank area boundaries from gpu retrieved? */
  1024. if (vbl > 0) {
  1025. /* Yes: Decode. */
  1026. ret |= RADEON_SCANOUTPOS_ACCURATE;
  1027. vbl_start = vbl & 0x1fff;
  1028. vbl_end = (vbl >> 16) & 0x1fff;
  1029. }
  1030. else {
  1031. /* No: Fake something reasonable which gives at least ok results. */
  1032. vbl_start = rdev->mode_info.crtcs[crtc]->base.mode.crtc_vdisplay;
  1033. vbl_end = 0;
  1034. }
  1035. /* Test scanout position against vblank region. */
  1036. if ((*vpos < vbl_start) && (*vpos >= vbl_end))
  1037. in_vbl = false;
  1038. /* Check if inside vblank area and apply corrective offsets:
  1039. * vpos will then be >=0 in video scanout area, but negative
  1040. * within vblank area, counting down the number of lines until
  1041. * start of scanout.
  1042. */
  1043. /* Inside "upper part" of vblank area? Apply corrective offset if so: */
  1044. if (in_vbl && (*vpos >= vbl_start)) {
  1045. vtotal = rdev->mode_info.crtcs[crtc]->base.mode.crtc_vtotal;
  1046. *vpos = *vpos - vtotal;
  1047. }
  1048. /* Correct for shifted end of vbl at vbl_end. */
  1049. *vpos = *vpos - vbl_end;
  1050. /* In vblank? */
  1051. if (in_vbl)
  1052. ret |= RADEON_SCANOUTPOS_INVBL;
  1053. return ret;
  1054. }