radeon_display.c 34 KB

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