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("%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("%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_id);
90 WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK, 0x00000007);
92 WREG32(EVERGREEN_DC_LUT_RW_INDEX, 0);
93 for (i = 0; i < 256; i++) {
94 WREG32(EVERGREEN_DC_LUT_30_COLOR,
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 size)
166 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
173 /* userspace palettes are always correct as is */
174 for (i = 0; i < 256; i++) {
175 radeon_crtc->lut_r[i] = red[i] >> 6;
176 radeon_crtc->lut_g[i] = green[i] >> 6;
177 radeon_crtc->lut_b[i] = blue[i] >> 6;
179 radeon_crtc_load_lut(crtc);
182 static void radeon_crtc_destroy(struct drm_crtc *crtc)
184 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
186 drm_crtc_cleanup(crtc);
190 static const struct drm_crtc_funcs radeon_crtc_funcs = {
191 .cursor_set = radeon_crtc_cursor_set,
192 .cursor_move = radeon_crtc_cursor_move,
193 .gamma_set = radeon_crtc_gamma_set,
194 .set_config = drm_crtc_helper_set_config,
195 .destroy = radeon_crtc_destroy,
198 static void radeon_crtc_init(struct drm_device *dev, int index)
200 struct radeon_device *rdev = dev->dev_private;
201 struct radeon_crtc *radeon_crtc;
204 radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
205 if (radeon_crtc == NULL)
208 drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
210 drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
211 radeon_crtc->crtc_id = index;
212 rdev->mode_info.crtcs[index] = radeon_crtc;
215 radeon_crtc->mode_set.crtc = &radeon_crtc->base;
216 radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
217 radeon_crtc->mode_set.num_connectors = 0;
220 for (i = 0; i < 256; i++) {
221 radeon_crtc->lut_r[i] = i << 2;
222 radeon_crtc->lut_g[i] = i << 2;
223 radeon_crtc->lut_b[i] = i << 2;
226 if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
227 radeon_atombios_init_crtc(dev, radeon_crtc);
229 radeon_legacy_init_crtc(dev, radeon_crtc);
232 static const char *encoder_names[34] = {
252 "INTERNAL_KLDSCP_TMDS1",
253 "INTERNAL_KLDSCP_DVO1",
254 "INTERNAL_KLDSCP_DAC1",
255 "INTERNAL_KLDSCP_DAC2",
264 "INTERNAL_KLDSCP_LVTMA",
269 static const char *connector_names[15] = {
287 static const char *hpd_names[7] = {
297 static void radeon_print_display_setup(struct drm_device *dev)
299 struct drm_connector *connector;
300 struct radeon_connector *radeon_connector;
301 struct drm_encoder *encoder;
302 struct radeon_encoder *radeon_encoder;
306 DRM_INFO("Radeon Display Connectors\n");
307 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
308 radeon_connector = to_radeon_connector(connector);
309 DRM_INFO("Connector %d:\n", i);
310 DRM_INFO(" %s\n", connector_names[connector->connector_type]);
311 if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
312 DRM_INFO(" %s\n", hpd_names[radeon_connector->hpd.hpd]);
313 if (radeon_connector->ddc_bus) {
314 DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
315 radeon_connector->ddc_bus->rec.mask_clk_reg,
316 radeon_connector->ddc_bus->rec.mask_data_reg,
317 radeon_connector->ddc_bus->rec.a_clk_reg,
318 radeon_connector->ddc_bus->rec.a_data_reg,
319 radeon_connector->ddc_bus->rec.en_clk_reg,
320 radeon_connector->ddc_bus->rec.en_data_reg,
321 radeon_connector->ddc_bus->rec.y_clk_reg,
322 radeon_connector->ddc_bus->rec.y_data_reg);
324 if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
325 connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
326 connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
327 connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
328 connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
329 connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
330 DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
332 DRM_INFO(" Encoders:\n");
333 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
334 radeon_encoder = to_radeon_encoder(encoder);
335 devices = radeon_encoder->devices & radeon_connector->devices;
337 if (devices & ATOM_DEVICE_CRT1_SUPPORT)
338 DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
339 if (devices & ATOM_DEVICE_CRT2_SUPPORT)
340 DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
341 if (devices & ATOM_DEVICE_LCD1_SUPPORT)
342 DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
343 if (devices & ATOM_DEVICE_DFP1_SUPPORT)
344 DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
345 if (devices & ATOM_DEVICE_DFP2_SUPPORT)
346 DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
347 if (devices & ATOM_DEVICE_DFP3_SUPPORT)
348 DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
349 if (devices & ATOM_DEVICE_DFP4_SUPPORT)
350 DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
351 if (devices & ATOM_DEVICE_DFP5_SUPPORT)
352 DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
353 if (devices & ATOM_DEVICE_TV1_SUPPORT)
354 DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
355 if (devices & ATOM_DEVICE_CV_SUPPORT)
356 DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
363 static bool radeon_setup_enc_conn(struct drm_device *dev)
365 struct radeon_device *rdev = dev->dev_private;
366 struct drm_connector *drm_connector;
370 if (rdev->is_atom_bios) {
371 ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
373 ret = radeon_get_atom_connector_info_from_object_table(dev);
375 ret = radeon_get_legacy_connector_info_from_bios(dev);
377 ret = radeon_get_legacy_connector_info_from_table(dev);
380 if (!ASIC_IS_AVIVO(rdev))
381 ret = radeon_get_legacy_connector_info_from_table(dev);
384 radeon_setup_encoder_clones(dev);
385 radeon_print_display_setup(dev);
386 list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head)
387 radeon_ddc_dump(drm_connector);
393 int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
395 struct drm_device *dev = radeon_connector->base.dev;
396 struct radeon_device *rdev = dev->dev_private;
399 if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
400 (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)) {
401 struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
402 if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
403 dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) && dig->dp_i2c_bus)
404 radeon_connector->edid = drm_get_edid(&radeon_connector->base, &dig->dp_i2c_bus->adapter);
406 if (!radeon_connector->ddc_bus)
408 if (!radeon_connector->edid) {
409 radeon_connector->edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
411 /* some servers provide a hardcoded edid in rom for KVMs */
412 if (!radeon_connector->edid)
413 radeon_connector->edid = radeon_combios_get_hardcoded_edid(rdev);
414 if (radeon_connector->edid) {
415 drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
416 ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
419 drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
423 static int radeon_ddc_dump(struct drm_connector *connector)
426 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
429 if (!radeon_connector->ddc_bus)
431 edid = drm_get_edid(connector, &radeon_connector->ddc_bus->adapter);
438 static inline uint32_t radeon_div(uint64_t n, uint32_t d)
448 static void radeon_compute_pll_legacy(struct radeon_pll *pll,
450 uint32_t *dot_clock_p,
452 uint32_t *frac_fb_div_p,
454 uint32_t *post_div_p)
456 uint32_t min_ref_div = pll->min_ref_div;
457 uint32_t max_ref_div = pll->max_ref_div;
458 uint32_t min_post_div = pll->min_post_div;
459 uint32_t max_post_div = pll->max_post_div;
460 uint32_t min_fractional_feed_div = 0;
461 uint32_t max_fractional_feed_div = 0;
462 uint32_t best_vco = pll->best_vco;
463 uint32_t best_post_div = 1;
464 uint32_t best_ref_div = 1;
465 uint32_t best_feedback_div = 1;
466 uint32_t best_frac_feedback_div = 0;
467 uint32_t best_freq = -1;
468 uint32_t best_error = 0xffffffff;
469 uint32_t best_vco_diff = 1;
471 u32 pll_out_min, pll_out_max;
473 DRM_DEBUG("PLL freq %llu %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
476 if (pll->flags & RADEON_PLL_IS_LCD) {
477 pll_out_min = pll->lcd_pll_out_min;
478 pll_out_max = pll->lcd_pll_out_max;
480 pll_out_min = pll->pll_out_min;
481 pll_out_max = pll->pll_out_max;
484 if (pll->flags & RADEON_PLL_USE_REF_DIV)
485 min_ref_div = max_ref_div = pll->reference_div;
487 while (min_ref_div < max_ref_div-1) {
488 uint32_t mid = (min_ref_div + max_ref_div) / 2;
489 uint32_t pll_in = pll->reference_freq / mid;
490 if (pll_in < pll->pll_in_min)
492 else if (pll_in > pll->pll_in_max)
499 if (pll->flags & RADEON_PLL_USE_POST_DIV)
500 min_post_div = max_post_div = pll->post_div;
502 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
503 min_fractional_feed_div = pll->min_frac_feedback_div;
504 max_fractional_feed_div = pll->max_frac_feedback_div;
507 for (post_div = min_post_div; post_div <= max_post_div; ++post_div) {
510 if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
513 /* legacy radeons only have a few post_divs */
514 if (pll->flags & RADEON_PLL_LEGACY) {
515 if ((post_div == 5) ||
526 for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
527 uint32_t feedback_div, current_freq = 0, error, vco_diff;
528 uint32_t pll_in = pll->reference_freq / ref_div;
529 uint32_t min_feed_div = pll->min_feedback_div;
530 uint32_t max_feed_div = pll->max_feedback_div + 1;
532 if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
535 while (min_feed_div < max_feed_div) {
537 uint32_t min_frac_feed_div = min_fractional_feed_div;
538 uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
539 uint32_t frac_feedback_div;
542 feedback_div = (min_feed_div + max_feed_div) / 2;
544 tmp = (uint64_t)pll->reference_freq * feedback_div;
545 vco = radeon_div(tmp, ref_div);
547 if (vco < pll_out_min) {
548 min_feed_div = feedback_div + 1;
550 } else if (vco > pll_out_max) {
551 max_feed_div = feedback_div;
555 while (min_frac_feed_div < max_frac_feed_div) {
556 frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
557 tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
558 tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
559 current_freq = radeon_div(tmp, ref_div * post_div);
561 if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
562 error = freq - current_freq;
563 error = error < 0 ? 0xffffffff : error;
565 error = abs(current_freq - freq);
566 vco_diff = abs(vco - best_vco);
568 if ((best_vco == 0 && error < best_error) ||
570 (error < best_error - 100 ||
571 (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
572 best_post_div = post_div;
573 best_ref_div = ref_div;
574 best_feedback_div = feedback_div;
575 best_frac_feedback_div = frac_feedback_div;
576 best_freq = current_freq;
578 best_vco_diff = vco_diff;
579 } else if (current_freq == freq) {
580 if (best_freq == -1) {
581 best_post_div = post_div;
582 best_ref_div = ref_div;
583 best_feedback_div = feedback_div;
584 best_frac_feedback_div = frac_feedback_div;
585 best_freq = current_freq;
587 best_vco_diff = vco_diff;
588 } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
589 ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
590 ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
591 ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
592 ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
593 ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
594 best_post_div = post_div;
595 best_ref_div = ref_div;
596 best_feedback_div = feedback_div;
597 best_frac_feedback_div = frac_feedback_div;
598 best_freq = current_freq;
600 best_vco_diff = vco_diff;
603 if (current_freq < freq)
604 min_frac_feed_div = frac_feedback_div + 1;
606 max_frac_feed_div = frac_feedback_div;
608 if (current_freq < freq)
609 min_feed_div = feedback_div + 1;
611 max_feed_div = feedback_div;
616 *dot_clock_p = best_freq / 10000;
617 *fb_div_p = best_feedback_div;
618 *frac_fb_div_p = best_frac_feedback_div;
619 *ref_div_p = best_ref_div;
620 *post_div_p = best_post_div;
624 calc_fb_div(struct radeon_pll *pll,
629 uint32_t *fb_div_frac)
631 fixed20_12 feedback_divider, a, b;
634 vco_freq = freq * post_div;
635 /* feedback_divider = vco_freq * ref_div / pll->reference_freq; */
636 a.full = dfixed_const(pll->reference_freq);
637 feedback_divider.full = dfixed_const(vco_freq);
638 feedback_divider.full = dfixed_div(feedback_divider, a);
639 a.full = dfixed_const(ref_div);
640 feedback_divider.full = dfixed_mul(feedback_divider, a);
642 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
643 /* feedback_divider = floor((feedback_divider * 10.0) + 0.5) * 0.1; */
644 a.full = dfixed_const(10);
645 feedback_divider.full = dfixed_mul(feedback_divider, a);
646 feedback_divider.full += dfixed_const_half(0);
647 feedback_divider.full = dfixed_floor(feedback_divider);
648 feedback_divider.full = dfixed_div(feedback_divider, a);
650 /* *fb_div = floor(feedback_divider); */
651 a.full = dfixed_floor(feedback_divider);
652 *fb_div = dfixed_trunc(a);
653 /* *fb_div_frac = fmod(feedback_divider, 1.0) * 10.0; */
654 a.full = dfixed_const(10);
655 b.full = dfixed_mul(feedback_divider, a);
657 feedback_divider.full = dfixed_floor(feedback_divider);
658 feedback_divider.full = dfixed_mul(feedback_divider, a);
659 feedback_divider.full = b.full - feedback_divider.full;
660 *fb_div_frac = dfixed_trunc(feedback_divider);
662 /* *fb_div = floor(feedback_divider + 0.5); */
663 feedback_divider.full += dfixed_const_half(0);
664 feedback_divider.full = dfixed_floor(feedback_divider);
666 *fb_div = dfixed_trunc(feedback_divider);
670 if (((*fb_div) < pll->min_feedback_div) || ((*fb_div) > pll->max_feedback_div))
677 calc_fb_ref_div(struct radeon_pll *pll,
681 uint32_t *fb_div_frac,
684 fixed20_12 ffreq, max_error, error, pll_out, a;
686 u32 pll_out_min, pll_out_max;
688 if (pll->flags & RADEON_PLL_IS_LCD) {
689 pll_out_min = pll->lcd_pll_out_min;
690 pll_out_max = pll->lcd_pll_out_max;
692 pll_out_min = pll->pll_out_min;
693 pll_out_max = pll->pll_out_max;
696 ffreq.full = dfixed_const(freq);
697 /* max_error = ffreq * 0.0025; */
698 a.full = dfixed_const(400);
699 max_error.full = dfixed_div(ffreq, a);
701 for ((*ref_div) = pll->min_ref_div; (*ref_div) < pll->max_ref_div; ++(*ref_div)) {
702 if (calc_fb_div(pll, freq, post_div, (*ref_div), fb_div, fb_div_frac)) {
703 vco = pll->reference_freq * (((*fb_div) * 10) + (*fb_div_frac));
704 vco = vco / ((*ref_div) * 10);
706 if ((vco < pll_out_min) || (vco > pll_out_max))
709 /* pll_out = vco / post_div; */
710 a.full = dfixed_const(post_div);
711 pll_out.full = dfixed_const(vco);
712 pll_out.full = dfixed_div(pll_out, a);
714 if (pll_out.full >= ffreq.full) {
715 error.full = pll_out.full - ffreq.full;
716 if (error.full <= max_error.full)
724 static void radeon_compute_pll_new(struct radeon_pll *pll,
726 uint32_t *dot_clock_p,
728 uint32_t *frac_fb_div_p,
730 uint32_t *post_div_p)
732 u32 fb_div = 0, fb_div_frac = 0, post_div = 0, ref_div = 0;
733 u32 best_freq = 0, vco_frequency;
734 u32 pll_out_min, pll_out_max;
736 if (pll->flags & RADEON_PLL_IS_LCD) {
737 pll_out_min = pll->lcd_pll_out_min;
738 pll_out_max = pll->lcd_pll_out_max;
740 pll_out_min = pll->pll_out_min;
741 pll_out_max = pll->pll_out_max;
744 /* freq = freq / 10; */
747 if (pll->flags & RADEON_PLL_USE_POST_DIV) {
748 post_div = pll->post_div;
749 if ((post_div < pll->min_post_div) || (post_div > pll->max_post_div))
752 vco_frequency = freq * post_div;
753 if ((vco_frequency < pll_out_min) || (vco_frequency > pll_out_max))
756 if (pll->flags & RADEON_PLL_USE_REF_DIV) {
757 ref_div = pll->reference_div;
758 if ((ref_div < pll->min_ref_div) || (ref_div > pll->max_ref_div))
760 if (!calc_fb_div(pll, freq, post_div, ref_div, &fb_div, &fb_div_frac))
764 for (post_div = pll->max_post_div; post_div >= pll->min_post_div; --post_div) {
765 if (pll->flags & RADEON_PLL_LEGACY) {
766 if ((post_div == 5) ||
774 if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
777 vco_frequency = freq * post_div;
778 if ((vco_frequency < pll_out_min) || (vco_frequency > pll_out_max))
780 if (pll->flags & RADEON_PLL_USE_REF_DIV) {
781 ref_div = pll->reference_div;
782 if ((ref_div < pll->min_ref_div) || (ref_div > pll->max_ref_div))
784 if (calc_fb_div(pll, freq, post_div, ref_div, &fb_div, &fb_div_frac))
787 if (calc_fb_ref_div(pll, freq, post_div, &fb_div, &fb_div_frac, &ref_div))
793 best_freq = pll->reference_freq * 10 * fb_div;
794 best_freq += pll->reference_freq * fb_div_frac;
795 best_freq = best_freq / (ref_div * post_div);
799 DRM_ERROR("Couldn't find valid PLL dividers\n");
801 *dot_clock_p = best_freq / 10;
803 *frac_fb_div_p = fb_div_frac;
804 *ref_div_p = ref_div;
805 *post_div_p = post_div;
807 DRM_DEBUG("%u %d.%d, %d, %d\n", *dot_clock_p, *fb_div_p, *frac_fb_div_p, *ref_div_p, *post_div_p);
810 void radeon_compute_pll(struct radeon_pll *pll,
812 uint32_t *dot_clock_p,
814 uint32_t *frac_fb_div_p,
816 uint32_t *post_div_p)
820 radeon_compute_pll_new(pll, freq, dot_clock_p, fb_div_p,
821 frac_fb_div_p, ref_div_p, post_div_p);
823 case PLL_ALGO_LEGACY:
825 radeon_compute_pll_legacy(pll, freq, dot_clock_p, fb_div_p,
826 frac_fb_div_p, ref_div_p, post_div_p);
831 static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
833 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
836 drm_gem_object_unreference_unlocked(radeon_fb->obj);
837 drm_framebuffer_cleanup(fb);
841 static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
842 struct drm_file *file_priv,
843 unsigned int *handle)
845 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
847 return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
850 static const struct drm_framebuffer_funcs radeon_fb_funcs = {
851 .destroy = radeon_user_framebuffer_destroy,
852 .create_handle = radeon_user_framebuffer_create_handle,
856 radeon_framebuffer_init(struct drm_device *dev,
857 struct radeon_framebuffer *rfb,
858 struct drm_mode_fb_cmd *mode_cmd,
859 struct drm_gem_object *obj)
862 drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
863 drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
866 static struct drm_framebuffer *
867 radeon_user_framebuffer_create(struct drm_device *dev,
868 struct drm_file *file_priv,
869 struct drm_mode_fb_cmd *mode_cmd)
871 struct drm_gem_object *obj;
872 struct radeon_framebuffer *radeon_fb;
874 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
876 dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
877 "can't create framebuffer\n", mode_cmd->handle);
881 radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
882 if (radeon_fb == NULL) {
886 radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
888 return &radeon_fb->base;
891 static void radeon_output_poll_changed(struct drm_device *dev)
893 struct radeon_device *rdev = dev->dev_private;
894 radeon_fb_output_poll_changed(rdev);
897 static const struct drm_mode_config_funcs radeon_mode_funcs = {
898 .fb_create = radeon_user_framebuffer_create,
899 .output_poll_changed = radeon_output_poll_changed
902 struct drm_prop_enum_list {
907 static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
912 static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
913 { { TV_STD_NTSC, "ntsc" },
914 { TV_STD_PAL, "pal" },
915 { TV_STD_PAL_M, "pal-m" },
916 { TV_STD_PAL_60, "pal-60" },
917 { TV_STD_NTSC_J, "ntsc-j" },
918 { TV_STD_SCART_PAL, "scart-pal" },
919 { TV_STD_PAL_CN, "pal-cn" },
920 { TV_STD_SECAM, "secam" },
923 static int radeon_modeset_create_props(struct radeon_device *rdev)
927 if (rdev->is_atom_bios) {
928 rdev->mode_info.coherent_mode_property =
929 drm_property_create(rdev->ddev,
932 if (!rdev->mode_info.coherent_mode_property)
935 rdev->mode_info.coherent_mode_property->values[0] = 0;
936 rdev->mode_info.coherent_mode_property->values[1] = 1;
939 if (!ASIC_IS_AVIVO(rdev)) {
940 sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
941 rdev->mode_info.tmds_pll_property =
942 drm_property_create(rdev->ddev,
945 for (i = 0; i < sz; i++) {
946 drm_property_add_enum(rdev->mode_info.tmds_pll_property,
948 radeon_tmds_pll_enum_list[i].type,
949 radeon_tmds_pll_enum_list[i].name);
953 rdev->mode_info.load_detect_property =
954 drm_property_create(rdev->ddev,
956 "load detection", 2);
957 if (!rdev->mode_info.load_detect_property)
959 rdev->mode_info.load_detect_property->values[0] = 0;
960 rdev->mode_info.load_detect_property->values[1] = 1;
962 drm_mode_create_scaling_mode_property(rdev->ddev);
964 sz = ARRAY_SIZE(radeon_tv_std_enum_list);
965 rdev->mode_info.tv_std_property =
966 drm_property_create(rdev->ddev,
969 for (i = 0; i < sz; i++) {
970 drm_property_add_enum(rdev->mode_info.tv_std_property,
972 radeon_tv_std_enum_list[i].type,
973 radeon_tv_std_enum_list[i].name);
979 void radeon_update_display_priority(struct radeon_device *rdev)
981 /* adjustment options for the display watermarks */
982 if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
983 /* set display priority to high for r3xx, rv515 chips
984 * this avoids flickering due to underflow to the
985 * display controllers during heavy acceleration.
987 if (ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515))
988 rdev->disp_priority = 2;
990 rdev->disp_priority = 0;
992 rdev->disp_priority = radeon_disp_priority;
996 int radeon_modeset_init(struct radeon_device *rdev)
1001 drm_mode_config_init(rdev->ddev);
1002 rdev->mode_info.mode_config_initialized = true;
1004 rdev->ddev->mode_config.funcs = (void *)&radeon_mode_funcs;
1006 if (ASIC_IS_AVIVO(rdev)) {
1007 rdev->ddev->mode_config.max_width = 8192;
1008 rdev->ddev->mode_config.max_height = 8192;
1010 rdev->ddev->mode_config.max_width = 4096;
1011 rdev->ddev->mode_config.max_height = 4096;
1014 rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
1016 ret = radeon_modeset_create_props(rdev);
1021 /* check combios for a valid hardcoded EDID - Sun servers */
1022 if (!rdev->is_atom_bios) {
1023 /* check for hardcoded EDID in BIOS */
1024 radeon_combios_check_hardcoded_edid(rdev);
1027 /* allocate crtcs */
1028 for (i = 0; i < rdev->num_crtc; i++) {
1029 radeon_crtc_init(rdev->ddev, i);
1032 /* okay we should have all the bios connectors */
1033 ret = radeon_setup_enc_conn(rdev->ddev);
1037 /* initialize hpd */
1038 radeon_hpd_init(rdev);
1040 /* Initialize power management */
1041 radeon_pm_init(rdev);
1043 radeon_fbdev_init(rdev);
1044 drm_kms_helper_poll_init(rdev->ddev);
1049 void radeon_modeset_fini(struct radeon_device *rdev)
1051 radeon_fbdev_fini(rdev);
1052 kfree(rdev->mode_info.bios_hardcoded_edid);
1053 radeon_pm_fini(rdev);
1055 if (rdev->mode_info.mode_config_initialized) {
1056 drm_kms_helper_poll_fini(rdev->ddev);
1057 radeon_hpd_fini(rdev);
1058 drm_mode_config_cleanup(rdev->ddev);
1059 rdev->mode_info.mode_config_initialized = false;
1063 bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
1064 struct drm_display_mode *mode,
1065 struct drm_display_mode *adjusted_mode)
1067 struct drm_device *dev = crtc->dev;
1068 struct drm_encoder *encoder;
1069 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1070 struct radeon_encoder *radeon_encoder;
1073 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1074 radeon_encoder = to_radeon_encoder(encoder);
1075 if (encoder->crtc != crtc)
1079 if (radeon_encoder->rmx_type == RMX_OFF)
1080 radeon_crtc->rmx_type = RMX_OFF;
1081 else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
1082 mode->vdisplay < radeon_encoder->native_mode.vdisplay)
1083 radeon_crtc->rmx_type = radeon_encoder->rmx_type;
1085 radeon_crtc->rmx_type = RMX_OFF;
1086 /* copy native mode */
1087 memcpy(&radeon_crtc->native_mode,
1088 &radeon_encoder->native_mode,
1089 sizeof(struct drm_display_mode));
1092 if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
1093 /* WARNING: Right now this can't happen but
1094 * in the future we need to check that scaling
1095 * are consistent accross different encoder
1096 * (ie all encoder can work with the same
1099 DRM_ERROR("Scaling not consistent accross encoder.\n");
1104 if (radeon_crtc->rmx_type != RMX_OFF) {
1106 a.full = dfixed_const(crtc->mode.vdisplay);
1107 b.full = dfixed_const(radeon_crtc->native_mode.hdisplay);
1108 radeon_crtc->vsc.full = dfixed_div(a, b);
1109 a.full = dfixed_const(crtc->mode.hdisplay);
1110 b.full = dfixed_const(radeon_crtc->native_mode.vdisplay);
1111 radeon_crtc->hsc.full = dfixed_div(a, b);
1113 radeon_crtc->vsc.full = dfixed_const(1);
1114 radeon_crtc->hsc.full = dfixed_const(1);