blob: d1ae1a013aadb010eb5627ac45bcc3ec7364f603 [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
56 subtype Port_Name is String (1 .. 8);
57 type Port_Name_Array is array (Port_Type) of Port_Name;
58 Port_Names : constant Port_Name_Array :=
59 (Disabled => "Disabled",
Nico Huber8beafd72020-01-07 14:59:44 +010060 LVDS => "LVDS ",
61 eDP => "eDP ",
Nico Huber83693c82016-10-08 22:17:55 +020062 DP1 => "DP1 ",
63 DP2 => "DP2 ",
64 DP3 => "DP3 ",
Nico Huber0d454cd2016-11-21 13:33:43 +010065 HDMI1 => "HDMI1 ",
66 HDMI2 => "HDMI2 ",
67 HDMI3 => "HDMI3 ",
Nico Huber83693c82016-10-08 22:17:55 +020068 Analog => "Analog ");
69
Nico Huber2b6f6992017-07-09 18:11:34 +020070 package Dev is new HW.PCI.Dev (PCI.Address'(0, 2, 0));
71
Nico Huber83693c82016-10-08 22:17:55 +020072 package Display_Controller renames Pipe_Setup;
73
Nico Huber99f10f32016-11-20 00:34:05 +010074 type PLLs_Type is array (Pipe_Index) of PLLs.T;
Nico Huber83693c82016-10-08 22:17:55 +020075
Nico Huber83693c82016-10-08 22:17:55 +020076 type HPD_Type is array (Port_Type) of Boolean;
Nico Huber3be61d42017-01-09 13:58:18 +010077 type HPD_Delay_Type is array (Active_Port_Type) of Time.T;
Nico Huber83693c82016-10-08 22:17:55 +020078
Nico Huber83693c82016-10-08 22:17:55 +020079 Allocated_PLLs : PLLs_Type;
Nico Huber83693c82016-10-08 22:17:55 +020080 HPD_Delay : HPD_Delay_Type;
81 Wait_For_HPD : HPD_Type;
82 Initialized : Boolean := False;
83
Nico Huberc3f66f62017-07-16 21:39:54 +020084 Linear_FB_Base : Word64;
85
Nico Huber83693c82016-10-08 22:17:55 +020086 ----------------------------------------------------------------------------
87
Nico Huberdde06302020-12-20 02:18:30 +010088 PCH_RAWCLK_FREQ_MASK : constant :=
89 (if Config.Has_Fractional_RawClk then 16#3fff# * 2 ** 16 else 16#3ff# * 2 ** 0);
Nico Huberf54d0962016-10-20 14:17:18 +020090
91 function PCH_RAWCLK_FREQ (Freq : Frequency_Type) return Word32
92 is
93 begin
Nico Huberdde06302020-12-20 02:18:30 +010094 if Config.Has_Fractional_RawClk then
95 declare
96 Fraction_K : constant Int64 := Freq / 1_000 mod 1_000;
97 Freq32 : Word32 := Shift_Left (Word32 (Freq / 1_000_000), 16);
98 begin
99 if Fraction_K /= 0 then
100 Freq32 := Freq32 or Shift_Left
101 (Word32 (Div_Round_Closest (1_000, Fraction_K) - 1), 26);
102 end if;
103 return Freq32;
104 end;
105 else
106 return Word32 (Freq / 1_000_000);
107 end if;
Nico Huberf54d0962016-10-20 14:17:18 +0200108 end PCH_RAWCLK_FREQ;
109
110 ----------------------------------------------------------------------------
111
Nico Huber43370ba2017-01-09 15:26:19 +0100112 procedure Enable_Output
113 (Pipe : in Pipe_Index;
114 Pipe_Cfg : in Pipe_Config;
115 Success : out Boolean)
Nico Huber8a5a3b52018-06-04 14:42:13 +0200116 with
Nico Huber9a4c4c32019-09-16 22:05:11 +0200117 Pre =>
118 Pipe_Cfg.Port in Active_Port_Type and
119 Config_Helpers.Valid_FB (Pipe_Cfg.Framebuffer, Pipe_Cfg.Mode)
Nico Huber43370ba2017-01-09 15:26:19 +0100120 is
121 Port_Cfg : Port_Config;
122 begin
Nico Huber3be61d42017-01-09 13:58:18 +0100123 pragma Debug (Debug.New_Line);
124 pragma Debug (Debug.Put_Line
125 ("Trying to enable port " & Port_Names (Pipe_Cfg.Port)));
126
Nico Huber43370ba2017-01-09 15:26:19 +0100127 Config_Helpers.Fill_Port_Config
128 (Port_Cfg, Pipe, Pipe_Cfg.Port, Pipe_Cfg.Mode, Success);
129
130 if Success then
Nico Huber43370ba2017-01-09 15:26:19 +0100131 Connector_Info.Preferred_Link_Setting (Port_Cfg, Success);
132 end if;
133
134 -- loop over all possible DP-lane configurations
135 -- (non-DP ports use a single fake configuration)
136 while Success loop
137 pragma Loop_Invariant
138 (Pipe_Cfg.Port in Active_Port_Type and
139 Port_Cfg.Mode = Port_Cfg.Mode'Loop_Entry);
140
141 PLLs.Alloc
142 (Port_Cfg => Port_Cfg,
143 PLL => Allocated_PLLs (Pipe),
144 Success => Success);
145
146 if Success then
147 -- try each DP-lane configuration twice
148 for Try in 1 .. 2 loop
149 pragma Loop_Invariant
150 (Pipe_Cfg.Port in Active_Port_Type);
151
Nico Huber4798c662017-01-11 12:44:48 +0100152 -- Clear pending hot-plug events before every try
153 Port_Detect.Clear_Hotplug_Detect (Pipe_Cfg.Port);
154
Nico Huber43370ba2017-01-09 15:26:19 +0100155 Connectors.Pre_On
156 (Pipe => Pipe,
157 Port_Cfg => Port_Cfg,
158 PLL_Hint => PLLs.Register_Value (Allocated_PLLs (Pipe)),
159 Success => Success);
160
161 if Success then
162 Display_Controller.On
163 (Pipe => Pipe,
164 Port_Cfg => Port_Cfg,
Nico Huber4dc4c612018-01-10 15:55:09 +0100165 Framebuffer => Pipe_Cfg.Framebuffer,
166 Cursor => Pipe_Cfg.Cursor);
Nico Huber43370ba2017-01-09 15:26:19 +0100167
168 Connectors.Post_On
Arthur Heymans60d0e5f2018-03-28 17:08:27 +0200169 (Pipe => Pipe,
170 Port_Cfg => Port_Cfg,
Nico Huber43370ba2017-01-09 15:26:19 +0100171 PLL_Hint => PLLs.Register_Value (Allocated_PLLs (Pipe)),
172 Success => Success);
173
174 if not Success then
175 Display_Controller.Off (Pipe);
176 Connectors.Post_Off (Port_Cfg);
177 end if;
178 end if;
179
180 exit when Success;
181 end loop;
182 exit when Success; -- connection established => stop loop
183
184 -- connection failed
185 PLLs.Free (Allocated_PLLs (Pipe));
186 end if;
187
188 Connector_Info.Next_Link_Setting (Port_Cfg, Success);
189 end loop;
190
191 if Success then
192 pragma Debug (Debug.Put_Line
193 ("Enabled port " & Port_Names (Pipe_Cfg.Port)));
194 else
195 Wait_For_HPD (Pipe_Cfg.Port) := True;
Nico Huber2bbd6e72020-01-07 18:22:59 +0100196 Panel.Off (Config_Helpers.To_Panel (Pipe_Cfg.Port));
Nico Huber43370ba2017-01-09 15:26:19 +0100197 end if;
198 end Enable_Output;
199
Nico Huber3be61d42017-01-09 13:58:18 +0100200 procedure Disable_Output (Pipe : Pipe_Index; Pipe_Cfg : Pipe_Config)
201 is
202 Port_Cfg : Port_Config;
203 Success : Boolean;
204 begin
205 Config_Helpers.Fill_Port_Config
206 (Port_Cfg, Pipe, Pipe_Cfg.Port, Pipe_Cfg.Mode, Success);
207 if Success then
208 pragma Debug (Debug.New_Line);
209 pragma Debug (Debug.Put_Line
210 ("Disabling port " & Port_Names (Pipe_Cfg.Port)));
211 pragma Debug (Debug.New_Line);
212
213 Connectors.Pre_Off (Port_Cfg);
214 Display_Controller.Off (Pipe);
215 Connectors.Post_Off (Port_Cfg);
216
217 PLLs.Free (Allocated_PLLs (Pipe));
218 end if;
219 end Disable_Output;
220
Nico Huber99f10f32016-11-20 00:34:05 +0100221 procedure Update_Outputs (Configs : Pipe_Configs)
Nico Huber83693c82016-10-08 22:17:55 +0200222 is
Nico Huber3be61d42017-01-09 13:58:18 +0100223 procedure Check_HPD (Port : in Active_Port_Type; Detected : out Boolean)
224 is
225 HPD_Delay_Over : constant Boolean := Time.Timed_Out (HPD_Delay (Port));
226 begin
227 if HPD_Delay_Over then
228 Port_Detect.Hotplug_Detect (Port, Detected);
229 HPD_Delay (Port) := Time.MS_From_Now (333);
230 else
231 Detected := False;
232 end if;
233 end Check_HPD;
Nico Huberb56b9c52017-01-11 15:12:23 +0100234
Nico Huber9a4c4c32019-09-16 22:05:11 +0200235 Scaler_Reservation : Display_Controller.Scaler_Reservation :=
236 Display_Controller.Null_Scaler_Reservation;
Nico Huber564103f2017-01-11 15:33:07 +0100237
Nico Huber9a4c4c32019-09-16 22:05:11 +0200238 Update_Power : Boolean := False;
Nico Huberb0bbdbc2019-09-27 22:32:21 +0200239 Update_CDClk : Boolean;
Nico Huber9a4c4c32019-09-16 22:05:11 +0200240 Old_Configs,
241 New_Configs : Pipe_Configs;
Nico Huber3d06de82018-05-29 01:35:04 +0200242
243 function Full_Update (Cur_Config, New_Config : Pipe_Config) return Boolean
244 is
245 begin
246 return
Nico Huber958c5642018-06-02 16:59:31 +0200247 Cur_Config.Port /= New_Config.Port
248 or else
249 Cur_Config.Mode /= New_Config.Mode
250 or else
Nico Huber3d06de82018-05-29 01:35:04 +0200251 (Config.Use_PDW_For_EDP_Scaling and then
Nico Huber8beafd72020-01-07 14:59:44 +0100252 (Cur_Config.Port = eDP and
Nico Huber958c5642018-06-02 16:59:31 +0200253 Requires_Scaling (Cur_Config) /= Requires_Scaling (New_Config)))
254 or else
255 (Config.Has_GMCH_PFIT_CONTROL and then
256 (Requires_Scaling (Cur_Config) /= Requires_Scaling (New_Config) or
257 Scaling_Type (Cur_Config) /= Scaling_Type (New_Config)));
Nico Huber3d06de82018-05-29 01:35:04 +0200258 end Full_Update;
Nico Huber83693c82016-10-08 22:17:55 +0200259 begin
260 Old_Configs := Cur_Configs;
Nico Huber9a4c4c32019-09-16 22:05:11 +0200261 New_Configs := Configs;
262
263 -- validate new configs, filter invalid configs and those waiting for HPD
264 for Pipe in Pipe_Index loop
265 declare
266 Success : Boolean := True;
267 Cur_Config : Pipe_Config renames Cur_Configs (Pipe);
268 New_Config : Pipe_Config renames New_Configs (Pipe);
269 begin
270 if New_Config.Port /= Disabled then
271 if Wait_For_HPD (New_Config.Port) then
272 Check_HPD (New_Config.Port, Success);
273 Wait_For_HPD (New_Config.Port) := not Success;
274 end if;
275
276 Success := Success and then
277 Config_Helpers.Validate_Config
278 (New_Config.Framebuffer, New_Config.Mode, Pipe);
279
280 if Success and then Requires_Scaling (New_Config) then
281 Display_Controller.Reserve_Scaler
282 (Success, Scaler_Reservation, Pipe);
283 end if;
284
285 if not Success then
286 New_Config.Port := Disabled;
287 end if;
288 end if;
289 end;
290 pragma Loop_Invariant
291 (for all P in Pipe_Index'First .. Pipe =>
292 New_Configs (P).Port = Disabled or
293 Config_Helpers.Valid_FB
294 (New_Configs (P).Framebuffer, New_Configs (P).Mode));
295 end loop;
Nico Huber83693c82016-10-08 22:17:55 +0200296
Nico Huberb0bbdbc2019-09-27 22:32:21 +0200297 -- limit dotclocks to maximum CDClk, if we are about
298 -- to switch CDClk, all pipes have to be disabled
299 Power_And_Clocks.Limit_Dotclocks (New_Configs, Update_CDClk);
300
Nico Huberb56b9c52017-01-11 15:12:23 +0100301 -- disable all pipes that changed or had a hot-plug event
302 for Pipe in Pipe_Index loop
303 declare
304 Unplug_Detected : Boolean;
305 Cur_Config : Pipe_Config renames Cur_Configs (Pipe);
Nico Huber9a4c4c32019-09-16 22:05:11 +0200306 New_Config : Pipe_Config renames New_Configs (Pipe);
Nico Huberb56b9c52017-01-11 15:12:23 +0100307 begin
308 if Cur_Config.Port /= Disabled then
309 Check_HPD (Cur_Config.Port, Unplug_Detected);
Nico Huber83693c82016-10-08 22:17:55 +0200310
Nico Huberb0bbdbc2019-09-27 22:32:21 +0200311 if Update_CDClk or
312 Unplug_Detected or
313 Full_Update (Cur_Config, New_Config)
314 then
Nico Huberb56b9c52017-01-11 15:12:23 +0100315 Disable_Output (Pipe, Cur_Config);
316 Cur_Config.Port := Disabled;
Nico Huber9a4c4c32019-09-16 22:05:11 +0200317 Update_Power := True;
Nico Huberb56b9c52017-01-11 15:12:23 +0100318 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200319 end if;
Nico Huberb56b9c52017-01-11 15:12:23 +0100320 end;
321 end loop;
Nico Huber83693c82016-10-08 22:17:55 +0200322
Nico Huberb0bbdbc2019-09-27 22:32:21 +0200323 -- switch CDClk if necessary and possible, limit dotclocks accordingly
324 if Update_CDClk then
325 Power_And_Clocks.Update_CDClk (New_Configs);
326 end if;
327
Nico Huberb56b9c52017-01-11 15:12:23 +0100328 -- enable all pipes that changed and should be active
329 for Pipe in Pipe_Index loop
330 declare
331 Success : Boolean;
332 Cur_Config : Pipe_Config renames Cur_Configs (Pipe);
Nico Huber9a4c4c32019-09-16 22:05:11 +0200333 New_Config : Pipe_Config renames New_Configs (Pipe);
Nico Huberb56b9c52017-01-11 15:12:23 +0100334 begin
Nico Huber9a4c4c32019-09-16 22:05:11 +0200335 -- full update
Nico Huber3d06de82018-05-29 01:35:04 +0200336 if New_Config.Port /= Disabled and
337 Full_Update (Cur_Config, New_Config)
Nico Huberb56b9c52017-01-11 15:12:23 +0100338 then
Nico Huber9a4c4c32019-09-16 22:05:11 +0200339 Power_And_Clocks.Power_Up (Old_Configs, New_Configs);
340 Update_Power := True;
Nico Huberc7a4fee2016-11-03 18:18:03 +0100341
Nico Huber9a4c4c32019-09-16 22:05:11 +0200342 Enable_Output (Pipe, New_Config, Success);
Nico Huber83693c82016-10-08 22:17:55 +0200343 if Success then
Nico Huberb56b9c52017-01-11 15:12:23 +0100344 Cur_Config := New_Config;
Nico Huber83693c82016-10-08 22:17:55 +0200345 end if;
Nico Huber3be61d42017-01-09 13:58:18 +0100346
Nico Huberb56b9c52017-01-11 15:12:23 +0100347 -- update framebuffer offset only
348 elsif New_Config.Port /= Disabled and
Nico Huberf361ec82018-06-02 18:01:45 +0200349 Cur_Config.Framebuffer /= New_Config.Framebuffer
Nico Huberb56b9c52017-01-11 15:12:23 +0100350 then
Nico Huber9a4c4c32019-09-16 22:05:11 +0200351 Display_Controller.Setup_FB
352 (Pipe, New_Config.Mode, New_Config.Framebuffer);
353 Display_Controller.Update_Cursor
354 (Pipe, New_Config.Framebuffer, New_Config.Cursor);
355 Cur_Config := New_Config;
Nico Huberb56b9c52017-01-11 15:12:23 +0100356 end if;
357 end;
Nico Huber83693c82016-10-08 22:17:55 +0200358 end loop;
359
Nico Huber9a4c4c32019-09-16 22:05:11 +0200360 if Update_Power then
361 Power_And_Clocks.Power_Down (Old_Configs, New_Configs, Cur_Configs);
Nico Huber83693c82016-10-08 22:17:55 +0200362 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200363 end Update_Outputs;
364
365 ----------------------------------------------------------------------------
366
Nico Huber15ffc4f2018-01-11 14:44:43 +0100367 procedure Update_Cursor (Pipe : Pipe_Index; Cursor : Cursor_Type)
368 is
369 begin
370 Cur_Configs (Pipe).Cursor := Cursor;
371 Display_Controller.Update_Cursor
372 (Pipe, Cur_Configs (Pipe).Framebuffer, Cur_Configs (Pipe).Cursor);
373 end Update_Cursor;
374
375 procedure Place_Cursor
376 (Pipe : Pipe_Index;
377 X : Cursor_Pos;
378 Y : Cursor_Pos)
379 is
380 begin
381 Cur_Configs (Pipe).Cursor.Center_X := X;
382 Cur_Configs (Pipe).Cursor.Center_Y := Y;
383 Display_Controller.Place_Cursor
384 (Pipe, Cur_Configs (Pipe).Framebuffer, Cur_Configs (Pipe).Cursor);
385 end Place_Cursor;
386
387 procedure Move_Cursor
388 (Pipe : Pipe_Index;
389 X : Cursor_Pos;
390 Y : Cursor_Pos)
391 is
392 function Cap_Add (A, B : Cursor_Pos) return Cursor_Pos is
393 (if A + B < 0
394 then Int32'Max (Cursor_Pos'First, A + B)
395 else Int32'Min (Cursor_Pos'Last, A + B));
396 begin
397 Place_Cursor
398 (Pipe => Pipe,
399 X => Cap_Add (Cur_Configs (Pipe).Cursor.Center_X, X),
400 Y => Cap_Add (Cur_Configs (Pipe).Cursor.Center_Y, Y));
401 end Move_Cursor;
402
403 ----------------------------------------------------------------------------
404
Nico Huber83693c82016-10-08 22:17:55 +0200405 procedure Initialize
Nico Huber2b6f6992017-07-09 18:11:34 +0200406 (Write_Delay : in Word64 := 0;
Nico Huber793a8d42016-11-21 18:57:03 +0100407 Clean_State : in Boolean := False;
Nico Huber83693c82016-10-08 22:17:55 +0200408 Success : out Boolean)
409 with
410 Refined_Global =>
Nico Huber27088aa2018-06-10 13:28:05 +0200411 (Input => (Time.State),
412 In_Out => (Dev.PCI_State, Registers.Register_State, Port_IO.State),
Nico Huber83693c82016-10-08 22:17:55 +0200413 Output =>
Nico Huberc5c66ec2019-09-28 23:59:45 +0200414 (PCI_Usable,
415 Config.Variable,
Nico Huber27088aa2018-06-10 13:28:05 +0200416 Dev.Address_State,
Nico Huber2b6f6992017-07-09 18:11:34 +0200417 Registers.Address_State,
Nico Huber312433c2019-09-28 03:15:48 +0200418 PCode.Mailbox_Ready,
Nico Huber83693c82016-10-08 22:17:55 +0200419 PLLs.State, Panel.Panel_State,
Nico Huber1a712d32017-01-09 15:11:04 +0100420 Cur_Configs, Allocated_PLLs,
Nico Huberc3f66f62017-07-16 21:39:54 +0200421 HPD_Delay, Wait_For_HPD,
422 Linear_FB_Base, Initialized))
Nico Huber83693c82016-10-08 22:17:55 +0200423 is
424 use type HW.Word64;
425
Nico Huber0b2329a2018-06-09 21:14:27 +0200426 function MMIO_GTT_Offset return Natural is
427 (if Config.Has_64bit_GTT
428 then Registers.MMIO_GTT_64_Offset
429 else Registers.MMIO_GTT_32_Offset);
Nico Huber2b6f6992017-07-09 18:11:34 +0200430 PCI_MMIO_Base, PCI_GTT_Base : Word64;
431
Nico Huber83693c82016-10-08 22:17:55 +0200432 Now : constant Time.T := Time.Now;
433
434 procedure Check_Platform (Success : out Boolean)
435 is
436 Audio_VID_DID : Word32;
437 begin
Nico Huber6621a142018-06-07 23:56:54 +0200438 case Config.Gen is
Arthur Heymans73ea0322018-03-28 17:17:07 +0200439 when G45 =>
440 Registers.Read (Registers.G4X_AUD_VID_DID, Audio_VID_DID);
Nico Huber6621a142018-06-07 23:56:54 +0200441 when Ironlake =>
442 Registers.Read (Registers.PCH_AUD_VID_DID, Audio_VID_DID);
Nico Huber83693c82016-10-08 22:17:55 +0200443 when Haswell .. Skylake =>
444 Registers.Read (Registers.AUD_VID_DID, Audio_VID_DID);
Nico Huber83693c82016-10-08 22:17:55 +0200445 end case;
446 Success :=
Nico Huber998ee2b2018-06-12 23:02:17 +0200447 ((Config.Gen_Broxton and Audio_VID_DID = 16#8086_280a#) or
Nico Huber88badbe2018-09-27 16:36:47 +0200448 (Config.CPU_Kabylake and Audio_VID_DID = 16#8086_280b#) or
449 (Config.CPU_Skylake and Audio_VID_DID = 16#8086_2809#) or
Nico Huber998ee2b2018-06-12 23:02:17 +0200450 (Config.CPU_Broadwell and Audio_VID_DID = 16#8086_2808#) or
451 (Config.CPU_Haswell and Audio_VID_DID = 16#8086_2807#) or
452 ((Config.CPU_Ivybridge or
453 Config.CPU_Sandybridge) and (Audio_VID_DID = 16#8086_2806# or
454 Audio_VID_DID = 16#8086_2805#)) or
455 (Config.CPU_Ironlake and Audio_VID_DID = 16#0000_0000#) or
456 (Config.Gen_G45 and (Audio_VID_DID = 16#8086_2801# or
457 Audio_VID_DID = 16#8086_2802# or
458 Audio_VID_DID = 16#8086_2803#)));
Nico Huber83693c82016-10-08 22:17:55 +0200459 end Check_Platform;
Nico Hubere7ac6eb2017-09-04 23:54:13 +0200460
461 procedure Check_Platform_PCI (Success : out Boolean)
462 is
463 use type HW.Word16;
464 Vendor, Device : Word16;
465 begin
466 Dev.Read16 (Vendor, PCI.Vendor_Id);
467 Dev.Read16 (Device, PCI.Device_Id);
468
Nico Huber6a996dc2018-06-17 16:30:33 +0200469 Config.Detect_CPU (Device);
Nico Hubere7ac6eb2017-09-04 23:54:13 +0200470 Success := Vendor = 16#8086# and Config.Compatible_GPU (Device);
471 end Check_Platform_PCI;
Nico Huber83693c82016-10-08 22:17:55 +0200472 begin
Nico Huber83693c82016-10-08 22:17:55 +0200473 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
474
475 pragma Debug (Debug.Set_Register_Write_Delay (Write_Delay));
476
Nico Huberc5c66ec2019-09-28 23:59:45 +0200477 PCI_Usable := False;
Nico Huberc3f66f62017-07-16 21:39:54 +0200478 Linear_FB_Base := 0;
Nico Huber312433c2019-09-28 03:15:48 +0200479 PCode.Mailbox_Ready := False;
Nico Huber83693c82016-10-08 22:17:55 +0200480 Wait_For_HPD := HPD_Type'(others => False);
481 HPD_Delay := HPD_Delay_Type'(others => Now);
Nico Huber83693c82016-10-08 22:17:55 +0200482 Allocated_PLLs := (others => PLLs.Invalid);
Nico Huber99f10f32016-11-20 00:34:05 +0100483 Cur_Configs := Pipe_Configs'
484 (others => Pipe_Config'
Nico Huber83693c82016-10-08 22:17:55 +0200485 (Port => Disabled,
486 Framebuffer => HW.GFX.Default_FB,
Nico Hubera02b2c62018-01-09 15:58:34 +0100487 Cursor => Default_Cursor,
Nico Huber83693c82016-10-08 22:17:55 +0200488 Mode => HW.GFX.Invalid_Mode));
Nico Huber27088aa2018-06-10 13:28:05 +0200489 Config.Variable := Config.Initial_Settings;
Nico Huber6a996dc2018-06-17 16:30:33 +0200490 Registers.Set_Register_Base (Config.Default_MMIO_Base);
Nico Huber83693c82016-10-08 22:17:55 +0200491 PLLs.Initialize;
492
Nico Huber2b6f6992017-07-09 18:11:34 +0200493 Dev.Initialize (Success);
494
495 if Success then
Nico Huber6a996dc2018-06-17 16:30:33 +0200496 Check_Platform_PCI (Success);
Nico Hubere7ac6eb2017-09-04 23:54:13 +0200497 if Success then
Nico Huber6a996dc2018-06-17 16:30:33 +0200498 Dev.Map (PCI_MMIO_Base, PCI.Res0, Length => MMIO_GTT_Offset);
499 Dev.Map (PCI_GTT_Base, PCI.Res0, Offset => MMIO_GTT_Offset);
500 if PCI_MMIO_Base /= 0 and PCI_GTT_Base /= 0 then
501 Registers.Set_Register_Base (PCI_MMIO_Base, PCI_GTT_Base);
Nico Huberc5c66ec2019-09-28 23:59:45 +0200502 PCI_Usable := True;
Nico Huber6a996dc2018-06-17 16:30:33 +0200503 else
504 pragma Debug (Debug.Put_Line
505 ("ERROR: Couldn't map resoure0."));
506 Success := Config.Default_MMIO_Base_Set;
507 end if;
Nico Hubere7ac6eb2017-09-04 23:54:13 +0200508 end if;
Nico Huber2b6f6992017-07-09 18:11:34 +0200509 else
510 pragma Debug (Debug.Put_Line
511 ("WARNING: Couldn't initialize PCI dev."));
Nico Huber2b6f6992017-07-09 18:11:34 +0200512 Success := Config.Default_MMIO_Base_Set;
Nico Huber2b6f6992017-07-09 18:11:34 +0200513
Nico Hubere7ac6eb2017-09-04 23:54:13 +0200514 if Success then
515 Check_Platform (Success);
516 end if;
Nico Huber2b6f6992017-07-09 18:11:34 +0200517 end if;
518
Nico Huber5dbaf4b2020-01-08 17:24:58 +0100519 Panel.Static_Init; -- early for flow analysis
520
Nico Huber83693c82016-10-08 22:17:55 +0200521 if not Success then
522 pragma Debug (Debug.Put_Line ("ERROR: Incompatible CPU or PCH."));
523
Nico Huber83693c82016-10-08 22:17:55 +0200524 Initialized := False;
525 return;
526 end if;
527
528 Panel.Setup_PP_Sequencer;
529 Port_Detect.Initialize;
Nico Huber0923b792017-06-09 15:28:41 +0200530 Connectors.Initialize;
Nico Huber83693c82016-10-08 22:17:55 +0200531
Nico Huber793a8d42016-11-21 18:57:03 +0100532 if Clean_State then
533 Power_And_Clocks.Pre_All_Off;
534 Connectors.Pre_All_Off;
535 Display_Controller.All_Off;
536 Connectors.Post_All_Off;
537 PLLs.All_Off;
538 Power_And_Clocks.Post_All_Off;
Nico Huber17d64b62017-07-15 20:51:25 +0200539 Registers.Clear_Fences;
Nico Huber33912aa2016-12-06 20:36:23 +0100540 else
541 -- According to PRMs, VGA plane is the only thing
Nico Huber3a0e2a02017-07-19 14:41:46 +0200542 -- that's enabled by default after reset...
Nico Huber33912aa2016-12-06 20:36:23 +0100543 Display_Controller.Legacy_VGA_Off;
Nico Huber3a0e2a02017-07-19 14:41:46 +0200544 -- ... along with some DDI port bits since Skylake.
545 Connectors.Post_Reset_Off;
Nico Huber793a8d42016-11-21 18:57:03 +0100546 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200547
548 -------------------- Now restart from a clean state ---------------------
549 Power_And_Clocks.Initialize;
550
Nico Huber1c3b9282017-02-09 13:57:04 +0100551 if Config.Has_PCH then
552 Registers.Unset_And_Set_Mask
553 (Register => Registers.PCH_RAWCLK_FREQ,
554 Mask_Unset => PCH_RAWCLK_FREQ_MASK,
Nico Huberc9ad9de2020-12-20 02:34:37 +0100555 Mask_Set => PCH_RAWCLK_FREQ (Config.Raw_Clock));
Nico Huber1c3b9282017-02-09 13:57:04 +0100556 end if;
Nico Huberf54d0962016-10-20 14:17:18 +0200557
Nico Huber83693c82016-10-08 22:17:55 +0200558 Initialized := True;
559
560 end Initialize;
561
562 function Is_Initialized return Boolean
563 with
564 Refined_Post => Is_Initialized'Result = Initialized
565 is
566 begin
567 return Initialized;
568 end Is_Initialized;
569
570 ----------------------------------------------------------------------------
571
Nico Hubercf88f3d2018-06-05 13:27:34 +0200572 pragma Warnings
573 (GNATprove, Off, """Registers.Register_State"" * is not modified*",
Nico Huberadfe11f2018-06-10 14:59:04 +0200574 Reason => "Power_Up_VGA is only effective in certain configurations.");
Nico Huber42fb2d02017-09-01 17:01:51 +0200575 procedure Power_Up_VGA
Nico Hubercf88f3d2018-06-05 13:27:34 +0200576 with
577 Refined_Global =>
Nico Huberadfe11f2018-06-10 14:59:04 +0200578 (Input => (Cur_Configs, Config.Variable, Time.State),
Nico Hubercf88f3d2018-06-05 13:27:34 +0200579 In_Out => (Registers.Register_State),
580 Proof_In => (Initialized))
Nico Huber42fb2d02017-09-01 17:01:51 +0200581 is
582 Fake_Config : constant Pipe_Configs :=
583 (Primary =>
584 (Port => Analog,
585 Framebuffer => HW.GFX.Default_FB,
Nico Hubera02b2c62018-01-09 15:58:34 +0100586 Cursor => Default_Cursor,
Nico Huber42fb2d02017-09-01 17:01:51 +0200587 Mode => HW.GFX.Invalid_Mode),
588 others =>
589 (Port => Disabled,
590 Framebuffer => HW.GFX.Default_FB,
Nico Hubera02b2c62018-01-09 15:58:34 +0100591 Cursor => Default_Cursor,
Nico Huber42fb2d02017-09-01 17:01:51 +0200592 Mode => HW.GFX.Invalid_Mode));
593 begin
594 Power_And_Clocks.Power_Up (Cur_Configs, Fake_Config);
595 end Power_Up_VGA;
Nico Hubercf88f3d2018-06-05 13:27:34 +0200596 pragma Warnings
597 (GNATprove, Off, "no check message justified*", Reason => "see below");
598 pragma Annotate
599 (GNATprove, Intentional, "unused global",
Nico Huberadfe11f2018-06-10 14:59:04 +0200600 "Power_Up_VGA is only effective in certain configurations.");
Nico Hubercf88f3d2018-06-05 13:27:34 +0200601 pragma Warnings (GNATprove, On, "no check message justified*");
602 pragma Warnings
603 (GNATprove, On, """Registers.Register_State"" * is not modified*");
Nico Huber42fb2d02017-09-01 17:01:51 +0200604
605 ----------------------------------------------------------------------------
606
Nico Huber5374c3a2017-07-15 21:48:06 +0200607 function FB_First_Page (FB : Framebuffer_Type) return Natural is
Nico Huber34be6542017-12-13 09:26:24 +0100608 (Natural (Phys_Offset (FB) / GTT_Page_Size));
Nico Huber5374c3a2017-07-15 21:48:06 +0200609 function FB_Pages (FB : Framebuffer_Type) return Natural is
610 (Natural (Div_Round_Up (FB_Size (FB), GTT_Page_Size)));
611 function FB_Last_Page (FB : Framebuffer_Type) return Natural is
612 (FB_First_Page (FB) + FB_Pages (FB) - 1);
613
Nico Huber34be6542017-12-13 09:26:24 +0100614 -- Check basics and that it fits in GTT. For 90 degree rotations,
615 -- the Offset should be above GTT_Rotation_Offset. The latter will
616 -- be subtracted for the aperture mapping.
Nico Huber5374c3a2017-07-15 21:48:06 +0200617 function Valid_FB (FB : Framebuffer_Type) return Boolean is
Nico Huber34be6542017-12-13 09:26:24 +0100618 (Valid_Stride (FB) and
619 FB_First_Page (FB) in GTT_Range and
Nico Huber2e87c0d2020-04-18 00:46:39 +0200620 FB_Last_Page (FB) + 128 in GTT_Range and
Nico Huber34be6542017-12-13 09:26:24 +0100621 (not Rotation_90 (FB) or
Nico Huber2e87c0d2020-04-18 00:46:39 +0200622 (FB_First_Page (FB) mod 64 = 0 and
623 FB_Last_Page (FB) + 128 + GTT_Rotation_Offset in GTT_Range and
Nico Huber34be6542017-12-13 09:26:24 +0100624 FB.Offset >= Word32 (GTT_Rotation_Offset) * GTT_Page_Size)));
Nico Huber5374c3a2017-07-15 21:48:06 +0200625
626 -- Also check that we don't overflow the GTT's 39-bit space
627 -- (always true with a 32-bit base)
628 function Valid_Phys_FB (FB : Framebuffer_Type; Phys_Base : Word32)
629 return Boolean is
630 (Valid_FB (FB) and
Nico Huber34be6542017-12-13 09:26:24 +0100631 Int64 (Phys_Base) + Int64 (Phys_Offset (FB)) + Int64 (FB_Size (FB)) <=
Nico Huber5374c3a2017-07-15 21:48:06 +0200632 Int64 (GTT_Address_Type'Last))
633 with
634 Ghost;
635
Nico Huber83693c82016-10-08 22:17:55 +0200636 procedure Write_GTT
637 (GTT_Page : GTT_Range;
638 Device_Address : GTT_Address_Type;
Nico Huber5374c3a2017-07-15 21:48:06 +0200639 Valid : Boolean)
640 is
Nico Huber83693c82016-10-08 22:17:55 +0200641 begin
642 Registers.Write_GTT (GTT_Page, Device_Address, Valid);
643 end Write_GTT;
644
Nico Huberceda17d2018-06-09 22:00:29 +0200645 procedure Read_GTT
646 (Device_Address : out GTT_Address_Type;
647 Valid : out Boolean;
648 GTT_Page : in GTT_Range)
649 is
650 begin
651 Registers.Read_GTT (Device_Address, Valid, GTT_Page);
652 end Read_GTT;
653
Nico Huber194e57e2017-07-15 21:15:46 +0200654 procedure Setup_Default_GTT (FB : Framebuffer_Type; Phys_Base : Word32)
Nico Huber5374c3a2017-07-15 21:48:06 +0200655 with
656 Pre => Is_Initialized and Valid_Phys_FB (FB, Phys_Base)
Nico Huber83693c82016-10-08 22:17:55 +0200657 is
Nico Huber194e57e2017-07-15 21:15:46 +0200658 Phys_Addr : GTT_Address_Type :=
Nico Huber34be6542017-12-13 09:26:24 +0100659 GTT_Address_Type (Phys_Base) + GTT_Address_Type (Phys_Offset (FB));
Nico Huber83693c82016-10-08 22:17:55 +0200660 begin
Nico Huber194e57e2017-07-15 21:15:46 +0200661 for Idx in FB_First_Page (FB) .. FB_Last_Page (FB) loop
Nico Huber83693c82016-10-08 22:17:55 +0200662 Registers.Write_GTT
663 (GTT_Page => Idx,
664 Device_Address => Phys_Addr,
665 Valid => True);
Nico Huber194e57e2017-07-15 21:15:46 +0200666 Phys_Addr := Phys_Addr + GTT_Page_Size;
Nico Huber83693c82016-10-08 22:17:55 +0200667 end loop;
Nico Huber2e87c0d2020-04-18 00:46:39 +0200668 -- Add another 128 dummy pages to work around buggy VT-d
669 for Idx in FB_Last_Page (FB) + 1 .. FB_Last_Page (FB) + 128 loop
670 Registers.Write_GTT (Idx, Phys_Addr, True);
671 end loop;
Nico Huber9b479412017-08-27 11:55:56 +0200672
673 if Rotation_90 (FB) and FB.Tiling = Y_Tiled and FB.V_Stride >= 32 then
674 declare
675 V_Pages : constant Natural := Natural (FB.V_Stride) / 32;
676 Bytes_Per_Row : constant GTT_Address_Type :=
677 GTT_Address_Type (Pixel_To_Bytes (32 * FB.Stride, FB));
678 begin
679 Phys_Addr := GTT_Address_Type (Phys_Base) +
Nico Huber34be6542017-12-13 09:26:24 +0100680 GTT_Address_Type (Phys_Offset (FB)) +
Nico Huber9b479412017-08-27 11:55:56 +0200681 GTT_Address_Type (FB_Size (FB));
682 for Page in FB_First_Page (FB) .. FB_Last_Page (FB) loop
683 Phys_Addr := Phys_Addr - Bytes_Per_Row;
684 Registers.Write_GTT
685 (GTT_Page => GTT_Rotation_Offset + Page,
686 Device_Address => Phys_Addr,
687 Valid => True);
688
689 if (Page - FB_First_Page (FB) + 1) mod V_Pages = 0 then
690 Phys_Addr := Phys_Addr + GTT_Page_Size +
691 GTT_Address_Type (V_Pages) * Bytes_Per_Row;
692 end if;
693 end loop;
694 end;
Nico Huber2e87c0d2020-04-18 00:46:39 +0200695 -- Add another 128 dummy pages to work around buggy VT-d
696 for Idx in FB_Last_Page (FB) + 1 .. FB_Last_Page (FB) + 128 loop
697 Registers.Write_GTT (GTT_Rotation_Offset + Idx, Phys_Addr, True);
698 end loop;
Nico Huber9b479412017-08-27 11:55:56 +0200699 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200700 end Setup_Default_GTT;
701
702 ----------------------------------------------------------------------------
703
Nico Hubereedde882017-07-16 02:54:39 +0200704 use type HW.Word16;
705 subtype Stolen_Size_Range is Int64 range 0 .. 2 ** 33;
706
707 function GGMS_Gen4 (GGC : Word16) return Natural is
708 (Natural (Shift_Right (GGC, 8) and 16#07#));
709 function GTT_Size_Gen4 (GGC : Word16) return Natural is
710 (if GGMS_Gen4 (GGC) in 1 .. 3 then
711 (GGMS_Gen4 (GGC) + 1) * 2 ** 19 else 0);
712
713 function GMS_Gen4 (GGC : Word16) return Natural is
714 (Natural (Shift_Right (GGC, 4) and 16#0f#));
715 Valid_Stolen_Size_Gen4 : constant
716 array (Natural range 1 .. 13) of Stolen_Size_Range :=
717 (1, 4, 8, 16, 32, 48, 64, 128, 256, 96, 160, 224, 352);
718 function Stolen_Size_Gen4 (GGC : Word16) return Stolen_Size_Range is
719 (if GMS_Gen4 (GGC) in Valid_Stolen_Size_Gen4'Range then
Arthur Heymans5fd9a312017-09-12 12:45:18 +0200720 Valid_Stolen_Size_Gen4 (GMS_Gen4 (GGC)) * 2 ** 20 else 0);
Nico Hubereedde882017-07-16 02:54:39 +0200721
722 function GTT_Size_Gen6 (GGC : Word16) return Natural is
723 (Natural (Shift_Right (GGC, 8) and 16#03#) * 2 ** 20);
724
725 function Stolen_Size_Gen6 (GGC : Word16) return Stolen_Size_Range is
726 (Stolen_Size_Range (Shift_Right (GGC, 3) and 16#1f#) * 32 * 2 ** 20);
727
Nico Huberfe7985f2019-10-12 22:19:24 +0200728 function GGMS_Gen8 (GGC : Word16) return Natural is
729 (Natural (Shift_Right (GGC, 6) and 16#03#));
Nico Hubereedde882017-07-16 02:54:39 +0200730 function GTT_Size_Gen8 (GGC : Word16) return Natural is
Nico Huberfe7985f2019-10-12 22:19:24 +0200731 (if GGMS_Gen8 (GGC) /= 0 then
732 Natural (Shift_Left (Word32'(1), 20 + GGMS_Gen8 (GGC))) else 0);
Nico Hubereedde882017-07-16 02:54:39 +0200733
734 function GMS_Gen8 (GGC : Word16) return Stolen_Size_Range is
735 (Stolen_Size_Range (Shift_Right (GGC, 8) and 16#ff#));
736 function Stolen_Size_Gen8 (GGC : Word16) return Stolen_Size_Range is
737 (GMS_Gen8 (GGC) * 32 * 2 ** 20);
738
739 function Stolen_Size_Gen9 (GGC : Word16) return Stolen_Size_Range is
740 (if GMS_Gen8 (GGC) < 16#f0# then
741 Stolen_Size_Gen8 (GGC)
742 else
743 (GMS_Gen8 (GGC) - 16#f0# + 1) * 4 * 2 ** 20);
744
745 procedure Decode_Stolen
746 (GTT_Size : out Natural;
747 Stolen_Size : out Stolen_Size_Range)
748 with
749 Pre => Is_Initialized
750 is
Nico Huber63ec8362018-06-09 17:42:19 +0200751 GGC_Reg : constant PCI.Index :=
Nico Huber998ee2b2018-06-12 23:02:17 +0200752 (if Config.Gen_G45 or Config.CPU_Ironlake then 16#52# else 16#50#);
Nico Hubereedde882017-07-16 02:54:39 +0200753 GGC : Word16;
754 begin
755 Dev.Read16 (GGC, GGC_Reg);
Nico Huber998ee2b2018-06-12 23:02:17 +0200756 if Config.Gen_G45 or Config.CPU_Ironlake then
757 GTT_Size := GTT_Size_Gen4 (GGC);
758 Stolen_Size := Stolen_Size_Gen4 (GGC);
759 elsif Config.CPU_Sandybridge or Config.CPU_Ivybridge or Config.CPU_Haswell
760 then
761 GTT_Size := GTT_Size_Gen6 (GGC);
762 Stolen_Size := Stolen_Size_Gen6 (GGC);
763 elsif Config.CPU_Broadwell then
764 GTT_Size := GTT_Size_Gen8 (GGC);
765 Stolen_Size := Stolen_Size_Gen8 (GGC);
766 else
767 GTT_Size := GTT_Size_Gen8 (GGC);
768 Stolen_Size := Stolen_Size_Gen9 (GGC);
769 end if;
Nico Hubereedde882017-07-16 02:54:39 +0200770 end Decode_Stolen;
771
772 -- Additional runtime validation that FB fits stolen memory and aperture.
773 procedure Validate_FB (FB : Framebuffer_Type; Valid : out Boolean)
774 with
775 Pre => Is_Initialized,
776 Post => (if Valid then Valid_FB (FB))
777 is
Nico Huber2e87c0d2020-04-18 00:46:39 +0200778 GTT_Off : constant Natural :=
779 (if Rotation_90 (FB) then GTT_Rotation_Offset else 0);
780
Nico Hubereedde882017-07-16 02:54:39 +0200781 GTT_Size, Aperture_Size : Natural;
782 Stolen_Size : Stolen_Size_Range;
783 begin
784 Valid := Valid_FB (FB);
785
786 if Valid then
787 Decode_Stolen (GTT_Size, Stolen_Size);
788 Dev.Resource_Size (Aperture_Size, PCI.Res2);
789 Valid :=
Nico Huber2e87c0d2020-04-18 00:46:39 +0200790 FB_Last_Page (FB) + 128 + GTT_Off < GTT_Size / Config.GTT_PTE_Size
791 and
792 FB_Last_Page (FB) < Natural (Stolen_Size / GTT_Page_Size)
793 and
Nico Hubereedde882017-07-16 02:54:39 +0200794 FB_Last_Page (FB) < Aperture_Size / GTT_Page_Size;
Nico Huber34be6542017-12-13 09:26:24 +0100795 pragma Debug (not Valid, Debug.Put_Line
Nico Hubereedde882017-07-16 02:54:39 +0200796 ("Stolen memory too small to hold framebuffer."));
797 end if;
798 end Validate_FB;
799
Nico Huber5374c3a2017-07-15 21:48:06 +0200800 procedure Setup_Default_FB
801 (FB : in Framebuffer_Type;
802 Clear : in Boolean := True;
803 Success : out Boolean)
804 is
805 GMA_Phys_Base : constant PCI.Index := 16#5c#;
806 GMA_Phys_Base_Mask : constant := 16#fff0_0000#;
807
808 Phys_Base : Word32;
809 begin
Nico Hubereedde882017-07-16 02:54:39 +0200810 Validate_FB (FB, Success);
Nico Huber5374c3a2017-07-15 21:48:06 +0200811
812 if Success then
813 Dev.Read32 (Phys_Base, GMA_Phys_Base);
814 Phys_Base := Phys_Base and GMA_Phys_Base_Mask;
815 Success := Phys_Base /= GMA_Phys_Base_Mask and Phys_Base /= 0;
816 pragma Debug (not Success, Debug.Put_Line
817 ("Failed to read stolen memory base."));
Nico Huber0164b022017-08-24 15:12:51 +0200818
819 if Success then
820 if FB.Tiling in XY_Tiling then
821 Registers.Add_Fence
822 (First_Page => FB_First_Page (FB),
823 Last_Page => FB_Last_Page (FB),
824 Tiling => FB.Tiling,
825 Pitch => FB_Pitch (FB.Stride, FB),
826 Success => Success);
827 end if;
828 pragma Debug (not Success, Debug.Put_Line
829 ("Tiled framebuffer but no fence regs available."));
830 end if;
831
Nico Huber5374c3a2017-07-15 21:48:06 +0200832 if Success then
833 Setup_Default_GTT (FB, Phys_Base);
834 end if;
835 end if;
836
837 if Success and then Clear then
838 declare
839 use type HW.Word64;
840 Linear_FB : Word64;
841 begin
Nico Huberc3f66f62017-07-16 21:39:54 +0200842 Map_Linear_FB (Linear_FB, FB);
Nico Huber5374c3a2017-07-15 21:48:06 +0200843 if Linear_FB /= 0 then
Nico Huberc3f66f62017-07-16 21:39:54 +0200844 Framebuffer_Filler.Fill (Linear_FB, FB);
Nico Huber5374c3a2017-07-15 21:48:06 +0200845 end if;
Nico Huber5374c3a2017-07-15 21:48:06 +0200846 end;
847 end if;
848 end Setup_Default_FB;
849
Nico Huberc3f66f62017-07-16 21:39:54 +0200850 procedure Map_Linear_FB (Linear_FB : out Word64; FB : in Framebuffer_Type)
851 is
852 use type HW.Word64;
853
854 Valid : Boolean;
855 begin
856 Linear_FB := 0;
857
858 if Linear_FB_Base = 0 then
859 Dev.Map (Linear_FB_Base, PCI.Res2);
860 pragma Debug
861 (Linear_FB_Base = 0, Debug.Put_Line ("Failed to map resource2."));
862 end if;
863
864 if Linear_FB_Base /= 0 then
865 Validate_FB (FB, Valid);
866 if Valid then
Nico Huber34be6542017-12-13 09:26:24 +0100867 Linear_FB := Linear_FB_Base + Word64 (Phys_Offset (FB));
Nico Huberc3f66f62017-07-16 21:39:54 +0200868 end if;
869 end if;
870 end Map_Linear_FB;
871
Nico Huber5374c3a2017-07-15 21:48:06 +0200872 ----------------------------------------------------------------------------
873
Nico Huber99f10f32016-11-20 00:34:05 +0100874 procedure Dump_Configs (Configs : Pipe_Configs)
Nico Huber83693c82016-10-08 22:17:55 +0200875 is
876 subtype Pipe_Name is String (1 .. 9);
Nico Huber99f10f32016-11-20 00:34:05 +0100877 type Pipe_Name_Array is array (Pipe_Index) of Pipe_Name;
Nico Huber83693c82016-10-08 22:17:55 +0200878 Pipe_Names : constant Pipe_Name_Array :=
879 (Primary => "Primary ",
880 Secondary => "Secondary",
881 Tertiary => "Tertiary ");
Nico Huber5ef4d602017-12-13 13:56:47 +0100882
883 subtype Tiling_Name is String (1 .. 7);
884 type Tiling_Name_Array is array (Tiling_Type) of Tiling_Name;
885 Tilings : constant Tiling_Name_Array :=
886 (Linear => "Linear ",
887 X_Tiled => "X_Tiled",
888 Y_Tiled => "Y_Tiled");
889
890 subtype Rotation_Name is String (1 .. 11);
891 type Rotation_Name_Array is array (Rotation_Type) of Rotation_Name;
892 Rotations : constant Rotation_Name_Array :=
893 (No_Rotation => "No_Rotation",
894 Rotated_90 => "Rotated_90 ",
895 Rotated_180 => "Rotated_180",
896 Rotated_270 => "Rotated_270");
Nico Huber83693c82016-10-08 22:17:55 +0200897 begin
898 Debug.New_Line;
Paul Menzelb83107c2017-05-04 09:02:33 +0200899 Debug.Put_Line ("CONFIG =>");
Nico Huber99f10f32016-11-20 00:34:05 +0100900 for Pipe in Pipe_Index loop
901 if Pipe = Pipe_Index'First then
Nico Huber83693c82016-10-08 22:17:55 +0200902 Debug.Put (" (");
903 else
904 Debug.Put (" ");
905 end if;
906 Debug.Put_Line (Pipe_Names (Pipe) & " =>");
907 Debug.Put_Line
908 (" (Port => " & Port_Names (Configs (Pipe).Port) & ",");
909 Debug.Put_Line (" Framebuffer =>");
Nico Huber5ef4d602017-12-13 13:56:47 +0100910 Debug.Put (" (Width => ");
Nico Huber83693c82016-10-08 22:17:55 +0200911 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Width);
912 Debug.Put_Line (",");
Nico Huber5ef4d602017-12-13 13:56:47 +0100913 Debug.Put (" Height => ");
Nico Huber83693c82016-10-08 22:17:55 +0200914 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Height);
915 Debug.Put_Line (",");
Nico Huber5ef4d602017-12-13 13:56:47 +0100916 Debug.Put (" Start_X => ");
917 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Start_X);
918 Debug.Put_Line (",");
919 Debug.Put (" Start_Y => ");
920 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Start_Y);
921 Debug.Put_Line (",");
922 Debug.Put (" Stride => ");
Nico Huber83693c82016-10-08 22:17:55 +0200923 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Stride);
924 Debug.Put_Line (",");
Nico Huber5ef4d602017-12-13 13:56:47 +0100925 Debug.Put (" V_Stride => ");
926 Debug.Put_Int32 (Configs (Pipe).Framebuffer.V_Stride);
927 Debug.Put_Line (",");
928 Debug.Put (" Tiling => ");
929 Debug.Put_Line (Tilings (Configs (Pipe).Framebuffer.Tiling) & ",");
930 Debug.Put (" Rotation => ");
931 Debug.Put_Line (Rotations (Configs (Pipe).Framebuffer.Rotation) & ",");
Nico Huber83693c82016-10-08 22:17:55 +0200932 Debug.Put (" Offset => ");
933 Debug.Put_Word32 (Configs (Pipe).Framebuffer.Offset);
934 Debug.Put_Line (",");
935 Debug.Put (" BPC => ");
936 Debug.Put_Int64 (Configs (Pipe).Framebuffer.BPC);
937 Debug.Put_Line ("),");
938 Debug.Put_Line (" Mode =>");
939 Debug.Put (" (Dotclock => ");
940 Debug.Put_Int64 (Configs (Pipe).Mode.Dotclock);
941 Debug.Put_Line (",");
942 Debug.Put (" H_Visible => ");
Nico Huberc5c767a2018-06-03 01:09:04 +0200943 Debug.Put_Int32 (Configs (Pipe).Mode.H_Visible);
Nico Huber83693c82016-10-08 22:17:55 +0200944 Debug.Put_Line (",");
945 Debug.Put (" H_Sync_Begin => ");
Nico Huberc5c767a2018-06-03 01:09:04 +0200946 Debug.Put_Int32 (Configs (Pipe).Mode.H_Sync_Begin);
Nico Huber83693c82016-10-08 22:17:55 +0200947 Debug.Put_Line (",");
948 Debug.Put (" H_Sync_End => ");
Nico Huberc5c767a2018-06-03 01:09:04 +0200949 Debug.Put_Int32 (Configs (Pipe).Mode.H_Sync_End);
Nico Huber83693c82016-10-08 22:17:55 +0200950 Debug.Put_Line (",");
951 Debug.Put (" H_Total => ");
Nico Huberc5c767a2018-06-03 01:09:04 +0200952 Debug.Put_Int32 (Configs (Pipe).Mode.H_Total);
Nico Huber83693c82016-10-08 22:17:55 +0200953 Debug.Put_Line (",");
954 Debug.Put (" V_Visible => ");
Nico Huberc5c767a2018-06-03 01:09:04 +0200955 Debug.Put_Int32 (Configs (Pipe).Mode.V_Visible);
Nico Huber83693c82016-10-08 22:17:55 +0200956 Debug.Put_Line (",");
957 Debug.Put (" V_Sync_Begin => ");
Nico Huberc5c767a2018-06-03 01:09:04 +0200958 Debug.Put_Int32 (Configs (Pipe).Mode.V_Sync_Begin);
Nico Huber83693c82016-10-08 22:17:55 +0200959 Debug.Put_Line (",");
960 Debug.Put (" V_Sync_End => ");
Nico Huberc5c767a2018-06-03 01:09:04 +0200961 Debug.Put_Int32 (Configs (Pipe).Mode.V_Sync_End);
Nico Huber83693c82016-10-08 22:17:55 +0200962 Debug.Put_Line (",");
963 Debug.Put (" V_Total => ");
Nico Huberc5c767a2018-06-03 01:09:04 +0200964 Debug.Put_Int32 (Configs (Pipe).Mode.V_Total);
Nico Huber83693c82016-10-08 22:17:55 +0200965 Debug.Put_Line (",");
966 Debug.Put_Line (" H_Sync_Active_High => " &
967 (if Configs (Pipe).Mode.H_Sync_Active_High
968 then "True,"
969 else "False,"));
970 Debug.Put_Line (" V_Sync_Active_High => " &
971 (if Configs (Pipe).Mode.V_Sync_Active_High
972 then "True,"
973 else "False,"));
974 Debug.Put (" BPC => ");
975 Debug.Put_Int64 (Configs (Pipe).Mode.BPC);
Nico Huber99f10f32016-11-20 00:34:05 +0100976 if Pipe /= Pipe_Index'Last then
Nico Huber83693c82016-10-08 22:17:55 +0200977 Debug.Put_Line (")),");
978 else
979 Debug.Put_Line (")));");
980 end if;
981 end loop;
982 end Dump_Configs;
983
Nico Huberc5c66ec2019-09-28 23:59:45 +0200984 ----------------------------------------------------------------------------
985
986 procedure PCI_Read16 (Value : out Word16; Offset : HW.PCI.Index) is
987 begin
988 Dev.Read16 (Value, Offset);
989 end PCI_Read16;
990
Nico Huber83693c82016-10-08 22:17:55 +0200991end HW.GFX.GMA;