blob: e26e75197f21ac2ee3b617c223c2d86399232c55 [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 +020036
37package body HW.GFX.GMA
38 with Refined_State =>
39 (State =>
Nico Hubere317e9c2019-09-29 03:03:18 +020040 (Config.Variable,
41 PCI_Usable,
Nico Huberc5c66ec2019-09-28 23:59:45 +020042 Dev.Address_State,
Nico Huber2b6f6992017-07-09 18:11:34 +020043 Registers.Address_State,
Nico Huber312433c2019-09-28 03:15:48 +020044 PCode.Mailbox_Ready,
Nico Huber83693c82016-10-08 22:17:55 +020045 PLLs.State, Panel.Panel_State,
Nico Huber1a712d32017-01-09 15:11:04 +010046 Cur_Configs, Allocated_PLLs,
Nico Huberc3f66f62017-07-16 21:39:54 +020047 HPD_Delay, Wait_For_HPD,
48 Linear_FB_Base),
Nico Huber83693c82016-10-08 22:17:55 +020049 Init_State => Initialized,
Nico Huber83693c82016-10-08 22:17:55 +020050 Device_State =>
Nico Huber2b6f6992017-07-09 18:11:34 +020051 (Dev.PCI_State, Registers.Register_State, Registers.GTT_State))
Nico Huber83693c82016-10-08 22:17:55 +020052is
Nico Huber2b6f6992017-07-09 18:11:34 +020053 pragma Disable_Atomic_Synchronization;
Nico Huber83693c82016-10-08 22:17:55 +020054
Nico Huberad096092024-07-02 18:45:44 +020055 subtype Port_Name is String (1 .. 8);
Nico Huber83693c82016-10-08 22:17:55 +020056 type Port_Name_Array is array (Port_Type) of Port_Name;
57 Port_Names : constant Port_Name_Array :=
Nico Huberad096092024-07-02 18:45:44 +020058 (Disabled => "Disabled",
59 LVDS => "LVDS ",
60 eDP => "eDP ",
61 DP1 => "DP1 ",
62 DP2 => "DP2 ",
63 DP3 => "DP3 ",
64 DP_TC1 => "DP_TC1 ",
65 DP_TC2 => "DP_TC2 ",
66 DP_TC3 => "DP_TC3 ",
67 DP_TC4 => "DP_TC4 ",
68 HDMI1 => "HDMI1 ",
69 HDMI2 => "HDMI2 ",
70 HDMI3 => "HDMI3 ",
71 HDMI_TC1 => "HDMI_TC1",
72 HDMI_TC2 => "HDMI_TC2",
73 HDMI_TC3 => "HDMI_TC3",
74 HDMI_TC4 => "HDMI_TC4",
75 Analog => "Analog ",
76 USBC1 => "USBC1 ",
77 USBC2 => "USBC2 ",
78 USBC3 => "USBC3 ",
79 USBC4 => "USBC4 ");
Nico Huber83693c82016-10-08 22:17:55 +020080
Nico Huber2b6f6992017-07-09 18:11:34 +020081 package Dev is new HW.PCI.Dev (PCI.Address'(0, 2, 0));
82
Nico Huber83693c82016-10-08 22:17:55 +020083 package Display_Controller renames Pipe_Setup;
84
Nico Huber99f10f32016-11-20 00:34:05 +010085 type PLLs_Type is array (Pipe_Index) of PLLs.T;
Nico Huber83693c82016-10-08 22:17:55 +020086
Nico Huber83693c82016-10-08 22:17:55 +020087 type HPD_Type is array (Port_Type) of Boolean;
Nico Huber3be61d42017-01-09 13:58:18 +010088 type HPD_Delay_Type is array (Active_Port_Type) of Time.T;
Nico Huber83693c82016-10-08 22:17:55 +020089
Nico Huber83693c82016-10-08 22:17:55 +020090 Allocated_PLLs : PLLs_Type;
Nico Huber83693c82016-10-08 22:17:55 +020091 HPD_Delay : HPD_Delay_Type;
92 Wait_For_HPD : HPD_Type;
93 Initialized : Boolean := False;
94
Nico Huberc3f66f62017-07-16 21:39:54 +020095 Linear_FB_Base : Word64;
96
Nico Huber83693c82016-10-08 22:17:55 +020097 ----------------------------------------------------------------------------
98
Tim Wawrzynczakfc49b602022-09-09 10:29:24 -060099 ICP_RAWCLK_NUM : constant := 1 * 2 ** 11;
100
101 function PCH_RAWCLK_FREQ_MASK return Word32 is
102 Mask : Word32;
103 begin
104 if Config.Need_Rawclk_Numerator then
105 Mask := 16#ffff_ffff#;
106 elsif Config.Has_Fractional_RawClk then
107 Mask := 16#3fff# * 2 ** 16;
108 else
109 Mask := 16#3ff# * 2 ** 0;
110 end if;
111 return Mask;
112 end PCH_RAWCLK_FREQ_MASK;
Nico Huberf54d0962016-10-20 14:17:18 +0200113
114 function PCH_RAWCLK_FREQ (Freq : Frequency_Type) return Word32
115 is
116 begin
Nico Huberdde06302020-12-20 02:18:30 +0100117 if Config.Has_Fractional_RawClk then
118 declare
119 Fraction_K : constant Int64 := Freq / 1_000 mod 1_000;
120 Freq32 : Word32 := Shift_Left (Word32 (Freq / 1_000_000), 16);
121 begin
122 if Fraction_K /= 0 then
123 Freq32 := Freq32 or Shift_Left
124 (Word32 (Div_Round_Closest (1_000, Fraction_K) - 1), 26);
125 end if;
Tim Wawrzynczakfc49b602022-09-09 10:29:24 -0600126
127 if Config.Need_Rawclk_Numerator then
128 Freq32 := Freq32 or ICP_RAWCLK_NUM;
129 end if;
Nico Huberdde06302020-12-20 02:18:30 +0100130 return Freq32;
131 end;
132 else
133 return Word32 (Freq / 1_000_000);
134 end if;
Nico Huberf54d0962016-10-20 14:17:18 +0200135 end PCH_RAWCLK_FREQ;
136
137 ----------------------------------------------------------------------------
138
Nico Huber43370ba2017-01-09 15:26:19 +0100139 procedure Enable_Output
140 (Pipe : in Pipe_Index;
141 Pipe_Cfg : in Pipe_Config;
142 Success : out Boolean)
Nico Huber8a5a3b52018-06-04 14:42:13 +0200143 with
Nico Huber9a4c4c32019-09-16 22:05:11 +0200144 Pre =>
145 Pipe_Cfg.Port in Active_Port_Type and
146 Config_Helpers.Valid_FB (Pipe_Cfg.Framebuffer, Pipe_Cfg.Mode)
Nico Huber43370ba2017-01-09 15:26:19 +0100147 is
148 Port_Cfg : Port_Config;
149 begin
Nico Huber3be61d42017-01-09 13:58:18 +0100150 pragma Debug (Debug.New_Line);
151 pragma Debug (Debug.Put_Line
152 ("Trying to enable port " & Port_Names (Pipe_Cfg.Port)));
153
Nico Huber43370ba2017-01-09 15:26:19 +0100154 Config_Helpers.Fill_Port_Config
155 (Port_Cfg, Pipe, Pipe_Cfg.Port, Pipe_Cfg.Mode, Success);
156
157 if Success then
Nico Huber41e86742024-07-17 17:10:28 +0200158 Power_And_Clocks.Power_Up (Pipe_Cfg.Port, Success);
159 end if;
160
161 if Success then
Nico Huber43370ba2017-01-09 15:26:19 +0100162 Connector_Info.Preferred_Link_Setting (Port_Cfg, Success);
163 end if;
164
165 -- loop over all possible DP-lane configurations
166 -- (non-DP ports use a single fake configuration)
167 while Success loop
168 pragma Loop_Invariant
169 (Pipe_Cfg.Port in Active_Port_Type and
170 Port_Cfg.Mode = Port_Cfg.Mode'Loop_Entry);
171
172 PLLs.Alloc
173 (Port_Cfg => Port_Cfg,
174 PLL => Allocated_PLLs (Pipe),
175 Success => Success);
176
177 if Success then
178 -- try each DP-lane configuration twice
179 for Try in 1 .. 2 loop
180 pragma Loop_Invariant
181 (Pipe_Cfg.Port in Active_Port_Type);
182
Nico Huber4798c662017-01-11 12:44:48 +0100183 -- Clear pending hot-plug events before every try
184 Port_Detect.Clear_Hotplug_Detect (Pipe_Cfg.Port);
185
Nico Huber43370ba2017-01-09 15:26:19 +0100186 Connectors.Pre_On
187 (Pipe => Pipe,
188 Port_Cfg => Port_Cfg,
189 PLL_Hint => PLLs.Register_Value (Allocated_PLLs (Pipe)),
190 Success => Success);
191
192 if Success then
193 Display_Controller.On
194 (Pipe => Pipe,
195 Port_Cfg => Port_Cfg,
Nico Huber4dc4c612018-01-10 15:55:09 +0100196 Framebuffer => Pipe_Cfg.Framebuffer,
197 Cursor => Pipe_Cfg.Cursor);
Nico Huber43370ba2017-01-09 15:26:19 +0100198
199 Connectors.Post_On
Arthur Heymans60d0e5f2018-03-28 17:08:27 +0200200 (Pipe => Pipe,
201 Port_Cfg => Port_Cfg,
Nico Huber43370ba2017-01-09 15:26:19 +0100202 PLL_Hint => PLLs.Register_Value (Allocated_PLLs (Pipe)),
203 Success => Success);
204
205 if not Success then
206 Display_Controller.Off (Pipe);
207 Connectors.Post_Off (Port_Cfg);
208 end if;
209 end if;
210
211 exit when Success;
212 end loop;
213 exit when Success; -- connection established => stop loop
214
215 -- connection failed
216 PLLs.Free (Allocated_PLLs (Pipe));
217 end if;
218
219 Connector_Info.Next_Link_Setting (Port_Cfg, Success);
220 end loop;
221
222 if Success then
223 pragma Debug (Debug.Put_Line
224 ("Enabled port " & Port_Names (Pipe_Cfg.Port)));
225 else
226 Wait_For_HPD (Pipe_Cfg.Port) := True;
Nico Huber2bbd6e72020-01-07 18:22:59 +0100227 Panel.Off (Config_Helpers.To_Panel (Pipe_Cfg.Port));
Nico Huber43370ba2017-01-09 15:26:19 +0100228 end if;
229 end Enable_Output;
230
Nico Huber3be61d42017-01-09 13:58:18 +0100231 procedure Disable_Output (Pipe : Pipe_Index; Pipe_Cfg : Pipe_Config)
232 is
233 Port_Cfg : Port_Config;
234 Success : Boolean;
235 begin
236 Config_Helpers.Fill_Port_Config
237 (Port_Cfg, Pipe, Pipe_Cfg.Port, Pipe_Cfg.Mode, Success);
238 if Success then
239 pragma Debug (Debug.New_Line);
240 pragma Debug (Debug.Put_Line
241 ("Disabling port " & Port_Names (Pipe_Cfg.Port)));
242 pragma Debug (Debug.New_Line);
243
Jeremy Compostellafe80fbb2023-01-11 14:05:42 -0700244 if Pipe_Cfg.Framebuffer.Offset = VGA_PLANE_FRAMEBUFFER_OFFSET then
245 Display_Controller.Legacy_VGA_Off;
246 end if;
247
Nico Huber3be61d42017-01-09 13:58:18 +0100248 Connectors.Pre_Off (Port_Cfg);
249 Display_Controller.Off (Pipe);
250 Connectors.Post_Off (Port_Cfg);
251
252 PLLs.Free (Allocated_PLLs (Pipe));
253 end if;
254 end Disable_Output;
255
Nico Huber99f10f32016-11-20 00:34:05 +0100256 procedure Update_Outputs (Configs : Pipe_Configs)
Nico Huber83693c82016-10-08 22:17:55 +0200257 is
Nico Huber3be61d42017-01-09 13:58:18 +0100258 procedure Check_HPD (Port : in Active_Port_Type; Detected : out Boolean)
259 is
260 HPD_Delay_Over : constant Boolean := Time.Timed_Out (HPD_Delay (Port));
261 begin
262 if HPD_Delay_Over then
263 Port_Detect.Hotplug_Detect (Port, Detected);
264 HPD_Delay (Port) := Time.MS_From_Now (333);
265 else
266 Detected := False;
267 end if;
268 end Check_HPD;
Nico Huberb56b9c52017-01-11 15:12:23 +0100269
Nico Huber9a4c4c32019-09-16 22:05:11 +0200270 Scaler_Reservation : Display_Controller.Scaler_Reservation :=
271 Display_Controller.Null_Scaler_Reservation;
Nico Huber564103f2017-01-11 15:33:07 +0100272
Nico Huber9a4c4c32019-09-16 22:05:11 +0200273 Update_Power : Boolean := False;
Nico Huberb0bbdbc2019-09-27 22:32:21 +0200274 Update_CDClk : Boolean;
Nico Huber9a4c4c32019-09-16 22:05:11 +0200275 Old_Configs,
276 New_Configs : Pipe_Configs;
Nico Huber3d06de82018-05-29 01:35:04 +0200277
278 function Full_Update (Cur_Config, New_Config : Pipe_Config) return Boolean
279 is
280 begin
281 return
Nico Huber958c5642018-06-02 16:59:31 +0200282 Cur_Config.Port /= New_Config.Port
283 or else
284 Cur_Config.Mode /= New_Config.Mode
285 or else
Nico Huber3d06de82018-05-29 01:35:04 +0200286 (Config.Use_PDW_For_EDP_Scaling and then
Nico Huber8beafd72020-01-07 14:59:44 +0100287 (Cur_Config.Port = eDP and
Nico Huber958c5642018-06-02 16:59:31 +0200288 Requires_Scaling (Cur_Config) /= Requires_Scaling (New_Config)))
289 or else
290 (Config.Has_GMCH_PFIT_CONTROL and then
291 (Requires_Scaling (Cur_Config) /= Requires_Scaling (New_Config) or
292 Scaling_Type (Cur_Config) /= Scaling_Type (New_Config)));
Nico Huber3d06de82018-05-29 01:35:04 +0200293 end Full_Update;
Nico Huber83693c82016-10-08 22:17:55 +0200294 begin
295 Old_Configs := Cur_Configs;
Nico Huber9a4c4c32019-09-16 22:05:11 +0200296 New_Configs := Configs;
297
298 -- validate new configs, filter invalid configs and those waiting for HPD
299 for Pipe in Pipe_Index loop
300 declare
301 Success : Boolean := True;
302 Cur_Config : Pipe_Config renames Cur_Configs (Pipe);
303 New_Config : Pipe_Config renames New_Configs (Pipe);
304 begin
305 if New_Config.Port /= Disabled then
306 if Wait_For_HPD (New_Config.Port) then
307 Check_HPD (New_Config.Port, Success);
308 Wait_For_HPD (New_Config.Port) := not Success;
309 end if;
310
311 Success := Success and then
312 Config_Helpers.Validate_Config
313 (New_Config.Framebuffer, New_Config.Mode, Pipe);
314
315 if Success and then Requires_Scaling (New_Config) then
316 Display_Controller.Reserve_Scaler
317 (Success, Scaler_Reservation, Pipe);
318 end if;
319
320 if not Success then
321 New_Config.Port := Disabled;
322 end if;
323 end if;
324 end;
325 pragma Loop_Invariant
326 (for all P in Pipe_Index'First .. Pipe =>
327 New_Configs (P).Port = Disabled or
328 Config_Helpers.Valid_FB
329 (New_Configs (P).Framebuffer, New_Configs (P).Mode));
330 end loop;
Nico Huber83693c82016-10-08 22:17:55 +0200331
Nico Huberb0bbdbc2019-09-27 22:32:21 +0200332 -- limit dotclocks to maximum CDClk, if we are about
333 -- to switch CDClk, all pipes have to be disabled
334 Power_And_Clocks.Limit_Dotclocks (New_Configs, Update_CDClk);
335
Nico Huberb56b9c52017-01-11 15:12:23 +0100336 -- disable all pipes that changed or had a hot-plug event
337 for Pipe in Pipe_Index loop
338 declare
339 Unplug_Detected : Boolean;
340 Cur_Config : Pipe_Config renames Cur_Configs (Pipe);
Nico Huber9a4c4c32019-09-16 22:05:11 +0200341 New_Config : Pipe_Config renames New_Configs (Pipe);
Nico Huberb56b9c52017-01-11 15:12:23 +0100342 begin
343 if Cur_Config.Port /= Disabled then
344 Check_HPD (Cur_Config.Port, Unplug_Detected);
Nico Huber83693c82016-10-08 22:17:55 +0200345
Nico Huberb0bbdbc2019-09-27 22:32:21 +0200346 if Update_CDClk or
347 Unplug_Detected or
348 Full_Update (Cur_Config, New_Config)
349 then
Nico Huberb56b9c52017-01-11 15:12:23 +0100350 Disable_Output (Pipe, Cur_Config);
351 Cur_Config.Port := Disabled;
Nico Huber9a4c4c32019-09-16 22:05:11 +0200352 Update_Power := True;
Nico Huberb56b9c52017-01-11 15:12:23 +0100353 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200354 end if;
Nico Huberb56b9c52017-01-11 15:12:23 +0100355 end;
356 end loop;
Nico Huber83693c82016-10-08 22:17:55 +0200357
Nico Huberb0bbdbc2019-09-27 22:32:21 +0200358 -- switch CDClk if necessary and possible, limit dotclocks accordingly
359 if Update_CDClk then
360 Power_And_Clocks.Update_CDClk (New_Configs);
361 end if;
362
Nico Huberb56b9c52017-01-11 15:12:23 +0100363 -- enable all pipes that changed and should be active
364 for Pipe in Pipe_Index loop
365 declare
366 Success : Boolean;
367 Cur_Config : Pipe_Config renames Cur_Configs (Pipe);
Nico Huber9a4c4c32019-09-16 22:05:11 +0200368 New_Config : Pipe_Config renames New_Configs (Pipe);
Nico Huberb56b9c52017-01-11 15:12:23 +0100369 begin
Nico Huber9a4c4c32019-09-16 22:05:11 +0200370 -- full update
Nico Huber3d06de82018-05-29 01:35:04 +0200371 if New_Config.Port /= Disabled and
372 Full_Update (Cur_Config, New_Config)
Nico Huberb56b9c52017-01-11 15:12:23 +0100373 then
Nico Huber9a4c4c32019-09-16 22:05:11 +0200374 Power_And_Clocks.Power_Up (Old_Configs, New_Configs);
375 Update_Power := True;
Nico Huberc7a4fee2016-11-03 18:18:03 +0100376
Nico Huber9a4c4c32019-09-16 22:05:11 +0200377 Enable_Output (Pipe, New_Config, Success);
Nico Huber83693c82016-10-08 22:17:55 +0200378 if Success then
Nico Huberb56b9c52017-01-11 15:12:23 +0100379 Cur_Config := New_Config;
Nico Huber83693c82016-10-08 22:17:55 +0200380 end if;
Nico Huber3be61d42017-01-09 13:58:18 +0100381
Nico Huberb56b9c52017-01-11 15:12:23 +0100382 -- update framebuffer offset only
383 elsif New_Config.Port /= Disabled and
Nico Huberf361ec82018-06-02 18:01:45 +0200384 Cur_Config.Framebuffer /= New_Config.Framebuffer
Nico Huberb56b9c52017-01-11 15:12:23 +0100385 then
Nico Huber9a4c4c32019-09-16 22:05:11 +0200386 Display_Controller.Setup_FB
387 (Pipe, New_Config.Mode, New_Config.Framebuffer);
388 Display_Controller.Update_Cursor
389 (Pipe, New_Config.Framebuffer, New_Config.Cursor);
390 Cur_Config := New_Config;
Nico Huberb56b9c52017-01-11 15:12:23 +0100391 end if;
392 end;
Nico Huber83693c82016-10-08 22:17:55 +0200393 end loop;
394
Nico Huber9a4c4c32019-09-16 22:05:11 +0200395 if Update_Power then
396 Power_And_Clocks.Power_Down (Old_Configs, New_Configs, Cur_Configs);
Nico Huber83693c82016-10-08 22:17:55 +0200397 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200398 end Update_Outputs;
399
400 ----------------------------------------------------------------------------
401
Nico Huber15ffc4f2018-01-11 14:44:43 +0100402 procedure Update_Cursor (Pipe : Pipe_Index; Cursor : Cursor_Type)
403 is
404 begin
405 Cur_Configs (Pipe).Cursor := Cursor;
406 Display_Controller.Update_Cursor
407 (Pipe, Cur_Configs (Pipe).Framebuffer, Cur_Configs (Pipe).Cursor);
408 end Update_Cursor;
409
410 procedure Place_Cursor
411 (Pipe : Pipe_Index;
412 X : Cursor_Pos;
413 Y : Cursor_Pos)
414 is
415 begin
416 Cur_Configs (Pipe).Cursor.Center_X := X;
417 Cur_Configs (Pipe).Cursor.Center_Y := Y;
418 Display_Controller.Place_Cursor
419 (Pipe, Cur_Configs (Pipe).Framebuffer, Cur_Configs (Pipe).Cursor);
420 end Place_Cursor;
421
422 procedure Move_Cursor
423 (Pipe : Pipe_Index;
424 X : Cursor_Pos;
425 Y : Cursor_Pos)
426 is
427 function Cap_Add (A, B : Cursor_Pos) return Cursor_Pos is
428 (if A + B < 0
429 then Int32'Max (Cursor_Pos'First, A + B)
430 else Int32'Min (Cursor_Pos'Last, A + B));
431 begin
432 Place_Cursor
433 (Pipe => Pipe,
434 X => Cap_Add (Cur_Configs (Pipe).Cursor.Center_X, X),
435 Y => Cap_Add (Cur_Configs (Pipe).Cursor.Center_Y, Y));
436 end Move_Cursor;
437
438 ----------------------------------------------------------------------------
439
Nico Huberbc0588e2020-07-21 12:17:19 +0200440 procedure Backlight_On (Port : Active_Port_Type)
441 with
442 Refined_Global => (In_Out => Registers.Register_State)
443 is
444 begin
445 Panel.Backlight_On (Config_Helpers.To_Panel (Port));
446 end Backlight_On;
447
448 procedure Backlight_Off (Port : Active_Port_Type)
449 with
450 Refined_Global => (In_Out => Registers.Register_State)
451 is
452 begin
453 Panel.Backlight_Off (Config_Helpers.To_Panel (Port));
454 end Backlight_Off;
455
456 procedure Set_Brightness (Port : Active_Port_Type; Level : Word32)
457 with
458 Refined_Global => (In_Out => Registers.Register_State)
459 is
460 begin
461 Panel.Set_Backlight (Config_Helpers.To_Panel (Port), Level);
462 end Set_Brightness;
463
464 procedure Get_Max_Brightness (Port : Active_Port_Type; Level : out Word32)
465 with
466 Refined_Global => (In_Out => Registers.Register_State)
467 is
468 begin
469 Panel.Get_Max_Backlight (Config_Helpers.To_Panel (Port), Level);
470 end Get_Max_Brightness;
471
472 ----------------------------------------------------------------------------
473
Nico Huber793f4f82022-09-04 14:24:00 +0000474 pragma Warnings
475 (GNATprove, Off, """Registers.GTT_State"" * is not modified*",
476 Reason => "The whole, abstract Device_State is modified in certain configurations.");
477 pragma Warnings
478 (GNATprove, Off, "no check message justified*", Reason => "see below");
Nico Huber83693c82016-10-08 22:17:55 +0200479 procedure Initialize
Nico Huber2b6f6992017-07-09 18:11:34 +0200480 (Write_Delay : in Word64 := 0;
Nico Huber793a8d42016-11-21 18:57:03 +0100481 Clean_State : in Boolean := False;
Nico Huber83693c82016-10-08 22:17:55 +0200482 Success : out Boolean)
483 with
484 Refined_Global =>
Nico Huber27088aa2018-06-10 13:28:05 +0200485 (Input => (Time.State),
Nico Huber793f4f82022-09-04 14:24:00 +0000486 In_Out =>
487 (Dev.PCI_State, Port_IO.State,
488 Registers.Register_State, Registers.GTT_State),
Nico Huber83693c82016-10-08 22:17:55 +0200489 Output =>
Nico Huberc5c66ec2019-09-28 23:59:45 +0200490 (PCI_Usable,
491 Config.Variable,
Nico Huber27088aa2018-06-10 13:28:05 +0200492 Dev.Address_State,
Nico Huber2b6f6992017-07-09 18:11:34 +0200493 Registers.Address_State,
Nico Huber312433c2019-09-28 03:15:48 +0200494 PCode.Mailbox_Ready,
Nico Huber83693c82016-10-08 22:17:55 +0200495 PLLs.State, Panel.Panel_State,
Nico Huber1a712d32017-01-09 15:11:04 +0100496 Cur_Configs, Allocated_PLLs,
Nico Huberc3f66f62017-07-16 21:39:54 +0200497 HPD_Delay, Wait_For_HPD,
498 Linear_FB_Base, Initialized))
Nico Huber83693c82016-10-08 22:17:55 +0200499 is
500 use type HW.Word64;
501
Nico Huber0b2329a2018-06-09 21:14:27 +0200502 function MMIO_GTT_Offset return Natural is
Arthur Heymans960e2392026-03-03 19:45:24 +0100503 (if Config.Has_I945_GTT_BAR
504 then 0 -- i945: GTT is on separate BAR3, not within BAR0
505 elsif Config.Has_64bit_GTT
Nico Huber0b2329a2018-06-09 21:14:27 +0200506 then Registers.MMIO_GTT_64_Offset
507 else Registers.MMIO_GTT_32_Offset);
Nico Huber2b6f6992017-07-09 18:11:34 +0200508 PCI_MMIO_Base, PCI_GTT_Base : Word64;
509
Nico Huber83693c82016-10-08 22:17:55 +0200510 Now : constant Time.T := Time.Now;
511
512 procedure Check_Platform (Success : out Boolean)
513 is
514 Audio_VID_DID : Word32;
515 begin
Arthur Heymans960e2392026-03-03 19:45:24 +0100516 if Config.Gen_I945 then
517 -- i945 has no integrated audio DID to verify
518 Success := True;
519 return;
520 end if;
Nico Huber6621a142018-06-07 23:56:54 +0200521 case Config.Gen is
Arthur Heymans960e2392026-03-03 19:45:24 +0100522 when I945 =>
523 Audio_VID_DID := 0; -- unreachable due to early return
Arthur Heymans73ea0322018-03-28 17:17:07 +0200524 when G45 =>
525 Registers.Read (Registers.G4X_AUD_VID_DID, Audio_VID_DID);
Nico Huber6621a142018-06-07 23:56:54 +0200526 when Ironlake =>
527 Registers.Read (Registers.PCH_AUD_VID_DID, Audio_VID_DID);
Tim Wawrzynczak605660b2022-06-08 12:48:19 -0600528 when Haswell .. Tigerlake =>
Nico Huber83693c82016-10-08 22:17:55 +0200529 Registers.Read (Registers.AUD_VID_DID, Audio_VID_DID);
Nico Huber83693c82016-10-08 22:17:55 +0200530 end case;
531 Success :=
Nico Huber998ee2b2018-06-12 23:02:17 +0200532 ((Config.Gen_Broxton and Audio_VID_DID = 16#8086_280a#) or
Nico Huber88badbe2018-09-27 16:36:47 +0200533 (Config.CPU_Kabylake and Audio_VID_DID = 16#8086_280b#) or
534 (Config.CPU_Skylake and Audio_VID_DID = 16#8086_2809#) or
Nico Huber998ee2b2018-06-12 23:02:17 +0200535 (Config.CPU_Broadwell and Audio_VID_DID = 16#8086_2808#) or
536 (Config.CPU_Haswell and Audio_VID_DID = 16#8086_2807#) or
537 ((Config.CPU_Ivybridge or
538 Config.CPU_Sandybridge) and (Audio_VID_DID = 16#8086_2806# or
539 Audio_VID_DID = 16#8086_2805#)) or
540 (Config.CPU_Ironlake and Audio_VID_DID = 16#0000_0000#) or
541 (Config.Gen_G45 and (Audio_VID_DID = 16#8086_2801# or
542 Audio_VID_DID = 16#8086_2802# or
Tim Wawrzynczak605660b2022-06-08 12:48:19 -0600543 Audio_VID_DID = 16#8086_2803#)) or
544 (Config.CPU_Tigerlake and (Audio_VID_DID = 16#8086_2812#)));
Nico Huber83693c82016-10-08 22:17:55 +0200545 end Check_Platform;
Nico Hubere7ac6eb2017-09-04 23:54:13 +0200546
547 procedure Check_Platform_PCI (Success : out Boolean)
548 is
549 use type HW.Word16;
550 Vendor, Device : Word16;
551 begin
552 Dev.Read16 (Vendor, PCI.Vendor_Id);
553 Dev.Read16 (Device, PCI.Device_Id);
554
Nico Huber6a996dc2018-06-17 16:30:33 +0200555 Config.Detect_CPU (Device);
Nico Hubere7ac6eb2017-09-04 23:54:13 +0200556 Success := Vendor = 16#8086# and Config.Compatible_GPU (Device);
557 end Check_Platform_PCI;
Nico Huber83693c82016-10-08 22:17:55 +0200558 begin
Nico Huber83693c82016-10-08 22:17:55 +0200559 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
560
561 pragma Debug (Debug.Set_Register_Write_Delay (Write_Delay));
562
Nico Huberc5c66ec2019-09-28 23:59:45 +0200563 PCI_Usable := False;
Nico Huberc3f66f62017-07-16 21:39:54 +0200564 Linear_FB_Base := 0;
Nico Huber312433c2019-09-28 03:15:48 +0200565 PCode.Mailbox_Ready := False;
Nico Huber83693c82016-10-08 22:17:55 +0200566 Wait_For_HPD := HPD_Type'(others => False);
567 HPD_Delay := HPD_Delay_Type'(others => Now);
Nico Huber83693c82016-10-08 22:17:55 +0200568 Allocated_PLLs := (others => PLLs.Invalid);
Nico Huber99f10f32016-11-20 00:34:05 +0100569 Cur_Configs := Pipe_Configs'
570 (others => Pipe_Config'
Nico Huber83693c82016-10-08 22:17:55 +0200571 (Port => Disabled,
572 Framebuffer => HW.GFX.Default_FB,
Nico Hubera02b2c62018-01-09 15:58:34 +0100573 Cursor => Default_Cursor,
Nico Huber83693c82016-10-08 22:17:55 +0200574 Mode => HW.GFX.Invalid_Mode));
Nico Huber27088aa2018-06-10 13:28:05 +0200575 Config.Variable := Config.Initial_Settings;
Nico Huber6a996dc2018-06-17 16:30:33 +0200576 Registers.Set_Register_Base (Config.Default_MMIO_Base);
Nico Huber83693c82016-10-08 22:17:55 +0200577 PLLs.Initialize;
578
Nico Huber2b6f6992017-07-09 18:11:34 +0200579 Dev.Initialize (Success);
580
581 if Success then
Nico Huber6a996dc2018-06-17 16:30:33 +0200582 Check_Platform_PCI (Success);
Nico Hubere7ac6eb2017-09-04 23:54:13 +0200583 if Success then
Arthur Heymans960e2392026-03-03 19:45:24 +0100584 if Config.Has_I945_GTT_BAR then
585 -- i945: MMIO is on BAR0, GTT is on separate BAR3
586 Dev.Map (PCI_MMIO_Base, PCI.Res0);
587 Dev.Map (PCI_GTT_Base, PCI.Res3);
588 else
589 Dev.Map (PCI_MMIO_Base, PCI.Res0, Length => MMIO_GTT_Offset);
590 Dev.Map (PCI_GTT_Base, PCI.Res0, Offset => MMIO_GTT_Offset);
591 end if;
Nico Huber6a996dc2018-06-17 16:30:33 +0200592 if PCI_MMIO_Base /= 0 and PCI_GTT_Base /= 0 then
593 Registers.Set_Register_Base (PCI_MMIO_Base, PCI_GTT_Base);
Nico Huberc5c66ec2019-09-28 23:59:45 +0200594 PCI_Usable := True;
Nico Huber6a996dc2018-06-17 16:30:33 +0200595 else
596 pragma Debug (Debug.Put_Line
Arthur Heymans960e2392026-03-03 19:45:24 +0100597 ("ERROR: Couldn't map resource0."));
Nico Huber6a996dc2018-06-17 16:30:33 +0200598 Success := Config.Default_MMIO_Base_Set;
599 end if;
Nico Hubere7ac6eb2017-09-04 23:54:13 +0200600 end if;
Nico Huber2b6f6992017-07-09 18:11:34 +0200601 else
602 pragma Debug (Debug.Put_Line
603 ("WARNING: Couldn't initialize PCI dev."));
Nico Huber2b6f6992017-07-09 18:11:34 +0200604 Success := Config.Default_MMIO_Base_Set;
Nico Huber2b6f6992017-07-09 18:11:34 +0200605
Nico Hubere7ac6eb2017-09-04 23:54:13 +0200606 if Success then
607 Check_Platform (Success);
608 end if;
Nico Huber2b6f6992017-07-09 18:11:34 +0200609 end if;
610
Nico Huber5dbaf4b2020-01-08 17:24:58 +0100611 Panel.Static_Init; -- early for flow analysis
612
Nico Huber83693c82016-10-08 22:17:55 +0200613 if not Success then
614 pragma Debug (Debug.Put_Line ("ERROR: Incompatible CPU or PCH."));
615
Nico Huber83693c82016-10-08 22:17:55 +0200616 Initialized := False;
617 return;
618 end if;
619
620 Panel.Setup_PP_Sequencer;
Nico Huber83693c82016-10-08 22:17:55 +0200621
Nico Huber793a8d42016-11-21 18:57:03 +0100622 if Clean_State then
623 Power_And_Clocks.Pre_All_Off;
624 Connectors.Pre_All_Off;
625 Display_Controller.All_Off;
626 Connectors.Post_All_Off;
627 PLLs.All_Off;
628 Power_And_Clocks.Post_All_Off;
Nico Huber17d64b62017-07-15 20:51:25 +0200629 Registers.Clear_Fences;
Nico Huber33912aa2016-12-06 20:36:23 +0100630 else
631 -- According to PRMs, VGA plane is the only thing
Nico Huber3a0e2a02017-07-19 14:41:46 +0200632 -- that's enabled by default after reset...
Nico Huber33912aa2016-12-06 20:36:23 +0100633 Display_Controller.Legacy_VGA_Off;
Nico Huber3a0e2a02017-07-19 14:41:46 +0200634 -- ... along with some DDI port bits since Skylake.
635 Connectors.Post_Reset_Off;
Nico Huber793a8d42016-11-21 18:57:03 +0100636 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200637
638 -------------------- Now restart from a clean state ---------------------
639 Power_And_Clocks.Initialize;
Nico Huber1b991852024-07-17 16:44:05 +0200640 Port_Detect.Initialize;
641 Connectors.Initialize;
Nico Huber83693c82016-10-08 22:17:55 +0200642
Nico Huber1c3b9282017-02-09 13:57:04 +0100643 if Config.Has_PCH then
644 Registers.Unset_And_Set_Mask
645 (Register => Registers.PCH_RAWCLK_FREQ,
646 Mask_Unset => PCH_RAWCLK_FREQ_MASK,
Nico Huberc9ad9de2020-12-20 02:34:37 +0100647 Mask_Set => PCH_RAWCLK_FREQ (Config.Raw_Clock));
Nico Huber1c3b9282017-02-09 13:57:04 +0100648 end if;
Nico Huberf54d0962016-10-20 14:17:18 +0200649
Nico Huber83693c82016-10-08 22:17:55 +0200650 Initialized := True;
651
652 end Initialize;
Nico Huber793f4f82022-09-04 14:24:00 +0000653 pragma Annotate
654 (GNATprove, Intentional, "unused global",
655 "The whole, abstract Device_State is modified in certain configurations.");
656 pragma Warnings (GNATprove, On, "no check message justified*");
657 pragma Warnings
658 (GNATprove, On, """Registers.GTT_State"" * is not modified*");
Nico Huber83693c82016-10-08 22:17:55 +0200659
660 function Is_Initialized return Boolean
661 with
662 Refined_Post => Is_Initialized'Result = Initialized
663 is
664 begin
665 return Initialized;
666 end Is_Initialized;
667
668 ----------------------------------------------------------------------------
669
Nico Hubercf88f3d2018-06-05 13:27:34 +0200670 pragma Warnings
671 (GNATprove, Off, """Registers.Register_State"" * is not modified*",
Nico Huberadfe11f2018-06-10 14:59:04 +0200672 Reason => "Power_Up_VGA is only effective in certain configurations.");
Nico Huber17b513e2022-09-04 13:36:02 +0200673 pragma Warnings
674 (GNATprove, Off, "no check message justified*", Reason => "see below");
Nico Huber42fb2d02017-09-01 17:01:51 +0200675 procedure Power_Up_VGA
Nico Hubercf88f3d2018-06-05 13:27:34 +0200676 with
677 Refined_Global =>
Nico Huberadfe11f2018-06-10 14:59:04 +0200678 (Input => (Cur_Configs, Config.Variable, Time.State),
Nico Hubercf88f3d2018-06-05 13:27:34 +0200679 In_Out => (Registers.Register_State),
680 Proof_In => (Initialized))
Nico Huber42fb2d02017-09-01 17:01:51 +0200681 is
682 Fake_Config : constant Pipe_Configs :=
683 (Primary =>
684 (Port => Analog,
685 Framebuffer => HW.GFX.Default_FB,
Nico Hubera02b2c62018-01-09 15:58:34 +0100686 Cursor => Default_Cursor,
Nico Huber42fb2d02017-09-01 17:01:51 +0200687 Mode => HW.GFX.Invalid_Mode),
688 others =>
689 (Port => Disabled,
690 Framebuffer => HW.GFX.Default_FB,
Nico Hubera02b2c62018-01-09 15:58:34 +0100691 Cursor => Default_Cursor,
Nico Huber42fb2d02017-09-01 17:01:51 +0200692 Mode => HW.GFX.Invalid_Mode));
693 begin
694 Power_And_Clocks.Power_Up (Cur_Configs, Fake_Config);
695 end Power_Up_VGA;
Nico Hubercf88f3d2018-06-05 13:27:34 +0200696 pragma Annotate
697 (GNATprove, Intentional, "unused global",
Nico Huberadfe11f2018-06-10 14:59:04 +0200698 "Power_Up_VGA is only effective in certain configurations.");
Nico Hubercf88f3d2018-06-05 13:27:34 +0200699 pragma Warnings (GNATprove, On, "no check message justified*");
700 pragma Warnings
701 (GNATprove, On, """Registers.Register_State"" * is not modified*");
Nico Huber42fb2d02017-09-01 17:01:51 +0200702
703 ----------------------------------------------------------------------------
704
Nico Huber5374c3a2017-07-15 21:48:06 +0200705 function FB_First_Page (FB : Framebuffer_Type) return Natural is
Nico Huber34be6542017-12-13 09:26:24 +0100706 (Natural (Phys_Offset (FB) / GTT_Page_Size));
Nico Huber5374c3a2017-07-15 21:48:06 +0200707 function FB_Pages (FB : Framebuffer_Type) return Natural is
708 (Natural (Div_Round_Up (FB_Size (FB), GTT_Page_Size)));
709 function FB_Last_Page (FB : Framebuffer_Type) return Natural is
710 (FB_First_Page (FB) + FB_Pages (FB) - 1);
711
Nico Huber34be6542017-12-13 09:26:24 +0100712 -- Check basics and that it fits in GTT. For 90 degree rotations,
713 -- the Offset should be above GTT_Rotation_Offset. The latter will
714 -- be subtracted for the aperture mapping.
Nico Huber5374c3a2017-07-15 21:48:06 +0200715 function Valid_FB (FB : Framebuffer_Type) return Boolean is
Nico Huber34be6542017-12-13 09:26:24 +0100716 (Valid_Stride (FB) and
717 FB_First_Page (FB) in GTT_Range and
Nico Huber2e87c0d2020-04-18 00:46:39 +0200718 FB_Last_Page (FB) + 128 in GTT_Range and
Nico Huber34be6542017-12-13 09:26:24 +0100719 (not Rotation_90 (FB) or
Nico Huber2e87c0d2020-04-18 00:46:39 +0200720 (FB_First_Page (FB) mod 64 = 0 and
721 FB_Last_Page (FB) + 128 + GTT_Rotation_Offset in GTT_Range and
Nico Huber34be6542017-12-13 09:26:24 +0100722 FB.Offset >= Word32 (GTT_Rotation_Offset) * GTT_Page_Size)));
Nico Huber5374c3a2017-07-15 21:48:06 +0200723
724 -- Also check that we don't overflow the GTT's 39-bit space
725 -- (always true with a 32-bit base)
726 function Valid_Phys_FB (FB : Framebuffer_Type; Phys_Base : Word32)
727 return Boolean is
728 (Valid_FB (FB) and
Nico Huber34be6542017-12-13 09:26:24 +0100729 Int64 (Phys_Base) + Int64 (Phys_Offset (FB)) + Int64 (FB_Size (FB)) <=
Nico Huber5374c3a2017-07-15 21:48:06 +0200730 Int64 (GTT_Address_Type'Last))
731 with
732 Ghost;
733
Nico Huber83693c82016-10-08 22:17:55 +0200734 procedure Write_GTT
735 (GTT_Page : GTT_Range;
736 Device_Address : GTT_Address_Type;
Nico Huber5374c3a2017-07-15 21:48:06 +0200737 Valid : Boolean)
738 is
Nico Huber83693c82016-10-08 22:17:55 +0200739 begin
740 Registers.Write_GTT (GTT_Page, Device_Address, Valid);
741 end Write_GTT;
742
Nico Huberceda17d2018-06-09 22:00:29 +0200743 procedure Read_GTT
744 (Device_Address : out GTT_Address_Type;
745 Valid : out Boolean;
746 GTT_Page : in GTT_Range)
747 is
748 begin
749 Registers.Read_GTT (Device_Address, Valid, GTT_Page);
750 end Read_GTT;
751
Nico Huber194e57e2017-07-15 21:15:46 +0200752 procedure Setup_Default_GTT (FB : Framebuffer_Type; Phys_Base : Word32)
Nico Huber5374c3a2017-07-15 21:48:06 +0200753 with
754 Pre => Is_Initialized and Valid_Phys_FB (FB, Phys_Base)
Nico Huber83693c82016-10-08 22:17:55 +0200755 is
Nico Huber194e57e2017-07-15 21:15:46 +0200756 Phys_Addr : GTT_Address_Type :=
Nico Huber34be6542017-12-13 09:26:24 +0100757 GTT_Address_Type (Phys_Base) + GTT_Address_Type (Phys_Offset (FB));
Nico Huber83693c82016-10-08 22:17:55 +0200758 begin
Nico Huber194e57e2017-07-15 21:15:46 +0200759 for Idx in FB_First_Page (FB) .. FB_Last_Page (FB) loop
Nico Huber83693c82016-10-08 22:17:55 +0200760 Registers.Write_GTT
761 (GTT_Page => Idx,
762 Device_Address => Phys_Addr,
763 Valid => True);
Nico Huber194e57e2017-07-15 21:15:46 +0200764 Phys_Addr := Phys_Addr + GTT_Page_Size;
Nico Huber83693c82016-10-08 22:17:55 +0200765 end loop;
Nico Huber2e87c0d2020-04-18 00:46:39 +0200766 -- Add another 128 dummy pages to work around buggy VT-d
767 for Idx in FB_Last_Page (FB) + 1 .. FB_Last_Page (FB) + 128 loop
768 Registers.Write_GTT (Idx, Phys_Addr, True);
769 end loop;
Nico Huber9b479412017-08-27 11:55:56 +0200770
771 if Rotation_90 (FB) and FB.Tiling = Y_Tiled and FB.V_Stride >= 32 then
772 declare
773 V_Pages : constant Natural := Natural (FB.V_Stride) / 32;
774 Bytes_Per_Row : constant GTT_Address_Type :=
775 GTT_Address_Type (Pixel_To_Bytes (32 * FB.Stride, FB));
776 begin
777 Phys_Addr := GTT_Address_Type (Phys_Base) +
Nico Huber34be6542017-12-13 09:26:24 +0100778 GTT_Address_Type (Phys_Offset (FB)) +
Nico Huber9b479412017-08-27 11:55:56 +0200779 GTT_Address_Type (FB_Size (FB));
780 for Page in FB_First_Page (FB) .. FB_Last_Page (FB) loop
781 Phys_Addr := Phys_Addr - Bytes_Per_Row;
782 Registers.Write_GTT
783 (GTT_Page => GTT_Rotation_Offset + Page,
784 Device_Address => Phys_Addr,
785 Valid => True);
786
787 if (Page - FB_First_Page (FB) + 1) mod V_Pages = 0 then
788 Phys_Addr := Phys_Addr + GTT_Page_Size +
789 GTT_Address_Type (V_Pages) * Bytes_Per_Row;
790 end if;
791 end loop;
792 end;
Nico Huber2e87c0d2020-04-18 00:46:39 +0200793 -- Add another 128 dummy pages to work around buggy VT-d
794 for Idx in FB_Last_Page (FB) + 1 .. FB_Last_Page (FB) + 128 loop
795 Registers.Write_GTT (GTT_Rotation_Offset + Idx, Phys_Addr, True);
796 end loop;
Nico Huber9b479412017-08-27 11:55:56 +0200797 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200798 end Setup_Default_GTT;
799
800 ----------------------------------------------------------------------------
801
Nico Hubereedde882017-07-16 02:54:39 +0200802 use type HW.Word16;
803 subtype Stolen_Size_Range is Int64 range 0 .. 2 ** 33;
804
805 function GGMS_Gen4 (GGC : Word16) return Natural is
806 (Natural (Shift_Right (GGC, 8) and 16#07#));
807 function GTT_Size_Gen4 (GGC : Word16) return Natural is
808 (if GGMS_Gen4 (GGC) in 1 .. 3 then
809 (GGMS_Gen4 (GGC) + 1) * 2 ** 19 else 0);
810
811 function GMS_Gen4 (GGC : Word16) return Natural is
812 (Natural (Shift_Right (GGC, 4) and 16#0f#));
813 Valid_Stolen_Size_Gen4 : constant
814 array (Natural range 1 .. 13) of Stolen_Size_Range :=
815 (1, 4, 8, 16, 32, 48, 64, 128, 256, 96, 160, 224, 352);
816 function Stolen_Size_Gen4 (GGC : Word16) return Stolen_Size_Range is
817 (if GMS_Gen4 (GGC) in Valid_Stolen_Size_Gen4'Range then
Arthur Heymans5fd9a312017-09-12 12:45:18 +0200818 Valid_Stolen_Size_Gen4 (GMS_Gen4 (GGC)) * 2 ** 20 else 0);
Nico Hubereedde882017-07-16 02:54:39 +0200819
820 function GTT_Size_Gen6 (GGC : Word16) return Natural is
821 (Natural (Shift_Right (GGC, 8) and 16#03#) * 2 ** 20);
822
823 function Stolen_Size_Gen6 (GGC : Word16) return Stolen_Size_Range is
824 (Stolen_Size_Range (Shift_Right (GGC, 3) and 16#1f#) * 32 * 2 ** 20);
825
Nico Huberfe7985f2019-10-12 22:19:24 +0200826 function GGMS_Gen8 (GGC : Word16) return Natural is
827 (Natural (Shift_Right (GGC, 6) and 16#03#));
Nico Hubereedde882017-07-16 02:54:39 +0200828 function GTT_Size_Gen8 (GGC : Word16) return Natural is
Nico Huberfe7985f2019-10-12 22:19:24 +0200829 (if GGMS_Gen8 (GGC) /= 0 then
830 Natural (Shift_Left (Word32'(1), 20 + GGMS_Gen8 (GGC))) else 0);
Nico Hubereedde882017-07-16 02:54:39 +0200831
832 function GMS_Gen8 (GGC : Word16) return Stolen_Size_Range is
833 (Stolen_Size_Range (Shift_Right (GGC, 8) and 16#ff#));
834 function Stolen_Size_Gen8 (GGC : Word16) return Stolen_Size_Range is
835 (GMS_Gen8 (GGC) * 32 * 2 ** 20);
836
837 function Stolen_Size_Gen9 (GGC : Word16) return Stolen_Size_Range is
838 (if GMS_Gen8 (GGC) < 16#f0# then
839 Stolen_Size_Gen8 (GGC)
840 else
841 (GMS_Gen8 (GGC) - 16#f0# + 1) * 4 * 2 ** 20);
842
843 procedure Decode_Stolen
844 (GTT_Size : out Natural;
845 Stolen_Size : out Stolen_Size_Range)
846 with
847 Pre => Is_Initialized
848 is
Nico Huber63ec8362018-06-09 17:42:19 +0200849 GGC_Reg : constant PCI.Index :=
Arthur Heymans960e2392026-03-03 19:45:24 +0100850 (if Config.Gen_I945 or Config.Gen_G45 or Config.CPU_Ironlake
851 then 16#52# else 16#50#);
Nico Hubereedde882017-07-16 02:54:39 +0200852 GGC : Word16;
853 begin
854 Dev.Read16 (GGC, GGC_Reg);
Arthur Heymans960e2392026-03-03 19:45:24 +0100855 if Config.Gen_I945 then
856 -- i945 GTT is on a separate BAR3; GGC GGMS encoding differs
857 -- from Gen4+. Match the Linux driver and use the BAR size.
858 Dev.Resource_Size (GTT_Size, PCI.Res3);
859 Stolen_Size := Stolen_Size_Gen4 (GGC);
860 elsif Config.Gen_G45 or Config.CPU_Ironlake then
Nico Huber998ee2b2018-06-12 23:02:17 +0200861 GTT_Size := GTT_Size_Gen4 (GGC);
862 Stolen_Size := Stolen_Size_Gen4 (GGC);
863 elsif Config.CPU_Sandybridge or Config.CPU_Ivybridge or Config.CPU_Haswell
864 then
865 GTT_Size := GTT_Size_Gen6 (GGC);
866 Stolen_Size := Stolen_Size_Gen6 (GGC);
867 elsif Config.CPU_Broadwell then
868 GTT_Size := GTT_Size_Gen8 (GGC);
869 Stolen_Size := Stolen_Size_Gen8 (GGC);
870 else
871 GTT_Size := GTT_Size_Gen8 (GGC);
872 Stolen_Size := Stolen_Size_Gen9 (GGC);
873 end if;
Nico Hubereedde882017-07-16 02:54:39 +0200874 end Decode_Stolen;
875
Arthur Heymans960e2392026-03-03 19:45:24 +0100876 procedure GTT_Entry_Count (Count : out Natural)
877 is
878 GTT_Size : Natural;
879
880 procedure Fake_Config_State_Access
881 with
882 Global => (Input => Config.Variable),
883 Annotate => (GNATprove, Intentional, "unused global",
884 "Used to have a common contract across platforms.");
885 procedure Fake_Config_State_Access is null;
886 begin
887 Fake_Config_State_Access;
888
889 if Config.Has_I945_GTT_BAR then
890 -- i945 GTT is on a separate BAR3; its size is the BAR size.
891 Dev.Resource_Size (GTT_Size, PCI.Res3);
892 else
893 -- Gen4+: GTT size is encoded in the GGC register.
894 declare
895 GGC_Reg : constant PCI.Index :=
896 (if Config.Gen_G45 or Config.CPU_Ironlake
897 then 16#52# else 16#50#);
898 GGC : Word16;
899 begin
900 Dev.Read16 (GGC, GGC_Reg);
901 if Config.Gen_G45 or Config.CPU_Ironlake then
902 GTT_Size := GTT_Size_Gen4 (GGC);
903 elsif Config.CPU_Sandybridge or
904 Config.CPU_Ivybridge or
905 Config.CPU_Haswell
906 then
907 GTT_Size := GTT_Size_Gen6 (GGC);
908 else
909 GTT_Size := GTT_Size_Gen8 (GGC);
910 end if;
911 end;
912 end if;
913 Count := Natural'Min (GTT_Size / Config.GTT_PTE_Size, GTT_Range'Last + 1);
914 end GTT_Entry_Count;
915
Nico Hubereedde882017-07-16 02:54:39 +0200916 -- Additional runtime validation that FB fits stolen memory and aperture.
917 procedure Validate_FB (FB : Framebuffer_Type; Valid : out Boolean)
918 with
919 Pre => Is_Initialized,
920 Post => (if Valid then Valid_FB (FB))
921 is
Nico Huber2e87c0d2020-04-18 00:46:39 +0200922 GTT_Off : constant Natural :=
923 (if Rotation_90 (FB) then GTT_Rotation_Offset else 0);
924
Nico Hubereedde882017-07-16 02:54:39 +0200925 GTT_Size, Aperture_Size : Natural;
926 Stolen_Size : Stolen_Size_Range;
927 begin
928 Valid := Valid_FB (FB);
929
930 if Valid then
931 Decode_Stolen (GTT_Size, Stolen_Size);
932 Dev.Resource_Size (Aperture_Size, PCI.Res2);
933 Valid :=
Nico Huber2e87c0d2020-04-18 00:46:39 +0200934 FB_Last_Page (FB) + 128 + GTT_Off < GTT_Size / Config.GTT_PTE_Size
935 and
936 FB_Last_Page (FB) < Natural (Stolen_Size / GTT_Page_Size)
937 and
Nico Hubereedde882017-07-16 02:54:39 +0200938 FB_Last_Page (FB) < Aperture_Size / GTT_Page_Size;
Nico Huber34be6542017-12-13 09:26:24 +0100939 pragma Debug (not Valid, Debug.Put_Line
Nico Hubereedde882017-07-16 02:54:39 +0200940 ("Stolen memory too small to hold framebuffer."));
941 end if;
942 end Validate_FB;
943
Nico Huber5374c3a2017-07-15 21:48:06 +0200944 procedure Setup_Default_FB
945 (FB : in Framebuffer_Type;
946 Clear : in Boolean := True;
947 Success : out Boolean)
948 is
Nico Huber5374c3a2017-07-15 21:48:06 +0200949 GMA_Phys_Base_Mask : constant := 16#fff0_0000#;
950
951 Phys_Base : Word32;
952 begin
Nico Hubereedde882017-07-16 02:54:39 +0200953 Validate_FB (FB, Success);
Nico Huber5374c3a2017-07-15 21:48:06 +0200954
955 if Success then
Tim Wawrzynczak1b65b842022-09-09 10:23:06 -0600956 if Config.GMA_Base_Is_64bit then
957 Dev.Read32 (Phys_Base, Config.GMA_Phys_Base_Index + 4);
958 if Phys_Base /= 0 then
959 pragma Debug (Debug.Put_Line ("Cannot handle 64-bit DSM yet."));
960 Success := False;
961 return;
962 end if;
963 end if;
964
965 Dev.Read32 (Phys_Base, Config.GMA_Phys_Base_Index);
Nico Huber5374c3a2017-07-15 21:48:06 +0200966 Phys_Base := Phys_Base and GMA_Phys_Base_Mask;
967 Success := Phys_Base /= GMA_Phys_Base_Mask and Phys_Base /= 0;
968 pragma Debug (not Success, Debug.Put_Line
969 ("Failed to read stolen memory base."));
Nico Huber0164b022017-08-24 15:12:51 +0200970
971 if Success then
972 if FB.Tiling in XY_Tiling then
973 Registers.Add_Fence
974 (First_Page => FB_First_Page (FB),
975 Last_Page => FB_Last_Page (FB),
976 Tiling => FB.Tiling,
977 Pitch => FB_Pitch (FB.Stride, FB),
978 Success => Success);
979 end if;
980 pragma Debug (not Success, Debug.Put_Line
981 ("Tiled framebuffer but no fence regs available."));
982 end if;
983
Nico Huber5374c3a2017-07-15 21:48:06 +0200984 if Success then
985 Setup_Default_GTT (FB, Phys_Base);
986 end if;
987 end if;
988
989 if Success and then Clear then
990 declare
991 use type HW.Word64;
992 Linear_FB : Word64;
993 begin
Nico Huberc3f66f62017-07-16 21:39:54 +0200994 Map_Linear_FB (Linear_FB, FB);
Nico Huber5374c3a2017-07-15 21:48:06 +0200995 if Linear_FB /= 0 then
Nico Huberc3f66f62017-07-16 21:39:54 +0200996 Framebuffer_Filler.Fill (Linear_FB, FB);
Nico Huber5374c3a2017-07-15 21:48:06 +0200997 end if;
Nico Huber5374c3a2017-07-15 21:48:06 +0200998 end;
999 end if;
1000 end Setup_Default_FB;
1001
Nico Huberc3f66f62017-07-16 21:39:54 +02001002 procedure Map_Linear_FB (Linear_FB : out Word64; FB : in Framebuffer_Type)
1003 is
1004 use type HW.Word64;
1005
1006 Valid : Boolean;
1007 begin
1008 Linear_FB := 0;
1009
1010 if Linear_FB_Base = 0 then
1011 Dev.Map (Linear_FB_Base, PCI.Res2);
1012 pragma Debug
1013 (Linear_FB_Base = 0, Debug.Put_Line ("Failed to map resource2."));
1014 end if;
1015
1016 if Linear_FB_Base /= 0 then
1017 Validate_FB (FB, Valid);
1018 if Valid then
Nico Huber34be6542017-12-13 09:26:24 +01001019 Linear_FB := Linear_FB_Base + Word64 (Phys_Offset (FB));
Nico Huberc3f66f62017-07-16 21:39:54 +02001020 end if;
1021 end if;
1022 end Map_Linear_FB;
1023
Nico Huber5374c3a2017-07-15 21:48:06 +02001024 ----------------------------------------------------------------------------
1025
Nico Huber99f10f32016-11-20 00:34:05 +01001026 procedure Dump_Configs (Configs : Pipe_Configs)
Nico Huber83693c82016-10-08 22:17:55 +02001027 is
1028 subtype Pipe_Name is String (1 .. 9);
Nico Huber99f10f32016-11-20 00:34:05 +01001029 type Pipe_Name_Array is array (Pipe_Index) of Pipe_Name;
Nico Huber83693c82016-10-08 22:17:55 +02001030 Pipe_Names : constant Pipe_Name_Array :=
1031 (Primary => "Primary ",
1032 Secondary => "Secondary",
1033 Tertiary => "Tertiary ");
Nico Huber5ef4d602017-12-13 13:56:47 +01001034
1035 subtype Tiling_Name is String (1 .. 7);
1036 type Tiling_Name_Array is array (Tiling_Type) of Tiling_Name;
1037 Tilings : constant Tiling_Name_Array :=
1038 (Linear => "Linear ",
1039 X_Tiled => "X_Tiled",
1040 Y_Tiled => "Y_Tiled");
1041
1042 subtype Rotation_Name is String (1 .. 11);
1043 type Rotation_Name_Array is array (Rotation_Type) of Rotation_Name;
1044 Rotations : constant Rotation_Name_Array :=
1045 (No_Rotation => "No_Rotation",
1046 Rotated_90 => "Rotated_90 ",
1047 Rotated_180 => "Rotated_180",
1048 Rotated_270 => "Rotated_270");
Nico Huber83693c82016-10-08 22:17:55 +02001049 begin
1050 Debug.New_Line;
Paul Menzelb83107c2017-05-04 09:02:33 +02001051 Debug.Put_Line ("CONFIG =>");
Nico Huber99f10f32016-11-20 00:34:05 +01001052 for Pipe in Pipe_Index loop
1053 if Pipe = Pipe_Index'First then
Nico Huber83693c82016-10-08 22:17:55 +02001054 Debug.Put (" (");
1055 else
1056 Debug.Put (" ");
1057 end if;
1058 Debug.Put_Line (Pipe_Names (Pipe) & " =>");
1059 Debug.Put_Line
1060 (" (Port => " & Port_Names (Configs (Pipe).Port) & ",");
1061 Debug.Put_Line (" Framebuffer =>");
Nico Huber5ef4d602017-12-13 13:56:47 +01001062 Debug.Put (" (Width => ");
Nico Huber83693c82016-10-08 22:17:55 +02001063 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Width);
1064 Debug.Put_Line (",");
Nico Huber5ef4d602017-12-13 13:56:47 +01001065 Debug.Put (" Height => ");
Nico Huber83693c82016-10-08 22:17:55 +02001066 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Height);
1067 Debug.Put_Line (",");
Nico Huber5ef4d602017-12-13 13:56:47 +01001068 Debug.Put (" Start_X => ");
1069 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Start_X);
1070 Debug.Put_Line (",");
1071 Debug.Put (" Start_Y => ");
1072 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Start_Y);
1073 Debug.Put_Line (",");
1074 Debug.Put (" Stride => ");
Nico Huber83693c82016-10-08 22:17:55 +02001075 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Stride);
1076 Debug.Put_Line (",");
Nico Huber5ef4d602017-12-13 13:56:47 +01001077 Debug.Put (" V_Stride => ");
1078 Debug.Put_Int32 (Configs (Pipe).Framebuffer.V_Stride);
1079 Debug.Put_Line (",");
1080 Debug.Put (" Tiling => ");
1081 Debug.Put_Line (Tilings (Configs (Pipe).Framebuffer.Tiling) & ",");
1082 Debug.Put (" Rotation => ");
1083 Debug.Put_Line (Rotations (Configs (Pipe).Framebuffer.Rotation) & ",");
Nico Huber83693c82016-10-08 22:17:55 +02001084 Debug.Put (" Offset => ");
1085 Debug.Put_Word32 (Configs (Pipe).Framebuffer.Offset);
1086 Debug.Put_Line (",");
1087 Debug.Put (" BPC => ");
1088 Debug.Put_Int64 (Configs (Pipe).Framebuffer.BPC);
1089 Debug.Put_Line ("),");
1090 Debug.Put_Line (" Mode =>");
1091 Debug.Put (" (Dotclock => ");
1092 Debug.Put_Int64 (Configs (Pipe).Mode.Dotclock);
1093 Debug.Put_Line (",");
1094 Debug.Put (" H_Visible => ");
Nico Huberc5c767a2018-06-03 01:09:04 +02001095 Debug.Put_Int32 (Configs (Pipe).Mode.H_Visible);
Nico Huber83693c82016-10-08 22:17:55 +02001096 Debug.Put_Line (",");
1097 Debug.Put (" H_Sync_Begin => ");
Nico Huberc5c767a2018-06-03 01:09:04 +02001098 Debug.Put_Int32 (Configs (Pipe).Mode.H_Sync_Begin);
Nico Huber83693c82016-10-08 22:17:55 +02001099 Debug.Put_Line (",");
1100 Debug.Put (" H_Sync_End => ");
Nico Huberc5c767a2018-06-03 01:09:04 +02001101 Debug.Put_Int32 (Configs (Pipe).Mode.H_Sync_End);
Nico Huber83693c82016-10-08 22:17:55 +02001102 Debug.Put_Line (",");
1103 Debug.Put (" H_Total => ");
Nico Huberc5c767a2018-06-03 01:09:04 +02001104 Debug.Put_Int32 (Configs (Pipe).Mode.H_Total);
Nico Huber83693c82016-10-08 22:17:55 +02001105 Debug.Put_Line (",");
1106 Debug.Put (" V_Visible => ");
Nico Huberc5c767a2018-06-03 01:09:04 +02001107 Debug.Put_Int32 (Configs (Pipe).Mode.V_Visible);
Nico Huber83693c82016-10-08 22:17:55 +02001108 Debug.Put_Line (",");
1109 Debug.Put (" V_Sync_Begin => ");
Nico Huberc5c767a2018-06-03 01:09:04 +02001110 Debug.Put_Int32 (Configs (Pipe).Mode.V_Sync_Begin);
Nico Huber83693c82016-10-08 22:17:55 +02001111 Debug.Put_Line (",");
1112 Debug.Put (" V_Sync_End => ");
Nico Huberc5c767a2018-06-03 01:09:04 +02001113 Debug.Put_Int32 (Configs (Pipe).Mode.V_Sync_End);
Nico Huber83693c82016-10-08 22:17:55 +02001114 Debug.Put_Line (",");
1115 Debug.Put (" V_Total => ");
Nico Huberc5c767a2018-06-03 01:09:04 +02001116 Debug.Put_Int32 (Configs (Pipe).Mode.V_Total);
Nico Huber83693c82016-10-08 22:17:55 +02001117 Debug.Put_Line (",");
1118 Debug.Put_Line (" H_Sync_Active_High => " &
1119 (if Configs (Pipe).Mode.H_Sync_Active_High
1120 then "True,"
1121 else "False,"));
1122 Debug.Put_Line (" V_Sync_Active_High => " &
1123 (if Configs (Pipe).Mode.V_Sync_Active_High
1124 then "True,"
1125 else "False,"));
1126 Debug.Put (" BPC => ");
1127 Debug.Put_Int64 (Configs (Pipe).Mode.BPC);
Nico Huber99f10f32016-11-20 00:34:05 +01001128 if Pipe /= Pipe_Index'Last then
Nico Huber83693c82016-10-08 22:17:55 +02001129 Debug.Put_Line (")),");
1130 else
1131 Debug.Put_Line (")));");
1132 end if;
1133 end loop;
1134 end Dump_Configs;
1135
Nico Huberc5c66ec2019-09-28 23:59:45 +02001136 ----------------------------------------------------------------------------
1137
1138 procedure PCI_Read16 (Value : out Word16; Offset : HW.PCI.Index) is
1139 begin
1140 Dev.Read16 (Value, Offset);
1141 end PCI_Read16;
1142
Nico Huber83693c82016-10-08 22:17:55 +02001143end HW.GFX.GMA;