blob: b92d2f2fcbed6a8bd472ce9f4b936aa82f309278 [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
228static const char *encoder_names[34] = {
229 "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",
263};
264
Alex Deucher196c58d2010-01-07 14:22:32 -0500265static const char *connector_names[15] = {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200266 "Unknown",
267 "VGA",
268 "DVI-I",
269 "DVI-D",
270 "DVI-A",
271 "Composite",
272 "S-video",
273 "LVDS",
274 "Component",
275 "DIN",
276 "DisplayPort",
277 "HDMI-A",
278 "HDMI-B",
Alex Deucher196c58d2010-01-07 14:22:32 -0500279 "TV",
280 "eDP",
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200281};
282
Alex Deuchercbd46232010-06-07 02:24:54 -0400283static const char *hpd_names[6] = {
Alex Deuchereed45b32009-12-04 14:45:27 -0500284 "HPD1",
285 "HPD2",
286 "HPD3",
287 "HPD4",
288 "HPD5",
289 "HPD6",
290};
291
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200292static void radeon_print_display_setup(struct drm_device *dev)
293{
294 struct drm_connector *connector;
295 struct radeon_connector *radeon_connector;
296 struct drm_encoder *encoder;
297 struct radeon_encoder *radeon_encoder;
298 uint32_t devices;
299 int i = 0;
300
301 DRM_INFO("Radeon Display Connectors\n");
302 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
303 radeon_connector = to_radeon_connector(connector);
304 DRM_INFO("Connector %d:\n", i);
305 DRM_INFO(" %s\n", connector_names[connector->connector_type]);
Alex Deuchereed45b32009-12-04 14:45:27 -0500306 if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
307 DRM_INFO(" %s\n", hpd_names[radeon_connector->hpd.hpd]);
Dave Airlie4b9d2a22010-02-08 13:16:55 +1000308 if (radeon_connector->ddc_bus) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200309 DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
310 radeon_connector->ddc_bus->rec.mask_clk_reg,
311 radeon_connector->ddc_bus->rec.mask_data_reg,
312 radeon_connector->ddc_bus->rec.a_clk_reg,
313 radeon_connector->ddc_bus->rec.a_data_reg,
Alex Deucher9b9fe722009-11-10 15:59:44 -0500314 radeon_connector->ddc_bus->rec.en_clk_reg,
315 radeon_connector->ddc_bus->rec.en_data_reg,
316 radeon_connector->ddc_bus->rec.y_clk_reg,
317 radeon_connector->ddc_bus->rec.y_data_reg);
Alex Deucher26b5bc92010-08-05 21:21:18 -0400318 if (radeon_connector->router_bus)
319 DRM_INFO(" DDC Router 0x%x/0x%x\n",
320 radeon_connector->router.mux_control_pin,
321 radeon_connector->router.mux_state);
Dave Airlie4b9d2a22010-02-08 13:16:55 +1000322 } else {
323 if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
324 connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
325 connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
326 connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
327 connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
328 connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
329 DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
330 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200331 DRM_INFO(" Encoders:\n");
332 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
333 radeon_encoder = to_radeon_encoder(encoder);
334 devices = radeon_encoder->devices & radeon_connector->devices;
335 if (devices) {
336 if (devices & ATOM_DEVICE_CRT1_SUPPORT)
337 DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
338 if (devices & ATOM_DEVICE_CRT2_SUPPORT)
339 DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
340 if (devices & ATOM_DEVICE_LCD1_SUPPORT)
341 DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
342 if (devices & ATOM_DEVICE_DFP1_SUPPORT)
343 DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
344 if (devices & ATOM_DEVICE_DFP2_SUPPORT)
345 DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
346 if (devices & ATOM_DEVICE_DFP3_SUPPORT)
347 DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
348 if (devices & ATOM_DEVICE_DFP4_SUPPORT)
349 DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
350 if (devices & ATOM_DEVICE_DFP5_SUPPORT)
351 DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
Alex Deucher73758a52010-09-24 14:59:32 -0400352 if (devices & ATOM_DEVICE_DFP6_SUPPORT)
353 DRM_INFO(" DFP6: %s\n", encoder_names[radeon_encoder->encoder_id]);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200354 if (devices & ATOM_DEVICE_TV1_SUPPORT)
355 DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
356 if (devices & ATOM_DEVICE_CV_SUPPORT)
357 DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
358 }
359 }
360 i++;
361 }
362}
363
Dave Airlie4ce001a2009-08-13 16:32:14 +1000364static bool radeon_setup_enc_conn(struct drm_device *dev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200365{
366 struct radeon_device *rdev = dev->dev_private;
367 struct drm_connector *drm_connector;
368 bool ret = false;
369
370 if (rdev->bios) {
371 if (rdev->is_atom_bios) {
Alex Deuchera084e6e2010-03-18 01:04:01 -0400372 ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
373 if (ret == false)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200374 ret = radeon_get_atom_connector_info_from_object_table(dev);
Alex Deucherb9597a12010-01-04 19:12:02 -0500375 } else {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200376 ret = radeon_get_legacy_connector_info_from_bios(dev);
Alex Deucherb9597a12010-01-04 19:12:02 -0500377 if (ret == false)
378 ret = radeon_get_legacy_connector_info_from_table(dev);
379 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200380 } else {
381 if (!ASIC_IS_AVIVO(rdev))
382 ret = radeon_get_legacy_connector_info_from_table(dev);
383 }
384 if (ret) {
Dave Airlie1f3b6a42009-10-13 14:10:37 +1000385 radeon_setup_encoder_clones(dev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200386 radeon_print_display_setup(dev);
387 list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head)
388 radeon_ddc_dump(drm_connector);
389 }
390
391 return ret;
392}
393
394int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
395{
Alex Deucher3c537882010-02-05 04:21:19 -0500396 struct drm_device *dev = radeon_connector->base.dev;
397 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200398 int ret = 0;
399
Alex Deucher26b5bc92010-08-05 21:21:18 -0400400 /* on hw with routers, select right port */
401 if (radeon_connector->router.valid)
402 radeon_router_select_port(radeon_connector);
403
Alex Deucher196c58d2010-01-07 14:22:32 -0500404 if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
405 (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)) {
Dave Airlie746c1aa2009-12-08 07:07:28 +1000406 struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
Dave Airlie7a15cbd42010-01-14 11:42:17 +1000407 if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
408 dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) && dig->dp_i2c_bus)
Alex Deucher9fa05c92009-11-27 13:01:46 -0500409 radeon_connector->edid = drm_get_edid(&radeon_connector->base, &dig->dp_i2c_bus->adapter);
Dave Airlie746c1aa2009-12-08 07:07:28 +1000410 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200411 if (!radeon_connector->ddc_bus)
412 return -1;
Dave Airlie4ce001a2009-08-13 16:32:14 +1000413 if (!radeon_connector->edid) {
Alex Deucher0294cf4f2009-10-15 16:16:35 -0400414 radeon_connector->edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
Alex Deucher0294cf4f2009-10-15 16:16:35 -0400415 }
Alex Deucher3c537882010-02-05 04:21:19 -0500416 /* some servers provide a hardcoded edid in rom for KVMs */
417 if (!radeon_connector->edid)
418 radeon_connector->edid = radeon_combios_get_hardcoded_edid(rdev);
Alex Deucher0294cf4f2009-10-15 16:16:35 -0400419 if (radeon_connector->edid) {
420 drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
421 ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200422 return ret;
423 }
424 drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
Dave Airlie42dea5d2009-09-15 20:21:11 +1000425 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200426}
427
428static int radeon_ddc_dump(struct drm_connector *connector)
429{
430 struct edid *edid;
431 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
432 int ret = 0;
433
Alex Deucher26b5bc92010-08-05 21:21:18 -0400434 /* on hw with routers, select right port */
435 if (radeon_connector->router.valid)
436 radeon_router_select_port(radeon_connector);
437
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200438 if (!radeon_connector->ddc_bus)
439 return -1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200440 edid = drm_get_edid(connector, &radeon_connector->ddc_bus->adapter);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200441 if (edid) {
442 kfree(edid);
443 }
444 return ret;
445}
446
447static inline uint32_t radeon_div(uint64_t n, uint32_t d)
448{
449 uint64_t mod;
450
451 n += d / 2;
452
453 mod = do_div(n, d);
454 return n;
455}
456
Alex Deucher7c27f872010-02-02 12:05:01 -0500457static void radeon_compute_pll_legacy(struct radeon_pll *pll,
458 uint64_t freq,
459 uint32_t *dot_clock_p,
460 uint32_t *fb_div_p,
461 uint32_t *frac_fb_div_p,
462 uint32_t *ref_div_p,
463 uint32_t *post_div_p)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200464{
465 uint32_t min_ref_div = pll->min_ref_div;
466 uint32_t max_ref_div = pll->max_ref_div;
Alex Deucherfc103322010-01-19 17:16:10 -0500467 uint32_t min_post_div = pll->min_post_div;
468 uint32_t max_post_div = pll->max_post_div;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200469 uint32_t min_fractional_feed_div = 0;
470 uint32_t max_fractional_feed_div = 0;
471 uint32_t best_vco = pll->best_vco;
472 uint32_t best_post_div = 1;
473 uint32_t best_ref_div = 1;
474 uint32_t best_feedback_div = 1;
475 uint32_t best_frac_feedback_div = 0;
476 uint32_t best_freq = -1;
477 uint32_t best_error = 0xffffffff;
478 uint32_t best_vco_diff = 1;
479 uint32_t post_div;
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500480 u32 pll_out_min, pll_out_max;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200481
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000482 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 +0200483 freq = freq * 1000;
484
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500485 if (pll->flags & RADEON_PLL_IS_LCD) {
486 pll_out_min = pll->lcd_pll_out_min;
487 pll_out_max = pll->lcd_pll_out_max;
488 } else {
489 pll_out_min = pll->pll_out_min;
490 pll_out_max = pll->pll_out_max;
491 }
492
Alex Deucherfc103322010-01-19 17:16:10 -0500493 if (pll->flags & RADEON_PLL_USE_REF_DIV)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200494 min_ref_div = max_ref_div = pll->reference_div;
495 else {
496 while (min_ref_div < max_ref_div-1) {
497 uint32_t mid = (min_ref_div + max_ref_div) / 2;
498 uint32_t pll_in = pll->reference_freq / mid;
499 if (pll_in < pll->pll_in_min)
500 max_ref_div = mid;
501 else if (pll_in > pll->pll_in_max)
502 min_ref_div = mid;
503 else
504 break;
505 }
506 }
507
Alex Deucherfc103322010-01-19 17:16:10 -0500508 if (pll->flags & RADEON_PLL_USE_POST_DIV)
509 min_post_div = max_post_div = pll->post_div;
510
511 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200512 min_fractional_feed_div = pll->min_frac_feedback_div;
513 max_fractional_feed_div = pll->max_frac_feedback_div;
514 }
515
Alex Deucherfc103322010-01-19 17:16:10 -0500516 for (post_div = min_post_div; post_div <= max_post_div; ++post_div) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200517 uint32_t ref_div;
518
Alex Deucherfc103322010-01-19 17:16:10 -0500519 if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200520 continue;
521
522 /* legacy radeons only have a few post_divs */
Alex Deucherfc103322010-01-19 17:16:10 -0500523 if (pll->flags & RADEON_PLL_LEGACY) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200524 if ((post_div == 5) ||
525 (post_div == 7) ||
526 (post_div == 9) ||
527 (post_div == 10) ||
528 (post_div == 11) ||
529 (post_div == 13) ||
530 (post_div == 14) ||
531 (post_div == 15))
532 continue;
533 }
534
535 for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
536 uint32_t feedback_div, current_freq = 0, error, vco_diff;
537 uint32_t pll_in = pll->reference_freq / ref_div;
538 uint32_t min_feed_div = pll->min_feedback_div;
539 uint32_t max_feed_div = pll->max_feedback_div + 1;
540
541 if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
542 continue;
543
544 while (min_feed_div < max_feed_div) {
545 uint32_t vco;
546 uint32_t min_frac_feed_div = min_fractional_feed_div;
547 uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
548 uint32_t frac_feedback_div;
549 uint64_t tmp;
550
551 feedback_div = (min_feed_div + max_feed_div) / 2;
552
553 tmp = (uint64_t)pll->reference_freq * feedback_div;
554 vco = radeon_div(tmp, ref_div);
555
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500556 if (vco < pll_out_min) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200557 min_feed_div = feedback_div + 1;
558 continue;
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500559 } else if (vco > pll_out_max) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200560 max_feed_div = feedback_div;
561 continue;
562 }
563
564 while (min_frac_feed_div < max_frac_feed_div) {
565 frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
566 tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
567 tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
568 current_freq = radeon_div(tmp, ref_div * post_div);
569
Alex Deucherfc103322010-01-19 17:16:10 -0500570 if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
Dan Carpenter167ffc42010-07-17 12:28:02 +0200571 if (freq < current_freq)
572 error = 0xffffffff;
573 else
574 error = freq - current_freq;
Alex Deucherd0e275a2009-07-13 11:08:18 -0400575 } else
576 error = abs(current_freq - freq);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200577 vco_diff = abs(vco - best_vco);
578
579 if ((best_vco == 0 && error < best_error) ||
580 (best_vco != 0 &&
Dan Carpenter167ffc42010-07-17 12:28:02 +0200581 ((best_error > 100 && error < best_error - 100) ||
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200582 (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
583 best_post_div = post_div;
584 best_ref_div = ref_div;
585 best_feedback_div = feedback_div;
586 best_frac_feedback_div = frac_feedback_div;
587 best_freq = current_freq;
588 best_error = error;
589 best_vco_diff = vco_diff;
590 } else if (current_freq == freq) {
591 if (best_freq == -1) {
592 best_post_div = post_div;
593 best_ref_div = ref_div;
594 best_feedback_div = feedback_div;
595 best_frac_feedback_div = frac_feedback_div;
596 best_freq = current_freq;
597 best_error = error;
598 best_vco_diff = vco_diff;
Alex Deucherfc103322010-01-19 17:16:10 -0500599 } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
600 ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
601 ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
602 ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
603 ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
604 ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200605 best_post_div = post_div;
606 best_ref_div = ref_div;
607 best_feedback_div = feedback_div;
608 best_frac_feedback_div = frac_feedback_div;
609 best_freq = current_freq;
610 best_error = error;
611 best_vco_diff = vco_diff;
612 }
613 }
614 if (current_freq < freq)
615 min_frac_feed_div = frac_feedback_div + 1;
616 else
617 max_frac_feed_div = frac_feedback_div;
618 }
619 if (current_freq < freq)
620 min_feed_div = feedback_div + 1;
621 else
622 max_feed_div = feedback_div;
623 }
624 }
625 }
626
627 *dot_clock_p = best_freq / 10000;
628 *fb_div_p = best_feedback_div;
629 *frac_fb_div_p = best_frac_feedback_div;
630 *ref_div_p = best_ref_div;
631 *post_div_p = best_post_div;
632}
633
Alex Deucher383be5d2010-02-23 03:24:38 -0500634static bool
635calc_fb_div(struct radeon_pll *pll,
636 uint32_t freq,
637 uint32_t post_div,
638 uint32_t ref_div,
639 uint32_t *fb_div,
640 uint32_t *fb_div_frac)
Alex Deucherb27b6372009-12-09 17:44:25 -0500641{
Alex Deucher383be5d2010-02-23 03:24:38 -0500642 fixed20_12 feedback_divider, a, b;
643 u32 vco_freq;
Alex Deucherb27b6372009-12-09 17:44:25 -0500644
Alex Deucher383be5d2010-02-23 03:24:38 -0500645 vco_freq = freq * post_div;
646 /* feedback_divider = vco_freq * ref_div / pll->reference_freq; */
Ben Skeggs68adac52010-04-28 11:46:42 +1000647 a.full = dfixed_const(pll->reference_freq);
648 feedback_divider.full = dfixed_const(vco_freq);
649 feedback_divider.full = dfixed_div(feedback_divider, a);
650 a.full = dfixed_const(ref_div);
651 feedback_divider.full = dfixed_mul(feedback_divider, a);
Alex Deucher383be5d2010-02-23 03:24:38 -0500652
653 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
654 /* feedback_divider = floor((feedback_divider * 10.0) + 0.5) * 0.1; */
Ben Skeggs68adac52010-04-28 11:46:42 +1000655 a.full = dfixed_const(10);
656 feedback_divider.full = dfixed_mul(feedback_divider, a);
657 feedback_divider.full += dfixed_const_half(0);
658 feedback_divider.full = dfixed_floor(feedback_divider);
659 feedback_divider.full = dfixed_div(feedback_divider, a);
Alex Deucher383be5d2010-02-23 03:24:38 -0500660
661 /* *fb_div = floor(feedback_divider); */
Ben Skeggs68adac52010-04-28 11:46:42 +1000662 a.full = dfixed_floor(feedback_divider);
663 *fb_div = dfixed_trunc(a);
Alex Deucher383be5d2010-02-23 03:24:38 -0500664 /* *fb_div_frac = fmod(feedback_divider, 1.0) * 10.0; */
Ben Skeggs68adac52010-04-28 11:46:42 +1000665 a.full = dfixed_const(10);
666 b.full = dfixed_mul(feedback_divider, a);
Alex Deucher383be5d2010-02-23 03:24:38 -0500667
Ben Skeggs68adac52010-04-28 11:46:42 +1000668 feedback_divider.full = dfixed_floor(feedback_divider);
669 feedback_divider.full = dfixed_mul(feedback_divider, a);
Alex Deucher383be5d2010-02-23 03:24:38 -0500670 feedback_divider.full = b.full - feedback_divider.full;
Ben Skeggs68adac52010-04-28 11:46:42 +1000671 *fb_div_frac = dfixed_trunc(feedback_divider);
Alex Deucher383be5d2010-02-23 03:24:38 -0500672 } else {
673 /* *fb_div = floor(feedback_divider + 0.5); */
Ben Skeggs68adac52010-04-28 11:46:42 +1000674 feedback_divider.full += dfixed_const_half(0);
675 feedback_divider.full = dfixed_floor(feedback_divider);
Alex Deucher383be5d2010-02-23 03:24:38 -0500676
Ben Skeggs68adac52010-04-28 11:46:42 +1000677 *fb_div = dfixed_trunc(feedback_divider);
Alex Deucher383be5d2010-02-23 03:24:38 -0500678 *fb_div_frac = 0;
679 }
680
681 if (((*fb_div) < pll->min_feedback_div) || ((*fb_div) > pll->max_feedback_div))
682 return false;
683 else
684 return true;
685}
686
687static bool
688calc_fb_ref_div(struct radeon_pll *pll,
689 uint32_t freq,
690 uint32_t post_div,
691 uint32_t *fb_div,
692 uint32_t *fb_div_frac,
693 uint32_t *ref_div)
694{
695 fixed20_12 ffreq, max_error, error, pll_out, a;
696 u32 vco;
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500697 u32 pll_out_min, pll_out_max;
698
699 if (pll->flags & RADEON_PLL_IS_LCD) {
700 pll_out_min = pll->lcd_pll_out_min;
701 pll_out_max = pll->lcd_pll_out_max;
702 } else {
703 pll_out_min = pll->pll_out_min;
704 pll_out_max = pll->pll_out_max;
705 }
Alex Deucher383be5d2010-02-23 03:24:38 -0500706
Ben Skeggs68adac52010-04-28 11:46:42 +1000707 ffreq.full = dfixed_const(freq);
Alex Deucher383be5d2010-02-23 03:24:38 -0500708 /* max_error = ffreq * 0.0025; */
Ben Skeggs68adac52010-04-28 11:46:42 +1000709 a.full = dfixed_const(400);
710 max_error.full = dfixed_div(ffreq, a);
Alex Deucherb27b6372009-12-09 17:44:25 -0500711
Alex Deucher383be5d2010-02-23 03:24:38 -0500712 for ((*ref_div) = pll->min_ref_div; (*ref_div) < pll->max_ref_div; ++(*ref_div)) {
713 if (calc_fb_div(pll, freq, post_div, (*ref_div), fb_div, fb_div_frac)) {
714 vco = pll->reference_freq * (((*fb_div) * 10) + (*fb_div_frac));
715 vco = vco / ((*ref_div) * 10);
Alex Deucherb27b6372009-12-09 17:44:25 -0500716
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500717 if ((vco < pll_out_min) || (vco > pll_out_max))
Alex Deucher383be5d2010-02-23 03:24:38 -0500718 continue;
Alex Deucherb27b6372009-12-09 17:44:25 -0500719
Alex Deucher383be5d2010-02-23 03:24:38 -0500720 /* pll_out = vco / post_div; */
Ben Skeggs68adac52010-04-28 11:46:42 +1000721 a.full = dfixed_const(post_div);
722 pll_out.full = dfixed_const(vco);
723 pll_out.full = dfixed_div(pll_out, a);
Alex Deucherb27b6372009-12-09 17:44:25 -0500724
Alex Deucher383be5d2010-02-23 03:24:38 -0500725 if (pll_out.full >= ffreq.full) {
726 error.full = pll_out.full - ffreq.full;
727 if (error.full <= max_error.full)
728 return true;
729 }
730 }
731 }
732 return false;
733}
Alex Deucherb27b6372009-12-09 17:44:25 -0500734
Alex Deucher383be5d2010-02-23 03:24:38 -0500735static void radeon_compute_pll_new(struct radeon_pll *pll,
736 uint64_t freq,
737 uint32_t *dot_clock_p,
738 uint32_t *fb_div_p,
739 uint32_t *frac_fb_div_p,
740 uint32_t *ref_div_p,
741 uint32_t *post_div_p)
742{
743 u32 fb_div = 0, fb_div_frac = 0, post_div = 0, ref_div = 0;
744 u32 best_freq = 0, vco_frequency;
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500745 u32 pll_out_min, pll_out_max;
746
747 if (pll->flags & RADEON_PLL_IS_LCD) {
748 pll_out_min = pll->lcd_pll_out_min;
749 pll_out_max = pll->lcd_pll_out_max;
750 } else {
751 pll_out_min = pll->pll_out_min;
752 pll_out_max = pll->pll_out_max;
753 }
Alex Deucherb27b6372009-12-09 17:44:25 -0500754
Alex Deucher383be5d2010-02-23 03:24:38 -0500755 /* freq = freq / 10; */
756 do_div(freq, 10);
Alex Deucherb27b6372009-12-09 17:44:25 -0500757
Alex Deucher383be5d2010-02-23 03:24:38 -0500758 if (pll->flags & RADEON_PLL_USE_POST_DIV) {
759 post_div = pll->post_div;
760 if ((post_div < pll->min_post_div) || (post_div > pll->max_post_div))
761 goto done;
Alex Deucherb27b6372009-12-09 17:44:25 -0500762
Alex Deucher383be5d2010-02-23 03:24:38 -0500763 vco_frequency = freq * post_div;
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500764 if ((vco_frequency < pll_out_min) || (vco_frequency > pll_out_max))
Alex Deucher383be5d2010-02-23 03:24:38 -0500765 goto done;
Alex Deucherb27b6372009-12-09 17:44:25 -0500766
Alex Deucher383be5d2010-02-23 03:24:38 -0500767 if (pll->flags & RADEON_PLL_USE_REF_DIV) {
768 ref_div = pll->reference_div;
769 if ((ref_div < pll->min_ref_div) || (ref_div > pll->max_ref_div))
770 goto done;
771 if (!calc_fb_div(pll, freq, post_div, ref_div, &fb_div, &fb_div_frac))
772 goto done;
773 }
774 } else {
775 for (post_div = pll->max_post_div; post_div >= pll->min_post_div; --post_div) {
776 if (pll->flags & RADEON_PLL_LEGACY) {
777 if ((post_div == 5) ||
778 (post_div == 7) ||
779 (post_div == 9) ||
780 (post_div == 10) ||
781 (post_div == 11))
782 continue;
783 }
Alex Deucherb27b6372009-12-09 17:44:25 -0500784
Alex Deucher383be5d2010-02-23 03:24:38 -0500785 if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
786 continue;
787
788 vco_frequency = freq * post_div;
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500789 if ((vco_frequency < pll_out_min) || (vco_frequency > pll_out_max))
Alex Deucher383be5d2010-02-23 03:24:38 -0500790 continue;
791 if (pll->flags & RADEON_PLL_USE_REF_DIV) {
792 ref_div = pll->reference_div;
793 if ((ref_div < pll->min_ref_div) || (ref_div > pll->max_ref_div))
794 goto done;
795 if (calc_fb_div(pll, freq, post_div, ref_div, &fb_div, &fb_div_frac))
796 break;
797 } else {
798 if (calc_fb_ref_div(pll, freq, post_div, &fb_div, &fb_div_frac, &ref_div))
799 break;
800 }
Alex Deucherb27b6372009-12-09 17:44:25 -0500801 }
802 }
803
Alex Deucher383be5d2010-02-23 03:24:38 -0500804 best_freq = pll->reference_freq * 10 * fb_div;
805 best_freq += pll->reference_freq * fb_div_frac;
806 best_freq = best_freq / (ref_div * post_div);
Alex Deucherb27b6372009-12-09 17:44:25 -0500807
Alex Deucher383be5d2010-02-23 03:24:38 -0500808done:
809 if (best_freq == 0)
810 DRM_ERROR("Couldn't find valid PLL dividers\n");
Alex Deucherb27b6372009-12-09 17:44:25 -0500811
Alex Deucher383be5d2010-02-23 03:24:38 -0500812 *dot_clock_p = best_freq / 10;
813 *fb_div_p = fb_div;
814 *frac_fb_div_p = fb_div_frac;
815 *ref_div_p = ref_div;
816 *post_div_p = post_div;
Alex Deucherb27b6372009-12-09 17:44:25 -0500817
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000818 DRM_DEBUG_KMS("%u %d.%d, %d, %d\n", *dot_clock_p, *fb_div_p, *frac_fb_div_p, *ref_div_p, *post_div_p);
Alex Deucherb27b6372009-12-09 17:44:25 -0500819}
820
Alex Deucher7c27f872010-02-02 12:05:01 -0500821void radeon_compute_pll(struct radeon_pll *pll,
822 uint64_t freq,
823 uint32_t *dot_clock_p,
824 uint32_t *fb_div_p,
825 uint32_t *frac_fb_div_p,
826 uint32_t *ref_div_p,
827 uint32_t *post_div_p)
828{
829 switch (pll->algo) {
Alex Deucher383be5d2010-02-23 03:24:38 -0500830 case PLL_ALGO_NEW:
831 radeon_compute_pll_new(pll, freq, dot_clock_p, fb_div_p,
832 frac_fb_div_p, ref_div_p, post_div_p);
Alex Deucher7c27f872010-02-02 12:05:01 -0500833 break;
834 case PLL_ALGO_LEGACY:
835 default:
836 radeon_compute_pll_legacy(pll, freq, dot_clock_p, fb_div_p,
837 frac_fb_div_p, ref_div_p, post_div_p);
838 break;
839 }
Dave Airlie445282d2009-09-09 17:40:54 +1000840}
841
842static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
843{
844 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200845
Dave Airlie29d08b32010-09-27 16:17:17 +1000846 if (radeon_fb->obj) {
Luca Barbieribc9025b2010-02-09 05:49:12 +0000847 drm_gem_object_unreference_unlocked(radeon_fb->obj);
Dave Airlie29d08b32010-09-27 16:17:17 +1000848 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200849 drm_framebuffer_cleanup(fb);
850 kfree(radeon_fb);
851}
852
853static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
854 struct drm_file *file_priv,
855 unsigned int *handle)
856{
857 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
858
859 return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
860}
861
862static const struct drm_framebuffer_funcs radeon_fb_funcs = {
863 .destroy = radeon_user_framebuffer_destroy,
864 .create_handle = radeon_user_framebuffer_create_handle,
865};
866
Dave Airlie38651672010-03-30 05:34:13 +0000867void
868radeon_framebuffer_init(struct drm_device *dev,
869 struct radeon_framebuffer *rfb,
870 struct drm_mode_fb_cmd *mode_cmd,
871 struct drm_gem_object *obj)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200872{
Dave Airlie38651672010-03-30 05:34:13 +0000873 rfb->obj = obj;
874 drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
875 drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200876}
877
878static struct drm_framebuffer *
879radeon_user_framebuffer_create(struct drm_device *dev,
880 struct drm_file *file_priv,
881 struct drm_mode_fb_cmd *mode_cmd)
882{
883 struct drm_gem_object *obj;
Dave Airlie38651672010-03-30 05:34:13 +0000884 struct radeon_framebuffer *radeon_fb;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200885
886 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
Jerome Glisse7e71c9e2010-01-17 21:21:41 +0100887 if (obj == NULL) {
888 dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
889 "can't create framebuffer\n", mode_cmd->handle);
Chris Wilsoncce13ff2010-08-08 13:36:38 +0100890 return ERR_PTR(-ENOENT);
Jerome Glisse7e71c9e2010-01-17 21:21:41 +0100891 }
Dave Airlie38651672010-03-30 05:34:13 +0000892
893 radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
Chris Wilsoncce13ff2010-08-08 13:36:38 +0100894 if (radeon_fb == NULL)
895 return ERR_PTR(-ENOMEM);
Dave Airlie38651672010-03-30 05:34:13 +0000896
897 radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
898
899 return &radeon_fb->base;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200900}
901
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000902static void radeon_output_poll_changed(struct drm_device *dev)
903{
904 struct radeon_device *rdev = dev->dev_private;
905 radeon_fb_output_poll_changed(rdev);
906}
907
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200908static const struct drm_mode_config_funcs radeon_mode_funcs = {
909 .fb_create = radeon_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000910 .output_poll_changed = radeon_output_poll_changed
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200911};
912
Dave Airlie445282d2009-09-09 17:40:54 +1000913struct drm_prop_enum_list {
914 int type;
915 char *name;
916};
917
918static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
919{ { 0, "driver" },
920 { 1, "bios" },
921};
922
923static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
924{ { TV_STD_NTSC, "ntsc" },
925 { TV_STD_PAL, "pal" },
926 { TV_STD_PAL_M, "pal-m" },
927 { TV_STD_PAL_60, "pal-60" },
928 { TV_STD_NTSC_J, "ntsc-j" },
929 { TV_STD_SCART_PAL, "scart-pal" },
930 { TV_STD_PAL_CN, "pal-cn" },
931 { TV_STD_SECAM, "secam" },
932};
933
Alex Deucher5b1714d2010-08-03 19:59:20 -0400934static struct drm_prop_enum_list radeon_underscan_enum_list[] =
935{ { UNDERSCAN_OFF, "off" },
936 { UNDERSCAN_ON, "on" },
937 { UNDERSCAN_AUTO, "auto" },
938};
939
Alex Deucherd79766f2009-12-17 19:00:29 -0500940static int radeon_modeset_create_props(struct radeon_device *rdev)
Dave Airlie445282d2009-09-09 17:40:54 +1000941{
942 int i, sz;
943
944 if (rdev->is_atom_bios) {
945 rdev->mode_info.coherent_mode_property =
946 drm_property_create(rdev->ddev,
947 DRM_MODE_PROP_RANGE,
948 "coherent", 2);
949 if (!rdev->mode_info.coherent_mode_property)
950 return -ENOMEM;
951
952 rdev->mode_info.coherent_mode_property->values[0] = 0;
Alex Deucher390d0bb2009-12-08 12:48:20 -0500953 rdev->mode_info.coherent_mode_property->values[1] = 1;
Dave Airlie445282d2009-09-09 17:40:54 +1000954 }
955
956 if (!ASIC_IS_AVIVO(rdev)) {
957 sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
958 rdev->mode_info.tmds_pll_property =
959 drm_property_create(rdev->ddev,
960 DRM_MODE_PROP_ENUM,
961 "tmds_pll", sz);
962 for (i = 0; i < sz; i++) {
963 drm_property_add_enum(rdev->mode_info.tmds_pll_property,
964 i,
965 radeon_tmds_pll_enum_list[i].type,
966 radeon_tmds_pll_enum_list[i].name);
967 }
968 }
969
970 rdev->mode_info.load_detect_property =
971 drm_property_create(rdev->ddev,
972 DRM_MODE_PROP_RANGE,
973 "load detection", 2);
974 if (!rdev->mode_info.load_detect_property)
975 return -ENOMEM;
976 rdev->mode_info.load_detect_property->values[0] = 0;
Alex Deucher390d0bb2009-12-08 12:48:20 -0500977 rdev->mode_info.load_detect_property->values[1] = 1;
Dave Airlie445282d2009-09-09 17:40:54 +1000978
979 drm_mode_create_scaling_mode_property(rdev->ddev);
980
981 sz = ARRAY_SIZE(radeon_tv_std_enum_list);
982 rdev->mode_info.tv_std_property =
983 drm_property_create(rdev->ddev,
984 DRM_MODE_PROP_ENUM,
985 "tv standard", sz);
986 for (i = 0; i < sz; i++) {
987 drm_property_add_enum(rdev->mode_info.tv_std_property,
988 i,
989 radeon_tv_std_enum_list[i].type,
990 radeon_tv_std_enum_list[i].name);
991 }
992
Alex Deucher5b1714d2010-08-03 19:59:20 -0400993 sz = ARRAY_SIZE(radeon_underscan_enum_list);
994 rdev->mode_info.underscan_property =
995 drm_property_create(rdev->ddev,
996 DRM_MODE_PROP_ENUM,
997 "underscan", sz);
998 for (i = 0; i < sz; i++) {
999 drm_property_add_enum(rdev->mode_info.underscan_property,
1000 i,
1001 radeon_underscan_enum_list[i].type,
1002 radeon_underscan_enum_list[i].name);
1003 }
1004
Dave Airlie445282d2009-09-09 17:40:54 +10001005 return 0;
1006}
1007
Alex Deucherf46c0122010-03-31 00:33:27 -04001008void radeon_update_display_priority(struct radeon_device *rdev)
1009{
1010 /* adjustment options for the display watermarks */
1011 if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
1012 /* set display priority to high for r3xx, rv515 chips
1013 * this avoids flickering due to underflow to the
1014 * display controllers during heavy acceleration.
Alex Deucher45737442010-05-20 11:26:11 -04001015 * Don't force high on rs4xx igp chips as it seems to
1016 * affect the sound card. See kernel bug 15982.
Alex Deucherf46c0122010-03-31 00:33:27 -04001017 */
Alex Deucher45737442010-05-20 11:26:11 -04001018 if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
1019 !(rdev->flags & RADEON_IS_IGP))
Alex Deucherf46c0122010-03-31 00:33:27 -04001020 rdev->disp_priority = 2;
1021 else
1022 rdev->disp_priority = 0;
1023 } else
1024 rdev->disp_priority = radeon_disp_priority;
1025
1026}
1027
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001028int radeon_modeset_init(struct radeon_device *rdev)
1029{
Alex Deucher18917b62010-02-01 16:02:25 -05001030 int i;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001031 int ret;
1032
1033 drm_mode_config_init(rdev->ddev);
1034 rdev->mode_info.mode_config_initialized = true;
1035
1036 rdev->ddev->mode_config.funcs = (void *)&radeon_mode_funcs;
1037
1038 if (ASIC_IS_AVIVO(rdev)) {
1039 rdev->ddev->mode_config.max_width = 8192;
1040 rdev->ddev->mode_config.max_height = 8192;
1041 } else {
1042 rdev->ddev->mode_config.max_width = 4096;
1043 rdev->ddev->mode_config.max_height = 4096;
1044 }
1045
1046 rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
1047
Dave Airlie445282d2009-09-09 17:40:54 +10001048 ret = radeon_modeset_create_props(rdev);
1049 if (ret) {
1050 return ret;
1051 }
Dave Airliedfee5612009-10-02 09:19:09 +10001052
Alex Deucherf376b942010-08-05 21:21:16 -04001053 /* init i2c buses */
1054 radeon_i2c_init(rdev);
1055
Alex Deucher3c537882010-02-05 04:21:19 -05001056 /* check combios for a valid hardcoded EDID - Sun servers */
1057 if (!rdev->is_atom_bios) {
1058 /* check for hardcoded EDID in BIOS */
1059 radeon_combios_check_hardcoded_edid(rdev);
1060 }
1061
Dave Airliedfee5612009-10-02 09:19:09 +10001062 /* allocate crtcs */
Alex Deucher18917b62010-02-01 16:02:25 -05001063 for (i = 0; i < rdev->num_crtc; i++) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001064 radeon_crtc_init(rdev->ddev, i);
1065 }
1066
1067 /* okay we should have all the bios connectors */
1068 ret = radeon_setup_enc_conn(rdev->ddev);
1069 if (!ret) {
1070 return ret;
1071 }
Alex Deucherd4877cf2009-12-04 16:56:37 -05001072 /* initialize hpd */
1073 radeon_hpd_init(rdev);
Dave Airlie38651672010-03-30 05:34:13 +00001074
Alex Deucherce8f5372010-05-07 15:10:16 -04001075 /* Initialize power management */
1076 radeon_pm_init(rdev);
1077
Dave Airlie38651672010-03-30 05:34:13 +00001078 radeon_fbdev_init(rdev);
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001079 drm_kms_helper_poll_init(rdev->ddev);
1080
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001081 return 0;
1082}
1083
1084void radeon_modeset_fini(struct radeon_device *rdev)
1085{
Dave Airlie38651672010-03-30 05:34:13 +00001086 radeon_fbdev_fini(rdev);
Alex Deucher3c537882010-02-05 04:21:19 -05001087 kfree(rdev->mode_info.bios_hardcoded_edid);
Alex Deucherce8f5372010-05-07 15:10:16 -04001088 radeon_pm_fini(rdev);
Alex Deucher3c537882010-02-05 04:21:19 -05001089
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001090 if (rdev->mode_info.mode_config_initialized) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001091 drm_kms_helper_poll_fini(rdev->ddev);
Alex Deucherd4877cf2009-12-04 16:56:37 -05001092 radeon_hpd_fini(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001093 drm_mode_config_cleanup(rdev->ddev);
1094 rdev->mode_info.mode_config_initialized = false;
1095 }
Alex Deucherf376b942010-08-05 21:21:16 -04001096 /* free i2c buses */
1097 radeon_i2c_fini(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001098}
1099
Alex Deucher039ed2d2010-08-20 11:57:19 -04001100static bool is_hdtv_mode(struct drm_display_mode *mode)
1101{
1102 /* try and guess if this is a tv or a monitor */
1103 if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
1104 (mode->vdisplay == 576) || /* 576p */
1105 (mode->vdisplay == 720) || /* 720p */
1106 (mode->vdisplay == 1080)) /* 1080p */
1107 return true;
1108 else
1109 return false;
1110}
1111
Jerome Glissec93bb852009-07-13 21:04:08 +02001112bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
1113 struct drm_display_mode *mode,
1114 struct drm_display_mode *adjusted_mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001115{
Jerome Glissec93bb852009-07-13 21:04:08 +02001116 struct drm_device *dev = crtc->dev;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001117 struct radeon_device *rdev = dev->dev_private;
Jerome Glissec93bb852009-07-13 21:04:08 +02001118 struct drm_encoder *encoder;
1119 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1120 struct radeon_encoder *radeon_encoder;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001121 struct drm_connector *connector;
1122 struct radeon_connector *radeon_connector;
Jerome Glissec93bb852009-07-13 21:04:08 +02001123 bool first = true;
Alex Deucherd65d65b2010-08-03 19:58:49 -04001124 u32 src_v = 1, dst_v = 1;
1125 u32 src_h = 1, dst_h = 1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001126
Alex Deucher5b1714d2010-08-03 19:59:20 -04001127 radeon_crtc->h_border = 0;
1128 radeon_crtc->v_border = 0;
1129
Jerome Glissec93bb852009-07-13 21:04:08 +02001130 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Jerome Glissec93bb852009-07-13 21:04:08 +02001131 if (encoder->crtc != crtc)
1132 continue;
Alex Deucherd65d65b2010-08-03 19:58:49 -04001133 radeon_encoder = to_radeon_encoder(encoder);
Alex Deucher5b1714d2010-08-03 19:59:20 -04001134 connector = radeon_get_connector_for_encoder(encoder);
1135 radeon_connector = to_radeon_connector(connector);
1136
Jerome Glissec93bb852009-07-13 21:04:08 +02001137 if (first) {
Alex Deucher80297e82009-11-12 14:55:14 -05001138 /* set scaling */
1139 if (radeon_encoder->rmx_type == RMX_OFF)
1140 radeon_crtc->rmx_type = RMX_OFF;
1141 else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
1142 mode->vdisplay < radeon_encoder->native_mode.vdisplay)
1143 radeon_crtc->rmx_type = radeon_encoder->rmx_type;
1144 else
1145 radeon_crtc->rmx_type = RMX_OFF;
1146 /* copy native mode */
Jerome Glissec93bb852009-07-13 21:04:08 +02001147 memcpy(&radeon_crtc->native_mode,
Alex Deucher80297e82009-11-12 14:55:14 -05001148 &radeon_encoder->native_mode,
Alex Deucherde2103e2009-10-09 15:14:30 -04001149 sizeof(struct drm_display_mode));
Alex Deucherff32a592010-09-07 13:26:39 -04001150 src_v = crtc->mode.vdisplay;
1151 dst_v = radeon_crtc->native_mode.vdisplay;
1152 src_h = crtc->mode.hdisplay;
1153 dst_h = radeon_crtc->native_mode.hdisplay;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001154
1155 /* fix up for overscan on hdmi */
1156 if (ASIC_IS_AVIVO(rdev) &&
Alex Deuchere6db0da2010-09-10 03:19:05 -04001157 (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
Alex Deucher5b1714d2010-08-03 19:59:20 -04001158 ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
1159 ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
Alex Deucher039ed2d2010-08-20 11:57:19 -04001160 drm_detect_hdmi_monitor(radeon_connector->edid) &&
1161 is_hdtv_mode(mode)))) {
Alex Deucher5b1714d2010-08-03 19:59:20 -04001162 radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
1163 radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
1164 radeon_crtc->rmx_type = RMX_FULL;
1165 src_v = crtc->mode.vdisplay;
1166 dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
1167 src_h = crtc->mode.hdisplay;
1168 dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
1169 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001170 first = false;
1171 } else {
1172 if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
1173 /* WARNING: Right now this can't happen but
1174 * in the future we need to check that scaling
Alex Deucherd65d65b2010-08-03 19:58:49 -04001175 * are consistent across different encoder
Jerome Glissec93bb852009-07-13 21:04:08 +02001176 * (ie all encoder can work with the same
1177 * scaling).
1178 */
Alex Deucherd65d65b2010-08-03 19:58:49 -04001179 DRM_ERROR("Scaling not consistent across encoder.\n");
Jerome Glissec93bb852009-07-13 21:04:08 +02001180 return false;
1181 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001182 }
1183 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001184 if (radeon_crtc->rmx_type != RMX_OFF) {
1185 fixed20_12 a, b;
Alex Deucherd65d65b2010-08-03 19:58:49 -04001186 a.full = dfixed_const(src_v);
1187 b.full = dfixed_const(dst_v);
Ben Skeggs68adac52010-04-28 11:46:42 +10001188 radeon_crtc->vsc.full = dfixed_div(a, b);
Alex Deucherd65d65b2010-08-03 19:58:49 -04001189 a.full = dfixed_const(src_h);
1190 b.full = dfixed_const(dst_h);
Ben Skeggs68adac52010-04-28 11:46:42 +10001191 radeon_crtc->hsc.full = dfixed_div(a, b);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001192 } else {
Ben Skeggs68adac52010-04-28 11:46:42 +10001193 radeon_crtc->vsc.full = dfixed_const(1);
1194 radeon_crtc->hsc.full = dfixed_const(1);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001195 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001196 return true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001197}