| Nico Huber | fda2d6e | 2017-07-09 16:47:52 +0200 | [diff] [blame] | 1 | with Ada.Unchecked_Conversion; |
| Nico Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 2 | with Ada.Command_Line; |
| 3 | with Interfaces.C; |
| 4 | |
| Nico Huber | 3b654a0 | 2017-07-15 22:27:14 +0200 | [diff] [blame] | 5 | with HW.Time; |
| Nico Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 6 | with HW.Debug; |
| Nico Huber | fda2d6e | 2017-07-09 16:47:52 +0200 | [diff] [blame] | 7 | with HW.PCI.Dev; |
| 8 | with HW.MMIO_Range; |
| Nico Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 9 | with HW.GFX.GMA; |
| Nico Huber | 3b654a0 | 2017-07-15 22:27:14 +0200 | [diff] [blame] | 10 | with HW.GFX.GMA.Config; |
| Nico Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 11 | with HW.GFX.GMA.Display_Probing; |
| 12 | |
| Nico Huber | fda2d6e | 2017-07-09 16:47:52 +0200 | [diff] [blame] | 13 | package body HW.GFX.GMA.GFX_Test |
| 14 | is |
| 15 | pragma Disable_Atomic_Synchronization; |
| Nico Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 16 | |
| Nico Huber | fda2d6e | 2017-07-09 16:47:52 +0200 | [diff] [blame] | 17 | package Dev is new PCI.Dev (PCI.Address'(0, 2, 0)); |
| Nico Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 18 | |
| Nico Huber | 3b654a0 | 2017-07-15 22:27:14 +0200 | [diff] [blame] | 19 | 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 Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 45 | 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 Huber | 244ea7e | 2017-08-28 11:38:23 +0200 | [diff] [blame^] | 59 | 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 Huber | fda2d6e | 2017-07-09 16:47:52 +0200 | [diff] [blame] | 65 | 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 Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 74 | Max_W : constant := 4096; |
| 75 | Max_H : constant := 2160; |
| 76 | FB_Align : constant := 16#0004_0000#; |
| Nico Huber | fda2d6e | 2017-07-09 16:47:52 +0200 | [diff] [blame] | 77 | 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 Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 80 | |
| Nico Huber | fda2d6e | 2017-07-09 16:47:52 +0200 | [diff] [blame] | 81 | package Screen is new MMIO_Range (0, Word32, Screen_Index, Screen_Type); |
| Nico Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 82 | |
| Nico Huber | 3b654a0 | 2017-07-15 22:27:14 +0200 | [diff] [blame] | 83 | 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 Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 104 | |
| Nico Huber | 244ea7e | 2017-08-28 11:38:23 +0200 | [diff] [blame^] | 105 | 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 Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 130 | function Fill |
| 131 | (X, Y : Natural; |
| 132 | Framebuffer : Framebuffer_Type; |
| Nico Huber | 244ea7e | 2017-08-28 11:38:23 +0200 | [diff] [blame^] | 133 | Pipe : Pipe_Index) |
| Nico Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 134 | 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 Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 159 | 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 Huber | 244ea7e | 2017-08-28 11:38:23 +0200 | [diff] [blame^] | 167 | 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 Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 173 | else |
| 174 | P := Fill (X, Y, Framebuffer, Pipe); |
| 175 | end if; |
| Nico Huber | fda2d6e | 2017-07-09 16:47:52 +0200 | [diff] [blame] | 176 | Screen.Write (Offset, Pixel_To_Word (P)); |
| Nico Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 177 | 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 Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 188 | 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 Huber | b747049 | 2017-11-30 14:48:35 +0100 | [diff] [blame] | 195 | V_Stride => Height_Type (Mode.V_Visible), |
| Nico Huber | 51375ad | 2017-08-24 14:44:06 +0200 | [diff] [blame] | 196 | Tiling => Linear, |
| Nico Huber | b747049 | 2017-11-30 14:48:35 +0100 | [diff] [blame] | 197 | Rotation => No_Rotation, |
| Nico Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 198 | Offset => Offset); |
| Nico Huber | b747049 | 2017-11-30 14:48:35 +0100 | [diff] [blame] | 199 | Offset := Offset + Word32 (FB_Size (FB)); |
| Nico Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 200 | end Calc_Framebuffer; |
| 201 | |
| Nico Huber | 3b654a0 | 2017-07-15 22:27:14 +0200 | [diff] [blame] | 202 | Pipes : GMA.Pipe_Configs; |
| 203 | |
| Nico Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 204 | procedure Prepare_Configs |
| 205 | is |
| 206 | use type HW.GFX.GMA.Port_Type; |
| 207 | |
| Nico Huber | fda2d6e | 2017-07-09 16:47:52 +0200 | [diff] [blame] | 208 | Offset : Word32 := 0; |
| Nico Huber | 3b654a0 | 2017-07-15 22:27:14 +0200 | [diff] [blame] | 209 | Success : Boolean; |
| Nico Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 210 | 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 Huber | 3b654a0 | 2017-07-15 22:27:14 +0200 | [diff] [blame] | 219 | 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 Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 226 | end if; |
| 227 | end loop; |
| 228 | |
| 229 | GMA.Dump_Configs (Pipes); |
| 230 | end Prepare_Configs; |
| 231 | |
| Nico Huber | 3b654a0 | 2017-07-15 22:27:14 +0200 | [diff] [blame] | 232 | 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 Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 240 | procedure Main |
| 241 | is |
| Nico Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 242 | use type HW.GFX.GMA.Port_Type; |
| Nico Huber | fda2d6e | 2017-07-09 16:47:52 +0200 | [diff] [blame] | 243 | use type HW.Word64; |
| Nico Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 244 | use type Interfaces.C.int; |
| 245 | |
| Nico Huber | fda2d6e | 2017-07-09 16:47:52 +0200 | [diff] [blame] | 246 | Res_Addr : Word64; |
| 247 | |
| Nico Huber | 3b654a0 | 2017-07-15 22:27:14 +0200 | [diff] [blame] | 248 | Delay_S : Natural; |
| 249 | |
| Nico Huber | fda2d6e | 2017-07-09 16:47:52 +0200 | [diff] [blame] | 250 | Dev_Init, |
| Nico Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 251 | Initialized : Boolean; |
| 252 | |
| 253 | function iopl (level : Interfaces.C.int) return Interfaces.C.int; |
| 254 | pragma Import (C, iopl, "iopl"); |
| 255 | begin |
| Nico Huber | 3b654a0 | 2017-07-15 22:27:14 +0200 | [diff] [blame] | 256 | 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 Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 263 | if iopl (3) /= 0 then |
| 264 | Debug.Put_Line ("Failed to change i/o privilege level."); |
| 265 | return; |
| 266 | end if; |
| 267 | |
| Nico Huber | fda2d6e | 2017-07-09 16:47:52 +0200 | [diff] [blame] | 268 | Dev.Initialize (Dev_Init); |
| 269 | if not Dev_Init then |
| 270 | Debug.Put_Line ("Failed to map PCI config."); |
| Nico Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 271 | return; |
| 272 | end if; |
| 273 | |
| Nico Huber | 3b654a0 | 2017-07-15 22:27:14 +0200 | [diff] [blame] | 274 | 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 Huber | fda2d6e | 2017-07-09 16:47:52 +0200 | [diff] [blame] | 281 | 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 Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 288 | GMA.Initialize |
| Nico Huber | 2b6f699 | 2017-07-09 18:11:34 +0200 | [diff] [blame] | 289 | (Clean_State => True, |
| Nico Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 290 | Success => Initialized); |
| 291 | |
| 292 | if Initialized then |
| Nico Huber | 3b654a0 | 2017-07-15 22:27:14 +0200 | [diff] [blame] | 293 | Backup_GTT; |
| 294 | |
| Nico Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 295 | 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 Huber | 3b654a0 | 2017-07-15 22:27:14 +0200 | [diff] [blame] | 301 | Backup_Screen (Pipes (Pipe).Framebuffer); |
| Nico Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 302 | Test_Screen |
| 303 | (Framebuffer => Pipes (Pipe).Framebuffer, |
| 304 | Pipe => Pipe); |
| 305 | end if; |
| 306 | end loop; |
| Nico Huber | 3b654a0 | 2017-07-15 22:27:14 +0200 | [diff] [blame] | 307 | |
| 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 Huber | 1d0abe4 | 2017-03-05 14:14:09 +0100 | [diff] [blame] | 316 | end if; |
| 317 | end Main; |
| 318 | |
| Nico Huber | fda2d6e | 2017-07-09 16:47:52 +0200 | [diff] [blame] | 319 | end HW.GFX.GMA.GFX_Test; |