radeon_display.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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 legacy_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. uint32_t dac2_cntl;
  67. dac2_cntl = RREG32(RADEON_DAC_CNTL2);
  68. if (radeon_crtc->crtc_id == 0)
  69. dac2_cntl &= (uint32_t)~RADEON_DAC2_PALETTE_ACC_CTL;
  70. else
  71. dac2_cntl |= RADEON_DAC2_PALETTE_ACC_CTL;
  72. WREG32(RADEON_DAC_CNTL2, dac2_cntl);
  73. WREG8(RADEON_PALETTE_INDEX, 0);
  74. for (i = 0; i < 256; i++) {
  75. WREG32(RADEON_PALETTE_30_DATA,
  76. (radeon_crtc->lut_r[i] << 20) |
  77. (radeon_crtc->lut_g[i] << 10) |
  78. (radeon_crtc->lut_b[i] << 0));
  79. }
  80. }
  81. void radeon_crtc_load_lut(struct drm_crtc *crtc)
  82. {
  83. struct drm_device *dev = crtc->dev;
  84. struct radeon_device *rdev = dev->dev_private;
  85. if (!crtc->enabled)
  86. return;
  87. if (ASIC_IS_AVIVO(rdev))
  88. avivo_crtc_load_lut(crtc);
  89. else
  90. legacy_crtc_load_lut(crtc);
  91. }
  92. /** Sets the color ramps on behalf of RandR */
  93. void radeon_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  94. u16 blue, int regno)
  95. {
  96. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  97. if (regno == 0)
  98. DRM_DEBUG("gamma set %d\n", radeon_crtc->crtc_id);
  99. radeon_crtc->lut_r[regno] = red >> 6;
  100. radeon_crtc->lut_g[regno] = green >> 6;
  101. radeon_crtc->lut_b[regno] = blue >> 6;
  102. }
  103. static void radeon_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  104. u16 *blue, uint32_t size)
  105. {
  106. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  107. int i, j;
  108. if (size != 256) {
  109. return;
  110. }
  111. if (crtc->fb == NULL) {
  112. return;
  113. }
  114. if (crtc->fb->depth == 16) {
  115. for (i = 0; i < 64; i++) {
  116. if (i <= 31) {
  117. for (j = 0; j < 8; j++) {
  118. radeon_crtc->lut_r[i * 8 + j] = red[i] >> 6;
  119. radeon_crtc->lut_b[i * 8 + j] = blue[i] >> 6;
  120. }
  121. }
  122. for (j = 0; j < 4; j++)
  123. radeon_crtc->lut_g[i * 4 + j] = green[i] >> 6;
  124. }
  125. } else {
  126. for (i = 0; i < 256; i++) {
  127. radeon_crtc->lut_r[i] = red[i] >> 6;
  128. radeon_crtc->lut_g[i] = green[i] >> 6;
  129. radeon_crtc->lut_b[i] = blue[i] >> 6;
  130. }
  131. }
  132. radeon_crtc_load_lut(crtc);
  133. }
  134. static void radeon_crtc_destroy(struct drm_crtc *crtc)
  135. {
  136. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  137. if (radeon_crtc->mode_set.mode) {
  138. drm_mode_destroy(crtc->dev, radeon_crtc->mode_set.mode);
  139. }
  140. drm_crtc_cleanup(crtc);
  141. kfree(radeon_crtc);
  142. }
  143. static const struct drm_crtc_funcs radeon_crtc_funcs = {
  144. .cursor_set = radeon_crtc_cursor_set,
  145. .cursor_move = radeon_crtc_cursor_move,
  146. .gamma_set = radeon_crtc_gamma_set,
  147. .set_config = drm_crtc_helper_set_config,
  148. .destroy = radeon_crtc_destroy,
  149. };
  150. static void radeon_crtc_init(struct drm_device *dev, int index)
  151. {
  152. struct radeon_device *rdev = dev->dev_private;
  153. struct radeon_crtc *radeon_crtc;
  154. int i;
  155. radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  156. if (radeon_crtc == NULL)
  157. return;
  158. drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
  159. drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
  160. radeon_crtc->crtc_id = index;
  161. rdev->mode_info.crtcs[index] = radeon_crtc;
  162. radeon_crtc->mode_set.crtc = &radeon_crtc->base;
  163. radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
  164. radeon_crtc->mode_set.num_connectors = 0;
  165. for (i = 0; i < 256; i++) {
  166. radeon_crtc->lut_r[i] = i << 2;
  167. radeon_crtc->lut_g[i] = i << 2;
  168. radeon_crtc->lut_b[i] = i << 2;
  169. }
  170. if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
  171. radeon_atombios_init_crtc(dev, radeon_crtc);
  172. else
  173. radeon_legacy_init_crtc(dev, radeon_crtc);
  174. }
  175. static const char *encoder_names[34] = {
  176. "NONE",
  177. "INTERNAL_LVDS",
  178. "INTERNAL_TMDS1",
  179. "INTERNAL_TMDS2",
  180. "INTERNAL_DAC1",
  181. "INTERNAL_DAC2",
  182. "INTERNAL_SDVOA",
  183. "INTERNAL_SDVOB",
  184. "SI170B",
  185. "CH7303",
  186. "CH7301",
  187. "INTERNAL_DVO1",
  188. "EXTERNAL_SDVOA",
  189. "EXTERNAL_SDVOB",
  190. "TITFP513",
  191. "INTERNAL_LVTM1",
  192. "VT1623",
  193. "HDMI_SI1930",
  194. "HDMI_INTERNAL",
  195. "INTERNAL_KLDSCP_TMDS1",
  196. "INTERNAL_KLDSCP_DVO1",
  197. "INTERNAL_KLDSCP_DAC1",
  198. "INTERNAL_KLDSCP_DAC2",
  199. "SI178",
  200. "MVPU_FPGA",
  201. "INTERNAL_DDI",
  202. "VT1625",
  203. "HDMI_SI1932",
  204. "DP_AN9801",
  205. "DP_DP501",
  206. "INTERNAL_UNIPHY",
  207. "INTERNAL_KLDSCP_LVTMA",
  208. "INTERNAL_UNIPHY1",
  209. "INTERNAL_UNIPHY2",
  210. };
  211. static const char *connector_names[13] = {
  212. "Unknown",
  213. "VGA",
  214. "DVI-I",
  215. "DVI-D",
  216. "DVI-A",
  217. "Composite",
  218. "S-video",
  219. "LVDS",
  220. "Component",
  221. "DIN",
  222. "DisplayPort",
  223. "HDMI-A",
  224. "HDMI-B",
  225. };
  226. static void radeon_print_display_setup(struct drm_device *dev)
  227. {
  228. struct drm_connector *connector;
  229. struct radeon_connector *radeon_connector;
  230. struct drm_encoder *encoder;
  231. struct radeon_encoder *radeon_encoder;
  232. uint32_t devices;
  233. int i = 0;
  234. DRM_INFO("Radeon Display Connectors\n");
  235. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  236. radeon_connector = to_radeon_connector(connector);
  237. DRM_INFO("Connector %d:\n", i);
  238. DRM_INFO(" %s\n", connector_names[connector->connector_type]);
  239. if (radeon_connector->ddc_bus)
  240. DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
  241. radeon_connector->ddc_bus->rec.mask_clk_reg,
  242. radeon_connector->ddc_bus->rec.mask_data_reg,
  243. radeon_connector->ddc_bus->rec.a_clk_reg,
  244. radeon_connector->ddc_bus->rec.a_data_reg,
  245. radeon_connector->ddc_bus->rec.put_clk_reg,
  246. radeon_connector->ddc_bus->rec.put_data_reg,
  247. radeon_connector->ddc_bus->rec.get_clk_reg,
  248. radeon_connector->ddc_bus->rec.get_data_reg);
  249. DRM_INFO(" Encoders:\n");
  250. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  251. radeon_encoder = to_radeon_encoder(encoder);
  252. devices = radeon_encoder->devices & radeon_connector->devices;
  253. if (devices) {
  254. if (devices & ATOM_DEVICE_CRT1_SUPPORT)
  255. DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  256. if (devices & ATOM_DEVICE_CRT2_SUPPORT)
  257. DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  258. if (devices & ATOM_DEVICE_LCD1_SUPPORT)
  259. DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  260. if (devices & ATOM_DEVICE_DFP1_SUPPORT)
  261. DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  262. if (devices & ATOM_DEVICE_DFP2_SUPPORT)
  263. DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  264. if (devices & ATOM_DEVICE_DFP3_SUPPORT)
  265. DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
  266. if (devices & ATOM_DEVICE_DFP4_SUPPORT)
  267. DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
  268. if (devices & ATOM_DEVICE_DFP5_SUPPORT)
  269. DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
  270. if (devices & ATOM_DEVICE_TV1_SUPPORT)
  271. DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  272. if (devices & ATOM_DEVICE_CV_SUPPORT)
  273. DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
  274. }
  275. }
  276. i++;
  277. }
  278. }
  279. bool radeon_setup_enc_conn(struct drm_device *dev)
  280. {
  281. struct radeon_device *rdev = dev->dev_private;
  282. struct drm_connector *drm_connector;
  283. bool ret = false;
  284. if (rdev->bios) {
  285. if (rdev->is_atom_bios) {
  286. if (rdev->family >= CHIP_R600)
  287. ret = radeon_get_atom_connector_info_from_object_table(dev);
  288. else
  289. ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
  290. } else
  291. ret = radeon_get_legacy_connector_info_from_bios(dev);
  292. } else {
  293. if (!ASIC_IS_AVIVO(rdev))
  294. ret = radeon_get_legacy_connector_info_from_table(dev);
  295. }
  296. if (ret) {
  297. radeon_print_display_setup(dev);
  298. list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head)
  299. radeon_ddc_dump(drm_connector);
  300. }
  301. return ret;
  302. }
  303. int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
  304. {
  305. struct edid *edid;
  306. int ret = 0;
  307. if (!radeon_connector->ddc_bus)
  308. return -1;
  309. radeon_i2c_do_lock(radeon_connector, 1);
  310. edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
  311. radeon_i2c_do_lock(radeon_connector, 0);
  312. if (edid) {
  313. /* update digital bits here */
  314. if (edid->input & DRM_EDID_INPUT_DIGITAL)
  315. radeon_connector->use_digital = 1;
  316. else
  317. radeon_connector->use_digital = 0;
  318. drm_mode_connector_update_edid_property(&radeon_connector->base, edid);
  319. ret = drm_add_edid_modes(&radeon_connector->base, edid);
  320. kfree(edid);
  321. return ret;
  322. }
  323. drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
  324. return -1;
  325. }
  326. static int radeon_ddc_dump(struct drm_connector *connector)
  327. {
  328. struct edid *edid;
  329. struct radeon_connector *radeon_connector = to_radeon_connector(connector);
  330. int ret = 0;
  331. if (!radeon_connector->ddc_bus)
  332. return -1;
  333. radeon_i2c_do_lock(radeon_connector, 1);
  334. edid = drm_get_edid(connector, &radeon_connector->ddc_bus->adapter);
  335. radeon_i2c_do_lock(radeon_connector, 0);
  336. if (edid) {
  337. kfree(edid);
  338. }
  339. return ret;
  340. }
  341. static inline uint32_t radeon_div(uint64_t n, uint32_t d)
  342. {
  343. uint64_t mod;
  344. n += d / 2;
  345. mod = do_div(n, d);
  346. return n;
  347. }
  348. void radeon_compute_pll(struct radeon_pll *pll,
  349. uint64_t freq,
  350. uint32_t *dot_clock_p,
  351. uint32_t *fb_div_p,
  352. uint32_t *frac_fb_div_p,
  353. uint32_t *ref_div_p,
  354. uint32_t *post_div_p,
  355. int flags)
  356. {
  357. uint32_t min_ref_div = pll->min_ref_div;
  358. uint32_t max_ref_div = pll->max_ref_div;
  359. uint32_t min_fractional_feed_div = 0;
  360. uint32_t max_fractional_feed_div = 0;
  361. uint32_t best_vco = pll->best_vco;
  362. uint32_t best_post_div = 1;
  363. uint32_t best_ref_div = 1;
  364. uint32_t best_feedback_div = 1;
  365. uint32_t best_frac_feedback_div = 0;
  366. uint32_t best_freq = -1;
  367. uint32_t best_error = 0xffffffff;
  368. uint32_t best_vco_diff = 1;
  369. uint32_t post_div;
  370. DRM_DEBUG("PLL freq %llu %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
  371. freq = freq * 1000;
  372. if (flags & RADEON_PLL_USE_REF_DIV)
  373. min_ref_div = max_ref_div = pll->reference_div;
  374. else {
  375. while (min_ref_div < max_ref_div-1) {
  376. uint32_t mid = (min_ref_div + max_ref_div) / 2;
  377. uint32_t pll_in = pll->reference_freq / mid;
  378. if (pll_in < pll->pll_in_min)
  379. max_ref_div = mid;
  380. else if (pll_in > pll->pll_in_max)
  381. min_ref_div = mid;
  382. else
  383. break;
  384. }
  385. }
  386. if (flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  387. min_fractional_feed_div = pll->min_frac_feedback_div;
  388. max_fractional_feed_div = pll->max_frac_feedback_div;
  389. }
  390. for (post_div = pll->min_post_div; post_div <= pll->max_post_div; ++post_div) {
  391. uint32_t ref_div;
  392. if ((flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
  393. continue;
  394. /* legacy radeons only have a few post_divs */
  395. if (flags & RADEON_PLL_LEGACY) {
  396. if ((post_div == 5) ||
  397. (post_div == 7) ||
  398. (post_div == 9) ||
  399. (post_div == 10) ||
  400. (post_div == 11) ||
  401. (post_div == 13) ||
  402. (post_div == 14) ||
  403. (post_div == 15))
  404. continue;
  405. }
  406. for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
  407. uint32_t feedback_div, current_freq = 0, error, vco_diff;
  408. uint32_t pll_in = pll->reference_freq / ref_div;
  409. uint32_t min_feed_div = pll->min_feedback_div;
  410. uint32_t max_feed_div = pll->max_feedback_div + 1;
  411. if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
  412. continue;
  413. while (min_feed_div < max_feed_div) {
  414. uint32_t vco;
  415. uint32_t min_frac_feed_div = min_fractional_feed_div;
  416. uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
  417. uint32_t frac_feedback_div;
  418. uint64_t tmp;
  419. feedback_div = (min_feed_div + max_feed_div) / 2;
  420. tmp = (uint64_t)pll->reference_freq * feedback_div;
  421. vco = radeon_div(tmp, ref_div);
  422. if (vco < pll->pll_out_min) {
  423. min_feed_div = feedback_div + 1;
  424. continue;
  425. } else if (vco > pll->pll_out_max) {
  426. max_feed_div = feedback_div;
  427. continue;
  428. }
  429. while (min_frac_feed_div < max_frac_feed_div) {
  430. frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
  431. tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
  432. tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
  433. current_freq = radeon_div(tmp, ref_div * post_div);
  434. if (flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
  435. error = freq - current_freq;
  436. error = error < 0 ? 0xffffffff : error;
  437. } else
  438. error = abs(current_freq - freq);
  439. vco_diff = abs(vco - best_vco);
  440. if ((best_vco == 0 && error < best_error) ||
  441. (best_vco != 0 &&
  442. (error < best_error - 100 ||
  443. (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
  444. best_post_div = post_div;
  445. best_ref_div = ref_div;
  446. best_feedback_div = feedback_div;
  447. best_frac_feedback_div = frac_feedback_div;
  448. best_freq = current_freq;
  449. best_error = error;
  450. best_vco_diff = vco_diff;
  451. } else if (current_freq == freq) {
  452. if (best_freq == -1) {
  453. best_post_div = post_div;
  454. best_ref_div = ref_div;
  455. best_feedback_div = feedback_div;
  456. best_frac_feedback_div = frac_feedback_div;
  457. best_freq = current_freq;
  458. best_error = error;
  459. best_vco_diff = vco_diff;
  460. } else if (((flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
  461. ((flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
  462. ((flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
  463. ((flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
  464. ((flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
  465. ((flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
  466. best_post_div = post_div;
  467. best_ref_div = ref_div;
  468. best_feedback_div = feedback_div;
  469. best_frac_feedback_div = frac_feedback_div;
  470. best_freq = current_freq;
  471. best_error = error;
  472. best_vco_diff = vco_diff;
  473. }
  474. }
  475. if (current_freq < freq)
  476. min_frac_feed_div = frac_feedback_div + 1;
  477. else
  478. max_frac_feed_div = frac_feedback_div;
  479. }
  480. if (current_freq < freq)
  481. min_feed_div = feedback_div + 1;
  482. else
  483. max_feed_div = feedback_div;
  484. }
  485. }
  486. }
  487. *dot_clock_p = best_freq / 10000;
  488. *fb_div_p = best_feedback_div;
  489. *frac_fb_div_p = best_frac_feedback_div;
  490. *ref_div_p = best_ref_div;
  491. *post_div_p = best_post_div;
  492. }
  493. static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
  494. {
  495. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  496. struct drm_device *dev = fb->dev;
  497. if (fb->fbdev)
  498. radeonfb_remove(dev, fb);
  499. if (radeon_fb->obj) {
  500. radeon_gem_object_unpin(radeon_fb->obj);
  501. mutex_lock(&dev->struct_mutex);
  502. drm_gem_object_unreference(radeon_fb->obj);
  503. mutex_unlock(&dev->struct_mutex);
  504. }
  505. drm_framebuffer_cleanup(fb);
  506. kfree(radeon_fb);
  507. }
  508. static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  509. struct drm_file *file_priv,
  510. unsigned int *handle)
  511. {
  512. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  513. return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
  514. }
  515. static const struct drm_framebuffer_funcs radeon_fb_funcs = {
  516. .destroy = radeon_user_framebuffer_destroy,
  517. .create_handle = radeon_user_framebuffer_create_handle,
  518. };
  519. struct drm_framebuffer *
  520. radeon_framebuffer_create(struct drm_device *dev,
  521. struct drm_mode_fb_cmd *mode_cmd,
  522. struct drm_gem_object *obj)
  523. {
  524. struct radeon_framebuffer *radeon_fb;
  525. radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
  526. if (radeon_fb == NULL) {
  527. return NULL;
  528. }
  529. drm_framebuffer_init(dev, &radeon_fb->base, &radeon_fb_funcs);
  530. drm_helper_mode_fill_fb_struct(&radeon_fb->base, mode_cmd);
  531. radeon_fb->obj = obj;
  532. return &radeon_fb->base;
  533. }
  534. static struct drm_framebuffer *
  535. radeon_user_framebuffer_create(struct drm_device *dev,
  536. struct drm_file *file_priv,
  537. struct drm_mode_fb_cmd *mode_cmd)
  538. {
  539. struct drm_gem_object *obj;
  540. obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
  541. return radeon_framebuffer_create(dev, mode_cmd, obj);
  542. }
  543. static const struct drm_mode_config_funcs radeon_mode_funcs = {
  544. .fb_create = radeon_user_framebuffer_create,
  545. .fb_changed = radeonfb_probe,
  546. };
  547. int radeon_modeset_init(struct radeon_device *rdev)
  548. {
  549. int num_crtc = 2, i;
  550. int ret;
  551. drm_mode_config_init(rdev->ddev);
  552. rdev->mode_info.mode_config_initialized = true;
  553. rdev->ddev->mode_config.funcs = (void *)&radeon_mode_funcs;
  554. if (ASIC_IS_AVIVO(rdev)) {
  555. rdev->ddev->mode_config.max_width = 8192;
  556. rdev->ddev->mode_config.max_height = 8192;
  557. } else {
  558. rdev->ddev->mode_config.max_width = 4096;
  559. rdev->ddev->mode_config.max_height = 4096;
  560. }
  561. rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
  562. /* allocate crtcs - TODO single crtc */
  563. for (i = 0; i < num_crtc; i++) {
  564. radeon_crtc_init(rdev->ddev, i);
  565. }
  566. /* okay we should have all the bios connectors */
  567. ret = radeon_setup_enc_conn(rdev->ddev);
  568. if (!ret) {
  569. return ret;
  570. }
  571. drm_helper_initial_config(rdev->ddev);
  572. return 0;
  573. }
  574. void radeon_modeset_fini(struct radeon_device *rdev)
  575. {
  576. if (rdev->mode_info.mode_config_initialized) {
  577. drm_mode_config_cleanup(rdev->ddev);
  578. rdev->mode_info.mode_config_initialized = false;
  579. }
  580. }
  581. bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
  582. struct drm_display_mode *mode,
  583. struct drm_display_mode *adjusted_mode)
  584. {
  585. struct drm_device *dev = crtc->dev;
  586. struct drm_encoder *encoder;
  587. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  588. struct radeon_encoder *radeon_encoder;
  589. bool first = true;
  590. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  591. radeon_encoder = to_radeon_encoder(encoder);
  592. if (encoder->crtc != crtc)
  593. continue;
  594. if (first) {
  595. radeon_crtc->rmx_type = radeon_encoder->rmx_type;
  596. radeon_crtc->devices = radeon_encoder->devices;
  597. memcpy(&radeon_crtc->native_mode,
  598. &radeon_encoder->native_mode,
  599. sizeof(struct radeon_native_mode));
  600. first = false;
  601. } else {
  602. if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
  603. /* WARNING: Right now this can't happen but
  604. * in the future we need to check that scaling
  605. * are consistent accross different encoder
  606. * (ie all encoder can work with the same
  607. * scaling).
  608. */
  609. DRM_ERROR("Scaling not consistent accross encoder.\n");
  610. return false;
  611. }
  612. }
  613. }
  614. if (radeon_crtc->rmx_type != RMX_OFF) {
  615. fixed20_12 a, b;
  616. a.full = rfixed_const(crtc->mode.vdisplay);
  617. b.full = rfixed_const(radeon_crtc->native_mode.panel_xres);
  618. radeon_crtc->vsc.full = rfixed_div(a, b);
  619. a.full = rfixed_const(crtc->mode.hdisplay);
  620. b.full = rfixed_const(radeon_crtc->native_mode.panel_yres);
  621. radeon_crtc->hsc.full = rfixed_div(a, b);
  622. } else {
  623. radeon_crtc->vsc.full = rfixed_const(1);
  624. radeon_crtc->hsc.full = rfixed_const(1);
  625. }
  626. return true;
  627. }