blob: e1964fa05099f8bc943832d012c08e7d005b3f38 [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
204 Did_Power_Up : Boolean := False;
205 Old_Configs : Pipe_Configs;
Nico Huber83693c82016-10-08 22:17:55 +0200206 begin
207 Old_Configs := Cur_Configs;
208
Nico Huberb56b9c52017-01-11 15:12:23 +0100209 -- disable all pipes that changed or had a hot-plug event
210 for Pipe in Pipe_Index loop
211 declare
212 Unplug_Detected : Boolean;
213 Cur_Config : Pipe_Config renames Cur_Configs (Pipe);
214 New_Config : Pipe_Config renames Configs (Pipe);
215 begin
216 if Cur_Config.Port /= Disabled then
217 Check_HPD (Cur_Config.Port, Unplug_Detected);
Nico Huber83693c82016-10-08 22:17:55 +0200218
Nico Huberb56b9c52017-01-11 15:12:23 +0100219 if Cur_Config.Port /= New_Config.Port or
220 Cur_Config.Mode /= New_Config.Mode or
221 Unplug_Detected
222 then
223 Disable_Output (Pipe, Cur_Config);
224 Cur_Config.Port := Disabled;
225 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200226 end if;
Nico Huberb56b9c52017-01-11 15:12:23 +0100227 end;
228 end loop;
Nico Huber83693c82016-10-08 22:17:55 +0200229
Nico Huberb56b9c52017-01-11 15:12:23 +0100230 -- enable all pipes that changed and should be active
231 for Pipe in Pipe_Index loop
232 declare
233 Success : Boolean;
234 Cur_Config : Pipe_Config renames Cur_Configs (Pipe);
235 New_Config : Pipe_Config renames Configs (Pipe);
236 begin
237 if New_Config.Port /= Disabled and then
238 (Cur_Config.Port /= New_Config.Port or
239 Cur_Config.Mode /= New_Config.Mode)
240 then
Nico Huber3be61d42017-01-09 13:58:18 +0100241 if Wait_For_HPD (New_Config.Port) then
242 Check_HPD (New_Config.Port, Success);
243 Wait_For_HPD (New_Config.Port) := not Success;
244 else
245 Success := True;
Nico Huber8c45bcf2016-11-20 17:30:57 +0100246 end if;
Nico Huberc7a4fee2016-11-03 18:18:03 +0100247
Nico Huber3be61d42017-01-09 13:58:18 +0100248 if Success then
249 if not Did_Power_Up then
250 Power_And_Clocks.Power_Up (Old_Configs, Configs);
251 Did_Power_Up := True;
252 end if;
Nico Huberb56b9c52017-01-11 15:12:23 +0100253 Enable_Output (Pipe, New_Config, Success);
Nico Huber3be61d42017-01-09 13:58:18 +0100254 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200255
256 if Success then
Nico Huberb56b9c52017-01-11 15:12:23 +0100257 Cur_Config := New_Config;
Nico Huber83693c82016-10-08 22:17:55 +0200258 end if;
Nico Huber3be61d42017-01-09 13:58:18 +0100259
Nico Huberb56b9c52017-01-11 15:12:23 +0100260 -- update framebuffer offset only
261 elsif New_Config.Port /= Disabled and
262 Cur_Config.Framebuffer /= New_Config.Framebuffer
263 then
264 Display_Controller.Update_Offset (Pipe, New_Config.Framebuffer);
265 Cur_Config := New_Config;
266 end if;
267 end;
Nico Huber83693c82016-10-08 22:17:55 +0200268 end loop;
269
270 if Did_Power_Up then
271 Power_And_Clocks.Power_Down (Old_Configs, Configs, Cur_Configs);
272 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200273 end Update_Outputs;
274
275 ----------------------------------------------------------------------------
276
277 procedure Initialize
278 (MMIO_Base : in Word64 := 0;
279 Write_Delay : in Word64 := 0;
Nico Huber793a8d42016-11-21 18:57:03 +0100280 Clean_State : in Boolean := False;
Nico Huber83693c82016-10-08 22:17:55 +0200281 Success : out Boolean)
282 with
283 Refined_Global =>
284 (In_Out =>
285 (Config.Valid_Port_GPU,
286 Registers.Register_State, Port_IO.State),
287 Input =>
288 (Time.State),
289 Output =>
290 (Registers.Address_State,
291 PLLs.State, Panel.Panel_State,
Nico Huber1a712d32017-01-09 15:11:04 +0100292 Cur_Configs, Allocated_PLLs,
Nico Huber83693c82016-10-08 22:17:55 +0200293 HPD_Delay, Wait_For_HPD, Initialized))
294 is
295 use type HW.Word64;
296
297 Now : constant Time.T := Time.Now;
298
299 procedure Check_Platform (Success : out Boolean)
300 is
301 Audio_VID_DID : Word32;
302 begin
303 case Config.CPU is
304 when Haswell .. Skylake =>
305 Registers.Read (Registers.AUD_VID_DID, Audio_VID_DID);
306 when Ironlake .. Ivybridge =>
307 Registers.Read (Registers.PCH_AUD_VID_DID, Audio_VID_DID);
308 end case;
309 Success :=
310 (case Config.CPU is
311 when Skylake => Audio_VID_DID = 16#8086_2809#,
312 when Broadwell => Audio_VID_DID = 16#8086_2808#,
313 when Haswell => Audio_VID_DID = 16#8086_2807#,
314 when Ivybridge |
315 Sandybridge => Audio_VID_DID = 16#8086_2806# or
316 Audio_VID_DID = 16#8086_2805#,
Nico Hubereeb5a392016-10-09 19:28:30 +0200317 when Ironlake => Audio_VID_DID = 16#0000_0000#);
Nico Huber83693c82016-10-08 22:17:55 +0200318 end Check_Platform;
319 begin
320 pragma Warnings (GNATprove, Off, "unused variable ""Write_Delay""",
321 Reason => "Write_Delay is used for debugging only");
322
323 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
324
325 pragma Debug (Debug.Set_Register_Write_Delay (Write_Delay));
326
327 Wait_For_HPD := HPD_Type'(others => False);
328 HPD_Delay := HPD_Delay_Type'(others => Now);
Nico Huber83693c82016-10-08 22:17:55 +0200329 Allocated_PLLs := (others => PLLs.Invalid);
Nico Huber99f10f32016-11-20 00:34:05 +0100330 Cur_Configs := Pipe_Configs'
331 (others => Pipe_Config'
Nico Huber83693c82016-10-08 22:17:55 +0200332 (Port => Disabled,
333 Framebuffer => HW.GFX.Default_FB,
334 Mode => HW.GFX.Invalid_Mode));
335 Registers.Set_Register_Base
336 (if MMIO_Base /= 0 then
337 MMIO_Base
338 else
339 Config.Default_MMIO_Base);
340 PLLs.Initialize;
341
342 Check_Platform (Success);
343 if not Success then
344 pragma Debug (Debug.Put_Line ("ERROR: Incompatible CPU or PCH."));
345
346 Panel.Static_Init; -- for flow analysis
347
348 Initialized := False;
349 return;
350 end if;
351
352 Panel.Setup_PP_Sequencer;
353 Port_Detect.Initialize;
354
Nico Huber793a8d42016-11-21 18:57:03 +0100355 if Clean_State then
356 Power_And_Clocks.Pre_All_Off;
357 Connectors.Pre_All_Off;
358 Display_Controller.All_Off;
359 Connectors.Post_All_Off;
360 PLLs.All_Off;
361 Power_And_Clocks.Post_All_Off;
Nico Huber33912aa2016-12-06 20:36:23 +0100362 else
363 -- According to PRMs, VGA plane is the only thing
364 -- that's enabled by default after reset.
365 Display_Controller.Legacy_VGA_Off;
Nico Huber793a8d42016-11-21 18:57:03 +0100366 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200367
368 -------------------- Now restart from a clean state ---------------------
369 Power_And_Clocks.Initialize;
370
Nico Huberf54d0962016-10-20 14:17:18 +0200371 Registers.Unset_And_Set_Mask
372 (Register => Registers.PCH_RAWCLK_FREQ,
373 Mask_Unset => PCH_RAWCLK_FREQ_MASK,
374 Mask_Set => PCH_RAWCLK_FREQ (Config.Default_RawClk_Freq));
375
Nico Huber83693c82016-10-08 22:17:55 +0200376 Initialized := True;
377
378 end Initialize;
379
380 function Is_Initialized return Boolean
381 with
382 Refined_Post => Is_Initialized'Result = Initialized
383 is
384 begin
385 return Initialized;
386 end Is_Initialized;
387
388 ----------------------------------------------------------------------------
389
390 procedure Write_GTT
391 (GTT_Page : GTT_Range;
392 Device_Address : GTT_Address_Type;
393 Valid : Boolean) is
394 begin
395 Registers.Write_GTT (GTT_Page, Device_Address, Valid);
396 end Write_GTT;
397
398 procedure Setup_Default_GTT (FB : Framebuffer_Type; Phys_FB : Word32)
399 is
400 FB_Size : constant Pos32 :=
401 FB.Stride * FB.Height * Pos32 (((FB.BPC * 4) / 8));
402 Phys_Addr : GTT_Address_Type := GTT_Address_Type (Phys_FB);
403 begin
404 for Idx in GTT_Range range 0 .. GTT_Range (((FB_Size + 4095) / 4096) - 1)
405 loop
406 Registers.Write_GTT
407 (GTT_Page => Idx,
408 Device_Address => Phys_Addr,
409 Valid => True);
410 Phys_Addr := Phys_Addr + 4096;
411 end loop;
412 end Setup_Default_GTT;
413
414 ----------------------------------------------------------------------------
415
Nico Huber99f10f32016-11-20 00:34:05 +0100416 procedure Dump_Configs (Configs : Pipe_Configs)
Nico Huber83693c82016-10-08 22:17:55 +0200417 is
418 subtype Pipe_Name is String (1 .. 9);
Nico Huber99f10f32016-11-20 00:34:05 +0100419 type Pipe_Name_Array is array (Pipe_Index) of Pipe_Name;
Nico Huber83693c82016-10-08 22:17:55 +0200420 Pipe_Names : constant Pipe_Name_Array :=
421 (Primary => "Primary ",
422 Secondary => "Secondary",
423 Tertiary => "Tertiary ");
424 begin
425 Debug.New_Line;
426 Debug.Put_Line ("CONFIG => ");
Nico Huber99f10f32016-11-20 00:34:05 +0100427 for Pipe in Pipe_Index loop
428 if Pipe = Pipe_Index'First then
Nico Huber83693c82016-10-08 22:17:55 +0200429 Debug.Put (" (");
430 else
431 Debug.Put (" ");
432 end if;
433 Debug.Put_Line (Pipe_Names (Pipe) & " =>");
434 Debug.Put_Line
435 (" (Port => " & Port_Names (Configs (Pipe).Port) & ",");
436 Debug.Put_Line (" Framebuffer =>");
437 Debug.Put (" (Width => ");
438 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Width);
439 Debug.Put_Line (",");
440 Debug.Put (" Height => ");
441 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Height);
442 Debug.Put_Line (",");
443 Debug.Put (" Stride => ");
444 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Stride);
445 Debug.Put_Line (",");
446 Debug.Put (" Offset => ");
447 Debug.Put_Word32 (Configs (Pipe).Framebuffer.Offset);
448 Debug.Put_Line (",");
449 Debug.Put (" BPC => ");
450 Debug.Put_Int64 (Configs (Pipe).Framebuffer.BPC);
451 Debug.Put_Line ("),");
452 Debug.Put_Line (" Mode =>");
453 Debug.Put (" (Dotclock => ");
454 Debug.Put_Int64 (Configs (Pipe).Mode.Dotclock);
455 Debug.Put_Line (",");
456 Debug.Put (" H_Visible => ");
457 Debug.Put_Int16 (Configs (Pipe).Mode.H_Visible);
458 Debug.Put_Line (",");
459 Debug.Put (" H_Sync_Begin => ");
460 Debug.Put_Int16 (Configs (Pipe).Mode.H_Sync_Begin);
461 Debug.Put_Line (",");
462 Debug.Put (" H_Sync_End => ");
463 Debug.Put_Int16 (Configs (Pipe).Mode.H_Sync_End);
464 Debug.Put_Line (",");
465 Debug.Put (" H_Total => ");
466 Debug.Put_Int16 (Configs (Pipe).Mode.H_Total);
467 Debug.Put_Line (",");
468 Debug.Put (" V_Visible => ");
469 Debug.Put_Int16 (Configs (Pipe).Mode.V_Visible);
470 Debug.Put_Line (",");
471 Debug.Put (" V_Sync_Begin => ");
472 Debug.Put_Int16 (Configs (Pipe).Mode.V_Sync_Begin);
473 Debug.Put_Line (",");
474 Debug.Put (" V_Sync_End => ");
475 Debug.Put_Int16 (Configs (Pipe).Mode.V_Sync_End);
476 Debug.Put_Line (",");
477 Debug.Put (" V_Total => ");
478 Debug.Put_Int16 (Configs (Pipe).Mode.V_Total);
479 Debug.Put_Line (",");
480 Debug.Put_Line (" H_Sync_Active_High => " &
481 (if Configs (Pipe).Mode.H_Sync_Active_High
482 then "True,"
483 else "False,"));
484 Debug.Put_Line (" V_Sync_Active_High => " &
485 (if Configs (Pipe).Mode.V_Sync_Active_High
486 then "True,"
487 else "False,"));
488 Debug.Put (" BPC => ");
489 Debug.Put_Int64 (Configs (Pipe).Mode.BPC);
Nico Huber99f10f32016-11-20 00:34:05 +0100490 if Pipe /= Pipe_Index'Last then
Nico Huber83693c82016-10-08 22:17:55 +0200491 Debug.Put_Line (")),");
492 else
493 Debug.Put_Line (")));");
494 end if;
495 end loop;
496 end Dump_Configs;
497
498end HW.GFX.GMA;