2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
23 * Authors: Dave Airlie
27 #include "radeon_drm.h"
28 #include "radeon_drv.h"
30 #include "drm_crtc_helper.h"
32 #include "atom-bits.h"
34 static void atombios_lock_crtc(struct drm_crtc *crtc, int lock)
36 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
37 struct drm_device *dev = crtc->dev;
38 struct drm_radeon_private *dev_priv = dev->dev_private;
39 int index = GetIndexIntoMasterTable(COMMAND, UpdateCRTC_DoubleBufferRegisters);
40 ENABLE_CRTC_PS_ALLOCATION args;
42 memset(&args, 0, sizeof(args));
44 args.ucCRTC = radeon_crtc->crtc_id;
47 atom_execute_table(dev_priv->mode_info.atom_context, index, (uint32_t *)&args);
50 static void atombios_enable_crtc(struct drm_crtc *crtc, int state)
52 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
53 struct drm_device *dev = crtc->dev;
54 struct drm_radeon_private *dev_priv = dev->dev_private;
55 int index = GetIndexIntoMasterTable(COMMAND, EnableCRTC);
56 ENABLE_CRTC_PS_ALLOCATION args;
58 memset(&args, 0, sizeof(args));
60 args.ucCRTC = radeon_crtc->crtc_id;
61 args.ucEnable = state;
63 atom_execute_table(dev_priv->mode_info.atom_context, index, (uint32_t *)&args);
66 static void atombios_enable_crtc_memreq(struct drm_crtc *crtc, int state)
68 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
69 struct drm_device *dev = crtc->dev;
70 struct drm_radeon_private *dev_priv = dev->dev_private;
71 int index = GetIndexIntoMasterTable(COMMAND, EnableCRTCMemReq);
72 ENABLE_CRTC_PS_ALLOCATION args;
74 memset(&args, 0, sizeof(args));
76 args.ucCRTC = radeon_crtc->crtc_id;
77 args.ucEnable = state;
79 atom_execute_table(dev_priv->mode_info.atom_context, index, (uint32_t *)&args);
82 static void atombios_blank_crtc(struct drm_crtc *crtc, int state)
84 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
85 struct drm_device *dev = crtc->dev;
86 struct drm_radeon_private *dev_priv = dev->dev_private;
87 int index = GetIndexIntoMasterTable(COMMAND, BlankCRTC);
88 BLANK_CRTC_PS_ALLOCATION args;
90 memset(&args, 0, sizeof(args));
92 args.ucCRTC = radeon_crtc->crtc_id;
93 args.ucBlanking = state;
95 atom_execute_table(dev_priv->mode_info.atom_context, index, (uint32_t *)&args);
98 void atombios_crtc_dpms(struct drm_crtc *crtc, int mode)
100 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
101 struct drm_device *dev = crtc->dev;
102 struct drm_radeon_private *dev_priv = dev->dev_private;
105 case DRM_MODE_DPMS_ON:
106 case DRM_MODE_DPMS_STANDBY:
107 case DRM_MODE_DPMS_SUSPEND:
108 if (radeon_is_dce3(dev_priv))
109 atombios_enable_crtc_memreq(crtc, 1);
110 atombios_enable_crtc(crtc, 1);
111 atombios_blank_crtc(crtc, 0);
113 radeon_crtc_load_lut(crtc);
115 case DRM_MODE_DPMS_OFF:
116 atombios_blank_crtc(crtc, 1);
117 atombios_enable_crtc(crtc, 0);
118 if (radeon_is_dce3(dev_priv))
119 atombios_enable_crtc_memreq(crtc, 0);
125 void atombios_crtc_set_timing(struct drm_crtc *crtc, SET_CRTC_TIMING_PARAMETERS_PS_ALLOCATION *crtc_param)
127 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
128 struct drm_device *dev = crtc->dev;
129 struct drm_radeon_private *dev_priv = dev->dev_private;
130 SET_CRTC_TIMING_PARAMETERS_PS_ALLOCATION conv_param;
131 int index = GetIndexIntoMasterTable(COMMAND, SetCRTC_Timing);
133 conv_param.usH_Total = cpu_to_le16(crtc_param->usH_Total);
134 conv_param.usH_Disp = cpu_to_le16(crtc_param->usH_Disp);
135 conv_param.usH_SyncStart = cpu_to_le16(crtc_param->usH_SyncStart);
136 conv_param.usH_SyncWidth = cpu_to_le16(crtc_param->usH_SyncWidth);
137 conv_param.usV_Total = cpu_to_le16(crtc_param->usV_Total);
138 conv_param.usV_Disp = cpu_to_le16(crtc_param->usV_Disp);
139 conv_param.usV_SyncStart = cpu_to_le16(crtc_param->usV_SyncStart);
140 conv_param.usV_SyncWidth = cpu_to_le16(crtc_param->usV_SyncWidth);
141 conv_param.susModeMiscInfo.usAccess = cpu_to_le16(crtc_param->susModeMiscInfo.usAccess);
142 conv_param.ucCRTC = crtc_param->ucCRTC;
143 conv_param.ucOverscanRight = crtc_param->ucOverscanRight;
144 conv_param.ucOverscanLeft = crtc_param->ucOverscanLeft;
145 conv_param.ucOverscanBottom = crtc_param->ucOverscanBottom;
146 conv_param.ucOverscanTop = crtc_param->ucOverscanTop;
147 conv_param.ucReserved = crtc_param->ucReserved;
149 printk("executing set crtc timing\n");
150 atom_execute_table(dev_priv->mode_info.atom_context, index, (uint32_t *)&conv_param);
153 void atombios_crtc_set_pll(struct drm_crtc *crtc, struct drm_display_mode *mode)
155 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
156 struct drm_device *dev = crtc->dev;
157 struct drm_radeon_private *dev_priv = dev->dev_private;
159 int index = GetIndexIntoMasterTable(COMMAND, SetPixelClock);
160 SET_PIXEL_CLOCK_PS_ALLOCATION spc_param;
161 PIXEL_CLOCK_PARAMETERS_V2 *spc2_ptr;
162 PIXEL_CLOCK_PARAMETERS_V3 *spc3_ptr;
163 uint32_t sclock = mode->clock;
164 uint32_t ref_div = 0, fb_div = 0, post_div = 0;
165 struct radeon_pll *pll;
168 memset(&spc_param, 0, sizeof(SET_PIXEL_CLOCK_PS_ALLOCATION));
170 if (!radeon_is_avivo(dev_priv))
171 pll_flags |= RADEON_PLL_LEGACY;
173 if (mode->clock > 120000) /* range limits??? */
174 pll_flags |= RADEON_PLL_PREFER_HIGH_FB_DIV;
176 pll_flags |= RADEON_PLL_PREFER_LOW_REF_DIV;
178 if (radeon_crtc->crtc_id == 0)
179 pll = &dev_priv->mode_info.p1pll;
181 pll = &dev_priv->mode_info.p2pll;
183 radeon_compute_pll(pll, mode->clock, &sclock,
184 &fb_div, &ref_div, &post_div, pll_flags);
186 if (radeon_is_avivo(dev_priv)) {
189 if (radeon_crtc->crtc_id == 0) {
190 ss_cntl = RADEON_READ(AVIVO_P1PLL_INT_SS_CNTL);
191 RADEON_WRITE(AVIVO_P1PLL_INT_SS_CNTL, ss_cntl & ~1);
193 ss_cntl = RADEON_READ(AVIVO_P2PLL_INT_SS_CNTL);
194 RADEON_WRITE(AVIVO_P2PLL_INT_SS_CNTL, ss_cntl & ~1);
200 atom_parse_cmd_header(dev_priv->mode_info.atom_context, index, &frev, &crev);
207 spc2_ptr = (PIXEL_CLOCK_PARAMETERS_V2*)&spc_param.sPCLKInput;
208 spc2_ptr->usPixelClock = cpu_to_le16(sclock);
209 spc2_ptr->usRefDiv = cpu_to_le16(ref_div);
210 spc2_ptr->usFbDiv = cpu_to_le16(fb_div);
211 spc2_ptr->ucPostDiv = post_div;
212 spc2_ptr->ucPpll = radeon_crtc->crtc_id ? ATOM_PPLL2 : ATOM_PPLL1;
213 spc2_ptr->ucCRTC = radeon_crtc->crtc_id;
214 spc2_ptr->ucRefDivSrc = 1;
217 spc3_ptr = (PIXEL_CLOCK_PARAMETERS_V3*)&spc_param.sPCLKInput;
218 spc3_ptr->usPixelClock = cpu_to_le16(sclock);
219 spc3_ptr->usRefDiv = cpu_to_le16(ref_div);
220 spc3_ptr->usFbDiv = cpu_to_le16(fb_div);
221 spc3_ptr->ucPostDiv = post_div;
222 spc3_ptr->ucPpll = radeon_crtc->crtc_id ? ATOM_PPLL2 : ATOM_PPLL1;
223 spc3_ptr->ucMiscInfo = (radeon_crtc->crtc_id << 2);
225 /* TODO insert output encoder object stuff herre for r600 */
228 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
233 DRM_ERROR("Unknown table version %d %d\n", frev, crev);
237 printk("executing set pll\n");
238 atom_execute_table(dev_priv->mode_info.atom_context, index, (uint32_t *)&spc_param);
241 void atombios_crtc_set_base(struct drm_crtc *crtc, int x, int y)
243 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
244 struct drm_device *dev = crtc->dev;
245 struct drm_radeon_private *dev_priv = dev->dev_private;
246 struct radeon_framebuffer *radeon_fb;
247 struct drm_gem_object *obj;
248 struct drm_radeon_gem_object *obj_priv;
249 uint32_t fb_location, fb_format, fb_pitch_pixels;
254 radeon_fb = to_radeon_framebuffer(crtc->fb);
256 obj = radeon_fb->base.mm_private;
257 obj_priv = obj->driver_private;
259 fb_location = obj_priv->bo->offset + dev_priv->fb_location;
261 switch(crtc->fb->bits_per_pixel) {
263 fb_format = AVIVO_D1GRPH_CONTROL_DEPTH_16BPP | AVIVO_D1GRPH_CONTROL_16BPP_ARGB1555;
266 fb_format = AVIVO_D1GRPH_CONTROL_DEPTH_16BPP | AVIVO_D1GRPH_CONTROL_16BPP_RGB565;
270 fb_format = AVIVO_D1GRPH_CONTROL_DEPTH_32BPP | AVIVO_D1GRPH_CONTROL_32BPP_ARGB8888;
273 DRM_ERROR("Unsupported screen depth %d\n", crtc->fb->bits_per_pixel);
278 if (radeon_crtc->crtc_id == 0)
279 RADEON_WRITE(AVIVO_D1VGA_CONTROL, 0);
281 RADEON_WRITE(AVIVO_D2VGA_CONTROL, 0);
283 RADEON_WRITE(AVIVO_D1GRPH_PRIMARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset, fb_location);
284 RADEON_WRITE(AVIVO_D1GRPH_SECONDARY_SURFACE_ADDRESS + radeon_crtc->crtc_offset, fb_location);
285 RADEON_WRITE(AVIVO_D1GRPH_CONTROL + radeon_crtc->crtc_offset, fb_format);
287 RADEON_WRITE(AVIVO_D1GRPH_SURFACE_OFFSET_X + radeon_crtc->crtc_offset, 0);
288 RADEON_WRITE(AVIVO_D1GRPH_SURFACE_OFFSET_Y + radeon_crtc->crtc_offset, 0);
289 RADEON_WRITE(AVIVO_D1GRPH_X_START + radeon_crtc->crtc_offset, x);
290 RADEON_WRITE(AVIVO_D1GRPH_Y_START + radeon_crtc->crtc_offset, y);
291 RADEON_WRITE(AVIVO_D1GRPH_X_END + radeon_crtc->crtc_offset, x + crtc->mode.hdisplay);
292 RADEON_WRITE(AVIVO_D1GRPH_Y_END + radeon_crtc->crtc_offset, y + crtc->mode.vdisplay);
294 fb_pitch_pixels = crtc->fb->pitch / (crtc->fb->bits_per_pixel / 8);
295 RADEON_WRITE(AVIVO_D1GRPH_PITCH + radeon_crtc->crtc_offset, fb_pitch_pixels);
296 RADEON_WRITE(AVIVO_D1GRPH_ENABLE + radeon_crtc->crtc_offset, 1);
298 RADEON_WRITE(AVIVO_D1MODE_DESKTOP_HEIGHT + radeon_crtc->crtc_offset,
299 crtc->mode.vdisplay);
300 RADEON_WRITE(AVIVO_D1MODE_VIEWPORT_START + radeon_crtc->crtc_offset, (x << 16) | y);
301 RADEON_WRITE(AVIVO_D1MODE_VIEWPORT_SIZE + radeon_crtc->crtc_offset,
302 (crtc->mode.hdisplay << 16) | crtc->mode.vdisplay);
304 if (crtc->mode.flags & DRM_MODE_FLAG_INTERLACE)
305 RADEON_WRITE(AVIVO_D1MODE_DATA_FORMAT + radeon_crtc->crtc_offset,
306 AVIVO_D1MODE_INTERLEAVE_EN);
308 RADEON_WRITE(AVIVO_D1MODE_DATA_FORMAT + radeon_crtc->crtc_offset,
312 void atombios_crtc_mode_set(struct drm_crtc *crtc,
313 struct drm_display_mode *mode,
314 struct drm_display_mode *adjusted_mode,
317 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
318 struct drm_device *dev = crtc->dev;
319 struct drm_radeon_private *dev_priv = dev->dev_private;
320 struct drm_encoder *encoder;
321 SET_CRTC_TIMING_PARAMETERS_PS_ALLOCATION crtc_timing;
322 /* TODO color tiling */
324 memset(&crtc_timing, 0, sizeof(crtc_timing));
326 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
332 crtc_timing.ucCRTC = radeon_crtc->crtc_id;
333 crtc_timing.usH_Total = adjusted_mode->crtc_htotal;
334 crtc_timing.usH_Disp = adjusted_mode->crtc_hdisplay;
335 crtc_timing.usH_SyncStart = adjusted_mode->crtc_hsync_start;
336 crtc_timing.usH_SyncWidth = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
338 crtc_timing.usV_Total = adjusted_mode->crtc_vtotal;
339 crtc_timing.usV_Disp = adjusted_mode->crtc_vdisplay;
340 crtc_timing.usV_SyncStart = adjusted_mode->crtc_vsync_start;
341 crtc_timing.usV_SyncWidth = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
343 if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
344 crtc_timing.susModeMiscInfo.usAccess |= ATOM_VSYNC_POLARITY;
346 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
347 crtc_timing.susModeMiscInfo.usAccess |= ATOM_HSYNC_POLARITY;
349 if (adjusted_mode->flags & DRM_MODE_FLAG_CSYNC)
350 crtc_timing.susModeMiscInfo.usAccess |= ATOM_COMPOSITESYNC;
352 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
353 crtc_timing.susModeMiscInfo.usAccess |= ATOM_INTERLACE;
355 if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
356 crtc_timing.susModeMiscInfo.usAccess |= ATOM_DOUBLE_CLOCK_MODE;
358 if (radeon_is_avivo(dev_priv))
359 atombios_crtc_set_base(crtc, x, y);
361 radeon_crtc_set_base(crtc, x, y);
363 atombios_crtc_set_pll(crtc, adjusted_mode);
365 atombios_crtc_set_timing(crtc, &crtc_timing);
369 static bool atombios_crtc_mode_fixup(struct drm_crtc *crtc,
370 struct drm_display_mode *mode,
371 struct drm_display_mode *adjusted_mode)
377 static void atombios_crtc_prepare(struct drm_crtc *crtc)
379 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
380 atombios_lock_crtc(crtc, 1);
383 static void atombios_crtc_commit(struct drm_crtc *crtc)
385 atombios_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
386 atombios_lock_crtc(crtc, 0);
389 static const struct drm_crtc_helper_funcs atombios_helper_funcs = {
390 .dpms = atombios_crtc_dpms,
391 .mode_fixup = atombios_crtc_mode_fixup,
392 .mode_set = atombios_crtc_mode_set,
393 .mode_set_base = atombios_crtc_set_base,
394 .prepare = atombios_crtc_prepare,
395 .commit = atombios_crtc_commit,
398 void radeon_atombios_init_crtc(struct drm_device *dev,
399 struct radeon_crtc *radeon_crtc)
401 if (radeon_crtc->crtc_id == 1)
402 radeon_crtc->crtc_offset = AVIVO_D2CRTC_H_TOTAL - AVIVO_D1CRTC_H_TOTAL;
403 drm_crtc_helper_add(&radeon_crtc->base, &atombios_helper_funcs);