blob: 83ded92d973759ec9d18afcea4cc00b7e2a7cd6f [file] [log] [blame]
Nico Huber83693c82016-10-08 22:17:55 +02001--
Nico Huber9a4c4c32019-09-16 22:05:11 +02002-- Copyright (C) 2014-2019 secunet Security Networks AG
Nico Huber2b6f6992017-07-09 18:11:34 +02003-- Copyright (C) 2017 Nico Huber <nico.h@gmx.de>
Nico Huber83693c82016-10-08 22:17:55 +02004--
5-- This program is free software; you can redistribute it and/or modify
6-- it under the terms of the GNU General Public License as published by
Nico Huber125a29e2016-10-18 00:23:54 +02007-- the Free Software Foundation; either version 2 of the License, or
8-- (at your option) any later version.
Nico Huber83693c82016-10-08 22:17:55 +02009--
10-- This program is distributed in the hope that it will be useful,
11-- but WITHOUT ANY WARRANTY; without even the implied warranty of
12-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13-- GNU General Public License for more details.
14--
15
Nico Huber2b6f6992017-07-09 18:11:34 +020016with HW.MMIO_Range;
17pragma Elaborate_All (HW.MMIO_Range);
18with HW.PCI.Dev;
19pragma Elaborate_All (HW.PCI.Dev);
20
Nico Huber83693c82016-10-08 22:17:55 +020021with HW.GFX.GMA.Config;
Nico Huber8c45bcf2016-11-20 17:30:57 +010022with HW.GFX.GMA.Config_Helpers;
Nico Huber83693c82016-10-08 22:17:55 +020023with HW.GFX.GMA.Registers;
Nico Huber312433c2019-09-28 03:15:48 +020024with HW.GFX.GMA.PCode;
Nico Huber83693c82016-10-08 22:17:55 +020025with HW.GFX.GMA.Power_And_Clocks;
26with HW.GFX.GMA.Panel;
27with HW.GFX.GMA.PLLs;
28with HW.GFX.GMA.Port_Detect;
29with HW.GFX.GMA.Connectors;
30with HW.GFX.GMA.Connector_Info;
31with HW.GFX.GMA.Pipe_Setup;
32
Nico Huber83693c82016-10-08 22:17:55 +020033with HW.Debug;
34with GNAT.Source_Info;
35
Nico Huber83693c82016-10-08 22:17:55 +020036use type HW.Int32;
37
38package body HW.GFX.GMA
39 with Refined_State =>
40 (State =>
Nico Hubere317e9c2019-09-29 03:03:18 +020041 (Config.Variable,
42 PCI_Usable,
Nico Huberc5c66ec2019-09-28 23:59:45 +020043 Dev.Address_State,
Nico Huber2b6f6992017-07-09 18:11:34 +020044 Registers.Address_State,
Nico Huber312433c2019-09-28 03:15:48 +020045 PCode.Mailbox_Ready,
Nico Huber83693c82016-10-08 22:17:55 +020046 PLLs.State, Panel.Panel_State,
Nico Huber1a712d32017-01-09 15:11:04 +010047 Cur_Configs, Allocated_PLLs,
Nico Huberc3f66f62017-07-16 21:39:54 +020048 HPD_Delay, Wait_For_HPD,
49 Linear_FB_Base),
Nico Huber83693c82016-10-08 22:17:55 +020050 Init_State => Initialized,
Nico Huber83693c82016-10-08 22:17:55 +020051 Device_State =>
Nico Huber2b6f6992017-07-09 18:11:34 +020052 (Dev.PCI_State, Registers.Register_State, Registers.GTT_State))
Nico Huber83693c82016-10-08 22:17:55 +020053is
Nico Huber2b6f6992017-07-09 18:11:34 +020054 pragma Disable_Atomic_Synchronization;
Nico Huber83693c82016-10-08 22:17:55 +020055
Tim Wawrzynczak605660b2022-06-08 12:48:19 -060056 subtype Port_Name is String (1 .. 10);
Nico Huber83693c82016-10-08 22:17:55 +020057 type Port_Name_Array is array (Port_Type) of Port_Name;
58 Port_Names : constant Port_Name_Array :=
Tim Wawrzynczak605660b2022-06-08 12:48:19 -060059 (Disabled => "Disabled ",
60 LVDS => "LVDS ",
61 eDP => "eDP ",
62 DP1 => "DP1 ",
63 DP2 => "DP2 ",
64 DP3 => "DP3 ",
65 HDMI1 => "HDMI1 ",
66 HDMI2 => "HDMI2 ",
67 HDMI3 => "HDMI3 ",
68 Analog => "Analog ",
69 USBC1_DP => "USBC1-DP ",
70 USBC2_DP => "USBC2-DP ",
71 USBC3_DP => "USBC3-DP ",
72 USBC4_DP => "USBC4-DP ",
73 USBC5_DP => "USBC5-DP ",
74 USBC6_DP => "USBC6-DP ",
75 USBC1_HDMI => "USBC1-HDMI",
76 USBC2_HDMI => "USBC2-HDMI",
77 USBC3_HDMI => "USBC3-HDMI",
78 USBC4_HDMI => "USBC4-HDMI",
79 USBC5_HDMI => "USBC5-HDMI",
80 USBC6_HDMI => "USBC6-HDMI");
Nico Huber83693c82016-10-08 22:17:55 +020081
Nico Huber2b6f6992017-07-09 18:11:34 +020082 package Dev is new HW.PCI.Dev (PCI.Address'(0, 2, 0));
83
Nico Huber83693c82016-10-08 22:17:55 +020084 package Display_Controller renames Pipe_Setup;
85
Nico Huber99f10f32016-11-20 00:34:05 +010086 type PLLs_Type is array (Pipe_Index) of PLLs.T;
Nico Huber83693c82016-10-08 22:17:55 +020087
Nico Huber83693c82016-10-08 22:17:55 +020088 type HPD_Type is array (Port_Type) of Boolean;
Nico Huber3be61d42017-01-09 13:58:18 +010089 type HPD_Delay_Type is array (Active_Port_Type) of Time.T;
Nico Huber83693c82016-10-08 22:17:55 +020090
Nico Huber83693c82016-10-08 22:17:55 +020091 Allocated_PLLs : PLLs_Type;
Nico Huber83693c82016-10-08 22:17:55 +020092 HPD_Delay : HPD_Delay_Type;
93 Wait_For_HPD : HPD_Type;
94 Initialized : Boolean := False;
95
Nico Huberc3f66f62017-07-16 21:39:54 +020096 Linear_FB_Base : Word64;
97
Nico Huber83693c82016-10-08 22:17:55 +020098 ----------------------------------------------------------------------------
99
Nico Huberdde06302020-12-20 02:18:30 +0100100 PCH_RAWCLK_FREQ_MASK : constant :=
101 (if Config.Has_Fractional_RawClk then 16#3fff# * 2 ** 16 else 16#3ff# * 2 ** 0);
Nico Huberf54d0962016-10-20 14:17:18 +0200102
103 function PCH_RAWCLK_FREQ (Freq : Frequency_Type) return Word32
104 is
105 begin
Nico Huberdde06302020-12-20 02:18:30 +0100106 if Config.Has_Fractional_RawClk then
107 declare
108 Fraction_K : constant Int64 := Freq / 1_000 mod 1_000;
109 Freq32 : Word32 := Shift_Left (Word32 (Freq / 1_000_000), 16);
110 begin
111 if Fraction_K /= 0 then
112 Freq32 := Freq32 or Shift_Left
113 (Word32 (Div_Round_Closest (1_000, Fraction_K) - 1), 26);
114 end if;
115 return Freq32;
116 end;
117 else
118 return Word32 (Freq / 1_000_000);
119 end if;
Nico Huberf54d0962016-10-20 14:17:18 +0200120 end PCH_RAWCLK_FREQ;
121
122 ----------------------------------------------------------------------------
123
Nico Huber43370ba2017-01-09 15:26:19 +0100124 procedure Enable_Output
125 (Pipe : in Pipe_Index;
126 Pipe_Cfg : in Pipe_Config;
127 Success : out Boolean)
Nico Huber8a5a3b52018-06-04 14:42:13 +0200128 with
Nico Huber9a4c4c32019-09-16 22:05:11 +0200129 Pre =>
130 Pipe_Cfg.Port in Active_Port_Type and
131 Config_Helpers.Valid_FB (Pipe_Cfg.Framebuffer, Pipe_Cfg.Mode)
Nico Huber43370ba2017-01-09 15:26:19 +0100132 is
133 Port_Cfg : Port_Config;
134 begin
Nico Huber3be61d42017-01-09 13:58:18 +0100135 pragma Debug (Debug.New_Line);
136 pragma Debug (Debug.Put_Line
137 ("Trying to enable port " & Port_Names (Pipe_Cfg.Port)));
138
Nico Huber43370ba2017-01-09 15:26:19 +0100139 Config_Helpers.Fill_Port_Config
140 (Port_Cfg, Pipe, Pipe_Cfg.Port, Pipe_Cfg.Mode, Success);
141
142 if Success then
Nico Huber43370ba2017-01-09 15:26:19 +0100143 Connector_Info.Preferred_Link_Setting (Port_Cfg, Success);
144 end if;
145
146 -- loop over all possible DP-lane configurations
147 -- (non-DP ports use a single fake configuration)
148 while Success loop
149 pragma Loop_Invariant
150 (Pipe_Cfg.Port in Active_Port_Type and
151 Port_Cfg.Mode = Port_Cfg.Mode'Loop_Entry);
152
153 PLLs.Alloc
154 (Port_Cfg => Port_Cfg,
155 PLL => Allocated_PLLs (Pipe),
156 Success => Success);
157
158 if Success then
159 -- try each DP-lane configuration twice
160 for Try in 1 .. 2 loop
161 pragma Loop_Invariant
162 (Pipe_Cfg.Port in Active_Port_Type);
163
Nico Huber4798c662017-01-11 12:44:48 +0100164 -- Clear pending hot-plug events before every try
165 Port_Detect.Clear_Hotplug_Detect (Pipe_Cfg.Port);
166
Nico Huber43370ba2017-01-09 15:26:19 +0100167 Connectors.Pre_On
168 (Pipe => Pipe,
169 Port_Cfg => Port_Cfg,
170 PLL_Hint => PLLs.Register_Value (Allocated_PLLs (Pipe)),
171 Success => Success);
172
173 if Success then
174 Display_Controller.On
175 (Pipe => Pipe,
176 Port_Cfg => Port_Cfg,
Nico Huber4dc4c612018-01-10 15:55:09 +0100177 Framebuffer => Pipe_Cfg.Framebuffer,
178 Cursor => Pipe_Cfg.Cursor);
Nico Huber43370ba2017-01-09 15:26:19 +0100179
180 Connectors.Post_On
Arthur Heymans60d0e5f2018-03-28 17:08:27 +0200181 (Pipe => Pipe,
182 Port_Cfg => Port_Cfg,
Nico Huber43370ba2017-01-09 15:26:19 +0100183 PLL_Hint => PLLs.Register_Value (Allocated_PLLs (Pipe)),
184 Success => Success);
185
186 if not Success then
187 Display_Controller.Off (Pipe);
188 Connectors.Post_Off (Port_Cfg);
189 end if;
190 end if;
191
192 exit when Success;
193 end loop;
194 exit when Success; -- connection established => stop loop
195
196 -- connection failed
197 PLLs.Free (Allocated_PLLs (Pipe));
198 end if;
199
200 Connector_Info.Next_Link_Setting (Port_Cfg, Success);
201 end loop;
202
203 if Success then
204 pragma Debug (Debug.Put_Line
205 ("Enabled port " & Port_Names (Pipe_Cfg.Port)));
206 else
207 Wait_For_HPD (Pipe_Cfg.Port) := True;
Nico Huber2bbd6e72020-01-07 18:22:59 +0100208 Panel.Off (Config_Helpers.To_Panel (Pipe_Cfg.Port));
Nico Huber43370ba2017-01-09 15:26:19 +0100209 end if;
210 end Enable_Output;
211
Nico Huber3be61d42017-01-09 13:58:18 +0100212 procedure Disable_Output (Pipe : Pipe_Index; Pipe_Cfg : Pipe_Config)
213 is
214 Port_Cfg : Port_Config;
215 Success : Boolean;
216 begin
217 Config_Helpers.Fill_Port_Config
218 (Port_Cfg, Pipe, Pipe_Cfg.Port, Pipe_Cfg.Mode, Success);
219 if Success then
220 pragma Debug (Debug.New_Line);
221 pragma Debug (Debug.Put_Line
222 ("Disabling port " & Port_Names (Pipe_Cfg.Port)));
223 pragma Debug (Debug.New_Line);
224
Jeremy Compostellafe80fbb2023-01-11 14:05:42 -0700225 if Pipe_Cfg.Framebuffer.Offset = VGA_PLANE_FRAMEBUFFER_OFFSET then
226 Display_Controller.Legacy_VGA_Off;
227 end if;
228
Nico Huber3be61d42017-01-09 13:58:18 +0100229 Connectors.Pre_Off (Port_Cfg);
230 Display_Controller.Off (Pipe);
231 Connectors.Post_Off (Port_Cfg);
232
233 PLLs.Free (Allocated_PLLs (Pipe));
234 end if;
235 end Disable_Output;
236
Nico Huber99f10f32016-11-20 00:34:05 +0100237 procedure Update_Outputs (Configs : Pipe_Configs)
Nico Huber83693c82016-10-08 22:17:55 +0200238 is
Nico Huber3be61d42017-01-09 13:58:18 +0100239 procedure Check_HPD (Port : in Active_Port_Type; Detected : out Boolean)
240 is
241 HPD_Delay_Over : constant Boolean := Time.Timed_Out (HPD_Delay (Port));
242 begin
243 if HPD_Delay_Over then
244 Port_Detect.Hotplug_Detect (Port, Detected);
245 HPD_Delay (Port) := Time.MS_From_Now (333);
246 else
247 Detected := False;
248 end if;
249 end Check_HPD;
Nico Huberb56b9c52017-01-11 15:12:23 +0100250
Nico Huber9a4c4c32019-09-16 22:05:11 +0200251 Scaler_Reservation : Display_Controller.Scaler_Reservation :=
252 Display_Controller.Null_Scaler_Reservation;
Nico Huber564103f2017-01-11 15:33:07 +0100253
Nico Huber9a4c4c32019-09-16 22:05:11 +0200254 Update_Power : Boolean := False;
Nico Huberb0bbdbc2019-09-27 22:32:21 +0200255 Update_CDClk : Boolean;
Nico Huber9a4c4c32019-09-16 22:05:11 +0200256 Old_Configs,
257 New_Configs : Pipe_Configs;
Nico Huber3d06de82018-05-29 01:35:04 +0200258
259 function Full_Update (Cur_Config, New_Config : Pipe_Config) return Boolean
260 is
261 begin
262 return
Nico Huber958c5642018-06-02 16:59:31 +0200263 Cur_Config.Port /= New_Config.Port
264 or else
265 Cur_Config.Mode /= New_Config.Mode
266 or else
Nico Huber3d06de82018-05-29 01:35:04 +0200267 (Config.Use_PDW_For_EDP_Scaling and then
Nico Huber8beafd72020-01-07 14:59:44 +0100268 (Cur_Config.Port = eDP and
Nico Huber958c5642018-06-02 16:59:31 +0200269 Requires_Scaling (Cur_Config) /= Requires_Scaling (New_Config)))
270 or else
271 (Config.Has_GMCH_PFIT_CONTROL and then
272 (Requires_Scaling (Cur_Config) /= Requires_Scaling (New_Config) or
273 Scaling_Type (Cur_Config) /= Scaling_Type (New_Config)));
Nico Huber3d06de82018-05-29 01:35:04 +0200274 end Full_Update;
Nico Huber83693c82016-10-08 22:17:55 +0200275 begin
276 Old_Configs := Cur_Configs;
Nico Huber9a4c4c32019-09-16 22:05:11 +0200277 New_Configs := Configs;
278
279 -- validate new configs, filter invalid configs and those waiting for HPD
280 for Pipe in Pipe_Index loop
281 declare
282 Success : Boolean := True;
283 Cur_Config : Pipe_Config renames Cur_Configs (Pipe);
284 New_Config : Pipe_Config renames New_Configs (Pipe);
285 begin
286 if New_Config.Port /= Disabled then
287 if Wait_For_HPD (New_Config.Port) then
288 Check_HPD (New_Config.Port, Success);
289 Wait_For_HPD (New_Config.Port) := not Success;
290 end if;
291
292 Success := Success and then
293 Config_Helpers.Validate_Config
294 (New_Config.Framebuffer, New_Config.Mode, Pipe);
295
296 if Success and then Requires_Scaling (New_Config) then
297 Display_Controller.Reserve_Scaler
298 (Success, Scaler_Reservation, Pipe);
299 end if;
300
301 if not Success then
302 New_Config.Port := Disabled;
303 end if;
304 end if;
305 end;
306 pragma Loop_Invariant
307 (for all P in Pipe_Index'First .. Pipe =>
308 New_Configs (P).Port = Disabled or
309 Config_Helpers.Valid_FB
310 (New_Configs (P).Framebuffer, New_Configs (P).Mode));
311 end loop;
Nico Huber83693c82016-10-08 22:17:55 +0200312
Nico Huberb0bbdbc2019-09-27 22:32:21 +0200313 -- limit dotclocks to maximum CDClk, if we are about
314 -- to switch CDClk, all pipes have to be disabled
315 Power_And_Clocks.Limit_Dotclocks (New_Configs, Update_CDClk);
316
Nico Huberb56b9c52017-01-11 15:12:23 +0100317 -- disable all pipes that changed or had a hot-plug event
318 for Pipe in Pipe_Index loop
319 declare
320 Unplug_Detected : Boolean;
321 Cur_Config : Pipe_Config renames Cur_Configs (Pipe);
Nico Huber9a4c4c32019-09-16 22:05:11 +0200322 New_Config : Pipe_Config renames New_Configs (Pipe);
Nico Huberb56b9c52017-01-11 15:12:23 +0100323 begin
324 if Cur_Config.Port /= Disabled then
325 Check_HPD (Cur_Config.Port, Unplug_Detected);
Nico Huber83693c82016-10-08 22:17:55 +0200326
Nico Huberb0bbdbc2019-09-27 22:32:21 +0200327 if Update_CDClk or
328 Unplug_Detected or
329 Full_Update (Cur_Config, New_Config)
330 then
Nico Huberb56b9c52017-01-11 15:12:23 +0100331 Disable_Output (Pipe, Cur_Config);
332 Cur_Config.Port := Disabled;
Nico Huber9a4c4c32019-09-16 22:05:11 +0200333 Update_Power := True;
Nico Huberb56b9c52017-01-11 15:12:23 +0100334 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200335 end if;
Nico Huberb56b9c52017-01-11 15:12:23 +0100336 end;
337 end loop;
Nico Huber83693c82016-10-08 22:17:55 +0200338
Nico Huberb0bbdbc2019-09-27 22:32:21 +0200339 -- switch CDClk if necessary and possible, limit dotclocks accordingly
340 if Update_CDClk then
341 Power_And_Clocks.Update_CDClk (New_Configs);
342 end if;
343
Nico Huberb56b9c52017-01-11 15:12:23 +0100344 -- enable all pipes that changed and should be active
345 for Pipe in Pipe_Index loop
346 declare
347 Success : Boolean;
348 Cur_Config : Pipe_Config renames Cur_Configs (Pipe);
Nico Huber9a4c4c32019-09-16 22:05:11 +0200349 New_Config : Pipe_Config renames New_Configs (Pipe);
Nico Huberb56b9c52017-01-11 15:12:23 +0100350 begin
Nico Huber9a4c4c32019-09-16 22:05:11 +0200351 -- full update
Nico Huber3d06de82018-05-29 01:35:04 +0200352 if New_Config.Port /= Disabled and
353 Full_Update (Cur_Config, New_Config)
Nico Huberb56b9c52017-01-11 15:12:23 +0100354 then
Nico Huber9a4c4c32019-09-16 22:05:11 +0200355 Power_And_Clocks.Power_Up (Old_Configs, New_Configs);
356 Update_Power := True;
Nico Huberc7a4fee2016-11-03 18:18:03 +0100357
Nico Huber9a4c4c32019-09-16 22:05:11 +0200358 Enable_Output (Pipe, New_Config, Success);
Nico Huber83693c82016-10-08 22:17:55 +0200359 if Success then
Nico Huberb56b9c52017-01-11 15:12:23 +0100360 Cur_Config := New_Config;
Nico Huber83693c82016-10-08 22:17:55 +0200361 end if;
Nico Huber3be61d42017-01-09 13:58:18 +0100362
Nico Huberb56b9c52017-01-11 15:12:23 +0100363 -- update framebuffer offset only
364 elsif New_Config.Port /= Disabled and
Nico Huberf361ec82018-06-02 18:01:45 +0200365 Cur_Config.Framebuffer /= New_Config.Framebuffer
Nico Huberb56b9c52017-01-11 15:12:23 +0100366 then
Nico Huber9a4c4c32019-09-16 22:05:11 +0200367 Display_Controller.Setup_FB
368 (Pipe, New_Config.Mode, New_Config.Framebuffer);
369 Display_Controller.Update_Cursor
370 (Pipe, New_Config.Framebuffer, New_Config.Cursor);
371 Cur_Config := New_Config;
Nico Huberb56b9c52017-01-11 15:12:23 +0100372 end if;
373 end;
Nico Huber83693c82016-10-08 22:17:55 +0200374 end loop;
375
Nico Huber9a4c4c32019-09-16 22:05:11 +0200376 if Update_Power then
377 Power_And_Clocks.Power_Down (Old_Configs, New_Configs, Cur_Configs);
Nico Huber83693c82016-10-08 22:17:55 +0200378 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200379 end Update_Outputs;
380
381 ----------------------------------------------------------------------------
382
Nico Huber15ffc4f2018-01-11 14:44:43 +0100383 procedure Update_Cursor (Pipe : Pipe_Index; Cursor : Cursor_Type)
384 is
385 begin
386 Cur_Configs (Pipe).Cursor := Cursor;
387 Display_Controller.Update_Cursor
388 (Pipe, Cur_Configs (Pipe).Framebuffer, Cur_Configs (Pipe).Cursor);
389 end Update_Cursor;
390
391 procedure Place_Cursor
392 (Pipe : Pipe_Index;
393 X : Cursor_Pos;
394 Y : Cursor_Pos)
395 is
396 begin
397 Cur_Configs (Pipe).Cursor.Center_X := X;
398 Cur_Configs (Pipe).Cursor.Center_Y := Y;
399 Display_Controller.Place_Cursor
400 (Pipe, Cur_Configs (Pipe).Framebuffer, Cur_Configs (Pipe).Cursor);
401 end Place_Cursor;
402
403 procedure Move_Cursor
404 (Pipe : Pipe_Index;
405 X : Cursor_Pos;
406 Y : Cursor_Pos)
407 is
408 function Cap_Add (A, B : Cursor_Pos) return Cursor_Pos is
409 (if A + B < 0
410 then Int32'Max (Cursor_Pos'First, A + B)
411 else Int32'Min (Cursor_Pos'Last, A + B));
412 begin
413 Place_Cursor
414 (Pipe => Pipe,
415 X => Cap_Add (Cur_Configs (Pipe).Cursor.Center_X, X),
416 Y => Cap_Add (Cur_Configs (Pipe).Cursor.Center_Y, Y));
417 end Move_Cursor;
418
419 ----------------------------------------------------------------------------
420
Nico Huberbc0588e2020-07-21 12:17:19 +0200421 procedure Backlight_On (Port : Active_Port_Type)
422 with
423 Refined_Global => (In_Out => Registers.Register_State)
424 is
425 begin
426 Panel.Backlight_On (Config_Helpers.To_Panel (Port));
427 end Backlight_On;
428
429 procedure Backlight_Off (Port : Active_Port_Type)
430 with
431 Refined_Global => (In_Out => Registers.Register_State)
432 is
433 begin
434 Panel.Backlight_Off (Config_Helpers.To_Panel (Port));
435 end Backlight_Off;
436
437 procedure Set_Brightness (Port : Active_Port_Type; Level : Word32)
438 with
439 Refined_Global => (In_Out => Registers.Register_State)
440 is
441 begin
442 Panel.Set_Backlight (Config_Helpers.To_Panel (Port), Level);
443 end Set_Brightness;
444
445 procedure Get_Max_Brightness (Port : Active_Port_Type; Level : out Word32)
446 with
447 Refined_Global => (In_Out => Registers.Register_State)
448 is
449 begin
450 Panel.Get_Max_Backlight (Config_Helpers.To_Panel (Port), Level);
451 end Get_Max_Brightness;
452
453 ----------------------------------------------------------------------------
454
Nico Huber793f4f82022-09-04 14:24:00 +0000455 pragma Warnings
456 (GNATprove, Off, """Registers.GTT_State"" * is not modified*",
457 Reason => "The whole, abstract Device_State is modified in certain configurations.");
458 pragma Warnings
459 (GNATprove, Off, "no check message justified*", Reason => "see below");
Nico Huber83693c82016-10-08 22:17:55 +0200460 procedure Initialize
Nico Huber2b6f6992017-07-09 18:11:34 +0200461 (Write_Delay : in Word64 := 0;
Nico Huber793a8d42016-11-21 18:57:03 +0100462 Clean_State : in Boolean := False;
Nico Huber83693c82016-10-08 22:17:55 +0200463 Success : out Boolean)
464 with
465 Refined_Global =>
Nico Huber27088aa2018-06-10 13:28:05 +0200466 (Input => (Time.State),
Nico Huber793f4f82022-09-04 14:24:00 +0000467 In_Out =>
468 (Dev.PCI_State, Port_IO.State,
469 Registers.Register_State, Registers.GTT_State),
Nico Huber83693c82016-10-08 22:17:55 +0200470 Output =>
Nico Huberc5c66ec2019-09-28 23:59:45 +0200471 (PCI_Usable,
472 Config.Variable,
Nico Huber27088aa2018-06-10 13:28:05 +0200473 Dev.Address_State,
Nico Huber2b6f6992017-07-09 18:11:34 +0200474 Registers.Address_State,
Nico Huber312433c2019-09-28 03:15:48 +0200475 PCode.Mailbox_Ready,
Nico Huber83693c82016-10-08 22:17:55 +0200476 PLLs.State, Panel.Panel_State,
Nico Huber1a712d32017-01-09 15:11:04 +0100477 Cur_Configs, Allocated_PLLs,
Nico Huberc3f66f62017-07-16 21:39:54 +0200478 HPD_Delay, Wait_For_HPD,
479 Linear_FB_Base, Initialized))
Nico Huber83693c82016-10-08 22:17:55 +0200480 is
481 use type HW.Word64;
482
Nico Huber0b2329a2018-06-09 21:14:27 +0200483 function MMIO_GTT_Offset return Natural is
484 (if Config.Has_64bit_GTT
485 then Registers.MMIO_GTT_64_Offset
486 else Registers.MMIO_GTT_32_Offset);
Nico Huber2b6f6992017-07-09 18:11:34 +0200487 PCI_MMIO_Base, PCI_GTT_Base : Word64;
488
Nico Huber83693c82016-10-08 22:17:55 +0200489 Now : constant Time.T := Time.Now;
490
491 procedure Check_Platform (Success : out Boolean)
492 is
493 Audio_VID_DID : Word32;
494 begin
Nico Huber6621a142018-06-07 23:56:54 +0200495 case Config.Gen is
Arthur Heymans73ea0322018-03-28 17:17:07 +0200496 when G45 =>
497 Registers.Read (Registers.G4X_AUD_VID_DID, Audio_VID_DID);
Nico Huber6621a142018-06-07 23:56:54 +0200498 when Ironlake =>
499 Registers.Read (Registers.PCH_AUD_VID_DID, Audio_VID_DID);
Tim Wawrzynczak605660b2022-06-08 12:48:19 -0600500 when Haswell .. Tigerlake =>
Nico Huber83693c82016-10-08 22:17:55 +0200501 Registers.Read (Registers.AUD_VID_DID, Audio_VID_DID);
Nico Huber83693c82016-10-08 22:17:55 +0200502 end case;
503 Success :=
Nico Huber998ee2b2018-06-12 23:02:17 +0200504 ((Config.Gen_Broxton and Audio_VID_DID = 16#8086_280a#) or
Nico Huber88badbe2018-09-27 16:36:47 +0200505 (Config.CPU_Kabylake and Audio_VID_DID = 16#8086_280b#) or
506 (Config.CPU_Skylake and Audio_VID_DID = 16#8086_2809#) or
Nico Huber998ee2b2018-06-12 23:02:17 +0200507 (Config.CPU_Broadwell and Audio_VID_DID = 16#8086_2808#) or
508 (Config.CPU_Haswell and Audio_VID_DID = 16#8086_2807#) or
509 ((Config.CPU_Ivybridge or
510 Config.CPU_Sandybridge) and (Audio_VID_DID = 16#8086_2806# or
511 Audio_VID_DID = 16#8086_2805#)) or
512 (Config.CPU_Ironlake and Audio_VID_DID = 16#0000_0000#) or
513 (Config.Gen_G45 and (Audio_VID_DID = 16#8086_2801# or
514 Audio_VID_DID = 16#8086_2802# or
Tim Wawrzynczak605660b2022-06-08 12:48:19 -0600515 Audio_VID_DID = 16#8086_2803#)) or
516 (Config.CPU_Tigerlake and (Audio_VID_DID = 16#8086_2812#)));
Nico Huber83693c82016-10-08 22:17:55 +0200517 end Check_Platform;
Nico Hubere7ac6eb2017-09-04 23:54:13 +0200518
519 procedure Check_Platform_PCI (Success : out Boolean)
520 is
521 use type HW.Word16;
522 Vendor, Device : Word16;
523 begin
524 Dev.Read16 (Vendor, PCI.Vendor_Id);
525 Dev.Read16 (Device, PCI.Device_Id);
526
Nico Huber6a996dc2018-06-17 16:30:33 +0200527 Config.Detect_CPU (Device);
Nico Hubere7ac6eb2017-09-04 23:54:13 +0200528 Success := Vendor = 16#8086# and Config.Compatible_GPU (Device);
529 end Check_Platform_PCI;
Nico Huber83693c82016-10-08 22:17:55 +0200530 begin
Nico Huber83693c82016-10-08 22:17:55 +0200531 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
532
533 pragma Debug (Debug.Set_Register_Write_Delay (Write_Delay));
534
Nico Huberc5c66ec2019-09-28 23:59:45 +0200535 PCI_Usable := False;
Nico Huberc3f66f62017-07-16 21:39:54 +0200536 Linear_FB_Base := 0;
Nico Huber312433c2019-09-28 03:15:48 +0200537 PCode.Mailbox_Ready := False;
Nico Huber83693c82016-10-08 22:17:55 +0200538 Wait_For_HPD := HPD_Type'(others => False);
539 HPD_Delay := HPD_Delay_Type'(others => Now);
Nico Huber83693c82016-10-08 22:17:55 +0200540 Allocated_PLLs := (others => PLLs.Invalid);
Nico Huber99f10f32016-11-20 00:34:05 +0100541 Cur_Configs := Pipe_Configs'
542 (others => Pipe_Config'
Nico Huber83693c82016-10-08 22:17:55 +0200543 (Port => Disabled,
544 Framebuffer => HW.GFX.Default_FB,
Nico Hubera02b2c62018-01-09 15:58:34 +0100545 Cursor => Default_Cursor,
Nico Huber83693c82016-10-08 22:17:55 +0200546 Mode => HW.GFX.Invalid_Mode));
Nico Huber27088aa2018-06-10 13:28:05 +0200547 Config.Variable := Config.Initial_Settings;
Nico Huber6a996dc2018-06-17 16:30:33 +0200548 Registers.Set_Register_Base (Config.Default_MMIO_Base);
Nico Huber83693c82016-10-08 22:17:55 +0200549 PLLs.Initialize;
550
Nico Huber2b6f6992017-07-09 18:11:34 +0200551 Dev.Initialize (Success);
552
553 if Success then
Nico Huber6a996dc2018-06-17 16:30:33 +0200554 Check_Platform_PCI (Success);
Nico Hubere7ac6eb2017-09-04 23:54:13 +0200555 if Success then
Nico Huber6a996dc2018-06-17 16:30:33 +0200556 Dev.Map (PCI_MMIO_Base, PCI.Res0, Length => MMIO_GTT_Offset);
557 Dev.Map (PCI_GTT_Base, PCI.Res0, Offset => MMIO_GTT_Offset);
558 if PCI_MMIO_Base /= 0 and PCI_GTT_Base /= 0 then
559 Registers.Set_Register_Base (PCI_MMIO_Base, PCI_GTT_Base);
Nico Huberc5c66ec2019-09-28 23:59:45 +0200560 PCI_Usable := True;
Nico Huber6a996dc2018-06-17 16:30:33 +0200561 else
562 pragma Debug (Debug.Put_Line
563 ("ERROR: Couldn't map resoure0."));
564 Success := Config.Default_MMIO_Base_Set;
565 end if;
Nico Hubere7ac6eb2017-09-04 23:54:13 +0200566 end if;
Nico Huber2b6f6992017-07-09 18:11:34 +0200567 else
568 pragma Debug (Debug.Put_Line
569 ("WARNING: Couldn't initialize PCI dev."));
Nico Huber2b6f6992017-07-09 18:11:34 +0200570 Success := Config.Default_MMIO_Base_Set;
Nico Huber2b6f6992017-07-09 18:11:34 +0200571
Nico Hubere7ac6eb2017-09-04 23:54:13 +0200572 if Success then
573 Check_Platform (Success);
574 end if;
Nico Huber2b6f6992017-07-09 18:11:34 +0200575 end if;
576
Nico Huber5dbaf4b2020-01-08 17:24:58 +0100577 Panel.Static_Init; -- early for flow analysis
578
Nico Huber83693c82016-10-08 22:17:55 +0200579 if not Success then
580 pragma Debug (Debug.Put_Line ("ERROR: Incompatible CPU or PCH."));
581
Nico Huber83693c82016-10-08 22:17:55 +0200582 Initialized := False;
583 return;
584 end if;
585
586 Panel.Setup_PP_Sequencer;
587 Port_Detect.Initialize;
Nico Huber0923b792017-06-09 15:28:41 +0200588 Connectors.Initialize;
Nico Huber83693c82016-10-08 22:17:55 +0200589
Nico Huber793a8d42016-11-21 18:57:03 +0100590 if Clean_State then
591 Power_And_Clocks.Pre_All_Off;
592 Connectors.Pre_All_Off;
593 Display_Controller.All_Off;
594 Connectors.Post_All_Off;
595 PLLs.All_Off;
596 Power_And_Clocks.Post_All_Off;
Nico Huber17d64b62017-07-15 20:51:25 +0200597 Registers.Clear_Fences;
Nico Huber33912aa2016-12-06 20:36:23 +0100598 else
599 -- According to PRMs, VGA plane is the only thing
Nico Huber3a0e2a02017-07-19 14:41:46 +0200600 -- that's enabled by default after reset...
Nico Huber33912aa2016-12-06 20:36:23 +0100601 Display_Controller.Legacy_VGA_Off;
Nico Huber3a0e2a02017-07-19 14:41:46 +0200602 -- ... along with some DDI port bits since Skylake.
603 Connectors.Post_Reset_Off;
Nico Huber793a8d42016-11-21 18:57:03 +0100604 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200605
606 -------------------- Now restart from a clean state ---------------------
607 Power_And_Clocks.Initialize;
608
Nico Huber1c3b9282017-02-09 13:57:04 +0100609 if Config.Has_PCH then
610 Registers.Unset_And_Set_Mask
611 (Register => Registers.PCH_RAWCLK_FREQ,
612 Mask_Unset => PCH_RAWCLK_FREQ_MASK,
Nico Huberc9ad9de2020-12-20 02:34:37 +0100613 Mask_Set => PCH_RAWCLK_FREQ (Config.Raw_Clock));
Nico Huber1c3b9282017-02-09 13:57:04 +0100614 end if;
Nico Huberf54d0962016-10-20 14:17:18 +0200615
Nico Huber83693c82016-10-08 22:17:55 +0200616 Initialized := True;
617
618 end Initialize;
Nico Huber793f4f82022-09-04 14:24:00 +0000619 pragma Annotate
620 (GNATprove, Intentional, "unused global",
621 "The whole, abstract Device_State is modified in certain configurations.");
622 pragma Warnings (GNATprove, On, "no check message justified*");
623 pragma Warnings
624 (GNATprove, On, """Registers.GTT_State"" * is not modified*");
Nico Huber83693c82016-10-08 22:17:55 +0200625
626 function Is_Initialized return Boolean
627 with
628 Refined_Post => Is_Initialized'Result = Initialized
629 is
630 begin
631 return Initialized;
632 end Is_Initialized;
633
634 ----------------------------------------------------------------------------
635
Nico Hubercf88f3d2018-06-05 13:27:34 +0200636 pragma Warnings
637 (GNATprove, Off, """Registers.Register_State"" * is not modified*",
Nico Huberadfe11f2018-06-10 14:59:04 +0200638 Reason => "Power_Up_VGA is only effective in certain configurations.");
Nico Huber17b513e2022-09-04 13:36:02 +0200639 pragma Warnings
640 (GNATprove, Off, "no check message justified*", Reason => "see below");
Nico Huber42fb2d02017-09-01 17:01:51 +0200641 procedure Power_Up_VGA
Nico Hubercf88f3d2018-06-05 13:27:34 +0200642 with
643 Refined_Global =>
Nico Huberadfe11f2018-06-10 14:59:04 +0200644 (Input => (Cur_Configs, Config.Variable, Time.State),
Nico Hubercf88f3d2018-06-05 13:27:34 +0200645 In_Out => (Registers.Register_State),
646 Proof_In => (Initialized))
Nico Huber42fb2d02017-09-01 17:01:51 +0200647 is
648 Fake_Config : constant Pipe_Configs :=
649 (Primary =>
650 (Port => Analog,
651 Framebuffer => HW.GFX.Default_FB,
Nico Hubera02b2c62018-01-09 15:58:34 +0100652 Cursor => Default_Cursor,
Nico Huber42fb2d02017-09-01 17:01:51 +0200653 Mode => HW.GFX.Invalid_Mode),
654 others =>
655 (Port => Disabled,
656 Framebuffer => HW.GFX.Default_FB,
Nico Hubera02b2c62018-01-09 15:58:34 +0100657 Cursor => Default_Cursor,
Nico Huber42fb2d02017-09-01 17:01:51 +0200658 Mode => HW.GFX.Invalid_Mode));
659 begin
660 Power_And_Clocks.Power_Up (Cur_Configs, Fake_Config);
661 end Power_Up_VGA;
Nico Hubercf88f3d2018-06-05 13:27:34 +0200662 pragma Annotate
663 (GNATprove, Intentional, "unused global",
Nico Huberadfe11f2018-06-10 14:59:04 +0200664 "Power_Up_VGA is only effective in certain configurations.");
Nico Hubercf88f3d2018-06-05 13:27:34 +0200665 pragma Warnings (GNATprove, On, "no check message justified*");
666 pragma Warnings
667 (GNATprove, On, """Registers.Register_State"" * is not modified*");
Nico Huber42fb2d02017-09-01 17:01:51 +0200668
669 ----------------------------------------------------------------------------
670
Nico Huber5374c3a2017-07-15 21:48:06 +0200671 function FB_First_Page (FB : Framebuffer_Type) return Natural is
Nico Huber34be6542017-12-13 09:26:24 +0100672 (Natural (Phys_Offset (FB) / GTT_Page_Size));
Nico Huber5374c3a2017-07-15 21:48:06 +0200673 function FB_Pages (FB : Framebuffer_Type) return Natural is
674 (Natural (Div_Round_Up (FB_Size (FB), GTT_Page_Size)));
675 function FB_Last_Page (FB : Framebuffer_Type) return Natural is
676 (FB_First_Page (FB) + FB_Pages (FB) - 1);
677
Nico Huber34be6542017-12-13 09:26:24 +0100678 -- Check basics and that it fits in GTT. For 90 degree rotations,
679 -- the Offset should be above GTT_Rotation_Offset. The latter will
680 -- be subtracted for the aperture mapping.
Nico Huber5374c3a2017-07-15 21:48:06 +0200681 function Valid_FB (FB : Framebuffer_Type) return Boolean is
Nico Huber34be6542017-12-13 09:26:24 +0100682 (Valid_Stride (FB) and
683 FB_First_Page (FB) in GTT_Range and
Nico Huber2e87c0d2020-04-18 00:46:39 +0200684 FB_Last_Page (FB) + 128 in GTT_Range and
Nico Huber34be6542017-12-13 09:26:24 +0100685 (not Rotation_90 (FB) or
Nico Huber2e87c0d2020-04-18 00:46:39 +0200686 (FB_First_Page (FB) mod 64 = 0 and
687 FB_Last_Page (FB) + 128 + GTT_Rotation_Offset in GTT_Range and
Nico Huber34be6542017-12-13 09:26:24 +0100688 FB.Offset >= Word32 (GTT_Rotation_Offset) * GTT_Page_Size)));
Nico Huber5374c3a2017-07-15 21:48:06 +0200689
690 -- Also check that we don't overflow the GTT's 39-bit space
691 -- (always true with a 32-bit base)
692 function Valid_Phys_FB (FB : Framebuffer_Type; Phys_Base : Word32)
693 return Boolean is
694 (Valid_FB (FB) and
Nico Huber34be6542017-12-13 09:26:24 +0100695 Int64 (Phys_Base) + Int64 (Phys_Offset (FB)) + Int64 (FB_Size (FB)) <=
Nico Huber5374c3a2017-07-15 21:48:06 +0200696 Int64 (GTT_Address_Type'Last))
697 with
698 Ghost;
699
Nico Huber83693c82016-10-08 22:17:55 +0200700 procedure Write_GTT
701 (GTT_Page : GTT_Range;
702 Device_Address : GTT_Address_Type;
Nico Huber5374c3a2017-07-15 21:48:06 +0200703 Valid : Boolean)
704 is
Nico Huber83693c82016-10-08 22:17:55 +0200705 begin
706 Registers.Write_GTT (GTT_Page, Device_Address, Valid);
707 end Write_GTT;
708
Nico Huberceda17d2018-06-09 22:00:29 +0200709 procedure Read_GTT
710 (Device_Address : out GTT_Address_Type;
711 Valid : out Boolean;
712 GTT_Page : in GTT_Range)
713 is
714 begin
715 Registers.Read_GTT (Device_Address, Valid, GTT_Page);
716 end Read_GTT;
717
Nico Huber194e57e2017-07-15 21:15:46 +0200718 procedure Setup_Default_GTT (FB : Framebuffer_Type; Phys_Base : Word32)
Nico Huber5374c3a2017-07-15 21:48:06 +0200719 with
720 Pre => Is_Initialized and Valid_Phys_FB (FB, Phys_Base)
Nico Huber83693c82016-10-08 22:17:55 +0200721 is
Nico Huber194e57e2017-07-15 21:15:46 +0200722 Phys_Addr : GTT_Address_Type :=
Nico Huber34be6542017-12-13 09:26:24 +0100723 GTT_Address_Type (Phys_Base) + GTT_Address_Type (Phys_Offset (FB));
Nico Huber83693c82016-10-08 22:17:55 +0200724 begin
Nico Huber194e57e2017-07-15 21:15:46 +0200725 for Idx in FB_First_Page (FB) .. FB_Last_Page (FB) loop
Nico Huber83693c82016-10-08 22:17:55 +0200726 Registers.Write_GTT
727 (GTT_Page => Idx,
728 Device_Address => Phys_Addr,
729 Valid => True);
Nico Huber194e57e2017-07-15 21:15:46 +0200730 Phys_Addr := Phys_Addr + GTT_Page_Size;
Nico Huber83693c82016-10-08 22:17:55 +0200731 end loop;
Nico Huber2e87c0d2020-04-18 00:46:39 +0200732 -- Add another 128 dummy pages to work around buggy VT-d
733 for Idx in FB_Last_Page (FB) + 1 .. FB_Last_Page (FB) + 128 loop
734 Registers.Write_GTT (Idx, Phys_Addr, True);
735 end loop;
Nico Huber9b479412017-08-27 11:55:56 +0200736
737 if Rotation_90 (FB) and FB.Tiling = Y_Tiled and FB.V_Stride >= 32 then
738 declare
739 V_Pages : constant Natural := Natural (FB.V_Stride) / 32;
740 Bytes_Per_Row : constant GTT_Address_Type :=
741 GTT_Address_Type (Pixel_To_Bytes (32 * FB.Stride, FB));
742 begin
743 Phys_Addr := GTT_Address_Type (Phys_Base) +
Nico Huber34be6542017-12-13 09:26:24 +0100744 GTT_Address_Type (Phys_Offset (FB)) +
Nico Huber9b479412017-08-27 11:55:56 +0200745 GTT_Address_Type (FB_Size (FB));
746 for Page in FB_First_Page (FB) .. FB_Last_Page (FB) loop
747 Phys_Addr := Phys_Addr - Bytes_Per_Row;
748 Registers.Write_GTT
749 (GTT_Page => GTT_Rotation_Offset + Page,
750 Device_Address => Phys_Addr,
751 Valid => True);
752
753 if (Page - FB_First_Page (FB) + 1) mod V_Pages = 0 then
754 Phys_Addr := Phys_Addr + GTT_Page_Size +
755 GTT_Address_Type (V_Pages) * Bytes_Per_Row;
756 end if;
757 end loop;
758 end;
Nico Huber2e87c0d2020-04-18 00:46:39 +0200759 -- Add another 128 dummy pages to work around buggy VT-d
760 for Idx in FB_Last_Page (FB) + 1 .. FB_Last_Page (FB) + 128 loop
761 Registers.Write_GTT (GTT_Rotation_Offset + Idx, Phys_Addr, True);
762 end loop;
Nico Huber9b479412017-08-27 11:55:56 +0200763 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200764 end Setup_Default_GTT;
765
766 ----------------------------------------------------------------------------
767
Nico Hubereedde882017-07-16 02:54:39 +0200768 use type HW.Word16;
769 subtype Stolen_Size_Range is Int64 range 0 .. 2 ** 33;
770
771 function GGMS_Gen4 (GGC : Word16) return Natural is
772 (Natural (Shift_Right (GGC, 8) and 16#07#));
773 function GTT_Size_Gen4 (GGC : Word16) return Natural is
774 (if GGMS_Gen4 (GGC) in 1 .. 3 then
775 (GGMS_Gen4 (GGC) + 1) * 2 ** 19 else 0);
776
777 function GMS_Gen4 (GGC : Word16) return Natural is
778 (Natural (Shift_Right (GGC, 4) and 16#0f#));
779 Valid_Stolen_Size_Gen4 : constant
780 array (Natural range 1 .. 13) of Stolen_Size_Range :=
781 (1, 4, 8, 16, 32, 48, 64, 128, 256, 96, 160, 224, 352);
782 function Stolen_Size_Gen4 (GGC : Word16) return Stolen_Size_Range is
783 (if GMS_Gen4 (GGC) in Valid_Stolen_Size_Gen4'Range then
Arthur Heymans5fd9a312017-09-12 12:45:18 +0200784 Valid_Stolen_Size_Gen4 (GMS_Gen4 (GGC)) * 2 ** 20 else 0);
Nico Hubereedde882017-07-16 02:54:39 +0200785
786 function GTT_Size_Gen6 (GGC : Word16) return Natural is
787 (Natural (Shift_Right (GGC, 8) and 16#03#) * 2 ** 20);
788
789 function Stolen_Size_Gen6 (GGC : Word16) return Stolen_Size_Range is
790 (Stolen_Size_Range (Shift_Right (GGC, 3) and 16#1f#) * 32 * 2 ** 20);
791
Nico Huberfe7985f2019-10-12 22:19:24 +0200792 function GGMS_Gen8 (GGC : Word16) return Natural is
793 (Natural (Shift_Right (GGC, 6) and 16#03#));
Nico Hubereedde882017-07-16 02:54:39 +0200794 function GTT_Size_Gen8 (GGC : Word16) return Natural is
Nico Huberfe7985f2019-10-12 22:19:24 +0200795 (if GGMS_Gen8 (GGC) /= 0 then
796 Natural (Shift_Left (Word32'(1), 20 + GGMS_Gen8 (GGC))) else 0);
Nico Hubereedde882017-07-16 02:54:39 +0200797
798 function GMS_Gen8 (GGC : Word16) return Stolen_Size_Range is
799 (Stolen_Size_Range (Shift_Right (GGC, 8) and 16#ff#));
800 function Stolen_Size_Gen8 (GGC : Word16) return Stolen_Size_Range is
801 (GMS_Gen8 (GGC) * 32 * 2 ** 20);
802
803 function Stolen_Size_Gen9 (GGC : Word16) return Stolen_Size_Range is
804 (if GMS_Gen8 (GGC) < 16#f0# then
805 Stolen_Size_Gen8 (GGC)
806 else
807 (GMS_Gen8 (GGC) - 16#f0# + 1) * 4 * 2 ** 20);
808
809 procedure Decode_Stolen
810 (GTT_Size : out Natural;
811 Stolen_Size : out Stolen_Size_Range)
812 with
813 Pre => Is_Initialized
814 is
Nico Huber63ec8362018-06-09 17:42:19 +0200815 GGC_Reg : constant PCI.Index :=
Nico Huber998ee2b2018-06-12 23:02:17 +0200816 (if Config.Gen_G45 or Config.CPU_Ironlake then 16#52# else 16#50#);
Nico Hubereedde882017-07-16 02:54:39 +0200817 GGC : Word16;
818 begin
819 Dev.Read16 (GGC, GGC_Reg);
Nico Huber998ee2b2018-06-12 23:02:17 +0200820 if Config.Gen_G45 or Config.CPU_Ironlake then
821 GTT_Size := GTT_Size_Gen4 (GGC);
822 Stolen_Size := Stolen_Size_Gen4 (GGC);
823 elsif Config.CPU_Sandybridge or Config.CPU_Ivybridge or Config.CPU_Haswell
824 then
825 GTT_Size := GTT_Size_Gen6 (GGC);
826 Stolen_Size := Stolen_Size_Gen6 (GGC);
827 elsif Config.CPU_Broadwell then
828 GTT_Size := GTT_Size_Gen8 (GGC);
829 Stolen_Size := Stolen_Size_Gen8 (GGC);
830 else
831 GTT_Size := GTT_Size_Gen8 (GGC);
832 Stolen_Size := Stolen_Size_Gen9 (GGC);
833 end if;
Nico Hubereedde882017-07-16 02:54:39 +0200834 end Decode_Stolen;
835
836 -- Additional runtime validation that FB fits stolen memory and aperture.
837 procedure Validate_FB (FB : Framebuffer_Type; Valid : out Boolean)
838 with
839 Pre => Is_Initialized,
840 Post => (if Valid then Valid_FB (FB))
841 is
Nico Huber2e87c0d2020-04-18 00:46:39 +0200842 GTT_Off : constant Natural :=
843 (if Rotation_90 (FB) then GTT_Rotation_Offset else 0);
844
Nico Hubereedde882017-07-16 02:54:39 +0200845 GTT_Size, Aperture_Size : Natural;
846 Stolen_Size : Stolen_Size_Range;
847 begin
848 Valid := Valid_FB (FB);
849
850 if Valid then
851 Decode_Stolen (GTT_Size, Stolen_Size);
852 Dev.Resource_Size (Aperture_Size, PCI.Res2);
853 Valid :=
Nico Huber2e87c0d2020-04-18 00:46:39 +0200854 FB_Last_Page (FB) + 128 + GTT_Off < GTT_Size / Config.GTT_PTE_Size
855 and
856 FB_Last_Page (FB) < Natural (Stolen_Size / GTT_Page_Size)
857 and
Nico Hubereedde882017-07-16 02:54:39 +0200858 FB_Last_Page (FB) < Aperture_Size / GTT_Page_Size;
Nico Huber34be6542017-12-13 09:26:24 +0100859 pragma Debug (not Valid, Debug.Put_Line
Nico Hubereedde882017-07-16 02:54:39 +0200860 ("Stolen memory too small to hold framebuffer."));
861 end if;
862 end Validate_FB;
863
Nico Huber5374c3a2017-07-15 21:48:06 +0200864 procedure Setup_Default_FB
865 (FB : in Framebuffer_Type;
866 Clear : in Boolean := True;
867 Success : out Boolean)
868 is
Nico Huber5374c3a2017-07-15 21:48:06 +0200869 GMA_Phys_Base_Mask : constant := 16#fff0_0000#;
870
871 Phys_Base : Word32;
872 begin
Nico Hubereedde882017-07-16 02:54:39 +0200873 Validate_FB (FB, Success);
Nico Huber5374c3a2017-07-15 21:48:06 +0200874
875 if Success then
Tim Wawrzynczak1b65b842022-09-09 10:23:06 -0600876 if Config.GMA_Base_Is_64bit then
877 Dev.Read32 (Phys_Base, Config.GMA_Phys_Base_Index + 4);
878 if Phys_Base /= 0 then
879 pragma Debug (Debug.Put_Line ("Cannot handle 64-bit DSM yet."));
880 Success := False;
881 return;
882 end if;
883 end if;
884
885 Dev.Read32 (Phys_Base, Config.GMA_Phys_Base_Index);
Nico Huber5374c3a2017-07-15 21:48:06 +0200886 Phys_Base := Phys_Base and GMA_Phys_Base_Mask;
887 Success := Phys_Base /= GMA_Phys_Base_Mask and Phys_Base /= 0;
888 pragma Debug (not Success, Debug.Put_Line
889 ("Failed to read stolen memory base."));
Nico Huber0164b022017-08-24 15:12:51 +0200890
891 if Success then
892 if FB.Tiling in XY_Tiling then
893 Registers.Add_Fence
894 (First_Page => FB_First_Page (FB),
895 Last_Page => FB_Last_Page (FB),
896 Tiling => FB.Tiling,
897 Pitch => FB_Pitch (FB.Stride, FB),
898 Success => Success);
899 end if;
900 pragma Debug (not Success, Debug.Put_Line
901 ("Tiled framebuffer but no fence regs available."));
902 end if;
903
Nico Huber5374c3a2017-07-15 21:48:06 +0200904 if Success then
905 Setup_Default_GTT (FB, Phys_Base);
906 end if;
907 end if;
908
909 if Success and then Clear then
910 declare
911 use type HW.Word64;
912 Linear_FB : Word64;
913 begin
Nico Huberc3f66f62017-07-16 21:39:54 +0200914 Map_Linear_FB (Linear_FB, FB);
Nico Huber5374c3a2017-07-15 21:48:06 +0200915 if Linear_FB /= 0 then
Nico Huberc3f66f62017-07-16 21:39:54 +0200916 Framebuffer_Filler.Fill (Linear_FB, FB);
Nico Huber5374c3a2017-07-15 21:48:06 +0200917 end if;
Nico Huber5374c3a2017-07-15 21:48:06 +0200918 end;
919 end if;
920 end Setup_Default_FB;
921
Nico Huberc3f66f62017-07-16 21:39:54 +0200922 procedure Map_Linear_FB (Linear_FB : out Word64; FB : in Framebuffer_Type)
923 is
924 use type HW.Word64;
925
926 Valid : Boolean;
927 begin
928 Linear_FB := 0;
929
930 if Linear_FB_Base = 0 then
931 Dev.Map (Linear_FB_Base, PCI.Res2);
932 pragma Debug
933 (Linear_FB_Base = 0, Debug.Put_Line ("Failed to map resource2."));
934 end if;
935
936 if Linear_FB_Base /= 0 then
937 Validate_FB (FB, Valid);
938 if Valid then
Nico Huber34be6542017-12-13 09:26:24 +0100939 Linear_FB := Linear_FB_Base + Word64 (Phys_Offset (FB));
Nico Huberc3f66f62017-07-16 21:39:54 +0200940 end if;
941 end if;
942 end Map_Linear_FB;
943
Nico Huber5374c3a2017-07-15 21:48:06 +0200944 ----------------------------------------------------------------------------
945
Nico Huber99f10f32016-11-20 00:34:05 +0100946 procedure Dump_Configs (Configs : Pipe_Configs)
Nico Huber83693c82016-10-08 22:17:55 +0200947 is
948 subtype Pipe_Name is String (1 .. 9);
Nico Huber99f10f32016-11-20 00:34:05 +0100949 type Pipe_Name_Array is array (Pipe_Index) of Pipe_Name;
Nico Huber83693c82016-10-08 22:17:55 +0200950 Pipe_Names : constant Pipe_Name_Array :=
951 (Primary => "Primary ",
952 Secondary => "Secondary",
953 Tertiary => "Tertiary ");
Nico Huber5ef4d602017-12-13 13:56:47 +0100954
955 subtype Tiling_Name is String (1 .. 7);
956 type Tiling_Name_Array is array (Tiling_Type) of Tiling_Name;
957 Tilings : constant Tiling_Name_Array :=
958 (Linear => "Linear ",
959 X_Tiled => "X_Tiled",
960 Y_Tiled => "Y_Tiled");
961
962 subtype Rotation_Name is String (1 .. 11);
963 type Rotation_Name_Array is array (Rotation_Type) of Rotation_Name;
964 Rotations : constant Rotation_Name_Array :=
965 (No_Rotation => "No_Rotation",
966 Rotated_90 => "Rotated_90 ",
967 Rotated_180 => "Rotated_180",
968 Rotated_270 => "Rotated_270");
Nico Huber83693c82016-10-08 22:17:55 +0200969 begin
970 Debug.New_Line;
Paul Menzelb83107c2017-05-04 09:02:33 +0200971 Debug.Put_Line ("CONFIG =>");
Nico Huber99f10f32016-11-20 00:34:05 +0100972 for Pipe in Pipe_Index loop
973 if Pipe = Pipe_Index'First then
Nico Huber83693c82016-10-08 22:17:55 +0200974 Debug.Put (" (");
975 else
976 Debug.Put (" ");
977 end if;
978 Debug.Put_Line (Pipe_Names (Pipe) & " =>");
979 Debug.Put_Line
980 (" (Port => " & Port_Names (Configs (Pipe).Port) & ",");
981 Debug.Put_Line (" Framebuffer =>");
Nico Huber5ef4d602017-12-13 13:56:47 +0100982 Debug.Put (" (Width => ");
Nico Huber83693c82016-10-08 22:17:55 +0200983 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Width);
984 Debug.Put_Line (",");
Nico Huber5ef4d602017-12-13 13:56:47 +0100985 Debug.Put (" Height => ");
Nico Huber83693c82016-10-08 22:17:55 +0200986 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Height);
987 Debug.Put_Line (",");
Nico Huber5ef4d602017-12-13 13:56:47 +0100988 Debug.Put (" Start_X => ");
989 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Start_X);
990 Debug.Put_Line (",");
991 Debug.Put (" Start_Y => ");
992 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Start_Y);
993 Debug.Put_Line (",");
994 Debug.Put (" Stride => ");
Nico Huber83693c82016-10-08 22:17:55 +0200995 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Stride);
996 Debug.Put_Line (",");
Nico Huber5ef4d602017-12-13 13:56:47 +0100997 Debug.Put (" V_Stride => ");
998 Debug.Put_Int32 (Configs (Pipe).Framebuffer.V_Stride);
999 Debug.Put_Line (",");
1000 Debug.Put (" Tiling => ");
1001 Debug.Put_Line (Tilings (Configs (Pipe).Framebuffer.Tiling) & ",");
1002 Debug.Put (" Rotation => ");
1003 Debug.Put_Line (Rotations (Configs (Pipe).Framebuffer.Rotation) & ",");
Nico Huber83693c82016-10-08 22:17:55 +02001004 Debug.Put (" Offset => ");
1005 Debug.Put_Word32 (Configs (Pipe).Framebuffer.Offset);
1006 Debug.Put_Line (",");
1007 Debug.Put (" BPC => ");
1008 Debug.Put_Int64 (Configs (Pipe).Framebuffer.BPC);
1009 Debug.Put_Line ("),");
1010 Debug.Put_Line (" Mode =>");
1011 Debug.Put (" (Dotclock => ");
1012 Debug.Put_Int64 (Configs (Pipe).Mode.Dotclock);
1013 Debug.Put_Line (",");
1014 Debug.Put (" H_Visible => ");
Nico Huberc5c767a2018-06-03 01:09:04 +02001015 Debug.Put_Int32 (Configs (Pipe).Mode.H_Visible);
Nico Huber83693c82016-10-08 22:17:55 +02001016 Debug.Put_Line (",");
1017 Debug.Put (" H_Sync_Begin => ");
Nico Huberc5c767a2018-06-03 01:09:04 +02001018 Debug.Put_Int32 (Configs (Pipe).Mode.H_Sync_Begin);
Nico Huber83693c82016-10-08 22:17:55 +02001019 Debug.Put_Line (",");
1020 Debug.Put (" H_Sync_End => ");
Nico Huberc5c767a2018-06-03 01:09:04 +02001021 Debug.Put_Int32 (Configs (Pipe).Mode.H_Sync_End);
Nico Huber83693c82016-10-08 22:17:55 +02001022 Debug.Put_Line (",");
1023 Debug.Put (" H_Total => ");
Nico Huberc5c767a2018-06-03 01:09:04 +02001024 Debug.Put_Int32 (Configs (Pipe).Mode.H_Total);
Nico Huber83693c82016-10-08 22:17:55 +02001025 Debug.Put_Line (",");
1026 Debug.Put (" V_Visible => ");
Nico Huberc5c767a2018-06-03 01:09:04 +02001027 Debug.Put_Int32 (Configs (Pipe).Mode.V_Visible);
Nico Huber83693c82016-10-08 22:17:55 +02001028 Debug.Put_Line (",");
1029 Debug.Put (" V_Sync_Begin => ");
Nico Huberc5c767a2018-06-03 01:09:04 +02001030 Debug.Put_Int32 (Configs (Pipe).Mode.V_Sync_Begin);
Nico Huber83693c82016-10-08 22:17:55 +02001031 Debug.Put_Line (",");
1032 Debug.Put (" V_Sync_End => ");
Nico Huberc5c767a2018-06-03 01:09:04 +02001033 Debug.Put_Int32 (Configs (Pipe).Mode.V_Sync_End);
Nico Huber83693c82016-10-08 22:17:55 +02001034 Debug.Put_Line (",");
1035 Debug.Put (" V_Total => ");
Nico Huberc5c767a2018-06-03 01:09:04 +02001036 Debug.Put_Int32 (Configs (Pipe).Mode.V_Total);
Nico Huber83693c82016-10-08 22:17:55 +02001037 Debug.Put_Line (",");
1038 Debug.Put_Line (" H_Sync_Active_High => " &
1039 (if Configs (Pipe).Mode.H_Sync_Active_High
1040 then "True,"
1041 else "False,"));
1042 Debug.Put_Line (" V_Sync_Active_High => " &
1043 (if Configs (Pipe).Mode.V_Sync_Active_High
1044 then "True,"
1045 else "False,"));
1046 Debug.Put (" BPC => ");
1047 Debug.Put_Int64 (Configs (Pipe).Mode.BPC);
Nico Huber99f10f32016-11-20 00:34:05 +01001048 if Pipe /= Pipe_Index'Last then
Nico Huber83693c82016-10-08 22:17:55 +02001049 Debug.Put_Line (")),");
1050 else
1051 Debug.Put_Line (")));");
1052 end if;
1053 end loop;
1054 end Dump_Configs;
1055
Nico Huberc5c66ec2019-09-28 23:59:45 +02001056 ----------------------------------------------------------------------------
1057
1058 procedure PCI_Read16 (Value : out Word16; Offset : HW.PCI.Index) is
1059 begin
1060 Dev.Read16 (Value, Offset);
1061 end PCI_Read16;
1062
Nico Huber83693c82016-10-08 22:17:55 +02001063end HW.GFX.GMA;