blob: c05f4f16bbb38c5b79427282cfb69ccc823b0ffe [file] [log] [blame]
Nico Huber83693c82016-10-08 22:17:55 +02001--
Nico Huber3d06de82018-05-29 01:35:04 +02002-- Copyright (C) 2015-2018 secunet Security Networks AG
Nico Huber194e57e2017-07-15 21:15:46 +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 Huber194e57e2017-07-15 21:15:46 +020016with HW.Config;
Nico Huber83693c82016-10-08 22:17:55 +020017with HW.Time;
Nico Huberc5c66ec2019-09-28 23:59:45 +020018with HW.PCI;
Nico Huber83693c82016-10-08 22:17:55 +020019with HW.Port_IO;
Nico Hubercf88f3d2018-06-05 13:27:34 +020020with HW.GFX.Framebuffer_Filler;
Nico Huber83693c82016-10-08 22:17:55 +020021
Elyes Haouas066e52e2022-09-26 12:48:47 +020022use type HW.Word32;
23
Nico Huber83693c82016-10-08 22:17:55 +020024package HW.GFX.GMA
25with
26 Abstract_State =>
27 (State,
28 Init_State,
Nico Huber83693c82016-10-08 22:17:55 +020029 (Device_State with External)),
Nico Huber27088aa2018-06-10 13:28:05 +020030 Initializes => Init_State
Nico Huber83693c82016-10-08 22:17:55 +020031is
32
Nico Hubera02b2c62018-01-09 15:58:34 +010033 GTT_Page_Size : constant := 4096;
34 type GTT_Address_Type is mod 2 ** 39;
35 subtype GTT_Range is Natural range 0 .. 16#8_0000# - 1;
36 GTT_Rotation_Offset : constant GTT_Range := GTT_Range'Last / 2 + 1;
37
Arthur Heymans960e2392026-03-03 19:45:24 +010038 type Generation is (I945, G45, Ironlake, Haswell, Broxton, Skylake, Tigerlake);
Nico Huber6621a142018-06-07 23:56:54 +020039
Nico Huber83693c82016-10-08 22:17:55 +020040 type CPU_Type is
Arthur Heymans960e2392026-03-03 19:45:24 +010041 (I945G,
42 I945GM,
43 G45,
Nico Huber7f3e2802019-09-28 20:40:55 +020044 GM45,
Arthur Heymans73ea0322018-03-28 17:17:07 +020045 Ironlake,
Nico Huber83693c82016-10-08 22:17:55 +020046 Sandybridge,
47 Ivybridge,
48 Haswell,
49 Broadwell,
Nico Huber21da5742017-01-20 14:00:53 +010050 Broxton,
Nico Huber88badbe2018-09-27 16:36:47 +020051 Skylake,
Tim Wawrzynczak605660b2022-06-08 12:48:19 -060052 Kabylake,
Tim Wawrzynczake9631d82022-09-09 12:26:32 -060053 Tigerlake,
54 Alderlake);
Nico Huber83693c82016-10-08 22:17:55 +020055
Nico Huber25fdb152019-02-17 15:54:39 +010056 type CPU_Variant is (Normal, ULT, ULX);
Nico Huber83693c82016-10-08 22:17:55 +020057
Nico Hubere79babd2020-12-20 01:33:26 +010058 type PCH_Type is
59 (No_PCH,
60 Ibex_Peak,
61 Cougar_Point, -- Panther Point compatible
62 Lynx_Point, -- Wildcat Point compatible
Nico Huberdde06302020-12-20 02:18:30 +010063 Sunrise_Point, -- Union Point compatible
Tim Wawrzynczak605660b2022-06-08 12:48:19 -060064 Cannon_Point,
Tim Wawrzynczake9631d82022-09-09 12:26:32 -060065 Tiger_Point,
66 Alder_Point);
Nico Hubere79babd2020-12-20 01:33:26 +010067
Nico Huber83693c82016-10-08 22:17:55 +020068 type Port_Type is
69 (Disabled,
Nico Huber8beafd72020-01-07 14:59:44 +010070 LVDS,
71 eDP,
Nico Huber83693c82016-10-08 22:17:55 +020072 DP1,
73 DP2,
74 DP3,
Nico Huber0d454cd2016-11-21 13:33:43 +010075 HDMI1, -- or DVI
76 HDMI2, -- or DVI
77 HDMI3, -- or DVI
Tim Wawrzynczak605660b2022-06-08 12:48:19 -060078 Analog,
79 USBC1_DP,
80 USBC2_DP,
81 USBC3_DP,
82 USBC4_DP,
83 USBC5_DP,
84 USBC6_DP,
85 USBC1_HDMI,
86 USBC2_HDMI,
87 USBC3_HDMI,
88 USBC4_HDMI,
89 USBC5_HDMI,
90 USBC6_HDMI);
Nico Huber040d9b62019-02-18 00:52:00 +010091 subtype Active_Port_Type is Port_Type
92 range Port_Type'Succ (Disabled) .. Port_Type'Last;
Nico Huber8beafd72020-01-07 14:59:44 +010093 subtype Internal_Port_Type is Port_Type range LVDS .. eDP;
Tim Wawrzynczak605660b2022-06-08 12:48:19 -060094 subtype Combo_Port_Type is Port_Type range DP1 .. HDMI3;
95 subtype USBC_Port_Type is Port_Type range USBC1_DP .. USBC6_HDMI;
Nico Huber83693c82016-10-08 22:17:55 +020096
Nico Hubera02b2c62018-01-09 15:58:34 +010097 type Cursor_Mode is (No_Cursor, ARGB_Cursor);
98 type Cursor_Size is (Cursor_64x64, Cursor_128x128, Cursor_256x256);
99 Cursor_Width : constant array (Cursor_Size) of Width_Type := (64, 128, 256);
100
101 subtype Cursor_Pos is Int32 range Int32'First / 2 .. Int32'Last / 2;
102
103 type Cursor_Type is record
104 Mode : Cursor_Mode;
105 Size : Cursor_Size;
106 Center_X : Cursor_Pos;
107 Center_Y : Cursor_Pos;
108 GTT_Offset : GTT_Range;
109 end record;
110 Default_Cursor : constant Cursor_Type :=
111 (Mode => No_Cursor,
112 Size => Cursor_Size'First,
113 Center_X => 0,
114 Center_Y => 0,
115 GTT_Offset => 0);
116
Nico Huber99f10f32016-11-20 00:34:05 +0100117 type Pipe_Config is record
Nico Huber83693c82016-10-08 22:17:55 +0200118 Port : Port_Type;
119 Framebuffer : Framebuffer_Type;
Nico Hubera02b2c62018-01-09 15:58:34 +0100120 Cursor : Cursor_Type;
Nico Huber83693c82016-10-08 22:17:55 +0200121 Mode : Mode_Type;
122 end record;
Nico Huber99f10f32016-11-20 00:34:05 +0100123 type Pipe_Index is (Primary, Secondary, Tertiary);
124 type Pipe_Configs is array (Pipe_Index) of Pipe_Config;
Nico Huber83693c82016-10-08 22:17:55 +0200125
Nico Huber3675db52016-11-04 16:27:29 +0100126 -- Special framebuffer offset to indicate legacy VGA plane.
127 -- Only valid on primary pipe.
128 VGA_PLANE_FRAMEBUFFER_OFFSET : constant := 16#ffff_ffff#;
129
Nico Huberbebca132017-06-12 23:04:46 +0200130 pragma Warnings (GNATprove, Off, "unused variable ""Write_Delay""",
131 Reason => "Write_Delay is used for debugging only");
Nico Huber83693c82016-10-08 22:17:55 +0200132 procedure Initialize
Nico Huber2b6f6992017-07-09 18:11:34 +0200133 (Write_Delay : in Word64 := 0;
Nico Huber793a8d42016-11-21 18:57:03 +0100134 Clean_State : in Boolean := False;
Nico Huber83693c82016-10-08 22:17:55 +0200135 Success : out Boolean)
136 with
137 Global =>
Nico Huber27088aa2018-06-10 13:28:05 +0200138 (In_Out => (Device_State, Port_IO.State),
Nico Hubere317e9c2019-09-29 03:03:18 +0200139 Output => (State, Init_State),
Nico Huber83693c82016-10-08 22:17:55 +0200140 Input => (Time.State)),
141 Post => Success = Is_Initialized;
142 function Is_Initialized return Boolean
143 with
144 Global => (Input => Init_State);
Nico Huberbebca132017-06-12 23:04:46 +0200145 pragma Warnings (GNATprove, On, "unused variable ""Write_Delay""");
Nico Huber83693c82016-10-08 22:17:55 +0200146
Nico Huber42fb2d02017-09-01 17:01:51 +0200147 procedure Power_Up_VGA
148 with
Nico Hubercf88f3d2018-06-05 13:27:34 +0200149 Global =>
Nico Hubere317e9c2019-09-29 03:03:18 +0200150 (Input => (State, Time.State),
Nico Hubercf88f3d2018-06-05 13:27:34 +0200151 In_Out => (Device_State),
152 Proof_In => (Init_State)),
Nico Huber42fb2d02017-09-01 17:01:51 +0200153 Pre => Is_Initialized;
154
Nico Hubercf88f3d2018-06-05 13:27:34 +0200155 ----------------------------------------------------------------------------
Nico Huber83693c82016-10-08 22:17:55 +0200156
Nico Hubercf88f3d2018-06-05 13:27:34 +0200157 procedure Update_Outputs (Configs : Pipe_Configs)
158 with
159 Global =>
Nico Hubere317e9c2019-09-29 03:03:18 +0200160 (Input => (Time.State),
Nico Hubercf88f3d2018-06-05 13:27:34 +0200161 In_Out => (State, Device_State, Port_IO.State),
162 Proof_In => (Init_State)),
163 Pre => Is_Initialized;
164
165 procedure Update_Cursor (Pipe : Pipe_Index; Cursor : Cursor_Type)
166 with
167 Global =>
Nico Hubere317e9c2019-09-29 03:03:18 +0200168 (In_Out => (State, Device_State),
Nico Hubercf88f3d2018-06-05 13:27:34 +0200169 Proof_In => (Init_State)),
Nico Hubere317e9c2019-09-29 03:03:18 +0200170 Pre => Is_Initialized;
Nico Hubercf88f3d2018-06-05 13:27:34 +0200171
Nico Huber15ffc4f2018-01-11 14:44:43 +0100172 procedure Place_Cursor
173 (Pipe : Pipe_Index;
174 X : Cursor_Pos;
Nico Hubercf88f3d2018-06-05 13:27:34 +0200175 Y : Cursor_Pos)
176 with
177 Global =>
Nico Hubere317e9c2019-09-29 03:03:18 +0200178 (In_Out => (State, Device_State),
Nico Hubercf88f3d2018-06-05 13:27:34 +0200179 Proof_In => (Init_State)),
Nico Hubere317e9c2019-09-29 03:03:18 +0200180 Pre => Is_Initialized;
Nico Hubercf88f3d2018-06-05 13:27:34 +0200181
Nico Huber15ffc4f2018-01-11 14:44:43 +0100182 procedure Move_Cursor
183 (Pipe : Pipe_Index;
184 X : Cursor_Pos;
Nico Hubercf88f3d2018-06-05 13:27:34 +0200185 Y : Cursor_Pos)
186 with
187 Global =>
Nico Hubere317e9c2019-09-29 03:03:18 +0200188 (In_Out => (State, Device_State),
Nico Hubercf88f3d2018-06-05 13:27:34 +0200189 Proof_In => (Init_State)),
Nico Hubere317e9c2019-09-29 03:03:18 +0200190 Pre => Is_Initialized;
Nico Huber15ffc4f2018-01-11 14:44:43 +0100191
Nico Hubercf88f3d2018-06-05 13:27:34 +0200192 ----------------------------------------------------------------------------
Nico Huber83693c82016-10-08 22:17:55 +0200193
Nico Huberbc0588e2020-07-21 12:17:19 +0200194 procedure Backlight_On (Port : Active_Port_Type)
195 with
196 Global => (In_Out => Device_State);
197
198 procedure Backlight_Off (Port : Active_Port_Type)
199 with
200 Global => (In_Out => Device_State);
201
202 procedure Set_Brightness (Port : Active_Port_Type; Level : Word32)
203 with
204 Global => (In_Out => Device_State);
205
206 procedure Get_Max_Brightness (Port : Active_Port_Type; Level : out Word32)
207 with
208 Global => (In_Out => Device_State);
209
210 ----------------------------------------------------------------------------
211
Nico Huber83693c82016-10-08 22:17:55 +0200212 procedure Write_GTT
213 (GTT_Page : GTT_Range;
214 Device_Address : GTT_Address_Type;
Nico Hubercf88f3d2018-06-05 13:27:34 +0200215 Valid : Boolean)
216 with
Nico Huberadfe11f2018-06-10 14:59:04 +0200217 Global =>
Nico Hubere317e9c2019-09-29 03:03:18 +0200218 (Input => State,
Nico Huberadfe11f2018-06-10 14:59:04 +0200219 In_Out => Device_State, Proof_In => Init_State),
Nico Hubercf88f3d2018-06-05 13:27:34 +0200220 Pre => Is_Initialized;
Nico Huber83693c82016-10-08 22:17:55 +0200221
Nico Huberceda17d2018-06-09 22:00:29 +0200222 procedure Read_GTT
223 (Device_Address : out GTT_Address_Type;
224 Valid : out Boolean;
225 GTT_Page : in GTT_Range)
226 with
Nico Huberadfe11f2018-06-10 14:59:04 +0200227 Global =>
Nico Hubere317e9c2019-09-29 03:03:18 +0200228 (Input => State,
Nico Huberadfe11f2018-06-10 14:59:04 +0200229 In_Out => Device_State, Proof_In => Init_State),
Nico Huberceda17d2018-06-09 22:00:29 +0200230 Pre => Is_Initialized;
231
Arthur Heymans960e2392026-03-03 19:45:24 +0100232 procedure GTT_Entry_Count (Count : out Natural)
233 with
234 Global =>
235 (Input => State,
236 In_Out => Device_State, Proof_In => Init_State),
237 Pre => Is_Initialized and HW.Config.Dynamic_MMIO;
238
Nico Huber5374c3a2017-07-15 21:48:06 +0200239 procedure Setup_Default_FB
240 (FB : in Framebuffer_Type;
241 Clear : in Boolean := True;
242 Success : out Boolean)
Nico Huber194e57e2017-07-15 21:15:46 +0200243 with
Nico Hubercf88f3d2018-06-05 13:27:34 +0200244 Global =>
Nico Hubere317e9c2019-09-29 03:03:18 +0200245 (In_Out =>
Nico Hubercf88f3d2018-06-05 13:27:34 +0200246 (State, Device_State,
247 Framebuffer_Filler.State, Framebuffer_Filler.Base_Address),
248 Proof_In => (Init_State)),
Nico Huber5374c3a2017-07-15 21:48:06 +0200249 Pre => Is_Initialized and HW.Config.Dynamic_MMIO;
Nico Huber83693c82016-10-08 22:17:55 +0200250
Nico Huberc3f66f62017-07-16 21:39:54 +0200251 procedure Map_Linear_FB (Linear_FB : out Word64; FB : in Framebuffer_Type)
252 with
Nico Hubercf88f3d2018-06-05 13:27:34 +0200253 Global =>
Nico Hubere317e9c2019-09-29 03:03:18 +0200254 (In_Out => (State, Device_State),
Nico Hubercf88f3d2018-06-05 13:27:34 +0200255 Proof_In => (Init_State)),
Nico Hubere317e9c2019-09-29 03:03:18 +0200256 Pre => Is_Initialized and HW.Config.Dynamic_MMIO;
Nico Huberc3f66f62017-07-16 21:39:54 +0200257
Nico Hubercf88f3d2018-06-05 13:27:34 +0200258 ----------------------------------------------------------------------------
259
260 pragma Warnings (GNATprove, Off, "subprogram ""Dump_Configs"" has no effect",
261 Reason => "It's only used for debugging");
262 procedure Dump_Configs (Configs : Pipe_Configs);
263
Nico Huber83693c82016-10-08 22:17:55 +0200264private
265
Nico Huberc5c66ec2019-09-28 23:59:45 +0200266 PCI_Usable : Boolean with Part_Of => State;
267 use type HW.PCI.Index;
268 procedure PCI_Read16 (Value : out Word16; Offset : HW.PCI.Index)
269 with
270 Pre => PCI_Usable and Offset mod 2 = 0;
271
272 ----------------------------------------------------------------------------
273
Nico Huber3299ad52018-06-02 16:53:39 +0200274 -- For the default framebuffer setup (see below) with 90 degree rotations,
275 -- we expect the offset which is used for the final scanout to be above
276 -- `GTT_Rotation_Offset`. So we can use `Offset - GTT_Rotation_Offset` for
277 -- the physical memory location and aperture mapping.
278 function Phys_Offset (FB : Framebuffer_Type) return Word32 is
279 (if Rotation_90 (FB)
280 then FB.Offset - Word32 (GTT_Rotation_Offset) * GTT_Page_Size
281 else FB.Offset);
282
Nico Huber8c45bcf2016-11-20 17:30:57 +0100283 ----------------------------------------------------------------------------
284 -- State tracking for the currently configured pipes
285
286 Cur_Configs : Pipe_Configs with Part_Of => State;
287
Nico Huber3d06de82018-05-29 01:35:04 +0200288 function Requires_Scaling (Pipe_Cfg : Pipe_Config) return Boolean is
289 (Requires_Scaling (Pipe_Cfg.Framebuffer, Pipe_Cfg.Mode));
290
Nico Huberb217ece2018-06-02 16:56:35 +0200291 function Scaling_Type (Pipe_Cfg : Pipe_Config) return Scaling_Aspect is
292 (Scaling_Type (Pipe_Cfg.Framebuffer, Pipe_Cfg.Mode));
293
Nico Huber8c45bcf2016-11-20 17:30:57 +0100294 ----------------------------------------------------------------------------
295 -- Internal representation of a single pipe's configuration
296
Tim Wawrzynczak605660b2022-06-08 12:48:19 -0600297 type GPU_Port is
298 (DIGI_A, DIGI_B, DIGI_C, DIGI_D, DIGI_E,
299 DDI_TC1, DDI_TC2, DDI_TC3, DDI_TC4, DDI_TC5, DDI_TC6,
300 LVDS, VGA);
Nico Huber83693c82016-10-08 22:17:55 +0200301
302 subtype Digital_Port is GPU_Port range DIGI_A .. DIGI_E;
Arthur Heymans5d08a932018-03-28 17:00:18 +0200303 subtype GMCH_DP_Port is GPU_Port range DIGI_B .. DIGI_D;
304 subtype GMCH_HDMI_Port is GPU_Port range DIGI_B .. DIGI_C;
Tim Wawrzynczak605660b2022-06-08 12:48:19 -0600305 subtype Combo_Port is GPU_Port range DIGI_A .. DIGI_C;
306 subtype USBC_Port is GPU_Port range DDI_TC1 .. DDI_TC6;
307 subtype TGL_Digital_Port is GPU_Port range DIGI_A .. DDI_TC6;
308
309 function Is_Digital_Port (Port : GPU_Port) return Boolean is
310 (Port in Digital_Port or Port in TGL_Digital_Port);
Nico Huber83693c82016-10-08 22:17:55 +0200311
312 type PCH_Port is
313 (PCH_DAC, PCH_LVDS,
Tim Wawrzynczak605660b2022-06-08 12:48:19 -0600314 PCH_HDMI_A, PCH_HDMI_B, PCH_HDMI_C, PCH_HDMI_D,
315 PCH_DP_B, PCH_DP_C, PCH_DP_D,
316 PCH_TC1, PCH_TC2, PCH_TC3, PCH_TC4, PCH_TC5, PCH_TC6);
Nico Huber83693c82016-10-08 22:17:55 +0200317
Tim Wawrzynczak605660b2022-06-08 12:48:19 -0600318 subtype PCH_HDMI_Port is PCH_Port range PCH_HDMI_A .. PCH_HDMI_D;
Nico Huber83693c82016-10-08 22:17:55 +0200319 subtype PCH_DP_Port is PCH_Port range PCH_DP_B .. PCH_DP_D;
Tim Wawrzynczak605660b2022-06-08 12:48:19 -0600320 subtype PCH_TC_Port is PCH_Port range PCH_TC1 .. PCH_TC6;
Nico Huber83693c82016-10-08 22:17:55 +0200321
Nico Huber5dbaf4b2020-01-08 17:24:58 +0100322 type Panel_Control is (No_Panel, Panel_1, Panel_2);
323 subtype Valid_Panels is Panel_Control range Panel_1 .. Panel_2;
Nico Huber2bbd6e72020-01-07 18:22:59 +0100324
Nico Huber83693c82016-10-08 22:17:55 +0200325 type Port_Config is
326 record
327 Port : GPU_Port;
328 PCH_Port : GMA.PCH_Port;
329 Display : Display_Type;
Nico Huber2bbd6e72020-01-07 18:22:59 +0100330 Panel : Panel_Control;
Nico Huber83693c82016-10-08 22:17:55 +0200331 Mode : Mode_Type;
332 Is_FDI : Boolean;
333 FDI : DP_Link;
334 DP : DP_Link;
335 end record;
336
337 type FDI_Training_Type is (Simple_Training, Full_Training, Auto_Training);
338
339 ----------------------------------------------------------------------------
340
Tim Wawrzynczak24748f32022-09-09 10:54:05 -0600341 type DP_Port is (DP_A, DP_B, DP_C, DP_D, DP_E, DP_F, DP_G, DP_H, DP_I);
Nico Huber83693c82016-10-08 22:17:55 +0200342
Nico Huber247adf32017-06-12 14:39:11 +0200343 ----------------------------------------------------------------------------
344
345 subtype DDI_HDMI_Buf_Trans_Range is Integer range 0 .. 11;
346
Nico Huber0164b022017-08-24 15:12:51 +0200347 ----------------------------------------------------------------------------
348
Nico Huberc5c767a2018-06-03 01:09:04 +0200349 Tile_Width : constant array (Tiling_Type) of Width_Type :=
Nico Huber0164b022017-08-24 15:12:51 +0200350 (Linear => 16, X_Tiled => 128, Y_Tiled => 32);
Nico Huberc5c767a2018-06-03 01:09:04 +0200351 Tile_Rows : constant array (Tiling_Type) of Height_Type :=
Nico Huber9b479412017-08-27 11:55:56 +0200352 (Linear => 1, X_Tiled => 8, Y_Tiled => 32);
Nico Huber0164b022017-08-24 15:12:51 +0200353
Nico Huber5ef4d602017-12-13 13:56:47 +0100354 function FB_Pitch (Px : Pos_Pixel_Type; FB : Framebuffer_Type) return Natural
355 is (Natural (Div_Round_Up
356 (Pixel_To_Bytes (Px, FB), Tile_Width (FB.Tiling) * 4)));
Nico Huber0164b022017-08-24 15:12:51 +0200357
358 function Valid_Stride (FB : Framebuffer_Type) return Boolean is
Nico Huber5ef4d602017-12-13 13:56:47 +0100359 (FB.Width + FB.Start_X <= FB.Stride and
Nico Huber9b479412017-08-27 11:55:56 +0200360 Pixel_To_Bytes (FB.Stride, FB) mod (Tile_Width (FB.Tiling) * 4) = 0 and
Nico Huber5ef4d602017-12-13 13:56:47 +0100361 FB.Height + FB.Start_Y <= FB.V_Stride and
Nico Huber9b479412017-08-27 11:55:56 +0200362 FB.V_Stride mod Tile_Rows (FB.Tiling) = 0);
Nico Huber0164b022017-08-24 15:12:51 +0200363
Nico Huber83693c82016-10-08 22:17:55 +0200364end HW.GFX.GMA;