radeon_display.c 23 KB

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