blob: 2697801e36e7165a77d8c493596b78639aab4e8d [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
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
36static int radeon_ddc_dump(struct drm_connector *connector);
37
38static 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
Dave Airlied9fdaaf2010-08-02 10:42:55 +100045 DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020046 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
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050071static void evergreen_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
Dave Airlied9fdaaf2010-08-02 10:42:55 +100078 DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050079 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
Alex Deucher677d0762010-04-22 22:58:50 -040089 WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
90 WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050091
Alex Deucher677d0762010-04-22 22:58:50 -040092 WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050093 for (i = 0; i < 256; i++) {
Alex Deucher677d0762010-04-22 22:58:50 -040094 WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050095 (radeon_crtc->lut_r[i] << 20) |
96 (radeon_crtc->lut_g[i] << 10) |
97 (radeon_crtc->lut_b[i] << 0));
98 }
99}
100
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200101static void legacy_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 uint32_t dac2_cntl;
108
109 dac2_cntl = RREG32(RADEON_DAC_CNTL2);
110 if (radeon_crtc->crtc_id == 0)
111 dac2_cntl &= (uint32_t)~RADEON_DAC2_PALETTE_ACC_CTL;
112 else
113 dac2_cntl |= RADEON_DAC2_PALETTE_ACC_CTL;
114 WREG32(RADEON_DAC_CNTL2, dac2_cntl);
115
116 WREG8(RADEON_PALETTE_INDEX, 0);
117 for (i = 0; i < 256; i++) {
118 WREG32(RADEON_PALETTE_30_DATA,
119 (radeon_crtc->lut_r[i] << 20) |
120 (radeon_crtc->lut_g[i] << 10) |
121 (radeon_crtc->lut_b[i] << 0));
122 }
123}
124
125void radeon_crtc_load_lut(struct drm_crtc *crtc)
126{
127 struct drm_device *dev = crtc->dev;
128 struct radeon_device *rdev = dev->dev_private;
129
130 if (!crtc->enabled)
131 return;
132
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500133 if (ASIC_IS_DCE4(rdev))
134 evergreen_crtc_load_lut(crtc);
135 else if (ASIC_IS_AVIVO(rdev))
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200136 avivo_crtc_load_lut(crtc);
137 else
138 legacy_crtc_load_lut(crtc);
139}
140
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000141/** Sets the color ramps on behalf of fbcon */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200142void radeon_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
143 u16 blue, int regno)
144{
145 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
146
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200147 radeon_crtc->lut_r[regno] = red >> 6;
148 radeon_crtc->lut_g[regno] = green >> 6;
149 radeon_crtc->lut_b[regno] = blue >> 6;
150}
151
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000152/** Gets the color ramps on behalf of fbcon */
153void radeon_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
154 u16 *blue, int regno)
155{
156 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
157
158 *red = radeon_crtc->lut_r[regno] << 6;
159 *green = radeon_crtc->lut_g[regno] << 6;
160 *blue = radeon_crtc->lut_b[regno] << 6;
161}
162
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200163static void radeon_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +0100164 u16 *blue, uint32_t start, uint32_t size)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200165{
166 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
James Simmons72034252010-08-03 01:33:19 +0100167 int end = (start + size > 256) ? 256 : start + size, i;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200168
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000169 /* userspace palettes are always correct as is */
James Simmons72034252010-08-03 01:33:19 +0100170 for (i = start; i < end; i++) {
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000171 radeon_crtc->lut_r[i] = red[i] >> 6;
172 radeon_crtc->lut_g[i] = green[i] >> 6;
173 radeon_crtc->lut_b[i] = blue[i] >> 6;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200174 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200175 radeon_crtc_load_lut(crtc);
176}
177
178static void radeon_crtc_destroy(struct drm_crtc *crtc)
179{
180 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
181
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200182 drm_crtc_cleanup(crtc);
183 kfree(radeon_crtc);
184}
185
186static const struct drm_crtc_funcs radeon_crtc_funcs = {
187 .cursor_set = radeon_crtc_cursor_set,
188 .cursor_move = radeon_crtc_cursor_move,
189 .gamma_set = radeon_crtc_gamma_set,
190 .set_config = drm_crtc_helper_set_config,
191 .destroy = radeon_crtc_destroy,
192};
193
194static void radeon_crtc_init(struct drm_device *dev, int index)
195{
196 struct radeon_device *rdev = dev->dev_private;
197 struct radeon_crtc *radeon_crtc;
198 int i;
199
200 radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
201 if (radeon_crtc == NULL)
202 return;
203
204 drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
205
206 drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
207 radeon_crtc->crtc_id = index;
Jerome Glissec93bb852009-07-13 21:04:08 +0200208 rdev->mode_info.crtcs[index] = radeon_crtc;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200209
Dave Airlie785b93e2009-08-28 15:46:53 +1000210#if 0
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200211 radeon_crtc->mode_set.crtc = &radeon_crtc->base;
212 radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
213 radeon_crtc->mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000214#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200215
216 for (i = 0; i < 256; i++) {
217 radeon_crtc->lut_r[i] = i << 2;
218 radeon_crtc->lut_g[i] = i << 2;
219 radeon_crtc->lut_b[i] = i << 2;
220 }
221
222 if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
223 radeon_atombios_init_crtc(dev, radeon_crtc);
224 else
225 radeon_legacy_init_crtc(dev, radeon_crtc);
226}
227
Alex Deucherbf982eb2010-11-22 17:56:24 -0500228static const char *encoder_names[36] = {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200229 "NONE",
230 "INTERNAL_LVDS",
231 "INTERNAL_TMDS1",
232 "INTERNAL_TMDS2",
233 "INTERNAL_DAC1",
234 "INTERNAL_DAC2",
235 "INTERNAL_SDVOA",
236 "INTERNAL_SDVOB",
237 "SI170B",
238 "CH7303",
239 "CH7301",
240 "INTERNAL_DVO1",
241 "EXTERNAL_SDVOA",
242 "EXTERNAL_SDVOB",
243 "TITFP513",
244 "INTERNAL_LVTM1",
245 "VT1623",
246 "HDMI_SI1930",
247 "HDMI_INTERNAL",
248 "INTERNAL_KLDSCP_TMDS1",
249 "INTERNAL_KLDSCP_DVO1",
250 "INTERNAL_KLDSCP_DAC1",
251 "INTERNAL_KLDSCP_DAC2",
252 "SI178",
253 "MVPU_FPGA",
254 "INTERNAL_DDI",
255 "VT1625",
256 "HDMI_SI1932",
257 "DP_AN9801",
258 "DP_DP501",
259 "INTERNAL_UNIPHY",
260 "INTERNAL_KLDSCP_LVTMA",
261 "INTERNAL_UNIPHY1",
262 "INTERNAL_UNIPHY2",
Alex Deucherbf982eb2010-11-22 17:56:24 -0500263 "NUTMEG",
264 "TRAVIS",
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200265};
266
Alex Deucher196c58d2010-01-07 14:22:32 -0500267static const char *connector_names[15] = {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200268 "Unknown",
269 "VGA",
270 "DVI-I",
271 "DVI-D",
272 "DVI-A",
273 "Composite",
274 "S-video",
275 "LVDS",
276 "Component",
277 "DIN",
278 "DisplayPort",
279 "HDMI-A",
280 "HDMI-B",
Alex Deucher196c58d2010-01-07 14:22:32 -0500281 "TV",
282 "eDP",
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200283};
284
Alex Deuchercbd46232010-06-07 02:24:54 -0400285static const char *hpd_names[6] = {
Alex Deuchereed45b32009-12-04 14:45:27 -0500286 "HPD1",
287 "HPD2",
288 "HPD3",
289 "HPD4",
290 "HPD5",
291 "HPD6",
292};
293
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200294static void radeon_print_display_setup(struct drm_device *dev)
295{
296 struct drm_connector *connector;
297 struct radeon_connector *radeon_connector;
298 struct drm_encoder *encoder;
299 struct radeon_encoder *radeon_encoder;
300 uint32_t devices;
301 int i = 0;
302
303 DRM_INFO("Radeon Display Connectors\n");
304 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
305 radeon_connector = to_radeon_connector(connector);
306 DRM_INFO("Connector %d:\n", i);
307 DRM_INFO(" %s\n", connector_names[connector->connector_type]);
Alex Deuchereed45b32009-12-04 14:45:27 -0500308 if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
309 DRM_INFO(" %s\n", hpd_names[radeon_connector->hpd.hpd]);
Dave Airlie4b9d2a22010-02-08 13:16:55 +1000310 if (radeon_connector->ddc_bus) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200311 DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
312 radeon_connector->ddc_bus->rec.mask_clk_reg,
313 radeon_connector->ddc_bus->rec.mask_data_reg,
314 radeon_connector->ddc_bus->rec.a_clk_reg,
315 radeon_connector->ddc_bus->rec.a_data_reg,
Alex Deucher9b9fe722009-11-10 15:59:44 -0500316 radeon_connector->ddc_bus->rec.en_clk_reg,
317 radeon_connector->ddc_bus->rec.en_data_reg,
318 radeon_connector->ddc_bus->rec.y_clk_reg,
319 radeon_connector->ddc_bus->rec.y_data_reg);
Alex Deucherfb939df2010-11-08 16:08:29 +0000320 if (radeon_connector->router.ddc_valid)
Alex Deucher26b5bc92010-08-05 21:21:18 -0400321 DRM_INFO(" DDC Router 0x%x/0x%x\n",
Alex Deucherfb939df2010-11-08 16:08:29 +0000322 radeon_connector->router.ddc_mux_control_pin,
323 radeon_connector->router.ddc_mux_state);
324 if (radeon_connector->router.cd_valid)
325 DRM_INFO(" Clock/Data Router 0x%x/0x%x\n",
326 radeon_connector->router.cd_mux_control_pin,
327 radeon_connector->router.cd_mux_state);
Dave Airlie4b9d2a22010-02-08 13:16:55 +1000328 } else {
329 if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
330 connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
331 connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
332 connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
333 connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
334 connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
335 DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
336 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200337 DRM_INFO(" Encoders:\n");
338 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
339 radeon_encoder = to_radeon_encoder(encoder);
340 devices = radeon_encoder->devices & radeon_connector->devices;
341 if (devices) {
342 if (devices & ATOM_DEVICE_CRT1_SUPPORT)
343 DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
344 if (devices & ATOM_DEVICE_CRT2_SUPPORT)
345 DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
346 if (devices & ATOM_DEVICE_LCD1_SUPPORT)
347 DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
348 if (devices & ATOM_DEVICE_DFP1_SUPPORT)
349 DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
350 if (devices & ATOM_DEVICE_DFP2_SUPPORT)
351 DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
352 if (devices & ATOM_DEVICE_DFP3_SUPPORT)
353 DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
354 if (devices & ATOM_DEVICE_DFP4_SUPPORT)
355 DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
356 if (devices & ATOM_DEVICE_DFP5_SUPPORT)
357 DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
Alex Deucher73758a52010-09-24 14:59:32 -0400358 if (devices & ATOM_DEVICE_DFP6_SUPPORT)
359 DRM_INFO(" DFP6: %s\n", encoder_names[radeon_encoder->encoder_id]);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200360 if (devices & ATOM_DEVICE_TV1_SUPPORT)
361 DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
362 if (devices & ATOM_DEVICE_CV_SUPPORT)
363 DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
364 }
365 }
366 i++;
367 }
368}
369
Dave Airlie4ce001a2009-08-13 16:32:14 +1000370static bool radeon_setup_enc_conn(struct drm_device *dev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200371{
372 struct radeon_device *rdev = dev->dev_private;
373 struct drm_connector *drm_connector;
374 bool ret = false;
375
376 if (rdev->bios) {
377 if (rdev->is_atom_bios) {
Alex Deuchera084e6e2010-03-18 01:04:01 -0400378 ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
379 if (ret == false)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200380 ret = radeon_get_atom_connector_info_from_object_table(dev);
Alex Deucherb9597a12010-01-04 19:12:02 -0500381 } else {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200382 ret = radeon_get_legacy_connector_info_from_bios(dev);
Alex Deucherb9597a12010-01-04 19:12:02 -0500383 if (ret == false)
384 ret = radeon_get_legacy_connector_info_from_table(dev);
385 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200386 } else {
387 if (!ASIC_IS_AVIVO(rdev))
388 ret = radeon_get_legacy_connector_info_from_table(dev);
389 }
390 if (ret) {
Dave Airlie1f3b6a42009-10-13 14:10:37 +1000391 radeon_setup_encoder_clones(dev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200392 radeon_print_display_setup(dev);
393 list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head)
394 radeon_ddc_dump(drm_connector);
395 }
396
397 return ret;
398}
399
400int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
401{
Alex Deucher3c537882010-02-05 04:21:19 -0500402 struct drm_device *dev = radeon_connector->base.dev;
403 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200404 int ret = 0;
405
Alex Deucher26b5bc92010-08-05 21:21:18 -0400406 /* on hw with routers, select right port */
Alex Deucherfb939df2010-11-08 16:08:29 +0000407 if (radeon_connector->router.ddc_valid)
408 radeon_router_select_ddc_port(radeon_connector);
Alex Deucher26b5bc92010-08-05 21:21:18 -0400409
Alex Deucher196c58d2010-01-07 14:22:32 -0500410 if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
411 (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)) {
Dave Airlie746c1aa2009-12-08 07:07:28 +1000412 struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
Dave Airlie7a15cbd42010-01-14 11:42:17 +1000413 if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
414 dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) && dig->dp_i2c_bus)
Alex Deucher9fa05c92009-11-27 13:01:46 -0500415 radeon_connector->edid = drm_get_edid(&radeon_connector->base, &dig->dp_i2c_bus->adapter);
Dave Airlie746c1aa2009-12-08 07:07:28 +1000416 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200417 if (!radeon_connector->ddc_bus)
418 return -1;
Dave Airlie4ce001a2009-08-13 16:32:14 +1000419 if (!radeon_connector->edid) {
Alex Deucher0294cf4f2009-10-15 16:16:35 -0400420 radeon_connector->edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
Alex Deucher0294cf4f2009-10-15 16:16:35 -0400421 }
Alex Deucher3c537882010-02-05 04:21:19 -0500422 /* some servers provide a hardcoded edid in rom for KVMs */
423 if (!radeon_connector->edid)
424 radeon_connector->edid = radeon_combios_get_hardcoded_edid(rdev);
Alex Deucher0294cf4f2009-10-15 16:16:35 -0400425 if (radeon_connector->edid) {
426 drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
427 ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200428 return ret;
429 }
430 drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
Dave Airlie42dea5d2009-09-15 20:21:11 +1000431 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200432}
433
434static int radeon_ddc_dump(struct drm_connector *connector)
435{
436 struct edid *edid;
437 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
438 int ret = 0;
439
Alex Deucher26b5bc92010-08-05 21:21:18 -0400440 /* on hw with routers, select right port */
Alex Deucherfb939df2010-11-08 16:08:29 +0000441 if (radeon_connector->router.ddc_valid)
442 radeon_router_select_ddc_port(radeon_connector);
Alex Deucher26b5bc92010-08-05 21:21:18 -0400443
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200444 if (!radeon_connector->ddc_bus)
445 return -1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200446 edid = drm_get_edid(connector, &radeon_connector->ddc_bus->adapter);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200447 if (edid) {
448 kfree(edid);
449 }
450 return ret;
451}
452
453static inline uint32_t radeon_div(uint64_t n, uint32_t d)
454{
455 uint64_t mod;
456
457 n += d / 2;
458
459 mod = do_div(n, d);
460 return n;
461}
462
Alex Deucher48dfaae2010-09-29 11:37:41 -0400463void radeon_compute_pll(struct radeon_pll *pll,
464 uint64_t freq,
465 uint32_t *dot_clock_p,
466 uint32_t *fb_div_p,
467 uint32_t *frac_fb_div_p,
468 uint32_t *ref_div_p,
469 uint32_t *post_div_p)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200470{
471 uint32_t min_ref_div = pll->min_ref_div;
472 uint32_t max_ref_div = pll->max_ref_div;
Alex Deucherfc103322010-01-19 17:16:10 -0500473 uint32_t min_post_div = pll->min_post_div;
474 uint32_t max_post_div = pll->max_post_div;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200475 uint32_t min_fractional_feed_div = 0;
476 uint32_t max_fractional_feed_div = 0;
477 uint32_t best_vco = pll->best_vco;
478 uint32_t best_post_div = 1;
479 uint32_t best_ref_div = 1;
480 uint32_t best_feedback_div = 1;
481 uint32_t best_frac_feedback_div = 0;
482 uint32_t best_freq = -1;
483 uint32_t best_error = 0xffffffff;
484 uint32_t best_vco_diff = 1;
485 uint32_t post_div;
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500486 u32 pll_out_min, pll_out_max;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200487
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000488 DRM_DEBUG_KMS("PLL freq %llu %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200489 freq = freq * 1000;
490
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500491 if (pll->flags & RADEON_PLL_IS_LCD) {
492 pll_out_min = pll->lcd_pll_out_min;
493 pll_out_max = pll->lcd_pll_out_max;
494 } else {
495 pll_out_min = pll->pll_out_min;
496 pll_out_max = pll->pll_out_max;
497 }
498
Alex Deucherfc103322010-01-19 17:16:10 -0500499 if (pll->flags & RADEON_PLL_USE_REF_DIV)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200500 min_ref_div = max_ref_div = pll->reference_div;
501 else {
502 while (min_ref_div < max_ref_div-1) {
503 uint32_t mid = (min_ref_div + max_ref_div) / 2;
504 uint32_t pll_in = pll->reference_freq / mid;
505 if (pll_in < pll->pll_in_min)
506 max_ref_div = mid;
507 else if (pll_in > pll->pll_in_max)
508 min_ref_div = mid;
509 else
510 break;
511 }
512 }
513
Alex Deucherfc103322010-01-19 17:16:10 -0500514 if (pll->flags & RADEON_PLL_USE_POST_DIV)
515 min_post_div = max_post_div = pll->post_div;
516
517 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200518 min_fractional_feed_div = pll->min_frac_feedback_div;
519 max_fractional_feed_div = pll->max_frac_feedback_div;
520 }
521
Alex Deucherbcac54d2010-09-29 11:37:39 -0400522 for (post_div = max_post_div; post_div >= min_post_div; --post_div) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200523 uint32_t ref_div;
524
Alex Deucherfc103322010-01-19 17:16:10 -0500525 if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200526 continue;
527
528 /* legacy radeons only have a few post_divs */
Alex Deucherfc103322010-01-19 17:16:10 -0500529 if (pll->flags & RADEON_PLL_LEGACY) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200530 if ((post_div == 5) ||
531 (post_div == 7) ||
532 (post_div == 9) ||
533 (post_div == 10) ||
534 (post_div == 11) ||
535 (post_div == 13) ||
536 (post_div == 14) ||
537 (post_div == 15))
538 continue;
539 }
540
541 for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
542 uint32_t feedback_div, current_freq = 0, error, vco_diff;
543 uint32_t pll_in = pll->reference_freq / ref_div;
544 uint32_t min_feed_div = pll->min_feedback_div;
545 uint32_t max_feed_div = pll->max_feedback_div + 1;
546
547 if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
548 continue;
549
550 while (min_feed_div < max_feed_div) {
551 uint32_t vco;
552 uint32_t min_frac_feed_div = min_fractional_feed_div;
553 uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
554 uint32_t frac_feedback_div;
555 uint64_t tmp;
556
557 feedback_div = (min_feed_div + max_feed_div) / 2;
558
559 tmp = (uint64_t)pll->reference_freq * feedback_div;
560 vco = radeon_div(tmp, ref_div);
561
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500562 if (vco < pll_out_min) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200563 min_feed_div = feedback_div + 1;
564 continue;
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500565 } else if (vco > pll_out_max) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200566 max_feed_div = feedback_div;
567 continue;
568 }
569
570 while (min_frac_feed_div < max_frac_feed_div) {
571 frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
572 tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
573 tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
574 current_freq = radeon_div(tmp, ref_div * post_div);
575
Alex Deucherfc103322010-01-19 17:16:10 -0500576 if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
Dan Carpenter167ffc42010-07-17 12:28:02 +0200577 if (freq < current_freq)
578 error = 0xffffffff;
579 else
580 error = freq - current_freq;
Alex Deucherd0e275a2009-07-13 11:08:18 -0400581 } else
582 error = abs(current_freq - freq);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200583 vco_diff = abs(vco - best_vco);
584
585 if ((best_vco == 0 && error < best_error) ||
586 (best_vco != 0 &&
Dan Carpenter167ffc42010-07-17 12:28:02 +0200587 ((best_error > 100 && error < best_error - 100) ||
Dave Airlie5480f722010-10-19 10:36:47 +1000588 (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200589 best_post_div = post_div;
590 best_ref_div = ref_div;
591 best_feedback_div = feedback_div;
592 best_frac_feedback_div = frac_feedback_div;
593 best_freq = current_freq;
594 best_error = error;
595 best_vco_diff = vco_diff;
Dave Airlie5480f722010-10-19 10:36:47 +1000596 } else if (current_freq == freq) {
597 if (best_freq == -1) {
598 best_post_div = post_div;
599 best_ref_div = ref_div;
600 best_feedback_div = feedback_div;
601 best_frac_feedback_div = frac_feedback_div;
602 best_freq = current_freq;
603 best_error = error;
604 best_vco_diff = vco_diff;
605 } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
606 ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
607 ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
608 ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
609 ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
610 ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
611 best_post_div = post_div;
612 best_ref_div = ref_div;
613 best_feedback_div = feedback_div;
614 best_frac_feedback_div = frac_feedback_div;
615 best_freq = current_freq;
616 best_error = error;
617 best_vco_diff = vco_diff;
618 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200619 }
620 if (current_freq < freq)
621 min_frac_feed_div = frac_feedback_div + 1;
622 else
623 max_frac_feed_div = frac_feedback_div;
624 }
625 if (current_freq < freq)
626 min_feed_div = feedback_div + 1;
627 else
628 max_feed_div = feedback_div;
629 }
630 }
631 }
632
633 *dot_clock_p = best_freq / 10000;
634 *fb_div_p = best_feedback_div;
635 *frac_fb_div_p = best_frac_feedback_div;
636 *ref_div_p = best_ref_div;
637 *post_div_p = best_post_div;
638}
639
640static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
641{
642 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200643
Dave Airlie29d08b32010-09-27 16:17:17 +1000644 if (radeon_fb->obj) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000645 drm_gem_object_unreference_unlocked(radeon_fb->obj);
Dave Airlie29d08b32010-09-27 16:17:17 +1000646 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200647 drm_framebuffer_cleanup(fb);
648 kfree(radeon_fb);
649}
650
651static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
652 struct drm_file *file_priv,
653 unsigned int *handle)
654{
655 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
656
657 return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
658}
659
660static const struct drm_framebuffer_funcs radeon_fb_funcs = {
661 .destroy = radeon_user_framebuffer_destroy,
662 .create_handle = radeon_user_framebuffer_create_handle,
663};
664
Dave Airlie38651672010-03-30 05:34:13 +0000665void
666radeon_framebuffer_init(struct drm_device *dev,
667 struct radeon_framebuffer *rfb,
668 struct drm_mode_fb_cmd *mode_cmd,
669 struct drm_gem_object *obj)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200670{
Dave Airlie38651672010-03-30 05:34:13 +0000671 rfb->obj = obj;
672 drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
673 drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200674}
675
676static struct drm_framebuffer *
677radeon_user_framebuffer_create(struct drm_device *dev,
678 struct drm_file *file_priv,
679 struct drm_mode_fb_cmd *mode_cmd)
680{
681 struct drm_gem_object *obj;
Dave Airlie38651672010-03-30 05:34:13 +0000682 struct radeon_framebuffer *radeon_fb;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200683
684 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
Jerome Glisse7e71c9e2010-01-17 21:21:41 +0100685 if (obj == NULL) {
686 dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
687 "can't create framebuffer\n", mode_cmd->handle);
Chris Wilsoncce13ff2010-08-08 13:36:38 +0100688 return ERR_PTR(-ENOENT);
Jerome Glisse7e71c9e2010-01-17 21:21:41 +0100689 }
Dave Airlie38651672010-03-30 05:34:13 +0000690
691 radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
Chris Wilsoncce13ff2010-08-08 13:36:38 +0100692 if (radeon_fb == NULL)
693 return ERR_PTR(-ENOMEM);
Dave Airlie38651672010-03-30 05:34:13 +0000694
695 radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
696
697 return &radeon_fb->base;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200698}
699
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000700static void radeon_output_poll_changed(struct drm_device *dev)
701{
702 struct radeon_device *rdev = dev->dev_private;
703 radeon_fb_output_poll_changed(rdev);
704}
705
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200706static const struct drm_mode_config_funcs radeon_mode_funcs = {
707 .fb_create = radeon_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000708 .output_poll_changed = radeon_output_poll_changed
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200709};
710
Dave Airlie445282d2009-09-09 17:40:54 +1000711struct drm_prop_enum_list {
712 int type;
713 char *name;
714};
715
716static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
717{ { 0, "driver" },
718 { 1, "bios" },
719};
720
721static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
722{ { TV_STD_NTSC, "ntsc" },
723 { TV_STD_PAL, "pal" },
724 { TV_STD_PAL_M, "pal-m" },
725 { TV_STD_PAL_60, "pal-60" },
726 { TV_STD_NTSC_J, "ntsc-j" },
727 { TV_STD_SCART_PAL, "scart-pal" },
728 { TV_STD_PAL_CN, "pal-cn" },
729 { TV_STD_SECAM, "secam" },
730};
731
Alex Deucher5b1714d2010-08-03 19:59:20 -0400732static struct drm_prop_enum_list radeon_underscan_enum_list[] =
733{ { UNDERSCAN_OFF, "off" },
734 { UNDERSCAN_ON, "on" },
735 { UNDERSCAN_AUTO, "auto" },
736};
737
Alex Deucherd79766f2009-12-17 19:00:29 -0500738static int radeon_modeset_create_props(struct radeon_device *rdev)
Dave Airlie445282d2009-09-09 17:40:54 +1000739{
740 int i, sz;
741
742 if (rdev->is_atom_bios) {
743 rdev->mode_info.coherent_mode_property =
744 drm_property_create(rdev->ddev,
745 DRM_MODE_PROP_RANGE,
746 "coherent", 2);
747 if (!rdev->mode_info.coherent_mode_property)
748 return -ENOMEM;
749
750 rdev->mode_info.coherent_mode_property->values[0] = 0;
Alex Deucher390d0bb2009-12-08 12:48:20 -0500751 rdev->mode_info.coherent_mode_property->values[1] = 1;
Dave Airlie445282d2009-09-09 17:40:54 +1000752 }
753
754 if (!ASIC_IS_AVIVO(rdev)) {
755 sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
756 rdev->mode_info.tmds_pll_property =
757 drm_property_create(rdev->ddev,
758 DRM_MODE_PROP_ENUM,
759 "tmds_pll", sz);
760 for (i = 0; i < sz; i++) {
761 drm_property_add_enum(rdev->mode_info.tmds_pll_property,
762 i,
763 radeon_tmds_pll_enum_list[i].type,
764 radeon_tmds_pll_enum_list[i].name);
765 }
766 }
767
768 rdev->mode_info.load_detect_property =
769 drm_property_create(rdev->ddev,
770 DRM_MODE_PROP_RANGE,
771 "load detection", 2);
772 if (!rdev->mode_info.load_detect_property)
773 return -ENOMEM;
774 rdev->mode_info.load_detect_property->values[0] = 0;
Alex Deucher390d0bb2009-12-08 12:48:20 -0500775 rdev->mode_info.load_detect_property->values[1] = 1;
Dave Airlie445282d2009-09-09 17:40:54 +1000776
777 drm_mode_create_scaling_mode_property(rdev->ddev);
778
779 sz = ARRAY_SIZE(radeon_tv_std_enum_list);
780 rdev->mode_info.tv_std_property =
781 drm_property_create(rdev->ddev,
782 DRM_MODE_PROP_ENUM,
783 "tv standard", sz);
784 for (i = 0; i < sz; i++) {
785 drm_property_add_enum(rdev->mode_info.tv_std_property,
786 i,
787 radeon_tv_std_enum_list[i].type,
788 radeon_tv_std_enum_list[i].name);
789 }
790
Alex Deucher5b1714d2010-08-03 19:59:20 -0400791 sz = ARRAY_SIZE(radeon_underscan_enum_list);
792 rdev->mode_info.underscan_property =
793 drm_property_create(rdev->ddev,
794 DRM_MODE_PROP_ENUM,
795 "underscan", sz);
796 for (i = 0; i < sz; i++) {
797 drm_property_add_enum(rdev->mode_info.underscan_property,
798 i,
799 radeon_underscan_enum_list[i].type,
800 radeon_underscan_enum_list[i].name);
801 }
802
Marius Gröger5bccf5e2010-09-21 21:30:59 +0200803 rdev->mode_info.underscan_hborder_property =
804 drm_property_create(rdev->ddev,
805 DRM_MODE_PROP_RANGE,
806 "underscan hborder", 2);
807 if (!rdev->mode_info.underscan_hborder_property)
808 return -ENOMEM;
809 rdev->mode_info.underscan_hborder_property->values[0] = 0;
810 rdev->mode_info.underscan_hborder_property->values[1] = 128;
811
812 rdev->mode_info.underscan_vborder_property =
813 drm_property_create(rdev->ddev,
814 DRM_MODE_PROP_RANGE,
815 "underscan vborder", 2);
816 if (!rdev->mode_info.underscan_vborder_property)
817 return -ENOMEM;
818 rdev->mode_info.underscan_vborder_property->values[0] = 0;
819 rdev->mode_info.underscan_vborder_property->values[1] = 128;
820
Dave Airlie445282d2009-09-09 17:40:54 +1000821 return 0;
822}
823
Alex Deucherf46c0122010-03-31 00:33:27 -0400824void radeon_update_display_priority(struct radeon_device *rdev)
825{
826 /* adjustment options for the display watermarks */
827 if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
828 /* set display priority to high for r3xx, rv515 chips
829 * this avoids flickering due to underflow to the
830 * display controllers during heavy acceleration.
Alex Deucher45737442010-05-20 11:26:11 -0400831 * Don't force high on rs4xx igp chips as it seems to
832 * affect the sound card. See kernel bug 15982.
Alex Deucherf46c0122010-03-31 00:33:27 -0400833 */
Alex Deucher45737442010-05-20 11:26:11 -0400834 if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
835 !(rdev->flags & RADEON_IS_IGP))
Alex Deucherf46c0122010-03-31 00:33:27 -0400836 rdev->disp_priority = 2;
837 else
838 rdev->disp_priority = 0;
839 } else
840 rdev->disp_priority = radeon_disp_priority;
841
842}
843
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200844int radeon_modeset_init(struct radeon_device *rdev)
845{
Alex Deucher18917b62010-02-01 16:02:25 -0500846 int i;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200847 int ret;
848
849 drm_mode_config_init(rdev->ddev);
850 rdev->mode_info.mode_config_initialized = true;
851
852 rdev->ddev->mode_config.funcs = (void *)&radeon_mode_funcs;
853
854 if (ASIC_IS_AVIVO(rdev)) {
855 rdev->ddev->mode_config.max_width = 8192;
856 rdev->ddev->mode_config.max_height = 8192;
857 } else {
858 rdev->ddev->mode_config.max_width = 4096;
859 rdev->ddev->mode_config.max_height = 4096;
860 }
861
862 rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
863
Dave Airlie445282d2009-09-09 17:40:54 +1000864 ret = radeon_modeset_create_props(rdev);
865 if (ret) {
866 return ret;
867 }
Dave Airliedfee5612009-10-02 09:19:09 +1000868
Alex Deucherf376b942010-08-05 21:21:16 -0400869 /* init i2c buses */
870 radeon_i2c_init(rdev);
871
Alex Deucher3c537882010-02-05 04:21:19 -0500872 /* check combios for a valid hardcoded EDID - Sun servers */
873 if (!rdev->is_atom_bios) {
874 /* check for hardcoded EDID in BIOS */
875 radeon_combios_check_hardcoded_edid(rdev);
876 }
877
Dave Airliedfee5612009-10-02 09:19:09 +1000878 /* allocate crtcs */
Alex Deucher18917b62010-02-01 16:02:25 -0500879 for (i = 0; i < rdev->num_crtc; i++) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200880 radeon_crtc_init(rdev->ddev, i);
881 }
882
883 /* okay we should have all the bios connectors */
884 ret = radeon_setup_enc_conn(rdev->ddev);
885 if (!ret) {
886 return ret;
887 }
Alex Deucherd4877cf2009-12-04 16:56:37 -0500888 /* initialize hpd */
889 radeon_hpd_init(rdev);
Dave Airlie38651672010-03-30 05:34:13 +0000890
Alex Deucherce8f5372010-05-07 15:10:16 -0400891 /* Initialize power management */
892 radeon_pm_init(rdev);
893
Dave Airlie38651672010-03-30 05:34:13 +0000894 radeon_fbdev_init(rdev);
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000895 drm_kms_helper_poll_init(rdev->ddev);
896
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200897 return 0;
898}
899
900void radeon_modeset_fini(struct radeon_device *rdev)
901{
Dave Airlie38651672010-03-30 05:34:13 +0000902 radeon_fbdev_fini(rdev);
Alex Deucher3c537882010-02-05 04:21:19 -0500903 kfree(rdev->mode_info.bios_hardcoded_edid);
Alex Deucherce8f5372010-05-07 15:10:16 -0400904 radeon_pm_fini(rdev);
Alex Deucher3c537882010-02-05 04:21:19 -0500905
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200906 if (rdev->mode_info.mode_config_initialized) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000907 drm_kms_helper_poll_fini(rdev->ddev);
Alex Deucherd4877cf2009-12-04 16:56:37 -0500908 radeon_hpd_fini(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200909 drm_mode_config_cleanup(rdev->ddev);
910 rdev->mode_info.mode_config_initialized = false;
911 }
Alex Deucherf376b942010-08-05 21:21:16 -0400912 /* free i2c buses */
913 radeon_i2c_fini(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200914}
915
Alex Deucher039ed2d2010-08-20 11:57:19 -0400916static bool is_hdtv_mode(struct drm_display_mode *mode)
917{
918 /* try and guess if this is a tv or a monitor */
919 if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
920 (mode->vdisplay == 576) || /* 576p */
921 (mode->vdisplay == 720) || /* 720p */
922 (mode->vdisplay == 1080)) /* 1080p */
923 return true;
924 else
925 return false;
926}
927
Jerome Glissec93bb852009-07-13 21:04:08 +0200928bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
929 struct drm_display_mode *mode,
930 struct drm_display_mode *adjusted_mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200931{
Jerome Glissec93bb852009-07-13 21:04:08 +0200932 struct drm_device *dev = crtc->dev;
Alex Deucher5b1714d2010-08-03 19:59:20 -0400933 struct radeon_device *rdev = dev->dev_private;
Jerome Glissec93bb852009-07-13 21:04:08 +0200934 struct drm_encoder *encoder;
935 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
936 struct radeon_encoder *radeon_encoder;
Alex Deucher5b1714d2010-08-03 19:59:20 -0400937 struct drm_connector *connector;
938 struct radeon_connector *radeon_connector;
Jerome Glissec93bb852009-07-13 21:04:08 +0200939 bool first = true;
Alex Deucherd65d65b2010-08-03 19:58:49 -0400940 u32 src_v = 1, dst_v = 1;
941 u32 src_h = 1, dst_h = 1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200942
Alex Deucher5b1714d2010-08-03 19:59:20 -0400943 radeon_crtc->h_border = 0;
944 radeon_crtc->v_border = 0;
945
Jerome Glissec93bb852009-07-13 21:04:08 +0200946 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Jerome Glissec93bb852009-07-13 21:04:08 +0200947 if (encoder->crtc != crtc)
948 continue;
Alex Deucherd65d65b2010-08-03 19:58:49 -0400949 radeon_encoder = to_radeon_encoder(encoder);
Alex Deucher5b1714d2010-08-03 19:59:20 -0400950 connector = radeon_get_connector_for_encoder(encoder);
951 radeon_connector = to_radeon_connector(connector);
952
Jerome Glissec93bb852009-07-13 21:04:08 +0200953 if (first) {
Alex Deucher80297e82009-11-12 14:55:14 -0500954 /* set scaling */
955 if (radeon_encoder->rmx_type == RMX_OFF)
956 radeon_crtc->rmx_type = RMX_OFF;
957 else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
958 mode->vdisplay < radeon_encoder->native_mode.vdisplay)
959 radeon_crtc->rmx_type = radeon_encoder->rmx_type;
960 else
961 radeon_crtc->rmx_type = RMX_OFF;
962 /* copy native mode */
Jerome Glissec93bb852009-07-13 21:04:08 +0200963 memcpy(&radeon_crtc->native_mode,
Alex Deucher80297e82009-11-12 14:55:14 -0500964 &radeon_encoder->native_mode,
Alex Deucherde2103e2009-10-09 15:14:30 -0400965 sizeof(struct drm_display_mode));
Alex Deucherff32a592010-09-07 13:26:39 -0400966 src_v = crtc->mode.vdisplay;
967 dst_v = radeon_crtc->native_mode.vdisplay;
968 src_h = crtc->mode.hdisplay;
969 dst_h = radeon_crtc->native_mode.hdisplay;
Alex Deucher5b1714d2010-08-03 19:59:20 -0400970
971 /* fix up for overscan on hdmi */
972 if (ASIC_IS_AVIVO(rdev) &&
Alex Deuchere6db0da2010-09-10 03:19:05 -0400973 (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
Alex Deucher5b1714d2010-08-03 19:59:20 -0400974 ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
975 ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
Alex Deucher039ed2d2010-08-20 11:57:19 -0400976 drm_detect_hdmi_monitor(radeon_connector->edid) &&
977 is_hdtv_mode(mode)))) {
Marius Gröger5bccf5e2010-09-21 21:30:59 +0200978 if (radeon_encoder->underscan_hborder != 0)
979 radeon_crtc->h_border = radeon_encoder->underscan_hborder;
980 else
981 radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
982 if (radeon_encoder->underscan_vborder != 0)
983 radeon_crtc->v_border = radeon_encoder->underscan_vborder;
984 else
985 radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
Alex Deucher5b1714d2010-08-03 19:59:20 -0400986 radeon_crtc->rmx_type = RMX_FULL;
987 src_v = crtc->mode.vdisplay;
988 dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
989 src_h = crtc->mode.hdisplay;
990 dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
991 }
Jerome Glissec93bb852009-07-13 21:04:08 +0200992 first = false;
993 } else {
994 if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
995 /* WARNING: Right now this can't happen but
996 * in the future we need to check that scaling
Alex Deucherd65d65b2010-08-03 19:58:49 -0400997 * are consistent across different encoder
Jerome Glissec93bb852009-07-13 21:04:08 +0200998 * (ie all encoder can work with the same
999 * scaling).
1000 */
Alex Deucherd65d65b2010-08-03 19:58:49 -04001001 DRM_ERROR("Scaling not consistent across encoder.\n");
Jerome Glissec93bb852009-07-13 21:04:08 +02001002 return false;
1003 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001004 }
1005 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001006 if (radeon_crtc->rmx_type != RMX_OFF) {
1007 fixed20_12 a, b;
Alex Deucherd65d65b2010-08-03 19:58:49 -04001008 a.full = dfixed_const(src_v);
1009 b.full = dfixed_const(dst_v);
Ben Skeggs68adac52010-04-28 11:46:42 +10001010 radeon_crtc->vsc.full = dfixed_div(a, b);
Alex Deucherd65d65b2010-08-03 19:58:49 -04001011 a.full = dfixed_const(src_h);
1012 b.full = dfixed_const(dst_h);
Ben Skeggs68adac52010-04-28 11:46:42 +10001013 radeon_crtc->hsc.full = dfixed_div(a, b);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001014 } else {
Ben Skeggs68adac52010-04-28 11:46:42 +10001015 radeon_crtc->vsc.full = dfixed_const(1);
1016 radeon_crtc->hsc.full = dfixed_const(1);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001017 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001018 return true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001019}
Mario Kleiner6383cf72010-10-05 19:57:36 -04001020
1021/*
1022 * Retrieve current video scanout position of crtc on a given gpu.
1023 *
1024 * \param rdev Device to query.
1025 * \param crtc Crtc to query.
1026 * \param *vpos Location where vertical scanout position should be stored.
1027 * \param *hpos Location where horizontal scanout position should go.
1028 *
1029 * Returns vpos as a positive number while in active scanout area.
1030 * Returns vpos as a negative number inside vblank, counting the number
1031 * of scanlines to go until end of vblank, e.g., -1 means "one scanline
1032 * until start of active scanout / end of vblank."
1033 *
1034 * \return Flags, or'ed together as follows:
1035 *
1036 * RADEON_SCANOUTPOS_VALID = Query successfull.
1037 * RADEON_SCANOUTPOS_INVBL = Inside vblank.
1038 * RADEON_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
1039 * this flag means that returned position may be offset by a constant but
1040 * unknown small number of scanlines wrt. real scanout position.
1041 *
1042 */
1043int radeon_get_crtc_scanoutpos(struct radeon_device *rdev, int crtc, int *vpos, int *hpos)
1044{
1045 u32 stat_crtc = 0, vbl = 0, position = 0;
1046 int vbl_start, vbl_end, vtotal, ret = 0;
1047 bool in_vbl = true;
1048
1049 if (ASIC_IS_DCE4(rdev)) {
1050 if (crtc == 0) {
1051 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1052 EVERGREEN_CRTC0_REGISTER_OFFSET);
1053 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1054 EVERGREEN_CRTC0_REGISTER_OFFSET);
1055 ret |= RADEON_SCANOUTPOS_VALID;
1056 }
1057 if (crtc == 1) {
1058 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1059 EVERGREEN_CRTC1_REGISTER_OFFSET);
1060 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1061 EVERGREEN_CRTC1_REGISTER_OFFSET);
1062 ret |= RADEON_SCANOUTPOS_VALID;
1063 }
1064 if (crtc == 2) {
1065 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1066 EVERGREEN_CRTC2_REGISTER_OFFSET);
1067 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1068 EVERGREEN_CRTC2_REGISTER_OFFSET);
1069 ret |= RADEON_SCANOUTPOS_VALID;
1070 }
1071 if (crtc == 3) {
1072 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1073 EVERGREEN_CRTC3_REGISTER_OFFSET);
1074 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1075 EVERGREEN_CRTC3_REGISTER_OFFSET);
1076 ret |= RADEON_SCANOUTPOS_VALID;
1077 }
1078 if (crtc == 4) {
1079 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1080 EVERGREEN_CRTC4_REGISTER_OFFSET);
1081 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1082 EVERGREEN_CRTC4_REGISTER_OFFSET);
1083 ret |= RADEON_SCANOUTPOS_VALID;
1084 }
1085 if (crtc == 5) {
1086 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
1087 EVERGREEN_CRTC5_REGISTER_OFFSET);
1088 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
1089 EVERGREEN_CRTC5_REGISTER_OFFSET);
1090 ret |= RADEON_SCANOUTPOS_VALID;
1091 }
1092 } else if (ASIC_IS_AVIVO(rdev)) {
1093 if (crtc == 0) {
1094 vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END);
1095 position = RREG32(AVIVO_D1CRTC_STATUS_POSITION);
1096 ret |= RADEON_SCANOUTPOS_VALID;
1097 }
1098 if (crtc == 1) {
1099 vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END);
1100 position = RREG32(AVIVO_D2CRTC_STATUS_POSITION);
1101 ret |= RADEON_SCANOUTPOS_VALID;
1102 }
1103 } else {
1104 /* Pre-AVIVO: Different encoding of scanout pos and vblank interval. */
1105 if (crtc == 0) {
1106 /* Assume vbl_end == 0, get vbl_start from
1107 * upper 16 bits.
1108 */
1109 vbl = (RREG32(RADEON_CRTC_V_TOTAL_DISP) &
1110 RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
1111 /* Only retrieve vpos from upper 16 bits, set hpos == 0. */
1112 position = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
1113 stat_crtc = RREG32(RADEON_CRTC_STATUS);
1114 if (!(stat_crtc & 1))
1115 in_vbl = false;
1116
1117 ret |= RADEON_SCANOUTPOS_VALID;
1118 }
1119 if (crtc == 1) {
1120 vbl = (RREG32(RADEON_CRTC2_V_TOTAL_DISP) &
1121 RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
1122 position = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
1123 stat_crtc = RREG32(RADEON_CRTC2_STATUS);
1124 if (!(stat_crtc & 1))
1125 in_vbl = false;
1126
1127 ret |= RADEON_SCANOUTPOS_VALID;
1128 }
1129 }
1130
1131 /* Decode into vertical and horizontal scanout position. */
1132 *vpos = position & 0x1fff;
1133 *hpos = (position >> 16) & 0x1fff;
1134
1135 /* Valid vblank area boundaries from gpu retrieved? */
1136 if (vbl > 0) {
1137 /* Yes: Decode. */
1138 ret |= RADEON_SCANOUTPOS_ACCURATE;
1139 vbl_start = vbl & 0x1fff;
1140 vbl_end = (vbl >> 16) & 0x1fff;
1141 }
1142 else {
1143 /* No: Fake something reasonable which gives at least ok results. */
1144 vbl_start = rdev->mode_info.crtcs[crtc]->base.mode.crtc_vdisplay;
1145 vbl_end = 0;
1146 }
1147
1148 /* Test scanout position against vblank region. */
1149 if ((*vpos < vbl_start) && (*vpos >= vbl_end))
1150 in_vbl = false;
1151
1152 /* Check if inside vblank area and apply corrective offsets:
1153 * vpos will then be >=0 in video scanout area, but negative
1154 * within vblank area, counting down the number of lines until
1155 * start of scanout.
1156 */
1157
1158 /* Inside "upper part" of vblank area? Apply corrective offset if so: */
1159 if (in_vbl && (*vpos >= vbl_start)) {
1160 vtotal = rdev->mode_info.crtcs[crtc]->base.mode.crtc_vtotal;
1161 *vpos = *vpos - vtotal;
1162 }
1163
1164 /* Correct for shifted end of vbl at vbl_end. */
1165 *vpos = *vpos - vbl_end;
1166
1167 /* In vblank? */
1168 if (in_vbl)
1169 ret |= RADEON_SCANOUTPOS_INVBL;
1170
1171 return ret;
1172}