blob: 616a170cf72c6abffbf97682c6c84c996293bd18 [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 Huber83693c82016-10-08 22:17:55 +02003--
4-- This program is free software; you can redistribute it and/or modify
5-- it under the terms of the GNU General Public License as published by
Nico Huber125a29e2016-10-18 00:23:54 +02006-- the Free Software Foundation; either version 2 of the License, or
7-- (at your option) any later version.
Nico Huber83693c82016-10-08 22:17:55 +02008--
9-- This program is distributed in the hope that it will be useful,
10-- but WITHOUT ANY WARRANTY; without even the implied warranty of
11-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12-- GNU General Public License for more details.
13--
14
Nico Huber83693c82016-10-08 22:17:55 +020015with HW.GFX.GMA.Config;
Nico Huber8c45bcf2016-11-20 17:30:57 +010016with HW.GFX.GMA.Config_Helpers;
Nico Huber83693c82016-10-08 22:17:55 +020017with HW.GFX.GMA.Registers;
18with HW.GFX.GMA.Power_And_Clocks;
19with HW.GFX.GMA.Panel;
20with HW.GFX.GMA.PLLs;
21with HW.GFX.GMA.Port_Detect;
22with HW.GFX.GMA.Connectors;
23with HW.GFX.GMA.Connector_Info;
24with HW.GFX.GMA.Pipe_Setup;
25
26with System;
27
28with HW.Debug;
29with GNAT.Source_Info;
30
Nico Huber83693c82016-10-08 22:17:55 +020031use type HW.Int32;
32
33package body HW.GFX.GMA
34 with Refined_State =>
35 (State =>
36 (Registers.Address_State,
37 PLLs.State, Panel.Panel_State,
Nico Huber1a712d32017-01-09 15:11:04 +010038 Cur_Configs, Allocated_PLLs,
Nico Huber83693c82016-10-08 22:17:55 +020039 HPD_Delay, Wait_For_HPD),
40 Init_State => Initialized,
41 Config_State => Config.Valid_Port_GPU,
42 Device_State =>
43 (Registers.Register_State, Registers.GTT_State))
44is
45
46 subtype Port_Name is String (1 .. 8);
47 type Port_Name_Array is array (Port_Type) of Port_Name;
48 Port_Names : constant Port_Name_Array :=
49 (Disabled => "Disabled",
50 Internal => "Internal",
51 DP1 => "DP1 ",
52 DP2 => "DP2 ",
53 DP3 => "DP3 ",
Nico Huber0d454cd2016-11-21 13:33:43 +010054 HDMI1 => "HDMI1 ",
55 HDMI2 => "HDMI2 ",
56 HDMI3 => "HDMI3 ",
Nico Huber83693c82016-10-08 22:17:55 +020057 Analog => "Analog ");
58
59 package Display_Controller renames Pipe_Setup;
60
Nico Huber99f10f32016-11-20 00:34:05 +010061 type PLLs_Type is array (Pipe_Index) of PLLs.T;
Nico Huber83693c82016-10-08 22:17:55 +020062
Nico Huber83693c82016-10-08 22:17:55 +020063 type HPD_Type is array (Port_Type) of Boolean;
Nico Huber3be61d42017-01-09 13:58:18 +010064 type HPD_Delay_Type is array (Active_Port_Type) of Time.T;
Nico Huber83693c82016-10-08 22:17:55 +020065
Nico Huber83693c82016-10-08 22:17:55 +020066 Allocated_PLLs : PLLs_Type;
Nico Huber83693c82016-10-08 22:17:55 +020067 HPD_Delay : HPD_Delay_Type;
68 Wait_For_HPD : HPD_Type;
69 Initialized : Boolean := False;
70
Nico Huber83693c82016-10-08 22:17:55 +020071 ----------------------------------------------------------------------------
72
Nico Huberf54d0962016-10-20 14:17:18 +020073 PCH_RAWCLK_FREQ_MASK : constant := 16#3ff# * 2 ** 0;
74
75 function PCH_RAWCLK_FREQ (Freq : Frequency_Type) return Word32
76 is
77 begin
78 return Word32 (Freq / 1_000_000);
79 end PCH_RAWCLK_FREQ;
80
81 ----------------------------------------------------------------------------
82
Nico Huber43370ba2017-01-09 15:26:19 +010083 procedure Enable_Output
84 (Pipe : in Pipe_Index;
85 Pipe_Cfg : in Pipe_Config;
86 Success : out Boolean)
87 is
88 Port_Cfg : Port_Config;
89 begin
Nico Huber3be61d42017-01-09 13:58:18 +010090 pragma Debug (Debug.New_Line);
91 pragma Debug (Debug.Put_Line
92 ("Trying to enable port " & Port_Names (Pipe_Cfg.Port)));
93
Nico Huber43370ba2017-01-09 15:26:19 +010094 Config_Helpers.Fill_Port_Config
95 (Port_Cfg, Pipe, Pipe_Cfg.Port, Pipe_Cfg.Mode, Success);
96
97 if Success then
98 Success := Config_Helpers.Validate_Config
99 (Pipe_Cfg.Framebuffer, Port_Cfg, Pipe);
100 end if;
101
Nico Huber43370ba2017-01-09 15:26:19 +0100102 if Success then
Nico Huber43370ba2017-01-09 15:26:19 +0100103 Connector_Info.Preferred_Link_Setting (Port_Cfg, Success);
104 end if;
105
106 -- loop over all possible DP-lane configurations
107 -- (non-DP ports use a single fake configuration)
108 while Success loop
109 pragma Loop_Invariant
110 (Pipe_Cfg.Port in Active_Port_Type and
111 Port_Cfg.Mode = Port_Cfg.Mode'Loop_Entry);
112
113 PLLs.Alloc
114 (Port_Cfg => Port_Cfg,
115 PLL => Allocated_PLLs (Pipe),
116 Success => Success);
117
118 if Success then
119 -- try each DP-lane configuration twice
120 for Try in 1 .. 2 loop
121 pragma Loop_Invariant
122 (Pipe_Cfg.Port in Active_Port_Type);
123
124 Connectors.Pre_On
125 (Pipe => Pipe,
126 Port_Cfg => Port_Cfg,
127 PLL_Hint => PLLs.Register_Value (Allocated_PLLs (Pipe)),
128 Success => Success);
129
130 if Success then
131 Display_Controller.On
132 (Pipe => Pipe,
133 Port_Cfg => Port_Cfg,
134 Framebuffer => Pipe_Cfg.Framebuffer);
135
136 Connectors.Post_On
137 (Port_Cfg => Port_Cfg,
138 PLL_Hint => PLLs.Register_Value (Allocated_PLLs (Pipe)),
139 Success => Success);
140
141 if not Success then
142 Display_Controller.Off (Pipe);
143 Connectors.Post_Off (Port_Cfg);
144 end if;
145 end if;
146
147 exit when Success;
148 end loop;
149 exit when Success; -- connection established => stop loop
150
151 -- connection failed
152 PLLs.Free (Allocated_PLLs (Pipe));
153 end if;
154
155 Connector_Info.Next_Link_Setting (Port_Cfg, Success);
156 end loop;
157
158 if Success then
159 pragma Debug (Debug.Put_Line
160 ("Enabled port " & Port_Names (Pipe_Cfg.Port)));
161 else
162 Wait_For_HPD (Pipe_Cfg.Port) := True;
163 if Pipe_Cfg.Port = Internal then
164 Panel.Off;
165 end if;
166 end if;
167 end Enable_Output;
168
Nico Huber3be61d42017-01-09 13:58:18 +0100169 procedure Disable_Output (Pipe : Pipe_Index; Pipe_Cfg : Pipe_Config)
170 is
171 Port_Cfg : Port_Config;
172 Success : Boolean;
173 begin
174 Config_Helpers.Fill_Port_Config
175 (Port_Cfg, Pipe, Pipe_Cfg.Port, Pipe_Cfg.Mode, Success);
176 if Success then
177 pragma Debug (Debug.New_Line);
178 pragma Debug (Debug.Put_Line
179 ("Disabling port " & Port_Names (Pipe_Cfg.Port)));
180 pragma Debug (Debug.New_Line);
181
182 Connectors.Pre_Off (Port_Cfg);
183 Display_Controller.Off (Pipe);
184 Connectors.Post_Off (Port_Cfg);
185
186 PLLs.Free (Allocated_PLLs (Pipe));
187 end if;
188 end Disable_Output;
189
Nico Huber99f10f32016-11-20 00:34:05 +0100190 procedure Update_Outputs (Configs : Pipe_Configs)
Nico Huber83693c82016-10-08 22:17:55 +0200191 is
Nico Huber3be61d42017-01-09 13:58:18 +0100192 procedure Check_HPD (Port : in Active_Port_Type; Detected : out Boolean)
193 is
194 HPD_Delay_Over : constant Boolean := Time.Timed_Out (HPD_Delay (Port));
195 begin
196 if HPD_Delay_Over then
197 Port_Detect.Hotplug_Detect (Port, Detected);
198 HPD_Delay (Port) := Time.MS_From_Now (333);
199 else
200 Detected := False;
201 end if;
202 end Check_HPD;
Nico Huberb56b9c52017-01-11 15:12:23 +0100203
Nico Huber564103f2017-01-11 15:33:07 +0100204 Power_Changed : Boolean := False;
Nico Huberb56b9c52017-01-11 15:12:23 +0100205 Old_Configs : Pipe_Configs;
Nico Huber564103f2017-01-11 15:33:07 +0100206
207 -- Only called when we actually tried to change something
208 -- so we don't congest the log with unnecessary messages.
209 procedure Update_Power
210 is
211 begin
212 if not Power_Changed then
213 Power_And_Clocks.Power_Up (Old_Configs, Configs);
214 Power_Changed := True;
215 end if;
216 end Update_Power;
Nico Huber83693c82016-10-08 22:17:55 +0200217 begin
218 Old_Configs := Cur_Configs;
219
Nico Huberb56b9c52017-01-11 15:12:23 +0100220 -- disable all pipes that changed or had a hot-plug event
221 for Pipe in Pipe_Index loop
222 declare
223 Unplug_Detected : Boolean;
224 Cur_Config : Pipe_Config renames Cur_Configs (Pipe);
225 New_Config : Pipe_Config renames Configs (Pipe);
226 begin
227 if Cur_Config.Port /= Disabled then
228 Check_HPD (Cur_Config.Port, Unplug_Detected);
Nico Huber83693c82016-10-08 22:17:55 +0200229
Nico Huberb56b9c52017-01-11 15:12:23 +0100230 if Cur_Config.Port /= New_Config.Port or
231 Cur_Config.Mode /= New_Config.Mode or
232 Unplug_Detected
233 then
234 Disable_Output (Pipe, Cur_Config);
235 Cur_Config.Port := Disabled;
Nico Huber564103f2017-01-11 15:33:07 +0100236 Update_Power;
Nico Huberb56b9c52017-01-11 15:12:23 +0100237 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200238 end if;
Nico Huberb56b9c52017-01-11 15:12:23 +0100239 end;
240 end loop;
Nico Huber83693c82016-10-08 22:17:55 +0200241
Nico Huberb56b9c52017-01-11 15:12:23 +0100242 -- enable all pipes that changed and should be active
243 for Pipe in Pipe_Index loop
244 declare
245 Success : Boolean;
246 Cur_Config : Pipe_Config renames Cur_Configs (Pipe);
247 New_Config : Pipe_Config renames Configs (Pipe);
248 begin
249 if New_Config.Port /= Disabled and then
250 (Cur_Config.Port /= New_Config.Port or
251 Cur_Config.Mode /= New_Config.Mode)
252 then
Nico Huber3be61d42017-01-09 13:58:18 +0100253 if Wait_For_HPD (New_Config.Port) then
254 Check_HPD (New_Config.Port, Success);
255 Wait_For_HPD (New_Config.Port) := not Success;
256 else
257 Success := True;
Nico Huber8c45bcf2016-11-20 17:30:57 +0100258 end if;
Nico Huberc7a4fee2016-11-03 18:18:03 +0100259
Nico Huber3be61d42017-01-09 13:58:18 +0100260 if Success then
Nico Huber564103f2017-01-11 15:33:07 +0100261 Update_Power;
Nico Huberb56b9c52017-01-11 15:12:23 +0100262 Enable_Output (Pipe, New_Config, Success);
Nico Huber3be61d42017-01-09 13:58:18 +0100263 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200264
265 if Success then
Nico Huberb56b9c52017-01-11 15:12:23 +0100266 Cur_Config := New_Config;
Nico Huber83693c82016-10-08 22:17:55 +0200267 end if;
Nico Huber3be61d42017-01-09 13:58:18 +0100268
Nico Huberb56b9c52017-01-11 15:12:23 +0100269 -- update framebuffer offset only
270 elsif New_Config.Port /= Disabled and
271 Cur_Config.Framebuffer /= New_Config.Framebuffer
272 then
273 Display_Controller.Update_Offset (Pipe, New_Config.Framebuffer);
274 Cur_Config := New_Config;
275 end if;
276 end;
Nico Huber83693c82016-10-08 22:17:55 +0200277 end loop;
278
Nico Huber564103f2017-01-11 15:33:07 +0100279 if Power_Changed then
Nico Huber83693c82016-10-08 22:17:55 +0200280 Power_And_Clocks.Power_Down (Old_Configs, Configs, Cur_Configs);
281 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200282 end Update_Outputs;
283
284 ----------------------------------------------------------------------------
285
286 procedure Initialize
287 (MMIO_Base : in Word64 := 0;
288 Write_Delay : in Word64 := 0;
Nico Huber793a8d42016-11-21 18:57:03 +0100289 Clean_State : in Boolean := False;
Nico Huber83693c82016-10-08 22:17:55 +0200290 Success : out Boolean)
291 with
292 Refined_Global =>
293 (In_Out =>
294 (Config.Valid_Port_GPU,
295 Registers.Register_State, Port_IO.State),
296 Input =>
297 (Time.State),
298 Output =>
299 (Registers.Address_State,
300 PLLs.State, Panel.Panel_State,
Nico Huber1a712d32017-01-09 15:11:04 +0100301 Cur_Configs, Allocated_PLLs,
Nico Huber83693c82016-10-08 22:17:55 +0200302 HPD_Delay, Wait_For_HPD, Initialized))
303 is
304 use type HW.Word64;
305
306 Now : constant Time.T := Time.Now;
307
308 procedure Check_Platform (Success : out Boolean)
309 is
310 Audio_VID_DID : Word32;
311 begin
312 case Config.CPU is
313 when Haswell .. Skylake =>
314 Registers.Read (Registers.AUD_VID_DID, Audio_VID_DID);
315 when Ironlake .. Ivybridge =>
316 Registers.Read (Registers.PCH_AUD_VID_DID, Audio_VID_DID);
317 end case;
318 Success :=
319 (case Config.CPU is
320 when Skylake => Audio_VID_DID = 16#8086_2809#,
321 when Broadwell => Audio_VID_DID = 16#8086_2808#,
322 when Haswell => Audio_VID_DID = 16#8086_2807#,
323 when Ivybridge |
324 Sandybridge => Audio_VID_DID = 16#8086_2806# or
325 Audio_VID_DID = 16#8086_2805#,
Nico Hubereeb5a392016-10-09 19:28:30 +0200326 when Ironlake => Audio_VID_DID = 16#0000_0000#);
Nico Huber83693c82016-10-08 22:17:55 +0200327 end Check_Platform;
328 begin
329 pragma Warnings (GNATprove, Off, "unused variable ""Write_Delay""",
330 Reason => "Write_Delay is used for debugging only");
331
332 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
333
334 pragma Debug (Debug.Set_Register_Write_Delay (Write_Delay));
335
336 Wait_For_HPD := HPD_Type'(others => False);
337 HPD_Delay := HPD_Delay_Type'(others => Now);
Nico Huber83693c82016-10-08 22:17:55 +0200338 Allocated_PLLs := (others => PLLs.Invalid);
Nico Huber99f10f32016-11-20 00:34:05 +0100339 Cur_Configs := Pipe_Configs'
340 (others => Pipe_Config'
Nico Huber83693c82016-10-08 22:17:55 +0200341 (Port => Disabled,
342 Framebuffer => HW.GFX.Default_FB,
343 Mode => HW.GFX.Invalid_Mode));
344 Registers.Set_Register_Base
345 (if MMIO_Base /= 0 then
346 MMIO_Base
347 else
348 Config.Default_MMIO_Base);
349 PLLs.Initialize;
350
351 Check_Platform (Success);
352 if not Success then
353 pragma Debug (Debug.Put_Line ("ERROR: Incompatible CPU or PCH."));
354
355 Panel.Static_Init; -- for flow analysis
356
357 Initialized := False;
358 return;
359 end if;
360
361 Panel.Setup_PP_Sequencer;
362 Port_Detect.Initialize;
363
Nico Huber793a8d42016-11-21 18:57:03 +0100364 if Clean_State then
365 Power_And_Clocks.Pre_All_Off;
366 Connectors.Pre_All_Off;
367 Display_Controller.All_Off;
368 Connectors.Post_All_Off;
369 PLLs.All_Off;
370 Power_And_Clocks.Post_All_Off;
Nico Huber33912aa2016-12-06 20:36:23 +0100371 else
372 -- According to PRMs, VGA plane is the only thing
373 -- that's enabled by default after reset.
374 Display_Controller.Legacy_VGA_Off;
Nico Huber793a8d42016-11-21 18:57:03 +0100375 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200376
377 -------------------- Now restart from a clean state ---------------------
378 Power_And_Clocks.Initialize;
379
Nico Huberf54d0962016-10-20 14:17:18 +0200380 Registers.Unset_And_Set_Mask
381 (Register => Registers.PCH_RAWCLK_FREQ,
382 Mask_Unset => PCH_RAWCLK_FREQ_MASK,
383 Mask_Set => PCH_RAWCLK_FREQ (Config.Default_RawClk_Freq));
384
Nico Huber83693c82016-10-08 22:17:55 +0200385 Initialized := True;
386
387 end Initialize;
388
389 function Is_Initialized return Boolean
390 with
391 Refined_Post => Is_Initialized'Result = Initialized
392 is
393 begin
394 return Initialized;
395 end Is_Initialized;
396
397 ----------------------------------------------------------------------------
398
399 procedure Write_GTT
400 (GTT_Page : GTT_Range;
401 Device_Address : GTT_Address_Type;
402 Valid : Boolean) is
403 begin
404 Registers.Write_GTT (GTT_Page, Device_Address, Valid);
405 end Write_GTT;
406
407 procedure Setup_Default_GTT (FB : Framebuffer_Type; Phys_FB : Word32)
408 is
409 FB_Size : constant Pos32 :=
410 FB.Stride * FB.Height * Pos32 (((FB.BPC * 4) / 8));
411 Phys_Addr : GTT_Address_Type := GTT_Address_Type (Phys_FB);
412 begin
413 for Idx in GTT_Range range 0 .. GTT_Range (((FB_Size + 4095) / 4096) - 1)
414 loop
415 Registers.Write_GTT
416 (GTT_Page => Idx,
417 Device_Address => Phys_Addr,
418 Valid => True);
419 Phys_Addr := Phys_Addr + 4096;
420 end loop;
421 end Setup_Default_GTT;
422
423 ----------------------------------------------------------------------------
424
Nico Huber99f10f32016-11-20 00:34:05 +0100425 procedure Dump_Configs (Configs : Pipe_Configs)
Nico Huber83693c82016-10-08 22:17:55 +0200426 is
427 subtype Pipe_Name is String (1 .. 9);
Nico Huber99f10f32016-11-20 00:34:05 +0100428 type Pipe_Name_Array is array (Pipe_Index) of Pipe_Name;
Nico Huber83693c82016-10-08 22:17:55 +0200429 Pipe_Names : constant Pipe_Name_Array :=
430 (Primary => "Primary ",
431 Secondary => "Secondary",
432 Tertiary => "Tertiary ");
433 begin
434 Debug.New_Line;
435 Debug.Put_Line ("CONFIG => ");
Nico Huber99f10f32016-11-20 00:34:05 +0100436 for Pipe in Pipe_Index loop
437 if Pipe = Pipe_Index'First then
Nico Huber83693c82016-10-08 22:17:55 +0200438 Debug.Put (" (");
439 else
440 Debug.Put (" ");
441 end if;
442 Debug.Put_Line (Pipe_Names (Pipe) & " =>");
443 Debug.Put_Line
444 (" (Port => " & Port_Names (Configs (Pipe).Port) & ",");
445 Debug.Put_Line (" Framebuffer =>");
446 Debug.Put (" (Width => ");
447 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Width);
448 Debug.Put_Line (",");
449 Debug.Put (" Height => ");
450 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Height);
451 Debug.Put_Line (",");
452 Debug.Put (" Stride => ");
453 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Stride);
454 Debug.Put_Line (",");
455 Debug.Put (" Offset => ");
456 Debug.Put_Word32 (Configs (Pipe).Framebuffer.Offset);
457 Debug.Put_Line (",");
458 Debug.Put (" BPC => ");
459 Debug.Put_Int64 (Configs (Pipe).Framebuffer.BPC);
460 Debug.Put_Line ("),");
461 Debug.Put_Line (" Mode =>");
462 Debug.Put (" (Dotclock => ");
463 Debug.Put_Int64 (Configs (Pipe).Mode.Dotclock);
464 Debug.Put_Line (",");
465 Debug.Put (" H_Visible => ");
466 Debug.Put_Int16 (Configs (Pipe).Mode.H_Visible);
467 Debug.Put_Line (",");
468 Debug.Put (" H_Sync_Begin => ");
469 Debug.Put_Int16 (Configs (Pipe).Mode.H_Sync_Begin);
470 Debug.Put_Line (",");
471 Debug.Put (" H_Sync_End => ");
472 Debug.Put_Int16 (Configs (Pipe).Mode.H_Sync_End);
473 Debug.Put_Line (",");
474 Debug.Put (" H_Total => ");
475 Debug.Put_Int16 (Configs (Pipe).Mode.H_Total);
476 Debug.Put_Line (",");
477 Debug.Put (" V_Visible => ");
478 Debug.Put_Int16 (Configs (Pipe).Mode.V_Visible);
479 Debug.Put_Line (",");
480 Debug.Put (" V_Sync_Begin => ");
481 Debug.Put_Int16 (Configs (Pipe).Mode.V_Sync_Begin);
482 Debug.Put_Line (",");
483 Debug.Put (" V_Sync_End => ");
484 Debug.Put_Int16 (Configs (Pipe).Mode.V_Sync_End);
485 Debug.Put_Line (",");
486 Debug.Put (" V_Total => ");
487 Debug.Put_Int16 (Configs (Pipe).Mode.V_Total);
488 Debug.Put_Line (",");
489 Debug.Put_Line (" H_Sync_Active_High => " &
490 (if Configs (Pipe).Mode.H_Sync_Active_High
491 then "True,"
492 else "False,"));
493 Debug.Put_Line (" V_Sync_Active_High => " &
494 (if Configs (Pipe).Mode.V_Sync_Active_High
495 then "True,"
496 else "False,"));
497 Debug.Put (" BPC => ");
498 Debug.Put_Int64 (Configs (Pipe).Mode.BPC);
Nico Huber99f10f32016-11-20 00:34:05 +0100499 if Pipe /= Pipe_Index'Last then
Nico Huber83693c82016-10-08 22:17:55 +0200500 Debug.Put_Line (")),");
501 else
502 Debug.Put_Line (")));");
503 end if;
504 end loop;
505 end Dump_Configs;
506
507end HW.GFX.GMA;