blob: 401666484bb0609ed8239917a3b8207b77e92b1b [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;
186 Offset : in out Word32)
187 is
Nico Huber1d0abe42017-03-05 14:14:09 +0100188 begin
189 Offset := (Offset + FB_Align - 1) and not (FB_Align - 1);
190 FB :=
191 (Width => Width_Type (Mode.H_Visible),
192 Height => Height_Type (Mode.V_Visible),
193 BPC => 8,
194 Stride => Width_Type ((Word32 (Mode.H_Visible) + 15) and not 15),
Nico Huberb7470492017-11-30 14:48:35 +0100195 V_Stride => Height_Type (Mode.V_Visible),
Nico Huber51375ad2017-08-24 14:44:06 +0200196 Tiling => Linear,
Nico Huberb7470492017-11-30 14:48:35 +0100197 Rotation => No_Rotation,
Nico Huber1d0abe42017-03-05 14:14:09 +0100198 Offset => Offset);
Nico Huberb7470492017-11-30 14:48:35 +0100199 Offset := Offset + Word32 (FB_Size (FB));
Nico Huber1d0abe42017-03-05 14:14:09 +0100200 end Calc_Framebuffer;
201
Nico Huber3b654a02017-07-15 22:27:14 +0200202 Pipes : GMA.Pipe_Configs;
203
Nico Huber1d0abe42017-03-05 14:14:09 +0100204 procedure Prepare_Configs
205 is
206 use type HW.GFX.GMA.Port_Type;
207
Nico Huberfda2d6e2017-07-09 16:47:52 +0200208 Offset : Word32 := 0;
Nico Huber3b654a02017-07-15 22:27:14 +0200209 Success : Boolean;
Nico Huber1d0abe42017-03-05 14:14:09 +0100210 begin
211 GMA.Display_Probing.Scan_Ports (Pipes);
212
213 for Pipe in GMA.Pipe_Index loop
214 if Pipes (Pipe).Port /= GMA.Disabled then
215 Calc_Framebuffer
216 (FB => Pipes (Pipe).Framebuffer,
217 Mode => Pipes (Pipe).Mode,
218 Offset => Offset);
Nico Huber3b654a02017-07-15 22:27:14 +0200219 GMA.Setup_Default_FB
220 (FB => Pipes (Pipe).Framebuffer,
221 Clear => False,
222 Success => Success);
223 if not Success then
224 Pipes (Pipe).Port := GMA.Disabled;
225 end if;
Nico Huber1d0abe42017-03-05 14:14:09 +0100226 end if;
227 end loop;
228
229 GMA.Dump_Configs (Pipes);
230 end Prepare_Configs;
231
Nico Huber3b654a02017-07-15 22:27:14 +0200232 procedure Print_Usage
233 is
234 begin
235 Debug.Put_Line
236 ("Usage: " & Ada.Command_Line.Command_Name & " <delay seconds>");
237 Debug.New_Line;
238 end Print_Usage;
239
Nico Huber1d0abe42017-03-05 14:14:09 +0100240 procedure Main
241 is
Nico Huber1d0abe42017-03-05 14:14:09 +0100242 use type HW.GFX.GMA.Port_Type;
Nico Huberfda2d6e2017-07-09 16:47:52 +0200243 use type HW.Word64;
Nico Huber1d0abe42017-03-05 14:14:09 +0100244 use type Interfaces.C.int;
245
Nico Huberfda2d6e2017-07-09 16:47:52 +0200246 Res_Addr : Word64;
247
Nico Huber3b654a02017-07-15 22:27:14 +0200248 Delay_S : Natural;
249
Nico Huberfda2d6e2017-07-09 16:47:52 +0200250 Dev_Init,
Nico Huber1d0abe42017-03-05 14:14:09 +0100251 Initialized : Boolean;
252
253 function iopl (level : Interfaces.C.int) return Interfaces.C.int;
254 pragma Import (C, iopl, "iopl");
255 begin
Nico Huber3b654a02017-07-15 22:27:14 +0200256 if Ada.Command_Line.Argument_Count /= 1 then
257 Print_Usage;
258 return;
259 end if;
260
261 Delay_S := Natural'Value (Ada.Command_Line.Argument (1));
262
Nico Huber1d0abe42017-03-05 14:14:09 +0100263 if iopl (3) /= 0 then
264 Debug.Put_Line ("Failed to change i/o privilege level.");
265 return;
266 end if;
267
Nico Huberfda2d6e2017-07-09 16:47:52 +0200268 Dev.Initialize (Dev_Init);
269 if not Dev_Init then
270 Debug.Put_Line ("Failed to map PCI config.");
Nico Huber1d0abe42017-03-05 14:14:09 +0100271 return;
272 end if;
273
Nico Huber3b654a02017-07-15 22:27:14 +0200274 Dev.Map (Res_Addr, PCI.Res0, Offset => Config.GTT_Offset);
275 if Res_Addr = 0 then
276 Debug.Put_Line ("Failed to map PCI resource0.");
277 return;
278 end if;
279 GTT.Set_Base_Address (Res_Addr);
280
Nico Huberfda2d6e2017-07-09 16:47:52 +0200281 Dev.Map (Res_Addr, PCI.Res2, WC => True);
282 if Res_Addr = 0 then
283 Debug.Put_Line ("Failed to map PCI resource2.");
284 return;
285 end if;
286 Screen.Set_Base_Address (Res_Addr);
287
Nico Huber1d0abe42017-03-05 14:14:09 +0100288 GMA.Initialize
Nico Huber2b6f6992017-07-09 18:11:34 +0200289 (Clean_State => True,
Nico Huber1d0abe42017-03-05 14:14:09 +0100290 Success => Initialized);
291
292 if Initialized then
Nico Huber3b654a02017-07-15 22:27:14 +0200293 Backup_GTT;
294
Nico Huber1d0abe42017-03-05 14:14:09 +0100295 Prepare_Configs;
296
297 GMA.Update_Outputs (Pipes);
298
299 for Pipe in GMA.Pipe_Index loop
300 if Pipes (Pipe).Port /= GMA.Disabled then
Nico Huber3b654a02017-07-15 22:27:14 +0200301 Backup_Screen (Pipes (Pipe).Framebuffer);
Nico Huber1d0abe42017-03-05 14:14:09 +0100302 Test_Screen
303 (Framebuffer => Pipes (Pipe).Framebuffer,
304 Pipe => Pipe);
305 end if;
306 end loop;
Nico Huber3b654a02017-07-15 22:27:14 +0200307
308 Time.M_Delay (Delay_S * 1_000);
309
310 for Pipe in GMA.Pipe_Index loop
311 if Pipes (Pipe).Port /= GMA.Disabled then
312 Restore_Screen (Pipes (Pipe).Framebuffer);
313 end if;
314 end loop;
315 Restore_GTT;
Nico Huber1d0abe42017-03-05 14:14:09 +0100316 end if;
317 end Main;
318
Nico Huberfda2d6e2017-07-09 16:47:52 +0200319end HW.GFX.GMA.GFX_Test;