31 #include <asm/div64.h>
43 DRM_DEBUG_KMS(
"%d\n", radeon_crtc->
crtc_id);
59 for (i = 0; i < 256; i++) {
61 (radeon_crtc->
lut_r[i] << 20) |
62 (radeon_crtc->
lut_g[i] << 10) |
63 (radeon_crtc->
lut_b[i] << 0));
69 static void dce4_crtc_load_lut(
struct drm_crtc *crtc)
76 DRM_DEBUG_KMS(
"%d\n", radeon_crtc->
crtc_id);
91 for (i = 0; i < 256; i++) {
93 (radeon_crtc->
lut_r[i] << 20) |
94 (radeon_crtc->
lut_g[i] << 10) |
95 (radeon_crtc->
lut_b[i] << 0));
99 static void dce5_crtc_load_lut(
struct drm_crtc *crtc)
106 DRM_DEBUG_KMS(
"%d\n", radeon_crtc->
crtc_id);
133 for (i = 0; i < 256; i++) {
135 (radeon_crtc->
lut_r[i] << 20) |
136 (radeon_crtc->
lut_g[i] << 10) |
137 (radeon_crtc->
lut_b[i] << 0));
159 static void legacy_crtc_load_lut(
struct drm_crtc *crtc)
175 for (i = 0; i < 256; i++) {
177 (radeon_crtc->
lut_r[i] << 20) |
178 (radeon_crtc->
lut_g[i] << 10) |
179 (radeon_crtc->
lut_b[i] << 0));
192 dce5_crtc_load_lut(crtc);
194 dce4_crtc_load_lut(crtc);
196 avivo_crtc_load_lut(crtc);
198 legacy_crtc_load_lut(crtc);
207 radeon_crtc->
lut_r[regno] = red >> 6;
208 radeon_crtc->
lut_g[regno] = green >> 6;
209 radeon_crtc->
lut_b[regno] = blue >> 6;
218 *red = radeon_crtc->
lut_r[regno] << 6;
219 *green = radeon_crtc->
lut_g[regno] << 6;
220 *blue = radeon_crtc->
lut_b[regno] << 6;
227 int end = (start + size > 256) ? 256 : start + size, i;
230 for (i = start; i <
end; i++) {
231 radeon_crtc->
lut_r[
i] = red[
i] >> 6;
232 radeon_crtc->
lut_g[
i] = green[
i] >> 6;
233 radeon_crtc->
lut_b[
i] = blue[
i] >> 6;
238 static void radeon_crtc_destroy(
struct drm_crtc *crtc)
249 static void radeon_unpin_work_func(
struct work_struct *__work)
260 DRM_ERROR(
"failed to unpin buffer after flip\n");
262 radeon_bo_unreserve(work->
old_rbo);
264 DRM_ERROR(
"failed to reserve buffer after flip\n");
266 drm_gem_object_unreference_unlocked(&work->
old_rbo->gem_base);
274 struct drm_pending_vblank_event *
e;
284 spin_unlock_irqrestore(&rdev->
ddev->event_lock, flags);
303 if (update_pending &&
306 ((vpos >= (99 * rdev->
mode_info.crtcs[crtc_id]->base.hwmode.crtc_vdisplay)/100) ||
316 if (update_pending) {
323 spin_unlock_irqrestore(&rdev->
ddev->event_lock, flags);
334 e->event.tv_sec = now.
tv_sec;
335 e->event.tv_usec = now.
tv_usec;
336 list_add_tail(&e->base.link, &e->base.file_priv->event_list);
339 spin_unlock_irqrestore(&rdev->
ddev->event_lock, flags);
347 static int radeon_crtc_page_flip(
struct drm_crtc *crtc,
349 struct drm_pending_vblank_event *
event)
356 struct drm_gem_object *obj;
360 u32 tiling_flags, pitch_pixels;
374 obj = old_radeon_fb->
obj;
376 drm_gem_object_reference(obj);
379 obj = new_radeon_fb->
obj;
381 if (rbo->
tbo.sync_obj)
388 DRM_DEBUG_DRIVER(
"flip queue: crtc already busy\n");
394 spin_unlock_irqrestore(&dev->event_lock, flags);
397 DRM_DEBUG_DRIVER(
"flip-ioctl() cur_fbo = %p, cur_bbo = %p\n",
402 DRM_ERROR(
"failed to reserve new rbo buffer before flip\n");
409 radeon_bo_unreserve(rbo);
411 DRM_ERROR(
"failed to pin new rbo buffer before flip\n");
415 radeon_bo_unreserve(rbo);
427 int tile_addr = (((crtc->
y >> 3) * pitch_pixels + crtc->
x) >> (8 - byteshift)) << 11;
428 base += tile_addr + ((crtc->
x << byteshift) % 256) + ((crtc->
y % 8) << 8);
431 int offset = crtc->
y * pitch_pixels + crtc->
x;
455 spin_unlock_irqrestore(&dev->event_lock, flags);
462 DRM_ERROR(
"failed to get vblank before flip\n");
473 DRM_ERROR(
"failed to reserve new rbo in error path\n");
477 DRM_ERROR(
"failed to unpin new rbo in error path\n");
479 radeon_bo_unreserve(rbo);
485 spin_unlock_irqrestore(&dev->event_lock, flags);
486 drm_gem_object_unreference_unlocked(old_radeon_fb->
obj);
496 .gamma_set = radeon_crtc_gamma_set,
498 .destroy = radeon_crtc_destroy,
499 .page_flip = radeon_crtc_page_flip,
505 struct radeon_crtc *radeon_crtc;
509 if (radeon_crtc ==
NULL)
519 radeon_crtc->mode_set.crtc = &radeon_crtc->
base;
520 radeon_crtc->mode_set.connectors = (
struct drm_connector **)(radeon_crtc + 1);
521 radeon_crtc->mode_set.num_connectors = 0;
524 for (i = 0; i < 256; i++) {
525 radeon_crtc->
lut_r[
i] = i << 2;
526 radeon_crtc->
lut_g[
i] = i << 2;
527 radeon_crtc->
lut_b[
i] = i << 2;
536 static const char *encoder_names[37] = {
556 "INTERNAL_KLDSCP_TMDS1",
557 "INTERNAL_KLDSCP_DVO1",
558 "INTERNAL_KLDSCP_DAC1",
559 "INTERNAL_KLDSCP_DAC2",
568 "INTERNAL_KLDSCP_LVTMA",
576 static const char *hpd_names[6] = {
585 static void radeon_print_display_setup(
struct drm_device *dev)
594 DRM_INFO(
"Radeon Display Connectors\n");
597 DRM_INFO(
"Connector %d:\n", i);
600 DRM_INFO(
" %s\n", hpd_names[radeon_connector->
hpd.hpd]);
601 if (radeon_connector->
ddc_bus) {
602 DRM_INFO(
" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
603 radeon_connector->
ddc_bus->rec.mask_clk_reg,
604 radeon_connector->
ddc_bus->rec.mask_data_reg,
605 radeon_connector->
ddc_bus->rec.a_clk_reg,
606 radeon_connector->
ddc_bus->rec.a_data_reg,
607 radeon_connector->
ddc_bus->rec.en_clk_reg,
608 radeon_connector->
ddc_bus->rec.en_data_reg,
609 radeon_connector->
ddc_bus->rec.y_clk_reg,
610 radeon_connector->
ddc_bus->rec.y_data_reg);
611 if (radeon_connector->
router.ddc_valid)
612 DRM_INFO(
" DDC Router 0x%x/0x%x\n",
613 radeon_connector->
router.ddc_mux_control_pin,
614 radeon_connector->
router.ddc_mux_state);
615 if (radeon_connector->
router.cd_valid)
616 DRM_INFO(
" Clock/Data Router 0x%x/0x%x\n",
617 radeon_connector->
router.cd_mux_control_pin,
618 radeon_connector->
router.cd_mux_state);
626 DRM_INFO(
" DDC: no ddc bus - possible BIOS bug - please report to [email protected]\n");
628 DRM_INFO(
" Encoders:\n");
634 DRM_INFO(
" CRT1: %s\n", encoder_names[radeon_encoder->
encoder_id]);
636 DRM_INFO(
" CRT2: %s\n", encoder_names[radeon_encoder->
encoder_id]);
638 DRM_INFO(
" LCD1: %s\n", encoder_names[radeon_encoder->
encoder_id]);
640 DRM_INFO(
" DFP1: %s\n", encoder_names[radeon_encoder->
encoder_id]);
642 DRM_INFO(
" DFP2: %s\n", encoder_names[radeon_encoder->
encoder_id]);
644 DRM_INFO(
" DFP3: %s\n", encoder_names[radeon_encoder->
encoder_id]);
646 DRM_INFO(
" DFP4: %s\n", encoder_names[radeon_encoder->
encoder_id]);
648 DRM_INFO(
" DFP5: %s\n", encoder_names[radeon_encoder->
encoder_id]);
650 DRM_INFO(
" DFP6: %s\n", encoder_names[radeon_encoder->
encoder_id]);
652 DRM_INFO(
" TV1: %s\n", encoder_names[radeon_encoder->
encoder_id]);
654 DRM_INFO(
" CV: %s\n", encoder_names[radeon_encoder->
encoder_id]);
661 static bool radeon_setup_enc_conn(
struct drm_device *dev)
682 radeon_print_display_setup(dev);
695 if (radeon_connector->
router.ddc_valid)
708 else if (radeon_connector->
ddc_bus && !radeon_connector->
edid)
710 &radeon_connector->
ddc_bus->adapter);
712 if (radeon_connector->
ddc_bus && !radeon_connector->
edid)
714 &radeon_connector->
ddc_bus->adapter);
717 if (!radeon_connector->
edid) {
727 if (radeon_connector->
edid) {
744 u32 tmp = post_div * ref_div;
752 else if (*fb_div < pll->min_feedback_div)
776 post_div = vco / target_clock;
777 tmp = vco % target_clock;
789 else if (post_div < pll->min_post_div)
795 #define MAX_TOLERANCE 10
805 u32 target_clock = freq / 10;
806 u32 post_div = avivo_get_post_div(pll, target_clock);
808 u32 fb_div = 0, frac_fb_div = 0,
tmp;
814 avivo_get_fb_div(pll, target_clock, post_div, ref_div, &fb_div, &frac_fb_div);
816 if (frac_fb_div >= 5) {
818 frac_fb_div = frac_fb_div / 10;
821 if (frac_fb_div >= 10) {
826 while (ref_div <= pll->max_ref_div) {
827 avivo_get_fb_div(pll, target_clock, post_div, ref_div,
828 &fb_div, &frac_fb_div);
833 tmp = (tmp * 10000) / target_clock;
845 (ref_div * post_div * 10);
847 *frac_fb_div_p = frac_fb_div;
848 *ref_div_p = ref_div;
849 *post_div_p = post_div;
850 DRM_DEBUG_KMS(
"%d, pll dividers - fb: %d.%d ref: %d, post %d\n",
851 *dot_clock_p, fb_div, frac_fb_div, ref_div, post_div);
877 uint32_t min_fractional_feed_div = 0;
878 uint32_t max_fractional_feed_div = 0;
883 uint32_t best_frac_feedback_div = 0;
888 u32 pll_out_min, pll_out_max;
901 if (pll_out_min > 64800)
907 while (min_ref_div < max_ref_div-1) {
910 if (pll_in < pll->pll_in_min)
920 min_post_div = max_post_div = pll->
post_div;
927 for (post_div = max_post_div; post_div >= min_post_div; --post_div) {
935 if ((post_div == 5) ||
946 for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
952 if (pll_in < pll->pll_in_min || pll_in > pll->
pll_in_max)
955 while (min_feed_div < max_feed_div) {
957 uint32_t min_frac_feed_div = min_fractional_feed_div;
958 uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
962 feedback_div = (min_feed_div + max_feed_div) / 2;
965 vco = radeon_div(tmp, ref_div);
967 if (vco < pll_out_min) {
968 min_feed_div = feedback_div + 1;
970 }
else if (vco > pll_out_max) {
971 max_feed_div = feedback_div;
975 while (min_frac_feed_div < max_frac_feed_div) {
976 frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
979 current_freq = radeon_div(tmp, ref_div * post_div);
982 if (freq < current_freq)
985 error = freq - current_freq;
988 vco_diff =
abs(vco - best_vco);
990 if ((best_vco == 0 &&
error < best_error) ||
992 ((best_error > 100 &&
error < best_error - 100) ||
993 (
abs(
error - best_error) < 100 && vco_diff < best_vco_diff)))) {
994 best_post_div = post_div;
995 best_ref_div = ref_div;
996 best_feedback_div = feedback_div;
997 best_frac_feedback_div = frac_feedback_div;
998 best_freq = current_freq;
1000 best_vco_diff = vco_diff;
1001 }
else if (current_freq == freq) {
1002 if (best_freq == -1) {
1003 best_post_div = post_div;
1004 best_ref_div = ref_div;
1005 best_feedback_div = feedback_div;
1006 best_frac_feedback_div = frac_feedback_div;
1007 best_freq = current_freq;
1009 best_vco_diff = vco_diff;
1016 best_post_div = post_div;
1017 best_ref_div = ref_div;
1018 best_feedback_div = feedback_div;
1019 best_frac_feedback_div = frac_feedback_div;
1020 best_freq = current_freq;
1022 best_vco_diff = vco_diff;
1025 if (current_freq < freq)
1026 min_frac_feed_div = frac_feedback_div + 1;
1028 max_frac_feed_div = frac_feedback_div;
1030 if (current_freq < freq)
1031 min_feed_div = feedback_div + 1;
1033 max_feed_div = feedback_div;
1038 *dot_clock_p = best_freq / 10000;
1039 *fb_div_p = best_feedback_div;
1040 *frac_fb_div_p = best_frac_feedback_div;
1041 *ref_div_p = best_ref_div;
1042 *post_div_p = best_post_div;
1043 DRM_DEBUG_KMS(
"%lld %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
1045 best_freq / 1000, best_feedback_div, best_frac_feedback_div,
1046 best_ref_div, best_post_div);
1050 static void radeon_user_framebuffer_destroy(
struct drm_framebuffer *fb)
1054 if (radeon_fb->
obj) {
1055 drm_gem_object_unreference_unlocked(radeon_fb->
obj);
1061 static int radeon_user_framebuffer_create_handle(
struct drm_framebuffer *fb,
1062 struct drm_file *file_priv,
1071 .destroy = radeon_user_framebuffer_destroy,
1072 .create_handle = radeon_user_framebuffer_create_handle,
1079 struct drm_gem_object *obj)
1093 radeon_user_framebuffer_create(
struct drm_device *dev,
1094 struct drm_file *file_priv,
1097 struct drm_gem_object *obj;
1103 dev_err(&dev->pdev->dev,
"No GEM object associated to handle 0x%08X, "
1104 "can't create framebuffer\n", mode_cmd->
handles[0]);
1108 radeon_fb = kzalloc(
sizeof(*radeon_fb),
GFP_KERNEL);
1109 if (radeon_fb ==
NULL)
1115 drm_gem_object_unreference_unlocked(obj);
1119 return &radeon_fb->
base;
1122 static void radeon_output_poll_changed(
struct drm_device *dev)
1129 .fb_create = radeon_user_framebuffer_create,
1130 .output_poll_changed = radeon_output_poll_changed
1155 static int radeon_modeset_create_props(
struct radeon_device *rdev)
1160 rdev->
mode_info.coherent_mode_property =
1162 if (!rdev->
mode_info.coherent_mode_property)
1171 radeon_tmds_pll_enum_list, sz);
1176 if (!rdev->
mode_info.load_detect_property)
1185 radeon_tv_std_enum_list, sz);
1191 radeon_underscan_enum_list, sz);
1193 rdev->
mode_info.underscan_hborder_property =
1195 "underscan hborder", 0, 128);
1196 if (!rdev->
mode_info.underscan_hborder_property)
1199 rdev->
mode_info.underscan_vborder_property =
1201 "underscan vborder", 0, 128);
1202 if (!rdev->
mode_info.underscan_vborder_property)
1321 rdev->
mode_info.mode_config_initialized =
true;
1323 rdev->
ddev->mode_config.funcs = &radeon_mode_funcs;
1326 rdev->
ddev->mode_config.max_width = 16384;
1327 rdev->
ddev->mode_config.max_height = 16384;
1329 rdev->
ddev->mode_config.max_width = 8192;
1330 rdev->
ddev->mode_config.max_height = 8192;
1332 rdev->
ddev->mode_config.max_width = 4096;
1333 rdev->
ddev->mode_config.max_height = 4096;
1336 rdev->
ddev->mode_config.preferred_depth = 24;
1337 rdev->
ddev->mode_config.prefer_shadow = 1;
1339 rdev->
ddev->mode_config.fb_base = rdev->
mc.aper_base;
1341 ret = radeon_modeset_create_props(rdev);
1356 for (i = 0; i < rdev->
num_crtc; i++) {
1357 radeon_crtc_init(rdev->
ddev, i);
1361 ret = radeon_setup_enc_conn(rdev->
ddev);
1376 radeon_afmt_init(rdev);
1393 if (rdev->
mode_info.mode_config_initialized) {
1394 radeon_afmt_fini(rdev);
1398 rdev->
mode_info.mode_config_initialized =
false;
1424 struct radeon_encoder *radeon_encoder;
1426 struct radeon_connector *radeon_connector;
1428 u32 src_v = 1, dst_v = 1;
1429 u32 src_h = 1, dst_h = 1;
1435 if (encoder->
crtc != crtc)
1454 src_v = crtc->
mode.vdisplay;
1456 src_h = crtc->
mode.hdisplay;
1465 is_hdtv_mode(mode)))) {
1475 src_v = crtc->
mode.vdisplay;
1476 dst_v = crtc->
mode.vdisplay - (radeon_crtc->
v_border * 2);
1477 src_h = crtc->
mode.hdisplay;
1478 dst_h = crtc->
mode.hdisplay - (radeon_crtc->
h_border * 2);
1489 DRM_ERROR(
"Scaling not consistent across encoder.\n");
1498 radeon_crtc->
vsc.
full = dfixed_div(a, b);
1501 radeon_crtc->
hsc.
full = dfixed_div(a, b);
1533 u32 stat_crtc = 0, vbl = 0, position = 0;
1534 int vbl_start, vbl_end, vtotal, ret = 0;
1545 ret |= DRM_SCANOUTPOS_VALID;
1552 ret |= DRM_SCANOUTPOS_VALID;
1559 ret |= DRM_SCANOUTPOS_VALID;
1566 ret |= DRM_SCANOUTPOS_VALID;
1573 ret |= DRM_SCANOUTPOS_VALID;
1580 ret |= DRM_SCANOUTPOS_VALID;
1586 ret |= DRM_SCANOUTPOS_VALID;
1591 ret |= DRM_SCANOUTPOS_VALID;
1604 if (!(stat_crtc & 1))
1607 ret |= DRM_SCANOUTPOS_VALID;
1614 if (!(stat_crtc & 1))
1617 ret |= DRM_SCANOUTPOS_VALID;
1622 *vpos = position & 0x1fff;
1623 *hpos = (position >> 16) & 0x1fff;
1628 ret |= DRM_SCANOUTPOS_ACCURATE;
1629 vbl_start = vbl & 0x1fff;
1630 vbl_end = (vbl >> 16) & 0x1fff;
1634 vbl_start = rdev->
mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
1639 if ((*vpos < vbl_start) && (*vpos >= vbl_end))
1649 if (in_vbl && (*vpos >= vbl_start)) {
1650 vtotal = rdev->
mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
1651 *vpos = *vpos - vtotal;
1655 *vpos = *vpos - vbl_end;
1659 ret |= DRM_SCANOUTPOS_INVBL;