radeon_display.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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[15] = {
  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. "TV",
  217. "eDP",
  218. };
  219. static const char *hpd_names[7] = {
  220. "NONE",
  221. "HPD1",
  222. "HPD2",
  223. "HPD3",
  224. "HPD4",
  225. "HPD5",
  226. "HPD6",
  227. };
  228. static void radeon_print_display_setup(struct drm_device *dev)
  229. {
  230. struct drm_connector *connector;
  231. struct radeon_connector *radeon_connector;
  232. struct drm_encoder *encoder;
  233. struct radeon_encoder *radeon_encoder;
  234. uint32_t devices;
  235. int i = 0;
  236. DRM_INFO("Radeon Display Connectors\n");
  237. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  238. radeon_connector = to_radeon_connector(connector);
  239. DRM_INFO("Connector %d:\n", i);
  240. DRM_INFO(" %s\n", connector_names[connector->connector_type]);
  241. if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
  242. DRM_INFO(" %s\n", hpd_names[radeon_connector->hpd.hpd]);
  243. if (radeon_connector->ddc_bus) {
  244. DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
  245. radeon_connector->ddc_bus->rec.mask_clk_reg,
  246. radeon_connector->ddc_bus->rec.mask_data_reg,
  247. radeon_connector->ddc_bus->rec.a_clk_reg,
  248. radeon_connector->ddc_bus->rec.a_data_reg,
  249. radeon_connector->ddc_bus->rec.en_clk_reg,
  250. radeon_connector->ddc_bus->rec.en_data_reg,
  251. radeon_connector->ddc_bus->rec.y_clk_reg,
  252. radeon_connector->ddc_bus->rec.y_data_reg);
  253. } else {
  254. if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
  255. connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
  256. connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
  257. connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
  258. connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
  259. connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
  260. DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
  261. }
  262. DRM_INFO(" Encoders:\n");
  263. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  264. radeon_encoder = to_radeon_encoder(encoder);
  265. devices = radeon_encoder->devices & radeon_connector->devices;
  266. if (devices) {
  267. if (devices & ATOM_DEVICE_CRT1_SUPPORT)
  268. DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  269. if (devices & ATOM_DEVICE_CRT2_SUPPORT)
  270. DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  271. if (devices & ATOM_DEVICE_LCD1_SUPPORT)
  272. DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  273. if (devices & ATOM_DEVICE_DFP1_SUPPORT)
  274. DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  275. if (devices & ATOM_DEVICE_DFP2_SUPPORT)
  276. DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  277. if (devices & ATOM_DEVICE_DFP3_SUPPORT)
  278. DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
  279. if (devices & ATOM_DEVICE_DFP4_SUPPORT)
  280. DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
  281. if (devices & ATOM_DEVICE_DFP5_SUPPORT)
  282. DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
  283. if (devices & ATOM_DEVICE_TV1_SUPPORT)
  284. DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  285. if (devices & ATOM_DEVICE_CV_SUPPORT)
  286. DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
  287. }
  288. }
  289. i++;
  290. }
  291. }
  292. static bool radeon_setup_enc_conn(struct drm_device *dev)
  293. {
  294. struct radeon_device *rdev = dev->dev_private;
  295. struct drm_connector *drm_connector;
  296. bool ret = false;
  297. if (rdev->bios) {
  298. if (rdev->is_atom_bios) {
  299. if (rdev->family >= CHIP_R600)
  300. ret = radeon_get_atom_connector_info_from_object_table(dev);
  301. else
  302. ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
  303. } else {
  304. ret = radeon_get_legacy_connector_info_from_bios(dev);
  305. if (ret == false)
  306. ret = radeon_get_legacy_connector_info_from_table(dev);
  307. }
  308. } else {
  309. if (!ASIC_IS_AVIVO(rdev))
  310. ret = radeon_get_legacy_connector_info_from_table(dev);
  311. }
  312. if (ret) {
  313. radeon_setup_encoder_clones(dev);
  314. radeon_print_display_setup(dev);
  315. list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head)
  316. radeon_ddc_dump(drm_connector);
  317. }
  318. return ret;
  319. }
  320. int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
  321. {
  322. int ret = 0;
  323. if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
  324. (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)) {
  325. struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
  326. if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
  327. dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) && dig->dp_i2c_bus)
  328. radeon_connector->edid = drm_get_edid(&radeon_connector->base, &dig->dp_i2c_bus->adapter);
  329. }
  330. if (!radeon_connector->ddc_bus)
  331. return -1;
  332. if (!radeon_connector->edid) {
  333. radeon_i2c_do_lock(radeon_connector->ddc_bus, 1);
  334. radeon_connector->edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
  335. radeon_i2c_do_lock(radeon_connector->ddc_bus, 0);
  336. }
  337. if (radeon_connector->edid) {
  338. drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
  339. ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
  340. return ret;
  341. }
  342. drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
  343. return 0;
  344. }
  345. static int radeon_ddc_dump(struct drm_connector *connector)
  346. {
  347. struct edid *edid;
  348. struct radeon_connector *radeon_connector = to_radeon_connector(connector);
  349. int ret = 0;
  350. if (!radeon_connector->ddc_bus)
  351. return -1;
  352. radeon_i2c_do_lock(radeon_connector->ddc_bus, 1);
  353. edid = drm_get_edid(connector, &radeon_connector->ddc_bus->adapter);
  354. radeon_i2c_do_lock(radeon_connector->ddc_bus, 0);
  355. if (edid) {
  356. kfree(edid);
  357. }
  358. return ret;
  359. }
  360. static inline uint32_t radeon_div(uint64_t n, uint32_t d)
  361. {
  362. uint64_t mod;
  363. n += d / 2;
  364. mod = do_div(n, d);
  365. return n;
  366. }
  367. void radeon_compute_pll(struct radeon_pll *pll,
  368. uint64_t freq,
  369. uint32_t *dot_clock_p,
  370. uint32_t *fb_div_p,
  371. uint32_t *frac_fb_div_p,
  372. uint32_t *ref_div_p,
  373. uint32_t *post_div_p)
  374. {
  375. uint32_t min_ref_div = pll->min_ref_div;
  376. uint32_t max_ref_div = pll->max_ref_div;
  377. uint32_t min_post_div = pll->min_post_div;
  378. uint32_t max_post_div = pll->max_post_div;
  379. uint32_t min_fractional_feed_div = 0;
  380. uint32_t max_fractional_feed_div = 0;
  381. uint32_t best_vco = pll->best_vco;
  382. uint32_t best_post_div = 1;
  383. uint32_t best_ref_div = 1;
  384. uint32_t best_feedback_div = 1;
  385. uint32_t best_frac_feedback_div = 0;
  386. uint32_t best_freq = -1;
  387. uint32_t best_error = 0xffffffff;
  388. uint32_t best_vco_diff = 1;
  389. uint32_t post_div;
  390. DRM_DEBUG("PLL freq %llu %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
  391. freq = freq * 1000;
  392. if (pll->flags & RADEON_PLL_USE_REF_DIV)
  393. min_ref_div = max_ref_div = pll->reference_div;
  394. else {
  395. while (min_ref_div < max_ref_div-1) {
  396. uint32_t mid = (min_ref_div + max_ref_div) / 2;
  397. uint32_t pll_in = pll->reference_freq / mid;
  398. if (pll_in < pll->pll_in_min)
  399. max_ref_div = mid;
  400. else if (pll_in > pll->pll_in_max)
  401. min_ref_div = mid;
  402. else
  403. break;
  404. }
  405. }
  406. if (pll->flags & RADEON_PLL_USE_POST_DIV)
  407. min_post_div = max_post_div = pll->post_div;
  408. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  409. min_fractional_feed_div = pll->min_frac_feedback_div;
  410. max_fractional_feed_div = pll->max_frac_feedback_div;
  411. }
  412. for (post_div = min_post_div; post_div <= max_post_div; ++post_div) {
  413. uint32_t ref_div;
  414. if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
  415. continue;
  416. /* legacy radeons only have a few post_divs */
  417. if (pll->flags & RADEON_PLL_LEGACY) {
  418. if ((post_div == 5) ||
  419. (post_div == 7) ||
  420. (post_div == 9) ||
  421. (post_div == 10) ||
  422. (post_div == 11) ||
  423. (post_div == 13) ||
  424. (post_div == 14) ||
  425. (post_div == 15))
  426. continue;
  427. }
  428. for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
  429. uint32_t feedback_div, current_freq = 0, error, vco_diff;
  430. uint32_t pll_in = pll->reference_freq / ref_div;
  431. uint32_t min_feed_div = pll->min_feedback_div;
  432. uint32_t max_feed_div = pll->max_feedback_div + 1;
  433. if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
  434. continue;
  435. while (min_feed_div < max_feed_div) {
  436. uint32_t vco;
  437. uint32_t min_frac_feed_div = min_fractional_feed_div;
  438. uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
  439. uint32_t frac_feedback_div;
  440. uint64_t tmp;
  441. feedback_div = (min_feed_div + max_feed_div) / 2;
  442. tmp = (uint64_t)pll->reference_freq * feedback_div;
  443. vco = radeon_div(tmp, ref_div);
  444. if (vco < pll->pll_out_min) {
  445. min_feed_div = feedback_div + 1;
  446. continue;
  447. } else if (vco > pll->pll_out_max) {
  448. max_feed_div = feedback_div;
  449. continue;
  450. }
  451. while (min_frac_feed_div < max_frac_feed_div) {
  452. frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
  453. tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
  454. tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
  455. current_freq = radeon_div(tmp, ref_div * post_div);
  456. if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
  457. error = freq - current_freq;
  458. error = error < 0 ? 0xffffffff : error;
  459. } else
  460. error = abs(current_freq - freq);
  461. vco_diff = abs(vco - best_vco);
  462. if ((best_vco == 0 && error < best_error) ||
  463. (best_vco != 0 &&
  464. (error < best_error - 100 ||
  465. (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
  466. best_post_div = post_div;
  467. best_ref_div = ref_div;
  468. best_feedback_div = feedback_div;
  469. best_frac_feedback_div = frac_feedback_div;
  470. best_freq = current_freq;
  471. best_error = error;
  472. best_vco_diff = vco_diff;
  473. } else if (current_freq == freq) {
  474. if (best_freq == -1) {
  475. best_post_div = post_div;
  476. best_ref_div = ref_div;
  477. best_feedback_div = feedback_div;
  478. best_frac_feedback_div = frac_feedback_div;
  479. best_freq = current_freq;
  480. best_error = error;
  481. best_vco_diff = vco_diff;
  482. } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
  483. ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
  484. ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
  485. ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
  486. ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
  487. ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
  488. best_post_div = post_div;
  489. best_ref_div = ref_div;
  490. best_feedback_div = feedback_div;
  491. best_frac_feedback_div = frac_feedback_div;
  492. best_freq = current_freq;
  493. best_error = error;
  494. best_vco_diff = vco_diff;
  495. }
  496. }
  497. if (current_freq < freq)
  498. min_frac_feed_div = frac_feedback_div + 1;
  499. else
  500. max_frac_feed_div = frac_feedback_div;
  501. }
  502. if (current_freq < freq)
  503. min_feed_div = feedback_div + 1;
  504. else
  505. max_feed_div = feedback_div;
  506. }
  507. }
  508. }
  509. *dot_clock_p = best_freq / 10000;
  510. *fb_div_p = best_feedback_div;
  511. *frac_fb_div_p = best_frac_feedback_div;
  512. *ref_div_p = best_ref_div;
  513. *post_div_p = best_post_div;
  514. }
  515. void radeon_compute_pll_avivo(struct radeon_pll *pll,
  516. uint64_t freq,
  517. uint32_t *dot_clock_p,
  518. uint32_t *fb_div_p,
  519. uint32_t *frac_fb_div_p,
  520. uint32_t *ref_div_p,
  521. uint32_t *post_div_p)
  522. {
  523. fixed20_12 m, n, frac_n, p, f_vco, f_pclk, best_freq;
  524. fixed20_12 pll_out_max, pll_out_min;
  525. fixed20_12 pll_in_max, pll_in_min;
  526. fixed20_12 reference_freq;
  527. fixed20_12 error, ffreq, a, b;
  528. pll_out_max.full = rfixed_const(pll->pll_out_max);
  529. pll_out_min.full = rfixed_const(pll->pll_out_min);
  530. pll_in_max.full = rfixed_const(pll->pll_in_max);
  531. pll_in_min.full = rfixed_const(pll->pll_in_min);
  532. reference_freq.full = rfixed_const(pll->reference_freq);
  533. do_div(freq, 10);
  534. ffreq.full = rfixed_const(freq);
  535. error.full = rfixed_const(100 * 100);
  536. /* max p */
  537. p.full = rfixed_div(pll_out_max, ffreq);
  538. p.full = rfixed_floor(p);
  539. /* min m */
  540. m.full = rfixed_div(reference_freq, pll_in_max);
  541. m.full = rfixed_ceil(m);
  542. while (1) {
  543. n.full = rfixed_div(ffreq, reference_freq);
  544. n.full = rfixed_mul(n, m);
  545. n.full = rfixed_mul(n, p);
  546. f_vco.full = rfixed_div(n, m);
  547. f_vco.full = rfixed_mul(f_vco, reference_freq);
  548. f_pclk.full = rfixed_div(f_vco, p);
  549. if (f_pclk.full > ffreq.full)
  550. error.full = f_pclk.full - ffreq.full;
  551. else
  552. error.full = ffreq.full - f_pclk.full;
  553. error.full = rfixed_div(error, f_pclk);
  554. a.full = rfixed_const(100 * 100);
  555. error.full = rfixed_mul(error, a);
  556. a.full = rfixed_mul(m, p);
  557. a.full = rfixed_div(n, a);
  558. best_freq.full = rfixed_mul(reference_freq, a);
  559. if (rfixed_trunc(error) < 25)
  560. break;
  561. a.full = rfixed_const(1);
  562. m.full = m.full + a.full;
  563. a.full = rfixed_div(reference_freq, m);
  564. if (a.full >= pll_in_min.full)
  565. continue;
  566. m.full = rfixed_div(reference_freq, pll_in_max);
  567. m.full = rfixed_ceil(m);
  568. a.full= rfixed_const(1);
  569. p.full = p.full - a.full;
  570. a.full = rfixed_mul(p, ffreq);
  571. if (a.full >= pll_out_min.full)
  572. continue;
  573. else {
  574. DRM_ERROR("Unable to find pll dividers\n");
  575. break;
  576. }
  577. }
  578. a.full = rfixed_const(10);
  579. b.full = rfixed_mul(n, a);
  580. frac_n.full = rfixed_floor(n);
  581. frac_n.full = rfixed_mul(frac_n, a);
  582. frac_n.full = b.full - frac_n.full;
  583. *dot_clock_p = rfixed_trunc(best_freq);
  584. *fb_div_p = rfixed_trunc(n);
  585. *frac_fb_div_p = rfixed_trunc(frac_n);
  586. *ref_div_p = rfixed_trunc(m);
  587. *post_div_p = rfixed_trunc(p);
  588. DRM_DEBUG("%u %d.%d, %d, %d\n", *dot_clock_p * 10, *fb_div_p, *frac_fb_div_p, *ref_div_p, *post_div_p);
  589. }
  590. static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
  591. {
  592. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  593. struct drm_device *dev = fb->dev;
  594. if (fb->fbdev)
  595. radeonfb_remove(dev, fb);
  596. if (radeon_fb->obj) {
  597. mutex_lock(&dev->struct_mutex);
  598. drm_gem_object_unreference(radeon_fb->obj);
  599. mutex_unlock(&dev->struct_mutex);
  600. }
  601. drm_framebuffer_cleanup(fb);
  602. kfree(radeon_fb);
  603. }
  604. static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  605. struct drm_file *file_priv,
  606. unsigned int *handle)
  607. {
  608. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  609. return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
  610. }
  611. static const struct drm_framebuffer_funcs radeon_fb_funcs = {
  612. .destroy = radeon_user_framebuffer_destroy,
  613. .create_handle = radeon_user_framebuffer_create_handle,
  614. };
  615. struct drm_framebuffer *
  616. radeon_framebuffer_create(struct drm_device *dev,
  617. struct drm_mode_fb_cmd *mode_cmd,
  618. struct drm_gem_object *obj)
  619. {
  620. struct radeon_framebuffer *radeon_fb;
  621. radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
  622. if (radeon_fb == NULL) {
  623. return NULL;
  624. }
  625. drm_framebuffer_init(dev, &radeon_fb->base, &radeon_fb_funcs);
  626. drm_helper_mode_fill_fb_struct(&radeon_fb->base, mode_cmd);
  627. radeon_fb->obj = obj;
  628. return &radeon_fb->base;
  629. }
  630. static struct drm_framebuffer *
  631. radeon_user_framebuffer_create(struct drm_device *dev,
  632. struct drm_file *file_priv,
  633. struct drm_mode_fb_cmd *mode_cmd)
  634. {
  635. struct drm_gem_object *obj;
  636. obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
  637. if (obj == NULL) {
  638. dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
  639. "can't create framebuffer\n", mode_cmd->handle);
  640. return NULL;
  641. }
  642. return radeon_framebuffer_create(dev, mode_cmd, obj);
  643. }
  644. static const struct drm_mode_config_funcs radeon_mode_funcs = {
  645. .fb_create = radeon_user_framebuffer_create,
  646. .fb_changed = radeonfb_probe,
  647. };
  648. struct drm_prop_enum_list {
  649. int type;
  650. char *name;
  651. };
  652. static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
  653. { { 0, "driver" },
  654. { 1, "bios" },
  655. };
  656. static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
  657. { { TV_STD_NTSC, "ntsc" },
  658. { TV_STD_PAL, "pal" },
  659. { TV_STD_PAL_M, "pal-m" },
  660. { TV_STD_PAL_60, "pal-60" },
  661. { TV_STD_NTSC_J, "ntsc-j" },
  662. { TV_STD_SCART_PAL, "scart-pal" },
  663. { TV_STD_PAL_CN, "pal-cn" },
  664. { TV_STD_SECAM, "secam" },
  665. };
  666. static int radeon_modeset_create_props(struct radeon_device *rdev)
  667. {
  668. int i, sz;
  669. if (rdev->is_atom_bios) {
  670. rdev->mode_info.coherent_mode_property =
  671. drm_property_create(rdev->ddev,
  672. DRM_MODE_PROP_RANGE,
  673. "coherent", 2);
  674. if (!rdev->mode_info.coherent_mode_property)
  675. return -ENOMEM;
  676. rdev->mode_info.coherent_mode_property->values[0] = 0;
  677. rdev->mode_info.coherent_mode_property->values[1] = 1;
  678. }
  679. if (!ASIC_IS_AVIVO(rdev)) {
  680. sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
  681. rdev->mode_info.tmds_pll_property =
  682. drm_property_create(rdev->ddev,
  683. DRM_MODE_PROP_ENUM,
  684. "tmds_pll", sz);
  685. for (i = 0; i < sz; i++) {
  686. drm_property_add_enum(rdev->mode_info.tmds_pll_property,
  687. i,
  688. radeon_tmds_pll_enum_list[i].type,
  689. radeon_tmds_pll_enum_list[i].name);
  690. }
  691. }
  692. rdev->mode_info.load_detect_property =
  693. drm_property_create(rdev->ddev,
  694. DRM_MODE_PROP_RANGE,
  695. "load detection", 2);
  696. if (!rdev->mode_info.load_detect_property)
  697. return -ENOMEM;
  698. rdev->mode_info.load_detect_property->values[0] = 0;
  699. rdev->mode_info.load_detect_property->values[1] = 1;
  700. drm_mode_create_scaling_mode_property(rdev->ddev);
  701. sz = ARRAY_SIZE(radeon_tv_std_enum_list);
  702. rdev->mode_info.tv_std_property =
  703. drm_property_create(rdev->ddev,
  704. DRM_MODE_PROP_ENUM,
  705. "tv standard", sz);
  706. for (i = 0; i < sz; i++) {
  707. drm_property_add_enum(rdev->mode_info.tv_std_property,
  708. i,
  709. radeon_tv_std_enum_list[i].type,
  710. radeon_tv_std_enum_list[i].name);
  711. }
  712. return 0;
  713. }
  714. int radeon_modeset_init(struct radeon_device *rdev)
  715. {
  716. int num_crtc = 2, i;
  717. int ret;
  718. drm_mode_config_init(rdev->ddev);
  719. rdev->mode_info.mode_config_initialized = true;
  720. rdev->ddev->mode_config.funcs = (void *)&radeon_mode_funcs;
  721. if (ASIC_IS_AVIVO(rdev)) {
  722. rdev->ddev->mode_config.max_width = 8192;
  723. rdev->ddev->mode_config.max_height = 8192;
  724. } else {
  725. rdev->ddev->mode_config.max_width = 4096;
  726. rdev->ddev->mode_config.max_height = 4096;
  727. }
  728. rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
  729. ret = radeon_modeset_create_props(rdev);
  730. if (ret) {
  731. return ret;
  732. }
  733. if (rdev->flags & RADEON_SINGLE_CRTC)
  734. num_crtc = 1;
  735. /* allocate crtcs */
  736. for (i = 0; i < num_crtc; i++) {
  737. radeon_crtc_init(rdev->ddev, i);
  738. }
  739. /* okay we should have all the bios connectors */
  740. ret = radeon_setup_enc_conn(rdev->ddev);
  741. if (!ret) {
  742. return ret;
  743. }
  744. /* initialize hpd */
  745. radeon_hpd_init(rdev);
  746. drm_helper_initial_config(rdev->ddev);
  747. return 0;
  748. }
  749. void radeon_modeset_fini(struct radeon_device *rdev)
  750. {
  751. if (rdev->mode_info.mode_config_initialized) {
  752. radeon_hpd_fini(rdev);
  753. drm_mode_config_cleanup(rdev->ddev);
  754. rdev->mode_info.mode_config_initialized = false;
  755. }
  756. }
  757. bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
  758. struct drm_display_mode *mode,
  759. struct drm_display_mode *adjusted_mode)
  760. {
  761. struct drm_device *dev = crtc->dev;
  762. struct drm_encoder *encoder;
  763. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  764. struct radeon_encoder *radeon_encoder;
  765. bool first = true;
  766. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  767. radeon_encoder = to_radeon_encoder(encoder);
  768. if (encoder->crtc != crtc)
  769. continue;
  770. if (first) {
  771. /* set scaling */
  772. if (radeon_encoder->rmx_type == RMX_OFF)
  773. radeon_crtc->rmx_type = RMX_OFF;
  774. else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
  775. mode->vdisplay < radeon_encoder->native_mode.vdisplay)
  776. radeon_crtc->rmx_type = radeon_encoder->rmx_type;
  777. else
  778. radeon_crtc->rmx_type = RMX_OFF;
  779. /* copy native mode */
  780. memcpy(&radeon_crtc->native_mode,
  781. &radeon_encoder->native_mode,
  782. sizeof(struct drm_display_mode));
  783. first = false;
  784. } else {
  785. if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
  786. /* WARNING: Right now this can't happen but
  787. * in the future we need to check that scaling
  788. * are consistent accross different encoder
  789. * (ie all encoder can work with the same
  790. * scaling).
  791. */
  792. DRM_ERROR("Scaling not consistent accross encoder.\n");
  793. return false;
  794. }
  795. }
  796. }
  797. if (radeon_crtc->rmx_type != RMX_OFF) {
  798. fixed20_12 a, b;
  799. a.full = rfixed_const(crtc->mode.vdisplay);
  800. b.full = rfixed_const(radeon_crtc->native_mode.hdisplay);
  801. radeon_crtc->vsc.full = rfixed_div(a, b);
  802. a.full = rfixed_const(crtc->mode.hdisplay);
  803. b.full = rfixed_const(radeon_crtc->native_mode.vdisplay);
  804. radeon_crtc->hsc.full = rfixed_div(a, b);
  805. } else {
  806. radeon_crtc->vsc.full = rfixed_const(1);
  807. radeon_crtc->hsc.full = rfixed_const(1);
  808. }
  809. return true;
  810. }