blob: cbb18a6859159a57dee2478f48829c2bb4c1dd10 [file] [log] [blame]
Nico Huber83693c82016-10-08 22:17:55 +02001--
2-- Copyright (C) 2014-2016 secunet Security Networks AG
3--
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;
64 type HPD_Delay_Type is array (Port_Type) of Time.T;
65
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 Check_HPD
84 (Port_Cfg : in Port_Config;
85 Port : in Port_Type;
86 Detected : out Boolean)
87 is
88 HPD_Delay_Over : constant Boolean := Time.Timed_Out (HPD_Delay (Port));
89 begin
90 if HPD_Delay_Over then
91 Port_Detect.Hotplug_Detect (Port_Cfg, Detected);
92 HPD_Delay (Port) := Time.MS_From_Now (333);
93 else
94 Detected := False;
95 end if;
96 end Check_HPD;
97
98 procedure Enable_Output
99 (Pipe : in Pipe_Index;
100 Pipe_Cfg : in Pipe_Config;
101 Success : out Boolean)
102 is
103 Port_Cfg : Port_Config;
104 begin
105 Config_Helpers.Fill_Port_Config
106 (Port_Cfg, Pipe, Pipe_Cfg.Port, Pipe_Cfg.Mode, Success);
107
108 if Success then
109 Success := Config_Helpers.Validate_Config
110 (Pipe_Cfg.Framebuffer, Port_Cfg, Pipe);
111 end if;
112
113 if Success and then Wait_For_HPD (Pipe_Cfg.Port) then
114 Check_HPD (Port_Cfg, Pipe_Cfg.Port, Success);
115 Wait_For_HPD (Pipe_Cfg.Port) := not Success;
116 end if;
117
118 if Success then
119 pragma Debug (Debug.New_Line);
120 pragma Debug (Debug.Put_Line
121 ("Trying to enable port " & Port_Names (Pipe_Cfg.Port)));
122
123 Connector_Info.Preferred_Link_Setting (Port_Cfg, Success);
124 end if;
125
126 -- loop over all possible DP-lane configurations
127 -- (non-DP ports use a single fake configuration)
128 while Success loop
129 pragma Loop_Invariant
130 (Pipe_Cfg.Port in Active_Port_Type and
131 Port_Cfg.Mode = Port_Cfg.Mode'Loop_Entry);
132
133 PLLs.Alloc
134 (Port_Cfg => Port_Cfg,
135 PLL => Allocated_PLLs (Pipe),
136 Success => Success);
137
138 if Success then
139 -- try each DP-lane configuration twice
140 for Try in 1 .. 2 loop
141 pragma Loop_Invariant
142 (Pipe_Cfg.Port in Active_Port_Type);
143
144 Connectors.Pre_On
145 (Pipe => Pipe,
146 Port_Cfg => Port_Cfg,
147 PLL_Hint => PLLs.Register_Value (Allocated_PLLs (Pipe)),
148 Success => Success);
149
150 if Success then
151 Display_Controller.On
152 (Pipe => Pipe,
153 Port_Cfg => Port_Cfg,
154 Framebuffer => Pipe_Cfg.Framebuffer);
155
156 Connectors.Post_On
157 (Port_Cfg => Port_Cfg,
158 PLL_Hint => PLLs.Register_Value (Allocated_PLLs (Pipe)),
159 Success => Success);
160
161 if not Success then
162 Display_Controller.Off (Pipe);
163 Connectors.Post_Off (Port_Cfg);
164 end if;
165 end if;
166
167 exit when Success;
168 end loop;
169 exit when Success; -- connection established => stop loop
170
171 -- connection failed
172 PLLs.Free (Allocated_PLLs (Pipe));
173 end if;
174
175 Connector_Info.Next_Link_Setting (Port_Cfg, Success);
176 end loop;
177
178 if Success then
179 pragma Debug (Debug.Put_Line
180 ("Enabled port " & Port_Names (Pipe_Cfg.Port)));
181 else
182 Wait_For_HPD (Pipe_Cfg.Port) := True;
183 if Pipe_Cfg.Port = Internal then
184 Panel.Off;
185 end if;
186 end if;
187 end Enable_Output;
188
Nico Huber99f10f32016-11-20 00:34:05 +0100189 procedure Update_Outputs (Configs : Pipe_Configs)
Nico Huber83693c82016-10-08 22:17:55 +0200190 is
191 Did_Power_Up : Boolean := False;
192
Nico Huber43370ba2017-01-09 15:26:19 +0100193 HPD, Success : Boolean;
Nico Huber99f10f32016-11-20 00:34:05 +0100194 Old_Config, New_Config : Pipe_Config;
195 Old_Configs : Pipe_Configs;
Nico Huber83693c82016-10-08 22:17:55 +0200196 Port_Cfg : Port_Config;
Nico Huber83693c82016-10-08 22:17:55 +0200197 begin
198 Old_Configs := Cur_Configs;
199
Nico Huber99f10f32016-11-20 00:34:05 +0100200 for I in Pipe_Index loop
Nico Huber83693c82016-10-08 22:17:55 +0200201 HPD := False;
202
203 Old_Config := Cur_Configs (I);
204 New_Config := Configs (I);
205
Nico Huber8c45bcf2016-11-20 17:30:57 +0100206 Config_Helpers.Fill_Port_Config
Nico Huber3c544ee2016-11-20 04:56:58 +0100207 (Port_Cfg, I, Old_Configs (I).Port, Old_Configs (I).Mode, Success);
Nico Huber83693c82016-10-08 22:17:55 +0200208 if Success then
209 Check_HPD (Port_Cfg, Old_Config.Port, HPD);
210 end if;
211
212 -- Connector changed?
213 if (Success and then HPD) or
214 Old_Config.Port /= New_Config.Port or
215 Old_Config.Mode /= New_Config.Mode
216 then
217 if Old_Config.Port /= Disabled then
218 if Success then
219 pragma Debug (Debug.New_Line);
220 pragma Debug (Debug.Put_Line
221 ("Disabling port " & Port_Names (Old_Config.Port)));
222
223 Connectors.Pre_Off (Port_Cfg);
224
Nico Huber7ad2d652016-12-07 15:19:32 +0100225 Display_Controller.Off (I);
Nico Huber83693c82016-10-08 22:17:55 +0200226
227 Connectors.Post_Off (Port_Cfg);
228 end if;
229
230 -- Free PLL
231 PLLs.Free (Allocated_PLLs (I));
232
233 Cur_Configs (I).Port := Disabled;
234 end if;
235
236 if New_Config.Port /= Disabled then
Nico Huber43370ba2017-01-09 15:26:19 +0100237 if not Did_Power_Up then
238 Power_And_Clocks.Power_Up (Old_Configs, Configs);
239 Did_Power_Up := True;
Nico Huber8c45bcf2016-11-20 17:30:57 +0100240 end if;
Nico Huberc7a4fee2016-11-03 18:18:03 +0100241
Nico Huber43370ba2017-01-09 15:26:19 +0100242 Enable_Output (I, New_Config, Success);
Nico Huber83693c82016-10-08 22:17:55 +0200243
244 if Success then
Nico Huber83693c82016-10-08 22:17:55 +0200245 Cur_Configs (I) := New_Config;
Nico Huber83693c82016-10-08 22:17:55 +0200246 end if;
247 else
248 Cur_Configs (I) := New_Config;
249 end if;
250 elsif Old_Config.Framebuffer /= New_Config.Framebuffer and
251 Old_Config.Port /= Disabled
252 then
Nico Huberf3e23662016-12-05 21:33:03 +0100253 Display_Controller.Update_Offset (I, New_Config.Framebuffer);
Nico Huber83693c82016-10-08 22:17:55 +0200254 Cur_Configs (I) := New_Config;
255 end if;
256 end loop;
257
258 if Did_Power_Up then
259 Power_And_Clocks.Power_Down (Old_Configs, Configs, Cur_Configs);
260 end if;
261
262 end Update_Outputs;
263
264 ----------------------------------------------------------------------------
265
266 procedure Initialize
267 (MMIO_Base : in Word64 := 0;
268 Write_Delay : in Word64 := 0;
Nico Huber793a8d42016-11-21 18:57:03 +0100269 Clean_State : in Boolean := False;
Nico Huber83693c82016-10-08 22:17:55 +0200270 Success : out Boolean)
271 with
272 Refined_Global =>
273 (In_Out =>
274 (Config.Valid_Port_GPU,
275 Registers.Register_State, Port_IO.State),
276 Input =>
277 (Time.State),
278 Output =>
279 (Registers.Address_State,
280 PLLs.State, Panel.Panel_State,
Nico Huber1a712d32017-01-09 15:11:04 +0100281 Cur_Configs, Allocated_PLLs,
Nico Huber83693c82016-10-08 22:17:55 +0200282 HPD_Delay, Wait_For_HPD, Initialized))
283 is
284 use type HW.Word64;
285
286 Now : constant Time.T := Time.Now;
287
288 procedure Check_Platform (Success : out Boolean)
289 is
290 Audio_VID_DID : Word32;
291 begin
292 case Config.CPU is
293 when Haswell .. Skylake =>
294 Registers.Read (Registers.AUD_VID_DID, Audio_VID_DID);
295 when Ironlake .. Ivybridge =>
296 Registers.Read (Registers.PCH_AUD_VID_DID, Audio_VID_DID);
297 end case;
298 Success :=
299 (case Config.CPU is
300 when Skylake => Audio_VID_DID = 16#8086_2809#,
301 when Broadwell => Audio_VID_DID = 16#8086_2808#,
302 when Haswell => Audio_VID_DID = 16#8086_2807#,
303 when Ivybridge |
304 Sandybridge => Audio_VID_DID = 16#8086_2806# or
305 Audio_VID_DID = 16#8086_2805#,
Nico Hubereeb5a392016-10-09 19:28:30 +0200306 when Ironlake => Audio_VID_DID = 16#0000_0000#);
Nico Huber83693c82016-10-08 22:17:55 +0200307 end Check_Platform;
308 begin
309 pragma Warnings (GNATprove, Off, "unused variable ""Write_Delay""",
310 Reason => "Write_Delay is used for debugging only");
311
312 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
313
314 pragma Debug (Debug.Set_Register_Write_Delay (Write_Delay));
315
316 Wait_For_HPD := HPD_Type'(others => False);
317 HPD_Delay := HPD_Delay_Type'(others => Now);
Nico Huber83693c82016-10-08 22:17:55 +0200318 Allocated_PLLs := (others => PLLs.Invalid);
Nico Huber99f10f32016-11-20 00:34:05 +0100319 Cur_Configs := Pipe_Configs'
320 (others => Pipe_Config'
Nico Huber83693c82016-10-08 22:17:55 +0200321 (Port => Disabled,
322 Framebuffer => HW.GFX.Default_FB,
323 Mode => HW.GFX.Invalid_Mode));
324 Registers.Set_Register_Base
325 (if MMIO_Base /= 0 then
326 MMIO_Base
327 else
328 Config.Default_MMIO_Base);
329 PLLs.Initialize;
330
331 Check_Platform (Success);
332 if not Success then
333 pragma Debug (Debug.Put_Line ("ERROR: Incompatible CPU or PCH."));
334
335 Panel.Static_Init; -- for flow analysis
336
337 Initialized := False;
338 return;
339 end if;
340
341 Panel.Setup_PP_Sequencer;
342 Port_Detect.Initialize;
343
Nico Huber793a8d42016-11-21 18:57:03 +0100344 if Clean_State then
345 Power_And_Clocks.Pre_All_Off;
346 Connectors.Pre_All_Off;
347 Display_Controller.All_Off;
348 Connectors.Post_All_Off;
349 PLLs.All_Off;
350 Power_And_Clocks.Post_All_Off;
Nico Huber33912aa2016-12-06 20:36:23 +0100351 else
352 -- According to PRMs, VGA plane is the only thing
353 -- that's enabled by default after reset.
354 Display_Controller.Legacy_VGA_Off;
Nico Huber793a8d42016-11-21 18:57:03 +0100355 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200356
357 -------------------- Now restart from a clean state ---------------------
358 Power_And_Clocks.Initialize;
359
Nico Huberf54d0962016-10-20 14:17:18 +0200360 Registers.Unset_And_Set_Mask
361 (Register => Registers.PCH_RAWCLK_FREQ,
362 Mask_Unset => PCH_RAWCLK_FREQ_MASK,
363 Mask_Set => PCH_RAWCLK_FREQ (Config.Default_RawClk_Freq));
364
Nico Huber83693c82016-10-08 22:17:55 +0200365 Initialized := True;
366
367 end Initialize;
368
369 function Is_Initialized return Boolean
370 with
371 Refined_Post => Is_Initialized'Result = Initialized
372 is
373 begin
374 return Initialized;
375 end Is_Initialized;
376
377 ----------------------------------------------------------------------------
378
379 procedure Write_GTT
380 (GTT_Page : GTT_Range;
381 Device_Address : GTT_Address_Type;
382 Valid : Boolean) is
383 begin
384 Registers.Write_GTT (GTT_Page, Device_Address, Valid);
385 end Write_GTT;
386
387 procedure Setup_Default_GTT (FB : Framebuffer_Type; Phys_FB : Word32)
388 is
389 FB_Size : constant Pos32 :=
390 FB.Stride * FB.Height * Pos32 (((FB.BPC * 4) / 8));
391 Phys_Addr : GTT_Address_Type := GTT_Address_Type (Phys_FB);
392 begin
393 for Idx in GTT_Range range 0 .. GTT_Range (((FB_Size + 4095) / 4096) - 1)
394 loop
395 Registers.Write_GTT
396 (GTT_Page => Idx,
397 Device_Address => Phys_Addr,
398 Valid => True);
399 Phys_Addr := Phys_Addr + 4096;
400 end loop;
401 end Setup_Default_GTT;
402
403 ----------------------------------------------------------------------------
404
Nico Huber99f10f32016-11-20 00:34:05 +0100405 procedure Dump_Configs (Configs : Pipe_Configs)
Nico Huber83693c82016-10-08 22:17:55 +0200406 is
407 subtype Pipe_Name is String (1 .. 9);
Nico Huber99f10f32016-11-20 00:34:05 +0100408 type Pipe_Name_Array is array (Pipe_Index) of Pipe_Name;
Nico Huber83693c82016-10-08 22:17:55 +0200409 Pipe_Names : constant Pipe_Name_Array :=
410 (Primary => "Primary ",
411 Secondary => "Secondary",
412 Tertiary => "Tertiary ");
413 begin
414 Debug.New_Line;
415 Debug.Put_Line ("CONFIG => ");
Nico Huber99f10f32016-11-20 00:34:05 +0100416 for Pipe in Pipe_Index loop
417 if Pipe = Pipe_Index'First then
Nico Huber83693c82016-10-08 22:17:55 +0200418 Debug.Put (" (");
419 else
420 Debug.Put (" ");
421 end if;
422 Debug.Put_Line (Pipe_Names (Pipe) & " =>");
423 Debug.Put_Line
424 (" (Port => " & Port_Names (Configs (Pipe).Port) & ",");
425 Debug.Put_Line (" Framebuffer =>");
426 Debug.Put (" (Width => ");
427 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Width);
428 Debug.Put_Line (",");
429 Debug.Put (" Height => ");
430 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Height);
431 Debug.Put_Line (",");
432 Debug.Put (" Stride => ");
433 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Stride);
434 Debug.Put_Line (",");
435 Debug.Put (" Offset => ");
436 Debug.Put_Word32 (Configs (Pipe).Framebuffer.Offset);
437 Debug.Put_Line (",");
438 Debug.Put (" BPC => ");
439 Debug.Put_Int64 (Configs (Pipe).Framebuffer.BPC);
440 Debug.Put_Line ("),");
441 Debug.Put_Line (" Mode =>");
442 Debug.Put (" (Dotclock => ");
443 Debug.Put_Int64 (Configs (Pipe).Mode.Dotclock);
444 Debug.Put_Line (",");
445 Debug.Put (" H_Visible => ");
446 Debug.Put_Int16 (Configs (Pipe).Mode.H_Visible);
447 Debug.Put_Line (",");
448 Debug.Put (" H_Sync_Begin => ");
449 Debug.Put_Int16 (Configs (Pipe).Mode.H_Sync_Begin);
450 Debug.Put_Line (",");
451 Debug.Put (" H_Sync_End => ");
452 Debug.Put_Int16 (Configs (Pipe).Mode.H_Sync_End);
453 Debug.Put_Line (",");
454 Debug.Put (" H_Total => ");
455 Debug.Put_Int16 (Configs (Pipe).Mode.H_Total);
456 Debug.Put_Line (",");
457 Debug.Put (" V_Visible => ");
458 Debug.Put_Int16 (Configs (Pipe).Mode.V_Visible);
459 Debug.Put_Line (",");
460 Debug.Put (" V_Sync_Begin => ");
461 Debug.Put_Int16 (Configs (Pipe).Mode.V_Sync_Begin);
462 Debug.Put_Line (",");
463 Debug.Put (" V_Sync_End => ");
464 Debug.Put_Int16 (Configs (Pipe).Mode.V_Sync_End);
465 Debug.Put_Line (",");
466 Debug.Put (" V_Total => ");
467 Debug.Put_Int16 (Configs (Pipe).Mode.V_Total);
468 Debug.Put_Line (",");
469 Debug.Put_Line (" H_Sync_Active_High => " &
470 (if Configs (Pipe).Mode.H_Sync_Active_High
471 then "True,"
472 else "False,"));
473 Debug.Put_Line (" V_Sync_Active_High => " &
474 (if Configs (Pipe).Mode.V_Sync_Active_High
475 then "True,"
476 else "False,"));
477 Debug.Put (" BPC => ");
478 Debug.Put_Int64 (Configs (Pipe).Mode.BPC);
Nico Huber99f10f32016-11-20 00:34:05 +0100479 if Pipe /= Pipe_Index'Last then
Nico Huber83693c82016-10-08 22:17:55 +0200480 Debug.Put_Line (")),");
481 else
482 Debug.Put_Line (")));");
483 end if;
484 end loop;
485 end Dump_Configs;
486
487end HW.GFX.GMA;