blob: 438c6000c385d9adb84c2cfedb42e0eef2bef246 [file] [log] [blame]
Nico Huber83693c82016-10-08 22:17:55 +02001--
Nico Huberfdb0df12018-02-07 14:30:34 +01002-- Copyright (C) 2015-2018 secunet Security Networks AG
Nico Huber83693c82016-10-08 22:17:55 +02003--
4-- This program is free software; you can redistribute it and/or modify
5-- it under the terms of the GNU General Public License as published by
Nico Huber125a29e2016-10-18 00:23:54 +02006-- the Free Software Foundation; either version 2 of the License, or
7-- (at your option) any later version.
Nico Huber83693c82016-10-08 22:17:55 +02008--
9-- This program is distributed in the hope that it will be useful,
10-- but WITHOUT ANY WARRANTY; without even the implied warranty of
11-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12-- GNU General Public License for more details.
13--
14
15with HW.Debug;
16with GNAT.Source_Info;
17
Nico Huber7ad2d652016-12-07 15:19:32 +010018with HW.GFX.GMA.Transcoder;
Nico Huber83693c82016-10-08 22:17:55 +020019
20package body HW.GFX.GMA.Pipe_Setup is
21
Nico Huberfbb42202016-11-07 15:08:26 +010022 ILK_DISPLAY_CHICKEN1_VGA_MASK : constant := 7 * 2 ** 29;
23 ILK_DISPLAY_CHICKEN1_VGA_ENABLE : constant := 5 * 2 ** 29;
24 ILK_DISPLAY_CHICKEN2_VGA_MASK : constant := 1 * 2 ** 25;
25 ILK_DISPLAY_CHICKEN2_VGA_ENABLE : constant := 0 * 2 ** 25;
26
Nico Huber7ad2d652016-12-07 15:19:32 +010027 DSPCNTR_ENABLE : constant := 1 * 2 ** 31;
28 DSPCNTR_GAMMA_CORRECTION : constant := 1 * 2 ** 30;
Nico Huber7ad2d652016-12-07 15:19:32 +010029 DSPCNTR_FORMAT_MASK : constant := 15 * 2 ** 26;
Arthur Heymans960e2392026-03-03 19:45:24 +010030 DSPCNTR_PIPE_SEL_MASK : constant := 3 * 2 ** 24;
31 DSPCNTR_PIPE_B_SELECT : constant := 1 * 2 ** 24;
Nico Huberab69e362018-05-29 21:20:30 +020032 DSPCNTR_DISABLE_TRICKLE_FEED : constant := 1 * 2 ** 14;
33 DSPCNTR_TILED_SURFACE_LINEAR : constant := 0 * 2 ** 10;
34 DSPCNTR_TILED_SURFACE_X_TILED : constant := 1 * 2 ** 10;
35
36 DSPCNTR_TILED_SURFACE : constant array (Tiling_Type) of Word32 :=
37 (Linear => DSPCNTR_TILED_SURFACE_LINEAR,
38 X_Tiled => DSPCNTR_TILED_SURFACE_X_TILED,
39 Y_Tiled => 0); -- unsupported
Nico Huber83693c82016-10-08 22:17:55 +020040
Arthur Heymans960e2392026-03-03 19:45:24 +010041 function DSPCNTR_PIPE_SEL (Pipe : Pipe_Index) return Word32 is
42 (if Pipe = Secondary then DSPCNTR_PIPE_B_SELECT else 0);
43
Nico Huber83693c82016-10-08 22:17:55 +020044 DSPCNTR_MASK : constant Word32 :=
45 DSPCNTR_ENABLE or
46 DSPCNTR_GAMMA_CORRECTION or
47 DSPCNTR_FORMAT_MASK or
Arthur Heymans960e2392026-03-03 19:45:24 +010048 DSPCNTR_PIPE_SEL_MASK or
Nico Huberab69e362018-05-29 21:20:30 +020049 DSPCNTR_DISABLE_TRICKLE_FEED or
50 DSPCNTR_TILED_SURFACE_X_TILED;
Nico Huber83693c82016-10-08 22:17:55 +020051
Tim Wawrzynczak0da761a2022-09-09 10:42:36 -060052 PLANE_COLOR_CTL_PLANE_GAMMA_DISABLE : constant := 1 * 2 ** 13;
53
Nico Huber83693c82016-10-08 22:17:55 +020054 PLANE_CTL_PLANE_ENABLE : constant := 1 * 2 ** 31;
55 PLANE_CTL_SRC_PIX_FMT_RGB_32B_8888 : constant := 4 * 2 ** 24;
56 PLANE_CTL_PLANE_GAMMA_DISABLE : constant := 1 * 2 ** 13;
Nico Huber0164b022017-08-24 15:12:51 +020057 PLANE_CTL_TILED_SURFACE_MASK : constant := 7 * 2 ** 10;
58 PLANE_CTL_TILED_SURFACE_LINEAR : constant := 0 * 2 ** 10;
59 PLANE_CTL_TILED_SURFACE_X_TILED : constant := 1 * 2 ** 10;
60 PLANE_CTL_TILED_SURFACE_Y_TILED : constant := 4 * 2 ** 10;
61 PLANE_CTL_TILED_SURFACE_YF_TILED : constant := 5 * 2 ** 10;
62
63 PLANE_CTL_TILED_SURFACE : constant array (Tiling_Type) of Word32 :=
64 (Linear => PLANE_CTL_TILED_SURFACE_LINEAR,
65 X_Tiled => PLANE_CTL_TILED_SURFACE_X_TILED,
66 Y_Tiled => PLANE_CTL_TILED_SURFACE_Y_TILED);
Nico Huber83693c82016-10-08 22:17:55 +020067
Nico Huber9b479412017-08-27 11:55:56 +020068 PLANE_CTL_PLANE_ROTATION_MASK : constant := 3 * 2 ** 0;
69 PLANE_CTL_PLANE_ROTATION : constant array (Rotation_Type) of Word32 :=
70 (No_Rotation => 0 * 2 ** 0,
71 Rotated_90 => 1 * 2 ** 0,
72 Rotated_180 => 2 * 2 ** 0,
73 Rotated_270 => 3 * 2 ** 0);
74
Nico Huber83693c82016-10-08 22:17:55 +020075 PLANE_WM_ENABLE : constant := 1 * 2 ** 31;
76 PLANE_WM_LINES_SHIFT : constant := 14;
77 PLANE_WM_LINES_MASK : constant := 16#001f# * 2 ** 14;
Tim Wawrzynczak0da761a2022-09-09 10:42:36 -060078 PLANE_WM_BLOCKS_MASK : constant :=
79 (if Config.Has_Wide_Watermarks then 16#7ff# else 16#3ff#);
80
81 PIPEMISC_HDR_MODE_PRECISION : constant := 1 * 2 ** 23;
82 PIPEMISC_PIXEL_ROUNDING_TRUNC : constant := 1 * 2 ** 8;
Nico Huber83693c82016-10-08 22:17:55 +020083
Nico Huber33912aa2016-12-06 20:36:23 +010084 VGA_SR_INDEX : constant := 16#03c4#;
85 VGA_SR_DATA : constant := 16#03c5#;
86 VGA_SR01 : constant := 16#01#;
87 VGA_SR01_SCREEN_OFF : constant := 1 * 2 ** 5;
Nico Huber3675db52016-11-04 16:27:29 +010088
89 VGA_CONTROL_VGA_DISPLAY_DISABLE : constant := 1 * 2 ** 31;
90 VGA_CONTROL_BLINK_DUTY_CYCLE_MASK : constant := 16#0003# * 2 ** 6;
91 VGA_CONTROL_BLINK_DUTY_CYCLE_50 : constant := 2 * 2 ** 6;
92 VGA_CONTROL_VSYNC_BLINK_RATE_MASK : constant := 16#003f# * 2 ** 0;
93
Nico Huber4dc4c612018-01-10 15:55:09 +010094 CUR_CTL_PIPE_SELECT : constant array (Pipe_Index) of Word32 :=
95 (Primary => 0 * 2 ** 28,
96 Secondary => 1 * 2 ** 28,
97 Tertiary => 2 * 2 ** 28);
98 CUR_CTL_MODE : constant array (Cursor_Mode, Cursor_Size) of Word32 :=
99 (No_Cursor => (others => 16#00#),
100 ARGB_Cursor =>
101 (Cursor_64x64 => 16#27#,
102 Cursor_128x128 => 16#22#,
103 Cursor_256x256 => 16#23#));
Tim Wawrzynczak0da761a2022-09-09 10:42:36 -0600104 subtype ARB_Slots is Natural range 0 .. 7;
105 function MCURSOR_ARB_SLOTS (N : ARB_Slots) return Word32 is
106 (Shift_Left (Word32 (N), 28));
Nico Huber4dc4c612018-01-10 15:55:09 +0100107
108 function CUR_POS_Y (Y : Int32) return Word32 is
109 ((if Y >= 0 then 0 else 1 * 2 ** 31) or Shift_Left (Word32 (abs Y), 16))
110 with
111 Pre => Y > Int32'First;
112 function CUR_POS_X (X : Int32) return Word32 is
113 ((if X >= 0 then 0 else 1 * 2 ** 15) or Word32 (abs X))
114 with
115 Pre => X > Int32'First;
116
Nico Huber3675db52016-11-04 16:27:29 +0100117 subtype VGA_Cycle_Count is Pos32 range 2 .. 128;
118 function VGA_CONTROL_VSYNC_BLINK_RATE
119 (Cycles : VGA_Cycle_Count)
120 return Word32
121 is
122 begin
123 return Word32 (Cycles) / 2 - 1;
124 end VGA_CONTROL_VSYNC_BLINK_RATE;
125
Nico Huber7ad2d652016-12-07 15:19:32 +0100126 PF_CTRL_ENABLE : constant := 1 * 2 ** 31;
127 PF_CTRL_PIPE_SELECT_MASK : constant := 3 * 2 ** 29;
128 PF_CTRL_FILTER_MED : constant := 1 * 2 ** 23;
Nico Huber83693c82016-10-08 22:17:55 +0200129
Nico Huber7ad2d652016-12-07 15:19:32 +0100130 PS_CTRL_ENABLE_SCALER : constant := 1 * 2 ** 31;
131 PS_CTRL_SCALER_MODE_7X5_EXTENDED : constant := 1 * 2 ** 28;
132 PS_CTRL_FILTER_SELECT_MEDIUM_2 : constant := 1 * 2 ** 23;
Nico Huber83693c82016-10-08 22:17:55 +0200133
Arthur Heymansd5198442018-03-28 17:05:12 +0200134 GMCH_PFIT_CONTROL_SELECT_MASK : constant := 3 * 2 ** 29;
135 GMCH_PFIT_CONTROL_SELECT_PIPE_A : constant := 0 * 2 ** 29;
136 GMCH_PFIT_CONTROL_SELECT_PIPE_B : constant := 1 * 2 ** 29;
Nico Huber958c5642018-06-02 16:59:31 +0200137 GMCH_PFIT_CONTROL_SCALING_MASK : constant := 3 * 2 ** 26;
138 GMCH_PFIT_CONTROL_SCALING : constant array (Scaling_Aspect) of Word32 :=
139 (Uniform => 0 * 2 ** 26,
140 Pillarbox => 2 * 2 ** 26,
141 Letterbox => 3 * 2 ** 26);
Arthur Heymansd5198442018-03-28 17:05:12 +0200142
Arthur Heymansdfcdd772018-03-28 16:42:50 +0200143 VGACNTRL_REG : constant Registers.Registers_Index :=
144 (if Config.Has_GMCH_VGACNTRL then
145 Registers.GMCH_VGACNTRL
146 else Registers.CPU_VGACNTRL);
147
Nico Huber83693c82016-10-08 22:17:55 +0200148 ---------------------------------------------------------------------------
149
Nico Huber83693c82016-10-08 22:17:55 +0200150 function PLANE_WM_LINES (Lines : Natural) return Word32 is
151 begin
152 return Shift_Left (Word32 (Lines), PLANE_WM_LINES_SHIFT)
153 and PLANE_WM_LINES_MASK;
154 end PLANE_WM_LINES;
155
156 function PLANE_WM_BLOCKS (Blocks : Natural) return Word32 is
157 begin
158 return Word32 (Blocks) and PLANE_WM_BLOCKS_MASK;
159 end PLANE_WM_BLOCKS;
160
161 ---------------------------------------------------------------------------
162
Nico Hubera7e1cd32026-07-03 09:23:24 +0000163 subtype Source_Size is Width_Type;
Nico Huberfb6dbad2026-04-10 16:23:39 +0000164
Nico Hubera7e1cd32026-07-03 09:23:24 +0000165 function Encode_Size (LSW, MSW : Source_Size) return Word32
Nico Huberfb6dbad2026-04-10 16:23:39 +0000166 is
Nico Hubera7e1cd32026-07-03 09:23:24 +0000167 (Shift_Left (Word32 (MSW) - 1, 16) or (Word32 (LSW) - 1));
168
169 function Source_Width (FB : Framebuffer_Type) return Source_Size
170 is
171 (if Config.Needs_Even_Source_Width and then Rotated_Width (FB) > 2 then
172 Rotated_Width (FB) - Rotated_Width (FB) mod 2
Nico Huberfb6dbad2026-04-10 16:23:39 +0000173 else
Nico Hubera7e1cd32026-07-03 09:23:24 +0000174 Rotated_Width (FB));
Nico Huberfb6dbad2026-04-10 16:23:39 +0000175
Nico Hubera7e1cd32026-07-03 09:23:24 +0000176 function Source_Height (FB : Framebuffer_Type) return Source_Size
Nico Huberfb6dbad2026-04-10 16:23:39 +0000177 is
Nico Hubera7e1cd32026-07-03 09:23:24 +0000178 (Rotated_Height (FB));
Nico Huber83693c82016-10-08 22:17:55 +0200179
180 ----------------------------------------------------------------------------
181
Nico Huber83693c82016-10-08 22:17:55 +0200182 procedure Clear_Watermarks (Controller : Controller_Type) is
183 begin
Nico Huber4dc4c612018-01-10 15:55:09 +0100184 Registers.Write (Controller.CUR_BUF_CFG, 16#0000_0000#);
185 for Level in WM_Levels loop
186 Registers.Write (Controller.CUR_WM (Level), 16#0000_0000#);
Nico Huber83693c82016-10-08 22:17:55 +0200187 end loop;
Nico Huber4dc4c612018-01-10 15:55:09 +0100188 Registers.Write (Controller.PLANE_BUF_CFG, 16#0000_0000#);
189 for Level in WM_Levels loop
190 Registers.Write (Controller.PLANE_WM (Level), 16#0000_0000#);
191 end loop;
192 Registers.Write (Controller.WM_LINETIME, 16#0000_0000#);
Nico Huber83693c82016-10-08 22:17:55 +0200193 end Clear_Watermarks;
194
195 procedure Setup_Watermarks (Controller : Controller_Type)
196 is
Nico Huberf3e23662016-12-05 21:33:03 +0100197 type Per_Plane_Buffer_Range is array (Pipe_Index) of Word32;
Nico Huber4dc4c612018-01-10 15:55:09 +0100198 Cur_Buffer_Range : constant Per_Plane_Buffer_Range :=
199 (Primary => Shift_Left ( 7, 16) or 0,
200 Secondary => Shift_Left (167, 16) or 160,
201 Tertiary => Shift_Left (327, 16) or 320);
202 Plane_Buffer_Range : constant Per_Plane_Buffer_Range :=
203 (Primary => Shift_Left (159, 16) or 8,
204 Secondary => Shift_Left (319, 16) or 168,
205 Tertiary => Shift_Left (479, 16) or 328);
Nico Huber83693c82016-10-08 22:17:55 +0200206 begin
207 Registers.Write
208 (Register => Controller.PLANE_BUF_CFG,
Nico Huber4dc4c612018-01-10 15:55:09 +0100209 Value => Plane_Buffer_Range (Controller.Pipe));
Nico Huber83693c82016-10-08 22:17:55 +0200210 Registers.Write
211 (Register => Controller.PLANE_WM (0),
212 Value => PLANE_WM_ENABLE or
213 PLANE_WM_LINES (2) or
Nico Huber4dc4c612018-01-10 15:55:09 +0100214 PLANE_WM_BLOCKS (152));
215 Registers.Write
216 (Register => Controller.CUR_BUF_CFG,
217 Value => Cur_Buffer_Range (Controller.Pipe));
218 Registers.Write
219 (Register => Controller.CUR_WM (0),
220 Value => PLANE_WM_ENABLE or
221 PLANE_WM_LINES (2) or
222 PLANE_WM_BLOCKS (8));
Nico Huber83693c82016-10-08 22:17:55 +0200223 end Setup_Watermarks;
224
225 ----------------------------------------------------------------------------
226
Nico Huber3675db52016-11-04 16:27:29 +0100227 procedure Setup_Hires_Plane
Nico Huber6a4dfc82016-11-04 15:50:58 +0100228 (Controller : Controller_Type;
Nico Huber0164b022017-08-24 15:12:51 +0200229 FB : HW.GFX.Framebuffer_Type)
Nico Huber83693c82016-10-08 22:17:55 +0200230 with
Nico Huber5ef4d602017-12-13 13:56:47 +0100231 Pre => FB.Height + FB.Start_Y <= FB.V_Stride
Nico Huber83693c82016-10-08 22:17:55 +0200232 is
Nico Hubera7e1cd32026-07-03 09:23:24 +0000233 Width : constant Source_Size := Source_Width (FB);
234 Height : constant Source_Size := Source_Height (FB);
Nico Huberfb6dbad2026-04-10 16:23:39 +0000235
Nico Huber83693c82016-10-08 22:17:55 +0200236 -- FIXME: setup correct format, based on framebuffer RGB format
237 Format : constant Word32 := 6 * 2 ** 26;
Arthur Heymans960e2392026-03-03 19:45:24 +0100238 PRI : Word32 := Format;
Nico Huber83693c82016-10-08 22:17:55 +0200239 begin
240 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
241
Nico Huber83693c82016-10-08 22:17:55 +0200242 if Config.Has_Plane_Control then
Nico Huber9b479412017-08-27 11:55:56 +0200243 declare
Nico Huber34be6542017-12-13 09:26:24 +0100244 Stride, Offset : Word32;
Tim Wawrzynczak0da761a2022-09-09 10:42:36 -0600245
246 function PLANE_CTL_ARB_SLOTS (N : Word32) return Word32 is
247 (if Config.Need_Pipe_Arb_Slots then Shift_Left (N, 28) else 0);
248
249 -- TODO: Hard coded format and arbitration slots for now,
250 -- for 4B-per-pixel XRGB, just like `Format` above.
251 -- ARB_SLOTS(1) matches the 4B per pixel.
252 Plane_Ctl : constant Word32 :=
253 PLANE_CTL_PLANE_ENABLE or
254 PLANE_CTL_TILED_SURFACE (FB.Tiling) or
255 PLANE_CTL_PLANE_ROTATION (FB.Rotation) or
256 PLANE_CTL_SRC_PIX_FMT_RGB_32B_8888 or
257 PLANE_CTL_ARB_SLOTS (1) or
258 (if not Config.Has_Plane_Color_Control
259 then PLANE_CTL_PLANE_GAMMA_DISABLE
260 else 0);
Nico Huber9b479412017-08-27 11:55:56 +0200261 begin
262 if Rotation_90 (FB) then
Nico Huber5ef4d602017-12-13 13:56:47 +0100263 Stride := Word32 (FB_Pitch (FB.V_Stride, FB));
264 Offset := Shift_Left (Word32 (FB.Start_X), 16) or
265 Word32 (FB.V_Stride - FB.Height - FB.Start_Y);
Nico Huber9b479412017-08-27 11:55:56 +0200266 else
Nico Huber5ef4d602017-12-13 13:56:47 +0100267 Stride := Word32 (FB_Pitch (FB.Stride, FB));
268 Offset := Shift_Left (Word32 (FB.Start_Y), 16) or
269 Word32 (FB.Start_X);
Nico Huber9b479412017-08-27 11:55:56 +0200270 end if;
Tim Wawrzynczak0da761a2022-09-09 10:42:36 -0600271
272 if Config.Has_Plane_Color_Control then
273 Registers.Write
274 (Register => Controller.PLANE_COLOR_CTL,
275 Value => PLANE_COLOR_CTL_PLANE_GAMMA_DISABLE);
276 end if;
277 Registers.Write (Controller.PLANE_AUX_DIST, 0);
278 Registers.Write (Controller.PLANE_CTL, Plane_Ctl);
Nico Huber9b479412017-08-27 11:55:56 +0200279 Registers.Write (Controller.PLANE_OFFSET, Offset);
Nico Huberfb6dbad2026-04-10 16:23:39 +0000280 Registers.Write (Controller.PLANE_SIZE, Encode_Size (Width, Height));
Nico Huber9b479412017-08-27 11:55:56 +0200281 Registers.Write (Controller.PLANE_STRIDE, Stride);
282 Registers.Write (Controller.PLANE_POS, 16#0000_0000#);
Nico Huber34be6542017-12-13 09:26:24 +0100283 Registers.Write (Controller.PLANE_SURF, FB.Offset and 16#ffff_f000#);
Nico Huber9b479412017-08-27 11:55:56 +0200284 end;
Nico Huber83693c82016-10-08 22:17:55 +0200285 else
Arthur Heymans960e2392026-03-03 19:45:24 +0100286 if Config.Has_DSPCNTR_Pipe_Select then
287 PRI := PRI or DSPCNTR_PIPE_SEL (Controller.Pipe);
288 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200289 if Config.Disable_Trickle_Feed then
290 PRI := PRI or DSPCNTR_DISABLE_TRICKLE_FEED;
291 end if;
Arthur Heymans960e2392026-03-03 19:45:24 +0100292
293 -- Write DSPCNTR *without* the enable bit first. On pre-SKL
294 -- hardware the control register self-arms when the plane
295 -- transitions from disabled to enabled, latching whatever
296 -- stride/size/offset values happen to be in the registers at
297 -- that moment. Programming format, pipe-select, and trickle-
298 -- feed now avoids a glitch with stale geometry values.
Nico Huber83693c82016-10-08 22:17:55 +0200299 Registers.Unset_And_Set_Mask
300 (Register => Controller.DSPCNTR,
301 Mask_Unset => DSPCNTR_MASK,
Nico Huberab69e362018-05-29 21:20:30 +0200302 Mask_Set => PRI or DSPCNTR_TILED_SURFACE (FB.Tiling));
Nico Huber83693c82016-10-08 22:17:55 +0200303
Nico Huber0164b022017-08-24 15:12:51 +0200304 Registers.Write
305 (Controller.DSPSTRIDE, Word32 (Pixel_To_Bytes (FB.Stride, FB)));
Arthur Heymans960e2392026-03-03 19:45:24 +0100306
307 -- Gen3 (i945): program DSPSIZE and DSPPOS before the surface
308 -- address write that arms the double-buffered plane registers.
309 if Config.Gen_I945 then
Nico Huberfb6dbad2026-04-10 16:23:39 +0000310 Registers.Write (Controller.DSPSIZE, Encode_Size (Width, Height));
Arthur Heymans960e2392026-03-03 19:45:24 +0100311 Registers.Write (Controller.DSPPOS, 16#0000_0000#);
312 end if;
313
Nico Huberab69e362018-05-29 21:20:30 +0200314 if Config.Has_DSP_Linoff and then FB.Tiling = Linear then
Arthur Heymans3f37cce2026-03-03 18:52:12 +0100315 pragma Assert_And_Cut
316 (FB.Start_Y * FB.Stride + FB.Start_X in Pixel_Type);
Nico Huberd49b56b2018-06-18 17:19:15 +0200317 declare
318 Linear_Offset : constant Pixel_Type :=
319 FB.Start_Y * FB.Stride + FB.Start_X;
320 begin
321 Registers.Write
322 (Register => Controller.DSPLINOFF,
Arthur Heymans960e2392026-03-03 19:45:24 +0100323 Value => (if Config.Has_DSPSURF
324 then Word32 (Pixel_To_Bytes (Linear_Offset, FB))
325 else (FB.Offset and 16#ffff_f000#) or
326 Word32 (Pixel_To_Bytes (Linear_Offset, FB))));
Nico Huberd49b56b2018-06-18 17:19:15 +0200327 Registers.Write (Controller.DSPTILEOFF, 0);
328 end;
Nico Huber5ef4d602017-12-13 13:56:47 +0100329 else
Nico Huberab69e362018-05-29 21:20:30 +0200330 if Config.Has_DSP_Linoff then
Arthur Heymans960e2392026-03-03 19:45:24 +0100331 Registers.Write (Controller.DSPLINOFF,
332 (if Config.Has_DSPSURF then 0
333 else FB.Offset and 16#ffff_f000#));
Nico Huberab69e362018-05-29 21:20:30 +0200334 end if;
Nico Huber5ef4d602017-12-13 13:56:47 +0100335 Registers.Write
336 (Register => Controller.DSPTILEOFF,
337 Value => Shift_Left (Word32 (FB.Start_Y), 16) or
338 Word32 (FB.Start_X));
Nico Huber83693c82016-10-08 22:17:55 +0200339 end if;
Arthur Heymans960e2392026-03-03 19:45:24 +0100340 if Config.Has_DSPSURF then
341 Registers.Write (Controller.DSPSURF, FB.Offset and 16#ffff_f000#);
342 end if;
343
344 -- Now enable the plane. All geometry registers are in place,
345 -- so the self-arm latches correct values.
346 Registers.Write
347 (Register => Controller.DSPCNTR,
348 Value => DSPCNTR_ENABLE or PRI or
349 DSPCNTR_TILED_SURFACE (FB.Tiling));
Nico Huber83693c82016-10-08 22:17:55 +0200350 end if;
Nico Huber3675db52016-11-04 16:27:29 +0100351 end Setup_Hires_Plane;
352
353 procedure Setup_Display
Nico Huber113a14b2016-12-06 21:59:15 +0100354 (Controller : Controller_Type;
355 Framebuffer : Framebuffer_Type;
356 Dither_BPC : BPC_Type;
357 Dither : Boolean)
Nico Huber3675db52016-11-04 16:27:29 +0100358 with
Nico Huber9b479412017-08-27 11:55:56 +0200359 Pre =>
360 Framebuffer.Offset = VGA_PLANE_FRAMEBUFFER_OFFSET or
Nico Huber5ef4d602017-12-13 13:56:47 +0100361 Framebuffer.Height + Framebuffer.Start_Y <= Framebuffer.V_Stride
Nico Huber3675db52016-11-04 16:27:29 +0100362 is
363 use type Word8;
364
365 Reg8 : Word8;
Tim Wawrzynczak0da761a2022-09-09 10:42:36 -0600366
367 type BW_Credit is new Natural range 0 .. 3;
368 function MBUS_DBOX_BW_CREDIT (C : BW_Credit) return Word32 is
369 (Shift_Left (Word32 (C), 14));
370
371 type B_Credit is new Natural range 0 .. 31;
372 function MBUS_DBOX_B_CREDIT (C : B_Credit) return Word32 is
373 (Shift_Left (Word32 (C), 8));
374
375 type A_Credit is new Natural range 0 .. 15;
376 function MBUS_DBOX_A_CREDIT (C : A_Credit) return Word32 is
377 (Word32 (C));
378
379 type B2B_Trans_Max is new Natural range 0 .. 31;
380 function MBUS_DBOX_B2B_TRANSACTIONS_MAX (B : B2B_Trans_Max) return Word32 is
381 (Shift_Left (Word32 (B), 20));
382
383 type B2B_Trans_Delay is new Natural range 0 .. 7;
384 function MBUS_DBOX_B2B_TRANSACTIONS_DELAY (B : B2B_Trans_Delay) return Word32 is
385 (Shift_Left (Word32 (B), 17));
386 MBUS_DBOX_REGULATE_B2B_TRANSACTIONS_EN : constant := 1 * 2 ** 16;
387
388 procedure Program_Mbus_Dbox_Credits is
389 Tmp : Word32;
390 begin
391 Tmp := MBUS_DBOX_B2B_TRANSACTIONS_MAX (16) or
392 MBUS_DBOX_B2B_TRANSACTIONS_DELAY (1) or
393 MBUS_DBOX_REGULATE_B2B_TRANSACTIONS_EN;
394
395 if Config.Has_New_Mbus_Dbox_Credits then
396 Tmp := Tmp or MBUS_DBOX_BW_CREDIT (2) or
397 MBUS_DBOX_B_CREDIT (8) or
398 MBUS_DBOX_A_CREDIT (4); -- No joined MBus support,
399 -- hence always use 4 for now.
400 else
401 Tmp := Tmp or MBUS_DBOX_BW_CREDIT (2) or
402 MBUS_DBOX_B_CREDIT (12) or
403 MBUS_DBOX_A_CREDIT (2);
404 end if;
405
406 Registers.Write
407 (Register => Controller.MBUS_DBOX_CTL,
408 Value => Tmp);
409 end Program_Mbus_Dbox_Credits;
410
411 -- Display WA # 1605353570: icl
412 -- Set the pixel rounding bit to 1 for allowing
413 -- passthrough of Frame buffer pixels unmodified
414 -- across pipe
415 PIXEL_ROUNDING_TRUNC_FB_PASSTHRU : constant := 1 * 2 ** 15;
416
417 -- Display WA #1153: icl
418 -- enable hardware to bypass the alpha math
419 -- and rounding for per-pixel values 00 and 0xff
420 PER_PIXEL_ALPHA_BYPASS_EN : constant := 1 * 2 ** 7;
421
422 -- ADL_P requires that we disable underrun recovery when
423 -- downscaling (or using the scaler for YUV420 pipe output),
424 -- using DSC, or using PSR2.
425 -- i915 always disables underrun recovery for gen 13+.
426 UNDERRUN_RECOVERY_DISABLE : constant := 1 * 2 ** 30;
Nico Huberfb6dbad2026-04-10 16:23:39 +0000427
Nico Hubera7e1cd32026-07-03 09:23:24 +0000428 Width : constant Source_Size := Source_Width (Framebuffer);
429 Height : constant Source_Size := Source_Height (Framebuffer);
Nico Huber3675db52016-11-04 16:27:29 +0100430 begin
431 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
432
Tim Wawrzynczak0da761a2022-09-09 10:42:36 -0600433 if Config.Has_Type_C_Ports then
434 Registers.Set_Mask
435 (Register => Controller.PIPE_CHICKEN,
436 Mask => PER_PIXEL_ALPHA_BYPASS_EN or
437 PIXEL_ROUNDING_TRUNC_FB_PASSTHRU or
438 (if Config.Need_Underrun_Rec_Disable
439 then UNDERRUN_RECOVERY_DISABLE
440 else 0));
441 end if;
442
Nico Huber3675db52016-11-04 16:27:29 +0100443 if Config.Has_Plane_Control then
444 Setup_Watermarks (Controller);
445 end if;
446
Tim Wawrzynczak0da761a2022-09-09 10:42:36 -0600447 if Config.Has_Mbus_Dbox_Credits then
448 Program_Mbus_Dbox_Credits;
449 end if;
450
Nico Huber3675db52016-11-04 16:27:29 +0100451 if Framebuffer.Offset = VGA_PLANE_FRAMEBUFFER_OFFSET then
Nico Huberfbb42202016-11-07 15:08:26 +0100452 if Config.VGA_Plane_Workaround then
453 Registers.Unset_And_Set_Mask
454 (Register => Registers.ILK_DISPLAY_CHICKEN1,
455 Mask_Unset => ILK_DISPLAY_CHICKEN1_VGA_MASK,
456 Mask_Set => ILK_DISPLAY_CHICKEN1_VGA_ENABLE);
457 Registers.Unset_And_Set_Mask
458 (Register => Registers.ILK_DISPLAY_CHICKEN2,
459 Mask_Unset => ILK_DISPLAY_CHICKEN2_VGA_MASK,
460 Mask_Set => ILK_DISPLAY_CHICKEN2_VGA_ENABLE);
461 end if;
462
Nico Huber3675db52016-11-04 16:27:29 +0100463 Registers.Unset_And_Set_Mask
Arthur Heymansdfcdd772018-03-28 16:42:50 +0200464 (Register => VGACNTRL_REG,
Nico Huber3675db52016-11-04 16:27:29 +0100465 Mask_Unset => VGA_CONTROL_VGA_DISPLAY_DISABLE or
466 VGA_CONTROL_BLINK_DUTY_CYCLE_MASK or
467 VGA_CONTROL_VSYNC_BLINK_RATE_MASK,
468 Mask_Set => VGA_CONTROL_BLINK_DUTY_CYCLE_50 or
469 VGA_CONTROL_VSYNC_BLINK_RATE (30));
470
471 Port_IO.OutB (VGA_SR_INDEX, VGA_SR01);
472 Port_IO.InB (Reg8, VGA_SR_DATA);
473 Port_IO.OutB (VGA_SR_DATA, Reg8 and not (VGA_SR01_SCREEN_OFF));
474 else
Nico Huber6a4dfc82016-11-04 15:50:58 +0100475 Setup_Hires_Plane (Controller, Framebuffer);
Nico Huber3675db52016-11-04 16:27:29 +0100476 end if;
477
478 Registers.Write
479 (Register => Controller.PIPESRC,
Nico Huberfb6dbad2026-04-10 16:23:39 +0000480 Value => Encode_Size (Height, Width));
Nico Huber83693c82016-10-08 22:17:55 +0200481
Nico Huber113a14b2016-12-06 21:59:15 +0100482 if Config.Has_Pipeconf_Misc then
483 Registers.Write
484 (Register => Controller.PIPEMISC,
Tim Wawrzynczak0da761a2022-09-09 10:42:36 -0600485 Value => Transcoder.BPC_Conf (Dither_BPC, Dither) or
486 -- FIXME: Should we set these at all?
487 (if Config.Has_Plane_Color_Control then
488 (PIPEMISC_PIXEL_ROUNDING_TRUNC or PIPEMISC_HDR_MODE_PRECISION) else 0));
Nico Huber113a14b2016-12-06 21:59:15 +0100489 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200490 end Setup_Display;
491
492 ----------------------------------------------------------------------------
493
Nico Huber4dc4c612018-01-10 15:55:09 +0100494 procedure Update_Cursor
495 (Pipe : Pipe_Index;
496 FB : Framebuffer_Type;
497 Cursor : Cursor_Type)
498 is
499 begin
500 -- on some platforms writing CUR_CTL disables self-arming of CUR_POS
501 -- so keep it first
502 Registers.Write
Nico Huber75a707f2018-06-18 16:28:33 +0200503 (Register => Cursors (Pipe).CTL,
Tim Wawrzynczak0da761a2022-09-09 10:42:36 -0600504 Value => CUR_CTL_MODE (Cursor.Mode, Cursor.Size) or
505 (if Config.Need_Pipe_Arb_Slots
506 then MCURSOR_ARB_SLOTS (1)
507 else CUR_CTL_PIPE_SELECT (Pipe)));
Nico Huber4dc4c612018-01-10 15:55:09 +0100508 Place_Cursor (Pipe, FB, Cursor);
509 end Update_Cursor;
510
511 procedure Place_Cursor
512 (Pipe : Pipe_Index;
513 FB : Framebuffer_Type;
514 Cursor : Cursor_Type)
515 is
516 Width : constant Width_Type := Cursor_Width (Cursor.Size);
517 X : Int32 := Cursor.Center_X - Width / 2;
518 Y : Int32 := Cursor.Center_Y - Width / 2;
519 begin
520 -- off-screen cursor needs special care
521 if X <= -Width or Y <= -Width or
Nico Hubera7e1cd32026-07-03 09:23:24 +0000522 X >= Source_Width (FB) or Y >= Source_Height (FB) or
Nico Huber4dc4c612018-01-10 15:55:09 +0100523 X > Config.Maximum_Cursor_X or Y > Config.Maximum_Cursor_Y
524 then
525 X := -Width;
526 Y := -Width;
527 end if;
528 Registers.Write
Nico Huber75a707f2018-06-18 16:28:33 +0200529 (Register => Cursors (Pipe).POS,
Nico Huberaae8a352020-06-15 13:09:29 +0200530 Value => CUR_POS_Y (Y) or CUR_POS_X (X),
531 Verbose => False);
Nico Huber4dc4c612018-01-10 15:55:09 +0100532 -- write to CUR_BASE always arms other CUR_* registers
533 Registers.Write
Nico Huber75a707f2018-06-18 16:28:33 +0200534 (Register => Cursors (Pipe).BASE,
Nico Huberaae8a352020-06-15 13:09:29 +0200535 Value => Shift_Left (Word32 (Cursor.GTT_Offset), 12),
536 Verbose => False);
Nico Huber4dc4c612018-01-10 15:55:09 +0100537 end Place_Cursor;
538
539 ----------------------------------------------------------------------------
540
Nico Huber4916e342016-11-04 14:37:53 +0100541 procedure Scale_Keep_Aspect
Nico Huberc5c767a2018-06-03 01:09:04 +0200542 (Width : out Width_Type;
543 Height : out Height_Type;
544 Max_Width : in Width_Type;
545 Max_Height : in Height_Type;
Nico Huber4916e342016-11-04 14:37:53 +0100546 Framebuffer : in Framebuffer_Type)
547 with
548 Pre =>
Nico Hubera7e1cd32026-07-03 09:23:24 +0000549 Source_Width (Framebuffer) <= Max_Width and
550 Source_Height (Framebuffer) <= Max_Height,
Nico Huber4916e342016-11-04 14:37:53 +0100551 Post =>
552 Width <= Max_Width and Height <= Max_Height
553 is
Nico Hubera7e1cd32026-07-03 09:23:24 +0000554 Src_Width : constant Width_Type := Source_Width (Framebuffer);
555 Src_Height : constant Height_Type := Source_Height (Framebuffer);
Nico Huber4916e342016-11-04 14:37:53 +0100556 begin
Nico Huberda1185e2018-06-03 01:07:46 +0200557 case Scaling_Type (Src_Width, Src_Height, Max_Width, Max_Height) is
558 when Letterbox =>
Nico Huber99200fe2026-04-14 16:37:45 +0200559 Height := (Src_Height * Max_Width) / Src_Width;
560 pragma Assert (Height <= Max_Height);
Nico Huberda1185e2018-06-03 01:07:46 +0200561 Width := Max_Width;
Nico Huberda1185e2018-06-03 01:07:46 +0200562 when Pillarbox =>
Nico Huber99200fe2026-04-14 16:37:45 +0200563 Width := (Src_Width * Max_Height) / Src_Height;
564 pragma Assert (Max_Height * Src_Width < Max_Width * Src_Height);
565 pragma Assert ((Max_Height * Src_Width) / Src_Height < Max_Width);
566 pragma Assert (Width <= Max_Width);
Nico Huberda1185e2018-06-03 01:07:46 +0200567 Height := Max_Height;
568 when Uniform =>
569 Width := Max_Width;
570 Height := Max_Height;
571 end case;
Nico Huber4916e342016-11-04 14:37:53 +0100572 end Scale_Keep_Aspect;
573
Nico Huberba84df22026-06-23 13:29:29 +0000574 procedure Scale
575 (Width : out Width_Type;
576 Height : out Height_Type;
577 Scaling : in GMA.Scaling;
578 Max_Width : in Width_Type;
579 Max_Height : in Height_Type;
580 Framebuffer : in Framebuffer_Type)
581 with
582 Pre =>
Nico Hubera7e1cd32026-07-03 09:23:24 +0000583 Source_Width (Framebuffer) <= Max_Width and
584 Source_Height (Framebuffer) <= Max_Height,
Nico Huberba84df22026-06-23 13:29:29 +0000585 Post =>
586 Width <= Max_Width and Height <= Max_Height
587 is
588 begin
589 case Scaling is
590 when None =>
Nico Hubera7e1cd32026-07-03 09:23:24 +0000591 Width := Source_Width (Framebuffer);
592 Height := Source_Height (Framebuffer);
Nico Huberba84df22026-06-23 13:29:29 +0000593 when Fit =>
594 Scale_Keep_Aspect (Width, Height, Max_Width, Max_Height, Framebuffer);
595 when Stretch =>
596 Width := Max_Width;
597 Height := Max_Height;
598 end case;
599 end Scale;
600
601 procedure Align_Framebuffer
602 (Pos_X : out Position_Type;
603 Pos_Y : out Position_Type;
604 Align : in Alignment;
605 Width : in Width_Type;
606 Height : in Height_Type;
607 Mode : in Mode_Type)
608 with
609 Pre => Width <= Mode.H_Visible and Height <= Mode.V_Visible
610 is
611 Gap_X : constant Position_Type := Mode.H_Visible - Width;
612 Gap_Y : constant Position_Type := Mode.V_Visible - Height;
613 begin
614 case Align is
615 when Top_Left => Pos_X := 0; Pos_Y := 0;
616 when Top => Pos_X := Gap_X / 2; Pos_Y := 0;
617 when Top_Right => Pos_X := Gap_X; Pos_Y := 0;
618 when Left => Pos_X := 0; Pos_Y := Gap_Y / 2;
619 when Center => Pos_X := Gap_X / 2; Pos_Y := Gap_Y / 2;
620 when Right => Pos_X := Gap_X; Pos_Y := Gap_Y / 2;
621 when Bottom_Left => Pos_X := 0; Pos_Y := Gap_Y;
622 when Bottom => Pos_X := Gap_X / 2; Pos_Y := Gap_Y;
623 when Bottom_Right => Pos_X := Gap_X; Pos_Y := Gap_Y;
624 end case;
625 end Align_Framebuffer;
626
Nico Huberfb6dbad2026-04-10 16:23:39 +0000627 type Pipe_Scaler_Limit_Config is record
628 Control : Word32;
629 Horizontal : Pos32;
630 Vertical : Pos32;
631 end record;
632
633 function Skylake_Scaler_Limits
634 (Controller : Controller_Type;
635 Width : Width_Type;
636 Height : Height_Type)
637 return Pipe_Scaler_Limit_Config
638 with
639 Post => Skylake_Scaler_Limits'Result.Horizontal >= Width
640 and Skylake_Scaler_Limits'Result.Vertical >= Height
641 is
642 use type Registers.Registers_Invalid_Index;
643
644 -- Enable 7x5 extended mode where possible:
645 Scaler_Mode : constant Word32 :=
646 (if Controller.PS_CTRL_2 /= Registers.Invalid_Register then
647 PS_CTRL_SCALER_MODE_7X5_EXTENDED else 0);
648
649 -- We can scale up to 2.99x horizontally:
650 Horizontal_Limit : constant Pos32 := (Width * 299) / 100;
651 -- The third scaler is limited to 1.99x
652 -- vertical scaling for source widths > 2048:
653 Vertical_Limit : constant Pos32 :=
654 (Height *
655 (if Controller.PS_CTRL_2 = Registers.Invalid_Register and
656 Width > 2048
657 then
658 199
659 else
660 299)) / 100;
661 begin
662 return (Scaler_Mode, Horizontal_Limit, Vertical_Limit);
663 end Skylake_Scaler_Limits;
664
665 function Tigerlake_Scaler_Limits
666 (Width : Width_Type;
667 Height : Height_Type)
668 return Pipe_Scaler_Limit_Config
669 with
670 Post => Tigerlake_Scaler_Limits'Result.Horizontal >= Width
671 and Tigerlake_Scaler_Limits'Result.Vertical >= Height
672 is
673 Scaling : constant := 32_000; -- PRM says: Scaling * 2**15 >= 1.0
674 -- so virtually unlimited
675 begin
676 return (Control => 0, Horizontal => Width * Scaling, Vertical => Height * Scaling);
677 end Tigerlake_Scaler_Limits;
678
Nico Huber4916e342016-11-04 14:37:53 +0100679 procedure Setup_Skylake_Pipe_Scaler
680 (Controller : in Controller_Type;
Nico Huber4d592b42026-06-23 12:21:37 +0000681 Pipe_Cfg : in Pipe_Config;
682 Mode : in HW.GFX.Mode_Type)
Nico Huber4916e342016-11-04 14:37:53 +0100683 with
684 Pre =>
Nico Huber4d592b42026-06-23 12:21:37 +0000685 Rotated_Width (Pipe_Cfg.Framebuffer) <= Mode.H_Visible and
686 Rotated_Height (Pipe_Cfg.Framebuffer) <= Mode.V_Visible
Nico Huber4916e342016-11-04 14:37:53 +0100687 is
Nico Hubera7e1cd32026-07-03 09:23:24 +0000688 Width_In : constant Width_Type := Source_Width (Pipe_Cfg.Framebuffer);
689 Height_In : constant Height_Type := Source_Height (Pipe_Cfg.Framebuffer);
Nico Huberfb6dbad2026-04-10 16:23:39 +0000690 Limits : constant Pipe_Scaler_Limit_Config :=
691 (if Config.Has_Skylake_Scaler_Limits then
692 Skylake_Scaler_Limits (Controller, Width_In, Height_In)
693 else
694 Tigerlake_Scaler_Limits (Width_In, Height_In));
Nico Huber4916e342016-11-04 14:37:53 +0100695
Nico Huberc5c767a2018-06-03 01:09:04 +0200696 Width : Width_Type;
697 Height : Height_Type;
Nico Huberba84df22026-06-23 13:29:29 +0000698 Pos_X, Pos_Y : Position_Type;
Nico Huber4916e342016-11-04 14:37:53 +0100699 begin
700 -- Writes to WIN_SZ arm the PS registers.
701
Nico Huberba84df22026-06-23 13:29:29 +0000702 Scale
Nico Huber4916e342016-11-04 14:37:53 +0100703 (Width => Width,
704 Height => Height,
Nico Huberba84df22026-06-23 13:29:29 +0000705 Scaling => Pipe_Cfg.Scaling,
Nico Huberfb6dbad2026-04-10 16:23:39 +0000706 Max_Width => Pos32'Min (Limits.Horizontal, Mode.H_Visible),
707 Max_Height => Pos32'Min (Limits.Vertical, Mode.V_Visible),
Nico Huber4d592b42026-06-23 12:21:37 +0000708 Framebuffer => Pipe_Cfg.Framebuffer);
Nico Huber4916e342016-11-04 14:37:53 +0100709
Nico Huberba84df22026-06-23 13:29:29 +0000710 Align_Framebuffer
711 (Pos_X => Pos_X,
712 Pos_Y => Pos_Y,
713 Align => Pipe_Cfg.Alignment,
714 Width => Width,
715 Height => Height,
716 Mode => Mode);
717
Nico Huber4916e342016-11-04 14:37:53 +0100718 Registers.Write
719 (Register => Controller.PS_CTRL_1,
Nico Huberfb6dbad2026-04-10 16:23:39 +0000720 Value => PS_CTRL_ENABLE_SCALER or Limits.Control);
Nico Huber4916e342016-11-04 14:37:53 +0100721 Registers.Write
722 (Register => Controller.PS_WIN_POS_1,
Nico Huberba84df22026-06-23 13:29:29 +0000723 Value => Shift_Left (Word32 (Pos_X), 16) or Word32 (Pos_Y));
Nico Huber4916e342016-11-04 14:37:53 +0100724 Registers.Write
725 (Register => Controller.PS_WIN_SZ_1,
726 Value => Shift_Left (Word32 (Width), 16) or Word32 (Height));
727 end Setup_Skylake_Pipe_Scaler;
728
729 procedure Setup_Ironlake_Panel_Fitter
730 (Controller : in Controller_Type;
Nico Huber4d592b42026-06-23 12:21:37 +0000731 Pipe_Cfg : in Pipe_Config;
732 Mode : in HW.GFX.Mode_Type)
Nico Huber4916e342016-11-04 14:37:53 +0100733 with
734 Pre =>
Nico Huber4d592b42026-06-23 12:21:37 +0000735 Rotated_Width (Pipe_Cfg.Framebuffer) <= Mode.H_Visible and
736 Rotated_Height (Pipe_Cfg.Framebuffer) <= Mode.V_Visible
Nico Huber4916e342016-11-04 14:37:53 +0100737 is
738 -- Force 1:1 mapping of panel fitter:pipe
739 PF_Ctrl_Pipe_Sel : constant Word32 :=
740 (if Config.Has_PF_Pipe_Select then
741 (case Controller.PF_CTRL is
742 when Registers.PFA_CTL_1 => 0 * 2 ** 29,
743 when Registers.PFB_CTL_1 => 1 * 2 ** 29,
744 when Registers.PFC_CTL_1 => 2 * 2 ** 29,
745 when others => 0) else 0);
746
Nico Huberc5c767a2018-06-03 01:09:04 +0200747 Width : Width_Type;
748 Height : Height_Type;
Nico Huberba84df22026-06-23 13:29:29 +0000749 Pos_X, Pos_Y : Position_Type;
Nico Huber4916e342016-11-04 14:37:53 +0100750 begin
751 -- Writes to WIN_SZ arm the PF registers.
752
Nico Huberba84df22026-06-23 13:29:29 +0000753 Scale
Nico Huber4916e342016-11-04 14:37:53 +0100754 (Width => Width,
755 Height => Height,
Nico Huberba84df22026-06-23 13:29:29 +0000756 Scaling => Pipe_Cfg.Scaling,
Nico Huberc5c767a2018-06-03 01:09:04 +0200757 Max_Width => Mode.H_Visible,
758 Max_Height => Mode.V_Visible,
Nico Huber4d592b42026-06-23 12:21:37 +0000759 Framebuffer => Pipe_Cfg.Framebuffer);
Nico Huber4916e342016-11-04 14:37:53 +0100760
Nico Huberfdb0df12018-02-07 14:30:34 +0100761 -- Do not scale to odd width (at least Haswell has trouble with this).
Nico Huberc5c767a2018-06-03 01:09:04 +0200762 if Width < Mode.H_Visible and Width mod 2 = 1 then
Nico Huberfdb0df12018-02-07 14:30:34 +0100763 Width := Width + 1;
764 end if;
Nico Huberb3b9fa32018-06-18 16:16:41 +0200765 -- Do not scale to odd height (at least Sandy Bridge makes trouble).
766 if Height < Mode.V_Visible and Height mod 2 = 1 then
767 Height := Height + 1;
768 end if;
Nico Huberfdb0df12018-02-07 14:30:34 +0100769
Nico Huberba84df22026-06-23 13:29:29 +0000770 Align_Framebuffer
771 (Pos_X => Pos_X,
772 Pos_Y => Pos_Y,
773 Align => Pipe_Cfg.Alignment,
774 Width => Width,
775 Height => Height,
776 Mode => Mode);
Nico Huberfdb0df12018-02-07 14:30:34 +0100777
778 -- Hardware is picky about minimal horizontal gaps.
Nico Huberc5c767a2018-06-03 01:09:04 +0200779 if Mode.H_Visible - Width <= 3 then
780 Width := Mode.H_Visible;
Nico Huberba84df22026-06-23 13:29:29 +0000781 Pos_X := 0;
Nico Huberfdb0df12018-02-07 14:30:34 +0100782 end if;
783
Nico Huber4916e342016-11-04 14:37:53 +0100784 Registers.Write
785 (Register => Controller.PF_CTRL,
786 Value => PF_CTRL_ENABLE or PF_Ctrl_Pipe_Sel or PF_CTRL_FILTER_MED);
787 Registers.Write
788 (Register => Controller.PF_WIN_POS,
Nico Huberba84df22026-06-23 13:29:29 +0000789 Value => Shift_Left (Word32 (Pos_X), 16) or Word32 (Pos_Y));
Nico Huber4916e342016-11-04 14:37:53 +0100790 Registers.Write
791 (Register => Controller.PF_WIN_SZ,
792 Value => Shift_Left (Word32 (Width), 16) or Word32 (Height));
793 end Setup_Ironlake_Panel_Fitter;
794
Arthur Heymansd5198442018-03-28 17:05:12 +0200795 procedure Setup_Gmch_Panel_Fitter
Nico Huber958c5642018-06-02 16:59:31 +0200796 (Controller : in Controller_Type;
797 Mode : in HW.GFX.Mode_Type;
798 Framebuffer : in HW.GFX.Framebuffer_Type)
Arthur Heymansd5198442018-03-28 17:05:12 +0200799 is
800 PF_Ctrl_Pipe_Sel : constant Word32 :=
801 (case Controller.Pipe is
802 when Primary => GMCH_PFIT_CONTROL_SELECT_PIPE_A,
803 when Secondary => GMCH_PFIT_CONTROL_SELECT_PIPE_B,
804 when others => 0);
Nico Huber958c5642018-06-02 16:59:31 +0200805
Arthur Heymansf70edda2018-08-21 18:37:00 +0200806 -- Work around a quirk:
807 -- In legacy VGA mode Pillarbox fails to display anything so just force
808 -- 'auto' mode on all displays, which will the output stretched to
809 -- fullscreen .
Nico Huber958c5642018-06-02 16:59:31 +0200810 PF_Ctrl_Scaling : constant Word32 :=
Arthur Heymansf70edda2018-08-21 18:37:00 +0200811 (if Framebuffer.Offset = VGA_PLANE_FRAMEBUFFER_OFFSET then
812 GMCH_PFIT_CONTROL_SCALING (Uniform)
813 else
814 GMCH_PFIT_CONTROL_SCALING (Scaling_Type (Framebuffer, Mode)));
Nico Huber958c5642018-06-02 16:59:31 +0200815
Arthur Heymansd5198442018-03-28 17:05:12 +0200816 In_Use : Boolean;
817 begin
818 Registers.Is_Set_Mask
819 (Register => Registers.GMCH_PFIT_CONTROL,
820 Mask => PF_CTRL_ENABLE,
821 Result => In_Use);
822
823 if not In_Use then
824 Registers.Write
825 (Register => Registers.GMCH_PFIT_CONTROL,
Nico Huber958c5642018-06-02 16:59:31 +0200826 Value => PF_CTRL_ENABLE or PF_Ctrl_Pipe_Sel or PF_Ctrl_Scaling);
Arthur Heymansd5198442018-03-28 17:05:12 +0200827 else
Nico Huber7ba7bd62018-06-06 12:27:09 +0200828 pragma Debug (Debug.Put_Line
829 ("GMCH Pannel fitter already in use, skipping..."));
Arthur Heymansd5198442018-03-28 17:05:12 +0200830 end if;
831 end Setup_Gmch_Panel_Fitter;
832
Nico Huberf361ec82018-06-02 18:01:45 +0200833 procedure Gmch_Panel_Fitter_Pipe (Pipe : out Pipe_Index)
834 is
835 Used_For_Secondary : Boolean;
836 begin
Arthur Heymans960e2392026-03-03 19:45:24 +0100837 if Config.Gen_I945 then
838 -- Gen3: panel fitter is hardwired to Pipe B (Secondary).
839 -- The PFIT_PIPE field (bits 30:29) does not exist on Gen3.
840 Pipe := Secondary;
841 else
842 Registers.Is_Set_Mask
843 (Register => Registers.GMCH_PFIT_CONTROL,
844 Mask => GMCH_PFIT_CONTROL_SELECT_PIPE_B,
845 Result => Used_For_Secondary);
846 Pipe := (if Used_For_Secondary then Secondary else Primary);
847 end if;
Nico Huberac2a1412026-07-02 10:41:59 +0200848 end Gmch_Panel_Fitter_Pipe;
Nico Huberf361ec82018-06-02 18:01:45 +0200849
Nico Huberb4b72792018-01-02 13:45:41 +0100850 procedure Panel_Fitter_Off (Controller : Controller_Type)
851 is
852 use type HW.GFX.GMA.Registers.Registers_Invalid_Index;
Nico Huberf361ec82018-06-02 18:01:45 +0200853 Pipe_Using_PF : Pipe_Index;
Nico Huberb4b72792018-01-02 13:45:41 +0100854 begin
855 -- Writes to WIN_SZ arm the PS/PF registers.
856 if Config.Has_Plane_Control then
857 Registers.Unset_Mask (Controller.PS_CTRL_1, PS_CTRL_ENABLE_SCALER);
858 Registers.Write (Controller.PS_WIN_SZ_1, 16#0000_0000#);
859 if Controller.PS_CTRL_2 /= Registers.Invalid_Register and
860 Controller.PS_WIN_SZ_2 /= Registers.Invalid_Register
861 then
862 Registers.Unset_Mask (Controller.PS_CTRL_2, PS_CTRL_ENABLE_SCALER);
863 Registers.Write (Controller.PS_WIN_SZ_2, 16#0000_0000#);
864 end if;
Arthur Heymansd5198442018-03-28 17:05:12 +0200865 elsif Config.Has_GMCH_PFIT_CONTROL then
Nico Huberf361ec82018-06-02 18:01:45 +0200866 Gmch_Panel_Fitter_Pipe (Pipe_Using_PF);
867 if Pipe_Using_PF = Controller.Pipe then
Arthur Heymans960e2392026-03-03 19:45:24 +0100868 -- Write 0 to clear all bits (enable, scaling mode, auto-scale,
869 -- interpolation). Just clearing the enable bit can leave stale
870 -- Gen3 auto-scale bits that confuse the hardware.
871 Registers.Write (Registers.GMCH_PFIT_CONTROL, 16#0000_0000#);
Arthur Heymansd5198442018-03-28 17:05:12 +0200872 end if;
Nico Huberb4b72792018-01-02 13:45:41 +0100873 else
874 Registers.Unset_Mask (Controller.PF_CTRL, PF_CTRL_ENABLE);
875 Registers.Write (Controller.PF_WIN_SZ, 16#0000_0000#);
876 end if;
877 end Panel_Fitter_Off;
878
Nico Huber4916e342016-11-04 14:37:53 +0100879 procedure Setup_Scaling
880 (Controller : in Controller_Type;
Nico Huber4d592b42026-06-23 12:21:37 +0000881 Pipe_Cfg : in Pipe_Config;
882 Mode : in HW.GFX.Mode_Type)
Nico Huber4916e342016-11-04 14:37:53 +0100883 with
884 Pre =>
Nico Huber4d592b42026-06-23 12:21:37 +0000885 Rotated_Width (Pipe_Cfg.Framebuffer) <= Mode.H_Visible and
886 Rotated_Height (Pipe_Cfg.Framebuffer) <= Mode.V_Visible
Nico Huber4916e342016-11-04 14:37:53 +0100887 is
888 begin
Nico Huber4d592b42026-06-23 12:21:37 +0000889 if Requires_Scaling (Pipe_Cfg.Framebuffer, Mode) then
Nico Huber4916e342016-11-04 14:37:53 +0100890 if Config.Has_Plane_Control then
Nico Huber4d592b42026-06-23 12:21:37 +0000891 Setup_Skylake_Pipe_Scaler (Controller, Pipe_Cfg, Mode);
Arthur Heymansd5198442018-03-28 17:05:12 +0200892 elsif Config.Has_GMCH_PFIT_CONTROL then
Nico Huber4d592b42026-06-23 12:21:37 +0000893 Setup_Gmch_Panel_Fitter (Controller, Mode, Pipe_Cfg.Framebuffer);
Nico Huber4916e342016-11-04 14:37:53 +0100894 else
Nico Huber4d592b42026-06-23 12:21:37 +0000895 Setup_Ironlake_Panel_Fitter (Controller, Pipe_Cfg, Mode);
Nico Huber4916e342016-11-04 14:37:53 +0100896 end if;
Nico Huberb4b72792018-01-02 13:45:41 +0100897 else
898 Panel_Fitter_Off (Controller);
Nico Huber4916e342016-11-04 14:37:53 +0100899 end if;
900 end Setup_Scaling;
901
Nico Huber9a4c4c32019-09-16 22:05:11 +0200902 procedure Reserve_Scaler
903 (Success : out Boolean;
904 Reservation : in out Scaler_Reservation;
905 Pipe : in Pipe_Index)
Nico Huberf361ec82018-06-02 18:01:45 +0200906 is
907 Pipe_Using_PF : Pipe_Index := Pipe_Index'First;
908 PF_Enabled : Boolean;
909 begin
910 if Config.Has_GMCH_PFIT_CONTROL then
Nico Huber9a4c4c32019-09-16 22:05:11 +0200911 if Reservation.Reserved then
912 Success := Reservation.Pipe = Pipe;
913 return;
914 end if;
915
Nico Huberf361ec82018-06-02 18:01:45 +0200916 Registers.Is_Set_Mask
917 (Register => Registers.GMCH_PFIT_CONTROL,
918 Mask => PF_CTRL_ENABLE,
919 Result => PF_Enabled);
920 if PF_Enabled then
921 Gmch_Panel_Fitter_Pipe (Pipe_Using_PF);
922 end if;
923
Nico Huber9a4c4c32019-09-16 22:05:11 +0200924 Success := not PF_Enabled or Pipe_Using_PF = Pipe;
925 if Success then
926 Reservation.Reserved := True;
927 Reservation.Pipe := Pipe;
928 end if;
Nico Huberf361ec82018-06-02 18:01:45 +0200929 else
Nico Huber9a4c4c32019-09-16 22:05:11 +0200930 Success := True;
Nico Huberf361ec82018-06-02 18:01:45 +0200931 end if;
Nico Huber9a4c4c32019-09-16 22:05:11 +0200932 end Reserve_Scaler;
Nico Huberf361ec82018-06-02 18:01:45 +0200933
Nico Huber4916e342016-11-04 14:37:53 +0100934 ----------------------------------------------------------------------------
935
Nico Huberf7f537e2018-01-02 14:15:43 +0100936 procedure Setup_FB
937 (Pipe : Pipe_Index;
Nico Huber4d592b42026-06-23 12:21:37 +0000938 Pipe_Cfg : Pipe_Config;
939 Mode : Mode_Type)
Nico Huberf7f537e2018-01-02 14:15:43 +0100940 is
941 -- Enable dithering if framebuffer BPC differs from port BPC,
942 -- as smooth gradients look really bad without.
Nico Huber4d592b42026-06-23 12:21:37 +0000943 Dither : constant Boolean := Pipe_Cfg.Framebuffer.BPC /= Mode.BPC;
Nico Huberf7f537e2018-01-02 14:15:43 +0100944 begin
945 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
946
Nico Huber4dc4c612018-01-10 15:55:09 +0100947 -- Disable the cursor first.
Nico Huber4d592b42026-06-23 12:21:37 +0000948 Update_Cursor (Pipe, Pipe_Cfg.Framebuffer, Default_Cursor);
Nico Huber4dc4c612018-01-10 15:55:09 +0100949
Nico Huber4d592b42026-06-23 12:21:37 +0000950 Setup_Display (Controllers (Pipe), Pipe_Cfg.Framebuffer, Mode.BPC, Dither);
951 Setup_Scaling (Controllers (Pipe), Pipe_Cfg, Mode);
Nico Huberf7f537e2018-01-02 14:15:43 +0100952 end Setup_FB;
953
Nico Huber83693c82016-10-08 22:17:55 +0200954 procedure On
Nico Huberf3e23662016-12-05 21:33:03 +0100955 (Pipe : Pipe_Index;
Nico Huber4d592b42026-06-23 12:21:37 +0000956 Pipe_Cfg : Pipe_Config;
957 Port_Cfg : Port_Config)
Nico Huber83693c82016-10-08 22:17:55 +0200958 is
959 begin
960 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
961
Nico Huber7ad2d652016-12-07 15:19:32 +0100962 Transcoder.Setup (Pipe, Port_Cfg);
Nico Huber83693c82016-10-08 22:17:55 +0200963
Nico Huber4d592b42026-06-23 12:21:37 +0000964 Setup_FB (Pipe, Pipe_Cfg, Port_Cfg.Mode);
965 Update_Cursor (Pipe, Pipe_Cfg.Framebuffer, Pipe_Cfg.Cursor);
Nico Huber83693c82016-10-08 22:17:55 +0200966
Nico Huberabb16d92018-05-29 01:44:26 +0200967 Transcoder.On
968 (Pipe => Pipe,
969 Port_Cfg => Port_Cfg,
Nico Huber4d592b42026-06-23 12:21:37 +0000970 Dither => Pipe_Cfg.Framebuffer.BPC /= Port_Cfg.Mode.BPC,
971 Scale => Requires_Scaling (Pipe_Cfg.Framebuffer, Port_Cfg.Mode));
Nico Huber83693c82016-10-08 22:17:55 +0200972 end On;
973
974 ----------------------------------------------------------------------------
975
Nico Huber75a707f2018-06-18 16:28:33 +0200976 procedure Planes_Off (Controller : Controller_Type; CUR : Cursor_Regs)
977 is
978 use type Registers.Registers_Invalid_Index;
Nico Huber83693c82016-10-08 22:17:55 +0200979 begin
Nico Huber75a707f2018-06-18 16:28:33 +0200980 Registers.Write (CUR.CTL, 16#0000_0000#);
981 if CUR.FBC_CTL /= Registers.Invalid_Register then
982 Registers.Write (CUR.FBC_CTL, 16#0000_0000#);
Nico Huber4dc4c612018-01-10 15:55:09 +0100983 end if;
Nico Huber7ad2d652016-12-07 15:19:32 +0100984 Registers.Unset_Mask (Controller.SPCNTR, DSPCNTR_ENABLE);
Nico Huber83693c82016-10-08 22:17:55 +0200985 if Config.Has_Plane_Control then
986 Clear_Watermarks (Controller);
987 Registers.Unset_Mask (Controller.PLANE_CTL, PLANE_CTL_PLANE_ENABLE);
988 Registers.Write (Controller.PLANE_SURF, 16#0000_0000#);
989 else
990 Registers.Unset_Mask (Controller.DSPCNTR, DSPCNTR_ENABLE);
991 end if;
992 end Planes_Off;
993
Nico Huber7ad2d652016-12-07 15:19:32 +0100994 procedure Off (Pipe : Pipe_Index)
Nico Huberf3e23662016-12-05 21:33:03 +0100995 is
Nico Huber83693c82016-10-08 22:17:55 +0200996 begin
997 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
998
Nico Huber75a707f2018-06-18 16:28:33 +0200999 Planes_Off (Controllers (Pipe), Cursors (Pipe));
Nico Huber7ad2d652016-12-07 15:19:32 +01001000 Transcoder.Off (Pipe);
Nico Huberf3e23662016-12-05 21:33:03 +01001001 Panel_Fitter_Off (Controllers (Pipe));
Nico Huber7ad2d652016-12-07 15:19:32 +01001002 Transcoder.Clk_Off (Pipe);
Nico Huber83693c82016-10-08 22:17:55 +02001003 end Off;
1004
Nico Huber33912aa2016-12-06 20:36:23 +01001005 procedure Legacy_VGA_Off
1006 is
1007 use type HW.Word8;
1008 Reg8 : Word8;
1009 begin
1010 Port_IO.OutB (VGA_SR_INDEX, VGA_SR01);
1011 Port_IO.InB (Reg8, VGA_SR_DATA);
1012 Port_IO.OutB (VGA_SR_DATA, Reg8 or VGA_SR01_SCREEN_OFF);
1013 Time.U_Delay (100); -- PRM says 100us, Linux does 300
Arthur Heymansdfcdd772018-03-28 16:42:50 +02001014 Registers.Set_Mask (VGACNTRL_REG, VGA_CONTROL_VGA_DISPLAY_DISABLE);
Nico Huber33912aa2016-12-06 20:36:23 +01001015 end Legacy_VGA_Off;
1016
Nico Huber83693c82016-10-08 22:17:55 +02001017 procedure All_Off
1018 is
Nico Huber83693c82016-10-08 22:17:55 +02001019 begin
1020 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
1021
Nico Huber33912aa2016-12-06 20:36:23 +01001022 Legacy_VGA_Off;
1023
Arthur Heymans960e2392026-03-03 19:45:24 +01001024 for Pipe in Pipe_Index range Pipe_Index'First .. Config.Max_Pipe loop
Nico Huber75a707f2018-06-18 16:28:33 +02001025 Planes_Off (Controllers (Pipe), Cursors (Pipe));
Nico Huber7ad2d652016-12-07 15:19:32 +01001026 Transcoder.Off (Pipe);
Nico Huberf3e23662016-12-05 21:33:03 +01001027 Panel_Fitter_Off (Controllers (Pipe));
Nico Huber7ad2d652016-12-07 15:19:32 +01001028 Transcoder.Clk_Off (Pipe);
Nico Huber83693c82016-10-08 22:17:55 +02001029 end loop;
Nico Huber83693c82016-10-08 22:17:55 +02001030 end All_Off;
1031
Nico Huber83693c82016-10-08 22:17:55 +02001032end HW.GFX.GMA.Pipe_Setup;