blob: a4445baaa8254987c4ae8703759a11e371b99572 [file] [log] [blame]
Nico Huber83693c82016-10-08 22:17:55 +02001--
Nico Huber3be61d42017-01-09 13:58:18 +01002-- Copyright (C) 2014-2017 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 Huber5374c3a2017-07-15 21:48:06 +020021with HW.GFX.Framebuffer_Filler;
22
Nico Huber83693c82016-10-08 22:17:55 +020023with HW.GFX.GMA.Config;
Nico Huber8c45bcf2016-11-20 17:30:57 +010024with HW.GFX.GMA.Config_Helpers;
Nico Huber83693c82016-10-08 22:17:55 +020025with HW.GFX.GMA.Registers;
26with HW.GFX.GMA.Power_And_Clocks;
27with HW.GFX.GMA.Panel;
28with HW.GFX.GMA.PLLs;
29with HW.GFX.GMA.Port_Detect;
30with HW.GFX.GMA.Connectors;
31with HW.GFX.GMA.Connector_Info;
32with HW.GFX.GMA.Pipe_Setup;
33
Nico Huber83693c82016-10-08 22:17:55 +020034with HW.Debug;
35with GNAT.Source_Info;
36
Nico Huber83693c82016-10-08 22:17:55 +020037use type HW.Int32;
38
39package body HW.GFX.GMA
40 with Refined_State =>
41 (State =>
Nico Huber2b6f6992017-07-09 18:11:34 +020042 (Dev.Address_State,
43 Registers.Address_State,
Nico Huber83693c82016-10-08 22:17:55 +020044 PLLs.State, Panel.Panel_State,
Nico Huber1a712d32017-01-09 15:11:04 +010045 Cur_Configs, Allocated_PLLs,
Nico Huberc3f66f62017-07-16 21:39:54 +020046 HPD_Delay, Wait_For_HPD,
47 Linear_FB_Base),
Nico Huber83693c82016-10-08 22:17:55 +020048 Init_State => Initialized,
Arthur Heymansd1988d12018-03-28 16:27:57 +020049 Config_State => (Config.Valid_Port_GPU, Config.Raw_Clock),
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
55 subtype Port_Name is String (1 .. 8);
56 type Port_Name_Array is array (Port_Type) of Port_Name;
57 Port_Names : constant Port_Name_Array :=
58 (Disabled => "Disabled",
59 Internal => "Internal",
60 DP1 => "DP1 ",
61 DP2 => "DP2 ",
62 DP3 => "DP3 ",
Nico Huber0d454cd2016-11-21 13:33:43 +010063 HDMI1 => "HDMI1 ",
64 HDMI2 => "HDMI2 ",
65 HDMI3 => "HDMI3 ",
Nico Huber83693c82016-10-08 22:17:55 +020066 Analog => "Analog ");
67
Nico Huber2b6f6992017-07-09 18:11:34 +020068 package Dev is new HW.PCI.Dev (PCI.Address'(0, 2, 0));
69
Nico Huber83693c82016-10-08 22:17:55 +020070 package Display_Controller renames Pipe_Setup;
71
Nico Huber99f10f32016-11-20 00:34:05 +010072 type PLLs_Type is array (Pipe_Index) of PLLs.T;
Nico Huber83693c82016-10-08 22:17:55 +020073
Nico Huber83693c82016-10-08 22:17:55 +020074 type HPD_Type is array (Port_Type) of Boolean;
Nico Huber3be61d42017-01-09 13:58:18 +010075 type HPD_Delay_Type is array (Active_Port_Type) of Time.T;
Nico Huber83693c82016-10-08 22:17:55 +020076
Nico Huber83693c82016-10-08 22:17:55 +020077 Allocated_PLLs : PLLs_Type;
Nico Huber83693c82016-10-08 22:17:55 +020078 HPD_Delay : HPD_Delay_Type;
79 Wait_For_HPD : HPD_Type;
80 Initialized : Boolean := False;
81
Nico Huberc3f66f62017-07-16 21:39:54 +020082 Linear_FB_Base : Word64;
83
Nico Huber83693c82016-10-08 22:17:55 +020084 ----------------------------------------------------------------------------
85
Nico Huberf54d0962016-10-20 14:17:18 +020086 PCH_RAWCLK_FREQ_MASK : constant := 16#3ff# * 2 ** 0;
87
88 function PCH_RAWCLK_FREQ (Freq : Frequency_Type) return Word32
89 is
90 begin
91 return Word32 (Freq / 1_000_000);
92 end PCH_RAWCLK_FREQ;
93
94 ----------------------------------------------------------------------------
95
Nico Huber43370ba2017-01-09 15:26:19 +010096 procedure Enable_Output
97 (Pipe : in Pipe_Index;
98 Pipe_Cfg : in Pipe_Config;
99 Success : out Boolean)
100 is
101 Port_Cfg : Port_Config;
102 begin
Nico Huber3be61d42017-01-09 13:58:18 +0100103 pragma Debug (Debug.New_Line);
104 pragma Debug (Debug.Put_Line
105 ("Trying to enable port " & Port_Names (Pipe_Cfg.Port)));
106
Nico Huber43370ba2017-01-09 15:26:19 +0100107 Config_Helpers.Fill_Port_Config
108 (Port_Cfg, Pipe, Pipe_Cfg.Port, Pipe_Cfg.Mode, Success);
109
110 if Success then
111 Success := Config_Helpers.Validate_Config
112 (Pipe_Cfg.Framebuffer, Port_Cfg, Pipe);
113 end if;
114
Nico Huber43370ba2017-01-09 15:26:19 +0100115 if Success then
Nico Huber43370ba2017-01-09 15:26:19 +0100116 Connector_Info.Preferred_Link_Setting (Port_Cfg, Success);
117 end if;
118
119 -- loop over all possible DP-lane configurations
120 -- (non-DP ports use a single fake configuration)
121 while Success loop
122 pragma Loop_Invariant
123 (Pipe_Cfg.Port in Active_Port_Type and
124 Port_Cfg.Mode = Port_Cfg.Mode'Loop_Entry);
125
126 PLLs.Alloc
127 (Port_Cfg => Port_Cfg,
128 PLL => Allocated_PLLs (Pipe),
129 Success => Success);
130
131 if Success then
132 -- try each DP-lane configuration twice
133 for Try in 1 .. 2 loop
134 pragma Loop_Invariant
135 (Pipe_Cfg.Port in Active_Port_Type);
136
Nico Huber4798c662017-01-11 12:44:48 +0100137 -- Clear pending hot-plug events before every try
138 Port_Detect.Clear_Hotplug_Detect (Pipe_Cfg.Port);
139
Nico Huber43370ba2017-01-09 15:26:19 +0100140 Connectors.Pre_On
141 (Pipe => Pipe,
142 Port_Cfg => Port_Cfg,
143 PLL_Hint => PLLs.Register_Value (Allocated_PLLs (Pipe)),
144 Success => Success);
145
146 if Success then
147 Display_Controller.On
148 (Pipe => Pipe,
149 Port_Cfg => Port_Cfg,
150 Framebuffer => Pipe_Cfg.Framebuffer);
151
152 Connectors.Post_On
153 (Port_Cfg => Port_Cfg,
154 PLL_Hint => PLLs.Register_Value (Allocated_PLLs (Pipe)),
155 Success => Success);
156
157 if not Success then
158 Display_Controller.Off (Pipe);
159 Connectors.Post_Off (Port_Cfg);
160 end if;
161 end if;
162
163 exit when Success;
164 end loop;
165 exit when Success; -- connection established => stop loop
166
167 -- connection failed
168 PLLs.Free (Allocated_PLLs (Pipe));
169 end if;
170
171 Connector_Info.Next_Link_Setting (Port_Cfg, Success);
172 end loop;
173
174 if Success then
175 pragma Debug (Debug.Put_Line
176 ("Enabled port " & Port_Names (Pipe_Cfg.Port)));
177 else
178 Wait_For_HPD (Pipe_Cfg.Port) := True;
179 if Pipe_Cfg.Port = Internal then
180 Panel.Off;
181 end if;
182 end if;
183 end Enable_Output;
184
Nico Huber3be61d42017-01-09 13:58:18 +0100185 procedure Disable_Output (Pipe : Pipe_Index; Pipe_Cfg : Pipe_Config)
186 is
187 Port_Cfg : Port_Config;
188 Success : Boolean;
189 begin
190 Config_Helpers.Fill_Port_Config
191 (Port_Cfg, Pipe, Pipe_Cfg.Port, Pipe_Cfg.Mode, Success);
192 if Success then
193 pragma Debug (Debug.New_Line);
194 pragma Debug (Debug.Put_Line
195 ("Disabling port " & Port_Names (Pipe_Cfg.Port)));
196 pragma Debug (Debug.New_Line);
197
198 Connectors.Pre_Off (Port_Cfg);
199 Display_Controller.Off (Pipe);
200 Connectors.Post_Off (Port_Cfg);
201
202 PLLs.Free (Allocated_PLLs (Pipe));
203 end if;
204 end Disable_Output;
205
Nico Huber99f10f32016-11-20 00:34:05 +0100206 procedure Update_Outputs (Configs : Pipe_Configs)
Nico Huber83693c82016-10-08 22:17:55 +0200207 is
Nico Huber3be61d42017-01-09 13:58:18 +0100208 procedure Check_HPD (Port : in Active_Port_Type; Detected : out Boolean)
209 is
210 HPD_Delay_Over : constant Boolean := Time.Timed_Out (HPD_Delay (Port));
211 begin
212 if HPD_Delay_Over then
213 Port_Detect.Hotplug_Detect (Port, Detected);
214 HPD_Delay (Port) := Time.MS_From_Now (333);
215 else
216 Detected := False;
217 end if;
218 end Check_HPD;
Nico Huberb56b9c52017-01-11 15:12:23 +0100219
Nico Huber564103f2017-01-11 15:33:07 +0100220 Power_Changed : Boolean := False;
Nico Huberb56b9c52017-01-11 15:12:23 +0100221 Old_Configs : Pipe_Configs;
Nico Huber564103f2017-01-11 15:33:07 +0100222
223 -- Only called when we actually tried to change something
224 -- so we don't congest the log with unnecessary messages.
225 procedure Update_Power
226 is
227 begin
228 if not Power_Changed then
229 Power_And_Clocks.Power_Up (Old_Configs, Configs);
230 Power_Changed := True;
231 end if;
232 end Update_Power;
Nico Huber83693c82016-10-08 22:17:55 +0200233 begin
234 Old_Configs := Cur_Configs;
235
Nico Huberb56b9c52017-01-11 15:12:23 +0100236 -- disable all pipes that changed or had a hot-plug event
237 for Pipe in Pipe_Index loop
238 declare
239 Unplug_Detected : Boolean;
240 Cur_Config : Pipe_Config renames Cur_Configs (Pipe);
241 New_Config : Pipe_Config renames Configs (Pipe);
242 begin
243 if Cur_Config.Port /= Disabled then
244 Check_HPD (Cur_Config.Port, Unplug_Detected);
Nico Huber83693c82016-10-08 22:17:55 +0200245
Nico Huberb56b9c52017-01-11 15:12:23 +0100246 if Cur_Config.Port /= New_Config.Port or
247 Cur_Config.Mode /= New_Config.Mode or
248 Unplug_Detected
249 then
250 Disable_Output (Pipe, Cur_Config);
251 Cur_Config.Port := Disabled;
Nico Huber564103f2017-01-11 15:33:07 +0100252 Update_Power;
Nico Huberb56b9c52017-01-11 15:12:23 +0100253 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200254 end if;
Nico Huberb56b9c52017-01-11 15:12:23 +0100255 end;
256 end loop;
Nico Huber83693c82016-10-08 22:17:55 +0200257
Nico Huberb56b9c52017-01-11 15:12:23 +0100258 -- enable all pipes that changed and should be active
259 for Pipe in Pipe_Index loop
260 declare
261 Success : Boolean;
262 Cur_Config : Pipe_Config renames Cur_Configs (Pipe);
263 New_Config : Pipe_Config renames Configs (Pipe);
264 begin
265 if New_Config.Port /= Disabled and then
266 (Cur_Config.Port /= New_Config.Port or
267 Cur_Config.Mode /= New_Config.Mode)
268 then
Nico Huber3be61d42017-01-09 13:58:18 +0100269 if Wait_For_HPD (New_Config.Port) then
270 Check_HPD (New_Config.Port, Success);
271 Wait_For_HPD (New_Config.Port) := not Success;
272 else
273 Success := True;
Nico Huber8c45bcf2016-11-20 17:30:57 +0100274 end if;
Nico Huberc7a4fee2016-11-03 18:18:03 +0100275
Nico Huber3be61d42017-01-09 13:58:18 +0100276 if Success then
Nico Huber564103f2017-01-11 15:33:07 +0100277 Update_Power;
Nico Huberb56b9c52017-01-11 15:12:23 +0100278 Enable_Output (Pipe, New_Config, Success);
Nico Huber3be61d42017-01-09 13:58:18 +0100279 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200280
281 if Success then
Nico Huberb56b9c52017-01-11 15:12:23 +0100282 Cur_Config := New_Config;
Nico Huber83693c82016-10-08 22:17:55 +0200283 end if;
Nico Huber3be61d42017-01-09 13:58:18 +0100284
Nico Huberb56b9c52017-01-11 15:12:23 +0100285 -- update framebuffer offset only
286 elsif New_Config.Port /= Disabled and
287 Cur_Config.Framebuffer /= New_Config.Framebuffer
288 then
289 Display_Controller.Update_Offset (Pipe, New_Config.Framebuffer);
290 Cur_Config := New_Config;
291 end if;
292 end;
Nico Huber83693c82016-10-08 22:17:55 +0200293 end loop;
294
Nico Huber564103f2017-01-11 15:33:07 +0100295 if Power_Changed then
Nico Huber83693c82016-10-08 22:17:55 +0200296 Power_And_Clocks.Power_Down (Old_Configs, Configs, Cur_Configs);
297 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200298 end Update_Outputs;
299
300 ----------------------------------------------------------------------------
301
302 procedure Initialize
Nico Huber2b6f6992017-07-09 18:11:34 +0200303 (Write_Delay : in Word64 := 0;
Nico Huber793a8d42016-11-21 18:57:03 +0100304 Clean_State : in Boolean := False;
Nico Huber83693c82016-10-08 22:17:55 +0200305 Success : out Boolean)
306 with
307 Refined_Global =>
308 (In_Out =>
Nico Hubere015e822017-08-25 20:12:09 +0200309 (Config.Valid_Port_GPU, Dev.PCI_State,
Arthur Heymansd1988d12018-03-28 16:27:57 +0200310 Registers.Register_State, Port_IO.State,
311 Config.Raw_Clock),
Nico Huber83693c82016-10-08 22:17:55 +0200312 Input =>
313 (Time.State),
314 Output =>
Nico Huber2b6f6992017-07-09 18:11:34 +0200315 (Dev.Address_State,
316 Registers.Address_State,
Nico Huber83693c82016-10-08 22:17:55 +0200317 PLLs.State, Panel.Panel_State,
Nico Huber1a712d32017-01-09 15:11:04 +0100318 Cur_Configs, Allocated_PLLs,
Nico Huberc3f66f62017-07-16 21:39:54 +0200319 HPD_Delay, Wait_For_HPD,
320 Linear_FB_Base, Initialized))
Nico Huber83693c82016-10-08 22:17:55 +0200321 is
322 use type HW.Word64;
323
Nico Huber2b6f6992017-07-09 18:11:34 +0200324 PCI_MMIO_Base, PCI_GTT_Base : Word64;
325
Nico Huber83693c82016-10-08 22:17:55 +0200326 Now : constant Time.T := Time.Now;
327
328 procedure Check_Platform (Success : out Boolean)
329 is
330 Audio_VID_DID : Word32;
331 begin
332 case Config.CPU is
333 when Haswell .. Skylake =>
334 Registers.Read (Registers.AUD_VID_DID, Audio_VID_DID);
335 when Ironlake .. Ivybridge =>
336 Registers.Read (Registers.PCH_AUD_VID_DID, Audio_VID_DID);
337 end case;
338 Success :=
339 (case Config.CPU is
Nico Huber21da5742017-01-20 14:00:53 +0100340 when Broxton => Audio_VID_DID = 16#8086_280a#,
Nico Huber83693c82016-10-08 22:17:55 +0200341 when Skylake => Audio_VID_DID = 16#8086_2809#,
342 when Broadwell => Audio_VID_DID = 16#8086_2808#,
343 when Haswell => Audio_VID_DID = 16#8086_2807#,
344 when Ivybridge |
345 Sandybridge => Audio_VID_DID = 16#8086_2806# or
346 Audio_VID_DID = 16#8086_2805#,
Nico Hubereeb5a392016-10-09 19:28:30 +0200347 when Ironlake => Audio_VID_DID = 16#0000_0000#);
Nico Huber83693c82016-10-08 22:17:55 +0200348 end Check_Platform;
Nico Hubere7ac6eb2017-09-04 23:54:13 +0200349
350 procedure Check_Platform_PCI (Success : out Boolean)
351 is
352 use type HW.Word16;
353 Vendor, Device : Word16;
354 begin
355 Dev.Read16 (Vendor, PCI.Vendor_Id);
356 Dev.Read16 (Device, PCI.Device_Id);
357
358 Success := Vendor = 16#8086# and Config.Compatible_GPU (Device);
359 end Check_Platform_PCI;
Nico Huber83693c82016-10-08 22:17:55 +0200360 begin
Nico Huber83693c82016-10-08 22:17:55 +0200361 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
362
363 pragma Debug (Debug.Set_Register_Write_Delay (Write_Delay));
364
Nico Huberc3f66f62017-07-16 21:39:54 +0200365 Linear_FB_Base := 0;
Nico Huber83693c82016-10-08 22:17:55 +0200366 Wait_For_HPD := HPD_Type'(others => False);
367 HPD_Delay := HPD_Delay_Type'(others => Now);
Nico Huber83693c82016-10-08 22:17:55 +0200368 Allocated_PLLs := (others => PLLs.Invalid);
Nico Huber99f10f32016-11-20 00:34:05 +0100369 Cur_Configs := Pipe_Configs'
370 (others => Pipe_Config'
Nico Huber83693c82016-10-08 22:17:55 +0200371 (Port => Disabled,
372 Framebuffer => HW.GFX.Default_FB,
373 Mode => HW.GFX.Invalid_Mode));
Nico Huber83693c82016-10-08 22:17:55 +0200374 PLLs.Initialize;
375
Nico Huber2b6f6992017-07-09 18:11:34 +0200376 Dev.Initialize (Success);
377
378 if Success then
379 Dev.Map (PCI_MMIO_Base, PCI.Res0, Length => Config.GTT_Offset);
380 Dev.Map (PCI_GTT_Base, PCI.Res0, Offset => Config.GTT_Offset);
381 if PCI_MMIO_Base /= 0 and PCI_GTT_Base /= 0 then
382 Registers.Set_Register_Base (PCI_MMIO_Base, PCI_GTT_Base);
383 else
384 pragma Debug (Debug.Put_Line
385 ("ERROR: Couldn't map resoure0."));
386 Registers.Set_Register_Base (Config.Default_MMIO_Base);
387 Success := Config.Default_MMIO_Base_Set;
388 end if;
Nico Hubere7ac6eb2017-09-04 23:54:13 +0200389
390 if Success then
391 Check_Platform_PCI (Success);
392 end if;
Nico Huber2b6f6992017-07-09 18:11:34 +0200393 else
394 pragma Debug (Debug.Put_Line
395 ("WARNING: Couldn't initialize PCI dev."));
396 Registers.Set_Register_Base (Config.Default_MMIO_Base);
397 Success := Config.Default_MMIO_Base_Set;
Nico Huber2b6f6992017-07-09 18:11:34 +0200398
Nico Hubere7ac6eb2017-09-04 23:54:13 +0200399 if Success then
400 Check_Platform (Success);
401 end if;
Nico Huber2b6f6992017-07-09 18:11:34 +0200402 end if;
403
Nico Huber83693c82016-10-08 22:17:55 +0200404 if not Success then
405 pragma Debug (Debug.Put_Line ("ERROR: Incompatible CPU or PCH."));
406
407 Panel.Static_Init; -- for flow analysis
408
409 Initialized := False;
410 return;
411 end if;
412
413 Panel.Setup_PP_Sequencer;
414 Port_Detect.Initialize;
Nico Huber0923b792017-06-09 15:28:41 +0200415 Connectors.Initialize;
Nico Huber83693c82016-10-08 22:17:55 +0200416
Nico Huber793a8d42016-11-21 18:57:03 +0100417 if Clean_State then
418 Power_And_Clocks.Pre_All_Off;
419 Connectors.Pre_All_Off;
420 Display_Controller.All_Off;
421 Connectors.Post_All_Off;
422 PLLs.All_Off;
423 Power_And_Clocks.Post_All_Off;
Nico Huber17d64b62017-07-15 20:51:25 +0200424 Registers.Clear_Fences;
Nico Huber33912aa2016-12-06 20:36:23 +0100425 else
426 -- According to PRMs, VGA plane is the only thing
Nico Huber3a0e2a02017-07-19 14:41:46 +0200427 -- that's enabled by default after reset...
Nico Huber33912aa2016-12-06 20:36:23 +0100428 Display_Controller.Legacy_VGA_Off;
Nico Huber3a0e2a02017-07-19 14:41:46 +0200429 -- ... along with some DDI port bits since Skylake.
430 Connectors.Post_Reset_Off;
Nico Huber793a8d42016-11-21 18:57:03 +0100431 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200432
433 -------------------- Now restart from a clean state ---------------------
434 Power_And_Clocks.Initialize;
435
Nico Huber1c3b9282017-02-09 13:57:04 +0100436 if Config.Has_PCH then
437 Registers.Unset_And_Set_Mask
438 (Register => Registers.PCH_RAWCLK_FREQ,
439 Mask_Unset => PCH_RAWCLK_FREQ_MASK,
440 Mask_Set => PCH_RAWCLK_FREQ (Config.Default_RawClk_Freq));
441 end if;
Nico Huberf54d0962016-10-20 14:17:18 +0200442
Nico Huber83693c82016-10-08 22:17:55 +0200443 Initialized := True;
444
445 end Initialize;
446
447 function Is_Initialized return Boolean
448 with
449 Refined_Post => Is_Initialized'Result = Initialized
450 is
451 begin
452 return Initialized;
453 end Is_Initialized;
454
455 ----------------------------------------------------------------------------
456
Nico Huber42fb2d02017-09-01 17:01:51 +0200457 procedure Power_Up_VGA
458 is
459 Fake_Config : constant Pipe_Configs :=
460 (Primary =>
461 (Port => Analog,
462 Framebuffer => HW.GFX.Default_FB,
463 Mode => HW.GFX.Invalid_Mode),
464 others =>
465 (Port => Disabled,
466 Framebuffer => HW.GFX.Default_FB,
467 Mode => HW.GFX.Invalid_Mode));
468 begin
469 Power_And_Clocks.Power_Up (Cur_Configs, Fake_Config);
470 end Power_Up_VGA;
471
472 ----------------------------------------------------------------------------
473
Nico Huber5374c3a2017-07-15 21:48:06 +0200474 function FB_First_Page (FB : Framebuffer_Type) return Natural is
475 (Natural (FB.Offset / GTT_Page_Size));
476 function FB_Pages (FB : Framebuffer_Type) return Natural is
477 (Natural (Div_Round_Up (FB_Size (FB), GTT_Page_Size)));
478 function FB_Last_Page (FB : Framebuffer_Type) return Natural is
479 (FB_First_Page (FB) + FB_Pages (FB) - 1);
480
481 -- Check basics and that it fits in GTT
482 function Valid_FB (FB : Framebuffer_Type) return Boolean is
Nico Huber9b479412017-08-27 11:55:56 +0200483 (Valid_Stride (FB) and FB_Last_Page (FB) < GTT_Rotation_Offset);
Nico Huber5374c3a2017-07-15 21:48:06 +0200484
485 -- Also check that we don't overflow the GTT's 39-bit space
486 -- (always true with a 32-bit base)
487 function Valid_Phys_FB (FB : Framebuffer_Type; Phys_Base : Word32)
488 return Boolean is
489 (Valid_FB (FB) and
490 Int64 (Phys_Base) + Int64 (FB.Offset) + Int64 (FB_Size (FB)) <=
491 Int64 (GTT_Address_Type'Last))
492 with
493 Ghost;
494
Nico Huber83693c82016-10-08 22:17:55 +0200495 procedure Write_GTT
496 (GTT_Page : GTT_Range;
497 Device_Address : GTT_Address_Type;
Nico Huber5374c3a2017-07-15 21:48:06 +0200498 Valid : Boolean)
499 is
Nico Huber83693c82016-10-08 22:17:55 +0200500 begin
501 Registers.Write_GTT (GTT_Page, Device_Address, Valid);
502 end Write_GTT;
503
Nico Huber194e57e2017-07-15 21:15:46 +0200504 procedure Setup_Default_GTT (FB : Framebuffer_Type; Phys_Base : Word32)
Nico Huber5374c3a2017-07-15 21:48:06 +0200505 with
506 Pre => Is_Initialized and Valid_Phys_FB (FB, Phys_Base)
Nico Huber83693c82016-10-08 22:17:55 +0200507 is
Nico Huber194e57e2017-07-15 21:15:46 +0200508 Phys_Addr : GTT_Address_Type :=
509 GTT_Address_Type (Phys_Base) + GTT_Address_Type (FB.Offset);
Nico Huber83693c82016-10-08 22:17:55 +0200510 begin
Nico Huber194e57e2017-07-15 21:15:46 +0200511 for Idx in FB_First_Page (FB) .. FB_Last_Page (FB) loop
Nico Huber83693c82016-10-08 22:17:55 +0200512 Registers.Write_GTT
513 (GTT_Page => Idx,
514 Device_Address => Phys_Addr,
515 Valid => True);
Nico Huber194e57e2017-07-15 21:15:46 +0200516 Phys_Addr := Phys_Addr + GTT_Page_Size;
Nico Huber83693c82016-10-08 22:17:55 +0200517 end loop;
Nico Huber9b479412017-08-27 11:55:56 +0200518
519 if Rotation_90 (FB) and FB.Tiling = Y_Tiled and FB.V_Stride >= 32 then
520 declare
521 V_Pages : constant Natural := Natural (FB.V_Stride) / 32;
522 Bytes_Per_Row : constant GTT_Address_Type :=
523 GTT_Address_Type (Pixel_To_Bytes (32 * FB.Stride, FB));
524 begin
525 Phys_Addr := GTT_Address_Type (Phys_Base) +
526 GTT_Address_Type (FB.Offset) +
527 GTT_Address_Type (FB_Size (FB));
528 for Page in FB_First_Page (FB) .. FB_Last_Page (FB) loop
529 Phys_Addr := Phys_Addr - Bytes_Per_Row;
530 Registers.Write_GTT
531 (GTT_Page => GTT_Rotation_Offset + Page,
532 Device_Address => Phys_Addr,
533 Valid => True);
534
535 if (Page - FB_First_Page (FB) + 1) mod V_Pages = 0 then
536 Phys_Addr := Phys_Addr + GTT_Page_Size +
537 GTT_Address_Type (V_Pages) * Bytes_Per_Row;
538 end if;
539 end loop;
540 end;
541 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200542 end Setup_Default_GTT;
543
544 ----------------------------------------------------------------------------
545
Nico Hubereedde882017-07-16 02:54:39 +0200546 use type HW.Word16;
547 subtype Stolen_Size_Range is Int64 range 0 .. 2 ** 33;
548
549 function GGMS_Gen4 (GGC : Word16) return Natural is
550 (Natural (Shift_Right (GGC, 8) and 16#07#));
551 function GTT_Size_Gen4 (GGC : Word16) return Natural is
552 (if GGMS_Gen4 (GGC) in 1 .. 3 then
553 (GGMS_Gen4 (GGC) + 1) * 2 ** 19 else 0);
554
555 function GMS_Gen4 (GGC : Word16) return Natural is
556 (Natural (Shift_Right (GGC, 4) and 16#0f#));
557 Valid_Stolen_Size_Gen4 : constant
558 array (Natural range 1 .. 13) of Stolen_Size_Range :=
559 (1, 4, 8, 16, 32, 48, 64, 128, 256, 96, 160, 224, 352);
560 function Stolen_Size_Gen4 (GGC : Word16) return Stolen_Size_Range is
561 (if GMS_Gen4 (GGC) in Valid_Stolen_Size_Gen4'Range then
Arthur Heymans5fd9a312017-09-12 12:45:18 +0200562 Valid_Stolen_Size_Gen4 (GMS_Gen4 (GGC)) * 2 ** 20 else 0);
Nico Hubereedde882017-07-16 02:54:39 +0200563
564 function GTT_Size_Gen6 (GGC : Word16) return Natural is
565 (Natural (Shift_Right (GGC, 8) and 16#03#) * 2 ** 20);
566
567 function Stolen_Size_Gen6 (GGC : Word16) return Stolen_Size_Range is
568 (Stolen_Size_Range (Shift_Right (GGC, 3) and 16#1f#) * 32 * 2 ** 20);
569
570 function GTT_Size_Gen8 (GGC : Word16) return Natural is
571 (Natural (Shift_Right (GGC, 6) and 16#03#) * 2 ** 20);
572
573 function GMS_Gen8 (GGC : Word16) return Stolen_Size_Range is
574 (Stolen_Size_Range (Shift_Right (GGC, 8) and 16#ff#));
575 function Stolen_Size_Gen8 (GGC : Word16) return Stolen_Size_Range is
576 (GMS_Gen8 (GGC) * 32 * 2 ** 20);
577
578 function Stolen_Size_Gen9 (GGC : Word16) return Stolen_Size_Range is
579 (if GMS_Gen8 (GGC) < 16#f0# then
580 Stolen_Size_Gen8 (GGC)
581 else
582 (GMS_Gen8 (GGC) - 16#f0# + 1) * 4 * 2 ** 20);
583
584 procedure Decode_Stolen
585 (GTT_Size : out Natural;
586 Stolen_Size : out Stolen_Size_Range)
587 with
588 Pre => Is_Initialized
589 is
590 GGC_Reg : constant :=
591 (case Config.CPU is
592 when Ironlake => 16#52#,
593 when Sandybridge .. Skylake => 16#50#);
594 GGC : Word16;
595 begin
596 Dev.Read16 (GGC, GGC_Reg);
597 case Config.CPU is
598 when Ironlake =>
599 GTT_Size := GTT_Size_Gen4 (GGC);
600 Stolen_Size := Stolen_Size_Gen4 (GGC);
601 when Sandybridge .. Haswell =>
602 GTT_Size := GTT_Size_Gen6 (GGC);
603 Stolen_Size := Stolen_Size_Gen6 (GGC);
604 when Broadwell =>
605 GTT_Size := GTT_Size_Gen8 (GGC);
606 Stolen_Size := Stolen_Size_Gen8 (GGC);
607 when Broxton .. Skylake =>
608 GTT_Size := GTT_Size_Gen8 (GGC);
609 Stolen_Size := Stolen_Size_Gen9 (GGC);
610 end case;
611 end Decode_Stolen;
612
613 -- Additional runtime validation that FB fits stolen memory and aperture.
614 procedure Validate_FB (FB : Framebuffer_Type; Valid : out Boolean)
615 with
616 Pre => Is_Initialized,
617 Post => (if Valid then Valid_FB (FB))
618 is
619 GTT_Size, Aperture_Size : Natural;
620 Stolen_Size : Stolen_Size_Range;
621 begin
622 Valid := Valid_FB (FB);
623
624 if Valid then
625 Decode_Stolen (GTT_Size, Stolen_Size);
626 Dev.Resource_Size (Aperture_Size, PCI.Res2);
627 Valid :=
628 FB_Last_Page (FB) < GTT_Size / Config.GTT_PTE_Size and
629 FB_Last_Page (FB) < Natural (Stolen_Size / GTT_Page_Size) and
630 FB_Last_Page (FB) < Aperture_Size / GTT_Page_Size;
631 pragma Debug (not Valid, Debug.Put
632 ("Stolen memory too small to hold framebuffer."));
633 end if;
634 end Validate_FB;
635
Nico Huber5374c3a2017-07-15 21:48:06 +0200636 procedure Setup_Default_FB
637 (FB : in Framebuffer_Type;
638 Clear : in Boolean := True;
639 Success : out Boolean)
640 is
641 GMA_Phys_Base : constant PCI.Index := 16#5c#;
642 GMA_Phys_Base_Mask : constant := 16#fff0_0000#;
643
644 Phys_Base : Word32;
645 begin
Nico Hubereedde882017-07-16 02:54:39 +0200646 Validate_FB (FB, Success);
Nico Huber5374c3a2017-07-15 21:48:06 +0200647
648 if Success then
649 Dev.Read32 (Phys_Base, GMA_Phys_Base);
650 Phys_Base := Phys_Base and GMA_Phys_Base_Mask;
651 Success := Phys_Base /= GMA_Phys_Base_Mask and Phys_Base /= 0;
652 pragma Debug (not Success, Debug.Put_Line
653 ("Failed to read stolen memory base."));
Nico Huber0164b022017-08-24 15:12:51 +0200654
655 if Success then
656 if FB.Tiling in XY_Tiling then
657 Registers.Add_Fence
658 (First_Page => FB_First_Page (FB),
659 Last_Page => FB_Last_Page (FB),
660 Tiling => FB.Tiling,
661 Pitch => FB_Pitch (FB.Stride, FB),
662 Success => Success);
663 end if;
664 pragma Debug (not Success, Debug.Put_Line
665 ("Tiled framebuffer but no fence regs available."));
666 end if;
667
Nico Huber5374c3a2017-07-15 21:48:06 +0200668 if Success then
669 Setup_Default_GTT (FB, Phys_Base);
670 end if;
671 end if;
672
673 if Success and then Clear then
674 declare
675 use type HW.Word64;
676 Linear_FB : Word64;
677 begin
Nico Huberc3f66f62017-07-16 21:39:54 +0200678 Map_Linear_FB (Linear_FB, FB);
Nico Huber5374c3a2017-07-15 21:48:06 +0200679 if Linear_FB /= 0 then
Nico Huberc3f66f62017-07-16 21:39:54 +0200680 Framebuffer_Filler.Fill (Linear_FB, FB);
Nico Huber5374c3a2017-07-15 21:48:06 +0200681 end if;
Nico Huber5374c3a2017-07-15 21:48:06 +0200682 end;
683 end if;
684 end Setup_Default_FB;
685
Nico Huberc3f66f62017-07-16 21:39:54 +0200686 procedure Map_Linear_FB (Linear_FB : out Word64; FB : in Framebuffer_Type)
687 is
688 use type HW.Word64;
689
690 Valid : Boolean;
691 begin
692 Linear_FB := 0;
693
694 if Linear_FB_Base = 0 then
695 Dev.Map (Linear_FB_Base, PCI.Res2);
696 pragma Debug
697 (Linear_FB_Base = 0, Debug.Put_Line ("Failed to map resource2."));
698 end if;
699
700 if Linear_FB_Base /= 0 then
701 Validate_FB (FB, Valid);
702 if Valid then
703 Linear_FB := Linear_FB_Base + Word64 (FB.Offset);
704 end if;
705 end if;
706 end Map_Linear_FB;
707
Nico Huber5374c3a2017-07-15 21:48:06 +0200708 ----------------------------------------------------------------------------
709
Nico Huber99f10f32016-11-20 00:34:05 +0100710 procedure Dump_Configs (Configs : Pipe_Configs)
Nico Huber83693c82016-10-08 22:17:55 +0200711 is
712 subtype Pipe_Name is String (1 .. 9);
Nico Huber99f10f32016-11-20 00:34:05 +0100713 type Pipe_Name_Array is array (Pipe_Index) of Pipe_Name;
Nico Huber83693c82016-10-08 22:17:55 +0200714 Pipe_Names : constant Pipe_Name_Array :=
715 (Primary => "Primary ",
716 Secondary => "Secondary",
717 Tertiary => "Tertiary ");
718 begin
719 Debug.New_Line;
Paul Menzelb83107c2017-05-04 09:02:33 +0200720 Debug.Put_Line ("CONFIG =>");
Nico Huber99f10f32016-11-20 00:34:05 +0100721 for Pipe in Pipe_Index loop
722 if Pipe = Pipe_Index'First then
Nico Huber83693c82016-10-08 22:17:55 +0200723 Debug.Put (" (");
724 else
725 Debug.Put (" ");
726 end if;
727 Debug.Put_Line (Pipe_Names (Pipe) & " =>");
728 Debug.Put_Line
729 (" (Port => " & Port_Names (Configs (Pipe).Port) & ",");
730 Debug.Put_Line (" Framebuffer =>");
731 Debug.Put (" (Width => ");
732 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Width);
733 Debug.Put_Line (",");
734 Debug.Put (" Height => ");
735 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Height);
736 Debug.Put_Line (",");
737 Debug.Put (" Stride => ");
738 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Stride);
739 Debug.Put_Line (",");
740 Debug.Put (" Offset => ");
741 Debug.Put_Word32 (Configs (Pipe).Framebuffer.Offset);
742 Debug.Put_Line (",");
743 Debug.Put (" BPC => ");
744 Debug.Put_Int64 (Configs (Pipe).Framebuffer.BPC);
745 Debug.Put_Line ("),");
746 Debug.Put_Line (" Mode =>");
747 Debug.Put (" (Dotclock => ");
748 Debug.Put_Int64 (Configs (Pipe).Mode.Dotclock);
749 Debug.Put_Line (",");
750 Debug.Put (" H_Visible => ");
751 Debug.Put_Int16 (Configs (Pipe).Mode.H_Visible);
752 Debug.Put_Line (",");
753 Debug.Put (" H_Sync_Begin => ");
754 Debug.Put_Int16 (Configs (Pipe).Mode.H_Sync_Begin);
755 Debug.Put_Line (",");
756 Debug.Put (" H_Sync_End => ");
757 Debug.Put_Int16 (Configs (Pipe).Mode.H_Sync_End);
758 Debug.Put_Line (",");
759 Debug.Put (" H_Total => ");
760 Debug.Put_Int16 (Configs (Pipe).Mode.H_Total);
761 Debug.Put_Line (",");
762 Debug.Put (" V_Visible => ");
763 Debug.Put_Int16 (Configs (Pipe).Mode.V_Visible);
764 Debug.Put_Line (",");
765 Debug.Put (" V_Sync_Begin => ");
766 Debug.Put_Int16 (Configs (Pipe).Mode.V_Sync_Begin);
767 Debug.Put_Line (",");
768 Debug.Put (" V_Sync_End => ");
769 Debug.Put_Int16 (Configs (Pipe).Mode.V_Sync_End);
770 Debug.Put_Line (",");
771 Debug.Put (" V_Total => ");
772 Debug.Put_Int16 (Configs (Pipe).Mode.V_Total);
773 Debug.Put_Line (",");
774 Debug.Put_Line (" H_Sync_Active_High => " &
775 (if Configs (Pipe).Mode.H_Sync_Active_High
776 then "True,"
777 else "False,"));
778 Debug.Put_Line (" V_Sync_Active_High => " &
779 (if Configs (Pipe).Mode.V_Sync_Active_High
780 then "True,"
781 else "False,"));
782 Debug.Put (" BPC => ");
783 Debug.Put_Int64 (Configs (Pipe).Mode.BPC);
Nico Huber99f10f32016-11-20 00:34:05 +0100784 if Pipe /= Pipe_Index'Last then
Nico Huber83693c82016-10-08 22:17:55 +0200785 Debug.Put_Line (")),");
786 else
787 Debug.Put_Line (")));");
788 end if;
789 end loop;
790 end Dump_Configs;
791
792end HW.GFX.GMA;