blob: ef6c91def5b98ce85d1d9556e900d936db122454 [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 Huberfda2d6e2017-07-09 16:47:52 +020059 function Pixel_To_Word (P : Pixel_Type) return Word32
60 with
61 SPARK_Mode => Off
62 is
63 function To_Word is new Ada.Unchecked_Conversion (Pixel_Type, Word32);
64 begin
65 return To_Word (P);
66 end Pixel_To_Word;
67
Nico Huber1d0abe42017-03-05 14:14:09 +010068 Max_W : constant := 4096;
69 Max_H : constant := 2160;
70 FB_Align : constant := 16#0004_0000#;
Nico Huberfda2d6e2017-07-09 16:47:52 +020071 subtype Screen_Index is Natural range
72 0 .. 3 * (Max_W * Max_H + FB_Align / 4) - 1;
73 type Screen_Type is array (Screen_Index) of Word32;
Nico Huber1d0abe42017-03-05 14:14:09 +010074
Nico Huberfda2d6e2017-07-09 16:47:52 +020075 package Screen is new MMIO_Range (0, Word32, Screen_Index, Screen_Type);
Nico Huber1d0abe42017-03-05 14:14:09 +010076
Nico Huber3b654a02017-07-15 22:27:14 +020077 Screen_Backup : Screen_Type;
78
79 procedure Backup_Screen (FB : Framebuffer_Type)
80 is
81 First : constant Screen_Index := Natural (FB.Offset) / 4;
82 Last : constant Screen_Index := First + Natural (FB_Size (FB)) / 4 - 1;
83 begin
84 for Idx in Screen_Index range First .. Last loop
85 Screen.Read (Screen_Backup (Idx), Idx);
86 end loop;
87 end Backup_Screen;
88
89 procedure Restore_Screen (FB : Framebuffer_Type)
90 is
91 First : constant Screen_Index := Natural (FB.Offset) / 4;
92 Last : constant Screen_Index := First + Natural (FB_Size (FB)) / 4 - 1;
93 begin
94 for Idx in Screen_Index range First .. Last loop
95 Screen.Write (Idx, Screen_Backup (Idx));
96 end loop;
97 end Restore_Screen;
Nico Huber1d0abe42017-03-05 14:14:09 +010098
99 function Fill
100 (X, Y : Natural;
101 Framebuffer : Framebuffer_Type;
102 Pipe : GMA.Pipe_Index)
103 return Pixel_Type
104 is
105 use type HW.Byte;
106
107 Xp : constant Natural := X * 256 / Natural (Framebuffer.Width);
108 Yp : constant Natural := Y * 256 / Natural (Framebuffer.Height);
109 Xn : constant Natural := 255 - Xp;
110 Yn : constant Natural := 255 - Yp;
111
112 function Map (X, Y : Natural) return Byte is
113 begin
114 return Byte (X * Y / 255);
115 end Map;
116 begin
117 return
118 (case Pipe is
119 when GMA.Primary => (Map (Xn, Yn), Map (Xp, Yn), Map (Xp, Yp), 255),
120 when GMA.Secondary => (Map (Xn, Yp), Map (Xn, Yn), Map (Xp, Yn), 255),
121 when GMA.Tertiary => (Map (Xp, Yp), Map (Xn, Yp), Map (Xn, Yn), 255));
122 end Fill;
123
124 procedure Test_Screen
125 (Framebuffer : Framebuffer_Type;
126 Pipe : GMA.Pipe_Index)
127 is
Nico Huber1d0abe42017-03-05 14:14:09 +0100128 P : Pixel_Type;
129 -- We have pixel offset wheras the framebuffer has a byte offset
130 Offset_Y : Natural := Natural (Framebuffer.Offset / 4);
131 Offset : Natural;
132 begin
133 for Y in 0 .. Natural (Framebuffer.Height) - 1 loop
134 Offset := Offset_Y;
135 for X in 0 .. Natural (Framebuffer.Width) - 1 loop
136 if Y mod 16 = 0 or X mod 16 = 0 then
137 P := (0, 0, 0, 0);
138 else
139 P := Fill (X, Y, Framebuffer, Pipe);
140 end if;
Nico Huberfda2d6e2017-07-09 16:47:52 +0200141 Screen.Write (Offset, Pixel_To_Word (P));
Nico Huber1d0abe42017-03-05 14:14:09 +0100142 Offset := Offset + 1;
143 end loop;
144 Offset_Y := Offset_Y + Natural (Framebuffer.Stride);
145 end loop;
146 end Test_Screen;
147
148 procedure Calc_Framebuffer
149 (FB : out Framebuffer_Type;
150 Mode : in Mode_Type;
151 Offset : in out Word32)
152 is
Nico Huber1d0abe42017-03-05 14:14:09 +0100153 begin
154 Offset := (Offset + FB_Align - 1) and not (FB_Align - 1);
155 FB :=
156 (Width => Width_Type (Mode.H_Visible),
157 Height => Height_Type (Mode.V_Visible),
158 BPC => 8,
159 Stride => Width_Type ((Word32 (Mode.H_Visible) + 15) and not 15),
Nico Huber51375ad2017-08-24 14:44:06 +0200160 Tiling => Linear,
Nico Huber1d0abe42017-03-05 14:14:09 +0100161 Offset => Offset);
162 Offset := Offset + Word32 (FB.Stride * FB.Height * 4);
163 end Calc_Framebuffer;
164
Nico Huber3b654a02017-07-15 22:27:14 +0200165 Pipes : GMA.Pipe_Configs;
166
Nico Huber1d0abe42017-03-05 14:14:09 +0100167 procedure Prepare_Configs
168 is
169 use type HW.GFX.GMA.Port_Type;
170
Nico Huberfda2d6e2017-07-09 16:47:52 +0200171 Offset : Word32 := 0;
Nico Huber3b654a02017-07-15 22:27:14 +0200172 Success : Boolean;
Nico Huber1d0abe42017-03-05 14:14:09 +0100173 begin
174 GMA.Display_Probing.Scan_Ports (Pipes);
175
176 for Pipe in GMA.Pipe_Index loop
177 if Pipes (Pipe).Port /= GMA.Disabled then
178 Calc_Framebuffer
179 (FB => Pipes (Pipe).Framebuffer,
180 Mode => Pipes (Pipe).Mode,
181 Offset => Offset);
Nico Huber3b654a02017-07-15 22:27:14 +0200182 GMA.Setup_Default_FB
183 (FB => Pipes (Pipe).Framebuffer,
184 Clear => False,
185 Success => Success);
186 if not Success then
187 Pipes (Pipe).Port := GMA.Disabled;
188 end if;
Nico Huber1d0abe42017-03-05 14:14:09 +0100189 end if;
190 end loop;
191
192 GMA.Dump_Configs (Pipes);
193 end Prepare_Configs;
194
Nico Huber3b654a02017-07-15 22:27:14 +0200195 procedure Print_Usage
196 is
197 begin
198 Debug.Put_Line
199 ("Usage: " & Ada.Command_Line.Command_Name & " <delay seconds>");
200 Debug.New_Line;
201 end Print_Usage;
202
Nico Huber1d0abe42017-03-05 14:14:09 +0100203 procedure Main
204 is
Nico Huber1d0abe42017-03-05 14:14:09 +0100205 use type HW.GFX.GMA.Port_Type;
Nico Huberfda2d6e2017-07-09 16:47:52 +0200206 use type HW.Word64;
Nico Huber1d0abe42017-03-05 14:14:09 +0100207 use type Interfaces.C.int;
208
Nico Huberfda2d6e2017-07-09 16:47:52 +0200209 Res_Addr : Word64;
210
Nico Huber3b654a02017-07-15 22:27:14 +0200211 Delay_S : Natural;
212
Nico Huberfda2d6e2017-07-09 16:47:52 +0200213 Dev_Init,
Nico Huber1d0abe42017-03-05 14:14:09 +0100214 Initialized : Boolean;
215
216 function iopl (level : Interfaces.C.int) return Interfaces.C.int;
217 pragma Import (C, iopl, "iopl");
218 begin
Nico Huber3b654a02017-07-15 22:27:14 +0200219 if Ada.Command_Line.Argument_Count /= 1 then
220 Print_Usage;
221 return;
222 end if;
223
224 Delay_S := Natural'Value (Ada.Command_Line.Argument (1));
225
Nico Huber1d0abe42017-03-05 14:14:09 +0100226 if iopl (3) /= 0 then
227 Debug.Put_Line ("Failed to change i/o privilege level.");
228 return;
229 end if;
230
Nico Huberfda2d6e2017-07-09 16:47:52 +0200231 Dev.Initialize (Dev_Init);
232 if not Dev_Init then
233 Debug.Put_Line ("Failed to map PCI config.");
Nico Huber1d0abe42017-03-05 14:14:09 +0100234 return;
235 end if;
236
Nico Huber3b654a02017-07-15 22:27:14 +0200237 Dev.Map (Res_Addr, PCI.Res0, Offset => Config.GTT_Offset);
238 if Res_Addr = 0 then
239 Debug.Put_Line ("Failed to map PCI resource0.");
240 return;
241 end if;
242 GTT.Set_Base_Address (Res_Addr);
243
Nico Huberfda2d6e2017-07-09 16:47:52 +0200244 Dev.Map (Res_Addr, PCI.Res2, WC => True);
245 if Res_Addr = 0 then
246 Debug.Put_Line ("Failed to map PCI resource2.");
247 return;
248 end if;
249 Screen.Set_Base_Address (Res_Addr);
250
Nico Huber1d0abe42017-03-05 14:14:09 +0100251 GMA.Initialize
Nico Huber2b6f6992017-07-09 18:11:34 +0200252 (Clean_State => True,
Nico Huber1d0abe42017-03-05 14:14:09 +0100253 Success => Initialized);
254
255 if Initialized then
Nico Huber3b654a02017-07-15 22:27:14 +0200256 Backup_GTT;
257
Nico Huber1d0abe42017-03-05 14:14:09 +0100258 Prepare_Configs;
259
260 GMA.Update_Outputs (Pipes);
261
262 for Pipe in GMA.Pipe_Index loop
263 if Pipes (Pipe).Port /= GMA.Disabled then
Nico Huber3b654a02017-07-15 22:27:14 +0200264 Backup_Screen (Pipes (Pipe).Framebuffer);
Nico Huber1d0abe42017-03-05 14:14:09 +0100265 Test_Screen
266 (Framebuffer => Pipes (Pipe).Framebuffer,
267 Pipe => Pipe);
268 end if;
269 end loop;
Nico Huber3b654a02017-07-15 22:27:14 +0200270
271 Time.M_Delay (Delay_S * 1_000);
272
273 for Pipe in GMA.Pipe_Index loop
274 if Pipes (Pipe).Port /= GMA.Disabled then
275 Restore_Screen (Pipes (Pipe).Framebuffer);
276 end if;
277 end loop;
278 Restore_GTT;
Nico Huber1d0abe42017-03-05 14:14:09 +0100279 end if;
280 end Main;
281
Nico Huberfda2d6e2017-07-09 16:47:52 +0200282end HW.GFX.GMA.GFX_Test;