2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
23 * Authors: Dave Airlie
27 #include "radeon_drm.h"
31 #include <asm/div64.h>
33 #include "drm_crtc_helper.h"
36 static int radeon_ddc_dump(struct drm_connector *connector);
38 static void avivo_crtc_load_lut(struct drm_crtc *crtc)
40 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
41 struct drm_device *dev = crtc->dev;
42 struct radeon_device *rdev = dev->dev_private;
45 DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
46 WREG32(AVIVO_DC_LUTA_CONTROL + radeon_crtc->crtc_offset, 0);
48 WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
49 WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
50 WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
52 WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
53 WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
54 WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
56 WREG32(AVIVO_DC_LUT_RW_SELECT, radeon_crtc->crtc_id);
57 WREG32(AVIVO_DC_LUT_RW_MODE, 0);
58 WREG32(AVIVO_DC_LUT_WRITE_EN_MASK, 0x0000003f);
60 WREG8(AVIVO_DC_LUT_RW_INDEX, 0);
61 for (i = 0; i < 256; i++) {
62 WREG32(AVIVO_DC_LUT_30_COLOR,
63 (radeon_crtc->lut_r[i] << 20) |
64 (radeon_crtc->lut_g[i] << 10) |
65 (radeon_crtc->lut_b[i] << 0));
68 WREG32(AVIVO_D1GRPH_LUT_SEL + radeon_crtc->crtc_offset, radeon_crtc->crtc_id);
71 static void evergreen_crtc_load_lut(struct drm_crtc *crtc)
73 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
74 struct drm_device *dev = crtc->dev;
75 struct radeon_device *rdev = dev->dev_private;
78 DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
79 WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
81 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
82 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
83 WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
85 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
86 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
87 WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
89 WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
90 WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
92 WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
93 for (i = 0; i < 256; i++) {
94 WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
95 (radeon_crtc->lut_r[i] << 20) |
96 (radeon_crtc->lut_g[i] << 10) |
97 (radeon_crtc->lut_b[i] << 0));
101 static void legacy_crtc_load_lut(struct drm_crtc *crtc)
103 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
104 struct drm_device *dev = crtc->dev;
105 struct radeon_device *rdev = dev->dev_private;
109 dac2_cntl = RREG32(RADEON_DAC_CNTL2);
110 if (radeon_crtc->crtc_id == 0)
111 dac2_cntl &= (uint32_t)~RADEON_DAC2_PALETTE_ACC_CTL;
113 dac2_cntl |= RADEON_DAC2_PALETTE_ACC_CTL;
114 WREG32(RADEON_DAC_CNTL2, dac2_cntl);
116 WREG8(RADEON_PALETTE_INDEX, 0);
117 for (i = 0; i < 256; i++) {
118 WREG32(RADEON_PALETTE_30_DATA,
119 (radeon_crtc->lut_r[i] << 20) |
120 (radeon_crtc->lut_g[i] << 10) |
121 (radeon_crtc->lut_b[i] << 0));
125 void radeon_crtc_load_lut(struct drm_crtc *crtc)
127 struct drm_device *dev = crtc->dev;
128 struct radeon_device *rdev = dev->dev_private;
133 if (ASIC_IS_DCE4(rdev))
134 evergreen_crtc_load_lut(crtc);
135 else if (ASIC_IS_AVIVO(rdev))
136 avivo_crtc_load_lut(crtc);
138 legacy_crtc_load_lut(crtc);
141 /** Sets the color ramps on behalf of fbcon */
142 void radeon_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
145 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
147 radeon_crtc->lut_r[regno] = red >> 6;
148 radeon_crtc->lut_g[regno] = green >> 6;
149 radeon_crtc->lut_b[regno] = blue >> 6;
152 /** Gets the color ramps on behalf of fbcon */
153 void radeon_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
154 u16 *blue, int regno)
156 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
158 *red = radeon_crtc->lut_r[regno] << 6;
159 *green = radeon_crtc->lut_g[regno] << 6;
160 *blue = radeon_crtc->lut_b[regno] << 6;
163 static void radeon_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
164 u16 *blue, uint32_t start, uint32_t size)
166 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
167 int end = (start + size > 256) ? 256 : start + size, i;
169 /* userspace palettes are always correct as is */
170 for (i = start; i < end; i++) {
171 radeon_crtc->lut_r[i] = red[i] >> 6;
172 radeon_crtc->lut_g[i] = green[i] >> 6;
173 radeon_crtc->lut_b[i] = blue[i] >> 6;
175 radeon_crtc_load_lut(crtc);
178 static void radeon_crtc_destroy(struct drm_crtc *crtc)
180 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
182 drm_crtc_cleanup(crtc);
186 static const struct drm_crtc_funcs radeon_crtc_funcs = {
187 .cursor_set = radeon_crtc_cursor_set,
188 .cursor_move = radeon_crtc_cursor_move,
189 .gamma_set = radeon_crtc_gamma_set,
190 .set_config = drm_crtc_helper_set_config,
191 .destroy = radeon_crtc_destroy,
194 static void radeon_crtc_init(struct drm_device *dev, int index)
196 struct radeon_device *rdev = dev->dev_private;
197 struct radeon_crtc *radeon_crtc;
200 radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
201 if (radeon_crtc == NULL)
204 drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
206 drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
207 radeon_crtc->crtc_id = index;
208 rdev->mode_info.crtcs[index] = radeon_crtc;
211 radeon_crtc->mode_set.crtc = &radeon_crtc->base;
212 radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
213 radeon_crtc->mode_set.num_connectors = 0;
216 for (i = 0; i < 256; i++) {
217 radeon_crtc->lut_r[i] = i << 2;
218 radeon_crtc->lut_g[i] = i << 2;
219 radeon_crtc->lut_b[i] = i << 2;
222 if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
223 radeon_atombios_init_crtc(dev, radeon_crtc);
225 radeon_legacy_init_crtc(dev, radeon_crtc);
228 static const char *encoder_names[34] = {
248 "INTERNAL_KLDSCP_TMDS1",
249 "INTERNAL_KLDSCP_DVO1",
250 "INTERNAL_KLDSCP_DAC1",
251 "INTERNAL_KLDSCP_DAC2",
260 "INTERNAL_KLDSCP_LVTMA",
265 static const char *connector_names[15] = {
283 static const char *hpd_names[6] = {
292 static void radeon_print_display_setup(struct drm_device *dev)
294 struct drm_connector *connector;
295 struct radeon_connector *radeon_connector;
296 struct drm_encoder *encoder;
297 struct radeon_encoder *radeon_encoder;
301 DRM_INFO("Radeon Display Connectors\n");
302 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
303 radeon_connector = to_radeon_connector(connector);
304 DRM_INFO("Connector %d:\n", i);
305 DRM_INFO(" %s\n", connector_names[connector->connector_type]);
306 if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
307 DRM_INFO(" %s\n", hpd_names[radeon_connector->hpd.hpd]);
308 if (radeon_connector->ddc_bus) {
309 DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
310 radeon_connector->ddc_bus->rec.mask_clk_reg,
311 radeon_connector->ddc_bus->rec.mask_data_reg,
312 radeon_connector->ddc_bus->rec.a_clk_reg,
313 radeon_connector->ddc_bus->rec.a_data_reg,
314 radeon_connector->ddc_bus->rec.en_clk_reg,
315 radeon_connector->ddc_bus->rec.en_data_reg,
316 radeon_connector->ddc_bus->rec.y_clk_reg,
317 radeon_connector->ddc_bus->rec.y_data_reg);
318 if (radeon_connector->router_bus)
319 DRM_INFO(" DDC Router 0x%x/0x%x\n",
320 radeon_connector->router.mux_control_pin,
321 radeon_connector->router.mux_state);
323 if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
324 connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
325 connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
326 connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
327 connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
328 connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
329 DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
331 DRM_INFO(" Encoders:\n");
332 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
333 radeon_encoder = to_radeon_encoder(encoder);
334 devices = radeon_encoder->devices & radeon_connector->devices;
336 if (devices & ATOM_DEVICE_CRT1_SUPPORT)
337 DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
338 if (devices & ATOM_DEVICE_CRT2_SUPPORT)
339 DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
340 if (devices & ATOM_DEVICE_LCD1_SUPPORT)
341 DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
342 if (devices & ATOM_DEVICE_DFP1_SUPPORT)
343 DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
344 if (devices & ATOM_DEVICE_DFP2_SUPPORT)
345 DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
346 if (devices & ATOM_DEVICE_DFP3_SUPPORT)
347 DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
348 if (devices & ATOM_DEVICE_DFP4_SUPPORT)
349 DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
350 if (devices & ATOM_DEVICE_DFP5_SUPPORT)
351 DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
352 if (devices & ATOM_DEVICE_DFP6_SUPPORT)
353 DRM_INFO(" DFP6: %s\n", encoder_names[radeon_encoder->encoder_id]);
354 if (devices & ATOM_DEVICE_TV1_SUPPORT)
355 DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
356 if (devices & ATOM_DEVICE_CV_SUPPORT)
357 DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
364 static bool radeon_setup_enc_conn(struct drm_device *dev)
366 struct radeon_device *rdev = dev->dev_private;
367 struct drm_connector *drm_connector;
371 if (rdev->is_atom_bios) {
372 ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
374 ret = radeon_get_atom_connector_info_from_object_table(dev);
376 ret = radeon_get_legacy_connector_info_from_bios(dev);
378 ret = radeon_get_legacy_connector_info_from_table(dev);
381 if (!ASIC_IS_AVIVO(rdev))
382 ret = radeon_get_legacy_connector_info_from_table(dev);
385 radeon_setup_encoder_clones(dev);
386 radeon_print_display_setup(dev);
387 list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head)
388 radeon_ddc_dump(drm_connector);
394 int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
396 struct drm_device *dev = radeon_connector->base.dev;
397 struct radeon_device *rdev = dev->dev_private;
400 /* on hw with routers, select right port */
401 if (radeon_connector->router.valid)
402 radeon_router_select_port(radeon_connector);
404 if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
405 (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)) {
406 struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
407 if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
408 dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) && dig->dp_i2c_bus)
409 radeon_connector->edid = drm_get_edid(&radeon_connector->base, &dig->dp_i2c_bus->adapter);
411 if (!radeon_connector->ddc_bus)
413 if (!radeon_connector->edid) {
414 radeon_connector->edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
416 /* some servers provide a hardcoded edid in rom for KVMs */
417 if (!radeon_connector->edid)
418 radeon_connector->edid = radeon_combios_get_hardcoded_edid(rdev);
419 if (radeon_connector->edid) {
420 drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
421 ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
424 drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
428 static int radeon_ddc_dump(struct drm_connector *connector)
431 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
434 /* on hw with routers, select right port */
435 if (radeon_connector->router.valid)
436 radeon_router_select_port(radeon_connector);
438 if (!radeon_connector->ddc_bus)
440 edid = drm_get_edid(connector, &radeon_connector->ddc_bus->adapter);
447 static inline uint32_t radeon_div(uint64_t n, uint32_t d)
457 void radeon_compute_pll(struct radeon_pll *pll,
459 uint32_t *dot_clock_p,
461 uint32_t *frac_fb_div_p,
463 uint32_t *post_div_p)
465 uint32_t min_ref_div = pll->min_ref_div;
466 uint32_t max_ref_div = pll->max_ref_div;
467 uint32_t min_post_div = pll->min_post_div;
468 uint32_t max_post_div = pll->max_post_div;
469 uint32_t min_fractional_feed_div = 0;
470 uint32_t max_fractional_feed_div = 0;
471 uint32_t best_vco = pll->best_vco;
472 uint32_t best_post_div = 1;
473 uint32_t best_ref_div = 1;
474 uint32_t best_feedback_div = 1;
475 uint32_t best_frac_feedback_div = 0;
476 uint32_t best_freq = -1;
477 uint32_t best_error = 0xffffffff;
478 uint32_t best_vco_diff = 1;
480 u32 pll_out_min, pll_out_max;
482 DRM_DEBUG_KMS("PLL freq %llu %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
485 if (pll->flags & RADEON_PLL_IS_LCD) {
486 pll_out_min = pll->lcd_pll_out_min;
487 pll_out_max = pll->lcd_pll_out_max;
489 pll_out_min = pll->pll_out_min;
490 pll_out_max = pll->pll_out_max;
493 if (pll->flags & RADEON_PLL_USE_REF_DIV)
494 min_ref_div = max_ref_div = pll->reference_div;
496 while (min_ref_div < max_ref_div-1) {
497 uint32_t mid = (min_ref_div + max_ref_div) / 2;
498 uint32_t pll_in = pll->reference_freq / mid;
499 if (pll_in < pll->pll_in_min)
501 else if (pll_in > pll->pll_in_max)
508 if (pll->flags & RADEON_PLL_USE_POST_DIV)
509 min_post_div = max_post_div = pll->post_div;
511 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
512 min_fractional_feed_div = pll->min_frac_feedback_div;
513 max_fractional_feed_div = pll->max_frac_feedback_div;
516 for (post_div = max_post_div; post_div >= min_post_div; --post_div) {
519 if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
522 /* legacy radeons only have a few post_divs */
523 if (pll->flags & RADEON_PLL_LEGACY) {
524 if ((post_div == 5) ||
535 for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
536 uint32_t feedback_div, current_freq = 0, error, vco_diff;
537 uint32_t pll_in = pll->reference_freq / ref_div;
538 uint32_t min_feed_div = pll->min_feedback_div;
539 uint32_t max_feed_div = pll->max_feedback_div + 1;
541 if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
544 while (min_feed_div < max_feed_div) {
546 uint32_t min_frac_feed_div = min_fractional_feed_div;
547 uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
548 uint32_t frac_feedback_div;
551 feedback_div = (min_feed_div + max_feed_div) / 2;
553 tmp = (uint64_t)pll->reference_freq * feedback_div;
554 vco = radeon_div(tmp, ref_div);
556 if (vco < pll_out_min) {
557 min_feed_div = feedback_div + 1;
559 } else if (vco > pll_out_max) {
560 max_feed_div = feedback_div;
564 while (min_frac_feed_div < max_frac_feed_div) {
565 frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
566 tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
567 tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
568 current_freq = radeon_div(tmp, ref_div * post_div);
570 if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
571 if (freq < current_freq)
574 error = freq - current_freq;
576 error = abs(current_freq - freq);
577 vco_diff = abs(vco - best_vco);
579 if ((best_vco == 0 && error < best_error) ||
581 ((best_error > 100 && error < best_error - 100) ||
582 (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
583 best_post_div = post_div;
584 best_ref_div = ref_div;
585 best_feedback_div = feedback_div;
586 best_frac_feedback_div = frac_feedback_div;
587 best_freq = current_freq;
589 best_vco_diff = vco_diff;
590 } else if (current_freq == freq) {
591 if (best_freq == -1) {
592 best_post_div = post_div;
593 best_ref_div = ref_div;
594 best_feedback_div = feedback_div;
595 best_frac_feedback_div = frac_feedback_div;
596 best_freq = current_freq;
598 best_vco_diff = vco_diff;
599 } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
600 ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
601 ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
602 ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
603 ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
604 ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
605 best_post_div = post_div;
606 best_ref_div = ref_div;
607 best_feedback_div = feedback_div;
608 best_frac_feedback_div = frac_feedback_div;
609 best_freq = current_freq;
611 best_vco_diff = vco_diff;
614 if (current_freq < freq)
615 min_frac_feed_div = frac_feedback_div + 1;
617 max_frac_feed_div = frac_feedback_div;
619 if (current_freq < freq)
620 min_feed_div = feedback_div + 1;
622 max_feed_div = feedback_div;
627 *dot_clock_p = best_freq / 10000;
628 *fb_div_p = best_feedback_div;
629 *frac_fb_div_p = best_frac_feedback_div;
630 *ref_div_p = best_ref_div;
631 *post_div_p = best_post_div;
634 static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
636 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
638 if (radeon_fb->obj) {
639 drm_gem_object_unreference_unlocked(radeon_fb->obj);
641 drm_framebuffer_cleanup(fb);
645 static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
646 struct drm_file *file_priv,
647 unsigned int *handle)
649 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
651 return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
654 static const struct drm_framebuffer_funcs radeon_fb_funcs = {
655 .destroy = radeon_user_framebuffer_destroy,
656 .create_handle = radeon_user_framebuffer_create_handle,
660 radeon_framebuffer_init(struct drm_device *dev,
661 struct radeon_framebuffer *rfb,
662 struct drm_mode_fb_cmd *mode_cmd,
663 struct drm_gem_object *obj)
666 drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
667 drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
670 static struct drm_framebuffer *
671 radeon_user_framebuffer_create(struct drm_device *dev,
672 struct drm_file *file_priv,
673 struct drm_mode_fb_cmd *mode_cmd)
675 struct drm_gem_object *obj;
676 struct radeon_framebuffer *radeon_fb;
678 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
680 dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
681 "can't create framebuffer\n", mode_cmd->handle);
682 return ERR_PTR(-ENOENT);
685 radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
686 if (radeon_fb == NULL)
687 return ERR_PTR(-ENOMEM);
689 radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
691 return &radeon_fb->base;
694 static void radeon_output_poll_changed(struct drm_device *dev)
696 struct radeon_device *rdev = dev->dev_private;
697 radeon_fb_output_poll_changed(rdev);
700 static const struct drm_mode_config_funcs radeon_mode_funcs = {
701 .fb_create = radeon_user_framebuffer_create,
702 .output_poll_changed = radeon_output_poll_changed
705 struct drm_prop_enum_list {
710 static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
715 static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
716 { { TV_STD_NTSC, "ntsc" },
717 { TV_STD_PAL, "pal" },
718 { TV_STD_PAL_M, "pal-m" },
719 { TV_STD_PAL_60, "pal-60" },
720 { TV_STD_NTSC_J, "ntsc-j" },
721 { TV_STD_SCART_PAL, "scart-pal" },
722 { TV_STD_PAL_CN, "pal-cn" },
723 { TV_STD_SECAM, "secam" },
726 static struct drm_prop_enum_list radeon_underscan_enum_list[] =
727 { { UNDERSCAN_OFF, "off" },
728 { UNDERSCAN_ON, "on" },
729 { UNDERSCAN_AUTO, "auto" },
732 static int radeon_modeset_create_props(struct radeon_device *rdev)
736 if (rdev->is_atom_bios) {
737 rdev->mode_info.coherent_mode_property =
738 drm_property_create(rdev->ddev,
741 if (!rdev->mode_info.coherent_mode_property)
744 rdev->mode_info.coherent_mode_property->values[0] = 0;
745 rdev->mode_info.coherent_mode_property->values[1] = 1;
748 if (!ASIC_IS_AVIVO(rdev)) {
749 sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
750 rdev->mode_info.tmds_pll_property =
751 drm_property_create(rdev->ddev,
754 for (i = 0; i < sz; i++) {
755 drm_property_add_enum(rdev->mode_info.tmds_pll_property,
757 radeon_tmds_pll_enum_list[i].type,
758 radeon_tmds_pll_enum_list[i].name);
762 rdev->mode_info.load_detect_property =
763 drm_property_create(rdev->ddev,
765 "load detection", 2);
766 if (!rdev->mode_info.load_detect_property)
768 rdev->mode_info.load_detect_property->values[0] = 0;
769 rdev->mode_info.load_detect_property->values[1] = 1;
771 drm_mode_create_scaling_mode_property(rdev->ddev);
773 sz = ARRAY_SIZE(radeon_tv_std_enum_list);
774 rdev->mode_info.tv_std_property =
775 drm_property_create(rdev->ddev,
778 for (i = 0; i < sz; i++) {
779 drm_property_add_enum(rdev->mode_info.tv_std_property,
781 radeon_tv_std_enum_list[i].type,
782 radeon_tv_std_enum_list[i].name);
785 sz = ARRAY_SIZE(radeon_underscan_enum_list);
786 rdev->mode_info.underscan_property =
787 drm_property_create(rdev->ddev,
790 for (i = 0; i < sz; i++) {
791 drm_property_add_enum(rdev->mode_info.underscan_property,
793 radeon_underscan_enum_list[i].type,
794 radeon_underscan_enum_list[i].name);
797 rdev->mode_info.underscan_hborder_property =
798 drm_property_create(rdev->ddev,
800 "underscan hborder", 2);
801 if (!rdev->mode_info.underscan_hborder_property)
803 rdev->mode_info.underscan_hborder_property->values[0] = 0;
804 rdev->mode_info.underscan_hborder_property->values[1] = 128;
806 rdev->mode_info.underscan_vborder_property =
807 drm_property_create(rdev->ddev,
809 "underscan vborder", 2);
810 if (!rdev->mode_info.underscan_vborder_property)
812 rdev->mode_info.underscan_vborder_property->values[0] = 0;
813 rdev->mode_info.underscan_vborder_property->values[1] = 128;
818 void radeon_update_display_priority(struct radeon_device *rdev)
820 /* adjustment options for the display watermarks */
821 if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
822 /* set display priority to high for r3xx, rv515 chips
823 * this avoids flickering due to underflow to the
824 * display controllers during heavy acceleration.
825 * Don't force high on rs4xx igp chips as it seems to
826 * affect the sound card. See kernel bug 15982.
828 if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
829 !(rdev->flags & RADEON_IS_IGP))
830 rdev->disp_priority = 2;
832 rdev->disp_priority = 0;
834 rdev->disp_priority = radeon_disp_priority;
838 int radeon_modeset_init(struct radeon_device *rdev)
843 drm_mode_config_init(rdev->ddev);
844 rdev->mode_info.mode_config_initialized = true;
846 rdev->ddev->mode_config.funcs = (void *)&radeon_mode_funcs;
848 if (ASIC_IS_AVIVO(rdev)) {
849 rdev->ddev->mode_config.max_width = 8192;
850 rdev->ddev->mode_config.max_height = 8192;
852 rdev->ddev->mode_config.max_width = 4096;
853 rdev->ddev->mode_config.max_height = 4096;
856 rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
858 ret = radeon_modeset_create_props(rdev);
864 radeon_i2c_init(rdev);
866 /* check combios for a valid hardcoded EDID - Sun servers */
867 if (!rdev->is_atom_bios) {
868 /* check for hardcoded EDID in BIOS */
869 radeon_combios_check_hardcoded_edid(rdev);
873 for (i = 0; i < rdev->num_crtc; i++) {
874 radeon_crtc_init(rdev->ddev, i);
877 /* okay we should have all the bios connectors */
878 ret = radeon_setup_enc_conn(rdev->ddev);
883 radeon_hpd_init(rdev);
885 /* Initialize power management */
886 radeon_pm_init(rdev);
888 radeon_fbdev_init(rdev);
889 drm_kms_helper_poll_init(rdev->ddev);
894 void radeon_modeset_fini(struct radeon_device *rdev)
896 radeon_fbdev_fini(rdev);
897 kfree(rdev->mode_info.bios_hardcoded_edid);
898 radeon_pm_fini(rdev);
900 if (rdev->mode_info.mode_config_initialized) {
901 drm_kms_helper_poll_fini(rdev->ddev);
902 radeon_hpd_fini(rdev);
903 drm_mode_config_cleanup(rdev->ddev);
904 rdev->mode_info.mode_config_initialized = false;
907 radeon_i2c_fini(rdev);
910 static bool is_hdtv_mode(struct drm_display_mode *mode)
912 /* try and guess if this is a tv or a monitor */
913 if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
914 (mode->vdisplay == 576) || /* 576p */
915 (mode->vdisplay == 720) || /* 720p */
916 (mode->vdisplay == 1080)) /* 1080p */
922 bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
923 struct drm_display_mode *mode,
924 struct drm_display_mode *adjusted_mode)
926 struct drm_device *dev = crtc->dev;
927 struct radeon_device *rdev = dev->dev_private;
928 struct drm_encoder *encoder;
929 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
930 struct radeon_encoder *radeon_encoder;
931 struct drm_connector *connector;
932 struct radeon_connector *radeon_connector;
934 u32 src_v = 1, dst_v = 1;
935 u32 src_h = 1, dst_h = 1;
937 radeon_crtc->h_border = 0;
938 radeon_crtc->v_border = 0;
940 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
941 if (encoder->crtc != crtc)
943 radeon_encoder = to_radeon_encoder(encoder);
944 connector = radeon_get_connector_for_encoder(encoder);
945 radeon_connector = to_radeon_connector(connector);
949 if (radeon_encoder->rmx_type == RMX_OFF)
950 radeon_crtc->rmx_type = RMX_OFF;
951 else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
952 mode->vdisplay < radeon_encoder->native_mode.vdisplay)
953 radeon_crtc->rmx_type = radeon_encoder->rmx_type;
955 radeon_crtc->rmx_type = RMX_OFF;
956 /* copy native mode */
957 memcpy(&radeon_crtc->native_mode,
958 &radeon_encoder->native_mode,
959 sizeof(struct drm_display_mode));
960 src_v = crtc->mode.vdisplay;
961 dst_v = radeon_crtc->native_mode.vdisplay;
962 src_h = crtc->mode.hdisplay;
963 dst_h = radeon_crtc->native_mode.hdisplay;
965 /* fix up for overscan on hdmi */
966 if (ASIC_IS_AVIVO(rdev) &&
967 (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
968 ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
969 ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
970 drm_detect_hdmi_monitor(radeon_connector->edid) &&
971 is_hdtv_mode(mode)))) {
972 if (radeon_encoder->underscan_hborder != 0)
973 radeon_crtc->h_border = radeon_encoder->underscan_hborder;
975 radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
976 if (radeon_encoder->underscan_vborder != 0)
977 radeon_crtc->v_border = radeon_encoder->underscan_vborder;
979 radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
980 radeon_crtc->rmx_type = RMX_FULL;
981 src_v = crtc->mode.vdisplay;
982 dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
983 src_h = crtc->mode.hdisplay;
984 dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
988 if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
989 /* WARNING: Right now this can't happen but
990 * in the future we need to check that scaling
991 * are consistent across different encoder
992 * (ie all encoder can work with the same
995 DRM_ERROR("Scaling not consistent across encoder.\n");
1000 if (radeon_crtc->rmx_type != RMX_OFF) {
1002 a.full = dfixed_const(src_v);
1003 b.full = dfixed_const(dst_v);
1004 radeon_crtc->vsc.full = dfixed_div(a, b);
1005 a.full = dfixed_const(src_h);
1006 b.full = dfixed_const(dst_h);
1007 radeon_crtc->hsc.full = dfixed_div(a, b);
1009 radeon_crtc->vsc.full = dfixed_const(1);
1010 radeon_crtc->hsc.full = dfixed_const(1);
1016 * Retrieve current video scanout position of crtc on a given gpu.
1018 * \param rdev Device to query.
1019 * \param crtc Crtc to query.
1020 * \param *vpos Location where vertical scanout position should be stored.
1021 * \param *hpos Location where horizontal scanout position should go.
1023 * Returns vpos as a positive number while in active scanout area.
1024 * Returns vpos as a negative number inside vblank, counting the number
1025 * of scanlines to go until end of vblank, e.g., -1 means "one scanline
1026 * until start of active scanout / end of vblank."
1028 * \return Flags, or'ed together as follows:
1030 * RADEON_SCANOUTPOS_VALID = Query successfull.
1031 * RADEON_SCANOUTPOS_INVBL = Inside vblank.
1032 * RADEON_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
1033 * this flag means that returned position may be offset by a constant but
1034 * unknown small number of scanlines wrt. real scanout position.
1037 int radeon_get_crtc_scanoutpos(struct radeon_device *rdev, int crtc, int *vpos, int *hpos)
1039 u32 stat_crtc = 0, vbl = 0, position = 0;
1040 int vbl_start, vbl_end, vtotal, ret = 0;
1043 if (ASIC_IS_DCE4(rdev)) {
1045 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1046 EVERGREEN_CRTC0_REGISTER_OFFSET);
1047 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1048 EVERGREEN_CRTC0_REGISTER_OFFSET);
1049 ret |= RADEON_SCANOUTPOS_VALID;
1052 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1053 EVERGREEN_CRTC1_REGISTER_OFFSET);
1054 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1055 EVERGREEN_CRTC1_REGISTER_OFFSET);
1056 ret |= RADEON_SCANOUTPOS_VALID;
1059 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1060 EVERGREEN_CRTC2_REGISTER_OFFSET);
1061 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1062 EVERGREEN_CRTC2_REGISTER_OFFSET);
1063 ret |= RADEON_SCANOUTPOS_VALID;
1066 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1067 EVERGREEN_CRTC3_REGISTER_OFFSET);
1068 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1069 EVERGREEN_CRTC3_REGISTER_OFFSET);
1070 ret |= RADEON_SCANOUTPOS_VALID;
1073 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1074 EVERGREEN_CRTC4_REGISTER_OFFSET);
1075 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1076 EVERGREEN_CRTC4_REGISTER_OFFSET);
1077 ret |= RADEON_SCANOUTPOS_VALID;
1080 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1081 EVERGREEN_CRTC5_REGISTER_OFFSET);
1082 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1083 EVERGREEN_CRTC5_REGISTER_OFFSET);
1084 ret |= RADEON_SCANOUTPOS_VALID;
1086 } else if (ASIC_IS_AVIVO(rdev)) {
1088 vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END);
1089 position = RREG32(AVIVO_D1CRTC_STATUS_POSITION);
1090 ret |= RADEON_SCANOUTPOS_VALID;
1093 vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END);
1094 position = RREG32(AVIVO_D2CRTC_STATUS_POSITION);
1095 ret |= RADEON_SCANOUTPOS_VALID;
1098 /* Pre-AVIVO: Different encoding of scanout pos and vblank interval. */
1100 /* Assume vbl_end == 0, get vbl_start from
1103 vbl = (RREG32(RADEON_CRTC_V_TOTAL_DISP) &
1104 RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
1105 /* Only retrieve vpos from upper 16 bits, set hpos == 0. */
1106 position = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
1107 stat_crtc = RREG32(RADEON_CRTC_STATUS);
1108 if (!(stat_crtc & 1))
1111 ret |= RADEON_SCANOUTPOS_VALID;
1114 vbl = (RREG32(RADEON_CRTC2_V_TOTAL_DISP) &
1115 RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
1116 position = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
1117 stat_crtc = RREG32(RADEON_CRTC2_STATUS);
1118 if (!(stat_crtc & 1))
1121 ret |= RADEON_SCANOUTPOS_VALID;
1125 /* Decode into vertical and horizontal scanout position. */
1126 *vpos = position & 0x1fff;
1127 *hpos = (position >> 16) & 0x1fff;
1129 /* Valid vblank area boundaries from gpu retrieved? */
1132 ret |= RADEON_SCANOUTPOS_ACCURATE;
1133 vbl_start = vbl & 0x1fff;
1134 vbl_end = (vbl >> 16) & 0x1fff;
1137 /* No: Fake something reasonable which gives at least ok results. */
1138 vbl_start = rdev->mode_info.crtcs[crtc]->base.mode.crtc_vdisplay;
1142 /* Test scanout position against vblank region. */
1143 if ((*vpos < vbl_start) && (*vpos >= vbl_end))
1146 /* Check if inside vblank area and apply corrective offsets:
1147 * vpos will then be >=0 in video scanout area, but negative
1148 * within vblank area, counting down the number of lines until
1152 /* Inside "upper part" of vblank area? Apply corrective offset if so: */
1153 if (in_vbl && (*vpos >= vbl_start)) {
1154 vtotal = rdev->mode_info.crtcs[crtc]->base.mode.crtc_vtotal;
1155 *vpos = *vpos - vtotal;
1158 /* Correct for shifted end of vbl at vbl_end. */
1159 *vpos = *vpos - vbl_end;
1163 ret |= RADEON_SCANOUTPOS_INVBL;