blob: 7fa58a42befb3bb6fee262d1f1c9629f0d1a1d0f [file] [log] [blame]
Nico Huber83693c82016-10-08 22:17:55 +02001--
Nico Huber3d06de82018-05-29 01:35:04 +02002-- Copyright (C) 2014-2018 secunet Security Networks AG
Nico Huber2b6f6992017-07-09 18:11:34 +02003-- Copyright (C) 2017 Nico Huber <nico.h@gmx.de>
Nico Huber83693c82016-10-08 22:17:55 +02004--
5-- This program is free software; you can redistribute it and/or modify
6-- it under the terms of the GNU General Public License as published by
Nico Huber125a29e2016-10-18 00:23:54 +02007-- the Free Software Foundation; either version 2 of the License, or
8-- (at your option) any later version.
Nico Huber83693c82016-10-08 22:17:55 +02009--
10-- This program is distributed in the hope that it will be useful,
11-- but WITHOUT ANY WARRANTY; without even the implied warranty of
12-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13-- GNU General Public License for more details.
14--
15
Nico Huber2b6f6992017-07-09 18:11:34 +020016with HW.MMIO_Range;
17pragma Elaborate_All (HW.MMIO_Range);
18with HW.PCI.Dev;
19pragma Elaborate_All (HW.PCI.Dev);
20
Nico Huber5374c3a2017-07-15 21:48:06 +020021with HW.GFX.Framebuffer_Filler;
22
Nico Huber83693c82016-10-08 22:17:55 +020023with HW.GFX.GMA.Config;
Nico Huber8c45bcf2016-11-20 17:30:57 +010024with HW.GFX.GMA.Config_Helpers;
Nico Huber83693c82016-10-08 22:17:55 +020025with HW.GFX.GMA.Registers;
26with HW.GFX.GMA.Power_And_Clocks;
27with HW.GFX.GMA.Panel;
28with HW.GFX.GMA.PLLs;
29with HW.GFX.GMA.Port_Detect;
30with HW.GFX.GMA.Connectors;
31with HW.GFX.GMA.Connector_Info;
32with HW.GFX.GMA.Pipe_Setup;
33
Nico Huber83693c82016-10-08 22:17:55 +020034with HW.Debug;
35with GNAT.Source_Info;
36
Nico Huber83693c82016-10-08 22:17:55 +020037use type HW.Int32;
38
39package body HW.GFX.GMA
40 with Refined_State =>
41 (State =>
Nico Huber2b6f6992017-07-09 18:11:34 +020042 (Dev.Address_State,
43 Registers.Address_State,
Nico Huber83693c82016-10-08 22:17:55 +020044 PLLs.State, Panel.Panel_State,
Nico Huber1a712d32017-01-09 15:11:04 +010045 Cur_Configs, Allocated_PLLs,
Nico Huberc3f66f62017-07-16 21:39:54 +020046 HPD_Delay, Wait_For_HPD,
47 Linear_FB_Base),
Nico Huber83693c82016-10-08 22:17:55 +020048 Init_State => Initialized,
Arthur Heymansd1988d12018-03-28 16:27:57 +020049 Config_State => (Config.Valid_Port_GPU, Config.Raw_Clock),
Nico Huber83693c82016-10-08 22:17:55 +020050 Device_State =>
Nico Huber2b6f6992017-07-09 18:11:34 +020051 (Dev.PCI_State, Registers.Register_State, Registers.GTT_State))
Nico Huber83693c82016-10-08 22:17:55 +020052is
Nico Huber2b6f6992017-07-09 18:11:34 +020053 pragma Disable_Atomic_Synchronization;
Nico Huber83693c82016-10-08 22:17:55 +020054
55 subtype Port_Name is String (1 .. 8);
56 type Port_Name_Array is array (Port_Type) of Port_Name;
57 Port_Names : constant Port_Name_Array :=
58 (Disabled => "Disabled",
59 Internal => "Internal",
60 DP1 => "DP1 ",
61 DP2 => "DP2 ",
62 DP3 => "DP3 ",
Nico Huber0d454cd2016-11-21 13:33:43 +010063 HDMI1 => "HDMI1 ",
64 HDMI2 => "HDMI2 ",
65 HDMI3 => "HDMI3 ",
Nico Huber83693c82016-10-08 22:17:55 +020066 Analog => "Analog ");
67
Nico Huber2b6f6992017-07-09 18:11:34 +020068 package Dev is new HW.PCI.Dev (PCI.Address'(0, 2, 0));
69
Nico Huber83693c82016-10-08 22:17:55 +020070 package Display_Controller renames Pipe_Setup;
71
Nico Huber99f10f32016-11-20 00:34:05 +010072 type PLLs_Type is array (Pipe_Index) of PLLs.T;
Nico Huber83693c82016-10-08 22:17:55 +020073
Nico Huber83693c82016-10-08 22:17:55 +020074 type HPD_Type is array (Port_Type) of Boolean;
Nico Huber3be61d42017-01-09 13:58:18 +010075 type HPD_Delay_Type is array (Active_Port_Type) of Time.T;
Nico Huber83693c82016-10-08 22:17:55 +020076
Nico Huber83693c82016-10-08 22:17:55 +020077 Allocated_PLLs : PLLs_Type;
Nico Huber83693c82016-10-08 22:17:55 +020078 HPD_Delay : HPD_Delay_Type;
79 Wait_For_HPD : HPD_Type;
80 Initialized : Boolean := False;
81
Nico Huberc3f66f62017-07-16 21:39:54 +020082 Linear_FB_Base : Word64;
83
Nico Huber83693c82016-10-08 22:17:55 +020084 ----------------------------------------------------------------------------
85
Nico Huberf54d0962016-10-20 14:17:18 +020086 PCH_RAWCLK_FREQ_MASK : constant := 16#3ff# * 2 ** 0;
87
88 function PCH_RAWCLK_FREQ (Freq : Frequency_Type) return Word32
89 is
90 begin
91 return Word32 (Freq / 1_000_000);
92 end PCH_RAWCLK_FREQ;
93
94 ----------------------------------------------------------------------------
95
Nico Huber43370ba2017-01-09 15:26:19 +010096 procedure Enable_Output
97 (Pipe : in Pipe_Index;
98 Pipe_Cfg : in Pipe_Config;
99 Success : out Boolean)
100 is
101 Port_Cfg : Port_Config;
102 begin
Nico Huber3be61d42017-01-09 13:58:18 +0100103 pragma Debug (Debug.New_Line);
104 pragma Debug (Debug.Put_Line
105 ("Trying to enable port " & Port_Names (Pipe_Cfg.Port)));
106
Nico Huber43370ba2017-01-09 15:26:19 +0100107 Config_Helpers.Fill_Port_Config
108 (Port_Cfg, Pipe, Pipe_Cfg.Port, Pipe_Cfg.Mode, Success);
109
110 if Success then
111 Success := Config_Helpers.Validate_Config
Nico Hubercbbaade2018-01-02 13:59:36 +0100112 (Pipe_Cfg.Framebuffer, Port_Cfg.Mode, Pipe);
Nico Huber43370ba2017-01-09 15:26:19 +0100113 end if;
114
Nico Huber43370ba2017-01-09 15:26:19 +0100115 if Success then
Nico Huber43370ba2017-01-09 15:26:19 +0100116 Connector_Info.Preferred_Link_Setting (Port_Cfg, Success);
117 end if;
118
119 -- loop over all possible DP-lane configurations
120 -- (non-DP ports use a single fake configuration)
121 while Success loop
122 pragma Loop_Invariant
123 (Pipe_Cfg.Port in Active_Port_Type and
124 Port_Cfg.Mode = Port_Cfg.Mode'Loop_Entry);
125
126 PLLs.Alloc
127 (Port_Cfg => Port_Cfg,
128 PLL => Allocated_PLLs (Pipe),
129 Success => Success);
130
131 if Success then
132 -- try each DP-lane configuration twice
133 for Try in 1 .. 2 loop
134 pragma Loop_Invariant
135 (Pipe_Cfg.Port in Active_Port_Type);
136
Nico Huber4798c662017-01-11 12:44:48 +0100137 -- Clear pending hot-plug events before every try
138 Port_Detect.Clear_Hotplug_Detect (Pipe_Cfg.Port);
139
Nico Huber43370ba2017-01-09 15:26:19 +0100140 Connectors.Pre_On
141 (Pipe => Pipe,
142 Port_Cfg => Port_Cfg,
143 PLL_Hint => PLLs.Register_Value (Allocated_PLLs (Pipe)),
144 Success => Success);
145
146 if Success then
147 Display_Controller.On
148 (Pipe => Pipe,
149 Port_Cfg => Port_Cfg,
Nico Huber4dc4c612018-01-10 15:55:09 +0100150 Framebuffer => Pipe_Cfg.Framebuffer,
151 Cursor => Pipe_Cfg.Cursor);
Nico Huber43370ba2017-01-09 15:26:19 +0100152
153 Connectors.Post_On
Arthur Heymans60d0e5f2018-03-28 17:08:27 +0200154 (Pipe => Pipe,
155 Port_Cfg => Port_Cfg,
Nico Huber43370ba2017-01-09 15:26:19 +0100156 PLL_Hint => PLLs.Register_Value (Allocated_PLLs (Pipe)),
157 Success => Success);
158
159 if not Success then
160 Display_Controller.Off (Pipe);
161 Connectors.Post_Off (Port_Cfg);
162 end if;
163 end if;
164
165 exit when Success;
166 end loop;
167 exit when Success; -- connection established => stop loop
168
169 -- connection failed
170 PLLs.Free (Allocated_PLLs (Pipe));
171 end if;
172
173 Connector_Info.Next_Link_Setting (Port_Cfg, Success);
174 end loop;
175
176 if Success then
177 pragma Debug (Debug.Put_Line
178 ("Enabled port " & Port_Names (Pipe_Cfg.Port)));
179 else
180 Wait_For_HPD (Pipe_Cfg.Port) := True;
181 if Pipe_Cfg.Port = Internal then
182 Panel.Off;
183 end if;
184 end if;
185 end Enable_Output;
186
Nico Huber3be61d42017-01-09 13:58:18 +0100187 procedure Disable_Output (Pipe : Pipe_Index; Pipe_Cfg : Pipe_Config)
188 is
189 Port_Cfg : Port_Config;
190 Success : Boolean;
191 begin
192 Config_Helpers.Fill_Port_Config
193 (Port_Cfg, Pipe, Pipe_Cfg.Port, Pipe_Cfg.Mode, Success);
194 if Success then
195 pragma Debug (Debug.New_Line);
196 pragma Debug (Debug.Put_Line
197 ("Disabling port " & Port_Names (Pipe_Cfg.Port)));
198 pragma Debug (Debug.New_Line);
199
200 Connectors.Pre_Off (Port_Cfg);
201 Display_Controller.Off (Pipe);
202 Connectors.Post_Off (Port_Cfg);
203
204 PLLs.Free (Allocated_PLLs (Pipe));
205 end if;
206 end Disable_Output;
207
Nico Huber99f10f32016-11-20 00:34:05 +0100208 procedure Update_Outputs (Configs : Pipe_Configs)
Nico Huber83693c82016-10-08 22:17:55 +0200209 is
Nico Huber3be61d42017-01-09 13:58:18 +0100210 procedure Check_HPD (Port : in Active_Port_Type; Detected : out Boolean)
211 is
212 HPD_Delay_Over : constant Boolean := Time.Timed_Out (HPD_Delay (Port));
213 begin
214 if HPD_Delay_Over then
215 Port_Detect.Hotplug_Detect (Port, Detected);
216 HPD_Delay (Port) := Time.MS_From_Now (333);
217 else
218 Detected := False;
219 end if;
220 end Check_HPD;
Nico Huberb56b9c52017-01-11 15:12:23 +0100221
Nico Huber564103f2017-01-11 15:33:07 +0100222 Power_Changed : Boolean := False;
Nico Huberb56b9c52017-01-11 15:12:23 +0100223 Old_Configs : Pipe_Configs;
Nico Huber564103f2017-01-11 15:33:07 +0100224
225 -- Only called when we actually tried to change something
226 -- so we don't congest the log with unnecessary messages.
227 procedure Update_Power
228 is
229 begin
230 if not Power_Changed then
231 Power_And_Clocks.Power_Up (Old_Configs, Configs);
232 Power_Changed := True;
233 end if;
234 end Update_Power;
Nico Huber3d06de82018-05-29 01:35:04 +0200235
236 function Full_Update (Cur_Config, New_Config : Pipe_Config) return Boolean
237 is
238 begin
239 return
240 Cur_Config.Port /= New_Config.Port or else
241 Cur_Config.Mode /= New_Config.Mode or else
242 (Config.Use_PDW_For_EDP_Scaling and then
243 (Cur_Config.Port = Internal and
244 Requires_Scaling (Cur_Config) /= Requires_Scaling (New_Config)));
245 end Full_Update;
Nico Huber83693c82016-10-08 22:17:55 +0200246 begin
247 Old_Configs := Cur_Configs;
248
Nico Huberb56b9c52017-01-11 15:12:23 +0100249 -- disable all pipes that changed or had a hot-plug event
250 for Pipe in Pipe_Index loop
251 declare
252 Unplug_Detected : Boolean;
253 Cur_Config : Pipe_Config renames Cur_Configs (Pipe);
254 New_Config : Pipe_Config renames Configs (Pipe);
255 begin
256 if Cur_Config.Port /= Disabled then
257 Check_HPD (Cur_Config.Port, Unplug_Detected);
Nico Huber83693c82016-10-08 22:17:55 +0200258
Nico Huber3d06de82018-05-29 01:35:04 +0200259 if Full_Update (Cur_Config, New_Config) or Unplug_Detected then
Nico Huberb56b9c52017-01-11 15:12:23 +0100260 Disable_Output (Pipe, Cur_Config);
261 Cur_Config.Port := Disabled;
Nico Huber564103f2017-01-11 15:33:07 +0100262 Update_Power;
Nico Huberb56b9c52017-01-11 15:12:23 +0100263 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200264 end if;
Nico Huberb56b9c52017-01-11 15:12:23 +0100265 end;
266 end loop;
Nico Huber83693c82016-10-08 22:17:55 +0200267
Nico Huberb56b9c52017-01-11 15:12:23 +0100268 -- enable all pipes that changed and should be active
269 for Pipe in Pipe_Index loop
270 declare
271 Success : Boolean;
272 Cur_Config : Pipe_Config renames Cur_Configs (Pipe);
273 New_Config : Pipe_Config renames Configs (Pipe);
274 begin
Nico Huber3d06de82018-05-29 01:35:04 +0200275 if New_Config.Port /= Disabled and
276 Full_Update (Cur_Config, New_Config)
Nico Huberb56b9c52017-01-11 15:12:23 +0100277 then
Nico Huber3be61d42017-01-09 13:58:18 +0100278 if Wait_For_HPD (New_Config.Port) then
279 Check_HPD (New_Config.Port, Success);
280 Wait_For_HPD (New_Config.Port) := not Success;
281 else
282 Success := True;
Nico Huber8c45bcf2016-11-20 17:30:57 +0100283 end if;
Nico Huberc7a4fee2016-11-03 18:18:03 +0100284
Nico Huber3be61d42017-01-09 13:58:18 +0100285 if Success then
Nico Huber564103f2017-01-11 15:33:07 +0100286 Update_Power;
Nico Huberb56b9c52017-01-11 15:12:23 +0100287 Enable_Output (Pipe, New_Config, Success);
Nico Huber3be61d42017-01-09 13:58:18 +0100288 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200289
290 if Success then
Nico Huberb56b9c52017-01-11 15:12:23 +0100291 Cur_Config := New_Config;
Nico Huber83693c82016-10-08 22:17:55 +0200292 end if;
Nico Huber3be61d42017-01-09 13:58:18 +0100293
Nico Huberb56b9c52017-01-11 15:12:23 +0100294 -- update framebuffer offset only
295 elsif New_Config.Port /= Disabled and
Nico Huberf7f537e2018-01-02 14:15:43 +0100296 Cur_Config.Framebuffer /= New_Config.Framebuffer and
297 Config_Helpers.Validate_Config
298 (New_Config.Framebuffer, New_Config.Mode, Pipe)
Nico Huberb56b9c52017-01-11 15:12:23 +0100299 then
Nico Huberf7f537e2018-01-02 14:15:43 +0100300 Display_Controller.Setup_FB
301 (Pipe, New_Config.Mode, New_Config.Framebuffer);
Nico Huberb56b9c52017-01-11 15:12:23 +0100302 Cur_Config := New_Config;
303 end if;
304 end;
Nico Huber83693c82016-10-08 22:17:55 +0200305 end loop;
306
Nico Huber564103f2017-01-11 15:33:07 +0100307 if Power_Changed then
Nico Huber83693c82016-10-08 22:17:55 +0200308 Power_And_Clocks.Power_Down (Old_Configs, Configs, Cur_Configs);
309 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200310 end Update_Outputs;
311
312 ----------------------------------------------------------------------------
313
314 procedure Initialize
Nico Huber2b6f6992017-07-09 18:11:34 +0200315 (Write_Delay : in Word64 := 0;
Nico Huber793a8d42016-11-21 18:57:03 +0100316 Clean_State : in Boolean := False;
Nico Huber83693c82016-10-08 22:17:55 +0200317 Success : out Boolean)
318 with
319 Refined_Global =>
320 (In_Out =>
Nico Hubere015e822017-08-25 20:12:09 +0200321 (Config.Valid_Port_GPU, Dev.PCI_State,
Arthur Heymansd1988d12018-03-28 16:27:57 +0200322 Registers.Register_State, Port_IO.State,
323 Config.Raw_Clock),
Nico Huber83693c82016-10-08 22:17:55 +0200324 Input =>
325 (Time.State),
326 Output =>
Nico Huber2b6f6992017-07-09 18:11:34 +0200327 (Dev.Address_State,
328 Registers.Address_State,
Nico Huber83693c82016-10-08 22:17:55 +0200329 PLLs.State, Panel.Panel_State,
Nico Huber1a712d32017-01-09 15:11:04 +0100330 Cur_Configs, Allocated_PLLs,
Nico Huberc3f66f62017-07-16 21:39:54 +0200331 HPD_Delay, Wait_For_HPD,
332 Linear_FB_Base, Initialized))
Nico Huber83693c82016-10-08 22:17:55 +0200333 is
334 use type HW.Word64;
335
Nico Huber2b6f6992017-07-09 18:11:34 +0200336 PCI_MMIO_Base, PCI_GTT_Base : Word64;
337
Nico Huber83693c82016-10-08 22:17:55 +0200338 Now : constant Time.T := Time.Now;
339
340 procedure Check_Platform (Success : out Boolean)
341 is
342 Audio_VID_DID : Word32;
343 begin
344 case Config.CPU is
Arthur Heymans73ea0322018-03-28 17:17:07 +0200345 when G45 =>
346 Registers.Read (Registers.G4X_AUD_VID_DID, Audio_VID_DID);
Nico Huber83693c82016-10-08 22:17:55 +0200347 when Haswell .. Skylake =>
348 Registers.Read (Registers.AUD_VID_DID, Audio_VID_DID);
349 when Ironlake .. Ivybridge =>
350 Registers.Read (Registers.PCH_AUD_VID_DID, Audio_VID_DID);
351 end case;
352 Success :=
353 (case Config.CPU is
Nico Huber21da5742017-01-20 14:00:53 +0100354 when Broxton => Audio_VID_DID = 16#8086_280a#,
Nico Huber83693c82016-10-08 22:17:55 +0200355 when Skylake => Audio_VID_DID = 16#8086_2809#,
356 when Broadwell => Audio_VID_DID = 16#8086_2808#,
357 when Haswell => Audio_VID_DID = 16#8086_2807#,
358 when Ivybridge |
359 Sandybridge => Audio_VID_DID = 16#8086_2806# or
360 Audio_VID_DID = 16#8086_2805#,
Arthur Heymans73ea0322018-03-28 17:17:07 +0200361 when Ironlake => Audio_VID_DID = 16#0000_0000#,
362 when G45 => Audio_VID_DID = 16#8086_2801# or
363 Audio_VID_DID = 16#8086_2802# or
364 Audio_VID_DID = 16#8086_2803#);
Nico Huber83693c82016-10-08 22:17:55 +0200365 end Check_Platform;
Nico Hubere7ac6eb2017-09-04 23:54:13 +0200366
367 procedure Check_Platform_PCI (Success : out Boolean)
368 is
369 use type HW.Word16;
370 Vendor, Device : Word16;
371 begin
372 Dev.Read16 (Vendor, PCI.Vendor_Id);
373 Dev.Read16 (Device, PCI.Device_Id);
374
375 Success := Vendor = 16#8086# and Config.Compatible_GPU (Device);
376 end Check_Platform_PCI;
Nico Huber83693c82016-10-08 22:17:55 +0200377 begin
Nico Huber83693c82016-10-08 22:17:55 +0200378 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
379
380 pragma Debug (Debug.Set_Register_Write_Delay (Write_Delay));
381
Nico Huberc3f66f62017-07-16 21:39:54 +0200382 Linear_FB_Base := 0;
Nico Huber83693c82016-10-08 22:17:55 +0200383 Wait_For_HPD := HPD_Type'(others => False);
384 HPD_Delay := HPD_Delay_Type'(others => Now);
Nico Huber83693c82016-10-08 22:17:55 +0200385 Allocated_PLLs := (others => PLLs.Invalid);
Nico Huber99f10f32016-11-20 00:34:05 +0100386 Cur_Configs := Pipe_Configs'
387 (others => Pipe_Config'
Nico Huber83693c82016-10-08 22:17:55 +0200388 (Port => Disabled,
389 Framebuffer => HW.GFX.Default_FB,
Nico Hubera02b2c62018-01-09 15:58:34 +0100390 Cursor => Default_Cursor,
Nico Huber83693c82016-10-08 22:17:55 +0200391 Mode => HW.GFX.Invalid_Mode));
Nico Huber83693c82016-10-08 22:17:55 +0200392 PLLs.Initialize;
393
Nico Huber2b6f6992017-07-09 18:11:34 +0200394 Dev.Initialize (Success);
395
396 if Success then
397 Dev.Map (PCI_MMIO_Base, PCI.Res0, Length => Config.GTT_Offset);
398 Dev.Map (PCI_GTT_Base, PCI.Res0, Offset => Config.GTT_Offset);
399 if PCI_MMIO_Base /= 0 and PCI_GTT_Base /= 0 then
400 Registers.Set_Register_Base (PCI_MMIO_Base, PCI_GTT_Base);
401 else
402 pragma Debug (Debug.Put_Line
403 ("ERROR: Couldn't map resoure0."));
404 Registers.Set_Register_Base (Config.Default_MMIO_Base);
405 Success := Config.Default_MMIO_Base_Set;
406 end if;
Nico Hubere7ac6eb2017-09-04 23:54:13 +0200407
408 if Success then
409 Check_Platform_PCI (Success);
410 end if;
Nico Huber2b6f6992017-07-09 18:11:34 +0200411 else
412 pragma Debug (Debug.Put_Line
413 ("WARNING: Couldn't initialize PCI dev."));
414 Registers.Set_Register_Base (Config.Default_MMIO_Base);
415 Success := Config.Default_MMIO_Base_Set;
Nico Huber2b6f6992017-07-09 18:11:34 +0200416
Nico Hubere7ac6eb2017-09-04 23:54:13 +0200417 if Success then
418 Check_Platform (Success);
419 end if;
Nico Huber2b6f6992017-07-09 18:11:34 +0200420 end if;
421
Nico Huber83693c82016-10-08 22:17:55 +0200422 if not Success then
423 pragma Debug (Debug.Put_Line ("ERROR: Incompatible CPU or PCH."));
424
425 Panel.Static_Init; -- for flow analysis
426
427 Initialized := False;
428 return;
429 end if;
430
431 Panel.Setup_PP_Sequencer;
432 Port_Detect.Initialize;
Nico Huber0923b792017-06-09 15:28:41 +0200433 Connectors.Initialize;
Nico Huber83693c82016-10-08 22:17:55 +0200434
Nico Huber793a8d42016-11-21 18:57:03 +0100435 if Clean_State then
436 Power_And_Clocks.Pre_All_Off;
437 Connectors.Pre_All_Off;
438 Display_Controller.All_Off;
439 Connectors.Post_All_Off;
440 PLLs.All_Off;
441 Power_And_Clocks.Post_All_Off;
Nico Huber17d64b62017-07-15 20:51:25 +0200442 Registers.Clear_Fences;
Nico Huber33912aa2016-12-06 20:36:23 +0100443 else
444 -- According to PRMs, VGA plane is the only thing
Nico Huber3a0e2a02017-07-19 14:41:46 +0200445 -- that's enabled by default after reset...
Nico Huber33912aa2016-12-06 20:36:23 +0100446 Display_Controller.Legacy_VGA_Off;
Nico Huber3a0e2a02017-07-19 14:41:46 +0200447 -- ... along with some DDI port bits since Skylake.
448 Connectors.Post_Reset_Off;
Nico Huber793a8d42016-11-21 18:57:03 +0100449 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200450
451 -------------------- Now restart from a clean state ---------------------
452 Power_And_Clocks.Initialize;
453
Nico Huber1c3b9282017-02-09 13:57:04 +0100454 if Config.Has_PCH then
455 Registers.Unset_And_Set_Mask
456 (Register => Registers.PCH_RAWCLK_FREQ,
457 Mask_Unset => PCH_RAWCLK_FREQ_MASK,
458 Mask_Set => PCH_RAWCLK_FREQ (Config.Default_RawClk_Freq));
459 end if;
Nico Huberf54d0962016-10-20 14:17:18 +0200460
Nico Huber83693c82016-10-08 22:17:55 +0200461 Initialized := True;
462
463 end Initialize;
464
465 function Is_Initialized return Boolean
466 with
467 Refined_Post => Is_Initialized'Result = Initialized
468 is
469 begin
470 return Initialized;
471 end Is_Initialized;
472
473 ----------------------------------------------------------------------------
474
Nico Huber42fb2d02017-09-01 17:01:51 +0200475 procedure Power_Up_VGA
476 is
477 Fake_Config : constant Pipe_Configs :=
478 (Primary =>
479 (Port => Analog,
480 Framebuffer => HW.GFX.Default_FB,
Nico Hubera02b2c62018-01-09 15:58:34 +0100481 Cursor => Default_Cursor,
Nico Huber42fb2d02017-09-01 17:01:51 +0200482 Mode => HW.GFX.Invalid_Mode),
483 others =>
484 (Port => Disabled,
485 Framebuffer => HW.GFX.Default_FB,
Nico Hubera02b2c62018-01-09 15:58:34 +0100486 Cursor => Default_Cursor,
Nico Huber42fb2d02017-09-01 17:01:51 +0200487 Mode => HW.GFX.Invalid_Mode));
488 begin
489 Power_And_Clocks.Power_Up (Cur_Configs, Fake_Config);
490 end Power_Up_VGA;
491
492 ----------------------------------------------------------------------------
493
Nico Huber5374c3a2017-07-15 21:48:06 +0200494 function FB_First_Page (FB : Framebuffer_Type) return Natural is
Nico Huber34be6542017-12-13 09:26:24 +0100495 (Natural (Phys_Offset (FB) / GTT_Page_Size));
Nico Huber5374c3a2017-07-15 21:48:06 +0200496 function FB_Pages (FB : Framebuffer_Type) return Natural is
497 (Natural (Div_Round_Up (FB_Size (FB), GTT_Page_Size)));
498 function FB_Last_Page (FB : Framebuffer_Type) return Natural is
499 (FB_First_Page (FB) + FB_Pages (FB) - 1);
500
Nico Huber34be6542017-12-13 09:26:24 +0100501 -- Check basics and that it fits in GTT. For 90 degree rotations,
502 -- the Offset should be above GTT_Rotation_Offset. The latter will
503 -- be subtracted for the aperture mapping.
Nico Huber5374c3a2017-07-15 21:48:06 +0200504 function Valid_FB (FB : Framebuffer_Type) return Boolean is
Nico Huber34be6542017-12-13 09:26:24 +0100505 (Valid_Stride (FB) and
506 FB_First_Page (FB) in GTT_Range and
507 FB_Last_Page (FB) in GTT_Range and
508 (not Rotation_90 (FB) or
509 (FB_Last_Page (FB) + GTT_Rotation_Offset in GTT_Range and
510 FB.Offset >= Word32 (GTT_Rotation_Offset) * GTT_Page_Size)));
Nico Huber5374c3a2017-07-15 21:48:06 +0200511
512 -- Also check that we don't overflow the GTT's 39-bit space
513 -- (always true with a 32-bit base)
514 function Valid_Phys_FB (FB : Framebuffer_Type; Phys_Base : Word32)
515 return Boolean is
516 (Valid_FB (FB) and
Nico Huber34be6542017-12-13 09:26:24 +0100517 Int64 (Phys_Base) + Int64 (Phys_Offset (FB)) + Int64 (FB_Size (FB)) <=
Nico Huber5374c3a2017-07-15 21:48:06 +0200518 Int64 (GTT_Address_Type'Last))
519 with
520 Ghost;
521
Nico Huber83693c82016-10-08 22:17:55 +0200522 procedure Write_GTT
523 (GTT_Page : GTT_Range;
524 Device_Address : GTT_Address_Type;
Nico Huber5374c3a2017-07-15 21:48:06 +0200525 Valid : Boolean)
526 is
Nico Huber83693c82016-10-08 22:17:55 +0200527 begin
528 Registers.Write_GTT (GTT_Page, Device_Address, Valid);
529 end Write_GTT;
530
Nico Huber194e57e2017-07-15 21:15:46 +0200531 procedure Setup_Default_GTT (FB : Framebuffer_Type; Phys_Base : Word32)
Nico Huber5374c3a2017-07-15 21:48:06 +0200532 with
533 Pre => Is_Initialized and Valid_Phys_FB (FB, Phys_Base)
Nico Huber83693c82016-10-08 22:17:55 +0200534 is
Nico Huber194e57e2017-07-15 21:15:46 +0200535 Phys_Addr : GTT_Address_Type :=
Nico Huber34be6542017-12-13 09:26:24 +0100536 GTT_Address_Type (Phys_Base) + GTT_Address_Type (Phys_Offset (FB));
Nico Huber83693c82016-10-08 22:17:55 +0200537 begin
Nico Huber194e57e2017-07-15 21:15:46 +0200538 for Idx in FB_First_Page (FB) .. FB_Last_Page (FB) loop
Nico Huber83693c82016-10-08 22:17:55 +0200539 Registers.Write_GTT
540 (GTT_Page => Idx,
541 Device_Address => Phys_Addr,
542 Valid => True);
Nico Huber194e57e2017-07-15 21:15:46 +0200543 Phys_Addr := Phys_Addr + GTT_Page_Size;
Nico Huber83693c82016-10-08 22:17:55 +0200544 end loop;
Nico Huber9b479412017-08-27 11:55:56 +0200545
546 if Rotation_90 (FB) and FB.Tiling = Y_Tiled and FB.V_Stride >= 32 then
547 declare
548 V_Pages : constant Natural := Natural (FB.V_Stride) / 32;
549 Bytes_Per_Row : constant GTT_Address_Type :=
550 GTT_Address_Type (Pixel_To_Bytes (32 * FB.Stride, FB));
551 begin
552 Phys_Addr := GTT_Address_Type (Phys_Base) +
Nico Huber34be6542017-12-13 09:26:24 +0100553 GTT_Address_Type (Phys_Offset (FB)) +
Nico Huber9b479412017-08-27 11:55:56 +0200554 GTT_Address_Type (FB_Size (FB));
555 for Page in FB_First_Page (FB) .. FB_Last_Page (FB) loop
556 Phys_Addr := Phys_Addr - Bytes_Per_Row;
557 Registers.Write_GTT
558 (GTT_Page => GTT_Rotation_Offset + Page,
559 Device_Address => Phys_Addr,
560 Valid => True);
561
562 if (Page - FB_First_Page (FB) + 1) mod V_Pages = 0 then
563 Phys_Addr := Phys_Addr + GTT_Page_Size +
564 GTT_Address_Type (V_Pages) * Bytes_Per_Row;
565 end if;
566 end loop;
567 end;
568 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200569 end Setup_Default_GTT;
570
571 ----------------------------------------------------------------------------
572
Nico Hubereedde882017-07-16 02:54:39 +0200573 use type HW.Word16;
574 subtype Stolen_Size_Range is Int64 range 0 .. 2 ** 33;
575
576 function GGMS_Gen4 (GGC : Word16) return Natural is
577 (Natural (Shift_Right (GGC, 8) and 16#07#));
578 function GTT_Size_Gen4 (GGC : Word16) return Natural is
579 (if GGMS_Gen4 (GGC) in 1 .. 3 then
580 (GGMS_Gen4 (GGC) + 1) * 2 ** 19 else 0);
581
582 function GMS_Gen4 (GGC : Word16) return Natural is
583 (Natural (Shift_Right (GGC, 4) and 16#0f#));
584 Valid_Stolen_Size_Gen4 : constant
585 array (Natural range 1 .. 13) of Stolen_Size_Range :=
586 (1, 4, 8, 16, 32, 48, 64, 128, 256, 96, 160, 224, 352);
587 function Stolen_Size_Gen4 (GGC : Word16) return Stolen_Size_Range is
588 (if GMS_Gen4 (GGC) in Valid_Stolen_Size_Gen4'Range then
Arthur Heymans5fd9a312017-09-12 12:45:18 +0200589 Valid_Stolen_Size_Gen4 (GMS_Gen4 (GGC)) * 2 ** 20 else 0);
Nico Hubereedde882017-07-16 02:54:39 +0200590
591 function GTT_Size_Gen6 (GGC : Word16) return Natural is
592 (Natural (Shift_Right (GGC, 8) and 16#03#) * 2 ** 20);
593
594 function Stolen_Size_Gen6 (GGC : Word16) return Stolen_Size_Range is
595 (Stolen_Size_Range (Shift_Right (GGC, 3) and 16#1f#) * 32 * 2 ** 20);
596
597 function GTT_Size_Gen8 (GGC : Word16) return Natural is
598 (Natural (Shift_Right (GGC, 6) and 16#03#) * 2 ** 20);
599
600 function GMS_Gen8 (GGC : Word16) return Stolen_Size_Range is
601 (Stolen_Size_Range (Shift_Right (GGC, 8) and 16#ff#));
602 function Stolen_Size_Gen8 (GGC : Word16) return Stolen_Size_Range is
603 (GMS_Gen8 (GGC) * 32 * 2 ** 20);
604
605 function Stolen_Size_Gen9 (GGC : Word16) return Stolen_Size_Range is
606 (if GMS_Gen8 (GGC) < 16#f0# then
607 Stolen_Size_Gen8 (GGC)
608 else
609 (GMS_Gen8 (GGC) - 16#f0# + 1) * 4 * 2 ** 20);
610
611 procedure Decode_Stolen
612 (GTT_Size : out Natural;
613 Stolen_Size : out Stolen_Size_Range)
614 with
615 Pre => Is_Initialized
616 is
617 GGC_Reg : constant :=
618 (case Config.CPU is
Arthur Heymans73ea0322018-03-28 17:17:07 +0200619 when G45 | Ironlake => 16#52#,
Nico Hubereedde882017-07-16 02:54:39 +0200620 when Sandybridge .. Skylake => 16#50#);
621 GGC : Word16;
622 begin
623 Dev.Read16 (GGC, GGC_Reg);
624 case Config.CPU is
Arthur Heymans73ea0322018-03-28 17:17:07 +0200625 when G45 | Ironlake =>
Nico Hubereedde882017-07-16 02:54:39 +0200626 GTT_Size := GTT_Size_Gen4 (GGC);
627 Stolen_Size := Stolen_Size_Gen4 (GGC);
628 when Sandybridge .. Haswell =>
629 GTT_Size := GTT_Size_Gen6 (GGC);
630 Stolen_Size := Stolen_Size_Gen6 (GGC);
631 when Broadwell =>
632 GTT_Size := GTT_Size_Gen8 (GGC);
633 Stolen_Size := Stolen_Size_Gen8 (GGC);
634 when Broxton .. Skylake =>
635 GTT_Size := GTT_Size_Gen8 (GGC);
636 Stolen_Size := Stolen_Size_Gen9 (GGC);
637 end case;
638 end Decode_Stolen;
639
640 -- Additional runtime validation that FB fits stolen memory and aperture.
641 procedure Validate_FB (FB : Framebuffer_Type; Valid : out Boolean)
642 with
643 Pre => Is_Initialized,
644 Post => (if Valid then Valid_FB (FB))
645 is
646 GTT_Size, Aperture_Size : Natural;
647 Stolen_Size : Stolen_Size_Range;
648 begin
649 Valid := Valid_FB (FB);
650
651 if Valid then
652 Decode_Stolen (GTT_Size, Stolen_Size);
653 Dev.Resource_Size (Aperture_Size, PCI.Res2);
654 Valid :=
655 FB_Last_Page (FB) < GTT_Size / Config.GTT_PTE_Size and
656 FB_Last_Page (FB) < Natural (Stolen_Size / GTT_Page_Size) and
657 FB_Last_Page (FB) < Aperture_Size / GTT_Page_Size;
Nico Huber34be6542017-12-13 09:26:24 +0100658 pragma Debug (not Valid, Debug.Put_Line
Nico Hubereedde882017-07-16 02:54:39 +0200659 ("Stolen memory too small to hold framebuffer."));
660 end if;
661 end Validate_FB;
662
Nico Huber5374c3a2017-07-15 21:48:06 +0200663 procedure Setup_Default_FB
664 (FB : in Framebuffer_Type;
665 Clear : in Boolean := True;
666 Success : out Boolean)
667 is
668 GMA_Phys_Base : constant PCI.Index := 16#5c#;
669 GMA_Phys_Base_Mask : constant := 16#fff0_0000#;
670
671 Phys_Base : Word32;
672 begin
Nico Hubereedde882017-07-16 02:54:39 +0200673 Validate_FB (FB, Success);
Nico Huber5374c3a2017-07-15 21:48:06 +0200674
675 if Success then
676 Dev.Read32 (Phys_Base, GMA_Phys_Base);
677 Phys_Base := Phys_Base and GMA_Phys_Base_Mask;
678 Success := Phys_Base /= GMA_Phys_Base_Mask and Phys_Base /= 0;
679 pragma Debug (not Success, Debug.Put_Line
680 ("Failed to read stolen memory base."));
Nico Huber0164b022017-08-24 15:12:51 +0200681
682 if Success then
683 if FB.Tiling in XY_Tiling then
684 Registers.Add_Fence
685 (First_Page => FB_First_Page (FB),
686 Last_Page => FB_Last_Page (FB),
687 Tiling => FB.Tiling,
688 Pitch => FB_Pitch (FB.Stride, FB),
689 Success => Success);
690 end if;
691 pragma Debug (not Success, Debug.Put_Line
692 ("Tiled framebuffer but no fence regs available."));
693 end if;
694
Nico Huber5374c3a2017-07-15 21:48:06 +0200695 if Success then
696 Setup_Default_GTT (FB, Phys_Base);
697 end if;
698 end if;
699
700 if Success and then Clear then
701 declare
702 use type HW.Word64;
703 Linear_FB : Word64;
704 begin
Nico Huberc3f66f62017-07-16 21:39:54 +0200705 Map_Linear_FB (Linear_FB, FB);
Nico Huber5374c3a2017-07-15 21:48:06 +0200706 if Linear_FB /= 0 then
Nico Huberc3f66f62017-07-16 21:39:54 +0200707 Framebuffer_Filler.Fill (Linear_FB, FB);
Nico Huber5374c3a2017-07-15 21:48:06 +0200708 end if;
Nico Huber5374c3a2017-07-15 21:48:06 +0200709 end;
710 end if;
711 end Setup_Default_FB;
712
Nico Huberc3f66f62017-07-16 21:39:54 +0200713 procedure Map_Linear_FB (Linear_FB : out Word64; FB : in Framebuffer_Type)
714 is
715 use type HW.Word64;
716
717 Valid : Boolean;
718 begin
719 Linear_FB := 0;
720
721 if Linear_FB_Base = 0 then
722 Dev.Map (Linear_FB_Base, PCI.Res2);
723 pragma Debug
724 (Linear_FB_Base = 0, Debug.Put_Line ("Failed to map resource2."));
725 end if;
726
727 if Linear_FB_Base /= 0 then
728 Validate_FB (FB, Valid);
729 if Valid then
Nico Huber34be6542017-12-13 09:26:24 +0100730 Linear_FB := Linear_FB_Base + Word64 (Phys_Offset (FB));
Nico Huberc3f66f62017-07-16 21:39:54 +0200731 end if;
732 end if;
733 end Map_Linear_FB;
734
Nico Huber5374c3a2017-07-15 21:48:06 +0200735 ----------------------------------------------------------------------------
736
Nico Huber99f10f32016-11-20 00:34:05 +0100737 procedure Dump_Configs (Configs : Pipe_Configs)
Nico Huber83693c82016-10-08 22:17:55 +0200738 is
739 subtype Pipe_Name is String (1 .. 9);
Nico Huber99f10f32016-11-20 00:34:05 +0100740 type Pipe_Name_Array is array (Pipe_Index) of Pipe_Name;
Nico Huber83693c82016-10-08 22:17:55 +0200741 Pipe_Names : constant Pipe_Name_Array :=
742 (Primary => "Primary ",
743 Secondary => "Secondary",
744 Tertiary => "Tertiary ");
Nico Huber5ef4d602017-12-13 13:56:47 +0100745
746 subtype Tiling_Name is String (1 .. 7);
747 type Tiling_Name_Array is array (Tiling_Type) of Tiling_Name;
748 Tilings : constant Tiling_Name_Array :=
749 (Linear => "Linear ",
750 X_Tiled => "X_Tiled",
751 Y_Tiled => "Y_Tiled");
752
753 subtype Rotation_Name is String (1 .. 11);
754 type Rotation_Name_Array is array (Rotation_Type) of Rotation_Name;
755 Rotations : constant Rotation_Name_Array :=
756 (No_Rotation => "No_Rotation",
757 Rotated_90 => "Rotated_90 ",
758 Rotated_180 => "Rotated_180",
759 Rotated_270 => "Rotated_270");
Nico Huber83693c82016-10-08 22:17:55 +0200760 begin
761 Debug.New_Line;
Paul Menzelb83107c2017-05-04 09:02:33 +0200762 Debug.Put_Line ("CONFIG =>");
Nico Huber99f10f32016-11-20 00:34:05 +0100763 for Pipe in Pipe_Index loop
764 if Pipe = Pipe_Index'First then
Nico Huber83693c82016-10-08 22:17:55 +0200765 Debug.Put (" (");
766 else
767 Debug.Put (" ");
768 end if;
769 Debug.Put_Line (Pipe_Names (Pipe) & " =>");
770 Debug.Put_Line
771 (" (Port => " & Port_Names (Configs (Pipe).Port) & ",");
772 Debug.Put_Line (" Framebuffer =>");
Nico Huber5ef4d602017-12-13 13:56:47 +0100773 Debug.Put (" (Width => ");
Nico Huber83693c82016-10-08 22:17:55 +0200774 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Width);
775 Debug.Put_Line (",");
Nico Huber5ef4d602017-12-13 13:56:47 +0100776 Debug.Put (" Height => ");
Nico Huber83693c82016-10-08 22:17:55 +0200777 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Height);
778 Debug.Put_Line (",");
Nico Huber5ef4d602017-12-13 13:56:47 +0100779 Debug.Put (" Start_X => ");
780 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Start_X);
781 Debug.Put_Line (",");
782 Debug.Put (" Start_Y => ");
783 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Start_Y);
784 Debug.Put_Line (",");
785 Debug.Put (" Stride => ");
Nico Huber83693c82016-10-08 22:17:55 +0200786 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Stride);
787 Debug.Put_Line (",");
Nico Huber5ef4d602017-12-13 13:56:47 +0100788 Debug.Put (" V_Stride => ");
789 Debug.Put_Int32 (Configs (Pipe).Framebuffer.V_Stride);
790 Debug.Put_Line (",");
791 Debug.Put (" Tiling => ");
792 Debug.Put_Line (Tilings (Configs (Pipe).Framebuffer.Tiling) & ",");
793 Debug.Put (" Rotation => ");
794 Debug.Put_Line (Rotations (Configs (Pipe).Framebuffer.Rotation) & ",");
Nico Huber83693c82016-10-08 22:17:55 +0200795 Debug.Put (" Offset => ");
796 Debug.Put_Word32 (Configs (Pipe).Framebuffer.Offset);
797 Debug.Put_Line (",");
798 Debug.Put (" BPC => ");
799 Debug.Put_Int64 (Configs (Pipe).Framebuffer.BPC);
800 Debug.Put_Line ("),");
801 Debug.Put_Line (" Mode =>");
802 Debug.Put (" (Dotclock => ");
803 Debug.Put_Int64 (Configs (Pipe).Mode.Dotclock);
804 Debug.Put_Line (",");
805 Debug.Put (" H_Visible => ");
806 Debug.Put_Int16 (Configs (Pipe).Mode.H_Visible);
807 Debug.Put_Line (",");
808 Debug.Put (" H_Sync_Begin => ");
809 Debug.Put_Int16 (Configs (Pipe).Mode.H_Sync_Begin);
810 Debug.Put_Line (",");
811 Debug.Put (" H_Sync_End => ");
812 Debug.Put_Int16 (Configs (Pipe).Mode.H_Sync_End);
813 Debug.Put_Line (",");
814 Debug.Put (" H_Total => ");
815 Debug.Put_Int16 (Configs (Pipe).Mode.H_Total);
816 Debug.Put_Line (",");
817 Debug.Put (" V_Visible => ");
818 Debug.Put_Int16 (Configs (Pipe).Mode.V_Visible);
819 Debug.Put_Line (",");
820 Debug.Put (" V_Sync_Begin => ");
821 Debug.Put_Int16 (Configs (Pipe).Mode.V_Sync_Begin);
822 Debug.Put_Line (",");
823 Debug.Put (" V_Sync_End => ");
824 Debug.Put_Int16 (Configs (Pipe).Mode.V_Sync_End);
825 Debug.Put_Line (",");
826 Debug.Put (" V_Total => ");
827 Debug.Put_Int16 (Configs (Pipe).Mode.V_Total);
828 Debug.Put_Line (",");
829 Debug.Put_Line (" H_Sync_Active_High => " &
830 (if Configs (Pipe).Mode.H_Sync_Active_High
831 then "True,"
832 else "False,"));
833 Debug.Put_Line (" V_Sync_Active_High => " &
834 (if Configs (Pipe).Mode.V_Sync_Active_High
835 then "True,"
836 else "False,"));
837 Debug.Put (" BPC => ");
838 Debug.Put_Int64 (Configs (Pipe).Mode.BPC);
Nico Huber99f10f32016-11-20 00:34:05 +0100839 if Pipe /= Pipe_Index'Last then
Nico Huber83693c82016-10-08 22:17:55 +0200840 Debug.Put_Line (")),");
841 else
842 Debug.Put_Line (")));");
843 end if;
844 end loop;
845 end Dump_Configs;
846
847end HW.GFX.GMA;