blob: 809881a973c34a7e33f0189d33b9891a5a0a7f0d [file] [log] [blame]
Nico Huber83693c82016-10-08 22:17:55 +02001--
Nico Huber3be61d42017-01-09 13:58:18 +01002-- Copyright (C) 2014-2017 secunet Security Networks AG
Nico 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,
150 Framebuffer => Pipe_Cfg.Framebuffer);
151
152 Connectors.Post_On
Arthur Heymans60d0e5f2018-03-28 17:08:27 +0200153 (Pipe => Pipe,
154 Port_Cfg => Port_Cfg,
Nico Huber43370ba2017-01-09 15:26:19 +0100155 PLL_Hint => PLLs.Register_Value (Allocated_PLLs (Pipe)),
156 Success => Success);
157
158 if not Success then
159 Display_Controller.Off (Pipe);
160 Connectors.Post_Off (Port_Cfg);
161 end if;
162 end if;
163
164 exit when Success;
165 end loop;
166 exit when Success; -- connection established => stop loop
167
168 -- connection failed
169 PLLs.Free (Allocated_PLLs (Pipe));
170 end if;
171
172 Connector_Info.Next_Link_Setting (Port_Cfg, Success);
173 end loop;
174
175 if Success then
176 pragma Debug (Debug.Put_Line
177 ("Enabled port " & Port_Names (Pipe_Cfg.Port)));
178 else
179 Wait_For_HPD (Pipe_Cfg.Port) := True;
180 if Pipe_Cfg.Port = Internal then
181 Panel.Off;
182 end if;
183 end if;
184 end Enable_Output;
185
Nico Huber3be61d42017-01-09 13:58:18 +0100186 procedure Disable_Output (Pipe : Pipe_Index; Pipe_Cfg : Pipe_Config)
187 is
188 Port_Cfg : Port_Config;
189 Success : Boolean;
190 begin
191 Config_Helpers.Fill_Port_Config
192 (Port_Cfg, Pipe, Pipe_Cfg.Port, Pipe_Cfg.Mode, Success);
193 if Success then
194 pragma Debug (Debug.New_Line);
195 pragma Debug (Debug.Put_Line
196 ("Disabling port " & Port_Names (Pipe_Cfg.Port)));
197 pragma Debug (Debug.New_Line);
198
199 Connectors.Pre_Off (Port_Cfg);
200 Display_Controller.Off (Pipe);
201 Connectors.Post_Off (Port_Cfg);
202
203 PLLs.Free (Allocated_PLLs (Pipe));
204 end if;
205 end Disable_Output;
206
Nico Huber99f10f32016-11-20 00:34:05 +0100207 procedure Update_Outputs (Configs : Pipe_Configs)
Nico Huber83693c82016-10-08 22:17:55 +0200208 is
Nico Huber3be61d42017-01-09 13:58:18 +0100209 procedure Check_HPD (Port : in Active_Port_Type; Detected : out Boolean)
210 is
211 HPD_Delay_Over : constant Boolean := Time.Timed_Out (HPD_Delay (Port));
212 begin
213 if HPD_Delay_Over then
214 Port_Detect.Hotplug_Detect (Port, Detected);
215 HPD_Delay (Port) := Time.MS_From_Now (333);
216 else
217 Detected := False;
218 end if;
219 end Check_HPD;
Nico Huberb56b9c52017-01-11 15:12:23 +0100220
Nico Huber564103f2017-01-11 15:33:07 +0100221 Power_Changed : Boolean := False;
Nico Huberb56b9c52017-01-11 15:12:23 +0100222 Old_Configs : Pipe_Configs;
Nico Huber564103f2017-01-11 15:33:07 +0100223
224 -- Only called when we actually tried to change something
225 -- so we don't congest the log with unnecessary messages.
226 procedure Update_Power
227 is
228 begin
229 if not Power_Changed then
230 Power_And_Clocks.Power_Up (Old_Configs, Configs);
231 Power_Changed := True;
232 end if;
233 end Update_Power;
Nico Huber83693c82016-10-08 22:17:55 +0200234 begin
235 Old_Configs := Cur_Configs;
236
Nico Huberb56b9c52017-01-11 15:12:23 +0100237 -- disable all pipes that changed or had a hot-plug event
238 for Pipe in Pipe_Index loop
239 declare
240 Unplug_Detected : Boolean;
241 Cur_Config : Pipe_Config renames Cur_Configs (Pipe);
242 New_Config : Pipe_Config renames Configs (Pipe);
243 begin
244 if Cur_Config.Port /= Disabled then
245 Check_HPD (Cur_Config.Port, Unplug_Detected);
Nico Huber83693c82016-10-08 22:17:55 +0200246
Nico Huberb56b9c52017-01-11 15:12:23 +0100247 if Cur_Config.Port /= New_Config.Port or
248 Cur_Config.Mode /= New_Config.Mode or
249 Unplug_Detected
250 then
251 Disable_Output (Pipe, Cur_Config);
252 Cur_Config.Port := Disabled;
Nico Huber564103f2017-01-11 15:33:07 +0100253 Update_Power;
Nico Huberb56b9c52017-01-11 15:12:23 +0100254 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200255 end if;
Nico Huberb56b9c52017-01-11 15:12:23 +0100256 end;
257 end loop;
Nico Huber83693c82016-10-08 22:17:55 +0200258
Nico Huberb56b9c52017-01-11 15:12:23 +0100259 -- enable all pipes that changed and should be active
260 for Pipe in Pipe_Index loop
261 declare
262 Success : Boolean;
263 Cur_Config : Pipe_Config renames Cur_Configs (Pipe);
264 New_Config : Pipe_Config renames Configs (Pipe);
265 begin
266 if New_Config.Port /= Disabled and then
267 (Cur_Config.Port /= New_Config.Port or
268 Cur_Config.Mode /= New_Config.Mode)
269 then
Nico Huber3be61d42017-01-09 13:58:18 +0100270 if Wait_For_HPD (New_Config.Port) then
271 Check_HPD (New_Config.Port, Success);
272 Wait_For_HPD (New_Config.Port) := not Success;
273 else
274 Success := True;
Nico Huber8c45bcf2016-11-20 17:30:57 +0100275 end if;
Nico Huberc7a4fee2016-11-03 18:18:03 +0100276
Nico Huber3be61d42017-01-09 13:58:18 +0100277 if Success then
Nico Huber564103f2017-01-11 15:33:07 +0100278 Update_Power;
Nico Huberb56b9c52017-01-11 15:12:23 +0100279 Enable_Output (Pipe, New_Config, Success);
Nico Huber3be61d42017-01-09 13:58:18 +0100280 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200281
282 if Success then
Nico Huberb56b9c52017-01-11 15:12:23 +0100283 Cur_Config := New_Config;
Nico Huber83693c82016-10-08 22:17:55 +0200284 end if;
Nico Huber3be61d42017-01-09 13:58:18 +0100285
Nico Huberb56b9c52017-01-11 15:12:23 +0100286 -- update framebuffer offset only
287 elsif New_Config.Port /= Disabled and
Nico Huberf7f537e2018-01-02 14:15:43 +0100288 Cur_Config.Framebuffer /= New_Config.Framebuffer and
289 Config_Helpers.Validate_Config
290 (New_Config.Framebuffer, New_Config.Mode, Pipe)
Nico Huberb56b9c52017-01-11 15:12:23 +0100291 then
Nico Huberf7f537e2018-01-02 14:15:43 +0100292 Display_Controller.Setup_FB
293 (Pipe, New_Config.Mode, New_Config.Framebuffer);
Nico Huberb56b9c52017-01-11 15:12:23 +0100294 Cur_Config := New_Config;
295 end if;
296 end;
Nico Huber83693c82016-10-08 22:17:55 +0200297 end loop;
298
Nico Huber564103f2017-01-11 15:33:07 +0100299 if Power_Changed then
Nico Huber83693c82016-10-08 22:17:55 +0200300 Power_And_Clocks.Power_Down (Old_Configs, Configs, Cur_Configs);
301 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200302 end Update_Outputs;
303
304 ----------------------------------------------------------------------------
305
306 procedure Initialize
Nico Huber2b6f6992017-07-09 18:11:34 +0200307 (Write_Delay : in Word64 := 0;
Nico Huber793a8d42016-11-21 18:57:03 +0100308 Clean_State : in Boolean := False;
Nico Huber83693c82016-10-08 22:17:55 +0200309 Success : out Boolean)
310 with
311 Refined_Global =>
312 (In_Out =>
Nico Hubere015e822017-08-25 20:12:09 +0200313 (Config.Valid_Port_GPU, Dev.PCI_State,
Arthur Heymansd1988d12018-03-28 16:27:57 +0200314 Registers.Register_State, Port_IO.State,
315 Config.Raw_Clock),
Nico Huber83693c82016-10-08 22:17:55 +0200316 Input =>
317 (Time.State),
318 Output =>
Nico Huber2b6f6992017-07-09 18:11:34 +0200319 (Dev.Address_State,
320 Registers.Address_State,
Nico Huber83693c82016-10-08 22:17:55 +0200321 PLLs.State, Panel.Panel_State,
Nico Huber1a712d32017-01-09 15:11:04 +0100322 Cur_Configs, Allocated_PLLs,
Nico Huberc3f66f62017-07-16 21:39:54 +0200323 HPD_Delay, Wait_For_HPD,
324 Linear_FB_Base, Initialized))
Nico Huber83693c82016-10-08 22:17:55 +0200325 is
326 use type HW.Word64;
327
Nico Huber2b6f6992017-07-09 18:11:34 +0200328 PCI_MMIO_Base, PCI_GTT_Base : Word64;
329
Nico Huber83693c82016-10-08 22:17:55 +0200330 Now : constant Time.T := Time.Now;
331
332 procedure Check_Platform (Success : out Boolean)
333 is
334 Audio_VID_DID : Word32;
335 begin
336 case Config.CPU is
337 when Haswell .. Skylake =>
338 Registers.Read (Registers.AUD_VID_DID, Audio_VID_DID);
339 when Ironlake .. Ivybridge =>
340 Registers.Read (Registers.PCH_AUD_VID_DID, Audio_VID_DID);
341 end case;
342 Success :=
343 (case Config.CPU is
Nico Huber21da5742017-01-20 14:00:53 +0100344 when Broxton => Audio_VID_DID = 16#8086_280a#,
Nico Huber83693c82016-10-08 22:17:55 +0200345 when Skylake => Audio_VID_DID = 16#8086_2809#,
346 when Broadwell => Audio_VID_DID = 16#8086_2808#,
347 when Haswell => Audio_VID_DID = 16#8086_2807#,
348 when Ivybridge |
349 Sandybridge => Audio_VID_DID = 16#8086_2806# or
350 Audio_VID_DID = 16#8086_2805#,
Nico Hubereeb5a392016-10-09 19:28:30 +0200351 when Ironlake => Audio_VID_DID = 16#0000_0000#);
Nico Huber83693c82016-10-08 22:17:55 +0200352 end Check_Platform;
Nico Hubere7ac6eb2017-09-04 23:54:13 +0200353
354 procedure Check_Platform_PCI (Success : out Boolean)
355 is
356 use type HW.Word16;
357 Vendor, Device : Word16;
358 begin
359 Dev.Read16 (Vendor, PCI.Vendor_Id);
360 Dev.Read16 (Device, PCI.Device_Id);
361
362 Success := Vendor = 16#8086# and Config.Compatible_GPU (Device);
363 end Check_Platform_PCI;
Nico Huber83693c82016-10-08 22:17:55 +0200364 begin
Nico Huber83693c82016-10-08 22:17:55 +0200365 pragma Debug (Debug.Put_Line (GNAT.Source_Info.Enclosing_Entity));
366
367 pragma Debug (Debug.Set_Register_Write_Delay (Write_Delay));
368
Nico Huberc3f66f62017-07-16 21:39:54 +0200369 Linear_FB_Base := 0;
Nico Huber83693c82016-10-08 22:17:55 +0200370 Wait_For_HPD := HPD_Type'(others => False);
371 HPD_Delay := HPD_Delay_Type'(others => Now);
Nico Huber83693c82016-10-08 22:17:55 +0200372 Allocated_PLLs := (others => PLLs.Invalid);
Nico Huber99f10f32016-11-20 00:34:05 +0100373 Cur_Configs := Pipe_Configs'
374 (others => Pipe_Config'
Nico Huber83693c82016-10-08 22:17:55 +0200375 (Port => Disabled,
376 Framebuffer => HW.GFX.Default_FB,
377 Mode => HW.GFX.Invalid_Mode));
Nico Huber83693c82016-10-08 22:17:55 +0200378 PLLs.Initialize;
379
Nico Huber2b6f6992017-07-09 18:11:34 +0200380 Dev.Initialize (Success);
381
382 if Success then
383 Dev.Map (PCI_MMIO_Base, PCI.Res0, Length => Config.GTT_Offset);
384 Dev.Map (PCI_GTT_Base, PCI.Res0, Offset => Config.GTT_Offset);
385 if PCI_MMIO_Base /= 0 and PCI_GTT_Base /= 0 then
386 Registers.Set_Register_Base (PCI_MMIO_Base, PCI_GTT_Base);
387 else
388 pragma Debug (Debug.Put_Line
389 ("ERROR: Couldn't map resoure0."));
390 Registers.Set_Register_Base (Config.Default_MMIO_Base);
391 Success := Config.Default_MMIO_Base_Set;
392 end if;
Nico Hubere7ac6eb2017-09-04 23:54:13 +0200393
394 if Success then
395 Check_Platform_PCI (Success);
396 end if;
Nico Huber2b6f6992017-07-09 18:11:34 +0200397 else
398 pragma Debug (Debug.Put_Line
399 ("WARNING: Couldn't initialize PCI dev."));
400 Registers.Set_Register_Base (Config.Default_MMIO_Base);
401 Success := Config.Default_MMIO_Base_Set;
Nico Huber2b6f6992017-07-09 18:11:34 +0200402
Nico Hubere7ac6eb2017-09-04 23:54:13 +0200403 if Success then
404 Check_Platform (Success);
405 end if;
Nico Huber2b6f6992017-07-09 18:11:34 +0200406 end if;
407
Nico Huber83693c82016-10-08 22:17:55 +0200408 if not Success then
409 pragma Debug (Debug.Put_Line ("ERROR: Incompatible CPU or PCH."));
410
411 Panel.Static_Init; -- for flow analysis
412
413 Initialized := False;
414 return;
415 end if;
416
417 Panel.Setup_PP_Sequencer;
418 Port_Detect.Initialize;
Nico Huber0923b792017-06-09 15:28:41 +0200419 Connectors.Initialize;
Nico Huber83693c82016-10-08 22:17:55 +0200420
Nico Huber793a8d42016-11-21 18:57:03 +0100421 if Clean_State then
422 Power_And_Clocks.Pre_All_Off;
423 Connectors.Pre_All_Off;
424 Display_Controller.All_Off;
425 Connectors.Post_All_Off;
426 PLLs.All_Off;
427 Power_And_Clocks.Post_All_Off;
Nico Huber17d64b62017-07-15 20:51:25 +0200428 Registers.Clear_Fences;
Nico Huber33912aa2016-12-06 20:36:23 +0100429 else
430 -- According to PRMs, VGA plane is the only thing
Nico Huber3a0e2a02017-07-19 14:41:46 +0200431 -- that's enabled by default after reset...
Nico Huber33912aa2016-12-06 20:36:23 +0100432 Display_Controller.Legacy_VGA_Off;
Nico Huber3a0e2a02017-07-19 14:41:46 +0200433 -- ... along with some DDI port bits since Skylake.
434 Connectors.Post_Reset_Off;
Nico Huber793a8d42016-11-21 18:57:03 +0100435 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200436
437 -------------------- Now restart from a clean state ---------------------
438 Power_And_Clocks.Initialize;
439
Nico Huber1c3b9282017-02-09 13:57:04 +0100440 if Config.Has_PCH then
441 Registers.Unset_And_Set_Mask
442 (Register => Registers.PCH_RAWCLK_FREQ,
443 Mask_Unset => PCH_RAWCLK_FREQ_MASK,
444 Mask_Set => PCH_RAWCLK_FREQ (Config.Default_RawClk_Freq));
445 end if;
Nico Huberf54d0962016-10-20 14:17:18 +0200446
Nico Huber83693c82016-10-08 22:17:55 +0200447 Initialized := True;
448
449 end Initialize;
450
451 function Is_Initialized return Boolean
452 with
453 Refined_Post => Is_Initialized'Result = Initialized
454 is
455 begin
456 return Initialized;
457 end Is_Initialized;
458
459 ----------------------------------------------------------------------------
460
Nico Huber42fb2d02017-09-01 17:01:51 +0200461 procedure Power_Up_VGA
462 is
463 Fake_Config : constant Pipe_Configs :=
464 (Primary =>
465 (Port => Analog,
466 Framebuffer => HW.GFX.Default_FB,
467 Mode => HW.GFX.Invalid_Mode),
468 others =>
469 (Port => Disabled,
470 Framebuffer => HW.GFX.Default_FB,
471 Mode => HW.GFX.Invalid_Mode));
472 begin
473 Power_And_Clocks.Power_Up (Cur_Configs, Fake_Config);
474 end Power_Up_VGA;
475
476 ----------------------------------------------------------------------------
477
Nico Huber5374c3a2017-07-15 21:48:06 +0200478 function FB_First_Page (FB : Framebuffer_Type) return Natural is
Nico Huber34be6542017-12-13 09:26:24 +0100479 (Natural (Phys_Offset (FB) / GTT_Page_Size));
Nico Huber5374c3a2017-07-15 21:48:06 +0200480 function FB_Pages (FB : Framebuffer_Type) return Natural is
481 (Natural (Div_Round_Up (FB_Size (FB), GTT_Page_Size)));
482 function FB_Last_Page (FB : Framebuffer_Type) return Natural is
483 (FB_First_Page (FB) + FB_Pages (FB) - 1);
484
Nico Huber34be6542017-12-13 09:26:24 +0100485 -- Check basics and that it fits in GTT. For 90 degree rotations,
486 -- the Offset should be above GTT_Rotation_Offset. The latter will
487 -- be subtracted for the aperture mapping.
Nico Huber5374c3a2017-07-15 21:48:06 +0200488 function Valid_FB (FB : Framebuffer_Type) return Boolean is
Nico Huber34be6542017-12-13 09:26:24 +0100489 (Valid_Stride (FB) and
490 FB_First_Page (FB) in GTT_Range and
491 FB_Last_Page (FB) in GTT_Range and
492 (not Rotation_90 (FB) or
493 (FB_Last_Page (FB) + GTT_Rotation_Offset in GTT_Range and
494 FB.Offset >= Word32 (GTT_Rotation_Offset) * GTT_Page_Size)));
Nico Huber5374c3a2017-07-15 21:48:06 +0200495
496 -- Also check that we don't overflow the GTT's 39-bit space
497 -- (always true with a 32-bit base)
498 function Valid_Phys_FB (FB : Framebuffer_Type; Phys_Base : Word32)
499 return Boolean is
500 (Valid_FB (FB) and
Nico Huber34be6542017-12-13 09:26:24 +0100501 Int64 (Phys_Base) + Int64 (Phys_Offset (FB)) + Int64 (FB_Size (FB)) <=
Nico Huber5374c3a2017-07-15 21:48:06 +0200502 Int64 (GTT_Address_Type'Last))
503 with
504 Ghost;
505
Nico Huber83693c82016-10-08 22:17:55 +0200506 procedure Write_GTT
507 (GTT_Page : GTT_Range;
508 Device_Address : GTT_Address_Type;
Nico Huber5374c3a2017-07-15 21:48:06 +0200509 Valid : Boolean)
510 is
Nico Huber83693c82016-10-08 22:17:55 +0200511 begin
512 Registers.Write_GTT (GTT_Page, Device_Address, Valid);
513 end Write_GTT;
514
Nico Huber194e57e2017-07-15 21:15:46 +0200515 procedure Setup_Default_GTT (FB : Framebuffer_Type; Phys_Base : Word32)
Nico Huber5374c3a2017-07-15 21:48:06 +0200516 with
517 Pre => Is_Initialized and Valid_Phys_FB (FB, Phys_Base)
Nico Huber83693c82016-10-08 22:17:55 +0200518 is
Nico Huber194e57e2017-07-15 21:15:46 +0200519 Phys_Addr : GTT_Address_Type :=
Nico Huber34be6542017-12-13 09:26:24 +0100520 GTT_Address_Type (Phys_Base) + GTT_Address_Type (Phys_Offset (FB));
Nico Huber83693c82016-10-08 22:17:55 +0200521 begin
Nico Huber194e57e2017-07-15 21:15:46 +0200522 for Idx in FB_First_Page (FB) .. FB_Last_Page (FB) loop
Nico Huber83693c82016-10-08 22:17:55 +0200523 Registers.Write_GTT
524 (GTT_Page => Idx,
525 Device_Address => Phys_Addr,
526 Valid => True);
Nico Huber194e57e2017-07-15 21:15:46 +0200527 Phys_Addr := Phys_Addr + GTT_Page_Size;
Nico Huber83693c82016-10-08 22:17:55 +0200528 end loop;
Nico Huber9b479412017-08-27 11:55:56 +0200529
530 if Rotation_90 (FB) and FB.Tiling = Y_Tiled and FB.V_Stride >= 32 then
531 declare
532 V_Pages : constant Natural := Natural (FB.V_Stride) / 32;
533 Bytes_Per_Row : constant GTT_Address_Type :=
534 GTT_Address_Type (Pixel_To_Bytes (32 * FB.Stride, FB));
535 begin
536 Phys_Addr := GTT_Address_Type (Phys_Base) +
Nico Huber34be6542017-12-13 09:26:24 +0100537 GTT_Address_Type (Phys_Offset (FB)) +
Nico Huber9b479412017-08-27 11:55:56 +0200538 GTT_Address_Type (FB_Size (FB));
539 for Page in FB_First_Page (FB) .. FB_Last_Page (FB) loop
540 Phys_Addr := Phys_Addr - Bytes_Per_Row;
541 Registers.Write_GTT
542 (GTT_Page => GTT_Rotation_Offset + Page,
543 Device_Address => Phys_Addr,
544 Valid => True);
545
546 if (Page - FB_First_Page (FB) + 1) mod V_Pages = 0 then
547 Phys_Addr := Phys_Addr + GTT_Page_Size +
548 GTT_Address_Type (V_Pages) * Bytes_Per_Row;
549 end if;
550 end loop;
551 end;
552 end if;
Nico Huber83693c82016-10-08 22:17:55 +0200553 end Setup_Default_GTT;
554
555 ----------------------------------------------------------------------------
556
Nico Hubereedde882017-07-16 02:54:39 +0200557 use type HW.Word16;
558 subtype Stolen_Size_Range is Int64 range 0 .. 2 ** 33;
559
560 function GGMS_Gen4 (GGC : Word16) return Natural is
561 (Natural (Shift_Right (GGC, 8) and 16#07#));
562 function GTT_Size_Gen4 (GGC : Word16) return Natural is
563 (if GGMS_Gen4 (GGC) in 1 .. 3 then
564 (GGMS_Gen4 (GGC) + 1) * 2 ** 19 else 0);
565
566 function GMS_Gen4 (GGC : Word16) return Natural is
567 (Natural (Shift_Right (GGC, 4) and 16#0f#));
568 Valid_Stolen_Size_Gen4 : constant
569 array (Natural range 1 .. 13) of Stolen_Size_Range :=
570 (1, 4, 8, 16, 32, 48, 64, 128, 256, 96, 160, 224, 352);
571 function Stolen_Size_Gen4 (GGC : Word16) return Stolen_Size_Range is
572 (if GMS_Gen4 (GGC) in Valid_Stolen_Size_Gen4'Range then
Arthur Heymans5fd9a312017-09-12 12:45:18 +0200573 Valid_Stolen_Size_Gen4 (GMS_Gen4 (GGC)) * 2 ** 20 else 0);
Nico Hubereedde882017-07-16 02:54:39 +0200574
575 function GTT_Size_Gen6 (GGC : Word16) return Natural is
576 (Natural (Shift_Right (GGC, 8) and 16#03#) * 2 ** 20);
577
578 function Stolen_Size_Gen6 (GGC : Word16) return Stolen_Size_Range is
579 (Stolen_Size_Range (Shift_Right (GGC, 3) and 16#1f#) * 32 * 2 ** 20);
580
581 function GTT_Size_Gen8 (GGC : Word16) return Natural is
582 (Natural (Shift_Right (GGC, 6) and 16#03#) * 2 ** 20);
583
584 function GMS_Gen8 (GGC : Word16) return Stolen_Size_Range is
585 (Stolen_Size_Range (Shift_Right (GGC, 8) and 16#ff#));
586 function Stolen_Size_Gen8 (GGC : Word16) return Stolen_Size_Range is
587 (GMS_Gen8 (GGC) * 32 * 2 ** 20);
588
589 function Stolen_Size_Gen9 (GGC : Word16) return Stolen_Size_Range is
590 (if GMS_Gen8 (GGC) < 16#f0# then
591 Stolen_Size_Gen8 (GGC)
592 else
593 (GMS_Gen8 (GGC) - 16#f0# + 1) * 4 * 2 ** 20);
594
595 procedure Decode_Stolen
596 (GTT_Size : out Natural;
597 Stolen_Size : out Stolen_Size_Range)
598 with
599 Pre => Is_Initialized
600 is
601 GGC_Reg : constant :=
602 (case Config.CPU is
603 when Ironlake => 16#52#,
604 when Sandybridge .. Skylake => 16#50#);
605 GGC : Word16;
606 begin
607 Dev.Read16 (GGC, GGC_Reg);
608 case Config.CPU is
609 when Ironlake =>
610 GTT_Size := GTT_Size_Gen4 (GGC);
611 Stolen_Size := Stolen_Size_Gen4 (GGC);
612 when Sandybridge .. Haswell =>
613 GTT_Size := GTT_Size_Gen6 (GGC);
614 Stolen_Size := Stolen_Size_Gen6 (GGC);
615 when Broadwell =>
616 GTT_Size := GTT_Size_Gen8 (GGC);
617 Stolen_Size := Stolen_Size_Gen8 (GGC);
618 when Broxton .. Skylake =>
619 GTT_Size := GTT_Size_Gen8 (GGC);
620 Stolen_Size := Stolen_Size_Gen9 (GGC);
621 end case;
622 end Decode_Stolen;
623
624 -- Additional runtime validation that FB fits stolen memory and aperture.
625 procedure Validate_FB (FB : Framebuffer_Type; Valid : out Boolean)
626 with
627 Pre => Is_Initialized,
628 Post => (if Valid then Valid_FB (FB))
629 is
630 GTT_Size, Aperture_Size : Natural;
631 Stolen_Size : Stolen_Size_Range;
632 begin
633 Valid := Valid_FB (FB);
634
635 if Valid then
636 Decode_Stolen (GTT_Size, Stolen_Size);
637 Dev.Resource_Size (Aperture_Size, PCI.Res2);
638 Valid :=
639 FB_Last_Page (FB) < GTT_Size / Config.GTT_PTE_Size and
640 FB_Last_Page (FB) < Natural (Stolen_Size / GTT_Page_Size) and
641 FB_Last_Page (FB) < Aperture_Size / GTT_Page_Size;
Nico Huber34be6542017-12-13 09:26:24 +0100642 pragma Debug (not Valid, Debug.Put_Line
Nico Hubereedde882017-07-16 02:54:39 +0200643 ("Stolen memory too small to hold framebuffer."));
644 end if;
645 end Validate_FB;
646
Nico Huber5374c3a2017-07-15 21:48:06 +0200647 procedure Setup_Default_FB
648 (FB : in Framebuffer_Type;
649 Clear : in Boolean := True;
650 Success : out Boolean)
651 is
652 GMA_Phys_Base : constant PCI.Index := 16#5c#;
653 GMA_Phys_Base_Mask : constant := 16#fff0_0000#;
654
655 Phys_Base : Word32;
656 begin
Nico Hubereedde882017-07-16 02:54:39 +0200657 Validate_FB (FB, Success);
Nico Huber5374c3a2017-07-15 21:48:06 +0200658
659 if Success then
660 Dev.Read32 (Phys_Base, GMA_Phys_Base);
661 Phys_Base := Phys_Base and GMA_Phys_Base_Mask;
662 Success := Phys_Base /= GMA_Phys_Base_Mask and Phys_Base /= 0;
663 pragma Debug (not Success, Debug.Put_Line
664 ("Failed to read stolen memory base."));
Nico Huber0164b022017-08-24 15:12:51 +0200665
666 if Success then
667 if FB.Tiling in XY_Tiling then
668 Registers.Add_Fence
669 (First_Page => FB_First_Page (FB),
670 Last_Page => FB_Last_Page (FB),
671 Tiling => FB.Tiling,
672 Pitch => FB_Pitch (FB.Stride, FB),
673 Success => Success);
674 end if;
675 pragma Debug (not Success, Debug.Put_Line
676 ("Tiled framebuffer but no fence regs available."));
677 end if;
678
Nico Huber5374c3a2017-07-15 21:48:06 +0200679 if Success then
680 Setup_Default_GTT (FB, Phys_Base);
681 end if;
682 end if;
683
684 if Success and then Clear then
685 declare
686 use type HW.Word64;
687 Linear_FB : Word64;
688 begin
Nico Huberc3f66f62017-07-16 21:39:54 +0200689 Map_Linear_FB (Linear_FB, FB);
Nico Huber5374c3a2017-07-15 21:48:06 +0200690 if Linear_FB /= 0 then
Nico Huberc3f66f62017-07-16 21:39:54 +0200691 Framebuffer_Filler.Fill (Linear_FB, FB);
Nico Huber5374c3a2017-07-15 21:48:06 +0200692 end if;
Nico Huber5374c3a2017-07-15 21:48:06 +0200693 end;
694 end if;
695 end Setup_Default_FB;
696
Nico Huberc3f66f62017-07-16 21:39:54 +0200697 procedure Map_Linear_FB (Linear_FB : out Word64; FB : in Framebuffer_Type)
698 is
699 use type HW.Word64;
700
701 Valid : Boolean;
702 begin
703 Linear_FB := 0;
704
705 if Linear_FB_Base = 0 then
706 Dev.Map (Linear_FB_Base, PCI.Res2);
707 pragma Debug
708 (Linear_FB_Base = 0, Debug.Put_Line ("Failed to map resource2."));
709 end if;
710
711 if Linear_FB_Base /= 0 then
712 Validate_FB (FB, Valid);
713 if Valid then
Nico Huber34be6542017-12-13 09:26:24 +0100714 Linear_FB := Linear_FB_Base + Word64 (Phys_Offset (FB));
Nico Huberc3f66f62017-07-16 21:39:54 +0200715 end if;
716 end if;
717 end Map_Linear_FB;
718
Nico Huber5374c3a2017-07-15 21:48:06 +0200719 ----------------------------------------------------------------------------
720
Nico Huber99f10f32016-11-20 00:34:05 +0100721 procedure Dump_Configs (Configs : Pipe_Configs)
Nico Huber83693c82016-10-08 22:17:55 +0200722 is
723 subtype Pipe_Name is String (1 .. 9);
Nico Huber99f10f32016-11-20 00:34:05 +0100724 type Pipe_Name_Array is array (Pipe_Index) of Pipe_Name;
Nico Huber83693c82016-10-08 22:17:55 +0200725 Pipe_Names : constant Pipe_Name_Array :=
726 (Primary => "Primary ",
727 Secondary => "Secondary",
728 Tertiary => "Tertiary ");
Nico Huber5ef4d602017-12-13 13:56:47 +0100729
730 subtype Tiling_Name is String (1 .. 7);
731 type Tiling_Name_Array is array (Tiling_Type) of Tiling_Name;
732 Tilings : constant Tiling_Name_Array :=
733 (Linear => "Linear ",
734 X_Tiled => "X_Tiled",
735 Y_Tiled => "Y_Tiled");
736
737 subtype Rotation_Name is String (1 .. 11);
738 type Rotation_Name_Array is array (Rotation_Type) of Rotation_Name;
739 Rotations : constant Rotation_Name_Array :=
740 (No_Rotation => "No_Rotation",
741 Rotated_90 => "Rotated_90 ",
742 Rotated_180 => "Rotated_180",
743 Rotated_270 => "Rotated_270");
Nico Huber83693c82016-10-08 22:17:55 +0200744 begin
745 Debug.New_Line;
Paul Menzelb83107c2017-05-04 09:02:33 +0200746 Debug.Put_Line ("CONFIG =>");
Nico Huber99f10f32016-11-20 00:34:05 +0100747 for Pipe in Pipe_Index loop
748 if Pipe = Pipe_Index'First then
Nico Huber83693c82016-10-08 22:17:55 +0200749 Debug.Put (" (");
750 else
751 Debug.Put (" ");
752 end if;
753 Debug.Put_Line (Pipe_Names (Pipe) & " =>");
754 Debug.Put_Line
755 (" (Port => " & Port_Names (Configs (Pipe).Port) & ",");
756 Debug.Put_Line (" Framebuffer =>");
Nico Huber5ef4d602017-12-13 13:56:47 +0100757 Debug.Put (" (Width => ");
Nico Huber83693c82016-10-08 22:17:55 +0200758 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Width);
759 Debug.Put_Line (",");
Nico Huber5ef4d602017-12-13 13:56:47 +0100760 Debug.Put (" Height => ");
Nico Huber83693c82016-10-08 22:17:55 +0200761 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Height);
762 Debug.Put_Line (",");
Nico Huber5ef4d602017-12-13 13:56:47 +0100763 Debug.Put (" Start_X => ");
764 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Start_X);
765 Debug.Put_Line (",");
766 Debug.Put (" Start_Y => ");
767 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Start_Y);
768 Debug.Put_Line (",");
769 Debug.Put (" Stride => ");
Nico Huber83693c82016-10-08 22:17:55 +0200770 Debug.Put_Int32 (Configs (Pipe).Framebuffer.Stride);
771 Debug.Put_Line (",");
Nico Huber5ef4d602017-12-13 13:56:47 +0100772 Debug.Put (" V_Stride => ");
773 Debug.Put_Int32 (Configs (Pipe).Framebuffer.V_Stride);
774 Debug.Put_Line (",");
775 Debug.Put (" Tiling => ");
776 Debug.Put_Line (Tilings (Configs (Pipe).Framebuffer.Tiling) & ",");
777 Debug.Put (" Rotation => ");
778 Debug.Put_Line (Rotations (Configs (Pipe).Framebuffer.Rotation) & ",");
Nico Huber83693c82016-10-08 22:17:55 +0200779 Debug.Put (" Offset => ");
780 Debug.Put_Word32 (Configs (Pipe).Framebuffer.Offset);
781 Debug.Put_Line (",");
782 Debug.Put (" BPC => ");
783 Debug.Put_Int64 (Configs (Pipe).Framebuffer.BPC);
784 Debug.Put_Line ("),");
785 Debug.Put_Line (" Mode =>");
786 Debug.Put (" (Dotclock => ");
787 Debug.Put_Int64 (Configs (Pipe).Mode.Dotclock);
788 Debug.Put_Line (",");
789 Debug.Put (" H_Visible => ");
790 Debug.Put_Int16 (Configs (Pipe).Mode.H_Visible);
791 Debug.Put_Line (",");
792 Debug.Put (" H_Sync_Begin => ");
793 Debug.Put_Int16 (Configs (Pipe).Mode.H_Sync_Begin);
794 Debug.Put_Line (",");
795 Debug.Put (" H_Sync_End => ");
796 Debug.Put_Int16 (Configs (Pipe).Mode.H_Sync_End);
797 Debug.Put_Line (",");
798 Debug.Put (" H_Total => ");
799 Debug.Put_Int16 (Configs (Pipe).Mode.H_Total);
800 Debug.Put_Line (",");
801 Debug.Put (" V_Visible => ");
802 Debug.Put_Int16 (Configs (Pipe).Mode.V_Visible);
803 Debug.Put_Line (",");
804 Debug.Put (" V_Sync_Begin => ");
805 Debug.Put_Int16 (Configs (Pipe).Mode.V_Sync_Begin);
806 Debug.Put_Line (",");
807 Debug.Put (" V_Sync_End => ");
808 Debug.Put_Int16 (Configs (Pipe).Mode.V_Sync_End);
809 Debug.Put_Line (",");
810 Debug.Put (" V_Total => ");
811 Debug.Put_Int16 (Configs (Pipe).Mode.V_Total);
812 Debug.Put_Line (",");
813 Debug.Put_Line (" H_Sync_Active_High => " &
814 (if Configs (Pipe).Mode.H_Sync_Active_High
815 then "True,"
816 else "False,"));
817 Debug.Put_Line (" V_Sync_Active_High => " &
818 (if Configs (Pipe).Mode.V_Sync_Active_High
819 then "True,"
820 else "False,"));
821 Debug.Put (" BPC => ");
822 Debug.Put_Int64 (Configs (Pipe).Mode.BPC);
Nico Huber99f10f32016-11-20 00:34:05 +0100823 if Pipe /= Pipe_Index'Last then
Nico Huber83693c82016-10-08 22:17:55 +0200824 Debug.Put_Line (")),");
825 else
826 Debug.Put_Line (")));");
827 end if;
828 end loop;
829 end Dump_Configs;
830
831end HW.GFX.GMA;