radeon_display.c 36 KB

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