radeon_display.c 23 KB

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