radeon_display.c 23 KB

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