radeon_display.c 36 KB

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