blob: c50d9433091365f2002691938ac2b1177829e34b [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
15with HW.GFX.EDID;
16with HW.GFX.GMA.Config;
17with HW.GFX.GMA.DP_Info;
18with HW.GFX.GMA.Registers;
19with HW.GFX.GMA.Power_And_Clocks;
20with HW.GFX.GMA.Panel;
21with HW.GFX.GMA.PLLs;
22with HW.GFX.GMA.Port_Detect;
23with HW.GFX.GMA.Connectors;
24with HW.GFX.GMA.Connector_Info;
25with HW.GFX.GMA.Pipe_Setup;
26
27with System;
28
29with HW.Debug;
30with GNAT.Source_Info;
31
32use type HW.Word8;
33use type HW.Int32;
34
35package body HW.GFX.GMA
36 with Refined_State =>
37 (State =>
38 (Registers.Address_State,
39 PLLs.State, Panel.Panel_State,
40 Cur_Configs, Allocated_PLLs, DP_Links,
41 HPD_Delay, Wait_For_HPD),
42 Init_State => Initialized,
43 Config_State => Config.Valid_Port_GPU,
44 Device_State =>
45 (Registers.Register_State, Registers.GTT_State))
46is
47
48 subtype Port_Name is String (1 .. 8);
49 type Port_Name_Array is array (Port_Type) of Port_Name;
50 Port_Names : constant Port_Name_Array :=
51 (Disabled => "Disabled",
52 Internal => "Internal",
53 DP1 => "DP1 ",
54 DP2 => "DP2 ",
55 DP3 => "DP3 ",
56 Digital1 => "Digital1",
57 Digital2 => "Digital2",
58 Digital3 => "Digital3",
59 Analog => "Analog ");
60
61 package Display_Controller renames Pipe_Setup;
62
63 type PLLs_Type is array (Config_Index) of PLLs.T;
64
65 type Links_Type is array (Config_Index) of DP_Link;
66
67 type HPD_Type is array (Port_Type) of Boolean;
68 type HPD_Delay_Type is array (Port_Type) of Time.T;
69
70 Cur_Configs : Configs_Type;
71 Allocated_PLLs : PLLs_Type;
72 DP_Links : Links_Type;
73 HPD_Delay : HPD_Delay_Type;
74 Wait_For_HPD : HPD_Type;
75 Initialized : Boolean := False;
76
77 subtype Active_Port_Type is Port_Type range Port_Type'Succ (Disabled) .. Port_Type'Last;
78
79 ----------------------------------------------------------------------------
80
Nico Huberf54d0962016-10-20 14:17:18 +020081 PCH_RAWCLK_FREQ_MASK : constant := 16#3ff# * 2 ** 0;
82
83 function PCH_RAWCLK_FREQ (Freq : Frequency_Type) return Word32
84 is
85 begin
86 return Word32 (Freq / 1_000_000);
87 end PCH_RAWCLK_FREQ;
88
89 ----------------------------------------------------------------------------
90
Nico Huber83693c82016-10-08 22:17:55 +020091 function To_GPU_Port
92 (Configs : Configs_Type;
93 Idx : Config_Index)
94 return GPU_Port
95 is
96 begin
97 return
98 (case Config.CPU is
99 when Ironlake .. Ivybridge => -- everything but eDP through FDI/PCH
100 (if Config.Internal_Is_EDP and then Configs (Idx).Port = Internal
101 then
102 DIGI_A
103 else
104 (case Idx is
105 -- FDIs are fixed to the CPU pipe
106 when Primary => DIGI_B,
107 when Secondary => DIGI_C,
108 when Tertiary => DIGI_D)),
109 when Haswell .. Skylake => -- everything but VGA directly on CPU
110 (case Configs (Idx).Port is
111 when Disabled => GPU_Port'First,
112 when Internal => DIGI_A, -- LVDS not available
113 when Digital1 | DP1 => DIGI_B,
114 when Digital2 | DP2 => DIGI_C,
115 when Digital3 | DP3 => DIGI_D,
116 when Analog => DIGI_E));
117 end To_GPU_Port;
118
119 function To_PCH_Port (Port : Active_Port_Type) return PCH_Port
120 is
121 begin
122 return
123 (case Port is
124 when Internal => PCH_LVDS, -- will be ignored if Internal is DP
125 when Analog => PCH_DAC,
126 when Digital1 => PCH_HDMI_B,
127 when Digital2 => PCH_HDMI_C,
128 when Digital3 => PCH_HDMI_D,
129 when DP1 => PCH_DP_B,
130 when DP2 => PCH_DP_C,
131 when DP3 => PCH_DP_D);
132 end To_PCH_Port;
133
134 function To_Display_Type (Port : Active_Port_Type) return Display_Type
135 with Pre => True
136 is
137 begin
138 return
139 (case Port is
140 when Internal => Config.Internal_Display,
141 when Analog => VGA,
142 when Digital1 |
143 Digital2 |
144 Digital3 => HDMI,
145 when DP1 |
146 DP2 |
147 DP3 => DP);
148 end To_Display_Type;
149
150 procedure Configure_FDI_Link
151 (Port_Cfg : in out Port_Config;
152 Success : out Boolean)
Nico Huber47ff0692016-11-04 14:29:39 +0100153 with
154 Post => Port_Cfg.Mode = Port_Cfg.Mode'Old
Nico Huber83693c82016-10-08 22:17:55 +0200155 is
156 procedure Limit_Lane_Count
157 is
158 FDI_TX_CTL_FDI_TX_ENABLE : constant := 1 * 2 ** 31;
159 Enabled : Boolean;
160 begin
161 -- if DIGI_D enabled: (FDI names are off by one)
162 Registers.Is_Set_Mask
163 (Register => Registers.FDI_TX_CTL_C,
164 Mask => FDI_TX_CTL_FDI_TX_ENABLE,
165 Result => Enabled);
166 if Enabled then
167 Port_Cfg.FDI.Receiver_Caps.Max_Lane_Count := DP_Lane_Count_2;
168 end if;
169 end Limit_Lane_Count;
170 begin
171 Port_Cfg.FDI.Receiver_Caps.Max_Link_Rate := DP_Bandwidth_2_7;
172 Port_Cfg.FDI.Receiver_Caps.Max_Lane_Count :=
173 Config.FDI_Lane_Count (Port_Cfg.Port);
174 Port_Cfg.FDI.Receiver_Caps.Enhanced_Framing := True;
175 if Config.Has_FDI_C and then Port_Cfg.Port = DIGI_C then
176 Limit_Lane_Count;
177 end if;
178 DP_Info.Preferred_Link_Setting (Port_Cfg.FDI, Port_Cfg.Mode, Success);
179 end Configure_FDI_Link;
180
Nico Huberc7a4fee2016-11-03 18:18:03 +0100181 function Validate_Config
182 (Framebuffer : Framebuffer_Type;
Nico Huberdcd274b2016-11-03 20:15:39 +0100183 Port_Cfg : Port_Config;
184 I : Config_Index)
Nico Huberc7a4fee2016-11-03 18:18:03 +0100185 return Boolean
Nico Huber47ff0692016-11-04 14:29:39 +0100186 with
187 Post =>
188 (if Validate_Config'Result then
189 Framebuffer.Width <= Pos32 (Port_Cfg.Mode.H_Visible) and
190 Framebuffer.Height <= Pos32 (Port_Cfg.Mode.V_Visible))
Nico Huberc7a4fee2016-11-03 18:18:03 +0100191 is
192 begin
193 -- No downscaling
Nico Huberdcd274b2016-11-03 20:15:39 +0100194 -- Respect maximum scalable width
Nico Huber3675db52016-11-04 16:27:29 +0100195 -- VGA plane is only allowed on the primary pipe
196 -- Only 32bpp RGB (ignored for VGA plane)
197 -- Stride must be a multiple of 64 (ignored for VGA plane)
Nico Huberc7a4fee2016-11-03 18:18:03 +0100198 return
Nico Huberdcd274b2016-11-03 20:15:39 +0100199 ((Framebuffer.Width = Pos32 (Port_Cfg.Mode.H_Visible) and
200 Framebuffer.Height = Pos32 (Port_Cfg.Mode.V_Visible)) or
201 (Framebuffer.Width <= Config.Maximum_Scalable_Width (I) and
202 Framebuffer.Width <= Pos32 (Port_Cfg.Mode.H_Visible) and
203 Framebuffer.Height <= Pos32 (Port_Cfg.Mode.V_Visible))) and
Nico Huber3675db52016-11-04 16:27:29 +0100204 (Framebuffer.Offset /= VGA_PLANE_FRAMEBUFFER_OFFSET or I = Primary) and
205 (Framebuffer.Offset = VGA_PLANE_FRAMEBUFFER_OFFSET or
206 (Framebuffer.BPC = 8 and
207 Framebuffer.Stride mod 64 = 0));
Nico Huberc7a4fee2016-11-03 18:18:03 +0100208 end Validate_Config;
209
Nico Huber83693c82016-10-08 22:17:55 +0200210 procedure Fill_Port_Config
211 (Port_Cfg : out Port_Config;
212 Configs : in Configs_Type;
213 Idx : in Config_Index;
214 Success : out Boolean)
215 with Pre => True
216 is
217 begin
218 Success :=
219 Config.Supported_Pipe (Idx) and then
220 Config.Valid_Port (Configs (Idx).Port) and then
221 Configs (Idx).Port /= Disabled;
222
223 if Success then
224 declare
225 Port : constant Port_Type := Configs (Idx).Port;
226 Mode : constant Mode_Type := Configs (Idx).Mode;
227 Link : constant DP_Link := DP_Links (Idx);
228 begin
229 Port_Cfg := Port_Config'
230 (Port => To_GPU_Port (Configs, Idx),
231 PCH_Port => To_PCH_Port (Port),
232 Display => To_Display_Type (Port),
233 Mode => Mode,
234 Is_FDI => Config.FDI_Port (To_GPU_Port (Configs, Idx)),
235 FDI => Default_DP,
236 DP => Link);
237 if Port_Cfg.Mode.BPC = Auto_BPC then
238 Port_Cfg.Mode.BPC := Connector_Info.Default_BPC (Port_Cfg);
239 end if;
240 end;
241 else
242 Port_Cfg := Port_Config'
243 (Port => GPU_Port'First,
244 PCH_Port => PCH_Port'First,
245 Display => Display_Type'First,
246 Mode => Invalid_Mode,
247 Is_FDI => False,
248 FDI => Default_DP,
249 DP => Default_DP);
250 end if;
251 end Fill_Port_Config;
252
253 ----------------------------------------------------------------------------
254
255 function To_Controller
256 (Dsp_Config : Config_Index) return Display_Controller.Controller_Type
257 is
258 Result : Display_Controller.Controller_Type;
259 begin
260 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
261
262 case Dsp_Config is
263 when Primary =>
264 Result := Display_Controller.Controllers (Display_Controller.A);
265 when Secondary =>
266 Result := Display_Controller.Controllers (Display_Controller.B);
267 when Tertiary =>
268 Result := Display_Controller.Controllers (Display_Controller.C);
269 end case;
270 return Result;
271 end To_Controller;
272
273 ----------------------------------------------------------------------------
274
275 function To_Head
276 (N_Config : Config_Index;
277 Port : Active_Port_Type)
278 return Display_Controller.Head_Type
279 is
280 Result : Display_Controller.Head_Type;
281 begin
282 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
283
284 if Config.Has_EDP_Pipe and then Port = Internal then
285 Result := Display_Controller.Heads (Display_Controller.Head_EDP);
286 else
287 case N_Config is
288 when Primary =>
289 Result := Display_Controller.Heads (Display_Controller.Head_A);
290 when Secondary =>
291 Result := Display_Controller.Heads (Display_Controller.Head_B);
292 when Tertiary =>
293 Result := Display_Controller.Heads (Display_Controller.Head_C);
294 end case;
295 end if;
296 return Result;
297 end To_Head;
298
299 ----------------------------------------------------------------------------
300
301 procedure Legacy_VGA_Off
302 is
303 Reg8 : Word8;
304 begin
305 -- disable legacy VGA plane, taking over control now
306 Port_IO.OutB (VGA_SR_INDEX, VGA_SR01);
307 Port_IO.InB (Reg8, VGA_SR_DATA);
308 Port_IO.OutB (VGA_SR_DATA, Reg8 or 1 * 2 ** 5);
309 Time.U_Delay (100); -- PRM says 100us, Linux does 300
310 Registers.Set_Mask (Registers.VGACNTRL, 1 * 2 ** 31);
311 end Legacy_VGA_Off;
312
313 ----------------------------------------------------------------------------
314
315 function Port_Configured
316 (Configs : Configs_Type;
317 Port : Port_Type)
318 return Boolean
319 with
320 Global => null
321 is
322 begin
323 return Configs (Primary).Port = Port or
324 Configs (Secondary).Port = Port or
325 Configs (Tertiary).Port = Port;
326 end Port_Configured;
327
328 procedure Scan_Ports
329 (Configs : out Configs_Type;
330 Ports : in Port_List)
331 is
332 Raw_EDID : EDID.Raw_EDID_Data := (others => 16#00#);
333 Port_Idx : Port_List_Range := Port_List_Range'First;
334 Port_Cfg : Port_Config;
335 Success : Boolean := False;
336 begin
337 Configs := (Config_Index =>
338 (Port => Disabled,
339 Mode => Invalid_Mode,
340 Framebuffer => Default_FB));
341
342 for Config_Idx in Config_Index loop
343 while Ports (Port_Idx) /= Disabled loop
344 if not Port_Configured (Configs, Ports (Port_Idx)) then
345 Configs (Config_Idx).Port := Ports (Port_Idx);
346 Fill_Port_Config (Port_Cfg, Configs, Config_Idx, Success);
347
348 if Success then
349 -- May need power to probe port
350 if Port_Cfg.Display = DP then
351 Power_And_Clocks.Power_Up (Cur_Configs, Configs);
352 end if;
353 if Ports (Port_Idx) = Internal then
354 Panel.On;
355 end if;
356
357 Connector_Info.Read_EDID (Raw_EDID, Port_Cfg, Success);
358 end if;
359
Nico Huber393aa8a2016-10-21 14:18:53 +0200360 if Success and then
361 (EDID.Compatible_Display (Raw_EDID, Port_Cfg.Display) and
362 EDID.Has_Preferred_Mode (Raw_EDID))
363 then
Nico Huber83693c82016-10-08 22:17:55 +0200364 Configs (Config_Idx).Mode := EDID.Preferred_Mode (Raw_EDID);
365 else
366 Configs (Config_Idx).Port := Disabled;
Nico Huber6a356672016-10-21 15:13:55 +0200367 Success := False;
Nico Huber83693c82016-10-08 22:17:55 +0200368
369 if Ports (Port_Idx) = Internal and
370 not Port_Configured (Cur_Configs, Internal)
371 then
372 Panel.Off;
373 end if;
374 end if;
375 end if;
376
377 exit when Port_Idx = Port_List_Range'Last;
378 Port_Idx := Port_List_Range'Succ (Port_Idx);
379
380 exit when Success;
381 end loop;
382 end loop;
383
384 Power_And_Clocks.Power_Set_To (Cur_Configs);
385 end Scan_Ports;
386
387 procedure Auto_Configure
388 (Configs : in out Configs_Type;
389 Keep_Power : in Boolean := False)
390 is
391 Raw_EDID : EDID.Raw_EDID_Data := (others => 16#00#);
392 Success : Boolean;
393
394 Config_Idx : Config_Index;
395 Port_Cfg : Port_Config;
396
397 function Free_Config return Boolean
398 with
399 Pre => True
400 is
401 begin
402 return Port_Configured (Configs, Disabled);
403 end Free_Config;
404
405 function First_Free_Config return Config_Index
406 with
407 Pre => Free_Config
408 is
409 begin
410 return (if Configs (Primary).Port = Disabled then Primary else
411 (if Configs (Secondary).Port = Disabled then Secondary
412 else Tertiary));
413 end First_Free_Config;
414 begin
415 -- TODO: Only check ports with hot-plug event?
416
417 if Config.Has_Internal_Display and then
418 not Keep_Power and then
419 not Port_Configured (Cur_Configs, Internal)
420 then
421 Panel.On (Wait => False);
422 end if;
423
424 -- Check if displays are still connected
425 for I in Config_Index loop
426 if Configs (I).Port /= Disabled then
427 Fill_Port_Config (Port_Cfg, Configs, I, Success);
428 if Success then
429 Connector_Info.Read_EDID
430 (Raw_EDID => Raw_EDID,
431 Port_Cfg => Port_Cfg,
432 Success => Success);
433 end if;
434 if not Success or else
435 not EDID.Has_Preferred_Mode (Raw_EDID) or else
436 Configs (I).Mode /= EDID.Preferred_Mode (Raw_EDID)
437 then
438 Configs (I).Port := Disabled;
439 end if;
440 end if;
441 end loop;
442
443 -- Add new displays as long as there is a free pipe config
444 for Port in Active_Port_Type loop
445 if Free_Config and then not Port_Configured (Configs, Port) then
446 Config_Idx := First_Free_Config;
447 Configs (Config_Idx).Port := Port;
448 Fill_Port_Config (Port_Cfg, Configs, Config_Idx, Success);
449
450 if Success then
451 -- Need power to probe port
452 if not Keep_Power and then To_Display_Type (Port) = DP then
453 Power_And_Clocks.Power_Up (Cur_Configs, Configs);
454 end if;
455 if not Keep_Power and then Port = Internal then
456 Panel.Wait_On;
457 end if;
458
459 Connector_Info.Read_EDID
460 (Raw_EDID => Raw_EDID,
461 Port_Cfg => Port_Cfg,
462 Success => Success);
463 end if;
464
Nico Huber393aa8a2016-10-21 14:18:53 +0200465 if Success and then
466 (EDID.Compatible_Display (Raw_EDID, Port_Cfg.Display) and
467 EDID.Has_Preferred_Mode (Raw_EDID))
468 then
Nico Huber83693c82016-10-08 22:17:55 +0200469 Configs (Config_Idx) := Config_Type'
470 (Port => Port,
471 Framebuffer => Configs (Config_Idx).Framebuffer,
472 Mode => EDID.Preferred_Mode (Raw_EDID));
473 else
474 Configs (Config_Idx).Port := Disabled;
475 end if;
476 end if;
477 end loop;
478
479 if not Keep_Power then
480 Power_And_Clocks.Power_Set_To (Cur_Configs);
481
482 if Config.Has_Internal_Display and then
483 not Port_Configured (Cur_Configs, Internal)
484 then
485 Panel.Off;
486 end if;
487 end if;
488 end Auto_Configure;
489
490 ----------------------------------------------------------------------------
491
492 procedure Update_Outputs (Configs : Configs_Type)
493 is
494 Did_Power_Up : Boolean := False;
495
496 HPD, HPD_Delay_Over, Success : Boolean;
497 Old_Config, New_Config : Config_Type;
498 Old_Configs : Configs_Type;
499 Port_Cfg : Port_Config;
500
501 procedure Check_HPD
502 (Port_Cfg : in Port_Config;
503 Port : in Port_Type;
504 Detected : out Boolean)
505 is
506 begin
507 HPD_Delay_Over := Time.Timed_Out (HPD_Delay (Port));
508 if HPD_Delay_Over then
509 Port_Detect.Hotplug_Detect (Port_Cfg, Detected);
510 HPD_Delay (Port) := Time.MS_From_Now (333);
511 else
512 Detected := False;
513 end if;
514 end Check_HPD;
515 begin
516 Old_Configs := Cur_Configs;
517
518 for I in Config_Index loop
519 HPD := False;
520
521 Old_Config := Cur_Configs (I);
522 New_Config := Configs (I);
523
524 Fill_Port_Config (Port_Cfg, Old_Configs, I, Success);
525 if Success then
526 Check_HPD (Port_Cfg, Old_Config.Port, HPD);
527 end if;
528
529 -- Connector changed?
530 if (Success and then HPD) or
531 Old_Config.Port /= New_Config.Port or
532 Old_Config.Mode /= New_Config.Mode
533 then
534 if Old_Config.Port /= Disabled then
535 if Success then
536 pragma Debug (Debug.New_Line);
537 pragma Debug (Debug.Put_Line
538 ("Disabling port " & Port_Names (Old_Config.Port)));
539
540 Connectors.Pre_Off (Port_Cfg);
541
542 Display_Controller.Off
543 (To_Controller (I), To_Head (I, Old_Config.Port));
544
545 Connectors.Post_Off (Port_Cfg);
546 end if;
547
548 -- Free PLL
549 PLLs.Free (Allocated_PLLs (I));
550
551 Cur_Configs (I).Port := Disabled;
552 end if;
553
554 if New_Config.Port /= Disabled then
555 Fill_Port_Config (Port_Cfg, Configs, I, Success);
556
Nico Huberc7a4fee2016-11-03 18:18:03 +0100557 Success := Success and then
Nico Huberdcd274b2016-11-03 20:15:39 +0100558 Validate_Config (New_Config.Framebuffer, Port_Cfg, I);
Nico Huberc7a4fee2016-11-03 18:18:03 +0100559
Nico Huber83693c82016-10-08 22:17:55 +0200560 if Success and then Wait_For_HPD (New_Config.Port) then
561 Check_HPD (Port_Cfg, New_Config.Port, Success);
562 Wait_For_HPD (New_Config.Port) := not Success;
563 end if;
564
565 if Success then
566 pragma Debug (Debug.New_Line);
567 pragma Debug (Debug.Put_Line
568 ("Trying to enable port " & Port_Names (New_Config.Port)));
569
570 if not Did_Power_Up then
571 Power_And_Clocks.Power_Up (Old_Configs, Configs);
572 Did_Power_Up := True;
573 end if;
574
575 if Port_Cfg.Is_FDI then
576 Configure_FDI_Link (Port_Cfg, Success);
577 end if;
578 end if;
579
580 if Success then
581 Connector_Info.Preferred_Link_Setting
582 (Port_Cfg => Port_Cfg,
583 Success => Success);
584 end if;
585
586 while Success loop
Nico Huber47ff0692016-11-04 14:29:39 +0100587 pragma Loop_Invariant
588 (New_Config.Port in Active_Port_Type and
589 Port_Cfg.Mode = Port_Cfg.Mode'Loop_Entry);
Nico Huber83693c82016-10-08 22:17:55 +0200590
591 PLLs.Alloc
592 (Port_Cfg => Port_Cfg,
593 PLL => Allocated_PLLs (I),
594 Success => Success);
595
596 if Success then
597 for Try in 1 .. 2 loop
598 pragma Loop_Invariant
599 (New_Config.Port in Active_Port_Type);
600
601 Connectors.Pre_On
602 (Port_Cfg => Port_Cfg,
603 PLL_Hint => PLLs.Register_Value
604 (Allocated_PLLs (I)),
605 Pipe_Hint => Display_Controller.Get_Pipe_Hint
606 (To_Head (I, New_Config.Port)),
607 Success => Success);
608
609 if Success then
610 Display_Controller.On
611 (Controller => To_Controller (I),
612 Head => To_Head (I, New_Config.Port),
613 Port_Cfg => Port_Cfg,
614 Framebuffer => New_Config.Framebuffer);
615
616 Connectors.Post_On
617 (Port_Cfg => Port_Cfg,
618 PLL_Hint => PLLs.Register_Value
619 (Allocated_PLLs (I)),
620 Success => Success);
621
622 if not Success then
623 Display_Controller.Off
624 (To_Controller (I),
625 To_Head (I, New_Config.Port));
626 Connectors.Post_Off (Port_Cfg);
627 end if;
628 end if;
629
630 exit when Success;
631 end loop;
632 exit when Success; -- connection established => stop loop
633
634 -- connection failed
635 PLLs.Free (Allocated_PLLs (I));
636 end if;
637
638 Connector_Info.Next_Link_Setting
639 (Port_Cfg => Port_Cfg,
640 Success => Success);
641 end loop;
642
643 if Success then
644 pragma Debug (Debug.Put_Line
645 ("Enabled port " & Port_Names (New_Config.Port)));
646 Cur_Configs (I) := New_Config;
647 DP_Links (I) := Port_Cfg.DP;
648 else
649 Wait_For_HPD (New_Config.Port) := True;
650 if New_Config.Port = Internal then
651 Panel.Off;
652 end if;
653 end if;
654 else
655 Cur_Configs (I) := New_Config;
656 end if;
657 elsif Old_Config.Framebuffer /= New_Config.Framebuffer and
658 Old_Config.Port /= Disabled
659 then
660 Display_Controller.Update_Offset
661 (Controller => To_Controller (I),
662 Framebuffer => New_Config.Framebuffer);
663 Cur_Configs (I) := New_Config;
664 end if;
665 end loop;
666
667 if Did_Power_Up then
668 Power_And_Clocks.Power_Down (Old_Configs, Configs, Cur_Configs);
669 end if;
670
671 end Update_Outputs;
672
673 ----------------------------------------------------------------------------
674
675 procedure Initialize
676 (MMIO_Base : in Word64 := 0;
677 Write_Delay : in Word64 := 0;
678 Success : out Boolean)
679 with
680 Refined_Global =>
681 (In_Out =>
682 (Config.Valid_Port_GPU,
683 Registers.Register_State, Port_IO.State),
684 Input =>
685 (Time.State),
686 Output =>
687 (Registers.Address_State,
688 PLLs.State, Panel.Panel_State,
689 Cur_Configs, Allocated_PLLs, DP_Links,
690 HPD_Delay, Wait_For_HPD, Initialized))
691 is
692 use type HW.Word64;
693
694 Now : constant Time.T := Time.Now;
695
696 procedure Check_Platform (Success : out Boolean)
697 is
698 Audio_VID_DID : Word32;
699 begin
700 case Config.CPU is
701 when Haswell .. Skylake =>
702 Registers.Read (Registers.AUD_VID_DID, Audio_VID_DID);
703 when Ironlake .. Ivybridge =>
704 Registers.Read (Registers.PCH_AUD_VID_DID, Audio_VID_DID);
705 end case;
706 Success :=
707 (case Config.CPU is
708 when Skylake => Audio_VID_DID = 16#8086_2809#,
709 when Broadwell => Audio_VID_DID = 16#8086_2808#,
710 when Haswell => Audio_VID_DID = 16#8086_2807#,
711 when Ivybridge |
712 Sandybridge => Audio_VID_DID = 16#8086_2806# or
713 Audio_VID_DID = 16#8086_2805#,
Nico Hubereeb5a392016-10-09 19:28:30 +0200714 when Ironlake => Audio_VID_DID = 16#0000_0000#);
Nico Huber83693c82016-10-08 22:17:55 +0200715 end Check_Platform;
716 begin
717 pragma Warnings (GNATprove, Off, "unused variable ""Write_Delay""",
718 Reason => "Write_Delay is used for debugging only");
719
720 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
721
722 pragma Debug (Debug.Set_Register_Write_Delay (Write_Delay));
723
724 Wait_For_HPD := HPD_Type'(others => False);
725 HPD_Delay := HPD_Delay_Type'(others => Now);
726 DP_Links := Links_Type'(others => HW.GFX.Default_DP);
727 Allocated_PLLs := (others => PLLs.Invalid);
728 Cur_Configs := Configs_Type'
729 (others => Config_Type'
730 (Port => Disabled,
731 Framebuffer => HW.GFX.Default_FB,
732 Mode => HW.GFX.Invalid_Mode));
733 Registers.Set_Register_Base
734 (if MMIO_Base /= 0 then
735 MMIO_Base
736 else
737 Config.Default_MMIO_Base);
738 PLLs.Initialize;
739
740 Check_Platform (Success);
741 if not Success then
742 pragma Debug (Debug.Put_Line ("ERROR: Incompatible CPU or PCH."));
743
744 Panel.Static_Init; -- for flow analysis
745
746 Initialized := False;
747 return;
748 end if;
749
750 Panel.Setup_PP_Sequencer;
751 Port_Detect.Initialize;
752
753 Power_And_Clocks.Pre_All_Off;
754
755 Legacy_VGA_Off;
756
757 Connectors.Pre_All_Off;
758 Display_Controller.All_Off;
759 Connectors.Post_All_Off;
760 PLLs.All_Off;
761
762 Power_And_Clocks.Post_All_Off;
763
764 -------------------- Now restart from a clean state ---------------------
765 Power_And_Clocks.Initialize;
766
Nico Huberf54d0962016-10-20 14:17:18 +0200767 Registers.Unset_And_Set_Mask
768 (Register => Registers.PCH_RAWCLK_FREQ,
769 Mask_Unset => PCH_RAWCLK_FREQ_MASK,
770 Mask_Set => PCH_RAWCLK_FREQ (Config.Default_RawClk_Freq));
771
Nico Huber83693c82016-10-08 22:17:55 +0200772 Initialized := True;
773
774 end Initialize;
775
776 function Is_Initialized return Boolean
777 with
778 Refined_Post => Is_Initialized'Result = Initialized
779 is
780 begin
781 return Initialized;
782 end Is_Initialized;
783
784 ----------------------------------------------------------------------------
785
786 procedure Write_GTT
787 (GTT_Page : GTT_Range;
788 Device_Address : GTT_Address_Type;
789 Valid : Boolean) is
790 begin
791 Registers.Write_GTT (GTT_Page, Device_Address, Valid);
792 end Write_GTT;
793
794 procedure Setup_Default_GTT (FB : Framebuffer_Type; Phys_FB : Word32)
795 is
796 FB_Size : constant Pos32 :=
797 FB.Stride * FB.Height * Pos32 (((FB.BPC * 4) / 8));
798 Phys_Addr : GTT_Address_Type := GTT_Address_Type (Phys_FB);
799 begin
800 for Idx in GTT_Range range 0 .. GTT_Range (((FB_Size + 4095) / 4096) - 1)
801 loop
802 Registers.Write_GTT
803 (GTT_Page => Idx,
804 Device_Address => Phys_Addr,
805 Valid => True);
806 Phys_Addr := Phys_Addr + 4096;
807 end loop;
808 end Setup_Default_GTT;
809
810 ----------------------------------------------------------------------------
811
812 procedure Dump_Configs (Configs : Configs_Type)
813 is
814 subtype Pipe_Name is String (1 .. 9);
815 type Pipe_Name_Array is array (Config_Index) of Pipe_Name;
816 Pipe_Names : constant Pipe_Name_Array :=
817 (Primary => "Primary ",
818 Secondary => "Secondary",
819 Tertiary => "Tertiary ");
820 begin
821 Debug.New_Line;
822 Debug.Put_Line ("CONFIG => ");
823 for Pipe in Config_Index loop
824 if Pipe = Config_Index'First then
825 Debug.Put (" (");
826 else
827 Debug.Put (" ");
828 end if;
829 Debug.Put_Line (Pipe_Names (Pipe) & " =>");
830 Debug.Put_Line
831 (" (Port => " & Port_Names (Configs (Pipe).Port) & ",");
832 Debug.Put_Line (" Framebuffer =>");
833 Debug.Put (" (Width => ");
834 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Width);
835 Debug.Put_Line (",");
836 Debug.Put (" Height => ");
837 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Height);
838 Debug.Put_Line (",");
839 Debug.Put (" Stride => ");
840 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Stride);
841 Debug.Put_Line (",");
842 Debug.Put (" Offset => ");
843 Debug.Put_Word32 (Configs (Pipe).Framebuffer.Offset);
844 Debug.Put_Line (",");
845 Debug.Put (" BPC => ");
846 Debug.Put_Int64 (Configs (Pipe).Framebuffer.BPC);
847 Debug.Put_Line ("),");
848 Debug.Put_Line (" Mode =>");
849 Debug.Put (" (Dotclock => ");
850 Debug.Put_Int64 (Configs (Pipe).Mode.Dotclock);
851 Debug.Put_Line (",");
852 Debug.Put (" H_Visible => ");
853 Debug.Put_Int16 (Configs (Pipe).Mode.H_Visible);
854 Debug.Put_Line (",");
855 Debug.Put (" H_Sync_Begin => ");
856 Debug.Put_Int16 (Configs (Pipe).Mode.H_Sync_Begin);
857 Debug.Put_Line (",");
858 Debug.Put (" H_Sync_End => ");
859 Debug.Put_Int16 (Configs (Pipe).Mode.H_Sync_End);
860 Debug.Put_Line (",");
861 Debug.Put (" H_Total => ");
862 Debug.Put_Int16 (Configs (Pipe).Mode.H_Total);
863 Debug.Put_Line (",");
864 Debug.Put (" V_Visible => ");
865 Debug.Put_Int16 (Configs (Pipe).Mode.V_Visible);
866 Debug.Put_Line (",");
867 Debug.Put (" V_Sync_Begin => ");
868 Debug.Put_Int16 (Configs (Pipe).Mode.V_Sync_Begin);
869 Debug.Put_Line (",");
870 Debug.Put (" V_Sync_End => ");
871 Debug.Put_Int16 (Configs (Pipe).Mode.V_Sync_End);
872 Debug.Put_Line (",");
873 Debug.Put (" V_Total => ");
874 Debug.Put_Int16 (Configs (Pipe).Mode.V_Total);
875 Debug.Put_Line (",");
876 Debug.Put_Line (" H_Sync_Active_High => " &
877 (if Configs (Pipe).Mode.H_Sync_Active_High
878 then "True,"
879 else "False,"));
880 Debug.Put_Line (" V_Sync_Active_High => " &
881 (if Configs (Pipe).Mode.V_Sync_Active_High
882 then "True,"
883 else "False,"));
884 Debug.Put (" BPC => ");
885 Debug.Put_Int64 (Configs (Pipe).Mode.BPC);
886 if Pipe /= Config_Index'Last then
887 Debug.Put_Line (")),");
888 else
889 Debug.Put_Line (")));");
890 end if;
891 end loop;
892 end Dump_Configs;
893
894end HW.GFX.GMA;