Subversion Repositories Kolibri OS

Rev

Rev 1963 | Rev 2004 | 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.  
  457. //       list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head)
  458. //           radeon_ddc_dump(drm_connector);
  459.         }
  460.  
  461.         return ret;
  462. }
  463.  
  464. int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
  465. {
  466.         struct drm_device *dev = radeon_connector->base.dev;
  467.         struct radeon_device *rdev = dev->dev_private;
  468.         int ret = 0;
  469.  
  470.         /* on hw with routers, select right port */
  471.         if (radeon_connector->router.ddc_valid)
  472.                 radeon_router_select_ddc_port(radeon_connector);
  473.  
  474.         if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
  475.             (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)) {
  476.                 struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
  477.                 if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
  478.                      dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) && dig->dp_i2c_bus)
  479.                         radeon_connector->edid = drm_get_edid(&radeon_connector->base, &dig->dp_i2c_bus->adapter);
  480.         }
  481.         if (!radeon_connector->ddc_bus)
  482.                 return -1;
  483.         if (!radeon_connector->edid) {
  484.                 radeon_connector->edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
  485.         }
  486.  
  487.         if (!radeon_connector->edid) {
  488.                 if (rdev->is_atom_bios) {
  489.                         /* some laptops provide a hardcoded edid in rom for LCDs */
  490.                         if (((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_LVDS) ||
  491.                              (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)))
  492.                                 radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
  493.                 } else
  494.         /* some servers provide a hardcoded edid in rom for KVMs */
  495.                         radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
  496.         }
  497.         if (radeon_connector->edid) {
  498.                 drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
  499.                 ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
  500.                 return ret;
  501.         }
  502.         drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
  503.         return 0;
  504. }
  505.  
  506. static int radeon_ddc_dump(struct drm_connector *connector)
  507. {
  508.         struct edid *edid;
  509.         struct radeon_connector *radeon_connector = to_radeon_connector(connector);
  510.         int ret = 0;
  511.  
  512.         /* on hw with routers, select right port */
  513.         if (radeon_connector->router.ddc_valid)
  514.                 radeon_router_select_ddc_port(radeon_connector);
  515.  
  516.         if (!radeon_connector->ddc_bus)
  517.                 return -1;
  518.         edid = drm_get_edid(connector, &radeon_connector->ddc_bus->adapter);
  519.         if (edid) {
  520.                 kfree(edid);
  521.         }
  522.         return ret;
  523. }
  524.  
  525. /* avivo */
  526. static void avivo_get_fb_div(struct radeon_pll *pll,
  527.                              u32 target_clock,
  528.                              u32 post_div,
  529.                              u32 ref_div,
  530.                              u32 *fb_div,
  531.                              u32 *frac_fb_div)
  532. {
  533.         u32 tmp = post_div * ref_div;
  534.  
  535.         tmp *= target_clock;
  536.         *fb_div = tmp / pll->reference_freq;
  537.         *frac_fb_div = tmp % pll->reference_freq;
  538.  
  539.         if (*fb_div > pll->max_feedback_div)
  540.                 *fb_div = pll->max_feedback_div;
  541.         else if (*fb_div < pll->min_feedback_div)
  542.                 *fb_div = pll->min_feedback_div;
  543. }
  544.  
  545. static u32 avivo_get_post_div(struct radeon_pll *pll,
  546.                               u32 target_clock)
  547. {
  548.         u32 vco, post_div, tmp;
  549.  
  550.         if (pll->flags & RADEON_PLL_USE_POST_DIV)
  551.                 return pll->post_div;
  552.  
  553.         if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP) {
  554.                 if (pll->flags & RADEON_PLL_IS_LCD)
  555.                         vco = pll->lcd_pll_out_min;
  556.                 else
  557.                         vco = pll->pll_out_min;
  558.         } else {
  559.                 if (pll->flags & RADEON_PLL_IS_LCD)
  560.                         vco = pll->lcd_pll_out_max;
  561.                 else
  562.                         vco = pll->pll_out_max;
  563.         }
  564.  
  565.         post_div = vco / target_clock;
  566.         tmp = vco % target_clock;
  567.  
  568.         if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP) {
  569.                 if (tmp)
  570.                         post_div++;
  571.         } else {
  572.                 if (!tmp)
  573.                         post_div--;
  574.         }
  575.  
  576.         if (post_div > pll->max_post_div)
  577.                 post_div = pll->max_post_div;
  578.         else if (post_div < pll->min_post_div)
  579.                 post_div = pll->min_post_div;
  580.  
  581.         return post_div;
  582. }
  583.  
  584. #define MAX_TOLERANCE 10
  585.  
  586. void radeon_compute_pll_avivo(struct radeon_pll *pll,
  587.                               u32 freq,
  588.                               u32 *dot_clock_p,
  589.                               u32 *fb_div_p,
  590.                               u32 *frac_fb_div_p,
  591.                               u32 *ref_div_p,
  592.                               u32 *post_div_p)
  593. {
  594.         u32 target_clock = freq / 10;
  595.         u32 post_div = avivo_get_post_div(pll, target_clock);
  596.         u32 ref_div = pll->min_ref_div;
  597.         u32 fb_div = 0, frac_fb_div = 0, tmp;
  598.  
  599.         if (pll->flags & RADEON_PLL_USE_REF_DIV)
  600.                 ref_div = pll->reference_div;
  601.  
  602.         if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  603.                 avivo_get_fb_div(pll, target_clock, post_div, ref_div, &fb_div, &frac_fb_div);
  604.                 frac_fb_div = (100 * frac_fb_div) / pll->reference_freq;
  605.                 if (frac_fb_div >= 5) {
  606.                         frac_fb_div -= 5;
  607.                         frac_fb_div = frac_fb_div / 10;
  608.                         frac_fb_div++;
  609.                 }
  610.                 if (frac_fb_div >= 10) {
  611.                         fb_div++;
  612.                         frac_fb_div = 0;
  613.                 }
  614.         } else {
  615.                 while (ref_div <= pll->max_ref_div) {
  616.                         avivo_get_fb_div(pll, target_clock, post_div, ref_div,
  617.                                          &fb_div, &frac_fb_div);
  618.                         if (frac_fb_div >= (pll->reference_freq / 2))
  619.                                 fb_div++;
  620.                         frac_fb_div = 0;
  621.                         tmp = (pll->reference_freq * fb_div) / (post_div * ref_div);
  622.                         tmp = (tmp * 10000) / target_clock;
  623.  
  624.                         if (tmp > (10000 + MAX_TOLERANCE))
  625.                                 ref_div++;
  626.                         else if (tmp >= (10000 - MAX_TOLERANCE))
  627.                                 break;
  628.                         else
  629.                                 ref_div++;
  630.                 }
  631.         }
  632.  
  633.         *dot_clock_p = ((pll->reference_freq * fb_div * 10) + (pll->reference_freq * frac_fb_div)) /
  634.                 (ref_div * post_div * 10);
  635.         *fb_div_p = fb_div;
  636.         *frac_fb_div_p = frac_fb_div;
  637.         *ref_div_p = ref_div;
  638.         *post_div_p = post_div;
  639.         DRM_DEBUG_KMS("%d, pll dividers - fb: %d.%d ref: %d, post %d\n",
  640.                       *dot_clock_p, fb_div, frac_fb_div, ref_div, post_div);
  641. }
  642.  
  643. /* pre-avivo */
  644. static inline uint32_t radeon_div(uint64_t n, uint32_t d)
  645. {
  646.         uint64_t mod;
  647.  
  648.         n += d / 2;
  649.  
  650.         mod = do_div(n, d);
  651.         return n;
  652. }
  653.  
  654. void radeon_compute_pll_legacy(struct radeon_pll *pll,
  655.                         uint64_t freq,
  656.                         uint32_t *dot_clock_p,
  657.                         uint32_t *fb_div_p,
  658.                         uint32_t *frac_fb_div_p,
  659.                         uint32_t *ref_div_p,
  660.                         uint32_t *post_div_p)
  661. {
  662.         uint32_t min_ref_div = pll->min_ref_div;
  663.         uint32_t max_ref_div = pll->max_ref_div;
  664.         uint32_t min_post_div = pll->min_post_div;
  665.         uint32_t max_post_div = pll->max_post_div;
  666.         uint32_t min_fractional_feed_div = 0;
  667.         uint32_t max_fractional_feed_div = 0;
  668.         uint32_t best_vco = pll->best_vco;
  669.         uint32_t best_post_div = 1;
  670.         uint32_t best_ref_div = 1;
  671.         uint32_t best_feedback_div = 1;
  672.         uint32_t best_frac_feedback_div = 0;
  673.         uint32_t best_freq = -1;
  674.         uint32_t best_error = 0xffffffff;
  675.         uint32_t best_vco_diff = 1;
  676.         uint32_t post_div;
  677.         u32 pll_out_min, pll_out_max;
  678.  
  679.         DRM_DEBUG_KMS("PLL freq %llu %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
  680.         freq = freq * 1000;
  681.  
  682.         if (pll->flags & RADEON_PLL_IS_LCD) {
  683.                 pll_out_min = pll->lcd_pll_out_min;
  684.                 pll_out_max = pll->lcd_pll_out_max;
  685.         } else {
  686.                 pll_out_min = pll->pll_out_min;
  687.                 pll_out_max = pll->pll_out_max;
  688.         }
  689.  
  690.         if (pll_out_min > 64800)
  691.                 pll_out_min = 64800;
  692.  
  693.         if (pll->flags & RADEON_PLL_USE_REF_DIV)
  694.                 min_ref_div = max_ref_div = pll->reference_div;
  695.         else {
  696.                 while (min_ref_div < max_ref_div-1) {
  697.                         uint32_t mid = (min_ref_div + max_ref_div) / 2;
  698.                         uint32_t pll_in = pll->reference_freq / mid;
  699.                         if (pll_in < pll->pll_in_min)
  700.                                 max_ref_div = mid;
  701.                         else if (pll_in > pll->pll_in_max)
  702.                                 min_ref_div = mid;
  703.                         else
  704.                                 break;
  705.                 }
  706.         }
  707.  
  708.         if (pll->flags & RADEON_PLL_USE_POST_DIV)
  709.                 min_post_div = max_post_div = pll->post_div;
  710.  
  711.         if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  712.                 min_fractional_feed_div = pll->min_frac_feedback_div;
  713.                 max_fractional_feed_div = pll->max_frac_feedback_div;
  714.         }
  715.  
  716.         for (post_div = max_post_div; post_div >= min_post_div; --post_div) {
  717.                 uint32_t ref_div;
  718.  
  719.                 if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
  720.                         continue;
  721.  
  722.                 /* legacy radeons only have a few post_divs */
  723.                 if (pll->flags & RADEON_PLL_LEGACY) {
  724.                         if ((post_div == 5) ||
  725.                             (post_div == 7) ||
  726.                             (post_div == 9) ||
  727.                             (post_div == 10) ||
  728.                             (post_div == 11) ||
  729.                             (post_div == 13) ||
  730.                             (post_div == 14) ||
  731.                             (post_div == 15))
  732.                                 continue;
  733.                 }
  734.  
  735.                 for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
  736.                         uint32_t feedback_div, current_freq = 0, error, vco_diff;
  737.                         uint32_t pll_in = pll->reference_freq / ref_div;
  738.                         uint32_t min_feed_div = pll->min_feedback_div;
  739.                         uint32_t max_feed_div = pll->max_feedback_div + 1;
  740.  
  741.                         if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
  742.                                 continue;
  743.  
  744.                         while (min_feed_div < max_feed_div) {
  745.                                 uint32_t vco;
  746.                                 uint32_t min_frac_feed_div = min_fractional_feed_div;
  747.                                 uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
  748.                                 uint32_t frac_feedback_div;
  749.                                 uint64_t tmp;
  750.  
  751.                                 feedback_div = (min_feed_div + max_feed_div) / 2;
  752.  
  753.                                 tmp = (uint64_t)pll->reference_freq * feedback_div;
  754.                                 vco = radeon_div(tmp, ref_div);
  755.  
  756.                                 if (vco < pll_out_min) {
  757.                                         min_feed_div = feedback_div + 1;
  758.                                         continue;
  759.                                 } else if (vco > pll_out_max) {
  760.                                         max_feed_div = feedback_div;
  761.                                         continue;
  762.                                 }
  763.  
  764.                                 while (min_frac_feed_div < max_frac_feed_div) {
  765.                                         frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
  766.                                         tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
  767.                                         tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
  768.                                         current_freq = radeon_div(tmp, ref_div * post_div);
  769.  
  770.                                         if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
  771.                                                 if (freq < current_freq)
  772.                                                         error = 0xffffffff;
  773.                                                 else
  774.                                                 error = freq - current_freq;
  775.                                         } else
  776.                                         error = abs(current_freq - freq);
  777.                                         vco_diff = abs(vco - best_vco);
  778.  
  779.                                         if ((best_vco == 0 && error < best_error) ||
  780.                                             (best_vco != 0 &&
  781.                                              ((best_error > 100 && error < best_error - 100) ||
  782.                                               (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
  783.                                                 best_post_div = post_div;
  784.                                                 best_ref_div = ref_div;
  785.                                                 best_feedback_div = feedback_div;
  786.                                                 best_frac_feedback_div = frac_feedback_div;
  787.                                                 best_freq = current_freq;
  788.                                                 best_error = error;
  789.                                                 best_vco_diff = vco_diff;
  790.                                         } else if (current_freq == freq) {
  791.                                                 if (best_freq == -1) {
  792.                                                         best_post_div = post_div;
  793.                                                         best_ref_div = ref_div;
  794.                                                         best_feedback_div = feedback_div;
  795.                                                         best_frac_feedback_div = frac_feedback_div;
  796.                                                         best_freq = current_freq;
  797.                                                         best_error = error;
  798.                                                         best_vco_diff = vco_diff;
  799.                                                 } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
  800.                                                            ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
  801.                                                            ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
  802.                                                            ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
  803.                                                            ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
  804.                                                            ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
  805.                                                         best_post_div = post_div;
  806.                                                         best_ref_div = ref_div;
  807.                                                         best_feedback_div = feedback_div;
  808.                                                         best_frac_feedback_div = frac_feedback_div;
  809.                                                         best_freq = current_freq;
  810.                                                         best_error = error;
  811.                                                         best_vco_diff = vco_diff;
  812.                                                 }
  813.                                         }
  814.                                         if (current_freq < freq)
  815.                                                 min_frac_feed_div = frac_feedback_div + 1;
  816.                                         else
  817.                                                 max_frac_feed_div = frac_feedback_div;
  818.                                 }
  819.                                 if (current_freq < freq)
  820.                                         min_feed_div = feedback_div + 1;
  821.                                 else
  822.                                         max_feed_div = feedback_div;
  823.                         }
  824.                 }
  825.         }
  826.  
  827.         *dot_clock_p = best_freq / 10000;
  828.         *fb_div_p = best_feedback_div;
  829.         *frac_fb_div_p = best_frac_feedback_div;
  830.         *ref_div_p = best_ref_div;
  831.         *post_div_p = best_post_div;
  832.         DRM_DEBUG_KMS("%lld %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
  833.                       (long long)freq,
  834.                       best_freq / 1000, best_feedback_div, best_frac_feedback_div,
  835.                       best_ref_div, best_post_div);
  836.  
  837. }
  838.  
  839. static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
  840. {
  841.         struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  842.  
  843.         drm_framebuffer_cleanup(fb);
  844.         kfree(radeon_fb);
  845. }
  846.  
  847. static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  848.                          struct drm_file *file_priv,
  849.                          unsigned int *handle)
  850. {
  851.    struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  852.  
  853.    return NULL;
  854. //   return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
  855. }
  856.  
  857. static const struct drm_framebuffer_funcs radeon_fb_funcs = {
  858.         .destroy = radeon_user_framebuffer_destroy,
  859.     .create_handle = radeon_user_framebuffer_create_handle,
  860. };
  861.  
  862. void
  863. radeon_framebuffer_init(struct drm_device *dev,
  864.                         struct radeon_framebuffer *rfb,
  865.                           struct drm_mode_fb_cmd *mode_cmd,
  866.                           struct drm_gem_object *obj)
  867. {
  868.         rfb->obj = obj;
  869.         drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
  870.         drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
  871. }
  872.  
  873.  
  874.  
  875. static const struct drm_mode_config_funcs radeon_mode_funcs = {
  876. //      .fb_create = radeon_user_framebuffer_create,
  877. //   .output_poll_changed = radeon_output_poll_changed
  878. };
  879.  
  880. struct drm_prop_enum_list {
  881.         int type;
  882.         char *name;
  883. };
  884.  
  885. static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
  886. {       { 0, "driver" },
  887.         { 1, "bios" },
  888. };
  889.  
  890. static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
  891. {       { TV_STD_NTSC, "ntsc" },
  892.         { TV_STD_PAL, "pal" },
  893.         { TV_STD_PAL_M, "pal-m" },
  894.         { TV_STD_PAL_60, "pal-60" },
  895.         { TV_STD_NTSC_J, "ntsc-j" },
  896.         { TV_STD_SCART_PAL, "scart-pal" },
  897.         { TV_STD_PAL_CN, "pal-cn" },
  898.         { TV_STD_SECAM, "secam" },
  899. };
  900.  
  901. static struct drm_prop_enum_list radeon_underscan_enum_list[] =
  902. {       { UNDERSCAN_OFF, "off" },
  903.         { UNDERSCAN_ON, "on" },
  904.         { UNDERSCAN_AUTO, "auto" },
  905. };
  906.  
  907. static int radeon_modeset_create_props(struct radeon_device *rdev)
  908. {
  909.         int i, sz;
  910.  
  911.         if (rdev->is_atom_bios) {
  912.                 rdev->mode_info.coherent_mode_property =
  913.                         drm_property_create(rdev->ddev,
  914.                                             DRM_MODE_PROP_RANGE,
  915.                                             "coherent", 2);
  916.                 if (!rdev->mode_info.coherent_mode_property)
  917.                         return -ENOMEM;
  918.  
  919.                 rdev->mode_info.coherent_mode_property->values[0] = 0;
  920.                 rdev->mode_info.coherent_mode_property->values[1] = 1;
  921.         }
  922.  
  923.         if (!ASIC_IS_AVIVO(rdev)) {
  924.                 sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
  925.                 rdev->mode_info.tmds_pll_property =
  926.                         drm_property_create(rdev->ddev,
  927.                                             DRM_MODE_PROP_ENUM,
  928.                                             "tmds_pll", sz);
  929.                 for (i = 0; i < sz; i++) {
  930.                         drm_property_add_enum(rdev->mode_info.tmds_pll_property,
  931.                                               i,
  932.                                               radeon_tmds_pll_enum_list[i].type,
  933.                                               radeon_tmds_pll_enum_list[i].name);
  934.                 }
  935.         }
  936.  
  937.         rdev->mode_info.load_detect_property =
  938.                 drm_property_create(rdev->ddev,
  939.                                     DRM_MODE_PROP_RANGE,
  940.                                     "load detection", 2);
  941.         if (!rdev->mode_info.load_detect_property)
  942.                 return -ENOMEM;
  943.         rdev->mode_info.load_detect_property->values[0] = 0;
  944.         rdev->mode_info.load_detect_property->values[1] = 1;
  945.  
  946.         drm_mode_create_scaling_mode_property(rdev->ddev);
  947.  
  948.         sz = ARRAY_SIZE(radeon_tv_std_enum_list);
  949.         rdev->mode_info.tv_std_property =
  950.                 drm_property_create(rdev->ddev,
  951.                                     DRM_MODE_PROP_ENUM,
  952.                                     "tv standard", sz);
  953.         for (i = 0; i < sz; i++) {
  954.                 drm_property_add_enum(rdev->mode_info.tv_std_property,
  955.                                       i,
  956.                                       radeon_tv_std_enum_list[i].type,
  957.                                       radeon_tv_std_enum_list[i].name);
  958.         }
  959.  
  960.         sz = ARRAY_SIZE(radeon_underscan_enum_list);
  961.         rdev->mode_info.underscan_property =
  962.                 drm_property_create(rdev->ddev,
  963.                                     DRM_MODE_PROP_ENUM,
  964.                                     "underscan", sz);
  965.         for (i = 0; i < sz; i++) {
  966.                 drm_property_add_enum(rdev->mode_info.underscan_property,
  967.                                       i,
  968.                                       radeon_underscan_enum_list[i].type,
  969.                                       radeon_underscan_enum_list[i].name);
  970.         }
  971.  
  972.         rdev->mode_info.underscan_hborder_property =
  973.                 drm_property_create(rdev->ddev,
  974.                                         DRM_MODE_PROP_RANGE,
  975.                                         "underscan hborder", 2);
  976.         if (!rdev->mode_info.underscan_hborder_property)
  977.                 return -ENOMEM;
  978.         rdev->mode_info.underscan_hborder_property->values[0] = 0;
  979.         rdev->mode_info.underscan_hborder_property->values[1] = 128;
  980.  
  981.         rdev->mode_info.underscan_vborder_property =
  982.                 drm_property_create(rdev->ddev,
  983.                                         DRM_MODE_PROP_RANGE,
  984.                                         "underscan vborder", 2);
  985.         if (!rdev->mode_info.underscan_vborder_property)
  986.                 return -ENOMEM;
  987.         rdev->mode_info.underscan_vborder_property->values[0] = 0;
  988.         rdev->mode_info.underscan_vborder_property->values[1] = 128;
  989.  
  990.         return 0;
  991. }
  992.  
  993. void radeon_update_display_priority(struct radeon_device *rdev)
  994. {
  995.         /* adjustment options for the display watermarks */
  996.         if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
  997.                 /* set display priority to high for r3xx, rv515 chips
  998.                  * this avoids flickering due to underflow to the
  999.                  * display controllers during heavy acceleration.
  1000.                  * Don't force high on rs4xx igp chips as it seems to
  1001.                  * affect the sound card.  See kernel bug 15982.
  1002.                  */
  1003.                 if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
  1004.                     !(rdev->flags & RADEON_IS_IGP))
  1005.                         rdev->disp_priority = 2;
  1006.                 else
  1007.                         rdev->disp_priority = 0;
  1008.         } else
  1009.                 rdev->disp_priority = radeon_disp_priority;
  1010.  
  1011. }
  1012.  
  1013. int radeon_modeset_init(struct radeon_device *rdev)
  1014. {
  1015.         int i;
  1016.         int ret;
  1017.  
  1018.     ENTER();
  1019.  
  1020.         drm_mode_config_init(rdev->ddev);
  1021.         rdev->mode_info.mode_config_initialized = true;
  1022.  
  1023.     rdev->ddev->mode_config.funcs = (void *)&radeon_mode_funcs;
  1024.  
  1025.         if (ASIC_IS_DCE5(rdev)) {
  1026.                 rdev->ddev->mode_config.max_width = 16384;
  1027.                 rdev->ddev->mode_config.max_height = 16384;
  1028.         } else if (ASIC_IS_AVIVO(rdev)) {
  1029.                 rdev->ddev->mode_config.max_width = 8192;
  1030.                 rdev->ddev->mode_config.max_height = 8192;
  1031.         } else {
  1032.                 rdev->ddev->mode_config.max_width = 4096;
  1033.                 rdev->ddev->mode_config.max_height = 4096;
  1034.         }
  1035.  
  1036.         rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
  1037.  
  1038.         ret = radeon_modeset_create_props(rdev);
  1039.         if (ret) {
  1040.                 return ret;
  1041.         }
  1042.  
  1043.         /* init i2c buses */
  1044.         radeon_i2c_init(rdev);
  1045.  
  1046.         /* check combios for a valid hardcoded EDID - Sun servers */
  1047.         if (!rdev->is_atom_bios) {
  1048.                 /* check for hardcoded EDID in BIOS */
  1049.                 radeon_combios_check_hardcoded_edid(rdev);
  1050.         }
  1051.  
  1052.         /* allocate crtcs */
  1053.         for (i = 0; i < rdev->num_crtc; i++) {
  1054.                 radeon_crtc_init(rdev->ddev, i);
  1055.         }
  1056.  
  1057.         /* okay we should have all the bios connectors */
  1058.         ret = radeon_setup_enc_conn(rdev->ddev);
  1059.         if (!ret) {
  1060.                 return ret;
  1061.         }
  1062.  
  1063.         /* init dig PHYs */
  1064.         if (rdev->is_atom_bios)
  1065.                 radeon_atom_encoder_init(rdev);
  1066.  
  1067.         /* initialize hpd */
  1068. //   radeon_hpd_init(rdev);
  1069.  
  1070.         /* Initialize power management */
  1071. //   radeon_pm_init(rdev);
  1072.  
  1073.         radeon_fbdev_init(rdev);
  1074. //   drm_kms_helper_poll_init(rdev->ddev);
  1075.  
  1076.     LEAVE();
  1077.  
  1078.         return 0;
  1079. }
  1080.  
  1081. void radeon_modeset_fini(struct radeon_device *rdev)
  1082. {
  1083.         kfree(rdev->mode_info.bios_hardcoded_edid);
  1084.  
  1085.         if (rdev->mode_info.mode_config_initialized) {
  1086. //       drm_kms_helper_poll_fini(rdev->ddev);
  1087. //       radeon_hpd_fini(rdev);
  1088.                 drm_mode_config_cleanup(rdev->ddev);
  1089.                 rdev->mode_info.mode_config_initialized = false;
  1090.         }
  1091.         /* free i2c buses */
  1092.         radeon_i2c_fini(rdev);
  1093. }
  1094.  
  1095. static bool is_hdtv_mode(struct drm_display_mode *mode)
  1096. {
  1097.         /* try and guess if this is a tv or a monitor */
  1098.         if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
  1099.             (mode->vdisplay == 576) || /* 576p */
  1100.             (mode->vdisplay == 720) || /* 720p */
  1101.             (mode->vdisplay == 1080)) /* 1080p */
  1102.                 return true;
  1103.         else
  1104.                 return false;
  1105. }
  1106.  
  1107. bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
  1108.                                 struct drm_display_mode *mode,
  1109.                                 struct drm_display_mode *adjusted_mode)
  1110. {
  1111.         struct drm_device *dev = crtc->dev;
  1112.         struct radeon_device *rdev = dev->dev_private;
  1113.         struct drm_encoder *encoder;
  1114.                 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1115.         struct radeon_encoder *radeon_encoder;
  1116.         struct drm_connector *connector;
  1117.         struct radeon_connector *radeon_connector;
  1118.         bool first = true;
  1119.         u32 src_v = 1, dst_v = 1;
  1120.         u32 src_h = 1, dst_h = 1;
  1121.  
  1122.         radeon_crtc->h_border = 0;
  1123.         radeon_crtc->v_border = 0;
  1124.  
  1125.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  1126.                 if (encoder->crtc != crtc)
  1127.                         continue;
  1128.                 radeon_encoder = to_radeon_encoder(encoder);
  1129.                 connector = radeon_get_connector_for_encoder(encoder);
  1130.                 radeon_connector = to_radeon_connector(connector);
  1131.  
  1132.                 if (first) {
  1133.                         /* set scaling */
  1134.                         if (radeon_encoder->rmx_type == RMX_OFF)
  1135.                                 radeon_crtc->rmx_type = RMX_OFF;
  1136.                         else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
  1137.                                  mode->vdisplay < radeon_encoder->native_mode.vdisplay)
  1138.                         radeon_crtc->rmx_type = radeon_encoder->rmx_type;
  1139.                         else
  1140.                                 radeon_crtc->rmx_type = RMX_OFF;
  1141.                         /* copy native mode */
  1142.                         memcpy(&radeon_crtc->native_mode,
  1143.                                 &radeon_encoder->native_mode,
  1144.                                 sizeof(struct drm_display_mode));
  1145.                         src_v = crtc->mode.vdisplay;
  1146.                         dst_v = radeon_crtc->native_mode.vdisplay;
  1147.                         src_h = crtc->mode.hdisplay;
  1148.                         dst_h = radeon_crtc->native_mode.hdisplay;
  1149.  
  1150.                         /* fix up for overscan on hdmi */
  1151.                         if (ASIC_IS_AVIVO(rdev) &&
  1152.                             (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
  1153.                             ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
  1154.                              ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
  1155.                               drm_detect_hdmi_monitor(radeon_connector->edid) &&
  1156.                               is_hdtv_mode(mode)))) {
  1157.                                 if (radeon_encoder->underscan_hborder != 0)
  1158.                                         radeon_crtc->h_border = radeon_encoder->underscan_hborder;
  1159.                                 else
  1160.                                 radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
  1161.                                 if (radeon_encoder->underscan_vborder != 0)
  1162.                                         radeon_crtc->v_border = radeon_encoder->underscan_vborder;
  1163.                                 else
  1164.                                 radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
  1165.                                 radeon_crtc->rmx_type = RMX_FULL;
  1166.                                 src_v = crtc->mode.vdisplay;
  1167.                                 dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
  1168.                                 src_h = crtc->mode.hdisplay;
  1169.                                 dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
  1170.                         }
  1171.                         first = false;
  1172.                 } else {
  1173.                         if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
  1174.                                 /* WARNING: Right now this can't happen but
  1175.                                  * in the future we need to check that scaling
  1176.                                  * are consistent across different encoder
  1177.                                  * (ie all encoder can work with the same
  1178.                                  *  scaling).
  1179.                                  */
  1180.                                 DRM_ERROR("Scaling not consistent across encoder.\n");
  1181.                                 return false;
  1182.                         }
  1183.                 }
  1184.         }
  1185.         if (radeon_crtc->rmx_type != RMX_OFF) {
  1186.         fixed20_12 a, b;
  1187.                 a.full = dfixed_const(src_v);
  1188.                 b.full = dfixed_const(dst_v);
  1189.                 radeon_crtc->vsc.full = dfixed_div(a, b);
  1190.                 a.full = dfixed_const(src_h);
  1191.                 b.full = dfixed_const(dst_h);
  1192.                 radeon_crtc->hsc.full = dfixed_div(a, b);
  1193.         } else {
  1194.                 radeon_crtc->vsc.full = dfixed_const(1);
  1195.                 radeon_crtc->hsc.full = dfixed_const(1);
  1196.         }
  1197.         return true;
  1198. }
  1199.  
  1200. /*
  1201.  * Retrieve current video scanout position of crtc on a given gpu.
  1202.  *
  1203.  * \param dev Device to query.
  1204.  * \param crtc Crtc to query.
  1205.  * \param *vpos Location where vertical scanout position should be stored.
  1206.  * \param *hpos Location where horizontal scanout position should go.
  1207.  *
  1208.  * Returns vpos as a positive number while in active scanout area.
  1209.  * Returns vpos as a negative number inside vblank, counting the number
  1210.  * of scanlines to go until end of vblank, e.g., -1 means "one scanline
  1211.  * until start of active scanout / end of vblank."
  1212.  *
  1213.  * \return Flags, or'ed together as follows:
  1214.  *
  1215.  * DRM_SCANOUTPOS_VALID = Query successful.
  1216.  * DRM_SCANOUTPOS_INVBL = Inside vblank.
  1217.  * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
  1218.  * this flag means that returned position may be offset by a constant but
  1219.  * unknown small number of scanlines wrt. real scanout position.
  1220.  *
  1221.  */
  1222. int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc, int *vpos, int *hpos)
  1223. {
  1224.         u32 stat_crtc = 0, vbl = 0, position = 0;
  1225.         int vbl_start, vbl_end, vtotal, ret = 0;
  1226.         bool in_vbl = true;
  1227.  
  1228.         struct radeon_device *rdev = dev->dev_private;
  1229.  
  1230.         if (ASIC_IS_DCE4(rdev)) {
  1231.                 if (crtc == 0) {
  1232.                         vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1233.                                      EVERGREEN_CRTC0_REGISTER_OFFSET);
  1234.                         position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1235.                                           EVERGREEN_CRTC0_REGISTER_OFFSET);
  1236.                         ret |= DRM_SCANOUTPOS_VALID;
  1237.                 }
  1238.                 if (crtc == 1) {
  1239.                         vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1240.                                      EVERGREEN_CRTC1_REGISTER_OFFSET);
  1241.                         position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1242.                                           EVERGREEN_CRTC1_REGISTER_OFFSET);
  1243.                         ret |= DRM_SCANOUTPOS_VALID;
  1244.                 }
  1245.                 if (crtc == 2) {
  1246.                         vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1247.                                      EVERGREEN_CRTC2_REGISTER_OFFSET);
  1248.                         position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1249.                                           EVERGREEN_CRTC2_REGISTER_OFFSET);
  1250.                         ret |= DRM_SCANOUTPOS_VALID;
  1251.                 }
  1252.                 if (crtc == 3) {
  1253.                         vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1254.                                      EVERGREEN_CRTC3_REGISTER_OFFSET);
  1255.                         position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1256.                                           EVERGREEN_CRTC3_REGISTER_OFFSET);
  1257.                         ret |= DRM_SCANOUTPOS_VALID;
  1258.                 }
  1259.                 if (crtc == 4) {
  1260.                         vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1261.                                      EVERGREEN_CRTC4_REGISTER_OFFSET);
  1262.                         position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1263.                                           EVERGREEN_CRTC4_REGISTER_OFFSET);
  1264.                         ret |= DRM_SCANOUTPOS_VALID;
  1265.                 }
  1266.                 if (crtc == 5) {
  1267.                         vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1268.                                      EVERGREEN_CRTC5_REGISTER_OFFSET);
  1269.                         position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1270.                                           EVERGREEN_CRTC5_REGISTER_OFFSET);
  1271.                         ret |= DRM_SCANOUTPOS_VALID;
  1272.                 }
  1273.         } else if (ASIC_IS_AVIVO(rdev)) {
  1274.                 if (crtc == 0) {
  1275.                         vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END);
  1276.                         position = RREG32(AVIVO_D1CRTC_STATUS_POSITION);
  1277.                         ret |= DRM_SCANOUTPOS_VALID;
  1278.                 }
  1279.                 if (crtc == 1) {
  1280.                         vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END);
  1281.                         position = RREG32(AVIVO_D2CRTC_STATUS_POSITION);
  1282.                         ret |= DRM_SCANOUTPOS_VALID;
  1283.                 }
  1284.         } else {
  1285.                 /* Pre-AVIVO: Different encoding of scanout pos and vblank interval. */
  1286.                 if (crtc == 0) {
  1287.                         /* Assume vbl_end == 0, get vbl_start from
  1288.                          * upper 16 bits.
  1289.                          */
  1290.                         vbl = (RREG32(RADEON_CRTC_V_TOTAL_DISP) &
  1291.                                 RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
  1292.                         /* Only retrieve vpos from upper 16 bits, set hpos == 0. */
  1293.                         position = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
  1294.                         stat_crtc = RREG32(RADEON_CRTC_STATUS);
  1295.                         if (!(stat_crtc & 1))
  1296.                                 in_vbl = false;
  1297.  
  1298.                         ret |= DRM_SCANOUTPOS_VALID;
  1299.                 }
  1300.                 if (crtc == 1) {
  1301.                         vbl = (RREG32(RADEON_CRTC2_V_TOTAL_DISP) &
  1302.                                 RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
  1303.                         position = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
  1304.                         stat_crtc = RREG32(RADEON_CRTC2_STATUS);
  1305.                         if (!(stat_crtc & 1))
  1306.                                 in_vbl = false;
  1307.  
  1308.                         ret |= DRM_SCANOUTPOS_VALID;
  1309.                 }
  1310.         }
  1311.  
  1312.         /* Decode into vertical and horizontal scanout position. */
  1313.         *vpos = position & 0x1fff;
  1314.         *hpos = (position >> 16) & 0x1fff;
  1315.  
  1316.         /* Valid vblank area boundaries from gpu retrieved? */
  1317.         if (vbl > 0) {
  1318.                 /* Yes: Decode. */
  1319.                 ret |= DRM_SCANOUTPOS_ACCURATE;
  1320.                 vbl_start = vbl & 0x1fff;
  1321.                 vbl_end = (vbl >> 16) & 0x1fff;
  1322.         }
  1323.         else {
  1324.                 /* No: Fake something reasonable which gives at least ok results. */
  1325.                 vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
  1326.                 vbl_end = 0;
  1327.         }
  1328.  
  1329.         /* Test scanout position against vblank region. */
  1330.         if ((*vpos < vbl_start) && (*vpos >= vbl_end))
  1331.                 in_vbl = false;
  1332.  
  1333.         /* Check if inside vblank area and apply corrective offsets:
  1334.          * vpos will then be >=0 in video scanout area, but negative
  1335.          * within vblank area, counting down the number of lines until
  1336.          * start of scanout.
  1337.          */
  1338.  
  1339.         /* Inside "upper part" of vblank area? Apply corrective offset if so: */
  1340.         if (in_vbl && (*vpos >= vbl_start)) {
  1341.                 vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
  1342.                 *vpos = *vpos - vtotal;
  1343.         }
  1344.  
  1345.         /* Correct for shifted end of vbl at vbl_end. */
  1346.         *vpos = *vpos - vbl_end;
  1347.  
  1348.         /* In vblank? */
  1349.         if (in_vbl)
  1350.                 ret |= DRM_SCANOUTPOS_INVBL;
  1351.  
  1352.         return ret;
  1353. }
  1354.