radeon_display.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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. drm_crtc_cleanup(crtc);
  138. kfree(radeon_crtc);
  139. }
  140. static const struct drm_crtc_funcs radeon_crtc_funcs = {
  141. .cursor_set = radeon_crtc_cursor_set,
  142. .cursor_move = radeon_crtc_cursor_move,
  143. .gamma_set = radeon_crtc_gamma_set,
  144. .set_config = drm_crtc_helper_set_config,
  145. .destroy = radeon_crtc_destroy,
  146. };
  147. static void radeon_crtc_init(struct drm_device *dev, int index)
  148. {
  149. struct radeon_device *rdev = dev->dev_private;
  150. struct radeon_crtc *radeon_crtc;
  151. int i;
  152. radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  153. if (radeon_crtc == NULL)
  154. return;
  155. drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
  156. drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
  157. radeon_crtc->crtc_id = index;
  158. rdev->mode_info.crtcs[index] = radeon_crtc;
  159. #if 0
  160. radeon_crtc->mode_set.crtc = &radeon_crtc->base;
  161. radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
  162. radeon_crtc->mode_set.num_connectors = 0;
  163. #endif
  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. static 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. if (!radeon_connector->edid) {
  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. } else
  313. edid = radeon_connector->edid;
  314. if (edid) {
  315. /* update digital bits here */
  316. if (edid->input & DRM_EDID_INPUT_DIGITAL)
  317. radeon_connector->use_digital = 1;
  318. else
  319. radeon_connector->use_digital = 0;
  320. drm_mode_connector_update_edid_property(&radeon_connector->base, edid);
  321. ret = drm_add_edid_modes(&radeon_connector->base, edid);
  322. kfree(edid);
  323. return ret;
  324. }
  325. drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
  326. return 0;
  327. }
  328. static int radeon_ddc_dump(struct drm_connector *connector)
  329. {
  330. struct edid *edid;
  331. struct radeon_connector *radeon_connector = to_radeon_connector(connector);
  332. int ret = 0;
  333. if (!radeon_connector->ddc_bus)
  334. return -1;
  335. radeon_i2c_do_lock(radeon_connector, 1);
  336. edid = drm_get_edid(connector, &radeon_connector->ddc_bus->adapter);
  337. radeon_i2c_do_lock(radeon_connector, 0);
  338. if (edid) {
  339. kfree(edid);
  340. }
  341. return ret;
  342. }
  343. static inline uint32_t radeon_div(uint64_t n, uint32_t d)
  344. {
  345. uint64_t mod;
  346. n += d / 2;
  347. mod = do_div(n, d);
  348. return n;
  349. }
  350. void radeon_compute_pll(struct radeon_pll *pll,
  351. uint64_t freq,
  352. uint32_t *dot_clock_p,
  353. uint32_t *fb_div_p,
  354. uint32_t *frac_fb_div_p,
  355. uint32_t *ref_div_p,
  356. uint32_t *post_div_p,
  357. int flags)
  358. {
  359. uint32_t min_ref_div = pll->min_ref_div;
  360. uint32_t max_ref_div = pll->max_ref_div;
  361. uint32_t min_fractional_feed_div = 0;
  362. uint32_t max_fractional_feed_div = 0;
  363. uint32_t best_vco = pll->best_vco;
  364. uint32_t best_post_div = 1;
  365. uint32_t best_ref_div = 1;
  366. uint32_t best_feedback_div = 1;
  367. uint32_t best_frac_feedback_div = 0;
  368. uint32_t best_freq = -1;
  369. uint32_t best_error = 0xffffffff;
  370. uint32_t best_vco_diff = 1;
  371. uint32_t post_div;
  372. DRM_DEBUG("PLL freq %llu %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
  373. freq = freq * 1000;
  374. if (flags & RADEON_PLL_USE_REF_DIV)
  375. min_ref_div = max_ref_div = pll->reference_div;
  376. else {
  377. while (min_ref_div < max_ref_div-1) {
  378. uint32_t mid = (min_ref_div + max_ref_div) / 2;
  379. uint32_t pll_in = pll->reference_freq / mid;
  380. if (pll_in < pll->pll_in_min)
  381. max_ref_div = mid;
  382. else if (pll_in > pll->pll_in_max)
  383. min_ref_div = mid;
  384. else
  385. break;
  386. }
  387. }
  388. if (flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  389. min_fractional_feed_div = pll->min_frac_feedback_div;
  390. max_fractional_feed_div = pll->max_frac_feedback_div;
  391. }
  392. for (post_div = pll->min_post_div; post_div <= pll->max_post_div; ++post_div) {
  393. uint32_t ref_div;
  394. if ((flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
  395. continue;
  396. /* legacy radeons only have a few post_divs */
  397. if (flags & RADEON_PLL_LEGACY) {
  398. if ((post_div == 5) ||
  399. (post_div == 7) ||
  400. (post_div == 9) ||
  401. (post_div == 10) ||
  402. (post_div == 11) ||
  403. (post_div == 13) ||
  404. (post_div == 14) ||
  405. (post_div == 15))
  406. continue;
  407. }
  408. for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
  409. uint32_t feedback_div, current_freq = 0, error, vco_diff;
  410. uint32_t pll_in = pll->reference_freq / ref_div;
  411. uint32_t min_feed_div = pll->min_feedback_div;
  412. uint32_t max_feed_div = pll->max_feedback_div + 1;
  413. if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
  414. continue;
  415. while (min_feed_div < max_feed_div) {
  416. uint32_t vco;
  417. uint32_t min_frac_feed_div = min_fractional_feed_div;
  418. uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
  419. uint32_t frac_feedback_div;
  420. uint64_t tmp;
  421. feedback_div = (min_feed_div + max_feed_div) / 2;
  422. tmp = (uint64_t)pll->reference_freq * feedback_div;
  423. vco = radeon_div(tmp, ref_div);
  424. if (vco < pll->pll_out_min) {
  425. min_feed_div = feedback_div + 1;
  426. continue;
  427. } else if (vco > pll->pll_out_max) {
  428. max_feed_div = feedback_div;
  429. continue;
  430. }
  431. while (min_frac_feed_div < max_frac_feed_div) {
  432. frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
  433. tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
  434. tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
  435. current_freq = radeon_div(tmp, ref_div * post_div);
  436. if (flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
  437. error = freq - current_freq;
  438. error = error < 0 ? 0xffffffff : error;
  439. } else
  440. error = abs(current_freq - freq);
  441. vco_diff = abs(vco - best_vco);
  442. if ((best_vco == 0 && error < best_error) ||
  443. (best_vco != 0 &&
  444. (error < best_error - 100 ||
  445. (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
  446. best_post_div = post_div;
  447. best_ref_div = ref_div;
  448. best_feedback_div = feedback_div;
  449. best_frac_feedback_div = frac_feedback_div;
  450. best_freq = current_freq;
  451. best_error = error;
  452. best_vco_diff = vco_diff;
  453. } else if (current_freq == freq) {
  454. if (best_freq == -1) {
  455. best_post_div = post_div;
  456. best_ref_div = ref_div;
  457. best_feedback_div = feedback_div;
  458. best_frac_feedback_div = frac_feedback_div;
  459. best_freq = current_freq;
  460. best_error = error;
  461. best_vco_diff = vco_diff;
  462. } else if (((flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
  463. ((flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
  464. ((flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
  465. ((flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
  466. ((flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
  467. ((flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
  468. best_post_div = post_div;
  469. best_ref_div = ref_div;
  470. best_feedback_div = feedback_div;
  471. best_frac_feedback_div = frac_feedback_div;
  472. best_freq = current_freq;
  473. best_error = error;
  474. best_vco_diff = vco_diff;
  475. }
  476. }
  477. if (current_freq < freq)
  478. min_frac_feed_div = frac_feedback_div + 1;
  479. else
  480. max_frac_feed_div = frac_feedback_div;
  481. }
  482. if (current_freq < freq)
  483. min_feed_div = feedback_div + 1;
  484. else
  485. max_feed_div = feedback_div;
  486. }
  487. }
  488. }
  489. *dot_clock_p = best_freq / 10000;
  490. *fb_div_p = best_feedback_div;
  491. *frac_fb_div_p = best_frac_feedback_div;
  492. *ref_div_p = best_ref_div;
  493. *post_div_p = best_post_div;
  494. }
  495. static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
  496. {
  497. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  498. struct drm_device *dev = fb->dev;
  499. if (fb->fbdev)
  500. radeonfb_remove(dev, fb);
  501. if (radeon_fb->obj) {
  502. radeon_gem_object_unpin(radeon_fb->obj);
  503. mutex_lock(&dev->struct_mutex);
  504. drm_gem_object_unreference(radeon_fb->obj);
  505. mutex_unlock(&dev->struct_mutex);
  506. }
  507. drm_framebuffer_cleanup(fb);
  508. kfree(radeon_fb);
  509. }
  510. static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  511. struct drm_file *file_priv,
  512. unsigned int *handle)
  513. {
  514. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  515. return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
  516. }
  517. static const struct drm_framebuffer_funcs radeon_fb_funcs = {
  518. .destroy = radeon_user_framebuffer_destroy,
  519. .create_handle = radeon_user_framebuffer_create_handle,
  520. };
  521. struct drm_framebuffer *
  522. radeon_framebuffer_create(struct drm_device *dev,
  523. struct drm_mode_fb_cmd *mode_cmd,
  524. struct drm_gem_object *obj)
  525. {
  526. struct radeon_framebuffer *radeon_fb;
  527. radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
  528. if (radeon_fb == NULL) {
  529. return NULL;
  530. }
  531. drm_framebuffer_init(dev, &radeon_fb->base, &radeon_fb_funcs);
  532. drm_helper_mode_fill_fb_struct(&radeon_fb->base, mode_cmd);
  533. radeon_fb->obj = obj;
  534. return &radeon_fb->base;
  535. }
  536. static struct drm_framebuffer *
  537. radeon_user_framebuffer_create(struct drm_device *dev,
  538. struct drm_file *file_priv,
  539. struct drm_mode_fb_cmd *mode_cmd)
  540. {
  541. struct drm_gem_object *obj;
  542. obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
  543. return radeon_framebuffer_create(dev, mode_cmd, obj);
  544. }
  545. static const struct drm_mode_config_funcs radeon_mode_funcs = {
  546. .fb_create = radeon_user_framebuffer_create,
  547. .fb_changed = radeonfb_probe,
  548. };
  549. struct drm_prop_enum_list {
  550. int type;
  551. char *name;
  552. };
  553. static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
  554. { { 0, "driver" },
  555. { 1, "bios" },
  556. };
  557. static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
  558. { { TV_STD_NTSC, "ntsc" },
  559. { TV_STD_PAL, "pal" },
  560. { TV_STD_PAL_M, "pal-m" },
  561. { TV_STD_PAL_60, "pal-60" },
  562. { TV_STD_NTSC_J, "ntsc-j" },
  563. { TV_STD_SCART_PAL, "scart-pal" },
  564. { TV_STD_PAL_CN, "pal-cn" },
  565. { TV_STD_SECAM, "secam" },
  566. };
  567. int radeon_modeset_create_props(struct radeon_device *rdev)
  568. {
  569. int i, sz;
  570. if (rdev->is_atom_bios) {
  571. rdev->mode_info.coherent_mode_property =
  572. drm_property_create(rdev->ddev,
  573. DRM_MODE_PROP_RANGE,
  574. "coherent", 2);
  575. if (!rdev->mode_info.coherent_mode_property)
  576. return -ENOMEM;
  577. rdev->mode_info.coherent_mode_property->values[0] = 0;
  578. rdev->mode_info.coherent_mode_property->values[0] = 1;
  579. }
  580. if (!ASIC_IS_AVIVO(rdev)) {
  581. sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
  582. rdev->mode_info.tmds_pll_property =
  583. drm_property_create(rdev->ddev,
  584. DRM_MODE_PROP_ENUM,
  585. "tmds_pll", sz);
  586. for (i = 0; i < sz; i++) {
  587. drm_property_add_enum(rdev->mode_info.tmds_pll_property,
  588. i,
  589. radeon_tmds_pll_enum_list[i].type,
  590. radeon_tmds_pll_enum_list[i].name);
  591. }
  592. }
  593. rdev->mode_info.load_detect_property =
  594. drm_property_create(rdev->ddev,
  595. DRM_MODE_PROP_RANGE,
  596. "load detection", 2);
  597. if (!rdev->mode_info.load_detect_property)
  598. return -ENOMEM;
  599. rdev->mode_info.load_detect_property->values[0] = 0;
  600. rdev->mode_info.load_detect_property->values[0] = 1;
  601. drm_mode_create_scaling_mode_property(rdev->ddev);
  602. sz = ARRAY_SIZE(radeon_tv_std_enum_list);
  603. rdev->mode_info.tv_std_property =
  604. drm_property_create(rdev->ddev,
  605. DRM_MODE_PROP_ENUM,
  606. "tv standard", sz);
  607. for (i = 0; i < sz; i++) {
  608. drm_property_add_enum(rdev->mode_info.tv_std_property,
  609. i,
  610. radeon_tv_std_enum_list[i].type,
  611. radeon_tv_std_enum_list[i].name);
  612. }
  613. return 0;
  614. }
  615. int radeon_modeset_init(struct radeon_device *rdev)
  616. {
  617. int num_crtc = 2, i;
  618. int ret;
  619. drm_mode_config_init(rdev->ddev);
  620. rdev->mode_info.mode_config_initialized = true;
  621. rdev->ddev->mode_config.funcs = (void *)&radeon_mode_funcs;
  622. if (ASIC_IS_AVIVO(rdev)) {
  623. rdev->ddev->mode_config.max_width = 8192;
  624. rdev->ddev->mode_config.max_height = 8192;
  625. } else {
  626. rdev->ddev->mode_config.max_width = 4096;
  627. rdev->ddev->mode_config.max_height = 4096;
  628. }
  629. rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
  630. ret = radeon_modeset_create_props(rdev);
  631. if (ret) {
  632. return ret;
  633. }
  634. /* allocate crtcs - TODO single crtc */
  635. for (i = 0; i < num_crtc; i++) {
  636. radeon_crtc_init(rdev->ddev, i);
  637. }
  638. /* okay we should have all the bios connectors */
  639. ret = radeon_setup_enc_conn(rdev->ddev);
  640. if (!ret) {
  641. return ret;
  642. }
  643. drm_helper_initial_config(rdev->ddev);
  644. return 0;
  645. }
  646. void radeon_modeset_fini(struct radeon_device *rdev)
  647. {
  648. if (rdev->mode_info.mode_config_initialized) {
  649. drm_mode_config_cleanup(rdev->ddev);
  650. rdev->mode_info.mode_config_initialized = false;
  651. }
  652. }
  653. bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
  654. struct drm_display_mode *mode,
  655. struct drm_display_mode *adjusted_mode)
  656. {
  657. struct drm_device *dev = crtc->dev;
  658. struct drm_encoder *encoder;
  659. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  660. struct radeon_encoder *radeon_encoder;
  661. bool first = true;
  662. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  663. radeon_encoder = to_radeon_encoder(encoder);
  664. if (encoder->crtc != crtc)
  665. continue;
  666. if (first) {
  667. radeon_crtc->rmx_type = radeon_encoder->rmx_type;
  668. memcpy(&radeon_crtc->native_mode,
  669. &radeon_encoder->native_mode,
  670. sizeof(struct radeon_native_mode));
  671. first = false;
  672. } else {
  673. if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
  674. /* WARNING: Right now this can't happen but
  675. * in the future we need to check that scaling
  676. * are consistent accross different encoder
  677. * (ie all encoder can work with the same
  678. * scaling).
  679. */
  680. DRM_ERROR("Scaling not consistent accross encoder.\n");
  681. return false;
  682. }
  683. }
  684. }
  685. if (radeon_crtc->rmx_type != RMX_OFF) {
  686. fixed20_12 a, b;
  687. a.full = rfixed_const(crtc->mode.vdisplay);
  688. b.full = rfixed_const(radeon_crtc->native_mode.panel_xres);
  689. radeon_crtc->vsc.full = rfixed_div(a, b);
  690. a.full = rfixed_const(crtc->mode.hdisplay);
  691. b.full = rfixed_const(radeon_crtc->native_mode.panel_yres);
  692. radeon_crtc->hsc.full = rfixed_div(a, b);
  693. } else {
  694. radeon_crtc->vsc.full = rfixed_const(1);
  695. radeon_crtc->hsc.full = rfixed_const(1);
  696. }
  697. return true;
  698. }