blob: d15649411982590fb9dd61f52514ea16ad130eee [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;
Nico Huber74ec9622016-11-19 03:00:43 +0100240 if Port_Cfg.Display = HDMI then
241 declare
242 pragma Assert (Config.HDMI_Max_Clock_24bpp * 8
243 / Port_Cfg.Mode.BPC >= Frequency_Type'First);
244 Max_Dotclock : constant Frequency_Type :=
245 Config.HDMI_Max_Clock_24bpp * 8 / Port_Cfg.Mode.BPC;
246 begin
247 if Port_Cfg.Mode.Dotclock > Max_Dotclock then
248 pragma Debug (Debug.Put ("Dotclock "));
249 pragma Debug (Debug.Put_Int64 (Port_Cfg.Mode.Dotclock));
250 pragma Debug (Debug.Put (" too high, limiting to "));
251 pragma Debug (Debug.Put_Int64 (Max_Dotclock));
252 pragma Debug (Debug.Put_Line ("."));
253 Port_Cfg.Mode.Dotclock := Max_Dotclock;
254 end if;
255 end;
256 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200257 end;
258 else
259 Port_Cfg := Port_Config'
260 (Port => GPU_Port'First,
261 PCH_Port => PCH_Port'First,
262 Display => Display_Type'First,
263 Mode => Invalid_Mode,
264 Is_FDI => False,
265 FDI => Default_DP,
266 DP => Default_DP);
267 end if;
268 end Fill_Port_Config;
269
270 ----------------------------------------------------------------------------
271
272 function To_Controller
273 (Dsp_Config : Config_Index) return Display_Controller.Controller_Type
274 is
275 Result : Display_Controller.Controller_Type;
276 begin
277 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
278
279 case Dsp_Config is
280 when Primary =>
281 Result := Display_Controller.Controllers (Display_Controller.A);
282 when Secondary =>
283 Result := Display_Controller.Controllers (Display_Controller.B);
284 when Tertiary =>
285 Result := Display_Controller.Controllers (Display_Controller.C);
286 end case;
287 return Result;
288 end To_Controller;
289
290 ----------------------------------------------------------------------------
291
292 function To_Head
293 (N_Config : Config_Index;
294 Port : Active_Port_Type)
295 return Display_Controller.Head_Type
296 is
297 Result : Display_Controller.Head_Type;
298 begin
299 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
300
301 if Config.Has_EDP_Pipe and then Port = Internal then
302 Result := Display_Controller.Heads (Display_Controller.Head_EDP);
303 else
304 case N_Config is
305 when Primary =>
306 Result := Display_Controller.Heads (Display_Controller.Head_A);
307 when Secondary =>
308 Result := Display_Controller.Heads (Display_Controller.Head_B);
309 when Tertiary =>
310 Result := Display_Controller.Heads (Display_Controller.Head_C);
311 end case;
312 end if;
313 return Result;
314 end To_Head;
315
316 ----------------------------------------------------------------------------
317
318 procedure Legacy_VGA_Off
319 is
320 Reg8 : Word8;
321 begin
322 -- disable legacy VGA plane, taking over control now
323 Port_IO.OutB (VGA_SR_INDEX, VGA_SR01);
324 Port_IO.InB (Reg8, VGA_SR_DATA);
325 Port_IO.OutB (VGA_SR_DATA, Reg8 or 1 * 2 ** 5);
326 Time.U_Delay (100); -- PRM says 100us, Linux does 300
327 Registers.Set_Mask (Registers.VGACNTRL, 1 * 2 ** 31);
328 end Legacy_VGA_Off;
329
330 ----------------------------------------------------------------------------
331
332 function Port_Configured
333 (Configs : Configs_Type;
334 Port : Port_Type)
335 return Boolean
336 with
337 Global => null
338 is
339 begin
340 return Configs (Primary).Port = Port or
341 Configs (Secondary).Port = Port or
342 Configs (Tertiary).Port = Port;
343 end Port_Configured;
344
345 procedure Scan_Ports
346 (Configs : out Configs_Type;
347 Ports : in Port_List)
348 is
349 Raw_EDID : EDID.Raw_EDID_Data := (others => 16#00#);
350 Port_Idx : Port_List_Range := Port_List_Range'First;
351 Port_Cfg : Port_Config;
352 Success : Boolean := False;
353 begin
354 Configs := (Config_Index =>
355 (Port => Disabled,
356 Mode => Invalid_Mode,
357 Framebuffer => Default_FB));
358
359 for Config_Idx in Config_Index loop
360 while Ports (Port_Idx) /= Disabled loop
361 if not Port_Configured (Configs, Ports (Port_Idx)) then
362 Configs (Config_Idx).Port := Ports (Port_Idx);
363 Fill_Port_Config (Port_Cfg, Configs, Config_Idx, Success);
364
365 if Success then
366 -- May need power to probe port
367 if Port_Cfg.Display = DP then
368 Power_And_Clocks.Power_Up (Cur_Configs, Configs);
369 end if;
370 if Ports (Port_Idx) = Internal then
371 Panel.On;
372 end if;
373
374 Connector_Info.Read_EDID (Raw_EDID, Port_Cfg, Success);
375 end if;
376
Nico Huber393aa8a2016-10-21 14:18:53 +0200377 if Success and then
378 (EDID.Compatible_Display (Raw_EDID, Port_Cfg.Display) and
379 EDID.Has_Preferred_Mode (Raw_EDID))
380 then
Nico Huber83693c82016-10-08 22:17:55 +0200381 Configs (Config_Idx).Mode := EDID.Preferred_Mode (Raw_EDID);
382 else
383 Configs (Config_Idx).Port := Disabled;
Nico Huber6a356672016-10-21 15:13:55 +0200384 Success := False;
Nico Huber83693c82016-10-08 22:17:55 +0200385
386 if Ports (Port_Idx) = Internal and
387 not Port_Configured (Cur_Configs, Internal)
388 then
389 Panel.Off;
390 end if;
391 end if;
392 end if;
393
394 exit when Port_Idx = Port_List_Range'Last;
395 Port_Idx := Port_List_Range'Succ (Port_Idx);
396
397 exit when Success;
398 end loop;
399 end loop;
400
401 Power_And_Clocks.Power_Set_To (Cur_Configs);
402 end Scan_Ports;
403
404 procedure Auto_Configure
405 (Configs : in out Configs_Type;
406 Keep_Power : in Boolean := False)
407 is
408 Raw_EDID : EDID.Raw_EDID_Data := (others => 16#00#);
409 Success : Boolean;
410
411 Config_Idx : Config_Index;
412 Port_Cfg : Port_Config;
413
414 function Free_Config return Boolean
415 with
416 Pre => True
417 is
418 begin
419 return Port_Configured (Configs, Disabled);
420 end Free_Config;
421
422 function First_Free_Config return Config_Index
423 with
424 Pre => Free_Config
425 is
426 begin
427 return (if Configs (Primary).Port = Disabled then Primary else
428 (if Configs (Secondary).Port = Disabled then Secondary
429 else Tertiary));
430 end First_Free_Config;
431 begin
432 -- TODO: Only check ports with hot-plug event?
433
434 if Config.Has_Internal_Display and then
435 not Keep_Power and then
436 not Port_Configured (Cur_Configs, Internal)
437 then
438 Panel.On (Wait => False);
439 end if;
440
441 -- Check if displays are still connected
442 for I in Config_Index loop
443 if Configs (I).Port /= Disabled then
444 Fill_Port_Config (Port_Cfg, Configs, I, Success);
445 if Success then
446 Connector_Info.Read_EDID
447 (Raw_EDID => Raw_EDID,
448 Port_Cfg => Port_Cfg,
449 Success => Success);
450 end if;
451 if not Success or else
452 not EDID.Has_Preferred_Mode (Raw_EDID) or else
453 Configs (I).Mode /= EDID.Preferred_Mode (Raw_EDID)
454 then
455 Configs (I).Port := Disabled;
456 end if;
457 end if;
458 end loop;
459
460 -- Add new displays as long as there is a free pipe config
461 for Port in Active_Port_Type loop
462 if Free_Config and then not Port_Configured (Configs, Port) then
463 Config_Idx := First_Free_Config;
464 Configs (Config_Idx).Port := Port;
465 Fill_Port_Config (Port_Cfg, Configs, Config_Idx, Success);
466
467 if Success then
468 -- Need power to probe port
469 if not Keep_Power and then To_Display_Type (Port) = DP then
470 Power_And_Clocks.Power_Up (Cur_Configs, Configs);
471 end if;
472 if not Keep_Power and then Port = Internal then
473 Panel.Wait_On;
474 end if;
475
476 Connector_Info.Read_EDID
477 (Raw_EDID => Raw_EDID,
478 Port_Cfg => Port_Cfg,
479 Success => Success);
480 end if;
481
Nico Huber393aa8a2016-10-21 14:18:53 +0200482 if Success and then
483 (EDID.Compatible_Display (Raw_EDID, Port_Cfg.Display) and
484 EDID.Has_Preferred_Mode (Raw_EDID))
485 then
Nico Huber83693c82016-10-08 22:17:55 +0200486 Configs (Config_Idx) := Config_Type'
487 (Port => Port,
488 Framebuffer => Configs (Config_Idx).Framebuffer,
489 Mode => EDID.Preferred_Mode (Raw_EDID));
490 else
491 Configs (Config_Idx).Port := Disabled;
492 end if;
493 end if;
494 end loop;
495
496 if not Keep_Power then
497 Power_And_Clocks.Power_Set_To (Cur_Configs);
498
499 if Config.Has_Internal_Display and then
500 not Port_Configured (Cur_Configs, Internal)
501 then
502 Panel.Off;
503 end if;
504 end if;
505 end Auto_Configure;
506
507 ----------------------------------------------------------------------------
508
509 procedure Update_Outputs (Configs : Configs_Type)
510 is
511 Did_Power_Up : Boolean := False;
512
513 HPD, HPD_Delay_Over, Success : Boolean;
514 Old_Config, New_Config : Config_Type;
515 Old_Configs : Configs_Type;
516 Port_Cfg : Port_Config;
517
518 procedure Check_HPD
519 (Port_Cfg : in Port_Config;
520 Port : in Port_Type;
521 Detected : out Boolean)
522 is
523 begin
524 HPD_Delay_Over := Time.Timed_Out (HPD_Delay (Port));
525 if HPD_Delay_Over then
526 Port_Detect.Hotplug_Detect (Port_Cfg, Detected);
527 HPD_Delay (Port) := Time.MS_From_Now (333);
528 else
529 Detected := False;
530 end if;
531 end Check_HPD;
532 begin
533 Old_Configs := Cur_Configs;
534
535 for I in Config_Index loop
536 HPD := False;
537
538 Old_Config := Cur_Configs (I);
539 New_Config := Configs (I);
540
541 Fill_Port_Config (Port_Cfg, Old_Configs, I, Success);
542 if Success then
543 Check_HPD (Port_Cfg, Old_Config.Port, HPD);
544 end if;
545
546 -- Connector changed?
547 if (Success and then HPD) or
548 Old_Config.Port /= New_Config.Port or
549 Old_Config.Mode /= New_Config.Mode
550 then
551 if Old_Config.Port /= Disabled then
552 if Success then
553 pragma Debug (Debug.New_Line);
554 pragma Debug (Debug.Put_Line
555 ("Disabling port " & Port_Names (Old_Config.Port)));
556
557 Connectors.Pre_Off (Port_Cfg);
558
559 Display_Controller.Off
560 (To_Controller (I), To_Head (I, Old_Config.Port));
561
562 Connectors.Post_Off (Port_Cfg);
563 end if;
564
565 -- Free PLL
566 PLLs.Free (Allocated_PLLs (I));
567
568 Cur_Configs (I).Port := Disabled;
569 end if;
570
571 if New_Config.Port /= Disabled then
572 Fill_Port_Config (Port_Cfg, Configs, I, Success);
573
Nico Huberc7a4fee2016-11-03 18:18:03 +0100574 Success := Success and then
Nico Huberdcd274b2016-11-03 20:15:39 +0100575 Validate_Config (New_Config.Framebuffer, Port_Cfg, I);
Nico Huberc7a4fee2016-11-03 18:18:03 +0100576
Nico Huber83693c82016-10-08 22:17:55 +0200577 if Success and then Wait_For_HPD (New_Config.Port) then
578 Check_HPD (Port_Cfg, New_Config.Port, Success);
579 Wait_For_HPD (New_Config.Port) := not Success;
580 end if;
581
582 if Success then
583 pragma Debug (Debug.New_Line);
584 pragma Debug (Debug.Put_Line
585 ("Trying to enable port " & Port_Names (New_Config.Port)));
586
587 if not Did_Power_Up then
588 Power_And_Clocks.Power_Up (Old_Configs, Configs);
589 Did_Power_Up := True;
590 end if;
591
592 if Port_Cfg.Is_FDI then
593 Configure_FDI_Link (Port_Cfg, Success);
594 end if;
595 end if;
596
597 if Success then
598 Connector_Info.Preferred_Link_Setting
599 (Port_Cfg => Port_Cfg,
600 Success => Success);
601 end if;
602
603 while Success loop
Nico Huber47ff0692016-11-04 14:29:39 +0100604 pragma Loop_Invariant
605 (New_Config.Port in Active_Port_Type and
606 Port_Cfg.Mode = Port_Cfg.Mode'Loop_Entry);
Nico Huber83693c82016-10-08 22:17:55 +0200607
608 PLLs.Alloc
609 (Port_Cfg => Port_Cfg,
610 PLL => Allocated_PLLs (I),
611 Success => Success);
612
613 if Success then
614 for Try in 1 .. 2 loop
615 pragma Loop_Invariant
616 (New_Config.Port in Active_Port_Type);
617
618 Connectors.Pre_On
619 (Port_Cfg => Port_Cfg,
620 PLL_Hint => PLLs.Register_Value
621 (Allocated_PLLs (I)),
622 Pipe_Hint => Display_Controller.Get_Pipe_Hint
623 (To_Head (I, New_Config.Port)),
624 Success => Success);
625
626 if Success then
627 Display_Controller.On
628 (Controller => To_Controller (I),
629 Head => To_Head (I, New_Config.Port),
630 Port_Cfg => Port_Cfg,
631 Framebuffer => New_Config.Framebuffer);
632
633 Connectors.Post_On
634 (Port_Cfg => Port_Cfg,
635 PLL_Hint => PLLs.Register_Value
636 (Allocated_PLLs (I)),
637 Success => Success);
638
639 if not Success then
640 Display_Controller.Off
641 (To_Controller (I),
642 To_Head (I, New_Config.Port));
643 Connectors.Post_Off (Port_Cfg);
644 end if;
645 end if;
646
647 exit when Success;
648 end loop;
649 exit when Success; -- connection established => stop loop
650
651 -- connection failed
652 PLLs.Free (Allocated_PLLs (I));
653 end if;
654
655 Connector_Info.Next_Link_Setting
656 (Port_Cfg => Port_Cfg,
657 Success => Success);
658 end loop;
659
660 if Success then
661 pragma Debug (Debug.Put_Line
662 ("Enabled port " & Port_Names (New_Config.Port)));
663 Cur_Configs (I) := New_Config;
664 DP_Links (I) := Port_Cfg.DP;
665 else
666 Wait_For_HPD (New_Config.Port) := True;
667 if New_Config.Port = Internal then
668 Panel.Off;
669 end if;
670 end if;
671 else
672 Cur_Configs (I) := New_Config;
673 end if;
674 elsif Old_Config.Framebuffer /= New_Config.Framebuffer and
675 Old_Config.Port /= Disabled
676 then
677 Display_Controller.Update_Offset
678 (Controller => To_Controller (I),
679 Framebuffer => New_Config.Framebuffer);
680 Cur_Configs (I) := New_Config;
681 end if;
682 end loop;
683
684 if Did_Power_Up then
685 Power_And_Clocks.Power_Down (Old_Configs, Configs, Cur_Configs);
686 end if;
687
688 end Update_Outputs;
689
690 ----------------------------------------------------------------------------
691
692 procedure Initialize
693 (MMIO_Base : in Word64 := 0;
694 Write_Delay : in Word64 := 0;
695 Success : out Boolean)
696 with
697 Refined_Global =>
698 (In_Out =>
699 (Config.Valid_Port_GPU,
700 Registers.Register_State, Port_IO.State),
701 Input =>
702 (Time.State),
703 Output =>
704 (Registers.Address_State,
705 PLLs.State, Panel.Panel_State,
706 Cur_Configs, Allocated_PLLs, DP_Links,
707 HPD_Delay, Wait_For_HPD, Initialized))
708 is
709 use type HW.Word64;
710
711 Now : constant Time.T := Time.Now;
712
713 procedure Check_Platform (Success : out Boolean)
714 is
715 Audio_VID_DID : Word32;
716 begin
717 case Config.CPU is
718 when Haswell .. Skylake =>
719 Registers.Read (Registers.AUD_VID_DID, Audio_VID_DID);
720 when Ironlake .. Ivybridge =>
721 Registers.Read (Registers.PCH_AUD_VID_DID, Audio_VID_DID);
722 end case;
723 Success :=
724 (case Config.CPU is
725 when Skylake => Audio_VID_DID = 16#8086_2809#,
726 when Broadwell => Audio_VID_DID = 16#8086_2808#,
727 when Haswell => Audio_VID_DID = 16#8086_2807#,
728 when Ivybridge |
729 Sandybridge => Audio_VID_DID = 16#8086_2806# or
730 Audio_VID_DID = 16#8086_2805#,
Nico Hubereeb5a392016-10-09 19:28:30 +0200731 when Ironlake => Audio_VID_DID = 16#0000_0000#);
Nico Huber83693c82016-10-08 22:17:55 +0200732 end Check_Platform;
733 begin
734 pragma Warnings (GNATprove, Off, "unused variable ""Write_Delay""",
735 Reason => "Write_Delay is used for debugging only");
736
737 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
738
739 pragma Debug (Debug.Set_Register_Write_Delay (Write_Delay));
740
741 Wait_For_HPD := HPD_Type'(others => False);
742 HPD_Delay := HPD_Delay_Type'(others => Now);
743 DP_Links := Links_Type'(others => HW.GFX.Default_DP);
744 Allocated_PLLs := (others => PLLs.Invalid);
745 Cur_Configs := Configs_Type'
746 (others => Config_Type'
747 (Port => Disabled,
748 Framebuffer => HW.GFX.Default_FB,
749 Mode => HW.GFX.Invalid_Mode));
750 Registers.Set_Register_Base
751 (if MMIO_Base /= 0 then
752 MMIO_Base
753 else
754 Config.Default_MMIO_Base);
755 PLLs.Initialize;
756
757 Check_Platform (Success);
758 if not Success then
759 pragma Debug (Debug.Put_Line ("ERROR: Incompatible CPU or PCH."));
760
761 Panel.Static_Init; -- for flow analysis
762
763 Initialized := False;
764 return;
765 end if;
766
767 Panel.Setup_PP_Sequencer;
768 Port_Detect.Initialize;
769
770 Power_And_Clocks.Pre_All_Off;
771
772 Legacy_VGA_Off;
773
774 Connectors.Pre_All_Off;
775 Display_Controller.All_Off;
776 Connectors.Post_All_Off;
777 PLLs.All_Off;
778
779 Power_And_Clocks.Post_All_Off;
780
781 -------------------- Now restart from a clean state ---------------------
782 Power_And_Clocks.Initialize;
783
Nico Huberf54d0962016-10-20 14:17:18 +0200784 Registers.Unset_And_Set_Mask
785 (Register => Registers.PCH_RAWCLK_FREQ,
786 Mask_Unset => PCH_RAWCLK_FREQ_MASK,
787 Mask_Set => PCH_RAWCLK_FREQ (Config.Default_RawClk_Freq));
788
Nico Huber83693c82016-10-08 22:17:55 +0200789 Initialized := True;
790
791 end Initialize;
792
793 function Is_Initialized return Boolean
794 with
795 Refined_Post => Is_Initialized'Result = Initialized
796 is
797 begin
798 return Initialized;
799 end Is_Initialized;
800
801 ----------------------------------------------------------------------------
802
803 procedure Write_GTT
804 (GTT_Page : GTT_Range;
805 Device_Address : GTT_Address_Type;
806 Valid : Boolean) is
807 begin
808 Registers.Write_GTT (GTT_Page, Device_Address, Valid);
809 end Write_GTT;
810
811 procedure Setup_Default_GTT (FB : Framebuffer_Type; Phys_FB : Word32)
812 is
813 FB_Size : constant Pos32 :=
814 FB.Stride * FB.Height * Pos32 (((FB.BPC * 4) / 8));
815 Phys_Addr : GTT_Address_Type := GTT_Address_Type (Phys_FB);
816 begin
817 for Idx in GTT_Range range 0 .. GTT_Range (((FB_Size + 4095) / 4096) - 1)
818 loop
819 Registers.Write_GTT
820 (GTT_Page => Idx,
821 Device_Address => Phys_Addr,
822 Valid => True);
823 Phys_Addr := Phys_Addr + 4096;
824 end loop;
825 end Setup_Default_GTT;
826
827 ----------------------------------------------------------------------------
828
829 procedure Dump_Configs (Configs : Configs_Type)
830 is
831 subtype Pipe_Name is String (1 .. 9);
832 type Pipe_Name_Array is array (Config_Index) of Pipe_Name;
833 Pipe_Names : constant Pipe_Name_Array :=
834 (Primary => "Primary ",
835 Secondary => "Secondary",
836 Tertiary => "Tertiary ");
837 begin
838 Debug.New_Line;
839 Debug.Put_Line ("CONFIG => ");
840 for Pipe in Config_Index loop
841 if Pipe = Config_Index'First then
842 Debug.Put (" (");
843 else
844 Debug.Put (" ");
845 end if;
846 Debug.Put_Line (Pipe_Names (Pipe) & " =>");
847 Debug.Put_Line
848 (" (Port => " & Port_Names (Configs (Pipe).Port) & ",");
849 Debug.Put_Line (" Framebuffer =>");
850 Debug.Put (" (Width => ");
851 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Width);
852 Debug.Put_Line (",");
853 Debug.Put (" Height => ");
854 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Height);
855 Debug.Put_Line (",");
856 Debug.Put (" Stride => ");
857 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Stride);
858 Debug.Put_Line (",");
859 Debug.Put (" Offset => ");
860 Debug.Put_Word32 (Configs (Pipe).Framebuffer.Offset);
861 Debug.Put_Line (",");
862 Debug.Put (" BPC => ");
863 Debug.Put_Int64 (Configs (Pipe).Framebuffer.BPC);
864 Debug.Put_Line ("),");
865 Debug.Put_Line (" Mode =>");
866 Debug.Put (" (Dotclock => ");
867 Debug.Put_Int64 (Configs (Pipe).Mode.Dotclock);
868 Debug.Put_Line (",");
869 Debug.Put (" H_Visible => ");
870 Debug.Put_Int16 (Configs (Pipe).Mode.H_Visible);
871 Debug.Put_Line (",");
872 Debug.Put (" H_Sync_Begin => ");
873 Debug.Put_Int16 (Configs (Pipe).Mode.H_Sync_Begin);
874 Debug.Put_Line (",");
875 Debug.Put (" H_Sync_End => ");
876 Debug.Put_Int16 (Configs (Pipe).Mode.H_Sync_End);
877 Debug.Put_Line (",");
878 Debug.Put (" H_Total => ");
879 Debug.Put_Int16 (Configs (Pipe).Mode.H_Total);
880 Debug.Put_Line (",");
881 Debug.Put (" V_Visible => ");
882 Debug.Put_Int16 (Configs (Pipe).Mode.V_Visible);
883 Debug.Put_Line (",");
884 Debug.Put (" V_Sync_Begin => ");
885 Debug.Put_Int16 (Configs (Pipe).Mode.V_Sync_Begin);
886 Debug.Put_Line (",");
887 Debug.Put (" V_Sync_End => ");
888 Debug.Put_Int16 (Configs (Pipe).Mode.V_Sync_End);
889 Debug.Put_Line (",");
890 Debug.Put (" V_Total => ");
891 Debug.Put_Int16 (Configs (Pipe).Mode.V_Total);
892 Debug.Put_Line (",");
893 Debug.Put_Line (" H_Sync_Active_High => " &
894 (if Configs (Pipe).Mode.H_Sync_Active_High
895 then "True,"
896 else "False,"));
897 Debug.Put_Line (" V_Sync_Active_High => " &
898 (if Configs (Pipe).Mode.V_Sync_Active_High
899 then "True,"
900 else "False,"));
901 Debug.Put (" BPC => ");
902 Debug.Put_Int64 (Configs (Pipe).Mode.BPC);
903 if Pipe /= Config_Index'Last then
904 Debug.Put_Line (")),");
905 else
906 Debug.Put_Line (")));");
907 end if;
908 end loop;
909 end Dump_Configs;
910
911end HW.GFX.GMA;