blob: 37dec9530b8847a7fb4941667c60e2765d19cb94 [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
Tim Wawrzynczak605660b2022-06-08 12:48:19 -060038 type Generation is (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 Heymans73ea0322018-03-28 17:17:07 +020041 (G45,
Nico Huber7f3e2802019-09-28 20:40:55 +020042 GM45,
Arthur Heymans73ea0322018-03-28 17:17:07 +020043 Ironlake,
Nico Huber83693c82016-10-08 22:17:55 +020044 Sandybridge,
45 Ivybridge,
46 Haswell,
47 Broadwell,
Nico Huber21da5742017-01-20 14:00:53 +010048 Broxton,
Nico Huber88badbe2018-09-27 16:36:47 +020049 Skylake,
Tim Wawrzynczak605660b2022-06-08 12:48:19 -060050 Kabylake,
51 Tigerlake);
Nico Huber83693c82016-10-08 22:17:55 +020052
Nico Huber25fdb152019-02-17 15:54:39 +010053 type CPU_Variant is (Normal, ULT, ULX);
Nico Huber83693c82016-10-08 22:17:55 +020054
Nico Hubere79babd2020-12-20 01:33:26 +010055 type PCH_Type is
56 (No_PCH,
57 Ibex_Peak,
58 Cougar_Point, -- Panther Point compatible
59 Lynx_Point, -- Wildcat Point compatible
Nico Huberdde06302020-12-20 02:18:30 +010060 Sunrise_Point, -- Union Point compatible
Tim Wawrzynczak605660b2022-06-08 12:48:19 -060061 Cannon_Point,
62 Tiger_Point);
Nico Hubere79babd2020-12-20 01:33:26 +010063
Nico Huber83693c82016-10-08 22:17:55 +020064 type Port_Type is
65 (Disabled,
Nico Huber8beafd72020-01-07 14:59:44 +010066 LVDS,
67 eDP,
Nico Huber83693c82016-10-08 22:17:55 +020068 DP1,
69 DP2,
70 DP3,
Nico Huber0d454cd2016-11-21 13:33:43 +010071 HDMI1, -- or DVI
72 HDMI2, -- or DVI
73 HDMI3, -- or DVI
Tim Wawrzynczak605660b2022-06-08 12:48:19 -060074 Analog,
75 USBC1_DP,
76 USBC2_DP,
77 USBC3_DP,
78 USBC4_DP,
79 USBC5_DP,
80 USBC6_DP,
81 USBC1_HDMI,
82 USBC2_HDMI,
83 USBC3_HDMI,
84 USBC4_HDMI,
85 USBC5_HDMI,
86 USBC6_HDMI);
Nico Huber040d9b62019-02-18 00:52:00 +010087 subtype Active_Port_Type is Port_Type
88 range Port_Type'Succ (Disabled) .. Port_Type'Last;
Nico Huber8beafd72020-01-07 14:59:44 +010089 subtype Internal_Port_Type is Port_Type range LVDS .. eDP;
Tim Wawrzynczak605660b2022-06-08 12:48:19 -060090 subtype Combo_Port_Type is Port_Type range DP1 .. HDMI3;
91 subtype USBC_Port_Type is Port_Type range USBC1_DP .. USBC6_HDMI;
Nico Huber83693c82016-10-08 22:17:55 +020092
Nico Hubera02b2c62018-01-09 15:58:34 +010093 type Cursor_Mode is (No_Cursor, ARGB_Cursor);
94 type Cursor_Size is (Cursor_64x64, Cursor_128x128, Cursor_256x256);
95 Cursor_Width : constant array (Cursor_Size) of Width_Type := (64, 128, 256);
96
97 subtype Cursor_Pos is Int32 range Int32'First / 2 .. Int32'Last / 2;
98
99 type Cursor_Type is record
100 Mode : Cursor_Mode;
101 Size : Cursor_Size;
102 Center_X : Cursor_Pos;
103 Center_Y : Cursor_Pos;
104 GTT_Offset : GTT_Range;
105 end record;
106 Default_Cursor : constant Cursor_Type :=
107 (Mode => No_Cursor,
108 Size => Cursor_Size'First,
109 Center_X => 0,
110 Center_Y => 0,
111 GTT_Offset => 0);
112
Nico Huber99f10f32016-11-20 00:34:05 +0100113 type Pipe_Config is record
Nico Huber83693c82016-10-08 22:17:55 +0200114 Port : Port_Type;
115 Framebuffer : Framebuffer_Type;
Nico Hubera02b2c62018-01-09 15:58:34 +0100116 Cursor : Cursor_Type;
Nico Huber83693c82016-10-08 22:17:55 +0200117 Mode : Mode_Type;
118 end record;
Nico Huber99f10f32016-11-20 00:34:05 +0100119 type Pipe_Index is (Primary, Secondary, Tertiary);
120 type Pipe_Configs is array (Pipe_Index) of Pipe_Config;
Nico Huber83693c82016-10-08 22:17:55 +0200121
Nico Huber3675db52016-11-04 16:27:29 +0100122 -- Special framebuffer offset to indicate legacy VGA plane.
123 -- Only valid on primary pipe.
124 VGA_PLANE_FRAMEBUFFER_OFFSET : constant := 16#ffff_ffff#;
125
Nico Huberbebca132017-06-12 23:04:46 +0200126 pragma Warnings (GNATprove, Off, "unused variable ""Write_Delay""",
127 Reason => "Write_Delay is used for debugging only");
Nico Huber83693c82016-10-08 22:17:55 +0200128 procedure Initialize
Nico Huber2b6f6992017-07-09 18:11:34 +0200129 (Write_Delay : in Word64 := 0;
Nico Huber793a8d42016-11-21 18:57:03 +0100130 Clean_State : in Boolean := False;
Nico Huber83693c82016-10-08 22:17:55 +0200131 Success : out Boolean)
132 with
133 Global =>
Nico Huber27088aa2018-06-10 13:28:05 +0200134 (In_Out => (Device_State, Port_IO.State),
Nico Hubere317e9c2019-09-29 03:03:18 +0200135 Output => (State, Init_State),
Nico Huber83693c82016-10-08 22:17:55 +0200136 Input => (Time.State)),
137 Post => Success = Is_Initialized;
138 function Is_Initialized return Boolean
139 with
140 Global => (Input => Init_State);
Nico Huberbebca132017-06-12 23:04:46 +0200141 pragma Warnings (GNATprove, On, "unused variable ""Write_Delay""");
Nico Huber83693c82016-10-08 22:17:55 +0200142
Nico Huber42fb2d02017-09-01 17:01:51 +0200143 procedure Power_Up_VGA
144 with
Nico Hubercf88f3d2018-06-05 13:27:34 +0200145 Global =>
Nico Hubere317e9c2019-09-29 03:03:18 +0200146 (Input => (State, Time.State),
Nico Hubercf88f3d2018-06-05 13:27:34 +0200147 In_Out => (Device_State),
148 Proof_In => (Init_State)),
Nico Huber42fb2d02017-09-01 17:01:51 +0200149 Pre => Is_Initialized;
150
Nico Hubercf88f3d2018-06-05 13:27:34 +0200151 ----------------------------------------------------------------------------
Nico Huber83693c82016-10-08 22:17:55 +0200152
Nico Hubercf88f3d2018-06-05 13:27:34 +0200153 procedure Update_Outputs (Configs : Pipe_Configs)
154 with
155 Global =>
Nico Hubere317e9c2019-09-29 03:03:18 +0200156 (Input => (Time.State),
Nico Hubercf88f3d2018-06-05 13:27:34 +0200157 In_Out => (State, Device_State, Port_IO.State),
158 Proof_In => (Init_State)),
159 Pre => Is_Initialized;
160
161 procedure Update_Cursor (Pipe : Pipe_Index; Cursor : Cursor_Type)
162 with
163 Global =>
Nico Hubere317e9c2019-09-29 03:03:18 +0200164 (In_Out => (State, Device_State),
Nico Hubercf88f3d2018-06-05 13:27:34 +0200165 Proof_In => (Init_State)),
Nico Hubere317e9c2019-09-29 03:03:18 +0200166 Pre => Is_Initialized;
Nico Hubercf88f3d2018-06-05 13:27:34 +0200167
Nico Huber15ffc4f2018-01-11 14:44:43 +0100168 procedure Place_Cursor
169 (Pipe : Pipe_Index;
170 X : Cursor_Pos;
Nico Hubercf88f3d2018-06-05 13:27:34 +0200171 Y : Cursor_Pos)
172 with
173 Global =>
Nico Hubere317e9c2019-09-29 03:03:18 +0200174 (In_Out => (State, Device_State),
Nico Hubercf88f3d2018-06-05 13:27:34 +0200175 Proof_In => (Init_State)),
Nico Hubere317e9c2019-09-29 03:03:18 +0200176 Pre => Is_Initialized;
Nico Hubercf88f3d2018-06-05 13:27:34 +0200177
Nico Huber15ffc4f2018-01-11 14:44:43 +0100178 procedure Move_Cursor
179 (Pipe : Pipe_Index;
180 X : Cursor_Pos;
Nico Hubercf88f3d2018-06-05 13:27:34 +0200181 Y : Cursor_Pos)
182 with
183 Global =>
Nico Hubere317e9c2019-09-29 03:03:18 +0200184 (In_Out => (State, Device_State),
Nico Hubercf88f3d2018-06-05 13:27:34 +0200185 Proof_In => (Init_State)),
Nico Hubere317e9c2019-09-29 03:03:18 +0200186 Pre => Is_Initialized;
Nico Huber15ffc4f2018-01-11 14:44:43 +0100187
Nico Hubercf88f3d2018-06-05 13:27:34 +0200188 ----------------------------------------------------------------------------
Nico Huber83693c82016-10-08 22:17:55 +0200189
Nico Huberbc0588e2020-07-21 12:17:19 +0200190 procedure Backlight_On (Port : Active_Port_Type)
191 with
192 Global => (In_Out => Device_State);
193
194 procedure Backlight_Off (Port : Active_Port_Type)
195 with
196 Global => (In_Out => Device_State);
197
198 procedure Set_Brightness (Port : Active_Port_Type; Level : Word32)
199 with
200 Global => (In_Out => Device_State);
201
202 procedure Get_Max_Brightness (Port : Active_Port_Type; Level : out Word32)
203 with
204 Global => (In_Out => Device_State);
205
206 ----------------------------------------------------------------------------
207
Nico Huber83693c82016-10-08 22:17:55 +0200208 procedure Write_GTT
209 (GTT_Page : GTT_Range;
210 Device_Address : GTT_Address_Type;
Nico Hubercf88f3d2018-06-05 13:27:34 +0200211 Valid : Boolean)
212 with
Nico Huberadfe11f2018-06-10 14:59:04 +0200213 Global =>
Nico Hubere317e9c2019-09-29 03:03:18 +0200214 (Input => State,
Nico Huberadfe11f2018-06-10 14:59:04 +0200215 In_Out => Device_State, Proof_In => Init_State),
Nico Hubercf88f3d2018-06-05 13:27:34 +0200216 Pre => Is_Initialized;
Nico Huber83693c82016-10-08 22:17:55 +0200217
Nico Huberceda17d2018-06-09 22:00:29 +0200218 procedure Read_GTT
219 (Device_Address : out GTT_Address_Type;
220 Valid : out Boolean;
221 GTT_Page : in GTT_Range)
222 with
Nico Huberadfe11f2018-06-10 14:59:04 +0200223 Global =>
Nico Hubere317e9c2019-09-29 03:03:18 +0200224 (Input => State,
Nico Huberadfe11f2018-06-10 14:59:04 +0200225 In_Out => Device_State, Proof_In => Init_State),
Nico Huberceda17d2018-06-09 22:00:29 +0200226 Pre => Is_Initialized;
227
Nico Huber5374c3a2017-07-15 21:48:06 +0200228 procedure Setup_Default_FB
229 (FB : in Framebuffer_Type;
230 Clear : in Boolean := True;
231 Success : out Boolean)
Nico Huber194e57e2017-07-15 21:15:46 +0200232 with
Nico Hubercf88f3d2018-06-05 13:27:34 +0200233 Global =>
Nico Hubere317e9c2019-09-29 03:03:18 +0200234 (In_Out =>
Nico Hubercf88f3d2018-06-05 13:27:34 +0200235 (State, Device_State,
236 Framebuffer_Filler.State, Framebuffer_Filler.Base_Address),
237 Proof_In => (Init_State)),
Nico Huber5374c3a2017-07-15 21:48:06 +0200238 Pre => Is_Initialized and HW.Config.Dynamic_MMIO;
Nico Huber83693c82016-10-08 22:17:55 +0200239
Nico Huberc3f66f62017-07-16 21:39:54 +0200240 procedure Map_Linear_FB (Linear_FB : out Word64; FB : in Framebuffer_Type)
241 with
Nico Hubercf88f3d2018-06-05 13:27:34 +0200242 Global =>
Nico Hubere317e9c2019-09-29 03:03:18 +0200243 (In_Out => (State, Device_State),
Nico Hubercf88f3d2018-06-05 13:27:34 +0200244 Proof_In => (Init_State)),
Nico Hubere317e9c2019-09-29 03:03:18 +0200245 Pre => Is_Initialized and HW.Config.Dynamic_MMIO;
Nico Huberc3f66f62017-07-16 21:39:54 +0200246
Nico Hubercf88f3d2018-06-05 13:27:34 +0200247 ----------------------------------------------------------------------------
248
249 pragma Warnings (GNATprove, Off, "subprogram ""Dump_Configs"" has no effect",
250 Reason => "It's only used for debugging");
251 procedure Dump_Configs (Configs : Pipe_Configs);
252
Nico Huber83693c82016-10-08 22:17:55 +0200253private
254
Nico Huberc5c66ec2019-09-28 23:59:45 +0200255 PCI_Usable : Boolean with Part_Of => State;
256 use type HW.PCI.Index;
257 procedure PCI_Read16 (Value : out Word16; Offset : HW.PCI.Index)
258 with
259 Pre => PCI_Usable and Offset mod 2 = 0;
260
261 ----------------------------------------------------------------------------
262
Nico Huber3299ad52018-06-02 16:53:39 +0200263 -- For the default framebuffer setup (see below) with 90 degree rotations,
264 -- we expect the offset which is used for the final scanout to be above
265 -- `GTT_Rotation_Offset`. So we can use `Offset - GTT_Rotation_Offset` for
266 -- the physical memory location and aperture mapping.
267 function Phys_Offset (FB : Framebuffer_Type) return Word32 is
268 (if Rotation_90 (FB)
269 then FB.Offset - Word32 (GTT_Rotation_Offset) * GTT_Page_Size
270 else FB.Offset);
271
Nico Huber8c45bcf2016-11-20 17:30:57 +0100272 ----------------------------------------------------------------------------
273 -- State tracking for the currently configured pipes
274
275 Cur_Configs : Pipe_Configs with Part_Of => State;
276
Nico Huber3d06de82018-05-29 01:35:04 +0200277 function Requires_Scaling (Pipe_Cfg : Pipe_Config) return Boolean is
278 (Requires_Scaling (Pipe_Cfg.Framebuffer, Pipe_Cfg.Mode));
279
Nico Huberb217ece2018-06-02 16:56:35 +0200280 function Scaling_Type (Pipe_Cfg : Pipe_Config) return Scaling_Aspect is
281 (Scaling_Type (Pipe_Cfg.Framebuffer, Pipe_Cfg.Mode));
282
Nico Huber8c45bcf2016-11-20 17:30:57 +0100283 ----------------------------------------------------------------------------
284 -- Internal representation of a single pipe's configuration
285
Tim Wawrzynczak605660b2022-06-08 12:48:19 -0600286 type GPU_Port is
287 (DIGI_A, DIGI_B, DIGI_C, DIGI_D, DIGI_E,
288 DDI_TC1, DDI_TC2, DDI_TC3, DDI_TC4, DDI_TC5, DDI_TC6,
289 LVDS, VGA);
Nico Huber83693c82016-10-08 22:17:55 +0200290
291 subtype Digital_Port is GPU_Port range DIGI_A .. DIGI_E;
Arthur Heymans5d08a932018-03-28 17:00:18 +0200292 subtype GMCH_DP_Port is GPU_Port range DIGI_B .. DIGI_D;
293 subtype GMCH_HDMI_Port is GPU_Port range DIGI_B .. DIGI_C;
Tim Wawrzynczak605660b2022-06-08 12:48:19 -0600294 subtype Combo_Port is GPU_Port range DIGI_A .. DIGI_C;
295 subtype USBC_Port is GPU_Port range DDI_TC1 .. DDI_TC6;
296 subtype TGL_Digital_Port is GPU_Port range DIGI_A .. DDI_TC6;
297
298 function Is_Digital_Port (Port : GPU_Port) return Boolean is
299 (Port in Digital_Port or Port in TGL_Digital_Port);
Nico Huber83693c82016-10-08 22:17:55 +0200300
301 type PCH_Port is
302 (PCH_DAC, PCH_LVDS,
Tim Wawrzynczak605660b2022-06-08 12:48:19 -0600303 PCH_HDMI_A, PCH_HDMI_B, PCH_HDMI_C, PCH_HDMI_D,
304 PCH_DP_B, PCH_DP_C, PCH_DP_D,
305 PCH_TC1, PCH_TC2, PCH_TC3, PCH_TC4, PCH_TC5, PCH_TC6);
Nico Huber83693c82016-10-08 22:17:55 +0200306
Tim Wawrzynczak605660b2022-06-08 12:48:19 -0600307 subtype PCH_HDMI_Port is PCH_Port range PCH_HDMI_A .. PCH_HDMI_D;
Nico Huber83693c82016-10-08 22:17:55 +0200308 subtype PCH_DP_Port is PCH_Port range PCH_DP_B .. PCH_DP_D;
Tim Wawrzynczak605660b2022-06-08 12:48:19 -0600309 subtype PCH_TC_Port is PCH_Port range PCH_TC1 .. PCH_TC6;
Nico Huber83693c82016-10-08 22:17:55 +0200310
Nico Huber5dbaf4b2020-01-08 17:24:58 +0100311 type Panel_Control is (No_Panel, Panel_1, Panel_2);
312 subtype Valid_Panels is Panel_Control range Panel_1 .. Panel_2;
Nico Huber2bbd6e72020-01-07 18:22:59 +0100313
Nico Huber83693c82016-10-08 22:17:55 +0200314 type Port_Config is
315 record
316 Port : GPU_Port;
317 PCH_Port : GMA.PCH_Port;
318 Display : Display_Type;
Nico Huber2bbd6e72020-01-07 18:22:59 +0100319 Panel : Panel_Control;
Nico Huber83693c82016-10-08 22:17:55 +0200320 Mode : Mode_Type;
321 Is_FDI : Boolean;
322 FDI : DP_Link;
323 DP : DP_Link;
324 end record;
325
326 type FDI_Training_Type is (Simple_Training, Full_Training, Auto_Training);
327
328 ----------------------------------------------------------------------------
329
330 type DP_Port is (DP_A, DP_B, DP_C, DP_D);
331
Nico Huber247adf32017-06-12 14:39:11 +0200332 ----------------------------------------------------------------------------
333
334 subtype DDI_HDMI_Buf_Trans_Range is Integer range 0 .. 11;
335
Nico Huber0164b022017-08-24 15:12:51 +0200336 ----------------------------------------------------------------------------
337
Nico Huberc5c767a2018-06-03 01:09:04 +0200338 Tile_Width : constant array (Tiling_Type) of Width_Type :=
Nico Huber0164b022017-08-24 15:12:51 +0200339 (Linear => 16, X_Tiled => 128, Y_Tiled => 32);
Nico Huberc5c767a2018-06-03 01:09:04 +0200340 Tile_Rows : constant array (Tiling_Type) of Height_Type :=
Nico Huber9b479412017-08-27 11:55:56 +0200341 (Linear => 1, X_Tiled => 8, Y_Tiled => 32);
Nico Huber0164b022017-08-24 15:12:51 +0200342
Nico Huber5ef4d602017-12-13 13:56:47 +0100343 function FB_Pitch (Px : Pos_Pixel_Type; FB : Framebuffer_Type) return Natural
344 is (Natural (Div_Round_Up
345 (Pixel_To_Bytes (Px, FB), Tile_Width (FB.Tiling) * 4)));
Nico Huber0164b022017-08-24 15:12:51 +0200346
347 function Valid_Stride (FB : Framebuffer_Type) return Boolean is
Nico Huber5ef4d602017-12-13 13:56:47 +0100348 (FB.Width + FB.Start_X <= FB.Stride and
Nico Huber9b479412017-08-27 11:55:56 +0200349 Pixel_To_Bytes (FB.Stride, FB) mod (Tile_Width (FB.Tiling) * 4) = 0 and
Nico Huber5ef4d602017-12-13 13:56:47 +0100350 FB.Height + FB.Start_Y <= FB.V_Stride and
Nico Huber9b479412017-08-27 11:55:56 +0200351 FB.V_Stride mod Tile_Rows (FB.Tiling) = 0);
Nico Huber0164b022017-08-24 15:12:51 +0200352
Nico Huber83693c82016-10-08 22:17:55 +0200353end HW.GFX.GMA;