blob: 6130ec9ed502db0d049fee11296403049b3516aa [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,
164 u16 *blue, uint32_t size)
165{
166 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000167 int i;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200168
169 if (size != 256) {
170 return;
171 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200172
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000173 /* userspace palettes are always correct as is */
174 for (i = 0; i < 256; i++) {
175 radeon_crtc->lut_r[i] = red[i] >> 6;
176 radeon_crtc->lut_g[i] = green[i] >> 6;
177 radeon_crtc->lut_b[i] = blue[i] >> 6;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200178 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200179 radeon_crtc_load_lut(crtc);
180}
181
182static void radeon_crtc_destroy(struct drm_crtc *crtc)
183{
184 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
185
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200186 drm_crtc_cleanup(crtc);
187 kfree(radeon_crtc);
188}
189
190static const struct drm_crtc_funcs radeon_crtc_funcs = {
191 .cursor_set = radeon_crtc_cursor_set,
192 .cursor_move = radeon_crtc_cursor_move,
193 .gamma_set = radeon_crtc_gamma_set,
194 .set_config = drm_crtc_helper_set_config,
195 .destroy = radeon_crtc_destroy,
196};
197
198static void radeon_crtc_init(struct drm_device *dev, int index)
199{
200 struct radeon_device *rdev = dev->dev_private;
201 struct radeon_crtc *radeon_crtc;
202 int i;
203
204 radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
205 if (radeon_crtc == NULL)
206 return;
207
208 drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
209
210 drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
211 radeon_crtc->crtc_id = index;
Jerome Glissec93bb852009-07-13 21:04:08 +0200212 rdev->mode_info.crtcs[index] = radeon_crtc;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200213
Dave Airlie785b93e2009-08-28 15:46:53 +1000214#if 0
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200215 radeon_crtc->mode_set.crtc = &radeon_crtc->base;
216 radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
217 radeon_crtc->mode_set.num_connectors = 0;
Dave Airlie785b93e2009-08-28 15:46:53 +1000218#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200219
220 for (i = 0; i < 256; i++) {
221 radeon_crtc->lut_r[i] = i << 2;
222 radeon_crtc->lut_g[i] = i << 2;
223 radeon_crtc->lut_b[i] = i << 2;
224 }
225
226 if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
227 radeon_atombios_init_crtc(dev, radeon_crtc);
228 else
229 radeon_legacy_init_crtc(dev, radeon_crtc);
230}
231
232static const char *encoder_names[34] = {
233 "NONE",
234 "INTERNAL_LVDS",
235 "INTERNAL_TMDS1",
236 "INTERNAL_TMDS2",
237 "INTERNAL_DAC1",
238 "INTERNAL_DAC2",
239 "INTERNAL_SDVOA",
240 "INTERNAL_SDVOB",
241 "SI170B",
242 "CH7303",
243 "CH7301",
244 "INTERNAL_DVO1",
245 "EXTERNAL_SDVOA",
246 "EXTERNAL_SDVOB",
247 "TITFP513",
248 "INTERNAL_LVTM1",
249 "VT1623",
250 "HDMI_SI1930",
251 "HDMI_INTERNAL",
252 "INTERNAL_KLDSCP_TMDS1",
253 "INTERNAL_KLDSCP_DVO1",
254 "INTERNAL_KLDSCP_DAC1",
255 "INTERNAL_KLDSCP_DAC2",
256 "SI178",
257 "MVPU_FPGA",
258 "INTERNAL_DDI",
259 "VT1625",
260 "HDMI_SI1932",
261 "DP_AN9801",
262 "DP_DP501",
263 "INTERNAL_UNIPHY",
264 "INTERNAL_KLDSCP_LVTMA",
265 "INTERNAL_UNIPHY1",
266 "INTERNAL_UNIPHY2",
267};
268
Alex Deucher196c58d2010-01-07 14:22:32 -0500269static const char *connector_names[15] = {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200270 "Unknown",
271 "VGA",
272 "DVI-I",
273 "DVI-D",
274 "DVI-A",
275 "Composite",
276 "S-video",
277 "LVDS",
278 "Component",
279 "DIN",
280 "DisplayPort",
281 "HDMI-A",
282 "HDMI-B",
Alex Deucher196c58d2010-01-07 14:22:32 -0500283 "TV",
284 "eDP",
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200285};
286
Alex Deuchercbd46232010-06-07 02:24:54 -0400287static const char *hpd_names[6] = {
Alex Deuchereed45b32009-12-04 14:45:27 -0500288 "HPD1",
289 "HPD2",
290 "HPD3",
291 "HPD4",
292 "HPD5",
293 "HPD6",
294};
295
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200296static void radeon_print_display_setup(struct drm_device *dev)
297{
298 struct drm_connector *connector;
299 struct radeon_connector *radeon_connector;
300 struct drm_encoder *encoder;
301 struct radeon_encoder *radeon_encoder;
302 uint32_t devices;
303 int i = 0;
304
305 DRM_INFO("Radeon Display Connectors\n");
306 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
307 radeon_connector = to_radeon_connector(connector);
308 DRM_INFO("Connector %d:\n", i);
309 DRM_INFO(" %s\n", connector_names[connector->connector_type]);
Alex Deuchereed45b32009-12-04 14:45:27 -0500310 if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
311 DRM_INFO(" %s\n", hpd_names[radeon_connector->hpd.hpd]);
Dave Airlie4b9d2a22010-02-08 13:16:55 +1000312 if (radeon_connector->ddc_bus) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200313 DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
314 radeon_connector->ddc_bus->rec.mask_clk_reg,
315 radeon_connector->ddc_bus->rec.mask_data_reg,
316 radeon_connector->ddc_bus->rec.a_clk_reg,
317 radeon_connector->ddc_bus->rec.a_data_reg,
Alex Deucher9b9fe722009-11-10 15:59:44 -0500318 radeon_connector->ddc_bus->rec.en_clk_reg,
319 radeon_connector->ddc_bus->rec.en_data_reg,
320 radeon_connector->ddc_bus->rec.y_clk_reg,
321 radeon_connector->ddc_bus->rec.y_data_reg);
Alex Deucher26b5bc92010-08-05 21:21:18 -0400322 if (radeon_connector->router_bus)
323 DRM_INFO(" DDC Router 0x%x/0x%x\n",
324 radeon_connector->router.mux_control_pin,
325 radeon_connector->router.mux_state);
Dave Airlie4b9d2a22010-02-08 13:16:55 +1000326 } else {
327 if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
328 connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
329 connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
330 connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
331 connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
332 connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
333 DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
334 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200335 DRM_INFO(" Encoders:\n");
336 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
337 radeon_encoder = to_radeon_encoder(encoder);
338 devices = radeon_encoder->devices & radeon_connector->devices;
339 if (devices) {
340 if (devices & ATOM_DEVICE_CRT1_SUPPORT)
341 DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
342 if (devices & ATOM_DEVICE_CRT2_SUPPORT)
343 DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
344 if (devices & ATOM_DEVICE_LCD1_SUPPORT)
345 DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
346 if (devices & ATOM_DEVICE_DFP1_SUPPORT)
347 DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
348 if (devices & ATOM_DEVICE_DFP2_SUPPORT)
349 DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
350 if (devices & ATOM_DEVICE_DFP3_SUPPORT)
351 DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
352 if (devices & ATOM_DEVICE_DFP4_SUPPORT)
353 DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
354 if (devices & ATOM_DEVICE_DFP5_SUPPORT)
355 DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
356 if (devices & ATOM_DEVICE_TV1_SUPPORT)
357 DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
358 if (devices & ATOM_DEVICE_CV_SUPPORT)
359 DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
360 }
361 }
362 i++;
363 }
364}
365
Dave Airlie4ce001a2009-08-13 16:32:14 +1000366static bool radeon_setup_enc_conn(struct drm_device *dev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200367{
368 struct radeon_device *rdev = dev->dev_private;
369 struct drm_connector *drm_connector;
370 bool ret = false;
371
372 if (rdev->bios) {
373 if (rdev->is_atom_bios) {
Alex Deuchera084e6e2010-03-18 01:04:01 -0400374 ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
375 if (ret == false)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200376 ret = radeon_get_atom_connector_info_from_object_table(dev);
Alex Deucherb9597a12010-01-04 19:12:02 -0500377 } else {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200378 ret = radeon_get_legacy_connector_info_from_bios(dev);
Alex Deucherb9597a12010-01-04 19:12:02 -0500379 if (ret == false)
380 ret = radeon_get_legacy_connector_info_from_table(dev);
381 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200382 } else {
383 if (!ASIC_IS_AVIVO(rdev))
384 ret = radeon_get_legacy_connector_info_from_table(dev);
385 }
386 if (ret) {
Dave Airlie1f3b6a42009-10-13 14:10:37 +1000387 radeon_setup_encoder_clones(dev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200388 radeon_print_display_setup(dev);
389 list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head)
390 radeon_ddc_dump(drm_connector);
391 }
392
393 return ret;
394}
395
396int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
397{
Alex Deucher3c537882010-02-05 04:21:19 -0500398 struct drm_device *dev = radeon_connector->base.dev;
399 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200400 int ret = 0;
401
Alex Deucher26b5bc92010-08-05 21:21:18 -0400402 /* on hw with routers, select right port */
403 if (radeon_connector->router.valid)
404 radeon_router_select_port(radeon_connector);
405
Alex Deucher196c58d2010-01-07 14:22:32 -0500406 if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
407 (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)) {
Dave Airlie746c1aa2009-12-08 07:07:28 +1000408 struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
Dave Airlie7a15cbd42010-01-14 11:42:17 +1000409 if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
410 dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) && dig->dp_i2c_bus)
Alex Deucher9fa05c92009-11-27 13:01:46 -0500411 radeon_connector->edid = drm_get_edid(&radeon_connector->base, &dig->dp_i2c_bus->adapter);
Dave Airlie746c1aa2009-12-08 07:07:28 +1000412 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200413 if (!radeon_connector->ddc_bus)
414 return -1;
Dave Airlie4ce001a2009-08-13 16:32:14 +1000415 if (!radeon_connector->edid) {
Alex Deucher0294cf4f2009-10-15 16:16:35 -0400416 radeon_connector->edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
Alex Deucher0294cf4f2009-10-15 16:16:35 -0400417 }
Alex Deucher3c537882010-02-05 04:21:19 -0500418 /* some servers provide a hardcoded edid in rom for KVMs */
419 if (!radeon_connector->edid)
420 radeon_connector->edid = radeon_combios_get_hardcoded_edid(rdev);
Alex Deucher0294cf4f2009-10-15 16:16:35 -0400421 if (radeon_connector->edid) {
422 drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
423 ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200424 return ret;
425 }
426 drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
Dave Airlie42dea5d2009-09-15 20:21:11 +1000427 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200428}
429
430static int radeon_ddc_dump(struct drm_connector *connector)
431{
432 struct edid *edid;
433 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
434 int ret = 0;
435
Alex Deucher26b5bc92010-08-05 21:21:18 -0400436 /* on hw with routers, select right port */
437 if (radeon_connector->router.valid)
438 radeon_router_select_port(radeon_connector);
439
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200440 if (!radeon_connector->ddc_bus)
441 return -1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200442 edid = drm_get_edid(connector, &radeon_connector->ddc_bus->adapter);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200443 if (edid) {
444 kfree(edid);
445 }
446 return ret;
447}
448
449static inline uint32_t radeon_div(uint64_t n, uint32_t d)
450{
451 uint64_t mod;
452
453 n += d / 2;
454
455 mod = do_div(n, d);
456 return n;
457}
458
Alex Deucher7c27f872010-02-02 12:05:01 -0500459static void radeon_compute_pll_legacy(struct radeon_pll *pll,
460 uint64_t freq,
461 uint32_t *dot_clock_p,
462 uint32_t *fb_div_p,
463 uint32_t *frac_fb_div_p,
464 uint32_t *ref_div_p,
465 uint32_t *post_div_p)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200466{
467 uint32_t min_ref_div = pll->min_ref_div;
468 uint32_t max_ref_div = pll->max_ref_div;
Alex Deucherfc103322010-01-19 17:16:10 -0500469 uint32_t min_post_div = pll->min_post_div;
470 uint32_t max_post_div = pll->max_post_div;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200471 uint32_t min_fractional_feed_div = 0;
472 uint32_t max_fractional_feed_div = 0;
473 uint32_t best_vco = pll->best_vco;
474 uint32_t best_post_div = 1;
475 uint32_t best_ref_div = 1;
476 uint32_t best_feedback_div = 1;
477 uint32_t best_frac_feedback_div = 0;
478 uint32_t best_freq = -1;
479 uint32_t best_error = 0xffffffff;
480 uint32_t best_vco_diff = 1;
481 uint32_t post_div;
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500482 u32 pll_out_min, pll_out_max;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200483
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000484 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 +0200485 freq = freq * 1000;
486
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500487 if (pll->flags & RADEON_PLL_IS_LCD) {
488 pll_out_min = pll->lcd_pll_out_min;
489 pll_out_max = pll->lcd_pll_out_max;
490 } else {
491 pll_out_min = pll->pll_out_min;
492 pll_out_max = pll->pll_out_max;
493 }
494
Alex Deucherfc103322010-01-19 17:16:10 -0500495 if (pll->flags & RADEON_PLL_USE_REF_DIV)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200496 min_ref_div = max_ref_div = pll->reference_div;
497 else {
498 while (min_ref_div < max_ref_div-1) {
499 uint32_t mid = (min_ref_div + max_ref_div) / 2;
500 uint32_t pll_in = pll->reference_freq / mid;
501 if (pll_in < pll->pll_in_min)
502 max_ref_div = mid;
503 else if (pll_in > pll->pll_in_max)
504 min_ref_div = mid;
505 else
506 break;
507 }
508 }
509
Alex Deucherfc103322010-01-19 17:16:10 -0500510 if (pll->flags & RADEON_PLL_USE_POST_DIV)
511 min_post_div = max_post_div = pll->post_div;
512
513 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200514 min_fractional_feed_div = pll->min_frac_feedback_div;
515 max_fractional_feed_div = pll->max_frac_feedback_div;
516 }
517
Alex Deucherfc103322010-01-19 17:16:10 -0500518 for (post_div = min_post_div; post_div <= max_post_div; ++post_div) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200519 uint32_t ref_div;
520
Alex Deucherfc103322010-01-19 17:16:10 -0500521 if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200522 continue;
523
524 /* legacy radeons only have a few post_divs */
Alex Deucherfc103322010-01-19 17:16:10 -0500525 if (pll->flags & RADEON_PLL_LEGACY) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200526 if ((post_div == 5) ||
527 (post_div == 7) ||
528 (post_div == 9) ||
529 (post_div == 10) ||
530 (post_div == 11) ||
531 (post_div == 13) ||
532 (post_div == 14) ||
533 (post_div == 15))
534 continue;
535 }
536
537 for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
538 uint32_t feedback_div, current_freq = 0, error, vco_diff;
539 uint32_t pll_in = pll->reference_freq / ref_div;
540 uint32_t min_feed_div = pll->min_feedback_div;
541 uint32_t max_feed_div = pll->max_feedback_div + 1;
542
543 if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
544 continue;
545
546 while (min_feed_div < max_feed_div) {
547 uint32_t vco;
548 uint32_t min_frac_feed_div = min_fractional_feed_div;
549 uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
550 uint32_t frac_feedback_div;
551 uint64_t tmp;
552
553 feedback_div = (min_feed_div + max_feed_div) / 2;
554
555 tmp = (uint64_t)pll->reference_freq * feedback_div;
556 vco = radeon_div(tmp, ref_div);
557
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500558 if (vco < pll_out_min) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200559 min_feed_div = feedback_div + 1;
560 continue;
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500561 } else if (vco > pll_out_max) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200562 max_feed_div = feedback_div;
563 continue;
564 }
565
566 while (min_frac_feed_div < max_frac_feed_div) {
567 frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
568 tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
569 tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
570 current_freq = radeon_div(tmp, ref_div * post_div);
571
Alex Deucherfc103322010-01-19 17:16:10 -0500572 if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
Dan Carpenter167ffc42010-07-17 12:28:02 +0200573 if (freq < current_freq)
574 error = 0xffffffff;
575 else
576 error = freq - current_freq;
Alex Deucherd0e275a2009-07-13 11:08:18 -0400577 } else
578 error = abs(current_freq - freq);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200579 vco_diff = abs(vco - best_vco);
580
581 if ((best_vco == 0 && error < best_error) ||
582 (best_vco != 0 &&
Dan Carpenter167ffc42010-07-17 12:28:02 +0200583 ((best_error > 100 && error < best_error - 100) ||
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200584 (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
585 best_post_div = post_div;
586 best_ref_div = ref_div;
587 best_feedback_div = feedback_div;
588 best_frac_feedback_div = frac_feedback_div;
589 best_freq = current_freq;
590 best_error = error;
591 best_vco_diff = vco_diff;
592 } else if (current_freq == freq) {
593 if (best_freq == -1) {
594 best_post_div = post_div;
595 best_ref_div = ref_div;
596 best_feedback_div = feedback_div;
597 best_frac_feedback_div = frac_feedback_div;
598 best_freq = current_freq;
599 best_error = error;
600 best_vco_diff = vco_diff;
Alex Deucherfc103322010-01-19 17:16:10 -0500601 } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
602 ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
603 ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
604 ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
605 ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
606 ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200607 best_post_div = post_div;
608 best_ref_div = ref_div;
609 best_feedback_div = feedback_div;
610 best_frac_feedback_div = frac_feedback_div;
611 best_freq = current_freq;
612 best_error = error;
613 best_vco_diff = vco_diff;
614 }
615 }
616 if (current_freq < freq)
617 min_frac_feed_div = frac_feedback_div + 1;
618 else
619 max_frac_feed_div = frac_feedback_div;
620 }
621 if (current_freq < freq)
622 min_feed_div = feedback_div + 1;
623 else
624 max_feed_div = feedback_div;
625 }
626 }
627 }
628
629 *dot_clock_p = best_freq / 10000;
630 *fb_div_p = best_feedback_div;
631 *frac_fb_div_p = best_frac_feedback_div;
632 *ref_div_p = best_ref_div;
633 *post_div_p = best_post_div;
634}
635
Alex Deucher383be5d2010-02-23 03:24:38 -0500636static bool
637calc_fb_div(struct radeon_pll *pll,
638 uint32_t freq,
639 uint32_t post_div,
640 uint32_t ref_div,
641 uint32_t *fb_div,
642 uint32_t *fb_div_frac)
Alex Deucherb27b6372009-12-09 17:44:25 -0500643{
Alex Deucher383be5d2010-02-23 03:24:38 -0500644 fixed20_12 feedback_divider, a, b;
645 u32 vco_freq;
Alex Deucherb27b6372009-12-09 17:44:25 -0500646
Alex Deucher383be5d2010-02-23 03:24:38 -0500647 vco_freq = freq * post_div;
648 /* feedback_divider = vco_freq * ref_div / pll->reference_freq; */
Ben Skeggs68adac52010-04-28 11:46:42 +1000649 a.full = dfixed_const(pll->reference_freq);
650 feedback_divider.full = dfixed_const(vco_freq);
651 feedback_divider.full = dfixed_div(feedback_divider, a);
652 a.full = dfixed_const(ref_div);
653 feedback_divider.full = dfixed_mul(feedback_divider, a);
Alex Deucher383be5d2010-02-23 03:24:38 -0500654
655 if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
656 /* feedback_divider = floor((feedback_divider * 10.0) + 0.5) * 0.1; */
Ben Skeggs68adac52010-04-28 11:46:42 +1000657 a.full = dfixed_const(10);
658 feedback_divider.full = dfixed_mul(feedback_divider, a);
659 feedback_divider.full += dfixed_const_half(0);
660 feedback_divider.full = dfixed_floor(feedback_divider);
661 feedback_divider.full = dfixed_div(feedback_divider, a);
Alex Deucher383be5d2010-02-23 03:24:38 -0500662
663 /* *fb_div = floor(feedback_divider); */
Ben Skeggs68adac52010-04-28 11:46:42 +1000664 a.full = dfixed_floor(feedback_divider);
665 *fb_div = dfixed_trunc(a);
Alex Deucher383be5d2010-02-23 03:24:38 -0500666 /* *fb_div_frac = fmod(feedback_divider, 1.0) * 10.0; */
Ben Skeggs68adac52010-04-28 11:46:42 +1000667 a.full = dfixed_const(10);
668 b.full = dfixed_mul(feedback_divider, a);
Alex Deucher383be5d2010-02-23 03:24:38 -0500669
Ben Skeggs68adac52010-04-28 11:46:42 +1000670 feedback_divider.full = dfixed_floor(feedback_divider);
671 feedback_divider.full = dfixed_mul(feedback_divider, a);
Alex Deucher383be5d2010-02-23 03:24:38 -0500672 feedback_divider.full = b.full - feedback_divider.full;
Ben Skeggs68adac52010-04-28 11:46:42 +1000673 *fb_div_frac = dfixed_trunc(feedback_divider);
Alex Deucher383be5d2010-02-23 03:24:38 -0500674 } else {
675 /* *fb_div = floor(feedback_divider + 0.5); */
Ben Skeggs68adac52010-04-28 11:46:42 +1000676 feedback_divider.full += dfixed_const_half(0);
677 feedback_divider.full = dfixed_floor(feedback_divider);
Alex Deucher383be5d2010-02-23 03:24:38 -0500678
Ben Skeggs68adac52010-04-28 11:46:42 +1000679 *fb_div = dfixed_trunc(feedback_divider);
Alex Deucher383be5d2010-02-23 03:24:38 -0500680 *fb_div_frac = 0;
681 }
682
683 if (((*fb_div) < pll->min_feedback_div) || ((*fb_div) > pll->max_feedback_div))
684 return false;
685 else
686 return true;
687}
688
689static bool
690calc_fb_ref_div(struct radeon_pll *pll,
691 uint32_t freq,
692 uint32_t post_div,
693 uint32_t *fb_div,
694 uint32_t *fb_div_frac,
695 uint32_t *ref_div)
696{
697 fixed20_12 ffreq, max_error, error, pll_out, a;
698 u32 vco;
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500699 u32 pll_out_min, pll_out_max;
700
701 if (pll->flags & RADEON_PLL_IS_LCD) {
702 pll_out_min = pll->lcd_pll_out_min;
703 pll_out_max = pll->lcd_pll_out_max;
704 } else {
705 pll_out_min = pll->pll_out_min;
706 pll_out_max = pll->pll_out_max;
707 }
Alex Deucher383be5d2010-02-23 03:24:38 -0500708
Ben Skeggs68adac52010-04-28 11:46:42 +1000709 ffreq.full = dfixed_const(freq);
Alex Deucher383be5d2010-02-23 03:24:38 -0500710 /* max_error = ffreq * 0.0025; */
Ben Skeggs68adac52010-04-28 11:46:42 +1000711 a.full = dfixed_const(400);
712 max_error.full = dfixed_div(ffreq, a);
Alex Deucherb27b6372009-12-09 17:44:25 -0500713
Alex Deucher383be5d2010-02-23 03:24:38 -0500714 for ((*ref_div) = pll->min_ref_div; (*ref_div) < pll->max_ref_div; ++(*ref_div)) {
715 if (calc_fb_div(pll, freq, post_div, (*ref_div), fb_div, fb_div_frac)) {
716 vco = pll->reference_freq * (((*fb_div) * 10) + (*fb_div_frac));
717 vco = vco / ((*ref_div) * 10);
Alex Deucherb27b6372009-12-09 17:44:25 -0500718
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500719 if ((vco < pll_out_min) || (vco > pll_out_max))
Alex Deucher383be5d2010-02-23 03:24:38 -0500720 continue;
Alex Deucherb27b6372009-12-09 17:44:25 -0500721
Alex Deucher383be5d2010-02-23 03:24:38 -0500722 /* pll_out = vco / post_div; */
Ben Skeggs68adac52010-04-28 11:46:42 +1000723 a.full = dfixed_const(post_div);
724 pll_out.full = dfixed_const(vco);
725 pll_out.full = dfixed_div(pll_out, a);
Alex Deucherb27b6372009-12-09 17:44:25 -0500726
Alex Deucher383be5d2010-02-23 03:24:38 -0500727 if (pll_out.full >= ffreq.full) {
728 error.full = pll_out.full - ffreq.full;
729 if (error.full <= max_error.full)
730 return true;
731 }
732 }
733 }
734 return false;
735}
Alex Deucherb27b6372009-12-09 17:44:25 -0500736
Alex Deucher383be5d2010-02-23 03:24:38 -0500737static void radeon_compute_pll_new(struct radeon_pll *pll,
738 uint64_t freq,
739 uint32_t *dot_clock_p,
740 uint32_t *fb_div_p,
741 uint32_t *frac_fb_div_p,
742 uint32_t *ref_div_p,
743 uint32_t *post_div_p)
744{
745 u32 fb_div = 0, fb_div_frac = 0, post_div = 0, ref_div = 0;
746 u32 best_freq = 0, vco_frequency;
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500747 u32 pll_out_min, pll_out_max;
748
749 if (pll->flags & RADEON_PLL_IS_LCD) {
750 pll_out_min = pll->lcd_pll_out_min;
751 pll_out_max = pll->lcd_pll_out_max;
752 } else {
753 pll_out_min = pll->pll_out_min;
754 pll_out_max = pll->pll_out_max;
755 }
Alex Deucherb27b6372009-12-09 17:44:25 -0500756
Alex Deucher383be5d2010-02-23 03:24:38 -0500757 /* freq = freq / 10; */
758 do_div(freq, 10);
Alex Deucherb27b6372009-12-09 17:44:25 -0500759
Alex Deucher383be5d2010-02-23 03:24:38 -0500760 if (pll->flags & RADEON_PLL_USE_POST_DIV) {
761 post_div = pll->post_div;
762 if ((post_div < pll->min_post_div) || (post_div > pll->max_post_div))
763 goto done;
Alex Deucherb27b6372009-12-09 17:44:25 -0500764
Alex Deucher383be5d2010-02-23 03:24:38 -0500765 vco_frequency = freq * post_div;
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500766 if ((vco_frequency < pll_out_min) || (vco_frequency > pll_out_max))
Alex Deucher383be5d2010-02-23 03:24:38 -0500767 goto done;
Alex Deucherb27b6372009-12-09 17:44:25 -0500768
Alex Deucher383be5d2010-02-23 03:24:38 -0500769 if (pll->flags & RADEON_PLL_USE_REF_DIV) {
770 ref_div = pll->reference_div;
771 if ((ref_div < pll->min_ref_div) || (ref_div > pll->max_ref_div))
772 goto done;
773 if (!calc_fb_div(pll, freq, post_div, ref_div, &fb_div, &fb_div_frac))
774 goto done;
775 }
776 } else {
777 for (post_div = pll->max_post_div; post_div >= pll->min_post_div; --post_div) {
778 if (pll->flags & RADEON_PLL_LEGACY) {
779 if ((post_div == 5) ||
780 (post_div == 7) ||
781 (post_div == 9) ||
782 (post_div == 10) ||
783 (post_div == 11))
784 continue;
785 }
Alex Deucherb27b6372009-12-09 17:44:25 -0500786
Alex Deucher383be5d2010-02-23 03:24:38 -0500787 if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
788 continue;
789
790 vco_frequency = freq * post_div;
Alex Deucher86cb2bb2010-03-08 12:55:16 -0500791 if ((vco_frequency < pll_out_min) || (vco_frequency > pll_out_max))
Alex Deucher383be5d2010-02-23 03:24:38 -0500792 continue;
793 if (pll->flags & RADEON_PLL_USE_REF_DIV) {
794 ref_div = pll->reference_div;
795 if ((ref_div < pll->min_ref_div) || (ref_div > pll->max_ref_div))
796 goto done;
797 if (calc_fb_div(pll, freq, post_div, ref_div, &fb_div, &fb_div_frac))
798 break;
799 } else {
800 if (calc_fb_ref_div(pll, freq, post_div, &fb_div, &fb_div_frac, &ref_div))
801 break;
802 }
Alex Deucherb27b6372009-12-09 17:44:25 -0500803 }
804 }
805
Alex Deucher383be5d2010-02-23 03:24:38 -0500806 best_freq = pll->reference_freq * 10 * fb_div;
807 best_freq += pll->reference_freq * fb_div_frac;
808 best_freq = best_freq / (ref_div * post_div);
Alex Deucherb27b6372009-12-09 17:44:25 -0500809
Alex Deucher383be5d2010-02-23 03:24:38 -0500810done:
811 if (best_freq == 0)
812 DRM_ERROR("Couldn't find valid PLL dividers\n");
Alex Deucherb27b6372009-12-09 17:44:25 -0500813
Alex Deucher383be5d2010-02-23 03:24:38 -0500814 *dot_clock_p = best_freq / 10;
815 *fb_div_p = fb_div;
816 *frac_fb_div_p = fb_div_frac;
817 *ref_div_p = ref_div;
818 *post_div_p = post_div;
Alex Deucherb27b6372009-12-09 17:44:25 -0500819
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000820 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 -0500821}
822
Alex Deucher7c27f872010-02-02 12:05:01 -0500823void radeon_compute_pll(struct radeon_pll *pll,
824 uint64_t freq,
825 uint32_t *dot_clock_p,
826 uint32_t *fb_div_p,
827 uint32_t *frac_fb_div_p,
828 uint32_t *ref_div_p,
829 uint32_t *post_div_p)
830{
831 switch (pll->algo) {
Alex Deucher383be5d2010-02-23 03:24:38 -0500832 case PLL_ALGO_NEW:
833 radeon_compute_pll_new(pll, freq, dot_clock_p, fb_div_p,
834 frac_fb_div_p, ref_div_p, post_div_p);
Alex Deucher7c27f872010-02-02 12:05:01 -0500835 break;
836 case PLL_ALGO_LEGACY:
837 default:
838 radeon_compute_pll_legacy(pll, freq, dot_clock_p, fb_div_p,
839 frac_fb_div_p, ref_div_p, post_div_p);
840 break;
841 }
Dave Airlie445282d2009-09-09 17:40:54 +1000842}
843
844static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
845{
846 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200847
Luca Barbieribc9025b2010-02-09 05:49:12 +0000848 if (radeon_fb->obj)
849 drm_gem_object_unreference_unlocked(radeon_fb->obj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200850 drm_framebuffer_cleanup(fb);
851 kfree(radeon_fb);
852}
853
854static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
855 struct drm_file *file_priv,
856 unsigned int *handle)
857{
858 struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
859
860 return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
861}
862
863static const struct drm_framebuffer_funcs radeon_fb_funcs = {
864 .destroy = radeon_user_framebuffer_destroy,
865 .create_handle = radeon_user_framebuffer_create_handle,
866};
867
Dave Airlie38651672010-03-30 05:34:13 +0000868void
869radeon_framebuffer_init(struct drm_device *dev,
870 struct radeon_framebuffer *rfb,
871 struct drm_mode_fb_cmd *mode_cmd,
872 struct drm_gem_object *obj)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200873{
Dave Airlie38651672010-03-30 05:34:13 +0000874 rfb->obj = obj;
875 drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
876 drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200877}
878
879static struct drm_framebuffer *
880radeon_user_framebuffer_create(struct drm_device *dev,
881 struct drm_file *file_priv,
882 struct drm_mode_fb_cmd *mode_cmd)
883{
884 struct drm_gem_object *obj;
Dave Airlie38651672010-03-30 05:34:13 +0000885 struct radeon_framebuffer *radeon_fb;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200886
887 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
Jerome Glisse7e71c9e2010-01-17 21:21:41 +0100888 if (obj == NULL) {
889 dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
890 "can't create framebuffer\n", mode_cmd->handle);
Chris Wilsoncce13ff2010-08-08 13:36:38 +0100891 return ERR_PTR(-ENOENT);
Jerome Glisse7e71c9e2010-01-17 21:21:41 +0100892 }
Dave Airlie38651672010-03-30 05:34:13 +0000893
894 radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
Chris Wilsoncce13ff2010-08-08 13:36:38 +0100895 if (radeon_fb == NULL)
896 return ERR_PTR(-ENOMEM);
Dave Airlie38651672010-03-30 05:34:13 +0000897
898 radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
899
900 return &radeon_fb->base;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200901}
902
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000903static void radeon_output_poll_changed(struct drm_device *dev)
904{
905 struct radeon_device *rdev = dev->dev_private;
906 radeon_fb_output_poll_changed(rdev);
907}
908
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200909static const struct drm_mode_config_funcs radeon_mode_funcs = {
910 .fb_create = radeon_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +0000911 .output_poll_changed = radeon_output_poll_changed
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200912};
913
Dave Airlie445282d2009-09-09 17:40:54 +1000914struct drm_prop_enum_list {
915 int type;
916 char *name;
917};
918
919static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
920{ { 0, "driver" },
921 { 1, "bios" },
922};
923
924static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
925{ { TV_STD_NTSC, "ntsc" },
926 { TV_STD_PAL, "pal" },
927 { TV_STD_PAL_M, "pal-m" },
928 { TV_STD_PAL_60, "pal-60" },
929 { TV_STD_NTSC_J, "ntsc-j" },
930 { TV_STD_SCART_PAL, "scart-pal" },
931 { TV_STD_PAL_CN, "pal-cn" },
932 { TV_STD_SECAM, "secam" },
933};
934
Alex Deucher5b1714d2010-08-03 19:59:20 -0400935static struct drm_prop_enum_list radeon_underscan_enum_list[] =
936{ { UNDERSCAN_OFF, "off" },
937 { UNDERSCAN_ON, "on" },
938 { UNDERSCAN_AUTO, "auto" },
939};
940
Alex Deucherd79766f2009-12-17 19:00:29 -0500941static int radeon_modeset_create_props(struct radeon_device *rdev)
Dave Airlie445282d2009-09-09 17:40:54 +1000942{
943 int i, sz;
944
945 if (rdev->is_atom_bios) {
946 rdev->mode_info.coherent_mode_property =
947 drm_property_create(rdev->ddev,
948 DRM_MODE_PROP_RANGE,
949 "coherent", 2);
950 if (!rdev->mode_info.coherent_mode_property)
951 return -ENOMEM;
952
953 rdev->mode_info.coherent_mode_property->values[0] = 0;
Alex Deucher390d0bb2009-12-08 12:48:20 -0500954 rdev->mode_info.coherent_mode_property->values[1] = 1;
Dave Airlie445282d2009-09-09 17:40:54 +1000955 }
956
957 if (!ASIC_IS_AVIVO(rdev)) {
958 sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
959 rdev->mode_info.tmds_pll_property =
960 drm_property_create(rdev->ddev,
961 DRM_MODE_PROP_ENUM,
962 "tmds_pll", sz);
963 for (i = 0; i < sz; i++) {
964 drm_property_add_enum(rdev->mode_info.tmds_pll_property,
965 i,
966 radeon_tmds_pll_enum_list[i].type,
967 radeon_tmds_pll_enum_list[i].name);
968 }
969 }
970
971 rdev->mode_info.load_detect_property =
972 drm_property_create(rdev->ddev,
973 DRM_MODE_PROP_RANGE,
974 "load detection", 2);
975 if (!rdev->mode_info.load_detect_property)
976 return -ENOMEM;
977 rdev->mode_info.load_detect_property->values[0] = 0;
Alex Deucher390d0bb2009-12-08 12:48:20 -0500978 rdev->mode_info.load_detect_property->values[1] = 1;
Dave Airlie445282d2009-09-09 17:40:54 +1000979
980 drm_mode_create_scaling_mode_property(rdev->ddev);
981
982 sz = ARRAY_SIZE(radeon_tv_std_enum_list);
983 rdev->mode_info.tv_std_property =
984 drm_property_create(rdev->ddev,
985 DRM_MODE_PROP_ENUM,
986 "tv standard", sz);
987 for (i = 0; i < sz; i++) {
988 drm_property_add_enum(rdev->mode_info.tv_std_property,
989 i,
990 radeon_tv_std_enum_list[i].type,
991 radeon_tv_std_enum_list[i].name);
992 }
993
Alex Deucher5b1714d2010-08-03 19:59:20 -0400994 sz = ARRAY_SIZE(radeon_underscan_enum_list);
995 rdev->mode_info.underscan_property =
996 drm_property_create(rdev->ddev,
997 DRM_MODE_PROP_ENUM,
998 "underscan", sz);
999 for (i = 0; i < sz; i++) {
1000 drm_property_add_enum(rdev->mode_info.underscan_property,
1001 i,
1002 radeon_underscan_enum_list[i].type,
1003 radeon_underscan_enum_list[i].name);
1004 }
1005
Dave Airlie445282d2009-09-09 17:40:54 +10001006 return 0;
1007}
1008
Alex Deucherf46c0122010-03-31 00:33:27 -04001009void radeon_update_display_priority(struct radeon_device *rdev)
1010{
1011 /* adjustment options for the display watermarks */
1012 if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
1013 /* set display priority to high for r3xx, rv515 chips
1014 * this avoids flickering due to underflow to the
1015 * display controllers during heavy acceleration.
Alex Deucher45737442010-05-20 11:26:11 -04001016 * Don't force high on rs4xx igp chips as it seems to
1017 * affect the sound card. See kernel bug 15982.
Alex Deucherf46c0122010-03-31 00:33:27 -04001018 */
Alex Deucher45737442010-05-20 11:26:11 -04001019 if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
1020 !(rdev->flags & RADEON_IS_IGP))
Alex Deucherf46c0122010-03-31 00:33:27 -04001021 rdev->disp_priority = 2;
1022 else
1023 rdev->disp_priority = 0;
1024 } else
1025 rdev->disp_priority = radeon_disp_priority;
1026
1027}
1028
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001029int radeon_modeset_init(struct radeon_device *rdev)
1030{
Alex Deucher18917b62010-02-01 16:02:25 -05001031 int i;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001032 int ret;
1033
1034 drm_mode_config_init(rdev->ddev);
1035 rdev->mode_info.mode_config_initialized = true;
1036
1037 rdev->ddev->mode_config.funcs = (void *)&radeon_mode_funcs;
1038
1039 if (ASIC_IS_AVIVO(rdev)) {
1040 rdev->ddev->mode_config.max_width = 8192;
1041 rdev->ddev->mode_config.max_height = 8192;
1042 } else {
1043 rdev->ddev->mode_config.max_width = 4096;
1044 rdev->ddev->mode_config.max_height = 4096;
1045 }
1046
1047 rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
1048
Dave Airlie445282d2009-09-09 17:40:54 +10001049 ret = radeon_modeset_create_props(rdev);
1050 if (ret) {
1051 return ret;
1052 }
Dave Airliedfee5612009-10-02 09:19:09 +10001053
Alex Deucherf376b942010-08-05 21:21:16 -04001054 /* init i2c buses */
1055 radeon_i2c_init(rdev);
1056
Alex Deucher3c537882010-02-05 04:21:19 -05001057 /* check combios for a valid hardcoded EDID - Sun servers */
1058 if (!rdev->is_atom_bios) {
1059 /* check for hardcoded EDID in BIOS */
1060 radeon_combios_check_hardcoded_edid(rdev);
1061 }
1062
Dave Airliedfee5612009-10-02 09:19:09 +10001063 /* allocate crtcs */
Alex Deucher18917b62010-02-01 16:02:25 -05001064 for (i = 0; i < rdev->num_crtc; i++) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001065 radeon_crtc_init(rdev->ddev, i);
1066 }
1067
1068 /* okay we should have all the bios connectors */
1069 ret = radeon_setup_enc_conn(rdev->ddev);
1070 if (!ret) {
1071 return ret;
1072 }
Alex Deucherd4877cf2009-12-04 16:56:37 -05001073 /* initialize hpd */
1074 radeon_hpd_init(rdev);
Dave Airlie38651672010-03-30 05:34:13 +00001075
Alex Deucherce8f5372010-05-07 15:10:16 -04001076 /* Initialize power management */
1077 radeon_pm_init(rdev);
1078
Dave Airlie38651672010-03-30 05:34:13 +00001079 radeon_fbdev_init(rdev);
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001080 drm_kms_helper_poll_init(rdev->ddev);
1081
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001082 return 0;
1083}
1084
1085void radeon_modeset_fini(struct radeon_device *rdev)
1086{
Dave Airlie38651672010-03-30 05:34:13 +00001087 radeon_fbdev_fini(rdev);
Alex Deucher3c537882010-02-05 04:21:19 -05001088 kfree(rdev->mode_info.bios_hardcoded_edid);
Alex Deucherce8f5372010-05-07 15:10:16 -04001089 radeon_pm_fini(rdev);
Alex Deucher3c537882010-02-05 04:21:19 -05001090
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001091 if (rdev->mode_info.mode_config_initialized) {
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001092 drm_kms_helper_poll_fini(rdev->ddev);
Alex Deucherd4877cf2009-12-04 16:56:37 -05001093 radeon_hpd_fini(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001094 drm_mode_config_cleanup(rdev->ddev);
1095 rdev->mode_info.mode_config_initialized = false;
1096 }
Alex Deucherf376b942010-08-05 21:21:16 -04001097 /* free i2c buses */
1098 radeon_i2c_fini(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001099}
1100
Jerome Glissec93bb852009-07-13 21:04:08 +02001101bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
1102 struct drm_display_mode *mode,
1103 struct drm_display_mode *adjusted_mode)
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001104{
Jerome Glissec93bb852009-07-13 21:04:08 +02001105 struct drm_device *dev = crtc->dev;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001106 struct radeon_device *rdev = dev->dev_private;
Jerome Glissec93bb852009-07-13 21:04:08 +02001107 struct drm_encoder *encoder;
1108 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1109 struct radeon_encoder *radeon_encoder;
Alex Deucher5b1714d2010-08-03 19:59:20 -04001110 struct drm_connector *connector;
1111 struct radeon_connector *radeon_connector;
Jerome Glissec93bb852009-07-13 21:04:08 +02001112 bool first = true;
Alex Deucherd65d65b2010-08-03 19:58:49 -04001113 u32 src_v = 1, dst_v = 1;
1114 u32 src_h = 1, dst_h = 1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001115
Alex Deucher5b1714d2010-08-03 19:59:20 -04001116 radeon_crtc->h_border = 0;
1117 radeon_crtc->v_border = 0;
1118
Jerome Glissec93bb852009-07-13 21:04:08 +02001119 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Jerome Glissec93bb852009-07-13 21:04:08 +02001120 if (encoder->crtc != crtc)
1121 continue;
Alex Deucherd65d65b2010-08-03 19:58:49 -04001122 radeon_encoder = to_radeon_encoder(encoder);
Alex Deucher5b1714d2010-08-03 19:59:20 -04001123 connector = radeon_get_connector_for_encoder(encoder);
1124 radeon_connector = to_radeon_connector(connector);
1125
Jerome Glissec93bb852009-07-13 21:04:08 +02001126 if (first) {
Alex Deucher80297e82009-11-12 14:55:14 -05001127 /* set scaling */
1128 if (radeon_encoder->rmx_type == RMX_OFF)
1129 radeon_crtc->rmx_type = RMX_OFF;
1130 else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
1131 mode->vdisplay < radeon_encoder->native_mode.vdisplay)
1132 radeon_crtc->rmx_type = radeon_encoder->rmx_type;
1133 else
1134 radeon_crtc->rmx_type = RMX_OFF;
Alex Deucherd65d65b2010-08-03 19:58:49 -04001135 src_v = crtc->mode.vdisplay;
1136 dst_v = radeon_crtc->native_mode.vdisplay;
1137 src_h = crtc->mode.hdisplay;
1138 dst_h = radeon_crtc->native_mode.vdisplay;
Alex Deucher80297e82009-11-12 14:55:14 -05001139 /* copy native mode */
Jerome Glissec93bb852009-07-13 21:04:08 +02001140 memcpy(&radeon_crtc->native_mode,
Alex Deucher80297e82009-11-12 14:55:14 -05001141 &radeon_encoder->native_mode,
Alex Deucherde2103e2009-10-09 15:14:30 -04001142 sizeof(struct drm_display_mode));
Alex Deucher5b1714d2010-08-03 19:59:20 -04001143
1144 /* fix up for overscan on hdmi */
1145 if (ASIC_IS_AVIVO(rdev) &&
1146 ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
1147 ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
1148 drm_detect_hdmi_monitor(radeon_connector->edid)))) {
1149 radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
1150 radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
1151 radeon_crtc->rmx_type = RMX_FULL;
1152 src_v = crtc->mode.vdisplay;
1153 dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
1154 src_h = crtc->mode.hdisplay;
1155 dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
1156 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001157 first = false;
1158 } else {
1159 if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
1160 /* WARNING: Right now this can't happen but
1161 * in the future we need to check that scaling
Alex Deucherd65d65b2010-08-03 19:58:49 -04001162 * are consistent across different encoder
Jerome Glissec93bb852009-07-13 21:04:08 +02001163 * (ie all encoder can work with the same
1164 * scaling).
1165 */
Alex Deucherd65d65b2010-08-03 19:58:49 -04001166 DRM_ERROR("Scaling not consistent across encoder.\n");
Jerome Glissec93bb852009-07-13 21:04:08 +02001167 return false;
1168 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001169 }
1170 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001171 if (radeon_crtc->rmx_type != RMX_OFF) {
1172 fixed20_12 a, b;
Alex Deucherd65d65b2010-08-03 19:58:49 -04001173 a.full = dfixed_const(src_v);
1174 b.full = dfixed_const(dst_v);
Ben Skeggs68adac52010-04-28 11:46:42 +10001175 radeon_crtc->vsc.full = dfixed_div(a, b);
Alex Deucherd65d65b2010-08-03 19:58:49 -04001176 a.full = dfixed_const(src_h);
1177 b.full = dfixed_const(dst_h);
Ben Skeggs68adac52010-04-28 11:46:42 +10001178 radeon_crtc->hsc.full = dfixed_div(a, b);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001179 } else {
Ben Skeggs68adac52010-04-28 11:46:42 +10001180 radeon_crtc->vsc.full = dfixed_const(1);
1181 radeon_crtc->hsc.full = dfixed_const(1);
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001182 }
Jerome Glissec93bb852009-07-13 21:04:08 +02001183 return true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001184}