radeon_display.c 26 KB

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