Subversion Repositories Kolibri OS

Rev

Rev 2004 | Rev 3192 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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.  
  30. #include "atom.h"
  31. #include <asm/div64.h>
  32.  
  33. #include "drm_crtc_helper.h"
  34. #include "drm_edid.h"
  35.  
  36. static int radeon_ddc_dump(struct drm_connector *connector);
  37.  
  38. static void avivo_crtc_load_lut(struct drm_crtc *crtc)
  39. {
  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;
  43.         int i;
  44.  
  45.         DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
  46.         WREG32(AVIVO_DC_LUTA_CONTROL + radeon_crtc->crtc_offset, 0);
  47.  
  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);
  51.  
  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);
  55.  
  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);
  59.  
  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));
  66.         }
  67.  
  68.         WREG32(AVIVO_D1GRPH_LUT_SEL + radeon_crtc->crtc_offset, radeon_crtc->crtc_id);
  69. }
  70.  
  71. static void dce4_crtc_load_lut(struct drm_crtc *crtc)
  72. {
  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;
  76.         int i;
  77.  
  78.         DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
  79.         WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
  80.  
  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);
  84.  
  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);
  88.  
  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);
  91.  
  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));
  98.         }
  99. }
  100.  
  101. static void dce5_crtc_load_lut(struct drm_crtc *crtc)
  102. {
  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;
  106.         int i;
  107.  
  108.         DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
  109.  
  110.         WREG32(NI_INPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
  111.                (NI_INPUT_CSC_GRPH_MODE(NI_INPUT_CSC_BYPASS) |
  112.                 NI_INPUT_CSC_OVL_MODE(NI_INPUT_CSC_BYPASS)));
  113.         WREG32(NI_PRESCALE_GRPH_CONTROL + radeon_crtc->crtc_offset,
  114.                NI_GRPH_PRESCALE_BYPASS);
  115.         WREG32(NI_PRESCALE_OVL_CONTROL + radeon_crtc->crtc_offset,
  116.                NI_OVL_PRESCALE_BYPASS);
  117.         WREG32(NI_INPUT_GAMMA_CONTROL + radeon_crtc->crtc_offset,
  118.                (NI_GRPH_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT) |
  119.                 NI_OVL_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT)));
  120.  
  121.         WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
  122.  
  123.         WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
  124.         WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
  125.         WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
  126.  
  127.         WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
  128.         WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
  129.         WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
  130.  
  131.         WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
  132.         WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
  133.  
  134.         WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
  135.         for (i = 0; i < 256; i++) {
  136.                 WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
  137.                        (radeon_crtc->lut_r[i] << 20) |
  138.                        (radeon_crtc->lut_g[i] << 10) |
  139.                        (radeon_crtc->lut_b[i] << 0));
  140.         }
  141.  
  142.         WREG32(NI_DEGAMMA_CONTROL + radeon_crtc->crtc_offset,
  143.                (NI_GRPH_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
  144.                 NI_OVL_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
  145.                 NI_ICON_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
  146.                 NI_CURSOR_DEGAMMA_MODE(NI_DEGAMMA_BYPASS)));
  147.         WREG32(NI_GAMUT_REMAP_CONTROL + radeon_crtc->crtc_offset,
  148.                (NI_GRPH_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS) |
  149.                 NI_OVL_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS)));
  150.         WREG32(NI_REGAMMA_CONTROL + radeon_crtc->crtc_offset,
  151.                (NI_GRPH_REGAMMA_MODE(NI_REGAMMA_BYPASS) |
  152.                 NI_OVL_REGAMMA_MODE(NI_REGAMMA_BYPASS)));
  153.         WREG32(NI_OUTPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
  154.                (NI_OUTPUT_CSC_GRPH_MODE(NI_OUTPUT_CSC_BYPASS) |
  155.                 NI_OUTPUT_CSC_OVL_MODE(NI_OUTPUT_CSC_BYPASS)));
  156.         /* XXX match this to the depth of the crtc fmt block, move to modeset? */
  157.         WREG32(0x6940 + radeon_crtc->crtc_offset, 0);
  158.  
  159. }
  160.  
  161. static void legacy_crtc_load_lut(struct drm_crtc *crtc)
  162. {
  163.         struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  164.         struct drm_device *dev = crtc->dev;
  165.         struct radeon_device *rdev = dev->dev_private;
  166.         int i;
  167.         uint32_t dac2_cntl;
  168.  
  169.         dac2_cntl = RREG32(RADEON_DAC_CNTL2);
  170.         if (radeon_crtc->crtc_id == 0)
  171.                 dac2_cntl &= (uint32_t)~RADEON_DAC2_PALETTE_ACC_CTL;
  172.         else
  173.                 dac2_cntl |= RADEON_DAC2_PALETTE_ACC_CTL;
  174.         WREG32(RADEON_DAC_CNTL2, dac2_cntl);
  175.  
  176.         WREG8(RADEON_PALETTE_INDEX, 0);
  177.         for (i = 0; i < 256; i++) {
  178.                 WREG32(RADEON_PALETTE_30_DATA,
  179.                              (radeon_crtc->lut_r[i] << 20) |
  180.                              (radeon_crtc->lut_g[i] << 10) |
  181.                              (radeon_crtc->lut_b[i] << 0));
  182.         }
  183. }
  184.  
  185. void radeon_crtc_load_lut(struct drm_crtc *crtc)
  186. {
  187.         struct drm_device *dev = crtc->dev;
  188.         struct radeon_device *rdev = dev->dev_private;
  189.  
  190.         if (!crtc->enabled)
  191.                 return;
  192.  
  193.         if (ASIC_IS_DCE5(rdev))
  194.                 dce5_crtc_load_lut(crtc);
  195.         else if (ASIC_IS_DCE4(rdev))
  196.                 dce4_crtc_load_lut(crtc);
  197.         else if (ASIC_IS_AVIVO(rdev))
  198.                 avivo_crtc_load_lut(crtc);
  199.         else
  200.                 legacy_crtc_load_lut(crtc);
  201. }
  202.  
  203. /** Sets the color ramps on behalf of fbcon */
  204. void radeon_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  205.                               u16 blue, int regno)
  206. {
  207.         struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  208.  
  209.         radeon_crtc->lut_r[regno] = red >> 6;
  210.         radeon_crtc->lut_g[regno] = green >> 6;
  211.         radeon_crtc->lut_b[regno] = blue >> 6;
  212. }
  213.  
  214. /** Gets the color ramps on behalf of fbcon */
  215. void radeon_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  216.                               u16 *blue, int regno)
  217. {
  218.         struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  219.  
  220.         *red = radeon_crtc->lut_r[regno] << 6;
  221.         *green = radeon_crtc->lut_g[regno] << 6;
  222.         *blue = radeon_crtc->lut_b[regno] << 6;
  223. }
  224.  
  225. static void radeon_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  226.                                   u16 *blue, uint32_t start, uint32_t size)
  227. {
  228.         struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  229.         int end = (start + size > 256) ? 256 : start + size, i;
  230.  
  231.         /* userspace palettes are always correct as is */
  232.         for (i = start; i < end; i++) {
  233.                         radeon_crtc->lut_r[i] = red[i] >> 6;
  234.                         radeon_crtc->lut_g[i] = green[i] >> 6;
  235.                         radeon_crtc->lut_b[i] = blue[i] >> 6;
  236.                 }
  237.         radeon_crtc_load_lut(crtc);
  238. }
  239.  
  240. static void radeon_crtc_destroy(struct drm_crtc *crtc)
  241. {
  242.         struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  243.  
  244.         drm_crtc_cleanup(crtc);
  245.         kfree(radeon_crtc);
  246. }
  247.  
  248. static const struct drm_crtc_funcs radeon_crtc_funcs = {
  249.     .cursor_set = NULL,
  250.     .cursor_move = NULL,
  251.         .gamma_set = radeon_crtc_gamma_set,
  252.         .set_config = drm_crtc_helper_set_config,
  253.         .destroy = radeon_crtc_destroy,
  254.         .page_flip = NULL,
  255. };
  256.  
  257. static void radeon_crtc_init(struct drm_device *dev, int index)
  258. {
  259.         struct radeon_device *rdev = dev->dev_private;
  260.         struct radeon_crtc *radeon_crtc;
  261.         int i;
  262.  
  263.         radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  264.         if (radeon_crtc == NULL)
  265.                 return;
  266.  
  267.         drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
  268.  
  269.         drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
  270.         radeon_crtc->crtc_id = index;
  271.         rdev->mode_info.crtcs[index] = radeon_crtc;
  272.  
  273. #if 0
  274.         radeon_crtc->mode_set.crtc = &radeon_crtc->base;
  275.         radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
  276.         radeon_crtc->mode_set.num_connectors = 0;
  277. #endif
  278.  
  279.         for (i = 0; i < 256; i++) {
  280.                 radeon_crtc->lut_r[i] = i << 2;
  281.                 radeon_crtc->lut_g[i] = i << 2;
  282.                 radeon_crtc->lut_b[i] = i << 2;
  283.         }
  284.  
  285.         if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
  286.                 radeon_atombios_init_crtc(dev, radeon_crtc);
  287.         else
  288.                 radeon_legacy_init_crtc(dev, radeon_crtc);
  289. }
  290.  
  291. static const char *encoder_names[36] = {
  292.         "NONE",
  293.         "INTERNAL_LVDS",
  294.         "INTERNAL_TMDS1",
  295.         "INTERNAL_TMDS2",
  296.         "INTERNAL_DAC1",
  297.         "INTERNAL_DAC2",
  298.         "INTERNAL_SDVOA",
  299.         "INTERNAL_SDVOB",
  300.         "SI170B",
  301.         "CH7303",
  302.         "CH7301",
  303.         "INTERNAL_DVO1",
  304.         "EXTERNAL_SDVOA",
  305.         "EXTERNAL_SDVOB",
  306.         "TITFP513",
  307.         "INTERNAL_LVTM1",
  308.         "VT1623",
  309.         "HDMI_SI1930",
  310.         "HDMI_INTERNAL",
  311.         "INTERNAL_KLDSCP_TMDS1",
  312.         "INTERNAL_KLDSCP_DVO1",
  313.         "INTERNAL_KLDSCP_DAC1",
  314.         "INTERNAL_KLDSCP_DAC2",
  315.         "SI178",
  316.         "MVPU_FPGA",
  317.         "INTERNAL_DDI",
  318.         "VT1625",
  319.         "HDMI_SI1932",
  320.         "DP_AN9801",
  321.         "DP_DP501",
  322.         "INTERNAL_UNIPHY",
  323.         "INTERNAL_KLDSCP_LVTMA",
  324.         "INTERNAL_UNIPHY1",
  325.         "INTERNAL_UNIPHY2",
  326.         "NUTMEG",
  327.         "TRAVIS",
  328. };
  329.  
  330. static const char *connector_names[15] = {
  331.         "Unknown",
  332.         "VGA",
  333.         "DVI-I",
  334.         "DVI-D",
  335.         "DVI-A",
  336.         "Composite",
  337.         "S-video",
  338.         "LVDS",
  339.         "Component",
  340.         "DIN",
  341.         "DisplayPort",
  342.         "HDMI-A",
  343.         "HDMI-B",
  344.         "TV",
  345.         "eDP",
  346. };
  347.  
  348. static const char *hpd_names[6] = {
  349.         "HPD1",
  350.         "HPD2",
  351.         "HPD3",
  352.         "HPD4",
  353.         "HPD5",
  354.         "HPD6",
  355. };
  356.  
  357. static void radeon_print_display_setup(struct drm_device *dev)
  358. {
  359.         struct drm_connector *connector;
  360.         struct radeon_connector *radeon_connector;
  361.         struct drm_encoder *encoder;
  362.         struct radeon_encoder *radeon_encoder;
  363.         uint32_t devices;
  364.         int i = 0;
  365.  
  366.         DRM_INFO("Radeon Display Connectors\n");
  367.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  368.                 radeon_connector = to_radeon_connector(connector);
  369.                 DRM_INFO("Connector %d:\n", i);
  370.                 DRM_INFO("  %s\n", connector_names[connector->connector_type]);
  371.                 if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
  372.                         DRM_INFO("  %s\n", hpd_names[radeon_connector->hpd.hpd]);
  373.                 if (radeon_connector->ddc_bus) {
  374.                         DRM_INFO("  DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
  375.                                  radeon_connector->ddc_bus->rec.mask_clk_reg,
  376.                                  radeon_connector->ddc_bus->rec.mask_data_reg,
  377.                                  radeon_connector->ddc_bus->rec.a_clk_reg,
  378.                                  radeon_connector->ddc_bus->rec.a_data_reg,
  379.                                  radeon_connector->ddc_bus->rec.en_clk_reg,
  380.                                  radeon_connector->ddc_bus->rec.en_data_reg,
  381.                                  radeon_connector->ddc_bus->rec.y_clk_reg,
  382.                                  radeon_connector->ddc_bus->rec.y_data_reg);
  383.                         if (radeon_connector->router.ddc_valid)
  384.                                 DRM_INFO("  DDC Router 0x%x/0x%x\n",
  385.                                          radeon_connector->router.ddc_mux_control_pin,
  386.                                          radeon_connector->router.ddc_mux_state);
  387.                         if (radeon_connector->router.cd_valid)
  388.                                 DRM_INFO("  Clock/Data Router 0x%x/0x%x\n",
  389.                                          radeon_connector->router.cd_mux_control_pin,
  390.                                          radeon_connector->router.cd_mux_state);
  391.                 } else {
  392.                         if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
  393.                             connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
  394.                             connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
  395.                             connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
  396.                             connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
  397.                             connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
  398.                                 DRM_INFO("  DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
  399.                 }
  400.                 DRM_INFO("  Encoders:\n");
  401.                 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  402.                         radeon_encoder = to_radeon_encoder(encoder);
  403.                         devices = radeon_encoder->devices & radeon_connector->devices;
  404.                         if (devices) {
  405.                                 if (devices & ATOM_DEVICE_CRT1_SUPPORT)
  406.                                         DRM_INFO("    CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  407.                                 if (devices & ATOM_DEVICE_CRT2_SUPPORT)
  408.                                         DRM_INFO("    CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  409.                                 if (devices & ATOM_DEVICE_LCD1_SUPPORT)
  410.                                         DRM_INFO("    LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  411.                                 if (devices & ATOM_DEVICE_DFP1_SUPPORT)
  412.                                         DRM_INFO("    DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  413.                                 if (devices & ATOM_DEVICE_DFP2_SUPPORT)
  414.                                         DRM_INFO("    DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  415.                                 if (devices & ATOM_DEVICE_DFP3_SUPPORT)
  416.                                         DRM_INFO("    DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
  417.                                 if (devices & ATOM_DEVICE_DFP4_SUPPORT)
  418.                                         DRM_INFO("    DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
  419.                                 if (devices & ATOM_DEVICE_DFP5_SUPPORT)
  420.                                         DRM_INFO("    DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
  421.                                 if (devices & ATOM_DEVICE_DFP6_SUPPORT)
  422.                                         DRM_INFO("    DFP6: %s\n", encoder_names[radeon_encoder->encoder_id]);
  423.                                 if (devices & ATOM_DEVICE_TV1_SUPPORT)
  424.                                         DRM_INFO("    TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  425.                                 if (devices & ATOM_DEVICE_CV_SUPPORT)
  426.                                         DRM_INFO("    CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
  427.                         }
  428.                 }
  429.                 i++;
  430.         }
  431. }
  432.  
  433. static bool radeon_setup_enc_conn(struct drm_device *dev)
  434. {
  435.         struct radeon_device *rdev = dev->dev_private;
  436.         struct drm_connector *drm_connector;
  437.         bool ret = false;
  438.  
  439.         if (rdev->bios) {
  440.                 if (rdev->is_atom_bios) {
  441.                         ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
  442.                         if (ret == false)
  443.                                 ret = radeon_get_atom_connector_info_from_object_table(dev);
  444.                 } else {
  445.                         ret = radeon_get_legacy_connector_info_from_bios(dev);
  446.                         if (ret == false)
  447.                                 ret = radeon_get_legacy_connector_info_from_table(dev);
  448.                 }
  449.         } else {
  450.                 if (!ASIC_IS_AVIVO(rdev))
  451.                         ret = radeon_get_legacy_connector_info_from_table(dev);
  452.         }
  453.         if (ret) {
  454.                 radeon_setup_encoder_clones(dev);
  455.                 radeon_print_display_setup(dev);
  456.                 list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head)
  457.                         radeon_ddc_dump(drm_connector);
  458.         }
  459.  
  460.         return ret;
  461. }
  462.  
  463. int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
  464. {
  465.         struct drm_device *dev = radeon_connector->base.dev;
  466.         struct radeon_device *rdev = dev->dev_private;
  467.         int ret = 0;
  468.  
  469.         /* on hw with routers, select right port */
  470.         if (radeon_connector->router.ddc_valid)
  471.                 radeon_router_select_ddc_port(radeon_connector);
  472.  
  473.         if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
  474.             (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)) {
  475.                 struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
  476.                 if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
  477.                      dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) && dig->dp_i2c_bus)
  478.                         radeon_connector->edid = drm_get_edid(&radeon_connector->base, &dig->dp_i2c_bus->adapter);
  479.         }
  480.         if (!radeon_connector->ddc_bus)
  481.                 return -1;
  482.         if (!radeon_connector->edid) {
  483.                 radeon_connector->edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
  484.         }
  485.  
  486.         if (!radeon_connector->edid) {
  487.                 if (rdev->is_atom_bios) {
  488.                         /* some laptops provide a hardcoded edid in rom for LCDs */
  489.                         if (((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_LVDS) ||
  490.                              (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)))
  491.                                 radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
  492.                 } else
  493.         /* some servers provide a hardcoded edid in rom for KVMs */
  494.                         radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
  495.         }
  496.         if (radeon_connector->edid) {
  497.                 drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
  498.                 ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
  499.                 return ret;
  500.         }
  501.         drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
  502.         return 0;
  503. }
  504.  
  505. static int radeon_ddc_dump(struct drm_connector *connector)
  506. {
  507.         struct edid *edid;
  508.         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
  509.         int ret = 0;
  510.  
  511.         /* on hw with routers, select right port */
  512.         if (radeon_connector->router.ddc_valid)
  513.                 radeon_router_select_ddc_port(radeon_connector);
  514.  
  515.         if (!radeon_connector->ddc_bus)
  516.                 return -1;
  517.         edid = drm_get_edid(connector, &radeon_connector->ddc_bus->adapter);
  518.         /* Log EDID retrieval status here. In particular with regard to
  519.          * connectors with requires_extended_probe flag set, that will prevent
  520.          * function radeon_dvi_detect() to fetch EDID on this connector,
  521.          * as long as there is no valid EDID header found */
  522.         if (edid) {
  523.                 DRM_INFO("Radeon display connector %s: Found valid EDID",
  524.                                 drm_get_connector_name(connector));
  525.                 kfree(edid);
  526.         } else {
  527.                 DRM_INFO("Radeon display connector %s: No monitor connected or invalid EDID",
  528.                                 drm_get_connector_name(connector));
  529.         }
  530.         return ret;
  531. }
  532.  
  533. /* avivo */
  534. static void avivo_get_fb_div(struct radeon_pll *pll,
  535.                              u32 target_clock,
  536.                              u32 post_div,
  537.                              u32 ref_div,
  538.                              u32 *fb_div,
  539.                              u32 *frac_fb_div)
  540. {
  541.         u32 tmp = post_div * ref_div;
  542.  
  543.         tmp *= target_clock;
  544.         *fb_div = tmp / pll->reference_freq;
  545.         *frac_fb_div = tmp % pll->reference_freq;
  546.  
  547.         if (*fb_div > pll->max_feedback_div)
  548.                 *fb_div = pll->max_feedback_div;
  549.         else if (*fb_div < pll->min_feedback_div)
  550.                 *fb_div = pll->min_feedback_div;
  551. }
  552.  
  553. static u32 avivo_get_post_div(struct radeon_pll *pll,
  554.                               u32 target_clock)
  555. {
  556.         u32 vco, post_div, tmp;
  557.  
  558.         if (pll->flags & RADEON_PLL_USE_POST_DIV)
  559.                 return pll->post_div;
  560.  
  561.         if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP) {
  562.                 if (pll->flags & RADEON_PLL_IS_LCD)
  563.                         vco = pll->lcd_pll_out_min;
  564.                 else
  565.                         vco = pll->pll_out_min;
  566.         } else {
  567.                 if (pll->flags & RADEON_PLL_IS_LCD)
  568.                         vco = pll->lcd_pll_out_max;
  569.                 else
  570.                         vco = pll->pll_out_max;
  571.         }
  572.  
  573.         post_div = vco / target_clock;
  574.         tmp = vco % target_clock;
  575.  
  576.         if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP) {
  577.                 if (tmp)
  578.                         post_div++;
  579.         } else {
  580.                 if (!tmp)
  581.                         post_div--;
  582.         }
  583.  
  584.         if (post_div > pll->max_post_div)
  585.                 post_div = pll->max_post_div;
  586.         else if (post_div < pll->min_post_div)
  587.                 post_div = pll->min_post_div;
  588.  
  589.         return post_div;
  590. }
  591.  
  592. #define MAX_TOLERANCE 10
  593.  
  594. void radeon_compute_pll_avivo(struct radeon_pll *pll,
  595.                               u32 freq,
  596.                               u32 *dot_clock_p,
  597.                               u32 *fb_div_p,
  598.                               u32 *frac_fb_div_p,
  599.                               u32 *ref_div_p,
  600.                               u32 *post_div_p)
  601. {
  602.         u32 target_clock = freq / 10;
  603.         u32 post_div = avivo_get_post_div(pll, target_clock);
  604.         u32 ref_div = pll->min_ref_div;
  605.         u32 fb_div = 0, frac_fb_div = 0, tmp;
  606.  
  607.         if (pll->flags & RADEON_PLL_USE_REF_DIV)
  608.                 ref_div = pll->reference_div;
  609.  
  610.         if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  611.                 avivo_get_fb_div(pll, target_clock, post_div, ref_div, &fb_div, &frac_fb_div);
  612.                 frac_fb_div = (100 * frac_fb_div) / pll->reference_freq;
  613.                 if (frac_fb_div >= 5) {
  614.                         frac_fb_div -= 5;
  615.                         frac_fb_div = frac_fb_div / 10;
  616.                         frac_fb_div++;
  617.                 }
  618.                 if (frac_fb_div >= 10) {
  619.                         fb_div++;
  620.                         frac_fb_div = 0;
  621.                 }
  622.         } else {
  623.                 while (ref_div <= pll->max_ref_div) {
  624.                         avivo_get_fb_div(pll, target_clock, post_div, ref_div,
  625.                                          &fb_div, &frac_fb_div);
  626.                         if (frac_fb_div >= (pll->reference_freq / 2))
  627.                                 fb_div++;
  628.                         frac_fb_div = 0;
  629.                         tmp = (pll->reference_freq * fb_div) / (post_div * ref_div);
  630.                         tmp = (tmp * 10000) / target_clock;
  631.  
  632.                         if (tmp > (10000 + MAX_TOLERANCE))
  633.                                 ref_div++;
  634.                         else if (tmp >= (10000 - MAX_TOLERANCE))
  635.                                 break;
  636.                         else
  637.                                 ref_div++;
  638.                 }
  639.         }
  640.  
  641.         *dot_clock_p = ((pll->reference_freq * fb_div * 10) + (pll->reference_freq * frac_fb_div)) /
  642.                 (ref_div * post_div * 10);
  643.         *fb_div_p = fb_div;
  644.         *frac_fb_div_p = frac_fb_div;
  645.         *ref_div_p = ref_div;
  646.         *post_div_p = post_div;
  647.         DRM_DEBUG_KMS("%d, pll dividers - fb: %d.%d ref: %d, post %d\n",
  648.                       *dot_clock_p, fb_div, frac_fb_div, ref_div, post_div);
  649. }
  650.  
  651. /* pre-avivo */
  652. static inline uint32_t radeon_div(uint64_t n, uint32_t d)
  653. {
  654.         uint64_t mod;
  655.  
  656.         n += d / 2;
  657.  
  658.         mod = do_div(n, d);
  659.         return n;
  660. }
  661.  
  662. void radeon_compute_pll_legacy(struct radeon_pll *pll,
  663.                         uint64_t freq,
  664.                         uint32_t *dot_clock_p,
  665.                         uint32_t *fb_div_p,
  666.                         uint32_t *frac_fb_div_p,
  667.                         uint32_t *ref_div_p,
  668.                         uint32_t *post_div_p)
  669. {
  670.         uint32_t min_ref_div = pll->min_ref_div;
  671.         uint32_t max_ref_div = pll->max_ref_div;
  672.         uint32_t min_post_div = pll->min_post_div;
  673.         uint32_t max_post_div = pll->max_post_div;
  674.         uint32_t min_fractional_feed_div = 0;
  675.         uint32_t max_fractional_feed_div = 0;
  676.         uint32_t best_vco = pll->best_vco;
  677.         uint32_t best_post_div = 1;
  678.         uint32_t best_ref_div = 1;
  679.         uint32_t best_feedback_div = 1;
  680.         uint32_t best_frac_feedback_div = 0;
  681.         uint32_t best_freq = -1;
  682.         uint32_t best_error = 0xffffffff;
  683.         uint32_t best_vco_diff = 1;
  684.         uint32_t post_div;
  685.         u32 pll_out_min, pll_out_max;
  686.  
  687.         DRM_DEBUG_KMS("PLL freq %llu %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
  688.         freq = freq * 1000;
  689.  
  690.         if (pll->flags & RADEON_PLL_IS_LCD) {
  691.                 pll_out_min = pll->lcd_pll_out_min;
  692.                 pll_out_max = pll->lcd_pll_out_max;
  693.         } else {
  694.                 pll_out_min = pll->pll_out_min;
  695.                 pll_out_max = pll->pll_out_max;
  696.         }
  697.  
  698.         if (pll_out_min > 64800)
  699.                 pll_out_min = 64800;
  700.  
  701.         if (pll->flags & RADEON_PLL_USE_REF_DIV)
  702.                 min_ref_div = max_ref_div = pll->reference_div;
  703.         else {
  704.                 while (min_ref_div < max_ref_div-1) {
  705.                         uint32_t mid = (min_ref_div + max_ref_div) / 2;
  706.                         uint32_t pll_in = pll->reference_freq / mid;
  707.                         if (pll_in < pll->pll_in_min)
  708.                                 max_ref_div = mid;
  709.                         else if (pll_in > pll->pll_in_max)
  710.                                 min_ref_div = mid;
  711.                         else
  712.                                 break;
  713.                 }
  714.         }
  715.  
  716.         if (pll->flags & RADEON_PLL_USE_POST_DIV)
  717.                 min_post_div = max_post_div = pll->post_div;
  718.  
  719.         if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  720.                 min_fractional_feed_div = pll->min_frac_feedback_div;
  721.                 max_fractional_feed_div = pll->max_frac_feedback_div;
  722.         }
  723.  
  724.         for (post_div = max_post_div; post_div >= min_post_div; --post_div) {
  725.                 uint32_t ref_div;
  726.  
  727.                 if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
  728.                         continue;
  729.  
  730.                 /* legacy radeons only have a few post_divs */
  731.                 if (pll->flags & RADEON_PLL_LEGACY) {
  732.                         if ((post_div == 5) ||
  733.                             (post_div == 7) ||
  734.                             (post_div == 9) ||
  735.                             (post_div == 10) ||
  736.                             (post_div == 11) ||
  737.                             (post_div == 13) ||
  738.                             (post_div == 14) ||
  739.                             (post_div == 15))
  740.                                 continue;
  741.                 }
  742.  
  743.                 for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
  744.                         uint32_t feedback_div, current_freq = 0, error, vco_diff;
  745.                         uint32_t pll_in = pll->reference_freq / ref_div;
  746.                         uint32_t min_feed_div = pll->min_feedback_div;
  747.                         uint32_t max_feed_div = pll->max_feedback_div + 1;
  748.  
  749.                         if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
  750.                                 continue;
  751.  
  752.                         while (min_feed_div < max_feed_div) {
  753.                                 uint32_t vco;
  754.                                 uint32_t min_frac_feed_div = min_fractional_feed_div;
  755.                                 uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
  756.                                 uint32_t frac_feedback_div;
  757.                                 uint64_t tmp;
  758.  
  759.                                 feedback_div = (min_feed_div + max_feed_div) / 2;
  760.  
  761.                                 tmp = (uint64_t)pll->reference_freq * feedback_div;
  762.                                 vco = radeon_div(tmp, ref_div);
  763.  
  764.                                 if (vco < pll_out_min) {
  765.                                         min_feed_div = feedback_div + 1;
  766.                                         continue;
  767.                                 } else if (vco > pll_out_max) {
  768.                                         max_feed_div = feedback_div;
  769.                                         continue;
  770.                                 }
  771.  
  772.                                 while (min_frac_feed_div < max_frac_feed_div) {
  773.                                         frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
  774.                                         tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
  775.                                         tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
  776.                                         current_freq = radeon_div(tmp, ref_div * post_div);
  777.  
  778.                                         if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
  779.                                                 if (freq < current_freq)
  780.                                                         error = 0xffffffff;
  781.                                                 else
  782.                                                 error = freq - current_freq;
  783.                                         } else
  784.                                         error = abs(current_freq - freq);
  785.                                         vco_diff = abs(vco - best_vco);
  786.  
  787.                                         if ((best_vco == 0 && error < best_error) ||
  788.                                             (best_vco != 0 &&
  789.                                              ((best_error > 100 && error < best_error - 100) ||
  790.                                               (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
  791.                                                 best_post_div = post_div;
  792.                                                 best_ref_div = ref_div;
  793.                                                 best_feedback_div = feedback_div;
  794.                                                 best_frac_feedback_div = frac_feedback_div;
  795.                                                 best_freq = current_freq;
  796.                                                 best_error = error;
  797.                                                 best_vco_diff = vco_diff;
  798.                                         } else if (current_freq == freq) {
  799.                                                 if (best_freq == -1) {
  800.                                                         best_post_div = post_div;
  801.                                                         best_ref_div = ref_div;
  802.                                                         best_feedback_div = feedback_div;
  803.                                                         best_frac_feedback_div = frac_feedback_div;
  804.                                                         best_freq = current_freq;
  805.                                                         best_error = error;
  806.                                                         best_vco_diff = vco_diff;
  807.                                                 } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
  808.                                                            ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
  809.                                                            ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
  810.                                                            ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
  811.                                                            ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
  812.                                                            ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
  813.                                                         best_post_div = post_div;
  814.                                                         best_ref_div = ref_div;
  815.                                                         best_feedback_div = feedback_div;
  816.                                                         best_frac_feedback_div = frac_feedback_div;
  817.                                                         best_freq = current_freq;
  818.                                                         best_error = error;
  819.                                                         best_vco_diff = vco_diff;
  820.                                                 }
  821.                                         }
  822.                                         if (current_freq < freq)
  823.                                                 min_frac_feed_div = frac_feedback_div + 1;
  824.                                         else
  825.                                                 max_frac_feed_div = frac_feedback_div;
  826.                                 }
  827.                                 if (current_freq < freq)
  828.                                         min_feed_div = feedback_div + 1;
  829.                                 else
  830.                                         max_feed_div = feedback_div;
  831.                         }
  832.                 }
  833.         }
  834.  
  835.         *dot_clock_p = best_freq / 10000;
  836.         *fb_div_p = best_feedback_div;
  837.         *frac_fb_div_p = best_frac_feedback_div;
  838.         *ref_div_p = best_ref_div;
  839.         *post_div_p = best_post_div;
  840.         DRM_DEBUG_KMS("%lld %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
  841.                       (long long)freq,
  842.                       best_freq / 1000, best_feedback_div, best_frac_feedback_div,
  843.                       best_ref_div, best_post_div);
  844.  
  845. }
  846.  
  847. static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
  848. {
  849.         struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  850.  
  851.         drm_framebuffer_cleanup(fb);
  852.         kfree(radeon_fb);
  853. }
  854.  
  855. static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  856.                          struct drm_file *file_priv,
  857.                          unsigned int *handle)
  858. {
  859.    struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  860.  
  861.    return NULL;
  862. //   return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
  863. }
  864.  
  865. static const struct drm_framebuffer_funcs radeon_fb_funcs = {
  866.         .destroy = radeon_user_framebuffer_destroy,
  867.     .create_handle = radeon_user_framebuffer_create_handle,
  868. };
  869.  
  870. void
  871. radeon_framebuffer_init(struct drm_device *dev,
  872.                         struct radeon_framebuffer *rfb,
  873.                           struct drm_mode_fb_cmd *mode_cmd,
  874.                           struct drm_gem_object *obj)
  875. {
  876.         rfb->obj = obj;
  877.         drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
  878.         drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
  879. }
  880.  
  881.  
  882.  
  883. static const struct drm_mode_config_funcs radeon_mode_funcs = {
  884. //      .fb_create = radeon_user_framebuffer_create,
  885. //   .output_poll_changed = radeon_output_poll_changed
  886. };
  887.  
  888. struct drm_prop_enum_list {
  889.         int type;
  890.         char *name;
  891. };
  892.  
  893. static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
  894. {       { 0, "driver" },
  895.         { 1, "bios" },
  896. };
  897.  
  898. static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
  899. {       { TV_STD_NTSC, "ntsc" },
  900.         { TV_STD_PAL, "pal" },
  901.         { TV_STD_PAL_M, "pal-m" },
  902.         { TV_STD_PAL_60, "pal-60" },
  903.         { TV_STD_NTSC_J, "ntsc-j" },
  904.         { TV_STD_SCART_PAL, "scart-pal" },
  905.         { TV_STD_PAL_CN, "pal-cn" },
  906.         { TV_STD_SECAM, "secam" },
  907. };
  908.  
  909. static struct drm_prop_enum_list radeon_underscan_enum_list[] =
  910. {       { UNDERSCAN_OFF, "off" },
  911.         { UNDERSCAN_ON, "on" },
  912.         { UNDERSCAN_AUTO, "auto" },
  913. };
  914.  
  915. static int radeon_modeset_create_props(struct radeon_device *rdev)
  916. {
  917.         int i, sz;
  918.  
  919.         if (rdev->is_atom_bios) {
  920.                 rdev->mode_info.coherent_mode_property =
  921.                         drm_property_create(rdev->ddev,
  922.                                             DRM_MODE_PROP_RANGE,
  923.                                             "coherent", 2);
  924.                 if (!rdev->mode_info.coherent_mode_property)
  925.                         return -ENOMEM;
  926.  
  927.                 rdev->mode_info.coherent_mode_property->values[0] = 0;
  928.                 rdev->mode_info.coherent_mode_property->values[1] = 1;
  929.         }
  930.  
  931.         if (!ASIC_IS_AVIVO(rdev)) {
  932.                 sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
  933.                 rdev->mode_info.tmds_pll_property =
  934.                         drm_property_create(rdev->ddev,
  935.                                             DRM_MODE_PROP_ENUM,
  936.                                             "tmds_pll", sz);
  937.                 for (i = 0; i < sz; i++) {
  938.                         drm_property_add_enum(rdev->mode_info.tmds_pll_property,
  939.                                               i,
  940.                                               radeon_tmds_pll_enum_list[i].type,
  941.                                               radeon_tmds_pll_enum_list[i].name);
  942.                 }
  943.         }
  944.  
  945.         rdev->mode_info.load_detect_property =
  946.                 drm_property_create(rdev->ddev,
  947.                                     DRM_MODE_PROP_RANGE,
  948.                                     "load detection", 2);
  949.         if (!rdev->mode_info.load_detect_property)
  950.                 return -ENOMEM;
  951.         rdev->mode_info.load_detect_property->values[0] = 0;
  952.         rdev->mode_info.load_detect_property->values[1] = 1;
  953.  
  954.         drm_mode_create_scaling_mode_property(rdev->ddev);
  955.  
  956.         sz = ARRAY_SIZE(radeon_tv_std_enum_list);
  957.         rdev->mode_info.tv_std_property =
  958.                 drm_property_create(rdev->ddev,
  959.                                     DRM_MODE_PROP_ENUM,
  960.                                     "tv standard", sz);
  961.         for (i = 0; i < sz; i++) {
  962.                 drm_property_add_enum(rdev->mode_info.tv_std_property,
  963.                                       i,
  964.                                       radeon_tv_std_enum_list[i].type,
  965.                                       radeon_tv_std_enum_list[i].name);
  966.         }
  967.  
  968.         sz = ARRAY_SIZE(radeon_underscan_enum_list);
  969.         rdev->mode_info.underscan_property =
  970.                 drm_property_create(rdev->ddev,
  971.                                     DRM_MODE_PROP_ENUM,
  972.                                     "underscan", sz);
  973.         for (i = 0; i < sz; i++) {
  974.                 drm_property_add_enum(rdev->mode_info.underscan_property,
  975.                                       i,
  976.                                       radeon_underscan_enum_list[i].type,
  977.                                       radeon_underscan_enum_list[i].name);
  978.         }
  979.  
  980.         rdev->mode_info.underscan_hborder_property =
  981.                 drm_property_create(rdev->ddev,
  982.                                         DRM_MODE_PROP_RANGE,
  983.                                         "underscan hborder", 2);
  984.         if (!rdev->mode_info.underscan_hborder_property)
  985.                 return -ENOMEM;
  986.         rdev->mode_info.underscan_hborder_property->values[0] = 0;
  987.         rdev->mode_info.underscan_hborder_property->values[1] = 128;
  988.  
  989.         rdev->mode_info.underscan_vborder_property =
  990.                 drm_property_create(rdev->ddev,
  991.                                         DRM_MODE_PROP_RANGE,
  992.                                         "underscan vborder", 2);
  993.         if (!rdev->mode_info.underscan_vborder_property)
  994.                 return -ENOMEM;
  995.         rdev->mode_info.underscan_vborder_property->values[0] = 0;
  996.         rdev->mode_info.underscan_vborder_property->values[1] = 128;
  997.  
  998.         return 0;
  999. }
  1000.  
  1001. void radeon_update_display_priority(struct radeon_device *rdev)
  1002. {
  1003.         /* adjustment options for the display watermarks */
  1004.         if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
  1005.                 /* set display priority to high for r3xx, rv515 chips
  1006.                  * this avoids flickering due to underflow to the
  1007.                  * display controllers during heavy acceleration.
  1008.                  * Don't force high on rs4xx igp chips as it seems to
  1009.                  * affect the sound card.  See kernel bug 15982.
  1010.                  */
  1011.                 if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
  1012.                     !(rdev->flags & RADEON_IS_IGP))
  1013.                         rdev->disp_priority = 2;
  1014.                 else
  1015.                         rdev->disp_priority = 0;
  1016.         } else
  1017.                 rdev->disp_priority = radeon_disp_priority;
  1018.  
  1019. }
  1020.  
  1021. int radeon_modeset_init(struct radeon_device *rdev)
  1022. {
  1023.         int i;
  1024.         int ret;
  1025.  
  1026.         drm_mode_config_init(rdev->ddev);
  1027.         rdev->mode_info.mode_config_initialized = true;
  1028.  
  1029.     rdev->ddev->mode_config.funcs = (void *)&radeon_mode_funcs;
  1030.  
  1031.         if (ASIC_IS_DCE5(rdev)) {
  1032.                 rdev->ddev->mode_config.max_width = 16384;
  1033.                 rdev->ddev->mode_config.max_height = 16384;
  1034.         } else if (ASIC_IS_AVIVO(rdev)) {
  1035.                 rdev->ddev->mode_config.max_width = 8192;
  1036.                 rdev->ddev->mode_config.max_height = 8192;
  1037.         } else {
  1038.                 rdev->ddev->mode_config.max_width = 4096;
  1039.                 rdev->ddev->mode_config.max_height = 4096;
  1040.         }
  1041.  
  1042.         rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
  1043.  
  1044.         ret = radeon_modeset_create_props(rdev);
  1045.         if (ret) {
  1046.                 return ret;
  1047.         }
  1048.  
  1049.         /* init i2c buses */
  1050.         radeon_i2c_init(rdev);
  1051.  
  1052.         /* check combios for a valid hardcoded EDID - Sun servers */
  1053.         if (!rdev->is_atom_bios) {
  1054.                 /* check for hardcoded EDID in BIOS */
  1055.                 radeon_combios_check_hardcoded_edid(rdev);
  1056.         }
  1057.  
  1058.         /* allocate crtcs */
  1059.         for (i = 0; i < rdev->num_crtc; i++) {
  1060.                 radeon_crtc_init(rdev->ddev, i);
  1061.         }
  1062.  
  1063.         /* okay we should have all the bios connectors */
  1064.         ret = radeon_setup_enc_conn(rdev->ddev);
  1065.         if (!ret) {
  1066.                 return ret;
  1067.         }
  1068.  
  1069.         /* init dig PHYs */
  1070.         if (rdev->is_atom_bios)
  1071.                 radeon_atom_encoder_init(rdev);
  1072.  
  1073.         /* initialize hpd */
  1074. //   radeon_hpd_init(rdev);
  1075.  
  1076.         /* Initialize power management */
  1077. //   radeon_pm_init(rdev);
  1078.  
  1079.         radeon_fbdev_init(rdev);
  1080. //   drm_kms_helper_poll_init(rdev->ddev);
  1081.  
  1082.         return 0;
  1083. }
  1084.  
  1085. void radeon_modeset_fini(struct radeon_device *rdev)
  1086. {
  1087.         kfree(rdev->mode_info.bios_hardcoded_edid);
  1088.  
  1089.         if (rdev->mode_info.mode_config_initialized) {
  1090. //       drm_kms_helper_poll_fini(rdev->ddev);
  1091. //       radeon_hpd_fini(rdev);
  1092.                 drm_mode_config_cleanup(rdev->ddev);
  1093.                 rdev->mode_info.mode_config_initialized = false;
  1094.         }
  1095.         /* free i2c buses */
  1096.         radeon_i2c_fini(rdev);
  1097. }
  1098.  
  1099. static bool is_hdtv_mode(struct drm_display_mode *mode)
  1100. {
  1101.         /* try and guess if this is a tv or a monitor */
  1102.         if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
  1103.             (mode->vdisplay == 576) || /* 576p */
  1104.             (mode->vdisplay == 720) || /* 720p */
  1105.             (mode->vdisplay == 1080)) /* 1080p */
  1106.                 return true;
  1107.         else
  1108.                 return false;
  1109. }
  1110.  
  1111. bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
  1112.                                 struct drm_display_mode *mode,
  1113.                                 struct drm_display_mode *adjusted_mode)
  1114. {
  1115.         struct drm_device *dev = crtc->dev;
  1116.         struct radeon_device *rdev = dev->dev_private;
  1117.         struct drm_encoder *encoder;
  1118.                 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1119.         struct radeon_encoder *radeon_encoder;
  1120.         struct drm_connector *connector;
  1121.         struct radeon_connector *radeon_connector;
  1122.         bool first = true;
  1123.         u32 src_v = 1, dst_v = 1;
  1124.         u32 src_h = 1, dst_h = 1;
  1125.  
  1126.         radeon_crtc->h_border = 0;
  1127.         radeon_crtc->v_border = 0;
  1128.  
  1129.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  1130.                 if (encoder->crtc != crtc)
  1131.                         continue;
  1132.                 radeon_encoder = to_radeon_encoder(encoder);
  1133.                 connector = radeon_get_connector_for_encoder(encoder);
  1134.                 radeon_connector = to_radeon_connector(connector);
  1135.  
  1136.                 if (first) {
  1137.                         /* set scaling */
  1138.                         if (radeon_encoder->rmx_type == RMX_OFF)
  1139.                                 radeon_crtc->rmx_type = RMX_OFF;
  1140.                         else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
  1141.                                  mode->vdisplay < radeon_encoder->native_mode.vdisplay)
  1142.                         radeon_crtc->rmx_type = radeon_encoder->rmx_type;
  1143.                         else
  1144.                                 radeon_crtc->rmx_type = RMX_OFF;
  1145.                         /* copy native mode */
  1146.                         memcpy(&radeon_crtc->native_mode,
  1147.                                 &radeon_encoder->native_mode,
  1148.                                 sizeof(struct drm_display_mode));
  1149.                         src_v = crtc->mode.vdisplay;
  1150.                         dst_v = radeon_crtc->native_mode.vdisplay;
  1151.                         src_h = crtc->mode.hdisplay;
  1152.                         dst_h = radeon_crtc->native_mode.hdisplay;
  1153.  
  1154.                         /* fix up for overscan on hdmi */
  1155.                         if (ASIC_IS_AVIVO(rdev) &&
  1156.                             (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
  1157.                             ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
  1158.                              ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
  1159.                               drm_detect_hdmi_monitor(radeon_connector->edid) &&
  1160.                               is_hdtv_mode(mode)))) {
  1161.                                 if (radeon_encoder->underscan_hborder != 0)
  1162.                                         radeon_crtc->h_border = radeon_encoder->underscan_hborder;
  1163.                                 else
  1164.                                 radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
  1165.                                 if (radeon_encoder->underscan_vborder != 0)
  1166.                                         radeon_crtc->v_border = radeon_encoder->underscan_vborder;
  1167.                                 else
  1168.                                 radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
  1169.                                 radeon_crtc->rmx_type = RMX_FULL;
  1170.                                 src_v = crtc->mode.vdisplay;
  1171.                                 dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
  1172.                                 src_h = crtc->mode.hdisplay;
  1173.                                 dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
  1174.                         }
  1175.                         first = false;
  1176.                 } else {
  1177.                         if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
  1178.                                 /* WARNING: Right now this can't happen but
  1179.                                  * in the future we need to check that scaling
  1180.                                  * are consistent across different encoder
  1181.                                  * (ie all encoder can work with the same
  1182.                                  *  scaling).
  1183.                                  */
  1184.                                 DRM_ERROR("Scaling not consistent across encoder.\n");
  1185.                                 return false;
  1186.                         }
  1187.                 }
  1188.         }
  1189.         if (radeon_crtc->rmx_type != RMX_OFF) {
  1190.         fixed20_12 a, b;
  1191.                 a.full = dfixed_const(src_v);
  1192.                 b.full = dfixed_const(dst_v);
  1193.                 radeon_crtc->vsc.full = dfixed_div(a, b);
  1194.                 a.full = dfixed_const(src_h);
  1195.                 b.full = dfixed_const(dst_h);
  1196.                 radeon_crtc->hsc.full = dfixed_div(a, b);
  1197.         } else {
  1198.                 radeon_crtc->vsc.full = dfixed_const(1);
  1199.                 radeon_crtc->hsc.full = dfixed_const(1);
  1200.         }
  1201.         return true;
  1202. }
  1203.  
  1204. /*
  1205.  * Retrieve current video scanout position of crtc on a given gpu.
  1206.  *
  1207.  * \param dev Device to query.
  1208.  * \param crtc Crtc to query.
  1209.  * \param *vpos Location where vertical scanout position should be stored.
  1210.  * \param *hpos Location where horizontal scanout position should go.
  1211.  *
  1212.  * Returns vpos as a positive number while in active scanout area.
  1213.  * Returns vpos as a negative number inside vblank, counting the number
  1214.  * of scanlines to go until end of vblank, e.g., -1 means "one scanline
  1215.  * until start of active scanout / end of vblank."
  1216.  *
  1217.  * \return Flags, or'ed together as follows:
  1218.  *
  1219.  * DRM_SCANOUTPOS_VALID = Query successful.
  1220.  * DRM_SCANOUTPOS_INVBL = Inside vblank.
  1221.  * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
  1222.  * this flag means that returned position may be offset by a constant but
  1223.  * unknown small number of scanlines wrt. real scanout position.
  1224.  *
  1225.  */
  1226. int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc, int *vpos, int *hpos)
  1227. {
  1228.         u32 stat_crtc = 0, vbl = 0, position = 0;
  1229.         int vbl_start, vbl_end, vtotal, ret = 0;
  1230.         bool in_vbl = true;
  1231.  
  1232.         struct radeon_device *rdev = dev->dev_private;
  1233.  
  1234.         if (ASIC_IS_DCE4(rdev)) {
  1235.                 if (crtc == 0) {
  1236.                         vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1237.                                      EVERGREEN_CRTC0_REGISTER_OFFSET);
  1238.                         position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1239.                                           EVERGREEN_CRTC0_REGISTER_OFFSET);
  1240.                         ret |= DRM_SCANOUTPOS_VALID;
  1241.                 }
  1242.                 if (crtc == 1) {
  1243.                         vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1244.                                      EVERGREEN_CRTC1_REGISTER_OFFSET);
  1245.                         position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1246.                                           EVERGREEN_CRTC1_REGISTER_OFFSET);
  1247.                         ret |= DRM_SCANOUTPOS_VALID;
  1248.                 }
  1249.                 if (crtc == 2) {
  1250.                         vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1251.                                      EVERGREEN_CRTC2_REGISTER_OFFSET);
  1252.                         position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1253.                                           EVERGREEN_CRTC2_REGISTER_OFFSET);
  1254.                         ret |= DRM_SCANOUTPOS_VALID;
  1255.                 }
  1256.                 if (crtc == 3) {
  1257.                         vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1258.                                      EVERGREEN_CRTC3_REGISTER_OFFSET);
  1259.                         position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1260.                                           EVERGREEN_CRTC3_REGISTER_OFFSET);
  1261.                         ret |= DRM_SCANOUTPOS_VALID;
  1262.                 }
  1263.                 if (crtc == 4) {
  1264.                         vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1265.                                      EVERGREEN_CRTC4_REGISTER_OFFSET);
  1266.                         position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1267.                                           EVERGREEN_CRTC4_REGISTER_OFFSET);
  1268.                         ret |= DRM_SCANOUTPOS_VALID;
  1269.                 }
  1270.                 if (crtc == 5) {
  1271.                         vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1272.                                      EVERGREEN_CRTC5_REGISTER_OFFSET);
  1273.                         position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1274.                                           EVERGREEN_CRTC5_REGISTER_OFFSET);
  1275.                         ret |= DRM_SCANOUTPOS_VALID;
  1276.                 }
  1277.         } else if (ASIC_IS_AVIVO(rdev)) {
  1278.                 if (crtc == 0) {
  1279.                         vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END);
  1280.                         position = RREG32(AVIVO_D1CRTC_STATUS_POSITION);
  1281.                         ret |= DRM_SCANOUTPOS_VALID;
  1282.                 }
  1283.                 if (crtc == 1) {
  1284.                         vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END);
  1285.                         position = RREG32(AVIVO_D2CRTC_STATUS_POSITION);
  1286.                         ret |= DRM_SCANOUTPOS_VALID;
  1287.                 }
  1288.         } else {
  1289.                 /* Pre-AVIVO: Different encoding of scanout pos and vblank interval. */
  1290.                 if (crtc == 0) {
  1291.                         /* Assume vbl_end == 0, get vbl_start from
  1292.                          * upper 16 bits.
  1293.                          */
  1294.                         vbl = (RREG32(RADEON_CRTC_V_TOTAL_DISP) &
  1295.                                 RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
  1296.                         /* Only retrieve vpos from upper 16 bits, set hpos == 0. */
  1297.                         position = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
  1298.                         stat_crtc = RREG32(RADEON_CRTC_STATUS);
  1299.                         if (!(stat_crtc & 1))
  1300.                                 in_vbl = false;
  1301.  
  1302.                         ret |= DRM_SCANOUTPOS_VALID;
  1303.                 }
  1304.                 if (crtc == 1) {
  1305.                         vbl = (RREG32(RADEON_CRTC2_V_TOTAL_DISP) &
  1306.                                 RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
  1307.                         position = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
  1308.                         stat_crtc = RREG32(RADEON_CRTC2_STATUS);
  1309.                         if (!(stat_crtc & 1))
  1310.                                 in_vbl = false;
  1311.  
  1312.                         ret |= DRM_SCANOUTPOS_VALID;
  1313.                 }
  1314.         }
  1315.  
  1316.         /* Decode into vertical and horizontal scanout position. */
  1317.         *vpos = position & 0x1fff;
  1318.         *hpos = (position >> 16) & 0x1fff;
  1319.  
  1320.         /* Valid vblank area boundaries from gpu retrieved? */
  1321.         if (vbl > 0) {
  1322.                 /* Yes: Decode. */
  1323.                 ret |= DRM_SCANOUTPOS_ACCURATE;
  1324.                 vbl_start = vbl & 0x1fff;
  1325.                 vbl_end = (vbl >> 16) & 0x1fff;
  1326.         }
  1327.         else {
  1328.                 /* No: Fake something reasonable which gives at least ok results. */
  1329.                 vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
  1330.                 vbl_end = 0;
  1331.         }
  1332.  
  1333.         /* Test scanout position against vblank region. */
  1334.         if ((*vpos < vbl_start) && (*vpos >= vbl_end))
  1335.                 in_vbl = false;
  1336.  
  1337.         /* Check if inside vblank area and apply corrective offsets:
  1338.          * vpos will then be >=0 in video scanout area, but negative
  1339.          * within vblank area, counting down the number of lines until
  1340.          * start of scanout.
  1341.          */
  1342.  
  1343.         /* Inside "upper part" of vblank area? Apply corrective offset if so: */
  1344.         if (in_vbl && (*vpos >= vbl_start)) {
  1345.                 vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
  1346.                 *vpos = *vpos - vtotal;
  1347.         }
  1348.  
  1349.         /* Correct for shifted end of vbl at vbl_end. */
  1350.         *vpos = *vpos - vbl_end;
  1351.  
  1352.         /* In vblank? */
  1353.         if (in_vbl)
  1354.                 ret |= DRM_SCANOUTPOS_INVBL;
  1355.  
  1356.         return ret;
  1357. }
  1358.