radeon_display.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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. radeon_crtc->mode_set.crtc = &radeon_crtc->base;
  162. radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
  163. radeon_crtc->mode_set.num_connectors = 0;
  164. for (i = 0; i < 256; i++) {
  165. radeon_crtc->lut_r[i] = i << 2;
  166. radeon_crtc->lut_g[i] = i << 2;
  167. radeon_crtc->lut_b[i] = i << 2;
  168. }
  169. if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
  170. radeon_atombios_init_crtc(dev, radeon_crtc);
  171. else
  172. radeon_legacy_init_crtc(dev, radeon_crtc);
  173. }
  174. static const char *encoder_names[34] = {
  175. "NONE",
  176. "INTERNAL_LVDS",
  177. "INTERNAL_TMDS1",
  178. "INTERNAL_TMDS2",
  179. "INTERNAL_DAC1",
  180. "INTERNAL_DAC2",
  181. "INTERNAL_SDVOA",
  182. "INTERNAL_SDVOB",
  183. "SI170B",
  184. "CH7303",
  185. "CH7301",
  186. "INTERNAL_DVO1",
  187. "EXTERNAL_SDVOA",
  188. "EXTERNAL_SDVOB",
  189. "TITFP513",
  190. "INTERNAL_LVTM1",
  191. "VT1623",
  192. "HDMI_SI1930",
  193. "HDMI_INTERNAL",
  194. "INTERNAL_KLDSCP_TMDS1",
  195. "INTERNAL_KLDSCP_DVO1",
  196. "INTERNAL_KLDSCP_DAC1",
  197. "INTERNAL_KLDSCP_DAC2",
  198. "SI178",
  199. "MVPU_FPGA",
  200. "INTERNAL_DDI",
  201. "VT1625",
  202. "HDMI_SI1932",
  203. "DP_AN9801",
  204. "DP_DP501",
  205. "INTERNAL_UNIPHY",
  206. "INTERNAL_KLDSCP_LVTMA",
  207. "INTERNAL_UNIPHY1",
  208. "INTERNAL_UNIPHY2",
  209. };
  210. static const char *connector_names[13] = {
  211. "Unknown",
  212. "VGA",
  213. "DVI-I",
  214. "DVI-D",
  215. "DVI-A",
  216. "Composite",
  217. "S-video",
  218. "LVDS",
  219. "Component",
  220. "DIN",
  221. "DisplayPort",
  222. "HDMI-A",
  223. "HDMI-B",
  224. };
  225. static void radeon_print_display_setup(struct drm_device *dev)
  226. {
  227. struct drm_connector *connector;
  228. struct radeon_connector *radeon_connector;
  229. struct drm_encoder *encoder;
  230. struct radeon_encoder *radeon_encoder;
  231. uint32_t devices;
  232. int i = 0;
  233. DRM_INFO("Radeon Display Connectors\n");
  234. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  235. radeon_connector = to_radeon_connector(connector);
  236. DRM_INFO("Connector %d:\n", i);
  237. DRM_INFO(" %s\n", connector_names[connector->connector_type]);
  238. if (radeon_connector->ddc_bus)
  239. DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
  240. radeon_connector->ddc_bus->rec.mask_clk_reg,
  241. radeon_connector->ddc_bus->rec.mask_data_reg,
  242. radeon_connector->ddc_bus->rec.a_clk_reg,
  243. radeon_connector->ddc_bus->rec.a_data_reg,
  244. radeon_connector->ddc_bus->rec.put_clk_reg,
  245. radeon_connector->ddc_bus->rec.put_data_reg,
  246. radeon_connector->ddc_bus->rec.get_clk_reg,
  247. radeon_connector->ddc_bus->rec.get_data_reg);
  248. DRM_INFO(" Encoders:\n");
  249. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  250. radeon_encoder = to_radeon_encoder(encoder);
  251. devices = radeon_encoder->devices & radeon_connector->devices;
  252. if (devices) {
  253. if (devices & ATOM_DEVICE_CRT1_SUPPORT)
  254. DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  255. if (devices & ATOM_DEVICE_CRT2_SUPPORT)
  256. DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  257. if (devices & ATOM_DEVICE_LCD1_SUPPORT)
  258. DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  259. if (devices & ATOM_DEVICE_DFP1_SUPPORT)
  260. DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  261. if (devices & ATOM_DEVICE_DFP2_SUPPORT)
  262. DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  263. if (devices & ATOM_DEVICE_DFP3_SUPPORT)
  264. DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
  265. if (devices & ATOM_DEVICE_DFP4_SUPPORT)
  266. DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
  267. if (devices & ATOM_DEVICE_DFP5_SUPPORT)
  268. DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
  269. if (devices & ATOM_DEVICE_TV1_SUPPORT)
  270. DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  271. if (devices & ATOM_DEVICE_CV_SUPPORT)
  272. DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
  273. }
  274. }
  275. i++;
  276. }
  277. }
  278. bool radeon_setup_enc_conn(struct drm_device *dev)
  279. {
  280. struct radeon_device *rdev = dev->dev_private;
  281. struct drm_connector *drm_connector;
  282. bool ret = false;
  283. if (rdev->bios) {
  284. if (rdev->is_atom_bios) {
  285. if (rdev->family >= CHIP_R600)
  286. ret = radeon_get_atom_connector_info_from_object_table(dev);
  287. else
  288. ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
  289. } else
  290. ret = radeon_get_legacy_connector_info_from_bios(dev);
  291. } else {
  292. if (!ASIC_IS_AVIVO(rdev))
  293. ret = radeon_get_legacy_connector_info_from_table(dev);
  294. }
  295. if (ret) {
  296. radeon_print_display_setup(dev);
  297. list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head)
  298. radeon_ddc_dump(drm_connector);
  299. }
  300. return ret;
  301. }
  302. int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
  303. {
  304. struct edid *edid;
  305. int ret = 0;
  306. if (!radeon_connector->ddc_bus)
  307. return -1;
  308. radeon_i2c_do_lock(radeon_connector, 1);
  309. edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
  310. radeon_i2c_do_lock(radeon_connector, 0);
  311. if (edid) {
  312. /* update digital bits here */
  313. if (edid->input & DRM_EDID_INPUT_DIGITAL)
  314. radeon_connector->use_digital = 1;
  315. else
  316. radeon_connector->use_digital = 0;
  317. drm_mode_connector_update_edid_property(&radeon_connector->base, edid);
  318. ret = drm_add_edid_modes(&radeon_connector->base, edid);
  319. kfree(edid);
  320. return ret;
  321. }
  322. drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
  323. return -1;
  324. }
  325. static int radeon_ddc_dump(struct drm_connector *connector)
  326. {
  327. struct edid *edid;
  328. struct radeon_connector *radeon_connector = to_radeon_connector(connector);
  329. int ret = 0;
  330. if (!radeon_connector->ddc_bus)
  331. return -1;
  332. radeon_i2c_do_lock(radeon_connector, 1);
  333. edid = drm_get_edid(connector, &radeon_connector->ddc_bus->adapter);
  334. radeon_i2c_do_lock(radeon_connector, 0);
  335. if (edid) {
  336. kfree(edid);
  337. }
  338. return ret;
  339. }
  340. static inline uint32_t radeon_div(uint64_t n, uint32_t d)
  341. {
  342. uint64_t mod;
  343. n += d / 2;
  344. mod = do_div(n, d);
  345. return n;
  346. }
  347. void radeon_compute_pll(struct radeon_pll *pll,
  348. uint64_t freq,
  349. uint32_t *dot_clock_p,
  350. uint32_t *fb_div_p,
  351. uint32_t *frac_fb_div_p,
  352. uint32_t *ref_div_p,
  353. uint32_t *post_div_p,
  354. int flags)
  355. {
  356. uint32_t min_ref_div = pll->min_ref_div;
  357. uint32_t max_ref_div = pll->max_ref_div;
  358. uint32_t min_fractional_feed_div = 0;
  359. uint32_t max_fractional_feed_div = 0;
  360. uint32_t best_vco = pll->best_vco;
  361. uint32_t best_post_div = 1;
  362. uint32_t best_ref_div = 1;
  363. uint32_t best_feedback_div = 1;
  364. uint32_t best_frac_feedback_div = 0;
  365. uint32_t best_freq = -1;
  366. uint32_t best_error = 0xffffffff;
  367. uint32_t best_vco_diff = 1;
  368. uint32_t post_div;
  369. DRM_DEBUG("PLL freq %llu %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
  370. freq = freq * 1000;
  371. if (flags & RADEON_PLL_USE_REF_DIV)
  372. min_ref_div = max_ref_div = pll->reference_div;
  373. else {
  374. while (min_ref_div < max_ref_div-1) {
  375. uint32_t mid = (min_ref_div + max_ref_div) / 2;
  376. uint32_t pll_in = pll->reference_freq / mid;
  377. if (pll_in < pll->pll_in_min)
  378. max_ref_div = mid;
  379. else if (pll_in > pll->pll_in_max)
  380. min_ref_div = mid;
  381. else
  382. break;
  383. }
  384. }
  385. if (flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  386. min_fractional_feed_div = pll->min_frac_feedback_div;
  387. max_fractional_feed_div = pll->max_frac_feedback_div;
  388. }
  389. for (post_div = pll->min_post_div; post_div <= pll->max_post_div; ++post_div) {
  390. uint32_t ref_div;
  391. if ((flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
  392. continue;
  393. /* legacy radeons only have a few post_divs */
  394. if (flags & RADEON_PLL_LEGACY) {
  395. if ((post_div == 5) ||
  396. (post_div == 7) ||
  397. (post_div == 9) ||
  398. (post_div == 10) ||
  399. (post_div == 11) ||
  400. (post_div == 13) ||
  401. (post_div == 14) ||
  402. (post_div == 15))
  403. continue;
  404. }
  405. for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
  406. uint32_t feedback_div, current_freq = 0, error, vco_diff;
  407. uint32_t pll_in = pll->reference_freq / ref_div;
  408. uint32_t min_feed_div = pll->min_feedback_div;
  409. uint32_t max_feed_div = pll->max_feedback_div + 1;
  410. if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
  411. continue;
  412. while (min_feed_div < max_feed_div) {
  413. uint32_t vco;
  414. uint32_t min_frac_feed_div = min_fractional_feed_div;
  415. uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
  416. uint32_t frac_feedback_div;
  417. uint64_t tmp;
  418. feedback_div = (min_feed_div + max_feed_div) / 2;
  419. tmp = (uint64_t)pll->reference_freq * feedback_div;
  420. vco = radeon_div(tmp, ref_div);
  421. if (vco < pll->pll_out_min) {
  422. min_feed_div = feedback_div + 1;
  423. continue;
  424. } else if (vco > pll->pll_out_max) {
  425. max_feed_div = feedback_div;
  426. continue;
  427. }
  428. while (min_frac_feed_div < max_frac_feed_div) {
  429. frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
  430. tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
  431. tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
  432. current_freq = radeon_div(tmp, ref_div * post_div);
  433. error = abs(current_freq - freq);
  434. vco_diff = abs(vco - best_vco);
  435. if ((best_vco == 0 && error < best_error) ||
  436. (best_vco != 0 &&
  437. (error < best_error - 100 ||
  438. (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
  439. best_post_div = post_div;
  440. best_ref_div = ref_div;
  441. best_feedback_div = feedback_div;
  442. best_frac_feedback_div = frac_feedback_div;
  443. best_freq = current_freq;
  444. best_error = error;
  445. best_vco_diff = vco_diff;
  446. } else if (current_freq == freq) {
  447. if (best_freq == -1) {
  448. best_post_div = post_div;
  449. best_ref_div = ref_div;
  450. best_feedback_div = feedback_div;
  451. best_frac_feedback_div = frac_feedback_div;
  452. best_freq = current_freq;
  453. best_error = error;
  454. best_vco_diff = vco_diff;
  455. } else if (((flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
  456. ((flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
  457. ((flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
  458. ((flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
  459. ((flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
  460. ((flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
  461. best_post_div = post_div;
  462. best_ref_div = ref_div;
  463. best_feedback_div = feedback_div;
  464. best_frac_feedback_div = frac_feedback_div;
  465. best_freq = current_freq;
  466. best_error = error;
  467. best_vco_diff = vco_diff;
  468. }
  469. }
  470. if (current_freq < freq)
  471. min_frac_feed_div = frac_feedback_div + 1;
  472. else
  473. max_frac_feed_div = frac_feedback_div;
  474. }
  475. if (current_freq < freq)
  476. min_feed_div = feedback_div + 1;
  477. else
  478. max_feed_div = feedback_div;
  479. }
  480. }
  481. }
  482. *dot_clock_p = best_freq / 10000;
  483. *fb_div_p = best_feedback_div;
  484. *frac_fb_div_p = best_frac_feedback_div;
  485. *ref_div_p = best_ref_div;
  486. *post_div_p = best_post_div;
  487. }
  488. static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
  489. {
  490. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  491. struct drm_device *dev = fb->dev;
  492. if (fb->fbdev)
  493. radeonfb_remove(dev, fb);
  494. if (radeon_fb->obj) {
  495. radeon_gem_object_unpin(radeon_fb->obj);
  496. mutex_lock(&dev->struct_mutex);
  497. drm_gem_object_unreference(radeon_fb->obj);
  498. mutex_unlock(&dev->struct_mutex);
  499. }
  500. drm_framebuffer_cleanup(fb);
  501. kfree(radeon_fb);
  502. }
  503. static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  504. struct drm_file *file_priv,
  505. unsigned int *handle)
  506. {
  507. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  508. return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
  509. }
  510. static const struct drm_framebuffer_funcs radeon_fb_funcs = {
  511. .destroy = radeon_user_framebuffer_destroy,
  512. .create_handle = radeon_user_framebuffer_create_handle,
  513. };
  514. struct drm_framebuffer *
  515. radeon_framebuffer_create(struct drm_device *dev,
  516. struct drm_mode_fb_cmd *mode_cmd,
  517. struct drm_gem_object *obj)
  518. {
  519. struct radeon_framebuffer *radeon_fb;
  520. radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
  521. if (radeon_fb == NULL) {
  522. return NULL;
  523. }
  524. drm_framebuffer_init(dev, &radeon_fb->base, &radeon_fb_funcs);
  525. drm_helper_mode_fill_fb_struct(&radeon_fb->base, mode_cmd);
  526. radeon_fb->obj = obj;
  527. return &radeon_fb->base;
  528. }
  529. static struct drm_framebuffer *
  530. radeon_user_framebuffer_create(struct drm_device *dev,
  531. struct drm_file *file_priv,
  532. struct drm_mode_fb_cmd *mode_cmd)
  533. {
  534. struct drm_gem_object *obj;
  535. obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
  536. return radeon_framebuffer_create(dev, mode_cmd, obj);
  537. }
  538. static const struct drm_mode_config_funcs radeon_mode_funcs = {
  539. .fb_create = radeon_user_framebuffer_create,
  540. .fb_changed = radeonfb_probe,
  541. };
  542. int radeon_modeset_init(struct radeon_device *rdev)
  543. {
  544. int num_crtc = 2, i;
  545. int ret;
  546. drm_mode_config_init(rdev->ddev);
  547. rdev->mode_info.mode_config_initialized = true;
  548. rdev->ddev->mode_config.funcs = (void *)&radeon_mode_funcs;
  549. if (ASIC_IS_AVIVO(rdev)) {
  550. rdev->ddev->mode_config.max_width = 8192;
  551. rdev->ddev->mode_config.max_height = 8192;
  552. } else {
  553. rdev->ddev->mode_config.max_width = 4096;
  554. rdev->ddev->mode_config.max_height = 4096;
  555. }
  556. rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
  557. /* allocate crtcs - TODO single crtc */
  558. for (i = 0; i < num_crtc; i++) {
  559. radeon_crtc_init(rdev->ddev, i);
  560. }
  561. /* okay we should have all the bios connectors */
  562. ret = radeon_setup_enc_conn(rdev->ddev);
  563. if (!ret) {
  564. return ret;
  565. }
  566. drm_helper_initial_config(rdev->ddev);
  567. return 0;
  568. }
  569. void radeon_modeset_fini(struct radeon_device *rdev)
  570. {
  571. if (rdev->mode_info.mode_config_initialized) {
  572. drm_mode_config_cleanup(rdev->ddev);
  573. rdev->mode_info.mode_config_initialized = false;
  574. }
  575. }
  576. void radeon_init_disp_bandwidth(struct drm_device *dev)
  577. {
  578. struct radeon_device *rdev = dev->dev_private;
  579. struct drm_display_mode *modes[2];
  580. int pixel_bytes[2];
  581. struct drm_crtc *crtc;
  582. pixel_bytes[0] = pixel_bytes[1] = 0;
  583. modes[0] = modes[1] = NULL;
  584. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  585. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  586. if (crtc->enabled && crtc->fb) {
  587. modes[radeon_crtc->crtc_id] = &crtc->mode;
  588. pixel_bytes[radeon_crtc->crtc_id] = crtc->fb->bits_per_pixel / 8;
  589. }
  590. }
  591. if (ASIC_IS_AVIVO(rdev)) {
  592. radeon_init_disp_bw_avivo(dev,
  593. modes[0],
  594. pixel_bytes[0],
  595. modes[1],
  596. pixel_bytes[1]);
  597. } else {
  598. radeon_init_disp_bw_legacy(dev,
  599. modes[0],
  600. pixel_bytes[0],
  601. modes[1],
  602. pixel_bytes[1]);
  603. }
  604. }