radeon_display.c 33 KB

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