blob: e0ad71fe228165d8e6f62cb2f31938aeeca7bb32 [file] [log] [blame]
Nico Huberfda2d6e2017-07-09 16:47:52 +02001with Ada.Unchecked_Conversion;
Nico Huber1d0abe42017-03-05 14:14:09 +01002with Ada.Command_Line;
3with Interfaces.C;
4
Nico Huber3b654a02017-07-15 22:27:14 +02005with HW.Time;
Nico Huber1d0abe42017-03-05 14:14:09 +01006with HW.Debug;
Nico Huberfda2d6e2017-07-09 16:47:52 +02007with HW.PCI.Dev;
8with HW.MMIO_Range;
Nico Huber1d0abe42017-03-05 14:14:09 +01009with HW.GFX.GMA;
Nico Huber3b654a02017-07-15 22:27:14 +020010with HW.GFX.GMA.Config;
Nico Huber1d0abe42017-03-05 14:14:09 +010011with HW.GFX.GMA.Display_Probing;
12
Nico Huberfda2d6e2017-07-09 16:47:52 +020013package body HW.GFX.GMA.GFX_Test
14is
15 pragma Disable_Atomic_Synchronization;
Nico Huber1d0abe42017-03-05 14:14:09 +010016
Nico Huberfda2d6e2017-07-09 16:47:52 +020017 package Dev is new PCI.Dev (PCI.Address'(0, 2, 0));
Nico Huber1d0abe42017-03-05 14:14:09 +010018
Nico Huber3b654a02017-07-15 22:27:14 +020019 type GTT_PTE_Type is mod 2 ** (Config.GTT_PTE_Size * 8);
20 type GTT_Registers_Type is array (GTT_Range) of GTT_PTE_Type;
21 package GTT is new MMIO_Range
22 (Base_Addr => 0,
23 Element_T => GTT_PTE_Type,
24 Index_T => GTT_Range,
25 Array_T => GTT_Registers_Type);
26
27 GTT_Backup : GTT_Registers_Type;
28
29 procedure Backup_GTT
30 is
31 begin
32 for Idx in GTT_Range loop
33 GTT.Read (GTT_Backup (Idx), Idx);
34 end loop;
35 end Backup_GTT;
36
37 procedure Restore_GTT
38 is
39 begin
40 for Idx in GTT_Range loop
41 GTT.Write (Idx, GTT_Backup (Idx));
42 end loop;
43 end Restore_GTT;
44
Nico Huber1d0abe42017-03-05 14:14:09 +010045 type Pixel_Type is record
46 Red : Byte;
47 Green : Byte;
48 Blue : Byte;
49 Alpha : Byte;
50 end record;
51
52 for Pixel_Type use record
53 Blue at 0 range 0 .. 7;
54 Green at 1 range 0 .. 7;
55 Red at 2 range 0 .. 7;
56 Alpha at 3 range 0 .. 7;
57 end record;
58
Nico Huber244ea7e2017-08-28 11:38:23 +020059 White : constant Pixel_Type := (255, 255, 255, 255);
60 Black : constant Pixel_Type := ( 0, 0, 0, 255);
61 Red : constant Pixel_Type := (255, 0, 0, 255);
62 Green : constant Pixel_Type := ( 0, 255, 0, 255);
63 Blue : constant Pixel_Type := ( 0, 0, 255, 255);
64
Nico Huberfda2d6e2017-07-09 16:47:52 +020065 function Pixel_To_Word (P : Pixel_Type) return Word32
66 with
67 SPARK_Mode => Off
68 is
69 function To_Word is new Ada.Unchecked_Conversion (Pixel_Type, Word32);
70 begin
71 return To_Word (P);
72 end Pixel_To_Word;
73
Nico Huber1d0abe42017-03-05 14:14:09 +010074 Max_W : constant := 4096;
75 Max_H : constant := 2160;
76 FB_Align : constant := 16#0004_0000#;
Nico Huberfda2d6e2017-07-09 16:47:52 +020077 subtype Screen_Index is Natural range
78 0 .. 3 * (Max_W * Max_H + FB_Align / 4) - 1;
79 type Screen_Type is array (Screen_Index) of Word32;
Nico Huber1d0abe42017-03-05 14:14:09 +010080
Nico Huberfda2d6e2017-07-09 16:47:52 +020081 package Screen is new MMIO_Range (0, Word32, Screen_Index, Screen_Type);
Nico Huber1d0abe42017-03-05 14:14:09 +010082
Nico Huber3b654a02017-07-15 22:27:14 +020083 Screen_Backup : Screen_Type;
84
85 procedure Backup_Screen (FB : Framebuffer_Type)
86 is
87 First : constant Screen_Index := Natural (FB.Offset) / 4;
88 Last : constant Screen_Index := First + Natural (FB_Size (FB)) / 4 - 1;
89 begin
90 for Idx in Screen_Index range First .. Last loop
91 Screen.Read (Screen_Backup (Idx), Idx);
92 end loop;
93 end Backup_Screen;
94
95 procedure Restore_Screen (FB : Framebuffer_Type)
96 is
97 First : constant Screen_Index := Natural (FB.Offset) / 4;
98 Last : constant Screen_Index := First + Natural (FB_Size (FB)) / 4 - 1;
99 begin
100 for Idx in Screen_Index range First .. Last loop
101 Screen.Write (Idx, Screen_Backup (Idx));
102 end loop;
103 end Restore_Screen;
Nico Huber1d0abe42017-03-05 14:14:09 +0100104
Nico Huber244ea7e2017-08-28 11:38:23 +0200105 function Corner_Fill
106 (X, Y : Natural;
107 FB : Framebuffer_Type;
108 Pipe : Pipe_Index)
109 return Pixel_Type
110 is
111 Xrel : constant Integer :=
112 (if X < 32 then X else X - (Natural (FB.Width) - 32));
113 Yrel : constant Integer :=
114 (if Y < 32 then Y else Y - (Natural (FB.Height) - 32));
115
116 function Color (Idx : Natural) return Pixel_Type is
117 (case (Idx + Pipe_Index'Pos (Pipe)) mod 4 is
118 when 0 => Blue, when 1 => Black,
119 when 3 => Green, when others => Red);
120 begin
121 return
122 (if Xrel mod 16 = 0 or Xrel = 31 or Yrel mod 16 = 0 or Yrel = 31 then
123 White
124 elsif Yrel < 16 then
125 (if Xrel < 16 then Color (0) else Color (1))
126 else
127 (if Xrel < 16 then Color (3) else Color (2)));
128 end Corner_Fill;
129
Nico Huber1d0abe42017-03-05 14:14:09 +0100130 function Fill
131 (X, Y : Natural;
132 Framebuffer : Framebuffer_Type;
Nico Huber244ea7e2017-08-28 11:38:23 +0200133 Pipe : Pipe_Index)
Nico Huber1d0abe42017-03-05 14:14:09 +0100134 return Pixel_Type
135 is
136 use type HW.Byte;
137
138 Xp : constant Natural := X * 256 / Natural (Framebuffer.Width);
139 Yp : constant Natural := Y * 256 / Natural (Framebuffer.Height);
140 Xn : constant Natural := 255 - Xp;
141 Yn : constant Natural := 255 - Yp;
142
143 function Map (X, Y : Natural) return Byte is
144 begin
145 return Byte (X * Y / 255);
146 end Map;
147 begin
148 return
149 (case Pipe is
150 when GMA.Primary => (Map (Xn, Yn), Map (Xp, Yn), Map (Xp, Yp), 255),
151 when GMA.Secondary => (Map (Xn, Yp), Map (Xn, Yn), Map (Xp, Yn), 255),
152 when GMA.Tertiary => (Map (Xp, Yp), Map (Xn, Yp), Map (Xn, Yn), 255));
153 end Fill;
154
155 procedure Test_Screen
156 (Framebuffer : Framebuffer_Type;
157 Pipe : GMA.Pipe_Index)
158 is
Nico Huber1d0abe42017-03-05 14:14:09 +0100159 P : Pixel_Type;
160 -- We have pixel offset wheras the framebuffer has a byte offset
161 Offset_Y : Natural := Natural (Framebuffer.Offset / 4);
162 Offset : Natural;
163 begin
164 for Y in 0 .. Natural (Framebuffer.Height) - 1 loop
165 Offset := Offset_Y;
166 for X in 0 .. Natural (Framebuffer.Width) - 1 loop
Nico Huber244ea7e2017-08-28 11:38:23 +0200167 if (X < 32 or X >= Natural (Framebuffer.Width) - 32) and
168 (Y < 32 or Y >= Natural (Framebuffer.Height) - 32)
169 then
170 P := Corner_Fill (X, Y, Framebuffer, Pipe);
171 elsif Y mod 16 = 0 or X mod 16 = 0 then
172 P := Black;
Nico Huber1d0abe42017-03-05 14:14:09 +0100173 else
174 P := Fill (X, Y, Framebuffer, Pipe);
175 end if;
Nico Huberfda2d6e2017-07-09 16:47:52 +0200176 Screen.Write (Offset, Pixel_To_Word (P));
Nico Huber1d0abe42017-03-05 14:14:09 +0100177 Offset := Offset + 1;
178 end loop;
179 Offset_Y := Offset_Y + Natural (Framebuffer.Stride);
180 end loop;
181 end Test_Screen;
182
183 procedure Calc_Framebuffer
184 (FB : out Framebuffer_Type;
185 Mode : in Mode_Type;
Nico Huber88f3c982017-08-28 13:31:38 +0200186 Rotation : in Rotation_Type;
Nico Huber1d0abe42017-03-05 14:14:09 +0100187 Offset : in out Word32)
188 is
Nico Huber1d0abe42017-03-05 14:14:09 +0100189 begin
190 Offset := (Offset + FB_Align - 1) and not (FB_Align - 1);
Nico Huber88f3c982017-08-28 13:31:38 +0200191 if Rotation = Rotated_90 or Rotation = Rotated_270 then
192 FB :=
193 (Width => Width_Type (Mode.V_Visible),
194 Height => Height_Type (Mode.H_Visible),
195 BPC => 8,
196 Stride => Div_Round_Up (Pos32 (Mode.V_Visible), 32) * 32,
197 V_Stride => Div_Round_Up (Pos32 (Mode.H_Visible), 32) * 32,
198 Tiling => Y_Tiled,
199 Rotation => Rotation,
200 Offset => Offset);
201 else
202 FB :=
203 (Width => Width_Type (Mode.H_Visible),
204 Height => Width_Type (Mode.V_Visible),
205 BPC => 8,
206 Stride => Div_Round_Up (Pos32 (Mode.H_Visible), 16) * 16,
207 V_Stride => Height_Type (Mode.V_Visible),
208 Tiling => Linear,
209 Rotation => Rotation,
210 Offset => Offset);
211 end if;
Nico Huberb7470492017-11-30 14:48:35 +0100212 Offset := Offset + Word32 (FB_Size (FB));
Nico Huber1d0abe42017-03-05 14:14:09 +0100213 end Calc_Framebuffer;
214
Nico Huber3b654a02017-07-15 22:27:14 +0200215 Pipes : GMA.Pipe_Configs;
216
Nico Huber88f3c982017-08-28 13:31:38 +0200217 procedure Prepare_Configs (Rotation : Rotation_Type)
Nico Huber1d0abe42017-03-05 14:14:09 +0100218 is
219 use type HW.GFX.GMA.Port_Type;
220
Nico Huberfda2d6e2017-07-09 16:47:52 +0200221 Offset : Word32 := 0;
Nico Huber3b654a02017-07-15 22:27:14 +0200222 Success : Boolean;
Nico Huber1d0abe42017-03-05 14:14:09 +0100223 begin
224 GMA.Display_Probing.Scan_Ports (Pipes);
225
226 for Pipe in GMA.Pipe_Index loop
227 if Pipes (Pipe).Port /= GMA.Disabled then
228 Calc_Framebuffer
229 (FB => Pipes (Pipe).Framebuffer,
230 Mode => Pipes (Pipe).Mode,
Nico Huber88f3c982017-08-28 13:31:38 +0200231 Rotation => Rotation,
Nico Huber1d0abe42017-03-05 14:14:09 +0100232 Offset => Offset);
Nico Huber3b654a02017-07-15 22:27:14 +0200233 GMA.Setup_Default_FB
234 (FB => Pipes (Pipe).Framebuffer,
235 Clear => False,
236 Success => Success);
237 if not Success then
238 Pipes (Pipe).Port := GMA.Disabled;
239 end if;
Nico Huber1d0abe42017-03-05 14:14:09 +0100240 end if;
241 end loop;
242
243 GMA.Dump_Configs (Pipes);
244 end Prepare_Configs;
245
Nico Huber3b654a02017-07-15 22:27:14 +0200246 procedure Print_Usage
247 is
248 begin
249 Debug.Put_Line
Nico Huber88f3c982017-08-28 13:31:38 +0200250 ("Usage: " & Ada.Command_Line.Command_Name &
251 " <delay seconds>" &
252 " [(0|90|180|270)]");
Nico Huber3b654a02017-07-15 22:27:14 +0200253 Debug.New_Line;
254 end Print_Usage;
255
Nico Huber1d0abe42017-03-05 14:14:09 +0100256 procedure Main
257 is
Nico Huber1d0abe42017-03-05 14:14:09 +0100258 use type HW.GFX.GMA.Port_Type;
Nico Huberfda2d6e2017-07-09 16:47:52 +0200259 use type HW.Word64;
Nico Huber1d0abe42017-03-05 14:14:09 +0100260 use type Interfaces.C.int;
261
Nico Huberfda2d6e2017-07-09 16:47:52 +0200262 Res_Addr : Word64;
263
Nico Huber3b654a02017-07-15 22:27:14 +0200264 Delay_S : Natural;
Nico Huber88f3c982017-08-28 13:31:38 +0200265 Rotation : Rotation_Type := No_Rotation;
Nico Huber3b654a02017-07-15 22:27:14 +0200266
Nico Huberfda2d6e2017-07-09 16:47:52 +0200267 Dev_Init,
Nico Huber1d0abe42017-03-05 14:14:09 +0100268 Initialized : Boolean;
269
270 function iopl (level : Interfaces.C.int) return Interfaces.C.int;
271 pragma Import (C, iopl, "iopl");
272 begin
Nico Huber88f3c982017-08-28 13:31:38 +0200273 if Ada.Command_Line.Argument_Count < 1 then
Nico Huber3b654a02017-07-15 22:27:14 +0200274 Print_Usage;
275 return;
276 end if;
277
278 Delay_S := Natural'Value (Ada.Command_Line.Argument (1));
279
Nico Huber88f3c982017-08-28 13:31:38 +0200280 if Ada.Command_Line.Argument_Count >= 2 then
281 declare
282 Rotation_Degree : constant String := Ada.Command_Line.Argument (2);
283 begin
284 if Rotation_Degree = "0" then Rotation := No_Rotation;
285 elsif Rotation_Degree = "90" then Rotation := Rotated_90;
286 elsif Rotation_Degree = "180" then Rotation := Rotated_180;
287 elsif Rotation_Degree = "270" then Rotation := Rotated_270;
288 else Print_Usage; return; end if;
289 end;
290 end if;
291
Nico Huber1d0abe42017-03-05 14:14:09 +0100292 if iopl (3) /= 0 then
293 Debug.Put_Line ("Failed to change i/o privilege level.");
294 return;
295 end if;
296
Nico Huberfda2d6e2017-07-09 16:47:52 +0200297 Dev.Initialize (Dev_Init);
298 if not Dev_Init then
299 Debug.Put_Line ("Failed to map PCI config.");
Nico Huber1d0abe42017-03-05 14:14:09 +0100300 return;
301 end if;
302
Nico Huber3b654a02017-07-15 22:27:14 +0200303 Dev.Map (Res_Addr, PCI.Res0, Offset => Config.GTT_Offset);
304 if Res_Addr = 0 then
305 Debug.Put_Line ("Failed to map PCI resource0.");
306 return;
307 end if;
308 GTT.Set_Base_Address (Res_Addr);
309
Nico Huberfda2d6e2017-07-09 16:47:52 +0200310 Dev.Map (Res_Addr, PCI.Res2, WC => True);
311 if Res_Addr = 0 then
312 Debug.Put_Line ("Failed to map PCI resource2.");
313 return;
314 end if;
315 Screen.Set_Base_Address (Res_Addr);
316
Nico Huber1d0abe42017-03-05 14:14:09 +0100317 GMA.Initialize
Nico Huber2b6f6992017-07-09 18:11:34 +0200318 (Clean_State => True,
Nico Huber1d0abe42017-03-05 14:14:09 +0100319 Success => Initialized);
320
321 if Initialized then
Nico Huber3b654a02017-07-15 22:27:14 +0200322 Backup_GTT;
323
Nico Huber88f3c982017-08-28 13:31:38 +0200324 Prepare_Configs (Rotation);
Nico Huber1d0abe42017-03-05 14:14:09 +0100325
326 GMA.Update_Outputs (Pipes);
327
328 for Pipe in GMA.Pipe_Index loop
329 if Pipes (Pipe).Port /= GMA.Disabled then
Nico Huber3b654a02017-07-15 22:27:14 +0200330 Backup_Screen (Pipes (Pipe).Framebuffer);
Nico Huber1d0abe42017-03-05 14:14:09 +0100331 Test_Screen
332 (Framebuffer => Pipes (Pipe).Framebuffer,
333 Pipe => Pipe);
334 end if;
335 end loop;
Nico Huber3b654a02017-07-15 22:27:14 +0200336
337 Time.M_Delay (Delay_S * 1_000);
338
339 for Pipe in GMA.Pipe_Index loop
340 if Pipes (Pipe).Port /= GMA.Disabled then
341 Restore_Screen (Pipes (Pipe).Framebuffer);
342 end if;
343 end loop;
344 Restore_GTT;
Nico Huber1d0abe42017-03-05 14:14:09 +0100345 end if;
346 end Main;
347
Nico Huberfda2d6e2017-07-09 16:47:52 +0200348end HW.GFX.GMA.GFX_Test;